Reused CheckConstraint in ColumnOption (#2063)
Some checks failed
license / Release Audit Tool (RAT) (push) Has been cancelled
Rust / codestyle (push) Has been cancelled
Rust / lint (push) Has been cancelled
Rust / benchmark-lint (push) Has been cancelled
Rust / compile (push) Has been cancelled
Rust / docs (push) Has been cancelled
Rust / compile-no-std (push) Has been cancelled
Rust / test (beta) (push) Has been cancelled
Rust / test (nightly) (push) Has been cancelled
Rust / test (stable) (push) Has been cancelled

This commit is contained in:
Luca Cappelletti 2025-10-16 10:38:50 +02:00 committed by GitHub
parent 218f43cf2d
commit f861566c8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 6 deletions

View file

@ -31,7 +31,7 @@ use sqlparser_derive::{Visit, VisitMut};
use crate::ast::value::escape_single_quote_string;
use crate::ast::{
display_comma_separated, display_separated,
table_constraints::{ForeignKeyConstraint, TableConstraint},
table_constraints::{CheckConstraint, ForeignKeyConstraint, TableConstraint},
ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
CreateFunctionUsing, CreateTableLikeKind, CreateTableOptions, CreateViewParams, DataType, Expr,
FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDesc, FunctionDeterminismSpecifier,
@ -1569,7 +1569,7 @@ pub enum ColumnOption {
/// `).
ForeignKey(ForeignKeyConstraint),
/// `CHECK (<expr>)`
Check(Expr),
Check(CheckConstraint),
/// Dialect-specific options, such as:
/// - MySQL's `AUTO_INCREMENT` or SQLite's `AUTOINCREMENT`
/// - ...
@ -1638,6 +1638,11 @@ pub enum ColumnOption {
Invisible,
}
impl From<CheckConstraint> for ColumnOption {
fn from(c: CheckConstraint) -> Self {
ColumnOption::Check(c)
}
}
impl From<ForeignKeyConstraint> for ColumnOption {
fn from(fk: ForeignKeyConstraint) -> Self {
ColumnOption::ForeignKey(fk)
@ -1693,7 +1698,7 @@ impl fmt::Display for ColumnOption {
}
Ok(())
}
Check(expr) => write!(f, "CHECK ({expr})"),
Check(constraint) => write!(f, "{constraint}"),
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
Collation(n) => write!(f, "COLLATE {n}"),

View file

@ -741,8 +741,8 @@ impl Spanned for ColumnOption {
ColumnOption::Ephemeral(expr) => expr.as_ref().map_or(Span::empty(), |e| e.span()),
ColumnOption::Alias(expr) => expr.span(),
ColumnOption::Unique { .. } => Span::empty(),
ColumnOption::Check(constraint) => constraint.span(),
ColumnOption::ForeignKey(constraint) => constraint.span(),
ColumnOption::Check(expr) => expr.span(),
ColumnOption::DialectSpecific(_) => Span::empty(),
ColumnOption::CharacterSet(object_name) => object_name.span(),
ColumnOption::Collation(object_name) => object_name.span(),

View file

@ -8104,7 +8104,14 @@ impl<'a> Parser<'a> {
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
self.expect_token(&Token::RParen)?;
Ok(Some(ColumnOption::Check(expr)))
Ok(Some(
CheckConstraint {
name: None, // Column-level check constraints don't have names
expr: Box::new(expr),
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
}
.into(),
))
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
&& dialect_of!(self is MySqlDialect | GenericDialect)
{