GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 1999-2010 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC)
10 \author Upgraded to GRASS 5.7 by Radim Blazek
11 */
12
13#include <stdlib.h>
14#include <string.h>
15#include <grass/gis.h>
16#include <grass/dbmi.h>
17#include <grass/glocale.h>
18
19/*!
20 \brief Get driver name from current DB connection settings
21
22 \return pointer to driver name
23 \return NULL if not set
24 */
26{
27 const char *drv;
28
29 if ((drv = G_getenv_nofatal2("DB_DRIVER", G_VAR_MAPSET)))
30 return G_store(drv);
31
32 return NULL;
33}
34
35/*!
36 \brief Get database name from current DB connection settings
37
38 \return pointer to database name
39 \return NULL if not set
40 */
42{
43 const char *drv;
44
45 if ((drv = G_getenv_nofatal2("DB_DATABASE", G_VAR_MAPSET)))
46 return G_store(drv);
47
48 return NULL;
49}
50
51/*!
52 \brief Get schema name from current DB connection settings
53
54 \return pointer to schema name
55 \return NULL if not set
56 */
58{
59 const char *sch;
60
61 if ((sch = G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET)))
62 return G_store(sch);
63
64 return NULL;
65}
66
67/*!
68 \brief Get group name from current DB connection settings
69
70 \return pointer to group name
71 \return NULL if not set
72 */
74{
75 const char *gr;
76
77 if ((gr = G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET)))
78 return G_store(gr);
79
80 return NULL;
81}
82
83/*!
84 \brief Sets up database connection settings using GRASS default from dbmi.h
85
86 This function ignores current DB connection settings and uses GRASS
87 default settings instead.
88
89 \todo DB_OK on success, DB_* error code on fail
90
91 \return returns DB_OK
92 */
94{
96 char buf[GPATH_MAX];
97
98 G_debug(1,
99 "Creating new default DB params with db_set_default_connection()");
100
101 /* do not use default DB connection settings for the current mapset */
102 G_zero(&connection, sizeof(dbConnection));
103
104 if (strcmp(DB_DEFAULT_DRIVER, "dbf") == 0) {
105 /* Set default values and create dbf db dir */
106
107 connection.driverName = "dbf";
108 connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
110
111 snprintf(buf, sizeof(buf), "%s/%s/dbf", G_location_path(), G_mapset());
113 }
114 else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) {
115 /* Set default values and create sqlite db dir */
116
117 connection.driverName = "sqlite";
118 /*
119 * TODO: Use one DB for entire mapset (LFS problems?)
120 * or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set
121 * that here?) or $MAPSET/sqlite/mapname.sql as with dbf?
122 */
123
124 /* https://www.sqlite.org/lockingv3.html
125 * When SQLite creates a journal file on Unix, it opens the
126 * directory that contains that file and calls fsync() on the
127 * directory, in an effort to push the directory information to disk.
128 *
129 * -> have sqlite.db in a separate directory
130 */
131 connection.databaseName =
132 "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db";
135 }
136 else
137 G_fatal_error(_("Programmer error"));
138
139 return DB_OK;
140}
#define NULL
Definition ccmath.h:32
const char * db_get_default_schema_name(void)
Get schema name from current DB connection settings.
const char * db_get_default_driver_name(void)
Get driver name from current DB connection settings.
int db_set_default_connection(void)
Sets up database connection settings using GRASS default from dbmi.h.
const char * db_get_default_group_name(void)
Get group name from current DB connection settings.
const char * db_get_default_database_name(void)
Get database name from current DB connection settings.
Main header of GRASS DataBase Management Interface.
#define DB_DEFAULT_DRIVER
Definition dbmi.h:19
#define DB_OK
Definition dbmi.h:69
int db_set_connection(dbConnection *)
Set default DB connection settings.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21
void void void void G_fatal_error(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:73
const char * G_getenv_nofatal2(const char *, int)
Get environment variable from specific place.
Definition env.c:422
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_location_path(void)
Get current location UNIX-like path.
Definition location.c:52
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
#define G_VAR_MAPSET
Definition gis.h:179
#define GPATH_MAX
Definition gis.h:196
#define _(str)
Definition glocale.h:10