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
4 changes: 4 additions & 0 deletions fuzz/fuzz_seeds/mssql_1800_block_without_semicolons.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BEGIN
SELECT 1
SELECT 2
END
11 changes: 9 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5399,7 +5399,8 @@ impl<'a> Parser<'a> {
self.parse_comma_separated(f)
}

/// Parses 0 or more statements, each followed by a semicolon.
/// Parses 0 or more statements, each followed by a semicolon, which is
/// optional when [ParserOptions::require_semicolon_stmt_delimiter] is false.
/// If the next token is any of `terminal_keywords` then no more
/// statements will be parsed.
pub(crate) fn parse_statement_list(
Expand All @@ -5408,6 +5409,10 @@ impl<'a> Parser<'a> {
) -> Result<Vec<Statement>, ParserError> {
let mut values = vec![];
loop {
if !self.options.require_semicolon_stmt_delimiter {
while self.consume_token(&Token::SemiColon) {}
}

match &self.peek_nth_token_ref(0).token {
Token::EOF => break,
Token::Word(w)
Expand All @@ -5419,7 +5424,9 @@ impl<'a> Parser<'a> {
}

values.push(self.parse_statement()?);
self.expect_token(&Token::SemiColon)?;
if self.options.require_semicolon_stmt_delimiter {
self.expect_token(&Token::SemiColon)?;
}
}
Ok(values)
}
Expand Down
43 changes: 43 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2983,3 +2983,46 @@ fn parse_quoted_statement_keyword_aliases() {
ms().verified_stmt("SELECT * FROM t [PRINT]");
ms().verified_stmt("SELECT * FROM t AS [PRINT]");
}

#[test]
fn parse_tsql_statement_list_without_semicolons() {
tsql().one_statement_parses_to(
"BEGIN SELECT 1 SELECT 2 END",
"BEGIN SELECT 1; SELECT 2; END",
);
tsql().one_statement_parses_to(
"WHILE @i < 3 BEGIN SET @i = @i + 1 SELECT @i END",
"WHILE @i < 3 BEGIN SET @i = @i + 1; SELECT @i; END",
);
tsql().one_statement_parses_to(
"CREATE PROCEDURE p AS BEGIN SELECT 1 SELECT 2 END",
"CREATE PROCEDURE p AS BEGIN SELECT 1; SELECT 2; END",
);
tsql().one_statement_parses_to(
"CREATE FUNCTION f() RETURNS INT AS BEGIN DECLARE @x INT SET @x = 1 RETURN @x END",
"CREATE FUNCTION f() RETURNS INT AS BEGIN DECLARE @x INT; SET @x = 1; RETURN @x; END",
);
tsql().one_statement_parses_to(
"CREATE TRIGGER tr ON t AFTER INSERT AS BEGIN SELECT 1 SELECT 2 END",
"CREATE TRIGGER tr ON t AFTER INSERT AS BEGIN SELECT 1; SELECT 2; END",
);
tsql().verified_stmt("BEGIN SELECT 1; SELECT 2; END");
tsql().one_statement_parses_to(
"CREATE PROCEDURE p AS BEGIN ; SELECT 1;; END",
"CREATE PROCEDURE p AS BEGIN SELECT 1; END",
);

assert_eq!(
tsql().parse_sql_statements("BEGIN SELECT 1 SELECT 2"),
Err(ParserError::ParserError(
"Expected: END, found: EOF".to_owned()
))
);

assert_eq!(
ms().parse_sql_statements("BEGIN SELECT 1 SELECT 2 END"),
Err(ParserError::ParserError(
"Expected: ;, found: SELECT".to_owned()
))
);
}
Loading