int islandtablesz;
DEBUG_DEFINE_DEBUGF(sql);
+DEBUG_DEFINE_SOME_DEBUGF(sql,debug2f);
static int busy_handler(void *u, int previous) {
debugf("[[DB BUSY %d]]",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)");
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);
+ debug2f("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));
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));
switch (sqr) {
case SQLITE_DONE:
- debugf("SQL %s DONE\n",ssh_string);
+ debug2f("SQL %s DONE\n",ssh_string);
return 0;
case SQLITE_ROW:
- if (DEBUGP(sql)) {
+ if (DEBUGP(sql2)) {
int i;
fprintf(debug,"SQL %s R",ssh_string);
for (i=0; i<sqlite3_column_count(ssh); i++) {