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

Add support for array_to_string and array_to_string_null array functions #4171

Merged
merged 13 commits into from
Aug 24, 2024
Merged
13 changes: 6 additions & 7 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,18 @@ define_sql_function! {
/// # Ok(())
/// # }
/// ```
fn array_to_string_null<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
#[sql_name = "array_to_string"]
fn array_to_string_with_null_string<Arr: ArrayOrNullableArray + SingleValue>(
arr: Arr,
delim: Text,
null_str: Nullable<Text>
null_str: Text
) -> Text;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
#[sql_name = "array_to_string"]
/// Converts each array element to its text representation, and concatenates those separated by the delimiter string.
/// This variant omits the `null_str` argument.
/// This variant omits the `null_str` argument. See [array_to_string_null] for a variant with that argument
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you rename this function, new name should be used:

Suggested change
/// This variant omits the `null_str` argument. See [array_to_string_null] for a variant with that argument
/// This variant omits the `null_str` argument. See [`array_to_string_with_null_string`] for a variant with that argument

/// # Example
///
/// ```rust
Expand All @@ -810,15 +810,14 @@ define_sql_function! {
/// # let conn = &mut establish_connection();
///
/// let result = diesel::select(array_to_string::<Array<Text>, Text>(
/// vec!["a".to_string(), "b".to_string(), "c".to_string()],
/// ", ".to_string()
/// vec!["a", "b", "c"], ","
/// )).get_result::<String>(conn)?;
/// assert_eq!("a, b, c".to_string(), result);
///
/// # Ok(())
/// # }
/// ```
fn array_to_string<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
fn array_to_string<Arr: ArrayOrNullableArray + SingleValue>(
arr: Arr,
delim: Text
) -> Text;
Expand Down
16 changes: 5 additions & 11 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::expression::grouped::Grouped;
use crate::expression::Expression;
use crate::pg::expression::expression_methods::private::{JsonIndex, JsonRemoveIndex};
use crate::pg::types::sql_types::Array;
use crate::sql_types::{Inet, Integer, Nullable, Text, VarChar};
use crate::sql_types::{Inet, Integer, Text, VarChar};

/// The return type of [`lhs.ilike(rhs)`](super::expression_methods::PgTextExpressionMethods::ilike)
#[cfg(feature = "postgres_backend")]
Expand Down Expand Up @@ -308,22 +308,16 @@ pub type NotLikeBinary<Lhs, Rhs> = crate::dsl::NotLike<Lhs, Rhs>;
#[deprecated(note = "Use `dsl::Concat` instead")]
pub type ConcatArray<Lhs, Rhs> = crate::dsl::Concat<Lhs, Rhs>;

/// Return type of [`array_to_string_null(arr, delim, null_str)`](super::functions::array_to_string_null())
/// Return type of [`array_to_string_with_null_string(arr, delim, null_str)`](super::functions::array_to_string_with_null_string())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_to_string_null<Arr, Elem> = super::functions::array_to_string_null<
SqlTypeOf<Arr>,
SqlTypeOf<Elem>,
Arr,
Elem,
Nullable<Text>,
>;
pub type array_to_string_with_null_string<Arr, Elem> =
super::functions::array_to_string_with_null_string<SqlTypeOf<Arr>, SqlTypeOf<Elem>, Arr, Text>;

/// Return type of [`array_to_string(arr, delim)`](super::functions::array_to_string())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_to_string<Arr, Elem> =
super::functions::array_to_string<SqlTypeOf<Arr>, SqlTypeOf<Elem>, Arr, Elem>;
pub type array_to_string<Arr> = super::functions::array_to_string<SqlTypeOf<Arr>, Arr, Text>;

/// Return type of [`lower(range)`](super::functions::lower())
#[allow(non_camel_case_types)]
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ fn postgres_functions() -> _ {
bound,
),
array_append(pg_extras::array, pg_extras::id),
array_to_string_null(pg_extras::array, ", ".into(), Some("NULL".into())),
array_to_string_with_null_string(pg_extras::array, ", ".into(), Some("NULL".into())),
array_to_string(pg_extras::array, ", ".to_string()),
)
}
Expand Down
Loading