2017-09-30 23:51:01 +02:00
|
|
|
#include "DbConnection.h"
|
|
|
|
#include "DbWorker.h"
|
|
|
|
#include "src/libraries/sapphire/mysqlConnector/MySqlConnector.h"
|
|
|
|
|
|
|
|
#include "src/servers/Server_Common/Logging/Logger.h"
|
|
|
|
#include "PreparedStatement.h"
|
2017-10-07 23:10:13 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
2017-09-30 23:51:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
extern Core::Logger g_log;
|
|
|
|
|
|
|
|
Core::Db::DbConnection::DbConnection( ConnectionInfo &connInfo ) :
|
|
|
|
m_reconnecting( false ),
|
|
|
|
m_prepareError( false ),
|
|
|
|
m_queue( nullptr ),
|
|
|
|
m_pConnection( nullptr ),
|
|
|
|
m_connectionInfo( connInfo ),
|
|
|
|
m_connectionFlags( CONNECTION_SYNCH )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
Core::Db::DbConnection::DbConnection( Core::LockedWaitQueue< boost::shared_ptr< Operation > >* queue,
|
|
|
|
Core::Db::ConnectionInfo& connInfo ) :
|
2017-09-30 23:51:01 +02:00
|
|
|
m_reconnecting( false ),
|
|
|
|
m_prepareError( false ),
|
|
|
|
m_queue( queue ),
|
|
|
|
m_pConnection( nullptr ),
|
|
|
|
m_connectionInfo( connInfo ),
|
|
|
|
m_connectionFlags( CONNECTION_ASYNC )
|
|
|
|
{
|
2017-10-07 23:10:13 +02:00
|
|
|
m_worker = boost::make_shared< DbWorker >( m_queue, this );
|
2017-09-30 23:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Core::Db::DbConnection::~DbConnection()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::close()
|
|
|
|
{
|
|
|
|
m_worker.reset();
|
|
|
|
m_stmts.clear();
|
|
|
|
|
|
|
|
if( m_pConnection )
|
|
|
|
{
|
|
|
|
m_pConnection->close();
|
|
|
|
m_pConnection.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Core::Db::DbConnection::open()
|
|
|
|
{
|
2017-10-07 23:10:13 +02:00
|
|
|
boost::shared_ptr< Mysql::MySqlBase > base( new Mysql::MySqlBase() );
|
2017-09-30 23:51:01 +02:00
|
|
|
Mysql::optionMap options;
|
|
|
|
options[ MYSQL_OPT_RECONNECT ] = "1";
|
|
|
|
options[ MYSQL_SET_CHARSET_NAME ] = "utf8";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-10-07 23:10:13 +02:00
|
|
|
m_pConnection = base->connect( m_connectionInfo.host, m_connectionInfo.user, m_connectionInfo.password,
|
|
|
|
options, m_connectionInfo.port );
|
|
|
|
|
2017-09-30 23:51:01 +02:00
|
|
|
m_pConnection->setSchema( m_connectionInfo.database );
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
g_log.error( e.what() );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Core::Db::DbConnection::getLastError()
|
|
|
|
{
|
|
|
|
return m_pConnection->getErrorNo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::ping()
|
|
|
|
{
|
|
|
|
m_pConnection->ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::Db::DbConnection::lockIfReady()
|
|
|
|
{
|
|
|
|
return m_mutex.try_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::unlock()
|
|
|
|
{
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::beginTransaction()
|
|
|
|
{
|
|
|
|
m_pConnection->beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::rollbackTransaction()
|
|
|
|
{
|
|
|
|
m_pConnection->rollbackTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::commitTransaction()
|
|
|
|
{
|
|
|
|
m_pConnection->commitTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::Db::DbConnection::execute( const std::string& sql )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-10-07 23:10:13 +02:00
|
|
|
auto stmt = m_pConnection->createStatement();
|
2017-09-30 23:51:01 +02:00
|
|
|
bool result = stmt->execute( sql );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
g_log.error( e.what() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
boost::shared_ptr< Mysql::ResultSet > Core::Db::DbConnection::query( const std::string& sql )
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-10-07 23:10:13 +02:00
|
|
|
auto stmt = m_pConnection->createStatement();
|
|
|
|
auto result = stmt->executeQuery( sql );
|
2017-09-30 23:51:01 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
g_log.error( e.what() );
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
boost::shared_ptr< Mysql::ResultSet > Core::Db::DbConnection::query( boost::shared_ptr< Core::Db::PreparedStatement > stmt )
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
2017-10-07 23:10:13 +02:00
|
|
|
boost::shared_ptr< Mysql::ResultSet > res( nullptr );
|
2017-09-30 23:51:01 +02:00
|
|
|
if( !stmt )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
uint32_t index = stmt->getIndex();
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
auto pStmt = getPreparedStatement( index );
|
2017-09-30 23:51:01 +02:00
|
|
|
|
|
|
|
if( !pStmt )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
stmt->setMysqlPS( pStmt );
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stmt->bindParameters();
|
|
|
|
return pStmt->executeQuery();
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
g_log.error( e.what() );
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
bool Core::Db::DbConnection::execute( boost::shared_ptr< Core::Db::PreparedStatement > stmt )
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
|
|
|
if( !stmt )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32_t index = stmt->getIndex();
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
auto pStmt = getPreparedStatement( index );
|
2017-09-30 23:51:01 +02:00
|
|
|
|
|
|
|
if( !pStmt )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
stmt->setMysqlPS( pStmt );
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stmt->bindParameters();
|
|
|
|
return pStmt->execute();
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
g_log.error( e.what() );
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
boost::shared_ptr< Mysql::PreparedStatement > Core::Db::DbConnection::getPreparedStatement( uint32_t index )
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
|
|
|
assert( index < m_stmts.size() );
|
2017-10-07 23:10:13 +02:00
|
|
|
auto ret = m_stmts[index];
|
2017-09-30 23:51:01 +02:00
|
|
|
if( !ret )
|
|
|
|
nullptr;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Db::DbConnection::prepareStatement( uint32_t index, const std::string &sql, Core::Db::ConnectionFlags flags )
|
|
|
|
{
|
|
|
|
m_queries.insert( PreparedStatementMap::value_type( index, std::make_pair( sql, flags ) ) );
|
|
|
|
|
|
|
|
// Check if specified query should be prepared on this connection
|
|
|
|
// i.e. don't prepare async statements on synchronous connections
|
|
|
|
// to save memory that will not be used.
|
|
|
|
if( !( m_connectionFlags & flags ) )
|
|
|
|
{
|
|
|
|
m_stmts[index].reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
boost::shared_ptr< Mysql::PreparedStatement > pStmt( nullptr );
|
2017-09-30 23:51:01 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pStmt = m_pConnection->prepareStatement( sql );
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
g_log.error( e.what() );
|
|
|
|
m_prepareError = true;
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:10:13 +02:00
|
|
|
m_stmts[index] = boost::shared_ptr< Mysql::PreparedStatement >( pStmt );
|
2017-09-30 23:51:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::Db::DbConnection::prepareStatements()
|
|
|
|
{
|
|
|
|
doPrepareStatements();
|
|
|
|
return !m_prepareError;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|