Skip to main content

koan_core/db/queries/
mod.rs

1mod albums;
2mod artists;
3pub mod auth;
4pub mod batch;
5mod favourites;
6pub mod history;
7pub mod lyrics;
8pub mod playback_state;
9pub mod radio;
10mod scan_cache;
11mod search;
12pub mod snapshots;
13mod stats;
14pub mod tracks;
15pub mod vectors;
16
17use std::path::PathBuf;
18
19// Re-export all public items so `use queries::*` still works.
20pub use albums::*;
21pub use artists::*;
22pub use batch::*;
23pub use favourites::*;
24pub use history::*;
25pub use lyrics::*;
26pub use playback_state::*;
27pub use radio::*;
28pub use scan_cache::*;
29pub use search::*;
30pub use snapshots::*;
31pub use stats::*;
32pub use tracks::*;
33pub use vectors::*;
34
35// --- Row types ---
36
37#[derive(Debug, Clone)]
38pub struct ArtistRow {
39    pub id: i64,
40    pub name: String,
41    pub sort_name: Option<String>,
42    pub remote_id: Option<String>,
43    /// Albums credited to this artist, and tracks across them. Aggregated in
44    /// the same query as the row itself — a count per artist would be one
45    /// query per row in a list thousands long.
46    pub album_count: i64,
47    pub track_count: i64,
48}
49
50#[derive(Debug, Clone)]
51pub struct AlbumRow {
52    pub id: i64,
53    pub title: String,
54    pub artist_id: i64,
55    pub artist_name: String,
56    pub date: Option<String>,
57    pub total_discs: Option<i32>,
58    pub total_tracks: Option<i32>,
59    pub codec: Option<String>,
60    pub label: Option<String>,
61    pub remote_id: Option<String>,
62    /// When the album entered the library — the server's `created` for remote
63    /// albums, otherwise the time it was first indexed.
64    pub added_at: Option<String>,
65}
66
67#[derive(Debug, Clone)]
68pub struct TrackRow {
69    pub id: i64,
70    pub album_id: Option<i64>,
71    pub artist_id: Option<i64>,
72    pub artist_name: String,
73    pub album_artist_name: String,
74    pub album_title: String,
75    pub disc: Option<i32>,
76    pub track_number: Option<i32>,
77    pub title: String,
78    pub duration_ms: Option<i64>,
79    pub path: Option<String>,
80    pub codec: Option<String>,
81    pub sample_rate: Option<i32>,
82    pub bit_depth: Option<i32>,
83    pub channels: Option<i32>,
84    pub bitrate: Option<i32>,
85    pub genre: Option<String>,
86    pub source: String,
87    pub remote_id: Option<String>,
88    pub cached_path: Option<String>,
89}
90
91/// Where to get audio data for playback. Local always wins.
92#[derive(Debug, Clone)]
93pub enum PlaybackSource {
94    Local(PathBuf),
95    Cached(PathBuf),
96    Remote(String),
97}
98
99#[derive(Debug, Clone, Default)]
100pub struct LibraryStats {
101    pub total_tracks: i64,
102    pub local_tracks: i64,
103    pub remote_tracks: i64,
104    pub cached_tracks: i64,
105    pub total_albums: i64,
106    pub total_artists: i64,
107}
108
109/// Metadata for inserting/updating a track.
110#[derive(Debug, Clone)]
111pub struct TrackMeta {
112    pub title: String,
113    pub artist: String,
114    pub album_artist: Option<String>,
115    pub album: String,
116    pub date: Option<String>,
117    pub disc: Option<i32>,
118    pub track_number: Option<i32>,
119    pub genre: Option<String>,
120    pub label: Option<String>,
121    pub duration_ms: Option<i64>,
122    pub codec: Option<String>,
123    pub sample_rate: Option<i32>,
124    pub bit_depth: Option<i32>,
125    pub channels: Option<i32>,
126    pub bitrate: Option<i32>,
127    pub size_bytes: Option<i64>,
128    pub mtime: Option<i64>,
129    pub path: Option<String>,
130    pub source: String,
131    pub remote_id: Option<String>,
132    pub remote_url: Option<String>,
133    /// The server's ids for the album and its artist.
134    ///
135    /// Carried alongside the track's own, because the server keys stars,
136    /// shares and cover art off them — a library synced without these has
137    /// albums and artists it can name but cannot refer to.
138    pub album_remote_id: Option<String>,
139    pub artist_remote_id: Option<String>,
140    /// MusicBrainz recording id. From the server today; a local scan could
141    /// read it from `MUSICBRAINZ_TRACKID` too.
142    pub mbid: Option<String>,
143    /// When the album this track belongs to entered the library. Remote sync
144    /// supplies the server's `created`; anything else leaves it and the album
145    /// is stamped with the time it was first seen.
146    pub album_added_at: Option<String>,
147}
148
149/// Test helper: build a sample TrackMeta for use in tests across sub-modules.
150#[cfg(test)]
151pub fn sample_meta(title: &str, artist: &str, album: &str) -> TrackMeta {
152    TrackMeta {
153        title: title.into(),
154        artist: artist.into(),
155        album_artist: Some(artist.into()),
156        album: album.into(),
157        date: Some("2024".into()),
158        disc: Some(1),
159        track_number: Some(1),
160        genre: Some("Electronic".into()),
161        label: None,
162        duration_ms: Some(240_000),
163        codec: Some("FLAC".into()),
164        sample_rate: Some(44100),
165        bit_depth: Some(16),
166        channels: Some(2),
167        bitrate: Some(1000),
168        size_bytes: Some(30_000_000),
169        mtime: Some(1700000000),
170        path: Some(format!("/music/{}/{}.flac", album, title)),
171        source: "local".into(),
172        remote_id: None,
173        album_remote_id: None,
174        artist_remote_id: None,
175        mbid: None,
176        remote_url: None,
177        album_added_at: None,
178    }
179}