From 82f8a1a5b6ad3f83f89c21a7d64225e9ea1d9704 Mon Sep 17 00:00:00 2001 From: Mordred Date: Fri, 1 Sep 2017 00:18:06 +0200 Subject: [PATCH] Fixed server_rest and more database code cleanup --- .../Server_Common/Database/Database.cpp | 41 ++++++++----------- src/servers/Server_Common/Database/Database.h | 17 ++++---- src/servers/Server_REST/PlayerMinimal.cpp | 14 +++---- src/servers/Server_REST/SapphireAPI.cpp | 10 ++--- src/servers/Server_Zone/Actor/PlayerSql.cpp | 4 +- 5 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/servers/Server_Common/Database/Database.cpp b/src/servers/Server_Common/Database/Database.cpp index d90cf759..e9e45cb0 100644 --- a/src/servers/Server_Common/Database/Database.cpp +++ b/src/servers/Server_Common/Database/Database.cpp @@ -33,7 +33,7 @@ bool QueryResult::nextRow() MYSQL_ROW row = mysql_fetch_row( m_result ); auto length = mysql_fetch_lengths( m_result ); - if( row == NULL ) + if( row == nullptr ) { return false; } @@ -52,7 +52,7 @@ bool QueryResult::nextRow() Database::Database() { m_port = 0; - _counter = 0; + m_counter = 0; m_pConnections = nullptr; m_connectionCount = -1; // Not connected. } @@ -77,20 +77,21 @@ bool Database::initialize( const DatabaseParams& params ) MYSQL * temp2; 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]; for( i = 0; i < params.connectionCount; ++i ) { - temp = mysql_init( NULL ); + temp = mysql_init( nullptr ); 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 ) ) { - 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, @@ -103,7 +104,7 @@ bool Database::initialize( const DatabaseParams& params ) 0 ); 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; } @@ -158,7 +159,7 @@ boost::shared_ptr Database::query( const std::string& QueryString ) boost::shared_ptr qResult( nullptr ); DatabaseConnection * con = getFreeConnection(); - if( _SendQuery( con, QueryString.c_str(), false ) ) + if( sendQuery(con, QueryString.c_str(), false) ) { qResult = boost::shared_ptr( _StoreQueryResult( con ) ); } @@ -169,19 +170,13 @@ boost::shared_ptr Database::query( 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(); - bool Result = _SendQuery( con, QueryString, false ); + bool Result = sendQuery(con, QueryString, false); con->lock.unlock(); return Result; } - void Database::freeQueryResult( QueryResult * p ) { delete p; @@ -232,29 +227,29 @@ std::string Database::escapeString( const char * esc, DatabaseConnection * con ) 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( 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. // The true on the end will prevent an endless loop here, as it will // stop after sending the query twice. - result = _SendQuery( con, Sql, true ); + result = sendQuery(con, sql, true); } else { - g_log.Log( Core::LoggingSeverity::error, "Database: query failed " + std::string( mysql_error( con->conn ) ) ); - g_log.Log( Core::LoggingSeverity::error, "\t" + std::string( Sql ) ); + g_log.error( "Database: query failed " + std::string( mysql_error( con->conn ) ) ); + g_log.error( "\t" + std::string( sql ) ); } } 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. switch( ErrorNumber ) { @@ -311,7 +306,7 @@ bool Database::_Reconnect( DatabaseConnection * conn ) 0 ); 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 ); return false; } diff --git a/src/servers/Server_Common/Database/Database.h b/src/servers/Server_Common/Database/Database.h index 077f4df7..c46a647c 100644 --- a/src/servers/Server_Common/Database/Database.h +++ b/src/servers/Server_Common/Database/Database.h @@ -30,10 +30,11 @@ namespace Core { } // return as string - __inline const char *getString() + __inline std::string getString() { - - return m_pValue ? m_pValue : ""; + if( !m_pValue ) + return ""; + return std::string( m_pValue ); } // return as string @@ -89,10 +90,6 @@ namespace Core { ~QueryResult(); bool nextRow(); - void Delete() - { - delete this; - } __inline Field* fetch() { @@ -177,14 +174,14 @@ namespace Core { protected: // 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 ); - bool _HandleError( DatabaseConnection *conn, uint32_t ErrorNumber ); + bool handleError( DatabaseConnection *conn, uint32_t ErrorNumber ); bool _Reconnect( DatabaseConnection *conn ); DatabaseConnection *m_pConnections; - uint32_t _counter; + uint32_t m_counter; /////////////////////////////// int32_t m_connectionCount; diff --git a/src/servers/Server_REST/PlayerMinimal.cpp b/src/servers/Server_REST/PlayerMinimal.cpp index 591b05ca..017a02e8 100644 --- a/src/servers/Server_REST/PlayerMinimal.cpp +++ b/src/servers/Server_REST/PlayerMinimal.cpp @@ -56,7 +56,7 @@ namespace Core { 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 ); @@ -67,11 +67,11 @@ namespace Core { m_lookMap[i] = m_look[i]; } - setBirthDay( field[2].getInt8(), field[3].getInt8() ); - m_guardianDeity = field[4].getInt8(); - m_class = field[5].getInt8(); - m_contentId = field[7].getUInt64(); - m_zoneId = field[8].getUInt16(); + setBirthDay( field[2].get< int8_t >(), field[3].get< int8_t >() ); + m_guardianDeity = field[4].get< int8_t >(); + m_class = field[5].get< int8_t >(); + m_contentId = field[7].get< uint64_t >(); + m_zoneId = field[8].get< uint16_t >(); 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++ ) { uint8_t index = i * 2; - m_classMap[i] = field2[index].getUInt8(); + m_classMap[i] = field2[index].get< uint8_t >(); //m_expArray[i] = } } diff --git a/src/servers/Server_REST/SapphireAPI.cpp b/src/servers/Server_REST/SapphireAPI.cpp index 2b3e572e..73cdfcf8 100644 --- a/src/servers/Server_REST/SapphireAPI.cpp +++ b/src/servers/Server_REST/SapphireAPI.cpp @@ -36,7 +36,7 @@ bool Core::Network::SapphireAPI::login( const std::string& username, const std:: return false; // user found, proceed - int32_t accountId = pQR->fetch()[0].getUInt32(); + int32_t accountId = pQR->fetch()[0].get< uint32_t >(); // session id string generation 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 // get the next free account id 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 g_database.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " + @@ -222,7 +222,7 @@ std::vector Core::Network::SapphireAPI::getCharList( uint32 Core::Db::Field *field = pQR->fetch(); - uint32_t charId = field[0].getInt32(); + uint32_t charId = field[0].get< uint32_t >(); player.load( charId ); @@ -256,7 +256,7 @@ uint32_t Core::Network::SapphireAPI::getNextCharId() return 0x00200001; } - charId = pQR->fetch()[0].getUInt32() + 1; + charId = pQR->fetch()[0].get< uint32_t >() + 1; if( charId < 0x00200001 ) { return 0x00200001; @@ -276,7 +276,7 @@ uint64_t Core::Network::SapphireAPI::getNextContentId() return 0x0040000001000001; } - contentId = pQR->fetch()[0].getUInt64() + 1; + contentId = pQR->fetch()[0].get< uint64_t >() + 1; if( contentId < 0x0040000001000001 ) { return 0x0040000001000001; diff --git a/src/servers/Server_Zone/Actor/PlayerSql.cpp b/src/servers/Server_Zone/Actor/PlayerSql.cpp index b6cfbc04..394f8036 100644 --- a/src/servers/Server_Zone/Actor/PlayerSql.cpp +++ b/src/servers/Server_Zone/Actor/PlayerSql.cpp @@ -97,7 +97,7 @@ bool Core::Entity::Player::load( uint32_t charId, Core::SessionPtr pSession ) 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 >() ); m_zoneId = field[1].get< int32_t >(); @@ -258,7 +258,7 @@ bool Core::Entity::Player::loadSearchInfo() m_searchSelectClass = field[1].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; }