2
0

adatabase.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /*!
  35. * \brief The DB class encapsulates the SQL database by providing fast access
  36. * to hot database data.
  37. */
  38. class ADataBase : public QObject {
  39. Q_OBJECT
  40. private:
  41. TableNames tableNames;
  42. TableColumns tableColumns;
  43. static ADataBase* instance;
  44. ADataBase() = default;
  45. public:
  46. // Ensure DB is not copiable or assignable
  47. ADataBase(const ADataBase&) = delete;
  48. void operator=(const ADataBase&) = delete;
  49. static ADataBase* getInstance();
  50. /*!
  51. * \brief The CompleterTarget enum provides the items for which QCompleter
  52. * completion lists are provided from the database.
  53. */
  54. enum DatabaseTarget {airport_identifier_icao, airport_identifier_iata, airport_identifier_all, airport_names, pilots, registrations, aircraft, companies, tails};
  55. /*!
  56. * \brief Connect to the database and populate database information.
  57. */
  58. bool connect();
  59. /*!
  60. * \brief closes the database connection.
  61. */
  62. void disconnect();
  63. /*!
  64. * \brief Can be used to access the database connection.
  65. * \return The QSqlDatabase object pertaining to the connection.
  66. */
  67. static QSqlDatabase database();
  68. /*!
  69. * \brief Can be used to send a complex query to the database.
  70. * \param query - the full sql query statement
  71. * \param returnValues - the number of return values
  72. */
  73. QVector<QString> customQuery(QString statement, int return_values);
  74. /*!
  75. * \brief Checks if an entry exists in the database, based on position data
  76. */
  77. bool exists(AEntry entry);
  78. bool exists(DataPosition data_position);
  79. /*!
  80. * \brief commits an entry to the database, calls either insert or update,
  81. * based on position data
  82. */
  83. bool commit(AEntry entry);
  84. /*!
  85. * \brief Create new entry in the databse based on UserInput
  86. */
  87. bool insert(AEntry new_entry);
  88. /*!
  89. * \brief Updates entry in database from existing entry tweaked by the user.
  90. */
  91. bool update(AEntry updated_entry);
  92. /*!
  93. * \brief deletes an entry from the database.
  94. */
  95. bool remove(AEntry entry);
  96. /*!
  97. * \brief deletes a list of entries from the database. Optimised for speed when
  98. * deleting many entries.
  99. */
  100. bool removeMany(QList<DataPosition>);
  101. /*!
  102. * \brief retreive entry data from the database to create an entry object
  103. */
  104. TableData getEntryData(DataPosition data_position);
  105. /*!
  106. * \brief retreive an Entry from the database.
  107. */
  108. AEntry getEntry(DataPosition data_position);
  109. /*!
  110. * \brief retreives a PilotEntry from the database.
  111. *
  112. * This function is a wrapper for DataBase::getEntry(DataPosition),
  113. * where the table is already set and which returns a PilotEntry
  114. * instead of an Entry. It allows for easy access to a pilot entry
  115. * with only the RowId required as input.
  116. */
  117. APilotEntry getPilotEntry(RowId row_id);
  118. /*!
  119. * \brief retreives a TailEntry from the database.
  120. *
  121. * This function is a wrapper for DataBase::getEntry(DataPosition),
  122. * where the table is already set and which returns a TailEntry
  123. * instead of an Entry. It allows for easy access to a tail entry
  124. * with only the RowId required as input.
  125. */
  126. ATailEntry getTailEntry(RowId row_id);
  127. /*!
  128. * \brief retreives a TailEntry from the database.
  129. *
  130. * This function is a wrapper for DataBase::getEntry(DataPosition),
  131. * where the table is already set and which returns an AAircraftEntry
  132. * instead of an AEntry. It allows for easy access to an aircraft entry
  133. * with only the RowId required as input.
  134. */
  135. AAircraftEntry getAircraftEntry(RowId row_id);
  136. /*!
  137. * \brief retreives a flight entry from the database.
  138. *
  139. * This function is a wrapper for DataBase::getEntry(DataPosition),
  140. * where the table is already set and which returns an AFlightEntry
  141. * instead of an AEntry. It allows for easy access to a flight entry
  142. * with only the RowId required as input.
  143. */
  144. AFlightEntry getFlightEntry(RowId row_id);
  145. /*!
  146. * \brief getCompletionList returns a QStringList of values for a
  147. * QCompleter based on database values
  148. * \return
  149. */
  150. const QStringList getCompletionList(DatabaseTarget);
  151. /*!
  152. * \brief returns a QMap<QString, int> of a human-readable database value and
  153. * its row id. Used in the Dialogs to map user input to unique database entries.
  154. * \return
  155. */
  156. const QMap<QString, int> getIdMap(DatabaseTarget);
  157. signals:
  158. void commitSuccessful();
  159. void deleteSuccessful();
  160. void sqlError(const QSqlError &sqlError, const QString &sqlStatement);
  161. };
  162. /*!
  163. * \brief Convinience function that returns instance of DataBase.
  164. * Instead of this:
  165. * DataBase::getInstance().commit(...)
  166. * Write this:
  167. * aDB()->commit(...)
  168. */
  169. ADataBase* aDB();
  170. } // namespace experimental
  171. #endif