@@ -23,6 +23,7 @@ use alloc::{
2323};
2424use core::{
2525 fmt::{self, Display},
26+ mem::discriminant,
2627 str::FromStr,
2728};
2829#[cfg(feature = "std")]
@@ -9819,7 +9820,7 @@ impl<'a> Parser<'a> {
98199820 if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS, Keyword::IDENTITY]) {
98209821 let mut sequence_options = vec![];
98219822 if self.expect_token(&Token::LParen).is_ok() {
9822- sequence_options = self.parse_create_sequence_options( )?;
9823+ sequence_options = self.parse_sequence_options(false )?;
98239824 self.expect_token(&Token::RParen)?;
98249825 }
98259826 Ok(Some(ColumnOption::Generated {
@@ -9837,7 +9838,7 @@ impl<'a> Parser<'a> {
98379838 ]) {
98389839 let mut sequence_options = vec![];
98399840 if self.expect_token(&Token::LParen).is_ok() {
9840- sequence_options = self.parse_create_sequence_options( )?;
9841+ sequence_options = self.parse_sequence_options(false )?;
98419842 self.expect_token(&Token::RParen)?;
98429843 }
98439844 Ok(Some(ColumnOption::Generated {
@@ -10975,7 +10976,7 @@ impl<'a> Parser<'a> {
1097510976
1097610977 if self.peek_token_ref().token == Token::LParen {
1097710978 self.expect_token(&Token::LParen)?;
10978- sequence_options = Some(self.parse_create_sequence_options( )?);
10979+ sequence_options = Some(self.parse_sequence_options(false )?);
1097910980 self.expect_token(&Token::RParen)?;
1098010981 }
1098110982
@@ -20316,72 +20317,88 @@ impl<'a> Parser<'a> {
2031620317 let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2031720318 //name
2031820319 let name = self.parse_object_name(false)?;
20319- //[ AS data_type ]
20320- let mut data_type: Option<DataType> = None;
20321- if self.parse_keywords(&[Keyword::AS]) {
20322- data_type = Some(self.parse_data_type()?)
20323- }
20324- let sequence_options = self.parse_create_sequence_options()?;
20325- // [ OWNED BY { table_name.column_name | NONE } ]
20326- let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20327- if self.parse_keywords(&[Keyword::NONE]) {
20328- Some(ObjectName::from(vec![Ident::new("NONE")]))
20329- } else {
20330- Some(self.parse_object_name(false)?)
20331- }
20332- } else {
20333- None
20334- };
20320+ let sequence_options = self.parse_sequence_options(true)?;
2033520321 Ok(Statement::CreateSequence {
2033620322 temporary,
2033720323 if_not_exists,
2033820324 name,
20339- data_type,
2034020325 sequence_options,
20341- owned_by,
2034220326 })
2034320327 }
2034420328
20345- fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
20346- let mut sequence_options = vec![];
20347- //[ INCREMENT [ BY ] increment ]
20348- if self.parse_keywords(&[Keyword::INCREMENT]) {
20349- if self.parse_keywords(&[Keyword::BY]) {
20350- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
20329+ /// Parse the sequence options shared by `CREATE SEQUENCE` and identity
20330+ /// columns. The options form an unordered list, each allowed at most once.
20331+ ///
20332+ /// `AS <data_type>` and `OWNED BY` are options of `CREATE SEQUENCE` only, so
20333+ /// `allow_type_and_owner` gates them off for an identity column.
20334+ fn parse_sequence_options(
20335+ &mut self,
20336+ allow_type_and_owner: bool,
20337+ ) -> Result<Vec<SequenceOptions>, ParserError> {
20338+ let mut sequence_options: Vec<SequenceOptions> = vec![];
20339+ loop {
20340+ let (option, name) = if self.parse_keyword(Keyword::INCREMENT) {
20341+ //[ INCREMENT [ BY ] increment ]
20342+ let by = self.parse_keyword(Keyword::BY);
20343+ (
20344+ SequenceOptions::IncrementBy(self.parse_number()?, by),
20345+ "INCREMENT",
20346+ )
20347+ } else if self.parse_keyword(Keyword::MINVALUE) {
20348+ //[ MINVALUE minvalue | NO MINVALUE ]
20349+ (
20350+ SequenceOptions::MinValue(Some(self.parse_number()?)),
20351+ "MINVALUE | NO MINVALUE",
20352+ )
20353+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20354+ (SequenceOptions::MinValue(None), "MINVALUE | NO MINVALUE")
20355+ } else if self.parse_keyword(Keyword::MAXVALUE) {
20356+ //[ MAXVALUE maxvalue | NO MAXVALUE ]
20357+ (
20358+ SequenceOptions::MaxValue(Some(self.parse_number()?)),
20359+ "MAXVALUE | NO MAXVALUE",
20360+ )
20361+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20362+ (SequenceOptions::MaxValue(None), "MAXVALUE | NO MAXVALUE")
20363+ } else if self.parse_keyword(Keyword::START) {
20364+ //[ START [ WITH ] start ]
20365+ let with = self.parse_keyword(Keyword::WITH);
20366+ (
20367+ SequenceOptions::StartWith(self.parse_number()?, with),
20368+ "START",
20369+ )
20370+ } else if self.parse_keyword(Keyword::CACHE) {
20371+ //[ CACHE cache ]
20372+ (SequenceOptions::Cache(self.parse_number()?), "CACHE")
20373+ } else if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20374+ // [ [ NO ] CYCLE ]
20375+ (SequenceOptions::Cycle(true), "CYCLE | NO CYCLE")
20376+ } else if self.parse_keyword(Keyword::CYCLE) {
20377+ (SequenceOptions::Cycle(false), "CYCLE | NO CYCLE")
20378+ } else if allow_type_and_owner && self.parse_keyword(Keyword::AS) {
20379+ //[ AS data_type ]
20380+ (SequenceOptions::DataType(self.parse_data_type()?), "AS")
20381+ } else if allow_type_and_owner && self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20382+ // [ OWNED BY { table_name.column_name | NONE } ]
20383+ let owner = if self.parse_keyword(Keyword::NONE) {
20384+ None
20385+ } else {
20386+ Some(self.parse_object_name(false)?)
20387+ };
20388+ (SequenceOptions::OwnedBy(owner), "OWNED BY")
2035120389 } else {
20352- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
20353- }
20354- }
20355- //[ MINVALUE minvalue | NO MINVALUE ]
20356- if self.parse_keyword(Keyword::MINVALUE) {
20357- sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
20358- } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20359- sequence_options.push(SequenceOptions::MinValue(None));
20360- }
20361- //[ MAXVALUE maxvalue | NO MAXVALUE ]
20362- if self.parse_keywords(&[Keyword::MAXVALUE]) {
20363- sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
20364- } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20365- sequence_options.push(SequenceOptions::MaxValue(None));
20366- }
20390+ break;
20391+ };
2036720392
20368- //[ START [ WITH ] start ]
20369- if self.parse_keywords(&[Keyword::START]) {
20370- if self.parse_keywords(&[Keyword::WITH]) {
20371- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
20372- } else {
20373- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
20393+ if sequence_options
20394+ .iter()
20395+ .any(|seen| discriminant(seen) == discriminant(&option))
20396+ {
20397+ return Err(ParserError::ParserError(format!(
20398+ "{name} specified more than once"
20399+ )));
2037420400 }
20375- }
20376- //[ CACHE cache ]
20377- if self.parse_keywords(&[Keyword::CACHE]) {
20378- sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
20379- }
20380- // [ [ NO ] CYCLE ]
20381- if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20382- sequence_options.push(SequenceOptions::Cycle(true));
20383- } else if self.parse_keywords(&[Keyword::CYCLE]) {
20384- sequence_options.push(SequenceOptions::Cycle(false));
20401+ sequence_options.push(option);
2038520402 }
2038620403
2038720404 Ok(sequence_options)
0 commit comments