TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DatabaseWorkerPool< T > Class Template Reference

#include <DatabaseWorkerPool.h>

Public Types

typedef T::Statements PreparedStatementIndex
 

Public Member Functions

 DatabaseWorkerPool ()
 
 ~DatabaseWorkerPool ()
 
void SetConnectionInfo (std::string const &infoString, uint8 const asyncThreads, uint8 const synchThreads)
 
uint32 Open ()
 
void Close ()
 
bool PrepareStatements ()
 Prepares all prepared statements. More...
 
MySQLConnectionInfo constGetConnectionInfo () const
 
void Execute (const char *sql)
 
template<typename Format , typename... Args>
void PExecute (Format &&sql, Args &&...args)
 
void Execute (PreparedStatement *stmt)
 
void DirectExecute (const char *sql)
 
template<typename Format , typename... Args>
void DirectPExecute (Format &&sql, Args &&...args)
 
void DirectExecute (PreparedStatement *stmt)
 
QueryResult Query (const char *sql, T *connection=nullptr)
 
template<typename Format , typename... Args>
QueryResult PQuery (Format &&sql, T *conn, Args &&...args)
 
template<typename Format , typename... Args>
QueryResult PQuery (Format &&sql, Args &&...args)
 
PreparedQueryResult Query (PreparedStatement *stmt)
 
QueryResultFuture AsyncQuery (const char *sql)
 
template<typename Format , typename... Args>
QueryResultFuture AsyncPQuery (Format &&sql, Args &&...args)
 
PreparedQueryResultFuture AsyncQuery (PreparedStatement *stmt)
 
QueryResultHolderFuture DelayQueryHolder (SQLQueryHolder *holder)
 
SQLTransaction BeginTransaction ()
 Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0) More...
 
void CommitTransaction (SQLTransaction transaction)
 
void DirectCommitTransaction (SQLTransaction &transaction)
 
void ExecuteOrAppend (SQLTransaction &trans, PreparedStatement *stmt)
 
void ExecuteOrAppend (SQLTransaction &trans, const char *sql)
 
PreparedStatementGetPreparedStatement (PreparedStatementIndex index)
 
void EscapeString (std::string &str)
 Apply escape string'ing for current collation. (utf8) More...
 
void KeepAlive ()
 Keeps all our MySQL connections alive, prevent the server from disconnecting us. More...
 

Private Types

enum  InternalIndex { IDX_ASYNC, IDX_SYNCH, IDX_SIZE }
 

Private Member Functions

uint32 OpenConnections (InternalIndex type, uint8 numConnections)
 
unsigned long EscapeString (char *to, const char *from, unsigned long length)
 
void Enqueue (SQLOperation *op)
 
T * GetFreeConnection ()
 
char constGetDatabaseName () const
 

Private Attributes

std::unique_ptr
< ProducerConsumerQueue
< SQLOperation * > > 
_queue
 Queue shared by async worker threads. More...
 
std::array< std::vector
< std::unique_ptr< T >
>, IDX_SIZE
_connections
 
std::unique_ptr
< MySQLConnectionInfo > 
_connectionInfo
 
uint8 _async_threads
 
uint8 _synch_threads
 

Member Typedef Documentation

template<class T>
typedef T::Statements DatabaseWorkerPool< T >::PreparedStatementIndex

Other

Member Enumeration Documentation

template<class T>
enum DatabaseWorkerPool::InternalIndex
private
Enumerator
IDX_ASYNC 
IDX_SYNCH 
IDX_SIZE 
52  {
53  IDX_ASYNC,
54  IDX_SYNCH,
55  IDX_SIZE
56  };
Definition: DatabaseWorkerPool.h:55
Definition: DatabaseWorkerPool.h:54
Definition: DatabaseWorkerPool.h:53

Constructor & Destructor Documentation

