sea_orm/entity/
partial_model.rs

1use crate::{EntityTrait, FromQueryResult, IdenStatic, Iterable, ModelTrait, QuerySelect};
2use sea_query::Expr;
3
4/// A trait for a part of [Model](super::model::ModelTrait)
5pub trait PartialModelTrait: FromQueryResult {
6    /// Select specific columns this [PartialModel] needs
7    ///
8    /// No need to implement this method, please implement `select_cols_nested` instead.
9    fn select_cols<S: QuerySelect>(select: S) -> S {
10        Self::select_cols_nested(select, None, None)
11    }
12
13    /// Used when nesting these structs into each other.
14    ///
15    /// Example impl
16    ///
17    /// ```ignore
18    /// fn select_cols_nested<S: QuerySelect>(mut select: S, prefix: Option<&str>) -> S {
19    ///     if let Some(prefix) = prefix {
20    ///         for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
21    ///             let alias = format!("{prefix}{}", col.as_str());
22    ///             select = select.column_as(col, alias);
23    ///         }
24    ///     } else {
25    ///         for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
26    ///             select = select.column(col);
27    ///         }
28    ///     }
29    ///     select
30    /// }
31    /// ```
32    fn select_cols_nested<S: QuerySelect>(
33        select: S,
34        prefix: Option<&str>,
35        alias: Option<&'static str>,
36    ) -> S;
37}
38
39impl<T: PartialModelTrait> PartialModelTrait for Option<T> {
40    fn select_cols_nested<S: QuerySelect>(
41        select: S,
42        prefix: Option<&str>,
43        alias: Option<&'static str>,
44    ) -> S {
45        T::select_cols_nested(select, prefix, alias)
46    }
47}
48
49impl<T: ModelTrait + FromQueryResult> PartialModelTrait for T {
50    fn select_cols_nested<S: QuerySelect>(
51        mut select: S,
52        prefix: Option<&str>,
53        alias: Option<&'static str>,
54    ) -> S {
55        match (prefix, alias) {
56            (Some(prefix), Some(alias)) => {
57                for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
58                    let select_as = format!("{prefix}{}", col.as_str());
59                    select = select.column_as(Expr::col((alias, col)), select_as);
60                }
61            }
62            (Some(prefix), None) => {
63                for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
64                    let select_as = format!("{prefix}{}", col.as_str());
65                    select = select.column_as(col, select_as);
66                }
67            }
68            (None, Some(alias)) => {
69                for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
70                    select = select.column_as(Expr::col((alias, col)), col.as_str());
71                }
72            }
73            (None, None) => {
74                for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
75                    select = select.column(col);
76                }
77            }
78        }
79        select
80    }
81}