Skip to content

Adds support for PostgreSQL "END" #1035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rust 1.73.0
16 changes: 15 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,11 @@ impl<'a> Parser<'a> {
Token::EOF => break,

// end of statement
Token::Word(word) if word.keyword == Keyword::END => break,
Token::Word(word) => {
if expecting_statement_delimiter && word.keyword == Keyword::END {
break;
}
}
_ => {}
}

Expand Down Expand Up @@ -501,6 +505,10 @@ impl<'a> Parser<'a> {
// standard `START TRANSACTION` statement. It is supported
// by at least PostgreSQL and MySQL.
Keyword::BEGIN => Ok(self.parse_begin()?),
// `END` is a nonstandard but common alias for the
// standard `COMMIT TRANSACTION` statement. It is supported
// by PostgreSQL.
Keyword::END => Ok(self.parse_end()?),
Keyword::SAVEPOINT => Ok(self.parse_savepoint()?),
Keyword::RELEASE => Ok(self.parse_release()?),
Keyword::COMMIT => Ok(self.parse_commit()?),
Expand Down Expand Up @@ -7808,6 +7816,12 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_end(&mut self) -> Result<Statement, ParserError> {
Ok(Statement::Commit {
chain: self.parse_commit_rollback_chain()?,
})
}

pub fn parse_transaction_modes(&mut self) -> Result<Vec<TransactionMode>, ParserError> {
let mut modes = vec![];
let mut required = false;
Expand Down
11 changes: 11 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6231,6 +6231,17 @@ fn parse_commit() {
one_statement_parses_to("COMMIT TRANSACTION", "COMMIT");
}

#[test]
fn parse_end() {
one_statement_parses_to("END AND NO CHAIN", "COMMIT");
one_statement_parses_to("END WORK AND NO CHAIN", "COMMIT");
one_statement_parses_to("END TRANSACTION AND NO CHAIN", "COMMIT");
one_statement_parses_to("END WORK AND CHAIN", "COMMIT AND CHAIN");
one_statement_parses_to("END TRANSACTION AND CHAIN", "COMMIT AND CHAIN");
one_statement_parses_to("END WORK", "COMMIT");
one_statement_parses_to("END TRANSACTION", "COMMIT");
}

#[test]
fn parse_rollback() {
match verified_stmt("ROLLBACK") {
Expand Down