pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: ***SPAM***  Re: With Recursive / Recursive View question
2+ messages / 1 participants
[nested] [flat]

* Re: ***SPAM***  Re: With Recursive / Recursive View question
@ 2022-08-21 02:02 ` Perry Smith <pedz@easesoftware.com>
  1 sibling, 0 replies; 2+ messages in thread

From: Perry Smith @ 2022-08-21 02:02 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-general list <pgsql-general@lists.postgresql.org>



> On Aug 20, 2022, at 19:38, Christophe Pettus <xof@thebuild.com> wrote:
> 
> 
> 
>> On Aug 20, 2022, at 15:42, Perry Smith <pedz@easesoftware.com> wrote:
>> 
>> To rephrase, is it possible to write a view that would work from the child terms out towards the ancestors?
> 
> Assuming that the concern is that you want to parameterize this predicate:
> 
> 	    WHERE basename = '10732.emlx'
> 
> ... you might consider an SQL function taking basename as a parameter.

Yea.  If I did a function, I would just pass in the id.  I’ve used functions only rarely.  For whatever reason, I’ve always been very skittish around them.  But perhaps I need to grow up.

Thank you again,
Perry

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: ***SPAM***  Re: With Recursive / Recursive View question
@ 2022-08-21 02:48 ` Perry Smith <pedz@easesoftware.com>
  1 sibling, 0 replies; 2+ messages in thread

From: Perry Smith @ 2022-08-21 02:48 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-general list <pgsql-general@lists.postgresql.org>


> On Aug 20, 2022, at 19:38, Christophe Pettus <xof@thebuild.com> wrote:
> 
> 
>> On Aug 20, 2022, at 15:42, Perry Smith <pedz@easesoftware.com> wrote:
>> 
>> To rephrase, is it possible to write a view that would work from the child terms out towards the ancestors?
> 
> Assuming that the concern is that you want to parameterize this predicate:
> 
> 	    WHERE basename = '10732.emlx'
> 
> ... you might consider an SQL function taking basename as a parameter.

That wasn’t so bad…

CREATE OR REPLACE FUNCTION pathname(in_id bigint)
RETURNS character varying AS
$$
DECLARE
  fullpath character varying;

BEGIN
  WITH RECURSIVE pathname(id, parent_id, basename) AS (
      SELECT child.id, child.parent_id, child.basename
      FROM dirents child
      WHERE child.id = in_id
    UNION ALL
      SELECT parent.id, parent.parent_id, CONCAT(parent.basename, '/', child.basename)
      FROM dirents parent, pathname child
      WHERE parent.id = child.parent_id
  )
  SELECT basename INTO fullpath FROM pathname where parent_id IS NULL;
  RETURN fullpath;
END;
$$ LANGUAGE plpgsql;

SELECT pathname(id) FROM dirents WHERE basename = 'OSX';

Thank you … again! :-)
Perry

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-08-21 02:48 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-08-21 02:02 ` Perry Smith <pedz@easesoftware.com>
2022-08-21 02:48 ` Perry Smith <pedz@easesoftware.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox