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

Commitbaf6c8d

Browse files
committed
Please, apply patch for contrib/ltree to current CVS and 7.3.2
CHANGESMar 28, 2003 Added finctions index(ltree,ltree,offset), text2ltree(text), ltree2text(text)Teodor Sigaev
1 parentd307a95 commitbaf6c8d

File tree

5 files changed

+300
-7
lines changed

5 files changed

+300
-7
lines changed

‎contrib/ltree/README.ltree

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,24 @@ int4 nlevel
192192
3
193193
Note, that arguments start, end, OFFSET, LEN have meaning of level of the
194194
node !
195-
195+
196+
int4 index(ltree,ltree), int4 index(ltree,ltree,OFFSET)
197+
returns number of level of the first occurence of second argument in first
198+
one beginning from OFFSET. if OFFSET is negative, than search begins from |
199+
OFFSET| levels from the end of the path.
200+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',3);
201+
index
202+
-------
203+
6
204+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-4);
205+
index
206+
-------
207+
9
208+
209+
ltree text2ltree(text), text ltree2text(text)
210+
cast functions for ltree and text.
211+
212+
196213
ltree lca(ltree,ltree,...) (up to 8 arguments)
197214
ltree lca(ltree[])
198215
Returns Lowest Common Ancestor (lca)
@@ -432,6 +449,9 @@ appreciate your input. So far, below some (rather obvious) results:
432449

433450
CHANGES
434451

452+
Mar 28, 2003
453+
Added functions index(ltree,ltree,offset), text2ltree(text),
454+
ltree2text(text)
435455
Feb 7, 2003
436456
Add ? operation
437457
Fix ~ operation bug: eg '1.1.1' ~ '*.1'

‎contrib/ltree/expected/ltree.out

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
\set ECHO none
22
psql:ltree.sql:9: NOTICE: ProcedureCreate: type ltree is not yet defined
33
psql:ltree.sql:14: NOTICE: Argument type "ltree" is only a shell
4-
psql:ltree.sql:281: NOTICE: ProcedureCreate: type lquery is not yet defined
5-
psql:ltree.sql:286: NOTICE: Argument type "lquery" is only a shell
6-
psql:ltree.sql:392: NOTICE: ProcedureCreate: type ltxtquery is not yet defined
7-
psql:ltree.sql:397: NOTICE: Argument type "ltxtquery" is only a shell
8-
psql:ltree.sql:459: NOTICE: ProcedureCreate: type ltree_gist is not yet defined
9-
psql:ltree.sql:464: NOTICE: Argument type "ltree_gist" is only a shell
4+
psql:ltree.sql:301: NOTICE: ProcedureCreate: type lquery is not yet defined
5+
psql:ltree.sql:306: NOTICE: Argument type "lquery" is only a shell
6+
psql:ltree.sql:412: NOTICE: ProcedureCreate: type ltxtquery is not yet defined
7+
psql:ltree.sql:417: NOTICE: Argument type "ltxtquery" is only a shell
8+
psql:ltree.sql:479: NOTICE: ProcedureCreate: type ltree_gist is not yet defined
9+
psql:ltree.sql:484: NOTICE: Argument type "ltree_gist" is only a shell
1010
SELECT ''::ltree;
1111
ltree
1212
-------
@@ -31,6 +31,18 @@ SELECT '1.2._3'::ltree;
3131
1.2._3
3232
(1 row)
3333

34+
SELECT ltree2text('1.2.3.34.sdf');
35+
ltree2text
36+
--------------
37+
1.2.3.34.sdf
38+
(1 row)
39+
40+
SELECT text2ltree('1.2.3.34.sdf');
41+
text2ltree
42+
--------------
43+
1.2.3.34.sdf
44+
(1 row)
45+
3446
SELECT subltree('Top.Child1.Child2',1,2);
3547
subltree
3648
----------
@@ -85,6 +97,114 @@ SELECT subpath('Top.Child1.Child2',1);
8597
Child1.Child2
8698
(1 row)
8799

