Skip to content

Commit

Permalink
Include SQLite 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
oe-ksterling committed Feb 14, 2016
1 parent 5d0470c commit cf63e7c
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/encstring.h
Expand Up @@ -5,7 +5,7 @@
License: Modified BSD (see license.txt)
******************************************************************************/

// Generated: 1451099456
// Generated: 1455476902

#ifndef ENCSTRING_H
#define ENCSTRING_H
Expand All @@ -15,24 +15,24 @@
namespace EncString
{
size_t reveal(char *, size_t, const int *);
const unsigned long enckey = 544663839;
const unsigned long enckey = 946134830;

const int basicTitle[] = {
0x2076e952, 0x2076e976, 0x2076e97c, 0x2076e96d, 0x2076e970, 0x2076e952, 0x2076e97e, 0x2076e97c,
0x2076e96d, 0x2076e970, 0x2076e93f, 0x2076e969, 0x2076e93a, 0x2076e973, 0x2076e97b, 0x2076e931,
0x2076e93a, 0x2076e92f, 0x2076e92d, 0x2076e973, 0x2076e97b, 0x2076e931, 0x2076e93a, 0x2076e973,
0x2076e97b, 0};
0x3864df63, 0x3864df47, 0x3864df4d, 0x3864df5c, 0x3864df41, 0x3864df63, 0x3864df4f, 0x3864df4d,
0x3864df5c, 0x3864df41, 0x3864df0e, 0x3864df58, 0x3864df0b, 0x3864df42, 0x3864df4a, 0x3864df00,
0x3864df0b, 0x3864df1e, 0x3864df1c, 0x3864df42, 0x3864df4a, 0x3864df00, 0x3864df0b, 0x3864df42,
0x3864df4a, 0};
const int website[] = {
0x2076e94c, 0x2076e970, 0x2076e973, 0x2076e97e, 0x2076e96d, 0x2076e94c, 0x2076e96b, 0x2076e96d,
0x2076e976, 0x2076e974, 0x2076e97a, 0x2076e93f, 0x2076e94c, 0x2076e970, 0x2076e979, 0x2076e96b,
0x2076e968, 0x2076e97e, 0x2076e96d, 0x2076e97a, 0x2076e916, 0x2076e977, 0x2076e96b, 0x2076e96b,
0x2076e96f, 0x2076e925, 0x2076e930, 0x2076e930, 0x2076e968, 0x2076e968, 0x2076e968, 0x2076e931,
0x2076e96c, 0x2076e970, 0x2076e973, 0x2076e97e, 0x2076e96d, 0x2076e96c, 0x2076e96b, 0x2076e96d,
0x2076e976, 0x2076e974, 0x2076e97a, 0x2076e931, 0x2076e971, 0x2076e97a, 0x2076e96b, 0};
0x3864df7d, 0x3864df41, 0x3864df42, 0x3864df4f, 0x3864df5c, 0x3864df7d, 0x3864df5a, 0x3864df5c,
0x3864df47, 0x3864df45, 0x3864df4b, 0x3864df0e, 0x3864df7d, 0x3864df41, 0x3864df48, 0x3864df5a,
0x3864df59, 0x3864df4f, 0x3864df5c, 0x3864df4b, 0x3864df27, 0x3864df46, 0x3864df5a, 0x3864df5a,
0x3864df5e, 0x3864df14, 0x3864df01, 0x3864df01, 0x3864df59, 0x3864df59, 0x3864df59, 0x3864df00,
0x3864df5d, 0x3864df41, 0x3864df42, 0x3864df4f, 0x3864df5c, 0x3864df5d, 0x3864df5a, 0x3864df5c,
0x3864df47, 0x3864df45, 0x3864df4b, 0x3864df00, 0x3864df40, 0x3864df4b, 0x3864df5a, 0};
const int logVersionFmt[] = {
0x2076e952, 0x2076e976, 0x2076e97c, 0x2076e96d, 0x2076e970, 0x2076e952, 0x2076e97e, 0x2076e97c,
0x2076e96d, 0x2076e970, 0x2076e93f, 0x2076e969, 0x2076e97a, 0x2076e96d, 0x2076e96c, 0x2076e976,
0x2076e970, 0x2076e971, 0x2076e93f, 0x2076e93a, 0x2076e96c, 0x2076e93f, 0x2076e937, 0x2076e93a,
0x2076e96c, 0x2076e936, 0x2076e93f, 0x2076e93a, 0x2076e96c, 0x2076e915, 0};
0x3864df63, 0x3864df47, 0x3864df4d, 0x3864df5c, 0x3864df41, 0x3864df63, 0x3864df4f, 0x3864df4d,
0x3864df5c, 0x3864df41, 0x3864df0e, 0x3864df58, 0x3864df4b, 0x3864df5c, 0x3864df5d, 0x3864df47,
0x3864df41, 0x3864df40, 0x3864df0e, 0x3864df0b, 0x3864df5d, 0x3864df0e, 0x3864df06, 0x3864df0b,
0x3864df5d, 0x3864df07, 0x3864df0e, 0x3864df0b, 0x3864df5d, 0x3864df24, 0};
}
#endif
2 changes: 2 additions & 0 deletions src/luaengine.cpp
Expand Up @@ -25,6 +25,7 @@
#include "hash_lua.h"
#include "cli_lua.h"
#include "memorychunk_lua.h"
#include "sqlite_lua.h"

