-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.cpp
More file actions
316 lines (260 loc) · 13.1 KB
/
Copy pathconnector.cpp
File metadata and controls
316 lines (260 loc) · 13.1 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "connector.h"
#include "utilities.h"
// for debugging and random data filling
#include <iostream>
//#include <random>
//#include <cstdlib>
Connector::Connector() {
driver = sql::mariadb::get_driver_instance();
}
bool Connector::validate(QString usr, QString pswd, QString db) {
try {
url = sql::SQLString("jdbc:mariadb://localhost:3306/" + db.toStdString());
properties = sql::Properties({{"user", usr.toStdString()}, {"password", pswd.toStdString()}});
conn = std::unique_ptr<sql::Connection>(driver->connect(url, properties));
} catch (sql::SQLException e) {
std::cout << "error";
std::cerr << e.what() << '\n';
return false;
}
return true;
}
void Connector::initDB() {
// std::unique_ptr<sql::PreparedStatement> stmnt;
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("CREATE TABLE IF NOT EXISTS hostelers ("
"room VARCHAR(4) NOT NULL, "
"roll INT(8) UNSIGNED PRIMARY KEY, "
"name VARCHAR(20) NOT NULL, "
"branch VARCHAR(10) NOT NULL, "
"phone BIGINT UNSIGNED UNIQUE NOT NULL, "
"email VARCHAR(20) UNIQUE NOT NULL, "
"emergency_contact BIGINT UNSIGNED NOT NULL, "
"remark VARCHAR(50))"));
stmnt->executeQuery();
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("CREATE TABLE IF NOT EXISTS staffs ("
"floor_block VARCHAR(3) NOT NULL, "
"staff_id INT(8) UNSIGNED PRIMARY KEY, "
"name VARCHAR(20) NOT NULL, "
"designation VARCHAR(15) NOT NULL, "
"phone BIGINT UNSIGNED UNIQUE NOT NULL, "
"email VARCHAR(20) UNIQUE NOT NULL, "
"remark VARCHAR(50))"));
stmnt->executeQuery();
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("CREATE TABLE IF NOT EXISTS complaints ("
"complaint_id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, "
"date DATE NOT NULL, "
"roll INT(8) UNSIGNED NOT NULL, "
"category VARCHAR(15) NOT NULL, "
"complaint VARCHAR(50) NOT NULL, "
"status BOOLEAN NOT NULL, "
"remark VARCHAR(50))"));
stmnt->executeQuery();
// refilling table with random values.
app_testing::tableFiller(conn);
}
// hostelers table functions
sql::ResultSet* Connector::getAllHostelers() {
std::unique_ptr<sql::Statement> stmnt(conn->createStatement());
sql::ResultSet *res = stmnt->executeQuery("SELECT * FROM hostelers ORDER BY room");
return res;
}
sqlerr::query_err Connector::addHosteler(Hosteler h) {
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("INSERT INTO hostelers VALUE ("
+ enquote(h.room) + ", " + std::to_string(h.roll) + ", "
+ enquote(h.name) + ", " + enquote(h.branch) + ", " + std::to_string(h.phone)
+ ", " + enquote(h.email) + ", " + std::to_string(h.emergency)
+ ", " + enquote(h.remark) + ")"));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLSyntaxErrorException e) {
std::cerr << e.what();
std::string err(e.what());
if (err.find("PRI") != std::string::npos)
return sqlerr::DUP_PRI;
if (err.find("phone") != std::string::npos)
return sqlerr::DUP_PHONE;
if (err.find("email") != std::string::npos)
return sqlerr::DUP_EMAIL;
else
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::deleteHosteler(uint64_t n, std::string rolls[]) {
std::string rollsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
rollsTuple += rolls[i] + ", ";
}
rollsTuple += rolls[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("DELETE FROM hostelers WHERE roll IN " + rollsTuple));
stmnt->executeQuery();
// also remove complaints of these hostelers
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("DELETE FROM complaints WHERE roll IN " + rollsTuple));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << e.what();
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::remarkHosteler(uint64_t n, std::string rolls[], std::string remark) {
std::string rollsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
rollsTuple += rolls[i] + ", ";
}
rollsTuple += rolls[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("UPDATE hostelers SET remark = " + enquote(remark) +
"WHERE roll IN " + rollsTuple ));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << "\n" << e.what();
return sqlerr::UNKNOWN;
}
}
// staffs table functions
sql::ResultSet* Connector::getAllStaffs() {
std::unique_ptr<sql::Statement> stmnt(conn->createStatement());
sql::ResultSet *res = stmnt->executeQuery("SELECT * FROM staffs ORDER BY floor_block");
return res;
}
sqlerr::query_err Connector::addStaff(Staff s) {
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("INSERT INTO staffs VALUE ("
+ enquote(s.floor_block) + ", " + std::to_string(s.staff_id) + ", "
+ enquote(s.name) + ", " + enquote(s.designation) + ", " + std::to_string(s.phone)
+ ", " + enquote(s.email) + ", " + enquote(s.remark) + ")"));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLSyntaxErrorException e) {
std::cerr << e.what();
std::string err(e.what());
if (err.find("PRI") != std::string::npos)
return sqlerr::DUP_PRI;
if (err.find("phone") != std::string::npos)
return sqlerr::DUP_PHONE;
if (err.find("email") != std::string::npos)
return sqlerr::DUP_EMAIL;
else
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::deleteStaff(uint64_t n, std::string staff_ids[]) {
std::string staff_idsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
staff_idsTuple += staff_ids[i] + ", ";
}
staff_idsTuple += staff_ids[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("DELETE FROM staffs WHERE staff_id IN " + staff_idsTuple));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << e.what();
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::remarkStaff(uint64_t n, std::string staff_ids[], std::string remark) {
std::string staff_idsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
staff_idsTuple += staff_ids[i] + ", ";
}
staff_idsTuple += staff_ids[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("UPDATE staffs SET remark = " + enquote(remark) +
"WHERE staff_id IN " + staff_idsTuple ));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << "\n" << e.what();
return sqlerr::UNKNOWN;
}
}
// complaints table functions
sql::ResultSet* Connector::getAllComplaints() {
std::unique_ptr<sql::Statement> stmnt(conn->createStatement());
// sql::ResultSet *res = stmnt->executeQuery("SELECT * FROM complaints ORDER BY complaint_id");
sql::ResultSet *res = stmnt->executeQuery("SELECT "
"c.complaint_id, c.date, c.roll, h.name, h.room, h.phone, c.category, "
"c.complaint, c.status, c.remark FROM complaints as c, hostelers as h "
"where c.roll = h.roll ORDER BY c.complaint_id");
return res;
}
sqlerr::query_err Connector::addComplaint(Complaint c) {
try {
// don't enter complaint_id (auto increment), CURDATE() for todays date, FALSE for status (complaint not resolved yet)
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("INSERT INTO complaints "
"(date, roll, category, complaint, status, remark)"
" VALUE (CURDATE(), " + std::to_string(c.roll) + ", "
+ enquote(c.category) + ", " + enquote(c.complaint)
+ ", FALSE, " + enquote(c.remark) + ")"));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLSyntaxErrorException e) {
std::cerr << e.what();
std::string err(e.what());
if (err.find("PRI") != std::string::npos)
return sqlerr::DUP_PRI;
else
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::deleteComplaint(uint64_t n, std::string complaint_ids[]) {
std::string complaint_idsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
complaint_idsTuple += complaint_ids[i] + ", ";
}
complaint_idsTuple += complaint_ids[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("DELETE FROM complaints WHERE complaint_id IN " + complaint_idsTuple));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << e.what();
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::remarkComplaint(uint64_t n, std::string complaint_ids[], std::string remark) {
std::string complaint_idsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
complaint_idsTuple += complaint_ids[i] + ", ";
}
complaint_idsTuple += complaint_ids[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("UPDATE complaints SET remark = " + enquote(remark) +
"WHERE complaint_id IN " + complaint_idsTuple ));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << "\n" << e.what();
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::resolveComplaint(uint64_t n, std::string complaint_ids[]) {
std::string complaint_idsTuple = "( ";
for (uint64_t i=0; i<n-1; i++) {
complaint_idsTuple += complaint_ids[i] + ", ";
}
complaint_idsTuple += complaint_ids[n-1] + " )";
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("UPDATE complaints SET status = TRUE "
"WHERE complaint_id IN " + complaint_idsTuple ));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << "\n" << e.what();
return sqlerr::UNKNOWN;
}
}
sqlerr::query_err Connector::deleteResolvedComplaints() {
try {
stmnt = std::unique_ptr<sql::PreparedStatement> (conn->prepareStatement("DELETE FROM complaints WHERE status = TRUE"));
stmnt->executeQuery();
return sqlerr::NOERR;
} catch (sql::SQLException e) {
std::cerr << "\n" << e.what();
return sqlerr::UNKNOWN;
}
}