summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/dblib.lua43
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