Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

refactor(rdsdata,redshiftdata): deduplicate sql parameter parsing logic (#3570) (#3577)

Vũ Thế Huy5ae3ed8976b7
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java b/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java
new file mode 100644
index 0000000000..ff68f17a3c
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java
@@ -0,0 +1,221 @@
+package io.github.hectorvent.floci.core.common;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Translates SQL statements containing named placeholders ({@code :name}) into
+ * JDBC-compatible positional placeholders ({@code ?}), recording placeholder order.
+ *
+ * <p>Handles comments (line {@code --} and block {@code /* ... *\/}), single- and
+ * double-quoted string literals, MySQL backtick identifiers, PostgreSQL dollar-quoted
+ * strings ({@code $tag$...$tag$}), PostgreSQL cast operators ({@code ::}), and
+ * PostgreSQL escape strings ({@code E'...'}).
+ *
+ * <p>Shared across RDS Data API and Redshift Data API implementations.
+ */
+public final class SqlParameterParser {
+
+ private SqlParameterParser() {
+ }
+
+ /**
+ * Dialect-specific quoting and escape rules for SQL parameter parsing.
+ *
+ * @param allowBackticks whether backticks ({@code `}) delimit identifiers (e.g. MySQL / RDS Data)
+ * @param backslashEscapes whether backslash escapes characters in string literals (e.g. MySQL)
+ * @param escapeStrings whether PostgreSQL escape strings ({@code E'...'}/{@code e'...'})
+ * honor backslash escapes (e.g. Redshift Data API)
+ */
+ public record Options(boolean allowBackticks, boolean backslashEscapes, boolean escapeStrings) {
+ public static final Options REDSHIFT = new Options(false, false, true);
+ public static final Options RDS_MYSQL = new Options(true, true, false);
+ public static final Options RDS_POSTGRESQL = new Options(true, false, false);
+ }
+
+ /**
+ * SQL rewritten with positional {@code ?} placeholders, plus the ordered
+ * list of parameter names each placeholder was derived from.
+ * A name repeats once per occurrence in the original SQL.
+ */
+ public record ParsedSql(String sql, List<String> parameterOrder) {
+ }
+
+ /**
+ * 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 according
+ * to {@code options}.
+ */
+ public static ParsedSql parse(String sql, Options options) {
+ StringBuilder out = new StringBuilder(sql.length());
+ List<String> order = new ArrayList<>();
+ int len = sql.length();
+ int i = 0;
+ while (i < len) {
+ char c = sql.charAt(i);
+
+ if (c == '-' && i + 1 < len && sql.charAt(i + 1) == '-') {
+ int end = sql.indexOf('\n', i);
+ end = end < 0 ? len : end;
+ out.append(sql, i, end);
+ i = end;
+ continue;
+ }
+
+ if (c == '/' && i + 1 < len && sql.charAt(i + 1) == '*') {
+ int end = sql.indexOf("*/", i + 2);
+ end = end < 0 ? len : end + 2;
+ out.append(sql, i, end);
+ i = end;
+ continue;
+ }
+
+ if (c == '\'' || c == '"' || (options.allowBackticks() && c == '`')) {
+ int end = skipQuoted(sql, i, c, options);
+ out.append(sql, i, end);
+ i = end;
+ continue;
+ }
+
+ if (c == '$') {
+ int consumed = skipDollarQuoted(sql, i);
+ if (consumed > i) {
+ out.append(sql, i, consumed);
+ i = consumed;
+ continue;
+ }
+ out.append(c);
+ i++;
+ continue;
+ }
+
+ if (c == ':') {
+ if (i + 1 < len && sql.charAt(i + 1) == ':') {
+ out.append("::");
+ i += 2;
+ continue;
+ }
+ if (i + 1 < len && isNameStart(sql.charAt(i + 1))) {
+ int j = i + 1;
+ while (j < len && isNamePart(sql.charAt(j))) {
+ j++;
+ }
+ order.add(sql.substring(i + 1, j));
+ out.append('?');
+ i = j;
+ continue;
+ }
+ }
+
+ out.append(c);
+ i++;
+ }
+ return new ParsedSql(out.toString(), order);
+ }
+
+ /**
+ * Whether {@code sql} holds more than one statement, applying the same
+ * literal, identifier, comment, and dollar-quote skipping as {@link #parse}
+ * so a {@code ;} inside any of those is not counted. Trailing {@code ;}
+ * characters (with only whitespace after) are permitted.
+ */
+ public static boolean isMultiStatement(String sql, Options options) {
+ int len = sql.length();
+ int i = 0;
+ boolean sawSemicolon = false;
+ while (i < len) {
+ char c = sql.charAt(i);
+
+ if (c == '-' && i + 1 < len && sql.charAt(i + 1) == '-') {
+ int end = sql.indexOf('\n', i);
+ i = end < 0 ? len : end;
+ continue;
+ }
+
+ if (c == '/' && i + 1 < len && sql.charAt(i + 1) == '*') {
+ int end = sql.indexOf("*/", i + 2);
+ i = end < 0 ? len : end + 2;
+ continue;
+ }
+
+ if (c == '\'' || c == '"' || (options.allowBackticks() && c == '`')) {
+ i = skipQuoted(sql, i, c, options);
+ continue;
+ }
+
+ if (c == '$') {
+ int consumed = skipDollarQuoted(sql, i);
+ if (consumed > i) {
+ i = consumed;
+ continue;
+ }
+ }
+
+ if (c == ';') {
+ sawSemicolon = true;
+ } else if (sawSemicolon && !Character.isWhitespace(c)) {
+ return true;
+ }
+
+ i++;
+ }
+ return false;
+ }
+
+ private static int skipQuoted(String sql, int start, char quote, Options options) {
+ int len = sql.length();
+ boolean escapable = (options.backslashEscapes() && quote != '`')
+ || (options.escapeStrings() && isEscapeStringStart(sql, start, quote));
+ int i = start + 1;
+ while (i < len) {
+ char c = sql.charAt(i);
+ if (escapable && c == '\\' && i + 1 < len) {
+ i += 2;
+ continue;
+ }
+ if (c == quote) {
+ if (i + 1 < len && sql.charAt(i + 1) == quote) {
+ i += 2;
+ continue;
+ }
+ return i + 1;
+ }
+ i++;
+ }
+ return i;
+ }
+
+ private static int skipDollarQuoted(String sql, int start) {
+ int len = sql.length();
+ int tagEnd = start + 1;
+ while (tagEnd < len && isNamePart(sql.charAt(tagEnd))) {
+ tagEnd++;
+ }
+ if (tagEnd >= len || sql.charAt(tagEnd) != '$') {
+ return start;
+ }
+ String tag = sql.substring(start, tagEnd + 1);
+ int close = sql.indexOf(tag, tagEnd + 1);
+ return close < 0 ? len : close + tag.length();
+ }
+
+ private static boolean isEscapeStringStart(String sql, int quotePos, char quote) {
+ if (quote != '\'' || quotePos == 0) {
+ return false;
+ }
+ char prefix = sql.charAt(quotePos - 1);
+ if (prefix != 'e' && prefix != 'E') {
+ return false;
+ }
+ return quotePos - 1 == 0 || !isNamePart(sql.charAt(quotePos - 2));
+ }
+
+ private static boolean isNameStart(char c) {
+ return Character.isLetter(c) || c == '_';
+ }
+
+ private static boolean isNamePart(char c) {
+ return Character.isLetterOrDigit(c) || c == '_';
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
index 54afbfd35b..dabbcf201f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
@@ -6,10 +6,11 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.Resettable;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
-import io.github.hectorvent.floci.core.common.Resettable;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
@@ -243,7 +244,7 @@ public class RdsDataService implements Resettable {
private ObjectNode executeOnConnection(Connection connection, DatabaseEngine engine, String sql,
Map<String, JsonNode> parameters, boolean includeMetadata)
throws SQLException {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(sql, usesBackslashEscapes(engine));
+ ParsedSql parsed = RdsDataSqlParameters.parse(sql, usesBackslashEscapes(engine));
try (PreparedStatement statement = prepare(connection, engine, parsed.sql())) {
RdsDataSqlParameters.bind(statement, parsed.parameterOrder(), parameters);
return buildResponse(statement, statement.execute(), includeMetadata, engine);
@@ -274,7 +275,7 @@ public class RdsDataService implements Resettable {
if (parameterSets.isEmpty()) {
return batchResponse(updateResults);
}
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(sql, usesBackslashEscapes(engine));
+ ParsedSql parsed = RdsDataSqlParameters.parse(sql, usesBackslashEscapes(engine));
try (PreparedStatement statement = prepare(connection, engine, parsed.sql())) {
rejectStatementReturningRows(statement, parsed.sql());
if (returnsGeneratedKeys(engine)) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParameters.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParameters.java
index 5b54714d20..fdfad2236a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParameters.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParameters.java
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.rdsdata;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.SqlParameterParser;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
import java.io.IOException;
import java.math.BigDecimal;
@@ -11,7 +13,6 @@ import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
-import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -33,14 +34,6 @@ final class RdsDataSqlParameters {
private RdsDataSqlParameters() {
}
- /**
- * SQL rewritten with positional {@code ?} placeholders, plus the ordered
- * list of the Data API parameter names each placeholder was derived from.
- * A name repeats once per occurrence in the original SQL.
- */
- record ParsedSql(String sql, List<String> parameterOrder) {
- }
-
/**
* Rewrites {@code :name} placeholders to positional {@code ?} without
* treating backslash as a string-literal escape (the PostgreSQL default
@@ -55,76 +48,11 @@ final class RdsDataSqlParameters {
* 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.
- *
- * @param backslashEscapes when {@code true}, a backslash inside a single- or
- * double-quoted string escapes the next character (MySQL/MariaDB
- * default, i.e. {@code NO_BACKSLASH_ESCAPES} disabled). Backtick
- * identifiers never honor backslash escaping.
*/
static ParsedSql parse(String sql, boolean backslashEscapes) {
- StringBuilder out = new StringBuilder(sql.length());
- List<String> order = new ArrayList<>();
- int len = sql.length();
- int i = 0;
- while (i < len) {
- char c = sql.charAt(i);
-
- if (c == '-' && i + 1 < len && sql.charAt(i + 1) == '-') {
- int end = sql.indexOf('\n', i);
- if (end < 0) {
- end = len;
- }
- out.append(sql, i, end);
- i = end;
- continue;
- }
-
- if (c == '/' && i + 1 < len && sql.charAt(i + 1) == '*') {
- int end = sql.indexOf("*/", i + 2);
- end = end < 0 ? len : end + 2;
- out.append(sql, i, end);
- i = end;
- continue;
- }
-
- if (c == '\'' || c == '"' || c == '`') {
- i = copyQuoted(sql, i, c, out, backslashEscapes);
- continue;
- }
-
- if (c == '$') {
- int consumed = copyDollarQuoted(sql, i, out);
- if (consumed > i) {
- i = consumed;
- continue;
- }
- out.append(c);
- i++;
- continue;
- }
-
- if (c == ':') {
- if (i + 1 < len && sql.charAt(i + 1) == ':') {
- out.append("::");
- i += 2;
- continue;
- }
- if (i + 1 < len && isNameStart(sql.charAt(i + 1))) {
- int j = i + 1;
- while (j < len && isNamePart(sql.charAt(j))) {
- j++;
- }
- order.add(sql.substring(i + 1, j));
- out.append('?');
- i = j;
- continue;
- }
- }
-
- out.append(c);
- i++;
- }
- return new ParsedSql(out.toString(), order);
+ return SqlParameterParser.parse(sql, backslashEscapes
+ ? SqlParameterParser.Options.RDS_MYSQL
+ : SqlParameterParser.Options.RDS_POSTGRESQL);
}
/**
@@ -216,56 +144,6 @@ final class RdsDataSqlParameters {
}
}
- private static int copyQuoted(String sql, int start, char quote, StringBuilder out, boolean backslashEscapes) {
- int len = sql.length();
- boolean escapable = backslashEscapes && quote != '`';
- out.append(quote);
- int i = start + 1;
- while (i < len) {
- char c = sql.charAt(i);
- out.append(c);
- if (escapable && c == '\\' && i + 1 < len) {
- out.append(sql.charAt(i + 1));
- i += 2;
- continue;
- }
- if (c == quote) {
- if (i + 1 < len && sql.charAt(i + 1) == quote) {
- out.append(quote);
- i += 2;
- continue;
- }
- return i + 1;
- }
- i++;
- }
- return i;
- }
-
- private static int copyDollarQuoted(String sql, int start, StringBuilder out) {
- int len = sql.length();
- int tagEnd = start + 1;
- while (tagEnd < len && isNamePart(sql.charAt(tagEnd))) {
- tagEnd++;
- }
- if (tagEnd >= len || sql.charAt(tagEnd) != '$') {
- return start;
- }
- String tag = sql.substring(start, tagEnd + 1);
- int close = sql.indexOf(tag, tagEnd + 1);
- int end = close < 0 ? len : close + tag.length();
- out.append(sql, start, end);
- return end;
- }
-
- private static boolean isNameStart(char c) {
- return Character.isLetter(c) || c == '_';
- }
-
- private static boolean isNamePart(char c) {
- return Character.isLetterOrDigit(c) || c == '_';
- }
-
private static String text(JsonNode node, String field) {
JsonNode value = node.get(field);
return value == null || value.isNull() ? null : value.asText();
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java
index f6603aa961..ff1492939b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Resettable;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -104,7 +105,7 @@ public class RedshiftDataService implements Resettable {
private void runOnConnection(RedshiftDataStatementStore.StoredStatement stored,
Connection connection, String sql, Map<String, String> parameters)
throws SQLException {
- RedshiftDataSqlParameters.ParsedSql parsed = RedshiftDataSqlParameters.parse(sql);
+ ParsedSql parsed = RedshiftDataSqlParameters.parse(sql);
long t0 = System.nanoTime();
try (PreparedStatement statement = connection.prepareStatement(parsed.sql())) {
RedshiftDataSqlParameters.bind(statement, parsed.parameterOrder(), parameters);
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParameters.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParameters.java
index 44d0148ec7..5d0714aebe 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParameters.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParameters.java
@@ -2,11 +2,12 @@ package io.github.hectorvent.floci.services.redshiftdata;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.SqlParameterParser;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
-import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -26,9 +27,6 @@ final class RedshiftDataSqlParameters {
private RedshiftDataSqlParameters() {
}
- record ParsedSql(String sql, List<String> parameterOrder) {
- }
-
/**
* Rewrites {@code :name} placeholders to positional {@code ?}, skipping over
* string literals, quoted identifiers, line and block comments, PostgreSQL
@@ -37,61 +35,7 @@ final class RedshiftDataSqlParameters {
* (the PostgreSQL default with {@code standard_conforming_strings} on).
*/
static ParsedSql parse(String sql) {
- StringBuilder out = new StringBuilder(sql.length());
- List<String> order = new ArrayList<>();
- int len = sql.length();
- int i = 0;
- while (i < len) {
- char c = sql.charAt(i);
- if (c == '-' && i + 1 < len && sql.charAt(i + 1) == '-') {
- int end = sql.indexOf('\n', i);
- end = end < 0 ? len : end;
- out.append(sql, i, end);
- i = end;
- continue;
- }
- if (c == '/' && i + 1 < len && sql.charAt(i + 1) == '*') {
- int end = sql.indexOf("*/", i + 2);
- end = end < 0 ? len : end + 2;
- out.append(sql, i, end);
- i = end;
- continue;
- }
- if (c == '\'' || c == '"') {
- i = copyQuoted(sql, i, c, out);
- continue;
- }
- if (c == '$') {
- int consumed = copyDollarQuoted(sql, i, out);
- if (consumed > i) {
- i = consumed;
- continue;
- }
- out.append(c);
- i++;
- continue;
- }
- if (c == ':') {
- if (i + 1 < len && sql.charAt(i + 1) == ':') {
- out.append("::");
- i += 2;
- continue;
- }
- if (i + 1 < len && isNameStart(sql.charAt(i + 1))) {
- int j = i + 1;
- while (j < len && isNamePart(sql.charAt(j))) {
- j++;
- }
- order.add(sql.substring(i + 1, j));
- out.append('?');
- i = j;
- continue;
- }
- }
- out.append(c);
- i++;
- }
- return new ParsedSql(out.toString(), order);
+ return SqlParameterParser.parse(sql, SqlParameterParser.Options.REDSHIFT);
}
/**
@@ -150,113 +94,6 @@ final class RedshiftDataSqlParameters {
* characters (with only whitespace after) are permitted.
*/
static boolean isMultiStatement(String sql) {
- int len = sql.length();
- int i = 0;
- boolean sawSemicolon = false;
- while (i < len) {
- char c = sql.charAt(i);
- if (c == '-' && i + 1 < len && sql.charAt(i + 1) == '-') {
- int end = sql.indexOf('\n', i);
- i = end < 0 ? len : end;
- continue;
- }
- if (c == '/' && i + 1 < len && sql.charAt(i + 1) == '*') {
- int end = sql.indexOf("*/", i + 2);
- i = end < 0 ? len : end + 2;
- continue;
- }
- if (c == '\'' || c == '"') {
- i = skipQuoted(sql, i, c, isEscapeStringStart(sql, i, c));
- continue;
- }
- if (c == '$') {
- int consumed = skipDollarQuoted(sql, i);
- if (consumed > i) {
- i = consumed;
- continue;
- }
- }
- if (c == ';') {
- sawSemicolon = true;
- } else if (sawSemicolon && !Character.isWhitespace(c)) {
- return true;
- }
- i++;
- }
- return false;
- }
-
- /**
- * Whether the quote at {@code quotePos} opens a PostgreSQL escape string
- * ({@code E'...'} / {@code e'...'}), inside which a backslash escapes the
- * next character. Plain {@code '...'} literals do not process backslashes
- * under {@code standard_conforming_strings}.
- */
- private static boolean isEscapeStringStart(String sql, int quotePos, char quote) {
- if (quote != '\'' || quotePos == 0) {
- return false;
- }
- char prefix = sql.charAt(quotePos - 1);
- if (prefix != 'e' && prefix != 'E') {
- return false;
- }
- return quotePos - 1 == 0 || !isNamePart(sql.charAt(quotePos - 2));
- }
-
- private static int skipQuoted(String sql, int start, char quote, boolean backslashEscapes) {
- int len = sql.length();
- int i = start + 1;
- while (i < len) {
- char c = sql.charAt(i);
- if (backslashEscapes && c == '\\' && i + 1 < len) {
- i += 2;
- continue;
- }
- if (c == quote) {
- if (i + 1 < len && sql.charAt(i + 1) == quote) {
- i += 2;
- continue;
- }
- return i + 1;
- }
- i++;
- }
- return i;
- }
-
- private static int skipDollarQuoted(String sql, int start) {
- int len = sql.length();
- int tagEnd = start + 1;
- while (tagEnd < len && isNamePart(sql.charAt(tagEnd))) {
- tagEnd++;
- }
- if (tagEnd >= len || sql.charAt(tagEnd) != '$') {
- return start;
- }
- String tag = sql.substring(start, tagEnd + 1);
- int close = sql.indexOf(tag, tagEnd + 1);
- return close < 0 ? len : close + tag.length();
- }
-
- private static int copyQuoted(String sql, int start, char quote, StringBuilder out) {
- int end = skipQuoted(sql, start, quote, isEscapeStringStart(sql, start, quote));
- out.append(sql, start, end);
- return end;
- }
-
- private static int copyDollarQuoted(String sql, int start, StringBuilder out) {
- int end = skipDollarQuoted(sql, start);
- if (end > start) {
- out.append(sql, start, end);
- }
- return end;
- }
-
- private static boolean isNameStart(char c) {
- return Character.isLetter(c) || c == '_';
- }
-
- private static boolean isNamePart(char c) {
- return Character.isLetterOrDigit(c) || c == '_';
+ return SqlParameterParser.isMultiStatement(sql, SqlParameterParser.Options.REDSHIFT);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java
new file mode 100644
index 0000000000..9421d77b1b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java
@@ -0,0 +1,149 @@
+package io.github.hectorvent.floci.core.common;
+
+import io.github.hectorvent.floci.core.common.SqlParameterParser.Options;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class SqlParameterParserTest {
+
+ @Test
+ void rewritesNamedPlaceholdersToPositional() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select * from t where id = :id and name = :name", Options.REDSHIFT);
+
+ assertEquals("select * from t where id = ? and name = ?", parsed.sql());
+ assertEquals(List.of("id", "name"), parsed.parameterOrder());
+ }
+
+ @Test
+ void repeatsPlaceholderOncePerOccurrence() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select * from t where a = :id or b = :id", Options.REDSHIFT);
+
+ assertEquals("select * from t where a = ? or b = ?", parsed.sql());
+ assertEquals(List.of("id", "id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void ignoresColonsInsideStringLiteralsAndIdentifiersWhenBackticksEnabled() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select ':notparam', \":col:\", `x:y` from t where id = :id", Options.RDS_POSTGRESQL);
+
+ assertEquals("select ':notparam', \":col:\", `x:y` from t where id = ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void redshiftDoesNotTreatBackticksAsQuotedIdentifiers() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select `x:y` from t where id = :id", Options.REDSHIFT);
+
+ assertEquals("select `x?` from t where id = ?", parsed.sql());
+ assertEquals(List.of("y", "id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void preservesPostgresCastOperatorAndCastsParameters() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select id::text from t where created = :ts::timestamp", Options.REDSHIFT);
+
+ assertEquals("select id::text from t where created = ?::timestamp", parsed.sql());
+ assertEquals(List.of("ts"), parsed.parameterOrder());
+ }
+
+ @Test
+ void ignoresColonsInsideComments() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select 1 -- :nope\n/* :also */ where id = :id", Options.REDSHIFT);
+
+ assertEquals("select 1 -- :nope\n/* :also */ where id = ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void ignoresColonsInsideDollarQuotedStrings() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select $tag$ :nope $tag$ where id = :id", Options.REDSHIFT);
+
+ assertEquals("select $tag$ :nope $tag$ where id = ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void treatsBackslashAsEscapeInStringLiteralWhenEnabled() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select * from t where note = 'it\\'s a :id' and id = :id", Options.RDS_MYSQL);
+
+ assertEquals("select * from t where note = 'it\\'s a :id' and id = ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void treatsBackslashQuoteAsClosingQuoteWhenEscapesDisabled() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select 'a\\' as c, :id", Options.RDS_POSTGRESQL);
+
+ assertEquals("select 'a\\' as c, ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void ignoresBackslashInsideBacktickIdentifierEvenWhenEscapesEnabled() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select `a\\` , id from t where id = :id", Options.RDS_MYSQL);
+
+ assertEquals("select `a\\` , id from t where id = ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void backslashEscapedQuoteInsideAnEscapeStringDoesNotEndTheLiteral() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select E'it\\'s :value' as v where id = :id", Options.REDSHIFT);
+
+ assertEquals("select E'it\\'s :value' as v where id = ?", parsed.sql());
+ assertEquals(List.of("id"), parsed.parameterOrder());
+ }
+
+ @Test
+ void rdsPostgresDoesNotTreatEscapeStringsSpecially() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select E'it\\'s :value' as v where id = :id", Options.RDS_POSTGRESQL);
+
+ assertEquals("select E'it\\'s ?' as v where id = :id", parsed.sql());
+ assertEquals(List.of("value"), parsed.parameterOrder());
+ }
+
+ @Test
+ void isMultiStatementIgnoresSemicolonsInsideCommentsLiteralsAndDollarQuotes() {
+ assertFalse(SqlParameterParser.isMultiStatement("select 1 -- a; b\n", Options.REDSHIFT));
+ assertFalse(SqlParameterParser.isMultiStatement("select * from t /* x; y */ where a = 1", Options.REDSHIFT));
+ assertFalse(SqlParameterParser.isMultiStatement("select ';' as sep", Options.REDSHIFT));
+ assertFalse(SqlParameterParser.isMultiStatement("select $tag$a;b$tag$", Options.REDSHIFT));
+ assertFalse(SqlParameterParser.isMultiStatement("select 1;", Options.REDSHIFT));
+ assertFalse(SqlParameterParser.isMultiStatement("select 1 ; \n ", Options.REDSHIFT));
+ }
+
+ @Test
+ void isMultiStatementDetectsARealSecondStatement() {
+ assertTrue(SqlParameterParser.isMultiStatement("select 1; select 2", Options.REDSHIFT));
+ assertTrue(SqlParameterParser.isMultiStatement("insert into t values (1); delete from t", Options.REDSHIFT));
+ }
+
+ @Test
+ void escapeStringWithAnEscapedQuoteIsNotSeenAsMultiStatement() {
+ assertFalse(SqlParameterParser.isMultiStatement("select E'a\\';b' as v", Options.REDSHIFT));
+ }
+
+ @Test
+ void isMultiStatementRespectsBacktickOption() {
+ assertTrue(SqlParameterParser.isMultiStatement("select `a;b`", Options.REDSHIFT));
+ assertFalse(SqlParameterParser.isMultiStatement("select `a;b`", Options.RDS_MYSQL));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParametersTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParametersTest.java
index bc9cf0037a..c7c01cd94c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParametersTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataSqlParametersTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.rdsdata;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
import org.junit.jupiter.api.Test;
import java.util.List;
@@ -10,7 +11,7 @@ class RdsDataSqlParametersTest {
@Test
void rewritesNamedPlaceholdersToPositional() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select * from t where id = :id and name = :name");
assertEquals("select * from t where id = ? and name = ?", parsed.sql());
@@ -19,7 +20,7 @@ class RdsDataSqlParametersTest {
@Test
void repeatsPlaceholderOncePerOccurrence() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select * from t where a = :id or b = :id");
assertEquals("select * from t where a = ? or b = ?", parsed.sql());
@@ -28,7 +29,7 @@ class RdsDataSqlParametersTest {
@Test
void ignoresColonsInsideStringLiteralsAndIdentifiers() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select ':notparam', \":col:\", `x:y` from t where id = :id");
assertEquals("select ':notparam', \":col:\", `x:y` from t where id = ?", parsed.sql());
@@ -37,7 +38,7 @@ class RdsDataSqlParametersTest {
@Test
void preservesPostgresCastOperatorAndCastsParameters() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select id::text from t where created = :ts::timestamp");
assertEquals("select id::text from t where created = ?::timestamp", parsed.sql());
@@ -46,7 +47,7 @@ class RdsDataSqlParametersTest {
@Test
void ignoresColonsInsideComments() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select 1 -- :nope\n/* :also */ where id = :id");
assertEquals("select 1 -- :nope\n/* :also */ where id = ?", parsed.sql());
@@ -55,7 +56,7 @@ class RdsDataSqlParametersTest {
@Test
void ignoresColonsInsideDollarQuotedStrings() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select $tag$ :nope $tag$ where id = :id");
assertEquals("select $tag$ :nope $tag$ where id = ?", parsed.sql());
@@ -64,7 +65,7 @@ class RdsDataSqlParametersTest {
@Test
void treatsBackslashAsEscapeInStringLiteralWhenEnabled() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select * from t where note = 'it\\'s a :id' and id = :id", true);
assertEquals("select * from t where note = 'it\\'s a :id' and id = ?", parsed.sql());
@@ -75,7 +76,7 @@ class RdsDataSqlParametersTest {
void treatsBackslashQuoteAsClosingQuoteWhenEscapesDisabled() {
// PostgreSQL default (standard_conforming_strings on): backslash is literal,
// so the first unescaped quote closes the literal.
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select 'a\\' as c, :id", false);
assertEquals("select 'a\\' as c, ?", parsed.sql());
@@ -84,10 +85,19 @@ class RdsDataSqlParametersTest {
@Test
void ignoresBackslashInsideBacktickIdentifierEvenWhenEscapesEnabled() {
- RdsDataSqlParameters.ParsedSql parsed = RdsDataSqlParameters.parse(
+ ParsedSql parsed = RdsDataSqlParameters.parse(
"select `a\\` , id from t where id = :id", true);
assertEquals("select `a\\` , id from t where id = ?", parsed.sql());
assertEquals(List.of("id"), parsed.parameterOrder());
}
+
+ @Test
+ void postgresModeDoesNotTreatBackslashAsEscapeEvenInEscapeStringLiteral() {
+ ParsedSql parsed = RdsDataSqlParameters.parse(
+ "select E'it\\'s :value' as v where id = :id", false);
+
+ assertEquals("select E'it\\'s ?' as v where id = :id", parsed.sql());
+ assertEquals(List.of("value"), parsed.parameterOrder());
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParametersTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParametersTest.java
index e0bf7b002d..349129abb6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParametersTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSqlParametersTest.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.redshiftdata;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
import org.h2.Driver;
import org.junit.jupiter.api.Test;
@@ -29,7 +30,7 @@ class RedshiftDataSqlParametersTest {
@Test
void rewritesNamedPlaceholdersInOrderAndSkipsLiteralsAndCasts() {
- RedshiftDataSqlParameters.ParsedSql parsed = RedshiftDataSqlParameters.parse(
+ ParsedSql parsed = RedshiftDataSqlParameters.parse(
"select * from t where a = :a and b = ':b' and c = :a and d = 1::int");
assertEquals("select * from t where a = ? and b = ':b' and c = ? and d = 1::int", parsed.sql());
assertEquals(List.of("a", "a"), parsed.parameterOrder());
@@ -41,7 +42,7 @@ class RedshiftDataSqlParametersTest {
try (Connection c = DriverManager.getConnection(url, "sa", "")) {
c.createStatement().execute("create table t (id int, name varchar(20))");
c.createStatement().execute("insert into t values (2, 'bob')");
- RedshiftDataSqlParameters.ParsedSql parsed =
+ ParsedSql parsed =
RedshiftDataSqlParameters.parse("select name from t where id = :id");
try (PreparedStatement ps = c.prepareStatement(parsed.sql())) {
RedshiftDataSqlParameters.bind(ps, parsed.parameterOrder(), Map.of("id", "2"));
@@ -58,7 +59,7 @@ class RedshiftDataSqlParametersTest {
String url = "jdbc:h2:mem:" + UUID.randomUUID() + ";DB_CLOSE_DELAY=-1";
try (Connection c = DriverManager.getConnection(url, "sa", "")) {
c.createStatement().execute("create table t (id int)");
- RedshiftDataSqlParameters.ParsedSql parsed =
+ ParsedSql parsed =
RedshiftDataSqlParameters.parse("select * from t where id = :id");
try (PreparedStatement ps = c.prepareStatement(parsed.sql())) {
AwsException e = assertThrows(AwsException.class,
@@ -98,7 +99,7 @@ class RedshiftDataSqlParametersTest {
@Test
void backslashEscapedQuoteInsideAnEscapeStringDoesNotEndTheLiteral() {
// E'it\'s :value' is one string literal; :value is literal text, not a bind marker.
- RedshiftDataSqlParameters.ParsedSql parsed =
+ ParsedSql parsed =
RedshiftDataSqlParameters.parse("select E'it\\'s :value' as v where id = :id");
assertEquals("select E'it\\'s :value' as v where id = ?", parsed.sql());
assertEquals(List.of("id"), parsed.parameterOrder());
@@ -112,9 +113,19 @@ class RedshiftDataSqlParametersTest {
@Test
void plainLiteralStillTreatsBackslashLiterally() {
// Not an E'' string: backslash is an ordinary character, '' still ends the literal.
- RedshiftDataSqlParameters.ParsedSql parsed =
+ ParsedSql parsed =
RedshiftDataSqlParameters.parse("select 'a\\' , :x");
assertEquals("select 'a\\' , ?", parsed.sql());
assertEquals(List.of("x"), parsed.parameterOrder());
}
+
+ @Test
+ void doesNotTreatBackticksAsQuotedIdentifiers() {
+ ParsedSql parsed =
+ RedshiftDataSqlParameters.parse("select `x:y` from t where id = :id");
+ assertEquals("select `x?` from t where id = ?", parsed.sql());
+ assertEquals(List.of("y", "id"), parsed.parameterOrder());
+
+ assertTrue(RedshiftDataSqlParameters.isMultiStatement("select `a;b`"));
+ }
}