]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Accept abbreviated JSON Paths on the right-hand side of the -> and ->> operators.
authordrh <>
Fri, 7 Jan 2022 15:47:12 +0000 (15:47 +0000)
committerdrh <>
Fri, 7 Jan 2022 15:47:12 +0000 (15:47 +0000)
FossilOrigin-Name: d15410900dccbb7159683c29f640fa321b1e019718827334f5fefe89da623008

ext/misc/json1.c
manifest
manifest.uuid
test/json102.test

index 5addd7dbcceb67b5d6420b6f96dbe97aa532a1a5..6f66d4e70cf9efcb5bc4b5e5a7e160ee20513b02 100644 (file)
@@ -1598,34 +1598,62 @@ static void jsonExtractFunc(
   JsonNode *pNode;
   const char *zPath;
   JsonString jx;
-  int i;
 
   if( argc<2 ) return;
   p = jsonParseCached(ctx, argv, ctx);
   if( p==0 ) return;
-  jsonInit(&jx, ctx);
-  jsonAppendChar(&jx, '[');
-  for(i=1; i<argc; i++){
-    zPath = (const char*)sqlite3_value_text(argv[i]);
-    pNode = jsonLookup(p, zPath, 0, ctx);
-    if( p->nErr ) break;
-    if( argc>2 ){
+  if( argc==2 ){
+    /* With a single PATH argument, the return is the unquoted SQL value */
+    zPath = (const char*)sqlite3_value_text(argv[1]);
+    if( zPath && zPath[0]!='$' && zPath[0]!=0
+     && *(int*)sqlite3_user_data(ctx)
+    ){
+      /* The -> and ->> operators accept abbreviated PATH arguments:
+      **     NUMBER   ==>  $[NUMBER]
+      **     LABEL    ==>  $.LABEL
+      **     [NUMBER] ==>  $[NUMBER]
+      */
+      jsonInit(&jx, ctx);
+      if( safe_isdigit(zPath[0]) ){
+        jsonAppendRaw(&jx, "$[", 2);
+        jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
+        jsonAppendRaw(&jx, "]", 2);
+      }else{
+        jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
+        jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
+        jsonAppendChar(&jx, 0);
+      }
+      pNode = jsonLookup(p, jx.zBuf, 0, ctx);
+      jsonReset(&jx);
+    }else{
+      pNode = jsonLookup(p, zPath, 0, ctx);
+    }
+    if( p->nErr ) return;
+    if( pNode ) jsonReturn(pNode, ctx, 0);
+  }else{
+    /* Two or more PATH arguments results in a JSON array with each
+    ** element of the array being the value selected by one of the PATHs */
+    int i;
+    jsonInit(&jx, ctx);
+    jsonAppendChar(&jx, '[');
+    for(i=1; i<argc; i++){
+      zPath = (const char*)sqlite3_value_text(argv[i]);
+      pNode = jsonLookup(p, zPath, 0, ctx);
+      if( p->nErr ) break;
       jsonAppendSeparator(&jx);
       if( pNode ){
         jsonRenderNode(pNode, &jx, 0);
       }else{
         jsonAppendRaw(&jx, "null", 4);
       }
-    }else if( pNode ){
-      jsonReturn(pNode, ctx, 0);
     }
+    if( i==argc ){
+      jsonAppendChar(&jx, ']');
+      jsonResult(&jx);
+      sqlite3_result_subtype(ctx, JSON_SUBTYPE);
+    }
+    jsonReset(&jx);
   }
-  if( argc>2 && i==argc ){
-    jsonAppendChar(&jx, ']');
-    jsonResult(&jx);
-    sqlite3_result_subtype(ctx, JSON_SUBTYPE);
-  }
-  jsonReset(&jx);
 }
 
 /* This is the RFC 7396 MergePatch algorithm.
index b962a3d961c001d437a022968ef4079a2b62d293..3fa4543e069c30e8fbec2cc736645c2f69d90ed0 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\snew\sbinary\soperators\s"->"\sand\s"->>"\sto\sthe\sparser\sthat\sevaluate\sto\n2-argument\sSQL\sfunctions\sby\sthe\ssame\sname.\s\sAdd\snew\s"->"\sand\s"->>"\sfunctions\nto\sthe\sJSON\sextension\sthat\sare\saliases\sfor\sjson_extract().
-D 2022-01-07T14:58:47.701
+C Accept\sabbreviated\sJSON\sPaths\son\sthe\sright-hand\sside\sof\sthe\s->\sand\s->>\soperators.
+D 2022-01-07T15:47:12.461
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -306,7 +306,7 @@ F ext/misc/fileio.c 4e7f7cd30de8df4820c552f14af3c9ca451c5ffe1f2e7bef34d598a12ebf
 F ext/misc/fossildelta.c 1240b2d3e52eab1d50c160c7fe1902a9bd210e052dc209200a750bbf885402d5
 F ext/misc/fuzzer.c eae560134f66333e9e1ca4c8ffea75df42056e2ce8456734565dbe1c2a92bf3d
 F ext/misc/ieee754.c 91a5594071143a4ab79c638fe9f059af1db09932faf2e704c3e29216a7d4f511
-F ext/misc/json1.c ee5d8250f78d598e0c8b81fc37e8fa1b52098e5f7bb71f0db3cd39c1acc4ca78
+F ext/misc/json1.c 81a11151fbeefd8870c31a9e77576daeacc9ef1c66153950ea2eb084193235c6
 F ext/misc/memstat.c 3017a0832c645c0f8c773435620d663855f04690172316bd127270d1a7523d4d
 F ext/misc/memtrace.c 7c0d115d2ef716ad0ba632c91e05bd119cb16c1aedf3bec9f06196ead2d5537b
 F ext/misc/memvfs.c 7dffa8cc89c7f2d73da4bd4ccea1bcbd2bd283e3bb4cea398df7c372a197291b
@@ -1147,7 +1147,7 @@ F test/jrnlmode.test 9b5bc01dac22223cb60ec2d5f97acf568d73820794386de5634dcadbea9
 F test/jrnlmode2.test 8759a1d4657c064637f8b079592651530db738419e1d649c6df7048cd724363d
 F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa
 F test/json101.test bb71538005f2d9e18620bdd3b76839a93ca0be61903eb8d751a64e78cf99b8fb
-F test/json102.test eeb54efa221e50b74a2d6fb9259963b48d7414dca3ce2fdfdeed45cb28487bc1
+F test/json102.test 8ee925a282f03f47526ca4e260b8e4af9f4e5b562f38647aa4f32dc84c19a0a3
 F test/json103.test aff6b7a4c17d5a20b487a7bc1a274bfdc63b829413bdfb83bedac42ec7f67e3b
 F test/json104.test 2cb7ff2cca2c8214d3e5260eeb9ce45faec0926f68b3e40c1aaa6ca247284144
 F test/json105.test 45f7d6a9a54c85f8a9589b68d3e7a1f42d02f2359911a8cdbad1f9988f571173
@@ -1937,11 +1937,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 2d6a16caa7d28ad5c766036b2eb6c2020683fcc9389b3c7df2013739929dd36f
-R a976b35db7b5d2f5710ec976fab3e1b7
-T *branch * json-enhancements
-T *sym-json-enhancements *
-T -sym-trunk *
+P c4e4e3a3fc5da0381ccb7930706e57d7831d87f9c63bafe49ae64117701e1cfe
+R dd2f603ec9049bd111aea3757f3d8f8e
 U drh
-Z e4a5223ea04d8f66a6958c2ae1a2641b
+Z 9891da1f488bb126e807d6977835eae4
 # Remove this line to create a well-formed Fossil manifest.
index 20f17305aee86b2af9a5a7bd1a6e60d3872ef28b..6b7aeda36ed6ebbbff5ad82010e6adb4d5630b36 100644 (file)
@@ -1 +1 @@
-c4e4e3a3fc5da0381ccb7930706e57d7831d87f9c63bafe49ae64117701e1cfe
\ No newline at end of file
+d15410900dccbb7159683c29f640fa321b1e019718827334f5fefe89da623008
\ No newline at end of file
index 18a6bbf5cc7dba686a77ada3f78f6b83a1db6e63..6afe2307b364cea57b13b0090a8674ffd8147ad2 100644 (file)
@@ -71,21 +71,48 @@ do_execsql_test json102-240 {
 do_execsql_test json102-250 {
   SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$');
 } {{{"a":2,"c":[4,5,{"f":7}]}}}
+do_execsql_test json102-251 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$';
+} {{{"a":2,"c":[4,5,{"f":7}]}}}
 do_execsql_test json102-260 {
   SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c');
 } {{[4,5,{"f":7}]}}
+do_execsql_test json102-261 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c';
+} {{[4,5,{"f":7}]}}
+do_execsql_test json102-262 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c';
+} {{[4,5,{"f":7}]}}
 do_execsql_test json102-270 {
   SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]');
 } {{{"f":7}}}
+do_execsql_test json102-271 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2]';
+} {{{"f":7}}}
+do_execsql_test json102-272 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2;
+} {{{"f":7}}}
 do_execsql_test json102-280 {
   SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f');
 } {{7}}
+do_execsql_test json102-281 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2 -> 'f';
+} {{7}}
+do_execsql_test json102-282 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> '[2]' -> 'f';
+} {{7}}
 do_execsql_test json102-290 {
   SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a');
 } {{[[4,5],2]}}
 do_execsql_test json102-300 {
   SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x');
 } {{}}
+do_execsql_test json102-301 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'x';
+} {{}}
+do_execsql_test json102-302 {
+  SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> NULL;
+} {{}}
 do_execsql_test json102-310 {
   SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a');
 } {{[null,2]}}