Skip to content

Commit 20d1c9d

Browse files
Moved DropFunction struct out of Statement enum
1 parent 8f70b00 commit 20d1c9d

File tree

5 files changed

+59
-41
lines changed

5 files changed

+59
-41
lines changed

src/ast/ddl.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ use crate::ast::{
3333
display_comma_separated, display_separated, ArgMode, AttachedToken, CommentDef,
3434
ConditionalStatements, CreateFunctionBody, CreateFunctionUsing, CreateTableLikeKind,
3535
CreateTableOptions, CreateViewParams, DataType, Expr, FileFormat, FunctionBehavior,
36-
FunctionCalledOnNull, FunctionDeterminismSpecifier, FunctionParallel, HiveDistributionStyle,
37-
HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident, InitializeKind,
38-
MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens, OperateFunctionArg,
39-
OrderByExpr, ProjectionSelect, Query, RefreshModeKind, RowAccessPolicy, SequenceOptions,
40-
Spanned, SqlOption, StorageSerializationPolicy, TableVersion, Tag, TriggerEvent,
41-
TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value, ValueWithSpan,
42-
WrappedCollection,
36+
FunctionCalledOnNull, FunctionDesc, FunctionDeterminismSpecifier, FunctionParallel,
37+
HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident,
38+
InitializeKind, MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens,
39+
OperateFunctionArg, OrderByExpr, ProjectionSelect, Query, RefreshModeKind, RowAccessPolicy,
40+
SequenceOptions, Spanned, SqlOption, StorageSerializationPolicy, TableVersion, Tag,
41+
TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value,
42+
ValueWithSpan, WrappedCollection,
4343
};
4444
use crate::display_utils::{DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
4545
use crate::keywords::Keyword;
@@ -3799,3 +3799,36 @@ impl fmt::Display for AlterTable {
37993799
Ok(())
38003800
}
38013801
}
3802+
3803+
/// DROP FUNCTION statement
3804+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3805+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3806+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3807+
pub struct DropFunction {
3808+
pub if_exists: bool,
3809+
/// One or more functions to drop
3810+
pub func_desc: Vec<FunctionDesc>,
3811+
/// `CASCADE` or `RESTRICT`
3812+
pub drop_behavior: Option<DropBehavior>,
3813+
}
3814+
3815+
impl fmt::Display for DropFunction {
3816+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3817+
write!(
3818+
f,
3819+
"DROP FUNCTION{} {}",
3820+
if self.if_exists { " IF EXISTS" } else { "" },
3821+
display_comma_separated(&self.func_desc),
3822+
)?;
3823+
if let Some(op) = &self.drop_behavior {
3824+
write!(f, " {op}")?;
3825+
}
3826+
Ok(())
3827+
}
3828+
}
3829+
3830+
impl Spanned for DropFunction {
3831+
fn span(&self) -> Span {
3832+
Span::empty()
3833+
}
3834+
}

