sqlx_core_oldapi/mysql/
transaction.rs1use futures_core::future::BoxFuture;
2
3use crate::error::Error;
4use crate::executor::Executor;
5use crate::mysql::connection::Waiting;
6use crate::mysql::protocol::text::Query;
7use crate::mysql::{MySql, MySqlConnection};
8use crate::transaction::{
9 begin_ansi_transaction_sql, commit_ansi_transaction_sql, rollback_ansi_transaction_sql,
10 TransactionManager,
11};
12
13pub struct MySqlTransactionManager;
15
16impl TransactionManager for MySqlTransactionManager {
17 type Database = MySql;
18
19 fn begin(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> {
20 Box::pin(async move {
21 let depth = conn.transaction_depth;
22
23 conn.execute(&*begin_ansi_transaction_sql(depth)).await?;
24 conn.transaction_depth = depth + 1;
25
26 Ok(())
27 })
28 }
29
30 fn commit(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> {
31 Box::pin(async move {
32 let depth = conn.transaction_depth;
33
34 if depth > 0 {
35 conn.execute(&*commit_ansi_transaction_sql(depth)).await?;
36 conn.transaction_depth = depth - 1;
37 }
38
39 Ok(())
40 })
41 }
42
43 fn rollback(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> {
44 Box::pin(async move {
45 let depth = conn.transaction_depth;
46
47 if depth > 0 {
48 conn.execute(&*rollback_ansi_transaction_sql(depth)).await?;
49 conn.transaction_depth = depth - 1;
50 }
51
52 Ok(())
53 })
54 }
55
56 fn start_rollback(conn: &mut MySqlConnection) {
57 let depth = conn.transaction_depth;
58
59 if depth > 0 {
60 conn.stream.waiting.push_back(Waiting::Result);
61 conn.stream.sequence_id = 0;
62 conn.stream
63 .write_packet(Query(&*rollback_ansi_transaction_sql(depth)));
64
65 conn.transaction_depth = depth - 1;
66 }
67 }
68}