musicus/src/library.rs

318 lines
12 KiB
Rust
Raw Normal View History

2023-09-30 18:26:11 +02:00
use gtk::{glib, glib::Properties, prelude::*, subclass::prelude::*};
2023-10-07 22:49:20 +02:00
use rusqlite::{Connection, Row};
2023-09-30 18:26:11 +02:00
use std::{
2023-10-07 22:49:20 +02:00
cell::OnceCell,
path::{Path, PathBuf},
2023-09-30 18:26:11 +02:00
};
mod imp {
use super::*;
#[derive(Properties, Default)]
#[properties(wrapper_type = super::MusicusLibrary)]
pub struct MusicusLibrary {
2023-10-07 22:49:20 +02:00
#[property(get, construct_only)]
pub folder: OnceCell<String>,
pub connection: OnceCell<Connection>,
2023-09-30 18:26:11 +02:00
}
#[glib::object_subclass]
impl ObjectSubclass for MusicusLibrary {
const NAME: &'static str = "MusicusLibrary";
type Type = super::MusicusLibrary;
}
#[glib::derived_properties]
2023-10-07 22:49:20 +02:00
impl ObjectImpl for MusicusLibrary {
fn constructed(&self) {
self.parent_constructed();
let db_path = PathBuf::from(self.folder.get().unwrap()).join("musicus.db");
self.connection
.set(Connection::open(db_path).unwrap())
.unwrap();
}
}
2023-09-30 18:26:11 +02:00
}
glib::wrapper! {
pub struct MusicusLibrary(ObjectSubclass<imp::MusicusLibrary>);
}
impl MusicusLibrary {
pub fn new(path: impl AsRef<Path>) -> Self {
2023-10-07 22:49:20 +02:00
glib::Object::builder()
.property("folder", path.as_ref().to_str().unwrap())
.build()
}
2023-09-30 18:26:11 +02:00
2023-10-07 22:49:20 +02:00
pub fn query(&self, query: &LibraryQuery) -> LibraryResults {
let search = format!("%{}%", query.search);
2023-09-30 18:26:11 +02:00
2023-10-07 22:49:20 +02:00
match query {
LibraryQuery {
person: None,
ensemble: None,
work: None,
..
} => {
let persons = self.con()
2023-10-08 00:16:41 +02:00
.prepare("SELECT id, first_name, last_name FROM persons WHERE first_name LIKE ?1 OR last_name LIKE ?1 LIMIT 9")
2023-10-07 22:49:20 +02:00
.unwrap()
.query_map([&search], Person::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Person>>>()
.unwrap();
2023-09-30 18:26:11 +02:00
2023-10-08 00:16:41 +02:00
let ensembles = self
.con()
.prepare("SELECT id, name FROM ensembles WHERE name LIKE ?1 LIMIT 9")
.unwrap()
.query_map([&search], Ensemble::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Ensemble>>>()
.unwrap();
let works = self
.con()
.prepare("SELECT works.id, works.title, persons.id, persons.first_name, persons.last_name FROM works INNER JOIN persons ON works.composer = persons.id WHERE title LIKE ?1 LIMIT 9")
.unwrap()
.query_map([&search], Work::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Work>>>()
.unwrap();
2023-10-07 22:49:20 +02:00
LibraryResults {
persons,
2023-10-08 00:16:41 +02:00
ensembles,
works,
2023-10-07 22:49:20 +02:00
..Default::default()
}
}
2023-10-08 00:16:41 +02:00
LibraryQuery {
person: Some(person),
ensemble: None,
work: None,
..
} => {
let persons = self.con()
.prepare("SELECT DISTINCT persons.id, persons.first_name, persons.last_name FROM persons INNER JOIN performances ON performances.person = persons.id INNER JOIN recordings ON recordings.id = performances.recording INNER JOIN works ON works.id = recordings.work WHERE works.composer IS ?1 AND (persons.first_name LIKE ?2 OR persons.last_name LIKE ?2) LIMIT 9")
.unwrap()
.query_map([&person.id, &search], Person::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Person>>>()
.unwrap();
let ensembles = self
.con()
.prepare("SELECT DISTINCT ensembles.id, ensembles.name FROM ensembles INNER JOIN performances ON performances.ensemble = ensembles.id INNER JOIN recordings ON recordings.id = performances.recording INNER JOIN works ON works.id = recordings.work WHERE works.composer IS ?1 AND ensembles.name LIKE ?2 LIMIT 9")
.unwrap()
.query_map([&person.id, &search], Ensemble::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Ensemble>>>()
.unwrap();
let works = self
.con()
.prepare("SELECT DISTINCT works.id, works.title, persons.id, persons.first_name, persons.last_name FROM works INNER JOIN persons ON works.composer = persons.id WHERE works.composer = ?1 AND title LIKE ?2 LIMIT 9")
.unwrap()
.query_map([&person.id, &search], Work::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Work>>>()
.unwrap();
let recordings = self
.con()
.prepare("SELECT DISTINCT recordings.id, works.id, works.title, persons.id, persons.first_name, persons.last_name FROM recordings INNER JOIN works ON recordings.work = works.id INNER JOIN persons ON works.composer = persons.id INNER JOIN performances ON recordings.id = performances.recording WHERE performances.person IS ?1 AND (works.title LIKE ?2 OR persons.first_name LIKE ?2 OR persons.last_name LIKE ?2) LIMIT 9")
.unwrap()
.query_map([&person.id, &search], Recording::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Recording>>>()
.unwrap();
LibraryResults {
persons,
ensembles,
works,
recordings,
}
}
2023-10-08 14:50:27 +02:00
LibraryQuery {
person: None,
ensemble: Some(ensemble),
work: None,
..
} => {
let persons = self.con()
.prepare("SELECT DISTINCT persons.id, persons.first_name, persons.last_name FROM persons INNER JOIN works ON works.composer = persons.id INNER JOIN recordings ON recordings.work = works.id INNER JOIN performances ON performances.recording = recordings.id WHERE performances.ensemble IS ?1 AND (persons.first_name LIKE ?2 OR persons.last_name LIKE ?2) LIMIT 9")
.unwrap()
.query_map([&ensemble.id, &search], Person::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Person>>>()
.unwrap();
let recordings = self
.con()
.prepare("SELECT DISTINCT recordings.id, works.id, works.title, persons.id, persons.first_name, persons.last_name FROM recordings INNER JOIN works ON recordings.work = works.id INNER JOIN persons ON works.composer = persons.id INNER JOIN performances ON recordings.id = performances.recording WHERE performances.ensemble IS ?1 AND (works.title LIKE ?2 OR persons.first_name LIKE ?2 OR persons.last_name LIKE ?2) LIMIT 9")
.unwrap()
.query_map([&ensemble.id, &search], Recording::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Recording>>>()
.unwrap();
LibraryResults {
persons,
recordings,
..Default::default()
}
},
LibraryQuery {
person: Some(person),
ensemble: Some(ensemble),
work: None,
..
} => {
let recordings = self
.con()
.prepare("SELECT DISTINCT recordings.id, works.id, works.title, persons.id, persons.first_name, persons.last_name FROM recordings INNER JOIN works ON recordings.work = works.id INNER JOIN persons ON works.composer = persons.id INNER JOIN performances ON recordings.id = performances.recording WHERE works.composer IS ?1 AND performances.ensemble IS ?2 AND (works.title LIKE ?3 OR persons.first_name LIKE ?3 OR persons.last_name LIKE ?3) LIMIT 9")
.unwrap()
.query_map([&person.id, &ensemble.id, &search], Recording::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Recording>>>()
.unwrap();
LibraryResults {
recordings,
..Default::default()
}
},
LibraryQuery {
work: Some(work),
..
} => {
let recordings = self
.con()
.prepare("SELECT DISTINCT recordings.id, works.id, works.title, persons.id, persons.first_name, persons.last_name FROM recordings INNER JOIN works ON recordings.work = works.id INNER JOIN persons ON works.composer IS persons.id WHERE works.id IS ?1")
.unwrap()
.query_map([&work.id], Recording::from_row)
.unwrap()
.collect::<rusqlite::Result<Vec<Recording>>>()
.unwrap();
LibraryResults {
recordings,
..Default::default()
}
}
2023-10-07 22:49:20 +02:00
}
2023-09-30 18:26:11 +02:00
}
2023-10-07 22:49:20 +02:00
fn con(&self) -> &Connection {
2023-09-30 18:26:11 +02:00
self.imp().connection.get().unwrap()
}
}
2023-10-07 22:49:20 +02:00
2023-10-08 00:16:41 +02:00
#[derive(Default, Debug)]
2023-10-07 22:49:20 +02:00
pub struct LibraryQuery {
pub person: Option<Person>,
pub ensemble: Option<Ensemble>,
pub work: Option<Work>,
pub search: String,
}
2023-10-08 00:16:41 +02:00
#[derive(Default, Debug)]
2023-10-07 22:49:20 +02:00
pub struct LibraryResults {
pub persons: Vec<Person>,
pub ensembles: Vec<Ensemble>,
pub works: Vec<Work>,
pub recordings: Vec<Recording>,
}
impl LibraryResults {
pub fn is_empty(&self) -> bool {
2023-10-08 00:16:41 +02:00
self.persons.is_empty()
&& self.ensembles.is_empty()
&& self.works.is_empty()
&& self.recordings.is_empty()
2023-10-07 22:49:20 +02:00
}
}
2023-10-08 00:16:41 +02:00
#[derive(Debug, Clone)]
2023-10-07 22:49:20 +02:00
pub struct Person {
2023-10-08 00:16:41 +02:00
pub id: String,
2023-10-07 22:49:20 +02:00
pub first_name: String,
pub last_name: String,
}
impl Person {
pub fn from_row(row: &Row) -> rusqlite::Result<Self> {
Ok(Self {
2023-10-08 00:16:41 +02:00
id: row.get(0)?,
first_name: row.get(1)?,
last_name: row.get(2)?,
2023-10-07 22:49:20 +02:00
})
}
pub fn name_fl(&self) -> String {
format!("{} {}", self.first_name, self.last_name)
}
}
2023-10-08 00:16:41 +02:00
#[derive(Debug, Clone)]
2023-10-07 22:49:20 +02:00
pub struct Ensemble {
2023-10-08 00:16:41 +02:00
pub id: String,
2023-10-07 22:49:20 +02:00
pub name: String,
}
2023-10-08 00:16:41 +02:00
impl Ensemble {
pub fn from_row(row: &Row) -> rusqlite::Result<Self> {
Ok(Self {
id: row.get(0)?,
name: row.get(1)?,
})
}
}
#[derive(Debug, Clone)]
2023-10-07 22:49:20 +02:00
pub struct Work {
2023-10-08 00:16:41 +02:00
pub id: String,
2023-10-07 22:49:20 +02:00
pub title: String,
2023-10-08 00:16:41 +02:00
pub composer: Person,
2023-10-07 22:49:20 +02:00
}
2023-10-08 00:16:41 +02:00
impl Work {
pub fn from_row(row: &Row) -> rusqlite::Result<Self> {
Ok(Self {
id: row.get(0)?,
title: row.get(1)?,
composer: Person {
id: row.get(2)?,
first_name: row.get(3)?,
last_name: row.get(4)?,
},
})
}
}
#[derive(Debug, Clone)]
pub struct Recording {
pub id: String,
pub work: Work,
}
impl Recording {
pub fn from_row(row: &Row) -> rusqlite::Result<Self> {
Ok(Self {
id: row.get(0)?,
work: Work {
id: row.get(1)?,
title: row.get(2)?,
composer: Person {
id: row.get(3)?,
first_name: row.get(4)?,
last_name: row.get(5)?,
},
},
})
}
}