template<class T >
DatabaseWorkerPool< T >::DatabaseWorkerPool ( )
28 {
29  WPFatal(mysql_thread_safe(), "Used MySQL library isn't thread-safe.");
30  WPFatal(mysql_get_client_version() >= MIN_MYSQL_CLIENT_VERSION, "TrinityCore does not support MySQL versions below 5.1");
31  WPFatal(mysql_get_client_version() == MYSQL_VERSION_ID, "Used MySQL library version (%s) does not match the version used to compile TrinityCore (%s).",
32  mysql_get_client_info(), MYSQL_SERVER_VERSION);
33 }
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
Definition: DatabaseWorkerPool.h:301
uint8 _synch_threads
Definition: DatabaseWorkerPool.h:304
uint8 _async_threads
Definition: DatabaseWorkerPool.h:304
#define WPFatal(cond,...)
Definition: Errors.h:50
#define MIN_MYSQL_CLIENT_VERSION
Definition: DatabaseWorkerPool.cpp:22
template<class T>
DatabaseWorkerPool< T >::~DatabaseWorkerPool ( )
inline
63  {
64  _queue->Cancel();
65  }
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
Definition: DatabaseWorkerPool.h:301

Member Function Documentation

template<class T>
template<typename Format , typename... Args>
QueryResultFuture DatabaseWorkerPool< T >::AsyncPQuery ( Format &&  sql,
Args &&...  args 
)
inline

Enqueues a query in string format -with variable args- that will set the value of the QueryResultFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods.

201  {
202  return AsyncQuery(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
203  }
QueryResultFuture AsyncQuery(const char *sql)
Definition: DatabaseWorkerPool.cpp:149
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28

+ Here is the call graph for this function:

template<class T >
QueryResultFuture DatabaseWorkerPool< T >::AsyncQuery ( const char *  sql)

Asynchronous query (with resultset) methods.Enqueues a query in string format that will set the value of the QueryResultFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods.

150 {
151  BasicStatementTask* task = new BasicStatementTask(sql, true);
152  // Store future result before enqueueing - task might get already processed and deleted before returning from this method
153  QueryResultFuture result = task->GetFuture();
154  Enqueue(task);
155  return result;
156 }
std::future< QueryResult > QueryResultFuture
Definition: Callback.h:24
Definition: AdhocStatement.h:28
QueryResultFuture GetFuture() const
Definition: AdhocStatement.h:35
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
PreparedQueryResultFuture DatabaseWorkerPool< T >::AsyncQuery ( PreparedStatement stmt)

Enqueues a query in prepared format that will set the value of the PreparedQueryResultFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods. Statement must be prepared with CONNECTION_ASYNC flag.

160 {
161  PreparedStatementTask* task = new PreparedStatementTask(stmt, true);
162  // Store future result before enqueueing - task might get already processed and deleted before returning from this method
163  PreparedQueryResultFuture result = task->GetFuture();
164  Enqueue(task);
165  return result;
166 }
PreparedQueryResultFuture GetFuture()
Definition: PreparedStatement.h:169
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
Definition: PreparedStatement.h:162

+ Here is the call graph for this function:

template<class T>
SQLTransaction DatabaseWorkerPool< T >::BeginTransaction ( )
inline

Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0)

Transaction context methods.

222  {
223  return SQLTransaction(new Transaction);
224  }
Definition: Transaction.h:28
std::shared_ptr< Transaction > SQLTransaction
Definition: Transaction.h:58
template<class T >
void DatabaseWorkerPool< T >::Close ( )

Closes the actualy MySQL connection.

Shut down the synchronous connections There's no need for locking the connection, because DatabaseWorkerPool<>::Close should only be called after any other thread tasks in the core have exited, meaning there can be no concurrent access at this point.

73 {
74  TC_LOG_INFO("sql.driver", "Closing down DatabasePool '%s'.", GetDatabaseName());
75 
77  _connections[IDX_ASYNC].clear();
78 
79  TC_LOG_INFO("sql.driver", "Asynchronous connections on DatabasePool '%s' terminated. "
80  "Proceeding with synchronous connections.",
81  GetDatabaseName());
82 
87  _connections[IDX_SYNCH].clear();
88 
89  TC_LOG_INFO("sql.driver", "All connections on DatabasePool '%s' closed.", GetDatabaseName());
90 }
char const * GetDatabaseName() const
Definition: DatabaseWorkerPool.h:295
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
Definition: DatabaseWorkerPool.h:54
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302
Definition: DatabaseWorkerPool.h:53

+ Here is the caller graph for this function:

template<class T >
void DatabaseWorkerPool< T >::CommitTransaction ( SQLTransaction  transaction)

Enqueues a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations were appended to the transaction will be respected during execution.

