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
26namespace pqxx
27{
29
40class PQXX_LIBEXPORT row
41{
42public:
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
120 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
189protected:
190 friend class field;
192 result m_result;
194
198 long m_index = 0;
200 size_type m_begin = 0;
202 size_type m_end = 0;
203};
204
205
207class PQXX_LIBEXPORT const_row_iterator : public field
208{
209public:
210 using iterator_category = std::random_access_iterator_tag;
211 using value_type = const field;
212 using pointer = const 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]
269 difference_type,
271
272 inline const_row_iterator operator-(difference_type) const; //[t82]
273 inline difference_type operator-(const_row_iterator) const; //[t82]
275};
276
277
279class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
280{
281public:
289
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
310 const_reverse_row_iterator &
311 operator=(const const_reverse_row_iterator &r) //[t82]
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}; }
333 difference_type
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
359inline const_row_iterator
361{
362 return const_row_iterator{
363 row(home(), idx()),
364 size_type(difference_type(col()) + o)};
365}
366
369 { return i + o; }
370
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
389Effective C++", points out that it is good style to have any class containing
390a member of pointer type define a destructor--just to show that it knows what
391it is doing with the pointer. This helps prevent nasty memory leak / double
392deletion bugs typically resulting from programmers' omission to deal with such
393issues in their destructors.
394
395The @c -Weffc++ option in gcc generates warnings for noncompliance with Scott's
396style guidelines, and hence necessitates the definition of this destructor,
397trivial as it may be.
398*/
399
400
401#include "pqxx/compiler-internal-post.hxx"
402
403#endif
STL namespace.
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
const_result_iterator operator+(result::difference_type o, const_result_iterator i)
Definition: result_iterator.hxx:212
signed int row_difference_type
Difference between row sizes.
Definition: types.hxx:27
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
Reference to a field in a result set.
Definition: field.hxx:50
row_size_type num() const
Definition: field.hxx:104
field_size_type size_type
Definition: field.hxx:52
Result set containing data returned by a query or command.
Definition: result.hxx:70
Reference to one row in a result.
Definition: row.hxx:41
size_t rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:113
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 size_type
Definition: row.hxx:43
row_difference_type difference_type
Definition: row.hxx:44
row()=default
size_t num() const
Definition: row.hxx:170
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
size_type column_number(const std::string &ColName) const
Number of given column (throws exception if it doesn't exist)
Definition: row.hxx:120
~row() noexcept=default
oid column_table(int ColNum) const
What table did this column come from?
Definition: row.hxx:145
oid column_type(const char ColName[]) const
Type of given column.
Definition: row.hxx:138
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(int ColNum) const
Type of given column.
Definition: row.hxx:130
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:208
bool operator!=(const const_row_iterator &i) const
Definition: row.hxx:250
const_row_iterator & operator++()
Definition: row.hxx:234
const_row_iterator & operator--()
Definition: row.hxx:236
bool operator<=(const const_row_iterator &i) const
Definition: row.hxx:254
std::random_access_iterator_tag iterator_category
Definition: row.hxx:210
const_row_iterator & operator-=(difference_type i)
Definition: row.hxx:240
const_row_iterator operator-(difference_type) const
Definition: row.hxx:372
const_row_iterator operator+(difference_type) const
Definition: row.hxx:360
bool operator==(const const_row_iterator &i) const
Definition: row.hxx:248
bool operator>=(const const_row_iterator &i) const
Definition: row.hxx:258
reference operator*() const
Definition: row.hxx:226
const_row_iterator(const row &T, row_size_type C) noexcept
Definition: row.hxx:217
row_difference_type difference_type
Definition: row.hxx:214
pointer operator->() const
Definition: row.hxx:225
bool operator<(const const_row_iterator &i) const
Definition: row.hxx:252
const_row_iterator & operator+=(difference_type i)
Definition: row.hxx:238
bool operator>(const const_row_iterator &i) const
Definition: row.hxx:256
const_row_iterator(const field &F) noexcept
Definition: row.hxx:219
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:280
const_reverse_row_iterator & operator+=(difference_type i)
Definition: row.hxx:319
const_reverse_row_iterator(const const_reverse_row_iterator &r)
Definition: row.hxx:290
const_reverse_row_iterator & operator-=(difference_type i)
Definition: row.hxx:321
bool operator!=(const const_reverse_row_iterator &rhs) const noexcept
Definition: row.hxx:344
const_reverse_row_iterator operator+(difference_type i) const
Definition: row.hxx:329
bool operator<(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:347
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
bool operator>=(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:353
bool operator>(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:351
bool operator==(const const_reverse_row_iterator &rhs) const noexcept
Definition: row.hxx:342
const_reverse_row_iterator & operator--()
Definition: row.hxx:316
difference_type operator-(const const_reverse_row_iterator &rhs) const
Definition: row.hxx:334
const_reverse_row_iterator operator++()
Definition: row.hxx:313
const_reverse_row_iterator(const super &rhs) noexcept
Definition: row.hxx:293