[英]jOOQ Dynamic WITH Clause
I am unable to find documentation on how to dynamically construct WITH Clauses (ie Common Table Expressions / CTEs) in jOOQ.我无法在 jOOQ 中找到有关如何动态构造 WITH 子句(即公用表表达式 / CTE)的文档。 My use case is this:我的用例是这样的:
I am attempting to use the following, with no luck:我正在尝试使用以下内容,但没有运气:
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() looks something like this: 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
What I would like to do at a minimum is to reference the coalesced
field above in additional downstream queries.我至少想做的是在其他下游查询中引用上面的coalesced
字段。 Ideally I would not be limited at all in the manner or number of dynamic references I could make when constructing a WITH clause in jOOQ.理想情况下,在 jOOQ 中构造 WITH 子句时,我可以进行的动态引用的方式或数量完全不受限制。 In the end, I want to be able to dynamically create queries such as these, which show CTEs referencing CTEs too:最后,我希望能够动态创建诸如此类的查询,这些查询也显示 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
;
Would that even be possible?这甚至可能吗? Or am I limited to static tables and static selections?还是我仅限于 static 表和 static 选择?
The issue turned out to be an issue with Jackson deserialization in a property in my JSON payload which gets turned into my query.这个问题原来是我的 JSON 有效负载中的一个属性中的 Jackson 反序列化问题,该有效负载变成了我的查询。 I was getting a NullPointerException
when one of the properties was getting converted into a part of the query class.当其中一个属性被转换为查询 class 的一部分时,我收到了NullPointerException
。 With regards to jOOQ
, this example works fine.关于jOOQ
,这个例子运行良好。 As an addition, this is a testing query which sums the first field by referencing relative position in the result-set:另外,这是一个测试查询,它通过引用结果集中的相对 position 来对第一个字段求和:
/* Output looks like this :
+-----------------+
|sum_of_everything|
+-----------------+
| 100|
+-----------------+
*/
Supplier<Stream<Map<String, Object>>> resultsWith =
() ->
dslContext
.with("s1")
.as(query)
.select(sum(field("1", Integer.class)).as("sum_of_everything"))
.from(table(name("s1")))
.fetchStream()
.map(Record::intoMap);
This supplier can be returned as a response in a RESTful framework to stream results back to the requestor.该供应商可以作为 RESTful 框架中对 stream 结果的响应返回给请求者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.