任何有jOOQ的运算符 [英] ANY operator with jOOQ
问题描述
我很难理解以下内容。我有一个字段和一些值:
I am having troubles understanding the following. I have a field and some values:
Field<T> field = ...;
List<T> values = ...;
现在,我想表达过滤器 field = ANY({..在
。 PostgreSQL支持这个 WHERE
子句中的值....}) ANY(值数组)
运算符。我从 https://blog.jooq.org/2017/03/30/sql-in-predicate-with-in-list-or-with-array-which-is-faster/ 。
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)));
第一个问题:为什么需要额外演员? API应该改变吗?我不确定generics错误实际上是哪一方(我和jOOQ)。
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
第二个问题:如何声明/创建值数组?上面的电话 PostgresDSL.array(...)
回落到 DSL.array(...)
,这可能是第二个问题的根源?
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?
推荐答案
使用 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));
您的错误是:
-
PostgresDSL.array(Select)
对应PostgreSQL的ARRAY(< SELECT expression>)
语法,当您想要将相关子查询的结果收集到数组中时,这非常有用。在你的情况下你不需要那个,因为如果这是你想要的,在这里,你可以简单地使用Field.in(选择)
。注意,没有DSL.array(...)
- 您引用了
DSL.any(T [])来自
运算符,它是PostgresDSL
的code>DSL
的子类。这是有效的,因为Java允许它,但可能会造成混乱。
PostgresDSL.array(Select)
corresponds to PostgreSQL'sARRAY(<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 useField.in(Select)
. Note, there is noDSL.array(...)
- You referenced the
DSL.any(T[])
operator fromPostgresDSL
, which is a subclass ofDSL
. This works because Java allows it, but might have contributed to the confusion here.
这篇关于任何有jOOQ的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!