PostgreSQL Source Code  git master
ltxtquery_io.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "crc32.h"
#include "libpq/pqformat.h"
#include "ltree.h"
#include "miscadmin.h"
Include dependency graph for ltxtquery_io.c:

Go to the source code of this file.

Data Structures

struct  NODE
 
struct  QPRS_STATE
 
struct  INFIX
 

Macros

#define WAITOPERAND   1
 
#define INOPERAND   2
 
#define WAITOPERATOR   3
 
#define STACKDEPTH   32
 
#define RESIZEBUF(inf, addsize)
 

Typedefs

typedef struct NODE NODE
 

Functions

static int32 gettoken_query (QPRS_STATE *state, int32 *val, int32 *lenval, char **strval, uint16 *flag)
 
static void pushquery (QPRS_STATE *state, int32 type, int32 val, int32 distance, int32 lenval, uint16 flag)
 
static void pushval_asis (QPRS_STATE *state, int type, char *strval, int lenval, uint16 flag)
 
static int32 makepol (QPRS_STATE *state)
 
static void findoprnd (ITEM *ptr, int32 *pos)
 
static ltxtqueryqueryin (char *buf)
 
 PG_FUNCTION_INFO_V1 (ltxtq_in)
 
Datum ltxtq_in (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (ltxtq_recv)
 
Datum ltxtq_recv (PG_FUNCTION_ARGS)
 
static void infix (INFIX *in, bool first)
 
 PG_FUNCTION_INFO_V1 (ltxtq_out)
 
Datum ltxtq_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (ltxtq_send)
 
Datum ltxtq_send (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ INOPERAND

#define INOPERAND   2

Definition at line 18 of file ltxtquery_io.c.

Referenced by gettoken_query().

◆ RESIZEBUF

#define RESIZEBUF (   inf,
  addsize 
)
Value:
while( ( (inf)->cur - (inf)->buf ) + (addsize) + 1 >= (inf)->buflen ) \
{ \
int32 len = (inf)->cur - (inf)->buf; \
(inf)->buflen *= 2; \
(inf)->buf = (char*) repalloc( (void*)(inf)->buf, (inf)->buflen ); \
(inf)->cur = (inf)->buf + len; \
}
struct cursor * cur
Definition: ecpg.c:28
static char * buf
Definition: pg_test_fsync.c:67
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069

Definition at line 429 of file ltxtquery_io.c.

Referenced by infix().

◆ STACKDEPTH

#define STACKDEPTH   32

Definition at line 195 of file ltxtquery_io.c.

Referenced by makepol().

◆ WAITOPERAND

#define WAITOPERAND   1

Definition at line 17 of file ltxtquery_io.c.

Referenced by gettoken_query(), and queryin().

◆ WAITOPERATOR

#define WAITOPERATOR   3

Definition at line 19 of file ltxtquery_io.c.

Referenced by gettoken_query().

Typedef Documentation

◆ NODE

typedef struct NODE NODE

Function Documentation

◆ findoprnd()

static void findoprnd ( ITEM ptr,
int32 pos 
)
static

Definition at line 275 of file ltxtquery_io.c.

References check_stack_depth(), ITEM::left, NODE::type, NODE::val, VAL, and VALTRUE.

Referenced by queryin().

276 {
277  /* since this function recurses, it could be driven to stack overflow. */
279 
280  if (ptr[*pos].type == VAL || ptr[*pos].type == VALTRUE)
281  {
282  ptr[*pos].left = 0;
283  (*pos)++;
284  }
285  else if (ptr[*pos].val == (int32) '!')
286  {
287  ptr[*pos].left = 1;
288  (*pos)++;
289  findoprnd(ptr, pos);
290  }
291  else
292  {
293  ITEM *curitem = &ptr[*pos];
294  int32 tmp = *pos;
295 
296  (*pos)++;
297  findoprnd(ptr, pos);
298  curitem->left = *pos - tmp;
299  findoprnd(ptr, pos);
300  }
301 }
Definition: _int.h:140
signed int int32
Definition: c.h:355
#define VALTRUE
Definition: ltree.h:161
void check_stack_depth(void)
Definition: postgres.c:3312
int16 left
Definition: _int.h:143
#define VAL
Definition: _int.h:162
static void findoprnd(ITEM *ptr, int32 *pos)
Definition: ltxtquery_io.c:275
long val
Definition: informix.c:664

◆ gettoken_query()

static int32 gettoken_query ( QPRS_STATE state,
int32 val,
int32 lenval,
char **  strval,
uint16 flag 
)
static

Definition at line 56 of file ltxtquery_io.c.

References QPRS_STATE::buf, CLOSE, QPRS_STATE::count, END, ereport, ERR, errcode(), errmsg(), ERROR, INOPERAND, ISALNUM, LVAR_ANYEND, LVAR_INCASE, LVAR_SUBLEXEME, OPEN, OPR, pg_mblen(), QPRS_STATE::state, t_iseq, t_isspace(), VAL, WAITOPERAND, and WAITOPERATOR.

Referenced by makepol().

57 {
58  int charlen;
59 
60  for (;;)
61  {
62  charlen = pg_mblen(state->buf);
63 
64  switch (state->state)
65  {
66  case WAITOPERAND:
67  if (charlen == 1 && t_iseq(state->buf, '!'))
68  {
69  (state->buf)++;
70  *val = (int32) '!';
71  return OPR;
72  }
73  else if (charlen == 1 && t_iseq(state->buf, '('))
74  {
75  state->count++;
76  (state->buf)++;
77  return OPEN;
78  }
79  else if (ISALNUM(state->buf))
80  {
81  state->state = INOPERAND;
82  *strval = state->buf;
83  *lenval = charlen;
84  *flag = 0;
85  }
86  else if (!t_isspace(state->buf))
87  ereport(ERROR,
88  (errcode(ERRCODE_SYNTAX_ERROR),
89  errmsg("operand syntax error")));
90  break;
91  case INOPERAND:
92  if (ISALNUM(state->buf))
93  {
94  if (*flag)
95  ereport(ERROR,
96  (errcode(ERRCODE_SYNTAX_ERROR),
97  errmsg("modifiers syntax error")));
98  *lenval += charlen;
99  }
100  else if (charlen == 1 && t_iseq(state->buf, '%'))
101  *flag |= LVAR_SUBLEXEME;
102  else if (charlen == 1 && t_iseq(state->buf, '@'))
103  *flag |= LVAR_INCASE;
104  else if (charlen == 1 && t_iseq(state->buf, '*'))
105  *flag |= LVAR_ANYEND;
106  else
107  {
108  state->state = WAITOPERATOR;
109  return VAL;
110  }
111  break;
112  case WAITOPERATOR:
113  if (charlen == 1 && (t_iseq(state->buf, '&') || t_iseq(state->buf, '|')))
114  {
115  state->state = WAITOPERAND;
116  *val = (int32) *(state->buf);
117  (state->buf)++;
118  return OPR;
119  }
120  else if (charlen == 1 && t_iseq(state->buf, ')'))
121  {
122  (state->buf)++;
123  state->count--;
124  return (state->count < 0) ? ERR : CLOSE;
125  }
126  else if (*(state->buf) == '\0')
127  return (state->count) ? ERR : END;
128  else if (charlen == 1 && !t_iseq(state->buf, ' '))
129  return ERR;
130  break;
131  default:
132  return ERR;
133  break;
134  }
135 
136  state->buf += charlen;
137  }
138 }
#define LVAR_INCASE
Definition: ltree.h:62
#define WAITOPERAND
Definition: ltxtquery_io.c:17
#define ERR
Definition: _int.h:161
#define ISALNUM(x)
Definition: ltree.h:116
#define LVAR_ANYEND
Definition: ltree.h:61
int32 count
Definition: ltxtquery_io.c:39
char * buf
Definition: ltxtquery_io.c:37
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:355
int32 state
Definition: ltxtquery_io.c:38
#define END
Definition: _int.h:160
#define CLOSE
Definition: _int.h:165
#define ERROR
Definition: elog.h:43
int t_isspace(const char *ptr)
Definition: ts_locale.c:52
#define WAITOPERATOR
Definition: ltxtquery_io.c:19
#define t_iseq(x, c)
Definition: ts_locale.h:43
char * flag(int b)
Definition: test-ctype.c:33
#define OPEN
Definition: _int.h:164
#define ereport(elevel,...)
Definition: elog.h:144
#define VAL
Definition: _int.h:162
#define OPR
Definition: _int.h:163
#define LVAR_SUBLEXEME
Definition: ltree.h:63
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
#define INOPERAND
Definition: ltxtquery_io.c:18
int errmsg(const char *fmt,...)
Definition: elog.c:824
long val
Definition: informix.c:664

◆ infix()

static void infix ( INFIX in,
bool  first 
)
static

Definition at line 443 of file ltxtquery_io.c.

References INFIX::buf, INFIX::buflen, check_stack_depth(), INFIX::cur, INFIX::curpol, ITEM::distance, ITEM::flag, ITEM::length, ltxtq_out(), LVAR_ANYEND, LVAR_INCASE, LVAR_SUBLEXEME, INFIX::op, OPR, palloc(), pfree(), PG_FUNCTION_INFO_V1(), RESIZEBUF, sprintf, ITEM::type, ITEM::val, and VAL.

Referenced by ltxtq_out(), and ltxtq_send().

444 {
445  /* since this function recurses, it could be driven to stack overflow. */
447 
448  if (in->curpol->type == VAL)
449  {
450  char *op = in->op + in->curpol->distance;
451 
452  RESIZEBUF(in, in->curpol->length * 2 + 5);
453  while (*op)
454  {
455  *(in->cur) = *op;
456  op++;
457  in->cur++;
458  }
459  if (in->curpol->flag & LVAR_SUBLEXEME)
460  {
461  *(in->cur) = '%';
462  in->cur++;
463  }
464  if (in->curpol->flag & LVAR_INCASE)
465  {
466  *(in->cur) = '@';
467  in->cur++;
468  }
469  if (in->curpol->flag & LVAR_ANYEND)
470  {
471  *(in->cur) = '*';
472  in->cur++;
473  }
474  *(in->cur) = '\0';
475  in->curpol++;
476  }
477  else if (in->curpol->val == (int32) '!')
478  {
479  bool isopr = false;
480 
481  RESIZEBUF(in, 1);
482  *(in->cur) = '!';
483  in->cur++;
484  *(in->cur) = '\0';
485  in->curpol++;
486  if (in->curpol->type == OPR)
487  {
488  isopr = true;
489  RESIZEBUF(in, 2);
490  sprintf(in->cur, "( ");
491  in->cur = strchr(in->cur, '\0');
492  }
493  infix(in, isopr);
494  if (isopr)
495  {
496  RESIZEBUF(in, 2);
497  sprintf(in->cur, " )");
498  in->cur = strchr(in->cur, '\0');
499  }
500  }
501  else
502  {
503  int32 op = in->curpol->val;
504  INFIX nrm;
505 
506  in->curpol++;
507  if (op == (int32) '|' && !first)
508  {
509  RESIZEBUF(in, 2);
510  sprintf(in->cur, "( ");
511  in->cur = strchr(in->cur, '\0');
512  }
513 
514  nrm.curpol = in->curpol;
515  nrm.op = in->op;
516  nrm.buflen = 16;
517  nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
518 
519  /* get right operand */
520  infix(&nrm, false);
521 
522  /* get & print left operand */
523  in->curpol = nrm.curpol;
524  infix(in, false);
525 
526  /* print operator & right operand */
527  RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
528  sprintf(in->cur, " %c %s", op, nrm.buf);
529  in->cur = strchr(in->cur, '\0');
530  pfree(nrm.buf);
531 
532  if (op == (int32) '|' && !first)
533  {
534  RESIZEBUF(in, 2);
535  sprintf(in->cur, " )");
536  in->cur = strchr(in->cur, '\0');
537  }
538  }
539 }
#define LVAR_INCASE
Definition: ltree.h:62
ITEM * curpol
Definition: _int_bool.c:550
#define LVAR_ANYEND
Definition: ltree.h:61
uint8 length
Definition: ltree.h:131
char * op
Definition: ltxtquery_io.c:425
uint8 flag
Definition: ltree.h:129
signed int int32
Definition: c.h:355
int16 type
Definition: _int.h:142
#define sprintf
Definition: port.h:195
void pfree(void *pointer)
Definition: mcxt.c:1056
void check_stack_depth(void)
Definition: postgres.c:3312
int32 val
Definition: _int.h:144
#define VAL
Definition: _int.h:162
#define OPR
Definition: _int.h:163
#define LVAR_SUBLEXEME
Definition: ltree.h:63
static void infix(INFIX *in, bool first)
Definition: ltxtquery_io.c:443
char * cur
Definition: _int_bool.c:552
void * palloc(Size size)
Definition: mcxt.c:949
int32 buflen
Definition: _int_bool.c:553
#define RESIZEBUF(inf, addsize)
Definition: ltxtquery_io.c:429
uint16 distance
Definition: ltree.h:132
char * buf
Definition: _int_bool.c:551

◆ ltxtq_in()

Datum ltxtq_in ( PG_FUNCTION_ARGS  )

Definition at line 384 of file ltxtquery_io.c.

References ltxtq_recv(), PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RETURN_POINTER, and queryin().

Referenced by queryin().

385 {
387 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static ltxtquery * queryin(char *buf)
Definition: ltxtquery_io.c:308

◆ ltxtq_out()

Datum ltxtq_out ( PG_FUNCTION_ARGS  )

Definition at line 543 of file ltxtquery_io.c.

References INFIX::buf, INFIX::buflen, INFIX::cur, INFIX::curpol, ereport, errcode(), errdetail(), errmsg(), ERROR, GETOPERAND, GETQUERY, infix(), ltxtq_send(), INFIX::op, palloc(), PG_FUNCTION_INFO_V1(), PG_GETARG_LTXTQUERY_P, PG_RETURN_POINTER, and ltxtquery::size.

Referenced by infix().

544 {
545  ltxtquery *query = PG_GETARG_LTXTQUERY_P(0);
546  INFIX nrm;
547 
548  if (query->size == 0)
549  ereport(ERROR,
550  (errcode(ERRCODE_SYNTAX_ERROR),
551  errmsg("syntax error"),
552  errdetail("Empty query.")));
553 
554  nrm.curpol = GETQUERY(query);
555  nrm.buflen = 32;
556  nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
557  *(nrm.cur) = '\0';
558  nrm.op = GETOPERAND(query);
559  infix(&nrm, true);
560 
561  PG_RETURN_POINTER(nrm.buf);
562 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
int32 size
Definition: ltree.h:142
ITEM * curpol
Definition: _int_bool.c:550
char * op
Definition: ltxtquery_io.c:425
int errcode(int sqlerrcode)
Definition: elog.c:610
#define GETQUERY(x)
Definition: _int.h:157
#define GETOPERAND(x)
Definition: ltree.h:151
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define ereport(elevel,...)
Definition: elog.h:144
static void infix(INFIX *in, bool first)
Definition: ltxtquery_io.c:443
#define PG_GETARG_LTXTQUERY_P(n)
Definition: ltree.h:214
char * cur
Definition: _int_bool.c:552
void * palloc(Size size)
Definition: mcxt.c:949
int32 buflen
Definition: _int_bool.c:553
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * buf
Definition: _int_bool.c:551

◆ ltxtq_recv()

Datum ltxtq_recv ( PG_FUNCTION_ARGS  )

Definition at line 399 of file ltxtquery_io.c.

References buf, cur, StringInfoData::cursor, elog, ERROR, StringInfoData::len, pfree(), PG_GETARG_POINTER, PG_RETURN_POINTER, pq_getmsgint(), pq_getmsgtext(), queryin(), and generate_unaccent_rules::str.

Referenced by ltxtq_in().

400 {
402  int version = pq_getmsgint(buf, 1);
403  char *str;
404  int nbytes;
405  ltxtquery *res;
406 
407  if (version != 1)
408  elog(ERROR, "unsupported ltxtquery version number %d", version);
409 
410  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
411  res = queryin(str);
412  pfree(str);
413 
414  PG_RETURN_POINTER(res);
415 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
static ltxtquery * queryin(char *buf)
Definition: ltxtquery_io.c:308
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
#define elog(elevel,...)
Definition: elog.h:214
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ ltxtq_send()

Datum ltxtq_send ( PG_FUNCTION_ARGS  )

Definition at line 574 of file ltxtquery_io.c.

References buf, INFIX::buf, INFIX::buflen, INFIX::cur, INFIX::curpol, ereport, errcode(), errdetail(), errmsg(), ERROR, GETOPERAND, GETQUERY, infix(), INFIX::op, palloc(), pfree(), PG_GETARG_LTXTQUERY_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint8(), pq_sendtext(), and ltxtquery::size.

Referenced by ltxtq_out().

575 {
576  ltxtquery *query = PG_GETARG_LTXTQUERY_P(0);
578  int version = 1;
579  INFIX nrm;
580 
581  if (query->size == 0)
582  ereport(ERROR,
583  (errcode(ERRCODE_SYNTAX_ERROR),
584  errmsg("syntax error"),
585  errdetail("Empty query.")));
586 
587  nrm.curpol = GETQUERY(query);
588  nrm.buflen = 32;
589  nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
590  *(nrm.cur) = '\0';
591  nrm.op = GETOPERAND(query);
592  infix(&nrm, true);
593 
594  pq_begintypsend(&buf);
595  pq_sendint8(&buf, version);
596  pq_sendtext(&buf, nrm.buf, strlen(nrm.buf));
597  pfree(nrm.buf);
598 
600 }
int32 size
Definition: ltree.h:142
ITEM * curpol
Definition: _int_bool.c:550
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
char * op
Definition: ltxtquery_io.c:425
int errcode(int sqlerrcode)
Definition: elog.c:610
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
#define GETQUERY(x)
Definition: _int.h:157
#define GETOPERAND(x)
Definition: ltree.h:151
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define ereport(elevel,...)
Definition: elog.h:144
static void infix(INFIX *in, bool first)
Definition: ltxtquery_io.c:443
#define PG_GETARG_LTXTQUERY_P(n)
Definition: ltree.h:214
char * cur
Definition: _int_bool.c:552
void * palloc(Size size)
Definition: mcxt.c:949
int32 buflen
Definition: _int_bool.c:553
int errmsg(const char *fmt,...)
Definition: elog.c:824
static void pq_sendint8(StringInfo buf, uint8 i)
Definition: pqformat.h:129
char * buf
Definition: _int_bool.c:551

◆ makepol()

static int32 makepol ( QPRS_STATE state)
static

Definition at line 200 of file ltxtquery_io.c.

References check_stack_depth(), CLOSE, elog, END, ereport, ERR, errcode(), errmsg(), ERROR, NODE::flag, gettoken_query(), OPEN, OPR, pushquery(), pushval_asis(), STACKDEPTH, NODE::type, NODE::val, and VAL.

Referenced by queryin().

201 {
202  int32 val = 0,
203  type;
204  int32 lenval = 0;
205  char *strval = NULL;
206  int32 stack[STACKDEPTH];
207  int32 lenstack = 0;
208  uint16 flag = 0;
209 
210  /* since this function recurses, it could be driven to stack overflow */
212 
213  while ((type = gettoken_query(state, &val, &lenval, &strval, &flag)) != END)
214  {
215  switch (type)
216  {
217  case VAL:
218  pushval_asis(state, VAL, strval, lenval, flag);
219  while (lenstack && (stack[lenstack - 1] == (int32) '&' ||
220  stack[lenstack - 1] == (int32) '!'))
221  {
222  lenstack--;
223  pushquery(state, OPR, stack[lenstack], 0, 0, 0);
224  }
225  break;
226  case OPR:
227  if (lenstack && val == (int32) '|')
228  pushquery(state, OPR, val, 0, 0, 0);
229  else
230  {
231  if (lenstack == STACKDEPTH)
232  /* internal error */
233  elog(ERROR, "stack too short");
234  stack[lenstack] = val;
235  lenstack++;
236  }
237  break;
238  case OPEN:
239  if (makepol(state) == ERR)
240  return ERR;
241  while (lenstack && (stack[lenstack - 1] == (int32) '&' ||
242  stack[lenstack - 1] == (int32) '!'))
243  {
244  lenstack--;
245  pushquery(state, OPR, stack[lenstack], 0, 0, 0);
246  }
247  break;
248  case CLOSE:
249  while (lenstack)
250  {
251  lenstack--;
252  pushquery(state, OPR, stack[lenstack], 0, 0, 0);
253  };
254  return END;
255  break;
256  case ERR:
257  default:
258  ereport(ERROR,
259  (errcode(ERRCODE_SYNTAX_ERROR),
260  errmsg("syntax error")));
261 
262  return ERR;
263 
264  }
265  }
266  while (lenstack)
267  {
268  lenstack--;
269  pushquery(state, OPR, stack[lenstack], 0, 0, 0);
270  };
271  return END;
272 }
static int32 makepol(QPRS_STATE *state)
Definition: ltxtquery_io.c:200
#define ERR
Definition: _int.h:161
static void pushval_asis(QPRS_STATE *state, int type, char *strval, int lenval, uint16 flag)
Definition: ltxtquery_io.c:170
int errcode(int sqlerrcode)
Definition: elog.c:610
static int32 gettoken_query(QPRS_STATE *state, int32 *val, int32 *lenval, char **strval, uint16 *flag)
Definition: ltxtquery_io.c:56
signed int int32
Definition: c.h:355
unsigned short uint16
Definition: c.h:366
#define END
Definition: _int.h:160
#define CLOSE
Definition: _int.h:165
#define ERROR
Definition: elog.h:43
void check_stack_depth(void)
Definition: postgres.c:3312
char * flag(int b)
Definition: test-ctype.c:33
#define OPEN
Definition: _int.h:164
static void pushquery(QPRS_STATE *state, int32 type, int32 val, int32 distance, int32 lenval, uint16 flag)
Definition: ltxtquery_io.c:144
#define ereport(elevel,...)
Definition: elog.h:144
#define VAL
Definition: _int.h:162
#define OPR
Definition: _int.h:163
#define STACKDEPTH
Definition: ltxtquery_io.c:195
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
long val
Definition: informix.c:664

◆ PG_FUNCTION_INFO_V1() [1/4]

PG_FUNCTION_INFO_V1 ( ltxtq_in  )

Referenced by infix(), ltxtq_in(), ltxtq_out(), and queryin().

◆ PG_FUNCTION_INFO_V1() [2/4]

PG_FUNCTION_INFO_V1 ( ltxtq_recv  )

◆ PG_FUNCTION_INFO_V1() [3/4]

PG_FUNCTION_INFO_V1 ( ltxtq_out  )

◆ PG_FUNCTION_INFO_V1() [4/4]

PG_FUNCTION_INFO_V1 ( ltxtq_send  )

◆ pushquery()

static void pushquery ( QPRS_STATE state,
int32  type,
int32  val,
int32  distance,
int32  lenval,
uint16  flag 
)
static

Definition at line 144 of file ltxtquery_io.c.

References NODE::distance, ereport, errcode(), errmsg(), ERROR, NODE::flag, NODE::length, NODE::next, QPRS_STATE::num, palloc(), QPRS_STATE::str, NODE::type, and NODE::val.

Referenced by makepol(), and pushval_asis().

145 {
146  NODE *tmp = (NODE *) palloc(sizeof(NODE));
147 
148  tmp->type = type;
149  tmp->val = val;
150  tmp->flag = flag;
151  if (distance > 0xffff)
152  ereport(ERROR,
153  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
154  errmsg("value is too big")));
155  if (lenval > 0xff)
156  ereport(ERROR,
157  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
158  errmsg("operand is too long")));
159  tmp->distance = distance;
160  tmp->length = lenval;
161  tmp->next = state->str;
162  state->str = tmp;
163  state->num++;
164 }
Definition: _int_bool.c:26
int32 val
Definition: _int_bool.c:29
int errcode(int sqlerrcode)
Definition: elog.c:610
struct NODE * next
Definition: _int_bool.c:30
NODE * str
Definition: ltxtquery_io.c:41
#define ERROR
Definition: elog.h:43
uint16 flag
Definition: ltxtquery_io.c:31
char * flag(int b)
Definition: test-ctype.c:33
#define ereport(elevel,...)
Definition: elog.h:144
int16 length
Definition: ltxtquery_io.c:30
int16 distance
Definition: ltxtquery_io.c:29
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
long val
Definition: informix.c:664
int32 type
Definition: _int_bool.c:28

◆ pushval_asis()

static void pushval_asis ( QPRS_STATE state,
int  type,
char *  strval,
int  lenval,
uint16  flag 
)
static

Definition at line 170 of file ltxtquery_io.c.

References QPRS_STATE::curop, ereport, errcode(), errmsg(), ERROR, QPRS_STATE::lenop, ltree_crc32_sz(), QPRS_STATE::op, pushquery(), repalloc(), and QPRS_STATE::sumlen.

Referenced by makepol().

171 {
172  if (lenval > 0xffff)
173  ereport(ERROR,
174  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
175  errmsg("word is too long")));
176 
177  pushquery(state, type, ltree_crc32_sz(strval, lenval),
178  state->curop - state->op, lenval, flag);
179 
180  while (state->curop - state->op + lenval + 1 >= state->lenop)
181  {
182  int32 tmp = state->curop - state->op;
183 
184  state->lenop *= 2;
185  state->op = (char *) repalloc((void *) state->op, state->lenop);
186  state->curop = state->op + tmp;
187  }
188  memcpy((void *) state->curop, (void *) strval, lenval);
189  state->curop += lenval;
190  *(state->curop) = '\0';
191  state->curop++;
192  state->sumlen += lenval + 1;
193 }
unsigned int ltree_crc32_sz(const char *buf, int size)
Definition: crc32.c:23
char * op
Definition: ltxtquery_io.c:48
char * curop
Definition: ltxtquery_io.c:49
int errcode(int sqlerrcode)
Definition: elog.c:610
int32 sumlen
Definition: ltxtquery_io.c:47
signed int int32
Definition: c.h:355
#define ERROR
Definition: elog.h:43
char * flag(int b)
Definition: test-ctype.c:33
static void pushquery(QPRS_STATE *state, int32 type, int32 val, int32 distance, int32 lenval, uint16 flag)
Definition: ltxtquery_io.c:144
#define ereport(elevel,...)
Definition: elog.h:144
int32 lenop
Definition: ltxtquery_io.c:46
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ queryin()

static ltxtquery* queryin ( char *  buf)
static

Definition at line 308 of file ltxtquery_io.c.

References QPRS_STATE::buf, buf, COMPUTESIZE, QPRS_STATE::count, cur, QPRS_STATE::curop, NODE::distance, ITEM::distance, ereport, errcode(), errdetail(), errmsg(), ERROR, findoprnd(), NODE::flag, ITEM::flag, GETOPERAND, GETQUERY, i, NODE::length, ITEM::length, QPRS_STATE::lenop, ltxtq_in(), LTXTQUERY_TOO_BIG, makepol(), NODE::next, QPRS_STATE::num, QPRS_STATE::op, palloc(), palloc0(), pfree(), PG_FUNCTION_INFO_V1(), SET_VARSIZE, ltxtquery::size, QPRS_STATE::state, QPRS_STATE::str, QPRS_STATE::sumlen, NODE::type, ITEM::type, NODE::val, ITEM::val, and WAITOPERAND.

Referenced by ltxtq_in(), and ltxtq_recv().

309 {
311  int32 i;
312  ltxtquery *query;
313  int32 commonlen;
314  ITEM *ptr;
315  NODE *tmp;
316  int32 pos = 0;
317 
318 #ifdef BS_DEBUG
319  char pbuf[16384],
320  *cur;
321 #endif
322 
323  /* init state */
324  state.buf = buf;
325  state.state = WAITOPERAND;
326  state.count = 0;
327  state.num = 0;
328  state.str = NULL;
329 
330  /* init list of operand */
331  state.sumlen = 0;
332  state.lenop = 64;
333  state.curop = state.op = (char *) palloc(state.lenop);
334  *(state.curop) = '\0';
335 
336  /* parse query & make polish notation (postfix, but in reverse order) */
337  makepol(&state);
338  if (!state.num)
339  ereport(ERROR,
340  (errcode(ERRCODE_SYNTAX_ERROR),
341  errmsg("syntax error"),
342  errdetail("Empty query.")));
343 
344  if (LTXTQUERY_TOO_BIG(state.num, state.sumlen))
345  ereport(ERROR,
346  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
347  errmsg("ltxtquery is too large")));
348  commonlen = COMPUTESIZE(state.num, state.sumlen);
349 
350  query = (ltxtquery *) palloc0(commonlen);
351  SET_VARSIZE(query, commonlen);
352  query->size = state.num;
353  ptr = GETQUERY(query);
354 
355  /* set item in polish notation */
356  for (i = 0; i < state.num; i++)
357  {
358  ptr[i].type = state.str->type;
359  ptr[i].val = state.str->val;
360  ptr[i].distance = state.str->distance;
361  ptr[i].length = state.str->length;
362  ptr[i].flag = state.str->flag;
363  tmp = state.str->next;
364  pfree(state.str);
365  state.str = tmp;
366  }
367 
368  /* set user-friendly operand view */
369  memcpy((void *) GETOPERAND(query), (void *) state.op, state.sumlen);
370  pfree(state.op);
371 
372  /* set left operand's position for every operator */
373  pos = 0;
374  findoprnd(ptr, &pos);
375 
376  return query;
377 }
Definition: _int.h:140
#define WAITOPERAND
Definition: ltxtquery_io.c:17
static int32 makepol(QPRS_STATE *state)
Definition: ltxtquery_io.c:200
Definition: _int_bool.c:26
#define LTXTQUERY_TOO_BIG(size, lenofoperand)
Definition: ltree.h:148
int32 size
Definition: ltree.h:142
int32 val
Definition: _int_bool.c:29
int32 count
Definition: ltxtquery_io.c:39
char * op
Definition: ltxtquery_io.c:48
uint8 length
Definition: ltree.h:131
char * curop
Definition: ltxtquery_io.c:49
struct cursor * cur
Definition: ecpg.c:28
char * buf
Definition: ltxtquery_io.c:37
int errcode(int sqlerrcode)
Definition: elog.c:610
uint8 flag
Definition: ltree.h:129
#define GETQUERY(x)
Definition: _int.h:157
int32 sumlen
Definition: ltxtquery_io.c:47
struct NODE * next
Definition: _int_bool.c:30
NODE * str
Definition: ltxtquery_io.c:41
signed int int32
Definition: c.h:355
#define GETOPERAND(x)
Definition: ltree.h:151
int16 type
Definition: _int.h:142
int32 state
Definition: ltxtquery_io.c:38
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
uint16 flag
Definition: ltxtquery_io.c:31
static char * buf
Definition: pg_test_fsync.c:67
int errdetail(const char *fmt,...)
Definition: elog.c:957
int32 val
Definition: _int.h:144
void * palloc0(Size size)
Definition: mcxt.c:980
#define COMPUTESIZE(size)
Definition: _int.h:155
#define ereport(elevel,...)
Definition: elog.h:144
int32 lenop
Definition: ltxtquery_io.c:46
int16 length
Definition: ltxtquery_io.c:30
Definition: regguts.h:298
int16 distance
Definition: ltxtquery_io.c:29
static void findoprnd(ITEM *ptr, int32 *pos)
Definition: ltxtquery_io.c:275
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
uint16 distance
Definition: ltree.h:132
int32 type
Definition: _int_bool.c:28