AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
DatabaseWorkerPool< T > Class Template Reference

#include "DatabaseWorkerPool.h"

Public Types

typedef T::Statements PreparedStatementIndex
 

Public Member Functions

 DatabaseWorkerPool ()
 
 ~DatabaseWorkerPool ()
 
void SetConnectionInfo (std::string_view infoString, uint8 const asyncThreads, uint8 const synchThreads)
 
uint32 Open ()
 
void Close ()
 
bool PrepareStatements ()
 Prepares all prepared statements.
 
MySQLConnectionInfo const * GetConnectionInfo () const
 
void Execute (std::string_view sql)
 
template<typename... Args>
void Execute (std::string_view sql, Args &&... args)
 
void Execute (PreparedStatement< T > *stmt)
 
void DirectExecute (std::string_view sql)
 
template<typename... Args>
void DirectExecute (std::string_view sql, Args &&... args)
 
void DirectExecute (PreparedStatement< T > *stmt)
 
QueryResult Query (std::string_view sql)
 
template<typename... Args>
QueryResult Query (std::string_view sql, Args &&... args)
 
PreparedQueryResult Query (PreparedStatement< T > *stmt)
 
QueryCallback AsyncQuery (std::string_view sql)
 
QueryCallback AsyncQuery (PreparedStatement< T > *stmt)
 
SQLQueryHolderCallback DelayQueryHolder (std::shared_ptr< SQLQueryHolder< T > > holder)
 
SQLTransaction< T > BeginTransaction ()
 Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0)
 
void CommitTransaction (SQLTransaction< T > transaction)
 
TransactionCallback AsyncCommitTransaction (SQLTransaction< T > transaction)
 
void DirectCommitTransaction (SQLTransaction< T > &transaction)
 
void ExecuteOrAppend (SQLTransaction< T > &trans, std::string_view sql)
 
void ExecuteOrAppend (SQLTransaction< T > &trans, PreparedStatement< T > *stmt)
 
PreparedStatement< T > * GetPreparedStatement (PreparedStatementIndex index)
 
void EscapeString (std::string &str)
 Apply escape string'ing for current collation. (utf8)
 
void KeepAlive ()
 Keeps all our MySQL connections alive, prevent the server from disconnecting us.
 
void WarnAboutSyncQueries (bool warn)
 
size_t QueueSize () const
 

Private Types

enum  InternalIndex {
  IDX_ASYNC ,
  IDX_SYNCH ,
  IDX_SIZE
}
 

Private Member Functions

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

Private Attributes

std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
 Queue shared by async worker threads.
 
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE_connections
 
std::unique_ptr< MySQLConnectionInfo_connectionInfo
 
std::vector< uint8_preparedStatementSize
 
uint8 _async_threads
 
uint8 _synch_threads
 

Detailed Description

template<class T>
class DatabaseWorkerPool< T >

Member Typedef Documentation

◆ PreparedStatementIndex

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

Other

Member Enumeration Documentation

◆ InternalIndex

template<class T >
enum DatabaseWorkerPool::InternalIndex
private
Enumerator
IDX_ASYNC 
IDX_SYNCH 
IDX_SIZE 
38 {
42 };
@ IDX_ASYNC
Definition: DatabaseWorkerPool.h:39
@ IDX_SIZE
Definition: DatabaseWorkerPool.h:41
@ IDX_SYNCH
Definition: DatabaseWorkerPool.h:40

Constructor & Destructor Documentation

◆ DatabaseWorkerPool()

