adatabase.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 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 ADATABASE_H
  19. #define ADATABASE_H
  20. #include <QPair>
  21. #include <QMap>
  22. #include <QString>
  23. #include <QDir>
  24. #include <QSqlDatabase>
  25. #include <QSqlDriver>
  26. #include <QSqlQuery>
  27. #include <QSqlError>
  28. #include <QSqlTableModel>
  29. #include <QSqlQuery>
  30. #include <QSqlRecord>
  31. #include <QSqlField>
  32. #include "src/database/adatabasetypes.h"
  33. #include "src/classes/aentry.h"
  34. #include "src/classes/apilotentry.h"
  35. #include "src/classes/atailentry.h"
  36. #include "src/classes/aaircraftentry.h"
  37. #include "src/classes/aflightentry.h"
  38. #include "src/classes/astandardpaths.h"
  39. #include "src/classes/acurrencyentry.h"
  40. #define SQLITE_DRIVER QStringLiteral("QSQLITE")
  41. /*!
  42. * \brief Convinience macro that returns instance of DataBase.
  43. * Instead of this:
  44. * DataBase::getInstance().commit(...)
  45. * Write this:
  46. * aDB->commit(...)
  47. */
  48. #define aDB ADatabase::instance()
  49. /*!
  50. * \brief The DBTarget enum lists database items that are
  51. * used by completers, for content matching or need to be accessed programatically.
  52. */
  53. enum class ADatabaseTarget
  54. {
  55. aircraft,
  56. airport_identifier_icao,
  57. airport_identifier_iata,
  58. airport_identifier_all,
  59. airport_names,
  60. pilots,
  61. registrations,
  62. companies,
  63. tails
  64. };
  65. /*!
  66. * \brief Enumerates the QMap keys used when summarising a database
  67. */
  68. enum class ADatabaseSummaryKey {
  69. total_flights,
  70. total_tails,
  71. total_pilots,
  72. last_flight,
  73. total_time,
  74. };
  75. /*!
  76. * \brief Custom Database Error derived from QSqlError.
  77. * Extends text() adding "Database Error: " before the text.
  78. */
  79. class ADatabaseError : public QSqlError {
  80. public:
  81. ADatabaseError() = default;
  82. ADatabaseError(QString msg);
  83. QString text() const;
  84. };
  85. /*!
  86. * \brief The DB class encapsulates the SQL database by providing fast access
  87. * to hot database data.
  88. */
  89. class ADatabase : public QObject {
  90. Q_OBJECT
  91. private:
  92. static ADatabase* self;
  93. TableNames_T tableNames;
  94. TableColumns_T tableColumns;
  95. int databaseRevision;
  96. ADatabase();
  97. int checkDbVersion() const;
  98. public:
  99. /*!
  100. * \brief lastError extends QSqlError. Holds information about the last error that ocurred during
  101. * a SQL operation.
  102. */
  103. ADatabaseError lastError;
  104. const QFileInfo databaseFile;
  105. // Ensure DB is not copiable or assignable
  106. ADatabase(const ADatabase&) = delete;
  107. void operator=(const ADatabase&) = delete;
  108. static ADatabase* instance();
  109. /*!
  110. * \brief dbRevision returns the database Revision Number. The Revision refers to what iteration
  111. * of the database layout is used. For the sqlite version of the database refer to sqliteVersion()
  112. * \return
  113. */
  114. int dbRevision() const;
  115. /*!
  116. * \brief Return the names of all tables in the database
  117. */
  118. TableNames_T getTableNames() const;
  119. /*!
  120. * \brief Return the names of a given table in the database.
  121. */
  122. ColumnNames_T getTableColumns(TableName_T table_name) const;
  123. /*!
  124. * \brief Updates the member variables tableNames and tableColumns with up-to-date layout information
  125. * if the database has been altered. This function is normally only required during database setup or maintenance.
  126. */
  127. void updateLayout();
  128. /*!
  129. * \brief ADatabase::sqliteVersion returns the database sqlite version. See also dbRevision()
  130. * \return sqlite version string
  131. */
  132. const QString sqliteVersion() const;
  133. /*!
  134. * \brief Connect to the database and populate database information.
  135. */
  136. bool connect();
  137. /*!
  138. * \brief closes the database connection.
  139. */
  140. void disconnect();
  141. /*!
  142. * \brief Can be used to access the database connection.
  143. * \return The QSqlDatabase object pertaining to the connection.
  144. */
  145. static QSqlDatabase database();
  146. /*!
  147. * \brief Can be used to send a complex query to the database.
  148. * \param query - the full sql query statement
  149. * \param returnValues - the number of return values
  150. */
  151. QVector<QVariant> customQuery(QString statement, int return_values);
  152. /*!
  153. * \brief Checks if an entry exists in the database, based on position data
  154. */
  155. bool exists(AEntry entry);
  156. bool exists(DataPosition data_position);
  157. /*!
  158. * \brief commits an entry to the database, calls either insert or update,
  159. * based on position data
  160. */
  161. bool commit(AEntry entry);
  162. /*!
  163. * \brief Create new entry in the databse based on UserInput
  164. */
  165. bool insert(AEntry new_entry);
  166. /*!
  167. * \brief Updates entry in database from existing entry tweaked by the user.
  168. */
  169. bool update(AEntry updated_entry);
  170. /*!
  171. * \brief deletes an entry from the database.
  172. */
  173. bool remove(AEntry entry);
  174. /*!
  175. * \brief deletes a list of entries from the database. Optimised for speed when
  176. * deleting many entries.
  177. */
  178. bool removeMany(QList<DataPosition>);
  179. /*!
  180. * \brief retreive entry data from the database to create an entry object
  181. */
  182. RowData_T getEntryData(DataPosition data_position);
  183. /*!
  184. * \brief retreive an Entry from the database.
  185. */
  186. AEntry getEntry(DataPosition data_position);
  187. /*!
  188. * \brief retreives a PilotEntry from the database.
  189. *
  190. * This function is a wrapper for DataBase::getEntry(DataPosition),
  191. * where the table is already set and which returns a PilotEntry
  192. * instead of an Entry. It allows for easy access to a pilot entry
  193. * with only the RowId required as input.
  194. */
  195. APilotEntry getPilotEntry(RowId_T row_id);
  196. /*!
  197. * \brief retreives a TailEntry from the database.
  198. *
  199. * This function is a wrapper for DataBase::getEntry(DataPosition),
  200. * where the table is already set and which returns a TailEntry
  201. * instead of an Entry. It allows for easy access to a tail entry
  202. * with only the RowId required as input.
  203. */
  204. ATailEntry getTailEntry(RowId_T row_id);
  205. /*!
  206. * \brief retreives a TailEntry from the database.
  207. *
  208. * This function is a wrapper for DataBase::getEntry(DataPosition),
  209. * where the table is already set and which returns an AAircraftEntry
  210. * instead of an AEntry. It allows for easy access to an aircraft entry
  211. * with only the RowId required as input.
  212. */
  213. AAircraftEntry getAircraftEntry(RowId_T row_id);
  214. /*!
  215. * \brief retreives a flight entry from the database.
  216. *
  217. * This function is a wrapper for DataBase::getEntry(DataPosition),
  218. * where the table is already set and which returns an AFlightEntry
  219. * instead of an AEntry. It allows for easy access to a flight entry
  220. * with only the RowId required as input.
  221. */
  222. AFlightEntry getFlightEntry(RowId_T row_id);
  223. /*!
  224. * \brief Retreives a currency entry from the database.
  225. */
  226. ACurrencyEntry getCurrencyEntry(ACurrencyEntry::CurrencyName currency_name);
  227. /*!
  228. * \brief getCompletionList returns a QStringList of values for a
  229. * QCompleter based on database values
  230. */
  231. const QStringList getCompletionList(ADatabaseTarget target);
  232. /*!
  233. * \brief returns a QMap<QString, RowId_t> of a human-readable database value and
  234. * its row id. Used in the Dialogs to map user input to unique database entries.
  235. * \todo What is this QString semantically? As i understand its a "QueryResult" QVariant cast to QString
  236. */
  237. const QMap<QString, RowId_T> getIdMap(ADatabaseTarget target);
  238. /*!
  239. * \brief returns the ROWID for the newest entry in the respective database.
  240. */
  241. int getLastEntry(ADatabaseTarget target);
  242. /*!
  243. * \brief returns a list of ROWID's in the flights table for which foreign key constraints
  244. * exist.
  245. */
  246. QList<RowId_T> getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTarget target);
  247. /*!
  248. * \brief Resolves the foreign key in a flight entry
  249. * \return The Pilot Entry referencted by the foreign key.
  250. */
  251. APilotEntry resolveForeignPilot(RowId_T foreign_key);
  252. /*!
  253. * \brief Resolves the foreign key in a flight entry
  254. * \return The Tail Entry referencted by the foreign key.
  255. */
  256. ATailEntry resolveForeignTail(RowId_T foreign_key);
  257. /*!
  258. * \brief Return a summary of a database
  259. * \details Creates a summary of the database giving a quick overview of the relevant contents. The
  260. * function runs several specialised SQL queries to create a QMap<ADatabaseSummaryKey, QString> containing
  261. * Total Flight Time, Number of unique aircraft and pilots, as well as the date of last flight. Uses a temporary
  262. * database connection separate from the default connection in order to not tamper with the currently active
  263. * database connection.
  264. */
  265. QMap<ADatabaseSummaryKey, QString> databaseSummary(const QString& db_path);
  266. /*!
  267. * \brief returns a short summary string of the database, containing total time and date of last flight.
  268. */
  269. const QString databaseSummaryString(const QString& db_path);
  270. bool restoreBackup(const QString& backup_file);
  271. bool createBackup(const QString& dest_file);
  272. signals:
  273. /*!
  274. * \brief updated is emitted whenever the database contents have been updated.
  275. * This can be either a commit, update or remove. This signal should be used to
  276. * trigger an update to the models of the views displaying database contents in
  277. * the user interface so that a user is always presented with up-to-date information.
  278. */
  279. void dataBaseUpdated();
  280. /*!
  281. * \brief connectionReset is emitted whenever the database connection is reset, for
  282. * example when creating or restoring a backup.
  283. */
  284. void connectionReset();
  285. };
  286. #endif // ADATABASE_H