ANY operator with jOOQ

雨燕双飞 提交于 2019-12-08 06:35:46

问题


I am having troubles understanding the following. I have a field and some values:

Field<T> field = ...;
List<T> values = ...;

Now, I want to express the filter field = ANY({... the values ....}) in a WHERE clause. PostgreSQL supports this ANY(array of values) operator. I got this idea from https://blog.jooq.org/2017/03/30/sql-in-predicate-with-in-list-or-with-array-which-is-faster/.

I tried the following to create a condition:

field.equal(PostgresDSL.any(PostgresDSL.array(values)));

The above doesn't work. This compiles:

field.equal((QuantifiedSelect) PostgresDSL.any(PostgresDSL.array(values)));

First question: Why is this additional cast necessary? Should the API be altered? I am not sure on which side (mine vs. jOOQ's) the generics error actually is.

However, after this change, the query itself is not working. It gives the error:

org.jooq.exception.SQLDialectNotSupportedException: Type class java.util.ArrayList is not supported in dialect DEFAULT

Second question: How do I declare/create an array of values? The call PostgresDSL.array(...) above is falling back to DSL.array(...), which is probably root of the second problem?


回答1:


Use DSL.any(T[]):

// Using varargs
field.equal(DSL.any(1, 2, 3));

// Using an actual array
Integer array = { 1, 2, 3 };
field.equal(DSL.any(array));

Your mistakes are:

  1. PostgresDSL.array(Select) corresponds to PostgreSQL's ARRAY(<SELECT expression>) syntax, which is useful when you want to collect the results of a correlated subquery into an array. You don't need that in your case, because if that's what you wanted, here, you could simply use Field.in(Select). Note, there is no DSL.array(...)
  2. You referenced the DSL.any(T[]) operator from PostgresDSL, which is a subclass of DSL. This works because Java allows it, but might have contributed to the confusion here.


来源:https://stackoverflow.com/questions/45835065/any-operator-with-jooq

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