Skip to content

Commit

Permalink
Added DROP SEQUENCE and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
samjay000 committed Aug 20, 2022
1 parent d0621de commit d7f1948
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
6 changes: 1 addition & 5 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ pub enum ColumnOption {
DialectSpecific(Vec<Token>),
CharacterSet(ObjectName),
Comment(String),
/// ALWAYS AS ( generation_expr ) STORED |
/// GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]
/// <identity column specification> ::=
/// GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY
/// [ <left paren> <common sequence generator options> <right paren> ]
/// https://www.postgresql.org/docs/current/sql-createtable.html
Generated {
always_or_by_default_or_always_as: AlwaysOrByDefaultOrAlwaysAs,
sequence_options: Vec<SequenceOptions>,
Expand Down
15 changes: 6 additions & 9 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,8 @@ pub enum Statement {
/// Hive allows you specify whether the table's stored data will be
/// deleted along with the dropped table
purge: bool,
/// DROP SEQUENCE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
restrict: bool,
},
/// DECLARE - Declaring Cursor Variables
///
Expand Down Expand Up @@ -1886,15 +1888,16 @@ impl fmt::Display for Statement {
if_exists,
names,
cascade,
purge,
purge, restrict,
} => write!(
f,
"DROP {}{} {}{}{}",
"DROP {}{} {}{}{}{}",
object_type,
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(names),
if *cascade { " CASCADE" } else { "" },
if *purge { " PURGE" } else { "" }
if *purge { " PURGE" } else { "" },
if *restrict { " RESTRICT" } else { "" }
),
Statement::Discard { object_type } => {
write!(f, "DISCARD {object_type}", object_type = object_type)?;
Expand Down Expand Up @@ -2212,12 +2215,6 @@ pub enum SequenceOptions {
Cycle(bool),
}

/// CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name
/// [ AS data_type ]
/// [ INCREMENT [ BY ] increment ]
/// [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
/// [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
/// [ OWNED BY { table_name.column_name | NONE } ]
/// https://www.postgresql.org/docs/current/sql-createsequence.html
/// Will not add to vec when there are no value since all blocks are optional
impl fmt::Display for SequenceOptions {
Expand Down
9 changes: 3 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,8 @@ impl<'a> Parser<'a> {
ObjectType::Index
} 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 or SCHEMA after DROP", self.peek_token());
};
Expand All @@ -2046,6 +2048,7 @@ impl<'a> Parser<'a> {
names,
cascade,
purge,
restrict
})
}

Expand Down Expand Up @@ -4884,12 +4887,6 @@ impl<'a> Parser<'a> {
})
}

/// CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] name
/// [ AS data_type ]
/// [ INCREMENT [ BY ] increment ]
/// [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
/// [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
/// [ OWNED BY { table_name.column_name | NONE } ]
/// https://www.postgresql.org/docs/current/sql-createsequence.html
/// Will not add to vec when there are no value since all blocks are optional
pub fn parse_create_sequence(&mut self, temporary: bool) -> Result<Statement, ParserError> {
Expand Down
14 changes: 7 additions & 7 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
//! sqlparser regardless of the chosen dialect (i.e. it doesn't conflict with
//! dialect-specific parsing rules).

#[macro_use]
mod test_utils;

use matches::assert_matches;

use sqlparser::ast::*;
use sqlparser::dialect::{
AnsiDialect, BigQueryDialect, ClickHouseDialect, GenericDialect, HiveDialect, MsSqlDialect,
PostgreSqlDialect, SQLiteDialect, SnowflakeDialect,
PostgreSqlDialect, SnowflakeDialect, SQLiteDialect,
};
use sqlparser::keywords::ALL_KEYWORDS;
use sqlparser::parser::{Parser, ParserError};

use test_utils::{
all_dialects, expr_from_projection, join, number, only, table, table_alias, TestedDialects,
};

#[macro_use]
mod test_utils;

#[test]
fn parse_insert_values() {
let row = vec![
Expand Down Expand Up @@ -4214,7 +4214,7 @@ fn parse_drop_table() {
if_exists,
names,
cascade,
purge: _,
purge: _, ..
} => {
assert!(!if_exists);
assert_eq!(ObjectType::Table, object_type);
Expand All @@ -4234,7 +4234,7 @@ fn parse_drop_table() {
if_exists,
names,
cascade,
purge: _,
purge: _, ..
} => {
assert!(if_exists);
assert_eq!(ObjectType::Table, object_type);
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ fn parse_create_sequence() {
);
}

#[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");
}

#[test]
fn parse_create_table_with_defaults() {
Expand Down

0 comments on commit d7f1948

Please sign in to comment.