From 0cd965169019490bc91a53d89670e86854300521 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 9 Nov 2017 21:12:25 +0100 Subject: [PATCH] fix sqlite linking for tests. implement save_order for sqlite. --- src/CMakeLists.txt | 1 + src/kernel/CMakeLists.txt | 3 ++ src/kernel/dbsqlite.c | 63 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/kernel/dbsqlite.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e5ae8de7a..cfbe6c474 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -267,6 +267,7 @@ install(TARGETS eressea DESTINATION "bin") if (SQLITE3_FOUND) target_link_libraries(eressea ${SQLITE3_LIBRARIES}) +target_link_libraries(test_eressea ${SQLITE3_LIBRARIES}) add_definitions(-DUSE_SQLITE) endif(SQLITE3_FOUND) diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index f4af301db..cbd8a6296 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -37,6 +37,9 @@ jsonconf.test.c ) SET(_DBFILES dbtrie.c) +IF(SQLITE3_FOUND) +SET(_DBFILES dbsqlite.c) +ENDIF(SQLITE3_FOUND) SET(_FILES ${_DBFILES} diff --git a/src/kernel/dbsqlite.c b/src/kernel/dbsqlite.c new file mode 100644 index 000000000..ac970ce4a --- /dev/null +++ b/src/kernel/dbsqlite.c @@ -0,0 +1,63 @@ +#include +#include "db.h" +#include "orderdb.h" + +#include + +#include + +#include +#include +#include + + +#define DBNAME ":memory:" +static sqlite3 *g_db; +static sqlite3_stmt * g_stmt_insert; +static sqlite3_stmt * g_stmt_select; + +order_data *db_load_order(int id) +{ + if (id > 0) { + order_data * od = NULL; + odata_create(&od, 0, NULL); + return od; + } + return NULL; +} + +int db_save_order(order_data *od) +{ + if (od->_str) { + int err; + err = sqlite3_reset(g_stmt_insert); + assert(err == SQLITE_OK); + err = sqlite3_bind_text(g_stmt_insert, 1, od->_str, -1, SQLITE_STATIC); + assert(err == SQLITE_OK); + err = sqlite3_step(g_stmt_insert); + assert(err == SQLITE_DONE); + } + return 0; +} + +void db_open(void) +{ + int err; + + err = sqlite3_open(DBNAME, &g_db); + assert(err == SQLITE_OK); + err = sqlite3_exec(g_db, "CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, data TEXT NOT NULL)", NULL, NULL, NULL); + assert(err == SQLITE_OK); + err = sqlite3_prepare_v2(g_db, "INSERT INTO orders (data) VALUES (?)", -1, &g_stmt_insert, NULL); + assert(err == SQLITE_OK); +} + +void db_close(void) +{ + int err; + + err = sqlite3_finalize(g_stmt_insert); + assert(err == SQLITE_OK); + err = sqlite3_close(g_db); + assert(err == SQLITE_OK); +}