@@ -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
@@ -20338,72 +20339,88 @@ impl<'a> Parser<'a> {
2033820339 let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2033920340 //name
2034020341 let name = self.parse_object_name(false)?;
20341- //[ AS data_type ]
20342- let mut data_type: Option<DataType> = None;
20343- if self.parse_keywords(&[Keyword::AS]) {
20344- data_type = Some(self.parse_data_type()?)
20345- }
20346- let sequence_options = self.parse_create_sequence_options()?;
20347- // [ OWNED BY { table_name.column_name | NONE } ]
20348- let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20349- if self.parse_keywords(&[Keyword::NONE]) {
20350- Some(ObjectName::from(vec![Ident::new("NONE")]))
20351- } else {
20352- Some(self.parse_object_name(false)?)
20353- }
20354- } else {
20355- None
20356- };
20342+ let sequence_options = self.parse_sequence_options(true)?;
2035720343 Ok(Statement::CreateSequence {
2035820344 temporary,
2035920345 if_not_exists,
2036020346 name,
20361- data_type,
2036220347 sequence_options,
20363- owned_by,
2036420348 })
2036520349 }
2036620350
20367- fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
20368- let mut sequence_options = vec![];
20369- //[ INCREMENT [ BY ] increment ]
20370- if self.parse_keywords(&[Keyword::INCREMENT]) {
20371- if self.parse_keywords(&[Keyword::BY]) {
20372- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
20351+ /// Parse the sequence options shared by `CREATE SEQUENCE` and identity
20352+ /// columns. The options form an unordered list, each allowed at most once.
20353+ ///
20354+ /// `AS <data_type>` and `OWNED BY` are options of `CREATE SEQUENCE` only, so
20355+ /// `allow_type_and_owner` gates them off for an identity column.
20356+ fn parse_sequence_options(
20357+ &mut self,
20358+ allow_type_and_owner: bool,
20359+ ) -> Result<Vec<SequenceOptions>, ParserError> {
20360+ let mut sequence_options: Vec<SequenceOptions> = vec![];
20361+ loop {
20362+ let (option, name) = if self.parse_keyword(Keyword::INCREMENT) {
20363+ //[ INCREMENT [ BY ] increment ]
20364+ let by = self.parse_keyword(Keyword::BY);
20365+ (
20366+ SequenceOptions::IncrementBy(self.parse_number()?, by),
20367+ "INCREMENT",
20368+ )
20369+ } else if self.parse_keyword(Keyword::MINVALUE) {
20370+ //[ MINVALUE minvalue | NO MINVALUE ]
20371+ (
20372+ SequenceOptions::MinValue(Some(self.parse_number()?)),
20373+ "MINVALUE | NO MINVALUE",
20374+ )
20375+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20376+ (SequenceOptions::MinValue(None), "MINVALUE | NO MINVALUE")
20377+ } else if self.parse_keyword(Keyword::MAXVALUE) {
20378+ //[ MAXVALUE maxvalue | NO MAXVALUE ]
20379+ (
20380+ SequenceOptions::MaxValue(Some(self.parse_number()?)),
20381+ "MAXVALUE | NO MAXVALUE",
20382+ )
20383+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20384+ (SequenceOptions::MaxValue(None), "MAXVALUE | NO MAXVALUE")
20385+ } else if self.parse_keyword(Keyword::START) {
20386+ //[ START [ WITH ] start ]
20387+ let with = self.parse_keyword(Keyword::WITH);
20388+ (
20389+ SequenceOptions::StartWith(self.parse_number()?, with),
20390+ "START",
20391+ )
20392+ } else if self.parse_keyword(Keyword::CACHE) {
20393+ //[ CACHE cache ]
20394+ (SequenceOptions::Cache(self.parse_number()?), "CACHE")
20395+ } else if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20396+ // [ [ NO ] CYCLE ]
20397+ (SequenceOptions::Cycle(true), "CYCLE | NO CYCLE")
20398+ } else if self.parse_keyword(Keyword::CYCLE) {
20399+ (SequenceOptions::Cycle(false), "CYCLE | NO CYCLE")
20400+ } else if allow_type_and_owner && self.parse_keyword(Keyword::AS) {
20401+ //[ AS data_type ]
20402+ (SequenceOptions::DataType(self.parse_data_type()?), "AS")
20403+ } else if allow_type_and_owner && self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20404+ // [ OWNED BY { table_name.column_name | NONE } ]
20405+ let owner = if self.parse_keyword(Keyword::NONE) {
20406+ None
20407+ } else {
20408+ Some(self.parse_object_name(false)?)
20409+ };
20410+ (SequenceOptions::OwnedBy(owner), "OWNED BY")
2037320411 } else {
20374- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
20375- }
20376- }
20377- //[ MINVALUE minvalue | NO MINVALUE ]
20378- if self.parse_keyword(Keyword::MINVALUE) {
20379- sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
20380- } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20381- sequence_options.push(SequenceOptions::MinValue(None));
20382- }
20383- //[ MAXVALUE maxvalue | NO MAXVALUE ]
20384- if self.parse_keywords(&[Keyword::MAXVALUE]) {
20385- sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
20386- } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20387- sequence_options.push(SequenceOptions::MaxValue(None));
20388- }
20412+ break;
20413+ };
2038920414
20390- //[ START [ WITH ] start ]
20391- if self.parse_keywords(&[Keyword::START]) {
20392- if self.parse_keywords(&[Keyword::WITH]) {
20393- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
20394- } else {
20395- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
20415+ if sequence_options
20416+ .iter()
20417+ .any(|seen| discriminant(seen) == discriminant(&option))
20418+ {
20419+ return Err(ParserError::ParserError(format!(
20420+ "{name} specified more than once"
20421+ )));
2039620422 }
20397- }
20398- //[ CACHE cache ]
20399- if self.parse_keywords(&[Keyword::CACHE]) {
20400- sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
20401- }
20402- // [ [ NO ] CYCLE ]
20403- if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20404- sequence_options.push(SequenceOptions::Cycle(true));
20405- } else if self.parse_keywords(&[Keyword::CYCLE]) {
20406- sequence_options.push(SequenceOptions::Cycle(false));
20423+ sequence_options.push(option);
2040720424 }
2040820425
2040920426 Ok(sequence_options)
0 commit comments