MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / parse_optional_table_constraint

Method parse_optional_table_constraint

src/sql-parser/src/parser.rs:5452–5519  ·  view source on GitHub ↗
(
        &mut self,
    )

Source from the content-addressed store, hash-verified

5450 }
5451
5452 fn parse_optional_table_constraint(
5453 &mut self,
5454 ) -> Result<Option<TableConstraint<Raw>>, ParserError> {
5455 let name = if self.parse_keyword(CONSTRAINT) {
5456 Some(self.parse_identifier()?)
5457 } else {
5458 None
5459 };
5460 match self.next_token() {
5461 Some(Token::Keyword(PRIMARY)) => {
5462 self.expect_keyword(KEY)?;
5463 let columns = self.parse_parenthesized_column_list(Mandatory)?;
5464 Ok(Some(TableConstraint::Unique {
5465 name,
5466 columns,
5467 is_primary: true,
5468 nulls_not_distinct: false,
5469 }))
5470 }
5471 Some(Token::Keyword(UNIQUE)) => {
5472 let nulls_not_distinct = if self.parse_keyword(NULLS) {
5473 self.expect_keywords(&[NOT, DISTINCT])?;
5474 true
5475 } else {
5476 false
5477 };
5478
5479 let columns = self.parse_parenthesized_column_list(Mandatory)?;
5480 Ok(Some(TableConstraint::Unique {
5481 name,
5482 columns,
5483 is_primary: false,
5484 nulls_not_distinct,
5485 }))
5486 }
5487 Some(Token::Keyword(FOREIGN)) => {
5488 self.expect_keyword(KEY)?;
5489 let columns = self.parse_parenthesized_column_list(Mandatory)?;
5490 self.expect_keyword(REFERENCES)?;
5491 let foreign_table = self.parse_raw_name()?;
5492 let referred_columns = self.parse_parenthesized_column_list(Mandatory)?;
5493 Ok(Some(TableConstraint::ForeignKey {
5494 name,
5495 columns,
5496 foreign_table,
5497 referred_columns,
5498 }))
5499 }
5500 Some(Token::Keyword(CHECK)) => {
5501 self.expect_token(&Token::LParen)?;
5502 let expr = Box::new(self.parse_expr()?);
5503 self.expect_token(&Token::RParen)?;
5504 Ok(Some(TableConstraint::Check { name, expr }))
5505 }
5506 unexpected => {
5507 if name.is_some() {
5508 self.expected(
5509 self.peek_prev_pos(),

Callers 2

parse_columnsMethod · 0.80

Calls 13

parse_keywordMethod · 0.80
parse_identifierMethod · 0.80
next_tokenMethod · 0.80
expect_keywordMethod · 0.80
expect_keywordsMethod · 0.80
parse_raw_nameMethod · 0.80
expect_tokenMethod · 0.80
parse_exprMethod · 0.80
is_someMethod · 0.80
expectedMethod · 0.80
peek_prev_posMethod · 0.80

Tested by

no test coverage detected