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