diesel/query_source/aliasing/
field_alias_mapper.rs1use super::{Alias, AliasSource, AliasedField};
2
3use crate::expression;
4use crate::query_source::{Column, Table, TableNotEqual};
5
6pub trait FieldAliasMapper<S> {
17 type Out;
22
23 fn map(self, alias: &Alias<S>) -> Self::Out;
25}
26
27#[doc(hidden)]
28pub trait FieldAliasMapperAssociatedTypesDisjointnessTrick<CT, S, C> {
33 type Out;
34 fn map(column: C, alias: &Alias<S>) -> Self::Out;
35}
36impl<S, C> FieldAliasMapper<S> for C
37where
38 S: AliasSource,
39 C: Column,
40 S::Target: FieldAliasMapperAssociatedTypesDisjointnessTrick<C::Table, S, C>,
41{
42 type Out = <S::Target as FieldAliasMapperAssociatedTypesDisjointnessTrick<C::Table, S, C>>::Out;
43
44 fn map(self, alias: &Alias<S>) -> Self::Out {
45 <S::Target as FieldAliasMapperAssociatedTypesDisjointnessTrick<C::Table, S, C>>::map(
46 self, alias,
47 )
48 }
49}
50
51impl<TS, TC, S, C> FieldAliasMapperAssociatedTypesDisjointnessTrick<TC, S, C> for TS
52where
53 S: AliasSource<Target = TS>,
54 C: Column<Table = TC>,
55 TC: Table,
56 TS: TableNotEqual<TC>,
57{
58 type Out = C;
59
60 fn map(column: C, _alias: &Alias<S>) -> Self::Out {
61 column
63 }
64}
65
66macro_rules! field_alias_mapper {
67 ($(
68 $Tuple:tt {
69 $(($idx:tt) -> $T:ident, $ST:ident, $TT:ident,)+
70 }
71 )+) => {
72 $(
73 impl<_S, $($T,)*> FieldAliasMapper<_S> for ($($T,)*)
74 where
75 _S: AliasSource,
76 $($T: FieldAliasMapper<_S>,)*
77 {
78 type Out = ($(<$T as FieldAliasMapper<_S>>::Out,)*);
79
80 fn map(self, alias: &Alias<_S>) -> Self::Out {
81 (
82 $(self.$idx.map(alias),)*
83 )
84 }
85 }
86 )*
87 }
88}
89
90diesel_derives::__diesel_for_each_tuple!(field_alias_mapper);
91
92impl<SPrev, SNew, F> FieldAliasMapper<SNew> for AliasedField<SPrev, F>
95where
96 SNew: AliasSource,
97{
98 type Out = Self;
99
100 fn map(self, _alias: &Alias<SNew>) -> Self::Out {
101 self
103 }
104}
105
106impl<S, F> FieldAliasMapper<S> for expression::nullable::Nullable<F>
107where
108 F: FieldAliasMapper<S>,
109{
110 type Out = expression::nullable::Nullable<<F as FieldAliasMapper<S>>::Out>;
111
112 fn map(self, alias: &Alias<S>) -> Self::Out {
113 expression::nullable::Nullable::new(self.0.map(alias))
114 }
115}
116
117impl<S, F> FieldAliasMapper<S> for expression::grouped::Grouped<F>
118where
119 F: FieldAliasMapper<S>,
120{
121 type Out = expression::grouped::Grouped<<F as FieldAliasMapper<S>>::Out>;
122
123 fn map(self, alias: &Alias<S>) -> Self::Out {
124 expression::grouped::Grouped(self.0.map(alias))
125 }
126}