jOOQ动态有子句

问题描述 投票:0回答:1

我无法找到有关如何在jOOQ中动态构造WITH子句(即公用表表达式/ CTE)的文档。我的用例是这样的:

  • 我需要嵌套动态创建的列,以便从那些动态创建的列中产生新数据
  • 例如,我正在创建一个新字段,它是完全外部联接的非空结果。该字段仅在执行查询时可用,因此我需要将其存储在WITH子句中,以便在其他查询中引用它以进行其他计算。
  • 理想情况下,我将能够动态请求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 ;

甚至有可能吗?还是仅限于静态表和静态选择?


LINKS

stackoverflow

  • JOIN on a CTE in jOOQ
  • Reference CTE in another CTE #1Reference CTE in another CTE #2
  • jOOQ网站

  • v3.13 Manual : WITH ClauseWithStep Javadocs
  • GitHub

  • Issue 454 : Add CTE Support
  • Issue 3174 : Add CTE Support w/ DML
  • Issue 3175 : Nested CTE
  • Issue 4474 : Form CTE from Plain SQL
  • jOOQ Google网上论坛

  • Using arbitrary SQL string as a CTE in conjunction with the DSL
  • WITH RECURSIVE CTE in Postgres
  • CTEs, recursive CTEs and JOOQ
  • Recursive CTE
  • Recursive CTE
  • 我无法找到有关如何在jOOQ中动态构造WITH子句(即公用表表达式/ CTE)的文档。我的用例是这样的:我需要将动态创建的列嵌套在...
  • java sql postgresql common-table-expression jooq
    1个回答
    0
    投票
    此供应商可以作为响应在RESTful框架中返回,以将结果流回请求者。
    © www.soinside.com 2019 - 2024. All rights reserved.