openPilotLog
database.h
1 /*
2  *openPilotLog - A FOSS Pilot Logbook Application
3  *Copyright (C) 2020-2022 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 DATABASE_H
19 #define DATABASE_H
20 
21 #include <QPair>
22 #include <QHash>
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/classes/paths.h"
35 #include "src/opl.h"
36 #include "src/database/row.h"
37 
38 
39 
40 namespace OPL {
41 
42 //using RowData_T = QHash<QString, QVariant>;
43 
51 #define DB OPL::Database::instance()
52 
57 class Database : public QObject {
58 
59 private:
60  Q_OBJECT
61  Database()
62  : databaseFile(OPL::Paths::databaseFileInfo())
63  {}
64  static Database* self;
65  const QFileInfo databaseFile;
66  QStringList tableNames;
67  QHash<QString, QStringList> tableColumns;
68 
69  inline const static QString SQLITE_DRIVER = QStringLiteral("QSQLITE");
70  inline const static QList<OPL::DbTable> USER_TABLES = {
71  OPL::DbTable::Flights,
72  OPL::DbTable::Pilots,
73  OPL::DbTable::Tails
74  };
75  inline const static QList<OPL::DbTable> TEMPLATE_TABLES = {
76  OPL::DbTable::Aircraft,
77  OPL::DbTable::Airports,
78  OPL::DbTable::Currencies,
79  OPL::DbTable::Changelog,
80  };
81 
82 
83 public:
84  Database(const Database&) = delete;
85  void operator=(const Database&) = delete;
86  static Database* instance();
87 
96  QSqlError lastError;
97 
101  bool connect();
102 
106  void disconnect();
107 
112  void updateLayout();
113 
117  const QString version() const;
118 
123  const QString sqliteVersion() const;
124 
128  const QStringList getTableNames() const;
129 
133  const QStringList getTableColumns(OPL::DbTable table_name) const;
134 
139  static QSqlDatabase database();
140 
146  QVector<QVariant> customQuery(QString statement, int return_values);
147 
151  bool exists(const OPL::Row &row);
152 
157  bool clear();
158 
163  bool commit(const OPL::Row &row);
164 
170  bool commit(const QJsonArray &json_arr, const OPL::DbTable table);
171 
175  bool insert(const OPL::Row &new_row);
176 
180  bool update(const OPL::Row &updated_row);
181 
185  bool remove(const OPL::Row &row);
186 
191  bool removeMany(OPL::DbTable table, const QList<int> &row_id_list);
192 
196  OPL::Row getRow(const OPL::DbTable table, const int row_id);
197 
201  RowData_T getRowData(const OPL::DbTable table, const int row_id);
202 
206  inline OPL::PilotEntry getPilotEntry(int row_id)
207  {
208  const auto data = getRowData(OPL::DbTable::Pilots, row_id);
209  return OPL::PilotEntry(row_id, data);
210  }
211 
215  inline OPL::TailEntry getTailEntry(int row_id)
216  {
217  const auto data = getRowData(OPL::DbTable::Tails, row_id);
218  return OPL::TailEntry(row_id, data);
219  }
220 
225  {
226  const auto data = getRowData(OPL::DbTable::Aircraft, row_id);
227  return OPL::AircraftEntry(row_id, data);
228  }
229 
233  inline OPL::FlightEntry getFlightEntry(int row_id)
234  {
235  const auto data = getRowData(OPL::DbTable::Flights, row_id);
236  return OPL::FlightEntry(row_id, data);
237  }
238 
242  inline OPL::SimulatorEntry getSimEntry(int row_id)
243  {
244  const auto data = getRowData(OPL::DbTable::Simulators, row_id);
245  return OPL::SimulatorEntry(row_id, data);
246  }
247 
252  {
253  const auto data = getRowData(OPL::DbTable::Currencies, row_id);
254  return OPL::CurrencyEntry(row_id, data);
255  }
256 
261  {
262  const auto data = getRowData(OPL::DbTable::Airports, row_id);
263  return OPL::AirportEntry(row_id, data);
264  }
265 
269  int getLastEntry(OPL::DbTable table);
270 
275  QList<int> getForeignKeyConstraints(int foreign_row_id, OPL::DbTable table);
276 
281  QVector<RowData_T> getTable(OPL::DbTable table);
282 
287  const QList<OPL::DbTable> &getUserTables() const;
288 
293  const QList<OPL::DbTable> &getTemplateTables() const;
294 
295  // Maintenance and setup
296 
302  bool createSchema();
311  bool importTemplateData(bool use_local_ressources);
312 
316  bool resetUserData();
317 
322  bool createBackup(const QString& dest_file);
323 
328  bool restoreBackup(const QString& backup_file);
329 
330 
331 
332 signals:
339  void dataBaseUpdated(const DbTable table);
345 };
346 
347 } // namespace OPL
348 
349 #endif // DATABASE_H
A Row representing an Aircraft entry. See Row class for details.
Definition: row.h:85
A Row representing an Airport entry. See Row class for details.
Definition: row.h:155
A Row representing a Currency entry. See Row class for details.
Definition: row.h:144
The DB class encapsulates the SQL database by providing fast access to hot database data.
Definition: database.h:57
const QString sqliteVersion() const
Database::sqliteVersion returns the database sqlite version. See also dbRevision()
Definition: database.cpp:117
bool connect()
Connect to the database and populate database information.
Definition: database.cpp:26
static QSqlDatabase database()
Can be used to access the database connection.
Definition: database.cpp:126
OPL::PilotEntry getPilotEntry(int row_id)
retreives a PilotEntry from the database. See row class for details.
Definition: database.h:206
bool remove(const OPL::Row &row)
deletes an entry from the database.
Definition: database.cpp:186
RowData_T getRowData(const OPL::DbTable table, const int row_id)
retreive a Map of <column name, column content> for a specific row in the database.
Definition: database.cpp:427
bool createBackup(const QString &dest_file)
Database::createBackup copies the currently used database to an external backup location provided by ...
Definition: database.cpp:582
bool update(const OPL::Row &updated_row)
Updates entry in database from existing entry tweaked by the user.
Definition: database.cpp:303
bool importTemplateData(bool use_local_ressources)
importTemplateData fills an empty database with the template data (Aircraft, Airports,...
Definition: database.cpp:666
QList< int > getForeignKeyConstraints(int foreign_row_id, OPL::DbTable table)
returns a list of ROWID's in the flights table for which foreign key constraints exist.
Definition: database.cpp:472
void updateLayout()
Updates the member variables tableNames and tableColumns with up-to-date layout information if the da...
Definition: database.cpp:92
OPL::FlightEntry getFlightEntry(int row_id)
retreives a flight entry from the database. See row class for details.
Definition: database.h:233
OPL::AirportEntry getAirportEntry(int row_id)
Retreives an airport entry from the database. See row class for details.
Definition: database.h:260
bool resetUserData()
Delete all rows from the user data tables (flights, pliots, tails)
Definition: database.cpp:706
bool exists(const OPL::Row &row)
Checks if an entry exists in the database, based on position data.
Definition: database.cpp:259
bool removeMany(OPL::DbTable table, const QList< int > &row_id_list)
deletes a batch of entries from the database. Optimised for speed when deleting many entries....
Definition: database.cpp:217
const QString version() const
Return the database revision number (not the sqlite version number).
Definition: database.cpp:65
bool restoreBackup(const QString &backup_file)
Database::restoreBackup restores the database from a given backup file and replaces the currently act...
Definition: database.cpp:600
QVector< RowData_T > getTable(OPL::DbTable table)
getTable returns all contents of a given table from the database
Definition: database.cpp:537
OPL::AircraftEntry getAircraftEntry(int row_id)
retreives a TailEntry from the database. See row class for details.
Definition: database.h:224
void dataBaseUpdated(const DbTable table)
updated is emitted whenever the database contents have been updated. This can be either a commit,...
bool createSchema()
Create or restore the database to its ready-to-use but empty state.
Definition: database.cpp:631
OPL::TailEntry getTailEntry(int row_id)
retreives a TailEntry from the database. See row class for details.
Definition: database.h:215
const QStringList getTableNames() const
Return the names of all tables in the database.
Definition: database.cpp:87
OPL::SimulatorEntry getSimEntry(int row_id)
retreives a Simulator entry from the database. See row class for details.
Definition: database.h:242
bool clear()
clear resets the database, i.e. deletes all content in the tables containing userdata (pilots,...
Definition: database.cpp:288
bool insert(const OPL::Row &new_row)
Create new entry in the databse based on UserInput.
Definition: database.cpp:346
QSqlError lastError
Holds information about the last error that ocurred during a SQL operation. If the error type is QSql...
Definition: database.h:96
QVector< QVariant > customQuery(QString statement, int return_values)
Can be used to send a complex query to the database.
Definition: database.cpp:509
const QList< OPL::DbTable > & getUserTables() const
getUserTables returns a list of the of the tables that contain user-created data (flights,...
Definition: database.cpp:77
const QStringList getTableColumns(OPL::DbTable table_name) const
Return the names of a given table in the database.
Definition: database.cpp:82
void connectionReset()
connectionReset is emitted whenever the database connection is reset, for example when creating or re...
void disconnect()
closes the database connection.
Definition: database.cpp:53
const QList< OPL::DbTable > & getTemplateTables() const
getTemplateTables returns a list of the tables that contain template data (aiports,...
Definition: database.cpp:72
bool commit(const OPL::Row &row)
commits an entry to the database, calls either insert or update, based on position data
Definition: database.cpp:131
OPL::Row getRow(const OPL::DbTable table, const int row_id)
retreive a Row from the database
Definition: database.cpp:395
int getLastEntry(OPL::DbTable table)
returns the ROWID for the newest entry in the respective table.
Definition: database.cpp:459
OPL::CurrencyEntry getCurrencyEntry(int row_id)
Retreives a currency entry from the database. See row class for details.
Definition: database.h:251
A Row representing a Flight entry. See Row class for details.
Definition: row.h:133
static const QFileInfo databaseFileInfo()
returns a QFileInfo for the default database file.
Definition: paths.cpp:39
A Row representing a Pilot entry. See Row class for details.
Definition: row.h:110
The Row class provides an interface for retreiving and submitting entries from the database.
Definition: row.h:46
A Row representing a Simulator entry. See Row class for details.
Definition: row.h:122
A Row representing a Tail (Registration) entry. See Row class for details.
Definition: row.h:96
A namespace to collect constants and enums used throughout the application.
Definition: paths.cpp:3
DbTable
Enumerates the tables in the database.
Definition: opl.h:122