libpqxx
The C++ client library for PostgreSQL
result_iterator.hxx
Go to the documentation of this file.
1 /* Definitions for the pqxx::result class and support classes.
2  *
3  * pqxx::result represents the set of result rows from a database query.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result instead.
6  *
7  * Copyright (c) 2000-2026, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_INTERNAL_RESULT_ITERATOR_HXX
14 #define PQXX_INTERNAL_RESULT_ITERATOR_HXX
15 
16 #include "pqxx/row.hxx"
17 
18 
19 /* Result iterator.
20  *
21  * Don't include this header from your own application; it is included for you
22  * by other libpqxx headers.
23  */
24 
25 namespace pqxx
26 {
28 
35 {
36 public:
37  using iterator_category = std::random_access_iterator_tag;
38  using value_type = row_ref const;
39  using pointer = row_ref const *;
40  using reference = row_ref;
43 
45  const_result_iterator() noexcept = default;
47  const_result_iterator(const_result_iterator const &) noexcept = default;
50 
53  m_row{r, i}
54  {}
55 
57  explicit const_result_iterator(row_ref const &r) noexcept :
58  m_row{r.home(), r.row_number()}
59  {}
60 
62 
63  reference operator[](difference_type d) const { return *(*this + d); }
64 
72  [[nodiscard]] PQXX_RETURNS_NONNULL pointer operator->() const noexcept
73  {
74  return &m_row;
75  }
76 
78  [[nodiscard]] reference operator*() const noexcept { return m_row; }
80 
81 
88 
89  const_result_iterator operator++(int) &;
91  {
93  return *this;
94  }
95  const_result_iterator operator--(int) &;
97  {
99  return *this;
100  }
101 
103  {
105  return *this;
106  }
108  {
110  return *this;
111  }
112 
114  void swap(const_result_iterator &other) noexcept
115  {
116  std::swap(m_row, other.m_row);
117  }
119 
124  [[nodiscard]] bool operator==(const_result_iterator const &i) const
125  {
126  return (&m_row.home() == &i.m_row.home()) and
127  (m_row.row_number() == i.m_row.row_number());
128  }
129  [[nodiscard]] bool operator!=(const_result_iterator const &i) const
130  {
131  return not(*this == i);
132  }
133  [[nodiscard]] bool operator<(const_result_iterator const &i) const
134  {
135  // Only needs to work when iterating the same result.
136  return m_row.row_number() < i.m_row.row_number();
137  }
138  [[nodiscard]] bool operator<=(const_result_iterator const &i) const
139  {
140  // Only needs to work when iterating the same result.
141  return m_row.row_number() <= i.m_row.row_number();
142  }
143  [[nodiscard]] bool operator>(const_result_iterator const &i) const
144  {
145  return m_row.row_number() > i.m_row.row_number();
146  }
147  [[nodiscard]] bool operator>=(const_result_iterator const &i) const
148  {
149  return m_row.row_number() >= i.m_row.row_number();
150  }
152 
157  [[nodiscard]] inline const_result_iterator operator+(difference_type) const;
158  friend const_result_iterator
159  operator+(difference_type, const_result_iterator const &);
160  [[nodiscard]] inline const_result_iterator operator-(difference_type) const;
161  [[nodiscard]] inline difference_type
162  operator-(const_result_iterator const &) const;
164 
165 private:
166  row_ref m_row;
167 };
168 
169 
172  : private const_result_iterator
173 {
174 public:
182 
184  const_reverse_result_iterator() noexcept = default;
186  const_reverse_result_iterator const &) noexcept = default;
188  default;
189 
192  const_result_iterator const &rhs) :
194  {
195  super::operator--();
196  }
197 
199 
201  [[nodiscard]] const_result_iterator base() const noexcept;
202 
208  using const_result_iterator::operator->;
210  using const_result_iterator::operator*;
212 
217  using const_result_iterator::operator[];
219 
225  operator=(const_reverse_result_iterator const &) noexcept = default;
227  operator=(const_reverse_result_iterator &&) noexcept = default;
228 
230  {
231  iterator_type::operator--();
232  return *this;
233  }
234  const_reverse_result_iterator operator++(int) &;
236  {
237  iterator_type::operator++();
238  return *this;
239  }
240  const_reverse_result_iterator operator--(int) &;
242  {
243  iterator_type::operator-=(i);
244  return *this;
245  }
247  {
248  iterator_type::operator+=(i);
249  return *this;
250  }
251 
252  void swap(const_reverse_result_iterator &other) noexcept
253  {
255  }
257 
262  [[nodiscard]] const_reverse_result_iterator
264  {
265  return const_reverse_result_iterator(base() - i);
266  }
268  {
269  return const_reverse_result_iterator(base() + i);
270  }
271  [[nodiscard]] difference_type
273  {
274  return rhs.const_result_iterator::operator-(*this);
275  }
277 
282  [[nodiscard]] bool
283  operator==(const_reverse_result_iterator const &rhs) const noexcept
284  {
285  return iterator_type::operator==(rhs);
286  }
287  [[nodiscard]] bool
288  operator!=(const_reverse_result_iterator const &rhs) const noexcept
289  {
290  return not operator==(rhs);
291  }
292 
293  [[nodiscard]] bool operator<(const_reverse_result_iterator const &rhs) const
294  {
295  return iterator_type::operator>(rhs);
296  }
297  [[nodiscard]] bool operator<=(const_reverse_result_iterator const &rhs) const
298  {
299  return iterator_type::operator>=(rhs);
300  }
301  [[nodiscard]] bool operator>(const_reverse_result_iterator const &rhs) const
302  {
303  return iterator_type::operator<(rhs);
304  }
305  [[nodiscard]] bool operator>=(const_reverse_result_iterator const &rhs) const
306  {
307  return iterator_type::operator<=(rhs);
308  }
310 };
311 
312 
313 inline const_result_iterator
315 {
316  return {
317  m_row.home(), size_type(result::difference_type(m_row.row_number()) + o)};
318 }
319 
322 {
323  return i + o;
324 }
325 
328 {
329  return {
330  m_row.home(),
332 }
333 
336 {
337  return result::difference_type(m_row.row_number() - i->row_number());
338 }
339 
341 {
342  return {*this, size()};
343 }
344 
345 
346 inline const_result_iterator result::cend() const noexcept
347 {
348  return end();
349 }
350 
351 
354 {
355  return const_reverse_result_iterator{i.base() - n};
356 }
357 
358 } // namespace pqxx
359 #endif
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
const_result_iterator(row_ref const &r) noexcept
Create an iterator pointing at a row.
Definition: result_iterator.hxx:57
void swap(const_result_iterator &other) noexcept
Interchange two iterators in an exception-safe manner.
Definition: result_iterator.hxx:114
PQXX_INLINE_ONLY const_result_iterator & operator--()
Definition: result_iterator.hxx:96
const_result_iterator & operator=(const_result_iterator const &)=default
const_result_iterator operator+(difference_type) const
Definition: result_iterator.hxx:314
std::random_access_iterator_tag iterator_category
Definition: result_iterator.hxx:37
const_result_iterator operator-(difference_type) const
Definition: result_iterator.hxx:327
bool operator<(const_result_iterator const &i) const
Definition: result_iterator.hxx:133
bool operator==(const_result_iterator const &i) const
Definition: result_iterator.hxx:124
bool operator<=(const_result_iterator const &i) const
Definition: result_iterator.hxx:138
reference operator*() const noexcept
Dereference the iterator.
Definition: result_iterator.hxx:78
bool operator>(const_result_iterator const &i) const
Definition: result_iterator.hxx:143
bool operator!=(const_result_iterator const &i) const
Definition: result_iterator.hxx:129
reference operator[](difference_type d) const
Definition: result_iterator.hxx:63
bool operator>=(const_result_iterator const &i) const
Definition: result_iterator.hxx:147
const_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:102
const_result_iterator() noexcept=default
Create an iterator, but in an unusable state.
const_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:107
row_ref const value_type
Definition: result_iterator.hxx:38
result_difference_type difference_type
Definition: result_iterator.hxx:42
result_size_type size_type
Definition: result_iterator.hxx:41
row_ref const * pointer
Definition: result_iterator.hxx:39
PQXX_INLINE_ONLY const_result_iterator & operator++()
Definition: result_iterator.hxx:90
const_result_iterator & operator=(const_result_iterator &&)=default
PQXX_RETURNS_NONNULL pointer operator->() const noexcept
Dereference the iterator.
Definition: result_iterator.hxx:72
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:173
bool operator<(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:293
bool operator>=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:305
const_result_iterator base() const noexcept
Return the underlying "regular" iterator (as per standard library).
Definition: result.cxx:682
const_reverse_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:246
const_reverse_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:241
const_reverse_result_iterator operator+(difference_type i) const
Definition: result_iterator.hxx:263
bool operator==(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:283
bool operator>(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:301
const_reverse_result_iterator() noexcept=default
Create an iterator, but in an unusable state.
void swap(const_reverse_result_iterator &other) noexcept
Definition: result_iterator.hxx:252
difference_type operator-(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:272
const_reverse_result_iterator operator-(difference_type i)
Definition: result_iterator.hxx:267
bool operator<=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:297
bool operator!=(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:288
const_reverse_result_iterator & operator--()
Definition: result_iterator.hxx:235
Definition: row_ref-const_result_iterator.hxx:15
Result set containing data returned by a query or command.
Definition: result.hxx:101
PQXX_PURE const_iterator end() const noexcept
Definition: result_iterator.hxx:340
result_difference_type difference_type
Definition: result.hxx:104
PQXX_PURE size_type size() const noexcept
Definition: result.cxx:137
PQXX_PURE const_iterator cend() const noexcept
Definition: result_iterator.hxx:346
Lightweight reference to one row in a result.
Definition: row.hxx:57
constexpr PQXX_PURE result::size_type row_number() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:197
PQXX_PURE result const & home() const noexcept
The result object to which this row_ref refers.
Definition: row.hxx:324
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
#define PQXX_RETURNS_NONNULL
Definition: header-pre.hxx:119
#define PQXX_INLINE_ONLY
Definition: header-pre.hxx:83
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
int result_difference_type
Difference between result sizes.
Definition: types.hxx:80
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:321
int result_size_type
Number of rows in a result set.
Definition: types.hxx:77
std::remove_cvref_t< std::ranges::range_value_t< CONTAINER > > value_type
The type of a container's elements.
Definition: types.hxx:138
constexpr bool operator==(char const lhs[], pqxx::zview rhs) noexcept
Disambiguating comparison operator: leave it to std::string_view.
Definition: zview.hxx:203