Skip to content

Commit

Permalink
(#29) resolve named and indexed parameters in correct order
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Sep 6, 2019
1 parent e9f933f commit 05951a7
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<Object> determineParameterOrder(final String statement,
final StatementParameters parameters) {
final Pattern parameterPattern = Pattern.compile("(:[a-zA-Z]+|\\?)");
final Matcher parameterMatcher = parameterPattern.matcher(statement);

final LinkedList<Object> 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;
}
}

0 comments on commit 05951a7

Please sign in to comment.