【问题标题】:Howto expose a native SQL function as a predicate如何将原生 SQL 函数公开为谓词
【发布时间】:2020-10-15 02:50:05
【问题描述】:

我的数据库中有一个表,它将字符串值列表存储为 jsonb 字段。

create table rabbits_json (
  rabbit_id bigserial primary key, 
  name text, 
  info jsonb not null
);

insert into rabbits_json (name, info) values
  ('Henry','["lettuce","carrots"]'),
  ('Herald','["carrots","zucchini"]'),
  ('Helen','["lettuce","cheese"]');

我想过滤我的行,检查 info 是否包含给定值。 在 SQL 中,我会使用 ? operator:

select * from rabbits_json where info ? 'carrots';

如果我今天的谷歌搜索技能很好,我相信这还没有在 JOOQ 中实现: https://github.com/jOOQ/jOOQ/issues/9997

如何在查询中使用原生谓词在 JOOQ 中编写等效查询?

【问题讨论】:

    标签: jooq


    【解决方案1】:

    对于 jOOQ 本身不支持的任何内容,您应该使用plain SQL templating,例如

    Condition condition = DSL.condition("{0} ? {1}", RABBITS_JSON.INFO, DSL.val("carrots"));
    

    不幸的是,在这种特定情况下,您将运行into this issue here。使用 JDBC PreparedStatement,您仍然不能将 ? 用于绑定变量以外的其他用途。作为一种解决方法,您可以:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-13
      • 2015-08-11
      • 2014-04-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      相关资源
      最近更新 更多