Skip to content

Commit

Permalink
Grammar: adds support for CREATE/ALTER/DROP DATABASE for Postgres dia…
Browse files Browse the repository at this point in the history
…lect (#2081)
  • Loading branch information
derickl committed Dec 10, 2021
1 parent cd8aceb commit a0f82f7
Show file tree
Hide file tree
Showing 8 changed files with 1,083 additions and 0 deletions.
162 changes: 162 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -1555,6 +1555,166 @@ class DropMaterializedViewStatementSegment(BaseSegment):
)


@postgres_dialect.segment(replace=True)
class CreateDatabaseStatementSegment(BaseSegment):
"""A `CREATE DATABASE` statement.
As specified in https://www.postgresql.org/docs/14/sql-createdatabase.html
"""

type = "create_database_statement"

match_grammar = StartsWith(Sequence("CREATE", "DATABASE"))

parse_grammar = Sequence(
"CREATE",
"DATABASE",
Ref("DatabaseReferenceSegment"),
Ref.keyword("WITH", optional=True),
AnyNumberOf(
Sequence(
"OWNER",
Ref("EqualsSegment", optional=True),
Ref("ObjectReferenceSegment"),
),
Sequence(
"TEMPLATE",
Ref("EqualsSegment", optional=True),
Ref("ObjectReferenceSegment"),
),
Sequence(
"ENCODING",
Ref("EqualsSegment", optional=True),
OneOf(Ref("QuotedLiteralSegment"), "DEFAULT"),
),
OneOf(
# LOCALE This is a shortcut for setting LC_COLLATE and LC_CTYPE at once.
# If you specify this, you cannot specify either of those parameters.
Sequence(
"LOCALE",
Ref("EqualsSegment", optional=True),
Ref("QuotedLiteralSegment"),
),
AnyNumberOf(
Sequence(
"LC_COLLATE",
Ref("EqualsSegment", optional=True),
Ref("QuotedLiteralSegment"),
),
Sequence(
"LC_CTYPE",
Ref("EqualsSegment", optional=True),
Ref("QuotedLiteralSegment"),
),
),
),
Sequence(
"TABLESPACE",
Ref("EqualsSegment", optional=True),
Ref("ParameterNameSegment"),
),
Sequence(
"ALLOW_CONNECTIONS",
Ref("EqualsSegment", optional=True),
Ref("BooleanLiteralGrammar"),
),
Sequence(
"CONNECTION",
"LIMIT",
Ref("EqualsSegment", optional=True),
Ref("NumericLiteralSegment"),
),
Sequence(
"IS_TEMPLATE",
Ref("EqualsSegment", optional=True),
Ref("BooleanLiteralGrammar"),
),
),
)


@postgres_dialect.segment()
class AlterDatabaseStatementSegment(BaseSegment):
"""A `ALTER DATABASE` statement.
As specified in https://www.postgresql.org/docs/14/sql-alterdatabase.html
"""

type = "alter_database_statement"

match_grammar = StartsWith(Sequence("ALTER", "DATABASE"))

parse_grammar = Sequence(
"ALTER",
"DATABASE",
Ref("DatabaseReferenceSegment"),
OneOf(
Sequence(
Ref.keyword("WITH", optional=True),
AnyNumberOf(
Sequence("ALLOW_CONNECTIONS", Ref("BooleanLiteralGrammar")),
Sequence(
"CONNECTION",
"LIMIT",
Ref("NumericLiteralSegment"),
),
Sequence("IS_TEMPLATE", Ref("BooleanLiteralGrammar")),
min_times=1,
),
),
Sequence("RENAME", "TO", Ref("DatabaseReferenceSegment")),
Sequence(
"OWNER",
"TO",
OneOf(
Ref("ObjectReferenceSegment"),
"CURRENT_ROLE",
"CURRENT_USER",
"SESSION_USER",
),
),
Sequence("SET", "TABLESPACE", Ref("ParameterNameSegment")),
Sequence(
"SET",
Ref("ParameterNameSegment"),
OneOf(
Sequence(
OneOf("TO", Ref("EqualsSegment")),
OneOf("DEFAULT", Ref("LiteralGrammar")),
),
Sequence("FROM", "CURRENT"),
),
),
Sequence("RESET", OneOf("ALL", Ref("ParameterNameSegment"))),
optional=True,
),
)


