Skip to content

Commit 397bceb

Browse files
authored
Replace ReferentialAction enum in DROP statements (#1648)
1 parent 62bcaa1 commit 397bceb

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

src/ast/ddl.rs

Lines changed: 20 additions & 0 deletions
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

Lines changed: 16 additions & 16 deletions
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};
@@ -2700,7 +2700,7 @@ pub enum Statement {
27002700
/// One or more function to drop
27012701
func_desc: Vec<FunctionDesc>,
27022702
/// `CASCADE` or `RESTRICT`
2703-
option: Option<ReferentialAction>,
2703+
drop_behavior: Option<DropBehavior>,
27042704
},
27052705
/// ```sql
27062706
/// DROP PROCEDURE
@@ -2710,7 +2710,7 @@ pub enum Statement {
27102710
/// One or more function to drop
27112711
proc_desc: Vec<FunctionDesc>,
27122712
/// `CASCADE` or `RESTRICT`
2713-
option: Option<ReferentialAction>,
2713+
drop_behavior: Option<DropBehavior>,
27142714
},
27152715
/// ```sql
27162716
/// DROP SECRET
@@ -2729,7 +2729,7 @@ pub enum Statement {
27292729
if_exists: bool,
27302730
name: Ident,
27312731
table_name: ObjectName,
2732-
option: Option<ReferentialAction>,
2732+
drop_behavior: Option<DropBehavior>,
27332733
},
27342734
/// ```sql
27352735
/// DECLARE
@@ -4317,31 +4317,31 @@ impl fmt::Display for Statement {
43174317
Statement::DropFunction {
43184318
if_exists,
43194319
func_desc,
4320-
option,
4320+
drop_behavior,
43214321
} => {
43224322
write!(
43234323
f,
43244324
"DROP FUNCTION{} {}",
43254325
if *if_exists { " IF EXISTS" } else { "" },
43264326
display_comma_separated(func_desc),
43274327
)?;
4328-
if let Some(op) = option {
4328+
if let Some(op) = drop_behavior {
43294329
write!(f, " {op}")?;
43304330
}
43314331
Ok(())
43324332
}
43334333
Statement::DropProcedure {
43344334
if_exists,
43354335
proc_desc,
4336-
option,
4336+
drop_behavior,
43374337
} => {
43384338
write!(
43394339
f,
43404340
"DROP PROCEDURE{} {}",
43414341
if *if_exists { " IF EXISTS" } else { "" },
43424342
display_comma_separated(proc_desc),
43434343
)?;
4344-
if let Some(op) = option {
4344+
if let Some(op) = drop_behavior {
43454345
write!(f, " {op}")?;
43464346
}
43474347
Ok(())
@@ -4370,15 +4370,15 @@ impl fmt::Display for Statement {
43704370
if_exists,
43714371
name,
43724372
table_name,
4373-
option,
4373+
drop_behavior,
43744374
} => {
43754375
write!(f, "DROP POLICY")?;
43764376
if *if_exists {
43774377
write!(f, " IF EXISTS")?;
43784378
}
43794379
write!(f, " {name} ON {table_name}")?;
4380-
if let Some(option) = option {
4381-
write!(f, " {option}")?;
4380+
if let Some(drop_behavior) = drop_behavior {
4381+
write!(f, " {drop_behavior}")?;
43824382
}
43834383
Ok(())
43844384
}

src/parser/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,10 +5514,10 @@ impl<'a> Parser<'a> {
55145514
})
55155515
}
55165516

5517-
fn parse_optional_referential_action(&mut self) -> Option<ReferentialAction> {
5517+
fn parse_optional_drop_behavior(&mut self) -> Option<DropBehavior> {
55185518
match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
5519-
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
5520-
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
5519+
Some(Keyword::CASCADE) => Some(DropBehavior::Cascade),
5520+
Some(Keyword::RESTRICT) => Some(DropBehavior::Restrict),
55215521
_ => None,
55225522
}
55235523
}
@@ -5529,11 +5529,11 @@ impl<'a> Parser<'a> {
55295529
fn parse_drop_function(&mut self) -> Result<Statement, ParserError> {
55305530
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
55315531
let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
5532-
let option = self.parse_optional_referential_action();
5532+
let drop_behavior = self.parse_optional_drop_behavior();
55335533
Ok(Statement::DropFunction {
55345534
if_exists,
55355535
func_desc,
5536-
option,
5536+
drop_behavior,
55375537
})
55385538
}
55395539

@@ -5547,12 +5547,12 @@ impl<'a> Parser<'a> {
55475547
let name = self.parse_identifier()?;
55485548
self.expect_keyword_is(Keyword::ON)?;
55495549
let table_name = self.parse_object_name(false)?;
5550-
let option = self.parse_optional_referential_action();
5550+
let drop_behavior = self.parse_optional_drop_behavior();
55515551
Ok(Statement::DropPolicy {
55525552
if_exists,
55535553
name,
55545554
table_name,
5555-
option,
5555+
drop_behavior,
55565556
})
55575557
}
55585558

@@ -5563,11 +5563,11 @@ impl<'a> Parser<'a> {
55635563
fn parse_drop_procedure(&mut self) -> Result<Statement, ParserError> {
55645564
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
55655565
let proc_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
5566-
let option = self.parse_optional_referential_action();
5566+
let drop_behavior = self.parse_optional_drop_behavior();
55675567
Ok(Statement::DropProcedure {
55685568
if_exists,
55695569
proc_desc,
5570-
option,
5570+
drop_behavior,
55715571
})
55725572
}
55735573

tests/sqlparser_common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11760,12 +11760,12 @@ fn test_drop_policy() {
1176011760
if_exists,
1176111761
name,
1176211762
table_name,
11763-
option,
11763+
drop_behavior,
1176411764
} => {
1176511765
assert_eq!(if_exists, true);
1176611766
assert_eq!(name.to_string(), "my_policy");
1176711767
assert_eq!(table_name.to_string(), "my_table");
11768-
assert_eq!(option, Some(ReferentialAction::Restrict));
11768+
assert_eq!(drop_behavior, Some(DropBehavior::Restrict));
1176911769
}
1177011770
_ => unreachable!(),
1177111771
}

tests/sqlparser_postgres.rs

Lines changed: 6 additions & 6 deletions
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)