-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#29) resolve named and indexed parameters in correct order
- Loading branch information
Showing
1 changed file
with
26 additions
and
2 deletions.
There are no files selected for viewing
28 changes: 26 additions & 2 deletions
28
driver/src/main/java/com/consol/citrus/db/driver/statement/StatementComposer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |