PostgreSQL Source Code  git master
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-2020, 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 
20 typedef struct
21 {
22  int maxlen;
23  bool rejectlong;
24 } DictInt;
25 
26 
29 
30 Datum
32 {
33  List *dictoptions = (List *) PG_GETARG_POINTER(0);
34  DictInt *d;
35  ListCell *l;
36 
37  d = (DictInt *) palloc0(sizeof(DictInt));
38  d->maxlen = 6;
39  d->rejectlong = false;
40 
41  foreach(l, dictoptions)
42  {
43  DefElem *defel = (DefElem *) lfirst(l);
44 
45  if (strcmp(defel->defname, "maxlen") == 0)
46  {
47  d->maxlen = atoi(defGetString(defel));
48 
49  if (d->maxlen < 1)
50  ereport(ERROR,
51  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
52  errmsg("maxlen value has to be >= 1")));
53  }
54  else if (strcmp(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 
68 }
69 
70 Datum
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 = palloc0(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 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
int maxlen
Definition: dict_int.c:22
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
bool rejectlong
Definition: dict_int.c:23
Datum dintdict_init(PG_FUNCTION_ARGS)
Definition: dict_int.c:31
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
bool defGetBoolean(DefElem *def)
Definition: define.c:111
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
PG_MODULE_MAGIC
Definition: dict_int.c:18
char * defGetString(DefElem *def)
Definition: define.c:49
char * lexeme
Definition: ts_public.h:111
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define lfirst(lc)
Definition: pg_list.h:190
PG_FUNCTION_INFO_V1(dintdict_init)
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
Datum dintdict_lexize(PG_FUNCTION_ARGS)
Definition: dict_int.c:71
char * defname
Definition: parsenodes.h:730
Definition: pg_list.h:50