]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Make edits directly to the JSONB BLOB when the input to json_replace()
authordrh <>
Tue, 21 Nov 2023 17:51:58 +0000 (17:51 +0000)
committerdrh <>
Tue, 21 Nov 2023 17:51:58 +0000 (17:51 +0000)
is a JSONB.

FossilOrigin-Name: d69c6acef54a81f46a97a05d443fe648635b4b70772069d6705ef829b718e985

manifest
manifest.uuid
src/json.c

index 5ede8a53d81e3e67be0d9f5818ce2d9217ec6db5..7339368a54b642522b4a5025df0a4c6aa0244c39 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\sthe\slatest\strunk\senhancements\sand\sfixes\sinto\sthe\sjsonb\sbranch.
-D 2023-11-17T17:03:45.388
+C Make\sedits\sdirectly\sto\sthe\sJSONB\sBLOB\swhen\sthe\sinput\sto\sjson_replace()\nis\sa\sJSONB.
+D 2023-11-21T17:51:58.335
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -685,7 +685,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 949c4a36244d402d1e5f9c1815d9ce2cf8e94cf27ce7419e2d1b9dd5a97a2baa
+F src/json.c 9be29c023ce9c839a507ddfd572dbca226c9707ba6b0714da004e00a7312a000
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9
@@ -2142,8 +2142,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 68d551730be0a3ea9579646ed4836c73554c83ca7f2303b69a18843f1750f1a7 ce6a75622ea5bca517bc6613e738aa670c9e1dd863596220eded5c2379c616c7
-R 3f0ae99fa66c7ee3590aa56d01ed6b36
+P 162f0509ef27bcd3ec87629640281a71c773e7c3bbd2cd0df76faf481531e7f1
+R 38409c37dc02439917dd59332bdf58d4
 U drh
-Z c061d55cfc7406269213a15ddaf22fe9
+Z 4892279bd4eeae014e8915fe6a430955
 # Remove this line to create a well-formed Fossil manifest.
index 475b441c306cbd42f00b37aeed96cb79f487ce11..4a62c55635b4d064248b10399b83bcd29acf317d 100644 (file)
@@ -1 +1 @@
-162f0509ef27bcd3ec87629640281a71c773e7c3bbd2cd0df76faf481531e7f1
\ No newline at end of file
+d69c6acef54a81f46a97a05d443fe648635b4b70772069d6705ef829b718e985
\ No newline at end of file
index 89109e00396c28c138fe4ed9b0eac952b2661b39..def5e63d307a2a3c7248392886173cd9e6bcf96b 100644 (file)
@@ -4304,6 +4304,143 @@ jsonRemoveFromBlob_patherror:
   return;
 }
 
