Skip to content

Commit

Permalink
fix: accept statements with only comments (#1122)
Browse files Browse the repository at this point in the history
Some drivers send a statement containing only a comment in order
to ping the server. These statements would return an error on
PGAdapter. This would cause the newest pgx driver to fail with
a 'driver: bad connection' error.
  • Loading branch information
olavloite authored Oct 14, 2023
1 parent 0ffa659 commit 7439aac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void execute() {
// Ignore the statement as it is a no-op to execute COMMIT/ROLLBACK when we are not in a
// transaction. TODO: Return a warning.
result.set(NO_RESULT);
} else if (statement.getSql().isEmpty()) {
} else if (parsedStatement.getSqlWithoutComments().isEmpty()) {
result.set(NO_RESULT);
} else if (parsedStatement.isDdl()) {
if (analyze) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,30 @@ public void testEmptyStatement() throws SQLException {
assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
}

@Test
public void testEmptyStatementWithSemiColon() throws SQLException {
String sql = ";";

try (Connection connection = DriverManager.getConnection(createUrl())) {
assertFalse(connection.createStatement().execute(sql));
}

// An empty statement is not sent to Spanner.
assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
}

@Test
public void testPing() throws SQLException {
String sql = "-- ping";

try (Connection connection = DriverManager.getConnection(createUrl())) {
assertFalse(connection.createStatement().execute(sql));
}

// An empty statement is not sent to Spanner.
assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
}

@Test
public void testInvalidDml() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,9 @@ public void testQueryResult() {
@Test
public void testGeneralException() {
Connection connection = mock(Connection.class);
ParsedStatement parsedStatement = mock(ParsedStatement.class);
Statement statement = Statement.of("select foo from bar");
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
RuntimeException error = new RuntimeException("test error");
when(connection.execute(statement)).thenThrow(error);

Expand All @@ -416,8 +417,9 @@ public void testGeneralException() {
@Test
public void testCancelledException() {
Connection connection = mock(Connection.class);
ParsedStatement parsedStatement = mock(ParsedStatement.class);
Statement statement = Statement.of("select foo from bar");
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
SpannerException error =
SpannerExceptionFactory.newSpannerException(ErrorCode.CANCELLED, "query cancelled");
when(connection.execute(statement)).thenThrow(error);
Expand Down

0 comments on commit 7439aac

Please sign in to comment.