Skip to content
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

Support drop sequence statement #673

Merged
merged 6 commits into from Oct 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ast/mod.rs
Expand Up @@ -1184,6 +1184,9 @@ pub enum Statement {
/// Whether `CASCADE` was specified. This will be `false` when
/// `RESTRICT` or no drop behavior at all was specified.
cascade: bool,
/// Whether `RESTRICT` was specified. This will be `false` when
/// `CASCADE` or no drop behavior at all was specified.
restrict: bool,
/// Hive allows you specify whether the table's stored data will be
/// deleted along with the dropped table
purge: bool,
Expand Down Expand Up @@ -2124,14 +2127,16 @@ impl fmt::Display for Statement {
if_exists,
names,
cascade,
restrict,
purge,
} => write!(
f,
"DROP {}{} {}{}{}",
"DROP {}{} {}{}{}{}",
object_type,
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(names),
if *cascade { " CASCADE" } else { "" },
if *restrict { " RESTRICT" } else { "" },
if *purge { " PURGE" } else { "" }
),
Statement::Discard { object_type } => {
Expand Down Expand Up @@ -2844,6 +2849,7 @@ pub enum ObjectType {
Index,
Schema,
Role,
Sequence,
}

impl fmt::Display for ObjectType {
Expand All @@ -2854,6 +2860,7 @@ impl fmt::Display for ObjectType {
ObjectType::Index => "INDEX",
ObjectType::Schema => "SCHEMA",
ObjectType::Role => "ROLE",
ObjectType::Sequence => "SEQUENCE",
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/parser.rs
Expand Up @@ -2328,9 +2328,11 @@ impl<'a> Parser<'a> {
ObjectType::Role
} else if self.parse_keyword(Keyword::SCHEMA) {
ObjectType::Schema
} else if self.parse_keyword(Keyword::SEQUENCE) {
ObjectType::Sequence
} else {
return self.expected(
"TABLE, VIEW, INDEX, ROLE, or SCHEMA after DROP",
"TABLE, VIEW, INDEX, ROLE, SCHEMA, or SEQUENCE after DROP",
self.peek_token(),
);
};
Expand All @@ -2352,6 +2354,7 @@ impl<'a> Parser<'a> {
if_exists,
names,
cascade,
restrict,
purge,
})
}
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_common.rs
Expand Up @@ -4463,6 +4463,7 @@ fn parse_drop_table() {
names,
cascade,
purge: _,
..
} => {
assert!(!if_exists);
assert_eq!(ObjectType::Table, object_type);
Expand All @@ -4483,6 +4484,7 @@ fn parse_drop_table() {
names,
cascade,
purge: _,
..
} => {
assert!(if_exists);
assert_eq!(ObjectType::Table, object_type);
Expand Down
19 changes: 19 additions & 0 deletions tests/sqlparser_postgres.rs
Expand Up @@ -22,6 +22,25 @@ use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
use sqlparser::parser::ParserError;

#[test]
fn parse_drop_sequence() {
// SimpleLogger::new().init().unwrap();
let sql1 = "DROP SEQUENCE IF EXISTS name0 CASCADE";
pg().one_statement_parses_to(sql1, "DROP SEQUENCE IF EXISTS name0 CASCADE");
let sql2 = "DROP SEQUENCE IF EXISTS name1 RESTRICT";
pg().one_statement_parses_to(sql2, "DROP SEQUENCE IF EXISTS name1 RESTRICT");
let sql3 = "DROP SEQUENCE name2 CASCADE";
pg().one_statement_parses_to(sql3, "DROP SEQUENCE name2 CASCADE");
let sql4 = "DROP SEQUENCE name2";
pg().one_statement_parses_to(sql4, "DROP SEQUENCE name2");
let sql5 = "DROP SEQUENCE name0 CASCADE";
pg().one_statement_parses_to(sql5, "DROP SEQUENCE name0 CASCADE");
let sql6 = "DROP SEQUENCE name1 RESTRICT";
pg().one_statement_parses_to(sql6, "DROP SEQUENCE name1 RESTRICT");
let sql7 = "DROP SEQUENCE name1, name2, name3";
pg().one_statement_parses_to(sql7, "DROP SEQUENCE name1, name2, name3");
}

#[test]
fn parse_create_table_with_defaults() {
let sql = "CREATE TABLE public.customer (
Expand Down