bitetti

luasqlite.cpp

Jan 10th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 KB | None | 0 0
  1. /*
  2.  * LuaSQLite.cpp
  3.  *
  4.  *  Created on: 27/10/2009
  5.  *      Author: bitetti
  6.  */
  7.  
  8. #include "LuaSQLite.h"
  9. #include <iostream>
  10.  
  11. //inicializa elementos
  12. vector<LuaSQLite*> LuaSQLite::elements;
  13.  
  14. LuaSQLite::LuaSQLite() {
  15.  
  16. }
  17.  
  18. LuaSQLite::~LuaSQLite() {
  19.  
  20. }
  21.  
  22.  
  23. void LuaSQLite::dispose() {
  24.     LuaSQLite* l;
  25.     for ( unsigned int i = 0; i<elements.size(); i++ )
  26.     {
  27.         l = elements[i];
  28.         delete l;
  29.     }
  30.     elements.clear();
  31. }
  32.  
  33. LuaSQLite* LuaSQLite::create(lua_State *L, const char* src ) {
  34.     LuaSQLite* lsql = LuaSQLite::create(L);
  35.     lsql->openDatabase(src);
  36.     return lsql;
  37. }
  38.  
  39. LuaSQLite* LuaSQLite::create(lua_State *L ) {
  40.     //vector<LuaSQLite*>::iterator lsql_it;
  41.     LuaSQLite* lsql = new LuaSQLite();
  42.     elements.push_back( lsql );
  43.     lsql->addLuaState(L);
  44.  
  45.     return lsql;
  46. }
  47.  
  48.  
  49. void LuaSQLite::addLuaState(lua_State* L ) {
  50.     Lua = L;
  51. }
  52.  
  53. void LuaSQLite::registraFuncoes(lua_State* L) {
  54.     //lua_register(L, "open_database", LuaSQLite::open_database);
  55.  
  56.     //
  57.     int metatable, methods;
  58.  
  59.     lua_pushliteral(L, LUASQLITE_CLASSNAME );           //Nome da classe em Lua
  60.     methods   = newtable(L);                            //Tabela de metodos
  61.     metatable = newtable(L);                            //SQLite class
  62.     lua_pushliteral(L, "__index");                      //add index event to metatable
  63.     lua_pushvalue(L, methods);                          //
  64.     lua_settable(L, metatable);                         //metatable.__index = methods
  65.     lua_pushliteral(L, "__metatable");                  //esconde metatable
  66.     lua_pushvalue(L, methods);
  67.     lua_settable(L, metatable);                         //metatable.__metatable = methods
  68.     luaL_openlib(L, 0, meta_methods,  0);               //preenche metatable
  69.     luaL_openlib(L, 0, class_methods, 1);
  70.  
  71.     lua_pushliteral(L,"sqlite_version");
  72.     lua_pushstring(L, SQLITE_VERSION );
  73.     lua_settable(L,-3);
  74.  
  75.     lua_settable(L, LUA_GLOBALSINDEX);
  76. }
  77.  
  78.  
  79. int LuaSQLite::run(lua_State* L, const char* db) {
  80.     LuaSQLite* lsql = create(L, db);
  81.     if (lsql) {
  82.         sqlite3_stmt* stmt = lsql->prepare_statment("SELECT codigo.codigo as codigo FROM codigo WHERE codigo.nome = (SELECT startup FROM bootstrap)");
  83.         if (stmt) {
  84.             if (stmt) {
  85.                 int ret = sqlite3_step(stmt);
  86.  
  87.                 if (ret == SQLITE_ROW) {
  88.                     const char* code = (const char*) sqlite3_column_text(stmt,0);
  89.                     //luaL_dostring(L, code);
  90.                     //prepara para pegar erro
  91.                     int err = luaL_loadstring(L, code) || lua_pcall(L, 0, LUA_MULTRET, 0);
  92.                     if (err) {
  93.                         std::cout<<"\033[35;1m"<<"![ \033[31m";
  94.                         string s = lua_tostring(L,1);
  95.                         std::cout<<s<<" \033[35m]\033[0m\n";
  96.                         lua_pop(L,1);
  97.                     }
  98.                 }
  99.                 //retira o stantment da pilha
  100.                 lsql->pop_statment();
  101.             }
  102.         }
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. /**
  109.  * Lua binding
  110.  */
  111. LuaSQLite* LuaSQLite::checkClass (lua_State *L, int index) {
  112.     lua_getmetatable(L, index);
  113.     if( ! lua_equal(L, lua_upvalueindex(1), -1) )
  114.         luaL_typerror(L, index, LUASQLITE_CLASSNAME); // die
  115.     lua_pop(L, 1);
  116.     return (LuaSQLite*)lua_touserdata(L, index);
  117. }
  118.  
  119. void* LuaSQLite::getObject (lua_State *L, int index) {
  120.     lua_getmetatable(L, index);
  121.     if( ! lua_equal(L, lua_upvalueindex(1), -1) )
  122.         luaL_typerror(L, index, LUASQLITE_CLASSNAME); // die
  123.     lua_pop(L, 1);
  124.     return (void*)lua_touserdata(L, index);
  125. }
  126.  
  127. /**
  128.  * Registros de Lua
  129.  */
  130. const luaL_reg LuaSQLite::meta_methods[] = {
  131.     /*{"__gc", image_destroy },*/
  132.     { 0, 0 }
  133. };
  134.  
  135. const luaL_reg LuaSQLite::class_methods[] = {
  136.     {"new", open_database },
  137.     {"open_database", open_database },
  138.     {"close", close_database},
  139.     {"getMainDatabase", getMainDatabase},
  140.  
  141.     /*{"sqlite_version", sqlite_version},*/
  142.  
  143.     {"query", query},
  144.     {"fetch", fetchRow},
  145.     {"reset", reset_stmt},
  146.     {"sql_error",sql_error},
  147.  
  148.     { 0, 0 }
  149. };
  150.  
  151.  
  152. /**
  153.  * Biblioteca de Funcoes lua
  154.  */
  155.  
  156. int LuaSQLite::open_database(lua_State* L) {
  157.     int stack = lua_gettop(L);
  158.     if (stack!=0) {
  159.         if (lua_isstring(L,1) ) {
  160.             const char* str = lua_tostring( L,1);
  161.             LuaSQLite* lsql = LuaSQLite::create( L, str );
  162.  
  163.             lua_pushlightuserdata(L, (void*) lsql);
  164.             lua_pushvalue(L, lua_upvalueindex(1));
  165.             lua_setmetatable(L, -2);
  166.  
  167.  
  168.             return 1;
  169.         }
  170.     }
  171.     return 0;
  172. }
  173.  
  174.  
  175. int LuaSQLite::close_database(lua_State* L) {
  176.     int stack = lua_gettop(L);
  177.     if (stack!=0) {
  178.         if (lua_isstring(L,1) ) {
  179.             const char* str = lua_tostring( L,1);
  180.  
  181.         }
  182.     }
  183.     return 0;
  184. }
  185.  
  186. int LuaSQLite::getMainDatabase(lua_State* L) {
  187.     lua_gettop(L);
  188.     if (elements.size()>0) {
  189.         LuaSQLite* lsql = elements[0];
  190.         lua_pushlightuserdata(L, (void*) lsql);
  191.         lua_pushvalue(L, lua_upvalueindex(1));
  192.         lua_setmetatable(L, -2);
  193.     } else {
  194.         lua_pushnil(L);
  195.     }
  196.     return 1;
  197. }
  198.  
  199. int LuaSQLite::sql_error(lua_State* L ) {
  200.     if (SQLiteBase::errMsg) {
  201.         lua_pushstring(L, SQLiteBase::errMsg);
  202.     } else {
  203.         lua_pushnil(L);
  204.     }
  205.     return 1;
  206. }
  207.  
  208. int LuaSQLite::query(lua_State* L) {
  209.     int stack = lua_gettop(L);
  210.     if (stack!=0) {
  211.         LuaSQLite* lsql = checkClass(L,1);
  212.         const char* sql = lua_tostring(L,2);
  213.  
  214.         sqlite3_stmt* stmt = lsql->prepare_statment(sql);
  215.         lua_pushlightuserdata(L, (void*) stmt);
  216.         lua_pushvalue(L, lua_upvalueindex(1));
  217.         lua_setmetatable(L, -2);
  218.         return 1;
  219.     }
  220.     return 0;
  221. }
  222.  
  223. int LuaSQLite::fetchRow(lua_State* L) {
  224.     int stack = lua_gettop(L);
  225.         if (stack!=0) {
  226.             sqlite3_stmt* stmt = (sqlite3_stmt*) getObject(L,1);
  227.             if (stmt) {
  228.                 int ret = sqlite3_step(stmt);
  229.  
  230.                 //retorna nil se nao houver row
  231.                 if (ret != SQLITE_ROW) {
  232.                     lua_pushnil(L);
  233.                 } else {
  234.  
  235.                     int table = newtable(L);
  236.  
  237.                     int colunas = sqlite3_column_count(stmt);
  238.                     const unsigned char* c;
  239.                     const char* cn;
  240.                     for( int i=0; i<colunas; i++) {
  241.                         cn = sqlite3_column_name(stmt,i);
  242.                         c = sqlite3_column_text(stmt,i);
  243.                         lua_pushstring(L,cn);
  244.                         lua_pushstring(L,(const char*) c);
  245.                         lua_settable(L,table);
  246.                     }
  247.                 }
  248.             } else {
  249.                 lua_pushnil(L);
  250.             }
  251.             return 1;
  252.         }
  253.     return 0;
  254. }
  255.  
  256. int LuaSQLite::reset_stmt(lua_State* L ) {
  257.     int stack = lua_gettop(L);
  258.     if (stack!=0) {
  259.         sqlite3_stmt* stmt = (sqlite3_stmt*) getObject(L,1);
  260.         if (stmt) {
  261.             int res = sqlite3_reset(stmt);
  262.             if (res==SQLITE_OK)
  263.                 lua_pushnumber(L,1);
  264.             else
  265.                 lua_pushnumber(L,0);
  266.         }
  267.     }
  268.     return 1;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment