GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2011 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Martin Landa <landa.martin gmail.com>
12 */
13
14#include <stdlib.h>
15#include <unistd.h>
16#include <grass/vector.h>
17#include <grass/dbmi.h>
18#include <grass/glocale.h>
19
20#include "local_proto.h"
21
22#ifdef HAVE_POSTGRES
23#include "pg_local_proto.h"
24#define NOPG_UNUSED
25#else
26#define NOPG_UNUSED G_UNUSED
27#endif
28
29/*!
30 \brief Close vector map (PostGIS layer) on level 1
31
32 \param Map pointer to Map_info structure
33
34 \return 0 on success
35 \return non-zero on error
36 */
38{
39#ifdef HAVE_POSTGRES
40 struct Format_info_pg *pg_info;
41
42 G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
43
44 if (!VECT_OPEN(Map))
45 return -1;
46
47 pg_info = &(Map->fInfo.pg);
48 if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
49 /* write header */
51 /* write frmt file for created PG-link */
53 }
54
55 /* clear result */
56 if (pg_info->res) {
57 PQclear(pg_info->res);
58 pg_info->res = NULL;
59 }
60
61 /* close open cursor */
62 if (pg_info->cursor_name) {
63 char stmt[DB_SQL_MAX];
64
65 snprintf(stmt, sizeof(stmt), "CLOSE %s", pg_info->cursor_name);
66 if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
67 G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
68 return -1;
69 }
70 Vect__execute_pg(pg_info->conn, "COMMIT");
71 G_free(pg_info->cursor_name);
72 pg_info->cursor_name = NULL;
73 }
74
75 PQfinish(pg_info->conn);
76
77 /* close DB connection (for atgtributes) */
78 if (pg_info->dbdriver) {
80 }
81
82 Vect__free_cache(&(pg_info->cache));
83
84 G_free(pg_info->db_name);
85 G_free(pg_info->schema_name);
86 G_free(pg_info->geom_column);
87 G_free(pg_info->fid_column);
88
89 if (pg_info->fi)
90 G_free(pg_info->fi);
91
92 if (pg_info->toposchema_name)
93 G_free(pg_info->toposchema_name);
94
95 if (pg_info->topogeom_column)
96 G_free(pg_info->topogeom_column);
97
98 return 0;
99#else
100 G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
101 return -1;
102#endif
103}
104
105/*!
106 \brief Close vector map (PostGIS layer) on topological level (write out fidx
107 file)
108
109 \param Map pointer to Map_info structure
110
111 \return 0 on success
112 \return non-zero on error
113 */
115{
116#ifdef HAVE_POSTGRES
117 G_debug(3, "V2_close_pg() name = %s mapset = %s", Map->name, Map->mapset);
118
119 if (!VECT_OPEN(Map))
120 return -1;
121
122 if (Map->fInfo.pg.toposchema_name) {
123 /* no fidx file for PostGIS topology
124
125 remove topo file (which was required for saving sidx file)
126 */
127 char buf[GPATH_MAX];
128 char file_path[GPATH_MAX];
129
130 /* delete old support files if available */
131 snprintf(buf, sizeof(buf), "%s/%s", GV_DIRECTORY, Map->name);
133 if (access(file_path, F_OK) == 0) /* file exists? */
135
136 return 0;
137 }
138
139 /* write fidx for maps in the current mapset */
140 if (Vect_save_fidx(Map, &(Map->fInfo.pg.offset)) != 1)
141 G_warning(_("Unable to save feature index file for vector map <%s>"),
142 Map->name);
143
144 Vect__free_offset(&(Map->fInfo.pg.offset));
145
146 return 0;
147#else
148 G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
149 return -1;
150#endif
151}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
#define NOPG_UNUSED
Definition close_pg.c:24
int V2_close_pg(struct Map_info *Map)
Close vector map (PostGIS layer) on topological level (write out fidx file)
Definition close_pg.c:114
int V1_close_pg(struct Map_info *Map)
Close vector map (PostGIS layer) on level 1.
Definition close_pg.c:37
Main header of GRASS DataBase Management Interface.
#define DB_SQL_MAX
Definition dbmi.h:140
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition db.c:58
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
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:107
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.
#define GV_DIRECTORY
Name of vector directory.
Definition dig_defines.h:8
#define GV_MODE_WRITE
Write vector map open mode.
#define GV_TOPO_ELEMENT
Native format, topology file.
Definition dig_defines.h:20
#define GV_MODE_RW
Read-write vector map open mode.
#define GPATH_MAX
Definition gis.h:196
#define _(str)
Definition glocale.h:10
int Vect__execute_pg(PGconn *conn, const char *stmt)
Execute SQL statement.
Definition read_pg.c:1563
Non-native format info (PostGIS)
Vector map info.
#define unlink
Definition unistd.h:11
#define access
Definition unistd.h:7
#define F_OK
Definition unistd.h:22
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)