@@ -31,7 +31,9 @@ use sqlparser_derive::{Visit, VisitMut};
3131use crate :: ast:: value:: escape_single_quote_string;
3232use crate :: ast:: {
3333 display_comma_separated, display_separated,
34- table_constraints:: { PrimaryKeyConstraint , TableConstraint , UniqueConstraint } ,
34+ table_constraints:: {
35+ ForeignKeyConstraint , PrimaryKeyConstraint , TableConstraint , UniqueConstraint ,
36+ } ,
3537 ArgMode , AttachedToken , CommentDef , ConditionalStatements , CreateFunctionBody ,
3638 CreateFunctionUsing , CreateTableLikeKind , CreateTableOptions , CreateViewParams , DataType , Expr ,
3739 FileFormat , FunctionBehavior , FunctionCalledOnNull , FunctionDesc , FunctionDeterminismSpecifier ,
@@ -1575,20 +1577,14 @@ pub enum ColumnOption {
15751577 PrimaryKey ( PrimaryKeyConstraint ) ,
15761578 /// `UNIQUE [<constraint_characteristics>]`
15771579 Unique ( UniqueConstraint ) ,
1578- /// A referential integrity constraint (`[FOREIGN KEY REFERENCES
1579- /// <foreign_table> (<referred_columns>)
1580+ /// A referential integrity constraint (`REFERENCES <foreign_table> (<referred_columns>)
1581+ /// [ MATCH { FULL | PARTIAL | SIMPLE } ]
15801582 /// { [ON DELETE <referential_action>] [ON UPDATE <referential_action>] |
15811583 /// [ON UPDATE <referential_action>] [ON DELETE <referential_action>]
1582- /// }
1584+ /// }
15831585 /// [<constraint_characteristics>]
15841586 /// `).
1585- ForeignKey {
1586- foreign_table : ObjectName ,
1587- referred_columns : Vec < Ident > ,
1588- on_delete : Option < ReferentialAction > ,
1589- on_update : Option < ReferentialAction > ,
1590- characteristics : Option < ConstraintCharacteristics > ,
1591- } ,
1587+ ForeignKey ( ForeignKeyConstraint ) ,
15921588 /// `CHECK (<expr>)`
15931589 Check ( Expr ) ,
15941590 /// Dialect-specific options, such as:
@@ -1671,6 +1667,12 @@ impl From<PrimaryKeyConstraint> for ColumnOption {
16711667 }
16721668}
16731669
1670+ impl From < ForeignKeyConstraint > for ColumnOption {
1671+ fn from ( fk : ForeignKeyConstraint ) -> Self {
1672+ ColumnOption :: ForeignKey ( fk)
1673+ }
1674+ }
1675+
16741676impl fmt:: Display for ColumnOption {
16751677 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
16761678 use ColumnOption :: * ;
@@ -1701,24 +1703,25 @@ impl fmt::Display for ColumnOption {
17011703 }
17021704 Ok ( ( ) )
17031705 }
1704- ForeignKey {
1705- foreign_table,
1706- referred_columns,
1707- on_delete,
1708- on_update,
1709- characteristics,
1710- } => {
1711- write ! ( f, "REFERENCES {foreign_table}" ) ?;
1712- if !referred_columns. is_empty ( ) {
1713- write ! ( f, " ({})" , display_comma_separated( referred_columns) ) ?;
1706+ ForeignKey ( constraint) => {
1707+ write ! ( f, "REFERENCES {}" , constraint. foreign_table) ?;
1708+ if !constraint. referred_columns . is_empty ( ) {
1709+ write ! (
1710+ f,
1711+ " ({})" ,
1712+ display_comma_separated( & constraint. referred_columns)
1713+ ) ?;
17141714 }
1715- if let Some ( action) = on_delete {
1715+ if let Some ( match_kind) = & constraint. match_kind {
1716+ write ! ( f, " {match_kind}" ) ?;
1717+ }
1718+ if let Some ( action) = & constraint. on_delete {
17161719 write ! ( f, " ON DELETE {action}" ) ?;
17171720 }
1718- if let Some ( action) = on_update {
1721+ if let Some ( action) = & constraint . on_update {
17191722 write ! ( f, " ON UPDATE {action}" ) ?;
17201723 }
1721- if let Some ( characteristics) = characteristics {
1724+ if let Some ( characteristics) = & constraint . characteristics {
17221725 write ! ( f, " {characteristics}" ) ?;
17231726 }
17241727 Ok ( ( ) )
0 commit comments