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)
41 if (db_exec_sql(db, *d)==FALSE)
49 * A simple check to test if a table exists
53 db_check(sqlite3 *db, const gchar *table)
59 g_snprintf(sql, sizeof(sql),"select count(*) from %s", table);
61 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
64 sqlite3_free_table(pszResult);
69 db_exec_sql(sqlite3 *db, const gchar *sql)
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));
77 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
81 * Helper that execs a given prepared sql statment and resets clears bindings.
82 * return TRUE or FALSE depending on success of the query.
86 db_exec(sqlite3 *db, sqlite3_stmt *sql)
92 if (r!=SQLITE_OK && r!=SQLITE_DONE)
93 g_printerr("SQL ERROR %d: %s\n", r, sqlite3_errmsg(db));
95 sqlite3_clear_bindings(sql);
97 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
101 * Close the database connection
105 db_close(sqlite3 **db)
115 db_transaction_begin(sqlite3 *db)
117 return db_exec_sql(db, "begin;");
121 db_transaction_commit(sqlite3 *db)
123 return db_exec_sql(db, "commit;");
127 * Connect to given sqlite database
131 db_connect(sqlite3 **db, const gchar *mapper_db)
141 if (SQLITE_OK!=(sqlite3_open(mapper_db, db))) {
142 g_printerr("SQL OPEN: %s\n", sqlite3_errmsg(*db));
148 sqlite3_exec(*db, "PRAGMA page_size=4096;", NULL, NULL, NULL);
149 sqlite3_exec(*db, "PRAGMA encoding = \"UTF-8\";", NULL, NULL, NULL);
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);
155 sqlite3_exec(*db, "PRAGMA cache_size = 8000;", NULL, NULL, NULL);