Files
qbz/crates/qbz-library/src/album_grouping.rs
T
vicrodh 54074a5580 feat(library): add metadata-grouped album queries
New album_grouping module exports the SQL CASE-WHEN expression for the
metadata group key shared by both new queries. The expression has three
priorities: (1) usable album tag => album|artist key, (2) fallback to
existing folder-based album_group_key, (3) orphan bucket
'__unknown_album__'.

get_albums_metadata_grouped runs the grouped query and returns
LocalAlbum rows with directory_path empty and source_folders populated
with the comma-separated list of contributing folders. Pinned 'Unknown
Album' to the bottom via ORDER BY.

get_album_tracks_metadata fetches all tracks belonging to a metadata
group, ordered by disc/track number.

Adds idx_local_tracks_album_lookup index on (album, album_artist,
artist) to keep the GROUP BY index-backed at 16k+ track libraries.

LocalAlbum gains an optional source_folders field (None for folder-
grouped rows; Some(comma-separated) for metadata-grouped rows). The
existing folder-grouped query continues to populate it as None so no
downstream consumer breaks.

Hidden filter (album_settings.hidden) does not apply to metadata-
grouped rows in this iteration; revisiting requires extending
album_settings to support metadata keys.

cc
2026-05-06 17:10:51 -06:00

32 lines
1.2 KiB
Rust

//! SQL fragments for metadata-based album grouping in the Local Library.
//!
//! The metadata-grouped Albums view groups tracks by `album` +
//! `COALESCE(album_artist, artist)` when the album tag is usable, falls
//! back to the existing folder-based `album_group_key` when it's not,
//! and dumps anything else into a single `__unknown_album__` bucket.
//!
//! Both `get_albums_metadata_grouped` and `get_album_tracks_metadata`
//! must produce the same group_key for the same row, so the expression
//! is centralized here.
/// SQL expression that produces the metadata group key for a row of
/// `local_tracks`. Insert wherever you would otherwise use a column.
pub fn metadata_group_key_sql_expression() -> &'static str {
r#"CASE
WHEN album IS NOT NULL
AND TRIM(album) != ''
AND album != 'Unknown Album'
THEN album || '|' || COALESCE(album_artist, artist, 'Unknown Artist')
WHEN album_group_key IS NOT NULL
AND album_group_key != ''
THEN album_group_key
ELSE '__unknown_album__'
END"#
}
/// Sentinel group_key value used for the orphan bucket. Frontend can
/// special-case this if it needs a localized label.
pub const UNKNOWN_ALBUM_GROUP_KEY: &str = "__unknown_album__";