jooq:将时间间隔添加到时间戳 postgres
jooq: Add interval to timestamp postgres
我正在尝试将 timestamptz 值进一步增加到未来的间隔秒数。有没有一种方法可以按摩这些类型,以便 jooq 允许我在一个语句中这样做,或者我只需要获取 TriggerRecord
并在 Java 代码中进行计算?
代码和尝试如下:
public final TableField<TriggerRecord, Instant> PAUSED_UNTIL = createField(DSL.name("paused_until"), SQLDataType.TIMESTAMPWITHTIMEZONE(6), this, "", new OffsetDateTimeInstantConverter());
public class OffsetDateTimeInstantConverter implements Converter<OffsetDateTime, Instant> {
private static Instant min;
public OffsetDateTimeInstantConverter() {
}
public Instant from(OffsetDateTime databaseObject) {
return databaseObject == null ? null : databaseObject.toInstant();
}
public OffsetDateTime to(Instant userObject) {
if (userObject == null) {
return null;
} else {
return userObject.isBefore(min) ? OffsetDateTime.MIN : userObject.atOffset(ZoneOffset.UTC);
}
}
public Class<OffsetDateTime> fromType() {
return OffsetDateTime.class;
}
public Class<Instant> toType() {
return Instant.class;
}
static {
min = OffsetDateTime.MIN.toInstant();
}
在一种情况下会出错
final Long ps = 360;
query = using(configuration)
.update(TRIGGER)
.set(TRIGGER.ACTIVE, active)
.set(TRIGGER.PAUSED_UNTIL,
TRIGGER.PAUSED_UNTIL.add(ps))
.returning()
.fetch();
ERROR: operator does not exist: timestamp with time zone + timestamp with time zone
并且在另一次尝试中出现错误
final var query = using(configuration)
.update(TRIGGER)
.set(TRIGGER.ACTIVE, active)
.set(TRIGGER.PAUSED_UNTIL,
TRIGGER.PAUSED_UNTIL
.add(val(DayToSecond.valueOf(Duration.ofSeconds(ps)))))
org.jooq.exception.DataTypeException: Cannot convert from +0 00:06:00.000000000 (class org.jooq.types.DayToSecond) to class java.time.OffsetDateTime
update trigger set "paused_until" = ("alert"."trigger"."paused_until" + cast(? as timestamp(6) with time zone))
对于这个特定的表达式,这看起来像 bug #12036, which has been fixed in jOOQ 3.17.0, 3.16.4, and 3.15.8. The workaround is to use plain SQL templating。
DSL.field("{0} + {1}",
TRIGGER.PAUSED_UNTIL.getDataType(),
TRIGGER.PAUSED_UNTIL, ps
);
我正在尝试将 timestamptz 值进一步增加到未来的间隔秒数。有没有一种方法可以按摩这些类型,以便 jooq 允许我在一个语句中这样做,或者我只需要获取 TriggerRecord
并在 Java 代码中进行计算?
代码和尝试如下:
public final TableField<TriggerRecord, Instant> PAUSED_UNTIL = createField(DSL.name("paused_until"), SQLDataType.TIMESTAMPWITHTIMEZONE(6), this, "", new OffsetDateTimeInstantConverter());
public class OffsetDateTimeInstantConverter implements Converter<OffsetDateTime, Instant> {
private static Instant min;
public OffsetDateTimeInstantConverter() {
}
public Instant from(OffsetDateTime databaseObject) {
return databaseObject == null ? null : databaseObject.toInstant();
}
public OffsetDateTime to(Instant userObject) {
if (userObject == null) {
return null;
} else {
return userObject.isBefore(min) ? OffsetDateTime.MIN : userObject.atOffset(ZoneOffset.UTC);
}
}
public Class<OffsetDateTime> fromType() {
return OffsetDateTime.class;
}
public Class<Instant> toType() {
return Instant.class;
}
static {
min = OffsetDateTime.MIN.toInstant();
}
在一种情况下会出错
final Long ps = 360;
query = using(configuration)
.update(TRIGGER)
.set(TRIGGER.ACTIVE, active)
.set(TRIGGER.PAUSED_UNTIL,
TRIGGER.PAUSED_UNTIL.add(ps))
.returning()
.fetch();
ERROR: operator does not exist: timestamp with time zone + timestamp with time zone
并且在另一次尝试中出现错误
final var query = using(configuration)
.update(TRIGGER)
.set(TRIGGER.ACTIVE, active)
.set(TRIGGER.PAUSED_UNTIL,
TRIGGER.PAUSED_UNTIL
.add(val(DayToSecond.valueOf(Duration.ofSeconds(ps)))))
org.jooq.exception.DataTypeException: Cannot convert from +0 00:06:00.000000000 (class org.jooq.types.DayToSecond) to class java.time.OffsetDateTime
update trigger set "paused_until" = ("alert"."trigger"."paused_until" + cast(? as timestamp(6) with time zone))
对于这个特定的表达式,这看起来像 bug #12036, which has been fixed in jOOQ 3.17.0, 3.16.4, and 3.15.8. The workaround is to use plain SQL templating。
DSL.field("{0} + {1}",
TRIGGER.PAUSED_UNTIL.getDataType(),
TRIGGER.PAUSED_UNTIL, ps
);