#ifdef NETWORKING_ENABLED
#include "network_lua.h"
Expand Down Expand Up @@ -192,6 +193,7 @@ int LuaEngine::init()
Process_lua::regmod,
Window_lua::regmod,
MemoryChunk_lua::regmod, // Is this needed?
Sqlite_lua::regmod,
#ifdef NETWORKING_ENABLED
Network_lua::regmod,
Socket_lua::regmod,
Expand Down
40 changes: 40 additions & 0 deletions src/luatypes.cpp
Expand Up @@ -28,6 +28,7 @@ extern "C"
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h>
#include <sqlite3.h>

namespace LuaType
{
Expand All @@ -36,10 +37,12 @@ namespace LuaType
const char *metatable_handle = "process.handle";
const char *metatable_windowDC = "window.windowDC";
const char *metatable_audioResource = "audio.audioResource";
const char *metatable_sqlitedb = "sqlite.dbhandle";
}

using MicroMacro::ProcHandle;
using MicroMacro::AudioResource;
using MicroMacro::SQLiteDb;

int registerLuaTypes(lua_State *L)
{
Expand Down Expand Up @@ -78,6 +81,19 @@ int registerLuaTypes(lua_State *L)
lua_settable(L, -3);
lua_pop(L, 1); // Pop metatable


// SQLite DB handles
luaL_newmetatable(L, LuaType::metatable_sqlitedb);
lua_pushstring(L, "__gc");
lua_pushcfunction(L, LuaType::sqlitedb_gc);
lua_settable(L, -3);

lua_pushstring(L, "__tostring");
lua_pushcfunction(L, LuaType::sqlitedb_tostring);
lua_settable(L, -3);
lua_pop(L, 1); // Pop metatable


return MicroMacro::ERR_OK;
}

Expand Down Expand Up @@ -173,3 +189,27 @@ int LuaType::audioResource_tostring(lua_State *L)
lua_pushstring(L, buffer);
return 1;
}

// Close the DB
int LuaType::sqlitedb_gc(lua_State *L)
{
checkType(L, LT_USERDATA, 1);
SQLiteDb *pDb = static_cast<SQLiteDb *>(lua_touserdata(L, 1));
if( pDb->opened )
sqlite3_close(pDb->db);
return 0;
}

int LuaType::sqlitedb_tostring(lua_State *L)
{
checkType(L, LT_USERDATA, 1);
SQLiteDb *pDb = static_cast<SQLiteDb *>(lua_touserdata(L, 1));
char buffer[128];

if( pDb->opened )
slprintf(buffer, sizeof(buffer)-1, "SQLite Database 0x%p", pDb);
else
slprintf(buffer, sizeof(buffer)-1, "Invalid SQLite Database");
lua_pushstring(L, buffer);
return 1;
}
4 changes: 4 additions & 0 deletions src/luatypes.h
Expand Up @@ -17,6 +17,7 @@
extern const char *metatable_handle;
//extern const char *metatable_windowDC;
extern const char *metatable_audioResource;
extern const char *metatable_sqlitedb;

int ncursesWindow_gc(lua_State *);
int ncursesWindow_tostring(lua_State *);
Expand All @@ -29,6 +30,9 @@

int audioResource_gc(lua_State *);
int audioResource_tostring(lua_State *);

int sqlitedb_gc(lua_State *);
int sqlitedb_tostring(lua_State *);
}

int registerLuaTypes(lua_State *);
Expand Down
172 changes: 172 additions & 0 deletions src/sqlite_lua.cpp
@@ -0,0 +1,172 @@
/******************************************************************************
Project: MicroMacro
Author: SolarStrike Software
URL: www.solarstrike.net
License: Modified BSD (see license.txt)
******************************************************************************/

#include "sqlite_lua.h"
#include "error.h"
#include "macro.h"
#include "luatypes.h"
#include "strl.h"

using MicroMacro::SQLResult;
using MicroMacro::SQLField;

extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

#include <sqlite3.h>

using MicroMacro::SQLiteDb;

int Sqlite_lua::regmod(lua_State *L)
{
static const luaL_Reg _funcs[] = {
{"open", Sqlite_lua::open},
{"close", Sqlite_lua::close},
{"execute", Sqlite_lua::execute},
{NULL, NULL}
};

luaL_newlib(L, _funcs);
lua_setglobal(L, SQLITE_MODULE_NAME);

return MicroMacro::ERR_OK;
}

// Used internally only - Don't register
int Sqlite_lua::callback(void *outvec, int argc, char **argv, char **colName)
{
std::vector<SQLResult> *results = (std::vector<SQLResult>*)(outvec);
SQLResult result;
for(unsigned int i = 0; i < argc; i++)
{
SQLField field;
field.name = colName[i];
field.value = (argv[i] ? argv[i] : "NULL");
result.fields.push_back(field);
}
results->push_back(result);

return 0;
}


