diff options
author | Shauren <shauren.trinity@gmail.com> | 2023-12-27 00:14:12 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-12-27 00:14:12 +0100 |
commit | 3a9cbd217aa14bd7543722d9f4849f736b7c3264 (patch) | |
tree | 1dbe14196d6a6ae0dcceb70a6edc9ebd9eb0bbba | |
parent | 13dc139742655ea523f02f67995ad376c9b912dc (diff) |
Core/Database: Replace union with std::variant in Transaction
-rw-r--r-- | src/server/database/Database/MySQLConnection.cpp | 35 | ||||
-rw-r--r-- | src/server/database/Database/SQLOperation.h | 44 | ||||
-rw-r--r-- | src/server/database/Database/Transaction.cpp | 26 | ||||
-rw-r--r-- | src/server/database/Database/Transaction.h | 19 |
4 files changed, 28 insertions, 96 deletions
diff --git a/src/server/database/Database/MySQLConnection.cpp b/src/server/database/Database/MySQLConnection.cpp index 56e384520c2..ab16d560ec2 100644 --- a/src/server/database/Database/MySQLConnection.cpp +++ b/src/server/database/Database/MySQLConnection.cpp @@ -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; } } diff --git a/src/server/database/Database/SQLOperation.h b/src/server/database/Database/SQLOperation.h deleted file mode 100644 index c4b5d325576..00000000000 --- a/src/server/database/Database/SQLOperation.h +++ /dev/null @@ -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 diff --git a/src/server/database/Database/Transaction.cpp b/src/server/database/Database/Transaction.cpp index 00ab5cbc7fa..f1ddb608c6c 100644 --- a/src/server/database/Database/Transaction.cpp +++ b/src/server/database/Database/Transaction.cpp @@ -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; } diff --git a/src/server/database/Database/Transaction.h b/src/server/database/Database/Transaction.h index 5fc81bd05c9..f4dcb9fae34 100644 --- a/src/server/database/Database/Transaction.h +++ b/src/server/database/Database/Transaction.h @@ -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; |