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
use super::*;

/// Struct to facilitate the definiion of a condition associated with a given field
/// For example, create a condition to filter name column equal to 'Jane':
///
/// ```rust
/// use derive_sql::structs::{Field, filter, order};
/// use derive_sql::traits::{Filter, Order};
///
/// let condition: filter::Condition<_> = Field::from("name").eq("Jane");
/// assert!(condition.filter().eq("`name` = 'Jane'"));
///
/// let condition: order::Condition = Field::from("name").ascending();
/// assert!(condition.as_order_clause().eq("`name` ASC"));
/// ```
pub struct Field {
  label: String,
}

impl Field {
  /// Create a new field with the given label
  pub fn from(label: &str) -> Field {
    Field { label: label.to_string() }
  }

  /// Generate a test on Null
  pub fn is_none(self) -> filter::Condition<bool> {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::IsNull,
    )
  }

  /// Generate a test on Not Null
  pub fn is_some(self) -> filter::Condition<bool> {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::IsNotNull,
    )
  }

  /// Generate an 'equal' condition
  pub fn eq<T>(self, t: T) -> filter::Condition<T> 
  where filter::Value<T>: std::convert::From<T>,
        T: std::fmt::Display,
  {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::Equal(t.into()),
    )
  }

  /// Generate a 'not-equal' condition
  pub fn ne<T>(self, t: T) -> filter::Condition<T> 
  where filter::Value<T>: std::convert::From<T>,
        T: std::fmt::Display,
  {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::NotEqual(t.into()),
    )
  }

  /// Generate a 'greater than' condition
  pub fn gt<T>(self, t: T) -> filter::Condition<T>
  where filter::Value<T>: std::convert::From<T>,
        T: std::fmt::Display
  {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::GreaterThan(t.into()),
    )
  }

  /// Generate a 'greater equal' condition
  pub fn ge<T>(self, t: T) -> filter::Condition<T>
  where filter::Value<T>: std::convert::From<T>,
        T: std::fmt::Display
  {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::GreaterEqual(t.into()),
    )
  }

  /// Generate a 'lower than' condition
  pub fn lt<T>(self, t: T) -> filter::Condition<T>
  where filter::Value<T>: std::convert::From<T>,
        T: std::fmt::Display
  {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::LowerThan(t.into()),
    )
  }

  /// Generate a 'lower equal' condition
  pub fn le<T>(self, t: T) -> filter::Condition<T>
  where filter::Value<T>: std::convert::From<T>,
        T: std::fmt::Display
  {
    filter::Condition::from_label_operator(
      self.label,
      filter::Operator::LowerEqual(t.into()),
    )
  }

  /// Generate an ascending order clause condition
  pub fn ascending(self) -> order::Condition
  {
    order::Condition::from_label_operator(
      self.label,
      order::Operator::Ascending,
    )
  }

  /// Generate a descending order clause condition
  pub fn descending(self) -> order::Condition
  {
    order::Condition::from_label_operator(
      self.label,
      order::Operator::Descending,
    )
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn it_display_correct_clause_for_null_not_null_tests() -> Result<()> {
    use traits::Filter;
    assert!(Field::from("key").is_some().filter().eq("`key` IS NOT NULL"));
    assert!(Field::from("key").is_none().filter().eq("`key` IS NULL"));
    Ok(())
  }

  #[test]
  fn it_display_correct_clause_for_u32() -> Result<()> {
    use traits::Filter;

    assert!(Field::from("key").eq(1u32).filter().eq("`key` = 1"));
    assert!(Field::from("key").ne(1u32).filter().eq("`key` != 1"));
    assert!(Field::from("key").gt(2u32).filter().eq("`key` > 2"));
    assert!(Field::from("key").ge(2u32).filter().eq("`key` >= 2"));
    assert!(Field::from("key").lt(2u32).filter().eq("`key` < 2"));
    assert!(Field::from("key").le(2u32).filter().eq("`key` <= 2"));

    Ok(())
  }

  #[test]
  fn it_display_correct_clause_for_string() -> Result<()> {
    use traits::Filter;

    assert!(Field::from("key_str").eq("val").filter().eq("`key_str` = 'val'"));
    assert!(Field::from("key_str").ne("val").filter().eq("`key_str` != 'val'"));
    assert!(Field::from("key_str").gt("val").filter().eq("`key_str` > 'val'"));
    assert!(Field::from("key_str").ge("val").filter().eq("`key_str` >= 'val'"));
    assert!(Field::from("key_str").lt("val").filter().eq("`key_str` < 'val'"));
    assert!(Field::from("key_str").le("val").filter().eq("`key_str` <= 'val'"));

    Ok(())
  }

  #[test]
  fn it_display_correct_order_clause() -> Result<()> {
    use traits::Order;

    assert!(Field::from("order").ascending().as_order_clause().eq("`order` ASC"));
    assert!(Field::from("order").descending().as_order_clause().eq("`order` DESC"));

    Ok(())
  }
}