Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl pg array append #4138

Merged
merged 8 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2730,10 +2730,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>;
}
2 changes: 1 addition & 1 deletion diesel/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ where
/// match *self {
/// // these string values need to match the labels used in your
/// // enum definition in SQL. So this expects that you defined the
/// /// relevat enum type as`ENUM('one', 'two')` in your `CREATE TABLE` statement
/// /// relevant enum type as`ENUM('one', 'two')` in your `CREATE TABLE` statement
/// Post::FirstValue => out.write_all(b"one")?,
/// Post::SecondValue => out.write_all(b"two")?,
/// }
Expand Down
Loading