-- ToDatabase - Internal function, to spawn a DB connection
function connectToDatabase(res)
mysqlConnection = mysql_connect(hostname, username, password, database, port)
if (not mysqlConnection) then
if (res == getThisResource()) then
cancelEvent(true, "Cannot connect to the database.")
end
return nil
end
return nil
end
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), connectToDatabase, false)
-- destroyDatabaseConnection - Internal function, kill the connection if theres one.
function destroyDatabaseConnection()
if (not mysqlConnection) then
return nil
end
mysql_close(mysqlConnection)
return nil
end
addEventHandler("onResourceStop", getResourceRootElement(getThisResource()), destroyDatabaseConnection, false)
-- do something usefull here
function sc_journalsQLError(str)
local message = str or 'N/A'
outputDebugString("mysql ERROR "..mysql_errno(mysqlConnection) .. ": " .. mysql_error(mysqlConnection))
exports['sc_journals']:logMessage("mysql ERROR :O! [QUERY] " .. message .. " [ERROR] " .. mysql_errno(mysqlConnection) .. ": " .. mysql_error(mysqlConnection), 24)
end
function getFreeResultpoolID()
local size = #resultpool
if (size == 0) then
return 1
end
for index, query in ipairs(resultpool) do
if (query == nil) then
return index
end
end
return (size + 1)
end
------------ EXPORTED FUNCTIONS ---------------
function ping()
if (mysql_ping(mysqlConnection) == false) then
-- FUU, NO MOAR CONNECTION
destroyDatabaseConnection()
connectToDatabase(nil)
if (mysql_ping(mysqlConnection) == false) then
sc_journalsQLError()
return false
end
return true
end
return true
end
function escape_string(str)
if (ping()) then
return mysql_escape_string(mysqlConnection, str)
end
return false
end
function query(str)
if sqllog then
exports['sc_journals']:logMessage(str, 24)
end
countqueries = countqueries + 1
if (ping()) then
local result = mysql_query(mysqlConnection, str)
if (not result) then
sc_journalsQLError(str)
return false
end
local resultid = getFreeResultpoolID()
resultpool[resultid] = result
return resultid
end
return false
end
function unbuffered_query(str)
if sqllog then
exports['sc_journals']:logMessage(str, 24)
end
countqueries = countqueries + 1
if (ping()) then
local result = mysql_unbuffered_query(mysqlConnection, str)
if (not result) then
sc_journalsQLError(str)
return false
end
local resultid = getFreeResultpoolID()
resultpool[resultid] = result
return resultid
end
return false
end
function query_free(str)
local queryresult = query(str)
if not (queryresult == false) then
free_result(queryresult)
return true
end
return false
end
function rows_assoc(resultid)
if (not resultpool[resultid]) then
return false
end
return mysql_rows_assoc(resultpool[resultid])
end
function fetch_assoc(resultid)
if (not resultpool[resultid]) then
return false
end
return mysql_fetch_assoc(resultpool[resultid])
end
function free_result(resultid)
if (not resultpool[resultid]) then
return false
end
mysql_free_result(resultpool[resultid])
table.remove(resultpool, resultid)
return nil
end
-- incase a nub wants to use it, FINE
function result(resultid, row_offset, field_offset)
if (not resultpool[resultid]) then
return false
end
return mysql_result(resultpool[resultid], row_offset, field_offset)
end
function num_rows(resultid)
if (not resultpool[resultid]) then
return false
end
return mysql_num_rows(resultpool[resultid])
end
function insert_id()
return mysql_insert_id(mysqlConnection) or false
end
function query_fetch_assoc(str)
local queryresult = query(str)
if not (queryresult == false) then
local result = fetch_assoc(queryresult)
free_result(queryresult)
return result
end
return false
end
function query_rows_assoc(str)
local queryresult = query(str)
if not (queryresult == false) then
local result = rows_assoc(queryresult)
free_result(queryresult)
return result
end
return false
end
function query_insert_free(str)
local queryresult = query(str)
if not (queryresult == false) then
local result = insert_id()
free_result(queryresult)
return result
end
return false
end
function escape_string(str)
return mysql_escape_string(mysqlConnection, str)
end
function debugMode()
if (sqllog) then
sqllog = false
else
sqllog = true
end
return sqllog
end
function returnQueryStats()
return countqueries
-- maybe later more
end
this script, I only removed the database login details