PostgreSQL Source Code  git master
dict_int.c File Reference
#include "postgres.h"
#include "commands/defrem.h"
#include "tsearch/ts_public.h"
Include dependency graph for dict_int.c:

Go to the source code of this file.

Data Structures

struct  DictInt
 

Functions

 PG_FUNCTION_INFO_V1 (dintdict_init)
 
 PG_FUNCTION_INFO_V1 (dintdict_lexize)
 
Datum dintdict_init (PG_FUNCTION_ARGS)
 
Datum dintdict_lexize (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ dintdict_init()

Datum dintdict_init ( PG_FUNCTION_ARGS  )

Definition at line 32 of file dict_int.c.

References DictInt::absval, defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, lfirst, DictInt::maxlen, palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, and DictInt::rejectlong.

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)
52  ereport(ERROR,
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  {
66  ereport(ERROR,
67  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
68  errmsg("unrecognized intdict parameter: \"%s\"",
69  defel->defname)));
70  }
71  }
72 
74 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
int maxlen
Definition: dict_int.c:22
bool rejectlong
Definition: dict_int.c:23
int errcode(int sqlerrcode)
Definition: elog.c:691
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
void * palloc0(Size size)
Definition: mcxt.c:981
#define ereport(elevel,...)
Definition: elog.h:155
#define lfirst(lc)
Definition: pg_list.h:169
int errmsg(const char *fmt,...)
Definition: elog.c:902
char * defname
Definition: parsenodes.h:733
Definition: pg_list.h:50
bool absval
Definition: dict_int.c:24

◆ dintdict_lexize()

Datum dintdict_lexize ( PG_FUNCTION_ARGS  )

Definition at line 77 of file dict_int.c.

References DictInt::absval, TSLexeme::lexeme, DictInt::maxlen, palloc0(), pfree(), PG_GETARG_INT32, PG_GETARG_POINTER, PG_RETURN_POINTER, pnstrdup(), and DictInt::rejectlong.

78 {
79  DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
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 
115  PG_RETURN_POINTER(res);
116 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
int maxlen
Definition: dict_int.c:22
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1198
bool rejectlong
Definition: dict_int.c:23
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
void pfree(void *pointer)
Definition: mcxt.c:1057
char * lexeme
Definition: ts_public.h:111
void * palloc0(Size size)
Definition: mcxt.c:981
bool absval
Definition: dict_int.c:24

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( dintdict_init  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( dintdict_lexize  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 18 of file dict_int.c.