· 10 years ago · Sep 05, 2016, 04:02 AM
1_GM = GM or GAMEMODE
2
3require("tmysql4")
4
5_GM.MYSQL = {}
6_GM.MYSQL.Host = "removed cus i can"
7_GM.MYSQL.Port = blah
8_GM.MYSQL.User = "blah"
9_GM.MYSQL.Pass = "no"
10_GM.MYSQL.DB = "blah"
11
12
13if CLIENT_MULTI_STATEMENTS then
14 _GM.ErrorHandler:DebugPrint( 1, "TMYSQL4 MODULE LOADED SUCCESSFULLY!", Color( 0, 255, 0 ) )
15else
16 _GM.ErrorHandler:DebugPrint( 1, "TMYSQL4 DID NOT LOAD PROPERLY! IS THE MODULE INSTALLED?", Color( 255, 0, 0 ) )
17 return
18end
19
20function _GM.MYSQL:Connect()
21 DB, ERR = tmysql.Create( _GM.MYSQL.Host, _GM.MYSQL.User, _GM.MYSQL.Pass, _GM.MYSQL.DB, _GM.MYSQL.Port )
22
23 _cur_status, _cur_err = DB:Connect()
24
25 if _cur_err or ERR then
26 _GM.ErrorHandler:DebugPrint( 1, _cur_err or ERR, Color( 255, 0, 0 ) )
27 end
28
29 if _cur_status then
30 _GM.ErrorHandler:DebugPrint( 1, "Gamemode Database successfully connected: " .. tostring( _GM.MYSQL.Host ), Color( 0, 255, 0 ) )
31 else
32 _GM.ErrorHandler:DebugPrint( 1, "Gamemode Database failed to connect to MySQL.", Color( 255, 0, 0 ) )
33 end
34end
35
36_GM.MYSQL:Connect()
37
38function _GM.MYSQL:Disconnect()
39 DB:Disconnect()
40 _GM.ErrorHandler:DebugPrint( 1, "Database has been disconnected", Color( 255, 255, 0 ) )
41 -- ¯\_(ツ)_/¯
42end
43
44function _GM.MYSQL:Query( _String, _Callback )
45
46 if DB then
47 DB:Query( _String, _Callback )
48 else
49 _GM.ErrorHandler:DebugPrint( 1, "No database detected. wtf?", Color( 255, 0, 0 ) )
50 end
51end
52
53function _GM.MYSQL:DebugQuery( _String, _Callback )
54
55 local _NiceString = string.format( _String )
56 local _NiceString = ''
57
58 if DB then
59 DB:Query( _String, onCompleted )
60 else
61 _GM.ErrorHandler:DebugPrint( 1, "No database detected. wtf?", Color( 255, 0, 0 ) )
62 end
63
64 function onCompleted( _Results )
65
66 if _Callback then
67 _Callback( _Results )
68 end
69
70 if _Results[1].status then
71 _GM.ErrorHandler:DebugPrint( 1, "Query " .. _NiceString .. " ran successfully", Color( 255, 255, 0 ) )
72 else
73 _GM.ErrorHandler:DebugPrint( 1, "Query " .. _NiceString .. " failed!\n" .. _Results[1].error, Color( 255, 0, 0 ) )
74 return
75 end
76
77 PrintTable( _Results[1].data )
78 end
79end
80
81function _GM.MYSQL:GetActiveConnections()
82 return tmysql.GetTable()
83end
84
85function _GM.MYSQL:Escape( _String )
86 if not DB then return end
87
88 return DB:Escape( _String )
89end
90
91function _GM.MYSQL:CreateTable( _Name, _Arguments )
92 --let's make sure we don't accidently write over a pre-existing table... ouch.
93 _GM.MYSQL:DebugQuery(" CREATE TABLE IF NOT EXISTS `" .. _Name .. "` ( " .. _Arguments .. " ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB;", function( _Returned ) end)
94end
95
96function _GM.MYSQL:CreateRPTables()
97
98 _GM.MYSQL:Query( "SELECT COUNT(*) FROM _players", function( _return )
99
100 if not _return[1].data or table.Count( _return[1].data ) == 0 then
101 _GM.ErrorHandler:DebugPrint( 1, "Creating RP tables (They don't seem to exist yet)", Color( 255, 255, 0 ) )
102
103 _GM.MYSQL:CreateTable( '_players', [[
104 `_steamid` CHAR(32) NOT NULL,
105 `_first_name` VARCHAR(32) NOT NULL,
106 `_last_name` VARCHAR(32) NOT NULL,
107 `_cash` BIGINT NOT NULL,
108 `_model` TEXT(32) NOT NULL,
109 `_xp` SMALLINT NOT NULL,
110 `_rank` TEXT(32) NOT NULL,
111 `_color` VARCHAR(32) NOT NULL DEFAULT '1;1;1',
112 `_physgun` VARCHAR(32) NOT NULL DEFAULT '1;1;1',
113 UNIQUE INDEX `steamid` (`_steamid`)
114 ]])
115
116 _GM.ErrorHandler:DebugPrint( 1, "Query finished! -- Player RP tables created", Color( 255, 0, 0 ) )
117 end
118 end)
119end
120
121_GM.MYSQL:CreateRPTables()