GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
temporal/lib/connect.c
Go to the documentation of this file.
1/*!
2 \file lib/temporal/lib/connect.c
3
4 \brief Temporal GIS Library - connect to TGIS DB
5
6 SPDX-FileCopyrightText: 2012 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Soeren Gebbert
10 Code is based on the dbmi library written by
11 Joel Jones (CERL/UIUC) and Radim Blazek
12 */
13
14#include <grass/temporal.h>
15#include <grass/glocale.h>
16
17/*!
18 * \brief Get TGIS driver name
19 *
20 * \return pointer to TGIS driver name
21 * \return NULL if not set
22 */
24{
25 const char *drv;
26
27 if ((drv = G_getenv_nofatal2("TGISDB_DRIVER", G_VAR_MAPSET)))
28 return G_store(drv);
29
30 return NULL;
31}
32
33/*!
34 * \brief Get TGIS database name
35 *
36 * \return pointer to TGIS database name
37 * \return NULL if not set
38 */
40{
41 const char *drv;
42
43 if ((drv = G_getenv_nofatal2("TGISDB_DATABASE", G_VAR_MAPSET)))
44 return G_store(drv);
45
46 return NULL;
47}
48
49/*!
50 \brief Set Temporal GIS DB connection settings
51
52 This function sets environmental variables as TGISDB_DRIVER, TGISDB_DATABASE.
53
54 \param connection pointer to dbConnection with default settings
55
56 \return DB_OK
57 */
59{
60 if (connection->driverName)
61 G_setenv2("TGISDB_DRIVER", connection->driverName, G_VAR_MAPSET);
62
63 if (connection->databaseName)
64 G_setenv2("TGISDB_DATABASE", connection->databaseName, G_VAR_MAPSET);
65
66 return DB_OK;
67}
68
69/*!
70 \brief Get Temporal GIS DB connection settings
71
72 \param[out] connection pointer to dbConnection to be modified
73
74 \return DB_OK
75 */
77{
79
80 connection->driverName =
81 (char *)G_getenv_nofatal2("TGISDB_DRIVER", G_VAR_MAPSET);
82 connection->databaseName =
83 (char *)G_getenv_nofatal2("TGISDB_DATABASE", G_VAR_MAPSET);
84
85 return DB_OK;
86}
87
88#define DRIVER_NAME 0
89#define DATABASE_NAME 1
90
91static char *get_mapset_connection_name(const char *mapset, int contype)
92{
93 const char *val = NULL;
94 char *ret_val = NULL;
95 ;
96 const char *gisdbase = G_getenv_nofatal("GISDBASE");
97 const char *location = G_getenv_nofatal("LOCATION_NAME");
98 int ret;
99
100 G_debug(1, "Checking mapset <%s>", mapset);
101 ret = G_mapset_permissions2(gisdbase, location, mapset);
102 switch (ret) {
103 case 0: /* Check if the mapset exists and user is owner */
104 /* We suppress this warning, since G_mapset_permission2() does not
105 * properly check the access privileges to the mapset of a different
106 user.
107 * TODO: develop a dedicated G_mapset_permission3() for that
108 G_warning(_("You are not the owner of mapset <%s>"),
109 mapset);
110 */
111 break;
112 case -1:
113 G_warning(_("Mapset <%s> does not exist."), mapset);
114 return ret_val;
115 default:
116 break;
117 }
118
120 G_setenv_nogisrc("GISDBASE", gisdbase);
121 G_setenv_nogisrc("LOCATION_NAME", location);
122 G_setenv_nogisrc("MAPSET", mapset);
124
125 if (contype == DATABASE_NAME) {
126 if ((val = G_getenv_nofatal2("TGISDB_DATABASE", G_VAR_MAPSET)))
127 ret_val = G_store(val);
128 }
129 else if (contype == DRIVER_NAME) {
130 if ((val = G_getenv_nofatal2("TGISDB_DRIVER", G_VAR_MAPSET)))
131 ret_val = G_store(val);
132 }
133
134 G_switch_env();
135
136 return ret_val;
137}
138
139/*!
140 * \brief Get TGIS driver name from a specific mapset
141 *
142 * This function give a warning in case the mapset does not exists
143 * or it is not allowed to access the mapset. NULL is returned in this case.
144 *
145 * \param mapset The name of the mapset to receive the driver name from
146 *
147 * \return pointer to TGIS driver name
148 * \return NULL if not set
149 */
150char *tgis_get_mapset_driver_name(const char *mapset)
151{
152 return get_mapset_connection_name(mapset, DRIVER_NAME);
153}
154
155/*!
156 * \brief Get TGIS database name
157 *
158 * This function give a warning in case the mapset does not exists
159 * or it is not allowed to access the mapset. NULL is returned in this case..
160 *
161 * \param mapset The name of the mapset to receive the driver name from
162
163 * \return pointer to TGIS database name
164 * \return NULL if not set
165 */
166char *tgis_get_mapset_database_name(const char *mapset)
167{
168 return get_mapset_connection_name(mapset, DATABASE_NAME);
169}
#define NULL
Definition ccmath.h:32
#define DB_OK
Definition dbmi.h:69
const char * G_getenv_nofatal(const char *)
Get environment variable.
Definition env.c:403
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21
void G_switch_env(void)
Switch environments.
Definition env.c:589
void G_warning(const char *,...) __attribute__((format(printf
void G_setenv2(const char *, const char *, int)
Set environment variable from specific place (updates .gisrc)
Definition env.c:457
const char * G_getenv_nofatal2(const char *, int)
Get environment variable from specific place.
Definition env.c:422
void G__read_mapset_env(void)
Force to read the mapset environment file VAR.
Definition env.c:96
int G_mapset_permissions2(const char *, const char *, const char *)
Check for user mapset permission.
Definition mapset_msc.c:319
void G_create_alt_env(void)
Set up alternative environment variables.
Definition env.c:567
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
int G_debug(int, const char *,...) __attribute__((format(printf
void G_setenv_nogisrc(const char *, const char *)
Set environment name to value (doesn't update .gisrc)
Definition env.c:470
#define G_VAR_MAPSET
Definition gis.h:179
#define _(str)
Definition glocale.h:10
char * tgis_get_driver_name(void)
Get TGIS driver name.
char * tgis_get_database_name(void)
Get TGIS database name.
#define DRIVER_NAME
int tgis_set_connection(dbConnection *connection)
Set Temporal GIS DB connection settings.
#define DATABASE_NAME
char * tgis_get_mapset_driver_name(const char *mapset)
Get TGIS driver name from a specific mapset.
char * tgis_get_mapset_database_name(const char *mapset)
Get TGIS database name.
int tgis_get_connection(dbConnection *connection)
Get Temporal GIS DB connection settings.