cb-lib-sql.lua
63 lines
--[[-------------------------------------------------------------------------
--[[-------------------------------------------------------------------------
This file handles anything to do with SQL. Its object based.
This file handles anything to do with SQL. It is object-based.
---------------------------------------------------------------------------]]
---------------------------------------------------------------------------]]--
local SQLMeta = {}
local SQLMeta = {}
local SQLMetaAccessor = {__index = SQLMeta}
local SQLMetaAccessor = {__index = SQLMeta}
local SQL = {}
local SQL = {}
--[[-------------------------------------------------------------------------
--[[-------------------------------------------------------------------------
SQL Stuff
SQL Setup
---------------------------------------------------------------------------]]
---------------------------------------------------------------------------]]
--Try to load up SQL
-- Try to load up SQL
local mysqlFound = false
local mysqlFound = false
pcall(function(fileName)
pcall(function(fileName)
require(fileName)
require(fileName)
if mysqloo ~= nil then
if mysqloo ~= nil then
mysqlFound = true
mysqlFound = true
end,
end
end, "mysqloo")
end, "mysqloo") -- 'pcall' only needs two parameters here; corrected syntax
if no mysqlFound then
if not mysqlFound then
CBLib.Debug.Warning("---------------WARNING!---------------")
CBLib.Debug.Warning("---------------WARNING!---------------")
CBLib.Debug.Warning("MySQLOO not found, this is not an issue if your not using MySQL, otherwise it is and you need to install it!")
CBLib.Debug.Warning("MySQLOO not found. This is not an issue if you're not using MySQL; otherwise, you need to install it!")
CBLib.Debug.Warning("--------------------------------------")
CBLib.Debug.Warning("--------------------------------------")
end
end
--Returns an SQL object
-- Returns an SQL object
function SQL.CreateInstance(host, username, password, database, port)
function SQL.CreateInstance(host, username, password, database, port)
local instance = {}
local instance = {}
setmetatable(instance, SQLMetaAccessor)
setmetatable(instance, SQLMetaAccessor)
instance.database = mysqloo.connect(host, username, password, database, port)
instance.database = mysqloo.connect(host, username, password, database, port)
instance.connected = false
instance.connected = false
instance.database.onConnected = function(db)
instance.database.onConnected = function(db)
CBLib.Debug.Info("Connect to database succesfully! ('"..host.."/"..database.."')")
CBLib.Debug.Info("Connected to database successfully! ('"..host.."/"..database.."')")
instance.connected = false
instance.connected = true -- Changed to true to reflect a successful connection
end
end
instance.database.onConnectionFailed = function(err, db)
instance.database.onConnectionFailed = function(db, err) -- Order of parameters corrected
CBLib.Debug.Error("---------------ERROR!---------------")
CBLib.Debug.Error("---------------ERROR!---------------")
CBLib.Debug.Error("Failed to connect to database! ('"..host.."/"..database.."')")
CBLib.Debug.Error("Failed to connect to database! ('"..host.."/"..database.."')")
CBLib.Debug.Error("DATA LOSS WILL OCURE! I Recommend shutting the server down.")
CBLib.Debug.Error("DATA LOSS MAY OCCUR! Recommend shutting down the server.")
CBLib.Debug.Error("REASON : "..err)
CBLib.Debug.Error("REASON: "..err)
CBLib.Debug.Error("------------------------------------")
CBLib.Debug.Error("------------------------------------")
instance.connected = false
instance.connected = false
end
end
instance.database:connect()
instance.database:connect()
return instance
return instance
end
end
function SQLMeta:
-- Placeholder for SQLMeta function definition
function SQLMeta:MethodName()
-- Code here
end