TrinityCore
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 "StringFormat.h"
24#include <functional>
25#include <mutex>
26#include <variant>
27#include <vector>
28
29class MySQLConnection;
30
32{
33 std::variant<std::unique_ptr<PreparedStatementBase>, std::string> query;
34
35 template<typename... Args>
36 TransactionData(Args&&... args) : query(std::forward<Args>(args)...) { }
37
38 static PreparedStatementBase* ToExecutable(std::unique_ptr<PreparedStatementBase> const& stmt) { return stmt.get(); }
39 static char const* ToExecutable(std::string const& sql) { return sql.c_str(); }
40};
41
44{
45 friend class TransactionTask;
46 friend class MySQLConnection;
47
48 template <typename T>
49 friend class DatabaseWorkerPool;
50
51 public:
52 TransactionBase() : _cleanedUp(false) { }
54 TransactionBase(TransactionBase &&) noexcept = default;
55 TransactionBase& operator=(TransactionBase const&) = delete;
56 TransactionBase& operator=(TransactionBase &&) noexcept = default;
57 virtual ~TransactionBase() { Cleanup(); }
58
59 void Append(char const* sql);
60 template<typename... Args>
61 void PAppend(Trinity::FormatString<Args...> sql, Args&&... args)
62 {
63 Append(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
64 }
65
66 std::size_t GetSize() const { return m_queries.size(); }
67
68 protected:
69 void AppendPreparedStatement(PreparedStatementBase* statement);
70 void Cleanup();
71 std::vector<TransactionData> m_queries;
72
73 private:
75};
76
77template<typename T>
79{
80public:
83 {
84 AppendPreparedStatement(statement);
85 }
86};
87
90{
91public:
92 static bool Execute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
93
94private:
95 static int TryExecute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
96
97 static std::mutex _deadlockLock;
98};
99
101{
102public:
103 TransactionCallback(TransactionFuture&& future) : m_future(std::move(future)) { }
105
107
108 void AfterComplete(std::function<void(bool)> callback) &
109 {
110 m_callback = std::move(callback);
111 }
112
113 bool InvokeIfReady();
114
116 std::function<void(bool)> m_callback;
117};
118
119#endif
std::future< bool > TransactionFuture
#define TC_DATABASE_API
Definition: Define.h:111
std::vector< TransactionData > m_queries
Definition: Transaction.h:71
std::size_t GetSize() const
Definition: Transaction.h:66
void PAppend(Trinity::FormatString< Args... > sql, Args &&... args)
Definition: Transaction.h:61
void AppendPreparedStatement(PreparedStatementBase *statement)
Definition: Transaction.cpp:41
TransactionBase(TransactionBase const &)=delete
void Append(char const *sql)
Definition: Transaction.cpp:34
TransactionBase(TransactionBase &&) noexcept=default
TransactionFuture m_future
Definition: Transaction.h:115
TransactionCallback(TransactionFuture &&future)
Definition: Transaction.h:103
TransactionCallback & operator=(TransactionCallback &&)=default
std::function< void(bool)> m_callback
Definition: Transaction.h:116
TransactionCallback(TransactionCallback &&)=default
void AfterComplete(std::function< void(bool)> callback) &
Definition: Transaction.h:108
static std::mutex _deadlockLock
Definition: Transaction.h:97
void Append(PreparedStatement< T > *statement)
Definition: Transaction.h:82
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
fmt::format_string< Args... > FormatString
Definition: StringFormat.h:27
STL namespace.
static PreparedStatementBase * ToExecutable(std::unique_ptr< PreparedStatementBase > const &stmt)
Definition: Transaction.h:38
TransactionData(Args &&... args)
Definition: Transaction.h:36
std::variant< std::unique_ptr< PreparedStatementBase >, std::string > query
Definition: Transaction.h:33
static char const * ToExecutable(std::string const &sql)
Definition: Transaction.h:39