Skip to content

Support DROP CONSTRAINT [ IF EXISTS ] <name> [ CASCADE ] #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2022
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
22 changes: 19 additions & 3 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ pub enum AlterTableOperation {
AddConstraint(TableConstraint),
/// `ADD [ COLUMN ] <column_def>`
AddColumn { column_def: ColumnDef },
/// TODO: implement `DROP CONSTRAINT <name>`
DropConstraint { name: Ident },
/// `DROP CONSTRAINT [ IF EXISTS ] <name>`
DropConstraint {
if_exists: bool,
name: Ident,
cascade: bool,
},
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
DropColumn {
column_name: Ident,
Expand Down Expand Up @@ -106,7 +110,19 @@ impl fmt::Display for AlterTableOperation {
display_comma_separated(partitions),
ie = if *if_exists { " IF EXISTS" } else { "" }
),
AlterTableOperation::DropConstraint { name } => write!(f, "DROP CONSTRAINT {}", name),
AlterTableOperation::DropConstraint {
if_exists,
name,
cascade,
} => {
write!(
f,
"DROP CONSTRAINT {}{}{}",
if *if_exists { "IF EXISTS " } else { "" },
name,
if *cascade { " CASCADE" } else { "" },
)
}
AlterTableOperation::DropColumn {
column_name,
if_exists,
Expand Down
9 changes: 9 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,15 @@ impl<'a> Parser<'a> {
partitions,
if_exists: false,
}
} else if self.parse_keyword(Keyword::CONSTRAINT) {
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let name = self.parse_identifier()?;
let cascade = self.parse_keyword(Keyword::CASCADE);
AlterTableOperation::DropConstraint {
if_exists,
name,
cascade,
}
} else {
let _ = self.parse_keyword(Keyword::COLUMN);
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
Expand Down
48 changes: 48 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,54 @@ fn parse_alter_table_alter_column_type() {
);
}

#[test]
fn parse_alter_table_drop_constraint() {
let alter_stmt = "ALTER TABLE tab";
match verified_stmt("ALTER TABLE tab DROP CONSTRAINT constraint_name CASCADE") {
Statement::AlterTable {
name,
operation:
AlterTableOperation::DropConstraint {
name: constr_name,
if_exists,
cascade,
},
} => {
assert_eq!("tab", name.to_string());
assert_eq!("constraint_name", constr_name.to_string());
assert!(!if_exists);
assert!(cascade);
}
_ => unreachable!(),
}
match verified_stmt("ALTER TABLE tab DROP CONSTRAINT IF EXISTS constraint_name") {
Statement::AlterTable {
name,
operation:
AlterTableOperation::DropConstraint {
name: constr_name,
if_exists,
cascade,
},
} => {
assert_eq!("tab", name.to_string());
assert_eq!("constraint_name", constr_name.to_string());
assert!(if_exists);
assert!(!cascade);
}
_ => unreachable!(),
}

let res = Parser::parse_sql(
&GenericDialect {},
&format!("{} DROP CONSTRAINT is_active TEXT", alter_stmt),
);
assert_eq!(
ParserError::ParserError("Expected end of statement, found: TEXT".to_string()),
res.unwrap_err()
);
}

#[test]
fn parse_bad_constraint() {
let res = parse_sql_statements("ALTER TABLE tab ADD");
Expand Down