AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
DatabaseLoader Class Reference

#include "DatabaseLoader.h"

Public Types

enum  DatabaseTypeFlags {
  DATABASE_NONE = 0 ,
  DATABASE_LOGIN = 1 ,
  DATABASE_CHARACTER = 2 ,
  DATABASE_WORLD = 4 ,
  DATABASE_DBC = 8 ,
  DATABASE_MODULE = 16 ,
  DATABASE_MASK_ALL = DATABASE_LOGIN | DATABASE_CHARACTER | DATABASE_WORLD | DATABASE_DBC | DATABASE_MODULE
}
 

Public Member Functions

 DatabaseLoader (std::string const &logger, uint32 const defaultUpdateMask=0, std::string_view modulesList={})
 
template<class T >
DatabaseLoaderAddDatabase (DatabaseWorkerPool< T > &pool, std::string const &name)
 
bool Load ()
 
uint32 GetUpdateFlags () const
 

Private Types

using Predicate = std::function< bool()>
 
using Closer = std::function< void()>
 

Private Member Functions

bool OpenDatabases ()
 
bool PopulateDatabases ()
 
bool UpdateDatabases ()
 
bool PrepareStatements ()
 
bool Process (std::queue< Predicate > &queue)
 

Private Attributes

std::string const _logger
 
std::string_view _modulesList
 
bool const _autoSetup
 
uint32 const _updateFlags
 
std::queue< Predicate_open
 
std::queue< Predicate_populate
 
std::queue< Predicate_update
 
std::queue< Predicate_prepare
 
std::stack< Closer_close
 

Detailed Description

Member Typedef Documentation

◆ Closer

using DatabaseLoader::Closer = std::function<void()>
private

◆ Predicate

using DatabaseLoader::Predicate = std::function<bool()>
private

Member Enumeration Documentation

◆ DatabaseTypeFlags

Enumerator
DATABASE_NONE 
DATABASE_LOGIN 
DATABASE_CHARACTER 
DATABASE_WORLD 
DATABASE_DBC 
DATABASE_MODULE 
DATABASE_MASK_ALL 
45 {
46 DATABASE_NONE = 0,
47
51 DATABASE_DBC = 8,
52 DATABASE_MODULE = 16,
53
55 };
@ DATABASE_CHARACTER
Definition: DatabaseLoader.h:49
@ DATABASE_MODULE
Definition: DatabaseLoader.h:52
@ DATABASE_WORLD
Definition: DatabaseLoader.h:50
@ DATABASE_DBC
Definition: DatabaseLoader.h:51
@ DATABASE_LOGIN
Definition: DatabaseLoader.h:48
@ DATABASE_NONE
Definition: DatabaseLoader.h:46
@ DATABASE_MASK_ALL
Definition: DatabaseLoader.h:54

Constructor & Destructor Documentation

◆ DatabaseLoader()

DatabaseLoader::DatabaseLoader ( std::string const &  logger,
uint32 const  defaultUpdateMask = 0,
std::string_view  modulesList = {} 
)
29 : _logger(logger),
30 _modulesList(modulesList),
31 _autoSetup(sConfigMgr->GetOption<bool>("Updates.AutoSetup", true)),
32 _updateFlags(sConfigMgr->GetOption<uint32>("Updates.EnableDatabases", defaultUpdateMask)) { }
#define sConfigMgr
Definition: Config.h:95
std::uint32_t uint32
Definition: Define.h:108
std::string const _logger
Definition: DatabaseLoader.h:75
bool const _autoSetup
Definition: DatabaseLoader.h:77
uint32 const _updateFlags
Definition: DatabaseLoader.h:78
std::string_view _modulesList
Definition: DatabaseLoader.h:76

Member Function Documentation

◆ AddDatabase()

