1 /*
2  *   SPDX-FileCopyrightText: 2013-2016 Ivan Cukic <ivan.cukic@kde.org>
3  *
4  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5  */
6 
7 #ifndef UTILS_QSQLQUERYITERATOR_H
8 #define UTILS_QSQLQUERYITERATOR_H
9 
10 #include <QSqlQuery>
11 #include <QVariant>
12 
13 template<typename ResultSet>
14 class NextValueIterator
15 {
16 public:
17     enum Type {
18         NormalIterator,
19         EndIterator,
20     };
21 
22     NextValueIterator(ResultSet &query, Type type = NormalIterator)
m_query(query)23         : m_query(query)
24         , m_type(type)
25     {
26         if (type != EndIterator) {
27             m_query.next();
28         }
29     }
30 
31     inline bool operator!=(const NextValueIterator<ResultSet> &other) const
32     {
33         Q_UNUSED(other);
34         return m_query.isValid();
35     }
36 
37     inline NextValueIterator<ResultSet> &operator*()
38     {
39         return *this;
40     }
41 
42     inline QVariant operator[](int index) const
43     {
44         return m_query.value(index);
45     }
46 
47     inline QVariant operator[](const QString &name) const
48     {
49         return m_query.value(name);
50     }
51 
52     inline NextValueIterator<ResultSet> &operator++()
53     {
54         m_query.next();
55         return *this;
56     }
57 
58 private:
59     ResultSet &m_query;
60     Type m_type;
61 };
62 
63 NextValueIterator<QSqlQuery> begin(QSqlQuery &query);
64 NextValueIterator<QSqlQuery> end(QSqlQuery &query);
65 
66 #endif /* UTILS_QSQLQUERYITERATOR_H */
67