Closed
Description
When building query for a query param, conditions may be optional. It's hard to determind which condition should be use as the initial one.
Assume that we have a query like (cond1 = 1 or cond2 = 2) and cond3 = 3
and a param class below.
static class Param {
Integer cond1;
Integer cond2;
Integer cond3;
}
cond1/2/3 are all optional, if absent, the condition will be ignored.
In this case, SqlCriterion and(BindableColumn<T> column, VisitableCondition<T> condition, SqlCriterion...subCriteria)
is not so convenience.
If we provide a way that can add list of conditions without specifing the initial one, it will be much better.
List<SqlCriterion> conditions = new LinkedList<>();
QueryExpressionDSL<SelectModel> dsl = select(column1, column2).from(table);
QueryExpressionWhereBuilder builder = dsl.where();
Optional.ofNullable(param.cond1).map(c -> or(column1, isEqualTo(c))).ifPresent(conditions::add);
Optional.ofNullable(param.cond2).map(c -> or(column2, isEqualTo(c))).ifPresent(conditions::add);
builder.and(conditions);
if (null != param.cond3) {
builder.and(column3, isEqualTo(param.cond3));
}
SelectStatementProvider selectStatement = dsl.build().render(RenderingStrategies.MYBATIS3);
Metadata
Metadata
Assignees
Labels
No labels