TrinityCore
Loading...
Searching...
No Matches
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 "Define.h"
22#include "DatabaseEnvFwd.h"
23#include "StringFormat.h"
24#include <array>
25#include <string>
26#include <vector>
27
28template <typename T>
30
31class SQLOperation;
33
34template <class T>
36{
37 private:
44
45 public:
46 /* Activity state */
48
50
51 void SetConnectionInfo(std::string const& infoString, uint8 const asyncThreads, uint8 const synchThreads);
52
53 uint32 Open();
54
55 void Close();
56
58 bool PrepareStatements();
59
61 {
62 return _connectionInfo.get();
63 }
64
71 void Execute(char const* sql);
72
75 template<typename... Args>
76 void PExecute(Trinity::FormatString<Args...> sql, Args&&... args)
77 {
79 return;
80
81 this->Execute(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
82 }
83
86 void Execute(PreparedStatement<T>* stmt);
87
94 void DirectExecute(char const* sql);
95
98 template<typename... Args>
100 {
102 return;
103
104 this->DirectExecute(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
105 }
106
110
117 QueryResult Query(char const* sql, T* connection = nullptr);
118
121 template<typename... Args>
123 {
125 return QueryResult(nullptr);
126
127 return this->Query(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str(), conn);
128 }
129
132 template<typename... Args>
134 {
136 return QueryResult(nullptr);
137
138 return this->Query(Trinity::StringFormat(sql, std::forward<Args>(args)...).c_str());
139 }
140
145
152 QueryCallback AsyncQuery(char const* sql);
153
158
164
171
174 void CommitTransaction(SQLTransaction<T> transaction);
175
179
183
186 void ExecuteOrAppend(SQLTransaction<T>& trans, char const* sql);
187
191
196 typedef typename T::Statements PreparedStatementIndex;
197
202
204 void EscapeString(std::string& str);
205
207 void KeepAlive();
208
209 void WarnAboutSyncQueries([[maybe_unused]] bool warn)
210 {
211#ifdef TRINITY_DEBUG
212 _warnSyncQueries = warn;
213#endif
214 }
215
216 size_t QueueSize() const;
217
218 private:
219 uint32 OpenConnections(InternalIndex type, uint8 numConnections);
220
221 unsigned long EscapeString(char* to, char const* from, unsigned long length);
222
223 void Enqueue(SQLOperation* op);
224
228
229 char const* GetDatabaseName() const;
230
232 std::unique_ptr<ProducerConsumerQueue<SQLOperation*>> _queue;
233 std::array<std::vector<std::unique_ptr<T>>, IDX_SIZE> _connections;
234 std::unique_ptr<MySQLConnectionInfo> _connectionInfo;
235 std::vector<uint8> _preparedStatementSize;
237#ifdef TRINITY_DEBUG
238 static inline thread_local bool _warnSyncQueries = false;
239#endif
240};
241
242#endif
std::shared_ptr< ResultSet > QueryResult
std::shared_ptr< Transaction< T > > SQLTransaction
std::shared_ptr< PreparedResultSet > PreparedQueryResult
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
uint32 OpenConnections(InternalIndex type, uint8 numConnections)
void WarnAboutSyncQueries(bool warn)
void CommitTransaction(SQLTransaction< T > transaction)
void Enqueue(SQLOperation *op)
std::vector< uint8 > _preparedStatementSize
QueryResult Query(char const *sql, T *connection=nullptr)
bool PrepareStatements()
Prepares all prepared statements.
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)
void ExecuteOrAppend(SQLTransaction< T > &trans, char const *sql)
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)
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
TransactionCallback AsyncCommitTransaction(SQLTransaction< T > transaction)
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
bool IsFormatEmptyOrNull(char const *fmt)
Returns true if the given char pointer is null.
fmt::format_string< Args... > FormatString