1use std::marker::PhantomData;
2
3pub use mobc;
4pub use sqlx;
5
6use mobc::{Manager, async_trait};
7use sqlx::{Connection as _, Database};
8
9mod migrator;
10pub use migrator::SqlxMigrationExt;
11
12pub struct SqlxConnectionManager<DB>
13where
14 DB: Database + Sync,
15{
16 url: String,
17 _phantom: PhantomData<DB>,
18}
19
20impl<DB> SqlxConnectionManager<DB>
21where
22 DB: Database + Sync,
23{
24 #[must_use]
25 pub fn new<S>(url: S) -> Self
26 where
27 S: ToString,
28 {
29 Self {
30 url: url.to_string(),
31 _phantom: PhantomData,
32 }
33 }
34}
35
36#[async_trait]
37impl<DB> Manager for SqlxConnectionManager<DB>
38where
39 DB: Database + Sync,
40{
41 type Connection = DB::Connection;
42 type Error = sqlx::Error;
43
44 async fn connect(&self) -> Result<Self::Connection, Self::Error> {
45 Self::Connection::connect(&self.url).await
46 }
47
48 async fn check(
49 &self,
50 mut conn: Self::Connection,
51 ) -> Result<Self::Connection, Self::Error> {
52 conn.ping().await.map(|()| conn)
53 }
54}