2008-04-17 06:58:09 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* =============================================================================
|
|
|
|
* SourceMod Client Preferences Extension
|
|
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "query.h"
|
|
|
|
|
|
|
|
|
|
|
|
void TQueryOp::RunThinkPart()
|
|
|
|
{
|
|
|
|
//handler for threaded sql queries
|
2008-10-20 10:40:46 +02:00
|
|
|
switch (m_type)
|
2008-04-17 06:58:09 +02:00
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
case Query_InsertCookie:
|
2008-04-17 06:58:09 +02:00
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
g_CookieManager.InsertCookieCallback(m_pCookie, m_insertId);
|
|
|
|
break;
|
|
|
|
}
|
2008-05-12 10:06:47 +02:00
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
case Query_SelectData:
|
|
|
|
{
|
2009-02-19 07:28:50 +01:00
|
|
|
g_CookieManager.ClientConnectCallback(m_serial, m_pResult);
|
2008-10-20 10:40:46 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
case Query_SelectId:
|
|
|
|
{
|
|
|
|
g_CookieManager.SelectIdCallback(m_pCookie, m_pResult);
|
|
|
|
break;
|
|
|
|
}
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2013-02-28 22:51:49 +01:00
|
|
|
case Query_Connect:
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
2008-04-17 06:58:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TQueryOp::RunThreadPart()
|
|
|
|
{
|
2008-07-15 02:24:08 +02:00
|
|
|
if (m_type == Query_Connect)
|
2008-04-17 06:58:09 +02:00
|
|
|
{
|
2008-07-15 02:24:08 +02:00
|
|
|
g_ClientPrefs.DatabaseConnect();
|
2013-02-28 22:51:49 +01:00
|
|
|
return;
|
2008-04-17 06:58:09 +02:00
|
|
|
}
|
2013-02-28 22:51:49 +01:00
|
|
|
|
|
|
|
assert(m_database != NULL);
|
|
|
|
/* I don't think this is needed anymore... keeping for now. */
|
|
|
|
m_database->LockForFullAtomicOperation();
|
2008-07-15 02:24:08 +02:00
|
|
|
if (!BindParamsAndRun())
|
2013-02-28 22:51:49 +01:00
|
|
|
{
|
|
|
|
g_pSM->LogError(myself,
|
|
|
|
"Failed SQL Query, Error: \"%s\" (Query id %i - serial %i)",
|
|
|
|
m_database->GetError(),
|
|
|
|
m_type,
|
|
|
|
m_serial);
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
2013-02-28 22:51:49 +01:00
|
|
|
|
|
|
|
m_database->UnlockFromFullAtomicOperation();
|
2008-04-17 06:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IDBDriver *TQueryOp::GetDriver()
|
|
|
|
{
|
2016-10-03 17:31:17 +02:00
|
|
|
return m_driver;
|
2008-04-17 06:58:09 +02:00
|
|
|
}
|
2008-10-20 10:40:46 +02:00
|
|
|
|
2008-04-17 06:58:09 +02:00
|
|
|
IdentityToken_t *TQueryOp::GetOwner()
|
|
|
|
{
|
|
|
|
return myself->GetIdentity();
|
|
|
|
}
|
2008-08-05 10:00:54 +02:00
|
|
|
|
2008-04-17 06:58:09 +02:00
|
|
|
void TQueryOp::Destroy()
|
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
if (m_pResult != NULL)
|
|
|
|
{
|
|
|
|
m_pResult->Destroy();
|
|
|
|
}
|
2008-04-17 06:58:09 +02:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2009-02-19 07:28:50 +01:00
|
|
|
TQueryOp::TQueryOp(enum querytype type, int serial)
|
2008-04-17 06:58:09 +02:00
|
|
|
{
|
|
|
|
m_type = type;
|
2009-02-19 07:28:50 +01:00
|
|
|
m_serial = serial;
|
2008-07-15 02:24:08 +02:00
|
|
|
m_database = NULL;
|
2016-10-03 17:31:17 +02:00
|
|
|
m_driver = NULL;
|
2008-10-20 10:40:46 +02:00
|
|
|
m_insertId = -1;
|
|
|
|
m_pResult = NULL;
|
2008-04-17 06:58:09 +02:00
|
|
|
}
|
|
|
|
|
2008-07-15 02:24:08 +02:00
|
|
|
TQueryOp::TQueryOp(enum querytype type, Cookie *cookie)
|
2008-04-17 06:58:09 +02:00
|
|
|
{
|
|
|
|
m_type = type;
|
2008-07-15 02:24:08 +02:00
|
|
|
m_pCookie = cookie;
|
|
|
|
m_database = NULL;
|
2016-10-03 17:31:17 +02:00
|
|
|
m_driver = NULL;
|
2008-10-20 10:40:46 +02:00
|
|
|
m_insertId = -1;
|
|
|
|
m_pResult = NULL;
|
2013-02-28 22:51:49 +01:00
|
|
|
m_serial = 0;
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
void TQueryOp::SetDatabase(IDatabase *db)
|
2008-07-15 02:24:08 +02:00
|
|
|
{
|
|
|
|
m_database = db;
|
2016-10-03 17:31:17 +02:00
|
|
|
m_driver = m_database->GetDriver();
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TQueryOp::BindParamsAndRun()
|
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
size_t ignore;
|
|
|
|
char query[2048];
|
2008-07-15 02:24:08 +02:00
|
|
|
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case Query_InsertCookie:
|
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
char safe_name[MAX_NAME_LENGTH*2 + 1];
|
|
|
|
char safe_desc[MAX_DESC_LENGTH*2 + 1];
|
|
|
|
|
|
|
|
m_database->QuoteString(m_params.cookie->name,
|
|
|
|
safe_name,
|
|
|
|
sizeof(safe_name),
|
|
|
|
&ignore);
|
|
|
|
m_database->QuoteString(m_params.cookie->description,
|
|
|
|
safe_desc,
|
|
|
|
sizeof(safe_desc),
|
|
|
|
&ignore);
|
|
|
|
|
|
|
|
if (g_DriverType == Driver_MySQL)
|
|
|
|
{
|
2013-02-28 22:51:49 +01:00
|
|
|
g_pSM->Format(query,
|
2008-10-20 10:40:46 +02:00
|
|
|
sizeof(query),
|
|
|
|
"INSERT IGNORE INTO sm_cookies (name, description, access) \
|
|
|
|
VALUES (\"%s\", \"%s\", %d)",
|
|
|
|
safe_name,
|
|
|
|
safe_desc,
|
|
|
|
m_params.cookie->access);
|
|
|
|
}
|
|
|
|
else if (g_DriverType == Driver_SQLite)
|
|
|
|
{
|
2013-02-28 22:51:49 +01:00
|
|
|
g_pSM->Format(query,
|
2008-10-20 10:40:46 +02:00
|
|
|
sizeof(query),
|
|
|
|
"INSERT OR IGNORE INTO sm_cookies (name, description, access) \
|
2009-08-11 04:16:58 +02:00
|
|
|
VALUES ('%s', '%s', %d)",
|
2008-10-20 10:40:46 +02:00
|
|
|
safe_name,
|
|
|
|
safe_desc,
|
|
|
|
m_params.cookie->access);
|
|
|
|
}
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
if (!m_database->DoSimpleQuery(query))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_insertId = m_database->GetInsertID();
|
|
|
|
|
|
|
|
return true;
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case Query_SelectData:
|
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
char safe_str[128];
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
m_database->QuoteString(m_params.steamId, safe_str, sizeof(safe_str), &ignore);
|
|
|
|
|
2013-02-28 22:51:49 +01:00
|
|
|
g_pSM->Format(query,
|
2008-10-20 10:40:46 +02:00
|
|
|
sizeof(query),
|
|
|
|
"SELECT sm_cookies.name, sm_cookie_cache.value, sm_cookies.description, \
|
2009-03-17 00:30:21 +01:00
|
|
|
sm_cookies.access, sm_cookie_cache.timestamp \
|
2008-10-20 10:40:46 +02:00
|
|
|
FROM sm_cookies \
|
|
|
|
JOIN sm_cookie_cache \
|
|
|
|
ON sm_cookies.id = sm_cookie_cache.cookie_id \
|
2009-08-11 04:16:58 +02:00
|
|
|
WHERE player = '%s'",
|
2008-10-20 10:40:46 +02:00
|
|
|
safe_str);
|
|
|
|
|
|
|
|
m_pResult = m_database->DoQuery(query);
|
|
|
|
|
|
|
|
return (m_pResult != NULL);
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case Query_InsertData:
|
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
char safe_id[128];
|
|
|
|
char safe_val[MAX_VALUE_LENGTH*2 + 1];
|
|
|
|
|
|
|
|
m_database->QuoteString(m_params.steamId,
|
|
|
|
safe_id,
|
|
|
|
sizeof(safe_id),
|
|
|
|
&ignore);
|
|
|
|
m_database->QuoteString(m_params.data->value,
|
|
|
|
safe_val,
|
|
|
|
sizeof(safe_val),
|
|
|
|
&ignore);
|
|
|
|
|
|
|
|
if (g_DriverType == Driver_MySQL)
|
|
|
|
{
|
2013-02-28 22:51:49 +01:00
|
|
|
g_pSM->Format(query,
|
2008-10-20 10:40:46 +02:00
|
|
|
sizeof(query),
|
|
|
|
"INSERT INTO sm_cookie_cache (player, cookie_id, value, timestamp) \
|
|
|
|
VALUES (\"%s\", %d, \"%s\", %d) \
|
|
|
|
ON DUPLICATE KEY UPDATE \
|
|
|
|
value = \"%s\", timestamp = %d",
|
|
|
|
safe_id,
|
|
|
|
m_params.cookieId,
|
|
|
|
safe_val,
|
2009-03-17 00:30:21 +01:00
|
|
|
(unsigned int)m_params.data->timestamp,
|
2008-10-20 10:40:46 +02:00
|
|
|
safe_val,
|
2009-03-17 00:30:21 +01:00
|
|
|
(unsigned int)m_params.data->timestamp);
|
2008-10-20 10:40:46 +02:00
|
|
|
}
|
|
|
|
else if (g_DriverType == Driver_SQLite)
|
|
|
|
{
|
2013-02-28 22:51:49 +01:00
|
|
|
g_pSM->Format(query,
|
2008-10-20 10:40:46 +02:00
|
|
|
sizeof(query),
|
|
|
|
"INSERT OR REPLACE INTO sm_cookie_cache \
|
|
|
|
(player, cookie_id, value, timestamp) \
|
2009-08-11 04:16:58 +02:00
|
|
|
VALUES ('%s', %d, '%s', %d)",
|
2008-10-20 10:40:46 +02:00
|
|
|
safe_id,
|
|
|
|
m_params.cookieId,
|
|
|
|
safe_val,
|
2009-03-17 00:30:21 +01:00
|
|
|
(unsigned int)m_params.data->timestamp);
|
2008-10-20 10:40:46 +02:00
|
|
|
}
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
if (!m_database->DoSimpleQuery(query))
|
2008-07-15 02:24:08 +02:00
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
return false;
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
m_insertId = m_database->GetInsertID();
|
|
|
|
|
|
|
|
return true;
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case Query_SelectId:
|
|
|
|
{
|
2008-10-20 10:40:46 +02:00
|
|
|
char safe_name[MAX_NAME_LENGTH*2 + 1];
|
|
|
|
|
2008-07-15 02:24:08 +02:00
|
|
|
/* the steamId var was actually used to store the name of the cookie - Save duplicating vars */
|
2008-10-20 10:40:46 +02:00
|
|
|
m_database->QuoteString(m_params.steamId,
|
|
|
|
safe_name,
|
|
|
|
sizeof(safe_name),
|
|
|
|
&ignore);
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2013-02-28 22:51:49 +01:00
|
|
|
g_pSM->Format(query,
|
2008-10-20 10:40:46 +02:00
|
|
|
sizeof(query),
|
2009-08-11 04:16:58 +02:00
|
|
|
"SELECT id FROM sm_cookies WHERE name = '%s'",
|
2008-10-20 10:40:46 +02:00
|
|
|
safe_name);
|
|
|
|
|
|
|
|
m_pResult = m_database->DoQuery(query);
|
|
|
|
|
|
|
|
return (m_pResult != NULL);
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
2008-04-17 06:58:09 +02:00
|
|
|
|
2008-07-15 02:24:08 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
assert(false);
|
2008-07-15 02:24:08 +02:00
|
|
|
|
2008-10-20 10:40:46 +02:00
|
|
|
return false;
|
2008-07-15 02:24:08 +02:00
|
|
|
}
|
|
|
|
|
2013-02-28 22:51:49 +01:00
|
|
|
querytype TQueryOp::PullQueryType()
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TQueryOp::PullQuerySerial()
|
|
|
|
{
|
|
|
|
return m_serial;
|
|
|
|
}
|
|
|
|
|
2008-07-15 02:24:08 +02:00
|
|
|
ParamData::~ParamData()
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
/* Data is only ever passed in a client disconnect query and always needs to be deleted */
|
|
|
|
delete data;
|
|
|
|
data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamData::ParamData()
|
|
|
|
{
|
|
|
|
cookie = NULL;
|
|
|
|
data = NULL;
|
|
|
|
steamId[0] = '\0';
|
|
|
|
cookieId = 0;
|
2008-07-15 04:26:22 +02:00
|
|
|
}
|
2008-10-20 10:40:46 +02:00
|
|
|
|