我无法找到有关如何在jOOQ中动态构造WITH子句(即公用表表达式/ CTE)的文档。我的用例是这样的:
我正在尝试使用以下内容,没有运气:
SelectQuery<Record> query =
getQuery(
dslContext,
selectFields,
fromClause,
groupFields,
conditionClause,
orderFields,
query.getOffset(),
query.getLimit());
// WORKS JUST FINE
Supplier<Stream<Map<String, Object>>> results = () ->
query
.fetchStream()
.map(Record::intoMap);
// make a nested query here. static for now.
// DOES NOT WORK
Supplier<Stream<Map<String, Object>>> resultsWith =
() ->
DSL.with("s1")
.as(query) // Shouldn't I be able to reference a SelectQuery here?
.select()
.from(table(name("s1")))
.fetchStream()
.map(Record::intoMap);
query.toString()看起来像这样:
select
table1.field1,
coalesce(
table1.id,
table2.id) as table1.id_table2.id,
count(*) as table2.field1.count,
sum(table2.field2) as table2.field2.sum
from table1.table1 as table1
full outer join table2.table2 as table2
on table1.id = table2.id
where table1.field2 < 3000.0
group by
table1.id_table2.id,
table1.field1
order by table1.field1 asc
limit 100
我想做的[至少]是在其他下游查询中引用上面的coalesced
字段。理想情况下,在jOOQ中构造WITH子句时,我完全不受动态引用的方式或数量的限制。最后,我希望能够动态创建诸如此类的查询,这些查询也显示了引用CTE的CTE:-- WITH Clause usage is preferrable
with
myFirstSelection as (
select
(id + 100) as newfield
from table1.table1 n
),
mySecondSelection as (
select
(newField + 200) as newerField
from myFirstSelection
)
select
*
from mySecondSelection
;
-- Inline queries, while not ideal, would be permissible
select
*
from (
select
(newField + 200) as newerField
from (
select
(id + 100) as newField
from table1.table1 n
) as myFirstSelection
) as mySecondSelection
;
甚至有可能吗?还是仅限于静态表和静态选择?