Skip to content

Commit

Permalink
Fix "BEGIN TRANSACTION" being serialized as "START TRANSACTION" (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa authored Jul 27, 2023
1 parent 0ddb853 commit 10a6ec5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
22 changes: 18 additions & 4 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1540,8 +1540,15 @@ pub enum Statement {
///
/// Note: This is a MySQL-specific statement.
Use { db_name: Ident },
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
StartTransaction { modes: Vec<TransactionMode> },
/// `START [ TRANSACTION | WORK ] | START TRANSACTION } ...`
/// If `begin` is false.
///
/// `BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
/// If `begin` is true
StartTransaction {
modes: Vec<TransactionMode>,
begin: bool,
},
/// `SET TRANSACTION ...`
SetTransaction {
modes: Vec<TransactionMode>,
Expand Down Expand Up @@ -2720,8 +2727,15 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::StartTransaction { modes } => {
write!(f, "START TRANSACTION")?;
Statement::StartTransaction {
modes,
begin: syntax_begin,
} => {
if *syntax_begin {
write!(f, "BEGIN TRANSACTION")?;
} else {
write!(f, "START TRANSACTION")?;
}
if !modes.is_empty() {
write!(f, " {}", display_comma_separated(modes))?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6905,13 +6905,15 @@ impl<'a> Parser<'a> {
self.expect_keyword(Keyword::TRANSACTION)?;
Ok(Statement::StartTransaction {
modes: self.parse_transaction_modes()?,
begin: false,
})
}

pub fn parse_begin(&mut self) -> Result<Statement, ParserError> {
let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
Ok(Statement::StartTransaction {
modes: self.parse_transaction_modes()?,
begin: true,
})
}

Expand Down
10 changes: 5 additions & 5 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5732,7 +5732,7 @@ fn lateral_derived() {
#[test]
fn parse_start_transaction() {
match verified_stmt("START TRANSACTION READ ONLY, READ WRITE, ISOLATION LEVEL SERIALIZABLE") {
Statement::StartTransaction { modes } => assert_eq!(
Statement::StartTransaction { modes, .. } => assert_eq!(
modes,
vec![
TransactionMode::AccessMode(TransactionAccessMode::ReadOnly),
Expand All @@ -5749,7 +5749,7 @@ fn parse_start_transaction() {
"START TRANSACTION READ ONLY READ WRITE ISOLATION LEVEL SERIALIZABLE",
"START TRANSACTION READ ONLY, READ WRITE, ISOLATION LEVEL SERIALIZABLE",
) {
Statement::StartTransaction { modes } => assert_eq!(
Statement::StartTransaction { modes, .. } => assert_eq!(
modes,
vec![
TransactionMode::AccessMode(TransactionAccessMode::ReadOnly),
Expand All @@ -5761,9 +5761,9 @@ fn parse_start_transaction() {
}

verified_stmt("START TRANSACTION");
one_statement_parses_to("BEGIN", "START TRANSACTION");
one_statement_parses_to("BEGIN WORK", "START TRANSACTION");
one_statement_parses_to("BEGIN TRANSACTION", "START TRANSACTION");
one_statement_parses_to("BEGIN", "BEGIN TRANSACTION");
one_statement_parses_to("BEGIN WORK", "BEGIN TRANSACTION");
one_statement_parses_to("BEGIN TRANSACTION", "BEGIN TRANSACTION");

verified_stmt("START TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
verified_stmt("START TRANSACTION ISOLATION LEVEL READ COMMITTED");
Expand Down

0 comments on commit 10a6ec5

Please sign in to comment.