Skip to content

Commit 04d8502

Browse files
committed
Adds support for pg DROP EXTENSION
``` DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ] ``` https://www.postgresql.org/docs/current/sql-dropextension.html
1 parent 8fcdf48 commit 04d8502

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

src/ast/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,6 +2759,15 @@ pub enum Statement {
27592759
version: Option<Ident>,
27602760
},
27612761
/// ```sql
2762+
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
2763+
/// ```
2764+
DropExtension {
2765+
names: Vec<Ident>,
2766+
if_exists: bool,
2767+
/// `CASCADE` or `RESTRICT`
2768+
cascade_or_restrict: Option<ReferentialAction>,
2769+
},
2770+
/// ```sql
27622771
/// FETCH
27632772
/// ```
27642773
/// Retrieve rows from a query using a cursor
@@ -4025,6 +4034,21 @@ impl fmt::Display for Statement {
40254034

40264035
Ok(())
40274036
}
4037+
Statement::DropExtension {
4038+
names,
4039+
if_exists,
4040+
cascade_or_restrict,
4041+
} => {
4042+
write!(f, "DROP EXTENSION")?;
4043+
if *if_exists {
4044+
write!(f, " IF EXISTS")?;
4045+
}
4046+
write!(f, " {}", display_comma_separated(names))?;
4047+
if let Some(cascade_or_restrict) = cascade_or_restrict {
4048+
write!(f, " {cascade_or_restrict}")?;
4049+
}
4050+
Ok(())
4051+
}
40284052
Statement::CreateRole {
40294053
names,
40304054
if_not_exists,

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ impl Spanned for Statement {
429429
Statement::DropSecret { .. } => Span::empty(),
430430
Statement::Declare { .. } => Span::empty(),
431431
Statement::CreateExtension { .. } => Span::empty(),
432+
Statement::DropExtension { .. } => Span::empty(),
432433
Statement::Fetch { .. } => Span::empty(),
433434
Statement::Flush { .. } => Span::empty(),
434435
Statement::Discard { .. } => Span::empty(),

src/parser/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5196,9 +5196,11 @@ impl<'a> Parser<'a> {
51965196
return self.parse_drop_secret(temporary, persistent);
51975197
} else if self.parse_keyword(Keyword::TRIGGER) {
51985198
return self.parse_drop_trigger();
5199+
} else if self.parse_keyword(Keyword::EXTENSION) {
5200+
return self.parse_drop_extension();
51995201
} else {
52005202
return self.expected(
5201-
"TABLE, VIEW, INDEX, ROLE, SCHEMA, DATABASE, FUNCTION, PROCEDURE, STAGE, TRIGGER, SECRET, SEQUENCE, or TYPE after DROP",
5203+
"TABLE, VIEW, INDEX, ROLE, SCHEMA, DATABASE, FUNCTION, PROCEDURE, STAGE, TRIGGER, SECRET, SEQUENCE, TYPE, or EXTENSION after DROP",
52025204
self.peek_token(),
52035205
);
52045206
};
@@ -5861,6 +5863,22 @@ impl<'a> Parser<'a> {
58615863
})
58625864
}
58635865

5866+
pub fn parse_drop_extension(&mut self) -> Result<Statement, ParserError> {
5867+
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5868+
let names = self.parse_comma_separated(|p| p.parse_identifier(false))?;
5869+
let cascade_or_restrict =
5870+
self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]);
5871+
Ok(Statement::DropExtension {
5872+
names,
5873+
if_exists,
5874+
cascade_or_restrict: cascade_or_restrict.map(|k| match k {
5875+
Keyword::CASCADE => ReferentialAction::Cascade,
5876+
Keyword::RESTRICT => ReferentialAction::Restrict,
5877+
_ => unreachable!(),
5878+
}),
5879+
})
5880+
}
5881+
58645882
//TODO: Implement parsing for Skewed
58655883
pub fn parse_hive_distribution(&mut self) -> Result<HiveDistributionStyle, ParserError> {
58665884
if self.parse_keywords(&[Keyword::PARTITIONED, Keyword::BY]) {

tests/sqlparser_postgres.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,22 @@ fn parse_create_extension() {
662662
.verified_stmt("CREATE EXTENSION extension_name WITH SCHEMA schema_name VERSION version");
663663
}
664664

665+
#[test]
666+
fn parse_drop_extension() {
667+
pg_and_generic().verified_stmt("DROP EXTENSION extension_name");
668+
pg_and_generic().verified_stmt("DROP EXTENSION extension_name CASCADE");
669+
pg_and_generic().verified_stmt("DROP EXTENSION extension_name RESTRICT");
670+
pg_and_generic().verified_stmt("DROP EXTENSION extension_name, extension_name2 CASCADE");
671+
pg_and_generic().verified_stmt("DROP EXTENSION extension_name, extension_name2 RESTRICT");
672+
673+
pg_and_generic().verified_stmt("DROP EXTENSION IF EXISTS extension_name");
674+
pg_and_generic().verified_stmt("DROP EXTENSION IF EXISTS extension_name CASCADE");
675+
pg_and_generic()
676+
.verified_stmt("DROP EXTENSION IF EXISTS extension_name1, extension_name2 CASCADE");
677+
pg_and_generic()
678+
.verified_stmt("DROP EXTENSION IF EXISTS extension_name1, extension_name2 RESTRICT");
679+
}
680+
665681
#[test]
666682
fn parse_alter_table_alter_column() {
667683
pg().one_statement_parses_to(

0 commit comments

Comments
 (0)