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; + } }