Skip to content

Need a convenience method to add group of SqlCriterion directly #430

Closed
@JoshuaJeme

Description

@JoshuaJeme

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions