-
Notifications
You must be signed in to change notification settings - Fork 2
/
bugmanager.cpp
813 lines (686 loc) · 28 KB
/
bugmanager.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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#include <QApplication>
#include <QBoxLayout>
#include <QDebug>
#include <QDir>
#include <QCheckBox>
#include <QComboBox>
#include <QMessageBox>
#include "bugmanager.h"
#include "db/sqlhelpers.h"
#define CREATE_TABLE(table, columns) \
res = createTable(table, columns); \
if (!res.isEmpty()) { \
__db.rollback(); \
return res; }
#define INSERT_DICT_VALUE(table, id, value) \
res = insertDictValue(table, id, value); \
if (!res.isEmpty()) { \
__db.rollback(); \
return res; }
QSqlDatabase __db;
void BugManager::closeDatabase()
{
BugManager::closeDictionaries();
QString connection = __db.connectionName();
__db.close();
__db = QSqlDatabase();
QSqlDatabase::removeDatabase(connection);
}
QString BugManager::openDatabase(const QString &fileName)
{
closeDatabase();
__db = QSqlDatabase::addDatabase("QSQLITE");
__db.setDatabaseName(fileName);
if (!__db.open())
return qApp->tr("Unable to establish a database connection.\n\n%1")
.arg(SqlHelper::errorText(__db.lastError()));
QSqlQuery query;
if (!query.exec("PRAGMA foreign_keys = ON;"))
return qApp->tr("Failed to enable foreign keys.\n\n%1").arg(SqlHelper::errorText(query));
QString res;
if (!__db.transaction())
return qApp->tr("Unable to begin transaction to create database structure.\n\n%1")
.arg(SqlHelper::errorText(__db.lastError()));
CREATE_TABLE(TABLE_CATEGORY, "Id integer primary key, Title varchar");
CREATE_TABLE(TABLE_SEVERITY, "Id integer primary key, Title varchar");
CREATE_TABLE(TABLE_PRIORITY, "Id integer primary key, Title varchar");
CREATE_TABLE(TABLE_STATUS, "Id integer primary key, Title varchar");
CREATE_TABLE(TABLE_SOLUTION, "Id integer primary key, Title varchar");
CREATE_TABLE(TABLE_REPEAT, "Id integer primary key, Title varchar");
CREATE_TABLE(TABLE_BUGS, "Id integer primary key, " // 0
"Summary varchar, " // 1
"Extra varchar, " // 2
"Category integer not null references Category(Id), " // 3
"Severity integer not null references Severity(Id), " // 4
"Priority integer not null references Priority(Id), " // 5
"Repeat integer not null references Repeatability(Id), " // 6
"Status integer not null references Status(Id), " // 7
"Solution integer not null references Solution(Id), " // 8
"Created datetime not null, " // 9
"Updated datetime not null"); // 10
CREATE_TABLE(TABLE_HISTORY, "Issue integer not null references Issue(Id) on delete cascade, "
"EventNum integer not null, "
"EventPart integer not null, "
"ChangedParam integer not null default -1, "
"OldValue, "
"NewValue, "
"Comment varchar, "
"Moment datetime not null, "
"primary key (Issue, EventNum, EventPart)");
CREATE_TABLE(TABLE_RELATIONS, "Id1 integer not null references Issue(Id) on delete cascade, "
"Id2 integer not null references Issue(Id) on delete cascade, "
"Created datetime not null, "
"primary key (Id1, Id2)");
CREATE_TABLE(TABLE_SETTINGS, "Name, Value");
__db.commit();
loadDictionaries();
return res;
}
QString BugManager::newDatabase(const QString &fileName)
{
if (QFileInfo(fileName) == QFileInfo(currentFile()))
closeDatabase();
if (QFile::exists(fileName))
if (!QFile::remove(fileName))
return qApp->tr("Unable to overwrite existing file. Probably file is locked.");
QString res = openDatabase(fileName);
if (!res.isEmpty())
return res;
if (!__db.transaction())
return qApp->tr("Unable to begin transaction to insert default dictionary values.\n\n%1")
.arg(SqlHelper::errorText(__db.lastError()));
INSERT_DICT_VALUE(TABLE_CATEGORY, 0, "<none>");
INSERT_DICT_VALUE(TABLE_CATEGORY, 100, "GUI");
INSERT_DICT_VALUE(TABLE_CATEGORY, 200, "Input");
INSERT_DICT_VALUE(TABLE_CATEGORY, 300, "Output");
INSERT_DICT_VALUE(TABLE_CATEGORY, 400, "Processing");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_TODO, "Todo");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_ENHANCE, "Enhancement");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_TEXT, "Text");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_TRIVIAL, "Trivial");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_ERROR, "Error");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_BLUNDER, "Blunder");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_CRUSH, "Crush");
INSERT_DICT_VALUE(TABLE_SEVERITY, SEVERITY_BLOCKER, "Blocker");
INSERT_DICT_VALUE(TABLE_PRIORITY, PRIORITY_MIN, "Minimal");
INSERT_DICT_VALUE(TABLE_PRIORITY, PRIORITY_LOW, "Low");
INSERT_DICT_VALUE(TABLE_PRIORITY, PRIORITY_NORMAL, "Normal");
INSERT_DICT_VALUE(TABLE_PRIORITY, PRIORITY_HIGH, "High");
INSERT_DICT_VALUE(TABLE_PRIORITY, PRIORITY_URGENT, "Urgent");
INSERT_DICT_VALUE(TABLE_STATUS, STATUS_OPENED, "Opened");
INSERT_DICT_VALUE(TABLE_STATUS, STATUS_SOLVED, "Solved");
INSERT_DICT_VALUE(TABLE_STATUS, STATUS_CLOSED, "Closed");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_NONE, "None");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_FIXED, "Fixed");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_UNREPEAT, "Unrepeatable");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_IRRECOVER, "Irrecoverable");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_DUPLICATE, "Duplicate");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_REJECTED, "Rejected");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_SUSPENDED, "Suspended");
INSERT_DICT_VALUE(TABLE_SOLUTION, SOLUTION_ABANDONED, "Abandoned");
INSERT_DICT_VALUE(TABLE_REPEAT, REPEAT_ALWAYS, "Always");
INSERT_DICT_VALUE(TABLE_REPEAT, REPEAT_SOMETIMES, "Sometimes");
INSERT_DICT_VALUE(TABLE_REPEAT, REPEAT_UNKNOWN, "Unknown");
__db.commit();
loadDictionaries();
return res;
}
QString BugManager::currentFile()
{
return __db.databaseName();
}
QString BugManager::createTable(const QString &name, const QString &columns)
{
QSqlQuery query;
if (!query.exec(QString("create table if not exists %1 (%2)").arg(name).arg(columns)))
return qApp->tr("Unable to create table %1.\n\n%2").arg(name.toUpper()).arg(SqlHelper::errorText(query));
return "";
}
QString BugManager::insertDictValue(const QString &table, int id, const QString &value)
{
QSqlQuery query;
if (!query.exec(QString("insert into %1 values (%2, '%3')").arg(table).arg(id).arg(value)))
return qApp->tr("Unable to insert value %3 (%4) into dictionary %1.\n\n%2")
.arg(QString(table).toUpper())
.arg(SqlHelper::errorText(query))
.arg(id)
.arg(value);
return "";
}
QVariant BugManager::generateBugId()
{
QSqlQuery query;
if (!query.exec(QString("select max(Id) from %1").arg(TABLE_BUGS)))
return SqlHelper::errorText(query);
if (query.isSelect() && query.first())
return query.record().value(0).toInt() + 1;
return 1;
}
QVariant BugManager::generateEventId(int bugId)
{
QSqlQuery query;
if (!query.exec(QString("select max(EventNum) from %1 where Issue = %2")
.arg(TABLE_HISTORY).arg(bugId)))
return SqlHelper::errorText(query);
if (query.isSelect() && query.first())
return query.record().value(0).toInt() + 1;
return 1;
}
QVariant BugManager::generateEventPart(int bugId, int eventId)
{
QSqlQuery query;
if (!query.exec(QString("select max(EventPart) from %1 where Issue = %2 and EventNum = %3")
.arg(TABLE_HISTORY).arg(bugId).arg(eventId)))
return SqlHelper::errorText(query);
if (query.isSelect() && query.first())
return query.record().value(0).toInt() + 1;
return 1;
}
QString BugManager::dictionaryTableName(int dictId)
{
switch (dictId)
{
case COL_CATEGORY: return TABLE_CATEGORY;
case COL_SEVERITY: return TABLE_SEVERITY;
case COL_PRIORITY: return TABLE_PRIORITY;
case COL_STATUS: return TABLE_STATUS;
case COL_REPEAT: return TABLE_REPEAT;
case COL_SOLUTION: return TABLE_SOLUTION;
}
return "";
}
QMap<int, QSqlTableModel*> dictionaries;
QMap<int, DictionaryCash*> dictionaryCashes;
QSqlTableModel* BugManager::dictionary(int dictId)
{
if (dictionaries.contains(dictId))
return dictionaries.value(dictId);
return NULL;
}
DictionaryCash *BugManager::dictionaryCash(int dictId)
{
if (dictionaryCashes.contains(dictId))
return dictionaryCashes.value(dictId);
return NULL;
}
void BugManager::closeDictionaries()
{
QSqlTableModel *table;
foreach (table, dictionaries.values())
delete table;
dictionaries.clear();
DictionaryCash *cash;
foreach (cash, dictionaryCashes.values())
delete cash;
dictionaryCashes.clear();
}
void BugManager::loadDictionaries()
{
closeDictionaries();
loadDictionary(COL_STATUS);
loadDictionary(COL_SOLUTION);
loadDictionary(COL_SEVERITY);
loadDictionary(COL_PRIORITY);
loadDictionary(COL_REPEAT);
loadDictionary(COL_CATEGORY);
updateDictionaryCash(COL_STATUS);
updateDictionaryCash(COL_SOLUTION);
updateDictionaryCash(COL_SEVERITY);
updateDictionaryCash(COL_PRIORITY);
updateDictionaryCash(COL_REPEAT);
updateDictionaryCash(COL_CATEGORY);
}
void BugManager::loadDictionary(int dictId)
{
QString tableName = dictionaryTableName(dictId);
if (tableName.isEmpty()) return;
QSqlTableModel *tableModel = new QSqlTableModel;
dictionaries.insert(dictId, tableModel);
tableModel->setTable(tableName);
tableModel->select();
}
void BugManager::updateDictionaryCash(int dictId)
{
QSqlTableModel *table = dictionary(dictId);
if (!table) return;
DictionaryCash* cash = dictionaryCash(dictId);
if (!cash)
{
cash = new DictionaryCash;
dictionaryCashes.insert(dictId, cash);
}
cash->clear();
for (int row = 0; row < table->rowCount(); row++)
{
QSqlRecord record = table->record(row);
cash->insert(record.field(DICT_COL_ID).value().toInt(),
record.field(DICT_COL_TITLE).value().toString());
}
}
QString BugManager::displayDictValue(int dictId, const QVariant &dictKey)
{
QMap<int, QString>* dict = dictionaryCash(dictId);
if (!dict)
return dictKey.toString();
if (!dict->contains(dictKey.toInt()))
return dictKey.toString();
return dict->value(dictKey.toInt());
}
QString BugManager::displayDateTime(const QVariant &value)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
return QLocale::system().toString(value.toDateTime(), QLocale::ShortFormat);
#else
return value.toDateTime().toString(Qt::SystemLocaleShortDate);
#endif
}
QString BugManager::addHistroyItem(int bugId, const QVariant& eventNum,
const QDateTime &moment, int changedParam,
const QVariant &oldValue, const QVariant &newValue)
{
QSqlTableModel model;
model.setTable(TABLE_HISTORY);
return addHistroyItem(&model, bugId, eventNum, moment, changedParam, oldValue, newValue);
}
QString BugManager::addHistroyItem(QSqlTableModel *model,
int bugId, const QVariant& eventNum,
const QDateTime &moment, int changedParam,
const QVariant &oldValue, const QVariant &newValue)
{
QVariant eventPart = generateEventPart(bugId, eventNum.toInt());
if (eventPart.type() == QVariant::String)
return qApp->tr("Unable to generate history item id.\n\n%1").arg(eventPart.toString());
QSqlRecord record;
SqlHelper::addField(record, "Issue", QVariant::Int, bugId);
SqlHelper::addField(record, "EventNum", QVariant::Int, eventNum);
SqlHelper::addField(record, "EventPart", QVariant::Int, eventPart);
SqlHelper::addField(record, "ChangedParam", QVariant::Int, changedParam);
SqlHelper::addField(record, "OldValue", QVariant::Int, oldValue);
SqlHelper::addField(record, "NewValue", QVariant::Int, newValue);
SqlHelper::addField(record, "Moment", QVariant::DateTime, moment);
if (!model->insertRecord(-1, record))
return SqlHelper::errorText(model);
return QString();
}
QString BugManager::addHistroyComment(int bugId, const QVariant& eventNum, const QDateTime &moment, const QString &comment)
{
QVariant eventPart = generateEventPart(bugId, eventNum.toInt());
if (eventPart.type() == QVariant::String)
return qApp->tr("Unable to generate history item id.\n\n%1").arg(eventPart.toString());
QSqlRecord record;
SqlHelper::addField(record, "Issue", QVariant::Int, bugId);
SqlHelper::addField(record, "EventNum", QVariant::Int, eventNum);
SqlHelper::addField(record, "EventPart", QVariant::Int, eventPart);
SqlHelper::addField(record, "ChangedParam", QVariant::Int, -1);
SqlHelper::addField(record, "Moment", QVariant::DateTime, moment);
SqlHelper::addField(record, "Comment", QVariant::String, comment);
QSqlTableModel model;
model.setTable(TABLE_HISTORY);
if (!model.insertRecord(-1, record))
return SqlHelper::errorText(model);
return QString();
}
QString BugManager::countBugs(int& total, int& opened, int& displayed, const QString& filter)
{
QSqlQuery query;
if (!query.exec(QString("select count(Id) from %1").arg(TABLE_BUGS)))
return qApp->tr("Unable to count total bugs: %1").arg(query.lastError().text());
if (query.isSelect() && query.first())
total = query.record().value(0).toInt();
if (!query.exec(QString("select count(Id) from %1 where Status = %2")
.arg(TABLE_BUGS).arg(STATUS_OPENED)))
return qApp->tr("Unable to count opened bugs: %1").arg(query.lastError().text());
if (query.isSelect() && query.first())
opened = query.record().value(0).toInt();
if (!filter.isEmpty())
{
if (!query.exec(QString("select count(Id) from %1 where %2").arg(TABLE_BUGS).arg(filter)))
return qApp->tr("Unable to count displayed bugs: %1").arg(query.lastError().text());
if (query.isSelect() && query.first())
displayed = query.record().value(0).toInt();
}
else
displayed = total;
return QString();
}
QString BugManager::columnTitle(int colId)
{
switch (colId)
{
case COL_ID: return qApp->translate("Column title", "ID");
case COL_SUMMARY: return qApp->translate("Column title", "Summary");
case COL_EXTRA: return qApp->translate("Column title", "Extra Information");
case COL_CATEGORY: return qApp->translate("Column title", "Category");
case COL_SEVERITY: return qApp->translate("Column title", "Severity");
case COL_PRIORITY: return qApp->translate("Column title", "Priority");
case COL_REPEAT: return qApp->translate("Column title", "Repeatability");
case COL_STATUS: return qApp->translate("Column title", "Status");
case COL_SOLUTION: return qApp->translate("Column title", "Solution");
case COL_CREATED: return qApp->translate("Column title", "Created");
case COL_UPDATED: return qApp->translate("Column title", "Updated");
default: return QString();
}
}
QString BugManager::operationTitle(int status)
{
switch (status)
{
case STATUS_OPENED: return qApp->tr("Solve...");
case STATUS_SOLVED: return qApp->tr("Close...");
case STATUS_CLOSED: return qApp->tr("Reopen...");
}
return qApp->tr("Process..."); // stub name, no action
}
QList<int> BugManager::dictionaryIds()
{
return { COL_CATEGORY, COL_SEVERITY, COL_PRIORITY, COL_STATUS, COL_SOLUTION, COL_REPEAT };
}
QFileInfo BugManager::fileInDatabaseFiles(const QString& fileName)
{
QFileInfo file(currentFile());
file.setFile(file.absoluteDir().path() + '/' + file.completeBaseName() +
QStringLiteral(".files/") + fileName);
return file;
}
//-----------------------------------------------------------------------------------------------
QString BugComparer::writeHistory(const BugInfo& oldValue, const BugInfo& newValue)
{
QVariant eventNum = BugManager::generateEventId(oldValue.id);
if (eventNum.type() == QVariant::String)
return qApp->tr("Unable to generate history item number.\n\n%1").arg(eventNum.toString());
QSqlTableModel model;
model.setTable(TABLE_HISTORY);
QString result;
QDateTime moment = QDateTime::currentDateTime();
if (oldValue.summary != newValue.summary)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_SUMMARY, oldValue.summary, newValue.summary);
if (oldValue.extra != newValue.extra)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_EXTRA, oldValue.extra, newValue.extra);
if (oldValue.category != newValue.category)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_CATEGORY, oldValue.category, newValue.category);
if (oldValue.severity != newValue.severity)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_SEVERITY, oldValue.severity, newValue.severity);
if (oldValue.priority != newValue.priority)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_PRIORITY, oldValue.priority, newValue.priority);
if (oldValue.status != newValue.status)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_STATUS, oldValue.status, newValue.status);
if (oldValue.solution != newValue.solution)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_SOLUTION, oldValue.solution, newValue.solution);
if (oldValue.repeat != newValue.repeat)
result += BugManager::addHistroyItem(&model, oldValue.id, eventNum, moment,
COL_REPEAT, oldValue.repeat, newValue.repeat);
return result;
}
//-----------------------------------------------------------------------------------------------
QComboBox* WidgetHelper::createDictionaryCombo(int dictId)
{
QComboBox *combo = new QComboBox;
combo->setMaxVisibleItems(24);
combo->setModel(BugManager::dictionary(dictId));
combo->setModelColumn(DICT_COL_TITLE);
return combo;
}
void WidgetHelper::selectText(QComboBox *combo, const QString &value)
{
for (int i = 0; i < combo->count(); i++)
if (combo->itemText(i) == value)
{
combo->setCurrentIndex(i);
return;
}
}
void WidgetHelper::selectId(QComboBox *combo, const QVariant &value)
{
QModelIndex startIndex = combo->model()->index(0, DICT_COL_ID);
QModelIndexList values = combo->model()->match(startIndex, Qt::EditRole, value, 1, Qt::MatchExactly);
if (!values.isEmpty())
combo->setCurrentIndex(values.at(0).row());
}
QVariant WidgetHelper::selectedId(QComboBox *combo)
{
return combo->model()->index(combo->currentIndex(), DICT_COL_ID).data();
}
//-----------------------------------------------------------------------------------------------
QString IssueFilter::getSql() const
{
return check? QString("%1 %2 %3").arg(name).arg(condition).arg(value): QString();
}
//-----------------------------------------------------------------------------------------------
void IssueFilters::add(const QString& name, bool check, const QString& condition, int value)
{
IssueFilter filter;
filter.name = name;
filter.check = check;
filter.condition = condition;
filter.value = value;
filters.append(filter);
}
QString IssueFilters::getSql() const
{
QString filter;
for (int i = 0; i < filters.size(); i++)
{
QString condition = filters.at(i).getSql();
if (!condition.isEmpty())
{
if (!filter.isEmpty()) filter += " and ";
filter += condition;
}
}
return filter;
}
QString IssueFilters::save()
{
DbSettings dbs(true);
if (!dbs.lastError.isEmpty())
return qApp->tr("Unable to begin transaction for filters saving.\n\n%1").arg(dbs.lastError);
return saveInternal(dbs);
}
QString IssueFilters::load()
{
DbSettings dbs(false);
return loadInternal(dbs);
}
QString IssueFilters::saveInternal(DbSettings& dbs)
{
QString res, key, names;
QString prefix = storagePrefix();
for (int i = 0; i < filters.size(); i++)
{
const IssueFilter& filter = filters.at(i);
key = QString("%1\\%2\\").arg(prefix, filter.name);
res = dbs.saveSetting(key + "Check", filter.check);
if (!res.isEmpty()) return res;
res = dbs.saveSetting(key + "Condition", filter.condition);
if (!res.isEmpty()) return res;
res = dbs.saveSetting(key + "Value", filter.value);
if (!res.isEmpty()) return res;
names += filter.name + ";";
}
return dbs.saveSetting(QString("%1\\Names").arg(prefix), names);
}
QString IssueFilters::loadInternal(DbSettings& dbs)
{
QVariant value;
QString prefix = storagePrefix();
QString key, res = dbs.loadSetting(QString("%1\\Names").arg(prefix), value, "");
if (!res.isEmpty() || value.toString().isEmpty()) return res;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
auto skipEmptyParts = Qt::SkipEmptyParts;
#else
auto skipEmptyParts = QString::SkipEmptyParts;
#endif
QStringList names = value.toString().split(';', skipEmptyParts);
for (const QString& name: names)
{
IssueFilter filter;
filter.name = name;
key = QString("%1\\%2\\").arg(prefix, filter.name);
res = dbs.loadSetting(key + "Check", value, false);
if (!res.isEmpty()) return res;
filter.check = value.toBool();
res = dbs.loadSetting(key + "Condition", value, "=");
if (!res.isEmpty()) return res;
filter.condition = value.toString();
res = dbs.loadSetting(key + "Value", value, 100);
if (!res.isEmpty()) return res;
filter.value = value.toInt();
filters.append(filter);
}
return QString();
}
//-----------------------------------------------------------------------------------------------
IntResult IssueFiltersPreset::generatePresetId()
{
DbSettings dbs(false);
QVariant value;
QString res = dbs.loadSetting("FilterPresetMaxId", value, 0);
if (!res.isEmpty()) return IntResult::fail(res);
int id = value.toInt() + 1;
res = dbs.saveSetting("FilterPresetMaxId", id);
if (!res.isEmpty()) return IntResult::fail(res);
return IntResult::ok(id);
}
IssueFiltersPreset::PresetsResult IssueFiltersPreset::loadPresets()
{
QSqlQuery query;
if (!query.exec(QString("select * from %1 where Name like 'FilterPreset\\%\\Title'").arg(TABLE_SETTINGS)))
return PresetsResult::fail(SqlHelper::errorText(query, true));
QMap<int, QString> presets;
if (query.isSelect() && query.first())
while (query.isValid())
{
QSqlRecord record = query.record();
QStringList id_parts = record.value("Name").toString().split('\\');
if (id_parts.size() > 1)
{
bool ok;
int id = id_parts.at(1).toInt(&ok);
if (ok) presets.insert(id, record.value("Value").toString());
}
query.next();
}
return PresetsResult::ok(presets);
}
QString IssueFiltersPreset::deletePreset(int id)
{
QSqlQuery query;
if (!query.exec(QString("delete from %1 where Name like 'FilterPreset\\%2\\%'").arg(TABLE_SETTINGS).arg(id)))
return SqlHelper::errorText(query, true);
return QString();
}
QString IssueFiltersPreset::storagePrefix() const
{
return QString::fromLatin1("FilterPreset\\%1").arg(_id);
}
QString IssueFiltersPreset::saveInternal(DbSettings& dbs)
{
QString prefix = storagePrefix();
QString res = dbs.saveSetting(QString("%1\\Title").arg(prefix), _title);
if (!res.isEmpty()) return res;
return IssueFilters::saveInternal(dbs);
}
//-----------------------------------------------------------------------------------------------
DbSettings::DbSettings(bool transaction): _transaction(transaction)
{
if (transaction)
if (!__db.transaction())
lastError = SqlHelper::errorText(__db.lastError());
}
DbSettings::~DbSettings()
{
if (_transaction)
__db.commit();
}
void DbSettings::rollback()
{
if (_transaction)
{
lastError.clear();
_transaction = false;
if (!__db.rollback())
lastError = SqlHelper::errorText(__db.lastError());
}
}
QString DbSettings::lastErrorStr()
{
return lastError.isEmpty()? QString(): "\n\n" + lastError;
}
QString DbSettings::saveSetting(const QString& name, const QVariant& value)
{
QSqlQuery query;
if (!query.exec(QString("select count(Name) from %1 where Name = '%2'").arg(TABLE_SETTINGS, name)))
{
rollback();
return qApp->tr("Unable to save setting '%1': %2%3")
.arg(name, SqlHelper::errorText(query), lastErrorStr());
}
QString sql;
if (query.isSelect() && query.first() && query.record().value(0).toInt() == 0)
{
sql = QString("insert into %1 (Name, Value) values ('%2', '%3')")
.arg(TABLE_SETTINGS, name, value.toString());
}
else
{
sql = QString("update %1 set Value = '%3' where Name = '%2'")
.arg(TABLE_SETTINGS, name, value.toString());
}
if (!query.exec(sql))
{
rollback();
return qApp->tr("Unable to save setting '%1': %2%3")
.arg(name, SqlHelper::errorText(query), lastErrorStr());
}
return QString();
}
QString DbSettings::loadSetting(const QString& name, QVariant& value, const QVariant& def)
{
QSqlQuery query;
if (!query.exec(QString("select Value from %1 where Name = '%2'").arg(TABLE_SETTINGS, name)))
return qApp->tr("Unable to load setting '%1': %2").arg(name, SqlHelper::errorText(query));
if (query.isSelect() && query.first())
{
value = query.record().value(0);
if (value.type() != QVariant::String)
return qApp->tr("Setting '%1' has no proper type").arg(name);
if (def.isValid() && def.type() != QVariant::String)
if (!value.convert(def.type()))
return qApp->tr("Setting '%1' has no proper type").arg(name);
}
else value = def;
return QString();
}
//-----------------------------------------------------------------------------------------------
QVariant ptr2var(void *p)
{
return QVariant::fromValue(p);
}
bool checkResult(QWidget *parent, const QVariant& result, const QString& message)
{
if (result.type() == QVariant::String)
{
QMessageBox::critical(parent, qApp->applicationName(), message + "\n\n" + result.toString());
return false;
}
return true;
}
bool checkResult(QWidget *parent, const QString& result, const QString& message)
{
if (!result.isEmpty())
{
QMessageBox::critical(parent, qApp->applicationName(), message + "\n\n" + result);
return false;
}
return true;
}
//-----------------------------------------------------------------------------------------------
QString DictManager::status(int id) { return BugManager::dictionaryCash(COL_STATUS)->value(id); }
QString DictManager::solution(int id) { return BugManager::dictionaryCash(COL_SOLUTION)->value(id); }