]> err.no Git - mapper/blob - src/db.c
Some more map widget work. Does not work yet.
[mapper] / src / db.c
1 /*
2  * This file is part of mapper
3  *
4  * Generic sqlite database helper functions.
5  *
6  * Copyright (C) 2007 Kaj-Michael Lang
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <sqlite3.h>
27
28 #include "db.h"
29
30 sqlite3 *_db=NULL;
31 gchar *_mapper_db=NULL;
32
33 gboolean
34 db_exec_sql_array(sqlite3 *db, const gchar **sql)
35 {
36 gboolean r=TRUE;
37 const char **d;
38
39 d=sql;
40 while (*d!=NULL) {
41         g_debug("[%s]", *d);
42         if (db_exec_sql(db, *d)==FALSE)
43                 r=FALSE;
44         d++;
45 }
46 return r;
47 }
48
49 /**
50  * A simple check to test if a table exists
51  *
52  */
53 gboolean 
54 db_check(sqlite3 *db, const gchar *table)
55 {
56 gchar **pszResult;
57 guint nRow, nColumn;
58 gchar sql[64];
59
60 g_snprintf(sql, sizeof(sql),"select count(*) from %s", table);
61
62 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
63         return FALSE;
64
65 sqlite3_free_table(pszResult);
66 return TRUE;
67 }
68
69 gboolean
70 db_exec_sql(sqlite3 *db, const gchar *sql)
71 {
72 gint r;
73
74 r=sqlite3_exec(db, sql, NULL, NULL, NULL);
75 if (r!=SQLITE_OK && r!=SQLITE_DONE)
76         g_printerr("SQL ERROR %d:(%s) %s\n", r, sql, sqlite3_errmsg(db));
77
78 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
79 }
80
81 /**
82  * Helper that execs a given prepared sql statment and resets clears bindings.
83  * return TRUE or FALSE depending on success of the query. 
84  *
85  */
86 gboolean
87 db_exec(sqlite3 *db, sqlite3_stmt *sql)
88 {
89 gint r;
90
91 g_assert(sql);
92 r=sqlite3_step(sql);
93 if (r!=SQLITE_OK && r!=SQLITE_DONE)
94         g_printerr("SQL ERROR %d: %s\n", r, sqlite3_errmsg(db));
95 sqlite3_reset(sql);
96 sqlite3_clear_bindings(sql);
97
98 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
99 }
100
101 /**
102  * Close the database connection
103  *
104  */
105 gboolean 
106 db_close(sqlite3 **db)
107 {
108 if (db && *db) {
109         sqlite3_close(*db);
110         *db=NULL;
111 }
112 return TRUE;
113 }
114
115 gboolean
116 db_transaction_begin(sqlite3 *db)
117 {
118 return db_exec_sql(db, "begin;");
119 }
120
121 gboolean
122 db_transaction_commit(sqlite3 *db)
123 {
124 return db_exec_sql(db, "commit;");
125 }
126
127 /**
128  * Connect to given sqlite database
129  *
130  */
131 gboolean 
132 db_connect(sqlite3 **db, const gchar *mapper_db)
133 {
134 if (db && *db) {
135         sqlite3_close(*db);
136         *db=NULL;
137 }
138
139 if (!mapper_db)
140         return FALSE;
141
142 if (SQLITE_OK!=(sqlite3_open(mapper_db, db))) {
143         g_printerr("SQL OPEN: %s\n", sqlite3_errmsg(*db));
144         sqlite3_close(*db);
145         *db=NULL;
146         return FALSE;
147 }
148
149 sqlite3_exec(*db, "PRAGMA page_size=4096;", NULL, NULL, NULL);
150 sqlite3_exec(*db, "PRAGMA encoding = \"UTF-8\";", NULL, NULL, NULL);
151
152 /* Use smaller cache as the IT does not have much memory to spare */
153 #ifdef WITH_DEVICE_770
154 sqlite3_exec(*db, "PRAGMA cache_size = 2000;", NULL, NULL, NULL);
155 #else
156 sqlite3_exec(*db, "PRAGMA cache_size = 8000;", NULL, NULL, NULL);
157 #endif
158
159 return TRUE;
160 }
161