|
pred@2009
|
1 |
/** |
|
pred@2009
|
2 |
* vim: set ts=4 : |
|
pred@2009
|
3 |
* ============================================================================= |
|
pred@2009
|
4 |
* SourceMod Client Preferences Extension |
|
pred@2009
|
5 |
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
|
pred@2009
|
6 |
* ============================================================================= |
|
pred@2009
|
7 |
* |
|
pred@2009
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
pred@2009
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
pred@2009
|
10 |
* Free Software Foundation. |
|
pred@2009
|
11 |
* |
|
pred@2009
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
pred@2009
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
pred@2009
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
pred@2009
|
15 |
* details. |
|
pred@2009
|
16 |
* |
|
pred@2009
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
pred@2009
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
pred@2009
|
19 |
* |
|
pred@2009
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
pred@2009
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
pred@2009
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
pred@2009
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
pred@2009
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
pred@2009
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
pred@2009
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
pred@2009
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
pred@2009
|
28 |
* |
|
pred@2009
|
29 |
* Version: $Id$ |
|
pred@2009
|
30 |
*/ |
|
pred@2009
|
31 |
|
|
pred@2009
|
32 |
#include "query.h" |
|
pred@2009
|
33 |
|
|
pred@2009
|
34 |
|
|
pred@2009
|
35 |
void TQueryOp::RunThinkPart() |
|
pred@2009
|
36 |
{ |
|
pred@2009
|
37 |
//handler for threaded sql queries |
|
pred@2009
|
38 |
|
|
pred@2207
|
39 |
if (m_type == Query_Connect) |
|
pred@2207
|
40 |
{ |
|
pred@2207
|
41 |
return; |
|
pred@2207
|
42 |
} |
|
pred@2207
|
43 |
|
|
pred@2009
|
44 |
if (m_pQuery) |
|
pred@2009
|
45 |
{ |
|
pred@2009
|
46 |
switch (m_type) |
|
pred@2009
|
47 |
{ |
|
pred@2207
|
48 |
case Query_InsertCookie: |
|
pred@2207
|
49 |
{ |
|
pred@2207
|
50 |
g_CookieManager.InsertCookieCallback(m_pCookie, m_insertId); |
|
pred@2207
|
51 |
break; |
|
pred@2207
|
52 |
} |
|
pred@2207
|
53 |
|
|
pred@2207
|
54 |
case Query_SelectData: |
|
pred@2207
|
55 |
{ |
|
pred@2207
|
56 |
g_CookieManager.ClientConnectCallback(m_client, m_pQuery); |
|
pred@2207
|
57 |
break; |
|
pred@2207
|
58 |
} |
|
pred@2207
|
59 |
|
|
pred@2207
|
60 |
case Query_SelectId: |
|
pred@2207
|
61 |
{ |
|
pred@2207
|
62 |
g_CookieManager.SelectIdCallback(m_pCookie, m_pQuery); |
|
pred@2207
|
63 |
break; |
|
pred@2207
|
64 |
} |
|
pred@2207
|
65 |
|
|
pred@2207
|
66 |
case Query_CreateTable: |
|
pred@2207
|
67 |
{ |
|
pred@2207
|
68 |
m_pQuery->Destroy(); |
|
pred@2207
|
69 |
delete m_pQuery; |
|
pred@2207
|
70 |
m_pQuery = NULL; |
|
pred@2207
|
71 |
break; |
|
pred@2207
|
72 |
} |
|
pred@2207
|
73 |
|
|
pred@2207
|
74 |
default: |
|
pred@2207
|
75 |
{ |
|
pred@2207
|
76 |
break; |
|
pred@2207
|
77 |
} |
|
pred@2009
|
78 |
} |
|
pred@2009
|
79 |
} |
|
pred@2009
|
80 |
} |
|
pred@2009
|
81 |
|
|
pred@2009
|
82 |
void TQueryOp::RunThreadPart() |
|
pred@2009
|
83 |
{ |
|
pred@2207
|
84 |
if (m_type == Query_Connect) |
|
pred@2207
|
85 |
{ |
|
pred@2207
|
86 |
g_ClientPrefs.DatabaseConnect(); |
|
pred@2207
|
87 |
} |
|
pred@2207
|
88 |
else |
|
pred@2009
|
89 |
{ |
|
pred@2207
|
90 |
if (m_database == NULL) |
|
pred@2207
|
91 |
{ |
|
pred@2207
|
92 |
return; |
|
pred@2207
|
93 |
} |
|
pred@2207
|
94 |
|
|
pred@2207
|
95 |
m_database->LockForFullAtomicOperation(); |
|
pred@2009
|
96 |
|
|
pred@2207
|
97 |
if (!BindParamsAndRun()) |
|
pred@2207
|
98 |
{ |
|
pred@2207
|
99 |
g_pSM->LogError(myself, "Failed SQL Query, Error: \"%s\" (Query id %i - client %i)", m_database->GetError(), m_type, m_client); |
|
pred@2207
|
100 |
} |
|
pred@2009
|
101 |
|
|
pred@2207
|
102 |
m_insertId = m_database->GetInsertID(); |
|
pred@2207
|
103 |
|
|
pred@2207
|
104 |
m_database->UnlockFromFullAtomicOperation(); |
|
pred@2207
|
105 |
} |
|
pred@2009
|
106 |
} |
|
pred@2009
|
107 |
|
|
pred@2009
|
108 |
IDBDriver *TQueryOp::GetDriver() |
|
pred@2009
|
109 |
{ |
|
pred@2207
|
110 |
if (m_database == NULL) |
|
pred@2207
|
111 |
{ |
|
pred@2207
|
112 |
return NULL; |
|
pred@2207
|
113 |
} |
|
pred@2207
|
114 |
|
|
pred@2207
|
115 |
return m_database->GetDriver(); |
|
pred@2009
|
116 |
} |
|
pred@2009
|
117 |
IdentityToken_t *TQueryOp::GetOwner() |
|
pred@2009
|
118 |
{ |
|
pred@2009
|
119 |
return myself->GetIdentity(); |
|
pred@2009
|
120 |
} |
|
pred@2009
|
121 |
void TQueryOp::Destroy() |
|
pred@2009
|
122 |
{ |
|
pred@2009
|
123 |
delete this; |
|
pred@2009
|
124 |
} |
|
pred@2009
|
125 |
|
|
pred@2207
|
126 |
TQueryOp::TQueryOp(enum querytype type, int client) |
|
pred@2009
|
127 |
{ |
|
pred@2009
|
128 |
m_type = type; |
|
pred@2009
|
129 |
m_client = client; |
|
pred@2131
|
130 |
m_pQuery = NULL; |
|
pred@2207
|
131 |
m_database = NULL; |
|
pred@2207
|
132 |
} |
|
pred@2009
|
133 |
|
|
pred@2207
|
134 |
TQueryOp::TQueryOp(enum querytype type, Cookie *cookie) |
|
pred@2207
|
135 |
{ |
|
pred@2207
|
136 |
m_type = type; |
|
pred@2207
|
137 |
m_pCookie = cookie; |
|
pred@2207
|
138 |
m_pQuery = NULL; |
|
pred@2207
|
139 |
m_database = NULL; |
|
pred@2207
|
140 |
} |
|
pred@2207
|
141 |
|
|
pred@2207
|
142 |
void TQueryOp::SetDatabase( IDatabase *db ) |
|
pred@2207
|
143 |
{ |
|
pred@2207
|
144 |
m_database = db; |
|
pred@2207
|
145 |
m_database->IncReferenceCount(); |
|
pred@2009
|
146 |
} |
|
pred@2009
|
147 |
|
|
pred@2207
|
148 |
bool TQueryOp::BindParamsAndRun() |
|
pred@2207
|
149 |
{ |
|
pred@2207
|
150 |
if (m_pQuery == NULL) |
|
pred@2207
|
151 |
{ |
|
pred@2207
|
152 |
return false; |
|
pred@2207
|
153 |
} |
|
pred@2207
|
154 |
|
|
pred@2207
|
155 |
switch (m_type) |
|
pred@2207
|
156 |
{ |
|
pred@2207
|
157 |
case Query_InsertCookie: |
|
pred@2207
|
158 |
{ |
|
pred@2207
|
159 |
m_pQuery->BindParamString(0, m_params.cookie->name, false); |
|
pred@2207
|
160 |
m_pQuery->BindParamString(1, m_params.cookie->description, false); |
|
pred@2207
|
161 |
m_pQuery->BindParamInt(2, m_params.cookie->access); |
|
pred@2207
|
162 |
|
|
pred@2207
|
163 |
break; |
|
pred@2207
|
164 |
} |
|
pred@2207
|
165 |
|
|
pred@2207
|
166 |
case Query_SelectData: |
|
pred@2207
|
167 |
{ |
|
pred@2207
|
168 |
m_pQuery->BindParamString(0, m_params.steamId, false); |
|
pred@2207
|
169 |
|
|
pred@2207
|
170 |
break; |
|
pred@2207
|
171 |
} |
|
pred@2207
|
172 |
|
|
pred@2207
|
173 |
case Query_InsertData: |
|
pred@2207
|
174 |
{ |
|
pred@2207
|
175 |
m_pQuery->BindParamString(0, m_params.steamId, false); |
|
pred@2207
|
176 |
m_pQuery->BindParamInt(1, m_params.cookieId); |
|
pred@2207
|
177 |
m_pQuery->BindParamString(2, m_params.data->value, false); |
|
pred@2207
|
178 |
m_pQuery->BindParamInt(3, (unsigned int)time(NULL), false); |
|
pred@2207
|
179 |
|
|
pred@2207
|
180 |
if (driver == DRIVER_MYSQL) |
|
pred@2207
|
181 |
{ |
|
pred@2207
|
182 |
m_pQuery->BindParamString(4, m_params.data->value, false); |
|
pred@2207
|
183 |
m_pQuery->BindParamInt(5, (unsigned int)time(NULL), false); |
|
pred@2207
|
184 |
} |
|
pred@2207
|
185 |
|
|
pred@2207
|
186 |
break; |
|
pred@2207
|
187 |
} |
|
pred@2207
|
188 |
|
|
pred@2207
|
189 |
case Query_SelectId: |
|
pred@2207
|
190 |
{ |
|
pred@2207
|
191 |
/* the steamId var was actually used to store the name of the cookie - Save duplicating vars */ |
|
pred@2207
|
192 |
m_pQuery->BindParamString(0, m_params.steamId, false); |
|
pred@2207
|
193 |
|
|
pred@2207
|
194 |
break; |
|
pred@2207
|
195 |
} |
|
pred@2207
|
196 |
|
|
pred@2207
|
197 |
default: |
|
pred@2207
|
198 |
{ |
|
pred@2207
|
199 |
break; |
|
pred@2207
|
200 |
} |
|
pred@2207
|
201 |
|
|
pred@2207
|
202 |
} |
|
pred@2207
|
203 |
|
|
pred@2207
|
204 |
return m_pQuery->Execute(); |
|
pred@2207
|
205 |
} |
|
pred@2207
|
206 |
|
|
pred@2207
|
207 |
void TQueryOp::SetPreparedQuery() |
|
pred@2009
|
208 |
{ |
|
pred@2207
|
209 |
switch (m_type) |
|
pred@2207
|
210 |
{ |
|
pred@2207
|
211 |
case Query_InsertCookie: |
|
pred@2207
|
212 |
{ |
|
pred@2207
|
213 |
m_pQuery = g_ClientPrefs.InsertCookieQuery; |
|
pred@2207
|
214 |
break; |
|
pred@2207
|
215 |
} |
|
pred@2207
|
216 |
|
|
pred@2207
|
217 |
case Query_SelectData: |
|
pred@2207
|
218 |
{ |
|
pred@2207
|
219 |
m_pQuery = g_ClientPrefs.SelectDataQuery; |
|
pred@2207
|
220 |
break; |
|
pred@2207
|
221 |
} |
|
pred@2207
|
222 |
|
|
pred@2207
|
223 |
case Query_InsertData: |
|
pred@2207
|
224 |
{ |
|
pred@2207
|
225 |
m_pQuery = g_ClientPrefs.InsertDataQuery; |
|
pred@2207
|
226 |
break; |
|
pred@2207
|
227 |
} |
|
pred@2207
|
228 |
|
|
pred@2207
|
229 |
case Query_SelectId: |
|
pred@2207
|
230 |
{ |
|
pred@2207
|
231 |
m_pQuery = g_ClientPrefs.SelectIdQuery; |
|
pred@2207
|
232 |
break; |
|
pred@2207
|
233 |
} |
|
pred@2207
|
234 |
|
|
pred@2207
|
235 |
default: |
|
pred@2207
|
236 |
{ |
|
pred@2207
|
237 |
break; |
|
pred@2207
|
238 |
} |
|
pred@2207
|
239 |
|
|
pred@2207
|
240 |
} |
|
pred@2207
|
241 |
} |
|
pred@2009
|
242 |
|
|
pred@2207
|
243 |
void TQueryOp::SetCustomPreparedQuery(IPreparedQuery *query) |
|
pred@2207
|
244 |
{ |
|
pred@2207
|
245 |
m_pQuery = query; |
|
pred@2009
|
246 |
} |
|
pred@2207
|
247 |
|
|
pred@2207
|
248 |
ParamData::~ParamData() |
|
pred@2207
|
249 |
{ |
|
pred@2207
|
250 |
if (cookie) |
|
pred@2207
|
251 |
{ |
|
pred@2207
|
252 |
g_ClientPrefs.cookieMutex->Lock(); |
|
pred@2207
|
253 |
cookie->usedInQuery--; |
|
pred@2207
|
254 |
|
|
pred@2207
|
255 |
if (cookie->shouldDelete && cookie->usedInQuery <= 0) |
|
pred@2207
|
256 |
{ |
|
pred@2207
|
257 |
g_ClientPrefs.cookieMutex->Unlock(); |
|
pred@2207
|
258 |
delete cookie; |
|
pred@2207
|
259 |
cookie = NULL; |
|
pred@2207
|
260 |
} |
|
pred@2207
|
261 |
|
|
pred@2207
|
262 |
g_ClientPrefs.cookieMutex->Unlock(); |
|
pred@2207
|
263 |
} |
|
pred@2207
|
264 |
|
|
pred@2207
|
265 |
if (data) |
|
pred@2207
|
266 |
{ |
|
pred@2207
|
267 |
/* Data is only ever passed in a client disconnect query and always needs to be deleted */ |
|
pred@2207
|
268 |
delete data; |
|
pred@2207
|
269 |
data = NULL; |
|
pred@2207
|
270 |
} |
|
pred@2207
|
271 |
} |
|
pred@2207
|
272 |
|
|
pred@2207
|
273 |
ParamData::ParamData() |
|
pred@2207
|
274 |
{ |
|
pred@2207
|
275 |
cookie = NULL; |
|
pred@2207
|
276 |
data = NULL; |
|
pred@2207
|
277 |
steamId[0] = '\0'; |
|
pred@2207
|
278 |
cookieId = 0; |
|
pred@2208
|
279 |
} |