Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/mssql_2992_while_single_statement.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WHILE @n < 10 SET @n = @n + 1; SELECT @n
16 changes: 14 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2805,8 +2805,20 @@ pub struct WhileStatement {
impl fmt::Display for WhileStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let WhileStatement { while_block } = self;
write!(f, "{while_block}")?;
Ok(())
match &while_block.conditional_statements {
// A body without `BEGIN` is one statement, ended by the enclosing list's `;`
ConditionalStatements::Sequence { statements } => {
write!(f, "{}", while_block.start_token.0)?;
if let Some(condition) = &while_block.condition {
write!(f, " {condition}")?;
}
if !statements.is_empty() {
write!(f, " {}", display_separated(statements, "; "))?;
}
Ok(())
}
ConditionalStatements::BeginEnd(_) => write!(f, "{while_block}"),
}
}
}

Expand Down
29 changes: 20 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,18 +871,33 @@ impl<'a> Parser<'a> {
})
}

/// Parse a `WHILE` statement.
/// Parse a `WHILE` statement, whose body is one statement or a
/// `BEGIN ... END` block.
///
/// See [Statement::While]
fn parse_while(&mut self) -> Result<WhileStatement, ParserError> {
self.expect_keyword_is(Keyword::WHILE)?;
let while_block = self.parse_conditional_statement_block(&[Keyword::END])?;
let start_token = self.expect_keyword(Keyword::WHILE)?;
let condition = self.parse_expr()?;
let conditional_statements = if self.peek_keyword(Keyword::BEGIN) {
self.parse_conditional_statements(&[Keyword::END])?
} else {
ConditionalStatements::Sequence {
statements: vec![self.parse_statement()?],
}
};

Ok(WhileStatement { while_block })
Ok(WhileStatement {
while_block: ConditionalStatementBlock {
start_token: AttachedToken(start_token),
condition: Some(condition),
then_token: None,
conditional_statements,
},
})
}

/// Parses an expression and associated list of statements
/// belonging to a conditional statement like `IF` or `WHEN` or `WHILE`.
/// belonging to a conditional statement like `IF` or `WHEN`.
///
/// Example:
/// ```sql
Expand All @@ -897,10 +912,6 @@ impl<'a> Parser<'a> {

let condition = match &start_token.token {
Token::Word(w) if w.keyword == Keyword::ELSE => None,
Token::Word(w) if w.keyword == Keyword::WHILE => {
let expr = self.parse_expr()?;
Some(expr)
}
_ => {
let expr = self.parse_expr()?;
then_token = Some(AttachedToken(self.expect_keyword(Keyword::THEN)?));
Expand Down
39 changes: 38 additions & 1 deletion tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ fn test_mssql_cursor() {

#[test]
fn test_mssql_while_statement() {
let while_single_statement = "WHILE 1 = 0 PRINT 'Hello World';";
let while_single_statement = "WHILE 1 = 0 PRINT 'Hello World'";
let stmt = ms().verified_stmt(while_single_statement);
assert_eq!(
stmt,
Expand Down Expand Up @@ -2983,3 +2983,40 @@ fn parse_quoted_statement_keyword_aliases() {
ms().verified_stmt("SELECT * FROM t [PRINT]");
ms().verified_stmt("SELECT * FROM t AS [PRINT]");
}

#[test]
fn parse_mssql_while_single_statement_body() {
let stmts = ms_and_generic()
.parse_sql_statements("WHILE @n < 10 SET @n = @n + 1; SELECT @n")
.unwrap();
let [Statement::While(w), Statement::Query(_)] = &stmts[..] else {
panic!("{stmts:?}")
};
assert!(matches!(
w.while_block.statements()[..],
[Statement::Set(_)]
));

let stmts = tsql()
.parse_sql_statements(
r#"WHILE @n < 10
SET @n = @n + 1
SELECT @n"#,
)
.unwrap();
assert!(matches!(
stmts[..],
[Statement::While(_), Statement::Query(_)]
));

ms_and_generic().verified_stmt("WHILE @i < 3 WHILE @j < 3 SET @j = @j + 1");
ms_and_generic().verified_stmt("WHILE 1 = 1 BEGIN WHILE 2 = 2 SELECT 1; SELECT 2; END");
ms().verified_stmt("IF 1 = 1 WHILE @i < 3 SET @i = @i + 1; ELSE PRINT 'x';");

assert_eq!(
ms_and_generic().parse_sql_statements("WHILE @n < 10"),
Err(ParserError::ParserError(
"Expected: an SQL statement, found: EOF".to_string()
))
);
}
Loading