@@ -23,6 +23,7 @@ use alloc::{
2323};
2424use core::{
2525 fmt::{self, Display},
26+ mem::discriminant,
2627 str::FromStr,
2728};
2829#[cfg(feature = "std")]
@@ -9791,7 +9792,7 @@ impl<'a> Parser<'a> {
97919792 if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS, Keyword::IDENTITY]) {
97929793 let mut sequence_options = vec![];
97939794 if self.expect_token(&Token::LParen).is_ok() {
9794- sequence_options = self.parse_create_sequence_options( )?;
9795+ sequence_options = self.parse_sequence_options(false )?;
97959796 self.expect_token(&Token::RParen)?;
97969797 }
97979798 Ok(Some(ColumnOption::Generated {
@@ -9809,7 +9810,7 @@ impl<'a> Parser<'a> {
98099810 ]) {
98109811 let mut sequence_options = vec![];
98119812 if self.expect_token(&Token::LParen).is_ok() {
9812- sequence_options = self.parse_create_sequence_options( )?;
9813+ sequence_options = self.parse_sequence_options(false )?;
98139814 self.expect_token(&Token::RParen)?;
98149815 }
98159816 Ok(Some(ColumnOption::Generated {
@@ -10945,7 +10946,7 @@ impl<'a> Parser<'a> {
1094510946
1094610947 if self.peek_token_ref().token == Token::LParen {
1094710948 self.expect_token(&Token::LParen)?;
10948- sequence_options = Some(self.parse_create_sequence_options( )?);
10949+ sequence_options = Some(self.parse_sequence_options(false )?);
1094910950 self.expect_token(&Token::RParen)?;
1095010951 }
1095110952
@@ -20256,72 +20257,88 @@ impl<'a> Parser<'a> {
2025620257 let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2025720258 //name
2025820259 let name = self.parse_object_name(false)?;
20259- //[ AS data_type ]
20260- let mut data_type: Option<DataType> = None;
20261- if self.parse_keywords(&[Keyword::AS]) {
20262- data_type = Some(self.parse_data_type()?)
20263- }
20264- let sequence_options = self.parse_create_sequence_options()?;
20265- // [ OWNED BY { table_name.column_name | NONE } ]
20266- let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20267- if self.parse_keywords(&[Keyword::NONE]) {
20268- Some(ObjectName::from(vec![Ident::new("NONE")]))
20269- } else {
20270- Some(self.parse_object_name(false)?)
20271- }
20272- } else {
20273- None
20274- };
20260+ let sequence_options = self.parse_sequence_options(true)?;
2027520261 Ok(Statement::CreateSequence {
2027620262 temporary,
2027720263 if_not_exists,
2027820264 name,
20279- data_type,
2028020265 sequence_options,
20281- owned_by,
2028220266 })
2028320267 }
2028420268
20285- fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
20286- let mut sequence_options = vec![];
20287- //[ INCREMENT [ BY ] increment ]
20288- if self.parse_keywords(&[Keyword::INCREMENT]) {
20289- if self.parse_keywords(&[Keyword::BY]) {
20290- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
20269+ /// Parse the sequence options shared by `CREATE SEQUENCE` and identity
20270+ /// columns. The options form an unordered list, each allowed at most once.
20271+ ///
20272+ /// `AS <data_type>` and `OWNED BY` are options of `CREATE SEQUENCE` only, so
20273+ /// `allow_type_and_owner` gates them off for an identity column.
20274+ fn parse_sequence_options(
20275+ &mut self,
20276+ allow_type_and_owner: bool,
20277+ ) -> Result<Vec<SequenceOptions>, ParserError> {
20278+ let mut sequence_options: Vec<SequenceOptions> = vec![];
20279+ loop {
20280+ let (option, name) = if self.parse_keyword(Keyword::INCREMENT) {
20281+ //[ INCREMENT [ BY ] increment ]
20282+ let by = self.parse_keyword(Keyword::BY);
20283+ (
20284+ SequenceOptions::IncrementBy(self.parse_number()?, by),
20285+ "INCREMENT",
20286+ )
20287+ } else if self.parse_keyword(Keyword::MINVALUE) {
20288+ //[ MINVALUE minvalue | NO MINVALUE ]
20289+ (
20290+ SequenceOptions::MinValue(Some(self.parse_number()?)),
20291+ "MINVALUE | NO MINVALUE",
20292+ )
20293+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20294+ (SequenceOptions::MinValue(None), "MINVALUE | NO MINVALUE")
20295+ } else if self.parse_keyword(Keyword::MAXVALUE) {
20296+ //[ MAXVALUE maxvalue | NO MAXVALUE ]
20297+ (
20298+ SequenceOptions::MaxValue(Some(self.parse_number()?)),
20299+ "MAXVALUE | NO MAXVALUE",
20300+ )
20301+ } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20302+ (SequenceOptions::MaxValue(None), "MAXVALUE | NO MAXVALUE")
20303+ } else if self.parse_keyword(Keyword::START) {
20304+ //[ START [ WITH ] start ]
20305+ let with = self.parse_keyword(Keyword::WITH);
20306+ (
20307+ SequenceOptions::StartWith(self.parse_number()?, with),
20308+ "START",
20309+ )
20310+ } else if self.parse_keyword(Keyword::CACHE) {
20311+ //[ CACHE cache ]
20312+ (SequenceOptions::Cache(self.parse_number()?), "CACHE")
20313+ } else if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20314+ // [ [ NO ] CYCLE ]
20315+ (SequenceOptions::Cycle(true), "CYCLE | NO CYCLE")
20316+ } else if self.parse_keyword(Keyword::CYCLE) {
20317+ (SequenceOptions::Cycle(false), "CYCLE | NO CYCLE")
20318+ } else if allow_type_and_owner && self.parse_keyword(Keyword::AS) {
20319+ //[ AS data_type ]
20320+ (SequenceOptions::DataType(self.parse_data_type()?), "AS")
20321+ } else if allow_type_and_owner && self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
20322+ // [ OWNED BY { table_name.column_name | NONE } ]
20323+ let owner = if self.parse_keyword(Keyword::NONE) {
20324+ None
20325+ } else {
20326+ Some(self.parse_object_name(false)?)
20327+ };
20328+ (SequenceOptions::OwnedBy(owner), "OWNED BY")
2029120329 } else {
20292- sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
20293- }
20294- }
20295- //[ MINVALUE minvalue | NO MINVALUE ]
20296- if self.parse_keyword(Keyword::MINVALUE) {
20297- sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
20298- } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
20299- sequence_options.push(SequenceOptions::MinValue(None));
20300- }
20301- //[ MAXVALUE maxvalue | NO MAXVALUE ]
20302- if self.parse_keywords(&[Keyword::MAXVALUE]) {
20303- sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
20304- } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
20305- sequence_options.push(SequenceOptions::MaxValue(None));
20306- }
20330+ break;
20331+ };
2030720332
20308- //[ START [ WITH ] start ]
20309- if self.parse_keywords(&[Keyword::START]) {
20310- if self.parse_keywords(&[Keyword::WITH]) {
20311- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
20312- } else {
20313- sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
20333+ if sequence_options
20334+ .iter()
20335+ .any(|seen| discriminant(seen) == discriminant(&option))
20336+ {
20337+ return Err(ParserError::ParserError(format!(
20338+ "{name} specified more than once"
20339+ )));
2031420340 }
20315- }
20316- //[ CACHE cache ]
20317- if self.parse_keywords(&[Keyword::CACHE]) {
20318- sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
20319- }
20320- // [ [ NO ] CYCLE ]
20321- if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
20322- sequence_options.push(SequenceOptions::Cycle(true));
20323- } else if self.parse_keywords(&[Keyword::CYCLE]) {
20324- sequence_options.push(SequenceOptions::Cycle(false));
20341+ sequence_options.push(option);
2032520342 }
2032620343
2032720344 Ok(sequence_options)
0 commit comments