1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*
* txtquery operations with ltree
* Teodor Sigaev <[email protected]>
*/
#include "ltree.h"
#include <ctype.h>
PG_FUNCTION_INFO_V1(ltxtq_exec);
PG_FUNCTION_INFO_V1(ltxtq_rexec);
/*
* check for boolean condition
*/
bool
execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void *checkval, ITEM * val)) {
if (curitem->type == VAL)
return (*chkcond) (checkval, curitem);
else if (curitem->val == (int4) '!') {
return (calcnot) ?
((execute(curitem + 1, checkval, calcnot, chkcond)) ? false : true)
: true;
} else if (curitem->val == (int4) '&') {
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
return execute(curitem + 1, checkval, calcnot, chkcond);
else
return false;
} else { /* |-operator */
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
return true;
else
return execute(curitem + 1, checkval, calcnot, chkcond);
}
return false;
}
typedef struct {
ltree *node;
char *operand;
} CHKVAL;
static bool
checkcondition_str(void* checkval, ITEM * val) {
ltree_level *level = LTREE_FIRST( ((CHKVAL*)checkval)->node );
int tlen = ((CHKVAL*)checkval)->node->numlevel;
char *op = ((CHKVAL*)checkval)->operand + val->distance;
int (*cmpptr)(const char *,const char *,size_t);
cmpptr = ( val->flag & LVAR_INCASE ) ? strncasecmp : strncmp;
while( tlen > 0 ) {
if ( val->flag & LVAR_SUBLEXEM ) {
if ( compare_subnode(level, op, val->length, cmpptr, (val->flag & LVAR_ANYEND) ) )
return true;
} else if (
(
val->length == level->len ||
( level->len > val->length && (val->flag & LVAR_ANYEND) )
) &&
(*cmpptr)( op, level->name, val->length) == 0 )
return true;
tlen--;
level = LEVEL_NEXT(level);
}
return false;
}
Datum
ltxtq_exec(PG_FUNCTION_ARGS) {
ltree *val = PG_GETARG_LTREE(0);
ltxtquery *query = PG_GETARG_LTXTQUERY(1);
CHKVAL chkval;
bool result;
chkval.node = val;
chkval.operand = GETOPERAND(query);
result = execute(
GETQUERY(query),
&chkval,
true,
checkcondition_str
);
PG_FREE_IF_COPY(val, 0);
PG_FREE_IF_COPY(query, 1);
PG_RETURN_BOOL(result);
}
Datum
ltxtq_rexec(PG_FUNCTION_ARGS) {
PG_RETURN_DATUM( DirectFunctionCall2( ltxtq_exec,
PG_GETARG_DATUM(1),
PG_GETARG_DATUM(0)
) );
}
|