chiark / gitweb /
WIP routesearch; before try not using interior point
[ypp-sc-tools.db-live.git] / yarrg / rssql.c
diff --git a/yarrg/rssql.c b/yarrg/rssql.c
new file mode 100644 (file)
index 0000000..463ef8b
--- /dev/null
@@ -0,0 +1,82 @@
+
+#include "rscommon.h"
+
+sqlite3 *db;
+sqlite3_stmt *ss_ipair;
+
+DEBUG_DEFINE_DEBUGF(sql);
+
+static int busy_handler(void *u, int previous) {
+  debugf("[[DB BUSY %d]]",previous);
+  sysassert(! usleep(5000) );
+  return 1;
+}
+
+void setup(void) {
+  sqlite3_stmt *sst;
+  
+  SQL_MUST( sqlite3_open("OCEAN-Midnight.db", &db) );
+  SQL_MUST( sqlite3_busy_handler(db, busy_handler, 0) );
+
+  sst= sql_prepare("BEGIN","(begin)");
+  assert( !SQL_STEP(sst) );
+  sqlite3_finalize(sst);
+
+  setup_value();
+}
+
+int sql_single_int(const char *stmt) {
+  sqlite3_stmt *sst;
+  sst= sql_prepare(stmt,"(single int)");
+  assert( SQL_STEP(sst) );
+  int rv= sqlite3_column_int(sst,0);
+  sqlite3_finalize(sst);
+  return rv;
+}
+
+void sql_fatal(const char *stmt_what, int sqr, const char *act_what) {
+  fatal("SQL call failed, stmt %s code %d: %s: %s",
+       stmt_what, sqr, sqlite3_errmsg(db), act_what);
+}
+
+void sql_bind(sqlite3_stmt *ss, int index, int value,
+             const char *ss_what, const char *val_what) {
+  debugf("SQL BIND %s #%d = %d = %s\n", ss_what, index, value, val_what);
+  int sqr= sqlite3_bind_int(ss, index, value);
+  if (sqr) sql_fatal(ss_what, sqr,
+                    masprintf("bind #%d (%s)", index, val_what));
+}
+  
+sqlite3_stmt *sql_prepare(const char *stmt, const char *what) {
+  sqlite3_stmt *ssr;
+  debugf("SQL PREPARE %s [[\n%s\n]]\n", what, stmt);
+  SQL_MUST( sqlite3_prepare(db, stmt, -1, &ssr, 0) );
+  return ssr;
+}
+
+int sql_step_wrap(sqlite3_stmt *ssh, const char *ssh_string,
+                 const char *file, int line) {
+  for (;;) {
+    int sqr;
+    sqr= sqlite3_step((ssh));
+    switch (sqr) {
+    case SQLITE_DONE:
+      debugf("SQL %s DONE\n",ssh_string);
+      return 0;
+    case SQLITE_ROW:
+      if (DEBUGP(sql)) {
+       int i;
+       fprintf(debug,"SQL %s R",ssh_string);
+       for (i=0; i<sqlite3_column_count(ssh); i++) {
+         fputc('\t',debug);
+         const char *txt= (const char*)sqlite3_column_text(ssh,i);
+         fputs(txt ? txt : "<null>", debug);
+       }
+       fputs("\n",debug);
+      }
+      return 1;
+    default: fatal("SQL step failed at %s:%d: code %d: %s: %s",
+                  file, line, sqr, sqlite3_errmsg(db), ssh_string);
+    }
+  }
+}