mirror of
https://github.com/EQEmu/Server.git
synced 2026-06-02 15:20:26 +00:00
Warning fixes, general cleanup (#5053)
This commit is contained in:
+23
-40
@@ -33,17 +33,9 @@
|
||||
#endif
|
||||
|
||||
DBcore::DBcore()
|
||||
: mysql(mysql_init(nullptr))
|
||||
, m_mutex(std::make_shared<Mutex>())
|
||||
{
|
||||
mysql = mysql_init(nullptr);
|
||||
mysqlOwner = true;
|
||||
pHost = nullptr;
|
||||
pUser = nullptr;
|
||||
pPassword = nullptr;
|
||||
pDatabase = nullptr;
|
||||
pCompress = false;
|
||||
pSSL = false;
|
||||
pStatus = Closed;
|
||||
m_mutex = new Mutex;
|
||||
}
|
||||
|
||||
DBcore::~DBcore()
|
||||
@@ -56,20 +48,17 @@ DBcore::~DBcore()
|
||||
if (mysqlOwner) {
|
||||
mysql_close(mysql);
|
||||
}
|
||||
|
||||
safe_delete_array(pHost);
|
||||
safe_delete_array(pUser);
|
||||
safe_delete_array(pPassword);
|
||||
safe_delete_array(pDatabase);
|
||||
}
|
||||
|
||||
// Sends the MySQL server a keepalive
|
||||
void DBcore::ping()
|
||||
{
|
||||
if (!m_mutex->trylock()) {
|
||||
if (!m_mutex->try_lock())
|
||||
{
|
||||
// well, if's it's locked, someone's using it. If someone's using it, it doesnt need a keepalive
|
||||
return;
|
||||
}
|
||||
|
||||
mysql_ping(mysql);
|
||||
m_mutex->unlock();
|
||||
}
|
||||
@@ -92,7 +81,7 @@ MySQLRequestResult DBcore::QueryDatabase(const char *query, uint32 querylen, boo
|
||||
BenchTimer timer;
|
||||
timer.reset();
|
||||
|
||||
LockMutex lock(m_mutex);
|
||||
std::scoped_lock lock(*m_mutex);
|
||||
|
||||
// Reconnect if we are not connected before hand.
|
||||
if (pStatus != Connected) {
|
||||
@@ -217,15 +206,12 @@ bool DBcore::Open(
|
||||
bool iSSL
|
||||
)
|
||||
{
|
||||
LockMutex lock(m_mutex);
|
||||
safe_delete_array(pHost);
|
||||
safe_delete_array(pUser);
|
||||
safe_delete_array(pPassword);
|
||||
safe_delete_array(pDatabase);
|
||||
pHost = strcpy(new char[strlen(iHost) + 1], iHost);
|
||||
pUser = strcpy(new char[strlen(iUser) + 1], iUser);
|
||||
pPassword = strcpy(new char[strlen(iPassword) + 1], iPassword);
|
||||
pDatabase = strcpy(new char[strlen(iDatabase) + 1], iDatabase);
|
||||
std::scoped_lock lock(*m_mutex);
|
||||
|
||||
m_host = iHost;
|
||||
m_user = iUser;
|
||||
m_password = iPassword;
|
||||
m_database = iDatabase;
|
||||
pCompress = iCompress;
|
||||
pPort = iPort;
|
||||
pSSL = iSSL;
|
||||
@@ -234,10 +220,12 @@ bool DBcore::Open(
|
||||
|
||||
bool DBcore::Open(uint32 *errnum, char *errbuf)
|
||||
{
|
||||
// Expects m_mutex to already be locked.
|
||||
|
||||
if (errbuf) {
|
||||
errbuf[0] = 0;
|
||||
}
|
||||
LockMutex lock(m_mutex);
|
||||
|
||||
if (GetStatus() == Connected) {
|
||||
return true;
|
||||
}
|
||||
@@ -245,7 +233,7 @@ bool DBcore::Open(uint32 *errnum, char *errbuf)
|
||||
mysql_close(mysql);
|
||||
mysql_init(mysql); // Initialize structure again
|
||||
}
|
||||
if (!pHost) {
|
||||
if (m_host.empty()) {
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
@@ -268,11 +256,10 @@ bool DBcore::Open(uint32 *errnum, char *errbuf)
|
||||
mysql_options(mysql, MYSQL_OPT_SSL_ENFORCE, &off);
|
||||
mysql_options(mysql, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &off);
|
||||
}
|
||||
if (mysql_real_connect(mysql, pHost, pUser, pPassword, pDatabase, pPort, 0, flags)) {
|
||||
if (mysql_real_connect(mysql, m_host.c_str(), m_user.c_str(), m_password.c_str(), m_database.c_str(), pPort, nullptr, flags)) {
|
||||
pStatus = Connected;
|
||||
|
||||
std::string connected_origin_host = pHost;
|
||||
SetOriginHost(connected_origin_host);
|
||||
SetOriginHost(m_host);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -293,9 +280,9 @@ const std::string &DBcore::GetOriginHost() const
|
||||
return origin_host;
|
||||
}
|
||||
|
||||
void DBcore::SetOriginHost(const std::string &origin_host)
|
||||
void DBcore::SetOriginHost(const std::string& originHost)
|
||||
{
|
||||
DBcore::origin_host = origin_host;
|
||||
DBcore::origin_host = originHost;
|
||||
}
|
||||
|
||||
std::string DBcore::Escape(const std::string& s)
|
||||
@@ -307,12 +294,8 @@ std::string DBcore::Escape(const std::string& s)
|
||||
return temp.data();
|
||||
}
|
||||
|
||||
void DBcore::SetMutex(Mutex *mutex)
|
||||
void DBcore::SetMutex(const std::shared_ptr<Mutex>& mutex)
|
||||
{
|
||||
if (m_mutex && m_mutex != mutex) {
|
||||
safe_delete(m_mutex);
|
||||
}
|
||||
|
||||
DBcore::m_mutex = mutex;
|
||||
}
|
||||
|
||||
@@ -326,7 +309,7 @@ MySQLRequestResult DBcore::QueryDatabaseMulti(const std::string &query)
|
||||
BenchTimer timer;
|
||||
timer.reset();
|
||||
|
||||
LockMutex lock(m_mutex);
|
||||
std::scoped_lock lock(*m_mutex);
|
||||
|
||||
// Reconnect if we are not connected before hand.
|
||||
if (pStatus != Connected) {
|
||||
@@ -449,5 +432,5 @@ MySQLRequestResult DBcore::QueryDatabaseMulti(const std::string &query)
|
||||
|
||||
mysql::PreparedStmt DBcore::Prepare(std::string query)
|
||||
{
|
||||
return mysql::PreparedStmt(*mysql, std::move(query), m_mutex);
|
||||
return mysql::PreparedStmt(*mysql, std::move(query), *m_mutex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user