From 05951a7023a8fa723a1e7563d22b0289d156b0ac Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 6 Sep 2019 10:27:35 +0200 Subject: [PATCH] (citrusframework/citrus-db#29) resolve named and indexed parameters in correct order --- .../driver/statement/StatementComposer.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/driver/src/main/java/com/consol/citrus/db/driver/statement/StatementComposer.java b/driver/src/main/java/com/consol/citrus/db/driver/statement/StatementComposer.java index dd30246..e95ee88 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/statement/StatementComposer.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/statement/StatementComposer.java @@ -1,15 +1,39 @@ package com.consol.citrus.db.driver.statement; +import java.util.Collection; +import java.util.LinkedList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; class StatementComposer { String composeStatement(final String statement, final StatementParameters parameters) { return statement + " - (" + - parameters.getParametersAsMap() - .values() + determineParameterOrder(statement, parameters) .stream() .map(param -> param != null ? param.toString() : "null") .collect(Collectors.joining(",")) + ")"; } + + private Collection determineParameterOrder(final String statement, + final StatementParameters parameters) { + final Pattern parameterPattern = Pattern.compile("(:[a-zA-Z]+|\\?)"); + final Matcher parameterMatcher = parameterPattern.matcher(statement); + + final LinkedList orderedParameterList = new LinkedList<>(); + + for(int matchIndex = 0; parameterMatcher.find(); matchIndex++){ + final String parameter = parameterMatcher.group(1); + if(parameter != null) { + if(parameter.startsWith(":")){ + final String parameterName = parameter.replace(":", ""); + orderedParameterList.add(parameters.get(parameterName)); + }else + orderedParameterList.add(parameters.get(matchIndex)); + } + } + + return orderedParameterList; + } }