+/*
+** pArg is a function argument that might be an SQL value or a JSON
+** value.  Figure out what it is and encode it as a JSONB blob.
+** Return the results in pParse.
+**
+** pParse is uninitialized upon entry.  This routine will handle the
+** initialization of pParse.  The result will be contained in
+** pParse->aBlob and pParse->nBlob.  pParse->aBlob might be dynamically
+** allocated (if pParse->nBlobAlloc is greater than zero) in which case
+** the caller is responsible for freeing the space allocated to pParse->aBlob
+** when it has finished with it.  Or pParse->aBlob might be a static string
+** or a value obtained from sqlite3_value_blob(pArg).
+**
+** If the argument is a BLOB that is clearly not a JSONB, then this
+** function might set an error message in ctx and return non-zero.
+** It might also set an error message and return non-zero on an OOM error.
+*/
+static int jsonFunctionArgToBlob(
+  sqlite3_context *ctx,
+  sqlite3_value *pArg,
+  JsonParse *pParse
+){
+  int eType = sqlite3_value_type(pArg);
+  static u8 aNull[] = { 0x00 };
+  memset(pParse, 0, sizeof(pParse[0]));
+  switch( eType ){
+    case SQLITE_NULL: {
+      pParse->aBlob = aNull;
+      pParse->nBlob = 1;
+      return 0;
+    }
+    case SQLITE_BLOB: {
+      if( jsonFuncArgMightBeBinary(pArg) ){
+        pParse->aBlob = (u8*)sqlite3_value_blob(pArg);
+        pParse->nBlob = sqlite3_value_bytes(pArg);
+      }else{
+        sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
+        return 1;
+      }
+      break;
+    }
+    case SQLITE_TEXT: {
+      const char *zJson = (const char*)sqlite3_value_text(pArg);
+      int nJson = sqlite3_value_bytes(pArg);
+      if( zJson==0 ) return 1;
+      if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){
+        pParse->zJson = (char*)zJson;
+        pParse->nJson = nJson;
+        if( jsonConvertTextToBlob(pParse, ctx) ){
+          sqlite3_result_error(ctx, "malformed JSON", -1);
+          sqlite3_free(pParse->aBlob);
+          memset(pParse, 0, sizeof(pParse[0]));
+          return 1;
+        }
+      }else{
+        jsonBlobAppendNodeType(pParse, JSONB_TEXTRAW, nJson);
+        jsonBlobAppendNBytes(pParse, (const u8*)zJson, nJson);
+      }
+      break;
+    }
+    case SQLITE_FLOAT:
+    case SQLITE_INTEGER: {
+      int n = sqlite3_value_bytes(pArg);
+      const char *z = (const char*)sqlite3_value_text(pArg);
+      int e = eType==SQLITE_INTEGER ? JSONB_INT : JSONB_FLOAT;
+      if( z==0 ) return 1;
+      jsonBlobAppendNodeType(pParse, e, n);
+      jsonBlobAppendNBytes(pParse, (const u8*)z, n);
+      break;
+    }
+  }
+  return 0;
+}
+/* argv[0] is a BLOB that seems likely to be a JSONB.  Subsequent
+** arguments come in parse where each pair contains a JSON path and
+** content to insert or set at that patch.  Do the updates
+** and return the result.
+**
+** The specific operation is determined by eEdit, which can be one
+** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
+*/
+static void jsonInsertIntoBlob(
+  sqlite3_context *ctx,
+  int argc,
+  sqlite3_value **argv,
+  int eEdit                /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
+){
+  int i;
+  u32 rc;
+  const char *zPath = 0;
+  int flgs;
+  JsonParse px, ax;
+
+  assert( (argc&1)==1 );
+  memset(&px, 0, sizeof(px));
+  px.nBlob = sqlite3_value_bytes(argv[0]);
+  px.aBlob = (u8*)sqlite3_value_blob(argv[0]);
+  if( px.aBlob==0 ) return;
+  for(i=1; i<argc-1; i+=2){
+    const char *zPath = (const char*)sqlite3_value_text(argv[i]);
+    if( zPath==0 ) goto jsonInsertIntoBlob_patherror;
+    if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
+    if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
+      break;
+    }
+    if( zPath[1]==0 ){
+      if( px.nBlobAlloc ) sqlite3_free(px.aBlob);
+      return;  /* return NULL if $ is removed */
+    }
+    px.eEdit = eEdit;
+    px.nIns = ax.nBlob;
+    px.aIns = ax.aBlob;
+    rc = jsonLookupBlobStep(&px, 0, zPath+1, 0);
+    if( ax.nBlobAlloc ) sqlite3_free(ax.aBlob);
+    if( rc==JSON_BLOB_NOTFOUND ) continue;
+    if( JSON_BLOB_ISERROR(rc) ) goto jsonInsertIntoBlob_patherror;
+  }
+  flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
+  if( flgs & JSON_BLOB ){
+    sqlite3_result_blob(ctx, px.aBlob, px.nBlob,
+                        px.nBlobAlloc>0 ? SQLITE_DYNAMIC : SQLITE_TRANSIENT);
+  }else{
+    JsonString s;
+    jsonStringInit(&s, ctx);
+    jsonXlateBlobToText(&px, 0, &s);
+    jsonReturnString(&s);
+    if( px.nBlobAlloc ) sqlite3_free(px.aBlob);
+  }
+  return;
+
+jsonInsertIntoBlob_patherror:
+  if( px.nBlobAlloc ) sqlite3_free(px.aBlob);
+  jsonPathSyntaxError(zPath, ctx);
+  return;
+}
+
 /****************************************************************************
 ** SQL functions used for testing and debugging
 ****************************************************************************/
@@ -4987,6 +5124,10 @@ static void jsonReplaceFunc(
     jsonWrongNumArgs(ctx, "replace");
     return;
   }
+  if( jsonFuncArgMightBeBinary(argv[0]) && argc>=3 ){
+    jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL);
+    return;
+  }
   pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
   if( pParse==0 ) return;
   pParse->nJPRef++;