cdbc_mysql/
transaction.rs

1
2use cdbc::error::Error;
3use cdbc::executor::Executor;
4use crate::connection::Waiting;
5use crate::protocol::text::Query;
6use crate::{MySql, MySqlConnection};
7use cdbc::transaction::{
8    begin_ansi_transaction_sql, commit_ansi_transaction_sql, rollback_ansi_transaction_sql,
9    TransactionManager,
10};
11
12/// Implementation of [`TransactionManager`] for MySQL.
13pub struct MySqlTransactionManager;
14
15impl TransactionManager for MySqlTransactionManager {
16    type Database = MySql;
17
18    fn begin(conn: &mut MySqlConnection) ->  Result<(), Error> {
19            let depth = conn.transaction_depth;
20            conn.execute(&*begin_ansi_transaction_sql(depth))?;
21            conn.transaction_depth = depth + 1;
22
23            Ok(())
24    }
25
26    fn commit(conn: &mut MySqlConnection) ->  Result<(), Error> {
27            let depth = conn.transaction_depth;
28            if depth > 0 {
29                conn.execute(&*commit_ansi_transaction_sql(depth))?;
30                conn.transaction_depth = depth - 1;
31            }
32            Ok(())
33    }
34
35    fn rollback(conn: &mut MySqlConnection) ->  Result<(), Error> {
36            let depth = conn.transaction_depth;
37            if depth > 0 {
38                conn.execute(&*rollback_ansi_transaction_sql(depth))?;
39                conn.transaction_depth = depth - 1;
40            }
41            Ok(())
42    }
43
44    fn start_rollback(conn: &mut MySqlConnection) {
45        let depth = conn.transaction_depth;
46
47        if depth > 0 {
48            conn.stream.waiting.push_back(Waiting::Result);
49            conn.stream.sequence_id = 0;
50            conn.stream
51                .write_packet(Query(&*rollback_ansi_transaction_sql(depth)));
52
53            conn.transaction_depth = depth - 1;
54        }
55    }
56}