libpqxx
row.hxx
1 
13 #ifndef PQXX_H_ROW
14 #define PQXX_H_ROW
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
18 
19 #include "pqxx/except.hxx"
20 #include "pqxx/field.hxx"
21 #include "pqxx/result.hxx"
22 
23 
24 // Methods tested in eg. test module test01 are marked with "//[t01]".
25 
26 namespace pqxx
27 {
29 
40 class PQXX_LIBEXPORT row
41 {
42 public:
47  using reference = field;
51 
52  row() =default;
53 
55  row(result r, size_t i) noexcept;
56 
57  ~row() noexcept =default; // Yes Scott Meyers, you're absolutely right[1]
58 
63  PQXX_PURE bool operator==(const row &) const noexcept; //[t75]
64  bool operator!=(const row &rhs) const noexcept //[t75]
65  { return not operator==(rhs); }
67 
68  const_iterator begin() const noexcept; //[t82]
69  const_iterator cbegin() const noexcept;
70  const_iterator end() const noexcept; //[t82]
71  const_iterator cend() const noexcept;
72 
77  reference front() const noexcept; //[t74]
78  reference back() const noexcept; //[t75]
79 
80  const_reverse_row_iterator rbegin() const; //[t82]
81  const_reverse_row_iterator crbegin() const;
82  const_reverse_row_iterator rend() const; //[t82]
83  const_reverse_row_iterator crend() const;
84 
85  reference operator[](size_type) const noexcept; //[t11]
86  reference operator[](int) const noexcept; //[t02]
90  reference operator[](const char[]) const; //[t11]
94  reference operator[](const std::string &) const; //[t11]
95  reference at(size_type) const; //[t11]
96  reference at(int) const; //[t11]
100  reference at(const char[]) const; //[t11]
104  reference at(const std::string &) const; //[t11]
106 
107  size_type size() const noexcept //[t11]
108  { return m_end-m_begin; }
109 
110  void swap(row &) noexcept; //[t11]
111 
113  size_t rownumber() const noexcept { return size_t(m_index); } //[t11]
114 
119  size_type column_number(const std::string &ColName) const //[t30]
121  { return column_number(ColName.c_str()); }
122 
124  size_type column_number(const char[]) const; //[t30]
125 
127  oid column_type(size_type) const; //[t07]
128 
130  oid column_type(int ColNum) const //[t07]
131  { return column_type(size_type(ColNum)); }
132 
134  oid column_type(const std::string &ColName) const //[t07]
135  { return column_type(column_number(ColName)); }
136 
138  oid column_type(const char ColName[]) const //[t07]
139  { return column_type(column_number(ColName)); }
140 
142  oid column_table(size_type ColNum) const; //[t02]
143 
145  oid column_table(int ColNum) const //[t02]
146  { return column_table(size_type(ColNum)); }
148  oid column_table(const std::string &ColName) const //[t02]
149  { return column_table(column_number(ColName)); }
150 
152 
159  size_type table_column(size_type) const; //[t93]
160 
162  size_type table_column(int ColNum) const //[t93]
163  { return table_column(size_type(ColNum)); }
164 
166  size_type table_column(const std::string &ColName) const //[t93]
167  { return table_column(column_number(ColName)); }
169 
170  size_t num() const { return rownumber(); } //[t01]
171 
184  row slice(size_type Begin, size_type End) const;
185 
186  // Is this an empty slice?
187  PQXX_PURE bool empty() const noexcept;
188 
189 protected:
190  friend class field;
194 
198  long m_index = 0;
200  size_type m_begin = 0;
202  size_type m_end = 0;
203 };
204 
205 
207 class PQXX_LIBEXPORT const_row_iterator : public field
208 {
209 public:
210  using iterator_category = std::random_access_iterator_tag;
211  using value_type = const field;
212  using pointer = const field *;
215  using reference = field;
216 
217  const_row_iterator(const row &T, row_size_type C) noexcept : //[t82]
218  field{T, C} {}
219  const_row_iterator(const field &F) noexcept : field{F} {} //[t82]
220 
225  pointer operator->() const { return this; } //[t82]
226  reference operator*() const { return field{*this}; } //[t82]
228 
233  const_row_iterator operator++(int); //[t82]
234  const_row_iterator &operator++() { ++m_col; return *this; } //[t82]
235  const_row_iterator operator--(int); //[t82]
236  const_row_iterator &operator--() { --m_col; return *this; } //[t82]
237 
239  { m_col = size_type(difference_type(m_col) + i); return *this; }
241  { m_col = size_type(difference_type(m_col) - i); return *this; }
243 
248  bool operator==(const const_row_iterator &i) const //[t82]
249  {return col()==i.col();}
250  bool operator!=(const const_row_iterator &i) const //[t82]
251  {return col()!=i.col();}
252  bool operator<(const const_row_iterator &i) const //[t82]
253  {return col()<i.col();}
254  bool operator<=(const const_row_iterator &i) const //[t82]
255  {return col()<=i.col();}
256  bool operator>(const const_row_iterator &i) const //[t82]
257  {return col()>i.col();}
258  bool operator>=(const const_row_iterator &i) const //[t82]
259  {return col()>=i.col();}
261 
266  inline const_row_iterator operator+(difference_type) const; //[t82]
267 
268  friend const_row_iterator operator+( //[t82]
271 
272  inline const_row_iterator operator-(difference_type) const; //[t82]
273  inline difference_type operator-(const_row_iterator) const; //[t82]
275 };
276 
277 
279 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
280 {
281 public:
289 
291  const_row_iterator{r} {}
292  explicit
293  const_reverse_row_iterator(const super &rhs) noexcept : //[t82]
294  const_row_iterator{rhs} { super::operator--(); }
295 
296  PQXX_PURE iterator_type base() const noexcept; //[t82]
297 
302  using iterator_type::operator->; //[t82]
303  using iterator_type::operator*; //[t82]
305 
312  { iterator_type::operator=(r); return *this; }
314  { iterator_type::operator--(); return *this; }
315  const_reverse_row_iterator operator++(int); //[t82]
317  { iterator_type::operator++(); return *this; }
318  const_reverse_row_iterator operator--(int); //[t82]
320  { iterator_type::operator-=(i); return *this; }
322  { iterator_type::operator+=(i); return *this; }
324 
330  { return const_reverse_row_iterator{base()-i}; }
332  { return const_reverse_row_iterator{base()+i}; }
334  operator-(const const_reverse_row_iterator &rhs) const //[t82]
335  { return rhs.const_row_iterator::operator-(*this); }
337 
342  bool operator==(const const_reverse_row_iterator &rhs) const noexcept //[t82]
343  { return iterator_type::operator==(rhs); }
344  bool operator!=(const const_reverse_row_iterator &rhs) const noexcept //[t82]
345  { return !operator==(rhs); }
346 
347  bool operator<(const const_reverse_row_iterator &rhs) const //[t82]
348  { return iterator_type::operator>(rhs); }
349  bool operator<=(const const_reverse_row_iterator &rhs) const //[t82]
350  { return iterator_type::operator>=(rhs); }
351  bool operator>(const const_reverse_row_iterator &rhs) const //[t82]
352  { return iterator_type::operator<(rhs); }
353  bool operator>=(const const_reverse_row_iterator &rhs) const //[t82]
354  { return iterator_type::operator<=(rhs); }
356 };
357 
358 
359 inline const_row_iterator
361 {
362  return const_row_iterator{
363  row(home(), idx()),
364  size_type(difference_type(col()) + o)};
365 }
366 
367 inline const_row_iterator
369  { return i + o; }
370 
371 inline const_row_iterator
373 {
374  return const_row_iterator{
375  row(home(), idx()),
376  size_type(difference_type(col()) - o)};
377 }
378 
381  { return difference_type(num() - i.num()); }
382 
383 
384 } // namespace pqxx
385 
386 
387 /*
388 [1] Scott Meyers, in one of his essential books, "Effective C++" and "More
389 Effective C++", points out that it is good style to have any class containing
390 a member of pointer type define a destructor--just to show that it knows what
391 it is doing with the pointer. This helps prevent nasty memory leak / double
392 deletion bugs typically resulting from programmers' omission to deal with such
393 issues in their destructors.
394 
395 The @c -Weffc++ option in gcc generates warnings for noncompliance with Scott's
396 style guidelines, and hence necessitates the definition of this destructor,
397 trivial as it may be.
398 */
399 
400 
401 #include "pqxx/compiler-internal-post.hxx"
402 
403 #endif
size_t rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:113
Reference to one row in a result.
Definition: row.hxx:40
const_row_iterator(const field &F) noexcept
Definition: row.hxx:219
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:240
oid column_type(int ColNum) const
Type of given column.
Definition: row.hxx:130
const_row_iterator operator-(difference_type) const
Definition: row.hxx:372
bool operator==(const const_row_iterator &i) const
Definition: row.hxx:248
bool operator>=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:353
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:331
bool operator<=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:349
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
difference_type operator-(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:334
bool operator<(const const_row_iterator &i) const
Definition: row.hxx:252
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:329
const_reverse_row_iterator(const super &rhs) noexcept
Definition: row.hxx:293
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:279
bool operator>(const const_row_iterator &i) const
Definition: row.hxx:256
bool operator>(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:351
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:319
field_size_type size_type
Definition: field.hxx:52
const_reverse_row_iterator & operator--()
Definition: row.hxx:316
bool operator>=(const const_row_iterator &i) const
Definition: row.hxx:258
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:207
size_type table_column(const std::string &ColName) const
What column number in its table did this result column come from?
Definition: row.hxx:166
signed int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
Reference to a field in a result set.
Definition: field.hxx:49
reference operator*() const
Definition: row.hxx:226
size_t num() const
Definition: row.hxx:170
pointer operator->() const
Definition: row.hxx:225
bool operator==(const const_reverse_row_iterator &rhs) const noexcept
Definition: row.hxx:342
oid column_type(const char ColName[]) const
Type of given column.
Definition: row.hxx:138
row_difference_type difference_type
Definition: row.hxx:44
const_row_iterator(const row &T, row_size_type C) noexcept
Definition: row.hxx:217
bool operator<(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:347
const_row_iterator & operator++()
Definition: row.hxx:234
const_reverse_row_iterator operator++()
Definition: row.hxx:313
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:321
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: row.hxx:148
result m_result
Result set of which this is one row.
Definition: row.hxx:192
const_reverse_row_iterator & operator=(const const_reverse_row_iterator &r)
Definition: row.hxx:311
bool operator<=(const const_row_iterator &i) const
Definition: row.hxx:254
size_type size() const noexcept
Definition: row.hxx:107
row_size_type num() const
Definition: field.hxx:104
bool operator!=(const const_row_iterator &i) const
Definition: row.hxx:250
oid column_table(int ColNum) const
What table did this column come from?
Definition: row.hxx:145
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
const_reverse_row_iterator(const const_reverse_row_iterator &r)
Definition: row.hxx:290
const_row_iterator operator+(difference_type) const
Definition: row.hxx:360
bool operator!=(const const_reverse_row_iterator &rhs) const noexcept
Definition: row.hxx:344
row_size_type size_type
Definition: row.hxx:43
size_type table_column(int ColNum) const
What column number in its table did this result column come from?
Definition: row.hxx:162
Result set containing data returned by a query or command.
Definition: result.hxx:69
std::random_access_iterator_tag iterator_category
Definition: row.hxx:210
const_row_iterator & operator--()
Definition: row.hxx:236
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:238
const_result_iterator operator+(result::difference_type o, const_result_iterator i)
Definition: result_iterator.hxx:212
oid column_type(const std::string &ColName) const
Type of given column.
Definition: row.hxx:134
row_difference_type difference_type
Definition: row.hxx:214