EasyQtSql_Util.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef EASYQTSQL_UTIL_H
  2. #define EASYQTSQL_UTIL_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 "EasyQtSql_QueryResult.h"
  29. #endif
  30. /*!
  31. \brief Utility functions.
  32. \code
  33. t.first("SELECT 1", [](const QueryResult &row){
  34. qDebug() << row.scalar<bool>();
  35. });
  36. t.each("SELECT * FROM table1", [](const QueryResult &row){
  37. qDebug() << row.toMap();
  38. });
  39. PreparedQuery query = t.prepare("SELECT a, b, c, d FROM table1 WHERE a = ? AND b = ?");
  40. Util::each(query.exec(1, 2), [](const QueryResult &row){
  41. qDebug() << row.toMap();
  42. });
  43. \endcode
  44. */
  45. class Util
  46. {
  47. public:
  48. /*!
  49. \brief Applies function (lambda) f to each row in res.
  50. \param res QueryResult
  51. \param f Function
  52. */
  53. template<typename Func>
  54. static int each(QueryResult &res, Func&& f)
  55. {
  56. int rowCount = 0;
  57. if (res.isActive())
  58. {
  59. while (res.next())
  60. {
  61. f(res);
  62. ++rowCount;
  63. }
  64. }
  65. return rowCount;
  66. }
  67. /*!
  68. \brief Applies function (lambda) f to count rows starting from start index
  69. \param res QueryResult
  70. \param start Index of start row
  71. \param count Rows count
  72. \param f Function
  73. */
  74. template<typename Func>
  75. static int range(QueryResult &res, int start, int count, Func&& f)
  76. {
  77. Q_ASSERT(start >= 0);
  78. Q_ASSERT(count >= 0);
  79. int rowCount = 0;
  80. if (res.isActive())
  81. {
  82. //skip items
  83. for (int i = 0; i < start; ++i)
  84. {
  85. if (!res.next())
  86. return rowCount;
  87. }
  88. for (int i = 0; i < count && res.next(); ++i)
  89. {
  90. f(res);
  91. rowCount++;
  92. }
  93. }
  94. return rowCount;
  95. }
  96. /*!
  97. \brief Applies function (lambda) f to topCount rows from res
  98. \param res QueryResult
  99. \param topCount Count of rows
  100. \param count Rows count
  101. \param f Function
  102. */
  103. template<typename Func>
  104. static int top(QueryResult &res, int topCount, Func&& f)
  105. {
  106. return range(res, 0, topCount, f);
  107. }
  108. /*!
  109. \brief Applies function (lambda) f to first result row
  110. \param res QueryResult
  111. \param f Function
  112. */
  113. template<typename Func>
  114. static int first(QueryResult &res, Func&& f)
  115. {
  116. return top(res, 1, f);
  117. }
  118. private:
  119. Util(){}
  120. };
  121. #endif // EASYQTSQL_UTIL_H