Library reference docs
Namespaces
google::cloud::spanner
Classes
Client
Functions
DatabaseAdminClient
Functions
Structs
Functions
Operators
google::cloud::spanner_admin
Classes
DatabaseAdminClient
Functions
DatabaseAdminConnectionIdempotencyPolicy
InstanceAdminClient
Functions
InstanceAdminConnectionIdempotencyPolicy
Structs
google::cloud::spanner_admin_mocks
Stay organized with collections Save and categorize content based on your preferences.
The Value class represents a type-safe, nullable Spanner value.
It is conceptually similar to a std::any
except the only allowed types are those supported by Spanner, and a "null" value (similar to a std::any
without a value) still has an associated type. The supported types are shown in the following table along with how they map to the Spanner types (https://cloud.google.com/spanner/docs/data-types):
[1] The type T
may be any of the other supported types, except for ARRAY/std::vector
.
Value is a regular C++ value type with support for copy, move, equality, etc. A default-constructed Value represents an empty value with no type.
Note: There is also a C++ type ofCommitTimestamp
that corresponds to a Cloud Spanner TIMESTAMP object for setting the commit timestamp on a column with the allow_commit_timestamp
set to true
in the schema. See Also
https://cloud.google.com/spanner/docs/commit-timestamp Callers may create instances by passing any of the supported values (shown in the table above) to the constructor. "Null" values are created using the MakeNullValue<T>()
factory function or by passing an empty absl::optional<T>
to the Value constructor..
Using a non-null value.
std::string msg = "hello";
spanner::Value v(msg);
StatusOr<std::string> copy = v.get<std::string>();
if (copy) {
std::cout << *copy; // prints "hello"
}
Example
Using a null value.
spanner::Value v = spanner::MakeNullValue<std::int64_t>();
StatusOr<std::int64_t> i = v.get<std::int64_t>();
assert(!i.ok()); // Can't get the value because v is null
StatusOr < absl::optional<std::int64_t> j =
v.get<absl::optional<std::int64_t>>();
assert(j.ok()); // OK because an empty option can represent the null
assert(!j->has_value()); // v held no value.
Nullness
All of the supported types (above) are "nullable". A null is created in one of two ways:
absl::optional<T>()
with no value to Value
's constructor.MakeNullValue<T>()
helper function (defined below).Nulls can be retrieved from a Value::get
<T>
by specifying the type T
as an absl::optional<U>
. The returned optional will either be empty (indicating null) or it will contain the actual value. See the documentation for Value::get
<T>
below for more details.
Spanner arrays are represented in C++ as a std::vector<T>
, where the type T
may be any of the other allowed Spanner types, such as bool
, std::int64_t
, etc. The only exception is that arrays may not directly contain another array; to achieve a similar result you could create an array of a 1-element struct holding an array. The following examples show usage of arrays.
std::vector<std::int64_t> vec = {1, 2, 3, 4, 5};
spanner::Value v(vec);
auto copy = *v.get<std::vector<std::int64_t>>();
assert(vec == copy);
Spanner Structs
Spanner structs are represented in C++ as instances of std::tuple
holding zero or more of the allowed Spanner types, such as bool
, std::int64_t
, std::vector
, and even other std::tuple
objects. Each tuple element corresponds to a single field in a Spanner STRUCT.
Spanner STRUCT fields may optionally contain a string indicating the field's name. Fields names may be empty, unique, or repeated. A named field may be specified as a tuple element of type std::pair<std::string, T>
, where the pair's .first
member indicates the field's name, and the .second
member is any valid Spanner type T
.
using Struct = std::tuple<bool, std::pair<std::string, std::int64_t>>;
Struct s = {true, {"Foo", 42}};
spanner::Value v(s);
assert(s == *v.get<Struct>());
Note: While a STRUCT's (optional) field names are not part of its C++ type, they are part of its Spanner STRUCT type. Array's (i.e., std::vector
) must contain a single element type, therefore it is an error to construct a std::vector
of std::tuple
objects with differently named fields. Constructors Value()
Constructs a Value
that holds nothing.
All calls to get<T>()
will return an error.
Value const &
Value &&
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
bool
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
std::int64_t
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
double
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
std::string
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
Bytes
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
Json
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
JsonB
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
Numeric
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
PgNumeric
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
Timestamp
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
CommitTimestamp
Constructs an instance with the specified type and value.
Parameter Name Descriptionv
absl::CivilDay
Constructs an instance from common C++ literal types that closely, though not exactly, match supported Spanner types.
An integer literal in C++ is of type int
, which is not exactly an allowed Spanner type. This will be allowed but it will be implicitly up converted to a std::int64_t
. Similarly, a C++ string literal will be implicitly converted to a std::string
. For example:
spanner::Value v1(42);
assert(42 == *v1.get<std::int64_t>());
spanner::Value v2("hello");
assert("hello" == *v2.get<std::string>());
Parameter Name Description v
int
Constructs an instance from common C++ literal types that closely, though not exactly, match supported Spanner types.
An integer literal in C++ is of type int
, which is not exactly an allowed Spanner type. This will be allowed but it will be implicitly up converted to a std::int64_t
. Similarly, a C++ string literal will be implicitly converted to a std::string
. For example:
spanner::Value v1(42);
assert(42 == *v1.get<std::int64_t>());
spanner::Value v2("hello");
assert("hello" == *v2.get<std::string>());
Parameter Name Description v
char const *
Constructs a non-null instance if opt
has a value, otherwise constructs a null instance with the specified type T
.
opt
absl::optional< T >
typename T
Constructs an instance from a Spanner ARRAY of the specified type and values.
The type T
may be any valid type shown above, except vectors of vectors are not allowed.
T
is a std::tuple
with field names (i.e., at least one of its element types is a std::pair<std::string, T>
) then, all of the vector's elements must have exactly the same field names. Any mismatch in in field names results in undefined behavior. Parameters Name Description v
std::vector< T >
typename T
Constructs an instance from a Spanner STRUCT with a type and values matching the given std::tuple
.
Any STRUCT field may optionally have a name, which is specified as std::pair<std::string, T>
.
tup
std::tuple< Ts... >
typename...
Value const &
Value &
operator=(Value &&) Parameter Name Description
Value &&
Value &
Functions get() const &
Returns the contained value wrapped in a google::cloud::StatusOr
<T>
.
Returns a non-OK status IFF:
T
is not an absl::optional
.T
.spanner::Value v{3.14};
StatusOr<double> d = v.get<double>();
if (d) {
std::cout << "d=" << *d;
}
// Now using a "null" std::int64_t
v = spanner::MakeNullValue<std::int64_t>();
StatusOr<std::int64_t> i = v.get<std::int64_t>();
if (!i) {
std::cerr << "Could not get integer: " << i.status();
}
StatusOr<absl::optional<std::int64_t>> j =
v.get<absl::optional<std::int64_t>>();
assert(j.ok()); // Since we know the types match in this example
assert(!v->has_value()); // Since we know v was null in this example
Parameter Name Description typename T
StatusOr< T >
get() &&
Returns the contained value wrapped in a google::cloud::StatusOr
<T>
.
Returns a non-OK status IFF:
T
is not an absl::optional
.T
.spanner::Value v{3.14};
StatusOr<double> d = v.get<double>();
if (d) {
std::cout << "d=" << *d;
}
// Now using a "null" std::int64_t
v = spanner::MakeNullValue<std::int64_t>();
StatusOr<std::int64_t> i = v.get<std::int64_t>();
if (!i) {
std::cerr << "Could not get integer: " << i.status();
}
StatusOr<absl::optional<std::int64_t>> j =
v.get<absl::optional<std::int64_t>>();
assert(j.ok()); // Since we know the types match in this example
assert(!v->has_value()); // Since we know v was null in this example
Parameter Name Description typename T
StatusOr< T >
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["The `Value` class in Spanner represents a type-safe, nullable value that supports various Spanner types, including `BOOL`, `INT64`, `FLOAT64`, `STRING`, `BYTES`, `JSON`, `JSONB`, `NUMERIC`, `TIMESTAMP`, `DATE`, `ARRAY`, and `STRUCT`."],["Null values can be created using either `MakeNullValue\u003cT\u003e()` or by passing an empty `absl::optional\u003cT\u003e` to the `Value` constructor, and these nulls can be retrieved by specifying the type `T` as an `absl::optional\u003cU\u003e`."],["Spanner arrays are represented as `std::vector\u003cT\u003e` in C++, where `T` can be any supported Spanner type except for another array, and structs are represented as `std::tuple\u003cTs...\u003e`, with optional field names using `std::pair\u003cstd::string, T\u003e`."],["The `Value` class provides constructors for all supported types, including implicit conversions from `int` and `char const *`, and it offers `get\u003cT\u003e()` methods to retrieve the contained value, which returns a `StatusOr\u003cT\u003e` and handles null values appropriately."],["The `CommitTimestamp` type in C++ corresponds to a Cloud Spanner `TIMESTAMP` object, which is useful for setting commit timestamps on columns in the schema that allow it."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4