2007-10-16 05:36:50 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* dict_int.c
|
|
|
|
* Text search dictionary for integers
|
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2007-2011, PostgreSQL Global Development Group
|
2007-10-16 05:36:50 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/dict_int/dict_int.c
|
2007-10-16 05:36:50 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "tsearch/ts_public.h"
|
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int maxlen;
|
|
|
|
bool rejectlong;
|
2009-06-11 22:49:15 +08:00
|
|
|
} DictInt;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(dintdict_init);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum dintdict_init(PG_FUNCTION_ARGS);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(dintdict_lexize);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum dintdict_lexize(PG_FUNCTION_ARGS);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
dintdict_init(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
List *dictoptions = (List *) PG_GETARG_POINTER(0);
|
|
|
|
DictInt *d;
|
|
|
|
ListCell *l;
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
d = (DictInt *) palloc0(sizeof(DictInt));
|
|
|
|
d->maxlen = 6;
|
|
|
|
d->rejectlong = false;
|
|
|
|
|
|
|
|
foreach(l, dictoptions)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
DefElem *defel = (DefElem *) lfirst(l);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
if (pg_strcasecmp(defel->defname, "MAXLEN") == 0)
|
|
|
|
{
|
|
|
|
d->maxlen = atoi(defGetString(defel));
|
|
|
|
}
|
|
|
|
else if (pg_strcasecmp(defel->defname, "REJECTLONG") == 0)
|
|
|
|
{
|
|
|
|
d->rejectlong = defGetBoolean(defel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unrecognized intdict parameter: \"%s\"",
|
|
|
|
defel->defname)));
|
|
|
|
}
|
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-10-16 05:36:50 +08:00
|
|
|
PG_RETURN_POINTER(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
dintdict_lexize(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
|
|
|
|
char *in = (char *) PG_GETARG_POINTER(1);
|
|
|
|
char *txt = pnstrdup(in, PG_GETARG_INT32(2));
|
|
|
|
TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
|
2007-10-16 05:36:50 +08:00
|
|
|
|
|
|
|
res[1].lexeme = NULL;
|
2007-11-16 05:14:46 +08:00
|
|
|
if (PG_GETARG_INT32(2) > d->maxlen)
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
if (d->rejectlong)
|
2007-10-16 05:36:50 +08:00
|
|
|
{
|
|
|
|
/* reject by returning void array */
|
|
|
|
pfree(txt);
|
|
|
|
res[0].lexeme = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* trim integer */
|
|
|
|
txt[d->maxlen] = '\0';
|
|
|
|
res[0].lexeme = txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res[0].lexeme = txt;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|