GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71924
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
db/dbmi_base/default_name.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/default_name.c
3 
4  \brief DBMI Library (base) - default settings
5 
6  (C) 1999-2010 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)
12  \author Upgraded to GRASS 5.7 by Radim Blazek
13 */
14 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <grass/gis.h>
18 #include <grass/dbmi.h>
19 #include <grass/glocale.h>
20 
21 /*!
22  \brief Get default driver name
23 
24  \return pointer to default driver name
25  \return NULL if not set
26 */
27 const char *db_get_default_driver_name(void)
28 {
29  const char *drv;
30 
31  if ((drv = G_getenv_nofatal2("DB_DRIVER", G_VAR_MAPSET)))
32  return G_store(drv);
33 
34  return NULL;
35 }
36 
37 /*!
38  \brief Get default database name
39 
40  \return pointer to default database name
41  \return NULL if not set
42 */
44 {
45  const char *drv;
46 
47  if ((drv = G_getenv_nofatal2("DB_DATABASE", G_VAR_MAPSET)))
48  return G_store(drv);
49 
50  return NULL;
51 }
52 
53 /*!
54  \brief Get default schema name
55 
56  \return pointer to default schema name
57  \return NULL if not set
58 */
59 const char *db_get_default_schema_name(void)
60 {
61  const char *sch;
62 
63  if ((sch = G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET)))
64  return G_store(sch);
65 
66  return NULL;
67 }
68 
69 /*!
70  \brief Get default group name
71 
72  \return pointer to default group name
73  \return NULL if not set
74 */
75 const char *db_get_default_group_name(void)
76 {
77  const char *gr;
78 
79  if ((gr = G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET)))
80  return G_store(gr);
81 
82  return NULL;
83 }
84 
85 /*!
86  \brief Sets up database connection settings using GRASS default from dbmi.h
87 
88  \todo DB_OK on success, DB_* error code on fail
89 
90  \return returns DB_OK
91 */
93 {
94  dbConnection connection;
95  char buf[GPATH_MAX];
96 
97  G_debug(1,
98  "Creating new default DB params with db_set_default_connection()");
99 
100  /* is this really needed ? */
101  db_get_connection(&connection);
102 
103  if (strcmp(DB_DEFAULT_DRIVER, "dbf") == 0) {
104  /* Set default values and create dbf db dir */
105 
106  connection.driverName = "dbf";
107  connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
108  db_set_connection(&connection);
109 
110  sprintf(buf, "%s/%s/dbf", G_location_path(), G_mapset());
111  G_make_mapset_element("dbf");
112  }
113  else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) {
114  /* Set default values and create sqlite db dir */
115 
116  connection.driverName = "sqlite";
117  /*
118  * TODO: Use one DB for entire mapset (LFS problems?)
119  * or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set that here?)
120  * or $MAPSET/sqlite/mapname.sql as with dbf?
121  */
122 
123  /* http://www.sqlite.org/lockingv3.html
124  * When SQLite creates a journal file on Unix, it opens the
125  * directory that contains that file and calls fsync() on the
126  * directory, in an effort to push the directory information to disk.
127  *
128  * -> have sqlite.db in a separate directory
129  */
130  connection.databaseName =
131  "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db";
132  G_make_mapset_element("sqlite");
133  db_set_connection(&connection);
134  }
135  else
136  G_fatal_error(_("Programmer error"));
137 
138  return DB_OK;
139 }
int G_make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:38
const char * db_get_default_driver_name(void)
Get default driver name.
int db_set_connection(dbConnection *connection)
Set default DB connection settings.
const char * db_get_default_database_name(void)
Get default database name.
#define DB_DEFAULT_DRIVER
Definition: dbmi.h:23
const char * db_get_default_schema_name(void)
Get default schema name.
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
#define NULL
Definition: ccmath.h:32
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
char * databaseName
Definition: dbmi.h:297
const char * db_get_default_group_name(void)
Get default group name.
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
#define GPATH_MAX
Definition: gis.h:151
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
#define _(str)
Definition: glocale.h:13
#define G_VAR_MAPSET
Definition: gis.h:134
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int db_set_default_connection(void)
Sets up database connection settings using GRASS default from dbmi.h.
#define DB_OK
Definition: dbmi.h:71