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

implement array_dims #4190

Merged
merged 23 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
31 changes: 31 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,34 @@ define_sql_function! {
/// ```
fn array_append<Arr: ArrayOrNullableArray<Inner=T> + SingleValue, T: SingleValue>(a: Arr, e: T) -> Array<T>;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns a text representation of the array's dimensions
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main(){
/// # run_test().unwrap();
/// # }
/// # fn run_test()->QueryResult<()>{
/// # use diesel::dsl::array_dims;
/// # use diesel::sql_types::{Nullable,Array,Integer};
/// # let connection = &mut establish_connection();
///
/// let dims = diesel::select(array_dims::<Array<_>,_,_>(vec![1,2]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// let dims = diesel::select(array_dims::<Array<_>,_,_>(vec![1,2]))
/// let dims = diesel::select(array_dims::<Array<Integer>,_,_>(vec![1,2]))

The error says that rust don't know with implementation use, this i32 could be Int4, Int8 or something else, to fix you need to specify the type here. The array_append doesn't need this because it Array inner type is the same of the second argument, and the second argument is specified

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the help!

/// .get_result::<String>(connection).unwrap();
/// assert!(String::from("[1:2]").eq(&dims));
///
/// let dims = diesel::select(array_dims::<Array<_>,_,_>(vec![vec![1,2,3],vec![4,5,6]]))
/// .get_result::<String>(connection).unwrap();
/// assert!(String::from("[1:2][1:3]").eq(&dims));
Copy link
Contributor

@guissalustiano guissalustiano Aug 22, 2024

Choose a reason for hiding this comment

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

The crazy error here say that diesel didn't know how to transform Vec<Vec<_>> to Array<_> because Diesel does not support multidimensional arrays yet. So you can remove this test for now.

///
/// # Ok(())
/// # }
///
fn array_dims<Arr:ArrayOrNullableArray<Inner=T> + SingleValue,T:SingleValue>(arr:Arr) -> Text;
}
5 changes: 5 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,8 @@ pub type range_merge<R1, R2> = super::functions::range_merge<SqlTypeOf<R1>, SqlT
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_append<A, E> = super::functions::array_append<SqlTypeOf<A>, SqlTypeOf<E>, A, E>;

/// Return type of [`array_dims(array)`](super::functions::array_append())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_dims<A,T> = super::functions::array_dims<SqlTypeOf<A>,SqlTypeOf<T>, A>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ fn postgres_functions() -> _ {
bound,
),
array_append(pg_extras::array, pg_extras::id),
array_dims(pg_extras::array)
)
}

Expand Down
Loading