]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Extend the json-path mechanism with array indexes of the form "#" or "#-n" for json-path-enhancement
authordrh <drh@noemail.net>
Fri, 22 Nov 2019 17:37:56 +0000 (17:37 +0000)
committerdrh <drh@noemail.net>
Fri, 22 Nov 2019 17:37:56 +0000 (17:37 +0000)
some positive number "n", to reference the end of an array.

FossilOrigin-Name: 35ed68a651f4cf8740597433b0f1c3b345841868022e0904e9f342840ba0e502

ext/misc/json1.c
manifest
manifest.uuid
test/json105.test [new file with mode: 0644]

index 82bffe874a7befe7b37c904a28ab8ed902e20160..012ac4a8906aea5f1167b3d8fb1fa434fcfb30b5 100644 (file)
@@ -1176,18 +1176,49 @@ static JsonNode *jsonLookupStep(
       }
       return pNode;
     }
-  }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
-    if( pRoot->eType!=JSON_ARRAY ) return 0;
+  }else if( zPath[0]=='[' ){
     i = 0;
     j = 1;
     while( safe_isdigit(zPath[j]) ){
       i = i*10 + zPath[j] - '0';
       j++;
     }
-    if( zPath[j]!=']' ){
-      *pzErr = zPath;
-      return 0;
+    if( j<2 || zPath[j]!=']' ){
+      if( zPath[1]=='#' ){
+        JsonNode *pBase = pRoot;
+        int iBase = iRoot;
+        if( pRoot->eType!=JSON_ARRAY ) return 0;
+        for(;;){
+          while( j<=pBase->n ){
+            if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
+            j += jsonNodeSize(&pBase[j]);
+          }
+          if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
+          iBase += pBase->u.iAppend;
+          pBase = &pParse->aNode[iBase];
+          j = 1;
+        }
+        j = 2;
+        if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
+          unsigned int x = 0;
+          j = 3;
+          do{
+            x = x*10 + zPath[j] - '0';
+            j++;
+          }while( safe_isdigit(zPath[j]) );
+          if( x>i ) return 0;
+          i -= x;
+        }
+        if( zPath[j]!=']' ){
+          *pzErr = zPath;
+          return 0;
+        }
+      }else{
+        *pzErr = zPath;
+        return 0;
+      }
     }
+    if( pRoot->eType!=JSON_ARRAY ) return 0;
     zPath += j + 1;
     j = 1;
     for(;;){
index 7493e92cfdc5efe9635c97e0b6ee10e7498fbb53..76300c848d609d0cc9584bada27b6c390a7cb5e5 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\sharmless\scompiler\swarning.
-D 2019-11-22T11:49:40.000
+C Extend\sthe\sjson-path\smechanism\swith\sarray\sindexes\sof\sthe\sform\s"#"\sor\s"#-n"\sfor\nsome\spositive\snumber\s"n",\sto\sreference\sthe\send\sof\san\sarray.
+D 2019-11-22T17:37:56.495
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -295,7 +295,7 @@ F ext/misc/fileio.c 288e7230e0fe464d71b0694e2d8bdd3a353118ac2e31da3964b95f460f09
 F ext/misc/fossildelta.c 7708651072eb5620ab21bbfb518d184f27b2c29c0131b09b9a2d8852a8016430
 F ext/misc/fuzzer.c c4e27daf41433a64cad5265cd27dbcb891147e9994d0422200ce81ce9a54b625
 F ext/misc/ieee754.c f190d0cc5182529acb15babd177781be1ac1718c
-F ext/misc/json1.c b4a8074e5a126379dd3af81b8595118c9c472b06f3fd508bd2ea579a75e3a1b1
+F ext/misc/json1.c 760107d0b97ab422952591b0d7519327dcb482954a7706ee387a54c135f8aabf
 F ext/misc/memstat.c 3017a0832c645c0f8c773435620d663855f04690172316bd127270d1a7523d4d
 F ext/misc/memtrace.c 7c0d115d2ef716ad0ba632c91e05bd119cb16c1aedf3bec9f06196ead2d5537b
 F ext/misc/memvfs.c ab36f49e02ebcdf85a1e08dc4d8599ea8f343e073ac9e0bca18a98b7e1ec9567
@@ -1099,6 +1099,7 @@ F test/json101.test bb71538005f2d9e18620bdd3b76839a93ca0be61903eb8d751a64e78cf99
 F test/json102.test eeb54efa221e50b74a2d6fb9259963b48d7414dca3ce2fdfdeed45cb28487bc1
 F test/json103.test aff6b7a4c17d5a20b487a7bc1a274bfdc63b829413bdfb83bedac42ec7f67e3b
 F test/json104.test 317f4ec4b2d87afbba4d2460cf5be297aea76f2285eb618d276dbcd40a50950f
+F test/json105.test 45f7d6a9a54c85f8a9589b68d3e7a1f42d02f2359911a8cdbad1f9988f571173
 F test/keyword1.test 37ef6bba5d2ed5b07ecdd6810571de2956599dff
 F test/kvtest.c 94da54bb66aae7a54e47cf7e4ea4acecc0f217560f79ad3abfcc0361d6d557ba
 F test/lastinsert.test 42e948fd6442f07d60acbd15d33fb86473e0ef63
@@ -1850,7 +1851,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P e6314e386c6877c669bd32788d3886253b0a187bee2c3889f4eaba219f5e52c7
-R f485ea78793a9a63600d3b4152792ab1
+P 34343c4b0657767f2676a2a9feb670cf000824e3eadfb1a6d87b57404191d8f9
+R c6da582f868a5f82d03ec9634a7b8150
+T *branch * json-path-enhancement
+T *sym-json-path-enhancement *
+T -sym-trunk *
 U drh
-Z a041bc0b0948e0c1d06b6422ec2beb5c
+Z 4a48cd4a80fd585147d8b1e47ef59f7e
index 33cefdfddc2a859feb9d217aca009c5b494cfc0f..fa75b62e991684cadba57d8c81232e23a213c1ce 100644 (file)
@@ -1 +1 @@
-34343c4b0657767f2676a2a9feb670cf000824e3eadfb1a6d87b57404191d8f9
\ No newline at end of file
+35ed68a651f4cf8740597433b0f1c3b345841868022e0904e9f342840ba0e502
\ No newline at end of file
diff --git a/test/json105.test b/test/json105.test
new file mode 100644 (file)
index 0000000..46d3677
--- /dev/null
@@ -0,0 +1,118 @@
+# 2019-11-22
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements tests for "[#]" extension to json-path
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix json104
+
+ifcapable !json1 {
+  finish_test
+  return
+}
+
+# This is the example from pages 2 and 3 of RFC-7396
+db eval {
+  CREATE TABLE t1(j);
+  INSERT INTO t1(j) VALUES('{"a":1,"b":[1,[2,3],4],"c":99}');
+}
+proc json_extract_test {testnum path result} {
+  do_execsql_test json105-1.$testnum "SELECT quote(json_extract(j,$path)) FROM t1" $result
+}
+json_extract_test 10 {'$.b[#]'}   NULL
+json_extract_test 20 {'$.b[#-1]'} 4
+json_extract_test 30 {'$.b[#-2]'} {'[2,3]'}
+json_extract_test 31 {'$.b[#-02]'} {'[2,3]'}
+json_extract_test 40 {'$.b[#-3]'} 1
+json_extract_test 50 {'$.b[#-4]'} NULL
+json_extract_test 60 {'$.b[#-2][#-1]'} 3
+json_extract_test 70 {'$.b[0]','$.b[#-1]'} {'[1,4]'}
+
+json_extract_test 100 {'$.a[#-1]'} NULL
+json_extract_test 110 {'$.b[#-000001]'} 4
+
+proc json_remove_test {testnum path result} {
+  do_execsql_test json105-2.$testnum "SELECT quote(json_remove(j,$path)) FROM t1" $result
+}
+json_remove_test 10 {'$.b[#]'}    {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_remove_test 20 {'$.b[#-0]'}  {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_remove_test 30 {'$.b[#-1]'}  {'{"a":1,"b":[1,[2,3]],"c":99}'}
+json_remove_test 40 {'$.b[#-2]'}  {'{"a":1,"b":[1,4],"c":99}'}
+json_remove_test 50 {'$.b[#-3]'}  {'{"a":1,"b":[[2,3],4],"c":99}'}
+json_remove_test 60 {'$.b[#-4]'}  {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_remove_test 70 {'$.b[#-2][#-1]'}  {'{"a":1,"b":[1,[2],4],"c":99}'}
+
+json_remove_test 100 {'$.b[0]','$.b[#-1]'} {'{"a":1,"b":[[2,3]],"c":99}'}
+json_remove_test 110 {'$.b[#-1]','$.b[0]'} {'{"a":1,"b":[[2,3]],"c":99}'}
+json_remove_test 120 {'$.b[#-1]','$.b[#-2]'} {'{"a":1,"b":[[2,3]],"c":99}'}
+json_remove_test 130 {'$.b[#-1]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'}
+json_remove_test 140 {'$.b[#-2]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'}
+
+proc json_insert_test {testnum x result} {
+  do_execsql_test json105-3.$testnum "SELECT quote(json_insert(j,$x)) FROM t1" $result
+}
+json_insert_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'}
+json_insert_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'}
+json_insert_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \
+     {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'}
+json_insert_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \
+     {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'}
+
+proc json_set_test {testnum x result} {
+  do_execsql_test json105-4.$testnum "SELECT quote(json_set(j,$x)) FROM t1" $result
+}
+json_set_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'}
+json_set_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'}
+json_set_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \
+     {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'}
+json_set_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \
+     {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'}
+json_set_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'}
+json_set_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'}
+json_set_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \
+     {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'}
+json_set_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \
+     {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'}
+
+proc json_replace_test {testnum x result} {
+  do_execsql_test json105-5.$testnum "SELECT quote(json_replace(j,$x)) FROM t1" $result
+}
+json_replace_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_replace_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_replace_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \
+     {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_replace_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \
+     {'{"a":1,"b":[1,[2,3],4],"c":99}'}
+json_replace_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'}
+json_replace_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'}
+json_replace_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \
+     {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'}
+json_replace_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \
+     {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'}
+
+do_catchsql_test json105-6.10 {
+  SELECT json_extract(j, '$.b[#-]') FROM t1;
+} {1 {JSON path error near '[#-]'}}
+do_catchsql_test json105-6.20 {
+  SELECT json_extract(j, '$.b[#9]') FROM t1;
+} {1 {JSON path error near '[#9]'}}
+do_catchsql_test json105-6.30 {
+  SELECT json_extract(j, '$.b[#+2]') FROM t1;
+} {1 {JSON path error near '[#+2]'}}
+do_catchsql_test json105-6.40 {
+  SELECT json_extract(j, '$.b[#-1') FROM t1;
+} {1 {JSON path error near '[#-1'}}
+do_catchsql_test json105-6.50 {
+  SELECT json_extract(j, '$.b[#-1x]') FROM t1;
+} {1 {JSON path error near '[#-1x]'}}
+
+finish_test