From 4e5240628ee6f3a3b3adb8d6c492833c0a912a48 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 25 Sep 2018 18:52:29 +0200 Subject: [PATCH] Extract gamedb open/close to separate functions. --- src/gamedb.c | 20 +++++++++++++++----- src/kernel/database.c | 7 +++++-- src/kernel/db/driver.h | 9 +++++++-- src/kernel/db/sqlite.c | 42 +++++++++++++++++++++++++++++------------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/gamedb.c b/src/gamedb.c index cf9a922bc..e3c16b06e 100644 --- a/src/gamedb.c +++ b/src/gamedb.c @@ -4,17 +4,27 @@ #include "gamedb.h" +#include "kernel/db/driver.h" #include "kernel/database.h" #include "kernel/faction.h" -void gamedb_update(void) +int gamedb_update(void) { faction *f; + int err; + const char *dbname; - for (f = factions; f; f = f->next) { - int uid = dblib_save_faction(f, turn); - if (uid > 0) { - f->uid = uid; + dbname = config_get("game.dbname"); + + err = db_driver_open(DB_GAME, dbname); + if (err == 0) { + for (f = factions; f; f = f->next) { + int uid = dblib_save_faction(f, turn); + if (uid > 0) { + f->uid = uid; + } } + db_driver_close(DB_GAME); } + return err; } diff --git a/src/kernel/database.c b/src/kernel/database.c index c27ca21fc..137665e31 100644 --- a/src/kernel/database.c +++ b/src/kernel/database.c @@ -34,10 +34,13 @@ int dblib_save_faction(const faction *f, int turn) { void dblib_open(void) { - db_driver_open(); + const char *dbname; + + dbname = config_get("game.dbswap"); + db_driver_open(DB_SWAP, dbname); } void dblib_close(void) { - db_driver_close(); + db_driver_close(DB_SWAP); } diff --git a/src/kernel/db/driver.h b/src/kernel/db/driver.h index 1e408e37e..26b0461e3 100644 --- a/src/kernel/db/driver.h +++ b/src/kernel/db/driver.h @@ -2,8 +2,13 @@ struct order_data; -void db_driver_open(void); -void db_driver_close(void); +typedef enum database_t { + DB_SWAP, + DB_GAME, +} database_t; + +int db_driver_open(database_t db, const char *dbname); +void db_driver_close(database_t db); int db_driver_order_save(struct order_data *od); struct order_data *db_driver_order_load(int id); int db_driver_faction_save(int id, int no, int turn, const char *email, const char *password); diff --git a/src/kernel/db/sqlite.c b/src/kernel/db/sqlite.c index 49dbd001e..70cacaf48 100644 --- a/src/kernel/db/sqlite.c +++ b/src/kernel/db/sqlite.c @@ -144,15 +144,9 @@ int db_driver_faction_save(int id, int no, int turn, const char *email, const ch return (int)row_id; } -void db_driver_open(void) -{ +static int db_open_game(const char *dbname) { int err; - const char *dbname, *dbtemp; - ERRNO_CHECK(); - - dbname = config_get("game.dbname"); - if (!dbname) dbname = ""; err = sqlite3_open(dbname, &g_game_db); assert(err == SQLITE_OK); err = sqlite3_exec(g_game_db, "CREATE TABLE IF NOT EXISTS factions (id INTEGER PRIMARY KEY, no INTEGER NOT NULL, email VARCHAR(128), password VARCHAR(128), turn INTEGER NOT NULL)", NULL, NULL, NULL); @@ -162,9 +156,16 @@ void db_driver_open(void) err = sqlite3_prepare_v2(g_game_db, "INSERT INTO factions (no, turn, email, password) VALUES (?,?,?,?)", -1, &g_stmt_insert_faction, NULL); assert(err == SQLITE_OK); + ERRNO_CHECK(); + return 0; +} + +static int db_open_swap(const char *dbname) { + int err; + g_order_batchsize = config_get_int("game.dbbatch", 100); - dbtemp = config_get("game.db.temp"); - err = sqlite3_open(dbtemp ? dbtemp : dbname, &g_temp_db); + + err = sqlite3_open(dbname, &g_temp_db); assert(err == SQLITE_OK); err = sqlite3_exec(g_temp_db, "PRAGMA journal_mode=OFF", NULL, NULL, NULL); assert(err == SQLITE_OK); @@ -176,16 +177,30 @@ void db_driver_open(void) assert(err == SQLITE_OK); err = sqlite3_prepare_v2(g_temp_db, "SELECT data FROM orders WHERE id=?", -1, &g_stmt_select_order, NULL); assert(err == SQLITE_OK); - ERRNO_CHECK(); + return 0; } -void db_driver_close(void) +int db_driver_open(database_t db, const char *dbname) +{ + ERRNO_CHECK(); + + if (db == DB_SWAP) { + return db_open_swap(dbname); + } + else if (db == DB_GAME) { + return db_open_game(dbname); + } + return -1; +} + +void db_driver_close(database_t db) { int err; ERRNO_CHECK(); - if (g_temp_db) { + if (db == DB_SWAP) { + assert(g_temp_db); err = sqlite3_finalize(g_stmt_select_order); assert(err == SQLITE_OK); err = sqlite3_finalize(g_stmt_insert_order); @@ -193,7 +208,8 @@ void db_driver_close(void) err = sqlite3_close(g_temp_db); assert(err == SQLITE_OK); } - if (g_game_db) { + else if (db == DB_GAME) { + assert(g_game_db); err = sqlite3_finalize(g_stmt_update_faction); assert(err == SQLITE_OK); err = sqlite3_finalize(g_stmt_insert_faction);