GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 driver name from current DB connection settings
23 
24  \return pointer to 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 database name from current DB connection settings
39 
40  \return pointer to 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 schema name from current DB connection settings
55 
56  \return pointer to 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 group name from current DB connection settings
71 
72  \return pointer to 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  This function ignores current DB connection settings and uses GRASS
89  default settings instead.
90 
91  \todo DB_OK on success, DB_* error code on fail
92 
93  \return returns DB_OK
94 */
96 {
97  dbConnection connection;
98  char buf[GPATH_MAX];
99 
100  G_debug(1,
101  "Creating new default DB params with db_set_default_connection()");
102 
103  /* do not use default DB connection settings for the current mapset */
104  G_zero(&connection, sizeof(dbConnection));
105 
106  if (strcmp(DB_DEFAULT_DRIVER, "dbf") == 0) {
107  /* Set default values and create dbf db dir */
108 
109  connection.driverName = "dbf";
110  connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
111  db_set_connection(&connection);
112 
113  sprintf(buf, "%s/%s/dbf", G_location_path(), G_mapset());
115  }
116  else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) {
117  /* Set default values and create sqlite db dir */
118 
119  connection.driverName = "sqlite";
120  /*
121  * TODO: Use one DB for entire mapset (LFS problems?)
122  * or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set that here?)
123  * or $MAPSET/sqlite/mapname.sql as with dbf?
124  */
125 
126  /* http://www.sqlite.org/lockingv3.html
127  * When SQLite creates a journal file on Unix, it opens the
128  * directory that contains that file and calls fsync() on the
129  * directory, in an effort to push the directory information to disk.
130  *
131  * -> have sqlite.db in a separate directory
132  */
133  connection.databaseName =
134  "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db";
135  G_make_mapset_object_group("sqlite");
136  db_set_connection(&connection);
137  }
138  else
139  G_fatal_error(_("Programmer error"));
140 
141  return DB_OK;
142 }
const char * db_get_default_driver_name(void)
Get driver name from current DB connection settings.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
const char * G_getenv_nofatal2(const char *, int)
Get environment variable from specific place.
Definition: env.c:417
const char * db_get_default_database_name(void)
Get database name from current DB connection settings.
#define DB_DEFAULT_DRIVER
Definition: dbmi.h:21
const char * db_get_default_schema_name(void)
Get schema name from current DB connection settings.
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
#define NULL
Definition: ccmath.h:32
char * databaseName
Definition: dbmi.h:297
const char * db_get_default_group_name(void)
Get group name from current DB connection settings.
#define GPATH_MAX
Definition: gis.h:180
char * driverName
Definition: dbmi.h:295
int db_set_connection(dbConnection *)
Set default DB connection settings.
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
#define _(str)
Definition: glocale.h:10
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
#define G_VAR_MAPSET
Definition: gis.h:163
int db_set_default_connection(void)
Sets up database connection settings using GRASS default from dbmi.h.
int G_debug(int, const char *,...) __attribute__((format(printf
int G_make_mapset_object_group(const char *)
Create directory for group of elements of a given type.
Definition: mapset_msc.c:74
#define DB_OK
Definition: dbmi.h:71