/* * Baltisot * Copyright (C) 1999-2007 Nicolas "Pixel" Noble * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* $Id: LuaSQL.cc,v 1.9 2007-05-30 11:57:10 pixel Exp $ */ #ifdef _WIN32 #include #endif #include #include "LuaSQL.h" LuaSQLConnection::LuaSQLConnection(SQLConnection * _c) : c(_c) { } enum SQLCONNECTION_methods_t { SQLCONNECTION_QUERY = 0, SQLCONNECTION_NUMROWS, SQLCONNECTION_NUMFIELDS, SQLCONNECTION_FETCHROW, SQLCONNECTION_NUMAFFECTEDROWS, SQLCONNECTION_INSERTID, SQLCONNECTION_ERRNO, SQLCONNECTION_ERROR, }; enum SQLCONNECTION_functions_t { SQLCONNECTION_NEWSQLCONNECTION = 0, }; struct lua_functypes_t SQLConnection_methods[] = { { SQLCONNECTION_QUERY, "Query", 1, 1, {LUA_STRING} }, { SQLCONNECTION_NUMROWS, "NumRows", 0, 0, { } }, { SQLCONNECTION_NUMFIELDS, "NumFields", 0, 0, { } }, { SQLCONNECTION_FETCHROW, "FetchRow", 0, 0, { } }, { SQLCONNECTION_NUMAFFECTEDROWS, "NumAffectedRows", 0, 0, { } }, { SQLCONNECTION_INSERTID, "InsertId", 0, 0, { } }, { SQLCONNECTION_ERRNO, "ErrNO", 0, 0, { } }, { SQLCONNECTION_ERROR, "Error", 0, 0, { } }, { -1, 0, 0, 0, 0 } }; struct lua_functypes_t SQLConnection_functions[] = { { SQLCONNECTION_NEWSQLCONNECTION, "SQLConnection" , 4, 7, {LUA_STRING, LUA_STRING, LUA_STRING, LUA_STRING, LUA_NUMBER, LUA_STRING, LUA_NUMBER } }, { -1, 0, 0, 0, 0 } }; class sLua_SQLConnection : public Base { public: DECLARE_METHOD(SQLConnection, SQLCONNECTION_QUERY); DECLARE_METHOD(SQLConnection, SQLCONNECTION_NUMROWS); DECLARE_METHOD(SQLConnection, SQLCONNECTION_NUMFIELDS); DECLARE_METHOD(SQLConnection, SQLCONNECTION_FETCHROW); DECLARE_METHOD(SQLConnection, SQLCONNECTION_NUMAFFECTEDROWS); DECLARE_METHOD(SQLConnection, SQLCONNECTION_INSERTID); DECLARE_METHOD(SQLConnection, SQLCONNECTION_ERRNO); DECLARE_METHOD(SQLConnection, SQLCONNECTION_ERROR); DECLARE_FUNCTION(SQLConnection, SQLCONNECTION_NEWSQLCONNECTION); private: static int SQLConnection_proceed(Lua * L, int n, SQLConnection * obj, int caller); static int SQLConnection_proceed_statics(Lua * L, int n, int caller); }; void LuaSQLConnection::pushmembers(Lua * L) { pushme(L, c, "SQLConnection"); PUSH_METHOD(SQLConnection, SQLCONNECTION_QUERY); PUSH_METHOD(SQLConnection, SQLCONNECTION_NUMROWS); PUSH_METHOD(SQLConnection, SQLCONNECTION_NUMFIELDS); PUSH_METHOD(SQLConnection, SQLCONNECTION_FETCHROW); PUSH_METHOD(SQLConnection, SQLCONNECTION_NUMAFFECTEDROWS); PUSH_METHOD(SQLConnection, SQLCONNECTION_INSERTID); PUSH_METHOD(SQLConnection, SQLCONNECTION_ERRNO); PUSH_METHOD(SQLConnection, SQLCONNECTION_ERROR); } void LuaSQLConnection::pushstatics(Lua * L) throw (GeneralException) { CHECK_METHODS(SQLConnection); CHECK_FUNCTIONS(SQLConnection); PUSH_FUNCTION(SQLConnection, SQLCONNECTION_NEWSQLCONNECTION); } int sLua_SQLConnection::SQLConnection_proceed(Lua * L, int n, SQLConnection * c, int caller) { String s; AssocArray r; AssocArrayIterator i; switch (caller) { case SQLCONNECTION_QUERY: s = L->tostring(2); L->push((lua_Number) c->query(s)); break; case SQLCONNECTION_NUMROWS: L->push((lua_Number) c->numrows()); break; case SQLCONNECTION_NUMFIELDS: L->push((lua_Number) c->numfields()); break; case SQLCONNECTION_FETCHROW: r = c->fetchrow(); L->newtable(); for (i = r.begin(); i != r.end(); i++) { L->push(i->first); L->push(i->second); L->settable(); } break; case SQLCONNECTION_NUMAFFECTEDROWS: L->push((lua_Number) c->numaffectedrows()); break; case SQLCONNECTION_INSERTID: L->push((lua_Number) c->insertid()); break; // case SQLCONNECTION_ERRNO: // L->push((lua_Number) c->errno()); // break; case SQLCONNECTION_ERROR: L->push(c->error()); break; } return 1; } int sLua_SQLConnection::SQLConnection_proceed_statics(Lua * L, int n, int caller) { String host, user, passwd, db, socket = ""; int port = 3306, cflags = 0; switch (caller) { case SQLCONNECTION_NEWSQLCONNECTION: host = L->tostring(1); user = L->tostring(2); passwd = L->tostring(3); db = L->tostring(4); if (n >= 5) port = L->tonumber(5); if (n >= 6) socket = L->tostring(6); if (n >= 7) cflags = L->tonumber(7); { LuaSQLConnection c(new SQLConnection(host, user, passwd, db, port, socket, cflags)); c.pushdestruct(L); } break; } return 1; }