Skip to content

Commit

Permalink
DATAMONGO-418 - Added support for StartingWith, EndingWith and Contai…
Browse files Browse the repository at this point in the history
…ning keywords.
  • Loading branch information
odrotbohm committed Mar 21, 2012
1 parent 05c2045 commit 97f57e3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ private Criteria from(Type type, MongoPersistentProperty property, Criteria crit
case IN:
return criteria.in(nextAsArray(parameters, property));
case LIKE:
case STARTING_WITH:
case ENDING_WITH:
case CONTAINING:
String value = parameters.next().toString();
return criteria.regex(toLikeRegex(value));
return criteria.regex(toLikeRegex(value, type));
case REGEX:
return criteria.regex(parameters.next().toString());
case EXISTS:
Expand Down Expand Up @@ -269,7 +272,19 @@ private Object[] nextAsArray(PotentiallyConvertingIterator iterator, MongoPersis
return new Object[] { next };
}

private String toLikeRegex(String source) {
private String toLikeRegex(String source, Type type) {

switch (type) {
case STARTING_WITH:
source = source + "*";
break;
case ENDING_WITH:
source = "*" + source;
break;
case CONTAINING:
source = "*" + source + "*";
break;
}

return source.replaceAll("\\*", ".*");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,45 @@ public void createsQueryReferencingADBRefCorrectly() {
assertThat(query.getQueryObject(), is(query(where("creator").is(dbref)).getQueryObject()));
}

/**
* @see DATAMONGO-418
*/
@Test
public void createsQueryWithStartingWithPredicateCorrectly() {

PartTree tree = new PartTree("findByUsernameStartingWith", User.class);
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "Matt"), context);
Query query = creator.createQuery();

assertThat(query.getQueryObject(), is(query(where("foo").regex("Matt.*")).getQueryObject()));
}

/**
* @see DATAMONGO-418
*/
@Test
public void createsQueryWithEndingWithPredicateCorrectly() {

PartTree tree = new PartTree("findByUsernameEndingWith", User.class);
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "ews"), context);
Query query = creator.createQuery();

assertThat(query.getQueryObject(), is(query(where("foo").regex(".*ews")).getQueryObject()));
}

/**
* @see DATAMONGO-418
*/
@Test
public void createsQueryWithContainingPredicateCorrectly() {

PartTree tree = new PartTree("findByUsernameContaining", User.class);
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "thew"), context);
Query query = creator.createQuery();

assertThat(query.getQueryObject(), is(query(where("foo").regex(".*thew.*")).getQueryObject()));
}

private void assertBindsDistanceToQuery(Point point, Distance distance, Query reference) throws Exception {

when(converter.convertToMongoType("Dave")).thenReturn("Dave");
Expand Down

0 comments on commit 97f57e3

Please sign in to comment.