DATAKV-169 - Add support for IN keyword in derived queries.

We now support `In` keyword in derived finder methods (eg. `findByFirstnameIn(Collection<String> firstnames)`) when using SpEL based queries.

Original pull request: #23.
This commit is contained in:
Christoph Strobl
2017-04-13 15:39:37 +02:00
committed by Mark Paluch
parent 979f3a33a8
commit f67a66bf25
3 changed files with 109 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
import org.springframework.data.repository.query.parser.Part.Type;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.data.repository.query.parser.PartTree.OrPart;
import org.springframework.expression.spel.standard.SpelExpression;
@@ -107,8 +108,12 @@ public class SpelQueryCreator extends AbstractQueryCreator<KeyValueQuery<SpelExp
for (Iterator<Part> partIter = orPart.iterator(); partIter.hasNext();) {
Part part = partIter.next();
partBuilder.append("#it?.");
partBuilder.append(part.getProperty().toDotPath().replace(".", "?."));
if(!requiresInverseLookup(part)) {
partBuilder.append("#it?.");
partBuilder.append(part.getProperty().toDotPath().replace(".", "?."));
}
// TODO: check if we can have caseinsensitive search
if (!part.shouldIgnoreCase().equals(IgnoreCaseType.NEVER)) {
@@ -173,6 +178,13 @@ public class SpelQueryCreator extends AbstractQueryCreator<KeyValueQuery<SpelExp
partBuilder.append(" matches ").append("[").append(parameterIndex++).append("]");
break;
case IN:
partBuilder.append("[").append(parameterIndex++).append("].contains(");
partBuilder.append("#it?.");
partBuilder.append(part.getProperty().toDotPath().replace(".", "?."));
partBuilder.append(")");
break;
case CONTAINING:
case NOT_CONTAINING:
case NEGATING_SIMPLE_PROPERTY:
@@ -202,4 +214,8 @@ public class SpelQueryCreator extends AbstractQueryCreator<KeyValueQuery<SpelExp
return PARSER.parseRaw(sb.toString());
}
private static boolean requiresInverseLookup(Part part) {
return part.getType() == Type.IN;
}
}

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import org.joda.time.format.DateTimeFormatter;
@@ -260,6 +261,51 @@ public class SpelQueryCreatorUnitTests {
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(ROBB), is(false));
}
@Test // DATAKV-169
public void inReturnsMatchCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(ROBB), is(true));
}
@Test // DATAKV-169
public void inNotMatchingReturnsCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
}
@Test // DATAKV-169
public void inWithNullCompareValuesCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add(null);
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
}
@Test // DATAKV-169
public void inWithNullSourceValuesMatchesCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(false));
}
@Test // DATAKV-169
public void inMatchesNullValuesCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add(null);
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(true));
}
private Evaluation evaluate(String methodName, Object... args) throws Exception {
return new Evaluation((SpelExpression) createQueryForMethodWithArgs(methodName, args).getCritieria());
}
@@ -344,6 +390,9 @@ public class SpelQueryCreatorUnitTests {
// Type.REGEX
Person findByLastnameMatches(String lastname);
// Type.IN
Person findByFirstnameIn(ArrayList<String> in);
}
static class Evaluation {

View File

@@ -168,13 +168,51 @@ public abstract class AbstractRepositoryUnitTests<T extends AbstractRepositoryUn
assertThat(result.get(0).getFirstname(), is(CERSEI.getFirstname()));
}
@Test // DATAKV-169
public void findsByValueInCollectionCorrectly() {
repository.save(LENNISTERS);
List<Person> result = repository.findByFirstnameIn(Arrays.asList(CERSEI.getFirstname(), JAIME.getFirstname()));
assertThat(result, hasSize(2));
assertThat(result, is(containsInAnyOrder(CERSEI, JAIME)));
}
@Test // DATAKV-169
public void findsByValueInCollectionCorrectlyWhenTargetPathContainsNullValue() {
repository.save(LENNISTERS);
repository.save(new Person(null, 10));
List<Person> result = repository.findByFirstnameIn(Arrays.asList(CERSEI.getFirstname(), JAIME.getFirstname()));
assertThat(result, hasSize(2));
assertThat(result, is(containsInAnyOrder(CERSEI, JAIME)));
}
@Test // DATAKV-169
public void findsByValueInCollectionCorrectlyWhenTargetPathAndCollectionContainNullValue() {
repository.save(LENNISTERS);
Person personWithNullAsFirstname = new Person(null, 10);
repository.save(personWithNullAsFirstname);
List<Person> result = repository
.findByFirstnameIn(Arrays.asList(CERSEI.getFirstname(), JAIME.getFirstname(), null));
assertThat(result, hasSize(3));
assertThat(result, is(containsInAnyOrder(CERSEI, JAIME, personWithNullAsFirstname)));
}
protected KeyValueRepositoryFactory createKeyValueRepositoryFactory(KeyValueOperations operations) {
return new KeyValueRepositoryFactory(operations);
}
protected abstract T getRepository(KeyValueRepositoryFactory factory);
public static interface PersonRepository extends CrudRepository<Person, String>, KeyValueRepository<Person, String> {
public interface PersonRepository extends CrudRepository<Person, String>, KeyValueRepository<Person, String> {
List<Person> findByAge(int age);
@@ -193,6 +231,8 @@ public abstract class AbstractRepositoryUnitTests<T extends AbstractRepositoryUn
List<PersonSummary> findByAgeGreaterThan(int age, Sort sort);
<T> List<T> findByAgeGreaterThan(int age, Sort sort, Class<T> projectionType);
List<Person> findByFirstnameIn(List<String> firstname);
}
interface PersonSummary {