Skip to content

Commit 1e18b87

Browse files
Moved CreateExtension and DropExtension structs out of Statement enum
1 parent ef260ca commit 1e18b87

File tree

5 files changed

+132
-89
lines changed

5 files changed

+132
-89
lines changed

src/ast/ddl.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,3 +3663,85 @@ impl Spanned for CreateView {
36633663
Span::union_iter(spans)
36643664
}
36653665
}
3666+
3667+
/// CREATE EXTENSION statement
3668+
/// Note: this is a PostgreSQL-specific statement
3669+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3670+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3671+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3672+
pub struct CreateExtension {
3673+
pub name: Ident,
3674+
pub if_not_exists: bool,
3675+
pub cascade: bool,
3676+
pub schema: Option<Ident>,
3677+
pub version: Option<Ident>,
3678+
}
3679+
3680+
impl fmt::Display for CreateExtension {
3681+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3682+
write!(
3683+
f,
3684+
"CREATE EXTENSION {if_not_exists}{name}",
3685+
if_not_exists = if self.if_not_exists {
3686+
"IF NOT EXISTS "
3687+
} else {
3688+
""
3689+
},
3690+
name = self.name
3691+
)?;
3692+
if self.cascade || self.schema.is_some() || self.version.is_some() {
3693+
write!(f, " WITH")?;
3694+
3695+
if let Some(name) = &self.schema {
3696+
write!(f, " SCHEMA {name}")?;
3697+
}
3698+
if let Some(version) = &self.version {
3699+
write!(f, " VERSION {version}")?;
3700+
}
3701+
if self.cascade {
3702+
write!(f, " CASCADE")?;
3703+
}
3704+
}
3705+
3706+
Ok(())
3707+
}
3708+
}
3709+
3710+
impl Spanned for CreateExtension {
3711+
fn span(&self) -> Span {
3712+
Span::empty()
3713+
}
3714+
}
3715+
3716+
/// DROP EXTENSION statement
3717+
/// Note: this is a PostgreSQL-specific statement
3718+
/// https://www.postgresql.org/docs/current/sql-dropextension.html
3719+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3720+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3721+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3722+
pub struct DropExtension {
3723+
pub names: Vec<Ident>,
3724+
pub if_exists: bool,
3725+
/// `CASCADE` or `RESTRICT`
3726+
pub cascade_or_restrict: Option<ReferentialAction>,
3727+
}
3728+
3729+
impl fmt::Display for DropExtension {
3730+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3731+
write!(f, "DROP EXTENSION")?;
3732+
if self.if_exists {
3733+
write!(f, " IF EXISTS")?;
3734+
}
3735+
write!(f, " {}", display_comma_separated(&self.names))?;
3736+
if let Some(cascade_or_restrict) = &self.cascade_or_restrict {
3737+
write!(f, " {cascade_or_restrict}")?;
3738+
}
3739+
Ok(())
3740+
}
3741+
}
3742+
3743+
impl Spanned for DropExtension {
3744+
fn span(&self) -> Span {
3745+
Span::empty()
3746+
}
3747+
}

