use crate::expression::{Expression, ValidGrouping}; use crate::pg::Pg; use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement}; use crate::query_source::QuerySource; use crate::result::QueryResult; use crate::{JoinTo, SelectableExpression, Table}; /// Represents a query with an `ONLY` clause. #[derive(Debug, Clone, Copy, Default)] pub struct Only { pub(crate) source: S, } impl QueryId for Only where Self: 'static, S: QueryId, { type QueryId = Self; const HAS_STATIC_QUERY_ID: bool = ::HAS_STATIC_QUERY_ID; } impl QuerySource for Only where S: Table + Clone, ::DefaultSelection: ValidGrouping<()> + SelectableExpression>, { type FromClause = Self; type DefaultSelection = ::DefaultSelection; fn from_clause(&self) -> Self::FromClause { self.clone() } fn default_selection(&self) -> Self::DefaultSelection { self.source.default_selection() } } impl QueryFragment for Only where S: QueryFragment, { fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, Pg>) -> QueryResult<()> { pass.push_sql(" ONLY "); self.source.walk_ast(pass.reborrow())?; Ok(()) } } impl AsQuery for Only where S: Table + Clone, ::DefaultSelection: ValidGrouping<()> + SelectableExpression>, { type SqlType = <::DefaultSelection as Expression>::SqlType; type Query = SelectStatement>; fn as_query(self) -> Self::Query { SelectStatement::simple(self) } } impl JoinTo for Only where S: JoinTo, T: Table, S: Table, { type FromClause = >::FromClause; type OnClause = >::OnClause; fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause) { >::join_target(rhs) } } impl Table for Only where S: Table + Clone + AsQuery, ::PrimaryKey: SelectableExpression>, ::AllColumns: SelectableExpression>, ::DefaultSelection: ValidGrouping<()> + SelectableExpression>, { type PrimaryKey = ::PrimaryKey; type AllColumns = ::AllColumns; fn primary_key(&self) -> Self::PrimaryKey { self.source.primary_key() } fn all_columns() -> Self::AllColumns { S::all_columns() } }