EasyQtSql_DeleteQuery.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef EASYQTSQL_DELETEQUERY_H
  2. #define EASYQTSQL_DELETEQUERY_H
  3. /*
  4. * The MIT License (MIT)
  5. * Copyright 2018 Alexey Kramin
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. #ifndef EASY_QT_SQL_MAIN
  28. #include <QtSql>
  29. #include "EasyQtSql_NonQueryResult.h"
  30. #endif
  31. /*!
  32. \brief QSqlQuery wrapper for <em>DELETE FROM ... WHERE ..</em> query execution.
  33. */
  34. class DeleteQuery
  35. {
  36. public:
  37. DeleteQuery(const QString &table, const QSqlDatabase &db)
  38. : m_query(db)
  39. , m_table(table)
  40. { }
  41. /*!
  42. \brief Executes conditional <em>DELETE FROM table WHERE expr</em> query
  43. \code
  44. //DELETE FROM table WHERE 1=0
  45. t.deleteFrom("table").where("1=0");
  46. \endcode
  47. */
  48. NonQueryResult where(const QString &expr)
  49. {
  50. const QString &sql = createSql(m_table, expr);
  51. const bool res = m_query.exec(sql);
  52. #ifdef DB_EXCEPTIONS_ENABLED
  53. if (!res)
  54. throw DBException(m_query);
  55. #endif
  56. return NonQueryResult(m_query);
  57. }
  58. /*!
  59. \brief Executes conditional <em>DELETE FROM table WHERE expr</em> query with parameter binding
  60. The method supports variable count of QVariant parameters.
  61. Parameters are bound with <em>QSqlQuery::addBindValue</em>.
  62. \code
  63. //DELETE FROM table WHERE a=1 AND b=2
  64. t.deleteFrom("table").where("a=? AND b=?", 1, 2);
  65. \endcode
  66. */
  67. NonQueryResult where(const QString &expr, const QVariant &last)
  68. {
  69. m_params.append(last);
  70. const QString &sql = createSql(m_table, expr);
  71. m_query.prepare(sql);
  72. for (int i = 0; i < m_params.count(); ++i)
  73. {
  74. m_query.addBindValue(m_params.at(i));
  75. }
  76. const bool res = m_query.exec();
  77. #ifdef DB_EXCEPTIONS_ENABLED
  78. if (!res)
  79. throw DBException(m_query);
  80. #endif
  81. return NonQueryResult(m_query);
  82. }
  83. template <typename... Rest> NonQueryResult where(const QString &expr, const QVariant &first, const Rest&... rest)
  84. {
  85. m_params.append(first);
  86. return where(expr, rest...);
  87. }
  88. /*!
  89. \brief Executes unconditional <em>DELETE FROM table</em> query
  90. \code
  91. //DELETE FROM table
  92. t.deleteFrom("table").exec();
  93. \endcode
  94. */
  95. NonQueryResult exec()
  96. {
  97. const QString &sql = createSql(m_table);
  98. const bool res = m_query.exec(sql);
  99. #ifdef DB_EXCEPTIONS_ENABLED
  100. if (!res)
  101. throw DBException(m_query);
  102. #endif
  103. return NonQueryResult(m_query);
  104. }
  105. private:
  106. QSqlQuery m_query;
  107. QString m_table;
  108. QVariantList m_params;
  109. static QString createSql(const QString &table, const QString &expr = "1=1")
  110. {
  111. return QString("DELETE FROM %0 WHERE %1").arg(table).arg(expr);
  112. }
  113. };
  114. #endif // EASYQTSQL_DELETEQUERY_H