【问题标题】:How to write OPTIONAL where clauses in JOOQ如何在 JOOQ 中编写可选的 where 子句
【发布时间】:2016-09-11 07:52:20
【问题描述】:
List<MyTable> result = DSL.using(configuration())
                .select()
                .from(MY_TABLE)
                .where(MY_TABLE.ID1.equal(pk_id1))
                        .and(MY_TABLE.ID2.equal(fk_id2))
                        .and(MY_TABLE.ID3.equal(fk_id3))
                .orderBy(MY_TABLE.ID.asc())
                .limit(limit)
                .fetchInto(MY_TABLE)
                .map(mapper());

我正在尝试编写一些代码,以允许我的查询采用三个 OPTIONAL id,例如我希望查询最终成为

select * from my_table where ID1=5 and ID2=6 and ID3=7 .... etc 

但是,我也希望能够排除任何 id 的选项

select * from my_table where ID2=6 and ID3=7 

或

select * from my_table where ID3=7

这样做的问题是第一个“where”子句属于 id one,其余的是 ands,所以如果我做了一个 if 语句并删除了 where,那么我将只剩下

List<MyTable> result = DSL.using(configuration())
                .select()
                .from(MY_TABLE)
                        .and(MY_TABLE.ID2.equal(fk_id2))
                        .and(MY_TABLE.ID3.equal(fk_id3))
                .orderBy(MY_TABLE.ID.asc())
                .limit(limit)
                .fetchInto(MY_TABLE)
                .map(mapper());

它不会工作。

我试图寻找类似 @​​987654326@ 的东西,其中 * 基本上没有过滤器,但我找不到类似的东西。

【问题讨论】:

    标签: java-8 jooq


    【解决方案1】:

    jOOQ 让编写 SQL 感觉就像是静态的嵌入式 SQL。但事实并非如此。每个 jOOQ 查询都是由表达式树组成的动态 SQL 查询 - 您只是没有注意到它。

    SelectWhereStep.where(Condition) 方法采用Condition 参数,您不必将其与WHERE 子句一起放在那里。您可以在查询之前构造它:

    Condition condition = DSL.noCondition(); // Alternatively, use trueCondition()
    if (something)
        condition = condition.and(MY_TABLE.ID1.equal(pk_id1));
    if (somethingElse)
        condition = condition.and(MY_TABLE.ID2.equal(fk_id2));
    if (somethingOther)
        condition = condition.and(MY_TABLE.ID3.equal(fk_id3));
    

    您现在可以将其传递给您的查询:

    List<MyTable> result = 
    DSL.using(configuration())
       .select()
       .from(MY_TABLE)
       .where(condition)
       .orderBy(MY_TABLE.ID.asc())
       .limit(limit)
       .fetchInto(MY_TABLE)
       .map(mapper());
    

    DSL中也有实用方法,如:

    这也记录在手册中:http://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql/

    请注意,通常确实不需要直接引用 XYZStep 类型。 You should mostly be able to write dynamic SQL through more elegant ways, as I've shown in this blog post.

    【讨论】:

    • 我正在尝试,但由于某种原因,trueCondition 始终评估为 true,因此我的查询只返回所有内容。还有其他条件可以使用吗?
    • @LukeXu:是的,这就是 trueCondition 的目的... :) 你确定将每个新条件分配回变量吗? and() 操作创建了一个新条件。它不会修改左侧
    【解决方案2】:

    您所要求的有时被称为“SQL '数字'通配符”,或者如果您搜索的话,至少您可以在网络上找到 cmets。

    SQL 不允许你写“where id = *”,如果你需要你的 DDL 保持静态,你可以通过像

    这样的范围检查来模拟它
    select * from table
      where
        (my_table.id1 >= fk_id1_low and my_table.id1 <= fk_id1_high) and
        (my_table.id2 >= fk_id2_low and my_table.id2 <= fk_id2_high) and
        (my_table.id3 >= fk_id3_low and my_table.id3 <= fk_id3_high)
    

    所以现在您总共向查询传递了 6 个参数,如果您想对 id1 进行匹配,您需要将 fk_id1_low 和 fk_id1_high 都设置为您想要匹配的值。如果您不想与 id1 匹配,则将 fk_id1_low 设置为可能的最小值,将 fk_id1_high 设置为可能的最大值。您必须考虑这样做的一件事是数据库引擎将如何处理生成的查询,因为可能会执行大量额外工作。

    对于 JOOQ,另一个可能的答案是远离流畅的界面,以便您可以使用 if..then 条件部分构建查询。

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2023-03-18
      • 1970-01-01
      • 2022-08-08
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 2013-09-19
      相关资源
      最近更新 更多