Skip to content

Commit

Permalink
Replace ReferentialAction enum in DROP statements (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg authored Jan 8, 2025
1 parent 62bcaa1 commit 397bceb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
20 changes: 20 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,26 @@ impl fmt::Display for ReferentialAction {
}
}

/// `<drop behavior> ::= CASCADE | RESTRICT`.
///
/// Used in `DROP` statements.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum DropBehavior {
Restrict,
Cascade,
}

impl fmt::Display for DropBehavior {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
DropBehavior::Restrict => "RESTRICT",
DropBehavior::Cascade => "CASCADE",
})
}
}

/// SQL user defined type definition
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
32 changes: 16 additions & 16 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ pub use self::dcl::{
pub use self::ddl::{
AlterColumnOperation, AlterIndexOperation, AlterPolicyOperation, AlterTableOperation,
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty,
ConstraintCharacteristics, CreateFunction, Deduplicate, DeferrableInitial, GeneratedAs,
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
IdentityPropertyKind, IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay,
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, TableConstraint,
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
ViewColumnDef,
ConstraintCharacteristics, CreateFunction, Deduplicate, DeferrableInitial, DropBehavior,
GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexOption,
IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner, Partition, ProcedureParam,
ReferentialAction, TableConstraint, TagsColumnOption, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
pub use self::operator::{BinaryOperator, UnaryOperator};
Expand Down Expand Up @@ -2700,7 +2700,7 @@ pub enum Statement {
/// One or more function to drop
func_desc: Vec<FunctionDesc>,
/// `CASCADE` or `RESTRICT`
option: Option<ReferentialAction>,
drop_behavior: Option<DropBehavior>,
},
/// ```sql
/// DROP PROCEDURE
Expand All @@ -2710,7 +2710,7 @@ pub enum Statement {
/// One or more function to drop
proc_desc: Vec<FunctionDesc>,
/// `CASCADE` or `RESTRICT`
option: Option<ReferentialAction>,
drop_behavior: Option<DropBehavior>,
},
/// ```sql
/// DROP SECRET
Expand All @@ -2729,7 +2729,7 @@ pub enum Statement {
if_exists: bool,
name: Ident,
table_name: ObjectName,
option: Option<ReferentialAction>,
drop_behavior: Option<DropBehavior>,
},
/// ```sql
/// DECLARE
Expand Down Expand Up @@ -4317,31 +4317,31 @@ impl fmt::Display for Statement {
Statement::DropFunction {
if_exists,
func_desc,
option,
drop_behavior,
} => {
write!(
f,
"DROP FUNCTION{} {}",
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(func_desc),
)?;
if let Some(op) = option {
if let Some(op) = drop_behavior {
write!(f, " {op}")?;
}
Ok(())
}
Statement::DropProcedure {
if_exists,
proc_desc,
option,
drop_behavior,
} => {
write!(
f,
"DROP PROCEDURE{} {}",
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(proc_desc),
)?;
if let Some(op) = option {
if let Some(op) = drop_behavior {
write!(f, " {op}")?;
}
Ok(())
Expand Down Expand Up @@ -4370,15 +4370,15 @@ impl fmt::Display for Statement {
if_exists,
name,
table_name,
option,
drop_behavior,
} => {
write!(f, "DROP POLICY")?;
if *if_exists {
write!(f, " IF EXISTS")?;
}
write!(f, " {name} ON {table_name}")?;
if let Some(option) = option {
write!(f, " {option}")?;
if let Some(drop_behavior) = drop_behavior {
write!(f, " {drop_behavior}")?;
}
Ok(())
}
Expand Down
18 changes: 9 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5514,10 +5514,10 @@ impl<'a> Parser<'a> {
})
}

fn parse_optional_referential_action(&mut self) -> Option<ReferentialAction> {
fn parse_optional_drop_behavior(&mut self) -> Option<DropBehavior> {
match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
Some(Keyword::CASCADE) => Some(DropBehavior::Cascade),
Some(Keyword::RESTRICT) => Some(DropBehavior::Restrict),
_ => None,
}
}
Expand All @@ -5529,11 +5529,11 @@ impl<'a> Parser<'a> {
fn parse_drop_function(&mut self) -> Result<Statement, ParserError> {
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
let option = self.parse_optional_referential_action();
let drop_behavior = self.parse_optional_drop_behavior();
Ok(Statement::DropFunction {
if_exists,
func_desc,
option,
drop_behavior,
})
}

Expand All @@ -5547,12 +5547,12 @@ impl<'a> Parser<'a> {
let name = self.parse_identifier()?;
self.expect_keyword_is(Keyword::ON)?;
let table_name = self.parse_object_name(false)?;
let option = self.parse_optional_referential_action();
let drop_behavior = self.parse_optional_drop_behavior();
Ok(Statement::DropPolicy {
if_exists,
name,
table_name,
option,
drop_behavior,
})
}

Expand All @@ -5563,11 +5563,11 @@ impl<'a> Parser<'a> {
fn parse_drop_procedure(&mut self) -> Result<Statement, ParserError> {
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let proc_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
let option = self.parse_optional_referential_action();
let drop_behavior = self.parse_optional_drop_behavior();
Ok(Statement::DropProcedure {
if_exists,
proc_desc,
option,
drop_behavior,
})
}

Expand Down
4 changes: 2 additions & 2 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11760,12 +11760,12 @@ fn test_drop_policy() {
if_exists,
name,
table_name,
option,
drop_behavior,
} => {
assert_eq!(if_exists, true);
assert_eq!(name.to_string(), "my_policy");
assert_eq!(table_name.to_string(), "my_table");
assert_eq!(option, Some(ReferentialAction::Restrict));
assert_eq!(drop_behavior, Some(DropBehavior::Restrict));
}
_ => unreachable!(),
}
Expand Down
12 changes: 6 additions & 6 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3805,7 +3805,7 @@ fn parse_drop_function() {
}]),
args: None
}],
option: None
drop_behavior: None
}
);

Expand All @@ -3830,7 +3830,7 @@ fn parse_drop_function() {
}
]),
}],
option: None
drop_behavior: None
}
);

Expand Down Expand Up @@ -3879,7 +3879,7 @@ fn parse_drop_function() {
]),
}
],
option: None
drop_behavior: None
}
);
}
Expand All @@ -3899,7 +3899,7 @@ fn parse_drop_procedure() {
}]),
args: None
}],
option: None
drop_behavior: None
}
);

Expand All @@ -3924,7 +3924,7 @@ fn parse_drop_procedure() {
}
]),
}],
option: None
drop_behavior: None
}
);

Expand Down Expand Up @@ -3973,7 +3973,7 @@ fn parse_drop_procedure() {
]),
}
],
option: None
drop_behavior: None
}
);

Expand Down

0 comments on commit 397bceb

Please sign in to comment.