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 = {} 
)
47 : _logger(logger),
48 _modulesList(modulesList),
49 _autoSetup(sConfigMgr->GetOption<bool>("Updates.AutoSetup", true)),
50 _updateFlags(sConfigMgr->GetOption<uint32>("Updates.EnableDatabases", defaultUpdateMask)) { }
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
#define sConfigMgr
Definition Config.h:93

Member Function Documentation

◆ AddDatabase()

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

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 ( )
174{
175 if (!_updateFlags)
176 LOG_WARN("sql.updates", "> AUTOUPDATER: Automatic database updates are disabled for all databases in the config! This is not recommended!");
177
178 if (!OpenDatabases())
179 return false;
180
181 if (!PopulateDatabases())
182 return false;
183
184 if (!UpdateDatabases())
185 return false;
186
187 if (!PrepareStatements())
188 return false;
189
190 return true;
191}
bool PrepareStatements()
Definition DatabaseLoader.cpp:208
bool PopulateDatabases()
Definition DatabaseLoader.cpp:198
bool UpdateDatabases()
Definition DatabaseLoader.cpp:203
bool OpenDatabases()
Definition DatabaseLoader.cpp:193

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

Referenced by StartDB().

◆ OpenDatabases()

bool DatabaseLoader::OpenDatabases ( )
private
194{
195 return Process(_open);
196}
bool Process(std::queue< Predicate > &queue)
Definition DatabaseLoader.cpp:213

References _open, and Process().

Referenced by Load().

◆ PopulateDatabases()

bool DatabaseLoader::PopulateDatabases ( )
private
199{
200 return Process(_populate);
201}

References _populate, and Process().

Referenced by Load().

◆ PrepareStatements()

bool DatabaseLoader::PrepareStatements ( )
private
209{
210 return Process(_prepare);
211}

References _prepare, and Process().

Referenced by Load().

◆ Process()

bool DatabaseLoader::Process ( std::queue< Predicate > &  queue)
private
214{
215 while (!queue.empty())
216 {
217 if (!queue.front()())
218 {
219 // Close all open databases which have a registered close operation
220 while (!_close.empty())
221 {
222 _close.top()();
223 _close.pop();
224 }
225
226 return false;
227 }
228
229 queue.pop();
230 }
231
232 return true;
233}

References _close.

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

◆ UpdateDatabases()

bool DatabaseLoader::UpdateDatabases ( )
private
204{
205 return Process(_update);
206}

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


The documentation for this class was generated from the following files: