Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose build_transaction through AsyncConnectionWrapper #116

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/async_connection_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ mod implementation {
}
}

#[cfg(feature = "postgres")]
impl<C, B> crate::pg::BuildTransaction<C> for AsyncConnectionWrapper<C, B>
where
C: crate::AsyncConnection + crate::pg::BuildTransaction<C>,
B: BlockOn + Send,
{
fn build_transaction(&mut self) -> crate::pg::TransactionBuilder<C> {
self.inner.build_transaction()
}
}

pub struct AsyncCursorWrapper<'a, S, B> {
stream: Pin<Box<S>>,
runtime: &'a B,
Expand Down
71 changes: 41 additions & 30 deletions src/pg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,36 +238,6 @@ fn type_from_oid(t: &PgTypeMetadata) -> QueryResult<Type> {
}

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<Self> {
TransactionBuilder::new(self)
}

/// Construct a new `AsyncPgConnection` instance from an existing [`tokio_postgres::Client`]
pub async fn try_from(conn: tokio_postgres::Client) -> ConnectionResult<Self> {
let mut conn = Self {
Expand Down Expand Up @@ -416,6 +386,47 @@ impl AsyncPgConnection {
}
}

/// If a type implements this trait it supports building transactions with a
/// TransactionBuilder.
pub trait BuildTransaction<C> {
/// Build a transaction
fn build_transaction(&mut self) -> TransactionBuilder<C>
where
C: Sized;
}

impl BuildTransaction<AsyncPgConnection> 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<Self> {
TransactionBuilder::new(self)
}
}

struct PgAsyncMetadataLookup {
unresolved_types: Vec<(Option<String>, String)>,
}
Expand Down
1 change: 1 addition & 0 deletions src/pg/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ impl QueryFragment<Pg> for Deferrable {
#[cfg(test)]
mod tests {
use super::*;
use crate::pg::BuildTransaction;

#[tokio::test]
async fn test_transaction_builder_generates_correct_sql() {
Expand Down