180 {
181 #ifdef TRINITY_DEBUG
182  switch (transaction->GetSize())
186  {
187  case 0:
188  TC_LOG_DEBUG("sql.driver", "Transaction contains 0 queries. Not executing.");
189  return;
190  case 1:
191  TC_LOG_DEBUG("sql.driver", "Warning: Transaction only holds 1 query, consider removing Transaction context in code.");
192  break;
193  default:
194  break;
195  }
196 #endif // TRINITY_DEBUG
197 
198  Enqueue(new TransactionTask(transaction));
199 }
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:198
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
Definition: Transaction.h:61
template<class T >
QueryResultHolderFuture DatabaseWorkerPool< T >::DelayQueryHolder ( SQLQueryHolder holder)

Enqueues a vector of SQL operations (can be both adhoc and prepared) that will set the value of the QueryResultHolderFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods. Any prepared statements added to this holder need to be prepared with the CONNECTION_ASYNC flag.

170 {
171  SQLQueryHolderTask* task = new SQLQueryHolderTask(holder);
172  // Store future result before enqueueing - task might get already processed and deleted before returning from this method
173  QueryResultHolderFuture result = task->GetFuture();
174  Enqueue(task);
175  return result;
176 }
std::future< SQLQueryHolder * > QueryResultHolderFuture
Definition: QueryHolder.h:46
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
Definition: QueryHolder.h:49
QueryResultHolderFuture GetFuture()
Definition: QueryHolder.h:63

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
void DatabaseWorkerPool< T >::DirectCommitTransaction ( SQLTransaction transaction)

Directly executes a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations were appended to the transaction will be respected during execution.

Handle MySQL Errno 1213 without extending deadlock to the core itself

Todo:
More elegant way

Clean up now.

203 {
204  T* connection = GetFreeConnection();
205  int errorCode = connection->ExecuteTransaction(transaction);
206  if (!errorCode)
207  {
208  connection->Unlock(); // OK, operation succesful
209  return;
210  }
211 
214  if (errorCode == ER_LOCK_DEADLOCK)
215  {
216  uint8 loopBreaker = 5;
217  for (uint8 i = 0; i < loopBreaker; ++i)
218  {
219  if (!connection->ExecuteTransaction(transaction))
220  break;
221  }
222  }
223 
225  transaction->Cleanup();
226 
227  connection->Unlock();
228 }
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:303
uint8_t uint8
Definition: Define.h:152
template<class T>
void DatabaseWorkerPool< T >::DirectExecute ( const char *  sql)
inline

Direct synchronous one-way statement methods.Directly executes a one-way SQL operation in string format, that will block the calling thread until finished. This method should only be used for queries that are only executed once, e.g during startup.

122  {
123  if (!sql)
124  return;
125 
126  T* connection = GetFreeConnection();
127  connection->Execute(sql);
128  connection->Unlock();
129  }
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:303

+ Here is the caller graph for this function:

template<class T>
void DatabaseWorkerPool< T >::DirectExecute ( PreparedStatement stmt)
inline

Directly executes a one-way SQL operation in prepared statement format, that will block the calling thread until finished. Statement must be prepared with the CONNECTION_SYNCH flag.

Delete proxy-class. Not needed anymore

145  {
146  T* connection = GetFreeConnection();
147  connection->Execute(stmt);
148  connection->Unlock();
149 
151  delete stmt;
152  }
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:303

+ Here is the call graph for this function:

template<class T>
template<typename Format , typename... Args>
void DatabaseWorkerPool< T >::DirectPExecute ( Format &&  sql,
Args &&...  args 
)
inline

Directly executes a one-way SQL operation in string format -with variable args-, that will block the calling thread until finished. This method should only be used for queries that are only executed once, e.g during startup.

135  {
137  return;
138 
139  DirectExecute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
140  }
bool IsFormatEmptyOrNull(const char *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:34
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
void DirectExecute(const char *sql)
Definition: DatabaseWorkerPool.h:121

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
void DatabaseWorkerPool< T >::Enqueue ( SQLOperation op)
inlineprivate
287  {
288  _queue->Push(op);
289  }
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
Definition: DatabaseWorkerPool.h:301

+ Here is the caller graph for this function:

template<class T >
void DatabaseWorkerPool< T >::EscapeString ( std::string &  str)

Apply escape string'ing for current collation. (utf8)

232 {
233  if (str.empty())
234  return;
235 
236  char* buf = new char[str.size() * 2 + 1];
237  EscapeString(buf, str.c_str(), uint32(str.size()));
238  str = buf;
239  delete[] buf;
240 }
void EscapeString(std::string &str)
Apply escape string'ing for current collation. (utf8)
Definition: DatabaseWorkerPool.cpp:231
uint32_t uint32
Definition: g3dmath.h:168

+ Here is the caller graph for this function:

template<class T>
unsigned long DatabaseWorkerPool< T >::EscapeString ( char *  to,
const char *  from,
unsigned long  length 
)
inlineprivate
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  }
float length(float v)
Definition: vectorMath.h:208
Definition: DatabaseWorkerPool.h:54
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302
template<class T>
void DatabaseWorkerPool< T >::Execute ( const char *  sql)
inline

