Casting into Postgres enum array when using Field.in(..) method

17 views
Skip to first unread message

stan....@gmail.com

unread,
Dec 5, 2017, 1:24:49 AM12/5/17
to jOOQ User Group
I'm using model API without code generation and trying to achieve SQL code with the following condition:

WHERE visitor_device
in (?)::visitor_device[]

where visitor_device is Postgres enum type. Example java code is:
Set<DeviceType> s = EnumSet.of(MOBILE);
query
.addConditions(field("visitor_device").in(devices));

where custom enum DeviceType implements org.jooq.EnumType.

jOOQ correctly obtain enum value using EnumType.getLiteral() method for bind parameter, but the generated array is not cast:
WHERE visitor_device in (?)
It's not a problem if I would need to use String literal directly like this:

Set<DeviceType> s = singleton("mobile");
query
.addConditions(field("visitor_device").in(devices));
However I still don't know how I can do the casting to enum array.

Many thanks!

Stan Svec

unread,
Dec 6, 2017, 12:26:39 PM12/6/17
to jOOQ User Group
I fixed it currently by converting custom Java enum instances into enum data type fields before passing them into in(..) method:
public Set<Field<VisitorDevice>> getVisitorDevicesAsEnumDataType() {
 
return visitorDevices.stream().map(dev -> cast(dev, PostgresDataType.VARCHAR.asEnumDataType(VisitorDevice.class))).collect(toSet());
}


Lukas Eder

unread,
Dec 7, 2017, 3:26:13 AM12/7/17
to jooq...@googlegroups.com
Hi Stan,

Thanks for your message. The easiest way forward in these cases is to use the code generator that is built into jOOQ. It will generate those VARCHAR.asEnumDataType() mappings for you, so you will never need to remember them.

If you cannot or do not want to use the code generator, then you will need to do the exact same thing as the code generator would have done, manually. Your usage of the API is correct, you somehow have to tell the jOOQ runtime API that what appears to be a VARCHAR type is really an enum type, in case of which jOOQ will be able to generate the necessary cast for you.

I hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stan Svec

unread,
Dec 7, 2017, 11:56:49 AM12/7/17
to jOOQ User Group
Hi Lukas,

Good to know I'm doing it correctly. Thanks for the confirmation and also for this great product!

Best,

Stan
Reply all
Reply to author
Forward
0 new messages