TrinityCore
Loading...
Searching...
No Matches
Transaction.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _TRANSACTION_H
19#define _TRANSACTION_H
20
21#include "Define.h"
22#include "DatabaseEnvFwd.h"
23#include "SQLOperation.h"
24#include "StringFormat.h"
25#include <functional>
26#include <mutex>
27#include <vector>
28
31{
32 friend class TransactionTask;
33 friend class MySQLConnection;
34
35 template <typename T>
36 friend class DatabaseWorkerPool;
37
38 public:
39 TransactionBase() : _cleanedUp(false) { }
40 virtual ~TransactionBase() { Cleanup(); }
41
42 void Append(char const* sql);
43 template<typename... Args>
44 void PAppend(Trinity::FormatString<Args...> sql, Args&&... args)
45 {
46 this->Append(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
47 }
48
49 std::size_t GetSize() const { return m_queries.size(); }
50
51 protected:
52 void AppendPreparedStatement(PreparedStatementBase* statement);
53 void Cleanup();
54 std::vector<SQLElementData> m_queries;
55
56 private:
58};
59
60template<typename T>
62{
63public:
66 {
67 this->AppendPreparedStatement(statement);
68 }
69};
70
73{
74 template <class T> friend class DatabaseWorkerPool;
75 friend class DatabaseWorker;
76 friend class TransactionCallback;
77
78 public:
79 TransactionTask(std::shared_ptr<TransactionBase> trans) : m_trans(trans) { }
81
82 protected:
83 bool Execute() override;
84 int TryExecute();
85 void CleanupOnFailure();
86
87 std::shared_ptr<TransactionBase> m_trans;
88 static std::mutex _deadlockLock;
89};
90
92{
93public:
94 TransactionWithResultTask(std::shared_ptr<TransactionBase> trans) : TransactionTask(trans) { }
95
96 TransactionFuture GetFuture() { return m_result.get_future(); }
97
98protected:
99 bool Execute() override;
100
102};
103
105{
106public:
107 TransactionCallback(TransactionFuture&& future) : m_future(std::move(future)) { }
109
111
112 void AfterComplete(std::function<void(bool)> callback) &
113 {
114 m_callback = std::move(callback);
115 }
116
117 bool InvokeIfReady();
118
120 std::function<void(bool)> m_callback;
121};
122
123inline bool InvokeAsyncCallbackIfReady(TransactionCallback& callback) { return callback.InvokeIfReady(); }
124
125#endif
std::promise< bool > TransactionPromise
std::future< bool > TransactionFuture
#define TC_DATABASE_API
Definition Define.h:102
bool InvokeAsyncCallbackIfReady(TransactionCallback &callback)
std::vector< SQLElementData > m_queries
Definition Transaction.h:54
std::size_t GetSize() const
Definition Transaction.h:49
void PAppend(Trinity::FormatString< Args... > sql, Args &&... args)
Definition Transaction.h:44
virtual ~TransactionBase()
Definition Transaction.h:40
void AppendPreparedStatement(PreparedStatementBase *statement)
void Append(char const *sql)
TransactionFuture m_future
TransactionCallback(TransactionFuture &&future)
TransactionCallback & operator=(TransactionCallback &&)=default
std::function< void(bool)> m_callback
TransactionCallback(TransactionCallback &&)=default
void AfterComplete(std::function< void(bool)> callback) &
TransactionTask(std::shared_ptr< TransactionBase > trans)
Definition Transaction.h:79
std::shared_ptr< TransactionBase > m_trans
Definition Transaction.h:87
static std::mutex _deadlockLock
Definition Transaction.h:88
TransactionFuture GetFuture()
Definition Transaction.h:96
TransactionWithResultTask(std::shared_ptr< TransactionBase > trans)
Definition Transaction.h:94
TransactionPromise m_result
void Append(PreparedStatement< T > *statement)
Definition Transaction.h:65
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
fmt::format_string< Args... > FormatString
STL namespace.