Skip to main content

easy_sql/traits/
easy_executor.rs

1use easy_macros::always_context;
2
3use crate::Driver;
4
5use super::DriverConnection;
6
7#[always_context]
8/// Abstraction over [`sqlx::Executor`](https://docs.rs/sqlx/latest/sqlx/trait.Executor.html) used by the query macros.
9///
10/// Implemented for easy_sql and sqlx connections/pools; most users only need to pass a compatible
11/// connection to [`query!`](crate::query) or [`query_lazy!`](crate::query_lazy).
12///
13/// Will contain sql query watch functions in the future (gated by a feature)
14pub trait EasyExecutor<D: Driver> {
15    type InternalExecutor<'b>: sqlx::Executor<'b, Database = D::InternalDriver>
16    where
17        Self: 'b;
18
19    async fn query_setup<O: SetupSql<D> + Send + Sync>(
20        &mut self,
21        sql: O,
22    ) -> anyhow::Result<O::Output>
23    where
24        DriverConnection<D>: Send + Sync;
25
26    fn executor<'a>(&'a mut self) -> Self::InternalExecutor<'a>;
27}
28
29pub trait EasyExecutorInto<D: Driver>: EasyExecutor<D> {
30    type IntoInternalExecutor<'b>: sqlx::Executor<'b, Database = D::InternalDriver>
31    where
32        Self: 'b;
33
34    fn into_executor<'a>(self) -> Self::IntoInternalExecutor<'a>
35    where
36        Self: 'a;
37}
38
39#[always_context(skip(!))]
40impl<D: Driver, E: EasyExecutor<D> + ?Sized> EasyExecutor<D> for &mut E {
41    type InternalExecutor<'b>
42        = E::InternalExecutor<'b>
43    where
44        Self: 'b;
45
46    async fn query_setup<O: SetupSql<D> + Send + Sync>(
47        &mut self,
48        sql: O,
49    ) -> anyhow::Result<O::Output>
50    where
51        DriverConnection<D>: Send + Sync,
52    {
53        (**self).query_setup(sql).await
54    }
55
56    fn executor<'a>(&'a mut self) -> Self::InternalExecutor<'a> {
57        (**self).executor()
58    }
59}
60
61impl<D: Driver, E: EasyExecutor<D> + ?Sized> EasyExecutorInto<D> for &mut E {
62    type IntoInternalExecutor<'b>
63        = E::InternalExecutor<'b>
64    where
65        Self: 'b;
66
67    fn into_executor<'a>(self) -> Self::IntoInternalExecutor<'a>
68    where
69        Self: 'a,
70    {
71        (*self).executor()
72    }
73}
74
75#[always_context]
76/// Gives information about the SQL query to execute, and how to execute it with the provided executor. Used by setup structures.
77pub trait SetupSql<D: Driver> {
78    type Output;
79
80    async fn query(self, exec: &mut impl EasyExecutor<D>) -> anyhow::Result<Self::Output>;
81}