· 9 years ago · Feb 04, 2017, 11:44 PM
1-- thanks to razor for cool wrapper :)
2
3local meta = FindMetaTable("Player")
4
5queryobject = {
6 SetQuery,
7 GetQuery,
8 DoQuerySqlLite,
9 DoQueryMySqloo,
10 StartQuery,
11 OnSuccess,
12 OnFailure,
13 New,
14 SetTable,
15 GetTable,
16 GetData,
17 SetData,
18 TableExists,
19};
20
21function queryobject:New(obj)
22
23 obj = obj || {};
24 setmetatable(obj, self);
25 self.__index = self;
26 return obj;
27
28end
29
30function queryobject:TableExists( tbl )
31
32 if ( gm.SqlType == "sqlite" ) then
33
34 return sql.TableExists( tbl )
35
36 elseif ( gm.SqlType == "mysqloo" ) then
37
38 local query = self:New();
39 query:SetQuery( string.format( "SHOW TABLES WHERE Tables_in_%s LIKE %s", gm.database, SQLStr( tbl ) ) )
40 query:StartQuery();
41
42 local result = query:GetResult()
43
44 if ( table.HasValue(result, tbl) ) then
45
46 return true
47
48 else
49
50 return false
51
52 end
53
54 end
55
56end
57
58function queryobject:SetTable( tbl )
59
60 self.m_strTableQuery = tbl;
61
62end
63
64function queryobject:GetTable( )
65
66 return self.m_strTableQuery;
67
68end
69
70function queryobject:SetResult( tbl )
71
72 self.m_strQueryResult = tbl;
73
74end
75
76function queryobject:GetResult( )
77
78 return self.m_strQueryResult
79
80end
81
82function queryobject:SetData( id, column, value ) -- if your value is a string, make sure to put apostraphes around your string or else the query will fail.
83
84 local query = self:New();
85 query:SetQuery( "UPDATE "..self:GetTable().." SET "..column.." = "..value.." WHERE char_id = '"..id.."'" );
86 query:Start( );
87
88end
89
90function queryobject:GetData( id, column )
91
92 local query = self:New();
93 query:SetQuery( "SELECT "..column.." FROM "..self:GetTable().." WHERE char_id = '"..id.."'" );
94 query:Start( );
95
96 return query:GetResult()
97
98end
99
100function queryobject:SetQuery( query )
101
102 self.m_strQuery = query;
103
104end
105
106function queryobject:GetQuery( )
107
108 return self.m_strQuery;
109
110end
111
112function queryobject:DoQuerySqlLite( )
113
114 local result = sql.Query( self:GetQuery( ) );
115
116 if( !result && result != nil ) then
117
118 self:OnFailure( sql.LastError( ) );
119
120 else
121
122 self:OnSuccess( );
123 self:SetResult( result );
124
125 end
126
127end
128
129function queryobject:DoQueryMySqloo( )
130
131 local qryobj = self
132
133 local query = db:query( self:GetQuery( ) );
134
135 function query:onSuccess( data )
136
137 DEBUG_PRINT("Success")
138
139 local ret = table.Copy( data )
140
141 qryobj:SetResult( ret )
142
143 end
144 query.onError = self.OnFailure;
145
146 query:start( );
147
148 self._query = query;
149
150end
151
152function queryobject:StartQuery( )
153
154 local sqltype = gm.SqlType
155
156 if( sqltype == "sqlite" ) then
157
158 self:DoQuerySqlLite( );
159
160 elseif( sqltype == "mysqloo" ) then
161
162 self:DoQueryMySqloo( );
163
164 end
165
166end
167
168function queryobject:OnSuccess( )
169
170 DEBUG_PRINT( "Success" );
171
172end
173
174function queryobject:OnFailure( reason )
175
176 print( "Error: " .. reason );
177
178end
179
180-------------------------------------------------
181
182sqlbf = {
183 SetupTables,
184};
185
186function sqlbf:SetupTables()
187
188 local characters = queryobject:New();
189 characters:SetQuery( "CREATE TABLE IF NOT EXISTS characters ( steam_id CHAR(64), char_id CHAR(64), xp INT UNSIGNED, level TINYINT UNSIGNED, name TEXT, model TEXT )" );
190
191 local skills = queryobject:New();
192 skills:SetQuery( "CREATE TABLE IF NOT EXISTS skills ( steam_id CHAR(64), char_id CHAR(64), barter INT UNSIGNED, energy INT UNSIGNED, explosives INT UNSIGNED, guns INT UNSIGNED, lockpick INT UNSIGNED, medicine INT UNSIGNED, melee INT UNSIGNED, repair INT UNSIGNED, science INT UNSIGNED, sneak INT UNSIGNED, speech INT UNSIGNED, survival INT UNSIGNED, unarmed INT UNSIGNED )" )
193
194 local inventory = queryobject:New();
195 inventory:SetQuery( "CREATE TABLE IF NOT EXISTS inventory ( steam_id CHAR(64), inv_id CHAR(64), inv TEXT )" );
196
197 inventory:StartQuery();
198 skills:StartQuery();
199 characters:StartQuery();
200
201end