2002-07-31 00:40:34 +08:00
|
|
|
/*
|
|
|
|
* in/out function for ltree and lquery
|
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
2007-03-01 06:44:38 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/ltree/ltree_io.c,v 1.14 2007/02/28 22:44:38 tgl Exp $
|
2002-07-31 00:40:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ltree.h"
|
|
|
|
#include <ctype.h>
|
2002-09-05 04:31:48 +08:00
|
|
|
#include "crc32.h"
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_in);
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum ltree_in(PG_FUNCTION_ARGS);
|
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_FUNCTION_INFO_V1(ltree_out);
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum ltree_out(PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(lquery_in);
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum lquery_in(PG_FUNCTION_ARGS);
|
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
PG_FUNCTION_INFO_V1(lquery_out);
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum lquery_out(PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
|
2003-07-25 01:52:50 +08:00
|
|
|
#define UNCHAR ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR), \
|
2003-08-04 08:43:34 +08:00
|
|
|
errmsg("syntax error at position %d near \"%c\"", \
|
2003-07-25 01:52:50 +08:00
|
|
|
(int)(ptr-buf), *ptr)));
|
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *start;
|
|
|
|
int len;
|
|
|
|
int flag;
|
|
|
|
} nodeitem;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define LTPRS_WAITNAME 0
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LTPRS_WAITDELIM 1
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ltree_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *buf = (char *) PG_GETARG_POINTER(0);
|
|
|
|
char *ptr;
|
|
|
|
nodeitem *list,
|
|
|
|
*lptr;
|
|
|
|
int num = 0,
|
|
|
|
totallen = 0;
|
|
|
|
int state = LTPRS_WAITNAME;
|
|
|
|
ltree *result;
|
|
|
|
ltree_level *curlevel;
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (*ptr == '.')
|
2002-07-31 00:40:34 +08:00
|
|
|
num++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
list = lptr = (nodeitem *) palloc(sizeof(nodeitem) * (num + 1));
|
|
|
|
ptr = buf;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (state == LTPRS_WAITNAME)
|
|
|
|
{
|
|
|
|
if (ISALNUM(*ptr))
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr->start = ptr;
|
|
|
|
state = LTPRS_WAITDELIM;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LTPRS_WAITDELIM)
|
|
|
|
{
|
|
|
|
if (*ptr == '.')
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr->len = ptr - lptr->start;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (lptr->len > 255)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NAME_TOO_LONG),
|
|
|
|
errmsg("name of level is too long"),
|
2006-03-01 14:30:32 +08:00
|
|
|
errdetail("Name length is %d, must "
|
|
|
|
"be < 256, in position %d.",
|
2005-10-15 10:49:52 +08:00
|
|
|
lptr->len, (int) (lptr->start - buf))));
|
2003-07-25 01:52:50 +08:00
|
|
|
|
2002-08-04 13:02:50 +08:00
|
|
|
totallen += MAXALIGN(lptr->len + LEVEL_HDRSIZE);
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr++;
|
|
|
|
state = LTPRS_WAITNAME;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (!ISALNUM(*ptr))
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "internal error in parser");
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (state == LTPRS_WAITDELIM)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr->len = ptr - lptr->start;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (lptr->len > 255)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NAME_TOO_LONG),
|
|
|
|
errmsg("name of level is too long"),
|
2006-03-01 14:30:32 +08:00
|
|
|
errdetail("Name length is %d, must "
|
|
|
|
"be < 256, in position %d.",
|
2003-08-04 08:43:34 +08:00
|
|
|
lptr->len, (int) (lptr->start - buf))));
|
2003-07-25 01:52:50 +08:00
|
|
|
|
2002-08-04 13:02:50 +08:00
|
|
|
totallen += MAXALIGN(lptr->len + LEVEL_HDRSIZE);
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr++;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (!(state == LTPRS_WAITNAME && lptr == list))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error"),
|
|
|
|
errdetail("Unexpected end of line.")));
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
result = (ltree *) palloc(LTREE_HDRSIZE + totallen);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(result, LTREE_HDRSIZE + totallen);
|
2002-09-05 04:31:48 +08:00
|
|
|
result->numlevel = lptr - list;
|
2002-07-31 00:40:34 +08:00
|
|
|
curlevel = LTREE_FIRST(result);
|
2002-09-05 04:31:48 +08:00
|
|
|
lptr = list;
|
|
|
|
while (lptr - list < result->numlevel)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
curlevel->len = (uint8) lptr->len;
|
2002-09-05 04:31:48 +08:00
|
|
|
memcpy(curlevel->name, lptr->start, lptr->len);
|
|
|
|
curlevel = LEVEL_NEXT(curlevel);
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(list);
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
ltree_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ltree *in = PG_GETARG_LTREE(0);
|
|
|
|
char *buf,
|
|
|
|
*ptr;
|
|
|
|
int i;
|
|
|
|
ltree_level *curlevel;
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
ptr = buf = (char *) palloc(VARSIZE(in));
|
2002-07-31 00:40:34 +08:00
|
|
|
curlevel = LTREE_FIRST(in);
|
2002-09-05 04:31:48 +08:00
|
|
|
for (i = 0; i < in->numlevel; i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '.';
|
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
memcpy(ptr, curlevel->name, curlevel->len);
|
|
|
|
ptr += curlevel->len;
|
2002-07-31 00:40:34 +08:00
|
|
|
curlevel = LEVEL_NEXT(curlevel);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
*ptr = '\0';
|
|
|
|
PG_FREE_IF_COPY(in, 0);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(buf);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LQPRS_WAITLEVEL 0
|
|
|
|
#define LQPRS_WAITDELIM 1
|
|
|
|
#define LQPRS_WAITOPEN 2
|
|
|
|
#define LQPRS_WAITFNUM 3
|
|
|
|
#define LQPRS_WAITSNUM 4
|
|
|
|
#define LQPRS_WAITND 5
|
|
|
|
#define LQPRS_WAITCLOSE 6
|
2002-07-31 00:40:34 +08:00
|
|
|
#define LQPRS_WAITEND 7
|
|
|
|
#define LQPRS_WAITVAR 8
|
|
|
|
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
#define GETVAR(x) ( *((nodeitem**)LQL_FIRST(x)) )
|
|
|
|
#define ITEMSIZE MAXALIGN(LQL_HDRSIZE+sizeof(nodeitem*))
|
|
|
|
#define NEXTLEV(x) ( (lquery_level*)( ((char*)(x)) + ITEMSIZE) )
|
|
|
|
|
|
|
|
Datum
|
|
|
|
lquery_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *buf = (char *) PG_GETARG_POINTER(0);
|
|
|
|
char *ptr;
|
|
|
|
int num = 0,
|
|
|
|
totallen = 0,
|
|
|
|
numOR = 0;
|
|
|
|
int state = LQPRS_WAITLEVEL;
|
|
|
|
lquery *result;
|
|
|
|
nodeitem *lptr = NULL;
|
|
|
|
lquery_level *cur,
|
|
|
|
*curqlevel,
|
|
|
|
*tmpql;
|
|
|
|
lquery_variant *lrptr = NULL;
|
|
|
|
bool hasnot = false;
|
|
|
|
bool wasbad = false;
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (*ptr == '.')
|
2002-07-31 00:40:34 +08:00
|
|
|
num++;
|
2002-09-05 04:31:48 +08:00
|
|
|
else if (*ptr == '|')
|
2002-07-31 00:40:34 +08:00
|
|
|
numOR++;
|
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
num++;
|
2002-09-05 04:31:48 +08:00
|
|
|
curqlevel = tmpql = (lquery_level *) palloc(ITEMSIZE * num);
|
|
|
|
memset((void *) tmpql, 0, ITEMSIZE * num);
|
|
|
|
ptr = buf;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (state == LQPRS_WAITLEVEL)
|
|
|
|
{
|
|
|
|
if (ISALNUM(*ptr))
|
|
|
|
{
|
|
|
|
GETVAR(curqlevel) = lptr = (nodeitem *) palloc(sizeof(nodeitem) * (numOR + 1));
|
|
|
|
memset((void *) GETVAR(curqlevel), 0, sizeof(nodeitem) * (numOR + 1));
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr->start = ptr;
|
|
|
|
state = LQPRS_WAITDELIM;
|
|
|
|
curqlevel->numvar = 1;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (*ptr == '!')
|
|
|
|
{
|
|
|
|
GETVAR(curqlevel) = lptr = (nodeitem *) palloc(sizeof(nodeitem) * (numOR + 1));
|
|
|
|
memset((void *) GETVAR(curqlevel), 0, sizeof(nodeitem) * (numOR + 1));
|
|
|
|
lptr->start = ptr + 1;
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITDELIM;
|
|
|
|
curqlevel->numvar = 1;
|
|
|
|
curqlevel->flag |= LQL_NOT;
|
2002-09-05 04:31:48 +08:00
|
|
|
hasnot = true;
|
|
|
|
}
|
|
|
|
else if (*ptr == '*')
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITOPEN;
|
2002-09-05 04:31:48 +08:00
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITVAR)
|
|
|
|
{
|
|
|
|
if (ISALNUM(*ptr))
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr++;
|
|
|
|
lptr->start = ptr;
|
|
|
|
state = LQPRS_WAITDELIM;
|
|
|
|
curqlevel->numvar++;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITDELIM)
|
|
|
|
{
|
|
|
|
if (*ptr == '@')
|
|
|
|
{
|
|
|
|
if (lptr->start == ptr)
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
|
|
|
lptr->flag |= LVAR_INCASE;
|
|
|
|
curqlevel->flag |= LVAR_INCASE;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (*ptr == '*')
|
|
|
|
{
|
|
|
|
if (lptr->start == ptr)
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
|
|
|
lptr->flag |= LVAR_ANYEND;
|
|
|
|
curqlevel->flag |= LVAR_ANYEND;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (*ptr == '%')
|
|
|
|
{
|
|
|
|
if (lptr->start == ptr)
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2006-03-01 14:30:32 +08:00
|
|
|
lptr->flag |= LVAR_SUBLEXEME;
|
|
|
|
curqlevel->flag |= LVAR_SUBLEXEME;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (*ptr == '|')
|
|
|
|
{
|
|
|
|
lptr->len = ptr - lptr->start -
|
2006-03-01 14:30:32 +08:00
|
|
|
((lptr->flag & LVAR_SUBLEXEME) ? 1 : 0) -
|
2002-09-05 04:31:48 +08:00
|
|
|
((lptr->flag & LVAR_INCASE) ? 1 : 0) -
|
|
|
|
((lptr->flag & LVAR_ANYEND) ? 1 : 0);
|
|
|
|
if (lptr->len > 255)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NAME_TOO_LONG),
|
|
|
|
errmsg("name of level is too long"),
|
2006-03-01 14:30:32 +08:00
|
|
|
errdetail("Name length is %d, must "
|
|
|
|
"be < 256, in position %d.",
|
2005-10-15 10:49:52 +08:00
|
|
|
lptr->len, (int) (lptr->start - buf))));
|
2003-07-25 01:52:50 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITVAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (*ptr == '.')
|
|
|
|
{
|
|
|
|
lptr->len = ptr - lptr->start -
|
2006-03-01 14:30:32 +08:00
|
|
|
((lptr->flag & LVAR_SUBLEXEME) ? 1 : 0) -
|
2002-09-05 04:31:48 +08:00
|
|
|
((lptr->flag & LVAR_INCASE) ? 1 : 0) -
|
|
|
|
((lptr->flag & LVAR_ANYEND) ? 1 : 0);
|
|
|
|
if (lptr->len > 255)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NAME_TOO_LONG),
|
|
|
|
errmsg("name of level is too long"),
|
2006-03-01 14:30:32 +08:00
|
|
|
errdetail("Name length is %d, must "
|
|
|
|
"be < 256, in position %d.",
|
2005-10-15 10:49:52 +08:00
|
|
|
lptr->len, (int) (lptr->start - buf))));
|
2003-07-25 01:52:50 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITLEVEL;
|
2002-08-04 13:02:50 +08:00
|
|
|
curqlevel = NEXTLEV(curqlevel);
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (ISALNUM(*ptr))
|
|
|
|
{
|
|
|
|
if (lptr->flag)
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITOPEN)
|
|
|
|
{
|
|
|
|
if (*ptr == '{')
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITFNUM;
|
2002-09-05 04:31:48 +08:00
|
|
|
else if (*ptr == '.')
|
|
|
|
{
|
|
|
|
curqlevel->low = 0;
|
|
|
|
curqlevel->high = 0xffff;
|
2002-08-04 13:02:50 +08:00
|
|
|
curqlevel = NEXTLEV(curqlevel);
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITLEVEL;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITFNUM)
|
|
|
|
{
|
|
|
|
if (*ptr == ',')
|
|
|
|
state = LQPRS_WAITSNUM;
|
2006-09-23 05:39:58 +08:00
|
|
|
else if (isdigit((unsigned char) *ptr))
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
curqlevel->low = atoi(ptr);
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITND;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
UNCHAR;
|
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITSNUM)
|
|
|
|
{
|
2006-09-23 05:39:58 +08:00
|
|
|
if (isdigit((unsigned char) *ptr))
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
curqlevel->high = atoi(ptr);
|
|
|
|
state = LQPRS_WAITCLOSE;
|
|
|
|
}
|
|
|
|
else if (*ptr == '}')
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
curqlevel->high = 0xffff;
|
|
|
|
state = LQPRS_WAITEND;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITCLOSE)
|
|
|
|
{
|
|
|
|
if (*ptr == '}')
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITEND;
|
2006-09-23 05:39:58 +08:00
|
|
|
else if (!isdigit((unsigned char) *ptr))
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITND)
|
|
|
|
{
|
|
|
|
if (*ptr == '}')
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
curqlevel->high = curqlevel->low;
|
|
|
|
state = LQPRS_WAITEND;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (*ptr == ',')
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITSNUM;
|
2006-09-23 05:39:58 +08:00
|
|
|
else if (!isdigit((unsigned char) *ptr))
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITEND)
|
|
|
|
{
|
|
|
|
if (*ptr == '.')
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
state = LQPRS_WAITLEVEL;
|
2002-08-04 13:02:50 +08:00
|
|
|
curqlevel = NEXTLEV(curqlevel);
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2002-07-31 00:40:34 +08:00
|
|
|
UNCHAR;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "internal error in parser");
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
if (state == LQPRS_WAITDELIM)
|
|
|
|
{
|
|
|
|
if (lptr->start == ptr)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error"),
|
|
|
|
errdetail("Unexpected end of line.")));
|
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr->len = ptr - lptr->start -
|
2006-03-01 14:30:32 +08:00
|
|
|
((lptr->flag & LVAR_SUBLEXEME) ? 1 : 0) -
|
2002-09-05 04:31:48 +08:00
|
|
|
((lptr->flag & LVAR_INCASE) ? 1 : 0) -
|
|
|
|
((lptr->flag & LVAR_ANYEND) ? 1 : 0);
|
|
|
|
if (lptr->len == 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error"),
|
|
|
|
errdetail("Unexpected end of line.")));
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (lptr->len > 255)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NAME_TOO_LONG),
|
|
|
|
errmsg("name of level is too long"),
|
2006-03-01 14:30:32 +08:00
|
|
|
errdetail("Name length is %d, must "
|
|
|
|
"be < 256, in position %d.",
|
2003-08-04 08:43:34 +08:00
|
|
|
lptr->len, (int) (lptr->start - buf))));
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (state == LQPRS_WAITOPEN)
|
2002-07-31 00:40:34 +08:00
|
|
|
curqlevel->high = 0xffff;
|
2002-09-05 04:31:48 +08:00
|
|
|
else if (state != LQPRS_WAITEND)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error"),
|
|
|
|
errdetail("Unexpected end of line.")));
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
curqlevel = tmpql;
|
2002-09-05 04:31:48 +08:00
|
|
|
totallen = LQUERY_HDRSIZE;
|
|
|
|
while ((char *) curqlevel - (char *) tmpql < num * ITEMSIZE)
|
|
|
|
{
|
|
|
|
totallen += LQL_HDRSIZE;
|
|
|
|
if (curqlevel->numvar)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr = GETVAR(curqlevel);
|
2002-09-05 04:31:48 +08:00
|
|
|
while (lptr - GETVAR(curqlevel) < curqlevel->numvar)
|
|
|
|
{
|
2002-08-04 13:02:50 +08:00
|
|
|
totallen += MAXALIGN(LVAR_HDRSIZE + lptr->len);
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (curqlevel->low > curqlevel->high)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error"),
|
|
|
|
errdetail("Low limit(%d) is greater than upper(%d).",
|
2003-08-04 08:43:34 +08:00
|
|
|
curqlevel->low, curqlevel->high)));
|
2003-07-25 01:52:50 +08:00
|
|
|
|
2002-08-04 13:02:50 +08:00
|
|
|
curqlevel = NEXTLEV(curqlevel);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
2002-08-04 13:02:50 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
result = (lquery *) palloc(totallen);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(result, totallen);
|
2002-07-31 00:40:34 +08:00
|
|
|
result->numlevel = num;
|
|
|
|
result->firstgood = 0;
|
2002-09-05 04:31:48 +08:00
|
|
|
result->flag = 0;
|
|
|
|
if (hasnot)
|
2002-07-31 00:40:34 +08:00
|
|
|
result->flag |= LQUERY_HASNOT;
|
|
|
|
cur = LQUERY_FIRST(result);
|
|
|
|
curqlevel = tmpql;
|
2002-09-05 04:31:48 +08:00
|
|
|
while ((char *) curqlevel - (char *) tmpql < num * ITEMSIZE)
|
|
|
|
{
|
|
|
|
memcpy(cur, curqlevel, LQL_HDRSIZE);
|
|
|
|
cur->totallen = LQL_HDRSIZE;
|
|
|
|
if (curqlevel->numvar)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
lrptr = LQL_FIRST(cur);
|
|
|
|
lptr = GETVAR(curqlevel);
|
2002-09-05 04:31:48 +08:00
|
|
|
while (lptr - GETVAR(curqlevel) < curqlevel->numvar)
|
|
|
|
{
|
2002-08-04 13:02:50 +08:00
|
|
|
cur->totallen += MAXALIGN(LVAR_HDRSIZE + lptr->len);
|
2002-09-05 04:31:48 +08:00
|
|
|
lrptr->len = lptr->len;
|
2002-07-31 00:40:34 +08:00
|
|
|
lrptr->flag = lptr->flag;
|
2005-09-25 03:14:05 +08:00
|
|
|
lrptr->val = ltree_crc32_sz(lptr->start, lptr->len);
|
2002-09-05 04:31:48 +08:00
|
|
|
memcpy(lrptr->name, lptr->start, lptr->len);
|
2002-07-31 00:40:34 +08:00
|
|
|
lptr++;
|
2002-09-05 04:31:48 +08:00
|
|
|
lrptr = LVAR_NEXT(lrptr);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
pfree(GETVAR(curqlevel));
|
|
|
|
if (cur->numvar > 1 || cur->flag != 0)
|
|
|
|
wasbad = true;
|
|
|
|
else if (wasbad == false)
|
|
|
|
(result->firstgood)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wasbad = true;
|
2002-08-04 13:02:50 +08:00
|
|
|
curqlevel = NEXTLEV(curqlevel);
|
2002-07-31 00:40:34 +08:00
|
|
|
cur = LQL_NEXT(cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(tmpql);
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
lquery_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
lquery *in = PG_GETARG_LQUERY(0);
|
|
|
|
char *buf,
|
|
|
|
*ptr;
|
|
|
|
int i,
|
|
|
|
j,
|
2003-12-18 04:15:41 +08:00
|
|
|
totallen = 1;
|
2002-09-05 04:31:48 +08:00
|
|
|
lquery_level *curqlevel;
|
|
|
|
lquery_variant *curtlevel;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
curqlevel = LQUERY_FIRST(in);
|
2002-09-05 04:31:48 +08:00
|
|
|
for (i = 0; i < in->numlevel; i++)
|
|
|
|
{
|
2003-12-18 04:15:41 +08:00
|
|
|
totallen++;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (curqlevel->numvar)
|
2003-12-18 04:15:41 +08:00
|
|
|
totallen += 1 + (curqlevel->numvar * 4) + curqlevel->totallen;
|
2002-07-31 00:40:34 +08:00
|
|
|
else
|
2003-12-18 04:15:41 +08:00
|
|
|
totallen += 2 * 11 + 4;
|
2002-07-31 00:40:34 +08:00
|
|
|
curqlevel = LQL_NEXT(curqlevel);
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
ptr = buf = (char *) palloc(totallen);
|
2002-07-31 00:40:34 +08:00
|
|
|
curqlevel = LQUERY_FIRST(in);
|
2002-09-05 04:31:48 +08:00
|
|
|
for (i = 0; i < in->numlevel; i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '.';
|
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
if (curqlevel->numvar)
|
|
|
|
{
|
|
|
|
if (curqlevel->flag & LQL_NOT)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '!';
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
curtlevel = LQL_FIRST(curqlevel);
|
2002-09-05 04:31:48 +08:00
|
|
|
for (j = 0; j < curqlevel->numvar; j++)
|
|
|
|
{
|
|
|
|
if (j != 0)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '|';
|
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
memcpy(ptr, curtlevel->name, curtlevel->len);
|
|
|
|
ptr += curtlevel->len;
|
2006-03-01 14:30:32 +08:00
|
|
|
if ((curtlevel->flag & LVAR_SUBLEXEME))
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '%';
|
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
if ((curtlevel->flag & LVAR_INCASE))
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '@';
|
|
|
|
ptr++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
if ((curtlevel->flag & LVAR_ANYEND))
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
*ptr = '*';
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
curtlevel = LVAR_NEXT(curtlevel);
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (curqlevel->low == curqlevel->high)
|
|
|
|
{
|
|
|
|
sprintf(ptr, "*{%d}", curqlevel->low);
|
|
|
|
}
|
|
|
|
else if (curqlevel->low == 0)
|
|
|
|
{
|
|
|
|
if (curqlevel->high == 0xffff)
|
|
|
|
{
|
|
|
|
*ptr = '*';
|
|
|
|
*(ptr + 1) = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sprintf(ptr, "*{,%d}", curqlevel->high);
|
|
|
|
}
|
|
|
|
else if (curqlevel->high == 0xffff)
|
|
|
|
{
|
|
|
|
sprintf(ptr, "*{%d,}", curqlevel->low);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sprintf(ptr, "*{%d,%d}", curqlevel->low, curqlevel->high);
|
|
|
|
ptr = strchr(ptr, '\0');
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
curqlevel = LQL_NEXT(curqlevel);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
*ptr = '\0';
|
|
|
|
PG_FREE_IF_COPY(in, 0);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(buf);
|
|
|
|
}
|