libpqxx  7.1.2
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[](char const[]) const;
97  [[nodiscard]] reference operator[](std::string const &s) const
98  {
99  return (*this)[s.c_str()];
100  }
101 
102  reference at(size_type) const;
106  reference at(char const[]) const;
110  reference at(std::string const &s) const { return at(s.c_str()); }
112 
113  [[nodiscard]] size_type size() const noexcept { return m_end - m_begin; }
114 
116  void swap(row &) noexcept;
117 
119  [[nodiscard]] result::size_type rownumber() const noexcept
120  {
121  return m_index;
122  }
123 
128  [[nodiscard]] size_type column_number(std::string const &col_name) const
130  {
131  return column_number(col_name.c_str());
132  }
133 
135  size_type column_number(char const[]) const;
136 
138  [[nodiscard]] oid column_type(size_type) const;
139 
141  template<typename STRING> oid column_type(STRING col_name) const
142  {
143  return column_type(column_number(col_name));
144  }
145 
147  [[nodiscard]] oid column_table(size_type col_num) const;
148 
150  template<typename STRING> oid column_table(STRING col_name) const
151  {
152  return column_table(column_number(col_name));
153  }
154 
156 
163  [[nodiscard]] size_type table_column(size_type) const;
164 
166  template<typename STRING> size_type table_column(STRING col_name) const
167  {
168  return table_column(column_number(col_name));
169  }
171 
172  [[nodiscard]] result::size_type num() const { return rownumber(); }
173 
186  [[nodiscard]] row slice(size_type sbegin, size_type send) const;
187 
188  // Is this an empty slice?
189  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
190 
192 
194  template<typename Tuple> void to(Tuple &t) const
195  {
196  check_size(std::tuple_size_v<Tuple>);
197  convert(t);
198  }
199 
200 protected:
201  friend class const_row_iterator;
202  friend class result;
203  row(result const &r, result_size_type i) noexcept;
204 
206  void check_size(size_type expected) const
207  {
208  if (size() != expected)
209  throw usage_error{
210  "Tried to extract " + to_string(expected) +
211  " field(s) from a row of " + to_string(size()) + "."};
212  }
213 
214  template<typename... T> friend class pqxx::internal::result_iter;
216  template<typename Tuple> void convert(Tuple &t) const
217  {
218  constexpr auto tup_size{std::tuple_size_v<Tuple>};
219  extract_fields(t, std::make_index_sequence<tup_size>{});
220  }
221 
222  friend class field;
226 
230  result::size_type m_index = 0;
232  size_type m_begin = 0;
234  size_type m_end = 0;
235 
236 private:
237  template<typename Tuple, std::size_t... indexes>
238  void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
239  {
240  (extract_value<Tuple, indexes>(t), ...);
241  }
242 
243  template<typename Tuple, std::size_t index>
244  void extract_value(Tuple &t) const;
245 };
246 
247 
249 class PQXX_LIBEXPORT const_row_iterator : public field
250 {
251 public:
252  using iterator_category = std::random_access_iterator_tag;
253  using value_type = field const;
254  using pointer = field const *;
257  using reference = field;
258 
259  const_row_iterator() = default;
260  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
261  const_row_iterator(field const &F) noexcept : field{F} {}
262  const_row_iterator(const_row_iterator const &) = default;
264 
269  [[nodiscard]] pointer operator->() const { return this; }
270  [[nodiscard]] reference operator*() const { return field{*this}; }
272 
277  const_row_iterator &operator=(const_row_iterator const &) = default;
278  const_row_iterator &operator=(const_row_iterator &&) = default;
279 
280  const_row_iterator operator++(int);
282  {
283  ++m_col;
284  return *this;
285  }
286  const_row_iterator operator--(int);
288  {
289  --m_col;
290  return *this;
291  }
292 
294  {
295  m_col = size_type(difference_type(m_col) + i);
296  return *this;
297  }
299  {
300  m_col = size_type(difference_type(m_col) - i);
301  return *this;
302  }
304 
309  [[nodiscard]] bool operator==(const_row_iterator const &i) const
310  {
311  return col() == i.col();
312  }
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  }
334 
339  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
340 
341  friend const_row_iterator
343 
344  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
345  [[nodiscard]] inline difference_type
346  operator-(const_row_iterator const &) const;
348 };
349 
350 
352 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
353 {
354 public:
362 
363  const_reverse_row_iterator() = default;
366 
367  explicit const_reverse_row_iterator(super const &rhs) noexcept :
368  const_row_iterator{rhs}
369  {
370  super::operator--();
371  }
372 
373  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
374 
379  using iterator_type::operator->;
380  using iterator_type::operator*;
382 
388  {
389  iterator_type::operator=(r);
390  return *this;
391  }
393  {
394  iterator_type::operator--();
395  return *this;
396  }
397  const_reverse_row_iterator operator++(int);
399  {
400  iterator_type::operator++();
401  return *this;
402  }
403  const_reverse_row_iterator operator--(int);
405  {
406  iterator_type::operator-=(i);
407  return *this;
408  }
410  {
411  iterator_type::operator+=(i);
412  return *this;
413  }
415 
421  {
422  return const_reverse_row_iterator{base() - i};
423  }
425  {
426  return const_reverse_row_iterator{base() + i};
427  }
428  [[nodiscard]] difference_type
430  {
431  return rhs.const_row_iterator::operator-(*this);
432  }
434 
439  [[nodiscard]] bool
440  operator==(const_reverse_row_iterator const &rhs) const noexcept
441  {
442  return iterator_type::operator==(rhs);
443  }
444  [[nodiscard]] bool
445  operator!=(const_reverse_row_iterator const &rhs) const noexcept
446  {
447  return !operator==(rhs);
448  }
449 
450  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
451  {
452  return iterator_type::operator>(rhs);
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  }
467 };
468 
469 
471 {
472  return const_row_iterator{
473  row(home(), idx()), size_type(difference_type(col()) + o)};
474 }
475 
476 inline const_row_iterator
478 {
479  return i + o;
480 }
481 
482 inline const_row_iterator
484 {
485  return const_row_iterator{
486  row(home(), idx()), size_type(difference_type(col()) - o)};
487 }
488 
491 {
492  return difference_type(num() - i.num());
493 }
494 
495 
496 template<typename Tuple, std::size_t index>
497 inline void row::extract_value(Tuple &t) const
498 {
499  using field_type = strip_t<decltype(std::get<index>(t))>;
500  field const f{*this, index};
501  std::get<index>(t) = from_string<field_type>(f);
502 }
503 } // namespace pqxx
504 
505 #include "pqxx/internal/compiler-internal-post.hxx"
506 #endif
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:119
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:260
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:298
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:317
row_size_type col() const noexcept
Definition: field.hxx:215
oid column_table(STRING col_name) const
What table did this column come from?
Definition: row.hxx:150
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: connection.hxx:60
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:409
Reference to one row in a result.
Definition: row.hxx:43
field const * pointer
Definition: row.hxx:254
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:404
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:462
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:429
result::size_type num() const
Definition: row.hxx:172
reference operator[](std::string const &s) const
Definition: row.hxx:97
std::random_access_iterator_tag iterator_category
Definition: row.hxx:252
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:293
field const value_type
Definition: row.hxx:253
oid column_type(STRING col_name) const
Return a column&#39;s type.
Definition: row.hxx:141
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:440
void check_size(size_type expected) const
Throw usage_error if row size is not expected.
Definition: row.hxx:206
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
void convert(Tuple &t) const
Convert entire row to tuple fields, without checking row size.
Definition: row.hxx:216
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:119
const_row_iterator & operator--()
Definition: row.hxx:287
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:367
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_row_iterator const &i) const
Definition: row.hxx:321
const_row_iterator operator+(difference_type) const
Definition: row.hxx:470
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:445
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:309
reference at(std::string const &s) const
Definition: row.hxx:110
void to(Tuple &t) const
Extract entire row&#39;s values into a tuple.
Definition: row.hxx:194
const_row_iterator & operator++()
Definition: row.hxx:281
size_type size() const noexcept
Definition: row.hxx:113
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:387
const_row_iterator(field const &F) noexcept
Definition: row.hxx:261
pointer operator->() const
Definition: row.hxx:269
reference operator*() const
Definition: row.hxx:270
size_type table_column(STRING col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:166
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:313
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:352
const_reverse_row_iterator & operator--()
Definition: row.hxx:398
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:454
Result set containing data returned by a query or command.
Definition: result.hxx:70
row_size_type num() const
Definition: field.hxx:91
row_difference_type difference_type
Definition: row.hxx:47
result_size_type size_type
Definition: result.hxx:73
row_size_type size_type
Definition: row.hxx:46
field_size_type size_type
Definition: field.hxx:35
row_difference_type difference_type
Definition: row.hxx:256
Definition: row.hxx:25
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:165
bool operator!=(row const &rhs) const noexcept
Definition: row.hxx:66
result m_result
Result set of which this is one row.
Definition: row.hxx:224
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:458
const_row_iterator operator-(difference_type) const
Definition: row.hxx:483
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:450
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:420
Reference to a field in a result set.
Definition: field.hxx:32
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:325
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:249
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:424
const_reverse_row_iterator operator++()
Definition: row.hxx:392
bool operator>=(const_row_iterator const &i) const
Definition: row.hxx:329