PostgreSQL Source Code  git master
_ltree_op.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "ltree.h"
#include "utils/array.h"
Include dependency graph for _ltree_op.c:

Go to the source code of this file.

Macros

#define NEXTVAL(x)   ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
 

Typedefs

typedef Datum(* PGCALL2) (PG_FUNCTION_ARGS)
 

Functions

 PG_FUNCTION_INFO_V1 (_ltree_isparent)
 
 PG_FUNCTION_INFO_V1 (_ltree_r_isparent)
 
 PG_FUNCTION_INFO_V1 (_ltree_risparent)
 
 PG_FUNCTION_INFO_V1 (_ltree_r_risparent)
 
 PG_FUNCTION_INFO_V1 (_ltq_regex)
 
 PG_FUNCTION_INFO_V1 (_ltq_rregex)
 
 PG_FUNCTION_INFO_V1 (_lt_q_regex)
 
 PG_FUNCTION_INFO_V1 (_lt_q_rregex)
 
 PG_FUNCTION_INFO_V1 (_ltxtq_exec)
 
 PG_FUNCTION_INFO_V1 (_ltxtq_rexec)
 
 PG_FUNCTION_INFO_V1 (_ltree_extract_isparent)
 
 PG_FUNCTION_INFO_V1 (_ltree_extract_risparent)
 
 PG_FUNCTION_INFO_V1 (_ltq_extract_regex)
 
 PG_FUNCTION_INFO_V1 (_ltxtq_extract_exec)
 
 PG_FUNCTION_INFO_V1 (_lca)
 
static bool array_iterator (ArrayType *la, PGCALL2 callback, void *param, ltree **found)
 
Datum _ltree_isparent (PG_FUNCTION_ARGS)
 
Datum _ltree_r_isparent (PG_FUNCTION_ARGS)
 
Datum _ltree_risparent (PG_FUNCTION_ARGS)
 
Datum _ltree_r_risparent (PG_FUNCTION_ARGS)
 
Datum _ltq_regex (PG_FUNCTION_ARGS)
 
Datum _ltq_rregex (PG_FUNCTION_ARGS)
 
Datum _lt_q_regex (PG_FUNCTION_ARGS)
 
Datum _lt_q_rregex (PG_FUNCTION_ARGS)
 
Datum _ltxtq_exec (PG_FUNCTION_ARGS)
 
Datum _ltxtq_rexec (PG_FUNCTION_ARGS)
 
Datum _ltree_extract_isparent (PG_FUNCTION_ARGS)
 
Datum _ltree_extract_risparent (PG_FUNCTION_ARGS)
 
Datum _ltq_extract_regex (PG_FUNCTION_ARGS)
 
Datum _ltxtq_extract_exec (PG_FUNCTION_ARGS)
 
Datum _lca (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ NEXTVAL

#define NEXTVAL (   x)    ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )

Definition at line 35 of file _ltree_op.c.

Typedef Documentation

◆ PGCALL2

typedef Datum(* PGCALL2) (PG_FUNCTION_ARGS)

Definition at line 33 of file _ltree_op.c.

Function Documentation

◆ _lca()

Datum _lca ( PG_FUNCTION_ARGS  )

Definition at line 293 of file _ltree_op.c.

294 {
296  int num = ArrayGetNItems(ARR_NDIM(la), ARR_DIMS(la));
297  ltree *item = (ltree *) ARR_DATA_PTR(la);
298  ltree **a,
299  *res;
300 
301  if (ARR_NDIM(la) > 1)
302  ereport(ERROR,
303  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
304  errmsg("array must be one-dimensional")));
305  if (array_contains_nulls(la))
306  ereport(ERROR,
307  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
308  errmsg("array must not contain nulls")));
309 
310  a = (ltree **) palloc(sizeof(ltree *) * num);
311  while (num > 0)
312  {
313  num--;
314  a[num] = item;
315  item = NEXTVAL(item);
316  }
318  pfree(a);
319 
320  PG_FREE_IF_COPY(la, 0);
321 
322  if (res)
324  else
325  PG_RETURN_NULL();
326 }
#define NEXTVAL(x)
Definition: _ltree_op.c:35
#define ARR_NDIM(a)
Definition: array.h:283
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_DIMS(a)
Definition: array.h:287
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3712
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:76
int errcode(int sqlerrcode)
Definition: elog.c:735
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
int a
Definition: isn.c:69
ltree * lca_inner(ltree **a, int len)
Definition: ltree_op.c:426
void pfree(void *pointer)
Definition: mcxt.c:1306
void * palloc(Size size)
Definition: mcxt.c:1199
Definition: ltree.h:43

