@@ -30,8 +30,9 @@ use sqlparser_derive::{Visit, VisitMut};
3030
3131use crate :: ast:: value:: escape_single_quote_string;
3232use crate :: ast:: {
33- display_comma_separated, display_separated, table_constraints:: TableConstraint , ArgMode ,
34- CommentDef , ConditionalStatements , CreateFunctionBody , CreateFunctionUsing ,
33+ display_comma_separated, display_separated,
34+ table_constraints:: { ForeignKeyConstraint , TableConstraint } ,
35+ ArgMode , CommentDef , ConditionalStatements , CreateFunctionBody , CreateFunctionUsing ,
3536 CreateTableLikeKind , CreateTableOptions , DataType , Expr , FileFormat , FunctionBehavior ,
3637 FunctionCalledOnNull , FunctionDeterminismSpecifier , FunctionParallel , HiveDistributionStyle ,
3738 HiveFormat , HiveIOFormat , HiveRowFormat , Ident , InitializeKind , MySQLColumnPosition ,
@@ -1558,20 +1559,14 @@ pub enum ColumnOption {
15581559 is_primary : bool ,
15591560 characteristics : Option < ConstraintCharacteristics > ,
15601561 } ,
1561- /// A referential integrity constraint (`[FOREIGN KEY REFERENCES
1562- /// <foreign_table> (<referred_columns>)
1562+ /// A referential integrity constraint (`REFERENCES <foreign_table> (<referred_columns>)
1563+ /// [ MATCH { FULL | PARTIAL | SIMPLE } ]
15631564 /// { [ON DELETE <referential_action>] [ON UPDATE <referential_action>] |
15641565 /// [ON UPDATE <referential_action>] [ON DELETE <referential_action>]
1565- /// }
1566+ /// }
15661567 /// [<constraint_characteristics>]
15671568 /// `).
1568- ForeignKey {
1569- foreign_table : ObjectName ,
1570- referred_columns : Vec < Ident > ,
1571- on_delete : Option < ReferentialAction > ,
1572- on_update : Option < ReferentialAction > ,
1573- characteristics : Option < ConstraintCharacteristics > ,
1574- } ,
1569+ ForeignKey ( ForeignKeyConstraint ) ,
15751570 /// `CHECK (<expr>)`
15761571 Check ( Expr ) ,
15771572 /// Dialect-specific options, such as:
@@ -1642,6 +1637,12 @@ pub enum ColumnOption {
16421637 Invisible ,
16431638}
16441639
1640+ impl From < ForeignKeyConstraint > for ColumnOption {
1641+ fn from ( fk : ForeignKeyConstraint ) -> Self {
1642+ ColumnOption :: ForeignKey ( fk)
1643+ }
1644+ }
1645+
16451646impl fmt:: Display for ColumnOption {
16461647 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
16471648 use ColumnOption :: * ;
@@ -1668,24 +1669,25 @@ impl fmt::Display for ColumnOption {
16681669 }
16691670 Ok ( ( ) )
16701671 }
1671- ForeignKey {
1672- foreign_table,
1673- referred_columns,
1674- on_delete,
1675- on_update,
1676- characteristics,
1677- } => {
1678- write ! ( f, "REFERENCES {foreign_table}" ) ?;
1679- if !referred_columns. is_empty ( ) {
1680- write ! ( f, " ({})" , display_comma_separated( referred_columns) ) ?;
1672+ ForeignKey ( constraint) => {
1673+ write ! ( f, "REFERENCES {}" , constraint. foreign_table) ?;
1674+ if !constraint. referred_columns . is_empty ( ) {
1675+ write ! (
1676+ f,
1677+ " ({})" ,
1678+ display_comma_separated( & constraint. referred_columns)
1679+ ) ?;
16811680 }
1682- if let Some ( action) = on_delete {
1681+ if let Some ( match_kind) = & constraint. match_kind {
1682+ write ! ( f, " {match_kind}" ) ?;
1683+ }
1684+ if let Some ( action) = & constraint. on_delete {
16831685 write ! ( f, " ON DELETE {action}" ) ?;
16841686 }
1685- if let Some ( action) = on_update {
1687+ if let Some ( action) = & constraint . on_update {
16861688 write ! ( f, " ON UPDATE {action}" ) ?;
16871689 }
1688- if let Some ( characteristics) = characteristics {
1690+ if let Some ( characteristics) = & constraint . characteristics {
16891691 write ! ( f, " {characteristics}" ) ?;
16901692 }
16911693 Ok ( ( ) )
0 commit comments