-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.h
242 lines (203 loc) · 5.27 KB
/
database.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Tibia GIMUD Server - a free and open-source MMORPG server emulator
* Copyright (C) 2017 Alejandro Mujica <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef FS_DATABASE_H_A484B0CDFDE542838F506DCE3D40C693
#define FS_DATABASE_H_A484B0CDFDE542838F506DCE3D40C693
#include <boost/lexical_cast.hpp>
#include <mysql/mysql.h>
class DBResult;
using DBResult_ptr = std::shared_ptr<DBResult>;
class Database
{
public:
Database() = default;
~Database();
// non-copyable
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
/**
* Singleton implementation.
*
* @return database connection handler singleton
*/
static Database& getInstance()
{
static Database instance;
return instance;
}
/**
* Connects to the database
*
* @return true on successful connection, false on error
*/
bool connect();
/**
* Executes command.
*
* Executes query which doesn't generates results (eg. INSERT, UPDATE, DELETE...).
*
* @param query command
* @return true on success, false on error
*/
bool executeQuery(const std::string& query);
/**
* Queries database.
*
* Executes query which generates results (mostly SELECT).
*
* @return results object (nullptr on error)
*/
DBResult_ptr storeQuery(const std::string& query);
/**
* Escapes string for query.
*
* Prepares string to fit SQL queries including quoting it.
*
* @param s string to be escaped
* @return quoted string
*/
std::string escapeString(const std::string& s) const;
/**
* Escapes binary stream for query.
*
* Prepares binary stream to fit SQL queries.
*
* @param s binary stream
* @param length stream length
* @return quoted string
*/
std::string escapeBlob(const char* s, uint32_t length) const;
/**
* Retrieve id of last inserted row
*
* @return id on success, 0 if last query did not result on any rows with auto_increment keys
*/
uint64_t getLastInsertId() const {
return static_cast<uint64_t>(mysql_insert_id(handle));
}
/**
* Get database engine version
*
* @return the database engine version
*/
static const char* getClientVersion() {
return mysql_get_client_info();
}
uint64_t getMaxPacketSize() const {
return maxPacketSize;
}
private:
/**
* Transaction related methods.
*
* Methods for starting, commiting and rolling back transaction. Each of the returns boolean value.
*
* @return true on success, false on error
*/
bool beginTransaction();
bool rollback();
bool commit();
MYSQL* handle = nullptr;
std::recursive_mutex databaseLock;
uint64_t maxPacketSize = 1048576;
friend class DBTransaction;
};
class DBResult
{
public:
explicit DBResult(MYSQL_RES* res);
~DBResult();
// non-copyable
DBResult(const DBResult&) = delete;
DBResult& operator=(const DBResult&) = delete;
template<typename T>
T getNumber(const std::string& s) const
{
auto it = listNames.find(s);
if (it == listNames.end()) {
std::cout << "[Error - DBResult::getNumber] Column '" << s << "' doesn't exist in the result set" << std::endl;
return static_cast<T>(0);
}
if (row[it->second] == nullptr) {
return static_cast<T>(0);
}
T data;
try {
data = boost::lexical_cast<T>(row[it->second]);
} catch (boost::bad_lexical_cast&) {
data = 0;
}
return data;
}
std::string getString(const std::string& s) const;
const char* getStream(const std::string& s, unsigned long& size) const;
bool hasNext() const;
bool next();
private:
MYSQL_RES* handle;
MYSQL_ROW row;
std::map<std::string, size_t> listNames;
friend class Database;
};
/**
* INSERT statement.
*/
class DBInsert
{
public:
explicit DBInsert(std::string query);
bool addRow(const std::string& row);
bool addRow(std::ostringstream& row);
bool execute();
private:
std::string query;
std::string values;
size_t length;
};
class DBTransaction
{
public:
constexpr DBTransaction() = default;
~DBTransaction() {
if (state == STATE_START) {
Database::getInstance().rollback();
}
}
// non-copyable
DBTransaction(const DBTransaction&) = delete;
DBTransaction& operator=(const DBTransaction&) = delete;
bool begin() {
state = STATE_START;
return Database::getInstance().beginTransaction();
}
bool commit() {
if (state != STATE_START) {
return false;
}
state = STATE_COMMIT;
return Database::getInstance().commit();
}
private:
enum TransactionStates_t {
STATE_NO_START,
STATE_START,
STATE_COMMIT,
};
TransactionStates_t state = STATE_NO_START;
};
#endif