libpqxx  7.0.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 
24 namespace pqxx
25 {
27 
38 class PQXX_LIBEXPORT row
39 {
40 public:
45  using reference = field;
49 
50  row() = default;
51 
56  [[nodiscard]] PQXX_PURE bool operator==(row const &) const noexcept;
57  [[nodiscard]] bool operator!=(row const &rhs) const noexcept
58  {
59  return not operator==(rhs);
60  }
62 
63  [[nodiscard]] const_iterator begin() const noexcept;
64  [[nodiscard]] const_iterator cbegin() const noexcept;
65  [[nodiscard]] const_iterator end() const noexcept;
66  [[nodiscard]] const_iterator cend() const noexcept;
67 
72  [[nodiscard]] reference front() const noexcept;
73  [[nodiscard]] reference back() const noexcept;
74 
75  [[nodiscard]] const_reverse_row_iterator rbegin() const;
76  [[nodiscard]] const_reverse_row_iterator crbegin() const;
77  [[nodiscard]] const_reverse_row_iterator rend() const;
78  [[nodiscard]] const_reverse_row_iterator crend() const;
79 
80  [[nodiscard]] reference operator[](size_type) const noexcept;
84  [[nodiscard]] reference operator[](char const[]) const;
88  [[nodiscard]] reference operator[](std::string const &s) const
89  {
90  return (*this)[s.c_str()];
91  }
92 
93  reference at(size_type) const;
97  reference at(char const[]) const;
101  reference at(std::string const &s) const { return at(s.c_str()); }
103 
104  [[nodiscard]] size_type size() const noexcept { return m_end - m_begin; }
105 
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(std::string const &col_name) const
120  {
121  return column_number(col_name.c_str());
122  }
123 
125  size_type column_number(char const[]) const;
126 
128  [[nodiscard]] oid column_type(size_type) const;
129 
131  template<typename STRING> oid column_type(STRING col_name) const
132  {
133  return column_type(column_number(col_name));
134  }
135 
137  [[nodiscard]] oid column_table(size_type col_num) const;
138 
140  template<typename STRING> oid column_table(STRING col_name) const
141  {
142  return column_table(column_number(col_name));
143  }
144 
146 
153  [[nodiscard]] size_type table_column(size_type) const;
154 
156  template<typename STRING> size_type table_column(STRING col_name) const
157  {
158  return table_column(column_number(col_name));
159  }
161 
162  [[nodiscard]] result::size_type num() const { return rownumber(); }
163 
176  [[nodiscard]] row slice(size_type sbegin, size_type send) const;
177 
178  // Is this an empty slice?
179  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
180 
181 protected:
182  friend class const_row_iterator;
183  friend class result;
184  row(result const &r, result_size_type i) noexcept;
185 
186  friend class field;
190 
194  result::size_type m_index = 0;
196  size_type m_begin = 0;
198  size_type m_end = 0;
199 };
200 
201 
203 class PQXX_LIBEXPORT const_row_iterator : public field
204 {
205 public:
206  using iterator_category = std::random_access_iterator_tag;
207  using value_type = field const;
208  using pointer = field const *;
211  using reference = field;
212 
213  const_row_iterator(row const &T, row_size_type C) noexcept : field{T, C} {}
214  const_row_iterator(field const &F) noexcept : field{F} {}
215 
220  [[nodiscard]] pointer operator->() const { return this; }
221  [[nodiscard]] reference operator*() const { return field{*this}; }
223 
228  const_row_iterator operator++(int);
230  {
231  ++m_col;
232  return *this;
233  }
234  const_row_iterator operator--(int);
236  {
237  --m_col;
238  return *this;
239  }
240 
242  {
243  m_col = size_type(difference_type(m_col) + i);
244  return *this;
245  }
247  {
248  m_col = size_type(difference_type(m_col) - i);
249  return *this;
250  }
252 
257  [[nodiscard]] bool operator==(const_row_iterator const &i) const
258  {
259  return col() == i.col();
260  }
261  [[nodiscard]] bool operator!=(const_row_iterator const &i) const
262  {
263  return col() != i.col();
264  }
265  [[nodiscard]] bool operator<(const_row_iterator const &i) const
266  {
267  return col() < i.col();
268  }
269  [[nodiscard]] bool operator<=(const_row_iterator const &i) const
270  {
271  return col() <= i.col();
272  }
273  [[nodiscard]] bool operator>(const_row_iterator const &i) const
274  {
275  return col() > i.col();
276  }
277  [[nodiscard]] bool operator>=(const_row_iterator const &i) const
278  {
279  return col() >= i.col();
280  }
282 
287  [[nodiscard]] inline const_row_iterator operator+(difference_type) const;
288 
289  friend const_row_iterator
291 
292  [[nodiscard]] inline const_row_iterator operator-(difference_type) const;
293  [[nodiscard]] inline difference_type
294  operator-(const_row_iterator const &) const;
296 };
297 
298 
300 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
301 {
302 public:
310 
312  explicit const_reverse_row_iterator(super const &rhs) noexcept :
313  const_row_iterator{rhs}
314  {
315  super::operator--();
316  }
317 
318  [[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
319 
324  using iterator_type::operator->;
325  using iterator_type::operator*;
327 
333  {
334  iterator_type::operator=(r);
335  return *this;
336  }
338  {
339  iterator_type::operator--();
340  return *this;
341  }
342  const_reverse_row_iterator operator++(int);
344  {
345  iterator_type::operator++();
346  return *this;
347  }
348  const_reverse_row_iterator operator--(int);
350  {
351  iterator_type::operator-=(i);
352  return *this;
353  }
355  {
356  iterator_type::operator+=(i);
357  return *this;
358  }
360 
366  {
367  return const_reverse_row_iterator{base() - i};
368  }
370  {
371  return const_reverse_row_iterator{base() + i};
372  }
373  [[nodiscard]] difference_type
375  {
376  return rhs.const_row_iterator::operator-(*this);
377  }
379 
384  [[nodiscard]] bool operator==(const_reverse_row_iterator const &rhs) const
385  noexcept
386  {
387  return iterator_type::operator==(rhs);
388  }
389  [[nodiscard]] bool operator!=(const_reverse_row_iterator const &rhs) const
390  noexcept
391  {
392  return !operator==(rhs);
393  }
394 
395  [[nodiscard]] bool operator<(const_reverse_row_iterator const &rhs) const
396  {
397  return iterator_type::operator>(rhs);
398  }
399  [[nodiscard]] bool operator<=(const_reverse_row_iterator const &rhs) const
400  {
401  return iterator_type::operator>=(rhs);
402  }
403  [[nodiscard]] bool operator>(const_reverse_row_iterator const &rhs) const
404  {
405  return iterator_type::operator<(rhs);
406  }
407  [[nodiscard]] bool operator>=(const_reverse_row_iterator const &rhs) const
408  {
409  return iterator_type::operator<=(rhs);
410  }
412 };
413 
414 
416 {
417  return const_row_iterator{row(home(), idx()),
418  size_type(difference_type(col()) + o)};
419 }
420 
421 inline const_row_iterator
423 {
424  return i + o;
425 }
426 
429 {
430  return const_row_iterator{row(home(), idx()),
431  size_type(difference_type(col()) - o)};
432 }
433 
436 {
437  return difference_type(num() - i.num());
438 }
439 
440 } // namespace pqxx
441 
442 #include "pqxx/internal/compiler-internal-post.hxx"
443 #endif
const_row_iterator & operator--()
Definition: row.hxx:235
row_size_type col() const noexcept
Definition: field.hxx:213
reference operator[](std::string const &s) const
Definition: row.hxx:88
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:203
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:389
bool operator==(const_row_iterator const &i) const
Definition: row.hxx:257
const_reverse_row_iterator operator++()
Definition: row.hxx:337
const_reverse_row_iterator & operator--()
Definition: row.hxx:343
int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
pointer operator->() const
Definition: row.hxx:220
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:369
bool operator!=(const_row_iterator const &i) const
Definition: row.hxx:261
oid column_table(STRING col_name) const
What table did this column come from?
Definition: row.hxx:140
bool operator>(const_row_iterator const &i) const
Definition: row.hxx:273
row_size_type size_type
Definition: row.hxx:41
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:349
bool operator>=(const_row_iterator const &i) const
Definition: row.hxx:277
const_row_iterator(row const &T, row_size_type C) noexcept
Definition: row.hxx:213
difference_type operator-(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:374
bool operator<(const_row_iterator const &i) const
Definition: row.hxx:265
const_reverse_row_iterator & operator=(const_reverse_row_iterator const &r)
Definition: row.hxx:332
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:274
row_difference_type difference_type
Definition: row.hxx:210
oid column_type(STRING col_name) const
Return a column&#39;s type.
Definition: row.hxx:131
const_row_iterator operator-(difference_type) const
Definition: row.hxx:428
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:206
result m_result
Result set of which this is one row.
Definition: row.hxx:188
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:109
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:241
row_difference_type difference_type
Definition: row.hxx:42
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:365
bool operator!=(row const &rhs) const noexcept
Definition: row.hxx:57
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:246
field const value_type
Definition: row.hxx:207
bool operator>=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:407
const_reverse_row_iterator(super const &rhs) noexcept
Definition: row.hxx:312
reference at(std::string const &s) const
Definition: row.hxx:101
result::size_type num() const
Definition: row.hxx:162
field const * pointer
Definition: row.hxx:208
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:354
Result set containing data returned by a query or command.
Definition: result.hxx:70
row_size_type num() const
Definition: field.hxx:89
result_size_type size_type
Definition: result.hxx:73
bool operator>(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:403
size_type table_column(STRING col_name) const
What column number in its table did this result column come from?
Definition: row.hxx:156
field_size_type size_type
Definition: field.hxx:35
bool operator<=(const_row_iterator const &i) const
Definition: row.hxx:269
const_row_iterator operator+(difference_type) const
Definition: row.hxx:415
Reference to one row in a result.
Definition: row.hxx:38
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition: row.hxx:384
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
const_row_iterator & operator++()
Definition: row.hxx:229
const_row_iterator(field const &F) noexcept
Definition: row.hxx:214
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:300
size_type size() const noexcept
Definition: row.hxx:104
bool operator<(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:395
bool operator<=(const_reverse_row_iterator const &rhs) const
Definition: row.hxx:399
reference operator*() const
Definition: row.hxx:221