Skip to content

Commit

Permalink
Implements TransactionOptions for Any
Browse files Browse the repository at this point in the history
  • Loading branch information
xfbs committed Jul 18, 2022
1 parent aceb108 commit d9987bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions sqlx-core/src/any/connection/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -158,7 +158,7 @@ impl Connection for AnyConnection {

fn begin_with(
&mut self,
options: (),
options: AnyTransactionOptions,
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
where
Self: Sized,
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
53 changes: 48 additions & 5 deletions sqlx-core/src/any/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,47 @@ 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) => {
<crate::postgres::Postgres as Database>::TransactionManager::begin_with(
conn,
Default::default(),
options.postgres,
)
}

#[cfg(feature = "mysql")]
AnyConnectionKind::MySql(conn) => {
<crate::mysql::MySql as Database>::TransactionManager::begin_with(
conn,
Default::default(),
options.mysql,
)
}

#[cfg(feature = "sqlite")]
AnyConnectionKind::Sqlite(conn) => {
<crate::sqlite::Sqlite as Database>::TransactionManager::begin_with(
conn,
Default::default(),
options.sqlite,
)
}

Expand Down Expand Up @@ -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 }
}
}

0 comments on commit d9987bd

Please sign in to comment.