EasyQtSql_PreparedQuery.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef EASYQTSQL_PREPAREDQUERY_H
  2. #define EASYQTSQL_PREPAREDQUERY_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_QueryResult.h"
  30. #include "EasyQtSql_ParamDirectionWrapper.h"
  31. #endif
  32. /*!
  33. \brief Class for query preparation and execution
  34. \code
  35. //SELECT a, b, c, d FROM table WHERE a=? AND b=?
  36. PreparedQuery query = t.prepare("SELECT a, b, c, d FROM table WHERE a=? AND b=?");
  37. //parameters a and b are bound with values 1 and 2, query executed
  38. QueryResult res = query.exec(1, 2);
  39. while(res.next())
  40. {
  41. //fetch data here
  42. }
  43. //parameters a and b are bound with values 3 and 4, query executed
  44. res = query.exec(3, 4);
  45. while(res.next())
  46. {
  47. //fetch data here
  48. }
  49. \endcode
  50. */
  51. class PreparedQuery
  52. {
  53. public:
  54. PreparedQuery(const QString &stmt, const QSqlDatabase &db, bool forwardOnly = true)
  55. :m_query(db)
  56. {
  57. m_query.setForwardOnly(forwardOnly);
  58. m_query.prepare(stmt);
  59. }
  60. QueryResult &exec()
  61. {
  62. m_index = 0;
  63. const bool res = m_query.exec();
  64. #ifdef DB_EXCEPTIONS_ENABLED
  65. if (!res)
  66. throw DBException(m_query);
  67. #endif
  68. m_result = QueryResult(m_query, m_aliases);
  69. m_aliases.clear();
  70. return m_result;
  71. }
  72. QueryResult &exec(const QVariant &bindValue)
  73. {
  74. m_query.bindValue(m_index, bindValue);
  75. return exec();
  76. }
  77. QueryResult &exec(const In &paramWrapper)
  78. {
  79. m_query.bindValue(m_index, paramWrapper.value, QSql::In);
  80. addAliasIfSet(paramWrapper, m_index);
  81. return exec();
  82. }
  83. QueryResult &exec(const Out &paramWrapper)
  84. {
  85. m_query.bindValue(m_index, paramWrapper.value, QSql::Out);
  86. addAliasIfSet(paramWrapper, m_index);
  87. return exec();
  88. }
  89. QueryResult &exec(const InOut &paramWrapper)
  90. {
  91. m_query.bindValue(m_index, paramWrapper.value, QSql::InOut);
  92. addAliasIfSet(paramWrapper, m_index);
  93. return exec();
  94. }
  95. template <typename... Rest> QueryResult &exec(const QVariant &bindValue, const Rest&... rest)
  96. {
  97. m_query.bindValue(m_index, bindValue);
  98. m_index++;
  99. return exec(rest...);
  100. }
  101. template <typename... Rest> QueryResult &exec(const In &paramWrapper, const Rest&... rest)
  102. {
  103. m_query.bindValue(m_index, paramWrapper.value, QSql::In);
  104. addAliasIfSet(paramWrapper, m_index);
  105. m_index++;
  106. return exec(rest...);
  107. }
  108. template <typename... Rest> QueryResult &exec(const Out &paramWrapper, const Rest&... rest)
  109. {
  110. m_query.bindValue(m_index, paramWrapper.value, QSql::Out);
  111. addAliasIfSet(paramWrapper, m_index);
  112. m_index++;
  113. return exec(rest...);
  114. }
  115. template <typename... Rest> QueryResult &exec(const InOut &paramWrapper, const Rest&... rest)
  116. {
  117. m_query.bindValue(m_index, paramWrapper.value, QSql::InOut);
  118. addAliasIfSet(paramWrapper, m_index);
  119. m_index++;
  120. return exec(rest...);
  121. }
  122. private:
  123. QSqlQuery m_query;
  124. int m_index = 0;
  125. QueryResult m_result;
  126. QMap<QString, int> m_aliases;
  127. void addAliasIfSet(const ParamDirectionWrapper &paramWrapper, int index)
  128. {
  129. if (!paramWrapper.alias.isEmpty())
  130. {
  131. m_aliases[paramWrapper.alias] = index;
  132. }
  133. }
  134. };
  135. #endif // EASYQTSQL_PREPAREDQUERY_H