1
use canyon_connection::canyon_database_connector::DatabaseType;
2

            
3
pub trait Operator {
4
    fn as_str(&self, placeholder_counter: usize, datasource_type: &DatabaseType) -> String;
5
}
6

            
7
/// Enumerated type for represent the comparison operations
8
/// in SQL sentences
9
pub enum Comp {
10
    /// Operator "=" equals
11
    Eq,
12
    /// Operator "!=" not equals
13
    Neq,
14
    /// Operator ">" greater than value
15
    Gt,
16
    /// Operator ">=" greater or equals than value
17
    GtEq,
18
    /// Operator "<" less than value
19
    Lt,
20
    /// Operator "=<" less or equals than value
21
    LtEq,
22
}
23

            
24
impl Operator for Comp {
25
34
    fn as_str(&self, placeholder_counter: usize, _datasource_type: &DatabaseType) -> String {
26
34
        match *self {
27
10
            Self::Eq => format!(" = ${placeholder_counter}"),
28
            Self::Neq => format!(" <> ${placeholder_counter}"),
29
12
            Self::Gt => format!(" > ${placeholder_counter}"),
30
            Self::GtEq => format!(" >= ${placeholder_counter}"),
31
7
            Self::Lt => format!(" < ${placeholder_counter}"),
32
5
            Self::LtEq => format!(" <= ${placeholder_counter}"),
33
        }
34
34
    }
35
}
36

            
37
pub enum Like {
38
    /// Operator "LIKE"  as '%pattern%'
39
    Full,
40
    /// Operator "LIKE"  as '%pattern'
41
    Left,
42
    /// Operator "LIKE"  as 'pattern%'
43
    Right,
44
}
45

            
46
impl Operator for Like {
47
9
    fn as_str(&self, placeholder_counter: usize, datasource_type: &DatabaseType) -> String {
48
9
        let type_data_to_cast_str = match datasource_type {
49
            #[cfg(feature = "postgres")]
50
4
            DatabaseType::PostgreSql => "VARCHAR",
51
            #[cfg(feature = "mssql")]
52
2
            DatabaseType::SqlServer => "VARCHAR",
53
            #[cfg(feature = "mysql")]
54
3
            DatabaseType::MySQL => "CHAR",
55
        };
56

            
57
9
        match *self {
58
            Like::Full => {
59
3
                format!(" LIKE CONCAT('%', CAST(${placeholder_counter} AS {type_data_to_cast_str}) ,'%')")
60
            }
61
3
            Like::Left => format!(
62
                " LIKE CONCAT('%', CAST(${placeholder_counter} AS {type_data_to_cast_str}))"
63
            ),
64
3
            Like::Right => format!(
65
                " LIKE CONCAT(CAST(${placeholder_counter} AS {type_data_to_cast_str}) ,'%')"
66
            ),
67
        }
68
9
    }
69
}