Borke out some of the settings sqlite code to it's own module.

This commit is contained in:
Linus Probert 2018-04-16 21:19:26 +02:00
parent f65f135bdf
commit cab3b501fc
4 changed files with 79 additions and 21 deletions

View File

@ -121,6 +121,7 @@ add_executable(breakhack
src/vector2d src/vector2d
src/map_room_modifiers src/map_room_modifiers
src/sqlite3 src/sqlite3
src/db
src/settings src/settings
) )

41
src/db.c Normal file
View File

@ -0,0 +1,41 @@
#include "db.h"
#include "sqlite3.h"
#include "util.h"
bool
db_open(const char *file, sqlite3 **db)
{
debug("Opening DB: %s", file);
int result = sqlite3_open(file, db);
if (result) {
error("Failed to open db: %s", sqlite3_errmsg(db));
}
return result == 0;
}
bool
db_execute_stmnt(const char *stmnt,
sqlite3 *db,
int(*cb)(void*, int, char**, char**),
void *cb_arg)
{
debug("Executing sql: %s", stmnt);
char *errorMsg = NULL;
if (sqlite3_exec(db, stmnt, cb, cb_arg, &errorMsg)) {
error("Faled to execute statement: %s", stmnt);
error("Sqlite3: %s", errorMsg);
sqlite3_free(errorMsg);
return false;
}
return true;
}
void db_close(sqlite3 **db)
{
char *fname = sqlite3_db_filename(*db, NULL);
debug("Closing db: %s", fname);
sqlite3_close(*db);
*db = NULL;
}

19
src/db.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef DB_H_
#define DB_H_
#include <stdbool.h>
#include "sqlite3.h"
bool
db_open(const char *file, sqlite3 **db);
bool
db_execute_stmnt(const char *stmnt,
sqlite3 *db,
int(*cb)(void*, int, char**, char**),
void *cb_arg);
void
db_close(sqlite3 **db);
#endif // DB_H_

View File

@ -22,6 +22,7 @@
#include "sqlite3.h" #include "sqlite3.h"
#include "util.h" #include "util.h"
#include "defines.h" #include "defines.h"
#include "db.h"
#define SETTINGS_DB_FILE ".data.db" #define SETTINGS_DB_FILE ".data.db"
@ -40,17 +41,11 @@ set_default_settings(void)
static void static void
execute_statement(const char *stmt, execute_statement(const char *stmt,
int (*cb)(void*, int, char**, char**), int(*cb)(void*, int, char**, char**),
void *cb_arg) void *cb_arg)
{ {
char *errorMsg; if (!db_execute_stmnt(stmt, db, cb, cb_arg)) {
db_close(&db);
set_default_settings();
if (sqlite3_exec(db, stmt, cb, cb_arg, &errorMsg)) {
error("Faled to execture statement: %s", stmt);
error("Sqlite3: %s", errorMsg);
sqlite3_free(errorMsg);
sqlite3_close(db);
fatal("Exiting"); fatal("Exiting");
} }
} }
@ -59,8 +54,8 @@ static void
create_tables(void) create_tables(void)
{ {
execute_statement("CREATE TABLE IF NOT EXISTS settings_int(key TEXT PRIMARY KEY, value INTEGER)", execute_statement("CREATE TABLE IF NOT EXISTS settings_int(key TEXT PRIMARY KEY, value INTEGER)",
NULL, NULL,
NULL); NULL);
} }
static int static int
@ -73,12 +68,13 @@ load_settings_cb(void *unused, int count, char **values, char **colNames)
if (strcmp("key", colNames[i]) != 0) if (strcmp("key", colNames[i]) != 0)
continue; continue;
debug("Loading setting: %s = %s", values[i], values[i+1]); debug("Loading setting: %s = %s", values[i], values[i + 1]);
if (!strcmp(KEY_SOUND_ENABLED, values[i])) { if (!strcmp(KEY_SOUND_ENABLED, values[i])) {
settings.sound_enabled = (bool) atoi(values[i+1]); settings.sound_enabled = (bool)atoi(values[i + 1]);
i += 2; i += 2;
} else if (!strcmp(KEY_MUSIC_ENABLED, values[i])) { }
settings.music_enabled = (bool) atoi(values[i+1]); else if (!strcmp(KEY_MUSIC_ENABLED, values[i])) {
settings.music_enabled = (bool)atoi(values[i + 1]);
i += 2; i += 2;
} }
} }
@ -94,12 +90,11 @@ load_settings(void)
void void
settings_init(void) settings_init(void)
{ {
int result = sqlite3_open(SETTINGS_DB_FILE, &db); if (!db_open(SETTINGS_DB_FILE, &db)) {
if (result) { db_close(&db);
error("Failed to open settings db: %s", sqlite3_errmsg(db));
sqlite3_close(db);
fatal("Exiting"); fatal("Exiting");
} }
set_default_settings();
create_tables(); create_tables();
load_settings(); load_settings();
} }
@ -107,6 +102,8 @@ settings_init(void)
static void static void
save_setting_int(const char *key, int value) save_setting_int(const char *key, int value)
{ {
// TODO(Linus): Move this into db.c, probably using varargs
const char *stmtStr = "INSERT OR REPLACE INTO settings_int(key, value) values (?, ?)"; const char *stmtStr = "INSERT OR REPLACE INTO settings_int(key, value) values (?, ?)";
const char *pzTest; const char *pzTest;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
@ -147,5 +144,5 @@ void
settings_close(void) settings_close(void)
{ {
settings_save(); settings_save();
sqlite3_close(db); db_close(&db);
} }