src/ast/mod.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub use self::ddl::{
6666
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
6767
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
6868
CreateIndex, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
69-
DropBehavior, DropExtension, DropTrigger, GeneratedAs, GeneratedExpressionMode,
69+
DropBehavior, DropExtension, DropFunction, DropTrigger, GeneratedAs, GeneratedExpressionMode,
7070
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
7171
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
7272
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, RenameTableNameKind,
@@ -3448,13 +3448,7 @@ pub enum Statement {
34483448
/// ```sql
34493449
/// DROP FUNCTION
34503450
/// ```
3451-
DropFunction {
3452-
if_exists: bool,
3453-
/// One or more function to drop
3454-
func_desc: Vec<FunctionDesc>,
3455-
/// `CASCADE` or `RESTRICT`
3456-
drop_behavior: Option<DropBehavior>,
3457-
},
3451+
DropFunction(DropFunction),
34583452
/// ```sql
34593453
/// DROP DOMAIN
34603454
/// ```
@@ -3523,7 +3517,7 @@ pub enum Statement {
35233517
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
35243518
/// ```
35253519
/// Note: this is a PostgreSQL-specific statement.
3526-
/// https://www.postgresql.org/docs/current/sql-dropextension.html
3520+
/// <https://www.postgresql.org/docs/current/sql-dropextension.html>
35273521
DropExtension(DropExtension),
35283522
/// ```sql
35293523
/// FETCH
@@ -4965,22 +4959,7 @@ impl fmt::Display for Statement {
49654959
};
49664960
Ok(())
49674961
}
4968-
Statement::DropFunction {
4969-
if_exists,
4970-
func_desc,
4971-
drop_behavior,
4972-
} => {
4973-
write!(
4974-
f,
4975-
"DROP FUNCTION{} {}",
4976-
if *if_exists { " IF EXISTS" } else { "" },
4977-
display_comma_separated(func_desc),
4978-
)?;
4979-
if let Some(op) = drop_behavior {
4980-
write!(f, " {op}")?;
4981-
}
4982-
Ok(())
4983-
}
4962+
Statement::DropFunction(drop_function) => write!(f, "{drop_function}"),
49844963
Statement::DropDomain(DropDomain {
49854964
if_exists,
49864965
name,
@@ -10524,6 +10503,12 @@ impl From<AlterTable> for Statement {
1052410503
}
1052510504
}
1052610505

10506+
impl From<DropFunction> for Statement {
10507+
fn from(df: DropFunction) -> Self {
10508+
Self::DropFunction(df)
10509+
}
10510+
}
10511+
1052710512
impl From<CreateExtension> for Statement {
1052810513
fn from(ce: CreateExtension) -> Self {
1052910514
Self::CreateExtension(ce)

src/ast/spans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl Spanned for Statement {
388388
Statement::AttachDuckDBDatabase { .. } => Span::empty(),
389389
Statement::DetachDuckDBDatabase { .. } => Span::empty(),
390390
Statement::Drop { .. } => Span::empty(),
391-
Statement::DropFunction { .. } => Span::empty(),
391+
Statement::DropFunction(drop_function) => drop_function.span(),
392392
Statement::DropDomain { .. } => Span::empty(),
393393
Statement::DropProcedure { .. } => Span::empty(),
394394
Statement::DropSecret { .. } => Span::empty(),

src/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6508,11 +6508,11 @@ impl<'a> Parser<'a> {
65086508
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
65096509
let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
65106510
let drop_behavior = self.parse_optional_drop_behavior();
6511-
Ok(Statement::DropFunction {
6511+
Ok(Statement::DropFunction(DropFunction {
65126512
if_exists,
65136513
func_desc,
65146514
drop_behavior,
6515-
})
6515+
}))
65166516
}
65176517

65186518
/// ```sql

tests/sqlparser_postgres.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,7 +4488,7 @@ fn parse_drop_function() {
44884488
let sql = "DROP FUNCTION IF EXISTS test_func";
44894489
assert_eq!(
44904490
pg().verified_stmt(sql),
4491-
Statement::DropFunction {
4491+
Statement::DropFunction(DropFunction {
44924492
if_exists: true,
44934493
func_desc: vec![FunctionDesc {
44944494
name: ObjectName::from(vec![Ident {
@@ -4499,13 +4499,13 @@ fn parse_drop_function() {
44994499
args: None
45004500
}],
45014501
drop_behavior: None
4502-
}
4502+
})
45034503
);
45044504

45054505
let sql = "DROP FUNCTION IF EXISTS test_func(a INTEGER, IN b INTEGER = 1)";
45064506
assert_eq!(
45074507
pg().verified_stmt(sql),
4508-
Statement::DropFunction {
4508+
Statement::DropFunction(DropFunction {
45094509
if_exists: true,
45104510
func_desc: vec![FunctionDesc {
45114511
name: ObjectName::from(vec![Ident {
@@ -4526,13 +4526,13 @@ fn parse_drop_function() {
45264526
]),
45274527
}],
45284528
drop_behavior: None
4529-
}
4529+
})
45304530
);
45314531

45324532
let sql = "DROP FUNCTION IF EXISTS test_func1(a INTEGER, IN b INTEGER = 1), test_func2(a VARCHAR, IN b INTEGER = 1)";
45334533
assert_eq!(
45344534
pg().verified_stmt(sql),
4535-
Statement::DropFunction {
4535+
Statement::DropFunction(DropFunction {
45364536
if_exists: true,
45374537
func_desc: vec![
45384538
FunctionDesc {
@@ -4573,7 +4573,7 @@ fn parse_drop_function() {
45734573
}
45744574
],
45754575
drop_behavior: None
4576-
}
4576+
})
45774577
);
45784578
}
45794579

0 commit comments

Comments
 (0)