Skip to content

Commit

Permalink
docs: add sample for using array of struct query param
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Dec 17, 2024
1 parent c22e654 commit b1f5822
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.Type.StructField;
import com.google.cloud.spanner.Value;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminSettings;
import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection;
Expand Down Expand Up @@ -1519,6 +1522,47 @@ static void partitionedDmlPostgreSQL(
}
// [END spanner_postgresql_partitioned_dml]

static void arrayOfStructAsQueryParameter(
final String project,
final String instance,
final String database,
final Properties properties) throws SQLException {
try (Connection connection =
DriverManager.getConnection(
String.format(
"jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s",
project, instance, database),
properties)) {
try (Statement statement = connection.createStatement()) {
statement.execute(
"create table if not exists my_table (col1 string(max), col2 int64) primary key (col1)");
statement.execute(
"insert or update into my_table (col1, col2) values ('value1', 1), ('value2', 2), ('value3', 3)");
}

try (PreparedStatement statement = connection.prepareStatement(
"select * from my_table where STRUCT<col1 STRING, col2 INT64>(col1, col2) in unnest (?)")) {
statement.setObject(
1,
Value.structArray(
com.google.cloud.spanner.Type.struct(
StructField.of("col1", com.google.cloud.spanner.Type.string()),
StructField.of("col2", com.google.cloud.spanner.Type.int64())),
ImmutableList.of(
Struct.newBuilder().set("col1").to("value1").set("col2").to(1L).build(),
Struct.newBuilder().set("col1").to("value2").set("col2").to(2L).build())));
try (java.sql.ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
for (int col = 1; col <= resultSet.getMetaData().getColumnCount(); col++) {
System.out.printf("%s;", resultSet.getString(col));
}
System.out.println();
}
}
}
}
}

/** The expected number of command line arguments. */
private static final int NUM_EXPECTED_ARGS = 3;

Expand Down Expand Up @@ -1697,6 +1741,13 @@ static boolean runGoogleSQLSample(
database.getDatabase(),
createProperties());
return true;
case "arrayofstructparam":
arrayOfStructAsQueryParameter(
database.getInstanceId().getProject(),
database.getInstanceId().getInstance(),
database.getDatabase(),
createProperties());
return true;
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.example.spanner.jdbc.JdbcSample.addColumn;
import static com.example.spanner.jdbc.JdbcSample.addColumnPostgreSQL;
import static com.example.spanner.jdbc.JdbcSample.arrayOfStructAsQueryParameter;
import static com.example.spanner.jdbc.JdbcSample.createConnection;
import static com.example.spanner.jdbc.JdbcSample.createConnectionWithEmulator;
import static com.example.spanner.jdbc.JdbcSample.createDatabase;
Expand Down Expand Up @@ -243,6 +244,10 @@ public void testGoogleSQLSamples() throws Exception {

result = runSample(() -> partitionedDml(PROJECT_ID, INSTANCE_ID, DATABASE_ID, properties));
assertEquals("Updated at least 3 albums\n", result);

result = runSample(
() -> arrayOfStructAsQueryParameter(PROJECT_ID, INSTANCE_ID, DATABASE_ID, properties));
assertEquals("value1;1;\nvalue2;2;\n", result);
}

@Test
Expand Down

0 comments on commit b1f5822

Please sign in to comment.