summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/dblib.lua61
1 files changed, 44 insertions, 17 deletions
diff --git a/lib/dblib.lua b/lib/dblib.lua
index a1036cd..df858bb 100644
--- a/lib/dblib.lua
+++ b/lib/dblib.lua
@@ -28,10 +28,32 @@
ddl = {
field1 = { type = "varchar", length = 32 },
- field2 = { type = "int", length = 11, decimal = 8, default = 42 },
- field3 = { options = "pri,auto" },
+ field2 = { type = "float", length = 11, decimal = 8, default = 42 },
+ id = { options = "pri,auto" },
}
+db = luadb.opendb(host, login, password, database)
+t = db:opentable("foobar", ddl)
+
+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: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"
+
+
]]--
local _luadb
@@ -59,8 +81,8 @@ _luadb = {
return "BLOB"
elseif ttype:upper() == "DATETIME" then
return "DATETIME"
- elseif ttype:upper() == "NUMBER" then
- return "NUMBER"
+ elseif ttype:upper() == "FLOAT" then
+ return "FLOAT"
else
error("Unknow data type in ddl - " .. k .. ": " .. ttype:upper())
end
@@ -263,7 +285,7 @@ _luadb = {
end
-- check base type first.
local ddltype = _luadb.get_canon_type(ddl[f].type, f)
- local desctype, desclength, descdecim = field.Type:match "(%w+)%((%d+)%.(%d+)%)"
+ local desctype, desclength, descdecim = field.Type:match "(%w+)%((%d+)%,(%d+)%)"
if desctype == nil then
desctype, desclength = field.Type:match "(%w+)%((%d+)%)"
end
@@ -372,7 +394,7 @@ _luadb = {
first = false
end
stmt = stmt .. "`" .. k .. "`="
- if type(v) == "string" then
+ if type(v) == "string" or type(v) == "number" then
stmt = stmt .. '"' .. t._.db.sql_escape(v) .. '"'
else
error("Complex UPDATE queries are not supported yet.")
@@ -390,17 +412,21 @@ _luadb = {
local stmt = "SELECT "
local first = true
local k, v
-
- for k, v in pairs(fields) do
- if 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
- stmt = stmt .. ", "
- else
- first = false
+
+ if (fields == nil) then
+ stmt = stmt .. "*"
+ else
+ for k, v in pairs(fields) do
+ if 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
+ stmt = stmt .. ", "
+ else
+ first = false
+ end
+ stmt = stmt .. "`" .. v .. "`"
end
- stmt = stmt .. "`" .. v .. "`"
end
stmt = stmt .. " FROM " .. t._.tname .. " WHERE " .. t._.conditions
@@ -434,7 +460,7 @@ _luadb = {
end,
varops = function(r, op, ...)
- local exprs, first, r, k, v = {...}, true
+ local exprs, first, r, k, v = {...}, true, ""
for k, v in pairs(exprs) do
if not first then
@@ -443,6 +469,7 @@ _luadb = {
r = r .. v
first = false
end
+ return r
end,
},