From fb2c7d02dd56eeb067c081378712501f8e23681b Mon Sep 17 00:00:00 2001 From: KimLS Date: Mon, 6 Feb 2023 23:30:07 -0800 Subject: [PATCH] Change to share underlying pointers. --- client_files/export/main.cpp | 11 +------- client_files/import/main.cpp | 11 +------- common/dbcore.cpp | 52 +++++++++++++++++------------------- common/dbcore.h | 5 +++- shared_memory/main.cpp | 11 +------- world/world_boot.cpp | 12 +-------- zone/main.cpp | 12 +-------- 7 files changed, 33 insertions(+), 81 deletions(-) diff --git a/client_files/export/main.cpp b/client_files/export/main.cpp index 1aab8105a1..67d19326f4 100644 --- a/client_files/export/main.cpp +++ b/client_files/export/main.cpp @@ -86,16 +86,7 @@ int main(int argc, char **argv) return 1; } } else { - if (!content_db.Connect( - Config->DatabaseHost.c_str(), - Config->DatabaseUsername.c_str(), - Config->DatabasePassword.c_str(), - Config->DatabaseDB.c_str(), - Config->DatabasePort - )) { - LogError("Cannot continue without a content database connection"); - return 1; - } + content_db.SetMySQL(database); } LogSys.SetDatabase(&database) diff --git a/client_files/import/main.cpp b/client_files/import/main.cpp index c4d71389d2..6e23821ab5 100644 --- a/client_files/import/main.cpp +++ b/client_files/import/main.cpp @@ -83,16 +83,7 @@ int main(int argc, char **argv) { return 1; } } else { - if (!content_db.Connect( - Config->DatabaseHost.c_str(), - Config->DatabaseUsername.c_str(), - Config->DatabasePassword.c_str(), - Config->DatabaseDB.c_str(), - Config->DatabasePort - )) { - LogError("Cannot continue without a content database connection"); - return 1; - } + content_db.SetMySQL(database); } LogSys.SetDatabase(&database) diff --git a/common/dbcore.cpp b/common/dbcore.cpp index f07d9116b3..a9ea6e357d 100644 --- a/common/dbcore.cpp +++ b/common/dbcore.cpp @@ -34,7 +34,8 @@ DBcore::DBcore() { - mysql_init(&mysql); + mysql = mysql_init(nullptr); + mysqlOwner = true; pHost = nullptr; pUser = nullptr; pPassword = nullptr; @@ -42,6 +43,7 @@ DBcore::DBcore() pCompress = false; pSSL = false; pStatus = Closed; + } DBcore::~DBcore() @@ -51,16 +53,10 @@ DBcore::~DBcore() * are re-using the default database connection pointer when we dont have an * external configuration setup ex: (content_database) */ - std::string mysql_connection_host; - if (mysql.host) { - mysql_connection_host = mysql.host; - } - - if (GetOriginHost() != mysql_connection_host) { - return; + if (mysqlOwner) { + mysql_close(mysql); } - mysql_close(&mysql); safe_delete_array(pHost); safe_delete_array(pUser); safe_delete_array(pPassword); @@ -74,7 +70,7 @@ void DBcore::ping() // well, if's it's locked, someone's using it. If someone's using it, it doesnt need a keepalive return; } - mysql_ping(&mysql); + mysql_ping(mysql); MDatabase.unlock(); } @@ -105,8 +101,8 @@ MySQLRequestResult DBcore::QueryDatabase(const char *query, uint32 querylen, boo // request query. != 0 indicates some kind of error. - if (mysql_real_query(&mysql, query, querylen) != 0) { - unsigned int errorNumber = mysql_errno(&mysql); + if (mysql_real_query(mysql, query, querylen) != 0) { + unsigned int errorNumber = mysql_errno(mysql); if (errorNumber == CR_SERVER_GONE_ERROR) { pStatus = Error; @@ -130,26 +126,26 @@ MySQLRequestResult DBcore::QueryDatabase(const char *query, uint32 querylen, boo auto errorBuffer = new char[MYSQL_ERRMSG_SIZE]; - snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql)); + snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(mysql), mysql_error(mysql)); - return MySQLRequestResult(nullptr, 0, 0, 0, 0, (uint32) mysql_errno(&mysql), errorBuffer); + return MySQLRequestResult(nullptr, 0, 0, 0, 0, (uint32) mysql_errno(mysql), errorBuffer); } auto errorBuffer = new char[MYSQL_ERRMSG_SIZE]; - snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql)); + snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(mysql), mysql_error(mysql)); /** * Error logging */ - if (mysql_errno(&mysql) > 0 && strlen(query) > 0) { - LogMySQLError("[{}] [{}]\n[{}]", mysql_errno(&mysql), mysql_error(&mysql), query); + if (mysql_errno(mysql) > 0 && strlen(query) > 0) { + LogMySQLError("[{}] [{}]\n[{}]", mysql_errno(mysql), mysql_error(mysql), query); } - return MySQLRequestResult(nullptr, 0, 0, 0, 0, mysql_errno(&mysql), errorBuffer); + return MySQLRequestResult(nullptr, 0, 0, 0, 0, mysql_errno(mysql), errorBuffer); } // successful query. get results. - MYSQL_RES *res = mysql_store_result(&mysql); + MYSQL_RES *res = mysql_store_result(mysql); uint32 rowCount = 0; if (res != nullptr) { @@ -158,10 +154,10 @@ MySQLRequestResult DBcore::QueryDatabase(const char *query, uint32 querylen, boo MySQLRequestResult requestResult( res, - (uint32) mysql_affected_rows(&mysql), + (uint32) mysql_affected_rows(mysql), rowCount, - (uint32) mysql_field_count(&mysql), - (uint32) mysql_insert_id(&mysql) + (uint32) mysql_field_count(mysql), + (uint32) mysql_insert_id(mysql) ); if (LogSys.log_settings[Logs::MySQLQuery].is_category_enabled == 1) { @@ -207,7 +203,7 @@ uint32 DBcore::DoEscapeString(char *tobuf, const char *frombuf, uint32 fromlen) { // No good reason to lock the DB, we only need it in the first place to check char encoding. // LockMutex lock(&MDatabase); - return mysql_real_escape_string(&mysql, tobuf, frombuf, fromlen); + return mysql_real_escape_string(mysql, tobuf, frombuf, fromlen); } bool DBcore::Open( @@ -247,8 +243,8 @@ bool DBcore::Open(uint32 *errnum, char *errbuf) return true; } if (GetStatus() == Error) { - mysql_close(&mysql); - mysql_init(&mysql); // Initialize structure again + mysql_close(mysql); + mysql_init(mysql); // Initialize structure again } if (!pHost) { return false; @@ -265,7 +261,7 @@ bool DBcore::Open(uint32 *errnum, char *errbuf) if (pSSL) { flags |= CLIENT_SSL; } - if (mysql_real_connect(&mysql, pHost, pUser, pPassword, pDatabase, pPort, 0, flags)) { + if (mysql_real_connect(mysql, pHost, pUser, pPassword, pDatabase, pPort, 0, flags)) { pStatus = Connected; std::string connected_origin_host = pHost; @@ -275,10 +271,10 @@ bool DBcore::Open(uint32 *errnum, char *errbuf) } else { if (errnum) { - *errnum = mysql_errno(&mysql); + *errnum = mysql_errno(mysql); } if (errbuf) { - snprintf(errbuf, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql)); + snprintf(errbuf, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(mysql), mysql_error(mysql)); } pStatus = Error; return false; diff --git a/common/dbcore.h b/common/dbcore.h index f8c589b64a..11bdc2846e 100644 --- a/common/dbcore.h +++ b/common/dbcore.h @@ -35,6 +35,8 @@ class DBcore { bool DoesTableExist(std::string table_name); + void SetMySQL(const DBcore& o) { mysql = o.mysql; mysqlOwner = false; } + protected: bool Open( const char *iHost, @@ -51,7 +53,8 @@ class DBcore { private: bool Open(uint32 *errnum = nullptr, char *errbuf = nullptr); - MYSQL mysql; + MYSQL* mysql; + bool mysqlOwner; Mutex MDatabase; eStatus pStatus; diff --git a/shared_memory/main.cpp b/shared_memory/main.cpp index 1ec904c403..b5473d43fc 100644 --- a/shared_memory/main.cpp +++ b/shared_memory/main.cpp @@ -124,16 +124,7 @@ int main(int argc, char **argv) return 1; } } else { - if (!content_db.Connect( - Config->DatabaseHost.c_str(), - Config->DatabaseUsername.c_str(), - Config->DatabasePassword.c_str(), - Config->DatabaseDB.c_str(), - Config->DatabasePort - )) { - LogError("Cannot continue without a content database connection"); - return 1; - } + content_db.SetMySQL(database); } LogSys.SetDatabase(&database) diff --git a/world/world_boot.cpp b/world/world_boot.cpp index a532b5ca88..e2fb0e4d58 100644 --- a/world/world_boot.cpp +++ b/world/world_boot.cpp @@ -153,17 +153,7 @@ bool WorldBoot::LoadDatabaseConnections() } } else { - if (!content_db.Connect( - c->DatabaseHost.c_str(), - c->DatabaseUsername.c_str(), - c->DatabasePassword.c_str(), - c->DatabaseDB.c_str(), - c->DatabasePort, - "content" - )) { - LogError("Cannot continue without a content database connection"); - return false; - } + content_db.SetMySQL(database); } return true; diff --git a/zone/main.cpp b/zone/main.cpp index cf57b09f7f..35b225fca1 100644 --- a/zone/main.cpp +++ b/zone/main.cpp @@ -256,17 +256,7 @@ int main(int argc, char** argv) { return 1; } } else { - if (!content_db.Connect( - Config->DatabaseHost.c_str(), - Config->DatabaseUsername.c_str(), - Config->DatabasePassword.c_str(), - Config->DatabaseDB.c_str(), - Config->DatabasePort, - "content" - )) { - LogError("Cannot continue without a content database connection"); - return 1; - } + content_db.SetMySQL(database); } /* Register Log System and Settings */