Skip to content

Commit

Permalink
feat: add children function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZavenArra committed Mar 10, 2022
1 parent d1b4b8c commit 94388c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/* Replace with your SQL commands */
DROP FUNCTION getStakeholderChildren;
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ CREATE FUNCTION getStakeholderChildren
RETURNS TABLE (stakeholder_id uuid, parent_id uuid, depth int, relations_type text, relations_role text)
AS $$
WITH RECURSIVE children AS (
SELECT stakeholder.id, stakeholder_relation.parent_id, 1 as depth, stakeholder_relation.type, stakeholder_relation.role
FROM stakeholder
LEFT JOIN stakeholder_relation ON stakeholder_relation.child_id = stakeholder.id
WHERE stakeholder.id = $1
SELECT stakeholder.stakeholder.id, stakeholder.stakeholder_relation.parent_id, 1 as depth, stakeholder.stakeholder_relation.type, stakeholder.stakeholder_relation.role
FROM stakeholder.stakeholder
LEFT JOIN stakeholder.stakeholder_relation ON stakeholder.stakeholder_relation.child_id = stakeholder.stakeholder.id
WHERE stakeholder.stakeholder.id = $1
UNION
SELECT next_child.id, stakeholder_relation.parent_id, depth + 1, stakeholder_relation.type, stakeholder_relation.role
FROM stakeholder next_child
JOIN stakeholder_relation ON stakeholder_relation.child_id = next_child.id
JOIN children c ON stakeholder_relation.parent_id = c.id
SELECT next_child.id, stakeholder.stakeholder_relation.parent_id, depth + 1, stakeholder.stakeholder_relation.type, stakeholder.stakeholder_relation.role
FROM stakeholder.stakeholder next_child
JOIN stakeholder.stakeholder_relation ON stakeholder.stakeholder_relation.child_id = next_child.id
JOIN children c ON stakeholder.stakeholder_relation.parent_id = c.id
)
SELECT *
FROM children
Expand Down

0 comments on commit 94388c7

Please sign in to comment.