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