mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat: support parameters in Redshift Data API batches (#6475)
This commit is contained in:
1 parent
2525e38179
commit
36e244f104
9 files changed
+199
-13
No files matched your search
@@ -35,7 +35,7 @@ public final class SqlParameterParser {
|
||||
*/
|
||||
public record Options(boolean allowBackticks, boolean backslashEscapes, boolean escapeStrings,
|
||||
boolean numericNames, boolean mysqlComments) {
|
||||
public static final Options REDSHIFT = new Options(false, false, true, false, false);
|
||||
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);
|
||||
}
|
||||
|
||||
+11
-8
@@ -382,11 +382,7 @@ public class RedshiftDataService implements Resettable {
|
||||
stored.dbUser = target.user();
|
||||
stored.database = target.database();
|
||||
stored.resultFormat = request.path("ResultFormat").asText("JSON");
|
||||
if (!parameters.isEmpty()) {
|
||||
stored.queryParameters = parameters.entrySet().stream()
|
||||
.map(entry -> new StoredParameter(entry.getKey(), entry.getValue()))
|
||||
.toList();
|
||||
}
|
||||
stored.queryParameters = storedParameters(parameters);
|
||||
capture(stored, target, region, request);
|
||||
|
||||
return new PendingStatement(stored, (execution, session) -> runStatement(stored, target, sql, parameters, execution, session),
|
||||
@@ -792,6 +788,7 @@ public class RedshiftDataService implements Resettable {
|
||||
for (JsonNode n : sqlsNode) {
|
||||
sqls.add(n.asText());
|
||||
}
|
||||
Map<String, String> parameters = RedshiftDataSqlParameters.parseBatchParameters(request, sqls);
|
||||
|
||||
ResolvedTarget resolved = resolveTarget(request, region, owner);
|
||||
RedshiftDataResourceResolver.DatabaseTarget target = resolved.target();
|
||||
@@ -806,13 +803,14 @@ public class RedshiftDataService implements Resettable {
|
||||
parent.database = target.database();
|
||||
parent.resultFormat = request.path("ResultFormat").asText("JSON");
|
||||
parent.subStatements = new ArrayList<>();
|
||||
parent.queryParameters = storedParameters(parameters);
|
||||
capture(parent, target, region, request);
|
||||
|
||||
return new PendingStatement(parent, (execution, session) -> runBatch(parent, sqls, target, execution, session),
|
||||
return new PendingStatement(parent, (execution, session) -> runBatch(parent, sqls, parameters, target, execution, session),
|
||||
target, request.deepCopy(), owner);
|
||||
}
|
||||
|
||||
private void runBatch(RedshiftDataStatementStore.StoredStatement parent, List<String> sqls,
|
||||
private void runBatch(RedshiftDataStatementStore.StoredStatement parent, List<String> sqls, Map<String, String> parameters,
|
||||
RedshiftDataResourceResolver.DatabaseTarget target, RedshiftDataExecution execution,
|
||||
RedshiftDataSessions.Reservation session) {
|
||||
long totalDuration = 0;
|
||||
@@ -849,7 +847,7 @@ public class RedshiftDataService implements Resettable {
|
||||
sub.updatedAt = now;
|
||||
try {
|
||||
execution.checkCancellation();
|
||||
runOnConnection(sub, connection, sqls.get(n), Map.of(), target.spectrum(), execution, false);
|
||||
runOnConnection(sub, connection, sqls.get(n), parameters, target.spectrum(), execution, false);
|
||||
} catch (SQLException e) {
|
||||
sub.status = RedshiftDataStatementStore.Status.FAILED;
|
||||
sub.error = e.getMessage();
|
||||
@@ -1238,6 +1236,11 @@ public class RedshiftDataService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
private static List<StoredParameter> storedParameters(Map<String, String> parameters) {
|
||||
return parameters.isEmpty() ? null : parameters.entrySet().stream()
|
||||
.map(entry -> new StoredParameter(entry.getKey(), entry.getValue())).toList();
|
||||
}
|
||||
|
||||
private static void putQueryParameters(ObjectNode target, List<StoredParameter> parameters) {
|
||||
if (parameters == null || parameters.isEmpty()) {
|
||||
return;
|
||||
|
||||
+41
@@ -9,8 +9,11 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Translates Redshift Data API {@code SqlParameter} bindings into JDBC
|
||||
@@ -24,6 +27,8 @@ import java.util.Map;
|
||||
*/
|
||||
final class RedshiftDataSqlParameters {
|
||||
|
||||
private static final Pattern PARAMETER_NAME = Pattern.compile("[0-9a-zA-Z_]+");
|
||||
|
||||
private RedshiftDataSqlParameters() {
|
||||
}
|
||||
|
||||
@@ -87,6 +92,42 @@ final class RedshiftDataSqlParameters {
|
||||
return byName;
|
||||
}
|
||||
|
||||
static Map<String, String> parseBatchParameters(JsonNode request, List<String> sqls) {
|
||||
JsonNode supplied = request.get("Parameters");
|
||||
if (supplied != null && !supplied.isNull()) {
|
||||
if (!supplied.isArray() || supplied.isEmpty()) {
|
||||
throw new AwsException("ValidationException", "Parameters must be a non-empty array of {name, value} objects.", 400);
|
||||
}
|
||||
for (JsonNode parameter : supplied) {
|
||||
JsonNode name = parameter.path("name");
|
||||
JsonNode value = parameter.path("value");
|
||||
if (!name.isTextual() || !PARAMETER_NAME.matcher(name.textValue()).matches()
|
||||
|| !value.isTextual() || value.textValue().isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Each parameter must have a name matching [0-9a-zA-Z_]+ and a non-empty string value.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, String> parameters = parseParameters(request, "Parameters");
|
||||
Set<String> referenced = new LinkedHashSet<>();
|
||||
for (String sql : sqls) {
|
||||
referenced.addAll(parse(sql).parameterOrder());
|
||||
}
|
||||
for (String name : referenced) {
|
||||
if (!parameters.containsKey(name)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"SQL references parameter :" + name + " but no matching value was supplied.", 400);
|
||||
}
|
||||
}
|
||||
for (String name : parameters.keySet()) {
|
||||
if (!referenced.contains(name)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Parameter :" + name + " is not referenced by any SQL statement in the batch.", 400);
|
||||
}
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code sql} holds more than one statement, applying the same
|
||||
* literal / identifier / comment / dollar-quote skipping as {@link #parse}
|
||||
|
||||
Reference in new issue
Block a user