diff --git a/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/InsensitiveLikeOperationExpressionProcessor.java b/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/InsensitiveLikeOperationExpressionProcessor.java index e9a77927..6c7bb504 100644 --- a/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/InsensitiveLikeOperationExpressionProcessor.java +++ b/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/InsensitiveLikeOperationExpressionProcessor.java @@ -10,6 +10,13 @@ public class InsensitiveLikeOperationExpressionProcessor implements FilterInfixOperationProcessor> { + private final LikeOperationExpressionProcessor likeOperationExpressionProcessor; + + public InsensitiveLikeOperationExpressionProcessor( + LikeOperationExpressionProcessor likeOperationExpressionProcessor) { + this.likeOperationExpressionProcessor = likeOperationExpressionProcessor; + } + @Override public Class getTransformerType() { return FilterExpressionTransformer.class; @@ -30,7 +37,8 @@ public Expression process(FilterExpressionTransformer transformer, InfixOpera .like(transformer.getCriteriaBuilder() .upper((Expression) transformer.transform(source.getLeft())), transformer.getCriteriaBuilder() - .upper((Expression) transformer.transform(source.getRight()))); + .upper(likeOperationExpressionProcessor.getLikePatternExpression(transformer, + source.getRight()))); } } diff --git a/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/LikeOperationExpressionProcessor.java b/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/LikeOperationExpressionProcessor.java index c4485cda..c0fd4d0d 100644 --- a/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/LikeOperationExpressionProcessor.java +++ b/jpa/src/main/java/com/turkraft/springfilter/transformer/processor/LikeOperationExpressionProcessor.java @@ -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> { @@ -28,7 +33,25 @@ public Expression process(FilterExpressionTransformer transformer, InfixOpera transformer.registerTargetType(source.getRight(), String.class); return transformer.getCriteriaBuilder() .like((Expression) transformer.transform(source.getLeft()), - (Expression) transformer.transform(source.getRight())); + getLikePatternExpression(transformer, source.getRight())); + } + + @SuppressWarnings("unchecked") + public Expression getLikePatternExpression(FilterExpressionTransformer transformer, + FilterNode node) { + + if (!(node instanceof InputNode)) { + return (Expression) transformer.transform(node); + } + + String pattern = ((InputNode) node).getValue().toString().replace("*", "%"); + + if (!pattern.contains("%")) { + pattern = "%" + pattern + "%"; + } + + return transformer.getCriteriaBuilder().literal(pattern); + } }