feat(server): SQL error handling

This commit is contained in:
Kasey FItton
2024-07-11 18:53:27 +01:00
parent b94cad23f6
commit e39fe89140
3 changed files with 50 additions and 0 deletions
+1
View File
@@ -17,6 +17,7 @@ local Translations = {
no_permission = 'You don\'t have permissions for this..',
no_waypoint = 'No Waypoint Set.',
tp_error = 'Error While Teleporting.',
ban_table_not_found = '[QBCORE] - Unable to find the bans table in the database. Please ensure you have imported the SQL file correctly.',
connecting_database_error = '[QBCORE] - A database error occurred while connecting to the server. (Is the SQL server on?)',
connecting_database_timeout = '[QBCORE] - Connection to database timed out. (Is the SQL server on?)',
},
+22
View File
@@ -20,6 +20,20 @@ end)
-- Player Connecting
local databaseConnected = false
local bansTableExists = false
MySQL.Ready(function()
databaseConnected = true
local DatabaseInfo = QBCore.Functions.GetDatabaseInfo()
if not DatabaseInfo or not DatabaseInfo.exists then return end
local result = MySQL.query.await('SELECT * FROM information_schema.tables WHERE table_schema = "%s" AND table_name = "bans";', {DatabaseInfo.database})
if result and result[1] then
bansTableExists = true
end
end)
local function onPlayerConnecting(name, _, deferrals)
local src = source
deferrals.defer()
@@ -28,6 +42,10 @@ local function onPlayerConnecting(name, _, deferrals)
return deferrals.done(QBCore.Config.Server.ClosedReason)
end
if not databaseConnected then
return deferrals.done(Lang:t('error.connecting_database_error'))
end
if QBCore.Config.Server.Whitelist then
Wait(0)
deferrals.update(string.format(Lang:t('info.checking_whitelisted'), name))
@@ -49,6 +67,10 @@ local function onPlayerConnecting(name, _, deferrals)
Wait(0)
deferrals.update(string.format(Lang:t('info.checking_ban'), name))
if not bansTableExists then
return deferrals.done(Lang:t('error.ban_table_not_found'))
end
local success, isBanned, reason = pcall(QBCore.Functions.IsPlayerBanned, src)
if not success then return deferrals.done(Lang:t('error.connecting_database_error')) end
if isBanned then return deferrals.done(reason) end
+27
View File
@@ -623,6 +623,33 @@ function QBCore.Functions.IsPlayerBanned(source)
return false
end
function QBCore.Functions.GetDatabaseInfo()
local details = {
exists = false,
database = "",
}
local connectionString = GetConvar("mysql_connection_string", "")
if connectionString == "" then
return details
elseif connectionString:find("mysql://") then
connectionString = connectionString:sub(9, -1)
details.database = connectionString:sub(connectionString:find("/") + 1, -1):gsub("[%?]+[%w%p]*$", "")
details.exists = true
else
connectionString = { string.strsplit(";", connectionString) }
for i = 1, #connectionString do
local v = connectionString[i]
if v:match("database") then
details.database = v:sub(10, #v)
details.exists = true
break
end
end
end
end
---Check for duplicate license
---@param license any
---@return boolean