sea_orm/executor/select/
six.rs

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