]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Convert the json_array_length() function to use JSONB instead of JsonNodes.
authordrh <>
Wed, 29 Nov 2023 01:38:15 +0000 (01:38 +0000)
committerdrh <>
Wed, 29 Nov 2023 01:38:15 +0000 (01:38 +0000)
FossilOrigin-Name: 5ab790736d943e08f097efcee5cfbf0d83c65b0a53f273060330ba719affa5e5

manifest
manifest.uuid
src/json.c

index 8499e1f61f6dd0e96bfd70005952cf674847eda5..0134cc2e4a398f88d664f63c710b86f8b26f7373 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sall\sknown\sproblems\swith\sJSONB\sjson_extract().
-D 2023-11-28T23:26:55.773
+C Convert\sthe\sjson_array_length()\sfunction\sto\suse\sJSONB\sinstead\sof\sJsonNodes.
+D 2023-11-29T01:38:15.043
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -688,7 +688,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
-F src/json.c dd5c3f52877448cc9644887e83a2ff33ab0be87e158de7d5a86db12f90b28045
+F src/json.c aebeb6c1beea6a143223139e9b168b4187aff02c60a49daa6bcafadafcb6ee15
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9
@@ -2145,9 +2145,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 b69786e746ae2b927b64d9871fd120b7f8f06cc53739fd46a4da51aa16cf8576 4f106b64fe8988435872806bd0a6c223b61f53af0dd1c47c847bb4eec4e03e27
-R e440543ab70160e0cdb3ca23863ac507
-T +closed 4f106b64fe8988435872806bd0a6c223b61f53af0dd1c47c847bb4eec4e03e27
+P d5f48c57e975ac468cf29a43a5d0b56ef6d06cf35a8b0bddf87ec1c0fc7ae028
+R 4f247130b18247c9fbdce9034c7ad3a3
 U drh
-Z c2c6abd51345cdb21ffc83ec27ff2d5d
+Z 81615ca9c91f916f71f930ae045acac7
 # Remove this line to create a well-formed Fossil manifest.
index 3a99a8156d83cae35cabdb24e8197e6e43414289..539a333b70c6727526eeb4f580ad0ddc76a7fff4 100644 (file)
@@ -1 +1 @@
-d5f48c57e975ac468cf29a43a5d0b56ef6d06cf35a8b0bddf87ec1c0fc7ae028
\ No newline at end of file
+5ab790736d943e08f097efcee5cfbf0d83c65b0a53f273060330ba719affa5e5
\ No newline at end of file
index 1a2af6f8f164ba31802b4459e255ba26aa12b1ea..d210e5e98687bfc20026d2dfe4a53a94312afbaa 100644 (file)
@@ -4538,6 +4538,18 @@ static void jsonArrayFunc(
   sqlite3_result_subtype(ctx, JSON_SUBTYPE);
 }
 
+/*
+** Generate a bad path error for json_extract()
+*/
+static void jsonBadPathError(
+  sqlite3_context *ctx,     /* The function call containing the error */
+  const char *zPath         /* The path with the problem */
+){
+  sqlite3 *db = sqlite3_context_db_handle(ctx);
+  char *zMsg = sqlite3MPrintf(db, "bad JSON path: %Q", zPath);
+  sqlite3_result_error(ctx, zMsg, -1);
+  sqlite3DbFree(db, zMsg);
+}
 
 /*
 ** json_array_length(JSON)
@@ -4552,49 +4564,52 @@ static void jsonArrayLengthFunc(
   sqlite3_value **argv
 ){
   JsonParse *p;          /* The parse */
-  sqlite3_int64 n = 0;
+  sqlite3_int64 cnt = 0;
   u32 i;
-  JsonNode *pNode;
+  u8 eErr = 0;
 
-  p = jsonParseCached(ctx, argv[0], ctx, 0);
+  p = jsonParseFuncArg(ctx, argv[0], 0);
   if( p==0 ) return;
-  assert( p->nNode );
   if( argc==2 ){
     const char *zPath = (const char*)sqlite3_value_text(argv[1]);
-    pNode = jsonLookup(p, zPath, 0, ctx);
-  }else{
-    pNode = p->aNode;
-  }
-  if( pNode==0 ){
-    return;
-  }
-  if( pNode->eType==JSON_ARRAY ){
-    while( 1 /*exit-by-break*/ ){
-      i = 1;
-      while( i<=pNode->n ){
-        if( (pNode[i].jnFlags & JNODE_REMOVE)==0 ) n++;
-        i += jsonNodeSize(&pNode[i]);
+    if( zPath==0 ){
+      jsonParseFree(p);
+      return;
+    }
+    i = jsonLookupBlobStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
+    if( JSON_BLOB_ISERROR(i) ){
+      if( i==JSON_BLOB_NOTFOUND ){
+        /* no-op */
+      }else if( i==JSON_BLOB_PATHERROR ){
+        jsonBadPathError(ctx, zPath);
+      }else{
+        sqlite3_result_error(ctx, "malformed JSON", -1);
       }
-      if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
-      if( p->useMod==0 ) break;
-      assert( pNode->eU==2 );
-      pNode = &p->aNode[pNode->u.iAppend];
+      eErr = 1;
+      i = 0;
     }
+  }else{
+    i = 0;
   }
-  sqlite3_result_int64(ctx, n);
-}
-
-/*
-** Generate a bad path error for json_extract()
-*/
-static void jsonExtractBadPathError(
-  sqlite3_context *ctx,     /* The function call containing the error */
-  const char *zPath         /* The path with the problem */
-){
-  sqlite3 *db = sqlite3_context_db_handle(ctx);
-  char *zMsg = sqlite3MPrintf(db, "bad JSON path: %Q", zPath);
-  sqlite3_result_error(ctx, zMsg, -1);
-  sqlite3DbFree(db, zMsg);
+  if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY ){
+    u32 n, sz = 0, iEnd;
+    n = jsonbPayloadSize(p, i, &sz);
+    if( n==0 ) eErr = 2;
+    iEnd = i+n+sz;
+    i += n;
+    while( eErr==0 && i<iEnd ){
+      cnt++;
+      n = jsonbPayloadSize(p, i, &sz);
+      if( n==0 ) eErr = 2;
+      i += n+sz;
+    }
+  }
+  if( eErr ){
+    if( eErr==2 ) sqlite3_result_error(ctx, "malformed JSON", -1);
+  }else{
+    sqlite3_result_int64(ctx, cnt);
+  }
+  jsonParseFree(p);
 }
 
 /*
@@ -4668,7 +4683,7 @@ static void jsonExtractFunc(
       j = jsonLookupBlobStep(p, 0, jx.zBuf, 0);
       jsonStringReset(&jx);
     }else{
-      jsonExtractBadPathError(ctx, zPath);
+      jsonBadPathError(ctx, zPath);
       goto json_extract_error;
     }
     if( j<p->nBlob ){
@@ -4703,7 +4718,7 @@ static void jsonExtractFunc(
       sqlite3_result_error(ctx, "malformed JSON", -1);
       goto json_extract_error;
     }else{
-      jsonExtractBadPathError(ctx, zPath);
+      jsonBadPathError(ctx, zPath);
       goto json_extract_error;
     }
   }