execute.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*!
  2. \file db/driver/postgres/execute.c
  3. \brief DBMI - Low Level PostgreSQL database driver - execute statemets
  4. This program is free software under the GNU General Public License
  5. (>=v2). Read the file COPYING that comes with GRASS for details.
  6. \author Radim Blazek
  7. */
  8. #include <stdlib.h>
  9. #include <grass/dbmi.h>
  10. #include <grass/gis.h>
  11. #include <grass/glocale.h>
  12. #include "globals.h"
  13. #include "proto.h"
  14. int db__driver_execute_immediate(dbString * sql)
  15. {
  16. PGresult *res;
  17. char *str;
  18. /* Postgres supports in addition to standard escape character '
  19. * (apostrophe) also \ (basckslash) as this is not SQL standard,
  20. * GRASS modules cannot work escape all \ in the text because
  21. * other drivers do not support this feature. For example, if a
  22. * text contains string \' GRASS modules escape ' by another ' and
  23. * string passed to driver is \'' postgres takes \' as ' but
  24. * second ' remains not escaped, result is error. Because of
  25. * this, all occurrences of \ in sql are escaped by \ */
  26. str = G_str_replace(db_get_string(sql), "\\", "\\\\");
  27. G_debug(3, "db__driver_execute_immediate(): Escaped SQL: '%s'", str);
  28. res = PQexec(pg_conn, str);
  29. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  30. db_d_append_error("%s\n%s\n%s",
  31. _("Unable to execute:"),
  32. str,
  33. PQerrorMessage(pg_conn));
  34. db_d_report_error();
  35. PQclear(res);
  36. if (str)
  37. G_free(str);
  38. return DB_FAILED;
  39. }
  40. if (str)
  41. G_free(str);
  42. PQclear(res);
  43. return DB_OK;
  44. }
  45. int db__driver_begin_transaction(void)
  46. {
  47. PGresult *res;
  48. G_debug(2, "pg : BEGIN");
  49. res = PQexec(pg_conn, "BEGIN");
  50. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  51. db_d_append_error(_("Unable to 'BEGIN' transaction"));
  52. db_d_report_error();
  53. PQclear(res);
  54. return DB_FAILED;
  55. }
  56. PQclear(res);
  57. return DB_OK;
  58. }
  59. int db__driver_commit_transaction(void)
  60. {
  61. PGresult *res;
  62. G_debug(2, "pg : COMMIT");
  63. res = PQexec(pg_conn, "COMMIT");
  64. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  65. db_d_append_error(_("Unable to 'COMMIT' transaction"));
  66. db_d_report_error();
  67. PQclear(res);
  68. return DB_FAILED;
  69. }
  70. PQclear(res);
  71. return DB_OK;
  72. }