Hi Geert,
Thank you for your answer.
There a great post on reccursive query with postgresql.
http://old.storytotell.org/blog/2009/08/11/postgresql84-recursive-queries.htmlThe following sequence works fine, but I should admit it's a bit complex !
-- All packages are defined in the t_object table except the root node of the model
select P.name from t_package P where P.name not in
(select O.name from t_object O where O.object_type='Package')
--[1]
CREATE TABLE t_nodepath_tmp (ea_guid VARCHAR,name VARCHAR, parent_ea_guid VARCHAR);
--[2]
INSERT INTO t_nodepath_tmp (
-- child is an element, parent is an object
select C.ea_guid as "id", 'E::' || C.name as "name", P.ea_guid as "parent_id"
from t_object C, t_object P
where C.parentid=P.object_id
union
-- child is an element, parent is a package
select C.ea_guid as "id", 'E::' || C.name as "name", P.ea_guid as "parent_id"
from t_object C, t_package P
where C.package_id=P.package_id
and C.object_type != 'Package'
and C.parentid='0'
union
-- child is a package, parent is a package
select C.ea_guid as "id", 'P::' || C.name as "name", P.ea_guid as "parent_id"
from t_object C, t_package P
where C.package_id=P.package_id
and C.object_type='Package'
and C.parentid='0'
union
-- child is the root node of the model, no parent
select C.ea_guid as "id", 'P::' || C.name as "name", '0' as "parent_id"
from t_package C
where C.parent_id='0'
union
-- child is a diagram, parent is an object
select C.ea_guid as "id", 'D::' || C.name as "name", P.ea_guid as "parent_id"
from t_diagram C, t_object P
where C.parentid=P.object_id
union
-- child is a diagram, parent is an object
select C.ea_guid as "id", 'D::'|| C.name as "name", P.ea_guid as "parent_id"
from t_diagram C, t_package P
where C.package_id=P.package_id
and C.parentid='0'
order by name)
-- [3]
CREATE TABLE t_nodepath (id SERIAL PRIMARY KEY, ea_guid VARCHAR,name VARCHAR, parent_ea_guid VARCHAR);
-- [4]
INSERT INTO t_nodepath (ea_guid, name, parent_ea_guid) select ea_guid, name, parent_ea_guid from t_nodepath_tmp
-- [5]
INSERT INTO fs (id, name, parent_id)
SELECT c.id as 'id', c.name as 'name', p.id as 'parent_id'
FROM t_nodepath c, t_nodepath p
where c.parent_ea_guid=P.ea_guid
INSERT INTO fs (id, name, parent_id)
SELECT c.id as "id", c.name as "name", NULL as "parent_id"
FROM t_nodepath c
WHERE c.parent_ea_guid is NULL
-- [6]
WITH RECURSIVE path(name, path, parent, id, parent_id) AS (
SELECT name, '/', NULL, id, parent_id FROM fs WHERE id = (SELECT id from fs where parent_id is NULL)
UNION
SELECT
fs.name,
parentpath.path ||
CASE parentpath.path
WHEN '/' THEN ''
ELSE '/'
END || fs.name,
parentpath.path, fs.id, fs.parent_id
FROM fs, path as parentpath
WHERE fs.parent_id = parentpath.id)
SELECT * FROM path;