execute.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /* In addition to standard escape character ' (apostrophe)
  21. * MySQL supports also \ (backslash). Because this is not SQL
  22. * standard, GRASS modules cannot escape all \ in the text
  23. * because other drivers do not support this feature.
  24. * For example, if a text contains string \' GRASS modules
  25. * escape ' by another ' and the string passed to the driver is \''
  26. * MySQL converts \' to ' but second ' remains not escaped and
  27. * result is error.
  28. * Because of this, all occurrences of \ in sql must be
  29. * escaped by \ */
  30. str = G_str_replace(db_get_string(sql), "\\", "\\\\");
  31. G_debug(3, "Escaped SQL: %s", str);
  32. if (mysql_query(connection, str) != 0) {
  33. db_d_append_error("%s\n%s\n%s",
  34. _("Unable to execute:"),
  35. str,
  36. mysql_error(connection));
  37. db_d_report_error();
  38. if (str)
  39. G_free(str);
  40. return DB_FAILED;
  41. }
  42. if (str)
  43. G_free(str);
  44. return DB_OK;
  45. }
  46. int db__driver_begin_transaction(void)
  47. {
  48. G_debug(2, "mysql: START TRANSACTION");
  49. if (mysql_query(connection, "START TRANSACTION") != 0) {
  50. db_d_append_error("%s %s",
  51. _("Unable to start transaction:"),
  52. mysql_error(connection));
  53. db_d_report_error();
  54. return DB_FAILED;
  55. }
  56. return DB_OK;
  57. }
  58. int db__driver_commit_transaction(void)
  59. {
  60. G_debug(2, "mysql: COMMIT");
  61. if (mysql_query(connection, "COMMIT") != 0) {
  62. db_d_append_error("%s %s",
  63. _("Unable to commit transaction:"),
  64. mysql_error(connection));
  65. db_d_report_error();
  66. return DB_FAILED;
  67. }
  68. return DB_OK;
  69. }