1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-05-23 18:17:46 +00:00

Fixed server_rest and more database code cleanup

This commit is contained in:
Mordred 2017-09-01 00:18:06 +02:00
parent 5cbdee5ac7
commit 82f8a1a5b6
5 changed files with 39 additions and 47 deletions

View file

@ -33,7 +33,7 @@ bool QueryResult::nextRow()
MYSQL_ROW row = mysql_fetch_row( m_result ); MYSQL_ROW row = mysql_fetch_row( m_result );
auto length = mysql_fetch_lengths( m_result ); auto length = mysql_fetch_lengths( m_result );
if( row == NULL ) if( row == nullptr )
{ {
return false; return false;
} }
@ -52,7 +52,7 @@ bool QueryResult::nextRow()
Database::Database() Database::Database()
{ {
m_port = 0; m_port = 0;
_counter = 0; m_counter = 0;
m_pConnections = nullptr; m_pConnections = nullptr;
m_connectionCount = -1; // Not connected. m_connectionCount = -1; // Not connected.
} }
@ -77,20 +77,21 @@ bool Database::initialize( const DatabaseParams& params )
MYSQL * temp2; MYSQL * temp2;
my_bool my_true = true; my_bool my_true = true;
g_log.Log( Core::LoggingSeverity::info, "Database: Connecting to " + params.hostname + ", database " + params.databaseName + "..." ); g_log.info( "Database: Connecting to " + params.hostname + ", database " + params.databaseName + "..." );
m_pConnections = new DatabaseConnection[params.connectionCount]; m_pConnections = new DatabaseConnection[params.connectionCount];
for( i = 0; i < params.connectionCount; ++i ) for( i = 0; i < params.connectionCount; ++i )
{ {
temp = mysql_init( NULL ); temp = mysql_init( nullptr );
if( mysql_options( temp, MYSQL_SET_CHARSET_NAME, "utf8" ) ) if( mysql_options( temp, MYSQL_SET_CHARSET_NAME, "utf8" ) )
{ {
g_log.Log( Core::LoggingSeverity::error, "Database: Could not set utf8 character set." ); g_log.error( "Database: Could not set utf8 character set." );
} }
if( mysql_options( temp, MYSQL_OPT_RECONNECT, &my_true ) ) if( mysql_options( temp, MYSQL_OPT_RECONNECT, &my_true ) )
{ {
g_log.Log( Core::LoggingSeverity::error, "Database: MYSQL_OPT_RECONNECT could not be set, connection drops may occur but will be counteracted." ); g_log.error( "Database: MYSQL_OPT_RECONNECT could not be set, "
"connection drops may occur but will be counteracted." );
} }
temp2 = mysql_real_connect( temp, temp2 = mysql_real_connect( temp,
@ -103,7 +104,7 @@ bool Database::initialize( const DatabaseParams& params )
0 ); 0 );
if( temp2 == NULL ) if( temp2 == NULL )
{ {
g_log.Log( Core::LoggingSeverity::fatal, "Database: Connection failed due to: `%s`" + std::string( mysql_error( temp ) ) ); g_log.fatal( "Database: Connection failed due to: `%s`" + std::string( mysql_error( temp ) ) );
return false; return false;
} }
@ -158,7 +159,7 @@ boost::shared_ptr<QueryResult> Database::query( const std::string& QueryString )
boost::shared_ptr<QueryResult> qResult( nullptr ); boost::shared_ptr<QueryResult> qResult( nullptr );
DatabaseConnection * con = getFreeConnection(); DatabaseConnection * con = getFreeConnection();
if( _SendQuery( con, QueryString.c_str(), false ) ) if( sendQuery(con, QueryString.c_str(), false) )
{ {
qResult = boost::shared_ptr<QueryResult>( _StoreQueryResult( con ) ); qResult = boost::shared_ptr<QueryResult>( _StoreQueryResult( con ) );
} }
@ -169,19 +170,13 @@ boost::shared_ptr<QueryResult> Database::query( const std::string& QueryString )
bool Database::execute( const std::string& QueryString ) bool Database::execute( const std::string& QueryString )
{
return waitExecuteNA( QueryString.c_str() );
}
bool Database::waitExecuteNA( const char* QueryString )
{ {
DatabaseConnection * con = getFreeConnection(); DatabaseConnection * con = getFreeConnection();
bool Result = _SendQuery( con, QueryString, false ); bool Result = sendQuery(con, QueryString, false);
con->lock.unlock(); con->lock.unlock();
return Result; return Result;
} }
void Database::freeQueryResult( QueryResult * p ) void Database::freeQueryResult( QueryResult * p )
{ {
delete p; delete p;
@ -232,29 +227,29 @@ std::string Database::escapeString( const char * esc, DatabaseConnection * con )
return std::string( ret ); return std::string( ret );
} }
bool Database::_SendQuery( DatabaseConnection *con, const char* Sql, bool Self ) bool Database::sendQuery( DatabaseConnection *con, const std::string &sql, bool Self )
{ {
int32_t result = mysql_query( con->conn, Sql ); int32_t result = mysql_query( con->conn, sql.c_str() );
if( result > 0 ) if( result > 0 )
{ {
if( Self == false && _HandleError( con, mysql_errno( con->conn ) ) ) if( Self == false && handleError( con, mysql_errno( con->conn ) ) )
{ {
// Re-send the query, the connection was successful. // Re-send the query, the connection was successful.
// The true on the end will prevent an endless loop here, as it will // The true on the end will prevent an endless loop here, as it will
// stop after sending the query twice. // stop after sending the query twice.
result = _SendQuery( con, Sql, true ); result = sendQuery(con, sql, true);
} }
else else
{ {
g_log.Log( Core::LoggingSeverity::error, "Database: query failed " + std::string( mysql_error( con->conn ) ) ); g_log.error( "Database: query failed " + std::string( mysql_error( con->conn ) ) );
g_log.Log( Core::LoggingSeverity::error, "\t" + std::string( Sql ) ); g_log.error( "\t" + std::string( sql ) );
} }
} }
return ( result == 0 ? true : false ); return ( result == 0 ? true : false );
} }
bool Database::_HandleError( DatabaseConnection * con, uint32_t ErrorNumber ) bool Database::handleError( DatabaseConnection *con, uint32_t ErrorNumber )
{ {
// Handle errors that should cause a reconnect to the CDatabase. // Handle errors that should cause a reconnect to the CDatabase.
switch( ErrorNumber ) { switch( ErrorNumber ) {
@ -311,7 +306,7 @@ bool Database::_Reconnect( DatabaseConnection * conn )
0 ); 0 );
if( temp2 == NULL ) if( temp2 == NULL )
{ {
g_log.Log( Core::LoggingSeverity::error, "Database: Could not reconnect to database because of " + std::string( mysql_error( temp ) ) ); g_log.error( "Database: Could not reconnect to database because of " + std::string( mysql_error( temp ) ) );
mysql_close( temp ); mysql_close( temp );
return false; return false;
} }

View file

@ -30,10 +30,11 @@ namespace Core {
} }
// return as string // return as string
__inline const char *getString() __inline std::string getString()
{ {
if( !m_pValue )
return m_pValue ? m_pValue : ""; return "";
return std::string( m_pValue );
} }
// return as string // return as string
@ -89,10 +90,6 @@ namespace Core {
~QueryResult(); ~QueryResult();
bool nextRow(); bool nextRow();
void Delete()
{
delete this;
}
__inline Field* fetch() __inline Field* fetch()
{ {
@ -177,14 +174,14 @@ namespace Core {
protected: protected:
// actual query function // actual query function
bool _SendQuery( DatabaseConnection *con, const char* Sql, bool Self ); bool sendQuery( DatabaseConnection *con, const std::string &sql, bool Self );
QueryResult * _StoreQueryResult( DatabaseConnection * con ); QueryResult * _StoreQueryResult( DatabaseConnection * con );
bool _HandleError( DatabaseConnection *conn, uint32_t ErrorNumber ); bool handleError( DatabaseConnection *conn, uint32_t ErrorNumber );
bool _Reconnect( DatabaseConnection *conn ); bool _Reconnect( DatabaseConnection *conn );
DatabaseConnection *m_pConnections; DatabaseConnection *m_pConnections;
uint32_t _counter; uint32_t m_counter;
/////////////////////////////// ///////////////////////////////
int32_t m_connectionCount; int32_t m_connectionCount;

View file

@ -56,7 +56,7 @@ namespace Core {
memset( m_name, 0, 32 ); memset( m_name, 0, 32 );
strcpy( m_name, field[0].getString() ); strcpy( m_name, field[0].getString().c_str() );
field[1].getBinary( (char*)m_look, 26 ); field[1].getBinary( (char*)m_look, 26 );
@ -67,11 +67,11 @@ namespace Core {
m_lookMap[i] = m_look[i]; m_lookMap[i] = m_look[i];
} }
setBirthDay( field[2].getInt8(), field[3].getInt8() ); setBirthDay( field[2].get< int8_t >(), field[3].get< int8_t >() );
m_guardianDeity = field[4].getInt8(); m_guardianDeity = field[4].get< int8_t >();
m_class = field[5].getInt8(); m_class = field[5].get< int8_t >();
m_contentId = field[7].getUInt64(); m_contentId = field[7].get< uint64_t >();
m_zoneId = field[8].getUInt16(); m_zoneId = field[8].get< uint16_t >();
auto pQR2 = g_database.query( "SELECT * FROM characlass WHERE CharacterId = " + std::to_string( charId ) + ";" ); auto pQR2 = g_database.query( "SELECT * FROM characlass WHERE CharacterId = " + std::to_string( charId ) + ";" );
@ -80,7 +80,7 @@ namespace Core {
for( uint8_t i = 0; i < 25; i++ ) for( uint8_t i = 0; i < 25; i++ )
{ {
uint8_t index = i * 2; uint8_t index = i * 2;
m_classMap[i] = field2[index].getUInt8(); m_classMap[i] = field2[index].get< uint8_t >();
//m_expArray[i] = //m_expArray[i] =
} }
} }

View file

@ -36,7 +36,7 @@ bool Core::Network::SapphireAPI::login( const std::string& username, const std::
return false; return false;
// user found, proceed // user found, proceed
int32_t accountId = pQR->fetch()[0].getUInt32(); int32_t accountId = pQR->fetch()[0].get< uint32_t >();
// session id string generation // session id string generation
srand( ( uint32_t )time( NULL ) + 42 ); srand( ( uint32_t )time( NULL ) + 42 );
@ -96,7 +96,7 @@ bool Core::Network::SapphireAPI::createAccount( const std::string& username, con
// we are clear and can create a new account // we are clear and can create a new account
// get the next free account id // get the next free account id
pQR = g_database.query( "SELECT MAX(account_id) FROM accounts;" ); pQR = g_database.query( "SELECT MAX(account_id) FROM accounts;" );
int32_t accountId = pQR->fetch()[0].getUInt32() + 1; int32_t accountId = pQR->fetch()[0].get< uint32_t >() + 1;
// store the account to the db // store the account to the db
g_database.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " + g_database.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " +
@ -222,7 +222,7 @@ std::vector<Core::PlayerMinimal> Core::Network::SapphireAPI::getCharList( uint32
Core::Db::Field *field = pQR->fetch(); Core::Db::Field *field = pQR->fetch();
uint32_t charId = field[0].getInt32(); uint32_t charId = field[0].get< uint32_t >();
player.load( charId ); player.load( charId );
@ -256,7 +256,7 @@ uint32_t Core::Network::SapphireAPI::getNextCharId()
return 0x00200001; return 0x00200001;
} }
charId = pQR->fetch()[0].getUInt32() + 1; charId = pQR->fetch()[0].get< uint32_t >() + 1;
if( charId < 0x00200001 ) if( charId < 0x00200001 )
{ {
return 0x00200001; return 0x00200001;
@ -276,7 +276,7 @@ uint64_t Core::Network::SapphireAPI::getNextContentId()
return 0x0040000001000001; return 0x0040000001000001;
} }
contentId = pQR->fetch()[0].getUInt64() + 1; contentId = pQR->fetch()[0].get< uint64_t >() + 1;
if( contentId < 0x0040000001000001 ) if( contentId < 0x0040000001000001 )
{ {
return 0x0040000001000001; return 0x0040000001000001;

View file

@ -97,7 +97,7 @@ bool Core::Entity::Player::load( uint32_t charId, Core::SessionPtr pSession )
Db::Field *field = pQR->fetch(); Db::Field *field = pQR->fetch();
strcpy( m_name, field[0].getString() ); strcpy( m_name, field[0].getString().c_str() );
ZonePtr pCurrZone = g_zoneMgr.getZone( field[1].get< int32_t >() ); ZonePtr pCurrZone = g_zoneMgr.getZone( field[1].get< int32_t >() );
m_zoneId = field[1].get< int32_t >(); m_zoneId = field[1].get< int32_t >();
@ -258,7 +258,7 @@ bool Core::Entity::Player::loadSearchInfo()
m_searchSelectClass = field[1].get< uint8_t >(); m_searchSelectClass = field[1].get< uint8_t >();
m_searchSelectRegion = field[2].get< uint8_t >(); m_searchSelectRegion = field[2].get< uint8_t >();
sprintf( m_searchMessage, field[3].getString() ); sprintf( m_searchMessage, field[3].getString().c_str() );
return true; return true;
} }