Skip to content

Commit 59b779c

Browse files
committed
Enable GO to terminate an IF statement
1 parent c9a846a commit 59b779c

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/parser/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,16 @@ impl<'a> Parser<'a> {
490490
}
491491

492492
let statement = self.parse_statement()?;
493+
expecting_statement_delimiter = match &statement {
494+
Statement::If(s) => match &s.if_block.conditional_statements {
495+
// the `END` keyword doesn't need to be followed by a statement delimiter, so it shouldn't be expected here
496+
ConditionalStatements::BeginEnd { .. } => false,
497+
// parsing the statement sequence consumes the statement delimiter, so it shouldn't be expected here
498+
ConditionalStatements::Sequence { .. } => false,
499+
},
500+
_ => true,
501+
};
493502
stmts.push(statement);
494-
expecting_statement_delimiter = true;
495503
}
496504
Ok(stmts)
497505
}

tests/sqlparser_mssql.rs

+78-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
mod test_utils;
2424

2525
use helpers::attached_token::AttachedToken;
26-
use sqlparser::tokenizer::{Location, Span};
26+
use sqlparser::keywords::Keyword;
27+
use sqlparser::tokenizer::{Location, Span, TokenWithSpan};
2728
use test_utils::*;
2829

2930
use sqlparser::ast::DataType::{Int, Text, Varbinary};
@@ -2106,3 +2107,79 @@ fn parse_mssql_go_keyword() {
21062107
"sql parser error: Expected: end of statement, found: x"
21072108
);
21082109
}
2110+
2111+
#[test]
2112+
fn test_mssql_if_and_go() {
2113+
let sql = r#"
2114+
IF 1 = 2
2115+
SELECT 3;
2116+
GO
2117+
"#;
2118+
let statements = ms().parse_sql_statements(sql).unwrap();
2119+
assert_eq!(2, statements.len());
2120+
assert_eq!(
2121+
statements[0],
2122+
Statement::If(IfStatement {
2123+
if_block: ConditionalStatementBlock {
2124+
start_token: AttachedToken(TokenWithSpan::wrap(sqlparser::tokenizer::Token::Word(
2125+
sqlparser::tokenizer::Word {
2126+
value: "IF".to_string(),
2127+
quote_style: None,
2128+
keyword: Keyword::IF
2129+
}
2130+
))),
2131+
condition: Some(Expr::BinaryOp {
2132+
left: Box::new(Expr::Value(
2133+
(number("1")).with_empty_span()
2134+
)),
2135+
op: sqlparser::ast::BinaryOperator::Eq,
2136+
right: Box::new(Expr::Value(
2137+
(number("2")).with_empty_span()
2138+
)),
2139+
}),
2140+
then_token: None,
2141+
conditional_statements: ConditionalStatements::Sequence {
2142+
statements: vec![Statement::Query(Box::new(Query {
2143+
with: None,
2144+
limit_clause: None,
2145+
fetch: None,
2146+
locks: vec![],
2147+
for_clause: None,
2148+
order_by: None,
2149+
settings: None,
2150+
format_clause: None,
2151+
body: Box::new(SetExpr::Select(Box::new(Select {
2152+
select_token: AttachedToken::empty(),
2153+
distinct: None,
2154+
top: None,
2155+
top_before_distinct: false,
2156+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(
2157+
(number("3")).with_empty_span()
2158+
))],
2159+
into: None,
2160+
from: vec![],
2161+
lateral_views: vec![],
2162+
prewhere: None,
2163+
selection: None,
2164+
group_by: GroupByExpr::Expressions(vec![], vec![]),
2165+
cluster_by: vec![],
2166+
distribute_by: vec![],
2167+
sort_by: vec![],
2168+
having: None,
2169+
named_window: vec![],
2170+
window_before_qualify: false,
2171+
qualify: None,
2172+
value_table_mode: None,
2173+
connect_by: None,
2174+
flavor: SelectFlavor::Standard,
2175+
}))),
2176+
}))],
2177+
},
2178+
},
2179+
elseif_blocks: vec![],
2180+
else_block: None,
2181+
end_token: None,
2182+
})
2183+
);
2184+
assert_eq!(statements[1], Statement::Go(GoStatement { count: None }));
2185+
}

0 commit comments

Comments
 (0)