adatabase.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 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 __DB_H__
  19. #define __DB_H__
  20. #include <QPair>
  21. #include <QMap>
  22. #include <QString>
  23. #include <QSqlQuery>
  24. #include <QSqlError>
  25. #include <QSqlTableModel>
  26. #include "src/database/dbinfo.h"
  27. #include "src/testing/adebug.h"
  28. #include "aentry.h"
  29. #include "apilotentry.h"
  30. #include "atailentry.h"
  31. #include "aaircraftentry.h"
  32. #include "aflightentry.h"
  33. namespace experimental {
  34. // [G]: Suspicious documentation -,O
  35. /*!
  36. * \brief The DBTarget enum provides the items for which QCompleter
  37. * completion lists are provided from the database.
  38. */
  39. enum class ADatabaseTarget
  40. {
  41. airport_identifier_icao,
  42. airport_identifier_iata,
  43. airport_identifier_all,
  44. airport_names,
  45. pilots,
  46. registrations,
  47. aircraft,
  48. companies,
  49. tails
  50. };
  51. // [G]: This is how we should handle custom "events" in the program.
  52. // In this case a custom error doesnt need to be built from scratch.
  53. // Find the type of error you want and extend it with a few tweaks.
  54. /*!
  55. * \brief Custom Database Error derived from QSqlError.
  56. * Extends text() adding "Database Error: " before the text.
  57. */
  58. class ADatabaseError : public QSqlError {
  59. public:
  60. ADatabaseError() = default;
  61. ADatabaseError(QString msg);
  62. QString text() const;
  63. };
  64. /*!
  65. * \brief The DB class encapsulates the SQL database by providing fast access
  66. * to hot database data.
  67. */
  68. class ADatabase : public QObject {
  69. Q_OBJECT
  70. private:
  71. TableNames tableNames;
  72. TableColumns tableColumns;
  73. static ADatabase* instance;
  74. ADatabase() = default;
  75. public:
  76. // Ensure DB is not copiable or assignable
  77. ADatabase(const ADatabase&) = delete;
  78. void operator=(const ADatabase&) = delete;
  79. static ADatabase* getInstance();
  80. ADatabaseError lastError;
  81. /*!
  82. * \brief Connect to the database and populate database information.
  83. */
  84. bool connect();
  85. /*!
  86. * \brief closes the database connection.
  87. */
  88. void disconnect();
  89. /*!
  90. * \brief Can be used to access the database connection.
  91. * \return The QSqlDatabase object pertaining to the connection.
  92. */
  93. static QSqlDatabase database();
  94. /*!
  95. * \brief Can be used to send a complex query to the database.
  96. * \param query - the full sql query statement
  97. * \param returnValues - the number of return values
  98. */
  99. QVector<QString> customQuery(QString statement, int return_values);
  100. /*!
  101. * \brief Checks if an entry exists in the database, based on position data
  102. */
  103. bool exists(AEntry entry);
  104. bool exists(DataPosition data_position);
  105. /*!
  106. * \brief commits an entry to the database, calls either insert or update,
  107. * based on position data
  108. */
  109. bool commit(AEntry entry);
  110. /*!
  111. * \brief Create new entry in the databse based on UserInput
  112. */
  113. bool insert(AEntry new_entry);
  114. /*!
  115. * \brief Updates entry in database from existing entry tweaked by the user.
  116. */
  117. bool update(AEntry updated_entry);
  118. /*!
  119. * \brief deletes an entry from the database.
  120. */
  121. bool remove(AEntry entry);
  122. /*!
  123. * \brief deletes a list of entries from the database. Optimised for speed when
  124. * deleting many entries.
  125. */
  126. bool removeMany(QList<DataPosition>);
  127. /*!
  128. * \brief retreive entry data from the database to create an entry object
  129. */
  130. TableData getEntryData(DataPosition data_position);
  131. /*!
  132. * \brief retreive an Entry from the database.
  133. */
  134. AEntry getEntry(DataPosition data_position);
  135. /*!
  136. * \brief retreives a PilotEntry from the database.
  137. *
  138. * This function is a wrapper for DataBase::getEntry(DataPosition),
  139. * where the table is already set and which returns a PilotEntry
  140. * instead of an Entry. It allows for easy access to a pilot entry
  141. * with only the RowId required as input.
  142. */
  143. APilotEntry getPilotEntry(RowId row_id);
  144. /*!
  145. * \brief retreives a TailEntry from the database.
  146. *
  147. * This function is a wrapper for DataBase::getEntry(DataPosition),
  148. * where the table is already set and which returns a TailEntry
  149. * instead of an Entry. It allows for easy access to a tail entry
  150. * with only the RowId required as input.
  151. */
  152. ATailEntry getTailEntry(RowId row_id);
  153. /*!
  154. * \brief retreives a TailEntry from the database.
  155. *
  156. * This function is a wrapper for DataBase::getEntry(DataPosition),
  157. * where the table is already set and which returns an AAircraftEntry
  158. * instead of an AEntry. It allows for easy access to an aircraft entry
  159. * with only the RowId required as input.
  160. */
  161. AAircraftEntry getAircraftEntry(RowId row_id);
  162. /*!
  163. * \brief retreives a flight entry from the database.
  164. *
  165. * This function is a wrapper for DataBase::getEntry(DataPosition),
  166. * where the table is already set and which returns an AFlightEntry
  167. * instead of an AEntry. It allows for easy access to a flight entry
  168. * with only the RowId required as input.
  169. */
  170. AFlightEntry getFlightEntry(RowId row_id);
  171. /*!
  172. * \brief getCompletionList returns a QStringList of values for a
  173. * QCompleter based on database values
  174. */
  175. const QStringList getCompletionList(ADatabaseTarget);
  176. /*!
  177. * \brief returns a QMap<QString, int> of a human-readable database value and
  178. * its row id. Used in the Dialogs to map user input to unique database entries.
  179. */
  180. const QMap<QString, int> getIdMap(ADatabaseTarget);
  181. /*!
  182. * \brief returns the ROWID for the newest entry in the respective database.
  183. */
  184. int getLastEntry(ADatabaseTarget);
  185. /*!
  186. * \brief returns a list of ROWID's in the flights table for which foreign key constraints
  187. * exist.
  188. */
  189. QList<int> getForeignKeyConstraints(int foreign_row_id, ADatabaseTarget target);
  190. signals:
  191. /*!
  192. * \brief updated is emitted whenever the database contents have been updated.
  193. * This can be either a commit, update or remove. This signal should be used to
  194. * trigger an update to the models of the views displaying database contents in
  195. * the user interface so that a user is always presented with up-to-date information.
  196. */
  197. void dataBaseUpdated();
  198. };
  199. /*!
  200. * \brief Convinience function that returns instance of DataBase.
  201. * Instead of this:
  202. * DataBase::getInstance().commit(...)
  203. * Write this:
  204. * aDB()->commit(...)
  205. */
  206. ADatabase* aDB();
  207. } // namespace experimental
  208. #endif