sea_orm/executor/select/
five.rs

1use super::*;
2use crate::{
3    JoinType, Paginator, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, QueryTrait, Related,
4    SelectC, SelectFive, SelectSix, Topology, TopologyStar,
5    combine::{SelectD, SelectE, prepare_select_col},
6};
7
8impl<E, F, G, H, I, TOP> SelectFive<E, F, G, H, I, TOP>
9where
10    E: EntityTrait,
11    F: EntityTrait,
12    G: EntityTrait,
13    H: EntityTrait,
14    I: EntityTrait,
15    TOP: Topology,
16{
17    pub(crate) fn new(query: SelectStatement) -> Self {
18        Self::new_without_prepare(query).prepare_select()
19    }
20
21    pub(crate) fn new_without_prepare(query: SelectStatement) -> Self {
22        Self {
23            query,
24            entity: PhantomData,
25        }
26    }
27
28    fn prepare_select(mut self) -> Self {
29        prepare_select_col::<I, _, _>(&mut self, SelectE);
30        self
31    }
32
33    /// Left Join with a Related Entity and select all Entities.
34    pub fn find_also<T, J>(self, _: T, _: J) -> SelectSix<E, F, G, H, I, J, TopologyStar>
35    where
36        J: EntityTrait,
37        T: EntityTrait + Related<J>,
38    {
39        SelectSix::new(
40            self.join_join(JoinType::LeftJoin, T::to(), T::via())
41                .into_query(),
42        )
43    }
44}
45
46macro_rules! impl_query_trait {
47    ( $trait: ident ) => {
48        impl<E, F, G, H, I, TOP> $trait for SelectFive<E, F, G, H, I, TOP>
49        where
50            E: EntityTrait,
51            F: EntityTrait,
52            G: EntityTrait,
53            H: EntityTrait,
54            I: EntityTrait,
55            TOP: Topology,
56        {
57            type QueryStatement = SelectStatement;
58
59            fn query(&mut self) -> &mut SelectStatement {
60                &mut self.query
61            }
62        }
63    };
64}
65
66impl<E, F, G, H, I, TOP> QueryTrait for SelectFive<E, F, G, H, I, TOP>
67where
68    E: EntityTrait,
69    F: EntityTrait,
70    G: EntityTrait,
71    H: EntityTrait,
72    I: EntityTrait,
73    TOP: Topology,
74{
75    type QueryStatement = SelectStatement;
76    fn query(&mut self) -> &mut SelectStatement {
77        &mut self.query
78    }
79    fn as_query(&self) -> &SelectStatement {
80        &self.query
81    }
82    fn into_query(self) -> SelectStatement {
83        self.query
84    }
85}
86
87impl_query_trait!(QuerySelect);
88impl_query_trait!(QueryFilter);
89impl_query_trait!(QueryOrder);
90
91impl<M, N, O, P, Q> SelectorTrait for SelectFiveModel<M, N, O, P, Q>
92where
93    M: FromQueryResult + Sized,
94    N: FromQueryResult + Sized,
95    O: FromQueryResult + Sized,
96    P: FromQueryResult + Sized,
97    Q: FromQueryResult + Sized,
98{
99    type Item = (M, Option<N>, Option<O>, Option<P>, Option<Q>);
100
101    fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, DbErr> {
102        Ok((
103            M::from_query_result(&res, SelectA.as_str())?,
104            N::from_query_result_optional(&res, SelectB.as_str())?,
105            O::from_query_result_optional(&res, SelectC.as_str())?,
106            P::from_query_result_optional(&res, SelectD.as_str())?,
107            Q::from_query_result_optional(&res, SelectE.as_str())?,
108        ))
109    }
110}
111
112impl<E, F, G, H, I, TOP> SelectFive<E, F, G, H, I, TOP>
113where
114    E: EntityTrait,
115    F: EntityTrait,
116    G: EntityTrait,
117    H: EntityTrait,
118    I: EntityTrait,
119    TOP: Topology,
120{
121    /// Perform a conversion into a [SelectFiveModel]
122    pub fn into_model<M, N, O, P, Q>(self) -> Selector<SelectFiveModel<M, N, O, P, Q>>
123    where
124        M: FromQueryResult,
125        N: FromQueryResult,
126        O: FromQueryResult,
127        P: FromQueryResult,
128        Q: FromQueryResult,
129    {
130        Selector {
131            query: self.query,
132            selector: PhantomData,
133        }
134    }
135
136    /// Perform a conversion into a [SelectFiveModel] with [PartialModel](PartialModelTrait)
137    pub fn into_partial_model<M, N, O, P, Q>(self) -> Selector<SelectFiveModel<M, N, O, P, Q>>
138    where
139        M: PartialModelTrait,
140        N: PartialModelTrait,
141        O: PartialModelTrait,
142        P: PartialModelTrait,
143        Q: PartialModelTrait,
144    {
145        let select = QuerySelect::select_only(self);
146        let select = M::select_cols(select);
147        let select = N::select_cols(select);
148        let select = O::select_cols(select);
149        let select = P::select_cols(select);
150        let select = Q::select_cols(select);
151        select.into_model::<M, N, O, P, Q>()
152    }
153
154    /// Convert the Models into JsonValue
155    #[cfg(feature = "with-json")]
156    pub fn into_json(
157        self,
158    ) -> Selector<SelectFiveModel<JsonValue, JsonValue, JsonValue, JsonValue, JsonValue>> {
159        Selector {
160            query: self.query,
161            selector: PhantomData,
162        }
163    }
164
165    /// Get one Model from the Select query
166    pub fn one<C>(
167        self,
168        db: &C,
169    ) -> Result<
170        Option<(
171            E::Model,
172            Option<F::Model>,
173            Option<G::Model>,
174            Option<H::Model>,
175            Option<I::Model>,
176        )>,
177        DbErr,
178    >
179    where
180        C: ConnectionTrait,
181    {
182        self.into_model().one(db)
183    }
184
185    /// Get all Models from the Select query
186    pub fn all<C>(
187        self,
188        db: &C,
189    ) -> Result<
190        Vec<(
191            E::Model,
192            Option<F::Model>,
193            Option<G::Model>,
194            Option<H::Model>,
195            Option<I::Model>,
196        )>,
197        DbErr,
198    >
199    where
200        C: ConnectionTrait,
201    {
202        self.into_model().all(db)
203    }
204
205    /// Stream the results of a Select operation on a Model
206    pub fn stream<'a: 'b, 'b, C>(
207        self,
208        db: &'a C,
209    ) -> Result<
210        impl Iterator<
211            Item = Result<
212                (
213                    E::Model,
214                    Option<F::Model>,
215                    Option<G::Model>,
216                    Option<H::Model>,
217                    Option<I::Model>,
218                ),
219                DbErr,
220            >,
221        > + 'b,
222        DbErr,
223    >
224    where
225        C: ConnectionTrait + StreamTrait,
226    {
227        self.into_model().stream(db)
228    }
229}
230
231impl<'db, C, EE, FF, GG, HH, II, E, F, G, H, I, TOP> PaginatorTrait<'db, C>
232    for SelectFive<E, F, G, H, I, TOP>
233where
234    C: ConnectionTrait,
235    E: EntityTrait<Model = EE>,
236    F: EntityTrait<Model = FF>,
237    G: EntityTrait<Model = GG>,
238    H: EntityTrait<Model = HH>,
239    I: EntityTrait<Model = II>,
240    EE: FromQueryResult + Sized + 'db,
241    FF: FromQueryResult + Sized + 'db,
242    GG: FromQueryResult + Sized + 'db,
243    HH: FromQueryResult + Sized + 'db,
244    II: FromQueryResult + Sized + 'db,
245    TOP: Topology,
246{
247    type Selector = SelectFiveModel<EE, FF, GG, HH, II>;
248
249    fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, Self::Selector> {
250        self.into_model().paginate(db, page_size)
251    }
252}