Trait Executor

Source
pub trait Executor<'executor> {
    type EnsureTransactionFuture: Future<Output = Result<TransactionGuard<'executor>, Error>> + Send;

    // Required methods
    fn execute<'data, 'result, Q>(
        self,
        query: String,
        values: Vec<Value<'data>>,
    ) -> Q::Result<'result>
       where Q: QueryStrategy,
             'executor: 'result,
             'data: 'result;
    fn dialect(&self) -> DBImpl;
    fn into_dyn(self) -> DynamicExecutor<'executor>;
    fn ensure_transaction(
        self,
    ) -> Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>;
}
Expand description

Some kind of database connection which can execute queries

This trait is implemented by the database connection itself as well as transactions.

§Object Safety

This trait is not object safe. However, there only exist two implementors, which were combined into the DynamicExecutor enum.

Required Associated Types§

Source

type EnsureTransactionFuture: Future<Output = Result<TransactionGuard<'executor>, Error>> + Send

A future producing a TransactionGuard returned by ensure_transaction

Required Methods§

Source

fn execute<'data, 'result, Q>( self, query: String, values: Vec<Value<'data>>, ) -> Q::Result<'result>
where Q: QueryStrategy, 'executor: 'result, 'data: 'result,

Executes a raw SQL query

The query is executed as prepared statement. To bind parameter, use ? as placeholder in SQLite and MySQL and $1, $2, $n in Postgres.

The generic Q is used to “select” what the database is supposed to respond with. See QueryStrategy for a list of available options.

db.execute::<All>("SELECT * FROM foo;".to_string(), vec![]);
Source

fn dialect(&self) -> DBImpl

Get the executor’s sql dialect.

Source

fn into_dyn(self) -> DynamicExecutor<'executor>

Convenience method to convert into a “dyn Executor

Source

fn ensure_transaction( self, ) -> Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>

Ensure a piece of code is run inside a transaction using a TransactionGuard.

In generic code an Executor might and might not be a &mut Transaction. But sometimes you’d want to ensure your code is run inside a transaction (for example bulk inserts).

This method solves this by producing a type which is either an owned or borrowed Transaction depending on the Executor it is called on.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'executor> Executor<'executor> for &'executor Database

Source§

type EnsureTransactionFuture = Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>

Source§

impl<'executor> Executor<'executor> for &'executor mut Transaction

Source§

impl<'executor> Executor<'executor> for DynamicExecutor<'executor>

Source§

type EnsureTransactionFuture = Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>