Skip to content

Commit

Permalink
feat: ON DUPLICATE KEY UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekStride committed Apr 4, 2024
1 parent b6b1f7f commit d03f501
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
43 changes: 31 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ module.exports = grammar({
keyword_start: _ => make_keyword("start"),
keyword_restart: _ => make_keyword("restart"),
keyword_key: _ => make_keyword("key"),
keyword_duplicate: _ => make_keyword("duplicate"),
keyword_as: _ => make_keyword("as"),
keyword_distinct: _ => make_keyword("distinct"),
keyword_constraint: _ => make_keyword("constraint"),
Expand Down Expand Up @@ -2150,24 +2151,42 @@ module.exports = grammar({
$._set_values,
),
optional(
seq(
$.keyword_on,
$.keyword_conflict,
choice(
$._on_conflict,
$._on_duplicate_key_update,
),
),
),

_on_conflict: $ => seq(
$.keyword_on,
$.keyword_conflict,
seq(
$.keyword_do,
choice(
$.keyword_nothing,
seq(
$.keyword_do,
choice(
$.keyword_nothing,
seq(
$.keyword_update,
$._set_values,
optional($.where),
),
),
$.keyword_update,
$._set_values,
optional($.where),
),
),
),
),

_on_duplicate_key_update: $ => seq(
$.keyword_on,
$.keyword_duplicate,
$.keyword_key,
$.keyword_update,
$.assignment_list,
),

assignment_list: $ => seq(
$.assignment,
repeat(seq(',', $.assignment)),
),

_insert_values: $ => seq(
optional(alias($._column_list, $.list)),
choice(
Expand Down
8 changes: 4 additions & 4 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
(invocation
(object_reference
name: (identifier) @function.call))

[
(keyword_gist)
(keyword_btree)
Expand All @@ -15,6 +11,10 @@
(object_reference
name: (identifier) @type)

(invocation
(object_reference
name: (identifier) @function.call))

(relation
alias: (identifier) @variable)

Expand Down
70 changes: 67 additions & 3 deletions test/corpus/insert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ RETURNING *;
(returning
(keyword_returning)
(select_expression
(term value: (all_fields))))))
(term
value: (all_fields))))))

================================================================================
Insert returning single column
Expand Down Expand Up @@ -449,7 +450,8 @@ FROM
(select
(keyword_select)
(select_expression
(term (all_fields))))
(term
(all_fields))))
(from
(keyword_from)
(relation
Expand Down Expand Up @@ -513,7 +515,8 @@ FROM
(select
(keyword_select)
(select_expression
(term (all_fields))))
(term
(all_fields))))
(from
(keyword_from)
(relation
Expand Down Expand Up @@ -561,3 +564,64 @@ INSERT INTO some_table
(select_expression
(term
(literal))))))))

================================================================================
ON DUPLICATE KEY UPDATE
================================================================================

INSERT INTO table_1 (pk, col1, col2)
VALUES
(1, 1, 1),
(2, 2, 2)
ON DUPLICATE KEY UPDATE
pk = VALUES(pk),
col1 = 1,
col2 = "foo";

--------------------------------------------------------------------------------

(program
(statement
(insert
(keyword_insert)
(keyword_into)
(object_reference
name: (identifier))
(list
(column
(identifier))
(column
(identifier))
(column
(identifier)))
(keyword_values)
(list
(literal)
(literal)
(literal))
(list
(literal)
(literal)
(literal))
(keyword_on)
(keyword_duplicate)
(keyword_key)
(keyword_update)
(assignment_list
(assignment
left: (field
name: (identifier))
right: (invocation
(object_reference
name: (identifier))
parameter: (term
value: (field
name: (identifier)))))
(assignment
left: (field
name: (identifier))
right: (literal))
(assignment
left: (field
name: (identifier))
right: (literal))))))

0 comments on commit d03f501

Please sign in to comment.