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  max_doft,
79  total_time,
80 };
81 
82 // [G]: This is how we should handle custom "events" in the program.
83 // In this case a custom error doesnt need to be built from scratch.
84 // Find the type of error you want and extend it with a few tweaks.
89 class ADatabaseError : public QSqlError {
90 public:
91  ADatabaseError() = default;
92  ADatabaseError(QString msg);
93  QString text() const;
94 };
95 
100 class ADatabase : public QObject {
101  Q_OBJECT
102 
103 private:
104  static ADatabase* self;
105  TableNames_T tableNames;
106  TableColumns_T tableColumns;
107  int databaseVersion;
108 
109  ADatabase();
110  int checkDbVersion() const;
111 public:
112  ADatabaseError lastError;
113  //const QDir databaseDir;
114  const QFileInfo databaseFile;
115 
116  // Ensure DB is not copiable or assignable
117  ADatabase(const ADatabase&) = delete;
118  void operator=(const ADatabase&) = delete;
119  static ADatabase* instance();
120 
121  int dbVersion() const;
122 
126  TableNames_T getTableNames() const;
127 
131  ColumnNames_T getTableColumns(TableName_T table_name) const;
132 
137  void updateLayout();
138 
143  const QString sqliteVersion() const;
144 
148  bool connect();
149 
153  void disconnect();
154 
159  static QSqlDatabase database();
160 
166  QVector<QVariant> customQuery(QString statement, int return_values);
167 
171  bool exists(AEntry entry);
172  bool exists(DataPosition data_position);
173 
178  bool commit(AEntry entry);
179 
183  bool insert(AEntry new_entry);
184 
188  bool update(AEntry updated_entry);
189 
193  bool remove(AEntry entry);
194 
199  bool removeMany(QList<DataPosition>);
200 
204  RowData_T getEntryData(DataPosition data_position);
205 
209  AEntry getEntry(DataPosition data_position);
210 
219  APilotEntry getPilotEntry(RowId_T row_id);
220 
229  ATailEntry getTailEntry(RowId_T row_id);
230 
239  AAircraftEntry getAircraftEntry(RowId_T row_id);
240 
249  AFlightEntry getFlightEntry(RowId_T row_id);
250 
254  ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name);
255 
260  const QStringList getCompletionList(ADatabaseTarget target);
261 
267  const QMap<QString, RowId_T> getIdMap(ADatabaseTarget target);
268 
272  int getLastEntry(ADatabaseTarget target);
273 
278  QList<RowId_T> getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTarget target);
279 
284  APilotEntry resolveForeignPilot(RowId_T foreign_key);
285 
290  ATailEntry resolveForeignTail(RowId_T foreign_key);
291 
299  QMap<ADatabaseSummaryKey, QString> databaseSummary(const QString& db_path);
300 
301  bool restoreBackup(const QString& backup_file);
302  bool createBackup(const QString& dest_file);
303 
304 
305 signals:
318 };
319 
320 #endif // ADATABASE_H
ADatabase::exists
bool exists(AEntry entry)
Checks if an entry exists in the database, based on position data.
Definition: adatabase.cpp:220
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:752
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::resolveForeignPilot
APilotEntry resolveForeignPilot(RowId_T foreign_key)
Resolves the foreign key in a flight entry.
Definition: adatabase.cpp:623
ADatabase::getCompletionList
const QStringList getCompletionList(ADatabaseTarget target)
getCompletionList returns a QStringList of values for a QCompleter based on database values
Definition: adatabase.cpp:461
ADatabase::disconnect
void disconnect()
closes the database connection.
Definition: adatabase.cpp:122
AFlightEntry
Definition: aflightentry.h:23
ADatabase::getPilotEntry
APilotEntry getPilotEntry(RowId_T row_id)
retreives a PilotEntry from the database.
Definition: adatabase.cpp:426
ADatabaseError
Custom Database Error derived from QSqlError. Extends text() adding "Database Error: " before the tex...
Definition: adatabase.h:89
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:724
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 database sqlite version.
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:633
ADatabase::databaseSummary
QMap< ADatabaseSummaryKey, QString > databaseSummary(const QString &db_path)
Return the summary of the DB_PATH as a stringlist.
Definition: adatabase.cpp:658
ADatabase::getEntry
AEntry getEntry(DataPosition data_position)
retreive an Entry from the database.
Definition: adatabase.cpp:419
ADatabase::getTailEntry
ATailEntry getTailEntry(RowId_T row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:433
ADatabase::getFlightEntry
AFlightEntry getFlightEntry(RowId_T row_id)
retreives a flight entry from the database.
Definition: adatabase.cpp:447
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:586
ADatabase::getAircraftEntry
AAircraftEntry getAircraftEntry(RowId_T row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:440
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:129
ADatabase
The DB class encapsulates the SQL database by providing fast access to hot database data.
Definition: adatabase.h:100
ADatabase::remove
bool remove(AEntry entry)
deletes an entry from the database.
Definition: adatabase.cpp:143
ADatabase::getLastEntry
int getLastEntry(ADatabaseTarget target)
returns the ROWID for the newest entry in the respective database.
Definition: adatabase.cpp:558
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:362
ADatabase::removeMany
bool removeMany(QList< DataPosition >)
deletes a list of entries from the database. Optimised for speed when deleting many entries.
Definition: adatabase.cpp:174
ADatabase::getCurrencyEntry
ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name)
Retreives a currency entry from the database.
Definition: adatabase.cpp:454
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:628
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:510
ADatabase::commit
bool commit(AEntry entry)
commits an entry to the database, calls either insert or update, based on position data
Definition: adatabase.cpp:134
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:316
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:280