Skip to content

Commit

Permalink
Merge pull request #3660 from sintemal/master
Browse files Browse the repository at this point in the history
Add support for the array concat operation in the dsl
  • Loading branch information
weiznich authored Jun 29, 2023
2 parents 732497c + 7914ab2 commit d8cade5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
46 changes: 46 additions & 0 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,52 @@ pub trait PgArrayExpressionMethods: Expression + Sized {
{
ArrayIndex::new(self, other.as_expression())
}

/// Creates a PostgreSQL `||` expression.
///
/// This operator concatenates two Array values and returns Array value
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # table! {
/// # posts {
/// # id -> Integer,
/// # tags -> Array<VarChar>,
/// # }
/// # }
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use self::posts::dsl::*;
/// # 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, tags TEXT[] NOT NULL)")
/// # .execute(conn)
/// # .unwrap();
/// #
/// 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]);
/// # Ok(())
/// # }
///
fn concat<T>(self, other: T) -> dsl::ConcatArray<Self, T>
where
Self::SqlType: SqlType,
T: AsExpression<Self::SqlType>,
{
Grouped(ConcatArray::new(self, other.as_expression()))
}
}

impl<T> PgArrayExpressionMethods for T
Expand Down
4 changes: 4 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,7 @@ pub type LikeBinary<Lhs, Rhs> = Grouped<super::operators::LikeBinary<Lhs, AsExpr
#[cfg(feature = "postgres_backend")]
pub type NotLikeBinary<Lhs, Rhs> =
Grouped<super::operators::NotLikeBinary<Lhs, AsExprOf<Rhs, Binary>>>;

/// The return type of [`lhs.concat(rhs)`](super::expression_methods::PgArrayExpressionMethods::concat)
#[cfg(feature = "postgres_backend")]
pub type ConcatArray<Lhs, Rhs> = Grouped<super::operators::ConcatArray<Lhs, AsExpr<Rhs, Lhs>>>;
6 changes: 6 additions & 0 deletions diesel/src/pg/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ infix_operator!(RemoveByPathFromJsonb, " #-", Jsonb, backend: Pg);
infix_operator!(ConcatBinary, " || ", Binary, backend: Pg);
infix_operator!(LikeBinary, " LIKE ", backend: Pg);
infix_operator!(NotLikeBinary, " NOT LIKE ", backend: Pg);
__diesel_infix_operator!(
ConcatArray,
" || ",
__diesel_internal_SameResultAsInput,
backend: Pg
);

#[derive(Debug, Clone, Copy, QueryId, DieselNumericOps, ValidGrouping)]
#[doc(hidden)]
Expand Down
4 changes: 2 additions & 2 deletions diesel_compile_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ error[E0277]: the trait bound `{integer}: diesel::Expression` is not satisfied
(T0, T1, T2, T3, T4, T5)
(T0, T1, T2, T3, T4, T5, T6)
(T0, T1, T2, T3, T4, T5, T6, T7)
and 124 others
and $N others
= note: required for `{integer}` to implement `AsExpression<diesel::sql_types::Text>`

0 comments on commit d8cade5

Please sign in to comment.