【问题标题】:Does jOOQ support PostgreSQL array functions and operators?jOOQ 是否支持 PostgreSQL 数组函数和运算符?
【发布时间】:2015-03-24 03:02:43
【问题描述】:

jOOQ 是否支持 PostgreSQL 数组函数和运算符?

array(select ... from ...)
array_length(array)
...

有关概述,请参阅http://www.postgresql.org/docs/current/static/functions-array.html

编辑

我在下面添加了一个示例查询。我还添加了查询来创建和填写表格,因此请尝试查询。

drop table if exists person_country;
drop table if exists person;
drop table if exists country;

create table person
(
    identifier      integer     not null,
    name            text        not null,

    primary key(identifier)
);
create table country
(
    identifier      integer     not null,
    name            text        not null,

    primary key(identifier)
);
create table person_country
(
    person_identifier   integer     not null,
    country_identifier  integer     not null,

    primary key(person_identifier, country_identifier),
    foreign key(person_identifier) references person,
    foreign key(country_identifier) references country
);

insert into person values(1, 'John');
insert into person values(2, 'Bill');
insert into person values(3, 'Jill');

insert into country values(1, 'Sweden');
insert into country values(2, 'China');
insert into country values(3, 'Germany');
insert into country values(4, 'Swiss');

insert into person_country values(1, 1);
insert into person_country values(1, 2);
insert into person_country values(1, 4);

insert into person_country values(2, 3);
insert into person_country values(2, 4);

select  r.identifier, r.name, r.a1 a1
from
(
    select  p.identifier, p.name, array(select pc.country_identifier from person_country pc where pc.person_identifier = p.identifier) a1
    from    person p
) r
where   array_length(a1, 1) >= 1;

【问题讨论】:

标签: java sql arrays postgresql jooq


【解决方案1】:

是的,jOOQ 支持 PostgreSQL 数组。 PostgresDSL 原生支持一些流行的功能。但是,并非所有特定于供应商的内置函数都受支持,因为实在太多了。如果您错过了 jOOQ 的某个功能,您可以随时求助于"plain SQL" 并自己创建对该功能的支持。例如:

@SuppressWarnings({ "rawtypes", "unchecked" })
static <T> Field<T[]> array(Select<? extends Record1<T>> select) {
    return DSL.field("array({0})", (DataType) 
        select.getSelect().get(0).getDataType().getArrayDataType(), select);
}

static Field<Integer> arrayLength(
    Field<? extends Object[]> array,
    Field<Integer> dimension
) {
    return DSL.field("array_length({0}, {1})", Integer.class, array, dimension);
}

请注意,这些特定功能将在 jOOQ 3.6 (#3985) 中得到开箱即用的支持

【讨论】:

  • 该死,模板非常灵活!非常好的解决方案。感谢您的反馈。
  • 是的,jOOQ 不可能支持所有这些特定于供应商的功能,但它们通常很容易集成和以字符串为基础编写...这样,您可以将实现隐藏在后面您自己的自定义 API,它再次是类型安全的。
  • 如果您希望尽可能完整,您可能还希望将单个字段的转换添加到包含该字段的数组中:public static &lt;T&gt; Field&lt;T[]&gt; array(final Field&lt;T&gt; field) { return DSL.field("array[{0}]", (DataType) field.getDataType().getArrayDataType(), field); }
  • @JefJedrison:嗯,是的。不过,我想知道是否应该将那个添加到 DSL 本身,因为它也可以在 HSQLDB 和 H2 上工作......此外,这种方法可能应该采用 varargs 参数......我已经为 #4006 创建了这个。
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 2021-04-16
  • 2012-02-24
  • 2011-08-27
  • 1970-01-01
  • 2022-06-29
  • 2013-08-28
  • 2017-12-05
相关资源
最近更新 更多