@@ -2228,7 +2228,33 @@ impl fmt::Display for IfStatement {
22282228 }
22292229}
22302230
2231- /// A block within a [Statement::Case] or [Statement::If]-like statement
2231+ /// A `WHILE` statement.
2232+ ///
2233+ /// Example:
2234+ /// ```sql
2235+ /// WHILE @@FETCH_STATUS = 0
2236+ /// BEGIN
2237+ /// FETCH NEXT FROM c1 INTO @var1, @var2;
2238+ /// END
2239+ /// ```
2240+ ///
2241+ /// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/while-transact-sql)
2242+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
2243+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2244+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
2245+ pub struct WhileStatement {
2246+ pub while_block : ConditionalStatementBlock ,
2247+ }
2248+
2249+ impl fmt:: Display for WhileStatement {
2250+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2251+ let WhileStatement { while_block } = self ;
2252+ write ! ( f, "{while_block}" ) ?;
2253+ Ok ( ( ) )
2254+ }
2255+ }
2256+
2257+ /// A block within a [Statement::Case] or [Statement::If] or [Statement::While]-like statement
22322258///
22332259/// Example 1:
22342260/// ```sql
@@ -2244,6 +2270,14 @@ impl fmt::Display for IfStatement {
22442270/// ```sql
22452271/// ELSE SELECT 1; SELECT 2;
22462272/// ```
2273+ ///
2274+ /// Example 4:
2275+ /// ```sql
2276+ /// WHILE @@FETCH_STATUS = 0
2277+ /// BEGIN
2278+ /// FETCH NEXT FROM c1 INTO @var1, @var2;
2279+ /// END
2280+ /// ```
22472281#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
22482282#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
22492283#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
@@ -2983,6 +3017,8 @@ pub enum Statement {
29833017 Case ( CaseStatement ) ,
29843018 /// An `IF` statement.
29853019 If ( IfStatement ) ,
3020+ /// A `WHILE` statement.
3021+ While ( WhileStatement ) ,
29863022 /// A `RAISE` statement.
29873023 Raise ( RaiseStatement ) ,
29883024 /// ```sql
@@ -3034,6 +3070,11 @@ pub enum Statement {
30343070 partition : Option < Box < Expr > > ,
30353071 } ,
30363072 /// ```sql
3073+ /// OPEN cursor_name
3074+ /// ```
3075+ /// Opens a cursor.
3076+ Open ( OpenStatement ) ,
3077+ /// ```sql
30373078 /// CLOSE
30383079 /// ```
30393080 /// Closes the portal underlying an open cursor.
@@ -3413,6 +3454,7 @@ pub enum Statement {
34133454 /// Cursor name
34143455 name : Ident ,
34153456 direction : FetchDirection ,
3457+ position : FetchPosition ,
34163458 /// Optional, It's possible to fetch rows form cursor to the table
34173459 into : Option < ObjectName > ,
34183460 } ,
@@ -4235,11 +4277,10 @@ impl fmt::Display for Statement {
42354277 Statement :: Fetch {
42364278 name,
42374279 direction,
4280+ position,
42384281 into,
42394282 } => {
4240- write ! ( f, "FETCH {direction} " ) ?;
4241-
4242- write ! ( f, "IN {name}" ) ?;
4283+ write ! ( f, "FETCH {direction} {position} {name}" ) ?;
42434284
42444285 if let Some ( into) = into {
42454286 write ! ( f, " INTO {into}" ) ?;
@@ -4329,6 +4370,9 @@ impl fmt::Display for Statement {
43294370 Statement :: If ( stmt) => {
43304371 write ! ( f, "{stmt}" )
43314372 }
4373+ Statement :: While ( stmt) => {
4374+ write ! ( f, "{stmt}" )
4375+ }
43324376 Statement :: Raise ( stmt) => {
43334377 write ! ( f, "{stmt}" )
43344378 }
@@ -4498,6 +4542,7 @@ impl fmt::Display for Statement {
44984542 Ok ( ( ) )
44994543 }
45004544 Statement :: Delete ( delete) => write ! ( f, "{delete}" ) ,
4545+ Statement :: Open ( open) => write ! ( f, "{open}" ) ,
45014546 Statement :: Close { cursor } => {
45024547 write ! ( f, "CLOSE {cursor}" ) ?;
45034548
@@ -6187,6 +6232,28 @@ impl fmt::Display for FetchDirection {
61876232 }
61886233}
61896234
6235+ /// The "position" for a FETCH statement.
6236+ ///
6237+ /// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/fetch-transact-sql)
6238+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
6239+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
6240+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
6241+ pub enum FetchPosition {
6242+ From ,
6243+ In ,
6244+ }
6245+
6246+ impl fmt:: Display for FetchPosition {
6247+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
6248+ match self {
6249+ FetchPosition :: From => f. write_str ( "FROM" ) ?,
6250+ FetchPosition :: In => f. write_str ( "IN" ) ?,
6251+ } ;
6252+
6253+ Ok ( ( ) )
6254+ }
6255+ }
6256+
61906257/// A privilege on a database object (table, sequence, etc.).
61916258#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
61926259#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
@@ -9354,6 +9421,21 @@ pub enum ReturnStatementValue {
93549421 Expr ( Expr ) ,
93559422}
93569423
9424+ /// Represents an `OPEN` statement.
9425+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
9426+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
9427+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
9428+ pub struct OpenStatement {
9429+ /// Cursor name
9430+ pub cursor_name : Ident ,
9431+ }
9432+
9433+ impl fmt:: Display for OpenStatement {
9434+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
9435+ write ! ( f, "OPEN {}" , self . cursor_name)
9436+ }
9437+ }
9438+
93579439#[ cfg( test) ]
93589440mod tests {
93599441 use super :: * ;
0 commit comments