diff --git a/src/async_connection_wrapper.rs b/src/async_connection_wrapper.rs index 0d25550..c0e2b70 100644 --- a/src/async_connection_wrapper.rs +++ b/src/async_connection_wrapper.rs @@ -200,6 +200,17 @@ mod implementation { } } + #[cfg(feature = "postgres")] + impl crate::pg::BuildTransaction for AsyncConnectionWrapper + where + C: crate::AsyncConnection + crate::pg::BuildTransaction, + B: BlockOn + Send, + { + fn build_transaction(&mut self) -> crate::pg::TransactionBuilder { + self.inner.build_transaction() + } + } + pub struct AsyncCursorWrapper<'a, S, B> { stream: Pin>, runtime: &'a B, diff --git a/src/pg/mod.rs b/src/pg/mod.rs index 654874d..a481d19 100644 --- a/src/pg/mod.rs +++ b/src/pg/mod.rs @@ -238,36 +238,6 @@ fn type_from_oid(t: &PgTypeMetadata) -> QueryResult { } impl AsyncPgConnection { - /// Build a transaction, specifying additional details such as isolation level - /// - /// See [`TransactionBuilder`] for more examples. - /// - /// [`TransactionBuilder`]: crate::pg::TransactionBuilder - /// - /// ```rust - /// # include!("../doctest_setup.rs"); - /// # use scoped_futures::ScopedFutureExt; - /// # - /// # #[tokio::main(flavor = "current_thread")] - /// # async fn main() { - /// # run_test().await.unwrap(); - /// # } - /// # - /// # async fn run_test() -> QueryResult<()> { - /// # use schema::users::dsl::*; - /// # let conn = &mut connection_no_transaction().await; - /// conn.build_transaction() - /// .read_only() - /// .serializable() - /// .deferrable() - /// .run(|conn| async move { Ok(()) }.scope_boxed()) - /// .await - /// # } - /// ``` - pub fn build_transaction(&mut self) -> TransactionBuilder { - TransactionBuilder::new(self) - } - /// Construct a new `AsyncPgConnection` instance from an existing [`tokio_postgres::Client`] pub async fn try_from(conn: tokio_postgres::Client) -> ConnectionResult { let mut conn = Self { @@ -416,6 +386,47 @@ impl AsyncPgConnection { } } +/// If a type implements this trait it supports building transactions with a +/// TransactionBuilder. +pub trait BuildTransaction { + /// Build a transaction + fn build_transaction(&mut self) -> TransactionBuilder + where + C: Sized; +} + +impl BuildTransaction for AsyncPgConnection { + /// Build a transaction, specifying additional details such as isolation level + /// + /// See [`TransactionBuilder`] for more examples. + /// + /// [`TransactionBuilder`]: crate::pg::TransactionBuilder + /// + /// ```rust + /// # include!("../doctest_setup.rs"); + /// # use scoped_futures::ScopedFutureExt; + /// # + /// # #[tokio::main(flavor = "current_thread")] + /// # async fn main() { + /// # run_test().await.unwrap(); + /// # } + /// # + /// # async fn run_test() -> QueryResult<()> { + /// # use schema::users::dsl::*; + /// # let conn = &mut connection_no_transaction().await; + /// conn.build_transaction() + /// .read_only() + /// .serializable() + /// .deferrable() + /// .run(|conn| async move { Ok(()) }.scope_boxed()) + /// .await + /// # } + /// ``` + fn build_transaction(&mut self) -> TransactionBuilder { + TransactionBuilder::new(self) + } +} + struct PgAsyncMetadataLookup { unresolved_types: Vec<(Option, String)>, } diff --git a/src/pg/transaction_builder.rs b/src/pg/transaction_builder.rs index 1096433..f9109e1 100644 --- a/src/pg/transaction_builder.rs +++ b/src/pg/transaction_builder.rs @@ -380,6 +380,7 @@ impl QueryFragment for Deferrable { #[cfg(test)] mod tests { use super::*; + use crate::pg::BuildTransaction; #[tokio::test] async fn test_transaction_builder_generates_correct_sql() {