From: Kaj-Michael Lang Date: Tue, 30 Oct 2007 20:08:04 +0000 (+0200) Subject: Database init changes. Rename some variables. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b74b947a3b8a44b94ae483eb92dd6b78dce081e2;p=mapper Database init changes. Rename some variables. --- diff --git a/src/osm-db.c b/src/osm-db.c index fcc8d74..2d375eb 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -45,6 +45,8 @@ #define OSM_RANGE_STEP (8192) #define OSM_RANGE_STOP (65535) +static sqlite3 *osmdb; + struct sql_select_stmt { sqlite3_stmt *select_way; sqlite3_stmt *select_way2; @@ -63,7 +65,7 @@ static GTimer *dbt; static GtkProgressBar *dbpw=NULL; /* Cache hash tables */ -static GHashTable *_place_cache; +static GHashTable *place_cache; static guint way_dist_range=OSM_RANGE_WAY; @@ -192,21 +194,31 @@ if (sqlite3_prepare_v2(db, "select ref,int_ref from way_ref where rid=?", return TRUE; } -gboolean -osm_init(void) -{ -_place_cache=g_hash_table_new(g_direct_hash, g_direct_equal); -dbt=g_timer_new(); -return TRUE; -} - void osm_deinit(void) { -g_hash_table_destroy(_place_cache); +osmdb=NULL; +g_hash_table_destroy(place_cache); g_timer_destroy(dbt); } +gboolean +osm_init(sqlite3 **db) +{ +if (!db || !*db) + return FALSE; + +osmdb=*db; +if (osm_db_prepare(osmdb)==FALSE) { + g_printerr("Failed to prepare OSM SQL statements:"); + g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb)); + return FALSE; +} +place_cache=g_hash_table_new(g_direct_hash, g_direct_equal); +dbt=g_timer_new(); +return TRUE; +} + /*****************************************************************************/ osm_way_node * @@ -289,21 +301,21 @@ return g_slice_new0(osm_place); static osm_place * osm_place_cache_lookup(guint32 id) { -return g_hash_table_lookup(_place_cache, GINT_TO_POINTER(id)); +return g_hash_table_lookup(place_cache, GINT_TO_POINTER(id)); } static void osm_place_cache_add(osm_place *p) { if (osm_place_cache_lookup(p->id)==NULL) - g_hash_table_insert(_place_cache, GINT_TO_POINTER(p->id), p); + g_hash_table_insert(place_cache, GINT_TO_POINTER(p->id), p); } static void osm_place_cache_gc(void) { gint r; -r=g_hash_table_foreach_remove(_place_cache, osm_place_remove, NULL); +r=g_hash_table_foreach_remove(place_cache, osm_place_remove, NULL); } static void @@ -335,7 +347,7 @@ if (n) { n=NULL; /* XXX: better place for this */ -if (g_hash_table_size(_place_cache)>OSM_PLACE_CACHE_MAX_ITEMS) +if (g_hash_table_size(place_cache)>OSM_PLACE_CACHE_MAX_ITEMS) osm_place_cache_gc(); sqlite3_clear_bindings(sql.select_place); diff --git a/src/osm-db.h b/src/osm-db.h index f2bec4d..2f9a072 100644 --- a/src/osm-db.h +++ b/src/osm-db.h @@ -26,7 +26,7 @@ #define OSM_RANGE_WAY (20000) -gboolean osm_init(void); +gboolean osm_init(sqlite3 **db); void osm_deinit(void); void osm_set_way_range_from_speed(gfloat speed); diff --git a/src/poi.c b/src/poi.c index a0d10b7..8253eb6 100644 --- a/src/poi.c +++ b/src/poi.c @@ -29,6 +29,8 @@ #include "osm.h" #include "osm-db.h" +static sqlite3 *poidb; + struct _poi_categories { node_type_t type; const gchar *name, *desc, *icon, *color; @@ -289,38 +291,19 @@ poi_db_prepare(sqlite3 *db) } gboolean -mapper_db_disconnect(sqlite3 **db) +poi_init(sqlite3 **db) { -if (db && *db) { - /* XXX: Handle the prepared statements */ -#if 0 - poi_db_unprepare(*db); - osm_db_unprepare(*db); -#endif - sqlite3_close(*db); - return TRUE; -} -return FALSE; -} +if (!db || !*db) + return FALSE; -gboolean -mapper_db_connect(sqlite3 **db, const gchar *data_db) -{ -if (db_connect(db, data_db)) { - poi_db_create(*db); - if (poi_db_prepare(*db)==FALSE) { - g_printerr("Failed to prepare POI SQL statements:"); - g_printf("SQLITE: %s\n", sqlite3_errmsg(*db)); - return FALSE; - } - if (osm_db_prepare(*db)==FALSE) { - g_printerr("Failed to prepare OSM SQL statements:"); - g_printf("SQLITE: %s\n", sqlite3_errmsg(*db)); - return FALSE; - } - return TRUE; +poidb=*db; +poi_db_create(poidb); +if (poi_db_prepare(poidb)==FALSE) { + g_printerr("Failed to prepare POI SQL statements:"); + g_printf("SQLITE: %s\n", sqlite3_errmsg(poidb)); + return FALSE; } -return FALSE; +return TRUE; } poi_info * diff --git a/src/poi.h b/src/poi.h index 5afc924..09cb4f4 100644 --- a/src/poi.h +++ b/src/poi.h @@ -81,6 +81,8 @@ struct sql_poi_stmt poisql; guint _poi_zoom; +gboolean poi_init(sqlite3 **db); + gboolean poi_db_create(sqlite3 *db); gboolean poi_db_prepare(sqlite3 *db); diff --git a/src/settings-gui.c b/src/settings-gui.c index 5b559d8..d4c35bc 100644 --- a/src/settings-gui.c +++ b/src/settings-gui.c @@ -656,13 +656,13 @@ settings_dialog(void) speak_set_parameters(_voice_speed, _voice_pitch); if (_db) { - mapper_db_disconnect(&_db); + db_close(&_db); _db = NULL; } g_free(_mapper_db); if (strlen(gtk_entry_get_text(GTK_ENTRY(txt_mapper_db)))) { _mapper_db = g_strdup(gtk_entry_get_text(GTK_ENTRY(txt_mapper_db))); - mapper_db_connect(&_db, _mapper_db); + db_connect(&_db, _mapper_db); } else { _mapper_db = NULL; }