organize database drivers in their own directory

This commit is contained in:
Enno Rehling 2017-11-18 12:54:48 +01:00
parent 9d328a07f2
commit 2224b59ff9
14 changed files with 224 additions and 152 deletions

View File

@ -134,6 +134,7 @@ set (ERESSEA_SRC
${TRIGGERS_SRC} ${TRIGGERS_SRC}
${ATTRIBUTES_SRC} ${ATTRIBUTES_SRC}
${KERNEL_SRC} ${KERNEL_SRC}
${DB_SRC}
${UTIL_SRC} ${UTIL_SRC}
) )

View File

@ -24,10 +24,10 @@
#include <util/functions.h> #include <util/functions.h>
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/curse.h> #include <kernel/curse.h>
#include <kernel/db.h>
#include <kernel/equipment.h> #include <kernel/equipment.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/xmlreader.h> #include <kernel/xmlreader.h>
#include <kernel/database.h>
#include <modules/gmcmd.h> #include <modules/gmcmd.h>
#include <modules/xmas.h> #include <modules/xmas.h>
#include <items/xerewards.h> #include <items/xerewards.h>

View File

@ -11,7 +11,7 @@ command.test.c
config.test.c config.test.c
# connection.test.c # connection.test.c
curse.test.c curse.test.c
db.test.c database.test.c
equipment.test.c equipment.test.c
faction.test.c faction.test.c
group.test.c group.test.c
@ -36,9 +36,9 @@ jsonconf.test.c
# xmlreader.test.c # xmlreader.test.c
) )
SET(_DBFILES dbtrie.c) SET(_DBFILES db/critbit.c)
IF(SQLITE3_FOUND) IF(SQLITE3_FOUND)
SET(_DBFILES dbsqlite.c) SET(_DBFILES db/sqlite.c)
ENDIF(SQLITE3_FOUND) ENDIF(SQLITE3_FOUND)
SET(_FILES SET(_FILES
@ -52,6 +52,7 @@ command.c
config.c config.c
connection.c connection.c
curse.c curse.c
database.c
equipment.c equipment.c
faction.c faction.c
group.c group.c

58
src/kernel/database.c Normal file
View File

@ -0,0 +1,58 @@
#include <platform.h>
#include "database.h"
#include <platform.h>
#include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/orderdb.h>
#include <util/log.h>
#ifdef USE_SQLITE
#include "db/sqlite.h"
#else
#include "db/critbit.h"
#endif
order_data *db_load_order(int id)
{
if (id > 0) {
#ifdef USE_SQLITE
return db_sqlite_order_load(id);
#else
return db_critbit_order_load(id);
#endif
}
return NULL;
}
int db_save_order(order_data *od)
{
if (od->_str) {
#ifdef USE_SQLITE
return db_sqlite_order_save(od);
#else
return db_critbit_order_save(od);
#endif
}
return 0;
}
void db_open(void)
{
#ifdef USE_SQLITE
db_sqlite_open();
#else
db_critbit_open();
#endif
}
void db_close(void)
{
#ifdef USE_SQLITE
db_sqlite_close();
#else
db_critbit_close();
#endif
}

View File

@ -1,7 +1,7 @@
#include <platform.h> #include <platform.h>
#include "db.h" #include "critbit.h"
#include "orderdb.h"
#include <kernel/orderdb.h>
#include <util/log.h> #include <util/log.h>
#include <critbit.h> #include <critbit.h>
@ -18,32 +18,30 @@ struct cb_entry {
order_data *data; order_data *data;
}; };
order_data *db_load_order(int id) order_data *db_critbit_order_load(int id)
{ {
void * match; void * match;
if (id > 0) { assert(id>0);
if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) { if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) {
struct cb_entry *ent = (struct cb_entry *)match; struct cb_entry *ent = (struct cb_entry *)match;
order_data * od = ent->data; order_data * od = ent->data;
++od->_refcount; ++od->_refcount;
return od; return od;
} }
}
return NULL; return NULL;
} }
int db_save_order(order_data *od) int db_critbit_order_save(order_data *od)
{ {
if (od->_str) {
struct cb_entry ent; struct cb_entry ent;
assert(od && od->_str);
++od->_refcount; ++od->_refcount;
ent.id = ++auto_id; ent.id = ++auto_id;
ent.data = od; ent.data = od;
cb_insert(&cb_orders, &ent, sizeof(ent)); cb_insert(&cb_orders, &ent, sizeof(ent));
return ent.id; return ent.id;
}
return 0;
} }
static int free_data_cb(const void *match, const void *key, size_t keylen, static int free_data_cb(const void *match, const void *key, size_t keylen,
@ -55,13 +53,13 @@ static int free_data_cb(const void *match, const void *key, size_t keylen,
return 0; return 0;
} }
void db_open(void) void db_critbit_open(void)
{ {
assert(auto_id == -1); assert(auto_id == -1);
auto_id = 0; auto_id = 0;
} }
void db_close(void) void db_critbit_close(void)
{ {
cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL); cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL);
cb_clear(&cb_orders); cb_clear(&cb_orders);

8
src/kernel/db/critbit.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
struct order_data;
void db_critbit_open(void);
void db_critbit_close(void);
int db_critbit_order_save(struct order_data *od);
struct order_data *db_critbit_order_load(int id);

121
src/kernel/db/sqlite.c Normal file
View File

@ -0,0 +1,121 @@
#include <platform.h>
#include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/orderdb.h>
#include <util/log.h>
#include <sqlite3.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
static sqlite3 *g_db;
static sqlite3_stmt * g_stmt_insert;
static sqlite3_stmt * g_stmt_select;
static int g_order_batchsize;
static int g_order_tx_size;
order_data *db_sqlite_order_load(int id)
{
order_data * od = NULL;
int err;
if (g_order_tx_size > 0) {
g_order_tx_size = 0;
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
err = sqlite3_reset(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_bind_int(g_stmt_select, 1, id);
assert(err == SQLITE_OK);
do {
err = sqlite3_step(g_stmt_select);
if (err == SQLITE_ROW) {
const unsigned char *text;
int bytes;
bytes = sqlite3_column_bytes(g_stmt_select, 0);
assert(bytes > 0);
text = sqlite3_column_text(g_stmt_select, 0);
odata_create(&od, 1+(size_t)bytes, (const char *)text);
return od;
}
} while (err == SQLITE_ROW);
assert(err == SQLITE_DONE);
return NULL;
}
int db_sqlite_order_save(order_data *od)
{
int err;
sqlite3_int64 id;
assert(od && od->_str);
if (g_order_batchsize > 0) {
if (g_order_tx_size == 0) {
err = sqlite3_exec(g_db, "BEGIN TRANSACTION", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
}
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);
id = sqlite3_last_insert_rowid(g_db);
assert(id <= INT_MAX);
if (g_order_batchsize > 0) {
if (++g_order_tx_size >= g_order_batchsize) {
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
g_order_tx_size = 0;
}
}
return (int)id;
}
void db_sqlite_open(void)
{
int err;
const char *dbname;
g_order_batchsize = config_get_int("game.dbbatch", 100);
dbname = config_get("game.dbname");
if (!dbname) {
dbname = "";
}
err = sqlite3_open(dbname, &g_db);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA journal_mode=OFF", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA synchronous=OFF", NULL, NULL, NULL);
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);
err = sqlite3_prepare_v2(g_db, "SELECT data FROM orders WHERE id = ?", -1, &g_stmt_select, NULL);
assert(err == SQLITE_OK);
}
void db_sqlite_close(void)
{
int err;
err = sqlite3_finalize(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_finalize(g_stmt_insert);
assert(err == SQLITE_OK);
err = sqlite3_close(g_db);
assert(err == SQLITE_OK);
}

8
src/kernel/db/sqlite.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
struct order_data;
void db_sqlite_open(void);
void db_sqlite_close(void);
int db_sqlite_order_save(struct order_data *od);
struct order_data *db_sqlite_order_load(int id);

View File

@ -1,123 +0,0 @@
#include <platform.h>
#include "db.h"
#include "orderdb.h"
#include "config.h"
#include <util/log.h>
#include <sqlite3.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
static sqlite3 *g_db;
static sqlite3_stmt * g_stmt_insert;
static sqlite3_stmt * g_stmt_select;
static int g_order_batchsize;
static int g_order_tx_size;
order_data *db_load_order(int id)
{
if (id > 0) {
order_data * od = NULL;
int err;
if (g_order_tx_size > 0) {
g_order_tx_size = 0;
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
err = sqlite3_reset(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_bind_int(g_stmt_select, 1, id);
assert(err == SQLITE_OK);
do {
err = sqlite3_step(g_stmt_select);
if (err == SQLITE_ROW) {
const unsigned char *text;
int bytes;
bytes = sqlite3_column_bytes(g_stmt_select, 0);
assert(bytes > 0);
text = sqlite3_column_text(g_stmt_select, 0);
odata_create(&od, 1+(size_t)bytes, (const char *)text);
return od;
}
} while (err == SQLITE_ROW);
assert(err == SQLITE_DONE);
}
return NULL;
}
int db_save_order(order_data *od)
{
if (od->_str) {
int err;
sqlite3_int64 id;
if (g_order_batchsize > 0) {
if (g_order_tx_size == 0) {
err = sqlite3_exec(g_db, "BEGIN TRANSACTION", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
}
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);
id = sqlite3_last_insert_rowid(g_db);
assert(id <= INT_MAX);
if (g_order_batchsize > 0) {
if (++g_order_tx_size >= g_order_batchsize) {
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
g_order_tx_size = 0;
}
}
return (int)id;
}
return 0;
}
void db_open(void)
{
int err;
const char *dbname;
g_order_batchsize = config_get_int("game.dbbatch", 100);
dbname = config_get("game.dbname");
if (!dbname) {
dbname = "";
}
err = sqlite3_open(dbname, &g_db);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA journal_mode=OFF", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA synchronous=OFF", NULL, NULL, NULL);
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);
err = sqlite3_prepare_v2(g_db, "SELECT data FROM orders WHERE id = ?", -1, &g_stmt_select, NULL);
assert(err == SQLITE_OK);
}
void db_close(void)
{
int err;
err = sqlite3_finalize(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_finalize(g_stmt_insert);
assert(err == SQLITE_OK);
err = sqlite3_close(g_db);
assert(err == SQLITE_OK);
}

View File

@ -1,5 +1,5 @@
#include <platform.h> #include <platform.h>
#include "db.h" #include "database.h"
#include "orderdb.h" #include "orderdb.h"
#include <util/log.h> #include <util/log.h>

View File

@ -20,7 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/log.h> #include <util/log.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/db.h> #include <kernel/database.h>
#include <kernel/version.h> #include <kernel/version.h>
#include <kernel/save.h> #include <kernel/save.h>
#include <util/filereader.h> #include <util/filereader.h>

View File

@ -1,7 +1,7 @@
#include <platform.h> #include <platform.h>
#include <eressea.h> #include <eressea.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/db.h> #include <kernel/database.h>
#include <CuTest.h> #include <CuTest.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>