TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DatabaseWorkerPool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 "Common.h"
22 #include "Callback.h"
23 #include "MySQLConnection.h"
24 #include "Transaction.h"
25 #include "DatabaseWorker.h"
26 #include "PreparedStatement.h"
27 #include "Log.h"
28 #include "QueryResult.h"
29 #include "QueryHolder.h"
30 #include "AdhocStatement.h"
31 #include "StringFormat.h"
32 
33 #include <mysqld_error.h>
34 #include <memory>
35 #include <array>
36 
38 {
40  bool Execute() override
41  {
42  m_conn->Ping();
43  return true;
44  }
45 };
46 
47 template <class T>
49 {
50  private:
52  {
56  };
57 
58  public:
59  /* Activity state */
61 
63  {
64  _queue->Cancel();
65  }
66 
67  void SetConnectionInfo(std::string const& infoString, uint8 const asyncThreads, uint8 const synchThreads);
68 
69  uint32 Open();
70 
71  void Close();
72 
74  bool PrepareStatements();
75 
76  inline MySQLConnectionInfo const* GetConnectionInfo() const
77  {
78  return _connectionInfo.get();
79  }
80 
85  void Execute(const char* sql)
88  {
90  return;
91 
92  BasicStatementTask* task = new BasicStatementTask(sql);
93  Enqueue(task);
94  }
95 
98  template<typename Format, typename... Args>
99  void PExecute(Format&& sql, Args&&... args)
100  {
102  return;
103 
104  Execute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
105  }
106 
110  {
112  Enqueue(task);
113  }
114 
119  void DirectExecute(const char* sql)
122  {
123  if (!sql)
124  return;
125 
126  T* connection = GetFreeConnection();
127  connection->Execute(sql);
128  connection->Unlock();
129  }
130 
133  template<typename Format, typename... Args>
134  void DirectPExecute(Format&& sql, Args&&... args)
135  {
137  return;
138 
139  DirectExecute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
140  }
141 
145  {
146  T* connection = GetFreeConnection();
147  connection->Execute(stmt);
148  connection->Unlock();
149 
151  delete stmt;
152  }
153 
158  QueryResult Query(const char* sql, T* connection = nullptr);
161 
164  template<typename Format, typename... Args>
165  QueryResult PQuery(Format&& sql, T* conn, Args&&... args)
166  {
168  return QueryResult(nullptr);
169 
170  return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str(), conn);
171  }
172 
175  template<typename Format, typename... Args>
176  QueryResult PQuery(Format&& sql, Args&&... args)
177  {
178  if (!sql)
179  return QueryResult(nullptr);
180 
181  return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
182  }
183 
188 
193  QueryResultFuture AsyncQuery(const char* sql);
196 
199  template<typename Format, typename... Args>
200  QueryResultFuture AsyncPQuery(Format&& sql, Args&&... args)
201  {
202  return AsyncQuery(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
203  }
204 
209 
215 
222  {
223  return SQLTransaction(new Transaction);
224  }
225 
228  void CommitTransaction(SQLTransaction transaction);
229 
232  void DirectCommitTransaction(SQLTransaction& transaction);
233 
237  {
238  if (!trans)
239  Execute(stmt);
240  else
241  trans->Append(stmt);
242  }
243 
246  void ExecuteOrAppend(SQLTransaction& trans, const char* sql)
247  {
248  if (!trans)
249  Execute(sql);
250  else
251  trans->Append(sql);
252  }
253 
258  typedef typename T::Statements PreparedStatementIndex;
259 
263  PreparedStatement* GetPreparedStatement(PreparedStatementIndex index)
264  {
265  return new PreparedStatement(index);
266  }
267 
269  void EscapeString(std::string& str);
270 
272  void KeepAlive();
273 
274  private:
275  uint32 OpenConnections(InternalIndex type, uint8 numConnections);
276 
277  unsigned long EscapeString(char *to, const char *from, unsigned long length)
278  {
279  if (!to || !from || !length)
280  return 0;
281 
282  return mysql_real_escape_string(
283  _connections[IDX_SYNCH].front()->GetHandle(), to, from, length);
284  }
285 
287  {
288  _queue->Push(op);
289  }
290 
293  T* GetFreeConnection();
294 
295  char const* GetDatabaseName() const
296  {
297  return _connectionInfo->database.c_str();
298  }
299 
301  std::unique_ptr<ProducerConsumerQueue<SQLOperation*>> _queue;
302  std::array<std::vector<std::unique_ptr<T>>, IDX_SIZE> _connections;
303  std::unique_ptr<MySQLConnectionInfo> _connectionInfo;
305 };
306 
307 #endif
~DatabaseWorkerPool()
Definition: DatabaseWorkerPool.h:62
void Execute(const char *sql)
Definition: DatabaseWorkerPool.h:87
std::future< QueryResult > QueryResultFuture
Definition: Callback.h:24
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
Definition: QueryHolder.h:23
QueryResult PQuery(Format &&sql, T *conn, Args &&...args)
Definition: DatabaseWorkerPool.h:165
void SetConnectionInfo(std::string const &infoString, uint8 const asyncThreads, uint8 const synchThreads)
Definition: DatabaseWorkerPool.cpp:36
void ExecuteOrAppend(SQLTransaction &trans, PreparedStatement *stmt)
Definition: DatabaseWorkerPool.h:236
QueryResultFuture AsyncPQuery(Format &&sql, Args &&...args)
Definition: DatabaseWorkerPool.h:200
void KeepAlive()
Keeps all our MySQL connections alive, prevent the server from disconnecting us.
Definition: DatabaseWorkerPool.cpp:243
SQLTransaction BeginTransaction()
Begins an automanaged transaction pointer that will automatically rollback if not commited...
Definition: DatabaseWorkerPool.h:221
MySQLConnection * m_conn
Definition: SQLOperation.h:70
QueryResultFuture AsyncQuery(const char *sql)
Definition: DatabaseWorkerPool.cpp:149
Definition: DatabaseWorkerPool.h:55
bool IsFormatEmptyOrNull(const char *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:34
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
Definition: DatabaseWorkerPool.h:301
uint32 OpenConnections(InternalIndex type, uint8 numConnections)
Definition: DatabaseWorkerPool.cpp:264
void Close()
Definition: DatabaseWorkerPool.cpp:72
Definition: PreparedStatement.h:74
uint8 _synch_threads
Definition: DatabaseWorkerPool.h:304
std::future< SQLQueryHolder * > QueryResultHolderFuture
Definition: QueryHolder.h:46
QueryResult PQuery(Format &&sql, Args &&...args)
Definition: DatabaseWorkerPool.h:176
bool PrepareStatements()
Prepares all prepared statements.
Definition: DatabaseWorkerPool.cpp:93
Definition: AdhocStatement.h:28
void CommitTransaction(SQLTransaction transaction)
Definition: DatabaseWorkerPool.cpp:179
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:303
void PExecute(Format &&sql, Args &&...args)
Definition: DatabaseWorkerPool.h:99
InternalIndex
Definition: DatabaseWorkerPool.h:51
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:303
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
uint8 _async_threads
Definition: DatabaseWorkerPool.h:304
void DirectPExecute(Format &&sql, Args &&...args)
Definition: DatabaseWorkerPool.h:134
T::Statements PreparedStatementIndex
Definition: DatabaseWorkerPool.h:258
MySQLConnectionInfo const * GetConnectionInfo() const
Definition: DatabaseWorkerPool.h:76
Definition: Transaction.h:28
uint32_t uint32
Definition: Define.h:150
void EscapeString(std::string &str)
Apply escape string'ing for current collation. (utf8)
Definition: DatabaseWorkerPool.cpp:231
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61
DatabaseWorkerPool()
Definition: DatabaseWorkerPool.cpp:25
QueryResultHolderFuture DelayQueryHolder(SQLQueryHolder *holder)
Definition: DatabaseWorkerPool.cpp:169
PreparedStatement * GetPreparedStatement(PreparedStatementIndex index)
Definition: DatabaseWorkerPool.h:263
bool Execute() override
Operation for idle delaythreads.
Definition: DatabaseWorkerPool.h:40
float length(float v)
Definition: vectorMath.h:208
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
Definition: DatabaseWorkerPool.h:37
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
Definition: PreparedStatement.h:162
void Execute(PreparedStatement *stmt)
Definition: DatabaseWorkerPool.h:109
QueryResult Query(const char *sql, T *connection=nullptr)
Definition: DatabaseWorkerPool.cpp:113
unsigned long EscapeString(char *to, const char *from, unsigned long length)
Definition: DatabaseWorkerPool.h:277
void DirectCommitTransaction(SQLTransaction &transaction)
Definition: DatabaseWorkerPool.cpp:202
void ExecuteOrAppend(SQLTransaction &trans, const char *sql)
Definition: DatabaseWorkerPool.h:246
char const * GetDatabaseName() const
Definition: DatabaseWorkerPool.h:295
uint8_t uint8
Definition: Define.h:152
Definition: SQLOperation.h:56
Definition: DatabaseWorkerPool.h:54
void DirectExecute(PreparedStatement *stmt)
Definition: DatabaseWorkerPool.h:144
void DirectExecute(const char *sql)
Definition: DatabaseWorkerPool.h:121
Definition: DatabaseWorkerPool.h:48
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302
Definition: DatabaseWorkerPool.h:53
uint32 Open()
Definition: DatabaseWorkerPool.cpp:46
std::shared_ptr< Transaction > SQLTransaction
Definition: Transaction.h:58