Added more resent lsqlite3 version and fixed compile

This commit is contained in:
Miodrag Milanovic 2015-01-10 14:53:39 +01:00
parent 001b907d02
commit 72d8f8fd36
4 changed files with 1480 additions and 1208 deletions

View File

@ -1,6 +1,6 @@
/************************************************************************ /************************************************************************
* lsqlite3 * * lsqlite3 *
* Copyright (C) 2002-2007 Tiago Dionizio, Doug Currie * * Copyright (C) 2002-2013 Tiago Dionizio, Doug Currie *
* All rights reserved. * * All rights reserved. *
* Author : Tiago Dionizio <tiago.dionizio@ist.utl.pt> * * Author : Tiago Dionizio <tiago.dionizio@ist.utl.pt> *
* Author : Doug Currie <doug.currie@alum.mit.edu> * * Author : Doug Currie <doug.currie@alum.mit.edu> *
@ -30,15 +30,32 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "lua.h" #define LUA_LIB
#include "lauxlib.h" #include "lua/lua.h"
#include "lua/lauxlib.h"
#include "sqlite3.h" #if LUA_VERSION_NUM > 501
//
// Lua 5.2
//
//#define lua_strlen lua_rawlen
// luaL_typerror always used with arg at ndx == NULL
#define luaL_typerror(L,ndx,str) luaL_error(L,"bad argument %d (%s expected, got nil)",ndx,str)
// luaL_register used once, so below expansion is OK for this case
//#define luaL_register(L,name,reg) lua_newtable(L);luaL_setfuncs(L,reg,0)
// luaL_openlib always used with name == NULL
#define luaL_openlib(L,name,reg,nup) luaL_setfuncs(L,reg,nup)
#endif
#include "sqlite3/sqlite3.h"
/* compile time features */ /* compile time features */
#if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK)
#define SQLITE_OMIT_PROGRESS_CALLBACK 0 #define SQLITE_OMIT_PROGRESS_CALLBACK 0
#endif #endif
#if !defined(LSQLITE_OMIT_UPDATE_HOOK)
#define LSQLITE_OMIT_UPDATE_HOOK 0
#endif
typedef struct sdb sdb; typedef struct sdb sdb;
typedef struct sdb_vm sdb_vm; typedef struct sdb_vm sdb_vm;
@ -76,6 +93,19 @@ struct sdb {
int trace_cb; /* trace callback */ int trace_cb; /* trace callback */
int trace_udata; int trace_udata;
#if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
int update_hook_cb; /* update_hook callback */
int update_hook_udata;
int commit_hook_cb; /* commit_hook callback */
int commit_hook_udata;
int rollback_hook_cb; /* rollback_hook callback */
int rollback_hook_udata;
#endif
}; };
static const char *sqlite_meta = ":sqlite3"; static const char *sqlite_meta = ":sqlite3";
@ -98,17 +128,17 @@ static void vm_push_column(lua_State *L, sqlite3_stmt *vm, int idx) {
if (n == i64) if (n == i64)
lua_pushnumber(L, n); lua_pushnumber(L, n);
else else
lua_pushlstring(L, sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx)); lua_pushlstring(L, (const char*)sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx));
} }
break; break;
case SQLITE_FLOAT: case SQLITE_FLOAT:
lua_pushnumber(L, sqlite3_column_double(vm, idx)); lua_pushnumber(L, sqlite3_column_double(vm, idx));
break; break;
case SQLITE_TEXT: case SQLITE_TEXT:
lua_pushlstring(L, sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx)); lua_pushlstring(L, (const char*)sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx));
break; break;
case SQLITE_BLOB: case SQLITE_BLOB:
lua_pushlstring(L, sqlite3_column_blob(vm, idx), sqlite3_column_bytes(vm, idx)); lua_pushlstring(L, (const char*)sqlite3_column_blob(vm, idx), sqlite3_column_bytes(vm, idx));
break; break;
case SQLITE_NULL: case SQLITE_NULL:
lua_pushnil(L); lua_pushnil(L);
@ -175,7 +205,8 @@ static int cleanupvm(lua_State *L, sdb_vm *svm) {
} }
static int stepvm(lua_State *L, sdb_vm *svm) { static int stepvm(lua_State *L, sdb_vm *svm) {
int result; // MAME: fixed Visual Studio compiler warning lsqlite3.c(235) : warning C4701: potentially uninitialized local variable 'result' used
int result = 0;
int loop_limit = 3; int loop_limit = 3;
while ( loop_limit-- ) { while ( loop_limit-- ) {
result = sqlite3_step(svm->vm); result = sqlite3_step(svm->vm);
@ -448,14 +479,13 @@ static int dbvm_bind_index(lua_State *L, sqlite3_stmt *vm, int index, int lindex
return sqlite3_bind_text(vm, index, lua_tostring(L, lindex), lua_strlen(L, lindex), SQLITE_TRANSIENT); return sqlite3_bind_text(vm, index, lua_tostring(L, lindex), lua_strlen(L, lindex), SQLITE_TRANSIENT);
case LUA_TNUMBER: case LUA_TNUMBER:
return sqlite3_bind_double(vm, index, lua_tonumber(L, lindex)); return sqlite3_bind_double(vm, index, lua_tonumber(L, lindex));
case LUA_TBOOLEAN:
return sqlite3_bind_int(vm, index, lua_toboolean(L, lindex) ? 1 : 0);
case LUA_TNONE: case LUA_TNONE:
case LUA_TNIL: case LUA_TNIL:
/* allow boolean values so i have a way to know which
** values were actually not set */
case LUA_TBOOLEAN:
return sqlite3_bind_null(vm, index); return sqlite3_bind_null(vm, index);
default: default:
luaL_error(L, "index (%d) - invalid data type for bind (%s)", index, lua_typename(L, lindex)); luaL_error(L, "index (%d) - invalid data type for bind (%s)", index, lua_typename(L, lua_type(L, lindex)));
return SQLITE_MISUSE; /*!*/ return SQLITE_MISUSE; /*!*/
} }
} }
@ -536,6 +566,7 @@ static int dbvm_bind_names(lua_State *L) {
lua_pushstring(L, ++name); lua_pushstring(L, ++name);
lua_gettable(L, 2); lua_gettable(L, 2);
result = dbvm_bind_index(L, vm, n, -1); result = dbvm_bind_index(L, vm, n, -1);
lua_pop(L, 1);
} }
else { else {
lua_pushnumber(L, n); lua_pushnumber(L, n);
@ -578,7 +609,16 @@ static sdb *newdb (lua_State *L) {
db->progress_cb = db->progress_cb =
db->progress_udata = db->progress_udata =
db->trace_cb = db->trace_cb =
db->trace_udata = LUA_NOREF; db->trace_udata =
#if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
db->update_hook_cb =
db->update_hook_udata =
db->commit_hook_cb =
db->commit_hook_udata =
db->rollback_hook_cb =
db->rollback_hook_udata =
#endif
LUA_NOREF;
luaL_getmetatable(L, sqlite_meta); luaL_getmetatable(L, sqlite_meta);
lua_setmetatable(L, -2); /* set metatable */ lua_setmetatable(L, -2); /* set metatable */
@ -605,7 +645,7 @@ static int cleanupdb(lua_State *L, sdb *db) {
top = lua_gettop(L); top = lua_gettop(L);
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, -2)) { while (lua_next(L, -2)) {
sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */ sdb_vm *svm = (sdb_vm *)lua_touserdata(L, -2); /* key: vm; val: sql text */
cleanupvm(L, svm); cleanupvm(L, svm);
lua_settop(L, top); lua_settop(L, top);
@ -626,6 +666,14 @@ static int cleanupdb(lua_State *L, sdb *db) {
luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata); luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb); luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata); luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
#if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_udata);
luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_udata);
luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_udata);
#endif
/* close database */ /* close database */
result = sqlite3_close(db->db); result = sqlite3_close(db->db);
@ -868,7 +916,7 @@ static int db_interrupt(lua_State *L) {
static void db_push_value(lua_State *L, sqlite3_value *value) { static void db_push_value(lua_State *L, sqlite3_value *value) {
switch (sqlite3_value_type(value)) { switch (sqlite3_value_type(value)) {
case SQLITE_TEXT: case SQLITE_TEXT:
lua_pushlstring(L, sqlite3_value_text(value), sqlite3_value_bytes(value)); lua_pushlstring(L, (const char*)sqlite3_value_text(value), sqlite3_value_bytes(value));
break; break;
case SQLITE_INTEGER: case SQLITE_INTEGER:
@ -878,7 +926,7 @@ static void db_push_value(lua_State *L, sqlite3_value *value) {
if (n == i64) if (n == i64)
lua_pushnumber(L, n); lua_pushnumber(L, n);
else else
lua_pushlstring(L, sqlite3_value_text(value), sqlite3_value_bytes(value)); lua_pushlstring(L, (const char*)sqlite3_value_text(value), sqlite3_value_bytes(value));
} }
break; break;
@ -887,7 +935,7 @@ static void db_push_value(lua_State *L, sqlite3_value *value) {
break; break;
case SQLITE_BLOB: case SQLITE_BLOB:
lua_pushlstring(L, sqlite3_value_blob(value), sqlite3_value_bytes(value)); lua_pushlstring(L, (const char*)sqlite3_value_blob(value), sqlite3_value_bytes(value));
break; break;
case SQLITE_NULL: case SQLITE_NULL:
@ -925,8 +973,7 @@ static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_v
ctx = lsqlite_make_context(L); /* push context - used to set results */ ctx = lsqlite_make_context(L); /* push context - used to set results */
} }
else { else {
// reuse context userdata value /* reuse context userdata value */
void *p = sqlite3_aggregate_context(context, 1); void *p = sqlite3_aggregate_context(context, 1);
/* i think it is OK to use assume that using a light user data /* i think it is OK to use assume that using a light user data
** as an entry on LUA REGISTRY table will be unique */ ** as an entry on LUA REGISTRY table will be unique */
@ -949,7 +996,7 @@ static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_v
db_push_value(L, argv[n]); db_push_value(L, argv[n]);
} }
// set context /* set context */
ctx->ctx = context; ctx->ctx = context;
if (lua_pcall(L, argc + 1, 0, 0)) { if (lua_pcall(L, argc + 1, 0, 0)) {
@ -958,7 +1005,7 @@ static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_v
sqlite3_result_error(context, errmsg, size); sqlite3_result_error(context, errmsg, size);
} }
// invalidate context /* invalidate context */
ctx->ctx = NULL; ctx->ctx = NULL;
if (!func->aggregate) { if (!func->aggregate) {
@ -992,14 +1039,14 @@ static void db_sql_finalize_function(sqlite3_context *context) {
else else
ctx = lsqlite_getcontext(L, -1); ctx = lsqlite_getcontext(L, -1);
// set context /* set context */
ctx->ctx = context; ctx->ctx = context;
if (lua_pcall(L, 1, 0, 0)) { if (lua_pcall(L, 1, 0, 0)) {
sqlite3_result_error(context, lua_tostring(L, -1), -1); sqlite3_result_error(context, lua_tostring(L, -1), -1);
} }
// invalidate context /* invalidate context */
ctx->ctx = NULL; ctx->ctx = NULL;
/* cleanup context */ /* cleanup context */
@ -1106,9 +1153,9 @@ static int collwrapper(scc *co,int l1,const void *p1,
int res=0; int res=0;
lua_State *L=co->L; lua_State *L=co->L;
lua_rawgeti(L,LUA_REGISTRYINDEX,co->ref); lua_rawgeti(L,LUA_REGISTRYINDEX,co->ref);
lua_pushlstring(L,p1,l1); lua_pushlstring(L,(const char*)p1,l1);
lua_pushlstring(L,p2,l2); lua_pushlstring(L,(const char*)p2,l2);
if (lua_pcall(L,2,1,0)==0) res=(int)lua_tonumber(L,-1); if (lua_pcall(L,2,1,0)==0) res=lua_tonumber(L,-1);
lua_pop(L,1); lua_pop(L,1);
return res; return res;
} }
@ -1130,8 +1177,8 @@ static int db_create_collation(lua_State *L) {
else if (!lua_isnil(L,3)) else if (!lua_isnil(L,3))
luaL_error(L,"create_collation: function or nil expected"); luaL_error(L,"create_collation: function or nil expected");
if (collfunc != NULL) { if (collfunc != NULL) {
co=(scc *)malloc(sizeof(scc)); // userdata is a no-no as it co=(scc *)malloc(sizeof(scc)); /* userdata is a no-no as it
// will be garbage-collected will be garbage-collected */
if (co) { if (co) {
co->L=L; co->L=L;
/* lua_settop(L,3) above means we don't need: lua_pushvalue(L,3); */ /* lua_settop(L,3) above means we don't need: lua_pushvalue(L,3); */
@ -1180,7 +1227,7 @@ static int db_trace(lua_State *L) {
db->trace_cb = db->trace_cb =
db->trace_udata = LUA_NOREF; db->trace_udata = LUA_NOREF;
/* clear busy handler */ /* clear trace handler */
sqlite3_trace(db->db, NULL, NULL); sqlite3_trace(db->db, NULL, NULL);
} }
else { else {
@ -1195,13 +1242,195 @@ static int db_trace(lua_State *L) {
db->trace_udata = luaL_ref(L, LUA_REGISTRYINDEX); db->trace_udata = luaL_ref(L, LUA_REGISTRYINDEX);
db->trace_cb = luaL_ref(L, LUA_REGISTRYINDEX); db->trace_cb = luaL_ref(L, LUA_REGISTRYINDEX);
/* set busy handler */ /* set trace handler */
sqlite3_trace(db->db, db_trace_callback, db); sqlite3_trace(db->db, db_trace_callback, db);
} }
return 0; return 0;
} }
#if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
/*
** update_hook callback:
** Params: database, callback function, userdata
**
** callback function:
** Params: userdata, {one of SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE},
** database name, table name (containing the affected row), rowid of the row
*/
static void db_update_hook_callback(void *user, int op, char const *dbname, char const *tblname, sqlite3_int64 rowid) {
sdb *db = (sdb*)user;
lua_State *L = db->L;
int top = lua_gettop(L);
lua_Number n;
/* setup lua callback call */
lua_rawgeti(L, LUA_REGISTRYINDEX, db->update_hook_cb); /* get callback */
lua_rawgeti(L, LUA_REGISTRYINDEX, db->update_hook_udata); /* get callback user data */
lua_pushnumber(L, (lua_Number )op);
lua_pushstring(L, dbname); /* update_hook database name */
lua_pushstring(L, tblname); /* update_hook database name */
n = (lua_Number)rowid;
if (n == rowid)
lua_pushnumber(L, n);
else
lua_pushfstring(L, "%ll", rowid);
/* call lua function */
lua_pcall(L, 5, 0, 0);
/* ignore any error generated by this function */
lua_settop(L, top);
}
static int db_update_hook(lua_State *L) {
sdb *db = lsqlite_checkdb(L, 1);
if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_udata);
db->update_hook_cb =
db->update_hook_udata = LUA_NOREF;
/* clear update_hook handler */
sqlite3_update_hook(db->db, NULL, NULL);
}
else {
luaL_checktype(L, 2, LUA_TFUNCTION);
/* make sure we have an userdata field (even if nil) */
lua_settop(L, 3);
luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_udata);
db->update_hook_udata = luaL_ref(L, LUA_REGISTRYINDEX);
db->update_hook_cb = luaL_ref(L, LUA_REGISTRYINDEX);
/* set update_hook handler */
sqlite3_update_hook(db->db, db_update_hook_callback, db);
}
return 0;
}
/*
** commit_hook callback:
** Params: database, callback function, userdata
**
** callback function:
** Params: userdata
** Returned value: Return false or nil to continue the COMMIT operation normally.
** return true (non false, non nil), then the COMMIT is converted into a ROLLBACK.
*/
static int db_commit_hook_callback(void *user) {
sdb *db = (sdb*)user;
lua_State *L = db->L;
int top = lua_gettop(L);
int rollback = 0;
/* setup lua callback call */
lua_rawgeti(L, LUA_REGISTRYINDEX, db->commit_hook_cb); /* get callback */
lua_rawgeti(L, LUA_REGISTRYINDEX, db->commit_hook_udata); /* get callback user data */
/* call lua function */
if (!lua_pcall(L, 1, 1, 0))
rollback = lua_toboolean(L, -1); /* use result if there was no error */
lua_settop(L, top);
return rollback;
}
static int db_commit_hook(lua_State *L) {
sdb *db = lsqlite_checkdb(L, 1);
if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_udata);
db->commit_hook_cb =
db->commit_hook_udata = LUA_NOREF;
/* clear commit_hook handler */
sqlite3_commit_hook(db->db, NULL, NULL);
}
else {
luaL_checktype(L, 2, LUA_TFUNCTION);
/* make sure we have an userdata field (even if nil) */
lua_settop(L, 3);
luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_udata);
db->commit_hook_udata = luaL_ref(L, LUA_REGISTRYINDEX);
db->commit_hook_cb = luaL_ref(L, LUA_REGISTRYINDEX);
/* set commit_hook handler */
sqlite3_commit_hook(db->db, db_commit_hook_callback, db);
}
return 0;
}
/*
** rollback hook callback:
** Params: database, callback function, userdata
**
** callback function:
** Params: userdata
*/
static void db_rollback_hook_callback(void *user) {
sdb *db = (sdb*)user;
lua_State *L = db->L;
int top = lua_gettop(L);
/* setup lua callback call */
lua_rawgeti(L, LUA_REGISTRYINDEX, db->rollback_hook_cb); /* get callback */
lua_rawgeti(L, LUA_REGISTRYINDEX, db->rollback_hook_udata); /* get callback user data */
/* call lua function */
lua_pcall(L, 1, 0, 0);
/* ignore any error generated by this function */
lua_settop(L, top);
}
static int db_rollback_hook(lua_State *L) {
sdb *db = lsqlite_checkdb(L, 1);
if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_udata);
db->rollback_hook_cb =
db->rollback_hook_udata = LUA_NOREF;
/* clear rollback_hook handler */
sqlite3_rollback_hook(db->db, NULL, NULL);
}
else {
luaL_checktype(L, 2, LUA_TFUNCTION);
/* make sure we have an userdata field (even if nil) */
lua_settop(L, 3);
luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_cb);
luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_udata);
db->rollback_hook_udata = luaL_ref(L, LUA_REGISTRYINDEX);
db->rollback_hook_cb = luaL_ref(L, LUA_REGISTRYINDEX);
/* set rollback_hook handler */
sqlite3_rollback_hook(db->db, db_rollback_hook_callback, db);
}
return 0;
}
#endif /* #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK */
#if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
@ -1355,8 +1584,7 @@ static int db_busy_timeout(lua_State *L) {
*/ */
static int db_exec_callback(void* user, int columns, char **data, char **names) { static int db_exec_callback(void* user, int columns, char **data, char **names) {
int result = SQLITE_ABORT; /* abort by default */ int result = SQLITE_ABORT; /* abort by default */
sdb *db = (sdb*)user; lua_State *L = (lua_State*)user;
lua_State *L = db->L;
int n; int n;
int top = lua_gettop(L); int top = lua_gettop(L);
@ -1412,7 +1640,7 @@ static int db_exec(lua_State *L) {
lua_pushnil(L); /* column names not known at this point */ lua_pushnil(L); /* column names not known at this point */
lua_newtable(L); /* column values table */ lua_newtable(L); /* column values table */
result = sqlite3_exec(db->db, sql, db_exec_callback, db, NULL); result = sqlite3_exec(db->db, sql, db_exec_callback, L, NULL);
} }
else { else {
/* no callbacks */ /* no callbacks */
@ -1517,7 +1745,7 @@ static int db_next_named_row(lua_State *L) {
} }
static int dbvm_do_rows(lua_State *L, int(*f)(lua_State *)) { static int dbvm_do_rows(lua_State *L, int(*f)(lua_State *)) {
//sdb_vm *svm = /* sdb_vm *svm = */
lsqlite_checkvm(L, 1); lsqlite_checkvm(L, 1);
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushcfunction(L, f); lua_pushcfunction(L, f);
@ -1599,7 +1827,7 @@ static int db_close_vm(lua_State *L) {
/* close all used handles */ /* close all used handles */
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, -2)) { while (lua_next(L, -2)) {
sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */ sdb_vm *svm = (sdb_vm *)lua_touserdata(L, -2); /* key: vm; val: sql text */
if ((!temp || svm->temp) && svm->vm) if ((!temp || svm->temp) && svm->vm)
{ {
@ -1722,13 +1950,47 @@ static const struct {
SC(INTEGER) SC(FLOAT) SC(TEXT) SC(BLOB) SC(INTEGER) SC(FLOAT) SC(TEXT) SC(BLOB)
SC(NULL) SC(NULL)
/* Authorizer Action Codes */
SC(CREATE_INDEX )
SC(CREATE_TABLE )
SC(CREATE_TEMP_INDEX )
SC(CREATE_TEMP_TABLE )
SC(CREATE_TEMP_TRIGGER)
SC(CREATE_TEMP_VIEW )
SC(CREATE_TRIGGER )
SC(CREATE_VIEW )
SC(DELETE )
SC(DROP_INDEX )
SC(DROP_TABLE )
SC(DROP_TEMP_INDEX )
SC(DROP_TEMP_TABLE )
SC(DROP_TEMP_TRIGGER )
SC(DROP_TEMP_VIEW )
SC(DROP_TRIGGER )
SC(DROP_VIEW )
SC(INSERT )
SC(PRAGMA )
SC(READ )
SC(SELECT )
SC(TRANSACTION )
SC(UPDATE )
SC(ATTACH )
SC(DETACH )
SC(ALTER_TABLE )
SC(REINDEX )
SC(ANALYZE )
SC(CREATE_VTABLE )
SC(DROP_VTABLE )
SC(FUNCTION )
SC(SAVEPOINT )
/* terminator */ /* terminator */
{ NULL, 0 } { NULL, 0 }
}; };
/* ======================================================= */ /* ======================================================= */
static const luaL_reg dblib[] = { static const luaL_Reg dblib[] = {
{"isopen", db_isopen }, {"isopen", db_isopen },
{"last_insert_rowid", db_last_insert_rowid }, {"last_insert_rowid", db_last_insert_rowid },
{"changes", db_changes }, {"changes", db_changes },
@ -1747,6 +2009,11 @@ static const luaL_reg dblib[] = {
{"progress_handler", db_progress_handler }, {"progress_handler", db_progress_handler },
{"busy_timeout", db_busy_timeout }, {"busy_timeout", db_busy_timeout },
{"busy_handler", db_busy_handler }, {"busy_handler", db_busy_handler },
#if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
{"update_hook", db_update_hook },
{"commit_hook", db_commit_hook },
{"rollback_hook", db_rollback_hook },
#endif
{"prepare", db_prepare }, {"prepare", db_prepare },
{"rows", db_rows }, {"rows", db_rows },
@ -1764,7 +2031,7 @@ static const luaL_reg dblib[] = {
{NULL, NULL} {NULL, NULL}
}; };
static const luaL_reg vmlib[] = { static const luaL_Reg vmlib[] = {
{"isopen", dbvm_isopen }, {"isopen", dbvm_isopen },
{"step", dbvm_step }, {"step", dbvm_step },
@ -1810,7 +2077,7 @@ static const luaL_reg vmlib[] = {
{ NULL, NULL } { NULL, NULL }
}; };
static const luaL_reg ctxlib[] = { static const luaL_Reg ctxlib[] = {
{"user_data", lcontext_user_data }, {"user_data", lcontext_user_data },
{"get_aggregate_data", lcontext_get_aggregate_context }, {"get_aggregate_data", lcontext_get_aggregate_context },
@ -1830,7 +2097,7 @@ static const luaL_reg ctxlib[] = {
{NULL, NULL} {NULL, NULL}
}; };
static const luaL_reg sqlitelib[] = { static const luaL_Reg sqlitelib[] = {
{"version", lsqlite_version }, {"version", lsqlite_version },
{"complete", lsqlite_complete }, {"complete", lsqlite_complete },
#ifndef WIN32 #ifndef WIN32
@ -1843,7 +2110,7 @@ static const luaL_reg sqlitelib[] = {
{NULL, NULL} {NULL, NULL}
}; };
static void create_meta(lua_State *L, const char *name, const luaL_reg *lib) { static void create_meta(lua_State *L, const char *name, const luaL_Reg *lib) {
luaL_newmetatable(L, name); luaL_newmetatable(L, name);
lua_pushstring(L, "__index"); lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* push metatable */ lua_pushvalue(L, -2); /* push metatable */
@ -1856,6 +2123,12 @@ static void create_meta(lua_State *L, const char *name, const luaL_reg *lib) {
lua_pop(L, 1); lua_pop(L, 1);
} }
static int luaopen_sqlite3 ( lua_State * L )
{
luaL_newlib(L, sqlitelib);
return 1;
}
LUALIB_API int luaopen_lsqlite3(lua_State *L) { LUALIB_API int luaopen_lsqlite3(lua_State *L) {
create_meta(L, sqlite_meta, dblib); create_meta(L, sqlite_meta, dblib);
create_meta(L, sqlite_vm_meta, vmlib); create_meta(L, sqlite_vm_meta, vmlib);
@ -1863,10 +2136,9 @@ LUALIB_API int luaopen_lsqlite3(lua_State *L) {
luaL_getmetatable(L, sqlite_ctx_meta); luaL_getmetatable(L, sqlite_ctx_meta);
sqlite_ctx_meta_ref = luaL_ref(L, LUA_REGISTRYINDEX); sqlite_ctx_meta_ref = luaL_ref(L, LUA_REGISTRYINDEX);
/* register (local) sqlite metatable */ /* register (local) sqlite metatable */
luaL_register(L, "sqlite3", sqlitelib); //luaL_register(L, "sqlite3", sqlitelib);
luaL_requiref(L, "sqlite3", luaopen_sqlite3, 1);
{ {
int i = 0; int i = 0;
/* add constants to global table */ /* add constants to global table */
@ -1877,7 +2149,6 @@ LUALIB_API int luaopen_lsqlite3(lua_State *L) {
++i; ++i;
} }
} }
/* set sqlite's metatable to itself - set as readonly (__newindex) */ /* set sqlite's metatable to itself - set as readonly (__newindex) */
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);

View File

@ -10,7 +10,6 @@
#include <limits> #include <limits>
#include "lua/lua.hpp" #include "lua/lua.hpp"
#include "lua/lib/lualibs.h"
#include "luabridge/Source/LuaBridge/LuaBridge.h" #include "luabridge/Source/LuaBridge/LuaBridge.h"
#include <signal.h> #include <signal.h>
#include "emu.h" #include "emu.h"
@ -45,6 +44,9 @@ static lua_State *globalL = NULL;
const char *const lua_engine::tname_ioport = "lua.ioport"; const char *const lua_engine::tname_ioport = "lua.ioport";
lua_engine* lua_engine::luaThis = NULL; lua_engine* lua_engine::luaThis = NULL;
extern "C" {
int luaopen_lsqlite3(lua_State *L);
}
static void lstop(lua_State *L, lua_Debug *ar) static void lstop(lua_State *L, lua_Debug *ar)
{ {

View File

@ -25,7 +25,6 @@
#endif #endif
#include "lua/lua.hpp" #include "lua/lua.hpp"
#include "lua/lib/lualibs.h"
#include "luabridge/Source/LuaBridge/LuaBridge.h" #include "luabridge/Source/LuaBridge/LuaBridge.h"
struct lua_State; struct lua_State;

View File

@ -24,7 +24,7 @@ OBJDIRS += \
$(LIBOBJ)/lib7z \ $(LIBOBJ)/lib7z \
$(LIBOBJ)/portmidi \ $(LIBOBJ)/portmidi \
$(LIBOBJ)/lua \ $(LIBOBJ)/lua \
$(LIBOBJ)/lua/lib \ $(LIBOBJ)/lua/lsqlite3 \
$(LIBOBJ)/mongoose \ $(LIBOBJ)/mongoose \
$(LIBOBJ)/jsoncpp \ $(LIBOBJ)/jsoncpp \
$(LIBOBJ)/sqlite3 \ $(LIBOBJ)/sqlite3 \