/
germanubis
/
sqlx
Обзор
Документация
Войти
/
germanubis
/
sqlx
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
sqlx-mysql/src/transaction.rs
76 строк
2 KB
Joey de Waal
breaking: add SqlStr (#3723)
07 июл 2025, 10:35
Не верифицирован
07 июл 2025, 10:35
469f227
Код
Авторство
О чём код?
use sqlx_core::sql_str::SqlStr; use crate::connection::Waiting; use crate::error::Error; use crate::executor::Executor; use crate::protocol::text::Query; use crate::{MySql, MySqlConnection}; pub(crate) use sqlx_core::transaction::*; /// Implementation of [`TransactionManager`] for MySQL. pub struct MySqlTransactionManager; impl TransactionManager for MySqlTransactionManager { type Database = MySql; async fn begin(conn: &mut MySqlConnection, statement: Option<SqlStr>) -> Result<(), Error> { let depth = conn.inner.transaction_depth; let statement = match statement { // custom `BEGIN` statements are not allowed if we're already in a transaction // (we need to issue a `SAVEPOINT` instead) Some(_) if depth > 0 => return Err(Error::InvalidSavePointStatement), Some(statement) => statement, None => begin_ansi_transaction_sql(depth), }; conn.execute(statement).await?; if !conn.in_transaction() { return Err(Error::BeginFailed); } conn.inner.transaction_depth += 1; Ok(()) } async fn commit(conn: &mut MySqlConnection) -> Result<(), Error> { let depth = conn.inner.transaction_depth; if depth > 0 { conn.execute(commit_ansi_transaction_sql(depth)).await?; conn.inner.transaction_depth = depth - 1; } Ok(()) } async fn rollback(conn: &mut MySqlConnection) -> Result<(), Error> { let depth = conn.inner.transaction_depth; if depth > 0 { conn.execute(rollback_ansi_transaction_sql(depth)).await?; conn.inner.transaction_depth = depth - 1; } Ok(()) } fn start_rollback(conn: &mut MySqlConnection) { let depth = conn.inner.transaction_depth; if depth > 0 { conn.inner.stream.waiting.push_back(Waiting::Result); conn.inner.stream.sequence_id = 0; conn.inner .stream .write_packet(Query(rollback_ansi_transaction_sql(depth).as_str())) .expect("BUG: unexpected error queueing ROLLBACK"); conn.inner.transaction_depth = depth - 1; } } fn get_transaction_depth(conn: &MySqlConnection) -> usize { conn.inner.transaction_depth } }