]> err.no Git - mapper/blob - src/db.c
db:
[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         if (db_exec_sql(db, *d)==FALSE)
42                 r=FALSE;
43         d++;
44 }
45 return r;
46 }
47
48 /**
49  * A simple check to test if a table exists
50  *
51  */
52 gboolean 
53 db_check(sqlite3 *db, const gchar *table)
54 {
55 gchar **pszResult;
56 guint nRow, nColumn;
57 gchar sql[64];
58
59 g_snprintf(sql, sizeof(sql),"select count(*) from %s", table);
60
61 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
62         return FALSE;
63
64 sqlite3_free_table(pszResult);
65 return TRUE;
66 }
67
68 gboolean
69 db_exec_sql(sqlite3 *db, const gchar *sql)
70 {
71 gint r;
72
73 r=sqlite3_exec(db, sql, NULL, NULL, NULL);
74 if (r!=SQLITE_OK && r!=SQLITE_DONE)
75         g_printerr("SQL ERROR %d:(%s) %s\n", r, sql, sqlite3_errmsg(db));
76
77 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
78 }
79
80 /**
81  * Helper that execs a given prepared sql statment and resets clears bindings.
82  * return TRUE or FALSE depending on success of the query. 
83  *
84  */
85 gboolean
86 db_exec(sqlite3 *db, sqlite3_stmt *sql)
87 {
88 gint r;
89
90 g_assert(sql);
91 r=sqlite3_step(sql);
92 if (r!=SQLITE_OK && r!=SQLITE_DONE)
93         g_printerr("SQL ERROR %d: %s\n", r, sqlite3_errmsg(db));
94 sqlite3_reset(sql);
95 sqlite3_clear_bindings(sql);
96
97 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
98 }
99
100 /**
101  * Close the database connection
102  *
103  */
104 gboolean 
105 db_close(sqlite3 **db)
106 {
107 if (db && *db) {
108         sqlite3_close(*db);
109         *db=NULL;
110 }
111 return TRUE;
112 }
113
114 gboolean
115 db_transaction_begin(sqlite3 *db)
116 {
117 return db_exec_sql(db, "begin;");
118 }
119
120 gboolean
121 db_transaction_commit(sqlite3 *db)
122 {
123 return db_exec_sql(db, "commit;");
124 }
125
126 /**
127  * Connect to given sqlite database
128  *
129  */
130 gboolean 
131 db_connect(sqlite3 **db, const gchar *mapper_db)
132 {
133 if (db && *db) {
134         sqlite3_close(*db);
135         *db=NULL;
136 }
137
138 if (!mapper_db)
139         return FALSE;
140
141 if (SQLITE_OK!=(sqlite3_open(mapper_db, db))) {
142         g_printerr("SQL OPEN: %s\n", sqlite3_errmsg(*db));
143         sqlite3_close(*db);
144         *db=NULL;
145         return FALSE;
146 }
147
148 sqlite3_exec(*db, "PRAGMA page_size=4096;", NULL, NULL, NULL);
149 sqlite3_exec(*db, "PRAGMA encoding = \"UTF-8\";", NULL, NULL, NULL);
150
151 /* Use smaller cache as the IT does not have much memory to spare */
152 #ifdef WITH_DEVICE_770
153 sqlite3_exec(*db, "PRAGMA cache_size = 2000;", NULL, NULL, NULL);
154 #else
155 sqlite3_exec(*db, "PRAGMA cache_size = 8000;", NULL, NULL, NULL);
156 #endif
157
158 return TRUE;
159 }
160