diff --git a/common/dbcore.cpp b/common/dbcore.cpp index 638fab4ae..0f07b5ab8 100644 --- a/common/dbcore.cpp +++ b/common/dbcore.cpp @@ -35,13 +35,14 @@ DBcore::DBcore() { mysql_init(&mysql); - pHost = 0; - pUser = 0; - pPassword = 0; - pDatabase = 0; - pCompress = false; - pSSL = false; - pStatus = Closed; + pHost = nullptr; + pUser = nullptr; + pPassword = nullptr; + pDatabase = nullptr; + pCompress = false; + pSSL = false; + pStatus = Closed; + connection_type = DATABASE_CONNECTION_DEFAULT; } DBcore::~DBcore() @@ -81,6 +82,8 @@ MySQLRequestResult DBcore::QueryDatabase(const char *query, uint32 querylen, boo Open(); } + + // request query. != 0 indicates some kind of error. if (mysql_real_query(&mysql, query, querylen) != 0) { unsigned int errorNumber = mysql_errno(&mysql); @@ -258,6 +261,21 @@ bool DBcore::Open(uint32 *errnum, char *errbuf) } } +void DBcore::SetMysql(MYSQL *mysql) +{ + DBcore::mysql = *mysql; +} + +int8 DBcore::GetConnectionType() const +{ + return connection_type; +} + +void DBcore::SetConnectionType(int8 connection_type) +{ + DBcore::connection_type = connection_type; +} + diff --git a/common/dbcore.h b/common/dbcore.h index 6fd09218e..cf5c4841f 100644 --- a/common/dbcore.h +++ b/common/dbcore.h @@ -2,8 +2,8 @@ #define DBCORE_H #ifdef _WINDOWS - #include - #include +#include +#include #endif #include "../common/mutex.h" @@ -13,38 +13,59 @@ #include #include +const int8 DATABASE_CONNECTION_DEFAULT = 0; +const int8 DATABASE_CONNECTION_CONTENT = 1; + class DBcore { public: - enum eStatus { Closed, Connected, Error }; + enum eStatus { + Closed, Connected, Error + }; DBcore(); ~DBcore(); - eStatus GetStatus() { return pStatus; } - MySQLRequestResult QueryDatabase(const char* query, uint32 querylen, bool retryOnFailureOnce = true); - MySQLRequestResult QueryDatabase(std::string query, bool retryOnFailureOnce = true); + eStatus GetStatus() { return pStatus; } + MySQLRequestResult QueryDatabase(const char *query, uint32 querylen, bool retryOnFailureOnce = true); + MySQLRequestResult QueryDatabase(std::string query, bool retryOnFailureOnce = true); void TransactionBegin(); void TransactionCommit(); void TransactionRollback(); - uint32 DoEscapeString(char* tobuf, const char* frombuf, uint32 fromlen); - void ping(); - MYSQL* getMySQL(){ return &mysql; } + uint32 DoEscapeString(char *tobuf, const char *frombuf, uint32 fromlen); + void ping(); + MYSQL *getMySQL() { return &mysql; } + void SetMysql(MYSQL *mysql); + + int8 GetConnectionType() const; + void SetConnectionType(int8 connection_type); protected: - bool Open(const char* iHost, const char* iUser, const char* iPassword, const char* iDatabase, uint32 iPort, uint32* errnum = 0, char* errbuf = 0, bool iCompress = false, bool iSSL = false); -private: - bool Open(uint32* errnum = 0, char* errbuf = 0); + bool Open( + const char *iHost, + const char *iUser, + const char *iPassword, + const char *iDatabase, + uint32 iPort, + uint32 *errnum = 0, + char *errbuf = 0, + bool iCompress = false, + bool iSSL = false + ); - MYSQL mysql; - Mutex MDatabase; +private: + bool Open(uint32 *errnum = nullptr, char *errbuf = nullptr); + + int8 connection_type; + MYSQL mysql; + Mutex MDatabase; eStatus pStatus; - char* pHost; - char* pUser; - char* pPassword; - char* pDatabase; - bool pCompress; - uint32 pPort; - bool pSSL; + char *pHost; + char *pUser; + char *pPassword; + char *pDatabase; + bool pCompress; + uint32 pPort; + bool pSSL; }; diff --git a/common/eqemu_config.cpp b/common/eqemu_config.cpp index cae737e98..4f29bb901 100644 --- a/common/eqemu_config.cpp +++ b/common/eqemu_config.cpp @@ -104,6 +104,15 @@ void EQEmuConfig::parse_config() DatabasePort = atoi(_root["server"]["database"].get("port", "3306").asString().c_str()); DatabaseDB = _root["server"]["database"].get("db", "eq").asString(); + /** + * Content Database + */ + ContentDbUsername = _root["server"]["content_database"].get("username", "").asString(); + ContentDbPassword = _root["server"]["content_database"].get("password", "").asString(); + ContentDbHost = _root["server"]["content_database"].get("host", "").asString(); + ContentDbPort = atoi(_root["server"]["content_database"].get("port", 0).asString().c_str()); + ContentDbName = _root["server"]["content_database"].get("db", "").asString(); + /** * QS */ diff --git a/common/eqemu_config.h b/common/eqemu_config.h index a4fb7797f..2f36a6317 100644 --- a/common/eqemu_config.h +++ b/common/eqemu_config.h @@ -74,6 +74,13 @@ class EQEmuConfig std::string DatabaseDB; uint16 DatabasePort; + // From + std::string ContentDbHost; + std::string ContentDbUsername; + std::string ContentDbPassword; + std::string ContentDbName; + uint16 ContentDbPort; + // From // QueryServ std::string QSDatabaseHost; std::string QSDatabaseUsername; diff --git a/zone/main.cpp b/zone/main.cpp index 6d3eee96c..1867ba42b 100644 --- a/zone/main.cpp +++ b/zone/main.cpp @@ -237,6 +237,29 @@ int main(int argc, char** argv) { return 1; } + /** + * Multi-tenancy: Content Database + */ + if (!Config->ContentDbHost.empty()) { + if (!content_db.Connect( + !Config->ContentDbHost.empty() ? Config->ContentDbHost.c_str() : Config->DatabaseHost.c_str(), + !Config->ContentDbUsername.empty() ? Config->ContentDbUsername.c_str() : Config->DatabaseUsername.c_str(), + !Config->ContentDbPassword.empty() ? Config->ContentDbPassword.c_str() : Config->DatabasePassword.c_str(), + !Config->ContentDbName.empty() ? Config->ContentDbName.c_str() : Config->DatabaseDB.c_str(), + Config->ContentDbPort != 0 ? Config->ContentDbPort : Config->DatabasePort + )) { + LogError("Cannot continue without a content database connection"); + return 1; + } + } else { + content_db.SetMysql(database.getMySQL()); + } +// +// auto results = content_db.QueryDatabase("SELECT id FROM items limit 10"); +// for (auto row = results.begin(); row != results.end(); ++row) { +// std::cout << row[0] << std::endl; +// } + /* Register Log System and Settings */ LogSys.SetGMSayHandler(&Zone::GMSayHookCallBackProcess); database.LoadLogSettings(LogSys.log_settings); @@ -543,6 +566,7 @@ int main(int argc, char** argv) { if (InterserverTimer.Check()) { InterserverTimer.Start(); database.ping(); + content_db.ping(); entity_list.UpdateWho(); } }; diff --git a/zone/zonedb.cpp b/zone/zonedb.cpp index cad4e1df8..60094bf84 100755 --- a/zone/zonedb.cpp +++ b/zone/zonedb.cpp @@ -20,6 +20,7 @@ extern Zone* zone; ZoneDatabase database; +ZoneDatabase content_db; ZoneDatabase::ZoneDatabase() : SharedDatabase() diff --git a/zone/zonedb.h b/zone/zonedb.h index b1d88539d..7c93b456c 100644 --- a/zone/zonedb.h +++ b/zone/zonedb.h @@ -577,6 +577,7 @@ protected: }; extern ZoneDatabase database; +extern ZoneDatabase content_db; #endif /*ZONEDB_H_*/