From 7f783bd0166722a12bbfa6700f73e8b510ec196c Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 22 Mar 2020 16:31:38 +0100 Subject: [PATCH] Database: Fix worksByComposer While the previous code was indeed valid SQL is wasn't the best way to do this. Also, only works with parts where included in the result set. This is fixed now. Some optimization may still be neccessary. --- lib/database.dart | 12 +++--------- lib/database.moor | 8 ++++---- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/database.dart b/lib/database.dart index d9ac728..084104d 100644 --- a/lib/database.dart +++ b/lib/database.dart @@ -35,14 +35,6 @@ class Database extends _$Database { await customStatement('PRAGMA foreign_keys = ON'); }); - // TODO: Remove this once https://github.com/simolus3/moor/issues/453 is fixed. - Selectable worksByComposer(int id) { - return customSelectQuery( - 'SELECT DISTINCT A.* FROM works A, works B ON A.id = B.part_of WHERE A.composer = :id OR B.composer = :id', - variables: [Variable.withInt(id)], - readsFrom: {works}).map(_rowToWork); - } - Future updatePerson(Person person) async { await into(persons).insert(person, orReplace: true); } @@ -85,7 +77,9 @@ class Database extends _$Database { Future updateRecording( Recording recording, List perfs) async { await transaction(() async { - await (delete(performances)..where((p) => p.recording.equals(recording.id))).go(); + await (delete(performances) + ..where((p) => p.recording.equals(recording.id))) + .go(); await into(recordings).insert(recording, orReplace: true); for (final perf in perfs) { await into(performances).insert(perf); diff --git a/lib/database.moor b/lib/database.moor index 1763455..2a6e327 100644 --- a/lib/database.moor +++ b/lib/database.moor @@ -63,10 +63,10 @@ SELECT * FROM works WHERE id = :id LIMIT 1; workParts: SELECT * FROM works WHERE part_of = :id ORDER BY part_index; --- TODO: Uncomment this once https://github.com/simolus3/moor/issues/453 is fixed. --- worksByComposer(:id AS INTEGER): --- SELECT DISTINCT A.* FROM works A, works B ON A.id = B.part_of --- WHERE A.composer = :id OR B.composer = :id; +-- TODO: Maybe optimize. +worksByComposer: +SELECT DISTINCT A.* FROM works A LEFT JOIN works B ON A.id = B.part_of + WHERE A.part_of IS NULL AND A.composer = :id OR B.composer = :id; composersByWork: SELECT DISTINCT persons.* FROM persons