100+
SELECT index('1.2.3.4.5.6','1.2');
101+
index
102+
-------
103+
0
104+
(1 row)
105+
106+
SELECT index('a.1.2.3.4.5.6','1.2');
107+
index
108+
-------
109+
1
110+
(1 row)
111+
112+
SELECT index('a.1.2.3.4.5.6','1.2.3');
113+
index
114+
-------
115+
1
116+
(1 row)
117+
118+
SELECT index('a.1.2.3.4.5.6','1.2.3.j');
119+
index
120+
-------
121+
-1
122+
(1 row)
123+
124+
SELECT index('a.1.2.3.4.5.6','1.2.3.j.4.5.5.5.5.5.5');
125+
index
126+
-------
127+
-1
128+
(1 row)
129+
130+
SELECT index('a.1.2.3.4.5.6','1.2.3');
131+
index
132+
-------
133+
1
134+
(1 row)
135+
136+
SELECT index('a.1.2.3.4.5.6','6');
137+
index
138+
-------
139+
6
140+
(1 row)
141+
142+
SELECT index('a.1.2.3.4.5.6','6.1');
143+
index
144+
-------
145+
-1
146+
(1 row)
147+
148+
SELECT index('a.1.2.3.4.5.6','5.6');
149+
index
150+
-------
151+
5
152+
(1 row)
153+
154+
SELECT index('0.1.2.3.5.4.5.6','5.6');
155+
index
156+
-------
157+
6
158+
(1 row)
159+
160+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',3);
161+
index
162+
-------
163+
6
164+
(1 row)
165+
166+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',6);
167+
index
168+
-------
169+
6
170+
(1 row)
171+
172+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',7);
173+
index
174+
-------
175+
9
176+
(1 row)
177+
178+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-7);
179+
index
180+
-------
181+
6
182+
(1 row)
183+
184+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-4);
185+
index
186+
-------
187+
9
188+
(1 row)
189+
190+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-3);
191+
index
192+
-------
193+
9
194+
(1 row)
195+
196+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-2);
197+
index
198+
-------
199+
-1
200+
(1 row)
201+
202+
SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-20000);
203+
index
204+
-------
205+
6
206+
(1 row)
207+
88208
SELECT 'Top.Child1.Child2'::ltree || 'Child3'::text;
89209
?column?
90210
--------------------------

‎contrib/ltree/ltree.sql.in

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,31 @@ RETURNS ltree
137137
AS 'MODULE_PATHNAME'
138138
LANGUAGE 'C' WITH (isstrict,iscachable);
139139

140+
CREATE FUNCTION index(ltree,ltree)
141+
RETURNS int4
142+
AS 'MODULE_PATHNAME', 'ltree_index'
143+
LANGUAGE 'C' WITH (isstrict,iscachable);
144+
145+
CREATE FUNCTION index(ltree,ltree,int4)
146+
RETURNS int4
147+
AS 'MODULE_PATHNAME', 'ltree_index'
148+
LANGUAGE 'C' WITH (isstrict,iscachable);
149+
140150
CREATE FUNCTION nlevel(ltree)
141151
RETURNS int4
142152
AS 'MODULE_PATHNAME'
143153
LANGUAGE 'C' WITH (isstrict,iscachable);
144154

155+
CREATE FUNCTION ltree2text(ltree)
156+
RETURNS text
157+
AS 'MODULE_PATHNAME'
158+
LANGUAGE 'C' WITH (isstrict,iscachable);
159+
160+
CREATE FUNCTION text2ltree(text)
161+
RETURNS ltree
162+
AS 'MODULE_PATHNAME'
163+
LANGUAGE 'C' WITH (isstrict,iscachable);
164+
145165
CREATE FUNCTION lca(_ltree)
146166
RETURNS ltree
147167
AS 'MODULE_PATHNAME','_lca'

‎contrib/ltree/ltree_op.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ PG_FUNCTION_INFO_V1(ltree_isparent);
1919
PG_FUNCTION_INFO_V1(ltree_risparent);
2020
PG_FUNCTION_INFO_V1(subltree);
2121
PG_FUNCTION_INFO_V1(subpath);
22+
PG_FUNCTION_INFO_V1(ltree_index);
2223
PG_FUNCTION_INFO_V1(ltree_addltree);
2324
PG_FUNCTION_INFO_V1(ltree_addtext);
2425
PG_FUNCTION_INFO_V1(ltree_textadd);
2526
PG_FUNCTION_INFO_V1(lca);
27+
PG_FUNCTION_INFO_V1(ltree2text);
28+
PG_FUNCTION_INFO_V1(text2ltree);
2629
Datumltree_cmp(PG_FUNCTION_ARGS);
2730
Datumltree_lt(PG_FUNCTION_ARGS);
2831
Datumltree_le(PG_FUNCTION_ARGS);
@@ -33,10 +36,13 @@ Datumltree_gt(PG_FUNCTION_ARGS);
3336
Datumnlevel(PG_FUNCTION_ARGS);
3437
Datumsubltree(PG_FUNCTION_ARGS);
3538
Datumsubpath(PG_FUNCTION_ARGS);
39+
Datumltree_index(PG_FUNCTION_ARGS);
3640
Datumltree_addltree(PG_FUNCTION_ARGS);
3741
Datumltree_addtext(PG_FUNCTION_ARGS);
3842
Datumltree_textadd(PG_FUNCTION_ARGS);
3943
Datumlca(PG_FUNCTION_ARGS);
44+
Datumltree2text(PG_FUNCTION_ARGS);
45+
Datumtext2ltree(PG_FUNCTION_ARGS);
4046

