#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![warn(missing_docs)]
use diesel::{
ConnectionResult, QueryResult,
backend::Backend,
query_builder::{AsQuery, QueryFragment, QueryId},
row::Row,
};
use futures_util::Stream;
#[cfg(feature = "mysql")]
pub mod mysql;
#[cfg(feature = "postgres")]
pub mod pg;
#[cfg(any(feature = "deadpool", feature = "bb8"))]
pub mod pooled_connection;
mod run_query_dsl;
#[allow(dead_code)]
mod stmt_cache;
mod test_connection;
#[cfg(feature = "mysql")]
#[doc(inline)]
pub use self::mysql::AsyncMysqlConnection;
#[cfg(feature = "postgres")]
#[doc(inline)]
pub use self::pg::AsyncPgConnection;
#[doc(inline)]
pub use self::run_query_dsl::*;
pub use self::test_connection::TestConnection;
pub trait AsyncExecute: Sized + Send {
fn batch_execute(
&mut self,
query: &str,
) -> impl std::future::Future<Output = QueryResult<()>> + Send;
type Stream<'conn>: Stream<Item = QueryResult<Self::Row<'conn>>> + Send
where
Self: 'conn;
type Row<'conn>: Row<'conn, Self::Backend>
where
Self: 'conn;
type Backend: Backend;
#[doc(hidden)]
fn load<T>(
&mut self,
source: T,
) -> impl std::future::Future<Output = QueryResult<Self::Stream<'_>>> + Send
where
T: AsQuery + Send,
T::Query: QueryFragment<Self::Backend> + QueryId + Send;
#[doc(hidden)]
fn execute_returning_count<T>(
&mut self,
source: T,
) -> impl std::future::Future<Output = QueryResult<usize>> + Send
where
T: QueryFragment<Self::Backend> + QueryId + Send;
}
pub trait AsyncTransaction {
fn commit(self) -> impl std::future::Future<Output = QueryResult<()>> + Send;
fn rollback(self) -> impl std::future::Future<Output = QueryResult<()>> + Send;
}
pub trait AsyncConnection: AsyncExecute {
fn establish(
database_url: &str,
) -> impl std::future::Future<Output = ConnectionResult<Self>> + Send;
fn is_broken(&self) -> bool;
}
pub trait AsyncTransactional: Send {
type Transaction<'a>: AsyncTransaction + Send
where
Self: 'a;
fn begin_transaction(
&mut self,
) -> impl std::future::Future<Output = QueryResult<Self::Transaction<'_>>> + Send;
fn transaction<'a, R, E, F>(
&'a mut self,
callback: F,
) -> impl Future<Output = Result<R, E>> + Send
where
for<'t> F: AsyncFnOnce(&'t mut Self::Transaction<'a>) -> Result<R, E>
+ AsyncFunc<&'t mut Self::Transaction<'a>, Result<R, E>>
+ Send
+ 'a,
E: From<diesel::result::Error> + Send,
R: Send,
{
async move {
let mut transaction = self.begin_transaction().await?;
let res = callback(&mut transaction).await?;
transaction.commit().await?;
Ok(res)
}
}
}
pub trait AsyncFunc<T, R>:
AsyncFnOnce(T) -> R + FnOnce(T) -> <Self as AsyncFunc<T, R>>::Fut + Send
{
type Fut: Future<Output = R> + Send;
}
impl<F, T, Fut, R> AsyncFunc<T, R> for F
where
F: AsyncFnOnce(T) -> R + FnOnce(T) -> Fut + Send,
Fut: Future<Output = R> + Send,
{
type Fut = Fut;
}