2 * This file is part of mapper
4 * Copyright (C) 2007 Kaj-Michael Lang
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "osm-sql-tables.h"
30 gchar *_mapper_db=NULL;
33 db_create_tables(sqlite3 *db)
35 db_exec_sql(db, OSM_TABLE_NODES);
36 db_exec_sql(db, OSM_TABLE_NODE_TAGS);
37 db_exec_sql(db, OSM_TABLE_WAY);
38 db_exec_sql(db, OSM_TABLE_WAY_TAGS);
39 db_exec_sql(db, OSM_TABLE_WAY_UPDATES);
40 db_exec_sql(db, OSM_TABLE_WAY_N2N);
41 db_exec_sql(db, OSM_TABLE_WAY_NAMES);
42 db_exec_sql(db, OSM_TABLE_WAY_PC);
43 db_exec_sql(db, OSM_TABLE_WAY_NAMES_NLS);
44 db_exec_sql(db, OSM_TABLE_WAY_REF);
45 db_exec_sql(db, OSM_TABLE_PLACES);
46 db_exec_sql(db, OSM_TABLE_POI);
47 db_exec_sql(db, OSM_TABLE_POI_CATEGORY);
51 db_create_indexes(sqlite3 *db)
53 db_exec_sql(db, OSM_INDEX_1);
54 db_exec_sql(db, OSM_INDEX_2);
55 db_exec_sql(db, OSM_INDEX_3);
56 db_exec_sql(db, OSM_INDEX_4);
57 db_exec_sql(db, OSM_INDEX_5);
58 db_exec_sql(db, OSM_INDEX_6);
59 db_exec_sql(db, OSM_INDEX_7);
60 db_exec_sql(db, OSM_INDEX_8);
61 db_exec_sql(db, OSM_INDEX_9);
62 db_exec_sql(db, OSM_INDEX_10);
63 db_exec_sql(db, OSM_INDEX_11);
64 db_exec_sql(db, OSM_INDEX_12);
65 db_exec_sql(db, OSM_INDEX_13);
66 db_exec_sql(db, OSM_INDEX_14);
67 db_exec_sql(db, OSM_INDEX_15);
71 * A simple check to test if a table exists
75 db_check(sqlite3 *db, const gchar *table)
81 g_snprintf(sql, sizeof(sql),"select count(*) from %s", table);
83 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
86 sqlite3_free_table(pszResult);
91 db_exec_sql(sqlite3 *db, const gchar *sql)
95 r=sqlite3_exec(db, sql, NULL, NULL, NULL);
96 if (r!=SQLITE_OK && r!=SQLITE_DONE)
97 g_printerr("SQL ERROR %d:(%s) %s\n", r, sql, sqlite3_errmsg(db));
99 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
103 * Helper that execs a given prepared sql statment and resets clears bindings.
104 * return TRUE or FALSE depending on success of the query.
108 db_exec(sqlite3 *db, sqlite3_stmt *sql)
114 if (r!=SQLITE_OK && r!=SQLITE_DONE)
115 g_printerr("SQL ERROR %d: %s\n", r, sqlite3_errmsg(db));
117 sqlite3_clear_bindings(sql);
119 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
123 * Close the database connection
127 db_close(sqlite3 **db)
137 db_transaction_begin(sqlite3 *db)
139 return db_exec_sql(db, "begin;");
143 db_transaction_commit(sqlite3 *db)
145 return db_exec_sql(db, "commit;");
149 * Connect to given sqlite database
153 db_connect(sqlite3 **db, const gchar *mapper_db)
163 if (SQLITE_OK!=(sqlite3_open(mapper_db, db))) {
164 g_printerr("SQL OPEN: %s\n", sqlite3_errmsg(db));
170 /* Use smaller cache as the IT does not have much memory to spare */
171 #ifdef WITH_DEVICE_770
172 sqlite3_exec(*db, "PRAGMA cache_size = 2000;", NULL, NULL, NULL);
174 sqlite3_exec(*db, "PRAGMA cache_size = 8000;", NULL, NULL, NULL);