Skip to content

Commit 20e60cc

Browse files
stepanchegayman-sigma
authored andcommitted
Replace ReferentialAction enum in DROP statements (apache#1648)
1 parent 8d924c9 commit 20e60cc

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

src/ast/ddl.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,26 @@ impl fmt::Display for ReferentialAction {
17861786
}
17871787
}
17881788

1789+
/// `<drop behavior> ::= CASCADE | RESTRICT`.
1790+
///
1791+
/// Used in `DROP` statements.
1792+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1793+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1794+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1795+
pub enum DropBehavior {
1796+
Restrict,
1797+
Cascade,
1798+
}
1799+
1800+
impl fmt::Display for DropBehavior {
1801+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1802+
f.write_str(match self {
1803+
DropBehavior::Restrict => "RESTRICT",
1804+
DropBehavior::Cascade => "CASCADE",
1805+
})
1806+
}
1807+
}
1808+
17891809
/// SQL user defined type definition
17901810
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
17911811
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/ast/mod.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ pub use self::dcl::{
4949
pub use self::ddl::{
5050
AlterColumnOperation, AlterIndexOperation, AlterPolicyOperation, AlterTableOperation,
5151
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty,
52-
ConstraintCharacteristics, CreateFunction, Deduplicate, DeferrableInitial, GeneratedAs,
53-
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
54-
IdentityPropertyKind, IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay,
55-
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, TableConstraint,
56-
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
57-
ViewColumnDef,
52+
ConstraintCharacteristics, CreateFunction, Deduplicate, DeferrableInitial, DropBehavior,
53+
GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
54+
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexOption,
55+
IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner, Partition, ProcedureParam,
56+
ReferentialAction, TableConstraint, TagsColumnOption, UserDefinedTypeCompositeAttributeDef,
57+
UserDefinedTypeRepresentation, ViewColumnDef,
5858
};
5959
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
6060
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -2718,7 +2718,7 @@ pub enum Statement {
27182718
/// One or more function to drop
27192719
func_desc: Vec<FunctionDesc>,
27202720
/// `CASCADE` or `RESTRICT`
2721-
option: Option<ReferentialAction>,
2721+
drop_behavior: Option<DropBehavior>,
27222722
},
27232723
/// ```sql
27242724
/// DROP PROCEDURE
@@ -2728,7 +2728,7 @@ pub enum Statement {
27282728
/// One or more function to drop
27292729
proc_desc: Vec<FunctionDesc>,
27302730
/// `CASCADE` or `RESTRICT`
2731-
option: Option<ReferentialAction>,
2731+
drop_behavior: Option<DropBehavior>,
27322732
},
27332733
/// ```sql
27342734
/// DROP SECRET
@@ -2747,7 +2747,7 @@ pub enum Statement {
27472747
if_exists: bool,
27482748
name: Ident,
27492749
table_name: ObjectName,
2750-
option: Option<ReferentialAction>,
2750+
drop_behavior: Option<DropBehavior>,
27512751
},
27522752
/// ```sql
27532753
/// DECLARE
@@ -4335,31 +4335,31 @@ impl fmt::Display for Statement {
43354335
Statement::DropFunction {
43364336
if_exists,
43374337
func_desc,
4338-
option,
4338+
drop_behavior,
43394339
} => {
43404340
write!(
43414341
f,
43424342
"DROP FUNCTION{} {}",
43434343
if *if_exists { " IF EXISTS" } else { "" },
43444344
display_comma_separated(func_desc),
43454345
)?;
4346-
if let Some(op) = option {
4346+
if let Some(op) = drop_behavior {
43474347
write!(f, " {op}")?;
43484348
}
43494349
Ok(())
43504350
}
43514351
Statement::DropProcedure {
43524352
if_exists,
43534353
proc_desc,
4354-
option,
4354+
drop_behavior,
43554355
} => {
43564356
write!(
43574357
f,
43584358
"DROP PROCEDURE{} {}",
43594359
if *if_exists { " IF EXISTS" } else { "" },
43604360
display_comma_separated(proc_desc),
43614361
)?;
4362-
if let Some(op) = option {
4362+
if let Some(op) = drop_behavior {
43634363
write!(f, " {op}")?;
43644364
}
43654365
Ok(())
@@ -4388,15 +4388,15 @@ impl fmt::Display for Statement {
43884388
if_exists,
43894389
name,
43904390
table_name,
4391-
option,
4391+
drop_behavior,
43924392
} => {
43934393
write!(f, "DROP POLICY")?;
43944394
if *if_exists {
43954395
write!(f, " IF EXISTS")?;
43964396
}
43974397
write!(f, " {name} ON {table_name}")?;
4398-
if let Some(option) = option {
4399-
write!(f, " {option}")?;
4398+
if let Some(drop_behavior) = drop_behavior {
4399+
write!(f, " {drop_behavior}")?;
44004400
}
44014401
Ok(())
44024402
}

src/parser/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -5524,10 +5524,10 @@ impl<'a> Parser<'a> {
55245524
})
55255525
}
55265526

5527-
fn parse_optional_referential_action(&mut self) -> Option<ReferentialAction> {
5527+
fn parse_optional_drop_behavior(&mut self) -> Option<DropBehavior> {
55285528
match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
5529-
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
5530-
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
5529+
Some(Keyword::CASCADE) => Some(DropBehavior::Cascade),
5530+
Some(Keyword::RESTRICT) => Some(DropBehavior::Restrict),
55315531
_ => None,
55325532
}
55335533
}
@@ -5539,11 +5539,11 @@ impl<'a> Parser<'a> {
55395539
fn parse_drop_function(&mut self) -> Result<Statement, ParserError> {
55405540
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
55415541
let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
5542-
let option = self.parse_optional_referential_action();
5542+
let drop_behavior = self.parse_optional_drop_behavior();
55435543
Ok(Statement::DropFunction {
55445544
if_exists,
55455545
func_desc,
5546-
option,
5546+
drop_behavior,
55475547
})
55485548
}
55495549

@@ -5557,12 +5557,12 @@ impl<'a> Parser<'a> {
55575557
let name = self.parse_identifier()?;
55585558
self.expect_keyword_is(Keyword::ON)?;
55595559
let table_name = self.parse_object_name(false)?;
5560-
let option = self.parse_optional_referential_action();
5560+
let drop_behavior = self.parse_optional_drop_behavior();
55615561
Ok(Statement::DropPolicy {
55625562
if_exists,
55635563
name,
55645564
table_name,
5565-
option,
5565+
drop_behavior,
55665566
})
55675567
}
55685568

@@ -5573,11 +5573,11 @@ impl<'a> Parser<'a> {
55735573
fn parse_drop_procedure(&mut self) -> Result<Statement, ParserError> {
55745574
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
55755575
let proc_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
5576-
let option = self.parse_optional_referential_action();
5576+
let drop_behavior = self.parse_optional_drop_behavior();
55775577
Ok(Statement::DropProcedure {
55785578
if_exists,
55795579
proc_desc,
5580-
option,
5580+
drop_behavior,
55815581
})
55825582
}
55835583

tests/sqlparser_common.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11763,12 +11763,12 @@ fn test_drop_policy() {
1176311763
if_exists,
1176411764
name,
1176511765
table_name,
11766-
option,
11766+
drop_behavior,
1176711767
} => {
1176811768
assert_eq!(if_exists, true);
1176911769
assert_eq!(name.to_string(), "my_policy");
1177011770
assert_eq!(table_name.to_string(), "my_table");
11771-
assert_eq!(option, Some(ReferentialAction::Restrict));
11771+
assert_eq!(drop_behavior, Some(DropBehavior::Restrict));
1177211772
}
1177311773
_ => unreachable!(),
1177411774
}

tests/sqlparser_postgres.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3805,7 +3805,7 @@ fn parse_drop_function() {
38053805
}]),
38063806
args: None
38073807
}],
3808-
option: None
3808+
drop_behavior: None
38093809
}
38103810
);
38113811

@@ -3830,7 +3830,7 @@ fn parse_drop_function() {
38303830
}
38313831
]),
38323832
}],
3833-
option: None
3833+
drop_behavior: None
38343834
}
38353835
);
38363836

@@ -3879,7 +3879,7 @@ fn parse_drop_function() {
38793879
]),
38803880
}
38813881
],
3882-
option: None
3882+
drop_behavior: None
38833883
}
38843884
);
38853885
}
@@ -3899,7 +3899,7 @@ fn parse_drop_procedure() {
38993899
}]),
39003900
args: None
39013901
}],
3902-
option: None
3902+
drop_behavior: None
39033903
}
39043904
);
39053905

@@ -3924,7 +3924,7 @@ fn parse_drop_procedure() {
39243924
}
39253925
]),
39263926
}],
3927-
option: None
3927+
drop_behavior: None
39283928
}
39293929
);
39303930

@@ -3973,7 +3973,7 @@ fn parse_drop_procedure() {
39733973
]),
39743974
}
39753975
],
3976-
option: None
3976+
drop_behavior: None
39773977
}
39783978
);
39793979

0 commit comments

Comments
 (0)