Skip to content

Commit c761f0b

Browse files
authored
Fix displaying WORK or TRANSACTION after BEGIN (#1565)
1 parent 6517da6 commit c761f0b

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

src/ast/mod.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -2944,6 +2944,7 @@ pub enum Statement {
29442944
StartTransaction {
29452945
modes: Vec<TransactionMode>,
29462946
begin: bool,
2947+
transaction: Option<BeginTransactionKind>,
29472948
/// Only for SQLite
29482949
modifier: Option<TransactionModifier>,
29492950
},
@@ -4519,16 +4520,20 @@ impl fmt::Display for Statement {
45194520
Statement::StartTransaction {
45204521
modes,
45214522
begin: syntax_begin,
4523+
transaction,
45224524
modifier,
45234525
} => {
45244526
if *syntax_begin {
45254527
if let Some(modifier) = *modifier {
4526-
write!(f, "BEGIN {} TRANSACTION", modifier)?;
4528+
write!(f, "BEGIN {}", modifier)?;
45274529
} else {
4528-
write!(f, "BEGIN TRANSACTION")?;
4530+
write!(f, "BEGIN")?;
45294531
}
45304532
} else {
4531-
write!(f, "START TRANSACTION")?;
4533+
write!(f, "START")?;
4534+
}
4535+
if let Some(transaction) = transaction {
4536+
write!(f, " {transaction}")?;
45324537
}
45334538
if !modes.is_empty() {
45344539
write!(f, " {}", display_comma_separated(modes))?;
@@ -5023,6 +5028,24 @@ pub enum TruncateCascadeOption {
50235028
Restrict,
50245029
}
50255030

5031+
/// Transaction started with [ TRANSACTION | WORK ]
5032+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5033+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5034+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5035+
pub enum BeginTransactionKind {
5036+
Transaction,
5037+
Work,
5038+
}
5039+
5040+
impl Display for BeginTransactionKind {
5041+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5042+
match self {
5043+
BeginTransactionKind::Transaction => write!(f, "TRANSACTION"),
5044+
BeginTransactionKind::Work => write!(f, "WORK"),
5045+
}
5046+
}
5047+
}
5048+
50265049
/// Can use to describe options in create sequence or table column type identity
50275050
/// [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
50285051
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]

src/parser/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -12123,6 +12123,7 @@ impl<'a> Parser<'a> {
1212312123
Ok(Statement::StartTransaction {
1212412124
modes: self.parse_transaction_modes()?,
1212512125
begin: false,
12126+
transaction: Some(BeginTransactionKind::Transaction),
1212612127
modifier: None,
1212712128
})
1212812129
}
@@ -12139,10 +12140,15 @@ impl<'a> Parser<'a> {
1213912140
} else {
1214012141
None
1214112142
};
12142-
let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
12143+
let transaction = match self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]) {
12144+
Some(Keyword::TRANSACTION) => Some(BeginTransactionKind::Transaction),
12145+
Some(Keyword::WORK) => Some(BeginTransactionKind::Work),
12146+
_ => None,
12147+
};
1214312148
Ok(Statement::StartTransaction {
1214412149
modes: self.parse_transaction_modes()?,
1214512150
begin: true,
12151+
transaction,
1214612152
modifier,
1214712153
})
1214812154
}

tests/sqlparser_common.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7736,9 +7736,9 @@ fn parse_start_transaction() {
77367736
}
77377737

77387738
verified_stmt("START TRANSACTION");
7739-
one_statement_parses_to("BEGIN", "BEGIN TRANSACTION");
7740-
one_statement_parses_to("BEGIN WORK", "BEGIN TRANSACTION");
7741-
one_statement_parses_to("BEGIN TRANSACTION", "BEGIN TRANSACTION");
7739+
verified_stmt("BEGIN");
7740+
verified_stmt("BEGIN WORK");
7741+
verified_stmt("BEGIN TRANSACTION");
77427742

77437743
verified_stmt("START TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
77447744
verified_stmt("START TRANSACTION ISOLATION LEVEL READ COMMITTED");

tests/sqlparser_mysql.rs

+5
Original file line numberDiff line numberDiff line change
@@ -3032,3 +3032,8 @@ fn parse_longblob_type() {
30323032
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar MEDIUMTEXT)");
30333033
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar LONGTEXT)");
30343034
}
3035+
3036+
#[test]
3037+
fn parse_begin_without_transaction() {
3038+
mysql().verified_stmt("BEGIN");
3039+
}

tests/sqlparser_sqlite.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,9 @@ fn parse_start_transaction_with_modifier() {
527527
sqlite_and_generic().verified_stmt("BEGIN DEFERRED TRANSACTION");
528528
sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE TRANSACTION");
529529
sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE TRANSACTION");
530-
sqlite_and_generic().one_statement_parses_to("BEGIN DEFERRED", "BEGIN DEFERRED TRANSACTION");
531-
sqlite_and_generic().one_statement_parses_to("BEGIN IMMEDIATE", "BEGIN IMMEDIATE TRANSACTION");
532-
sqlite_and_generic().one_statement_parses_to("BEGIN EXCLUSIVE", "BEGIN EXCLUSIVE TRANSACTION");
530+
sqlite_and_generic().verified_stmt("BEGIN DEFERRED");
531+
sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE");
532+
sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE");
533533

534534
let unsupported_dialects = TestedDialects::new(
535535
all_dialects()

0 commit comments

Comments
 (0)