TrinityCore
DatabaseWorkerPool.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 _DATABASEWORKERPOOL_H
19#define _DATABASEWORKERPOOL_H
20
21#include "AsioHacksFwd.h"
22#include "Define.h"
23#include "DatabaseEnvFwd.h"
24#include "StringFormat.h"
25#include <array>
26#include <string>
27#include <vector>
28
30
31template <class T>
33{
34 private:
36 {
40 };
41
42 public:
43 /* Activity state */
45
47
48 void SetConnectionInfo(std::string const& infoString, uint8 const asyncThreads, uint8 const synchThreads);
49
50 uint32 Open();
51
52 void Close();
53
55 bool PrepareStatements();
56
58 {
59 return _connectionInfo.get();
60 }
61
68 void Execute(char const* sql);
69
72 template<typename... Args>
73 void PExecute(Trinity::FormatString<Args...> sql, Args&&... args)
74 {
76 return;
77
78 Execute(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
79 }
80
83 void Execute(PreparedStatement<T>* stmt);
84
91 void DirectExecute(char const* sql);
92
95 template<typename... Args>
97 {
99 return;
100
101 DirectExecute(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
102 }
103
107
114 QueryResult Query(char const* sql, T* connection = nullptr);
115
118 template<typename... Args>
120 {
122 return QueryResult(nullptr);
123
124 return Query(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str(), conn);
125 }
126
129 template<typename... Args>
131 {
133 return QueryResult(nullptr);
134
135 return Query(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
136 }
137
142
149 QueryCallback AsyncQuery(char const* sql);
150
155
161
168
171 void CommitTransaction(SQLTransaction<T> transaction);
172
176
180
183 void ExecuteOrAppend(SQLTransaction<T>& trans, char const* sql);
184
188
193 typedef typename T::Statements PreparedStatementIndex;
194
199
201 void EscapeString(std::string& str);
202
204 void KeepAlive();
205
206 void WarnAboutSyncQueries([[maybe_unused]] bool warn)
207 {
208#ifdef TRINITY_DEBUG
209 _warnSyncQueries = warn;
210#endif
211 }
212
213 size_t QueueSize() const;
214
215 private:
216 uint32 OpenConnections(InternalIndex type, uint8 numConnections);
217
218 unsigned long EscapeString(char* to, char const* from, unsigned long length);
219
223
225
226 char const* GetDatabaseName() const;
227
228 struct QueueSizeTracker;
230
232 std::unique_ptr<Trinity::Asio::IoContext> _ioContext;
233 std::atomic<size_t> _queueSize;
234 std::array<std::vector<std::unique_ptr<T>>, IDX_SIZE> _connections;
235 std::unique_ptr<MySQLConnectionInfo> _connectionInfo;
236 std::vector<uint8> _preparedStatementSize;
238#ifdef TRINITY_DEBUG
239 static inline thread_local bool _warnSyncQueries = false;
240#endif
241};
242
243#endif
std::shared_ptr< ResultSet > QueryResult
std::shared_ptr< Transaction< T > > SQLTransaction
std::shared_ptr< PreparedResultSet > PreparedQueryResult
uint8_t uint8
Definition: Define.h:144
uint32_t uint32
Definition: Define.h:142
uint32 OpenConnections(InternalIndex type, uint8 numConnections)
void WarnAboutSyncQueries(bool warn)
void CommitTransaction(SQLTransaction< T > transaction)
std::vector< uint8 > _preparedStatementSize
QueryResult Query(char const *sql, T *connection=nullptr)
bool PrepareStatements()
Prepares all prepared statements.
T * GetAsyncConnectionForCurrentThread() const
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
void DirectPExecute(Trinity::FormatString< Args... > sql, Args &&... args)
void SetConnectionInfo(std::string const &infoString, uint8 const asyncThreads, uint8 const synchThreads)
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
QueryResult PQuery(Trinity::FormatString< Args... > sql, Args &&... args)
std::unique_ptr< Trinity::Asio::IoContext > _ioContext
Queue shared by async worker threads.
void ExecuteOrAppend(SQLTransaction< T > &trans, char const *sql)
std::atomic< size_t > _queueSize
SQLTransaction< T > BeginTransaction()
Begins an automanaged transaction pointer that will automatically rollback if not commited....
void DirectCommitTransaction(SQLTransaction< T > &transaction)
void KeepAlive()
Keeps all our MySQL connections alive, prevent the server from disconnecting us.
QueryResult PQuery(Trinity::FormatString< Args... > sql, T *conn, Args &&... args)
T::Statements PreparedStatementIndex
char const * GetDatabaseName() const
PreparedStatement< T > * GetPreparedStatement(PreparedStatementIndex index)
MySQLConnectionInfo const * GetConnectionInfo() const
void DirectExecute(char const *sql)
SQLQueryHolderCallback DelayQueryHolder(std::shared_ptr< SQLQueryHolder< T > > holder)
void EscapeString(std::string &str)
Apply escape string'ing for current collation. (utf8)
void Execute(char const *sql)
void PExecute(Trinity::FormatString< Args... > sql, Args &&... args)
QueryCallback AsyncQuery(char const *sql)
TransactionCallback AsyncCommitTransaction(SQLTransaction< T > transaction)
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
bool IsFormatEmptyOrNull(char const *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:89
fmt::format_string< Args... > FormatString
Definition: StringFormat.h:27