From b4febd1154dc3abbe634b921aca3bd56f08c36fd Mon Sep 17 00:00:00 2001 From: "Guilherme S. Salustiano" Date: Wed, 31 Jul 2024 12:28:35 -0300 Subject: [PATCH 1/8] impl array_append --- diesel/src/pg/expression/functions.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index a9662e22e914..98a26c45aa84 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -687,3 +687,28 @@ define_sql_function! { #[cfg(feature = "postgres_backend")] fn daterange(lower: Nullable, upper: Nullable, bound: RangeBoundEnum) -> Daterange; } + +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, array_append}; + /// # use diesel::sql_types::{Integer, Nullable}; + /// # let connection = &mut establish_connection(); + /// let ints = diesel::select(array_append::(Some(vec![Some(1), Some(2)]), Some(3))) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![Some(1), Some(2), Some(3)], ints); + /// # Ok(()) + /// # } + /// ``` + #[cfg(feature = "postgres_backend")] + fn array_append(ts: Nullable>>, t: Nullable) -> Array>; +} From 8d53f2fef12124b1e1e45074685b0ba842cc9db7 Mon Sep 17 00:00:00 2001 From: "Guilherme S. Salustiano" Date: Wed, 31 Jul 2024 13:37:09 -0300 Subject: [PATCH 2/8] allow non nullable range --- diesel/src/pg/expression/expression_methods.rs | 12 +++++++++--- diesel/src/pg/expression/functions.rs | 9 +++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/diesel/src/pg/expression/expression_methods.rs b/diesel/src/pg/expression/expression_methods.rs index f14116a5d0df..55b86eca571b 100644 --- a/diesel/src/pg/expression/expression_methods.rs +++ b/diesel/src/pg/expression/expression_methods.rs @@ -2730,10 +2730,16 @@ pub(in crate::pg) mod private { message = "`{Self}` is neither `diesel::sql_types::Array<_>` nor `diesel::sql_types::Nullable>`", note = "try to provide an expression that produces one of the expected sql types" )] - pub trait ArrayOrNullableArray {} + pub trait ArrayOrNullableArray { + type Inner; + } - impl ArrayOrNullableArray for Array {} - impl ArrayOrNullableArray for Nullable> {} + impl ArrayOrNullableArray for Array { + type Inner = T; + } + impl ArrayOrNullableArray for Nullable> { + type Inner = T; + } /// Marker trait used to implement `PgNetExpressionMethods` on the appropriate types. #[diagnostic::on_unimplemented( diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 98a26c45aa84..6445bbd8483f 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -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! { @@ -700,15 +701,15 @@ define_sql_function! { /// # } /// # /// # fn run_test() -> QueryResult<()> { - /// # use diesel::dsl::{array, array_append}; - /// # use diesel::sql_types::{Integer, Nullable}; + /// # use diesel::dsl::array_append; + /// # use diesel::sql_types::{Integer, Array}; /// # let connection = &mut establish_connection(); - /// let ints = diesel::select(array_append::(Some(vec![Some(1), Some(2)]), Some(3))) + /// let ints = diesel::select(array_append::, _, _>(vec![Some(1), Some(2)], Some(3))) /// .get_result::>>(connection)?; /// assert_eq!(vec![Some(1), Some(2), Some(3)], ints); /// # Ok(()) /// # } /// ``` #[cfg(feature = "postgres_backend")] - fn array_append(ts: Nullable>>, t: Nullable) -> Array>; + fn array_append> + SingleValue>(a: Arr, e: Nullable) -> Array>; } From 444bab7eb1c054fbcde123c45d9fd671ebf79973 Mon Sep 17 00:00:00 2001 From: "Guilherme S. Salustiano" Date: Wed, 31 Jul 2024 14:03:50 -0300 Subject: [PATCH 3/8] typo --- diesel/src/serialize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diesel/src/serialize.rs b/diesel/src/serialize.rs index 9fc0b00bd01e..ef8053776b4f 100644 --- a/diesel/src/serialize.rs +++ b/diesel/src/serialize.rs @@ -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")?, /// } From 8ed4040a5be0f3830f7d3c2e8bf1ea24960b4562 Mon Sep 17 00:00:00 2001 From: "Guilherme S. Salustiano" Date: Thu, 1 Aug 2024 08:14:42 -0300 Subject: [PATCH 4/8] move cfg to outside the macro --- diesel/src/pg/expression/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 6445bbd8483f..5c4a99ccb03a 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -689,6 +689,7 @@ define_sql_function! { fn daterange(lower: Nullable, upper: Nullable, bound: RangeBoundEnum) -> Daterange; } +#[cfg(feature = "postgres_backend")] define_sql_function! { /// Append an element to the end of an array. /// # Example @@ -710,6 +711,5 @@ define_sql_function! { /// # Ok(()) /// # } /// ``` - #[cfg(feature = "postgres_backend")] fn array_append> + SingleValue>(a: Arr, e: Nullable) -> Array>; } From e99956027879127a2f2e3e1978295766fbf2a0ba Mon Sep 17 00:00:00 2001 From: "Guilherme S. Salustiano" Date: Thu, 1 Aug 2024 08:21:35 -0300 Subject: [PATCH 5/8] remove nullable element --- diesel/src/pg/expression/functions.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 5c4a99ccb03a..8c26d0643fbf 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -705,11 +705,11 @@ define_sql_function! { /// # use diesel::dsl::array_append; /// # use diesel::sql_types::{Integer, Array}; /// # let connection = &mut establish_connection(); - /// let ints = diesel::select(array_append::, _, _>(vec![Some(1), Some(2)], Some(3))) - /// .get_result::>>(connection)?; - /// assert_eq!(vec![Some(1), Some(2), Some(3)], ints); + /// let ints = diesel::select(array_append::, _, _>(vec![1, 2], 3)) + /// .get_result::>(connection)?; + /// assert_eq!(vec![1, 2, 3], ints); /// # Ok(()) /// # } /// ``` - fn array_append> + SingleValue>(a: Arr, e: Nullable) -> Array>; + fn array_append + SingleValue>(a: Arr, e: T) -> Array; } From 263d1f1e6facffd279e24fcea853422a887fccd1 Mon Sep 17 00:00:00 2001 From: "Guilherme S. Salustiano" Date: Thu, 1 Aug 2024 22:47:33 -0300 Subject: [PATCH 6/8] add tests --- diesel/src/pg/expression/functions.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 8c26d0643fbf..caeda1ed48b4 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -703,11 +703,23 @@ define_sql_function! { /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::dsl::array_append; - /// # use diesel::sql_types::{Integer, Array}; + /// # use diesel::sql_types::{Nullable, Integer, Array}; /// # let connection = &mut establish_connection(); /// let ints = diesel::select(array_append::, _, _>(vec![1, 2], 3)) /// .get_result::>(connection)?; /// assert_eq!(vec![1, 2, 3], ints); + /// + /// let ints = diesel::select(array_append::, Array<_>, _, _>(vec![Some(1), Some(2)], None::)) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![Some(1), Some(2), None], ints); + /// + /// let ints = diesel::select(array_append::>, _, _>(None::>, 3)) + /// .get_result::>(connection)?; + /// assert_eq!(vec![3], ints); + /// + /// let ints = diesel::select(array_append::, Nullable>, _, _>(None::>, None::)) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![None], ints); /// # Ok(()) /// # } /// ``` From 4f843d63619707dc7144cafb009887c4a2170900 Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Fri, 2 Aug 2024 09:59:22 +0200 Subject: [PATCH 7/8] Switch the generic argument order to make it conistent with the real argument order --- diesel/src/pg/expression/functions.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index caeda1ed48b4..322b7291022d 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -705,23 +705,23 @@ define_sql_function! { /// # use diesel::dsl::array_append; /// # use diesel::sql_types::{Nullable, Integer, Array}; /// # let connection = &mut establish_connection(); - /// let ints = diesel::select(array_append::, _, _>(vec![1, 2], 3)) + /// let ints = diesel::select(array_append::, Integer, _, _>(vec![1, 2], 3)) /// .get_result::>(connection)?; /// assert_eq!(vec![1, 2, 3], ints); /// - /// let ints = diesel::select(array_append::, Array<_>, _, _>(vec![Some(1), Some(2)], None::)) + /// let ints = diesel::select(array_append::, Nullable, _, _>(vec![Some(1), Some(2)], None::)) /// .get_result::>>(connection)?; /// assert_eq!(vec![Some(1), Some(2), None], ints); /// - /// let ints = diesel::select(array_append::>, _, _>(None::>, 3)) + /// let ints = diesel::select(array_append::>, Integer, _, _>(None::>, 3)) /// .get_result::>(connection)?; /// assert_eq!(vec![3], ints); /// - /// let ints = diesel::select(array_append::, Nullable>, _, _>(None::>, None::)) + /// let ints = diesel::select(array_append::>, Nullable, _, _>(None::>, None::)) /// .get_result::>>(connection)?; /// assert_eq!(vec![None], ints); /// # Ok(()) /// # } /// ``` - fn array_append + SingleValue>(a: Arr, e: T) -> Array; + fn array_append + SingleValue, T: SingleValue>(a: Arr, e: T) -> Array; } From 56d979c6e395a2422b2036bfce9fd527bc287b6b Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Fri, 2 Aug 2024 09:59:48 +0200 Subject: [PATCH 8/8] Fix running the doctests without time and numeric features --- diesel/src/pg/expression/functions.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 322b7291022d..313958927be8 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -512,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; @@ -560,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; @@ -656,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;