tsuxkky
tsuxkky

Reputation: 51

How to use 'ANY' function with JOOQ

I currently use JOOQ to get data from postgresql DB. Then I have faced a problem to get data which data-type is array type.

Current condition is like below:

Table schema is:

CREATE TABLE favorites (
    id int,
    items    varchar(100)[]
);

Sample data is:

INSERT INTO favorites (id, items)
    VALUES (1, '{orange, lemon, banana}');
INSERT INTO favorites (id, items)
    VALUES (2, '{apple, grape}');

To get first data, SQL is like:

SELECT id, items FROM favorites WHERE 'orange' = ANY (items);

But, I cannot create sql like above with JOOQ.

Connection connection = ...;
DSLContext context = DSL.using(connection, ...);
List<Table> table = context.select().from(TABLE).fetchInto(class.TABLE);

Can I use ANY function with JOOQ? If it is possible, how can I create it with JOOQ. If not, is there any other ways to get the same result with JOOQ?

Thank you in advance.

Upvotes: 4

Views: 1951

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221275

Do it like this:

List<TableRecord> table = context
    .selectFrom(TABLE)
    .where(val("orange").eq(any(TABLE.ITEMS)))
    .fetch();

The above query uses DSL.any(Field<T[]>)

Upvotes: 4

Related Questions