References a, ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), ArrayGetNItems(), ereport, errcode(), errmsg(), ERROR, lca_inner(), NEXTVAL, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_RETURN_NULL, PG_RETURN_POINTER, and res.

◆ _lt_q_regex()

Datum _lt_q_regex ( PG_FUNCTION_ARGS  )

Definition at line 135 of file _ltree_op.c.

136 {
137  ArrayType *_tree = PG_GETARG_ARRAYTYPE_P(0);
138  ArrayType *_query = PG_GETARG_ARRAYTYPE_P(1);
139  lquery *query = (lquery *) ARR_DATA_PTR(_query);
140  bool res = false;
141  int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
142 
143  if (ARR_NDIM(_query) > 1)
144  ereport(ERROR,
145  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
146  errmsg("array must be one-dimensional")));
147  if (array_contains_nulls(_query))
148  ereport(ERROR,
149  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
150  errmsg("array must not contain nulls")));
151 
152  while (num > 0)
153  {
154  if (array_iterator(_tree, ltq_regex, (void *) query, NULL))
155  {
156  res = true;
157  break;
158  }
159  num--;
160  query = (lquery *) NEXTVAL(query);
161  }
162 
163  PG_FREE_IF_COPY(_tree, 0);
164  PG_FREE_IF_COPY(_query, 1);
166 }
static bool array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree **found)
Definition: _ltree_op.c:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum ltq_regex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:215
Definition: ltree.h:114

References ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), array_iterator(), ArrayGetNItems(), ereport, errcode(), errmsg(), ERROR, ltq_regex(), NEXTVAL, PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_RETURN_BOOL, and res.

Referenced by _lt_q_rregex().

◆ _lt_q_rregex()

Datum _lt_q_rregex ( PG_FUNCTION_ARGS  )

Definition at line 169 of file _ltree_op.c.