template<class T >
template AC_DATABASE_API DatabaseLoader & DatabaseLoader::AddDatabase< ModuleDatabaseConnection > ( DatabaseWorkerPool< T > &  pool,
std::string const &  name 
)
36{
37 bool const updatesEnabledForThis = DBUpdater<T>::IsEnabled(_updateFlags);
38
39 _open.push([this, name, updatesEnabledForThis, &pool]() -> bool
40 {
41 std::string const dbString = sConfigMgr->GetOption<std::string>(name + "DatabaseInfo", "");
42 if (dbString.empty())
43 {
44 LOG_ERROR(_logger, "{}DatabaseInfo is not specified in configuration file!"
45 "\nSearch the wiki for ACE00047 in Common Errors (https://www.azerothcore.org/wiki/common-errors#ace00043)", name);
46 return false;
47 }
48
49 uint8 const asyncThreads = sConfigMgr->GetOption<uint8>(name + "Database.WorkerThreads", 1);
50 if (asyncThreads < 1 || asyncThreads > 32)
51 {
52 LOG_ERROR(_logger, "{} database: invalid number of worker threads specified. "
53 "Please pick a value between 1 and 32.", name);
54 return false;
55 }
56
57 uint8 const synchThreads = sConfigMgr->GetOption<uint8>(name + "Database.SynchThreads", 1);
58
59 pool.SetConnectionInfo(dbString, asyncThreads, synchThreads);
60
61 if (uint32 error = pool.Open())
62 {
63 // Try reconnect
64 if (error == CR_CONNECTION_ERROR)
65 {
66 uint8 const attempts = sConfigMgr->GetOption<uint8>("Database.Reconnect.Attempts", 20);
67 Seconds reconnectSeconds = Seconds(sConfigMgr->GetOption<uint8>("Database.Reconnect.Seconds", 15));
68 uint8 reconnectCount = 0;
69
70 while (reconnectCount < attempts)
71 {
72 LOG_WARN(_logger, "> Retrying after {} seconds", static_cast<uint32>(reconnectSeconds.count()));
73 std::this_thread::sleep_for(reconnectSeconds);
74 error = pool.Open();
75
76 if (error == CR_CONNECTION_ERROR)
77 {
78 reconnectCount++;
79 }
80 else
81 {
82 break;
83 }
84 }
85 }
86
87 // Database does not exist
88 if ((error == ER_BAD_DB_ERROR) && updatesEnabledForThis && _autoSetup)
89 {
90 // Try to create the database and connect again if auto setup is enabled
91 if (DBUpdater<T>::Create(pool) && (!pool.Open()))
92 {
93 error = 0;
94 }
95 }
96
97 // If the error wasn't handled quit
98 if (error)
99 {
100 LOG_ERROR(_logger, "DatabasePool {} NOT opened. There were errors opening the MySQL connections. "
101 "Check your log file for specific errors", name);
102
103 return false;
104 }
105 }
106 // Add the close operation
107 _close.push([&pool]
108 {
109 pool.Close();
110 });
111
112 return true;
113 });
114
115 // Populate and update only if updates are enabled for this pool
116 if (updatesEnabledForThis)
117 {
118 _populate.push([this, name, &pool]() -> bool
119 {
120 if (!DBUpdater<T>::Populate(pool))
121 {
122 LOG_ERROR(_logger, "Could not populate the {} database, see log for details.", name);
123 return false;
124 }
125
126 return true;
127 });
128
129 _update.push([this, name, &pool]() -> bool
130 {
132 {
133 LOG_ERROR(_logger, "Could not update the {} database, see log for details.", name);
134 return false;
135 }
136
137 return true;
138 });
139 }
140
141 _prepare.push([this, name, &pool]() -> bool
142 {
143 if (!pool.PrepareStatements())
144 {
145 LOG_ERROR(_logger, "Could not prepare statements of the {} database, see log for details.", name);
146 return false;
147 }
148
149 return true;
150 });
151
152 return *this;
153}
std::uint8_t uint8
Definition: Define.h:110
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
#define LOG_WARN(filterType__,...)
Definition: Log.h:163
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition: Duration.h:30
bool PrepareStatements()
Prepares all prepared statements.
Definition: DatabaseWorkerPool.cpp:146
uint32 Open()
Definition: DatabaseWorkerPool.cpp:100
void SetConnectionInfo(std::string_view infoString, uint8 const asyncThreads, uint8 const synchThreads)
Definition: DatabaseWorkerPool.cpp:91
void Close()
Definition: DatabaseWorkerPool.cpp:126
std::queue< Predicate > _populate
Definition: DatabaseLoader.h:80
std::queue< Predicate > _prepare
Definition: DatabaseLoader.h:80
std::queue< Predicate > _update
Definition: DatabaseLoader.h:80
std::queue< Predicate > _open
Definition: DatabaseLoader.h:80
std::stack< Closer > _close
Definition: DatabaseLoader.h:81
Definition: DBUpdater.h:68
static bool IsEnabled(uint32 const updateMask)

