boden
boden

Reputation: 1681

How to execute specific query using jOOQ

I am using jOOQ in my project that is working with PostgreSQL database. I don't have any problems with query execution without specific condition, but how can I execute the following query with jOOQ ?

-- missed the top of query
WHERE pow.tags @> ARRAY [?] 
AND pow.tags @> ARRAY [?] 
AND position('access,yes' IN array_to_string(pow.tags, ',')) = 0 
AND city = ?

Sometimes, I need to execute requests with PostGIS functions, how must I write its in jOOQ?

Upvotes: 1

Views: 172

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220877

Here's how to write that predicate:

Condition condition =
     POW.TAGS.contains(new String[] { "a", "b", "c" })
.and(POW.TAGS.contains(new String[] { "b", "c", "d" }) // Just assuming some tags here
.and(position("access,yes", arrayToString(POW.TAGS, ",")).eq(zero()))
.and(POW.CITY.eq("city"));

The above answer is assuming the following static imports:

import static org.jooq.impl.DSL.*;
import static org.jooq.util.postgres.PostgresDSL.*;

Possible issue with Field.contains()

There's a known issue with Field.contains() in the event when your TAGS column is of type TEXT, rather than VARCHAR:

https://github.com/jOOQ/jOOQ/issues/4754

The workaround is to use plain SQL:

public static Condition myContains(Field<String[]> field, String[] array) {
    return contains("{0} @> {1}", field, val(array));
}

Upvotes: 2

Related Questions