File indexing completed on 2024-05-12 05:29:23

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