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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/// A Toql field can select, filter and order a database column or expression
/// A field can be created from a field name and filtered, sorted with its methods.
/// However the Toql derive creates fields structs for a derived struct, so instead of
/// ``` ignore
///  
///  let f = Field::from("id");
/// ```
/// its easier and recommended to write
/// ``` ignore
///  let f = User::fields().id();
/// ```
use super::concatenation::Concatenation;
use super::field_filter::FieldFilter;
use super::field_order::FieldOrder;
use crate::sql_arg::SqlArg;
//use heck::MixedCase;

#[derive(Clone, Debug)]
pub struct Field {
    pub(crate) concatenation: Concatenation,
    pub(crate) name: String,
    pub(crate) hidden: bool,
    pub(crate) order: Option<FieldOrder>,
    pub(crate) filter: Option<FieldFilter>,
    //  pub(crate) aggregation: bool,
}

impl Field {
    /// Create a field for the given name.
    pub fn from<T>(name: T) -> Self
    where
        T: Into<String>,
    {
        let name = name.into();
        #[cfg(debug_assertions)]
        {
            // Ensure name does not end with wildcard
            if !name.chars().all(|x| x.is_alphanumeric() || x == '_') {
                panic!(
                    "Field {:?} must only contain alphanumeric characters and underscores.",
                    name
                );
            }
        }

        Field {
            concatenation: Concatenation::And,
            name,
            hidden: false,
            order: None,
            filter: None,
        }
    }
    /// Hide field. Useful if a field should not be selected, but be used for filtering.
    pub fn hide(mut self) -> Self {
        self.hidden = true;
        self
    }
    /// Use this field to order records in ascending way. Give ordering priority when records are ordered by multiple fields.
    pub fn asc(mut self, order: u8) -> Self {
        self.order = Some(FieldOrder::Asc(order));
        self
    }
    /// Use this field to order records in descending way. Give ordering priority when records are ordered by multiple fields.
    pub fn desc(mut self, order: u8) -> Self {
        self.order = Some(FieldOrder::Desc(order));
        self
    }
    /// Filter records with _equal_ predicate.
    pub fn eq(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Eq(criteria.into()));
        self
    }
    /// Filter records with _equal null_ predicate.
    pub fn eqn(mut self) -> Self {
        self.filter = Some(FieldFilter::Eqn);
        self
    }
    /// Filter records with _not equal_ predicate.
    pub fn ne(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Ne(criteria.into()));
        self
    }
    /// Filter records with _not equal null_ predicate.
    pub fn nen(mut self) -> Self {
        self.filter = Some(FieldFilter::Nen);
        self
    }
    /// Filter records with greater that_ predicate.
    pub fn gt(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Gt(criteria.into()));
        self
    }
    /// Filter records with greater or equal_ predicate.
    pub fn ge(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Ge(criteria.into()));
        self
    }
    /// Filter records with lesser than_ predicate.
    pub fn lt(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Lt(criteria.into()));
        self
    }
    /// Filter records with lesser or equal_ predicate.
    pub fn le(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Le(criteria.into()));
        self
    }
    /// Filter records with _between_ predicate. This is inclusive, so `x bw 3 6` is the same as `x ge 3, x le 6`
    pub fn bw(mut self, lower: impl Into<SqlArg>, upper: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Bw(lower.into(), upper.into()));
        self
    }
    /// Filter records with _like_ predicate.
    pub fn lk(mut self, criteria: impl Into<SqlArg>) -> Self {
        self.filter = Some(FieldFilter::Lk(criteria.into()));
        self
    }
    /// Filter records with _inside_ predicate.
    pub fn ins<T, I>(mut self, criteria: I) -> Self
    where
        T: Into<SqlArg>,
        I: IntoIterator<Item = T>,
    {
        self.filter = Some(FieldFilter::In(
            criteria.into_iter().map(|c| c.into()).collect(),
        ));
        self
    }
    /// Filter records with _outside_ predicate.
    pub fn out<T, I>(mut self, criteria: I) -> Self
    where
        T: Into<SqlArg>,
        I: IntoIterator<Item = T>,
    {
        self.filter = Some(FieldFilter::Out(
            criteria.into_iter().map(|c| c.into()).collect(),
        ));
        self
    }
    /// Filter records with custom function.
    /// To provide a custom function you must implement (FieldHandler)[../table_mapper/trait.FieldHandler.html]
    /// See _custom handler test_ for an example.
    pub fn fnc<U, T, I>(mut self, name: U, args: I) -> Self
    where
        U: Into<String>,
        T: Into<SqlArg>,
        I: IntoIterator<Item = T>,
    {
        self.filter = Some(FieldFilter::Fn(
            name.into(),
            args.into_iter().map(|c| c.into()).collect(),
        ));
        self
    }

    pub fn concatenate(mut self, concatenation: Concatenation) -> Self {
        self.concatenation = concatenation;
        self
    }

    pub fn into_name(self) -> String {
        self.name
    }
}

impl ToString for Field {
    fn to_string(&self) -> String {
        let mut s = String::new();
        match self.order {
            Some(FieldOrder::Asc(o)) => {
                s.push('+');
                s.push_str(&o.to_string());
            }
            Some(FieldOrder::Desc(o)) => {
                s.push('-');
                s.push_str(&o.to_string());
            }
            None => {}
        };
        if self.hidden {
            s.push('.');
        }
        s.push_str(&self.name);

        if self.filter.is_some() {
            s.push(' ');
        }
        match self.filter {
            None => {}
            Some(ref filter) => {
                s.push_str(filter.to_string().as_str());
            }
        }
        s
    }
}

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

    #[test]
    fn build() {
        assert_eq!(Field::from("prop").eq(true).to_string(), "prop EQ 1");
        assert_eq!(Field::from("prop").eqn().to_string(), "prop EQN");
        assert_eq!(Field::from("prop").ne(1).to_string(), "prop NE 1");
        assert_eq!(Field::from("prop").nen().to_string(), "prop NEN");
        assert_eq!(Field::from("prop").gt(1).to_string(), "prop GT 1");
        assert_eq!(Field::from("prop").ge(1.5).to_string(), "prop GE 1.5");
        assert_eq!(Field::from("prop").lt(1.5).to_string(), "prop LT 1.5");
        assert_eq!(Field::from("prop").le(1).to_string(), "prop LE 1");
        assert_eq!(
            Field::from("prop").lk("%ABC%").to_string(),
            "prop LK '%ABC%'"
        );
        assert_eq!(Field::from("prop").bw(1, 10).to_string(), "prop BW 1 10");
        assert_eq!(
            Field::from("prop").ins(vec![1, 10]).to_string(),
            "prop IN 1 10"
        );
        assert_eq!(
            Field::from("prop").out(vec![1, 10]).to_string(),
            "prop OUT 1 10"
        );
        assert_eq!(
            Field::from("prop").fnc("SC", vec![1, 10]).to_string(),
            "prop FN SC 1 10"
        );

        assert_eq!(Field::from("prop").asc(1).to_string(), "+1prop");
        assert_eq!(Field::from("prop").desc(3).to_string(), "-3prop");
        assert_eq!(Field::from("prop").hide().to_string(), ".prop");

        // Combination
        assert_eq!(
            Field::from("level3_prop").eq(10).hide().asc(4).to_string(),
            "+4.level3_prop EQ 10"
        );
    }

    #[test]
    fn into_name() {
        assert_eq!(
            Field::from("level3_prop").eq(10).hide().asc(4).into_name(),
            "level3_prop"
        );
    }

    #[test]
    #[should_panic]
    fn invalid_name() {
        Field::from("level%2");
    }
}