Skip to content

Fix displaying WORK or TRANSACTION after BEGIN #1565

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 4 commits into from
Dec 4, 2024
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
29 changes: 26 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,7 @@ pub enum Statement {
StartTransaction {
modes: Vec<TransactionMode>,
begin: bool,
transaction: Option<BeginTransactionKind>,
/// Only for SQLite
modifier: Option<TransactionModifier>,
},
Expand Down Expand Up @@ -4518,16 +4519,20 @@ impl fmt::Display for Statement {
Statement::StartTransaction {
modes,
begin: syntax_begin,
transaction,
modifier,
} => {
if *syntax_begin {
if let Some(modifier) = *modifier {
write!(f, "BEGIN {} TRANSACTION", modifier)?;
write!(f, "BEGIN {}", modifier)?;
} else {
write!(f, "BEGIN TRANSACTION")?;
write!(f, "BEGIN")?;
}
} else {
write!(f, "START TRANSACTION")?;
write!(f, "START")?;
}
if let Some(transaction) = transaction {
write!(f, " {transaction}")?;
}
if !modes.is_empty() {
write!(f, " {}", display_comma_separated(modes))?;
Expand Down Expand Up @@ -5022,6 +5027,24 @@ pub enum TruncateCascadeOption {
Restrict,
}

/// Transaction started with [ TRANSACTION | WORK ]
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum BeginTransactionKind {
Transaction,
Work,
}

impl Display for BeginTransactionKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BeginTransactionKind::Transaction => write!(f, "TRANSACTION"),
BeginTransactionKind::Work => write!(f, "WORK"),
}
}
}

/// Can use to describe options in create sequence or table column type identity
/// [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down
8 changes: 7 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12099,6 +12099,7 @@ impl<'a> Parser<'a> {
Ok(Statement::StartTransaction {
modes: self.parse_transaction_modes()?,
begin: false,
transaction: Some(BeginTransactionKind::Transaction),
modifier: None,
})
}
Expand All @@ -12115,10 +12116,15 @@ impl<'a> Parser<'a> {
} else {
None
};
let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
let transaction = match self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]) {
Some(Keyword::TRANSACTION) => Some(BeginTransactionKind::Transaction),
Some(Keyword::WORK) => Some(BeginTransactionKind::Work),
_ => None,
};
Ok(Statement::StartTransaction {
modes: self.parse_transaction_modes()?,
begin: true,
transaction,
modifier,
})
}
Expand Down
6 changes: 3 additions & 3 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7736,9 +7736,9 @@ fn parse_start_transaction() {
}

verified_stmt("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("BEGIN");
verified_stmt("BEGIN WORK");
verified_stmt("BEGIN TRANSACTION");

verified_stmt("START TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
verified_stmt("START TRANSACTION ISOLATION LEVEL READ COMMITTED");
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3031,3 +3031,8 @@ fn parse_longblob_type() {
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar MEDIUMTEXT)");
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar LONGTEXT)");
}

#[test]
fn parse_begin_without_transaction() {
mysql().verified_stmt("BEGIN");
}
6 changes: 3 additions & 3 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ fn parse_start_transaction_with_modifier() {
sqlite_and_generic().verified_stmt("BEGIN DEFERRED TRANSACTION");
sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE TRANSACTION");
sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE TRANSACTION");
sqlite_and_generic().one_statement_parses_to("BEGIN DEFERRED", "BEGIN DEFERRED TRANSACTION");
sqlite_and_generic().one_statement_parses_to("BEGIN IMMEDIATE", "BEGIN IMMEDIATE TRANSACTION");
sqlite_and_generic().one_statement_parses_to("BEGIN EXCLUSIVE", "BEGIN EXCLUSIVE TRANSACTION");
sqlite_and_generic().verified_stmt("BEGIN DEFERRED");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE");
sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE");

let unsupported_dialects = TestedDialects::new(
all_dialects()
Expand Down
Loading