/* sqlite.open(string filename)
Returns: boolean
Opens an SQLite DB. Returns a SQLite handle(class) on success.
Returns nil on failure.
*/
int Sqlite_lua::open(lua_State *L)
{
if( lua_gettop(L) != 1 )
wrongArgs(L);
checkType(L, LT_STRING, 1);

std::string filename = lua_tostring(L, 1);

// Create a new, empty sqlite3 DB struct on the Lua stack
SQLiteDb *pDb = static_cast<SQLiteDb *>(lua_newuserdata(L, sizeof(SQLiteDb)));
luaL_getmetatable(L, LuaType::metatable_sqlitedb);
lua_setmetatable(L, -2);
pDb->opened = false;

// Non-zero = error
int rc = sqlite3_open(filename.c_str(), &pDb->db);
if( rc )
{
std::string errmsg = sqlite3_errmsg(pDb->db);
char *fmt = "SQLite error occurred opening database: %s\n";
#ifdef DISPLAY_DEBUG_MESSAGES
fprintf(stderr, fmt, errmsg.c_str());
#endif

lua_pop(L, 1); // Pop our resource (pDb) off the stack.
pushLuaErrorEvent(L, fmt, errmsg.c_str());
return 0;
}

pDb->opened = true;
return 1;
}

/* sqlite.close(class sqlitedb)
Returns: nil
Closes an opened SQLite DB.
*/
int Sqlite_lua::close(lua_State *L)
{
if( lua_gettop(L) != 1 )
wrongArgs(L);
checkType(L, LT_USERDATA, 1);

SQLiteDb *pDb = static_cast<SQLiteDb *>(lua_touserdata(L, 1));
if( pDb->opened )
sqlite3_close(pDb->db);

pDb->opened = false;
return 0;
}

/* sqlite.execute(sqlitedb, string)
Returns: table of results (on success)
Returns: nil + error message (on fail)
Runs an SQL query on a SQLite DB.
*/
int Sqlite_lua::execute(lua_State *L)
{
if( lua_gettop(L) != 2 )
wrongArgs(L);
checkType(L, LT_USERDATA, 1);
checkType(L, LT_STRING, 2);

SQLiteDb *pDb = static_cast<SQLiteDb *>(lua_touserdata(L, 1));
if( !pDb->opened )
{
lua_pushboolean(L, false);
return 1;
}

// Prep a holding vector for results
std::vector<SQLResult> results;

char *errMsg;
std::string query = lua_tostring(L, 2);
int rc = sqlite3_exec(pDb->db, query.c_str(), callback, &results, &errMsg);

if( rc != SQLITE_OK )
{ // If an error occurs return false + errmsg
printf("SQLite err: %s\nn", errMsg);
lua_pushnil(L);
lua_pushstring(L, errMsg);
sqlite3_free(errMsg);

return 2;
}

// Dump results as a table of tables
lua_newtable(L);
for(unsigned int i = 0; i < results.size(); i++)
{
lua_pushinteger(L, i+1); // Push key
lua_newtable(L); // Push value
for(unsigned int j = 0; j < results.at(i).fields.size(); j++)
{
lua_pushstring(L, results.at(i).fields.at(j).name.c_str()); // Push key
lua_pushstring(L, results.at(i).fields.at(j).value.c_str()); // Push value
lua_settable(L, -3); // Compile into inner table ("row")
}
lua_settable(L, -3); // Compile into outer table
}

return 1;
}
27 changes: 27 additions & 0 deletions src/sqlite_lua.h
@@ -0,0 +1,27 @@
/******************************************************************************
Project: MicroMacro
Author: SolarStrike Software
URL: www.solarstrike.net
License: Modified BSD (see license.txt)
******************************************************************************/

#ifndef SQLITE_LUA_H
#define SQLITE_LUA_H

#define SQLITE_MODULE_NAME "sqlite"
typedef struct lua_State lua_State;

class Sqlite_lua
{
protected:
static int callback(void *, int, char **, char **); // Used internally

static int open(lua_State *);
static int close(lua_State *);
static int execute(lua_State *);

public:
static int regmod(lua_State *);
};

#endif
20 changes: 20 additions & 0 deletions src/types.h
Expand Up @@ -14,6 +14,8 @@
#include "wininclude.h"
#include "mutex.h"

struct sqlite3;

namespace MicroMacro
{

Expand Down Expand Up @@ -79,6 +81,24 @@
char *data;
};

/* Holds SQLite3 database info */
struct SQLiteDb
{
sqlite3 *db;
bool opened;
};

struct SQLField
{
std::string name;
std::string value;
};

struct SQLResult
{
std::vector<SQLField> fields;
};

template <class T>
T getChunkVariable(MemoryChunk *pChunk, unsigned int offset, int &err)
{
Expand Down

0 comments on commit cf63e7c

Please sign in to comment.