@@ -213,92 +213,60 @@ printOperation(StringInfo buf, JsQueryItemType type)
213213}
214214
215215static 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
221221check_stack_depth ();
222222
223- pos = readJsQueryHeader (base ,pos ,& type ,& nextPos );
224-
225- switch (type )
223+ switch (v -> type )
226224{
227225case jqiNull :
228226appendStringInfoString (buf ,"null" );
229227break ;
230228case jqiKey :
229+ if (inKey )
230+ appendStringInfoChar (buf ,'.' );
231231case 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 ));
241233break ;
242234case 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 ;
253239case 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 );
264244break ;
265245case 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 ,']' );
287260break ;
288261case jqiAnd :
289262case 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 ,')' );
302270break ;
303271case jqiEqual :
304272case jqiLess :
@@ -308,36 +276,21 @@ printJsQueryItem(StringInfo buf, char *base, int32 pos, bool inKey, bool printBr
308276case jqiContains :
309277case jqiContained :
310278case 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);
319282break ;
320283case 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 ,')' );
330288break ;
331289case 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 ,')' );
341294break ;
342295case jqiAny :
343296if (inKey )
@@ -360,11 +313,11 @@ printJsQueryItem(StringInfo buf, char *base, int32 pos, bool inKey, bool printBr
360313appendStringInfoChar (buf ,'%' );
361314break ;
362315default :
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
370323PG_FUNCTION_INFO_V1 (jsquery_out );
@@ -373,11 +326,13 @@ jsquery_out(PG_FUNCTION_ARGS)
373326{
374327JsQuery * in = PG_GETARG_JSQUERY (0 );
375328StringInfoData buf ;
329+ JsQueryItemR v ;
376330
377331initStringInfo (& buf );
378332enlargeStringInfo (& 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
382337PG_RETURN_CSTRING (buf .data );
383338}