mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 18:57:25 +01:00
Redo how works are stored
Before works where stored using a complicated tree structure where each part references its parent. Now it's a flat structure that keeps track of the relations using a new field named part_level. The corresponding query was simplified accordingly. Also, the updateWork function was rewritten.
This commit is contained in:
parent
b15591b388
commit
4b178752b9
2 changed files with 44 additions and 21 deletions
|
|
@ -7,6 +7,16 @@ part 'database.g.dart';
|
|||
final _random = Random(DateTime.now().millisecondsSinceEpoch);
|
||||
int generateId() => _random.nextInt(0xFFFFFFFF);
|
||||
|
||||
class WorkModel {
|
||||
final Work work;
|
||||
final List<int> instrumentIds;
|
||||
|
||||
WorkModel({
|
||||
@required this.work,
|
||||
@required this.instrumentIds,
|
||||
});
|
||||
}
|
||||
|
||||
@UseMoor(
|
||||
include: {
|
||||
'database.moor',
|
||||
|
|
@ -19,6 +29,14 @@ class Database extends _$Database {
|
|||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
||||
// TODO: Remove this once https://github.com/simolus3/moor/issues/453 is fixed.
|
||||
Selectable<Work> 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<void> updatePerson(Person person) async {
|
||||
await into(persons).insert(person);
|
||||
}
|
||||
|
|
@ -27,19 +45,26 @@ class Database extends _$Database {
|
|||
await into(instruments).insert(instrument);
|
||||
}
|
||||
|
||||
Future<void> updateWork(Work work, List<int> instrumentIds) async {
|
||||
Future<void> updateWork(WorkModel model, List<WorkModel> parts) async {
|
||||
await transaction(() async {
|
||||
await into(works).insert(work);
|
||||
final workId = model.work.id;
|
||||
await (delete(works)..where((w) => w.id.equals(workId))).go();
|
||||
await (delete(works)..where((w) => w.partOf.equals(workId))).go();
|
||||
|
||||
await (delete(instrumentations)..where((i) => i.work.equals(work.id)))
|
||||
.go();
|
||||
Future<void> insertWork(WorkModel model) async {
|
||||
await into(works).insert(model.work);
|
||||
await batch((b) => b.insertAll(
|
||||
instrumentations,
|
||||
model.instrumentIds
|
||||
.map((id) =>
|
||||
Instrumentation(work: model.work.id, instrument: id))
|
||||
.toList()));
|
||||
}
|
||||
|
||||
await into(instrumentations).insertAll(instrumentIds
|
||||
.map((id) => Instrumentation(
|
||||
work: work.id,
|
||||
instrument: id,
|
||||
))
|
||||
.toList());
|
||||
await insertWork(model);
|
||||
for (final part in parts) {
|
||||
await insertWork(part);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ CREATE TABLE works (
|
|||
composer INTEGER REFERENCES persons(id),
|
||||
title TEXT NOT NULL,
|
||||
part_of INTEGER REFERENCES works(id) ON DELETE CASCADE,
|
||||
part_index INTEGER
|
||||
part_index INTEGER,
|
||||
part_level INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE instrumentations (
|
||||
|
|
@ -37,16 +38,13 @@ SELECT * FROM instruments WHERE id = :id LIMIT 1;
|
|||
workById:
|
||||
SELECT * FROM works WHERE id = :id LIMIT 1;
|
||||
|
||||
worksByComposer:
|
||||
WITH RECURSIVE work_parts(id, part_of) AS (
|
||||
SELECT id, part_of FROM works WHERE composer = :composerId
|
||||
UNION ALL
|
||||
SELECT works.id, works.part_of FROM works
|
||||
JOIN work_parts ON works.id = work_parts.part_of
|
||||
)
|
||||
SELECT works.* FROM works
|
||||
JOIN work_parts ON works.id = work_parts.id
|
||||
WHERE work_parts.part_of IS NULL;
|
||||
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;
|
||||
|
||||
instrumentsByWork:
|
||||
SELECT instruments.* FROM instrumentations
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue