EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql_Util.h
Go to the documentation of this file.
1 #ifndef EASYQTSQL_UTIL_H
2 #define EASYQTSQL_UTIL_H
3 
4 /*
5  * The MIT License (MIT)
6  * Copyright 2018 Alexey Kramin
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27 */
28 
29 #ifndef EASY_QT_SQL_MAIN
30 
31 #include "EasyQtSql_QueryResult.h"
32 
33 #endif
34 
54 class Util
55 {
56 public:
57 
63  template<typename Func>
64  static int each(QueryResult &res, Func&& f)
65  {
66  int rowCount = 0;
67 
68  if (res.isActive())
69  {
70  while (res.next())
71  {
72  f(res);
73  ++rowCount;
74  }
75  }
76 
77  return rowCount;
78  }
79 
87  template<typename Func>
88  static int range(QueryResult &res, int start, int count, Func&& f)
89  {
90  Q_ASSERT(start >= 0);
91  Q_ASSERT(count >= 0);
92 
93  int rowCount = 0;
94 
95  if (res.isActive())
96  {
97  //skip items
98  for (int i = 0; i < start; ++i)
99  {
100  if (!res.next())
101  return rowCount;
102  }
103 
104  for (int i = 0; i < count && res.next(); ++i)
105  {
106  f(res);
107  rowCount++;
108  }
109  }
110 
111  return rowCount;
112  }
113 
121  template<typename Func>
122  static int top(QueryResult &res, int topCount, Func&& f)
123  {
124  return range(res, 0, topCount, f);
125  }
126 
132  template<typename Func>
133  static int first(QueryResult &res, Func&& f)
134  {
135  return top(res, 1, f);
136  }
137 
138 private:
139  Util(){}
140 };
141 
142 #endif // EASYQTSQL_UTIL_H
static int range(QueryResult &res, int start, int count, Func &&f)
Applies function (lambda) f to count rows starting from start index.
Definition: EasyQtSql_Util.h:88
static int top(QueryResult &res, int topCount, Func &&f)
Applies function (lambda) f to topCount rows from res.
Definition: EasyQtSql_Util.h:122
Utility functions.
Definition: EasyQtSql_Util.h:54
bool isActive() const
Returns true if the query is active. An active QSqlQuery is one that has been exec()&#39;d successfully b...
Definition: EasyQtSql_QueryResult.h:151
QSqlQuery wrapper with handy data fetch methods.
Definition: EasyQtSql_QueryResult.h:39
static int first(QueryResult &res, Func &&f)
Applies function (lambda) f to first result row.
Definition: EasyQtSql_Util.h:133
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: EasyQtSql_QueryResult.h:60
static int each(QueryResult &res, Func &&f)
Applies function (lambda) f to each row in res.
Definition: EasyQtSql_Util.h:64