adatabase.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #define SQLITE_DRIVER QStringLiteral("QSQLITE")
  40. /*!
  41. * \brief Convinience macro that returns instance of DataBase.
  42. * Instead of this:
  43. * DataBase::getInstance().commit(...)
  44. * Write this:
  45. * aDB->commit(...)
  46. */
  47. #define aDB ADatabase::instance()
  48. /*!
  49. * \brief The DBTarget enum lists database items that are
  50. * used by completers, for content matching or need to be accessed programatically.
  51. */
  52. enum class ADatabaseTarget
  53. {
  54. aircraft,
  55. airport_identifier_icao,
  56. airport_identifier_iata,
  57. airport_identifier_all,
  58. airport_names,
  59. pilots,
  60. registrations,
  61. companies,
  62. tails
  63. };
  64. // [G]: This is how we should handle custom "events" in the program.
  65. // In this case a custom error doesnt need to be built from scratch.
  66. // Find the type of error you want and extend it with a few tweaks.
  67. /*!
  68. * \brief Custom Database Error derived from QSqlError.
  69. * Extends text() adding "Database Error: " before the text.
  70. */
  71. class ADatabaseError : public QSqlError {
  72. public:
  73. ADatabaseError() = default;
  74. ADatabaseError(QString msg);
  75. QString text() const;
  76. };
  77. /*!
  78. * \brief The DB class encapsulates the SQL database by providing fast access
  79. * to hot database data.
  80. */
  81. class ADatabase : public QObject {
  82. Q_OBJECT
  83. public:
  84. private:
  85. ADatabase();
  86. static ADatabase* self;
  87. TableNames_T tableNames;
  88. TableColumns_T tableColumns;
  89. public:
  90. // Ensure DB is not copiable or assignable
  91. ADatabase(const ADatabase&) = delete;
  92. void operator=(const ADatabase&) = delete;
  93. static ADatabase* instance();
  94. TableNames_T getTableNames() const;
  95. ColumnNames_T getTableColumns(TableName_T table_name) const;
  96. void updateLayout();
  97. const QString sqliteVersion();
  98. ADatabaseError lastError;
  99. //const QDir databaseDir;
  100. const QFileInfo databaseFile;
  101. /*!
  102. * \brief Connect to the database and populate database information.
  103. */
  104. bool connect();
  105. /*!
  106. * \brief closes the database connection.
  107. */
  108. void disconnect();
  109. /*!
  110. * \brief Can be used to access the database connection.
  111. * \return The QSqlDatabase object pertaining to the connection.
  112. */
  113. static QSqlDatabase database();
  114. /*!
  115. * \brief Can be used to send a complex query to the database.
  116. * \param query - the full sql query statement
  117. * \param returnValues - the number of return values
  118. */
  119. QVector<QString> customQuery(QString statement, int return_values);
  120. /*!
  121. * \brief Checks if an entry exists in the database, based on position data
  122. */
  123. bool exists(AEntry entry);
  124. bool exists(DataPosition data_position);
  125. /*!
  126. * \brief commits an entry to the database, calls either insert or update,
  127. * based on position data
  128. */
  129. bool commit(AEntry entry);
  130. /*!
  131. * \brief Create new entry in the databse based on UserInput
  132. */
  133. bool insert(AEntry new_entry);
  134. /*!
  135. * \brief Updates entry in database from existing entry tweaked by the user.
  136. */
  137. bool update(AEntry updated_entry);
  138. /*!
  139. * \brief deletes an entry from the database.
  140. */
  141. bool remove(AEntry entry);
  142. /*!
  143. * \brief deletes a list of entries from the database. Optimised for speed when
  144. * deleting many entries.
  145. */
  146. bool removeMany(QList<DataPosition>);
  147. /*!
  148. * \brief retreive entry data from the database to create an entry object
  149. */
  150. RowData_T getEntryData(DataPosition data_position);
  151. /*!
  152. * \brief retreive an Entry from the database.
  153. */
  154. AEntry getEntry(DataPosition data_position);
  155. /*!
  156. * \brief retreives a PilotEntry from the database.
  157. *
  158. * This function is a wrapper for DataBase::getEntry(DataPosition),
  159. * where the table is already set and which returns a PilotEntry
  160. * instead of an Entry. It allows for easy access to a pilot entry
  161. * with only the RowId required as input.
  162. */
  163. APilotEntry getPilotEntry(RowId_T row_id);
  164. /*!
  165. * \brief retreives a TailEntry from the database.
  166. *
  167. * This function is a wrapper for DataBase::getEntry(DataPosition),
  168. * where the table is already set and which returns a TailEntry
  169. * instead of an Entry. It allows for easy access to a tail entry
  170. * with only the RowId required as input.
  171. */
  172. ATailEntry getTailEntry(RowId_T row_id);
  173. /*!
  174. * \brief retreives a TailEntry from the database.
  175. *
  176. * This function is a wrapper for DataBase::getEntry(DataPosition),
  177. * where the table is already set and which returns an AAircraftEntry
  178. * instead of an AEntry. It allows for easy access to an aircraft entry
  179. * with only the RowId required as input.
  180. */
  181. AAircraftEntry getAircraftEntry(RowId_T row_id);
  182. /*!
  183. * \brief retreives a flight entry from the database.
  184. *
  185. * This function is a wrapper for DataBase::getEntry(DataPosition),
  186. * where the table is already set and which returns an AFlightEntry
  187. * instead of an AEntry. It allows for easy access to a flight entry
  188. * with only the RowId required as input.
  189. */
  190. AFlightEntry getFlightEntry(RowId_T row_id);
  191. /*!
  192. * \brief getCompletionList returns a QStringList of values for a
  193. * QCompleter based on database values
  194. */
  195. const QStringList getCompletionList(ADatabaseTarget target);
  196. /*!
  197. * \brief returns a QMap<QString, RowId_t> of a human-readable database value and
  198. * its row id. Used in the Dialogs to map user input to unique database entries.
  199. * \todo What is this QString semantically? As i understand its a "QueryResult" QVariant cast to QString
  200. */
  201. const QMap<QString, RowId_T> getIdMap(ADatabaseTarget target);
  202. /*!
  203. * \brief returns the ROWID for the newest entry in the respective database.
  204. */
  205. int getLastEntry(ADatabaseTarget target);
  206. /*!
  207. * \brief returns a list of ROWID's in the flights table for which foreign key constraints
  208. * exist.
  209. */
  210. QList<RowId_T> getForeignKeyConstraints(RowId_T foreign_row_id, ADatabaseTarget target);
  211. /*!
  212. * \brief Resolves the foreign key in a flight entry
  213. * \return The Pilot Entry referencted by the foreign key.
  214. */
  215. APilotEntry resolveForeignPilot(RowId_T foreign_key);
  216. /*!
  217. * \brief Resolves the foreign key in a flight entry
  218. * \return The Tail Entry referencted by the foreign key.
  219. */
  220. ATailEntry resolveForeignTail(RowId_T foreign_key);
  221. signals:
  222. /*!
  223. * \brief updated is emitted whenever the database contents have been updated.
  224. * This can be either a commit, update or remove. This signal should be used to
  225. * trigger an update to the models of the views displaying database contents in
  226. * the user interface so that a user is always presented with up-to-date information.
  227. */
  228. void dataBaseUpdated();
  229. };
  230. #endif // ADATABASE_H