Skip to content

create drop schema conditionally #276

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 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ pub enum Statement {
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
Rollback { chain: bool },
/// CREATE SCHEMA
CreateSchema { schema_name: ObjectName },
CreateSchema {
schema_name: ObjectName,
if_not_exists: bool,
},
/// `ASSERT <condition> [AS <message>]`
Assert {
condition: Expr,
Expand Down Expand Up @@ -829,7 +832,19 @@ 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),
Statement::CreateSchema {
schema_name,
if_not_exists,
} => write!(
f,
"CREATE SCHEMA{if_not_exists}{name}",
Copy link
Contributor

Choose a reason for hiding this comment

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

Using "CREATE SCHEMA {if_not_exists}{name}" and an empty string for if_not_exists when it needs to be omitted both is consistent with other implementations, and results in more compact code.

if_not_exists = if *if_not_exists {
" IF NOT EXISTS "
} else {
" "
},
name = schema_name
),
Statement::Assert { condition, message } => {
write!(f, "ASSERT {}", condition)?;
if let Some(m) = message {
Expand Down
6 changes: 5 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,12 @@ impl<'a> Parser<'a> {
}

pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let schema_name = self.parse_object_name()?;
Ok(Statement::CreateSchema { schema_name })
Ok(Statement::CreateSchema {
schema_name,
if_not_exists,
})
}

pub fn parse_create_external_table(
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ fn parse_create_schema() {
let sql = "CREATE SCHEMA X";

match verified_stmt(sql) {
Statement::CreateSchema { schema_name } => {
Statement::CreateSchema { schema_name, .. } => {
assert_eq!(schema_name.to_string(), "X".to_owned())
}
_ => unreachable!(),
Expand Down
27 changes: 27 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,33 @@ fn parse_bad_if_not_exists() {
);
}

#[test]
fn parse_create_schema_if_not_exists() {
let sql = "CREATE SCHEMA IF NOT EXISTS schema_name";
let ast = pg().verified_stmt(sql);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please default to pg_and_generic() unless testing a construct that can't be parsed by the generic parser. The idea of the generic parser is to be able to parse as much SQL code as possible.

match ast {
Statement::CreateSchema {
if_not_exists: true,
schema_name,
} => assert_eq!("schema_name", schema_name.to_string()),
_ => unreachable!(),
}
}

#[test]
fn parse_drop_schema_if_exists() {
let sql = "DROP SCHEMA IF EXISTS schema_name";
let ast = pg().verified_stmt(sql);
match ast {
Statement::Drop {
object_type,
if_exists: true,
..
} => assert_eq!(object_type, ObjectType::Schema),
_ => unreachable!(),
}
}

#[test]
fn parse_copy_example() {
let sql = r#"COPY public.actor (actor_id, first_name, last_name, last_update, value) FROM stdin;
Expand Down