Skip to content

Commit

Permalink
Merge pull request #4202 from wowinter13/feature/add_array_cat_function
Browse files Browse the repository at this point in the history
Implement `array_cat` and `array_length` functions
  • Loading branch information
weiznich authored Aug 27, 2024
2 parents 09d5e10 + 3140390 commit b718c47
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
58 changes: 58 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,3 +1038,61 @@ define_sql_function! {
///
fn trim_array<Arr:ArrayOrNullableArray + SingleValue>(a: Arr, n: Integer) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Concatenates two arrays
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::array_cat;
/// # use diesel::sql_types::{Integer, Array, Nullable};
/// # let connection = &mut establish_connection();
/// let result = diesel::select(array_cat::<Array<Integer>, _, _>(vec![1, 2], vec![3, 4]))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(vec![1, 2, 3, 4], result);
///
/// let nullable_result = diesel::select(array_cat::<Nullable<Array<Integer>>, _, _>(
/// None::<Vec<i32>>,
/// None::<Vec<i32>>
/// )).get_result::<Option<Vec<i32>>>(connection)?;
/// assert_eq!(None, nullable_result);
/// # Ok(())
/// # }
/// ```
fn array_cat<Arr: ArrayOrNullableArray + SingleValue>(a: Arr, b: Arr) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns the length of the requested array
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::array_length;
/// # use diesel::sql_types::{Integer, Array};
/// # let connection = &mut establish_connection();
/// let result = diesel::select(array_length::<Array<Integer>, _, _>(vec![1, 2, 3], 1))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(Some(3), result);
/// # Ok(())
/// # }
/// ```
fn array_length<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, dimension: Integer) -> Nullable<Integer>;
}
10 changes: 10 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,13 @@ pub type cardinality<A> = super::functions::cardinality<SqlTypeOf<A>, A>;
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type trim_array<A, N> = super::functions::trim_array<SqlTypeOf<A>, A, N>;

/// Return type of [`array_cat(array_a, array_b)`](super::functions::array_cat())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_cat<A, B> = super::functions::array_cat<SqlTypeOf<A>, A, B>;

/// Return type of [`array_length(array, dimension)`](super::functions::array_length())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_length<A, D> = super::functions::array_length<SqlTypeOf<A>, A, D>;
2 changes: 2 additions & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ fn postgres_functions() -> _ {
array_to_string_with_null_string(pg_extras::array, pg_extras::name, pg_extras::name),
cardinality(pg_extras::array),
trim_array(pg_extras::array, pg_extras::id),
array_cat(pg_extras::array, pg_extras::array),
array_length(pg_extras::array, 1_i32),
)
}

Expand Down

0 comments on commit b718c47

Please sign in to comment.