@@ -2982,6 +2982,63 @@ impl From<Set> for Statement {
29822982 }
29832983}
29842984
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+
29853042/// A top-level statement (SELECT, INSERT, CREATE, etc.)
29863043#[ allow( clippy:: large_enum_variant) ]
29873044#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
@@ -3670,17 +3727,24 @@ pub enum Statement {
36703727 /// END;
36713728 /// ```
36723729 statements : Vec < Statement > ,
3673- /// Statements of an exception clause .
3730+ /// Exception handling with exception clauses and raises .
36743731 /// Example:
36753732 /// ```sql
36763733 /// BEGIN
36773734 /// 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;
36813743 /// END;
3744+ /// ```
36823745 /// <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 > ,
36843748 /// TRUE if the statement has an `END` keyword.
36853749 has_end_keyword : bool ,
36863750 } ,
@@ -5525,7 +5589,7 @@ impl fmt::Display for Statement {
55255589 transaction,
55265590 modifier,
55275591 statements,
5528- exception_statements ,
5592+ exception ,
55295593 has_end_keyword,
55305594 } => {
55315595 if * syntax_begin {
@@ -5547,12 +5611,8 @@ impl fmt::Display for Statement {
55475611 write ! ( f, " " ) ?;
55485612 format_statement_list ( f, statements) ?;
55495613 }
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}" ) ?;
55565616 }
55575617 if * has_end_keyword {
55585618 write ! ( f, " END" ) ?;
0 commit comments