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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
use backend::Backend; use expression::*; use expression::helper_types::SqlTypeOf; use query_builder::{QueryBuilder, QueryFragment, BuildQueryResult}; use result::QueryResult; use types::Bool; #[derive(Debug, Copy, Clone)] pub struct In<T, U> { left: T, values: U, } pub type EqAny<T, U> = In<T, <U as AsInExpression<SqlTypeOf<T>>>::InExpression>; impl<T, U> In<T, U> { pub fn new(left: T, values: U) -> Self { In { left: left, values: values, } } } impl<T, U> Expression for In<T, U> where T: Expression, U: Expression<SqlType=T::SqlType>, { type SqlType = Bool; } impl<T, U, QS> SelectableExpression<QS> for In<T, U> where In<T, U>: Expression, T: SelectableExpression<QS>, U: SelectableExpression<QS>, { } impl<T, U> NonAggregate for In<T, U> where In<T, U>: Expression, { } impl<T, U, DB> QueryFragment<DB> for In<T, U> where DB: Backend, T: QueryFragment<DB>, U: QueryFragment<DB>, { fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult { try!(self.left.to_sql(out)); out.push_sql(" IN ("); try!(self.values.to_sql(out)); out.push_sql(")"); Ok(()) } fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> { try!(self.left.collect_binds(out)); try!(self.values.collect_binds(out)); Ok(()) } fn is_safe_to_cache_prepared(&self) -> bool { self.left.is_safe_to_cache_prepared() && self.values.is_safe_to_cache_prepared() } } impl_query_id!(In<T, U>); use std::marker::PhantomData; use query_builder::SelectStatement; pub trait AsInExpression<T> { type InExpression: Expression<SqlType=T>; fn as_in_expression(self) -> Self::InExpression; } impl<I, T, ST> AsInExpression<ST> for I where I: IntoIterator<Item=T>, T: AsExpression<ST>, { type InExpression = Many<T::Expression>; fn as_in_expression(self) -> Self::InExpression { let expressions = self.into_iter() .map(AsExpression::as_expression).collect(); Many(expressions) } } impl<ST, S, F, W, O, L, Of> AsInExpression<ST> for SelectStatement<ST, S, F, W, O, L, Of> where SelectStatement<ST, S, F, W, O, L, Of>: Expression, { type InExpression = Subselect<Self, ST>; fn as_in_expression(self) -> Self::InExpression { Subselect { values: self, _sql_type: PhantomData } } } #[derive(Debug)] pub struct Many<T>(Vec<T>); impl<T: Expression> Expression for Many<T> { type SqlType = T::SqlType; } impl<T, QS> SelectableExpression<QS> for Many<T> where Many<T>: Expression, T: SelectableExpression<QS>, { } impl<T, DB> QueryFragment<DB> for Many<T> where DB: Backend, T: QueryFragment<DB>, { fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult { try!(self.0[0].to_sql(out)); for value in self.0[1..].iter() { out.push_sql(", "); try!(value.to_sql(out)); } Ok(()) } fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> { for value in self.0.iter() { try!(value.collect_binds(out)); } Ok(()) } fn is_safe_to_cache_prepared(&self) -> bool { false } } impl_query_id!(noop: Many<T>); #[derive(Debug, Copy, Clone)] pub struct Subselect<T, ST> { values: T, _sql_type: PhantomData<ST>, } impl<T: Expression, ST> Expression for Subselect<T, ST> { type SqlType = ST; } impl<T, ST, QS> SelectableExpression<QS> for Subselect<T, ST> where Subselect<T, ST>: Expression, T: SelectableExpression<QS>, { } impl<T, ST, DB> QueryFragment<DB> for Subselect<T, ST> where DB: Backend, T: QueryFragment<DB>, { fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult { self.values.to_sql(out) } fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> { self.values.collect_binds(out) } fn is_safe_to_cache_prepared(&self) -> bool { self.values.is_safe_to_cache_prepared() } } impl_query_id!(Subselect<T, ST>);