DATAJPA-341 - Fixed regression introduced in advanced LIKE binding.

We now don't throw an exception anymore in case a normal LIKE binding is used in manually defined queries.
This commit is contained in:
Oliver Gierke
2013-05-04 11:46:18 +02:00
parent 617423797d
commit 31006e1866
2 changed files with 26 additions and 4 deletions

View File

@@ -202,7 +202,7 @@ class StringQuery {
return Type.STARTING_WITH;
}
throw new IllegalArgumentException(String.format("Illegal like pattern %s!", expression));
return Type.LIKE;
}
/**
@@ -214,7 +214,7 @@ class StringQuery {
static class LikeBinding {
private static final List<Type> SUPPORTED_TYPES = Arrays.asList(Type.CONTAINING, Type.STARTING_WITH,
Type.ENDING_WITH);
Type.ENDING_WITH, Type.LIKE);
private final String name;
private final Integer position;
@@ -303,6 +303,7 @@ class StringQuery {
return String.format("%%%s", value.toString());
case CONTAINING:
return String.format("%%%s%%", value.toString());
case LIKE:
default:
return value;
}

View File

@@ -25,18 +25,39 @@ import org.springframework.data.jpa.repository.query.StringQuery.LikeBinding;
import org.springframework.data.repository.query.parser.Part.Type;
/**
* Unit tests for {@link StringQuery}.
*
* @author Oliver Gierke
*/
public class StringQueryUnitTests {
/**
* @see DATAJPA-341
*/
@Test
public void doesNotConsiderPlainLikeABinding() {
String source = "select from User u where u.firstname like :firstname";
StringQuery query = new StringQuery(source);
assertThat(query.hasLikeBindings(), is(true));
assertThat(query.getQuery(), is(source));
List<LikeBinding> bindings = query.getLikeBindings();
assertThat(bindings, hasSize(1));
LikeBinding binding = bindings.get(0);
assertThat(binding.getType(), is(Type.LIKE));
assertThat(binding.hasName("firstname"), is(true));
}
@Test
public void detectsPositionalLikeBindings() {
StringQuery query = new StringQuery("select u from User u where u.firstname like %?1% or u.lastname like %?2");
assertThat(query.hasLikeBindings(), is(true));
assertThat(query.getQuery(),
is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
assertThat(query.getQuery(), is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
List<LikeBinding> bindings = query.getLikeBindings();
assertThat(bindings, hasSize(2));