2002-07-31 00:40:34 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/ltree/_ltree_op.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*
|
|
|
|
*
|
2002-09-05 04:31:48 +08:00
|
|
|
* op function for ltree[]
|
2002-07-31 00:40:34 +08:00
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
|
|
|
*/
|
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(_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);
|
2003-02-19 11:50:09 +08:00
|
|
|
PG_FUNCTION_INFO_V1(_lt_q_regex);
|
|
|
|
PG_FUNCTION_INFO_V1(_lt_q_rregex);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_FUNCTION_INFO_V1(_ltxtq_exec);
|
|
|
|
PG_FUNCTION_INFO_V1(_ltxtq_rexec);
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum _ltree_r_isparent(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltree_r_risparent(PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
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);
|
2002-09-05 04:31:48 +08:00
|
|
|
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);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-08-04 13:02:50 +08:00
|
|
|
PG_FUNCTION_INFO_V1(_lca);
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum _lca(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
typedef Datum (*PGCALL2) (PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define NEXTVAL(x) ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
|
|
|
|
|
|
|
|
static bool
|
2009-06-11 22:49:15 +08:00
|
|
|
array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree **found)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
int num = ArrayGetNItems(ARR_NDIM(la), ARR_DIMS(la));
|
|
|
|
ltree *item = (ltree *) ARR_DATA_PTR(la);
|
|
|
|
|
2010-02-25 02:02:24 +08:00
|
|
|
if (ARR_NDIM(la) > 1)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
2003-08-04 08:43:34 +08:00
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("array must be one-dimensional")));
|
2011-01-10 02:09:07 +08:00
|
|
|
if (array_contains_nulls(la))
|
2005-11-19 10:08:45 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("array must not contain nulls")));
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
if (found)
|
|
|
|
*found = NULL;
|
|
|
|
while (num > 0)
|
|
|
|
{
|
|
|
|
if (DatumGetBool(DirectFunctionCall2(callback,
|
2005-10-15 10:49:52 +08:00
|
|
|
PointerGetDatum(item), PointerGetDatum(param))))
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (found)
|
2002-07-31 00:40:34 +08:00
|
|
|
*found = item;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
num--;
|
2002-09-05 04:31:48 +08:00
|
|
|
item = NEXTVAL(item);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_ltree_isparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ltree *query = PG_GETARG_LTREE(1);
|
|
|
|
bool res = array_iterator(la, ltree_isparent, (void *) query, NULL);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_ltree_r_isparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(_ltree_isparent,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_ltree_risparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ltree *query = PG_GETARG_LTREE(1);
|
|
|
|
bool res = array_iterator(la, ltree_risparent, (void *) query, NULL);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_ltree_r_risparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(_ltree_risparent,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_ltq_regex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
lquery *query = PG_GETARG_LQUERY(1);
|
|
|
|
bool res = array_iterator(la, ltq_regex, (void *) query, NULL);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_ltq_rregex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(_ltq_regex,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
2003-02-19 11:50:09 +08:00
|
|
|
Datum
|
|
|
|
_lt_q_regex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
ArrayType *_tree = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ArrayType *_query = PG_GETARG_ARRAYTYPE_P(1);
|
|
|
|
lquery *query = (lquery *) ARR_DATA_PTR(_query);
|
|
|
|
bool res = false;
|
|
|
|
int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
|
|
|
|
|
2010-02-25 02:02:24 +08:00
|
|
|
if (ARR_NDIM(_query) > 1)
|
2003-08-04 08:43:34 +08:00
|
|
|
ereport(ERROR,
|
2003-07-25 01:52:50 +08:00
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("array must be one-dimensional")));
|
2011-01-10 02:09:07 +08:00
|
|
|
if (array_contains_nulls(_query))
|
2005-11-19 10:08:45 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("array must not contain nulls")));
|
2003-02-19 11:50:09 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
while (num > 0)
|
|
|
|
{
|
|
|
|
if (array_iterator(_tree, ltq_regex, (void *) query, NULL))
|
|
|
|
{
|
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
num--;
|
|
|
|
query = (lquery *) NEXTVAL(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(_tree, 0);
|
|
|
|
PG_FREE_IF_COPY(_query, 1);
|
|
|
|
PG_RETURN_BOOL(res);
|
2003-02-19 11:50:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
_lt_q_rregex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(_lt_q_regex,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2003-02-19 11:50:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
_ltxtq_exec(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ltxtquery *query = PG_GETARG_LTXTQUERY(1);
|
|
|
|
bool res = array_iterator(la, ltxtq_exec, (void *) query, NULL);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
_ltree_extract_isparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ltree *query = PG_GETARG_LTREE(1);
|
|
|
|
ltree *found,
|
|
|
|
*item;
|
|
|
|
|
|
|
|
if (!array_iterator(la, ltree_isparent, (void *) query, &found))
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
item = (ltree *) palloc(VARSIZE(found));
|
|
|
|
memcpy(item, found, VARSIZE(found));
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_POINTER(item);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
_ltree_extract_risparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ltree *query = PG_GETARG_LTREE(1);
|
|
|
|
ltree *found,
|
|
|
|
*item;
|
|
|
|
|
|
|
|
if (!array_iterator(la, ltree_risparent, (void *) query, &found))
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
item = (ltree *) palloc(VARSIZE(found));
|
|
|
|
memcpy(item, found, VARSIZE(found));
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_POINTER(item);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
_ltq_extract_regex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
lquery *query = PG_GETARG_LQUERY(1);
|
|
|
|
ltree *found,
|
|
|
|
*item;
|
|
|
|
|
|
|
|
if (!array_iterator(la, ltq_regex, (void *) query, &found))
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
item = (ltree *) palloc(VARSIZE(found));
|
|
|
|
memcpy(item, found, VARSIZE(found));
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_POINTER(item);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
_ltxtq_extract_exec(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ltxtquery *query = PG_GETARG_LTXTQUERY(1);
|
|
|
|
ltree *found,
|
|
|
|
*item;
|
|
|
|
|
|
|
|
if (!array_iterator(la, ltxtq_exec, (void *) query, &found))
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
item = (ltree *) palloc(VARSIZE(found));
|
|
|
|
memcpy(item, found, VARSIZE(found));
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(la, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_RETURN_POINTER(item);
|
|
|
|
}
|
|
|
|
|
2002-08-04 13:02:50 +08:00
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
_lca(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *la = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
int num = ArrayGetNItems(ARR_NDIM(la), ARR_DIMS(la));
|
|
|
|
ltree *item = (ltree *) ARR_DATA_PTR(la);
|
|
|
|
ltree **a,
|
|
|
|
*res;
|
|
|
|
|
2010-02-25 02:02:24 +08:00
|
|
|
if (ARR_NDIM(la) > 1)
|
2005-11-19 10:08:45 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("array must be one-dimensional")));
|
2011-01-10 02:09:07 +08:00
|
|
|
if (array_contains_nulls(la))
|
2005-11-19 10:08:45 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("array must not contain nulls")));
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
a = (ltree **) palloc(sizeof(ltree *) * num);
|
|
|
|
while (num > 0)
|
|
|
|
{
|
2002-08-04 13:02:50 +08:00
|
|
|
num--;
|
|
|
|
a[num] = item;
|
|
|
|
item = NEXTVAL(item);
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
res = lca_inner(a, ArrayGetNItems(ARR_NDIM(la), ARR_DIMS(la)));
|
2002-08-04 13:02:50 +08:00
|
|
|
pfree(a);
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
PG_FREE_IF_COPY(la, 0);
|
2002-08-04 13:02:50 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (res)
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
else
|
|
|
|
PG_RETURN_NULL();
|
2002-08-04 13:02:50 +08:00
|
|
|
}
|