libpqxx  7.0.3
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 
24 namespace pqxx
25 {
27 
38 class PQXX_LIBEXPORT row
39 {
40 public:
45  using reference = field;
49 
50  row() = default;
51  row(row &&) = default;
52  row(row const &) = default;
53  row &operator=(row const &) = default;
54  row &operator=(row &&) = default;
55 
60  [[nodiscard]] PQXX_PURE bool operator==(row const &) const noexcept;
61  [[nodiscard]] bool operator!=(row const &rhs) const noexcept
62  {
63  return not operator==(rhs);
64  }
66 
67  [[nodiscard]] const_iterator begin() const noexcept;
68  [[nodiscard]] const_iterator cbegin() const noexcept;
69  [[nodiscard]] const_iterator end() const noexcept;
70  [[nodiscard]] const_iterator cend() const noexcept;
71 
76  [[nodiscard]] reference front() const noexcept;
77  [[nodiscard]] reference back() const noexcept;
78 
79  [[nodiscard]] const_reverse_row_iterator rbegin() const;
80  [[nodiscard]] const_reverse_row_iterator crbegin() const;
81  [[nodiscard]] const_reverse_row_iterator rend() const;
82  [[nodiscard]] const_reverse_row_iterator crend() const;
83 
84  [[nodiscard]] reference operator[](size_type) const noexcept;
88  [[nodiscard]] reference operator[](char const[]) const;
92  [[nodiscard]] reference operator[](std::string const &s) const
93  {
94  return (*this)[s.c_str()];
95  }
96 
97  reference at(size_type) const;
101  reference at(char const[]) const;
105  reference at(std::string const &s) const { return at(s.c_str()); }
107 
108  [[nodiscard]] size_type size() const noexcept { return m_end - m_begin; }
109 
111  void swap(row &) noexcept;
112 
114  [[nodiscard]] result::size_type rownumber() const noexcept
115  {
116  return m_index;
117  }
118 
123  [[nodiscard]] size_type column_number(std::string const &col_name) const
125  {
126  return column_number(col_name.c_str());
127  }
128 
130  size_type column_number(char const[]) const;
131 
133  [[nodiscard]] oid column_type(size_type) const;
134 
136  template<typename STRING> oid column_type(STRING col_name) const
137  {
138  return column_type(column_number(col_name));
139  }
140 
142  [[nodiscard]] oid column_table(size_type col_num) const;
143 
145  template<typename STRING> oid column_table(STRING col_name) const
146  {
147  return column_table(column_number(col_name));
148  }
149 
151 
158  [[nodiscard]] size_type table_column(size_type) const;
159 
161  template<typename STRING> size_type table_column(STRING col_name) const
162  {
163  return table_column(column_number(col_name));
164  }
166 
167  [[nodiscard]] result::size_type num() const { return rownumber(); }
168 
181  [[nodiscard]] row slice(size_type sbegin, size_type send) const;
182 
183  // Is this an empty slice?
184  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
185 
186 protected:
187  friend class const_row_iterator;
188  friend class result;
189  row(result const &r, result_size_type i) noexcept;
190 
191  friend class field;
195 
199  result::size_type m_index = 0;
201  size_type m_begin = 0;
203  size_type m_end = 0;
204 };
205 
206 
208 class PQXX_LIBEXPORT const_row_iterator : public field
209 {
210 public:
211  using iterator_category = std::random_access_iterator_tag;
212  using value_type = field const;
213  using pointer = field const *;
216  using reference = field;
217 
218  const_row_iterator() = default;
219  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
220  const_row_iterator(field const &F) noexcept : field{F} {}
221  const_row_iterator(const_row_iterator const &) = default;
223 
228  [[nodiscard]] pointer operator->() const { return this; }
229  [[nodiscard]] reference operator*() const { return field{*this}; }
231 
236  const_row_iterator &operator=(const_row_iterator const &) = default;
237  const_row_iterator &operator=(const_row_iterator &&) = default;
238 
239  const_row_iterator operator++(int);
241  {
242  ++m_col;
243  return *this;
244  }
245  const_row_iterator operator--(int);
247  {
248  --m_col;
249  return *this;
250  }
251 
253  {
254  m_col = size_type(difference_type(m_col) + i);
255  return *this;
256  }
258  {
259  m_col = size_type(difference_type(m_col) - i);
260  return *this;
261  }
263 
268  [[nodiscard]] bool operator==(const_row_iterator const &i) const
269  {
270  return col() == i.col();
271  }
272  [[nodiscard]] bool operator!=(const_row_iterator const &i) const
273  {
274  return col() != i.col();
275  }
276  [[nodiscard]] bool operator<(const_row_iterator const &i) const
277  {
278  return col() < i.col();
279  }
280  [[nodiscard]] bool operator<=(const_row_iterator const &i) const
281  {
282  return col() <= i.col();
283  }
284  [[nodiscard]] bool operator>(const_row_iterator const &i) const
285  {
286  return col() > i.col();
287  }
288  [[nodiscard]] bool operator>=(const_row_iterator const &i) const
289  {
290  return col() >= i.col();
291  }
293 
298  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
299 
300  friend const_row_iterator
302 
303  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
304  [[nodiscard]] inline difference_type
305  operator-(const_row_iterator const &) const;
307 };
308 
309 
311 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
312 {
313 public:
321 
322  const_reverse_row_iterator() = default;
325 
326  explicit const_reverse_row_iterator(super const &rhs) noexcept :
327  const_row_iterator{rhs}
328  {
329  super::operator--();
330  }
331 
332  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
333 
338  using iterator_type::operator->;
339  using iterator_type::operator*;
341 
347  {
348  iterator_type::operator=(r);
349  return *this;
350  }
352  {
353  iterator_type::operator--();
354  return *this;
355  }
356  const_reverse_row_iterator operator++(int);
358  {
359  iterator_type::operator++();
360  return *this;
361  }
362  const_reverse_row_iterator operator--(int);
364  {
365  iterator_type::operator-=(i);
366  return *this;
367  }
369  {
370  iterator_type::operator+=(i);
371  return *this;
372  }
374 
380  {
381  return const_reverse_row_iterator{base() - i};
382  }
384  {
385  return const_reverse_row_iterator{base() + i};
386  }
387  [[nodiscard]] difference_type
389  {
390  return rhs.const_row_iterator::operator-(*this);
391  }
393 
398  [[nodiscard]] bool operator==(const_reverse_row_iterator const &rhs) const
399  noexcept
400  {
401  return iterator_type::operator==(rhs);
402  }
403  [[nodiscard]] bool operator!=(const_reverse_row_iterator const &rhs) const
404  noexcept
405  {
406  return !operator==(rhs);
407  }
408 
409  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
410  {
411  return iterator_type::operator>(rhs);
412  }
413  [[nodiscard]] bool operator<=(const_reverse_row_iterator const &rhs) const
414  {
415  return iterator_type::operator>=(rhs);
416  }
417  [[nodiscard]] bool operator>(const_reverse_row_iterator const &rhs) const
418  {
419  return iterator_type::operator<(rhs);
420  }
421  [[nodiscard]] bool operator>=(const_reverse_row_iterator const &rhs) const
422  {
423  return iterator_type::operator<=(rhs);
424  }
426 };
427 
428 
430 {
431  return const_row_iterator{row(home(), idx()),
432  size_type(difference_type(col()) + o)};
433 }
434 
435 inline const_row_iterator
437 {
438  return i + o;
439 }
440 
443 {
444  return const_row_iterator{row(home(), idx()),
445  size_type(difference_type(col()) - o)};
446 }
447 
450 {
451  return difference_type(num() - i.num());
452 }
453 
454 } // namespace pqxx
455 
456 #include "pqxx/internal/compiler-internal-post.hxx"
457 #endif
const_row_iterator & operator--()
Definition: row.hxx:246
row_size_type col() const noexcept
Definition: field.hxx:215
reference operator[](std::string const &s) const
Definition: row.hxx:92
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:208
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:403
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:268
const_reverse_row_iterator operator++()
Definition: row.hxx:351
const_reverse_row_iterator & operator--()
Definition: row.hxx:357
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
pointer operator->() const
Definition: row.hxx:228
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:383
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:272
oid column_table(STRING col_name) const
What table did this column come from?
Definition: row.hxx:145
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:284
row_size_type size_type
Definition: row.hxx:41
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:363
bool operator>=(const_row_iterator const &i) const
Definition: row.hxx:288
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:219
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:388
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:276
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:346
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
row_difference_type difference_type
Definition: row.hxx:215
oid column_type(STRING col_name) const
Return a column&#39;s type.
Definition: row.hxx:136
const_row_iterator operator-(difference_type) const
Definition: row.hxx:442
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
std::random_access_iterator_tag iterator_category
Definition: row.hxx:211
result m_result
Result set of which this is one row.
Definition: row.hxx:193
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:114
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:252
row_difference_type difference_type
Definition: row.hxx:42
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:379
bool operator!=(row const &rhs) const noexcept
Definition: row.hxx:61
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:257
field const value_type
Definition: row.hxx:212
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:421
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:326
reference at(std::string const &s) const
Definition: row.hxx:105
result::size_type num() const
Definition: row.hxx:167
field const * pointer
Definition: row.hxx:213
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:368
Result set containing data returned by a query or command.
Definition: result.hxx:70
row_size_type num() const
Definition: field.hxx:91
result_size_type size_type
Definition: result.hxx:73
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:417
size_type table_column(STRING col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:161
field_size_type size_type
Definition: field.hxx:35
bool operator<=(const_row_iterator const &i) const
Definition: row.hxx:280
const_row_iterator operator+(difference_type) const
Definition: row.hxx:429
Reference to one row in a result.
Definition: row.hxx:38
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:398
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
const_row_iterator & operator++()
Definition: row.hxx:240
const_row_iterator(field const &F) noexcept
Definition: row.hxx:220
Reference to a field in a result set.
Definition: field.hxx:32
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:311
size_type size() const noexcept
Definition: row.hxx:108
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:409
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:413
reference operator*() const
Definition: row.hxx:229