adatabase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef __DB_H__
  2. #define __DB_H__
  3. #include <QPair>
  4. #include <QMap>
  5. #include <QString>
  6. #include <QSqlQuery>
  7. #include <QSqlError>
  8. #include <QSqlTableModel>
  9. #include "src/database/dbinfo.h"
  10. #include "debug.h"
  11. #include "aentry.h"
  12. namespace experimental {
  13. /*!
  14. * \brief The DB class encapsulates the SQL database by providing fast access
  15. * to hot database data.
  16. */
  17. class ADataBase : public QObject {
  18. Q_OBJECT
  19. private:
  20. TableNames tableNames;
  21. TableColumns tableColumns;
  22. static ADataBase* instance;
  23. ADataBase() = default;
  24. public:
  25. // Ensure DB is not copiable or assignable
  26. ADataBase(const ADataBase&) = delete;
  27. void operator=(const ADataBase&) = delete;
  28. static ADataBase* getInstance();
  29. /*!
  30. * \brief The CompleterTarget enum provides the items for which QCompleter
  31. * completion lists are provided from the database.
  32. */
  33. enum CompleterTarget {airports, pilots, registrations, aircraft, companies};
  34. /*!
  35. * \brief Connect to the database and populate database information.
  36. */
  37. bool connect();
  38. /*!
  39. * \brief closes the database connection.
  40. */
  41. void disconnect();
  42. /*!
  43. * \brief Can be used to access the database connection.
  44. * \return The QSqlDatabase object pertaining to the connection.
  45. */
  46. static QSqlDatabase database();
  47. /*!
  48. * \brief Can be used to send a complex query to the database.
  49. * \param query - the full sql query statement
  50. * \param returnValues - the number of return values
  51. */
  52. QVector<QString> customQuery(QString statement, int return_values);
  53. /*!
  54. * \brief Checks if an entry exists in the database, based on position data
  55. */
  56. bool exists(AEntry entry);
  57. /*!
  58. * \brief commits an entry to the database, calls either insert or update,
  59. * based on position data
  60. */
  61. bool commit(AEntry entry);
  62. /*!
  63. * \brief Create new entry in the databse based on UserInput
  64. */
  65. bool insert(AEntry new_entry);
  66. /*!
  67. * \brief Updates entry in database from existing entry tweaked by the user.
  68. */
  69. bool update(AEntry updated_entry);
  70. /*!
  71. * \brief deletes an entry from the database.
  72. */
  73. bool remove(AEntry entry);
  74. /*!
  75. * \brief retreive entry data from the database to create an entry object
  76. */
  77. TableData getEntryData(DataPosition data_position);
  78. /*!
  79. * \brief retreive an Entry from the database.
  80. */
  81. AEntry getEntry(DataPosition data_position);
  82. /*!
  83. * \brief retreives a PilotEntry from the database.
  84. *
  85. * This function is a wrapper for DataBase::getEntry(DataPosition),
  86. * where the table is already set and which returns a PilotEntry
  87. * instead of an Entry. It allows for easy access to a pilot entry
  88. * with only the RowId required as input.
  89. */
  90. APilotEntry getPilotEntry(RowId row_id);
  91. // [G] TODO: Ensure PilotDialog works great and slowly move to
  92. // other dialogs
  93. /*!
  94. * \brief getCompletionList returns a QStringList of values for a
  95. * QCompleter based on database values
  96. * \return
  97. */
  98. QStringList getCompletionList(CompleterTarget);
  99. signals:
  100. void sqlSuccessful();
  101. void sqlError(const QSqlError &sqlError, const QString &sqlStatement);
  102. };
  103. /*!
  104. * \brief Convinience function that returns instance of DataBase.
  105. * Instead of this:
  106. * DataBase::getInstance().commit(...)
  107. * Write this:
  108. * aDB()->commit(...)
  109. */
  110. ADataBase* aDB();
  111. } // namespace experimental
  112. #endif