1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use mysql_common::packets::OkPacket;
use std::{borrow::Cow, fmt};
use crate::{
conn::{
query_result::{Binary, Text},
ConnMut,
},
prelude::*,
LocalInfileHandler, Params, QueryResult, Result, Statement,
};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct TxOpts {
with_consistent_snapshot: bool,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
}
impl TxOpts {
pub fn with_consistent_snapshot(&self) -> bool {
self.with_consistent_snapshot
}
pub fn access_mode(&self) -> Option<AccessMode> {
self.access_mode
}
pub fn isolation_level(&self) -> Option<IsolationLevel> {
self.isolation_level
}
pub fn set_with_consistent_snapshot(mut self, val: bool) -> Self {
self.with_consistent_snapshot = val;
self
}
pub fn set_access_mode(mut self, access_mode: Option<AccessMode>) -> Self {
self.access_mode = access_mode;
self
}
pub fn set_isolation_level(mut self, level: Option<IsolationLevel>) -> Self {
self.isolation_level = level;
self
}
}
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
#[repr(u8)]
pub enum AccessMode {
ReadOnly,
ReadWrite,
}
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
#[repr(u8)]
pub enum IsolationLevel {
ReadUncommitted,
ReadCommitted,
RepeatableRead,
Serializable,
}
impl fmt::Display for IsolationLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
IsolationLevel::ReadUncommitted => write!(f, "READ UNCOMMITTED"),
IsolationLevel::ReadCommitted => write!(f, "READ COMMITTED"),
IsolationLevel::RepeatableRead => write!(f, "REPEATABLE READ"),
IsolationLevel::Serializable => write!(f, "SERIALIZABLE"),
}
}
}
#[derive(Debug)]
pub struct Transaction<'a> {
pub(crate) conn: ConnMut<'a, 'static, 'static>,
committed: bool,
rolled_back: bool,
restore_local_infile_handler: Option<LocalInfileHandler>,
}
impl Transaction<'_> {
pub(crate) fn new<'a>(conn: ConnMut<'a, 'static, 'static>) -> Transaction<'a> {
let handler = conn.0.local_infile_handler.clone();
Transaction {
conn,
committed: false,
rolled_back: false,
restore_local_infile_handler: handler,
}
}
pub fn commit(mut self) -> Result<()> {
self.conn.query_drop("COMMIT")?;
self.committed = true;
Ok(())
}
pub fn rollback(mut self) -> Result<()> {
self.conn.query_drop("ROLLBACK")?;
self.rolled_back = true;
Ok(())
}
pub fn set_local_infile_handler(&mut self, handler: Option<LocalInfileHandler>) {
self.conn.set_local_infile_handler(handler);
}
pub fn affected_rows(&self) -> u64 {
self.conn.affected_rows()
}
pub fn last_insert_id(&self) -> Option<u64> {
self.conn
.0
.ok_packet
.as_ref()
.and_then(OkPacket::last_insert_id)
}
pub fn warnings(&self) -> u16 {
self.conn.warnings()
}
pub fn info_ref(&self) -> &[u8] {
self.conn.info_ref()
}
pub fn info_str(&self) -> Cow<str> {
self.conn.info_str()
}
}
impl<'a> Queryable for Transaction<'a> {
fn query_iter<T: AsRef<str>>(&mut self, query: T) -> Result<QueryResult<'_, '_, '_, Text>> {
self.conn.query_iter(query)
}
fn prep<T: AsRef<str>>(&mut self, query: T) -> Result<Statement> {
self.conn.prep(query)
}
fn close(&mut self, stmt: Statement) -> Result<()> {
self.conn.close(stmt)
}
fn exec_iter<S, P>(&mut self, stmt: S, params: P) -> Result<QueryResult<'_, '_, '_, Binary>>
where
S: AsStatement,
P: Into<Params>,
{
self.conn.exec_iter(stmt, params)
}
}
impl<'a> Drop for Transaction<'a> {
fn drop(&mut self) {
if !self.committed && !self.rolled_back {
let _ = self.conn.query_drop("ROLLBACK");
}
self.conn.0.local_infile_handler = self.restore_local_infile_handler.take();
}
}