From ce4cadc9692e8bd5a178fb0b83f61742bda0572e Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Tue, 1 Aug 2023 00:11:15 +0200 Subject: [PATCH 1/6] feat: add CAD database, Alter index, CAD USER/ROLE/GROUP --- grammar.js | 174 +++++++++++++++++++++++++++++++++++++++-- test/corpus/alter.txt | 56 +++++++++++++ test/corpus/create.txt | 75 ++++++++++++++++++ test/corpus/drop.txt | 46 +++++++++++ 4 files changed, 346 insertions(+), 5 deletions(-) diff --git a/grammar.js b/grammar.js index 47975d37..9ab6293d 100644 --- a/grammar.js +++ b/grammar.js @@ -145,8 +145,18 @@ module.exports = grammar({ keyword_type: _ => make_keyword("type"), keyword_rename: _ => make_keyword("rename"), keyword_to: _ => make_keyword("to"), + keyword_database: _ => make_keyword("database"), keyword_schema: _ => make_keyword("schema"), keyword_owner: _ => make_keyword("owner"), + keyword_user: _ => make_keyword("user"), + keyword_admin: _ => make_keyword("admin"), + keyword_password: _ => make_keyword("password"), + keyword_encrypted: _ => make_keyword("encrypted"), + keyword_valid: _ => make_keyword("valid"), + keyword_until: _ => make_keyword("until"), + keyword_connection: _ => make_keyword("connection"), + keyword_role: _ => make_keyword("role"), + keyword_reset: _ => make_keyword("reset"), keyword_temp: _ => make_keyword("temp"), keyword_temporary: _ => make_keyword("temporary"), keyword_unlogged: _ => make_keyword("unlogged"), @@ -738,6 +748,8 @@ module.exports = grammar({ $.create_index, $.create_function, $.create_type, + $.create_database, + $.create_role, prec.left(seq( $.create_schema, repeat($._create_statement), @@ -1094,6 +1106,74 @@ module.exports = grammar({ ), )), + _with_settings: $ => seq( + field('name', $.identifier), + optional('='), + field('value', choice($.identifier, alias($._single_quote_string, $.literal))), + ), + + create_database: $ => seq( + $.keyword_create, + $.keyword_database, + $.identifier, + optional($.keyword_with), + repeat( + $._with_settings + ), + ), + + create_role: $ => seq( + $.keyword_create, + choice( + $.keyword_user, + $.keyword_role, + $.keyword_group, + ), + $.identifier, + optional($.keyword_with), + repeat( + choice( + $._user_access_role_config, + $._role_options, + ), + ), + ), + + _role_options: $ => choice( + field("option", $.identifier), + seq( + $.keyword_valid, + $.keyword_until, + field("valid_until", alias($._literal_string, $.literal)) + ), + seq( + $.keyword_connection, + $.keyword_limit, + field("connection_limit", alias($._integer, $.literal)) + ), + seq( + optional($.keyword_encrypted), + $.keyword_password, + choice( + field("password", alias($._literal_string, $.literal)), + $.keyword_null, + ), + ), + ), + + + + _user_access_role_config: $ => prec.left(seq( + choice( + seq(optional($.keyword_in), $.keyword_role), + seq($.keyword_in, $.keyword_group), + $.keyword_admin, + $.keyword_user, + ), + comma_list($.identifier, true), + ), + ), + create_type: $ => seq( $.keyword_create, $.keyword_type, @@ -1119,11 +1199,7 @@ module.exports = grammar({ ) ), paren_list( - seq( - field('name', $.identifier), - '=', - field('value', choice($.identifier,alias($._single_quote_string, $.literal))), - ), + $._with_settings ), ), ), @@ -1141,6 +1217,9 @@ module.exports = grammar({ $.alter_view, $.alter_schema, $.alter_type, + $.alter_index, + $.alter_database, + $.alter_role, ), ), @@ -1339,6 +1418,69 @@ module.exports = grammar({ $.identifier, ), + // TODO more variants + alter_database: $ => seq( + $.keyword_alter, + $.keyword_database, + $.identifier, + optional($.keyword_with), + choice( + seq($.rename_object), + seq($.change_ownership), + ), + ), + + alter_role: $ => seq( + $.keyword_alter, + choice( + $.keyword_role, + $.keyword_group, + $.keyword_user, + ), + choice($.identifier, $.keyword_all), + choice( + $.rename_object, + seq(optional($.keyword_with),repeat($._role_options)), + seq( + optional(seq($.keyword_in, $.keyword_database, $.identifier)), + choice( + seq( + $.keyword_set, + field("option", $.identifier), + choice( + seq($.keyword_from, $.keyword_current), + seq( + choice($.keyword_to, "="), + choice( + field("parameter", $.identifier), + $.literal, + $.keyword_default + ) + ), + ), + ), + seq( + $.keyword_reset, + choice( + $.keyword_all, + field("option", $.identifier), + )), + ), + ) + ), + ), + + // TODO more variants + alter_index: $ => seq( + $.keyword_alter, + $.keyword_index, + optional($._if_exists), + $.identifier, + choice( + $.rename_object + ), + ), + alter_type: $ => seq( $.keyword_alter, $.keyword_type, @@ -1413,6 +1555,8 @@ module.exports = grammar({ $.drop_index, $.drop_type, $.drop_schema, + $.drop_database, + $.drop_role, ), ), @@ -1440,6 +1584,26 @@ module.exports = grammar({ optional($._drop_behavior) ), + drop_database: $ => seq( + $.keyword_drop, + $.keyword_database, + optional($._if_exists), + $.identifier, + optional($.keyword_with), + optional($.keyword_force), + ), + + drop_role: $ => seq( + $.keyword_drop, + choice( + $.keyword_group, + $.keyword_role, + $.keyword_user, + ), + optional($._if_exists), + $.identifier, + ), + drop_type: $ => seq( $.keyword_drop, $.keyword_type, diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt index 467f0d84..4d5ea9e8 100644 --- a/test/corpus/alter.txt +++ b/test/corpus/alter.txt @@ -697,3 +697,59 @@ ALTER SCHEMA sales OWNER TO CURRENT_USER; (keyword_owner) (keyword_to) (identifier)))) + +================================================================================ +ALTER INDEX RENAME +================================================================================ + +ALTER INDEX IF EXISTS myindex RENAME TO my_index + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_index + (keyword_alter) + (keyword_index) + (keyword_if) + (keyword_exists) + (identifier) + (rename_object + (keyword_rename) + (keyword_to) + (object_reference + (identifier)))))) + +================================================================================ +ALTER DATABASE RENAME +================================================================================ + +ALTER DATABASE hollywood RENAME TO bollywood; + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (rename_object + (keyword_rename) + (keyword_to) + (object_reference + (identifier)))))) + +================================================================================ +ALTER ROLE +================================================================================ + +ALTER ROLE rapunzel RENAME TO snow_white; +ALTER ROLE rapunzel SUPERUSER NOLOGIN CONNECTION LIMIT 69; +ALTER ROLE chris VALID UNTIL 'May 4 12:00:00 2015 +1'; +ALTER ROLE fred VALID UNTIL 'infinity'; +ALTER ROLE miriam CREATEROLE CREATEDB; +ALTER ROLE worker_bee SET maintenance_work_mem = 100000; +ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; + +-------------------------------------------------------------------------------- diff --git a/test/corpus/create.txt b/test/corpus/create.txt index fd042e6c..a21a87c8 100644 --- a/test/corpus/create.txt +++ b/test/corpus/create.txt @@ -1722,3 +1722,78 @@ CREATE SCHEMA hollywood (keyword_not)) (literal (keyword_null))))))))) + +================================================================================ +CREATE DATABASE +================================================================================ + +CREATE DATABASE hollywood + +-------------------------------------------------------------------------------- + +(program + (statement + (create_database + (keyword_create) + (keyword_database) + (identifier)))) + +================================================================================ +CREATE DATABASE with settings +================================================================================ + +CREATE DATABASE sales OWNER operations_dept; + +-------------------------------------------------------------------------------- + +(program + (statement + (create_database + (keyword_create) + (keyword_database) + (identifier) + (identifier) + (identifier)))) + +================================================================================ +CREATE USER +================================================================================ + +CREATE ROLE rapunzel +WITH ROLE hansel, gretel +IN GROUP fairy, tale +ADMIN grandma +PASSWORD 'secret' +VALID UNTIL '2022-01-01' +CONNECTION LIMIT 42 +NOLOGIN +INHERIT; + +-------------------------------------------------------------------------------- + +(program + (statement + (create_role + (keyword_create) + (keyword_role) + (identifier) + (keyword_with) + (keyword_role) + (identifier) + (identifier) + (keyword_in) + (keyword_group) + (identifier) + (identifier) + (keyword_admin) + (identifier) + (keyword_password) + (literal) + (keyword_valid) + (keyword_until) + (literal) + (keyword_connection) + (keyword_limit) + (literal) + (identifier) + (identifier)))) diff --git a/test/corpus/drop.txt b/test/corpus/drop.txt index 24b8507f..6895242f 100644 --- a/test/corpus/drop.txt +++ b/test/corpus/drop.txt @@ -141,3 +141,49 @@ DROP SCHEMA IF EXISTS myschema CASCADE; (keyword_exists) (identifier) (keyword_cascade)))) + +================================================================================ +Drop Database +================================================================================ + +DROP DATABASE IF EXISTS hollywood WITH FORCE; + +-------------------------------------------------------------------------------- + +(program + (statement + (drop_database + (keyword_drop) + (keyword_database) + (keyword_if) + (keyword_exists) + (identifier) + (keyword_with) + (keyword_force)))) + +================================================================================ +Drop Role +================================================================================ + +DROP ROLE hansel; +DROP GROUP fairy; +DROP USER rapunzel; + +-------------------------------------------------------------------------------- + +(program + (statement + (drop_role + (keyword_drop) + (keyword_role) + (identifier))) + (statement + (drop_role + (keyword_drop) + (keyword_group) + (identifier))) + (statement + (drop_role + (keyword_drop) + (keyword_user) + (identifier)))) From 56e6d6bd54c5baafcb3ce7eaeaa83234c57bac19 Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Tue, 1 Aug 2023 21:59:13 +0200 Subject: [PATCH 2/6] feat: add CAD sequence, variant for alter index/database --- grammar.js | 148 +++++++++++++++++++++++--- queries/highlights.scm | 19 ++++ test/corpus/alter.txt | 233 +++++++++++++++++++++++++++++++++++++++++ test/corpus/create.txt | 60 +++++++++++ test/corpus/drop.txt | 19 ++++ 5 files changed, 463 insertions(+), 16 deletions(-) diff --git a/grammar.js b/grammar.js index 9ab6293d..0aeebd36 100644 --- a/grammar.js +++ b/grammar.js @@ -99,9 +99,16 @@ module.exports = grammar({ keyword_table: _ => make_keyword("table"), keyword_tables: _ => make_keyword("tables"), keyword_view: _ => make_keyword("view"), - keyword_materialized: _ => make_keyword("materialized"), keyword_column: _ => make_keyword("column"), keyword_columns: _ => make_keyword("columns"), + keyword_materialized: _ => make_keyword("materialized"), + keyword_tablespace: _ => make_keyword("tablespace"), + keyword_sequence: _ => make_keyword("sequence"), + keyword_increment: _ => make_keyword("increment"), + keyword_none: _ => make_keyword("none"), + keyword_owned: _ => make_keyword("owned"), + keyword_start: _ => make_keyword("start"), + keyword_restart: _ => make_keyword("restart"), keyword_key: _ => make_keyword("key"), keyword_as: _ => make_keyword("as"), keyword_distinct: _ => make_keyword("distinct"), @@ -160,6 +167,8 @@ module.exports = grammar({ keyword_temp: _ => make_keyword("temp"), keyword_temporary: _ => make_keyword("temporary"), keyword_unlogged: _ => make_keyword("unlogged"), + keyword_logged: _ => make_keyword("logged"), + keyword_cycle: _ => make_keyword("cycle"), keyword_union: _ => make_keyword("union"), keyword_all: _ => make_keyword("all"), keyword_any: _ => make_keyword("any"), @@ -750,11 +759,11 @@ module.exports = grammar({ $.create_type, $.create_database, $.create_role, + $.create_sequence, prec.left(seq( $.create_schema, repeat($._create_statement), )), - // TODO sequence ), ), @@ -1161,8 +1170,6 @@ module.exports = grammar({ ), ), - - _user_access_role_config: $ => prec.left(seq( choice( seq(optional($.keyword_in), $.keyword_role), @@ -1171,7 +1178,29 @@ module.exports = grammar({ $.keyword_user, ), comma_list($.identifier, true), - ), + )), + + create_sequence: $ => seq( + $.keyword_create, + optional( + choice( + choice($.keyword_temporary, $.keyword_temp), + $.keyword_unlogged, + ) + ), + $.keyword_sequence, + optional($._if_not_exists), + $.object_reference, + optional(seq($.keyword_as, $._type)), + optional(seq($.keyword_increment, optional($.keyword_by), field("increment", alias($._integer, $.literal)))), + optional(seq( + $.keyword_start, + optional($.keyword_with), + field("start", alias($._integer, $.literal)), + optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), + optional(seq(optional($.keyword_no), $.keyword_cycle)) + )), + optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), ), create_type: $ => seq( @@ -1220,6 +1249,7 @@ module.exports = grammar({ $.alter_index, $.alter_database, $.alter_role, + $.alter_sequence, ), ), @@ -1418,7 +1448,6 @@ module.exports = grammar({ $.identifier, ), - // TODO more variants alter_database: $ => seq( $.keyword_alter, $.keyword_database, @@ -1427,6 +1456,26 @@ module.exports = grammar({ choice( seq($.rename_object), seq($.change_ownership), + seq( + $.keyword_reset, + choice( + $.keyword_all, + field("configuration_parameter", $.identifier) + ), + ), + seq( + $.keyword_set, + choice( + seq($.keyword_tablespace, $.identifier), + seq( + field("configuration_parameter", $.identifier), + choice( + seq($.keyword_from, $.keyword_current), + $.set_configuration, + ) + ), + ), + ), ), ), @@ -1449,14 +1498,7 @@ module.exports = grammar({ field("option", $.identifier), choice( seq($.keyword_from, $.keyword_current), - seq( - choice($.keyword_to, "="), - choice( - field("parameter", $.identifier), - $.literal, - $.keyword_default - ) - ), + $.set_configuration, ), ), seq( @@ -1470,14 +1512,79 @@ module.exports = grammar({ ), ), - // TODO more variants + set_configuration: $ => seq( + choice($.keyword_to, "="), + choice( + field("parameter", $.identifier), + $.literal, + $.keyword_default + ) + ), + alter_index: $ => seq( $.keyword_alter, $.keyword_index, optional($._if_exists), $.identifier, choice( - $.rename_object + $.rename_object, + seq( + $.keyword_alter, + optional($.keyword_column), + alias($._natural_number, $.literal), + $.keyword_set, + $.keyword_statistics, + alias($._natural_number, $.literal), + ), + seq($.keyword_reset, paren_list($.identifier)), + seq( + $.keyword_set, + choice( + seq($.keyword_tablespace, $.identifier), + paren_list(seq($.identifier, '=', field("value", $.literal))) + ), + ), + ), + ), + + alter_sequence: $ => seq( + $.keyword_alter, + $.keyword_sequence, + optional($._if_exists), + $.object_reference, + choice( + seq( + optional(seq( + $.keyword_start, + optional($.keyword_with), + field("start", alias($._integer, $.literal)), + optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), + optional(seq(optional($.keyword_no), $.keyword_cycle)) + )), + optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), + optional(seq( + $.keyword_start, + optional($.keyword_with), + field("start", alias($._integer, $.literal)), + optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), + optional(seq(optional($.keyword_no), $.keyword_cycle)) + )), + optional(seq( + $.keyword_restart, + optional($.keyword_with), + field("restart", alias($._integer, $.literal)), + )), + optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), + ), + $.rename_object, + $.change_ownership, + seq( + $.keyword_set, + choice( + choice($.keyword_logged, $.keyword_unlogged), + seq($.keyword_schema, $.identifier) + ), + ), ), ), @@ -1557,6 +1664,7 @@ module.exports = grammar({ $.drop_schema, $.drop_database, $.drop_role, + $.drop_sequence, ), ), @@ -1612,6 +1720,14 @@ module.exports = grammar({ optional($._drop_behavior), ), + drop_sequence: $ => seq( + $.keyword_drop, + $.keyword_sequence, + optional($._if_exists), + $.object_reference, + optional($._drop_behavior), + ), + drop_index: $ => seq( $.keyword_drop, $.keyword_index, diff --git a/queries/highlights.scm b/queries/highlights.scm index fae13433..36cf4bd0 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -242,6 +242,25 @@ (keyword_tblproperties) (keyword_trigger) (keyword_unsafe) + (keyword_admin) + (keyword_connection) + (keyword_cycle) + (keyword_database) + (keyword_encrypted) + (keyword_increment) + (keyword_logged) + (keyword_none) + (keyword_owned) + (keyword_password) + (keyword_reset) + (keyword_role) + (keyword_sequence) + (keyword_start) + (keyword_restart) + (keyword_tablespace) + (keyword_until) + (keyword_user) + (keyword_valid) ] @keyword [ diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt index 4d5ea9e8..c1937ad2 100644 --- a/test/corpus/alter.txt +++ b/test/corpus/alter.txt @@ -720,6 +720,53 @@ ALTER INDEX IF EXISTS myindex RENAME TO my_index (object_reference (identifier)))))) +================================================================================ +ALTER INDEX +================================================================================ + +ALTER INDEX distributors SET (fillfactor = 75); +ALTER INDEX distributors RESET (fillfactor); +ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000; +ALTER INDEX distributors SET TABLESPACE fasttablespace; + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_set) + (identifier) + (literal))) + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_reset) + (identifier))) + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_alter) + (keyword_column) + (literal) + (keyword_set) + (keyword_statistics) + (literal))) + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_set) + (keyword_tablespace) + (identifier)))) + ================================================================================ ALTER DATABASE RENAME ================================================================================ @@ -740,6 +787,72 @@ ALTER DATABASE hollywood RENAME TO bollywood; (object_reference (identifier)))))) +================================================================================ +ALTER DATABASE +================================================================================ + +ALTER DATABASE test SET enable_indexscan TO off; +ALTER DATABASE test SET enable_indexscan TO DEFAULT; +ALTER DATABASE test OWNER TO me; +ALTER DATABASE test SET TABLESPACE fasttablespace; +ALTER DATABASE test RESET ALL; +ALTER DATABASE test RESET enable_indexscan; + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (keyword_set) + (identifier) + (set_configuration + (keyword_to) + (identifier)))) + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (keyword_set) + (identifier) + (set_configuration + (keyword_to) + (keyword_default)))) + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (change_ownership + (keyword_owner) + (keyword_to) + (identifier)))) + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (keyword_set) + (keyword_tablespace) + (identifier))) + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (keyword_reset) + (keyword_all))) + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (keyword_reset) + (identifier)))) + ================================================================================ ALTER ROLE ================================================================================ @@ -753,3 +866,123 @@ ALTER ROLE worker_bee SET maintenance_work_mem = 100000; ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; -------------------------------------------------------------------------------- + +(program + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (rename_object + (keyword_rename) + (keyword_to) + (object_reference + (identifier))))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (identifier) + (identifier) + (keyword_connection) + (keyword_limit) + (literal))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (keyword_valid) + (keyword_until) + (literal))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (keyword_valid) + (keyword_until) + (literal))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (identifier) + (identifier))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (keyword_set) + (identifier) + (set_configuration + (literal)))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (keyword_in) + (keyword_database) + (identifier) + (keyword_set) + (identifier) + (set_configuration + (identifier))))) + +================================================================================ +ALTER SEQUENCE +================================================================================ + +ALTER SEQUENCE serial RESTART WITH 105; +ALTER SEQUENCE IF EXISTS serial RENAME TO cereal; +ALTER SEQUENCE serial SET SCHEMA serious_schema; +ALTER SEQUENCE serial OWNER TO count_von_count; + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_sequence + (keyword_alter) + (keyword_sequence) + (object_reference + (identifier)) + (keyword_restart) + (keyword_with) + (literal))) + (statement + (alter_sequence + (keyword_alter) + (keyword_sequence) + (keyword_if) + (keyword_exists) + (object_reference + (identifier)) + (rename_object + (keyword_rename) + (keyword_to) + (object_reference + (identifier))))) + (statement + (alter_sequence + (keyword_alter) + (keyword_sequence) + (object_reference + (identifier)) + (keyword_set) + (keyword_schema) + (identifier))) + (statement + (alter_sequence + (keyword_alter) + (keyword_sequence) + (object_reference + (identifier)) + (change_ownership + (keyword_owner) + (keyword_to) + (identifier))))) diff --git a/test/corpus/create.txt b/test/corpus/create.txt index a21a87c8..c017e2c0 100644 --- a/test/corpus/create.txt +++ b/test/corpus/create.txt @@ -1797,3 +1797,63 @@ INHERIT; (literal) (identifier) (identifier)))) + +================================================================================ +CREATE SEQUENCE +================================================================================ + +CREATE SEQUENCE IF NOT EXISTS serial START 101; +CREATE UNLOGGED SEQUENCE serial START 101; +CREATE TEMP SEQUENCE serial START 101 OWNED BY sequences.serials; +CREATE SEQUENCE serial START WITH 101 CACHE 1000 NO CYCLE; + +-------------------------------------------------------------------------------- + +(program + (statement + (create_sequence + (keyword_create) + (keyword_sequence) + (keyword_if) + (keyword_not) + (keyword_exists) + (object_reference + (identifier)) + (keyword_start) + (literal))) + (statement + (create_sequence + (keyword_create) + (keyword_unlogged) + (keyword_sequence) + (object_reference + (identifier)) + (keyword_start) + (literal))) + (statement + (create_sequence + (keyword_create) + (keyword_temp) + (keyword_sequence) + (object_reference + (identifier)) + (keyword_start) + (literal) + (keyword_owned) + (keyword_by) + (object_reference + (identifier) + (identifier)))) + (statement + (create_sequence + (keyword_create) + (keyword_sequence) + (object_reference + (identifier)) + (keyword_start) + (keyword_with) + (literal) + (keyword_cache) + (literal) + (keyword_no) + (keyword_cycle)))) diff --git a/test/corpus/drop.txt b/test/corpus/drop.txt index 6895242f..75bf3b88 100644 --- a/test/corpus/drop.txt +++ b/test/corpus/drop.txt @@ -187,3 +187,22 @@ DROP USER rapunzel; (keyword_drop) (keyword_user) (identifier)))) + +================================================================================ +Drop sequence +================================================================================ + +DROP SEQUENCE IF EXISTS serial RESTRICT; + +-------------------------------------------------------------------------------- + +(program + (statement + (drop_sequence + (keyword_drop) + (keyword_sequence) + (keyword_if) + (keyword_exists) + (object_reference + (identifier)) + (keyword_restrict)))) From 4fe97da8d6cb9079175f6cfad91c1938424a59de Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Wed, 2 Aug 2023 21:52:51 +0200 Subject: [PATCH 3/6] improve alter sequence and create table --- grammar.js | 63 +++++++++++++++++-------------------------- test/corpus/alter.txt | 8 +++--- 2 files changed, 28 insertions(+), 43 deletions(-) diff --git a/grammar.js b/grammar.js index 0aeebd36..b5906f86 100644 --- a/grammar.js +++ b/grammar.js @@ -105,6 +105,8 @@ module.exports = grammar({ keyword_tablespace: _ => make_keyword("tablespace"), keyword_sequence: _ => make_keyword("sequence"), keyword_increment: _ => make_keyword("increment"), + keyword_minvalue: _ => make_keyword("minvalue"), + keyword_maxvalue: _ => make_keyword("maxvalue"), keyword_none: _ => make_keyword("none"), keyword_owned: _ => make_keyword("owned"), keyword_start: _ => make_keyword("start"), @@ -1170,7 +1172,7 @@ module.exports = grammar({ ), ), - _user_access_role_config: $ => prec.left(seq( + _user_access_role_config: $ => seq( choice( seq(optional($.keyword_in), $.keyword_role), seq($.keyword_in, $.keyword_group), @@ -1178,7 +1180,7 @@ module.exports = grammar({ $.keyword_user, ), comma_list($.identifier, true), - )), + ), create_sequence: $ => seq( $.keyword_create, @@ -1467,17 +1469,11 @@ module.exports = grammar({ $.keyword_set, choice( seq($.keyword_tablespace, $.identifier), - seq( - field("configuration_parameter", $.identifier), - choice( - seq($.keyword_from, $.keyword_current), - $.set_configuration, - ) + $.set_configuration, ), ), ), ), - ), alter_role: $ => seq( $.keyword_alter, @@ -1495,11 +1491,7 @@ module.exports = grammar({ choice( seq( $.keyword_set, - field("option", $.identifier), - choice( - seq($.keyword_from, $.keyword_current), - $.set_configuration, - ), + $.set_configuration, ), seq( $.keyword_reset, @@ -1513,12 +1505,18 @@ module.exports = grammar({ ), set_configuration: $ => seq( - choice($.keyword_to, "="), + field("option", $.identifier), choice( - field("parameter", $.identifier), - $.literal, - $.keyword_default - ) + seq($.keyword_from, $.keyword_current), + seq( + choice($.keyword_to, "="), + choice( + field("parameter", $.identifier), + $.literal, + $.keyword_default + ) + ) + ), ), alter_index: $ => seq( @@ -1554,26 +1552,13 @@ module.exports = grammar({ $.object_reference, choice( seq( - optional(seq( - $.keyword_start, - optional($.keyword_with), - field("start", alias($._integer, $.literal)), - optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), - optional(seq(optional($.keyword_no), $.keyword_cycle)) - )), - optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), - optional(seq( - $.keyword_start, - optional($.keyword_with), - field("start", alias($._integer, $.literal)), - optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), - optional(seq(optional($.keyword_no), $.keyword_cycle)) - )), - optional(seq( - $.keyword_restart, - optional($.keyword_with), - field("restart", alias($._integer, $.literal)), - )), + optional(seq($.keyword_as, $._type)), + optional(seq($.keyword_increment, optional($.keyword_by), $.literal)), + optional(seq($.keyword_minvalue, choice($.literal, seq($.keyword_no, $.keyword_minvalue)))), + optional(seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue)))), + optional(seq($.keyword_start, optional($.keyword_with), field("start", alias($._integer, $.literal)))), + optional(seq($.keyword_restart, optional($.keyword_with), field("restart", alias($._integer, $.literal)))), + optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)), optional(seq(optional($.keyword_no), $.keyword_cycle)))), optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), ), $.rename_object, diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt index c1937ad2..57e37ded 100644 --- a/test/corpus/alter.txt +++ b/test/corpus/alter.txt @@ -807,8 +807,8 @@ ALTER DATABASE test RESET enable_indexscan; (keyword_database) (identifier) (keyword_set) - (identifier) (set_configuration + (identifier) (keyword_to) (identifier)))) (statement @@ -817,8 +817,8 @@ ALTER DATABASE test RESET enable_indexscan; (keyword_database) (identifier) (keyword_set) - (identifier) (set_configuration + (identifier) (keyword_to) (keyword_default)))) (statement @@ -917,8 +917,8 @@ ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; (keyword_role) (identifier) (keyword_set) - (identifier) (set_configuration + (identifier) (literal)))) (statement (alter_role @@ -929,8 +929,8 @@ ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; (keyword_database) (identifier) (keyword_set) - (identifier) (set_configuration + (identifier) (identifier))))) ================================================================================ From 51f9e5021cb6a44ffda2de26b8ef912c22ef5a62 Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Wed, 2 Aug 2023 22:27:58 +0200 Subject: [PATCH 4/6] breakout tests, sort test and format test names --- grammar.js | 2 + queries/highlights.scm | 2 + test/corpus/alter.txt | 299 ++++++++++++++++++++++++++--------- test/corpus/create.txt | 97 ++++++------ test/corpus/custom_types.txt | 36 ++--- 5 files changed, 297 insertions(+), 139 deletions(-) diff --git a/grammar.js b/grammar.js index b5906f86..277236b6 100644 --- a/grammar.js +++ b/grammar.js @@ -1195,6 +1195,8 @@ module.exports = grammar({ $.object_reference, optional(seq($.keyword_as, $._type)), optional(seq($.keyword_increment, optional($.keyword_by), field("increment", alias($._integer, $.literal)))), + optional(seq($.keyword_minvalue, choice($.literal, seq($.keyword_no, $.keyword_minvalue)))), + optional(seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue)))), optional(seq( $.keyword_start, optional($.keyword_with), diff --git a/queries/highlights.scm b/queries/highlights.scm index 36cf4bd0..99e33b3a 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -285,6 +285,8 @@ (keyword_noscan) (keyword_stats) (keyword_statistics) + (keyword_maxvalue) + (keyword_minvalue) ] @type.qualifier [ diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt index 57e37ded..b1abdebe 100644 --- a/test/corpus/alter.txt +++ b/test/corpus/alter.txt @@ -533,27 +533,6 @@ ALTER TABLE "Role" ADD CONSTRAINT "pkRole" PRIMARY KEY ("roleId"); (column name: (literal)))))))) -================================================================================ -CREATE UNIQUE INDEX -================================================================================ - -CREATE UNIQUE INDEX "akRoleName" ON "Role" ("name"); - --------------------------------------------------------------------------------- - -(program - (statement - (create_index - (keyword_create) - (keyword_unique) - (keyword_index) - column: (literal) - (keyword_on) - (object_reference - name: (identifier)) - (ordered_columns - (column - name: (literal)))))) ================================================================================ Add foreign key constraint @@ -663,7 +642,7 @@ col2 VARCHAR(255) NOT NULL DEFAULT('EMPTY'); (literal))))))) ================================================================================ -ALTER SCHEMA RENAME +Alter schema rename ================================================================================ ALTER SCHEMA sales RENAME TO mysales; @@ -681,7 +660,7 @@ ALTER SCHEMA sales RENAME TO mysales; (identifier)))) ================================================================================ -ALTER SCHEMA OWNER +Alter schema owner ================================================================================ ALTER SCHEMA sales OWNER TO CURRENT_USER; @@ -699,7 +678,7 @@ ALTER SCHEMA sales OWNER TO CURRENT_USER; (identifier)))) ================================================================================ -ALTER INDEX RENAME +Alter index rename ================================================================================ ALTER INDEX IF EXISTS myindex RENAME TO my_index @@ -721,13 +700,12 @@ ALTER INDEX IF EXISTS myindex RENAME TO my_index (identifier)))))) ================================================================================ -ALTER INDEX +Alter index set ================================================================================ ALTER INDEX distributors SET (fillfactor = 75); -ALTER INDEX distributors RESET (fillfactor); -ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000; ALTER INDEX distributors SET TABLESPACE fasttablespace; +ALTER INDEX distributors RESET (fillfactor); -------------------------------------------------------------------------------- @@ -745,8 +723,26 @@ ALTER INDEX distributors SET TABLESPACE fasttablespace; (keyword_alter) (keyword_index) (identifier) - (keyword_reset) + (keyword_set) + (keyword_tablespace) (identifier))) + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_reset) + (identifier)))) + +================================================================================ +Alter index alter column +================================================================================ + +ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000; + +-------------------------------------------------------------------------------- + +(program (statement (alter_index (keyword_alter) @@ -757,6 +753,26 @@ ALTER INDEX distributors SET TABLESPACE fasttablespace; (literal) (keyword_set) (keyword_statistics) + (literal)))) + +================================================================================ +Alter index set and reset +================================================================================ + +ALTER INDEX distributors SET (fillfactor = 75); +ALTER INDEX distributors SET TABLESPACE fasttablespace; +ALTER INDEX distributors RESET (fillfactor); + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_set) + (identifier) (literal))) (statement (alter_index @@ -765,10 +781,17 @@ ALTER INDEX distributors SET TABLESPACE fasttablespace; (identifier) (keyword_set) (keyword_tablespace) + (identifier))) + (statement + (alter_index + (keyword_alter) + (keyword_index) + (identifier) + (keyword_reset) (identifier)))) ================================================================================ -ALTER DATABASE RENAME +Alter database rename ================================================================================ ALTER DATABASE hollywood RENAME TO bollywood; @@ -788,15 +811,12 @@ ALTER DATABASE hollywood RENAME TO bollywood; (identifier)))))) ================================================================================ -ALTER DATABASE +Alter database set ================================================================================ ALTER DATABASE test SET enable_indexscan TO off; ALTER DATABASE test SET enable_indexscan TO DEFAULT; -ALTER DATABASE test OWNER TO me; ALTER DATABASE test SET TABLESPACE fasttablespace; -ALTER DATABASE test RESET ALL; -ALTER DATABASE test RESET enable_indexscan; -------------------------------------------------------------------------------- @@ -821,15 +841,6 @@ ALTER DATABASE test RESET enable_indexscan; (identifier) (keyword_to) (keyword_default)))) - (statement - (alter_database - (keyword_alter) - (keyword_database) - (identifier) - (change_ownership - (keyword_owner) - (keyword_to) - (identifier)))) (statement (alter_database (keyword_alter) @@ -837,7 +848,18 @@ ALTER DATABASE test RESET enable_indexscan; (identifier) (keyword_set) (keyword_tablespace) - (identifier))) + (identifier)))) + +================================================================================ +Alter datbase reset +================================================================================ + +ALTER DATABASE test RESET ALL; +ALTER DATABASE test RESET enable_indexscan; + +-------------------------------------------------------------------------------- + +(program (statement (alter_database (keyword_alter) @@ -854,16 +876,29 @@ ALTER DATABASE test RESET enable_indexscan; (identifier)))) ================================================================================ -ALTER ROLE +ALTER DATBASE change ownership +================================================================================ + +ALTER DATABASE test OWNER TO me; + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_database + (keyword_alter) + (keyword_database) + (identifier) + (change_ownership + (keyword_owner) + (keyword_to) + (identifier))))) + +================================================================================ +Alter role rename ================================================================================ ALTER ROLE rapunzel RENAME TO snow_white; -ALTER ROLE rapunzel SUPERUSER NOLOGIN CONNECTION LIMIT 69; -ALTER ROLE chris VALID UNTIL 'May 4 12:00:00 2015 +1'; -ALTER ROLE fred VALID UNTIL 'infinity'; -ALTER ROLE miriam CREATEROLE CREATEDB; -ALTER ROLE worker_bee SET maintenance_work_mem = 100000; -ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; -------------------------------------------------------------------------------- @@ -877,17 +912,18 @@ ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; (keyword_rename) (keyword_to) (object_reference - (identifier))))) - (statement - (alter_role - (keyword_alter) - (keyword_role) - (identifier) - (identifier) - (identifier) - (keyword_connection) - (keyword_limit) - (literal))) + (identifier)))))) + +================================================================================ +Alter role valid until +================================================================================ + +ALTER ROLE chris VALID UNTIL 'May 4 12:00:00 2015 +1'; +ALTER ROLE fred VALID UNTIL 'infinity'; + +-------------------------------------------------------------------------------- + +(program (statement (alter_role (keyword_alter) @@ -903,14 +939,30 @@ ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; (identifier) (keyword_valid) (keyword_until) - (literal))) + (literal)))) + +================================================================================ +Alter role set +================================================================================ + +ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; +ALTER ROLE worker_bee SET maintenance_work_mem = 100000; + +-------------------------------------------------------------------------------- + +(program (statement (alter_role (keyword_alter) (keyword_role) (identifier) + (keyword_in) + (keyword_database) (identifier) - (identifier))) + (keyword_set) + (set_configuration + (identifier) + (identifier)))) (statement (alter_role (keyword_alter) @@ -919,28 +971,41 @@ ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG; (keyword_set) (set_configuration (identifier) - (literal)))) + (literal))))) + +================================================================================ +Alter role assigns +================================================================================ + +ALTER ROLE miriam CREATEROLE CREATEDB; +ALTER ROLE rapunzel SUPERUSER NOLOGIN CONNECTION LIMIT 69; + +-------------------------------------------------------------------------------- + +(program (statement (alter_role (keyword_alter) (keyword_role) (identifier) - (keyword_in) - (keyword_database) (identifier) - (keyword_set) - (set_configuration - (identifier) - (identifier))))) + (identifier))) + (statement + (alter_role + (keyword_alter) + (keyword_role) + (identifier) + (identifier) + (identifier) + (keyword_connection) + (keyword_limit) + (literal)))) ================================================================================ -ALTER SEQUENCE +Alter sequence restart ================================================================================ ALTER SEQUENCE serial RESTART WITH 105; -ALTER SEQUENCE IF EXISTS serial RENAME TO cereal; -ALTER SEQUENCE serial SET SCHEMA serious_schema; -ALTER SEQUENCE serial OWNER TO count_von_count; -------------------------------------------------------------------------------- @@ -953,7 +1018,17 @@ ALTER SEQUENCE serial OWNER TO count_von_count; (identifier)) (keyword_restart) (keyword_with) - (literal))) + (literal)))) + +================================================================================ +Alter sequence rename +================================================================================ + +ALTER SEQUENCE IF EXISTS serial RENAME TO cereal; + +-------------------------------------------------------------------------------- + +(program (statement (alter_sequence (keyword_alter) @@ -966,7 +1041,17 @@ ALTER SEQUENCE serial OWNER TO count_von_count; (keyword_rename) (keyword_to) (object_reference - (identifier))))) + (identifier)))))) + +================================================================================ +Alter sequence change Schema +================================================================================ + +ALTER SEQUENCE serial SET SCHEMA serious_schema; + +-------------------------------------------------------------------------------- + +(program (statement (alter_sequence (keyword_alter) @@ -975,7 +1060,17 @@ ALTER SEQUENCE serial OWNER TO count_von_count; (identifier)) (keyword_set) (keyword_schema) - (identifier))) + (identifier)))) + +================================================================================ +Alter sequence change owner +================================================================================ + +ALTER SEQUENCE serial OWNER TO count_von_count; + +-------------------------------------------------------------------------------- + +(program (statement (alter_sequence (keyword_alter) @@ -986,3 +1081,53 @@ ALTER SEQUENCE serial OWNER TO count_von_count; (keyword_owner) (keyword_to) (identifier))))) + +================================================================================ +Alter sequence complex example +================================================================================ + +ALTER SEQUENCE IF EXISTS serial +AS BIGINT +INCREMENT BY 2 +MINVALUE 3 +MAXVALUE NO MAXVALUE +START WITH 11 +RESTART WITH 1111 +CACHE 100 +OWNED BY numbers.number_sequences + +-------------------------------------------------------------------------------- + +(program + (statement + (alter_sequence + (keyword_alter) + (keyword_sequence) + (keyword_if) + (keyword_exists) + (object_reference + (identifier)) + (keyword_as) + (bigint + (keyword_bigint)) + (keyword_increment) + (keyword_by) + (literal) + (keyword_minvalue) + (literal) + (keyword_maxvalue) + (keyword_no) + (keyword_maxvalue) + (keyword_start) + (keyword_with) + (literal) + (keyword_restart) + (keyword_with) + (literal) + (keyword_cache) + (literal) + (keyword_owned) + (keyword_by) + (object_reference + (identifier) + (identifier))))) diff --git a/test/corpus/create.txt b/test/corpus/create.txt index c017e2c0..69a58ab7 100644 --- a/test/corpus/create.txt +++ b/test/corpus/create.txt @@ -1133,6 +1133,28 @@ CREATE TABLE some_table( name: (keyword_engine) value: (identifier))))) +================================================================================ +Create unique index +================================================================================ + +CREATE UNIQUE INDEX "akRoleName" ON "Role" ("name"); + +-------------------------------------------------------------------------------- + +(program + (statement + (create_index + (keyword_create) + (keyword_unique) + (keyword_index) + column: (literal) + (keyword_on) + (object_reference + name: (identifier)) + (ordered_columns + (column + name: (literal)))))) + ================================================================================ Create table as select ================================================================================ @@ -1557,7 +1579,7 @@ CREATE TABLE "Session" ( (keyword_null)))))) ================================================================================ -CREATE TABLE with ARRAYS and MATRIX +Create table with arrays and matrix ================================================================================ CREATE TABLE tab ( @@ -1624,7 +1646,7 @@ CREATE TABLE tab ( (literal))))))) ================================================================================ -CREATE SCHEMA +Create schema ================================================================================ CREATE SCHEMA myschema; @@ -1639,7 +1661,7 @@ CREATE SCHEMA myschema; (identifier)))) ================================================================================ -CREATE SCHEMA with authorization +Create schema with authorization ================================================================================ CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe; @@ -1659,7 +1681,7 @@ CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe; (identifier)))) ================================================================================ -CREATE SCHEMA with subsequent creates +Create schema with subsequent creates ================================================================================ CREATE SCHEMA hollywood @@ -1724,7 +1746,7 @@ CREATE SCHEMA hollywood (keyword_null))))))))) ================================================================================ -CREATE DATABASE +Create database ================================================================================ CREATE DATABASE hollywood @@ -1739,7 +1761,7 @@ CREATE DATABASE hollywood (identifier)))) ================================================================================ -CREATE DATABASE with settings +Create database with settings ================================================================================ CREATE DATABASE sales OWNER operations_dept; @@ -1756,7 +1778,7 @@ CREATE DATABASE sales OWNER operations_dept; (identifier)))) ================================================================================ -CREATE USER +Create user ================================================================================ CREATE ROLE rapunzel @@ -1799,13 +1821,16 @@ INHERIT; (identifier)))) ================================================================================ -CREATE SEQUENCE +Create sequence ================================================================================ -CREATE SEQUENCE IF NOT EXISTS serial START 101; -CREATE UNLOGGED SEQUENCE serial START 101; -CREATE TEMP SEQUENCE serial START 101 OWNED BY sequences.serials; -CREATE SEQUENCE serial START WITH 101 CACHE 1000 NO CYCLE; +CREATE TEMP SEQUENCE IF NOT EXISTS serial +AS BIGINT +INCREMENT BY 3 +MINVALUE 10 +MAXVALUE 9999 +START 101 CACHE 1000 NO CYCLE +OWNED BY numbers.number_sequences; -------------------------------------------------------------------------------- @@ -1813,47 +1838,31 @@ CREATE SEQUENCE serial START WITH 101 CACHE 1000 NO CYCLE; (statement (create_sequence (keyword_create) + (keyword_temp) (keyword_sequence) (keyword_if) (keyword_not) (keyword_exists) (object_reference (identifier)) - (keyword_start) - (literal))) - (statement - (create_sequence - (keyword_create) - (keyword_unlogged) - (keyword_sequence) - (object_reference - (identifier)) - (keyword_start) - (literal))) - (statement - (create_sequence - (keyword_create) - (keyword_temp) - (keyword_sequence) - (object_reference - (identifier)) - (keyword_start) - (literal) - (keyword_owned) + (keyword_as) + (bigint + (keyword_bigint)) + (keyword_increment) (keyword_by) - (object_reference - (identifier) - (identifier)))) - (statement - (create_sequence - (keyword_create) - (keyword_sequence) - (object_reference - (identifier)) + (literal) + (keyword_minvalue) + (literal) + (keyword_maxvalue) + (literal) (keyword_start) - (keyword_with) (literal) (keyword_cache) (literal) (keyword_no) - (keyword_cycle)))) + (keyword_cycle) + (keyword_owned) + (keyword_by) + (object_reference + (identifier) + (identifier))))) diff --git a/test/corpus/custom_types.txt b/test/corpus/custom_types.txt index e99e9c5e..501f8f9f 100644 --- a/test/corpus/custom_types.txt +++ b/test/corpus/custom_types.txt @@ -1,5 +1,5 @@ ================================================================================ -CREATE TYPE +Create type ================================================================================ CREATE TYPE sports @@ -14,7 +14,7 @@ CREATE TYPE (identifier)))) ================================================================================ -CREATE TYPE ENUM +Create type enum ================================================================================ CREATE TYPE colors as ENUM ( @@ -39,7 +39,7 @@ CREATE TYPE colors as ENUM ( (literal))))) ================================================================================ -CREATE TYPE Object +Create type object ================================================================================ CREATE TYPE cities AS ( cityname text, @@ -64,7 +64,7 @@ CREATE TYPE cities AS ( (keyword_int))))))) ================================================================================ -CREATE TYPE RANGE +Create type range ================================================================================ CREATE TYPE marathon AS RANGE ( @@ -85,7 +85,7 @@ CREATE TYPE marathon AS RANGE ( (identifier)))) ================================================================================ -CREATE TYPE IO +Create type io ================================================================================ CREATE TYPE bigobj ( INPUT = lo_filein, OUTPUT = lo_fileout, @@ -107,7 +107,7 @@ CREATE TYPE bigobj ( (identifier)))) ================================================================================ -DROP TYPE +Drop type ================================================================================ DROP TYPE IF EXISTS boxes CASCADE; @@ -126,7 +126,7 @@ DROP TYPE IF EXISTS boxes CASCADE; (keyword_cascade)))) ================================================================================ -ALTER TYPE RENAME +Alter type rename ================================================================================ ALTER TYPE boxes RENAME TO cubes; @@ -146,7 +146,7 @@ ALTER TYPE boxes RENAME TO cubes; (identifier)))))) ================================================================================ -ALTER TYPE RENAME ATTRIBUTE +Alter type rename attribute ================================================================================ ALTER TYPE boxes @@ -167,7 +167,7 @@ RENAME ATTRIBUTE width TO height; (identifier)))) ================================================================================ -ALTER TYPE CHANGE OWNER +Alter type change owner ================================================================================ ALTER TYPE boxes @@ -187,7 +187,7 @@ OWNER TO user2 (identifier))))) ================================================================================ -ALTER TYPE CHANGE SCHEMA +Alter type change schema ================================================================================ ALTER TYPE boxes @@ -207,7 +207,7 @@ SET SCHEMA new_schema (identifier))))) ================================================================================ -ALTER TYPE ADD VALUE +Alter type add value ================================================================================ ALTER TYPE boxes @@ -231,7 +231,7 @@ ADD VALUE IF NOT EXISTS 'color' AFTER 'weight' (literal)))) ================================================================================ -ALTER TYPE RENAME VALUE +Alter type rename value ================================================================================ ALTER TYPE boxes @@ -252,7 +252,7 @@ RENAME VALUE 'weight' TO 'mass' (literal)))) ================================================================================ -ALTER TYPE ADD ATTRIBUTE +Alter type add attribute ================================================================================ ALTER TYPE boxes @@ -272,7 +272,7 @@ ADD ATTRIBUTE label text (keyword_text)))) ================================================================================ -ALTER TYPE DROP ATTRIBUTE +Alter type drop attribute ================================================================================ ALTER TYPE boxes DROP ATTRIBUTE IF EXISTS label @@ -291,7 +291,7 @@ ALTER TYPE boxes DROP ATTRIBUTE IF EXISTS label (identifier)))) ================================================================================ -ALTER TYPE ALTER ATTRIBUTE +Alter type alter attribute ================================================================================ ALTER TYPE boxes @@ -313,7 +313,7 @@ ADD ATTRIBUTE label varchar(255) (literal))))) ================================================================================ -CREATE TABLE WITH CUSTOM TYPE +Create table with custom type ================================================================================ CREATE TABLE shipments ( @@ -333,7 +333,7 @@ shipment boxes (identifier)))))) ================================================================================ -INSERT INTO TABLE WITH CUSTOM TYPE +Insert into table with custom type ================================================================================ INSERT INTO shipments (shipment) @@ -386,7 +386,7 @@ VALUES (ROW(10, 500, 'Box1')), (literal))))))) ================================================================================ -SELECT FROM CUSTOM TYPE +Select from custom type ================================================================================ SELECT From bcd61d6e8c14c7abcd0bfaf5432995e127a9f179 Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Sun, 6 Aug 2023 20:54:53 +0200 Subject: [PATCH 5/6] fix: alter/create sequence with no cache --- grammar.js | 5 +++-- test/corpus/alter.txt | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/grammar.js b/grammar.js index 277236b6..8b998ff6 100644 --- a/grammar.js +++ b/grammar.js @@ -1202,8 +1202,8 @@ module.exports = grammar({ optional($.keyword_with), field("start", alias($._integer, $.literal)), optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), - optional(seq(optional($.keyword_no), $.keyword_cycle)) )), + optional(seq(optional($.keyword_no), $.keyword_cycle)), optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), ), @@ -1560,7 +1560,8 @@ module.exports = grammar({ optional(seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue)))), optional(seq($.keyword_start, optional($.keyword_with), field("start", alias($._integer, $.literal)))), optional(seq($.keyword_restart, optional($.keyword_with), field("restart", alias($._integer, $.literal)))), - optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)), optional(seq(optional($.keyword_no), $.keyword_cycle)))), + optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), + optional(seq(optional($.keyword_no), $.keyword_cycle)), optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), ), $.rename_object, diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt index b1abdebe..d728ccf7 100644 --- a/test/corpus/alter.txt +++ b/test/corpus/alter.txt @@ -876,7 +876,7 @@ ALTER DATABASE test RESET enable_indexscan; (identifier)))) ================================================================================ -ALTER DATBASE change ownership +Alter datbase change ownership ================================================================================ ALTER DATABASE test OWNER TO me; From 727e9ea86cd4b09a07c1cefe4a2d1fd76c472cde Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Mon, 7 Aug 2023 17:59:36 +0200 Subject: [PATCH 6/6] refactor alter/create sequence --- grammar.js | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/grammar.js b/grammar.js index 8b998ff6..af39e370 100644 --- a/grammar.js +++ b/grammar.js @@ -1193,18 +1193,18 @@ module.exports = grammar({ $.keyword_sequence, optional($._if_not_exists), $.object_reference, - optional(seq($.keyword_as, $._type)), - optional(seq($.keyword_increment, optional($.keyword_by), field("increment", alias($._integer, $.literal)))), - optional(seq($.keyword_minvalue, choice($.literal, seq($.keyword_no, $.keyword_minvalue)))), - optional(seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue)))), - optional(seq( - $.keyword_start, - optional($.keyword_with), - field("start", alias($._integer, $.literal)), - optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), - )), - optional(seq(optional($.keyword_no), $.keyword_cycle)), - optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), + repeat( + choice( + seq($.keyword_as, $._type), + seq($.keyword_increment, optional($.keyword_by), field("increment", alias($._integer, $.literal))), + seq($.keyword_minvalue, choice($.literal, seq($.keyword_no, $.keyword_minvalue))), + seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue))), + seq($.keyword_start, optional($.keyword_with), field("start", alias($._integer, $.literal))), + seq($.keyword_cache, field("cache", alias($._integer, $.literal))), + seq(optional($.keyword_no), $.keyword_cycle), + seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference)), + ) + ), ), create_type: $ => seq( @@ -1553,16 +1553,18 @@ module.exports = grammar({ optional($._if_exists), $.object_reference, choice( - seq( - optional(seq($.keyword_as, $._type)), - optional(seq($.keyword_increment, optional($.keyword_by), $.literal)), - optional(seq($.keyword_minvalue, choice($.literal, seq($.keyword_no, $.keyword_minvalue)))), - optional(seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue)))), - optional(seq($.keyword_start, optional($.keyword_with), field("start", alias($._integer, $.literal)))), - optional(seq($.keyword_restart, optional($.keyword_with), field("restart", alias($._integer, $.literal)))), - optional(seq($.keyword_cache, field("cache", alias($._integer, $.literal)))), - optional(seq(optional($.keyword_no), $.keyword_cycle)), - optional(seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference))), + repeat1( + choice( + seq($.keyword_as, $._type), + seq($.keyword_increment, optional($.keyword_by), $.literal), + seq($.keyword_minvalue, choice($.literal, seq($.keyword_no, $.keyword_minvalue))), + seq($.keyword_maxvalue, choice($.literal, seq($.keyword_no, $.keyword_maxvalue))), + seq($.keyword_start, optional($.keyword_with), field("start", alias($._integer, $.literal))), + seq($.keyword_restart, optional($.keyword_with), field("restart", alias($._integer, $.literal))), + seq($.keyword_cache, field("cache", alias($._integer, $.literal))), + seq(optional($.keyword_no), $.keyword_cycle), + seq($.keyword_owned, $.keyword_by, choice($.keyword_none, $.object_reference)), + ), ), $.rename_object, $.change_ownership,