Core/DBLayer: Prevent mixing databases with query holders

This commit is contained in:
Shauren
2019-07-27 11:45:56 +02:00
parent f6e2b8cdc1
commit 13440434c3
10 changed files with 44 additions and 23 deletions

View File

@@ -63,9 +63,17 @@ using HotfixDatabaseTransaction = SQLTransaction<HotfixDatabaseConnection>;
using LoginDatabaseTransaction = SQLTransaction<LoginDatabaseConnection>;
using WorldDatabaseTransaction = SQLTransaction<WorldDatabaseConnection>;
class SQLQueryHolderBase;
typedef std::future<SQLQueryHolderBase*> QueryResultHolderFuture;
typedef std::promise<SQLQueryHolderBase*> QueryResultHolderPromise;
template<typename T>
class SQLQueryHolder;
typedef std::future<SQLQueryHolder*> QueryResultHolderFuture;
typedef std::promise<SQLQueryHolder*> QueryResultHolderPromise;
using CharacterDatabaseQueryHolder = SQLQueryHolder<CharacterDatabaseConnection>;
using HotfixDatabaseQueryHolder = SQLQueryHolder<HotfixDatabaseConnection>;
using LoginDatabaseQueryHolder = SQLQueryHolder<LoginDatabaseConnection>;
using WorldDatabaseQueryHolder = SQLQueryHolder<WorldDatabaseConnection>;
// mysql
typedef struct st_mysql MYSQL;

View File

