2002-07-31 00:40:34 +08:00
|
|
|
/*
|
2002-09-05 04:31:48 +08:00
|
|
|
* txtquery operations with ltree
|
2002-07-31 00:40:34 +08:00
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/ltree/ltxtquery_op.c
|
2002-07-31 00:40:34 +08:00
|
|
|
*/
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "postgres.h"
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "ltree.h"
|
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_FUNCTION_INFO_V1(ltxtq_exec);
|
|
|
|
PG_FUNCTION_INFO_V1(ltxtq_rexec);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for boolean condition
|
|
|
|
*/
|
2002-09-05 04:31:48 +08:00
|
|
|
bool
|
2009-06-11 22:49:15 +08:00
|
|
|
ltree_execute(ITEM *curitem, void *checkval, bool calcnot, bool (*chkcond) (void *checkval, ITEM *val))
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
if (curitem->type == VAL)
|
|
|
|
return (*chkcond) (checkval, curitem);
|
2002-09-05 04:31:48 +08:00
|
|
|
else if (curitem->val == (int4) '!')
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
return (calcnot) ?
|
2002-08-11 04:46:24 +08:00
|
|
|
((ltree_execute(curitem + 1, checkval, calcnot, chkcond)) ? false : true)
|
2002-07-31 00:40:34 +08:00
|
|
|
: true;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (curitem->val == (int4) '&')
|
|
|
|
{
|
|
|
|
if (ltree_execute(curitem + curitem->left, checkval, calcnot, chkcond))
|
2002-08-11 04:46:24 +08:00
|
|
|
return ltree_execute(curitem + 1, checkval, calcnot, chkcond);
|
2002-07-31 00:40:34 +08:00
|
|
|
else
|
|
|
|
return false;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* |-operator */
|
2002-08-11 04:46:24 +08:00
|
|
|
if (ltree_execute(curitem + curitem->left, checkval, calcnot, chkcond))
|
2002-07-31 00:40:34 +08:00
|
|
|
return true;
|
|
|
|
else
|
2002-08-11 04:46:24 +08:00
|
|
|
return ltree_execute(curitem + 1, checkval, calcnot, chkcond);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ltree *node;
|
|
|
|
char *operand;
|
2007-11-16 06:25:18 +08:00
|
|
|
} CHKVAL;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
static bool
|
2009-06-11 22:49:15 +08:00
|
|
|
checkcondition_str(void *checkval, ITEM *val)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
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);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
cmpptr = (val->flag & LVAR_INCASE) ? ltree_strncasecmp : strncmp;
|
2002-09-05 04:31:48 +08:00
|
|
|
while (tlen > 0)
|
|
|
|
{
|
2006-03-01 14:30:32 +08:00
|
|
|
if (val->flag & LVAR_SUBLEXEME)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
if (compare_subnode(level, op, val->length, cmpptr, (val->flag & LVAR_ANYEND)))
|
2002-07-31 00:40:34 +08:00
|
|
|
return true;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (
|
|
|
|
(
|
|
|
|
val->length == level->len ||
|
|
|
|
(level->len > val->length && (val->flag & LVAR_ANYEND))
|
|
|
|
) &&
|
|
|
|
(*cmpptr) (op, level->name, val->length) == 0)
|
2002-07-31 00:40:34 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
tlen--;
|
2002-09-05 04:31:48 +08:00
|
|
|
level = LEVEL_NEXT(level);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
ltxtq_exec(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ltree *val = PG_GETARG_LTREE(0);
|
2002-07-31 00:40:34 +08:00
|
|
|
ltxtquery *query = PG_GETARG_LTXTQUERY(1);
|
2002-09-05 04:31:48 +08:00
|
|
|
CHKVAL chkval;
|
|
|
|
bool result;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
chkval.node = val;
|
|
|
|
chkval.operand = GETOPERAND(query);
|
|
|
|
|
2002-08-11 04:46:24 +08:00
|
|
|
result = ltree_execute(
|
2002-09-05 04:31:48 +08:00
|
|
|
GETQUERY(query),
|
|
|
|
&chkval,
|
|
|
|
true,
|
|
|
|
checkcondition_str
|
|
|
|
);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(val, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
ltxtq_rexec(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(ltxtq_exec,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|