Cleanup and applying code style to some Database files.
--HG-- branch : trunk
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
SET(trinitydatabase_STAT_SRCS
|
||||
DBCFileLoader.cpp
|
||||
DBCFileLoader.h
|
||||
DBCStore.h
|
||||
Database.cpp
|
||||
Database.h
|
||||
DatabaseEnv.h
|
||||
DatabaseImpl.h
|
||||
DatabaseMysql.cpp
|
||||
DatabasePostgre.cpp
|
||||
DatabaseMysql.h
|
||||
DatabasePostgre.h
|
||||
Field.cpp
|
||||
Field.h
|
||||
MySQLDelayThread.h
|
||||
PGSQLDelayThread.h
|
||||
QueryResult.h
|
||||
QueryResultMysql.cpp
|
||||
QueryResultMysql.h
|
||||
QueryResultPostgre.cpp
|
||||
QueryResultPostgre.h
|
||||
SQLStorage.cpp
|
||||
SQLStorage.h
|
||||
SqlDelayThread.cpp
|
||||
SqlDelayThread.h
|
||||
SqlOperations.cpp
|
||||
SqlOperations.h
|
||||
DBCFileLoader.cpp
|
||||
DBCFileLoader.h
|
||||
DBCStore.h
|
||||
Database.cpp
|
||||
Database.h
|
||||
DatabaseEnv.h
|
||||
DatabaseImpl.h
|
||||
DatabaseMysql.cpp
|
||||
DatabasePostgre.cpp
|
||||
DatabaseMysql.h
|
||||
DatabasePostgre.h
|
||||
Field.cpp
|
||||
Field.h
|
||||
MySQLDelayThread.h
|
||||
PGSQLDelayThread.h
|
||||
QueryResult.h
|
||||
QueryResultMysql.cpp
|
||||
QueryResultMysql.h
|
||||
QueryResultPostgre.cpp
|
||||
QueryResultPostgre.h
|
||||
SQLStorage.cpp
|
||||
SQLStorage.h
|
||||
SqlDelayThread.cpp
|
||||
SqlDelayThread.h
|
||||
SqlOperations.cpp
|
||||
SqlOperations.h
|
||||
)
|
||||
|
||||
add_library(trinitydatabase STATIC ${trinitydatabase_STAT_SRCS})
|
||||
|
||||
@@ -32,40 +32,41 @@ DBCFileLoader::DBCFileLoader()
|
||||
|
||||
bool DBCFileLoader::Load(const char *filename, const char *fmt)
|
||||
{
|
||||
|
||||
uint32 header;
|
||||
if(data)
|
||||
if (data)
|
||||
{
|
||||
delete [] data;
|
||||
data=NULL;
|
||||
data = NULL;
|
||||
}
|
||||
|
||||
FILE * f=fopen(filename,"rb");
|
||||
if(!f)return false;
|
||||
FILE * f = fopen(filename,"rb");
|
||||
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;
|
||||
|
||||
EndianConvert(header);
|
||||
if(header!=0x43424457)
|
||||
return false; //'WDBC'
|
||||
|
||||
if(fread(&recordCount,4,1,f)!=1) // Number of records
|
||||
if (header!=0x43424457)
|
||||
return false; //'WDBC'
|
||||
|
||||
if (fread(&recordCount,4,1,f)!=1) // Number of records
|
||||
return false;
|
||||
|
||||
EndianConvert(recordCount);
|
||||
|
||||
if(fread(&fieldCount,4,1,f)!=1) // Number of fields
|
||||
if (fread(&fieldCount,4,1,f)!=1) // Number of fields
|
||||
return false;
|
||||
|
||||
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;
|
||||
|
||||
EndianConvert(recordSize);
|
||||
|
||||
if(fread(&stringSize,4,1,f)!=1) // String size
|
||||
if (fread(&stringSize,4,1,f)!=1) // String size
|
||||
return false;
|
||||
|
||||
EndianConvert(stringSize);
|
||||
@@ -84,18 +85,20 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
|
||||
data = new unsigned char[recordSize*recordCount+stringSize];
|
||||
stringTable = data + recordSize*recordCount;
|
||||
|
||||
if(fread(data,recordSize*recordCount+stringSize,1,f)!=1)
|
||||
if (fread(data,recordSize*recordCount+stringSize,1,f)!=1)
|
||||
return false;
|
||||
|
||||
fclose(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DBCFileLoader::~DBCFileLoader()
|
||||
{
|
||||
if(data)
|
||||
if (data)
|
||||
delete [] data;
|
||||
if(fieldsOffset)
|
||||
|
||||
if (fieldsOffset)
|
||||
delete [] fieldsOffset;
|
||||
}
|
||||
|
||||
@@ -110,6 +113,7 @@ uint32 DBCFileLoader::GetFormatRecordSize(const char * format,int32* index_pos)
|
||||
uint32 recordsize = 0;
|
||||
int32 i = -1;
|
||||
for (uint32 x=0; format[x]; ++x)
|
||||
{
|
||||
switch(format[x])
|
||||
{
|
||||
case FT_FLOAT:
|
||||
@@ -130,8 +134,9 @@ uint32 DBCFileLoader::GetFormatRecordSize(const char * format,int32* index_pos)
|
||||
recordsize += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(index_pos)
|
||||
if (index_pos)
|
||||
*index_pos = i;
|
||||
|
||||
return recordsize;
|
||||
@@ -151,14 +156,14 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
||||
*/
|
||||
|
||||
typedef char * ptr;
|
||||
if(strlen(format)!=fieldCount)
|
||||
if (strlen(format)!=fieldCount)
|
||||
return NULL;
|
||||
|
||||
//get struct size and index pos
|
||||
int32 i;
|
||||
uint32 recordsize=GetFormatRecordSize(format,&i);
|
||||
|
||||
if(i>=0)
|
||||
if (i>=0)
|
||||
{
|
||||
uint32 maxi=0;
|
||||
//find max index
|
||||
@@ -190,9 +195,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
||||
for (uint32 y =0; y<recordCount; ++y)
|
||||
{
|
||||
if(i>=0)
|
||||
{
|
||||
indexTable[getRecord(y).getUInt(i)]=&dataTable[offset];
|
||||
}
|
||||
else
|
||||
indexTable[y]=&dataTable[offset];
|
||||
|
||||
@@ -220,6 +223,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlDataTable = dataTable + offset;
|
||||
|
||||
return dataTable;
|
||||
@@ -227,7 +231,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
|
||||
|
||||
char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
|
||||
{
|
||||
if(strlen(format)!=fieldCount)
|
||||
if (strlen(format)!=fieldCount)
|
||||
return NULL;
|
||||
|
||||
char* stringPool= new char[stringSize];
|
||||
|
||||
@@ -77,7 +77,7 @@ class DBCStorage
|
||||
{
|
||||
DBCFileLoader dbc;
|
||||
// Check if load was sucessful, only then continue
|
||||
if(!dbc.Load(fn, fmt))
|
||||
if (!dbc.Load(fn, fmt))
|
||||
return false;
|
||||
|
||||
uint32 sqlRecordCount = 0;
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
|
||||
Database::~Database()
|
||||
{
|
||||
/*Delete objects*/
|
||||
}
|
||||
|
||||
bool Database::Initialize(const char *)
|
||||
@@ -39,9 +38,9 @@ bool Database::Initialize(const char *)
|
||||
// (See method: PExecuteLog)
|
||||
m_logSQL = sConfig.GetBoolDefault("LogSQL", false);
|
||||
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("/");
|
||||
}
|
||||
|
||||
@@ -58,7 +57,7 @@ void Database::ThreadEnd()
|
||||
|
||||
void Database::escape_string(std::string& str)
|
||||
{
|
||||
if(str.empty())
|
||||
if (str.empty())
|
||||
return;
|
||||
|
||||
char* buf = new char[str.size()*2+1];
|
||||
@@ -75,23 +74,23 @@ bool Database::PExecuteLog(const char * format,...)
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
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);
|
||||
|
||||
if(res==-1)
|
||||
if (res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return false;
|
||||
}
|
||||
|
||||
if( m_logSQL )
|
||||
if (m_logSQL)
|
||||
{
|
||||
time_t curr;
|
||||
tm local;
|
||||
time(&curr); // get current time_t value
|
||||
local=*(localtime(&curr)); // dereference and assign
|
||||
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;
|
||||
std::string logsDir_fname = m_logsDir+fName;
|
||||
@@ -114,20 +113,20 @@ bool Database::PExecuteLog(const char * format,...)
|
||||
void Database::SetResultQueue(SqlResultQueue * queue)
|
||||
{
|
||||
m_queryQueues[ACE_Based::Thread::current()] = queue;
|
||||
|
||||
}
|
||||
|
||||
QueryResult_AutoPtr Database::PQuery(const char *format,...)
|
||||
{
|
||||
if(!format) return QueryResult_AutoPtr(NULL);
|
||||
if (!format)
|
||||
return QueryResult_AutoPtr(NULL);
|
||||
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
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);
|
||||
|
||||
if(res==-1)
|
||||
if (res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return QueryResult_AutoPtr(NULL);
|
||||
@@ -138,15 +137,16 @@ QueryResult_AutoPtr Database::PQuery(const char *format,...)
|
||||
|
||||
QueryNamedResult* Database::PQueryNamed(const char *format,...)
|
||||
{
|
||||
if(!format) return NULL;
|
||||
if (!format)
|
||||
return NULL;
|
||||
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
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);
|
||||
|
||||
if(res==-1)
|
||||
if (res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return false;
|
||||
@@ -163,10 +163,10 @@ bool Database::PExecute(const char * format,...)
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
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);
|
||||
|
||||
if(res==-1)
|
||||
if (res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return false;
|
||||
@@ -202,10 +202,10 @@ bool Database::DirectPExecute(const char * format,...)
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
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);
|
||||
|
||||
if(res==-1)
|
||||
if (res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return false;
|
||||
@@ -214,24 +214,24 @@ bool Database::DirectPExecute(const char * format,...)
|
||||
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
|
||||
QueryResult_AutoPtr result = PQuery("SELECT %s FROM %s LIMIT 1",required_name,table_name);
|
||||
if(result)
|
||||
if (result)
|
||||
return true;
|
||||
|
||||
// check fail, prepare readabale error message
|
||||
|
||||
// search current required_* field in DB
|
||||
QueryNamedResult* result2 = PQueryNamed("SELECT * FROM %s LIMIT 1",table_name);
|
||||
if(result2)
|
||||
if (result2)
|
||||
{
|
||||
QueryFieldNames const& namesMap = result2->GetFieldNames();
|
||||
std::string reqName;
|
||||
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;
|
||||
break;
|
||||
@@ -240,7 +240,7 @@ bool Database::CheckRequiredField( char const* table_name, char const* required_
|
||||
|
||||
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);
|
||||
else
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
#include "Database/Database.h"
|
||||
#include "Database/SqlOperations.h"
|
||||
|
||||
|
||||
/// Function body definitions for the template function members of the Database class
|
||||
|
||||
|
||||
#define ASYNC_QUERY_BODY(sql, queue_itr) \
|
||||
if (!sql) return false; \
|
||||
\
|
||||
@@ -34,6 +37,7 @@
|
||||
if (queue_itr == m_queryQueues.end()) return false; \
|
||||
}
|
||||
|
||||
|
||||
#define ASYNC_PQUERY_BODY(format, szQuery) \
|
||||
if(!format) return false; \
|
||||
\
|
||||
@@ -53,6 +57,7 @@
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#define ASYNC_DELAYHOLDER_BODY(holder, queue_itr) \
|
||||
if (!holder) return false; \
|
||||
\
|
||||
@@ -64,8 +69,10 @@
|
||||
if (queue_itr == m_queryQueues.end()) return false; \
|
||||
}
|
||||
|
||||
|
||||
// -- Query / member --
|
||||
|
||||
|
||||
template<class Class>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1, typename ParamType2>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
// -- Query / static --
|
||||
|
||||
|
||||
template<typename ParamType1>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
template<typename ParamType1, typename ParamType2>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
template<typename ParamType1, typename ParamType2, typename ParamType3>
|
||||
bool
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
// -- PQuery / member --
|
||||
|
||||
|
||||
template<class Class>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1, typename ParamType2>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
// -- PQuery / static --
|
||||
|
||||
|
||||
template<typename ParamType1>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
template<typename ParamType1, typename ParamType2>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
template<typename ParamType1, typename ParamType2, typename ParamType3>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
// -- QueryHolder --
|
||||
|
||||
|
||||
template<class Class>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
template<class Class, typename ParamType1>
|
||||
bool
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
#undef ASYNC_QUERY_BODY
|
||||
#undef ASYNC_PQUERY_BODY
|
||||
#undef ASYNC_DELAYHOLDER_BODY
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ size_t DatabaseMysql::db_count = 0;
|
||||
DatabaseMysql::DatabaseMysql() : Database(), mMysql(0)
|
||||
{
|
||||
// before first connection
|
||||
if( db_count++ == 0 )
|
||||
if (db_count++ == 0)
|
||||
{
|
||||
// Mysql Library Init
|
||||
mysql_library_init(-1, NULL, NULL);
|
||||
@@ -65,15 +65,14 @@ DatabaseMysql::~DatabaseMysql()
|
||||
if (mMysql)
|
||||
mysql_close(mMysql);
|
||||
|
||||
//Free Mysql library pointers for last ~DB
|
||||
if(--db_count == 0)
|
||||
// Free Mysql library pointers for last ~DB
|
||||
if (--db_count == 0)
|
||||
mysql_library_end();
|
||||
}
|
||||
|
||||
bool DatabaseMysql::Initialize(const char *infoString)
|
||||
{
|
||||
|
||||
if(!Database::Initialize(infoString))
|
||||
if (!Database::Initialize(infoString))
|
||||
return false;
|
||||
|
||||
tranThread = NULL;
|
||||
@@ -81,7 +80,7 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
||||
mysqlInit = mysql_init(NULL);
|
||||
if (!mysqlInit)
|
||||
{
|
||||
sLog.outError( "Could not initialize Mysql connection" );
|
||||
sLog.outError("Could not initialize Mysql connection");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -97,23 +96,23 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
||||
|
||||
iter = tokens.begin();
|
||||
|
||||
if(iter != tokens.end())
|
||||
if (iter != tokens.end())
|
||||
host = *iter++;
|
||||
if(iter != tokens.end())
|
||||
if (iter != tokens.end())
|
||||
port_or_socket = *iter++;
|
||||
if(iter != tokens.end())
|
||||
if (iter != tokens.end())
|
||||
user = *iter++;
|
||||
if(iter != tokens.end())
|
||||
if (iter != tokens.end())
|
||||
password = *iter++;
|
||||
if(iter != tokens.end())
|
||||
if (iter != tokens.end())
|
||||
database = *iter++;
|
||||
|
||||
mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8");
|
||||
mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8");
|
||||
#ifdef WIN32
|
||||
if(host==".") // named pipe use option (Windows)
|
||||
if (host==".") // named pipe use option (Windows)
|
||||
{
|
||||
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;
|
||||
unix_socket = 0;
|
||||
}
|
||||
@@ -123,10 +122,10 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
||||
unix_socket = 0;
|
||||
}
|
||||
#else
|
||||
if(host==".") // socket use option (Unix/Linux)
|
||||
if (host==".") // socket use option (Unix/Linux)
|
||||
{
|
||||
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";
|
||||
port = 0;
|
||||
unix_socket = port_or_socket.c_str();
|
||||
@@ -143,24 +142,14 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
||||
|
||||
if (mMysql)
|
||||
{
|
||||
sLog.outDetail( "Connected to MySQL database at %s",
|
||||
host.c_str());
|
||||
sLog.outString( "MySQL client library: %s", mysql_get_client_info());
|
||||
sLog.outString( "MySQL server ver: %s ", mysql_get_server_info( mMysql));
|
||||
sLog.outDetail("Connected to MySQL database at %s", host.c_str());
|
||||
sLog.outString("MySQL client library: %s", mysql_get_client_info());
|
||||
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))
|
||||
sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1");
|
||||
else
|
||||
sLog.outDetail("AUTOCOMMIT NOT SET TO 1");
|
||||
/*-------------------------------------*/
|
||||
|
||||
// set connection properties to UTF8 to properly handle locales for different
|
||||
// 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
|
||||
my_bool my_true = (my_bool)1;
|
||||
if (mysql_options(mMysql, MYSQL_OPT_RECONNECT, &my_true))
|
||||
{
|
||||
sLog.outDetail("Failed to turn on MYSQL_OPT_RECONNECT.");
|
||||
}
|
||||
else
|
||||
{
|
||||
sLog.outDetail("Successfully turned on MYSQL_OPT_RECONNECT.");
|
||||
}
|
||||
#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."
|
||||
#endif
|
||||
@@ -185,8 +170,7 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
||||
}
|
||||
else
|
||||
{
|
||||
sLog.outError( "Could not connect to MySQL database at %s: %s\n",
|
||||
host.c_str(),mysql_error(mysqlInit));
|
||||
sLog.outError("Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit));
|
||||
mysql_close(mysqlInit);
|
||||
return false;
|
||||
}
|
||||
@@ -203,9 +187,9 @@ bool DatabaseMysql::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **p
|
||||
#ifdef TRINITY_DEBUG
|
||||
uint32 _s = getMSTime();
|
||||
#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));
|
||||
return false;
|
||||
}
|
||||
@@ -219,7 +203,6 @@ bool DatabaseMysql::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **p
|
||||
*pResult = mysql_store_result(mMysql);
|
||||
*pRowCount = mysql_affected_rows(mMysql);
|
||||
*pFieldCount = mysql_field_count(mMysql);
|
||||
// end guarded block
|
||||
}
|
||||
|
||||
if (!*pResult )
|
||||
@@ -242,7 +225,7 @@ QueryResult_AutoPtr DatabaseMysql::Query(const char *sql)
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
|
||||
if (!_Query(sql, &result, &fields, &rowCount, &fieldCount))
|
||||
return QueryResult_AutoPtr(NULL);
|
||||
|
||||
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
|
||||
@@ -259,18 +242,18 @@ QueryNamedResult* DatabaseMysql::QueryNamed(const char *sql)
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
|
||||
if (!_Query(sql, &result, &fields, &rowCount, &fieldCount))
|
||||
return NULL;
|
||||
|
||||
QueryFieldNames names(fieldCount);
|
||||
for (uint32 i = 0; i < fieldCount; i++)
|
||||
names[i] = fields[i].name;
|
||||
names[i] = fields[i].name;
|
||||
|
||||
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
|
||||
|
||||
queryResult->NextRow();
|
||||
|
||||
return new QueryNamedResult(queryResult,names);
|
||||
return new QueryNamedResult(queryResult, names);
|
||||
}
|
||||
|
||||
bool DatabaseMysql::Execute(const char *sql)
|
||||
@@ -279,19 +262,15 @@ bool DatabaseMysql::Execute(const char *sql)
|
||||
return false;
|
||||
|
||||
// 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
|
||||
TransactionQueues::iterator i = m_tranQueues.find(tranThread);
|
||||
if (i != m_tranQueues.end() && i->second != NULL)
|
||||
{ // Statement for transaction
|
||||
i->second->DelayExecute(sql);
|
||||
}
|
||||
i->second->DelayExecute(sql); // Statement for transaction
|
||||
else
|
||||
{
|
||||
// Simple sql statement
|
||||
m_threadBody->Delay(new SqlStatement(sql));
|
||||
}
|
||||
m_threadBody->Delay(new SqlStatement(sql)); // Simple sql statement
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -308,7 +287,7 @@ bool DatabaseMysql::DirectExecute(const char* sql)
|
||||
#ifdef TRINITY_DEBUG
|
||||
uint32 _s = getMSTime();
|
||||
#endif
|
||||
if(mysql_query(mMysql, sql))
|
||||
if (mysql_query(mMysql, sql))
|
||||
{
|
||||
sLog.outErrorDb("SQL: %s", sql);
|
||||
sLog.outErrorDb("SQL ERROR: %s", mysql_error(mMysql));
|
||||
@@ -317,10 +296,9 @@ bool DatabaseMysql::DirectExecute(const char* sql)
|
||||
else
|
||||
{
|
||||
#ifdef TRINITY_DEBUG
|
||||
sLog.outDebug("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql );
|
||||
sLog.outDebug("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql);
|
||||
#endif
|
||||
}
|
||||
// end guarded block
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -335,9 +313,8 @@ bool DatabaseMysql::_TransactionCmd(const char *sql)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUG_LOG("SQL: %s", sql);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -383,6 +360,7 @@ bool DatabaseMysql::CommitTransaction()
|
||||
{
|
||||
if (tranThread != ACE_Based::Thread::current())
|
||||
return false;
|
||||
|
||||
bool _res = _TransactionCmd("COMMIT");
|
||||
tranThread = NULL;
|
||||
mMutex.release();
|
||||
@@ -411,6 +389,7 @@ bool DatabaseMysql::RollbackTransaction()
|
||||
{
|
||||
if (tranThread != ACE_Based::Thread::current())
|
||||
return false;
|
||||
|
||||
bool _res = _TransactionCmd("ROLLBACK");
|
||||
tranThread = NULL;
|
||||
mMutex.release();
|
||||
@@ -424,6 +403,7 @@ bool DatabaseMysql::RollbackTransaction()
|
||||
delete i->second;
|
||||
i->second = NULL;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -446,7 +426,8 @@ void DatabaseMysql::InitDelayThread()
|
||||
|
||||
void DatabaseMysql::HaltDelayThread()
|
||||
{
|
||||
if (!m_threadBody || !m_delayThread) return;
|
||||
if (!m_threadBody || !m_delayThread)
|
||||
return;
|
||||
|
||||
m_threadBody->Stop(); //Stop event
|
||||
m_delayThread->wait(); //Wait for flush to DB
|
||||
@@ -455,4 +436,3 @@ void DatabaseMysql::HaltDelayThread()
|
||||
m_threadBody = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -50,12 +50,14 @@ mType(type)
|
||||
|
||||
Field::~Field()
|
||||
{
|
||||
if(mValue) delete [] mValue;
|
||||
if (mValue)
|
||||
delete [] mValue;
|
||||
}
|
||||
|
||||
void Field::SetValue(const char *value)
|
||||
{
|
||||
if(mValue) delete [] mValue;
|
||||
if (mValue)
|
||||
delete [] mValue;
|
||||
|
||||
if (value)
|
||||
{
|
||||
@@ -65,4 +67,3 @@ void Field::SetValue(const char *value)
|
||||
else
|
||||
mValue = NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ QueryResultMysql::QueryResultMysql(MYSQL_RES *result, MYSQL_FIELD *fields, uint6
|
||||
ASSERT(mCurrentRow);
|
||||
|
||||
for (uint32 i = 0; i < mFieldCount; i++)
|
||||
mCurrentRow[i].SetType(ConvertNativeType(fields[i].type));
|
||||
mCurrentRow[i].SetType(ConvertNativeType(fields[i].type));
|
||||
}
|
||||
|
||||
QueryResultMysql::~QueryResultMysql()
|
||||
@@ -105,4 +105,3 @@ enum Field::DataTypes QueryResultMysql::ConvertNativeType(enum_field_types mysql
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -48,4 +48,3 @@ class QueryResultMysql : public QueryResult
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
@@ -33,14 +33,14 @@ void SqlDelayThread::run()
|
||||
#endif
|
||||
|
||||
SqlAsyncTask * s = NULL;
|
||||
// Lets wait for next async task no more than 2 secs
|
||||
|
||||
ACE_Time_Value _time(2);
|
||||
while (m_running)
|
||||
{
|
||||
// if the running state gets turned off while sleeping
|
||||
// empty the queue before exiting
|
||||
s = dynamic_cast<SqlAsyncTask*> (m_sqlQueue.dequeue());
|
||||
if(s)
|
||||
if (s)
|
||||
{
|
||||
s->call();
|
||||
delete s;
|
||||
|
||||
@@ -48,4 +48,3 @@ class SqlDelayThread : public ACE_Based::Runnable
|
||||
virtual void run(); ///< Main Thread loop
|
||||
};
|
||||
#endif //__SQLDELAYTHREAD_H
|
||||
|
||||
|
||||
@@ -37,16 +37,16 @@ void SqlTransaction::Execute(Database *db)
|
||||
return;
|
||||
|
||||
db->DirectExecute("START TRANSACTION");
|
||||
while(!m_queue.empty())
|
||||
while (!m_queue.empty())
|
||||
{
|
||||
char const *sql = m_queue.front();
|
||||
m_queue.pop();
|
||||
|
||||
if(!db->DirectExecute(sql))
|
||||
if (!db->DirectExecute(sql))
|
||||
{
|
||||
free((void*)const_cast<char*>(sql));
|
||||
db->DirectExecute("ROLLBACK");
|
||||
while(!m_queue.empty())
|
||||
while (!m_queue.empty())
|
||||
{
|
||||
free((void*)const_cast<char*>(m_queue.front()));
|
||||
m_queue.pop();
|
||||
@@ -56,6 +56,7 @@ void SqlTransaction::Execute(Database *db)
|
||||
|
||||
free((void*)const_cast<char*>(sql));
|
||||
}
|
||||
|
||||
db->DirectExecute("COMMIT");
|
||||
}
|
||||
|
||||
@@ -63,8 +64,9 @@ void SqlTransaction::Execute(Database *db)
|
||||
|
||||
void SqlQuery::Execute(Database *db)
|
||||
{
|
||||
if(!m_callback || !m_queue)
|
||||
if (!m_callback || !m_queue)
|
||||
return;
|
||||
|
||||
/// execute the query and store the result in the callback
|
||||
m_callback->SetResult(db->Query(m_sql));
|
||||
/// 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)
|
||||
{
|
||||
if(!callback || !thread || !queue)
|
||||
if (!callback || !thread || !queue)
|
||||
return false;
|
||||
|
||||
/// 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)
|
||||
{
|
||||
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);
|
||||
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])",
|
||||
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, ...)
|
||||
{
|
||||
if(!format)
|
||||
if (!format)
|
||||
{
|
||||
sLog.outError("Query (index: %u) is empty.",index);
|
||||
return false;
|
||||
@@ -125,10 +127,10 @@ bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
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);
|
||||
|
||||
if(res==-1)
|
||||
if (res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return false;
|
||||
@@ -139,10 +141,10 @@ bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
|
||||
|
||||
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
|
||||
if(m_queries[index].first != NULL)
|
||||
if (m_queries[index].first != NULL)
|
||||
{
|
||||
free((void*)(const_cast<char*>(m_queries[index].first)));
|
||||
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)
|
||||
{
|
||||
/// store the result in the holder
|
||||
if(index < m_queries.size())
|
||||
if (index < m_queries.size())
|
||||
m_queries[index].second = result;
|
||||
}
|
||||
|
||||
@@ -167,7 +169,7 @@ SqlQueryHolder::~SqlQueryHolder()
|
||||
{
|
||||
/// if the result was never used, free the resources
|
||||
/// 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)));
|
||||
}
|
||||
}
|
||||
@@ -180,7 +182,7 @@ void SqlQueryHolder::SetSize(size_t size)
|
||||
|
||||
void SqlQueryHolderEx::Execute(Database *db)
|
||||
{
|
||||
if(!m_holder || !m_callback || !m_queue)
|
||||
if (!m_holder || !m_callback || !m_queue)
|
||||
return;
|
||||
|
||||
/// we can do this, we are friends
|
||||
@@ -196,4 +198,3 @@ void SqlQueryHolderEx::Execute(Database *db)
|
||||
/// sync with the caller thread
|
||||
m_queue->add(m_callback);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user