简体   繁体   中英

Remove “Select * from dual” from UNION in JOOQ

Here's the code:

SelectQuery<Record> resultQuery = dslContext.selectQuery();
for(int floor : floors) {
    method1(resultQuery,floor);
}

And then:

method1(SelectQuery<Record> resultQuery,int floor){
    SelectQuery<Record> query = dslContext.selectQuery();
    /*
     Now I am creating subqueries for parameter floor by adding condition 
     in above created query 
    */

    resultQuery.union(query);
}

Issue is that in final query(resultQuery) I am getting "Select * from dual" appended. It is adding because I created "resultQuery" as empty.

Is there any way I can replace or remove "Select * from dual" from final query.

Thanks

Imperative style:

SelectQuery<Record> result = null;
for (int floor : floors) {
    result = method1(result, floor);
}

With:

SelectQuery<Record> method1(SelectQuery<Record> result, int floor) {
    SelectQuery<Record> query = dslContext.selectQuery();

    /* your logic ... */

    if (result == null)
        return query;
    else
        return result.union(query);
}

Functional style

Optional<Select<Record>> result =
floors.stream()
      .map(this::method2)
      .reduce(Select::union)

With:

Select<Record> method2(int floor) {
    SelectQuery<Record> query = dslContext.selectQuery();

    /* your logic ... */

    return query;
}

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM