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

124 lines
3.3 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"
2010-09-02 17:47:50 +02:00
#include "SQLOperation.h"
#include "StringFormat.h"
#include <functional>
2017-05-11 22:29:51 +02:00
#include <mutex>
2017-05-12 18:49:51 +02:00
#include <vector>
2010-09-04 16:49:23 +02:00
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) { }
virtual ~TransactionBase() { Cleanup(); }
2010-08-21 03:19:25 +02:00
void Append(char const* sql);
template<typename... Args>
void PAppend(std::string_view 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();
2017-05-12 18:49:51 +02:00
std::vector<SQLElementData> 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 SQLOperation
2010-08-21 03:19:25 +02:00
{
2010-09-02 17:47:50 +02:00
template <class T> friend class DatabaseWorkerPool;
2010-12-13 09:18:49 +01:00
friend class DatabaseWorker;
friend class TransactionCallback;
2010-08-21 03:19:25 +02:00
public:
TransactionTask(std::shared_ptr<TransactionBase> trans) : m_trans(trans) { }
2015-04-08 20:00:00 -04:00
~TransactionTask() { }
2010-08-21 03:19:25 +02:00
protected:
2014-08-09 20:17:40 +01:00
bool Execute() override;
int TryExecute();
void CleanupOnFailure();
2010-08-21 03:19:25 +02:00
std::shared_ptr<TransactionBase> m_trans;
static std::mutex _deadlockLock;
2010-08-21 03:19:25 +02:00
};
class TC_DATABASE_API TransactionWithResultTask : public TransactionTask
{
public:
TransactionWithResultTask(std::shared_ptr<TransactionBase> trans) : TransactionTask(trans) { }
TransactionFuture GetFuture() { return m_result.get_future(); }
protected:
bool Execute() override;
TransactionPromise m_result;
};
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