PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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.

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 }
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
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_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc0(Size size)
Definition: mcxt.c:1347
#define lfirst(lc)
Definition: pg_list.h:172
char * defname
Definition: parsenodes.h:817
bool absval
Definition: dict_int.c:24
bool rejectlong
Definition: dict_int.c:23
int maxlen
Definition: dict_int.c:22
Definition: pg_list.h:54

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

◆ dintdict_lexize()

Datum dintdict_lexize ( PG_FUNCTION_ARGS  )

Definition at line 77 of file dict_int.c.

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 
116 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
void pfree(void *pointer)
Definition: mcxt.c:1521
const void size_t len

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

◆ 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.