44 * Sample to demonstrate C functions which return setof scalar
55 * and setof composite.
66 * Joe Conway <mail@joeconway.com>
7+ * And contributors:
8+ * Nabil Sayegh <postgresql@e-trolley.de>
79 *
810 * Copyright 2002 by PostgreSQL Global Development Group
911 *
@@ -60,9 +62,11 @@ Installation:
6062 - requires anonymous composite type syntax in the FROM clause. See
6163 the instructions in the documentation below.
6264
63- connectby(text relname, text keyid_fld, text parent_keyid_fld,
64- text start_with, int max_depth [, text branch_delim])
65+ connectby(text relname, text keyid_fld, text parent_keyid_fld
66+ [, text orderby_fld], text start_with, int max_depth
67+ [, text branch_delim])
6568 - returns keyid, parent_keyid, level, and an optional branch string
69+ and an optional serial column for ordering siblings
6670 - requires anonymous composite type syntax in the FROM clause. See
6771 the instructions in the documentation below.
6872
452456==================================================================
453457Name
454458
455- connectby(text, text, text, text, int[, text]) - returns a set
459+ connectby(text, text, text[, text], text , text, int[, text]) - returns a set
456460 representing a hierarchy (tree structure)
457461
458462Synopsis
459463
460- connectby(text relname, text keyid_fld, text parent_keyid_fld,
461- text start_with, int max_depth [, text branch_delim])
464+ connectby(text relname, text keyid_fld, text parent_keyid_fld
465+ [, text orderby_fld], text start_with, int max_depth
466+ [, text branch_delim])
462467
463468Inputs
464469
@@ -474,6 +479,11 @@ Inputs
474479
475480 Name of the key_parent field
476481
482+ orderby_fld
483+
484+ If optional ordering of siblings is desired:
485+ Name of the field to order siblings
486+
477487 start_with
478488
479489 root value of the tree input as a text value regardless of keyid_fld type
@@ -500,6 +510,16 @@ Outputs
500510
501511 SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
502512 AS t(keyid text, parent_keyid text, level int);
513+
514+ - or -
515+
516+ SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~')
517+ AS t(keyid text, parent_keyid text, level int, branch text, pos int);
518+
519+ - or -
520+
521+ SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0)
522+ AS t(keyid text, parent_keyid text, level int, pos int);
503523
504524Notes
505525
@@ -520,22 +540,25 @@ Notes
520540 5. The parameters representing table and field names must include double
521541 quotes if the names are mixed-case or contain special characters.
522542
543+ 6. If sorting of siblings is desired, the orderby_fld input parameter *and*
544+ a name for the resulting serial field (type INT32) in the query column
545+ definition must be given.
523546
524547Example usage
525548
526- CREATE TABLE connectby_tree(keyid text, parent_keyid text);
549+ CREATE TABLE connectby_tree(keyid text, parent_keyid text, pos int );
527550
528- INSERT INTO connectby_tree VALUES('row1',NULL);
529- INSERT INTO connectby_tree VALUES('row2','row1');
530- INSERT INTO connectby_tree VALUES('row3','row1');
531- INSERT INTO connectby_tree VALUES('row4','row2');
532- INSERT INTO connectby_tree VALUES('row5','row2');
533- INSERT INTO connectby_tree VALUES('row6','row4');
534- INSERT INTO connectby_tree VALUES('row7','row3');
535- INSERT INTO connectby_tree VALUES('row8','row6');
536- INSERT INTO connectby_tree VALUES('row9','row5');
551+ INSERT INTO connectby_tree VALUES('row1',NULL, 0 );
552+ INSERT INTO connectby_tree VALUES('row2','row1', 0 );
553+ INSERT INTO connectby_tree VALUES('row3','row1', 0 );
554+ INSERT INTO connectby_tree VALUES('row4','row2', 1 );
555+ INSERT INTO connectby_tree VALUES('row5','row2', 0 );
556+ INSERT INTO connectby_tree VALUES('row6','row4', 0 );
557+ INSERT INTO connectby_tree VALUES('row7','row3', 0 );
558+ INSERT INTO connectby_tree VALUES('row8','row6', 0 );
559+ INSERT INTO connectby_tree VALUES('row9','row5', 0 );
537560
538- -- with branch
561+ -- with branch, without orderby_fld
539562SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
540563 AS t(keyid text, parent_keyid text, level int, branch text);
541564 keyid | parent_keyid | level | branch
@@ -548,7 +571,7 @@ SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~
548571 row9 | row5 | 2 | row2~row5~row9
549572(6 rows)
550573
551- -- without branch
574+ -- without branch, without orderby_fld
552575SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
553576 AS t(keyid text, parent_keyid text, level int);
554577 keyid | parent_keyid | level
@@ -561,6 +584,32 @@ SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
561584 row9 | row5 | 2
562585(6 rows)
563586
587+ -- with branch, with orderby_fld (notice that row5 comes before row4)
588+ SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~')
589+ AS t(keyid text, parent_keyid text, level int, branch text, pos int) ORDER BY t.pos;
590+ keyid | parent_keyid | level | branch | pos
591+ -------+--------------+-------+---------------------+-----
592+ row2 | | 0 | row2 | 1
593+ row5 | row2 | 1 | row2~row5 | 2
594+ row9 | row5 | 2 | row2~row5~row9 | 3
595+ row4 | row2 | 1 | row2~row4 | 4
596+ row6 | row4 | 2 | row2~row4~row6 | 5
597+ row8 | row6 | 3 | row2~row4~row6~row8 | 6
598+ (6 rows)
599+
600+ -- without branch, with orderby_fld (notice that row5 comes before row4)
601+ SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0)
602+ AS t(keyid text, parent_keyid text, level int, pos int) ORDER BY t.pos;
603+ keyid | parent_keyid | level | pos
604+ -------+--------------+-------+-----
605+ row2 | | 0 | 1
606+ row5 | row2 | 1 | 2
607+ row9 | row5 | 2 | 3
608+ row4 | row2 | 1 | 4
609+ row6 | row4 | 2 | 5
610+ row8 | row6 | 3 | 6
611+ (6 rows)
612+
564613==================================================================
565614-- Joe Conway
566615