【发布时间】:2014-05-26 06:30:49
【问题描述】:
我知道 jOOQ 会在不支持它的系统(例如 PostgreSQL)上模拟 SQL MERGE。
我有一个带有串行(自动增量)“id”列和字符串“uri”列的表。我想在我的数据库中使用数字 ID 而不是 URI,所以我必须确保在 ID 查找表中有一个 URI。因此,按照 jOOQ 手册中的示例,我认为这会起作用:
createDSLContext().mergeInto(tableByName("uris"))
.using(createDSLContext().selectOne())
.on(fieldByName("uri").equal("http://example.com/"))
.whenNotMatchedThenInsert(fieldByName("uri"))
.values("http://example.com/").execute();
这给了我一个DataAccessException 说类似的东西:
SQL [merge into "uris" using (select 1) on "uri" = ? when not matched then insert ("uri") values (?)]; ERROR: syntax error at or near "merge"
但随后日志显示 jOOQ 继续并尝试使用绑定值执行查询。但该表永远不会更新。所以我猜 jOOQ 不会在 PostgreSQL 上模拟 MERGE?
所以我然后尝试 H2 数据库语法:
createDSLContext().mergeInto(tableByName("uris"), fieldByName("uri")).values(uri.toString()).execute();
我明白了:
The H2-specific MERGE syntax is not supported in dialect : POSTGRES
什么!?但jOOQ documentation 表示 H2 语法“可以由 jOOQ 完全模拟支持 SQL 标准的所有其他数据库”。 PostgreSQL 肯定支持 SQL 标准。它真的意味着“……MERGE 的 SQL 标准版本吗?”
有什么方法可以通过 jOOQ 获得 PostgreSQL 对 MERGE 的支持,还是我一直在做同样的解决方法?
【问题讨论】:
标签: sql postgresql jooq sql-merge