@@ -2293,18 +2293,14 @@ pub enum ConditionalStatements {
22932293 /// SELECT 1; SELECT 2; SELECT 3; ...
22942294 Sequence { statements : Vec < Statement > } ,
22952295 /// BEGIN SELECT 1; SELECT 2; SELECT 3; ... END
2296- BeginEnd {
2297- begin_token : AttachedToken ,
2298- statements : Vec < Statement > ,
2299- end_token : AttachedToken ,
2300- } ,
2296+ BeginEnd ( BeginEndStatements ) ,
23012297}
23022298
23032299impl ConditionalStatements {
23042300 pub fn statements ( & self ) -> & Vec < Statement > {
23052301 match self {
23062302 ConditionalStatements :: Sequence { statements } => statements,
2307- ConditionalStatements :: BeginEnd { statements , .. } => statements,
2303+ ConditionalStatements :: BeginEnd ( bes ) => & bes . statements ,
23082304 }
23092305 }
23102306}
@@ -2318,15 +2314,44 @@ impl fmt::Display for ConditionalStatements {
23182314 }
23192315 Ok ( ( ) )
23202316 }
2321- ConditionalStatements :: BeginEnd { statements, .. } => {
2322- write ! ( f, "BEGIN " ) ?;
2323- format_statement_list ( f, statements) ?;
2324- write ! ( f, " END" )
2325- }
2317+ ConditionalStatements :: BeginEnd ( bes) => write ! ( f, "{}" , bes) ,
23262318 }
23272319 }
23282320}
23292321
2322+ /// Represents a list of statements enclosed within `BEGIN` and `END` keywords.
2323+ /// Example:
2324+ /// ```sql
2325+ /// BEGIN
2326+ /// SELECT 1;
2327+ /// SELECT 2;
2328+ /// END
2329+ /// ```
2330+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
2331+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2332+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
2333+ pub struct BeginEndStatements {
2334+ pub begin_token : AttachedToken ,
2335+ pub statements : Vec < Statement > ,
2336+ pub end_token : AttachedToken ,
2337+ }
2338+ 2339+ impl fmt:: Display for BeginEndStatements {
2340+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2341+ let BeginEndStatements {
2342+ begin_token : AttachedToken ( begin_token) ,
2343+ statements,
2344+ end_token : AttachedToken ( end_token) ,
2345+ } = self ;
2346+ 2347+ write ! ( f, "{begin_token} " ) ?;
2348+ if !statements. is_empty ( ) {
2349+ format_statement_list ( f, statements) ?;
2350+ }
2351+ write ! ( f, " {end_token}" )
2352+ }
2353+ }
2354+ 23302355/// A `RAISE` statement.
23312356///
23322357/// Examples:
@@ -3615,6 +3640,7 @@ pub enum Statement {
36153640 /// 1. [Hive](https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction)
36163641 /// 2. [PostgreSQL](https://www.postgresql.org/docs/15/sql-createfunction.html)
36173642 /// 3. [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement)
3643+ /// 4. [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql)
36183644 CreateFunction ( CreateFunction ) ,
36193645 /// CREATE TRIGGER
36203646 ///
@@ -4061,6 +4087,12 @@ pub enum Statement {
40614087 ///
40624088 /// See: <https://learn.microsoft.com/en-us/sql/t-sql/statements/print-transact-sql>
40634089 Print ( PrintStatement ) ,
4090+ /// ```sql
4091+ /// RETURN [ expression ]
4092+ /// ```
4093+ ///
4094+ /// See [ReturnStatement]
4095+ Return ( ReturnStatement ) ,
40644096}
40654097
40664098#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
@@ -5753,6 +5785,7 @@ impl fmt::Display for Statement {
57535785 Ok ( ( ) )
57545786 }
57555787 Statement :: Print ( s) => write ! ( f, "{s}" ) ,
5788+ Statement :: Return ( r) => write ! ( f, "{r}" ) ,
57565789 Statement :: List ( command) => write ! ( f, "LIST {command}" ) ,
57575790 Statement :: Remove ( command) => write ! ( f, "REMOVE {command}" ) ,
57585791 }
@@ -8355,6 +8388,7 @@ impl fmt::Display for FunctionDeterminismSpecifier {
83558388///
83568389/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_11
83578390/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
8391+ /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql
83588392#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
83598393#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
83608394#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
@@ -8383,6 +8417,22 @@ pub enum CreateFunctionBody {
83838417 ///
83848418 /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_11
83858419 AsAfterOptions ( Expr ) ,
8420+ /// Function body with statements before the `RETURN` keyword.
8421+ ///
8422+ /// Example:
8423+ /// ```sql
8424+ /// CREATE FUNCTION my_scalar_udf(a INT, b INT)
8425+ /// RETURNS INT
8426+ /// AS
8427+ /// BEGIN
8428+ /// DECLARE c INT;
8429+ /// SET c = a + b;
8430+ /// RETURN c;
8431+ /// END
8432+ /// ```
8433+ ///
8434+ /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql
8435+ AsBeginEnd ( BeginEndStatements ) ,
83868436 /// Function body expression using the 'RETURN' keyword.
83878437 ///
83888438 /// Example:
@@ -9231,6 +9281,34 @@ impl fmt::Display for PrintStatement {
92319281 }
92329282}
92339283
9284+ /// Represents a `Return` statement.
9285+ ///
9286+ /// [MsSql triggers](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql)
9287+ /// [MsSql functions](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql)
9288+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
9289+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
9290+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
9291+ pub struct ReturnStatement {
9292+ pub value : Option < ReturnStatementValue > ,
9293+ }
9294+ 9295+ impl fmt:: Display for ReturnStatement {
9296+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
9297+ match & self . value {
9298+ Some ( ReturnStatementValue :: Expr ( expr) ) => write ! ( f, "RETURN {}" , expr) ,
9299+ None => write ! ( f, "RETURN" ) ,
9300+ }
9301+ }
9302+ }
9303+ 9304+ /// Variants of a `RETURN` statement
9305+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
9306+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
9307+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
9308+ pub enum ReturnStatementValue {
9309+ Expr ( Expr ) ,
9310+ }
9311+ 92349312#[ cfg( test) ]
92359313mod tests {
92369314 use super :: * ;
0 commit comments