15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_ROW_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_ROW_H
18#include "google/cloud/spanner/internal/tuple_utils.h"
19#include "google/cloud/spanner/value.h"
20#include "google/cloud/spanner/version.h"
21#include "google/cloud/status.h"
22#include "google/cloud/status_or.h"
35GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
37GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
40namespace spanner_internal {
41GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
44 std::shared_ptr<std::vector<std::string>
const>);
46GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
50GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
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
98 std::size_t
size()
const {
return columns_->size(); }
101 std::vector<std::string>
const&
columns()
const {
return *columns_; }
104 std::vector<
Value>
const&
values()
const& {
return values_; }
107 std::vector<
Value>&&
values() && {
return std::move(values_); }
110 StatusOr<
Value>
get(std::size_t pos)
const;
113 StatusOr<
Value>
get(std::string
const& name)
const;
116
117
118
119
120
121 template <
typename T,
typename Arg>
122 StatusOr<T>
get(Arg&& arg)
const {
123 auto v = get(std::forward<Arg>(arg));
124 if (v)
return v->
template get<T>();
129
130
131
132
133
134 template <
typename Tuple>
135 StatusOr<Tuple>
get()
const& {
136 if (
size() != std::tuple_size<Tuple>::value) {
137 auto constexpr kMsg =
"Tuple has the wrong number of elements";
141 auto it = values_.begin();
143 spanner_internal::ForEach(tup, ExtractValue{status}, it);
144 if (!status
.ok())
return status;
149
150
151
152
153
154 template <
typename Tuple>
155 StatusOr<Tuple>
get() && {
156 if (
size() != std::tuple_size<Tuple>::value) {
157 auto constexpr kMsg =
"Tuple has the wrong number of elements";
161 auto it = std::make_move_iterator(values_.begin());
163 spanner_internal::ForEach(tup, ExtractValue{status}, it);
164 if (!status
.ok())
return status;
175 friend struct spanner_internal::RowFriend;
176 struct ExtractValue {
178 template <
typename T,
typename It>
179 void operator()(T& t, It& it)
const {
180 auto x = it++->
template get<T>();
182 status = std::move(x).status();
190
191
192
193
194
195 Row(std::vector<
Value> values,
196 std::shared_ptr<
const std::vector<std::string>> columns);
198 std::vector<
Value> values_;
199 std::shared_ptr<
const std::vector<std::string>> columns_;
203
204
205
206
207
208
209
210
215
216
217
218
219
220
221
222
223
224
225
226template <
typename... Ts>
229 auto columns = std::make_shared<std::vector<std::string>>();
230 for (std::size_t i = 0; i <
sizeof...(ts); ++i) {
231 columns->emplace_back(std::to_string(i));
233 std::vector<
Value> v{
Value(std::forward<Ts>(ts))...};
234 return spanner_internal::RowFriend::MakeRow(std::move(v), std::move(columns));
238
239
240
241
242
243
244
245
246
247
248
249
250
254
255
256
257 using Source = std::function<StatusOr<
Row>()>;
261 using iterator_category = std::input_iterator_tag;
262 using value_type = StatusOr<
Row>;
263 using difference_type = std::ptrdiff_t;
264 using pointer = value_type*;
265 using reference = value_type&;
266 using const_pointer = value_type
const*;
267 using const_reference = value_type
const&;
274
275
276
279 reference
operator*() {
return row_; }
280 pointer
operator->() {
return &row_; }
282 const_reference
operator*()
const {
return row_; }
283 const_pointer
operator->()
const {
return &row_; }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316template <
typename Tuple>
321 using iterator_category = std::input_iterator_tag;
322 using value_type = StatusOr<Tuple>;
323 using difference_type = std::ptrdiff_t;
324 using pointer = value_type*;
325 using reference = value_type&;
326 using const_pointer = value_type
const*;
327 using const_reference = value_type
const&;
335 : it_(std::move(begin)), end_(std::move(end)) {
339 reference
operator*() {
return tup_; }
340 pointer
operator->() {
return &tup_; }
342 const_reference
operator*()
const {
return tup_; }
343 const_pointer
operator->()
const {
return &tup_; }
356 auto const old = *
this;
363 return a.it_ == b.it_;
373 if (it_
== end_)
return;
374 tup_ =
*it_ ? std::move(
*it_)
->template get<Tuple>() : it_
->status();
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411template <
typename Tuple>
415 static_assert(spanner_internal::IsTuple<Tuple>::value,
416 "TupleStream<T> requires a std::tuple parameter");
418 iterator
begin()
const {
return begin_; }
419 iterator
end()
const {
return end_; }
422 template <
typename T,
typename RowRange>
425 template <
typename It>
426 explicit TupleStream(It&& start, It&& end)
427 : begin_(std::forward<It>(start), std::forward<It>(end)) {}
434
435
436
437
438
439
440
441
442
443
444
445template <
typename Tuple,
typename RowRange>
447 static_assert(std::is_lvalue_reference<
decltype(range)>::value,
448 "range must be an lvalue since it must outlive StreamOf");
449 return TupleStream<Tuple>(std::begin(std::forward<RowRange>(range)),
450 std::end(std::forward<RowRange>(range)));
454
455
456
457
458
459
460
461
462
463
464
465
466template <
typename RowRange>
468 typename std::decay<
decltype(*range.begin())>::
type {
469 auto const e = range.end();
470 auto it = range.begin();
472 auto row = std::move(*it);
477GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
T const * operator->() const &
Status const & status() const &
Status(StatusCode code, std::string message, ErrorInfo info={})
A RowStreamIterator is an Input Iterator (see below) that returns a sequence of StatusOr<Row> objects...
Definition: row.h:251
friend bool operator==(RowStreamIterator const &, RowStreamIterator const &)
reference operator*()
Definition: row.h:279
RowStreamIterator & operator++()
RowStreamIterator operator++(int)
const_reference operator*() const
Definition: row.h:282
friend bool operator!=(RowStreamIterator const &, RowStreamIterator const &)
pointer operator->()
Definition: row.h:280
RowStreamIterator()
Default constructs an "end" iterator.
RowStreamIterator(Source source)
Constructs a RowStreamIterator that will consume rows from the given source, which must not be nullpt...
const_pointer operator->() const
Definition: row.h:283
A Row is a sequence of columns each with a name and an associated Value.
Definition: row.h:84
Row & operator=(Row const &)=default
friend bool operator==(Row const &a, Row const &b)
StatusOr< Tuple > get() const &
Returns all the native C++ values for the whole row in a std::tuple with the specified type.
Definition: row.h:135
std::size_t size() const
Returns the number of columns in the row.
Definition: row.h:98
StatusOr< Tuple > get() &&
Returns all the native C++ values for the whole row in a std::tuple with the specified type.
Definition: row.h:155
Row & operator=(Row &&)=default
std::vector< Value > const & values() const &
Returns the Value objects in the given row.
Definition: row.h:104
StatusOr< Value > get(std::string const &name) const
Returns the Value in the column with name.
StatusOr< T > get(Arg &&arg) const
Returns the native C++ value at the given position or column name.
Definition: row.h:122
friend bool operator!=(Row const &a, Row const &b)
Definition: row.h:171
Row()
Default constructs an empty row with no columns nor values.
std::vector< Value > && values() &&
Returns the Value objects in the given row.
Definition: row.h:107
std::vector< std::string > const & columns() const
Returns the column names for the row.
Definition: row.h:101
StatusOr< Value > get(std::size_t pos) const
Returns the Value at the given pos.
A TupleStreamIterator<Tuple> is an "Input Iterator" that wraps a RowStreamIterator,...
Definition: row.h:317
TupleStreamIterator(RowStreamIterator begin, RowStreamIterator end)
Creates an iterator that wraps the given RowStreamIterator range.
Definition: row.h:334
TupleStreamIterator operator++(int)
Definition: row.h:355
reference operator*()
Definition: row.h:339
friend bool operator==(TupleStreamIterator const &a, TupleStreamIterator const &b)
Definition: row.h:361
pointer operator->()
Definition: row.h:340
TupleStreamIterator & operator++()
Definition: row.h:345
const_pointer operator->() const
Definition: row.h:343
const_reference operator*() const
Definition: row.h:342
friend bool operator!=(TupleStreamIterator const &a, TupleStreamIterator const &b)
Definition: row.h:366
TupleStreamIterator()=default
Default constructs an "end" iterator.
A TupleStream<Tuple> defines a range that parses Tuple objects from the given range of RowStreamItera...
Definition: row.h:412
iterator end() const
Definition: row.h:419
iterator begin() const
Definition: row.h:418
friend TupleStream< T > StreamOf(RowRange &&range)
A factory that creates a TupleStream<Tuple> by wrapping the given range.
Definition: row.h:446
The Value class represents a type-safe, nullable Spanner value.
Definition: value.h:170
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
TupleStream< Tuple > StreamOf(RowRange &&range)
A factory that creates a TupleStream<Tuple> by wrapping the given range.
Definition: row.h:446
Row MakeTestRow(Ts &&... ts)
Creates a Row with Values created from the given arguments and with auto-generated column names.
Definition: row.h:228
auto GetSingularRow(RowRange range) -> typename std::decay< decltype(*range.begin())>::type
Returns the only row from a range that contains exactly one row.
Definition: row.h:467
Row MakeTestRow(std::vector< std::pair< std::string, Value > > pairs)
Creates a Row with the specified column names and values.
#define GOOGLE_CLOUD_CPP_SPANNER_MAKE_TEST_ROW_DEPRECATED()
Definition: version.h:30