extensions/clientprefs/query.cpp
author David Anderson <dvander@alliedmods.net>
Wed Oct 15 01:01:36 2008 -0700 (2008-10-15)
changeset 2359 a51e376f3700
parent 2338 b9a152a270c9
child 2364 d0625900d257
permissions -rw-r--r--
Various clientprefs logging to try and catch bug 2698 in the act.
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
			default:
pred@2207
    67
			{
pred@2207
    68
				break;
pred@2207
    69
			}
pred@2009
    70
		}
pred@2009
    71
	}
pred@2009
    72
}
pred@2009
    73
pred@2009
    74
void TQueryOp::RunThreadPart()
pred@2009
    75
{
pred@2207
    76
	if (m_type == Query_Connect)
pred@2207
    77
	{
pred@2207
    78
		g_ClientPrefs.DatabaseConnect();
pred@2207
    79
	}
pred@2207
    80
	else
pred@2009
    81
	{
dvander@2359
    82
		assert(m_database != NULL);
pred@2207
    83
pred@2207
    84
		m_database->LockForFullAtomicOperation();
pred@2009
    85
pred@2207
    86
		if (!BindParamsAndRun())
pred@2207
    87
		{
pred@2207
    88
			g_pSM->LogError(myself, "Failed SQL Query, Error: \"%s\" (Query id %i - client %i)", m_database->GetError(), m_type, m_client);
pred@2207
    89
		}
pred@2009
    90
pred@2207
    91
		m_insertId = m_database->GetInsertID();
pred@2207
    92
pred@2207
    93
		m_database->UnlockFromFullAtomicOperation();
pred@2207
    94
	}
pred@2009
    95
}
pred@2009
    96
pred@2009
    97
IDBDriver *TQueryOp::GetDriver()
pred@2009
    98
{
dvander@2359
    99
	assert(m_database != NULL);
pred@2207
   100
pred@2207
   101
	return m_database->GetDriver();
pred@2009
   102
}
pred@2009
   103
IdentityToken_t *TQueryOp::GetOwner()
pred@2009
   104
{
pred@2009
   105
	return myself->GetIdentity();
pred@2009
   106
}
pred@2220
   107
pred@2009
   108
void TQueryOp::Destroy()
pred@2009
   109
{
pred@2009
   110
	delete this;
pred@2009
   111
}
pred@2009
   112
pred@2207
   113
TQueryOp::TQueryOp(enum querytype type, int client)
pred@2009
   114
{
pred@2009
   115
	m_type = type;
pred@2009
   116
	m_client = client;
pred@2131
   117
	m_pQuery = NULL;
pred@2207
   118
	m_database = NULL;
pred@2207
   119
}
pred@2009
   120
pred@2207
   121
TQueryOp::TQueryOp(enum querytype type, Cookie *cookie)
pred@2207
   122
{
pred@2207
   123
	m_type = type;
pred@2207
   124
	m_pCookie = cookie;
pred@2207
   125
	m_pQuery = NULL;
pred@2207
   126
	m_database = NULL;
pred@2207
   127
}
pred@2207
   128
pred@2207
   129
void TQueryOp::SetDatabase( IDatabase *db )
pred@2207
   130
{
pred@2207
   131
	m_database = db;
pred@2207
   132
	m_database->IncReferenceCount();
pred@2009
   133
}
pred@2009
   134
pred@2207
   135
