libpqxx
The C++ client library for PostgreSQL
Loading...
Searching...
No Matches
row.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-2025, 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_H_ROW
14#define PQXX_H_ROW
15
16#if !defined(PQXX_HEADER_PRE)
17# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
18#endif
19
20#include "pqxx/except.hxx"
21#include "pqxx/field.hxx"
22#include "pqxx/result.hxx"
23
25
26namespace pqxx::internal
27{
28template<typename... T> class result_iter;
29} // namespace pqxx::internal
30
31
32namespace pqxx
33{
35
46class PQXX_LIBEXPORT row
47{
48public:
49 // TODO: Some of these types conflict: class is both iterator and container.
50 // TODO: Set iterator nested types using std::iterator_traits.
51 using size_type = row_size_type;
52 using difference_type = row_difference_type;
53 using const_iterator = const_row_iterator;
54 using iterator = const_iterator;
55 using reference = field;
56 using pointer = const_row_iterator;
57 using const_reverse_iterator = const_reverse_row_iterator;
58 using reverse_iterator = const_reverse_iterator;
59
60 row() noexcept = default;
61 row(row &&) noexcept = default;
62 row(row const &) noexcept = default;
63 row &operator=(row const &) noexcept = default;
64 row &operator=(row &&) noexcept = default;
65
70 [[nodiscard]] PQXX_PURE bool operator==(row const &) const noexcept;
71 [[nodiscard]] bool operator!=(row const &rhs) const noexcept
72 {
73 return not operator==(rhs);
74 }
76
77 [[nodiscard]] const_iterator begin() const noexcept;
78 [[nodiscard]] const_iterator cbegin() const noexcept;
79 [[nodiscard]] const_iterator end() const noexcept;
80 [[nodiscard]] const_iterator cend() const noexcept;
81
86 [[nodiscard]] reference front() const noexcept;
87 [[nodiscard]] reference back() const noexcept;
88
89 [[nodiscard]] const_reverse_row_iterator rbegin() const noexcept;
90 [[nodiscard]] const_reverse_row_iterator crbegin() const noexcept;
91 [[nodiscard]] const_reverse_row_iterator rend() const noexcept;
92 [[nodiscard]] const_reverse_row_iterator crend() const noexcept;
93
94 [[nodiscard]] reference operator[](size_type) const noexcept;
98 [[nodiscard]] reference operator[](zview col_name) const;
99
100 reference at(size_type) const;
104 reference at(zview col_name) const;
105
106 [[nodiscard]] constexpr size_type size() const noexcept
107 {
108 return m_end - m_begin;
109 }
110
112 [[nodiscard]] constexpr result::size_type rownumber() const noexcept
113 {
114 return m_index;
115 }
116
121
122 [[nodiscard]] size_type column_number(zview col_name) const;
123
125 [[nodiscard]] oid column_type(size_type) const;
126
128 [[nodiscard]] oid column_type(zview col_name) const
129 {
130 return column_type(column_number(col_name));
131 }
132
134 [[nodiscard]] oid column_table(size_type col_num) const;
135
137 [[nodiscard]] oid column_table(zview col_name) const
138 {
139 return column_table(column_number(col_name));
140 }
141
143
150 [[nodiscard]] size_type table_column(size_type) const;
151
153 [[nodiscard]] size_type table_column(zview col_name) const
154 {
155 return table_column(column_number(col_name));
156 }
158
159 [[nodiscard]] constexpr result::size_type num() const noexcept
160 {
161 return rownumber();
162 }
163
165
173 template<typename Tuple> void to(Tuple &t) const
174 {
175 check_size(std::tuple_size_v<Tuple>);
176 convert(t);
177 }
178
180
188 template<typename... TYPE> std::tuple<TYPE...> as() const
189 {
190 check_size(sizeof...(TYPE));
191 using seq = std::make_index_sequence<sizeof...(TYPE)>;
192 return get_tuple<std::tuple<TYPE...>>(seq{});
193 }
194
196
202 template<typename TUPLE> TUPLE as_tuple() const
203 {
204 check_size(std::tuple_size_v<TUPLE>);
205 using seq = std::make_index_sequence<std::tuple_size_v<TUPLE>>;
206 return get_tuple<TUPLE>(seq{});
207 }
208
209 [[deprecated("Swap iterators, not rows.")]] void swap(row &) noexcept;
210
222 [[deprecated("Row slicing is going away. File a bug if you need it.")]] row
223 slice(size_type sbegin, size_type send) const;
224
226 [[nodiscard, deprecated("Row slicing is going away.")]] PQXX_PURE bool
227 empty() const noexcept;
228
229protected:
230 friend class const_row_iterator;
231 friend class result;
232 row(result r, result_size_type index, size_type cols) noexcept;
233
235 void check_size(size_type expected) const
236 {
237 if (size() != expected)
238 throw usage_error{internal::concat(
239 "Tried to extract ", expected, " field(s) from a row of ", size(),
240 ".")};
241 }
242
243 template<typename... T> friend class pqxx::internal::result_iter;
245 template<typename Tuple> void convert(Tuple &t) const
246 {
247 extract_fields(t, std::make_index_sequence<std::tuple_size_v<Tuple>>{});
248 }
249
250 friend class field;
251
253 result m_result;
254
256
260 result::size_type m_index = 0;
261
262 // TODO: Remove m_begin and (if possible) m_end when we remove slice().
264 size_type m_begin = 0;
266 size_type m_end = 0;
267
268private:
269 template<typename Tuple, std::size_t... indexes>
270 void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
271 {
272 (extract_value<Tuple, indexes>(t), ...);
273 }
274
275 template<typename Tuple, std::size_t index>
276 void extract_value(Tuple &t) const;
277
279 template<typename TUPLE, std::size_t... indexes>
280 auto get_tuple(std::index_sequence<indexes...>) const
281 {
282 return std::make_tuple(get_field<TUPLE, indexes>()...);
283 }
284
286 template<typename TUPLE, std::size_t index> auto get_field() const
287 {
288 return (*this)[index].as<std::tuple_element_t<index, TUPLE>>();
289 }
290};
291
292
294class PQXX_LIBEXPORT const_row_iterator : public field
295{
296public:
297 using iterator_category = std::random_access_iterator_tag;
298 using value_type = field const;
299 using pointer = field const *;
300 using size_type = row_size_type;
301 using difference_type = row_difference_type;
302 using reference = field;
303
305 const_row_iterator() noexcept = default;
306#include "pqxx/internal/ignore-deprecated-post.hxx"
307 const_row_iterator(row const &t, row_size_type c) noexcept :
308 field{t.m_result, t.m_index, c}
309 {}
310 const_row_iterator(field const &F) noexcept : field{F} {}
311 const_row_iterator(const_row_iterator const &) noexcept = default;
312 const_row_iterator(const_row_iterator &&) noexcept = default;
313
318 [[nodiscard]] constexpr pointer operator->() const noexcept { return this; }
319 [[nodiscard]] reference operator*() const noexcept { return {*this}; }
321
326 const_row_iterator &operator=(const_row_iterator const &) noexcept = default;
327 const_row_iterator &operator=(const_row_iterator &&) noexcept = default;
328
329 const_row_iterator operator++(int) & noexcept;
330 const_row_iterator &operator++() noexcept
331 {
332 ++m_col;
333 return *this;
334 }
335 const_row_iterator operator--(int) & noexcept;
336 const_row_iterator &operator--() noexcept
337 {
338 --m_col;
339 return *this;
340 }
341
342 const_row_iterator &operator+=(difference_type i) noexcept
343 {
344 m_col = size_type(difference_type(m_col) + i);
345 return *this;
346 }
347 const_row_iterator &operator-=(difference_type i) noexcept
348 {
349 m_col = size_type(difference_type(m_col) - i);
350 return *this;
351 }
353
358 [[nodiscard]] constexpr bool
359 operator==(const_row_iterator const &i) const noexcept
360 {
361 return col() == i.col();
362 }
363 [[nodiscard]] constexpr bool
364 operator!=(const_row_iterator const &i) const noexcept
365 {
366 return col() != i.col();
367 }
368 [[nodiscard]] constexpr bool
369 operator<(const_row_iterator const &i) const noexcept
370 {
371 return col() < i.col();
372 }
373 [[nodiscard]] constexpr bool
374 operator<=(const_row_iterator const &i) const noexcept
375 {
376 return col() <= i.col();
377 }
378 [[nodiscard]] constexpr bool
379 operator>(const_row_iterator const &i) const noexcept
380 {
381 return col() > i.col();
382 }
383 [[nodiscard]] constexpr bool
384 operator>=(const_row_iterator const &i) const noexcept
385 {
386 return col() >= i.col();
387 }
389
394 [[nodiscard]] inline const_row_iterator
395 operator+(difference_type) const noexcept;
396
397 friend const_row_iterator
398 operator+(difference_type, const_row_iterator const &) noexcept;
399
400 [[nodiscard]] inline const_row_iterator
401 operator-(difference_type) const noexcept;
402 [[nodiscard]] inline difference_type
403 operator-(const_row_iterator const &) const noexcept;
404
405 [[nodiscard]] inline field operator[](difference_type offset) const noexcept
406 {
407 return *(*this + offset);
408 }
410};
411
412
414class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
415{
416public:
417 using super = const_row_iterator;
418 using iterator_type = const_row_iterator;
419 using iterator_type::difference_type;
420 using iterator_type::iterator_category;
421 using iterator_type::pointer;
422 using value_type = iterator_type::value_type;
423 using reference = iterator_type::reference;
424
425 const_reverse_row_iterator() noexcept = default;
426 const_reverse_row_iterator(const_reverse_row_iterator const &) noexcept =
427 default;
428 const_reverse_row_iterator(const_reverse_row_iterator &&) noexcept = default;
429
430 explicit const_reverse_row_iterator(super const &rhs) noexcept :
431 const_row_iterator{rhs}
432 {
433 super::operator--();
434 }
435
436 [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
437
442 using iterator_type::operator->;
443 using iterator_type::operator*;
445
450 const_reverse_row_iterator &
451 operator=(const_reverse_row_iterator const &r) noexcept
452 {
453 iterator_type::operator=(r);
454 return *this;
455 }
456 const_reverse_row_iterator operator++() noexcept
457 {
458 iterator_type::operator--();
459 return *this;
460 }
461 const_reverse_row_iterator operator++(int) & noexcept;
462 const_reverse_row_iterator &operator--() noexcept
463 {
464 iterator_type::operator++();
465 return *this;
466 }
467 const_reverse_row_iterator operator--(int) &;
468 const_reverse_row_iterator &operator+=(difference_type i) noexcept
469 {
470 iterator_type::operator-=(i);
471 return *this;
472 }
473 const_reverse_row_iterator &operator-=(difference_type i) noexcept
474 {
475 iterator_type::operator+=(i);
476 return *this;
477 }
479
484 [[nodiscard]] const_reverse_row_iterator
485 operator+(difference_type i) const noexcept
486 {
487 return const_reverse_row_iterator{base() - i};
488 }
489 [[nodiscard]] const_reverse_row_iterator
490 operator-(difference_type i) noexcept
491 {
492 return const_reverse_row_iterator{base() + i};
493 }
494 [[nodiscard]] difference_type
495 operator-(const_reverse_row_iterator const &rhs) const noexcept
496 {
497 return rhs.const_row_iterator::operator-(*this);
498 }
499 [[nodiscard]] inline field operator[](difference_type offset) const noexcept
500 {
501 return *(*this + offset);
502 }
504
509 [[nodiscard]] bool
510 operator==(const_reverse_row_iterator const &rhs) const noexcept
511 {
512 return iterator_type::operator==(rhs);
513 }
514 [[nodiscard]] bool
515 operator!=(const_reverse_row_iterator const &rhs) const noexcept
516 {
517 return !operator==(rhs);
518 }
519
520 [[nodiscard]] constexpr bool
521 operator<(const_reverse_row_iterator const &rhs) const noexcept
522 {
523 return iterator_type::operator>(rhs);
524 }
525 [[nodiscard]] constexpr bool
526 operator<=(const_reverse_row_iterator const &rhs) const noexcept
527 {
528 return iterator_type::operator>=(rhs);
529 }
530 [[nodiscard]] constexpr bool
531 operator>(const_reverse_row_iterator const &rhs) const noexcept
532 {
533 return iterator_type::operator<(rhs);
534 }
535 [[nodiscard]] constexpr bool
536 operator>=(const_reverse_row_iterator const &rhs) const noexcept
537 {
538 return iterator_type::operator<=(rhs);
539 }
541};
542
543
544const_row_iterator
545const_row_iterator::operator+(difference_type o) const noexcept
546{
547 // TODO:: More direct route to home().columns()?
548 return {
549 row{home(), idx(), home().columns()},
550 size_type(difference_type(col()) + o)};
551}
552
553inline const_row_iterator operator+(
554 const_row_iterator::difference_type o, const_row_iterator const &i) noexcept
555{
556 return i + o;
557}
558
559inline const_row_iterator
560const_row_iterator::operator-(difference_type o) const noexcept
561{
562 // TODO:: More direct route to home().columns()?
563 return {
564 row{home(), idx(), home().columns()},
565 size_type(difference_type(col()) - o)};
566}
567
568inline const_row_iterator::difference_type
569const_row_iterator::operator-(const_row_iterator const &i) const noexcept
570{
571 return difference_type(num() - i.num());
572}
573
574
575template<typename Tuple, std::size_t index>
576inline void row::extract_value(Tuple &t) const
577{
578 using field_type = strip_t<decltype(std::get<index>(t))>;
579 field const f{m_result, m_index, index};
580 std::get<index>(t) = from_string<field_type>(f);
581}
582} // namespace pqxx
583#endif
Iterator for looped unpacking of a result.
Definition result_iter.hxx:27
#define PQXX_LIBEXPORT
Definition header-pre.hxx:157
#define PQXX_PURE
Declare function "pure": no side effects, only reads globals and its args.
Definition header-pre.hxx:77
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
bool operator!=(stream_query_end_iterator, stream_query_input_iterator< TYPE... > const &i)
Definition stream_query_impl.hxx:143
bool operator==(stream_query_end_iterator, stream_query_input_iterator< TYPE... > const &i)
Definition stream_query_impl.hxx:135
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
unsigned int oid
PostgreSQL database row identifier.
Definition libpq-forward.hxx:33
int row_size_type
Number of fields in a row of database data.
Definition types.hxx:34
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition result_iterator.hxx:581
strip_t< decltype(*std::begin(std::declval< CONTAINER >()))> value_type
The type of a container's elements.
Definition types.hxx:96
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition types.hxx:80
int row_difference_type
Difference between row sizes.
Definition types.hxx:37
T from_string(field const &value)
Convert a field's value to type T.
Definition field.hxx:548