mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Database: Replace union with std::variant in Transaction
This commit is contained in:
@@ -382,7 +382,7 @@ void MySQLConnection::CommitTransaction()
|
||||
|
||||
int MySQLConnection::ExecuteTransaction(std::shared_ptr<TransactionBase> transaction)
|
||||
{
|
||||
std::vector<SQLElementData> const& queries = transaction->m_queries;
|
||||
std::vector<TransactionData> const& queries = transaction->m_queries;
|
||||
if (queries.empty())
|
||||
return -1;
|
||||
|
||||
@@ -390,35 +390,12 @@ int MySQLConnection::ExecuteTransaction(std::shared_ptr<TransactionBase> transac
|
||||
|
||||
for (auto itr = queries.begin(); itr != queries.end(); ++itr)
|
||||
{
|
||||
SQLElementData const& data = *itr;
|
||||
switch (itr->type)
|
||||
if (!std::visit([this](auto&& data) { return this->Execute(TransactionData::ToExecutable(data)); }, itr->query))
|
||||
{
|
||||
case SQL_ELEMENT_PREPARED:
|
||||
{
|
||||
PreparedStatementBase* stmt = data.element.stmt;
|
||||
ASSERT(stmt);
|
||||
if (!Execute(stmt))
|
||||
{
|
||||
TC_LOG_WARN("sql.sql", "Transaction aborted. {} queries not executed.", (uint32)queries.size());
|
||||
int errorCode = GetLastError();
|
||||
RollbackTransaction();
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SQL_ELEMENT_RAW:
|
||||
{
|
||||
char const* sql = data.element.query;
|
||||
ASSERT(sql);
|
||||
if (!Execute(sql))
|
||||
{
|
||||
TC_LOG_WARN("sql.sql", "Transaction aborted. {} queries not executed.", (uint32)queries.size());
|
||||
int errorCode = GetLastError();
|
||||
RollbackTransaction();
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
break;
|
||||
TC_LOG_WARN("sql.sql", "Transaction aborted. {} queries not executed.", queries.size());
|
||||
int errorCode = GetLastError();
|
||||
RollbackTransaction();
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _SQLOPERATION_H
|
||||
#define _SQLOPERATION_H
|
||||
|
||||
#include "DatabaseEnvFwd.h"
|
||||
|
||||
//- Union that holds element data
|
||||
union SQLElementUnion
|
||||
{
|
||||
PreparedStatementBase* stmt;
|
||||
char const* query;
|
||||
};
|
||||
|
||||
//- Type specifier of our element data
|
||||
enum SQLElementDataType
|
||||
{
|
||||
SQL_ELEMENT_RAW,
|
||||
SQL_ELEMENT_PREPARED
|
||||
};
|
||||
|
||||
//- The element
|
||||
struct SQLElementData
|
||||
{
|
||||
SQLElementUnion element;
|
||||
SQLElementDataType type;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
#include "Transaction.h"
|
||||
#include "Errors.h"
|
||||
#include "Log.h"
|
||||
#include "MySQLConnection.h"
|
||||
#include "PreparedStatement.h"
|
||||
@@ -32,19 +33,15 @@ std::mutex TransactionTask::_deadlockLock;
|
||||
//- Append a raw ad-hoc query to the transaction
|
||||
void TransactionBase::Append(char const* sql)
|
||||
{
|
||||
SQLElementData data;
|
||||
data.type = SQL_ELEMENT_RAW;
|
||||
data.element.query = strdup(sql);
|
||||
m_queries.push_back(data);
|
||||
ASSERT(sql);
|
||||
m_queries.emplace_back(std::in_place_type<std::string>, sql);
|
||||
}
|
||||
|
||||
//- Append a prepared statement to the transaction
|
||||
void TransactionBase::AppendPreparedStatement(PreparedStatementBase* stmt)
|
||||
{
|
||||
SQLElementData data;
|
||||
data.type = SQL_ELEMENT_PREPARED;
|
||||
data.element.stmt = stmt;
|
||||
m_queries.push_back(data);
|
||||
ASSERT(stmt);
|
||||
m_queries.emplace_back(std::in_place_type<std::unique_ptr<PreparedStatementBase>>, stmt);
|
||||
}
|
||||
|
||||
void TransactionBase::Cleanup()
|
||||
@@ -53,19 +50,6 @@ void TransactionBase::Cleanup()
|
||||
if (_cleanedUp)
|
||||
return;
|
||||
|
||||
for (SQLElementData const& data : m_queries)
|
||||
{
|
||||
switch (data.type)
|
||||
{
|
||||
case SQL_ELEMENT_PREPARED:
|
||||
delete data.element.stmt;
|
||||
break;
|
||||
case SQL_ELEMENT_RAW:
|
||||
free((void*)(data.element.query));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_queries.clear();
|
||||
_cleanedUp = true;
|
||||
}
|
||||
|
||||
@@ -20,14 +20,25 @@
|
||||
|
||||
#include "Define.h"
|
||||
#include "DatabaseEnvFwd.h"
|
||||
#include "SQLOperation.h"
|
||||
#include "StringFormat.h"
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
class MySQLConnection;
|
||||
|
||||
struct TransactionData
|
||||
{
|
||||
std::variant<std::unique_ptr<PreparedStatementBase>, std::string> query;
|
||||
|
||||
template<typename... Args>
|
||||
TransactionData(Args&&... args) : query(std::forward<Args>(args)...) { }
|
||||
|
||||
static PreparedStatementBase* ToExecutable(std::unique_ptr<PreparedStatementBase> const& stmt) { return stmt.get(); }
|
||||
static char const* ToExecutable(std::string const& sql) { return sql.c_str(); }
|
||||
};
|
||||
|
||||
/*! Transactions, high level class. */
|
||||
class TC_DATABASE_API TransactionBase
|
||||
{
|
||||
@@ -39,6 +50,10 @@ class TC_DATABASE_API TransactionBase
|
||||
|
||||
public:
|
||||
TransactionBase() : _cleanedUp(false) { }
|
||||
TransactionBase(TransactionBase const&) = delete;
|
||||
TransactionBase(TransactionBase &&) noexcept = default;
|
||||
TransactionBase& operator=(TransactionBase const&) = delete;
|
||||
TransactionBase& operator=(TransactionBase &&) noexcept = default;
|
||||
virtual ~TransactionBase() { Cleanup(); }
|
||||
|
||||
void Append(char const* sql);
|
||||
@@ -53,7 +68,7 @@ class TC_DATABASE_API TransactionBase
|
||||
protected:
|
||||
void AppendPreparedStatement(PreparedStatementBase* statement);
|
||||
void Cleanup();
|
||||
std::vector<SQLElementData> m_queries;
|
||||
std::vector<TransactionData> m_queries;
|
||||
|
||||
private:
|
||||
bool _cleanedUp;
|
||||
|
||||
Reference in New Issue
Block a user