|
22 | 22 | import com.google.cloud.spanner.Mutation; |
23 | 23 | import com.google.cloud.spanner.SpannerExceptionFactory; |
24 | 24 | import com.google.cloud.spanner.SpannerOptions; |
| 25 | +import com.google.cloud.spanner.Struct; |
| 26 | +import com.google.cloud.spanner.Type.StructField; |
| 27 | +import com.google.cloud.spanner.Value; |
25 | 28 | import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
26 | 29 | import com.google.cloud.spanner.admin.database.v1.DatabaseAdminSettings; |
27 | 30 | import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection; |
@@ -1519,6 +1522,47 @@ static void partitionedDmlPostgreSQL( |
1519 | 1522 | } |
1520 | 1523 | // [END spanner_postgresql_partitioned_dml] |
1521 | 1524 |
|
| 1525 | + static void arrayOfStructAsQueryParameter( |
| 1526 | + final String project, |
| 1527 | + final String instance, |
| 1528 | + final String database, |
| 1529 | + final Properties properties) throws SQLException { |
| 1530 | + try (Connection connection = |
| 1531 | + DriverManager.getConnection( |
| 1532 | + String.format( |
| 1533 | + "jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", |
| 1534 | + project, instance, database), |
| 1535 | + properties)) { |
| 1536 | + try (Statement statement = connection.createStatement()) { |
| 1537 | + statement.execute( |
| 1538 | + "create table if not exists my_table (col1 string(max), col2 int64) primary key (col1)"); |
| 1539 | + statement.execute( |
| 1540 | + "insert or update into my_table (col1, col2) values ('value1', 1), ('value2', 2), ('value3', 3)"); |
| 1541 | + } |
| 1542 | + |
| 1543 | + try (PreparedStatement statement = connection.prepareStatement( |
| 1544 | + "select * from my_table where STRUCT<col1 STRING, col2 INT64>(col1, col2) in unnest (?)")) { |
| 1545 | + statement.setObject( |
| 1546 | + 1, |
| 1547 | + Value.structArray( |
| 1548 | + com.google.cloud.spanner.Type.struct( |
| 1549 | + StructField.of("col1", com.google.cloud.spanner.Type.string()), |
| 1550 | + StructField.of("col2", com.google.cloud.spanner.Type.int64())), |
| 1551 | + ImmutableList.of( |
| 1552 | + Struct.newBuilder().set("col1").to("value1").set("col2").to(1L).build(), |
| 1553 | + Struct.newBuilder().set("col1").to("value2").set("col2").to(2L).build()))); |
| 1554 | + try (java.sql.ResultSet resultSet = statement.executeQuery()) { |
| 1555 | + while (resultSet.next()) { |
| 1556 | + for (int col = 1; col <= resultSet.getMetaData().getColumnCount(); col++) { |
| 1557 | + System.out.printf("%s;", resultSet.getString(col)); |
| 1558 | + } |
| 1559 | + System.out.println(); |
| 1560 | + } |
| 1561 | + } |
| 1562 | + } |
| 1563 | + } |
| 1564 | + } |
| 1565 | + |
1522 | 1566 | /** The expected number of command line arguments. */ |
1523 | 1567 | private static final int NUM_EXPECTED_ARGS = 3; |
1524 | 1568 |
|
@@ -1697,6 +1741,13 @@ static boolean runGoogleSQLSample( |
1697 | 1741 | database.getDatabase(), |
1698 | 1742 | createProperties()); |
1699 | 1743 | return true; |
| 1744 | + case "arrayofstructparam": |
| 1745 | + arrayOfStructAsQueryParameter( |
| 1746 | + database.getInstanceId().getProject(), |
| 1747 | + database.getInstanceId().getInstance(), |
| 1748 | + database.getDatabase(), |
| 1749 | + createProperties()); |
| 1750 | + return true; |
1700 | 1751 | default: |
1701 | 1752 | return false; |
1702 | 1753 | } |
|
0 commit comments