openPilotLog
adatabase.h
1 /*
2  *openPilotLog - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020-2021 Felix Turowsky
4  *
5  *This program is free software: you can redistribute it and/or modify
6  *it under the terms of the GNU General Public License as published by
7  *the Free Software Foundation, either version 3 of the License, or
8  *(at your option) any later version.
9  *
10  *This program is distributed in the hope that it will be useful,
11  *but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *GNU General Public License for more details.
14  *
15  *You should have received a copy of the GNU General Public License
16  *along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 #ifndef ADATABASE_H
19 #define ADATABASE_H
20 
21 #include <QPair>
22 #include <QMap>
23 #include <QString>
24 #include <QDir>
25 #include <QSqlDatabase>
26 #include <QSqlDriver>
27 #include <QSqlQuery>
28 #include <QSqlError>
29 #include <QSqlTableModel>
30 #include <QSqlQuery>
31 #include <QSqlRecord>
32 #include <QSqlField>
33 
34 #include "src/database/adatabasetypes.h"
35 #include "src/classes/aentry.h"
36 #include "src/classes/apilotentry.h"
37 #include "src/classes/atailentry.h"
38 #include "src/classes/aaircraftentry.h"
39 #include "src/classes/aflightentry.h"
40 #include "src/classes/astandardpaths.h"
41 #include "src/classes/acurrencyentry.h"
42 
43 #define SQLITE_DRIVER QStringLiteral("QSQLITE")
44 
52 #define aDB ADatabase::instance()
53 
58 enum class ADatabaseTarget
59 {
60  aircraft,
61  airport_identifier_icao,
62  airport_identifier_iata,
63  airport_identifier_all,
64  airport_names,
65  pilots,
66  registrations,
67  companies,
68  tails
69 };
70 
74 enum class ADatabaseSummaryKey {
75  total_flights,
76  total_tails,
77  total_pilots,
78  last_flight,
79  total_time,
80 };
81 
86 class ADatabaseError : public QSqlError {
87 public:
88  ADatabaseError() = default;
89  ADatabaseError(QString msg);
90  QString text() const;
91 };
92 
97 class ADatabase : public QObject {
98  Q_OBJECT
99 
100 private:
101  static ADatabase* self;
102  TableNames_T tableNames;
103  TableColumns_T tableColumns;
104  int databaseRevision;
105 
106  ADatabase();
107  int checkDbVersion() const;
108 public:
114 
115  const QFileInfo databaseFile;
116 
117  // Ensure DB is not copiable or assignable
118  ADatabase(const ADatabase&) = delete;
119  void operator=(const ADatabase&) = delete;
120  static ADatabase* instance();
121 
127  int dbRevision() const;
128 
132  TableNames_T getTableNames() const;
133 
137  ColumnNames_T getTableColumns(TableName_T table_name) const;
138 
143  void updateLayout();
144 
149  const QString sqliteVersion() const;
150 
154  bool connect();
155 
159  void disconnect();
160 
165  static QSqlDatabase database();
166 
172  QVector<QVariant> customQuery(QString statement, int return_values);
173 
177  bool exists(AEntry entry);
178  bool exists(DataPosition data_position);
179 
184  bool commit(AEntry entry);
185 
189  bool insert(AEntry new_entry);
190 
194  bool update(AEntry updated_entry);
195 
199  bool remove(AEntry entry);
200 
205  bool removeMany(QList<DataPosition>);
206 
210  RowData_T getEntryData(DataPosition data_position);
211 
215  AEntry getEntry(DataPosition data_position);
216 
225  APilotEntry getPilotEntry(RowId_T row_id);
226 
235  ATailEntry getTailEntry(RowId_T row_id);
236 
245  AAircraftEntry getAircraftEntry(RowId_T row_id);
246 
255  AFlightEntry getFlightEntry(RowId_T row_id);
256 
260  ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name);
261 
266  const QStringList getCompletionList(ADatabaseTarget target);
267 
273  const QMap<QString, RowId_T> getIdMap(ADatabaseTarget target);
274 
278  int getLastEntry(ADatabaseTarget target);
279 
284  QList<RowId_T> getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTarget target);
285 
290  APilotEntry resolveForeignPilot(RowId_T foreign_key);
291 
296  ATailEntry resolveForeignTail(RowId_T foreign_key);
297 
306  QMap<ADatabaseSummaryKey, QString> databaseSummary(const QString& db_path);
307 
311  const QString databaseSummaryString(const QString& db_path);
312 
313  bool restoreBackup(const QString& backup_file);
314  bool createBackup(const QString& dest_file);
315 
316 
317 signals:
330 };
331 
332 #endif // ADATABASE_H
ADatabase::exists
bool exists(AEntry entry)
Checks if an entry exists in the database, based on position data.
Definition: adatabase.cpp:229
ADatabase::restoreBackup
bool restoreBackup(const QString &backup_file)
ADatabase::restoreBackup restores the database from a given backup file and replaces the currently ac...
Definition: adatabase.cpp:766
ADatabase::updateLayout
void updateLayout()
Updates the member variables tableNames and tableColumns with up-to-date layout information if the da...
Definition: adatabase.cpp:65
ADatabase::databaseSummaryString
const QString databaseSummaryString(const QString &db_path)
returns a short summary string of the database, containing total time and date of last flight.
Definition: adatabase.cpp:726
ADatabase::resolveForeignPilot
APilotEntry resolveForeignPilot(RowId_T foreign_key)
Resolves the foreign key in a flight entry.
Definition: adatabase.cpp:630
ADatabase::getCompletionList
const QStringList getCompletionList(ADatabaseTarget target)
getCompletionList returns a QStringList of values for a QCompleter based on database values
Definition: adatabase.cpp:470
ADatabase::disconnect
void disconnect()
closes the database connection.
Definition: adatabase.cpp:131
AFlightEntry
Definition: aflightentry.h:23
ADatabase::getPilotEntry
APilotEntry getPilotEntry(RowId_T row_id)
retreives a PilotEntry from the database.
Definition: adatabase.cpp:435
ADatabaseError
Custom Database Error derived from QSqlError. Extends text() adding "Database Error: " before the tex...
Definition: adatabase.h:86
APilotEntry
Definition: apilotentry.h:24
ADatabase::connect
bool connect()
Connect to the database and populate database information.
Definition: adatabase.cpp:103
ADatabase::createBackup
bool createBackup(const QString &dest_file)
ADatabase::createBackup copies the currently used database to an external backup location provided by...
Definition: adatabase.cpp:744
ADatabase::connectionReset
void connectionReset()
connectionReset is emitted whenever the database connection is reset, for example when creating or re...
ADatabase::sqliteVersion
const QString sqliteVersion() const
ADatabase::sqliteVersion returns the database sqlite version. See also dbRevision()
Definition: adatabase.cpp:94
ADatabase::dataBaseUpdated
void dataBaseUpdated()
updated is emitted whenever the database contents have been updated. This can be either a commit,...
ADatabase::customQuery
QVector< QVariant > customQuery(QString statement, int return_values)
Can be used to send a complex query to the database.
Definition: adatabase.cpp:640
ADatabase::databaseSummary
QMap< ADatabaseSummaryKey, QString > databaseSummary(const QString &db_path)
Return a summary of a database.
Definition: adatabase.cpp:665
ADatabase::getEntry
AEntry getEntry(DataPosition data_position)
retreive an Entry from the database.
Definition: adatabase.cpp:428
ADatabase::getTailEntry
ATailEntry getTailEntry(RowId_T row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:442
ADatabase::getFlightEntry
AFlightEntry getFlightEntry(RowId_T row_id)
retreives a flight entry from the database.
Definition: adatabase.cpp:456
ADatabase::getForeignKeyConstraints
QList< RowId_T > getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTarget target)
returns a list of ROWID's in the flights table for which foreign key constraints exist.
Definition: adatabase.cpp:593
ADatabase::getAircraftEntry
AAircraftEntry getAircraftEntry(RowId_T row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:449
DataPosition
Definition: adatabasetypes.h:44
ATailEntry
Definition: atailentry.h:24
ADatabase::database
static QSqlDatabase database()
Can be used to access the database connection.
Definition: adatabase.cpp:138
ADatabase
The DB class encapsulates the SQL database by providing fast access to hot database data.
Definition: adatabase.h:97
ADatabase::remove
bool remove(AEntry entry)
deletes an entry from the database.
Definition: adatabase.cpp:152
ADatabase::getLastEntry
int getLastEntry(ADatabaseTarget target)
returns the ROWID for the newest entry in the respective database.
Definition: adatabase.cpp:565
ADatabase::getTableNames
TableNames_T getTableNames() const
Return the names of all tables in the database.
Definition: adatabase.cpp:60
ADatabase::getEntryData
RowData_T getEntryData(DataPosition data_position)
retreive entry data from the database to create an entry object
Definition: adatabase.cpp:371
ADatabase::removeMany
bool removeMany(QList< DataPosition >)
deletes a list of entries from the database. Optimised for speed when deleting many entries.
Definition: adatabase.cpp:183
ADatabase::getCurrencyEntry
ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name)
Retreives a currency entry from the database.
Definition: adatabase.cpp:463
AAircraftEntry
Definition: aaircraftentry.h:24
ACurrencyEntry
Definition: acurrencyentry.h:8
ADatabase::resolveForeignTail
ATailEntry resolveForeignTail(RowId_T foreign_key)
Resolves the foreign key in a flight entry.
Definition: adatabase.cpp:635
ADatabase::dbRevision
int dbRevision() const
dbRevision returns the database Revision Number. The Revision refers to what iteration of the databas...
Definition: adatabase.cpp:43
ADatabase::getIdMap
const QMap< QString, RowId_T > getIdMap(ADatabaseTarget target)
returns a QMap<QString, RowId_t> of a human-readable database value and its row id....
Definition: adatabase.cpp:519
ADatabase::commit
bool commit(AEntry entry)
commits an entry to the database, calls either insert or update, based on position data
Definition: adatabase.cpp:143
ADatabase::lastError
ADatabaseError lastError
lastError extends QSqlError. Holds information about the last error that ocurred during a SQL operati...
Definition: adatabase.h:113
AEntry
The Entry class encapsulates table metadata(table name, row id) and data for new and existing entries...
Definition: aentry.h:40
ADatabase::insert
bool insert(AEntry new_entry)
Create new entry in the databse based on UserInput.
Definition: adatabase.cpp:325
ADatabase::getTableColumns
ColumnNames_T getTableColumns(TableName_T table_name) const
Return the names of a given table in the database.
Definition: adatabase.cpp:55
ADatabase::update
bool update(AEntry updated_entry)
Updates entry in database from existing entry tweaked by the user.
Definition: adatabase.cpp:289