Working with STRUCT objects

Spanner allows you to create STRUCT objects from data, as well as to use STRUCT objects as bound parameters when running a SQL query with one of the Spanner client libraries.

For more information about the STRUCT type in Spanner, see Data types.

Declaring a user-defined type of STRUCT object

You can declare a STRUCT object in queries using the syntax described in Declaring a STRUCT type.

You can define a type of STRUCT object as a sequence of field names and their data types. You can then supply this type along with queries containing STRUCT-typed parameter bindings and Spanner will use it to check that the STRUCT parameter values in your query are valid.

C++

// Cloud Spanner STRUCT<> types are represented by std::tuple<...>. The
// following represents a STRUCT<> with two unnamed STRING fields.
using NameType = std::tuple<std::string, std::string>;

C#

var nameType = new SpannerStruct
{
    { "FirstName", SpannerDbType.String, null},
    { "LastName", SpannerDbType.String, null}
};

Go


type nameType struct {
	FirstName string
	LastName  string
}

Java

Type nameType =
    Type.struct(
        Arrays.asList(
            StructField.of("FirstName", Type.string()),
            StructField.of("LastName", Type.string())));

Node.js

const nameType = {
  type: 'struct',
  fields: [
    {
      name: 'FirstName',
      type: 'string',
    },
    {
      name: 'LastName',
      type: 'string',
    },
  ],
};

PHP