use super::{Alias, AliasSource, AliasedField}; use crate::expression; use crate::query_source::{Column, Table, TableNotEqual}; /// Serves to map `Self` to `Alias` /// /// Any column `Self` that belongs to `S::Table` will be transformed into `AliasedField` /// /// Any column `Self` that does not belong to `S::Table` will be left untouched. /// /// This also works with tuples and some expressions. // This first part is implemented by the `table!` macro. // The second part is useful to implement the joins, and may be useful to an end-user for // ergonomics. pub trait FieldAliasMapper { /// Output type when mapping `C` to `Alias` /// /// If `C: Column`, `Out = AliasedField` /// Otherwise, `Out = C` type Out; /// Does the mapping fn map(self, alias: &Alias) -> Self::Out; } #[doc(hidden)] /// Allows implementing `FieldAliasMapper` in external crates without running into conflicting impl /// errors due to https://github.com/rust-lang/rust/issues/20400 /// /// We will always have `Self = S::Table` and `CT = C::Table` pub trait FieldAliasMapperAssociatedTypesDisjointnessTrick { type Out; fn map(column: C, alias: &Alias) -> Self::Out; } impl FieldAliasMapper for C where S: AliasSource, C: Column, S::Target: FieldAliasMapperAssociatedTypesDisjointnessTrick, { type Out = >::Out; fn map(self, alias: &Alias) -> Self::Out { >::map( self, alias, ) } } impl FieldAliasMapperAssociatedTypesDisjointnessTrick for TS where S: AliasSource, C: Column
, TC: Table, TS: TableNotEqual, { type Out = C; fn map(column: C, _alias: &Alias) -> Self::Out { // left untouched because the tables are different column } } macro_rules! field_alias_mapper { ($( $Tuple:tt { $(($idx:tt) -> $T:ident, $ST:ident, $TT:ident,)+ } )+) => { $( impl<_S, $($T,)*> FieldAliasMapper<_S> for ($($T,)*) where _S: AliasSource, $($T: FieldAliasMapper<_S>,)* { type Out = ($(<$T as FieldAliasMapper<_S>>::Out,)*); fn map(self, alias: &Alias<_S>) -> Self::Out { ( $(self.$idx.map(alias),)* ) } } )* } } diesel_derives::__diesel_for_each_tuple!(field_alias_mapper); // The following `FieldAliasMapper` impls are useful for the generic join implementations. // More may be added. impl FieldAliasMapper for AliasedField where SNew: AliasSource, { type Out = Self; fn map(self, _alias: &Alias) -> Self::Out { // left untouched because it has already been aliased self } } impl FieldAliasMapper for expression::nullable::Nullable where F: FieldAliasMapper, { type Out = expression::nullable::Nullable<>::Out>; fn map(self, alias: &Alias) -> Self::Out { expression::nullable::Nullable::new(self.0.map(alias)) } } impl FieldAliasMapper for expression::grouped::Grouped where F: FieldAliasMapper, { type Out = expression::grouped::Grouped<>::Out>; fn map(self, alias: &Alias) -> Self::Out { expression::grouped::Grouped(self.0.map(alias)) } }