File indexing completed on 2024-12-22 05:13:36

0001 /*
0002     SPDX-FileCopyrightText: 2013 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef UTILS_QSQLQUERYITERATOR_H
0008 #define UTILS_QSQLQUERYITERATOR_H
0009 
0010 #include <QSqlQuery>
0011 #include <QVariant>
0012 
0013 template<typename ResultSet>
0014 class NextValueIterator
0015 {
0016 public:
0017     enum Type {
0018         NormalIterator,
0019         EndIterator,
0020     };
0021 
0022     NextValueIterator(ResultSet &query, Type type = NormalIterator)
0023         : m_query(query)
0024         , m_type(type)
0025     {
0026         if (type != EndIterator) {
0027             m_query.next();
0028         }
0029     }
0030 
0031     inline bool operator!=(const NextValueIterator<ResultSet> &other) const
0032     {
0033         Q_UNUSED(other);
0034         return m_query.isValid();
0035     }
0036 
0037     inline NextValueIterator<ResultSet> &operator*()
0038     {
0039         return *this;
0040     }
0041 
0042     inline QVariant operator[](int index) const
0043     {
0044         return m_query.value(index);
0045     }
0046 
0047     inline QVariant operator[](const QString &name) const
0048     {
0049         return m_query.value(name);
0050     }
0051 
0052     inline NextValueIterator<ResultSet> &operator++()
0053     {
0054         m_query.next();
0055         return *this;
0056     }
0057 
0058 private:
0059     ResultSet &m_query;
0060     Type m_type;
0061 };
0062 
0063 NextValueIterator<QSqlQuery> begin(QSqlQuery &query);
0064 NextValueIterator<QSqlQuery> end(QSqlQuery &query);
0065 
0066 #endif /* UTILS_QSQLQUERYITERATOR_H */