Skip to content

Commit

Permalink
Merge pull request #48 from citrusframework/fix/29/callable-statement…
Browse files Browse the repository at this point in the history
…-regression

(#29) fixed regression concerning return values of callable statements
  • Loading branch information
svettwer authored Oct 29, 2019
2 parents 59ebae0 + 4009e70 commit d0b0fa4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ String composeStatement(final String statement, final StatementParameters parame

private Collection<Object> determineParameterOrder(final String statement,
final StatementParameters parameters) {
final Pattern parameterPattern = Pattern.compile("(:[a-zA-Z]+|\\?)");
/*
Regex explanation:
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
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 Matcher parameterMatcher = parameterPattern.matcher(statement);

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

for(int matchIndex = 1; parameterMatcher.find(); matchIndex++){
final String parameterPlaceholder = parameterMatcher.group(1);
final String parameterPlaceholder = parameterMatcher.group(2);
if(parameterPlaceholder != null) {
orderedParameterList.add(
getParameterValue(parameters, parameterPlaceholder, matchIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,35 @@ public void testNoVariableMappingPreservesPlaceholder(){
//THEN
assertEquals(composedStatement, expectedComposedStatement);
}

@Test
public void TestCallableStatementParameterAreReplacedCorrectly(){

//GIVEN
final String statement = "CALL someClobFunction(?,:foo)";
final StatementParameters statementParameters = new StatementParameters();
statementParameters.setParameter("foo", "bar");
statementParameters.setParameter(1, "foobar");
final String expectedComposedStatement = statement + " - (foobar,bar)";

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

//THEN
assertEquals(composedStatement, expectedComposedStatement);
}

@Test
public void testReturnValuesOfCallableStatementsAreParsedCorrectly(){

//GIVEN
final String statement = "? = CALL someFunction(?)";
final String expectedComposedStatement = statement + " - (?)";

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

//THEN
assertEquals(composedStatement, expectedComposedStatement);
}
}

0 comments on commit d0b0fa4

Please sign in to comment.