Enhance the path arguments in JSON functions to access "#-N" array indexes.

FossilOrigin-Name: ffeec62cb5223ae70bc1889df4c0b241f5d17b1a10156362ca59953028741480
This commit is contained in:
drh 2019-11-23 08:51:21 +00:00
commit 0f31327eee
4 changed files with 164 additions and 13 deletions

View 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(;;){

View File

@ -1,5 +1,5 @@
C Fix\sa\smemory\sleak\sin\sfts5\serror\shandling\scode.
D 2019-11-23T08:36:35.474
C Enhance\sthe\spath\sarguments\sin\sJSON\sfunctions\sto\saccess\s"#-N"\sarray\sindexes.
D 2019-11-23T08:51:21.228
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,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 34343c4b0657767f2676a2a9feb670cf000824e3eadfb1a6d87b57404191d8f9
R 647490f9972b5ad6b10dfe256dcbb8a2
U dan
Z 1cb9b1e092594c250aea1afbf779c5a9
P de6637e15d95b7aee2caea8b8d5b9865f276cbbf152ecf0401c4c7bfdf6dee6f 35ed68a651f4cf8740597433b0f1c3b345841868022e0904e9f342840ba0e502
R 0f33bb5fad7c4c628b306f509d5f962d
T +closed 35ed68a651f4cf8740597433b0f1c3b345841868022e0904e9f342840ba0e502
U drh
Z 604b4a7ce2ba7e5019d946836f03e6f7

View File

@ -1 +1 @@
de6637e15d95b7aee2caea8b8d5b9865f276cbbf152ecf0401c4c7bfdf6dee6f
ffeec62cb5223ae70bc1889df4c0b241f5d17b1a10156362ca59953028741480

118
test/json105.test Normal file
View File

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