X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=blobdiff_plain;f=yarrg%2Frssql.c;h=043e34ed7790999cc4d09a9295e3dfca6d83f7b3;hp=463ef8b560c76dff97b047b22db55f5e4b7d73ca;hb=024f4a767b43a66e6ff004532f039b874e401b80;hpb=23f08e0abdbd5212e4a867c80f7833e4ebcbb1cf diff --git a/yarrg/rssql.c b/yarrg/rssql.c index 463ef8b..043e34e 100644 --- a/yarrg/rssql.c +++ b/yarrg/rssql.c @@ -1,10 +1,39 @@ +/* + * Route searcher - database helper functions + */ +/* + * This is part of the YARRG website, a tool for assisting + * players of Yohoho Puzzle Pirates. + * + * Copyright (C) 2009 Ian Jackson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * Yohoho and Puzzle Pirates are probably trademarks of Three Rings and + * are used without permission. This program is not endorsed or + * sponsored by Three Rings. + */ #include "rscommon.h" sqlite3 *db; sqlite3_stmt *ss_ipair; +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); @@ -12,17 +41,18 @@ static int busy_handler(void *u, int previous) { return 1; } -void setup(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)"); assert( !SQL_STEP(sst) ); sqlite3_finalize(sst); - setup_value(); + islandtablesz= 1 + sql_single_int("SELECT max(islandid) FROM islands"); + debugf("SQL islandtablesz=%d\n",islandtablesz); } int sql_single_int(const char *stmt) { @@ -41,7 +71,7 @@ void sql_fatal(const char *stmt_what, int sqr, const char *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); + 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)); @@ -54,17 +84,37 @@ 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