DBManager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include <utils/Logger.h>
  3. #include <QMap>
  4. #include <QVariant>
  5. #include <QPair>
  6. #include <QVector>
  7. class QSqlDatabase;
  8. class QSqlQuery;
  9. typedef QPair<QString,QVariant> CPair;
  10. typedef QVector<CPair> VectorPair;
  11. ///
  12. /// @brief Database interface for SQLite3.
  13. /// Inherit this class to create component specific methods based on this interface
  14. /// Usage: setTable(name) once before you use read/write actions
  15. /// To use another database use setDb(newDB) (defaults to "hyperion")
  16. ///
  17. /// Incompatible functions with SQlite3:
  18. /// QSqlQuery::size() returns always -1
  19. ///
  20. class DBManager : public QObject
  21. {
  22. Q_OBJECT
  23. public:
  24. DBManager(QObject* parent = nullptr);
  25. ~DBManager() override;
  26. /// set root path
  27. void setRootPath(const QString& rootPath);
  28. /// define the database to work with
  29. void setDatabaseName(const QString& dbn) { _dbn = dbn; };
  30. /// set a table to work with
  31. void setTable(const QString& table);
  32. /// get current database object set with setDB()
  33. QSqlDatabase getDB() const;
  34. ///
  35. /// @brief Create a table (if required) with the given columns. Older tables will be updated accordingly with missing columns
  36. /// Does not delete or migrate old columns
  37. /// @param[in] columns The columns of the table. Requires at least one entry!
  38. /// @return True on success else false
  39. ///
  40. bool createTable(QStringList& columns) const;
  41. ///
  42. /// @brief Create a column if the column already exists returns false and logs error
  43. /// @param[in] column The column of the table
  44. /// @return True on success else false
  45. ///
  46. bool createColumn(const QString& column) const;
  47. ///
  48. /// @brief Check if at least one record exists in table with the conditions
  49. /// @param[in] conditions The search conditions (WHERE)
  50. /// @return True on success else false
  51. ///
  52. bool recordExists(const VectorPair& conditions) const;
  53. ///
  54. /// @brief Create a new record in table when the conditions find no existing entry. Add additional key:value pairs in columns
  55. /// DO NOT repeat column keys between 'conditions' and 'columns' as they will be merged on creation
  56. /// @param[in] conditions conditions to search for, as a record may exist and should be updated instead (WHERE)
  57. /// @param[in] columns columns to create or update (optional)
  58. /// @return True on success else false
  59. ///
  60. bool createRecord(const VectorPair& conditions, const QVariantMap& columns = QVariantMap()) const;
  61. ///
  62. /// @brief Update a record with conditions and additional key:value pairs in columns
  63. /// @param[in] conditions conditions which rows should be updated (WHERE)
  64. /// @param[in] columns columns to update
  65. /// @return True on success else false
  66. ///
  67. bool updateRecord(const VectorPair& conditions, const QVariantMap& columns) const;
  68. ///
  69. /// @brief Get data of a single record, multiple records are not supported
  70. /// @param[in] conditions condition to search for (WHERE)
  71. /// @param[out] results results of query
  72. /// @param[in] tColumns target columns to search in (optional) if not provided returns all columns
  73. /// @param[in] tOrder target order columns with order by ASC/DESC (optional)
  74. /// @return True on success else false
  75. ///
  76. bool getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns = QStringList(), const QStringList& tOrder = QStringList()) const;
  77. ///
  78. /// @brief Get data of multiple records, you need to specify the columns. This search is without conditions. Good to grab all data from db
  79. /// @param[in] conditions condition to search for (WHERE)
  80. /// @param[out] results results of query
  81. /// @param[in] tColumns target columns to search in (optional) if not provided returns all columns
  82. /// @param[in] tOrder target order columns with order by ASC/DESC (optional)
  83. /// @return True on success else false
  84. ///
  85. bool getRecords(QVector<QVariantMap>& results, const QStringList& tColumns = QStringList(), const QStringList& tOrder = QStringList()) const;
  86. ///
  87. /// @brief Delete a record determined by conditions
  88. /// @param[in] conditions conditions of the row to delete it (WHERE)
  89. /// @return True on success; on error or not found false
  90. ///
  91. bool deleteRecord(const VectorPair& conditions) const;
  92. ///
  93. /// @brief Check if table exists in current database
  94. /// @param[in] table The name of the table
  95. /// @return True on success else false
  96. ///
  97. bool tableExists(const QString& table) const;
  98. ///
  99. /// @brief Delete a table, fails silent (return true) if table is not found
  100. /// @param[in] table The name of the table
  101. /// @return True on success else false
  102. ///
  103. bool deleteTable(const QString& table) const;
  104. ///
  105. /// @brief Sets a table in read-only mode.
  106. /// Updates will not written to the table
  107. /// @param[in] readOnly True read-only, false - read/write
  108. ///
  109. void setReadonlyMode(bool readOnly) { _readonlyMode = readOnly; };
  110. private:
  111. Logger* _log;
  112. /// databse connection & file name, defaults to hyperion
  113. QString _dbn = "hyperion";
  114. /// table in database
  115. QString _table;
  116. bool _readonlyMode;
  117. /// addBindValue to query given by QVariantList
  118. void doAddBindValue(QSqlQuery& query, const QVariantList& variants) const;
  119. };