Changes to settings.c db queries

Sort of experimental. No harm but perhaps not an improvement.
This commit is contained in:
Linus Probert 2018-05-05 20:59:53 +02:00
parent f290a0dc1c
commit 677a7d8b69
1 changed files with 24 additions and 8 deletions

View File

@ -32,6 +32,20 @@ static Settings settings;
static const char *KEY_MUSIC_ENABLED = "music_enabled"; static const char *KEY_MUSIC_ENABLED = "music_enabled";
static const char *KEY_SOUND_ENABLED = "sound_enabled"; static const char *KEY_SOUND_ENABLED = "sound_enabled";
typedef struct db_query {
char *stmt;
int (*cb)(void*, int, char**, char**);
void *cb_arg;
} db_query;
db_query MIGRATE_COMMANDS[] = {
{ "CREATE TABLE IF NOT EXISTS settings_int(key TEXT PRIMARY KEY, value INTEGER)", NULL, NULL },
{ NULL, NULL, NULL } // Sentinel
};
static int load_settings_cb(void *unused, int count, char **values, char **colNames);
db_query LOAD_SETTINGS = { "SELECT * FROM settings_int", load_settings_cb, NULL };
static void static void
set_default_settings(void) set_default_settings(void)
{ {
@ -40,11 +54,9 @@ set_default_settings(void)
} }
static void static void
execute_statement(const char *stmt, execute_statement(db_query *query)
int(*cb)(void*, int, char**, char**),
void *cb_arg)
{ {
if (!db_execute_stmnt(stmt, db, cb, cb_arg)) { if (!db_execute_stmnt(query->stmt, db, query->cb, query->cb_arg)) {
db_close(&db); db_close(&db);
fatal("Exiting"); fatal("Exiting");
} }
@ -53,9 +65,13 @@ execute_statement(const char *stmt,
static void static void
create_tables(void) create_tables(void)
{ {
execute_statement("CREATE TABLE IF NOT EXISTS settings_int(key TEXT PRIMARY KEY, value INTEGER)", for (unsigned int i = 0;; ++i) {
NULL, db_query *query = &MIGRATE_COMMANDS[i];
NULL); if (query->stmt == NULL)
break;
execute_statement(query);
}
} }
static int static int
@ -84,7 +100,7 @@ load_settings_cb(void *unused, int count, char **values, char **colNames)
static void static void
load_settings(void) load_settings(void)
{ {
execute_statement("SELECT * FROM settings_int", load_settings_cb, NULL); execute_statement(&LOAD_SETTINGS);
} }
void void