File: default_name.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (142 lines) | stat: -rw-r--r-- 3,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*!
   \file lib/db/dbmi_base/default_name.c

   \brief DBMI Library (base) - default settings

   (C) 1999-2010 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2). Read the file COPYING that comes with GRASS for details.

   \author Joel Jones (CERL/UIUC)
   \author Upgraded to GRASS 5.7 by Radim Blazek
 */

#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>

/*!
   \brief Get driver name from current DB connection settings

   \return pointer to driver name
   \return NULL if not set
 */
const char *db_get_default_driver_name(void)
{
    const char *drv;

    if ((drv = G_getenv_nofatal2("DB_DRIVER", G_VAR_MAPSET)))
        return G_store(drv);

    return NULL;
}

/*!
   \brief Get database name from current DB connection settings

   \return pointer to database name
   \return NULL if not set
 */
const char *db_get_default_database_name(void)
{
    const char *drv;

    if ((drv = G_getenv_nofatal2("DB_DATABASE", G_VAR_MAPSET)))
        return G_store(drv);

    return NULL;
}

/*!
   \brief Get schema name from current DB connection settings

   \return pointer to schema name
   \return NULL if not set
 */
const char *db_get_default_schema_name(void)
{
    const char *sch;

    if ((sch = G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET)))
        return G_store(sch);

    return NULL;
}

/*!
   \brief Get group name from current DB connection settings

   \return pointer to group name
   \return NULL if not set
 */
const char *db_get_default_group_name(void)
{
    const char *gr;

    if ((gr = G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET)))
        return G_store(gr);

    return NULL;
}

/*!
   \brief Sets up database connection settings using GRASS default from dbmi.h

   This function ignores current DB connection settings and uses GRASS
   default settings instead.

   \todo DB_OK on success, DB_* error code on fail

   \return returns DB_OK
 */
int db_set_default_connection(void)
{
    dbConnection connection;
    char buf[GPATH_MAX];

    G_debug(1,
            "Creating new default DB params with db_set_default_connection()");

    /* do not use default DB connection settings for the current mapset */
    G_zero(&connection, sizeof(dbConnection));

    if (strcmp(DB_DEFAULT_DRIVER, "dbf") == 0) {
        /* Set default values and create dbf db dir */

        connection.driverName = "dbf";
        connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
        db_set_connection(&connection);

        sprintf(buf, "%s/%s/dbf", G_location_path(), G_mapset());
        G_make_mapset_object_group("dbf");
    }
    else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) {
        /* Set default values and create sqlite db dir */

        connection.driverName = "sqlite";
        /*
         * TODO: Use one DB for entire mapset (LFS problems?)
         *      or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set
         * that here?) or $MAPSET/sqlite/mapname.sql as with dbf?
         */

        /* https://www.sqlite.org/lockingv3.html
         * When SQLite creates a journal file on Unix, it opens the
         * directory that contains that file and calls fsync() on the
         * directory, in an effort to push the directory information to disk.
         *
         * -> have sqlite.db in a separate directory
         */
        connection.databaseName =
            "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db";
        G_make_mapset_object_group("sqlite");
        db_set_connection(&connection);
    }
    else
        G_fatal_error(_("Programmer error"));

    return DB_OK;
}