@@ -2293,18 +2293,14 @@ pub enum ConditionalStatements {
2293
2293
/// SELECT 1; SELECT 2; SELECT 3; ...
2294
2294
Sequence { statements : Vec < Statement > } ,
2295
2295
/// 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 ) ,
2301
2297
}
2302
2298
2303
2299
impl ConditionalStatements {
2304
2300
pub fn statements ( & self ) -> & Vec < Statement > {
2305
2301
match self {
2306
2302
ConditionalStatements :: Sequence { statements } => statements,
2307
- ConditionalStatements :: BeginEnd { statements , .. } => statements,
2303
+ ConditionalStatements :: BeginEnd ( bes ) => & bes . statements ,
2308
2304
}
2309
2305
}
2310
2306
}
@@ -2318,15 +2314,44 @@ impl fmt::Display for ConditionalStatements {
2318
2314
}
2319
2315
Ok ( ( ) )
2320
2316
}
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) ,
2326
2318
}
2327
2319
}
2328
2320
}
2329
2321
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
+
2330
2355
/// A `RAISE` statement.
2331
2356
///
2332
2357
/// Examples:
@@ -3615,6 +3640,7 @@ pub enum Statement {
3615
3640
/// 1. [Hive](https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction)
3616
3641
/// 2. [PostgreSQL](https://www.postgresql.org/docs/15/sql-createfunction.html)
3617
3642
/// 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)
3618
3644
CreateFunction ( CreateFunction ) ,
3619
3645
/// CREATE TRIGGER
3620
3646
///
@@ -4061,6 +4087,12 @@ pub enum Statement {
4061
4087
///
4062
4088
/// See: <https://learn.microsoft.com/en-us/sql/t-sql/statements/print-transact-sql>
4063
4089
Print ( PrintStatement ) ,
4090
+ /// ```sql
4091
+ /// RETURN [ expression ]
4092
+ /// ```
4093
+ ///
4094
+ /// See [ReturnStatement]
4095
+ Return ( ReturnStatement ) ,
4064
4096
}
4065
4097
4066
4098
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
@@ -5753,6 +5785,7 @@ impl fmt::Display for Statement {
5753
5785
Ok ( ( ) )
5754
5786
}
5755
5787
Statement :: Print ( s) => write ! ( f, "{s}" ) ,
5788
+ Statement :: Return ( r) => write ! ( f, "{r}" ) ,
5756
5789
Statement :: List ( command) => write ! ( f, "LIST {command}" ) ,
5757
5790
Statement :: Remove ( command) => write ! ( f, "REMOVE {command}" ) ,
5758
5791
}
@@ -8355,6 +8388,7 @@ impl fmt::Display for FunctionDeterminismSpecifier {
8355
8388
///
8356
8389
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_11
8357
8390
/// [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
8358
8392
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
8359
8393
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
8360
8394
#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
@@ -8383,6 +8417,22 @@ pub enum CreateFunctionBody {
8383
8417
///
8384
8418
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_11
8385
8419
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 ) ,
8386
8436
/// Function body expression using the 'RETURN' keyword.
8387
8437
///
8388
8438
/// Example:
@@ -9231,6 +9281,34 @@ impl fmt::Display for PrintStatement {
9231
9281
}
9232
9282
}
9233
9283
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
+
9234
9312
#[ cfg( test) ]
9235
9313
mod tests {
9236
9314
use super :: * ;
0 commit comments