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_MASK_ALL = DATABASE_LOGIN | DATABASE_CHARACTER | DATABASE_WORLD
}
 

Public Member Functions

 DatabaseLoader (std::string const &logger, uint32 const defaultUpdateMask=7, 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_MASK_ALL 
45 {
46 DATABASE_NONE = 0,
47
51
53 };
@ DATABASE_CHARACTER
Definition: DatabaseLoader.h:49
@ DATABASE_WORLD
Definition: DatabaseLoader.h:50
@ DATABASE_LOGIN
Definition: DatabaseLoader.h:48
@ DATABASE_NONE
Definition: DatabaseLoader.h:46
@ DATABASE_MASK_ALL
Definition: DatabaseLoader.h:52

Constructor & Destructor Documentation

◆ DatabaseLoader()

DatabaseLoader::DatabaseLoader ( std::string const &  logger,
uint32 const  defaultUpdateMask = 7,
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:74
std::uint32_t uint32
Definition: Define.h:107
std::string const _logger
Definition: DatabaseLoader.h:73
bool const _autoSetup
Definition: DatabaseLoader.h:75
uint32 const _updateFlags
Definition: DatabaseLoader.h:76
std::string_view _modulesList
Definition: DatabaseLoader.h:74

Member Function Documentation

◆ AddDatabase()

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

Referenced by StartDB().

◆ Load()

bool DatabaseLoader::Load ( )
155{
156 if (!_updateFlags)
157 LOG_WARN("sql.updates", "> AUTOUPDATER: Automatic database updates are disabled for all databases in the config! This is not recommended!");
158
159 if (!OpenDatabases())
160 return false;
161
162 if (!PopulateDatabases())
163 return false;
164
165 if (!UpdateDatabases())
166 return false;
167
168 if (!PrepareStatements())
169 return false;
170
171 return true;
172}
bool PrepareStatements()
Definition: DatabaseLoader.cpp:189
bool PopulateDatabases()
Definition: DatabaseLoader.cpp:179
bool UpdateDatabases()
Definition: DatabaseLoader.cpp:184
bool OpenDatabases()
Definition: DatabaseLoader.cpp:174

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

Referenced by StartDB().

◆ OpenDatabases()

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

References _open, and Process().

Referenced by Load().

◆ PopulateDatabases()

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

References _populate, and Process().

Referenced by Load().

◆ PrepareStatements()

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

References _prepare, and Process().

Referenced by Load().

◆ Process()

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

References _close.

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

◆ UpdateDatabases()

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

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().