# openPilotLog: src/database/adatabase.h Source File

adatabase.h
/*
*openPilotLog - A FOSS Pilot Logbook Application
*Copyright (C) 2020-2021 Felix Turowsky
*
*This program is free software: you can redistribute it and/or modify
*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.
*
*This program is distributed in the hope that it will be useful,
*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
*along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ADATABASE_H
#define ADATABASE_H
#include <QPair>
#include <QMap>
#include <QString>
#include <QDir>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include "src/database/adatabasetypes.h"
#include "src/classes/aentry.h"
#include "src/classes/apilotentry.h"
#include "src/classes/atailentry.h"
#include "src/classes/aaircraftentry.h"
#include "src/classes/aflightentry.h"
#include "src/classes/astandardpaths.h"
#include "src/classes/acurrencyentry.h"
#define SQLITE_DRIVER QStringLiteral("QSQLITE")
#define aDB ADatabase::instance()
enum class ADatabaseTarget
{
airport_identifier_icao,
airport_identifier_iata,
airport_identifier_all,
airport_names,
registrations,
companies,
tails,
pilots,
aircraft
};
enum class ADatabaseTable
{
tails,
flights,
currencies,
aircraft,
pilots,
};
enum class ADatabaseSummaryKey {
total_flights,
total_tails,
total_pilots,
last_flight,
total_time,
};
class ADatabaseError : public QSqlError {
public:
ADatabaseError() = default;
ADatabaseError(QString msg);
QString text() const;
ADatabaseError(QSqlError);
};
class ADatabase : public QObject {
Q_OBJECT
private:
static ADatabase* self;
TableNames_T tableNames;
TableColumns_T tableColumns;
int databaseRevision;
ADatabase();
int checkDbVersion() const;
const static QStringList userTableNames;
const static QStringList templateTableNames;
const static int minimumDatabaseRevision;
public:
QSqlError lastError;
const QFileInfo databaseFile;
// Ensure DB is not copiable or assignable
ADatabase(const ADatabase&) = delete;
void operator=(const ADatabase&) = delete;
static ADatabase* instance();
int dbRevision() const;
const TableNames_T getTableNames() const;
const ColumnNames_T getTableColumns(TableName_T table_name) const;
void updateLayout();
const QString sqliteVersion() const;
bool connect();
void disconnect();
static QSqlDatabase database();
QVector<QVariant> customQuery(QString statement, int return_values);
bool exists(AEntry entry);
bool exists(DataPosition data_position);
bool clear();
bool commit(AEntry entry);
bool insert(AEntry new_entry);
bool update(AEntry updated_entry);
bool remove(AEntry entry);
bool removeMany(QList<DataPosition>);
RowData_T getEntryData(DataPosition data_position);
AEntry getEntry(DataPosition data_position);
APilotEntry getPilotEntry(RowId_T row_id);
ATailEntry getTailEntry(RowId_T row_id);
AAircraftEntry getAircraftEntry(RowId_T row_id);
AFlightEntry getFlightEntry(RowId_T row_id);
ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name);
const QStringList getCompletionList(ADatabaseTarget target);
const QMap<QString, RowId_T> getIdMap(ADatabaseTarget target);
int getLastEntry(ADatabaseTable table);
QList<RowId_T> getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTable target);
APilotEntry resolveForeignPilot(RowId_T foreign_key);
ATailEntry resolveForeignTail(RowId_T foreign_key);
QMap<ADatabaseSummaryKey, QString> databaseSummary(const QString& db_path);
const QString databaseSummaryString(const QString& db_path);
bool restoreBackup(const QString& backup_file);
bool createBackup(const QString& dest_file);
QVector<RowData_T> getTable(ADatabaseTable table_name);
QStringList getUserTableNames();
QStringList getTemplateTableNames();
static int getMinimumDatabaseRevision();
signals:
void dataBaseUpdated();
void connectionReset();
};
#endif // ADATABASE_H
ADatabase::exists
bool exists(AEntry entry)
Checks if an entry exists in the database, based on position data.
Definition: adatabase.cpp:257
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:855
ADatabase::updateLayout
void updateLayout()
Updates the member variables tableNames and tableColumns with up-to-date layout information if the da...
Definition: adatabase.cpp:98
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:770
ADatabase::clear
bool clear()
clear resets the database, i.e. deletes all content in the tables containing userdata (pilots,...
Definition: adatabase.cpp:316
ADatabase::getLastEntry
int getLastEntry(ADatabaseTable table)
returns the ROWID for the newest entry in the respective database.
Definition: adatabase.cpp:605
ADatabase::resolveForeignPilot
APilotEntry resolveForeignPilot(RowId_T foreign_key)
Resolves the foreign key in a flight entry.
Definition: adatabase.cpp:670
ADatabase::getCompletionList
const QStringList getCompletionList(ADatabaseTarget target)
getCompletionList returns a QStringList of values for a QCompleter based on database values
Definition: adatabase.cpp:510
ADatabase::getTableColumns
const ColumnNames_T getTableColumns(TableName_T table_name) const
Return the names of a given table in the database.
Definition: adatabase.cpp:88
ADatabase::disconnect
void disconnect()
closes the database connection.
Definition: adatabase.cpp:165
AFlightEntry
Definition: aflightentry.h:23
ADatabase::getPilotEntry
APilotEntry getPilotEntry(RowId_T row_id)
retreives a PilotEntry from the database.
Definition: adatabase.cpp:475
ADatabaseError
Custom Database Error derived from QSqlError. Extends text() adding "Database Error: " before the tex...
Definition: adatabase.h:98
ADatabase::getMinimumDatabaseRevision
static int getMinimumDatabaseRevision()
getMinimumDatabaseRevision returns the minimum required database revision number required by the appl...
Definition: adatabase.cpp:73
ADatabase::lastError
QSqlError lastError
lastError extends QSqlError. Holds information about the last error that ocurred during a SQL operati...
Definition: adatabase.h:134
APilotEntry
Definition: apilotentry.h:24
ADatabase::connect
bool connect()
Connect to the database and populate database information.
Definition: adatabase.cpp:136
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:788
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:127
ADatabase::dataBaseUpdated
void dataBaseUpdated()
updated is emitted whenever the database contents have been updated. This can be either a commit,...
ADatabase::getUserTableNames
QStringList getUserTableNames()
getUserTableNames returns a list of the table names of tables that contain user-created data (flights...
Definition: adatabase.cpp:83
ADatabase::customQuery
QVector< QVariant > customQuery(QString statement, int return_values)
Can be used to send a complex query to the database.
Definition: adatabase.cpp:680
ADatabase::databaseSummary
QMap< ADatabaseSummaryKey, QString > databaseSummary(const QString &db_path)
Return a summary of a database.
Definition: adatabase.cpp:709
ADatabase::getEntry
AEntry getEntry(DataPosition data_position)
retreive an Entry from the database.
Definition: adatabase.cpp:468
ADatabase::getTableNames
const TableNames_T getTableNames() const
Return the names of all tables in the database.
Definition: adatabase.cpp:93
ADatabase::getTailEntry
ATailEntry getTailEntry(RowId_T row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:482
ADatabase::getFlightEntry
AFlightEntry getFlightEntry(RowId_T row_id)
retreives a flight entry from the database.
Definition: adatabase.cpp:496
ADatabase::getAircraftEntry
AAircraftEntry getAircraftEntry(RowId_T row_id)
retreives a TailEntry from the database.
Definition: adatabase.cpp:489
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:172
ADatabase
The DB class encapsulates the SQL database by providing fast access to hot database data.
Definition: adatabase.h:110
ADatabase::remove
bool remove(AEntry entry)
deletes an entry from the database.
Definition: adatabase.cpp:186
ADatabase::getEntryData
RowData_T getEntryData(DataPosition data_position)
retreive entry data from the database to create an entry object
Definition: adatabase.cpp:410
ADatabase::removeMany
bool removeMany(QList< DataPosition >)
deletes a list of entries from the database. Optimised for speed when deleting many entries.
Definition: adatabase.cpp:215
ADatabase::getCurrencyEntry
ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name)
Retreives a currency entry from the database.
Definition: adatabase.cpp:503
ADatabase::getForeignKeyConstraints
QList< RowId_T > getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTable target)
returns a list of ROWID's in the flights table for which foreign key constraints exist.
Definition: adatabase.cpp:633
AAircraftEntry
Definition: aaircraftentry.h:24
ADatabase::getTable
QVector< RowData_T > getTable(ADatabaseTable table_name)
getTable returns all contents of a given table from the database
Definition: adatabase.cpp:806
ACurrencyEntry
Definition: acurrencyentry.h:7
ADatabase::resolveForeignTail
ATailEntry resolveForeignTail(RowId_T foreign_key)
Resolves the foreign key in a flight entry.
Definition: adatabase.cpp:675
ADatabase::dbRevision
int dbRevision() const
dbRevision returns the database Revision Number. The Revision refers to what iteration of the databas...
Definition: adatabase.cpp:61
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:559
ADatabase::commit
bool commit(AEntry entry)
commits an entry to the database, calls either insert or update, based on position data
Definition: adatabase.cpp:177
AEntry
The Entry class encapsulates table metadata(table name, row id) and data for new and existing entries...
Definition: aentry.h:40
ADatabase::getTemplateTableNames
QStringList getTemplateTableNames()
getTemplateTableNames returns a list of the table names of tables that contain template data (aiports...
Definition: adatabase.cpp:78
ADatabase::insert
bool insert(AEntry new_entry)
Create new entry in the databse based on UserInput.
Definition: adatabase.cpp:366
ADatabase::update
bool update(AEntry updated_entry)
Updates entry in database from existing entry tweaked by the user.
Definition: adatabase.cpp:332