extensions/clientprefs/query.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4039 e8b8d660b50c
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Client Preferences Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "query.h"
    33 
    34 
    35 void TQueryOp::RunThinkPart()
    36 {
    37 	//handler for threaded sql queries
    38 	switch (m_type)
    39 	{
    40 		case Query_InsertCookie:
    41 		{
    42 			g_CookieManager.InsertCookieCallback(m_pCookie, m_insertId);
    43 			break;
    44 		}
    45 
    46 		case Query_SelectData:
    47 		{
    48 			g_CookieManager.ClientConnectCallback(m_serial, m_pResult);
    49 			break;
    50 		}
    51 
    52 		case Query_SelectId:
    53 		{
    54 			g_CookieManager.SelectIdCallback(m_pCookie, m_pResult);
    55 			break;
    56 		}
    57 
    58 		case Query_Connect:
    59 		{
    60 			return;
    61 		}
    62 		
    63 		default:
    64 		{
    65 			break;
    66 		}
    67 	}
    68 }
    69 
    70 void TQueryOp::RunThreadPart()
    71 {
    72 	if (m_type == Query_Connect)
    73 	{
    74 		g_ClientPrefs.DatabaseConnect();
    75 		return;
    76 	}
    77 	
    78 	assert(m_database != NULL);
    79 	/* I don't think this is needed anymore... keeping for now. */
    80 	m_database->LockForFullAtomicOperation();
    81 		if (!BindParamsAndRun())
    82 	{
    83 		g_pSM->LogError(myself, 
    84 						"Failed SQL Query, Error: \"%s\" (Query id %i - serial %i)", 
    85 						m_database->GetError(),
    86 						m_type, 
    87 						m_serial);
    88 	}
    89 
    90 	m_database->UnlockFromFullAtomicOperation();
    91 }
    92 
    93 IDBDriver *TQueryOp::GetDriver()
    94 {
    95 	return m_database->GetDriver();
    96 }
    97 
    98 IdentityToken_t *TQueryOp::GetOwner()
    99 {
   100 	return myself->GetIdentity();
   101 }
   102 
   103 void TQueryOp::Destroy()
   104 {
   105 	if (m_pResult != NULL)
   106 	{
   107 		m_pResult->Destroy();
   108 	}
   109 	delete this;
   110 }
   111 
   112 TQueryOp::TQueryOp(enum querytype type, int serial)
   113 {
   114 	m_type = type;
   115 	m_serial = serial;
   116 	m_database = NULL;
   117 	m_insertId = -1;
   118 	m_pResult = NULL;
   119 }
   120 
   121 TQueryOp::TQueryOp(enum querytype type, Cookie *cookie)
   122 {
   123 	m_type = type;
   124 	m_pCookie = cookie;
   125 	m_database = NULL;
   126 	m_insertId = -1;
   127 	m_pResult = NULL;
   128 	m_serial = 0;
   129 }
   130 
   131 void TQueryOp::SetDatabase(IDatabase *db)
   132 {
   133 	m_database = db;
   134 }
   135 
   136 bool TQueryOp::BindParamsAndRun()
   137 {
   138 	size_t ignore;
   139 	char query[2048];
   140 
   141 	switch (m_type)
   142 	{
   143 		case Query_InsertCookie:
   144 		{
   145 			char safe_name[MAX_NAME_LENGTH*2 + 1];
   146 			char safe_desc[MAX_DESC_LENGTH*2 + 1];
   147 			
   148 			m_database->QuoteString(m_params.cookie->name, 
   149 				safe_name, 
   150 				sizeof(safe_name),
   151 				&ignore);
   152 			m_database->QuoteString(m_params.cookie->description,
   153 				safe_desc,
   154 				sizeof(safe_desc),
   155 				&ignore);
   156 
   157 			if (g_DriverType == Driver_MySQL)
   158 			{
   159 				g_pSM->Format(query,
   160 					sizeof(query),
   161 					"INSERT IGNORE INTO sm_cookies (name, description, access) \
   162 					 VALUES (\"%s\", \"%s\", %d)",
   163 					safe_name,
   164 					safe_desc,
   165 					m_params.cookie->access);
   166 			}
   167 			else if (g_DriverType == Driver_SQLite)
   168 			{
   169 				g_pSM->Format(query,
   170 					sizeof(query),
   171 					"INSERT OR IGNORE INTO sm_cookies (name, description, access) \
   172 					 VALUES ('%s', '%s', %d)",
   173 					 safe_name,
   174 					 safe_desc,
   175 					 m_params.cookie->access);
   176 			}
   177 
   178 			if (!m_database->DoSimpleQuery(query))
   179 			{
   180 				return false;
   181 			}
   182 
   183 			m_insertId = m_database->GetInsertID();
   184 
   185 			return true;
   186 		}
   187 
   188 		case Query_SelectData:
   189 		{
   190 			char safe_str[128];
   191 
   192 			m_database->QuoteString(m_params.steamId, safe_str, sizeof(safe_str), &ignore);
   193 
   194 			g_pSM->Format(query,
   195 				sizeof(query),
   196 				"SELECT sm_cookies.name, sm_cookie_cache.value, sm_cookies.description, \
   197 						sm_cookies.access, sm_cookie_cache.timestamp 	\
   198 				FROM sm_cookies				\
   199 				JOIN sm_cookie_cache		\
   200 				ON sm_cookies.id = sm_cookie_cache.cookie_id \
   201 				WHERE player = '%s'",
   202 				safe_str);
   203 
   204 			m_pResult = m_database->DoQuery(query);
   205 
   206 			return (m_pResult != NULL);
   207 		}
   208 
   209 		case Query_InsertData:
   210 		{
   211 			char safe_id[128];
   212 			char safe_val[MAX_VALUE_LENGTH*2 + 1];
   213 
   214 			m_database->QuoteString(m_params.steamId,
   215 				safe_id,
   216 				sizeof(safe_id),
   217 				&ignore);
   218 			m_database->QuoteString(m_params.data->value,
   219 				safe_val,
   220 				sizeof(safe_val),
   221 				&ignore);
   222 
   223 			if (g_DriverType == Driver_MySQL)
   224 			{
   225 				g_pSM->Format(query, 
   226 					sizeof(query),
   227 					"INSERT INTO sm_cookie_cache (player, cookie_id, value, timestamp) 	\
   228 					 VALUES (\"%s\", %d, \"%s\", %d)									\
   229 					 ON DUPLICATE KEY UPDATE											\
   230 					 value = \"%s\", timestamp = %d",
   231 					safe_id,
   232 					m_params.cookieId,
   233 					safe_val,
   234 					(unsigned int)m_params.data->timestamp,
   235 					safe_val,
   236 					(unsigned int)m_params.data->timestamp);
   237 			}
   238 			else if (g_DriverType == Driver_SQLite)
   239 			{
   240 				g_pSM->Format(query,
   241 					sizeof(query),
   242 					"INSERT OR REPLACE INTO sm_cookie_cache						\
   243 					 (player, cookie_id, value, timestamp)						\
   244 					 VALUES ('%s', %d, '%s', %d)",
   245 					safe_id,
   246 					m_params.cookieId,
   247 					safe_val,
   248 					(unsigned int)m_params.data->timestamp);
   249 			}
   250 
   251 			if (!m_database->DoSimpleQuery(query))
   252 			{
   253 				return false;
   254 			}
   255 
   256 			m_insertId = m_database->GetInsertID();
   257 
   258 			return true;
   259 		}
   260 
   261 		case Query_SelectId:
   262 		{
   263 			char safe_name[MAX_NAME_LENGTH*2 + 1];
   264 
   265 			/* the steamId var was actually used to store the name of the cookie - Save duplicating vars */
   266 			m_database->QuoteString(m_params.steamId, 
   267 				safe_name,
   268 				sizeof(safe_name),
   269 				&ignore);
   270 
   271 			g_pSM->Format(query, 
   272 				sizeof(query),
   273 				"SELECT id FROM sm_cookies WHERE name = '%s'",
   274 				safe_name);
   275 
   276 			m_pResult = m_database->DoQuery(query);
   277 
   278 			return (m_pResult != NULL);
   279 		}
   280 
   281 		default:
   282 		{
   283 			break;
   284 		}
   285 			
   286 	}
   287 
   288 	assert(false);
   289 
   290 	return false;
   291 }
   292 
   293 querytype TQueryOp::PullQueryType()
   294 {
   295 	return m_type;
   296 }
   297 
   298 int TQueryOp::PullQuerySerial()
   299 {
   300 	return m_serial;
   301 }
   302 
   303 ParamData::~ParamData()
   304 {
   305 	if (data)
   306 	{
   307 		/* Data is only ever passed in a client disconnect query and always needs to be deleted */
   308 		delete data;
   309 		data = NULL;
   310 	}
   311 }
   312 
   313 ParamData::ParamData()
   314 {
   315 	cookie = NULL;
   316 	data = NULL;
   317 	steamId[0] = '\0';
   318 	cookieId = 0;
   319 }
   320