Skip to content

Commit

Permalink
Change to share underlying pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
KimLS authored and KimLS committed Feb 7, 2023
1 parent 0b02b22 commit fb2c7d0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 81 deletions.
11 changes: 1 addition & 10 deletions client_files/export/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions client_files/import/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 24 additions & 28 deletions common/dbcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@

DBcore::DBcore()
{
mysql_init(&mysql);
mysql = mysql_init(nullptr);
mysqlOwner = true;
pHost = nullptr;
pUser = nullptr;
pPassword = nullptr;
pDatabase = nullptr;
pCompress = false;
pSSL = false;
pStatus = Closed;

}

DBcore::~DBcore()
Expand All @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion common/dbcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down
11 changes: 1 addition & 10 deletions shared_memory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 1 addition & 11 deletions world/world_boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 1 addition & 11 deletions zone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit fb2c7d0

Please sign in to comment.