ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the 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/master/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{
20
21
33 SQLITEPP_EXPORTED
34 class cell
35 {
36 public:
40 cell(const statement * owner, int idx) noexcept:
41 _owner(owner), _idx(idx)
42 {}
43
44 cell(const std::unique_ptr<statement> & owner, int idx) noexcept:
45 cell(owner.get(), idx)
46 {}
47
56 int type() const noexcept
57 { return _owner->column_type(_idx); }
58
70 const char * name() const noexcept
71 { return _owner->column_name(_idx); }
72
84 template<class T> T value() const noexcept
85 { return _owner->column_value<T>(_idx); }
86
94 const char * database_name() const noexcept
95 { return _owner->column_database_name(_idx); }
96
103 const char * table_name() const noexcept
104 { return _owner->column_table_name(_idx); }
105
113 const char * origin_name() const noexcept
114 { return _owner->column_origin_name(_idx); }
115
123 const char * declared_type() const noexcept
124 { return _owner->column_declared_type(_idx); }
125
126 protected:
127 const statement * _owner;
128 int _idx;
129 };
130
143 SQLITEPP_EXPORTED
144 class row
145 {
146 public:
147 using value_type = cell;
148 using size_type = int;
149 using difference_type = int;
150 using reference = value_type;
151 using pointer = void;
152
153 class const_iterator : private cell
154 {
155 friend class row;
156 public:
157 using value_type = row::value_type;
158 using size_type = row::size_type;
159 using difference_type = row::difference_type;
160 using reference = row::reference;
161 using pointer = row::pointer;
162 using iterator_category = std::random_access_iterator_tag;
163 public:
164 const_iterator() noexcept:
165 cell(nullptr, -1)
166 {}
167
168 cell operator*() const noexcept
169 { return *this; }
170 const cell * operator->() const noexcept
171 { return this; }
172 cell operator[](size_type idx) const noexcept
173 { return *(*this + idx); }
174
175 const_iterator & operator++() noexcept
176 { ++_idx; return *this; }
177 const_iterator operator++(int) noexcept
178 { return const_iterator(_owner, _idx++); }
179 const_iterator & operator+=(int val) noexcept
180 { _idx += val; return *this; }
181 const_iterator & operator--() noexcept
182 { --_idx; return *this; }
183 const_iterator operator--(int) noexcept
184 { return const_iterator(_owner, _idx--); }
185 const_iterator & operator-=(int val) noexcept
186 { _idx -= val; return *this; }
187
188 friend const_iterator operator+(const const_iterator & lhs, int rhs) noexcept
189 { return const_iterator(lhs._owner, lhs._idx + rhs); }
190 friend const_iterator operator+(int lhs, const const_iterator & rhs) noexcept
191 { return const_iterator(rhs._owner, rhs._idx + lhs); }
192
193 friend int operator-(const const_iterator & lhs, const const_iterator & rhs) noexcept
194 { return lhs._idx - rhs._idx; }
195 friend const_iterator operator-(const const_iterator & lhs, int rhs) noexcept
196 { return const_iterator(lhs._owner, lhs._idx - rhs); }
197
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 friend bool operator>=(const const_iterator & lhs, const const_iterator & rhs) noexcept
209 { return lhs._idx >= rhs._idx; }
210 private:
211 const_iterator(const statement * owner, int idx) noexcept:
212 cell(owner, idx)
213 {}
214 };
215 using iterator = const_iterator;
216 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
217 using reverse_iterator = std::reverse_iterator<iterator>;
218 public:
225 row(const statement * owner) noexcept:
226 _owner(owner)
227 {}
228
229 row(const std::unique_ptr<statement> & owner) noexcept:
230 row(owner.get())
231 {}
232
233 int size() const noexcept
234 { return _owner->data_count(); }
235 bool empty() const noexcept
236 { return size() == 0; }
237
238 cell operator[](int idx) const noexcept
239 { return cell(_owner, idx); }
240
241 const_iterator begin() const noexcept
242 { return const_iterator(_owner, 0); }
243 const_iterator cbegin() const noexcept
244 { return const_iterator(_owner, 0); }
245 const_iterator end() const noexcept
246 { return const_iterator(_owner, size()); }
247 const_iterator cend() const noexcept
248 { return const_iterator(_owner, size()); }
249
250 const_reverse_iterator rbegin() const noexcept
251 { return const_reverse_iterator(end()); }
252 const_reverse_iterator crbegin() const noexcept
253 { return const_reverse_iterator(end()); }
254 const_reverse_iterator rend() const noexcept
255 { return const_reverse_iterator(begin()); }
256 const_reverse_iterator crend() const noexcept
257 { return const_reverse_iterator(begin()); }
258 protected:
259 const statement * _owner;
260 };
261
271 SQLITEPP_EXPORTED
272 class row_iterator : private row
273 {
274 public:
275 using value_type = row;
276 using size_type = int;
277 using difference_type = int;
278 using reference = row;
279 using pointer = void;
280 using iterator_category = std::input_iterator_tag;
281
282 public:
288 row_iterator() noexcept:
289 row(nullptr)
290 {}
291
298 row(owner)
299 {
300 if (_owner)
301 increment();
302 }
303
306 row_iterator(owner.get())
307 {}
308
309 row operator*() const noexcept
310 { return *this; }
311
312 const row * operator->() const noexcept
313 { return this; }
314
315 row_iterator & operator++()
316 { increment(); return *this; }
317 void operator++(int)
318 { increment(); }
319
320 friend bool operator==(const row_iterator & lhs, const row_iterator & rhs) noexcept
321 { return lhs._owner == rhs._owner; }
322 friend bool operator!=(const row_iterator & lhs, const row_iterator & rhs) noexcept
323 { return lhs._owner != rhs._owner; }
324 private:
325 void increment()
326 {
327 if (!const_cast<statement *>(_owner)->step())
328 _owner = nullptr;
329 }
330 };
331
341 SQLITEPP_EXPORTED
342 class row_range {
343 public:
344 using value_type = row;
345 using size_type = int;
346 using difference_type = int;
347 using reference = value_type;
348 using pointer = void;
349
350 using const_iterator = row_iterator;
351 using iterator = const_iterator;
352 public:
360 _it(owner)
361 {}
362
365 row_range(owner.get())
366 {}
367
368 const_iterator begin() const noexcept
369 { return _it; }
370 const_iterator cbegin() const noexcept
371 { return _it; }
372 const_iterator end() const noexcept
373 { return const_iterator(); }
374 const_iterator cend() const noexcept
375 { return const_iterator(); }
376
377 private:
378 row_iterator _it;
379 };
380
382}
383
384#endif
A cell in a row.
Definition row_iterator.hpp:35
const char * origin_name() const noexcept
Table column that is the origin of the cell.
Definition row_iterator.hpp:113
const char * name() const noexcept
Name of the cell's column.
Definition row_iterator.hpp:70
cell(const statement *owner, int idx) noexcept
Construct a cell for a given statement and column index.
Definition row_iterator.hpp:40
const char * database_name() const noexcept
Database that is the origin of the cell.
Definition row_iterator.hpp:94
const char * table_name() const noexcept
Table that is the origin of the cell.
Definition row_iterator.hpp:103
const char * declared_type() const noexcept
Declared datatype of the cell.
Definition row_iterator.hpp:123
T value() const noexcept
Cell's value.
Definition row_iterator.hpp:84
int type() const noexcept
Default datatype of the cell.
Definition row_iterator.hpp:56
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:44
An input iterator for statement results.
Definition row_iterator.hpp:273
row_iterator() noexcept
Create an empty iterator.
Definition row_iterator.hpp:288
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:305
row_iterator(statement *owner)
Create an instance referring to a given statement.
Definition row_iterator.hpp:297
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:364
row_range(statement *owner)
Create an instance referring to a given statement.
Definition row_iterator.hpp:359
Row result of a statement.
Definition row_iterator.hpp:145
row(const statement *owner) noexcept
Construct a row for a given statement.
Definition row_iterator.hpp:225
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:229
Prepared Statement Object.
Definition statement_iface.hpp:87
int data_count() const noexcept
Number of columns in a result set.
Definition statement_iface.hpp:593
ThinSQLite++ namespace.
Definition backup_iface.hpp:17