任何有jOOQ的运算符 [英] ANY operator with jOOQ

查看:198
本文介绍了任何有jOOQ的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解以下内容。我有一个字段和一些值:

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

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

现在,我想表达过滤器 field = ANY({..在 WHERE 子句中的值....})。 PostgreSQL支持这个 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));

您的错误是:


  1. PostgresDSL.array(Select) 对应PostgreSQL的 ARRAY(< SELECT expression>)语法,当您想要将相关子查询的结果收集到数组中时,这非常有用。在你的情况下你不需要那个,因为如果这是你想要的,在这里,你可以简单地使用 Field.in(选择) 。注意,没有 DSL.array(...)

  2. 您引用了 DSL.any(T []) PostgresDSL 的code> 运算符,它是 DSL 的子类。这是有效的,因为Java允许它,但可能会造成混乱。

  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.

这篇关于任何有jOOQ的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