mirror of
https://github.com/qbcore-fivem/qb-core.git
synced 2026-08-29 01:08:57 +00:00
Merge pull request #1126 from Mycroft-Studios/sql-error-handling
feat(server): SQL error handling
This commit is contained in:
+3
-2
@@ -17,8 +17,9 @@ local Translations = {
|
||||
no_permission = 'You don\'t have permissions for this..',
|
||||
no_waypoint = 'No Waypoint Set.',
|
||||
tp_error = 'Error While Teleporting.',
|
||||
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?)',
|
||||
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] - An error occurred while connecting to the database. Ensure that the SQL server is running and that the details in the server.cfg file are correct.',
|
||||
connecting_database_timeout = '[QBCORE] - The database connection has timed out. Ensure that the SQL server is running and that the details in the server.cfg file are correct.',
|
||||
},
|
||||
success = {
|
||||
server_opened = 'The server has been opened',
|
||||
|
||||
@@ -19,6 +19,21 @@ AddEventHandler('playerDropped', function(reason)
|
||||
end)
|
||||
|
||||
-- Player Connecting
|
||||
local readyFunction = MySQL.ready
|
||||
local databaseConnected, bansTableExists = readyFunction == nil, readyFunction == nil
|
||||
if readyFunction ~= nil then
|
||||
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 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCEMA = ? AND TABLE_NAME = "bans";', {DatabaseInfo.database})
|
||||
if result and result[1] then
|
||||
bansTableExists = true
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
local function onPlayerConnecting(name, _, deferrals)
|
||||
local src = source
|
||||
@@ -28,6 +43,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 +68,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
|
||||
|
||||
@@ -623,6 +623,36 @@ function QBCore.Functions.IsPlayerBanned(source)
|
||||
return false
|
||||
end
|
||||
|
||||
-- Retrieves information about the database connection.
|
||||
--- @return table; A table containing the database information.
|
||||
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
|
||||
return details
|
||||
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
|
||||
return details
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Check for duplicate license
|
||||
---@param license any
|
||||
---@return boolean
|
||||
|
||||
Reference in New Issue
Block a user