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:
Elias Projahn 2020-03-21 14:34:45 +01:00
parent b15591b388
commit 4b178752b9
2 changed files with 44 additions and 21 deletions

View file

@ -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