PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dict_int.c
Go to the documentation of this file.
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
19
20typedef struct
21{
22 int maxlen;
24 bool absval;
25} DictInt;
26
27
30
33{
34 List *dictoptions = (List *) PG_GETARG_POINTER(0);
35 DictInt *d;
36 ListCell *l;
37
38 d = (DictInt *) palloc0(sizeof(DictInt));
39 d->maxlen = 6;
40 d->rejectlong = false;
41 d->absval = false;
42
43 foreach(l, dictoptions)
44 {
45 DefElem *defel = (DefElem *) lfirst(l);
46
47 if (strcmp(defel->defname, "maxlen") == 0)
48 {
49 d->maxlen = atoi(defGetString(defel));
50
51 if (d->maxlen < 1)
53 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
54 errmsg("maxlen value has to be >= 1")));
55 }
56 else if (strcmp(defel->defname, "rejectlong") == 0)
57 {
58 d->rejectlong = defGetBoolean(defel);
59 }
60 else if (strcmp(defel->defname, "absval") == 0)
61 {
62 d->absval = defGetBoolean(defel);
63 }
64 else
65 {
67 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
68 errmsg("unrecognized intdict parameter: \"%s\"",
69 defel->defname)));
70 }
71 }
72
74}
75
78{
80 char *in = (char *) PG_GETARG_POINTER(1);
81 int len = PG_GETARG_INT32(2);
82 char *txt;
83 TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
84
85 res[1].lexeme = NULL;
86
87 if (d->absval && (in[0] == '+' || in[0] == '-'))
88 {
89 len--;
90 txt = pnstrdup(in + 1, len);
91 }
92 else
93 txt = pnstrdup(in, len);
94
95 if (len > d->maxlen)
96 {
97 if (d->rejectlong)
98 {
99 /* reject by returning void array */
100 pfree(txt);
101 res[0].lexeme = NULL;
102 }
103 else
104 {
105 /* trim integer */
106 txt[d->maxlen] = '\0';
107 res[0].lexeme = txt;
108 }
109 }
110 else
111 {
112 res[0].lexeme = txt;
113 }
114
116}
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
PG_MODULE_MAGIC
Definition: dict_int.c:18
Datum dintdict_lexize(PG_FUNCTION_ARGS)
Definition: dict_int.c:77
PG_FUNCTION_INFO_V1(dintdict_init)
Datum dintdict_init(PG_FUNCTION_ARGS)
Definition: dict_int.c:32
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
const void size_t len
#define lfirst(lc)
Definition: pg_list.h:172
uintptr_t Datum
Definition: postgres.h:64
char * defname
Definition: parsenodes.h:817
bool absval
Definition: dict_int.c:24
bool rejectlong
Definition: dict_int.c:23
int maxlen
Definition: dict_int.c:22
Definition: pg_list.h:54