summaryrefslogtreecommitdiff
path: root/gamemode/sv_various/mysql.lua
blob: 4e51e6c42629c6a8b6ad2f0426717d8d739fb24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pcall(require,"mysqloo")

if (!mysqloo) then Msg("--- MySQL functions disabled. MySQLoo Module not found! ---\n") return end

mysql = {}

local Retries		= 0

function mysql:Start(Host,User,Pass,Database)
	local db = mysqloo.connect(Host,User,Pass,Database)
	function db.onConnectionFailed(self,er) Msg( "Connection failed: "..er.."\n" ) end
	function db.onConnected(self) Msg( "Connection has been established.\n" ) end
	db:connect()
	return db
end
	
function mysql:Query(DAT,DB)
	local db = DB or nil
	if (!db) then Msg( "Database does not exist. Call mysql:Start()\n" ) return end
	if (db:status() == mysqloo.DATABASE_NOT_CONNECTED) then 
		if (Retries < 4) then
			Msg( "Reconnecting...\n" ) 
			db:connect() 
			timer.Simple(5,function() self:Query(DAT,DB) end)
			
			 Retries = Retries+1
		else Retries = 0 end
		return
	end

	local DATABASE = db:query(DAT)
	if (DATABASE) then
		DATABASE:start()
		function DATABASE.onError(s,er) Msg( er.."\n" ) end
		function DATABASE.onFailure(s,er) Msg( er.."\n" ) end
	end
	return DATABASE
end