Skip to content

Commit f02867c

Browse files
committed
Enable GO to terminate an IF statement
1 parent e99ba87 commit f02867c

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/parser/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,16 @@ impl<'a> Parser<'a> {
510510
}
511511

512512
let statement = self.parse_statement()?;
513+
expecting_statement_delimiter = match &statement {
514+
Statement::If(s) => match &s.if_block.conditional_statements {
515+
// the `END` keyword doesn't need to be followed by a statement delimiter, so it shouldn't be expected here
516+
ConditionalStatements::BeginEnd { .. } => false,
517+
// parsing the statement sequence consumes the statement delimiter, so it shouldn't be expected here
518+
ConditionalStatements::Sequence { .. } => false,
519+
},
520+
_ => true,
521+
};
513522
stmts.push(statement);
514-
expecting_statement_delimiter = true;
515523
}
516524
Ok(stmts)
517525
}

tests/sqlparser_mssql.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,3 +2593,77 @@ fn parse_mssql_go_keyword() {
25932593
"sql parser error: Expected: end of statement, found: x"
25942594
);
25952595
}
2596+
2597+
#[test]
2598+
fn test_mssql_if_and_go() {
2599+
let sql = r#"
2600+
IF 1 = 2
2601+
SELECT 3;
2602+
GO
2603+
"#;
2604+
let statements = ms().parse_sql_statements(sql).unwrap();
2605+
assert_eq!(2, statements.len());
2606+
assert_eq!(
2607+
statements[0],
2608+
Statement::If(IfStatement {
2609+
if_block: ConditionalStatementBlock {
2610+
start_token: AttachedToken(TokenWithSpan::wrap(sqlparser::tokenizer::Token::Word(
2611+
sqlparser::tokenizer::Word {
2612+
value: "IF".to_string(),
2613+
quote_style: None,
2614+
keyword: Keyword::IF
2615+
}
2616+
))),
2617+
condition: Some(Expr::BinaryOp {
2618+
left: Box::new(Expr::Value((number("1")).with_empty_span())),
2619+
op: sqlparser::ast::BinaryOperator::Eq,
2620+
right: Box::new(Expr::Value((number("2")).with_empty_span())),
2621+
}),
2622+
then_token: None,
2623+
conditional_statements: ConditionalStatements::Sequence {
2624+
statements: vec![Statement::Query(Box::new(Query {
2625+
with: None,
2626+
limit_clause: None,
2627+
fetch: None,
2628+
locks: vec![],
2629+
for_clause: None,
2630+
order_by: None,
2631+
settings: None,
2632+
format_clause: None,
2633+
pipe_operators: vec![],
2634+
body: Box::new(SetExpr::Select(Box::new(Select {
2635+
select_token: AttachedToken::empty(),
2636+
distinct: None,
2637+
top: None,
2638+
top_before_distinct: false,
2639+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(
2640+
(number("3")).with_empty_span()
2641+
))],
2642+
exclude: None,
2643+
into: None,
2644+
from: vec![],
2645+
lateral_views: vec![],
2646+
prewhere: None,
2647+
selection: None,
2648+
group_by: GroupByExpr::Expressions(vec![], vec![]),
2649+
cluster_by: vec![],
2650+
distribute_by: vec![],
2651+
sort_by: vec![],
2652+
having: None,
2653+
named_window: vec![],
2654+
window_before_qualify: false,
2655+
qualify: None,
2656+
value_table_mode: None,
2657+
connect_by: None,
2658+
flavor: SelectFlavor::Standard,
2659+
}))),
2660+
}))],
2661+
},
2662+
},
2663+
elseif_blocks: vec![],
2664+
else_block: None,
2665+
end_token: None,
2666+
})
2667+
);
2668+
assert_eq!(statements[1], Statement::Go(GoStatement { count: None }));
2669+
}

0 commit comments

Comments
 (0)