TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DatabaseLoader Class Reference

#include <DatabaseLoader.h>

Public Types

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

Public Member Functions

 DatabaseLoader (std::string const &logger, uint32 const defaultUpdateMask)
 
template<class T >
DatabaseLoaderAddDatabase (DatabaseWorkerPool< T > &pool, std::string const &name)
 
bool Load ()
 

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
 
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
 

Member Typedef Documentation

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

Member Enumeration Documentation

Enumerator
DATABASE_NONE 
DATABASE_LOGIN 
DATABASE_CHARACTER 
DATABASE_WORLD 
DATABASE_HOTFIX 
DATABASE_MASK_ALL 
43  {
44  DATABASE_NONE = 0,
45 
46  DATABASE_LOGIN = 1,
48  DATABASE_WORLD = 4,
49  DATABASE_HOTFIX = 8,
50 
52  };
Definition: DatabaseLoader.h:48
Definition: DatabaseLoader.h:44
Definition: DatabaseLoader.h:47
Definition: DatabaseLoader.h:46
Definition: DatabaseLoader.h:49
Definition: DatabaseLoader.h:51

Constructor & Destructor Documentation

DatabaseLoader::DatabaseLoader ( std::string const logger,
uint32 const  defaultUpdateMask 
)
25  : _logger(logger), _autoSetup(sConfigMgr->GetBoolDefault("Updates.AutoSetup", true)),
26  _updateFlags(sConfigMgr->GetIntDefault("Updates.EnableDatabases", defaultUpdateMask))
27 {
28 }
bool const _autoSetup
Definition: DatabaseLoader.h:68
#define sConfigMgr
Definition: Config.h:61
uint32 const _updateFlags
Definition: DatabaseLoader.h:69
std::string const _logger
Definition: DatabaseLoader.h:67

Member Function Documentation

