PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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{
38 DictInt *d;
39 ListCell *l;
40
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 {
53
54 if (d->maxlen < 1)
57 errmsg("maxlen value has to be >= 1")));
58 }
59 else if (strcmp(defel->defname, "rejectlong") == 0)
60 {
62 }
63 else if (strcmp(defel->defname, "absval") == 0)
64 {
66 }
67 else
68 {
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;
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:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
Datum dintdict_lexize(PG_FUNCTION_ARGS)
Definition dict_int.c:80
Datum dintdict_init(PG_FUNCTION_ARGS)
Definition dict_int.c:35
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
void pfree(void *pointer)
Definition mcxt.c:1616
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1792
const void size_t len
#define lfirst(lc)
Definition pg_list.h:172
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
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