2 * This file is part of mapper
4 * Generic sqlite database helper functions.
6 * Copyright (C) 2007 Kaj-Michael Lang
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.
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.
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.
31 gchar *_mapper_db=NULL;
34 db_exec_sql_array(sqlite3 *db, const gchar **sql)
42 if (db_exec_sql(db, *d)==FALSE)
50 * A simple check to test if a table exists
54 db_check(sqlite3 *db, const gchar *table)
60 g_snprintf(sql, sizeof(sql),"select count(*) from %s", table);
62 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
65 sqlite3_free_table(pszResult);
70 db_exec_sql(sqlite3 *db, const gchar *sql)
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));
78 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
82 * Helper that execs a given prepared sql statment and resets clears bindings.
83 * return TRUE or FALSE depending on success of the query.
87 db_exec(sqlite3 *db, sqlite3_stmt *sql)
93 if (r!=SQLITE_OK && r!=SQLITE_DONE)
94 g_printerr("SQL ERROR %d: %s\n", r, sqlite3_errmsg(db));
96 sqlite3_clear_bindings(sql);
98 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
102 * Close the database connection
106 db_close(sqlite3 **db)
116 db_transaction_begin(sqlite3 *db)
118 return db_exec_sql(db, "begin;");
122 db_transaction_commit(sqlite3 *db)
124 return db_exec_sql(db, "commit;");
128 * Connect to given sqlite database
132 db_connect(sqlite3 **db, const gchar *mapper_db)
142 if (SQLITE_OK!=(sqlite3_open(mapper_db, db))) {
143 g_printerr("SQL OPEN: %s\n", sqlite3_errmsg(*db));
149 sqlite3_exec(*db, "PRAGMA page_size=4096;", NULL, NULL, NULL);
150 sqlite3_exec(*db, "PRAGMA encoding = \"UTF-8\";", NULL, NULL, NULL);
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);
156 sqlite3_exec(*db, "PRAGMA cache_size = 8000;", NULL, NULL, NULL);