jooq: How to configure dialect for static DSL methods?

末鹿安然 提交于 2021-01-28 14:23:16

问题


I've got dsl with POSTGRES_9_4 dialect. I try to use custom selection query with org.jooq.impl.DSL.Condition:

dsl.selectFrom(TAG_JSON).where(condition("translations ??| array[?]", normValues)).fetch(mapper);

It throws an exception:

org.jooq.exception.SQLDialectNotSupportedException: Type class java.util.ArrayList is not supported in dialect DEFAULT
    at org.jooq.impl.DefaultDataType.getDataType(DefaultDataType.java:757)
    at org.jooq.impl.DefaultDataType.getDataType(DefaultDataType.java:704)
    at org.jooq.impl.DSL.getDataType(DSL.java:14371)
    at org.jooq.impl.Utils.queryParts(Utils.java:1565)
    at org.jooq.impl.SQLImpl.<init>(SQLImpl.java:64)
    at org.jooq.impl.DSL.sql(DSL.java:6240)
    at org.jooq.impl.DSL.condition(DSL.java:7323)

Why DEFAULT dialect is used? How to configure global one?


回答1:


The error message is a bit misleading. Your intention seems for array[?] to take a List<String> as a single bind value to pass to an array constructor. This won't work. You have two options:

Binding a single array

condition("translations <op> ?::text[]", normValues.toArray(new String[0]))

Binding the array as a list of bind values

condition("translations <op> {0}", list(
    normValues.stream().map(DSL::val).toArray(QueryPart[]::new)
))

This is using DSL.list() to create a list of bind variables.

Note, in both cases, I have avoided your ??| operator because jOOQ parses plain SQL text for bind values (?) and currently doesn't recognise ??| as a non-bind variable sequence (I've registered an issue for this). If that operator has a textual alternative representation, I recommend using that instead.



来源:https://stackoverflow.com/questions/37485419/jooq-how-to-configure-dialect-for-static-dsl-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!