cancel
Showing results for 
Search instead for 
Did you mean: 

RE: CAP Java Documentation -- Copy & Modify CDS QL Statements -- Remove Where clause entirely

0 Kudos

In the documentation for Copy & Modify CDS QL Statements, we are shown how to modify a where clause as follows:

CqnSelect copy = CQL.copy(query, new Modifier() {
   @Override
   public Predicate where(Predicate where) {
      return where.or(CQL.get("title").eq("CAP Java SDK"));
   }
});

However, this only allows me to append segments to the where clause.

What if I wanted to remove the where-clause entirely?

Or, what if I wanted to filter out specific segments of the where clause?

View Entire Topic
marcbecker
Contributor
0 Kudos

In case you want to remove the where condition entirely you can simply return null in the modifier's where(Predicate) method:

CqnSelect copy = CQL.copy(query, new Modifier() {
   @Override
   public Predicate where(Predicate where) {
      return null; // remove where clause
   }
})

The modifier itself automatically traverses inside the structure of the where condition. You can therefore use the Modifiers methods to modify ElementRef, comparisons, connectives or negations to modify the existing where clause. For example this turns all < into <= and all > into >=. Of course you can also modify or exchange the values itself in these methods.

CqnSelect copy = CQL.copy(query, new Modifier() {
    @Override
    public Predicate comparison(Value<?> lhs, Operator op, Value<?> rhs) {
        if(op == Operator.GT) {
            return CQL.comparison(lhs, Operator.GE, rhs);
        } else if (op == Operator.LT) {
            return CQL.comparison(lhs, Operator.LE, rhs);
        }
        return CQL.comparison(lhs, op, rhs);
    }
})
0 Kudos

Thanks a lot Marc! You saved me again.

former_member333492
Discoverer
0 Kudos

Hi,

I have a similar requirement. But I want to remove the clause from where if the "lhs" contains a certain substring. How can I do that?

My where caluse looks like this:

{"SELECT":{"from":{"ref":["ContractService.ContractItem"]},"columns":["*"],"where":[{"ref":["incoTerms_id"]},"is",{"val":"CIF"},"and",{"ref":["deliveryStartDate"]},"=",{"val":"2021-07-25","literal":"date"},"and",{"ref":["deliveryStartDate"]},"=",{"val":"2021-08-26","literal":"date"}],"orderBy":[{"ref":["itemNumber"]},{"ref":["contract_contractNumber"]}],"limit":{"rows":{"val":1000}}}}

I want to remove all the condition from where clause where the ref is "deliveryStartDate". Rest all will remain same. How can I do that?

I am not able to modify Value<?> lhs or rhs. How can I do that too?

Thanks,

Ruchika

marcbecker
Contributor
0 Kudos

You could either use the "Predicate where(Predicate where)" method in the modifier to rebuild and replace the whole where condition or simply replace these comparisons with "true == true", e.g. by using "CQL.constant(true).eq(CQL.constant(true))"