Skip to content

Commit 374985d

Browse files
author
Takahiro Ebato
committed
refactor BEGIN TRANSACTION statements in SQLite
1 parent 1318e49 commit 374985d

File tree

7 files changed

+24
-36
lines changed

7 files changed

+24
-36
lines changed

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ pub enum Statement {
18191819
StartTransaction {
18201820
modes: Vec<TransactionMode>,
18211821
begin: bool,
1822-
// Only for sqlite
1822+
/// Only for SQLite
18231823
modifier: Option<TransactionModifier>,
18241824
},
18251825
/// `SET TRANSACTION ...`

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ impl Dialect for GenericDialect {
3838
fn supports_group_by_expr(&self) -> bool {
3939
true
4040
}
41+
42+
fn supports_start_transaction_modifier(&self) -> bool {
43+
true
44+
}
4145
}

src/dialect/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ pub trait Dialect: Debug + Any {
128128
fn supports_in_empty_list(&self) -> bool {
129129
false
130130
}
131+
/// Returns true if the dialect supports `BEGIN {DEFERRED | IMMEDIATE | EXCLUSIVE} [TRANSACTION]` statements
132+
fn supports_start_transaction_modifier(&self) -> bool {
133+
false
134+
}
131135
/// Returns true if the dialect has a CONVERT function which accepts a type first
132136
/// and an expression second, e.g. `CONVERT(varchar, 1)`
133137
fn convert_type_before_value(&self) -> bool {

src/dialect/sqlite.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ impl Dialect for SQLiteDialect {
4040
true
4141
}
4242

43+
fn supports_start_transaction_modifier(&self) -> bool {
44+
true
45+
}
46+
4347
fn is_identifier_part(&self, ch: char) -> bool {
4448
self.is_identifier_start(ch) || ch.is_ascii_digit()
4549
}

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7839,7 +7839,7 @@ impl<'a> Parser<'a> {
78397839
}
78407840

78417841
pub fn parse_begin(&mut self) -> Result<Statement, ParserError> {
7842-
let modifier = if !dialect_of!(self is SQLiteDialect) {
7842+
let modifier = if !self.dialect.supports_start_transaction_modifier() {
78437843
None
78447844
} else if self.parse_keyword(Keyword::DEFERRED) {
78457845
Some(TransactionModifier::Deferred)

tests/sqlparser_common.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6232,40 +6232,6 @@ fn parse_start_transaction() {
62326232
);
62336233
}
62346234

6235-
#[test]
6236-
fn parse_start_transaction_sqlite() {
6237-
let dialect = SQLiteDialect {};
6238-
6239-
let check = |sql: &str, expected_modifier: Option<TransactionModifier>| match Parser::parse_sql(
6240-
&dialect, &sql,
6241-
)
6242-
.unwrap()
6243-
.pop()
6244-
.unwrap()
6245-
{
6246-
Statement::StartTransaction { modifier, .. } => assert_eq!(modifier, expected_modifier),
6247-
_ => panic!("{}", sql),
6248-
};
6249-
6250-
let sql = "BEGIN DEFERRED";
6251-
check(sql, Some(TransactionModifier::Deferred));
6252-
6253-
let sql = "BEGIN DEFERRED TRANSACTION";
6254-
check(sql, Some(TransactionModifier::Deferred));
6255-
6256-
let sql = "BEGIN IMMEDIATE";
6257-
check(sql, Some(TransactionModifier::Immediate));
6258-
6259-
let sql = "BEGIN IMMEDIATE TRANSACTION";
6260-
check(sql, Some(TransactionModifier::Immediate));
6261-
6262-
let sql = "BEGIN EXCLUSIVE";
6263-
check(sql, Some(TransactionModifier::Exclusive));
6264-
6265-
let sql = "BEGIN EXCLUSIVE TRANSACTION";
6266-
check(sql, Some(TransactionModifier::Exclusive));
6267-
}
6268-
62696235
#[test]
62706236
fn parse_set_transaction() {
62716237
// SET TRANSACTION shares transaction mode parsing code with START

tests/sqlparser_sqlite.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ fn invalid_empty_list() {
431431
);
432432
}
433433

434+
#[test]
435+
fn parse_start_transaction_with_modifier() {
436+
sqlite_and_generic().verified_stmt("BEGIN DEFERRED TRANSACTION");
437+
sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE TRANSACTION");
438+
sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE TRANSACTION");
439+
sqlite_and_generic().one_statement_parses_to("BEGIN DEFERRED", "BEGIN DEFERRED TRANSACTION");
440+
sqlite_and_generic().one_statement_parses_to("BEGIN IMMEDIATE", "BEGIN IMMEDIATE TRANSACTION");
441+
sqlite_and_generic().one_statement_parses_to("BEGIN EXCLUSIVE", "BEGIN EXCLUSIVE TRANSACTION");
442+
}
443+
434444
fn sqlite() -> TestedDialects {
435445
TestedDialects {
436446
dialects: vec![Box::new(SQLiteDialect {})],

0 commit comments

Comments
 (0)