libpqxx  7.3.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-2020, 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 
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  const_row_iterator() = default;
264  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
265  const_row_iterator(field const &F) noexcept : field{F} {}
266  const_row_iterator(const_row_iterator const &) = default;
268 
273  [[nodiscard]] pointer operator->() const { return this; }
274  [[nodiscard]] reference operator*() const { return field{*this}; }
276 
281  const_row_iterator &operator=(const_row_iterator const &) = default;
282  const_row_iterator &operator=(const_row_iterator &&) = default;
283 
284  const_row_iterator operator++(int);
286  {
287  ++m_col;
288  return *this;
289  }
290  const_row_iterator operator--(int);
292  {
293  --m_col;
294  return *this;
295  }
296 
298  {
299  m_col = size_type(difference_type(m_col) + i);
300  return *this;
301  }
303  {
304  m_col = size_type(difference_type(m_col) - i);
305  return *this;
306  }
308 
313  [[nodiscard]] bool operator==(const_row_iterator const &i) const
314  {
315  return col() == i.col();
316  }
317  [[nodiscard]] bool operator!=(const_row_iterator const &i) const
318  {
319  return col() != i.col();
320  }
321  [[nodiscard]] bool operator<(const_row_iterator const &i) const
322  {
323  return col() < i.col();
324  }
325  [[nodiscard]] bool operator<=(const_row_iterator const &i) const
326  {
327  return col() <= i.col();
328  }
329  [[nodiscard]] bool operator>(const_row_iterator const &i) const
330  {
331  return col() > i.col();
332  }
333  [[nodiscard]] bool operator>=(const_row_iterator const &i) const
334  {
335  return col() >= i.col();
336  }
338 
343  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
344 
345  friend const_row_iterator
347 
348  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
349  [[nodiscard]] inline difference_type
350  operator-(const_row_iterator const &) const;
352 };
353 
354 
356 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
357 {
358 public:
366 
367  const_reverse_row_iterator() = default;
370 
371  explicit const_reverse_row_iterator(super const &rhs) noexcept :
372  const_row_iterator{rhs}
373  {
374  super::operator--();
375  }
376 
377  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
378 
383  using iterator_type::operator->;
384  using iterator_type::operator*;
386 
392  {
393  iterator_type::operator=(r);
394  return *this;
395  }
397  {
398  iterator_type::operator--();
399  return *this;
400  }
401  const_reverse_row_iterator operator++(int);
403  {
404  iterator_type::operator++();
405  return *this;
406  }
407  const_reverse_row_iterator operator--(int);
409  {
410  iterator_type::operator-=(i);
411  return *this;
412  }
414  {
415  iterator_type::operator+=(i);
416  return *this;
417  }
419 
425  {
426  return const_reverse_row_iterator{base() - i};
427  }
429  {
430  return const_reverse_row_iterator{base() + i};
431  }
432  [[nodiscard]] difference_type
434  {
435  return rhs.const_row_iterator::operator-(*this);
436  }
438 
443  [[nodiscard]] bool
444  operator==(const_reverse_row_iterator const &rhs) const noexcept
445  {
446  return iterator_type::operator==(rhs);
447  }
448  [[nodiscard]] bool
449  operator!=(const_reverse_row_iterator const &rhs) const noexcept
450  {
451  return !operator==(rhs);
452  }
453 
454  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
455  {
456  return iterator_type::operator>(rhs);
457  }
458  [[nodiscard]] bool operator<=(const_reverse_row_iterator const &rhs) const
459  {
460  return iterator_type::operator>=(rhs);
461  }
462  [[nodiscard]] bool operator>(const_reverse_row_iterator const &rhs) const
463  {
464  return iterator_type::operator<(rhs);
465  }
466  [[nodiscard]] bool operator>=(const_reverse_row_iterator const &rhs) const
467  {
468  return iterator_type::operator<=(rhs);
469  }
471 };
472 
473 
475 {
476  return const_row_iterator{
477  row(home(), idx()), size_type(difference_type(col()) + o)};
478 }
479 
480 inline const_row_iterator
482 {
483  return i + o;
484 }
485 
486 inline const_row_iterator
488 {
489  return const_row_iterator{
490  row(home(), idx()), size_type(difference_type(col()) - o)};
491 }
492 
495 {
496  return difference_type(num() - i.num());
497 }
498 
499 
500 template<typename Tuple, std::size_t index>
501 inline void row::extract_value(Tuple &t) const
502 {
503  using field_type = strip_t<decltype(std::get<index>(t))>;
504  field const f{*this, index};
505  std::get<index>(t) = from_string<field_type>(f);
506 }
507 } // namespace pqxx
508 
509 #include "pqxx/internal/compiler-internal-post.hxx"
510 #endif
Definition: row.hxx:27
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
row_difference_type difference_type
Definition: row.hxx:260
size_type table_column(zview col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:150
const_reverse_row_iterator operator++()
Definition: row.hxx:396
const_reverse_row_iterator & operator--()
Definition: row.hxx:402
const_row_iterator(field const &F) noexcept
Definition: row.hxx:265
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:207
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
const_row_iterator operator-(difference_type) const
Definition: row.hxx:487
row_size_type size_type
Definition: row.hxx:48
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:391
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:321
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:458
field_size_type size_type
Definition: field.hxx:36
PQXX_PURE row_size_type num() const
Definition: field.hxx:92
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:371
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:253
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:317
bool operator<=(const_row_iterator const &i) const
Definition: row.hxx:325
std::random_access_iterator_tag iterator_category
Definition: row.hxx:256
reference operator*() const
Definition: row.hxx:274
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:313
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
row_size_type col() const noexcept
Definition: field.hxx:257
field const value_type
Definition: row.hxx:257
result::size_type num() const
Definition: row.hxx:156
std::tuple< TYPE... > as() const
Definition: row.hxx:184
Reference to one row in a result.
Definition: row.hxx:45
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:329
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:466
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:264
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:413
oid column_type(zview col_name) const
Return a column&#39;s type.
Definition: row.hxx:125
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:333
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:408
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:109
oid column_table(zview col_name) const
What table did this column come from?
Definition: row.hxx:134
pointer operator->() const
Definition: row.hxx:273
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:302
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:297
field const * pointer
Definition: row.hxx:258
result_size_type size_type
Definition: result.hxx:73
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:444
void to(Tuple &t) const
Extract entire row&#39;s values into a tuple.
Definition: row.hxx:178
const_row_iterator & operator++()
Definition: row.hxx:285
size_type size() const noexcept
Definition: row.hxx:103
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:454
void check_size(size_type expected) const
Throw usage_error if row size is not expected.
Definition: row.hxx:197
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:428
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:462
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:424
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
const_row_iterator & operator--()
Definition: row.hxx:291
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:449
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:329
result m_result
Result set of which this is one row.
Definition: row.hxx:215
const_row_iterator operator+(difference_type) const
Definition: row.hxx:474
Reference to a field in a result set.
Definition: field.hxx:33
Result set containing data returned by a query or command.
Definition: result.hxx:70
row_difference_type difference_type
Definition: row.hxx:49
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:433
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:356