Skip to content

Commit 6f59fb5

Browse files
committed
Start of transaction configuration
1 parent 269c176 commit 6f59fb5

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

src/lib.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,12 @@ pub enum IsolationLevel {
322322
}
323323

324324
impl IsolationLevel {
325-
fn to_set_query(&self) -> &'static str {
325+
fn to_sql(&self) -> &'static str {
326326
match *self {
327-
IsolationLevel::ReadUncommitted => {
328-
"SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"
329-
}
330-
IsolationLevel::ReadCommitted => {
331-
"SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED"
332-
}
333-
IsolationLevel::RepeatableRead => {
334-
"SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ"
335-
}
336-
IsolationLevel::Serializable => {
337-
"SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE"
338-
}
327+
IsolationLevel::ReadUncommitted => "READ UNCOMMITTED",
328+
IsolationLevel::ReadCommitted => "READ COMMITTED",
329+
IsolationLevel::RepeatableRead => "REPEATABLE READ",
330+
IsolationLevel::Serializable => "SERIALIZABLE",
339331
}
340332
}
341333

@@ -1246,7 +1238,13 @@ impl Connection {
12461238
///
12471239
/// This will not change the behavior of an active transaction.
12481240
pub fn set_transaction_isolation(&self, level: IsolationLevel) -> Result<()> {
1249-
self.batch_execute(level.to_set_query())
1241+
self.set_transaction_config(transaction::Config::new().isolation_level(level))
1242+
}
1243+
1244+
pub fn set_transaction_config(&self, config: &transaction::Config) -> Result<()> {
1245+
let mut command = "SET SESSION CHARACTERISTICS AS TRANSACTION".to_owned();
1246+
config.build_command(&mut command);
1247+
self.batch_execute(&command)
12501248
}
12511249

12521250
/// Execute a sequence of SQL statements.
@@ -1491,3 +1489,7 @@ trait TransactionInternals<'conn> {
14911489

14921490
fn depth(&self) -> u32;
14931491
}
1492+
1493+
trait ConfigInternals {
1494+
fn build_command(&self, s: &mut String);
1495+
}

src/transaction.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,78 @@
33
use std::cell::Cell;
44
use std::fmt;
55

6-
use {Result, Connection, TransactionInternals};
6+
use {Result, Connection, TransactionInternals, ConfigInternals, IsolationLevel};
77
use stmt::Statement;
88
use rows::Rows;
99
use types::ToSql;
1010

11+
#[derive(Debug)]
12+
pub struct Config {
13+
isolation_level: Option<IsolationLevel>,
14+
read_only: Option<bool>,
15+
deferrable: Option<bool>,
16+
}
17+
18+
impl ConfigInternals for Config {
19+
fn build_command(&self, s: &mut String) {
20+
let mut first = true;
21+
22+
if let Some(isolation_level) = self.isolation_level {
23+
s.push_str(" ISOLATION LEVEL ");
24+
s.push_str(isolation_level.to_sql());
25+
first = false;
26+
}
27+
28+
if let Some(read_only) = self.read_only {
29+
if first {
30+
s.push(',');
31+
}
32+
if read_only {
33+
s.push_str(" READ ONLY");
34+
} else {
35+
s.push_str(" READ WRITE");
36+
}
37+
first = false;
38+
}
39+
40+
if let Some(deferrable) = self.deferrable {
41+
if first {
42+
s.push(',');
43+
}
44+
if deferrable {
45+
s.push_str(" DEFERRABLE");
46+
} else {
47+
s.push_str(" NOT DEFERRABLE");
48+
}
49+
}
50+
}
51+
}
52+
53+
impl Config {
54+
pub fn new() -> Config {
55+
Config {
56+
isolation_level: None,
57+
read_only: None,
58+
deferrable: None,
59+
}
60+
}
61+
62+
pub fn isolation_level(&mut self, isolation_level: IsolationLevel) -> &mut Config {
63+
self.isolation_level = Some(isolation_level);
64+
self
65+
}
66+
67+
pub fn read_only(&mut self, read_only: bool) -> &mut Config {
68+
self.read_only = Some(read_only);
69+
self
70+
}
71+
72+
pub fn deferrable(&mut self, deferrable: bool) -> &mut Config {
73+
self.deferrable = Some(deferrable);
74+
self
75+
}
76+
}
77+
1178
/// A transaction on a database connection.
1279
///
1380
/// The transaction will roll back by default.

0 commit comments

Comments
 (0)