DataBase.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "Entry.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 DataBase : public QObject {
  18. Q_OBJECT
  19. private:
  20. TableNames tableNames;
  21. TableColumns tableColumns;
  22. static DataBase* instance;
  23. DataBase() = default;
  24. public:
  25. // Ensure DB is not copiable or assignable
  26. DataBase(const DataBase&) = delete;
  27. void operator=(const DataBase&) = delete;
  28. static DataBase* getInstance();
  29. /*!
  30. * \brief Connect to the database and populate database information.
  31. */
  32. bool connect();
  33. /*!
  34. * \brief closes the database connection.
  35. */
  36. void disconnect();
  37. /*!
  38. * \brief Can be used to access the database connection.
  39. * \return The QSqlDatabase object pertaining to the connection.
  40. */
  41. static QSqlDatabase database();
  42. /*!
  43. * \brief Checks if an entry exists in the database, based on position data
  44. */
  45. bool exists(Entry entry);
  46. /*!
  47. * \brief commits an entry to the database, calls either insert or update,
  48. * based on position data
  49. */
  50. bool commit(Entry entry);
  51. /*!
  52. * \brief Create new entry in the databse based on UserInput
  53. */
  54. bool insert(Entry newEntry);
  55. /*!
  56. * \brief Updates entry in database from existing entry tweaked by the user.
  57. */
  58. bool update(Entry updated_entry);
  59. /*!
  60. * \brief deletes an entry from the database.
  61. */
  62. bool remove(Entry entry);
  63. /*!
  64. * \brief retreive entry data from the database to create an entry object
  65. */
  66. TableData getEntryData(DataPosition);
  67. /*!
  68. * \brief retreive an Entry from the database.
  69. */
  70. Entry getEntry(DataPosition);
  71. /*!
  72. * \brief retreives a PilotEntry from the database.
  73. *
  74. * This function is a wrapper for DataBase::getEntry(DataPosition),
  75. * where the table is already set and which returns a PilotEntry
  76. * instead of an Entry. It allows for easy access to a pilot entry
  77. * with only the RowId required as input.
  78. */
  79. PilotEntry getPilotEntry(RowId);
  80. ///[F] the same can easily be implemented for tails/flights
  81. signals:
  82. void commitSuccessful();
  83. void commitUnsuccessful(const QString &sqlError, const QString &sqlStatement);
  84. };
  85. /*!
  86. * \brief Convinience function that returns instance of DataBase.
  87. * Instead of this:
  88. * DataBase::getInstance().commit(...)
  89. * Write this:
  90. * DB()->commit(...)
  91. */
  92. DataBase* DB();
  93. } // namespace experimental
  94. #endif