Skip to content

Commit

Permalink
(#29)changed first regex group to be non-catching
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Oct 30, 2019
1 parent f9cfff1 commit 48ce4ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@ private Collection<Object> determineParameterOrder(final String statement,
The intended group to catch is the group of variables in the sql statement.
In case of a callable statement, it is possible that the statement starts with a '? = '
indicating a out parameter of the statement. This parameter should be ignored concerning variable
replacement. That is why the first group catches the '? = ' (if existing) and the second group catches
replacement. That is why the first non-catching group handles the '? = ' (if existing) and the second group catches
named and unnamed variable references within the rest of the sql statement.
This is why the first group should be ignored due variable substitution while the second group contains all
variables to replace
*/
final Pattern parameterPattern = Pattern.compile("^(\\? ?=)+|(:[a-zA-Z]+|\\?)");
final Pattern parameterPattern = Pattern.compile("(?:\\? ?=)|(:[a-zA-Z]+|\\?)");
final Matcher parameterMatcher = parameterPattern.matcher(statement);

final LinkedList<Object> orderedParameterList = new LinkedList<>();

for(int matchIndex = 1; parameterMatcher.find(); matchIndex++){
final String parameterPlaceholder = parameterMatcher.group(2);
final String parameterPlaceholder = parameterMatcher.group(1);
if(parameterPlaceholder != null) {
orderedParameterList.add(
getParameterValue(parameters, parameterPlaceholder, matchIndex));
Expand All @@ -76,4 +74,4 @@ private Object getParameterValue(final StatementParameters parameters,
return parameterPlaceholder;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ public void testCallableStatementParameterAreReplacedCorrectly(){
public void testReturnValuesOfCallableStatementsAreParsedCorrectly(){

//GIVEN
final String statement = "? = CALL someFunction(?)";
final String expectedComposedStatement = statement + " - (?)";
final String statement = "{? = CALL someFunction(?)}";
final StatementParameters statementParameters = new StatementParameters();
statementParameters.setParameter(2, "foobar");
final String expectedComposedStatement = statement + " - (foobar)";

//WHEN
final String composedStatement = statementComposer.composeStatement(statement, new StatementParameters());
final String composedStatement = statementComposer.composeStatement(statement, statementParameters);

//THEN
assertEquals(composedStatement, expectedComposedStatement);
Expand Down

0 comments on commit 48ce4ad

Please sign in to comment.