rewind_pg.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*!
  2. \file lib/vector/Vlib/rewind_pg.c
  3. \brief Vector library - rewind data (PostGIS layers)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2011-2012 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 <grass/vector.h>
  11. #include <grass/glocale.h>
  12. #ifdef HAVE_POSTGRES
  13. #include "pg_local_proto.h"
  14. #endif
  15. /*!
  16. \brief Rewind vector map (PostGIS layer) to cause reads to start
  17. at beginning (level 1)
  18. \param Map pointer to Map_info structure
  19. \return 0 on success
  20. \return -1 on error
  21. */
  22. int V1_rewind_pg(struct Map_info *Map)
  23. {
  24. G_debug(2, "V1_rewind_pg(): name = %s", Map->name);
  25. #ifdef HAVE_POSTGRES
  26. struct Format_info_pg *pg_info;
  27. pg_info = &(Map->fInfo.pg);
  28. /* reset reading */
  29. pg_info->next_line = 0;
  30. /* reset cache */
  31. pg_info->cache.lines_num = pg_info->cache.lines_next = 0;
  32. pg_info->cache.fid = -1;
  33. /* close DB cursor if necessary */
  34. if (pg_info->res) {
  35. char stmt[DB_SQL_MAX];
  36. PQclear(pg_info->res);
  37. pg_info->res = NULL;
  38. sprintf(stmt, "CLOSE %s_%s%p",
  39. pg_info->schema_name, pg_info->table_name, pg_info->conn);
  40. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  41. G_warning(_("Unable to close cursor"));
  42. return -1;
  43. }
  44. Vect__execute_pg(pg_info->conn, "COMMIT");
  45. }
  46. return 0;
  47. #else
  48. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  49. return -1;
  50. #endif
  51. }
  52. /*!
  53. \brief Rewind vector map (PostGIS layer) to cause reads to start
  54. at beginning on topological level (level 2)
  55. \param Map pointer to Map_info structure
  56. \return 0 on success
  57. \return -1 on error
  58. */
  59. int V2_rewind_pg(struct Map_info *Map)
  60. {
  61. G_debug(2, "V2_rewind_pg(): name = %s", Map->name);
  62. #ifdef HAVE_POSTGRES
  63. /* reset reading */
  64. Map->next_line = 1;
  65. V1_rewind_pg(Map);
  66. return 0;
  67. #else
  68. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  69. return -1;
  70. #endif
  71. }