Delayed one-way statement methods.Enqueues a one-way SQL operation in string format that will be executed asynchronously. This method should only be used for queries that are only executed once, e.g during startup.

88  {
90  return;
91 
92  BasicStatementTask* task = new BasicStatementTask(sql);
93  Enqueue(task);
94  }
bool IsFormatEmptyOrNull(const char *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:34
Definition: AdhocStatement.h:28
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
template<class T>
void DatabaseWorkerPool< T >::Execute ( PreparedStatement stmt)
inline

Enqueues a one-way SQL operation in prepared statement format that will be executed asynchronously. Statement must be prepared with CONNECTION_ASYNC flag.

110  {
112  Enqueue(task);
113  }
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
Definition: PreparedStatement.h:162

+ Here is the call graph for this function:

template<class T>
void DatabaseWorkerPool< T >::ExecuteOrAppend ( SQLTransaction trans,
PreparedStatement stmt 
)
inline

Method used to execute prepared statements in a diverse context. Will be wrapped in a transaction if valid object is present, otherwise executed standalone.

237  {
238  if (!trans)
239  Execute(stmt);
240  else
241  trans->Append(stmt);
242  }
void Execute(const char *sql)
Definition: DatabaseWorkerPool.h:87

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
void DatabaseWorkerPool< T >::ExecuteOrAppend ( SQLTransaction trans,
const char *  sql 
)
inline

Method used to execute ad-hoc statements in a diverse context. Will be wrapped in a transaction if valid object is present, otherwise executed standalone.

247  {
248  if (!trans)
249  Execute(sql);
250  else
251  trans->Append(sql);
252  }
void Execute(const char *sql)
Definition: DatabaseWorkerPool.h:87

+ Here is the call graph for this function:

template<class T>
MySQLConnectionInfo const* DatabaseWorkerPool< T >::GetConnectionInfo ( ) const
inline
77  {
78  return _connectionInfo.get();
79  }
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:303

+ Here is the caller graph for this function:

template<class T>
char const* DatabaseWorkerPool< T >::GetDatabaseName ( ) const
inlineprivate
296  {
297  return _connectionInfo->database.c_str();
298  }
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:303
template<class T >
T * DatabaseWorkerPool< T >::GetFreeConnection ( )
private

Gets a free connection in the synchronous connection pool. Caller MUST call t->Unlock() after touching the MySQL context to prevent deadlocks.

Block forever until a connection is free

Must be matched with t->Unlock() or you will get deadlocks

304 {
305  uint8 i = 0;
306  auto const num_cons = _connections[IDX_SYNCH].size();
307  T* connection = nullptr;
309  for (;;)
310  {
311  connection = _connections[IDX_SYNCH][++i % num_cons].get();
313  if (connection->LockIfReady())
314  break;
315  }
316 
317  return connection;
318 }
uint8_t uint8
Definition: Define.h:152
Definition: DatabaseWorkerPool.h:54
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302

+ Here is the caller graph for this function:

template<class T>
PreparedStatement* DatabaseWorkerPool< T >::GetPreparedStatement ( PreparedStatementIndex  index)
inline

Automanaged (internally) pointer to a prepared statement object for usage in upper level code. Pointer is deleted in this->DirectExecute(PreparedStatement*), this->Query(PreparedStatement*) or PreparedStatementTask::~PreparedStatementTask. This object is not tied to the prepared statement on the MySQL context yet until execution.

264  {
265  return new PreparedStatement(index);
266  }
Definition: PreparedStatement.h:74
template<class T >
void DatabaseWorkerPool< T >::KeepAlive ( )

Keeps all our MySQL connections alive, prevent the server from disconnecting us.

Ping synchronous connections

Assuming all worker threads are free, every worker thread will receive 1 ping operation request If one or more worker threads are busy, the ping operations will not be split evenly, but this doesn't matter as the sole purpose is to prevent connections from idling.

244 {
246  for (auto& connection : _connections[IDX_SYNCH])
247  {
248  if (connection->LockIfReady())
249  {
250  connection->Ping();
251  connection->Unlock();
252  }
253  }
254 
258  auto const count = _connections[IDX_ASYNC].size();
259  for (uint8 i = 0; i < count; ++i)
260  Enqueue(new PingOperation);
261 }
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.h:286
Definition: DatabaseWorkerPool.h:37
uint8_t uint8
Definition: Define.h:152
Definition: DatabaseWorkerPool.h:54
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302
Definition: DatabaseWorkerPool.h:53

+ Here is the caller graph for this function:

template<class T >
uint32 DatabaseWorkerPool< T >::Open ( )
47 {
48  WPFatal(_connectionInfo.get(), "Connection info was not set!");
49 
50  TC_LOG_INFO("sql.driver", "Opening DatabasePool '%s'. "
51  "Asynchronous connections: %u, synchronous connections: %u.",
53 
55 
56  if (error)
57  return error;
58 
60 
61  if (!error)
62  {
63  TC_LOG_INFO("sql.driver", "DatabasePool '%s' opened successfully. " SZFMTD
64  " total connections running.", GetDatabaseName(),
65  (_connections[IDX_SYNCH].size() + _connections[IDX_ASYNC].size()));
66  }
67 
68  return error;
69 }
#define SZFMTD
Definition: Define.h:143
uint32 OpenConnections(InternalIndex type, uint8 numConnections)
Definition: DatabaseWorkerPool.cpp:264
uint8 _synch_threads
Definition: DatabaseWorkerPool.h:304
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:303
uint8 _async_threads
Definition: DatabaseWorkerPool.h:304
uint32_t uint32
Definition: Define.h:150
#define WPFatal(cond,...)
Definition: Errors.h:50
char const * GetDatabaseName() const
Definition: DatabaseWorkerPool.h:295
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
Definition: DatabaseWorkerPool.h:54
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302
Definition: DatabaseWorkerPool.h:53

+ Here is the caller graph for this function:

template<class T >
uint32 DatabaseWorkerPool< T >::OpenConnections ( InternalIndex  type,
uint8  numConnections 
)
private
265 {
266  for (uint8 i = 0; i < numConnections; ++i)
267  {
268  // Create the connection
269  auto connection = [&] {
270  switch (type)
271  {
272  case IDX_ASYNC:
273  return Trinity::make_unique<T>(_queue.get(), *_connectionInfo);
274  case IDX_SYNCH:
275  return Trinity::make_unique<T>(*_connectionInfo);
276  default:
277  ABORT();
278  }
279  }();
280 
281  if (uint32 error = connection->Open())
282  {
283  // Failed to open a connection or invalid version, abort and cleanup
284  _connections[type].clear();
285  return error;
286  }
287  else if (mysql_get_server_version(connection->GetHandle()) < MIN_MYSQL_SERVER_VERSION)
288  {
289  TC_LOG_ERROR("sql.driver", "TrinityCore does not support MySQL versions below 5.1");
290  return 1;
291  }
292  else
293  {
294  _connections[type].push_back(std::move(connection));
295  }
296  }
297 
298  // Everything is fine
299  return 0;
300 }
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
Definition: DatabaseWorkerPool.h:301
#define MIN_MYSQL_SERVER_VERSION
Definition: DatabaseWorkerPool.cpp:21
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:303
uint32_t uint32
Definition: Define.h:150
#define ABORT
Definition: Errors.h:56
uint8_t uint8
Definition: Define.h:152
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
Definition: DatabaseWorkerPool.h:54
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302
Definition: DatabaseWorkerPool.h:53
template<class T>
template<typename Format , typename... Args>
void DatabaseWorkerPool< T >::PExecute ( Format &&  sql,
Args &&...  args 
)
inline

Enqueues a one-way SQL operation in string format -with variable args- that will be executed asynchronously. This method should only be used for queries that are only executed once, e.g during startup.

100  {
102  return;
103 
104  Execute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
105  }
void Execute(const char *sql)
Definition: DatabaseWorkerPool.h:87
bool IsFormatEmptyOrNull(const char *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:34
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
template<typename Format , typename... Args>
QueryResult DatabaseWorkerPool< T >::PQuery ( Format &&  sql,
T *  conn,
Args &&...  args 
)
inline

Directly executes an SQL query in string format -with variable args- that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code.

166  {
168  return QueryResult(nullptr);
169 
170  return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str(), conn);
171  }
bool IsFormatEmptyOrNull(const char *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:34
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
QueryResult Query(const char *sql, T *connection=nullptr)
Definition: DatabaseWorkerPool.cpp:113

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
template<typename Format , typename... Args>
QueryResult DatabaseWorkerPool< T >::PQuery ( Format &&  sql,
Args &&...  args 
)
inline

Directly executes an SQL query in string format -with variable args- that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code.

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  }
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
QueryResult Query(const char *sql, T *connection=nullptr)
Definition: DatabaseWorkerPool.cpp:113

+ Here is the call graph for this function:

template<class T >
bool DatabaseWorkerPool< T >::PrepareStatements ( )

Prepares all prepared statements.

94 {
95  for (auto& connections : _connections)
96  for (auto& connection : connections)
97  {
98  connection->LockIfReady();
99  if (!connection->PrepareStatements())
100  {
101  connection->Unlock();
102  Close();
103  return false;
104  }
105  else
106  connection->Unlock();
107  }
108 
109  return true;
110 }
void Close()
Definition: DatabaseWorkerPool.cpp:72
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:302

+ Here is the caller graph for this function:

template<class T >
QueryResult DatabaseWorkerPool< T >::Query ( const char *  sql,
T *  connection = nullptr 
)

Synchronous query (with resultset) methods.Directly executes an SQL query in string format that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code.

114 {
115  if (!connection)
116  connection = GetFreeConnection();
117 
118  ResultSet* result = connection->Query(sql);
119  connection->Unlock();
120  if (!result || !result->GetRowCount() || !result->NextRow())
121  {
122  delete result;
123  return QueryResult(NULL);
124  }
125 
126  return QueryResult(result);
127 }
bool NextRow()
Definition: QueryResult.cpp:176
arena_t NULL
Definition: jemalloc_internal.h:624
Definition: QueryResult.h:30
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:303
uint64 GetRowCount() const
Definition: QueryResult.h:37
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61

+ Here is the call graph for this function:

template<class T >
PreparedQueryResult DatabaseWorkerPool< T >::Query ( PreparedStatement stmt)

Directly executes an SQL query in prepared format that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code. Statement must be prepared with CONNECTION_SYNCH flag.

Delete proxy-class. Not needed anymore

131 {
132  auto connection = GetFreeConnection();
133  PreparedResultSet* ret = connection->Query(stmt);
134  connection->Unlock();
135 
137  delete stmt;
138 
139  if (!ret || !ret->GetRowCount())
140  {
141  delete ret;
142  return PreparedQueryResult(NULL);
143  }
144 
145  return PreparedQueryResult(ret);
146 }
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
arena_t NULL
Definition: jemalloc_internal.h:624
uint64 GetRowCount() const
Definition: QueryResult.h:70
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:303
Definition: QueryResult.h:63

+ Here is the call graph for this function:

template<class T >
void DatabaseWorkerPool< T >::SetConnectionInfo ( std::string const infoString,
uint8 const  asyncThreads,
uint8 const  synchThreads 
)
38 {
39  _connectionInfo = Trinity::make_unique<MySQLConnectionInfo>(infoString);
40 
41  _async_threads = asyncThreads;
42  _synch_threads = synchThreads;
43 }
uint8 _synch_threads
Definition: DatabaseWorkerPool.h:304
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:303
uint8 _async_threads
Definition: DatabaseWorkerPool.h:304

+ Here is the caller graph for this function:

Member Data Documentation

template<class T>
uint8 DatabaseWorkerPool< T >::_async_threads
private
template<class T>
std::unique_ptr<MySQLConnectionInfo> DatabaseWorkerPool< T >::_connectionInfo
private
template<class T>
std::array<std::vector<std::unique_ptr<T> >, IDX_SIZE> DatabaseWorkerPool< T >::_connections
private
template<class T>
std::unique_ptr<ProducerConsumerQueue<SQLOperation*> > DatabaseWorkerPool< T >::_queue
private

Queue shared by async worker threads.

template<class T>
uint8 DatabaseWorkerPool< T >::_synch_threads
private

The documentation for this class was generated from the following files: