feat(Core/Database): port TrinityCore database API (#5611)

This commit is contained in:
Kargatum 2021-06-22 11:21:07 +07:00 committed by GitHub
parent 2a2e54d8c5
commit 9ac6fddcae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
155 changed files with 5818 additions and 4321 deletions

View file

@ -5,12 +5,18 @@
#include "DatabaseLoader.h"
#include "Config.h"
// #include "DBUpdater.h" not implement
#include "DatabaseEnv.h"
#include "Duration.h"
#include "Log.h"
#include "Duration.h"
#include <errmsg.h>
#include <mysqld_error.h>
#include <thread>
DatabaseLoader::DatabaseLoader(std::string const& logger)
: _logger(logger) { }
template <class T>
DatabaseLoader& DatabaseLoader::AddDatabase(DatabaseWorkerPool<T>& pool, std::string const& name)
{
@ -19,14 +25,15 @@ DatabaseLoader& DatabaseLoader::AddDatabase(DatabaseWorkerPool<T>& pool, std::st
std::string const dbString = sConfigMgr->GetOption<std::string>(name + "DatabaseInfo", "");
if (dbString.empty())
{
LOG_INFO("sql.driver", "Database %s not specified in configuration file!", name.c_str());
LOG_ERROR(_logger, "Database %s not specified in configuration file!", name.c_str());
return false;
}
uint8 const asyncThreads = sConfigMgr->GetOption<uint8>(name + "Database.WorkerThreads", 1);
if (asyncThreads < 1 || asyncThreads > 32)
{
LOG_INFO("sql.driver", "%s database: invalid number of worker threads specified. Please pick a value between 1 and 32.", name.c_str());
LOG_ERROR(_logger, "%s database: invalid number of worker threads specified. "
"Please pick a value between 1 and 32.", name.c_str());
return false;
}
@ -39,36 +46,36 @@ DatabaseLoader& DatabaseLoader::AddDatabase(DatabaseWorkerPool<T>& pool, std::st
// Try reconnect
if (error == CR_CONNECTION_ERROR)
{
uint8 const ATTEMPTS = sConfigMgr->GetOption<uint8>("Database.Reconnect.Attempts", 20);
Seconds RECONNECT_SECONDS = Seconds(sConfigMgr->GetOption<uint8>("Database.Reconnect.Seconds", 15));
uint8 count = 0;
uint8 const attempts = sConfigMgr->GetOption<uint8>("Database.Reconnect.Attempts", 20);
Seconds reconnectSeconds = Seconds(sConfigMgr->GetOption<uint8>("Database.Reconnect.Seconds", 15));
uint8 reconnectCount = 0;
while (count < ATTEMPTS)
while (reconnectCount < attempts)
{
LOG_INFO("sql.driver", "> Retrying after %u seconds", static_cast<uint32>(RECONNECT_SECONDS.count()));
std::this_thread::sleep_for(RECONNECT_SECONDS);
LOG_INFO(_logger, "> Retrying after %u seconds", static_cast<uint32>(reconnectSeconds.count()));
std::this_thread::sleep_for(reconnectSeconds);
error = pool.Open();
if (error == CR_CONNECTION_ERROR)
{
count++;
reconnectCount++;
}
else
{
break;
}
}
}
// If the error wasn't handled quit
if (error)
{
LOG_ERROR("sql.driver", "DatabasePool %s NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile for specific errors", name.c_str());
LOG_ERROR(_logger, "DatabasePool %s NOT opened. There were errors opening the MySQL connections. "
"Check your log file for specific errors", name.c_str());
return false;
}
}
// Add the close operation
_close.push([&pool]
{
@ -78,11 +85,11 @@ DatabaseLoader& DatabaseLoader::AddDatabase(DatabaseWorkerPool<T>& pool, std::st
return true;
});
_prepare.push([name, &pool]() -> bool
_prepare.push([this, name, &pool]() -> bool
{
if (!pool.PrepareStatements())
{
LOG_ERROR("sql.driver", "Could not prepare statements of the %s database, see log for details.", name.c_str());
LOG_ERROR(_logger, "Could not prepare statements of the %s database, see log for details.", name.c_str());
return false;
}
@ -129,6 +136,9 @@ bool DatabaseLoader::Process(std::queue<Predicate>& queue)
return true;
}
template DatabaseLoader& DatabaseLoader::AddDatabase<LoginDatabaseConnection>(DatabaseWorkerPool<LoginDatabaseConnection>&, std::string const&);
template DatabaseLoader& DatabaseLoader::AddDatabase<CharacterDatabaseConnection>(DatabaseWorkerPool<CharacterDatabaseConnection>&, std::string const&);
template DatabaseLoader& DatabaseLoader::AddDatabase<WorldDatabaseConnection>(DatabaseWorkerPool<WorldDatabaseConnection>&, std::string const&);
template AC_DATABASE_API
DatabaseLoader& DatabaseLoader::AddDatabase<LoginDatabaseConnection>(DatabaseWorkerPool<LoginDatabaseConnection>&, std::string const&);
template AC_DATABASE_API
DatabaseLoader& DatabaseLoader::AddDatabase<CharacterDatabaseConnection>(DatabaseWorkerPool<CharacterDatabaseConnection>&, std::string const&);
template AC_DATABASE_API
DatabaseLoader& DatabaseLoader::AddDatabase<WorldDatabaseConnection>(DatabaseWorkerPool<WorldDatabaseConnection>&, std::string const&);