Skip to content

Commit

Permalink
feat: Add create trigger statement for postgres.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineB committed Oct 25, 2023
1 parent 44e3603 commit e0922d7
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
76 changes: 76 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,19 @@ module.exports = grammar({
keyword_constraints : _ => make_keyword("constraints"),
keyword_snapshot: _ => make_keyword("snapshot"),
keyword_characteristics: _ => make_keyword("characteristics"),
keyword_follows: _ => make_keyword("follows"),
keyword_precedes: _ => make_keyword("precedes"),
keyword_definer: _ => make_keyword("definer"),
keyword_each: _ => make_keyword("each"),
keyword_instead: _ => make_keyword("instead"),
keyword_of: _ => make_keyword("of"),
keyword_initially: _ => make_keyword("initially"),
keyword_old: _ => make_keyword("old"),
keyword_new: _ => make_keyword("new"),
keyword_referencing: _ => make_keyword("referencing"),
keyword_statement: _ => make_keyword("statement"),
keyword_execute: _ => make_keyword("execute"),
keyword_procedure: _ => make_keyword("procedure"),

// Hive Keywords
keyword_external: _ => make_keyword("external"),
Expand Down Expand Up @@ -896,6 +909,7 @@ module.exports = grammar({
$.create_role,
$.create_sequence,
$.create_extension,
$.create_trigger,
prec.left(seq(
$.create_schema,
repeat($._create_statement),
Expand Down Expand Up @@ -1419,6 +1433,68 @@ module.exports = grammar({
optional($.keyword_cascade),
),

create_trigger: $ => seq(
$.keyword_create,
optional($._or_replace),
// mariadb
optional(seq($.keyword_definer, '=', $.identifier)),
optional($.keyword_constraint),
// sqlite
optional($._temporary),
$.keyword_trigger,
// sqlite/mariadb
optional($._if_not_exists),
$.object_reference,
choice(
$.keyword_before,
$.keyword_after,
seq($.keyword_instead, $.keyword_of),
),
$._create_trigger_event,
repeat(seq($.keyword_or, $._create_trigger_event)),
$.keyword_on,
$.object_reference,
repeat(
choice(
seq($.keyword_from, $.object_reference),
choice(
seq($.keyword_not, $.keyword_deferrable),
$.keyword_deferrable,
seq($.keyword_initially, $.keyword_immediate),
seq($.keyword_initially, $.keyword_deferred),
),
seq($.keyword_referencing, choice($.keyword_old, $.keyword_new), $.keyword_table, optional($.keyword_as), $.identifier),
seq(
$.keyword_for,
optional($.keyword_each),
choice($.keyword_row, $.keyword_statement),
// mariadb
optional(seq(choice($.keyword_follows, $.keyword_precedes), $.identifier)),
),
seq($.keyword_when, wrapped_in_parenthesis($._expression)),
),
),
$.keyword_execute,
choice($.keyword_function, $.keyword_procedure),
$.object_reference,
paren_list(field('parameter', $.term)),
),

_create_trigger_event: $ => choice(
$.keyword_insert,
seq(
$.keyword_update,
optional(
seq(
$.keyword_of,
comma_list($.identifier, true),
),
),
),
$.keyword_delete,
$.keyword_truncate,
),

create_type: $ => seq(
$.keyword_create,
$.keyword_type,
Expand Down
13 changes: 13 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@
(keyword_snapshot)
(keyword_characteristics)
(keyword_off)
(keyword_follows)
(keyword_precedes)
(keyword_definer)
(keyword_each)
(keyword_instead)
(keyword_of)
(keyword_initially)
(keyword_old)
(keyword_new)
(keyword_referencing)
(keyword_statement)
(keyword_execute)
(keyword_procedure)
] @keyword

[
Expand Down
59 changes: 59 additions & 0 deletions test/corpus/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2045,3 +2045,62 @@ create view toto as select '123'::timestamp with check option;
(keyword_with)
(keyword_check)
(keyword_option))))

================================================================================
Create a trigger on delete for each row
================================================================================

CREATE TRIGGER update_at BEFORE DELETE ON public.table_A FOR EACH ROW EXECUTE FUNCTION public.update_timestamp();

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

(program
(statement
(create_trigger
(keyword_create)
(keyword_trigger)
(object_reference
(identifier))
(keyword_before)
(keyword_delete)
(keyword_on)
(object_reference
(identifier)
(identifier))
(keyword_for)
(keyword_each)
(keyword_row)
(keyword_execute)
(keyword_function)
(object_reference
(identifier)
(identifier)))))

================================================================================
Create a trigger on update
================================================================================

CREATE TRIGGER update_at_user AFTER UPDATE OF name ON public."user" EXECUTE FUNCTION public.update_timestamp();

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

(program
(statement
(create_trigger
(keyword_create)
(keyword_trigger)
(object_reference
(identifier))
(keyword_after)
(keyword_update)
(keyword_of)
(identifier)
(keyword_on)
(object_reference
(identifier)
(identifier))
(keyword_execute)
(keyword_function)
(object_reference
(identifier)
(identifier)))))

0 comments on commit e0922d7

Please sign in to comment.