close_pg.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <grass/vector.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/glocale.h>
  14. #ifdef HAVE_POSTGRES
  15. #include "pg_local_proto.h"
  16. #endif
  17. /*!
  18. \brief Close vector map (PostGIS layer) on level 1
  19. \param Map pointer to Map_info structure
  20. \return 0 on success
  21. \return non-zero on error
  22. */
  23. int V1_close_pg(struct Map_info *Map)
  24. {
  25. #ifdef HAVE_POSTGRES
  26. int i;
  27. struct Format_info_pg *pg_info;
  28. G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
  29. if (!VECT_OPEN(Map))
  30. return -1;
  31. pg_info = &(Map->fInfo.pg);
  32. if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)
  33. Vect__write_head(Map);
  34. /* close connection */
  35. if (pg_info->res) {
  36. char stmt[DB_SQL_MAX];
  37. PQclear(pg_info->res);
  38. pg_info->res = NULL;
  39. sprintf(stmt, "CLOSE %s_%s%p",
  40. pg_info->schema_name, pg_info->table_name, pg_info->conn);
  41. if (execute(pg_info->conn, stmt) == -1) {
  42. G_warning(_("Unable to close cursor"));
  43. return -1;
  44. }
  45. execute(pg_info->conn, "COMMIT");
  46. }
  47. PQfinish(pg_info->conn);
  48. /* close DB connection (for atgtributes) */
  49. if (pg_info->dbdriver) {
  50. db_close_database_shutdown_driver(pg_info->dbdriver);
  51. }
  52. /* free allocated space */
  53. for (i = 0; i < pg_info->cache.lines_alloc; i++) {
  54. Vect_destroy_line_struct(pg_info->cache.lines[i]);
  55. }
  56. if (pg_info->cache.lines)
  57. G_free(pg_info->cache.lines);
  58. G_free(pg_info->db_name);
  59. G_free(pg_info->schema_name);
  60. G_free(pg_info->geom_column);
  61. G_free(pg_info->fid_column);
  62. if (pg_info->toposchema_name)
  63. G_free(pg_info->toposchema_name);
  64. if (pg_info->topogeom_column)
  65. G_free(pg_info->topogeom_column);
  66. return 0;
  67. #else
  68. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  69. return -1;
  70. #endif
  71. }
  72. /*!
  73. \brief Close vector map (PostGIS layer) on topological level (write out fidx file)
  74. \param Map pointer to Map_info structure
  75. \return 0 on success
  76. \return non-zero on error
  77. */
  78. int V2_close_pg(struct Map_info *Map)
  79. {
  80. #ifdef HAVE_POSTGRES
  81. G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
  82. if (!VECT_OPEN(Map))
  83. return -1;
  84. /* write fidx for maps in the current mapset */
  85. if (Vect_save_fidx(Map, &(Map->fInfo.pg.offset)) != 1)
  86. G_warning(_("Unable to save feature index file for vector map <%s>"),
  87. Map->name);
  88. G_free(Map->fInfo.pg.offset.array);
  89. return 0;
  90. #else
  91. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  92. return -1;
  93. #endif
  94. }