database.h
/*
*openPilotLog - A FOSS Pilot Logbook Application
*Copyright (C) 2020-2022 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 DATABASE_H
#define DATABASE_H
#include <QPair>
#include <QHash>
#include <QString>
#include <QDir>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include "src/classes/paths.h"
#include "src/opl.h"
#include "src/database/row.h"
namespace OPL {
//using RowData_T = QHash<QString, QVariant>;
#define DB OPL::Database::instance()
class Database : public QObject {
private:
Q_OBJECT
Database()
: databaseFile(OPL::Paths::databaseFileInfo())
{}
static Database* self;
const QFileInfo databaseFile;
QStringList tableNames;
QHash<QString, QStringList> tableColumns;
inline const static QString SQLITE_DRIVER = QStringLiteral("QSQLITE");
inline const static QList<OPL::DbTable> USER_TABLES = {
OPL::DbTable::Flights,
OPL::DbTable::Pilots,
OPL::DbTable::Tails
};
inline const static QList<OPL::DbTable> TEMPLATE_TABLES = {
OPL::DbTable::Aircraft,
OPL::DbTable::Airports,
OPL::DbTable::Currencies,
OPL::DbTable::Changelog,
};
public:
Database(const Database&) = delete;
void operator=(const Database&) = delete;
static Database* instance();
QSqlError lastError;
bool connect();
void disconnect();
void updateLayout();
const QString version() const;
const QString sqliteVersion() const;
const QStringList getTableNames() const;
const QStringList getTableColumns(OPL::DbTable table_name) const;
static QSqlDatabase database();
QVector<QVariant> customQuery(QString statement, int return_values);
bool exists(const OPL::Row &row);
bool clear();
bool commit(const OPL::Row &row);
bool commit(const QJsonArray &json_arr, const OPL::DbTable table);
bool insert(const OPL::Row &new_row);
bool update(const OPL::Row &updated_row);
bool remove(const OPL::Row &row);
bool removeMany(OPL::DbTable table, const QList<int> &row_id_list);
OPL::Row getRow(const OPL::DbTable table, const int row_id);
RowData_T getRowData(const OPL::DbTable table, const int row_id);
inline OPL::PilotEntry getPilotEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Pilots, row_id);
return OPL::PilotEntry(row_id, data);
}
inline OPL::TailEntry getTailEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Tails, row_id);
return OPL::TailEntry(row_id, data);
}
inline OPL::AircraftEntry getAircraftEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Aircraft, row_id);
return OPL::AircraftEntry(row_id, data);
}
inline OPL::FlightEntry getFlightEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Flights, row_id);
return OPL::FlightEntry(row_id, data);
}
inline OPL::SimulatorEntry getSimEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Simulators, row_id);
return OPL::SimulatorEntry(row_id, data);
}
inline OPL::CurrencyEntry getCurrencyEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Currencies, row_id);
return OPL::CurrencyEntry(row_id, data);
}
inline OPL::AirportEntry getAirportEntry(int row_id)
{
const auto data = getRowData(OPL::DbTable::Airports, row_id);
return OPL::AirportEntry(row_id, data);
}
int getLastEntry(OPL::DbTable table);
QList<int> getForeignKeyConstraints(int foreign_row_id, OPL::DbTable table);
QVector<RowData_T> getTable(OPL::DbTable table);
const QList<OPL::DbTable> &getUserTables() const;
const QList<OPL::DbTable> &getTemplateTables() const;
// Maintenance and setup
bool createSchema();
bool importTemplateData(bool use_local_ressources);
bool resetUserData();
bool createBackup(const QString& dest_file);
bool restoreBackup(const QString& backup_file);
signals:
void dataBaseUpdated(const DbTable table);
void connectionReset();
};
} // namespace OPL
#endif // DATABASE_H
OPL::AircraftEntry
A Row representing an Aircraft entry. See Row class for details.
Definition: row.h:85
OPL::AirportEntry
A Row representing an Airport entry. See Row class for details.
Definition: row.h:155
OPL::CurrencyEntry
A Row representing a Currency entry. See Row class for details.
Definition: row.h:144
OPL::Database
The DB class encapsulates the SQL database by providing fast access to hot database data.
Definition: database.h:57
OPL::Database::sqliteVersion
const QString sqliteVersion() const
Database::sqliteVersion returns the database sqlite version. See also dbRevision()
Definition: database.cpp:117
OPL::Database::connect
bool connect()
Connect to the database and populate database information.
Definition: database.cpp:26
OPL::Database::database
static QSqlDatabase database()
Can be used to access the database connection.
Definition: database.cpp:126
OPL::Database::getPilotEntry
OPL::PilotEntry getPilotEntry(int row_id)
retreives a PilotEntry from the database. See row class for details.
Definition: database.h:206
OPL::Database::remove
bool remove(const OPL::Row &row)
deletes an entry from the database.
Definition: database.cpp:186
OPL::Database::getRowData
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
OPL::Database::createBackup
bool createBackup(const QString &dest_file)
Database::createBackup copies the currently used database to an external backup location provided by ...
Definition: database.cpp:582
OPL::Database::update
bool update(const OPL::Row &updated_row)
Updates entry in database from existing entry tweaked by the user.
Definition: database.cpp:303
OPL::Database::importTemplateData
bool importTemplateData(bool use_local_ressources)
importTemplateData fills an empty database with the template data (Aircraft, Airports,...
Definition: database.cpp:666
OPL::Database::getForeignKeyConstraints
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
OPL::Database::updateLayout
void updateLayout()
Updates the member variables tableNames and tableColumns with up-to-date layout information if the da...
Definition: database.cpp:92
OPL::Database::getFlightEntry
OPL::FlightEntry getFlightEntry(int row_id)
retreives a flight entry from the database. See row class for details.
Definition: database.h:233
OPL::Database::getAirportEntry
OPL::AirportEntry getAirportEntry(int row_id)
Retreives an airport entry from the database. See row class for details.
Definition: database.h:260
OPL::Database::resetUserData
bool resetUserData()
Delete all rows from the user data tables (flights, pliots, tails)
Definition: database.cpp:706
OPL::Database::exists
bool exists(const OPL::Row &row)
Checks if an entry exists in the database, based on position data.
Definition: database.cpp:259
OPL::Database::removeMany
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
OPL::Database::version
const QString version() const
Return the database revision number (not the sqlite version number).
Definition: database.cpp:65
OPL::Database::restoreBackup
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
OPL::Database::getTable
QVector< RowData_T > getTable(OPL::DbTable table)
getTable returns all contents of a given table from the database
Definition: database.cpp:537
OPL::Database::getAircraftEntry
OPL::AircraftEntry getAircraftEntry(int row_id)
retreives a TailEntry from the database. See row class for details.
Definition: database.h:224
OPL::Database::dataBaseUpdated
void dataBaseUpdated(const DbTable table)
updated is emitted whenever the database contents have been updated. This can be either a commit,...
OPL::Database::createSchema
bool createSchema()
Create or restore the database to its ready-to-use but empty state.
Definition: database.cpp:631
OPL::Database::getTailEntry
OPL::TailEntry getTailEntry(int row_id)
retreives a TailEntry from the database. See row class for details.
Definition: database.h:215
OPL::Database::getTableNames
const QStringList getTableNames() const
Return the names of all tables in the database.
Definition: database.cpp:87
OPL::Database::getSimEntry
OPL::SimulatorEntry getSimEntry(int row_id)
retreives a Simulator entry from the database. See row class for details.
Definition: database.h:242
OPL::Database::clear
bool clear()
clear resets the database, i.e. deletes all content in the tables containing userdata (pilots,...
Definition: database.cpp:288
OPL::Database::insert
bool insert(const OPL::Row &new_row)
Create new entry in the databse based on UserInput.
Definition: database.cpp:346
OPL::Database::lastError
QSqlError lastError
Holds information about the last error that ocurred during a SQL operation. If the error type is QSql...
Definition: database.h:96
OPL::Database::customQuery
QVector< QVariant > customQuery(QString statement, int return_values)
Can be used to send a complex query to the database.
Definition: database.cpp:509
OPL::Database::getUserTables
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
OPL::Database::getTableColumns
const QStringList getTableColumns(OPL::DbTable table_name) const
Return the names of a given table in the database.
Definition: database.cpp:82
OPL::Database::connectionReset
void connectionReset()
connectionReset is emitted whenever the database connection is reset, for example when creating or re...
OPL::Database::disconnect
void disconnect()
closes the database connection.
Definition: database.cpp:53
OPL::Database::getTemplateTables
const QList< OPL::DbTable > & getTemplateTables() const
getTemplateTables returns a list of the tables that contain template data (aiports,...
Definition: database.cpp:72
OPL::Database::commit
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::Database::getRow
OPL::Row getRow(const OPL::DbTable table, const int row_id)
retreive a Row from the database
Definition: database.cpp:395
OPL::Database::getLastEntry
int getLastEntry(OPL::DbTable table)
returns the ROWID for the newest entry in the respective table.
Definition: database.cpp:459
OPL::Database::getCurrencyEntry
OPL::CurrencyEntry getCurrencyEntry(int row_id)
Retreives a currency entry from the database. See row class for details.
Definition: database.h:251
OPL::FlightEntry
A Row representing a Flight entry. See Row class for details.
Definition: row.h:133
OPL::Paths::databaseFileInfo
static const QFileInfo databaseFileInfo()
returns a QFileInfo for the default database file.
Definition: paths.cpp:39
OPL::PilotEntry
A Row representing a Pilot entry. See Row class for details.
Definition: row.h:110
OPL::Row
The Row class provides an interface for retreiving and submitting entries from the database.
Definition: row.h:46
OPL::SimulatorEntry
A Row representing a Simulator entry. See Row class for details.
Definition: row.h:122
OPL::TailEntry
A Row representing a Tail (Registration) entry. See Row class for details.
Definition: row.h:96
OPL
A namespace to collect constants and enums used throughout the application.
Definition: paths.cpp:3
OPL::DbTable
DbTable
Enumerates the tables in the database.
Definition: opl.h:122