libpqxx  7.5.0
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  PQXX_DEPRECATED("Swap iterators, not rows.")
106  void swap(row &) noexcept;
107 
109  [[nodiscard]] result::size_type rownumber() const noexcept
110  {
111  return m_index;
112  }
113 
118  [[nodiscard]] size_type column_number(zview col_name) const;
120 
122  [[nodiscard]] oid column_type(size_type) const;
123 
125  [[nodiscard]] oid column_type(zview col_name) const
126  {
127  return column_type(column_number(col_name));
128  }
129 
131  [[nodiscard]] oid column_table(size_type col_num) const;
132 
134  [[nodiscard]] oid column_table(zview col_name) const
135  {
136  return column_table(column_number(col_name));
137  }
138 
140 
147  [[nodiscard]] size_type table_column(size_type) const;
148 
150  [[nodiscard]] size_type table_column(zview col_name) const
151  {
152  return table_column(column_number(col_name));
153  }
155 
156  [[nodiscard]] result::size_type num() const { return rownumber(); }
157 
170  [[nodiscard]] row slice(size_type sbegin, size_type send) const;
171 
172  // Is this an empty slice?
173  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
174 
176 
178  template<typename Tuple> void to(Tuple &t) const
179  {
180  check_size(std::tuple_size_v<Tuple>);
181  convert(t);
182  }
183 
184  template<typename... TYPE> std::tuple<TYPE...> as() const
185  {
186  check_size(sizeof...(TYPE));
187  using seq = std::make_index_sequence<sizeof...(TYPE)>;
188  return get_tuple<std::tuple<TYPE...>>(seq{});
189  }
190 
191 protected:
192  friend class const_row_iterator;
193  friend class result;
194  row(result const &r, result_size_type i) noexcept;
195 
197  void check_size(size_type expected) const
198  {
199  if (size() != expected)
200  throw usage_error{internal::concat(
201  "Tried to extract ", expected, " field(s) from a row of ", size(),
202  ".")};
203  }
204 
205  template<typename... T> friend class pqxx::internal::result_iter;
207  template<typename Tuple> void convert(Tuple &t) const
208  {
209  constexpr auto tup_size{std::tuple_size_v<Tuple>};
210  extract_fields(t, std::make_index_sequence<tup_size>{});
211  }
212 
213  friend class field;
217 
221  result::size_type m_index = 0;
223  size_type m_begin = 0;
225  size_type m_end = 0;
226 
227 private:
228  template<typename Tuple, std::size_t... indexes>
229  void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
230  {
231  (extract_value<Tuple, indexes>(t), ...);
232  }
233 
234  template<typename Tuple, std::size_t index>
235  void extract_value(Tuple &t) const;
236 
238  template<typename TUPLE, std::size_t... indexes>
239  auto get_tuple(std::index_sequence<indexes...>) const
240  {
241  return std::make_tuple(get_field<TUPLE, indexes>()...);
242  }
243 
245  template<typename TUPLE, std::size_t index> auto get_field() const
246  {
247  return (*this)[index].as<std::tuple_element_t<index, TUPLE>>();
248  }
249 };
250 
251 
253 class PQXX_LIBEXPORT const_row_iterator : public field
254 {
255 public:
256  using iterator_category = std::random_access_iterator_tag;
257  using value_type = field const;
258  using pointer = field const *;
261  using reference = field;
262 
263 #include "pqxx/internal/ignore-deprecated-pre.hxx"
264  const_row_iterator() = default;
265  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
266 #include "pqxx/internal/ignore-deprecated-post.hxx"
267  const_row_iterator(field const &F) noexcept : field{F} {}
268  const_row_iterator(const_row_iterator const &) = default;
270 
275  [[nodiscard]] pointer operator->() const { return this; }
276  [[nodiscard]] reference operator*() const { return field{*this}; }
278 
283  const_row_iterator &operator=(const_row_iterator const &) = default;
284  const_row_iterator &operator=(const_row_iterator &&) = default;
285 
286  const_row_iterator operator++(int);
288  {
289  ++m_col;
290  return *this;
291  }
292  const_row_iterator operator--(int);
294  {
295  --m_col;
296  return *this;
297  }
298 
300  {
301  m_col = size_type(difference_type(m_col) + i);
302  return *this;
303  }
305  {
306  m_col = size_type(difference_type(m_col) - i);
307  return *this;
308  }
310 
315  [[nodiscard]] bool operator==(const_row_iterator const &i) const
316  {
317  return col() == i.col();
318  }
319  [[nodiscard]] bool operator!=(const_row_iterator const &i) const
320  {
321  return col() != i.col();
322  }
323  [[nodiscard]] bool operator<(const_row_iterator const &i) const
324  {
325  return col() < i.col();
326  }
327  [[nodiscard]] bool operator<=(const_row_iterator const &i) const
328  {
329  return col() <= i.col();
330  }
331  [[nodiscard]] bool operator>(const_row_iterator const &i) const
332  {
333  return col() > i.col();
334  }
335  [[nodiscard]] bool operator>=(const_row_iterator const &i) const
336  {
337  return col() >= i.col();
338  }
340 
345  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
346 
347  friend const_row_iterator
349 
350  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
351  [[nodiscard]] inline difference_type
352  operator-(const_row_iterator const &) const;
354 };
355 
356 
358 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
359 {
360 public:
368 
369  const_reverse_row_iterator() = default;
372 
373  explicit const_reverse_row_iterator(super const &rhs) noexcept :
374  const_row_iterator{rhs}
375  {
376  super::operator--();
377  }
378 
379  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
380 
385  using iterator_type::operator->;
386  using iterator_type::operator*;
388 
394  {
395  iterator_type::operator=(r);
396  return *this;
397  }
399  {
400  iterator_type::operator--();
401  return *this;
402  }
403  const_reverse_row_iterator operator++(int);
405  {
406  iterator_type::operator++();
407  return *this;
408  }
409  const_reverse_row_iterator operator--(int);
411  {
412  iterator_type::operator-=(i);
413  return *this;
414  }
416  {
417  iterator_type::operator+=(i);
418  return *this;
419  }
421 
427  {
428  return const_reverse_row_iterator{base() - i};
429  }
431  {
432  return const_reverse_row_iterator{base() + i};
433  }
434  [[nodiscard]] difference_type
436  {
437  return rhs.const_row_iterator::operator-(*this);
438  }
440 
445  [[nodiscard]] bool
446  operator==(const_reverse_row_iterator const &rhs) const noexcept
447  {
448  return iterator_type::operator==(rhs);
449  }
450  [[nodiscard]] bool
451  operator!=(const_reverse_row_iterator const &rhs) const noexcept
452  {
453  return !operator==(rhs);
454  }
455 
456  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
457  {
458  return iterator_type::operator>(rhs);
459  }
460  [[nodiscard]] bool operator<=(const_reverse_row_iterator const &rhs) const
461  {
462  return iterator_type::operator>=(rhs);
463  }
464  [[nodiscard]] bool operator>(const_reverse_row_iterator const &rhs) const
465  {
466  return iterator_type::operator<(rhs);
467  }
468  [[nodiscard]] bool operator>=(const_reverse_row_iterator const &rhs) const
469  {
470  return iterator_type::operator<=(rhs);
471  }
473 };
474 
475 
477 {
478  return const_row_iterator{
479  row(home(), idx()), size_type(difference_type(col()) + o)};
480 }
481 
482 inline const_row_iterator
484 {
485  return i + o;
486 }
487 
488 inline const_row_iterator
490 {
491  return const_row_iterator{
492  row(home(), idx()), size_type(difference_type(col()) - o)};
493 }
494 
497 {
498  return difference_type(num() - i.num());
499 }
500 
501 
502 template<typename Tuple, std::size_t index>
503 inline void row::extract_value(Tuple &t) const
504 {
505  using field_type = strip_t<decltype(std::get<index>(t))>;
506 #include "pqxx/internal/ignore-deprecated-pre.hxx"
507  field const f{*this, index};
508 #include "pqxx/internal/ignore-deprecated-post.hxx"
509  std::get<index>(t) = from_string<field_type>(f);
510 }
511 } // namespace pqxx
512 
513 #include "pqxx/internal/compiler-internal-post.hxx"
514 #endif
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:323
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:464
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:410
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:253
const_row_iterator operator+(difference_type) const
Definition: row.hxx:476
std::tuple< TYPE... > as() const
Definition: row.hxx:184
row_difference_type difference_type
Definition: row.hxx:260
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:460
field const value_type
Definition: row.hxx:257
oid column_type(zview col_name) const
Return a column&#39;s type.
Definition: row.hxx:125
oid column_table(zview col_name) const
What table did this column come from?
Definition: row.hxx:134
const_reverse_row_iterator & operator--()
Definition: row.hxx:404
result::size_type num() const
Definition: row.hxx:156
field const * pointer
Definition: row.hxx:258
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
size_type table_column(zview col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:150
Result set containing data returned by a query or command.
Definition: result.hxx:70
pointer operator->() const
Definition: row.hxx:275
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
const_row_iterator operator-(difference_type) const
Definition: row.hxx:489
void to(Tuple &t) const
Extract entire row&#39;s values into a tuple.
Definition: row.hxx:178
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:393
row_size_type col() const noexcept
Definition: field.hxx:261
Definition: row.hxx:27
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:430
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:373
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
reference operator*() const
Definition: row.hxx:276
bool operator>=(const_row_iterator const &i) const
Definition: row.hxx:335
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:304
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:265
bool operator!=(row const &rhs) const noexcept
Definition: row.hxx:68
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:299
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:451
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:435
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:315
const_row_iterator & operator--()
Definition: row.hxx:293
result m_result
Result set of which this is one row.
Definition: row.hxx:215
void convert(Tuple &t) const
Convert entire row to tuple fields, without checking row size.
Definition: row.hxx:207
field_size_type size_type
Definition: field.hxx:36
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:147
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
Reference to one row in a result.
Definition: row.hxx:45
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:415
const_row_iterator(field const &F) noexcept
Definition: row.hxx:267
std::random_access_iterator_tag iterator_category
Definition: row.hxx:256
const_row_iterator & operator++()
Definition: row.hxx:287
bool operator<=(const_row_iterator const &i) const
Definition: row.hxx:327
Reference to a field in a result set.
Definition: field.hxx:33
result_size_type size_type
Definition: result.hxx:73
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:456
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:319
row_difference_type difference_type
Definition: row.hxx:49
const_reverse_row_iterator operator++()
Definition: row.hxx:398
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
row_size_type size_type
Definition: row.hxx:48
void check_size(size_type expected) const
Throw usage_error if row size is not expected.
Definition: row.hxx:197
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:426
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:331
size_type size() const noexcept
Definition: row.hxx:103
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:358
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:446
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:357
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:468