execute.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <grass/glocale.h>
  18. #include "globals.h"
  19. #include "proto.h"
  20. int db__driver_execute_immediate(dbString * sql)
  21. {
  22. PGresult *res;
  23. char *str;
  24. init_error();
  25. /* Postgres supports in addition to standard escape character ' (apostrophe) also \ (basckslash)
  26. * as this is not SQL standard, GRASS modules cannot work escape all \ in the text
  27. * because other drivers do not support this feature. For example, if a text contains
  28. * string \' GRASS modules escape ' by another ' and string passed to driver is \''
  29. * postgres takes \' as ' but second ' remains not escaped, result is error.
  30. * Because of this, all occurencies of \ in sql are escaped by \ */
  31. str = G_str_replace(db_get_string(sql), "\\", "\\\\");
  32. G_debug(3, "Escaped SQL: %s", str);
  33. res = PQexec(pg_conn, str);
  34. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  35. append_error(_("Unable to execute:\n"));
  36. append_error(str);
  37. append_error("\n");
  38. append_error(PQerrorMessage(pg_conn));
  39. report_error();
  40. PQclear(res);
  41. if (str)
  42. G_free(str);
  43. return DB_FAILED;
  44. }
  45. if (str)
  46. G_free(str);
  47. PQclear(res);
  48. return DB_OK;
  49. }
  50. int db__driver_begin_transaction(void)
  51. {
  52. PGresult *res;
  53. G_debug(2, "pg : BEGIN");
  54. init_error();
  55. res = PQexec(pg_conn, "BEGIN");
  56. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  57. append_error(_("Unable to 'BEGIN' transaction"));
  58. report_error();
  59. PQclear(res);
  60. return DB_FAILED;
  61. }
  62. PQclear(res);
  63. return DB_OK;
  64. }
  65. int db__driver_commit_transaction(void)
  66. {
  67. PGresult *res;
  68. G_debug(2, "pg : COMMIT");
  69. init_error();
  70. res = PQexec(pg_conn, "COMMIT");
  71. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  72. append_error(_("Unable to 'COMMIT' transaction"));
  73. report_error();
  74. PQclear(res);
  75. return DB_FAILED;
  76. }
  77. PQclear(res);
  78. return DB_OK;
  79. }