Skip to content

Commit 01d85a0

Browse files
committed
Introduce FetchPosition to simplify FROM vs IN
1 parent a72e1c1 commit 01d85a0

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/ast/mod.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,10 +3447,7 @@ pub enum Statement {
34473447
/// Cursor name
34483448
name: Ident,
34493449
direction: FetchDirection,
3450-
/// Differentiate between dialects that fetch `FROM` vs fetch `IN`
3451-
///
3452-
/// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/fetch-transact-sql)
3453-
from_or_in: AttachedToken,
3450+
position: FetchPosition,
34543451
/// Optional, It's possible to fetch rows form cursor to the table
34553452
into: Option<ObjectName>,
34563453
},
@@ -4273,25 +4270,10 @@ impl fmt::Display for Statement {
42734270
Statement::Fetch {
42744271
name,
42754272
direction,
4276-
from_or_in,
4273+
position,
42774274
into,
42784275
} => {
4279-
write!(f, "FETCH {direction} ")?;
4280-
4281-
match &from_or_in.0.token {
4282-
Token::Word(w) => match w.keyword {
4283-
Keyword::FROM => {
4284-
write!(f, "FROM {name}")?;
4285-
}
4286-
Keyword::IN => {
4287-
write!(f, "IN {name}")?;
4288-
}
4289-
_ => unreachable!(),
4290-
},
4291-
_ => {
4292-
unreachable!()
4293-
}
4294-
}
4276+
write!(f, "FETCH {direction} {position} {name}")?;
42954277

42964278
if let Some(into) = into {
42974279
write!(f, " INTO {into}")?;
@@ -6232,6 +6214,28 @@ impl fmt::Display for FetchDirection {
62326214
}
62336215
}
62346216

6217+
/// The "position" for a FETCH statement.
6218+
///
6219+
/// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/fetch-transact-sql)
6220+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6221+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6222+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6223+
pub enum FetchPosition {
6224+
From,
6225+
In,
6226+
}
6227+
6228+
impl fmt::Display for FetchPosition {
6229+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6230+
match self {
6231+
FetchPosition::From => f.write_str("FROM")?,
6232+
FetchPosition::In => f.write_str("IN")?,
6233+
};
6234+
6235+
Ok(())
6236+
}
6237+
}
6238+
62356239
/// A privilege on a database object (table, sequence, etc.).
62366240
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
62376241
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/parser/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6646,10 +6646,12 @@ impl<'a> Parser<'a> {
66466646
}
66476647
};
66486648

6649-
let from_or_in_token = if self.peek_keyword(Keyword::FROM) {
6650-
self.expect_keyword(Keyword::FROM)?
6649+
let position = if self.peek_keyword(Keyword::FROM) {
6650+
self.expect_keyword(Keyword::FROM)?;
6651+
FetchPosition::From
66516652
} else if self.peek_keyword(Keyword::IN) {
6652-
self.expect_keyword(Keyword::IN)?
6653+
self.expect_keyword(Keyword::IN)?;
6654+
FetchPosition::In
66536655
} else {
66546656
return parser_err!("Expected FROM or IN", self.peek_token().span.start);
66556657
};
@@ -6665,7 +6667,7 @@ impl<'a> Parser<'a> {
66656667
Ok(Statement::Fetch {
66666668
name,
66676669
direction,
6668-
from_or_in: AttachedToken(from_or_in_token),
6670+
position,
66696671
into,
66706672
})
66716673
}

0 commit comments

Comments
 (0)