src/ast/mod.rs

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ pub use self::ddl::{
6464
AlterType, AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
6565
AlterTypeRenameValue, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
6666
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
67-
CreateFunction, CreateIndex, CreateTable, CreateTrigger, CreateView, Deduplicate,
68-
DeferrableInitial, DropBehavior, DropTrigger, GeneratedAs, GeneratedExpressionMode,
69-
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
70-
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
71-
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, RenameTableNameKind,
72-
ReplicaIdentity, TableConstraint, TagsColumnOption, Truncate,
73-
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
67+
CreateExtension, CreateFunction, CreateIndex, CreateTable, CreateTrigger, CreateView,
68+
Deduplicate, DeferrableInitial, DropBehavior, DropExtension, DropTrigger, GeneratedAs,
69+
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
70+
IdentityPropertyKind, IdentityPropertyOrder, IndexColumn, IndexOption, IndexType,
71+
KeyOrIndexDisplay, Msck, NullsDistinctOption, Owner, Partition, ProcedureParam,
72+
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TableConstraint, TagsColumnOption,
73+
Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
7474
};
7575
pub use self::dml::{Delete, Insert, Update};
7676
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -3534,25 +3534,14 @@ pub enum Statement {
35343534
/// ```
35353535
///
35363536
/// Note: this is a PostgreSQL-specific statement,
3537-
CreateExtension {
3538-
name: Ident,
3539-
if_not_exists: bool,
3540-
cascade: bool,
3541-
schema: Option<Ident>,
3542-
version: Option<Ident>,
3543-
},
3537+
CreateExtension(CreateExtension),
35443538
/// ```sql
35453539
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
35463540
///
35473541
/// Note: this is a PostgreSQL-specific statement.
35483542
/// https://www.postgresql.org/docs/current/sql-dropextension.html
35493543
/// ```
3550-
DropExtension {
3551-
names: Vec<Ident>,
3552-
if_exists: bool,
3553-
/// `CASCADE` or `RESTRICT`
3554-
cascade_or_restrict: Option<ReferentialAction>,
3555-
},
3544+
DropExtension(DropExtension),
35563545
/// ```sql
35573546
/// FETCH
35583547
/// ```
@@ -4806,49 +4795,8 @@ impl fmt::Display for Statement {
48064795
Ok(())
48074796
}
48084797
Statement::CreateIndex(create_index) => create_index.fmt(f),
4809-
Statement::CreateExtension {
4810-
name,
4811-
if_not_exists,
4812-
cascade,
4813-
schema,
4814-
version,
4815-
} => {
4816-
write!(
4817-
f,
4818-
"CREATE EXTENSION {if_not_exists}{name}",
4819-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" }
4820-
)?;
4821-
if *cascade || schema.is_some() || version.is_some() {
4822-
write!(f, " WITH")?;
4823-
4824-
if let Some(name) = schema {
4825-
write!(f, " SCHEMA {name}")?;
4826-
}
4827-
if let Some(version) = version {
4828-
write!(f, " VERSION {version}")?;
4829-
}
4830-
if *cascade {
4831-
write!(f, " CASCADE")?;
4832-
}
4833-
}
4834-
4835-
Ok(())
4836-
}
4837-
Statement::DropExtension {
4838-
names,
4839-
if_exists,
4840-
cascade_or_restrict,
4841-
} => {
4842-
write!(f, "DROP EXTENSION")?;
4843-
if *if_exists {
4844-
write!(f, " IF EXISTS")?;
4845-
}
4846-
write!(f, " {}", display_comma_separated(names))?;
4847-
if let Some(cascade_or_restrict) = cascade_or_restrict {
4848-
write!(f, " {cascade_or_restrict}")?;
4849-
}
4850-
Ok(())
4851-
}
4798+
Statement::CreateExtension(create_extension) => write!(f, "{create_extension}"),
4799+
Statement::DropExtension(drop_extension) => write!(f, "{drop_extension}"),
48524800
Statement::CreateRole(create_role) => write!(f, "{create_role}"),
48534801
Statement::CreateSecret {
48544802
or_replace,
@@ -10622,6 +10570,18 @@ impl From<CreateRole> for Statement {
1062210570
}
1062310571
}
1062410572

10573+
impl From<CreateExtension> for Statement {
10574+
fn from(ce: CreateExtension) -> Self {
10575+
Self::CreateExtension(ce)
10576+
}
10577+
}
10578+
10579+
impl From<DropExtension> for Statement {
10580+
fn from(de: DropExtension) -> Self {
10581+
Self::DropExtension(de)
10582+
}
10583+
}
10584+
1062510585
impl From<CaseStatement> for Statement {
1062610586
fn from(c: CaseStatement) -> Self {
1062710587
Self::Case(c)

src/ast/spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ impl Spanned for Statement {
362362
),
363363
Statement::CreateIndex(create_index) => create_index.span(),
364364
Statement::CreateRole(create_role) => create_role.span(),
365+
Statement::CreateExtension(create_extension) => create_extension.span(),
366+
Statement::DropExtension(drop_extension) => drop_extension.span(),
365367
Statement::CreateSecret { .. } => Span::empty(),
366368
Statement::CreateServer { .. } => Span::empty(),
367369
Statement::CreateConnector { .. } => Span::empty(),
@@ -405,8 +407,6 @@ impl Spanned for Statement {
405407
Statement::DropProcedure { .. } => Span::empty(),
406408
Statement::DropSecret { .. } => Span::empty(),
407409
Statement::Declare { .. } => Span::empty(),
408-
Statement::CreateExtension { .. } => Span::empty(),
409-
Statement::DropExtension { .. } => Span::empty(),
410410
Statement::Fetch { .. } => Span::empty(),
411411
Statement::Flush { .. } => Span::empty(),
412412
Statement::Discard { .. } => Span::empty(),

src/parser/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7180,13 +7180,14 @@ impl<'a> Parser<'a> {
71807180
(None, None, false)
71817181
};
71827182

7183-
Ok(Statement::CreateExtension {
7183+
Ok(CreateExtension {
71847184
name,
71857185
if_not_exists,
71867186
schema,
71877187
version,
71887188
cascade,
7189-
})
7189+
}
7190+
.into())
71907191
}
71917192

71927193
/// Parse a PostgreSQL-specific [Statement::DropExtension] statement.
@@ -7195,7 +7196,7 @@ impl<'a> Parser<'a> {
71957196
let names = self.parse_comma_separated(|p| p.parse_identifier())?;
71967197
let cascade_or_restrict =
71977198
self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]);
7198-
Ok(Statement::DropExtension {
7199+
Ok(Statement::DropExtension(DropExtension {
71997200
names,
72007201
if_exists,
72017202
cascade_or_restrict: cascade_or_restrict
@@ -7205,7 +7206,7 @@ impl<'a> Parser<'a> {
72057206
_ => self.expected("CASCADE or RESTRICT", self.peek_token()),
72067207
})
72077208
.transpose()?,
7208-
})
7209+
}))
72097210
}
72107211

