Line data Source code
1 : /*
2 : * txtquery operations with ltree
3 : * Teodor Sigaev <teodor@stack.net>
4 : * contrib/ltree/ltxtquery_op.c
5 : */
6 : #include "postgres.h"
7 :
8 : #include <ctype.h>
9 :
10 : #include "ltree.h"
11 : #include "miscadmin.h"
12 :
13 0 : PG_FUNCTION_INFO_V1(ltxtq_exec);
14 0 : PG_FUNCTION_INFO_V1(ltxtq_rexec);
15 :
16 : /*
17 : * check for boolean condition
18 : */
19 : bool
20 0 : ltree_execute(ITEM *curitem, void *checkval, bool calcnot, bool (*chkcond) (void *checkval, ITEM *val))
21 : {
22 : /* since this function recurses, it could be driven to stack overflow */
23 0 : check_stack_depth();
24 :
25 0 : if (curitem->type == VAL)
26 0 : return (*chkcond) (checkval, curitem);
27 0 : else if (curitem->val == (int32) '!')
28 : {
29 0 : return calcnot ?
30 0 : ((ltree_execute(curitem + 1, checkval, calcnot, chkcond)) ? false : true)
31 : : true;
32 : }
33 0 : else if (curitem->val == (int32) '&')
34 : {
35 0 : if (ltree_execute(curitem + curitem->left, checkval, calcnot, chkcond))
36 0 : return ltree_execute(curitem + 1, checkval, calcnot, chkcond);
37 : else
38 0 : return false;
39 : }
40 : else
41 : { /* |-operator */
42 0 : if (ltree_execute(curitem + curitem->left, checkval, calcnot, chkcond))
43 0 : return true;
44 : else
45 0 : return ltree_execute(curitem + 1, checkval, calcnot, chkcond);
46 : }
47 0 : }
48 :
49 : typedef struct
50 : {
51 : ltree *node;
52 : char *operand;
53 : } CHKVAL;
54 :
55 : static bool
56 0 : checkcondition_str(void *checkval, ITEM *val)
57 : {
58 0 : ltree_level *level = LTREE_FIRST(((CHKVAL *) checkval)->node);
59 0 : int tlen = ((CHKVAL *) checkval)->node->numlevel;
60 0 : char *op = ((CHKVAL *) checkval)->operand + val->distance;
61 0 : ltree_prefix_eq_func prefix_eq;
62 :
63 0 : prefix_eq = (val->flag & LVAR_INCASE) ? ltree_prefix_eq_ci : ltree_prefix_eq;
64 0 : while (tlen > 0)
65 : {
66 0 : if (val->flag & LVAR_SUBLEXEME)
67 : {
68 0 : if (compare_subnode(level, op, val->length, prefix_eq, (val->flag & LVAR_ANYEND)))
69 0 : return true;
70 0 : }
71 0 : else if ((val->length == level->len ||
72 0 : (level->len > val->length && (val->flag & LVAR_ANYEND))) &&
73 0 : (*prefix_eq) (op, val->length, level->name, level->len))
74 0 : return true;
75 :
76 0 : tlen--;
77 0 : level = LEVEL_NEXT(level);
78 : }
79 :
80 0 : return false;
81 0 : }
82 :
83 : Datum
84 0 : ltxtq_exec(PG_FUNCTION_ARGS)
85 : {
86 0 : ltree *val = PG_GETARG_LTREE_P(0);
87 0 : ltxtquery *query = PG_GETARG_LTXTQUERY_P(1);
88 0 : CHKVAL chkval;
89 0 : bool result;
90 :
91 0 : chkval.node = val;
92 0 : chkval.operand = GETOPERAND(query);
93 :
94 0 : result = ltree_execute(GETQUERY(query),
95 : &chkval,
96 : true,
97 : checkcondition_str);
98 :
99 0 : PG_FREE_IF_COPY(val, 0);
100 0 : PG_FREE_IF_COPY(query, 1);
101 0 : PG_RETURN_BOOL(result);
102 0 : }
103 :
104 : Datum
105 0 : ltxtq_rexec(PG_FUNCTION_ARGS)
106 : {
107 0 : PG_RETURN_DATUM(DirectFunctionCall2(ltxtq_exec,
108 : PG_GETARG_DATUM(1),
109 : PG_GETARG_DATUM(0)
110 : ));
111 : }
|