From: Kaj-Michael Lang Date: Sat, 3 Nov 2007 14:48:54 +0000 (+0200) Subject: Add poi_get_by_id(), add enum and define for common poi sql fields X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aebb55ff2b597b59ee03a754a17776d056f7687a;p=mapper Add poi_get_by_id(), add enum and define for common poi sql fields --- diff --git a/src/poi.c b/src/poi.c index 2fe4f80..4a45ca1 100644 --- a/src/poi.c +++ b/src/poi.c @@ -198,13 +198,33 @@ poi_populate_categories(db); return TRUE; } +typedef enum { + PS_LAT=0, + PS_LON, + PS_ID, + PS_LABEL, + PS_DESC, + PS_CAT_ID, + PS_CAT_LABEL, + PS_CAT_DESC, + PS_CAT_ICON, + PS_CAT_COLOR, + PS_SOURCE, + PS_PUBLIC, + PS_URL, + PS_POSTAL_CODE, +} poi_sql_column; + +#define POI_BASE_SQL_FIELDS "p.lat, p.lon, p.poi_id, p.label, p.desc, p.cat_id, c.label, c.desc, c.icon, c.color, p.source, p.public, p.url, p.postal_code" +#define POI_MINI_SQL_FIELDS "p.lat, p.lon, p.poi_id, p.label, p.desc, p.cat_id, c.label, c.desc" + gboolean poi_db_prepare(sqlite3 *db) { /* Select POIs inside given minmax lat,lon */ if (sqlite3_prepare_v2(db, - "select p.lat, p.lon, p.poi_id, p.label, p.desc," - " p.cat_id, c.label, c.desc, c.icon, c.color" + "select " + POI_BASE_SQL_FIELDS " from poi p, category c " " where p.lat between ? and ? " " and p.lon between ? and ? " @@ -214,8 +234,8 @@ poi_db_prepare(sqlite3 *db) /* Get POI with given ID */ if (sqlite3_prepare_v2(db, - "select p.lat, p.lon, p.poi_id, p.label, p.desc," - " p.cat_id, c.label, c.desc, c.icon, c.color" + "select " + POI_BASE_SQL_FIELDS " from poi p, category c " " where p.poi_id = ? " " and p.cat_id=c.cat_id", @@ -224,8 +244,8 @@ poi_db_prepare(sqlite3 *db) /* Search POIs by label */ if (sqlite3_prepare_v2(db, - "select p.lat, p.lon, p.poi_id, p.label, p.desc," - " p.cat_id, c.label, c.desc, c.icon, c.color" + "select " + POI_BASE_SQL_FIELDS " from poi p, category c " " where p.lat between ? and ? " " and p.lon between ? and ? " @@ -235,8 +255,8 @@ poi_db_prepare(sqlite3 *db) /* Search POI by label and specific category */ if (sqlite3_prepare_v2(db, - "select p.lat, p.lon, p.poi_id, p.label, p.desc," - " p.cat_id, c.label, c.desc, c.icon, c.color" + "select " + POI_BASE_SQL_FIELDS " from poi p, category c " " where p.lat between ? and ? " " and p.lon between ? and ? " @@ -246,8 +266,8 @@ poi_db_prepare(sqlite3 *db) /* Search specific category */ if (sqlite3_prepare_v2(db, - "select p.lat, p.lon, p.poi_id, p.label, p.desc," - " p.cat_id, c.label, c.desc, c.icon, c.color" + "select " + POI_BASE_SQL_FIELDS " from poi p, category c " " where p.lat between ? and ? " " and p.lon between ? and ? " @@ -257,8 +277,8 @@ poi_db_prepare(sqlite3 *db) /* Select nearest POI */ if (sqlite3_prepare_v2(db, - "select p.lat, p.lon, p.poi_id, p.label, p.desc," - " p.cat_id, c.label, c.desc, c.icon, c.color " + "select " + POI_MINI_SQL_FIELDS " from poi p, category c " " where c.enabled = 1 and p.cat_id = c.cat_id " " and p.lat between $LAT-0.10 and $LAT+0.10 " @@ -698,6 +718,34 @@ unit2latlon(x, y, lat2, lon2); return poi_get_list_inside(lat1, lon1, lat2, lon2, store, num_poi); } +poi_info * +poi_get_by_id(guint id) +{ +poi_info *p=NULL; + +if (SQLITE_OK!=sqlite3_bind_double(poisql.select_poi_by_id, 1, id)) + return NULL; + +if (SQLITE_ROW==sqlite3_step(poisql.select_poi_by_id)) { + p=poi_new(); + p->poi_id=sqlite3_column_int(poisql.select_poi_by_id, PS_ID); + p->lat=sqlite3_column_int(poisql.select_poi_by_id, PS_LAT); + p->lon=sqlite3_column_int(poisql.select_poi_by_id, PS_LON); + p->source=sqlite3_column_int(poisql.select_poi_by_id, PS_SOURCE); + p->public=sqlite3_column_int(poisql.select_poi_by_id, PS_PUBLIC)==1 ? TRUE : FALSE; + p->label=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_LABEL)); + p->desc=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_DESC)); + p->url=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_URL)); + p->postal_code=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_POSTAL_CODE)); + + p->cat_id=sqlite3_column_int(poisql.select_poi_by_id, PS_CAT_ID); + p->cat_label=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_CAT_LABEL)); + p->cat_desc=g_strdup(sqlite3_column_text(poisql.select_poi_by_id, PS_CAT_DESC)); +} + +return p; +} + gboolean poi_update(poi_info *p) { diff --git a/src/poi.h b/src/poi.h index ebd421d..0f31507 100644 --- a/src/poi.h +++ b/src/poi.h @@ -115,6 +115,8 @@ void poi_free(poi_info *p); gboolean poi_get_list_near_unit(guint unitx, guint unity, guint range, GtkListStore **store, guint *num_poi); gboolean poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore **store, guint *num_poi); +poi_info *poi_get_by_id(guint id); + gboolean poi_update(poi_info *p); gboolean poi_add(poi_info *p); gboolean poi_delete(guint id);