merged in mysqlx fixes

This commit is contained in:
David Anderson 2006-06-04 19:40:34 +00:00
parent 24cdcde968
commit 915c3d244c
5 changed files with 77 additions and 233 deletions

View File

@ -413,6 +413,11 @@ static cell AMX_NATIVE_CALL SQL_SetAffinity(AMX *amx, cell *params)
int len; int len;
char *str = MF_GetAmxString(amx, params[1], 0, &len); char *str = MF_GetAmxString(amx, params[1], 0, &len);
if (!str[0])
{
return 1;
}
if (stricmp(str, g_Sqlite.NameString()) == 0) if (stricmp(str, g_Sqlite.NameString()) == 0)
{ {
return 1; return 1;

View File

@ -35,7 +35,7 @@
Name="VCCustomBuildTool"/> Name="VCCustomBuildTool"/>
<Tool <Tool
Name="VCLinkerTool" Name="VCLinkerTool"
OutputFile="$(OutDir)/sqlitex_amxx.dll" OutputFile="$(OutDir)/sqlite_amxx.dll"
LinkIncremental="2" LinkIncremental="2"
IgnoreDefaultLibraryNames="" IgnoreDefaultLibraryNames=""
ModuleDefinitionFile="" ModuleDefinitionFile=""
@ -91,7 +91,7 @@
Name="VCCustomBuildTool"/> Name="VCCustomBuildTool"/>
<Tool <Tool
Name="VCLinkerTool" Name="VCLinkerTool"
OutputFile="$(OutDir)/sqlitex_amxx.dll" OutputFile="$(OutDir)/sqlite_amxx.dll"
LinkIncremental="1" LinkIncremental="1"
GenerateDebugInformation="TRUE" GenerateDebugInformation="TRUE"
SubSystem="2" SubSystem="2"

View File

@ -4,12 +4,12 @@
#define __MODULECONFIG_H__ #define __MODULECONFIG_H__
// Module info // Module info
#define MODULE_NAME "SQLiteX" #define MODULE_NAME "SQLite"
#define MODULE_VERSION "1.75" #define MODULE_VERSION "1.75"
#define MODULE_AUTHOR "AMX Mod X Dev Team" #define MODULE_AUTHOR "AMX Mod X Dev Team"
#define MODULE_URL "http://www.amxmodx.org/" #define MODULE_URL "http://www.amxmodx.org/"
#define MODULE_LOGTAG "SQLITEX" #define MODULE_LOGTAG "SQLITE"
#define MODULE_LIBRARY "sqlitex" #define MODULE_LIBRARY "sqlite"
#define MODULE_LIBCLASS "sqlx" #define MODULE_LIBCLASS "sqlx"
// If you want the module not to be reloaded on mapchange, remove / comment out the next line // If you want the module not to be reloaded on mapchange, remove / comment out the next line
#define MODULE_RELOAD_ON_MAPCHANGE #define MODULE_RELOAD_ON_MAPCHANGE

View File

@ -8,7 +8,6 @@ using namespace SourceHook;
MainThreader g_Threader; MainThreader g_Threader;
ThreadWorker *g_pWorker = NULL; ThreadWorker *g_pWorker = NULL;
extern DLL_FUNCTIONS *g_pFunctionTable; extern DLL_FUNCTIONS *g_pFunctionTable;
StringPool g_StringPool;
IMutex *g_QueueLock = NULL; IMutex *g_QueueLock = NULL;
CStack<MysqlThread *> g_ThreadQueue; CStack<MysqlThread *> g_ThreadQueue;
CStack<MysqlThread *> g_FreeThreads; CStack<MysqlThread *> g_FreeThreads;
@ -260,9 +259,8 @@ void OnPluginsLoaded()
return; return;
} }
if (!g_StringPool.IsThreadable()) if (!g_QueueLock)
{ {
g_StringPool.SetMutex(g_Threader.MakeMutex());
g_QueueLock = g_Threader.MakeMutex(); g_QueueLock = g_Threader.MakeMutex();
} }
g_pWorker = new ThreadWorker(&g_Threader, 250); g_pWorker = new ThreadWorker(&g_Threader, 250);
@ -317,17 +315,16 @@ void OnPluginsUnloading()
} }
/*********************** /***********************
* ATOMIC RESULT STUFF * * ATOMIC RESULT STUFF *
***********************/ ***********************/
AtomicResult::AtomicResult() AtomicResult::AtomicResult()
{ {
m_IsFree = true; m_IsFree = true;
m_CurRow = 0; m_CurRow = 1;
m_AllocFields = 0; m_RowCount = NULL;
m_AllocRows = 0; m_Table = NULL;
m_Rows = NULL; m_AllocSize = 0;
m_Fields = NULL;
} }
AtomicResult::~AtomicResult() AtomicResult::~AtomicResult()
@ -337,20 +334,15 @@ AtomicResult::~AtomicResult()
FreeHandle(); FreeHandle();
} }
if (m_AllocFields) for (size_t i=0; i<=m_AllocSize; i++)
{ {
delete [] m_Fields; delete m_Table[i];
m_AllocFields = 0;
m_Fields = NULL;
}
if (m_AllocRows)
{
for (unsigned int i=0; i<m_AllocRows; i++)
delete [] m_Rows[i];
delete [] m_Rows;
m_AllocRows = 0;
m_Rows = NULL;
} }
delete [] m_Table;
m_Table = NULL;
m_IsFree = true;
} }
unsigned int AtomicResult::RowCount() unsigned int AtomicResult::RowCount()
@ -372,14 +364,22 @@ bool AtomicResult::FieldNameToNum(const char *name, unsigned int *columnId)
{ {
for (unsigned int i=0; i<m_FieldCount; i++) for (unsigned int i=0; i<m_FieldCount; i++)
{ {
if (strcmp(g_StringPool.GetString(m_Fields[i]), name) == 0) assert(m_Table[i] != NULL);
if (strcmp(m_Table[i]->c_str(), name) == 0)
{ {
if (*columnId) if (columnId)
{
*columnId = i; *columnId = i;
}
return true; return true;
} }
} }
if (columnId)
{
*columnId = -1;
}
return false; return false;
} }
@ -388,7 +388,9 @@ const char *AtomicResult::FieldNumToName(unsigned int num)
if (num >= m_FieldCount) if (num >= m_FieldCount)
return NULL; return NULL;
return g_StringPool.GetString(m_Fields[num]); assert(m_Table[num] != NULL);
return m_Table[num]->c_str();
} }
double AtomicResult::GetDouble(unsigned int columnId) double AtomicResult::GetDouble(unsigned int columnId)
@ -416,7 +418,7 @@ const char *AtomicResult::GetRaw(unsigned int columnId, size_t *length)
const char *AtomicResult::GetStringSafe(unsigned int columnId) const char *AtomicResult::GetStringSafe(unsigned int columnId)
{ {
const char *str = GetString(columnId); const char *str = GetString(columnId);
return str ? str : ""; return str ? str : "";
} }
@ -425,7 +427,11 @@ const char *AtomicResult::GetString(unsigned int columnId)
if (columnId >= m_FieldCount) if (columnId >= m_FieldCount)
return NULL; return NULL;
return g_StringPool.GetString(m_Rows[m_CurRow][columnId]); size_t idx = (m_CurRow * m_FieldCount) + columnId;
assert(m_Table[idx] != NULL);
return m_Table[idx]->c_str();
} }
IResultRow *AtomicResult::GetRow() IResultRow *AtomicResult::GetRow()
@ -435,7 +441,7 @@ IResultRow *AtomicResult::GetRow()
bool AtomicResult::IsDone() bool AtomicResult::IsDone()
{ {
if (m_CurRow >= m_RowCount) if (m_CurRow > m_RowCount)
return true; return true;
return false; return false;
@ -452,19 +458,6 @@ void AtomicResult::_InternalClear()
return; return;
m_IsFree = true; m_IsFree = true;
g_StringPool.StartHardLock();
for (unsigned int i=0; i<m_FieldCount; i++)
g_StringPool.FreeString(m_Fields[i]);
for (unsigned int i=0; i<m_RowCount; i++)
{
for (unsigned int j=0; j<m_FieldCount; j++)
g_StringPool.FreeString(m_Rows[i][j]);
}
g_StringPool.StopHardLock();
} }
void AtomicResult::FreeHandle() void AtomicResult::FreeHandle()
@ -483,176 +476,50 @@ void AtomicResult::CopyFrom(IResultSet *rs)
m_FieldCount = rs->FieldCount(); m_FieldCount = rs->FieldCount();
m_RowCount = rs->RowCount(); m_RowCount = rs->RowCount();
if (m_RowCount > m_AllocRows) m_CurRow = 1;
size_t newTotal = (m_RowCount * m_FieldCount) + m_FieldCount;
if (newTotal > m_AllocSize)
{ {
/** allocate new array, zero it */ SourceHook::String **table = new SourceHook::String *[newTotal];
stridx_t **newRows = new stridx_t *[m_RowCount]; memset(table, 0, newTotal * sizeof(SourceHook::String *));
memset(newRows, 0, m_RowCount * sizeof(stridx_t *)); if (m_Table)
/** if we have a new field count, just delete all the old stuff. */
if (m_FieldCount > m_AllocFields)
{ {
for (unsigned int i=0; i<m_AllocRows; i++) memcpy(table, m_Table, m_AllocSize * sizeof(SourceHook::String *));
{ delete [] m_Table;
delete [] m_Rows[i];
newRows[i] = new stridx_t[m_FieldCount];
}
for (unsigned int i=m_AllocRows; i<m_RowCount; i++)
newRows[i] = new stridx_t[m_FieldCount];
} else {
/** copy the old pointers */
memcpy(newRows, m_Rows, m_AllocRows * sizeof(stridx_t *));
for (unsigned int i=m_AllocRows; i<m_RowCount; i++)
newRows[i] = new stridx_t[m_AllocFields];
} }
delete [] m_Rows; m_Table = table;
m_Rows = newRows; m_AllocSize = newTotal;
m_AllocRows = m_RowCount;
}
if (m_FieldCount > m_AllocFields)
{
delete [] m_Fields;
m_Fields = new stridx_t[m_FieldCount];
m_AllocFields = m_FieldCount;
}
m_CurRow = 0;
g_StringPool.StartHardLock();
IResultRow *row;
unsigned int idx = 0;
while (!rs->IsDone())
{
row = rs->GetRow();
for (size_t i=0; i<m_FieldCount; i++)
m_Rows[idx][i] = g_StringPool.MakeString(row->GetString(i));
rs->NextRow();
idx++;
} }
for (unsigned int i=0; i<m_FieldCount; i++) for (unsigned int i=0; i<m_FieldCount; i++)
m_Fields[i] = g_StringPool.MakeString(rs->FieldNumToName(i));
g_StringPool.StopHardLock();
}
/*********************
* STRING POOL STUFF *
*********************/
StringPool::StringPool()
{
m_mutex = NULL;
m_stoplock = false;
}
StringPool::~StringPool()
{
if (m_stoplock)
StopHardLock();
if (m_mutex)
UnsetMutex();
for (size_t i=0; i<m_Strings.size(); i++)
delete m_Strings[i];
m_Strings.clear();
m_UseTable.clear();
while (!m_FreeStrings.empty())
m_FreeStrings.pop();
}
bool StringPool::IsThreadable()
{
return (m_mutex != NULL);
}
const char *StringPool::GetString(stridx_t idx)
{
if (idx < 0 || idx >= (int)m_Strings.size() || !m_UseTable[idx])
return NULL;
return m_Strings[idx]->c_str();
}
void StringPool::FreeString(stridx_t idx)
{
if (idx < 0 || idx >= (int)m_Strings.size())
return;
if (!m_stoplock && m_mutex)
m_mutex->Lock();
if (m_UseTable[idx])
{ {
m_FreeStrings.push(idx); if (m_Table[i])
m_UseTable[idx] = 0; {
m_Table[i]->assign(rs->FieldNumToName(i));
} else {
m_Table[i] = new SourceHook::String(rs->FieldNumToName(i));
}
} }
if (!m_stoplock && m_mutex) IResultRow *row;
m_mutex->Unlock(); unsigned int idx = m_FieldCount;
} while (!rs->IsDone())
stridx_t StringPool::MakeString(const char *str)
{
if (!str)
return StringPool::NullString;
if (!m_stoplock && m_mutex)
m_mutex->Lock();
stridx_t idx;
if (m_FreeStrings.empty())
{ {
idx = static_cast<stridx_t>(m_Strings.size()); row = rs->GetRow();
SourceHook::String *shString = new SourceHook::String(str); for (unsigned int i=0; i<m_FieldCount; i++,idx++)
m_Strings.push_back(shString); {
m_UseTable.push_back(1); if (m_Table[idx])
} else { {
idx = m_FreeStrings.front(); m_Table[idx]->assign(row->GetString(i));
m_FreeStrings.pop(); } else {
m_UseTable[idx] = 1; m_Table[idx] = new SourceHook::String(row->GetString(i));
m_Strings[idx]->assign(str); }
} }
rs->NextRow();
if (!m_stoplock && m_mutex)
m_mutex->Unlock();
return idx;
}
void StringPool::SetMutex(IMutex *m)
{
m_mutex = m;
}
void StringPool::UnsetMutex()
{
if (m_mutex)
{
m_mutex->DestroyThis();
m_mutex = NULL;
} }
} }
void StringPool::StartHardLock()
{
if (m_stoplock)
return;
m_mutex->Lock();
m_stoplock = true;
}
void StringPool::StopHardLock()
{
if (!m_stoplock)
return;
m_mutex->Unlock();
m_stoplock = false;
}
AMX_NATIVE_INFO g_ThreadSqlNatives[] = AMX_NATIVE_INFO g_ThreadSqlNatives[] =
{ {

View File

@ -14,32 +14,6 @@ struct QueuedResultInfo
bool query_success; bool query_success;
}; };
typedef int stridx_t;
class StringPool
{
public:
StringPool();
~StringPool();
void SetMutex(IMutex *m);
void UnsetMutex();
bool IsThreadable();
public:
stridx_t MakeString(const char *str);
void FreeString(stridx_t idx);
const char *GetString(stridx_t idx);
void StartHardLock();
void StopHardLock();
public:
static const int NullString = -1;
private:
CStack<stridx_t> m_FreeStrings;
CVector<stridx_t> m_UseTable;
CVector<SourceHook::String *> m_Strings;
IMutex *m_mutex;
bool m_stoplock;
};
class AtomicResult : class AtomicResult :
public IResultSet, public IResultSet,
public IResultRow public IResultRow
@ -73,10 +47,8 @@ private:
private: private:
unsigned int m_RowCount; unsigned int m_RowCount;
unsigned int m_FieldCount; unsigned int m_FieldCount;
unsigned int m_AllocFields; size_t m_AllocSize;
unsigned int m_AllocRows; SourceHook::String **m_Table;
stridx_t *m_Fields;
stridx_t **m_Rows;
unsigned int m_CurRow; unsigned int m_CurRow;
bool m_IsFree; bool m_IsFree;
}; };