TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DatabaseLoader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef DatabaseLoader_h__
19 #define DatabaseLoader_h__
20 
21 #include "DatabaseWorkerPool.h"
22 #include "DBUpdater.h"
23 
24 #include <functional>
25 #include <stack>
26 #include <queue>
27 
28 // A helper class to initiate all database worker pools,
29 // handles updating, delays preparing of statements and cleans up on failure.
31 {
32 public:
33  DatabaseLoader(std::string const& logger, uint32 const defaultUpdateMask);
34 
35  // Register a database to the loader (lazy implemented)
36  template <class T>
37  DatabaseLoader& AddDatabase(DatabaseWorkerPool<T>& pool, std::string const& name);
38 
39  // Load all databases
40  bool Load();
41 
43  {
44  DATABASE_NONE = 0,
45 
46  DATABASE_LOGIN = 1,
47  DATABASE_CHARACTER = 2,
48  DATABASE_WORLD = 4,
49  DATABASE_HOTFIX = 8,
50 
51  DATABASE_MASK_ALL = DATABASE_LOGIN | DATABASE_CHARACTER | DATABASE_WORLD | DATABASE_HOTFIX
52  };
53 
54 private:
55  bool OpenDatabases();
56  bool PopulateDatabases();
57  bool UpdateDatabases();
58  bool PrepareStatements();
59 
60  using Predicate = std::function<bool()>;
61  using Closer = std::function<void()>;
62 
63  // Invokes all functions in the given queue and closes the databases on errors.
64  // Returns false when there was an error.
65  bool Process(std::queue<Predicate>& queue);
66 
67  std::string const _logger;
68  bool const _autoSetup;
70 
71  std::queue<Predicate> _open, _populate, _update, _prepare;
72  std::stack<Closer> _close;
73 };
74 
75 #endif // DatabaseLoader_h__
#define TC_DATABASE_API
Definition: Define.h:122
DatabaseTypeFlags
Definition: DatabaseLoader.h:42
std::queue< Predicate > _update
Definition: DatabaseLoader.h:71
bool const _autoSetup
Definition: DatabaseLoader.h:68
std::function< void()> Closer
Definition: DatabaseLoader.h:61
uint32 const _updateFlags
Definition: DatabaseLoader.h:69
Definition: DatabaseLoader.h:30
std::function< bool()> Predicate
Definition: DatabaseLoader.h:60
uint32_t uint32
Definition: Define.h:150
std::stack< Closer > _close
Definition: DatabaseLoader.h:72
std::string const _logger
Definition: DatabaseLoader.h:67
Definition: DatabaseWorkerPool.h:48