Skip to content

Commit ee6082f

Browse files
authored
feat: add methods for unwrapping Spanner client (#1914)
Adds methods for unwrapping the underlying DatabaseClient and Spanner instance that is used by a JDBC connection.
1 parent 4744c11 commit ee6082f

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

clirr-ignored-differences.xml

+15
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,19 @@
108108
<className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
109109
<method>void setAutoBatchDmlUpdateCountVerification(boolean)</method>
110110
</difference>
111+
<difference>
112+
<differenceType>7012</differenceType>
113+
<className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
114+
<method>com.google.cloud.spanner.DatabaseClient getDatabaseClient()</method>
115+
</difference>
116+
<difference>
117+
<differenceType>7012</differenceType>
118+
<className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
119+
<method>com.google.cloud.spanner.Spanner getSpanner()</method>
120+
</difference>
121+
<difference>
122+
<differenceType>7012</differenceType>
123+
<className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
124+
<method>com.google.cloud.spanner.DatabaseId getDatabaseId()</method>
125+
</difference>
111126
</differences>

src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcConnection.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.spanner.jdbc;
1818

19+
import com.google.cloud.spanner.DatabaseClient;
20+
import com.google.cloud.spanner.DatabaseId;
1921
import com.google.cloud.spanner.Dialect;
2022
import com.google.cloud.spanner.Spanner;
2123
import com.google.cloud.spanner.SpannerException;
@@ -68,6 +70,16 @@ abstract class AbstractJdbcConnection extends AbstractJdbcWrapper
6870
this.usesDirectExecutor = ConnectionOptionsHelper.usesDirectExecutor(options);
6971
}
7072

73+
@Override
74+
public DatabaseId getDatabaseId() {
75+
return this.options.getDatabaseId();
76+
}
77+
78+
@Override
79+
public DatabaseClient getDatabaseClient() {
80+
return getSpannerConnection().getDatabaseClient();
81+
}
82+
7183
/** Return the corresponding {@link com.google.cloud.spanner.connection.Connection} */
7284
com.google.cloud.spanner.connection.Connection getSpannerConnection() {
7385
return spanner;
@@ -82,7 +94,8 @@ ConnectionOptions getConnectionOptions() {
8294
return options;
8395
}
8496

85-
Spanner getSpanner() {
97+
@Override
98+
public Spanner getSpanner() {
8699
return this.spanner.getSpanner();
87100
}
88101

src/main/java/com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection.java

+25
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import com.google.cloud.spanner.AbortedException;
2121
import com.google.cloud.spanner.CommitResponse;
2222
import com.google.cloud.spanner.CommitStats;
23+
import com.google.cloud.spanner.DatabaseClient;
24+
import com.google.cloud.spanner.DatabaseId;
2325
import com.google.cloud.spanner.Dialect;
2426
import com.google.cloud.spanner.Mutation;
2527
import com.google.cloud.spanner.Options.QueryOption;
2628
import com.google.cloud.spanner.PartitionOptions;
29+
import com.google.cloud.spanner.Spanner;
2730
import com.google.cloud.spanner.TimestampBound;
2831
import com.google.cloud.spanner.connection.AutocommitDmlMode;
2932
import com.google.cloud.spanner.connection.SavepointSupport;
@@ -47,6 +50,28 @@
4750
*/
4851
public interface CloudSpannerJdbcConnection extends Connection {
4952

53+
/**
54+
* Returns the {@link DatabaseId} of the database that this {@link Connection} is connected to.
55+
*/
56+
default DatabaseId getDatabaseId() {
57+
throw new UnsupportedOperationException();
58+
}
59+
60+
/**
61+
* Returns the underlying {@link DatabaseClient} that is used by this connection. Operations that
62+
* are executed on the {@link DatabaseClient} that is returned has no impact on this {@link
63+
* Connection}, e.g. starting a read/write transaction on the {@link DatabaseClient} will not
64+
* start a transaction on this connection.
65+
*/
66+
default DatabaseClient getDatabaseClient() {
67+
throw new UnsupportedOperationException();
68+
}
69+
70+
/** Returns the underlying {@link Spanner} instance that is used by this connection. */
71+
default Spanner getSpanner() {
72+
throw new UnsupportedOperationException();
73+
}
74+
5075
/**
5176
* Sets the transaction tag to use for the current transaction. This method may only be called
5277
* when in a transaction, and before the transaction is actually started, i.e. before any

src/test/java/com/google/cloud/spanner/jdbc/DdlMockServerTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
2525
import com.google.cloud.spanner.Statement;
2626
import com.google.cloud.spanner.connection.AbstractMockServerTest;
27+
import com.google.common.collect.ImmutableList;
2728
import com.google.longrunning.Operation;
2829
import com.google.protobuf.Any;
2930
import com.google.protobuf.Empty;
3031
import com.google.rpc.Code;
32+
import com.google.spanner.admin.database.v1.GetDatabaseDdlResponse;
3133
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
3234
import java.sql.Connection;
3335
import java.sql.DriverManager;
3436
import java.sql.PreparedStatement;
3537
import java.sql.SQLException;
38+
import java.util.List;
3639
import org.junit.Test;
3740
import org.junit.runner.RunWith;
3841
import org.junit.runners.JUnit4;
@@ -50,6 +53,29 @@ private Connection createConnection(boolean autoCommit) throws SQLException {
5053
return DriverManager.getConnection(createUrl(autoCommit));
5154
}
5255

56+
@Test
57+
public void testGetDatabaseDdl() throws SQLException {
58+
List<String> expectedDdl =
59+
ImmutableList.of(
60+
"create table foo (id int64) primary key (id)",
61+
"create table bar (id int64) primary key (id)");
62+
mockDatabaseAdmin.addResponse(
63+
GetDatabaseDdlResponse.newBuilder().addAllStatements(expectedDdl).build());
64+
65+
try (Connection connection = createConnection(/* autoCommit = */ true)) {
66+
CloudSpannerJdbcConnection spannerJdbcConnection =
67+
connection.unwrap(CloudSpannerJdbcConnection.class);
68+
List<String> ddl =
69+
spannerJdbcConnection
70+
.getSpanner()
71+
.getDatabaseAdminClient()
72+
.getDatabaseDdl(
73+
spannerJdbcConnection.getDatabaseId().getInstanceId().getInstance(),
74+
spannerJdbcConnection.getDatabaseId().getDatabase());
75+
assertEquals(expectedDdl, ddl);
76+
}
77+
}
78+
5379
@Test
5480
public void testDdlInAutoCommitIsTrue_succeeds() throws SQLException {
5581
mockDatabaseAdmin.addResponse(

src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertNotNull;
2324
import static org.junit.Assert.assertThrows;
2425
import static org.junit.Assert.assertTrue;
2526
import static org.junit.Assert.fail;
@@ -100,6 +101,16 @@ private ConnectionOptions mockOptions() {
100101
return options;
101102
}
102103

104+
@Test
105+
public void testGetDatabaseClient() throws SQLException {
106+
ConnectionOptions options = mockOptions();
107+
try (Connection connection = createConnection(options)) {
108+
CloudSpannerJdbcConnection spannerJdbcConnection =
109+
connection.unwrap(CloudSpannerJdbcConnection.class);
110+
assertNotNull(spannerJdbcConnection.getDatabaseClient());
111+
}
112+
}
113+
103114
@Test
104115
public void testAutoCommit() throws SQLException {
105116
ConnectionOptions options = mockOptions();

0 commit comments

Comments
 (0)