/
resultset.hpp
304 lines (278 loc) · 7.73 KB
/
resultset.hpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// 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_MYSQL_RESULTSET_HPP
#define BOOST_MYSQL_RESULTSET_HPP
#include <boost/mysql/metadata.hpp>
#include <boost/mysql/metadata_collection_view.hpp>
#include <boost/mysql/resultset_view.hpp>
#include <boost/mysql/row_view.hpp>
#include <boost/mysql/rows.hpp>
#include <boost/mysql/detail/config.hpp>
#include <boost/assert.hpp>
namespace boost {
namespace mysql {
/**
* \brief An owning resultset, containing metadata, rows and additional info.
* \details
* Similar to \ref results, but can only represent a single resultset (while `results` can hold
* multiple resultsets). Can be used to take ownership of a \ref resultset_view.
*/
class resultset
{
public:
/**
* \brief Constructs an empty resultset.
* \details
* The constructed object has `this->has_value() == false`.
*
* \par Exception safety
* No-throw guarantee.
*/
resultset() = default;
/**
* \brief Copy constructor.
* \par Exception safety
* Strong guarantee. Internal allocations may throw.
*
* \par Object lifetimes
* `*this` lifetime will be independent of `other`'s.
*
* \par Complexity
* Linear on rows and metadata size for `other`.
*/
resultset(const resultset& other) = default;
/**
* \brief Move constructor.
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* Views obtained from `other` remain valid.
*
* \par Complexity
* Constant.
*/
resultset(resultset&& other) = default;
/**
* \brief Copy assignment.
* \par Exception safety
* Basic guarantee. Internal allocations may throw.
*
* \par Object lifetimes
* `*this` lifetime will be independent of `other`'s. Views obtained from `*this`
* are invalidated.
*
* \par Complexity
* Linear on rows and metadata size for `other`.
*/
resultset& operator=(const resultset& other) = default;
/**
* \brief Move assignment.
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* Views obtained from `*this` are invalidated. Views obtained from `other` remain valid.
*
* \par Complexity
* Constant.
*/
resultset& operator=(resultset&& other) = default;
/**
* \brief Destructor.
*/
~resultset() = default;
/**
* \brief Constructs a resultset object by taking ownership of a view.
* \par Exception safety
* Strong guarantee. Internal allocations may throw.
*
* \par Object lifetimes
* `*this` lifetime will be independent of `v`'s (the contents of `v` will be copied
* into `*this`).
*
* \par Complexity
* Linear on rows and metadata size for `v`.
*/
resultset(resultset_view v) { assign(v); }
/**
* \brief Replaces the contents of `*this` with a \ref resultset_view.
* \par Exception safety
* Basic guarantee. Internal allocations may throw.
*
* \par Object lifetimes
* `*this` lifetime will be independent of `v`'s (the contents of `v` will be copied
* into `*this`). Views obtained from `*this` are invalidated.
*
* \par Complexity
* Linear on rows and metadata size for `v` and `*this`.
*/
resultset& operator=(resultset_view v)
{
assign(v);
return *this;
}
/**
* \brief Returns whether this object contains actual data or not.
* \details
* Only returns true for default-constructed objects.
*
* \par Exception safety
* No-throw guarantee.
*
* \par Complexity
* Constant.
*/
bool has_value() const noexcept { return has_value_; }
/**
* \brief Returns the rows that this resultset contains.
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* The returned reference and any other references obtained from it are valid as long as
* `*this` or an object move-constructed from `*this` are alive.
*
* \par Complexity
* Constant.
*/
rows_view rows() const noexcept
{
BOOST_ASSERT(has_value_);
return rws_;
}
/**
* \brief Returns metadata for this resultset.
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* The returned reference and any other references obtained from it are valid as long as
* `*this` or an object move-constructed from `*this` are alive.
*
* \par Complexity
* Constant.
*/
metadata_collection_view meta() const noexcept
{
BOOST_ASSERT(has_value_);
return meta_;
}
/**
* \brief Returns the number of affected rows for this resultset.
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Complexity
* Constant.
*/
std::uint64_t affected_rows() const noexcept
{
BOOST_ASSERT(has_value_);
return affected_rows_;
}
/**
* \brief Returns the last insert ID for this resultset.
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Complexity
* Constant.
*/
std::uint64_t last_insert_id() const noexcept
{
BOOST_ASSERT(has_value_);
return last_insert_id_;
}
/**
* \brief Returns the number of warnings for this resultset.
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Complexity
* Constant.
*/
unsigned warning_count() const noexcept
{
BOOST_ASSERT(has_value_);
return warnings_;
}
/**
* \brief Returns additional information for this resultset.
* \details
* The format of this information is documented by MySQL <a
* href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
* \n
* The returned string always uses ASCII encoding, regardless of the connection's character set.
*
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* The returned reference and any other references obtained from it are valid as long as
* `*this` or an object move-constructed from `*this` are alive.
*
* \par Complexity
* Constant.
*/
string_view info() const noexcept
{
BOOST_ASSERT(has_value_);
return string_view(info_.data(), info_.size());
}
/**
* \brief Returns whether this resultset represents a procedure OUT params.
* \par Preconditions
* `this->has_value() == true`
*
* \par Exception safety
* No-throw guarantee.
*
* \par Complexity
* Constant.
*/
bool is_out_params() const noexcept
{
BOOST_ASSERT(has_value_);
return is_out_params_;
}
private:
bool has_value_{false};
std::vector<metadata> meta_;
::boost::mysql::rows rws_;
std::uint64_t affected_rows_{};
std::uint64_t last_insert_id_{};
std::uint16_t warnings_{};
std::vector<char> info_;
bool is_out_params_{false};
BOOST_MYSQL_DECL
void assign(resultset_view v);
};
} // namespace mysql
} // namespace boost
#ifdef BOOST_MYSQL_HEADER_ONLY
#include <boost/mysql/impl/resultset.ipp>
#endif
#endif