Skip to content
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

#14 - Add proper support for string starts/ends operators #16

Merged
merged 1 commit into from
Nov 19, 2021
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 @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these cases.


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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add tests for these operators in the SQLExpressionTest file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Added a test for each of those.

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
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