pub struct SyncConnectionWrapper<C> { /* private fields */ }
sync-connection-wrapper
only.Expand description
A wrapper of a diesel::connection::Connection
usable in async context.
It implements AsyncConnection if diesel::connection::Connection
fullfils requirements:
- it’s a
diesel::connection::LoadConnection
- its
diesel::connection::Connection::Backend
has adiesel::query_builder::BindCollector
implementingdiesel::query_builder::MoveableBindCollector
- its
diesel::connection::LoadConnection::Row
implementsdiesel::row::IntoOwnedRow
Internally this wrapper type will use spawn_blocking
on tokio
to execute the request on the inner connection. This implies a
dependency on tokio and that the runtime is running.
Note that only SQLite is supported at the moment.
§Examples
use diesel_async::RunQueryDsl;
use schema::users;
async fn some_async_fn() {
use diesel_async::AsyncConnection;
use diesel::sqlite::SqliteConnection;
let mut conn =
SyncConnectionWrapper::<SqliteConnection>::establish(&database_url).await.unwrap();
let all_users = users::table.load::<(i32, String)>(&mut conn).await.unwrap();
}
Implementations§
Source§impl SyncConnectionWrapper<SqliteConnection>
impl SyncConnectionWrapper<SqliteConnection>
Sourcepub async fn immediate_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
Available on crate feature sqlite
only.
pub async fn immediate_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
sqlite
only.Run a transaction with BEGIN IMMEDIATE
This method will return an error if a transaction is already open.
WARNING: Canceling the returned future does currently not close an already open transaction. You may end up with a connection containing a dangling transaction.
§Example
use diesel::result::Error;
use scoped_futures::ScopedFutureExt;
use diesel_async::{RunQueryDsl, AsyncConnection};
conn.immediate_transaction(|conn| async move {
diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn)
.await?;
let all_names = users.select(name).load::<String>(conn).await?;
assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);
Ok(())
}.scope_boxed()).await
Sourcepub async fn exclusive_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
Available on crate feature sqlite
only.
pub async fn exclusive_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
sqlite
only.Run a transaction with BEGIN EXCLUSIVE
This method will return an error if a transaction is already open.
WARNING: Canceling the returned future does currently not close an already open transaction. You may end up with a connection containing a dangling transaction.
§Example
use diesel::result::Error;
use scoped_futures::ScopedFutureExt;
use diesel_async::{RunQueryDsl, AsyncConnection};
conn.exclusive_transaction(|conn| async move {
diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn)
.await?;
let all_names = users.select(name).load::<String>(conn).await?;
assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);
Ok(())
}.scope_boxed()).await
Source§impl<C> SyncConnectionWrapper<C>
impl<C> SyncConnectionWrapper<C>
Sourcepub fn new(connection: C) -> Selfwhere
C: Connection,
pub fn new(connection: C) -> Selfwhere
C: Connection,
Builds a wrapper with this underlying sync connection
Sourcepub fn spawn_blocking<'a, R>(
&mut self,
task: impl FnOnce(&mut C) -> QueryResult<R> + Send + 'static,
) -> BoxFuture<'a, QueryResult<R>>where
C: Connection + 'static,
R: Send + 'static,
pub fn spawn_blocking<'a, R>(
&mut self,
task: impl FnOnce(&mut C) -> QueryResult<R> + Send + 'static,
) -> BoxFuture<'a, QueryResult<R>>where
C: Connection + 'static,
R: Send + 'static,
Run a operation directly with the inner connection
This function is usful to register custom functions and collection for Sqlite for example
§Example
conn.spawn_blocking(|conn| {
// sqlite.rs sqlite NOCASE only works for ASCII characters,
// this collation allows handling UTF-8 (barring locale differences)
conn.register_collation("RUSTNOCASE", |rhs, lhs| {
rhs.to_lowercase().cmp(&lhs.to_lowercase())
})
}).await
Trait Implementations§
Source§impl<C, MD, O> AsyncConnection for SyncConnectionWrapper<C>where
<C as Connection>::Backend: Default + DieselReserveSpecialization,
<C::Backend as Backend>::QueryBuilder: Default,
C: Connection + LoadConnection + WithMetadataLookup + 'static,
<C as Connection>::TransactionManager: Send,
MD: Send + 'static,
for<'a> <C::Backend as Backend>::BindCollector<'a>: MoveableBindCollector<C::Backend, BindData = MD> + Default,
O: 'static + Send + for<'conn> Row<'conn, C::Backend>,
for<'conn, 'query> <C as LoadConnection>::Row<'conn, 'query>: IntoOwnedRow<'conn, <C as Connection>::Backend, OwnedRow = O>,
impl<C, MD, O> AsyncConnection for SyncConnectionWrapper<C>where
<C as Connection>::Backend: Default + DieselReserveSpecialization,
<C::Backend as Backend>::QueryBuilder: Default,
C: Connection + LoadConnection + WithMetadataLookup + 'static,
<C as Connection>::TransactionManager: Send,
MD: Send + 'static,
for<'a> <C::Backend as Backend>::BindCollector<'a>: MoveableBindCollector<C::Backend, BindData = MD> + Default,
O: 'static + Send + for<'conn> Row<'conn, C::Backend>,
for<'conn, 'query> <C as LoadConnection>::Row<'conn, 'query>: IntoOwnedRow<'conn, <C as Connection>::Backend, OwnedRow = O>,
Source§type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<SyncConnectionWrapper<C> as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query>>
type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<SyncConnectionWrapper<C> as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query>>
AsyncConnection::load
Source§type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query>>
type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query>>
AsyncConnection::execute
Source§type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<<SyncConnectionWrapper<C> as AsyncConnection>::Row<'conn, 'query>, Error>> + Send>>
type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<<SyncConnectionWrapper<C> as AsyncConnection>::Row<'conn, 'query>, Error>> + Send>>
AsyncConnection::load
Source§type Backend = <C as Connection>::Backend
type Backend = <C as Connection>::Backend
Source§fn establish<'life0, 'async_trait>(
database_url: &'life0 str,
) -> Pin<Box<dyn Future<Output = ConnectionResult<Self>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn establish<'life0, 'async_trait>(
database_url: &'life0 str,
) -> Pin<Box<dyn Future<Output = ConnectionResult<Self>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)
fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)
Instrumentation
implementation for this connectionSource§fn transaction<'a, 'life0, 'async_trait, R, E, F>(
&'life0 mut self,
callback: F,
) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>
fn transaction<'a, 'life0, 'async_trait, R, E, F>( &'life0 mut self, callback: F, ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>
Source§fn begin_test_transaction<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn begin_test_transaction<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§impl<C> SimpleAsyncConnection for SyncConnectionWrapper<C>where
C: Connection + 'static,
impl<C> SimpleAsyncConnection for SyncConnectionWrapper<C>where
C: Connection + 'static,
Source§fn batch_execute<'life0, 'life1, 'async_trait>(
&'life0 mut self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn batch_execute<'life0, 'life1, 'async_trait>(
&'life0 mut self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§impl<T, C> TransactionManager<SyncConnectionWrapper<C>> for SyncTransactionManagerWrapper<T>where
SyncConnectionWrapper<C>: AsyncConnection,
C: Connection + 'static,
T: TransactionManager<C> + Send,
impl<T, C> TransactionManager<SyncConnectionWrapper<C>> for SyncTransactionManagerWrapper<T>where
SyncConnectionWrapper<C>: AsyncConnection,
C: Connection + 'static,
T: TransactionManager<C> + Send,
Source§type TransactionStateData = <T as TransactionManager<C>>::TransactionStateData
type TransactionStateData = <T as TransactionManager<C>>::TransactionStateData
Source§fn begin_transaction<'life0, 'async_trait>(
conn: &'life0 mut SyncConnectionWrapper<C>,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
'life0: 'async_trait,
fn begin_transaction<'life0, 'async_trait>(
conn: &'life0 mut SyncConnectionWrapper<C>,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
'life0: 'async_trait,
Source§fn commit_transaction<'life0, 'async_trait>(
conn: &'life0 mut SyncConnectionWrapper<C>,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
'life0: 'async_trait,
fn commit_transaction<'life0, 'async_trait>(
conn: &'life0 mut SyncConnectionWrapper<C>,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
'life0: 'async_trait,
Source§fn rollback_transaction<'life0, 'async_trait>(
conn: &'life0 mut SyncConnectionWrapper<C>,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
'life0: 'async_trait,
fn rollback_transaction<'life0, 'async_trait>(
conn: &'life0 mut SyncConnectionWrapper<C>,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
'life0: 'async_trait,
Source§fn transaction<'a, 'life0, 'async_trait, F, R, E>(
conn: &'life0 mut Conn,
callback: F,
) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>where
F: for<'r> FnOnce(&'r mut Conn) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait,
E: From<Error> + Send + 'async_trait,
R: Send + 'async_trait,
Self: 'async_trait,
'a: 'async_trait,
'life0: 'async_trait,
fn transaction<'a, 'life0, 'async_trait, F, R, E>(
conn: &'life0 mut Conn,
callback: F,
) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>where
F: for<'r> FnOnce(&'r mut Conn) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait,
E: From<Error> + Send + 'async_trait,
R: Send + 'async_trait,
Self: 'async_trait,
'a: 'async_trait,
'life0: 'async_trait,
Auto Trait Implementations§
impl<C> Freeze for SyncConnectionWrapper<C>
impl<C> RefUnwindSafe for SyncConnectionWrapper<C>
impl<C> Send for SyncConnectionWrapper<C>where
C: Send,
impl<C> Sync for SyncConnectionWrapper<C>where
C: Send,
impl<C> Unpin for SyncConnectionWrapper<C>
impl<C> UnwindSafe for SyncConnectionWrapper<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self
to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self
to an expression for Diesel’s query builder. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T, Conn> RunQueryDsl<Conn> for T
impl<T, Conn> RunQueryDsl<Conn> for T
Source§fn execute<'conn, 'query>(
self,
conn: &'conn mut Conn,
) -> Conn::ExecuteFuture<'conn, 'query>
fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> Conn::ExecuteFuture<'conn, 'query>
Source§fn load<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> LoadFuture<'conn, 'query, Self, Conn, U>
fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
Source§fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnection,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnection,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
Source§fn get_result<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> GetResult<'conn, 'query, Self, Conn, U>
fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Self, Conn, U>
Source§fn get_results<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> LoadFuture<'conn, 'query, Self, Conn, U>
fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
Vec
with the affected rows. Read more