Skip to content

Commit

Permalink
refactor: buildWhereClause with table.concat (#12)
Browse files Browse the repository at this point in the history
* refactor: buildWhereClause with table.concat

* fix: or statements in brackets
  • Loading branch information
artur-michalak authored Jun 18, 2024
1 parent dc27b20 commit a229d49
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ local function buildWhereClause(filters)
if not filters then
return '', {}
end
local query = ' WHERE 1=1'
local placeholders = {}
local whereClauseCrumbs = {}
if filters.vehicleId then
query = query .. ' AND id = ?'
whereClauseCrumbs[#whereClauseCrumbs+1] = 'id = ?'
placeholders[#placeholders+1] = filters.vehicleId
end
if filters.citizenid then
query = query .. ' AND citizenid = ?'
whereClauseCrumbs[#whereClauseCrumbs+1] = 'citizenid = ?'
placeholders[#placeholders+1] = filters.citizenid
end
if filters.garage then
query = query .. ' AND garage = ?'
whereClauseCrumbs[#whereClauseCrumbs+1] = 'garage = ?'
placeholders[#placeholders+1] = filters.garage
end
if filters.states then
Expand All @@ -80,10 +80,11 @@ local function buildWhereClause(filters)
placeholders[#placeholders+1] = filters.states[i]
statePlaceholders[i] = 'state = ?'
end
query = query .. string.format(' AND (%s)', table.concat(statePlaceholders, ' OR '))
whereClauseCrumbs[#whereClauseCrumbs+1] = string.format('(%s)', table.concat(statePlaceholders, ' OR '))
end
end
return query, placeholders

return string.format(' WHERE %s', table.concat(whereClauseCrumbs, ' AND ')), placeholders
end

---@param filters? PlayerVehiclesInternalFilters
Expand Down Expand Up @@ -197,4 +198,4 @@ local function deletePlayerVehicles(idType, idValue)
return true
end

exports('DeletePlayerVehicles', deletePlayerVehicles)
exports('DeletePlayerVehicles', deletePlayerVehicles)

0 comments on commit a229d49

Please sign in to comment.