bool TQueryOp::BindParamsAndRun()
pred@2207
   136
{
dvander@2359
   137
	assert(m_pQuery != NULL);
dvander@2359
   138
dvander@2359
   139
	/* :TODO: remove this check. */
pred@2207
   140
	if (m_pQuery == NULL)
pred@2207
   141
	{
dvander@2359
   142
		g_pSM->LogError(myself, "Attempted to run with a NULL Query (type %d)", m_type);
pred@2207
   143
		return false;
pred@2207
   144
	}
pred@2207
   145
pred@2207
   146
	switch (m_type)
pred@2207
   147
	{
pred@2207
   148
		case Query_InsertCookie:
pred@2207
   149
		{
pred@2207
   150
			m_pQuery->BindParamString(0, m_params.cookie->name, false);
pred@2207
   151
			m_pQuery->BindParamString(1, m_params.cookie->description, false);
pred@2207
   152
			m_pQuery->BindParamInt(2, m_params.cookie->access);
pred@2207
   153
pred@2207
   154
			break;
pred@2207
   155
		}
pred@2207
   156
pred@2207
   157
		case Query_SelectData:
pred@2207
   158
		{
pred@2207
   159
			m_pQuery->BindParamString(0, m_params.steamId, false);
pred@2207
   160
pred@2207
   161
			break;
pred@2207
   162
		}
pred@2207
   163
pred@2207
   164
		case Query_InsertData:
pred@2207
   165
		{
pred@2207
   166
			m_pQuery->BindParamString(0, m_params.steamId, false);
pred@2207
   167
			m_pQuery->BindParamInt(1, m_params.cookieId);
pred@2207
   168
			m_pQuery->BindParamString(2, m_params.data->value, false);
pred@2207
   169
			m_pQuery->BindParamInt(3, (unsigned int)time(NULL), false);
pred@2207
   170
pred@2207
   171
			if (driver == DRIVER_MYSQL)
pred@2207
   172
			{
pred@2207
   173
				m_pQuery->BindParamString(4, m_params.data->value, false);
pred@2207
   174
				m_pQuery->BindParamInt(5, (unsigned int)time(NULL), false);
pred@2207
   175
			}
pred@2207
   176
pred@2207
   177
			break;
pred@2207
   178
		}
pred@2207
   179
pred@2207
   180
		case Query_SelectId:
pred@2207
   181
		{
pred@2207
   182
			/* the steamId var was actually used to store the name of the cookie - Save duplicating vars */
pred@2207
   183
			m_pQuery->BindParamString(0, m_params.steamId, false);
pred@2207
   184
pred@2207
   185
			break;
pred@2207
   186
		}
pred@2207
   187
pred@2207
   188
		default:
pred@2207
   189
		{
pred@2207
   190
			break;
pred@2207
   191
		}
pred@2207
   192
			
pred@2207
   193
	}
pred@2207
   194
pred@2207
   195
	return m_pQuery->Execute();
pred@2207
   196
}
pred@2207
   197
pred@2207
   198
void TQueryOp::SetPreparedQuery()
pred@2009
   199
{
pred@2207
   200
	switch (m_type)
pred@2207
   201
	{
pred@2207
   202
		case Query_InsertCookie:
pred@2207
   203
		{
pred@2207
   204
			m_pQuery = g_ClientPrefs.InsertCookieQuery;
pred@2207
   205
			break;
pred@2207
   206
		}
pred@2207
   207
pred@2207
   208
		case Query_SelectData:
pred@2207
   209
		{
pred@2207
   210
			m_pQuery = g_ClientPrefs.SelectDataQuery;
pred@2207
   211
			break;
pred@2207
   212
		}
pred@2207
   213
pred@2207
   214
		case Query_InsertData:
pred@2207
   215
		{
pred@2207
   216
			m_pQuery = g_ClientPrefs.InsertDataQuery;
pred@2207
   217
			break;
pred@2207
   218
		}
pred@2207
   219
pred@2207
   220
		case Query_SelectId:
pred@2207
   221
		{
pred@2207
   222
			m_pQuery = g_ClientPrefs.SelectIdQuery;
pred@2207
   223
			break;
pred@2207
   224
		}
pred@2207
   225
pred@2207
   226
		default:
pred@2207
   227
		{
pred@2207
   228
			break;
pred@2207
   229
		}
pred@2207
   230
	}
pred@2207
   231
}
pred@2009
   232
pred@2207
   233
ParamData::~ParamData()
pred@2207
   234
{
pred@2207
   235
	if (cookie)
pred@2207
   236
	{
pred@2207
   237
		g_ClientPrefs.cookieMutex->Lock();
pred@2207
   238
		cookie->usedInQuery--;
pred@2207
   239
pred@2207
   240
		if (cookie->shouldDelete && cookie->usedInQuery <= 0)
pred@2207
   241
		{
pred@2207
   242
			g_ClientPrefs.cookieMutex->Unlock();
pred@2207
   243
			delete cookie;
pred@2207
   244
			cookie = NULL;
pred@2207
   245
		}
pred@2207
   246
pred@2207
   247
		g_ClientPrefs.cookieMutex->Unlock();	
pred@2207
   248
	}
pred@2207
   249
pred@2207
   250
	if (data)
pred@2207
   251
	{
pred@2207
   252
		/* Data is only ever passed in a client disconnect query and always needs to be deleted */
pred@2207
   253
		delete data;
pred@2207
   254
		data = NULL;
pred@2207
   255
	}
pred@2207
   256
}
pred@2207
   257
pred@2207
   258
ParamData::ParamData()
pred@2207
   259
{
pred@2207
   260
	cookie = NULL;
pred@2207
   261
	data = NULL;
pred@2207
   262
	steamId[0] = '\0';
pred@2207
   263
	cookieId = 0;
pred@2208
   264
}