From 71434c48ff3601fef2659819f7dfb2e529a9d2f5 Mon Sep 17 00:00:00 2001 From: DanyLE Date: Mon, 20 Feb 2023 15:21:02 +0100 Subject: [PATCH] Allow to declare multiple ORs of the same field in the SQL filter object --- silkmvc/core/sqlite.lua | 14 ++++++++++++-- test/test_core.lua | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/silkmvc/core/sqlite.lua b/silkmvc/core/sqlite.lua index 3aa4441..3471bf9 100644 --- a/silkmvc/core/sqlite.lua +++ b/silkmvc/core/sqlite.lua @@ -60,6 +60,10 @@ function SQLQueryGenerator:sql_select() table.insert(segments, o) end + if self.limit then + table.insert(segments, "LIMIT "..self.limit) + end + return true, table.concat(segments, " ") end @@ -161,7 +165,13 @@ function SQLQueryGenerator:sql_where(cond, obj) if k == "$and" or k == "$or" then table.insert(conds, self:sql_where(k, v)) else - table.insert(conds, self:binary(k, v)) + if type(v) == "table" and not (k:match("%$.*in") or k:match("%$.*between") ) then + for i,el in ipairs(v) do + table.insert(conds, self:binary(k, el)) + end + else + table.insert(conds, self:binary(k, v)) + end end end @@ -170,7 +180,7 @@ end function SQLQueryGenerator:parse_value(v, types) if not types[type(v)] then - self:error("Type error: unexpected type %d", type(v)) + self:error("Type error: unexpected type %s", type(v)) end if type(v) == "number" then return tostring(v) diff --git a/test/test_core.lua b/test/test_core.lua index 305e0d3..0ad115e 100644 --- a/test/test_core.lua +++ b/test/test_core.lua @@ -401,6 +401,7 @@ test("SQL Generator", function() user = "dany'", ["$or"] = { email = "test@mail.com", + age = {10,20,30,40}, ["age$ne"] = 30, ["$and"] = { ["birth$ne"] = 1986,