pub trait Comparator {
type Expression;
fn new(expression: Self::Expression) -> Self;
fn expression(&self) -> &Self::Expression;
fn eq(&self, other: &str) -> String;
fn ne(&self, other: &str) -> String {
format!("NOT ({})", self.eq(other))
}
fn lt(&self, other: &str) -> String;
fn le(&self, other: &str) -> String;
fn gt(&self, other: &str) -> String;
fn ge(&self, other: &str) -> String;
}
pub struct UpperCaseComparator {
expression: String,
}
impl Comparator for UpperCaseComparator {
type Expression = String;
fn new(expression: String) -> Self {
Self { expression }
}
fn expression(&self) -> &String {
&self.expression
}
fn eq(&self, other: &str) -> String {
format!("UPPER({}) = UPPER({})", self.expression, other)
}
fn lt(&self, other: &str) -> String {
format!("UPPER({}) < UPPER({})", self.expression, other)
}
fn le(&self, other: &str) -> String {
format!("UPPER({}) <= UPPER({})", self.expression, other)
}
fn gt(&self, other: &str) -> String {
format!("UPPER({}) > UPPER({})", self.expression, other)
}
fn ge(&self, other: &str) -> String {
format!("UPPER({}) >= UPPER({})", self.expression, other)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_uppercase_comparator_eq() {
let comparator = UpperCaseComparator::new("table.column".to_string());
assert_eq!(
comparator.eq("'value'"),
"UPPER(table.column) = UPPER('value')"
);
}
#[test]
fn test_uppercase_comparator_ne() {
let comparator = UpperCaseComparator::new("table.column".to_string());
assert_eq!(
comparator.ne("'value'"),
"NOT (UPPER(table.column) = UPPER('value'))"
);
}
#[test]
fn test_uppercase_comparator_lt() {
let comparator = UpperCaseComparator::new("table.column".to_string());
assert_eq!(
comparator.lt("'value'"),
"UPPER(table.column) < UPPER('value')"
);
}
}