JOOQ:如何使用 postgres generate_series 并插入 table?
JOOQ: How to use postgres generate_series and insert into table?
我正在尝试使用 JOOQ 编写此 PostgreSQL 查询:
INSERT INTO my_table (date, from_city, to_city, rate_per_mile)
select g.dt::date, 'city1', 'city2', 13.12
from generate_series(current_date, current_date + 364, interval '1 day') as g(dt);
我对 JOOQ 有点陌生,看到了 here 我正在寻找的一部分,我得到了这个:
var advanceDates = DSL.table("generate_series({0}, {0} + 364, interval '1 day')",
currencyConversion.getDate()).as("dates");
dslContext.insertInto(CURRENCY_CONVERSION).values(
dslContext
.select(
advanceDates.field(0),
val("city1"),
val("city2"),
val(13.12))
.from(advanceDates));
但我认为advanceDates.field(0)
不是正确的用法,它返回null
。
如何在 JOOQ 中形成此查询?
您不能通过索引从 plain SQL table 中取消引用字段,因为 jOOQ 运行时模型不知道 table 的任何字段。我怀疑这里的问题是您也不知道该名称,因为它是一个 table 值函数并且您没有明确命名该字段。如何明确命名它:
var advanceDates = DSL.table("generate_series({0}, {0} + 364, interval '1 day')",
currencyConversion.getDate()).as("dates", "d");
然后像这样创建对它的引用:
.select(
field(name("dates", "d"), currencyConversion.getDate().getDataType())
...
)
一如既往,假设此静态导入:
import static org.jooq.impl.DSL.*;
我正在尝试使用 JOOQ 编写此 PostgreSQL 查询:
INSERT INTO my_table (date, from_city, to_city, rate_per_mile)
select g.dt::date, 'city1', 'city2', 13.12
from generate_series(current_date, current_date + 364, interval '1 day') as g(dt);
我对 JOOQ 有点陌生,看到了 here 我正在寻找的一部分,我得到了这个:
var advanceDates = DSL.table("generate_series({0}, {0} + 364, interval '1 day')",
currencyConversion.getDate()).as("dates");
dslContext.insertInto(CURRENCY_CONVERSION).values(
dslContext
.select(
advanceDates.field(0),
val("city1"),
val("city2"),
val(13.12))
.from(advanceDates));
但我认为advanceDates.field(0)
不是正确的用法,它返回null
。
如何在 JOOQ 中形成此查询?
您不能通过索引从 plain SQL table 中取消引用字段,因为 jOOQ 运行时模型不知道 table 的任何字段。我怀疑这里的问题是您也不知道该名称,因为它是一个 table 值函数并且您没有明确命名该字段。如何明确命名它:
var advanceDates = DSL.table("generate_series({0}, {0} + 364, interval '1 day')",
currencyConversion.getDate()).as("dates", "d");
然后像这样创建对它的引用:
.select(
field(name("dates", "d"), currencyConversion.getDate().getDataType())
...
)
一如既往,假设此静态导入:
import static org.jooq.impl.DSL.*;