Skip to content

Commit 77fd7aa

Browse files
committed
Parse the MSSQL DECLARE statement in a more rustc-like manner
1 parent c76ba45 commit 77fd7aa

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

src/parser/mod.rs

+50-44
Original file line numberDiff line numberDiff line change
@@ -5292,55 +5292,61 @@ impl<'a> Parser<'a> {
52925292
/// ```
52935293
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
52945294
pub fn parse_mssql_declare(&mut self) -> Result<Statement, ParserError> {
5295-
let mut stmts = vec![];
5296-
5297-
loop {
5298-
let name = {
5299-
let ident = self.parse_identifier(false)?;
5300-
if !ident.value.starts_with('@') {
5301-
Err(ParserError::TokenizerError(
5302-
"Invalid MsSql variable declaration.".to_string(),
5303-
))
5304-
} else {
5305-
Ok(ident)
5306-
}
5307-
}?;
5295+
let stmts = self.parse_comma_separated(Parser::parse_mssql_declare_stmt)?;
53085296

5309-
let (declare_type, data_type) = match self.peek_token().token {
5310-
Token::Word(w) => match w.keyword {
5311-
Keyword::CURSOR => {
5312-
self.next_token();
5313-
(Some(DeclareType::Cursor), None)
5314-
}
5315-
Keyword::AS => {
5316-
self.next_token();
5317-
(None, Some(self.parse_data_type()?))
5318-
}
5319-
_ => (None, Some(self.parse_data_type()?)),
5320-
},
5321-
_ => (None, Some(self.parse_data_type()?)),
5322-
};
5297+
Ok(Statement::Declare { stmts })
5298+
}
53235299

5324-
let assignment = self.parse_mssql_variable_declaration_expression()?;
5300+
/// Parse the body of a [MsSql] `DECLARE`statement.
5301+
///
5302+
/// Syntax:
5303+
/// ```text
5304+
// {
5305+
// { @local_variable [AS] data_type [ = value ] }
5306+
// | { @cursor_variable_name CURSOR }
5307+
// } [ ,...n ]
5308+
/// ```
5309+
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
5310+
pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
5311+
let name = {
5312+
let ident = self.parse_identifier(false)?;
5313+
if !ident.value.starts_with('@') {
5314+
Err(ParserError::TokenizerError(
5315+
"Invalid MsSql variable declaration.".to_string(),
5316+
))
5317+
} else {
5318+
Ok(ident)
5319+
}
5320+
}?;
53255321

5326-
stmts.push(Declare {
5327-
names: vec![name],
5328-
data_type,
5329-
assignment,
5330-
declare_type,
5331-
binary: None,
5332-
sensitive: None,
5333-
scroll: None,
5334-
hold: None,
5335-
for_query: None,
5336-
});
5322+
let (declare_type, data_type) = match self.peek_token().token {
5323+
Token::Word(w) => match w.keyword {
5324+
Keyword::CURSOR => {
5325+
self.next_token();
5326+
(Some(DeclareType::Cursor), None)
5327+
}
5328+
Keyword::AS => {
5329+
self.next_token();
5330+
(None, Some(self.parse_data_type()?))
5331+
}
5332+
_ => (None, Some(self.parse_data_type()?)),
5333+
},
5334+
_ => (None, Some(self.parse_data_type()?)),
5335+
};
53375336

5338-
if self.peek_token().token == Token::SemiColon || self.next_token() != Token::Comma {
5339-
break;
5340-
}
5341-
}
5337+
let assignment = self.parse_mssql_variable_declaration_expression()?;
53425338

5343-
Ok(Statement::Declare { stmts })
5339+
Ok(Declare {
5340+
names: vec![name],
5341+
data_type,
5342+
assignment,
5343+
declare_type,
5344+
binary: None,
5345+
sensitive: None,
5346+
scroll: None,
5347+
hold: None,
5348+
for_query: None,
5349+
})
53445350
}
53455351

53465352
/// Parses the assigned expression in a variable declaration.

0 commit comments

Comments
 (0)