execute.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**********************************************************
  2. * MODULE: mysql
  3. * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  4. * PURPOSE: MySQL database driver
  5. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  6. * This program is free software under the
  7. * GNU General Public License (>=v2).
  8. * Read the file COPYING that comes with GRASS
  9. * for details.
  10. **********************************************************/
  11. #include <stdlib.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. #include "globals.h"
  16. #include "proto.h"
  17. int db__driver_execute_immediate(dbString * sql)
  18. {
  19. char *str;
  20. init_error();
  21. /* In addition to standard escape character ' (apostrophe)
  22. * MySQL supports also \ (backslash). Because this is not SQL
  23. * standard, GRASS modules cannot escape all \ in the text
  24. * because other drivers do not support this feature.
  25. * For example, if a text contains string \' GRASS modules
  26. * escape ' by another ' and the string passed to the driver is \''
  27. * MySQL converts \' to ' but second ' remains not escaped and
  28. * result is error.
  29. * Because of this, all occurencies of \ in sql must be
  30. * escaped by \ */
  31. str = G_str_replace(db_get_string(sql), "\\", "\\\\");
  32. G_debug(3, "Escaped SQL: %s", str);
  33. if (mysql_query(connection, str) != 0) {
  34. append_error("Cannot execute: \n");
  35. append_error(str);
  36. append_error("\n");
  37. append_error(mysql_error(connection));
  38. report_error();
  39. if (str)
  40. G_free(str);
  41. return DB_FAILED;
  42. }
  43. if (str)
  44. G_free(str);
  45. return DB_OK;
  46. }
  47. int db__driver_begin_transaction(void)
  48. {
  49. G_debug(2, "mysql: START TRANSACTION");
  50. init_error();
  51. if (mysql_query(connection, "START TRANSACTION") != 0) {
  52. append_error("Cannot start transaction: \n");
  53. append_error(mysql_error(connection));
  54. report_error();
  55. return DB_FAILED;
  56. }
  57. return DB_OK;
  58. }
  59. int db__driver_commit_transaction(void)
  60. {
  61. G_debug(2, "mysql: COMMIT");
  62. init_error();
  63. if (mysql_query(connection, "COMMIT") != 0) {
  64. append_error("Cannot commit transaction: \n");
  65. append_error(mysql_error(connection));
  66. report_error();
  67. return DB_FAILED;
  68. }
  69. return DB_OK;
  70. }