Skip to content

Commit 803fd6c

Browse files
Add CREATE DATABASE test and parsing (#451)
* Add test * Fix CreateDatabse parsing * Linting * Cleanup for code style
1 parent d38c30e commit 803fd6c

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ impl fmt::Display for Statement {
12181218
location,
12191219
managed_location,
12201220
} => {
1221-
write!(f, "CREATE")?;
1221+
write!(f, "CREATE DATABASE")?;
12221222
if *if_not_exists {
12231223
write!(f, " IF NOT EXISTS")?;
12241224
}

src/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,8 @@ impl<'a> Parser<'a> {
15211521
self.parse_create_virtual_table()
15221522
} else if self.parse_keyword(Keyword::SCHEMA) {
15231523
self.parse_create_schema()
1524+
} else if self.parse_keyword(Keyword::DATABASE) {
1525+
self.parse_create_database()
15241526
} else {
15251527
self.expected("an object type after CREATE", self.peek_token())
15261528
}

tests/sqlparser_common.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,10 +3297,9 @@ fn parse_scalar_subqueries() {
32973297
assert_matches!(
32983298
verified_expr(sql),
32993299
Expr::BinaryOp {
3300-
op: BinaryOperator::Plus, ..
3301-
//left: box Subquery { .. },
3302-
//right: box Subquery { .. },
3303-
}
3300+
op: BinaryOperator::Plus,
3301+
..
3302+
}
33043303
);
33053304
}
33063305

@@ -3386,6 +3385,44 @@ fn parse_exists_subquery() {
33863385
);
33873386
}
33883387

3388+
#[test]
3389+
fn parse_create_database() {
3390+
let sql = "CREATE DATABASE mydb";
3391+
match verified_stmt(sql) {
3392+
Statement::CreateDatabase {
3393+
db_name,
3394+
if_not_exists,
3395+
location,
3396+
managed_location,
3397+
} => {
3398+
assert_eq!("mydb", db_name.to_string());
3399+
assert!(!if_not_exists);
3400+
assert_eq!(None, location);
3401+
assert_eq!(None, managed_location);
3402+
}
3403+
_ => unreachable!(),
3404+
}
3405+
}
3406+
3407+
#[test]
3408+
fn parse_create_database_ine() {
3409+
let sql = "CREATE DATABASE IF NOT EXISTS mydb";
3410+
match verified_stmt(sql) {
3411+
Statement::CreateDatabase {
3412+
db_name,
3413+
if_not_exists,
3414+
location,
3415+
managed_location,
3416+
} => {
3417+
assert_eq!("mydb", db_name.to_string());
3418+
assert!(if_not_exists);
3419+
assert_eq!(None, location);
3420+
assert_eq!(None, managed_location);
3421+
}
3422+
_ => unreachable!(),
3423+
}
3424+
}
3425+
33893426
#[test]
33903427
fn parse_create_view() {
33913428
let sql = "CREATE VIEW myschema.myview AS SELECT foo FROM bar";

0 commit comments

Comments
 (0)