Skip to content

Commit

Permalink
Merge pull request #4138 from guissalustiano/array_append
Browse files Browse the repository at this point in the history
impl pg array append
  • Loading branch information
Ten0 authored Aug 2, 2024
2 parents 71fb528 + 56d979c commit 861893c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
12 changes: 9 additions & 3 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2877,10 +2877,16 @@ pub(in crate::pg) mod private {
message = "`{Self}` is neither `diesel::sql_types::Array<_>` nor `diesel::sql_types::Nullable<Array<_>>`",
note = "try to provide an expression that produces one of the expected sql types"
)]
pub trait ArrayOrNullableArray {}
pub trait ArrayOrNullableArray {
type Inner;
}

impl<T> ArrayOrNullableArray for Array<T> {}
impl<T> ArrayOrNullableArray for Nullable<Array<T>> {}
impl<T> ArrayOrNullableArray for Array<T> {
type Inner = T;
}
impl<T> ArrayOrNullableArray for Nullable<Array<T>> {
type Inner = T;
}

/// Marker trait used to implement `PgNetExpressionMethods` on the appropriate types.
#[diagnostic::on_unimplemented(
Expand Down
44 changes: 44 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::expression_methods::InetOrCidr;
use super::expression_methods::RangeHelper;
use crate::expression::functions::define_sql_function;
use crate::pg::expression::expression_methods::ArrayOrNullableArray;
use crate::sql_types::*;

define_sql_function! {
Expand Down Expand Up @@ -511,9 +512,11 @@ define_sql_function! {
/// # }
/// #
/// # fn main() {
/// # #[cfg(feature = "numeric")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "numeric")]
/// # fn run_test() -> QueryResult<()> {
/// # use self::posts::dsl::*;
/// # use std::collections::Bound;
Expand Down Expand Up @@ -559,9 +562,11 @@ define_sql_function! {
/// # }
/// #
/// # fn main() {
/// # #[cfg(feature = "time")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "time")]
/// # fn run_test() -> QueryResult<()> {
/// # use self::posts::dsl::*;
/// # use std::collections::Bound;
Expand Down Expand Up @@ -655,9 +660,11 @@ define_sql_function! {
/// # }
/// #
/// # fn main() {
/// # #[cfg(feature = "time")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "time")]
/// # fn run_test() -> QueryResult<()> {
/// # use self::posts::dsl::*;
/// # use std::collections::Bound;
Expand Down Expand Up @@ -687,3 +694,40 @@ define_sql_function! {
#[cfg(feature = "postgres_backend")]
fn daterange(lower: Nullable<Date>, upper: Nullable<Date>, bound: RangeBoundEnum) -> Daterange;
}

#[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_append;
/// # use diesel::sql_types::{Nullable, Integer, Array};
/// # let connection = &mut establish_connection();
/// let ints = diesel::select(array_append::<Array<_>, Integer, _, _>(vec![1, 2], 3))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(vec![1, 2, 3], ints);
///
/// let ints = diesel::select(array_append::<Array<_>, Nullable<Integer>, _, _>(vec![Some(1), Some(2)], 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>>, 3))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(vec![3], 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_append<Arr: ArrayOrNullableArray<Inner=T> + SingleValue, T: SingleValue>(a: Arr, e: T) -> Array<T>;
}

0 comments on commit 861893c

Please sign in to comment.