]> err.no Git - mapper/commitdiff
Use a macro helper to prepare SQL statements
authorKaj-Michael Lang <milang@tal.org>
Tue, 11 Mar 2008 08:26:40 +0000 (10:26 +0200)
committerKaj-Michael Lang <milang@tal.org>
Tue, 11 Mar 2008 08:26:40 +0000 (10:26 +0200)
src/osm-db.c

index dbadff3a34807eb024d94e8b1a7c10417ae30a29..bb5664a9c0aaa43c4deceebe39c58bf6f2abdd35 100644 (file)
@@ -165,40 +165,45 @@ if (w!=NULL) {
 
 /*****************************************************************************/
 
+#define DB_PREP(db_,sql_,stmt_) \
+       if (sqlite3_prepare_v2(db_, sql_, -1, &stmt_, NULL)!=SQLITE_OK) { \
+               g_printerr("Failed to prepare: [%s] Error: %s", \
+                       sql_, sqlite3_errmsg(db_)); \
+               return FALSE; \
+       }
+
 gboolean
 osm_db_prepare(sqlite3 *db)
 {
 /* Select nearest place inside lat,lon+-range */
-if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
+DB_PREP(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
                                        " ilat,ilon,places.nid,isin_p,isin_c "
                                        " from places,nodes where type=$TYPE "
                                        " and nodes.nid=places.nid "
-                                       " and ilat between $LAT-$RANGE and $LAT+$RANGE"
-                                       " and ilon between $LON-$RANGE and $LON+$RANGE order by d limit 1",
-                   -1, &sql.select_place_near, NULL)!=SQLITE_OK)
-       return FALSE;
+                                       " and ilat between $LAT-$RANGE and $LAT+$RANGE "
+                                       " and ilon between $LON-$RANGE and $LON+$RANGE "
+                                       " order by d limit 1", 
+                                       sql.select_place_near);
 
 /* Select place name, distance, location, parent-place and type with given ID */
-if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
+DB_PREP(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
                                        " ilat,ilon,type,isin_p,isin_c "
                                        " from places,nodes where "
                                        " nodes.nid=places.nid "
                                        " and places.nid=$NID order by d limit 1",
-                   -1, &sql.select_place, NULL)!=SQLITE_OK)
-       return FALSE;
+                                       sql.select_place);
 
 /* Search */
-if (sqlite3_prepare_v2(db, "select places.nid,name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
+DB_PREP(db, "select places.nid,name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
                                        " rlat,rlon,type,isin_p,isin_c "
                                        " from places,nodes where "
                                        " nodes.nid=places.nid "
                                        " and name like $NAME order by d limit 200",
-                   -1, &sql.select_place_search, NULL)!=SQLITE_OK)
-       return FALSE;
+                                       sql.select_place_search);
 
 /* Ways */
 /* Select nearest ways inside lat,lon+-range */
-if (sqlite3_prepare_v2(db, "select w.wid,type,nodes,flags,"
+DB_PREP(db, "select w.wid,type,nodes,flags,"
                                        "(($LAT-n.ilat)*($LAT-n.ilat))+(($LON-n.ilon)*($LON-n.ilon)) as d,wn.f,wn.t,n.ilat,n.ilon "
                                        " from way as w,way_n2n as wn,nodes as n "
                                        " where w.wid=wn.wid and wn.f=n.nid "
@@ -206,10 +211,9 @@ if (sqlite3_prepare_v2(db, "select w.wid,type,nodes,flags,"
                                        " and n.ilon between $LON-$RANGE and $LON+$RANGE "
                                        " and w.type between $WTS and $WTY " 
                                        " order by d",
-                   -1, &sql.select_way2, NULL)!=SQLITE_OK)
-       return FALSE;
+                                       sql.select_way2);
 
-if (sqlite3_prepare_v2(db, "select w.wid,w.name as name,"
+DB_PREP(db, "select w.wid,w.name as name,"
                                        "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
                                        " from way_names as w,way as ww where "
                                        " ww.type between $WTS and $WTY and w.wid=ww.wid and w.name like $NAME "
@@ -223,35 +227,28 @@ if (sqlite3_prepare_v2(db, "select w.wid,w.name as name,"
                                        " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
                                        " and ww.lon between $LON-$RANGE and $LON+$RANGE "
                                        " order by name limit 200",
-                       -1, &sql.select_way_name_search, NULL)!=SQLITE_OK)
-       return FALSE;
+                                       sql.select_way_name_search);
 
-if (sqlite3_prepare_v2(db, "select wn.t,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.f=? and wn.t=nodes.nid limit 1",
-                   -1, &sql.select_way_next_seg, NULL)!=SQLITE_OK)
-       return FALSE;
+DB_PREP(db, "select wn.t,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.f=? and wn.t=nodes.nid limit 1",
+                   sql.select_way_next_seg);
 
-if (sqlite3_prepare_v2(db, "select wn.f,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.t=? and wn.f=nodes.nid limit 1",
-                   -1, &sql.select_way_prev_seg, NULL)!=SQLITE_OK)
-       return FALSE;
+DB_PREP(db, "select wn.f,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.t=? and wn.f=nodes.nid limit 1",
+                   sql.select_way_prev_seg);
 
 /* Way name */
-if (sqlite3_prepare_v2(db, "select name from way_names where wid=?", -1, &sql.select_way_name, NULL)!=SQLITE_OK)
-       return FALSE;
+DB_PREP(db, "select name from way_names where wid=?", sql.select_way_name);
 
 /* Way ref and int_ref */
-if (sqlite3_prepare_v2(db, "select ref,int_ref from way_ref where rid=?", -1, &sql.select_way_ref, NULL)!=SQLITE_OK)
-       return FALSE;
+DB_PREP(db, "select ref,int_ref from way_ref where rid=?", sql.select_way_ref);
 
 /* Get next, prev node + way information. For routing. */
-if (sqlite3_prepare_v2(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
+DB_PREP(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
                        "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.f=?",
-                   -1, &sql.select_node_next, NULL)!=SQLITE_OK)
-       return FALSE;
+                       sql.select_node_next);
 
-if (sqlite3_prepare_v2(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
+DB_PREP(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
                        "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.t=?",
-                   -1, &sql.select_node_prev, NULL)!=SQLITE_OK)
-       return FALSE;
+                   sql.select_node_prev);
 
 return TRUE;
 }