Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,16 @@ pub enum Statement {
name: Ident,
storage_specifier: Option<Ident>,
},
///```sql
/// DROP POLICY
/// ```
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-droppolicy.html)
DropPolicy {
if_exists: bool,
name: Ident,
table_name: ObjectName,
option: Option<ReferentialAction>,
},
/// ```sql
/// DECLARE
/// ```
Expand Down Expand Up @@ -4258,6 +4268,22 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::DropPolicy {
if_exists,
name,
table_name,
option,
} => {
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}")?;
}
Ok(())
}
Statement::Discard { object_type } => {
write!(f, "DISCARD {object_type}")?;
Ok(())
Expand Down
42 changes: 31 additions & 11 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4887,6 +4887,8 @@ impl<'a> Parser<'a> {
ObjectType::Stage
} else if self.parse_keyword(Keyword::FUNCTION) {
return self.parse_drop_function();
} else if self.parse_keyword(Keyword::POLICY) {
return self.parse_drop_policy();
} else if self.parse_keyword(Keyword::PROCEDURE) {
return self.parse_drop_procedure();
} else if self.parse_keyword(Keyword::SECRET) {
Expand Down Expand Up @@ -4928,38 +4930,56 @@ impl<'a> Parser<'a> {
})
}

fn parse_optional_referential_action(&mut self) -> Option<ReferentialAction> {
match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
_ => None,
}
}

/// ```sql
/// DROP FUNCTION [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
/// [ CASCADE | RESTRICT ]
/// ```
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 = match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
_ => None,
};
let option = self.parse_optional_referential_action();
Ok(Statement::DropFunction {
if_exists,
func_desc,
option,
})
}

/// ```sql
/// DROP POLICY [ IF EXISTS ] name ON table_name [ CASCADE | RESTRICT ]
/// ```
///
/// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-droppolicy.html)
fn parse_drop_policy(&mut self) -> Result<Statement, ParserError> {
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let name = self.parse_identifier(false)?;
self.expect_keyword(Keyword::ON)?;
let table_name = self.parse_object_name(false)?;
let option = self.parse_optional_referential_action();
Ok(Statement::DropPolicy {
if_exists,
name,
table_name,
option,
})
}

/// ```sql
/// DROP PROCEDURE [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
/// [ CASCADE | RESTRICT ]
/// ```
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 = match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
Some(_) => unreachable!(), // parse_one_of_keywords does not return other keywords
None => None,
};
let option = self.parse_optional_referential_action();
Ok(Statement::DropProcedure {
if_exists,
proc_desc,
Expand Down
41 changes: 41 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11089,3 +11089,44 @@ fn test_create_policy() {
"sql parser error: Expected: (, found: EOF"
);
}

#[test]
fn test_drop_policy() {
let sql = "DROP POLICY IF EXISTS my_policy ON my_table RESTRICT";
match all_dialects().verified_stmt(sql) {
Statement::DropPolicy {
if_exists,
name,
table_name,
option,
} => {
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));
}
_ => unreachable!(),
}

// omit IF EXISTS is allowed
all_dialects().verified_stmt("DROP POLICY my_policy ON my_table CASCADE");
// omit option is allowed
all_dialects().verified_stmt("DROP POLICY my_policy ON my_table");

// missing table name
assert_eq!(
all_dialects()
.parse_sql_statements("DROP POLICY my_policy")
.unwrap_err()
.to_string(),
"sql parser error: Expected: ON, found: EOF"
);
// Wrong option name
assert_eq!(
all_dialects()
.parse_sql_statements("DROP POLICY my_policy ON my_table WRONG")
.unwrap_err()
.to_string(),
"sql parser error: Expected: end of statement, found: WRONG"
);
}
Loading