问题描述
尝试使用 jdbi 在 Postgresql 中更新或插入包含 INTERVAL 的表时,出现以下错误:
org.jdbi.v3.core.statement.UnabletoCreateStatementException: No argument factory registered for '1 years 0 mons 0 days 0 hours 0 mins 0.0 secs' of qualified type org.postgresql.util.PGInterval [...]
最小可重现示例:
- 在数据库中:
CREATE TABLE testing (i INTERVAL);
- 在 Kotlin 中(在普通 Java 中可能会产生相同的结果):
jdbi.create(PGSimpleDataSource().apply {
setURL([connection string goes here])
}).withHandle<Int,sqlException> { handle: Handle ->
handle.createUpdate("INSERT INTO testing(i) VALUES (:interval)")
.bind("interval",PGInterval(1,0.0))
.execute()
}
我尝试将它作为字符串插入,因为查询接受“P1Y”,但这给了我 ERROR: column "i" is of type interval but expression is of type character varying
,如果第一种方法有效,这将是有意义的。
似乎只有 PGIntervals 是预期的工作类型才有意义,因为从结果集中获取间隔时,会返回 PGInterval。
虽然我有点怀疑这是否重要,但我正在使用:
- Postgresql 13.2 (Debian 13.2-1.pgdg100+1) 在 x86_64-pc-linux-gnu 上,由 gcc (Debian 8.3.0-6) 8.3.0 编译,64 位
- org.jdbi:jdbi3-core:3.18.1 + org.jdbi:jdbi3-kotlin:3.18.1 + org.postgresql:postgresql:42.2.19
解决方法
我认为您应该使用 PostgreSQL JDBI Plugin 来处理 PostgreSQL 特定的类型。