Skip to content

Commit e93a186

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

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};
@@ -2106,3 +2107,75 @@ 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((number("1")).with_empty_span())),
2133+
op: sqlparser::ast::BinaryOperator::Eq,
2134+
right: Box::new(Expr::Value((number("2")).with_empty_span())),
2135+
}),
2136+
then_token: None,
2137+
conditional_statements: ConditionalStatements::Sequence {
2138+
statements: vec![Statement::Query(Box::new(Query {
2139+
with: None,
2140+
limit_clause: None,
2141+
fetch: None,
2142+
locks: vec![],
2143+
for_clause: None,
2144+
order_by: None,
2145+
settings: None,
2146+
format_clause: None,
2147+
body: Box::new(SetExpr::Select(Box::new(Select {
2148+
select_token: AttachedToken::empty(),
2149+
distinct: None,
2150+
top: None,
2151+
top_before_distinct: false,
2152+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(
2153+
(number("3")).with_empty_span()
2154+
))],
2155+
into: None,
2156+
from: vec![],
2157+
lateral_views: vec![],
2158+
prewhere: None,
2159+
selection: None,
2160+
group_by: GroupByExpr::Expressions(vec![], vec![]),
2161+
cluster_by: vec![],
2162+
distribute_by: vec![],
2163+
sort_by: vec![],
2164+
having: None,
2165+
named_window: vec![],
2166+
window_before_qualify: false,
2167+
qualify: None,
2168+
value_table_mode: None,
2169+
connect_by: None,
2170+
flavor: SelectFlavor::Standard,
2171+
}))),
2172+
}))],
2173+
},
2174+
},
2175+
elseif_blocks: vec![],
2176+
else_block: None,
2177+
end_token: None,
2178+
})
2179+
);
2180+
assert_eq!(statements[1], Statement::Go(GoStatement { count: None }));
2181+
}

0 commit comments

Comments
 (0)