TrinityCore
DatabaseLoader.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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 "Define.h"
22
23#include <functional>
24#include <queue>
25#include <stack>
26#include <string>
27
28template <class T>
30
31// A helper class to initiate all database worker pools,
32// handles updating, delays preparing of statements and cleans up on failure.
34{
35public:
36 DatabaseLoader(std::string const& logger, uint32 const defaultUpdateMask);
37
38 // Register a database to the loader (lazy implemented)
39 template <class T>
40 DatabaseLoader& AddDatabase(DatabaseWorkerPool<T>& pool, std::string const& name);
41
42 // Load all databases
43 bool Load();
44
46 {
47 DATABASE_NONE = 0,
48
49 DATABASE_LOGIN = 1,
50 DATABASE_CHARACTER = 2,
51 DATABASE_WORLD = 4,
52 DATABASE_HOTFIX = 8,
53
54 DATABASE_MASK_ALL = DATABASE_LOGIN | DATABASE_CHARACTER | DATABASE_WORLD | DATABASE_HOTFIX
55 };
56
57private:
58 bool OpenDatabases();
59 bool PopulateDatabases();
60 bool UpdateDatabases();
61 bool PrepareStatements();
62
63 using Predicate = std::function<bool()>;
64 using Closer = std::function<void()>;
65
66 // Invokes all functions in the given queue and closes the databases on errors.
67 // Returns false when there was an error.
68 bool Process(std::queue<Predicate>& queue);
69
70 std::string const _logger;
71 bool const _autoSetup;
73
74 std::queue<Predicate> _open, _populate, _update, _prepare;
75 std::stack<Closer> _close;
76};
77
78#endif // DatabaseLoader_h__
#define TC_DATABASE_API
Definition: Define.h:111
uint32_t uint32
Definition: Define.h:142
std::string const _logger
bool const _autoSetup
std::function< void()> Closer
uint32 const _updateFlags
std::function< bool()> Predicate
std::queue< Predicate > _open
std::stack< Closer > _close