diff options
author | Pixel <pixel@nobis-crew.org> | 2008-10-03 18:52:48 -0700 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2008-10-03 18:52:48 -0700 |
commit | 6733763ad7c13864ccdfd3c50c1547724e20e7aa (patch) | |
tree | 158347715acad0ea6059cc874e5291df77c6225b | |
parent | 14e0d57d6962b3be7faabb58764b3b07983e4733 (diff) |
More solid example, and a couple more functions
-rw-r--r-- | lib/dblib.lua | 85 |
1 files changed, 66 insertions, 19 deletions
diff --git a/lib/dblib.lua b/lib/dblib.lua index 65c9013..e9da88a 100644 --- a/lib/dblib.lua +++ b/lib/dblib.lua @@ -34,25 +34,32 @@ ddl = { db = luadb.opendb(host, login, password, database) t = db:opentable("foobar", ddl) +t:empty() -x = t:insert{ field1 = "test1" } -- returns 1 -x = t:insert{ field1 = "test2" } -- returns 2 -x = t:insert{ field1 = "test3" } -- returns 3 +x = t:insert{ field1 = "test1" } -- returns 1 +x = t:insert{ field1 = "test2" } -- returns 2 +x = t:insert{ field1 = "test3" } -- returns 3 x = t:insert{ field1 = "test4", field2 = 18 } -- returns 4 x = t:insert{ field1 = "test5", field2 = 20 } -- returns 5 r = t:restricter() -t:restrict(r:EQ("id", 3)) -t:update{ field1 = "3tset" } -t:restrict(r:EQ("field2", 42)) -t:update{ field2 = 28 } -t:restrict(r:EQ("id", 4)) -t:delete() +t:restrict(r:EQ("id", 3)) -- WHERE `id` = "3" +x = t:update{ field1 = "3tset" } -- returns 1 +t:restrict(r:EQ("field2", 42)) -- WHERE `field2` = "42" +x = t:update{ field2 = 28 } -- returns 3 +t:restrict(r:EQ("id", 4)) -- WHERE `id` = "4" +x = t:delete() -- returns 1 t:restrict(r:AND(r:LIKE("field1", "test%"), r:NEQ("field2", 20))) -for r in t:select() do print(r.field1) end - -- prints the strings "test1" and "test2" - + -- WHERE `field` LIKE "test%" AND `field2` != 20 +for r in t:gselect{ "field1" } do print(r.field1) end + -- prints the strings "test1" and "test2" +t:restrict(r:LIKE("field1", "test%")) +print(t:count()) -- prints "3" +t:restrict() -- WHERE 1 +t:select() +print(t:numrows()) -- prints "4" +print(t:numfields()) -- prints "3" ]]-- @@ -263,9 +270,15 @@ _luadb = { insert = _luadb.insert, delete = _luadb.delete, select = _luadb.select, + gselect = _luadb.gselect, + numrows = _luadb.numrows, + numfields = _luadb.numfields, + nextrow = _luadb.nextrow, + count = _luadb.count, update = _luadb.update, restrict = _luadb.restrict, restricter = _luadb.restricter, + empty = _luadb.empty, _ = { tablename = tablename, tname = tname, @@ -376,7 +389,8 @@ _luadb = { if t._.db:SafeQuery(stmt) ~= 0 then error("Error deleting row(s) inside table " .. t._.tablename .. ": " .. t._.db:Error()) - end + end + return t._.db._.conn:NumAffectedRows() end, update = function(t, data) @@ -405,19 +419,20 @@ _luadb = { if t._.db:SafeQuery(stmt) ~= 0 then error("Error updating row(s) inside table " .. t._.tablename .. ": " .. t._.db:Error()) - end + end + return t._.db._.conn:NumAffectedRows() end, - select = function(t, fields) + iselect = function(t, fields, bypass) local stmt = "SELECT " local first = true local k, v - if (fields == nil) then + if fields == nil then stmt = stmt .. "*" else for k, v in pairs(fields) do - if not t._.ddl[v] then + if not bypass and not t._.ddl[v] then error("Trying to select a field which doesn't exist: " .. k .. " - inside of table " .. t._.tablename) end if not first then @@ -425,7 +440,11 @@ _luadb = { else first = false end - stmt = stmt .. "`" .. v .. "`" + if bypass then + stmt = stmt .. v + else + stmt = stmt .. "`" .. v .. "`" + end end end @@ -435,7 +454,35 @@ _luadb = { error("Error selecting row(s) inside table " .. t._.tablename .. ": " .. t._.db:Error()) end - return _luadb.nextrow, t + return t:numrows(), t:numfields() + end, + + empty = function(t, fields) + if t._.db:SafeQuery("TRUNCATE TABLE " .. t._.tname) ~= 0 then + error("Error emptying table " .. t._.tablename .. ": " .. t._.db:Error()) + end + end, + + select = function(t, fields) + return _luadb.iselect(t, fields) + end, + + numrows = function(t) + return t._.db._.conn:NumRows() + end, + + numfields = function(t) + return t._.db._.conn:NumFields() + end, + + count = function(t) + _luadb.iselect(t, {"COUNT(*) AS count"}, true) + return t:nextrow().count + end, + + gselect = function(t, fields) + _luadb.iselect(t, fields) + return t.nextrow, t end, nextrow = function(t) |