3 * =============================================================================
4 * SourceMod Client Preferences Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
35 void TQueryOp::RunThinkPart()
37 //handler for threaded sql queries
39 if (m_type == Query_Connect)
46 case Query_InsertCookie:
48 g_CookieManager.InsertCookieCallback(m_pCookie, m_insertId);
52 case Query_SelectData:
54 g_CookieManager.ClientConnectCallback(m_serial, m_pResult);
60 g_CookieManager.SelectIdCallback(m_pCookie, m_pResult);
71 void TQueryOp::RunThreadPart()
73 if (m_type == Query_Connect)
75 g_ClientPrefs.DatabaseConnect();
79 assert(m_database != NULL);
81 /* I don't think this is needed anymore... keeping for now. */
82 m_database->LockForFullAtomicOperation();
84 if (!BindParamsAndRun())
86 g_pSM->LogError(myself,
87 "Failed SQL Query, Error: \"%s\" (Query id %i - serial %i)",
88 m_database->GetError(),
93 m_database->UnlockFromFullAtomicOperation();
97 IDBDriver *TQueryOp::GetDriver()
99 assert(m_database != NULL);
101 return m_database->GetDriver();
104 IdentityToken_t *TQueryOp::GetOwner()
106 return myself->GetIdentity();
109 void TQueryOp::Destroy()
111 if (m_pResult != NULL)
113 m_pResult->Destroy();
118 TQueryOp::TQueryOp(enum querytype type, int serial)
127 TQueryOp::TQueryOp(enum querytype type, Cookie *cookie)
136 void TQueryOp::SetDatabase(IDatabase *db)
139 m_database->IncReferenceCount();
142 bool TQueryOp::BindParamsAndRun()
149 case Query_InsertCookie:
151 char safe_name[MAX_NAME_LENGTH*2 + 1];
152 char safe_desc[MAX_DESC_LENGTH*2 + 1];
154 m_database->QuoteString(m_params.cookie->name,
158 m_database->QuoteString(m_params.cookie->description,
163 if (g_DriverType == Driver_MySQL)
167 "INSERT IGNORE INTO sm_cookies (name, description, access) \
168 VALUES (\"%s\", \"%s\", %d)",
171 m_params.cookie->access);
173 else if (g_DriverType == Driver_SQLite)
177 "INSERT OR IGNORE INTO sm_cookies (name, description, access) \
178 VALUES ('%s', '%s', %d)",
181 m_params.cookie->access);
184 if (!m_database->DoSimpleQuery(query))
189 m_insertId = m_database->GetInsertID();
194 case Query_SelectData:
198 m_database->QuoteString(m_params.steamId, safe_str, sizeof(safe_str), &ignore);
202 "SELECT sm_cookies.name, sm_cookie_cache.value, sm_cookies.description, \
203 sm_cookies.access, sm_cookie_cache.timestamp \
205 JOIN sm_cookie_cache \
206 ON sm_cookies.id = sm_cookie_cache.cookie_id \
207 WHERE player = '%s'",
210 m_pResult = m_database->DoQuery(query);
212 return (m_pResult != NULL);
215 case Query_InsertData:
218 char safe_val[MAX_VALUE_LENGTH*2 + 1];
220 m_database->QuoteString(m_params.steamId,
224 m_database->QuoteString(m_params.data->value,
229 if (g_DriverType == Driver_MySQL)
233 "INSERT INTO sm_cookie_cache (player, cookie_id, value, timestamp) \
234 VALUES (\"%s\", %d, \"%s\", %d) \
235 ON DUPLICATE KEY UPDATE \
236 value = \"%s\", timestamp = %d",
240 (unsigned int)m_params.data->timestamp,
242 (unsigned int)m_params.data->timestamp);
244 else if (g_DriverType == Driver_SQLite)
248 "INSERT OR REPLACE INTO sm_cookie_cache \
249 (player, cookie_id, value, timestamp) \
250 VALUES ('%s', %d, '%s', %d)",
254 (unsigned int)m_params.data->timestamp);
257 if (!m_database->DoSimpleQuery(query))
262 m_insertId = m_database->GetInsertID();
269 char safe_name[MAX_NAME_LENGTH*2 + 1];
271 /* the steamId var was actually used to store the name of the cookie - Save duplicating vars */
272 m_database->QuoteString(m_params.steamId,
279 "SELECT id FROM sm_cookies WHERE name = '%s'",
282 m_pResult = m_database->DoQuery(query);
284 return (m_pResult != NULL);
299 ParamData::~ParamData()
303 g_ClientPrefs.cookieMutex->Lock();
304 cookie->usedInQuery--;
306 if (cookie->shouldDelete && cookie->usedInQuery <= 0)
308 g_ClientPrefs.cookieMutex->Unlock();
313 g_ClientPrefs.cookieMutex->Unlock();
318 /* Data is only ever passed in a client disconnect query and always needs to be deleted */
324 ParamData::ParamData()