2016-03-03 01:31:51 +01:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2016-03-03 01:31:51 +01:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DatabaseWorkerPool.h"
2017-05-12 18:49:51 +02:00
#include "AdhocStatement.h"
#include "Common.h"
#include "Errors.h"
2023-12-15 12:06:59 +01:00
#include "IoContext.h"
2017-05-12 18:49:51 +02:00
#include "Implementation/LoginDatabase.h"
#include "Implementation/WorldDatabase.h"
#include "Implementation/CharacterDatabase.h"
#include "Implementation/HotfixDatabase.h"
#include "Log.h"
2019-11-01 16:21:14 +01:00
#include "MySQLPreparedStatement.h"
2017-05-12 18:49:51 +02:00
#include "PreparedStatement.h"
#include "ProducerConsumerQueue.h"
2017-01-13 21:38:03 +01:00
#include "QueryCallback.h"
2017-05-12 18:49:51 +02:00
#include "QueryHolder.h"
#include "QueryResult.h"
#include "Transaction.h"
2019-11-01 16:21:14 +01:00
#include "MySQLWorkaround.h"
2023-12-15 12:06:59 +01:00
#include <boost/asio/use_future.hpp>
2017-05-12 18:49:51 +02:00
#include <mysqld_error.h>
2023-12-17 23:21:10 +01:00
#include <utility>
2020-07-30 19:42:27 +00:00
#ifdef TRINITY_DEBUG
#include <sstream>
#include <boost/stacktrace.hpp>
#endif
2016-03-03 01:31:51 +01:00
2021-08-18 10:16:06 +02:00
#define MIN_MYSQL_SERVER_VERSION 50700u
2021-08-27 18:40:20 +02:00
#define MIN_MYSQL_SERVER_VERSION_STRING "5.7"
2021-08-18 10:16:06 +02:00
#define MIN_MYSQL_CLIENT_VERSION 50700u
2021-08-27 18:40:20 +02:00
#define MIN_MYSQL_CLIENT_VERSION_STRING "5.7"
2016-03-03 01:31:51 +01:00
2022-06-14 14:10:25 +02:00
#define MIN_MARIADB_SERVER_VERSION 100209u
#define MIN_MARIADB_SERVER_VERSION_STRING "10.2.9"
#define MIN_MARIADB_CLIENT_VERSION 30003u
#define MIN_MARIADB_CLIENT_VERSION_STRING "3.0.3"
2023-12-15 12:06:59 +01:00
template < typename T >
struct DatabaseWorkerPool < T >:: QueueSizeTracker
2017-05-12 18:49:51 +02:00
{
2023-12-15 12:06:59 +01:00
explicit QueueSizeTracker ( DatabaseWorkerPool * pool ) : _pool ( pool )
2017-05-12 18:49:51 +02:00
{
2023-12-15 12:06:59 +01:00
++ _pool -> _queueSize ;
2017-05-12 18:49:51 +02:00
}
2023-12-15 12:06:59 +01:00
QueueSizeTracker ( QueueSizeTracker const & other ) : _pool ( other . _pool ) { ++ _pool -> _queueSize ; }
QueueSizeTracker ( QueueSizeTracker && other ) noexcept : _pool ( std :: exchange ( other . _pool , nullptr )) { }
QueueSizeTracker & operator = ( QueueSizeTracker const & other )
{
if ( this != & other )
{
if ( _pool != other . _pool )
{
if ( _pool )
-- _pool -> _queueSize ;
if ( other . _pool )
++ other . _pool -> _queueSize ;
}
_pool = other . _pool ;
}
return * this ;
}
QueueSizeTracker & operator = ( QueueSizeTracker && other ) noexcept
{
if ( this != & other )
{
if ( _pool != other . _pool )
{
if ( _pool )
-- _pool -> _queueSize ;
}
_pool = std :: exchange ( other . _pool , nullptr );
}
return * this ;
}
~ QueueSizeTracker ()
{
if ( _pool )
-- _pool -> _queueSize ;
}
private :
DatabaseWorkerPool * _pool ;
2017-05-12 18:49:51 +02:00
};
2016-03-03 01:31:51 +01:00
template < class T >
DatabaseWorkerPool < T >:: DatabaseWorkerPool ()
2023-12-15 12:06:59 +01:00
: _async_threads ( 0 ), _synch_threads ( 0 )
2016-03-03 01:31:51 +01:00
{
WPFatal ( mysql_thread_safe (), "Used MySQL library isn't thread-safe." );
2022-06-14 14:10:25 +02:00
2022-06-23 13:53:51 +02:00
#if defined(LIBMARIADB) && MARIADB_PACKAGE_VERSION_ID >= 30200
2022-06-14 14:10:25 +02:00
WPFatal ( mysql_get_client_version () >= MIN_MARIADB_CLIENT_VERSION , "TrinityCore does not support MariaDB versions below " MIN_MARIADB_CLIENT_VERSION_STRING " (found %s id %lu, need id >= %u), please update your MariaDB client library" , mysql_get_client_info (), mysql_get_client_version (), MIN_MARIADB_CLIENT_VERSION );
WPFatal ( mysql_get_client_version () == MARIADB_PACKAGE_VERSION_ID , "Used MariaDB library version (%s id %lu) does not match the version id used to compile TrinityCore (id %u). Search on forum for TCE00011." , mysql_get_client_info (), mysql_get_client_version (), MARIADB_PACKAGE_VERSION_ID );
2022-06-23 13:53:51 +02:00
#else
WPFatal ( mysql_get_client_version () >= MIN_MYSQL_CLIENT_VERSION , "TrinityCore does not support MySQL versions below " MIN_MYSQL_CLIENT_VERSION_STRING " (found %s id %lu, need id >= %u), please update your MySQL client library" , mysql_get_client_info (), mysql_get_client_version (), MIN_MYSQL_CLIENT_VERSION );
WPFatal ( mysql_get_client_version () == MYSQL_VERSION_ID , "Used MySQL library version (%s id %lu) does not match the version id used to compile TrinityCore (id %u). Search on forum for TCE00011." , mysql_get_client_info (), mysql_get_client_version (), MYSQL_VERSION_ID );
2022-06-14 14:10:25 +02:00
#endif
2016-03-03 01:31:51 +01:00
}
2017-05-11 22:29:51 +02:00
template < class T >
DatabaseWorkerPool < T >::~ DatabaseWorkerPool ()
{
}
2016-03-03 01:31:51 +01:00
template < class T >
void DatabaseWorkerPool < T >:: SetConnectionInfo ( std :: string const & infoString ,
uint8 const asyncThreads , uint8 const synchThreads )
{
2020-06-23 08:54:12 +02:00
_connectionInfo = std :: make_unique < MySQLConnectionInfo > ( infoString );
2016-03-03 01:31:51 +01:00
_async_threads = asyncThreads ;
_synch_threads = synchThreads ;
}
template < class T >
uint32 DatabaseWorkerPool < T >:: Open ()
{
WPFatal ( _connectionInfo . get (), "Connection info was not set!" );
2023-01-08 21:16:53 +01:00
TC_LOG_INFO ( "sql.driver" , "Opening DatabasePool '{}'. "
"Asynchronous connections: {}, synchronous connections: {}." ,
2016-03-03 01:31:51 +01:00
GetDatabaseName (), _async_threads , _synch_threads );
2023-12-15 12:06:59 +01:00
_ioContext = std :: make_unique < Trinity :: Asio :: IoContext > ( _async_threads );
2016-03-03 01:31:51 +01:00
uint32 error = OpenConnections ( IDX_ASYNC , _async_threads );
if ( error )
return error ;
error = OpenConnections ( IDX_SYNCH , _synch_threads );
2023-12-21 00:04:14 +01:00
if ( error )
return error ;
for ( std :: unique_ptr < T > const & connection : _connections [ IDX_ASYNC ])
connection -> StartWorkerThread ( _ioContext . get ());
TC_LOG_INFO ( "sql.driver" , "DatabasePool '{}' opened successfully. "
"{} total connections running." , GetDatabaseName (),
( _connections [ IDX_SYNCH ]. size () + _connections [ IDX_ASYNC ]. size ()));
2016-03-03 01:31:51 +01:00
2023-12-21 00:04:14 +01:00
return 0 ;
2016-03-03 01:31:51 +01:00
}
template < class T >
void DatabaseWorkerPool < T >:: Close ()
{
2023-01-08 21:16:53 +01:00
TC_LOG_INFO ( "sql.driver" , "Closing down DatabasePool '{}'." , GetDatabaseName ());
2016-03-03 01:31:51 +01:00
2023-12-29 00:31:55 +01:00
if ( _ioContext )
_ioContext -> stop ();
2023-12-15 12:06:59 +01:00
2016-03-03 01:31:51 +01:00
//! Closes the actualy MySQL connection.
_connections [ IDX_ASYNC ]. clear ();
2023-12-15 12:06:59 +01:00
_ioContext . reset ();
2023-01-08 21:16:53 +01:00
TC_LOG_INFO ( "sql.driver" , "Asynchronous connections on DatabasePool '{}' terminated. "
2016-03-03 01:31:51 +01:00
"Proceeding with synchronous connections." ,
GetDatabaseName ());
//! 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.
_connections [ IDX_SYNCH ]. clear ();
2023-01-08 21:16:53 +01:00
TC_LOG_INFO ( "sql.driver" , "All connections on DatabasePool '{}' closed." , GetDatabaseName ());
2016-03-03 01:31:51 +01:00
}
template < class T >
bool DatabaseWorkerPool < T >:: PrepareStatements ()
{
for ( auto & connections : _connections )
2017-03-01 22:20:53 -03:00
{
2016-03-03 01:31:51 +01:00
for ( auto & connection : connections )
{
connection -> LockIfReady ();
if ( ! connection -> PrepareStatements ())
{
connection -> Unlock ();
Close ();
return false ;
}
else
connection -> Unlock ();
2017-03-01 22:20:53 -03:00
size_t const preparedSize = connection -> m_stmts . size ();
if ( _preparedStatementSize . size () < preparedSize )
_preparedStatementSize . resize ( preparedSize );
for ( size_t i = 0 ; i < preparedSize ; ++ i )
{
// already set by another connection
// (each connection only has prepared statements of it's own type sync/async)
if ( _preparedStatementSize [ i ] > 0 )
continue ;
if ( MySQLPreparedStatement * stmt = connection -> m_stmts [ i ]. get ())
{
uint32 const paramCount = stmt -> GetParameterCount ();
// TC only supports uint8 indices.
ASSERT ( paramCount < std :: numeric_limits < uint8 >:: max ());
_preparedStatementSize [ i ] = static_cast < uint8 > ( paramCount );
}
}
2016-03-03 01:31:51 +01:00
}
2017-03-01 22:20:53 -03:00
}
2016-03-03 01:31:51 +01:00
return true ;
}
template < class T >
2020-09-04 13:38:24 +02:00
QueryResult DatabaseWorkerPool < T >:: Query ( char const * sql , T * connection /*= nullptr*/ )
2016-03-03 01:31:51 +01:00
{
if ( ! connection )
connection = GetFreeConnection ();
2023-12-15 12:06:59 +01:00
QueryResult result = BasicStatementTask :: Query ( connection , sql );
2016-03-03 01:31:51 +01:00
connection -> Unlock ();
2023-12-15 12:06:59 +01:00
return result ;
2016-03-03 01:31:51 +01:00
}
template < class T >
2019-07-27 01:00:37 +02:00
PreparedQueryResult DatabaseWorkerPool < T >:: Query ( PreparedStatement < T >* stmt )
2016-03-03 01:31:51 +01:00
{
2023-12-15 12:06:59 +01:00
T * connection = GetFreeConnection ();
PreparedQueryResult ret = PreparedStatementTask :: Query ( connection , stmt );
2016-03-03 01:31:51 +01:00
connection -> Unlock ();
//! Delete proxy-class. Not needed anymore
delete stmt ;
2023-12-15 12:06:59 +01:00
return ret ;
2016-03-03 01:31:51 +01:00
}
template < class T >
2020-09-04 13:38:24 +02:00
QueryCallback DatabaseWorkerPool < T >:: AsyncQuery ( char const * sql )
2016-03-03 01:31:51 +01:00
{
2023-12-15 12:06:59 +01:00
QueryResultFuture result = boost :: asio :: post ( _ioContext -> get_executor (), boost :: asio :: use_future ([ this , sql = std :: string ( sql ), tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
return BasicStatementTask :: Query ( conn , sql . c_str ());
}));
2017-01-13 21:38:03 +01:00
return QueryCallback ( std :: move ( result ));
2016-03-03 01:31:51 +01:00
}
template < class T >
2019-07-27 01:00:37 +02:00
QueryCallback DatabaseWorkerPool < T >:: AsyncQuery ( PreparedStatement < T >* stmt )
2016-03-03 01:31:51 +01:00
{
2023-12-15 12:06:59 +01:00
PreparedQueryResultFuture result = boost :: asio :: post ( _ioContext -> get_executor (), boost :: asio :: use_future ([ this , stmt = std :: unique_ptr < PreparedStatement < T >> ( stmt ), tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
return PreparedStatementTask :: Query ( conn , stmt . get ());
}));
2017-01-13 21:38:03 +01:00
return QueryCallback ( std :: move ( result ));
2016-03-03 01:31:51 +01:00
}
template < class T >
2020-10-24 17:37:24 +02:00
SQLQueryHolderCallback DatabaseWorkerPool < T >:: DelayQueryHolder ( std :: shared_ptr < SQLQueryHolder < T >> holder )
2016-03-03 01:31:51 +01:00
{
2023-12-15 12:06:59 +01:00
QueryResultHolderFuture result = boost :: asio :: post ( _ioContext -> get_executor (), boost :: asio :: use_future ([ this , holder , tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
SQLQueryHolderTask :: Execute ( conn , holder . get ());
}));
2020-10-24 17:37:24 +02:00
return { std :: move ( holder ), std :: move ( result ) };
2016-03-03 01:31:51 +01:00
}
2017-05-11 22:29:51 +02:00
template < class T >
2019-07-27 10:52:33 +02:00
SQLTransaction < T > DatabaseWorkerPool < T >:: BeginTransaction ()
2017-05-11 22:29:51 +02:00
{
2019-07-27 10:52:33 +02:00
return std :: make_shared < Transaction < T >> ();
2017-05-11 22:29:51 +02:00
}
2016-03-03 01:31:51 +01:00
template < class T >
2019-07-27 10:52:33 +02:00
void DatabaseWorkerPool < T >:: CommitTransaction ( SQLTransaction < T > transaction )
2016-03-03 01:31:51 +01:00
{
#ifdef TRINITY_DEBUG
//! Only analyze transaction weaknesses in Debug mode.
//! Ideally we catch the faults in Debug mode and then correct them,
//! so there's no need to waste these CPU cycles in Release mode.
switch ( transaction -> GetSize ())
{
case 0 :
TC_LOG_DEBUG ( "sql.driver" , "Transaction contains 0 queries. Not executing." );
return ;
case 1 :
TC_LOG_DEBUG ( "sql.driver" , "Warning: Transaction only holds 1 query, consider removing Transaction context in code." );
break ;
default :
break ;
}
#endif // TRINITY_DEBUG
2023-12-15 12:06:59 +01:00
boost :: asio :: post ( _ioContext -> get_executor (), [ this , transaction , tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
TransactionTask :: Execute ( conn , transaction );
});
2016-03-03 01:31:51 +01:00
}
2020-04-14 16:23:44 +02:00
template < class T >
TransactionCallback DatabaseWorkerPool < T >:: AsyncCommitTransaction ( SQLTransaction < T > transaction )
{
#ifdef TRINITY_DEBUG
//! Only analyze transaction weaknesses in Debug mode.
//! Ideally we catch the faults in Debug mode and then correct them,
//! so there's no need to waste these CPU cycles in Release mode.
switch ( transaction -> GetSize ())
{
case 0 :
TC_LOG_DEBUG ( "sql.driver" , "Transaction contains 0 queries. Not executing." );
2020-04-25 00:15:26 +02:00
break ;
2020-04-14 16:23:44 +02:00
case 1 :
TC_LOG_DEBUG ( "sql.driver" , "Warning: Transaction only holds 1 query, consider removing Transaction context in code." );
break ;
default :
break ;
}
#endif // TRINITY_DEBUG
2023-12-15 12:06:59 +01:00
TransactionFuture result = boost :: asio :: post ( _ioContext -> get_executor (), boost :: asio :: use_future ([ this , transaction , tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
return TransactionTask :: Execute ( conn , transaction );
}));
2020-04-14 16:23:44 +02:00
return TransactionCallback ( std :: move ( result ));
}
2016-03-03 01:31:51 +01:00
template < class T >
2019-07-27 10:52:33 +02:00
void DatabaseWorkerPool < T >:: DirectCommitTransaction ( SQLTransaction < T >& transaction )
2016-03-03 01:31:51 +01:00
{
T * connection = GetFreeConnection ();
int errorCode = connection -> ExecuteTransaction ( transaction );
if ( ! errorCode )
{
connection -> Unlock (); // OK, operation succesful
return ;
}
//! Handle MySQL Errno 1213 without extending deadlock to the core itself
/// @todo More elegant way
if ( errorCode == ER_LOCK_DEADLOCK )
{
2018-02-10 16:07:56 +01:00
//todo: handle multiple sync threads deadlocking in a similar way as async threads
2016-03-03 01:31:51 +01:00
uint8 loopBreaker = 5 ;
for ( uint8 i = 0 ; i < loopBreaker ; ++ i )
{
if ( ! connection -> ExecuteTransaction ( transaction ))
break ;
}
}
//! Clean up now.
transaction -> Cleanup ();
connection -> Unlock ();
}
2017-05-12 18:49:51 +02:00
template < class T >
2019-07-27 01:00:37 +02:00
PreparedStatement < T >* DatabaseWorkerPool < T >:: GetPreparedStatement ( PreparedStatementIndex index )
2017-05-12 18:49:51 +02:00
{
2017-03-01 22:20:53 -03:00
return new PreparedStatement < T > ( index , _preparedStatementSize [ index ]);
2017-05-12 18:49:51 +02:00
}
2016-03-03 01:31:51 +01:00
template < class T >
void DatabaseWorkerPool < T >:: EscapeString ( std :: string & str )
{
if ( str . empty ())
return ;
char * buf = new char [ str . size () * 2 + 1 ];
2016-04-05 00:15:56 +02:00
EscapeString ( buf , str . c_str (), uint32 ( str . size ()));
2016-03-03 01:31:51 +01:00
str = buf ;
delete [] buf ;
}
template < class T >
void DatabaseWorkerPool < T >:: KeepAlive ()
{
//! Ping synchronous connections
for ( auto & connection : _connections [ IDX_SYNCH ])
{
if ( connection -> LockIfReady ())
{
connection -> Ping ();
connection -> Unlock ();
}
}
//! 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.
auto const count = _connections [ IDX_ASYNC ]. size ();
for ( uint8 i = 0 ; i < count ; ++ i )
2023-12-15 12:06:59 +01:00
{
boost :: asio :: post ( _ioContext -> get_executor (), [ this , tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
conn -> Ping ();
});
}
2016-03-03 01:31:51 +01:00
}
template < class T >
uint32 DatabaseWorkerPool < T >:: OpenConnections ( InternalIndex type , uint8 numConnections )
{
for ( uint8 i = 0 ; i < numConnections ; ++ i )
{
// Create the connection
2023-12-21 00:04:14 +01:00
constexpr std :: array < ConnectionFlags , IDX_SIZE > flags = { { CONNECTION_ASYNC , CONNECTION_SYNCH } };
std :: unique_ptr < T > connection = std :: make_unique < T > ( * _connectionInfo , flags [ type ]);
2016-03-03 01:31:51 +01:00
if ( uint32 error = connection -> Open ())
{
// Failed to open a connection or invalid version, abort and cleanup
_connections [ type ]. clear ();
return error ;
}
2022-06-14 14:10:25 +02:00
#ifndef LIBMARIADB
2019-11-01 16:21:14 +01:00
else if ( connection -> GetServerVersion () < MIN_MYSQL_SERVER_VERSION )
2022-06-14 14:10:25 +02:00
#else
else if ( connection -> GetServerVersion () < MIN_MARIADB_SERVER_VERSION )
#endif
2016-03-03 01:31:51 +01:00
{
2022-06-14 14:10:25 +02:00
#ifndef LIBMARIADB
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "sql.driver" , "TrinityCore does not support MySQL versions below " MIN_MYSQL_SERVER_VERSION_STRING " (found id {}, need id >= {}), please update your MySQL server" , connection -> GetServerVersion (), MIN_MYSQL_SERVER_VERSION );
2022-06-14 14:10:25 +02:00
#else
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "sql.driver" , "TrinityCore does not support MariaDB versions below " MIN_MARIADB_SERVER_VERSION_STRING " (found id {}, need id >= {}), please update your MySQL server" , connection -> GetServerVersion (), MIN_MARIADB_SERVER_VERSION );
2022-06-14 14:10:25 +02:00
#endif
2016-03-03 01:31:51 +01:00
return 1 ;
}
else
{
_connections [ type ]. push_back ( std :: move ( connection ));
}
}
// Everything is fine
return 0 ;
}
2017-05-12 18:49:51 +02:00
template < class T >
2020-09-04 13:38:24 +02:00
unsigned long DatabaseWorkerPool < T >:: EscapeString ( char * to , char const * from , unsigned long length )
2017-05-12 18:49:51 +02:00
{
if ( ! to || ! from || ! length )
return 0 ;
2019-11-01 16:21:14 +01:00
return _connections [ IDX_SYNCH ]. front () -> EscapeString ( to , from , length );
2017-05-12 18:49:51 +02:00
}
2021-07-24 14:50:30 +02:00
template < class T >
size_t DatabaseWorkerPool < T >:: QueueSize () const
{
2023-12-15 12:06:59 +01:00
return _queueSize ;
2021-07-24 14:50:30 +02:00
}
2016-03-03 01:31:51 +01:00
template < class T >
T * DatabaseWorkerPool < T >:: GetFreeConnection ()
{
2020-07-30 19:42:27 +00:00
#ifdef TRINITY_DEBUG
if ( _warnSyncQueries )
{
std :: ostringstream ss ;
ss << boost :: stacktrace :: stacktrace ();
2023-01-08 21:16:53 +01:00
TC_LOG_WARN ( "sql.performances" , "Sync query at: \n {}" , ss . str ());
2020-07-30 19:42:27 +00:00
}
#endif
2016-03-03 01:31:51 +01:00
uint8 i = 0 ;
auto const num_cons = _connections [ IDX_SYNCH ]. size ();
T * connection = nullptr ;
//! Block forever until a connection is free
for (;;)
{
2016-10-31 15:16:46 +02:00
connection = _connections [ IDX_SYNCH ][ i ++ % num_cons ]. get ();
2016-03-03 01:31:51 +01:00
//! Must be matched with t->Unlock() or you will get deadlocks
if ( connection -> LockIfReady ())
break ;
}
return connection ;
}
2023-12-15 12:06:59 +01:00
template < class T >
T * DatabaseWorkerPool < T >:: GetAsyncConnectionForCurrentThread () const
{
std :: thread :: id id = std :: this_thread :: get_id ();
for ( auto && connection : _connections [ IDX_ASYNC ])
if ( connection -> GetWorkerThreadId () == id )
return connection . get ();
return nullptr ;
}
2017-05-12 18:49:51 +02:00
template < class T >
char const * DatabaseWorkerPool < T >:: GetDatabaseName () const
{
return _connectionInfo -> database . c_str ();
}
template < class T >
2020-09-04 13:38:24 +02:00
void DatabaseWorkerPool < T >:: Execute ( char const * sql )
2017-05-12 18:49:51 +02:00
{
2023-01-08 21:16:53 +01:00
if ( ! sql )
2017-05-12 18:49:51 +02:00
return ;
2023-12-15 12:06:59 +01:00
boost :: asio :: post ( _ioContext -> get_executor (), [ this , sql = std :: string ( sql ), tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
BasicStatementTask :: Execute ( conn , sql . c_str ());
});
2017-05-12 18:49:51 +02:00
}
template < class T >
2019-07-27 01:00:37 +02:00
void DatabaseWorkerPool < T >:: Execute ( PreparedStatement < T >* stmt )
2017-05-12 18:49:51 +02:00
{
2023-12-15 12:06:59 +01:00
boost :: asio :: post ( _ioContext -> get_executor (), [ this , stmt = std :: unique_ptr < PreparedStatement < T >> ( stmt ), tracker = QueueSizeTracker ( this )]
{
T * conn = GetAsyncConnectionForCurrentThread ();
PreparedStatementTask :: Execute ( conn , stmt . get ());
});
2017-05-12 18:49:51 +02:00
}
template < class T >
2020-09-04 13:38:24 +02:00
void DatabaseWorkerPool < T >:: DirectExecute ( char const * sql )
2017-05-12 18:49:51 +02:00
{
2023-01-08 21:16:53 +01:00
if ( ! sql )
2017-05-12 18:49:51 +02:00
return ;
T * connection = GetFreeConnection ();
2023-12-15 12:06:59 +01:00
BasicStatementTask :: Execute ( connection , sql );
2017-05-12 18:49:51 +02:00
connection -> Unlock ();
}
template < class T >
2019-07-27 01:00:37 +02:00
void DatabaseWorkerPool < T >:: DirectExecute ( PreparedStatement < T >* stmt )
2017-05-12 18:49:51 +02:00
{
T * connection = GetFreeConnection ();
2023-12-15 12:06:59 +01:00
PreparedStatementTask :: Execute ( connection , stmt );
2017-05-12 18:49:51 +02:00
connection -> Unlock ();
//! Delete proxy-class. Not needed anymore
delete stmt ;
}
template < class T >
2020-09-04 13:38:24 +02:00
void DatabaseWorkerPool < T >:: ExecuteOrAppend ( SQLTransaction < T >& trans , char const * sql )
2017-05-12 18:49:51 +02:00
{
if ( ! trans )
Execute ( sql );
else
trans -> Append ( sql );
}
template < class T >
2019-07-27 10:52:33 +02:00
void DatabaseWorkerPool < T >:: ExecuteOrAppend ( SQLTransaction < T >& trans , PreparedStatement < T >* stmt )
2017-05-12 18:49:51 +02:00
{
if ( ! trans )
Execute ( stmt );
else
trans -> Append ( stmt );
}
2016-03-11 15:57:26 +01:00
template class TC_DATABASE_API DatabaseWorkerPool < LoginDatabaseConnection > ;
template class TC_DATABASE_API DatabaseWorkerPool < WorldDatabaseConnection > ;
template class TC_DATABASE_API DatabaseWorkerPool < CharacterDatabaseConnection > ;
template class TC_DATABASE_API DatabaseWorkerPool < HotfixDatabaseConnection > ;