# openPilotLog: ADatabase Class Reference

ADatabase Class Reference

The DB class encapsulates the SQL database by providing fast access to hot database data. More...

`#include <adatabase.h>`

Inheritance diagram for ADatabase:

## Signals

void | dataBaseUpdated ()
| updated is emitted whenever the database contents have been updated. This can be either a commit, update or remove. This signal should be used to trigger an update to the models of the views displaying database contents in the user interface so that a user is always presented with up-to-date information.
void | connectionReset ()
| connectionReset is emitted whenever the database connection is reset, for example when creating or restoring a backup.

## Public Member Functions

| ADatabase (const ADatabase &)=delete
void | operator= (const ADatabase &)=delete
int | dbRevision () const
| dbRevision returns the database Revision Number. The Revision refers to what iteration of the database layout is used. For the sqlite version of the database refer to sqliteVersion() More...
const TableNames_T | getTableNames () const
| Return the names of all tables in the database.
const ColumnNames_T | getTableColumns (TableName_T table_name) const
| Return the names of a given table in the database.
void | updateLayout ()
| Updates the member variables tableNames and tableColumns with up-to-date layout information if the database has been altered. This function is normally only required during database setup or maintenance.
const QString | sqliteVersion () const
| ADatabase::sqliteVersion returns the database sqlite version. See also dbRevision() More...
bool | connect ()
| Connect to the database and populate database information.
void | disconnect ()
| closes the database connection.
QVector< QVariant > | customQuery (QString statement, int return_values)
| Can be used to send a complex query to the database. More...
bool | exists (AEntry entry)
| Checks if an entry exists in the database, based on position data.
bool | exists (DataPosition data_position)
bool | clear ()
| clear resets the database, i.e. deletes all content in the tables containing userdata (pilots, flights, tails)
bool | commit (AEntry entry)
| commits an entry to the database, calls either insert or update, based on position data
bool | insert (AEntry new_entry)
| Create new entry in the databse based on UserInput.
bool | update (AEntry updated_entry)
| Updates entry in database from existing entry tweaked by the user.
bool | remove (AEntry entry)
| deletes an entry from the database.
bool | removeMany (QList< DataPosition >)
| deletes a list of entries from the database. Optimised for speed when deleting many entries.
RowData_T | getEntryData (DataPosition data_position)
| retreive entry data from the database to create an entry object
AEntry | getEntry (DataPosition data_position)
| retreive an Entry from the database.
APilotEntry | getPilotEntry (RowId_T row_id)
| retreives a PilotEntry from the database. More...
ATailEntry | getTailEntry (RowId_T row_id)
| retreives a TailEntry from the database. More...
AAircraftEntry | getAircraftEntry (RowId_T row_id)
| retreives a TailEntry from the database. More...
AFlightEntry | getFlightEntry (RowId_T row_id)
| retreives a flight entry from the database. More...
ACurrencyEntry | getCurrencyEntry (ACurrencyEntry::CurrencyName currency_name)
| Retreives a currency entry from the database.
const QStringList | getCompletionList (ADatabaseTarget target)
| getCompletionList returns a QStringList of values for a QCompleter based on database values
const QMap< QString, RowId_T > | getIdMap (ADatabaseTarget target)
| returns a QMap<QString, RowId_t> of a human-readable database value and its row id. Used in the Dialogs to map user input to unique database entries. More...
int | getLastEntry (ADatabaseTable table)
| returns the ROWID for the newest entry in the respective database.
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.
APilotEntry | resolveForeignPilot (RowId_T foreign_key)
| Resolves the foreign key in a flight entry. More...
ATailEntry | resolveForeignTail (RowId_T foreign_key)
| Resolves the foreign key in a flight entry. More...
QMap< ADatabaseSummaryKey, QString > | databaseSummary (const QString &db_path)
| Return a summary of a database. More...
const QString | databaseSummaryString (const QString &db_path)
| returns a short summary string of the database, containing total time and date of last flight.
bool | restoreBackup (const QString &backup_file)
| ADatabase::restoreBackup restores the database from a given backup file and replaces the currently active database. More...
bool | createBackup (const QString &dest_file)
| ADatabase::createBackup copies the currently used database to an external backup location provided by the user. More...
QVector< RowData_T > | getTable (ADatabaseTable table_name)
| getTable returns all contents of a given table from the database More...
QStringList | getUserTableNames ()
| getUserTableNames returns a list of the table names of tables that contain user-created data (flights, pilots,..)
QStringList | getTemplateTableNames ()
| getTemplateTableNames returns a list of the table names of tables that contain template data (aiports, aircraft,..)

## Static Public Member Functions

static ADatabase * | instance ()
static QSqlDatabase | database ()
| Can be used to access the database connection. More...
static int | getMinimumDatabaseRevision ()
| getMinimumDatabaseRevision returns the minimum required database revision number required by the application.

## Public Attributes

QSqlError | lastError
| lastError extends QSqlError. Holds information about the last error that ocurred during a SQL operation. If the error type is QSqlError::UnknownError, the error is related to data from the database (entry not found,...), otherwise the error is related to SQL execution. In this case error.type() provides further information. More...
const QFileInfo | databaseFile

