Skip to content

Commit

Permalink
Support drop sequence statement (#673)
Browse files Browse the repository at this point in the history
* Add ObjectType Sequence

* Drop sequence test cases added.

* Parser and Drop statement Display updated.

* Parser and Drop statement Display updated.

* Fix compile errors

* add new test case
  • Loading branch information
samjay000 committed Oct 15, 2022
1 parent b42632f commit b32cbbd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
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 @@ -2143,14 +2146,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 @@ -2910,6 +2915,7 @@ pub enum ObjectType {
Index,
Schema,
Role,
Sequence,
}

impl fmt::Display for ObjectType {
Expand All @@ -2920,6 +2926,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 @@ -2441,9 +2441,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 @@ -2465,6 +2467,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 @@ -4469,6 +4469,7 @@ fn parse_drop_table() {
names,
cascade,
purge: _,
..
} => {
assert!(!if_exists);
assert_eq!(ObjectType::Table, object_type);
Expand All @@ -4489,6 +4490,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

0 comments on commit b32cbbd

Please sign in to comment.