Skip to main content

PgArrayExpressionMethods

Trait PgArrayExpressionMethods 

Source
pub trait PgArrayExpressionMethods: Expression + Sized {
    // Provided methods
    fn overlaps_with<T>(self, other: T) -> OverlapsWith<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn contains<T>(self, other: T) -> Contains<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 index<T>(self, other: T) -> Index<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Integer> { ... }
    fn index_nullable<T>(self, other: T) -> IndexNullable<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Nullable<Integer>> { ... }
    fn slice<T1, T2>(self, start: T1, end: T2) -> Slice<Self, T1, T2>
       where Self::SqlType: SqlType,
             T1: AsExpression<Integer>,
             T2: AsExpression<Integer> { ... }
    fn slice_nullable<T1, T2>(
        self,
        start: T1,
        end: T2,
    ) -> SliceNullable<Self, T1, T2>
       where Self::SqlType: SqlType,
             T1: AsExpression<Nullable<Integer>>,
             T2: AsExpression<Nullable<Integer>> { ... }
    fn slice_from<T>(self, start: T) -> SliceFrom<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Integer> { ... }
    fn slice_from_nullable<T>(self, start: T) -> SliceFromNullable<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Nullable<Integer>> { ... }
    fn slice_to<T>(self, end: T) -> SliceTo<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Integer> { ... }
    fn slice_to_nullable<T>(self, end: T) -> SliceToNullable<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Nullable<Integer>> { ... }
    fn concat<T>(self, other: T) -> Concat<Self, T>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
}
Available on crate feature postgres_backend only.
Expand description

PostgreSQL specific methods present on array expressions.

Provided Methods§

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 whether two arrays have common elements.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["awesome", "great"]),
        tags.eq(vec!["cool", "great"]),
    ])
    .execute(conn)?;

let data = posts.select(id)
    .filter(tags.overlaps_with(vec!["horrid", "cool"]))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 3], data);

let data = posts.select(id)
    .filter(tags.overlaps_with(vec!["cool", "great"]))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2, 3], data);

let data = posts.select(id)
    .filter(tags.overlaps_with(vec!["horrid"]))
    .load::<i32>(conn)?;
assert!(data.is_empty());
Source

fn contains<T>(self, other: T) -> Contains<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL @> expression.

This operator returns whether an array contains another array.

§Example
diesel::insert_into(posts)
    .values(tags.eq(vec!["cool", "awesome"]))
    .execute(conn)?;

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

let amazing_posts = posts.select(id)
    .filter(tags.contains(vec!["cool", "amazing"]))
    .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 whether an array is contained by another array. foo.contains(bar) is the same as bar.is_contained_by(foo)

§Example
diesel::insert_into(posts)
    .values(tags.eq(vec!["cool", "awesome"]))
    .execute(conn)?;

let data = posts.select(id)
    .filter(tags.is_contained_by(vec!["cool", "awesome", "amazing"]))
    .load::<i32>(conn)?;
assert_eq!(vec![1], data);

let data = posts.select(id)
    .filter(tags.is_contained_by(vec!["cool"]))
    .load::<i32>(conn)?;
assert!(data.is_empty());
Source

fn index<T>(self, other: T) -> Index<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Integer>,

Indexes a PostgreSQL array.

This operator indexes in to an array to access a single element.

Note that PostgreSQL arrays are 1-indexed, so foo.index(1) is the first element in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.index(id))
    .load::<String>(conn)?;
assert_eq!(vec!["cool", "marvellous"], data);

let data = posts.select(id)
    .filter(tags.index(1).eq("splendid"))
    .load::<i32>(conn)?;
assert_eq!(vec![2], data);
Source

fn index_nullable<T>(self, other: T) -> IndexNullable<Self, T>

Indexes a PostgreSQL array. This is the same as PgArrayExpressionMethods::index but takes a nullable expression for the parameter.

This operator indexes in to an array to access a single element.

Note that PostgreSQL arrays are 1-indexed, so foo.index_nullable(1) is the first element in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.index_nullable(id.nullable()))
    .load::<Option<String>>(conn)?;
assert_eq!(vec![Some("cool".to_string()), Some("marvellous".to_string())], data);

let data = posts.select(id)
    .filter(tags.index_nullable(1).eq(Some("splendid")))
    .load::<i32>(conn)?;
assert_eq!(vec![2], data);
Source

fn slice<T1, T2>(self, start: T1, end: T2) -> Slice<Self, T1, T2>

Indexes a PostgreSQL array with the slice delimited by the given indexes.

This operator indexes in to an array to access a subslice of the array.

Note that PostgreSQL array slices are 1-indexed and inclusive, so foo.slice(1, 3) returns the first three elements in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.slice(id, id))
    .load::<Vec<String>>(conn)?;
assert_eq!(vec![vec!["cool"], vec!["marvellous"]], data);

