Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow unknown properties in connection url with lenient mode #284

Merged
merged 6 commits into from Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java
Expand Up @@ -28,6 +28,7 @@
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.logging.Logger;
Expand Down Expand Up @@ -172,7 +173,11 @@ public Connection connect(String url, Properties info) throws SQLException {
// Connection API
String connectionUri = appendPropertiesToUrl(url.substring(5), info);
ConnectionOptions options = ConnectionOptions.newBuilder().setUri(connectionUri).build();
return new JdbcConnection(url, options);
JdbcConnection connection = new JdbcConnection(url, options);
if (options.getWarnings() != null) {
connection.pushWarning(new SQLWarning(options.getWarnings()));
}
return connection;
}
} catch (SpannerException e) {
throw JdbcSqlExceptionFactory.of(e);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java
Expand Up @@ -161,4 +161,32 @@ public String apply(DriverPropertyInfo input) {
});
assertThat(driverPropertyNames).containsExactlyElementsIn(validConnectionPropertyNames);
}

@Test
public void testLenient() throws SQLException {
// With lenient=true the driver should accept unknown properties and only generate a warning.
try (Connection connection =
DriverManager.getConnection(
String.format(
"jdbc:cloudspanner://localhost:%d/projects/p/instances/i/databases/d?usePlainText=true;credentials=%s;lenient=true;foo=bar",
server.getPort(), TEST_KEY_PATH))) {
assertThat(connection.isClosed()).isFalse();
assertThat((Throwable) connection.getWarnings()).isNotNull();
assertThat(connection.getWarnings().getMessage()).contains("foo");
}

// Without lenient the driver should throw an exception for unknown properties.
try (Connection connection =
DriverManager.getConnection(
String.format(
"jdbc:cloudspanner://localhost:%d/projects/p/instances/i/databases/d?usePlainText=true;credentials=%s;foo=bar",
server.getPort(), TEST_KEY_PATH))) {
fail("missing expected exception");
} catch (SQLException e) {
assertThat((Throwable) e).isInstanceOf(JdbcSqlException.class);
JdbcSqlException jdbc = (JdbcSqlException) e;
assertThat(jdbc.getMessage()).contains("foo");
assertThat(jdbc.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
}
}
}