grammar: add foreign key column constraint

This commit is contained in:
Dmitriy Pleshevskiy 2023-01-07 23:20:00 +03:00
parent e4e1756164
commit b6be01e6a9
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 4425 additions and 2213 deletions

View file

@ -63,14 +63,45 @@ module.exports = grammar({
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct)
)
),
$.keyword_primary_key
$._primary_key,
seq(
$.keyword_references,
$.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match),
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
)
)
// TODO: CHECK
// TODO: GENERATED
// TODO: FOREIGN KEY
)
// TODO: DEFERRABLE
),
_foreign_key_match: ($) =>
seq(
$.keyword_match,
choice($.keyword_full, $.keyword_partial, $.keyword_simple)
),
_foreign_key_on_delete: ($) =>
seq($.keyword_on, $.keyword_delete, $.referencial_action),
_foreign_key_on_update: ($) =>
seq($.keyword_on, $.keyword_update, $.referencial_action),
referencial_action: ($) =>
choice(
seq($.keyword_no, $.keyword_action),
$.keyword_restrict,
$.keyword_cascade,
seq(
$.keyword_set,
choice($.keyword_null, $.keyword_default),
optional(seq("(", commaSepRepeat1($.identifier), ")"))
)
),
table_reference: ($) =>
seq(
optional(seq(field("schema", $.identifier), ".")),
@ -174,6 +205,8 @@ module.exports = grammar({
_type_json: ($) => choice($.keyword_json, $.keyword_jsonb),
// keywords
_primary_key: ($) => seq($.keyword_primary, $.keyword_key),
_foreign_key: ($) => seq($.keyword_foreign, $.keyword_key),
_if_not_exists: ($) => seq($.keyword_if, $.keyword_not, $.keyword_exists),
_not_null: ($) => seq($.keyword_not, $.keyword_null),
_without_time_zone: ($) => seq(mkKeyword("without"), $._keyword_time_zone),
@ -195,7 +228,22 @@ module.exports = grammar({
keyword_nulls: (_) => mkKeyword("nulls"),
keyword_distinct: (_) => mkKeyword("distinct"),
keyword_unique: (_) => mkKeyword("unique"),
keyword_primary_key: (_) => seq(mkKeyword("primary"), mkKeyword("key")),
keyword_primary: (_) => mkKeyword("primary"),
keyword_foreign: (_) => mkKeyword("foreign"),
keyword_key: (_) => mkKeyword("key"),
keyword_references: (_) => mkKeyword("references"),
keyword_on: (_) => mkKeyword("on"),
keyword_no: (_) => mkKeyword("no"),
keyword_delete: (_) => mkKeyword("delete"),
keyword_update: (_) => mkKeyword("update"),
keyword_match: (_) => mkKeyword("match"),
keyword_full: (_) => mkKeyword("full"),
keyword_partial: (_) => mkKeyword("partial"),
keyword_simple: (_) => mkKeyword("simple"),
keyword_action: (_) => mkKeyword("action"),
keyword_set: (_) => mkKeyword("set"),
keyword_restrict: (_) => mkKeyword("restrict"),
keyword_cascade: (_) => mkKeyword("cascade"),
// References: https://www.postgresql.org/docs/15/datatype-xml.html
keyword_xml: (_) => mkKeyword("xml"),
// References: https://www.postgresql.org/docs/15/datatype-uuid.html

View file

@ -287,7 +287,246 @@
},
{
"type": "SYMBOL",
"name": "keyword_primary_key"
"name": "_primary_key"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_references"
},
{
"type": "SYMBOL",
"name": "table_reference"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "refcolumn",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_match"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
}
]
}
]
}
]
}
]
}
]
},
"_foreign_key_match": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_match"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "keyword_full"
},
{
"type": "SYMBOL",
"name": "keyword_partial"
},
{
"type": "SYMBOL",
"name": "keyword_simple"
}
]
}
]
},
"_foreign_key_on_delete": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_on"
},
{
"type": "SYMBOL",
"name": "keyword_delete"
},
{
"type": "SYMBOL",
"name": "referencial_action"
}
]
},
"_foreign_key_on_update": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_on"
},
{
"type": "SYMBOL",
"name": "keyword_update"
},
{
"type": "SYMBOL",
"name": "referencial_action"
}
]
},
"referencial_action": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_no"
},
{
"type": "SYMBOL",
"name": "keyword_action"
}
]
},
{
"type": "SYMBOL",
"name": "keyword_restrict"
},
{
"type": "SYMBOL",
"name": "keyword_cascade"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_set"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "keyword_null"
},
{
"type": "SYMBOL",
"name": "keyword_default"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
@ -859,6 +1098,32 @@
}
]
},
"_primary_key": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_primary"
},
{
"type": "SYMBOL",
"name": "keyword_key"
}
]
},
"_foreign_key": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_foreign"
},
{
"type": "SYMBOL",
"name": "keyword_key"
}
]
},
"_if_not_exists": {
"type": "SEQ",
"members": [
@ -997,18 +1262,69 @@
"type": "PATTERN",
"value": "unique|UNIQUE"
},
"keyword_primary_key": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "primary|PRIMARY"
},
{
"type": "PATTERN",
"value": "key|KEY"
}
]
"keyword_primary": {
"type": "PATTERN",
"value": "primary|PRIMARY"
},
"keyword_foreign": {
"type": "PATTERN",
"value": "foreign|FOREIGN"
},
"keyword_key": {
"type": "PATTERN",
"value": "key|KEY"
},
"keyword_references": {
"type": "PATTERN",
"value": "references|REFERENCES"
},
"keyword_on": {
"type": "PATTERN",
"value": "on|ON"
},
"keyword_no": {
"type": "PATTERN",
"value": "no|NO"
},
"keyword_delete": {
"type": "PATTERN",
"value": "delete|DELETE"
},
"keyword_update": {
"type": "PATTERN",
"value": "update|UPDATE"
},
"keyword_match": {
"type": "PATTERN",
"value": "match|MATCH"
},
"keyword_full": {
"type": "PATTERN",
"value": "full|FULL"
},
"keyword_partial": {
"type": "PATTERN",
"value": "partial|PARTIAL"
},
"keyword_simple": {
"type": "PATTERN",
"value": "simple|SIMPLE"
},
"keyword_action": {
"type": "PATTERN",
"value": "action|ACTION"
},
"keyword_set": {
"type": "PATTERN",
"value": "set|SET"
},
"keyword_restrict": {
"type": "PATTERN",
"value": "restrict|RESTRICT"
},
"keyword_cascade": {
"type": "PATTERN",
"value": "cascade|CASCADE"
},
"keyword_xml": {
"type": "PATTERN",

View file

@ -43,6 +43,16 @@
"named": true
}
]
},
"refcolumn": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
@ -57,10 +67,26 @@
"type": "keyword_default",
"named": true
},
{
"type": "keyword_delete",
"named": true
},
{
"type": "keyword_distinct",
"named": true
},
{
"type": "keyword_full",
"named": true
},
{
"type": "keyword_key",
"named": true
},
{
"type": "keyword_match",
"named": true
},
{
"type": "keyword_not",
"named": true
@ -74,16 +100,44 @@
"named": true
},
{
"type": "keyword_primary_key",
"type": "keyword_on",
"named": true
},
{
"type": "keyword_partial",
"named": true
},
{
"type": "keyword_primary",
"named": true
},
{
"type": "keyword_references",
"named": true
},
{
"type": "keyword_simple",
"named": true
},
{
"type": "keyword_unique",
"named": true
},
{
"type": "keyword_update",
"named": true
},
{
"type": "literal",
"named": true
},
{
"type": "referencial_action",
"named": true
},
{
"type": "table_reference",
"named": true
}
]
}
@ -325,11 +379,6 @@
"named": true,
"fields": {}
},
{
"type": "keyword_primary_key",
"named": true,
"fields": {}
},
{
"type": "keyword_temporary",
"named": true,
@ -419,6 +468,49 @@
]
}
},
{
"type": "referencial_action",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "keyword_action",
"named": true
},
{
"type": "keyword_cascade",
"named": true
},
{
"type": "keyword_default",
"named": true
},
{
"type": "keyword_no",
"named": true
},
{
"type": "keyword_null",
"named": true
},
{
"type": "keyword_restrict",
"named": true
},
{
"type": "keyword_set",
"named": true
}
]
}
},
{
"type": "source_file",
"named": true,
@ -541,6 +633,10 @@
"type": ";",
"named": false
},
{
"type": "keyword_action",
"named": true
},
{
"type": "keyword_bigint",
"named": true
@ -557,6 +653,10 @@
"type": "keyword_bytea",
"named": true
},
{
"type": "keyword_cascade",
"named": true
},
{
"type": "keyword_constraint",
"named": true
@ -581,6 +681,10 @@
"type": "keyword_default",
"named": true
},
{
"type": "keyword_delete",
"named": true
},
{
"type": "keyword_distinct",
"named": true
@ -593,6 +697,14 @@
"type": "keyword_false",
"named": true
},
{
"type": "keyword_foreign",
"named": true
},
{
"type": "keyword_full",
"named": true
},
{
"type": "keyword_if",
"named": true
@ -609,10 +721,22 @@
"type": "keyword_jsonb",
"named": true
},
{
"type": "keyword_key",
"named": true
},
{
"type": "keyword_match",
"named": true
},
{
"type": "keyword_money",
"named": true
},
{
"type": "keyword_no",
"named": true
},
{
"type": "keyword_not",
"named": true
@ -629,14 +753,42 @@
"type": "keyword_numeric",
"named": true
},
{
"type": "keyword_on",
"named": true
},
{
"type": "keyword_partial",
"named": true
},
{
"type": "keyword_primary",
"named": true
},
{
"type": "keyword_real",
"named": true
},
{
"type": "keyword_references",
"named": true
},
{
"type": "keyword_restrict",
"named": true
},
{
"type": "keyword_serial",
"named": true
},
{
"type": "keyword_set",
"named": true
},
{
"type": "keyword_simple",
"named": true
},
{
"type": "keyword_smallint",
"named": true
@ -665,6 +817,10 @@
"type": "keyword_unlogged",
"named": true
},
{
"type": "keyword_update",
"named": true
},
{
"type": "keyword_uuid",
"named": true

File diff suppressed because it is too large Load diff

View file

@ -192,7 +192,8 @@ create table foo (
name: (identifier)
datatype: (keyword_uuid)
(column_constraint
(keyword_primary_key)
(keyword_primary)
(keyword_key)
)
)
(column_definition
@ -227,6 +228,62 @@ create table foo (
)
)
================================================================================
Create a table with primary key and foreign key
================================================================================
create table foo (
id uuid primary key,
bar_id uuid null references bar (id) on update set null on delete cascade
);
--------------------------------------------------------------------------------
(source_file
(statement
(create_table
(keyword_create)
(keyword_table)
(table_reference
name: (identifier)
)
(column_definitions
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(column_constraint
(keyword_primary)
(keyword_key)
)
)
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(column_constraint
(keyword_null)
)
(column_constraint
(keyword_references)
(table_reference
name: (identifier)
)
refcolumn: (identifier)
(keyword_on)
(keyword_update)
(referencial_action
(keyword_set)
(keyword_null)
)
(keyword_on)
(keyword_delete)
(referencial_action
(keyword_cascade)
)
)
)
)
)
)
)
================================================================================
Create a table with named column constraints
================================================================================
@ -254,7 +311,8 @@ create table foo (
(column_constraint
(keyword_constraint)
name: (identifier)
(keyword_primary_key)
(keyword_primary)
(keyword_key)
)
)
(column_definition

Binary file not shown.