From 73dc8a352d7d9edad3def308042f4db2a3c9a027 Mon Sep 17 00:00:00 2001 From: Heran Lin Date: Sun, 29 Sep 2024 18:17:19 +0800 Subject: [PATCH] Support `DROP DATABASE` (#1443) --- src/ast/mod.rs | 2 ++ src/parser/mod.rs | 4 +++- tests/sqlparser_common.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 03db24fcd..19354ab96 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -5691,6 +5691,7 @@ pub enum ObjectType { View, Index, Schema, + Database, Role, Sequence, Stage, @@ -5703,6 +5704,7 @@ impl fmt::Display for ObjectType { ObjectType::View => "VIEW", ObjectType::Index => "INDEX", ObjectType::Schema => "SCHEMA", + ObjectType::Database => "DATABASE", ObjectType::Role => "ROLE", ObjectType::Sequence => "SEQUENCE", ObjectType::Stage => "STAGE", diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 404a16c48..e5fd53554 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4881,6 +4881,8 @@ impl<'a> Parser<'a> { ObjectType::Role } else if self.parse_keyword(Keyword::SCHEMA) { ObjectType::Schema + } else if self.parse_keyword(Keyword::DATABASE) { + ObjectType::Database } else if self.parse_keyword(Keyword::SEQUENCE) { ObjectType::Sequence } else if self.parse_keyword(Keyword::STAGE) { @@ -4897,7 +4899,7 @@ impl<'a> Parser<'a> { return self.parse_drop_trigger(); } else { return self.expected( - "TABLE, VIEW, INDEX, ROLE, SCHEMA, FUNCTION, PROCEDURE, STAGE, TRIGGER, SECRET or SEQUENCE after DROP", + "TABLE, VIEW, INDEX, ROLE, SCHEMA, DATABASE, FUNCTION, PROCEDURE, STAGE, TRIGGER, SECRET or SEQUENCE after DROP", self.peek_token(), ); }; diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 48c4aafe1..e066451df 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -6734,6 +6734,43 @@ fn parse_create_database_ine() { } } +#[test] +fn parse_drop_database() { + let sql = "DROP DATABASE mycatalog.mydb"; + match verified_stmt(sql) { + Statement::Drop { + names, + object_type, + if_exists, + .. + } => { + assert_eq!( + vec!["mycatalog.mydb"], + names.iter().map(ToString::to_string).collect::>() + ); + assert_eq!(ObjectType::Database, object_type); + assert!(!if_exists); + } + _ => unreachable!(), + } +} + +#[test] +fn parse_drop_database_if_exists() { + let sql = "DROP DATABASE IF EXISTS mydb"; + match verified_stmt(sql) { + Statement::Drop { + object_type, + if_exists, + .. + } => { + assert_eq!(ObjectType::Database, object_type); + assert!(if_exists); + } + _ => unreachable!(), + } +} + #[test] fn parse_create_view() { let sql = "CREATE VIEW myschema.myview AS SELECT foo FROM bar";