Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some multirange operations support #4261

Merged
merged 12 commits into from
Oct 24, 2024
39 changes: 29 additions & 10 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 @@ -836,6 +836,7 @@ pub trait PgRangeExpressionMethods: Expression + Sized {
/// # posts {
/// # id -> Integer,
/// # versions -> Range<Integer>,
/// # age -> Multirange<Integer>,
/// # }
/// # }
/// #
Expand All @@ -848,10 +849,10 @@ pub trait PgRangeExpressionMethods: Expression + Sized {
/// # use std::collections::Bound;
/// # let conn = &mut establish_connection();
/// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap();
/// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, versions INT4RANGE NOT NULL)").execute(conn).unwrap();
/// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, versions INT4RANGE NOT NULL, age INT4MULTIRANGE)").execute(conn).unwrap();
/// #
/// diesel::insert_into(posts)
/// .values(versions.eq((Bound::Included(5), Bound::Unbounded)))
/// .values((versions.eq(5..), age.eq(vec![1..18, 60..99])))
/// .execute(conn)?;
///
/// let cool_posts = posts.select(id)
Expand All @@ -863,14 +864,24 @@ pub trait PgRangeExpressionMethods: Expression + Sized {
/// .filter(versions.contains(1))
/// .load::<i32>(conn)?;
/// assert!(amazing_posts.is_empty());
///
/// let cool_posts = posts.select(id)
/// .filter(age.contains(10))
/// .load::<i32>(conn)?;
/// assert_eq!(vec![1], cool_posts);
///
/// let amazing_posts = posts.select(id)
/// .filter(age.contains(20))
/// .load::<i32>(conn)?;
/// assert!(amazing_posts.is_empty());
/// # Ok(())
/// # }
/// ```
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 @@ -1779,7 +1790,7 @@ pub trait PgRangeExpressionMethods: Expression + Sized {
impl<T> PgRangeExpressionMethods for T
where
T: Expression,
T::SqlType: RangeOrNullableRange,
T::SqlType: MultirangeOrRangeMaybeNullable,
guissalustiano marked this conversation as resolved.
Show resolved Hide resolved
{
}

Expand Down Expand Up @@ -3466,12 +3477,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
Loading