From eb56f5fda2ddde78a23042bd6b0bb73eaa6ef59c Mon Sep 17 00:00:00 2001 From: NotAdam Date: Mon, 7 Jan 2019 23:16:01 +1100 Subject: [PATCH] cleanup db connection config --- src/api/main.cpp | 15 +++------------ src/common/Config/ConfigDef.h | 13 +++---------- src/common/Config/ConfigMgr.cpp | 2 +- src/common/Database/DbCommon.h | 18 ++++++++++++++++++ src/common/Database/DbConnection.h | 12 +----------- src/world/ServerMgr.cpp | 11 +---------- 6 files changed, 27 insertions(+), 44 deletions(-) create mode 100644 src/common/Database/DbCommon.h diff --git a/src/api/main.cpp b/src/api/main.cpp index 23580484..372cad9f 100644 --- a/src/api/main.cpp +++ b/src/api/main.cpp @@ -76,7 +76,7 @@ void reloadConfig() if( failedLoad ) { - Logger::fatal( "If this is the first time starting the server, we've copied the default one for your editing pleasure." ); + Logger::fatal( "If this is the first time starting the server, we've copied the default configs for your editing pleasure." ); throw "Error loading config"; } @@ -128,23 +128,14 @@ bool loadSettings( int32_t argc, char* argv[] ) Sapphire::Db::DbLoader loader; - Sapphire::Db::ConnectionInfo info; - info.password = m_config.global.database.password; - info.host = m_config.global.database.host; - info.database = m_config.global.database.database; - info.port = m_config.global.database.port; - info.user = m_config.global.database.username; - info.syncThreads = m_config.global.database.syncThreads; - info.asyncThreads = m_config.global.database.asyncThreads; - - loader.addDb( g_charaDb, info ); + loader.addDb( g_charaDb, m_config.global.database ); if( !loader.initDbs() ) return false; server.config.port = m_config.network.listenPort; server.config.address = m_config.network.listenIP; - Logger::info( "Database: Connected to {0}:{1}", info.host, info.port ); + Logger::info( "Database: Connected to {0}:{1}", m_config.global.database.host, m_config.global.database.port ); return true; } diff --git a/src/common/Config/ConfigDef.h b/src/common/Config/ConfigDef.h index ac4f3952..2fcc8251 100644 --- a/src/common/Config/ConfigDef.h +++ b/src/common/Config/ConfigDef.h @@ -1,20 +1,13 @@ #ifndef SAPPHIRE_CONFIGDEF_H #define SAPPHIRE_CONFIGDEF_H +#include + namespace Sapphire::Common::Config { struct GlobalConfig { - struct Database - { - std::string host; - uint16_t port; - std::string database; - std::string username; - std::string password; - uint8_t syncThreads; - uint8_t asyncThreads; - } database; + Sapphire::Db::ConnectionInfo database; struct Parameters { diff --git a/src/common/Config/ConfigMgr.cpp b/src/common/Config/ConfigMgr.cpp index aa5ddc9a..e42e04bb 100644 --- a/src/common/Config/ConfigMgr.cpp +++ b/src/common/Config/ConfigMgr.cpp @@ -49,7 +49,7 @@ bool Sapphire::ConfigMgr::loadGlobalConfig( Common::Config::GlobalConfig& config config.database.host = getValue< std::string >( "Database", "Host", "127.0.0.1" ); config.database.port = getValue< uint16_t >( "Database", "Port", 3306 ); config.database.database = getValue< std::string >( "Database", "Database", "sapphire" ); - config.database.username = getValue< std::string >( "Database", "Username", "sapphire" ); + config.database.user = getValue< std::string >( "Database", "Username", "sapphire" ); config.database.password = getValue< std::string >( "Database", "Password", "" ); config.database.syncThreads = getValue< uint8_t >( "Database", "SyncThreads", 2 ); config.database.asyncThreads = getValue< uint8_t >( "Database", "AsyncThreads", 2 ); diff --git a/src/common/Database/DbCommon.h b/src/common/Database/DbCommon.h new file mode 100644 index 00000000..89d537b6 --- /dev/null +++ b/src/common/Database/DbCommon.h @@ -0,0 +1,18 @@ +#ifndef SAPPHIRE_DBCOMMON_H +#define SAPPHIRE_DBCOMMON_H + +namespace Sapphire::Db +{ + struct ConnectionInfo + { + std::string user; + std::string password; + std::string database; + std::string host; + uint16_t port; + uint8_t syncThreads; + uint8_t asyncThreads; + }; +} + +#endif //SAPPHIRE_DBCOMMON_H diff --git a/src/common/Database/DbConnection.h b/src/common/Database/DbConnection.h index 05bfd793..a31a531d 100644 --- a/src/common/Database/DbConnection.h +++ b/src/common/Database/DbConnection.h @@ -7,6 +7,7 @@ #include #include #include "Util/LockedWaitQueue.h" +#include "DbCommon.h" namespace Mysql { @@ -31,17 +32,6 @@ namespace Sapphire::Db CONNECTION_BOTH = CONNECTION_ASYNC | CONNECTION_SYNC }; - struct ConnectionInfo - { - std::string user; - std::string password; - std::string database; - std::string host; - uint16_t port; - uint8_t syncThreads; - uint8_t asyncThreads; - }; - using PreparedStatementMap = std::map< uint32_t, std::pair< std::string, ConnectionFlags > >; class DbConnection : diff --git a/src/world/ServerMgr.cpp b/src/world/ServerMgr.cpp index a5e74e29..5014dc08 100644 --- a/src/world/ServerMgr.cpp +++ b/src/world/ServerMgr.cpp @@ -136,18 +136,9 @@ void Sapphire::World::ServerMgr::run( int32_t argc, char* argv[] ) } framework()->set< Data::ExdDataGenerated >( pExdData ); - Sapphire::Db::ConnectionInfo info; - info.password = m_config.global.database.password; - info.host = m_config.global.database.host; - info.database = m_config.global.database.database; - info.port = m_config.global.database.port; - info.user = m_config.global.database.username; - info.syncThreads = m_config.global.database.syncThreads; - info.asyncThreads = m_config.global.database.asyncThreads; - auto pDb = std::make_shared< Db::DbWorkerPool< Db::ZoneDbConnection > >(); Sapphire::Db::DbLoader loader; - loader.addDb( *pDb, info ); + loader.addDb( *pDb, m_config.global.database ); if( !loader.initDbs() ) { Logger::fatal( "Database not initialized properly!" );