Skip to content

Commit b59326e

Browse files
committed
docs: Acquire examples and alternative
1 parent 2182925 commit b59326e

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

.github/workflows/sqlx.yml

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
runs-on: ubuntu-20.04
7272
strategy:
7373
matrix:
74+
postgres: [14, 9_6]
7475
runtime: [async-std, tokio, actix]
7576
tls: [native-tls, rustls]
7677
steps:
@@ -90,12 +91,18 @@ jobs:
9091
target
9192
key: ${{ runner.os }}-test-${{ hashFiles('**/Cargo.lock') }}
9293

94+
- run: |
95+
docker-compose -f tests/docker-compose.yml run -d -p 5432:5432 --name postgres_${{ matrix.postgres }} postgres_${{ matrix.postgres }}
96+
docker exec postgres_${{ matrix.postgres }} bash -c "until pg_isready; do sleep 1; done"
97+
9398
- uses: actions-rs/cargo@v1
9499
with:
95100
command: test
96101
args: >
97102
--manifest-path sqlx-core/Cargo.toml
98103
--features offline,all-databases,all-types,runtime-${{ matrix.runtime }}-${{ matrix.tls }}
104+
env:
105+
DATABASE_URL: postgres://postgres:password@localhost:5432/sqlx
99106

100107
cli:
101108
name: CLI Binaries

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-core/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,6 @@ bstr = { version = "0.2.17", default-features = false, features = ["std"], optio
164164
git2 = { version = "0.13.25", default-features = false, optional = true }
165165
hashlink = "0.7.0"
166166
indexmap = "1.7.0"
167+
168+
[dev-dependencies]
169+
sqlx = { version = "0.5.10", path = "..", features = ["postgres"] }

sqlx-core/src/acquire.rs

+61
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,67 @@ use crate::transaction::Transaction;
55
use futures_core::future::BoxFuture;
66
use std::ops::{Deref, DerefMut};
77

8+
/// Acquire connections or transactions from a database in a generic way.
9+
///
10+
/// If you want to accept generic database connections that implement
11+
/// [`Acquire`] which then allows you to [`acquire`][`Acquire::acquire`] a
12+
/// connection or [`begin`][`Acquire::begin`] a transaction, then you can do it
13+
/// like that:
14+
///
15+
/// ```rust
16+
/// # use sqlx::{Acquire, postgres::Postgres, error::BoxDynError};
17+
/// async fn run_query<'a, A>(conn: A) -> Result<(), BoxDynError>
18+
/// where
19+
/// A: Acquire<'a, Database = Postgres>,
20+
/// {
21+
/// let mut conn = conn.acquire().await?;
22+
///
23+
/// sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
24+
/// sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;
25+
///
26+
/// Ok(())
27+
/// }
28+
/// ```
29+
///
30+
/// If you run into a lifetime error about "implementation of `sqlx::Acquire` is
31+
/// not general enough", the [workaround] looks like this:
32+
///
33+
/// ```rust
34+
/// # use std::future::Future;
35+
/// # use sqlx::{Acquire, postgres::Postgres, error::BoxDynError};
36+
/// fn run_query<'a, 'c, A>(conn: A) -> impl Future<Output = Result<(), BoxDynError>> + Send + 'a
37+
/// where
38+
/// A: Acquire<'c, Database = Postgres> + Send + 'a,
39+
/// {
40+
/// async move {
41+
/// let mut conn = conn.acquire().await?;
42+
///
43+
/// sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
44+
/// sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;
45+
///
46+
/// Ok(())
47+
/// }
48+
/// }
49+
/// ```
50+
///
51+
/// However, if you really just want to accept both, a transaction or a
52+
/// connection as an argument to a function, then it's easier to just accept a
53+
/// mutable reference to a database connection like so:
54+
///
55+
/// ```rust
56+
/// # use sqlx::{postgres::PgConnection, error::BoxDynError};
57+
/// async fn run_query(conn: &mut PgConnection) -> Result<(), BoxDynError> {
58+
/// sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
59+
/// sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;
60+
///
61+
/// Ok(())
62+
/// }
63+
/// ```
64+
///
65+
/// The downside of this approach is that you have to `acquire` a connection
66+
/// from a pool first and can't directly pass the pool as argument.
67+
///
68+
/// [workaround]: https://github.com/launchbadge/sqlx/issues/1015#issuecomment-767787777
869
pub trait Acquire<'c> {
970
type Database: Database;
1071

0 commit comments

Comments
 (0)