I had understood that jOOQ would simulate SQL MERGE on systems (such as PostgreSQL) that don't support it.

I have a table with a serial (autoincrement) "id" column and a string "uri" column. I want to use numeric IDs instead of URIs in my database, so I have to make sure I have a URI in the ID lookup table. So following the example in the jOOQ manual, I thought this would work:

createDSLContext().mergeInto(tableByName("uris"))
.using(createDSLContext().selectOne())
.on(fieldByName("uri").equal("http://example.com/"))
.whenNotMatchedThenInsert(fieldByName("uri"))
.values("http://example.com/").execute();

This gives me a DataAccessException saying something like:

SQL [merge into "uris" using (select 1) on "uri" = ? when not matched then insert ("uri") values (?)]; ERROR: syntax error at or near "merge"

But then the log says jOOQ goes ahead and tries to execute the query with bind values. But the table is never updated. So I'm guessing the jOOQ doesn't simulate MERGE on PostgreSQL?

So I then try the H2 database syntax:

createDSLContext().mergeInto(tableByName("uris"), fieldByName("uri")).values(uri.toString()).execute();

I get:

The H2-specific MERGE syntax is not supported in dialect : POSTGRES

What!? But the jOOQ documentation says that the H2 syntax "can be fully simulated by jOOQ for all other databases that support the SQL standard." Surely PostgreSQL supports the SQL standard. Does it really mean "...the SQL standard version of MERGE?"

Is there any way to get PostgreSQL support for MERGE via jOOQ, or am I stuck doing the same workarounds I would do anyway?

有帮助吗?

解决方案

To be sure if a given SQL feature is supported by jOOQ for your database, please consider the Javadoc's @Support annotation on the relevant DSL method. This is also documented in the manual. In this case, DSLContext.mergeInto(), where you can see that this statement is currently only supported for these SQLDialects:

@Support(value={CUBRID,DB2,HSQLDB,ORACLE,SQLSERVER,SYBASE})

MERGE is a very powerful statement that is not really easy to emulate if your database doesn't natively support it.

"can be fully simulated by jOOQ for all other databases that support the SQL standard." Surely PostgreSQL supports the SQL standard. Does it really mean "...the SQL standard version of MERGE?"

Yes of course, the SQL standard MERGE statement must be supported :-) We'll clarify this in the manual. I have registered issue #3183 for this.

Is there any way to get PostgreSQL support for MERGE via jOOQ, or am I stuck doing the same workarounds I would do anyway?

Right now, unfortunately, we don't have a solution for this in PostgreSQL. Feel free to discuss possible solutions on the jOOQ User Group.

其他提示

Yes , it can support which database support the merge in SQL stand. but postgresql unsupport this feature in SQL standard. Please see F312 MERGE statement
F313 Enhanced MERGE statement
F314 MERGE statement with DELETE branch

http://www.postgresql.org/docs/9.3/static/unsupported-features-sql-standard.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top