template<class T >
DatabaseWorkerPool< T >::DatabaseWorkerPool
63 :
67{
68 WPFatal(mysql_thread_safe(), "Used MySQL library isn't thread-safe.");
69
70#if !defined(MARIADB_VERSION_ID) || MARIADB_VERSION_ID < 100600
71 bool isSupportClientDB = mysql_get_client_version() >= MIN_MYSQL_CLIENT_VERSION;
72 bool isSameClientDB = mysql_get_client_version() == MYSQL_VERSION_ID;
73#else // MariaDB 10.6+
74 bool isSupportClientDB = mysql_get_client_version() >= MIN_MYSQL_CLIENT_VERSION;
75 bool isSameClientDB = true; // Client version 3.2.3?
76#endif
77
78 WPFatal(isSupportClientDB, "AzerothCore does not support MySQL versions below 5.7 or MariaDB versions below 10.5.\n\nFound version: {} / {}. Server compiled with: {}.\nSearch the wiki for ACE00043 in Common Errors (https://www.azerothcore.org/wiki/common-errors#ace00043).",
79 mysql_get_client_info(), mysql_get_client_version(), MYSQL_VERSION_ID);
80 WPFatal(isSameClientDB, "Used MySQL library version ({} id {}) does not match the version id used to compile AzerothCore (id {}).\nSearch the wiki for ACE00046 in Common Errors (https://www.azerothcore.org/wiki/common-errors#ace00046).",
81 mysql_get_client_info(), mysql_get_client_version(), MYSQL_VERSION_ID);
82}
#define WPFatal(cond,...)
Definition: Errors.h:59
#define MIN_MYSQL_CLIENT_VERSION
Definition: DatabaseWorkerPool.cpp:49
Definition: PCQueue.h:29
uint8 _synch_threads
Definition: DatabaseWorkerPool.h:222
uint8 _async_threads
Definition: DatabaseWorkerPool.h:222
std::unique_ptr< ProducerConsumerQueue< SQLOperation * > > _queue
Queue shared by async worker threads.
Definition: DatabaseWorkerPool.h:218

References MIN_MYSQL_CLIENT_VERSION, and WPFatal.

◆ ~DatabaseWorkerPool()

template<class T >
DatabaseWorkerPool< T >::~DatabaseWorkerPool
86{
87 _queue->Cancel();
88}

Member Function Documentation

◆ AsyncCommitTransaction()

