2014-08-04 10:29:31 +00:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
|
|
|
//
|
|
|
|
// MySQL Module
|
|
|
|
//
|
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
#include "amxxmodule.h"
|
|
|
|
#include "mysql2_header.h"
|
|
|
|
#include "threading.h"
|
|
|
|
|
|
|
|
using namespace SourceMod;
|
|
|
|
using namespace SourceHook;
|
|
|
|
|
|
|
|
MainThreader g_Threader;
|
|
|
|
ThreadWorker *g_pWorker = NULL;
|
|
|
|
extern DLL_FUNCTIONS *g_pFunctionTable;
|
|
|
|
IMutex *g_QueueLock = NULL;
|
|
|
|
CStack<MysqlThread *> g_ThreadQueue;
|
|
|
|
CStack<MysqlThread *> g_FreeThreads;
|
|
|
|
float g_lasttime = 0.0f;
|
|
|
|
|
2006-05-07 21:40:12 +00:00
|
|
|
void ShutdownThreading()
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
if (g_pWorker)
|
|
|
|
{
|
2006-08-19 19:37:45 +00:00
|
|
|
g_pWorker->Stop(true);
|
2006-04-23 01:10:06 +00:00
|
|
|
delete g_pWorker;
|
|
|
|
g_pWorker = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
while (!g_ThreadQueue.empty())
|
|
|
|
{
|
|
|
|
delete g_ThreadQueue.front();
|
|
|
|
g_ThreadQueue.pop();
|
|
|
|
}
|
|
|
|
while (!g_FreeThreads.empty())
|
|
|
|
{
|
|
|
|
delete g_FreeThreads.front();
|
|
|
|
g_FreeThreads.pop();
|
|
|
|
}
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
g_QueueLock->DestroyThis();
|
2006-04-24 10:27:25 +00:00
|
|
|
|
|
|
|
FreeHandleTable();
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//public QueryHandler(state, Handle:query, error[], errnum, data[], size)
|
|
|
|
//native SQL_ThreadQuery(Handle:cn_tuple, const handler[], const query[], const data[]="", dataSize=0);
|
|
|
|
static cell AMX_NATIVE_CALL SQL_ThreadQuery(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
if (!g_pWorker)
|
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Thread worker was unable to start.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SQL_Connection *cn = (SQL_Connection *)GetHandle(params[1], Handle_Connection);
|
|
|
|
if (!cn)
|
|
|
|
{
|
2006-08-18 02:19:38 +00:00
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid info tuple handle: %d", params[1]);
|
2006-04-23 01:10:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len;
|
|
|
|
const char *handler = MF_GetAmxString(amx, params[2], 0, &len);
|
2006-10-01 19:46:56 +00:00
|
|
|
int fwd = MF_RegisterSPForwardByName(amx, handler, FP_CELL, FP_CELL, FP_STRING, FP_CELL, FP_ARRAY, FP_CELL, FP_CELL, FP_DONE);
|
2006-04-23 01:10:06 +00:00
|
|
|
if (fwd < 1)
|
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Function not found: %s", handler);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MysqlThread *kmThread;
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
if (g_FreeThreads.empty())
|
|
|
|
{
|
|
|
|
kmThread = new MysqlThread();
|
2007-10-30 00:09:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
kmThread = g_FreeThreads.front();
|
|
|
|
g_FreeThreads.pop();
|
|
|
|
}
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
|
2006-10-01 19:46:56 +00:00
|
|
|
kmThread->SetInfo(cn->host, cn->user, cn->pass, cn->db, cn->port, cn->max_timeout);
|
2006-04-23 01:10:06 +00:00
|
|
|
kmThread->SetForward(fwd);
|
|
|
|
kmThread->SetQuery(MF_GetAmxString(amx, params[3], 1, &len));
|
|
|
|
kmThread->SetCellData(MF_GetAmxAddr(amx, params[4]), (ucell)params[5]);
|
2014-04-18 20:04:32 +00:00
|
|
|
kmThread->SetCharacterSet(cn->charset);
|
2006-04-23 01:10:06 +00:00
|
|
|
|
|
|
|
g_pWorker->MakeThread(kmThread);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MysqlThread::MysqlThread()
|
|
|
|
{
|
|
|
|
m_fwd = 0;
|
|
|
|
m_data = NULL;
|
|
|
|
m_datalen = 0;
|
|
|
|
m_maxdatalen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MysqlThread::~MysqlThread()
|
|
|
|
{
|
|
|
|
if (m_fwd)
|
|
|
|
{
|
|
|
|
MF_UnregisterSPForward(m_fwd);
|
|
|
|
m_fwd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] m_data;
|
|
|
|
m_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlThread::SetCellData(cell data[], ucell len)
|
|
|
|
{
|
|
|
|
if (len > m_maxdatalen)
|
|
|
|
{
|
|
|
|
delete [] m_data;
|
|
|
|
m_data = new cell[len];
|
|
|
|
m_maxdatalen = len;
|
|
|
|
}
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
m_datalen = len;
|
|
|
|
memcpy(m_data, data, len*sizeof(cell));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlThread::SetForward(int forward)
|
|
|
|
{
|
|
|
|
m_fwd = forward;
|
|
|
|
}
|
|
|
|
|
2006-10-01 19:46:56 +00:00
|
|
|
void MysqlThread::SetInfo(const char *host, const char *user, const char *pass, const char *db, int port, unsigned int max_timeout)
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
m_host.assign(host);
|
|
|
|
m_user.assign(user);
|
|
|
|
m_pass.assign(pass);
|
|
|
|
m_db.assign(db);
|
2013-02-13 07:14:37 +00:00
|
|
|
m_max_timeout = max_timeout;
|
2006-04-23 01:10:06 +00:00
|
|
|
m_port = port;
|
2006-10-01 19:46:56 +00:00
|
|
|
m_qrInfo.queue_time = gpGlobals->time;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 20:04:32 +00:00
|
|
|
void MysqlThread::SetCharacterSet(const char *charset)
|
|
|
|
{
|
|
|
|
m_charset.assign(charset);
|
|
|
|
}
|
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
void MysqlThread::SetQuery(const char *query)
|
|
|
|
{
|
|
|
|
m_query.assign(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlThread::RunThread(IThreadHandle *pHandle)
|
|
|
|
{
|
|
|
|
DatabaseInfo info;
|
|
|
|
|
|
|
|
info.database = m_db.c_str();
|
|
|
|
info.pass = m_pass.c_str();
|
|
|
|
info.user = m_user.c_str();
|
|
|
|
info.host = m_host.c_str();
|
|
|
|
info.port = m_port;
|
2006-10-01 19:46:56 +00:00
|
|
|
info.max_timeout = m_max_timeout;
|
2014-04-18 20:04:32 +00:00
|
|
|
info.charset = m_charset.c_str();
|
2006-10-01 19:46:56 +00:00
|
|
|
|
|
|
|
float save_time = m_qrInfo.queue_time;
|
2006-04-23 01:10:06 +00:00
|
|
|
|
|
|
|
memset(&m_qrInfo, 0, sizeof(m_qrInfo));
|
|
|
|
|
2006-10-01 19:46:56 +00:00
|
|
|
m_qrInfo.queue_time = save_time;
|
|
|
|
|
|
|
|
IDatabase *pDatabase = g_Mysql.Connect2(&info, &m_qrInfo.amxinfo.info.errorcode, m_qrInfo.amxinfo.error, 254);
|
2006-04-23 01:10:06 +00:00
|
|
|
IQuery *pQuery = NULL;
|
|
|
|
if (!pDatabase)
|
|
|
|
{
|
|
|
|
m_qrInfo.connect_success = false;
|
|
|
|
m_qrInfo.query_success = false;
|
2007-10-30 00:09:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
m_qrInfo.connect_success = true;
|
|
|
|
pQuery = pDatabase->PrepareQuery(m_query.c_str());
|
2006-08-28 19:17:26 +00:00
|
|
|
if (!pQuery->Execute2(&m_qrInfo.amxinfo.info, m_qrInfo.amxinfo.error, 254))
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
m_qrInfo.query_success = false;
|
2007-10-30 00:09:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
m_qrInfo.query_success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_qrInfo.query_success && m_qrInfo.amxinfo.info.rs)
|
|
|
|
{
|
|
|
|
m_atomicResult.CopyFrom(m_qrInfo.amxinfo.info.rs);
|
|
|
|
m_qrInfo.amxinfo.info.rs = &m_atomicResult;
|
2006-10-01 19:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pQuery)
|
|
|
|
{
|
2007-10-30 00:09:18 +00:00
|
|
|
pQuery->FreeHandle();
|
|
|
|
pQuery = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_qrInfo.amxinfo.pQuery = NULL;
|
|
|
|
m_qrInfo.amxinfo.opt_ptr = new char[m_query.size() + 1];
|
|
|
|
strcpy(m_qrInfo.amxinfo.opt_ptr, m_query.c_str());
|
2006-09-15 23:11:35 +00:00
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
if (pDatabase)
|
|
|
|
{
|
|
|
|
pDatabase->FreeHandle();
|
|
|
|
pDatabase = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlThread::Invalidate()
|
|
|
|
{
|
|
|
|
m_atomicResult.FreeHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlThread::OnTerminate(IThreadHandle *pHandle, bool cancel)
|
|
|
|
{
|
|
|
|
if (cancel)
|
|
|
|
{
|
|
|
|
Invalidate();
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
g_FreeThreads.push(this);
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
} else {
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
g_ThreadQueue.push(this);
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NullFunc(void *ptr, unsigned int num)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//public QueryHandler(state, Handle:query, error[], errnum, data[], size)
|
|
|
|
void MysqlThread::Execute()
|
|
|
|
{
|
|
|
|
cell data_addr;
|
|
|
|
if (m_datalen)
|
|
|
|
{
|
|
|
|
data_addr = MF_PrepareCellArray(m_data, m_datalen);
|
2007-10-30 00:09:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
static cell tmpdata[1] = {0};
|
|
|
|
data_addr = MF_PrepareCellArray(tmpdata, 1);
|
|
|
|
}
|
|
|
|
int state = 0;
|
|
|
|
if (!m_qrInfo.connect_success)
|
|
|
|
{
|
|
|
|
state = -2;
|
2007-10-30 00:09:18 +00:00
|
|
|
}
|
|
|
|
else if (!m_qrInfo.query_success)
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
state = -1;
|
|
|
|
}
|
2006-10-01 19:46:56 +00:00
|
|
|
float diff = gpGlobals->time - m_qrInfo.queue_time;
|
|
|
|
cell c_diff = amx_ftoc(diff);
|
|
|
|
unsigned int hndl = MakeHandle(&m_qrInfo.amxinfo, Handle_Query, NullFunc);
|
2006-04-23 01:10:06 +00:00
|
|
|
if (state != 0)
|
|
|
|
{
|
|
|
|
MF_ExecuteForward(m_fwd,
|
|
|
|
(cell)state,
|
2006-10-01 19:46:56 +00:00
|
|
|
(cell)hndl,
|
2006-04-23 01:10:06 +00:00
|
|
|
m_qrInfo.amxinfo.error,
|
|
|
|
m_qrInfo.amxinfo.info.errorcode,
|
|
|
|
data_addr,
|
2006-10-01 19:46:56 +00:00
|
|
|
m_datalen,
|
|
|
|
c_diff);
|
2007-10-30 00:09:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
MF_ExecuteForward(m_fwd,
|
|
|
|
(cell)0,
|
|
|
|
(cell)hndl,
|
|
|
|
"",
|
2006-04-23 04:24:46 +00:00
|
|
|
(cell)0,
|
2006-04-23 01:10:06 +00:00
|
|
|
data_addr,
|
2006-10-01 19:46:56 +00:00
|
|
|
m_datalen,
|
|
|
|
c_diff);
|
|
|
|
}
|
|
|
|
FreeHandle(hndl);
|
|
|
|
delete [] m_qrInfo.amxinfo.opt_ptr;
|
|
|
|
m_qrInfo.amxinfo.opt_ptr = NULL;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
* METAMOD STUFF *
|
|
|
|
*****************/
|
|
|
|
|
2006-05-07 21:40:12 +00:00
|
|
|
void OnPluginsLoaded()
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
if (g_pWorker)
|
|
|
|
{
|
2006-05-07 21:40:12 +00:00
|
|
|
return;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
2006-06-04 19:26:43 +00:00
|
|
|
if (!g_QueueLock)
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
g_QueueLock = g_Threader.MakeMutex();
|
|
|
|
}
|
2006-06-04 19:26:43 +00:00
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
g_pWorker = new ThreadWorker(&g_Threader, 250);
|
|
|
|
if (!g_pWorker->Start())
|
|
|
|
{
|
|
|
|
delete g_pWorker;
|
|
|
|
g_pWorker = NULL;
|
|
|
|
}
|
|
|
|
g_pFunctionTable->pfnSpawn = NULL;
|
|
|
|
|
|
|
|
g_lasttime = 0.0f;
|
|
|
|
|
2006-05-07 21:40:12 +00:00
|
|
|
return;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StartFrame()
|
|
|
|
{
|
2006-05-19 01:54:28 +00:00
|
|
|
if (g_pWorker && (g_lasttime < gpGlobals->time))
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
2010-04-27 07:24:50 +00:00
|
|
|
g_lasttime = gpGlobals->time + 0.05f;
|
2006-04-23 01:10:06 +00:00
|
|
|
g_QueueLock->Lock();
|
|
|
|
size_t remaining = g_ThreadQueue.size();
|
|
|
|
if (remaining)
|
|
|
|
{
|
|
|
|
MysqlThread *kmThread;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
kmThread = g_ThreadQueue.front();
|
|
|
|
g_ThreadQueue.pop();
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
kmThread->Execute();
|
|
|
|
kmThread->Invalidate();
|
|
|
|
g_FreeThreads.push(kmThread);
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
} while (!g_ThreadQueue.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_META(MRES_IGNORED);
|
|
|
|
}
|
|
|
|
|
2006-05-07 21:40:12 +00:00
|
|
|
void OnPluginsUnloading()
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
if (!g_pWorker)
|
2006-08-19 19:29:33 +00:00
|
|
|
{
|
2006-05-07 21:40:12 +00:00
|
|
|
return;
|
2006-08-19 19:29:33 +00:00
|
|
|
}
|
2006-04-23 01:10:06 +00:00
|
|
|
|
|
|
|
g_pWorker->Stop(false);
|
|
|
|
delete g_pWorker;
|
|
|
|
g_pWorker = NULL;
|
2006-08-19 19:29:33 +00:00
|
|
|
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
size_t remaining = g_ThreadQueue.size();
|
|
|
|
if (remaining)
|
|
|
|
{
|
|
|
|
MysqlThread *kmThread;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
kmThread = g_ThreadQueue.front();
|
|
|
|
g_ThreadQueue.pop();
|
|
|
|
g_QueueLock->Unlock();
|
|
|
|
kmThread->Execute();
|
|
|
|
kmThread->Invalidate();
|
|
|
|
g_FreeThreads.push(kmThread);
|
|
|
|
g_QueueLock->Lock();
|
|
|
|
} while (!g_ThreadQueue.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
g_QueueLock->Unlock();
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************
|
|
|
|
* ATOMIC RESULT STUFF *
|
|
|
|
***********************/
|
|
|
|
|
|
|
|
AtomicResult::AtomicResult()
|
|
|
|
{
|
|
|
|
m_IsFree = true;
|
2006-06-04 19:26:43 +00:00
|
|
|
m_CurRow = 1;
|
2006-08-18 19:08:51 +00:00
|
|
|
m_RowCount = 0;
|
2006-06-04 19:26:43 +00:00
|
|
|
m_Table = NULL;
|
|
|
|
m_AllocSize = 0;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomicResult::~AtomicResult()
|
|
|
|
{
|
|
|
|
if (!m_IsFree)
|
|
|
|
{
|
|
|
|
FreeHandle();
|
|
|
|
}
|
2006-04-23 23:17:20 +00:00
|
|
|
|
2006-06-04 19:26:43 +00:00
|
|
|
for (size_t i=0; i<=m_AllocSize; i++)
|
2006-04-23 23:17:20 +00:00
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
delete m_Table[i];
|
2006-04-23 23:17:20 +00:00
|
|
|
}
|
2006-06-04 19:26:43 +00:00
|
|
|
|
|
|
|
delete [] m_Table;
|
|
|
|
|
|
|
|
m_Table = NULL;
|
|
|
|
m_IsFree = true;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AtomicResult::RowCount()
|
|
|
|
{
|
|
|
|
return m_RowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomicResult::IsNull(unsigned int columnId)
|
|
|
|
{
|
|
|
|
return (GetString(columnId) == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AtomicResult::FieldCount()
|
|
|
|
{
|
|
|
|
return m_FieldCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomicResult::FieldNameToNum(const char *name, unsigned int *columnId)
|
|
|
|
{
|
|
|
|
for (unsigned int i=0; i<m_FieldCount; i++)
|
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
assert(m_Table[i] != NULL);
|
|
|
|
if (strcmp(m_Table[i]->c_str(), name) == 0)
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
if (columnId)
|
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
*columnId = i;
|
2006-06-04 19:26:43 +00:00
|
|
|
}
|
2006-04-23 01:10:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AtomicResult::FieldNumToName(unsigned int num)
|
|
|
|
{
|
|
|
|
if (num >= m_FieldCount)
|
|
|
|
return NULL;
|
|
|
|
|
2006-06-04 19:26:43 +00:00
|
|
|
assert(m_Table[num] != NULL);
|
|
|
|
|
|
|
|
return m_Table[num]->c_str();
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double AtomicResult::GetDouble(unsigned int columnId)
|
|
|
|
{
|
|
|
|
return atof(GetStringSafe(columnId));
|
|
|
|
}
|
|
|
|
|
|
|
|
float AtomicResult::GetFloat(unsigned int columnId)
|
|
|
|
{
|
|
|
|
return atof(GetStringSafe(columnId));
|
|
|
|
}
|
|
|
|
|
|
|
|
int AtomicResult::GetInt(unsigned int columnId)
|
|
|
|
{
|
|
|
|
return atoi(GetStringSafe(columnId));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AtomicResult::GetRaw(unsigned int columnId, size_t *length)
|
|
|
|
{
|
|
|
|
//we don't support this yet...
|
|
|
|
*length = 0;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AtomicResult::GetStringSafe(unsigned int columnId)
|
|
|
|
{
|
|
|
|
const char *str = GetString(columnId);
|
|
|
|
|
|
|
|
return str ? str : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AtomicResult::GetString(unsigned int columnId)
|
|
|
|
{
|
|
|
|
if (columnId >= m_FieldCount)
|
|
|
|
return NULL;
|
|
|
|
|
2006-06-04 19:26:43 +00:00
|
|
|
size_t idx = (m_CurRow * m_FieldCount) + columnId;
|
|
|
|
|
|
|
|
assert(m_Table[idx] != NULL);
|
|
|
|
|
|
|
|
return m_Table[idx]->c_str();
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IResultRow *AtomicResult::GetRow()
|
|
|
|
{
|
|
|
|
return static_cast<IResultRow *>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomicResult::IsDone()
|
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
if (m_CurRow > m_RowCount)
|
2006-04-23 01:10:06 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomicResult::NextRow()
|
|
|
|
{
|
|
|
|
m_CurRow++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomicResult::_InternalClear()
|
|
|
|
{
|
|
|
|
if (m_IsFree)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_IsFree = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomicResult::FreeHandle()
|
|
|
|
{
|
|
|
|
_InternalClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomicResult::CopyFrom(IResultSet *rs)
|
|
|
|
{
|
|
|
|
if (!m_IsFree)
|
|
|
|
{
|
|
|
|
_InternalClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_IsFree = false;
|
|
|
|
|
|
|
|
m_FieldCount = rs->FieldCount();
|
|
|
|
m_RowCount = rs->RowCount();
|
2006-06-04 19:26:43 +00:00
|
|
|
m_CurRow = 1;
|
|
|
|
|
|
|
|
size_t newTotal = (m_RowCount * m_FieldCount) + m_FieldCount;
|
|
|
|
if (newTotal > m_AllocSize)
|
2006-04-23 23:17:20 +00:00
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
SourceHook::String **table = new SourceHook::String *[newTotal];
|
|
|
|
memset(table, 0, newTotal * sizeof(SourceHook::String *));
|
|
|
|
if (m_Table)
|
2006-04-23 23:17:20 +00:00
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
memcpy(table, m_Table, m_AllocSize * sizeof(SourceHook::String *));
|
|
|
|
delete [] m_Table;
|
2006-04-23 23:17:20 +00:00
|
|
|
}
|
2006-06-04 19:26:43 +00:00
|
|
|
m_Table = table;
|
|
|
|
m_AllocSize = newTotal;
|
2006-04-23 23:17:20 +00:00
|
|
|
}
|
2006-06-04 19:26:43 +00:00
|
|
|
|
|
|
|
for (unsigned int i=0; i<m_FieldCount; i++)
|
2006-04-23 23:17:20 +00:00
|
|
|
{
|
2006-06-04 19:26:43 +00:00
|
|
|
if (m_Table[i])
|
|
|
|
{
|
|
|
|
m_Table[i]->assign(rs->FieldNumToName(i));
|
|
|
|
} else {
|
|
|
|
m_Table[i] = new SourceHook::String(rs->FieldNumToName(i));
|
|
|
|
}
|
2006-04-23 23:17:20 +00:00
|
|
|
}
|
2006-04-23 01:10:06 +00:00
|
|
|
|
|
|
|
IResultRow *row;
|
2006-06-04 19:26:43 +00:00
|
|
|
unsigned int idx = m_FieldCount;
|
2006-04-23 01:10:06 +00:00
|
|
|
while (!rs->IsDone())
|
|
|
|
{
|
|
|
|
row = rs->GetRow();
|
2006-06-04 19:26:43 +00:00
|
|
|
for (unsigned int i=0; i<m_FieldCount; i++,idx++)
|
|
|
|
{
|
|
|
|
if (m_Table[idx])
|
|
|
|
{
|
|
|
|
m_Table[idx]->assign(row->GetString(i));
|
|
|
|
} else {
|
|
|
|
m_Table[idx] = new SourceHook::String(row->GetString(i));
|
|
|
|
}
|
|
|
|
}
|
2006-04-23 01:10:06 +00:00
|
|
|
rs->NextRow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-24 15:46:33 +00:00
|
|
|
void AtomicResult::Rewind()
|
|
|
|
{
|
|
|
|
m_CurRow = 1;
|
|
|
|
}
|
2006-04-23 01:10:06 +00:00
|
|
|
|
2007-10-22 21:31:02 +00:00
|
|
|
bool AtomicResult::NextResultSet()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
AMX_NATIVE_INFO g_ThreadSqlNatives[] =
|
|
|
|
{
|
|
|
|
{"SQL_ThreadQuery", SQL_ThreadQuery},
|
|
|
|
{NULL, NULL},
|
|
|
|
};
|
2007-10-30 00:09:18 +00:00
|
|
|
|