klog
database.h
1 #ifndef KLOG_DATABASE_H
2 #define KLOG_DATABASE_H
3 /***************************************************************************
4  database.h - description
5  -------------------
6  begin : sept 2011
7  copyright : (C) 2011 by Jaime Robles
8  email : jaime@robles.es
9  ***************************************************************************/
10 
11 /*****************************************************************************
12  * This file is part of KLog. *
13  * *
14  * KLog is free software: you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation, either version 3 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * KLog is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with KLog. If not, see <https://www.gnu.org/licenses/>. *
26  * *
27  *****************************************************************************/
28 
29 #include <QObject>
30 //#include<QtCore/QtCore>
31 //#include<QtCore/QObject>
32 #include <QSqlDatabase>
33 #include <QMessageBox>
34 #include <QSqlQuery>
35 #include <QtSql>
36 #include <QString>
37 #include <QDateTime>
38 #include <QInputDialog>
39 #include <QProgressDialog>
40 #include "queryexecutor.h"
41 #include "../utilities.h"
42 #include "../frequency.h"
43 #include "../global.h"
44 #include "../klogdefinitions.h"
45 
46 class QSqlRelationalTableModel;
47 const float DBVersionf = 0.028f; // This is the latest version of the DB.
48 
49 class DataBase : public QObject
50 {
51  Q_OBJECT
52  friend class tst_DataBase;
53 
54 public:
55  DataBase(const QString &_parentClass, const QString &_DBName);
56  DataBase(const QString &_parentClass, const QString &_softVersion, const QString &_DBName);
57  //virtual ~DataBase();
58  ~DataBase();
59 
60  bool beginTransaction();
61  bool commitTransaction();
62  bool rollbackTransaction();
63 
64  QString getSoftVersion();
65  float getDBVersion();
66  QString getDBName();
67 
68  bool createConnection(const QString &function, bool newDB=false); // If true that means that we are creating the DB,
69  // not just connecting to an existing one.
70  // That will be done in the default path
71  bool reConnect(const QString &_DBName);
72  //bool setDir(const QString &_dir);
73  QStringList getColumnNamesFromTable(const QString &_tableName);
74 
75 
76  //KLOG_DEPRECATED bool isValidBand (const QString &b);
77  //KLOG_DEPRECATED bool isValidMode (const QString &b, const bool _tmp=false);
78 
79  // qString getBandNameFromNumber(const int _n);
80  //QHash<QString, int> getHashTableData(const DataTableHash _data); //Returns a QHash from a Table (Band, Mode, World)
81  // qString getModeNameFromNumber(const int _n, bool _tmp=false);
82  // qString getSubModeNameFromNumber(const int _n, bool _tmp=false);
83 
84  //KLOG_DEPRECATED int getBandIdFromName(const QString &b);
85  //KLOG_DEPRECATED int getModeIdFromName(const QString &b);
86  //KLOG_DEPRECATED int getModeIdFromSubMode(const QString &b);
87 
88 
89  //KLOG_DEPRECATED int getBandIdFromFreq(const QString &fr); //Freq should be in MHz // Move to dataproxy
90  // bool isThisFreqInBand(const QString &b, const QString &fr); //Freq should be in MHz // Move to dataproxy
91 
92  //Frequency getFreqFromBandId(const int _i);
93 
94  //int getLogTypeNumber(const QString &_logType);
95  // qString getLogTypeName(const int _logType);
96 
97  bool unMarkAllQSO();
98  bool updateIfNeeded();
99  void backupB4Update();
100  void logBackup();
101  void compress();
102 
103  QMultiMap<QString, int> fillCountryCodes();
104  bool updateTheEntityTableISONames();
105  bool updateTableLogs();
106 
107  //bool updateAwardWAZTable();
108  int getNumberOfQsos(int _logNumber = -1);
109  int getLastInsertedQSO();
110  void setLogLevel (const DebugLogLevel _b);
111  bool populateTablePrimarySubdivisions();
112  bool isTheDBCreated();
113 //private slots:
114 // void slotPrintErrors(QString _func, QString _msg, int _level);
115 
116 private:
117  bool execQuery(const QString &function, const QString &stringQuery);
118  bool updateEntity (const QString &_codeString, const int _code);
119  bool doesEntityTablehaveData();
120  bool createDataBase();
121  bool setPragma(); // Defines the PRAGMA for the DB
122 
123  bool isTheTableExisting(const QString &_tableName);
124  bool hasTheTableData(const QString &_tableName);
125  bool requiresManualUpgrade();
126  bool updateToLatest();
127  bool updateTo003(); // Updates the DB to 0.0.3
128  bool updateTo004();
129  bool updateTo005();
130  bool updateTo006();
131  bool updateTo007();
132  bool updateTo008();
133  bool updateTo009(); // Updates DB and add the Satellite tables
134  bool updateTo010(); // Updates DB and recreates the supportedcontest table
135  bool updateTo011(); // Updates DB and recreates Satellite data
136  bool updateTo012(); // Updates the flags and so on.
137  bool updateTo013(); // Update the qsl_via_enumeration bug caused for calling the table just qsl_via
138  bool updateTo014(); // Update the Satellite DB
139  bool updateTo015(); // Adds the FT4 mode
140  bool updateTo016(); // Recreates the satellite table to add the QO-100
141  bool updateTo017(); // Merges the qso_date & time_on fields
142  bool updateTo018(); // Creates Subdivision
143  bool updateTo019(); // Adds FTS4 and FST4W modes and RS-44 satellite
144  //bool updateTo020(); // Adds Q65 mode
145  bool updateTo021(); // Adds 5M & 8M bands
146  bool updateTo022(); // Recovers the 020 that was not executed and adds the Q65 mode
147  bool updateTo023(); // Fixes the cabrillo fields in the table band
148  bool updateTo024(); // Fixes the entity table fixinf the DL id
149  bool updateTo025(); // Adds modes from ADIF 3.1.3
150  bool updateTo026(); // KLog-2.4: Recreates entity to make UTC a real & add new ADIF fields
151  bool updateTo027(); // KLog-2.4.2: Recreates entity to make UTC a real & add new ADIF fields
152  bool updateTo028(); // KLog-2.4.3: Adds FT2 submode
153 
154  bool updateTableLog(const int _version);
155  bool updateDBVersion(QString _softV, QString _dbV);
156 
157  bool createTheModeQuickReference();
158 
159  //bool updateLog(); // Updates the log table
160  bool recreateTableLog();
161  bool createTableLog(bool temp = false); // false creates the production DB. True a temporal one.
162  bool createTableLogs(const bool real=true); // real = true creates the production DB. False a temporal one.
163  bool createTableEntity(const bool NoTmp);
164  bool recreateTableEntity();
165 
166  //bool createTableClubLogStatus();
167  //bool populateTableClubLogStatus();
168 
169  bool createTableMode(const bool NoTmp);
170  bool populateTableMode(const bool NoTmp);
171  //bool populateTableModePSK(bool NoTmp); //Refactored from populateTableMode
172  //bool populateTableModeMFSK(bool NoTmp);
173  bool populateTableWithModes(const QStringList& submodes, const QString& mode, const QString &cabrillo, bool NoTmp); // General function for modes
174  bool updateTheModeTableAndSyncLog();
175 
176  bool createTableBand(const bool NoTmp);
177  bool populateTableBand(const bool NoTmp);
178  bool recreateTableBand();
179  bool syncLogQSOsOnBandTableChange();
180 
181  //bool recreateTableDXCC();
182  //bool createTableAwardDXCC();
183  //bool recreateTableWAZ();
184  //bool createTableAwardWAZ();
185 
186  bool createTablePrimarySubdivisions(const bool NoTmp);
187 
188  bool recreateTablePrimarySubdivisions();
189 
190  bool recreateSatelliteData();
191  bool createTableSatellites(const bool NoTmp);
192  bool populateTableSatellites(const bool NoTmp);
193 
194  bool recreatePropModes();
195  bool createTablePropModes();
196  bool populatePropagationModes();
197 
198  bool recreateContestData();
199  bool recreateSupportedContest();
200  bool createTableContest();
201  bool createTableSupportedContest();
202  bool populateContestData();
203 
204  bool populateTableSupportedContest();
205  //bool createAndPopulateQSLSentRecStatus();
206  bool createAndPopulateContinents();
207  bool createAndPopulateAnt_path_enumeration();
208  //bool recreateTableQSO_CompleteEnumeration();
209  //bool createAndPopulateQSO_CompleteEnumeration();
210  bool createAndPopulateAwardEnumeration();
211  bool createAndPopulateARRLSectEnumeration();
212 
213  bool createTableQSL_Via_enumeration();
214  bool populateTableQSL_Via_enumeration();
215 
216  bool howManyQSOsInLog(const int i);
217  //void showError();
218 
219  //bool moveFromModeIdToSubmodeId();
220  bool updateModeIdFromSubModeId();
221  bool updateBandIdTableLogToNewOnes();
222  bool confirmCancellation(QWidget *parent = nullptr);
223 
224  void queryErrorManagement(const QString &_functionFailed, const QString &errorCodeS, const QString &_nativeError, const QString &_failedQuery);
225  void logEvent(const QString &_func, const QString &_msg, DebugLogLevel _level);
226 
227  DebugLogLevel logLevel;
228  bool created;
229  float dbVersion; // The current version of the DB. May differ from latestReaded if we are updating the DB!
230  QString dbConnectionName;
231  QString softVersion;
232  float latestReaded; // The latest version of DB readed in the DB itself
233  //bool inMemoryOnly; // The DB is to be created in memory, no file support... Faster but less safe!
234  // qDateTime date;
235 
236 
237  //KLOG_DEPRECATED QHash<int, QString> bandIDHash;
238  //KLOG_DEPRECATED QHash<int, Frequency> freqBandIDHash; // Validate if it is still needed, move to dataproxy
239 
240 
241  // qMap<QString, int> bandQMap;
242  // qMap<QString, int> modeQMap;
243 
244  Utilities *util;
245  QueryExecutor *exe;
246 
247  QSqlDatabase db;
248  QString dbDir, dbName;
249 
250  QStringList insertPreparedQueries, insertQueryFields;
251  QSqlQuery preparedQuery;
252 
253  int constrid; // Just an id for the constructor to check who is being executed at one specific time
254 
255 signals:
256  //void queryError(QString functionFailed, QString errorCodeS, QString nativeError, QString failedQuery); // To alert about any failed query execution
257  void debugLog (QString _func, QString _msg, DebugLogLevel _level);
258 };
259 
260 #endif // DATABASE_H
261 
Definition: database.h:49
Definition: utilities.h:54
Definition: queryexecutor.h:38
Definition: tst_database.cpp:46