klog/database.h

252 lines
9.1 KiB
C
Raw Permalink Normal View History

2015-01-14 18:51:30 +00:00
#ifndef DATABASE_H
#define DATABASE_H
/***************************************************************************
database.h - description
-------------------
begin : sept 2011
copyright : (C) 2011 by Jaime Robles
email : jaime@robles.es
***************************************************************************/
/*****************************************************************************
2015-11-23 17:36:29 +00:00
* This file is part of KLog. *
2015-01-14 18:51:30 +00:00
* *
2015-11-23 17:36:29 +00:00
* KLog is free software: you can redistribute it and/or modify *
2015-01-14 18:51:30 +00:00
* 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. *
* *
2015-11-23 17:36:29 +00:00
* KLog is distributed in the hope that it will be useful, *
2015-01-14 18:51:30 +00:00
* 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 *
2015-11-23 17:36:29 +00:00
* along with KLog. If not, see <http://www.gnu.org/licenses/>. *
2015-01-14 18:51:30 +00:00
* *
*****************************************************************************/
2015-04-20 17:55:01 +00:00
#include <QObject>
2015-01-14 18:51:30 +00:00
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <QtSql>
#include <QString>
#include <QDateTime>
2015-04-20 17:55:01 +00:00
#include <QInputDialog>
2015-07-19 18:26:11 +00:00
#include <QProgressDialog>
#include "utilities.h"
2015-01-14 18:51:30 +00:00
class QSqlRelationalTableModel;
2017-12-01 14:06:54 +00:00
// Previous db update 0.011
2018-09-14 16:09:25 +00:00
const float DBVersionf = 0.014; // TODO:
2017-10-17 15:59:56 +00:00
2017-12-23 22:14:15 +00:00
struct AwarddxccEntry
2017-12-31 16:23:23 +00:00
{ // Information to update the awarddxcc table; For other Award tables, the DXCC is just the ID of the award element
2017-12-23 22:14:15 +00:00
QString dxcc;
QString band;
QString mode;
QString status;
QString logNumber;
QString qsoID;
};
struct AwarddxccEntryCheck
2017-12-31 16:23:23 +00:00
{ // Information to update the awarddxcc table; For other Award tables, the DXCC is just the ID of the award element
2017-12-23 22:14:15 +00:00
QString dxcc;
QString band;
QString mode;
QString status;
QString logNumber;
};
class DataBase
{
2015-01-14 18:51:30 +00:00
public:
2018-01-16 17:08:47 +00:00
DataBase(const QString _parentClass, const QString _DBName);
//DataBase(const QString _softVersion, bool inmemoryonly = false);
2018-01-16 17:08:47 +00:00
DataBase(const QString _parentClass, const QString _softVersion, const QString _DBName);
2015-01-14 18:51:30 +00:00
~DataBase();
2017-12-01 14:06:54 +00:00
QString getSoftVersion();
QString getDBVersion();
2018-01-16 17:08:47 +00:00
QString getDBName();
2017-12-01 14:06:54 +00:00
2017-07-21 09:57:17 +00:00
bool createConnection(bool newDB=false); // If true that means that we are creating the DB,
// not just connecting to an existing one.
// That will be done in the default path
2018-01-16 17:08:47 +00:00
bool reConnect(const QString _DBName);
//bool setDir(const QString _dir);
2017-12-01 14:06:54 +00:00
QStringList getColumnNamesFromTable(const QString _tableName);
2015-01-14 18:51:30 +00:00
bool isValidBand (const QString b);
2018-01-16 17:08:47 +00:00
bool isValidMode (const QString b, const bool _tmp=false);
2015-01-14 18:51:30 +00:00
bool isValidBandNumber (const int b);
bool isValidModeNumber (const int b);
QString getBandNameFromNumber(const int _n);
2017-10-09 16:36:21 +00:00
QString getModeNameFromNumber(const int _n, bool _tmp=false);
QString getSubModeNameFromNumber(const int _n, bool _tmp=false);
2015-01-14 18:51:30 +00:00
int getBandIdFromName(const QString b);
int getModeIdFromName(const QString b);
int getModeIdFromSubMode(const QString b);
2015-01-14 18:51:30 +00:00
bool createBandModeMaps();
int getBandIDFromName2(const QString b);
int getModeIDFromName2(const QString b);
2017-10-09 16:36:21 +00:00
int getSubModeIDFromName2(const QString b);
2015-01-14 18:51:30 +00:00
QString getBandNameFromID2(const int _i);
QString getModeNameFromID2(const int _i);
2017-10-09 16:36:21 +00:00
QString getSubModeNameFromID2(const int _i);
2015-01-14 18:51:30 +00:00
int getBandIdFromFreq(const QString fr); //Freq should be in MHz
bool isThisFreqInBand(const QString b, const QString fr); //Freq should be in MHz
2015-01-14 18:51:30 +00:00
2015-11-23 17:36:29 +00:00
QString getFreqFromBandId(const int _i);
2015-01-18 20:04:46 +00:00
2017-07-03 21:44:26 +00:00
int getLogTypeNumber(const QString _logType);
QString getLogTypeName(const int _logType);
2015-01-14 18:51:30 +00:00
bool unMarkAllQSO();
bool updateIfNeeded();
void compress();
bool updateTheEntityTableISONames();
2016-08-16 15:21:15 +00:00
bool updateTableLogs();
2015-01-14 18:51:30 +00:00
2017-12-01 14:06:54 +00:00
bool queryAddField(const QString _field, const QString value);
2017-12-23 22:14:15 +00:00
//bool queryPrepare();
//bool queryExec();
2017-12-01 14:06:54 +00:00
bool queryPrepare(const QString _query);
bool queryBind(const QString _field, const QString value);
2017-12-23 22:14:15 +00:00
bool updateAwardDXCCTable();
2017-12-31 16:23:23 +00:00
bool updateAwardWAZTable();
2017-12-23 22:14:15 +00:00
int getNumberOfQsos(const int _logNumber);
2017-12-01 14:06:54 +00:00
2015-01-14 18:51:30 +00:00
private:
2017-10-31 17:10:03 +00:00
bool beginTransaction();
bool commitTransaction();
bool execQuery(const QString function, const QString stringQuery);
2015-01-14 18:51:30 +00:00
bool createDataBase();
bool isTheDBCreated();
2018-07-15 18:00:37 +00:00
bool isTheTableExisting(const QString _tableName);
bool hasTheTableData(const QString _tableName);
2015-01-14 18:51:30 +00:00
bool updateToLatest();
bool updateTo003(); // Updates the DB to 0.0.3
bool updateTo004();
2015-02-23 19:25:55 +00:00
bool updateTo005();
2015-05-03 11:19:18 +00:00
bool updateTo006();
2015-11-14 11:07:54 +00:00
bool updateTo007();
2016-01-09 11:35:01 +00:00
bool updateTo008();
2016-07-31 12:41:34 +00:00
bool updateTo009(); // Updates DB and add the Satellite tables
2017-07-03 21:44:26 +00:00
bool updateTo010(); // Updates DB and recreates the supportedcontest table
2017-10-17 15:59:56 +00:00
bool updateTo011(); // Updates DB and recreates Satellite data
2018-02-03 13:26:51 +00:00
bool updateTo012(); // Updates the flags and so on.
2018-07-05 20:34:39 +00:00
bool updateTo013(); // Update the qsl_via_enumeration bug caused for calling the table just qsl_via
2018-09-14 16:09:25 +00:00
bool updateTo014(); // Update the Satellite DB
2015-07-11 10:41:41 +00:00
bool updateTableLog(const int _v);
2018-07-15 18:00:37 +00:00
bool updateDBVersion(QString _softV, QString _dbV);
2015-06-13 11:12:15 +00:00
2015-01-14 18:51:30 +00:00
bool createTheBandQuickReference();
bool createTheModeQuickReference();
2015-03-01 19:32:29 +00:00
//bool updateLog(); // Updates the log table
2017-12-01 14:06:54 +00:00
bool recreateTableLog();
2016-08-16 15:21:15 +00:00
bool createTableLog(bool temp = false); // false creates the production DB. True a temporal one.
bool createTableLogs(const bool real=true); // real = true creates the production DB. False a temporal one.
bool createTableEntity(const bool NoTmp);
bool updateTableEntity();
2015-05-23 18:34:30 +00:00
bool createTableClubLogStatus();
bool populateTableClubLogStatus();
2016-01-09 11:35:01 +00:00
2015-07-11 10:41:41 +00:00
bool createTableMode(const bool NoTmp);
bool populateTableMode(const bool NoTmp);
2016-01-09 11:35:01 +00:00
bool updateTheModeTableAndSyncLog();
2015-07-11 10:41:41 +00:00
bool createTableBand(const bool NoTmp);
bool populateTableBand(const bool NoTmp);
2017-09-26 17:17:05 +00:00
bool recreateTableBand();
2015-03-01 19:32:29 +00:00
2017-12-31 16:23:23 +00:00
bool recreateTableDXCC();
bool createTableAwardDXCC();
bool recreateTableWAZ();
bool createTableAwardWAZ();
2017-07-06 15:05:43 +00:00
bool recreateSatelliteData();
2016-07-31 12:41:34 +00:00
bool createTableSatellites(const bool NoTmp);
bool populateTableSatellites(const bool NoTmp);
2017-09-20 16:18:16 +00:00
bool recreatePropModes();
bool createTablePropModes();
bool populatePropagationModes();
2015-02-28 20:50:49 +00:00
bool recreateContestData();
2017-07-03 21:44:26 +00:00
bool recreateSupportedContest();
2015-02-28 20:50:49 +00:00
bool createTableContest();
2017-07-03 21:44:26 +00:00
bool createTableSupportedContest();
2015-02-23 19:25:55 +00:00
bool populateContestData();
2017-09-20 16:18:16 +00:00
2017-07-03 21:44:26 +00:00
bool populateTableSupportedContest();
2015-05-03 11:19:18 +00:00
2018-07-05 20:34:39 +00:00
bool createTableQSL_Via_enumeration();
bool populateTableQSL_Via_enumeration();
bool howManyQSOsInLog(const int i);
2017-12-01 14:06:54 +00:00
//void showError();
2015-02-23 19:25:55 +00:00
//bool moveFromModeIdToSubmodeId();
2015-07-11 10:41:41 +00:00
bool updateModeIdFromSubModeId();
bool updateBandIdTableLogToNewOnes();
bool updateBandIdTableAward(const int _db);
bool updateModeIdTableAward(const int _db);
2017-12-23 22:14:15 +00:00
2017-10-31 17:10:03 +00:00
void queryErrorManagement(QString functionFailed, QString errorCodeS, int errorCodeN, QString failedQuery);
2017-10-17 15:59:56 +00:00
2015-07-11 10:41:41 +00:00
bool created;
2015-01-14 18:51:30 +00:00
float dbVersion; // The current version of the DB. May differ from latestReaded if we are updating the DB!
2017-12-01 14:06:54 +00:00
QString dbConnectionName;
2015-01-14 18:51:30 +00:00
QString softVersion;
float latestReaded; // The latest version of DB readed in the DB itself
2018-01-16 17:08:47 +00:00
//bool inMemoryOnly; // The DB is to be created in memory, no file support... Faster but less safe!
2017-07-06 15:05:43 +00:00
//QDateTime date;
2015-01-14 18:51:30 +00:00
QHash<QString, int> bandIDHash;
QHash<QString, int> modeIDHash;
2017-10-09 16:36:21 +00:00
QHash<QString, int> subModeIDHash;
2015-01-14 18:51:30 +00:00
QHash<int, QString> IDBandHash;
QHash<int, QString> IDModeHash;
2017-10-09 16:36:21 +00:00
QHash<int, QString> IDSubModeHash;
2015-11-23 17:36:29 +00:00
QHash<int, QString> freqBandIdHash;
2015-01-18 20:04:46 +00:00
2015-01-14 18:51:30 +00:00
QMap<QString, int> bandQMap;
QMap<QString, int> modeQMap;
2015-07-19 18:26:11 +00:00
Utilities *util;
2015-01-18 20:04:46 +00:00
2015-11-06 19:47:52 +00:00
QSqlDatabase db;
2017-12-01 14:06:54 +00:00
QString dbDir, dbName;
QStringList insertPreparedQueries, insertQueryFields;
QSqlQuery preparedQuery;
2015-01-14 18:51:30 +00:00
2017-12-31 16:23:23 +00:00
int constrid; // Just an id for the constructor to check who is being executed at one specific time
2017-10-02 16:10:58 +00:00
signals:
2017-10-31 17:10:03 +00:00
void queryError(QString functionFailed, QString errorCodeS, int errorCodeN, QString failedQuery); // To alert about any failed query execution
2017-10-02 16:10:58 +00:00
2017-09-29 06:16:04 +00:00
2015-01-14 18:51:30 +00:00
};
#endif // DATABASE_H