feat: drop if exists (#2859)

* feat: drop if exists

Signed-off-by: tison <wander4096@gmail.com>

* sqlness cases

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
This commit is contained in:
tison
2023-12-05 02:18:33 +00:00
committed by GitHub
parent 96e12e9ee5
commit 7d506b3c5f
13 changed files with 126 additions and 41 deletions
+23 -11
View File
@@ -29,6 +29,7 @@ impl<'a> ParserContext<'a> {
}
let _ = self.parser.next_token();
let if_exists = self.parser.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let raw_table_ident =
self.parser
.parse_object_name()
@@ -45,7 +46,7 @@ impl<'a> ParserContext<'a> {
}
);
Ok(Statement::DropTable(DropTable::new(table_ident)))
Ok(Statement::DropTable(DropTable::new(table_ident, if_exists)))
}
}
@@ -63,7 +64,15 @@ mod tests {
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::DropTable(DropTable::new(ObjectName(vec![Ident::new("foo")])))
Statement::DropTable(DropTable::new(ObjectName(vec![Ident::new("foo")]), false))
);
let sql = "DROP TABLE IF EXISTS foo";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::DropTable(DropTable::new(ObjectName(vec![Ident::new("foo")]), true))
);
let sql = "DROP TABLE my_schema.foo";
@@ -71,10 +80,10 @@ mod tests {
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::DropTable(DropTable::new(ObjectName(vec![
Ident::new("my_schema"),
Ident::new("foo")
])))
Statement::DropTable(DropTable::new(
ObjectName(vec![Ident::new("my_schema"), Ident::new("foo")]),
false
))
);
let sql = "DROP TABLE my_catalog.my_schema.foo";
@@ -82,11 +91,14 @@ mod tests {
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::DropTable(DropTable::new(ObjectName(vec![
Ident::new("my_catalog"),
Ident::new("my_schema"),
Ident::new("foo")
])))
Statement::DropTable(DropTable::new(
ObjectName(vec![
Ident::new("my_catalog"),
Ident::new("my_schema"),
Ident::new("foo")
]),
false
))
)
}
}
+11 -2
View File
@@ -19,15 +19,24 @@ use sqlparser_derive::{Visit, VisitMut};
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
pub struct DropTable {
table_name: ObjectName,
/// drop table if exists
drop_if_exists: bool,
}
impl DropTable {
/// Creates a statement for `DROP TABLE`
pub fn new(table_name: ObjectName) -> Self {
Self { table_name }
pub fn new(table_name: ObjectName, if_exists: bool) -> Self {
Self {
table_name,
drop_if_exists: if_exists,
}
}
pub fn table_name(&self) -> &ObjectName {
&self.table_name
}
pub fn drop_if_exists(&self) -> bool {
self.drop_if_exists
}
}