adatabase.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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
  47. QSqlDatabase database();
  48. /*!
  49. * \brief Checks if an entry exists in the database, based on position data
  50. */
  51. bool exists(AEntry entry);
  52. /*!
  53. * \brief commits an entry to the database, calls either insert or update,
  54. * based on position data
  55. */
  56. bool commit(AEntry entry);
  57. /*!
  58. * \brief Create new entry in the databse based on UserInput
  59. */
  60. bool insert(AEntry new_entry);
  61. /*!
  62. * \brief Updates entry in database from existing entry tweaked by the user.
  63. */
  64. bool update(AEntry updated_entry);
  65. /*!
  66. * \brief deletes an entry from the database.
  67. */
  68. bool remove(AEntry entry);
  69. /*!
  70. * \brief retreive entry data from the database to create an entry object
  71. */
  72. TableData getEntryData(DataPosition data_position);
  73. /*!
  74. * \brief retreive an Entry from the database.
  75. */
  76. AEntry getEntry(DataPosition data_position);
  77. /*!
  78. * \brief retreives a PilotEntry from the database.
  79. *
  80. * This function is a wrapper for DataBase::getEntry(DataPosition),
  81. * where the table is already set and which returns a PilotEntry
  82. * instead of an Entry. It allows for easy access to a pilot entry
  83. * with only the RowId required as input.
  84. */
  85. APilotEntry getPilotEntry(RowId row_id);
  86. // [G] TODO: Ensure PilotDialog works great and slowly move to
  87. // other dialogs
  88. /*!
  89. * \brief getCompletionList returns a QStringList of values for a
  90. * QCompleter based on database values
  91. * \return
  92. */
  93. QStringList getCompletionList(CompleterTarget);
  94. signals:
  95. void commitSuccessful();
  96. void sqlError(const QSqlError &sqlError, const QString &sqlStatement);
  97. };
  98. /*!
  99. * \brief Convinience function that returns instance of DataBase.
  100. * Instead of this:
  101. * DataBase::getInstance().commit(...)
  102. * Write this:
  103. * aDB()->commit(...)
  104. */
  105. ADataBase* aDB();
  106. } // namespace experimental
  107. #endif