英文:
How can I perform a jOOQ update where the value assignment depends on the value of another column of the same row?
问题
Here's the translated code part:
使用Postgres 14.5(和jOOQ 3.17.6),假设以下`options`表定义:
CREATE TABLE options (
letter uuid NOT NULL,
is_selected bool NULL,
);
...带有数据:
| letter | is_selected |
|--------|-------------|
| A | (null) |
| B | (null) |
| C | (null) |
| ... | (null) |
如何编写更新以指示选择了`B`,而其他值没有?
我尝试了一些方法:
```java
jooq
.update(OPTIONS)
.set(OPTIONS.IS_SELECTED, OPTIONS.LETTER == val("B")) //<-- 这不起作用,因为我认为这是一个内存地址比较
.set(OPTIONS.IS_SELECTED, OPTIONS.LETTER.equal(val("B"))) //<-- 这不会编译
.execute()
Please note that the code is provided for reference, and I've translated it as requested. If you have any further questions or need additional assistance, please feel free to ask.
<details>
<summary>英文:</summary>
Using Postgres 14.5 (and jOOQ 3.17.6), assume the following `options` table definition:
CREATE TABLE options (
letter uuid NOT NULL,
is_selected bool NULL,
);
...with data:
<pre>
<table>
<tr><td>| letter</td> | <td>is_selected |</td></tr>
<tr><td>| A</td> | <td>(null) |</td></tr>
<tr><td>| B</td> | <td>(null) |</td></tr>
<tr><td>| C</td> | <td>(null) |</td></tr>
<tr><td>| ...</td> | <td>(null) |</td></tr>
</table>
</pre>
How can I write an update to indicate that `B` was selected and the other values were not?
I tried a few things:
jooq
.update(OPTIONS)
.set(OPTIONS.IS_SELECTED, OPTIONS.LETTER == val("B")) <-- this didn't work because I
think this a memory address
comparison
.set(OPTIONS.IS_SELECTED, OPTIONS.LETTER.equal(val("B"))) <-- this doesn't compile
.execute()
</details>
# 答案1
**得分**: 2
Starting from jOOQ 3.17 and [#11969][1], this compiles just fine, assuming your `BOOL` column is really a `BOOLEAN`:
```java
.set(OPTIONS.IS_SELECTED, OPTIONS.LETTER.equal(val("B")))
The reasons is that with #11969, Condition
is now a subtype of Field<Boolean>
, which is what's expected here. Prior to jOOQ 3.17, you can always wrap a Condition
in a Field<Boolean>
explicitly, using DSL.field(Condition)
.set(OPTIONS.IS_SELECTED, field(OPTIONS.LETTER.equal(val("B"))))
See also this section of the manual about boolean columns
英文:
Starting from jOOQ 3.17 and #11969, this compiles just fine, assuming your BOOL
column is really a BOOLEAN
:
.set(OPTIONS.IS_SELECTED, OPTIONS.LETTER.equal(val("B")))
The reasons is that with #11969, Condition
is now a subtype of Field<Boolean>
, which is what's expected here. Prior to jOOQ 3.17, you can always wrap a Condition
in a Field<Boolean>
explicitly, using DSL.field(Condition)
.set(OPTIONS.IS_SELECTED, field(OPTIONS.LETTER.equal(val("B"))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论