use super::{AstPass, QueryFragment, QueryId}; use crate::backend::Backend; use crate::query_source::AppearsInFromClause; use crate::{QueryResult, QuerySource}; /// This type represents a not existing from clause /// /// Custom backends can provide a custom [`QueryFragment`] /// impl by specializing the implementation via /// [`SqlDialect::EmptyFromClauseSyntax`](crate::backend::SqlDialect::EmptyFromClauseSyntax) #[cfg_attr( feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes", cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes") )] #[derive(Debug, Clone, Copy, QueryId)] pub struct NoFromClause; impl QueryFragment for NoFromClause where Self: QueryFragment, DB: Backend, { fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { >::walk_ast(self, pass) } } impl QueryFragment for NoFromClause where DB: Backend, { fn walk_ast<'b>(&'b self, _pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { Ok(()) } } pub trait AsQuerySource { type QuerySource: QuerySource; fn as_query_source(&self) -> &Self::QuerySource; } impl AsQuerySource for QS where QS: QuerySource, { type QuerySource = Self; fn as_query_source(&self) -> &Self::QuerySource { self } } #[doc(hidden)] pub struct FromClause { pub(crate) source: F, pub(crate) from_clause: F::FromClause, } impl AsQuerySource for FromClause where F: QuerySource, { type QuerySource = F; fn as_query_source(&self) -> &Self::QuerySource { &self.source } } impl QueryId for FromClause where F: QuerySource + QueryId, { type QueryId = F::QueryId; const HAS_STATIC_QUERY_ID: bool = F::HAS_STATIC_QUERY_ID; } impl Copy for FromClause where F: QuerySource + Copy, F::FromClause: Copy, { } impl Clone for FromClause where F: QuerySource + Clone, F::FromClause: Clone, { fn clone(&self) -> Self { Self { source: self.source.clone(), from_clause: self.from_clause.clone(), } } } impl std::fmt::Debug for FromClause where F: QuerySource, F::FromClause: std::fmt::Debug, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("FromClause") .field("from_clause", &self.from_clause) .finish() } } impl FromClause { pub(crate) fn new(qs: F) -> Self { Self { from_clause: qs.from_clause(), source: qs, } } } impl QueryFragment for FromClause where F: QuerySource, DB: Backend, F::FromClause: QueryFragment, { fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { pass.push_sql(" FROM "); self.from_clause.walk_ast(pass) } } impl AppearsInFromClause for FromClause where QS1: QuerySource, QS2: QuerySource, QS2: AppearsInFromClause, { type Count = >::Count; }