Skip to content

Commit

Permalink
fix: comments should be sent to Spanner for PostgreSQL databases (#1331)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
olavloite authored Sep 15, 2023
1 parent c50da41 commit 7c9e781
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.jdbc;

import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.PartitionOptions;
import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode;
Expand All @@ -38,7 +39,6 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement
implements CloudSpannerJdbcPreparedStatement {
private static final char POS_PARAM_CHAR = '?';
private final String sql;
private final String sqlWithoutComments;
private final ParametersInfo parameters;
private final ImmutableList<String> generatedKeysColumns;

Expand All @@ -48,9 +48,15 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement
super(connection);
this.sql = sql;
try {
this.sqlWithoutComments = parser.removeCommentsAndTrim(this.sql);
// The PostgreSQL parser allows comments to be present in the SQL string that is used to parse
// the query parameters.
String sqlForParameterExtraction =
getConnection().getDialect() == Dialect.POSTGRESQL
? this.sql
: parser.removeCommentsAndTrim(this.sql);
this.parameters =
parser.convertPositionalParametersToNamedParameters(POS_PARAM_CHAR, sqlWithoutComments);
parser.convertPositionalParametersToNamedParameters(
POS_PARAM_CHAR, sqlForParameterExtraction);
} catch (SpannerException e) {
throw JdbcSqlExceptionFactory.of(e);
}
Expand Down
Loading

0 comments on commit 7c9e781

Please sign in to comment.