2
0

DataBase.h 2.5 KB

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