@@ -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
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
370323PG_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