openPilotLog
adatabase.h
1 /*
2  *openPilot Log - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020 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 __DB_H__
19 #define __DB_H__
20 
21 #include <QPair>
22 #include <QMap>
23 #include <QString>
24 #include <QSqlQuery>
25 #include <QSqlError>
26 #include <QSqlTableModel>
27 #include "src/database/dbinfo.h"
28 #include "src/testing/adebug.h"
29 
30 #include "aentry.h"
31 #include "apilotentry.h"
32 #include "atailentry.h"
33 #include "aaircraftentry.h"
34 #include "aflightentry.h"
35 
36 namespace experimental {
37 
38 
39 // [G]: Suspicious documentation -,O
44 enum class ADatabaseTarget
45 {
46  airport_identifier_icao,
47  airport_identifier_iata,
48  airport_identifier_all,
49  airport_names,
50  pilots,
51  registrations,
52  aircraft,
53  companies,
54  tails
55 };
56 
57 // [G]: This is how we should handle custom "events" in the program.
58 // In this case a custom error doesnt need to be built from scratch.
59 // Find the type of error you want and extend it with a few tweaks.
64 class ADatabaseError : public QSqlError {
65 public:
66  ADatabaseError() = default;
67  ADatabaseError(QString msg);
68  QString text() const;
69 };
70 
75 class ADatabase : public QObject {
76  Q_OBJECT
77 private:
78  TableNames tableNames;
79  TableColumns tableColumns;
80  static ADatabase* instance;
81  ADatabase() = default;
82 public:
83  // Ensure DB is not copiable or assignable
84  ADatabase(const ADatabase&) = delete;
85  void operator=(const ADatabase&) = delete;
86  static ADatabase* getInstance();
87  ADatabaseError lastError;
88 
92  bool connect();
93 
97  void disconnect();
98 
103  static QSqlDatabase database();
104 
110  QVector<QString> customQuery(QString statement, int return_values);
111 
115  bool exists(AEntry entry);
116  bool exists(DataPosition data_position);
117 
122  bool commit(AEntry entry);
123 
127  bool insert(AEntry new_entry);
128 
132  bool update(AEntry updated_entry);
133 
137  bool remove(AEntry entry);
138 
143  bool removeMany(QList<DataPosition>);
144 
148  TableData getEntryData(DataPosition data_position);
149 
153  AEntry getEntry(DataPosition data_position);
154 
163  APilotEntry getPilotEntry(RowId row_id);
164 
173  ATailEntry getTailEntry(RowId row_id);
174 
183  AAircraftEntry getAircraftEntry(RowId row_id);
184 
193  AFlightEntry getFlightEntry(RowId row_id);
194 
199  const QStringList getCompletionList(ADatabaseTarget);
200 
205  const QMap<QString, int> getIdMap(ADatabaseTarget);
206 
211 
216  QList<int> getForeignKeyConstraints(int foreign_row_id, ADatabaseTarget target);
217 
218 signals:
226 };
227 
235 ADatabase* aDB();
236 
237 } // namespace experimental
238 
239 #endif
experimental
Temporary namespace for experimental features. Will be removed in later versions.
Definition: aaircraftentry.cpp:20
experimental::ADatabase::dataBaseUpdated
void dataBaseUpdated()
updated is emitted whenever the database contents have been updated. This can be either a commit,...
experimental::ADatabase::disconnect
void disconnect()
closes the database connection.
Definition: adatabase.cpp:75
experimental::DataPosition
Definition: decl.h:45
experimental::AEntry
The Entry class encapsulates table metadata(table name, row id) and data for new and existing entries...
Definition: aentry.h:42
experimental::ADatabase::commit
bool commit(AEntry entry)
commits an entry to the database, calls either insert or update, based on position data
Definition: adatabase.cpp:88
experimental::ADatabase::getIdMap
const QMap< QString, int > getIdMap(ADatabaseTarget)
returns a QMap<QString, int> of a human-readable database value and its row id. Used in the Dialogs t...
Definition: adatabase.cpp:452
experimental::ADatabaseTarget
ADatabaseTarget
The DBTarget enum provides the items for which QCompleter completion lists are provided from the data...
Definition: adatabase.h:45
experimental::ADatabase::update
bool update(AEntry updated_entry)
Updates entry in database from existing entry tweaked by the user.
Definition: adatabase.cpp:230
experimental::ADatabase::getAircraftEntry
AAircraftEntry getAircraftEntry(RowId row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:390
experimental::ADatabase::getPilotEntry
APilotEntry getPilotEntry(RowId row_id)
retreives a PilotEntry from the database.
Definition: adatabase.cpp:376
experimental::AFlightEntry
Definition: aflightentry.h:9
experimental::ADatabase::customQuery
QVector< QString > customQuery(QString statement, int return_values)
Can be used to send a complex query to the database.
Definition: adatabase.cpp:564
experimental::ATailEntry
Definition: atailentry.h:27
experimental::ADatabase::removeMany
bool removeMany(QList< DataPosition >)
deletes a list of entries from the database. Optimised for speed when deleting many entries.
Definition: adatabase.cpp:128
experimental::ADatabase::getTailEntry
ATailEntry getTailEntry(RowId row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:383
experimental::ADatabase::insert
bool insert(AEntry new_entry)
Create new entry in the databse based on UserInput.
Definition: adatabase.cpp:266
experimental::ADatabase::database
static QSqlDatabase database()
Can be used to access the database connection.
Definition: adatabase.cpp:83
experimental::ADatabaseError
Custom Database Error derived from QSqlError. Extends text() adding "Database Error: " before the tex...
Definition: adatabase.h:64
experimental::AAircraftEntry
Definition: aaircraftentry.h:9
experimental::ADatabase::getForeignKeyConstraints
QList< int > getForeignKeyConstraints(int foreign_row_id, ADatabaseTarget target)
returns a list of ROWID's in the flights table for which foreign key constraints exist.
Definition: adatabase.cpp:527
experimental::ADatabase
The DB class encapsulates the SQL database by providing fast access to hot database data.
Definition: adatabase.h:75
experimental::ADatabase::exists
bool exists(AEntry entry)
Checks if an entry exists in the database, based on position data.
Definition: adatabase.cpp:170
experimental::ADatabase::getFlightEntry
AFlightEntry getFlightEntry(RowId row_id)
retreives a flight entry from the database.
Definition: adatabase.cpp:397
experimental::ADatabase::connect
bool connect()
Connect to the database and populate database information.
Definition: adatabase.cpp:41
experimental::APilotEntry
Definition: apilotentry.h:26
experimental::ADatabase::getLastEntry
int getLastEntry(ADatabaseTarget)
returns the ROWID for the newest entry in the respective database.
Definition: adatabase.cpp:499
experimental::aDB
ADatabase * aDB()
Convinience function that returns instance of DataBase. Instead of this: DataBase::getInstance()....
Definition: adatabase.cpp:590
experimental::ADatabase::getEntryData
TableData getEntryData(DataPosition data_position)
retreive entry data from the database to create an entry object
Definition: adatabase.cpp:312
experimental::ADatabase::getEntry
AEntry getEntry(DataPosition data_position)
retreive an Entry from the database.
Definition: adatabase.cpp:369
experimental::ADatabase::getCompletionList
const QStringList getCompletionList(ADatabaseTarget)
getCompletionList returns a QStringList of values for a QCompleter based on database values
Definition: adatabase.cpp:404
experimental::ADatabase::remove
bool remove(AEntry entry)
deletes an entry from the database.
Definition: adatabase.cpp:97