feat(core): implement Executor for blocking runtimes

This commit is contained in:
Ryan Leckey 2021-02-18 23:58:59 -08:00
parent 562ea39ee3
commit 3078957a66
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9

View File

@ -5,27 +5,89 @@ pub trait Executor<Rt: Runtime>: crate::Executor<Rt>
where
Self::Database: Database,
{
fn execute<'x, 'e, 'q>(
/// Execute the SQL query and return information about the result, including
/// the number of rows affected, if any.
fn execute<'x, 'e, 'q, 'a, E>(
&'e mut self,
sql: &'q str,
query: E,
) -> crate::Result<<Self::Database as Database>::QueryResult>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x;
'q: 'x,
'a: 'x;
fn fetch_all<'x, 'e, 'q>(
fn fetch_all<'x, 'e, 'q, 'a, E>(
&'e mut self,
sql: &'q str,
query: E,
) -> crate::Result<Vec<<Self::Database as Database>::Row>>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x;
'q: 'x,
'a: 'x;
fn fetch_optional<'x, 'e, 'q>(
fn fetch_optional<'x, 'e, 'q, 'a, E>(
&'e mut self,
sql: &'q str,
query: E,
) -> crate::Result<Option<<Self::Database as Database>::Row>>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x;
'q: 'x,
'a: 'x;
fn fetch_one<'x, 'e, 'q, 'a, E>(
&'e mut self,
query: E,
) -> crate::Result<<Self::Database as Database>::Row>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x,
'a: 'x,
{
Executor::fetch_optional(self, query)?.ok_or(crate::Error::RowNotFound)
}
}
impl<Rt: Runtime, X: Executor<Rt>> Executor<Rt> for &'_ mut X {
fn execute<'x, 'e, 'q, 'a, E>(
&'e mut self,
query: E,
) -> crate::Result<<Self::Database as Database>::QueryResult>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x,
'a: 'x,
{
Executor::execute(&mut **self, query)
}
fn fetch_all<'x, 'e, 'q, 'a, E>(
&'e mut self,
query: E,
) -> crate::Result<Vec<<Self::Database as Database>::Row>>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x,
'a: 'x,
{
Executor::fetch_all(&mut **self, query)
}
fn fetch_optional<'x, 'e, 'q, 'a, E>(
&'e mut self,
query: E,
) -> crate::Result<Option<<Self::Database as Database>::Row>>
where
E: 'x + crate::Execute<'q, 'a, Self::Database>,
'e: 'x,
'q: 'x,
'a: 'x,
{
Executor::fetch_optional(&mut **self, query)
}
}