Row.h
1/* Copyright (C) 2006-2026 by Tommi Maekitalo
2 Copyright (C) 2006-2026 by Marc Boris Duerner
3 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
4*/
5
6#ifndef PT_DB_ROW_H
7#define PT_DB_ROW_H
8
9#include <Pt/Db/Api.h>
10#include <Pt/Db/IRow.h>
11#include <Pt/Db/Value.h>
12#include <Pt/SmartPtr.h>
13
14namespace Pt {
15
16namespace Db {
17
33 class PT_DB_API Row
34 {
35 public:
36 class ConstIterator;
37
38 typedef std::size_t size_type;
39
40 typedef Value value_type;
41
42 private:
43
45
46 public:
47
50 Row() { }
51
54 Row(IRow* row_)
55 : row(row_)
56 {
57 }
58
61 size_type size() const { return row->size(); }
62
65 bool empty() const { return !row || size() == 0; }
66
69 Value getValue(size_type field_num) const
70 { return row->getValue(field_num); }
71
74 Value operator[] (size_type field_num) const
75 { return row->getValue(field_num); }
76
79 bool isNull(size_type field_num) const
80 { return getValue(field_num).isNull(); }
81
86 bool getBool(size_type field_num) const
87 { return getValue(field_num).getBool(); }
88
93 int getInt(size_type field_num) const
94 { return getValue(field_num).getInt(); }
95
100 unsigned getUnsigned(size_type field_num) const
101 { return getValue(field_num).getUnsigned(); }
102
107 float getFloat(size_type field_num) const
108 { return getValue(field_num).getFloat(); }
109
114 double getDouble(size_type field_num) const
115 { return getValue(field_num).getDouble(); }
116
123 char getChar(size_type field_num) const
124 { return getValue(field_num).getChar(); }
125
130 std::string getString(size_type field_num) const
131 { return getValue(field_num).getString(); }
132
136
140
143 bool operator!() const { return !row; }
144
147 const IRow* getImpl() const { return &*row; }
148 };
149
150
154 {
155 public:
156 typedef std::random_access_iterator_tag iterator_category;
157 typedef Value value_type;
158 typedef std::ptrdiff_t difference_type;
159 typedef const Value* pointer;
160 typedef const Value& reference;
161
162 typedef const value_type& const_reference;
163
164 typedef const value_type* const_pointer;
165
166 private:
167 const Row& _row;
168 size_type _offset;
169 Value _current;
170
171 void setOffset(size_type off)
172 {
173 if (off != _offset)
174 {
175 _offset = off;
176 // is range checking needed here ???
177 if (_offset < _row.size())
178 _current = _row.getValue(_offset);
179 }
180 }
181
182 public:
183
184 ConstIterator(const Row& row, size_type offset)
185 : _row(row)
186 , _offset(offset)
187 {
188 // is range checking needed here ???
189 if (_offset < row.size())
190 _current = row.getValue(_offset);
191 }
192
193 bool operator== (const ConstIterator& it) const
194 { return _offset == it._offset; }
195
196 bool operator!= (const ConstIterator& it) const
197 { return !operator== (it); }
198
199 ConstIterator& operator++()
200 { this->setOffset(_offset + 1); return *this; }
201
202 ConstIterator operator++(int)
203 {
204 ConstIterator ret = *this;
205 this->setOffset(_offset + 1);
206 return ret;
207 }
208
209 ConstIterator operator--()
210 { setOffset(_offset - 1); return *this; }
211
212 ConstIterator operator--(int)
213 {
214 ConstIterator ret = *this;
215 this->setOffset(_offset - 1);
216 return ret;
217 }
218
219 const_reference operator*() const
220 { return _current; }
221
222 const_pointer operator->() const
223 { return &_current; }
224
225 ConstIterator& operator+= (difference_type n)
226 {
227 this->setOffset(_offset + n);
228 return *this;
229 }
230
231 ConstIterator operator+ (difference_type n) const
232 {
233 ConstIterator it(*this);
234 it += n;
235 return it;
236 }
237
238 ConstIterator& operator-= (difference_type n)
239 {
240 this->setOffset(_offset - n);
241 return *this;
242 }
243
244 ConstIterator operator- (difference_type n) const
245 {
246 ConstIterator it(*this);
247 it -= n;
248 return it;
249 }
250
251 difference_type operator- (const ConstIterator& it) const
252 { return _offset - it._offset; }
253 };
254
255} // namespace Db
256
257} // namespace Pt
258
259#endif // PTV_DB_ROW_H
Database-row backend.
Definition IRow.h:22
Random-access iterator over the values of a Row.
Definition Row.h:154
bool isNull(size_type field_num) const
Returns true if the value at field_num is SQL NULL.
Definition Row.h:79
float getFloat(size_type field_num) const
Returns the value at field_num as float.
Definition Row.h:107
std::string getString(size_type field_num) const
Returns the value at field_num as string.
Definition Row.h:130
double getDouble(size_type field_num) const
Returns the value at field_num as double.
Definition Row.h:114
size_type size() const
Returns the number of columns.
Definition Row.h:61
bool operator!() const
Returns true if this object is not bound to a row.
Definition Row.h:143
bool empty() const
Returns true if this row has no columns.
Definition Row.h:65
Row(IRow *row_)
Takes ownership of the backend row_.
Definition Row.h:54
ConstIterator end() const
Returns a random-access iterator past the last column.
const IRow * getImpl() const
Returns the backend implementation.
Definition Row.h:147
int getInt(size_type field_num) const
Returns the value at field_num as int.
Definition Row.h:93
bool getBool(size_type field_num) const
Returns the value at field_num as bool.
Definition Row.h:86
char getChar(size_type field_num) const
Returns the value at field_num as char.
Definition Row.h:123
Row()
Creates an unbound row.
Definition Row.h:50
ConstIterator begin() const
Returns a random-access iterator to the first column.
unsigned getUnsigned(size_type field_num) const
Returns the value at field_num as unsigned.
Definition Row.h:100
Value getValue(size_type field_num) const
Returns the value at field_num without range checking.
Definition Row.h:69
One column value of a query result.
Definition Value.h:35
Policy based smart pointer.
Definition SmartPtr.h:462
Portable SQL database access.
Core module.
Definition Allocator.h:33