GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 #endif
27 
28 /*!
29  \brief Close vector map (PostGIS layer) on level 1
30 
31  \param Map pointer to Map_info structure
32 
33  \return 0 on success
34  \return non-zero on error
35  */
36 int V1_close_pg(struct Map_info *Map)
37 {
38 #ifdef HAVE_POSTGRES
39  struct Format_info_pg *pg_info;
40 
41  G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
42 
43  if (!VECT_OPEN(Map))
44  return -1;
45 
46  pg_info = &(Map->fInfo.pg);
47  if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
48  /* write header */
49  Vect__write_head(Map);
50  /* write frmt file for created PG-link */
51  Vect_save_frmt(Map);
52  }
53 
54  /* clear result */
55  if (pg_info->res) {
56  PQclear(pg_info->res);
57  pg_info->res = NULL;
58  }
59 
60  /* close open cursor */
61  if (pg_info->cursor_name) {
62  char stmt[DB_SQL_MAX];
63 
64  sprintf(stmt, "CLOSE %s", pg_info->cursor_name);
65  if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
66  G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
67  return -1;
68  }
69  Vect__execute_pg(pg_info->conn, "COMMIT");
70  G_free(pg_info->cursor_name);
71  pg_info->cursor_name = NULL;
72  }
73 
74  PQfinish(pg_info->conn);
75 
76  /* close DB connection (for atgtributes) */
77  if (pg_info->dbdriver) {
79  }
80 
81  Vect__free_cache(&(pg_info->cache));
82 
83  G_free(pg_info->db_name);
84  G_free(pg_info->schema_name);
85  G_free(pg_info->geom_column);
86  G_free(pg_info->fid_column);
87 
88  if (pg_info->fi)
89  G_free(pg_info->fi);
90 
91  if (pg_info->toposchema_name)
92  G_free(pg_info->toposchema_name);
93 
94  if (pg_info->topogeom_column)
95  G_free(pg_info->topogeom_column);
96 
97  return 0;
98 #else
99  G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
100  return -1;
101 #endif
102 }
103 
104 /*!
105  \brief Close vector map (PostGIS layer) on topological level (write out fidx file)
106 
107  \param Map pointer to Map_info structure
108 
109  \return 0 on success
110  \return non-zero on error
111  */
112 int V2_close_pg(struct Map_info *Map)
113 {
114 #ifdef HAVE_POSTGRES
115  G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
116 
117  if (!VECT_OPEN(Map))
118  return -1;
119 
120  if (Map->fInfo.pg.toposchema_name) {
121  /* no fidx file for PostGIS topology
122 
123  remove topo file (which was required for saving sidx file)
124  */
125  char buf[GPATH_MAX];
126  char file_path[GPATH_MAX];
127 
128  /* delete old support files if available */
129  sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
130  Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT);
131  if (access(file_path, F_OK) == 0) /* file exists? */
132  unlink(file_path);
133 
134  return 0;
135  }
136 
137  /* write fidx for maps in the current mapset */
138  if (Vect_save_fidx(Map, &(Map->fInfo.pg.offset)) != 1)
139  G_warning(_("Unable to save feature index file for vector map <%s>"),
140  Map->name);
141 
142  Vect__free_offset(&(Map->fInfo.pg.offset));
143 
144  return 0;
145 #else
146  G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
147  return -1;
148 #endif
149 }
char * toposchema_name
Topology schema name and id.
Definition: dig_structs.h:699
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char * Vect__get_element_path(char *file_path, const struct Map_info *Map, const char *element)
Get map element full path (internal use only)
#define GV_DIRECTORY
Name of vector directory.
Definition: dig_defines.h:8
int Vect_save_frmt(struct Map_info *)
Save format definition file for vector map.
PGconn * conn
PGconn object (generated by PQconnectdb)
Definition: dig_structs.h:663
#define DB_SQL_MAX
Definition: dbmi.h:142
void Vect__free_cache(struct Format_info_cache *cache)
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define GV_TOPO_ELEMENT
Native format, topology file.
Definition: dig_defines.h:20
struct Format_info fInfo
Format info for non-native formats.
Definition: dig_structs.h:1415
#define NULL
Definition: ccmath.h:32
dbDriver * dbdriver
Open DB driver when writing attributes.
Definition: dig_structs.h:652
char * db_name
Database name (derived from conninfo)
Definition: dig_structs.h:611
char * topogeom_column
TopoGeometry column (feature table)
Definition: dig_structs.h:695
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition: db.c:62
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
int V1_close_pg(struct Map_info *Map)
Close vector map (PostGIS layer) on level 1.
Definition: close_pg.c:36
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:136
void Vect__free_offset(struct Format_info_offset *offset)
Non-native format info (PostGIS)
Definition: dig_structs.h:602
char * geom_column
Geometry column (simple feature access)
Definition: dig_structs.h:631
struct Format_info_cache cache
Lines cache for reading feature.
Definition: dig_structs.h:683
char * cursor_name
Open cursor.
Definition: dig_structs.h:672
struct Format_info_pg pg
PostGIS info.
Definition: dig_structs.h:726
struct field_info * fi
Definition: dig_structs.h:653
#define GPATH_MAX
Definition: gis.h:170
char * mapset
Mapset name.
Definition: dig_structs.h:1336
Vector map info.
Definition: dig_structs.h:1259
struct Format_info_offset offset
Offset list used for building pseudo-topology (simple features access)
Definition: dig_structs.h:689
int Vect__execute_pg(PGconn *conn, const char *stmt)
Execute SQL statement.
Definition: read_pg.c:1514
void G_warning(const char *,...) __attribute__((format(printf
PGresult * res
Definition: dig_structs.h:664
#define _(str)
Definition: glocale.h:10
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
int Vect__write_head(const struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
char * schema_name
Schema name.
Definition: dig_structs.h:615
int Vect_save_fidx(struct Map_info *, struct Format_info_offset *)
Save feature index file for vector map.
Definition: build_ogr.c:116
int V2_close_pg(struct Map_info *Map)
Close vector map (PostGIS layer) on topological level (write out fidx file)
Definition: close_pg.c:112
int G_debug(int, const char *,...) __attribute__((format(printf
char * fid_column
FID column.
Definition: dig_structs.h:627