1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 14:07:46 +00:00
sapphire/src/common/Database/DbLoader.cpp

109 lines
2.7 KiB
C++
Raw Normal View History

#include "DbLoader.h"
#include <mysqld_error.h>
#include "ZoneDbConnection.h"
#include "DbWorkerPool.h"
2018-03-06 22:22:19 +01:00
#include "Logging/Logger.h"
Sapphire::Db::DbLoader::DbLoader()
{
}
template< class T >
Sapphire::Db::DbLoader& Sapphire::Db::DbLoader::addDb( Sapphire::Db::DbWorkerPool< T >& pool, const ConnectionInfo& info )
{
m_open.push( [ this, info, &pool ]()->bool
{
const uint8_t asyncThreads = info.asyncThreads;
const uint8_t synchThreads = info.syncThreads;
if( asyncThreads < 1 || asyncThreads > 32 )
{
2018-12-23 03:53:08 +01:00
Logger::error(
"database: invalid number of worker threads specified. Please pick a value between 1 and 32." );
return false;
}
pool.setConnectionInfo( info, asyncThreads, synchThreads );
if( uint32_t error = pool.open() )
{
// Database does not exist
if( error == ER_BAD_DB_ERROR )
{
return false;
}
if( error )
{
2018-12-23 03:53:08 +01:00
Logger::error( "DatabasePool failed to open." );
return false;
}
}
m_close.push( [ &pool ]
{ pool.close(); } );
return true;
} );
m_prepare.push( [ this, info, &pool ]()->bool
{
if( !pool.prepareStatements() )
{
2018-12-23 03:53:08 +01:00
Logger::error( "Could not prepare statements of the database, see log for details." );
return false;
}
return true;
} );
return *this;
}
bool Sapphire::Db::DbLoader::initDbs()
{
if( !openDatabases() )
return false;
if( !prepareStatements() )
return false;
return true;
}
bool Sapphire::Db::DbLoader::openDatabases()
{
return process( m_open );
}
bool Sapphire::Db::DbLoader::prepareStatements()
{
return process( m_prepare );
}
bool Sapphire::Db::DbLoader::process( std::queue< Predicate >& queue )
{
while( !queue.empty() )
{
if( !queue.front()() )
{
// Close all open databases which have a registered close operation
while( !m_close.empty() )
{
m_close.top()();
m_close.pop();
}
return false;
}
queue.pop();
}
return true;
}
template
Sapphire::Db::DbLoader&
Sapphire::Db::DbLoader::addDb< Sapphire::Db::ZoneDbConnection >( Sapphire::Db::DbWorkerPool< Sapphire::Db::ZoneDbConnection >&,
const ConnectionInfo& );