Skip to content

Commit e3f1507

Browse files
committed
Adds support for mysql's drop index
1 parent 3017265 commit e3f1507

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/ast/mod.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,9 @@ pub enum Statement {
34053405
purge: bool,
34063406
/// MySQL-specific "TEMPORARY" keyword
34073407
temporary: bool,
3408+
/// MySQL-specific drop index syntax, which requires table specification
3409+
/// See <https://dev.mysql.com/doc/refman/8.4/en/drop-index.html>
3410+
table: Option<ObjectName>,
34083411
},
34093412
/// ```sql
34103413
/// DROP FUNCTION
@@ -5242,17 +5245,24 @@ impl fmt::Display for Statement {
52425245
restrict,
52435246
purge,
52445247
temporary,
5245-
} => write!(
5246-
f,
5247-
"DROP {}{}{} {}{}{}{}",
5248-
if *temporary { "TEMPORARY " } else { "" },
5249-
object_type,
5250-
if *if_exists { " IF EXISTS" } else { "" },
5251-
display_comma_separated(names),
5252-
if *cascade { " CASCADE" } else { "" },
5253-
if *restrict { " RESTRICT" } else { "" },
5254-
if *purge { " PURGE" } else { "" }
5255-
),
5248+
table,
5249+
} => {
5250+
write!(
5251+
f,
5252+
"DROP {}{}{} {}{}{}{}",
5253+
if *temporary { "TEMPORARY " } else { "" },
5254+
object_type,
5255+
if *if_exists { " IF EXISTS" } else { "" },
5256+
display_comma_separated(names),
5257+
if *cascade { " CASCADE" } else { "" },
5258+
if *restrict { " RESTRICT" } else { "" },
5259+
if *purge { " PURGE" } else { "" },
5260+
)?;
5261+
if let Some(table_name) = table.as_ref() {
5262+
write!(f, " ON {}", table_name)?;
5263+
};
5264+
Ok(())
5265+
}
52565266
Statement::DropFunction {
52575267
if_exists,
52585268
func_desc,

src/parser/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6249,6 +6249,11 @@ impl<'a> Parser<'a> {
62496249
loc
62506250
);
62516251
}
6252+
// Mysql requires table specification on index drop
6253+
let table = match self.parse_keyword(Keyword::ON) {
6254+
true => Some(self.parse_object_name(false)?),
6255+
false => None,
6256+
};
62526257
Ok(Statement::Drop {
62536258
object_type,
62546259
if_exists,
@@ -6257,6 +6262,7 @@ impl<'a> Parser<'a> {
62576262
restrict,
62586263
purge,
62596264
temporary,
6265+
table,
62606266
})
62616267
}
62626268

tests/sqlparser_mysql.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,3 +3985,34 @@ fn parse_straight_join() {
39853985
mysql()
39863986
.verified_stmt("SELECT a.*, b.* FROM table_a STRAIGHT_JOIN table_b AS b ON a.b_id = b.id");
39873987
}
3988+
3989+
#[test]
3990+
fn parse_drop_index() {
3991+
let sql = "DROP INDEX idx_name ON table_name";
3992+
match mysql().verified_stmt(sql) {
3993+
Statement::Drop {
3994+
object_type,
3995+
if_exists,
3996+
names,
3997+
cascade,
3998+
restrict,
3999+
purge,
4000+
temporary,
4001+
table,
4002+
} => {
4003+
assert!(!if_exists);
4004+
assert_eq!(ObjectType::Index, object_type);
4005+
assert_eq!(
4006+
vec!["idx_name"],
4007+
names.iter().map(ToString::to_string).collect::<Vec<_>>()
4008+
);
4009+
assert!(!cascade);
4010+
assert!(!restrict);
4011+
assert!(!purge);
4012+
assert!(!temporary);
4013+
assert!(table.is_some());
4014+
assert_eq!("table_name", table.unwrap().to_string());
4015+
}
4016+
_ => unreachable!(),
4017+
}
4018+
}

0 commit comments

Comments
 (0)