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

adatabase.h
/*
*openPilot Log - A FOSS Pilot Logbook Application
*Copyright (C) 2020 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 __DB_H__
#define __DB_H__
#include <QPair>
#include <QMap>
#include <QString>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlTableModel>
#include "src/database/dbinfo.h"
#include "src/testing/adebug.h"
#include "aentry.h"
#include "apilotentry.h"
#include "atailentry.h"
#include "aaircraftentry.h"
#include "aflightentry.h"
namespace experimental {
// [G]: Suspicious documentation -,O
enum class ADatabaseTarget
{
airport_identifier_icao,
airport_identifier_iata,
airport_identifier_all,
airport_names,
pilots,
registrations,
aircraft,
companies,
tails
};
// [G]: This is how we should handle custom "events" in the program.
// In this case a custom error doesnt need to be built from scratch.
// Find the type of error you want and extend it with a few tweaks.
class ADatabaseError : public QSqlError {
public:
ADatabaseError() = default;
ADatabaseError(QString msg);
QString text() const;
};
class ADatabase : public QObject {
Q_OBJECT
private:
TableNames tableNames;
TableColumns tableColumns;
static ADatabase* instance;
ADatabase() = default;
public:
// Ensure DB is not copiable or assignable
ADatabase(const ADatabase&) = delete;
void operator=(const ADatabase&) = delete;
static ADatabase* getInstance();
ADatabaseError lastError;
bool connect();
void disconnect();
static QSqlDatabase database();
QVector<QString> customQuery(QString statement, int return_values);
bool exists(AEntry entry);
bool exists(DataPosition data_position);
bool commit(AEntry entry);
bool insert(AEntry new_entry);
bool update(AEntry updated_entry);
bool remove(AEntry entry);
bool removeMany(QList<DataPosition>);
TableData getEntryData(DataPosition data_position);
AEntry getEntry(DataPosition data_position);
APilotEntry getPilotEntry(RowId row_id);
ATailEntry getTailEntry(RowId row_id);
AAircraftEntry getAircraftEntry(RowId row_id);
AFlightEntry getFlightEntry(RowId row_id);
const QStringList getCompletionList(ADatabaseTarget);
const QMap<QString, int> getIdMap(ADatabaseTarget);
int getLastEntry(ADatabaseTarget);
QList<int> getForeignKeyConstraints(int foreign_row_id, ADatabaseTarget target);
signals:
void dataBaseUpdated();
};
ADatabase* aDB();
} // namespace experimental
#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