document 'on conflict' in query language guide

This commit is contained in:
Gavin King 2024-03-02 09:37:05 +01:00
parent e02317f3d6
commit 0d7a7087ff
3 changed files with 31 additions and 1 deletions

View File

@ -427,6 +427,24 @@ It's not available for entities whose id generator is implemented in Java, nor f
The same two options are available for a `@Version` attribute. The same two options are available for a `@Version` attribute.
When no version is explicitly specified, the version for a new entity instance is used. When no version is explicitly specified, the version for a new entity instance is used.
The `on conflict` clause lets us specify what action should be taken when the database already contains the record we're attempting to insert.
[source, antlrv4]
----
include::{extrasdir}/on_conflict_bnf.txt[]
----
Note that the `on constraint` variant accepting the name of a unique constraint only works on certain databases, or when just a single row is being inserted.
[[onconflict-example]]
[source, hql]
----
insert Person (ssn, name, phone)
values ('116-76-1234', 'Jane Doe', '404 888 4319')
on conflict (ssn) set phone = excluded.phone
----
Like `update` and `delete` statements, an `insert` statement must be executed by calling `Query.executeUpdate()`. Like `update` and `delete` statements, an `insert` statement must be executed by calling `Query.executeUpdate()`.
Now it's time to look at something _much_ more complicated. Now it's time to look at something _much_ more complicated.

View File

@ -0,0 +1,10 @@
conflictClause
: ON CONFLICT conflictTarget? "DO" conflictAction
conflictTarget
: ON CONSTRAINT identifier
| "(" simplePath ("," simplePath)* ")"
conflictAction
: "NOTHING"
| "UPDATE" setClause whereClause?

View File

@ -1,5 +1,7 @@
insertStatement insertStatement
: "INSERT" "INTO"? targetEntity targetFields (queryExpression | valuesList) : "INSERT" "INTO"? targetEntity targetFields
(queryExpression | valuesList)
conflictClause?
targetEntity targetEntity
: entityName variable? : entityName variable?