DATAJPA-473 - Fixed bug in binding detection in String queries.
Query parameter binding replacements were undone if a simple binding was contained in the query. Fixed that and also make sure we don't create superfluous multiple bindings for the same variable and binding type.
This commit is contained in:
@@ -219,7 +219,6 @@ class StringQuery {
|
||||
|
||||
bindings.add(parameterIndex != null ? new ParameterBinding(parameterIndex) : new ParameterBinding(
|
||||
parameterName));
|
||||
result = query;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +233,9 @@ class StringQuery {
|
||||
}
|
||||
}
|
||||
|
||||
bindings.add(binding);
|
||||
if (!bindings.contains(binding)) {
|
||||
bindings.add(binding);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -194,6 +194,24 @@ public class StringQueryUnitTests {
|
||||
assertPositionalBinding(ParameterBinding.class, 1, bindings.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-473
|
||||
*/
|
||||
@Test
|
||||
public void removesLikeBindingsFromQueryIfQueryContainsSimpleBinding() {
|
||||
|
||||
StringQuery query = new StringQuery("SELECT a FROM Article a WHERE a.overview LIKE %:escapedWord% ESCAPE '~'"
|
||||
+ " OR a.content LIKE %:escapedWord% ESCAPE '~' OR a.title = :word ORDER BY a.articleId DESC");
|
||||
|
||||
List<ParameterBinding> bindings = query.getParameterBindings();
|
||||
|
||||
assertThat(bindings, hasSize(2));
|
||||
assertNamedBinding(LikeParameterBinding.class, "escapedWord", bindings.get(0));
|
||||
assertNamedBinding(ParameterBinding.class, "word", bindings.get(1));
|
||||
assertThat(query.getQueryString(), is("SELECT a FROM Article a WHERE a.overview LIKE :escapedWord ESCAPE '~'"
|
||||
+ " OR a.content LIKE :escapedWord ESCAPE '~' OR a.title = :word ORDER BY a.articleId DESC"));
|
||||
}
|
||||
|
||||
private void assertPositionalBinding(Class<? extends ParameterBinding> bindingType, Integer position,
|
||||
ParameterBinding expectedBinding) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user