Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita265b7f

Browse files
committed
> Am Son, 2003-06-22 um 02.09 schrieb Joe Conway:
>>Sounds like all that's needed for your case. But to be complete, in>>addition to changing tablefunc.c we'd have to:>>1) come up with a new function call signature that makes sense and does>>not cause backward compatibility problems for other people>>2) make needed changes to tablefunc.sql.in>>3) adjust the README.tablefunc appropriately>>4) adjust the regression test for new functionality>>5) be sure we don't break any of the old cases>>>>If you want to submit a complete patch, it would be gratefully accepted>>-- for review at least ;-)>> Here's the patch, at least for steps 1-3Nabil SayeghJoe Conway
1 parent9ae6905 commita265b7f

File tree

7 files changed

+309
-61
lines changed

7 files changed

+309
-61
lines changed

‎contrib/tablefunc/README.tablefunc

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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

@@ -452,13 +456,14 @@ AS
452456
==================================================================
453457
Name
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

458462
Synopsis
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

463468
Inputs
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

504524
Notes
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

524547
Example 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
539562
SELECT * 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
552575
SELECT * 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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
row1\N
2-
row2row1
3-
row3row1
4-
row4row2
5-
row5row2
6-
row6row4
7-
row7row3
8-
row8row6
9-
row9row5
1+
row1\N0
2+
row2row10
3+
row3row10
4+
row4row21
5+
row5row20
6+
row6row40
7+
row7row30
8+
row8row60
9+
row9row50

‎contrib/tablefunc/expected/tablefunc.out

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ ERROR: provided "categories" SQL must return 1 column of at least one row
197197
-- connectby
198198
--
199199
-- test connectby with text based hierarchy
200-
CREATE TABLE connectby_text(keyid text, parent_keyid text);
200+
CREATE TABLE connectby_text(keyid text, parent_keyid text, pos int);
201201
\copy connectby_text from 'data/connectby_text.data'
202-
-- with branch
202+
-- with branch, without orderby
203203
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text);
204204
keyid | parent_keyid | level | branch
205205
-------+--------------+-------+---------------------
@@ -211,7 +211,7 @@ SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0, '~
211211
row9 | row5 | 2 | row2~row5~row9
212212
(6 rows)
213213

214-
-- without branch
214+
-- without branch, without orderby
215215
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int);
216216
keyid | parent_keyid | level
217217
-------+--------------+-------
@@ -223,6 +223,30 @@ SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0) AS
223223
row9 | row5 | 2
224224
(6 rows)
225225

226+
-- with branch, with orderby
227+
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text, pos int) ORDER BY t.pos;
228+
keyid | parent_keyid | level | branch | pos
229+
-------+--------------+-------+---------------------+-----
230+
row2 | | 0 | row2 | 1
231+
row5 | row2 | 1 | row2~row5 | 2
232+
row9 | row5 | 2 | row2~row5~row9 | 3
233+
row4 | row2 | 1 | row2~row4 | 4
234+
row6 | row4 | 2 | row2~row4~row6 | 5
235+
row8 | row6 | 3 | row2~row4~row6~row8 | 6
236+
(6 rows)
237+
238+
-- without branch, with orderby
239+
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0) AS t(keyid text, parent_keyid text, level int, pos int) ORDER BY t.pos;
240+
keyid | parent_keyid | level | pos
241+
-------+--------------+-------+-----
242+
row2 | | 0 | 1
243+
row5 | row2 | 1 | 2
244+
row9 | row5 | 2 | 3
245+
row4 | row2 | 1 | 4
246+
row6 | row4 | 2 | 5
247+
row8 | row6 | 3 | 6
248+
(6 rows)
249+
226250
-- test connectby with int based hierarchy
227251
CREATE TABLE connectby_int(keyid int, parent_keyid int);
228252
\copy connectby_int from 'data/connectby_int.data'

‎contrib/tablefunc/sql/tablefunc.sql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,21 @@ AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_start
9494
--
9595

9696
-- test connectby with text based hierarchy
97-
CREATETABLEconnectby_text(keyidtext, parent_keyidtext);
97+
CREATETABLEconnectby_text(keyidtext, parent_keyidtext, posint);
9898
\copy connectby_textfrom'data/connectby_text.data'
9999

100-
-- with branch
100+
-- with branch, without orderby
101101
SELECT*FROM connectby('connectby_text','keyid','parent_keyid','row2',0,'~')AS t(keyidtext, parent_keyidtext, levelint, branchtext);
102102

103-
-- without branch
103+
-- without branch, without orderby
104104
SELECT*FROM connectby('connectby_text','keyid','parent_keyid','row2',0)AS t(keyidtext, parent_keyidtext, levelint);
105105

106+
-- with branch, with orderby
107+
SELECT*FROM connectby('connectby_text','keyid','parent_keyid','pos','row2',0,'~')AS t(keyidtext, parent_keyidtext, levelint, branchtext, posint)ORDER BYt.pos;
108+
109+
-- without branch, with orderby
110+
SELECT*FROM connectby('connectby_text','keyid','parent_keyid','pos','row2',0)AS t(keyidtext, parent_keyidtext, levelint, posint)ORDER BYt.pos;
111+
106112
-- test connectby with int based hierarchy
107113
CREATETABLEconnectby_int(keyidint, parent_keyidint);
108114
\copy connectby_intfrom'data/connectby_int.data'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp