com.sap.cds.jdbc.hana.HanaPredicateMapper Maven / Gradle / Ivy
/*******************************************************************
* © 2023 SAP SE or an SAP affiliate company. All rights reserved. *
*******************************************************************/
package com.sap.cds.jdbc.hana;
import java.util.List;
import java.util.stream.Collectors;
import com.sap.cds.impl.parser.token.CqnBoolLiteral;
import com.sap.cds.jdbc.generic.GenericPredicateMapper;
import com.sap.cds.jdbc.generic.GenericPredicateModifier;
import com.sap.cds.ql.CQL;
import com.sap.cds.ql.Predicate;
import com.sap.cds.ql.Value;
import com.sap.cds.ql.cqn.CqnComparisonPredicate.Operator;
import com.sap.cds.ql.cqn.CqnListValue;
import com.sap.cds.ql.cqn.CqnPredicate;
import com.sap.cds.ql.cqn.CqnSyntaxException;
import com.sap.cds.ql.cqn.CqnValue;
import com.sap.cds.ql.cqn.Modifier;
import com.sap.cds.ql.impl.ExpressionVisitor;
public class HanaPredicateMapper extends GenericPredicateMapper {
@Override
public CqnPredicate apply(CqnPredicate pred) {
Modifier m = new GenericPredicateModifier() {
@Override
public Predicate comparison(Value> lhs, Operator op, Value> rhs) {
switch (op) {
case GT:
case GE:
case LT:
case LE:
if (lhs.isList() && rhs.isList()) {
CqnListValue left = lhs.asList();
CqnListValue right = rhs.asList();
int n = left.size();
if (n != right.size()) {
throw new CqnSyntaxException("Only list values of same size can be compared");
}
List ls = left.values().collect(Collectors.toList());
List rs = right.values().collect(Collectors.toList());
// Col1>ST1
// OR (Col2>ST2 AND Col1=ST1)
// OR (Col3>ST3 AND Col2=ST2 AND Col1=ST1)
Predicate p = CqnBoolLiteral.FALSE;
for (int i = 0; i < n; i++) {
Predicate pi = CQL.comparison(ls.get(i), op, rs.get(i));
for (int j = i - 1; j >= 0; j--) {
Predicate pj = CQL.comparison(ls.get(j), Operator.EQ, rs.get(j));
pi = pj.and(pi);
}
p = p.or(pi);
}
return p;
}
break;
default:
break;
}
return super.comparison(lhs, op, rhs);
}
};
return ExpressionVisitor.copy(pred, m);
}
}