close_pg.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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%p",
  40. 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. /* free allocated space */
  49. for (i = 0; i < pg_info->cache.lines_alloc; i++) {
  50. Vect_destroy_line_struct(pg_info->cache.lines[i]);
  51. }
  52. if (pg_info->cache.lines)
  53. G_free(pg_info->cache.lines);
  54. if (pg_info->db_name)
  55. G_free(pg_info->db_name);
  56. if (pg_info->geom_column)
  57. G_free(pg_info->geom_column);
  58. if (pg_info->fid_column)
  59. G_free(pg_info->fid_column);
  60. return 0;
  61. #else
  62. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  63. return -1;
  64. #endif
  65. }
  66. /*!
  67. \brief Close vector map (PostGIS layer) on topological level (write out fidx file)
  68. \param Map pointer to Map_info structure
  69. \return 0 on success
  70. \return non-zero on error
  71. */
  72. int V2_close_pg(struct Map_info *Map)
  73. {
  74. #ifdef HAVE_POSTGRES
  75. G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
  76. if (!VECT_OPEN(Map))
  77. return -1;
  78. /* write fidx for maps in the current mapset */
  79. if (Vect_save_fidx(Map, &(Map->fInfo.pg.offset)) != 1)
  80. G_warning(_("Unable to save feature index file for vector map <%s>"),
  81. Map->name);
  82. G_free(Map->fInfo.pg.offset.array);
  83. return 0;
  84. #else
  85. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  86. return -1;
  87. #endif
  88. }