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-2017, 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 (pg_strcasecmp(defel->defname, "MAXLEN") == 0)
46  {
47  d->maxlen = atoi(defGetString(defel));
48  }
49  else if (pg_strcasecmp(defel->defname, "REJECTLONG") == 0)
50  {
51  d->rejectlong = defGetBoolean(defel);
52  }
53  else
54  {
55  ereport(ERROR,
56  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57  errmsg("unrecognized intdict parameter: \"%s\"",
58  defel->defname)));
59  }
60  }
61 
63 }
64 
65 Datum
67 {
68  DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
69  char *in = (char *) PG_GETARG_POINTER(1);
70  char *txt = pnstrdup(in, PG_GETARG_INT32(2));
71  TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
72 
73  res[1].lexeme = NULL;
74  if (PG_GETARG_INT32(2) > d->maxlen)
75  {
76  if (d->rejectlong)
77  {
78  /* reject by returning void array */
79  pfree(txt);
80  res[0].lexeme = NULL;
81  }
82  else
83  {
84  /* trim integer */
85  txt[d->maxlen] = '\0';
86  res[0].lexeme = txt;
87  }
88  }
89  else
90  {
91  res[0].lexeme = txt;
92  }
93 
94  PG_RETURN_POINTER(res);
95 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
int maxlen
Definition: dict_int.c:22
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1074
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:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool defGetBoolean(DefElem *def)
Definition: define.c:111
void pfree(void *pointer)
Definition: mcxt.c:936
#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:122
void * palloc0(Size size)
Definition: mcxt.c:864
uintptr_t Datum
Definition: postgres.h:372
#define lfirst(lc)
Definition: pg_list.h:106
PG_FUNCTION_INFO_V1(dintdict_init)
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
Datum dintdict_lexize(PG_FUNCTION_ARGS)
Definition: dict_int.c:66
char * defname
Definition: parsenodes.h:719
Definition: pg_list.h:45