PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ltxtquery_io.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "crc32.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

 PG_FUNCTION_INFO_V1 (ltxtq_in)
 
 PG_FUNCTION_INFO_V1 (ltxtq_out)
 
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)
 
Datum ltxtq_in (PG_FUNCTION_ARGS)
 
static void infix (INFIX *in, bool first)
 
Datum ltxtq_out (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define INOPERAND   2

Definition at line 20 of file ltxtquery_io.c.

Referenced by gettoken_query().

#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
signed int int32
Definition: c.h:256
static char * buf
Definition: pg_test_fsync.c:65
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963

Definition at line 403 of file ltxtquery_io.c.

Referenced by infix().

#define STACKDEPTH   32

Definition at line 198 of file ltxtquery_io.c.

Referenced by makepol().

#define WAITOPERAND   1

Definition at line 19 of file ltxtquery_io.c.

Referenced by gettoken_query(), and queryin().

#define WAITOPERATOR   3

Definition at line 21 of file ltxtquery_io.c.

Referenced by gettoken_query().

Typedef Documentation

typedef struct NODE NODE

Function Documentation

static void findoprnd ( ITEM ptr,
int32 pos 
)
static

Definition at line 278 of file ltxtquery_io.c.

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

Referenced by queryin().

279 {
280  /* since this function recurses, it could be driven to stack overflow. */
282 
283  if (ptr[*pos].type == VAL || ptr[*pos].type == VALTRUE)
284  {
285  ptr[*pos].left = 0;
286  (*pos)++;
287  }
288  else if (ptr[*pos].val == (int32) '!')
289  {
290  ptr[*pos].left = 1;
291  (*pos)++;
292  findoprnd(ptr, pos);
293  }
294  else
295  {
296  ITEM *curitem = &ptr[*pos];
297  int32 tmp = *pos;
298 
299  (*pos)++;
300  findoprnd(ptr, pos);
301  curitem->left = *pos - tmp;
302  findoprnd(ptr, pos);
303  }
304 }
Definition: _int.h:125
signed int int32
Definition: c.h:256
#define VALTRUE
Definition: ltree.h:128
void check_stack_depth(void)
Definition: postgres.c:3098
int16 left
Definition: _int.h:128
#define VAL
Definition: _int.h:147
static void findoprnd(ITEM *ptr, int32 *pos)
Definition: ltxtquery_io.c:278
long val
Definition: informix.c:689
static int32 gettoken_query ( QPRS_STATE state,
int32 val,
int32 lenval,
char **  strval,
uint16 flag 
)
static

Definition at line 58 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().

59 {
60  int charlen;
61 
62  for (;;)
63  {
64  charlen = pg_mblen(state->buf);
65 
66  switch (state->state)
67  {
68  case WAITOPERAND:
69  if (charlen == 1 && t_iseq(state->buf, '!'))
70  {
71  (state->buf)++;
72  *val = (int32) '!';
73  return OPR;
74  }
75  else if (charlen == 1 && t_iseq(state->buf, '('))
76  {
77  state->count++;
78  (state->buf)++;
79  return OPEN;
80  }
81  else if (ISALNUM(state->buf))
82  {
83  state->state = INOPERAND;
84  *strval = state->buf;
85  *lenval = charlen;
86  *flag = 0;
87  }
88  else if (!t_isspace(state->buf))
89  ereport(ERROR,
90  (errcode(ERRCODE_SYNTAX_ERROR),
91  errmsg("operand syntax error")));
92  break;
93  case INOPERAND:
94  if (ISALNUM(state->buf))
95  {
96  if (*flag)
97  ereport(ERROR,
98  (errcode(ERRCODE_SYNTAX_ERROR),
99  errmsg("modifiers syntax error")));
100  *lenval += charlen;
101  }
102  else if (charlen == 1 && t_iseq(state->buf, '%'))
103  *flag |= LVAR_SUBLEXEME;
104  else if (charlen == 1 && t_iseq(state->buf, '@'))
105  *flag |= LVAR_INCASE;
106  else if (charlen == 1 && t_iseq(state->buf, '*'))
107  *flag |= LVAR_ANYEND;
108  else
109  {
110  state->state = WAITOPERATOR;
111  return VAL;
112  }
113  break;
114  case WAITOPERATOR:
115  if (charlen == 1 && (t_iseq(state->buf, '&') || t_iseq(state->buf, '|')))
116  {
117  state->state = WAITOPERAND;
118  *val = (int32) *(state->buf);
119  (state->buf)++;
120  return OPR;
121  }
122  else if (charlen == 1 && t_iseq(state->buf, ')'))
123  {
124  (state->buf)++;
125  state->count--;
126  return (state->count < 0) ? ERR : CLOSE;
127  }
128  else if (*(state->buf) == '\0')
129  return (state->count) ? ERR : END;
130  else if (charlen == 1 && !t_iseq(state->buf, ' '))
131  return ERR;
132  break;
133  default:
134  return ERR;
135  break;
136  }
137 
138  state->buf += charlen;
139  }
140 }
#define t_isspace(x)
Definition: ts_locale.h:58
#define LVAR_INCASE
Definition: ltree.h:44
#define WAITOPERAND
Definition: ltxtquery_io.c:19
#define ERR
Definition: _int.h:146
#define ISALNUM(x)
Definition: ltree.h:83
#define LVAR_ANYEND
Definition: ltree.h:43
int32 count
Definition: ltxtquery_io.c:41
char * buf
Definition: ltxtquery_io.c:39
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
int32 state
Definition: ltxtquery_io.c:40
#define END
Definition: _int.h:145
#define CLOSE
Definition: _int.h:150
#define ERROR
Definition: elog.h:43
#define WAITOPERATOR
Definition: ltxtquery_io.c:21
#define t_iseq(x, c)
Definition: ts_locale.h:61
char * flag(int b)
Definition: test-ctype.c:33
#define ereport(elevel, rest)
Definition: elog.h:122
#define OPEN
Definition: _int.h:149
#define VAL
Definition: _int.h:147
#define OPR
Definition: _int.h:148
#define LVAR_SUBLEXEME
Definition: ltree.h:45
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
#define INOPERAND
Definition: ltxtquery_io.c:20
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
static void infix ( INFIX in,
bool  first 
)
static

Definition at line 417 of file ltxtquery_io.c.

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

Referenced by ltxtq_out().

418 {
419  /* since this function recurses, it could be driven to stack overflow. */
421 
422  if (in->curpol->type == VAL)
423  {
424  char *op = in->op + in->curpol->distance;
425 
426  RESIZEBUF(in, in->curpol->length * 2 + 5);
427  while (*op)
428  {
429  *(in->cur) = *op;
430  op++;
431  in->cur++;
432  }
433  if (in->curpol->flag & LVAR_SUBLEXEME)
434  {
435  *(in->cur) = '%';
436  in->cur++;
437  }
438  if (in->curpol->flag & LVAR_INCASE)
439  {
440  *(in->cur) = '@';
441  in->cur++;
442  }
443  if (in->curpol->flag & LVAR_ANYEND)
444  {
445  *(in->cur) = '*';
446  in->cur++;
447  }
448  *(in->cur) = '\0';
449  in->curpol++;
450  }
451  else if (in->curpol->val == (int32) '!')
452  {
453  bool isopr = false;
454 
455  RESIZEBUF(in, 1);
456  *(in->cur) = '!';
457  in->cur++;
458  *(in->cur) = '\0';
459  in->curpol++;
460  if (in->curpol->type == OPR)
461  {
462  isopr = true;
463  RESIZEBUF(in, 2);
464  sprintf(in->cur, "( ");
465  in->cur = strchr(in->cur, '\0');
466  }
467  infix(in, isopr);
468  if (isopr)
469  {
470  RESIZEBUF(in, 2);
471  sprintf(in->cur, " )");
472  in->cur = strchr(in->cur, '\0');
473  }
474  }
475  else
476  {
477  int32 op = in->curpol->val;
478  INFIX nrm;
479 
480  in->curpol++;
481  if (op == (int32) '|' && !first)
482  {
483  RESIZEBUF(in, 2);
484  sprintf(in->cur, "( ");
485  in->cur = strchr(in->cur, '\0');
486  }
487 
488  nrm.curpol = in->curpol;
489  nrm.op = in->op;
490  nrm.buflen = 16;
491  nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
492 
493  /* get right operand */
494  infix(&nrm, false);
495 
496  /* get & print left operand */
497  in->curpol = nrm.curpol;
498  infix(in, false);
499 
500  /* print operator & right operand */
501  RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
502  sprintf(in->cur, " %c %s", op, nrm.buf);
503  in->cur = strchr(in->cur, '\0');
504  pfree(nrm.buf);
505 
506  if (op == (int32) '|' && !first)
507  {
508  RESIZEBUF(in, 2);
509  sprintf(in->cur, " )");
510  in->cur = strchr(in->cur, '\0');
511  }
512  }
513 }
#define LVAR_INCASE
Definition: ltree.h:44
ITEM * curpol
Definition: _int_bool.c:551
#define LVAR_ANYEND
Definition: ltree.h:43
uint8 length
Definition: ltree.h:98
char * op
Definition: ltxtquery_io.c:399
uint8 flag
Definition: ltree.h:96
signed int int32
Definition: c.h:256
int16 type
Definition: _int.h:127
void pfree(void *pointer)
Definition: mcxt.c:950
void check_stack_depth(void)
Definition: postgres.c:3098
int32 val
Definition: _int.h:129
#define VAL
Definition: _int.h:147
#define OPR
Definition: _int.h:148
#define LVAR_SUBLEXEME
Definition: ltree.h:45
static void infix(INFIX *in, bool first)
Definition: ltxtquery_io.c:417
char * cur
Definition: _int_bool.c:553
void * palloc(Size size)
Definition: mcxt.c:849
int32 buflen
Definition: _int_bool.c:554
#define RESIZEBUF(inf, addsize)
Definition: ltxtquery_io.c:403
uint16 distance
Definition: ltree.h:99
char * buf
Definition: _int_bool.c:552
Datum ltxtq_in ( PG_FUNCTION_ARGS  )

Definition at line 386 of file ltxtquery_io.c.

References PG_GETARG_POINTER, PG_RETURN_POINTER, and queryin().

387 {
389 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static ltxtquery * queryin(char *buf)
Definition: ltxtquery_io.c:311
Datum ltxtq_out ( PG_FUNCTION_ARGS  )

Definition at line 516 of file ltxtquery_io.c.

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

517 {
518  ltxtquery *query = PG_GETARG_LTXTQUERY(0);
519  INFIX nrm;
520 
521  if (query->size == 0)
522  ereport(ERROR,
523  (errcode(ERRCODE_SYNTAX_ERROR),
524  errmsg("syntax error"),
525  errdetail("Empty query.")));
526 
527  nrm.curpol = GETQUERY(query);
528  nrm.buflen = 32;
529  nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
530  *(nrm.cur) = '\0';
531  nrm.op = GETOPERAND(query);
532  infix(&nrm, true);
533 
534  PG_FREE_IF_COPY(query, 0);
535  PG_RETURN_POINTER(nrm.buf);
536 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
int32 size
Definition: ltree.h:109
ITEM * curpol
Definition: _int_bool.c:551
#define PG_GETARG_LTXTQUERY(x)
Definition: ltree.h:172
char * op
Definition: ltxtquery_io.c:399
int errcode(int sqlerrcode)
Definition: elog.c:575
#define GETQUERY(x)
Definition: _int.h:142
#define GETOPERAND(x)
Definition: ltree.h:118
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
static void infix(INFIX *in, bool first)
Definition: ltxtquery_io.c:417
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
char * cur
Definition: _int_bool.c:553
void * palloc(Size size)
Definition: mcxt.c:849
int32 buflen
Definition: _int_bool.c:554
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * buf
Definition: _int_bool.c:552
static int32 makepol ( QPRS_STATE state)
static

Definition at line 203 of file ltxtquery_io.c.

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

Referenced by queryin().

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

Definition at line 146 of file ltxtquery_io.c.

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

Referenced by makepol(), and pushval_asis().

147 {
148  NODE *tmp = (NODE *) palloc(sizeof(NODE));
149 
150  tmp->type = type;
151  tmp->val = val;
152  tmp->flag = flag;
153  if (distance > 0xffff)
154  ereport(ERROR,
155  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
156  errmsg("value is too big")));
157  if (lenval > 0xff)
158  ereport(ERROR,
159  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
160  errmsg("operand is too long")));
161  tmp->distance = distance;
162  tmp->length = lenval;
163  tmp->next = state->str;
164  state->str = tmp;
165  state->num++;
166 }
Definition: _int_bool.c:27
int32 val
Definition: _int_bool.c:30
int errcode(int sqlerrcode)
Definition: elog.c:575
struct NODE * next
Definition: _int_bool.c:31
NODE * str
Definition: ltxtquery_io.c:43
#define ERROR
Definition: elog.h:43
uint16 flag
Definition: ltxtquery_io.c:33
char * flag(int b)
Definition: test-ctype.c:33
#define ereport(elevel, rest)
Definition: elog.h:122
int16 length
Definition: ltxtquery_io.c:32
int16 distance
Definition: ltxtquery_io.c:31
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
int32 type
Definition: _int_bool.c:29
static void pushval_asis ( QPRS_STATE state,
int  type,
char *  strval,
int  lenval,
uint16  flag 
)
static

Definition at line 172 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().

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

Definition at line 311 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, LTXTQUERY_TOO_BIG, makepol(), NODE::next, NULL, QPRS_STATE::num, QPRS_STATE::op, palloc(), palloc0(), pfree(), 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().

312 {
314  int32 i;
315  ltxtquery *query;
316  int32 commonlen;
317  ITEM *ptr;
318  NODE *tmp;
319  int32 pos = 0;
320 
321 #ifdef BS_DEBUG
322  char pbuf[16384],
323  *cur;
324 #endif
325 
326  /* init state */
327  state.buf = buf;
328  state.state = WAITOPERAND;
329  state.count = 0;
330  state.num = 0;
331  state.str = NULL;
332 
333  /* init list of operand */
334  state.sumlen = 0;
335  state.lenop = 64;
336  state.curop = state.op = (char *) palloc(state.lenop);
337  *(state.curop) = '\0';
338 
339  /* parse query & make polish notation (postfix, but in reverse order) */
340  makepol(&state);
341  if (!state.num)
342  ereport(ERROR,
343  (errcode(ERRCODE_SYNTAX_ERROR),
344  errmsg("syntax error"),
345  errdetail("Empty query.")));
346 
347  if (LTXTQUERY_TOO_BIG(state.num, state.sumlen))
348  ereport(ERROR,
349  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
350  errmsg("ltxtquery is too large")));
351  commonlen = COMPUTESIZE(state.num, state.sumlen);
352 
353  query = (ltxtquery *) palloc0(commonlen);
354  SET_VARSIZE(query, commonlen);
355  query->size = state.num;
356  ptr = GETQUERY(query);
357 
358  /* set item in polish notation */
359  for (i = 0; i < state.num; i++)
360  {
361  ptr[i].type = state.str->type;
362  ptr[i].val = state.str->val;
363  ptr[i].distance = state.str->distance;
364  ptr[i].length = state.str->length;
365  ptr[i].flag = state.str->flag;
366  tmp = state.str->next;
367  pfree(state.str);
368  state.str = tmp;
369  }
370 
371  /* set user friendly-operand view */
372  memcpy((void *) GETOPERAND(query), (void *) state.op, state.sumlen);
373  pfree(state.op);
374 
375  /* set left operand's position for every operator */
376  pos = 0;
377  findoprnd(ptr, &pos);
378 
379  return query;
380 }
Definition: _int.h:125
#define WAITOPERAND
Definition: ltxtquery_io.c:19
static int32 makepol(QPRS_STATE *state)
Definition: ltxtquery_io.c:203
Definition: _int_bool.c:27
#define LTXTQUERY_TOO_BIG(size, lenofoperand)
Definition: ltree.h:115
int32 size
Definition: ltree.h:109
int32 val
Definition: _int_bool.c:30
int32 count
Definition: ltxtquery_io.c:41
char * op
Definition: ltxtquery_io.c:50
uint8 length
Definition: ltree.h:98
char * curop
Definition: ltxtquery_io.c:51
struct cursor * cur
Definition: ecpg.c:28
char * buf
Definition: ltxtquery_io.c:39
int errcode(int sqlerrcode)
Definition: elog.c:575
uint8 flag
Definition: ltree.h:96
#define GETQUERY(x)
Definition: _int.h:142
int32 sumlen
Definition: ltxtquery_io.c:49
struct NODE * next
Definition: _int_bool.c:31
NODE * str
Definition: ltxtquery_io.c:43
signed int int32
Definition: c.h:256
#define GETOPERAND(x)
Definition: ltree.h:118
int16 type
Definition: _int.h:127
int32 state
Definition: ltxtquery_io.c:40
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
uint16 flag
Definition: ltxtquery_io.c:33
static char * buf
Definition: pg_test_fsync.c:65
int errdetail(const char *fmt,...)
Definition: elog.c:873
int32 val
Definition: _int.h:129
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:878
#define COMPUTESIZE(size)
Definition: _int.h:140
int32 lenop
Definition: ltxtquery_io.c:48
int16 length
Definition: ltxtquery_io.c:32
#define NULL
Definition: c.h:229
Definition: regguts.h:298
int16 distance
Definition: ltxtquery_io.c:31
static void findoprnd(ITEM *ptr, int32 *pos)
Definition: ltxtquery_io.c:278
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
uint16 distance
Definition: ltree.h:99
int32 type
Definition: _int_bool.c:29