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: RangeHelper,
             <Self::SqlType as RangeHelper>::Inner: SqlType + TypedExpressionType,
             T: AsExpression<<Self::SqlType as RangeHelper>::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: RangeHelper, <Self::SqlType as RangeHelper>::Inner: SqlType + TypedExpressionType, T: AsExpression<<Self::SqlType as RangeHelper>::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
diesel::insert_into(posts)
    .values(versions.eq((Bound::Included(5), Bound::Unbounded)))
    .execute(conn)?;

let cool_posts = posts.select(id)
    .filter(versions.contains(42))
    .load::<i32>(conn)?;
assert_eq!(vec![1], cool_posts);

let amazing_posts = posts.select(id)
    .filter(versions.contains(1))
    .load::<i32>(conn)?;
assert!(amazing_posts.is_empty());
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
diesel::insert_into(posts)
    .values(versions.eq((Bound::Included(5), Bound::Unbounded)))
    .execute(conn)?;

let cool_posts = posts.select(id)
    .filter(versions.contains_range((Bound::Included(10), Bound::Included(50))))
    .load::<i32>(conn)?;
assert_eq!(vec![1], cool_posts);

let amazing_posts = posts.select(id)
    .filter(versions.contains_range((Bound::Included(2), Bound::Included(7))))
    .load::<i32>(conn)?;
assert!(amazing_posts.is_empty());
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
diesel::insert_into(posts)
    .values(versions.eq((Bound::Included(5), Bound::Unbounded)))
    .execute(conn)?;

let cool_posts = posts.select(id)
    .filter(versions.is_contained_by((Bound::Included(1), Bound::Unbounded)))
    .load::<i32>(conn)?;
assert_eq!(vec![1], cool_posts);

let amazing_posts = posts.select(id)
    .filter(versions.is_contained_by((Bound::Included(1), Bound::Included(2))))
    .load::<i32>(conn)?;
assert!(amazing_posts.is_empty());
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
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.overlaps_with((Bound::Included(1), Bound::Included(4))))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], data);

let data = posts.select(id)
    .filter(versions.overlaps_with((Bound::Included(7), Bound::Included(8))))
    .load::<i32>(conn)?;
assert!(data.is_empty());
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
diesel::insert_into(posts)
    .values(versions.eq((Bound::Included(1), Bound::Excluded(20))))
    .execute(conn)?;

let cool_posts = posts.select(versions.range_extends_right_to((Bound::Included(18), Bound::Excluded(20))))
    .get_result::<bool>(conn)?;
assert_eq!(true, cool_posts);

let cool_posts = posts.select(versions.range_extends_right_to((Bound::Included(25), Bound::Excluded(30))))
    .get_result::<bool>(conn)?;
assert_eq!(true, cool_posts);

let amazing_posts = posts.select(versions.range_extends_right_to((Bound::Included(-10), Bound::Excluded(0))))
    .get_result::<bool>(conn)?;
assert_eq!(false, amazing_posts);
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
diesel::insert_into(posts)
    .values(versions.eq((Bound::Included(1), Bound::Excluded(20))))
    .execute(conn)?;

let cool_posts = posts.select(versions.range_extends_left_to((Bound::Included(-10), Bound::Excluded(5))))
    .get_result::<bool>(conn)?;
assert_eq!(true, cool_posts);

let cool_posts = posts.select(versions.range_extends_left_to((Bound::Included(-10), Bound::Excluded(-5))))
    .get_result::<bool>(conn)?;
assert_eq!(true, cool_posts);

let amazing_posts = posts.select(versions.range_extends_left_to((Bound::Included(25), Bound::Excluded(30))))
    .get_result::<bool>(conn)?;
assert_eq!(false, amazing_posts);
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
diesel::insert_into(posts)
    .values(&vec![
        (versions.eq((Bound::Included(1), Bound::Excluded(2)))),
        (versions.eq((Bound::Included(4), Bound::Excluded(7))))
    ])
    .execute(conn)?;

let data = posts.select(versions.range_adjacent((Bound::Included(2),Bound::Included(6))))
    .load::<bool>(conn)?;
let expected = vec![true, false];
assert_eq!(expected, data);
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
diesel::insert_into(posts)
    .values(&vec![
        (versions.eq((Bound::Included(1), Bound::Included(2))))
    ])
    .execute(conn)?;

let data = posts.select(versions.union_range((Bound::Included(2),Bound::Included(6))))
    .load::<(Bound<i32>,Bound<i32>)>(conn)?;
let expected_range = (Bound::Included(1),Bound::Excluded(7));
assert_eq!(expected_range, data[0]);
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
diesel::insert_into(posts)
    .values(&vec![
        (versions.eq((Bound::Included(1), Bound::Included(8))))
    ])
    .execute(conn)?;

let data = posts.select(versions.difference_range((Bound::Included(3),Bound::Included(8))))
    .load::<(Bound<i32>,Bound<i32>)>(conn)?;
let expected_range = (Bound::Included(1),Bound::Excluded(3));
assert_eq!(expected_range, data[0]);
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
diesel::insert_into(posts)
    .values(&vec![
        (versions.eq((Bound::Included(1), Bound::Included(8))))
    ])
    .execute(conn)?;

let data = posts.select(versions.intersection_range((Bound::Included(3),Bound::Included(8))))
    .load::<(Bound<i32>,Bound<i32>)>(conn)?;
let expected_range = (Bound::Included(3),Bound::Excluded(9));
assert_eq!(expected_range, data[0]);

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: RangeOrNullableRange,