Skip to content

Commit 0633b54

Browse files
committed
Enable GO to terminate an IF statement
1 parent a75bb7f commit 0633b54

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
@@ -498,8 +498,16 @@ impl<'a> Parser<'a> {
498498
}
499499

500500
let statement = self.parse_statement()?;
501+
expecting_statement_delimiter = match &statement {
502+
Statement::If(s) => match &s.if_block.conditional_statements {
503+
// the `END` keyword doesn't need to be followed by a statement delimiter, so it shouldn't be expected here
504+
ConditionalStatements::BeginEnd { .. } => false,
505+
// parsing the statement sequence consumes the statement delimiter, so it shouldn't be expected here
506+
ConditionalStatements::Sequence { .. } => false,
507+
},
508+
_ => true,
509+
};
501510
stmts.push(statement);
502-
expecting_statement_delimiter = true;
503511
}
504512
Ok(stmts)
505513
}

tests/sqlparser_mssql.rs

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

0 commit comments

Comments
 (0)