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