Skip to content

Commit 2a3f5de

Browse files
committed
Introduce diesel_sqlite feature (for rocket_db_pools support)
1 parent 504efef commit 2a3f5de

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

contrib/db_pools/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ sqlx_macros = ["sqlx/macros"]
2828
# diesel features
2929
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "deadpool", "diesel"]
3030
diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "deadpool", "diesel"]
31+
diesel_sqlite = ["diesel-async/sqlite", "diesel-async/deadpool", "deadpool", "diesel"]
3132
# implicit features: mongodb
3233

3334
[dependencies.rocket]

contrib/db_pools/lib/src/diesel.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ pub use diesel_async::AsyncMysqlConnection;
9595
#[cfg(feature = "diesel_postgres")]
9696
pub use diesel_async::AsyncPgConnection;
9797

98+
#[doc(inline)]
99+
#[cfg(feature = "diesel_sqlite")]
100+
pub use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
101+
98102
/// Alias of a `Result` with an error type of [`Debug`] for a `diesel::Error`.
99103
///
100104
/// `QueryResult` is a [`Responder`](rocket::response::Responder) when `T` (the
@@ -150,3 +154,25 @@ pub type MysqlPool = Pool<AsyncMysqlConnection>;
150154
/// ```
151155
#[cfg(feature = "diesel_postgres")]
152156
pub type PgPool = Pool<AsyncPgConnection>;
157+
158+
/// Type alias for an `async` pool of Sqlite connections for `async` [diesel].
159+
///
160+
/// ```rust
161+
/// # extern crate rocket;
162+
/// # #[cfg(feature = "diesel_sqlite")] {
163+
/// # use rocket::get;
164+
/// use rocket_db_pools::{Database, Connection};
165+
/// use rocket_db_pools::diesel::{SqlitePool, prelude::*};
166+
///
167+
/// #[derive(Database)]
168+
/// #[database("my_sqlite_db_name")]
169+
/// struct Db(SqlitePool);
170+
///
171+
/// #[get("/")]
172+
/// async fn use_db(mut db: Connection<Db>) {
173+
/// /* .. */
174+
/// }
175+
/// # }
176+
/// ```
177+
#[cfg(feature = "diesel_sqlite")]
178+
pub type PgPool = Pool<SyncConnectionWrapper<SqliteConnection>>;

contrib/db_pools/lib/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@
141141
//!
142142
//! ## `diesel` (v2)
143143
//!
144-
//! | Database | Feature | [`Pool`] Type | [`Connection`] Deref |
145-
//! |----------|-------------------|-----------------------|----------------------------------|
146-
//! | Postgres | `diesel_postgres` | [`diesel::PgPool`] | [`diesel::AsyncPgConnection`] |
147-
//! | MySQL | `diesel_mysql` | [`diesel::MysqlPool`] | [`diesel::AsyncMysqlConnection`] | //!
144+
//! | Database | Feature | [`Pool`] Type | [`Connection`] Deref |
145+
//! |----------|-------------------|------------------------|--------------------------------------------------------------|
146+
//! | Postgres | `diesel_postgres` | [`diesel::PgPool`] | [`diesel::AsyncPgConnection`] |
147+
//! | MySQL | `diesel_mysql` | [`diesel::MysqlPool`] | [`diesel::AsyncMysqlConnection`] |
148+
//! | SQLite | `diesel_sqlite` | [`diesel::SqlitePool`] | [`diesel::SyncConnectionWrapper<diesel::SqliteConnection>>`] |
148149
//!
149150
//! See [`diesel`] for usage details.
150151
//!
@@ -243,7 +244,11 @@ pub use rocket;
243244
#[doc(inline)]
244245
pub use rocket::figment;
245246

246-
#[cfg(any(feature = "diesel_postgres", feature = "diesel_mysql"))] pub mod diesel;
247+
#[cfg(any(
248+
feature = "diesel_postgres",
249+
feature = "diesel_mysql",
250+
feature = "diesel_sqlite"
251+
))] pub mod diesel;
247252
#[cfg(feature = "deadpool_postgres")] pub use deadpool_postgres;
248253
#[cfg(feature = "deadpool_redis")] pub use deadpool_redis;
249254
#[cfg(feature = "mongodb")] pub use mongodb;

contrib/db_pools/lib/src/pool.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ mod deadpool_postgres {
192192
}
193193
}
194194

195+
#[cfg(feature = "diesel_sqlite")]
196+
impl DeadManager for AsyncDieselConnectionManager<
197+
diesel_async::sync_connection_wrapper::SyncConnectionWrapper::<diesel::SqliteConnection>
198+
> {
199+
fn new(config: &Config) -> Result<Self, Self::Error> {
200+
Ok(Self::new(config.url.as_str()))
201+
}
202+
}
203+
195204
#[rocket::async_trait]
196205
impl<M: DeadManager, C: From<Object<M>>> crate::Pool for Pool<M, C>
197206
where M::Type: Send, C: Send + Sync + 'static, M::Error: std::error::Error

0 commit comments

Comments
 (0)