GnuCash  5.6-150-g038405b370+
gnc-dbisqlresult.hpp
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 
30 #include "gnc-backend-dbi.h"
31 #include <gnc-sql-result.hpp>
32 
34 
39 {
40 public:
41  GncDbiSqlResult(const GncDbiSqlConnection* conn, dbi_result result) :
42  m_conn{conn}, m_dbi_result{result}, m_iter{this}, m_row{&m_iter},
43  m_sentinel{nullptr} {}
44  ~GncDbiSqlResult();
45  uint64_t size() const noexcept;
46  int dberror() const noexcept;
47  GncSqlRow& begin();
48  GncSqlRow& end() { return m_sentinel; }
49 protected:
51  {
52  public:
53  ~IteratorImpl() = default;
54  IteratorImpl(GncDbiSqlResult* inst) : m_inst{inst} {}
55  virtual GncSqlRow& operator++();
56  virtual GncSqlRow& operator++(int) { return ++(*this); };
57  virtual GncSqlResult* operator*() { return m_inst; }
58  virtual std::optional<int64_t> get_int_at_col (const char* col) const;
59  virtual std::optional<double> get_float_at_col (const char* col) const;
60  virtual std::optional<double> get_double_at_col (const char* col) const;
61  virtual std::optional<std::string> get_string_at_col (const char* col)const;
62  virtual std::optional<time64> get_time64_at_col (const char* col) const;
63  virtual bool is_col_null(const char* col) const noexcept
64  {
65  return dbi_result_field_is_null(m_inst->m_dbi_result, col);
66  }
67  private:
68  GncDbiSqlResult* m_inst = nullptr;
69  };
70 
71 private:
72  const GncDbiSqlConnection* m_conn = nullptr;
73  dbi_result m_dbi_result;
74  IteratorImpl m_iter;
75  GncSqlRow m_row;
76  GncSqlRow m_sentinel;
77 
78 };
79 
80 #endif //__GNC_DBISQLRESULT_HPP__
load and save data to SQL via libdbi
Row of SQL Query results.
Encapsulate a libdbi dbi_conn connection.
An iterable wrapper for dbi_result; allows using C++11 range for.
Pure virtual class to iterate over a query result set.