Skip to content

Commit 7c9e781

Browse files
authored
fix: comments should be sent to Spanner for PostgreSQL databases (#1331)
Comments for PostgreSQL-dialect databases should be included in the request that is sent to Cloud Spanner. This is needed, because for PostgreSQL comments can contain query hints.
1 parent c50da41 commit 7c9e781

File tree

2 files changed

+165
-124
lines changed

2 files changed

+165
-124
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner.jdbc;
1818

19+
import com.google.cloud.spanner.Dialect;
1920
import com.google.cloud.spanner.Options.QueryOption;
2021
import com.google.cloud.spanner.PartitionOptions;
2122
import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode;
@@ -38,7 +39,6 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement
3839
implements CloudSpannerJdbcPreparedStatement {
3940
private static final char POS_PARAM_CHAR = '?';
4041
private final String sql;
41-
private final String sqlWithoutComments;
4242
private final ParametersInfo parameters;
4343
private final ImmutableList<String> generatedKeysColumns;
4444

@@ -48,9 +48,15 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement
4848
super(connection);
4949
this.sql = sql;
5050
try {
51-
this.sqlWithoutComments = parser.removeCommentsAndTrim(this.sql);
51+
// The PostgreSQL parser allows comments to be present in the SQL string that is used to parse
52+
// the query parameters.
53+
String sqlForParameterExtraction =
54+
getConnection().getDialect() == Dialect.POSTGRESQL
55+
? this.sql
56+
: parser.removeCommentsAndTrim(this.sql);
5257
this.parameters =
53-
parser.convertPositionalParametersToNamedParameters(POS_PARAM_CHAR, sqlWithoutComments);
58+
parser.convertPositionalParametersToNamedParameters(
59+
POS_PARAM_CHAR, sqlForParameterExtraction);
5460
} catch (SpannerException e) {
5561
throw JdbcSqlExceptionFactory.of(e);
5662
}

0 commit comments

Comments
 (0)