~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to db/drivers/sqlite/db.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
#include <stdlib.h>
16
16
#include <string.h>
 
17
#include <unistd.h>
17
18
#include <grass/gis.h>
18
19
#include <grass/dbmi.h>
19
20
#include <grass/glocale.h>
31
32
 */
32
33
int db__driver_open_database(dbHandle * handle)
33
34
{
34
 
    char name2[2000];
 
35
    char name2[GPATH_MAX], *path;
35
36
    const char *name;
 
37
    int i;
36
38
 
37
39
    G_debug(3, "\ndb_driver_open_database()");
38
40
 
39
 
    init_error();
40
41
    name = db_get_handle_dbname(handle);
41
42
 
42
43
    /* if name is empty use connection.databaseName */
59
60
 
60
61
        name2[0] = '\0';
61
62
        for (n = 0; n < no_tokens; n++) {
 
63
            G_chop(tokens[n]);
62
64
            if (n > 0)
63
65
                strcat(name2, "/");
64
66
 
66
68
            if (tokens[n][0] == '$') {
67
69
                G_strchg(tokens[n], '$', ' ');
68
70
                G_chop(tokens[n]);
69
 
                strcat(name2, G__getenv(tokens[n]));
70
 
                G_debug(3, "   -> %s", G__getenv(tokens[n]));
 
71
                strcat(name2, G_getenv_nofatal(tokens[n]));
 
72
                G_debug(3, "   -> %s", G_getenv_nofatal(tokens[n]));
71
73
            }
72
74
            else {
73
75
                strcat(name2, tokens[n]);
81
83
 
82
84
    G_debug(2, "name2 = '%s'", name2);
83
85
 
 
86
    path = G_store(name2);
 
87
    path = G_convert_dirseps_to_host(path);
 
88
    i = strlen(path);
 
89
    while (path[i] != HOST_DIRSEP && i > 0)
 
90
        i--;
 
91
 
 
92
    path[i] = '\0';
 
93
    if (*path) {
 
94
        G_debug(2, "path to db is %s", path);
 
95
 
 
96
        /* create directory if not existing */
 
97
        if (access(path, 0) != 0) {
 
98
            if (G_mkdir(path) != 0)
 
99
                G_fatal_error(_("Unable to create directory '%s' for sqlite database"),
 
100
                              path);
 
101
        }
 
102
    }
 
103
    G_free(path);
 
104
 
84
105
    if (sqlite3_open(name2, &sqlite) != SQLITE_OK) {
85
 
        append_error(_("Unable to open database: "));
86
 
        append_error((char *)sqlite3_errmsg(sqlite));
87
 
        report_error();
 
106
        db_d_append_error("%s %s\n%s",
 
107
                          _("Unable to open database:"),
 
108
                          name2,
 
109
                          (char *)sqlite3_errmsg(sqlite));
 
110
        db_d_report_error();
88
111
        return DB_FAILED;
89
112
    }
90
113
 
100
123
 *
101
124
 * \return always returns DB_OK
102
125
 */
103
 
 
104
126
int db__driver_close_database(void)
105
127
{
106
128
    G_debug(3, "db_close_database()");
107
129
 
108
 
    init_error();
109
130
    if (sqlite3_close(sqlite) == SQLITE_BUSY)
110
131
        G_fatal_error(_("SQLite database connection is still busy"));
111
132
 
112
133
    return DB_OK;
113
134
}
 
135
 
 
136
/**
 
137
 * \brief Create new empty SQLite database.
 
138
 *
 
139
 * \param handle dbHandle
 
140
 *
 
141
 * \return DB_OK on success
 
142
 * \return DB_FAILED on failure
 
143
 */
 
144
int db__driver_create_database(dbHandle *handle)
 
145
{
 
146
    const char *name;
 
147
    name = db_get_handle_dbname(handle);
 
148
    
 
149
    G_debug(1, "db_create_database(): %s", name);
 
150
 
 
151
    if (access(name, F_OK) == 0) {
 
152
        db_d_append_error(_("Database <%s> already exists"), name);
 
153
        db_d_report_error();
 
154
        return DB_FAILED;
 
155
    }
 
156
    
 
157
    if (sqlite3_open(name, &sqlite) != SQLITE_OK) {
 
158
        db_d_append_error("%s %s\n%s",
 
159
                          _("Unable to create database:"),
 
160
                          name,
 
161
                          (char *) sqlite3_errmsg(sqlite));
 
162
        db_d_report_error();
 
163
        return DB_FAILED;
 
164
    }
 
165
    
 
166
    return DB_OK;
 
167
}
 
168
 
 
169
/**
 
170
 * \brief Delete existing SQLite database.
 
171
 *
 
172
 * \param handle dbHandle
 
173
 *
 
174
 * \return DB_OK on success
 
175
 * \return DB_FAILED on failure
 
176
 */
 
177
int db__driver_delete_database(dbHandle *handle)
 
178
{
 
179
    const char *name;
 
180
    name = db_get_handle_dbname(handle);
 
181
    
 
182
    if (access(name, F_OK) != 0) {
 
183
        db_d_append_error(_("Database <%s> not found"), name);
 
184
        db_d_report_error();
 
185
        return DB_FAILED;
 
186
    }
 
187
 
 
188
    return remove(name) == 0 ? DB_OK : DB_FAILED;
 
189
}