2
0

DataBase.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "src/database/dbinfo.h"
  9. #include "debug.h"
  10. #include "src/experimental/UserInput.h"
  11. //#include "Decl.h"
  12. #include "Entry.h"
  13. namespace experimental {
  14. /// [F] ideas for functions of db class:
  15. /// https://github.com/fiffty-50/openpilotlog/wiki/New-DB-class-brainstorming
  16. /*!
  17. * \brief The DB class encapsulates the SQL database by providing fast access
  18. * to hot database data.
  19. */
  20. class DataBase {
  21. private:
  22. QStringList tableNames;
  23. QMap<QString, QStringList> tableColumns;
  24. static DataBase* instance;
  25. DataBase() = default;
  26. signals:
  27. void commitSuccessful();
  28. void commitUnsuccessful(QString message);
  29. public:
  30. // Ensure DB is not copiable or assignable
  31. DataBase(const DataBase&) = delete;
  32. void operator=(const DataBase&) = delete;
  33. static DataBase* getInstance();
  34. /*!
  35. * \brief Connect to the database and populate database information.
  36. */
  37. bool connect();
  38. /*!
  39. * \brief closes the database connection.
  40. */
  41. bool disconnect();
  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. /*!
  65. * \brief Convinience function that returns instance of DataBase.
  66. * Instead of this:
  67. * DataBase::getInstance().commit(...)
  68. * Write this:
  69. * DB().commit(...)
  70. */
  71. DataBase* DB();
  72. } // namespace experimental
  73. #endif