@@ -2982,6 +2982,63 @@ impl From<Set> for Statement {
2982
2982
}
2983
2983
}
2984
2984
2985
+ /// An exception representing exception handling with the `EXCEPT` keyword.
2986
+ ///
2987
+ /// Snowflake: <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/exception>
2988
+ /// BigQuery: <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend>
2989
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
2990
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2991
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
2992
+ pub struct Exception {
2993
+ pub when : Vec < ExceptionWhen > ,
2994
+ pub raises : Option < Box < Statement > > ,
2995
+ }
2996
+
2997
+ impl Display for Exception {
2998
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2999
+ write ! ( f, " EXCEPTION" ) ?;
3000
+ for w in & self . when {
3001
+ write ! ( f, "{w}" ) ?;
3002
+ }
3003
+
3004
+ if let Some ( raises) = & self . raises {
3005
+ write ! ( f, " {raises};" ) ?;
3006
+ }
3007
+
3008
+ Ok ( ( ) )
3009
+ }
3010
+ }
3011
+
3012
+ /// A representation of a `WHEN` arm with all the identifiers catched and the statements to execute
3013
+ /// for the arm.
3014
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
3015
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
3016
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
3017
+ pub struct ExceptionWhen {
3018
+ pub idents : Vec < Ident > ,
3019
+ pub statements : Vec < Statement > ,
3020
+ }
3021
+
3022
+ impl Display for ExceptionWhen {
3023
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
3024
+ let idents = self
3025
+ . idents
3026
+ . iter ( )
3027
+ . map ( ToString :: to_string)
3028
+ . collect :: < Vec < _ > > ( )
3029
+ . join ( " OR " ) ;
3030
+
3031
+ write ! ( f, " WHEN {idents} THEN" , idents = idents) ?;
3032
+
3033
+ if !self . statements . is_empty ( ) {
3034
+ write ! ( f, " " ) ?;
3035
+ format_statement_list ( f, & self . statements ) ?;
3036
+ }
3037
+
3038
+ Ok ( ( ) )
3039
+ }
3040
+ }
3041
+
2985
3042
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
2986
3043
#[ allow( clippy:: large_enum_variant) ]
2987
3044
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
@@ -3670,17 +3727,24 @@ pub enum Statement {
3670
3727
/// END;
3671
3728
/// ```
3672
3729
statements : Vec < Statement > ,
3673
- /// Statements of an exception clause .
3730
+ /// Exception handling with exception clauses and raises .
3674
3731
/// Example:
3675
3732
/// ```sql
3676
3733
/// BEGIN
3677
3734
/// SELECT 1;
3678
- /// EXCEPTION WHEN ERROR THEN
3679
- /// SELECT 2;
3680
- /// SELECT 3;
3735
+ /// EXCEPTION
3736
+ /// WHEN EXCEPTION_1 THEN
3737
+ /// select 2;
3738
+ /// WHEN EXCEPTION_2 OR EXCEPTION_3 THEN
3739
+ /// select 3;
3740
+ /// WHEN OTHER THEN
3741
+ /// SELECT 4;
3742
+ /// RAISE;
3681
3743
/// END;
3744
+ /// ```
3682
3745
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend>
3683
- exception_statements : Option < Vec < Statement > > ,
3746
+ /// <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/exception>
3747
+ exception : Option < Exception > ,
3684
3748
/// TRUE if the statement has an `END` keyword.
3685
3749
has_end_keyword : bool ,
3686
3750
} ,
@@ -5525,7 +5589,7 @@ impl fmt::Display for Statement {
5525
5589
transaction,
5526
5590
modifier,
5527
5591
statements,
5528
- exception_statements ,
5592
+ exception ,
5529
5593
has_end_keyword,
5530
5594
} => {
5531
5595
if * syntax_begin {
@@ -5547,12 +5611,8 @@ impl fmt::Display for Statement {
5547
5611
write ! ( f, " " ) ?;
5548
5612
format_statement_list ( f, statements) ?;
5549
5613
}
5550
- if let Some ( exception_statements) = exception_statements {
5551
- write ! ( f, " EXCEPTION WHEN ERROR THEN" ) ?;
5552
- if !exception_statements. is_empty ( ) {
5553
- write ! ( f, " " ) ?;
5554
- format_statement_list ( f, exception_statements) ?;
5555
- }
5614
+ if let Some ( exception) = exception {
5615
+ write ! ( f, "{exception}" ) ?;
5556
5616
}
5557
5617
if * has_end_keyword {
5558
5618
write ! ( f, " END" ) ?;
0 commit comments