Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * dict_int.c
4 : : * Text search dictionary for integers
5 : : *
6 : : * Copyright (c) 2007-2025, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * contrib/dict_int/dict_int.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres.h"
14 : :
15 : : #include "commands/defrem.h"
16 : : #include "tsearch/ts_public.h"
17 : :
164 tgl@sss.pgh.pa.us 18 :CBC 1 : PG_MODULE_MAGIC_EXT(
19 : : .name = "dict_int",
20 : : .version = PG_VERSION
21 : : );
22 : :
23 : : typedef struct
24 : : {
25 : : int maxlen;
26 : : bool rejectlong;
27 : : bool absval;
28 : : } DictInt;
29 : :
30 : :
6536 31 : 2 : PG_FUNCTION_INFO_V1(dintdict_init);
32 : 2 : PG_FUNCTION_INFO_V1(dintdict_lexize);
33 : :
34 : : Datum
35 : 9 : dintdict_init(PG_FUNCTION_ARGS)
36 : : {
6505 bruce@momjian.us 37 : 9 : List *dictoptions = (List *) PG_GETARG_POINTER(0);
38 : : DictInt *d;
39 : : ListCell *l;
40 : :
6536 tgl@sss.pgh.pa.us 41 : 9 : d = (DictInt *) palloc0(sizeof(DictInt));
42 : 9 : d->maxlen = 6;
43 : 9 : d->rejectlong = false;
2008 44 : 9 : d->absval = false;
45 : :
6536 46 [ + + + + : 21 : foreach(l, dictoptions)
+ + ]
47 : : {
6505 bruce@momjian.us 48 : 13 : DefElem *defel = (DefElem *) lfirst(l);
49 : :
2780 tgl@sss.pgh.pa.us 50 [ + + ]: 13 : if (strcmp(defel->defname, "maxlen") == 0)
51 : : {
6536 52 : 7 : d->maxlen = atoi(defGetString(defel));
53 : :
2104 tomas.vondra@postgre 54 [ + + ]: 7 : if (d->maxlen < 1)
55 [ + - ]: 1 : ereport(ERROR,
56 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57 : : errmsg("maxlen value has to be >= 1")));
58 : : }
2780 tgl@sss.pgh.pa.us 59 [ + + ]: 6 : else if (strcmp(defel->defname, "rejectlong") == 0)
60 : : {
6536 61 : 2 : d->rejectlong = defGetBoolean(defel);
62 : : }
2008 63 [ + - ]: 4 : else if (strcmp(defel->defname, "absval") == 0)
64 : : {
65 : 4 : d->absval = defGetBoolean(defel);
66 : : }
67 : : else
68 : : {
6536 tgl@sss.pgh.pa.us 69 [ # # ]:UBC 0 : ereport(ERROR,
70 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
71 : : errmsg("unrecognized intdict parameter: \"%s\"",
72 : : defel->defname)));
73 : : }
74 : : }
75 : :
6536 tgl@sss.pgh.pa.us 76 :CBC 8 : PG_RETURN_POINTER(d);
77 : : }
78 : :
79 : : Datum
80 : 57 : dintdict_lexize(PG_FUNCTION_ARGS)
81 : : {
6505 bruce@momjian.us 82 : 57 : DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
83 : 57 : char *in = (char *) PG_GETARG_POINTER(1);
2008 tgl@sss.pgh.pa.us 84 : 57 : int len = PG_GETARG_INT32(2);
85 : : char *txt;
5056 86 : 57 : TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
87 : :
6536 88 : 57 : res[1].lexeme = NULL;
89 : :
2008 90 [ + + + + : 57 : if (d->absval && (in[0] == '+' || in[0] == '-'))
+ - ]
91 : : {
92 : 5 : len--;
93 : 5 : txt = pnstrdup(in + 1, len);
94 : : }
95 : : else
96 : 52 : txt = pnstrdup(in, len);
97 : :
98 [ + + ]: 57 : if (len > d->maxlen)
99 : : {
6505 bruce@momjian.us 100 [ + + ]: 38 : if (d->rejectlong)
101 : : {
102 : : /* reject by returning void array */
6536 tgl@sss.pgh.pa.us 103 : 2 : pfree(txt);
104 : 2 : res[0].lexeme = NULL;
105 : : }
106 : : else
107 : : {
108 : : /* trim integer */
109 : 36 : txt[d->maxlen] = '\0';
110 : 36 : res[0].lexeme = txt;
111 : : }
112 : : }
113 : : else
114 : : {
115 : 19 : res[0].lexeme = txt;
116 : : }
117 : :
118 : 57 : PG_RETURN_POINTER(res);
119 : : }
|