Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / contrib / dict_int / dict_int.c
blob93abbe71d4c48f4ce51c0779fa51a2b9676ba897
1 /*-------------------------------------------------------------------------
3 * dict_int.c
4 * Text search dictionary for integers
6 * Copyright (c) 2007-2008, PostgreSQL Global Development Group
8 * IDENTIFICATION
9 * $PostgreSQL$
11 *-------------------------------------------------------------------------
13 #include "postgres.h"
15 #include "commands/defrem.h"
16 #include "fmgr.h"
17 #include "tsearch/ts_public.h"
19 PG_MODULE_MAGIC;
22 typedef struct
24 int maxlen;
25 bool rejectlong;
26 } DictInt;
29 PG_FUNCTION_INFO_V1(dintdict_init);
30 Datum dintdict_init(PG_FUNCTION_ARGS);
32 PG_FUNCTION_INFO_V1(dintdict_lexize);
33 Datum dintdict_lexize(PG_FUNCTION_ARGS);
35 Datum
36 dintdict_init(PG_FUNCTION_ARGS)
38 List *dictoptions = (List *) PG_GETARG_POINTER(0);
39 DictInt *d;
40 ListCell *l;
42 d = (DictInt *) palloc0(sizeof(DictInt));
43 d->maxlen = 6;
44 d->rejectlong = false;
46 foreach(l, dictoptions)
48 DefElem *defel = (DefElem *) lfirst(l);
50 if (pg_strcasecmp(defel->defname, "MAXLEN") == 0)
52 d->maxlen = atoi(defGetString(defel));
54 else if (pg_strcasecmp(defel->defname, "REJECTLONG") == 0)
56 d->rejectlong = defGetBoolean(defel);
58 else
60 ereport(ERROR,
61 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
62 errmsg("unrecognized intdict parameter: \"%s\"",
63 defel->defname)));
67 PG_RETURN_POINTER(d);
70 Datum
71 dintdict_lexize(PG_FUNCTION_ARGS)
73 DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
74 char *in = (char *) PG_GETARG_POINTER(1);
75 char *txt = pnstrdup(in, PG_GETARG_INT32(2));
76 TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
78 res[1].lexeme = NULL;
79 if (PG_GETARG_INT32(2) > d->maxlen)
81 if (d->rejectlong)
83 /* reject by returning void array */
84 pfree(txt);
85 res[0].lexeme = NULL;
87 else
89 /* trim integer */
90 txt[d->maxlen] = '\0';
91 res[0].lexeme = txt;
94 else
96 res[0].lexeme = txt;
99 PG_RETURN_POINTER(res);