execute.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. {
  35. append_error( "Cannot execute: \n" );
  36. append_error( str );
  37. append_error( "\n" );
  38. append_error ( mysql_error(connection) );
  39. report_error();
  40. if ( str )
  41. G_free ( str );
  42. return DB_FAILED;
  43. }
  44. if ( str )
  45. G_free ( str );
  46. return DB_OK;
  47. }
  48. int db__driver_begin_transaction(void)
  49. {
  50. G_debug (2, "mysql: START TRANSACTION");
  51. init_error();
  52. if ( mysql_query ( connection, "START TRANSACTION" ) != 0 )
  53. {
  54. append_error( "Cannot start transaction: \n" );
  55. append_error ( mysql_error(connection) );
  56. report_error();
  57. return DB_FAILED;
  58. }
  59. return DB_OK;
  60. }
  61. int db__driver_commit_transaction(void)
  62. {
  63. G_debug (2, "mysql: COMMIT");
  64. init_error();
  65. if ( mysql_query ( connection, "COMMIT" ) != 0 )
  66. {
  67. append_error( "Cannot commit transaction: \n" );
  68. append_error ( mysql_error(connection) );
  69. report_error();
  70. return DB_FAILED;
  71. }
  72. return DB_OK;
  73. }