boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
row.hpp
1// Copyright (c) 2022 Klemens D. Morgenstern
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5#ifndef BOOST_SQLITE_ROW_HPP
6#define BOOST_SQLITE_ROW_HPP
7
8#include <boost/sqlite/field.hpp>
9
10BOOST_SQLITE_BEGIN_NAMESPACE
11
20struct row
21{
23 std::size_t size() const
24 {
25 return sqlite3_column_count(stm_);
26 }
28 BOOST_SQLITE_DECL
29 field at(std::size_t idx) const;
31 field operator[](std::size_t idx) const
32 {
33 field f;
34 f.stm_ = stm_;
35 f.col_ = static_cast<int>(idx);
36 return f;
37 }
40 {
41 using difference_type = int;
42 using reference = field&;
43 using iterator_category = std::random_access_iterator_tag;
44
45 const_iterator & operator++()
46 {
47 f_.col_++;
48 return *this;
49 }
50
51 const_iterator operator++(int)
52 {
53 auto last = *this;
54 ++(*this);
55 return last;
56 }
57
58 const_iterator & operator--()
59 {
60 f_.col_--;
61 return *this;
62 }
63
64 const_iterator operator--(int)
65 {
66 auto last = *this;
67 --(*this);
68 return last;
69 }
70
71 field operator[](int i) const
72 {
73 auto f = f_;
74 f.col_ += i;
75 return f;
76 }
77
78 const_iterator operator+(int i) const
79 {
80 auto r = *this;
81 r.f_.col_ += i;
82 return r;
83 }
84
85 const_iterator operator-(int i) const
86 {
87 auto r = *this;
88 r.f_.col_ -= i;
89 return r;
90 }
91
92 const_iterator & operator+=(int i)
93 {
94 f_.col_ += i;
95 return *this;
96 }
97
98 const_iterator & operator-=(int i)
99 {
100 f_.col_ -= i;
101 return *this;
102 }
103
104 const field & operator*() const
105 {
106 return f_;
107 }
108
109 const field * operator->() const
110 {
111 return &f_;
112 }
113
114 bool operator==(const const_iterator& other) const
115 {
116 return f_.col_ == other.f_.col_
117 && f_.stm_ == other.f_.stm_;
118 }
119
120 bool operator!=(const const_iterator& other) const
121 {
122 return f_.col_ != other.f_.col_
123 || f_.stm_ != other.f_.stm_;
124 }
125
126 bool operator<(const const_iterator& other) const
127 {
128 return f_.col_ < other.f_.col_
129 && f_.stm_ < other.f_.stm_;
130 }
131
132 bool operator>(const const_iterator& other) const
133 {
134 return f_.col_ > other.f_.col_
135 && f_.stm_ > other.f_.stm_;
136 }
137
138 const_iterator() = default;
139 private:
140 friend struct row;
141 field f_;
142 };
145 {
147 ci.f_.col_ = 0;
148 ci.f_.stm_ = stm_;
149 return ci;
150 }
153 {
155 ci.f_.col_ = sqlite3_column_count(stm_);
156 ci.f_.stm_ = stm_;
157 return ci;
158 }
159 private:
160 friend struct resultset;
161 sqlite3_stmt * stm_;
162
163};
164
165BOOST_SQLITE_END_NAMESPACE
166
167#endif //BOOST_SQLITE_ROW_HPP
A holder for a sqlite field, i.e. something returned from a query.
Definition field.hpp:22
Representation of a result from a database.
Definition resultset.hpp:41
Random access iterator used to iterate over the columns.
Definition row.hpp:40
Representation of a row in a database.
Definition row.hpp:21
const_iterator end() const
Returns the end of the column-range.
Definition row.hpp:152
const_iterator begin() const
Returns the begin of the column-range.
Definition row.hpp:144
field operator[](std::size_t idx) const
Returns the field at idx.
Definition row.hpp:31
field at(std::size_t idx) const
Returns the field at idx,.
std::size_t size() const
The size of the row.
Definition row.hpp:23