@postgres_dialect.segment()
class DropDatabaseStatementSegment(BaseSegment):
"""A `DROP DATABASE` statement.
As specified in https://www.postgresql.org/docs/14/sql-dropdatabase.html
"""

type = "drop_database_statement"

match_grammar = StartsWith(Sequence("DROP", "DATABASE"))

parse_grammar = Sequence(
"DROP",
"DATABASE",
Sequence("IF", "EXISTS", optional=True),
Ref("DatabaseReferenceSegment"),
Sequence(
Ref.keyword("WITH", optional=True),
Bracketed("FORCE"),
optional=True,
),
)


@postgres_dialect.segment()
class LikeOptionSegment(BaseSegment):
"""Like Option Segment.
Expand Down Expand Up @@ -2414,6 +2574,8 @@ class StatementSegment(BaseSegment):
Ref("AlterMaterializedViewStatementSegment"),
Ref("DropMaterializedViewStatementSegment"),
Ref("RefreshMaterializedViewStatementSegment"),
Ref("AlterDatabaseStatementSegment"),
Ref("DropDatabaseStatementSegment"),
],
)

Expand Down
5 changes: 5 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres_keywords.py
Expand Up @@ -879,6 +879,7 @@ def get_keywords(keyword_list, keyword_type):
]

postgres_nondocs_keywords = [
("ALLOW_CONNECTIONS", "non-reserved"),
("BUFFERS", "non-reserved"),
("CONNECT", "reserved"),
("COSTS", "non-reserved"),
Expand All @@ -890,8 +891,12 @@ def get_keywords(keyword_list, keyword_type):
("FORMAT", "non-reserved"),
("HASH", "non-reserved"),
("IGNORE", "non-reserved"),
("IS_TEMPLATE", "non-reserved"),
("JSON", "non-reserved"),
("LC_COLLATE", "non-reserved"),
("LC_CTYPE", "non-reserved"),
("LIST", "non-reserved"),
("LOCALE", "non-reserved"),
("MAIN", "non-reserved"),
("MODULUS", "non-reserved"),
("PLAIN", "non-reserved"),
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/dialects/postgres/postgres_alter_database.sql
@@ -0,0 +1,31 @@
ALTER DATABASE db;
ALTER DATABASE db ALLOW_CONNECTIONS true;
ALTER DATABASE db WITH ALLOW_CONNECTIONS true;
ALTER DATABASE db CONNECTION LIMIT 10;
ALTER DATABASE db WITH CONNECTION LIMIT 10;
ALTER DATABASE db IS_TEMPLATE true;
ALTER DATABASE db WITH IS_TEMPLATE true;
ALTER DATABASE db IS_TEMPLATE true ALLOW_CONNECTIONS true;
ALTER DATABASE db WITH IS_TEMPLATE true ALLOW_CONNECTIONS true;
ALTER DATABASE db CONNECTION LIMIT 10 IS_TEMPLATE true ALLOW_CONNECTIONS true;
ALTER DATABASE db WITH CONNECTION LIMIT 10 IS_TEMPLATE true ALLOW_CONNECTIONS true;

ALTER DATABASE db RENAME TO new_db;
ALTER DATABASE db OWNER TO other_role;
ALTER DATABASE db OWNER TO CURRENT_ROLE;
ALTER DATABASE db OWNER TO CURRENT_USER;
ALTER DATABASE db OWNER TO SESSION_USER;

-- Issue:2017
ALTER DATABASE postgres SET password_encryption TO 'scram-sha-256';
ALTER DATABASE db SET TABLESPACE new_tablespace;
ALTER DATABASE db SET parameter1 TO 1;
ALTER DATABASE db SET parameter1 TO 'some_value';
ALTER DATABASE db SET parameter1 TO DEFAULT;
ALTER DATABASE db SET parameter1 = 1;
ALTER DATABASE db SET parameter1 = 'some_value';
ALTER DATABASE db SET parameter1 = DEFAULT;
ALTER DATABASE db SET parameter1 FROM CURRENT;

ALTER DATABASE db RESET parameter1;
ALTER DATABASE db RESET ALL;

0 comments on commit a0f82f7

Please sign in to comment.