From 9e485eac11a77d5cd8c2027f1c63fc0e15adb96a Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Fri, 6 May 2022 15:12:10 +0200 Subject: [PATCH] database: Update schema --- database/lib/src/database.dart | 32 +---- database/lib/src/database.drift | 231 ++++++++++++++++++++------------ database/lib/src/info.dart | 30 +---- 3 files changed, 157 insertions(+), 136 deletions(-) diff --git a/database/lib/src/database.dart b/database/lib/src/database.dart index f8a5e0f..3f67f2b 100644 --- a/database/lib/src/database.dart +++ b/database/lib/src/database.dart @@ -37,7 +37,6 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { @required DatabaseConnection connection, }) : super.connect(connection); - /// Get all available persons. /// /// This will return a list of [pageSize] persons. You can get another page @@ -78,37 +77,20 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { Future getWorkInfo(Work work) async { final id = work.id; - final composers = await partComposersByWork(id).get(); - composers.insert(0, await personById(work.composer).getSingle()); + final composer = await personById(work.composer).getSingle(); final instruments = await instrumentsByWork(id).get(); - - final List parts = []; - for (final part in await partsByWork(id).get()) { - parts.add(PartInfo( - part: part, - composer: part.composer != null - ? await personById(part.composer).getSingle() - : null, - instruments: await instrumentsByWorkPart(part.id).get(), - )); - } - - final List sections = []; - for (final section in await sectionsByWork(id).get()) { - sections.add(section); - } + final parts = await partsByWork(id).get(); return WorkInfo( work: work, + composer: composer, instruments: instruments, - composers: composers, parts: parts, - sections: sections, ); } /// Get all available information on a work. - Future getWork(int id) async { + Future getWork(String id) async { final work = await workById(id).getSingle(); return await getWorkInfo(work); } @@ -118,7 +100,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { /// This will return a list of [pageSize] results. You can get another page /// using the [page] parameter. If a non empty [search] string is provided, /// the works will be filtered using that string. - Future> getWorks(int personId, + Future> getWorks(String personId, [int page, String search]) async { final offset = page != null ? page * pageSize : 0; List works; @@ -183,7 +165,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { } /// Get all available information on a recording. - Future getRecording(int id) async { + Future getRecording(String id) async { final recording = await recordingById(id).getSingle(); return await getRecordingInfo(recording); } @@ -192,7 +174,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { /// /// This will return a list of [pageSize] recordings. You can get the other /// pages using the [page] parameter. - Future> getRecordings(int workId, [int page]) async { + Future> getRecordings(String workId, [int page]) async { final offset = page != null ? page * pageSize : 0; final recordings = await recordingsByWork(workId, pageSize, offset).get(); diff --git a/database/lib/src/database.drift b/database/lib/src/database.drift index 612255e..4c32376 100644 --- a/database/lib/src/database.drift +++ b/database/lib/src/database.drift @@ -1,139 +1,200 @@ -CREATE TABLE persons ( - id INTEGER NOT NULL PRIMARY KEY, - first_name TEXT NOT NULL, - last_name TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "persons" ( + "id" TEXT NOT NULL PRIMARY KEY, + "first_name" TEXT NOT NULL, + "last_name" TEXT NOT NULL, + "last_used" BIGINT, + "last_played" BIGINT ); --- This represents real instruments as well as other roles that can be played --- in a recording. -CREATE TABLE instruments ( - id INTEGER NOT NULL PRIMARY KEY, - name TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "instruments" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT NOT NULL, + "last_used" BIGINT, + "last_played" BIGINT ); -CREATE TABLE works ( - id INTEGER NOT NULL PRIMARY KEY, - composer INTEGER REFERENCES persons(id) ON DELETE SET NULL, - title TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "works" ( + "id" TEXT NOT NULL PRIMARY KEY, + "composer" TEXT NOT NULL REFERENCES "persons"("id"), + "title" TEXT NOT NULL, + "last_used" BIGINT, + "last_played" BIGINT ); -CREATE TABLE instrumentations ( - work INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE, - instrument INTEGER NOT NULL REFERENCES instruments(id) ON DELETE CASCADE +CREATE TABLE IF NOT EXISTS "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 INTEGER NOT NULL PRIMARY KEY, - composer INTEGER REFERENCES persons(id) ON DELETE SET NULL, - title TEXT NOT NULL, - part_of INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE, - part_index INTEGER NOT NULL +CREATE TABLE IF NOT EXISTS "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 part_instrumentations ( - work_part INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE, - instrument INTEGER NOT NULL REFERENCES instruments(id) ON DELETE CASCADE +CREATE TABLE IF NOT EXISTS "work_sections" ( + "id" BIGINT NOT NULL PRIMARY KEY, + "work" TEXT NOT NULL REFERENCES "works"("id") ON DELETE CASCADE, + "title" TEXT NOT NULL, + "before_index" BIGINT NOT NULL ); -CREATE TABLE work_sections ( - id INTEGER NOT NULL PRIMARY KEY, - work INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE, - title TEXT NOT NULL, - before_part_index INTEGER NOT NULL +CREATE TABLE IF NOT EXISTS "ensembles" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT NOT NULL, + "last_used" BIGINT, + "last_played" BIGINT ); -CREATE TABLE ensembles ( - id INTEGER NOT NULL PRIMARY KEY, - name TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "recordings" ( + "id" TEXT NOT NULL PRIMARY KEY, + "work" TEXT NOT NULL REFERENCES "works"("id"), + "comment" TEXT NOT NULL, + "last_used" BIGINT, + "last_played" BIGINT ); -CREATE TABLE recordings ( - id INTEGER NOT NULL PRIMARY KEY, - work INTEGER REFERENCES works(id) ON DELETE SET NULL, - comment TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "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 performances ( - recording INTEGER NOT NULL REFERENCES recordings(id) ON DELETE CASCADE, - person INTEGER REFERENCES persons(id) ON DELETE CASCADE, - ensemble INTEGER REFERENCES ensembles(id) ON DELETE CASCADE, - role INTEGER REFERENCES instruments(id) ON DELETE SET NULL +CREATE TABLE IF NOT EXISTS "mediums" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT NOT NULL, + "discid" TEXT, + "last_used" BIGINT, + "last_played" BIGINT +); + +CREATE TABLE IF NOT EXISTS "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, + "last_used" BIGINT, + "last_played" BIGINT ); allPersons: -SELECT * FROM persons ORDER BY last_name, first_name - LIMIT :limit OFFSET :offset; +SELECT * +FROM persons +ORDER BY last_name, + first_name +LIMIT :limit OFFSET :offset; searchPersons: -SELECT * FROM persons WHERE last_name LIKE :search - ORDER BY last_name, first_name LIMIT :limit OFFSET :offset; +SELECT * +FROM persons +WHERE last_name LIKE :search +ORDER BY last_name, + first_name +LIMIT :limit OFFSET :offset; personById: -SELECT * FROM persons WHERE id = :id LIMIT 1; +SELECT * +FROM persons +WHERE id = :id +LIMIT 1; allInstruments: -SELECT * FROM instruments ORDER BY name LIMIT :limit OFFSET :offset; +SELECT * +FROM instruments +ORDER BY name +LIMIT :limit OFFSET :offset; searchInstruments: -SELECT * FROM instruments WHERE name LIKE :search ORDER BY name - LIMIT :limit OFFSET :offset; +SELECT * +FROM instruments +WHERE name LIKE :search +ORDER BY name +LIMIT :limit OFFSET :offset; instrumentById: -SELECT * FROM instruments WHERE id = :id LIMIT 1; +SELECT * +FROM instruments +WHERE id = :id +LIMIT 1; workById: -SELECT * FROM works WHERE id = :id LIMIT 1; +SELECT * +FROM works +WHERE id = :id +LIMIT 1; partsByWork: -SELECT * FROM work_parts WHERE part_of = :id ORDER BY part_index; - -sectionsByWork: -SELECT * FROM work_sections WHERE work = :id ORDER BY before_part_index; +SELECT * +FROM work_parts +WHERE work = :id +ORDER BY part_index; worksByComposer: -SELECT DISTINCT works.* FROM works - JOIN work_parts ON work_parts.part_of = works.id - WHERE works.composer = :id OR work_parts.composer = :id - ORDER BY works.title LIMIT :limit OFFSET :offset; +SELECT * +FROM works +WHERE composer = :id +ORDER BY title +LIMIT :limit OFFSET :offset; searchWorksByComposer: -SELECT DISTINCT works.* FROM works - JOIN work_parts ON work_parts.part_of = works.id - WHERE (works.composer = :id OR work_parts.composer = :id) - AND works.title LIKE :search - ORDER BY works.title LIMIT :limit OFFSET :offset; - -partComposersByWork: -SELECT DISTINCT persons.* FROM persons - JOIN work_parts ON work_parts.composer = persons.id - WHERE work_parts.part_of = :id; +SELECT * +FROM works +WHERE composer = :id + AND title LIKE :search +ORDER BY title +LIMIT :limit OFFSET :offset; instrumentsByWork: -SELECT instruments.* FROM instrumentations +SELECT instruments.* +FROM instrumentations JOIN instruments ON instrumentations.instrument = instruments.id - WHERE instrumentations.work = :workId; - -instrumentsByWorkPart: -SELECT instruments.* FROM part_instrumentations - JOIN instruments ON part_instrumentations.instrument = instruments.id - WHERE part_instrumentations.work_part = :id; +WHERE instrumentations.work = :workId; allEnsembles: -SELECT * FROM ensembles ORDER BY name LIMIT :limit OFFSET :offset; +SELECT * +FROM ensembles +ORDER BY name +LIMIT :limit OFFSET :offset; searchEnsembles: -SELECT * FROM ensembles WHERE name LIKE :search ORDER BY name - LIMIT :limit OFFSET :offset; +SELECT * +FROM ensembles +WHERE name LIKE :search +ORDER BY name +LIMIT :limit OFFSET :offset; ensembleById: -SELECT * FROM ensembles WHERE id = :id LIMIT 1; +SELECT * +FROM ensembles +WHERE id = :id +LIMIT 1; recordingById: -SELECT * FROM recordings WHERE id = :id; +SELECT * +FROM recordings +WHERE id = :id; recordingsByWork: -SELECT * FROM recordings WHERE work = :id ORDER BY id - LIMIT :limit OFFSET :offset; +SELECT * +FROM recordings +WHERE work = :id +ORDER BY id +LIMIT :limit OFFSET :offset; performancesByRecording: -SELECT * FROM performances WHERE recording = :id; +SELECT * +FROM performances +WHERE recording = :id; + +tracksByRecording: +SELECT * +FROM tracks +WHERE recording = :id +ORDER BY "index"; diff --git a/database/lib/src/info.dart b/database/lib/src/info.dart index 74a9333..fd80414 100644 --- a/database/lib/src/info.dart +++ b/database/lib/src/info.dart @@ -1,27 +1,5 @@ import 'database.dart'; -/// A bundle of all available information on a work part. -class PartInfo { - /// The work part itself. - final WorkPart part; - - /// A list of instruments. - /// - /// This will include the instruments, that are specific to this part. - final List instruments; - - /// The composer of this part. - /// - /// This is null, if this part doesn't have a specific composer. - final Person composer; - - PartInfo({ - this.part, - this.instruments, - this.composer, - }); -} - /// A bundle information on a work. /// /// This includes all available information except for recordings of this work. @@ -35,11 +13,11 @@ class WorkInfo { /// parts. final List instruments; - /// A list of persons, which will include all part composers. - final List composers; + /// The work's composer. + final Person composer; /// All available information on the work parts. - final List parts; + final List parts; /// The sections of this work. final List sections; @@ -47,7 +25,7 @@ class WorkInfo { WorkInfo({ this.work, this.instruments, - this.composers, + this.composer, this.parts, this.sections, });