mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-08 15:10:32 +00:00
added create and drop schema
This commit is contained in:
parent
b28dd82838
commit
91f769e460
4 changed files with 42 additions and 2 deletions
|
@ -535,6 +535,8 @@ pub enum Statement {
|
||||||
Commit { chain: bool },
|
Commit { chain: bool },
|
||||||
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
|
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
|
||||||
Rollback { chain: bool },
|
Rollback { chain: bool },
|
||||||
|
/// CREATE SCHEMA
|
||||||
|
CreateSchema { schema_name: ObjectName },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Statement {
|
impl fmt::Display for Statement {
|
||||||
|
@ -754,6 +756,7 @@ impl fmt::Display for Statement {
|
||||||
Statement::Rollback { chain } => {
|
Statement::Rollback { chain } => {
|
||||||
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
|
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
|
||||||
}
|
}
|
||||||
|
Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -852,6 +855,7 @@ pub enum ObjectType {
|
||||||
Table,
|
Table,
|
||||||
View,
|
View,
|
||||||
Index,
|
Index,
|
||||||
|
Schema,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for ObjectType {
|
impl fmt::Display for ObjectType {
|
||||||
|
@ -860,6 +864,7 @@ impl fmt::Display for ObjectType {
|
||||||
ObjectType::Table => "TABLE",
|
ObjectType::Table => "TABLE",
|
||||||
ObjectType::View => "VIEW",
|
ObjectType::View => "VIEW",
|
||||||
ObjectType::Index => "INDEX",
|
ObjectType::Index => "INDEX",
|
||||||
|
ObjectType::Schema => "SCHEMA",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -330,6 +330,7 @@ define_keywords!(
|
||||||
ROW_NUMBER,
|
ROW_NUMBER,
|
||||||
ROWS,
|
ROWS,
|
||||||
SAVEPOINT,
|
SAVEPOINT,
|
||||||
|
SCHEMA,
|
||||||
SCOPE,
|
SCOPE,
|
||||||
SCROLL,
|
SCROLL,
|
||||||
SEARCH,
|
SEARCH,
|
||||||
|
|
|
@ -864,11 +864,21 @@ impl Parser {
|
||||||
self.parse_create_view()
|
self.parse_create_view()
|
||||||
} else if self.parse_keyword("EXTERNAL") {
|
} else if self.parse_keyword("EXTERNAL") {
|
||||||
self.parse_create_external_table()
|
self.parse_create_external_table()
|
||||||
|
} else if self.parse_keyword("SCHEMA") {
|
||||||
|
self.parse_create_schema()
|
||||||
} else {
|
} else {
|
||||||
self.expected("TABLE, VIEW or INDEX after CREATE", self.peek_token())
|
self.expected(
|
||||||
|
"TABLE, VIEW, INDEX or SCHEMA after CREATE",
|
||||||
|
self.peek_token(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
|
||||||
|
let schema_name = self.parse_object_name()?;
|
||||||
|
Ok(Statement::CreateSchema { schema_name })
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_create_external_table(&mut self) -> Result<Statement, ParserError> {
|
pub fn parse_create_external_table(&mut self) -> Result<Statement, ParserError> {
|
||||||
self.expect_keyword("TABLE")?;
|
self.expect_keyword("TABLE")?;
|
||||||
let table_name = self.parse_object_name()?;
|
let table_name = self.parse_object_name()?;
|
||||||
|
@ -918,8 +928,10 @@ impl Parser {
|
||||||
ObjectType::View
|
ObjectType::View
|
||||||
} else if self.parse_keyword("INDEX") {
|
} else if self.parse_keyword("INDEX") {
|
||||||
ObjectType::Index
|
ObjectType::Index
|
||||||
|
} else if self.parse_keyword("SCHEMA") {
|
||||||
|
ObjectType::Schema
|
||||||
} else {
|
} else {
|
||||||
return self.expected("TABLE, VIEW or INDEX after DROP", self.peek_token());
|
return self.expected("TABLE, VIEW, INDEX or SCHEMA after DROP", self.peek_token());
|
||||||
};
|
};
|
||||||
// Many dialects support the non standard `IF EXISTS` clause and allow
|
// Many dialects support the non standard `IF EXISTS` clause and allow
|
||||||
// specifying multiple objects to delete in a single statement
|
// specifying multiple objects to delete in a single statement
|
||||||
|
|
|
@ -1027,6 +1027,28 @@ fn parse_create_table_with_multiple_on_delete_fails() {
|
||||||
.expect_err("should have failed");
|
.expect_err("should have failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_schema() {
|
||||||
|
let sql = "CREATE SCHEMA X";
|
||||||
|
|
||||||
|
match verified_stmt(sql) {
|
||||||
|
Statement::CreateSchema { schema_name } => {
|
||||||
|
assert_eq!(schema_name.to_string(), "X".to_owned())
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_drop_schema() {
|
||||||
|
let sql = "DROP SCHEMA X";
|
||||||
|
|
||||||
|
match verified_stmt(sql) {
|
||||||
|
Statement::Drop { object_type, .. } => assert_eq!(object_type, ObjectType::Schema),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_create_table_with_on_delete_on_update_2in_any_order() -> Result<(), ParserError> {
|
fn parse_create_table_with_on_delete_on_update_2in_any_order() -> Result<(), ParserError> {
|
||||||
let sql = |options: &str| -> String {
|
let sql = |options: &str| -> String {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue