chiark / gitweb /
routesearch: found another bug
[ypp-sc-tools.db-test.git] / yarrg / rssql.c
index 898ac3b268350b4824021ba987002622600c88e3..26a77bb7811dd0be8fc504037a86b8a06f44a20d 100644 (file)
@@ -14,10 +14,10 @@ static int busy_handler(void *u, int previous) {
   return 1;
 }
 
-void setup_sql(void) {
+void setup_sql(const char *database) {
   sqlite3_stmt *sst;
   
-  SQL_MUST( sqlite3_open("OCEAN-Midnight.db", &db) );
+  SQL_MUST( sqlite3_open(database, &db) );
   SQL_MUST( sqlite3_busy_handler(db, busy_handler, 0) );
 
   sst= sql_prepare("BEGIN","(begin)");
@@ -57,8 +57,28 @@ sqlite3_stmt *sql_prepare(const char *stmt, const char *what) {
   return ssr;
 }
 
-int sql_step_wrap(sqlite3_stmt *ssh, const char *ssh_string,
-                 const char *file, int line) {
+int sql_step_distinct(sqlite3_stmt *ssh, const char *ssh_string,
+                     const char *file, int line,
+                     int *cols, int ncols, int nkeycols) {
+  for (;;) {
+    if (!sql_step(ssh, ssh_string, file, line)) return 0;
+
+    int i;
+    for (i=0; i<ncols; i++) {
+      int v= sqlite3_column_int(ssh, i);
+      if (v == cols[i]) continue;
+      
+      assert(i<nkeycols);
+      cols[i++]= v;
+      for ( ; i<ncols; i++)
+       cols[i]= sqlite3_column_int(ssh, i);
+      return 1;
+    }
+  }
+}
+
+int sql_step(sqlite3_stmt *ssh, const char *ssh_string,
+            const char *file, int line) {
   for (;;) {
     int sqr;
     sqr= sqlite3_step((ssh));