]> err.no Git - mapper/blob - src/db.c
Fixes to gstreamer element and caps handlings.
[mapper] / src / db.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22
23 #include <glib.h>
24 #include <sqlite3.h>
25
26 #include "db.h"
27 #include "osm-sql-tables.h"
28
29 sqlite3 *_db=NULL;
30 gchar *_mapper_db=NULL;
31
32 void
33 db_create_tables(sqlite3 *db)
34 {
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);
48 }
49
50 void
51 db_create_indexes(sqlite3 *db)
52 {
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);
68 }
69
70 /**
71  * A simple check to test if a table exists
72  *
73  */
74 gboolean 
75 db_check(sqlite3 *db, const gchar *table)
76 {
77 gchar **pszResult;
78 guint nRow, nColumn;
79 gchar sql[64];
80
81 g_snprintf(sql, sizeof(sql),"select count(*) from %s", table);
82
83 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
84         return FALSE;
85
86 sqlite3_free_table(pszResult);
87 return TRUE;
88 }
89
90 gboolean
91 db_exec_sql(sqlite3 *db, const gchar *sql)
92 {
93 gint r;
94
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));
98
99 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
100 }
101
102 /**
103  * Helper that execs a given prepared sql statment and resets clears bindings.
104  * return TRUE or FALSE depending on success of the query. 
105  *
106  */
107 gboolean
108 db_exec(sqlite3 *db, sqlite3_stmt *sql)
109 {
110 gint r;
111
112 g_assert(sql);
113 r=sqlite3_step(sql);
114 if (r!=SQLITE_OK && r!=SQLITE_DONE)
115         g_printerr("SQL ERROR %d: %s\n", r, sqlite3_errmsg(db));
116 sqlite3_reset(sql);
117 sqlite3_clear_bindings(sql);
118
119 return (r==SQLITE_OK || r==SQLITE_DONE) ? TRUE : FALSE;
120 }
121
122 /**
123  * Close the database connection
124  *
125  */
126 gboolean 
127 db_close(sqlite3 **db)
128 {
129 if (db && *db) {
130         sqlite3_close(*db);
131         *db=NULL;
132 }
133 return TRUE;
134 }
135
136 gboolean
137 db_transaction_begin(sqlite3 *db)
138 {
139 return db_exec_sql(db, "begin;");
140 }
141
142 gboolean
143 db_transaction_commit(sqlite3 *db)
144 {
145 return db_exec_sql(db, "commit;");
146 }
147
148 /**
149  * Connect to given sqlite database
150  *
151  */
152 gboolean 
153 db_connect(sqlite3 **db, const gchar *mapper_db)
154 {
155 if (db && *db) {
156         sqlite3_close(*db);
157         *db=NULL;
158 }
159
160 if (!mapper_db)
161         return FALSE;
162
163 if (SQLITE_OK!=(sqlite3_open(mapper_db, db))) {
164         g_printerr("SQL OPEN: %s\n", sqlite3_errmsg(db));
165         sqlite3_close(*db);
166         *db=NULL;
167         return FALSE;
168 }
169
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);
173 #else
174 sqlite3_exec(*db, "PRAGMA cache_size = 8000;", NULL, NULL, NULL);
175 #endif
176
177 return TRUE;
178 }
179