libpqxx  7.2.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 namespace pqxx::internal
24 {
25 template<typename... T> class result_iter;
26 } // namespace pqxx::internal
27 
28 
29 namespace pqxx
30 {
32 
43 class PQXX_LIBEXPORT row
44 {
45 public:
50  using reference = field;
54 
55  row() = default;
56  row(row &&) = default;
57  row(row const &) = default;
58  row &operator=(row const &) = default;
59  row &operator=(row &&) = default;
60 
65  [[nodiscard]] PQXX_PURE bool operator==(row const &) const noexcept;
66  [[nodiscard]] bool operator!=(row const &rhs) const noexcept
67  {
68  return not operator==(rhs);
69  }
71 
72  [[nodiscard]] const_iterator begin() const noexcept;
73  [[nodiscard]] const_iterator cbegin() const noexcept;
74  [[nodiscard]] const_iterator end() const noexcept;
75  [[nodiscard]] const_iterator cend() const noexcept;
76 
81  [[nodiscard]] reference front() const noexcept;
82  [[nodiscard]] reference back() const noexcept;
83 
84  [[nodiscard]] const_reverse_row_iterator rbegin() const;
85  [[nodiscard]] const_reverse_row_iterator crbegin() const;
86  [[nodiscard]] const_reverse_row_iterator rend() const;
87  [[nodiscard]] const_reverse_row_iterator crend() const;
88 
89  [[nodiscard]] reference operator[](size_type) const noexcept;
93  [[nodiscard]] reference operator[](zview col_name) const;
94 
95  reference at(size_type) const;
99  reference at(zview col_name) const;
100 
101  [[nodiscard]] size_type size() const noexcept { return m_end - m_begin; }
102 
104  void swap(row &) noexcept;
105 
107  [[nodiscard]] result::size_type rownumber() const noexcept
108  {
109  return m_index;
110  }
111 
116  [[nodiscard]] size_type column_number(zview col_name) const;
118 
120  [[nodiscard]] oid column_type(size_type) const;
121 
123  [[nodiscard]] oid column_type(zview col_name) const
124  {
125  return column_type(column_number(col_name));
126  }
127 
129  [[nodiscard]] oid column_table(size_type col_num) const;
130 
132  [[nodiscard]] oid column_table(zview col_name) const
133  {
134  return column_table(column_number(col_name));
135  }
136 
138 
145  [[nodiscard]] size_type table_column(size_type) const;
146 
148  [[nodiscard]] size_type table_column(zview col_name) const
149  {
150  return table_column(column_number(col_name));
151  }
153 
154  [[nodiscard]] result::size_type num() const { return rownumber(); }
155 
168  [[nodiscard]] row slice(size_type sbegin, size_type send) const;
169 
170  // Is this an empty slice?
171  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
172 
174 
176  template<typename Tuple> void to(Tuple &t) const
177  {
178  check_size(std::tuple_size_v<Tuple>);
179  convert(t);
180  }
181 
182 protected:
183  friend class const_row_iterator;
184  friend class result;
185  row(result const &r, result_size_type i) noexcept;
186 
188  void check_size(size_type expected) const
189  {
190  if (size() != expected)
191  throw usage_error{
192  "Tried to extract " + to_string(expected) +
193  " field(s) from a row of " + to_string(size()) + "."};
194  }
195 
196  template<typename... T> friend class pqxx::internal::result_iter;
198  template<typename Tuple> void convert(Tuple &t) const
199  {
200  constexpr auto tup_size{std::tuple_size_v<Tuple>};
201  extract_fields(t, std::make_index_sequence<tup_size>{});
202  }
203 
204  friend class field;
208 
212  result::size_type m_index = 0;
214  size_type m_begin = 0;
216  size_type m_end = 0;
217 
218 private:
219  template<typename Tuple, std::size_t... indexes>
220  void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
221  {
222  (extract_value<Tuple, indexes>(t), ...);
223  }
224 
225  template<typename Tuple, std::size_t index>
226  void extract_value(Tuple &t) const;
227 };
228 
229 
231 class PQXX_LIBEXPORT const_row_iterator : public field
232 {
233 public:
234  using iterator_category = std::random_access_iterator_tag;
235  using value_type = field const;
236  using pointer = field const *;
239  using reference = field;
240 
241  const_row_iterator() = default;
242  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
243  const_row_iterator(field const &F) noexcept : field{F} {}
244  const_row_iterator(const_row_iterator const &) = default;
246 
251  [[nodiscard]] pointer operator->() const { return this; }
252  [[nodiscard]] reference operator*() const { return field{*this}; }
254 
259  const_row_iterator &operator=(const_row_iterator const &) = default;
260  const_row_iterator &operator=(const_row_iterator &&) = default;
261 
262  const_row_iterator operator++(int);
264  {
265  ++m_col;
266  return *this;
267  }
268  const_row_iterator operator--(int);
270  {
271  --m_col;
272  return *this;
273  }
274 
276  {
277  m_col = size_type(difference_type(m_col) + i);
278  return *this;
279  }
281  {
282  m_col = size_type(difference_type(m_col) - i);
283  return *this;
284  }
286 
291  [[nodiscard]] bool operator==(const_row_iterator const &i) const
292  {
293  return col() == i.col();
294  }
295  [[nodiscard]] bool operator!=(const_row_iterator const &i) const
296  {
297  return col() != i.col();
298  }
299  [[nodiscard]] bool operator<(const_row_iterator const &i) const
300  {
301  return col() < i.col();
302  }
303  [[nodiscard]] bool operator<=(const_row_iterator const &i) const
304  {
305  return col() <= i.col();
306  }
307  [[nodiscard]] bool operator>(const_row_iterator const &i) const
308  {
309  return col() > i.col();
310  }
311  [[nodiscard]] bool operator>=(const_row_iterator const &i) const
312  {
313  return col() >= i.col();
314  }
316 
321  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
322 
323  friend const_row_iterator
325 
326  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
327  [[nodiscard]] inline difference_type
328  operator-(const_row_iterator const &) const;
330 };
331 
332 
334 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
335 {
336 public:
344 
345  const_reverse_row_iterator() = default;
348 
349  explicit const_reverse_row_iterator(super const &rhs) noexcept :
350  const_row_iterator{rhs}
351  {
352  super::operator--();
353  }
354 
355  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
356 
361  using iterator_type::operator->;
362  using iterator_type::operator*;
364 
370  {
371  iterator_type::operator=(r);
372  return *this;
373  }
375  {
376  iterator_type::operator--();
377  return *this;
378  }
379  const_reverse_row_iterator operator++(int);
381  {
382  iterator_type::operator++();
383  return *this;
384  }
385  const_reverse_row_iterator operator--(int);
387  {
388  iterator_type::operator-=(i);
389  return *this;
390  }
392  {
393  iterator_type::operator+=(i);
394  return *this;
395  }
397 
403  {
404  return const_reverse_row_iterator{base() - i};
405  }
407  {
408  return const_reverse_row_iterator{base() + i};
409  }
410  [[nodiscard]] difference_type
412  {
413  return rhs.const_row_iterator::operator-(*this);
414  }
416 
421  [[nodiscard]] bool
422  operator==(const_reverse_row_iterator const &rhs) const noexcept
423  {
424  return iterator_type::operator==(rhs);
425  }
426  [[nodiscard]] bool
427  operator!=(const_reverse_row_iterator const &rhs) const noexcept
428  {
429  return !operator==(rhs);
430  }
431 
432  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
433  {
434  return iterator_type::operator>(rhs);
435  }
436  [[nodiscard]] bool operator<=(const_reverse_row_iterator const &rhs) const
437  {
438  return iterator_type::operator>=(rhs);
439  }
440  [[nodiscard]] bool operator>(const_reverse_row_iterator const &rhs) const
441  {
442  return iterator_type::operator<(rhs);
443  }
444  [[nodiscard]] bool operator>=(const_reverse_row_iterator const &rhs) const
445  {
446  return iterator_type::operator<=(rhs);
447  }
449 };
450 
451 
453 {
454  return const_row_iterator{
455  row(home(), idx()), size_type(difference_type(col()) + o)};
456 }
457 
458 inline const_row_iterator
460 {
461  return i + o;
462 }
463 
464 inline const_row_iterator
466 {
467  return const_row_iterator{
468  row(home(), idx()), size_type(difference_type(col()) - o)};
469 }
470 
473 {
474  return difference_type(num() - i.num());
475 }
476 
477 
478 template<typename Tuple, std::size_t index>
479 inline void row::extract_value(Tuple &t) const
480 {
481  using field_type = strip_t<decltype(std::get<index>(t))>;
482  field const f{*this, index};
483  std::get<index>(t) = from_string<field_type>(f);
484 }
485 } // namespace pqxx
486 
487 #include "pqxx/internal/compiler-internal-post.hxx"
488 #endif
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:369
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:436
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:402
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: composite.hxx:70
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:307
const_row_iterator operator+(difference_type) const
Definition: row.hxx:452
size_type size() const noexcept
Definition: row.hxx:101
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:386
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:107
bool operator<=(const_row_iterator const &i) const
Definition: row.hxx:303
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:432
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
void to(Tuple &t) const
Extract entire row&#39;s values into a tuple.
Definition: row.hxx:176
field const value_type
Definition: row.hxx:235
bool operator!=(row const &rhs) const noexcept
Definition: row.hxx:66
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:406
const_reverse_row_iterator & operator--()
Definition: row.hxx:380
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:242
result m_result
Result set of which this is one row.
Definition: row.hxx:206
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:427
void check_size(size_type expected) const
Throw usage_error if row size is not expected.
Definition: row.hxx:188
const_row_iterator operator-(difference_type) const
Definition: row.hxx:465
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:299
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:120
bool operator>=(const_row_iterator const &i) const
Definition: row.hxx:311
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:275
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
field_size_type size_type
Definition: field.hxx:36
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:329
PQXX_PURE row_size_type num() const
Definition: field.hxx:92
Reference to one row in a result.
Definition: row.hxx:43
oid column_type(zview col_name) const
Return a column&#39;s type.
Definition: row.hxx:123
result_size_type size_type
Definition: result.hxx:73
Reference to a field in a result set.
Definition: field.hxx:33
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:295
std::random_access_iterator_tag iterator_category
Definition: row.hxx:234
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
reference operator*() const
Definition: row.hxx:252
const_reverse_row_iterator operator++()
Definition: row.hxx:374
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:291
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:334
row_size_type col() const noexcept
Definition: field.hxx:232
void convert(Tuple &t) const
Convert entire row to tuple fields, without checking row size.
Definition: row.hxx:198
Result set containing data returned by a query or command.
Definition: result.hxx:70
size_type table_column(zview col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:148
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:36
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:391
const_row_iterator & operator--()
Definition: row.hxx:269
Definition: row.hxx:25
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
row_size_type size_type
Definition: row.hxx:46
oid column_table(zview col_name) const
What table did this column come from?
Definition: row.hxx:132
pointer operator->() const
Definition: row.hxx:251
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:280
field const * pointer
Definition: row.hxx:236
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:411
row_difference_type difference_type
Definition: row.hxx:47
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:349
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:422
const_row_iterator & operator++()
Definition: row.hxx:263
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:231
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
row_difference_type difference_type
Definition: row.hxx:238
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:444
result::size_type num() const
Definition: row.hxx:154
const_row_iterator(field const &F) noexcept
Definition: row.hxx:243
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:440