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§
Sourcetype EnsureTransactionFuture: Future<Output = Result<TransactionGuard<'executor>, Error>> + Send
type EnsureTransactionFuture: Future<Output = Result<TransactionGuard<'executor>, Error>> + Send
A future producing a TransactionGuard
returned by ensure_transaction
Required Methods§
Sourcefn execute<'data, 'result, Q>(
self,
query: String,
values: Vec<Value<'data>>,
) -> Q::Result<'result>where
Q: QueryStrategy,
'executor: 'result,
'data: 'result,
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![]);
Sourcefn into_dyn(self) -> DynamicExecutor<'executor>
fn into_dyn(self) -> DynamicExecutor<'executor>
Convenience method to convert into a “dyn Executor
”
Sourcefn ensure_transaction(
self,
) -> Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>
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.