DATAJDBC-560 - Fix Criteria mapping when composing a group from top-level criteria.

Using Criteria.from(…) with multiple Criteria objects now uses properly AND combination along with group nesting to render a correct criteria. Previously, the INITIAL combinator in groups caused a mapping exception.
This commit is contained in:
Mark Paluch
2020-06-09 15:31:14 +02:00
parent 81aba29fda
commit eeeb188117
2 changed files with 20 additions and 4 deletions

View File

@@ -191,7 +191,7 @@ class QueryMapper {
Condition condition = getCondition(criterion, parameterSource, table, entity);
if (condition != null) {
result = combine(criterion, mapped, criterion.getCombinator(), condition);
result = combine(mapped, criterion.getCombinator(), condition);
}
if (result != null) {
@@ -221,7 +221,7 @@ class QueryMapper {
Condition condition = unroll(criterion, table, entity, parameterSource);
mapped = combine(criterion, mapped, combinator, condition);
mapped = combine(mapped, combinator, condition);
}
return mapped;
@@ -245,17 +245,19 @@ class QueryMapper {
return mapCondition(criteria, parameterSource, table, entity);
}
private Condition combine(CriteriaDefinition criteria, @Nullable Condition currentCondition,
private Condition combine(@Nullable Condition currentCondition,
CriteriaDefinition.Combinator combinator, Condition nextCondition) {
if (currentCondition == null) {
currentCondition = nextCondition;
} else if (combinator == CriteriaDefinition.Combinator.INITIAL) {
currentCondition = currentCondition.and(Conditions.nest(nextCondition));
} else if (combinator == CriteriaDefinition.Combinator.AND) {
currentCondition = currentCondition.and(nextCondition);
} else if (combinator == CriteriaDefinition.Combinator.OR) {
currentCondition = currentCondition.or(nextCondition);
} else {
throw new IllegalStateException("Combinator " + criteria.getCombinator() + " not supported");
throw new IllegalStateException("Combinator " + combinator + " not supported");
}
return currentCondition;

View File

@@ -126,6 +126,20 @@ public class QueryMapperUnitTests {
.hasToString("person.\"NAME\" = ?[:name] AND (person.\"NAME\" = ?[:name1] OR person.age < ?[:age])");
}
@Test // DATAJDBC-560
public void shouldMapFromConcat() {
Criteria criteria = Criteria.from(Criteria.where("name").is("Foo"), Criteria.where("name").is("Bar") //
.or("age").lessThan(49));
assertThat(criteria.isEmpty()).isFalse();
Condition condition = map(criteria);
assertThat(condition)
.hasToString("(person.\"NAME\" = ?[:name] AND (person.\"NAME\" = ?[:name1] OR person.age < ?[:age]))");
}
@Test // DATAJDBC-318
public void shouldMapSimpleCriteria() {