From ddbd4f73c840fd780f29e3f7ac58b95e3919cf3f Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 13 Aug 2018 17:05:16 +0200 Subject: [PATCH] attributes can be inherited through special relation "inheritAttributes" --- .../a2c75661-f9e2-478f-a69f-6a9409e69997.xml | 25 +------------- .idea/sqldialects.xml | 6 ++++ src/entities/note.js | 33 ++++++++++++------- src/services/attributes.js | 3 +- 4 files changed, 31 insertions(+), 36 deletions(-) create mode 100644 .idea/sqldialects.xml diff --git a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml index e820877ad..1317d871f 100644 --- a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml +++ b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml @@ -1,6 +1,6 @@ - + 3.16.1 @@ -59,7 +59,6 @@ 1 apiTokenId - 1 @@ -128,7 +127,6 @@ 1 attributeId - 1 @@ -190,21 +188,17 @@ 1 branchId - 1 noteId parentNoteId - noteId - parentNoteId - branchId @@ -232,7 +226,6 @@ parentNoteId 1 eventId - 1 @@ -289,7 +282,6 @@ parentNoteId 1 imageId - 1 @@ -337,21 +329,17 @@ parentNoteId 1 noteImageId - 1 noteId imageId - noteId - imageId - noteImageId @@ -413,20 +401,16 @@ imageId 1 noteRevisionId - 1 noteId - dateModifiedFrom - dateModifiedTo - noteRevisionId @@ -493,12 +477,10 @@ imageId 1 noteId - 1 type - noteId @@ -539,7 +521,6 @@ imageId 1 name - 1 @@ -575,7 +556,6 @@ imageId 1 branchId - 1 @@ -596,7 +576,6 @@ imageId 1 sourceId - 1 @@ -659,12 +638,10 @@ imageId entityName entityId - 1 syncDate - id diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 000000000..c0e01cabc --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/entities/note.js b/src/entities/note.js index d37ff3cc8..89c05e6b8 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -72,17 +72,28 @@ class Note extends Entity { async getAttributes() { const attributes = await repository.getEntities(` - WITH RECURSIVE tree(noteId, level) AS ( - SELECT ?, 0 - UNION - SELECT branches.parentNoteId, tree.level + 1 FROM branches - JOIN tree ON branches.noteId = tree.noteId - JOIN notes ON notes.noteId = branches.parentNoteId - WHERE notes.isDeleted = 0 AND branches.isDeleted = 0 - ) - SELECT attributes.* FROM attributes JOIN tree ON attributes.noteId = tree.noteId - WHERE attributes.isDeleted = 0 AND (attributes.isInheritable = 1 OR attributes.noteId = ?) - ORDER BY level, noteId, position`, [this.noteId, this.noteId]); + WITH RECURSIVE + tree(noteId, level) AS ( + SELECT ?, 0 + UNION + SELECT branches.parentNoteId, tree.level + 1 FROM branches + JOIN tree ON branches.noteId = tree.noteId + JOIN notes ON notes.noteId = branches.parentNoteId + WHERE notes.isDeleted = 0 + AND branches.isDeleted = 0 + ), + treeWithAttrs(noteId, level) AS ( + SELECT * FROM tree + UNION + SELECT attributes.value, treeWithAttrs.level + 1 FROM attributes + JOIN treeWithAttrs ON treeWithAttrs.noteId = attributes.noteId + WHERE attributes.isDeleted = 0 + AND attributes.type = 'relation' + AND attributes.name = 'inheritAttributes' + ) + SELECT attributes.* FROM attributes JOIN treeWithAttrs ON attributes.noteId = treeWithAttrs.noteId + WHERE attributes.isDeleted = 0 AND (attributes.isInheritable = 1 OR attributes.noteId = ?) + ORDER BY level, noteId, position`, [this.noteId, this.noteId]); // attributes are ordered so that "closest" attributes are first // we order by noteId so that attributes from same note stay together. Actual noteId ordering doesn't matter. diff --git a/src/services/attributes.js b/src/services/attributes.js index 6cfea1c86..176678f11 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -20,7 +20,8 @@ const BUILTIN_ATTRIBUTES = [ // relation names { type: 'relation', name: 'runOnNoteView' }, { type: 'relation', name: 'runOnNoteTitleChange' }, - { type: 'relation', name: 'runOnAttributeChange' } + { type: 'relation', name: 'runOnAttributeChange' }, + { type: 'relation', name: 'inheritAttributes' } ]; async function getNotesWithLabel(name, value) {