From a229d49f7ed27c70b880ae925fd5b50629ca782b Mon Sep 17 00:00:00 2001 From: HC DEV <36608116+ArturMichalak@users.noreply.github.com> Date: Tue, 18 Jun 2024 22:05:28 +0200 Subject: [PATCH] refactor: buildWhereClause with table.concat (#12) * refactor: buildWhereClause with table.concat * fix: or statements in brackets --- server/main.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server/main.lua b/server/main.lua index 7611d3b..17b6339 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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 @@ -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 @@ -197,4 +198,4 @@ local function deletePlayerVehicles(idType, idValue) return true end -exports('DeletePlayerVehicles', deletePlayerVehicles) \ No newline at end of file +exports('DeletePlayerVehicles', deletePlayerVehicles)