72117212
//TODO: Implement parsing for Skewed

tests/sqlparser_postgres.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -673,93 +673,93 @@ fn parse_create_extension() {
673673
fn parse_drop_extension() {
674674
assert_eq!(
675675
pg_and_generic().verified_stmt("DROP EXTENSION extension_name"),
676-
Statement::DropExtension {
676+
Statement::DropExtension(DropExtension {
677677
names: vec!["extension_name".into()],
678678
if_exists: false,
679679
cascade_or_restrict: None,
680-
}
680+
})
681681
);
682682
assert_eq!(
683683
pg_and_generic().verified_stmt("DROP EXTENSION extension_name CASCADE"),
684-
Statement::DropExtension {
684+
Statement::DropExtension(DropExtension {
685685
names: vec!["extension_name".into()],
686686
if_exists: false,
687687
cascade_or_restrict: Some(ReferentialAction::Cascade),
688-
}
688+
})
689689
);
690690

691691
assert_eq!(
692692
pg_and_generic().verified_stmt("DROP EXTENSION extension_name RESTRICT"),
693-
Statement::DropExtension {
693+
Statement::DropExtension(DropExtension {
694694
names: vec!["extension_name".into()],
695695
if_exists: false,
696696
cascade_or_restrict: Some(ReferentialAction::Restrict),
697-
}
697+
})
698698
);
699699

700700
assert_eq!(
701701
pg_and_generic().verified_stmt("DROP EXTENSION extension_name, extension_name2 CASCADE"),
702-
Statement::DropExtension {
702+
Statement::DropExtension(DropExtension {
703703
names: vec!["extension_name".into(), "extension_name2".into()],
704704
if_exists: false,
705705
cascade_or_restrict: Some(ReferentialAction::Cascade),
706-
}
706+
})
707707
);
708708

709709
assert_eq!(
710710
pg_and_generic().verified_stmt("DROP EXTENSION extension_name, extension_name2 RESTRICT"),
711-
Statement::DropExtension {
711+
Statement::DropExtension(DropExtension {
712712
names: vec!["extension_name".into(), "extension_name2".into()],
713713
if_exists: false,
714714
cascade_or_restrict: Some(ReferentialAction::Restrict),
715-
}
715+
})
716716
);
717717

718718
assert_eq!(
719719
pg_and_generic().verified_stmt("DROP EXTENSION IF EXISTS extension_name"),
720-
Statement::DropExtension {
720+
Statement::DropExtension(DropExtension {
721721
names: vec!["extension_name".into()],
722722
if_exists: true,
723723
cascade_or_restrict: None,
724-
}
724+
})
725725
);
726726

727727
assert_eq!(
728728
pg_and_generic().verified_stmt("DROP EXTENSION IF EXISTS extension_name CASCADE"),
729-
Statement::DropExtension {
729+
Statement::DropExtension(DropExtension {
730730
names: vec!["extension_name".into()],
731731
if_exists: true,
732732
cascade_or_restrict: Some(ReferentialAction::Cascade),
733-
}
733+
})
734734
);
735735

736736
assert_eq!(
737737
pg_and_generic().verified_stmt("DROP EXTENSION IF EXISTS extension_name RESTRICT"),
738-
Statement::DropExtension {
738+
Statement::DropExtension(DropExtension {
739739
names: vec!["extension_name".into()],
740740
if_exists: true,
741741
cascade_or_restrict: Some(ReferentialAction::Restrict),
742-
}
742+
})
743743
);
744744

745745
assert_eq!(
746746
pg_and_generic()
747747
.verified_stmt("DROP EXTENSION IF EXISTS extension_name1, extension_name2 CASCADE"),
748-
Statement::DropExtension {
748+
Statement::DropExtension(DropExtension {
749749
names: vec!["extension_name1".into(), "extension_name2".into()],
750750
if_exists: true,
751751
cascade_or_restrict: Some(ReferentialAction::Cascade),
752-
}
752+
})
753753
);
754754

755755
assert_eq!(
756756
pg_and_generic()
757757
.verified_stmt("DROP EXTENSION IF EXISTS extension_name1, extension_name2 RESTRICT"),
758-
Statement::DropExtension {
758+
Statement::DropExtension(DropExtension {
759759
names: vec!["extension_name1".into(), "extension_name2".into()],
760760
if_exists: true,
761761
cascade_or_restrict: Some(ReferentialAction::Restrict),
762-
}
762+
})
763763
);
764764
}
765765

0 commit comments

Comments
 (0)