170 {
172  PG_GETARG_DATUM(1),
173  PG_GETARG_DATUM(0)
174  ));
175 }
Datum _lt_q_regex(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:135
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353

References _lt_q_regex(), DirectFunctionCall2, PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ _ltq_extract_regex()

Datum _ltq_extract_regex ( PG_FUNCTION_ARGS  )

Definition at line 247 of file _ltree_op.c.

248 {
250  lquery *query = PG_GETARG_LQUERY_P(1);
251  ltree *found,
252  *item;
253 
254  if (!array_iterator(la, ltq_regex, (void *) query, &found))
255  {
256  PG_FREE_IF_COPY(la, 0);
257  PG_FREE_IF_COPY(query, 1);
258  PG_RETURN_NULL();
259  }
260 
261  item = (ltree *) palloc0(VARSIZE(found));
262  memcpy(item, found, VARSIZE(found));
263 
264  PG_FREE_IF_COPY(la, 0);
265  PG_FREE_IF_COPY(query, 1);
266  PG_RETURN_POINTER(item);
267 }
#define PG_GETARG_LQUERY_P(n)
Definition: ltree.h:222
void * palloc0(Size size)
Definition: mcxt.c:1230
#define VARSIZE(PTR)
Definition: postgres.h:317

References array_iterator(), ltq_regex(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LQUERY_P, PG_RETURN_NULL, PG_RETURN_POINTER, and VARSIZE.

◆ _ltq_regex()

Datum _ltq_regex ( PG_FUNCTION_ARGS  )

Definition at line 114 of file _ltree_op.c.

115 {
117  lquery *query = PG_GETARG_LQUERY_P(1);
118  bool res = array_iterator(la, ltq_regex, (void *) query, NULL);
119 
120  PG_FREE_IF_COPY(la, 0);
121  PG_FREE_IF_COPY(query, 1);
123 }

References array_iterator(), ltq_regex(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LQUERY_P, PG_RETURN_BOOL, and res.

Referenced by _ltq_rregex().

◆ _ltq_rregex()

Datum _ltq_rregex ( PG_FUNCTION_ARGS  )

Definition at line 126 of file _ltree_op.c.

127 {
129  PG_GETARG_DATUM(1),
130  PG_GETARG_DATUM(0)
131  ));
132 }
Datum _ltq_regex(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:114

References _ltq_regex(), DirectFunctionCall2, PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ _ltree_extract_isparent()

Datum _ltree_extract_isparent ( PG_FUNCTION_ARGS  )

Definition at line 201 of file _ltree_op.c.

202 {
204  ltree *query = PG_GETARG_LTREE_P(1);
205  ltree *found,
206  *item;
207 
208  if (!array_iterator(la, ltree_isparent, (void *) query, &found))
209  {
210  PG_FREE_IF_COPY(la, 0);
211  PG_FREE_IF_COPY(query, 1);
212  PG_RETURN_NULL();
213  }
214 
215  item = (ltree *) palloc0(VARSIZE(found));
216  memcpy(item, found, VARSIZE(found));
217 
218  PG_FREE_IF_COPY(la, 0);
219  PG_FREE_IF_COPY(query, 1);
220  PG_RETURN_POINTER(item);
221 }
PGDLLEXPORT Datum ltree_isparent(PG_FUNCTION_ARGS)
Definition: ltree_op.c:167
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:217

References array_iterator(), ltree_isparent(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LTREE_P, PG_RETURN_NULL, PG_RETURN_POINTER, and VARSIZE.

◆ _ltree_extract_risparent()

Datum _ltree_extract_risparent ( PG_FUNCTION_ARGS  )

Definition at line 224 of file _ltree_op.c.

225 {
227  ltree *query = PG_GETARG_LTREE_P(1);
228  ltree *found,
229  *item;
230 
231  if (!array_iterator(la, ltree_risparent, (void *) query, &found))
232  {
233  PG_FREE_IF_COPY(la, 0);
234  PG_FREE_IF_COPY(query, 1);
235  PG_RETURN_NULL();
236  }
237 
238  item = (ltree *) palloc0(VARSIZE(found));
239  memcpy(item, found, VARSIZE(found));
240 
241  PG_FREE_IF_COPY(la, 0);
242  PG_FREE_IF_COPY(query, 1);
243  PG_RETURN_POINTER(item);
244 }
PGDLLEXPORT Datum ltree_risparent(PG_FUNCTION_ARGS)
Definition: ltree_op.c:179

References array_iterator(), ltree_risparent(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LTREE_P, PG_RETURN_NULL, PG_RETURN_POINTER, and VARSIZE.

◆ _ltree_isparent()

Datum _ltree_isparent ( PG_FUNCTION_ARGS  )

Definition at line 72 of file _ltree_op.c.

73 {
75  ltree *query = PG_GETARG_LTREE_P(1);
76  bool res = array_iterator(la, ltree_isparent, (void *) query, NULL);
77 
78  PG_FREE_IF_COPY(la, 0);
79  PG_FREE_IF_COPY(query, 1);
81 }

References array_iterator(), ltree_isparent(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LTREE_P, PG_RETURN_BOOL, and res.

Referenced by _ltree_r_isparent().

◆ _ltree_r_isparent()

Datum _ltree_r_isparent ( PG_FUNCTION_ARGS  )

Definition at line 84 of file _ltree_op.c.

85 {
87  PG_GETARG_DATUM(1),
89  ));
90 }
Datum _ltree_isparent(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:72

References _ltree_isparent(), DirectFunctionCall2, PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ _ltree_r_risparent()

Datum _ltree_r_risparent ( PG_FUNCTION_ARGS  )

Definition at line 105 of file _ltree_op.c.

106 {
108  PG_GETARG_DATUM(1),
109  PG_GETARG_DATUM(0)
110  ));
111 }
Datum _ltree_risparent(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:93

References _ltree_risparent(), DirectFunctionCall2, PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ _ltree_risparent()

Datum _ltree_risparent ( PG_FUNCTION_ARGS  )

Definition at line 93 of file _ltree_op.c.

94 {
96  ltree *query = PG_GETARG_LTREE_P(1);
97  bool res = array_iterator(la, ltree_risparent, (void *) query, NULL);
98 
99  PG_FREE_IF_COPY(la, 0);
100  PG_FREE_IF_COPY(query, 1);
102 }

References array_iterator(), ltree_risparent(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LTREE_P, PG_RETURN_BOOL, and res.

Referenced by _ltree_r_risparent().

◆ _ltxtq_exec()

Datum _ltxtq_exec ( PG_FUNCTION_ARGS  )

Definition at line 179 of file _ltree_op.c.

180 {
182  ltxtquery *query = PG_GETARG_LTXTQUERY_P(1);
183  bool res = array_iterator(la, ltxtq_exec, (void *) query, NULL);
184 
185  PG_FREE_IF_COPY(la, 0);
186  PG_FREE_IF_COPY(query, 1);
188 }
#define PG_GETARG_LTXTQUERY_P(n)
Definition: ltree.h:227
PGDLLEXPORT Datum ltxtq_exec(PG_FUNCTION_ARGS)
Definition: ltxtquery_op.c:84

References array_iterator(), ltxtq_exec(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LTXTQUERY_P, PG_RETURN_BOOL, and res.

Referenced by _ltxtq_rexec().

◆ _ltxtq_extract_exec()

Datum _ltxtq_extract_exec ( PG_FUNCTION_ARGS  )

Definition at line 270 of file _ltree_op.c.

271 {
273  ltxtquery *query = PG_GETARG_LTXTQUERY_P(1);
274  ltree *found,
275  *item;
276 
277  if (!array_iterator(la, ltxtq_exec, (void *) query, &found))
278  {
279  PG_FREE_IF_COPY(la, 0);
280  PG_FREE_IF_COPY(query, 1);
281  PG_RETURN_NULL();
282  }
283 
284  item = (ltree *) palloc0(VARSIZE(found));
285  memcpy(item, found, VARSIZE(found));
286 
287  PG_FREE_IF_COPY(la, 0);
288  PG_FREE_IF_COPY(query, 1);
289  PG_RETURN_POINTER(item);
290 }

References array_iterator(), ltxtq_exec(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LTXTQUERY_P, PG_RETURN_NULL, PG_RETURN_POINTER, and VARSIZE.

◆ _ltxtq_rexec()

Datum _ltxtq_rexec ( PG_FUNCTION_ARGS  )

Definition at line 191 of file _ltree_op.c.

192 {
194  PG_GETARG_DATUM(1),
195  PG_GETARG_DATUM(0)
196  ));
197 }
Datum _ltxtq_exec(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:179

References _ltxtq_exec(), DirectFunctionCall2, PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ array_iterator()

static bool array_iterator ( ArrayType la,
PGCALL2  callback,
void *  param,
ltree **  found 
)
static

Definition at line 38 of file _ltree_op.c.

39 {
40  int num = ArrayGetNItems(ARR_NDIM(la), ARR_DIMS(la));
41  ltree *item = (ltree *) ARR_DATA_PTR(la);
42 
43  if (ARR_NDIM(la) > 1)
44  ereport(ERROR,
45  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
46  errmsg("array must be one-dimensional")));
47  if (array_contains_nulls(la))
48  ereport(ERROR,
49  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
50  errmsg("array must not contain nulls")));
51 
52  if (found)
53  *found = NULL;
54  while (num > 0)
55  {
57  PointerGetDatum(item), PointerGetDatum(param))))
58  {
59 
60  if (found)
61  *found = item;
62  return true;
63  }
64  num--;
65  item = NEXTVAL(item);
66  }
67 
68  return false;
69 }
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), ArrayGetNItems(), callback(), DatumGetBool(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, NEXTVAL, and PointerGetDatum().

Referenced by _lt_q_regex(), _ltq_extract_regex(), _ltq_regex(), _ltree_extract_isparent(), _ltree_extract_risparent(), _ltree_isparent(), _ltree_risparent(), _ltxtq_exec(), _ltxtq_extract_exec(), array_position_common(), array_positions(), and exec_stmt_foreach_a().

◆ PG_FUNCTION_INFO_V1() [1/15]

PG_FUNCTION_INFO_V1 ( _lca  )

◆ PG_FUNCTION_INFO_V1() [2/15]

PG_FUNCTION_INFO_V1 ( _lt_q_regex  )

◆ PG_FUNCTION_INFO_V1() [3/15]

PG_FUNCTION_INFO_V1 ( _lt_q_rregex  )

◆ PG_FUNCTION_INFO_V1() [4/15]

PG_FUNCTION_INFO_V1 ( _ltq_extract_regex  )

◆ PG_FUNCTION_INFO_V1() [5/15]

PG_FUNCTION_INFO_V1 ( _ltq_regex  )

◆ PG_FUNCTION_INFO_V1() [6/15]

PG_FUNCTION_INFO_V1 ( _ltq_rregex  )

◆ PG_FUNCTION_INFO_V1() [7/15]

PG_FUNCTION_INFO_V1 ( _ltree_extract_isparent  )

◆ PG_FUNCTION_INFO_V1() [8/15]

PG_FUNCTION_INFO_V1 ( _ltree_extract_risparent  )

◆ PG_FUNCTION_INFO_V1() [9/15]

PG_FUNCTION_INFO_V1 ( _ltree_isparent  )

◆ PG_FUNCTION_INFO_V1() [10/15]

PG_FUNCTION_INFO_V1 ( _ltree_r_isparent  )

◆ PG_FUNCTION_INFO_V1() [11/15]

PG_FUNCTION_INFO_V1 ( _ltree_r_risparent  )

◆ PG_FUNCTION_INFO_V1() [12/15]

PG_FUNCTION_INFO_V1 ( _ltree_risparent  )

◆ PG_FUNCTION_INFO_V1() [13/15]

PG_FUNCTION_INFO_V1 ( _ltxtq_exec  )

◆ PG_FUNCTION_INFO_V1() [14/15]

PG_FUNCTION_INFO_V1 ( _ltxtq_extract_exec  )

◆ PG_FUNCTION_INFO_V1() [15/15]

PG_FUNCTION_INFO_V1 ( _ltxtq_rexec  )