diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4e8db8bf9..c42e3eb2e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -117,7 +117,6 @@ set (ERESSEA_SRC morale.c move.c names.c - orderdb.c orderfile.c piracy.c prefix.c @@ -230,7 +229,6 @@ set(TESTS_SRC monsters.test.c move.test.c names.test.c - orderdb.test.c orderfile.test.c piracy.test.c prefix.test.c diff --git a/src/eressea.c b/src/eressea.c index 1221f93e0..8e7da7f7a 100644 --- a/src/eressea.c +++ b/src/eressea.c @@ -28,7 +28,6 @@ #include "creport.h" #include "report.h" #include "names.h" -#include "orderdb.h" #include "reports.h" #include "spells.h" #include "vortex.h" diff --git a/src/kernel/database.c b/src/kernel/database.c index fec8867a2..796aa2380 100644 --- a/src/kernel/database.c +++ b/src/kernel/database.c @@ -14,15 +14,16 @@ void swapdb_close(void) db_driver_close(DB_SWAP); } -dbstring_id db_string_save(const char *s) { +dbrow_id db_string_save(const char *s) { (void)s; return 0; } -dbstring_id dbstring_save(const char *s) { +dbrow_id dbstring_save(const char *s) { return db_driver_string_save(s); } -const char *dbstring_load(dbstring_id id, size_t *size) { +const char *dbstring_load(dbrow_id id, size_t *size) { return db_driver_string_load(id, size); } + diff --git a/src/kernel/database.h b/src/kernel/database.h index 584b21908..8147ccc61 100644 --- a/src/kernel/database.h +++ b/src/kernel/database.h @@ -1,11 +1,11 @@ #pragma once -#include +#include "db/driver.h" -typedef unsigned int dbstring_id; +#include void swapdb_open(void); void swapdb_close(void); -dbstring_id dbstring_save(const char *s); -const char *dbstring_load(dbstring_id id, size_t *size); +dbrow_id dbstring_save(const char *s); +const char *dbstring_load(dbrow_id id, size_t *size); diff --git a/src/kernel/database.test.c b/src/kernel/database.test.c index 3ec44896e..04108aa85 100644 --- a/src/kernel/database.test.c +++ b/src/kernel/database.test.c @@ -3,8 +3,8 @@ #include #include +#include "database.h" #include "db/driver.h" -#include "orderdb.h" #include #include @@ -12,6 +12,17 @@ #include #include +static void test_orderdb(CuTest *tc) { + order_data *od = NULL; + const char * s = "GIB enno 1 Hodor"; + + odata_create(&od, strlen(s) + 1, s); + CuAssertPtrNotNull(tc, od); + CuAssertStrEquals(tc, s, od->_str); + CuAssertTrue(tc, od->_refcount >= 1); + odata_release(od); +} + static void test_save_load_order(CuTest *tc) { order_data *od; int id; @@ -52,6 +63,7 @@ CuSuite *get_db_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_save_load_order); SUITE_ADD_TEST(suite, test_update_faction); + SUITE_ADD_TEST(suite, test_orderdb); return suite; } diff --git a/src/kernel/db/driver.h b/src/kernel/db/driver.h index e127c6662..341653991 100644 --- a/src/kernel/db/driver.h +++ b/src/kernel/db/driver.h @@ -4,6 +4,8 @@ struct order_data; +typedef unsigned int dbrow_id; + extern void odata_create(struct order_data **pdata, size_t len, const char *str); typedef enum database_t { @@ -13,8 +15,8 @@ typedef enum database_t { int db_driver_open(database_t db, const char *dbname); void db_driver_close(database_t db); -int db_driver_order_save(const char *str); -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); -unsigned int db_driver_string_save(const char *s); -const char *db_driver_string_load(unsigned int id, size_t *size); +dbrow_id db_driver_order_save(const char *str); +struct order_data *db_driver_order_load(dbrow_id id); +dbrow_id db_driver_faction_save(dbrow_id id, int no, int turn, const char *email, const char *password); +dbrow_id db_driver_string_save(const char *s); +const char *db_driver_string_load(dbrow_id id, size_t *size); diff --git a/src/kernel/db/sqlite.c b/src/kernel/db/sqlite.c index 9df27f365..9a4dbc9ff 100644 --- a/src/kernel/db/sqlite.c +++ b/src/kernel/db/sqlite.c @@ -35,7 +35,7 @@ static void end_transaction(void) { } } -struct order_data *db_driver_order_load(int id) +struct order_data *db_driver_order_load(dbrow_id id) { struct order_data * od = NULL; int err; @@ -61,7 +61,7 @@ struct order_data *db_driver_order_load(int id) return NULL; } -int db_driver_order_save(const char *str) { +dbrow_id db_driver_order_save(const char *str) { int err; sqlite3_int64 id; @@ -83,7 +83,7 @@ int db_driver_order_save(const char *str) { err = sqlite3_step(g_stmt_insert_order); assert(err == SQLITE_DONE); id = sqlite3_last_insert_rowid(g_swap_db); - assert(id <= INT_MAX); + assert(id > 0 && id <= UINT_MAX); if (g_insert_batchsize > 0) { if (++g_insert_tx_size >= g_insert_batchsize) { @@ -91,11 +91,11 @@ int db_driver_order_save(const char *str) { } } ERRNO_CHECK(); - return (int)id; + return (dbrow_id)id; } -int db_driver_faction_save(int id, int no, int turn, const char *email, const char *password) +dbrow_id db_driver_faction_save(dbrow_id id, int no, int turn, const char *email, const char *password) { sqlite3_int64 row_id; int err; @@ -140,8 +140,8 @@ int db_driver_faction_save(int id, int no, int turn, const char *email, const ch ERRNO_CHECK(); row_id = sqlite3_last_insert_rowid(g_game_db); - assert(row_id <= INT_MAX); - return (int)row_id; + assert(row_id>0 && row_id <= UINT_MAX); + return (dbrow_id)row_id; } static int db_open_game(const char *dbname) { @@ -240,7 +240,7 @@ void db_driver_close(database_t db) ERRNO_CHECK(); } -unsigned int db_driver_string_save(const char *str) { +dbrow_id db_driver_string_save(const char *str) { int err; sqlite3_int64 id; @@ -262,7 +262,7 @@ unsigned int db_driver_string_save(const char *str) { err = sqlite3_step(g_stmt_insert_string); assert(err == SQLITE_DONE); id = sqlite3_last_insert_rowid(g_swap_db); - assert(id <= INT_MAX); + assert(id > 0 && id <= UINT_MAX); if (g_insert_batchsize > 0) { if (++g_insert_tx_size >= g_insert_batchsize) { @@ -270,10 +270,10 @@ unsigned int db_driver_string_save(const char *str) { } } ERRNO_CHECK(); - return (int)id; + return (dbrow_id)id; } -const char *db_driver_string_load(unsigned int id, size_t *size) { +const char *db_driver_string_load(dbrow_id id, size_t *size) { int err; end_transaction(); diff --git a/src/kernel/orderdb.c b/src/kernel/orderdb.c deleted file mode 100644 index 42248c0ef..000000000 --- a/src/kernel/orderdb.c +++ /dev/null @@ -1,43 +0,0 @@ -#include - -#include "kernel/config.h" -#include "kernel/db/driver.h" - -#include "orderdb.h" - -#include - -#include - -#include -#include -#include - -void orderdb_open(void) -{ - const char *dbname; - - dbname = config_get("game.dbswap"); - db_driver_open(DB_SWAP, dbname); -} - -void orderdb_close(void) -{ - db_driver_close(DB_SWAP); -} - -order_data *odata_load(int id) -{ - if (id > 0) { - return db_driver_order_load(id); - } - return NULL; -} - -int odata_save(order_data *od) -{ - if (od->_str) { - return db_driver_order_save(od->_str); - } - return 0; -} diff --git a/src/kernel/orderdb.h b/src/kernel/orderdb.h deleted file mode 100644 index e17bdfea8..000000000 --- a/src/kernel/orderdb.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef H_ORDERDB -#define H_ORDERDB - -#include - -#ifdef __cplusplus -extern "C" { -#endif - - void orderdb_open(void); - void orderdb_close(void); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/orderdb.c b/src/orderdb.c deleted file mode 100644 index 87b6e626c..000000000 --- a/src/orderdb.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#include "kernel/config.h" - -#include "orderdb.h" - -#include - -#include - -#include -#include -#include - diff --git a/src/orderdb.h b/src/orderdb.h deleted file mode 100644 index cc809b1a6..000000000 --- a/src/orderdb.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef H_ORDERDB -#define H_ORDERDB - -#include - -#ifdef __cplusplus -extern "C" { -#endif - - typedef struct order_data { - const char *_str; - int _refcount; - } order_data; - - void odata_create(order_data **pdata, size_t len, const char *str); - void odata_release(order_data * od); - void odata_addref(order_data *od); - - void orderdb_open(void); - void orderdb_close(void); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/orderdb.test.c b/src/orderdb.test.c deleted file mode 100644 index bdb6f0a95..000000000 --- a/src/orderdb.test.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -#include "orderdb.h" - -#include -#include - -#include - -static void test_orderdb(CuTest *tc) { - order_data *od = NULL; - const char * s = "GIB enno 1 Hodor"; - - odata_create(&od, strlen(s) + 1, s); - CuAssertPtrNotNull(tc, od); - CuAssertStrEquals(tc, s, od->_str); - CuAssertTrue(tc, od->_refcount >= 1); - odata_release(od); -} - -CuSuite *get_orderdb_suite(void) -{ - CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_orderdb); - - return suite; -} diff --git a/src/test_eressea.c b/src/test_eressea.c index 2b36d4e03..12c658f91 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -138,7 +138,6 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(monsters); ADD_SUITE(move); ADD_SUITE(names); - ADD_SUITE(orderdb); ADD_SUITE(orderfile); ADD_SUITE(otherfaction); ADD_SUITE(piracy);