diesel::expression_methods

Trait PgRangeExpressionMethods

Source
pub trait PgRangeExpressionMethods: Expression + Sized {
    // Provided methods
    fn contains<T>(self, other: T) -> RangeContains<Self, T>
       where Self::SqlType: RangeOrMultirange,
             <Self::SqlType as RangeOrMultirange>::Inner: SqlType + TypedExpressionType,
             T: AsExpression<<Self::SqlType as RangeOrMultirange>::Inner> { ... }
    fn contains_range<T>(self, other: T) -> ContainsRange<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn is_contained_by<T>(self, other: T) -> IsContainedBy<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn overlaps_with<T>(self, other: T) -> OverlapsWith<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn range_extends_right_to<T>(self, other: T) -> RangeExtendsRightTo<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn range_extends_left_to<T>(self, other: T) -> RangeExtendsLeftTo<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn lesser_than<T>(self, other: T) -> LesserThanRange<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn greater_than<T>(self, other: T) -> GreaterThanRange<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn range_adjacent<T>(self, other: T) -> RangeAdjacent<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn union_range<T>(self, other: T) -> UnionRange<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn difference_range<T>(self, other: T) -> Difference<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn intersection_range<T>(self, other: T) -> Intersection<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
}
Available on crate feature postgres_backend only.
Expand description

PostgreSQL specific methods present on range expressions.

Provided Methods§

Source

fn contains<T>(self, other: T) -> RangeContains<Self, T>
where Self::SqlType: RangeOrMultirange, <Self::SqlType as RangeOrMultirange>::Inner: SqlType + TypedExpressionType, T: AsExpression<<Self::SqlType as RangeOrMultirange>::Inner>,

Creates a PostgreSQL @> expression.

This operator returns true whether a range contains an specific element

This operator evaluates to true for the following cases:

self:   [-----]
other:    |
self:  [----]
other: |
self:  [----]
other:      |

This operator evaluates to false for the following cases:

self:   [-----]
other:          |
self:    [----]
other: |
self:  [----)
other:      |
§Example
assert!(diesel::select((1..5).into_sql::<Range<Integer>>().contains(4)).first::<bool>(conn).unwrap());
assert!(!diesel::select((1..5).into_sql::<Range<Integer>>().contains(8)).first::<bool>(conn).unwrap());

assert!(diesel::select((vec![1..5]).into_sql::<Multirange<Integer>>().contains(4)).first::<bool>(conn).unwrap());
assert!(!diesel::select((vec![1..5]).into_sql::<Multirange<Integer>>().contains(8)).first::<bool>(conn).unwrap());
Source

fn contains_range<T>(self, other: T) -> ContainsRange<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL @> expression.

This operator returns true whether a range contains another range

This operator evaluates to true for the following cases:

self:   [-------]
other:     [--]
self:  [------]
other: [------]

This operator evaluates to false for the following cases:

self:   [-----]
other:     [----]
self:    [----]
other: [--------]
self:  [----)
other: [----]
§Example
assert!(diesel::select(
    (1..5).into_sql::<Range<Integer>>().contains_range(1..5)
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    (1..5).into_sql::<Range<Integer>>().contains_range(3..7)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..5].into_sql::<Multirange<Integer>>().contains_range(vec![1..5])
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    vec![1..5].into_sql::<Multirange<Integer>>().contains_range(vec![3..7])
    ).first::<bool>(conn).unwrap());
Source

fn is_contained_by<T>(self, other: T) -> IsContainedBy<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL <@ expression.

This operator returns true whether a range is contained by another range

This operator evaluates to true for the following cases:

self:   [-----]
other: [-------]
self:  [----]
other: [----]

This operator evaluates to false for the following cases:

self:   [------]
other:   [---]
self:   [----]
other: [-----)
self:  [----]
other:   [----]
§Example
assert!(diesel::select(
    (1..5).into_sql::<Range<Integer>>().is_contained_by(1..5)
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    (1..5).into_sql::<Range<Integer>>().is_contained_by(3..7)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..5].into_sql::<Multirange<Integer>>().is_contained_by(vec![1..5])
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    vec![1..5].into_sql::<Multirange<Integer>>().is_contained_by(vec![3..7])
    ).first::<bool>(conn).unwrap());
Source

fn overlaps_with<T>(self, other: T) -> OverlapsWith<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL && expression.

This operator returns true whether two ranges overlap.

