@@ -30,14 +30,15 @@ use sqlparser_derive::{Visit, VisitMut};
3030
3131use crate :: ast:: value:: escape_single_quote_string;
3232use crate :: ast:: {
33- display_comma_separated, display_separated, ArgMode , AttachedToken , CommentDef ,
34- ConditionalStatements , CreateFunctionBody , CreateFunctionUsing , CreateTableLikeKind ,
35- CreateTableOptions , CreateViewParams , DataType , Expr , FileFormat , FunctionBehavior ,
36- FunctionCalledOnNull , FunctionDesc , FunctionDeterminismSpecifier , FunctionParallel ,
37- HiveDistributionStyle , HiveFormat , HiveIOFormat , HiveRowFormat , HiveSetLocation , Ident ,
38- InitializeKind , MySQLColumnPosition , ObjectName , OnCommit , OneOrManyWithParens ,
39- OperateFunctionArg , OrderByExpr , ProjectionSelect , Query , RefreshModeKind , RowAccessPolicy ,
40- SequenceOptions , Spanned , SqlOption , StorageSerializationPolicy , TableConstraint , TableVersion ,
33+ display_comma_separated, display_separated,
34+ table_constraints:: { ForeignKeyConstraint , TableConstraint } ,
35+ ArgMode , AttachedToken , CommentDef , ConditionalStatements , CreateFunctionBody ,
36+ CreateFunctionUsing , CreateTableLikeKind , CreateTableOptions , CreateViewParams , DataType , Expr ,
37+ FileFormat , FunctionBehavior , FunctionCalledOnNull , FunctionDesc , FunctionDeterminismSpecifier ,
38+ FunctionParallel , HiveDistributionStyle , HiveFormat , HiveIOFormat , HiveRowFormat ,
39+ HiveSetLocation , Ident , InitializeKind , MySQLColumnPosition , ObjectName , OnCommit ,
40+ OneOrManyWithParens , OperateFunctionArg , OrderByExpr , ProjectionSelect , Query , RefreshModeKind ,
41+ RowAccessPolicy , SequenceOptions , Spanned , SqlOption , StorageSerializationPolicy , TableVersion ,
4142 Tag , TriggerEvent , TriggerExecBody , TriggerObject , TriggerPeriod , TriggerReferencing , Value ,
4243 ValueWithSpan , WrappedCollection ,
4344} ;
@@ -1559,20 +1560,14 @@ pub enum ColumnOption {
15591560 is_primary : bool ,
15601561 characteristics : Option < ConstraintCharacteristics > ,
15611562 } ,
1562- /// A referential integrity constraint (`[FOREIGN KEY REFERENCES
1563- /// <foreign_table> (<referred_columns>)
1563+ /// A referential integrity constraint (`REFERENCES <foreign_table> (<referred_columns>)
1564+ /// [ MATCH { FULL | PARTIAL | SIMPLE } ]
15641565 /// { [ON DELETE <referential_action>] [ON UPDATE <referential_action>] |
15651566 /// [ON UPDATE <referential_action>] [ON DELETE <referential_action>]
1566- /// }
1567+ /// }
15671568 /// [<constraint_characteristics>]
15681569 /// `).
1569- ForeignKey {
1570- foreign_table : ObjectName ,
1571- referred_columns : Vec < Ident > ,
1572- on_delete : Option < ReferentialAction > ,
1573- on_update : Option < ReferentialAction > ,
1574- characteristics : Option < ConstraintCharacteristics > ,
1575- } ,
1570+ ForeignKey ( ForeignKeyConstraint ) ,
15761571 /// `CHECK (<expr>)`
15771572 Check ( Expr ) ,
15781573 /// Dialect-specific options, such as:
@@ -1643,6 +1638,12 @@ pub enum ColumnOption {
16431638 Invisible ,
16441639}
16451640
1641+ impl From < ForeignKeyConstraint > for ColumnOption {
1642+ fn from ( fk : ForeignKeyConstraint ) -> Self {
1643+ ColumnOption :: ForeignKey ( fk)
1644+ }
1645+ }
1646+
16461647impl fmt:: Display for ColumnOption {
16471648 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
16481649 use ColumnOption :: * ;
@@ -1669,24 +1670,25 @@ impl fmt::Display for ColumnOption {
16691670 }
16701671 Ok ( ( ) )
16711672 }
1672- ForeignKey {
1673- foreign_table,
1674- referred_columns,
1675- on_delete,
1676- on_update,
1677- characteristics,
1678- } => {
1679- write ! ( f, "REFERENCES {foreign_table}" ) ?;
1680- if !referred_columns. is_empty ( ) {
1681- write ! ( f, " ({})" , display_comma_separated( referred_columns) ) ?;
1673+ ForeignKey ( constraint) => {
1674+ write ! ( f, "REFERENCES {}" , constraint. foreign_table) ?;
1675+ if !constraint. referred_columns . is_empty ( ) {
1676+ write ! (
1677+ f,
1678+ " ({})" ,
1679+ display_comma_separated( & constraint. referred_columns)
1680+ ) ?;
16821681 }
1683- if let Some ( action) = on_delete {
1682+ if let Some ( match_kind) = & constraint. match_kind {
1683+ write ! ( f, " {match_kind}" ) ?;
1684+ }
1685+ if let Some ( action) = & constraint. on_delete {
16841686 write ! ( f, " ON DELETE {action}" ) ?;
16851687 }
1686- if let Some ( action) = on_update {
1688+ if let Some ( action) = & constraint . on_update {
16871689 write ! ( f, " ON UPDATE {action}" ) ?;
16881690 }
1689- if let Some ( characteristics) = characteristics {
1691+ if let Some ( characteristics) = & constraint . characteristics {
16901692 write ! ( f, " {characteristics}" ) ?;
16911693 }
16921694 Ok ( ( ) )
0 commit comments