execute.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /****************************************************************************
  2. *
  3. * MODULE: execute
  4. * AUTHOR(S): Alex Shevlakov <sixote yahoo.com> (original contributor)
  5. * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Radim Blazek <radim.blazek gmail.com>
  6. * PURPOSE: PostgreSQL driver
  7. * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/gis.h>
  17. #include "globals.h"
  18. #include "proto.h"
  19. int db__driver_execute_immediate(dbString * sql)
  20. {
  21. PGresult *res;
  22. char *str;
  23. init_error();
  24. /* Postgres supports in addition to standard escape character ' (apostrophe) also \ (basckslash)
  25. * as this is not SQL standard, GRASS modules cannot work escape all \ in the text
  26. * because other drivers do not support this feature. For example, if a text contains
  27. * string \' GRASS modules escape ' by another ' and string passed to driver is \''
  28. * postgres takes \' as ' but second ' remains not escaped, result is error.
  29. * Because of this, all occurencies of \ in sql are escaped by \ */
  30. str = G_str_replace(db_get_string(sql), "\\", "\\\\");
  31. G_debug(3, "Escaped SQL: %s", str);
  32. res = PQexec(pg_conn, str);
  33. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  34. append_error("Cannot execute: \n");
  35. append_error(str);
  36. append_error("\n");
  37. append_error(PQerrorMessage(pg_conn));
  38. report_error();
  39. PQclear(res);
  40. if (str)
  41. G_free(str);
  42. return DB_FAILED;
  43. }
  44. if (str)
  45. G_free(str);
  46. PQclear(res);
  47. return DB_OK;
  48. }
  49. int db__driver_begin_transaction(void)
  50. {
  51. PGresult *res;
  52. G_debug(2, "pg : BEGIN");
  53. init_error();
  54. res = PQexec(pg_conn, "BEGIN");
  55. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  56. append_error("Cannot 'BEGIN' transaction");
  57. report_error();
  58. PQclear(res);
  59. return DB_FAILED;
  60. }
  61. PQclear(res);
  62. return DB_OK;
  63. }
  64. int db__driver_commit_transaction(void)
  65. {
  66. PGresult *res;
  67. G_debug(2, "pg : COMMIT");
  68. init_error();
  69. res = PQexec(pg_conn, "COMMIT");
  70. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  71. append_error("Cannot 'COMMIT' transaction");
  72. report_error();
  73. PQclear(res);
  74. return DB_FAILED;
  75. }
  76. PQclear(res);
  77. return DB_OK;
  78. }