fix(rdsdata): escape PostgreSQL question-mark operators (#7632)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-06 08:55:58 -04:00
1 parent 576646aae7
commit 7f43385349
4 files changed
+122 -19

No files matched your search

@@ -32,12 +32,13 @@ public final class SqlParameterParser {
* @param numericNames whether a parameter name may start with a digit, as in
* {@code :1} (RDS Data API)
* @param mysqlComments whether comments follow MySQL's hash, double-dash, and non-nesting rules
* @param escapeQuestionMarks whether literal {@code ?} operators need pgjdbc's {@code ??} escape
*/
public record Options(boolean allowBackticks, boolean backslashEscapes, boolean escapeStrings,
boolean numericNames, boolean mysqlComments) {
public static final Options REDSHIFT = new Options(false, false, true, true, false);
public static final Options RDS_MYSQL = new Options(true, true, false, true, true);
public static final Options RDS_POSTGRESQL = new Options(true, false, false, true, false);
boolean numericNames, boolean mysqlComments, boolean escapeQuestionMarks) {
public static final Options REDSHIFT = new Options(false, false, true, true, false, false);
public static final Options RDS_MYSQL = new Options(true, true, false, true, true, false);
public static final Options RDS_POSTGRESQL = new Options(true, false, true, true, false, true);
}
/**
@@ -165,6 +166,9 @@ public final class SqlParameterParser {
}
order.add(sql.substring(i + 1, j));
out.append('?');
if (options.escapeQuestionMarks() && j < len && sql.charAt(j) == '?') {
out.append(' ');
}
i = j;
afterOperand = true;
subscriptStart = false;
@@ -177,6 +181,11 @@ public final class SqlParameterParser {
afterOperand = false;
subscriptStart = false;
}
if (c == '?' && options.escapeQuestionMarks()) {
out.append("??");
i++;
continue;
}
out.append(c);
i++;
}
@@ -25,9 +25,9 @@ import java.util.UUID;
* <p>The Data API uses named placeholders ({@code :name}); JDBC uses positional
* {@code ?}. {@link #parse(String)} rewrites the SQL to positional form while
* recording the placeholder order, and {@link #bind} binds each value variant.
* The rewrite is shared across MySQL, MariaDB, and PostgreSQL; the only
* dialect difference is whether a backslash escapes quotes inside string
* literals (see {@link #parse(String, boolean)}).
* The rewrite is shared across MySQL, MariaDB, and PostgreSQL. PostgreSQL
* escape strings and pgjdbc's {@code ??} escape for literal question-mark
* operators are handled separately from MySQL and MariaDB quoting rules.
*/
final class RdsDataSqlParameters {
@@ -36,8 +36,9 @@ final class RdsDataSqlParameters {
/**
* Rewrites {@code :name} placeholders to positional {@code ?} without
* treating backslash as a string-literal escape (the PostgreSQL default
* with {@code standard_conforming_strings} on).
* treating backslash as an escape in ordinary string literals (the PostgreSQL
* default with {@code standard_conforming_strings} on). {@code E'...'}
* escape strings still use backslash escapes.
*/
static ParsedSql parse(String sql) {
return parse(sql, false);
@@ -47,11 +48,12 @@ final class RdsDataSqlParameters {
* Rewrites {@code :name} placeholders to positional {@code ?}, skipping over
* string literals, quoted/backtick identifiers, line and block comments,
* PostgreSQL {@code ::} casts, and PostgreSQL dollar-quoted strings so a
* colon inside any of those is left untouched.
* colon inside any of those is left untouched. PostgreSQL question-mark
* operators are escaped for pgjdbc without changing generated bindings.
*/
static ParsedSql parse(String sql, boolean backslashEscapes) {
SqlParameterParser.Options statementOptions = new SqlParameterParser.Options(
true, backslashEscapes, !backslashEscapes, true, backslashEscapes);
true, backslashEscapes, !backslashEscapes, true, backslashEscapes, !backslashEscapes);
if (SqlParameterParser.isMultiStatement(sql, statementOptions)) {
throw new AwsException("ValidationException", "Multistatements aren't supported.", 400);
}