diff --git a/grammar.js b/grammar.js index 6f58f3ce..3d0953b8 100644 --- a/grammar.js +++ b/grammar.js @@ -235,6 +235,7 @@ module.exports = grammar({ keyword_nowait: _ => make_keyword("nowait"), keyword_attribute: _ => make_keyword("attribute"), keyword_authorization: _ => make_keyword("authorization"), + keyword_action: _ => make_keyword("action"), keyword_trigger: _ => make_keyword('trigger'), keyword_function: _ => make_keyword("function"), @@ -2274,13 +2275,31 @@ module.exports = grammar({ seq( $.keyword_on, $.keyword_delete, - $.keyword_cascade, + $.referential_action, + ), + ), + optional( + seq( + $.keyword_on, + $.keyword_update, + $.referential_action, ), ), ), ), ), + referential_action: $ => choice( + seq($.keyword_no, $.keyword_action), + $.keyword_restrict, + $.keyword_cascade, + seq( + $.keyword_set, + choice($.keyword_null, $.keyword_default), + optional(paren_list($.identifier)) + ), + ), + ordered_columns: $ => paren_list(alias($.ordered_column, $.column), true), ordered_column: $ => seq( diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt index d728ccf7..b8c9fc78 100644 --- a/test/corpus/alter.txt +++ b/test/corpus/alter.txt @@ -533,7 +533,6 @@ ALTER TABLE "Role" ADD CONSTRAINT "pkRole" PRIMARY KEY ("roleId"); (column name: (literal)))))))) - ================================================================================ Add foreign key constraint ================================================================================ @@ -568,7 +567,8 @@ FOREIGN KEY ("accountId") REFERENCES "Account" ("accountId") ON DELETE CASCADE; name: (literal))) (keyword_on) (keyword_delete) - (keyword_cascade)))))) + (referential_action + (keyword_cascade))))))) ================================================================================ MySQL: Rename table @@ -1131,3 +1131,48 @@ OWNED BY numbers.number_sequences (object_reference (identifier) (identifier))))) + +================================================================================ +Add foreign key constraint +================================================================================ + +ALTER TABLE "AccountRole" ADD CONSTRAINT "fkAccountRoleAccount" +FOREIGN KEY ("accountId") REFERENCES "Account" ("accountId") +ON DELETE SET NULL +ON UPDATE SET DEFAULT; + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_table + (keyword_alter) + (keyword_table) + (object_reference + (identifier)) + (add_constraint + (keyword_add) + (keyword_constraint) + (identifier) + (constraint + (keyword_foreign) + (keyword_key) + (ordered_columns + (column + (literal))) + (keyword_references) + (object_reference + (identifier)) + (ordered_columns + (column + (literal))) + (keyword_on) + (keyword_delete) + (referential_action + (keyword_set) + (keyword_null)) + (keyword_on) + (keyword_update) + (referential_action + (keyword_set) + (keyword_default)))))))