Skip to content

Commit

Permalink
feat: add CAD database, Alter index, CAD USER/ROLE/GROUP
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-Q committed Jul 31, 2023
1 parent 5c92e8a commit ce4cadc
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 5 deletions.
174 changes: 169 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -1119,11 +1199,7 @@ module.exports = grammar({
)
),
paren_list(
seq(
field('name', $.identifier),
'=',
field('value', choice($.identifier,alias($._single_quote_string, $.literal))),
),
$._with_settings
),
),
),
Expand All @@ -1141,6 +1217,9 @@ module.exports = grammar({
$.alter_view,
$.alter_schema,
$.alter_type,
$.alter_index,
$.alter_database,
$.alter_role,
),
),

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1413,6 +1555,8 @@ module.exports = grammar({
$.drop_index,
$.drop_type,
$.drop_schema,
$.drop_database,
$.drop_role,
),
),

Expand Down Expand Up @@ -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,
Expand Down
56 changes: 56 additions & 0 deletions test/corpus/alter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;

--------------------------------------------------------------------------------
75 changes: 75 additions & 0 deletions test/corpus/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Loading

0 comments on commit ce4cadc

Please sign in to comment.