feat(Core/Database): implement db loader (#4431)

This commit is contained in:
Kargatum 2021-04-12 15:09:13 +07:00 committed by GitHub
parent 81301c67d9
commit 53ce87d0f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 426 additions and 226 deletions

View file

@ -47,12 +47,14 @@ MySQLConnection::MySQLConnection(ACE_Activation_Queue* queue, MySQLConnectionInf
MySQLConnection::~MySQLConnection()
{
ASSERT (m_Mysql); /// MySQL context must be present at this point
for (auto stmt : m_stmts)
delete stmt;
for (size_t i = 0; i < m_stmts.size(); ++i)
delete m_stmts[i];
mysql_close(m_Mysql);
if (m_Mysql)
{
mysql_close(m_Mysql);
m_Mysql = nullptr;
}
}
void MySQLConnection::Close()
@ -61,10 +63,9 @@ void MySQLConnection::Close()
delete this;
}
bool MySQLConnection::Open()
uint32 MySQLConnection::Open()
{
MYSQL* mysqlInit;
mysqlInit = mysql_init(nullptr);
MYSQL* mysqlInit = mysql_init(nullptr);
if (!mysqlInit)
{
sLog->outError("Could not initialize Mysql connection to database `%s`", m_connectionInfo.database.c_str());
@ -106,59 +107,44 @@ bool MySQLConnection::Open()
}
#endif
// Possible improvement for future: make ATTEMPTS and SECONDS configurable values
uint32 const ATTEMPTS = 180;
m_Mysql = mysql_real_connect(
mysqlInit,
m_connectionInfo.host.c_str(),
m_connectionInfo.user.c_str(),
m_connectionInfo.password.c_str(),
m_connectionInfo.database.c_str(),
port,
unix_socket,
0);
uint32 count = 0;
do
if (m_Mysql)
{
m_Mysql = mysql_real_connect(
mysqlInit,
m_connectionInfo.host.c_str(),
m_connectionInfo.user.c_str(),
m_connectionInfo.password.c_str(),
m_connectionInfo.database.c_str(),
port,
unix_socket,
0);
if (m_Mysql)
if (!m_reconnecting)
{
if (!m_reconnecting)
sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info());
sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql));
if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
{
sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info());
sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql));
// MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1
// if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
// sLog->outInfo(LOG_FILTER_SQL, "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
sLog->outSQLDriver("[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
}
}
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
#endif
mysql_autocommit(m_Mysql, 1);
mysql_autocommit(m_Mysql, 1);
// set connection properties to UTF8 to properly handle locales for different
// server configs - core sends data in UTF8, so MySQL must expect UTF8 too
mysql_set_character_set(m_Mysql, "utf8");
return PrepareStatements();
}
else
{
count++;
sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
sLog->outError("Retrying in 10 seconds...\n\n");
std::this_thread::sleep_for(10s);
}
} while (!m_Mysql && count < ATTEMPTS);
// set connection properties to UTF8 to properly handle locales for different
// server configs - core sends data in UTF8, so MySQL must expect UTF8 too
mysql_set_character_set(m_Mysql, "utf8");
return 0;
}
sLog->outError(
"Could not connect to MySQL database at %s: %s after %d attempts\n",
m_connectionInfo.host.c_str(),
mysql_error(mysqlInit),
ATTEMPTS);
sLog->outError("Could not connect to MySQL database at %s: %s", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
uint32 errorCode = mysql_errno(mysqlInit);
mysql_close(mysqlInit);
return false;
return errorCode;
}
bool MySQLConnection::PrepareStatements()