use super::from_clause::AsQuerySource; use super::*; use crate::backend::DieselReserveSpecialization; use crate::expression::grouped::Grouped; use crate::expression::operators::{And, Or}; use crate::expression::*; use crate::sql_types::BoolOrNullableBool; /// Add `Predicate` to the current `WHERE` clause, joining with `AND` if /// applicable. pub trait WhereAnd { /// What is the type of the resulting `WHERE` clause? type Output; /// See the trait-level docs. fn and(self, predicate: Predicate) -> Self::Output; } /// Add `Predicate` to the current `WHERE` clause, joining with `OR` if /// applicable. pub trait WhereOr { /// What is the type of the resulting `WHERE` clause? type Output; /// See the trait-level docs. fn or(self, predicate: Predicate) -> Self::Output; } /// Represents that a query has no `WHERE` clause. #[derive(Debug, Clone, Copy, QueryId)] pub struct NoWhereClause; impl QueryFragment for NoWhereClause where DB: Backend + DieselReserveSpecialization, { fn walk_ast<'b>(&'b self, _: AstPass<'_, 'b, DB>) -> QueryResult<()> { Ok(()) } } impl WhereAnd for NoWhereClause where Predicate: Expression, Predicate::SqlType: BoolOrNullableBool, { type Output = WhereClause; fn and(self, predicate: Predicate) -> Self::Output { WhereClause(predicate) } } impl WhereOr for NoWhereClause where Predicate: Expression, Predicate::SqlType: BoolOrNullableBool, { type Output = WhereClause; fn or(self, predicate: Predicate) -> Self::Output { WhereClause(predicate) } } impl From for BoxedWhereClause<'_, DB> { fn from(_: NoWhereClause) -> Self { BoxedWhereClause::None } } /// The `WHERE` clause of a query. #[derive(Debug, Clone, Copy)] pub struct WhereClause(Expr); impl diesel::query_builder::QueryId for WhereClause { type QueryId = WhereClause<::QueryId>; const HAS_STATIC_QUERY_ID: bool = ::HAS_STATIC_QUERY_ID && true; const IS_WINDOW_FUNCTION: bool = const { if Expr::IS_WINDOW_FUNCTION { panic!("Using window functions in WHERE clauses is not supported"); } false }; } impl QueryFragment for WhereClause where DB: Backend + DieselReserveSpecialization, Expr: QueryFragment, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { out.push_sql(" WHERE "); self.0.walk_ast(out.reborrow())?; Ok(()) } } impl WhereAnd for WhereClause where Expr: Expression, Expr::SqlType: BoolOrNullableBool, Predicate: Expression, Predicate::SqlType: BoolOrNullableBool, { type Output = WhereClause>>; fn and(self, predicate: Predicate) -> Self::Output { WhereClause(Grouped(And::new(self.0, predicate))) } } impl WhereOr for WhereClause where Expr: Expression, Expr::SqlType: BoolOrNullableBool, Predicate: Expression, Predicate::SqlType: BoolOrNullableBool, { type Output = WhereClause>>; fn or(self, predicate: Predicate) -> Self::Output { WhereClause(Grouped(Or::new(self.0, predicate))) } } impl<'a, DB, Predicate> From> for BoxedWhereClause<'a, DB> where DB: Backend, Predicate: QueryFragment + Send + 'a, { fn from(where_clause: WhereClause) -> Self { BoxedWhereClause::Where(Box::new(where_clause.0)) } } /// Marker trait indicating that a `WHERE` clause is valid for a given query /// source. pub trait ValidWhereClause {} impl ValidWhereClause for NoWhereClause {} impl ValidWhereClause for WhereClause where Expr: AppearsOnTable, QS: AsQuerySource, { } impl ValidWhereClause for WhereClause where Expr: AppearsOnTable { } #[allow(missing_debug_implementations)] // We can't... pub enum BoxedWhereClause<'a, DB> { Where(Box + Send + 'a>), None, } impl QueryFragment for BoxedWhereClause<'_, DB> where DB: Backend + DieselReserveSpecialization, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { match *self { BoxedWhereClause::Where(ref where_clause) => { out.push_sql(" WHERE "); where_clause.walk_ast(out) } BoxedWhereClause::None => Ok(()), } } } impl QueryId for BoxedWhereClause<'_, DB> { type QueryId = (); const HAS_STATIC_QUERY_ID: bool = false; } impl<'a, DB, Predicate> WhereAnd for BoxedWhereClause<'a, DB> where DB: Backend + 'a, Predicate: QueryFragment + Send + 'a, Grouped + Send + 'a>, Predicate>>: QueryFragment, { type Output = Self; fn and(self, predicate: Predicate) -> Self::Output { use self::BoxedWhereClause::Where; match self { Where(where_clause) => Where(Box::new(Grouped(And::new(where_clause, predicate)))), BoxedWhereClause::None => Where(Box::new(predicate)), } } } impl<'a, DB, Predicate> WhereOr for BoxedWhereClause<'a, DB> where DB: Backend + 'a, Predicate: QueryFragment + Send + 'a, Grouped + Send + 'a>, Predicate>>: QueryFragment, { type Output = Self; fn or(self, predicate: Predicate) -> Self::Output { use self::BoxedWhereClause::Where; match self { Where(where_clause) => Where(Box::new(Grouped(Or::new(where_clause, predicate)))), BoxedWhereClause::None => Where(Box::new(predicate)), } } }