summaryrefslogtreecommitdiff
path: root/client/luadb.cc
diff options
context:
space:
mode:
authordeva <deva>2010-08-31 09:34:19 +0000
committerdeva <deva>2010-08-31 09:34:19 +0000
commit43e82b32f2d812225e6d7ef76efdbf4873efc343 (patch)
treedf5ace9b3fb39b7e58145c185e63e2ce5a8d8466 /client/luadb.cc
parentf3847624ee5f99a37cf60be52067ad19bcdb4446 (diff)
luaComboBox and luaDB fixes and improvements.
Diffstat (limited to 'client/luadb.cc')
-rw-r--r--client/luadb.cc32
1 files changed, 24 insertions, 8 deletions
diff --git a/client/luadb.cc b/client/luadb.cc
index 8666f98..a9e77e6 100644
--- a/client/luadb.cc
+++ b/client/luadb.cc
@@ -48,9 +48,12 @@ public:
db.setUserName(user);
if(password != "") db.setPassword(password);
db.setConnectOptions("connect_timeout=2000");
+ db.open();
}
- ~DB() { }
+ ~DB() {
+ db.close();
+ }
void exec(const QString &querystr) {
query = db.exec(querystr);
@@ -118,13 +121,20 @@ static int db_next(lua_State *L)
static int db_new(lua_State *L)
{
+ const char *driver = luaL_checkstring(L, 1);
+ const char *host = luaL_checkstring(L, 2);
+ const char *database = luaL_checkstring(L, 3);
+ const char *user = luaL_checkstring(L, 4);
+ const char *password = luaL_checkstring(L, 5);
+ int port = luaL_checknumber(L, 6);
+
db_userdata *dbu;
dbu = (db_userdata *)lua_newuserdata(L, sizeof(db_userdata));
luaL_getmetatable(L, "DB");
lua_setmetatable(L, -2);
- dbu->db = new DB("", "", "", "");
+ dbu->db = new DB(driver, host, database, user, password, port);
return 1;
}
@@ -142,7 +152,7 @@ static int db_gc(lua_State *L)
}
static const struct luaL_Reg db_meths[] = {
- {"__new", db_new},
+ // {"__new", db_new},
{"__gc" ,db_gc},
{"value", db_value},
{"next", db_next},
@@ -150,6 +160,11 @@ static const struct luaL_Reg db_meths[] = {
{NULL, NULL}
};
+static const struct luaL_reg db_funcs[] = {
+ {"new", db_new},
+ {NULL, NULL}
+};
+
void register_db(lua_State *L)
{
luaL_newmetatable(L, "DB");
@@ -157,19 +172,20 @@ void register_db(lua_State *L)
lua_pushvalue(L, -2);
lua_rawset(L, -3);
luaL_register(L, NULL, db_meths);
+ luaL_openlib (L, "DB", db_funcs, 0);
}
/*
Example:
--------
-db = DB:new(...)
-db.exec('...')
+db = DB.new(...)
+db:exec('...')
-while db.next()
+while db:next()
do
- val0 = db.value(0)
- val1 = db.value(1)
+ val0 = db:value(0)
+ val1 = db:value(1)
...
end