DATADOC-17 - Added support for 'Like' keyword in finders.

This commit is contained in:
Oliver Gierke
2010-12-03 09:27:54 +01:00
parent 4a80c2c7dc
commit 36a6f4ec86
4 changed files with 52 additions and 1 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.document.mongodb.repository;
import java.util.regex.Pattern;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.SimpleParameterAccessor;
import org.springframework.data.repository.query.SimpleParameterAccessor.BindableParameterIterator;
@@ -131,6 +133,9 @@ class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
return criteria.notEquals(null);
case IS_NULL:
return criteria.is(null);
case LIKE:
String value = parameters.next().toString();
return criteria.regex(toLikeRegex(value));
case SIMPLE_PROPERTY:
return criteria.is(parameters.next());
case NEGATING_SIMPLE_PROPERTY:
@@ -139,4 +144,11 @@ class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
throw new IllegalArgumentException("Unsupported keyword!");
}
private Pattern toLikeRegex(String source) {
String regex = source.replaceAll("\\*", ".*");
return Pattern.compile(regex);
}
}

View File

@@ -20,6 +20,10 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.support.IsNewAware;
import org.springframework.data.repository.support.RepositorySupport;
import org.springframework.util.Assert;
@@ -33,7 +37,7 @@ import com.mongodb.QueryBuilder;
* @author Oliver Gierke
*/
public class SimpleMongoRepository<T, ID extends Serializable> extends
RepositorySupport<T, ID> {
RepositorySupport<T, ID> implements PagingAndSortingRepository<T, ID> {
private final MongoTemplate template;
private MongoEntityInformation entityInformation;
@@ -176,6 +180,34 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Pageable)
*/
public Page<T> findAll(Pageable pageable) {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Sort)
*/
public List<T> findAll(Sort sort) {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*

View File

@@ -26,4 +26,7 @@ import java.util.List;
public interface PersonRepository extends MongoRepository<Person, Long> {
List<Person> findByLastname(String lastname);
List<Person> findByFirstnameLike(String firstname);
}

View File

@@ -53,5 +53,9 @@ public class PersonRepositoryIntegrationTests {
List<Person> result = repository.findByLastname("Gierke");
assertThat(result.size(), is(1));
assertThat(result, hasItem(person));
result = repository.findByFirstnameLike("Oli*");
assertThat(result.size(), is(1));
assertThat(result, hasItem(person));
}
}