update!: use new filtering syntax in DBModel
All checks were successful
gitea-sync/silk/pipeline/head This commit looks good

This commit is contained in:
DanyLE 2023-04-03 17:00:05 +02:00
parent 170079053e
commit 7c2e44f178

View File

@ -281,22 +281,16 @@ end
function DBModel:insert(name, m) function DBModel:insert(name, m)
local keys = {} local keys = {}
local values = {} local vals = {}
for k, v in pairs(m) do local gen = SQLQueryGenerator:new({})
for k,v in pairs(m) do
if k ~= "id" then if k ~= "id" then
table.insert(keys, k) table.insert(keys,k)
if type(v) == "number" then table.insert(vals,gen:parse_value(v, {[type(v)] = true}))
table.insert(values, v)
elseif type(v) == "boolean" then
table.insert(values, v and 1 or 0)
else
local t = "\"" .. v:gsub('"', '""') .. "\""
table.insert(values, t)
end
end end
end end
local sql = "INSERT INTO " .. name .. " (" .. table.concat(keys, ',') .. ') VALUES (' local sql = string.format("INSERT INTO %s (%s) VALUES(%s)", name, table.concat(keys,","), table.concat(vals,","))
sql = sql .. table.concat(values, ',') .. ');' LOG_DEBUG("Execute query: [%s]", sql)
return self:exec(sql) return self:exec(sql)
end end
@ -322,30 +316,21 @@ function DBModel:getAll(name)
end end
function DBModel:find(name, cond) function DBModel:find(name, cond)
local cnd = "1=1" local filter = {}
local sel = "*" if cond then
if cond.exp then filter = cond
cnd = self:gencond(cond.exp)
end end
if cond.order then filter.table_name = name
cnd = cnd .. " ORDER BY "
local l = {} local generator = SQLQueryGenerator:new(filter)
local i = 1 local r,sql = generator:sql_select()
for k, v in pairs(cond.order) do if not r then
l[i] = k .. " " .. v LOG_ERROR(sql)
i = i + 1 return nil,sql
end
cnd = cnd .. table.concat(l, ",")
end end
if cond.limit then LOG_DEBUG("Execute query: %s", sql);
cnd = cnd .. " LIMIT " .. cond.limit
end local data = self:query(sql)
if cond.fields then
sel = table.concat(cond.fields, ",")
-- print(sel)
end
-- print(cnd)
local data = self:query(string.format("SELECT %s FROM %s WHERE %s", sel, name, cnd))
if data == nil then if data == nil then
return nil return nil
end end
@ -379,17 +364,15 @@ end
function DBModel:update(name, m) function DBModel:update(name, m)
local id = m['id'] local id = m['id']
if id ~= nil then if id ~= nil then
local lst = {} local tb = {}
for k, v in pairs(m) do local gen = SQLQueryGenerator:new({})
if (type(v) == "number") then for k,v in pairs(m) do
table.insert(lst, k .. "=" .. v) if k ~= "id" then
elseif type(v) == "boolean" then table.insert(tb, string.format("%s=%s", k, gen:parse_value(v, {[type(v)] = true})))
table.insert(lst, k .. "=" .. (v and 1 or 0))
else
table.insert(lst, k .. "=\"" .. v:gsub('"', '""') .. "\"")
end end
end end
local sql = "UPDATE " .. name .. " SET " .. table.concat(lst, ",") .. " WHERE id=" .. id .. ";" local sql = string.format("UPDATE %s SET %s WHERE id = %d", name, table.concat(tb,","), m.id)
LOG_DEBUG("Execute query: [%s]", sql)
return self:exec(sql) return self:exec(sql)
end end
return false return false
@ -403,29 +386,20 @@ function DBModel:deleteByID(name, id)
local sql = "DELETE FROM " .. name .. " WHERE id=" .. id .. ";" local sql = "DELETE FROM " .. name .. " WHERE id=" .. id .. ";"
return self:exec(sql) return self:exec(sql)
end end
function DBModel:gencond(o)
for k, v in pairs(o) do
if k == "and" or k == "or" then
local cnd = {}
local i = 1
for k1, v1 in pairs(v) do
cnd[i] = self:gencond(v1)
i = i + 1
end
return " (" .. table.concat(cnd, " " .. k .. " ") .. ") "
else
for k1, v1 in pairs(v) do
local t = type(v1)
if (t == "string") then
return " (" .. k1 .. " " .. k .. ' "' .. v1:gsub('"', '""') .. '") '
end
return " (" .. k1 .. " " .. k .. " " .. v1 .. ") "
end
end
end
end
function DBModel:delete(name, cond) function DBModel:delete(name, cond)
local sql = "DELETE FROM " .. name .. " WHERE " .. self:gencond(cond) .. ";" local filter = {}
if cond then
filter = cond
end
filter.table_name = name
local generator = SQLQueryGenerator:new(filter)
local r,sql = generator:sql_delete()
if not r then
return error(sql)
end
LOG_DEBUG("Execute query: %s", sql);
return self:exec(sql) return self:exec(sql)
end end