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: support PostgreSQL JDBC 42.7.0 #1208

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
<version>42.7.0</version>
</dependency>
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ boolean isClient(List<String> orderedParameterKeys, Map<String, String> paramete
&& parameters.get("options").contains("spanner.well_known_client")) {
return false;
}
return parameters.get("DateStyle").equals("ISO");
return parameters.get("DateStyle").equals("ISO, MDY")
|| parameters.get("DateStyle").equals("ISO");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
import com.google.cloud.spanner.pgadapter.error.Severity;
import com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse;
import com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Map;
import org.postgresql.util.ReaderInputStream;

/**
* PGAdapter will convert a password message into gRPC authentication in the following ways:
Expand Down Expand Up @@ -142,7 +143,8 @@ private Credentials checkCredentials(String username, String password) {

// Try to parse the password field as a JSON string that contains a credentials object.
try {
return GoogleCredentials.fromStream(new ReaderInputStream(new StringReader(password)));
return GoogleCredentials.fromStream(
new ByteArrayInputStream(password.getBytes(StandardCharsets.UTF_8)));
} catch (IOException ioException) {
// Ignore and fallthrough..
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,10 +966,11 @@ public void testShowValidSetting() throws SQLException {
@Test
public void testShowSettingWithStartupValue() throws SQLException {
try (Connection connection = createConnection()) {
// DATESTYLE is set to 'ISO' by the JDBC driver at startup.
// DATESTYLE is set to 'ISO' by the JDBC driver at startup ('ISO, MDY' in 42.7.0).
try (ResultSet resultSet = connection.createStatement().executeQuery("show DATESTYLE")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString(1));
String dateStyle = resultSet.getString(1);
assertTrue(dateStyle, "ISO".equals(dateStyle) || "ISO, MDY".equals(dateStyle));
assertFalse(resultSet.next());
}
}
Expand Down Expand Up @@ -1054,9 +1055,13 @@ public void testResetValidSetting() throws SQLException {
@Test
public void testResetSettingWithStartupValue() throws SQLException {
try (Connection connection = createConnection()) {
String originalDateStyle;
try (ResultSet resultSet = connection.createStatement().executeQuery("show datestyle")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString(1));
originalDateStyle = resultSet.getString(1);
assertTrue(
originalDateStyle,
"ISO".equals(originalDateStyle) || "ISO, MDY".equals(originalDateStyle));
assertFalse(resultSet.next());
}

Expand All @@ -1072,7 +1077,7 @@ public void testResetSettingWithStartupValue() throws SQLException {

try (ResultSet resultSet = connection.createStatement().executeQuery("show datestyle")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString(1));
assertEquals(originalDateStyle, resultSet.getString(1));
assertFalse(resultSet.next());
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/com/google/cloud/spanner/pgadapter/ITJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1038,13 +1038,18 @@ public void testFetchSize() throws SQLException {
public void testPGSettings() throws SQLException {
try (Connection connection = DriverManager.getConnection(getConnectionUrl())) {
// First verify the default value.
// JDBC sets the DateStyle to 'ISO' for every connection in the connection request.
// JDBC sets the DateStyle to 'ISO' for every connection in the connection request, except in
// version 42.7.0, where the value is 'ISO, MDY'.
String originalDateStyle;
try (ResultSet resultSet =
connection
.createStatement()
.executeQuery("select setting from pg_settings where name='DateStyle'")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString("setting"));
originalDateStyle = resultSet.getString("setting");
assertTrue(
originalDateStyle,
"ISO".equals(originalDateStyle) || "ISO, MDY".equals(originalDateStyle));
assertFalse(resultSet.next());
}
// Verify that we can also use a statement parameter to query the pg_settings table.
Expand All @@ -1053,7 +1058,8 @@ public void testPGSettings() throws SQLException {
preparedStatement.setString(1, "DateStyle");
try (ResultSet resultSet = preparedStatement.executeQuery()) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString("setting"));
String dateStyle = resultSet.getString("setting");
assertTrue(dateStyle, "ISO".equals(dateStyle) || "ISO, MDY".equals(dateStyle));
assertFalse(resultSet.next());
}
}
Expand Down Expand Up @@ -1098,7 +1104,7 @@ public void testPGSettings() throws SQLException {
.createStatement()
.executeQuery("select setting from pg_settings where name='DateStyle'")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString("setting"));
assertEquals(originalDateStyle, resultSet.getString("setting"));
assertFalse(resultSet.next());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.stream.Collectors;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -600,6 +601,7 @@ public void testTwoDdlStatements() throws SQLException {
assertEquals("CREATE TABLE BAR (id bigint primary key)", request.getStatements(1));
}

@Ignore("https://github.com/pgjdbc/pgjdbc/issues/3007")
@Test
public void testBeginTransactionWithOptions() throws SQLException {
String sql = "BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3546,10 +3546,12 @@ public void testShowValidSetting() throws SQLException {
@Test
public void testShowSettingWithStartupValue() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
// DATESTYLE is set to 'ISO' by the JDBC driver at startup.
// DATESTYLE is set to 'ISO' by the JDBC driver at startup, except in version 42.7.0, which
// sets 'ISO, MDY'.
try (ResultSet resultSet = connection.createStatement().executeQuery("show DATESTYLE")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString(1));
String dateStyle = resultSet.getString(1);
assertTrue(dateStyle, "ISO".equals(dateStyle) || "ISO, MDY".equals(dateStyle));
assertFalse(resultSet.next());
}
}
Expand Down Expand Up @@ -3634,9 +3636,13 @@ public void testResetValidSetting() throws SQLException {
@Test
public void testResetSettingWithStartupValue() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
String originalDateStyle;
try (ResultSet resultSet = connection.createStatement().executeQuery("show datestyle")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString(1));
originalDateStyle = resultSet.getString(1);
assertTrue(
originalDateStyle,
"ISO".equals(originalDateStyle) || "ISO, MDY".equals(originalDateStyle));
assertFalse(resultSet.next());
}

Expand All @@ -3652,7 +3658,7 @@ public void testResetSettingWithStartupValue() throws SQLException {

try (ResultSet resultSet = connection.createStatement().executeQuery("show datestyle")) {
assertTrue(resultSet.next());
assertEquals("ISO", resultSet.getString(1));
assertEquals(originalDateStyle, resultSet.getString(1));
assertFalse(resultSet.next());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.TimeZone;
import java.util.stream.Collectors;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -441,7 +442,7 @@ public void testQueryWithParameters() throws SQLException {
// that the type is available.
assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
ExecuteSqlRequest jsonbRequest = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
assertEquals(jsonbRequest.getSql(), SELECT_JSONB_TYPE_BY_NAME_SIMPLE_PROTOCOL.getSql());
assertEquals(SELECT_JSONB_TYPE_BY_NAME_SIMPLE_PROTOCOL.getSql(), jsonbRequest.getSql());

ExecuteSqlRequest request = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(1);
assertEquals(QueryMode.NORMAL, request.getQueryMode());
Expand Down Expand Up @@ -756,6 +757,7 @@ public void testMixedDdlBatchWithStartAndRun() throws SQLException {
assertEquals(0, requests.size());
}

@Ignore("https://github.com/pgjdbc/pgjdbc/issues/3007")
@Test
public void testImplicitBatchOfClientSideStatements() throws SQLException {
String sql = "set statement_timeout = '10s'; " + "show statement_timeout; ";
Expand Down
Loading