diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/client.pro | 2 | ||||
-rw-r--r-- | client/lua.cc | 2 | ||||
-rw-r--r-- | client/luadb.cc | 176 | ||||
-rw-r--r-- | client/luadb.h | 35 | ||||
-rw-r--r-- | client/luawidget.cc | 2 | ||||
-rw-r--r-- | client/luawidget.h | 2 |
6 files changed, 217 insertions, 2 deletions
diff --git a/client/client.pro b/client/client.pro index 3758693..877f19f 100644 --- a/client/client.pro +++ b/client/client.pro @@ -36,6 +36,7 @@ HEADERS += \ collapser.h \ debug.h \ lua.h \ + luadb.h \ luawidget.h \ macro.h \ macrowindow.h \ @@ -72,6 +73,7 @@ SOURCES += \ collapser.cc \ debug.cc \ lua.cc \ + luadb.cc \ luawidget.cc \ macro.cc \ macrowindow.cc \ diff --git a/client/lua.cc b/client/lua.cc index eae2e32..2753139 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -31,6 +31,7 @@ #include "widgets/widget.h" #include "luawidget.h" +#include "luadb.h" #include "debug.h" @@ -121,6 +122,7 @@ void LUA::clear() lua_register(L, "debug", debug); register_widget(L); + register_db(L); } QString LUA::runScriptS(QString script, Widget *widget, QString name) diff --git a/client/luadb.cc b/client/luadb.cc new file mode 100644 index 0000000..8666f98 --- /dev/null +++ b/client/luadb.cc @@ -0,0 +1,176 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * luadb.cc + * + * Sat Aug 28 13:40:58 CEST 2010 + * Copyright 2010 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "luadb.h" + +#include <QSqlDatabase> +#include <QSqlQuery> + +#include <lauxlib.h> + +#include "debug.h" + +#define luaL_checkbool(L, i) \ + (lua_isboolean(L,i) ? lua_toboolean(L,i) : luaL_checkint(L,i)) + +class DB { +public: + DB(QString driver, QString server, QString database, + QString user, QString password = "", int port = -1) { + db = QSqlDatabase::addDatabase(driver); + db.setHostName(server); + if(port != -1) db.setPort(port); + db.setDatabaseName(database); + db.setUserName(user); + if(password != "") db.setPassword(password); + db.setConnectOptions("connect_timeout=2000"); + } + + ~DB() { } + + void exec(const QString &querystr) { + query = db.exec(querystr); + } + + bool next() + { + return query.next(); + } + + QString value(int idx) { + return query.value(idx).toString(); + } + +private: + QSqlDatabase db; + QSqlQuery query; +}; + +typedef struct db_userdata { + DB *db; +} db_userdata; + +static int db_value(lua_State *L) +{ + db_userdata *dbu; + + dbu = (db_userdata *)luaL_checkudata(L, 1, "DB"); + luaL_argcheck(L, dbu, 1, "DB expected"); + + int idx = luaL_checkinteger(L, 2); + + lua_pushstring(L, dbu->db->value(idx).toStdString().c_str()); + + return 1; +} + +static int db_exec(lua_State *L) +{ + db_userdata *dbu; + + dbu = (db_userdata *)luaL_checkudata(L, 1, "DB"); + luaL_argcheck(L, dbu, 1, "DB expected"); + + const char *query = luaL_checkstring(L, 2); + + dbu->db->exec(query); + + return 0; +} + +static int db_next(lua_State *L) +{ + db_userdata *dbu; + + dbu = (db_userdata *)luaL_checkudata(L, 1, "DB"); + luaL_argcheck(L, dbu, 1, "DB expected"); + + bool more = dbu->db->next(); + + lua_pushboolean(L, more); + + return 1; +} + +static int db_new(lua_State *L) +{ + db_userdata *dbu; + dbu = (db_userdata *)lua_newuserdata(L, sizeof(db_userdata)); + + luaL_getmetatable(L, "DB"); + lua_setmetatable(L, -2); + + dbu->db = new DB("", "", "", ""); + + return 1; +} + +static int db_gc(lua_State *L) +{ + db_userdata *dbu; + + dbu = (db_userdata *)luaL_checkudata(L, 1, "DB"); + luaL_argcheck(L, dbu, 1, "DB expected"); + + delete dbu->db; + + return 0; +} + +static const struct luaL_Reg db_meths[] = { + {"__new", db_new}, + {"__gc" ,db_gc}, + {"value", db_value}, + {"next", db_next}, + {"exec", db_exec}, + {NULL, NULL} +}; + +void register_db(lua_State *L) +{ + luaL_newmetatable(L, "DB"); + lua_pushliteral(L, "__index"); + lua_pushvalue(L, -2); + lua_rawset(L, -3); + luaL_register(L, NULL, db_meths); +} + +/* + +Example: +-------- +db = DB:new(...) +db.exec('...') + +while db.next() +do + val0 = db.value(0) + val1 = db.value(1) + ... +end + + */ diff --git a/client/luadb.h b/client/luadb.h new file mode 100644 index 0000000..a87baf5 --- /dev/null +++ b/client/luadb.h @@ -0,0 +1,35 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * luadb.h + * + * Sat Aug 28 13:40:58 CEST 2010 + * Copyright 2010 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_LUADB_H__ +#define __PRACRO_LUADB_H__ + +#include <lua.hpp> + +void register_db(lua_State *L); + +#endif/*__PRACRO_LUADB_H__*/ diff --git a/client/luawidget.cc b/client/luawidget.cc index ba64257..bd000f0 100644 --- a/client/luawidget.cc +++ b/client/luawidget.cc @@ -37,7 +37,7 @@ ** Copied from lauxlib.c, but modified return NULL upon error instead of ** casting a lua error. **/ -LUALIB_API void *luaL_isudata (lua_State *L, int ud, const char *tname) { +static void *luaL_isudata (lua_State *L, int ud, const char *tname) { void *p = lua_touserdata(L, ud); if (p != NULL) { /* value is a userdata? */ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ diff --git a/client/luawidget.h b/client/luawidget.h index db615b5..b110598 100644 --- a/client/luawidget.h +++ b/client/luawidget.h @@ -29,7 +29,7 @@ #define __PRACRO_LUAWIDGET_H__ #include <lua.hpp> -#include <lauxlib.h> +//#include <lauxlib.h> #include "widgets/widget.h" |