如何在jOOQ中根据另一列更新一次性列

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

How to update one time column based on another in jOOQ

问题

假设一个超级简单的用例:
想象一下,我有一个表(dummyTable),其中包含以下列:

id
dueAt
createdAt

如何像下面这样使用jOOQ执行批量更新:

update dummyTable
set dueAt = TIMESTAMPADD(MINUTE, 5, createdAt) 
where ids in (...)

我在网上看到一些示例,它们使用硬编码和提供的值 -
https://www.jooq.org/doc/3.13/manual/sql-building/column-expressions/datetime-functions/timestampadd-function/。但我无法弄清楚如何让TIMESTAMPADD函数与现有列值一起工作。

在空白处应该填写什么?

return rwContext
        .update(DUMMYTABLE)
        .set(DUMMYTABLE.DUEAT, ______________ )
        .where(DUMMYTABLE.ID.in(someIds))
        .execute();

我尝试在空白处使用以下代码,但它会导致编译错误:

timestampAdd(Timestamp.valueOf(DUMMYTABLE.CREATEDAT), 5, DatePart.MINUTE)

任何帮助或指导都将不胜感激。

英文:

Let's assume a super simple use-case:
Imagine, I have a table (dummyTable) with these columns:

id
dueAt
createdAt

How do I perform a bulk update like the one below using jOOQ

update dummyTable
set dueAt = TIMESTAMPADD(MINUTE, 5, createdAt) 
where ids in (...)

I see examples online that uses hardcoded and supplied values -
https://www.jooq.org/doc/3.13/manual/sql-building/column-expressions/datetime-functions/timestampadd-function/. But I couldn't figure out how to get the TIMESTAMPADD function work with existing column values.

What should I have it in the blank?

return rwContext
        .update(DUMMYTABLE)
        .set(DUMMYTABLE.DUEAT, ______________ )
        .where(DUMMYTABLE.ID.in(someIds))
        .execute();

I tried the below code in the blank but it gives a compilation error

timestampAdd(Timestamp.valueOf(DUMMYTABLE.CREATEDAT), 5, DatePart.MINUTE)

Any help or guidance is appreciated.

答案1

得分: 1

Timestamp.valueOf(String) 是 JDBC API 用于从字符串文字创建 Timestamp 值的方法,例如:

Timestamp.valueOf("2000-01-01 00:00:00.0");

您不能将 jOOQ 的 Field<Timestamp> 传递给它。但是为什么要这样做呢?只需直接将字段引用传递给函数:

timestampAdd(DUMMYTABLE.CREATEDAT, 5, DatePart.MINUTE);

或者,如果使用 JSR 310 类型,DSL.localDateTimeAdd()

localDateTimeAdd(DUMMYTABLE.CREATEDAT, 5, DatePart.MINUTE);
英文:

Timestamp.valueOf(String) is JDBC API to create a Timestamp value from a string literal, e.g.

Timestamp.valueOf("2000-01-01 00:00:00.0");

You cannot pass a jOOQ Field<Timestamp> to it. But why would you? Just pass the field reference to the function directly:

timestampAdd(DUMMYTABLE.CREATEDAT, 5, DatePart.MINUTE);

Or, if using JSR 310 types, DSL.localDateTimeAdd():

localDateTimeAdd(DUMMYTABLE.CREATEDAT, 5, DatePart.MINUTE);

huangapple
  • 本文由 发表于 2020年7月31日 23:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63194822.html
匿名

发表评论

匿名网友

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

确定