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
#[macro_use]
extern crate diesel;

mod functions {
    use diesel::sql_types::*;

    sql_function! {
        fn similarity<T: IntoNullable>(x: T, y: T) -> Float;
    }

    sql_function! {
        fn word_similarity<T: IntoNullable>(x: T, y: T) -> Float;
    }
}

mod dsl {
    use diesel::expression::{AsExpression, Expression};

    mod predicates {
        use diesel::pg::Pg;
        use diesel::sql_types::*;

        diesel_infix_operator!(Similar, " % ", Bool, backend: Pg);
        diesel_infix_operator!(WordSimilar, " <% ", Bool, backend: Pg);
        diesel_infix_operator!(WordSimilarComm, " %> ", Bool, backend: Pg);
        diesel_infix_operator!(Distance, " <-> ", Float, backend: Pg);
        diesel_infix_operator!(WordDistance, " <<-> ", Float, backend: Pg);
        diesel_infix_operator!(WordDistanceComm, " <->> ", Float, backend: Pg);
    }

    use self::predicates::*;

    pub trait TrgmExtensions: Expression + Sized {
        fn similar<T: AsExpression<Self::SqlType>>(self, other: T) -> Similar<Self, T::Expression> {
            Similar::new(self, other.as_expression())
        }
    }

    impl<T: Expression> TrgmExtensions for T {}
}

pub use self::dsl::*;
pub use self::functions::*;