diesel_async::sync_connection_wrapper

Struct SyncConnectionWrapper

Source
pub struct SyncConnectionWrapper<C> { /* private fields */ }
Available on crate feature 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:

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>

Source

pub async fn immediate_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a, E: From<Error> + Send + 'a, R: Send + 'a,

Available on crate feature 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
Source

pub async fn exclusive_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a, E: From<Error> + Send + 'a, R: Send + 'a,

Available on crate feature 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>

Source

pub fn new(connection: C) -> Self
where C: Connection,

Builds a wrapper with this underlying sync connection

Source

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>,

Source§

type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<SyncConnectionWrapper<C> as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query>>

The future returned by AsyncConnection::load
Source§

type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query>>

The future returned by AsyncConnection::execute
Source§

type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<<SyncConnectionWrapper<C> as AsyncConnection>::Row<'conn, 'query>, Error>> + Send>>

The inner stream returned by AsyncConnection::load
Source§

type Row<'conn, 'query> = O

The row type used by the stream returned by AsyncConnection::load
Source§

type Backend = <C as Connection>::Backend

The backend this type connects to
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,

Establishes a new connection to the database Read more
Source§

fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)

Set a specific Instrumentation implementation for this connection
Source§

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>>
where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait, E: From<Error> + Send + 'a + 'async_trait, R: Send + 'a + 'async_trait, Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,

Executes the given function inside of a database transaction Read more
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,

Creates a transaction that will never be committed. This is useful for tests. Panics if called while inside of a transaction or if called with a connection containing a broken transaction
Source§

fn test_transaction<'a, 'async_trait, R, E, F>( &'a mut self, f: F, ) -> Pin<Box<dyn Future<Output = R> + Send + 'async_trait>>
where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait, E: Debug + Send + 'a + 'async_trait, R: Send + 'a + 'async_trait, Self: 'a + 'async_trait, 'a: 'async_trait,

Executes the given function inside a transaction, but does not commit it. Panics if the given function returns an error. Read more
Source§

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,

Execute multiple SQL statements within the same string. Read more
Source§

impl<T, C> TransactionManager<SyncConnectionWrapper<C>> for SyncTransactionManagerWrapper<T>

Source§

type TransactionStateData = <T as TransactionManager<C>>::TransactionStateData

Data stored as part of the connection implementation to track the current transaction state of a connection
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,

Begin a new transaction or savepoint Read more
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,

Commit the inner-most transaction or savepoint Read more
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,

Rollback the inner-most transaction or savepoint Read more
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,

Executes the given function inside of a database transaction Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoSql for T

Source§

fn into_sql<T>(self) -> Self::Expression

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

Convert &self to an expression for Diesel’s query builder. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, Conn> RunQueryDsl<Conn> for T

Source§

fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> Conn::ExecuteFuture<'conn, 'query>
where Conn: AsyncConnection + Send, Self: ExecuteDsl<Conn> + 'query,

Executes the given command, returning the number of rows affected. Read more
Source§

fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
where U: Send, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a Vec with the returned rows. Read more
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,

Executes the given query, returning a Stream with the returned rows. Read more
Source§

fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Self, Conn, U>
where U: Send + 'conn, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, and returns the affected row. Read more
Source§

fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
where U: Send, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, returning an Vec with the affected rows. Read more
Source§

fn first<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Limit<Self>, Conn, U>
where U: Send + 'conn, Conn: AsyncConnection, Self: LimitDsl, Limit<Self>: LoadQuery<'query, Conn, U> + Send + 'query,

Attempts to load a single record. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more