Debian Codegraph @

src/acquire.rs

raw symbols SCIP
1 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::databaserust-analyzer cargo sqlx-core 0.8.3 database/::Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#;
2 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::errorrust-analyzer cargo sqlx-core 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
3 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::poolrust-analyzer cargo sqlx-core 0.8.3 pool/::{MaybePoolConnectionrust-analyzer cargo sqlx-core 0.8.3 pool/maybe/MaybePoolConnection#, Poolrust-analyzer cargo sqlx-core 0.8.3 pool/Pool#, PoolConnectionrust-analyzer cargo sqlx-core 0.8.3 pool/connection/PoolConnection#};
4
5 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::transactionrust-analyzer cargo sqlx-core 0.8.3 transaction/::Transactionrust-analyzer cargo sqlx-core 0.8.3 transaction/Transaction#;
6 use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::futurerust-analyzer cargo futures-core 0.3.34 future/::BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#};
8
9 /// Acquire connections or transactions from a database in a generic way.
10 ///
11 /// If you want to accept generic database connections that implement
12 /// [`Acquire`] which then allows you to [`acquire`][`Acquire::acquire`] a
13 /// connection or [`begin`][`Acquire::begin`] a transaction, then you can do it
14 /// like that:
15 ///
16 /// ```rust
17 /// # use sqlx::{Acquire, postgres::Postgres, error::BoxDynError};
18 /// # #[cfg(any(postgres_9_6, postgres_15))]
19 /// async fn run_query<'a, A>(conn: A) -> Result<(), BoxDynError>
20 /// where
21 /// A: Acquire<'a, Database = Postgres>,
22 /// {
23 /// let mut conn = conn.acquire().await?;
24 ///
25 /// sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
26 /// sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;
27 ///
28 /// Ok(())
29 /// }
30 /// ```
31 ///
32 /// If you run into a lifetime error about "implementation of `sqlx::Acquire` is
33 /// not general enough", the [workaround] looks like this:
34 ///
35 /// ```rust
36 /// # use std::future::Future;
37 /// # use sqlx::{Acquire, postgres::Postgres, error::BoxDynError};
38 /// # #[cfg(any(postgres_9_6, postgres_15))]
39 /// fn run_query<'a, 'c, A>(conn: A) -> impl Future<Output = Result<(), BoxDynError>> + Send + 'a
40 /// where
41 /// A: Acquire<'c, Database = Postgres> + Send + 'a,
42 /// {
43 /// async move {
44 /// let mut conn = conn.acquire().await?;
45 ///
46 /// sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
47 /// sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;
48 ///
49 /// Ok(())
50 /// }
51 /// }
52 /// ```
53 ///
54 /// However, if you really just want to accept both, a transaction or a
55 /// connection as an argument to a function, then it's easier to just accept a
56 /// mutable reference to a database connection like so:
57 ///
58 /// ```rust
59 /// # use sqlx::{postgres::PgConnection, error::BoxDynError};
60 /// # #[cfg(any(postgres_9_6, postgres_15))]
61 /// async fn run_query(conn: &mut PgConnection) -> Result<(), BoxDynError> {
62 /// sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
63 /// sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;
64 ///
65 /// Ok(())
66 /// }
67 /// ```
68 ///
69 /// The downside of this approach is that you have to `acquire` a connection
70 /// from a pool first and can't directly pass the pool as argument.
71 ///
72 /// [workaround]: https://github.com/launchbadge/sqlx/issues/1015#issuecomment-767787777
73 pub trait Acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#<'c> {
74 type Databaserust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Database#: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#;
75
76 type Connectionrust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Connection#: Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#<Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# = <Self::Databaserust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Database# as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::Connectionrust-analyzer cargo sqlx-core 0.8.3 database/Database#Connection#> + DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#;
77
78 fn acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#acquire().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'c, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self::Connectionrust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Connection#, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>>;
79
80 fn beginrust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#begin().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'c, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Transactionrust-analyzer cargo sqlx-core 0.8.3 transaction/Transaction#<'c, Self::Databaserust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Database#>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>>;
81 }
82
83 impl<'a, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#> Acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#<'a> for &'_ Poolrust-analyzer cargo sqlx-core 0.8.3 pool/Pool#<DB> {
84 type Databaserust-analyzer cargo sqlx-core 0.8.3 acquire/impl#[`&Pool<DB>`][`Acquire<'a>`]Database# = DB;
85
86 type Connectionrust-analyzer cargo sqlx-core 0.8.3 acquire/impl#[`&Pool<DB>`][`Acquire<'a>`]Connection# = PoolConnectionrust-analyzer cargo sqlx-core 0.8.3 pool/connection/PoolConnection#<DB>;
87
88 fn acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/impl#[`&Pool<DB>`][`Acquire<'a>`]acquire().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'static, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-core 0.8.3 acquire/impl#[`&Pool<DB>`][`Acquire<'a>`]::Connectionrust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Connection#, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> {
89 Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(self.acquirerust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]acquire().())
90 }
91
92 fn beginrust-analyzer cargo sqlx-core 0.8.3 acquire/impl#[`&Pool<DB>`][`Acquire<'a>`]begin().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'static, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Transactionrust-analyzer cargo sqlx-core 0.8.3 transaction/Transaction#<'a, DB>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> {
93 let conn = self.acquirerust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]acquire().();
94
95 Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move {
96 Transactionrust-analyzer cargo sqlx-core 0.8.3 transaction/Transaction#::beginrust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`]begin().(MaybePoolConnectionrust-analyzer cargo sqlx-core 0.8.3 pool/maybe/MaybePoolConnection#::PoolConnectionrust-analyzer cargo sqlx-core 0.8.3 pool/maybe/MaybePoolConnection#PoolConnection#(conn.rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().
97 })
98 }
99 }
100
101 #[macro_export]
102 macro_rules! impl_acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/impl_acquire! {
103 ($DB:ident, $C:ident) => {
104 impl<'c> $crate::acquire::Acquire<'c> for &'c mut $C {
105 type Database = $DB;
106
107 type Connection = &'c mut <$DB as $crate::database::Database>::Connection;
108
109 #[inline]
110 fn acquire(
111 self,
112 ) -> futures_core::future::BoxFuture<'c, Result<Self::Connection, $crate::error::Error>>
113 {
114 Box::pin(futures_util::future::ok(self))
115 }
116
117 #[inline]
118 fn begin(
119 self,
120 ) -> futures_core::future::BoxFuture<
121 'c,
122 Result<$crate::transaction::Transaction<'c, $DB>, $crate::error::Error>,
123 > {
124 $crate::transaction::Transaction::begin(self)
125 }
126 }
127 };
128 }