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
use super::{destructure_query_filter, QueryTerm, CAST_COLUMN};

pub struct ColumnFilter {
    column: String,
    query: String,
}

impl From<(&str, &str)> for ColumnFilter {
    fn from(tp: (&str, &str)) -> Self {
        Self {
            column: tp.0.to_string(),
            query: tp.1.to_string(),
        }
    }
}

// TODO: figure out how to not repeat ourselves here
impl From<(&str, &String)> for ColumnFilter {
    fn from(tp: (&str, &String)) -> Self {
        Self {
            column: tp.0.to_string(),
            query: tp.1.to_string(),
        }
    }
}

impl ColumnFilter {
    pub fn sql_fragments(&self, matched_before: bool) -> Vec<String> {
        let column = if self.column == CAST_COLUMN {
            "`cast`"
        } else {
            &self.column
        };
        let mut first = !matched_before;
        let terms = destructure_query_filter(&self.query);
        let filters: Vec<String> = terms
            .iter()
            .map(|term| match term {
                QueryTerm::Not(term) => {
                    let and = if first { "" } else { " AND" };
                    first = false;
                    format!("\n {} NOT {} LIKE '%{}%'", and, column, term)
                }
                QueryTerm::And(term) => {
                    let and = if first { "" } else { " AND" };
                    first = false;
                    format!("\n {} {} LIKE '%{}%'", and, column, term)
                }
            })
            .collect();
        filters
    }
}