Skip to content

Commit

Permalink
Merge pull request #325 from turkraft/3.x.x
Browse files Browse the repository at this point in the history
Added ListExpression as an alternative to cb.in('...')
  • Loading branch information
torshid authored Sep 26, 2023
2 parents 65de41c + b1f8320 commit 4d64e30
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.turkraft.springfilter.transformer;

import jakarta.persistence.criteria.Expression;

public interface ConvertibleExpression {

Expression<?> convertTo(Class<?> type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public Expression<?> transformPostfixOperation(PostfixOperationNode node) {
}

private Expression<?> castIfNeeded(Expression<?> expression, @Nullable Class<?> targetType) {
if (targetType != null && expression instanceof ConvertibleExpression convertibleExpression) {
return convertibleExpression.convertTo(targetType);
}
if (targetType != null && expression.getJavaType() != null
&& !targetType.isAssignableFrom(expression.getJavaType())) {
return expression.as(targetType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.turkraft.springfilter.transformer;

import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Selection;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class ListExpression<T> implements Expression<T>, ConvertibleExpression {

private List<Expression<T>> values;

public ListExpression(List<Expression<T>> values) {
this.values = values;
}

public List<Expression<T>> getValues() {
return values;
}

public void setValues(List<Expression<T>> values) {
this.values = values;
}

@Override
public Predicate isNull() {
throw new UnsupportedOperationException();
}

@Override
public Predicate isNotNull() {
throw new UnsupportedOperationException();
}

@Override
public Predicate in(Object... values) {
throw new UnsupportedOperationException();
}

@Override
public Predicate in(Expression<?>... values) {
return null;
}

@Override
public Predicate in(Collection<?> values) {
return null;
}

@Override
public Predicate in(Expression<Collection<?>> values) {
return null;
}

@Override
public <X> Expression<X> as(Class<X> type) {
throw new UnsupportedOperationException();
}

@Override
public Selection<T> alias(String name) {
return null;
}

@Override
public boolean isCompoundSelection() {
throw new UnsupportedOperationException();
}

@Override
public List<Selection<?>> getCompoundSelectionItems() {
return null;
}

@Override
public Class<? extends T> getJavaType() {
return null;
}

@Override
public String getAlias() {
throw new UnsupportedOperationException();
}

@Override
public Expression<T> convertTo(Class<?> type) {
return new ListExpression(values.stream().map(
v -> v != null && v.getJavaType() != null && !type.isAssignableFrom(v.getJavaType()) ? v.as(
type) : v).collect(
Collectors.toList()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.turkraft.springfilter.parser.node.FilterNode;
import com.turkraft.springfilter.parser.node.InfixOperationNode;
import com.turkraft.springfilter.transformer.FilterExpressionTransformer;
import com.turkraft.springfilter.transformer.ListExpression;
import jakarta.persistence.criteria.CriteriaBuilder.In;
import jakarta.persistence.criteria.Expression;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -47,8 +48,22 @@ public Expression<?> process(FilterExpressionTransformer transformer,

transformer.registerTargetType(source.getRight(), left.getJavaType());

Expression<?> right = transformer.transform(source.getRight());

if (right instanceof ListExpression<?> listExpression) {

In<Object> in = transformer.getCriteriaBuilder().in(left);

for (Expression<?> value : listExpression.getValues()) {
in.value(value);
}

return in;

}

return transformer.getCriteriaBuilder().in(left)
.value((Expression) transformer.transform(source.getRight()));
.value((Expression) right);

}

Expand Down

0 comments on commit 4d64e30

Please sign in to comment.