Result.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_RESULT_H
7#define PT_DB_RESULT_H
8
9#include <Pt/Db/Api.h>
10#include <Pt/SmartPtr.h>
11#include <Pt/Db/IResult.h>
12#include <Pt/Db/Row.h>
13
14namespace Pt {
15
16namespace Db {
17
18 class Row;
19 class Value;
20
41 class PT_DB_API Result
42 {
43 public:
44 class ConstIterator;
45
46 typedef std::size_t size_type;
47
48 typedef Row value_type;
49
50 private:
52
53 public:
56 Result() { }
57
61 : _result(res)
62 { }
63
66 Row getRow(size_type row_num) const;
67
70 Value getValue(size_type row_num, size_type field_num) const;
71
74 size_type size() const { return _result->size(); }
75
78 bool empty() const { return size() == 0; }
79
82 size_type getFieldCount() const { return _result->getFieldCount(); }
83
86 Row operator[] (size_type row_num) const;
87
91
95
98 bool operator!() const { return !_result; }
99
102 const IResult* getImpl() const { return &*_result; }
103 };
104
108 {
109 public:
110 typedef std::random_access_iterator_tag iterator_category;
111 typedef Row value_type;
112 typedef std::ptrdiff_t difference_type;
113 typedef const Row* pointer;
114 typedef const Row& reference;
115
116 typedef const value_type& const_reference;
117 typedef const value_type* const_pointer;
118
119 private:
120 Result _result;
121 Row _current;
122 size_type _offset;
123
124 void setOffset(size_type off)
125 {
126 if (off != _offset)
127 {
128 _offset = off;
129 // is range checking needed here ???
130 if (_offset < _result.size())
131 _current = _result.getRow(_offset);
132 }
133 }
134
135 public:
136
137 ConstIterator(const Result& r, size_type off)
138 : _result(r)
139 , _offset(off)
140 {
141 if (_offset < r.size())
142 _current = r.getRow(_offset);
143 }
144
145 bool operator== (const ConstIterator& it) const
146 { return _offset == it._offset; }
147
148 bool operator!= (const ConstIterator& it) const
149 { return !operator== (it); }
150
151 ConstIterator& operator++()
152 { setOffset(_offset + 1); return *this; }
153
154 ConstIterator operator++(int)
155 {
156 ConstIterator ret = *this;
157 setOffset(_offset + 1);
158 return ret;
159 }
160
161 ConstIterator operator--()
162 { setOffset(_offset - 1); return *this; }
163
164 ConstIterator operator--(int)
165 {
166 ConstIterator ret = *this;
167 setOffset(_offset - 1);
168 return ret;
169 }
170
171 const_reference operator*() const
172 { return _current; }
173
174 const_pointer operator->() const
175 { return &_current; }
176
177 ConstIterator& operator+= (difference_type n)
178 {
179 setOffset(_offset + n);
180 return *this;
181 }
182
183 ConstIterator operator+ (difference_type n) const
184 {
185 ConstIterator it(*this);
186 it += n;
187 return it;
188 }
189
190 ConstIterator& operator-= (difference_type n)
191 {
192 setOffset(_offset - n);
193 return *this;
194 }
195
196 ConstIterator operator- (difference_type n) const
197 {
198 ConstIterator it(*this);
199 it -= n;
200 return it;
201 }
202
203 difference_type operator- (const ConstIterator& it) const
204 { return _offset - it._offset; }
205 };
206
207} // namespace Db
208
209} // namespace Pt
210
211#endif // PTV_DB_RESULT_H
Buffered query-result backend.
Definition IResult.h:24
Random-access iterator over the rows of a Result.
Definition Result.h:108
size_type getFieldCount() const
Returns the number of columns.
Definition Result.h:82
Row getRow(size_type row_num) const
Returns the row at row_num without range checking.
size_type size() const
Returns the number of rows.
Definition Result.h:74
bool operator!() const
Returns true if this object is not bound to a result.
Definition Result.h:98
bool empty() const
Returns true if this result has no rows.
Definition Result.h:78
ConstIterator end() const
Returns a random-access iterator past the last row.
Result()
Creates an unbound result.
Definition Result.h:56
ConstIterator begin() const
Returns a random-access iterator to the first row.
Result(IResult *res)
Takes ownership of the backend res.
Definition Result.h:60
Value getValue(size_type row_num, size_type field_num) const
Returns the value at row_num, field_num without range checking.
const IResult * getImpl() const
Returns the backend implementation.
Definition Result.h:102
One row of a buffered query result.
Definition Row.h:34
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