libpqxx  7.3.1
row.hxx
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-2021, 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 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include "pqxx/except.hxx"
20 #include "pqxx/field.hxx"
21 #include "pqxx/result.hxx"
22 
23 #include "pqxx/internal/concat.hxx"
24 
25 namespace pqxx::internal
26 {
27 template<typename... T> class result_iter;
28 } // namespace pqxx::internal
29 
30 
31 namespace pqxx
32 {
34 
45 class PQXX_LIBEXPORT row
46 {
47 public:
52  using reference = field;
56 
57  row() = default;
58  row(row &&) = default;
59  row(row const &) = default;
60  row &operator=(row const &) = default;
61  row &operator=(row &&) = default;
62 
67  [[nodiscard]] PQXX_PURE bool operator==(row const &) const noexcept;
68  [[nodiscard]] bool operator!=(row const &rhs) const noexcept
69  {
70  return not operator==(rhs);
71  }
73 
74  [[nodiscard]] const_iterator begin() const noexcept;
75  [[nodiscard]] const_iterator cbegin() const noexcept;
76  [[nodiscard]] const_iterator end() const noexcept;
77  [[nodiscard]] const_iterator cend() const noexcept;
78 
83  [[nodiscard]] reference front() const noexcept;
84  [[nodiscard]] reference back() const noexcept;
85 
86  [[nodiscard]] const_reverse_row_iterator rbegin() const;
87  [[nodiscard]] const_reverse_row_iterator crbegin() const;
88  [[nodiscard]] const_reverse_row_iterator rend() const;
89  [[nodiscard]] const_reverse_row_iterator crend() const;
90 
91  [[nodiscard]] reference operator[](size_type) const noexcept;
95  [[nodiscard]] reference operator[](zview col_name) const;
96 
97  reference at(size_type) const;
101  reference at(zview col_name) const;
102 
103  [[nodiscard]] size_type size() const noexcept { return m_end - m_begin; }
104 
105  // TODO: Remove this. Deprecated since early 2020.
107  void swap(row &) noexcept;
108 
110  [[nodiscard]] result::size_type rownumber() const noexcept
111  {
112  return m_index;
113  }
114 
119  [[nodiscard]] size_type column_number(zview col_name) const;
121 
123  [[nodiscard]] oid column_type(size_type) const;
124 
126  [[nodiscard]] oid column_type(zview col_name) const
127  {
128  return column_type(column_number(col_name));
129  }
130 
132  [[nodiscard]] oid column_table(size_type col_num) const;
133 
135  [[nodiscard]] oid column_table(zview col_name) const
136  {
137  return column_table(column_number(col_name));
138  }
139 
141 
148  [[nodiscard]] size_type table_column(size_type) const;
149 
151  [[nodiscard]] size_type table_column(zview col_name) const
152  {
153  return table_column(column_number(col_name));
154  }
156 
157  [[nodiscard]] result::size_type num() const { return rownumber(); }
158 
171  [[nodiscard]] row slice(size_type sbegin, size_type send) const;
172 
173  // Is this an empty slice?
174  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
175 
177 
179  template<typename Tuple> void to(Tuple &t) const
180  {
181  check_size(std::tuple_size_v<Tuple>);
182  convert(t);
183  }
184 
185  template<typename... TYPE> std::tuple<TYPE...> as() const
186  {
187  check_size(sizeof...(TYPE));
188  using seq = std::make_index_sequence<sizeof...(TYPE)>;
189  return get_tuple<std::tuple<TYPE...>>(seq{});
190  }
191 
192 protected:
193  friend class const_row_iterator;
194  friend class result;
195  row(result const &r, result_size_type i) noexcept;
196 
198  void check_size(size_type expected) const
199  {
200  if (size() != expected)
201  throw usage_error{internal::concat(
202  "Tried to extract ", expected, " field(s) from a row of ", size(),
203  ".")};
204  }
205 
206  template<typename... T> friend class pqxx::internal::result_iter;
208  template<typename Tuple> void convert(Tuple &t) const
209  {
210  constexpr auto tup_size{std::tuple_size_v<Tuple>};
211  extract_fields(t, std::make_index_sequence<tup_size>{});
212  }
213 
214  friend class field;
218 
222  result::size_type m_index = 0;
224  size_type m_begin = 0;
226  size_type m_end = 0;
227 
228 private:
229  template<typename Tuple, std::size_t... indexes>
230  void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
231  {
232  (extract_value<Tuple, indexes>(t), ...);
233  }
234 
235  template<typename Tuple, std::size_t index>
236  void extract_value(Tuple &t) const;
237 
239  template<typename TUPLE, std::size_t... indexes>
240  auto get_tuple(std::index_sequence<indexes...>) const
241  {
242  return std::make_tuple(get_field<TUPLE, indexes>()...);
243  }
244 
246  template<typename TUPLE, std::size_t index> auto get_field() const
247  {
248  return (*this)[index].as<std::tuple_element_t<index, TUPLE>>();
249  }
250 };
251 
252 
254 class PQXX_LIBEXPORT const_row_iterator : public field
255 {
256 public:
257  using iterator_category = std::random_access_iterator_tag;
258  using value_type = field const;
259  using pointer = field const *;
262  using reference = field;
263 
264  const_row_iterator() = default;
265  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
266  const_row_iterator(field const &F) noexcept : field{F} {}
267  const_row_iterator(const_row_iterator const &) = default;
269 
274  [[nodiscard]] pointer operator->() const { return this; }
275  [[nodiscard]] reference operator*() const { return field{*this}; }
277 
282  const_row_iterator &operator=(const_row_iterator const &) = default;
283  const_row_iterator &operator=(const_row_iterator &&) = default;
284 
285  const_row_iterator operator++(int);
287  {
288  ++m_col;
289  return *this;
290  }
291  const_row_iterator operator--(int);
293  {
294  --m_col;
295  return *this;
296  }
297 
299  {
300  m_col = size_type(difference_type(m_col) + i);
301  return *this;
302  }
304  {
305  m_col = size_type(difference_type(m_col) - i);
306  return *this;
307  }
309 
314  [[nodiscard]] bool operator==(const_row_iterator const &i) const
315  {
316  return col() == i.col();
317  }
318  [[nodiscard]] bool operator!=(const_row_iterator const &i) const
319  {
320  return col() != i.col();
321  }
322  [[nodiscard]] bool operator<(const_row_iterator const &i) const
323  {
324  return col() < i.col();
325  }
326  [[nodiscard]] bool operator<=(const_row_iterator const &i) const
327  {
328  return col() <= i.col();
329  }
330  [[nodiscard]] bool operator>(const_row_iterator const &i) const
331  {
332  return col() > i.col();
333  }
334  [[nodiscard]] bool operator>=(const_row_iterator const &i) const
335  {
336  return col() >= i.col();
337  }
339 
344  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
345 
346  friend const_row_iterator
348 
349  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
350  [[nodiscard]] inline difference_type
351  operator-(const_row_iterator const &) const;
353 };
354 
355 
357 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
358 {
359 public:
367 
368  const_reverse_row_iterator() = default;
371 
372  explicit const_reverse_row_iterator(super const &rhs) noexcept :
373  const_row_iterator{rhs}
374  {
375  super::operator--();
376  }
377 
378  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
379 
384  using iterator_type::operator->;
385  using iterator_type::operator*;
387 
393  {
394  iterator_type::operator=(r);
395  return *this;
396  }
398  {
399  iterator_type::operator--();
400  return *this;
401  }
402  const_reverse_row_iterator operator++(int);
404  {
405  iterator_type::operator++();
406  return *this;
407  }
408  const_reverse_row_iterator operator--(int);
410  {
411  iterator_type::operator-=(i);
412  return *this;
413  }
415  {
416  iterator_type::operator+=(i);
417  return *this;
418  }
420 
426  {
427  return const_reverse_row_iterator{base() - i};
428  }
430  {
431  return const_reverse_row_iterator{base() + i};
432  }
433  [[nodiscard]] difference_type
435  {
436  return rhs.const_row_iterator::operator-(*this);
437  }
439 
444  [[nodiscard]] bool
445  operator==(const_reverse_row_iterator const &rhs) const noexcept
446  {
447  return iterator_type::operator==(rhs);
448  }
449  [[nodiscard]] bool
450  operator!=(const_reverse_row_iterator const &rhs) const noexcept
451  {
452  return !operator==(rhs);
453  }
454 
455  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
456  {
457  return iterator_type::operator>(rhs);
458  }
459  [[nodiscard]] bool operator<=(const_reverse_row_iterator const &rhs) const
460  {
461  return iterator_type::operator>=(rhs);
462  }
463  [[nodiscard]] bool operator>(const_reverse_row_iterator const &rhs) const
464  {
465  return iterator_type::operator<(rhs);
466  }
467  [[nodiscard]] bool operator>=(const_reverse_row_iterator const &rhs) const
468  {
469  return iterator_type::operator<=(rhs);
470  }
472 };
473 
474 
476 {
477  return const_row_iterator{
478  row(home(), idx()), size_type(difference_type(col()) + o)};
479 }
480 
481 inline const_row_iterator
483 {
484  return i + o;
485 }
486 
487 inline const_row_iterator
489 {
490  return const_row_iterator{
491  row(home(), idx()), size_type(difference_type(col()) - o)};
492 }
493 
496 {
497  return difference_type(num() - i.num());
498 }
499 
500 
501 template<typename Tuple, std::size_t index>
502 inline void row::extract_value(Tuple &t) const
503 {
504  using field_type = strip_t<decltype(std::get<index>(t))>;
505  field const f{*this, index};
506  std::get<index>(t) = from_string<field_type>(f);
507 }
508 } // namespace pqxx
509 
510 #include "pqxx/internal/compiler-internal-post.hxx"
511 #endif
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:318
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:265
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:322
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:303
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:409
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:467
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:357
bool operator>=(const_row_iterator const &i) const
Definition: row.hxx:334
result_size_type size_type
Definition: result.hxx:73
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
std::random_access_iterator_tag iterator_category
Definition: row.hxx:257
result m_result
Result set of which this is one row.
Definition: row.hxx:216
const_row_iterator operator-(difference_type) const
Definition: row.hxx:488
const_row_iterator operator+(difference_type) const
Definition: row.hxx:475
bool operator<=(const_row_iterator const &i) const
Definition: row.hxx:326
result::size_type num() const
Definition: row.hxx:157
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:254
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:425
std::tuple< TYPE... > as() const
Definition: row.hxx:185
bool operator!=(row const &rhs) const noexcept
Definition: row.hxx:68
void convert(Tuple &t) const
Convert entire row to tuple fields, without checking row size.
Definition: row.hxx:208
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:314
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:434
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:445
Reference to a field in a result set.
Definition: field.hxx:33
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:463
Reference to one row in a result.
Definition: row.hxx:45
const_reverse_row_iterator & operator--()
Definition: row.hxx:403
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:372
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:450
row_size_type size_type
Definition: row.hxx:48
Result set containing data returned by a query or command.
Definition: result.hxx:70
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:429
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:143
reference operator*() const
Definition: row.hxx:275
Definition: row.hxx:27
void check_size(size_type expected) const
Throw usage_error if row size is not expected.
Definition: row.hxx:198
row_size_type col() const noexcept
Definition: field.hxx:261
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:342
pointer operator->() const
Definition: row.hxx:274
field const * pointer
Definition: row.hxx:259
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:110
const_row_iterator & operator--()
Definition: row.hxx:292
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:298
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
size_type table_column(zview col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:151
const_row_iterator & operator++()
Definition: row.hxx:286
const_reverse_row_iterator operator++()
Definition: row.hxx:397
size_type size() const noexcept
Definition: row.hxx:103
oid column_type(zview col_name) const
Return a column&#39;s type.
Definition: row.hxx:126
row_difference_type difference_type
Definition: row.hxx:49
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:414
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:455
oid column_table(zview col_name) const
What table did this column come from?
Definition: row.hxx:135
const_row_iterator(field const &F) noexcept
Definition: row.hxx:266
field const value_type
Definition: row.hxx:258
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:459
field_size_type size_type
Definition: field.hxx:36
PQXX_PURE row_size_type num() const
Return row number. The first row is row 0, the second is row 1, etc.
Definition: field.hxx:96
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:392
void to(Tuple &t) const
Extract entire row&#39;s values into a tuple.
Definition: row.hxx:179
row_difference_type difference_type
Definition: row.hxx:261
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:330