您可以执行一个 jOOQ 更新操作,其中值的赋值取决于同一行的另一列的值。

huangapple go评论90阅读模式
英文:

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:
&lt;pre&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;| letter&lt;/td&gt; | &lt;td&gt;is_selected |&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;| A&lt;/td&gt;      | &lt;td&gt;(null)      |&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;| B&lt;/td&gt;      | &lt;td&gt;(null)      |&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;| C&lt;/td&gt;      | &lt;td&gt;(null)      |&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;| ...&lt;/td&gt;    | &lt;td&gt;(null)      |&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/pre&gt;


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(&quot;B&quot;)))

The reasons is that with #11969, Condition is now a subtype of Field&lt;Boolean&gt;, which is what's expected here. Prior to jOOQ 3.17, you can always wrap a Condition in a Field&lt;Boolean&gt; explicitly, using DSL.field(Condition)

.set(OPTIONS.IS_SELECTED, field(OPTIONS.LETTER.equal(val(&quot;B&quot;))))

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(&quot;B&quot;)))

The reasons is that with #11969, Condition is now a subtype of Field&lt;Boolean&gt;, which is what's expected here. Prior to jOOQ 3.17, you can always wrap a Condition in a Field&lt;Boolean&gt; explicitly, using DSL.field(Condition)

.set(OPTIONS.IS_SELECTED, field(OPTIONS.LETTER.equal(val(&quot;B&quot;))))

See also this section of the manual about boolean columns

huangapple
  • 本文由 发表于 2023年4月16日 23:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定