I would save the data in something like
CREATE TABLE software ( id SERIAL PRIMARY KEY, ... ); CREATE TABLE software_dependency ( dependent int NOT NULL REFERENCES software(id), dependee int NOT NULL REFERENCES software(id), deptype int,
You can get a list of all the dependencies with something like:
WITH RECURSIVE t(id,type) AS ( SELECT dependee,deptype FROM software_dependency WHERE dependent=3 UNION ALL SELECT d.dependee,deptype FROM software_dependency d INNER JOIN t ON t.id=d.dependent ) SELECT * FROM t;
Edit: to get a tree, a good way is to accumulate dependencies in the ARRAY format. For each step in recursion, use the append operator (||) array to add a new identifier to the array and get it at the end.
source share