use diesel::expression::{AsExpression, Expression};
use diesel::sql_types::{Array, Nullable, Text};
use super::dsl::array_to_string;
use super::operators::*;
pub trait TrgmTextType {}
impl TrgmTextType for Text {}
impl TrgmTextType for Nullable<Text> {}
pub trait TrgmExpressionMethods: Expression + Sized
where
Self::SqlType: TrgmTextType,
{
fn trgm_similar_to<T>(self, other: T) -> Similar<Self, T::Expression>
where
T: AsExpression<Text>,
{
Similar::new(self, other.as_expression())
}
fn trgm_word_similar_to<T>(self, other: T) -> WordSimilarRight<Self, T::Expression>
where
T: AsExpression<Text>,
{
WordSimilarRight::new(self, other.as_expression())
}
fn trgm_strict_word_similar_to<T>(self, other: T) -> StrictWordSimilarRight<Self, T::Expression>
where
T: AsExpression<Text>,
{
StrictWordSimilarRight::new(self, other.as_expression())
}
fn trgm_distance<T>(self, other: T) -> Distance<Self, T::Expression>
where
T: AsExpression<Text>,
{
Distance::new(self, other.as_expression())
}
fn trgm_word_distance<T>(self, other: T) -> WordDistanceRight<Self, T::Expression>
where
T: AsExpression<Text>,
{
WordDistanceRight::new(self, other.as_expression())
}
fn trgm_strict_word_distance<T>(self, other: T) -> StrictWordDistanceRight<Self, T::Expression>
where
T: AsExpression<Text>,
{
StrictWordDistanceRight::new(self, other.as_expression())
}
}
impl<T> TrgmExpressionMethods for T
where
T: Expression,
T::SqlType: TrgmTextType,
{
}
type ArrayToStringExpr<T> =
super::dsl::array_to_string<T, <&'static str as AsExpression<Text>>::Expression>;
pub trait TrgmArrayExpressionMethods: Expression + Sized
where
Self::SqlType: TrgmTextType,
{
fn trgm_similar_to_array<T>(self, other: T) -> Similar<Self, ArrayToStringExpr<T::Expression>>
where
T: AsExpression<Array<Text>>,
{
Similar::new(self, array_to_string(other.as_expression(), " "))
}
fn trgm_distance_to_array<T>(self, other: T) -> Distance<Self, ArrayToStringExpr<T::Expression>>
where
T: AsExpression<Array<Text>>,
{
Distance::new(self, array_to_string(other.as_expression(), " "))
}
}
impl<T> TrgmArrayExpressionMethods for T
where
T: Expression,
T::SqlType: TrgmTextType,
{
}