diesel::pg::expression::dsl

Function array_to_string_with_null_string

Source
pub fn array_to_string_with_null_string<Arr: ArrayOrNullableArray + SingleValue, array, del, null>(
    array: array,
    del: del,
    null: null,
) -> array_to_string_with_null_string<Arr, array, del, null>
where array: AsExpression<Arr>, del: AsExpression<Text>, null: AsExpression<Text>,
Available on crate feature postgres_backend only.
Expand description

Converts each array element to its text representation and concatenates those elements separated by the delimiter string. If null_string is provided and is not NULL, then NULL array entries are represented by that string; otherwise, they are omitted.

ยงExample


// Example with `NULL` representation as a string
let result: String = diesel::select(array_to_string_with_null_string::<Array<Nullable<Text>>, _, _, _>(
    vec![Some("first"), None::<&str>, Some("third")], ",", "NULL"))
    .get_result(connection)?;
assert_eq!(result, "first,NULL,third");

// Example without any `NULL` values
let result: String = diesel::select(array_to_string_with_null_string::<Array<Nullable<Text>>, _, _, _>(
    vec![Some("first"), Some("second")], ",", "NULL"))
    .get_result(connection)?;
assert_eq!(result, "first,second");

// Example with all `NULL` values
let result: String = diesel::select(array_to_string_with_null_string::<Array<Nullable<Text>>, _, _, _>(
    vec![None::<&str>, None::<&str>], ",", "NULL"))
    .get_result(connection)?;
assert_eq!(result, "NULL,NULL");