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