EasyQtSql_UpdateQuery.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef EASYQTSQL_UPDATEQUERY_H
  2. #define EASYQTSQL_UPDATEQUERY_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>UPDATE ... SET ... WHERE ...</em> query execution.
  33. */
  34. class UpdateQuery
  35. {
  36. public:
  37. UpdateQuery(const QString &table, const QSqlDatabase &db)
  38. : q(db)
  39. , m_table(table)
  40. { }
  41. /*!
  42. \brief Sets table <em>field</em> to <em>value</em>
  43. \param field Table field name
  44. \param value Value to set
  45. \return UpdateQuery reference
  46. The method returns an UpdateQuery reference so its call can be chained:
  47. \code
  48. //UPDATE table SET a=111, b=222
  49. t.update("table")
  50. .set("a", 111) //method chaining
  51. .set("b", 222)
  52. .exec();
  53. \endcode
  54. */
  55. UpdateQuery &set(const QString &field, const QVariant &value)
  56. {
  57. m_updateMap[field] = value;
  58. return *this;
  59. }
  60. /*!
  61. \brief Sets table fields to values from <em>map</em>
  62. \param map Values map. Map key is field name, map value is field value.
  63. The method returns an UpdateQuery reference so its calls can be chained.
  64. \code
  65. //UPDATE table SET a=111, b=222
  66. t.update("table")
  67. .set(QVariantMap{ {"a", 111}, {"b", 222} })
  68. .exec();
  69. \endcode
  70. */
  71. UpdateQuery &set(const QVariantMap &map)
  72. {
  73. for (auto it = map.begin(); it != map.end(); ++it)
  74. {
  75. m_updateMap[it.key()] = it.value();
  76. }
  77. return *this;
  78. }
  79. /*!
  80. \brief Executes update query with <em>expr</em> condition.
  81. \param expr WHERE condition
  82. \code
  83. //UPDATE table SET a=111, b=222 WHERE a=1 AND b=2
  84. t.update("table")
  85. .set(QVariantMap{ {"a", 111}, {"b", 222} })
  86. .where("a=1 AND b=2");
  87. \endcode
  88. */
  89. NonQueryResult where(const QString &expr)
  90. {
  91. m_whereExpr = expr;
  92. return exec();
  93. }
  94. /*!
  95. \brief Executes update query with <em>expr</em> condition and <em>last</em> parameter binding.
  96. \param expr WHERE condition
  97. \param last Parameter to bind on WHERE expression
  98. The method supports variable count of QVariant parameters.
  99. Parameters are bound with <em>QSqlQuery::addBindValue</em>.
  100. \code
  101. //UPDATE table SET a=111, b=222 WHERE a=1 AND b=2
  102. t.update("table")
  103. .set(QVariantMap{ {"a", 111}, {"b", 222} })
  104. .where("a=? AND b=?", 1, 2);
  105. \endcode
  106. */
  107. NonQueryResult where(const QString &expr, const QVariant &last)
  108. {
  109. m_params.append(last);
  110. m_whereExpr = expr;
  111. return exec();
  112. }
  113. template <typename... Rest> NonQueryResult where(const QString &expr, const QVariant &first, const Rest&... rest)
  114. {
  115. m_params.append(first);
  116. return where(expr, rest...);
  117. }
  118. /*!
  119. \brief Executes UPDATE query without conditions
  120. */
  121. NonQueryResult exec()
  122. {
  123. QString sql = "UPDATE " + m_table + " SET ";
  124. QStringList fieldValuePairs;
  125. for (auto it = m_updateMap.begin(); it != m_updateMap.end(); ++it)
  126. {
  127. fieldValuePairs.append(it.key() + "=?");
  128. }
  129. sql += fieldValuePairs.join(",");
  130. if (!m_whereExpr.isEmpty())
  131. {
  132. sql += " WHERE " + m_whereExpr;
  133. }
  134. q.prepare(sql);
  135. for (auto it = m_updateMap.begin(); it != m_updateMap.end(); ++it)
  136. {
  137. q.addBindValue(it.value());
  138. }
  139. for (auto it = m_params.begin(); it != m_params.end(); ++it)
  140. {
  141. q.addBindValue(*it);
  142. }
  143. bool res = q.exec();
  144. #ifdef DB_EXCEPTIONS_ENABLED
  145. if (!res)
  146. throw DBException(q);
  147. #endif
  148. return NonQueryResult(q);
  149. }
  150. private:
  151. QSqlQuery q;
  152. QString m_table;
  153. QVariantMap m_updateMap;
  154. QVariantList m_params;
  155. QString m_whereExpr;
  156. };
  157. #endif // EASYQTSQL_UPDATEQUERY_H