Skip to content

Commit 91f769e

Browse files
committed
added create and drop schema
1 parent b28dd82 commit 91f769e

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/ast/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ pub enum Statement {
535535
Commit { chain: bool },
536536
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
537537
Rollback { chain: bool },
538+
/// CREATE SCHEMA
539+
CreateSchema { schema_name: ObjectName },
538540
}
539541

540542
impl fmt::Display for Statement {
@@ -754,6 +756,7 @@ impl fmt::Display for Statement {
754756
Statement::Rollback { chain } => {
755757
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
756758
}
759+
Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name),
757760
}
758761
}
759762
}
@@ -852,6 +855,7 @@ pub enum ObjectType {
852855
Table,
853856
View,
854857
Index,
858+
Schema,
855859
}
856860

857861
impl fmt::Display for ObjectType {
@@ -860,6 +864,7 @@ impl fmt::Display for ObjectType {
860864
ObjectType::Table => "TABLE",
861865
ObjectType::View => "VIEW",
862866
ObjectType::Index => "INDEX",
867+
ObjectType::Schema => "SCHEMA",
863868
})
864869
}
865870
}

src/dialect/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ define_keywords!(
330330
ROW_NUMBER,
331331
ROWS,
332332
SAVEPOINT,
333+
SCHEMA,
333334
SCOPE,
334335
SCROLL,
335336
SEARCH,

src/parser.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -864,11 +864,21 @@ impl Parser {
864864
self.parse_create_view()
865865
} else if self.parse_keyword("EXTERNAL") {
866866
self.parse_create_external_table()
867+
} else if self.parse_keyword("SCHEMA") {
868+
self.parse_create_schema()
867869
} else {
868-
self.expected("TABLE, VIEW or INDEX after CREATE", self.peek_token())
870+
self.expected(
871+
"TABLE, VIEW, INDEX or SCHEMA after CREATE",
872+
self.peek_token(),
873+
)
869874
}
870875
}
871876

877+
pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
878+
let schema_name = self.parse_object_name()?;
879+
Ok(Statement::CreateSchema { schema_name })
880+
}
881+
872882
pub fn parse_create_external_table(&mut self) -> Result<Statement, ParserError> {
873883
self.expect_keyword("TABLE")?;
874884
let table_name = self.parse_object_name()?;
@@ -918,8 +928,10 @@ impl Parser {
918928
ObjectType::View
919929
} else if self.parse_keyword("INDEX") {
920930
ObjectType::Index
931+
} else if self.parse_keyword("SCHEMA") {
932+
ObjectType::Schema
921933
} else {
922-
return self.expected("TABLE, VIEW or INDEX after DROP", self.peek_token());
934+
return self.expected("TABLE, VIEW, INDEX or SCHEMA after DROP", self.peek_token());
923935
};
924936
// Many dialects support the non standard `IF EXISTS` clause and allow
925937
// specifying multiple objects to delete in a single statement

tests/sqlparser_common.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,28 @@ fn parse_create_table_with_multiple_on_delete_fails() {
10271027
.expect_err("should have failed");
10281028
}
10291029

1030+
#[test]
1031+
fn parse_create_schema() {
1032+
let sql = "CREATE SCHEMA X";
1033+
1034+
match verified_stmt(sql) {
1035+
Statement::CreateSchema { schema_name } => {
1036+
assert_eq!(schema_name.to_string(), "X".to_owned())
1037+
}
1038+
_ => unreachable!(),
1039+
}
1040+
}
1041+
1042+
#[test]
1043+
fn parse_drop_schema() {
1044+
let sql = "DROP SCHEMA X";
1045+
1046+
match verified_stmt(sql) {
1047+
Statement::Drop { object_type, .. } => assert_eq!(object_type, ObjectType::Schema),
1048+
_ => unreachable!(),
1049+
}
1050+
}
1051+
10301052
#[test]
10311053
fn parse_create_table_with_on_delete_on_update_2in_any_order() -> Result<(), ParserError> {
10321054
let sql = |options: &str| -> String {

0 commit comments

Comments
 (0)