Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: buildWhereClause with table.concat #12

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 '))
Manason marked this conversation as resolved.
Show resolved Hide resolved
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)