Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 5C9914758E6 for ; Sun, 17 Nov 2002 14:40:31 -0500 (EST) Received: from nezlok.unixathome.org (nezlok.unixathome.org [216.187.87.230]) by postgresql.org (Postfix) with ESMTP id 6CF804763D3 for ; Sun, 17 Nov 2002 14:40:27 -0500 (EST) Received: by nezlok.unixathome.org (Postfix, from userid 1000) id B4D79AE4FE; Sun, 17 Nov 2002 11:40:09 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by nezlok.unixathome.org (Postfix) with ESMTP id 868CBAE4F7 for ; Sun, 17 Nov 2002 11:40:09 -0800 (PST) Date: Sun, 17 Nov 2002 11:39:57 -0800 (PST) From: Dan Langille X-X-Sender: dan@nezlok.unixathome.org To: pgsql-sql@postgresql.org Subject: Trees: maintaining pathnames Message-ID: <20021117112520.C8127-200000@nezlok.unixathome.org> MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="0-1303306285-1037561397=:8127" Content-ID: <20021117113244.D9135@nezlok.unixathome.org> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/251 X-Sequence-Number: 10393 This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. Send mail to mime@docserver.cac.washington.edu for more info. --0-1303306285-1037561397=:8127 Content-Type: TEXT/PLAIN; CHARSET=US-ASCII Content-ID: <20021117113244.Q9135@nezlok.unixathome.org> My existing tree implementation reflects the files contained on disk. The full pathname to a particlar file is obtained from the path to the parent directory. I am now considering putting this information into a field in the table. Attached you will find the pg_dump from my test database (2.4k) if you want to test with this setup and in case what I have pasted below contains an error. Here is the table and the test data: create table tree(id int not null, parent_id int, name text not null, pathname text not null, primary key (id)); insert into tree (id, name, pathname) values (1, 'usr', '/usr'); insert into tree (id, name, parent_id, pathname) values (2, 'ports', 1, '/usr/ports'); insert into tree values (3, 2, 'security', 'test'); select * from tree; test=# select * from tree; id | parent_id | name | pathname ----+-----------+----------+--------------------- 1 | | usr | /usr 2 | 1 | ports | /usr/ports 3 | 2 | security | /usr/ports/security (3 rows) The goal is to ensure that pathname always contains the correct value. Here are the functions/triggers which I created in order to attain that goal. This function ensures that the pathname is set correctly when a row is inserted or changed. create or replace function tree_pathname_set() returns opaque as ' DECLARE parent_pathname text; BEGIN RAISE NOTICE \'into tree_pathname_set with %:%:%\', new.id, new.name, new.pathname; select pathname into parent_pathname from tree where id = new.parent_id; if found then new.pathname = parent_pathname || \'/\' || new.name; else new.pathname = \'/\' || new.name; end if; RETURN new; END;' language 'plpgsql';\ create trigger tree_pathname_set before insert or update on tree for each row execute procedure tree_pathname_set(); This function ensures that any childre of a recently modified row are also kept up to date. create or replace function tree_pathname_set_children() returns opaque as 'BEGIN RAISE NOTICE \'into tree_pathname_set_children with %:%:%\', new.id, new.name, new.pathname; update tree set pathname = new.pathname || \'/\' || name where parent_id = new.id; RETURN new; END;' language 'plpgsql'; create trigger tree_pathname_set_children after insert or update on tree for each row execute procedure tree_pathname_set_children(); NOTE: the above is "insert or update" but as I typed this I realize that only update is sufficent. A change to the top level row is shown below: test=# update tree set name = 'dan' where id = 1; NOTICE: into tree_pathname_set with 1:dan:/usr NOTICE: into tree_pathname_set_children with 1:dan:/dan NOTICE: into tree_pathname_set with 2:ports:/dan/ports NOTICE: into tree_pathname_set_children with 2:ports:/dan/ports NOTICE: into tree_pathname_set with 3:security:/dan/ports/security NOTICE: into tree_pathname_set_children with 3:security:/dan/ports/security UPDATE 1 test=# select * from tree; id | parent_id | name | pathname ----+-----------+----------+--------------------- 1 | | dan | /dan 2 | 1 | ports | /dan/ports 3 | 2 | security | /dan/ports/security (3 rows) test=# Suggestions, comment, open ridicule, most welcome. thanks. --0-1303306285-1037561397=:8127 Content-Type: TEXT/PLAIN; CHARSET=US-ASCII; NAME="test.sql" Content-Transfer-Encoding: BASE64 Content-ID: <20021117112957.Q8127@nezlok.unixathome.org> Content-Description: Content-Disposition: ATTACHMENT; FILENAME="test.sql" LS0NCi0tIFNlbG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