Cleanup and applying code style to some Database files.
--HG-- branch : trunk
This commit is contained in:
@@ -32,40 +32,41 @@ DBCFileLoader::DBCFileLoader()
|
|||||||
|
|
||||||
bool DBCFileLoader::Load(const char *filename, const char *fmt)
|
bool DBCFileLoader::Load(const char *filename, const char *fmt)
|
||||||
{
|
{
|
||||||
|
|
||||||
uint32 header;
|
uint32 header;
|
||||||
if(data)
|
if (data)
|
||||||
{
|
{
|
||||||
delete [] data;
|
delete [] data;
|
||||||
data=NULL;
|
data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE * f=fopen(filename,"rb");
|
FILE * f = fopen(filename,"rb");
|
||||||
if(!f)return false;
|
if (!f)
|
||||||
|
return false;
|
||||||
|
|
||||||
if(fread(&header,4,1,f)!=1) // Number of records
|
if (fread(&header,4,1,f)!=1) // Number of records
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
EndianConvert(header);
|
EndianConvert(header);
|
||||||
if(header!=0x43424457)
|
|
||||||
|
if (header!=0x43424457)
|
||||||
return false; //'WDBC'
|
return false; //'WDBC'
|
||||||
|
|
||||||
if(fread(&recordCount,4,1,f)!=1) // Number of records
|
if (fread(&recordCount,4,1,f)!=1) // Number of records
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
EndianConvert(recordCount);
|
EndianConvert(recordCount);
|
||||||
|
|
||||||
if(fread(&fieldCount,4,1,f)!=1) // Number of fields
|
if (fread(&fieldCount,4,1,f)!=1) // Number of fields
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
EndianConvert(fieldCount);
|
EndianConvert(fieldCount);
|
||||||
|
|
||||||
if(fread(&recordSize,4,1,f)!=1) // Size of a record
|
if (fread(&recordSize,4,1,f)!=1) // Size of a record
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
EndianConvert(recordSize);
|
EndianConvert(recordSize);
|
||||||
|
|
||||||
if(fread(&stringSize,4,1,f)!=1) // String size
|
if (fread(&stringSize,4,1,f)!=1) // String size
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
EndianConvert(stringSize);
|
EndianConvert(stringSize);
|
||||||
@@ -84,18 +85,20 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
|
|||||||
data = new unsigned char[recordSize*recordCount+stringSize];
|
data = new unsigned char[recordSize*recordCount+stringSize];
|
||||||
stringTable = data + recordSize*recordCount;
|
stringTable = data + recordSize*recordCount;
|
||||||
|
|
||||||
if(fread(data,recordSize*recordCount+stringSize,1,f)!=1)
|
if (fread(data,recordSize*recordCount+stringSize,1,f)!=1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
DBCFileLoader::~DBCFileLoader()
|
DBCFileLoader::~DBCFileLoader()
|
||||||
{
|
{
|
||||||
if(data)
|
if (data)
|
||||||
delete [] data;
|
delete [] data;
|
||||||
if(fieldsOffset)
|
|
||||||
|
if (fieldsOffset)
|
||||||
delete [] fieldsOffset;
|
delete [] fieldsOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +113,7 @@ uint32 DBCFileLoader::GetFormatRecordSize(const char * format,int32* index_pos)
|
|||||||
uint32 recordsize = 0;
|
uint32 recordsize = 0;
|
||||||
int32 i = -1;
|
int32 i = -1;
|
||||||
for (uint32 x=0; format[x]; ++x)
|
for (uint32 x=0; format[x]; ++x)
|
||||||
|
{
|
||||||
switch(format[x])
|
switch(format[x])
|
||||||
{
|
{
|
||||||
case FT_FLOAT:
|
case FT_FLOAT:
|
||||||
@@ -130,8 +134,9 @@ uint32 DBCFileLoader::GetFormatRecordSize(const char * format,int32* index_pos)
|
|||||||
recordsize += 1;
|
recordsize += 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(index_pos)
|
if (index_pos)
|
||||||
*index_pos = i;
|
*index_pos = i;
|
||||||
|
|
||||||
return recordsize;
|
return recordsize;
|
||||||
@@ -151,14 +156,14 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
typedef char * ptr;
|
typedef char * ptr;
|
||||||
if(strlen(format)!=fieldCount)
|
if (strlen(format)!=fieldCount)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
//get struct size and index pos
|
//get struct size and index pos
|
||||||
int32 i;
|
int32 i;
|
||||||
uint32 recordsize=GetFormatRecordSize(format,&i);
|
uint32 recordsize=GetFormatRecordSize(format,&i);
|
||||||
|
|
||||||
if(i>=0)
|
if (i>=0)
|
||||||
{
|
{
|
||||||
uint32 maxi=0;
|
uint32 maxi=0;
|
||||||
//find max index
|
//find max index
|
||||||
@@ -190,9 +195,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
|||||||
for (uint32 y =0; y<recordCount; ++y)
|
for (uint32 y =0; y<recordCount; ++y)
|
||||||
{
|
{
|
||||||
if(i>=0)
|
if(i>=0)
|
||||||
{
|
|
||||||
indexTable[getRecord(y).getUInt(i)]=&dataTable[offset];
|
indexTable[getRecord(y).getUInt(i)]=&dataTable[offset];
|
||||||
}
|
|
||||||
else
|
else
|
||||||
indexTable[y]=&dataTable[offset];
|
indexTable[y]=&dataTable[offset];
|
||||||
|
|
||||||
@@ -220,6 +223,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlDataTable = dataTable + offset;
|
sqlDataTable = dataTable + offset;
|
||||||
|
|
||||||
return dataTable;
|
return dataTable;
|
||||||
@@ -227,7 +231,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
|||||||
|
|
||||||
char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
|
char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
|
||||||
{
|
{
|
||||||
if(strlen(format)!=fieldCount)
|
if (strlen(format)!=fieldCount)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
char* stringPool= new char[stringSize];
|
char* stringPool= new char[stringSize];
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ class DBCStorage
|
|||||||
{
|
{
|
||||||
DBCFileLoader dbc;
|
DBCFileLoader dbc;
|
||||||
// Check if load was sucessful, only then continue
|
// Check if load was sucessful, only then continue
|
||||||
if(!dbc.Load(fn, fmt))
|
if (!dbc.Load(fn, fmt))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint32 sqlRecordCount = 0;
|
uint32 sqlRecordCount = 0;
|
||||||
|
|||||||
@@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
Database::~Database()
|
Database::~Database()
|
||||||
{
|
{
|
||||||
/*Delete objects*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Database::Initialize(const char *)
|
bool Database::Initialize(const char *)
|
||||||
@@ -39,9 +38,9 @@ bool Database::Initialize(const char *)
|
|||||||
// (See method: PExecuteLog)
|
// (See method: PExecuteLog)
|
||||||
m_logSQL = sConfig.GetBoolDefault("LogSQL", false);
|
m_logSQL = sConfig.GetBoolDefault("LogSQL", false);
|
||||||
m_logsDir = sConfig.GetStringDefault("LogsDir","");
|
m_logsDir = sConfig.GetStringDefault("LogsDir","");
|
||||||
if(!m_logsDir.empty())
|
if (!m_logsDir.empty())
|
||||||
{
|
{
|
||||||
if((m_logsDir.at(m_logsDir.length()-1)!='/') && (m_logsDir.at(m_logsDir.length()-1)!='\\'))
|
if ((m_logsDir.at(m_logsDir.length()-1)!='/') && (m_logsDir.at(m_logsDir.length()-1)!='\\'))
|
||||||
m_logsDir.append("/");
|
m_logsDir.append("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +57,7 @@ void Database::ThreadEnd()
|
|||||||
|
|
||||||
void Database::escape_string(std::string& str)
|
void Database::escape_string(std::string& str)
|
||||||
{
|
{
|
||||||
if(str.empty())
|
if (str.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char* buf = new char[str.size()*2+1];
|
char* buf = new char[str.size()*2+1];
|
||||||
@@ -75,23 +74,23 @@ bool Database::PExecuteLog(const char * format,...)
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
char szQuery [MAX_QUERY_LEN];
|
char szQuery [MAX_QUERY_LEN];
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(res==-1)
|
if (res==-1)
|
||||||
{
|
{
|
||||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( m_logSQL )
|
if (m_logSQL)
|
||||||
{
|
{
|
||||||
time_t curr;
|
time_t curr;
|
||||||
tm local;
|
tm local;
|
||||||
time(&curr); // get current time_t value
|
time(&curr); // get current time_t value
|
||||||
local=*(localtime(&curr)); // dereference and assign
|
local=*(localtime(&curr)); // dereference and assign
|
||||||
char fName[128];
|
char fName[128];
|
||||||
sprintf( fName, "%04d-%02d-%02d_logSQL.sql", local.tm_year+1900, local.tm_mon+1, local.tm_mday );
|
sprintf(fName, "%04d-%02d-%02d_logSQL.sql", local.tm_year+1900, local.tm_mon+1, local.tm_mday);
|
||||||
|
|
||||||
FILE* log_file;
|
FILE* log_file;
|
||||||
std::string logsDir_fname = m_logsDir+fName;
|
std::string logsDir_fname = m_logsDir+fName;
|
||||||
@@ -114,20 +113,20 @@ bool Database::PExecuteLog(const char * format,...)
|
|||||||
void Database::SetResultQueue(SqlResultQueue * queue)
|
void Database::SetResultQueue(SqlResultQueue * queue)
|
||||||
{
|
{
|
||||||
m_queryQueues[ACE_Based::Thread::current()] = queue;
|
m_queryQueues[ACE_Based::Thread::current()] = queue;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryResult_AutoPtr Database::PQuery(const char *format,...)
|
QueryResult_AutoPtr Database::PQuery(const char *format,...)
|
||||||
{
|
{
|
||||||
if(!format) return QueryResult_AutoPtr(NULL);
|
if (!format)
|
||||||
|
return QueryResult_AutoPtr(NULL);
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
char szQuery [MAX_QUERY_LEN];
|
char szQuery [MAX_QUERY_LEN];
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(res==-1)
|
if (res==-1)
|
||||||
{
|
{
|
||||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||||
return QueryResult_AutoPtr(NULL);
|
return QueryResult_AutoPtr(NULL);
|
||||||
@@ -138,15 +137,16 @@ QueryResult_AutoPtr Database::PQuery(const char *format,...)
|
|||||||
|
|
||||||
QueryNamedResult* Database::PQueryNamed(const char *format,...)
|
QueryNamedResult* Database::PQueryNamed(const char *format,...)
|
||||||
{
|
{
|
||||||
if(!format) return NULL;
|
if (!format)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
char szQuery [MAX_QUERY_LEN];
|
char szQuery [MAX_QUERY_LEN];
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(res==-1)
|
if (res==-1)
|
||||||
{
|
{
|
||||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||||
return false;
|
return false;
|
||||||
@@ -163,10 +163,10 @@ bool Database::PExecute(const char * format,...)
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
char szQuery [MAX_QUERY_LEN];
|
char szQuery [MAX_QUERY_LEN];
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(res==-1)
|
if (res==-1)
|
||||||
{
|
{
|
||||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||||
return false;
|
return false;
|
||||||
@@ -202,10 +202,10 @@ bool Database::DirectPExecute(const char * format,...)
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
char szQuery [MAX_QUERY_LEN];
|
char szQuery [MAX_QUERY_LEN];
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(res==-1)
|
if (res==-1)
|
||||||
{
|
{
|
||||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||||
return false;
|
return false;
|
||||||
@@ -214,24 +214,24 @@ bool Database::DirectPExecute(const char * format,...)
|
|||||||
return DirectExecute(szQuery);
|
return DirectExecute(szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Database::CheckRequiredField( char const* table_name, char const* required_name )
|
bool Database::CheckRequiredField(char const* table_name, char const* required_name)
|
||||||
{
|
{
|
||||||
// check required field
|
// check required field
|
||||||
QueryResult_AutoPtr result = PQuery("SELECT %s FROM %s LIMIT 1",required_name,table_name);
|
QueryResult_AutoPtr result = PQuery("SELECT %s FROM %s LIMIT 1",required_name,table_name);
|
||||||
if(result)
|
if (result)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// check fail, prepare readabale error message
|
// check fail, prepare readabale error message
|
||||||
|
|
||||||
// search current required_* field in DB
|
// search current required_* field in DB
|
||||||
QueryNamedResult* result2 = PQueryNamed("SELECT * FROM %s LIMIT 1",table_name);
|
QueryNamedResult* result2 = PQueryNamed("SELECT * FROM %s LIMIT 1",table_name);
|
||||||
if(result2)
|
if (result2)
|
||||||
{
|
{
|
||||||
QueryFieldNames const& namesMap = result2->GetFieldNames();
|
QueryFieldNames const& namesMap = result2->GetFieldNames();
|
||||||
std::string reqName;
|
std::string reqName;
|
||||||
for (QueryFieldNames::const_iterator itr = namesMap.begin(); itr != namesMap.end(); ++itr)
|
for (QueryFieldNames::const_iterator itr = namesMap.begin(); itr != namesMap.end(); ++itr)
|
||||||
{
|
{
|
||||||
if(itr->substr(0,9)=="required_")
|
if (itr->substr(0,9)=="required_")
|
||||||
{
|
{
|
||||||
reqName = *itr;
|
reqName = *itr;
|
||||||
break;
|
break;
|
||||||
@@ -240,7 +240,7 @@ bool Database::CheckRequiredField( char const* table_name, char const* required_
|
|||||||
|
|
||||||
delete result2;
|
delete result2;
|
||||||
|
|
||||||
if(!reqName.empty())
|
if (!reqName.empty())
|
||||||
sLog.outErrorDb("Table `%s` have field `%s` but expected `%s`! Not all sql updates applied?",table_name,reqName.c_str(),required_name);
|
sLog.outErrorDb("Table `%s` have field `%s` but expected `%s`! Not all sql updates applied?",table_name,reqName.c_str(),required_name);
|
||||||
else
|
else
|
||||||
sLog.outErrorDb("Table `%s` not have required_* field but expected `%s`! Not all sql updates applied?",table_name,required_name);
|
sLog.outErrorDb("Table `%s` not have required_* field but expected `%s`! Not all sql updates applied?",table_name,required_name);
|
||||||
|
|||||||
@@ -18,11 +18,14 @@
|
|||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "Database/Database.h"
|
#include "Database/Database.h"
|
||||||
#include "Database/SqlOperations.h"
|
#include "Database/SqlOperations.h"
|
||||||
|
|
||||||
|
|
||||||
/// Function body definitions for the template function members of the Database class
|
/// Function body definitions for the template function members of the Database class
|
||||||
|
|
||||||
|
|
||||||
#define ASYNC_QUERY_BODY(sql, queue_itr) \
|
#define ASYNC_QUERY_BODY(sql, queue_itr) \
|
||||||
if (!sql) return false; \
|
if (!sql) return false; \
|
||||||
\
|
\
|
||||||
@@ -34,6 +37,7 @@
|
|||||||
if (queue_itr == m_queryQueues.end()) return false; \
|
if (queue_itr == m_queryQueues.end()) return false; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define ASYNC_PQUERY_BODY(format, szQuery) \
|
#define ASYNC_PQUERY_BODY(format, szQuery) \
|
||||||
if(!format) return false; \
|
if(!format) return false; \
|
||||||
\
|
\
|
||||||
@@ -53,6 +57,7 @@
|
|||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define ASYNC_DELAYHOLDER_BODY(holder, queue_itr) \
|
#define ASYNC_DELAYHOLDER_BODY(holder, queue_itr) \
|
||||||
if (!holder) return false; \
|
if (!holder) return false; \
|
||||||
\
|
\
|
||||||
@@ -64,8 +69,10 @@
|
|||||||
if (queue_itr == m_queryQueues.end()) return false; \
|
if (queue_itr == m_queryQueues.end()) return false; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- Query / member --
|
// -- Query / member --
|
||||||
|
|
||||||
|
|
||||||
template<class Class>
|
template<class Class>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr), const char *sql)
|
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr), const char *sql)
|
||||||
@@ -74,6 +81,7 @@ Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr),
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class>(object, method), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class>(object, method), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1>
|
template<class Class, typename ParamType1>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *sql)
|
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *sql)
|
||||||
@@ -82,6 +90,7 @@ Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, P
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1>(object, method, (QueryResult_AutoPtr)NULL, param1), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1>(object, method, (QueryResult_AutoPtr)NULL, param1), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1, typename ParamType2>
|
template<class Class, typename ParamType1, typename ParamType2>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql)
|
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql)
|
||||||
@@ -90,6 +99,7 @@ Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, P
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1, ParamType2>(object, method, (QueryResult_AutoPtr)NULL, param1, param2), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1, ParamType2>(object, method, (QueryResult_AutoPtr)NULL, param1, param2), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
|
template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *sql)
|
Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *sql)
|
||||||
@@ -98,8 +108,10 @@ Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, P
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1, ParamType2, ParamType3>(object, method, (QueryResult_AutoPtr)NULL, param1, param2, param3), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1, ParamType2, ParamType3>(object, method, (QueryResult_AutoPtr)NULL, param1, param2, param3), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- Query / static --
|
// -- Query / static --
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamType1>
|
template<typename ParamType1>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *sql)
|
Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *sql)
|
||||||
@@ -108,6 +120,7 @@ Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1>(method, (QueryResult_AutoPtr)NULL, param1), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1>(method, (QueryResult_AutoPtr)NULL, param1), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamType1, typename ParamType2>
|
template<typename ParamType1, typename ParamType2>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql)
|
Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql)
|
||||||
@@ -116,6 +129,7 @@ Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2)
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1, ParamType2>(method, (QueryResult_AutoPtr)NULL, param1, param2), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1, ParamType2>(method, (QueryResult_AutoPtr)NULL, param1, param2), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamType1, typename ParamType2, typename ParamType3>
|
template<typename ParamType1, typename ParamType2, typename ParamType3>
|
||||||
bool
|
bool
|
||||||
Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *sql)
|
Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *sql)
|
||||||
@@ -124,8 +138,10 @@ Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2,
|
|||||||
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1, ParamType2, ParamType3>(method, (QueryResult_AutoPtr)NULL, param1, param2, param3), itr->second));
|
return m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1, ParamType2, ParamType3>(method, (QueryResult_AutoPtr)NULL, param1, param2, param3), itr->second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- PQuery / member --
|
// -- PQuery / member --
|
||||||
|
|
||||||
|
|
||||||
template<class Class>
|
template<class Class>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr), const char *format,...)
|
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr), const char *format,...)
|
||||||
@@ -134,6 +150,7 @@ Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr),
|
|||||||
return AsyncQuery(object, method, szQuery);
|
return AsyncQuery(object, method, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1>
|
template<class Class, typename ParamType1>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *format,...)
|
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *format,...)
|
||||||
@@ -142,6 +159,7 @@ Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr,
|
|||||||
return AsyncQuery(object, method, param1, szQuery);
|
return AsyncQuery(object, method, param1, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1, typename ParamType2>
|
template<class Class, typename ParamType1, typename ParamType2>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...)
|
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...)
|
||||||
@@ -150,6 +168,7 @@ Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr,
|
|||||||
return AsyncQuery(object, method, param1, param2, szQuery);
|
return AsyncQuery(object, method, param1, param2, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
|
template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *format,...)
|
Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *format,...)
|
||||||
@@ -158,8 +177,10 @@ Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr,
|
|||||||
return AsyncQuery(object, method, param1, param2, param3, szQuery);
|
return AsyncQuery(object, method, param1, param2, param3, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- PQuery / static --
|
// -- PQuery / static --
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamType1>
|
template<typename ParamType1>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *format,...)
|
Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *format,...)
|
||||||
@@ -168,6 +189,7 @@ Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType
|
|||||||
return AsyncQuery(method, param1, szQuery);
|
return AsyncQuery(method, param1, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamType1, typename ParamType2>
|
template<typename ParamType1, typename ParamType2>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...)
|
Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...)
|
||||||
@@ -176,6 +198,7 @@ Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2
|
|||||||
return AsyncQuery(method, param1, param2, szQuery);
|
return AsyncQuery(method, param1, param2, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamType1, typename ParamType2, typename ParamType3>
|
template<typename ParamType1, typename ParamType2, typename ParamType3>
|
||||||
bool
|
bool
|
||||||
Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *format,...)
|
Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *format,...)
|
||||||
@@ -184,8 +207,10 @@ Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2
|
|||||||
return AsyncQuery(method, param1, param2, param3, szQuery);
|
return AsyncQuery(method, param1, param2, param3, szQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- QueryHolder --
|
// -- QueryHolder --
|
||||||
|
|
||||||
|
|
||||||
template<class Class>
|
template<class Class>
|
||||||
bool
|
bool
|
||||||
Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*), SqlQueryHolder *holder)
|
Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*), SqlQueryHolder *holder)
|
||||||
@@ -194,6 +219,7 @@ Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_Auto
|
|||||||
return holder->Execute(new Trinity::QueryCallback<Class, SqlQueryHolder*>(object, method, (QueryResult_AutoPtr)NULL, holder), m_threadBody, itr->second);
|
return holder->Execute(new Trinity::QueryCallback<Class, SqlQueryHolder*>(object, method, (QueryResult_AutoPtr)NULL, holder), m_threadBody, itr->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Class, typename ParamType1>
|
template<class Class, typename ParamType1>
|
||||||
bool
|
bool
|
||||||
Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1)
|
Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1)
|
||||||
@@ -202,7 +228,7 @@ Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_Auto
|
|||||||
return holder->Execute(new Trinity::QueryCallback<Class, SqlQueryHolder*, ParamType1>(object, method, (QueryResult_AutoPtr)NULL, holder, param1), m_threadBody, itr->second);
|
return holder->Execute(new Trinity::QueryCallback<Class, SqlQueryHolder*, ParamType1>(object, method, (QueryResult_AutoPtr)NULL, holder, param1), m_threadBody, itr->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#undef ASYNC_QUERY_BODY
|
#undef ASYNC_QUERY_BODY
|
||||||
#undef ASYNC_PQUERY_BODY
|
#undef ASYNC_PQUERY_BODY
|
||||||
#undef ASYNC_DELAYHOLDER_BODY
|
#undef ASYNC_DELAYHOLDER_BODY
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ size_t DatabaseMysql::db_count = 0;
|
|||||||
DatabaseMysql::DatabaseMysql() : Database(), mMysql(0)
|
DatabaseMysql::DatabaseMysql() : Database(), mMysql(0)
|
||||||
{
|
{
|
||||||
// before first connection
|
// before first connection
|
||||||
if( db_count++ == 0 )
|
if (db_count++ == 0)
|
||||||
{
|
{
|
||||||
// Mysql Library Init
|
// Mysql Library Init
|
||||||
mysql_library_init(-1, NULL, NULL);
|
mysql_library_init(-1, NULL, NULL);
|
||||||
@@ -65,15 +65,14 @@ DatabaseMysql::~DatabaseMysql()
|
|||||||
if (mMysql)
|
if (mMysql)
|
||||||
mysql_close(mMysql);
|
mysql_close(mMysql);
|
||||||
|
|
||||||
//Free Mysql library pointers for last ~DB
|
// Free Mysql library pointers for last ~DB
|
||||||
if(--db_count == 0)
|
if (--db_count == 0)
|
||||||
mysql_library_end();
|
mysql_library_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DatabaseMysql::Initialize(const char *infoString)
|
bool DatabaseMysql::Initialize(const char *infoString)
|
||||||
{
|
{
|
||||||
|
if (!Database::Initialize(infoString))
|
||||||
if(!Database::Initialize(infoString))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
tranThread = NULL;
|
tranThread = NULL;
|
||||||
@@ -81,7 +80,7 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||||||
mysqlInit = mysql_init(NULL);
|
mysqlInit = mysql_init(NULL);
|
||||||
if (!mysqlInit)
|
if (!mysqlInit)
|
||||||
{
|
{
|
||||||
sLog.outError( "Could not initialize Mysql connection" );
|
sLog.outError("Could not initialize Mysql connection");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,23 +96,23 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||||||
|
|
||||||
iter = tokens.begin();
|
iter = tokens.begin();
|
||||||
|
|
||||||
if(iter != tokens.end())
|
if (iter != tokens.end())
|
||||||
host = *iter++;
|
host = *iter++;
|
||||||
if(iter != tokens.end())
|
if (iter != tokens.end())
|
||||||
port_or_socket = *iter++;
|
port_or_socket = *iter++;
|
||||||
if(iter != tokens.end())
|
if (iter != tokens.end())
|
||||||
user = *iter++;
|
user = *iter++;
|
||||||
if(iter != tokens.end())
|
if (iter != tokens.end())
|
||||||
password = *iter++;
|
password = *iter++;
|
||||||
if(iter != tokens.end())
|
if (iter != tokens.end())
|
||||||
database = *iter++;
|
database = *iter++;
|
||||||
|
|
||||||
mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8");
|
mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8");
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if(host==".") // named pipe use option (Windows)
|
if (host==".") // named pipe use option (Windows)
|
||||||
{
|
{
|
||||||
unsigned int opt = MYSQL_PROTOCOL_PIPE;
|
unsigned int opt = MYSQL_PROTOCOL_PIPE;
|
||||||
mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt);
|
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
|
||||||
port = 0;
|
port = 0;
|
||||||
unix_socket = 0;
|
unix_socket = 0;
|
||||||
}
|
}
|
||||||
@@ -123,10 +122,10 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||||||
unix_socket = 0;
|
unix_socket = 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if(host==".") // socket use option (Unix/Linux)
|
if (host==".") // socket use option (Unix/Linux)
|
||||||
{
|
{
|
||||||
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
|
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
|
||||||
mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt);
|
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
|
||||||
host = "localhost";
|
host = "localhost";
|
||||||
port = 0;
|
port = 0;
|
||||||
unix_socket = port_or_socket.c_str();
|
unix_socket = port_or_socket.c_str();
|
||||||
@@ -143,24 +142,14 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||||||
|
|
||||||
if (mMysql)
|
if (mMysql)
|
||||||
{
|
{
|
||||||
sLog.outDetail( "Connected to MySQL database at %s",
|
sLog.outDetail("Connected to MySQL database at %s", host.c_str());
|
||||||
host.c_str());
|
sLog.outString("MySQL client library: %s", mysql_get_client_info());
|
||||||
sLog.outString( "MySQL client library: %s", mysql_get_client_info());
|
sLog.outString("MySQL server ver: %s ", mysql_get_server_info( mMysql));
|
||||||
sLog.outString( "MySQL server ver: %s ", mysql_get_server_info( mMysql));
|
|
||||||
|
|
||||||
/*----------SET AUTOCOMMIT ON---------*/
|
|
||||||
// It seems mysql 5.0.x have enabled this feature
|
|
||||||
// by default. In crash case you can lose data!!!
|
|
||||||
// So better to turn this off
|
|
||||||
// ---
|
|
||||||
// This is wrong since Trinity use transactions,
|
|
||||||
// autocommit is turned of during it.
|
|
||||||
// Setting it to on makes atomic updates work
|
|
||||||
if (!mysql_autocommit(mMysql, 1))
|
if (!mysql_autocommit(mMysql, 1))
|
||||||
sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1");
|
sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1");
|
||||||
else
|
else
|
||||||
sLog.outDetail("AUTOCOMMIT NOT SET TO 1");
|
sLog.outDetail("AUTOCOMMIT NOT SET TO 1");
|
||||||
/*-------------------------------------*/
|
|
||||||
|
|
||||||
// set connection properties to UTF8 to properly handle locales for different
|
// set connection properties to UTF8 to properly handle locales for different
|
||||||
// server configs - core sends data in UTF8, so MySQL must expect UTF8 too
|
// server configs - core sends data in UTF8, so MySQL must expect UTF8 too
|
||||||
@@ -170,13 +159,9 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||||||
#if MYSQL_VERSION_ID >= 50003
|
#if MYSQL_VERSION_ID >= 50003
|
||||||
my_bool my_true = (my_bool)1;
|
my_bool my_true = (my_bool)1;
|
||||||
if (mysql_options(mMysql, MYSQL_OPT_RECONNECT, &my_true))
|
if (mysql_options(mMysql, MYSQL_OPT_RECONNECT, &my_true))
|
||||||
{
|
|
||||||
sLog.outDetail("Failed to turn on MYSQL_OPT_RECONNECT.");
|
sLog.outDetail("Failed to turn on MYSQL_OPT_RECONNECT.");
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
sLog.outDetail("Successfully turned on MYSQL_OPT_RECONNECT.");
|
sLog.outDetail("Successfully turned on MYSQL_OPT_RECONNECT.");
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
#warning "Your mySQL client lib version does not support reconnecting after a timeout.\nIf this causes you any trouble we advice you to upgrade your mySQL client libs to at least mySQL 5.0.13 to resolve this problem."
|
#warning "Your mySQL client lib version does not support reconnecting after a timeout.\nIf this causes you any trouble we advice you to upgrade your mySQL client libs to at least mySQL 5.0.13 to resolve this problem."
|
||||||
#endif
|
#endif
|
||||||
@@ -185,8 +170,7 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sLog.outError( "Could not connect to MySQL database at %s: %s\n",
|
sLog.outError("Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit));
|
||||||
host.c_str(),mysql_error(mysqlInit));
|
|
||||||
mysql_close(mysqlInit);
|
mysql_close(mysqlInit);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -203,9 +187,9 @@ bool DatabaseMysql::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **p
|
|||||||
#ifdef TRINITY_DEBUG
|
#ifdef TRINITY_DEBUG
|
||||||
uint32 _s = getMSTime();
|
uint32 _s = getMSTime();
|
||||||
#endif
|
#endif
|
||||||
if(mysql_query(mMysql, sql))
|
if (mysql_query(mMysql, sql))
|
||||||
{
|
{
|
||||||
sLog.outErrorDb( "SQL: %s", sql );
|
sLog.outErrorDb("SQL: %s", sql);
|
||||||
sLog.outErrorDb("query ERROR: %s", mysql_error(mMysql));
|
sLog.outErrorDb("query ERROR: %s", mysql_error(mMysql));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -219,7 +203,6 @@ bool DatabaseMysql::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **p
|
|||||||
*pResult = mysql_store_result(mMysql);
|
*pResult = mysql_store_result(mMysql);
|
||||||
*pRowCount = mysql_affected_rows(mMysql);
|
*pRowCount = mysql_affected_rows(mMysql);
|
||||||
*pFieldCount = mysql_field_count(mMysql);
|
*pFieldCount = mysql_field_count(mMysql);
|
||||||
// end guarded block
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!*pResult )
|
if (!*pResult )
|
||||||
@@ -242,7 +225,7 @@ QueryResult_AutoPtr DatabaseMysql::Query(const char *sql)
|
|||||||
uint64 rowCount = 0;
|
uint64 rowCount = 0;
|
||||||
uint32 fieldCount = 0;
|
uint32 fieldCount = 0;
|
||||||
|
|
||||||
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
|
if (!_Query(sql, &result, &fields, &rowCount, &fieldCount))
|
||||||
return QueryResult_AutoPtr(NULL);
|
return QueryResult_AutoPtr(NULL);
|
||||||
|
|
||||||
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
|
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
|
||||||
@@ -259,7 +242,7 @@ QueryNamedResult* DatabaseMysql::QueryNamed(const char *sql)
|
|||||||
uint64 rowCount = 0;
|
uint64 rowCount = 0;
|
||||||
uint32 fieldCount = 0;
|
uint32 fieldCount = 0;
|
||||||
|
|
||||||
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
|
if (!_Query(sql, &result, &fields, &rowCount, &fieldCount))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
QueryFieldNames names(fieldCount);
|
QueryFieldNames names(fieldCount);
|
||||||
@@ -270,7 +253,7 @@ QueryNamedResult* DatabaseMysql::QueryNamed(const char *sql)
|
|||||||
|
|
||||||
queryResult->NextRow();
|
queryResult->NextRow();
|
||||||
|
|
||||||
return new QueryNamedResult(queryResult,names);
|
return new QueryNamedResult(queryResult, names);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DatabaseMysql::Execute(const char *sql)
|
bool DatabaseMysql::Execute(const char *sql)
|
||||||
@@ -279,19 +262,15 @@ bool DatabaseMysql::Execute(const char *sql)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// don't use queued execution if it has not been initialized
|
// don't use queued execution if it has not been initialized
|
||||||
if (!m_threadBody) return DirectExecute(sql);
|
if (!m_threadBody)
|
||||||
|
return DirectExecute(sql);
|
||||||
|
|
||||||
tranThread = ACE_Based::Thread::current(); // owner of this transaction
|
tranThread = ACE_Based::Thread::current(); // owner of this transaction
|
||||||
TransactionQueues::iterator i = m_tranQueues.find(tranThread);
|
TransactionQueues::iterator i = m_tranQueues.find(tranThread);
|
||||||
if (i != m_tranQueues.end() && i->second != NULL)
|
if (i != m_tranQueues.end() && i->second != NULL)
|
||||||
{ // Statement for transaction
|
i->second->DelayExecute(sql); // Statement for transaction
|
||||||
i->second->DelayExecute(sql);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
m_threadBody->Delay(new SqlStatement(sql)); // Simple sql statement
|
||||||
// Simple sql statement
|
|
||||||
m_threadBody->Delay(new SqlStatement(sql));
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -308,7 +287,7 @@ bool DatabaseMysql::DirectExecute(const char* sql)
|
|||||||
#ifdef TRINITY_DEBUG
|
#ifdef TRINITY_DEBUG
|
||||||
uint32 _s = getMSTime();
|
uint32 _s = getMSTime();
|
||||||
#endif
|
#endif
|
||||||
if(mysql_query(mMysql, sql))
|
if (mysql_query(mMysql, sql))
|
||||||
{
|
{
|
||||||
sLog.outErrorDb("SQL: %s", sql);
|
sLog.outErrorDb("SQL: %s", sql);
|
||||||
sLog.outErrorDb("SQL ERROR: %s", mysql_error(mMysql));
|
sLog.outErrorDb("SQL ERROR: %s", mysql_error(mMysql));
|
||||||
@@ -317,10 +296,9 @@ bool DatabaseMysql::DirectExecute(const char* sql)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef TRINITY_DEBUG
|
#ifdef TRINITY_DEBUG
|
||||||
sLog.outDebug("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql );
|
sLog.outDebug("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
// end guarded block
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -335,9 +313,8 @@ bool DatabaseMysql::_TransactionCmd(const char *sql)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
|
||||||
DEBUG_LOG("SQL: %s", sql);
|
DEBUG_LOG("SQL: %s", sql);
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -383,6 +360,7 @@ bool DatabaseMysql::CommitTransaction()
|
|||||||
{
|
{
|
||||||
if (tranThread != ACE_Based::Thread::current())
|
if (tranThread != ACE_Based::Thread::current())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool _res = _TransactionCmd("COMMIT");
|
bool _res = _TransactionCmd("COMMIT");
|
||||||
tranThread = NULL;
|
tranThread = NULL;
|
||||||
mMutex.release();
|
mMutex.release();
|
||||||
@@ -411,6 +389,7 @@ bool DatabaseMysql::RollbackTransaction()
|
|||||||
{
|
{
|
||||||
if (tranThread != ACE_Based::Thread::current())
|
if (tranThread != ACE_Based::Thread::current())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool _res = _TransactionCmd("ROLLBACK");
|
bool _res = _TransactionCmd("ROLLBACK");
|
||||||
tranThread = NULL;
|
tranThread = NULL;
|
||||||
mMutex.release();
|
mMutex.release();
|
||||||
@@ -424,6 +403,7 @@ bool DatabaseMysql::RollbackTransaction()
|
|||||||
delete i->second;
|
delete i->second;
|
||||||
i->second = NULL;
|
i->second = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,7 +426,8 @@ void DatabaseMysql::InitDelayThread()
|
|||||||
|
|
||||||
void DatabaseMysql::HaltDelayThread()
|
void DatabaseMysql::HaltDelayThread()
|
||||||
{
|
{
|
||||||
if (!m_threadBody || !m_delayThread) return;
|
if (!m_threadBody || !m_delayThread)
|
||||||
|
return;
|
||||||
|
|
||||||
m_threadBody->Stop(); //Stop event
|
m_threadBody->Stop(); //Stop event
|
||||||
m_delayThread->wait(); //Wait for flush to DB
|
m_delayThread->wait(); //Wait for flush to DB
|
||||||
@@ -455,4 +436,3 @@ void DatabaseMysql::HaltDelayThread()
|
|||||||
m_threadBody = NULL;
|
m_threadBody = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -50,12 +50,14 @@ mType(type)
|
|||||||
|
|
||||||
Field::~Field()
|
Field::~Field()
|
||||||
{
|
{
|
||||||
if(mValue) delete [] mValue;
|
if (mValue)
|
||||||
|
delete [] mValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Field::SetValue(const char *value)
|
void Field::SetValue(const char *value)
|
||||||
{
|
{
|
||||||
if(mValue) delete [] mValue;
|
if (mValue)
|
||||||
|
delete [] mValue;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
{
|
{
|
||||||
@@ -65,4 +67,3 @@ void Field::SetValue(const char *value)
|
|||||||
else
|
else
|
||||||
mValue = NULL;
|
mValue = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,4 +105,3 @@ enum Field::DataTypes QueryResultMysql::ConvertNativeType(enum_field_types mysql
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -48,4 +48,3 @@ class QueryResultMysql : public QueryResult
|
|||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ void SqlDelayThread::run()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
SqlAsyncTask * s = NULL;
|
SqlAsyncTask * s = NULL;
|
||||||
// Lets wait for next async task no more than 2 secs
|
|
||||||
ACE_Time_Value _time(2);
|
ACE_Time_Value _time(2);
|
||||||
while (m_running)
|
while (m_running)
|
||||||
{
|
{
|
||||||
// if the running state gets turned off while sleeping
|
// if the running state gets turned off while sleeping
|
||||||
// empty the queue before exiting
|
// empty the queue before exiting
|
||||||
s = dynamic_cast<SqlAsyncTask*> (m_sqlQueue.dequeue());
|
s = dynamic_cast<SqlAsyncTask*> (m_sqlQueue.dequeue());
|
||||||
if(s)
|
if (s)
|
||||||
{
|
{
|
||||||
s->call();
|
s->call();
|
||||||
delete s;
|
delete s;
|
||||||
|
|||||||
@@ -48,4 +48,3 @@ class SqlDelayThread : public ACE_Based::Runnable
|
|||||||
virtual void run(); ///< Main Thread loop
|
virtual void run(); ///< Main Thread loop
|
||||||
};
|
};
|
||||||
#endif //__SQLDELAYTHREAD_H
|
#endif //__SQLDELAYTHREAD_H
|
||||||
|
|
||||||
|
|||||||
@@ -37,16 +37,16 @@ void SqlTransaction::Execute(Database *db)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
db->DirectExecute("START TRANSACTION");
|
db->DirectExecute("START TRANSACTION");
|
||||||
while(!m_queue.empty())
|
while (!m_queue.empty())
|
||||||
{
|
{
|
||||||
char const *sql = m_queue.front();
|
char const *sql = m_queue.front();
|
||||||
m_queue.pop();
|
m_queue.pop();
|
||||||
|
|
||||||
if(!db->DirectExecute(sql))
|
if (!db->DirectExecute(sql))
|
||||||
{
|
{
|
||||||
free((void*)const_cast<char*>(sql));
|
free((void*)const_cast<char*>(sql));
|
||||||
db->DirectExecute("ROLLBACK");
|
db->DirectExecute("ROLLBACK");
|
||||||
while(!m_queue.empty())
|
while (!m_queue.empty())
|
||||||
{
|
{
|
||||||
free((void*)const_cast<char*>(m_queue.front()));
|
free((void*)const_cast<char*>(m_queue.front()));
|
||||||
m_queue.pop();
|
m_queue.pop();
|
||||||
@@ -56,6 +56,7 @@ void SqlTransaction::Execute(Database *db)
|
|||||||
|
|
||||||
free((void*)const_cast<char*>(sql));
|
free((void*)const_cast<char*>(sql));
|
||||||
}
|
}
|
||||||
|
|
||||||
db->DirectExecute("COMMIT");
|
db->DirectExecute("COMMIT");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,8 +64,9 @@ void SqlTransaction::Execute(Database *db)
|
|||||||
|
|
||||||
void SqlQuery::Execute(Database *db)
|
void SqlQuery::Execute(Database *db)
|
||||||
{
|
{
|
||||||
if(!m_callback || !m_queue)
|
if (!m_callback || !m_queue)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/// execute the query and store the result in the callback
|
/// execute the query and store the result in the callback
|
||||||
m_callback->SetResult(db->Query(m_sql));
|
m_callback->SetResult(db->Query(m_sql));
|
||||||
/// add the callback to the sql result queue of the thread it originated from
|
/// add the callback to the sql result queue of the thread it originated from
|
||||||
@@ -84,7 +86,7 @@ void SqlResultQueue::Update()
|
|||||||
|
|
||||||
bool SqlQueryHolder::Execute(Trinity::IQueryCallback * callback, SqlDelayThread *thread, SqlResultQueue *queue)
|
bool SqlQueryHolder::Execute(Trinity::IQueryCallback * callback, SqlDelayThread *thread, SqlResultQueue *queue)
|
||||||
{
|
{
|
||||||
if(!callback || !thread || !queue)
|
if (!callback || !thread || !queue)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/// delay the execution of the queries, sync them with the delay thread
|
/// delay the execution of the queries, sync them with the delay thread
|
||||||
@@ -96,13 +98,13 @@ bool SqlQueryHolder::Execute(Trinity::IQueryCallback * callback, SqlDelayThread
|
|||||||
|
|
||||||
bool SqlQueryHolder::SetQuery(size_t index, const char *sql)
|
bool SqlQueryHolder::SetQuery(size_t index, const char *sql)
|
||||||
{
|
{
|
||||||
if(m_queries.size() <= index)
|
if (m_queries.size() <= index)
|
||||||
{
|
{
|
||||||
sLog.outError("Query index (%u) out of range (size: %u) for query: %s",index,(uint32)m_queries.size(),sql);
|
sLog.outError("Query index (%u) out of range (size: %u) for query: %s",index,(uint32)m_queries.size(),sql);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(m_queries[index].first != NULL)
|
if (m_queries[index].first != NULL)
|
||||||
{
|
{
|
||||||
sLog.outError("Attempt assign query to holder index (%u) where other query stored (Old: [%s] New: [%s])",
|
sLog.outError("Attempt assign query to holder index (%u) where other query stored (Old: [%s] New: [%s])",
|
||||||
index,m_queries[index].first,sql);
|
index,m_queries[index].first,sql);
|
||||||
@@ -116,7 +118,7 @@ bool SqlQueryHolder::SetQuery(size_t index, const char *sql)
|
|||||||
|
|
||||||
bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
|
bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
|
||||||
{
|
{
|
||||||
if(!format)
|
if (!format)
|
||||||
{
|
{
|
||||||
sLog.outError("Query (index: %u) is empty.",index);
|
sLog.outError("Query (index: %u) is empty.",index);
|
||||||
return false;
|
return false;
|
||||||
@@ -125,10 +127,10 @@ bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
char szQuery [MAX_QUERY_LEN];
|
char szQuery [MAX_QUERY_LEN];
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(res==-1)
|
if (res==-1)
|
||||||
{
|
{
|
||||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||||
return false;
|
return false;
|
||||||
@@ -139,10 +141,10 @@ bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
|
|||||||
|
|
||||||
QueryResult_AutoPtr SqlQueryHolder::GetResult(size_t index)
|
QueryResult_AutoPtr SqlQueryHolder::GetResult(size_t index)
|
||||||
{
|
{
|
||||||
if(index < m_queries.size())
|
if (index < m_queries.size())
|
||||||
{
|
{
|
||||||
/// the query strings are freed on the first GetResult or in the destructor
|
/// the query strings are freed on the first GetResult or in the destructor
|
||||||
if(m_queries[index].first != NULL)
|
if (m_queries[index].first != NULL)
|
||||||
{
|
{
|
||||||
free((void*)(const_cast<char*>(m_queries[index].first)));
|
free((void*)(const_cast<char*>(m_queries[index].first)));
|
||||||
m_queries[index].first = NULL;
|
m_queries[index].first = NULL;
|
||||||
@@ -157,7 +159,7 @@ QueryResult_AutoPtr SqlQueryHolder::GetResult(size_t index)
|
|||||||
void SqlQueryHolder::SetResult(size_t index, QueryResult_AutoPtr result)
|
void SqlQueryHolder::SetResult(size_t index, QueryResult_AutoPtr result)
|
||||||
{
|
{
|
||||||
/// store the result in the holder
|
/// store the result in the holder
|
||||||
if(index < m_queries.size())
|
if (index < m_queries.size())
|
||||||
m_queries[index].second = result;
|
m_queries[index].second = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +169,7 @@ SqlQueryHolder::~SqlQueryHolder()
|
|||||||
{
|
{
|
||||||
/// if the result was never used, free the resources
|
/// if the result was never used, free the resources
|
||||||
/// results used already (getresult called) are expected to be deleted
|
/// results used already (getresult called) are expected to be deleted
|
||||||
if(m_queries[i].first != NULL)
|
if (m_queries[i].first != NULL)
|
||||||
free((void*)(const_cast<char*>(m_queries[i].first)));
|
free((void*)(const_cast<char*>(m_queries[i].first)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,7 +182,7 @@ void SqlQueryHolder::SetSize(size_t size)
|
|||||||
|
|
||||||
void SqlQueryHolderEx::Execute(Database *db)
|
void SqlQueryHolderEx::Execute(Database *db)
|
||||||
{
|
{
|
||||||
if(!m_holder || !m_callback || !m_queue)
|
if (!m_holder || !m_callback || !m_queue)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/// we can do this, we are friends
|
/// we can do this, we are friends
|
||||||
@@ -196,4 +198,3 @@ void SqlQueryHolderEx::Execute(Database *db)
|
|||||||
/// sync with the caller thread
|
/// sync with the caller thread
|
||||||
m_queue->add(m_callback);
|
m_queue->add(m_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user