Files
TCPlayerbotCore/src/server/database/Database/Transaction.h
T

120 lines
3.4 KiB
C++
Raw Normal View History

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"
#include "StringFormat.h"
#include <functional>
2017-05-11 22:29:51 +02:00
#include <mutex>
#include <variant>
2017-05-12 18:49:51 +02:00
#include <vector>
2010-09-04 16:49:23 +02:00
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(); }
};
2010-08-21 03:19:25 +02:00
/*! Transactions, high level class. */
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:
TransactionBase() : _cleanedUp(false) { }
TransactionBase(TransactionBase const&) = delete;
TransactionBase(TransactionBase &&) noexcept = default;
TransactionBase& operator=(TransactionBase const&) = delete;
TransactionBase& operator=(TransactionBase &&) noexcept = default;
virtual ~TransactionBase() { Cleanup(); }
2010-08-21 03:19:25 +02:00
void Append(char const* sql);
template<typename... Args>
void PAppend(Trinity::FormatString<Args...> sql, Args&&... args)
{
Append(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
}
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:
void AppendPreparedStatement(PreparedStatementBase* statement);
2010-08-21 03:19:25 +02:00
void Cleanup();
std::vector<TransactionData> m_queries;
private:
bool _cleanedUp;
};
2010-08-21 03:19:25 +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*/
class TC_DATABASE_API TransactionTask
{
public:
static bool Execute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
private:
static int TryExecute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
static std::mutex _deadlockLock;
};
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