diff options
author | Pixel <pixel@nobis-crew.org> | 2008-10-06 16:32:24 -0700 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2008-10-06 16:32:24 -0700 |
commit | 943ddf6d6cc69e4b29ab0730ce4347b42bb9a446 (patch) | |
tree | 83b86796ee9b495c813c1d68a4b7624b79ace244 /lib | |
parent | 1cfb3dfa1f9a3a827bb674e5d3becbc6dbf7d780 (diff) |
Adding options field to selects.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dblib.lua | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/dblib.lua b/lib/dblib.lua index 7043a2d..43d719c 100644 --- a/lib/dblib.lua +++ b/lib/dblib.lua @@ -518,6 +518,49 @@ _luadb = { stmt = stmt .. " FROM " .. t._.tname .. " WHERE " .. t._.conditions end + if options then + if options.order then + local direction = "ASC" + if type(options.order) ~= "table" then + error("Wrong type for options.order; should be a table.") + end + if type(options.order.by) ~= "string" then + error("The order option has to contain one valid 'by' field.") + end + if options.order.dir then + if type(options.order.dir) ~= "string" then + error("options.order.dir has to be a string") + end + if options.order.dir:upper() == "ASC" then + direction = "ASC" + elseif options.order.dir:upper() == "DESC" then + direction = "DESC" + else + error("options.order.dir has to be either 'asc' or 'desc'") + end + end + stmt = stmt .. " ORDER BY `" .. options.order.by .. "` " .. direction + end + if options.limit then + local start, size = 0, 30 + if options.limit.start then + if type(options.limit.start) ~= "number" then + error("options.limit.start has to be a number") + end + start = options.limit.start + end + if options.limit.size then + if type(options.limit.size) ~= "number" then + error("options.limit.size has to be a number") + end + size = options.limit.size + end + stmt = stmt .. " LIMIT " .. start .. "," .. size + end + end + + stmt = stmt .. ";" + if t._.db:SafeQuery(stmt) ~= 0 then error("Error selecting row(s) inside table " .. t._.tablename .. ": " .. t._.db:Error()) end |