libpqxx
The C++ client library for PostgreSQL
result_iter.hxx
1 
9 #ifndef PQXX_H_RESULT_ITER
10 #define PQXX_H_RESULT_ITER
11 
12 #include <memory>
13 
14 #include "pqxx/strconv.hxx"
15 
16 namespace pqxx
17 {
18 class result;
19 } // namespace pqxx
20 
21 
22 namespace pqxx::internal
23 {
24 // C++20: Replace with generator?
26 template<typename... TYPE> class result_iter
27 {
28 public:
29  using value_type = std::tuple<TYPE...>;
30 
32  result_iter() = default;
33 
34  explicit result_iter(result const &home) :
35  m_home{&home}, m_size{std::size(home)}
36  {
37  if (not std::empty(home))
38  read();
39  }
40  result_iter(result_iter const &) = default;
41 
42  result_iter &operator++()
43  {
44  m_index++;
45  if (m_index >= m_size)
46  m_home = nullptr;
47  else
48  read();
49  return *this;
50  }
51 
53  bool operator==(result_iter const &rhs) const
54  {
55  return m_home == rhs.m_home;
56  }
57  bool operator!=(result_iter const &rhs) const { return not(*this == rhs); }
58 
59  value_type const &operator*() const { return m_value; }
60 
61 private:
62  void read() { (*m_home)[m_index].convert(m_value); }
63 
64  result const *m_home{nullptr};
65  result::size_type m_index{0};
66  result::size_type m_size;
67  value_type m_value;
68 };
69 
70 
71 template<typename... TYPE> class result_iteration
72 {
73 public:
74  using iterator = result_iter<TYPE...>;
75  explicit result_iteration(result const &home) : m_home{home}
76  {
77  constexpr auto tup_size{sizeof...(TYPE)};
78  if (home.columns() != tup_size)
80  "Tried to extract ", to_string(tup_size),
81  " field(s) from a result with ", to_string(home.columns()),
82  " column(s).")};
83  }
84  iterator begin() const
85  {
86  if (std::size(m_home) == 0)
87  return end();
88  else
89  return iterator{m_home};
90  }
91  iterator end() const { return {}; }
92 
93 private:
94  pqxx::result const m_home;
95 };
96 } // namespace pqxx::internal
97 
98 
99 template<typename... TYPE> inline auto pqxx::result::iter() const
100 {
101  return pqxx::internal::result_iteration<TYPE...>{*this};
102 }
103 
104 
105 template<typename CALLABLE>
106 inline void pqxx::result::for_each(CALLABLE &&func) const
107 {
108  using args_tuple = internal::args_t<decltype(func)>;
109  constexpr auto sz{std::tuple_size_v<args_tuple>};
110  static_assert(
111  sz > 0,
112  "Callback for for_each must take parameters, one for each column in the "
113  "result.");
114 
115  auto const cols{this->columns()};
116  if (sz != cols)
118  "Callback to for_each takes ", sz, "parameter", (sz == 1) ? "" : "s",
119  ", but result set has ", cols, "field", (cols == 1) ? "" : "s", ".")};
120 
122  for (auto const r : *this) std::apply(func, r.as_tuple<pass_tuple>());
123 }
124 #endif
Iterator for looped unpacking of a result.
Definition: result_iter.hxx:27
result_iter()=default
Construct an "end" iterator.
bool operator==(result_iter const &rhs) const
Comparison only works for comparing to end().
Definition: result_iter.hxx:53
Definition: result_iter.hxx:72
Result set containing data returned by a query or command.
Definition: result.hxx:73
PQXX_PURE row_size_type columns() const noexcept
Number of columns in result.
Definition: result.cxx:493
void for_each(CALLABLE &&func) const
Run func on each row, passing the row's fields as parameters.
Definition: result_iter.hxx:106
auto iter() const
Iterate rows, reading them directly into a tuple of "TYPE...".
Definition: result_iter.hxx:99
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:249
Internal items for libpqxx' own use. Do not use these yourself.
Definition: encodings.cxx:33
std::string concat(TYPE... item)
Efficiently combine a bunch of items into one big string.
Definition: concat.hxx:31
decltype(strip_types(std::declval< TYPES... >())) strip_types_t
Take a tuple type and apply strip_t to its component types.
Definition: util.hxx:629
decltype(args_f(std::declval< CALLABLE >())) args_t
A callable's parameter types, as a tuple.
Definition: util.hxx:616
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:27
strip_t< decltype(*std::begin(std::declval< CONTAINER >()))> value_type
The type of a container's elements.
Definition: types.hxx:94
PQXX_LIBEXPORT std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:566