References _autoSetup, _close, _logger, _modulesList, _open, _populate, _prepare, _update, _updateFlags, DatabaseWorkerPool< T >::Close(), DBUpdater< T >::IsEnabled(), LOG_ERROR, LOG_WARN, DatabaseWorkerPool< T >::Open(), DatabaseWorkerPool< T >::PrepareStatements(), sConfigMgr, and DatabaseWorkerPool< T >::SetConnectionInfo().

Referenced by StartDB().

◆ GetUpdateFlags()

uint32 DatabaseLoader::GetUpdateFlags ( ) const
inline
58 {
59 return _updateFlags;
60 }

Referenced by StartDB().

◆ Load()

bool DatabaseLoader::Load ( )
156{
157 if (!_updateFlags)
158 LOG_INFO("sql.updates", "Automatic database updates are disabled for all databases!");
159
160 if (!OpenDatabases())
161 return false;
162
163 if (!PopulateDatabases())
164 return false;
165
166 if (!UpdateDatabases())
167 return false;
168
169 if (!PrepareStatements())
170 return false;
171
172 return true;
173}
#define LOG_INFO(filterType__,...)
Definition: Log.h:167
bool PrepareStatements()
Definition: DatabaseLoader.cpp:190
bool PopulateDatabases()
Definition: DatabaseLoader.cpp:180
bool UpdateDatabases()
Definition: DatabaseLoader.cpp:185
bool OpenDatabases()
Definition: DatabaseLoader.cpp:175

References _updateFlags, LOG_INFO, OpenDatabases(), PopulateDatabases(), PrepareStatements(), and UpdateDatabases().

Referenced by StartDB().

◆ OpenDatabases()

bool DatabaseLoader::OpenDatabases ( )
private
176{
177 return Process(_open);
178}
bool Process(std::queue< Predicate > &queue)
Definition: DatabaseLoader.cpp:195

References _open, and Process().

Referenced by Load().

◆ PopulateDatabases()

bool DatabaseLoader::PopulateDatabases ( )
private
181{
182 return Process(_populate);
183}

References _populate, and Process().

Referenced by Load().

◆ PrepareStatements()

bool DatabaseLoader::PrepareStatements ( )
private
191{
192 return Process(_prepare);
193}

References _prepare, and Process().

Referenced by Load().

◆ Process()

bool DatabaseLoader::Process ( std::queue< Predicate > &  queue)
private
196{
197 while (!queue.empty())
198 {
199 if (!queue.front()())
200 {
201 // Close all open databases which have a registered close operation
202 while (!_close.empty())
203 {
204 _close.top()();
205 _close.pop();
206 }
207
208 return false;
209 }
210
211 queue.pop();
212 }
213
214 return true;
215}

References _close.

Referenced by OpenDatabases(), PopulateDatabases(), PrepareStatements(), and UpdateDatabases().

◆ UpdateDatabases()

bool DatabaseLoader::UpdateDatabases ( )
private
186{
187 return Process(_update);
188}

References _update, and Process().

Referenced by Load().

Member Data Documentation

◆ _autoSetup

bool const DatabaseLoader::_autoSetup
private

Referenced by AddDatabase().

◆ _close

std::stack<Closer> DatabaseLoader::_close
private

Referenced by AddDatabase(), and Process().

◆ _logger

std::string const DatabaseLoader::_logger
private

Referenced by AddDatabase().

◆ _modulesList

std::string_view DatabaseLoader::_modulesList
private

Referenced by AddDatabase().

◆ _open

std::queue<Predicate> DatabaseLoader::_open
private

Referenced by AddDatabase(), and OpenDatabases().

◆ _populate

std::queue<Predicate> DatabaseLoader::_populate
private

Referenced by AddDatabase(), and PopulateDatabases().

◆ _prepare

std::queue<Predicate> DatabaseLoader::_prepare
private

Referenced by AddDatabase(), and PrepareStatements().

◆ _update

std::queue<Predicate> DatabaseLoader::_update
private

Referenced by AddDatabase(), and UpdateDatabases().

◆ _updateFlags

uint32 const DatabaseLoader::_updateFlags
private

Referenced by AddDatabase(), and Load().