Skip to content

Commit

Permalink
added array_replace pg array function
Browse files Browse the repository at this point in the history
  • Loading branch information
Azan Ali authored and Azan Ali committed Aug 22, 2024
1 parent ae82c4a commit 5ab7f20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,41 @@ define_sql_function! {
/// ```
fn array_append<Arr: ArrayOrNullableArray<Inner=T> + SingleValue, T: SingleValue>(a: Arr, e: T) -> Array<T>;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Append an element to the end of an array
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::array_replace;
/// # use diesel::sql_types::{Nullable, Integer, Array};
/// # let connection = &mut establish_connection();
/// let ints = diesel::select(array_replace::<Array<_>, Integer, _, _>(vec![1, 2, 5, 4], 5, 3))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(vec![1, 2, 3, 4], ints);
///
/// let ints = diesel::select(array_replace::<Array<_>, Nullable<Integer>, _, _>(vec![Some(1), Some(2), Some(3)], Some(3), None::<i32>))
/// .get_result::<Vec<Option<i32>>>(connection)?;
/// assert_eq!(vec![Some(1), Some(2), None], ints);
///
/// let ints = diesel::select(array_append::<Nullable<Array<_>>, Integer, _, _>(None::<Vec<i32>>, Some(1), Some(2)))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(vec![None], ints);
///
/// let ints = diesel::select(array_append::<Nullable<Array<_>>, Nullable<Integer>, _, _>(None::<Vec<i32>>, None::<i32>))
/// .get_result::<Vec<Option<i32>>>(connection)?;
/// assert_eq!(vec![None], ints);
/// # Ok(())
/// # }
/// ```
fn array_replace<Arr: ArrayOrNullableArray<Inner=T> + SingleValue, T: SingleValue>(a: Arr, e: T, r: T) -> Array<T>;
}
5 changes: 5 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,8 @@ pub type range_merge<R1, R2> = super::functions::range_merge<SqlTypeOf<R1>, SqlT
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_append<A, E> = super::functions::array_append<SqlTypeOf<A>, SqlTypeOf<E>, A, E>;

/// Return type of [`array_replace(array, element, replace_with)`](super::functions::array_replace())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_replace<A, E, R> = super::functions::array_replace<SqlTypeOf<A>, SqlTypeOf<E>, A, E, R>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ fn postgres_functions() -> _ {
bound,
),
array_append(pg_extras::array, pg_extras::id),
array_replace(pg_extras::array, pg_extras::id, pg_extras::id)
)
}

Expand Down

0 comments on commit 5ab7f20

Please sign in to comment.