Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 40ff744

Browse filesBrowse files
committed
new interface to JsQUery
1 parent b0094a7 commit 40ff744
Copy full SHA for 40ff744

File tree

5 files changed

+514
-354
lines changed
Filter options

5 files changed

+514
-354
lines changed

‎Makefile

Copy file name to clipboardExpand all lines: Makefile
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# contrib/jsquery/Makefile
22

33
MODULE_big = jsquery
4-
OBJS = jsquery_io.o jsquery_gram.o jsquery_op.o \
5-
jsquery_constr.o jsquery_extract.o jsonb_gin_ops.o
4+
OBJS = jsonb_gin_ops.o jsquery_constr.o jsquery_extract.o \
5+
jsquery_gram.o jsquery_io.o jsquery_op.o jsquery_support.o
66

77
EXTENSION = jsquery
88
DATA = jsquery--1.0.sql

‎jsquery.h

Copy file name to clipboardExpand all lines: jsquery.h
+39-2Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct JsQueryItem JsQueryItem;
5858

5959
struct JsQueryItem {
6060
JsQueryItemType type;
61+
JsQueryItem *next; /* next in path */
6162

6263
union {
6364
struct {
@@ -71,7 +72,7 @@ struct JsQueryItem {
7172
bool boolean;
7273
struct {
7374
uint32 len;
74-
char *val; /* could be not null-terminated */
75+
char *val; /* could not be not null-terminated */
7576
} string;
7677

7778
struct {
@@ -80,9 +81,45 @@ struct JsQueryItem {
8081
} array;
8182
};
8283

83-
JsQueryItem *next; /* next in path */
8484
};
8585

86+
typedef struct JsQueryItemR {
87+
JsQueryItemType type;
88+
int32 nextPos;
89+
char *base;
90+
91+
union {
92+
struct {
93+
char *data;
94+
int datalen; /* filled only for string */
95+
} value;
96+
97+
struct {
98+
int32 left;
99+
int32 right;
100+
} args;
101+
int32 arg;
102+
struct {
103+
int nelems;
104+
int current;
105+
int32 *arrayPtr;
106+
} array;
107+
};
108+
109+
110+
} JsQueryItemR;
111+
112+
extern void jsqInit(JsQueryItemR *v, char *base, int32 pos);
113+
extern bool jsqGetNext(JsQueryItemR *v, JsQueryItemR *a);
114+
extern void jsqGetArg(JsQueryItemR *v, JsQueryItemR *a);
115+
extern void jsqGetLeftArg(JsQueryItemR *v, JsQueryItemR *a);
116+
extern void jsqGetRightArg(JsQueryItemR *v, JsQueryItemR *a);
117+
extern Numeric jsqGetNumeric(JsQueryItemR *v);
118+
extern bool jsqGetBool(JsQueryItemR *v);
119+
extern char * jsqGetString(JsQueryItemR *v, int32 *len);
120+
extern void jsqIterateInit(JsQueryItemR *v);
121+
extern bool jsqIterateArray(JsQueryItemR *v, JsQueryItemR *e);
122+
86123
/*
87124
* support
88125
*/

‎jsquery_io.c

Copy file name to clipboardExpand all lines: jsquery_io.c
+51-96Lines changed: 51 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -213,92 +213,60 @@ printOperation(StringInfo buf, JsQueryItemType type)
213213
}
214214

215215
static void
216-
printJsQueryItem(StringInfo buf, char *base, int32 pos, bool inKey, bool printBracketes)
216+
printJsQueryItem(StringInfo buf, JsQueryItemR *v, bool inKey, bool printBracketes)
217217
{
218-
JsQueryItemType type;
219-
int32 nextPos;
218+
JsQueryItemR elem;
219+
bool first = true;
220220

221221
check_stack_depth();
222222

223-
pos = readJsQueryHeader(base, pos, &type, &nextPos);
224-
225-
switch(type)
223+
switch(v->type)
226224
{
227225
case jqiNull:
228226
appendStringInfoString(buf, "null");
229227
break;
230228
case jqiKey:
229+
if (inKey)
230+
appendStringInfoChar(buf, '.');
231231
case jqiString:
232-
{
233-
int32 len;
234-
235-
read_int32(len, base, pos);
236-
if (inKey && type == jqiKey)
237-
appendStringInfoChar(buf, '.');
238-
escape_json(buf, base + pos);
239-
pos += len + 1;
240-
}
232+
escape_json(buf, jsqGetString(v, NULL));
241233
break;
242234
case jqiNumeric:
243-
{
244-
Numeric n = (Numeric)(base + pos);
245-
246-
pos += VARSIZE(n);
247-
248-
appendStringInfoString(buf,
249-
DatumGetCString(DirectFunctionCall1(numeric_out,
250-
PointerGetDatum(n))));
251-
}
252-
break;
235+
appendStringInfoString(buf,
236+
DatumGetCString(DirectFunctionCall1(numeric_out,
237+
PointerGetDatum(jsqGetNumeric(v)))));
238+
break;
253239
case jqiBool:
254-
{
255-
bool v;
256-
257-
read_byte(v, base, pos);
258-
259-
if (v)
260-
appendBinaryStringInfo(buf, "true", 4);
261-
else
262-
appendBinaryStringInfo(buf, "false", 5);
263-
}
240+
if (jsqGetBool(v))
241+
appendBinaryStringInfo(buf, "true", 4);
242+
else
243+
appendBinaryStringInfo(buf, "false", 5);
264244
break;
265245
case jqiArray:
266-
{
267-
int32 i, nelems, *arrayPos;
268-
269-
read_int32(nelems, base, pos);
270-
arrayPos = (int32*)(base + pos);
271-
pos += nelems * sizeof(*arrayPos);
272-
273-
if (printBracketes)
274-
appendStringInfoChar(buf, '[');
275-
276-
for(i=0; i<nelems; i++)
277-
{
278-
if (i != 0)
279-
appendBinaryStringInfo(buf, ", ", 2);
280-
281-
printJsQueryItem(buf, base, arrayPos[i], false, true);
282-
}
246+
if (printBracketes)
247+
appendStringInfoChar(buf, '[');
283248

284-
if (printBracketes)
285-
appendStringInfoChar(buf, ']');
249+
while(jsqIterateArray(v, &elem))
250+
{
251+
if (first == false)
252+
appendBinaryStringInfo(buf, ", ", 2);
253+
else
254+
first = false;
255+
printJsQueryItem(buf, &elem, false, true);
286256
}
257+
258+
if (printBracketes)
259+
appendStringInfoChar(buf, ']');
287260
break;
288261
case jqiAnd:
289262
case jqiOr:
290-
{
291-
int32 left, right;
292-
293-
read_int32(left, base, pos);
294-
read_int32(right, base, pos);
295-
296-
appendStringInfoChar(buf, '(');
297-
printJsQueryItem(buf, base, left, false, true);
298-
printOperation(buf, type);
299-
printJsQueryItem(buf, base, right, false, true);
300-
appendStringInfoChar(buf, ')');
301-
}
263+
appendStringInfoChar(buf, '(');
264+
jsqGetLeftArg(v, &elem);
265+
printJsQueryItem(buf, &elem, false, true);
266+
printOperation(buf, v->type);
267+
jsqGetRightArg(v, &elem);
268+
printJsQueryItem(buf, &elem, false, true);
269+
appendStringInfoChar(buf, ')');
302270
break;
303271
case jqiEqual:
304272
case jqiLess:
@@ -308,36 +276,21 @@ printJsQueryItem(StringInfo buf, char *base, int32 pos, bool inKey, bool printBr
308276
case jqiContains:
309277
case jqiContained:
310278
case jqiOverlap:
311-
{
312-
int32 arg;
313-
314-
read_int32(arg, base, pos);
315-
316-
printOperation(buf, type);
317-
printJsQueryItem(buf, base, arg, false, true);
318-
}
279+
printOperation(buf, v->type);
280+
jsqGetArg(v, &elem);
281+
printJsQueryItem(buf, &elem, false, true);
319282
break;
320283
case jqiIn:
321-
{
322-
int32 arg;
323-
324-
read_int32(arg, base, pos);
325-
326-
appendBinaryStringInfo(buf, " IN (", 5);
327-
printJsQueryItem(buf, base, arg, false, false);
328-
appendStringInfoChar(buf, ')');
329-
}
284+
appendBinaryStringInfo(buf, " IN (", 5);
285+
jsqGetArg(v, &elem);
286+
printJsQueryItem(buf, &elem, false, false);
287+
appendStringInfoChar(buf, ')');
330288
break;
331289
case jqiNot:
332-
{
333-
int32 arg;
334-
335-
read_int32(arg, base, pos);
336-
337-
appendBinaryStringInfo(buf, "!(", 2);
338-
printJsQueryItem(buf, base, arg, false, true);
339-
appendStringInfoChar(buf, ')');
340-
}
290+
appendBinaryStringInfo(buf, "!(", 2);
291+
jsqGetArg(v, &elem);
292+
printJsQueryItem(buf, &elem, false, true);
293+
appendStringInfoChar(buf, ')');
341294
break;
342295
case jqiAny:
343296
if (inKey)
@@ -360,11 +313,11 @@ printJsQueryItem(StringInfo buf, char *base, int32 pos, bool inKey, bool printBr
360313
appendStringInfoChar(buf, '%');
361314
break;
362315
default:
363-
elog(ERROR, "Unknown JsQueryItem type: %d", type);
316+
elog(ERROR, "Unknown JsQueryItem type: %d", v->type);
364317
}
365318

366-
if (nextPos > 0)
367-
printJsQueryItem(buf, base, nextPos, true, true);
319+
if (jsqGetNext(v, &elem))
320+
printJsQueryItem(buf, &elem, true, true);
368321
}
369322

370323
PG_FUNCTION_INFO_V1(jsquery_out);
@@ -373,11 +326,13 @@ jsquery_out(PG_FUNCTION_ARGS)
373326
{
374327
JsQuery *in = PG_GETARG_JSQUERY(0);
375328
StringInfoData buf;
329+
JsQueryItemR v;
376330

377331
initStringInfo(&buf);
378332
enlargeStringInfo(&buf, VARSIZE(in) /* estimation */);
379333

380-
printJsQueryItem(&buf, VARDATA(in), 0, false, true);
334+
jsqInit(&v, VARDATA(in), 0);
335+
printJsQueryItem(&buf, &v, false, true);
381336

382337
PG_RETURN_CSTRING(buf.data);
383338
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.