GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71785
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  if (connection->driverName)
33  G_setenv2("DB_DRIVER", connection->driverName, G_VAR_MAPSET);
34 
35  if (connection->databaseName)
36  G_setenv2("DB_DATABASE", connection->databaseName, G_VAR_MAPSET);
37 
38  if (connection->schemaName)
39  G_setenv2("DB_SCHEMA", connection->schemaName, G_VAR_MAPSET);
40 
41  if (connection->group)
42  G_setenv2("DB_GROUP", connection->group, G_VAR_MAPSET);
43 
44  /* below commented due to new mechanism:
45  if ( connection->hostName )
46  G_setenv("DB_HOST", connection->hostName);
47 
48  if ( connection->location )
49  G_setenv("DB_LOCATION", connection->location);
50 
51  if ( connection->user )
52  G_setenv("DB_USER", connection->user);
53 
54  if ( connection->password )
55  G_setenv("DB_PASSWORD", connection->password);
56  */
57 
58  return DB_OK;
59 }
60 
61 /*!
62  \brief Get default DB connection settings
63 
64  \param[out] connection pointer to dbConnection to be modified
65 
66  \return DB_OK
67  \return DB_FAILED
68  */
70 {
71  G_zero(connection, sizeof(dbConnection));
72 
73  connection->driverName = (char *)G_getenv_nofatal2("DB_DRIVER", G_VAR_MAPSET);
74  connection->databaseName = (char *)G_getenv_nofatal2("DB_DATABASE", G_VAR_MAPSET);
75 
76  if (connection->driverName == NULL ||
77  connection->databaseName == NULL)
78  return DB_FAILED;
79 
80  connection->schemaName = (char *)G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET);
81  connection->group = (char *)G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET);
82 
83  /* try to get user/password */
84  db_get_login2(connection->driverName, connection->databaseName,
85  (const char **) &(connection->user),
86  (const char **) &(connection->password),
87  (const char **) &(connection->hostName),
88  (const char **) &(connection->port));
89 
90  return DB_OK;
91 }
int db_set_connection(dbConnection *connection)
Set default DB connection settings.
#define NULL
Definition: ccmath.h:32
char * schemaName
Definition: dbmi.h:298
void G_setenv2(const char *name, const char *value, int loc)
Set environment variable from specific place (updates .gisrc)
Definition: env.c:435
char * databaseName
Definition: dbmi.h:297
const char * G_getenv_nofatal2(const char *name, int loc)
Get environment variable from specific place.
Definition: env.c:400
int db_get_connection(dbConnection *connection)
Get default DB connection settings.
char * driverName
Definition: dbmi.h:295
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
#define G_VAR_MAPSET
Definition: gis.h:134
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
char * group
Definition: dbmi.h:303
int db_get_login2(const char *driver, const char *database, const char **user, const char **password, const char **host, const char **port)
Get login parameters for driver/database.
Definition: login.c:379
char * user
Definition: dbmi.h:300
#define DB_OK
Definition: dbmi.h:71