grammar: add table constraint

This commit is contained in:
Dmitriy Pleshevskiy 2023-01-08 16:17:16 +03:00
parent acda125c15
commit de04a49b20
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 2432 additions and 2024 deletions

View file

@ -37,17 +37,13 @@ module.exports = grammar({
seq( seq(
"(", "(",
optional( optional(
commaSepRepeat1(choice($.column_definition /*, $.table_constraint*/)) commaSepRepeat1(choice($.column_definition, $.table_constraint))
), ),
")" ")"
), ),
column_definition: ($) => column_definition: ($) =>
seq( seq(field("name", $.identifier), $.type, repeat($.column_constraint)),
field("name", $.identifier),
field("datatype", $._type),
repeat($.column_constraint)
),
column_constraint: ($) => column_constraint: ($) =>
seq( seq(
@ -57,31 +53,52 @@ module.exports = grammar({
$.keyword_null, $.keyword_null,
seq($.keyword_default, $._expression), seq($.keyword_default, $._expression),
// TODO: add index_parameters in UNIQUE, PRIMARY KEY // TODO: add index_parameters in UNIQUE, PRIMARY KEY
seq( $._unique_constraint,
$.keyword_unique,
optional(
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct)
)
),
$._primary_key, $._primary_key,
seq( $._foreign_key_references
$.keyword_references,
$.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match),
optional(
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
)
)
)
// TODO: CHECK // TODO: CHECK
// TODO: GENERATED // TODO: GENERATED
) )
// TODO: DEFERRABLE // TODO: DEFERRABLE
), ),
table_constraint: ($) =>
seq(
optional(seq($.keyword_constraint, field("name", $.identifier))),
choice(
seq($._unique_constraint, $.column_list),
seq($._primary_key, $.column_list),
seq($._foreign_key, $.column_list, $._foreign_key_references)
// TODO: CHECK
// TODO: EXCLUDE
)
// TODO: DEFERRABLE
),
column_list: ($) =>
seq("(", commaSepRepeat1(field("name", $.identifier)), ")"),
_unique_constraint: ($) =>
seq(
$.keyword_unique,
optional(
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct)
)
),
_foreign_key_references: ($) =>
seq(
$.keyword_references,
$.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match),
optional(
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
)
)
),
_foreign_key_match: ($) => _foreign_key_match: ($) =>
seq( seq(
$.keyword_match, $.keyword_match,
@ -126,8 +143,9 @@ module.exports = grammar({
), ),
// References: https://www.postgresql.org/docs/15/datatype.html // References: https://www.postgresql.org/docs/15/datatype.html
_type: ($) => type: ($) =>
choice( choice(
field("name", $.identifier),
$._type_numeric, $._type_numeric,
$._type_character, $._type_character,
$._type_datetime, $._type_datetime,

View file

@ -126,6 +126,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "column_definition" "name": "column_definition"
},
{
"type": "SYMBOL",
"name": "table_constraint"
} }
] ]
}, },
@ -144,6 +148,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "column_definition" "name": "column_definition"
},
{
"type": "SYMBOL",
"name": "table_constraint"
} }
] ]
} }
@ -175,12 +183,8 @@
} }
}, },
{ {
"type": "FIELD", "type": "SYMBOL",
"name": "datatype", "name": "type"
"content": {
"type": "SYMBOL",
"name": "_type"
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -244,97 +248,168 @@
] ]
}, },
{ {
"type": "SEQ", "type": "SYMBOL",
"members": [ "name": "_unique_constraint"
{
"type": "SYMBOL",
"name": "keyword_unique"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_nulls"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "keyword_not"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "keyword_distinct"
}
]
},
{
"type": "BLANK"
}
]
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_primary_key" "name": "_primary_key"
}, },
{
"type": "SYMBOL",
"name": "_foreign_key_references"
}
]
}
]
},
"table_constraint": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "keyword_references" "name": "keyword_constraint"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_unique_constraint"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "table_reference" "name": "column_list"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_primary_key"
}, },
{ {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "column_list"
{ }
"type": "SEQ", ]
"members": [ },
{ {
"type": "STRING", "type": "SEQ",
"value": "(" "members": [
}, {
{ "type": "SYMBOL",
"type": "FIELD", "name": "_foreign_key"
"name": "refcolumn", },
"content": { {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "column_list"
} },
}, {
{ "type": "SYMBOL",
"type": "STRING", "name": "_foreign_key_references"
"value": ")" }
} ]
] }
}, ]
{ }
"type": "BLANK" ]
},
"column_list": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
} }
] }
]
}
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"_unique_constraint": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_unique"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_nulls"
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_foreign_key_match" "name": "keyword_not"
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -342,45 +417,106 @@
] ]
}, },
{ {
"type": "CHOICE", "type": "SYMBOL",
"name": "keyword_distinct"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"_foreign_key_references": {
"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": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "_foreign_key_on_delete"
{
"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"
}
]
}
]
}, },
{ {
"type": "BLANK" "type": "SYMBOL",
"name": "_foreign_key_on_update"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
} }
] ]
} }
] ]
},
{
"type": "BLANK"
} }
] ]
} }
@ -612,9 +748,17 @@
} }
] ]
}, },
"_type": { "type": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_type_numeric" "name": "_type_numeric"

View file

@ -146,104 +146,6 @@
"type": "column_definition", "type": "column_definition",
"named": true, "named": true,
"fields": { "fields": {
"datatype": {
"multiple": false,
"required": true,
"types": [
{
"type": "char",
"named": true
},
{
"type": "decimal",
"named": true
},
{
"type": "double",
"named": true
},
{
"type": "keyword_bigint",
"named": true
},
{
"type": "keyword_bigserial",
"named": true
},
{
"type": "keyword_boolean",
"named": true
},
{
"type": "keyword_bytea",
"named": true
},
{
"type": "keyword_date",
"named": true
},
{
"type": "keyword_datetime",
"named": true
},
{
"type": "keyword_integer",
"named": true
},
{
"type": "keyword_money",
"named": true
},
{
"type": "keyword_real",
"named": true
},
{
"type": "keyword_serial",
"named": true
},
{
"type": "keyword_smallint",
"named": true
},
{
"type": "keyword_smallserial",
"named": true
},
{
"type": "keyword_text",
"named": true
},
{
"type": "keyword_time",
"named": true
},
{
"type": "keyword_timestamp",
"named": true
},
{
"type": "keyword_timestamptz",
"named": true
},
{
"type": "keyword_uuid",
"named": true
},
{
"type": "keyword_xml",
"named": true
},
{
"type": "numeric",
"named": true
},
{
"type": "varchar",
"named": true
}
]
},
"name": { "name": {
"multiple": false, "multiple": false,
"required": true, "required": true,
@ -257,11 +159,15 @@
}, },
"children": { "children": {
"multiple": true, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "column_constraint", "type": "column_constraint",
"named": true "named": true
},
{
"type": "type",
"named": true
} }
] ]
} }
@ -277,10 +183,30 @@
{ {
"type": "column_definition", "type": "column_definition",
"named": true "named": true
},
{
"type": "table_constraint",
"named": true
} }
] ]
} }
}, },
{
"type": "column_list",
"named": true,
"fields": {
"name": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{ {
"type": "create_table", "type": "create_table",
"named": true, "named": true,
@ -554,6 +480,114 @@
] ]
} }
}, },
{
"type": "table_constraint",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"refcolumn": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "column_list",
"named": true
},
{
"type": "keyword_constraint",
"named": true
},
{
"type": "keyword_delete",
"named": true
},
{
"type": "keyword_distinct",
"named": true
},
{
"type": "keyword_foreign",
"named": true
},
{
"type": "keyword_full",
"named": true
},
{
"type": "keyword_key",
"named": true
},
{
"type": "keyword_match",
"named": true
},
{
"type": "keyword_not",
"named": true
},
{
"type": "keyword_nulls",
"named": true
},
{
"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": "referencial_action",
"named": true
},
{
"type": "table_reference",
"named": true
}
]
}
},
{ {
"type": "table_reference", "type": "table_reference",
"named": true, "named": true,
@ -580,6 +614,120 @@
} }
} }
}, },
{
"type": "type",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "char",
"named": true
},
{
"type": "decimal",
"named": true
},
{
"type": "double",
"named": true
},
{
"type": "keyword_bigint",
"named": true
},
{
"type": "keyword_bigserial",
"named": true
},
{
"type": "keyword_boolean",
"named": true
},
{
"type": "keyword_bytea",
"named": true
},
{
"type": "keyword_date",
"named": true
},
{
"type": "keyword_datetime",
"named": true
},
{
"type": "keyword_integer",
"named": true
},
{
"type": "keyword_money",
"named": true
},
{
"type": "keyword_real",
"named": true
},
{
"type": "keyword_serial",
"named": true
},
{
"type": "keyword_smallint",
"named": true
},
{
"type": "keyword_smallserial",
"named": true
},
{
"type": "keyword_text",
"named": true
},
{
"type": "keyword_time",
"named": true
},
{
"type": "keyword_timestamp",
"named": true
},
{
"type": "keyword_timestamptz",
"named": true
},
{
"type": "keyword_uuid",
"named": true
},
{
"type": "keyword_xml",
"named": true
},
{
"type": "numeric",
"named": true
},
{
"type": "varchar",
"named": true
}
]
}
},
{ {
"type": "varchar", "type": "varchar",
"named": true, "named": true,

File diff suppressed because it is too large Load diff

View file

@ -135,33 +135,33 @@ create table foo (
(column_definitions (column_definitions
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_boolean) (type (keyword_boolean))
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (numeric (type (numeric
(keyword_numeric) (keyword_numeric)
precision: (number) precision: (number)
) ))
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_timestamptz) (type (keyword_timestamptz))
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (varchar (type (varchar
(keyword_varchar) (keyword_varchar)
size: (number) size: (number)
) ))
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_uuid) (type (keyword_uuid))
) )
) )
) )
@ -190,7 +190,7 @@ create table foo (
(column_definitions (column_definitions
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_uuid) (type (keyword_uuid))
(column_constraint (column_constraint
(keyword_primary) (keyword_primary)
(keyword_key) (keyword_key)
@ -198,7 +198,7 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_not) (keyword_not)
(keyword_null) (keyword_null)
@ -206,14 +206,14 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_null) (keyword_null)
) )
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_not) (keyword_not)
(keyword_null) (keyword_null)
@ -250,7 +250,7 @@ create table foo (
(column_definitions (column_definitions
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_uuid) (type (keyword_uuid))
(column_constraint (column_constraint
(keyword_primary) (keyword_primary)
(keyword_key) (keyword_key)
@ -258,7 +258,7 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_uuid) (type (keyword_uuid))
(column_constraint (column_constraint
(keyword_null) (keyword_null)
) )
@ -309,7 +309,7 @@ create table foo (
(column_definitions (column_definitions
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_uuid) (type (keyword_uuid))
(column_constraint (column_constraint
(keyword_constraint) (keyword_constraint)
name: (identifier) name: (identifier)
@ -319,7 +319,7 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_constraint) (keyword_constraint)
name: (identifier) name: (identifier)
@ -329,7 +329,7 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_constraint) (keyword_constraint)
name: (identifier) name: (identifier)
@ -338,7 +338,7 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_constraint) (keyword_constraint)
name: (identifier) name: (identifier)
@ -356,7 +356,7 @@ create table foo (
) )
(column_definition (column_definition
name: (identifier) name: (identifier)
datatype: (keyword_text) (type (keyword_text))
(column_constraint (column_constraint
(keyword_constraint) (keyword_constraint)
name: (identifier) name: (identifier)

Binary file not shown.