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" 28 #include <type_traits> 39 Row MakeRow(std::vector<Value>,
40 std::shared_ptr<
const std::vector<std::string>>);
86 Row& operator=(
Row const&) =
default;
88 Row& operator=(
Row&&) =
default;
92 std::size_t
size()
const {
return columns_->size(); }
95 std::vector<std::string>
const&
columns()
const {
return *columns_; }
98 std::vector<Value>
const&
values() const& {
return values_; }
101 std::vector<Value>&&
values() && {
return std::move(values_); }
104 StatusOr<Value> get(std::size_t pos)
const;
107 StatusOr<Value> get(std::string
const& name)
const;
115 template <
typename T,
typename Arg>
116 StatusOr<T>
get(Arg&& arg)
const {
117 auto v = get(std::forward<Arg>(arg));
118 if (v)
return v->template get<T>();
128 template <
typename Tuple>
129 StatusOr<Tuple>
get() const& {
130 if (size() != std::tuple_size<Tuple>::value) {
131 auto const msg =
"Tuple has the wrong number of elements";
132 return Status(StatusCode::kInvalidArgument, msg);
135 auto it = values_.begin();
137 internal::ForEach(tup, ExtractValue{status}, it);
138 if (!status.ok())
return status;
148 template <
typename Tuple>
149 StatusOr<Tuple>
get() && {
150 if (size() != std::tuple_size<Tuple>::value) {
151 auto const msg =
"Tuple has the wrong number of elements";
152 return Status(StatusCode::kInvalidArgument, msg);
155 auto it = std::make_move_iterator(values_.begin());
157 internal::ForEach(tup, ExtractValue{status}, it);
158 if (!status.ok())
return status;
169 friend Row internal::MakeRow(std::vector<Value>,
170 std::shared_ptr<
const std::vector<std::string>>);
171 struct ExtractValue {
173 template <
typename T,
typename It>
174 void operator()(T& t, It& it)
const {
175 auto x = it++->template get<T>();
177 status = std::move(x).status();
190 Row(std::vector<Value> values,
191 std::shared_ptr<
const std::vector<std::string>> columns);
193 std::vector<Value> values_;
194 std::shared_ptr<const std::vector<std::string>> columns_;
206 Row
MakeTestRow(std::vector<std::pair<std::string, Value>> pairs);
220 template <
typename... Ts>
222 auto columns = std::make_shared<std::vector<std::string>>();
223 for (std::size_t i = 0; i <
sizeof...(ts); ++i) {
224 columns->emplace_back(std::to_string(i));
226 std::vector<Value> v{
Value(std::forward<Ts>(ts))...};
227 return internal::MakeRow(std::move(v), std::move(columns));
250 using Source = std::function<StatusOr<Row>()>;
308 template <
typename Tuple>
327 : it_(std::move(begin)), end_(std::move(end)) {
348 auto const old = *
this;
355 return a.it_ == b.it_;
365 if (it_ == end_)
return;
366 tup_ = *it_ ? std::move(*it_)->template get<Tuple>() : it_->status();
370 RowStreamIterator it_;
371 RowStreamIterator end_;
401 template <
typename Tuple>
405 static_assert(internal::IsTuple<Tuple>::value,
406 "TupleStream<T> requires a std::tuple parameter");
412 template <
typename T,
typename RowRange>
415 template <
typename It>
417 : begin_(std::forward<It>(start), std::forward<It>(end)) {}
435 template <
typename Tuple,
typename RowRange>
437 static_assert(std::is_lvalue_reference<decltype(range)>::value,
438 "range must be an lvalue since it must outlive StreamOf");
440 std::end(std::forward<RowRange>(range)));
461 template <
typename RowRange>
463 decltype(*std::forward<RowRange>(range).begin())>::type {
464 auto const e = std::forward<RowRange>(range).end();
465 auto it = std::forward<RowRange>(range).begin();
466 if (it == e)
return Status(StatusCode::kInvalidArgument,
"no rows");
467 auto row = std::move(*it);
468 if (++it != e)
return Status(StatusCode::kInvalidArgument,
"too many rows");
477 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_ROW_H TupleStreamIterator operator++(int)
The Value class represents a type-safe, nullable Spanner value.
bool operator!=(Backup const &a, Backup const &b)
std::function< StatusOr< Row >()> Source
A function that returns a sequence of StatusOr<Row> objects.
StatusOr< Tuple > get() &&
Returns all the native C++ values for the whole row in a std::tuple with the specified type.
std::input_iterator_tag iterator_category
TupleStreamIterator(RowStreamIterator begin, RowStreamIterator end)
Creates an iterator that wraps the given RowStreamIterator range.
const_reference operator*() const
const_pointer operator->() const
value_type const & const_reference
std::size_t size() const
Returns the number of columns in the row.
A TupleStream<Tuple> defines a range that parses Tuple objects from the given range of RowStreamItera...
StatusOr< Row > value_type
value_type const * const_pointer
bool operator==(Backup const &a, Backup const &b)
TupleStream< Tuple > StreamOf(RowRange &&range)
A factory that creates a TupleStream<Tuple> by wrapping the given range.
A RowStreamIterator is an Input Iterator (see below) that returns a sequence of StatusOr<Row> objects...
std::vector< Value > && values() &&
Returns the Value objects in the given row.
StatusOr< Tuple > value_type
const_reference operator*() const
value_type const & const_reference
Contains all the Cloud Spanner C++ client types and functions.
std::vector< std::string > const & columns() const
Returns the column names for the row.
friend bool operator!=(Row const &a, Row const &b)
std::ptrdiff_t difference_type
StatusOr< Tuple > get() const &
Returns all the native C++ values for the whole row in a std::tuple with the specified type.
auto GetSingularRow(RowRange &&range) -> typename std::decay< decltype(*std::forward< RowRange >(range).begin())>::type
Returns the only row from a range that contains exactly one row.
std::input_iterator_tag iterator_category
std::ptrdiff_t difference_type
A Row is a sequence of columns each with a name and an associated Value.
#define SPANNER_CLIENT_NS
friend bool operator==(TupleStreamIterator const &a, TupleStreamIterator const &b)
const_pointer operator->() const
friend bool operator!=(TupleStreamIterator const &a, TupleStreamIterator const &b)
std::vector< Value > const & values() const &
Returns the Value objects in the given row.
A TupleStreamIterator<Tuple> is an "Input Iterator" that wraps a RowStreamIterator,...
value_type const * const_pointer
Row MakeTestRow(Ts &&... ts)
Creates a Row with Values created from the given arguments and with auto-generated column names.
StatusOr< T > get(Arg &&arg) const
Returns the native C++ value at the given position or column name.
TupleStreamIterator & operator++()