Hi,
I am trying to make a Statement using Expression depending on multiple conditions.
public Statement getStatement() {
Expression selectExpression = x(bucketName + ".*");
Expression whereExpression = getEmpty();
if (!StringUtils.isBlank(searchParams.column1())) {
whereExpression = whereExpression.and(x("column1").eq(x("$column1")));
}
if (!StringUtils.isBlank(searchParams.column2())) {
whereExpression = whereExpression.and(x("column2").eq(x("$column2")));
}
Statement selectStatement;
selectStatement = select(selectExpression)
.from(i(bucketName))
.where(whereExpression);
return selectStatement;
}
private Expression getEmpty() {
return path("1=1");
}
In order to avoid multiple if and else while creating statement with where.and, I am seeding the statement with an empty condition getEmpty.
Question:
- Is this correct way?
- Any other alternative?
Thanks,
Ashwani