From ff85cda72436c5df88e5dd17be88fc935c9b1b0d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 26 Jan 2018 17:56:24 +0100 Subject: [PATCH] stop using sqlite for a player database --- scripts/run-turn.lua | 16 +--- src/CMakeLists.txt | 13 +--- src/bind_sqlite.c | 88 ---------------------- src/bindings.c | 3 - src/bindings.h | 1 - src/sqlite.c | 172 ------------------------------------------- 6 files changed, 3 insertions(+), 290 deletions(-) delete mode 100644 src/bind_sqlite.c delete mode 100644 src/sqlite.c diff --git a/scripts/run-turn.lua b/scripts/run-turn.lua index 34c1ea4fb..88744db94 100644 --- a/scripts/run-turn.lua +++ b/scripts/run-turn.lua @@ -25,20 +25,6 @@ function callbacks(rules, name, ...) end end -local function dbupdate() - update_scores() - if config.dbname then - dbname = config.basepath..'/'..config.dbname - edb = db.open(dbname) - if edb~=nil then - edb:update_factions() - edb:update_scores() - else - eressea.log.error("could not open "..dbname) - end - end -end - local function write_emails(locales) local files = {} local key @@ -161,7 +147,7 @@ function process(rules, orders) turn_end() -- ageing, etc. write_files(config.locales) - dbupdate() + update_scores() file = '' .. get_turn() .. '.dat' if eressea.write_game(file)~=0 then diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 268d34efd..7518bb2d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -162,13 +162,6 @@ set(SERVER_SRC bind_unit.c ) -if (SQLITE3_FOUND) -set (SERVER_SRC ${SERVER_SRC} - sqlite.c - bind_sqlite.c -) -endif (SQLITE3_FOUND) - if (CURSES_FOUND) set (SERVER_SRC ${SERVER_SRC} gmtool.c @@ -295,15 +288,13 @@ target_link_libraries(convert ${DB_LIBRARIES}) target_link_libraries(eressea ${DB_LIBRARIES}) target_link_libraries(test_eressea ${DB_LIBRARIES}) add_definitions(-DUSE_DB) -endif(DB_FOUND) - -if (SQLITE3_FOUND) +elseif (SQLITE3_FOUND) include_directories (${SQLITE3_INCLUDE_DIR}) target_link_libraries(eressea ${SQLITE3_LIBRARIES}) target_link_libraries(convert ${SQLITE3_LIBRARIES}) target_link_libraries(test_eressea ${SQLITE3_LIBRARIES}) add_definitions(-DUSE_SQLITE) -endif(SQLITE3_FOUND) +endif(DB_FOUND) if (CURSES_FOUND) include_directories (${CURSES_INCLUDE_DIR}) diff --git a/src/bind_sqlite.c b/src/bind_sqlite.c deleted file mode 100644 index 3af4843ea..000000000 --- a/src/bind_sqlite.c +++ /dev/null @@ -1,88 +0,0 @@ -#ifdef _MSC_VER -#include -#endif - -#include "bind_unit.h" -#include "bindings.h" - -#include /* for game_id */ -#include - -#include -#include -#include - -#define LTYPE_DB TOLUA_CAST "db" - -extern int db_update_factions(sqlite3 * db, bool force, int game); -static int tolua_db_update_factions(lua_State * L) -{ - sqlite3 *db = (sqlite3 *)tolua_tousertype(L, 1, 0); - db_update_factions(db, tolua_toboolean(L, 2, 0), game_id()); - return 0; -} - -extern int db_update_scores(sqlite3 * db, bool force); -static int tolua_db_update_scores(lua_State * L) -{ - sqlite3 *db = (sqlite3 *)tolua_tousertype(L, 1, 0); - db_update_scores(db, tolua_toboolean(L, 2, 0)); - return 0; -} - -static int tolua_db_execute(lua_State * L) -{ - sqlite3 *db = (sqlite3 *)tolua_tousertype(L, 1, 0); - const char *sql = tolua_tostring(L, 2, 0); - - int res = sqlite3_exec(db, sql, 0, 0, 0); - - lua_pushinteger(L, res); - return 1; -} - -static int tolua_db_close(lua_State * L) -{ - sqlite3 *db = (sqlite3 *)tolua_tousertype(L, 1, 0); - sqlite3_close(db); - return 0; -} - -static int tolua_db_create(lua_State * L) -{ - sqlite3 *db; - const char *dbname = tolua_tostring(L, 1, 0); - int result = sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READWRITE, 0); - if (result == SQLITE_OK) { - tolua_pushusertype(L, (void *)db, LTYPE_DB); - return 1; - } - return 0; -} - -int tolua_sqlite_open(lua_State * L) -{ - /* register user types */ - - tolua_usertype(L, LTYPE_DB); - - tolua_module(L, NULL, 0); - tolua_beginmodule(L, NULL); - { - tolua_cclass(L, LTYPE_DB, LTYPE_DB, TOLUA_CAST "", NULL); - tolua_beginmodule(L, LTYPE_DB); - { - tolua_function(L, TOLUA_CAST "open", &tolua_db_create); - tolua_function(L, TOLUA_CAST "close", &tolua_db_close); - - tolua_function(L, TOLUA_CAST "update_factions", - &tolua_db_update_factions); - tolua_function(L, TOLUA_CAST "update_scores", &tolua_db_update_scores); - tolua_function(L, TOLUA_CAST "execute", &tolua_db_execute); - } - tolua_endmodule(L); - - } - tolua_endmodule(L); - return 0; -} diff --git a/src/bindings.c b/src/bindings.c index c5494e64b..4bf999af1 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -1119,9 +1119,6 @@ lua_State *lua_init(const dictionary *inifile) { register_tolua_helpers(); tolua_bindings_open(L, inifile); tolua_eressea_open(L); -#ifdef USE_SQLITE - tolua_sqlite_open(L); -#endif tolua_unit_open(L); tolua_building_open(L); tolua_ship_open(L); diff --git a/src/bindings.h b/src/bindings.h index b5ebf916b..038afd31e 100755 --- a/src/bindings.h +++ b/src/bindings.h @@ -19,7 +19,6 @@ extern "C" { struct selist; int tolua_toid(struct lua_State *L, int idx, int def); - int tolua_sqlite_open(struct lua_State *L); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_itemlist_next(struct lua_State *L); int tolua_selist_push(struct lua_State *L, const char *list_type, diff --git a/src/sqlite.c b/src/sqlite.c deleted file mode 100644 index e25d105d2..000000000 --- a/src/sqlite.c +++ /dev/null @@ -1,172 +0,0 @@ -#ifdef _MSC_VER -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef struct db_faction { - int uid; - int no; - char *email; - char *name; -} db_faction; - -static struct selist * -read_factions(sqlite3 * db, int game_id) { - int res; - selist *result = 0; - const char * sql = - "SELECT f.id, fd.code, fd.name, fd.email FROM faction f" - " LEFT OUTER JOIN faction_data fd" - " WHERE f.id=fd.faction_id AND f.game_id=? AND" - " fd.turn=(SELECT MAX(turn) FROM faction_data fx WHERE fx.faction_id=f.id)" - " ORDER BY f.id"; - sqlite3_stmt *stmt = 0; - sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - sqlite3_bind_int(stmt, 1, game_id); - - res = sqlite3_step(stmt); - while (res == SQLITE_ROW) { - const char * text; - db_faction * dbf = (db_faction*)calloc(1, sizeof(db_faction)); - dbf->uid = (int)sqlite3_column_int64(stmt, 0); - text = (const char *)sqlite3_column_text(stmt, 1); - if (text) dbf->no = atoi36(text); - text = (const char *)sqlite3_column_text(stmt, 2); - if (text) dbf->name = str_strdup(text); - text = (const char *)sqlite3_column_text(stmt, 3); - if (text) dbf->email = str_strdup(text); - selist_push(&result, dbf); - res = sqlite3_step(stmt); - } - sqlite3_finalize(stmt); - return result; -} - -static int insert_faction(sqlite3 *db, int game_id, faction *f) { - const char *sql = "INSERT INTO faction (game_id, race) VALUES (?, ?)"; - sqlite3_stmt *stmt = 0; - sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - sqlite3_bind_int(stmt, 1, game_id); - sqlite3_bind_text(stmt, 2, f->race->_name, -1, SQLITE_STATIC); - sqlite3_step(stmt); - sqlite3_finalize(stmt); - return (int)sqlite3_last_insert_rowid(db); -} - -static void update_faction(sqlite3 *db, const faction *f) { - char code[5]; - const char *sql = - "INSERT INTO faction_data (faction_id, code, name, email, lang, turn)" - " VALUES (?, ?, ?, ?, ?, ?)"; - sqlite3_stmt *stmt = 0; - strcpy(code, itoa36(f->no)); - sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - sqlite3_bind_int(stmt, 1, f->subscription); - sqlite3_bind_text(stmt, 2, code, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, f->name, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 4, f->email, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 5, locale_name(f->locale), -1, SQLITE_STATIC); - sqlite3_bind_int(stmt, 6, turn); - sqlite3_step(stmt); - sqlite3_finalize(stmt); -} - -struct cb_data { - const faction *f; - sqlite3 *db; - db_faction *dbf; -}; - -static bool db_faction_cb(void *el, void *arg) { - db_faction *df = (db_faction *)el; - struct cb_data *cb = (struct cb_data *)arg; - const faction *f = cb->f; - - /* FIXME comparing name and email is inaccurate ... */ - if (f->no == df->no || strcmp(faction_getemail(f), df->email?df->email:"") == 0 || strcmp(f->name, df->name) == 0) { - cb->dbf = df; - } - if (f->subscription == df->uid) { - cb->dbf = df; - return false; - } - return true; -} - -int db_update_factions(sqlite3 * db, bool force, int game_id) { - selist *ql = read_factions(db, game_id); - if (ql) { - faction *f; - struct cb_data cbdata; - cbdata.db = db; - cbdata.dbf = NULL; - sqlite3_exec(db, "BEGIN", 0, 0, 0); - for (f = factions; f; f = f->next) { - bool update = force; - cbdata.f = f; - selist_foreach_ex(ql, db_faction_cb, &cbdata); - if (cbdata.dbf) { - const db_faction *dbf = cbdata.dbf; - if (dbf->uid != f->subscription) { - log_warning("faction %s(%d) not found in database, but matches %d\n", itoa36(f->no), f->subscription, dbf->uid); - f->subscription = dbf->uid; - } - update = (dbf->no != f->no) || (strcmp(faction_getemail(f), dbf->email?dbf->email:"") != 0) || (strcmp(f->name, dbf->name) != 0); - } - else { - f->subscription = insert_faction(db, game_id, f); - log_warning("faction %s not found in database, created as %d\n", itoa36(f->no), f->subscription); - update = true; - } - if (update) { - update_faction(db, f); - log_debug("faction %s updated\n", itoa36(f->no)); - } - } - sqlite3_exec(db, "COMMIT", 0, 0, 0); - } - return SQLITE_OK; -} - -int db_update_scores(sqlite3 * db, bool force) -{ - /* - const char *sselist_ins = - "INSERT OR FAIL INTO score (value,faction_id,turn) VALUES (?,?,?)"; - sqlite3_stmt *stmt_ins = stmt_cache_get(db, sselist_ins); - const char *sselist_upd = - "UPDATE score set value=? WHERE faction_id=? AND turn=?"; - sqlite3_stmt *stmt_upd = stmt_cache_get(db, sselist_upd); - faction *f; - sqlite3_exec(db, "BEGIN", 0, 0, 0); - for (f = factions; f; f = f->next) { - int res; - sqlite3_bind_int(stmt_ins, 1, f->score); - sqlite3_bind_int64(stmt_ins, 2, f->subscription); - sqlite3_bind_int(stmt_ins, 3, turn); - res = sqlite3_step(stmt_ins); - if (res == SQLITE_CONSTRAINT) { - sqlite3_bind_int(stmt_upd, 1, f->score); - sqlite3_bind_int64(stmt_upd, 2, f->subscription); - sqlite3_bind_int(stmt_upd, 3, turn); - res = sqlite3_step(stmt_upd); - sqlite3_reset(stmt_upd); - } - sqlite3_reset(stmt_ins); - } - sqlite3_exec(db, "COMMIT", 0, 0, 0); - */ - return SQLITE_OK; -}