v13 + other support
[smlar.git] / tsarr.c
1 #include "postgres.h"
2
3 #include "catalog/pg_type.h"
4 #include "tsearch/ts_type.h"
5 #include "utils/array.h"
6
7 PG_FUNCTION_INFO_V1(tsvector2textarray);
8 Datum       tsvector2textarray(PG_FUNCTION_ARGS);
9 Datum
10 tsvector2textarray(PG_FUNCTION_ARGS)
11 {
12         TSVector        ts = PG_GETARG_TSVECTOR(0);
13         ArrayType       *a;
14         Datum           *words;
15         int                     i;
16         WordEntry       *wptr = ARRPTR(ts);
17
18         words = palloc( sizeof(Datum) * (ts->size+1) );
19
20         for(i=0; i<ts->size; i++)
21         {
22                 text            *t = palloc(VARHDRSZ + wptr->len);
23
24                 SET_VARSIZE(t, VARHDRSZ + wptr->len);
25                 memcpy( VARDATA(t), STRPTR(ts) + wptr->pos, wptr->len);
26                 words[i] = PointerGetDatum(t);
27         
28                 wptr++;
29         }
30
31         a = construct_array( words, ts->size,
32                                                         TEXTOID, -1, false, 'i' );
33
34         PG_FREE_IF_COPY(ts, 0);
35
36         PG_RETURN_ARRAYTYPE_P(a);
37 }
38