| 1 |
/* |
| 2 |
* txtquery operations with ltree |
| 3 |
* Teodor Sigaev <[email protected]> |
| 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 |
PG_FUNCTION_INFO_V1(ltxtq_exec); |
| 14 |
PG_FUNCTION_INFO_V1(ltxtq_rexec); |
| 15 |
|
| 16 |
/* |
| 17 |
* check for boolean condition |
| 18 |
*/ |
| 19 |
bool |
| 20 |
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 |
check_stack_depth(); |
| 24 |
|
| 25 |
if (curitem->type == VAL) |
| 26 |
return (*chkcond) (checkval, curitem); |
| 27 |
else if (curitem->val == (int32) '!') |
| 28 |
{ |
| 29 |
return calcnot ? |
| 30 |
((ltree_execute(curitem + 1, checkval, calcnot, chkcond)) ? false : true) |
| 31 |
: true; |
| 32 |
} |
| 33 |
else if (curitem->val == (int32) '&') |
| 34 |
{ |
| 35 |
if (ltree_execute(curitem + curitem->left, checkval, calcnot, chkcond)) |
| 36 |
return ltree_execute(curitem + 1, checkval, calcnot, chkcond); |
| 37 |
else |
| 38 |
return false; |
| 39 |
} |
| 40 |
else |
| 41 |
{ /* |-operator */ |
| 42 |
if (ltree_execute(curitem + curitem->left, checkval, calcnot, chkcond)) |
| 43 |
return true; |
| 44 |
else |
| 45 |
return ltree_execute(curitem + 1, checkval, calcnot, chkcond); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
typedef struct |
| 50 |
{ |
| 51 |
ltree *node; |
| 52 |
char *operand; |
| 53 |
} CHKVAL; |
| 54 |
|
| 55 |
static bool |
| 56 |
checkcondition_str(void *checkval, ITEM *val) |
| 57 |
{ |
| 58 |
ltree_level *level = LTREE_FIRST(((CHKVAL *) checkval)->node); |
| 59 |
int tlen = ((CHKVAL *) checkval)->node->numlevel; |
| 60 |
char *op = ((CHKVAL *) checkval)->operand + val->distance; |
| 61 |
bool prefix = (val->flag & LVAR_ANYEND); |
| 62 |
bool ci = (val->flag & LVAR_INCASE); |
| 63 |
|
| 64 |
while (tlen > 0) |
| 65 |
{ |
| 66 |
if (val->flag & LVAR_SUBLEXEME) |
| 67 |
{ |
| 68 |
if (compare_subnode(level, op, val->length, prefix, ci)) |
| 69 |
return true; |
| 70 |
} |
| 71 |
else if (ltree_label_match(op, val->length, level->name, level->len, |
| 72 |
prefix, ci)) |
| 73 |
return true; |
| 74 |
|
| 75 |
tlen--; |
| 76 |
level = LEVEL_NEXT(level); |
| 77 |
} |
| 78 |
|
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
Datum |
| 83 |
ltxtq_exec(PG_FUNCTION_ARGS) |
| 84 |
{ |
| 85 |
ltree *val = PG_GETARG_LTREE_P(0); |
| 86 |
ltxtquery *query = PG_GETARG_LTXTQUERY_P(1); |
| 87 |
CHKVAL chkval; |
| 88 |
bool result; |
| 89 |
|
| 90 |
chkval.node = val; |
| 91 |
chkval.operand = GETOPERAND(query); |
| 92 |
|
| 93 |
result = ltree_execute(GETQUERY(query), |
| 94 |
&chkval, |
| 95 |
true, |
| 96 |
checkcondition_str); |
| 97 |
|
| 98 |
PG_FREE_IF_COPY(val, 0); |
| 99 |
PG_FREE_IF_COPY(query, 1); |
| 100 |
PG_RETURN_BOOL(result); |
| 101 |
} |
| 102 |
|
| 103 |
Datum |
| 104 |
ltxtq_rexec(PG_FUNCTION_ARGS) |
| 105 |
{ |
| 106 |
PG_RETURN_DATUM(DirectFunctionCall2(ltxtq_exec, |
| 107 |
PG_GETARG_DATUM(1), |
| 108 |
PG_GETARG_DATUM(0) |
| 109 |
)); |
| 110 |
} |