mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 19:57:25 +01:00
Move crates to subdirectory
This commit is contained in:
parent
1db96062fb
commit
ac4b29e86d
115 changed files with 10 additions and 5 deletions
1
crates/database/.gitignore
vendored
Normal file
1
crates/database/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
test.sqlite
|
||||
13
crates/database/Cargo.toml
Normal file
13
crates/database/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "musicus_database"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
diesel = { version = "1.4.8", features = ["sqlite"] }
|
||||
diesel_migrations = "1.4.0"
|
||||
chrono = "0.4.19"
|
||||
log = "0.4.16"
|
||||
rand = "0.8.5"
|
||||
thiserror = "1.0.31"
|
||||
uuid = { version = "1.0.0", features = ["v4"] }
|
||||
2
crates/database/diesel.toml
Normal file
2
crates/database/diesel.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[print_schema]
|
||||
file = "src/schema.rs"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
PRAGMA defer_foreign_keys;
|
||||
|
||||
DROP TABLE "persons";
|
||||
DROP TABLE "instruments";
|
||||
DROP TABLE "works";
|
||||
DROP TABLE "instrumentations";
|
||||
DROP TABLE "work_parts";
|
||||
DROP TABLE "ensembles";
|
||||
DROP TABLE "recordings";
|
||||
DROP TABLE "performances";
|
||||
DROP TABLE "mediums";
|
||||
DROP TABLE "tracks";
|
||||
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
CREATE TABLE "persons" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"first_name" TEXT NOT NULL,
|
||||
"last_name" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "instruments" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"name" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "works" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"composer" TEXT NOT NULL REFERENCES "persons"("id"),
|
||||
"title" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "instrumentations" (
|
||||
"id" BIGINT NOT NULL PRIMARY KEY,
|
||||
"work" TEXT NOT NULL REFERENCES "works"("id") ON DELETE CASCADE,
|
||||
"instrument" TEXT NOT NULL REFERENCES "instruments"("id") ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE "work_parts" (
|
||||
"id" BIGINT NOT NULL PRIMARY KEY,
|
||||
"work" TEXT NOT NULL REFERENCES "works"("id") ON DELETE CASCADE,
|
||||
"part_index" BIGINT NOT NULL,
|
||||
"title" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "ensembles" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"name" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "recordings" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"work" TEXT NOT NULL REFERENCES "works"("id"),
|
||||
"comment" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "performances" (
|
||||
"id" BIGINT NOT NULL PRIMARY KEY,
|
||||
"recording" TEXT NOT NULL REFERENCES "recordings"("id") ON DELETE CASCADE,
|
||||
"person" TEXT REFERENCES "persons"("id"),
|
||||
"ensemble" TEXT REFERENCES "ensembles"("id"),
|
||||
"role" TEXT REFERENCES "instruments"("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "mediums" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"name" TEXT NOT NULL,
|
||||
"discid" TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE "tracks" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"medium" TEXT NOT NULL REFERENCES "mediums"("id") ON DELETE CASCADE,
|
||||
"index" INTEGER NOT NULL,
|
||||
"recording" TEXT NOT NULL REFERENCES "recordings"("id"),
|
||||
"work_parts" TEXT NOT NULL,
|
||||
"source_index" INTEGER NOT NULL,
|
||||
"path" TEXT NOT NULL
|
||||
);
|
||||
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
ALTER TABLE "persons" DROP COLUMN "last_used";
|
||||
ALTER TABLE "persons" DROP COLUMN "last_played";
|
||||
|
||||
ALTER TABLE "instruments" DROP COLUMN "last_used";
|
||||
ALTER TABLE "instruments" DROP COLUMN "last_played";
|
||||
|
||||
ALTER TABLE "works" DROP COLUMN "last_used";
|
||||
ALTER TABLE "works" DROP COLUMN "last_played";
|
||||
|
||||
ALTER TABLE "ensembles" DROP COLUMN "last_used";
|
||||
ALTER TABLE "ensembles" DROP COLUMN "last_played";
|
||||
|
||||
ALTER TABLE "recordings" DROP COLUMN "last_used";
|
||||
ALTER TABLE "recordings" DROP COLUMN "last_played";
|
||||
|
||||
ALTER TABLE "mediums" DROP COLUMN "last_used";
|
||||
ALTER TABLE "mediums" DROP COLUMN "last_played";
|
||||
|
||||
ALTER TABLE "tracks" DROP COLUMN "last_used";
|
||||
ALTER TABLE "tracks" DROP COLUMN "last_played";
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
ALTER TABLE "persons" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "persons" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
ALTER TABLE "instruments" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "instruments" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
ALTER TABLE "works" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "works" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
ALTER TABLE "ensembles" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "ensembles" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
ALTER TABLE "recordings" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "recordings" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
ALTER TABLE "mediums" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "mediums" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
ALTER TABLE "tracks" ADD COLUMN "last_used" BIGINT;
|
||||
ALTER TABLE "tracks" ADD COLUMN "last_played" BIGINT;
|
||||
|
||||
76
crates/database/src/ensembles.rs
Normal file
76
crates/database/src/ensembles.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
use super::schema::ensembles;
|
||||
use super::{Database, Result};
|
||||
use chrono::Utc;
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
/// An ensemble that takes part in recordings.
|
||||
#[derive(Insertable, Queryable, PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Ensemble {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
impl Ensemble {
|
||||
pub fn new(id: String, name: String) -> Self {
|
||||
Self {
|
||||
id,
|
||||
name,
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Update an existing ensemble or insert a new one.
|
||||
pub fn update_ensemble(&self, mut ensemble: Ensemble) -> Result<()> {
|
||||
info!("Updating ensemble {:?}", ensemble);
|
||||
self.defer_foreign_keys()?;
|
||||
|
||||
ensemble.last_used = Some(Utc::now().timestamp());
|
||||
|
||||
self.connection.transaction(|| {
|
||||
diesel::replace_into(ensembles::table)
|
||||
.values(ensemble)
|
||||
.execute(&self.connection)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get an existing ensemble.
|
||||
pub fn get_ensemble(&self, id: &str) -> Result<Option<Ensemble>> {
|
||||
let ensemble = ensembles::table
|
||||
.filter(ensembles::id.eq(id))
|
||||
.load::<Ensemble>(&self.connection)?
|
||||
.into_iter()
|
||||
.next();
|
||||
|
||||
Ok(ensemble)
|
||||
}
|
||||
|
||||
/// Delete an existing ensemble.
|
||||
pub fn delete_ensemble(&self, id: &str) -> Result<()> {
|
||||
info!("Deleting ensemble {}", id);
|
||||
diesel::delete(ensembles::table.filter(ensembles::id.eq(id))).execute(&self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all existing ensembles.
|
||||
pub fn get_ensembles(&self) -> Result<Vec<Ensemble>> {
|
||||
let ensembles = ensembles::table.load::<Ensemble>(&self.connection)?;
|
||||
Ok(ensembles)
|
||||
}
|
||||
|
||||
/// Get recently used ensembles.
|
||||
pub fn get_recent_ensembles(&self) -> Result<Vec<Ensemble>> {
|
||||
let ensembles = ensembles::table
|
||||
.order(ensembles::last_used.desc())
|
||||
.load::<Ensemble>(&self.connection)?;
|
||||
|
||||
Ok(ensembles)
|
||||
}
|
||||
}
|
||||
24
crates/database/src/error.rs
Normal file
24
crates/database/src/error.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/// Error that happens within the database module.
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
ConnectionError(#[from] diesel::result::ConnectionError),
|
||||
|
||||
#[error(transparent)]
|
||||
MigrationsError(#[from] diesel_migrations::RunMigrationsError),
|
||||
|
||||
#[error(transparent)]
|
||||
QueryError(#[from] diesel::result::Error),
|
||||
|
||||
#[error("Missing item dependency ({0} {1})")]
|
||||
MissingItem(&'static str, String),
|
||||
|
||||
#[error("Failed to parse {0} from '{1}'")]
|
||||
ParsingError(&'static str, String),
|
||||
|
||||
#[error("{0}")]
|
||||
Other(&'static str),
|
||||
}
|
||||
|
||||
/// Return type for database methods.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
79
crates/database/src/instruments.rs
Normal file
79
crates/database/src/instruments.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
use super::schema::instruments;
|
||||
use super::{Database, Result};
|
||||
use chrono::Utc;
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
/// An instrument or any other possible role within a recording.
|
||||
#[derive(Insertable, Queryable, PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Instrument {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
impl Instrument {
|
||||
pub fn new(id: String, name: String) -> Self {
|
||||
Self {
|
||||
id,
|
||||
name,
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Update an existing instrument or insert a new one.
|
||||
pub fn update_instrument(&self, mut instrument: Instrument) -> Result<()> {
|
||||
info!("Updating instrument {:?}", instrument);
|
||||
self.defer_foreign_keys()?;
|
||||
|
||||
instrument.last_used = Some(Utc::now().timestamp());
|
||||
|
||||
self.connection.transaction(|| {
|
||||
diesel::replace_into(instruments::table)
|
||||
.values(instrument)
|
||||
.execute(&self.connection)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get an existing instrument.
|
||||
pub fn get_instrument(&self, id: &str) -> Result<Option<Instrument>> {
|
||||
let instrument = instruments::table
|
||||
.filter(instruments::id.eq(id))
|
||||
.load::<Instrument>(&self.connection)?
|
||||
.into_iter()
|
||||
.next();
|
||||
|
||||
Ok(instrument)
|
||||
}
|
||||
|
||||
/// Delete an existing instrument.
|
||||
pub fn delete_instrument(&self, id: &str) -> Result<()> {
|
||||
info!("Deleting instrument {}", id);
|
||||
diesel::delete(instruments::table.filter(instruments::id.eq(id)))
|
||||
.execute(&self.connection)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all existing instruments.
|
||||
pub fn get_instruments(&self) -> Result<Vec<Instrument>> {
|
||||
let instruments = instruments::table.load::<Instrument>(&self.connection)?;
|
||||
|
||||
Ok(instruments)
|
||||
}
|
||||
|
||||
/// Get recently used instruments.
|
||||
pub fn get_recent_instruments(&self) -> Result<Vec<Instrument>> {
|
||||
let instruments = instruments::table
|
||||
.order(instruments::last_used.desc())
|
||||
.load::<Instrument>(&self.connection)?;
|
||||
|
||||
Ok(instruments)
|
||||
}
|
||||
}
|
||||
66
crates/database/src/lib.rs
Normal file
66
crates/database/src/lib.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// Required for schema.rs
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
// Required for embed_migrations macro in database.rs
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
pub mod ensembles;
|
||||
pub use ensembles::*;
|
||||
|
||||
pub mod error;
|
||||
pub use error::*;
|
||||
|
||||
pub mod instruments;
|
||||
pub use instruments::*;
|
||||
|
||||
pub mod medium;
|
||||
pub use medium::*;
|
||||
|
||||
pub mod persons;
|
||||
pub use persons::*;
|
||||
|
||||
pub mod recordings;
|
||||
pub use recordings::*;
|
||||
|
||||
pub mod works;
|
||||
pub use works::*;
|
||||
|
||||
mod schema;
|
||||
|
||||
// This makes the SQL migration scripts accessible from the code.
|
||||
embed_migrations!();
|
||||
|
||||
/// Generate a random string suitable as an item ID.
|
||||
pub fn generate_id() -> String {
|
||||
uuid::Uuid::new_v4().simple().to_string()
|
||||
}
|
||||
|
||||
/// Interface to a Musicus database.
|
||||
pub struct Database {
|
||||
connection: SqliteConnection,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Create a new database interface and run migrations if necessary.
|
||||
pub fn new(file_name: &str) -> Result<Database> {
|
||||
info!("Opening database file '{}'", file_name);
|
||||
let connection = SqliteConnection::establish(file_name)?;
|
||||
diesel::sql_query("PRAGMA foreign_keys = ON").execute(&connection)?;
|
||||
|
||||
info!("Running migrations if necessary");
|
||||
embedded_migrations::run(&connection)?;
|
||||
|
||||
Ok(Database { connection })
|
||||
}
|
||||
|
||||
/// Defer all foreign keys for the next transaction.
|
||||
fn defer_foreign_keys(&self) -> Result<()> {
|
||||
diesel::sql_query("PRAGMA defer_foreign_keys = ON").execute(&self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
337
crates/database/src/medium.rs
Normal file
337
crates/database/src/medium.rs
Normal file
|
|
@ -0,0 +1,337 @@
|
|||
use super::generate_id;
|
||||
use super::schema::{ensembles, mediums, performances, persons, recordings, tracks};
|
||||
use super::{Database, Error, Recording, Result};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
/// Representation of someting like a physical audio disc or a folder with
|
||||
/// audio files (i.e. a collection of tracks for one or more recordings).
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Medium {
|
||||
/// An unique ID for the medium.
|
||||
pub id: String,
|
||||
|
||||
/// The human identifier for the medium.
|
||||
pub name: String,
|
||||
|
||||
/// If applicable, the MusicBrainz DiscID.
|
||||
pub discid: Option<String>,
|
||||
|
||||
/// The tracks of the medium.
|
||||
pub tracks: Vec<Track>,
|
||||
|
||||
pub last_used: Option<DateTime<Utc>>,
|
||||
pub last_played: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl Medium {
|
||||
pub fn new(id: String, name: String, discid: Option<String>, tracks: Vec<Track>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
name,
|
||||
discid,
|
||||
tracks,
|
||||
last_used: Some(Utc::now()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A track on a medium.
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Track {
|
||||
/// The recording on this track.
|
||||
pub recording: Recording,
|
||||
|
||||
/// The work parts that are played on this track. They are indices to the
|
||||
/// work parts of the work that is associated with the recording.
|
||||
pub work_parts: Vec<usize>,
|
||||
|
||||
/// The index of the track within its source. This is used to associate
|
||||
/// the metadata with the audio data from the source when importing.
|
||||
pub source_index: usize,
|
||||
|
||||
/// The path to the audio file containing this track.
|
||||
pub path: String,
|
||||
|
||||
pub last_used: Option<DateTime<Utc>>,
|
||||
pub last_played: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl Track {
|
||||
pub fn new(recording: Recording, work_parts: Vec<usize>, source_index: usize, path: String) -> Self {
|
||||
Self {
|
||||
recording,
|
||||
work_parts,
|
||||
source_index,
|
||||
path,
|
||||
last_used: Some(Utc::now()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Table data for a [`Medium`].
|
||||
#[derive(Insertable, Queryable, Debug, Clone)]
|
||||
#[table_name = "mediums"]
|
||||
struct MediumRow {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub discid: Option<String>,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
/// Table data for a [`Track`].
|
||||
#[derive(Insertable, Queryable, QueryableByName, Debug, Clone)]
|
||||
#[table_name = "tracks"]
|
||||
struct TrackRow {
|
||||
pub id: String,
|
||||
pub medium: String,
|
||||
pub index: i32,
|
||||
pub recording: String,
|
||||
pub work_parts: String,
|
||||
pub source_index: i32,
|
||||
pub path: String,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Update an existing medium or insert a new one.
|
||||
pub fn update_medium(&self, medium: Medium) -> Result<()> {
|
||||
info!("Updating medium {:?}", medium);
|
||||
self.defer_foreign_keys()?;
|
||||
|
||||
self.connection.transaction::<(), Error, _>(|| {
|
||||
let medium_id = &medium.id;
|
||||
|
||||
// This will also delete the tracks.
|
||||
self.delete_medium(medium_id)?;
|
||||
|
||||
// Add the new medium.
|
||||
|
||||
let medium_row = MediumRow {
|
||||
id: medium_id.to_owned(),
|
||||
name: medium.name.clone(),
|
||||
discid: medium.discid.clone(),
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: medium.last_played.map(|t| t.timestamp()),
|
||||
};
|
||||
|
||||
diesel::insert_into(mediums::table)
|
||||
.values(medium_row)
|
||||
.execute(&self.connection)?;
|
||||
|
||||
for (index, track) in medium.tracks.iter().enumerate() {
|
||||
// Add associated items from the server, if they don't already exist.
|
||||
|
||||
if self.get_recording(&track.recording.id)?.is_none() {
|
||||
self.update_recording(track.recording.clone())?;
|
||||
}
|
||||
|
||||
// Add the actual track data.
|
||||
|
||||
let work_parts = track
|
||||
.work_parts
|
||||
.iter()
|
||||
.map(|part_index| part_index.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(",");
|
||||
|
||||
let track_row = TrackRow {
|
||||
id: generate_id(),
|
||||
medium: medium_id.to_owned(),
|
||||
index: index as i32,
|
||||
recording: track.recording.id.clone(),
|
||||
work_parts,
|
||||
source_index: track.source_index as i32,
|
||||
path: track.path.clone(),
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: track.last_played.map(|t| t.timestamp()),
|
||||
};
|
||||
|
||||
diesel::insert_into(tracks::table)
|
||||
.values(track_row)
|
||||
.execute(&self.connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get an existing medium.
|
||||
pub fn get_medium(&self, id: &str) -> Result<Option<Medium>> {
|
||||
let row = mediums::table
|
||||
.filter(mediums::id.eq(id))
|
||||
.load::<MediumRow>(&self.connection)?
|
||||
.into_iter()
|
||||
.next();
|
||||
|
||||
let medium = match row {
|
||||
Some(row) => Some(self.get_medium_data(row)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(medium)
|
||||
}
|
||||
|
||||
/// Get mediums that have a specific source ID.
|
||||
pub fn get_mediums_by_source_id(&self, source_id: &str) -> Result<Vec<Medium>> {
|
||||
let mut mediums: Vec<Medium> = Vec::new();
|
||||
|
||||
let rows = mediums::table
|
||||
.filter(mediums::discid.nullable().eq(source_id))
|
||||
.load::<MediumRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
let medium = self.get_medium_data(row)?;
|
||||
mediums.push(medium);
|
||||
}
|
||||
|
||||
Ok(mediums)
|
||||
}
|
||||
|
||||
/// Get mediums on which this person is performing.
|
||||
pub fn get_mediums_for_person(&self, person_id: &str) -> Result<Vec<Medium>> {
|
||||
let mut mediums: Vec<Medium> = Vec::new();
|
||||
|
||||
let rows = mediums::table
|
||||
.inner_join(tracks::table.on(tracks::medium.eq(mediums::id)))
|
||||
.inner_join(recordings::table.on(recordings::id.eq(tracks::recording)))
|
||||
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
|
||||
.inner_join(persons::table.on(persons::id.nullable().eq(performances::person)))
|
||||
.filter(persons::id.eq(person_id))
|
||||
.select(mediums::table::all_columns())
|
||||
.distinct()
|
||||
.load::<MediumRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
let medium = self.get_medium_data(row)?;
|
||||
mediums.push(medium);
|
||||
}
|
||||
|
||||
Ok(mediums)
|
||||
}
|
||||
|
||||
/// Get mediums on which this ensemble is performing.
|
||||
pub fn get_mediums_for_ensemble(&self, ensemble_id: &str) -> Result<Vec<Medium>> {
|
||||
let mut mediums: Vec<Medium> = Vec::new();
|
||||
|
||||
let rows = mediums::table
|
||||
.inner_join(tracks::table.on(tracks::medium.eq(tracks::id)))
|
||||
.inner_join(recordings::table.on(recordings::id.eq(tracks::recording)))
|
||||
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
|
||||
.inner_join(ensembles::table.on(ensembles::id.nullable().eq(performances::ensemble)))
|
||||
.filter(ensembles::id.eq(ensemble_id))
|
||||
.select(mediums::table::all_columns())
|
||||
.distinct()
|
||||
.load::<MediumRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
let medium = self.get_medium_data(row)?;
|
||||
mediums.push(medium);
|
||||
}
|
||||
|
||||
Ok(mediums)
|
||||
}
|
||||
|
||||
/// Delete a medium and all of its tracks. This will fail, if the music
|
||||
/// library contains audio files referencing any of those tracks.
|
||||
pub fn delete_medium(&self, id: &str) -> Result<()> {
|
||||
info!("Deleting medium {}", id);
|
||||
diesel::delete(mediums::table.filter(mediums::id.eq(id))).execute(&self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all available tracks for a recording.
|
||||
pub fn get_tracks(&self, recording_id: &str) -> Result<Vec<Track>> {
|
||||
let mut tracks: Vec<Track> = Vec::new();
|
||||
|
||||
let rows = tracks::table
|
||||
.inner_join(recordings::table.on(recordings::id.eq(tracks::recording)))
|
||||
.filter(recordings::id.eq(recording_id))
|
||||
.select(tracks::table::all_columns())
|
||||
.load::<TrackRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
let track = self.get_track_from_row(row)?;
|
||||
tracks.push(track);
|
||||
}
|
||||
|
||||
Ok(tracks)
|
||||
}
|
||||
|
||||
/// Get a random track from the database.
|
||||
pub fn random_track(&self) -> Result<Track> {
|
||||
let row = diesel::sql_query("SELECT * FROM tracks ORDER BY RANDOM() LIMIT 1")
|
||||
.load::<TrackRow>(&self.connection)?
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or(Error::Other("Failed to generate random track"))?;
|
||||
|
||||
self.get_track_from_row(row)
|
||||
}
|
||||
|
||||
/// Retrieve all available information on a medium from related tables.
|
||||
fn get_medium_data(&self, row: MediumRow) -> Result<Medium> {
|
||||
let track_rows = tracks::table
|
||||
.filter(tracks::medium.eq(&row.id))
|
||||
.order_by(tracks::index)
|
||||
.load::<TrackRow>(&self.connection)?;
|
||||
|
||||
let mut tracks = Vec::new();
|
||||
|
||||
for track_row in track_rows {
|
||||
let track = self.get_track_from_row(track_row)?;
|
||||
tracks.push(track);
|
||||
}
|
||||
|
||||
let medium = Medium {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
discid: row.discid,
|
||||
tracks,
|
||||
last_used: row.last_used.map(|t| Utc.timestamp(t, 0)),
|
||||
last_played: row.last_played.map(|t| Utc.timestamp(t, 0)),
|
||||
};
|
||||
|
||||
Ok(medium)
|
||||
}
|
||||
|
||||
/// Convert a track row from the database to an actual track.
|
||||
fn get_track_from_row(&self, row: TrackRow) -> Result<Track> {
|
||||
let recording_id = row.recording;
|
||||
|
||||
let recording = self
|
||||
.get_recording(&recording_id)?
|
||||
.ok_or(Error::MissingItem("recording", recording_id))?;
|
||||
|
||||
let mut part_indices = Vec::new();
|
||||
|
||||
let work_parts = row.work_parts.split(',');
|
||||
|
||||
for part_index in work_parts {
|
||||
if !part_index.is_empty() {
|
||||
let index = str::parse(part_index)
|
||||
.map_err(|_| Error::ParsingError("part index", String::from(part_index)))?;
|
||||
|
||||
part_indices.push(index);
|
||||
}
|
||||
}
|
||||
|
||||
let track = Track {
|
||||
recording,
|
||||
work_parts: part_indices,
|
||||
source_index: row.source_index as usize,
|
||||
path: row.path,
|
||||
last_used: row.last_used.map(|t| Utc.timestamp(t, 0)),
|
||||
last_played: row.last_played.map(|t| Utc.timestamp(t, 0)),
|
||||
};
|
||||
|
||||
Ok(track)
|
||||
}
|
||||
}
|
||||
89
crates/database/src/persons.rs
Normal file
89
crates/database/src/persons.rs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
use super::schema::persons;
|
||||
use super::{Database, Result};
|
||||
use chrono::Utc;
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
/// A person that is a composer, an interpret or both.
|
||||
#[derive(Insertable, Queryable, PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Person {
|
||||
pub id: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
impl Person {
|
||||
pub fn new(id: String, first_name: String, last_name: String) -> Self {
|
||||
Self {
|
||||
id,
|
||||
first_name,
|
||||
last_name,
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the full name in the form "First Last".
|
||||
pub fn name_fl(&self) -> String {
|
||||
format!("{} {}", self.first_name, self.last_name)
|
||||
}
|
||||
|
||||
/// Get the full name in the form "Last, First".
|
||||
pub fn name_lf(&self) -> String {
|
||||
format!("{}, {}", self.last_name, self.first_name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Update an existing person or insert a new one.
|
||||
pub fn update_person(&self, mut person: Person) -> Result<()> {
|
||||
info!("Updating person {:?}", person);
|
||||
self.defer_foreign_keys()?;
|
||||
|
||||
person.last_used = Some(Utc::now().timestamp());
|
||||
|
||||
self.connection.transaction(|| {
|
||||
diesel::replace_into(persons::table)
|
||||
.values(person)
|
||||
.execute(&self.connection)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get an existing person.
|
||||
pub fn get_person(&self, id: &str) -> Result<Option<Person>> {
|
||||
let person = persons::table
|
||||
.filter(persons::id.eq(id))
|
||||
.load::<Person>(&self.connection)?
|
||||
.into_iter()
|
||||
.next();
|
||||
|
||||
Ok(person)
|
||||
}
|
||||
|
||||
/// Delete an existing person.
|
||||
pub fn delete_person(&self, id: &str) -> Result<()> {
|
||||
info!("Deleting person {}", id);
|
||||
diesel::delete(persons::table.filter(persons::id.eq(id))).execute(&self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all existing persons.
|
||||
pub fn get_persons(&self) -> Result<Vec<Person>> {
|
||||
let persons = persons::table.load::<Person>(&self.connection)?;
|
||||
|
||||
Ok(persons)
|
||||
}
|
||||
|
||||
/// Get recently used persons.
|
||||
pub fn get_recent_persons(&self) -> Result<Vec<Person>> {
|
||||
let persons = persons::table
|
||||
.order(persons::last_used.desc())
|
||||
.load::<Person>(&self.connection)?;
|
||||
|
||||
Ok(persons)
|
||||
}
|
||||
}
|
||||
344
crates/database/src/recordings.rs
Normal file
344
crates/database/src/recordings.rs
Normal file
|
|
@ -0,0 +1,344 @@
|
|||
use super::generate_id;
|
||||
use super::schema::{ensembles, performances, persons, recordings};
|
||||
use super::{Database, Ensemble, Error, Instrument, Person, Result, Work};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
/// A specific recording of a work.
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Recording {
|
||||
pub id: String,
|
||||
pub work: Work,
|
||||
pub comment: String,
|
||||
pub performances: Vec<Performance>,
|
||||
pub last_used: Option<DateTime<Utc>>,
|
||||
pub last_played: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl Recording {
|
||||
pub fn new(id: String, work: Work, comment: String, performances: Vec<Performance>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
work,
|
||||
comment,
|
||||
performances,
|
||||
last_used: Some(Utc::now()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize a new recording with a work.
|
||||
pub fn from_work(work: Work) -> Self {
|
||||
Self {
|
||||
id: generate_id(),
|
||||
work,
|
||||
comment: String::new(),
|
||||
performances: Vec::new(),
|
||||
last_used: Some(Utc::now()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a string representation of the performances in this recording.
|
||||
// TODO: Maybe replace with impl Display?
|
||||
pub fn get_performers(&self) -> String {
|
||||
let texts: Vec<String> = self
|
||||
.performances
|
||||
.iter()
|
||||
.map(|performance| performance.get_title())
|
||||
.collect();
|
||||
|
||||
texts.join(", ")
|
||||
}
|
||||
}
|
||||
|
||||
/// How a person or ensemble was involved in a recording.
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Performance {
|
||||
pub performer: PersonOrEnsemble,
|
||||
pub role: Option<Instrument>,
|
||||
}
|
||||
|
||||
impl Performance {
|
||||
/// Get a string representation of the performance.
|
||||
// TODO: Replace with impl Display.
|
||||
pub fn get_title(&self) -> String {
|
||||
let performer_title = self.performer.get_title();
|
||||
|
||||
if let Some(role) = &self.role {
|
||||
format!("{} ({})", performer_title, role.name)
|
||||
} else {
|
||||
performer_title
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Either a person or an ensemble.
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
|
||||
pub enum PersonOrEnsemble {
|
||||
Person(Person),
|
||||
Ensemble(Ensemble),
|
||||
}
|
||||
|
||||
impl PersonOrEnsemble {
|
||||
/// Get a short textual representation of the item.
|
||||
pub fn get_title(&self) -> String {
|
||||
match self {
|
||||
PersonOrEnsemble::Person(person) => person.name_lf(),
|
||||
PersonOrEnsemble::Ensemble(ensemble) => ensemble.name.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Database table data for a recording.
|
||||
#[derive(Insertable, Queryable, QueryableByName, Debug, Clone)]
|
||||
#[table_name = "recordings"]
|
||||
struct RecordingRow {
|
||||
pub id: String,
|
||||
pub work: String,
|
||||
pub comment: String,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
impl From<Recording> for RecordingRow {
|
||||
fn from(recording: Recording) -> Self {
|
||||
RecordingRow {
|
||||
id: recording.id,
|
||||
work: recording.work.id,
|
||||
comment: recording.comment,
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: recording.last_played.map(|t| t.timestamp()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Database table data for a performance.
|
||||
#[derive(Insertable, Queryable, Debug, Clone)]
|
||||
#[table_name = "performances"]
|
||||
struct PerformanceRow {
|
||||
pub id: i64,
|
||||
pub recording: String,
|
||||
pub person: Option<String>,
|
||||
pub ensemble: Option<String>,
|
||||
pub role: Option<String>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Update an existing recording or insert a new one.
|
||||
// TODO: Think about whether to also insert the other items.
|
||||
pub fn update_recording(&self, recording: Recording) -> Result<()> {
|
||||
info!("Updating recording {:?}", recording);
|
||||
self.defer_foreign_keys()?;
|
||||
self.connection.transaction::<(), Error, _>(|| {
|
||||
let recording_id = &recording.id;
|
||||
self.delete_recording(recording_id)?;
|
||||
|
||||
// Add associated items from the server, if they don't already exist.
|
||||
|
||||
if self.get_work(&recording.work.id)?.is_none() {
|
||||
self.update_work(recording.work.clone())?;
|
||||
}
|
||||
|
||||
for performance in &recording.performances {
|
||||
match &performance.performer {
|
||||
PersonOrEnsemble::Person(person) => {
|
||||
if self.get_person(&person.id)?.is_none() {
|
||||
self.update_person(person.clone())?;
|
||||
}
|
||||
}
|
||||
PersonOrEnsemble::Ensemble(ensemble) => {
|
||||
if self.get_ensemble(&ensemble.id)?.is_none() {
|
||||
self.update_ensemble(ensemble.clone())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(role) = &performance.role {
|
||||
if self.get_instrument(&role.id)?.is_none() {
|
||||
self.update_instrument(role.clone())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the actual recording.
|
||||
|
||||
let row: RecordingRow = recording.clone().into();
|
||||
diesel::insert_into(recordings::table)
|
||||
.values(row)
|
||||
.execute(&self.connection)?;
|
||||
|
||||
for performance in recording.performances {
|
||||
let (person, ensemble) = match performance.performer {
|
||||
PersonOrEnsemble::Person(person) => (Some(person.id), None),
|
||||
PersonOrEnsemble::Ensemble(ensemble) => (None, Some(ensemble.id)),
|
||||
};
|
||||
|
||||
let row = PerformanceRow {
|
||||
id: rand::random(),
|
||||
recording: recording_id.to_string(),
|
||||
person,
|
||||
ensemble,
|
||||
role: performance.role.map(|role| role.id),
|
||||
};
|
||||
|
||||
diesel::insert_into(performances::table)
|
||||
.values(row)
|
||||
.execute(&self.connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check whether the database contains a recording.
|
||||
pub fn recording_exists(&self, id: &str) -> Result<bool> {
|
||||
let exists = recordings::table
|
||||
.filter(recordings::id.eq(id))
|
||||
.load::<RecordingRow>(&self.connection)?
|
||||
.first()
|
||||
.is_some();
|
||||
|
||||
Ok(exists)
|
||||
}
|
||||
|
||||
/// Get an existing recording.
|
||||
pub fn get_recording(&self, id: &str) -> Result<Option<Recording>> {
|
||||
let row = recordings::table
|
||||
.filter(recordings::id.eq(id))
|
||||
.load::<RecordingRow>(&self.connection)?
|
||||
.into_iter()
|
||||
.next();
|
||||
|
||||
let recording = match row {
|
||||
Some(row) => Some(self.get_recording_data(row)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(recording)
|
||||
}
|
||||
|
||||
/// Get a random recording from the database.
|
||||
pub fn random_recording(&self) -> Result<Recording> {
|
||||
let row = diesel::sql_query("SELECT * FROM recordings ORDER BY RANDOM() LIMIT 1")
|
||||
.load::<RecordingRow>(&self.connection)?
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or(Error::Other("Failed to find random recording."))?;
|
||||
|
||||
self.get_recording_data(row)
|
||||
}
|
||||
|
||||
/// Retrieve all available information on a recording from related tables.
|
||||
fn get_recording_data(&self, row: RecordingRow) -> Result<Recording> {
|
||||
let mut performance_descriptions: Vec<Performance> = Vec::new();
|
||||
|
||||
let performance_rows = performances::table
|
||||
.filter(performances::recording.eq(&row.id))
|
||||
.load::<PerformanceRow>(&self.connection)?;
|
||||
|
||||
for row in performance_rows {
|
||||
performance_descriptions.push(Performance {
|
||||
performer: if let Some(id) = row.person {
|
||||
PersonOrEnsemble::Person(
|
||||
self.get_person(&id)?
|
||||
.ok_or(Error::MissingItem("person", id))?,
|
||||
)
|
||||
} else if let Some(id) = row.ensemble {
|
||||
PersonOrEnsemble::Ensemble(
|
||||
self.get_ensemble(&id)?
|
||||
.ok_or(Error::MissingItem("ensemble", id))?,
|
||||
)
|
||||
} else {
|
||||
return Err(Error::Other("Performance without performer"));
|
||||
},
|
||||
role: match row.role {
|
||||
Some(id) => Some(
|
||||
self.get_instrument(&id)?
|
||||
.ok_or(Error::MissingItem("instrument", id))?,
|
||||
),
|
||||
None => None,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let work_id = row.work;
|
||||
let work = self
|
||||
.get_work(&work_id)?
|
||||
.ok_or(Error::MissingItem("work", work_id))?;
|
||||
|
||||
let recording_description = Recording {
|
||||
id: row.id,
|
||||
work,
|
||||
comment: row.comment,
|
||||
performances: performance_descriptions,
|
||||
last_used: row.last_used.map(|t| Utc.timestamp(t, 0)),
|
||||
last_played: row.last_played.map(|t| Utc.timestamp(t, 0)),
|
||||
};
|
||||
|
||||
Ok(recording_description)
|
||||
}
|
||||
|
||||
/// Get all available information on all recordings where a person is performing.
|
||||
pub fn get_recordings_for_person(&self, person_id: &str) -> Result<Vec<Recording>> {
|
||||
let mut recordings: Vec<Recording> = Vec::new();
|
||||
|
||||
let rows = recordings::table
|
||||
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
|
||||
.inner_join(persons::table.on(persons::id.nullable().eq(performances::person)))
|
||||
.filter(persons::id.eq(person_id))
|
||||
.select(recordings::table::all_columns())
|
||||
.load::<RecordingRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
recordings.push(self.get_recording_data(row)?);
|
||||
}
|
||||
|
||||
Ok(recordings)
|
||||
}
|
||||
|
||||
/// Get all available information on all recordings where an ensemble is performing.
|
||||
pub fn get_recordings_for_ensemble(&self, ensemble_id: &str) -> Result<Vec<Recording>> {
|
||||
let mut recordings: Vec<Recording> = Vec::new();
|
||||
|
||||
let rows = recordings::table
|
||||
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
|
||||
.inner_join(ensembles::table.on(ensembles::id.nullable().eq(performances::ensemble)))
|
||||
.filter(ensembles::id.eq(ensemble_id))
|
||||
.select(recordings::table::all_columns())
|
||||
.load::<RecordingRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
recordings.push(self.get_recording_data(row)?);
|
||||
}
|
||||
|
||||
Ok(recordings)
|
||||
}
|
||||
|
||||
/// Get allavailable information on all recordings of a work.
|
||||
pub fn get_recordings_for_work(&self, work_id: &str) -> Result<Vec<Recording>> {
|
||||
let mut recordings: Vec<Recording> = Vec::new();
|
||||
|
||||
let rows = recordings::table
|
||||
.filter(recordings::work.eq(work_id))
|
||||
.load::<RecordingRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
recordings.push(self.get_recording_data(row)?);
|
||||
}
|
||||
|
||||
Ok(recordings)
|
||||
}
|
||||
|
||||
/// Delete an existing recording. This will fail if there are still references to this
|
||||
/// recording from other tables that are not directly part of the recording data.
|
||||
pub fn delete_recording(&self, id: &str) -> Result<()> {
|
||||
info!("Deleting recording {}", id);
|
||||
diesel::delete(recordings::table.filter(recordings::id.eq(id)))
|
||||
.execute(&self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
123
crates/database/src/schema.rs
Normal file
123
crates/database/src/schema.rs
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
table! {
|
||||
ensembles (id) {
|
||||
id -> Text,
|
||||
name -> Text,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
instrumentations (id) {
|
||||
id -> BigInt,
|
||||
work -> Text,
|
||||
instrument -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
instruments (id) {
|
||||
id -> Text,
|
||||
name -> Text,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mediums (id) {
|
||||
id -> Text,
|
||||
name -> Text,
|
||||
discid -> Nullable<Text>,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
performances (id) {
|
||||
id -> BigInt,
|
||||
recording -> Text,
|
||||
person -> Nullable<Text>,
|
||||
ensemble -> Nullable<Text>,
|
||||
role -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
persons (id) {
|
||||
id -> Text,
|
||||
first_name -> Text,
|
||||
last_name -> Text,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
recordings (id) {
|
||||
id -> Text,
|
||||
work -> Text,
|
||||
comment -> Text,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
tracks (id) {
|
||||
id -> Text,
|
||||
medium -> Text,
|
||||
index -> Integer,
|
||||
recording -> Text,
|
||||
work_parts -> Text,
|
||||
source_index -> Integer,
|
||||
path -> Text,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
work_parts (id) {
|
||||
id -> BigInt,
|
||||
work -> Text,
|
||||
part_index -> BigInt,
|
||||
title -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
works (id) {
|
||||
id -> Text,
|
||||
composer -> Text,
|
||||
title -> Text,
|
||||
last_used -> Nullable<BigInt>,
|
||||
last_played -> Nullable<BigInt>,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(instrumentations -> instruments (instrument));
|
||||
joinable!(instrumentations -> works (work));
|
||||
joinable!(performances -> ensembles (ensemble));
|
||||
joinable!(performances -> instruments (role));
|
||||
joinable!(performances -> persons (person));
|
||||
joinable!(performances -> recordings (recording));
|
||||
joinable!(recordings -> works (work));
|
||||
joinable!(tracks -> mediums (medium));
|
||||
joinable!(tracks -> recordings (recording));
|
||||
joinable!(work_parts -> works (work));
|
||||
joinable!(works -> persons (composer));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
ensembles,
|
||||
instrumentations,
|
||||
instruments,
|
||||
mediums,
|
||||
performances,
|
||||
persons,
|
||||
recordings,
|
||||
tracks,
|
||||
work_parts,
|
||||
works,
|
||||
);
|
||||
249
crates/database/src/works.rs
Normal file
249
crates/database/src/works.rs
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
use super::generate_id;
|
||||
use super::schema::{instrumentations, work_parts, works};
|
||||
use super::{Database, Error, Instrument, Person, Result};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use diesel::prelude::*;
|
||||
use diesel::{Insertable, Queryable};
|
||||
use log::info;
|
||||
|
||||
/// Table row data for a work.
|
||||
#[derive(Insertable, Queryable, Debug, Clone)]
|
||||
#[table_name = "works"]
|
||||
struct WorkRow {
|
||||
pub id: String,
|
||||
pub composer: String,
|
||||
pub title: String,
|
||||
pub last_used: Option<i64>,
|
||||
pub last_played: Option<i64>,
|
||||
}
|
||||
|
||||
impl From<Work> for WorkRow {
|
||||
fn from(work: Work) -> Self {
|
||||
WorkRow {
|
||||
id: work.id,
|
||||
composer: work.composer.id,
|
||||
title: work.title,
|
||||
last_used: Some(Utc::now().timestamp()),
|
||||
last_played: work.last_played.map(|t| t.timestamp()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Definition that a work uses an instrument.
|
||||
#[derive(Insertable, Queryable, Debug, Clone)]
|
||||
#[table_name = "instrumentations"]
|
||||
struct InstrumentationRow {
|
||||
pub id: i64,
|
||||
pub work: String,
|
||||
pub instrument: String,
|
||||
}
|
||||
|
||||
/// Table row data for a work part.
|
||||
#[derive(Insertable, Queryable, Debug, Clone)]
|
||||
#[table_name = "work_parts"]
|
||||
struct WorkPartRow {
|
||||
pub id: i64,
|
||||
pub work: String,
|
||||
pub part_index: i64,
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
/// A concrete work part that can be recorded.
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct WorkPart {
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
/// A specific work by a composer.
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Work {
|
||||
pub id: String,
|
||||
pub title: String,
|
||||
pub composer: Person,
|
||||
pub instruments: Vec<Instrument>,
|
||||
pub parts: Vec<WorkPart>,
|
||||
pub last_used: Option<DateTime<Utc>>,
|
||||
pub last_played: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl Work {
|
||||
pub fn new(id: String, title: String, composer: Person, instruments: Vec<Instrument>, parts: Vec<WorkPart>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
title,
|
||||
composer,
|
||||
instruments,
|
||||
parts,
|
||||
last_used: Some(Utc::now()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize a new work with a composer.
|
||||
pub fn from_composer(composer: Person) -> Self {
|
||||
Self {
|
||||
id: generate_id(),
|
||||
title: String::new(),
|
||||
composer,
|
||||
instruments: Vec::new(),
|
||||
parts: Vec::new(),
|
||||
last_used: Some(Utc::now()),
|
||||
last_played: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a string including the composer and title of the work.
|
||||
// TODO: Replace with impl Display.
|
||||
pub fn get_title(&self) -> String {
|
||||
format!("{}: {}", self.composer.name_fl(), self.title)
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Update an existing work or insert a new one.
|
||||
// TODO: Think about also inserting related items.
|
||||
pub fn update_work(&self, work: Work) -> Result<()> {
|
||||
info!("Updating work {:?}", work);
|
||||
self.defer_foreign_keys()?;
|
||||
|
||||
self.connection.transaction::<(), Error, _>(|| {
|
||||
let work_id = &work.id;
|
||||
self.delete_work(work_id)?;
|
||||
|
||||
// Add associated items from the server, if they don't already exist.
|
||||
|
||||
if self.get_person(&work.composer.id)?.is_none() {
|
||||
self.update_person(work.composer.clone())?;
|
||||
}
|
||||
|
||||
for instrument in &work.instruments {
|
||||
if self.get_instrument(&instrument.id)?.is_none() {
|
||||
self.update_instrument(instrument.clone())?;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the actual work.
|
||||
|
||||
let row: WorkRow = work.clone().into();
|
||||
diesel::insert_into(works::table)
|
||||
.values(row)
|
||||
.execute(&self.connection)?;
|
||||
|
||||
let Work {
|
||||
instruments, parts, ..
|
||||
} = work;
|
||||
|
||||
for instrument in instruments {
|
||||
let row = InstrumentationRow {
|
||||
id: rand::random(),
|
||||
work: work_id.to_string(),
|
||||
instrument: instrument.id,
|
||||
};
|
||||
|
||||
diesel::insert_into(instrumentations::table)
|
||||
.values(row)
|
||||
.execute(&self.connection)?;
|
||||
}
|
||||
|
||||
for (index, part) in parts.into_iter().enumerate() {
|
||||
let row = WorkPartRow {
|
||||
id: rand::random(),
|
||||
work: work_id.to_string(),
|
||||
part_index: index as i64,
|
||||
title: part.title,
|
||||
};
|
||||
|
||||
diesel::insert_into(work_parts::table)
|
||||
.values(row)
|
||||
.execute(&self.connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get an existing work.
|
||||
pub fn get_work(&self, id: &str) -> Result<Option<Work>> {
|
||||
let row = works::table
|
||||
.filter(works::id.eq(id))
|
||||
.load::<WorkRow>(&self.connection)?
|
||||
.first()
|
||||
.cloned();
|
||||
|
||||
let work = match row {
|
||||
Some(row) => Some(self.get_work_data(row)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(work)
|
||||
}
|
||||
|
||||
/// Retrieve all available information on a work from related tables.
|
||||
fn get_work_data(&self, row: WorkRow) -> Result<Work> {
|
||||
let mut instruments: Vec<Instrument> = Vec::new();
|
||||
|
||||
let instrumentations = instrumentations::table
|
||||
.filter(instrumentations::work.eq(&row.id))
|
||||
.load::<InstrumentationRow>(&self.connection)?;
|
||||
|
||||
for instrumentation in instrumentations {
|
||||
let id = instrumentation.instrument;
|
||||
instruments.push(
|
||||
self.get_instrument(&id)?
|
||||
.ok_or(Error::MissingItem("instrument", id))?,
|
||||
);
|
||||
}
|
||||
|
||||
let mut parts: Vec<WorkPart> = Vec::new();
|
||||
|
||||
let part_rows = work_parts::table
|
||||
.filter(work_parts::work.eq(&row.id))
|
||||
.load::<WorkPartRow>(&self.connection)?;
|
||||
|
||||
for part_row in part_rows {
|
||||
parts.push(WorkPart {
|
||||
title: part_row.title,
|
||||
});
|
||||
}
|
||||
|
||||
let person_id = row.composer;
|
||||
let person = self
|
||||
.get_person(&person_id)?
|
||||
.ok_or(Error::MissingItem("person", person_id))?;
|
||||
|
||||
Ok(Work {
|
||||
id: row.id,
|
||||
composer: person,
|
||||
title: row.title,
|
||||
instruments,
|
||||
parts,
|
||||
last_used: row.last_used.map(|t| Utc.timestamp(t, 0)),
|
||||
last_played: row.last_played.map(|t| Utc.timestamp(t, 0)),
|
||||
})
|
||||
}
|
||||
|
||||
/// Delete an existing work. This will fail if there are still other tables that relate to
|
||||
/// this work except for the things that are part of the information on the work it
|
||||
pub fn delete_work(&self, id: &str) -> Result<()> {
|
||||
info!("Deleting work {}", id);
|
||||
diesel::delete(works::table.filter(works::id.eq(id))).execute(&self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all existing works by a composer and related information from other tables.
|
||||
pub fn get_works(&self, composer_id: &str) -> Result<Vec<Work>> {
|
||||
let mut works: Vec<Work> = Vec::new();
|
||||
|
||||
let rows = works::table
|
||||
.filter(works::composer.eq(composer_id))
|
||||
.load::<WorkRow>(&self.connection)?;
|
||||
|
||||
for row in rows {
|
||||
works.push(self.get_work_data(row)?);
|
||||
}
|
||||
|
||||
Ok(works)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue