sqlite3 *db;
sqlite3_stmt *ss_ipair;
+DEBUG_DEFINE_DEBUGF(sql);
+
static int busy_handler(void *u, int previous) {
- if (DEBUGP(sql)) fprintf(stderr,"[[DB BUSY %d]]",previous);
+ debugf("[[DB BUSY %d]]",previous);
sysassert(! usleep(5000) );
return 1;
}
SQL_MUST( sqlite3_open("OCEAN-Midnight.db", &db) );
SQL_MUST( sqlite3_busy_handler(db, busy_handler, 0) );
- SQL_MUST( sqlite3_prepare(db, "BEGIN", -1, &sst, 0) );
+ sst= sql_prepare("BEGIN","(begin)");
assert( !SQL_STEP(sst) );
sqlite3_finalize(sst);
int sql_single_int(const char *stmt) {
sqlite3_stmt *sst;
- SQL_MUST( sqlite3_prepare(db, stmt, -1,&sst,0) );
+ 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 (;;) {
sqr= sqlite3_step((ssh));
switch (sqr) {
case SQLITE_DONE:
- if (DEBUGP(sql))
- fprintf(stderr,"SQL %s DONE\n",ssh_string);
+ debugf("SQL %s DONE\n",ssh_string);
return 0;
case SQLITE_ROW:
if (DEBUGP(sql)) {
int i;
- fprintf(stderr,"SQL %s R",ssh_string);
+ fprintf(debug,"SQL %s R",ssh_string);
for (i=0; i<sqlite3_column_count(ssh); i++) {
- fputc('\t',stderr);
+ fputc('\t',debug);
const char *txt= (const char*)sqlite3_column_text(ssh,i);
- fputs(txt ? txt : "<null>", stderr);
+ fputs(txt ? txt : "<null>", debug);
}
- fputs("\n",stderr);
+ fputs("\n",debug);
}
return 1;
default: fatal("SQL step failed at %s:%d: code %d: %s: %s",