-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
278 lines (223 loc) · 9.09 KB
/
db.cpp
File metadata and controls
278 lines (223 loc) · 9.09 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
#include "db.h"
const QString db::DATABASE_PATH = "/database/oasis.db";
/*
* Create a new db to a existing oasis database, defined by db::DATABASE_PATH
*
* Throws:
* If the database could not be opened
*/
db::db() {
oasisDB = QSqlDatabase::addDatabase("QSQLITE");
oasisDB.setDatabaseName("oasis.db");
if (!oasisDB.open()) {
throw "Error: Database could not be opened";
}
if (!DBInit()) {
throw "Error: Database could not be initialized";
}
}
bool db::DBInit() {
oasisDB.transaction();
QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS users ( pid INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT, battery_level REAL NOT NULL, power_level INTEGER NOT NULL);");
query.exec("CREATE TABLE IF NOT EXISTS frequencies ( name TEXT NOT NULL UNIQUE PRIMARY KEY);");
query.exec("CREATE TABLE IF NOT EXISTS therapies ( name TEXT NOT NULL UNIQUE PRIMARY KEY, frequency TEXT NOT NULL REFERENCES frequencies, duration INTEGER NOT NULL);");
query.exec("CREATE TABLE IF NOT EXISTS records ( rid INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT,date TEXT NOT NULL,power_level INTEGER NOT NULL,duration INTEGER NOT NULL);");
query.exec("CREATE TABLE IF NOT EXISTS therapy_records(name TEXT NOT NULL REFERENCES therapies,tid INTEGER NOT NULL REFERENCES records(rid) ON DELETE CASCADE, PRIMARY KEY (name, tid));");
query.exec("CREATE TABLE IF NOT EXISTS frequency_records( name TEXT NOT NULL REFERENCES frequencies,fid INTEGER NOT NULL REFERENCES records(rid) ON DELETE CASCADE,PRIMARY KEY (name, fid));");
// initialize device
query.exec("INSERT OR REPLACE INTO frequencies VALUES ('10Hz'),('20Hz'),('60Hz'),('200Hz'),('7710');");
query.exec("INSERT OR REPLACE INTO therapies VALUES('PAIN', '10Hz', 900),('GYNECOLOGIC PAIN', '7710', 900),('POTENCY', '60Hz', 900),('HEAD', '60Hz', 300);");
return oasisDB.commit();
}
user* db::getUser(int id) {
oasisDB.transaction();
QSqlQuery query;
query.prepare("SELECT * FROM users WHERE pid=:pid");
query.bindValue(":pid", id);
query.exec();
if (!oasisDB.commit()) {
throw "Error: Query failed to execute";
}
// user does not exist
if (!query.next()) {
addUser(id, 100.0, 1);
user* pro = new user(id, 100, 0);
return pro;
}
// user exists
user* pro = new user(query.value(0).toInt(), query.value(1).toDouble(), query.value(2).toInt());
return pro;
}
QVector<record*> db::getRecordings() {
QSqlQuery query;
QVector<record*> qvr;
oasisDB.transaction();
query.prepare("SELECT name as treatment,date,power_level,duration FROM ( SELECT name as name,tid as rid FROM therapy_records UNION SELECT name as name,fid as rid FROM frequency_records ) NATURAL JOIN records ORDER BY rid;");
query.exec();
while (query.next()) {
QString name = query.value(0).toString();
QDateTime start = QDateTime::fromString(query.value(1).toString(), DATE_FORMAT);
int power = query.value(2).toString().toInt();
int duration = query.value(3).toString().toInt();
record* r = new record(name, start, power, duration);
qvr.push_back(r);
}
return qvr;
}
bool db::addUser(int id, double batteryLvl, int powerLvl) {
oasisDB.transaction();
QSqlQuery query;
//query.prepare("INSERT OR REPLACE INTO users (pid, battery_level, power_level) VALUES (:pid, :battery_level, :power_level);");
query.prepare("REPLACE INTO users (pid, battery_level, power_level) VALUES (:pid, :battery_level, :power_level);");
query.bindValue(":pid", id);
query.bindValue(":battery_level", batteryLvl);
query.bindValue(":power_level", powerLvl);
query.exec();
return oasisDB.commit();
}
/*
* Type: Public
* Adds a therapy record to the database, if the arguments are valid.
*
* Parameters:
* therapy - the name of the therapy to add to the records, must be capitalized and in the database already
* time - the time the therapy started in QDateTime format
* powerLevel - the maximum power level observed during a therapy
* duration - the amount of time in seconds that the therapy was in use
*
* Returns:
* True - If the record was successfully added to the database
* False - If the arguments were invalid, or if the record couldn't be added to the database
*/
bool db::addTherapyRecord(const QString& therapy, const QDateTime& time, int powerLevel, int duration) {
if (!isValidRecord("therapy", time, powerLevel, duration)) {
return false;
}
return addRecord("therapy", therapy, time, powerLevel, duration);
}
/*
* Type: Public
* Adds a frequency record to the database, if the arguments are valid.
*
* Parameters:
* frequency - the name of the frequency to add to the records, must be in the database already
* time - the time the frequency treatment started in QDateTime format
* powerLevel - the maximum power level observed during a frequency treatment
* duration - the amount of time in seconds that the frequency treatment was in use
*
* Returns:
* True - If the record was successfully added to the database
* False - If the arguments were invalid, or if the record couldn't be added to the database
*/
bool db::addFrequencyRecord(const QString& frequency, const QDateTime& time, int powerLevel, int duration) {
if (!isValidRecord("frequency", time, powerLevel, duration)) {
return false;
}
return addRecord("frequency", frequency, time, powerLevel, duration);
}
/*
* Type: Public
* Deletes all records from the database.
*
* Returns:
* True - If the records were successfully removed from the database
* False - If the records couldn't be deleted from the database
*/
bool db::deleteRecords() {
QSqlQuery query;
query.prepare("DELETE FROM records");
query.exec();
query.prepare("DELETE FROM therapy_records");
query.exec();
query.prepare("DELETE FROM frequency_records");
return query.exec();
}
QList<QString>* db::frequencies() {
QList<QString>* frequencies = new QList<QString>;
QSqlQuery query;
query.exec("SELECT * FROM FREQUENCIES");
while (query.next()) {
frequencies->append(query.value(0).toString());
}
return frequencies;
}
QList<therapy*>* db::therapies() {
/*
QList<therapy*>* therapies = new QList<therapy*>;
QSqlQuery query;
query.exec("SELECT * FROM therapies");
while (query.next()) {
therapy* t = new therapy(query.value(0).toString(), query.value(2).toInt(), query.value(1).toString());
therapies->append(t);
}
return therapies;*/
}
/*
* Type: Private
* Validates the given parameters.
*
* Parameters:
* recordType - The type of record, used only for debugging
* time - the time the treatment started in QDateTime format
* powerLevel - the maximum power level observed during a treatment
* duration - the amount of time in milliseconds that the treatment was in use
*
* Returns:
* True - If the parameters are acceptable
* False - Otherwise
*
* See Also:
* db::addTherapyrecord
* db::addFrequencyrecord
*/
bool db::isValidRecord(const QString& recordType, const QDateTime& time, int powerLevel, int duration) {
bool valid = true;
if (!time.isValid()) {
qDebug() << "Error: Cannot add " << recordType << " record, time is not valid";
valid = false;
}
else if (powerLevel < 0 || powerLevel > 100) {
qDebug() << "Error: Cannot add " << recordType << " record, power level is not valid";
valid = false;
}
else if (duration < 0) {
qDebug() << "Error: Cannot add " << recordType << " record, duration is not valid";
valid = false;
}
return valid;
}
/*
* Type: Private
* Adds a treatment record to the database.
*
* Parameters:
* tableName - The database table to insert this record into
* name - The name of the frequency or therapy
* time - the time the treatment started in QDateTime format
* powerLevel - the maximum power level observed during a treatment
* duration - the amount of time in milliseconds that the treatment was in use
*
* Returns:
* True - If the record was added successfully
* False - Otherwise
*
* See Also:
* db::addTherapyrecord
* db::addFrequencyrecord
*/
bool db::addRecord(const QString& tableName, const QString& name, const QDateTime& time, int powerLevel, int duration) {
oasisDB.transaction();
QSqlQuery query;
query.prepare("INSERT INTO records (date, power_level, duration) VALUES (:date, :power_level, :duration);");
query.bindValue(":date", time.toString(DATE_FORMAT));
query.bindValue(":power_level", powerLevel);
query.bindValue(":duration", duration);
query.exec();
int rowid = query.lastInsertId().toInt();
query.prepare("INSERT INTO " + tableName + "_records VALUES (:name, :id);");
query.bindValue(":name", name);
query.bindValue(":id", rowid);
query.exec();
return oasisDB.commit();
}