Skip to content

Commit 0f6c71b

Browse files
committed
Add drop behavior to DROP PRIMARY/FOREIGN KEY
1 parent 60a5c8d commit 0f6c71b

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

src/ast/ddl.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,18 @@ pub enum AlterTableOperation {
200200
},
201201
/// `DROP PRIMARY KEY`
202202
///
203-
/// Note: this is a [MySQL]-specific operation.
204-
///
205-
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
206-
DropPrimaryKey,
203+
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html)
204+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/constraints-drop)
205+
DropPrimaryKey {
206+
drop_behavior: Option<DropBehavior>,
207+
},
207208
/// `DROP FOREIGN KEY <fk_symbol>`
208209
///
209-
/// Note: this is a [MySQL]-specific operation.
210-
///
211-
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
210+
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html)
211+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/constraints-drop)
212212
DropForeignKey {
213213
name: Ident,
214+
drop_behavior: Option<DropBehavior>,
214215
},
215216
/// `DROP INDEX <index_name>`
216217
///
@@ -658,8 +659,31 @@ impl fmt::Display for AlterTableOperation {
658659
}
659660
)
660661
}
661-
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
662-
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
662+
AlterTableOperation::DropPrimaryKey { drop_behavior } => {
663+
write!(
664+
f,
665+
"DROP PRIMARY KEY{}",
666+
match drop_behavior {
667+
None => "",
668+
Some(DropBehavior::Restrict) => " RESTRICT",
669+
Some(DropBehavior::Cascade) => " CASCADE",
670+
}
671+
)
672+
}
673+
AlterTableOperation::DropForeignKey {
674+
name,
675+
drop_behavior,
676+
} => {
677+
write!(
678+
f,
679+
"DROP FOREIGN KEY {name}{}",
680+
match drop_behavior {
681+
None => "",
682+
Some(DropBehavior::Restrict) => " RESTRICT",
683+
Some(DropBehavior::Cascade) => " CASCADE",
684+
}
685+
)
686+
}
663687
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
664688
AlterTableOperation::DropColumn {
665689
has_column_keyword,

src/ast/spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,8 @@ impl Spanned for AlterTableOperation {
11511151
} => partition
11521152
.span()
11531153
.union_opt(&with_name.as_ref().map(|n| n.span)),
1154-
AlterTableOperation::DropPrimaryKey => Span::empty(),
1155-
AlterTableOperation::DropForeignKey { name } => name.span,
1154+
AlterTableOperation::DropPrimaryKey { .. } => Span::empty(),
1155+
AlterTableOperation::DropForeignKey { name, .. } => name.span,
11561156
AlterTableOperation::DropIndex { name } => name.span,
11571157
AlterTableOperation::EnableAlwaysRule { name } => name.span,
11581158
AlterTableOperation::EnableAlwaysTrigger { name } => name.span,

src/parser/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8896,10 +8896,15 @@ impl<'a> Parser<'a> {
88968896
drop_behavior,
88978897
}
88988898
} else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
8899-
AlterTableOperation::DropPrimaryKey
8899+
let drop_behavior = self.parse_optional_drop_behavior();
8900+
AlterTableOperation::DropPrimaryKey { drop_behavior }
89008901
} else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::KEY]) {
89018902
let name = self.parse_identifier()?;
8902-
AlterTableOperation::DropForeignKey { name }
8903+
let drop_behavior = self.parse_optional_drop_behavior();
8904+
AlterTableOperation::DropForeignKey {
8905+
name,
8906+
drop_behavior,
8907+
}
89038908
} else if self.parse_keyword(Keyword::INDEX) {
89048909
let name = self.parse_identifier()?;
89058910
AlterTableOperation::DropIndex { name }

tests/sqlparser_mysql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,9 @@ fn parse_alter_table_add_columns() {
27522752
fn parse_alter_table_drop_primary_key() {
27532753
assert_matches!(
27542754
alter_table_op(mysql_and_generic().verified_stmt("ALTER TABLE tab DROP PRIMARY KEY")),
2755-
AlterTableOperation::DropPrimaryKey
2755+
AlterTableOperation::DropPrimaryKey {
2756+
drop_behavior: None
2757+
}
27562758
);
27572759
}
27582760

@@ -2762,7 +2764,7 @@ fn parse_alter_table_drop_foreign_key() {
27622764
alter_table_op(
27632765
mysql_and_generic().verified_stmt("ALTER TABLE tab DROP FOREIGN KEY foo_ibfk_1")
27642766
),
2765-
AlterTableOperation::DropForeignKey { name } if name.value == "foo_ibfk_1"
2767+
AlterTableOperation::DropForeignKey { name, .. } if name.value == "foo_ibfk_1"
27662768
);
27672769
}
27682770

tests/sqlparser_snowflake.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4571,3 +4571,13 @@ fn test_create_database() {
45714571
.to_string();
45724572
assert!(err.contains("Expected"), "Unexpected error: {err}");
45734573
}
4574+
4575+
#[test]
4576+
fn test_drop_constraints() {
4577+
snowflake().verified_stmt("ALTER TABLE tbl DROP PRIMARY KEY");
4578+
snowflake().verified_stmt("ALTER TABLE tbl DROP FOREIGN KEY k1");
4579+
snowflake().verified_stmt("ALTER TABLE tbl DROP CONSTRAINT c1");
4580+
snowflake().verified_stmt("ALTER TABLE tbl DROP PRIMARY KEY CASCADE");
4581+
snowflake().verified_stmt("ALTER TABLE tbl DROP FOREIGN KEY k1 RESTRICT");
4582+
snowflake().verified_stmt("ALTER TABLE tbl DROP CONSTRAINT c1 CASCADE");
4583+
}

0 commit comments

Comments
 (0)