sea_orm/entity/
partial_model.rs1use crate::{EntityTrait, FromQueryResult, IdenStatic, Iterable, ModelTrait, QuerySelect};
2use sea_query::Expr;
3
4pub trait PartialModelTrait: FromQueryResult {
6 fn select_cols<S: QuerySelect>(select: S) -> S {
10 Self::select_cols_nested(select, None, None)
11 }
12
13 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}