-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqaccelerator-db.h
681 lines (603 loc) · 19.3 KB
/
qaccelerator-db.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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// TODO(ogaro): Validate!
// TODO(ogaro): Handle locked tables gracefully. Ony one instance of the
// application should be allowed to exist. Enable creation of more than a single
// session (safely).
// TODO(ogaro): For each model, and for each field, have structs that store
// the most recent value (if that value is null, read from db; when writing to
// db, set that value too). Use these to keep track of the fields of the most
// recently read/written N rows.
// TODO(ogaro): All accessor methods should be const. Cast if necessary.
#ifndef QACCELERATOR_DB_H_
#define QACCELERATOR_DB_H_
#include "speed-grapher.h"
#include "qaccelerator-utils.h"
#include <unordered_map>
#include <iostream>
#include <string>
#include <QtSql/QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <memory>
#include <QString>
#include <QMap>
#include <vector>
#include <QDir>
#include <QDebug>
// Caller must make sure session does not go out of scope while models
// depending on it are still existent.
class Session {
public:
Session();
bool Exec(const QString& query);
QSqlQuery& GetQuery() {
return *query_;
}
private:
void CreateDownloadItemsTable();
void CreatePreferencesTable();
void CreateTable(const QString& table_name,
const QMap<QString, QString>& types,
const QMap<QString, QString>& extra_defs);
QSqlDatabase db_;
std::unique_ptr<QSqlQuery> query_;
};
template <typename T>
class Model {
public:
static const QString& TableName() {
return table_name_;
}
static const QMap<QString, QString>& Types() {
return types_;
}
static const QMap<QString, QString>& ExtraDefs() {
return extra_defs_;
}
static Nullable<T> AddNew(
const QMap<QString, QVariant>& data,
Session* session) {
QStringList fields, values;
QMapIterator<QString, QVariant> it(data);
while (it.hasNext()) {
it.next();
fields.append(it.key());
values.append(Prepare(it.key(), it.value()));
}
QString query = QString("INSERT INTO %1 (%2) VALUES (%3)")
.arg(table_name_)
.arg(fields.join(","))
.arg(values.join(","));
session->Exec(query);
QVariant id = session->GetQuery().lastInsertId();
if (id.isValid()) {
return T(session, id.toInt());
} else {
qDebug() << "Database INSERT operation did not return a row number.";
return Nullable<T>();
}
}
static Nullable<T> Get(Session* session, int id) {
QString query = QString("SELECT id FROM %1 WHERE id = %2")
.arg(TableName())
.arg(id);
session->Exec(query);
if (session->GetQuery().next()) {
return T(session, session->GetQuery().value(0).toInt());
} else {
return Nullable<T>();
}
}
static Nullable<T> Get(Session* session,
const QString& field,
const QVariant& value) {
QString query = QString("SELECT id FROM %1 WHERE %2 = %3")
.arg(TableName())
.arg(field)
.arg(Prepare(field, value));
session->Exec(query);
if (session->GetQuery().next()) {
int id = session->GetQuery().value(0).toInt();
// Positions the query on the last record so that no next() call after this
// returns results for this SELECT query.
session->GetQuery().last();
return T(session, id);
} else {
return Nullable<T>();
}
}
static void GetAll(Session* session,
const QString& field,
const QVariant& value,
std::vector<T>* models) {
QString query = QString("SELECT id FROM %1 WHERE %2 = %3")
.arg(TableName())
.arg(field)
.arg(Prepare(field, value));
session->Exec(query);
while (session->GetQuery().next()) {
int id = session->GetQuery().value(0).toInt();
models->push_back(T(session, id));
}
}
static void GetAll(Session* session, std::vector<T>* models) {
QString query = QString("SELECT id FROM %1")
.arg(TableName());
session->Exec(query);
while (session->GetQuery().next()) {
int id = session->GetQuery().value(0).toInt();
models->push_back(T(session, id));
}
}
static int Count(Session* session) {
QString query = QString("SELECT COUNT(*) FROM %1").arg(table_name_);
session->Exec(query);
if (session->GetQuery().next()) {
return session->GetQuery().value(0).toInt();
} else {
return -1;
}
}
bool Delete() {
QString query = QString("DELETE FROM %1 WHERE id = %2")
.arg(TableName())
.arg(id_);
return session_->Exec(query);
}
int Id() const {
return id_;
}
bool IsValid() {
return session_ != nullptr && id_ >= 0;
}
protected:
Model(Session* session, int id) : session_(session), id_(id) {}
static const QString& GetType(const QString& field) {
// TODO(ogaro): Validate!
return types_.find(field).value();
}
static void GetFields(QStringList* fields) {
QMap<QString, QString>::const_iterator it = types_.constBegin();
while(it != types_.constEnd()) {
fields->append(it.key());
}
}
static QString Prepare(const QString& field, const QVariant& value) {
if (GetType(field) == "VARCHAR") {
return QString("'%1'").arg(value.toString());
} else {
return value.toString();
}
}
Nullable<QVariant> GetField(const QString& field) {
QString query = QString("SELECT %1 FROM %2 WHERE id = %4")
.arg(field)
.arg(TableName())
.arg(id_);
if (session_ == nullptr) {
qDebug() << "Session is null.";
}
session_->Exec(query);
if (session_->GetQuery().next()) {
return session_->GetQuery().value(0);
} else {
return Nullable<QVariant>();
}
}
bool SetField(const QString& field, const QVariant& val) {
QString query = QString("UPDATE %1 SET %2 = %3 WHERE id = %4")
.arg(TableName())
.arg(field)
.arg(Prepare(field, val))
.arg(id_);
return session_->Exec(query);
}
Session* session_;
int id_;
static const QString table_name_;
static const QMap<QString, QString> types_;
static const QMap<QString, QString> extra_defs_;
};
// TODO(ogaro): Category field is not needed.
// TODO(ogaro): Validate the field names!
class DownloadItem : public Model<DownloadItem> {
public:
enum class StatusEnum {
IN_PROGRESS = 0,
PAUSED = 1,
COMPLETED = 2,
CANCELLED = 3,
FAILED = 4,
QUEUED = 5
};
static StatusEnum MakeStatus(int enum_id) {
switch(enum_id) {
case 0:
return StatusEnum::IN_PROGRESS;
case 1:
return StatusEnum::PAUSED;
case 2:
return StatusEnum::COMPLETED;
case 3:
return StatusEnum::CANCELLED;
case 4:
return StatusEnum::FAILED;
case 5:
return StatusEnum::QUEUED;
default:
std::cout << "Invalid status id: " << enum_id << std::endl;
exit(-1);
}
}
static int ToInt(const StatusEnum& status) {
switch(status) {
case StatusEnum::IN_PROGRESS:
return 0;
case StatusEnum::PAUSED:
return 1;
case StatusEnum::COMPLETED:
return 2;
case StatusEnum::CANCELLED:
return 3;
case StatusEnum::FAILED:
return 4;
case StatusEnum::QUEUED:
return 5;
default:
// Control should never reach here!
std::cout << "Unrecognized status." << std::endl;
exit(-1);
}
}
static QString ToString(const StatusEnum& status) {
switch(status) {
case StatusEnum::IN_PROGRESS:
return "In Progress";
case StatusEnum::PAUSED:
return "Paused";
case StatusEnum::COMPLETED:
return "Completed";
case StatusEnum::CANCELLED:
return "Cancelled";
case StatusEnum::FAILED:
return "Failed";
case StatusEnum::QUEUED:
return "Queued";
default:
// Control should never reach here!
std::cout << "Unrecognized status." << std::endl;
exit(-1);
}
}
DownloadItem(Session* session, int id)
: Model<DownloadItem>::Model(session, id) {}
DownloadItem() : Model<DownloadItem>::Model(nullptr, -1) {
// TODO(ogaro): Should we be able to place these objects in containers?
}
// Getters
Nullable<QString> Url() {
Nullable<QVariant> value = GetField("url");
if (value.IsNull()) {
return Nullable<QString>();
}
return value.Get().toString();
}
Nullable<QString> SaveAs() {
Nullable<QVariant> value = GetField("save_as");
if (value.IsNull()) {
return Nullable<QString>();
}
return value.Get().toString();
}
Nullable<double> Progress() {
Nullable<QVariant> value = GetField("progress");
if (value.IsNull()) {
return Nullable<double>();
}
return value.Get().toDouble();
}
Nullable<qint64> StartTime() {
Nullable<QVariant> value = GetField("start_time");
if (value.IsNull()) {
return Nullable<qint64>();
}
return value.Get().toLongLong();
}
Nullable<int> NumConnections() {
Nullable<QVariant> value = GetField("num_connections");
if (value.IsNull()) {
return Nullable<int>();
}
return value.Get().toInt();
}
Nullable<int> ChunkSize() {
Nullable<QVariant> value = GetField("chunk_size");
if (value.IsNull()) {
return Nullable<int>();
}
return value.Get().toInt();
}
Nullable<qint64> FileSize() {
Nullable<QVariant> value = GetField("file_size");
if (value.IsNull()) {
return Nullable<qint64>();
}
return value.Get().toLongLong();
}
Nullable<QString> WorkDir() {
Nullable<QVariant> value = GetField("work_dir");
if (value.IsNull()) {
return Nullable<QString>();
}
return value.Get().toString();
}
Nullable<StatusEnum> Status() {
Nullable<QVariant> value = GetField("status");
if (value.IsNull()) {
return Nullable<StatusEnum>();
}
return MakeStatus(value.Get().toInt());
}
Nullable<qint64> MillisElapsed() {
Nullable<QVariant> value = GetField("millis_elapsed");
if (value.IsNull()) {
return Nullable<qint64>();
}
return value.Get().toLongLong();
}
// Setters
void SetUrl(const QString& url) {
SetField("url", url);
}
void SetSaveAs(const QString& save_as) {
SetField("save_as", save_as);
}
void SetProgress(double progress) {
SetField("progress", progress);
}
void SetStartTime(qint64 start_time) {
SetField("start_time", start_time);
}
void SetNumConnections(int num_connections) {
SetField("num_connections", num_connections);
}
void SetChunkSize(int chunk_size) {
SetField("chunk_size", chunk_size);
}
void SetFileSize(qint64 file_size) {
SetField("file_size", file_size);
}
void SetWorkDir(const QString& work_dir) {
SetField("work_dir", work_dir);
}
void SetStatus(const StatusEnum& status) {
SetField("status", ToInt(status));
}
void SetMillisElapsed(qint64 millis_elapsed) {
SetField("millis_elapsed", millis_elapsed);
}
};
class Preference : public Model<Preference> {
public:
Preference(Session* session, int id)
: Model<Preference>::Model(session, id) {}
Nullable<QString> Name() {
Nullable<QVariant> value = GetField("name");
if (value.IsNull()) {
return Nullable<QString>();
}
return value.Get().toString();
}
Nullable<QString> Value() {
Nullable<QVariant> value = GetField("value");
if (value.IsNull()) {
return Nullable<QString>();
}
return value.Get().toString();
}
void SetName(const QString& name) {
SetField("name", name);
}
void SetValue(const QString& value) {
SetField("value", value);
}
};
// Use mutexes in every public function.
class PreferenceManager {
public:
PreferenceManager(Session* session) : session_(session) {
QString download_dir = QDir(QDir::homePath()).absoluteFilePath("Downloads");
defaults_ = {
{"in_progress_background_brush_color", "#fff"},
{"in_progress_background_brush_alpha", 1.0},
{"in_progress_grid_pen_color", "#ccc"},
{"in_progress_grid_pen_alpha", 0.15},
{"in_progress_grid_pen_width", 0.0},
{"in_progress_curve_pen_color", "#060"},
{"in_progress_curve_pen_alpha", 0.8},
{"in_progress_curve_pen_width", 0.0},
{"in_progress_curve_brush_color", "#0f0"},
{"in_progress_curve_brush_alpha", 0.5},
{"in_progress_chart_brush_color", "#0f0"},
{"in_progress_chart_brush_alpha", 0.33},
{"in_progress_indicator_dot_pen_color", "#ccc"},
{"in_progress_indicator_dot_pen_alpha", 1.0},
{"in_progress_indicator_dot_pen_width", 0.0},
{"in_progress_indicator_dot_brush_color", "#0f0"},
{"in_progress_indicator_dot_brush_alpha", 0.5},
{"in_progress_indicator_dot_size", 8.0},
{"in_progress_indicator_line_pen_color", "#ccc"},
{"in_progress_indicator_line_pen_alpha", 1.0},
{"in_progress_indicator_line_pen_width", 0.0},
{"in_progress_indicator_text_font_name", "Verdana"},
{"in_progress_indicator_text_font_size", 10.0},
{"in_progress_indicator_text_color", "#ccc"},
{"in_progress_indicator_text_alpha", 1.0},
{"paused_background_brush_color", "#fff"},
{"paused_background_brush_alpha", 1.0},
{"paused_grid_pen_color", "#ccc"},
{"paused_grid_pen_alpha", 0.15},
{"paused_grid_pen_width", 0.0},
{"paused_curve_pen_color", "#f06"},
{"paused_curve_pen_alpha", 0.8},
{"paused_curve_pen_width", 0.0},
{"paused_curve_brush_color", "#f06"},
{"paused_curve_brush_alpha", 0.5},
{"paused_chart_brush_color", "#f06"},
{"paused_chart_brush_alpha", 0.33},
{"paused_indicator_dot_pen_color", "#ccc"},
{"paused_indicator_dot_pen_alpha", 1.0},
{"paused_indicator_dot_pen_width", 0.0},
{"paused_indicator_dot_brush_color", "#f06"},
{"paused_indicator_dot_brush_alpha", 0.5},
{"paused_indicator_dot_size", 8.0},
{"paused_indicator_line_pen_color", "#ccc"},
{"paused_indicator_line_pen_alpha", 1.0},
{"paused_indicator_line_pen_width", 0.0},
{"paused_indicator_text_font_name", "Verdana"},
{"paused_indicator_text_font_size", 10.0},
{"paused_indicator_text_color", "#ccc"},
{"paused_indicator_text_alpha", 1.0},
{"completed_background_brush_color", "#fff"},
{"completed_background_brush_alpha", 1.0},
{"completed_grid_pen_color", "#ccc"},
{"completed_grid_pen_alpha", 0.15},
{"completed_grid_pen_width", 0.0},
{"completed_curve_pen_color", "#777"},
{"completed_curve_pen_alpha", 0.8},
{"completed_curve_pen_width", 0.0},
{"completed_curve_brush_color", "#777"},
{"completed_curve_brush_alpha", 0.5},
{"completed_chart_brush_color", "#777"},
{"completed_chart_brush_alpha", 0.33},
{"download_dir", download_dir},
{"num_connections", 10},
{"concurrent_cap", 2},
{"multiple_filters", 0}
};
if (Preference::Count(session_) > 0) {
return;
}
QMapIterator<QString, QVariant> it(defaults_);
while (it.hasNext()) {
it.next();
Set(it.key(), it.value());
}
}
void Set(const QString& preference, const QVariant& value) {
EnsureValid(preference);
Nullable<Preference> db_row = Preference::Get(
session_,
"name",
preference);
if (db_row.IsNull()) {
Preference::AddNew({{"name", preference}, {"value", value}}, session_);
} else {
db_row.Get().SetValue(value.toString());
}
}
void SetDefault(const QString& preference) {
QVariant default_value = GetDefault(preference);
Set(preference, default_value);
}
void GetDefault(const QString& preference, QString* read_value) {
const QVariant* value = nullptr;
GetDefaultOrDie(preference, &value);
*read_value = value->toString();
}
void GetDefault(const QString& preference, double* read_value) {
const QVariant* value = nullptr;
GetDefaultOrDie(preference, &value);
*read_value = value->toDouble();
}
void GetDefault(const QString& preference, int* read_value) {
const QVariant* value = nullptr;
GetDefaultOrDie(preference, &value);
*read_value = value->toInt();
}
// TODO(ogaro): Repeating db_row.Get().Value() is not great style.
// TODO(ogaro): Die in a more transparent manner (print message that says
// preference "%s" does not exist.
void Get(const QString& preference, QString* read_value) {
EnsureValid(preference);
Nullable<Preference> db_row = Preference::Get(session_,
"name",
preference);
db_row.Get().Value(); // Will die if value is not in database.
*read_value = db_row.Get().Value().Get();
}
void Get(const QString& preference, double* read_value) {
EnsureValid(preference);
Nullable<Preference> db_row = Preference::Get(session_,
"name",
preference);
db_row.Get().Value(); // Will die if value is not in database.
*read_value = QVariant(db_row.Get().Value().Get()).toDouble();
}
void Get(const QString& preference, int* read_value) {
EnsureValid(preference);
Nullable<Preference> db_row = Preference::Get(session_,
"name",
preference);
db_row.Get().Value(); // Will die if value is not in database.
*read_value = QVariant(db_row.Get().Value().Get()).toInt();
}
void Get(const QString& preference, bool* read_value) {
QVariant value;
Get(preference, &value);
*read_value = value.toBool();
}
void Get(const QString& preference, QVariant* read_value) {
EnsureValid(preference);
Nullable<Preference> db_row = Preference::Get(session_,
"name",
preference);
db_row.Get().Value(); // Will die if value is not in database.
*read_value = QVariant(db_row.Get().Value().Get());
}
void ApplySavedPreferences(SpeedGrapherState state,
SpeedGrapher* speed_grapher,
bool show_indicators) {
QString str_state = ToString(state);
QMapIterator<QString, QVariant> it(defaults_);
while (it.hasNext()) {
it.next();
if (it.key().startsWith(str_state)) {
QString key(it.key());
key = key.replace(str_state, "");
key = key.mid(1);
QVariant value;
Get(it.key(), &value);
speed_grapher->SetStyleAttribute(key, value);
}
}
speed_grapher->UpdatePlot(show_indicators);
}
// TODO(ogaro): This is a lazy way of getting the Session in the download
// manager.
Session* GetSession() {
return session_;
}
private:
const QVariant& GetDefault(const QString& preference) {
const QVariant* value;
GetDefaultOrDie(preference, &value);
return *value;
}
void GetDefaultOrDie(const QString& preference, const QVariant** read_value) {
auto it = defaults_.find(preference);
if (it != defaults_.end()) {
*read_value = &(it.value());
} else {
qDebug() << "Preference '" << preference << "' not recognized.";
exit(-1);
}
}
void EnsureValid(const QString& preference) {
const QVariant* value;
GetDefaultOrDie(preference, &value);
}
QMap<QString, QVariant> defaults_;
Session* session_;
};
#endif // QACCELERATOR_DB_H_