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,13 +53,40 @@ 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
$._unique_constraint,
$._primary_key,
$._foreign_key_references
// TODO: CHECK
// TODO: GENERATED
)
// 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( seq(
$.keyword_unique, $.keyword_unique,
optional( optional(
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct) seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct)
) )
), ),
$._primary_key,
_foreign_key_references: ($) =>
seq( seq(
$.keyword_references, $.keyword_references,
$.table_reference, $.table_reference,
@ -75,13 +98,7 @@ module.exports = grammar({
seq($._foreign_key_on_update, $._foreign_key_on_delete) seq($._foreign_key_on_update, $._foreign_key_on_delete)
) )
) )
)
// TODO: CHECK
// TODO: GENERATED
)
// TODO: DEFERRABLE
), ),
_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",
"name": "datatype",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_type" "name": "type"
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -244,6 +248,146 @@
] ]
}, },
{ {
"type": "SYMBOL",
"name": "_unique_constraint"
},
{
"type": "SYMBOL",
"name": "_primary_key"
},
{
"type": "SYMBOL",
"name": "_foreign_key_references"
}
]
}
]
},
"table_constraint": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"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",
"name": "column_list"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_primary_key"
},
{
"type": "SYMBOL",
"name": "column_list"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key"
},
{
"type": "SYMBOL",
"name": "column_list"
},
{
"type": "SYMBOL",
"name": "_foreign_key_references"
}
]
}
]
}
]
},
"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", "type": "SEQ",
"members": [ "members": [
{ {
@ -285,11 +429,7 @@
} }
] ]
}, },
{ "_foreign_key_references": {
"type": "SYMBOL",
"name": "_primary_key"
},
{
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -381,10 +521,6 @@
] ]
} }
] ]
}
]
}
]
}, },
"_foreign_key_match": { "_foreign_key_match": {
"type": "SEQ", "type": "SEQ",
@ -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.