GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
close_pg.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/close_pg.c
3 
4  \brief Vector library - Close map (PostGIS)
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2011 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Martin Landa <landa.martin gmail.com>
14  */
15 
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <grass/vector.h>
19 #include <grass/dbmi.h>
20 #include <grass/glocale.h>
21 
22 #include "local_proto.h"
23 
24 #ifdef HAVE_POSTGRES
25 #include "pg_local_proto.h"
26 #define NOPG_UNUSED
27 #else
28 #define NOPG_UNUSED UNUSED
29 #endif
30 
31 /*!
32  \brief Close vector map (PostGIS layer) on level 1
33 
34  \param Map pointer to Map_info structure
35 
36  \return 0 on success
37  \return non-zero on error
38  */
40 {
41 #ifdef HAVE_POSTGRES
42  struct Format_info_pg *pg_info;
43 
44  G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
45 
46  if (!VECT_OPEN(Map))
47  return -1;
48 
49  pg_info = &(Map->fInfo.pg);
50  if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
51  /* write header */
52  Vect__write_head(Map);
53  /* write frmt file for created PG-link */
54  Vect_save_frmt(Map);
55  }
56 
57  /* clear result */
58  if (pg_info->res) {
59  PQclear(pg_info->res);
60  pg_info->res = NULL;
61  }
62 
63  /* close open cursor */
64  if (pg_info->cursor_name) {
65  char stmt[DB_SQL_MAX];
66 
67  sprintf(stmt, "CLOSE %s", pg_info->cursor_name);
68  if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
69  G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
70  return -1;
71  }
72  Vect__execute_pg(pg_info->conn, "COMMIT");
73  G_free(pg_info->cursor_name);
74  pg_info->cursor_name = NULL;
75  }
76 
77  PQfinish(pg_info->conn);
78 
79  /* close DB connection (for atgtributes) */
80  if (pg_info->dbdriver) {
82  }
83 
84  Vect__free_cache(&(pg_info->cache));
85 
86  G_free(pg_info->db_name);
87  G_free(pg_info->schema_name);
88  G_free(pg_info->geom_column);
89  G_free(pg_info->fid_column);
90 
91  if (pg_info->fi)
92  G_free(pg_info->fi);
93 
94  if (pg_info->toposchema_name)
95  G_free(pg_info->toposchema_name);
96 
97  if (pg_info->topogeom_column)
98  G_free(pg_info->topogeom_column);
99 
100  return 0;
101 #else
102  G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
103  return -1;
104 #endif
105 }
106 
107 /*!
108  \brief Close vector map (PostGIS layer) on topological level (write out fidx
109  file)
110 
111  \param Map pointer to Map_info structure
112 
113  \return 0 on success
114  \return non-zero on error
115  */
117 {
118 #ifdef HAVE_POSTGRES
119  G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
120 
121  if (!VECT_OPEN(Map))
122  return -1;
123 
124  if (Map->fInfo.pg.toposchema_name) {
125  /* no fidx file for PostGIS topology
126 
127  remove topo file (which was required for saving sidx file)
128  */
129  char buf[GPATH_MAX];
130  char file_path[GPATH_MAX];
131 
132  /* delete old support files if available */
133  sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
134  Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT);
135  if (access(file_path, F_OK) == 0) /* file exists? */
136  unlink(file_path);
137 
138  return 0;
139  }
140 
141  /* write fidx for maps in the current mapset */
142  if (Vect_save_fidx(Map, &(Map->fInfo.pg.offset)) != 1)
143  G_warning(_("Unable to save feature index file for vector map <%s>"),
144  Map->name);
145 
146  Vect__free_offset(&(Map->fInfo.pg.offset));
147 
148  return 0;
149 #else
150  G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
151  return -1;
152 #endif
153 }
#define NULL
Definition: ccmath.h:32
#define NOPG_UNUSED
Definition: close_pg.c:26
int V2_close_pg(struct Map_info *Map NOPG_UNUSED)
Close vector map (PostGIS layer) on topological level (write out fidx file)
Definition: close_pg.c:116
int V1_close_pg(struct Map_info *Map NOPG_UNUSED)
Close vector map (PostGIS layer) on level 1.
Definition: close_pg.c:39
#define DB_SQL_MAX
Definition: dbmi.h:142
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition: db.c:61
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_save_frmt(struct Map_info *)
Save format definition file for vector map.
int Vect_save_fidx(struct Map_info *, struct Format_info_offset *)
Save feature index file for vector map.
Definition: build_ogr.c:116
int Vect__write_head(struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:137
#define GV_DIRECTORY
Name of vector directory.
Definition: dig_defines.h:8
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
#define GV_TOPO_ELEMENT
Native format, topology file.
Definition: dig_defines.h:20
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
#define GPATH_MAX
Definition: gis.h:194
#define _(str)
Definition: glocale.h:10
int Vect__execute_pg(PGconn *conn, const char *stmt)
Execute SQL statement.
Definition: read_pg.c:1562
Non-native format info (PostGIS)
Definition: dig_structs.h:590
char * db_name
Database name (derived from conninfo)
Definition: dig_structs.h:598
char * fid_column
FID column.
Definition: dig_structs.h:614
char * schema_name
Schema name.
Definition: dig_structs.h:602
dbDriver * dbdriver
Open DB driver when writing attributes.
Definition: dig_structs.h:639
PGconn * conn
PGconn object (generated by PQconnectdb)
Definition: dig_structs.h:650
char * toposchema_name
Topology schema name and id.
Definition: dig_structs.h:686
char * cursor_name
Open cursor.
Definition: dig_structs.h:659
PGresult * res
Definition: dig_structs.h:651
struct Format_info_cache cache
Lines cache for reading feature.
Definition: dig_structs.h:670
char * topogeom_column
TopoGeometry column (feature table)
Definition: dig_structs.h:682
struct field_info * fi
Definition: dig_structs.h:640
char * geom_column
Geometry column (simple feature access)
Definition: dig_structs.h:618
Vector map info.
Definition: dig_structs.h:1243
void Vect__free_offset(struct Format_info_offset *offset)
void Vect__free_cache(struct Format_info_cache *cache)
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)