]> granicus.if.org Git - postgresql/blob - contrib/dict_int/dict_int.c
163a5029a0b1d4dc235ca9b867c0ff1faf3bbb9f
[postgresql] / contrib / dict_int / dict_int.c
1 /*-------------------------------------------------------------------------
2  *
3  * dict_int.c
4  *        Text search dictionary for integers
5  *
6  * Copyright (c) 2007-2008, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *        $PostgreSQL: pgsql/contrib/dict_int/dict_int.c,v 1.3 2008/01/01 20:31:21 tgl Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14
15 #include "commands/defrem.h"
16 #include "fmgr.h"
17 #include "tsearch/ts_public.h"
18
19 PG_MODULE_MAGIC;
20
21
22 typedef struct
23 {
24         int                     maxlen;
25         bool            rejectlong;
26 }       DictInt;
27
28
29 PG_FUNCTION_INFO_V1(dintdict_init);
30 Datum           dintdict_init(PG_FUNCTION_ARGS);
31
32 PG_FUNCTION_INFO_V1(dintdict_lexize);
33 Datum           dintdict_lexize(PG_FUNCTION_ARGS);
34
35 Datum
36 dintdict_init(PG_FUNCTION_ARGS)
37 {
38         List       *dictoptions = (List *) PG_GETARG_POINTER(0);
39         DictInt    *d;
40         ListCell   *l;
41
42         d = (DictInt *) palloc0(sizeof(DictInt));
43         d->maxlen = 6;
44         d->rejectlong = false;
45
46         foreach(l, dictoptions)
47         {
48                 DefElem    *defel = (DefElem *) lfirst(l);
49
50                 if (pg_strcasecmp(defel->defname, "MAXLEN") == 0)
51                 {
52                         d->maxlen = atoi(defGetString(defel));
53                 }
54                 else if (pg_strcasecmp(defel->defname, "REJECTLONG") == 0)
55                 {
56                         d->rejectlong = defGetBoolean(defel);
57                 }
58                 else
59                 {
60                         ereport(ERROR,
61                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
62                                          errmsg("unrecognized intdict parameter: \"%s\"",
63                                                         defel->defname)));
64                 }
65         }
66
67         PG_RETURN_POINTER(d);
68 }
69
70 Datum
71 dintdict_lexize(PG_FUNCTION_ARGS)
72 {
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);
77
78         res[1].lexeme = NULL;
79         if (PG_GETARG_INT32(2) > d->maxlen)
80         {
81                 if (d->rejectlong)
82                 {
83                         /* reject by returning void array */
84                         pfree(txt);
85                         res[0].lexeme = NULL;
86                 }
87                 else
88                 {
89                         /* trim integer */
90                         txt[d->maxlen] = '\0';
91                         res[0].lexeme = txt;
92                 }
93         }
94         else
95         {
96                 res[0].lexeme = txt;
97         }
98
99         PG_RETURN_POINTER(res);
100 }