mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 15:40:45 +01:00
* There is still the possibility to static link against database.
(cherry picked from commit 6cfea2d8f7)
186 lines
5.6 KiB
C++
186 lines
5.6 KiB
C++
/*
|
|
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
* option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "DatabaseLoader.h"
|
|
#include "DBUpdater.h"
|
|
#include "Config.h"
|
|
|
|
#include <mysqld_error.h>
|
|
|
|
DatabaseLoader::DatabaseLoader(std::string const& logger, uint32 const defaultUpdateMask)
|
|
: _logger(logger), _autoSetup(sConfigMgr->GetBoolDefault("Updates.AutoSetup", true)),
|
|
_updateFlags(sConfigMgr->GetIntDefault("Updates.EnableDatabases", defaultUpdateMask))
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
DatabaseLoader& DatabaseLoader::AddDatabase(DatabaseWorkerPool<T>& pool, std::string const& name)
|
|
{
|
|
bool const updatesEnabledForThis = DBUpdater<T>::IsEnabled(_updateFlags);
|
|
|
|
_open.push([this, name, updatesEnabledForThis, &pool]() -> bool
|
|
{
|
|
std::string const dbString = sConfigMgr->GetStringDefault(name + "DatabaseInfo", "");
|
|
if (dbString.empty())
|
|
{
|
|
TC_LOG_ERROR(_logger, "Database %s not specified in configuration file!", name.c_str());
|
|
return false;
|
|
}
|
|
|
|
uint8 const asyncThreads = uint8(sConfigMgr->GetIntDefault(name + "Database.WorkerThreads", 1));
|
|
if (asyncThreads < 1 || asyncThreads > 32)
|
|
{
|
|
TC_LOG_ERROR(_logger, "%s database: invalid number of worker threads specified. "
|
|
"Please pick a value between 1 and 32.", name.c_str());
|
|
return false;
|
|
}
|
|
|
|
uint8 const synchThreads = uint8(sConfigMgr->GetIntDefault(name + "Database.SynchThreads", 1));
|
|
|
|
pool.SetConnectionInfo(dbString, asyncThreads, synchThreads);
|
|
if (uint32 error = pool.Open())
|
|
{
|
|
// Database does not exist
|
|
if ((error == ER_BAD_DB_ERROR) && updatesEnabledForThis && _autoSetup)
|
|
{
|
|
// Try to create the database and connect again if auto setup is enabled
|
|
if (DBUpdater<T>::Create(pool) && (!pool.Open()))
|
|
error = 0;
|
|
}
|
|
|
|
// If the error wasn't handled quit
|
|
if (error)
|
|
{
|
|
TC_LOG_ERROR("sql.driver", "\nDatabasePool %s NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile "
|
|
"for specific errors. Read wiki at http://www.trinitycore.info/display/tc/TrinityCore+Home", name.c_str());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
// Add the close operation
|
|
_close.push([&pool]
|
|
{
|
|
pool.Close();
|
|
});
|
|
return true;
|
|
});
|
|
|
|
// Populate and update only if updates are enabled for this pool
|
|
if (updatesEnabledForThis)
|
|
{
|
|
_populate.push([this, name, &pool]() -> bool
|
|
{
|
|
if (!DBUpdater<T>::Populate(pool))
|
|
{
|
|
TC_LOG_ERROR(_logger, "Could not populate the %s database, see log for details.", name.c_str());
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
_update.push([this, name, &pool]() -> bool
|
|
{
|
|
if (!DBUpdater<T>::Update(pool))
|
|
{
|
|
TC_LOG_ERROR(_logger, "Could not update the %s database, see log for details.", name.c_str());
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
_prepare.push([this, name, &pool]() -> bool
|
|
{
|
|
if (!pool.PrepareStatements())
|
|
{
|
|
TC_LOG_ERROR(_logger, "Could not prepare statements of the %s database, see log for details.", name.c_str());
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
return *this;
|
|
}
|
|
|
|
bool DatabaseLoader::Load()
|
|
{
|
|
if (!_updateFlags)
|
|
TC_LOG_INFO("sql.updates", "Automatic database updates are disabled for all databases!");
|
|
|
|
if (!OpenDatabases())
|
|
return false;
|
|
|
|
if (!PopulateDatabases())
|
|
return false;
|
|
|
|
if (!UpdateDatabases())
|
|
return false;
|
|
|
|
if (!PrepareStatements())
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool DatabaseLoader::OpenDatabases()
|
|
{
|
|
return Process(_open);
|
|
}
|
|
|
|
bool DatabaseLoader::PopulateDatabases()
|
|
{
|
|
return Process(_populate);
|
|
}
|
|
|
|
bool DatabaseLoader::UpdateDatabases()
|
|
{
|
|
return Process(_update);
|
|
}
|
|
|
|
bool DatabaseLoader::PrepareStatements()
|
|
{
|
|
return Process(_prepare);
|
|
}
|
|
|
|
bool DatabaseLoader::Process(std::queue<Predicate>& queue)
|
|
{
|
|
while (!queue.empty())
|
|
{
|
|
if (!queue.front()())
|
|
{
|
|
// Close all open databases which have a registered close operation
|
|
while (!_close.empty())
|
|
{
|
|
_close.top()();
|
|
_close.pop();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
queue.pop();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template TC_DATABASE_API
|
|
DatabaseLoader& DatabaseLoader::AddDatabase<LoginDatabaseConnection>(LoginDatabaseWorkerPool&, std::string const&);
|
|
template TC_DATABASE_API
|
|
DatabaseLoader& DatabaseLoader::AddDatabase<CharacterDatabaseConnection>(CharacterDatabaseWorkerPool&, std::string const&);
|
|
template TC_DATABASE_API
|
|
DatabaseLoader& DatabaseLoader::AddDatabase<WorldDatabaseConnection>(WorldDatabaseWorkerPool&, std::string const&);
|