Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * LuaSQLite.cpp
- *
- * Created on: 27/10/2009
- * Author: bitetti
- */
- #include "LuaSQLite.h"
- #include <iostream>
- //inicializa elementos
- vector<LuaSQLite*> LuaSQLite::elements;
- LuaSQLite::LuaSQLite() {
- }
- LuaSQLite::~LuaSQLite() {
- }
- void LuaSQLite::dispose() {
- LuaSQLite* l;
- for ( unsigned int i = 0; i<elements.size(); i++ )
- {
- l = elements[i];
- delete l;
- }
- elements.clear();
- }
- LuaSQLite* LuaSQLite::create(lua_State *L, const char* src ) {
- LuaSQLite* lsql = LuaSQLite::create(L);
- lsql->openDatabase(src);
- return lsql;
- }
- LuaSQLite* LuaSQLite::create(lua_State *L ) {
- //vector<LuaSQLite*>::iterator lsql_it;
- LuaSQLite* lsql = new LuaSQLite();
- elements.push_back( lsql );
- lsql->addLuaState(L);
- return lsql;
- }
- void LuaSQLite::addLuaState(lua_State* L ) {
- Lua = L;
- }
- void LuaSQLite::registraFuncoes(lua_State* L) {
- //lua_register(L, "open_database", LuaSQLite::open_database);
- //
- int metatable, methods;
- lua_pushliteral(L, LUASQLITE_CLASSNAME ); //Nome da classe em Lua
- methods = newtable(L); //Tabela de metodos
- metatable = newtable(L); //SQLite class
- lua_pushliteral(L, "__index"); //add index event to metatable
- lua_pushvalue(L, methods); //
- lua_settable(L, metatable); //metatable.__index = methods
- lua_pushliteral(L, "__metatable"); //esconde metatable
- lua_pushvalue(L, methods);
- lua_settable(L, metatable); //metatable.__metatable = methods
- luaL_openlib(L, 0, meta_methods, 0); //preenche metatable
- luaL_openlib(L, 0, class_methods, 1);
- lua_pushliteral(L,"sqlite_version");
- lua_pushstring(L, SQLITE_VERSION );
- lua_settable(L,-3);
- lua_settable(L, LUA_GLOBALSINDEX);
- }
- int LuaSQLite::run(lua_State* L, const char* db) {
- LuaSQLite* lsql = create(L, db);
- if (lsql) {
- sqlite3_stmt* stmt = lsql->prepare_statment("SELECT codigo.codigo as codigo FROM codigo WHERE codigo.nome = (SELECT startup FROM bootstrap)");
- if (stmt) {
- if (stmt) {
- int ret = sqlite3_step(stmt);
- if (ret == SQLITE_ROW) {
- const char* code = (const char*) sqlite3_column_text(stmt,0);
- //luaL_dostring(L, code);
- //prepara para pegar erro
- int err = luaL_loadstring(L, code) || lua_pcall(L, 0, LUA_MULTRET, 0);
- if (err) {
- std::cout<<"\033[35;1m"<<"![ \033[31m";
- string s = lua_tostring(L,1);
- std::cout<<s<<" \033[35m]\033[0m\n";
- lua_pop(L,1);
- }
- }
- //retira o stantment da pilha
- lsql->pop_statment();
- }
- }
- }
- return 0;
- }
- /**
- * Lua binding
- */
- LuaSQLite* LuaSQLite::checkClass (lua_State *L, int index) {
- lua_getmetatable(L, index);
- if( ! lua_equal(L, lua_upvalueindex(1), -1) )
- luaL_typerror(L, index, LUASQLITE_CLASSNAME); // die
- lua_pop(L, 1);
- return (LuaSQLite*)lua_touserdata(L, index);
- }
- void* LuaSQLite::getObject (lua_State *L, int index) {
- lua_getmetatable(L, index);
- if( ! lua_equal(L, lua_upvalueindex(1), -1) )
- luaL_typerror(L, index, LUASQLITE_CLASSNAME); // die
- lua_pop(L, 1);
- return (void*)lua_touserdata(L, index);
- }
- /**
- * Registros de Lua
- */
- const luaL_reg LuaSQLite::meta_methods[] = {
- /*{"__gc", image_destroy },*/
- { 0, 0 }
- };
- const luaL_reg LuaSQLite::class_methods[] = {
- {"new", open_database },
- {"open_database", open_database },
- {"close", close_database},
- {"getMainDatabase", getMainDatabase},
- /*{"sqlite_version", sqlite_version},*/
- {"query", query},
- {"fetch", fetchRow},
- {"reset", reset_stmt},
- {"sql_error",sql_error},
- { 0, 0 }
- };
- /**
- * Biblioteca de Funcoes lua
- */
- int LuaSQLite::open_database(lua_State* L) {
- int stack = lua_gettop(L);
- if (stack!=0) {
- if (lua_isstring(L,1) ) {
- const char* str = lua_tostring( L,1);
- LuaSQLite* lsql = LuaSQLite::create( L, str );
- lua_pushlightuserdata(L, (void*) lsql);
- lua_pushvalue(L, lua_upvalueindex(1));
- lua_setmetatable(L, -2);
- return 1;
- }
- }
- return 0;
- }
- int LuaSQLite::close_database(lua_State* L) {
- int stack = lua_gettop(L);
- if (stack!=0) {
- if (lua_isstring(L,1) ) {
- const char* str = lua_tostring( L,1);
- }
- }
- return 0;
- }
- int LuaSQLite::getMainDatabase(lua_State* L) {
- lua_gettop(L);
- if (elements.size()>0) {
- LuaSQLite* lsql = elements[0];
- lua_pushlightuserdata(L, (void*) lsql);
- lua_pushvalue(L, lua_upvalueindex(1));
- lua_setmetatable(L, -2);
- } else {
- lua_pushnil(L);
- }
- return 1;
- }
- int LuaSQLite::sql_error(lua_State* L ) {
- if (SQLiteBase::errMsg) {
- lua_pushstring(L, SQLiteBase::errMsg);
- } else {
- lua_pushnil(L);
- }
- return 1;
- }
- int LuaSQLite::query(lua_State* L) {
- int stack = lua_gettop(L);
- if (stack!=0) {
- LuaSQLite* lsql = checkClass(L,1);
- const char* sql = lua_tostring(L,2);
- sqlite3_stmt* stmt = lsql->prepare_statment(sql);
- lua_pushlightuserdata(L, (void*) stmt);
- lua_pushvalue(L, lua_upvalueindex(1));
- lua_setmetatable(L, -2);
- return 1;
- }
- return 0;
- }
- int LuaSQLite::fetchRow(lua_State* L) {
- int stack = lua_gettop(L);
- if (stack!=0) {
- sqlite3_stmt* stmt = (sqlite3_stmt*) getObject(L,1);
- if (stmt) {
- int ret = sqlite3_step(stmt);
- //retorna nil se nao houver row
- if (ret != SQLITE_ROW) {
- lua_pushnil(L);
- } else {
- int table = newtable(L);
- int colunas = sqlite3_column_count(stmt);
- const unsigned char* c;
- const char* cn;
- for( int i=0; i<colunas; i++) {
- cn = sqlite3_column_name(stmt,i);
- c = sqlite3_column_text(stmt,i);
- lua_pushstring(L,cn);
- lua_pushstring(L,(const char*) c);
- lua_settable(L,table);
- }
- }
- } else {
- lua_pushnil(L);
- }
- return 1;
- }
- return 0;
- }
- int LuaSQLite::reset_stmt(lua_State* L ) {
- int stack = lua_gettop(L);
- if (stack!=0) {
- sqlite3_stmt* stmt = (sqlite3_stmt*) getObject(L,1);
- if (stmt) {
- int res = sqlite3_reset(stmt);
- if (res==SQLITE_OK)
- lua_pushnumber(L,1);
- else
- lua_pushnumber(L,0);
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment