Skip to content

Commit 985d2e0

Browse files
committed
Drop ExceptionClause, fix Display
1 parent 4848e91 commit 985d2e0

File tree

4 files changed

+22
-74
lines changed

4 files changed

+22
-74
lines changed

src/ast/mod.rs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,44 +2982,6 @@ impl From<Set> for Statement {
29822982
}
29832983
}
29842984

2985-
/// An exception representing exception handling with the `EXCEPTION` keyword.
2986-
///
2987-
/// Snowflake: <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/exception>
2988-
/// BigQuery: <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend>
2989-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2990-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2991-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2992-
pub struct ExceptionClause {
2993-
/// When represents each `WHEN` case
2994-
pub when: Vec<ExceptionWhen>,
2995-
/// The exception that is being raised or the current exception being handled if `None`.
2996-
///
2997-
/// Example
2998-
/// RAISE;
2999-
/// RAISE MY_EXCEPTION;
3000-
/// RAISE USING MESSAGE = "Some error";
3001-
///
3002-
/// BigQuery: <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#raise>
3003-
/// Snowflake: <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/raise>
3004-
// pub raises: Option<Option<Ident>>,
3005-
pub raises: Option<Box<Statement>>,
3006-
}
3007-
3008-
impl Display for ExceptionClause {
3009-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3010-
write!(f, " EXCEPTION")?;
3011-
for w in &self.when {
3012-
write!(f, "{w}")?;
3013-
}
3014-
3015-
if let Some(raises) = &self.raises {
3016-
write!(f, " {raises};")?;
3017-
}
3018-
3019-
Ok(())
3020-
}
3021-
}
3022-
30232985
/// A representation of a `WHEN` arm with all the identifiers catched and the statements to execute
30242986
/// for the arm.
30252987
///
@@ -3037,7 +2999,7 @@ impl Display for ExceptionWhen {
30372999
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30383000
write!(
30393001
f,
3040-
" WHEN {idents} THEN",
3002+
"WHEN {idents} THEN",
30413003
idents = display_separated(&self.idents, " OR ")
30423004
)?;
30433005

@@ -3738,24 +3700,20 @@ pub enum Statement {
37383700
/// END;
37393701
/// ```
37403702
statements: Vec<Statement>,
3741-
/// Exception handling with exception clauses and raises.
3703+
/// Exception handling with exception clauses.
37423704
/// Example:
37433705
/// ```sql
3744-
/// BEGIN
3745-
/// SELECT 1;
37463706
/// EXCEPTION
37473707
/// WHEN EXCEPTION_1 THEN
37483708
/// SELECT 2;
37493709
/// WHEN EXCEPTION_2 OR EXCEPTION_3 THEN
37503710
/// SELECT 3;
37513711
/// WHEN OTHER THEN
37523712
/// SELECT 4;
3753-
/// RAISE;
3754-
/// END;
37553713
/// ```
37563714
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend>
37573715
/// <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/exception>
3758-
exception: Option<ExceptionClause>,
3716+
exception: Option<Vec<ExceptionWhen>>,
37593717
/// TRUE if the statement has an `END` keyword.
37603718
has_end_keyword: bool,
37613719
},
@@ -5600,7 +5558,7 @@ impl fmt::Display for Statement {
56005558
transaction,
56015559
modifier,
56025560
statements,
5603-
exception,
5561+
exception: exception_handling,
56045562
has_end_keyword,
56055563
} => {
56065564
if *syntax_begin {
@@ -5622,8 +5580,11 @@ impl fmt::Display for Statement {
56225580
write!(f, " ")?;
56235581
format_statement_list(f, statements)?;
56245582
}
5625-
if let Some(exception) = exception {
5626-
write!(f, "{exception}")?;
5583+
if let Some(exception_when) = exception_handling {
5584+
write!(f, " EXCEPTION")?;
5585+
for when in exception_when {
5586+
write!(f, " {when}")?;
5587+
}
56275588
}
56285589
if *has_end_keyword {
56295590
write!(f, " END")?;

src/parser/mod.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15136,15 +15136,11 @@ impl<'a> Parser<'a> {
1513615136
pub fn parse_begin_exception_end(&mut self) -> Result<Statement, ParserError> {
1513715137
let statements = self.parse_statement_list(&[Keyword::EXCEPTION, Keyword::END])?;
1513815138

15139-
let exception = if self.parse_keyword(Keyword::EXCEPTION) {
15139+
let exception_handling = if self.parse_keyword(Keyword::EXCEPTION) {
1514015140
let mut when = Vec::new();
1514115141

15142-
// We can have multiple `WHEN` arms so we consume all cases until `END` or an exception
15143-
// is `RAISE`ed.
15144-
while self
15145-
.peek_one_of_keywords(&[Keyword::END, Keyword::RAISE])
15146-
.is_none()
15147-
{
15142+
// We can have multiple `WHEN` arms so we consume all cases until `END`
15143+
while !self.peek_keyword(Keyword::END) {
1514815144
self.expect_keyword(Keyword::WHEN)?;
1514915145

1515015146
// Each `WHEN` case can have one or more conditions, e.g.
@@ -15159,21 +15155,12 @@ impl<'a> Parser<'a> {
1515915155
self.maybe_parse(|p| p.expect_keyword(Keyword::OR))?;
1516015156
}
1516115157

15162-
let statements =
15163-
self.parse_statement_list(&[Keyword::WHEN, Keyword::RAISE, Keyword::END])?;
15158+
let statements = self.parse_statement_list(&[Keyword::WHEN, Keyword::END])?;
1516415159

1516515160
when.push(ExceptionWhen { idents, statements });
1516615161
}
1516715162

15168-
let raises = if self.peek_keyword(Keyword::RAISE) {
15169-
let raises = Some(Box::new(self.parse_raise_stmt()?));
15170-
self.expect_token(&Token::SemiColon)?;
15171-
raises
15172-
} else {
15173-
None
15174-
};
15175-
15176-
Some(ExceptionClause { when, raises })
15163+
Some(when)
1517715164
} else {
1517815165
None
1517915166
};
@@ -15183,7 +15170,7 @@ impl<'a> Parser<'a> {
1518315170
Ok(Statement::StartTransaction {
1518415171
begin: true,
1518515172
statements,
15186-
exception,
15173+
exception: exception_handling,
1518715174
has_end_keyword: true,
1518815175
transaction: None,
1518915176
modifier: None,

tests/sqlparser_bigquery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn parse_at_at_identifier() {
261261

262262
#[test]
263263
fn parse_begin() {
264-
let sql = r#"BEGIN SELECT 1; EXCEPTION WHEN ERROR THEN SELECT 2; END"#;
264+
let sql = r#"BEGIN SELECT 1; EXCEPTION WHEN ERROR THEN SELECT 2; RAISE USING MESSAGE = FORMAT('ERR: %s', 'Bad'); END"#;
265265
let Statement::StartTransaction {
266266
statements,
267267
exception,
@@ -275,7 +275,7 @@ fn parse_begin() {
275275
assert!(exception.is_some());
276276

277277
let exception = exception.unwrap();
278-
assert_eq!(1, exception.when.len());
278+
assert_eq!(1, exception.len());
279279
assert!(has_end_keyword);
280280

281281
bigquery().verified_stmt(

tests/sqlparser_snowflake.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4124,9 +4124,9 @@ END
41244124
assert!(has_end_keyword);
41254125

41264126
let exception = exception.unwrap();
4127-
assert_eq!(3, exception.when.len());
4128-
assert_eq!(1, exception.when[0].idents.len());
4129-
assert_eq!(1, exception.when[0].statements.len());
4130-
assert_eq!(2, exception.when[1].idents.len());
4131-
assert_eq!(2, exception.when[1].statements.len());
4127+
assert_eq!(3, exception.len());
4128+
assert_eq!(1, exception[0].idents.len());
4129+
assert_eq!(1, exception[0].statements.len());
4130+
assert_eq!(2, exception[1].idents.len());
4131+
assert_eq!(2, exception[1].statements.len());
41324132
}

0 commit comments

Comments
 (0)