Skip to content

Commit b32cbbd

Browse files
authored
Support drop sequence statement (#673)
* 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
1 parent b42632f commit b32cbbd

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/ast/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,9 @@ pub enum Statement {
11841184
/// Whether `CASCADE` was specified. This will be `false` when
11851185
/// `RESTRICT` or no drop behavior at all was specified.
11861186
cascade: bool,
1187+
/// Whether `RESTRICT` was specified. This will be `false` when
1188+
/// `CASCADE` or no drop behavior at all was specified.
1189+
restrict: bool,
11871190
/// Hive allows you specify whether the table's stored data will be
11881191
/// deleted along with the dropped table
11891192
purge: bool,
@@ -2143,14 +2146,16 @@ impl fmt::Display for Statement {
21432146
if_exists,
21442147
names,
21452148
cascade,
2149+
restrict,
21462150
purge,
21472151
} => write!(
21482152
f,
2149-
"DROP {}{} {}{}{}",
2153+
"DROP {}{} {}{}{}{}",
21502154
object_type,
21512155
if *if_exists { " IF EXISTS" } else { "" },
21522156
display_comma_separated(names),
21532157
if *cascade { " CASCADE" } else { "" },
2158+
if *restrict { " RESTRICT" } else { "" },
21542159
if *purge { " PURGE" } else { "" }
21552160
),
21562161
Statement::Discard { object_type } => {
@@ -2910,6 +2915,7 @@ pub enum ObjectType {
29102915
Index,
29112916
Schema,
29122917
Role,
2918+
Sequence,
29132919
}
29142920

29152921
impl fmt::Display for ObjectType {
@@ -2920,6 +2926,7 @@ impl fmt::Display for ObjectType {
29202926
ObjectType::Index => "INDEX",
29212927
ObjectType::Schema => "SCHEMA",
29222928
ObjectType::Role => "ROLE",
2929+
ObjectType::Sequence => "SEQUENCE",
29232930
})
29242931
}
29252932
}

src/parser.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2441,9 +2441,11 @@ impl<'a> Parser<'a> {
24412441
ObjectType::Role
24422442
} else if self.parse_keyword(Keyword::SCHEMA) {
24432443
ObjectType::Schema
2444+
} else if self.parse_keyword(Keyword::SEQUENCE) {
2445+
ObjectType::Sequence
24442446
} else {
24452447
return self.expected(
2446-
"TABLE, VIEW, INDEX, ROLE, or SCHEMA after DROP",
2448+
"TABLE, VIEW, INDEX, ROLE, SCHEMA, or SEQUENCE after DROP",
24472449
self.peek_token(),
24482450
);
24492451
};
@@ -2465,6 +2467,7 @@ impl<'a> Parser<'a> {
24652467
if_exists,
24662468
names,
24672469
cascade,
2470+
restrict,
24682471
purge,
24692472
})
24702473
}

tests/sqlparser_common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4469,6 +4469,7 @@ fn parse_drop_table() {
44694469
names,
44704470
cascade,
44714471
purge: _,
4472+
..
44724473
} => {
44734474
assert!(!if_exists);
44744475
assert_eq!(ObjectType::Table, object_type);
@@ -4489,6 +4490,7 @@ fn parse_drop_table() {
44894490
names,
44904491
cascade,
44914492
purge: _,
4493+
..
44924494
} => {
44934495
assert!(if_exists);
44944496
assert_eq!(ObjectType::Table, object_type);

tests/sqlparser_postgres.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ use sqlparser::ast::*;
2222
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
2323
use sqlparser::parser::ParserError;
2424

25+
#[test]
26+
fn parse_drop_sequence() {
27+
// SimpleLogger::new().init().unwrap();
28+
let sql1 = "DROP SEQUENCE IF EXISTS name0 CASCADE";
29+
pg().one_statement_parses_to(sql1, "DROP SEQUENCE IF EXISTS name0 CASCADE");
30+
let sql2 = "DROP SEQUENCE IF EXISTS name1 RESTRICT";
31+
pg().one_statement_parses_to(sql2, "DROP SEQUENCE IF EXISTS name1 RESTRICT");
32+
let sql3 = "DROP SEQUENCE name2 CASCADE";
33+
pg().one_statement_parses_to(sql3, "DROP SEQUENCE name2 CASCADE");
34+
let sql4 = "DROP SEQUENCE name2";
35+
pg().one_statement_parses_to(sql4, "DROP SEQUENCE name2");
36+
let sql5 = "DROP SEQUENCE name0 CASCADE";
37+
pg().one_statement_parses_to(sql5, "DROP SEQUENCE name0 CASCADE");
38+
let sql6 = "DROP SEQUENCE name1 RESTRICT";
39+
pg().one_statement_parses_to(sql6, "DROP SEQUENCE name1 RESTRICT");
40+
let sql7 = "DROP SEQUENCE name1, name2, name3";
41+
pg().one_statement_parses_to(sql7, "DROP SEQUENCE name1, name2, name3");
42+
}
43+
2544
#[test]
2645
fn parse_create_table_with_defaults() {
2746
let sql = "CREATE TABLE public.customer (

0 commit comments

Comments
 (0)