sqlx/src/executor.rs
2019-08-20 21:01:19 -07:00

59 lines
1.6 KiB
Rust

use crate::{backend::Backend, query::RawQuery, row::FromRow};
use futures_core::{future::BoxFuture, stream::BoxStream};
use std::io;
pub trait Executor: Send {
type Backend: Backend;
fn execute<'c, 'q, Q: 'q + 'c>(&'c self, query: Q) -> BoxFuture<'c, io::Result<u64>>
where
Q: RawQuery<'q, Backend = Self::Backend>;
fn fetch<'c, 'q, T: 'c, Q: 'q + 'c>(&'c self, query: Q) -> BoxStream<'c, io::Result<T>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
T: FromRow<Self::Backend> + Send + Unpin;
fn fetch_optional<'c, 'q, T: 'c, Q: 'q + 'c>(
&'c self,
query: Q,
) -> BoxFuture<'c, io::Result<Option<T>>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
T: FromRow<Self::Backend>;
}
impl<'e, E> Executor for &'e E
where
E: Executor + Send + Sync,
{
type Backend = E::Backend;
#[inline]
fn execute<'c, 'q, Q: 'q + 'c>(&'c self, query: Q) -> BoxFuture<'c, io::Result<u64>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
{
(*self).execute(query)
}
fn fetch<'c, 'q, T: 'c, Q: 'q + 'c>(&'c self, query: Q) -> BoxStream<'c, io::Result<T>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
T: FromRow<Self::Backend> + Send + Unpin,
{
(*self).fetch(query)
}
fn fetch_optional<'c, 'q, T: 'c, Q: 'q + 'c>(
&'c self,
query: Q,
) -> BoxFuture<'c, io::Result<Option<T>>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
T: FromRow<Self::Backend>,
{
(*self).fetch_optional(query)
}
}