template<class T >
template TC_DATABASE_API DatabaseLoader & DatabaseLoader::AddDatabase< WorldDatabaseConnection > ( DatabaseWorkerPool< T > &  pool,
std::string const name 
)
32 {
33  bool const updatesEnabledForThis = DBUpdater<T>::IsEnabled(_updateFlags);
34 
35  _open.push([this, name, updatesEnabledForThis, &pool]() -> bool
36  {
37  std::string const dbString = sConfigMgr->GetStringDefault(name + "DatabaseInfo", "");
38  if (dbString.empty())
39  {
40  TC_LOG_ERROR(_logger, "Database %s not specified in configuration file!", name.c_str());
41  return false;
42  }
43 
44  uint8 const asyncThreads = uint8(sConfigMgr->GetIntDefault(name + "Database.WorkerThreads", 1));
45  if (asyncThreads < 1 || asyncThreads > 32)
46  {
47  TC_LOG_ERROR(_logger, "%s database: invalid number of worker threads specified. "
48  "Please pick a value between 1 and 32.", name.c_str());
49  return false;
50  }
51 
52  uint8 const synchThreads = uint8(sConfigMgr->GetIntDefault(name + "Database.SynchThreads", 1));
53 
54  pool.SetConnectionInfo(dbString, asyncThreads, synchThreads);
55  if (uint32 error = pool.Open())
56  {
57  // Database does not exist
58  if ((error == ER_BAD_DB_ERROR) && updatesEnabledForThis && _autoSetup)
59  {
60  // Try to create the database and connect again if auto setup is enabled
61  if (DBUpdater<T>::Create(pool) && (!pool.Open()))
62  error = 0;
63  }
64 
65  // If the error wasn't handled quit
66  if (error)
67  {
68  TC_LOG_ERROR("sql.driver", "\nDatabasePool %s NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile "
69  "for specific errors. Read wiki at http://www.trinitycore.info/display/tc/TrinityCore+Home", name.c_str());
70 
71  return false;
72  }
73  }
74  // Add the close operation
75  _close.push([&pool]
76  {
77  pool.Close();
78  });
79  return true;
80  });
81 
82  // Populate and update only if updates are enabled for this pool
83  if (updatesEnabledForThis)
84  {
85  _populate.push([this, name, &pool]() -> bool
86  {
87  if (!DBUpdater<T>::Populate(pool))
88  {
89  TC_LOG_ERROR(_logger, "Could not populate the %s database, see log for details.", name.c_str());
90  return false;
91  }
92  return true;
93  });
94 
95  _update.push([this, name, &pool]() -> bool
96  {
97  if (!DBUpdater<T>::Update(pool))
98  {
99  TC_LOG_ERROR(_logger, "Could not update the %s database, see log for details.", name.c_str());
100  return false;
101  }
102  return true;
103  });
104  }
105 
106  _prepare.push([this, name, &pool]() -> bool
107  {
108  if (!pool.PrepareStatements())
109  {
110  TC_LOG_ERROR(_logger, "Could not prepare statements of the %s database, see log for details.", name.c_str());
111  return false;
112  }
113  return true;
114  });
115 
116  return *this;
117 }
std::queue< Predicate > _update
Definition: DatabaseLoader.h:71
bool const _autoSetup
Definition: DatabaseLoader.h:68
std::queue< Predicate > _prepare
Definition: DatabaseLoader.h:71
void SetConnectionInfo(std::string const &infoString, uint8 const asyncThreads, uint8 const synchThreads)
Definition: DatabaseWorkerPool.cpp:36
#define sConfigMgr
Definition: Config.h:61
uint32 const _updateFlags
Definition: DatabaseLoader.h:69
void Close()
Definition: DatabaseWorkerPool.cpp:72
bool PrepareStatements()
Prepares all prepared statements.
Definition: DatabaseWorkerPool.cpp:93
uint32_t uint32
Definition: Define.h:150
std::queue< Predicate > _populate
Definition: DatabaseLoader.h:71
std::stack< Closer > _close
Definition: DatabaseLoader.h:72
uint8_t uint8
Definition: g3dmath.h:164
Definition: DBUpdater.h:69
uint8_t uint8
Definition: Define.h:152
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
std::queue< Predicate > _open
Definition: DatabaseLoader.h:71
std::string const _logger
Definition: DatabaseLoader.h:67
static bool IsEnabled(uint32 const updateMask)
uint32 Open()
Definition: DatabaseWorkerPool.cpp:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DatabaseLoader::Load ( )
120 {
121  if (!_updateFlags)
122  TC_LOG_INFO("sql.updates", "Automatic database updates are disabled for all databases!");
123 
124  if (!OpenDatabases())
125  return false;
126 
127  if (!PopulateDatabases())
128  return false;
129 
130  if (!UpdateDatabases())
131  return false;
132 
133  if (!PrepareStatements())
134  return false;
135 
136  return true;
137 }
uint32 const _updateFlags
Definition: DatabaseLoader.h:69
bool PopulateDatabases()
Definition: DatabaseLoader.cpp:144
bool OpenDatabases()
Definition: DatabaseLoader.cpp:139
bool PrepareStatements()
Definition: DatabaseLoader.cpp:154
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
bool UpdateDatabases()
Definition: DatabaseLoader.cpp:149

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DatabaseLoader::OpenDatabases ( )
private
140 {
141  return Process(_open);
142 }
bool Process(std::queue< Predicate > &queue)
Definition: DatabaseLoader.cpp:159
std::queue< Predicate > _open
Definition: DatabaseLoader.h:71

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DatabaseLoader::PopulateDatabases ( )
private
145 {
146  return Process(_populate);
147 }
bool Process(std::queue< Predicate > &queue)
Definition: DatabaseLoader.cpp:159
std::queue< Predicate > _populate
Definition: DatabaseLoader.h:71

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DatabaseLoader::PrepareStatements ( )
private
155 {
156  return Process(_prepare);
157 }
std::queue< Predicate > _prepare
Definition: DatabaseLoader.h:71
bool Process(std::queue< Predicate > &queue)
Definition: DatabaseLoader.cpp:159

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DatabaseLoader::Process ( std::queue< Predicate > &  queue)
private
160 {
161  while (!queue.empty())
162  {
163  if (!queue.front()())
164  {
165  // Close all open databases which have a registered close operation
166  while (!_close.empty())
167  {
168  _close.top()();
169  _close.pop();
170  }
171 
172  return false;
173  }
174 
175  queue.pop();
176  }
177  return true;
178 }
std::stack< Closer > _close
Definition: DatabaseLoader.h:72

+ Here is the caller graph for this function:

bool DatabaseLoader::UpdateDatabases ( )
private
150 {
151  return Process(_update);
152 }
std::queue< Predicate > _update
Definition: DatabaseLoader.h:71
bool Process(std::queue< Predicate > &queue)
Definition: DatabaseLoader.cpp:159

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

bool const DatabaseLoader::_autoSetup
private
std::stack<Closer> DatabaseLoader::_close
private
std::string const DatabaseLoader::_logger
private
std::queue<Predicate> DatabaseLoader::_open
private
std::queue<Predicate> DatabaseLoader::_populate
private
std::queue<Predicate> DatabaseLoader::_prepare
private
std::queue<Predicate> DatabaseLoader::_update
private
uint32 const DatabaseLoader::_updateFlags
private

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