mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47: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);
|
final _random = Random(DateTime.now().millisecondsSinceEpoch);
|
||||||
int generateId() => _random.nextInt(0xFFFFFFFF);
|
int generateId() => _random.nextInt(0xFFFFFFFF);
|
||||||
|
|
||||||
|
class WorkModel {
|
||||||
|
final Work work;
|
||||||
|
final List<int> instrumentIds;
|
||||||
|
|
||||||
|
WorkModel({
|
||||||
|
@required this.work,
|
||||||
|
@required this.instrumentIds,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@UseMoor(
|
@UseMoor(
|
||||||
include: {
|
include: {
|
||||||
'database.moor',
|
'database.moor',
|
||||||
|
|
@ -19,6 +29,14 @@ class Database extends _$Database {
|
||||||
@override
|
@override
|
||||||
int get schemaVersion => 1;
|
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 {
|
Future<void> updatePerson(Person person) async {
|
||||||
await into(persons).insert(person);
|
await into(persons).insert(person);
|
||||||
}
|
}
|
||||||
|
|
@ -27,19 +45,26 @@ class Database extends _$Database {
|
||||||
await into(instruments).insert(instrument);
|
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 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)))
|
Future<void> insertWork(WorkModel model) async {
|
||||||
.go();
|
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
|
await insertWork(model);
|
||||||
.map((id) => Instrumentation(
|
for (final part in parts) {
|
||||||
work: work.id,
|
await insertWork(part);
|
||||||
instrument: id,
|
}
|
||||||
))
|
|
||||||
.toList());
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ CREATE TABLE works (
|
||||||
composer INTEGER REFERENCES persons(id),
|
composer INTEGER REFERENCES persons(id),
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
part_of INTEGER REFERENCES works(id) ON DELETE CASCADE,
|
part_of INTEGER REFERENCES works(id) ON DELETE CASCADE,
|
||||||
part_index INTEGER
|
part_index INTEGER,
|
||||||
|
part_level INTEGER
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE instrumentations (
|
CREATE TABLE instrumentations (
|
||||||
|
|
@ -37,16 +38,13 @@ SELECT * FROM instruments WHERE id = :id LIMIT 1;
|
||||||
workById:
|
workById:
|
||||||
SELECT * FROM works WHERE id = :id LIMIT 1;
|
SELECT * FROM works WHERE id = :id LIMIT 1;
|
||||||
|
|
||||||
worksByComposer:
|
workParts:
|
||||||
WITH RECURSIVE work_parts(id, part_of) AS (
|
SELECT * FROM works WHERE part_of = :id ORDER BY part_index;
|
||||||
SELECT id, part_of FROM works WHERE composer = :composerId
|
|
||||||
UNION ALL
|
-- TODO: Uncomment this once https://github.com/simolus3/moor/issues/453 is fixed.
|
||||||
SELECT works.id, works.part_of FROM works
|
-- worksByComposer(:id AS INTEGER):
|
||||||
JOIN work_parts ON works.id = work_parts.part_of
|
-- SELECT DISTINCT A.* FROM works A, works B ON A.id = B.part_of
|
||||||
)
|
-- WHERE A.composer = :id OR B.composer = :id;
|
||||||
SELECT works.* FROM works
|
|
||||||
JOIN work_parts ON works.id = work_parts.id
|
|
||||||
WHERE work_parts.part_of IS NULL;
|
|
||||||
|
|
||||||
instrumentsByWork:
|
instrumentsByWork:
|
||||||
SELECT instruments.* FROM instrumentations
|
SELECT instruments.* FROM instrumentations
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue