Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange translation of jOOQ query for array contains function

I have the following type in my PostgreSQL database :

myoptions text[]

I use a jOOQ converter so that I have a Set as corresponding type in my record :

Set<String> myoptions

In my query I have the following Condition :

c.MYOPTIONS.contains(Sets.newHashSet("option1"))

which is translated in SQL like this :

cast("c"."myoptions" as varchar) like ('%' || '[option1]' || '%') escape '!'

Is it the normal behavior ?

I would like to have something like :

c.myoptions @> ARRAY['option1']

or

'option1' = ANY(c.myoptions)

Thanks in advance for your help

like image 455
Manu Avatar asked Oct 16 '16 22:10

Manu


2 Answers

jOOQ currently (as of version 3.8) does not recognise your custom data type as still being an array data type in PostgreSQL, which is why the Field.contains() default behaviour kicks in - i.e. the one that treats all values as strings.

I've created feature request #5602 for this. As a workaround, you might need to roll your own using plain SQL:

public static <T, C extends Collection<T>> Condition contains(
    Field<? extends C> left, 
    C right
) {
    return DSL.condition("{0} @> {1}::text[]", left, DSL.val(right, left.getDataType()));
}

... which you can then use as such:

contains(c.MYOPTIONS, Sets.newHashSet("option1"))
like image 53
Lukas Eder Avatar answered Nov 06 '22 08:11

Lukas Eder


Please try following solution. Worked in my case.

select.where(ARRAY_FIELD.contains(DSL.cast(DSL.array(VALUE), ARRAY_FIELD.getDataType())));
like image 41
Arnold Krutolevich Avatar answered Nov 06 '22 07:11

Arnold Krutolevich