diff --git a/sqlx-core/src/any/connection/mod.rs b/sqlx-core/src/any/connection/mod.rs index 6a0fb9f55d..39d4e00795 100644 --- a/sqlx-core/src/any/connection/mod.rs +++ b/sqlx-core/src/any/connection/mod.rs @@ -1,6 +1,6 @@ use futures_core::future::BoxFuture; -use crate::any::{Any, AnyConnectOptions, AnyKind}; +use crate::any::{Any, AnyConnectOptions, AnyKind, AnyTransactionOptions}; use crate::connection::Connection; use crate::error::Error; @@ -158,7 +158,7 @@ impl Connection for AnyConnection { fn begin_with( &mut self, - options: (), + options: AnyTransactionOptions, ) -> BoxFuture<'_, Result, Error>> where Self: Sized, diff --git a/sqlx-core/src/any/mod.rs b/sqlx-core/src/any/mod.rs index bb07016051..b9d8e67c54 100644 --- a/sqlx-core/src/any/mod.rs +++ b/sqlx-core/src/any/mod.rs @@ -43,7 +43,7 @@ pub use options::AnyConnectOptions; pub use query_result::AnyQueryResult; pub use row::AnyRow; pub use statement::AnyStatement; -pub use transaction::AnyTransactionManager; +pub use transaction::{AnyTransactionManager, AnyTransactionOptions}; pub use type_info::AnyTypeInfo; pub use value::{AnyValue, AnyValueRef}; diff --git a/sqlx-core/src/any/transaction.rs b/sqlx-core/src/any/transaction.rs index 7122d8d92c..ffb9c7c193 100644 --- a/sqlx-core/src/any/transaction.rs +++ b/sqlx-core/src/any/transaction.rs @@ -4,21 +4,31 @@ use crate::any::connection::AnyConnectionKind; use crate::any::{Any, AnyConnection}; use crate::database::Database; use crate::error::Error; +#[cfg(feature = "mysql")] +use crate::mysql::MySqlTransactionOptions; +#[cfg(feature = "postgres")] +use crate::postgres::PgTransactionOptions; +#[cfg(feature = "sqlite")] +use crate::sqlite::SqliteTransactionOptions; use crate::transaction::TransactionManager; +/// Transaction manager for generic database connection. pub struct AnyTransactionManager; impl TransactionManager for AnyTransactionManager { type Database = Any; - type Options = (); + type Options = AnyTransactionOptions; - fn begin_with(conn: &mut AnyConnection, _options: ()) -> BoxFuture<'_, Result<(), Error>> { + fn begin_with( + conn: &mut AnyConnection, + options: AnyTransactionOptions, + ) -> BoxFuture<'_, Result<(), Error>> { match &mut conn.0 { #[cfg(feature = "postgres")] AnyConnectionKind::Postgres(conn) => { ::TransactionManager::begin_with( conn, - Default::default(), + options.postgres, ) } @@ -26,7 +36,7 @@ impl TransactionManager for AnyTransactionManager { AnyConnectionKind::MySql(conn) => { ::TransactionManager::begin_with( conn, - Default::default(), + options.mysql, ) } @@ -34,7 +44,7 @@ impl TransactionManager for AnyTransactionManager { AnyConnectionKind::Sqlite(conn) => { ::TransactionManager::begin_with( conn, - Default::default(), + options.sqlite, ) } @@ -120,3 +130,36 @@ impl TransactionManager for AnyTransactionManager { } } } + +/// Transaction initiation options for generic database connection. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct AnyTransactionOptions { + /// Options that are used when the connection is to SQLite. + #[cfg(feature = "sqlite")] + pub(crate) sqlite: SqliteTransactionOptions, + + /// Options that are used when the connection is to Postgres. + #[cfg(feature = "postgres")] + pub(crate) postgres: PgTransactionOptions, + + /// Options that are used when the connection is to MySQL. + #[cfg(feature = "mysql")] + pub(crate) mysql: MySqlTransactionOptions, +} + +impl AnyTransactionOptions { + #[cfg(feature = "postgres")] + pub fn postgres(self, postgres: PgTransactionOptions) -> Self { + Self { postgres, ..self } + } + + #[cfg(feature = "sqlite")] + pub fn sqlite(self, sqlite: SqliteTransactionOptions) -> Self { + Self { sqlite, ..self } + } + + #[cfg(feature = "mysql")] + pub fn mysql(self, mysql: MySqlTransactionOptions) -> Self { + Self { mysql, ..self } + } +}