diff options
Diffstat (limited to 'src/shared/Database')
| -rw-r--r-- | src/shared/Database/CMakeLists.txt | 30 | ||||
| -rw-r--r-- | src/shared/Database/DBCFileLoader.cpp | 270 | ||||
| -rw-r--r-- | src/shared/Database/DBCFileLoader.h | 110 | ||||
| -rw-r--r-- | src/shared/Database/DBCStore.h | 265 | ||||
| -rw-r--r-- | src/shared/Database/Database.cpp | 657 | ||||
| -rw-r--r-- | src/shared/Database/Database.h | 155 | ||||
| -rw-r--r-- | src/shared/Database/DatabaseEnv.h | 43 | ||||
| -rw-r--r-- | src/shared/Database/DatabaseImpl.h | 234 | ||||
| -rw-r--r-- | src/shared/Database/Field.cpp | 77 | ||||
| -rw-r--r-- | src/shared/Database/Field.h | 89 | ||||
| -rw-r--r-- | src/shared/Database/QueryResult.cpp | 105 | ||||
| -rw-r--r-- | src/shared/Database/QueryResult.h | 102 | ||||
| -rw-r--r-- | src/shared/Database/SQLStorage.cpp | 77 | ||||
| -rw-r--r-- | src/shared/Database/SQLStorage.h | 118 | ||||
| -rw-r--r-- | src/shared/Database/SQLStorageImpl.h | 210 | ||||
| -rw-r--r-- | src/shared/Database/SqlDelayThread.cpp | 61 | ||||
| -rw-r--r-- | src/shared/Database/SqlDelayThread.h | 50 | ||||
| -rw-r--r-- | src/shared/Database/SqlOperations.cpp | 209 | ||||
| -rw-r--r-- | src/shared/Database/SqlOperations.h | 168 |
19 files changed, 0 insertions, 3030 deletions
diff --git a/src/shared/Database/CMakeLists.txt b/src/shared/Database/CMakeLists.txt deleted file mode 100644 index 405a5f89a57..00000000000 --- a/src/shared/Database/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -SET(trinitydatabase_STAT_SRCS - DBCFileLoader.cpp - DBCFileLoader.h - DBCStore.h - Database.cpp - Database.h - DatabaseEnv.h - DatabaseImpl.h - Field.cpp - Field.h - QueryResult.cpp - QueryResult.h - SQLStorage.cpp - SQLStorage.h - SqlDelayThread.cpp - SqlDelayThread.h - SqlOperations.cpp - SqlOperations.h -) - -include_directories( - ${ACE_INCLUDE_DIR} - ${CMAKE_BINARY_DIR} - ${CMAKE_SOURCE_DIR}/src/shared - ${CMAKE_SOURCE_DIR}/src/shared/Database - ${CMAKE_SOURCE_DIR}/src/framework - ${MYSQL_INCLUDE_DIR} -) - -add_library(trinitydatabase STATIC ${trinitydatabase_STAT_SRCS}) diff --git a/src/shared/Database/DBCFileLoader.cpp b/src/shared/Database/DBCFileLoader.cpp deleted file mode 100644 index bad87a36fce..00000000000 --- a/src/shared/Database/DBCFileLoader.cpp +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "DBCFileLoader.h" - -DBCFileLoader::DBCFileLoader() -{ - data = NULL; - fieldsOffset = NULL; -} - -bool DBCFileLoader::Load(const char *filename, const char *fmt) -{ - uint32 header; - if (data) - { - delete [] data; - data = NULL; - } - - FILE * f = fopen(filename,"rb"); - if (!f) - return false; - - 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 - return false; - - EndianConvert(recordCount); - - 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 - return false; - - EndianConvert(recordSize); - - if (fread(&stringSize,4,1,f)!=1) // String size - return false; - - EndianConvert(stringSize); - - fieldsOffset = new uint32[fieldCount]; - fieldsOffset[0] = 0; - for (uint32 i = 1; i < fieldCount; i++) - { - fieldsOffset[i] = fieldsOffset[i - 1]; - if (fmt[i - 1] == 'b' || fmt[i - 1] == 'X') // byte fields - fieldsOffset[i] += 1; - else // 4 byte fields (int32/float/strings) - fieldsOffset[i] += 4; - } - - data = new unsigned char[recordSize*recordCount+stringSize]; - stringTable = data + recordSize*recordCount; - - if (fread(data,recordSize*recordCount+stringSize,1,f)!=1) - return false; - - fclose(f); - - return true; -} - -DBCFileLoader::~DBCFileLoader() -{ - if (data) - delete [] data; - - if (fieldsOffset) - delete [] fieldsOffset; -} - -DBCFileLoader::Record DBCFileLoader::getRecord(size_t id) -{ - assert(data); - return Record(*this, data + id*recordSize); -} - -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: - case FT_INT: - recordsize+=4; - break; - case FT_STRING: - recordsize+=sizeof(char*); - break; - case FT_SORT: - i=x; - break; - case FT_IND: - i=x; - recordsize+=4; - break; - case FT_BYTE: - recordsize += 1; - break; - } - } - - if (index_pos) - *index_pos = i; - - return recordsize; -} - -char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**& indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char *& sqlDataTable) -{ - /* - format STRING, NA, FLOAT,NA,INT <=> - struct{ - char* field0, - float field1, - int field2 - }entry; - - this func will generate entry[rows] data; - */ - - typedef char * ptr; - if (strlen(format)!=fieldCount) - return NULL; - - //get struct size and index pos - int32 i; - uint32 recordsize=GetFormatRecordSize(format,&i); - - if (i>=0) - { - uint32 maxi=0; - //find max index - for (uint32 y=0; y<recordCount; y++) - { - uint32 ind=getRecord(y).getUInt (i); - if(ind>maxi)maxi=ind; - } - - // If higher index avalible from sql - use it instead of dbcs - if (sqlHighestIndex > maxi) - maxi = sqlHighestIndex; - - ++maxi; - records=maxi; - indexTable=new ptr[maxi]; - memset(indexTable,0,maxi*sizeof(ptr)); - } - else - { - records = recordCount + sqlRecordCount; - indexTable = new ptr[recordCount+ sqlRecordCount]; - } - - char* dataTable= new char[(recordCount + sqlRecordCount)*recordsize]; - - uint32 offset=0; - - for (uint32 y =0; y<recordCount; ++y) - { - if(i>=0) - indexTable[getRecord(y).getUInt(i)]=&dataTable[offset]; - else - indexTable[y]=&dataTable[offset]; - - for (uint32 x=0; x<fieldCount; x++) - { - switch(format[x]) - { - case FT_FLOAT: - *((float*)(&dataTable[offset]))=getRecord(y).getFloat(x); - offset+=4; - break; - case FT_IND: - case FT_INT: - *((uint32*)(&dataTable[offset]))=getRecord(y).getUInt(x); - offset+=4; - break; - case FT_BYTE: - *((uint8*)(&dataTable[offset]))=getRecord(y).getUInt8(x); - offset+=1; - break; - case FT_STRING: - *((char**)(&dataTable[offset]))=NULL; // will be replaces non-empty or "" strings in AutoProduceStrings - offset+=sizeof(char*); - break; - } - } - } - - sqlDataTable = dataTable + offset; - - return dataTable; -} - -char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable) -{ - if (strlen(format)!=fieldCount) - return NULL; - - char* stringPool= new char[stringSize]; - memcpy(stringPool,stringTable,stringSize); - - uint32 offset=0; - - for (uint32 y =0; y<recordCount; y++) - { - for (uint32 x=0; x<fieldCount; x++) - switch(format[x]) - { - case FT_FLOAT: - case FT_IND: - case FT_INT: - offset+=4; - break; - case FT_BYTE: - offset+=1; - break; - case FT_STRING: - // fill only not filled entries - char** slot = (char**)(&dataTable[offset]); - if(!*slot || !**slot) - { - const char * st = getRecord(y).getString(x); - *slot=stringPool+(st-(const char*)stringTable); - } - offset+=sizeof(char*); - break; - } - } - - return stringPool; -} - diff --git a/src/shared/Database/DBCFileLoader.h b/src/shared/Database/DBCFileLoader.h deleted file mode 100644 index a97ab4d60fa..00000000000 --- a/src/shared/Database/DBCFileLoader.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef DBC_FILE_LOADER_H -#define DBC_FILE_LOADER_H -#include "Platform/Define.h" -#include "Utilities/ByteConverter.h" -#include <cassert> - -enum -{ - FT_NA='x', //not used or unknown, 4 byte size - FT_NA_BYTE='X', //not used or unknown, byte - FT_STRING='s', //char* - FT_FLOAT='f', //float - FT_INT='i', //uint32 - FT_BYTE='b', //uint8 - FT_SORT='d', //sorted by this field, field is not included - FT_IND='n', //the same,but parsed to data - FT_LOGIC='l', //Logical (boolean) - FT_SQL_PRESENT='p', //Used in sql format to mark column present in sql dbc - FT_SQL_ABSENT='a' //Used in sql format to mark column absent in sql dbc -}; - -class DBCFileLoader -{ - public: - DBCFileLoader(); - ~DBCFileLoader(); - - bool Load(const char *filename, const char *fmt); - - class Record - { - public: - float getFloat(size_t field) const - { - assert(field < file.fieldCount); - float val = *reinterpret_cast<float*>(offset+file.GetOffset(field)); - EndianConvert(val); - return val; - } - uint32 getUInt(size_t field) const - { - assert(field < file.fieldCount); - uint32 val = *reinterpret_cast<uint32*>(offset+file.GetOffset(field)); - EndianConvert(val); - return val; - } - uint8 getUInt8(size_t field) const - { - assert(field < file.fieldCount); - return *reinterpret_cast<uint8*>(offset+file.GetOffset(field)); - } - - const char *getString(size_t field) const - { - assert(field < file.fieldCount); - size_t stringOffset = getUInt(field); - assert(stringOffset < file.stringSize); - return reinterpret_cast<char*>(file.stringTable + stringOffset); - } - - private: - Record(DBCFileLoader &file_, unsigned char *offset_): offset(offset_), file(file_) {} - unsigned char *offset; - DBCFileLoader &file; - - friend class DBCFileLoader; - - }; - - // Get record by id - Record getRecord(size_t id); - /// Get begin iterator over records - - uint32 GetNumRows() const { return recordCount; } - uint32 GetRowSize() const { return recordSize; } - uint32 GetCols() const { return fieldCount; } - uint32 GetOffset(size_t id) const { return (fieldsOffset != NULL && id < fieldCount) ? fieldsOffset[id] : 0; } - bool IsLoaded() { return data != NULL; } - char* AutoProduceData(const char* fmt, uint32& count, char**& indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char *& sqlDataTable); - char* AutoProduceStrings(const char* fmt, char* dataTable); - static uint32 GetFormatRecordSize(const char * format, int32 * index_pos = NULL); - private: - - uint32 recordSize; - uint32 recordCount; - uint32 fieldCount; - uint32 stringSize; - uint32 *fieldsOffset; - unsigned char *data; - unsigned char *stringTable; -}; -#endif diff --git a/src/shared/Database/DBCStore.h b/src/shared/Database/DBCStore.h deleted file mode 100644 index 61e2f7a6d06..00000000000 --- a/src/shared/Database/DBCStore.h +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef DBCSTORE_H -#define DBCSTORE_H - -#include "DBCFileLoader.h" -#include "Log.h" - -struct SqlDbc -{ - const std::string * formatString; - const std::string * indexName; - std::string sqlTableName; - int32 indexPos; - int32 sqlIndexPos; - SqlDbc(const std::string * _filename, const std::string * _format, const std::string * _idname, const char * fmt) - :formatString(_format),sqlIndexPos(0), indexName (_idname) - { - // Convert dbc file name to sql table name - sqlTableName = *_filename; - for (uint32 i = 0; i< sqlTableName.size(); ++i) - { - if (isalpha(sqlTableName[i])) - sqlTableName[i] = tolower(sqlTableName[i]); - else if (sqlTableName[i] == '.') - sqlTableName[i] = '_'; - } - - // Get sql index position - DBCFileLoader::GetFormatRecordSize(fmt, &indexPos); - if (indexPos>=0) - { - for (int32 x=0; x < formatString->size(); x++) - { - // Count only fields present in sql - if ((*formatString)[x] == FT_SQL_PRESENT) - { - if (x == indexPos) - break; - ++sqlIndexPos; - } - } - } - } -}; - -template<class T> -class DBCStorage -{ - typedef std::list<char*> StringPoolList; - public: - explicit DBCStorage(const char *f) : nCount(0), fieldCount(0), fmt(f), indexTable(NULL), m_dataTable(NULL) { } - ~DBCStorage() { Clear(); } - - T const* LookupEntry(uint32 id) const { return (id>=nCount)?NULL:indexTable[id]; } - uint32 GetNumRows() const { return nCount; } - char const* GetFormat() const { return fmt; } - uint32 GetFieldCount() const { return fieldCount; } - - bool Load(char const* fn, SqlDbc * sql) - { - DBCFileLoader dbc; - // Check if load was sucessful, only then continue - if (!dbc.Load(fn, fmt)) - return false; - - uint32 sqlRecordCount = 0; - uint32 sqlHighestIndex = 0; - Field *fields = NULL; - QueryResult_AutoPtr result = QueryResult_AutoPtr(NULL); - // Load data from sql - if (sql) - { - std::string query = "SELECT * FROM " + sql->sqlTableName; - if (sql->indexPos >= 0) - query +=" ORDER BY + " + *sql->indexName + " DESC"; - query += ";"; - - result = WorldDatabase.Query(query.c_str()); - if (result) - { - sqlRecordCount = result->GetRowCount(); - if (sql->indexPos >= 0) - { - fields = result->Fetch(); - sqlHighestIndex = fields[sql->sqlIndexPos].GetUInt32(); - } - // Check if sql index pos is valid - if (int32(result->GetFieldCount()-1) < sql->sqlIndexPos) - { - sLog.outError("Invalid index pos for dbc:'%s'", sql->sqlTableName.c_str()); - return false; - } - } - } - char * sqlDataTable; - fieldCount = dbc.GetCols(); - m_dataTable = (T*)dbc.AutoProduceData(fmt,nCount,(char**&)indexTable, sqlRecordCount, sqlHighestIndex, sqlDataTable); - - m_stringPoolList.push_back(dbc.AutoProduceStrings(fmt,(char*)m_dataTable)); - - // Insert sql data into arrays - if (result) - { - if (indexTable) - { - uint32 offset = 0; - uint32 rowIndex = dbc.GetNumRows(); - do - { - if (!fields) - fields = result->Fetch(); - - if(sql->indexPos >= 0) - { - uint32 id = fields[sql->sqlIndexPos].GetUInt32(); - if (indexTable[id]) - { - sLog.outError("Index %d already exists in dbc:'%s'", id, sql->sqlTableName.c_str()); - return false; - } - indexTable[id]=(T*)&sqlDataTable[offset]; - } - else - indexTable[rowIndex]=(T*)&sqlDataTable[offset]; - uint32 columnNumber = 0; - uint32 sqlColumnNumber = 0; - - for (; columnNumber < sql->formatString->size(); ++columnNumber) - { - if ((*sql->formatString)[columnNumber] == FT_SQL_ABSENT) - { - switch(fmt[columnNumber]) - { - case FT_FLOAT: - *((float*)(&sqlDataTable[offset]))= 0.0f; - offset+=4; - break; - case FT_IND: - case FT_INT: - *((uint32*)(&sqlDataTable[offset]))=uint32(0); - offset+=4; - break; - case FT_BYTE: - *((uint8*)(&sqlDataTable[offset]))=uint8(0); - offset+=1; - break; - case FT_STRING: - // Beginning of the pool - empty string - *((char**)(&sqlDataTable[offset]))=m_stringPoolList.back(); - offset+=sizeof(char*); - break; - } - } - else if ((*sql->formatString)[columnNumber] == FT_SQL_PRESENT) - { - bool validSqlColumn = true; - switch(fmt[columnNumber]) - { - case FT_FLOAT: - *((float*)(&sqlDataTable[offset]))=fields[sqlColumnNumber].GetFloat(); - offset+=4; - break; - case FT_IND: - case FT_INT: - *((uint32*)(&sqlDataTable[offset]))=fields[sqlColumnNumber].GetUInt32(); - offset+=4; - break; - case FT_BYTE: - *((uint8*)(&sqlDataTable[offset]))=fields[sqlColumnNumber].GetUInt8(); - offset+=1; - break; - case FT_STRING: - sLog.outError("Unsupported data type in table '%s' at char %d", sql->sqlTableName.c_str(), columnNumber); - return false; - case FT_SORT: - break; - default: - validSqlColumn = false; - } - if (validSqlColumn && (columnNumber != (sql->formatString->size()-1))) - sqlColumnNumber++; - } - else - { - sLog.outError("Incorrect sql format string '%s' at char %d", sql->sqlTableName.c_str(), columnNumber); - return false; - } - } - if (sqlColumnNumber != (result->GetFieldCount()-1)) - { - sLog.outError("SQL and DBC format strings are not matching for table: '%s'", sql->sqlTableName.c_str()); - return false; - } - - fields = NULL; - ++rowIndex; - }while (result->NextRow()); - } - } - - // error in dbc file at loading if NULL - return indexTable!=NULL; - } - - bool LoadStringsFrom(char const* fn) - { - // DBC must be already loaded using Load - if(!indexTable) - return false; - - DBCFileLoader dbc; - // Check if load was successful, only then continue - if(!dbc.Load(fn, fmt)) - return false; - - m_stringPoolList.push_back(dbc.AutoProduceStrings(fmt,(char*)m_dataTable)); - - return true; - } - - void Clear() - { - if (!indexTable) - return; - - delete[] ((char*)indexTable); - indexTable = NULL; - delete[] ((char*)m_dataTable); - m_dataTable = NULL; - - while(!m_stringPoolList.empty()) - { - delete[] m_stringPoolList.front(); - m_stringPoolList.pop_front(); - } - nCount = 0; - } - - private: - char const* fmt; - uint32 nCount; - uint32 fieldCount; - T** indexTable; - T* m_dataTable; - StringPoolList m_stringPoolList; -}; - -#endif diff --git a/src/shared/Database/Database.cpp b/src/shared/Database/Database.cpp deleted file mode 100644 index 379388d7997..00000000000 --- a/src/shared/Database/Database.cpp +++ /dev/null @@ -1,657 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "DatabaseEnv.h" -#include "Config/ConfigEnv.h" - -#include "Common.h" -#include "../../game/UpdateFields.h" - -#include "Util.h" -#include "Policies/SingletonImp.h" -#include "Platform/Define.h" -#include "Threading.h" -#include "Database/SqlDelayThread.h" -#include "Database/SqlOperations.h" -#include "Timer.h" - - -#include <ctime> -#include <iostream> -#include <fstream> - -size_t Database::db_count = 0; - -Database::Database() : mMysql(NULL) -{ - // before first connection - if (db_count++ == 0) - { - // Mysql Library Init - mysql_library_init(-1, NULL, NULL); - - if (!mysql_thread_safe()) - { - sLog.outError("FATAL ERROR: Used MySQL library isn't thread-safe."); - exit(1); - } - } -} - -Database::~Database() -{ - if (m_delayThread) - HaltDelayThread(); - - if (mMysql) - mysql_close(mMysql); - - // Free Mysql library pointers for last ~DB - if (--db_count == 0) - mysql_library_end(); -} - -bool Database::Initialize(const char *infoString) -{ - // Enable logging of SQL commands (usally only GM commands) - // (See method: PExecuteLog) - m_logSQL = sConfig.GetBoolDefault("LogSQL", false); - m_logsDir = sConfig.GetStringDefault("LogsDir",""); - if (!m_logsDir.empty()) - { - if ((m_logsDir.at(m_logsDir.length()-1)!='/') && (m_logsDir.at(m_logsDir.length()-1)!='\\')) - m_logsDir.append("/"); - } - - tranThread = NULL; - MYSQL *mysqlInit; - mysqlInit = mysql_init(NULL); - if (!mysqlInit) - { - sLog.outError("Could not initialize Mysql connection"); - return false; - } - - InitDelayThread(); - - Tokens tokens = StrSplit(infoString, ";"); - - Tokens::iterator iter; - - std::string host, port_or_socket, user, password, database; - int port; - char const* unix_socket; - - iter = tokens.begin(); - - if (iter != tokens.end()) - host = *iter++; - if (iter != tokens.end()) - port_or_socket = *iter++; - if (iter != tokens.end()) - user = *iter++; - if (iter != tokens.end()) - password = *iter++; - if (iter != tokens.end()) - database = *iter++; - - mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8"); - #ifdef WIN32 - if (host==".") // named pipe use option (Windows) - { - unsigned int opt = MYSQL_PROTOCOL_PIPE; - mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); - port = 0; - unix_socket = 0; - } - else // generic case - { - port = atoi(port_or_socket.c_str()); - unix_socket = 0; - } - #else - if (host==".") // socket use option (Unix/Linux) - { - unsigned int opt = MYSQL_PROTOCOL_SOCKET; - mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); - host = "localhost"; - port = 0; - unix_socket = port_or_socket.c_str(); - } - else // generic case - { - port = atoi(port_or_socket.c_str()); - unix_socket = 0; - } - #endif - - mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(), - password.c_str(), database.c_str(), port, unix_socket, 0); - - 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)); - - 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 - PExecute("SET NAMES `utf8`"); - PExecute("SET CHARACTER SET `utf8`"); - - #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 - return true; - } - else - { - sLog.outError("Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit)); - mysql_close(mysqlInit); - return false; - } -} - -void Database::ThreadStart() -{ - mysql_thread_init(); -} - -void Database::ThreadEnd() -{ - mysql_thread_end(); -} - -void Database::escape_string(std::string& str) -{ - if (str.empty()) - return; - - char* buf = new char[str.size()*2+1]; - escape_string(buf,str.c_str(),str.size()); - str = buf; - delete[] buf; -} - -unsigned long Database::escape_string(char *to, const char *from, unsigned long length) -{ - if (!mMysql || !to || !from || !length) - return 0; - - return(mysql_real_escape_string(mMysql, to, from, length)); -} - - -bool Database::PExecuteLog(const char * format,...) -{ - if (!format) - return false; - - va_list ap; - char szQuery [MAX_QUERY_LEN]; - va_start(ap, format); - int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return false; - } - - 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); - - FILE* log_file; - std::string logsDir_fname = m_logsDir+fName; - log_file = fopen(logsDir_fname.c_str(), "a"); - if (log_file) - { - fprintf(log_file, "%s;\n", szQuery); - fclose(log_file); - } - else - { - // The file could not be opened - sLog.outError("SQL-Logging is disabled - Log file for the SQL commands could not be openend: %s",fName); - } - } - - return Execute(szQuery); -} - -void Database::SetResultQueue(SqlResultQueue * queue) -{ - m_queryQueues[ACE_Based::Thread::current()] = queue; -} - -bool Database::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount) -{ - if (!mMysql) - return 0; - - { - // guarded block for thread-safe mySQL request - ACE_Guard<ACE_Thread_Mutex> query_connection_guard(mMutex); - #ifdef TRINITY_DEBUG - uint32 _s = getMSTime(); - #endif - if (mysql_query(mMysql, sql)) - { - sLog.outErrorDb("SQL: %s", sql); - sLog.outErrorDb("query ERROR: %s", mysql_error(mMysql)); - return false; - } - else - { - #ifdef TRINITY_DEBUG - sLog.outDebug("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql ); - #endif - } - - *pResult = mysql_store_result(mMysql); - *pRowCount = mysql_affected_rows(mMysql); - *pFieldCount = mysql_field_count(mMysql); - } - - if (!*pResult ) - return false; - - if (!*pRowCount) - { - mysql_free_result(*pResult); - return false; - } - - *pFields = mysql_fetch_fields(*pResult); - return true; -} - -QueryResult_AutoPtr Database::Query(const char *sql) -{ - MYSQL_RES *result = NULL; - MYSQL_FIELD *fields = NULL; - uint64 rowCount = 0; - uint32 fieldCount = 0; - - if (!_Query(sql, &result, &fields, &rowCount, &fieldCount)) - return QueryResult_AutoPtr(NULL); - - QueryResult *queryResult = new QueryResult(result, fields, rowCount, fieldCount); - - queryResult->NextRow(); - - return QueryResult_AutoPtr(queryResult); -} - -QueryResult_AutoPtr Database::PQuery(const char *format,...) -{ - 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); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return QueryResult_AutoPtr(NULL); - } - - return Query(szQuery); -} - -QueryNamedResult* Database::QueryNamed(const char *sql) -{ - MYSQL_RES *result = NULL; - MYSQL_FIELD *fields = NULL; - uint64 rowCount = 0; - uint32 fieldCount = 0; - - if (!_Query(sql, &result, &fields, &rowCount, &fieldCount)) - return NULL; - - QueryFieldNames names(fieldCount); - for (uint32 i = 0; i < fieldCount; i++) - names[i] = fields[i].name; - - QueryResult *queryResult = new QueryResult(result, fields, rowCount, fieldCount); - - queryResult->NextRow(); - - return new QueryNamedResult(queryResult, names); -} - -QueryNamedResult* Database::PQueryNamed(const char *format,...) -{ - 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); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return false; - } - - return QueryNamed(szQuery); -} - -bool Database::Execute(const char *sql) -{ - if (!mMysql) - return false; - - // don't use queued execution if it has not been initialized - if (!m_threadBody) - return DirectExecute(sql); - - nMutex.acquire(); - tranThread = ACE_Based::Thread::current(); // owner of this transaction - TransactionQueues::iterator i = m_tranQueues.find(tranThread); - if (i != m_tranQueues.end() && i->second != NULL) - i->second->DelayExecute(sql); // Statement for transaction - else - m_threadBody->Delay(new SqlStatement(sql)); // Simple sql statement - - nMutex.release(); - return true; -} - -bool Database::PExecute(const char * format,...) -{ - if (!format) - return false; - - va_list ap; - char szQuery [MAX_QUERY_LEN]; - va_start(ap, format); - int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return false; - } - - return Execute(szQuery); -} - -bool Database::_UpdateDataBlobValue(const uint32 guid, const uint32 field, const int32 value) -{ - return PExecute( - "UPDATE characters SET data=" - "CONCAT(SUBSTRING_INDEX(`data`,' ',%u),' '," - "GREATEST(SUBSTRING_INDEX(SUBSTRING_INDEX(`data`,' ',%u),' ',-1)+%i,0)," - "' ',SUBSTRING_INDEX(`data`,' ',%i)) WHERE guid=%u", - field, field+1, value, -int32(PLAYER_END-field), guid); -} - -bool Database::_SetDataBlobValue(const uint32 guid, const uint32 field, const uint32 value) -{ - return PExecute( - "UPDATE characters SET data=" - "CONCAT(SUBSTRING_INDEX(`data`,' ',%u),' '," - "%u,' ',SUBSTRING_INDEX(`data`,' ',%i)) WHERE guid=%u", - field, value, -int32(PLAYER_END-field), guid); -} - -bool Database::DirectExecute(const char* sql) -{ - if (!mMysql) - return false; - - { - // guarded block for thread-safe mySQL request - ACE_Guard<ACE_Thread_Mutex> query_connection_guard(mMutex); - - #ifdef TRINITY_DEBUG - uint32 _s = getMSTime(); - #endif - if (mysql_query(mMysql, sql)) - { - sLog.outErrorDb("SQL: %s", sql); - sLog.outErrorDb("SQL ERROR: %s", mysql_error(mMysql)); - return false; - } - else - { - #ifdef TRINITY_DEBUG - sLog.outDebug("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql); - #endif - } - } - - return true; -} - -bool Database::DirectPExecute(const char * format,...) -{ - if (!format) - return false; - - va_list ap; - char szQuery [MAX_QUERY_LEN]; - va_start(ap, format); - int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return false; - } - - return DirectExecute(szQuery); -} - -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) - 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) - { - 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_") - { - reqName = *itr; - break; - } - } - - delete result2; - - 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); - } - else - sLog.outErrorDb("Table `%s` fields list query fail but expected have `%s`! No records in `%s`?",table_name,required_name,table_name); - - return false; -} - -bool Database::_TransactionCmd(const char *sql) -{ - if (mysql_query(mMysql, sql)) - { - sLog.outError("SQL: %s", sql); - sLog.outError("SQL ERROR: %s", mysql_error(mMysql)); - return false; - } - else - DEBUG_LOG("SQL: %s", sql); - - return true; -} - -bool Database::BeginTransaction() -{ - if (!mMysql) - return false; - - // don't use queued execution if it has not been initialized - if (!m_threadBody) - { - if (tranThread == ACE_Based::Thread::current()) - return false; // huh? this thread already started transaction - - mMutex.acquire(); - if (!_TransactionCmd("START TRANSACTION")) - { - mMutex.release(); // can't start transaction - return false; - } - return true; // transaction started - } - - nMutex.acquire(); - tranThread = ACE_Based::Thread::current(); // owner of this transaction - TransactionQueues::iterator i = m_tranQueues.find(tranThread); - if (i != m_tranQueues.end() && i->second != NULL) - // If for thread exists queue and also contains transaction - // delete that transaction (not allow trans in trans) - delete i->second; - - m_tranQueues[tranThread] = new SqlTransaction(); - nMutex.release(); - return true; -} - -bool Database::CommitTransaction() -{ - if (!mMysql) - return false; - - bool _res = false; - - // don't use queued execution if it has not been initialized - if (!m_threadBody) - { - if (tranThread != ACE_Based::Thread::current()) - return false; - - _res = _TransactionCmd("COMMIT"); - tranThread = NULL; - mMutex.release(); - return _res; - } - - nMutex.acquire(); - tranThread = ACE_Based::Thread::current(); - TransactionQueues::iterator i = m_tranQueues.find(tranThread); - if (i != m_tranQueues.end() && i->second != NULL) - { - m_threadBody->Delay(i->second); - m_tranQueues.erase(i); - _res = true; - } - nMutex.release(); - return _res; -} - -bool Database::RollbackTransaction() -{ - if (!mMysql) - return false; - - // don't use queued execution if it has not been initialized - if (!m_threadBody) - { - if (tranThread != ACE_Based::Thread::current()) - return false; - - bool _res = _TransactionCmd("ROLLBACK"); - tranThread = NULL; - mMutex.release(); - return _res; - } - - nMutex.acquire(); - tranThread = ACE_Based::Thread::current(); - TransactionQueues::iterator i = m_tranQueues.find(tranThread); - if (i != m_tranQueues.end() && i->second != NULL) - { - delete i->second; - i->second = NULL; - m_tranQueues.erase(i); - } - nMutex.release(); - return true; -} - -void Database::InitDelayThread() -{ - assert(!m_delayThread); - - //New delay thread for delay execute - m_threadBody = new SqlDelayThread(this); // will deleted at m_delayThread delete - m_delayThread = new ACE_Based::Thread(m_threadBody); -} - -void Database::HaltDelayThread() -{ - if (!m_threadBody || !m_delayThread) - return; - - m_threadBody->Stop(); //Stop event - m_delayThread->wait(); //Wait for flush to DB - delete m_delayThread; //This also deletes m_threadBody - m_delayThread = NULL; - m_threadBody = NULL; -} - diff --git a/src/shared/Database/Database.h b/src/shared/Database/Database.h deleted file mode 100644 index 4ad5d29c993..00000000000 --- a/src/shared/Database/Database.h +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef DATABASE_H -#define DATABASE_H - -#include "Threading.h" -#include "Utilities/UnorderedMap.h" -#include "Database/SqlDelayThread.h" -#include "Policies/Singleton.h" -#include "ace/Thread_Mutex.h" -#include "ace/Guard_T.h" - -#ifdef WIN32 -#define FD_SETSIZE 1024 -#include <winsock2.h> -#include <mysql/mysql.h> -#else -#include <mysql.h> -#endif - -class SqlTransaction; -class SqlResultQueue; -class SqlQueryHolder; - -typedef UNORDERED_MAP<ACE_Based::Thread* , SqlTransaction*> TransactionQueues; -typedef UNORDERED_MAP<ACE_Based::Thread* , SqlResultQueue*> QueryQueues; - -#define MAX_QUERY_LEN 32*1024 - -class Database -{ - protected: - TransactionQueues m_tranQueues; ///< Transaction queues from diff. threads - QueryQueues m_queryQueues; ///< Query queues from diff threads - SqlDelayThread* m_threadBody; ///< Pointer to delay sql executer (owned by m_delayThread) - ACE_Based::Thread* m_delayThread; ///< Pointer to executer thread - - public: - - Database(); - ~Database(); - - /*! infoString should be formated like hostname;username;password;database. */ - bool Initialize(const char *infoString); - - void InitDelayThread(); - void HaltDelayThread(); - - QueryResult_AutoPtr Query(const char *sql); - QueryResult_AutoPtr PQuery(const char *format,...) ATTR_PRINTF(2,3); - QueryNamedResult* QueryNamed(const char *sql); - QueryNamedResult* PQueryNamed(const char *format,...) ATTR_PRINTF(2,3); - - /// Async queries and query holders, implemented in DatabaseImpl.h - - // Query / member - template<class Class> - bool AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr), const char *sql); - template<class Class, typename ParamType1> - bool AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *sql); - template<class Class, typename ParamType1, typename ParamType2> - bool AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql); - template<class Class, typename ParamType1, typename ParamType2, typename ParamType3> - bool AsyncQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *sql); - // Query / static - template<typename ParamType1> - bool AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *sql); - template<typename ParamType1, typename ParamType2> - bool AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql); - template<typename ParamType1, typename ParamType2, typename ParamType3> - bool AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *sql); - // PQuery / member - template<class Class> - bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr), const char *format,...) ATTR_PRINTF(4,5); - template<class Class, typename ParamType1> - bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6); - template<class Class, typename ParamType1, typename ParamType2> - bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...) ATTR_PRINTF(6,7); - template<class Class, typename ParamType1, typename ParamType2, typename ParamType3> - bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *format,...) ATTR_PRINTF(7,8); - // PQuery / static - template<typename ParamType1> - bool AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(4,5); - template<typename ParamType1, typename ParamType2> - bool AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...) ATTR_PRINTF(5,6); - template<typename ParamType1, typename ParamType2, typename ParamType3> - bool AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char *format,...) ATTR_PRINTF(6,7); - template<class Class> - // QueryHolder - bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*), SqlQueryHolder *holder); - template<class Class, typename ParamType1> - bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1); - - bool Execute(const char *sql); - bool PExecute(const char *format,...) ATTR_PRINTF(2,3); - bool DirectExecute(const char* sql); - bool DirectPExecute(const char *format,...) ATTR_PRINTF(2,3); - - bool _UpdateDataBlobValue(const uint32 guid, const uint32 field, const int32 value); - bool _SetDataBlobValue(const uint32 guid, const uint32 field, const uint32 value); - - // Writes SQL commands to a LOG file (see Trinityd.conf "LogSQL") - bool PExecuteLog(const char *format,...) ATTR_PRINTF(2,3); - - bool BeginTransaction(); - bool CommitTransaction(); - bool RollbackTransaction(); - - operator bool () const { return mMysql != NULL; } - unsigned long escape_string(char *to, const char *from, unsigned long length); - void escape_string(std::string& str); - - void ThreadStart(); - void ThreadEnd(); - - // sets the result queue of the current thread, be careful what thread you call this from - void SetResultQueue(SqlResultQueue * queue); - - bool CheckRequiredField(char const* table_name, char const* required_name); - - private: - bool m_logSQL; - std::string m_logsDir; - ACE_Thread_Mutex mMutex; // For thread safe operations between core and mySQL server - ACE_Thread_Mutex nMutex; // For thread safe operations on m_transQueues - - ACE_Based::Thread * tranThread; - - MYSQL *mMysql; - - static size_t db_count; - - bool _TransactionCmd(const char *sql); - bool _Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount); -}; -#endif - diff --git a/src/shared/Database/DatabaseEnv.h b/src/shared/Database/DatabaseEnv.h deleted file mode 100644 index 69236b076e9..00000000000 --- a/src/shared/Database/DatabaseEnv.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#if !defined(DATABASEENV_H) -#define DATABASEENV_H - -#include "Common.h" -#include "Log.h" -#include "Errors.h" - -#include "Database/Field.h" -#include "Database/QueryResult.h" - -#include "Database/Database.h" -typedef Database DatabaseType; -#define _LIKE_ "LIKE" -#define _TABLE_SIM_ "`" -#define _CONCAT3_(A,B,C) "CONCAT( " A " , " B " , " C " )" -#define _OFFSET_ "LIMIT %d,1" - -extern DatabaseType WorldDatabase; -extern DatabaseType CharacterDatabase; -extern DatabaseType LoginDatabase; - -#endif - diff --git a/src/shared/Database/DatabaseImpl.h b/src/shared/Database/DatabaseImpl.h deleted file mode 100644 index f0ba9c84a30..00000000000 --- a/src/shared/Database/DatabaseImpl.h +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * 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; \ - \ - QueryQueues::iterator queue_itr; \ - \ - { \ - ACE_Based::Thread * queryThread = ACE_Based::Thread::current(); \ - queue_itr = m_queryQueues.find(queryThread); \ - if (queue_itr == m_queryQueues.end()) return false; \ - } - - -#define ASYNC_PQUERY_BODY(format, szQuery) \ - if(!format) return false; \ - \ - char szQuery [MAX_QUERY_LEN]; \ - \ - { \ - va_list ap; \ - \ - va_start(ap, format); \ - int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap ); \ - va_end(ap); \ - \ - if(res==-1) \ - { \ - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); \ - return false; \ - } \ - } - - -#define ASYNC_DELAYHOLDER_BODY(holder, queue_itr) \ - if (!holder) return false; \ - \ - QueryQueues::iterator queue_itr; \ - \ - { \ - ACE_Based::Thread * queryThread = ACE_Based::Thread::current(); \ - queue_itr = m_queryQueues.find(queryThread); \ - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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) -{ - ASYNC_QUERY_BODY(sql, itr) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - 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,...) -{ - ASYNC_PQUERY_BODY(format, szQuery) - return AsyncQuery(method, param1, param2, param3, szQuery); -} - - -// -- QueryHolder -- - - -template<class Class> -bool -Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*), SqlQueryHolder *holder) -{ - ASYNC_DELAYHOLDER_BODY(holder, itr) - 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) -{ - ASYNC_DELAYHOLDER_BODY(holder, itr) - 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 diff --git a/src/shared/Database/Field.cpp b/src/shared/Database/Field.cpp deleted file mode 100644 index 191a2884aac..00000000000 --- a/src/shared/Database/Field.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "DatabaseEnv.h" - -Field::Field() : -mValue(NULL), mType(DB_TYPE_UNKNOWN) -{ -} - -Field::Field(Field &f) -{ - const char *value; - - value = f.GetString(); - - if (value) - { - mValue = new char[strlen(value) + 1]; - if (mValue) - strcpy(mValue, value); - } - else - mValue = NULL; - - mType = f.GetType(); -} - -Field::Field(const char *value, enum Field::DataTypes type) : -mType(type) -{ - if (value) - { - mValue = new char[strlen(value) + 1]; - if (mValue) - strcpy(mValue, value); - } - else - mValue = NULL; -} - -Field::~Field() -{ - if (mValue) - delete [] mValue; -} - -void Field::SetValue(const char *value) -{ - if (mValue) - delete [] mValue; - - if (value) - { - mValue = new char[strlen(value) + 1]; - strcpy(mValue, value); - } - else - mValue = NULL; -} diff --git a/src/shared/Database/Field.h b/src/shared/Database/Field.h deleted file mode 100644 index 8d149858a42..00000000000 --- a/src/shared/Database/Field.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#if !defined(FIELD_H) -#define FIELD_H - -class Field -{ - public: - - enum DataTypes - { - DB_TYPE_UNKNOWN = 0x00, - DB_TYPE_STRING = 0x01, - DB_TYPE_INTEGER = 0x02, - DB_TYPE_FLOAT = 0x03, - DB_TYPE_BOOL = 0x04 - }; - - Field(); - Field(Field &f); - Field(const char *value, enum DataTypes type); - - ~Field(); - - enum DataTypes GetType() const { return mType; } - - const char *GetString() const { return mValue; } - std::string GetCppString() const - { - return mValue ? mValue : ""; // std::string s = 0 have undefine result in C++ - } - float GetFloat() const { return mValue ? static_cast<float>(atof(mValue)) : 0.0f; } - bool GetBool() const { return mValue ? atoi(mValue) > 0 : false; } - int32 GetInt32() const { return mValue ? static_cast<int32>(atol(mValue)) : int32(0); } - uint8 GetUInt8() const { return mValue ? static_cast<uint8>(atol(mValue)) : uint8(0); } - uint16 GetUInt16() const { return mValue ? static_cast<uint16>(atol(mValue)) : uint16(0); } - int16 GetInt16() const { return mValue ? static_cast<int16>(atol(mValue)) : int16(0); } - uint32 GetUInt32() const { return mValue ? static_cast<uint32>(atol(mValue)) : uint32(0); } - uint64 GetUInt64() const - { - if(mValue) - { - uint64 value; - sscanf(mValue,UI64FMTD,&value); - return value; - } - else - return 0; - } - uint64 GetInt64() const - { - if(mValue) - { - int64 value; - sscanf(mValue,SI64FMTD,&value); - return value; - } - else - return 0; - } - - void SetType(enum DataTypes type) { mType = type; } - - void SetValue(const char *value); - - private: - char *mValue; - enum DataTypes mType; -}; -#endif - diff --git a/src/shared/Database/QueryResult.cpp b/src/shared/Database/QueryResult.cpp deleted file mode 100644 index 8b0c437b066..00000000000 --- a/src/shared/Database/QueryResult.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "DatabaseEnv.h" - -QueryResult::QueryResult(MYSQL_RES *result, MYSQL_FIELD *fields, uint64 rowCount, uint32 fieldCount) -: mResult(result) -, mFieldCount(fieldCount) -, mRowCount(rowCount) -{ - mCurrentRow = new Field[mFieldCount]; - ASSERT(mCurrentRow); - - for (uint32 i = 0; i < mFieldCount; i++) - mCurrentRow[i].SetType(ConvertNativeType(fields[i].type)); -} - -QueryResult::~QueryResult() -{ - EndQuery(); -} - -bool QueryResult::NextRow() -{ - MYSQL_ROW row; - - if (!mResult) - return false; - - row = mysql_fetch_row(mResult); - if (!row) - { - EndQuery(); - return false; - } - - for (uint32 i = 0; i < mFieldCount; i++) - mCurrentRow[i].SetValue(row[i]); - - return true; -} - -void QueryResult::EndQuery() -{ - if (mCurrentRow) - { - delete [] mCurrentRow; - mCurrentRow = 0; - } - - if (mResult) - { - mysql_free_result(mResult); - mResult = 0; - } -} - -enum Field::DataTypes QueryResult::ConvertNativeType(enum_field_types mysqlType) const -{ - switch (mysqlType) - { - case FIELD_TYPE_TIMESTAMP: - case FIELD_TYPE_DATE: - case FIELD_TYPE_TIME: - case FIELD_TYPE_DATETIME: - case FIELD_TYPE_YEAR: - case FIELD_TYPE_STRING: - case FIELD_TYPE_VAR_STRING: - case FIELD_TYPE_BLOB: - case FIELD_TYPE_SET: - case FIELD_TYPE_NULL: - return Field::DB_TYPE_STRING; - case FIELD_TYPE_TINY: - - case FIELD_TYPE_SHORT: - case FIELD_TYPE_LONG: - case FIELD_TYPE_INT24: - case FIELD_TYPE_LONGLONG: - case FIELD_TYPE_ENUM: - return Field::DB_TYPE_INTEGER; - case FIELD_TYPE_DECIMAL: - case FIELD_TYPE_FLOAT: - case FIELD_TYPE_DOUBLE: - return Field::DB_TYPE_FLOAT; - default: - return Field::DB_TYPE_UNKNOWN; - } -} diff --git a/src/shared/Database/QueryResult.h b/src/shared/Database/QueryResult.h deleted file mode 100644 index 4eec9915362..00000000000 --- a/src/shared/Database/QueryResult.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#if !defined(QUERYRESULT_H) -#define QUERYRESULT_H - -#include <ace/Refcounted_Auto_Ptr.h> -#include <ace/Null_Mutex.h> - -#include "Field.h" - -#ifdef WIN32 -#define FD_SETSIZE 1024 -#include <winsock2.h> -#include <mysql/mysql.h> -#else -#include <mysql.h> -#endif - -class QueryResult -{ - public: - QueryResult(MYSQL_RES *result, MYSQL_FIELD *fields, uint64 rowCount, uint32 fieldCount); - ~QueryResult(); - - bool NextRow(); - - Field *Fetch() const { return mCurrentRow; } - - const Field & operator [] (int index) const { return mCurrentRow[index]; } - - uint32 GetFieldCount() const { return mFieldCount; } - uint64 GetRowCount() const { return mRowCount; } - - protected: - Field *mCurrentRow; - uint32 mFieldCount; - uint64 mRowCount; - - private: - enum Field::DataTypes ConvertNativeType(enum_field_types mysqlType) const; - void EndQuery(); - MYSQL_RES *mResult; - -}; - -typedef ACE_Refcounted_Auto_Ptr<QueryResult, ACE_Null_Mutex> QueryResult_AutoPtr; - -typedef std::vector<std::string> QueryFieldNames; - -class QueryNamedResult -{ - public: - explicit QueryNamedResult(QueryResult* query, QueryFieldNames const& names) : mQuery(query), mFieldNames(names) {} - ~QueryNamedResult() { delete mQuery; } - - // compatible interface with QueryResult - bool NextRow() { return mQuery->NextRow(); } - Field *Fetch() const { return mQuery->Fetch(); } - uint32 GetFieldCount() const { return mQuery->GetFieldCount(); } - uint64 GetRowCount() const { return mQuery->GetRowCount(); } - Field const& operator[] (int index) const { return (*mQuery)[index]; } - - // named access - Field const& operator[] (const std::string &name) const { return mQuery->Fetch()[GetField_idx(name)]; } - QueryFieldNames const& GetFieldNames() const { return mFieldNames; } - - uint32 GetField_idx(const std::string &name) const - { - for (size_t idx = 0; idx < mFieldNames.size(); ++idx) - { - if(mFieldNames[idx] == name) - return idx; - } - ASSERT(false && "unknown field name"); - return uint32(-1); - } - - protected: - QueryResult *mQuery; - QueryFieldNames mFieldNames; -}; - -#endif - diff --git a/src/shared/Database/SQLStorage.cpp b/src/shared/Database/SQLStorage.cpp deleted file mode 100644 index f42c31b2fea..00000000000 --- a/src/shared/Database/SQLStorage.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "SQLStorage.h" -#include "SQLStorageImpl.h" - -extern Database WorldDatabase; - -const char CreatureInfosrcfmt[]="iiiiiiiiiisssiiiiiiifffiffiifiiiiiiiiiiffiiiiiiiiiiiiiiiiiiiiiiiisiifffliiiiiiiliiisi"; -const char CreatureInfodstfmt[]="iiiiiiiiiisssibbiiiifffiffiifiiiiiiiiiiffiiiiiiiiiiiiiiiiiiiiiiiisiifffliiiiiiiliiiii"; -const char CreatureDataAddonInfofmt[]="iiiiiis"; -const char CreatureModelfmt[]="iffbi"; -const char CreatureInfoAddonInfofmt[]="iiiiiis"; -const char EquipmentInfofmt[]="iiii"; -const char GameObjectInfosrcfmt[]="iiissssiifiiiiiiiiiiiiiiiiiiiiiiiiiiiiiisi"; -const char GameObjectInfodstfmt[]="iiissssiifiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"; -const char ItemPrototypesrcfmt[]="iiiisiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiffiffiiiiiiiiiifiiifiiiiiifiiiiiifiiiiiifiiiiiifiiiisiiiiiiiiiiiiiiiiiiiiiiiiifiiisiiiii"; -const char ItemPrototypedstfmt[]="iiiisiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiffiffiiiiiiiiiifiiifiiiiiifiiiiiifiiiiiifiiiiiifiiiisiiiiiiiiiiiiiiiiiiiiiiiiifiiiiiiiii"; -const char PageTextfmt[]="isii"; -const char InstanceTemplatesrcfmt[]="iiiffffsb"; -const char InstanceTemplatedstfmt[]="iiiffffib"; - -SQLStorage sCreatureStorage(CreatureInfosrcfmt, CreatureInfodstfmt, "entry","creature_template"); -SQLStorage sCreatureDataAddonStorage(CreatureDataAddonInfofmt,"guid","creature_addon"); -SQLStorage sCreatureModelStorage(CreatureModelfmt,"modelid","creature_model_info"); -SQLStorage sCreatureInfoAddonStorage(CreatureInfoAddonInfofmt,"entry","creature_template_addon"); -SQLStorage sEquipmentStorage(EquipmentInfofmt,"entry","creature_equip_template"); -SQLStorage sGOStorage(GameObjectInfosrcfmt, GameObjectInfodstfmt, "entry","gameobject_template"); -SQLStorage sItemStorage(ItemPrototypesrcfmt, ItemPrototypedstfmt, "entry","item_template"); -SQLStorage sPageTextStore(PageTextfmt,"entry","page_text"); -SQLStorage sInstanceTemplate(InstanceTemplatesrcfmt, InstanceTemplatedstfmt, "map","instance_template"); - -void SQLStorage::Free () -{ - uint32 offset=0; - for (uint32 x=0; x<iNumFields; x++) - if (dst_format[x]==FT_STRING) - { - for (uint32 y=0; y<MaxEntry; y++) - if(pIndex[y]) - delete [] *(char**)((char*)(pIndex[y])+offset); - - offset += sizeof(char*); - } - else if (dst_format[x]==FT_LOGIC) - offset += sizeof(bool); - else if (dst_format[x]==FT_BYTE) - offset += sizeof(char); - else - offset += 4; - - delete [] pIndex; - delete [] data; -} - -void SQLStorage::Load() -{ - SQLStorageLoader loader; - loader.Load(*this); -} diff --git a/src/shared/Database/SQLStorage.h b/src/shared/Database/SQLStorage.h deleted file mode 100644 index 931c46b1995..00000000000 --- a/src/shared/Database/SQLStorage.h +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef SQLSTORAGE_H -#define SQLSTORAGE_H - -#include "Common.h" -#include "Database/DatabaseEnv.h" - -class SQLStorage -{ - template<class T> - friend struct SQLStorageLoaderBase; - - public: - - SQLStorage(const char* fmt, const char * _entry_field, const char * sqlname) - { - src_format = fmt; - dst_format = fmt; - init(_entry_field, sqlname); - } - - SQLStorage(const char* src_fmt, const char* dst_fmt, const char * _entry_field, const char * sqlname) - { - src_format = src_fmt; - dst_format = dst_fmt; - init(_entry_field, sqlname); - } - - ~SQLStorage() - { - Free(); - } - - template<class T> - T const* LookupEntry(uint32 id) const - { - if( id == 0 ) - return NULL; - if(id >= MaxEntry) - return NULL; - return reinterpret_cast<T const*>(pIndex[id]); - } - - uint32 RecordCount; - uint32 MaxEntry; - uint32 iNumFields; - - char const* GetTableName() const { return table; } - - void Load(); - void Free(); - - private: - void init(const char * _entry_field, const char * sqlname) - { - entry_field = _entry_field; - table=sqlname; - data=NULL; - pIndex=NULL; - iNumFields = strlen(src_format); - MaxEntry = 0; - } - - char** pIndex; - - char *data; - const char *src_format; - const char *dst_format; - const char *table; - const char *entry_field; - //bool HasString; -}; - -template <class T> -struct SQLStorageLoaderBase -{ - public: - void Load(SQLStorage &storage); - - template<class S, class D> - void convert(uint32 field_pos, S src, D &dst); - template<class S> - void convert_to_str(uint32 field_pos, S src, char * & dst); - template<class D> - void convert_from_str(uint32 field_pos, char * src, D& dst); - void convert_str_to_str(uint32 field_pos, char *src, char *&dst); - - private: - template<class V> - void storeValue(V value, SQLStorage &store, char *p, int x, uint32 &offset); - void storeValue(char * value, SQLStorage &store, char *p, int x, uint32 &offset); -}; - -struct SQLStorageLoader : public SQLStorageLoaderBase<SQLStorageLoader> -{ -}; - -#endif - diff --git a/src/shared/Database/SQLStorageImpl.h b/src/shared/Database/SQLStorageImpl.h deleted file mode 100644 index c74be48c34c..00000000000 --- a/src/shared/Database/SQLStorageImpl.h +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "ProgressBar.h" -#include "Log.h" -#include "DBCFileLoader.h" - -template<class T> -template<class S, class D> -void SQLStorageLoaderBase<T>::convert(uint32 /*field_pos*/, S src, D &dst) -{ - dst = D(src); -} - -template<class T> -void SQLStorageLoaderBase<T>::convert_str_to_str(uint32 /*field_pos*/, char *src, char *&dst) -{ - if(!src) - { - dst = new char[1]; - *dst = 0; - } - else - { - uint32 l = strlen(src) + 1; - dst = new char[l]; - memcpy(dst, src, l); - } -} - -template<class T> -template<class S> -void SQLStorageLoaderBase<T>::convert_to_str(uint32 /*field_pos*/, S /*src*/, char * & dst) -{ - dst = new char[1]; - *dst = 0; -} - -template<class T> -template<class D> -void SQLStorageLoaderBase<T>::convert_from_str(uint32 /*field_pos*/, char * /*src*/, D& dst) -{ - dst = 0; -} - -template<class T> -template<class V> -void SQLStorageLoaderBase<T>::storeValue(V value, SQLStorage &store, char *p, int x, uint32 &offset) -{ - T * subclass = (static_cast<T*>(this)); - switch(store.dst_format[x]) - { - case FT_LOGIC: - subclass->convert(x, value, *((bool*)(&p[offset])) ); - offset+=sizeof(bool); - break; - case FT_BYTE: - subclass->convert(x, value, *((char*)(&p[offset])) ); - offset+=sizeof(char); - break; - case FT_INT: - subclass->convert(x, value, *((uint32*)(&p[offset])) ); - offset+=sizeof(uint32); - break; - case FT_FLOAT: - subclass->convert(x, value, *((float*)(&p[offset])) ); - offset+=sizeof(float); - break; - case FT_STRING: - subclass->convert_to_str(x, value, *((char**)(&p[offset])) ); - offset+=sizeof(char*); - break; - } -} - -template<class T> -void SQLStorageLoaderBase<T>::storeValue(char * value, SQLStorage &store, char *p, int x, uint32 &offset) -{ - T * subclass = (static_cast<T*>(this)); - switch(store.dst_format[x]) - { - case FT_LOGIC: - subclass->convert_from_str(x, value, *((bool*)(&p[offset])) ); - offset+=sizeof(bool); - break; - case FT_BYTE: - subclass->convert_from_str(x, value, *((char*)(&p[offset])) ); - offset+=sizeof(char); - break; - case FT_INT: - subclass->convert_from_str(x, value, *((uint32*)(&p[offset])) ); - offset+=sizeof(uint32); - break; - case FT_FLOAT: - subclass->convert_from_str(x, value, *((float*)(&p[offset])) ); - offset+=sizeof(float); - break; - case FT_STRING: - subclass->convert_str_to_str(x, value, *((char**)(&p[offset])) ); - offset+=sizeof(char*); - break; - } -} - -template<class T> -void SQLStorageLoaderBase<T>::Load(SQLStorage &store) -{ - uint32 maxi; - Field *fields; - QueryResult_AutoPtr result = WorldDatabase.PQuery("SELECT MAX(%s) FROM %s", store.entry_field, store.table); - if(!result) - { - sLog.outError("Error loading %s table (not exist?)\n", store.table); - exit(1); // Stop server at loading non exited table or not accessable table - } - - maxi = (*result)[0].GetUInt32()+1; - - result = WorldDatabase.PQuery("SELECT COUNT(*) FROM %s", store.table); - if(result) - { - fields = result->Fetch(); - store.RecordCount = fields[0].GetUInt32(); - } - else - store.RecordCount = 0; - - result = WorldDatabase.PQuery("SELECT * FROM %s", store.table); - - if(!result) - { - sLog.outError("%s table is empty!\n", store.table); - store.RecordCount = 0; - return; - } - - uint32 recordsize = 0; - uint32 offset = 0; - - if(store.iNumFields != result->GetFieldCount()) - { - store.RecordCount = 0; - sLog.outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n", store.table, store.iNumFields); - exit(1); // Stop server at loading broken or non-compatible table. - } - - //get struct size - uint32 sc=0; - uint32 bo=0; - uint32 bb=0; - for (uint32 x=0; x< store.iNumFields; x++) - if(store.dst_format[x]==FT_STRING) - ++sc; - else if (store.dst_format[x]==FT_LOGIC) - ++bo; - else if (store.dst_format[x]==FT_BYTE) - ++bb; - recordsize=(store.iNumFields-sc-bo-bb)*4+sc*sizeof(char*)+bo*sizeof(bool)+bb*sizeof(char); - - char** newIndex=new char*[maxi]; - memset(newIndex,0,maxi*sizeof(char*)); - - char * _data= new char[store.RecordCount *recordsize]; - uint32 count=0; - barGoLink bar( store.RecordCount ); - do - { - fields = result->Fetch(); - bar.step(); - char *p=(char*)&_data[recordsize*count]; - newIndex[fields[0].GetUInt32()]=p; - - offset=0; - for (uint32 x = 0; x < store.iNumFields; x++) - switch(store.src_format[x]) - { - case FT_LOGIC: - storeValue((bool)(fields[x].GetUInt32() > 0), store, p, x, offset); break; - case FT_BYTE: - storeValue((char)fields[x].GetUInt8(), store, p, x, offset); break; - case FT_INT: - storeValue((uint32)fields[x].GetUInt32(), store, p, x, offset); break; - case FT_FLOAT: - storeValue((float)fields[x].GetFloat(), store, p, x, offset); break; - case FT_STRING: - storeValue((char*)fields[x].GetString(), store, p, x, offset); break; - } - ++count; - }while( result->NextRow() ); - - store.pIndex = newIndex; - store.MaxEntry = maxi; - store.data = _data; -} - diff --git a/src/shared/Database/SqlDelayThread.cpp b/src/shared/Database/SqlDelayThread.cpp deleted file mode 100644 index 43de3c63ffc..00000000000 --- a/src/shared/Database/SqlDelayThread.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "Database/SqlDelayThread.h" -#include "Database/SqlOperations.h" -#include "DatabaseEnv.h" - -SqlDelayThread::SqlDelayThread(Database* db) : m_dbEngine(db), m_running(true) -{ -} - -void SqlDelayThread::run() -{ - mysql_thread_init(); - - SqlAsyncTask * s = NULL; - - 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) - { - s->call(); - delete s; - } - } - - mysql_thread_end(); -} - -void SqlDelayThread::Stop() -{ - m_running = false; - m_sqlQueue.queue()->deactivate(); -} - -bool SqlDelayThread::Delay(SqlOperation* sql) -{ - int res = m_sqlQueue.enqueue(new SqlAsyncTask(m_dbEngine, sql)); - return (res != -1); -} diff --git a/src/shared/Database/SqlDelayThread.h b/src/shared/Database/SqlDelayThread.h deleted file mode 100644 index d603813c8fa..00000000000 --- a/src/shared/Database/SqlDelayThread.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef __SQLDELAYTHREAD_H -#define __SQLDELAYTHREAD_H - -#include "ace/Thread_Mutex.h" -#include "ace/Activation_Queue.h" -#include "Threading.h" - -class Database; -class SqlOperation; - -class SqlDelayThread : public ACE_Based::Runnable -{ - typedef ACE_Activation_Queue SqlQueue; - - private: - SqlQueue m_sqlQueue; ///< Queue of SQL statements - Database* m_dbEngine; ///< Pointer to used Database engine - volatile bool m_running; - - SqlDelayThread(); - public: - SqlDelayThread(Database* db); - - ///< Put sql statement to delay queue - bool Delay(SqlOperation* sql); - - void Stop(); ///< Stop event - virtual void run(); ///< Main Thread loop -}; -#endif //__SQLDELAYTHREAD_H diff --git a/src/shared/Database/SqlOperations.cpp b/src/shared/Database/SqlOperations.cpp deleted file mode 100644 index 785c5cb84e7..00000000000 --- a/src/shared/Database/SqlOperations.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "SqlOperations.h" -#include "SqlDelayThread.h" -#include "DatabaseEnv.h" -#include "DatabaseImpl.h" - -/// ---- ASYNC STATEMENTS / TRANSACTIONS ---- - -void SqlStatement::Execute(Database *db) -{ - /// just do it - db->DirectExecute(m_sql); -} - -void SqlTransaction::Execute(Database *db) -{ - const char* sql; - - m_Mutex.acquire(); - if (m_queue.empty()) - { - m_Mutex.release(); - return; - } - - db->DirectExecute("START TRANSACTION"); - while (!m_queue.empty()) - { - sql = m_queue.front(); - - if (!db->DirectExecute(sql)) - { - free((void*)const_cast<char*>(sql)); - m_queue.pop(); - db->DirectExecute("ROLLBACK"); - while (!m_queue.empty()) - { - free((void*)const_cast<char*>(m_queue.front())); - m_queue.pop(); - } - m_Mutex.release(); - return; - } - - free((void*)const_cast<char*>(sql)); - m_queue.pop(); - } - - db->DirectExecute("COMMIT"); - m_Mutex.release(); -} - -/// ---- ASYNC QUERIES ---- - -void SqlQuery::Execute(Database *db) -{ - 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 - m_queue->add(m_callback); -} - -void SqlResultQueue::Update() -{ - /// execute the callbacks waiting in the synchronization queue - Trinity::IQueryCallback* callback; - while (next(callback)) - { - callback->Execute(); - delete callback; - } -} - -bool SqlQueryHolder::Execute(Trinity::IQueryCallback * callback, SqlDelayThread *thread, SqlResultQueue *queue) -{ - if (!callback || !thread || !queue) - return false; - - /// delay the execution of the queries, sync them with the delay thread - /// which will in turn resync on execution (via the queue) and call back - SqlQueryHolderEx *holderEx = new SqlQueryHolderEx(this, callback, queue); - thread->Delay(holderEx); - return true; -} - -bool SqlQueryHolder::SetQuery(size_t index, const char *sql) -{ - 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) - { - sLog.outError("Attempt assign query to holder index (%u) where other query stored (Old: [%s] New: [%s])", - index,m_queries[index].first,sql); - return false; - } - - /// not executed yet, just stored (it's not called a holder for nothing) - m_queries[index] = SqlResultPair(strdup(sql), QueryResult_AutoPtr(NULL)); - return true; -} - -bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...) -{ - if (!format) - { - sLog.outError("Query (index: %u) is empty.",index); - return false; - } - - va_list ap; - char szQuery [MAX_QUERY_LEN]; - va_start(ap, format); - int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return false; - } - - return SetQuery(index,szQuery); -} - -QueryResult_AutoPtr SqlQueryHolder::GetResult(size_t index) -{ - if (index < m_queries.size()) - { - /// the query strings are freed on the first GetResult or in the destructor - if (m_queries[index].first != NULL) - { - free((void*)(const_cast<char*>(m_queries[index].first))); - m_queries[index].first = NULL; - } - /// when you get a result aways remember to delete it! - return m_queries[index].second; - } - else - return QueryResult_AutoPtr(NULL); -} - -void SqlQueryHolder::SetResult(size_t index, QueryResult_AutoPtr result) -{ - /// store the result in the holder - if (index < m_queries.size()) - m_queries[index].second = result; -} - -SqlQueryHolder::~SqlQueryHolder() -{ - for (size_t i = 0; i < m_queries.size(); i++) - { - /// 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) - free((void*)(const_cast<char*>(m_queries[i].first))); - } -} - -void SqlQueryHolder::SetSize(size_t size) -{ - /// to optimize push_back, reserve the number of queries about to be executed - m_queries.resize(size); -} - -void SqlQueryHolderEx::Execute(Database *db) -{ - if (!m_holder || !m_callback || !m_queue) - return; - - /// we can do this, we are friends - std::vector<SqlQueryHolder::SqlResultPair> &queries = m_holder->m_queries; - - for (size_t i = 0; i < queries.size(); i++) - { - /// execute all queries in the holder and pass the results - char const *sql = queries[i].first; - if(sql) m_holder->SetResult(i, db->Query(sql)); - } - - /// sync with the caller thread - m_queue->add(m_callback); -} diff --git a/src/shared/Database/SqlOperations.h b/src/shared/Database/SqlOperations.h deleted file mode 100644 index f2e09c0c921..00000000000 --- a/src/shared/Database/SqlOperations.h +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * - * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef __SQLOPERATIONS_H -#define __SQLOPERATIONS_H - -#include "Common.h" - -#include "ace/Thread_Mutex.h" -#include "ace/Method_Request.h" -#include "LockedQueue.h" -#include <queue> -#include "Utilities/Callback.h" -#include "QueryResult.h" - -/// ---- BASE --- - -class Database; -class SqlDelayThread; - -class SqlOperation -{ - public: - virtual void OnRemove() { delete this; } - virtual void Execute(Database *db) = 0; - virtual ~SqlOperation() {} -}; - -/// ---- ASYNC STATEMENTS / TRANSACTIONS ---- - -class SqlStatement : public SqlOperation -{ - private: - const char *m_sql; - public: - SqlStatement(const char *sql) : m_sql(strdup(sql)){} - ~SqlStatement() { void* tofree = const_cast<char*>(m_sql); free(tofree); } - void Execute(Database *db); -}; - -class SqlTransaction : public SqlOperation -{ - private: - std::queue<const char*> m_queue; - ACE_Thread_Mutex m_Mutex; - public: - SqlTransaction() {} - void DelayExecute(const char *sql) - { - m_Mutex.acquire(); - char* _sql = strdup(sql); - if (_sql) - m_queue.push(_sql); - m_Mutex.release(); - } - void Execute(Database *db); -}; - -/// ---- ASYNC QUERIES ---- - -class SqlQuery; /// contains a single async query -class QueryResult; /// the result of one -class SqlResultQueue; /// queue for thread sync -class SqlQueryHolder; /// groups several async quries -class SqlQueryHolderEx; /// points to a holder, added to the delay thread - -class SqlResultQueue : public ACE_Based::LockedQueue<Trinity::IQueryCallback* , ACE_Thread_Mutex> -{ - public: - SqlResultQueue() {} - void Update(); -}; - -class SqlQuery : public SqlOperation -{ - private: - const char *m_sql; - Trinity::IQueryCallback * m_callback; - SqlResultQueue * m_queue; - public: - SqlQuery(const char *sql, Trinity::IQueryCallback * callback, SqlResultQueue * queue) - : m_sql(strdup(sql)), m_callback(callback), m_queue(queue) {} - ~SqlQuery() { void* tofree = const_cast<char*>(m_sql); free(tofree); } - void Execute(Database *db); -}; - -class SqlQueryHolder -{ - friend class SqlQueryHolderEx; - private: - typedef std::pair<const char*, QueryResult_AutoPtr> SqlResultPair; - std::vector<SqlResultPair> m_queries; - public: - SqlQueryHolder() {} - ~SqlQueryHolder(); - bool SetQuery(size_t index, const char *sql); - bool SetPQuery(size_t index, const char *format, ...) ATTR_PRINTF(3,4); - void SetSize(size_t size); - QueryResult_AutoPtr GetResult(size_t index); - void SetResult(size_t index, QueryResult_AutoPtr result); - bool Execute(Trinity::IQueryCallback * callback, SqlDelayThread *thread, SqlResultQueue *queue); -}; - -class SqlQueryHolderEx : public SqlOperation -{ - private: - SqlQueryHolder * m_holder; - Trinity::IQueryCallback * m_callback; - SqlResultQueue * m_queue; - public: - SqlQueryHolderEx(SqlQueryHolder *holder, Trinity::IQueryCallback * callback, SqlResultQueue * queue) - : m_holder(holder), m_callback(callback), m_queue(queue) {} - void Execute(Database *db); -}; - -class SqlAsyncTask : public ACE_Method_Request -{ -public: - SqlAsyncTask(Database * db, SqlOperation * op) : m_db(db), m_op(op){} - ~SqlAsyncTask() - { - if (!m_op) - return; - - delete m_op; - m_op = NULL; - } - - int call() - { - if (m_db == NULL || m_op == NULL) - return -1; - - try - { - m_op->Execute(m_db); - } - catch(...) - { - return -1; - } - - return 0; - } - -private: - Database * m_db; - SqlOperation * m_op; -}; -#endif //__SQLOPERATIONS_H - |
