ThinSQLite++
A thin, safe and convenient modern C++ wrapper for SQLite API.
Loading...
Searching...
No Matches
row_iterator.hpp
1/*
2 Copyright 2019 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/main/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_ROW_ITERATOR_INCLUDED
10#define HEADER_SQLITEPP_ROW_ITERATOR_INCLUDED
11
12#include "statement_iface.hpp"
13
14namespace thinsqlitepp
15{
33 class cell
34 {
35 public:
39 cell(const statement * owner, int idx) noexcept:
40 _owner(owner), _idx(idx)
41 {}
43 cell(const std::unique_ptr<statement> & owner, int idx) noexcept:
44 cell(owner.get(), idx)
45 {}
46
55 int type() const noexcept
56 { return _owner->column_type(_idx); }
69 const char * name() const noexcept
70 { return _owner->column_name(_idx); }
83 template<class T> T value() const noexcept
84 { return _owner->column_value<T>(_idx); }
85
93 const char * database_name() const noexcept
94 { return _owner->column_database_name(_idx); }
102 const char * table_name() const noexcept
103 { return _owner->column_table_name(_idx); }
104
112 const char * origin_name() const noexcept
113 { return _owner->column_origin_name(_idx); }
114
122 const char * declared_type() const noexcept
123 { return _owner->column_declared_type(_idx); }
124
125 protected:
126 const statement * _owner;
127 int _idx;
128 };
129
142 class row
143 {
144 public:
145 using value_type = cell;
146 using size_type = int;
147 using difference_type = int;
148 using reference = value_type;
149 using pointer = void;
150
151 class const_iterator : private cell
152 {
153 friend class row;
154 public:
156 using size_type = row::size_type;
157 using difference_type = row::difference_type;
159 using pointer = row::pointer;
160 using iterator_category = std::random_access_iterator_tag;
161 public:
162 const_iterator() noexcept:
163 cell(nullptr, -1)
164 {}
165
166 cell operator*() const noexcept
167 { return *this; }
168 const cell * operator->() const noexcept
169 { return this; }
170 cell operator[](size_type idx) const noexcept
171 { return *(*this + idx); }
172
173 const_iterator & operator++() noexcept
174 { ++_idx; return *this; }
175 const_iterator operator++(int) noexcept
176 { return const_iterator(_owner, _idx++); }
177 const_iterator & operator+=(int val) noexcept
178 { _idx += val; return *this; }
179 const_iterator & operator--() noexcept
180 { --_idx; return *this; }
181 const_iterator operator--(int) noexcept
182 { return const_iterator(_owner, _idx--); }
183 const_iterator & operator-=(int val) noexcept
184 { _idx -= val; return *this; }
185
186 friend const_iterator operator+(const const_iterator & lhs, int rhs) noexcept
187 { return const_iterator(lhs._owner, lhs._idx + rhs); }
188 friend const_iterator operator+(int lhs, const const_iterator & rhs) noexcept
189 { return const_iterator(rhs._owner, rhs._idx + lhs); }
190
191 friend int operator-(const const_iterator & lhs, const const_iterator & rhs) noexcept
192 { return lhs._idx - rhs._idx; }
193 friend const_iterator operator-(const const_iterator & lhs, int rhs) noexcept
194 { return const_iterator(lhs._owner, lhs._idx - rhs); }
195
196 friend bool operator==(const const_iterator & lhs, const const_iterator & rhs) noexcept
197 { return lhs._idx == rhs._idx; }
198 friend bool operator!=(const const_iterator & lhs, const const_iterator & rhs) noexcept
199 { return lhs._idx != rhs._idx; }
200 friend bool operator<(const const_iterator & lhs, const const_iterator & rhs) noexcept
201 { return lhs._idx < rhs._idx; }
202 friend bool operator<=(const const_iterator & lhs, const const_iterator & rhs) noexcept
203 { return lhs._idx <= rhs._idx; }
204 friend bool operator>(const const_iterator & lhs, const const_iterator & rhs) noexcept
205 { return lhs._idx > rhs._idx; }
206 friend bool operator>=(const const_iterator & lhs, const const_iterator & rhs) noexcept
207 { return lhs._idx >= rhs._idx; }
208 private:
209 const_iterator(const statement * owner, int idx) noexcept:
210 cell(owner, idx)
211 {}
212 };
213 using iterator = const_iterator;
216 public:
223 row(const statement * owner) noexcept:
224 _owner(owner)
225 {}
227 row(const std::unique_ptr<statement> & owner) noexcept:
228 row(owner.get())
229 {}
230
231 int size() const noexcept
232 { return _owner->data_count(); }
233 bool empty() const noexcept
234 { return size() == 0; }
235
236 cell operator[](int idx) const noexcept
237 { return cell(_owner, idx); }
238
239 const_iterator begin() const noexcept
240 { return const_iterator(_owner, 0); }
241 const_iterator cbegin() const noexcept
242 { return const_iterator(_owner, 0); }
243 const_iterator end() const noexcept
244 { return const_iterator(_owner, size()); }
245 const_iterator cend() const noexcept
246 { return const_iterator(_owner, size()); }
247
248 const_reverse_iterator rbegin() const noexcept
249 { return const_reverse_iterator(end()); }
250 const_reverse_iterator crbegin() const noexcept
251 { return const_reverse_iterator(end()); }
252 const_reverse_iterator rend() const noexcept
253 { return const_reverse_iterator(begin()); }
254 const_reverse_iterator crend() const noexcept
255 { return const_reverse_iterator(begin()); }
256 protected:
257 const statement * _owner;
258 };
259
269 class row_iterator : private row
270 {
271 public:
272 using value_type = row;
273 using size_type = int;
274 using difference_type = int;
275 using reference = row;
276 using pointer = void;
278
279 public:
285 row_iterator() noexcept:
286 row(nullptr)
287 {}
295 row(owner)
296 {
297 if (_owner)
298 increment();
299 }
300
303 row_iterator(owner.get())
304 {}
305
306 row operator*() const noexcept
307 { return *this; }
308
309 const row * operator->() const noexcept
310 { return this; }
311
312 row_iterator & operator++()
313 { increment(); return *this; }
314 row_iterator operator++(int)
315 { increment(); return *this; }
316
317 friend bool operator==(const row_iterator & lhs, const row_iterator & rhs) noexcept
318 { return lhs._owner == rhs._owner; }
319 friend bool operator!=(const row_iterator & lhs, const row_iterator & rhs) noexcept
320 { return lhs._owner != rhs._owner; }
321 private:
322 void increment()
323 {
324 if (!const_cast<statement *>(_owner)->step())
325 _owner = nullptr;
326 }
327 };
328
338 class row_range {
339 public:
340 using value_type = row;
341 using size_type = int;
342 using difference_type = int;
343 using reference = value_type;
344 using pointer = void;
345
347 using iterator = const_iterator;
348 public:
356 _it(owner)
357 {}
358
361 row_range(owner.get())
362 {}
363
364 const_iterator begin() const noexcept
365 { return _it; }
366 const_iterator cbegin() const noexcept
367 { return _it; }
368 const_iterator end() const noexcept
369 { return const_iterator(); }
370 const_iterator cend() const noexcept
371 { return const_iterator(); }
372
373 private:
374 row_iterator _it;
375 };
376
378}
379
380#endif
A cell in a row.
Definition row_iterator.hpp:34
const char * origin_name() const noexcept
Table column that is the origin of the cell.
Definition row_iterator.hpp:112
const char * name() const noexcept
Name of the cell's column.
Definition row_iterator.hpp:69
cell(const statement *owner, int idx) noexcept
Construct a cell for a given statement and column index.
Definition row_iterator.hpp:39
const char * database_name() const noexcept
Database that is the origin of the cell.
Definition row_iterator.hpp:93
const char * table_name() const noexcept
Table that is the origin of the cell.
Definition row_iterator.hpp:102
const char * declared_type() const noexcept
Declared datatype of the cell.
Definition row_iterator.hpp:122
T value() const noexcept
Cell's value.
Definition row_iterator.hpp:83
int type() const noexcept
Default datatype of the cell.
Definition row_iterator.hpp:55
cell(const std::unique_ptr< statement > &owner, int idx) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition row_iterator.hpp:43
A forward iterator for statement results.
Definition row_iterator.hpp:270
row_iterator() noexcept
Create an empty iterator.
Definition row_iterator.hpp:285
row_iterator(std::unique_ptr< statement > &owner)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition row_iterator.hpp:302
row_iterator(statement *owner)
Create an instance referring to a given statement.
Definition row_iterator.hpp:294
A forward range for statement results.
Definition row_iterator.hpp:338
row_range(std::unique_ptr< statement > &owner)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition row_iterator.hpp:360
row_range(statement *owner)
Create an instance referring to a given statement.
Definition row_iterator.hpp:355
Row result of a statement.
Definition row_iterator.hpp:143
row(const statement *owner) noexcept
Construct a row for a given statement.
Definition row_iterator.hpp:223
row(const std::unique_ptr< statement > &owner) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition row_iterator.hpp:227
Prepared Statement Object.
Definition statement_iface.hpp:41
int column_type(int idx) const noexcept
Default datatype of the result column.
Definition statement_iface.hpp:483
const char * column_table_name(int idx) const noexcept
Table that is the origin of a result column.
Definition statement_iface.hpp:512
const char * column_origin_name(int idx) const noexcept
Table column that is the origin of a result column.
Definition statement_iface.hpp:520
const char * column_database_name(int idx) const noexcept
Database that is the origin of a result column.
Definition statement_iface.hpp:504
const char * column_name(int idx) const noexcept
Name of the result column.
Definition statement_iface.hpp:496
T column_value(int idx) const noexcept
Get result value from a query.
const char * column_declared_type(int idx) const noexcept
Declared datatype of a result column.
Definition statement_iface.hpp:528
int data_count() const noexcept
Number of columns in a result set.
Definition statement_iface.hpp:427
ThinSQLite++ namespace.
Definition backup_iface.hpp:17