Skip to content

Commit 3e4df37

Browse files
committed
Enable GO to terminate an IF statement
1 parent 6296781 commit 3e4df37

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-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

+74-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};
@@ -2123,3 +2124,75 @@ fn parse_mssql_go_keyword() {
21232124
"sql parser error: Expected: end of statement, found: x"
21242125
);
21252126
}
2127+
2128+
#[test]
2129+
fn test_mssql_if_and_go() {
2130+
let sql = r#"
2131+
IF 1 = 2
2132+
SELECT 3;
2133+
GO
2134+
"#;
2135+
let statements = ms().parse_sql_statements(sql).unwrap();
2136+
assert_eq!(2, statements.len());
2137+
assert_eq!(
2138+
statements[0],
2139+
Statement::If(IfStatement {
2140+
if_block: ConditionalStatementBlock {
2141+
start_token: AttachedToken(TokenWithSpan::wrap(sqlparser::tokenizer::Token::Word(
2142+
sqlparser::tokenizer::Word {
2143+
value: "IF".to_string(),
2144+
quote_style: None,
2145+
keyword: Keyword::IF
2146+
}
2147+
))),
2148+
condition: Some(Expr::BinaryOp {
2149+
left: Box::new(Expr::Value((number("1")).with_empty_span())),
2150+
op: sqlparser::ast::BinaryOperator::Eq,
2151+
right: Box::new(Expr::Value((number("2")).with_empty_span())),
2152+
}),
2153+
then_token: None,
2154+
conditional_statements: ConditionalStatements::Sequence {
2155+
statements: vec![Statement::Query(Box::new(Query {
2156+
with: None,
2157+
limit_clause: None,
2158+
fetch: None,
2159+
locks: vec![],
2160+
for_clause: None,
2161+
order_by: None,
2162+
settings: None,
2163+
format_clause: None,
2164+
body: Box::new(SetExpr::Select(Box::new(Select {
2165+
select_token: AttachedToken::empty(),
2166+
distinct: None,
2167+
top: None,
2168+
top_before_distinct: false,
2169+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(
2170+
(number("3")).with_empty_span()
2171+
))],
2172+
into: None,
2173+
from: vec![],
2174+
lateral_views: vec![],
2175+
prewhere: None,
2176+
selection: None,
2177+
group_by: GroupByExpr::Expressions(vec![], vec![]),
2178+
cluster_by: vec![],
2179+
distribute_by: vec![],
2180+
sort_by: vec![],
2181+
having: None,
2182+
named_window: vec![],
2183+
window_before_qualify: false,
2184+
qualify: None,
2185+
value_table_mode: None,
2186+
connect_by: None,
2187+
flavor: SelectFlavor::Standard,
2188+
}))),
2189+
}))],
2190+
},
2191+
},
2192+
elseif_blocks: vec![],
2193+
else_block: None,
2194+
end_token: None,
2195+
})
2196+
);
2197+
assert_eq!(statements[1], Statement::Go(GoStatement { count: None }));
2198+
}

0 commit comments

Comments
 (0)