How do I initialize a Condition object without an explicit assignment?
Example :
Condition condition;
condition = CAR.COLOR.eq("blue");
condition = condition.or(CAR.MODEL.eq("Genesis"));
condition = condition.or(CAR.MANUFACTOR.eq("ford").and(CAR.COLOR.eq("blue")));
Is there a way to initialize the org.jooq.Condition object without an explicit assignment? Something like:
Condition condition = new Condition();
And then I want to increment my condition for each loop and return that condition object in some method. But if I do this:
public Condition getRuleConditions(List<Rule> rules){
Condition condition;
for(Rule rule: rules){
condition = condition.or(rule.getField().eq(rule.getValue()));
}
return condition;
}
I am unable to return this Condition object.
+3
source to share
1 answer
You have two options:
Using the original layout Condition
:
Condition condition = DSL.falseCondition();
for (Rule rule : rules) {
condition = condition.or(rule.getField().eq(rule.getValue()));
}
Usage null
:
Condition condition = null;
for (Rule rule : rules) {
Condition c = rule.getField().eq(rule.getValue());
condition = (condition == null) ? c : condition.or(c);
}
In the second example, the result Condition
can be null
, of course, and you have to handle it elsewhere in the code. In the first example, the resulting Condition
will never benull
+1
source to share