@@ -23,6 +23,7 @@ use alloc::{
2323};
2424use core::{
2525 fmt::{self, Display},
26+ mem::discriminant,
2627 str::FromStr,
2728};
2829#[cfg(feature = "std")]
@@ -9829,7 +9830,7 @@ impl<'a> Parser<'a> {
98299830 if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS, Keyword::IDENTITY]) {
98309831 let mut sequence_options = vec![];
98319832 if self.expect_token(&Token::LParen).is_ok() {
9832- sequence_options = self.parse_create_sequence_options( )?;
9833+ sequence_options = self.parse_sequence_options(false )?;
98339834 self.expect_token(&Token::RParen)?;
98349835 }
98359836 Ok(Some(ColumnOption::Generated {
@@ -9847,7 +9848,7 @@ impl<'a> Parser<'a> {
98479848 ]) {
98489849 let mut sequence_options = vec![];
98499850 if self.expect_token(&Token::LParen).is_ok() {
9850- sequence_options = self.parse_create_sequence_options( )?;
9851+ sequence_options = self.parse_sequence_options(false )?;
98519852 self.expect_token(&Token::RParen)?;
98529853 }
98539854 Ok(Some(ColumnOption::Generated {
@@ -10985,7 +10986,7 @@ impl<'a> Parser<'a> {
1098510986
1098610987 if self.peek_token_ref().token == Token::LParen {
1098710988 self.expect_token(&Token::LParen)?;
10988- sequence_options = Some(self.parse_create_sequence_options( )?);
10989+ sequence_options = Some(self.parse_sequence_options(false )?);
1098910990 self.expect_token(&Token::RParen)?;
1099010991 }
1099110992
@@ -20310,72 +20311,88 @@ impl<'a> Parser<'a> {
2031020311 let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2031120312 //name
2031220313 let name = self.parse_object_name(false)?;
20313- //[ AS data_type ]
20314- let mut data_type: Option<DataType> = None;
20315- if self.parse_keywords(&[Keyword::AS]) {
20316- data_type = Some(self.parse_data_type()?)
20317- }
20318- let sequence_options = self.parse_create_sequence_options()?;
20319- // [ OWNED BY { table_name.column_name | NONE } ]
20320- let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20321- if self.parse_keywords(&[Keyword::NONE]) {
20322- Some(ObjectName::from(vec![Ident::new("NONE")]))
20323- } else {
20324- Some(self.parse_object_name(false)?)
20325- }
20326- } else {
20327- None
20328- };
20314+ let sequence_options = self.parse_sequence_options(true)?;
2032920315 Ok(Statement::CreateSequence {
2033020316 temporary,
2033120317 if_not_exists,
2033220318 name,
20333- data_type,
2033420319 sequence_options,
20335- owned_by,
2033620320 })
2033720321 }
2033820322
20339- fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
20340- let mut sequence_options = vec![];
20341- //[ INCREMENT [ BY ] increment ]
20342- if self.parse_keywords(&[Keyword::INCREMENT]) {
20343- if self.parse_keywords(&[Keyword::BY]) {
20344- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
20323+ /// Parse the sequence options shared by `CREATE SEQUENCE` and identity
20324+ /// columns. The options form an unordered list, each allowed at most once.
20325+ ///
20326+ /// `AS <data_type>` and `OWNED BY` are options of `CREATE SEQUENCE` only, so
20327+ /// `allow_type_and_owner` gates them off for an identity column.
20328+ fn parse_sequence_options(
20329+ &mut self,
20330+ allow_type_and_owner: bool,
20331+ ) -> Result<Vec<SequenceOptions>, ParserError> {
20332+ let mut sequence_options: Vec<SequenceOptions> = vec![];
20333+ loop {
20334+ let (option, name) = if self.parse_keyword(Keyword::INCREMENT) {
20335+ //[ INCREMENT [ BY ] increment ]
20336+ let by = self.parse_keyword(Keyword::BY);
20337+ (
20338+ SequenceOptions::IncrementBy(self.parse_number()?, by),
20339+ "INCREMENT",
20340+ )
20341+ } else if self.parse_keyword(Keyword::MINVALUE) {
20342+ //[ MINVALUE minvalue | NO MINVALUE ]
20343+ (
20344+ SequenceOptions::MinValue(Some(self.parse_number()?)),
20345+ "MINVALUE | NO MINVALUE",
20346+ )
20347+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20348+ (SequenceOptions::MinValue(None), "MINVALUE | NO MINVALUE")
20349+ } else if self.parse_keyword(Keyword::MAXVALUE) {
20350+ //[ MAXVALUE maxvalue | NO MAXVALUE ]
20351+ (
20352+ SequenceOptions::MaxValue(Some(self.parse_number()?)),
20353+ "MAXVALUE | NO MAXVALUE",
20354+ )
20355+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20356+ (SequenceOptions::MaxValue(None), "MAXVALUE | NO MAXVALUE")
20357+ } else if self.parse_keyword(Keyword::START) {
20358+ //[ START [ WITH ] start ]
20359+ let with = self.parse_keyword(Keyword::WITH);
20360+ (
20361+ SequenceOptions::StartWith(self.parse_number()?, with),
20362+ "START",
20363+ )
20364+ } else if self.parse_keyword(Keyword::CACHE) {
20365+ //[ CACHE cache ]
20366+ (SequenceOptions::Cache(self.parse_number()?), "CACHE")
20367+ } else if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20368+ // [ [ NO ] CYCLE ]
20369+ (SequenceOptions::Cycle(true), "CYCLE | NO CYCLE")
20370+ } else if self.parse_keyword(Keyword::CYCLE) {
20371+ (SequenceOptions::Cycle(false), "CYCLE | NO CYCLE")
20372+ } else if allow_type_and_owner && self.parse_keyword(Keyword::AS) {
20373+ //[ AS data_type ]
20374+ (SequenceOptions::DataType(self.parse_data_type()?), "AS")
20375+ } else if allow_type_and_owner && self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20376+ // [ OWNED BY { table_name.column_name | NONE } ]
20377+ let owner = if self.parse_keyword(Keyword::NONE) {
20378+ None
20379+ } else {
20380+ Some(self.parse_object_name(false)?)
20381+ };
20382+ (SequenceOptions::OwnedBy(owner), "OWNED BY")
2034520383 } else {
20346- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
20347- }
20348- }
20349- //[ MINVALUE minvalue | NO MINVALUE ]
20350- if self.parse_keyword(Keyword::MINVALUE) {
20351- sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
20352- } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20353- sequence_options.push(SequenceOptions::MinValue(None));
20354- }
20355- //[ MAXVALUE maxvalue | NO MAXVALUE ]
20356- if self.parse_keywords(&[Keyword::MAXVALUE]) {
20357- sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
20358- } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20359- sequence_options.push(SequenceOptions::MaxValue(None));
20360- }
20384+ break;
20385+ };
2036120386
20362- //[ START [ WITH ] start ]
20363- if self.parse_keywords(&[Keyword::START]) {
20364- if self.parse_keywords(&[Keyword::WITH]) {
20365- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
20366- } else {
20367- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
20387+ if sequence_options
20388+ .iter()
20389+ .any(|seen| discriminant(seen) == discriminant(&option))
20390+ {
20391+ return Err(ParserError::ParserError(format!(
20392+ "{name} specified more than once"
20393+ )));
2036820394 }
20369- }
20370- //[ CACHE cache ]
20371- if self.parse_keywords(&[Keyword::CACHE]) {
20372- sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
20373- }
20374- // [ [ NO ] CYCLE ]
20375- if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20376- sequence_options.push(SequenceOptions::Cycle(true));
20377- } else if self.parse_keywords(&[Keyword::CYCLE]) {
20378- sequence_options.push(SequenceOptions::Cycle(false));
20395+ sequence_options.push(option);
2037920396 }
2038020397
2038120398 Ok(sequence_options)
0 commit comments