forked from Flamesnort/Verklegt-verkefni-1
/
dbmanager.cpp
480 lines (401 loc) · 12.4 KB
/
dbmanager.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "dbmanager.h"
//========CONSTRUCTORS==========
DbManager::DbManager()
{
QFileInfo checkFile(QCoreApplication::applicationDirPath() + "\\data.db");
bool db_exists = checkFile.exists();
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(QCoreApplication::applicationDirPath() + "\\data.db");
if(!db_exists)
{
createTables();
}
execQuery("SELECT * FROM *"); //for some reason you have to display the list twice unless you do this
}
DbManager::~DbManager()
{
//Intentionally left empty
}
//========PUBLIC FUNCTIONS==========
QVector<Person> DbManager::getAllPersons(QString order_by, QString view_gender)
{
if(view_gender != "BOTH")
{
return findPersons("WHERE gender LIKE '" + view_gender + "' " +
"ORDER BY " + ascOrDesc(order_by));
}
else
{
return findPersons("ORDER BY " + ascOrDesc(order_by));
}
}
QVector<Computer> DbManager::getAllComputers(QString order_by)
{
return findComputers("ORDER BY " + ascOrDesc(order_by));
}
QVector<Person> DbManager::searchPersons(QString search_type, QString search_query, QString order_by, QString view_gender)
{
if(view_gender != "BOTH")
{
return findPersons("WHERE " + search_type + " COLLATE UTF8_GENERAL_CI LIKE '%" + search_query + "%' "
"AND gender LIKE '" + view_gender + "' " +
"ORDER BY " + ascOrDesc(order_by));
}
else
{
return findPersons("WHERE " + search_type + " COLLATE UTF8_GENERAL_CI LIKE '%" + search_query + "%' "
"ORDER BY " + ascOrDesc(order_by));
}
}
QVector<Computer> DbManager::searchComputers(QString search_type, QString search_query, QString order_by)
{
return findComputers("WHERE " + search_type + " COLLATE UTF8_GENERAL_CI LIKE '%" + search_query + "%' "
"ORDER BY " + ascOrDesc(order_by));
}
ComputerXPersons DbManager::getComputerXPersons(QString cid)
{
db.open();
QSqlQuery qry;
QVector<Person> pers;
QVector<QString> pids;
ComputerXPersons cxp;
qry.exec("PRAGMA foreign_keys=ON");
qry.exec("SELECT pID "
"FROM ComputersXPersons "
"WHERE cID = " + cid);
int i_pid = qry.record().indexOf("pID");
while(qry.next())
{
pids.push_back(qry.value(i_pid).toString());
}
for(int i = 0; i < pids.size(); i++)
{
pers.push_back(findPersons("WHERE pID = " + pids[i]).first());
}
db.close();
cxp.setComputer(findComputers("WHERE cID = " + cid).first());
cxp.setPersons(pers);
return cxp;
}
PersonXComputers DbManager::getPersonXComputers(QString pid)
{
db.open();
QSqlQuery qry;
QVector<Computer> comps;
QVector<QString> cids;
PersonXComputers pxc;
qry.exec("PRAGMA foreign_keys=ON");
qry.exec("SELECT cID "
"FROM ComputersXPersons "
"WHERE pID = " + pid);
int i_cid = qry.record().indexOf("cID");
while(qry.next())
{
cids.push_back(qry.value(i_cid).toString());
}
for(int i = 0; i < cids.size(); i++)
{
comps.push_back(findComputers("WHERE cID = " + cids[i]).first());
}
db.close();
pxc.setPerson(findPersons("WHERE pID = " + pid).first());
pxc.setComputers(comps);
return pxc;
}
bool DbManager::addPerson(Person pers)
{
QString dob_iso = toISO(pers.getDoB());
QString dod_iso = toISO(pers.getDoD());
return execQuery("INSERT INTO Persons VALUES ( "
"NULL, '" +
pers.getName() + "','" +
pers.getGender() + "','" +
dob_iso + "','" +
dod_iso + "')");
}
bool DbManager::addComputer(Computer comp)
{
QString was_built = "";
switch (comp.getBuilt())
{
case 0:
was_built = "0";
break;
case 1:
was_built = "1";
break;
}
return execQuery("INSERT INTO Computers (name, year, type, built) "
"VALUES ( '" +
comp.getName() + "'," +
comp.getYear() + ",'" +
comp.getType() + "', " +
was_built + " )");
}
bool DbManager::addComputerXPerson(QString cid, QString pid)
{
bool executed = false;
db.open();
QSqlQuery qry;
executed = qry.exec("INSERT INTO ComputersXPersons (pID, cID) "
"VALUES (" +
pid + "," +
cid + ")");
db.close();
return executed;
}
bool DbManager::deletePerson(QString pid)
{
bool executed = false;
db.open();
QSqlQuery qry;
executed = qry.exec("DELETE FROM ComputersXPersons "
"WHERE pID = " + pid);
if(!executed)
{
db.close();
return executed;
}
executed = qry.exec("DELETE FROM Persons "
"WHERE pID = " + pid);
db.close();
return executed;
}
bool DbManager::deleteComputer(QString cid)
{
bool executed = false;
db.open();
QSqlQuery qry;
executed = qry.exec("DELETE FROM ComputersXPersons "
"WHERE cID = " + cid);
if(!executed)
{
db.close();
return executed;
}
executed = qry.exec("DELETE FROM Computers "
"WHERE cID = " + cid);
db.close();
return executed;
}
bool DbManager::deleteComputerXPerson(QString cid, QString pid)
{
bool executed = false;
db.open();
QSqlQuery qry;
executed = qry.exec("DELETE FROM ComputersXPersons "
"WHERE pID = " + pid + " " +
"AND cID = " + cid);
db.close();
return executed;
}
bool DbManager::editPerson(Person pers)
{
QString dob_iso = toISO(pers.getDoB());
QString dod_iso = toISO(pers.getDoD());
return execQuery("UPDATE Persons "
"SET name = '" + pers.getName() + "', "
"gender = '" + pers.getGender() + "', "
"dob = '" + dob_iso + "', "
"dod = '" + dod_iso + "' "
"WHERE pID = " + pers.getId());
}
bool DbManager::editComputer(Computer comp)
{
QString was_built = "";
switch (comp.getBuilt())
{
case 0:
was_built = "0";
break;
case 1:
was_built = "1";
break;
}
return execQuery("UPDATE Computers "
"SET name = '" + comp.getName() + "', "
"year = " + comp.getYear() + ", "
"type = '" + comp.getType() + "', "
"built = " + was_built + " "
"WHERE cID = " + comp.getId());
}
Person DbManager::getPerson(QString pid)
{
db.open();
Person temp;
QSqlQuery qry;
qry.exec("PRAGMA foreign_keys=ON");
qry.exec("SELECT pID, name, gender, dob, dod "
"FROM Persons "
"WHERE pID = " + pid);
int i_id = qry.record().indexOf("pID");
int i_name = qry.record().indexOf("name");
int i_gender = qry.record().indexOf("gender");
int i_dob = qry.record().indexOf("dob");
int i_dod = qry.record().indexOf("dod");
while(qry.next())
{
temp.setId(qry.value(i_id).toString().toStdString());
temp.setName(qry.value(i_name).toString().toStdString());
temp.setGender(qry.value(i_gender).toString().toStdString());
temp.setDoB(fromISO(qry.value(i_dob).toString()).toStdString());
temp.setDoD(fromISO(qry.value(i_dod).toString()).toStdString());
}
db.close();
return temp;
}
Computer DbManager::getComputer(QString cid)
{
db.open();
Computer temp;
QSqlQuery qry;
qry.exec("PRAGMA foreign_keys=ON");
qry.exec("SELECT cID, name, year, type, built "
"FROM Computers "
"WHERE cID = " + cid);
int i_id = qry.record().indexOf("cID");
int i_name = qry.record().indexOf("name");
int i_year = qry.record().indexOf("year");
int i_type = qry.record().indexOf("type");
int i_built = qry.record().indexOf("built");
while(qry.next())
{
temp.setId(qry.value(i_id).toString().toStdString());
temp.setName(qry.value(i_name).toString().toStdString());
temp.setYear(qry.value(i_year).toString().toStdString());
temp.setType(qry.value(i_type).toString().toStdString());
temp.setBuilt(stoi(qry.value(i_built).toString().toStdString())); //stoi to change to bool
}
db.close();
return temp;
}
//========PRIVATE FUNCTIONS==========
bool DbManager::execQuery(QString query_string)
{
bool executed = false;
db.open();
QSqlQuery qry;
qry.exec("PRAGMA foreign_keys = ON");
executed = qry.exec(query_string);
db.close();
return executed;
}
void DbManager::createTables()
{
execQuery("CREATE TABLE Persons ( "
"pID INTEGER PRIMARY KEY AUTOINCREMENT, " //we don't have to worry about ID with autoincrement
"name VARCHAR[40], "
"gender VARCHAR[7], " //this might be changed to char
"dob DATE, "
"dod DATE, "
"UNIQUE (name) )");
execQuery("CREATE TABLE Computers ( "
"cID INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR[40], "
"year INT, "
"type VARCHAR[20], "
"built BOOLEAN, "
"UNIQUE (name) )");
execQuery("CREATE TABLE ComputersXPersons ( "
"pID INT, "
"cID INT, "
"FOREIGN KEY (pID) REFERENCES Persons(pID), "
"FOREIGN KEY (cID) REFERENCES Computers(cID), "
"PRIMARY KEY(pID, cID))");
}
QVector<Person> DbManager::findPersons(QString conditions)
{
db.open();
Person temp;
QVector<Person> results;
QSqlQuery qry;
qry.exec("PRAGMA foreign_keys=ON");
qry.exec("SELECT pID, name, gender, dob, dod "
"FROM Persons "
+ conditions);
int i_id = qry.record().indexOf("pID");
int i_name = qry.record().indexOf("name");
int i_gender = qry.record().indexOf("gender");
int i_dob = qry.record().indexOf("dob");
int i_dod = qry.record().indexOf("dod");
while(qry.next())
{
temp.setId(qry.value(i_id).toString().toStdString());
temp.setName(qry.value(i_name).toString().toStdString());
temp.setGender(qry.value(i_gender).toString().toStdString());
temp.setDoB(fromISO(qry.value(i_dob).toString()).toStdString());
temp.setDoD(fromISO(qry.value(i_dod).toString()).toStdString());
results.push_back(temp);
}
db.close();
return results;
}
QVector<Computer> DbManager::findComputers(QString conditions)
{
db.open();
Computer temp;
QVector<Computer> results;
QSqlQuery qry;
qry.exec("PRAGMA foreign_keys=ON");
qry.exec("SELECT cID, name, year, type, built "
"FROM Computers "
+ conditions);
int i_id = qry.record().indexOf("cID");
int i_name = qry.record().indexOf("name");
int i_year = qry.record().indexOf("year");
int i_type = qry.record().indexOf("type");
int i_built = qry.record().indexOf("built");
while(qry.next())
{
temp.setId(qry.value(i_id).toString().toStdString());
temp.setName(qry.value(i_name).toString().toStdString());
temp.setYear(qry.value(i_year).toString().toStdString());
temp.setType(qry.value(i_type).toString().toStdString());
temp.setBuilt(stoi(qry.value(i_built).toString().toStdString())); //stoi to change to bool
results.push_back(temp);
}
db.close();
return results;
}
QString DbManager::ascOrDesc(QString order_by)
{
if(order_by.toStdString().find("_R") <= order_by.size())
{
QString n_order_by = "";
for(int i = 0; i < order_by.size(); i++)
{
if(order_by[i] == '_')
{
break;
}
n_order_by += order_by[i];
}
return n_order_by + " DESC";
}
else
{
return order_by + " ASC";
}
}
QString DbManager::toISO(QString date)
{
if(date == "alive")
{
return "alive";
}
QString year = date.mid(6,4);
QString month = date.mid(3,2);
QString day = date.mid(0,2);
return year + '-' + month + '-' + day;
}
QString DbManager::fromISO(QString date)
{
if(date == "alive")
{
return "alive";
}
QString year = date.mid(0,4);
QString month = date.mid(5,2);
QString day = date.mid(8,2);
return day + '/' + month + '/' + year;
}