extensions/clientprefs/query.cpp
author Matt Woodrow <pred@alliedmods.net>
Tue Jul 15 00:24:08 2008 +0000 (2008-07-15)
changeset 2207 3a97a56ded62
parent 2131 cbad21fc7626
child 2208 babfbc636787
permissions -rw-r--r--
Fixed amb1810 - Clientprefs no longer blocks load.
     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 
    39 	if (m_type == Query_Connect)
    40 	{
    41 		return;
    42 	}
    43 
    44 	if (m_pQuery)
    45 	{
    46 		switch (m_type)
    47 		{
    48 			case Query_InsertCookie:
    49 			{
    50 				g_CookieManager.InsertCookieCallback(m_pCookie, m_insertId);
    51 				break;
    52 			}
    53 
    54 			case Query_SelectData:
    55 			{
    56 				g_CookieManager.ClientConnectCallback(m_client, m_pQuery);
    57 				break;
    58 			}
    59 
    60 			case Query_SelectId:
    61 			{
    62 				g_CookieManager.SelectIdCallback(m_pCookie, m_pQuery);
    63 				break;
    64 			}
    65 
    66 			case Query_CreateTable:
    67 			{
    68 				m_pQuery->Destroy();
    69 				delete m_pQuery;
    70 				m_pQuery = NULL;
    71 				break;
    72 			}
    73 
    74 			default:
    75 			{
    76 				break;
    77 			}
    78 		}
    79 	}
    80 }
    81 
    82 void TQueryOp::RunThreadPart()
    83 {
    84 	if (m_type == Query_Connect)
    85 	{
    86 		g_ClientPrefs.DatabaseConnect();
    87 	}
    88 	else
    89 	{
    90 		if (m_database == NULL)
    91 		{
    92 			return;
    93 		}
    94 
    95 		m_database->LockForFullAtomicOperation();
    96 
    97 		if (!BindParamsAndRun())
    98 		{
    99 			g_pSM->LogError(myself, "Failed SQL Query, Error: \"%s\" (Query id %i - client %i)", m_database->GetError(), m_type, m_client);
   100 		}
   101 
   102 		m_insertId = m_database->GetInsertID();
   103 
   104 		m_database->UnlockFromFullAtomicOperation();
   105 	}
   106 }
   107 
   108 IDBDriver *TQueryOp::GetDriver()
   109 {
   110 	if (m_database == NULL)
   111 	{
   112 		return NULL;
   113 	}
   114 
   115 	return m_database->GetDriver();
   116 }
   117 IdentityToken_t *TQueryOp::GetOwner()
   118 {
   119 	return myself->GetIdentity();
   120 }
   121 void TQueryOp::Destroy()
   122 {
   123 	delete this;
   124 }
   125 
   126 TQueryOp::TQueryOp(enum querytype type, int client)
   127 {
   128 	m_type = type;
   129 	m_client = client;
   130 	m_pQuery = NULL;
   131 	m_database = NULL;
   132 }
   133 
   134 TQueryOp::TQueryOp(enum querytype type, Cookie *cookie)
   135 {
   136 	m_type = type;
   137 	m_pCookie = cookie;
   138 	m_pQuery = NULL;
   139 	m_database = NULL;
   140 }
   141 
   142 void TQueryOp::SetDatabase( IDatabase *db )
   143 {
   144 	m_database = db;
   145 	m_database->IncReferenceCount();
   146 }
   147 
   148 bool TQueryOp::BindParamsAndRun()
   149 {
   150 	if (m_pQuery == NULL)
   151 	{
   152 		return false;
   153 	}
   154 
   155 	switch (m_type)
   156 	{
   157 		case Query_InsertCookie:
   158 		{
   159 			m_pQuery->BindParamString(0, m_params.cookie->name, false);
   160 			m_pQuery->BindParamString(1, m_params.cookie->description, false);
   161 			m_pQuery->BindParamInt(2, m_params.cookie->access);
   162 
   163 			break;
   164 		}
   165 
   166 		case Query_SelectData:
   167 		{
   168 			m_pQuery->BindParamString(0, m_params.steamId, false);
   169 
   170 			break;
   171 		}
   172 
   173 		case Query_InsertData:
   174 		{
   175 			m_pQuery->BindParamString(0, m_params.steamId, false);
   176 			m_pQuery->BindParamInt(1, m_params.cookieId);
   177 			m_pQuery->BindParamString(2, m_params.data->value, false);
   178 			m_pQuery->BindParamInt(3, (unsigned int)time(NULL), false);
   179 
   180 			if (driver == DRIVER_MYSQL)
   181 			{
   182 				m_pQuery->BindParamString(4, m_params.data->value, false);
   183 				m_pQuery->BindParamInt(5, (unsigned int)time(NULL), false);
   184 			}
   185 
   186 			break;
   187 		}
   188 
   189 		case Query_SelectId:
   190 		{
   191 			/* the steamId var was actually used to store the name of the cookie - Save duplicating vars */
   192 			m_pQuery->BindParamString(0, m_params.steamId, false);
   193 
   194 			break;
   195 		}
   196 
   197 		default:
   198 		{
   199 			break;
   200 		}
   201 			
   202 	}
   203 
   204 	return m_pQuery->Execute();
   205 }
   206 
   207 void TQueryOp::SetPreparedQuery()
   208 {
   209 	switch (m_type)
   210 	{
   211 		case Query_InsertCookie:
   212 		{
   213 			m_pQuery = g_ClientPrefs.InsertCookieQuery;
   214 			break;
   215 		}
   216 
   217 		case Query_SelectData:
   218 		{
   219 			m_pQuery = g_ClientPrefs.SelectDataQuery;
   220 			break;
   221 		}
   222 
   223 		case Query_InsertData:
   224 		{
   225 			m_pQuery = g_ClientPrefs.InsertDataQuery;
   226 			break;
   227 		}
   228 
   229 		case Query_SelectId:
   230 		{
   231 			m_pQuery = g_ClientPrefs.SelectIdQuery;
   232 			break;
   233 		}
   234 
   235 		default:
   236 		{
   237 			break;
   238 		}
   239 
   240 	}
   241 }
   242 
   243 void TQueryOp::SetCustomPreparedQuery(IPreparedQuery *query)
   244 {
   245 	m_pQuery = query;
   246 }
   247 
   248 ParamData::~ParamData()
   249 {
   250 	if (cookie)
   251 	{
   252 		g_ClientPrefs.cookieMutex->Lock();
   253 		cookie->usedInQuery--;
   254 
   255 		if (cookie->shouldDelete && cookie->usedInQuery <= 0)
   256 		{
   257 			g_ClientPrefs.cookieMutex->Unlock();
   258 			delete cookie;
   259 			cookie = NULL;
   260 		}
   261 
   262 		g_ClientPrefs.cookieMutex->Unlock();	
   263 	}
   264 
   265 	if (data)
   266 	{
   267 		/* Data is only ever passed in a client disconnect query and always needs to be deleted */
   268 		delete data;
   269 		data = NULL;
   270 	}
   271 }
   272 
   273 ParamData::ParamData()
   274 {
   275 	cookie = NULL;
   276 	data = NULL;
   277 	steamId[0] = '\0';
   278 	cookieId = 0;
   279 }