简体   繁体   English

jOOQ 动态 WITH 子句

[英]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 need to nest dynamically created columns in order to produce new data from those dynamically created columns我需要嵌套动态创建的列,以便从这些动态创建的列中生成新数据
  • For example, I am creating a new field which is the non-null result from a full outer join.例如,我正在创建一个新字段,它是完全外连接的非空结果。 This field is ONLY available at the time of the query execution, so I need to store it within a WITH clause to reference it in other queries for additional calculations.该字段仅在查询执行时可用,因此我需要将其存储在 WITH 子句中以在其他查询中引用它以进行额外计算。
  • Ideally, I would be able to request a WITH clause type query dynamically, and this dependency can be sorted out by placing this joined dataset in its own CTE to use in downstream references.理想情况下,我将能够动态请求 WITH 子句类型查询,并且可以通过将此连接的数据集放在它自己的 CTE 中以在下游引用中使用来解决这种依赖关系。

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 选择?


LINKS链接

stackoverflow堆栈溢出

jOOQ Website jOOQ网站

GitHub GitHub

jOOQ Google Group jOOQ 谷歌群组

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.

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