From 9061e22a05f7b0037a9121a8f58f26e37ae8c2f0 Mon Sep 17 00:00:00 2001 From: drh <> Date: Thu, 5 Oct 2023 15:02:00 +0000 Subject: Allow the PG-style syntax for the PATH operand on the right-hand side of the ->> and -> operators. FossilOrigin-Name: bae5071b0834aa3b7fd4bd871f863e1b148c6558989c8f6cdd02dc4da4770953 --- src/json.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/json.c b/src/json.c index 955c35377..3307db0ba 100644 --- a/src/json.c +++ b/src/json.c @@ -3990,12 +3990,38 @@ static void jsonExtractFromBlob( u32 i; JsonParse px; if( zPath==0 ) return; - if( zPath[0]=='$' ) zPath++; memset(&px, 0, sizeof(px)); px.nBlob = sqlite3_value_bytes(pJson); px.aBlob = (u8*)sqlite3_value_blob(pJson); if( px.aBlob==0 ) return; - i = jsonLookupBlobStep(&px, 0, zPath, &zErr); + if( zPath[0]=='$' ){ + zPath++; + i = jsonLookupBlobStep(&px, 0, zPath, &zErr); + }else if( (flags & JSON_ABPATH) ){ + /* The -> and ->> operators accept abbreviated PATH arguments. This + ** is mostly for compatibility with PostgreSQL, but also for + ** convenience. + ** + ** NUMBER ==> $[NUMBER] // PG compatible + ** LABEL ==> $.LABEL // PG compatible + ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience + */ + JsonString jx; + jsonStringInit(&jx, ctx); + if( sqlite3Isdigit(zPath[0]) ){ + jsonAppendRawNZ(&jx, "[", 1); + jsonAppendRaw(&jx, zPath, (int)strlen(zPath)); + jsonAppendRawNZ(&jx, "]", 2); + zPath = jx.zBuf; + }else if( zPath[0]!='[' ){ + jsonAppendRawNZ(&jx, ".", 1); + jsonAppendRaw(&jx, zPath, (int)strlen(zPath)); + jsonAppendChar(&jx, 0); + zPath = jx.zBuf; + } + i = jsonLookupBlobStep(&px, 0, zPath, &zErr); + jsonStringReset(&jx); + } if( i