1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
use backend::Backend; use expression::*; use expression::operators::And; use expression_methods::*; use result::QueryResult; use super::*; use types::Bool; pub trait WhereAnd<Predicate> { type Output; fn and(self, predicate: Predicate) -> Self::Output; } #[derive(Debug, Clone, Copy)] pub struct NoWhereClause; impl_query_id!(NoWhereClause); impl<DB: Backend> QueryFragment<DB> for NoWhereClause { fn walk_ast(&self, _: AstPass<DB>) -> QueryResult<()> { Ok(()) } } impl<Predicate> WhereAnd<Predicate> for NoWhereClause where Predicate: Expression<SqlType=Bool>, { type Output = WhereClause<Predicate>; fn and(self, predicate: Predicate) -> Self::Output { WhereClause(predicate) } } impl<DB: Backend> Into<Option<Box<QueryFragment<DB>>>> for NoWhereClause { fn into(self) -> Option<Box<QueryFragment<DB>>> { None } } #[derive(Debug, Clone, Copy)] pub struct WhereClause<Expr>(Expr); impl<DB, Expr> QueryFragment<DB> for WhereClause<Expr> where DB: Backend, Expr: QueryFragment<DB>, { fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> { out.push_sql(" WHERE "); self.0.walk_ast(out.reborrow())?; Ok(()) } } impl_query_id!(WhereClause<T>); impl<Expr, Predicate> WhereAnd<Predicate> for WhereClause<Expr> where Expr: Expression<SqlType=Bool>, Predicate: Expression<SqlType=Bool>, { type Output = WhereClause<And<Expr, Predicate>>; fn and(self, predicate: Predicate) -> Self::Output { WhereClause(self.0.and(predicate)) } } impl<'a, DB, Predicate> Into<Option<Box<QueryFragment<DB> + 'a>>> for WhereClause<Predicate> where DB: Backend, Predicate: QueryFragment<DB> + 'a, { fn into(self) -> Option<Box<QueryFragment<DB> + 'a>> { Some(Box::new(self.0)) } }