use super::field_alias_mapper::FieldAliasMapper; use super::{AliasSource, AliasedField}; use crate::backend::{sql_dialect, Backend}; use crate::expression::{Expression, SelectableExpression, ValidGrouping}; use crate::helper_types::AliasedFields; use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement}; use crate::query_source::{AppearsInFromClause, Column, Never, QuerySource, Table, TableNotEqual}; use crate::result::QueryResult; use std::marker::PhantomData; #[derive(Debug, Clone, Copy, Default)] /// Represents an alias within diesel's query builder /// /// See [`alias!`](crate::alias) for more details. #[diesel_derives::__diesel_public_if( feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes", public_fields(source) )] pub struct Alias { /// The inner alias definition pub(crate) source: S, } impl Alias { /// Maps a single field of the source table in this alias pub fn field(&self, field: F) -> AliasedField where F: Column, { AliasedField { _alias_source: PhantomData, _field: field, } } /// Maps multiple fields of the source table in this alias /// (takes in tuples and some expressions) pub fn fields(&self, fields: Fields) -> AliasedFields where Fields: FieldAliasMapper, { fields.map(self) } } impl Alias { #[doc(hidden)] /// May be used to create an alias. Used by the [`alias!`](crate::alias) macro. pub const fn new(source: S) -> Self { Self { source } } } impl QueryId for Alias where Self: 'static, S: AliasSource, S::Target: QueryId, { type QueryId = Self; const HAS_STATIC_QUERY_ID: bool = ::HAS_STATIC_QUERY_ID; } impl QuerySource for Alias where Self: Clone, S: AliasSource, S::Target: QuerySource, ::DefaultSelection: FieldAliasMapper, <::DefaultSelection as FieldAliasMapper>::Out: SelectableExpression, { type FromClause = Self; type DefaultSelection = <::DefaultSelection as FieldAliasMapper>::Out; fn from_clause(&self) -> Self::FromClause { self.clone() } fn default_selection(&self) -> Self::DefaultSelection { self.fields(self.source.target().default_selection()) } } impl QueryFragment for Alias where S: AliasSource, DB: Backend, Self: QueryFragment, { fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { >::walk_ast(self, pass) } } impl QueryFragment for Alias where S: AliasSource, DB: Backend, S::Target: QueryFragment, { fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { self.source.target().walk_ast(pass.reborrow())?; pass.push_sql(" AS "); pass.push_identifier(S::NAME)?; Ok(()) } } impl AsQuery for Alias where S: AliasSource, S::Target: AsQuery, Self: QuerySource, ::DefaultSelection: ValidGrouping<()>, { type SqlType = <::DefaultSelection as Expression>::SqlType; type Query = SelectStatement>; fn as_query(self) -> Self::Query { SelectStatement::simple(self) } } /// This trait is used to allow external crates to implement /// `AppearsInFromClause for Alias` /// /// at the table level without running in conflicting impl issues /// /// Implementing this at the table level (in the crate that defines the tables) /// does not result in conflicting impl issues because the table is the struct /// we're implementing on. pub trait AliasAppearsInFromClause { /// Will be passed on to the `impl AppearsInFromClause` type Count; } impl AppearsInFromClause for Alias where S: AliasSource, S::Target: AliasAppearsInFromClause, { type Count = >::Count; } /// This trait is used to allow external crates to implement /// `AppearsInFromClause> for Alias` /// /// at the table level without running in conflicting impl issues /// /// Implementing this at the table level (in the crate that defines the tables) /// does not result in conflicting impl issues because the tables are specified as both first /// argument of the trait and struct we're implementing on. pub trait AliasAliasAppearsInFromClause { /// Will be passed on to the `impl AppearsInFromClause` type Count; } impl AliasAppearsInFromClause> for T1 where S2: AliasSource, T1: AliasAliasAppearsInFromClause, { type Count = >::Count; } /// This trait is used to allow external crates to implement /// `AppearsInFromClause> for Alias` /// /// at the alias level without running in conflicting impl issues /// /// Implementing this at the alias level (in the crate that defines the aliases) /// does not result in conflicting impl issues because the aliases are specified as both first /// argument of the trait and struct we're implementing on. /// /// The corresponding implementation of `AliasAliasAppearsInFromClause` for implementors of this /// trait is done at the table level, to avoid conflict with the implementation for distinct tables /// below. pub trait AliasAliasAppearsInFromClauseSameTable { /// Will be passed on to the `impl AppearsInFromClause` type Count; } // impl> AppearsInFromClause for Alias // where T1 != T2 impl AliasAppearsInFromClause for T1 where T1: TableNotEqual + Table, T2: Table, S: AliasSource, { type Count = Never; } // impl AppearsInFromClause> for Alias // where S1: AliasSource, S2: AliasSource, S1::Table != S2::Table impl AliasAliasAppearsInFromClause for T2 where T1: TableNotEqual + Table, T2: Table, S1: AliasSource, S2: AliasSource, { type Count = Never; } /// To be able to define `helper_types::Fields` with the usual conventions /// /// This type is intentionally not publicly exported #[allow(unreachable_pub)] pub trait GetAliasSourceFromAlias { type Source; } impl GetAliasSourceFromAlias for Alias { type Source = S; }