Commit 689f6d3e authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Fixed ltree parentPath issue + removed get_ltree SQL function. Thanks to Sonia Zorba.

parent 37d5e357
Loading
Loading
Loading
Loading
Loading
+0 −0

File moved.

+0 −20
Original line number Diff line number Diff line
/*
    Author: Sonia Zorba
    Email: sonia.zorba@inaf.it
*/

-- Returns the ltree path for a given os_path
CREATE OR REPLACE FUNCTION get_ltree_path(input_os_path character varying) RETURNS ltree AS $$
BEGIN
    RETURN 
    ltree_path FROM (
        SELECT node_id, string_agg(name, '/') AS os_path, first_value(path) over() ltree_path FROM (
            SELECT f1.name, f2.path, f2.node_id
            FROM Node f1 
            JOIN Node f2 ON f1.path @> f2.path
            WHERE f2.name = (string_to_array(input_os_path, '/'))[cardinality(string_to_array(input_os_path, '/'))]
            ORDER BY f2.node_id, length(f1.path::varchar) ASC
        ) AS t1 GROUP BY node_id, path
    ) AS t2 WHERE os_path = input_os_path;
END;
$$ LANGUAGE plpgsql;
+4 −1
Original line number Diff line number Diff line
@@ -3,7 +3,10 @@
    Email: sonia.zorba@inaf.it
*/

-- Alternative way to obtain the os_path
-- This view allows to obtain the the ltree_path from the os_path
-- Example query:
-- SELECT path FROM Node n JOIN node_os_path o ON n.node_id = o.node_id WHERE os_path = '/curban/store';

CREATE VIEW node_os_path AS
SELECT node_id, '/' AS os_path FROM Node WHERE path = ''
UNION
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ class DbConnector(object):
        if self.conn:
            out = open("db_connector_log.txt", "a")
            #print(f"parentOSPath: {parentOSPath}")
            self.cursor.execute("SELECT get_ltree_path(%s)", (parentOSPath,))
            self.cursor.execute("SELECT path FROM Node n JOIN node_os_path o ON n.node_id = o.node_id WHERE os_path = %s;", (parentOSPath,))
            result = self.cursor.fetchall()
            for i in result:
                out.write(f"queryResult: {i}\n")