-- Made by Phoenixf129. This system is designed for one MySQL connection only. NOT MULTIPLE! require("mysqloo") -- Always room for sanity checking :) PLib = PLib or { MySQL = {}, QueryQueue = {}, MySQLActive = false, Host = "127.0.0.1", -- Hostname or IP Address to connect to User = "nig", -- Username to connect with Pass = "nag", -- Password to connect with DBName = "nog", -- Database to select Port = 3306 -- Port to connect through. Default 3306. } function PLib:Init() MsgN( "[PLib] Connecting to database.." ) PLib.MySQL = mysqloo.connect( PLib.Host, PLib.User, PLib.Pass, PLib.DBName, PLib.Port ) function PLib.MySQL:onConnected() MsgN( "[PLib] Connection established!" ) PLib.MySQLActive = true for i = 1, #PLib.QueryQueue do MsgN( "[PLib] Running queued query.." ) PLib:RunPreparedQuery( PLib.QueryQueue[ i ] ) end PLib.QueryQueue = {} for k, v in pairs( player.GetAll() ) do v:ColChat( "[PLib]", Color( 255, 0, 0, 255 ), "Connection Successful!" ) end hook.Call( "PLib_DatabaseConnected", nil, PLib ) end function PLib.MySQL:onConnectionFailed( e, sql ) MsgN( "[PLib] Connection FAILED:" .. e ) for k, v in pairs( player.GetAll() ) do v:ColChat( "[PLib]", Color( 255, 0, 0, 255 ), "Connection Failed!" ) end end -- Start the connection process PLib.MySQL:connect() end function PLib:RunPreparedQuery( q ) if istable( self.MySQL ) then table.insert( self.QueryQueue, q ) return false end local query = self.MySQL:query( q.sql ) function query:onSuccess( data ) if string.Left( q.sql, 6 ) == "INSERT" then data = self:lastInsert() end -- MsgN( "[PLib] Query Successful!\nQuery: " .. q.sql ) if q.callback and isfunction( q.callback ) then q.callback( data, unpack( q.vargs or {} ) ) end end function query:onError( e, sql ) MsgN( "[PLib] Query Failed!\nQuery: " .. sql .. "\nError: " .. e ) local stat = PLib.MySQL:status() if stat != mysqloo.DATABASE_CONNECTED then table.insert( PLib.QueryQueue, q ) PLib.MySQL:connect() if stat != mysqloo.DATABASE_CONNECTED then ErrorNoHalt( "[PLib] Re-connection to database server failed!" ) end end end function query:onAborted() ErrorNoHalt( "[PLib] Query aborted!" ) end query:start() end function PLib:QuickQuery( sql ) PLib:RunPreparedQuery( { sql = sql } ) end function PLib:escape( txt ) if type( txt ) != "string" then txt = tostring( txt ) end return txt end hook.Add( "InitPostEntity", "InitPostEntity.InitMySQL", function( p ) PLib:Init() end )