close_pg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*!
  2. \file lib/vector/Vlib/close_pg.c
  3. \brief Vector library - Close map (PostGIS)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2011 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <grass/vector.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. #ifdef HAVE_POSTGRES
  16. #include "pg_local_proto.h"
  17. #endif
  18. /*!
  19. \brief Close vector map (PostGIS layer) on level 1
  20. \param Map pointer to Map_info structure
  21. \return 0 on success
  22. \return non-zero on error
  23. */
  24. int V1_close_pg(struct Map_info *Map)
  25. {
  26. #ifdef HAVE_POSTGRES
  27. int i;
  28. struct Format_info_pg *pg_info;
  29. G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
  30. if (!VECT_OPEN(Map))
  31. return -1;
  32. pg_info = &(Map->fInfo.pg);
  33. if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
  34. /* write header */
  35. Vect__write_head(Map);
  36. /* write frmt file for created PG-link */
  37. Vect_save_frmt(Map);
  38. }
  39. /* clear result */
  40. if (pg_info->res) {
  41. PQclear(pg_info->res);
  42. pg_info->res = NULL;
  43. }
  44. /* close open cursor */
  45. if (pg_info->cursor_name) {
  46. char stmt[DB_SQL_MAX];
  47. sprintf(stmt, "CLOSE %s", pg_info->cursor_name);
  48. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  49. G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
  50. return -1;
  51. }
  52. Vect__execute_pg(pg_info->conn, "COMMIT");
  53. G_free(pg_info->cursor_name);
  54. pg_info->cursor_name = NULL;
  55. }
  56. PQfinish(pg_info->conn);
  57. /* close DB connection (for atgtributes) */
  58. if (pg_info->dbdriver) {
  59. db_close_database_shutdown_driver(pg_info->dbdriver);
  60. }
  61. /* free allocated space */
  62. for (i = 0; i < pg_info->cache.lines_alloc; i++) {
  63. Vect_destroy_line_struct(pg_info->cache.lines[i]);
  64. }
  65. if (pg_info->cache.lines)
  66. G_free(pg_info->cache.lines);
  67. G_free(pg_info->db_name);
  68. G_free(pg_info->schema_name);
  69. G_free(pg_info->geom_column);
  70. G_free(pg_info->fid_column);
  71. if (pg_info->toposchema_name)
  72. G_free(pg_info->toposchema_name);
  73. if (pg_info->topogeom_column)
  74. G_free(pg_info->topogeom_column);
  75. return 0;
  76. #else
  77. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  78. return -1;
  79. #endif
  80. }
  81. /*!
  82. \brief Close vector map (PostGIS layer) on topological level (write out fidx file)
  83. \param Map pointer to Map_info structure
  84. \return 0 on success
  85. \return non-zero on error
  86. */
  87. int V2_close_pg(struct Map_info *Map)
  88. {
  89. #ifdef HAVE_POSTGRES
  90. G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
  91. if (!VECT_OPEN(Map))
  92. return -1;
  93. if (Map->fInfo.pg.toposchema_name) {
  94. /* no fidx file for PostGIS topology
  95. remove topo file (which was required for saving sidx file)
  96. */
  97. char buf[GPATH_MAX];
  98. char file_path[GPATH_MAX];
  99. /* delete old support files if available */
  100. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  101. G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  102. if (access(file_path, F_OK) == 0) /* file exists? */
  103. unlink(file_path);
  104. return 0;
  105. }
  106. /* write fidx for maps in the current mapset */
  107. if (Vect_save_fidx(Map, &(Map->fInfo.pg.offset)) != 1)
  108. G_warning(_("Unable to save feature index file for vector map <%s>"),
  109. Map->name);
  110. G_free(Map->fInfo.pg.offset.array);
  111. return 0;
  112. #else
  113. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  114. return -1;
  115. #endif
  116. }