let data = posts.select(id)
    .filter(tags.slice(1, 2).eq(vec!["splendid", "marvellous"]))
    .load::<i32>(conn)?;
assert_eq!(vec![2], data);
Source

fn slice_nullable<T1, T2>( self, start: T1, end: T2, ) -> SliceNullable<Self, T1, T2>

Indexes a PostgreSQL array with the slice delimited by the given indexes. This is the same as PgArrayExpressionMethods::slice but takes nullable expressions for the parameters.

This operator indexes in to an array to access a subslice of the array.

Note that PostgreSQL array slices are 1-indexed and inclusive, so foo.slice_nullable(1, 3) returns the first three elements in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.slice_nullable(id.nullable(), id.nullable()))
    .load::<Option<Vec<String>>>(conn)?;
assert_eq!(vec![Some(vec!["cool".to_string()]), Some(vec!["marvellous".to_string()])], data);

let data = posts.select(id)
    .filter(tags.slice_nullable(1, 2).eq(Some(vec!["splendid", "marvellous"])))
    .load::<i32>(conn)?;
assert_eq!(vec![2], data);
Source

fn slice_from<T>(self, start: T) -> SliceFrom<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Integer>,

Indexes a PostgreSQL array with the slice starting from the given index.

This operator indexes in to an array to access a subslice of the array,

Note that PostgreSQL array slices are 1-indexed, so foo.slice_from(1) returns all the elements in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.slice_from(id))
    .load::<Vec<String>>(conn)?;
assert_eq!(vec![vec!["cool", "awesome"], vec!["marvellous"]], data);

let data = posts.select(id)
    .filter(tags.slice_from(2).eq(vec!["marvellous"]))
    .load::<i32>(conn)?;
assert_eq!(vec![2], data);
Source

fn slice_from_nullable<T>(self, start: T) -> SliceFromNullable<Self, T>

Indexes a PostgreSQL array with the slice starting from the given index. This is the same as PgArrayExpressionMethods::slice_from but takes a nullable expression for the parameter.

This operator indexes in to an array to access a subslice of the array,

Note that PostgreSQL array slices are 1-indexed, so foo.slice_from_nullable(1) returns all the elements in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.slice_from_nullable(id.nullable()))
    .load::<Option<Vec<String>>>(conn)?;
assert_eq!(vec![Some(vec!["cool".to_string(), "awesome".to_string()]), Some(vec!["marvellous".to_string()])], data);

let data = posts.select(id)
    .filter(tags.slice_from_nullable(2).eq(Some(vec!["marvellous"])))
    .load::<i32>(conn)?;
assert_eq!(vec![2], data);
Source

fn slice_to<T>(self, end: T) -> SliceTo<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Integer>,

Indexes a PostgreSQL array with the slice ending with the given index.

This operator indexes in to an array to access a subslice of the array,

Note that PostgreSQL array slices are 1-indexed, so foo.slice_to(3) returns the first three elements in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.slice_to(id))
    .load::<Vec<String>>(conn)?;
assert_eq!(vec![vec!["cool"], vec!["splendid", "marvellous"]], data);

let data = posts.select(id)
    .filter(tags.slice_to(1).eq(vec!["cool"]))
    .load::<i32>(conn)?;
assert_eq!(vec![1], data);
Source

fn slice_to_nullable<T>(self, end: T) -> SliceToNullable<Self, T>

Indexes a PostgreSQL array with the slice ending with the given index. This is the same as PgArrayExpressionMethods::slice_to but takes a nullable expression for the parameter.

This operator indexes in to an array to access a subslice of the array,

Note that PostgreSQL array slices are 1-indexed, so foo.slice_to_nullable(3) returns the first three elements in the array.

§Example
diesel::insert_into(posts)
    .values(&vec![
        tags.eq(vec!["cool", "awesome"]),
        tags.eq(vec!["splendid", "marvellous"]),
   ])
    .execute(conn)?;

let data = posts.select(tags.slice_to_nullable(id.nullable()))
    .load::<Option<Vec<String>>>(conn)?;
assert_eq!(vec![Some(vec!["cool".to_string()]), Some(vec!["splendid".to_string(), "marvellous".to_string()])], data);

let data = posts.select(id)
    .filter(tags.slice_to_nullable(1).eq(Some(vec!["cool"])))
    .load::<i32>(conn)?;
assert_eq!(vec![1], data);
Source

fn concat<T>(self, other: T) -> Concat<Self, T>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL || expression.

This operator concatenates two Array values and returns Array value

§Example
diesel::insert_into(posts)
    .values(tags.eq(vec!["cool", "awesome"]))
    .execute(conn)?;

let res = posts.select(tags.concat(vec!["amazing"])).load::<Vec<String>>(conn)?;
let expected_tags = vec!["cool", "awesome", "amazing"];
assert_eq!(expected_tags, res[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> PgArrayExpressionMethods for T
where T: Expression, T::SqlType: ArrayOrNullableArray,