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 .name = "dict_int",
20 .version = PG_VERSION
21);
22
23typedef struct
24{
25 int maxlen;
27 bool absval;
28} DictInt;
29
30
33
36{
37 List *dictoptions = (List *) PG_GETARG_POINTER(0);
38 DictInt *d;
39 ListCell *l;
40
41 d = (DictInt *) palloc0(sizeof(DictInt));
42 d->maxlen = 6;
43 d->rejectlong = false;
44 d->absval = false;
45
46 foreach(l, dictoptions)
47 {
48 DefElem *defel = (DefElem *) lfirst(l);
49
50 if (strcmp(defel->defname, "maxlen") == 0)
51 {
52 d->maxlen = atoi(defGetString(defel));
53
54 if (d->maxlen < 1)
56 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57 errmsg("maxlen value has to be >= 1")));
58 }
59 else if (strcmp(defel->defname, "rejectlong") == 0)
60 {
61 d->rejectlong = defGetBoolean(defel);
62 }
63 else if (strcmp(defel->defname, "absval") == 0)
64 {
65 d->absval = defGetBoolean(defel);
66 }
67 else
68 {
70 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
71 errmsg("unrecognized intdict parameter: \"%s\"",
72 defel->defname)));
73 }
74 }
75
77}
78
81{
83 char *in = (char *) PG_GETARG_POINTER(1);
84 int len = PG_GETARG_INT32(2);
85 char *txt;
86 TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
87
88 res[1].lexeme = NULL;
89
90 if (d->absval && (in[0] == '+' || in[0] == '-'))
91 {
92 len--;
93 txt = pnstrdup(in + 1, len);
94 }
95 else
96 txt = pnstrdup(in, len);
97
98 if (len > d->maxlen)
99 {
100 if (d->rejectlong)
101 {
102 /* reject by returning void array */
103 pfree(txt);
104 res[0].lexeme = NULL;
105 }
106 else
107 {
108 /* trim integer */
109 txt[d->maxlen] = '\0';
110 res[0].lexeme = txt;
111 }
112 }
113 else
114 {
115 res[0].lexeme = txt;
116 }
117
119}
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
Datum dintdict_lexize(PG_FUNCTION_ARGS)
Definition: dict_int.c:80
PG_FUNCTION_INFO_V1(dintdict_init)
PG_MODULE_MAGIC_EXT(.name="dict_int",.version=PG_VERSION)
Datum dintdict_init(PG_FUNCTION_ARGS)
Definition: dict_int.c:35
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:1524
void * palloc0(Size size)
Definition: mcxt.c:1347
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1710
const void size_t len
#define lfirst(lc)
Definition: pg_list.h:172
uintptr_t Datum
Definition: postgres.h:69
char * defname
Definition: parsenodes.h:826
bool absval
Definition: dict_int.c:27
bool rejectlong
Definition: dict_int.c:26
int maxlen
Definition: dict_int.c:25
Definition: pg_list.h:54
char * lexeme
Definition: ts_public.h:138
const char * name