Jef*_*son 5 java sql arrays postgresql jooq
jOOQ是否支持PostgreSQL数组函数和运算符?
array(select ... from ...)
array_length(array)
...
Run Code Online (Sandbox Code Playgroud)
有关概述,请参见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;
Run Code Online (Sandbox Code Playgroud)
是的,jOOQ支持PostgreSQL数组.一些流行的功能原生支持PostgresDSL.但是,并非所有特定于供应商的内置函数都受支持,因为它们太多了.如果您错过了jOOQ中的某个功能,您可以随时使用"纯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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,这些特定功能将在jOOQ 3.6(#3985)中支持开箱即用