2010-10-07 15:54:07 +02:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
2010-10-07 15:54:07 +02:00
|
|
|
*
|
|
|
|
|
* 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/>.
|
2010-08-21 03:19:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _TRANSACTION_H
|
|
|
|
|
#define _TRANSACTION_H
|
|
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
#include "Define.h"
|
|
|
|
|
#include "DatabaseEnvFwd.h"
|
2015-03-28 19:10:21 +01:00
|
|
|
#include "StringFormat.h"
|
2020-04-14 16:23:44 +02:00
|
|
|
#include <functional>
|
2017-05-11 22:29:51 +02:00
|
|
|
#include <mutex>
|
2023-12-27 00:14:12 +01:00
|
|
|
#include <variant>
|
2017-05-12 18:49:51 +02:00
|
|
|
#include <vector>
|
2010-09-04 16:49:23 +02:00
|
|
|
|
2023-12-15 12:06:59 +01:00
|
|
|
class MySQLConnection;
|
|
|
|
|
|
2023-12-27 00:14:12 +01:00
|
|
|
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(); }
|
|
|
|
|
};
|
|
|
|
|
|
2010-08-21 03:19:25 +02:00
|
|
|
/*! Transactions, high level class. */
|
2019-07-27 10:52:33 +02:00
|
|
|
class TC_DATABASE_API TransactionBase
|
2010-08-21 03:19:25 +02:00
|
|
|
{
|
|
|
|
|
friend class TransactionTask;
|
2011-01-13 20:07:09 +01:00
|
|
|
friend class MySQLConnection;
|
|
|
|
|
|
2013-01-02 12:22:50 +01:00
|
|
|
template <typename T>
|
|
|
|
|
friend class DatabaseWorkerPool;
|
|
|
|
|
|
2010-08-21 03:19:25 +02:00
|
|
|
public:
|
2019-07-27 10:52:33 +02:00
|
|
|
TransactionBase() : _cleanedUp(false) { }
|
2023-12-27 00:14:12 +01:00
|
|
|
TransactionBase(TransactionBase const&) = delete;
|
|
|
|
|
TransactionBase(TransactionBase &&) noexcept = default;
|
|
|
|
|
TransactionBase& operator=(TransactionBase const&) = delete;
|
|
|
|
|
TransactionBase& operator=(TransactionBase &&) noexcept = default;
|
2019-07-27 10:52:33 +02:00
|
|
|
virtual ~TransactionBase() { Cleanup(); }
|
2010-08-21 03:19:25 +02:00
|
|
|
|
2020-09-04 13:38:24 +02:00
|
|
|
void Append(char const* sql);
|
2022-09-19 00:33:19 +02:00
|
|
|
template<typename... Args>
|
2023-01-08 21:16:53 +01:00
|
|
|
void PAppend(Trinity::FormatString<Args...> sql, Args&&... args)
|
2015-07-22 01:03:40 +02:00
|
|
|
{
|
2022-09-19 00:33:19 +02:00
|
|
|
Append(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
|
2015-07-22 01:03:40 +02:00
|
|
|
}
|
2010-09-12 01:40:27 +02:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
std::size_t GetSize() const { return m_queries.size(); }
|
2010-08-21 03:19:25 +02:00
|
|
|
|
|
|
|
|
protected:
|
2019-07-27 10:52:33 +02:00
|
|
|
void AppendPreparedStatement(PreparedStatementBase* statement);
|
2010-08-21 03:19:25 +02:00
|
|
|
void Cleanup();
|
2023-12-27 00:14:12 +01:00
|
|
|
std::vector<TransactionData> m_queries;
|
2011-05-23 16:33:33 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool _cleanedUp;
|
2019-07-27 10:52:33 +02:00
|
|
|
};
|
2010-08-21 03:19:25 +02:00
|
|
|
|
2019-07-27 10:52:33 +02:00
|
|
|
template<typename T>
|
|
|
|
|
class Transaction : public TransactionBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using TransactionBase::Append;
|
|
|
|
|
void Append(PreparedStatement<T>* statement)
|
|
|
|
|
{
|
|
|
|
|
AppendPreparedStatement(statement);
|
|
|
|
|
}
|
2010-08-21 03:19:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*! Low level class*/
|
2023-12-15 12:06:59 +01:00
|
|
|
class TC_DATABASE_API TransactionTask
|
2020-04-14 16:23:44 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2023-12-15 12:06:59 +01:00
|
|
|
static bool Execute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
|
2020-04-14 16:23:44 +02:00
|
|
|
|
2023-12-15 12:06:59 +01:00
|
|
|
private:
|
|
|
|
|
static int TryExecute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
|
2020-04-14 16:23:44 +02:00
|
|
|
|
2023-12-15 12:06:59 +01:00
|
|
|
static std::mutex _deadlockLock;
|
2020-04-14 16:23:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TC_DATABASE_API TransactionCallback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TransactionCallback(TransactionFuture&& future) : m_future(std::move(future)) { }
|
|
|
|
|
TransactionCallback(TransactionCallback&&) = default;
|
|
|
|
|
|
|
|
|
|
TransactionCallback& operator=(TransactionCallback&&) = default;
|
|
|
|
|
|
|
|
|
|
void AfterComplete(std::function<void(bool)> callback) &
|
|
|
|
|
{
|
|
|
|
|
m_callback = std::move(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InvokeIfReady();
|
|
|
|
|
|
|
|
|
|
TransactionFuture m_future;
|
|
|
|
|
std::function<void(bool)> m_callback;
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-02 12:22:50 +01:00
|
|
|
#endif
|