-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdatabase.cpp
6644 lines (5867 loc) · 271 KB
/
database.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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
database.cpp - description
-------------------
begin : sept 2011
copyright : (C) 2011 by Jaime Robles
email : [email protected]
***************************************************************************/
/*****************************************************************************
* This file is part of KLog. *
* *
* KLog is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* KLog is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with KLog. If not, see <https://www.gnu.org/licenses/>. *
* *
*****************************************************************************/
#include "database.h"
//#include <qDebug>
DataBase::DataBase(const QString &_parentClass, const QString &_DBName)
{
Q_UNUSED(_parentClass);
//qDebug() << Q_FUNC_INFO << _parentClass << " / Name = " << _DBName ;
logLevel = None;
constrid = 1;
created = false;
//qDebug() << Q_FUNC_INFO << " 001";
util = new Utilities(Q_FUNC_INFO);
//qDebug() << Q_FUNC_INFO << " 003";
softVersion = util->getVersion();
//qDebug() << Q_FUNC_INFO << " 004";
dbName = _DBName;
//qDebug() << Q_FUNC_INFO << " dbName: " << dbName ;
dbVersion = DBVersionf;
//qDebug() << Q_FUNC_INFO << " 005";
exe = new QueryExecutor(Q_FUNC_INFO);
if (!createConnection(QString(Q_FUNC_INFO)+"1"))
{
//qDebug() << Q_FUNC_INFO << " 006:Could not create a connection";
return;
}
//qDebug() << Q_FUNC_INFO << " 010";
//qDebug() << Q_FUNC_INFO << " - connection Name: " << dbConnectionName ;
//qDebug() << Q_FUNC_INFO << " - DB Name: " << db.databaseName() ;
insertPreparedQueries.clear();
insertQueryFields.clear();
//qDebug() << Q_FUNC_INFO << " - END" ;
}
DataBase::DataBase(const QString &_parentClass, const QString &_softVersion, const QString &_DBName)
{
Q_UNUSED(_parentClass);
//qDebug() << "DataBase::DataBase2: " << _parentClass << "/" << _softVersion << " / Name = " << _DBName ;
//TODO: Sometimes the DB is created without the proper calling (without passing softVersion)
logLevel = None;
constrid = 2;
created = false;
dbVersion = DBVersionf;
softVersion = _softVersion;
//inMemoryOnly = inmemoryonly;
latestReaded = 0.0f;
util = new Utilities(Q_FUNC_INFO);
util->setVersion(softVersion);
dbName = _DBName;
if (util->getVersionDouble()>0)
{
if (!createConnection(QString(Q_FUNC_INFO)+"2"))
return;
}
//qDebug() << "DataBase::DataBase: - connection Name: " << dbConnectionName ;
//qDebug() << "DataBase::DataBase: - DB Name: " << db.databaseName() ;
insertPreparedQueries.clear();
insertQueryFields.clear();
//qDebug() << "DataBase::DataBase2: END" ;
}
DataBase::~DataBase()
{
logEvent(Q_FUNC_INFO, "Start", Debug);
delete(util);
logEvent(Q_FUNC_INFO, "END", Debug);
//qDebug() << "DataBase::~DataBase" ;
}
QString DataBase::getSoftVersion()
{
logEvent(Q_FUNC_INFO, "Start", Debug);
QSqlQuery query;
QString stringQuery ("SELECT MAX (softversion) FROM softwarecontrol");
bool sqlOK = query.exec(stringQuery);
if (sqlOK)
{
query.next();
if (query.isValid())
{
logEvent(Q_FUNC_INFO, "END-1", Debug);
QString aux = (query.value(0)).toString();
query.finish();
return aux;
}
else
{
query.finish();
logEvent(Q_FUNC_INFO, "END-2", Debug);
return QString();
}
}
else
{ //ERROR in Query execution
queryErrorManagement(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
query.finish();
logEvent(Q_FUNC_INFO, "END-3", Debug);
return QString();
}
}
float DataBase::getDBVersion()
{
logEvent(Q_FUNC_INFO, "Start", Debug);
QSqlQuery query;
QString stringQuery ("SELECT MAX (dbversion) FROM softwarecontrol");
bool sqlOK = query.exec(stringQuery);
if (sqlOK)
{
query.next();
if (query.isValid())
{
logEvent(Q_FUNC_INFO, "END-1", Debug);
float version = (query.value(0)).toFloat(&sqlOK);
query.finish();
if (sqlOK)
return version;
return -1.3;
}
else
{
query.finish();
logEvent(Q_FUNC_INFO, "END-2", Debug);
return -1.1;
}
}
else
{ //ERROR in Query execution
queryErrorManagement(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
query.finish();
logEvent(Q_FUNC_INFO, "END-3", Debug);
return -1.0;
}
}
/*
bool DataBase::setDir(const QString &_dir)
{
dbDir = _dir;
return true;
}
*/
QString DataBase::getDBName()
{
logEvent(Q_FUNC_INFO, "Start-EndEND", Debug);
return db.databaseName();
}
QStringList DataBase::getColumnNamesFromTable(const QString &_tableName)
{
logEvent(Q_FUNC_INFO, "Start", Debug);
QSqlQuery query;
QString queryString = "PRAGMA table_info(:table)";
query.prepare(queryString);
query.bindValue(":table", _tableName);
bool sqlOK = query.exec();
QStringList list;
if (sqlOK)
{
while (query.next())
{
QString columnName = query.value(1).toString();
if (!columnName.isEmpty() && columnName.toUpper() != "ID")
{
list << columnName;
}
}
}
else
{
queryErrorManagement(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
}
query.finish();
logEvent(Q_FUNC_INFO, "END", Debug);
return list;
}
void DataBase::compress()
{
logEvent(Q_FUNC_INFO, "Start", Debug);
//QSqlDatabase db = QSqlDatabase::database();
if (!db.open()) { /* Flawfinder: ignore */
QMessageBox::warning(nullptr, QObject::tr("Database Error"),
db.lastError().text());
}
else
{
QSqlQuery query("VACUUM;");
query.exec("VACUUM;");
}
logEvent(Q_FUNC_INFO, "END", Debug);
}
bool DataBase::reConnect(const QString &_DBName)
{
logEvent(Q_FUNC_INFO, "Start", Debug);
db.close();
dbName = _DBName;
//qDebug() << "DataBase::reConnect: DB closed" ;
//qDebug() << "DataBase::reConnect: DB: " << dbDir ;
bool sqlOK = createConnection(Q_FUNC_INFO);
if (!sqlOK)
{
// emit debugLog(Q_FUNC_INFO, "1", 7);
}
logEvent(Q_FUNC_INFO, "END", Debug);
return sqlOK;
}
bool DataBase::createConnection(const QString &function, bool newDB)
{
//qDebug() << Q_FUNC_INFO << " - Start";
//qDebug() << Q_FUNC_INFO << " :" << function << "-" << QString::number(dbVersion) << "/" << softVersion ;
logEvent(Q_FUNC_INFO, "Start", Debug);
Q_UNUSED(function);
Q_UNUSED(newDB);
QString stringQuery;
QSqlQuery query;
//qDebug() << Q_FUNC_INFO << ": check if open";
if (!db.isOpen())
{
//qDebug() << Q_FUNC_INFO << ": DB NOT Opened" ;
if (!db.isValid())
{
//qDebug() << Q_FUNC_INFO << ": DB is not valid, let's call addDataBase" ;
if (!db.isOpen())
{
//qDebug() << Q_FUNC_INFO << ": DB is NOT open, let's open: connection name" << db.connectionName();
QSqlDatabase::removeDatabase("qt_sql_default_connection");
db = QSqlDatabase::addDatabase("QSQLITE");
}
//qDebug() << Q_FUNC_INFO << ": Now we call setDatabaseName" ;
db.setDatabaseName(dbName);
//qDebug() <<Q_FUNC_INFO << ": end of not valid" ;
}
//qDebug() << Q_FUNC_INFO << ": end of valid check, let's try if it is open" ;
if (!db.open()) /* Flawfinder: ignore */
{
//qDebug() << Q_FUNC_INFO << ": Not open " ;
QMessageBox::warning(nullptr, QObject::tr("Database Error"), db.lastError().text());
//qDebug() << Q_FUNC_INFO << ": DB creation ERROR" ;
// emit debugLog(Q_FUNC_INFO, "1", 7);
logEvent(Q_FUNC_INFO, "END-1", Debug);
return false;
}
else
{
//qDebug() << Q_FUNC_INFO << ": created and opened after the creation" ;
if (!isTheDBCreated())
{
//qDebug() << Q_FUNC_INFO << ": DB is still not created" ;
if (!createDataBase())
{
//qDebug() << Q_FUNC_INFO << ":DB creation failed!!" ;
logEvent(Q_FUNC_INFO, "END-2", Debug);
return false;
}
//qDebug() << Q_FUNC_INFO << ": After creation" ;
setPragma();
}
}
}
else
{
//qDebug() << Q_FUNC_INFO << ": No Error, DB is open";
}
//qDebug() << Q_FUNC_INFO << ": Going to run - createBandModeMaps " ;
if (!createBandModeMaps())
{
//qDebug() << Q_FUNC_INFO << ": createBandModeMaps false Stop";
logEvent(Q_FUNC_INFO, "END-3", Debug);
return false;
}
logEvent(Q_FUNC_INFO, "END", Debug);
//qDebug() << Q_FUNC_INFO << " - END";
return unMarkAllQSO();
}
bool DataBase::setPragma()
{
//qDebug() << Q_FUNC_INFO << " - Start" ;
if (!execQuery(Q_FUNC_INFO, "PRAGMA main.page_size = 4096;"))
return false;
if (!execQuery(Q_FUNC_INFO, "PRAGMA main.cache_size=10000;"))
return false;
if (!execQuery(Q_FUNC_INFO, "PRAGMA main.synchronous=NORMAL;"))
return false;
if (!execQuery(Q_FUNC_INFO, "PRAGMA main.journal_mode=WAL;"))
return false;
if (!execQuery(Q_FUNC_INFO, "PRAGMA main.cache_size=5000;"))
return false;
if (!execQuery(Q_FUNC_INFO, "PRAGMA synchronous=OFF;"))
return false;
if (!execQuery(Q_FUNC_INFO, "PRAGMA main.temp_store = MEMORY;"))
return false;
//qDebug() << Q_FUNC_INFO << " - END" ;
return execQuery(Q_FUNC_INFO, "PRAGMA case_sensitive_like=OFF;");
}
bool DataBase::isTheDBCreated()
{
//qDebug() << "DataBase::isTheDBCreated: Called from: " << QString::number(constrid) ;
logEvent(Q_FUNC_INFO, "Start", Debug);
QSqlQuery query;
int _num = 0;
//QString stringQuery ("SELECT name FROM sqlite_sequence WHERE name='softwarecontrol'");
QString stringQuery ("SELECT count(id) FROM softwarecontrol");
bool sqlOK = query.exec(stringQuery);
if (!sqlOK)
{
queryErrorManagement(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
query.finish();
logEvent(Q_FUNC_INFO, "END-1", Debug);
return false;
}
if (!query.next())
{
query.finish();
logEvent(Q_FUNC_INFO, "END-2", Debug);
return false;
}
if (!query.isValid())
{
query.finish();
logEvent(Q_FUNC_INFO, "END-3", Debug);
return false;
}
//qDebug() << "DataBase::isTheDBCreated - valid" ;
_num = (query.value(0)).toInt();
query.finish();
return (_num>0);
}
bool DataBase::recreateTableLog()
{
logEvent(Q_FUNC_INFO, "Start", Debug);
if (!createTableLog(false)) // Create modetemp
{
//qDebug() << Q_FUNC_INFO << ": CreateTableLog returned false" ;
logEvent(Q_FUNC_INFO, "END-1", Debug);
return false;
}
QString queryString;
queryString.clear();
QStringList columns;
columns.clear();
columns << getColumnNamesFromTable("log");
queryString = columns.first();
for (int i=1;i<columns.size()-1;i++)
{
if ( !(columns.at(i) == "time_on") && !(columns.at(i) == "time_off") )
{
queryString = queryString + ", " + columns.at(i);
}
}
queryString = "INSERT INTO logtemp (" + queryString + ", " + columns.last() + ") SELECT " + queryString + ", " + columns.last() + " FROM log";
if (!execQuery(Q_FUNC_INFO, queryString))
{
//qDebug() << Q_FUNC_INFO << ": ERROR - Data not moved" ;
logEvent(Q_FUNC_INFO, "END-4", Debug);
return false;
}
if (!execQuery(Q_FUNC_INFO, "DROP table log"))
{
//qDebug() << Q_FUNC_INFO << ": ERROR - log table not dropped" ;
return false;
}
return execQuery(Q_FUNC_INFO, "ALTER TABLE logtemp RENAME TO log");
}
bool DataBase::createTableLog(bool temp)
{ //Creates a temporal table or the normal one.
logEvent(Q_FUNC_INFO, "Start", Debug);
//qDebug() << Q_FUNC_INFO << " - Start" ;
QString stringQuery = QString();
if (temp)
{
stringQuery = "CREATE TABLE log" ;
//qDebug() << Q_FUNC_INFO << ": log" ;
}
else
{
stringQuery = "CREATE TABLE logtemp" ;
//qDebug() << Q_FUNC_INFO << ": logtemp" ;
}
stringQuery = stringQuery + QString(" (id INTEGER PRIMARY KEY AUTOINCREMENT, "
"qso_date DATETIME NOT NULL, " // 2020-01-01 10:12:01
"call VARCHAR(40) NOT NULL, "
"rst_sent VARCHAR, "
"rst_rcvd VARCHAR, "
"bandid INTEGER NOT NULL, "
"modeid INTEGER NOT NULL, "
"cqz INTEGER, "
"ituz INTEGER, "
"dxcc INTEGER, "
"address VARCHAR, "
"age INTEGER, "
"a_index INTEGER, "
"altitude REAL, "
"ant_az INTEGER, "
"ant_el INTEGER, "
"ant_path INTEGER, "
"arrl_sect VARCHAR(3), "
"award_submitted VARCHAR, "
"award_granted VARCHAR, "
"band_rx INTEGER, "
"checkcontest VARCHAR, "
"class VARCHAR, "
"clublog_qso_upload_date DATETIME, "
"clublog_qso_upload_status VARCHAR(1), "
"cnty VARCHAR, "
"comment VARCHAR, "
"cont VARCHAR(2), "
"contacted_op VARCHAR(40), "
"contest_id VARCHAR, "
"country VARCHAR, "
"credit_submitted VARCHAR, "
"credit_granted VARCHAR, "
"darc_dok VARCHAR,"
"distance INTEGER, "
"email VARCHAR, "
"eq_call VARCHAR, "
"eqsl_qslrdate DATETIME, "
"eqsl_qslsdate DATETIME, "
"eqsl_qsl_rcvd VARCHAR(1), "
"eqsl_qsl_sent VARCHAR(1), "
"fists INTEGER, "
"fists_cc INTEGER, "
"force_init INTEGER, "
"freq VARCHAR, "
"freq_rx VARCHAR, "
"gridsquare VARCHAR, "
"gridsquare_ext VARCHAR, "
"hamlogeu_qso_upload_date DATETIME, "
"hamlogeu_qso_upload_status VARCHAR(1), "
"hamqth_qso_upload_date DATETIME, "
"hamqth_qso_upload_status VARCHAR(1), "
"hrdlog_qso_upload_date DATETIME, "
"hrdlog_qso_upload_status VARCHAR(1), "
"iota VARCHAR(6), "
"iota_island_id VARCHAR, "
"k_index INTEGER, "
"lat VARCHAR(11), "
"lon VARCHAR(11), "
"lotw_qslrdate DATETIME, "
"lotw_qslsdate DATETIME, "
"lotw_qsl_rcvd VARCHAR(1), "
"lotw_qsl_sent VARCHAR(1), "
"max_bursts INTEGER, "
"ms_shower VARCHAR, "
"my_altitude REAL, "
"my_antenna VARCHAR, "
"my_arrl_sect VARCHAR(3), "
"my_city VARCHAR, "
"my_cnty VARCHAR, "
"my_country INTEGER, "
"my_cq_zone INTEGER, "
"my_dxcc INTEGER, "
"my_fists INTEGER, "
"my_gridsquare VARCHAR, "
"my_gridsquare_ext VARCHAR, "
"my_iota VARCHAR(6), "
"my_iota_island_id VARCHAR, "
"my_itu_zone INTEGER, "
"my_lat VARCHAR(11), "
"my_lon VARCHAR(11), "
"my_name VARCHAR, "
"my_pota_ref VARCHAR, "
"my_postal_code VARCHAR ,"
"my_rig VARCHAR, "
"my_sig VARCHAR, "
"my_sig_info VARCHAR, "
"my_sota_ref VARCHAR, "
"my_state VARCHAR, "
"my_street VARCHAR, "
"my_usaca_counties VARCHAR, "
"my_vucc_grids VARCHAR, "
"my_wwff_ref VARCHAR(11), "
"name VARCHAR, "
"notes VARCHAR, "
"nr_bursts INTEGER, "
"nr_pings INTEGER, "
"operator VARCHAR, "
"owner_callsign VARCHAR, "
"pfx VARCHAR, "
"pota_ref VARCHAR, "
"precedence VARCHAR, "
"prop_mode VARCHAR, "
"public_key VARCHAR, "
"qrzcom_qso_upload_date DATETIME, "
"qrzcom_qso_upload_status VARCHAR(1), "
"qslmsg VARCHAR, "
"qslrdate DATETIME, "
"qslsdate DATETIME, "
"qsl_rcvd VARCHAR(1), "
"qsl_sent VARCHAR(1), "
"qsl_rcvd_via VARCHAR(1), "
"qsl_sent_via VARCHAR(1), "
"qsl_via VARCHAR, "
"qso_complete VARCHAR(1), "
"qso_random INTEGER, "
"qth VARCHAR, "
"region VARCHAR, "
"rig VARCHAR, "
"rx_pwr REAL, "
"sat_mode VARCHAR, "
"sat_name VARCHAR, "
"sfi INTEGER, "
"sig VARCHAR, "
"sig_info VARCHAR, "
"silent_key VARCHAR(1), "
"skcc VARCHAR, "
"sota_ref VARCHAR, "
"srx_string VARCHAR, "
"srx VARCHAR(10), "
"stx_string VARCHAR, "
"stx VARCHAR(10), "
"state VARCHAR, "
"station_callsign VARCHAR, "
"submode VARCHAR, "
"swl INTEGER, "
"ten_ten INTEGER, "
"tx_pwr REAL, "
"uksmg INTEGER, "
"usaca_counties VARCHAR, "
"ve_prov VARCHAR, "
"vucc_grids VARCHAR, "
"web VARCHAR, "
"wwff_ref VARCHAR(11), "
"qso_date_off DATETIME, " //2020-01-01
"marked VARCHAR(1), "
"lognumber INTEGER NOT NULL, "
"UNIQUE (call, qso_date, bandid, modeid, lognumber), "
"FOREIGN KEY (qso_complete) REFERENCES qso_complete_enumeration, "
"FOREIGN KEY (qsl_rcvd_via) REFERENCES qsl_via_enumeration, "
"FOREIGN KEY (qsl_sent_via) REFERENCES qsl_via_enumeration, "
"FOREIGN KEY (qsl_rcvd) REFERENCES qsl_rec_status, "
"FOREIGN KEY (qsl_sent) REFERENCES qsl_sent_status, "
"FOREIGN KEY (prop_mode) REFERENCES prop_mode_enumeration, "
"FOREIGN KEY (my_country) REFERENCES entity, "
"FOREIGN KEY (lotw_qsl_rcvd) REFERENCES qsl_rec_status, "
"FOREIGN KEY (lotw_qsl_sent) REFERENCES qsl_sent_status, "
"FOREIGN KEY (eqsl_qsl_rcvd) REFERENCES qsl_rec_status, "
"FOREIGN KEY (eqsl_qsl_sent) REFERENCES qsl_sent_status, "
"FOREIGN KEY (credit_submitted) REFERENCES award_enumeration, "
"FOREIGN KEY (credit_granted) REFERENCES award_enumeration, "
"FOREIGN KEY (country) REFERENCES entity, "
"FOREIGN KEY (ant_path) REFERENCES ant_path_enumeration, "
"FOREIGN KEY (arrl_sect) REFERENCES arrl_sect_enumeration, "
"FOREIGN KEY (my_arrl_sect) REFERENCES arrl_sect_enumeration, "
"FOREIGN KEY (band_rx) REFERENCES band (id), "
"FOREIGN KEY (modeid) REFERENCES mode (id), "
"FOREIGN KEY (submode) REFERENCES mode (id), "
"FOREIGN KEY (dxcc) REFERENCES entity (dxcc), "
"FOREIGN KEY (bandid) REFERENCES band (id))");
//qDebug() << Q_FUNC_INFO << ": " << stringQuery;
if (execQuery(Q_FUNC_INFO, stringQuery))
{
//qDebug() << Q_FUNC_INFO << ": Query OK";
logEvent(Q_FUNC_INFO, "END-1", Debug);
return true;
}
else
{
//qDebug() << Q_FUNC_INFO << ": Query NOK";
logEvent(Q_FUNC_INFO, "END-2", Debug);
return false;
}
}
bool DataBase::createDataBase()
{
logEvent(Q_FUNC_INFO, "Start: " + QString::number(constrid) , Debug);
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists log");
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists band");
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists mode");
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists prefixesofentity");
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists continent");
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists entity");
execQuery(Q_FUNC_INFO, "DROP TABLE IF exists softwarecontrol");
QString stringQuery = QString ("CREATE TABLE softwarecontrol ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"dateupgrade VARCHAR(10) NOT NULL, "
"softversion REAL NOT NULL, "
"dbversion REAL NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
if (!createTableBand(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the bands table";
logEvent(Q_FUNC_INFO, "END-1", Debug);
return false;
}
if (!populateTableBand(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate the bands table";
logEvent(Q_FUNC_INFO, "END-2", Debug);
return false;
}
if (!createTableMode(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the modes table";
return false;
}
if (!populateTableMode(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate the modes table";
logEvent(Q_FUNC_INFO, "END-3", Debug);
return false;
}
if (!createTableSatellites(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the satellites table";
logEvent(Q_FUNC_INFO, "END-4", Debug);
return false;
}
if (!populateTableSatellites(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate the satellites table";
logEvent(Q_FUNC_INFO, "END-5", Debug);
return false;
}
if (!createTableLog(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the log table";
logEvent(Q_FUNC_INFO, "END-6", Debug);
return false;
}
if (!createTableEntity(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the entity table";
logEvent(Q_FUNC_INFO, "END-7", Debug);
return false;
}
if (!createTablePrimarySubdivisions(true))
return false;
//http://www.sqlite.org/lang_datefunc.html
/*
"confirmed INTEGER NOT NULL, "
confirmed means:
confirmed = 0 Set as Worked
confirmed = 1 Set as Confirmed
*/
stringQuery = QString("CREATE TABLE continent ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(2) NOT NULL, "
"name VARCHAR(15) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
stringQuery = QString("CREATE TABLE ant_path_enumeration ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(1) NOT NULL, "
"name VARCHAR(15) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
stringQuery = QString("CREATE TABLE arrl_sect_enumeration ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(2) NOT NULL, "
"name VARCHAR(30) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
stringQuery = QString("CREATE TABLE qso_complete_enumeration ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(3) NOT NULL, "
"name VARCHAR(10) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
createTableContest();
stringQuery = QString("CREATE TABLE contestcategory ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(20) NOT NULL, "
"name VARCHAR(40) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
stringQuery = QString("CREATE TABLE award_enumeration ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR(15) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
stringQuery = QString("CREATE TABLE prefixesofentity ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"prefix VARCHAR(15) NOT NULL,"
"dxcc INTEGER NOT NULL,"
"cqz INTEGER NOT NULL,"
"ituz INTEGER NOT NULL,"
"UNIQUE (prefix, dxcc), "
"FOREIGN KEY (dxcc) REFERENCES entity (dxcc) )");
execQuery(Q_FUNC_INFO, stringQuery);
createTableAwardDXCC();
createTableAwardWAZ();
stringQuery = QString("CREATE TABLE qsl_rec_status ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(1) NOT NULL, "
"name VARCHAR(15) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
stringQuery = QString("CREATE TABLE qsl_sent_status ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"shortname VARCHAR(1) NOT NULL, "
"name VARCHAR(15) NOT NULL)");
execQuery(Q_FUNC_INFO, stringQuery);
if (!createTableQSL_Via_enumeration())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the qsl_via_enumeration table";
logEvent(Q_FUNC_INFO, "END-9", Debug);
return false;
}
if (!populateTableQSL_Via_enumeration())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate the qsl_via_enumeration table";
logEvent(Q_FUNC_INFO, "END-10", Debug);
return false;
}
if (!createTablePropModes())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the propModes table";
logEvent(Q_FUNC_INFO, "END-11", Debug);
return false;
}
if (!createTableLogs(true))
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the logs table";
logEvent(Q_FUNC_INFO, "END-12", Debug);
return false;
}
if (!createTableClubLogStatus())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to create the clublogstatus table";
logEvent(Q_FUNC_INFO, "END-13", Debug);
return false;
}
if (!populateTableClubLogStatus())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate clublog status table";
logEvent(Q_FUNC_INFO, "END-14", Debug);
return false;
}
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_sent_status (shortname, name) VALUES ('Y', 'Yes')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_sent_status (shortname, name) VALUES ('N', 'No')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_sent_status (shortname, name) VALUES ('R', 'Requested')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_sent_status (shortname, name) VALUES ('Q', 'Queued')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_sent_status (shortname, name) VALUES ('I', 'Ignore/Invalid')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_rec_status (shortname, name) VALUES ('Y', 'Yes')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_rec_status (shortname, name) VALUES ('N', 'No')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_rec_status (shortname, name) VALUES ('R', 'Requested')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_rec_status (shortname, name) VALUES ('I', 'Ignore/Invalid')");
execQuery(Q_FUNC_INFO, "INSERT INTO qsl_rec_status (shortname, name) VALUES ('V', 'Validated')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('AF', 'Africa')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('AS', 'Asia')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('EU', 'Europe')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('NA', 'North America')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('OC', 'Oceania')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('SA', 'South America')");
execQuery(Q_FUNC_INFO, "INSERT INTO continent (shortname, name) VALUES ('AN', 'Antartica')");
if (!populateContestData())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate contestData table";
logEvent(Q_FUNC_INFO, "END-15", Debug);
return false;
}
if (!populatePropagationModes())
{
//qDebug() << Q_FUNC_INFO << ": Not possible to populate propagation modes table";
logEvent(Q_FUNC_INFO, "END-16", Debug);
return false;
}
execQuery(Q_FUNC_INFO, "INSERT INTO ant_path_enumeration (shortname, name) VALUES ('G', 'GrayLine')");
execQuery(Q_FUNC_INFO, "INSERT INTO ant_path_enumeration (shortname, name) VALUES ('O', 'Other')");
execQuery(Q_FUNC_INFO, "INSERT INTO ant_path_enumeration (shortname, name) VALUES ('S', 'ShortPath')");
execQuery(Q_FUNC_INFO, "INSERT INTO ant_path_enumeration (shortname, name) VALUES ('L', 'LongPath')");
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('AL', 'Alabama')");
/*
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('AK', 'Alaska')");
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('AB', 'Alberta')");
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('AR', 'Arkansas')");
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('AZ', 'Arizona')");
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('BC', 'British Columbia')");
execQuery(Q_FUNC_INFO, "INSERT INTO arrl_sect_enumeration (shortname, name) VALUES ('CO', 'Colorado')");
*/
//TODO: Awards are deprecated
execQuery(Q_FUNC_INFO, "INSERT INTO award_enumeration (name) VALUES ('AJA')");
execQuery(Q_FUNC_INFO, "INSERT INTO award_enumeration (name) VALUES ('CQDX')");
execQuery(Q_FUNC_INFO, "INSERT INTO award_enumeration (name) VALUES ('CQDXFIELD')");
execQuery(Q_FUNC_INFO, "INSERT INTO award_enumeration (name) VALUES ('DXCC')");
execQuery(Q_FUNC_INFO, "INSERT INTO qso_complete_enumeration (shortname, name) VALUES ('Y', 'Yes')");
execQuery(Q_FUNC_INFO, "INSERT INTO qso_complete_enumeration (shortname, name) VALUES ('N', 'No')");
execQuery(Q_FUNC_INFO, "INSERT INTO qso_complete_enumeration (shortname, name) VALUES ('NIL', 'Not heard')");
execQuery(Q_FUNC_INFO, "INSERT INTO qso_complete_enumeration (shortname, name) VALUES ('?', 'Uncertain')");
if (updateDBVersion(softVersion, QString::number(DBVersionf)))
{ // It was not possible to save the DB version
//qDebug() << Q_FUNC_INFO << ": Not possible to update the DB version";
logEvent(Q_FUNC_INFO, "END-17", Debug);
return false;
}
logEvent(Q_FUNC_INFO, "END", Debug);
return true;
}
bool DataBase::recreateTableDXCC()
{
logEvent(Q_FUNC_INFO, "Start", Debug);
QSqlQuery query ("DROP TABLE awarddxcc");
if (!query.exec())
{
//qDebug() << Q_FUNC_INFO << ": awarddxcc NOT dropped";
//qDebug() << Q_FUNC_INFO << ": " << query.lastQuery();
//qDebug() << Q_FUNC_INFO << ": " << query.lastError();
query.finish();
return false;
}
query.finish();
return createTableAwardDXCC();
logEvent(Q_FUNC_INFO, "END", Debug);
return true;
}
bool DataBase::createTableAwardDXCC()
{
return execQuery(Q_FUNC_INFO, "CREATE TABLE awarddxcc ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"dxcc INTEGER NOT NULL,"
"band INTEGER NOT NULL, "
"mode INTEGER NOT NULL, "
"confirmed INTEGER, "
"qsoid INTEGER NOT NULL, "
"lognumber INTEGER, "
"UNIQUE (dxcc, band, mode, lognumber))");
/*
"UNIQUE (dxcc, band, mode, lognumber), "
"FOREIGN KEY (dxcc) REFERENCES entity (dxcc), "
"FOREIGN KEY (band) REFERENCES band (id), "
"FOREIGN KEY (mode) REFERENCES mode (id), "
"FOREIGN KEY (qsoid) REFERENCES log (id) )");
*/
/*
In awarddxcc confirmed means:
confirmed = 0 Set as Worked
confirmed = 1 Set as Confirmed
*/
}
bool DataBase::recreateTableWAZ()
{
//QSqlQuery query;
if (execQuery(Q_FUNC_INFO, "DROP TABLE awardwaz"))
{
return createTableAwardWAZ();
}
return true;
}
bool DataBase::createTableAwardWAZ()
{
return execQuery(Q_FUNC_INFO, "CREATE TABLE awardwaz ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"cqz INTEGER NOT NULL,"
"band INTEGER NOT NULL, "
"mode INTEGER NOT NULL, "
"confirmed INTEGER, "
"qsoid INTEGER NOT NULL, "
"lognumber INTEGER, "
"UNIQUE (cqz, band, mode, lognumber), "
"FOREIGN KEY (band) REFERENCES band (id), "
"FOREIGN KEY (mode) REFERENCES mode (id), "
"FOREIGN KEY (qsoid) REFERENCES log (id))");
/*
In awardwaz confirmed means:
confirmed = 0 Set as Worked
confirmed = 1 Set as Confirmed
*/
}
bool DataBase::createTablePrimarySubdivisions(const bool NoTmp)
{
// NoTmp = false => TMP data table to operate and be deleted afterwards
//qDebug() << Q_FUNC_INFO ;
/*
* prefnumber (id autoincrement)
* subdivision main entity (arrlid) : dxcc : 281
* subdivision main prefix : prefix : EA4
* subdivision shortname : shortname : M
* subdivision name : name : Madrid
* subdivision group : regionalgroup : Comunidad de Madrid
* subdivisional id : regionalid : -1 (oblast for Russia)
* subdivision CQ (if different) : cqz : 14
* subdivision ITU (if different) : ituz : 37
* subdivision deleted : deleted : N
* subdivision start_date : start_date : N/A
* subdivision end_date : end_date : N/A
*
*/
QString stringQuery = QString();
QString table = QString();
if (NoTmp)
{
table = "primary_subdivisions" ;
}
else
{
table = "primary_subdivisionstemp" ;
}
stringQuery = "CREATE TABLE "+ table;