Skip to content

feat(GH-204): added LOWER predicate for case insensitive match #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ private GraphQLInputType getWhereAttributeType(Attribute<?,?> attribute) {
.type(getAttributeInputType(attribute))
.build()
)
.field(GraphQLInputObjectField.newInputObjectField()
.name(Criteria.LOWER.name())
.description("Case insensitive match criteria")
.type(getAttributeInputType(attribute))
.build()
)
.field(GraphQLInputObjectField.newInputObjectField()
.name(Criteria.CASE.name())
.description("Case sensitive match criteria")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected Predicate getStringPredicate(Path<String> root, PredicateFilter filter
if(arrayValuePredicate == null) {
String compareValue = filter.getValue().toString();
Expression<String> fieldValue = root;

if (filter.getCriterias().contains(PredicateFilter.Criteria.IN)) {
CriteriaBuilder.In<Object> in = cb.in(root);
return in.value(compareValue);
Expand All @@ -141,6 +141,10 @@ protected Predicate getStringPredicate(Path<String> root, PredicateFilter filter
if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
return cb.equal(fieldValue, compareValue);
}
else if (filter.getCriterias().contains(PredicateFilter.Criteria.LOWER)) {
return cb.equal(cb.lower(fieldValue),
compareValue.toLowerCase());
}
else if (filter.getCriterias().contains(PredicateFilter.Criteria.NE)) {
return cb.notEqual(fieldValue, compareValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public enum Criteria {
* case sensitive (strings)
*/
CASE,
/**
* case insensitive match
* <pre>LOWER(field) = LOWER(SEARCH)</pre>
*/
LOWER,
/**
* end of the string matches
* <pre>LOWER(field) LIKE LOWER(SEARCH)</pre> if not set then case
* insensitive match
*/
ENDS,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,38 @@ public void queryWithEQMatchingCase() {
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryWithLOWERNotMatchingCase() {
//given:
String query = "query { Books ( where: { title: {LOWER: \"WAR AND PEACE\"}}) { select { id title} } }";

String expected = "{Books={select=[" +
"{id=2, title=War and Peace}" +
"]}}";

//when:
Object result = executor.execute(query).getData();

//then:
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryWithLOWERMatchingCase() {
//given:
String query = "query { Books ( where: { title: {LOWER: \"War and Peace\"}}) { select { id title} } }";

String expected = "{Books={select=[" +
"{id=2, title=War and Peace}" +
"]}}";

//when:
Object result = executor.execute(query).getData();

//then:
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void shouldNotReturnStaleCacheResultsFromPreviousQueryForCollectionCriteriaExpression() {
//given:
Expand Down