Skip to content

Commit c4cbc83

Browse files
authored
Support DROP CONSTRAINT [ IF EXISTS ] <name> [ CASCADE ] (#396)
* adding support for DROP CONSTRAINT [ IF EXISTS ] <name> * implementing [ CASCADE ] for DROP CONSTRAINT
1 parent e495969 commit c4cbc83

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/ast/ddl.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ pub enum AlterTableOperation {
3232
AddConstraint(TableConstraint),
3333
/// `ADD [ COLUMN ] <column_def>`
3434
AddColumn { column_def: ColumnDef },
35-
/// TODO: implement `DROP CONSTRAINT <name>`
36-
DropConstraint { name: Ident },
35+
/// `DROP CONSTRAINT [ IF EXISTS ] <name>`
36+
DropConstraint {
37+
if_exists: bool,
38+
name: Ident,
39+
cascade: bool,
40+
},
3741
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
3842
DropColumn {
3943
column_name: Ident,
@@ -107,7 +111,19 @@ impl fmt::Display for AlterTableOperation {
107111
display_comma_separated(partitions),
108112
ie = if *if_exists { " IF EXISTS" } else { "" }
109113
),
110-
AlterTableOperation::DropConstraint { name } => write!(f, "DROP CONSTRAINT {}", name),
114+
AlterTableOperation::DropConstraint {
115+
if_exists,
116+
name,
117+
cascade,
118+
} => {
119+
write!(
120+
f,
121+
"DROP CONSTRAINT {}{}{}",
122+
if *if_exists { "IF EXISTS " } else { "" },
123+
name,
124+
if *cascade { " CASCADE" } else { "" },
125+
)
126+
}
111127
AlterTableOperation::DropColumn {
112128
column_name,
113129
if_exists,

src/parser.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,15 @@ impl<'a> Parser<'a> {
20302030
partitions,
20312031
if_exists: false,
20322032
}
2033+
} else if self.parse_keyword(Keyword::CONSTRAINT) {
2034+
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
2035+
let name = self.parse_identifier()?;
2036+
let cascade = self.parse_keyword(Keyword::CASCADE);
2037+
AlterTableOperation::DropConstraint {
2038+
if_exists,
2039+
name,
2040+
cascade,
2041+
}
20332042
} else {
20342043
let _ = self.parse_keyword(Keyword::COLUMN);
20352044
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);

tests/sqlparser_common.rs

+48
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,54 @@ fn parse_alter_table_alter_column_type() {
20772077
);
20782078
}
20792079

2080+
#[test]
2081+
fn parse_alter_table_drop_constraint() {
2082+
let alter_stmt = "ALTER TABLE tab";
2083+
match verified_stmt("ALTER TABLE tab DROP CONSTRAINT constraint_name CASCADE") {
2084+
Statement::AlterTable {
2085+
name,
2086+
operation:
2087+
AlterTableOperation::DropConstraint {
2088+
name: constr_name,
2089+
if_exists,
2090+
cascade,
2091+
},
2092+
} => {
2093+
assert_eq!("tab", name.to_string());
2094+
assert_eq!("constraint_name", constr_name.to_string());
2095+
assert!(!if_exists);
2096+
assert!(cascade);
2097+
}
2098+
_ => unreachable!(),
2099+
}
2100+
match verified_stmt("ALTER TABLE tab DROP CONSTRAINT IF EXISTS constraint_name") {
2101+
Statement::AlterTable {
2102+
name,
2103+
operation:
2104+
AlterTableOperation::DropConstraint {
2105+
name: constr_name,
2106+
if_exists,
2107+
cascade,
2108+
},
2109+
} => {
2110+
assert_eq!("tab", name.to_string());
2111+
assert_eq!("constraint_name", constr_name.to_string());
2112+
assert!(if_exists);
2113+
assert!(!cascade);
2114+
}
2115+
_ => unreachable!(),
2116+
}
2117+
2118+
let res = Parser::parse_sql(
2119+
&GenericDialect {},
2120+
&format!("{} DROP CONSTRAINT is_active TEXT", alter_stmt),
2121+
);
2122+
assert_eq!(
2123+
ParserError::ParserError("Expected end of statement, found: TEXT".to_string()),
2124+
res.unwrap_err()
2125+
);
2126+
}
2127+
20802128
#[test]
20812129
fn parse_bad_constraint() {
20822130
let res = parse_sql_statements("ALTER TABLE tab ADD");

0 commit comments

Comments
 (0)