Skip to content

Commit

Permalink
Merge pull request #287 from turkraft/3.x.x
Browse files Browse the repository at this point in the history
Like wildcard fix
  • Loading branch information
torshid authored May 18, 2023
2 parents 6cfde6c + 090a6b6 commit d0dc665
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
public class InsensitiveLikeOperationExpressionProcessor implements
FilterInfixOperationProcessor<FilterExpressionTransformer, Expression<?>> {

private final LikeOperationExpressionProcessor likeOperationExpressionProcessor;

public InsensitiveLikeOperationExpressionProcessor(
LikeOperationExpressionProcessor likeOperationExpressionProcessor) {
this.likeOperationExpressionProcessor = likeOperationExpressionProcessor;
}

@Override
public Class<FilterExpressionTransformer> getTransformerType() {
return FilterExpressionTransformer.class;
Expand All @@ -30,7 +37,8 @@ public Expression<?> process(FilterExpressionTransformer transformer, InfixOpera
.like(transformer.getCriteriaBuilder()
.upper((Expression<String>) transformer.transform(source.getLeft())),
transformer.getCriteriaBuilder()
.upper((Expression<String>) transformer.transform(source.getRight())));
.upper(likeOperationExpressionProcessor.getLikePatternExpression(transformer,
source.getRight())));
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.turkraft.springfilter.transformer.processor;

import com.turkraft.springfilter.language.LikeOperator;
import com.turkraft.springfilter.parser.node.FilterNode;
import com.turkraft.springfilter.parser.node.InfixOperationNode;
import com.turkraft.springfilter.parser.node.InputNode;
import com.turkraft.springfilter.transformer.FilterExpressionTransformer;
import jakarta.persistence.criteria.Expression;
import org.springframework.stereotype.Component;

/**
* Note: the behavior of this operation might differ between different SQL dialects.
*/
@Component
public class LikeOperationExpressionProcessor implements
FilterInfixOperationProcessor<FilterExpressionTransformer, Expression<?>> {
Expand All @@ -28,7 +33,25 @@ public Expression<?> process(FilterExpressionTransformer transformer, InfixOpera
transformer.registerTargetType(source.getRight(), String.class);
return transformer.getCriteriaBuilder()
.like((Expression<String>) transformer.transform(source.getLeft()),
(Expression<String>) transformer.transform(source.getRight()));
getLikePatternExpression(transformer, source.getRight()));
}

@SuppressWarnings("unchecked")
public Expression<String> getLikePatternExpression(FilterExpressionTransformer transformer,
FilterNode node) {

if (!(node instanceof InputNode)) {
return (Expression<String>) transformer.transform(node);
}

String pattern = ((InputNode) node).getValue().toString().replace("*", "%");

if (!pattern.contains("%")) {
pattern = "%" + pattern + "%";
}

return transformer.getCriteriaBuilder().literal(pattern);

}

}

0 comments on commit d0dc665

Please sign in to comment.