template<class T >
TransactionCallback DatabaseWorkerPool< T >::AsyncCommitTransaction ( SQLTransaction< T >  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.

286{
287#ifdef ACORE_DEBUG
291 switch (transaction->GetSize())
292 {
293 case 0:
294 LOG_DEBUG("sql.driver", "Transaction contains 0 queries. Not executing.");
295 break;
296 case 1:
297 LOG_DEBUG("sql.driver", "Warning: Transaction only holds 1 query, consider removing Transaction context in code.");
298 break;
299 default:
300 break;
301 }
302#endif // ACORE_DEBUG
303
305 TransactionFuture result = task->GetFuture();
306 Enqueue(task);
307 return TransactionCallback(std::move(result));
308}
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:171
std::future< bool > TransactionFuture
Definition: DatabaseEnvFwd.h:63
void Enqueue(SQLOperation *op)
Definition: DatabaseWorkerPool.cpp:433
Definition: Transaction.h:97
TransactionFuture GetFuture()
Definition: Transaction.h:101
Definition: Transaction.h:110

References TransactionWithResultTask::GetFuture(), and LOG_DEBUG.

◆ AsyncQuery() [1/2]

template<class T >
QueryCallback DatabaseWorkerPool< T >::AsyncQuery ( PreparedStatement< T > *  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.

237{
238 PreparedStatementTask* task = new PreparedStatementTask(stmt, true);
239 // Store future result before enqueueing - task might get already processed and deleted before returning from this method
240 PreparedQueryResultFuture result = task->GetFuture();
241 Enqueue(task);
242 return QueryCallback(std::move(result));
243}
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: DatabaseEnvFwd.h:51
Definition: PreparedStatement.h:171
PreparedQueryResultFuture GetFuture()
Definition: PreparedStatement.h:177
Definition: QueryCallback.h:30

References PreparedStatementTask::GetFuture().

◆ AsyncQuery() [2/2]

template<class T >
QueryCallback DatabaseWorkerPool< T >::AsyncQuery ( std::string_view  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.

227{
228 BasicStatementTask* task = new BasicStatementTask(sql, true);
229 // Store future result before enqueueing - task might get already processed and deleted before returning from this method
230 QueryResultFuture result = task->GetFuture();
231 Enqueue(task);
232 return QueryCallback(std::move(result));
233}
std::future< QueryResult > QueryResultFuture
Definition: DatabaseEnvFwd.h:29
Definition: AdhocStatement.h:27
QueryResultFuture GetFuture() const
Definition: AdhocStatement.h:33

References BasicStatementTask::GetFuture().

◆ BeginTransaction()

template<class T >
SQLTransaction< T > DatabaseWorkerPool< T >::BeginTransaction

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

Transaction context methods.

257{
258 return std::make_shared<Transaction<T>>();
259}

◆ Close()

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.

127{
128 LOG_INFO("sql.driver", "Closing down DatabasePool '{}'.", GetDatabaseName());
129
131 _connections[IDX_ASYNC].clear();
132
133 LOG_INFO("sql.driver", "Asynchronous connections on DatabasePool '{}' terminated. Proceeding with synchronous connections.",
135
140 _connections[IDX_SYNCH].clear();
141
142 LOG_INFO("sql.driver", "All connections on DatabasePool '{}' closed.", GetDatabaseName());
143}
#define LOG_INFO(filterType__,...)
Definition: Log.h:167
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE > _connections
Definition: DatabaseWorkerPool.h:219
std::string_view GetDatabaseName() const
Definition: DatabaseWorkerPool.cpp:473

References LOG_INFO.

Referenced by DatabaseLoader::AddDatabase().

◆ CommitTransaction()

template<class T >
void DatabaseWorkerPool< T >::CommitTransaction ( SQLTransaction< T >  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.

263{
264#ifdef ACORE_DEBUG
268 switch (transaction->GetSize())
269 {
270 case 0:
271 LOG_DEBUG("sql.driver", "Transaction contains 0 queries. Not executing.");
272 return;
273 case 1:
274 LOG_DEBUG("sql.driver", "Warning: Transaction only holds 1 query, consider removing Transaction context in code.");
275 break;
276 default:
277 break;
278 }
279#endif // ACORE_DEBUG
280
281 Enqueue(new TransactionTask(transaction));
282}
Definition: Transaction.h:76

References LOG_DEBUG.

◆ DelayQueryHolder()

template<class T >
SQLQueryHolderCallback DatabaseWorkerPool< T >::DelayQueryHolder ( std::shared_ptr< SQLQueryHolder< T > >  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.

247{
248 SQLQueryHolderTask* task = new SQLQueryHolderTask(holder);
249 // Store future result before enqueueing - task might get already processed and deleted before returning from this method
250 QueryResultHolderFuture result = task->GetFuture();
251 Enqueue(task);
252 return { std::move(holder), std::move(result) };
253}
std::future< void > QueryResultHolderFuture
Definition: DatabaseEnvFwd.h:81
Definition: QueryHolder.h:53
QueryResultHolderFuture GetFuture()
Definition: QueryHolder.h:61

References SQLQueryHolderTask::GetFuture().

◆ DirectCommitTransaction()

template<class T >
void DatabaseWorkerPool< T >::DirectCommitTransaction ( SQLTransaction< T > &  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.

312{
313 T* connection = GetFreeConnection();
314 int errorCode = connection->ExecuteTransaction(transaction);
315
316 if (!errorCode)
317 {
318 connection->Unlock(); // OK, operation succesful
319 return;
320 }
321
324 if (errorCode == ER_LOCK_DEADLOCK)
325 {
326 //todo: handle multiple sync threads deadlocking in a similar way as async threads
327 uint8 loopBreaker = 5;
328
329 for (uint8 i = 0; i < loopBreaker; ++i)
330 {
331 if (!connection->ExecuteTransaction(transaction))
332 break;
333 }
334 }
335
337 transaction->Cleanup();
338
339 connection->Unlock();
340}
std::uint8_t uint8
Definition: Define.h:110
T * GetFreeConnection()
Definition: DatabaseWorkerPool.cpp:445

◆ DirectExecute() [1/3]

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

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

508{
509 T* connection = GetFreeConnection();
510 connection->Execute(stmt);
511 connection->Unlock();
512
514 delete stmt;
515}

◆ DirectExecute() [2/3]

template<class T >
void DatabaseWorkerPool< T >::DirectExecute ( std::string_view  sql)

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.

497{
498 if (sql.empty())
499 return;
500
501 T* connection = GetFreeConnection();
502 connection->Execute(sql);
503 connection->Unlock();
504}

Referenced by DBUpdater< T >::Apply(), and DatabaseWorkerPool< T >::DirectExecute().

◆ DirectExecute() [3/3]

template<class T >
template<typename... Args>
void DatabaseWorkerPool< T >::DirectExecute ( std::string_view  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.

97 {
98 if (sql.empty())
99 return;
100
101 DirectExecute(Acore::StringFormatFmt(sql, std::forward<Args>(args)...));
102 }
std::string StringFormatFmt(std::string_view fmt, Args &&... args)
Definition: StringFormat.h:44
void DirectExecute(std::string_view sql)
Definition: DatabaseWorkerPool.cpp:496

References DatabaseWorkerPool< T >::DirectExecute(), and Acore::StringFormatFmt().

◆ Enqueue()

template<class T >
void DatabaseWorkerPool< T >::Enqueue ( SQLOperation op)
private
434{
435 _queue->Push(op);
436}

◆ EscapeString() [1/2]

template<class T >
unsigned long DatabaseWorkerPool< T >::EscapeString ( char *  to,
char const *  from,
unsigned long  length 
)
private
425{
426 if (!to || !from || !length)
427 return 0;
428
429 return _connections[IDX_SYNCH].front()->EscapeString(to, from, length);
430}

◆ EscapeString() [2/2]

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

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

350{
351 if (str.empty())
352 return;
353
354 char* buf = new char[str.size() * 2 + 1];
355 EscapeString(buf, str.c_str(), uint32(str.size()));
356 str = buf;
357 delete[] buf;
358}
std::uint32_t uint32
Definition: Define.h:108
void EscapeString(std::string &str)
Apply escape string'ing for current collation. (utf8)
Definition: DatabaseWorkerPool.cpp:349

◆ Execute() [1/3]

template<class T >
void DatabaseWorkerPool< T >::Execute ( PreparedStatement< T > *  stmt)

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

490{
492 Enqueue(task);
493}

◆ Execute() [2/3]

template<class T >
void DatabaseWorkerPool< T >::Execute ( std::string_view  sql)

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.

480{
481 if (sql.empty())
482 return;
483
484 BasicStatementTask* task = new BasicStatementTask(sql);
485 Enqueue(task);
486}

Referenced by DatabaseWorkerPool< T >::Execute().

◆ Execute() [3/3]

template<class T >
template<typename... Args>
void DatabaseWorkerPool< T >::Execute ( std::string_view  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.

74 {
75 if (sql.empty())
76 return;
77
78 Execute(Acore::StringFormatFmt(sql, std::forward<Args>(args)...));
79 }
void Execute(std::string_view sql)
Definition: DatabaseWorkerPool.cpp:479

References DatabaseWorkerPool< T >::Execute(), and Acore::StringFormatFmt().

◆ ExecuteOrAppend() [1/2]

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

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

528{
529 if (!trans)
530 Execute(stmt);
531 else
532 trans->Append(stmt);
533}

◆ ExecuteOrAppend() [2/2]

template<class T >
void DatabaseWorkerPool< T >::ExecuteOrAppend ( SQLTransaction< T > &  trans,
std::string_view  sql 
)

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.

519{
520 if (!trans)
521 Execute(sql);
522 else
523 trans->Append(sql);
524}

◆ GetConnectionInfo()

template<class T >
MySQLConnectionInfo const * DatabaseWorkerPool< T >::GetConnectionInfo ( ) const
inline
58 {
59 return _connectionInfo.get();
60 }
std::unique_ptr< MySQLConnectionInfo > _connectionInfo
Definition: DatabaseWorkerPool.h:220

References DatabaseWorkerPool< T >::_connectionInfo.

Referenced by DBUpdater< T >::ApplyFile(), DBUpdater< T >::Create(), and DBUpdater< T >::Update().

◆ GetDatabaseName()

template<class T >
std::string_view DatabaseWorkerPool< T >::GetDatabaseName
private
474{
475 return std::string_view{ _connectionInfo->database };
476}

◆ GetFreeConnection()

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

446{
447#ifdef ACORE_DEBUG
448 if (_warnSyncQueries)
449 {
450 std::ostringstream ss;
451 ss << boost::stacktrace::stacktrace();
452 LOG_WARN("sql.performances", "Sync query at:\n{}", ss.str());
453 }
454#endif
455
456 uint8 i = 0;
457 auto const num_cons = _connections[IDX_SYNCH].size();
458 T* connection = nullptr;
459
461 for (;;)
462 {
463 connection = _connections[IDX_SYNCH][++i % num_cons].get();
465 if (connection->LockIfReady())
466 break;
467 }
468
469 return connection;
470}
#define LOG_WARN(filterType__,...)
Definition: Log.h:163

References LOG_WARN.

◆ GetPreparedStatement()

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

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.

344{
345 return new PreparedStatement<T>(index, _preparedStatementSize[index]);
346}
Definition: PreparedStatement.h:158
std::vector< uint8 > _preparedStatementSize
Definition: DatabaseWorkerPool.h:221

◆ KeepAlive()

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.

362{
364 for (auto& connection : _connections[IDX_SYNCH])
365 {
366 if (connection->LockIfReady())
367 {
368 connection->Ping();
369 connection->Unlock();
370 }
371 }
372
376 auto const count = _connections[IDX_ASYNC].size();
377
378 for (uint8 i = 0; i < count; ++i)
380}
Definition: DatabaseWorkerPool.cpp:53

◆ Open()

template<class T >
uint32 DatabaseWorkerPool< T >::Open
101{
102 WPFatal(_connectionInfo.get(), "Connection info was not set!");
103
104 LOG_INFO("sql.driver", "Opening DatabasePool '{}'. Asynchronous connections: {}, synchronous connections: {}.",
106
108
109 if (error)
110 return error;
111
113
114 if (!error)
115 {
116 LOG_INFO("sql.driver", "DatabasePool '{}' opened successfully. {} total connections running.",
118 }
119
120 LOG_INFO("sql.driver", " ");
121
122 return error;
123}
uint32 OpenConnections(InternalIndex type, uint8 numConnections)
Definition: DatabaseWorkerPool.cpp:383

References LOG_INFO, and WPFatal.

Referenced by DatabaseLoader::AddDatabase().

◆ OpenConnections()

template<class T >
uint32 DatabaseWorkerPool< T >::OpenConnections ( InternalIndex  type,
uint8  numConnections 
)
private
384{
385 for (uint8 i = 0; i < numConnections; ++i)
386 {
387 // Create the connection
388 auto connection = [&]
389 {
390 switch (type)
391 {
392 case IDX_ASYNC:
393 return std::make_unique<T>(_queue.get(), *_connectionInfo);
394 case IDX_SYNCH:
395 return std::make_unique<T>(*_connectionInfo);
396 default:
397 ABORT();
398 }
399 }();
400
401 if (uint32 error = connection->Open())
402 {
403 // Failed to open a connection or invalid version, abort and cleanup
404 _connections[type].clear();
405 return error;
406 }
407 else if (connection->GetServerVersion() < MIN_MYSQL_SERVER_VERSION)
408 {
409 LOG_ERROR("sql.driver", "AzerothCore does not support MySQL versions below 5.7 or MariaDB versions below 10.5.\n\nFound server version: {}. Server compiled with: {}.",
410 connection->GetServerVersion(), MYSQL_VERSION_ID);
411 return 1;
412 }
413 else
414 {
415 _connections[type].push_back(std::move(connection));
416 }
417 }
418
419 // Everything is fine
420 return 0;
421}
#define ABORT
Definition: Errors.h:76
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
#define MIN_MYSQL_SERVER_VERSION
Definition: DatabaseWorkerPool.cpp:48

References ABORT, LOG_ERROR, and MIN_MYSQL_SERVER_VERSION.

◆ PrepareStatements()

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

Prepares all prepared statements.

147{
148 for (auto const& connections : _connections)
149 {
150 for (auto const& connection : connections)
151 {
152 connection->LockIfReady();
153 if (!connection->PrepareStatements())
154 {
155 connection->Unlock();
156 Close();
157 return false;
158 }
159 else
160 connection->Unlock();
161
162 size_t const preparedSize = connection->m_stmts.size();
163 if (_preparedStatementSize.size() < preparedSize)
164 _preparedStatementSize.resize(preparedSize);
165
166 for (size_t i = 0; i < preparedSize; ++i)
167 {
168 // already set by another connection
169 // (each connection only has prepared statements of it's own type sync/async)
170 if (_preparedStatementSize[i] > 0)
171 continue;
172
173 if (MySQLPreparedStatement* stmt = connection->m_stmts[i].get())
174 {
175 uint32 const paramCount = stmt->GetParameterCount();
176
177 // WH only supports uint8 indices.
178 ASSERT(paramCount < std::numeric_limits<uint8>::max());
179
180 _preparedStatementSize[i] = static_cast<uint8>(paramCount);
181 }
182 }
183 }
184 }
185
186 return true;
187}
#define ASSERT
Definition: Errors.h:68
void Close()
Definition: DatabaseWorkerPool.cpp:126
Definition: MySQLPreparedStatement.h:34

References ASSERT, and Close.

Referenced by DatabaseLoader::AddDatabase().

◆ Query() [1/3]

template<class T >
PreparedQueryResult DatabaseWorkerPool< T >::Query ( PreparedStatement< T > *  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

208{
209 auto connection = GetFreeConnection();
210 PreparedResultSet* ret = connection->Query(stmt);
211 connection->Unlock();
212
214 delete stmt;
215
216 if (!ret || !ret->GetRowCount())
217 {
218 delete ret;
219 return PreparedQueryResult(nullptr);
220 }
221
222 return PreparedQueryResult(ret);
223}
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:50
Definition: QueryResult.h:99
uint64 GetRowCount() const
Definition: QueryResult.h:105

References PreparedResultSet::GetRowCount().

◆ Query() [2/3]

template<class T >
QueryResult DatabaseWorkerPool< T >::Query ( std::string_view  sql)

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.

191{
192 auto connection = GetFreeConnection();
193
194 ResultSet* result = connection->Query(sql);
195 connection->Unlock();
196
197 if (!result || !result->GetRowCount() || !result->NextRow())
198 {
199 delete result;
200 return QueryResult(nullptr);
201 }
202
203 return QueryResult(result);
204}
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:28
Definition: QueryResult.h:49
uint64 GetRowCount() const
Definition: QueryResult.h:55
bool NextRow()
Definition: QueryResult.cpp:188

References ResultSet::GetRowCount(), and ResultSet::NextRow().

Referenced by DatabaseWorkerPool< T >::Query(), and DBUpdater< T >::Retrieve().

◆ Query() [3/3]

template<class T >
template<typename... Args>
QueryResult DatabaseWorkerPool< T >::Query ( std::string_view  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.

120 {
121 if (sql.empty())
122 return QueryResult(nullptr);
123
124 return Query(Acore::StringFormatFmt(sql, std::forward<Args>(args)...));
125 }
QueryResult Query(std::string_view sql)
Definition: DatabaseWorkerPool.cpp:190

References DatabaseWorkerPool< T >::Query(), and Acore::StringFormatFmt().

◆ QueueSize()

template<class T >
size_t DatabaseWorkerPool< T >::QueueSize
440{
441 return _queue->Size();
442}

◆ SetConnectionInfo()

template<class T >
void DatabaseWorkerPool< T >::SetConnectionInfo ( std::string_view  infoString,
uint8 const  asyncThreads,
uint8 const  synchThreads 
)
92{
93 _connectionInfo = std::make_unique<MySQLConnectionInfo>(infoString);
94
95 _async_threads = asyncThreads;
96 _synch_threads = synchThreads;
97}

Referenced by DatabaseLoader::AddDatabase().

◆ WarnAboutSyncQueries()

template<class T >
void DatabaseWorkerPool< T >::WarnAboutSyncQueries ( bool  warn)
inline
196 {
197#ifdef ACORE_DEBUG
198 _warnSyncQueries = warn;
199#endif
200 }

Member Data Documentation

◆ _async_threads

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

◆ _connectionInfo

template<class T >
std::unique_ptr<MySQLConnectionInfo> DatabaseWorkerPool< T >::_connectionInfo
private

◆ _connections

template<class T >
std::array<std::vector<std::unique_ptr<T> >, IDX_SIZE> DatabaseWorkerPool< T >::_connections
private

◆ _preparedStatementSize

template<class T >
std::vector<uint8> DatabaseWorkerPool< T >::_preparedStatementSize
private

◆ _queue

template<class T >
std::unique_ptr<ProducerConsumerQueue<SQLOperation*> > DatabaseWorkerPool< T >::_queue
private

Queue shared by async worker threads.

◆ _synch_threads

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