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
use chrono;

use super::ToSharedPredicate;
use super::super::expression;
use super::super::field;
use super::super::sql;

#[derive(Clone, Copy, Debug)]
pub enum Inequality {
    LessThan,
    LessThanEqual,
    GreaterThan,
    GreaterThanEqual
}

#[derive(Clone, Debug)]
pub struct InequalityPredicate<F, T> {
    field: F,
    value: T,
    inequality: Inequality
}

impl<F, T> InequalityPredicate<F, T> {
    pub fn get_field(&self) -> &F { &self.field }
    pub fn get_value(&self) -> &T { &self.value }
    pub fn get_inequality(&self) -> &Inequality { &self.inequality }
}

pub trait ToInequalityPredicate<T> {
    fn lt<B>(&self, val: B) -> super::SharedPredicate
        where B: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static;

    fn lte<B>(&self, val: B) -> super::SharedPredicate
        where B: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static;

    fn gt<B>(&self, val: B) -> super::SharedPredicate
        where B: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static;

    fn gte<B>(&self, val: B) -> super::SharedPredicate
        where B: expression::ToExpression<T> + sql::ToPredicateValue + Clone + 'static;
}

impl<F, T> super::Predicate for InequalityPredicate<F, T>
    where F: sql::ToPredicateValue,
          T: sql::ToPredicateValue { }

macro_rules! impl_for {
    ($field:ty, $expr:ty) => (

        impl ToInequalityPredicate<$expr> for $field {
            fn lt<B>(&self, val: B) -> super::SharedPredicate
                where B: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static {
                InequalityPredicate { field: self.clone(), value: val, inequality: Inequality::LessThan }.upcast()
            }

            fn lte<B>(&self, val: B) -> super::SharedPredicate
                where B: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static {
                InequalityPredicate { field: self.clone(), value: val, inequality: Inequality::LessThanEqual }.upcast()
            }

            fn gt<B>(&self, val: B) -> super::SharedPredicate
                where B: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static {
                InequalityPredicate { field: self.clone(), value: val, inequality: Inequality::GreaterThan }.upcast()
            }

            fn gte<B>(&self, val: B) -> super::SharedPredicate
                where B: expression::ToExpression<$expr> + sql::ToPredicateValue + Clone + 'static {
                InequalityPredicate { field: self.clone(), value: val, inequality: Inequality::GreaterThanEqual }.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);