Class Statement.StatementFactory (6.93.0)

public static final class Statement.StatementFactory

Factory for creating Statements with unnamed parameters.

This class is primarily intended for framework developers who want to integrate the Spanner client with a framework that uses unnamed parameters. Developers who want to use the Spanner client in their application, should use named parameters.

Usage Example

Simple SQL query


 Statement statement = databaseClient.getStatementFactory()
     .withUnnamedParameters("SELECT * FROM TABLE WHERE ID = ?", 10L)
 

SQL query with multiple parameters


 long id = 10L;
 String name = "google";
 List<String> phoneNumbers = Arrays.asList("1234567890", "0987654321");
 Statement statement = databaseClient.getStatementFactory()
      .withUnnamedParameters("INSERT INTO TABLE (ID, name, phonenumbers) VALUES(?, ?, ?)", id, name, phoneNumbers)
 

How to use arrays with the IN operator


 long[] ids = {10L, 12L, 1483L};
 Statement statement = databaseClient.getStatementFactory()
     .withUnnamedParameters("SELECT * FROM TABLE WHERE ID = UNNEST(?)", ids)
 

See Also: DatabaseClient#getStatementFactory(), StatementFactory#withUnnamedParameters(String, Object...)

Inheritance

java.lang.Object > Statement.StatementFactory

Methods

of(String sql)

public Statement of(String sql)
Parameter
Name Description
sql String
Returns
Type Description
Statement

withUnnamedParameters(String sql, Object[] values)

public Statement withUnnamedParameters(String sql, Object[] values)

This function accepts a SQL statement with unnamed parameters (?) and accepts a list of objects that should be used as the values for those parameters. Primitive types are supported.

For parameters of type DATE, the following types are supported

  • java.time.LocalDate
  • com.google.cloud.Date

For parameters of type TIMESTAMP, the following types are supported. Note that Spanner stores all timestamps in UTC. Instances of ZonedDateTime and OffsetDateTime that use other timezones than UTC, will be converted to the corresponding UTC values before being sent to Spanner. Instances of LocalDateTime will be converted to a ZonedDateTime using the system default timezone, and then converted to UTC before being sent to Spanner.

  • java.time.LocalDateTime
  • java.time.OffsetDateTime
  • java.time.ZonedDateTime

See Also: DatabaseClient#getStatementFactory

Parameters
Name Description
sql String

SQL statement with unnamed parameters denoted as ?

values Object[]

positional list of values for the unnamed parameters in the SQL string

Returns
Type Description
Statement

Statement a statement that can be executed on Spanner