Skip to content

Commit 24d813b

Browse files
committed
Enable GO to terminate an IF statement
1 parent 25fd849 commit 24d813b

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/parser/mod.rs

Lines changed: 9 additions & 1 deletion
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

Lines changed: 74 additions & 1 deletion
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};
@@ -2226,3 +2227,75 @@ fn parse_mssql_go_keyword() {
22262227
"sql parser error: Expected: end of statement, found: x"
22272228
);
22282229
}
2230+
2231+
#[test]
2232+
fn test_mssql_if_and_go() {
2233+
let sql = r#"
2234+
IF 1 = 2
2235+
SELECT 3;
2236+
GO
2237+
"#;
2238+
let statements = ms().parse_sql_statements(sql).unwrap();
2239+
assert_eq!(2, statements.len());
2240+
assert_eq!(
2241+
statements[0],
2242+
Statement::If(IfStatement {
2243+
if_block: ConditionalStatementBlock {
2244+
start_token: AttachedToken(TokenWithSpan::wrap(sqlparser::tokenizer::Token::Word(
2245+
sqlparser::tokenizer::Word {
2246+
value: "IF".to_string(),
2247+
quote_style: None,
2248+
keyword: Keyword::IF
2249+
}
2250+
))),
2251+
condition: Some(Expr::BinaryOp {
2252+
left: Box::new(Expr::Value((number("1")).with_empty_span())),
2253+
op: sqlparser::ast::BinaryOperator::Eq,
2254+
right: Box::new(Expr::Value((number("2")).with_empty_span())),
2255+
}),
2256+
then_token: None,
2257+
conditional_statements: ConditionalStatements::Sequence {
2258+
statements: vec![Statement::Query(Box::new(Query {
2259+
with: None,
2260+
limit_clause: None,
2261+
fetch: None,
2262+
locks: vec![],
2263+
for_clause: None,
2264+
order_by: None,
2265+
settings: None,
2266+
format_clause: None,
2267+
body: Box::new(SetExpr::Select(Box::new(Select {
2268+
select_token: AttachedToken::empty(),
2269+
distinct: None,
2270+
top: None,
2271+
top_before_distinct: false,
2272+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(
2273+
(number("3")).with_empty_span()
2274+
))],
2275+
into: None,
2276+
from: vec![],
2277+
lateral_views: vec![],
2278+
prewhere: None,
2279+
selection: None,
2280+
group_by: GroupByExpr::Expressions(vec![], vec![]),
2281+
cluster_by: vec![],
2282+
distribute_by: vec![],
2283+
sort_by: vec![],
2284+
having: None,
2285+
named_window: vec![],
2286+
window_before_qualify: false,
2287+
qualify: None,
2288+
value_table_mode: None,
2289+
connect_by: None,
2290+
flavor: SelectFlavor::Standard,
2291+
}))),
2292+
}))],
2293+
},
2294+
},
2295+
elseif_blocks: vec![],
2296+
else_block: None,
2297+
end_token: None,
2298+
})
2299+
);
2300+
assert_eq!(statements[1], Statement::Go(GoStatement { count: None }));
2301+
}

0 commit comments

Comments
 (0)