libpqxx  5.0
row.hxx
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * pqxx/result.hxx
5  *
6  * DESCRIPTION
7  * definitions for the pqxx::result class and support classes.
8  * pqxx::result represents the set of result rows from a database query
9  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result instead.
10  *
11  * Copyright (c) 2001-2016, Jeroen T. Vermeulen <jtv@xs4all.nl>
12  *
13  * See COPYING for copyright license. If you did not receive a file called
14  * COPYING with this source code, please notify the distributor of this mistake,
15  * or contact the author.
16  *
17  *-------------------------------------------------------------------------
18  */
19 #ifndef PQXX_H_ROW
20 #define PQXX_H_ROW
21 
22 #include "pqxx/compiler-public.hxx"
23 #include "pqxx/compiler-internal-pre.hxx"
24 
25 #include "pqxx/except"
26 #include "pqxx/field"
27 
28 
29 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
30  */
31 
32 namespace pqxx
33 {
34 class const_row_iterator;
35 class const_reverse_row_iterator;
36 class result;
37 class range_error;
38 
39 
41 
52 class PQXX_LIBEXPORT row
53 {
54 public:
59  typedef field reference;
63 
65  row(const result *r, size_t i) PQXX_NOEXCEPT;
66 
67  ~row() PQXX_NOEXCEPT {} // Yes Scott Meyers, you're absolutely right[1]
68 
73  PQXX_PURE bool operator==(const row &) const PQXX_NOEXCEPT; //[t75]
74  bool operator!=(const row &rhs) const PQXX_NOEXCEPT //[t75]
75  { return !operator==(rhs); }
77 
78  const_iterator begin() const PQXX_NOEXCEPT; //[t82]
79  const_iterator cbegin() const PQXX_NOEXCEPT;
80  const_iterator end() const PQXX_NOEXCEPT; //[t82]
81  const_iterator cend() const PQXX_NOEXCEPT;
82 
87  reference front() const PQXX_NOEXCEPT; //[t74]
88  reference back() const PQXX_NOEXCEPT; //[t75]
89 
90  const_reverse_row_iterator rbegin() const; //[t82]
91  const_reverse_row_iterator crbegin() const;
92  const_reverse_row_iterator rend() const; //[t82]
93  const_reverse_row_iterator crend() const;
94 
95  reference operator[](size_type) const PQXX_NOEXCEPT; //[t11]
96  reference operator[](int) const PQXX_NOEXCEPT; //[t2]
100  reference operator[](const char[]) const; //[t11]
104  reference operator[](const std::string &) const; //[t11]
105  reference at(size_type) const; //[t11]
106  reference at(int) const; //[t11]
110  reference at(const char[]) const; //[t11]
114  reference at(const std::string &) const; //[t11]
116 
117  size_type size() const PQXX_NOEXCEPT //[t11]
118  { return m_End-m_Begin; }
119 
120  void swap(row &) PQXX_NOEXCEPT; //[t11]
121 
122  size_t rownumber() const PQXX_NOEXCEPT { return m_Index; } //[t11]
123 
128  size_type column_number(const std::string &ColName) const //[t30]
130  { return column_number(ColName.c_str()); }
131 
133  size_type column_number(const char[]) const; //[t30]
134 
136  oid column_type(size_type) const; //[t7]
137 
139  oid column_type(int ColNum) const //[t7]
140  { return column_type(size_type(ColNum)); }
141 
143  oid column_type(const std::string &ColName) const //[t7]
144  { return column_type(column_number(ColName)); }
145 
147  oid column_type(const char ColName[]) const //[t7]
148  { return column_type(column_number(ColName)); }
149 
151  oid column_table(size_type ColNum) const; //[t2]
152 
154  oid column_table(int ColNum) const //[t2]
155  { return column_table(size_type(ColNum)); }
157  oid column_table(const std::string &ColName) const //[t2]
158  { return column_table(column_number(ColName)); }
159 
161 
168  size_type table_column(size_type) const; //[t93]
169 
171  size_type table_column(int ColNum) const //[t93]
172  { return table_column(size_type(ColNum)); }
173 
175  size_type table_column(const std::string &ColName) const //[t93]
176  { return table_column(column_number(ColName)); }
178 
179  size_t num() const { return rownumber(); } //[t1]
180 
193  row slice(size_type Begin, size_type End) const;
194 
195  // Is this an empty slice?
196  PQXX_PURE bool empty() const PQXX_NOEXCEPT;
197 
198 protected:
199  friend class field;
200  const result *m_Home;
201  size_t m_Index;
202  size_type m_Begin;
203  size_type m_End;
204 
205 private:
206  // Not allowed:
207  row() PQXX_DELETED_OP;
208 };
209 
210 
212 class PQXX_LIBEXPORT const_row_iterator :
213  public std::iterator<std::random_access_iterator_tag,
214  const field,
215  row::size_type>,
216  public field
217 {
218  typedef std::iterator<std::random_access_iterator_tag,
219  const field,
220  row::size_type> it;
221 public:
222  using it::pointer;
225  typedef field reference;
226 
227  const_row_iterator(const row &T, row::size_type C) //[t82]
228  PQXX_NOEXCEPT :
229  field(T, C) {}
230  const_row_iterator(const field &F) PQXX_NOEXCEPT : field(F) {} //[t82]
231 
236  pointer operator->() const { return this; } //[t82]
237  reference operator*() const { return field(*this); } //[t82]
239 
244  const_row_iterator operator++(int); //[t82]
245  const_row_iterator &operator++() { ++m_col; return *this; } //[t82]
246  const_row_iterator operator--(int); //[t82]
247  const_row_iterator &operator--() { --m_col; return *this; } //[t82]
248 
249  const_row_iterator &operator+=(difference_type i) //[t82]
250  { m_col = size_type(difference_type(m_col) + i); return *this; }
251  const_row_iterator &operator-=(difference_type i) //[t82]
252  { m_col = size_type(difference_type(m_col) - i); return *this; }
254 
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();}
269  bool operator>=(const const_row_iterator &i) const //[t82]
270  {return col()>=i.col();}
272 
277  inline const_row_iterator operator+(difference_type) const; //[t82]
278 
279  friend const_row_iterator operator+( //[t82]
280  difference_type,
282 
283  inline const_row_iterator operator-(difference_type) const; //[t82]
284  inline difference_type operator-(const_row_iterator) const; //[t82]
286 };
287 
288 
290 class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
291 {
292 public:
295  using iterator_type::iterator_category;
297  using iterator_type::pointer;
298 #ifndef _MSC_VER
299  using iterator_type::value_type;
301 #else
302  // Workaround for Visual C++.NET 2003, which has access problems
303  typedef field value_type;
304  typedef const field &reference;
305 #endif
306 
308  const_row_iterator(r) {}
309  explicit
310  const_reverse_row_iterator(const super &rhs) PQXX_NOEXCEPT : //[t82]
311  const_row_iterator(rhs) { super::operator--(); }
312 
313  PQXX_PURE iterator_type base() const PQXX_NOEXCEPT; //[t82]
314 
319  using iterator_type::operator->; //[t82]
320  using iterator_type::operator*; //[t82]
322 
328  operator=(const const_reverse_row_iterator &r) //[t82]
329  { iterator_type::operator=(r); return *this; }
331  { iterator_type::operator--(); return *this; }
332  const_reverse_row_iterator operator++(int); //[t82]
334  { iterator_type::operator++(); return *this; }
335  const_reverse_row_iterator operator--(int); //[t82]
337  { iterator_type::operator-=(i); return *this; }
339  { iterator_type::operator+=(i); return *this; }
341 
347  { return const_reverse_row_iterator(base()-i); }
349  { return const_reverse_row_iterator(base()+i); }
350  difference_type
351  operator-(const const_reverse_row_iterator &rhs) const //[t82]
352  { return rhs.const_row_iterator::operator-(*this); }
354 
359  bool operator==(const const_reverse_row_iterator &rhs) //[t82]
360  const PQXX_NOEXCEPT
361  { return iterator_type::operator==(rhs); }
362  bool operator!=(const const_reverse_row_iterator &rhs) //[t82]
363  const PQXX_NOEXCEPT
364  { return !operator==(rhs); }
365 
366  bool operator<(const const_reverse_row_iterator &rhs) const //[t82]
367  { return iterator_type::operator>(rhs); }
368  bool operator<=(const const_reverse_row_iterator &rhs) const //[t82]
369  { return iterator_type::operator>=(rhs); }
370  bool operator>(const const_reverse_row_iterator &rhs) const //[t82]
371  { return iterator_type::operator<(rhs); }
372  bool operator>=(const const_reverse_row_iterator &rhs) const //[t82]
373  { return iterator_type::operator<=(rhs); }
375 };
376 
377 
378 inline const_row_iterator
380 {
381  return const_row_iterator(
382  row(home(), idx()),
383  size_type(difference_type(col()) + o));
384 }
385 
386 inline const_row_iterator
388  { return i + o; }
389 
390 inline const_row_iterator
392 {
393  return const_row_iterator(
394  row(home(), idx()),
395  size_type(difference_type(col()) - o));
396 }
397 
400  { return difference_type(num() - i.num()); }
401 
402 
403 } // namespace pqxx
404 
405 
406 /*
407 [1] Scott Meyers, in one of his essential books, "Effective C++" and "More
408 Effective C++", points out that it is good style to have any class containing
409 a member of pointer type define a destructor--just to show that it knows what it
410 is doing with the pointer. This helps prevent nasty memory leak / double
411 deletion bugs typically resulting from programmers' omission to deal with such
412 issues in their destructors.
413 
414 The @c -Weffc++ option in gcc generates warnings for noncompliance with Scott's
415 style guidelines, and hence necessitates the definition of this destructor,
416 trivial as it may be.
417 */
418 
419 
420 #include "pqxx/compiler-internal-post.hxx"
421 
422 #endif
row_size_type size_type
Definition: row.hxx:55
unsigned int row_size_type
Definition: field.hxx:42
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:251
const_reverse_row_iterator const_reverse_iterator
Definition: row.hxx:61
bool operator!=(const const_reverse_row_iterator &rhs) const PQXX_NOEXCEPT
Definition: row.hxx:362
row_difference_type difference_type
Definition: row.hxx:56
difference_type operator-(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:351
bool operator==(const const_row_iterator &i) const
Definition: row.hxx:259
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:249
bool operator>(const const_row_iterator &i) const
Definition: row.hxx:267
reference operator*() const
Definition: row.hxx:237
bool operator<(const const_row_iterator &i) const
Definition: row.hxx:263
oid column_table(int ColNum) const
What table did this column come from?
Definition: row.hxx:154
const_row_iterator & operator--()
Definition: row.hxx:247
~row() PQXX_NOEXCEPT
Definition: row.hxx:67
const_row_iterator super
Definition: row.hxx:293
const_result_iterator operator+(result::difference_type o, const_result_iterator i)
Definition: result.hxx:476
bool operator==(const const_reverse_row_iterator &rhs) const PQXX_NOEXCEPT
Definition: row.hxx:359
Reference to one row in a result.
Definition: row.hxx:52
size_type table_column(const std::string &ColName) const
What column number in its table did this result column come from?
Definition: row.hxx:175
bool operator<=(const const_row_iterator &i) const
Definition: row.hxx:265
Reference to a field in a result set.
Definition: field.hxx:51
oid column_type(const std::string &ColName) const
Type of given column.
Definition: row.hxx:143
bool operator!=(const const_row_iterator &i) const
Definition: row.hxx:261
const_reverse_iterator reverse_iterator
Definition: row.hxx:62
const_iterator iterator
Definition: row.hxx:58
row::size_type size_type
Definition: row.hxx:223
const_row_iterator & operator++()
Definition: row.hxx:245
const_row_iterator operator+(difference_type) const
Definition: row.hxx:379
bool operator>(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:370
const_row_iterator const_iterator
Definition: row.hxx:57
bool operator>=(const const_row_iterator &i) const
Definition: row.hxx:269
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: row.hxx:157
const_reverse_row_iterator operator-(difference_type i)
Definition: row.hxx:348
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:212
const_row_iterator(const row &T, row::size_type C) PQXX_NOEXCEPT
Definition: row.hxx:227
bool operator<=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:368
oid column_type(const char ColName[]) const
Type of given column.
Definition: row.hxx:147
const_row_iterator pointer
Definition: row.hxx:60
const_row_iterator operator-(difference_type) const
Definition: row.hxx:391
const_reverse_row_iterator & operator--()
Definition: row.hxx:333
bool operator<(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:366
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:336
bool operator>=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:372
const_reverse_row_iterator operator++()
Definition: row.hxx:330
const_reverse_row_iterator(const const_reverse_row_iterator &r)
Definition: row.hxx:307
size_t num() const
Definition: row.hxx:179
signed int row_difference_type
Definition: field.hxx:45
const_row_iterator iterator_type
Definition: row.hxx:294
const_reverse_row_iterator(const super &rhs) PQXX_NOEXCEPT
Definition: row.hxx:310
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:338
size_type table_column(int ColNum) const
What column number in its table did this result column come from?
Definition: row.hxx:171
row::difference_type difference_type
Definition: row.hxx:224
size_t size_type
Definition: field.hxx:54
pointer operator->() const
Definition: row.hxx:236
field reference
Definition: row.hxx:225
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:290
const_row_iterator(const field &F) PQXX_NOEXCEPT
Definition: row.hxx:230
Result set containing data returned by a query or command.
Definition: result.hxx:78
row_size_type num() const
Definition: field.hxx:106
field reference
Definition: row.hxx:59
oid column_type(int ColNum) const
Type of given column.
Definition: row.hxx:139
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:346