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 <future>
26#include <mutex>
27#include <variant>
28#include <vector>
29
30class MySQLConnection;
31
33{
34 std::variant<std::unique_ptr<PreparedStatementBase>, std::string> query;
35
36 template<typename... Args>
37 TransactionData(Args&&... args) : query(std::forward<Args>(args)...) { }
39 TransactionData(TransactionData&&) noexcept = default;
40 TransactionData& operator=(TransactionData const&) = delete;
41 TransactionData& operator=(TransactionData&&) noexcept = default;
43
44 static PreparedStatementBase* ToExecutable(std::unique_ptr<PreparedStatementBase> const& stmt) { return stmt.get(); }
45 static char const* ToExecutable(std::string const& sql) { return sql.c_str(); }
46};
47
50{
51 friend class TransactionTask;
52 friend class MySQLConnection;
53
54 template <typename T>
55 friend class DatabaseWorkerPool;
56
57 public:
58 TransactionBase() : _cleanedUp(false) { }
60 TransactionBase(TransactionBase &&) noexcept = default;
61 TransactionBase& operator=(TransactionBase const&) = delete;
62 TransactionBase& operator=(TransactionBase &&) noexcept = default;
63 virtual ~TransactionBase() { Cleanup(); }
64
65 void Append(char const* sql);
66 template<typename... Args>
67 void PAppend(Trinity::FormatString<Args...> sql, Args&&... args)
68 {
69 Append(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
70 }
71
72 std::size_t GetSize() const { return m_queries.size(); }
73
74 protected:
75 void AppendPreparedStatement(PreparedStatementBase* statement);
76 void Cleanup();
77 std::vector<TransactionData> m_queries;
78
79 private:
81};
82
83template<typename T>
85{
86public:
89 {
90 AppendPreparedStatement(statement);
91 }
92};
93
96{
97public:
98 static bool Execute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
99
100private:
101 static int TryExecute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans);
102
103 static std::mutex _deadlockLock;
104};
105
107{
108public:
109 TransactionCallback(std::future<bool>&& future) : m_future(std::move(future)) { }
111
113
114 void AfterComplete(std::function<void(bool)> callback) &
115 {
116 m_callback = std::move(callback);
117 }
118
119 bool InvokeIfReady();
120
121 std::future<bool> m_future;
122 std::function<void(bool)> m_callback;
123};
124
125#endif
#define TC_DATABASE_API
Definition: Define.h:111
std::vector< TransactionData > m_queries
Definition: Transaction.h:77
std::size_t GetSize() const
Definition: Transaction.h:72
void PAppend(Trinity::FormatString< Args... > sql, Args &&... args)
Definition: Transaction.h:67
void AppendPreparedStatement(PreparedStatementBase *statement)
Definition: Transaction.cpp:43
TransactionBase(TransactionBase const &)=delete
void Append(char const *sql)
Definition: Transaction.cpp:36
TransactionBase(TransactionBase &&) noexcept=default
TransactionCallback(std::future< bool > &&future)
Definition: Transaction.h:109
std::future< bool > m_future
Definition: Transaction.h:121
TransactionCallback & operator=(TransactionCallback &&)=default
std::function< void(bool)> m_callback
Definition: Transaction.h:122
TransactionCallback(TransactionCallback &&)=default
void AfterComplete(std::function< void(bool)> callback) &
Definition: Transaction.h:114
static std::mutex _deadlockLock
Definition: Transaction.h:103
void Append(PreparedStatement< T > *statement)
Definition: Transaction.h:88
void Execute(Creature *me, EventMap &events, uint32 eventId)
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:44
TransactionData(Args &&... args)
Definition: Transaction.h:37
std::variant< std::unique_ptr< PreparedStatementBase >, std::string > query
Definition: Transaction.h:34
TransactionData(TransactionData const &)=delete
static char const * ToExecutable(std::string const &sql)
Definition: Transaction.h:45
TransactionData(TransactionData &&) noexcept=default