reduce dependeny spaghetti for database code.

This commit is contained in:
Enno Rehling 2018-09-28 20:50:24 +02:00
parent 17f7aa2c0a
commit 7b9e72e559
18 changed files with 98 additions and 126 deletions

View File

@ -144,6 +144,7 @@ set (ERESSEA_SRC
set(CHECK_SRC
checker.c
orderfile.c
)
set(SERVER_SRC

View File

@ -12,6 +12,7 @@
#include <kernel/save.h>
#include <util/language.h>
#include <util/log.h>
#include <stream.h>
#include <stdio.h>
@ -34,7 +35,13 @@ int eressea_write_game(const char * filename) {
}
int eressea_read_orders(const char * filename) {
return readorders(filename);
FILE * F = fopen(filename, "r");
if (!F) {
perror(filename);
return -1;
}
log_info("reading orders from %s", filename);
return readorders(F);
}
int eressea_export_json(const char * filename, int flags) {

View File

@ -2,7 +2,21 @@
#include <platform.h>
#endif
#include <stdio.h>
#include "orderfile.h"
int main(int argc, char **argv) {
FILE * F = stdin;
if (argc >= 1) {
const char *filename = argv[1];
F = fopen(filename, "r");
if (!F) {
perror(filename);
return -1;
}
readorders(F);
fclose(F);
}
return 0;
}

View File

@ -1,14 +1,14 @@
#include <platform.h>
#include "eressea.h"
#include "kernel/building.h"
#include "kernel/calendar.h"
#include "kernel/config.h"
#include "kernel/curse.h"
#include "kernel/faction.h"
#include "kernel/building.h"
#include "kernel/equipment.h"
#include "kernel/faction.h"
#include "kernel/item.h"
#include "kernel/database.h"
#include "kernel/orderdb.h"
#include "util/functions.h"
#include "util/language.h"
@ -54,12 +54,12 @@ void game_done(void)
free_locales();
#endif
kernel_done();
dblib_close();
orderdb_close();
}
void game_init(void)
{
dblib_open();
orderdb_open();
errno = 0;
kernel_init();
register_triggers();

View File

@ -6,7 +6,6 @@
#include "kernel/config.h"
#include "kernel/calendar.h"
#include "kernel/database.h"
#include "kernel/faction.h"
#include "kernel/db/driver.h"
@ -21,7 +20,7 @@ int gamedb_update(void)
err = db_driver_open(DB_GAME, dbname);
if (err == 0) {
for (f = factions; f; f = f->next) {
int uid = dblib_save_faction(f, turn);
int uid = db_driver_faction_save(f->uid, f->no, turn, f->email, f->_password);
if (uid > 0) {
f->uid = uid;
}

View File

@ -55,7 +55,6 @@ command.c
config.c
connection.c
curse.c
database.c
equipment.c
faction.c
group.c

View File

@ -1,46 +0,0 @@
#include <platform.h>
#include "database.h"
#include <platform.h>
#include <kernel/config.h>
#include <kernel/faction.h>
#include <kernel/database.h>
#include <kernel/orderdb.h>
#include <util/log.h>
#include "db/driver.h"
order_data *dblib_load_order(int id)
{
if (id > 0) {
return db_driver_order_load(id);
}
return NULL;
}
int dblib_save_order(order_data *od)
{
if (od->_str) {
return db_driver_order_save(od);
}
return 0;
}
int dblib_save_faction(const faction *f, int turn) {
return db_driver_faction_save(f->uid, f->no, turn, f->email, f->_password);
}
void dblib_open(void)
{
const char *dbname;
dbname = config_get("game.dbswap");
db_driver_open(DB_SWAP, dbname);
}
void dblib_close(void)
{
db_driver_close(DB_SWAP);
}

View File

@ -1,23 +0,0 @@
#ifndef H_DATABASE
#define H_DATABASE
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
struct order_data;
struct faction;
void dblib_open(void);
void dblib_close(void);
struct order_data *dblib_load_order(int id);
int dblib_save_order(struct order_data *od);
int dblib_save_faction(const struct faction *f, int turn);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -2,7 +2,7 @@
#include <kernel/config.h>
#include <kernel/faction.h>
#include "database.h"
#include "db/driver.h"
#include "orderdb.h"
#include <CuTest.h>
@ -39,9 +39,9 @@ static void test_update_faction(CuTest *tc) {
test_setup();
f = test_create_faction(NULL);
uid = dblib_save_faction(f, 0);
uid = db_driver_faction_save(f->uid, f->no, 0, f->email, f->_password);
f->uid = uid;
uid = dblib_save_faction(f, 0);
uid = db_driver_faction_save(f->uid, f->no, 0, f->email, f->_password);
CuAssertIntEquals(tc, f->uid, uid);
test_teardown();
}

View File

@ -4,36 +4,35 @@
#include <platform.h>
#include "driver.h"
#include <kernel/config.h>
#include <kernel/orderdb.h>
#include <assert.h>
#include <string.h>
static DB *g_dbp;
void db_driver_open(void)
void db_driver_open(database_t db, const char *dbname)
{
int ret;
u_int32_t flags = DB_CREATE;
const char * dbname;
if (db == DB_SWAP) {
int ret;
u_int32_t flags = DB_CREATE;
dbname = config_get("game.dbname");
ret = db_create(&g_dbp, NULL, 0);
assert(ret==0);
ret = db_create(&g_dbp, NULL, 0);
assert(ret == 0);
ret = g_dbp->open(g_dbp, NULL, dbname, NULL, DB_RECNO, flags, 0);
assert(ret==0);
ret = g_dbp->open(g_dbp, NULL, dbname, NULL, DB_RECNO, flags, 0);
assert(ret == 0);
}
}
void db_driver_close(void)
void db_driver_close(database_t db)
{
int ret;
ret = g_dbp->close(g_dbp, 0);
assert(ret==0);
if (db == DB_SWAP) {
int ret;
ret = g_dbp->close(g_dbp, 0);
assert(ret == 0);
}
}
int db_driver_order_save(struct order_data *od)
int db_driver_order_save(const char *str)
{
int ret;
DBT key, data;
@ -46,18 +45,13 @@ int db_driver_order_save(struct order_data *od)
key.size = key.ulen = sizeof(recno);
key.flags = DB_DBT_USERMEM;
data.data = (void *)od->_str;
data.size = data.ulen = strlen(od->_str) + 1;
data.size = data.ulen = strlen(str) + 1;
data.flags = DB_DBT_USERMEM;
ret = g_dbp->put(g_dbp, NULL, &key, &data, DB_APPEND);
assert(ret == 0);
return (int)recno;
}
int db_driver_faction_save(int id, int no, int turn, const char *email, const char *password)
{
return -1;
}
struct order_data *db_driver_order_load(int id)
{
int ret;
@ -79,3 +73,8 @@ struct order_data *db_driver_order_load(int id)
return od;
}
int db_driver_faction_save(int id, int no, int turn, const char *email, const char *password)
{
return -1;
}

View File

@ -1,7 +1,11 @@
#pragma once
#include <stddef.h>
struct order_data;
extern void odata_create(struct order_data **pdata, size_t len, const char *str);
typedef enum database_t {
DB_SWAP,
DB_GAME,
@ -9,6 +13,6 @@ 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(struct order_data *od);
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);

View File

@ -1,8 +1,6 @@
#include <platform.h>
#include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/orderdb.h>
#include <util/log.h>
@ -25,9 +23,9 @@ static sqlite3_stmt * g_stmt_insert_faction;
static int g_order_batchsize;
static int g_order_tx_size;
order_data *db_driver_order_load(int id)
struct order_data *db_driver_order_load(int id)
{
order_data * od = NULL;
struct order_data * od = NULL;
int err;
ERRNO_CHECK();
@ -58,12 +56,12 @@ order_data *db_driver_order_load(int id)
return NULL;
}
int db_driver_order_save(order_data *od)
int db_driver_order_save(const char *str)
{
int err;
sqlite3_int64 id;
assert(od && od->_str);
assert(str);
ERRNO_CHECK();
@ -76,7 +74,7 @@ int db_driver_order_save(order_data *od)
err = sqlite3_reset(g_stmt_insert_order);
assert(err == SQLITE_OK);
err = sqlite3_bind_text(g_stmt_insert_order, 1, od->_str, -1, SQLITE_STATIC);
err = sqlite3_bind_text(g_stmt_insert_order, 1, str, -1, SQLITE_STATIC);
assert(err == SQLITE_OK);
err = sqlite3_step(g_stmt_insert_order);
assert(err == SQLITE_DONE);

View File

@ -1,5 +1,8 @@
#include <platform.h>
#include "database.h"
#include "config.h"
#include "db/driver.h"
#include "orderdb.h"
#include <util/log.h>
@ -10,6 +13,19 @@
#include <stdlib.h>
#include <string.h>
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);
}
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
@ -42,10 +58,16 @@ void odata_addref(order_data *od)
order_data *odata_load(int id)
{
return dblib_load_order(id);
if (id > 0) {
return db_driver_order_load(id);
}
return NULL;
}
int odata_save(order_data *od)
{
return dblib_save_order(od);
if (od->_str) {
return db_driver_order_save(od->_str);
}
return 0;
}

View File

@ -19,6 +19,9 @@ extern "C" {
order_data *odata_load(int id);
int odata_save(order_data *od);
void orderdb_open(void);
void orderdb_close(void);
#ifdef __cplusplus
}
#endif

View File

@ -226,20 +226,13 @@ static const char * file_getbuf(void *data)
return getbuf(F, ENCODING_UTF8);
}
int readorders(const char *filename)
int readorders(FILE *F)
{
input in;
int result;
FILE *F = fopen(filename, "r");
if (!F) {
perror(filename);
return -1;
}
log_info("reading orders from %s", filename);
in.getbuf = file_getbuf;
in.data = F;
result = read_orders(&in);
fclose(F);
return result;
}

View File

@ -1,7 +1,7 @@
#ifndef H_ORDERFILE
#define H_ORDERFILE
#include <skill.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
@ -13,7 +13,7 @@ extern "C" {
} input;
int read_orders(struct input *in);
int readorders(const char *filename);
int readorders(FILE *F);
#ifdef __cplusplus
}

View File

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

3
tests/orders.txt Normal file
View File

@ -0,0 +1,3 @@
ERESSEA uhfr "Hodor"
EINHEIT 8bz3
BENENNEN EINHEIT 'Goldene Herde'