Skip to content

Commit

Permalink
Merge pull request #4190 from valkrypton/feat/add/array_dims
Browse files Browse the repository at this point in the history
implement array_dims
  • Loading branch information
weiznich authored Aug 23, 2024
2 parents 01c2eae + 949ba5e commit 6db1ada
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
36 changes: 35 additions & 1 deletion diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ define_sql_function! {
///
/// let ints = diesel::select(array_replace::<Nullable<Array<_>>, Integer, _, _, _>(None::<Vec<i32>>, 1, 2))
/// .get_result::<Option<Vec<i32>>>(connection)?;
///
///
/// let ints = diesel::select(array_replace::<Nullable<Array<_>>, Nullable<Integer>, _, _, _>(None::<Vec<i32>>, None::<i32>, Some(1)))
/// .get_result::<Option<Vec<Option<i32>>>>(connection)?;
/// assert_eq!(None, ints);
Expand All @@ -779,3 +779,37 @@ define_sql_function! {
/// ```
fn array_replace<Arr: ArrayOrNullableArray<Inner=T> + SingleValue, T: SingleValue>(a: Arr, e: T, r: T) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns a text representation of the array's dimensions
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main(){
/// # run_test().unwrap();
/// # }
/// # fn run_test()->QueryResult<()>{
/// # use diesel::dsl::array_dims;
/// # use diesel::sql_types::{Nullable,Array,Integer};
/// # let connection = &mut establish_connection();
///
/// let dims = diesel::select(array_dims::<Array<Integer>,_>(vec![1,2]))
/// .get_result::<String>(connection)?;
/// assert!(String::from("[1:2]").eq(&dims));
///
/// let dims = diesel::select(array_dims::<Array<Nullable<Integer>>,_>(vec![None::<i32>,Some(2)]))
/// .get_result::<String>(connection)?;
/// assert!(String::from("[1:2]").eq(&dims));
///
/// let dims = diesel::select(array_dims::<Array<Nullable<Integer>>,_>(vec![None::<i32>]))
/// .get_result::<String>(connection)?;
/// assert!(String::from("[1:1]").eq(&dims));
/// # Ok(())
/// # }
///
fn array_dims<Arr:ArrayOrNullableArray<> + SingleValue>(arr:Arr) -> Text;
}
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 @@ -357,3 +357,8 @@ pub type array_append<A, E> = super::functions::array_append<SqlTypeOf<A>, SqlTy
#[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>;

/// Return type of [`array_dims(array)`](super::functions::array_append())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_dims<A> = super::functions::array_dims<SqlTypeOf<A>, A>;
3 changes: 2 additions & 1 deletion diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ fn postgres_functions() -> _ {
bound,
),
array_append(pg_extras::array, pg_extras::id),
array_replace(pg_extras::array, pg_extras::id, pg_extras::id)
array_replace(pg_extras::array, pg_extras::id, pg_extras::id),
array_dims(pg_extras::array),
)
}

Expand Down

0 comments on commit 6db1ada

Please sign in to comment.