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

Creating db.h and #37

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/action_safe.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>

/* include main header file */
#include "mud.h"
Expand All @@ -31,12 +32,12 @@ void cmd_quit(D_MOBILE *dMob, char *arg)

dMob->socket->player = NULL;
free_mobile(dMob);
close_socket(dMob->socket, FALSE);
close_socket(dMob->socket, false);
}

void cmd_shutdown(D_MOBILE *dMob, char *arg)
{
shut_down = TRUE;
shut_down = true;
}

void cmd_commands(D_MOBILE *dMob, char *arg)
Expand Down Expand Up @@ -128,7 +129,7 @@ void cmd_compress(D_MOBILE *dMob, char *arg)
}
else /* disable compression */
{
if (!compressEnd(dMob->socket, dMob->socket->compressing, FALSE))
if (!compressEnd(dMob->socket, dMob->socket->compressing, false))
{
text_to_mobile(dMob, "Failed.\n\r");
return;
Expand Down Expand Up @@ -162,12 +163,12 @@ void cmd_copyover(D_MOBILE *dMob, char *arg)
AttachIterator(&Iter, dsock_list);
while ((dsock = (D_SOCKET *) NextInList(&Iter)) != NULL)
{
compressEnd(dsock, dsock->compressing, FALSE);
compressEnd(dsock, dsock->compressing, false);

if (dsock->state != STATE_PLAYING)
{
text_to_socket(dsock, "\n\rSorry, we are rebooting. Come back in a few minutes.\n\r");
close_socket(dsock, FALSE);
close_socket(dsock, false);
}
else
{
Expand Down Expand Up @@ -205,7 +206,7 @@ void cmd_linkdead(D_MOBILE *dMob, char *arg)
D_MOBILE *xMob;
ITERATOR Iter;
char buf[MAX_BUFFER];
bool found = FALSE;
bool found = false;

AttachIterator(&Iter, dmobile_list);
while ((xMob = (D_MOBILE *) NextInList(&Iter)) != NULL)
Expand All @@ -214,7 +215,7 @@ void cmd_linkdead(D_MOBILE *dMob, char *arg)
{
snprintf(buf, MAX_BUFFER, "%s is linkdead.\n\r", xMob->name);
text_to_mobile(dMob, buf);
found = TRUE;
found = true;
}
}
DetachIterator(&Iter);
Expand Down
124 changes: 112 additions & 12 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

/* 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 @@ -18,24 +46,34 @@ bool db_open()

db_close();

return FALSE;
return false;
}

return TRUE;
return true;
}

/*
* Close the global database.
*
*/

bool db_close()
{
if (sqlite3_close(db) != SQLITE_OK )
{
bug("Unable to close database: %s", sqlite3_errmsg(db));

return FALSE;
return false;
}

return TRUE;
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 @@ -48,23 +86,22 @@ bool db_execute(const char *sql, ...)
va_end(vars);

if ( stmt == NULL ) {
return FALSE;
return false;
}


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

return FALSE;
return false;
}

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

return FALSE;
return false;
}

return TRUE;
return true;
}

sqlite3_stmt *db_prepare(const char *sql, ...)
Expand Down Expand Up @@ -173,18 +210,81 @@ 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();
}

/* 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)") )
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();
}
}

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

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();
}

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
14 changes: 8 additions & 6 deletions src/event-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>


/* include main header file */
#include "mud.h"
Expand All @@ -28,7 +30,7 @@ bool enqueue_event(EVENT_DATA *event, int game_pulses)
if (event->ownertype == EVENT_UNOWNED)
{
bug("enqueue_event: event type %d with no owner.", event->type);
return FALSE;
return false;
}

/* An event must be enqueued into the future */
Expand All @@ -49,7 +51,7 @@ bool enqueue_event(EVENT_DATA *event, int game_pulses)
AttachToList(event, eventqueue[bucket]);

/* success */
return TRUE;
return true;
}

/* function :: dequeue_event()
Expand Down Expand Up @@ -181,7 +183,7 @@ void heartbeat()
*
* bool event_function ( EVENT_DATA *event );
*
* Any event returning TRUE is not dequeued, it is assumed
* Any event returning true is not dequeued, it is assumed
* that the event has dequeued itself.
*/
if (!((*event->fun)(event)))
Expand Down Expand Up @@ -221,7 +223,7 @@ void add_event_mobile(EVENT_DATA *event, D_MOBILE *dMob, int delay)
AttachToList(event, dMob->events);

/* attempt to enqueue the event */
if (enqueue_event(event, delay) == FALSE)
if (enqueue_event(event, delay) == false)
bug("add_event_mobile: event type %d failed to be enqueued.", event->type);
}

Expand Down Expand Up @@ -256,7 +258,7 @@ void add_event_socket(EVENT_DATA *event, D_SOCKET *dSock, int delay)
AttachToList(event, dSock->events);

/* attempt to enqueue the event */
if (enqueue_event(event, delay) == FALSE)
if (enqueue_event(event, delay) == false)
bug("add_event_socket: event type %d failed to be enqueued.", event->type);
}

Expand Down Expand Up @@ -290,7 +292,7 @@ void add_event_game(EVENT_DATA *event, int delay)
AttachToList(event, global_events);

/* attempt to enqueue the event */
if (enqueue_event(event, delay) == FALSE)
if (enqueue_event(event, delay) == false)
bug("add_event_game: event type %d failed to be enqueued.", event->type);
}

Expand Down
Loading