]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Improvements to json_extract() to better support JSONB. Still not 100% working.
authordrh <>
Thu, 28 Sep 2023 15:56:35 +0000 (15:56 +0000)
committerdrh <>
Thu, 28 Sep 2023 15:56:35 +0000 (15:56 +0000)
FossilOrigin-Name: 8c82576176539c4d132b14d46adbf31366c4bcaa59a61dd639dc9cc308fe8825

manifest
manifest.uuid
src/json.c

index 0ab35841990c708f40f789c64c46e78e06c345bf..d05d5a4eac77c60a5efa08e88ceb7f5db73b157b 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Work\stoward\sgetting\sjson_extract()\sto\soperate\sdirectly\son\sthe\sBLOB,\somitting\nthe\stranslation\sinto\sa\sJsonNode\sarray.
-D 2023-09-28T10:20:56.267
+C Improvements\sto\sjson_extract()\sto\sbetter\ssupport\sJSONB.\sStill\snot\s100%\sworking.
+D 2023-09-28T15:56:35.608
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -670,7 +670,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 1f174d19f143e4968ae391de3d504563f320c6dbc2ba437231b40f19631a6dc4
+F src/json.c c9d70a47aabc97b51cd21a33be7602db4760a980920b44b497dbb331a91c12d8
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 98cfba10989b3da6f1807ad42444017742db7f100a54f1032af7a8b1295912c0
 F src/main.c 618aeb399e993cf561864f4b0cf6a331ee4f355cf663635f8d9da3193a46aa40
@@ -2123,11 +2123,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 7c1be8e361db87458ac9d8fcee080c2b558936539c852bb80f0f7941d61bf15d
-R b9d3bc6ee4123d23cc875353cfd5e9aa
-T *branch * jsonb-direct-extract
-T *sym-jsonb-direct-extract *
-T -sym-jsonb *
+P c1feba70f55a8e5f4696d48e4706855415d173ac8ac3c2656787c242a883b4f5
+R df67f115b3402f37b4410f24659e50ec
 U drh
-Z 3a4258d315e3af636c1707e0dba68e38
+Z a8b3beeec30b8fa8de8cb3c4ca40d323
 # Remove this line to create a well-formed Fossil manifest.
index 066de478dca963cdbebe91a448f6b7c1d14a7fe6..a7573f00b7c735a7b60a0723f143558adee18886 100644 (file)
@@ -1 +1 @@
-c1feba70f55a8e5f4696d48e4706855415d173ac8ac3c2656787c242a883b4f5
\ No newline at end of file
+8c82576176539c4d132b14d46adbf31366c4bcaa59a61dd639dc9cc308fe8825
\ No newline at end of file
index a6b91e76eb94a92d625fb3f71bf63e6822840a73..33c940410533eec5240e4fccfd691566225a69d2 100644 (file)
@@ -3634,7 +3634,34 @@ static u32 jsonLookupBlobStep(
 }
 
 /*
-** 
+** Convert a JSON BLOB into text and make that text the return value
+** of an SQL function.
+*/
+static void jsonReturnTextJsonFromBlob(
+  sqlite3_context *ctx,
+  const u8 *aBlob,
+  u32 nBlob
+){
+  JsonParse x;
+  JsonString s;
+
+  if( aBlob==0 ) return;
+  memset(&x, 0, sizeof(x));
+  x.aBlob = (u8*)aBlob;
+  x.nBlob = nBlob;
+  jsonInit(&s, ctx);
+  jsonRenderBlob(&x, 0, &s);
+  jsonResult(&s);
+}
+
+
+/*
+** Return the value of the BLOB node at index i.
+**
+** If the value is a primitive, return it as an SQL value.
+** If the value is an array or object, return it as either
+** JSON text or the BLOB encoding, depending on the JSON_B flag
+** on the userdata.
 */
 static void jsonReturnFromBlob(
   JsonParse *pParse,          /* Complete JSON parse tree */
@@ -3785,7 +3812,12 @@ static void jsonReturnFromBlob(
     }
     case JSONB_ARRAY:
     case JSONB_OBJECT: {
-      sqlite3_result_blob(pCtx, &pParse->aBlob[i+n], sz, SQLITE_TRANSIENT);
+      int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx));
+      if( flags & JSON_BLOB ){
+        sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT);
+      }else{
+        jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
+      }
       break;
     }
   }
@@ -3992,23 +4024,13 @@ static void jsonbTest2(
   int argc,
   sqlite3_value **argv
 ){
-  JsonParse *pParse;
   const u8 *aBlob;
   int nBlob;
-  JsonParse x;
-  JsonString s;
   UNUSED_PARAMETER(argc);
 
   aBlob = (const u8*)sqlite3_value_blob(argv[0]);
-  if( aBlob==0 ) return;
   nBlob = sqlite3_value_bytes(argv[0]);
-  pParse = &x;
-  memset(&x, 0, sizeof(x));
-  x.aBlob = (u8*)aBlob;
-  x.nBlob = nBlob;
-  jsonInit(&s, ctx);
-  jsonRenderBlob(pParse, 0, &s);
-  jsonResult(&s);
+  jsonReturnTextJsonFromBlob(ctx, aBlob, nBlob);
 }
 
 /*
@@ -5401,6 +5423,7 @@ void sqlite3RegisterJsonFunctions(void){
     JFUNCTION(json_array_length,  2, 0,                    jsonArrayLengthFunc),
     JFUNCTION(json_error_position,1, 0,                    jsonErrorFunc),
     JFUNCTION(json_extract,      -1, 0,                    jsonExtractFunc),
+    JFUNCTION(jsonb_extract,     -1, JSON_BLOB,            jsonExtractFunc),
     JFUNCTION(->,                 2, JSON_JSON,            jsonExtractFunc),
     JFUNCTION(->>,                2, JSON_SQL,             jsonExtractFunc),
     JFUNCTION(json_insert,       -1, 0,                    jsonSetFunc),