sqlquerymanager.h 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef SQLQUERYMANAGER_HH
  2. #define SQLQUERYMANAGER_HH
  3. #include "cppwebframework_global.h"
  4. #include "sqlquery.h"
  5. #include <memory>
  6. CWF_BEGIN_NAMESPACE
  7. class Model;
  8. /**
  9. * @brief The SqlQueryManager class allows to create query by chaining public methods instead of writing raw sql.
  10. *
  11. * The class is meant as an interface to create query without writing raw sql. A query can be created by chaining the public
  12. * methods of the call that emulates sql statements. The sql is generated from within these methods and should be adaptated to
  13. * suit the database type (MySQL, SQLite, Postgresql...) currently used.
  14. */
  15. class CPPWEBFRAMEWORKSHARED_EXPORT SqlQueryManager
  16. {
  17. SqlDatabaseStorage& connection;
  18. QString queryText; ///< @brief The text of the query being contructed by the manager
  19. CWF::SqlQuery query; ///< @brief The query being constructed
  20. qint32 bindingDone = 0; ///< @brief How many bindings were done with the current query ?
  21. public:
  22. explicit SqlQueryManager(SqlDatabaseStorage& connection)
  23. : connection(connection)
  24. , query(connection)
  25. {} ///< @brief Constructor
  26. /**
  27. * @brief reset Reset the query manager
  28. */
  29. void reset();
  30. /**
  31. * @brief createTable Construct a query to create a new table in a database
  32. * @param tableName The name of the table to be added
  33. */
  34. QString createTable(const QString& tableName);
  35. /**
  36. * @brief createIndex Construct a query to create a new index in the database
  37. * @param indexName The name of the new index
  38. * @param tableName The name of the table targeted
  39. * @param field The name of the column targeted
  40. * @param unique Should the index be unique ?
  41. */
  42. void createIndex(const QString& indexName,
  43. const QString& tableName,
  44. const QString& field,
  45. bool unique = false);
  46. // clang-format off
  47. SqlQueryManager& alterTable(const QString& tableName); ///< @brief Alter a table (can be chained)
  48. SqlQueryManager& addColumn(const QString& field, const QString& type); ///< @brief Add a column (can be chained)
  49. SqlQueryManager& select(const QString& what, const QString& tableName); ///< @brief Select statement (can be chained)
  50. SqlQueryManager& count(const QString& what, const QString& tableName); ///< @brief Count statement (can be chained)
  51. SqlQueryManager& insert(const QString& tableName, const QString& fields); ///< @brief Insert statement (can be chained)
  52. SqlQueryManager& remove(const QString& tableName, const QString& cond); ///< @brief Remove statement (can be chained)
  53. SqlQueryManager& update(const QString& tableName, const QString& fieldValue); ///< @brief Update statement (can be chained)
  54. SqlQueryManager& where(const QString& c); ///< @brief Where statement (can be chained)
  55. SqlQueryManager& orderBy(const QString& c); ///< @brief OrderBy statement (can be chained)
  56. SqlQueryManager& leftJoin(const QString& tableName, const QString& cond); ///< @brief leftJoin statement (can be chained)
  57. SqlQueryManager& innerJoin(const QString& tableName, const QString& cond); ///< @brief innerJoin statement (can be chained)
  58. SqlQueryManager& addBindValue(const QVariant& v); ///< @brief addBindValue statemnet (can be chained)
  59. // clang-format on
  60. bool prepare(); ///< @brief Prepare the query
  61. QJsonObject exec(); ///< @brief // Executte the query
  62. QJsonObject exec(const QString& sql); ///< @brief // Executte the query
  63. inline QJsonArray toJson()
  64. {
  65. return query.toJson();
  66. } ///< @brief // Get the result of the query in JSON format
  67. /**
  68. * @brief textQuery Get the text of the query (raw sql)
  69. * @param addEndDot Should we add ";" at the end of the query if it is not present ?
  70. * @return QString
  71. */
  72. QString textQuery(bool addEndDot = false) const;
  73. /**
  74. * @brief prefixPropNames Create a string made of all the properties of a given model. These are all prefixed by the name of the model.
  75. * @param model The targeted model object
  76. * @return QString
  77. *
  78. * This feature is useful when building complex leftJoin request: it allows to build aliases to avoid name conflicts.
  79. */
  80. QString prefixPropNames(Model& model);
  81. inline QString getQueryText() const { return queryText; }
  82. };
  83. CWF_END_NAMESPACE
  84. #endif // SQLQUERYMANAGER_HH