This operator evaluates to true for the following cases:

self:   [-----]
other:    [-----]
self:     [----]
other: [----]
self:     [----]
other:  [-------]
self:   [----]
other:  [----]
self:   [----]
other:  [----)

This operator evaluates to false for the following cases:

self:   [-----]
other:          [-----]
self:       [----]
other: [--]
§Example
assert!(diesel::select(
    (1..5).into_sql::<Range<Integer>>().overlaps_with(3..7)
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    (1..5).into_sql::<Range<Integer>>().overlaps_with(10..15)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..5].into_sql::<Multirange<Integer>>().overlaps_with(vec![3..7])
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    vec![1..5].into_sql::<Multirange<Integer>>().overlaps_with(vec![10..15])
    ).first::<bool>(conn).unwrap());
Source

fn range_extends_right_to<T>(self, other: T) -> RangeExtendsRightTo<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL &< expression.

This operator returns true whether the argument range extend to the right of the current range

Postgresql defines “extends” as does not have a lower bound smaller than the lower bound of the self range. That means the right hand side range can overlap parts of the left hand side range or be on the right side of the left hand side range

The following constelations evaluate to true:

self:   [------)
other:    [----)
self:  [----)
other:         [----)
self:  [------)
other:    [------)

The following constelations evaluate to false:

self:             [------]
other:    [----]
self:            [------]
other:         [----]
self:  [------]
other: [------)
§Example
assert!(diesel::select(
    (1..20).into_sql::<Range<Integer>>().range_extends_right_to(18..20)
    ).first::<bool>(conn).unwrap());
assert!(diesel::select(
    (1..20).into_sql::<Range<Integer>>().range_extends_right_to(25..30)
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    (1..20).into_sql::<Range<Integer>>().range_extends_right_to(-10..0)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..20].into_sql::<Multirange<Integer>>().range_extends_right_to(vec![18..20])
    ).first::<bool>(conn).unwrap());
assert!(diesel::select(
    vec![1..20].into_sql::<Multirange<Integer>>().range_extends_right_to(vec![25..30])
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    vec![1..20].into_sql::<Multirange<Integer>>().range_extends_right_to(vec![-10..0])
    ).first::<bool>(conn).unwrap());
Source

fn range_extends_left_to<T>(self, other: T) -> RangeExtendsLeftTo<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL &> expression.

This operator returns true whether a range does extend to the left of another

Postgresql defines “extends” as does not have a upper bound greater than the upper bound of the self range. That means the right hand side range can overlap parts of the left hand side range or be on the left side of the left hand side range

The following constelations evaluate to true:

self:        [------)
other:    [----)
self:          [----)
other: [----)
self:  [------)
other:        [------)

The following constelations evaluate to false:

self:   [--------]
other:    [----]
self:  [------]
other:          [----]
self:  [------]
other: (------]
§Example
assert!(diesel::select(
    (1..20).into_sql::<Range<Integer>>().range_extends_left_to(-10..5)
    ).first::<bool>(conn).unwrap());
assert!(diesel::select(
    (1..20).into_sql::<Range<Integer>>().range_extends_left_to(-10..-5)
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    (1..20).into_sql::<Range<Integer>>().range_extends_left_to(25..30)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..20].into_sql::<Multirange<Integer>>().range_extends_left_to(vec![-10..5])
    ).first::<bool>(conn).unwrap());
assert!(diesel::select(
    vec![1..20].into_sql::<Multirange<Integer>>().range_extends_left_to(vec![-10..-5])
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    vec![1..20].into_sql::<Multirange<Integer>>().range_extends_left_to(vec![25..30])
    ).first::<bool>(conn).unwrap());
Source

fn lesser_than<T>(self, other: T) -> LesserThanRange<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL << expression.

Is the first range strictly left of the second?

The following constelations evaluate to true:

self:   [------)
other:            [----)
self:  [----)
other:      [----)

The following constelations evaluate to false:

self:             [------]
other:    [----]
self:     [------]
other:         [----]
self:  [------]
other:        [------)
§Example
diesel::insert_into(posts)
    .values(&vec![
        (versions.eq((Bound::Included(1), Bound::Included(2)))),
        (versions.eq((Bound::Included(3), Bound::Included(4)))),
        (versions.eq((Bound::Included(5), Bound::Included(6))))
    ])
    .execute(conn)?;

