Revision 11837
Added by Aaron Marcuse-Kubitza about 11 years ago
vegbien.sql | ||
---|---|---|
1529 | 1529 |
LANGUAGE plpgsql |
1530 | 1530 |
AS $$ |
1531 | 1531 |
BEGIN |
1532 |
IF parent_id_ IS DISTINCT FROM old_parent_id THEN |
|
1533 |
DECLARE |
|
1534 |
-- These include the parent itself |
|
1535 |
old_ancestors integer[] := ( |
|
1536 |
SELECT array_agg(ancestor_id) FROM taxonlabel_relationship |
|
1537 |
WHERE descendant_id = old_parent_id |
|
1538 |
); |
|
1539 |
new_ancestors integer[] := ( |
|
1540 |
SELECT array_agg(ancestor_id) FROM taxonlabel_relationship |
|
1541 |
WHERE descendant_id = parent_id_ |
|
1542 |
); |
|
1543 |
descendant_id_ taxonlabel_relationship.descendant_id%TYPE; |
|
1544 |
ancestor_id_ taxonlabel_relationship.ancestor_id%TYPE; |
|
1545 |
BEGIN |
|
1546 |
FOR descendant_id_ IN -- also includes self |
|
1547 |
SELECT descendant_id |
|
1548 |
FROM taxonlabel_relationship |
|
1549 |
WHERE ancestor_id = new.taxonlabel_id |
|
1550 |
LOOP |
|
1551 |
-- Delete old parent's ancestors |
|
1552 |
DELETE FROM taxonlabel_relationship |
|
1553 |
WHERE descendant_id = descendant_id_ |
|
1554 |
AND ancestor_id = ANY (old_ancestors) |
|
1555 |
; |
|
1556 |
|
|
1557 |
-- Add new parent's ancestors |
|
1558 |
FOR ancestor_id_ IN |
|
1559 |
SELECT unnest(new_ancestors) |
|
1560 |
LOOP |
|
1561 |
INSERT INTO taxonlabel_relationship |
|
1562 |
(descendant_id, ancestor_id) |
|
1563 |
SELECT descendant_id_, ancestor_id_ |
|
1564 |
; |
|
1565 |
END LOOP; |
|
1566 |
END LOOP; |
|
1567 |
END; |
|
1568 |
END IF; |
|
1569 |
|
|
1570 |
/* Note: We don't need an ON DELETE trigger to update the descendants' |
|
1571 |
ancestors when a node is deleted, because the |
|
1572 |
taxonlabel_relationship.ancestor_id foreign key is ON DELETE CASCADE. */ |
|
1532 |
IF parent_id_ IS DISTINCT FROM old_parent_id THEN |
|
1533 |
DECLARE |
|
1534 |
-- These include the parent itself |
|
1535 |
old_ancestors integer[] = |
|
1536 |
CASE WHEN old_parent_id IS NULL THEN array[]::integer[] ELSE ( |
|
1537 |
SELECT array_agg(ancestor_id) FROM taxonlabel_relationship |
|
1538 |
WHERE descendant_id = old_parent_id |
|
1539 |
) END; |
|
1540 |
new_ancestors integer[] = |
|
1541 |
CASE WHEN parent_id_ IS NULL THEN array[]::integer[] ELSE ( |
|
1542 |
SELECT array_agg(ancestor_id) FROM taxonlabel_relationship |
|
1543 |
WHERE descendant_id = parent_id_ |
|
1544 |
) END; |
|
1545 |
descendant_id_ taxonlabel_relationship.descendant_id%TYPE; |
|
1546 |
ancestor_id_ taxonlabel_relationship.ancestor_id%TYPE; |
|
1547 |
BEGIN |
|
1548 |
FOR descendant_id_ IN -- also includes self |
|
1549 |
SELECT descendant_id |
|
1550 |
FROM taxonlabel_relationship |
|
1551 |
WHERE ancestor_id = new.taxonlabel_id |
|
1552 |
LOOP |
|
1553 |
-- Delete old parent's ancestors |
|
1554 |
IF descendant_id_ IS NOT NULL THEN |
|
1555 |
DELETE FROM taxonlabel_relationship |
|
1556 |
WHERE descendant_id = descendant_id_ |
|
1557 |
AND ancestor_id = ANY (old_ancestors) |
|
1558 |
; |
|
1559 |
END IF; |
|
1560 |
|
|
1561 |
-- Add new parent's ancestors |
|
1562 |
FOR ancestor_id_ IN |
|
1563 |
SELECT unnest(new_ancestors) |
|
1564 |
LOOP |
|
1565 |
INSERT INTO taxonlabel_relationship |
|
1566 |
(descendant_id, ancestor_id) |
|
1567 |
SELECT descendant_id_, ancestor_id_ |
|
1568 |
; |
|
1569 |
END LOOP; |
|
1570 |
END LOOP; |
|
1571 |
END; |
|
1572 |
END IF; |
|
1573 |
|
|
1574 |
/* Note: We don't need an ON DELETE trigger to update the descendants' |
|
1575 |
ancestors when a node is deleted, because the |
|
1576 |
taxonlabel_relationship.ancestor_id foreign key is ON DELETE CASCADE. */ |
|
1573 | 1577 |
END; |
1574 | 1578 |
$$; |
1575 | 1579 |
|
Also available in: Unified diff
schemas/vegbien.sql: taxonlabel_update_ancestors(): don't do an index scan if the value being scanned for is NULL, to support testing this function without the indexes in place, without extra full-table scans for NULL values affecting things. this can be used to determine if the function is actually using the indexes, by turning them off and seeing if the runtime changes.