Skip to content

Commit 2a3f2eb

Browse files
authored
fix: allow QueryMessage with empty query string (#113)
PostgreSQL accepts QueryMessages with an empty query string. PGAdapter now also accepts these and sends back a NoDataResponse without sending the statement to Spanner.
1 parent f7d05b2 commit 2a3f2eb

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/main/java/com/google/cloud/spanner/pgadapter/statements/IntermediateStatement.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,14 @@ private void executeSingleStatement(int index) {
411411
executionStatus = ExecutionStatus.AUTOCOMMIT;
412412
}
413413
}
414-
try {
415-
StatementResult result = this.connection.execute(Statement.of(statement));
416-
updateResultCount(index, result);
417-
} catch (SpannerException e) {
418-
handleExecutionExceptionAndTransactionStatus(index, e);
414+
// Ignore empty statements.
415+
if (!"".equals(statement)) {
416+
try {
417+
StatementResult result = this.connection.execute(Statement.of(statement));
418+
updateResultCount(index, result);
419+
} catch (SpannerException e) {
420+
handleExecutionExceptionAndTransactionStatus(index, e);
421+
}
419422
}
420423
}
421424

src/test/java/com/google/cloud/spanner/pgadapter/JdbcSimpleModeMockServerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ public void testQuery() throws SQLException {
9292
assertTrue(request.getTransaction().getSingleUse().hasReadOnly());
9393
}
9494

95+
@Test
96+
public void testEmptyStatement() throws SQLException {
97+
String sql = "";
98+
99+
try (Connection connection = DriverManager.getConnection(createUrl())) {
100+
assertFalse(connection.createStatement().execute(sql));
101+
}
102+
103+
// An empty statement is not sent to Spanner.
104+
assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
105+
}
106+
95107
@Test
96108
public void testWrongDialect() {
97109
// Let the mock server respond with the Google SQL dialect instead of PostgreSQL. The

0 commit comments

Comments
 (0)