modelbasicoperation.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef MODELBASICOPERATION_HH
  2. #define MODELBASICOPERATION_HH
  3. #include "sqlquery.h"
  4. #include "sqlquerymanager.h"
  5. #include <QMap>
  6. #include <QSqlField>
  7. #include <QVector>
  8. CWF_BEGIN_NAMESPACE
  9. /**
  10. * @brief The ModelBasicOperation class implements several utility functions that are used in the Model class.
  11. */
  12. class CPPWEBFRAMEWORKSHARED_EXPORT ModelBasicOperation
  13. {
  14. protected:
  15. SqlDatabaseStorage& connection;
  16. public:
  17. /**
  18. * @brief Contructor
  19. */
  20. explicit ModelBasicOperation(SqlDatabaseStorage& connection);
  21. /**
  22. * @brief Destructor
  23. */
  24. ~ModelBasicOperation() = default;
  25. /**
  26. * @brief createTable Create a table in the database
  27. * @param name The name of the table to create
  28. * @return Bool
  29. */
  30. bool createTable(const QString& name);
  31. /**
  32. * @brief Creates a version table.
  33. * @return Bool
  34. */
  35. bool createVersionTable();
  36. /**
  37. * @brief addFieldToTable Add a column in a database table
  38. * @param fieldName The name of the column to add
  39. * @param type The type of data stored in the column
  40. * @param tableName The name of the database table
  41. * @return Bool
  42. */
  43. bool addFieldToTable(const QString& fieldName,
  44. const QVariant::Type type,
  45. const QString& tableName) const;
  46. /**
  47. * @brief changeTableVersion Change the version number of a database table
  48. * @param tableName The name of the table
  49. * @param version The version to be saved
  50. * @return Bool
  51. */
  52. bool changeTableVersion(const QString& tableName, qint32 version) const;
  53. /**
  54. * @brief tables List all the tables of a database
  55. * @return QStringList: a list of all tables
  56. */
  57. inline QStringList tables() const { return connection.getDatabase().tables(); }
  58. /**
  59. * @brief fields List all the fields of a database table
  60. * @param tableName The name of the database table
  61. * @return QStringList
  62. */
  63. QStringList fields(const QString& tableName) const;
  64. /**
  65. * @brief tableVersion Get the version of the database table
  66. * @param tableName The name of the database table
  67. * @return qint32
  68. */
  69. qint32 tableVersion(const QString& tableName) const;
  70. /**
  71. * @brief convertQVariantTypeToSQLType Convert a given type (enum) to a string representing an sql type
  72. * @param type The type of the data to be converted
  73. * @return QString
  74. */
  75. QString convertQVariantTypeToSQLType(const QVariant::Type type) const;
  76. /**
  77. * @brief save Save the data contained in a map (given as an argument) in the given database table.
  78. * @param tableName The name of the database table to receive a new entry
  79. * @param map The data to be added
  80. * @return qint64: The id of the new entry
  81. */
  82. qint64 save(const QString& tableName, const QMap<QString, QVariant>& map);
  83. /**
  84. * @brief buildVector Build a vector of map where each map represents an entry that was retrieved from the database
  85. * @param tableName The name of the database table on which the method should work
  86. * @param selectCondition A set of conditions to filter the entries of the database table
  87. * @param props The properties to be retrieved for each selected entry
  88. * @param orderBy What is the order by criteria ?
  89. * @return QVector<QMap<QString, QVariant> >
  90. */
  91. QVector<QMap<QString, QVariant> > buildVector(const QString& tableName,
  92. const QMap<QString, QVariant>& selectCondition,
  93. const QStringList& props,
  94. const QString& orderBy = "id");
  95. /**
  96. * @brief build Construct a map of properties retrieved from a single database entry
  97. * @param tableName The name of the table with the entry
  98. * @param selectCondition The conditions used to select the entry
  99. * @param props The properties to retrieve from the entry
  100. * @return QMap<QString, QVariant>
  101. */
  102. QMap<QString, QVariant> build(const QString& tableName,
  103. const QMap<QString, QVariant>& selectCondition,
  104. const QStringList& props);
  105. /**
  106. * @brief remove Delete an entry from the database
  107. * @param tableName The table with the entry
  108. * @param id The id of the entry to be deleted
  109. * @return Bool
  110. */
  111. bool remove(const QString& tableName, const qint64& id);
  112. /**
  113. * @brief constructInsertTextQuery Build an insert query and return its text version
  114. * @param tableName The name of the table in which an insertion should be done
  115. * @param map The data to be inserted
  116. * @param values The data to be inserted but sorted to match the field order
  117. * @return QString
  118. */
  119. QString constructInsertTextQuery(const QString& tableName,
  120. const QMap<QString, QVariant>& map,
  121. QVector<QVariant>& values);
  122. /**
  123. * @brief constructUpdateTextQuery
  124. * @param tableName
  125. * @param map The data to be inserted
  126. * @param values The data to be inserted but sorted to match the field order
  127. * @return QString
  128. */
  129. QString constructUpdateTextQuery(const QString& tableName,
  130. const QMap<QString, QVariant>& map,
  131. QVector<QVariant>& values);
  132. /**
  133. * @brief createIndex Create an index for a given table and column
  134. * @param tableName The name of the table to have an index
  135. * @param column The name of the column (or field) which should be indexed
  136. * @param unique Should the index be unique and enforce this ?
  137. * @return Bool
  138. */
  139. bool createIndex(const QString& tableName, const QString& column, bool unique = false) const;
  140. /**
  141. * @brief isTableInDb Check if a table is present within the database
  142. * @param tableName The name of the table
  143. * @toUtf8ool
  144. */
  145. inline bool isTableInDb(const QString& tableName) const
  146. {
  147. return connection.getDatabase().tables().contains(QLatin1String(tableName.toUtf8()));
  148. }
  149. private:
  150. /**
  151. * @brief insertEntry Insert an entry in a database table.
  152. * @param tableName The name of the table to receive the data
  153. * @param map The data
  154. * @return qint64: The id of the new entry
  155. */
  156. qint64 insertEntry(const QString& tableName, const QMap<QString, QVariant>& map);
  157. /**
  158. * @brief updateEntry Update a database entry
  159. * @param tableName The name of the database table to be updated
  160. * @param map The data to be inserted in the entry. An id should be present here to select the correct entry.
  161. */
  162. qint64 updateEntry(const QString& tableName, const QMap<QString, QVariant>& map);
  163. };
  164. CWF_END_NAMESPACE
  165. #endif // MODELBASICOPERATION_HH