## Detailed Description

The DB class encapsulates the SQL database by providing fast access to hot database data.

## Member Function Documentation

## createBackup()

bool ADatabase::createBackup | ( | const QString & | dest_file | ) |

ADatabase::createBackup copies the currently used database to an external backup location provided by the user.

Parameters

  dest_file | This is the full path and filename of where the backup will be created, e.g. 'home/Sully/myBackups/backupFromOpl.db'

## customQuery()

QVector< QVariant > ADatabase::customQuery | ( | QString | statement,
|  | int | return_values
| ) |  |

Can be used to send a complex query to the database.

Parameters

  query | - the full sql query statement
  returnValues | - the number of return values

## database()

QSqlDatabase ADatabase::database | ( |  | ) |

static

Can be used to access the database connection.

Returns
  The QSqlDatabase object pertaining to the connection.

## databaseSummary()

QMap< ADatabaseSummaryKey, QString > ADatabase::databaseSummary | ( | const QString & | db_path | ) |

Return a summary of a database.

Creates a summary of the database giving a quick overview of the relevant contents. The function runs several specialised SQL queries to create a QMap<ADatabaseSummaryKey, QString> containing Total Flight Time, Number of unique aircraft and pilots, as well as the date of last flight. Uses a temporary database connection separate from the default connection in order to not tamper with the currently active database connection.

## dbRevision()

int ADatabase::dbRevision | ( |  | ) | const

dbRevision returns the database Revision Number. The Revision refers to what iteration of the database layout is used. For the sqlite version of the database refer to sqliteVersion()

Returns

## getAircraftEntry()

AAircraftEntry ADatabase::getAircraftEntry | ( | RowId_T | row_id | ) |

retreives a TailEntry from the database.

This function is a wrapper for DataBase::getEntry(DataPosition), where the table is already set and which returns an AAircraftEntry instead of an AEntry. It allows for easy access to an aircraft entry with only the RowId required as input.

## getFlightEntry()

AFlightEntry ADatabase::getFlightEntry | ( | RowId_T | row_id | ) |

retreives a flight entry from the database.

This function is a wrapper for DataBase::getEntry(DataPosition), where the table is already set and which returns an AFlightEntry instead of an AEntry. It allows for easy access to a flight entry with only the RowId required as input.

## getIdMap()

const QMap< QString, RowId_T > ADatabase::getIdMap | ( | ADatabaseTarget | target | ) |

returns a QMap<QString, RowId_t> of a human-readable database value and its row id. Used in the Dialogs to map user input to unique database entries.

Todo:
  What is this QString semantically? As i understand its a "QueryResult" QVariant cast to QString

## getPilotEntry()

APilotEntry ADatabase::getPilotEntry | ( | RowId_T | row_id | ) |

retreives a PilotEntry from the database.

This function is a wrapper for DataBase::getEntry(DataPosition), where the table is already set and which returns a PilotEntry instead of an Entry. It allows for easy access to a pilot entry with only the RowId required as input.

## getTable()

QVector< RowData_T > ADatabase::getTable | ( | ADatabaseTable | table_name | ) |

getTable returns all contents of a given table from the database

Returns

## getTailEntry()

ATailEntry ADatabase::getTailEntry | ( | RowId_T | row_id | ) |

retreives a TailEntry from the database.

This function is a wrapper for DataBase::getEntry(DataPosition), where the table is already set and which returns a TailEntry instead of an Entry. It allows for easy access to a tail entry with only the RowId required as input.

## resolveForeignPilot()

APilotEntry ADatabase::resolveForeignPilot | ( | RowId_T | foreign_key | ) |

Resolves the foreign key in a flight entry.

Returns
  The Pilot Entry referencted by the foreign key.

## resolveForeignTail()

ATailEntry ADatabase::resolveForeignTail | ( | RowId_T | foreign_key | ) |

Resolves the foreign key in a flight entry.

Returns
  The Tail Entry referencted by the foreign key.

## restoreBackup()

bool ADatabase::restoreBackup | ( | const QString & | backup_file | ) |

ADatabase::restoreBackup restores the database from a given backup file and replaces the currently active database.

Parameters

  backup_file | This is the full path and filename of the backup, e.g. 'home/Sully/myBackups/backupFromOpl.db'

## sqliteVersion()

const QString ADatabase::sqliteVersion | ( |  | ) | const

ADatabase::sqliteVersion returns the database sqlite version. See also dbRevision()

Returns
  sqlite version string

## Member Data Documentation

## lastError

QSqlError ADatabase::lastError

lastError extends QSqlError. Holds information about the last error that ocurred during a SQL operation. If the error type is QSqlError::UnknownError, the error is related to data from the database (entry not found,...), otherwise the error is related to SQL execution. In this case error.type() provides further information.

If the error type is QSqlError::NoError, the last executed database query was successful.

The documentation for this class was generated from the following files:

- src/database/adatabase.h
- src/database/adatabase.cpp