Skip to content

Commit

Permalink
add multirange contains support
Browse files Browse the repository at this point in the history
  • Loading branch information
guissalustiano committed Sep 13, 2024
1 parent 97898ec commit 0fda44e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(in crate::pg) use self::private::{
ArrayOrNullableArray, InetOrCidr, JsonIndex, JsonOrNullableJson,
JsonOrNullableJsonOrJsonbOrNullableJsonb, JsonRemoveIndex, JsonbOrNullableJsonb,
MaybeNullableValue, MultirangeOrNullableMultirange, MultirangeOrRangeMaybeNullable,
RangeHelper, RangeOrNullableRange, TextArrayOrNullableTextArray, TextOrNullableText,
RangeOrMultirange, RangeOrNullableRange, TextArrayOrNullableTextArray, TextOrNullableText,
};
use super::date_and_time::{AtTimeZone, DateTimeLike};
use super::operators::*;
Expand Down Expand Up @@ -868,9 +868,9 @@ pub trait PgRangeExpressionMethods: Expression + Sized {
/// ```
fn contains<T>(self, other: T) -> dsl::RangeContains<Self, T>
where
Self::SqlType: RangeHelper,
<Self::SqlType as RangeHelper>::Inner: SqlType + TypedExpressionType,
T: AsExpression<<Self::SqlType as RangeHelper>::Inner>,
Self::SqlType: RangeOrMultirange,
<Self::SqlType as RangeOrMultirange>::Inner: SqlType + TypedExpressionType,
T: AsExpression<<Self::SqlType as RangeOrMultirange>::Inner>,
{
Grouped(Contains::new(self, other.as_expression()))
}
Expand Down Expand Up @@ -3466,12 +3466,20 @@ pub(in crate::pg) mod private {
impl TextOrNullableText for Nullable<Text> {}

/// Marker trait used to extract the inner type
/// of our `Range<T>` sql type, used to implement `PgRangeExpressionMethods`
pub trait RangeHelper: SqlType + SingleValue {
/// of our `Range<T>` and `Multirange<T>` sql type, used to implement `PgRangeExpressionMethods`
pub trait RangeOrMultirange: SqlType + SingleValue {
type Inner: SingleValue;
}

impl<ST> RangeHelper for Range<ST>
impl<ST> RangeOrMultirange for Range<ST>
where
Self: 'static,
ST: SingleValue,
{
type Inner = ST;
}

impl<ST> RangeOrMultirange for Multirange<ST>
where
Self: 'static,
ST: SingleValue,
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub type ArrayContains<Lhs, Rhs> = Contains<Lhs, Rhs>;
pub type RangeContains<Lhs, Rhs> = Grouped<
super::operators::Contains<
Lhs,
AsExprOf<Rhs, <SqlTypeOf<Lhs> as super::expression_methods::RangeHelper>::Inner>,
AsExprOf<Rhs, <SqlTypeOf<Lhs> as super::expression_methods::RangeOrMultirange>::Inner>,
>,
>;

Expand Down

0 comments on commit 0fda44e

Please sign in to comment.