Skip to content

Commit

Permalink
#14 - Add proper support for string starts/ends operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Schaer authored and jaiswalsk committed Nov 19, 2021
1 parent a84f0fe commit 7779a9c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Specification<T> visitCompoundExpression(CompoundExpression compoundExpre
@Override
public Specification<T> visitBinaryExpression(BinaryExpression binaryExpression, Specification<T> data) {

Specification<T> specification = new Specification<T>() {
return new Specification<T>() {
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder
criteriaBuilder) {
Expand All @@ -118,7 +118,13 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, Crite
switch (binaryExpression.getOperator()) {
/* String operations.*/
case STARTS:
predicate = criteriaBuilder.like(path, operandValue.value() + "%");
break;

case ENDS:
predicate = criteriaBuilder.like(path, "%" + operandValue.value());
break;

case CONTAINS:
predicate = criteriaBuilder.like(path, "%" + operandValue.value() + "%");
break;
Expand Down Expand Up @@ -159,7 +165,6 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, Crite
return predicate;
}
};
return specification;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ public String visitExpressionValue(ExpressionValue<? extends Comparable> value,
Operator operator = operatorStack.pop();
List<? extends Comparable> expressionValues = value.getValues();

if (operator == Operator.CONTAINS || operator == Operator.STARTS || operator == Operator.ENDS ) {
if (operator == Operator.STARTS) {
expressionBuilder.append("'").append(expressionValues.get(0)).append("%").append("'");
} else if (operator == Operator.ENDS) {
expressionBuilder.append("'").append("%").append(expressionValues.get(0)).append("'");
} else if (operator == Operator.CONTAINS) {
expressionBuilder.append("'").append("%").append(expressionValues.get(0)).append("%").append("'");
} else if(operator == Operator.BETWEEN) {
expressionBuilder.append("'").append(expressionValues.get(0)).append("'")
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/intuit/graphql/filter/common/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,31 @@ public class TestConstants {
" lastName\n" +
" }\n" +
"}";

public static final String FIRST_NAME_STARTS = "{\n" +
" searchEmployees(filter: {\n" +
" firstName : {starts: \"Sa\"}\n" +
" }) {\n" +
" firstName\n" +
" lastName\n" +
" }\n" +
"}";
public static final String FIRST_NAME_CONTAINS = "{\n" +
" searchEmployees(filter: {\n" +
" firstName : {contains: \"ura\"}\n" +
" }) {\n" +
" firstName\n" +
" lastName\n" +
" }\n" +
"}";

public static final String FIRST_NAME_ENDS = "{\n" +
" searchEmployees(filter: {\n" +
" firstName : {ends: \"bh\"}\n" +
" }) {\n" +
" firstName\n" +
" lastName\n" +
" }\n" +
"}";

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,32 @@ public void compoundFilterExpressionWithNot() {

Assert.assertEquals(expectedExpression,getEmployeeDataFetcher().getSqlExpression());
}

@Test
public void compoundFilterExpressionWithStarts() {
ExecutionResult result = getGraphQL().execute(TestConstants.FIRST_NAME_STARTS);

String expectedExpression = "WHERE (empFirstName LIKE 'Sa%')";

Assert.assertEquals(expectedExpression,getEmployeeDataFetcher().getSqlExpression());
}

@Test
public void compoundFilterExpressionWithContains() {
ExecutionResult result = getGraphQL().execute(TestConstants.FIRST_NAME_CONTAINS);

String expectedExpression = "WHERE (empFirstName LIKE '%ura%')";

Assert.assertEquals(expectedExpression,getEmployeeDataFetcher().getSqlExpression());
}

@Test
public void compoundFilterExpressionWithEnds() {
ExecutionResult result = getGraphQL().execute(TestConstants.FIRST_NAME_ENDS);

String expectedExpression = "WHERE (empFirstName LIKE '%bh')";

Assert.assertEquals(expectedExpression,getEmployeeDataFetcher().getSqlExpression());
}

}
2 changes: 2 additions & 0 deletions src/test/resources/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ input EmployeeFilter {
# Define String expression
input StringExpression {
equals: String
starts: String
contains: String
ends: String
in: [String!]
}

Expand Down

0 comments on commit 7779a9c

Please sign in to comment.