Skip to content

Commit 123a7bc

Browse files
authored
feat: support for spanner external host (#1884)
* feat(spanner): jdbc support for spanner external host * added unit tests for the new introduced pattern
1 parent 9d90d70 commit 123a7bc

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.cloud.spanner.connection.ConnectionOptionsHelper;
2626
import com.google.cloud.spanner.connection.ConnectionPropertiesHelper;
2727
import com.google.cloud.spanner.connection.ConnectionProperty;
28+
import com.google.common.annotations.VisibleForTesting;
2829
import com.google.rpc.Code;
2930
import io.opentelemetry.api.OpenTelemetry;
3031
import java.sql.Connection;
@@ -145,6 +146,11 @@ public class JdbcDriver implements Driver {
145146
private static final String JDBC_URL_FORMAT =
146147
"jdbc:" + ConnectionOptions.Builder.SPANNER_URI_FORMAT;
147148
private static final Pattern URL_PATTERN = Pattern.compile(JDBC_URL_FORMAT);
149+
private static final String JDBC_EXTERNAL_HOST_FORMAT =
150+
"jdbc:" + ConnectionOptions.Builder.EXTERNAL_HOST_FORMAT;
151+
152+
@VisibleForTesting
153+
static final Pattern EXTERNAL_HOST_URL_PATTERN = Pattern.compile(JDBC_EXTERNAL_HOST_FORMAT);
148154

149155
@InternalApi
150156
public static String getClientLibToken() {
@@ -213,7 +219,8 @@ public Connection connect(String url, Properties info) throws SQLException {
213219
if (url != null && url.startsWith("jdbc:cloudspanner")) {
214220
try {
215221
Matcher matcher = URL_PATTERN.matcher(url);
216-
if (matcher.matches()) {
222+
Matcher matcherExternalHost = EXTERNAL_HOST_URL_PATTERN.matcher(url);
223+
if (matcher.matches() || matcherExternalHost.matches()) {
217224
// strip 'jdbc:' from the URL, add any extra properties and pass on to the generic
218225
// Connection API
219226
String connectionUri = appendPropertiesToUrl(url.substring(5), info);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

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

19+
import static com.google.cloud.spanner.jdbc.JdbcDriver.EXTERNAL_HOST_URL_PATTERN;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
2123
import static org.junit.Assert.assertNotNull;
2224
import static org.junit.Assert.assertTrue;
2325
import static org.junit.Assert.fail;
@@ -47,6 +49,7 @@
4749
import java.util.Collection;
4850
import java.util.Objects;
4951
import java.util.Properties;
52+
import java.util.regex.Matcher;
5053
import org.junit.AfterClass;
5154
import org.junit.BeforeClass;
5255
import org.junit.Test;
@@ -211,4 +214,32 @@ public void testLenient() throws SQLException {
211214
assertThat(jdbc.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
212215
}
213216
}
217+
218+
@Test
219+
public void testJdbcExternalHostFormat() {
220+
Matcher matcherWithoutInstance =
221+
EXTERNAL_HOST_URL_PATTERN.matcher("jdbc:cloudspanner://localhost:15000/databases/test-db");
222+
assertTrue(matcherWithoutInstance.matches());
223+
assertEquals("test-db", matcherWithoutInstance.group("DATABASEGROUP"));
224+
Matcher matcherWithProperty =
225+
EXTERNAL_HOST_URL_PATTERN.matcher(
226+
"jdbc:cloudspanner://localhost:15000/instances/default/databases/singers-db?usePlainText=true");
227+
assertTrue(matcherWithProperty.matches());
228+
assertEquals("default", matcherWithProperty.group("INSTANCEGROUP"));
229+
assertEquals("singers-db", matcherWithProperty.group("DATABASEGROUP"));
230+
Matcher matcherWithoutPort =
231+
EXTERNAL_HOST_URL_PATTERN.matcher(
232+
"jdbc:cloudspanner://localhost/instances/default/databases/test-db");
233+
assertTrue(matcherWithoutPort.matches());
234+
assertEquals("default", matcherWithoutPort.group("INSTANCEGROUP"));
235+
assertEquals("test-db", matcherWithoutPort.group("DATABASEGROUP"));
236+
Matcher matcherWithProject =
237+
EXTERNAL_HOST_URL_PATTERN.matcher(
238+
"jdbc:cloudspanner://localhost:15000/projects/default/instances/default/databases/singers-db");
239+
assertFalse(matcherWithProject.matches());
240+
Matcher matcherWithoutHost =
241+
EXTERNAL_HOST_URL_PATTERN.matcher(
242+
"jdbc:cloudspanner:/instances/default/databases/singers-db");
243+
assertFalse(matcherWithoutHost.matches());
244+
}
214245
}

0 commit comments

Comments
 (0)