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 !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 :
208  public std::iterator<
209  std::random_access_iterator_tag,
210  const field,
211  row_size_type
212  >,
213  public field
214 {
215  using it = std::iterator<
216  std::random_access_iterator_tag,
217  const field,
219  >;
220 public:
221  using it::pointer;
224  using reference = field;
225 
226  const_row_iterator(const row &T, row_size_type C) noexcept : //[t82]
227  field(T, C) {}
228  const_row_iterator(const field &F) noexcept : field(F) {} //[t82]
229 
234  pointer operator->() const { return this; } //[t82]
235  reference operator*() const { return field(*this); } //[t82]
237 
242  const_row_iterator operator++(int); //[t82]
243  const_row_iterator &operator++() { ++m_col; return *this; } //[t82]
244  const_row_iterator operator--(int); //[t82]
245  const_row_iterator &operator--() { --m_col; return *this; } //[t82]
246 
248  { m_col = size_type(difference_type(m_col) + i); return *this; }
250  { m_col = size_type(difference_type(m_col) - i); return *this; }
252 
257  bool operator==(const const_row_iterator &i) const //[t82]
258  {return col()==i.col();}
259  bool operator!=(const const_row_iterator &i) const //[t82]
260  {return col()!=i.col();}
261  bool operator<(const const_row_iterator &i) const //[t82]
262  {return col()<i.col();}
263  bool operator<=(const const_row_iterator &i) const //[t82]
264  {return col()<=i.col();}
265  bool operator>(const const_row_iterator &i) const //[t82]
266  {return col()>i.col();}
267  bool operator>=(const const_row_iterator &i) const //[t82]
268  {return col()>=i.col();}
270 
275  inline const_row_iterator operator+(difference_type) const; //[t82]
276 
277  friend const_row_iterator operator+( //[t82]
280 
281  inline const_row_iterator operator-(difference_type) const; //[t82]
282  inline difference_type operator-(const_row_iterator) const; //[t82]
284 };
285 
286 
288 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
289 {
290 public:
293  using iterator_type::iterator_category;
295  using iterator_type::pointer;
296  using value_type = iterator_type::value_type;
298 
300  const_row_iterator(r) {}
301  explicit
302  const_reverse_row_iterator(const super &rhs) noexcept : //[t82]
303  const_row_iterator(rhs) { super::operator--(); }
304 
305  PQXX_PURE iterator_type base() const noexcept; //[t82]
306 
311  using iterator_type::operator->; //[t82]
312  using iterator_type::operator*; //[t82]
314 
321  { iterator_type::operator=(r); return *this; }
323  { iterator_type::operator--(); return *this; }
324  const_reverse_row_iterator operator++(int); //[t82]
326  { iterator_type::operator++(); return *this; }
327  const_reverse_row_iterator operator--(int); //[t82]
329  { iterator_type::operator-=(i); return *this; }
331  { iterator_type::operator+=(i); return *this; }
333 
339  { return const_reverse_row_iterator(base()-i); }
341  { return const_reverse_row_iterator(base()+i); }
343  operator-(const const_reverse_row_iterator &rhs) const //[t82]
344  { return rhs.const_row_iterator::operator-(*this); }
346 
351  bool operator==(const const_reverse_row_iterator &rhs) const noexcept //[t82]
352  { return iterator_type::operator==(rhs); }
353  bool operator!=(const const_reverse_row_iterator &rhs) const noexcept //[t82]
354  { return !operator==(rhs); }
355 
356  bool operator<(const const_reverse_row_iterator &rhs) const //[t82]
357  { return iterator_type::operator>(rhs); }
358  bool operator<=(const const_reverse_row_iterator &rhs) const //[t82]
359  { return iterator_type::operator>=(rhs); }
360  bool operator>(const const_reverse_row_iterator &rhs) const //[t82]
361  { return iterator_type::operator<(rhs); }
362  bool operator>=(const const_reverse_row_iterator &rhs) const //[t82]
363  { return iterator_type::operator<=(rhs); }
365 };
366 
367 
368 inline const_row_iterator
370 {
371  return const_row_iterator(
372  row(home(), idx()),
373  size_type(difference_type(col()) + o));
374 }
375 
376 inline const_row_iterator
378  { return i + o; }
379 
380 inline const_row_iterator
382 {
383  return const_row_iterator(
384  row(home(), idx()),
385  size_type(difference_type(col()) - o));
386 }
387 
390  { return difference_type(num() - i.num()); }
391 
392 
393 } // namespace pqxx
394 
395 
396 /*
397 [1] Scott Meyers, in one of his essential books, "Effective C++" and "More
398 Effective C++", points out that it is good style to have any class containing
399 a member of pointer type define a destructor--just to show that it knows what
400 it is doing with the pointer. This helps prevent nasty memory leak / double
401 deletion bugs typically resulting from programmers' omission to deal with such
402 issues in their destructors.
403 
404 The @c -Weffc++ option in gcc generates warnings for noncompliance with Scott's
405 style guidelines, and hence necessitates the definition of this destructor,
406 trivial as it may be.
407 */
408 
409 
410 #include "pqxx/compiler-internal-post.hxx"
411 
412 #endif
bool operator<(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:356
const_row_iterator & operator--()
Definition: row.hxx:245
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:247
row_size_type size_type
Definition: row.hxx:43
iterator_type::value_type value_type
Definition: row.hxx:296
size_type table_column(int ColNum) const
What column number in its table did this result column come from?
Definition: row.hxx:162
oid column_type(const std::string &ColName) const
Type of given column.
Definition: row.hxx:134
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: row.hxx:148
row_size_type num() const
Definition: field.hxx:96
Reference to one row in a result.
Definition: row.hxx:40
difference_type operator-(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:343
size_type size() const noexcept
Definition: row.hxx:107
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:330
bool operator!=(const const_row_iterator &i) const
Definition: row.hxx:259
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:288
bool operator<=(const const_row_iterator &i) const
Definition: row.hxx:263
result m_result
Result set of which this is one row.
Definition: row.hxx:192
bool operator>(const const_row_iterator &i) const
Definition: row.hxx:265
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
bool operator<=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:358
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:207
row_difference_type difference_type
Definition: row.hxx:44
const_result_iterator operator+(result::difference_type o, const_result_iterator i)
Definition: result_iterator.hxx:217
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:340
bool operator==(const const_row_iterator &i) const
Definition: row.hxx:257
bool operator>=(const const_row_iterator &i) const
Definition: row.hxx:267
bool operator>(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:360
Result set containing data returned by a query or command.
Definition: result.hxx:67
const_reverse_row_iterator operator++()
Definition: row.hxx:322
const_row_iterator operator-(difference_type) const
Definition: row.hxx:381
size_t rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:113
bool operator==(const const_reverse_row_iterator &rhs) const noexcept
Definition: row.hxx:351
oid column_type(const char ColName[]) const
Type of given column.
Definition: row.hxx:138
const_reverse_row_iterator & operator--()
Definition: row.hxx:325
const_reverse_row_iterator(const const_reverse_row_iterator &r)
Definition: row.hxx:299
field_size_type size_type
Definition: field.hxx:44
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
const_row_iterator & operator++()
Definition: row.hxx:243
const_row_iterator(const row &T, row_size_type C) noexcept
Definition: row.hxx:226
row_difference_type difference_type
Definition: row.hxx:223
const_reverse_row_iterator(const super &rhs) noexcept
Definition: row.hxx:302
bool operator<(const const_row_iterator &i) const
Definition: row.hxx:261
oid column_table(int ColNum) const
What table did this column come from?
Definition: row.hxx:145
const_reverse_row_iterator & operator=(const const_reverse_row_iterator &r)
Definition: row.hxx:320
oid column_type(int ColNum) const
Type of given column.
Definition: row.hxx:130
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:328
reference operator*() const
Definition: row.hxx:235
const_row_iterator(const field &F) noexcept
Definition: row.hxx:228
Reference to a field in a result set.
Definition: field.hxx:41
size_t num() const
Definition: row.hxx:170
const_row_iterator operator+(difference_type) const
Definition: row.hxx:369
bool operator!=(const const_reverse_row_iterator &rhs) const noexcept
Definition: row.hxx:353
bool operator>=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:362
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:338
signed int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:249
pointer operator->() const
Definition: row.hxx:234