4147
int
4248
ltree_compare(constltree*a,constltree*b)
@@ -317,6 +323,57 @@ ltree_addtext(PG_FUNCTION_ARGS)
317323
PG_RETURN_POINTER(r);
318324
}
319325

326+
Datum
327+
ltree_index(PG_FUNCTION_ARGS)
328+
{
329+
ltree*a=PG_GETARG_LTREE(0);
330+
ltree*b=PG_GETARG_LTREE(1);
331+
intstart=(fcinfo->nargs==3) ?PG_GETARG_INT32(2) :0;
332+
inti,j;
333+
ltree_level*startptr,*aptr,*bptr;
334+
boolfound=false;
335+
336+
if (start<0 ) {
337+
if (-start >=a->numlevel )
338+
start=0;
339+
else
340+
start= (int)(a->numlevel)+start;
341+
}
342+
343+
if (a->numlevel-start<b->numlevel||a->numlevel==0||b->numlevel==0 ) {
344+
PG_FREE_IF_COPY(a,0);
345+
PG_FREE_IF_COPY(b,1);
346+
PG_RETURN_INT32(-1);
347+
}
348+
349+
startptr=LTREE_FIRST(a);
350+
for(i=0;i<=a->numlevel-b->numlevel;i++) {
351+
if (i>=start ) {
352+
aptr=startptr;
353+
bptr=LTREE_FIRST(b);
354+
for(j=0;j<b->numlevel;j++) {
355+
if ( !(aptr->len==bptr->len&&strncmp(aptr->name,bptr->name,aptr->len)==0) )
356+
break;
357+
aptr=LEVEL_NEXT(aptr);
358+
bptr=LEVEL_NEXT(bptr);
359+
}
360+
361+
if (j==b->numlevel ) {
362+
found=true;
363+
break;
364+
}
365+
}
366+
startptr=LEVEL_NEXT(startptr);
367+
}
368+
369+
if ( !found )
370+
i=-1;
371+
372+
PG_FREE_IF_COPY(a,0);
373+
PG_FREE_IF_COPY(b,1);
374+
PG_RETURN_INT32(i);
375+
}
376+
320377
Datum
321378
ltree_textadd(PG_FUNCTION_ARGS)
322379
{
@@ -431,3 +488,55 @@ lca(PG_FUNCTION_ARGS)
431488
else
432489
PG_RETURN_NULL();
433490
}
491+
492+
Datum
493+
text2ltree(PG_FUNCTION_ARGS)
494+
{
495+
text*in=PG_GETARG_TEXT_P(0);
496+
char*s= (char*)palloc(VARSIZE(in)-VARHDRSZ+1);
497+
ltree*out;
498+
499+
memcpy(s,VARDATA(in),VARSIZE(in)-VARHDRSZ);
500+
s[VARSIZE(in)-VARHDRSZ]='\0';
501+
502+
out= (ltree*)DatumGetPointer(DirectFunctionCall1(
503+
ltree_in,
504+
PointerGetDatum(s)
505+
));
506+
pfree(s);
507+
PG_FREE_IF_COPY(in,0);
508+
PG_RETURN_POINTER(out);
509+
}
510+
511+
512+
Datum
513+
ltree2text(PG_FUNCTION_ARGS)
514+
{
515+
ltree*in=PG_GETARG_LTREE(0);
516+
char*ptr;
517+
inti;
518+
ltree_level*curlevel;
519+
text*out;
520+
521+
out=(text*)palloc(in->len+VARHDRSZ);
522+
ptr=VARDATA(out);
523+
curlevel=LTREE_FIRST(in);
524+
for (i=0;i<in->numlevel;i++) {
525+
if (i!=0) {
526+
*ptr='.';
527+
ptr++;
528+
}
529+
memcpy(ptr,curlevel->name,curlevel->len);
530+
ptr+=curlevel->len;
531+
curlevel=LEVEL_NEXT(curlevel);
532+
}
533+
534+
VARATT_SIZEP(out)=VARHDRSZ+ (ptr-VARDATA(out));
535+
PG_FREE_IF_COPY(in,0);
536+
537+
PG_RETURN_POINTER(out);
538+
}
539+
540+
541+
542+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp