Skip to content

Commit 97f3e65

Browse files
committed
add support for default session transaction characteristics
Signed-off-by: poonai <[email protected]>
1 parent 3b28cbf commit 97f3e65

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/ast/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ pub enum Statement {
786786
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
787787
StartTransaction { modes: Vec<TransactionMode> },
788788
/// `SET TRANSACTION ...`
789-
SetTransaction { modes: Vec<TransactionMode>, snapshot: Option<Value> },
789+
SetTransaction { modes: Vec<TransactionMode>, snapshot: Option<Value>, session: bool },
790790
/// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
791791
Commit { chain: bool },
792792
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
@@ -1369,8 +1369,12 @@ impl fmt::Display for Statement {
13691369
}
13701370
Ok(())
13711371
}
1372-
Statement::SetTransaction { modes, snapshot } => {
1373-
write!(f, "SET TRANSACTION")?;
1372+
Statement::SetTransaction { modes, snapshot, session } => {
1373+
if *session {
1374+
write!(f, "SET SESSION CHARACTERISTICS AS TRANSACTION")?;
1375+
} else {
1376+
write!(f, "SET TRANSACTION")?;
1377+
}
13741378
if !modes.is_empty() {
13751379
write!(f, " {}", display_comma_separated(modes))?;
13761380
}

src/parser.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,17 +2670,26 @@ impl<'a> Parser<'a> {
26702670
value: values,
26712671
});
26722672
}
2673+
} else if variable.value == "CHARACTERISTICS" {
2674+
self.expect_keywords(&[Keyword::AS, Keyword::TRANSACTION])?;
2675+
Ok(Statement::SetTransaction {
2676+
modes: self.parse_transaction_modes()?,
2677+
snapshot: None,
2678+
session: true,
2679+
})
26732680
} else if variable.value == "TRANSACTION" && modifier.is_none() {
2674-
if self.parse_keyword(Keyword::SNAPSHOT){
2681+
if self.parse_keyword(Keyword::SNAPSHOT) {
26752682
let snaphot_id = self.parse_value()?;
2676-
return Ok(Statement::SetTransaction{
2683+
return Ok(Statement::SetTransaction {
26772684
modes: vec![],
2678-
snapshot: Some(snaphot_id)
2679-
})
2685+
snapshot: Some(snaphot_id),
2686+
session: false
2687+
});
26802688
}
26812689
Ok(Statement::SetTransaction {
26822690
modes: self.parse_transaction_modes()?,
2683-
snapshot: None
2691+
snapshot: None,
2692+
session: false
26842693
})
26852694
} else {
26862695
self.expected("equals sign or TO", self.peek_token())

tests/sqlparser_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3586,7 +3586,7 @@ fn parse_set_transaction() {
35863586
// TRANSACTION, so no need to duplicate the tests here. We just do a quick
35873587
// sanity check.
35883588
match verified_stmt("SET TRANSACTION READ ONLY, READ WRITE, ISOLATION LEVEL SERIALIZABLE") {
3589-
Statement::SetTransaction { modes, snapshot:None } => assert_eq!(
3589+
Statement::SetTransaction { modes, .. } => assert_eq!(
35903590
modes,
35913591
vec![
35923592
TransactionMode::AccessMode(TransactionAccessMode::ReadOnly),

tests/sqlparser_postgres.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,27 @@ fn parse_map_access_expr() {
726726
#[test]
727727
fn test_transaction_statement() {
728728
let statement = pg().verified_stmt("SET TRANSACTION SNAPSHOT '000003A1-1'");
729-
assert_eq!(statement, Statement::SetTransaction{
730-
modes: vec![],
731-
snapshot: Some(Value::SingleQuotedString(String::from("000003A1-1")))
732-
})
729+
assert_eq!(
730+
statement,
731+
Statement::SetTransaction {
732+
modes: vec![],
733+
snapshot: Some(Value::SingleQuotedString(String::from("000003A1-1"))),
734+
session: false
735+
}
736+
);
737+
let statement = pg().verified_stmt("SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY, READ WRITE, ISOLATION LEVEL SERIALIZABLE");
738+
assert_eq!(
739+
statement,
740+
Statement::SetTransaction {
741+
modes: vec![
742+
TransactionMode::AccessMode(TransactionAccessMode::ReadOnly),
743+
TransactionMode::AccessMode(TransactionAccessMode::ReadWrite),
744+
TransactionMode::IsolationLevel(TransactionIsolationLevel::Serializable),
745+
],
746+
snapshot: None,
747+
session: true
748+
}
749+
);
733750
}
734751

735752
fn pg() -> TestedDialects {

0 commit comments

Comments
 (0)