GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
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 =
78  (char *)G_getenv_nofatal2("DB_DRIVER", G_VAR_MAPSET);
79  connection->databaseName =
80  (char *)G_getenv_nofatal2("DB_DATABASE", G_VAR_MAPSET);
81 
82  if (connection->driverName == NULL || connection->databaseName == NULL)
83  return DB_FAILED;
84 
85  connection->schemaName =
86  (char *)G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET);
87  connection->group = (char *)G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET);
88 
89  /* try to get user/password */
90  db_get_login(connection->driverName, connection->databaseName,
91  (const char **)&(connection->user),
92  (const char **)&(connection->password),
93  (const char **)&(connection->hostName),
94  (const char **)&(connection->port));
95 
96  return DB_OK;
97 }
#define NULL
Definition: ccmath.h:32
int db_get_connection(dbConnection *connection)
Get default DB connection settings for the current mapset.
int db_set_connection(dbConnection *connection)
Set default DB connection settings.
#define DB_FAILED
Definition: dbmi.h:72
#define DB_OK
Definition: dbmi.h:71
int db_get_login(const char *, const char *, const char **, const char **, const char **, const char **)
Get login parameters for driver/database.
Definition: login.c:381
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
void G_setenv2(const char *, const char *, int)
Set environment variable from specific place (updates .gisrc)
Definition: env.c:459
void G_unsetenv2(const char *, int)
Remove name from environment from specific place.
Definition: env.c:514
const char * G_getenv_nofatal2(const char *, int)
Get environment variable from specific place.
Definition: env.c:424
#define G_VAR_MAPSET
Definition: gis.h:177
char * password
Definition: dbmi.h:284
char * hostName
Definition: dbmi.h:279
char * schemaName
Definition: dbmi.h:281
char * port
Definition: dbmi.h:282
char * group
Definition: dbmi.h:286
char * user
Definition: dbmi.h:283
char * databaseName
Definition: dbmi.h:280
char * driverName
Definition: dbmi.h:278