Skip to content

Commit c9f12f3

Browse files
sagnghosolavloite
andauthored
chore: support spanner prefix jdbc url (#1927)
* feat: support spanner prefix jdbc url * test: add tests * modified unit test --------- Co-authored-by: Knut Olav Løite <[email protected]>
1 parent 00f163b commit c9f12f3

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public JdbcDriver() {}
216216

217217
@Override
218218
public Connection connect(String url, Properties info) throws SQLException {
219-
if (url != null && url.startsWith("jdbc:cloudspanner")) {
219+
if (url != null && (url.startsWith("jdbc:cloudspanner") || url.startsWith("jdbc:spanner"))) {
220220
try {
221221
Matcher matcher = URL_PATTERN.matcher(url);
222222
Matcher matcherExternalHost = EXTERNAL_HOST_URL_PATTERN.matcher(url);
@@ -271,7 +271,7 @@ private String appendPropertiesToUrl(String url, Properties info) {
271271

272272
@Override
273273
public boolean acceptsURL(String url) {
274-
return URL_PATTERN.matcher(url).matches();
274+
return URL_PATTERN.matcher(url).matches() || EXTERNAL_HOST_URL_PATTERN.matcher(url).matches();
275275
}
276276

277277
@Override

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,14 @@ public void testRegister() throws SQLException {
135135

136136
@Test
137137
public void testConnect() throws SQLException {
138-
try (Connection connection =
139-
DriverManager.getConnection(
140-
String.format(
141-
"jdbc:cloudspanner://localhost:%d/projects/some-company.com:test-project/instances/static-test-instance/databases/test-database;usePlainText=true;credentials=%s",
142-
server.getPort(), TEST_KEY_PATH))) {
143-
assertThat(connection.isClosed()).isFalse();
144-
}
138+
for (String prefix : new String[] {"cloudspanner", "spanner"})
139+
try (Connection connection =
140+
DriverManager.getConnection(
141+
String.format(
142+
"jdbc:%s://localhost:%d/projects/some-company.com:test-project/instances/static-test-instance/databases/test-database;usePlainText=true;credentials=%s",
143+
prefix, server.getPort(), TEST_KEY_PATH))) {
144+
assertThat(connection.isClosed()).isFalse();
145+
}
145146
}
146147

147148
@Test(expected = SQLException.class)
@@ -215,6 +216,17 @@ public void testLenient() throws SQLException {
215216
}
216217
}
217218

219+
@Test
220+
public void testAcceptsURL() throws SQLException {
221+
JdbcDriver driver = JdbcDriver.getRegisteredDriver();
222+
assertTrue(
223+
driver.acceptsURL(
224+
"jdbc:cloudspanner:/projects/my-project/instances/my-instance/databases/my-database"));
225+
assertTrue(
226+
driver.acceptsURL(
227+
"jdbc:spanner:/projects/my-project/instances/my-instance/databases/my-database"));
228+
}
229+
218230
@Test
219231
public void testJdbcExternalHostFormat() {
220232
Matcher matcherWithoutInstance =

0 commit comments

Comments
 (0)