Line data Source code
1 : /********************************************************************
2 : * gnc-dbisqlresult.hpp: Iterable wrapper for dbi_result. *
3 : * *
4 : * Copyright 2016 John Ralls <jralls@ceridwen.us *
5 : * *
6 : * This program is free software; you can redistribute it and/or *
7 : * modify it under the terms of the GNU General Public License as *
8 : * published by the Free Software Foundation; either version 2 of *
9 : * the License, or (at your option) any later version. *
10 : * *
11 : * This program is distributed in the hope that it will be useful, *
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : * GNU General Public License for more details. *
15 : * *
16 : * You should have received a copy of the GNU General Public License*
17 : * along with this program; if not, contact: *
18 : * *
19 : * Free Software Foundation Voice: +1-617-542-5942 *
20 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : \********************************************************************/
23 :
24 : /* Private structures and variables for gnc-backend-dbi.c and its unit tests */
25 : #ifndef __GNC_DBISQLBACKEND_HPP__
26 : #define __GNC_DBISQLBACKEND_HPP__
27 :
28 : #include <optional>
29 : #include <cstdint>
30 :
31 : #include "gnc-backend-dbi.h"
32 : #include <gnc-sql-result.hpp>
33 :
34 : class GncDbiSqlConnection;
35 :
36 : /**
37 : * An iterable wrapper for dbi_result; allows using C++11 range for.
38 : */
39 : class GncDbiSqlResult : public GncSqlResult
40 : {
41 : public:
42 0 : GncDbiSqlResult(const GncDbiSqlConnection* conn, dbi_result result) :
43 0 : m_conn{conn}, m_dbi_result{result}, m_iter{this}, m_row{&m_iter},
44 0 : m_sentinel{nullptr} {}
45 : ~GncDbiSqlResult();
46 : uint64_t size() const noexcept;
47 : int dberror() const noexcept;
48 : GncSqlRow& begin();
49 0 : GncSqlRow& end() { return m_sentinel; }
50 : protected:
51 : class IteratorImpl : public GncSqlResult::IteratorImpl
52 : {
53 : public:
54 0 : ~IteratorImpl() = default;
55 0 : IteratorImpl(GncDbiSqlResult* inst) : m_inst{inst} {}
56 : virtual GncSqlRow& operator++();
57 0 : virtual GncSqlRow& operator++(int) { return ++(*this); };
58 0 : virtual GncSqlResult* operator*() { return m_inst; }
59 : virtual std::optional<int64_t> get_int_at_col (const char* col) const;
60 : virtual std::optional<double> get_float_at_col (const char* col) const;
61 : virtual std::optional<double> get_double_at_col (const char* col) const;
62 : virtual std::optional<std::string> get_string_at_col (const char* col)const;
63 : virtual std::optional<time64> get_time64_at_col (const char* col) const;
64 0 : virtual bool is_col_null(const char* col) const noexcept
65 : {
66 0 : return dbi_result_field_is_null(m_inst->m_dbi_result, col);
67 : }
68 : private:
69 : GncDbiSqlResult* m_inst = nullptr;
70 : };
71 :
72 : private:
73 : const GncDbiSqlConnection* m_conn = nullptr;
74 : dbi_result m_dbi_result;
75 : IteratorImpl m_iter;
76 : GncSqlRow m_row;
77 : GncSqlRow m_sentinel;
78 :
79 : };
80 :
81 : #endif //__GNC_DBISQLRESULT_HPP__
|