GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
db/dbmi_base/connect.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/connect.c
3 
4  \brief DBMI Library (base) - connect to DB
5 
6  (C) 1999-2009, 2011 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 Joel Jones (CERL/UIUC), Radim Blazek
12  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
13 */
14 
15 #include <grass/gis.h>
16 #include <grass/dbmi.h>
17 
18 /*!
19  \brief Set default DB connection settings
20 
21  This function sets environmental variables as DB_DRIVER, DB_DATABASE,
22  DB_SCHEMA, DB_GROUP.
23 
24  \param connection pointer to dbConnection with default settings
25 
26  \return DB_OK
27  */
29 {
30  /* TODO: add checks and return DB_* error code if needed */
31 
32  G_unsetenv2("DB_DRIVER", G_VAR_MAPSET);
33  if (connection->driverName)
34  G_setenv2("DB_DRIVER", connection->driverName, G_VAR_MAPSET);
35 
36  G_unsetenv2("DB_DATABASE", G_VAR_MAPSET);
37  if (connection->databaseName)
38  G_setenv2("DB_DATABASE", connection->databaseName, G_VAR_MAPSET);
39 
40  G_unsetenv2("DB_SCHEMA", G_VAR_MAPSET);
41  if (connection->schemaName)
42  G_setenv2("DB_SCHEMA", connection->schemaName, G_VAR_MAPSET);
43 
44  G_unsetenv2("DB_GROUP", G_VAR_MAPSET);
45  if (connection->group)
46  G_setenv2("DB_GROUP", connection->group, G_VAR_MAPSET);
47 
48  /* below commented due to new mechanism:
49  if ( connection->hostName )
50  G_setenv("DB_HOST", connection->hostName);
51 
52  if ( connection->location )
53  G_setenv("DB_LOCATION", connection->location);
54 
55  if ( connection->user )
56  G_setenv("DB_USER", connection->user);
57 
58  if ( connection->password )
59  G_setenv("DB_PASSWORD", connection->password);
60  */
61 
62  return DB_OK;
63 }
64 
65 /*!
66  \brief Get default DB connection settings for the current mapset
67 
68  \param[out] connection pointer to dbConnection to be modified
69 
70  \return DB_OK
71  \return DB_FAILED
72  */
74 {
75  G_zero(connection, sizeof(dbConnection));
76 
77  connection->driverName = (char *)G_getenv_nofatal2("DB_DRIVER", G_VAR_MAPSET);
78  connection->databaseName = (char *)G_getenv_nofatal2("DB_DATABASE", G_VAR_MAPSET);
79 
80  if (connection->driverName == NULL ||
81  connection->databaseName == NULL)
82  return DB_FAILED;
83 
84  connection->schemaName = (char *)G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET);
85  connection->group = (char *)G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET);
86 
87  /* try to get user/password */
88  db_get_login2(connection->driverName, connection->databaseName,
89  (const char **) &(connection->user),
90  (const char **) &(connection->password),
91  (const char **) &(connection->hostName),
92  (const char **) &(connection->port));
93 
94  return DB_OK;
95 }
int db_set_connection(dbConnection *connection)
Set default DB connection settings.
const char * G_getenv_nofatal2(const char *, int)
Get environment variable from specific place.
Definition: env.c:417
void G_setenv2(const char *, const char *, int)
Set environment variable from specific place (updates .gisrc)
Definition: env.c:452
#define NULL
Definition: ccmath.h:32
char * schemaName
Definition: dbmi.h:298
char * databaseName
Definition: dbmi.h:297
int db_get_connection(dbConnection *connection)
Get default DB connection settings for the current mapset.
char * driverName
Definition: dbmi.h:295
void G_unsetenv2(const char *, int)
Remove name from environment from specific place.
Definition: env.c:506
char * port
Definition: dbmi.h:299
char * password
Definition: dbmi.h:301
char * hostName
Definition: dbmi.h:296
#define DB_FAILED
Definition: dbmi.h:72
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
int db_get_login2(const char *, const char *, const char **, const char **, const char **, const char **)
Get login parameters for driver/database.
Definition: login.c:379
#define G_VAR_MAPSET
Definition: gis.h:163
char * group
Definition: dbmi.h:303
char * user
Definition: dbmi.h:300
#define DB_OK
Definition: dbmi.h:71