GnuCash  5.6-150-g038405b370+
gnc-sql-result.hpp
1 /***********************************************************************\
2  * gnc-sql-result.hpp: Encapsulate the results of a SQL query. *
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 #ifndef __GNC_SQL_RESULT_HPP__
25 #define __GNC_SQL_RESULT_HPP__
26 
27 #include <qof.h>
28 
29 #include <cstdint>
30 #include <optional>
31 #include <string>
32 #include <vector>
33 
34 class GncSqlRow;
40 {
41 public:
42  virtual ~GncSqlResult() = default;
43  virtual uint64_t size() const noexcept = 0;
44  virtual GncSqlRow& begin() = 0;
45  virtual GncSqlRow& end() = 0;
46  friend GncSqlRow;
47 protected:
48  class IteratorImpl {
49  public:
50  virtual ~IteratorImpl() = default;
51  virtual GncSqlRow& operator++() = 0;
52  virtual GncSqlResult* operator*() = 0;
53  virtual std::optional<int64_t> get_int_at_col (const char* col) const = 0;
54  virtual std::optional<double> get_float_at_col (const char* col) const = 0;
55  virtual std::optional<double> get_double_at_col (const char* col) const = 0;
56  virtual std::optional<std::string> get_string_at_col (const char* col) const = 0;
57  virtual std::optional<time64> get_time64_at_col (const char* col) const = 0;
58  virtual bool is_col_null (const char* col) const noexcept = 0;
59  };
60 };
61 
80 class GncSqlRow
81 {
82 public:
83  GncSqlRow (GncSqlResult::IteratorImpl* iter) : m_iter{iter} {}
84  ~GncSqlRow() { }
85  GncSqlRow& operator++();
86  GncSqlRow& operator*() { return *this; }
87  friend bool operator!=(const GncSqlRow&, const GncSqlRow&);
88  std::optional<int64_t> get_int_at_col (const char* col) const {
89  return m_iter->get_int_at_col (col); }
90  std::optional<double> get_float_at_col (const char* col) const {
91  return m_iter->get_float_at_col (col); }
92  std::optional<double> get_double_at_col (const char* col) const {
93  return m_iter->get_double_at_col (col); }
94  std::optional<std::string> get_string_at_col (const char* col) const {
95  return m_iter->get_string_at_col (col); }
96  std::optional<time64> get_time64_at_col (const char* col) const {
97  return m_iter->get_time64_at_col (col); }
98  bool is_col_null (const char* col) const noexcept {
99  return m_iter->is_col_null (col); }
100 private:
102 };
103 
104 inline bool operator!=(const GncSqlRow& lr, const GncSqlRow& rr) {
105  return lr.m_iter != rr.m_iter;
106 }
107 
108 inline bool operator==(const GncSqlRow& lr, const GncSqlRow& rr) {
109  return !(lr != rr);
110 }
111 
112 
113 #endif //__GNC_SQL_RESULT_HPP__
Row of SQL Query results.
Pure virtual class to iterate over a query result set.