Skip to content
This repository has been archived by the owner on Feb 9, 2020. It is now read-only.

Commit

Permalink
Create db.h and db schema update logic (#38)
Browse files Browse the repository at this point in the history
* Create db.h and update c source files so they can find it.

* Update db_migrate logic so it can handle schema upgrades.
  • Loading branch information
rogersm authored and zachflower committed Apr 21, 2018
1 parent 93a9e93 commit 6687f72
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
112 changes: 108 additions & 4 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,37 @@

/* main header file */
#include "mud.h"
#include "db.h"

sqlite3 * db;

sqlite3_stmt *db_prepare_internal(const char *sql, va_list vars);
int get_db_schema();

/*
* Array of commands to be executed by db_migrate to apply changes to the database.
*
*/

typedef struct migration_commands {
int schema;
char *action;
} migration_commands;

migration_commands command[] = {
/* { DB_SCHEMA, Action to upgrade the database to the DB_SCHEMA } */
{ 0, "CREATE TABLE IF NOT EXISTS players (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, password TEXT NOT NULL, level INTEGER)" },
{ 1, "CREATE TABLE IF NOT EXISTS NEXT_ID (ID DECIMAL(9,0) NOT NULL)" },
{ 1, "INSERT INTO NEXT_ID (ID) VALUES(1)"},
{-1, NULL } // Sentinel to identify end of commands. DO NOT REMOVE.
};



/*
* Open the global database.
*
*/

bool db_open()
{
Expand All @@ -25,6 +52,11 @@ bool db_open()
return true;
}

/*
* Close the global database.
*
*/

bool db_close()
{
if (sqlite3_close(db) != SQLITE_OK )
Expand All @@ -37,6 +69,11 @@ bool db_close()
return true;
}

/*
* Execute a query to the database expecting no answer.
* Used for things like DROP, INSERT, CREATE TABLE...
*/

bool db_execute(const char *sql, ...)
{
va_list vars;
Expand All @@ -52,7 +89,6 @@ bool db_execute(const char *sql, ...)
return false;
}


if ( db_step(stmt) != SQLITE_DONE ) {
bug("Failed to step through statement: %s", sqlite3_errmsg(db));

Expand Down Expand Up @@ -174,18 +210,86 @@ sqlite3_stmt *db_prepare_internal(const char *sql, va_list vars)
void db_migrate()
{

int db_schema = get_db_schema();
int i = 0;

if ( !db_open() )
{
abort();
}

if (-1 == db_schema) {
if( !db_execute("CREATE TABLE IF NOT EXISTS DB_SCHEMA (db_version DECIMAL(6,0) NOT NULL)")
|| !db_execute("INSERT INTO DB_SCHEMA (db_version) VALUES(1)")) {
abort();
}
}

db_execute("BEGIN EXCLUSIVE TRANSACTION");
while(command[i].schema != -1) {
if (command[i].schema > db_schema) {
if ( !db_execute(command[i].action) ) {
db_execute("ROLLBACK TRANSACTION");
abort();
}
}
if( !db_execute("UPDATE DB_SCHEMA SET db_version = %i", command[i].schema) ) {
db_execute("ROLLBACK TRANSACTION");
abort();
}
i++;
}
db_execute("COMMIT TRANSACTION");


if(command[i-1].schema > db_schema ) {
log_string("db_schema updated to version %d", command[i-1].schema);
} else {
log_string("Current db_schema: %d", db_schema);
}

db_close();

return;
}


/*
* Retrieve the schema version.
*
* SELECT db_version FROM DB_SCHEMA
*
*/

int get_db_schema()
{
sqlite3_stmt *stmt;
int db_schema = -1;

if ( !db_open() )
{
abort();
}

/* players table */
if ( !db_execute("CREATE TABLE IF NOT EXISTS players (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, password TEXT NOT NULL, level INTEGER)") )
stmt = db_prepare("SELECT db_version FROM DB_SCHEMA");

if ( stmt == NULL )
{
db_close();
return db_schema;
}

if ( db_step(stmt) == SQLITE_ROW ) {
db_schema = sqlite3_column_int(stmt, 0);
}

if ( db_finalize(stmt) != SQLITE_OK ) {
bug("Failed to finalize statement: %s", sqlite3_errmsg(db));

abort();
}

db_close();

return;
return db_schema;
}
23 changes: 23 additions & 0 deletions src/db.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* file: db.h
*
* Headerfile for sqlite interface
*/

#ifndef _DB_HEADER
#define _DB_HEADER

#include <sqlite3.h>
#include <stdbool.h>

/*
* db.c
*/
bool db_open ( void );
bool db_close ( void );
bool db_execute ( const char *sql, ... );
sqlite3_stmt *db_prepare (const char *sql, ...);
int db_step (sqlite3_stmt *stmt);
int db_finalize (sqlite3_stmt *stmt);
void db_migrate ( void );

#endif
11 changes: 1 addition & 10 deletions src/mud.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "list.h"
#include "stack.h"
#include "crypt_blowfish-1.3-mini/ow-crypt.h"
#include "db.h"

/************************
* Standard definitions *
Expand Down Expand Up @@ -317,16 +318,6 @@ void save_player ( D_M *dMob );
D_M *load_player ( char *player );
D_M *load_profile ( char *player );

/*
* db.c
*/
bool db_open ( void );
bool db_close ( void );
bool db_execute ( const char *sql, ... );
sqlite3_stmt *db_prepare (const char *sql, ...);
int db_step (sqlite3_stmt *stmt);
int db_finalize (sqlite3_stmt *stmt);
void db_migrate ( void );

/*******************************
* End of prototype declartion *
Expand Down
1 change: 1 addition & 0 deletions src/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/* main header file */
#include "mud.h"
#include "db.h"

void save_player(D_MOBILE *dMob)
{
Expand Down

0 comments on commit 6687f72

Please sign in to comment.