let data = posts.select(id)
    .filter(versions.lesser_than((Bound::Included(1), Bound::Included(4))))
    .load::<i32>(conn)?;
assert!(data.is_empty());

let data = posts.select(id)
    .filter(versions.lesser_than((Bound::Included(5), Bound::Included(8))))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], data);
Source

fn greater_than<T>(self, other: T) -> GreaterThanRange<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL >> expression.

Is the first range strictly right of the second?

The following constelations evaluate to true:

self:          [------)
other: [----)
self:        [----)
other:  [----)

The following constelations evaluate to false:

self:  [------]
other:          [----]
self:     [------]
other:         [----]
self:         [------]
other: [------]
§Example
diesel::insert_into(posts)
    .values(&vec![
        (versions.eq((Bound::Included(1), Bound::Included(2)))),
        (versions.eq((Bound::Included(3), Bound::Included(4)))),
        (versions.eq((Bound::Included(5), Bound::Included(6))))
    ])
    .execute(conn)?;

let data = posts.select(id)
    .filter(versions.greater_than((Bound::Included(1), Bound::Included(2))))
    .load::<i32>(conn)?;
assert_eq!(vec![2, 3], data);

let data = posts.select(id)
    .filter(versions.greater_than((Bound::Included(5), Bound::Included(8))))
    .load::<i32>(conn)?;
assert!(data.is_empty());
Source

fn range_adjacent<T>(self, other: T) -> RangeAdjacent<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL -|- expression.

This operator evaluates to true if the two ranges are adjacent

The following constelations evaluate to true:

self:   [------)
other:         [----)
self:       [----)
other: [----)
self:        [----)
other: [----]
self:  [----]
other:       [----]

The following constelations evaluate to false:

self:        [------]
other:    [----]
self:     [------]
other:         [----]
self:  [------]
other:        [------]
self:  [------]
other:           [------]
§Example
assert!(diesel::select(
    (1..2).into_sql::<Range<Integer>>().range_adjacent(2..=6)
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    (4..7).into_sql::<Range<Integer>>().range_adjacent(2..=6)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..2].into_sql::<Multirange<Integer>>().range_adjacent(vec![2..=6])
    ).first::<bool>(conn).unwrap());
assert!(!diesel::select(
    vec![4..7].into_sql::<Multirange<Integer>>().range_adjacent(vec![2..=6])
    ).first::<bool>(conn).unwrap());
Source

fn union_range<T>(self, other: T) -> UnionRange<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL + expression.

This operator unions two ranges and returns the union.

self:   [------)
other:      [----)
result: [--------)
self:          [----)
other: [----)
result: error
self:      [----)
other: [----]
result [--------)
§Example
assert!(diesel::select(
    (1..=2).into_sql::<Range<Integer>>().union_range(2..=6).eq(1..=6)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..=2].into_sql::<Multirange<Integer>>().union_range(vec![1..=6]).eq(vec![1..=6])
    ).first::<bool>(conn).unwrap());
Source

fn difference_range<T>(self, other: T) -> Difference<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL - expression.

This operator takes two ranges and returns the difference.

The second range must not be contained in the first in such a way that the difference would not be a single range.

self:   [------)
other:      [----)
result: [---)
self:      [----)
other:  [----)
result:      [--)
self:      [--------)
other:       [----]
result: error
§Example
assert!(diesel::select(
    (1..=8).into_sql::<Range<Integer>>().difference_range(3..=8).eq(1..3)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..=8].into_sql::<Multirange<Integer>>().difference_range(vec![3..=8]).eq(vec![1..3])
    ).first::<bool>(conn).unwrap());
Source

fn intersection_range<T>(self, other: T) -> Intersection<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL * expression.

This operator takes two ranges and returns the intersection.

self:   [------)
other:      [----)
result:     [--)
self:      [----)
other:  [----)
result:    [-)
self:    [--------)
other:     [----]
result:    [----]
self:    [--------)
other:               [----]
result: empty range
§Example
assert!(diesel::select(
    (1..=8).into_sql::<Range<Integer>>().intersection_range(3..=8).eq(3..=8)
    ).first::<bool>(conn).unwrap());

assert!(diesel::select(
    vec![1..=8].into_sql::<Multirange<Integer>>().intersection_range(vec![3..=8]).eq(vec![3..=8])
    ).first::<bool>(conn).unwrap());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> PgRangeExpressionMethods for T
where T: Expression, T::SqlType: MultirangeOrRangeMaybeNullable,