Skip to content

added create and drop schema #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ pub enum Statement {
Commit { chain: bool },
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
Rollback { chain: bool },
/// CREATE SCHEMA
CreateSchema { schema_name: ObjectName },
}

impl fmt::Display for Statement {
Expand Down Expand Up @@ -754,6 +756,7 @@ impl fmt::Display for Statement {
Statement::Rollback { chain } => {
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
}
Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name),
}
}
}
Expand Down Expand Up @@ -852,6 +855,7 @@ pub enum ObjectType {
Table,
View,
Index,
Schema,
}

impl fmt::Display for ObjectType {
Expand All @@ -860,6 +864,7 @@ impl fmt::Display for ObjectType {
ObjectType::Table => "TABLE",
ObjectType::View => "VIEW",
ObjectType::Index => "INDEX",
ObjectType::Schema => "SCHEMA",
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/dialect/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ define_keywords!(
ROW_NUMBER,
ROWS,
SAVEPOINT,
SCHEMA,
SCOPE,
SCROLL,
SEARCH,
Expand Down
16 changes: 14 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,21 @@ impl Parser {
self.parse_create_view()
} else if self.parse_keyword("EXTERNAL") {
self.parse_create_external_table()
} else if self.parse_keyword("SCHEMA") {
self.parse_create_schema()
} else {
self.expected("TABLE, VIEW or INDEX after CREATE", self.peek_token())
self.expected(
"TABLE, VIEW, INDEX or SCHEMA after CREATE",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with the next addition it makes sense to change the message, so that it doesn't duplicate the list above (as it's incorrect already, missing "EXTERNAL").

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> {
self.expect_keyword("TABLE")?;
let table_name = self.parse_object_name()?;
Expand Down Expand Up @@ -918,8 +928,10 @@ impl Parser {
ObjectType::View
} else if self.parse_keyword("INDEX") {
ObjectType::Index
} else if self.parse_keyword("SCHEMA") {
ObjectType::Schema
} 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
// specifying multiple objects to delete in a single statement
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,28 @@ fn parse_create_table_with_multiple_on_delete_fails() {
.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]
fn parse_create_table_with_on_delete_on_update_2in_any_order() -> Result<(), ParserError> {
let sql = |options: &str| -> String {
Expand Down