jOOQ-如何在条件更新中使用.whereNotExists()? [英] jOOQ - how to use .whereNotExists() with conditional update?
问题描述
我有一个以下jOOQ查询,它最初是由 Lukas Eder 组成的。 https://stackoverflow.com/questions/48563726/jooq-how-to-use-wherenotexists-properly>此问题。
create.insertInto(DATA,DATA.TICKER,DATA.OPEN,DATA.HIGH,DATA.LOW,DATA.CLOSE,DATA.DATE)
.select(
select(
val(dailyData.getTicker()),
val(dailyData.getOpen()),
val(dailyData.getHigh()),
val(dailyData.getLow()) ,
val(dailyData.getClose()),
val(dailyData.getDate())
)
.whereNotExists(
selectOne()
。 from(DATA)
.where(DATA.DATE.eq(dailyData.getDate()))
)
).execute();
此查询正常运行。另外,我想进行修改以完成以下任务,但是我不确定它是否确实可行。用简单的英语:
如果表中不存在具有相同 date列的行,则插入该行。如果存在并且实时关闭列为true,请更新关闭,否则不执行任何操作。
第一部分已被现有查询覆盖,第二部分包含if ...更新...不是,这就是我需要的帮助。
在普通PostgreSQL中,您可以按以下方式编写此查询:
< pre $ = lang-sql prettyprint-override>
插入数据(股票代号,开盘价,最高价,最低价,收盘价,日期)
值(:ticker,:open,:high 、:低,:close,:date)
发生冲突(日期)
执行更新设置close = false WHERE关闭
这将转换为以下jOOQ查询:
DSL.using (配置)
.insertInto(DATA)
.columns(
DATA.TICKER,
DATA.OPEN,
DATA.HIGH,
DATA.LOW ,
DATA.CLOSE,
DATA.DATE)
.values(
dailyData.getTicker(),
dailyData.getOpen(),
dailyData。 getHigh(),
dailyData.getLow(),
dailyData.getClose(),
dailyData.getDate())
.onConflict()
.doUpdate()
.set(DATA.CLOSE,inline(fals e))
.where(DATA.CLOSE)
.execute();
I have a following jOOQ query, originally composed with the help of Lukas Eder in this question.
create.insertInto(DATA,DATA.TICKER,DATA.OPEN,DATA.HIGH,DATA.LOW,DATA.CLOSE,DATA.DATE)
.select(
select(
val(dailyData.getTicker()),
val(dailyData.getOpen()),
val(dailyData.getHigh()),
val(dailyData.getLow()),
val(dailyData.getClose()),
val(dailyData.getDate())
)
.whereNotExists(
selectOne()
.from(DATA)
.where(DATA.DATE.eq(dailyData.getDate()))
)
).execute();
This query works properly. In addition, I would like to modify to accomplish the following feat, but I am not certain it is actually doable. In simple english:
"Insert the row if a row with the same 'date' column doesn't already exist in the table. If it exists AND 'realtime close' column is true, update the 'close', otherwise do nothing."
The first part is already covered by the existing query, but the second part with if...update... is not and that's what I need help with.
In plain PostgreSQL, you would write this query as follows:
INSERT INTO data (ticker, open, high, low, close, date)
VALUES (:ticker, :open, :high, :low, :close, :date)
ON CONFLICT (date)
DO UPDATE SET close = false WHERE close
This translates to the following jOOQ query:
DSL.using(configuration)
.insertInto(DATA)
.columns(
DATA.TICKER,
DATA.OPEN,
DATA.HIGH,
DATA.LOW,
DATA.CLOSE,
DATA.DATE)
.values(
dailyData.getTicker(),
dailyData.getOpen(),
dailyData.getHigh(),
dailyData.getLow(),
dailyData.getClose(),
dailyData.getDate())
.onConflict()
.doUpdate()
.set(DATA.CLOSE, inline(false))
.where(DATA.CLOSE)
.execute();
这篇关于jOOQ-如何在条件更新中使用.whereNotExists()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!