@@ -599,6 +599,7 @@ pub enum ColumnOption {
599599 generated_as : GeneratedAs ,
600600 sequence_options : Option < Vec < SequenceOptions > > ,
601601 generation_expr : Option < Expr > ,
602+ generation_expr_mode : Option < GeneratedExpressionMode > ,
602603 } ,
603604}
604605
@@ -639,25 +640,25 @@ impl fmt::Display for ColumnOption {
639640 generated_as,
640641 sequence_options,
641642 generation_expr,
642- } => match generated_as {
643- GeneratedAs :: Always => {
644- write ! ( f, "GENERATED ALWAYS AS IDENTITY" ) ?;
645- if sequence_options. is_some ( ) {
646- let so = sequence_options. as_ref ( ) . unwrap ( ) ;
647- if !so. is_empty ( ) {
648- write ! ( f, " (" ) ?;
649- }
650- for sequence_option in so {
651- write ! ( f, "{sequence_option}" ) ?;
652- }
653- if !so. is_empty ( ) {
654- write ! ( f, " )" ) ?;
655- }
656- }
643+ generation_expr_mode,
644+ } => {
645+ if let Some ( expr) = generation_expr {
646+ let modifier = match generation_expr_mode {
647+ None => "" ,
648+ Some ( GeneratedExpressionMode :: Virtual ) => " VIRTUAL" ,
649+ Some ( GeneratedExpressionMode :: Stored ) => " STORED" ,
650+ } ;
651+ write ! ( f, "GENERATED ALWAYS AS ({expr}){modifier}" ) ?;
657652 Ok ( ( ) )
658- }
659- GeneratedAs :: ByDefault => {
660- write ! ( f, "GENERATED BY DEFAULT AS IDENTITY" ) ?;
653+ } else {
654+ // Like Postgres - generated from sequence
655+ let when = match generated_as {
656+ GeneratedAs :: Always => "ALWAYS" ,
657+ GeneratedAs :: ByDefault => "BY DEFAULT" ,
658+ // ExpStored goes with an expression, handled above
659+ GeneratedAs :: ExpStored => unreachable ! ( ) ,
660+ } ;
661+ write ! ( f, "GENERATED {when} AS IDENTITY" ) ?;
661662 if sequence_options. is_some ( ) {
662663 let so = sequence_options. as_ref ( ) . unwrap ( ) ;
663664 if !so. is_empty ( ) {
@@ -672,17 +673,13 @@ impl fmt::Display for ColumnOption {
672673 }
673674 Ok ( ( ) )
674675 }
675- GeneratedAs :: ExpStored => {
676- let expr = generation_expr. as_ref ( ) . unwrap ( ) ;
677- write ! ( f, "GENERATED ALWAYS AS ({expr}) STORED" )
678- }
679- } ,
676+ }
680677 }
681678 }
682679}
683680
684681/// `GeneratedAs`s are modifiers that follow a column option in a `generated`.
685- /// 'ExpStored' is PostgreSQL specific
682+ /// 'ExpStored' is used for a column generated from an expression and stored.
686683#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
687684#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
688685#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
@@ -692,6 +689,16 @@ pub enum GeneratedAs {
692689 ExpStored ,
693690}
694691
692+ /// `GeneratedExpressionMode`s are modifiers that follow an expression in a `generated`.
693+ /// No modifier is typically the same as Virtual.
694+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
695+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
696+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
697+ pub enum GeneratedExpressionMode {
698+ Virtual ,
699+ Stored ,
700+ }
701+
695702fn display_constraint_name ( name : & ' _ Option < Ident > ) -> impl fmt:: Display + ' _ {
696703 struct ConstraintName < ' a > ( & ' a Option < Ident > ) ;
697704 impl < ' a > fmt:: Display for ConstraintName < ' a > {
0 commit comments