Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MySQL = {}
- MySQL.ConVars = {}
- MySQL.ConVars.Host = CreateConVar("mysql_host", "localhost", { FCVAR_PROTECTED })
- MySQL.ConVars.User = CreateConVar("mysql_user", "root", { FCVAR_PROTECTED })
- MySQL.ConVars.Pass = CreateConVar("mysql_pass", "", { FCVAR_PROTECTED })
- MySQL.ConVars.DB = CreateConVar("mysql_db", "sledge", { FCVAR_PROTECTED })
- MySQL.ConVars.Port = CreateConVar("mysql_port", "3306", { FCVAR_PROTECTED })
- MySQL.ConVars.Connections = CreateConVar("mysql_connections", "6", { FCVAR_PROTECTED })
- MySQL.ConVars.Threads = CreateConVar("mysql_threads", "5", { FCVAR_PROTECTED })
- MySQL.ConVars.Debug = CreateConVar("mysql_debug", 0, { FCVAR_NOTIFY })
- MySQL.ConCmds = {}
- MySQL.QueryHistory = {}
- MySQL.QueryID = 0
- MySQL.Queries = 0
- MySQL.CompletedQueries = 0
- MySQL.Thinks = 0
- MySQL.Connected = false
- MySQL.Type = "none"
- require("mysqloo")
- if mysqloo then
- MySQL.Type = "mysqloo"
- else
- require("tmysql")
- if(tmysql) then
- MySQL.Type = "tmysql"
- else
- require("mysql")
- if(mysql) then
- MySQL.Type = "mysql"
- end
- end
- end
- --- Connect MySQL. Self explanatory.
- function MySQL:Connect()
- if(self.Type == "none") then return end
- if (self.NextConnection or 0) > CurTime() then return end
- local host, user, pass, db, port, connections, threads = self.ConVars.Host:GetString(), self.ConVars.User:GetString(), self.ConVars.Pass:GetString(), self.ConVars.DB:GetString(), self.ConVars.Port:GetInt(), self.ConVars.Connections:GetInt(), self.ConVars.Threads:GetInt()
- if self.Type == "mysqloo" then
- self.Database = mysqloo.connect(host, user, pass, db, port)
- self.Database.onConnected = self.OOConnected
- self.Database:connect()
- Msg("Initializing connection to "..user.."@"..host..":"..port.." (MySQLOO)\n")
- self.NextConnection = CurTime()+5
- elseif(self.Type == "tmysql") then
- Msg("Initializing connection to "..user.."@"..host..":"..port.." with "..connections.." connection(s) on "..threads.." thread(s) (tMySQL)\n")
- local p, err = pcall(function() return tmysql.initialize(host, user, pass, db, port, connections, threads) end)
- self.Connected = util.tobool(p)
- elseif(self.Type == "mysql") then
- Msg("Initializing connection to "..user.."@"..host..":"..port.." (MySQL)\n")
- self.Connection, self.ConnectionError = mysql.connect(host, user, pass, db, port)
- if(not util.tobool(self.Connection)) then
- Msg("Connection failed: "..self.ConnectionError.."\n")
- end
- else
- Msg("Unknown MySQL type: "..self.Type.."!\n")
- end
- end
- function MySQL.OOConnected()
- local host, user, pass, db, port, connections, threads = MySQL.ConVars.Host:GetString(), MySQL.ConVars.User:GetString(), MySQL.ConVars.Pass:GetString(), MySQL.ConVars.DB:GetString(), MySQL.ConVars.Port:GetInt(), MySQL.ConVars.Connections:GetInt(), MySQL.ConVars.Threads:GetInt()
- Msg("Successfuly connected to "..user.."@"..host..":"..port.."\n")
- end
- --- Run a MySQL query
- -- @param query The query to be run
- -- @param callback The callback to be run when finished, will be called as <code>callback((table) results, (number) status, (string) error, args)</code>
- -- @param ... Extra arguments to be called in the callback
- function MySQL:Query(query, callback, ...)
- if(self.Type == "none") then return end
- if(not self.IgnoreConnection and not self:ConnectionCheck()) then
- return false, "MySQL Not Connected"
- end
- if(type(callback) ~= "function") then
- Msg("[WARNING] Using default callback for MySQL query.\n")
- callback = self.DefaultCallback
- end
- local qid = self.QueryID
- self.QueryID = qid+1
- self.QueryHistory[qid] = {
- Query = query,
- StartTime = RealTime(),
- FinishTime = -1,
- Args = {...}
- }
- if self.Type == "mysqloo" then
- local q = self.Database:query(query)
- if q == nil then return false, "Failed to create query" end
- q.onSuccess = function() self:QueryDone(qid, callback, q:getData(), true, "Done") end
- q.onFailure = function(err) self:QueryDone(qid, callback, {}, false, err) end
- q:start()
- return true, "OK"
- elseif(self.Type == "tmysql") then
- --Msg("Running query: "..query.."\n")
- tmysql.query(query, function(...) return self:QueryDone(qid, callback, ...) end, 1)
- self.Queries = self.Queries+1
- return true, "OK"
- elseif(self.Type == "mysql") then
- Msg("MySQL querying not implemented yet, go get tMySQL!\n")
- return false, "MySQL module not implemented"
- end
- return false, "No known MySQL module"
- end
- function MySQL:QueryDone(qid, fn, result, status, err)
- self.QueryHistory[qid].FinishTime = RealTime()
- self.CompletedQueries = self.CompletedQueries+1
- local r, s, e = result, status, err
- if(type(r) ~= "table") then
- r, s, e = {}, r, s
- end
- return fn(r, s, e, unpack(self.QueryHistory[qid].Args))
- end
- --- Escape a string for use in a query
- -- @param str The string to escape
- -- @param IgnoreConnection (OPTIONAL) (Defaults to false) Wether or not to check the connection
- -- @return The escaped string
- function MySQL:Escape(str, IgnoreConnection)
- if(self.Type == "none") then return end
- if(not IgnoreConnection and not self:ConnectionCheck()) then
- error("MySQL not connected", 2)
- return
- end
- if(type(str) ~= "string") then
- str = tostring(str)
- end
- if self.Type == "mysqloo" then
- return self.Database:escape(str)
- elseif self.Type == "tmysql" then
- --[[local ret = tmysql.escape_real(str)
- if(type(ret) ~= "string") then
- ret = tmysql.escape(str)
- end]]--
- local ret = tmysql.escape(str)
- ret = (type(ret) == "string" and ret or nil) -- If it isn't a valid string, return nil for safety's sake.
- return ret
- end
- end
- function MySQL:DefaultCallback(result, status, err)
- Msg("Default MySQL query:\n")
- Msg("\tResult:\n")
- if(type(result) == "table") then
- PrintTable(result)
- else
- Msg("\t\t"..tostring(result).."\n")
- end
- Msg("\tStatus: "..tostring(status).."\n")
- Msg("\tError: "..tostring(err).."\n")
- end
- --- Checks the MySQL connection
- -- @param rep Wether or not this is a repeated check
- -- @return The connection status
- function MySQL:ConnectionCheck()
- if self.Type == "mysqloo" then
- if self.Database == nil then return false end
- return true
- elseif self.Type == "tymsql" then
- return util.tobool(self.Connected)
- end
- end
- function MySQL:Think()
- if(self.Type == "none") then return end
- --[[tmysql.debugmode(self.ConVars.Debug:GetBool())]]--
- if(type(self.LastConnectionCheck) ~= "number") then self.LastConnectionCheck = 0 end
- if(self.Thinks == 0) then
- self:Connect()
- elseif(self.LastConnectionCheck+3 <= RealTime()) then
- if(not self:ConnectionCheck()) then
- self:Connect()
- end
- self.LastConnectionCheck = RealTime()
- end
- self.Thinks = self.Thinks+1
- end
- MODULE.Think = function(self, ...) return MySQL:Think(...) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement