Skip to content

Commit 0d7a708

Browse files
committed
document 'on conflict' in query language guide
1 parent e02317f commit 0d7a708

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

documentation/src/main/asciidoc/querylanguage/Concepts.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,24 @@ It's not available for entities whose id generator is implemented in Java, nor f
427427
The same two options are available for a `@Version` attribute.
428428
When no version is explicitly specified, the version for a new entity instance is used.
429429

430+
The `on conflict` clause lets us specify what action should be taken when the database already contains the record we're attempting to insert.
431+
432+
[source, antlrv4]
433+
----
434+
include::{extrasdir}/on_conflict_bnf.txt[]
435+
----
436+
437+
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.
438+
439+
[[onconflict-example]]
440+
[source, hql]
441+
----
442+
insert Person (ssn, name, phone)
443+
values ('116-76-1234', 'Jane Doe', '404 888 4319')
444+
on conflict (ssn) set phone = excluded.phone
445+
----
446+
447+
430448
Like `update` and `delete` statements, an `insert` statement must be executed by calling `Query.executeUpdate()`.
431449

432450
Now it's time to look at something _much_ more complicated.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
conflictClause
2+
: ON CONFLICT conflictTarget? "DO" conflictAction
3+
4+
conflictTarget
5+
: ON CONSTRAINT identifier
6+
| "(" simplePath ("," simplePath)* ")"
7+
8+
conflictAction
9+
: "NOTHING"
10+
| "UPDATE" setClause whereClause?

documentation/src/main/asciidoc/querylanguage/extras/statement_insert_bnf.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
insertStatement
2-
: "INSERT" "INTO"? targetEntity targetFields (queryExpression | valuesList)
2+
: "INSERT" "INTO"? targetEntity targetFields
3+
(queryExpression | valuesList)
4+
conflictClause?
35

46
targetEntity
57
: entityName variable?

0 commit comments

Comments
 (0)