@@ -201,7 +201,7 @@ QueryCallback DatabaseWorkerPool<T>::AsyncQuery(PreparedStatement<T>* stmt)
}
template <class T>
QueryResultHolderFuture DatabaseWorkerPool<T>::DelayQueryHolder(SQLQueryHolder* holder)
QueryResultHolderFuture DatabaseWorkerPool<T>::DelayQueryHolder(SQLQueryHolder<T>* holder)
{
SQLQueryHolderTask* task = new SQLQueryHolderTask(holder);
// Store future result before enqueueing - task might get already processed and deleted before returning from this method

View File

@@ -160,7 +160,7 @@ class DatabaseWorkerPool
//! return object as soon as the query is executed.
//! The return value is then processed in ProcessQueryCallback methods.
//! Any prepared statements added to this holder need to be prepared with the CONNECTION_ASYNC flag.
QueryResultHolderFuture DelayQueryHolder(SQLQueryHolder* holder);
QueryResultHolderFuture DelayQueryHolder(SQLQueryHolder<T>* holder);
/**
Transaction context methods.

View File

@@ -21,7 +21,7 @@
#include "Log.h"
#include "QueryResult.h"
bool SQLQueryHolder::SetPreparedQuery(size_t index, PreparedStatementBase* stmt)
bool SQLQueryHolderBase::SetPreparedQueryImpl(size_t index, PreparedStatementBase* stmt)
{
if (m_queries.size() <= index)
{
@@ -33,7 +33,7 @@ bool SQLQueryHolder::SetPreparedQuery(size_t index, PreparedStatementBase* stmt)
return true;
}
PreparedQueryResult SQLQueryHolder::GetPreparedResult(size_t index)
PreparedQueryResult SQLQueryHolderBase::GetPreparedResult(size_t index)
{
// Don't call to this function if the index is of a prepared statement
if (index < m_queries.size())
@@ -42,7 +42,7 @@ PreparedQueryResult SQLQueryHolder::GetPreparedResult(size_t index)
return PreparedQueryResult(nullptr);
}
void SQLQueryHolder::SetPreparedResult(size_t index, PreparedResultSet* result)
void SQLQueryHolderBase::SetPreparedResult(size_t index, PreparedResultSet* result)
{
if (result && !result->GetRowCount())
{
@@ -55,7 +55,7 @@ void SQLQueryHolder::SetPreparedResult(size_t index, PreparedResultSet* result)
m_queries[index].second = PreparedQueryResult(result);
}
SQLQueryHolder::~SQLQueryHolder()
SQLQueryHolderBase::~SQLQueryHolderBase()
{
for (size_t i = 0; i < m_queries.size(); i++)
{
@@ -65,7 +65,7 @@ SQLQueryHolder::~SQLQueryHolder()
}
}
void SQLQueryHolder::SetSize(size_t size)
void SQLQueryHolderBase::SetSize(size_t size)
{
/// to optimize push_back, reserve the number of queries about to be executed
m_queries.resize(size);

View File

@@ -20,29 +20,41 @@
#include "SQLOperation.h"
class TC_DATABASE_API SQLQueryHolder
class TC_DATABASE_API SQLQueryHolderBase
{
friend class SQLQueryHolderTask;
private:
std::vector<std::pair<PreparedStatementBase*, PreparedQueryResult>> m_queries;
public:
SQLQueryHolder() { }
virtual ~SQLQueryHolder();
bool SetPreparedQuery(size_t index, PreparedStatementBase* stmt);
SQLQueryHolderBase() { }
virtual ~SQLQueryHolderBase();
void SetSize(size_t size);
PreparedQueryResult GetPreparedResult(size_t index);
void SetPreparedResult(size_t index, PreparedResultSet* result);
protected:
bool SetPreparedQueryImpl(size_t index, PreparedStatementBase* stmt);
};
template<typename T>
class SQLQueryHolder : public SQLQueryHolderBase
{
public:
bool SetPreparedQuery(size_t index, PreparedStatement<T>* stmt)
{
return SetPreparedQueryImpl(index, stmt);
}
};
class TC_DATABASE_API SQLQueryHolderTask : public SQLOperation
{
private:
SQLQueryHolder* m_holder;
SQLQueryHolderBase* m_holder;
QueryResultHolderPromise m_result;
bool m_executed;
public:
SQLQueryHolderTask(SQLQueryHolder* holder)
SQLQueryHolderTask(SQLQueryHolderBase* holder)
: m_holder(holder), m_executed(false) { }
~SQLQueryHolderTask();

View File

@@ -17540,7 +17540,7 @@ bool Player::IsLoading() const
return GetSession()->PlayerLoading();
}
bool Player::LoadFromDB(ObjectGuid guid, SQLQueryHolder *holder)
bool Player::LoadFromDB(ObjectGuid guid, CharacterDatabaseQueryHolder* holder)
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//"SELECT guid, account, name, race, class, gender, level, xp, money, skin, face, hairStyle, hairColor, facialStyle, customDisplay1, customDisplay2, customDisplay3, inventorySlots, bankSlots, restState, playerFlags, playerFlagsEx, "

View File

@@ -1432,7 +1432,7 @@ class TC_GAME_API Player : public Unit, public GridObject<Player>
/*** LOAD SYSTEM ***/
/*********************************************************/
bool LoadFromDB(ObjectGuid guid, SQLQueryHolder *holder);
bool LoadFromDB(ObjectGuid guid, CharacterDatabaseQueryHolder* holder);
bool IsLoading() const override;
static uint32 GetUInt32ValueFromArray(Tokenizer const& data, uint16 index);

View File

@@ -63,7 +63,7 @@
#include "Util.h"
#include "World.h"
class LoginQueryHolder : public SQLQueryHolder
class LoginQueryHolder : public CharacterDatabaseQueryHolder
{
private:
uint32 m_accountId;

View File

@@ -882,7 +882,8 @@ void WorldSession::ProcessQueryCallbacks()
if (_realmAccountLoginCallback.valid() && _realmAccountLoginCallback.wait_for(std::chrono::seconds(0)) == std::future_status::ready &&
_accountLoginCallback.valid() && _accountLoginCallback.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
InitializeSessionCallback(_realmAccountLoginCallback.get(), _accountLoginCallback.get());
InitializeSessionCallback(static_cast<LoginDatabaseQueryHolder*>(_realmAccountLoginCallback.get()),
static_cast<CharacterDatabaseQueryHolder*>(_accountLoginCallback.get()));
//! HandlePlayerLoginOpcode
if (_charLoginCallback.valid() && _charLoginCallback.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
@@ -930,7 +931,7 @@ QueryCallback WorldSession::LoadPermissionsAsync()
return _RBACData->LoadFromDBAsync();
}
class AccountInfoQueryHolderPerRealm : public SQLQueryHolder
class AccountInfoQueryHolderPerRealm : public CharacterDatabaseQueryHolder
{
public:
enum
@@ -959,7 +960,7 @@ public:
}
};
class AccountInfoQueryHolder : public SQLQueryHolder
class AccountInfoQueryHolder : public LoginDatabaseQueryHolder
{
public:
enum
@@ -1041,7 +1042,7 @@ void WorldSession::InitializeSession()
_accountLoginCallback = LoginDatabase.DelayQueryHolder(holder);
}
void WorldSession::InitializeSessionCallback(SQLQueryHolder* realmHolder, SQLQueryHolder* holder)
void WorldSession::InitializeSessionCallback(LoginDatabaseQueryHolder* realmHolder, CharacterDatabaseQueryHolder* holder)
{
LoadAccountData(realmHolder->GetPreparedResult(AccountInfoQueryHolderPerRealm::GLOBAL_ACCOUNT_DATA), GLOBAL_CACHE_MASK);
LoadTutorialsData(realmHolder->GetPreparedResult(AccountInfoQueryHolderPerRealm::TUTORIALS));

View File

@@ -918,7 +918,7 @@ class TC_GAME_API WorldSession
void SendAvailableHotfixes(int32 version);
void InitializeSession();
void InitializeSessionCallback(SQLQueryHolder* realmHolder, SQLQueryHolder* holder);
void InitializeSessionCallback(LoginDatabaseQueryHolder* realmHolder, CharacterDatabaseQueryHolder* holder);
rbac::RBACData* GetRBACData();
bool HasPermission(uint32 permissionId);