sqlquery.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #ifndef SQLQUERY_H
  8. #define SQLQUERY_H
  9. #include <QVector>
  10. #include <QSqlQuery>
  11. #include <QSqlRecord>
  12. #include <QJsonArray>
  13. #include <QJsonObject>
  14. #include <QJsonValue>
  15. #include <QJsonDocument>
  16. #include "sqldatabasestorage.h"
  17. CWF_BEGIN_NAMESPACE
  18. /**
  19. * @brief The SqlQuery class was created to facilitate integration with SqlDataBaseStorage and manipulation to the database through JSON.
  20. */
  21. class CPPWEBFRAMEWORKSHARED_EXPORT SqlQuery : public QSqlQuery
  22. {
  23. QVector<QString> columns;
  24. void loadColumns();
  25. void makeBinds(const QVector<QVariant> &col, const QString &sql);
  26. QJsonObject validate(const QJsonObject &json, const QString &table);
  27. public:
  28. using QSqlQuery::QSqlQuery;
  29. /**
  30. * @brief Constructs a QSqlQuery object using the database dbStorage.
  31. * @param dbStorage
  32. */
  33. explicit SqlQuery(SqlDatabaseStorage &dbStorage);
  34. /**
  35. * @brief Executes and returns a JSON indicating success, or failure through the key success.
  36. * @return QJsonObject
  37. */
  38. QJsonObject exec();
  39. /**
  40. * @brief Executes a query and returns a JSON indicating success, or failure through the key "success" and "message".
  41. * @return QJsonObject
  42. */
  43. QJsonObject exec(const QString &query);
  44. /**
  45. * @brief Returns the result of a select converted to a QJsonArray.
  46. * If there is a failure, a JSON object with two fields will be returned: "success" and "message".
  47. * If there are no results, QJsonArray will return a empty object.
  48. * @return QJsonArray
  49. */
  50. QJsonArray toJson();
  51. /**
  52. * @brief Delete a record given a table and a condition.
  53. * Returns a JSON indicating success, or failure through the key "success" and "message".
  54. * @param table
  55. * @param condition
  56. * @return QJsonObject
  57. */
  58. QJsonObject deleteRecord(const QString &table, const QString &condition);
  59. /**
  60. * @brief Insert a record given a json and a table.
  61. * Returns a JSON indicating success, or failure through the key "success" and "message".
  62. * @param json
  63. * @param table
  64. * @return QJsonObject
  65. */
  66. QJsonObject insertFromJson(const QJsonObject &json, const QString &table);
  67. /**
  68. * @brief Update a record given a json, a table and a condition.
  69. * Returns a JSON indicating success, or failure through the key "success" and "message".
  70. * @param json
  71. * @param table
  72. * @return QJsonObject
  73. */
  74. QJsonObject updateFromJson(const QJsonObject &json, const QString &table, const QString &condition);
  75. };
  76. CWF_END_NAMESPACE
  77. #endif // SQLQUERY_H