daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Jun 5th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MySQL = {}
  2. MySQL.ConVars = {}
  3. MySQL.ConVars.Host = CreateConVar("mysql_host", "localhost", { FCVAR_PROTECTED })
  4. MySQL.ConVars.User = CreateConVar("mysql_user", "root", { FCVAR_PROTECTED })
  5. MySQL.ConVars.Pass = CreateConVar("mysql_pass", "", { FCVAR_PROTECTED })
  6. MySQL.ConVars.DB = CreateConVar("mysql_db", "sledge", { FCVAR_PROTECTED })
  7. MySQL.ConVars.Port = CreateConVar("mysql_port", "3306", { FCVAR_PROTECTED })
  8. MySQL.ConVars.Connections = CreateConVar("mysql_connections", "6", { FCVAR_PROTECTED })
  9. MySQL.ConVars.Threads = CreateConVar("mysql_threads", "5", { FCVAR_PROTECTED })
  10. MySQL.ConVars.Debug = CreateConVar("mysql_debug", 0, { FCVAR_NOTIFY })
  11. MySQL.ConCmds = {}
  12. MySQL.QueryHistory = {}
  13. MySQL.QueryID = 0
  14. MySQL.Queries = 0
  15. MySQL.CompletedQueries = 0
  16. MySQL.Thinks = 0
  17. MySQL.Connected = false
  18. MySQL.Type = "none"
  19. require("mysqloo")
  20. if mysqloo then
  21.     MySQL.Type = "mysqloo"
  22. else
  23.     require("tmysql")
  24.     if(tmysql) then
  25.         MySQL.Type = "tmysql"
  26.     else
  27.         require("mysql")
  28.         if(mysql) then
  29.             MySQL.Type = "mysql"
  30.         end
  31.     end
  32. end
  33. --- Connect MySQL. Self explanatory.
  34. function MySQL:Connect()
  35.     if(self.Type == "none") then return end
  36.     if (self.NextConnection or 0) > CurTime() then return end
  37.     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()
  38.     if self.Type == "mysqloo" then
  39.         self.Database = mysqloo.connect(host, user, pass, db, port)
  40.         self.Database.onConnected = self.OOConnected
  41.         self.Database:connect()
  42.         Msg("Initializing connection to "..user.."@"..host..":"..port.." (MySQLOO)\n")
  43.         self.NextConnection = CurTime()+5
  44.     elseif(self.Type == "tmysql") then
  45.         Msg("Initializing connection to "..user.."@"..host..":"..port.." with "..connections.." connection(s) on "..threads.." thread(s) (tMySQL)\n")
  46.         local p, err = pcall(function() return tmysql.initialize(host, user, pass, db, port, connections, threads) end)
  47.         self.Connected = util.tobool(p)
  48.     elseif(self.Type == "mysql") then
  49.         Msg("Initializing connection to "..user.."@"..host..":"..port.." (MySQL)\n")
  50.         self.Connection, self.ConnectionError = mysql.connect(host, user, pass, db, port)
  51.         if(not util.tobool(self.Connection)) then
  52.             Msg("Connection failed: "..self.ConnectionError.."\n")
  53.         end
  54.     else
  55.         Msg("Unknown MySQL type: "..self.Type.."!\n")
  56.     end
  57. end
  58.  
  59. function MySQL.OOConnected()
  60.     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()
  61.     Msg("Successfuly connected to "..user.."@"..host..":"..port.."\n")
  62. end
  63. --- Run a MySQL query
  64. -- @param query The query to be run
  65. -- @param callback The callback to be run when finished, will be called as <code>callback((table) results, (number) status, (string) error, args)</code>
  66. -- @param ... Extra arguments to be called in the callback
  67. function MySQL:Query(query, callback, ...)
  68.     if(self.Type == "none") then return end
  69.     if(not self.IgnoreConnection and not self:ConnectionCheck()) then
  70.         return false, "MySQL Not Connected"
  71.     end
  72.     if(type(callback) ~= "function") then
  73.         Msg("[WARNING] Using default callback for MySQL query.\n")
  74.         callback = self.DefaultCallback
  75.     end
  76.     local qid = self.QueryID
  77.     self.QueryID = qid+1
  78.     self.QueryHistory[qid] = {
  79.         Query = query,
  80.         StartTime = RealTime(),
  81.         FinishTime = -1,
  82.         Args = {...}
  83.     }
  84.     if self.Type == "mysqloo" then
  85.         local q = self.Database:query(query)
  86.         if q == nil then return false, "Failed to create query" end
  87.         q.onSuccess = function() self:QueryDone(qid, callback, q:getData(), true, "Done") end
  88.         q.onFailure = function(err) self:QueryDone(qid, callback, {}, false, err) end
  89.         q:start()
  90.         return true, "OK"
  91.     elseif(self.Type == "tmysql") then
  92.         --Msg("Running query: "..query.."\n")
  93.         tmysql.query(query, function(...) return self:QueryDone(qid, callback, ...) end, 1)
  94.         self.Queries = self.Queries+1
  95.         return true, "OK"
  96.     elseif(self.Type == "mysql") then
  97.         Msg("MySQL querying not implemented yet, go get tMySQL!\n")
  98.         return false, "MySQL module not implemented"
  99.     end
  100.     return false, "No known MySQL module"
  101. end
  102. function MySQL:QueryDone(qid, fn, result, status, err)
  103.     self.QueryHistory[qid].FinishTime = RealTime()
  104.     self.CompletedQueries = self.CompletedQueries+1
  105.     local r, s, e = result, status, err
  106.     if(type(r) ~= "table") then
  107.         r, s, e = {}, r, s
  108.     end
  109.     return fn(r, s, e, unpack(self.QueryHistory[qid].Args))
  110. end
  111. --- Escape a string for use in a query
  112. -- @param str The string to escape
  113. -- @param IgnoreConnection (OPTIONAL) (Defaults to false) Wether or not to check the connection
  114. -- @return The escaped string
  115. function MySQL:Escape(str, IgnoreConnection)
  116.     if(self.Type == "none") then return end
  117.     if(not IgnoreConnection and not self:ConnectionCheck()) then
  118.         error("MySQL not connected", 2)
  119.         return
  120.     end
  121.     if(type(str) ~= "string") then
  122.         str = tostring(str)
  123.     end
  124.     if self.Type == "mysqloo" then
  125.         return self.Database:escape(str)
  126.     elseif self.Type == "tmysql" then
  127.         --[[local ret = tmysql.escape_real(str)
  128.         if(type(ret) ~= "string") then
  129.             ret = tmysql.escape(str)
  130.         end]]--
  131.         local ret = tmysql.escape(str)
  132.         ret = (type(ret) == "string" and ret or nil) -- If it isn't a valid string, return nil for safety's sake.
  133.         return ret
  134.     end
  135. end
  136. function MySQL:DefaultCallback(result, status, err)
  137.     Msg("Default MySQL query:\n")
  138.     Msg("\tResult:\n")
  139.     if(type(result) == "table") then
  140.         PrintTable(result)
  141.     else
  142.         Msg("\t\t"..tostring(result).."\n")
  143.     end
  144.     Msg("\tStatus: "..tostring(status).."\n")
  145.     Msg("\tError: "..tostring(err).."\n")
  146. end
  147. --- Checks the MySQL connection
  148. -- @param rep Wether or not this is a repeated check
  149. -- @return The connection status
  150. function MySQL:ConnectionCheck()
  151.     if self.Type == "mysqloo" then
  152.         if self.Database == nil then return false end
  153.         return true
  154.     elseif self.Type == "tymsql" then
  155.         return util.tobool(self.Connected)
  156.     end
  157. end
  158. function MySQL:Think()
  159.     if(self.Type == "none") then return end
  160.     --[[tmysql.debugmode(self.ConVars.Debug:GetBool())]]--
  161.     if(type(self.LastConnectionCheck) ~= "number") then self.LastConnectionCheck = 0 end
  162.     if(self.Thinks == 0) then
  163.         self:Connect()
  164.     elseif(self.LastConnectionCheck+3 <= RealTime()) then
  165.         if(not self:ConnectionCheck()) then
  166.             self:Connect()
  167.         end
  168.         self.LastConnectionCheck = RealTime()
  169.     end
  170.     self.Thinks = self.Thinks+1
  171. end
  172. MODULE.Think = function(self, ...) return MySQL:Think(...) end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top