-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.cpp
274 lines (264 loc) · 10.8 KB
/
Database.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**********************************************************************
OJ: An online judge server written with only C++ and MySQL.
Copyright (C) 2024 langningchen
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 3 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, see <https://www.gnu.org/licenses/>.
**********************************************************************/
#include "Database.hpp"
#include "Settings.hpp"
#include "Utilities.hpp"
DATABASE::SELECT::SELECT(std::string TableName) {
this->Connection = DATABASE::CreateConnection();
this->TableName = TableName;
}
DATABASE::SELECT::~SELECT() { DATABASE::CloseConnection(Connection); }
DATABASE::SELECT &DATABASE::SELECT::Select(std::string Column) {
this->Columns.push_back(Column);
return *this;
}
DATABASE::SELECT &DATABASE::SELECT::Where(std::string Column, std::string Value) {
this->Conditions.push_back({Column, Value});
return *this;
}
DATABASE::SELECT &DATABASE::SELECT::Where(std::string Column, int Value) { return Where(Column, std::to_string(Value)); }
DATABASE::SELECT &DATABASE::SELECT::Order(std::string Column, bool Ascending) {
this->Orders.push_back({Column, Ascending});
this->UseOrder = true;
return *this;
}
DATABASE::SELECT &DATABASE::SELECT::Limit(int Limits) {
this->Limits = Limits;
return *this;
}
DATABASE::SELECT &DATABASE::SELECT::Offset(int Offsets) {
this->Offsets = Offsets;
return *this;
}
void DATABASE::SELECT::Execute(std::function<void(std::vector<std::map<std::string, std::string>>)> Callback) {
if (Connection == nullptr)
throw EXCEPTION("Not connected");
if (Columns.empty()) {
sql::ResultSet *ResultSet(Connection->createStatement()->executeQuery("DESCRIBE " + TableName));
while (ResultSet->next())
Columns.push_back(ResultSet->getString("Field"));
ResultSet->close();
delete ResultSet;
}
std::string Query = "SELECT ";
for (auto &Column : Columns)
Query += "`" + Column + "`, ";
Query.erase(Query.end() - 2, Query.end());
Query += " FROM `" + TableName + "`";
if (!Conditions.empty()) {
Query += " WHERE ";
for (auto &Condition : Conditions)
Query += "`" + Condition.first + "`=? AND ";
Query.erase(Query.end() - 5, Query.end());
}
if (!Orders.empty()) {
Query += " ORDER BY ";
for (auto &Order : Orders)
Query += "`" + Order.first + "` " + (Order.second ? "ASC" : "DESC") + ", ";
Query.erase(Query.end() - 2, Query.end());
}
if (Limits)
Query += " LIMIT " + std::to_string(Limits);
if (Offsets)
Query += " OFFSET " + std::to_string(Offsets);
try {
sql::PreparedStatement *PreparedStatement(Connection->prepareStatement(Query));
int Index = 1;
for (auto &Condition : Conditions)
PreparedStatement->setString(Index++, Condition.second);
sql::ResultSet *ResultSet(PreparedStatement->executeQuery());
PreparedStatement->close();
delete PreparedStatement;
std::vector<std::map<std::string, std::string>> Data;
while (ResultSet->next()) {
std::map<std::string, std::string> Row;
for (auto &Column : Columns)
Row[Column] = ResultSet->getString(Column);
Data.push_back(Row);
}
if (Callback != nullptr)
Callback(Data);
ResultSet->close();
delete ResultSet;
} catch (sql::SQLException &e) {
throw EXCEPTION("SQLException " + std::to_string(e.getErrorCode()) + " " + e.what() + " " + e.getSQLState());
}
}
DATABASE::INSERT::INSERT(std::string TableName) {
this->Connection = DATABASE::CreateConnection();
this->TableName = TableName;
}
DATABASE::INSERT::~INSERT() { DATABASE::CloseConnection(Connection); }
DATABASE::INSERT &DATABASE::INSERT::Insert(std::string Column, std::string Value) {
this->Columns.push_back(Column);
this->Values.push_back(Value);
return *this;
}
DATABASE::INSERT &DATABASE::INSERT::Insert(std::string Column, int Value) { return Insert(Column, std::to_string(Value)); }
void DATABASE::INSERT::Execute(std::function<void(int)> Callback) {
if (Connection == nullptr)
throw EXCEPTION("Not connected");
std::string Query = "INSERT INTO " + TableName + " (";
for (auto &Column : Columns)
Query += "`" + Column + "`, ";
Query.erase(Query.end() - 2, Query.end());
Query += ") VALUES (";
for (size_t i = 0; i < Values.size(); i++)
Query += "?, ";
Query.erase(Query.end() - 2, Query.end());
Query += ")";
try {
sql::PreparedStatement *PreparedStatement(Connection->prepareStatement(Query));
int Index = 1;
for (auto &Value : Values)
PreparedStatement->setString(Index++, Value);
PreparedStatement->execute();
PreparedStatement->close();
delete PreparedStatement;
sql::ResultSet *ResultSet(Connection->createStatement()->executeQuery("SELECT LAST_INSERT_ID()"));
ResultSet->next();
if (Callback != nullptr)
Callback(ResultSet->getInt(1));
ResultSet->close();
delete ResultSet;
} catch (sql::SQLException &e) {
throw EXCEPTION("SQLException " + std::to_string(e.getErrorCode()) + " " + e.what() + " " + e.getSQLState());
}
}
DATABASE::DELETE::DELETE(std::string TableName) {
this->Connection = DATABASE::CreateConnection();
this->TableName = TableName;
}
DATABASE::DELETE::~DELETE() { DATABASE::CloseConnection(Connection); }
DATABASE::DELETE &DATABASE::DELETE::Where(std::string Column, std::string Value) {
this->Conditions.push_back({Column, Value});
return *this;
}
DATABASE::DELETE &DATABASE::DELETE::Where(std::string Column, int Value) {
return Where(Column, std::to_string(Value));
}
void DATABASE::DELETE::Execute() {
if (Connection == nullptr)
throw EXCEPTION("Not connected");
std::string Query = "DELETE FROM " + TableName;
if (!Conditions.empty()) {
Query += " WHERE ";
for (auto &Condition : Conditions)
Query += "`" + Condition.first + "`=? AND ";
Query.erase(Query.end() - 5, Query.end());
}
try {
sql::PreparedStatement *PreparedStatement(Connection->prepareStatement(Query));
int Index = 1;
for (auto &Condition : Conditions)
PreparedStatement->setString(Index++, Condition.second);
PreparedStatement->execute();
PreparedStatement->close();
delete PreparedStatement;
} catch (sql::SQLException &e) {
throw EXCEPTION("SQLException " + std::to_string(e.getErrorCode()) + " " + e.what() + " " + e.getSQLState());
}
}
DATABASE::UPDATE::UPDATE(std::string TableName) {
this->Connection = DATABASE::CreateConnection();
this->TableName = TableName;
}
DATABASE::UPDATE::~UPDATE() { DATABASE::CloseConnection(Connection); }
DATABASE::UPDATE &DATABASE::UPDATE::Set(std::string Column, std::string Value) {
this->Columns.push_back({Column, Value});
return *this;
}
DATABASE::UPDATE &DATABASE::UPDATE::Set(std::string Column, int Value) { return Set(Column, std::to_string(Value)); }
DATABASE::UPDATE &DATABASE::UPDATE::Where(std::string Column, std::string Value) {
this->Conditions.push_back({Column, Value});
return *this;
}
DATABASE::UPDATE &DATABASE::UPDATE::Where(std::string Column, int Value) { return Where(Column, std::to_string(Value)); }
void DATABASE::UPDATE::Execute() {
if (Connection == nullptr)
throw EXCEPTION("Not connected");
std::string Query = "UPDATE " + TableName + " SET ";
for (auto &Column : Columns)
Query += "`" + Column.first + "`=?, ";
Query.erase(Query.end() - 2, Query.end());
if (!Conditions.empty()) {
Query += " WHERE ";
for (auto &Condition : Conditions)
Query += "`" + Condition.first + "`=? AND ";
Query.erase(Query.end() - 5, Query.end());
}
try {
sql::PreparedStatement *PreparedStatement(Connection->prepareStatement(Query));
int Index = 1;
for (auto &Column : Columns)
PreparedStatement->setString(Index++, Column.second);
for (auto &Condition : Conditions)
PreparedStatement->setString(Index++, Condition.second);
PreparedStatement->execute();
PreparedStatement->close();
delete PreparedStatement;
} catch (sql::SQLException &e) {
throw EXCEPTION("SQLException " + std::to_string(e.getErrorCode()) + " " + e.what() + " " + e.getSQLState());
}
}
DATABASE::SIZE::SIZE(std::string TableName) {
this->Connection = DATABASE::CreateConnection();
this->TableName = TableName;
}
void DATABASE::SIZE::Execute(std::function<void(int)> Callback) {
if (Connection == nullptr)
throw EXCEPTION("Not connected");
try {
sql::ResultSet *ResultSet(Connection->createStatement()->executeQuery("SELECT COUNT(*) FROM " + TableName));
ResultSet->next();
if (Callback != nullptr)
Callback(ResultSet->getInt(1));
ResultSet->close();
delete ResultSet;
} catch (sql::SQLException &e) {
throw EXCEPTION("SQLException " + std::to_string(e.getErrorCode()) + " " + e.what() + " " + e.getSQLState());
}
}
DATABASE::TRUNCATE::TRUNCATE(std::string TableName) {
this->Connection = DATABASE::CreateConnection();
this->TableName = TableName;
}
void DATABASE::TRUNCATE::Execute() {
if (Connection == nullptr)
throw EXCEPTION("Not connected");
try {
Connection->createStatement()->execute("TRUNCATE TABLE " + TableName);
} catch (sql::SQLException &e) {
throw EXCEPTION("SQLException " + std::to_string(e.getErrorCode()) + " " + e.what() + " " + e.getSQLState());
}
}
sql::Connection *DATABASE::CreateConnection() {
sql::Connection *Connection;
try {
sql::Driver *Driver = sql::mysql::get_mysql_driver_instance();
Connection = Driver->connect(Settings.DatabaseHost.c_str(),
Settings.DatabaseUsername.c_str(),
Settings.DatabasePassword.c_str());
Connection->setSchema(Settings.DatabaseName.c_str());
} catch (sql::SQLException &e) {
std::cout << "SQLException " << std::to_string(e.getErrorCode()) << " " << e.what() << " " << e.getSQLState() << std::endl;
return nullptr;
}
return Connection;
}
void DATABASE::CloseConnection(sql::Connection *Connection) {
Connection->close();
delete Connection;
}