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
use chrono; use super::super::sql; use super::super::expression; use super::super::field; use super::ToSharedPredicate; #[derive(Clone, Copy, Debug)] pub enum InRangeBounds { ExcludeBoth, IncludeBoth, ExcludeRight, ExcludeLeft } #[derive(Clone, Debug)] pub struct InRangePredicate<F, T1, T2> { field: F, from: T1, to: T2, bounds: InRangeBounds } impl<F, T1, T2> InRangePredicate<F, T1, T2> { pub fn get_field(&self) -> &F { &self.field } pub fn get_from(&self) -> &T1 { &self.from } pub fn get_to(&self) -> &T2 { &self.to } pub fn get_bounds(&self) -> &InRangeBounds { &self.bounds } } pub trait ToInRangePredicate<T> { fn in_range<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static; fn in_range_exclude_left<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static; fn in_range_exclude_right<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static; fn in_range_exclude<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static; } impl<F, T1, T2> super::Predicate for InRangePredicate<F, T1, T2> where F: sql::ToPredicateValue, T1: sql::ToPredicateValue, T2: sql::ToPredicateValue { } macro_rules! impl_for { ($field:ty, $expr:ty) => ( impl ToInRangePredicate<$expr> for $field { fn in_range<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static { InRangePredicate { field: self.clone(), from: from, to: to, bounds: InRangeBounds::IncludeBoth }.upcast() } fn in_range_exclude_left<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static { InRangePredicate { field: self.clone(), from: from, to: to, bounds: InRangeBounds::ExcludeLeft }.upcast() } fn in_range_exclude_right<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static { InRangePredicate { field: self.clone(), from: from, to: to, bounds: InRangeBounds::ExcludeRight }.upcast() } fn in_range_exclude<B1, B2>(&self, from: B1, to: B2) -> super::SharedPredicate where B1: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static, B2: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static { InRangePredicate { field: self.clone(), from: from, to: to, bounds: InRangeBounds::ExcludeBoth }.upcast() } } ) } impl_for!(field::I8Field, i8); impl_for!(field::I16Field, i16); impl_for!(field::I32Field, i32); impl_for!(field::I64Field, i64); impl_for!(field::F32Field, f32); impl_for!(field::F64Field, f64); impl_for!(field::TimespecField, chrono::NaiveDateTime); impl_for!(field::OptionalI8Field, Option<i8>); impl_for!(field::OptionalI16Field, Option<i16>); impl_for!(field::OptionalI32Field, Option<i32>); impl_for!(field::OptionalI64Field, Option<i64>); impl_for!(field::OptionalF32Field, Option<f32>); impl_for!(field::OptionalF64Field, Option<f64>); impl_for!(field::OptionalTimespecField, Option<chrono::NaiveDateTime>); impl_for!(expression::RawExpression, expression::RawExpression);