DATAJPA-92 - JpaQueryCreator now inspects enum to decide how to handle ignore case scenarios.
This commit is contained in:
committed by
Oliver Gierke
parent
aa6a7c1983
commit
27ad91fbbc
@@ -37,6 +37,7 @@ import org.springframework.data.repository.query.Parameter;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
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.Property;
|
||||
@@ -406,12 +407,27 @@ public class JpaQueryCreator extends
|
||||
*/
|
||||
private <T> Expression<T> upperIfIgnoreCase(Expression<T> expression) {
|
||||
|
||||
if (part.shouldIgnoreCase()
|
||||
&& String.class.equals(expression.getJavaType())) {
|
||||
return (Expression<T>) builder
|
||||
.upper((Expression<String>) expression);
|
||||
}
|
||||
return expression;
|
||||
switch (part.shouldIgnoreCase()) {
|
||||
case ALWAYS:
|
||||
Assert.state(canUpperCase(expression),
|
||||
"Unable to ignore case of "
|
||||
+ expression.getJavaType().getName()
|
||||
+ " types, the property '"
|
||||
+ part.getProperty().getName()
|
||||
+ "' must reference a String");
|
||||
return (Expression<T>) builder
|
||||
.upper((Expression<String>) expression);
|
||||
case WHEN_POSSIBLE:
|
||||
if (canUpperCase(expression)) {
|
||||
return (Expression<T>) builder
|
||||
.upper((Expression<String>) expression);
|
||||
}
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private boolean canUpperCase(Expression<?> expression) {
|
||||
return String.class.equals(expression.getJavaType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -42,6 +46,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
public class PartTreeJpaQueryIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@PersistenceContext
|
||||
EntityManager entityManager;
|
||||
|
||||
@@ -67,8 +74,40 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
jpaQuery.createQuery(new Object[] { "Matthews", new PageRequest(0, 1) });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cannotIgnoreCaseIfNotString() throws Exception {
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
thrown.expectMessage("Unable to ignore case of java.lang.Integer types, the property 'id' must reference a String");
|
||||
testIgnoreCase("findByIdIgnoringCase", 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cannotIgnoreCaseIfNotStringUnlessIgnoringAll() throws Exception {
|
||||
|
||||
testIgnoreCase("findByIdAllIgnoringCase", 3);
|
||||
}
|
||||
|
||||
private void testIgnoreCase(String methodName, Object...values) throws Exception {
|
||||
|
||||
Class<?>[] parameterTypes = new Class[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
parameterTypes[i] = values[i].getClass();
|
||||
}
|
||||
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
|
||||
JpaQueryMethod queryMethod =
|
||||
new JpaQueryMethod(method, new DefaultRepositoryMetadata(
|
||||
UserRepository.class),
|
||||
PersistenceProvider.fromEntityManager(entityManager));
|
||||
PartTreeJpaQuery jpaQuery =
|
||||
new PartTreeJpaQuery(queryMethod, entityManager);
|
||||
jpaQuery.createQuery(values);
|
||||
}
|
||||
|
||||
interface UserRepository extends Repository<User, Long> {
|
||||
|
||||
Page<User> findByFirstname(String firstname, Pageable pageable);
|
||||
User findByIdIgnoringCase(Integer id);
|
||||
User findByIdAllIgnoringCase(Integer id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user