-
Hello! use diesel::{
dsl::{self, ArrayContains},
helper_types::{GroupBy, Having, Select},
sql_types::{self, Array, Uuid},
};
sql_function!(
#[aggregate]
fn array_agg<T: diesel::sql_types::SingleValue>(expr: T) -> Array<T>;
);
type GroupBySong = GroupBy<song_artist::table, song_artist::song>;
type HavingArtists = Having<GroupBySong, ArtistsContains>;
type SongByArtist = Select<HavingArtists, song_artist::song>;
type ArtistsContains =
ArrayContains<array_agg::HelperType<Uuid, song_artist::artist>, Vec<uuid::Uuid>>;
#[derive(Queryable, Debug)]
pub struct SongArtist {
song: uuid::Uuid,
artist: uuid::Uuid
}
impl SongArtist {
pub fn artists_contains(artists: &[uuid::Uuid]) -> ArtistsContains {
array_agg(song_artist::artist).contains(artist_uuids)
}
pub fn songs_by_artists(artists: &[uuid::Uuid]) -> SongByArtist {
song_artist::table
.group_by(song_artist::song)
.having(Self::artists_contains(artists))
.select(song_artist::song)
}
} It bugs me though that for writing that return type of the function |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
That's currently the only way to specify these return types. I'm not aware of any other solution that retains all necessary type information to beeing able to perform compile time checking without encoding them in the types itself. We are open for concrete suggestions on how to improve this. Please keep in mind that we don't look for simple: "Why not just do xyz" style suggestions, but for something more substantial. For example just suggesting to use |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. It's just that writting the return type felt like rewriting the query itself and idk, it felt kinda wrong. |
Beta Was this translation helpful? Give feedback.
That's currently the only way to specify these return types. I'm not aware of any other solution that retains all necessary type information to beeing able to perform compile time checking without encoding them in the types itself. We are open for concrete suggestions on how to improve this. Please keep in mind that we don't look for simple: "Why not just do xyz" style suggestions, but for something more substantial. For example just suggesting to use
impl Trait
is not helpful has this cannot ever work.