blob: 26f08e9168d154ab5c0f38e3f576a342a4899921 [file] [log] [blame]
drh5fa5c102015-08-12 16:49:40 +00001/*
2** 2015-08-12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This SQLite extension implements JSON functions. The interface is
14** modeled after MySQL JSON functions:
15**
16** https://dev.mysql.com/doc/refman/5.7/en/json.html
17**
drh5634cc02015-08-17 11:28:03 +000018** For the time being, all JSON is stored as pure text. (We might add
19** a JSONB type in the future which stores a binary encoding of JSON in
drhcb6c6c62015-08-19 22:47:17 +000020** a BLOB, but there is no support for JSONB in the current implementation.
21** This implementation parses JSON text at 250 MB/s, so it is hard to see
22** how JSONB might improve on that.)
drh5fa5c102015-08-12 16:49:40 +000023*/
drh50065652015-10-08 19:29:18 +000024#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
drh666d34c2017-01-25 13:54:27 +000025#if !defined(SQLITEINT_H)
drh5fa5c102015-08-12 16:49:40 +000026#include "sqlite3ext.h"
drhf2df7e72015-08-28 20:07:40 +000027#endif
drh5fa5c102015-08-12 16:49:40 +000028SQLITE_EXTENSION_INIT1
drhf3c33c62021-12-20 23:46:44 +000029
30/* If compiling this extension separately (why would anybody do that when
31** it is built into the amalgamation?) we must set NDEBUG if SQLITE_DEBUG
32** is not defined *before* including <assert.h>, in order to disable asserts().
33*/
34#if !defined(SQLITE_AMALGAMATION) && !defined(SQLITE_DEBUG)
35# define NDEBUG 1
36#endif
37
drh5fa5c102015-08-12 16:49:40 +000038#include <assert.h>
39#include <string.h>
drh987eb1f2015-08-17 15:17:37 +000040#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000041#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000042
drhdf3a9072016-02-11 15:37:18 +000043/* Mark a function parameter as unused, to suppress nuisance compiler
44** warnings. */
45#ifndef UNUSED_PARAM
46# define UNUSED_PARAM(X) (void)(X)
47#endif
drh6fd5c1e2015-08-21 20:37:12 +000048
drh8deb4b82015-10-09 18:21:43 +000049#ifndef LARGEST_INT64
50# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
51# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
52#endif
53
drh08b92082020-08-10 14:18:00 +000054#ifndef deliberate_fall_through
55# define deliberate_fall_through
56#endif
57
dan2e8f5512015-09-17 17:21:09 +000058/*
59** Versions of isspace(), isalnum() and isdigit() to which it is safe
60** to pass signed char values.
61*/
drh49472652015-10-16 15:35:39 +000062#ifdef sqlite3Isdigit
63 /* Use the SQLite core versions if this routine is part of the
64 ** SQLite amalgamation */
drhad875e72016-11-07 13:37:28 +000065# define safe_isdigit(x) sqlite3Isdigit(x)
66# define safe_isalnum(x) sqlite3Isalnum(x)
67# define safe_isxdigit(x) sqlite3Isxdigit(x)
drh49472652015-10-16 15:35:39 +000068#else
69 /* Use the standard library for separate compilation */
70#include <ctype.h> /* amalgamator: keep */
drhad875e72016-11-07 13:37:28 +000071# define safe_isdigit(x) isdigit((unsigned char)(x))
72# define safe_isalnum(x) isalnum((unsigned char)(x))
73# define safe_isxdigit(x) isxdigit((unsigned char)(x))
drh49472652015-10-16 15:35:39 +000074#endif
dan2e8f5512015-09-17 17:21:09 +000075
drh95677942015-09-24 01:06:37 +000076/*
77** Growing our own isspace() routine this way is twice as fast as
78** the library isspace() function, resulting in a 7% overall performance
79** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
80*/
81static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000082 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98};
99#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
100
drh9a4718f2015-10-10 14:00:37 +0000101#ifndef SQLITE_AMALGAMATION
102 /* Unsigned integer types. These are already defined in the sqliteInt.h,
103 ** but the definitions need to be repeated for separate compilation. */
104 typedef sqlite3_uint64 u64;
105 typedef unsigned int u32;
drhff6d50e2017-04-11 18:55:05 +0000106 typedef unsigned short int u16;
drh9a4718f2015-10-10 14:00:37 +0000107 typedef unsigned char u8;
drh6a726fa2021-10-05 15:30:52 +0000108# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
109# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
110# endif
111# if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
112# define ALWAYS(X) (1)
113# define NEVER(X) (0)
114# elif !defined(NDEBUG)
115# define ALWAYS(X) ((X)?1:(assert(0),0))
116# define NEVER(X) ((X)?(assert(0),1):0)
117# else
118# define ALWAYS(X) (X)
119# define NEVER(X) (X)
120# endif
drh285f2ef2021-10-15 16:15:04 +0000121# define testcase(X)
drh9a4718f2015-10-10 14:00:37 +0000122#endif
drh16fc5e62021-11-01 12:53:01 +0000123#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
drh285f2ef2021-10-15 16:15:04 +0000124# define VVA(X)
125#else
126# define VVA(X) X
127#endif
128
129/*
130** Some of the testcase() macros in this file are problematic for gcov
131** in that they generate false-miss errors randomly. This is a gcov problem,
132** not a problem in this case. But to work around it, we disable the
133** problematic test cases for production builds.
134*/
135#define json_testcase(X)
drh5fa5c102015-08-12 16:49:40 +0000136
drh52216ad2015-08-18 02:28:03 +0000137/* Objects */
drh505ad2c2015-08-21 17:33:11 +0000138typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +0000139typedef struct JsonNode JsonNode;
140typedef struct JsonParse JsonParse;
141
drh5634cc02015-08-17 11:28:03 +0000142/* An instance of this object represents a JSON string
143** under construction. Really, this is a generic string accumulator
144** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000145*/
drh505ad2c2015-08-21 17:33:11 +0000146struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000147 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000148 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000149 u64 nAlloc; /* Bytes of storage available in zBuf[] */
150 u64 nUsed; /* Bytes of zBuf[] currently used */
151 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000152 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000153 char zSpace[100]; /* Initial static space */
154};
155
drhe9c37f32015-08-15 21:25:36 +0000156/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000157*/
drhe9c37f32015-08-15 21:25:36 +0000158#define JSON_NULL 0
159#define JSON_TRUE 1
160#define JSON_FALSE 2
161#define JSON_INT 3
162#define JSON_REAL 4
163#define JSON_STRING 5
164#define JSON_ARRAY 6
165#define JSON_OBJECT 7
166
drhf5ddb9c2015-09-11 00:06:41 +0000167/* The "subtype" set for JSON values */
168#define JSON_SUBTYPE 74 /* Ascii for "J" */
169
drh987eb1f2015-08-17 15:17:37 +0000170/*
171** Names of the various JSON types:
172*/
173static const char * const jsonType[] = {
174 "null", "true", "false", "integer", "real", "text", "array", "object"
175};
176
drh301eecc2015-08-17 20:14:19 +0000177/* Bit values for the JsonNode.jnFlag field
178*/
179#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
180#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
181#define JNODE_REMOVE 0x04 /* Do not output */
drh633647a2017-03-22 21:24:31 +0000182#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
183#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
184#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
185#define JNODE_LABEL 0x40 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000186
drh987eb1f2015-08-17 15:17:37 +0000187
drhe9c37f32015-08-15 21:25:36 +0000188/* A single node of parsed JSON
189*/
drhe9c37f32015-08-15 21:25:36 +0000190struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000191 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000192 u8 jnFlags; /* JNODE flags */
drh285f2ef2021-10-15 16:15:04 +0000193 u8 eU; /* Which union element to use */
drhe9c37f32015-08-15 21:25:36 +0000194 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000195 union {
drh285f2ef2021-10-15 16:15:04 +0000196 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
197 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
198 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
199 u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
200 JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
drh52216ad2015-08-18 02:28:03 +0000201 } u;
drhe9c37f32015-08-15 21:25:36 +0000202};
203
204/* A completely parsed JSON string
205*/
drhe9c37f32015-08-15 21:25:36 +0000206struct JsonParse {
207 u32 nNode; /* Number of slots of aNode[] used */
208 u32 nAlloc; /* Number of slots of aNode[] allocated */
209 JsonNode *aNode; /* Array of nodes containing the parse */
210 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000211 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000212 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000213 u8 nErr; /* Number of errors seen */
drhff6d50e2017-04-11 18:55:05 +0000214 u16 iDepth; /* Nesting depth */
drh3fb153c2017-05-11 16:49:59 +0000215 int nJson; /* Length of the zJson string in bytes */
drhe35fc302018-08-30 01:52:10 +0000216 u32 iHold; /* Replace cache line with the lowest iHold value */
drhe9c37f32015-08-15 21:25:36 +0000217};
218
drhff6d50e2017-04-11 18:55:05 +0000219/*
220** Maximum nesting depth of JSON for this implementation.
221**
222** This limit is needed to avoid a stack overflow in the recursive
223** descent parser. A depth of 2000 is far deeper than any sane JSON
224** should go.
225*/
226#define JSON_MAX_DEPTH 2000
227
drh505ad2c2015-08-21 17:33:11 +0000228/**************************************************************************
229** Utility routines for dealing with JsonString objects
230**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000231
drh505ad2c2015-08-21 17:33:11 +0000232/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000233*/
drh505ad2c2015-08-21 17:33:11 +0000234static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000235 p->zBuf = p->zSpace;
236 p->nAlloc = sizeof(p->zSpace);
237 p->nUsed = 0;
238 p->bStatic = 1;
239}
240
drh505ad2c2015-08-21 17:33:11 +0000241/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000242*/
drh505ad2c2015-08-21 17:33:11 +0000243static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000244 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000245 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000246 jsonZero(p);
247}
248
249
drh505ad2c2015-08-21 17:33:11 +0000250/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000251** initial state.
252*/
drh505ad2c2015-08-21 17:33:11 +0000253static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000254 if( !p->bStatic ) sqlite3_free(p->zBuf);
255 jsonZero(p);
256}
257
258
259/* Report an out-of-memory (OOM) condition
260*/
drh505ad2c2015-08-21 17:33:11 +0000261static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000262 p->bErr = 1;
263 sqlite3_result_error_nomem(p->pCtx);
264 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000265}
266
267/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
268** Return zero on success. Return non-zero on an OOM error
269*/
drh505ad2c2015-08-21 17:33:11 +0000270static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000271 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000272 char *zNew;
273 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000274 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000275 zNew = sqlite3_malloc64(nTotal);
276 if( zNew==0 ){
277 jsonOom(p);
278 return SQLITE_NOMEM;
279 }
drh6fd5c1e2015-08-21 20:37:12 +0000280 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000281 p->zBuf = zNew;
282 p->bStatic = 0;
283 }else{
284 zNew = sqlite3_realloc64(p->zBuf, nTotal);
285 if( zNew==0 ){
286 jsonOom(p);
287 return SQLITE_NOMEM;
288 }
289 p->zBuf = zNew;
290 }
291 p->nAlloc = nTotal;
292 return SQLITE_OK;
293}
294
drh505ad2c2015-08-21 17:33:11 +0000295/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000296*/
drh505ad2c2015-08-21 17:33:11 +0000297static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drhc795e3d2020-05-17 13:47:28 +0000298 if( N==0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000299 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
300 memcpy(p->zBuf+p->nUsed, zIn, N);
301 p->nUsed += N;
302}
303
drh4af352d2015-08-21 20:02:48 +0000304/* Append formatted text (not to exceed N bytes) to the JsonString.
305*/
306static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
307 va_list ap;
308 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
309 va_start(ap, zFormat);
310 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
311 va_end(ap);
312 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
313}
314
drh5634cc02015-08-17 11:28:03 +0000315/* Append a single character
316*/
drh505ad2c2015-08-21 17:33:11 +0000317static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000318 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
319 p->zBuf[p->nUsed++] = c;
320}
321
drh301eecc2015-08-17 20:14:19 +0000322/* Append a comma separator to the output buffer, if the previous
323** character is not '[' or '{'.
324*/
drh505ad2c2015-08-21 17:33:11 +0000325static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000326 char c;
327 if( p->nUsed==0 ) return;
328 c = p->zBuf[p->nUsed-1];
329 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
330}
331
drh505ad2c2015-08-21 17:33:11 +0000332/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000333** under construction. Enclose the string in "..." and escape
334** any double-quotes or backslash characters contained within the
335** string.
336*/
drh505ad2c2015-08-21 17:33:11 +0000337static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000338 u32 i;
drh76baad92021-04-30 16:12:40 +0000339 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
drh5fa5c102015-08-12 16:49:40 +0000340 p->zBuf[p->nUsed++] = '"';
341 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000342 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000343 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000344 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000345 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000346 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000347 }else if( c<=0x1f ){
348 static const char aSpecial[] = {
349 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
351 };
352 assert( sizeof(aSpecial)==32 );
353 assert( aSpecial['\b']=='b' );
354 assert( aSpecial['\f']=='f' );
355 assert( aSpecial['\n']=='n' );
356 assert( aSpecial['\r']=='r' );
357 assert( aSpecial['\t']=='t' );
358 if( aSpecial[c] ){
359 c = aSpecial[c];
360 goto json_simple_escape;
361 }
362 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
363 p->zBuf[p->nUsed++] = '\\';
364 p->zBuf[p->nUsed++] = 'u';
365 p->zBuf[p->nUsed++] = '0';
366 p->zBuf[p->nUsed++] = '0';
367 p->zBuf[p->nUsed++] = '0' + (c>>4);
368 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000369 }
370 p->zBuf[p->nUsed++] = c;
371 }
372 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000373 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000374}
375
drhd0960592015-08-17 21:22:32 +0000376/*
377** Append a function parameter value to the JSON string under
378** construction.
379*/
380static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000381 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000382 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000383){
384 switch( sqlite3_value_type(pValue) ){
385 case SQLITE_NULL: {
386 jsonAppendRaw(p, "null", 4);
387 break;
388 }
389 case SQLITE_INTEGER:
390 case SQLITE_FLOAT: {
391 const char *z = (const char*)sqlite3_value_text(pValue);
392 u32 n = (u32)sqlite3_value_bytes(pValue);
393 jsonAppendRaw(p, z, n);
394 break;
395 }
396 case SQLITE_TEXT: {
397 const char *z = (const char*)sqlite3_value_text(pValue);
398 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000399 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000400 jsonAppendRaw(p, z, n);
401 }else{
402 jsonAppendString(p, z, n);
403 }
drhd0960592015-08-17 21:22:32 +0000404 break;
405 }
406 default: {
407 if( p->bErr==0 ){
408 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000409 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000410 jsonReset(p);
411 }
412 break;
413 }
414 }
415}
416
417
drhbd0621b2015-08-13 13:54:59 +0000418/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000419*/
drh505ad2c2015-08-21 17:33:11 +0000420static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000421 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000422 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
423 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
424 SQLITE_UTF8);
425 jsonZero(p);
426 }
427 assert( p->bStatic );
428}
429
drh505ad2c2015-08-21 17:33:11 +0000430/**************************************************************************
431** Utility routines for dealing with JsonNode and JsonParse objects
432**************************************************************************/
433
434/*
435** Return the number of consecutive JsonNode slots need to represent
436** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
437** OBJECT types, the number might be larger.
438**
439** Appended elements are not counted. The value returned is the number
440** by which the JsonNode counter should increment in order to go to the
441** next peer value.
442*/
443static u32 jsonNodeSize(JsonNode *pNode){
444 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
445}
446
447/*
448** Reclaim all memory allocated by a JsonParse object. But do not
449** delete the JsonParse object itself.
450*/
451static void jsonParseReset(JsonParse *pParse){
452 sqlite3_free(pParse->aNode);
453 pParse->aNode = 0;
454 pParse->nNode = 0;
455 pParse->nAlloc = 0;
456 sqlite3_free(pParse->aUp);
457 pParse->aUp = 0;
458}
459
drh5634cc02015-08-17 11:28:03 +0000460/*
drh3fb153c2017-05-11 16:49:59 +0000461** Free a JsonParse object that was obtained from sqlite3_malloc().
462*/
463static void jsonParseFree(JsonParse *pParse){
464 jsonParseReset(pParse);
465 sqlite3_free(pParse);
466}
467
468/*
drh5634cc02015-08-17 11:28:03 +0000469** Convert the JsonNode pNode into a pure JSON string and
470** append to pOut. Subsubstructure is also included. Return
471** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000472*/
drh52216ad2015-08-18 02:28:03 +0000473static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000474 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000475 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000476 sqlite3_value **aReplace /* Replacement values */
477){
drh7d4c94b2021-10-04 22:34:38 +0000478 assert( pNode!=0 );
drh633647a2017-03-22 21:24:31 +0000479 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
drh7d4c94b2021-10-04 22:34:38 +0000480 if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){
drh285f2ef2021-10-15 16:15:04 +0000481 assert( pNode->eU==4 );
drh633647a2017-03-22 21:24:31 +0000482 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
483 return;
484 }
drh285f2ef2021-10-15 16:15:04 +0000485 assert( pNode->eU==5 );
drh633647a2017-03-22 21:24:31 +0000486 pNode = pNode->u.pPatch;
487 }
drh5634cc02015-08-17 11:28:03 +0000488 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000489 default: {
490 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000491 jsonAppendRaw(pOut, "null", 4);
492 break;
493 }
494 case JSON_TRUE: {
495 jsonAppendRaw(pOut, "true", 4);
496 break;
497 }
498 case JSON_FALSE: {
499 jsonAppendRaw(pOut, "false", 5);
500 break;
501 }
502 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000503 if( pNode->jnFlags & JNODE_RAW ){
drh285f2ef2021-10-15 16:15:04 +0000504 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000505 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000506 break;
507 }
drh08b92082020-08-10 14:18:00 +0000508 /* no break */ deliberate_fall_through
drh5634cc02015-08-17 11:28:03 +0000509 }
510 case JSON_REAL:
511 case JSON_INT: {
drh285f2ef2021-10-15 16:15:04 +0000512 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000513 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000514 break;
515 }
516 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000517 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000518 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000519 for(;;){
520 while( j<=pNode->n ){
drh633647a2017-03-22 21:24:31 +0000521 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +0000522 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000523 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000524 }
drh505ad2c2015-08-21 17:33:11 +0000525 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000526 }
drh52216ad2015-08-18 02:28:03 +0000527 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +0000528 assert( pNode->eU==2 );
drh52216ad2015-08-18 02:28:03 +0000529 pNode = &pNode[pNode->u.iAppend];
530 j = 1;
drh5634cc02015-08-17 11:28:03 +0000531 }
532 jsonAppendChar(pOut, ']');
533 break;
534 }
535 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000536 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000537 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000538 for(;;){
539 while( j<=pNode->n ){
540 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
541 jsonAppendSeparator(pOut);
542 jsonRenderNode(&pNode[j], pOut, aReplace);
543 jsonAppendChar(pOut, ':');
drh633647a2017-03-22 21:24:31 +0000544 jsonRenderNode(&pNode[j+1], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000545 }
drh505ad2c2015-08-21 17:33:11 +0000546 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000547 }
drh52216ad2015-08-18 02:28:03 +0000548 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +0000549 assert( pNode->eU==2 );
drh52216ad2015-08-18 02:28:03 +0000550 pNode = &pNode[pNode->u.iAppend];
551 j = 1;
drh5634cc02015-08-17 11:28:03 +0000552 }
553 jsonAppendChar(pOut, '}');
554 break;
555 }
drhbd0621b2015-08-13 13:54:59 +0000556 }
drh5634cc02015-08-17 11:28:03 +0000557}
558
559/*
drhf2df7e72015-08-28 20:07:40 +0000560** Return a JsonNode and all its descendents as a JSON string.
561*/
562static void jsonReturnJson(
563 JsonNode *pNode, /* Node to return */
564 sqlite3_context *pCtx, /* Return value for this function */
565 sqlite3_value **aReplace /* Array of replacement values */
566){
567 JsonString s;
568 jsonInit(&s, pCtx);
569 jsonRenderNode(pNode, &s, aReplace);
570 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000571 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000572}
573
574/*
drh48eb03b2019-11-10 11:09:06 +0000575** Translate a single byte of Hex into an integer.
576** This routine only works if h really is a valid hexadecimal
577** character: 0..9a..fA..F
578*/
579static u8 jsonHexToInt(int h){
580 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
581#ifdef SQLITE_EBCDIC
582 h += 9*(1&~(h>>4));
583#else
584 h += 9*(1&(h>>6));
585#endif
586 return (u8)(h & 0xf);
587}
588
589/*
590** Convert a 4-byte hex string into an integer
591*/
592static u32 jsonHexToInt4(const char *z){
593 u32 v;
594 assert( safe_isxdigit(z[0]) );
595 assert( safe_isxdigit(z[1]) );
596 assert( safe_isxdigit(z[2]) );
597 assert( safe_isxdigit(z[3]) );
598 v = (jsonHexToInt(z[0])<<12)
599 + (jsonHexToInt(z[1])<<8)
600 + (jsonHexToInt(z[2])<<4)
601 + jsonHexToInt(z[3]);
602 return v;
603}
604
605/*
drh5634cc02015-08-17 11:28:03 +0000606** Make the JsonNode the return value of the function.
607*/
drhd0960592015-08-17 21:22:32 +0000608static void jsonReturn(
609 JsonNode *pNode, /* Node to return */
610 sqlite3_context *pCtx, /* Return value for this function */
611 sqlite3_value **aReplace /* Array of replacement values */
612){
drh5634cc02015-08-17 11:28:03 +0000613 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000614 default: {
615 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000616 sqlite3_result_null(pCtx);
617 break;
618 }
619 case JSON_TRUE: {
620 sqlite3_result_int(pCtx, 1);
621 break;
622 }
623 case JSON_FALSE: {
624 sqlite3_result_int(pCtx, 0);
625 break;
626 }
drh987eb1f2015-08-17 15:17:37 +0000627 case JSON_INT: {
628 sqlite3_int64 i = 0;
drh285f2ef2021-10-15 16:15:04 +0000629 const char *z;
630 assert( pNode->eU==1 );
631 z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000632 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000633 while( z[0]>='0' && z[0]<='9' ){
634 unsigned v = *(z++) - '0';
635 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000636 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000637 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
638 if( v==9 ) goto int_as_real;
639 if( v==8 ){
640 if( pNode->u.zJContent[0]=='-' ){
641 sqlite3_result_int64(pCtx, SMALLEST_INT64);
642 goto int_done;
643 }else{
644 goto int_as_real;
645 }
646 }
647 }
648 i = i*10 + v;
649 }
drh52216ad2015-08-18 02:28:03 +0000650 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000651 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000652 int_done:
653 break;
drhe85e1da2021-10-01 21:01:07 +0000654 int_as_real: ; /* no break */ deliberate_fall_through
drh8deb4b82015-10-09 18:21:43 +0000655 }
656 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000657 double r;
658#ifdef SQLITE_AMALGAMATION
drh285f2ef2021-10-15 16:15:04 +0000659 const char *z;
660 assert( pNode->eU==1 );
661 z = pNode->u.zJContent;
drh49472652015-10-16 15:35:39 +0000662 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
663#else
drh285f2ef2021-10-15 16:15:04 +0000664 assert( pNode->eU==1 );
drh49472652015-10-16 15:35:39 +0000665 r = strtod(pNode->u.zJContent, 0);
666#endif
drh8deb4b82015-10-09 18:21:43 +0000667 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000668 break;
669 }
drh5634cc02015-08-17 11:28:03 +0000670 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000671#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
672 ** json_insert() and json_replace() and those routines do not
673 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000674 if( pNode->jnFlags & JNODE_RAW ){
drh285f2ef2021-10-15 16:15:04 +0000675 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000676 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
677 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000678 }else
679#endif
680 assert( (pNode->jnFlags & JNODE_RAW)==0 );
681 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000682 /* JSON formatted without any backslash-escapes */
drh285f2ef2021-10-15 16:15:04 +0000683 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000684 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000685 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000686 }else{
687 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000688 u32 i;
689 u32 n = pNode->n;
drh285f2ef2021-10-15 16:15:04 +0000690 const char *z;
drh987eb1f2015-08-17 15:17:37 +0000691 char *zOut;
692 u32 j;
drh285f2ef2021-10-15 16:15:04 +0000693 assert( pNode->eU==1 );
694 z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000695 zOut = sqlite3_malloc( n+1 );
696 if( zOut==0 ){
697 sqlite3_result_error_nomem(pCtx);
698 break;
699 }
700 for(i=1, j=0; i<n-1; i++){
701 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000702 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000703 zOut[j++] = c;
704 }else{
705 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000706 if( c=='u' ){
drh48eb03b2019-11-10 11:09:06 +0000707 u32 v = jsonHexToInt4(z+i+1);
708 i += 4;
drh80d87402015-08-24 12:42:41 +0000709 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000710 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000711 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000712 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000713 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000714 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000715 }else{
drh48eb03b2019-11-10 11:09:06 +0000716 u32 vlo;
717 if( (v&0xfc00)==0xd800
718 && i<n-6
719 && z[i+1]=='\\'
720 && z[i+2]=='u'
721 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
722 ){
723 /* We have a surrogate pair */
724 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
725 i += 6;
726 zOut[j++] = 0xf0 | (v>>18);
727 zOut[j++] = 0x80 | ((v>>12)&0x3f);
728 zOut[j++] = 0x80 | ((v>>6)&0x3f);
729 zOut[j++] = 0x80 | (v&0x3f);
730 }else{
731 zOut[j++] = 0xe0 | (v>>12);
732 zOut[j++] = 0x80 | ((v>>6)&0x3f);
733 zOut[j++] = 0x80 | (v&0x3f);
734 }
drh987eb1f2015-08-17 15:17:37 +0000735 }
736 }else{
737 if( c=='b' ){
738 c = '\b';
739 }else if( c=='f' ){
740 c = '\f';
741 }else if( c=='n' ){
742 c = '\n';
743 }else if( c=='r' ){
744 c = '\r';
745 }else if( c=='t' ){
746 c = '\t';
747 }
748 zOut[j++] = c;
749 }
750 }
751 }
752 zOut[j] = 0;
753 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000754 }
755 break;
756 }
757 case JSON_ARRAY:
758 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000759 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000760 break;
761 }
762 }
drhbd0621b2015-08-13 13:54:59 +0000763}
764
drh95677942015-09-24 01:06:37 +0000765/* Forward reference */
766static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
767
768/*
769** A macro to hint to the compiler that a function should not be
770** inlined.
771*/
772#if defined(__GNUC__)
773# define JSON_NOINLINE __attribute__((noinline))
774#elif defined(_MSC_VER) && _MSC_VER>=1310
775# define JSON_NOINLINE __declspec(noinline)
776#else
777# define JSON_NOINLINE
778#endif
779
780
781static JSON_NOINLINE int jsonParseAddNodeExpand(
782 JsonParse *pParse, /* Append the node to this object */
783 u32 eType, /* Node type */
784 u32 n, /* Content size or sub-node count */
785 const char *zContent /* Content */
786){
787 u32 nNew;
788 JsonNode *pNew;
789 assert( pParse->nNode>=pParse->nAlloc );
790 if( pParse->oom ) return -1;
791 nNew = pParse->nAlloc*2 + 10;
drh2d77d802019-01-08 20:02:48 +0000792 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
drh95677942015-09-24 01:06:37 +0000793 if( pNew==0 ){
794 pParse->oom = 1;
795 return -1;
796 }
797 pParse->nAlloc = nNew;
798 pParse->aNode = pNew;
799 assert( pParse->nNode<pParse->nAlloc );
800 return jsonParseAddNode(pParse, eType, n, zContent);
801}
802
drh5fa5c102015-08-12 16:49:40 +0000803/*
drhe9c37f32015-08-15 21:25:36 +0000804** Create a new JsonNode instance based on the arguments and append that
805** instance to the JsonParse. Return the index in pParse->aNode[] of the
806** new node, or -1 if a memory allocation fails.
807*/
808static int jsonParseAddNode(
809 JsonParse *pParse, /* Append the node to this object */
810 u32 eType, /* Node type */
811 u32 n, /* Content size or sub-node count */
812 const char *zContent /* Content */
813){
814 JsonNode *p;
drhaa6fe5b2021-10-04 13:18:44 +0000815 if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000816 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000817 }
818 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000819 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000820 p->jnFlags = 0;
drh285f2ef2021-10-15 16:15:04 +0000821 VVA( p->eU = zContent ? 1 : 0 );
drhe9c37f32015-08-15 21:25:36 +0000822 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000823 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000824 return pParse->nNode++;
825}
826
827/*
drhad875e72016-11-07 13:37:28 +0000828** Return true if z[] begins with 4 (or more) hexadecimal digits
829*/
830static int jsonIs4Hex(const char *z){
831 int i;
832 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
833 return 1;
834}
835
836/*
drhe9c37f32015-08-15 21:25:36 +0000837** Parse a single JSON value which begins at pParse->zJson[i]. Return the
838** index of the first character past the end of the value parsed.
839**
840** Return negative for a syntax error. Special cases: return -2 if the
841** first non-whitespace character is '}' and return -3 if the first
842** non-whitespace character is ']'.
843*/
844static int jsonParseValue(JsonParse *pParse, u32 i){
845 char c;
846 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000847 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000848 int x;
drh852944e2015-09-10 03:29:11 +0000849 JsonNode *pNode;
drh9fa866a2017-04-08 18:18:22 +0000850 const char *z = pParse->zJson;
851 while( safe_isspace(z[i]) ){ i++; }
852 if( (c = z[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000853 /* Parse object */
854 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000855 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000856 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000857 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000858 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000859 x = jsonParseValue(pParse, j);
860 if( x<0 ){
drhff6d50e2017-04-11 18:55:05 +0000861 pParse->iDepth--;
drhf27cd1f2015-09-23 01:10:29 +0000862 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000863 return -1;
864 }
drhbe9474e2015-08-22 03:05:54 +0000865 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000866 pNode = &pParse->aNode[pParse->nNode-1];
867 if( pNode->eType!=JSON_STRING ) return -1;
868 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000869 j = x;
drh9fa866a2017-04-08 18:18:22 +0000870 while( safe_isspace(z[j]) ){ j++; }
871 if( z[j]!=':' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000872 j++;
873 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000874 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000875 if( x<0 ) return -1;
876 j = x;
drh9fa866a2017-04-08 18:18:22 +0000877 while( safe_isspace(z[j]) ){ j++; }
878 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000879 if( c==',' ) continue;
880 if( c!='}' ) return -1;
881 break;
882 }
drhbc8f0922015-08-22 19:39:04 +0000883 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000884 return j+1;
885 }else if( c=='[' ){
886 /* Parse array */
887 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000888 if( iThis<0 ) return -1;
drh285f2ef2021-10-15 16:15:04 +0000889 memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
drhe9c37f32015-08-15 21:25:36 +0000890 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000891 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000892 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000893 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000894 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000895 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000896 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000897 return -1;
898 }
899 j = x;
drh9fa866a2017-04-08 18:18:22 +0000900 while( safe_isspace(z[j]) ){ j++; }
901 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000902 if( c==',' ) continue;
903 if( c!=']' ) return -1;
904 break;
905 }
drhbc8f0922015-08-22 19:39:04 +0000906 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000907 return j+1;
908 }else if( c=='"' ){
909 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000910 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000911 j = i+1;
912 for(;;){
drh9fa866a2017-04-08 18:18:22 +0000913 c = z[j];
drh86715382017-04-13 00:12:32 +0000914 if( (c & ~0x1f)==0 ){
915 /* Control characters are not allowed in strings */
916 return -1;
917 }
drhe9c37f32015-08-15 21:25:36 +0000918 if( c=='\\' ){
drh9fa866a2017-04-08 18:18:22 +0000919 c = z[++j];
drhad875e72016-11-07 13:37:28 +0000920 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
921 || c=='n' || c=='r' || c=='t'
drh9fa866a2017-04-08 18:18:22 +0000922 || (c=='u' && jsonIs4Hex(z+j+1)) ){
drhad875e72016-11-07 13:37:28 +0000923 jnFlags = JNODE_ESCAPE;
924 }else{
925 return -1;
926 }
drhe9c37f32015-08-15 21:25:36 +0000927 }else if( c=='"' ){
928 break;
929 }
930 j++;
931 }
drh9fa866a2017-04-08 18:18:22 +0000932 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
drhbe9474e2015-08-22 03:05:54 +0000933 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000934 return j+1;
935 }else if( c=='n'
drh9fa866a2017-04-08 18:18:22 +0000936 && strncmp(z+i,"null",4)==0
937 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000938 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
939 return i+4;
940 }else if( c=='t'
drh9fa866a2017-04-08 18:18:22 +0000941 && strncmp(z+i,"true",4)==0
942 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000943 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
944 return i+4;
945 }else if( c=='f'
drh9fa866a2017-04-08 18:18:22 +0000946 && strncmp(z+i,"false",5)==0
947 && !safe_isalnum(z[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000948 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
949 return i+5;
950 }else if( c=='-' || (c>='0' && c<='9') ){
951 /* Parse number */
952 u8 seenDP = 0;
953 u8 seenE = 0;
drh9fa866a2017-04-08 18:18:22 +0000954 assert( '-' < '0' );
955 if( c<='0' ){
956 j = c=='-' ? i+1 : i;
957 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
958 }
drhe9c37f32015-08-15 21:25:36 +0000959 j = i+1;
960 for(;; j++){
drh9fa866a2017-04-08 18:18:22 +0000961 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000962 if( c>='0' && c<='9' ) continue;
963 if( c=='.' ){
drh9fa866a2017-04-08 18:18:22 +0000964 if( z[j-1]=='-' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000965 if( seenDP ) return -1;
966 seenDP = 1;
967 continue;
968 }
969 if( c=='e' || c=='E' ){
drh9fa866a2017-04-08 18:18:22 +0000970 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000971 if( seenE ) return -1;
972 seenDP = seenE = 1;
drh9fa866a2017-04-08 18:18:22 +0000973 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000974 if( c=='+' || c=='-' ){
975 j++;
drh9fa866a2017-04-08 18:18:22 +0000976 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000977 }
drhd1f00682015-08-29 16:02:37 +0000978 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000979 continue;
980 }
981 break;
982 }
drh9fa866a2017-04-08 18:18:22 +0000983 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000984 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
drh9fa866a2017-04-08 18:18:22 +0000985 j - i, &z[i]);
drhe9c37f32015-08-15 21:25:36 +0000986 return j;
987 }else if( c=='}' ){
988 return -2; /* End of {...} */
989 }else if( c==']' ){
990 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000991 }else if( c==0 ){
992 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000993 }else{
994 return -1; /* Syntax error */
995 }
996}
997
998/*
999** Parse a complete JSON string. Return 0 on success or non-zero if there
1000** are any errors. If an error occurs, free all memory associated with
1001** pParse.
1002**
1003** pParse is uninitialized when this routine is called.
1004*/
drhbc8f0922015-08-22 19:39:04 +00001005static int jsonParse(
1006 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1007 sqlite3_context *pCtx, /* Report errors here */
1008 const char *zJson /* Input JSON text to be parsed */
1009){
drhe9c37f32015-08-15 21:25:36 +00001010 int i;
drhe9c37f32015-08-15 21:25:36 +00001011 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +00001012 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +00001013 pParse->zJson = zJson;
1014 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +00001015 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +00001016 if( i>0 ){
drhff6d50e2017-04-11 18:55:05 +00001017 assert( pParse->iDepth==0 );
dan2e8f5512015-09-17 17:21:09 +00001018 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +00001019 if( zJson[i] ) i = -1;
1020 }
drhd1f00682015-08-29 16:02:37 +00001021 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +00001022 if( pCtx!=0 ){
1023 if( pParse->oom ){
1024 sqlite3_result_error_nomem(pCtx);
1025 }else{
1026 sqlite3_result_error(pCtx, "malformed JSON", -1);
1027 }
1028 }
drh505ad2c2015-08-21 17:33:11 +00001029 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +00001030 return 1;
1031 }
1032 return 0;
1033}
drh301eecc2015-08-17 20:14:19 +00001034
drh505ad2c2015-08-21 17:33:11 +00001035/* Mark node i of pParse as being a child of iParent. Call recursively
1036** to fill in all the descendants of node i.
1037*/
1038static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
1039 JsonNode *pNode = &pParse->aNode[i];
1040 u32 j;
1041 pParse->aUp[i] = iParent;
1042 switch( pNode->eType ){
1043 case JSON_ARRAY: {
1044 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
1045 jsonParseFillInParentage(pParse, i+j, i);
1046 }
1047 break;
1048 }
1049 case JSON_OBJECT: {
1050 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
1051 pParse->aUp[i+j] = i;
1052 jsonParseFillInParentage(pParse, i+j+1, i);
1053 }
1054 break;
1055 }
1056 default: {
1057 break;
1058 }
1059 }
1060}
1061
1062/*
1063** Compute the parentage of all nodes in a completed parse.
1064*/
1065static int jsonParseFindParents(JsonParse *pParse){
1066 u32 *aUp;
1067 assert( pParse->aUp==0 );
drh2d77d802019-01-08 20:02:48 +00001068 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +00001069 if( aUp==0 ){
1070 pParse->oom = 1;
1071 return SQLITE_NOMEM;
1072 }
drh505ad2c2015-08-21 17:33:11 +00001073 jsonParseFillInParentage(pParse, 0, 0);
1074 return SQLITE_OK;
1075}
1076
drh8cb0c832015-09-22 00:21:03 +00001077/*
drh3fb153c2017-05-11 16:49:59 +00001078** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1079*/
drhe35fc302018-08-30 01:52:10 +00001080#define JSON_CACHE_ID (-429938) /* First cache entry */
1081#define JSON_CACHE_SZ 4 /* Max number of cache entries */
drh3fb153c2017-05-11 16:49:59 +00001082
1083/*
1084** Obtain a complete parse of the JSON found in the first argument
1085** of the argv array. Use the sqlite3_get_auxdata() cache for this
1086** parse if it is available. If the cache is not available or if it
1087** is no longer valid, parse the JSON again and return the new parse,
1088** and also register the new parse so that it will be available for
1089** future sqlite3_get_auxdata() calls.
1090*/
1091static JsonParse *jsonParseCached(
1092 sqlite3_context *pCtx,
drhe35fc302018-08-30 01:52:10 +00001093 sqlite3_value **argv,
1094 sqlite3_context *pErrCtx
drh3fb153c2017-05-11 16:49:59 +00001095){
1096 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
1097 int nJson = sqlite3_value_bytes(argv[0]);
1098 JsonParse *p;
drhe35fc302018-08-30 01:52:10 +00001099 JsonParse *pMatch = 0;
1100 int iKey;
1101 int iMinKey = 0;
1102 u32 iMinHold = 0xffffffff;
1103 u32 iMaxHold = 0;
drh3fb153c2017-05-11 16:49:59 +00001104 if( zJson==0 ) return 0;
drhe35fc302018-08-30 01:52:10 +00001105 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
1106 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
1107 if( p==0 ){
1108 iMinKey = iKey;
1109 break;
1110 }
1111 if( pMatch==0
1112 && p->nJson==nJson
1113 && memcmp(p->zJson,zJson,nJson)==0
1114 ){
1115 p->nErr = 0;
1116 pMatch = p;
1117 }else if( p->iHold<iMinHold ){
1118 iMinHold = p->iHold;
1119 iMinKey = iKey;
1120 }
1121 if( p->iHold>iMaxHold ){
1122 iMaxHold = p->iHold;
1123 }
1124 }
1125 if( pMatch ){
1126 pMatch->nErr = 0;
1127 pMatch->iHold = iMaxHold+1;
1128 return pMatch;
drh3fb153c2017-05-11 16:49:59 +00001129 }
drh2d77d802019-01-08 20:02:48 +00001130 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
drh3fb153c2017-05-11 16:49:59 +00001131 if( p==0 ){
1132 sqlite3_result_error_nomem(pCtx);
1133 return 0;
1134 }
1135 memset(p, 0, sizeof(*p));
1136 p->zJson = (char*)&p[1];
1137 memcpy((char*)p->zJson, zJson, nJson+1);
drhe35fc302018-08-30 01:52:10 +00001138 if( jsonParse(p, pErrCtx, p->zJson) ){
drh3fb153c2017-05-11 16:49:59 +00001139 sqlite3_free(p);
1140 return 0;
1141 }
1142 p->nJson = nJson;
drhe35fc302018-08-30 01:52:10 +00001143 p->iHold = iMaxHold+1;
1144 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
1145 (void(*)(void*))jsonParseFree);
1146 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
drh3fb153c2017-05-11 16:49:59 +00001147}
1148
1149/*
drh8cb0c832015-09-22 00:21:03 +00001150** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1151** a match.
1152*/
mistachkinf2c26ed2015-10-12 22:20:29 +00001153static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh285f2ef2021-10-15 16:15:04 +00001154 assert( pNode->eU==1 );
drh8cb0c832015-09-22 00:21:03 +00001155 if( pNode->jnFlags & JNODE_RAW ){
1156 if( pNode->n!=nKey ) return 0;
1157 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
1158 }else{
1159 if( pNode->n!=nKey+2 ) return 0;
1160 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
1161 }
1162}
1163
drh52216ad2015-08-18 02:28:03 +00001164/* forward declaration */
drha7714022015-08-29 00:54:49 +00001165static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +00001166
drh987eb1f2015-08-17 15:17:37 +00001167/*
1168** Search along zPath to find the node specified. Return a pointer
1169** to that node, or NULL if zPath is malformed or if there is no such
1170** node.
drh52216ad2015-08-18 02:28:03 +00001171**
1172** If pApnd!=0, then try to append new nodes to complete zPath if it is
1173** possible to do so and if no existing node corresponds to zPath. If
1174** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +00001175*/
drha7714022015-08-29 00:54:49 +00001176static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +00001177 JsonParse *pParse, /* The JSON to search */
1178 u32 iRoot, /* Begin the search at this node */
1179 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +00001180 int *pApnd, /* Append nodes to complete path if not NULL */
1181 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +00001182){
drhbc8f0922015-08-22 19:39:04 +00001183 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +00001184 const char *zKey;
drh52216ad2015-08-18 02:28:03 +00001185 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +00001186 if( zPath[0]==0 ) return pRoot;
drh7e35e812019-07-31 12:13:58 +00001187 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
drh987eb1f2015-08-17 15:17:37 +00001188 if( zPath[0]=='.' ){
1189 if( pRoot->eType!=JSON_OBJECT ) return 0;
1190 zPath++;
drh6b43cc82015-08-19 23:02:49 +00001191 if( zPath[0]=='"' ){
1192 zKey = zPath + 1;
1193 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
1194 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +00001195 if( zPath[i] ){
1196 i++;
1197 }else{
1198 *pzErr = zPath;
1199 return 0;
1200 }
drh6b43cc82015-08-19 23:02:49 +00001201 }else{
1202 zKey = zPath;
1203 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
1204 nKey = i;
1205 }
drha7714022015-08-29 00:54:49 +00001206 if( nKey==0 ){
1207 *pzErr = zPath;
1208 return 0;
1209 }
drh987eb1f2015-08-17 15:17:37 +00001210 j = 1;
drh52216ad2015-08-18 02:28:03 +00001211 for(;;){
1212 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +00001213 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +00001214 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001215 }
1216 j++;
drh505ad2c2015-08-21 17:33:11 +00001217 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +00001218 }
drh52216ad2015-08-18 02:28:03 +00001219 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001220 assert( pRoot->eU==2 );
drh52216ad2015-08-18 02:28:03 +00001221 iRoot += pRoot->u.iAppend;
1222 pRoot = &pParse->aNode[iRoot];
1223 j = 1;
1224 }
1225 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001226 u32 iStart, iLabel;
1227 JsonNode *pNode;
1228 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
danfe9a8322019-06-17 14:50:33 +00001229 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
drh52216ad2015-08-18 02:28:03 +00001230 zPath += i;
drha7714022015-08-29 00:54:49 +00001231 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001232 if( pParse->oom ) return 0;
1233 if( pNode ){
1234 pRoot = &pParse->aNode[iRoot];
drh285f2ef2021-10-15 16:15:04 +00001235 assert( pRoot->eU==0 );
drhbc8f0922015-08-22 19:39:04 +00001236 pRoot->u.iAppend = iStart - iRoot;
1237 pRoot->jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001238 VVA( pRoot->eU = 2 );
drhbc8f0922015-08-22 19:39:04 +00001239 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1240 }
1241 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001242 }
drh52818642019-11-22 17:37:56 +00001243 }else if( zPath[0]=='[' ){
drh987eb1f2015-08-17 15:17:37 +00001244 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001245 j = 1;
1246 while( safe_isdigit(zPath[j]) ){
1247 i = i*10 + zPath[j] - '0';
1248 j++;
drh987eb1f2015-08-17 15:17:37 +00001249 }
drh52818642019-11-22 17:37:56 +00001250 if( j<2 || zPath[j]!=']' ){
1251 if( zPath[1]=='#' ){
1252 JsonNode *pBase = pRoot;
1253 int iBase = iRoot;
1254 if( pRoot->eType!=JSON_ARRAY ) return 0;
1255 for(;;){
1256 while( j<=pBase->n ){
1257 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
1258 j += jsonNodeSize(&pBase[j]);
1259 }
1260 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001261 assert( pBase->eU==2 );
drh52818642019-11-22 17:37:56 +00001262 iBase += pBase->u.iAppend;
1263 pBase = &pParse->aNode[iBase];
1264 j = 1;
1265 }
1266 j = 2;
1267 if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
1268 unsigned int x = 0;
1269 j = 3;
1270 do{
1271 x = x*10 + zPath[j] - '0';
1272 j++;
1273 }while( safe_isdigit(zPath[j]) );
1274 if( x>i ) return 0;
1275 i -= x;
1276 }
1277 if( zPath[j]!=']' ){
1278 *pzErr = zPath;
1279 return 0;
1280 }
1281 }else{
1282 *pzErr = zPath;
1283 return 0;
1284 }
drha7714022015-08-29 00:54:49 +00001285 }
drh52818642019-11-22 17:37:56 +00001286 if( pRoot->eType!=JSON_ARRAY ) return 0;
drh3d1d2a92015-09-22 01:15:49 +00001287 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001288 j = 1;
drh52216ad2015-08-18 02:28:03 +00001289 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001290 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1291 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001292 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001293 }
1294 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001295 assert( pRoot->eU==2 );
drh52216ad2015-08-18 02:28:03 +00001296 iRoot += pRoot->u.iAppend;
1297 pRoot = &pParse->aNode[iRoot];
1298 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001299 }
1300 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001301 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001302 }
1303 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001304 u32 iStart;
1305 JsonNode *pNode;
1306 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001307 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001308 if( pParse->oom ) return 0;
1309 if( pNode ){
1310 pRoot = &pParse->aNode[iRoot];
drh285f2ef2021-10-15 16:15:04 +00001311 assert( pRoot->eU==0 );
drhbc8f0922015-08-22 19:39:04 +00001312 pRoot->u.iAppend = iStart - iRoot;
1313 pRoot->jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001314 VVA( pRoot->eU = 2 );
drhbc8f0922015-08-22 19:39:04 +00001315 }
1316 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001317 }
drh3d1d2a92015-09-22 01:15:49 +00001318 }else{
drha7714022015-08-29 00:54:49 +00001319 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001320 }
1321 return 0;
1322}
1323
drh52216ad2015-08-18 02:28:03 +00001324/*
drhbc8f0922015-08-22 19:39:04 +00001325** Append content to pParse that will complete zPath. Return a pointer
1326** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001327*/
1328static JsonNode *jsonLookupAppend(
1329 JsonParse *pParse, /* Append content to the JSON parse */
1330 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001331 int *pApnd, /* Set this flag to 1 */
1332 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001333){
1334 *pApnd = 1;
1335 if( zPath[0]==0 ){
1336 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1337 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1338 }
1339 if( zPath[0]=='.' ){
1340 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1341 }else if( strncmp(zPath,"[0]",3)==0 ){
1342 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1343 }else{
1344 return 0;
1345 }
1346 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001347 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001348}
1349
drhbc8f0922015-08-22 19:39:04 +00001350/*
drha7714022015-08-29 00:54:49 +00001351** Return the text of a syntax error message on a JSON path. Space is
1352** obtained from sqlite3_malloc().
1353*/
1354static char *jsonPathSyntaxError(const char *zErr){
1355 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1356}
1357
1358/*
1359** Do a node lookup using zPath. Return a pointer to the node on success.
1360** Return NULL if not found or if there is an error.
1361**
1362** On an error, write an error message into pCtx and increment the
1363** pParse->nErr counter.
1364**
1365** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1366** nodes are appended.
drha7714022015-08-29 00:54:49 +00001367*/
1368static JsonNode *jsonLookup(
1369 JsonParse *pParse, /* The JSON to search */
1370 const char *zPath, /* The path to search */
1371 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001372 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001373){
1374 const char *zErr = 0;
1375 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001376 char *zMsg;
drha7714022015-08-29 00:54:49 +00001377
1378 if( zPath==0 ) return 0;
1379 if( zPath[0]!='$' ){
1380 zErr = zPath;
1381 goto lookup_err;
1382 }
1383 zPath++;
drha7714022015-08-29 00:54:49 +00001384 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001385 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001386
1387lookup_err:
1388 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001389 assert( zErr!=0 && pCtx!=0 );
1390 zMsg = jsonPathSyntaxError(zErr);
1391 if( zMsg ){
1392 sqlite3_result_error(pCtx, zMsg, -1);
1393 sqlite3_free(zMsg);
1394 }else{
1395 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001396 }
drha7714022015-08-29 00:54:49 +00001397 return 0;
1398}
1399
1400
1401/*
drhbc8f0922015-08-22 19:39:04 +00001402** Report the wrong number of arguments for json_insert(), json_replace()
1403** or json_set().
1404*/
1405static void jsonWrongNumArgs(
1406 sqlite3_context *pCtx,
1407 const char *zFuncName
1408){
1409 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1410 zFuncName);
1411 sqlite3_result_error(pCtx, zMsg, -1);
1412 sqlite3_free(zMsg);
1413}
drh52216ad2015-08-18 02:28:03 +00001414
drh29c99692017-03-24 12:35:17 +00001415/*
1416** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1417*/
1418static void jsonRemoveAllNulls(JsonNode *pNode){
1419 int i, n;
1420 assert( pNode->eType==JSON_OBJECT );
1421 n = pNode->n;
1422 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1423 switch( pNode[i].eType ){
1424 case JSON_NULL:
1425 pNode[i].jnFlags |= JNODE_REMOVE;
1426 break;
1427 case JSON_OBJECT:
1428 jsonRemoveAllNulls(&pNode[i]);
1429 break;
1430 }
1431 }
1432}
1433
drha7714022015-08-29 00:54:49 +00001434
drh987eb1f2015-08-17 15:17:37 +00001435/****************************************************************************
1436** SQL functions used for testing and debugging
1437****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001438
drh301eecc2015-08-17 20:14:19 +00001439#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001440/*
drh5634cc02015-08-17 11:28:03 +00001441** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001442** a parse of the JSON provided. Or it returns NULL if JSON is not
1443** well-formed.
1444*/
drh5634cc02015-08-17 11:28:03 +00001445static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001446 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001447 int argc,
1448 sqlite3_value **argv
1449){
drh505ad2c2015-08-21 17:33:11 +00001450 JsonString s; /* Output string - not real JSON */
1451 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001452 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001453
1454 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001455 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001456 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001457 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001458 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001459 const char *zType;
1460 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1461 assert( x.aNode[i].eType==JSON_STRING );
1462 zType = "label";
1463 }else{
1464 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001465 }
drh852944e2015-09-10 03:29:11 +00001466 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1467 i, zType, x.aNode[i].n, x.aUp[i]);
drh285f2ef2021-10-15 16:15:04 +00001468 assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
drh852944e2015-09-10 03:29:11 +00001469 if( x.aNode[i].u.zJContent!=0 ){
drh285f2ef2021-10-15 16:15:04 +00001470 assert( x.aNode[i].eU==1 );
drh852944e2015-09-10 03:29:11 +00001471 jsonAppendRaw(&s, " ", 1);
1472 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
drh285f2ef2021-10-15 16:15:04 +00001473 }else{
1474 assert( x.aNode[i].eU==0 );
drh852944e2015-09-10 03:29:11 +00001475 }
1476 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001477 }
drh505ad2c2015-08-21 17:33:11 +00001478 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001479 jsonResult(&s);
1480}
1481
drh5634cc02015-08-17 11:28:03 +00001482/*
drhf5ddb9c2015-09-11 00:06:41 +00001483** The json_test1(JSON) function return true (1) if the input is JSON
1484** text generated by another json function. It returns (0) if the input
1485** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001486*/
1487static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001488 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001489 int argc,
1490 sqlite3_value **argv
1491){
mistachkin16a93122015-09-11 18:05:01 +00001492 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001493 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001494}
drh301eecc2015-08-17 20:14:19 +00001495#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001496
drh987eb1f2015-08-17 15:17:37 +00001497/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001498** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001499****************************************************************************/
1500
1501/*
drh2ad96f52016-06-17 13:01:51 +00001502** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1503** corresponding to the SQL value input. Mostly this means putting
1504** double-quotes around strings and returning the unquoted string "null"
1505** when given a NULL input.
1506*/
1507static void jsonQuoteFunc(
1508 sqlite3_context *ctx,
1509 int argc,
1510 sqlite3_value **argv
1511){
1512 JsonString jx;
drhb0df5402016-08-01 17:06:44 +00001513 UNUSED_PARAM(argc);
drh2ad96f52016-06-17 13:01:51 +00001514
1515 jsonInit(&jx, ctx);
1516 jsonAppendValue(&jx, argv[0]);
1517 jsonResult(&jx);
1518 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1519}
1520
1521/*
drh987eb1f2015-08-17 15:17:37 +00001522** Implementation of the json_array(VALUE,...) function. Return a JSON
1523** array that contains all values given in arguments. Or if any argument
1524** is a BLOB, throw an error.
1525*/
1526static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001527 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001528 int argc,
1529 sqlite3_value **argv
1530){
1531 int i;
drh505ad2c2015-08-21 17:33:11 +00001532 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001533
drhbc8f0922015-08-22 19:39:04 +00001534 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001535 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001536 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001537 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001538 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001539 }
drhd0960592015-08-17 21:22:32 +00001540 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001541 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001542 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001543}
1544
1545
1546/*
1547** json_array_length(JSON)
1548** json_array_length(JSON, PATH)
1549**
1550** Return the number of elements in the top-level JSON array.
1551** Return 0 if the input is not a well-formed JSON array.
1552*/
1553static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001554 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001555 int argc,
1556 sqlite3_value **argv
1557){
drh3fb153c2017-05-11 16:49:59 +00001558 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001559 sqlite3_int64 n = 0;
1560 u32 i;
drha8f39a92015-09-21 22:53:16 +00001561 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001562
drhe35fc302018-08-30 01:52:10 +00001563 p = jsonParseCached(ctx, argv, ctx);
drh3fb153c2017-05-11 16:49:59 +00001564 if( p==0 ) return;
1565 assert( p->nNode );
drha8f39a92015-09-21 22:53:16 +00001566 if( argc==2 ){
1567 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
drh3fb153c2017-05-11 16:49:59 +00001568 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00001569 }else{
drh3fb153c2017-05-11 16:49:59 +00001570 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00001571 }
1572 if( pNode==0 ){
drh3fb153c2017-05-11 16:49:59 +00001573 return;
1574 }
1575 if( pNode->eType==JSON_ARRAY ){
drha8f39a92015-09-21 22:53:16 +00001576 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1577 for(i=1; i<=pNode->n; n++){
1578 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001579 }
drh987eb1f2015-08-17 15:17:37 +00001580 }
drh3fb153c2017-05-11 16:49:59 +00001581 sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001582}
1583
1584/*
drhdc60c682022-01-08 15:05:53 +00001585** Bit values for the flags passed into jsonExtractFunc() or
1586** jsonSetFunc() via the user-data value.
1587*/
1588#define JSON_NULLERR 0x01 /* Return NULL if input is not JSON */
1589#define JSON_ABPATH 0x02 /* Allow abbreviated JSON path specs */
1590#define JSON_ISSET 0x04 /* json_set(), not json_insert() */
1591
1592/*
drh3ad93bb2015-08-29 19:41:45 +00001593** json_extract(JSON, PATH, ...)
drhdc60c682022-01-08 15:05:53 +00001594** json_nextract(JSON, PATH, ...)
1595** "->"(JSON,PATH)
1596** "->>"(JSON,PATH)
drh987eb1f2015-08-17 15:17:37 +00001597**
drhdc60c682022-01-08 15:05:53 +00001598** Return the element described by PATH. Return NULL if that PATH element
1599** is not found. For leaf nodes of the JSON, the value returned is a pure
1600** SQL value. In other words, quotes have been removed from strings.
1601**
1602** If there are multiple PATHs, then the value returned is a JSON array
1603** with one entry in the array for each PATH term.
1604**
1605** Throw an error if any PATH is malformed.
1606**
1607** If JSON is not well-formed JSON then:
1608**
1609** (1) raise an error if the JSON_NULLERR flag is not set.
1610**
1611** (2) Otherwise (if the JSON_NULLERR flags is set and) if there
1612** is a single PATH argument with the value '$', simply quote
1613** the JSON input as if by json_quote(). In other words, treat
1614** the JSON input as a string and convert it into a valid JSON
1615** string.
1616**
1617** (3) Otherwise (if JSON_NULLERR is set and the PATH is not '$')
1618** return NULL
1619**
1620** If the JSON_ABPATH flag is set and there is only a single PATH, then
1621** allow abbreviated PATH specs that omit the leading "$".
drh987eb1f2015-08-17 15:17:37 +00001622*/
1623static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001624 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001625 int argc,
1626 sqlite3_value **argv
1627){
drh3fb153c2017-05-11 16:49:59 +00001628 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001629 JsonNode *pNode;
1630 const char *zPath;
drh338b1fd2022-01-07 17:08:48 +00001631 int flags = *(int*)sqlite3_user_data(ctx);
drh3ad93bb2015-08-29 19:41:45 +00001632 JsonString jx;
drh3ad93bb2015-08-29 19:41:45 +00001633
1634 if( argc<2 ) return;
drhdc60c682022-01-08 15:05:53 +00001635 p = jsonParseCached(ctx, argv, (flags & JSON_NULLERR)!=0 ? 0 : ctx);
drh338b1fd2022-01-07 17:08:48 +00001636 if( p==0 ){
1637 /* If the form is "json_nextract(IN,'$')" and IN is not well-formed JSON,
1638 ** then return IN as a quoted JSON string. */
drhdc60c682022-01-08 15:05:53 +00001639 if( (flags & JSON_NULLERR)!=0
drh338b1fd2022-01-07 17:08:48 +00001640 && argc==2
1641 && (zPath = (const char*)sqlite3_value_text(argv[1]))!=0
1642 && zPath[0]=='$' && zPath[1]==0
1643 ){
1644 jsonQuoteFunc(ctx, argc, argv);
1645 }
1646 return;
1647 }
drh12b9fa92022-01-07 15:47:12 +00001648 if( argc==2 ){
1649 /* With a single PATH argument, the return is the unquoted SQL value */
1650 zPath = (const char*)sqlite3_value_text(argv[1]);
drhdc60c682022-01-08 15:05:53 +00001651 if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & JSON_ABPATH)!=0 ){
1652 /* The -> and ->> operators accept abbreviated PATH arguments. This
1653 ** is mostly for compatibility with PostgreSQL, but also for convenience.
1654 **
1655 ** NUMBER ==> $[NUMBER] // PG compatible
1656 ** LABEL ==> $.LABEL // PG compatible
1657 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
drh12b9fa92022-01-07 15:47:12 +00001658 */
1659 jsonInit(&jx, ctx);
1660 if( safe_isdigit(zPath[0]) ){
1661 jsonAppendRaw(&jx, "$[", 2);
1662 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
1663 jsonAppendRaw(&jx, "]", 2);
1664 }else{
1665 jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
1666 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
1667 jsonAppendChar(&jx, 0);
1668 }
drhdc60c682022-01-08 15:05:53 +00001669 pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx);
drh12b9fa92022-01-07 15:47:12 +00001670 jsonReset(&jx);
1671 }else{
1672 pNode = jsonLookup(p, zPath, 0, ctx);
1673 }
1674 if( p->nErr ) return;
1675 if( pNode ) jsonReturn(pNode, ctx, 0);
1676 }else{
1677 /* Two or more PATH arguments results in a JSON array with each
1678 ** element of the array being the value selected by one of the PATHs */
1679 int i;
1680 jsonInit(&jx, ctx);
1681 jsonAppendChar(&jx, '[');
1682 for(i=1; i<argc; i++){
1683 zPath = (const char*)sqlite3_value_text(argv[i]);
1684 pNode = jsonLookup(p, zPath, 0, ctx);
1685 if( p->nErr ) break;
drh3ad93bb2015-08-29 19:41:45 +00001686 jsonAppendSeparator(&jx);
1687 if( pNode ){
1688 jsonRenderNode(pNode, &jx, 0);
1689 }else{
1690 jsonAppendRaw(&jx, "null", 4);
1691 }
drh3ad93bb2015-08-29 19:41:45 +00001692 }
drh12b9fa92022-01-07 15:47:12 +00001693 if( i==argc ){
1694 jsonAppendChar(&jx, ']');
1695 jsonResult(&jx);
1696 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1697 }
1698 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001699 }
drh987eb1f2015-08-17 15:17:37 +00001700}
1701
drh633647a2017-03-22 21:24:31 +00001702/* This is the RFC 7396 MergePatch algorithm.
1703*/
1704static JsonNode *jsonMergePatch(
1705 JsonParse *pParse, /* The JSON parser that contains the TARGET */
mistachkinb1ed7172017-04-14 14:50:34 +00001706 u32 iTarget, /* Node of the TARGET in pParse */
drh633647a2017-03-22 21:24:31 +00001707 JsonNode *pPatch /* The PATCH */
1708){
drh0002d242017-03-23 00:46:15 +00001709 u32 i, j;
1710 u32 iRoot;
drh633647a2017-03-22 21:24:31 +00001711 JsonNode *pTarget;
1712 if( pPatch->eType!=JSON_OBJECT ){
1713 return pPatch;
1714 }
1715 assert( iTarget>=0 && iTarget<pParse->nNode );
1716 pTarget = &pParse->aNode[iTarget];
1717 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1718 if( pTarget->eType!=JSON_OBJECT ){
drh29c99692017-03-24 12:35:17 +00001719 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001720 return pPatch;
1721 }
drhbb7aa2d2017-03-23 00:13:52 +00001722 iRoot = iTarget;
drh633647a2017-03-22 21:24:31 +00001723 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
drhba7cce32017-03-24 19:45:05 +00001724 u32 nKey;
drh633647a2017-03-22 21:24:31 +00001725 const char *zKey;
1726 assert( pPatch[i].eType==JSON_STRING );
1727 assert( pPatch[i].jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00001728 assert( pPatch[i].eU==1 );
drh633647a2017-03-22 21:24:31 +00001729 nKey = pPatch[i].n;
1730 zKey = pPatch[i].u.zJContent;
drhbb7aa2d2017-03-23 00:13:52 +00001731 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh633647a2017-03-22 21:24:31 +00001732 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1733 assert( pTarget[j].eType==JSON_STRING );
1734 assert( pTarget[j].jnFlags & JNODE_LABEL );
drhbb7aa2d2017-03-23 00:13:52 +00001735 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh1fe162f2017-03-23 12:56:44 +00001736 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1737 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
drh633647a2017-03-22 21:24:31 +00001738 if( pPatch[i+1].eType==JSON_NULL ){
1739 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1740 }else{
drhbb7aa2d2017-03-23 00:13:52 +00001741 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
drh633647a2017-03-22 21:24:31 +00001742 if( pNew==0 ) return 0;
1743 pTarget = &pParse->aNode[iTarget];
1744 if( pNew!=&pTarget[j+1] ){
drha2852ac2021-11-15 01:45:11 +00001745 assert( pTarget[j+1].eU==0
1746 || pTarget[j+1].eU==1
1747 || pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001748 testcase( pTarget[j+1].eU==1 );
drha2852ac2021-11-15 01:45:11 +00001749 testcase( pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001750 VVA( pTarget[j+1].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001751 pTarget[j+1].u.pPatch = pNew;
1752 pTarget[j+1].jnFlags |= JNODE_PATCH;
1753 }
1754 }
1755 break;
1756 }
1757 }
drhbb7aa2d2017-03-23 00:13:52 +00001758 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
drh633647a2017-03-22 21:24:31 +00001759 int iStart, iPatch;
1760 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1761 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1762 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1763 if( pParse->oom ) return 0;
drh29c99692017-03-24 12:35:17 +00001764 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001765 pTarget = &pParse->aNode[iTarget];
drh8b554e22021-10-20 20:22:37 +00001766 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
1767 testcase( pParse->aNode[iRoot].eU==2 );
drhbb7aa2d2017-03-23 00:13:52 +00001768 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001769 VVA( pParse->aNode[iRoot].eU = 2 );
drhbb7aa2d2017-03-23 00:13:52 +00001770 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
1771 iRoot = iStart;
drh285f2ef2021-10-15 16:15:04 +00001772 assert( pParse->aNode[iPatch].eU==0 );
1773 VVA( pParse->aNode[iPatch].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001774 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
1775 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
1776 }
1777 }
1778 return pTarget;
1779}
1780
1781/*
1782** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1783** object that is the result of running the RFC 7396 MergePatch() algorithm
1784** on the two arguments.
1785*/
drh37f03df2017-03-23 20:33:49 +00001786static void jsonPatchFunc(
drh633647a2017-03-22 21:24:31 +00001787 sqlite3_context *ctx,
1788 int argc,
1789 sqlite3_value **argv
1790){
1791 JsonParse x; /* The JSON that is being patched */
1792 JsonParse y; /* The patch */
drhbb7aa2d2017-03-23 00:13:52 +00001793 JsonNode *pResult; /* The result of the merge */
drh633647a2017-03-22 21:24:31 +00001794
drh2fb79e92017-03-25 12:08:11 +00001795 UNUSED_PARAM(argc);
drh633647a2017-03-22 21:24:31 +00001796 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1797 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
1798 jsonParseReset(&x);
1799 return;
1800 }
drhbb7aa2d2017-03-23 00:13:52 +00001801 pResult = jsonMergePatch(&x, 0, y.aNode);
1802 assert( pResult!=0 || x.oom );
1803 if( pResult ){
1804 jsonReturnJson(pResult, ctx, 0);
1805 }else{
1806 sqlite3_result_error_nomem(ctx);
1807 }
drh633647a2017-03-22 21:24:31 +00001808 jsonParseReset(&x);
1809 jsonParseReset(&y);
1810}
1811
1812
drh987eb1f2015-08-17 15:17:37 +00001813/*
1814** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1815** object that contains all name/value given in arguments. Or if any name
1816** is not a string or if any value is a BLOB, throw an error.
1817*/
1818static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001819 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001820 int argc,
1821 sqlite3_value **argv
1822){
1823 int i;
drh505ad2c2015-08-21 17:33:11 +00001824 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001825 const char *z;
1826 u32 n;
1827
1828 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001829 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001830 "of arguments", -1);
1831 return;
1832 }
drhbc8f0922015-08-22 19:39:04 +00001833 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001834 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001835 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001836 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001837 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001838 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001839 return;
1840 }
drhd0960592015-08-17 21:22:32 +00001841 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001842 z = (const char*)sqlite3_value_text(argv[i]);
1843 n = (u32)sqlite3_value_bytes(argv[i]);
1844 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001845 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001846 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001847 }
drhd0960592015-08-17 21:22:32 +00001848 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001849 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001850 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001851}
1852
1853
1854/*
drh301eecc2015-08-17 20:14:19 +00001855** json_remove(JSON, PATH, ...)
1856**
drh3ad93bb2015-08-29 19:41:45 +00001857** Remove the named elements from JSON and return the result. malformed
1858** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001859*/
1860static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001861 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001862 int argc,
1863 sqlite3_value **argv
1864){
1865 JsonParse x; /* The parse */
1866 JsonNode *pNode;
1867 const char *zPath;
1868 u32 i;
1869
1870 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001871 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001872 assert( x.nNode );
1873 for(i=1; i<(u32)argc; i++){
1874 zPath = (const char*)sqlite3_value_text(argv[i]);
1875 if( zPath==0 ) goto remove_done;
1876 pNode = jsonLookup(&x, zPath, 0, ctx);
1877 if( x.nErr ) goto remove_done;
1878 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1879 }
1880 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1881 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001882 }
drha7714022015-08-29 00:54:49 +00001883remove_done:
drh505ad2c2015-08-21 17:33:11 +00001884 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001885}
1886
1887/*
1888** json_replace(JSON, PATH, VALUE, ...)
1889**
1890** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001891** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001892*/
1893static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001894 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001895 int argc,
1896 sqlite3_value **argv
1897){
1898 JsonParse x; /* The parse */
1899 JsonNode *pNode;
1900 const char *zPath;
1901 u32 i;
1902
1903 if( argc<1 ) return;
1904 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001905 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001906 return;
1907 }
drhbc8f0922015-08-22 19:39:04 +00001908 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001909 assert( x.nNode );
1910 for(i=1; i<(u32)argc; i+=2){
1911 zPath = (const char*)sqlite3_value_text(argv[i]);
1912 pNode = jsonLookup(&x, zPath, 0, ctx);
1913 if( x.nErr ) goto replace_err;
1914 if( pNode ){
drh285f2ef2021-10-15 16:15:04 +00001915 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
1916 json_testcase( pNode->eU!=0 && pNode->eU!=1 );
drha8f39a92015-09-21 22:53:16 +00001917 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh285f2ef2021-10-15 16:15:04 +00001918 VVA( pNode->eU = 4 );
drh633647a2017-03-22 21:24:31 +00001919 pNode->u.iReplace = i + 1;
drhd0960592015-08-17 21:22:32 +00001920 }
drha8f39a92015-09-21 22:53:16 +00001921 }
1922 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001923 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001924 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001925 }else{
1926 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001927 }
drha7714022015-08-29 00:54:49 +00001928replace_err:
drh505ad2c2015-08-21 17:33:11 +00001929 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001930}
drh505ad2c2015-08-21 17:33:11 +00001931
drhdc60c682022-01-08 15:05:53 +00001932
drh52216ad2015-08-18 02:28:03 +00001933/*
1934** json_set(JSON, PATH, VALUE, ...)
1935**
1936** Set the value at PATH to VALUE. Create the PATH if it does not already
1937** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001938** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001939**
1940** json_insert(JSON, PATH, VALUE, ...)
1941**
1942** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001943** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001944*/
1945static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001946 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001947 int argc,
1948 sqlite3_value **argv
1949){
1950 JsonParse x; /* The parse */
1951 JsonNode *pNode;
1952 const char *zPath;
1953 u32 i;
1954 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001955 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001956
1957 if( argc<1 ) return;
1958 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001959 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001960 return;
1961 }
drhbc8f0922015-08-22 19:39:04 +00001962 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001963 assert( x.nNode );
1964 for(i=1; i<(u32)argc; i+=2){
1965 zPath = (const char*)sqlite3_value_text(argv[i]);
1966 bApnd = 0;
1967 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1968 if( x.oom ){
1969 sqlite3_result_error_nomem(ctx);
1970 goto jsonSetDone;
1971 }else if( x.nErr ){
1972 goto jsonSetDone;
1973 }else if( pNode && (bApnd || bIsSet) ){
drh285f2ef2021-10-15 16:15:04 +00001974 json_testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 );
1975 assert( pNode->eU!=3 || pNode->eU!=5 );
1976 VVA( pNode->eU = 4 );
drha8f39a92015-09-21 22:53:16 +00001977 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001978 pNode->u.iReplace = i + 1;
drh52216ad2015-08-18 02:28:03 +00001979 }
drha8f39a92015-09-21 22:53:16 +00001980 }
1981 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001982 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001983 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001984 }else{
1985 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001986 }
drhbc8f0922015-08-22 19:39:04 +00001987jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001988 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001989}
drh301eecc2015-08-17 20:14:19 +00001990
1991/*
drh987eb1f2015-08-17 15:17:37 +00001992** json_type(JSON)
drha4e4e182022-01-07 16:03:00 +00001993** json_ntype(JSON)
drh987eb1f2015-08-17 15:17:37 +00001994** json_type(JSON, PATH)
1995**
drha4e4e182022-01-07 16:03:00 +00001996** Return the top-level "type" of a JSON string. json_type() raises an
1997** error if either the JSON or PATH inputs are not well-formed. json_ntype()
1998** works like the one-argument version of json_type() except that it
1999** returns NULL if the JSON argument is not well-formed.
drh987eb1f2015-08-17 15:17:37 +00002000*/
2001static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00002002 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00002003 int argc,
2004 sqlite3_value **argv
2005){
drhe35fc302018-08-30 01:52:10 +00002006 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00002007 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00002008 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00002009
drha4e4e182022-01-07 16:03:00 +00002010 p = jsonParseCached(ctx, argv, *(int*)sqlite3_user_data(ctx) ? 0 : ctx);
drhe35fc302018-08-30 01:52:10 +00002011 if( p==0 ) return;
drha8f39a92015-09-21 22:53:16 +00002012 if( argc==2 ){
2013 zPath = (const char*)sqlite3_value_text(argv[1]);
drhe35fc302018-08-30 01:52:10 +00002014 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00002015 }else{
drhe35fc302018-08-30 01:52:10 +00002016 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00002017 }
2018 if( pNode ){
2019 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00002020 }
drh987eb1f2015-08-17 15:17:37 +00002021}
drh5634cc02015-08-17 11:28:03 +00002022
drhbc8f0922015-08-22 19:39:04 +00002023/*
2024** json_valid(JSON)
2025**
drh3ad93bb2015-08-29 19:41:45 +00002026** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
2027** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00002028*/
2029static void jsonValidFunc(
2030 sqlite3_context *ctx,
2031 int argc,
2032 sqlite3_value **argv
2033){
drhe35fc302018-08-30 01:52:10 +00002034 JsonParse *p; /* The parse */
mistachkin16a93122015-09-11 18:05:01 +00002035 UNUSED_PARAM(argc);
drhe35fc302018-08-30 01:52:10 +00002036 p = jsonParseCached(ctx, argv, 0);
2037 sqlite3_result_int(ctx, p!=0);
drhbc8f0922015-08-22 19:39:04 +00002038}
2039
drhff135ae2015-12-30 01:07:02 +00002040
2041/****************************************************************************
2042** Aggregate SQL function implementations
2043****************************************************************************/
2044/*
2045** json_group_array(VALUE)
2046**
2047** Return a JSON array composed of all values in the aggregate.
2048*/
2049static void jsonArrayStep(
2050 sqlite3_context *ctx,
2051 int argc,
2052 sqlite3_value **argv
2053){
2054 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00002055 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002056 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2057 if( pStr ){
2058 if( pStr->zBuf==0 ){
2059 jsonInit(pStr, ctx);
2060 jsonAppendChar(pStr, '[');
drhfab5b072019-09-14 00:21:34 +00002061 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002062 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002063 }
dan8505d732021-04-14 12:11:39 +00002064 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002065 jsonAppendValue(pStr, argv[0]);
2066 }
2067}
drh8be47a72018-07-05 20:05:29 +00002068static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002069 JsonString *pStr;
2070 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2071 if( pStr ){
2072 pStr->pCtx = ctx;
2073 jsonAppendChar(pStr, ']');
2074 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00002075 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002076 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002077 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002078 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002079 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2080 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002081 }else{
mistachkined008ec2018-09-12 01:05:26 +00002082 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002083 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002084 }
2085 }else{
2086 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
2087 }
2088 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2089}
drh8be47a72018-07-05 20:05:29 +00002090static void jsonArrayValue(sqlite3_context *ctx){
2091 jsonArrayCompute(ctx, 0);
2092}
2093static void jsonArrayFinal(sqlite3_context *ctx){
2094 jsonArrayCompute(ctx, 1);
2095}
2096
2097#ifndef SQLITE_OMIT_WINDOWFUNC
2098/*
2099** This method works for both json_group_array() and json_group_object().
2100** It works by removing the first element of the group by searching forward
2101** to the first comma (",") that is not within a string and deleting all
2102** text through that comma.
2103*/
2104static void jsonGroupInverse(
2105 sqlite3_context *ctx,
2106 int argc,
2107 sqlite3_value **argv
2108){
drhe39f3882019-09-21 17:31:03 +00002109 unsigned int i;
drh8be47a72018-07-05 20:05:29 +00002110 int inStr = 0;
drhfab5b072019-09-14 00:21:34 +00002111 int nNest = 0;
drh8be47a72018-07-05 20:05:29 +00002112 char *z;
drhfab5b072019-09-14 00:21:34 +00002113 char c;
drh8be47a72018-07-05 20:05:29 +00002114 JsonString *pStr;
drhc7bf5712018-07-09 22:49:01 +00002115 UNUSED_PARAM(argc);
2116 UNUSED_PARAM(argv);
drh8be47a72018-07-05 20:05:29 +00002117 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
drh491d4c82018-07-07 20:23:46 +00002118#ifdef NEVER
drhfd4b7282018-07-07 19:47:21 +00002119 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2120 ** always have been called to initalize it */
2121 if( NEVER(!pStr) ) return;
drh491d4c82018-07-07 20:23:46 +00002122#endif
drh8be47a72018-07-05 20:05:29 +00002123 z = pStr->zBuf;
drh0e5cd342021-04-13 01:12:32 +00002124 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
drhfab5b072019-09-14 00:21:34 +00002125 if( c=='"' ){
drh8be47a72018-07-05 20:05:29 +00002126 inStr = !inStr;
drhfab5b072019-09-14 00:21:34 +00002127 }else if( c=='\\' ){
drh8be47a72018-07-05 20:05:29 +00002128 i++;
drhfab5b072019-09-14 00:21:34 +00002129 }else if( !inStr ){
2130 if( c=='{' || c=='[' ) nNest++;
2131 if( c=='}' || c==']' ) nNest--;
drh8be47a72018-07-05 20:05:29 +00002132 }
2133 }
drh0e5cd342021-04-13 01:12:32 +00002134 if( i<pStr->nUsed ){
2135 pStr->nUsed -= i;
2136 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
2137 z[pStr->nUsed] = 0;
2138 }else{
2139 pStr->nUsed = 1;
2140 }
drh8be47a72018-07-05 20:05:29 +00002141}
2142#else
2143# define jsonGroupInverse 0
2144#endif
2145
drhff135ae2015-12-30 01:07:02 +00002146
2147/*
2148** json_group_obj(NAME,VALUE)
2149**
2150** Return a JSON object composed of all names and values in the aggregate.
2151*/
2152static void jsonObjectStep(
2153 sqlite3_context *ctx,
2154 int argc,
2155 sqlite3_value **argv
2156){
2157 JsonString *pStr;
2158 const char *z;
2159 u32 n;
drhdf3a9072016-02-11 15:37:18 +00002160 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002161 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2162 if( pStr ){
2163 if( pStr->zBuf==0 ){
2164 jsonInit(pStr, ctx);
2165 jsonAppendChar(pStr, '{');
drhfab5b072019-09-14 00:21:34 +00002166 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002167 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002168 }
drhd2f55772021-05-28 12:15:19 +00002169 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002170 z = (const char*)sqlite3_value_text(argv[0]);
2171 n = (u32)sqlite3_value_bytes(argv[0]);
2172 jsonAppendString(pStr, z, n);
2173 jsonAppendChar(pStr, ':');
2174 jsonAppendValue(pStr, argv[1]);
2175 }
2176}
drh8be47a72018-07-05 20:05:29 +00002177static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002178 JsonString *pStr;
2179 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2180 if( pStr ){
2181 jsonAppendChar(pStr, '}');
2182 if( pStr->bErr ){
drh6850a632016-11-07 18:18:08 +00002183 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002184 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002185 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002186 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002187 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2188 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002189 }else{
mistachkined008ec2018-09-12 01:05:26 +00002190 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002191 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002192 }
2193 }else{
2194 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
2195 }
2196 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2197}
drh8be47a72018-07-05 20:05:29 +00002198static void jsonObjectValue(sqlite3_context *ctx){
2199 jsonObjectCompute(ctx, 0);
2200}
2201static void jsonObjectFinal(sqlite3_context *ctx){
2202 jsonObjectCompute(ctx, 1);
2203}
2204
drhff135ae2015-12-30 01:07:02 +00002205
2206
drhd2975922015-08-29 17:22:33 +00002207#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00002208/****************************************************************************
2209** The json_each virtual table
2210****************************************************************************/
2211typedef struct JsonEachCursor JsonEachCursor;
2212struct JsonEachCursor {
2213 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00002214 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00002215 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00002216 u32 i; /* Index in sParse.aNode[] of current row */
2217 u32 iEnd; /* EOF when i equals or exceeds this value */
2218 u8 eType; /* Type of top-level element */
2219 u8 bRecursive; /* True for json_tree(). False for json_each() */
2220 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00002221 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00002222 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00002223};
2224
2225/* Constructor for the json_each virtual table */
2226static int jsonEachConnect(
2227 sqlite3 *db,
2228 void *pAux,
2229 int argc, const char *const*argv,
2230 sqlite3_vtab **ppVtab,
2231 char **pzErr
2232){
2233 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00002234 int rc;
drhcb6c6c62015-08-19 22:47:17 +00002235
2236/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00002237#define JEACH_KEY 0
2238#define JEACH_VALUE 1
2239#define JEACH_TYPE 2
2240#define JEACH_ATOM 3
2241#define JEACH_ID 4
2242#define JEACH_PARENT 5
2243#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00002244#define JEACH_PATH 7
drh43579192018-11-16 16:04:50 +00002245/* The xBestIndex method assumes that the JSON and ROOT columns are
2246** the last two columns in the table. Should this ever changes, be
2247** sure to update the xBestIndex method. */
drh383de692015-09-10 17:20:57 +00002248#define JEACH_JSON 8
2249#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00002250
drh6fd5c1e2015-08-21 20:37:12 +00002251 UNUSED_PARAM(pzErr);
2252 UNUSED_PARAM(argv);
2253 UNUSED_PARAM(argc);
2254 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00002255 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00002256 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2257 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00002258 if( rc==SQLITE_OK ){
2259 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
2260 if( pNew==0 ) return SQLITE_NOMEM;
2261 memset(pNew, 0, sizeof(*pNew));
drh2b1c2aa2020-01-07 19:45:40 +00002262 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
drh505ad2c2015-08-21 17:33:11 +00002263 }
2264 return rc;
drhcb6c6c62015-08-19 22:47:17 +00002265}
2266
2267/* destructor for json_each virtual table */
2268static int jsonEachDisconnect(sqlite3_vtab *pVtab){
2269 sqlite3_free(pVtab);
2270 return SQLITE_OK;
2271}
2272
drh505ad2c2015-08-21 17:33:11 +00002273/* constructor for a JsonEachCursor object for json_each(). */
2274static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00002275 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00002276
2277 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00002278 pCur = sqlite3_malloc( sizeof(*pCur) );
2279 if( pCur==0 ) return SQLITE_NOMEM;
2280 memset(pCur, 0, sizeof(*pCur));
2281 *ppCursor = &pCur->base;
2282 return SQLITE_OK;
2283}
2284
drh505ad2c2015-08-21 17:33:11 +00002285/* constructor for a JsonEachCursor object for json_tree(). */
2286static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2287 int rc = jsonEachOpenEach(p, ppCursor);
2288 if( rc==SQLITE_OK ){
2289 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
2290 pCur->bRecursive = 1;
2291 }
2292 return rc;
2293}
2294
drhcb6c6c62015-08-19 22:47:17 +00002295/* Reset a JsonEachCursor back to its original state. Free any memory
2296** held. */
2297static void jsonEachCursorReset(JsonEachCursor *p){
2298 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00002299 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00002300 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00002301 p->iRowid = 0;
2302 p->i = 0;
2303 p->iEnd = 0;
2304 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00002305 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00002306 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002307}
2308
2309/* Destructor for a jsonEachCursor object */
2310static int jsonEachClose(sqlite3_vtab_cursor *cur){
2311 JsonEachCursor *p = (JsonEachCursor*)cur;
2312 jsonEachCursorReset(p);
2313 sqlite3_free(cur);
2314 return SQLITE_OK;
2315}
2316
2317/* Return TRUE if the jsonEachCursor object has been advanced off the end
2318** of the JSON object */
2319static int jsonEachEof(sqlite3_vtab_cursor *cur){
2320 JsonEachCursor *p = (JsonEachCursor*)cur;
2321 return p->i >= p->iEnd;
2322}
2323
drh505ad2c2015-08-21 17:33:11 +00002324/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00002325static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00002326 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00002327 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00002328 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
2329 p->i++;
drh4af352d2015-08-21 20:02:48 +00002330 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00002331 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00002332 u32 iUp = p->sParse.aUp[p->i];
2333 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00002334 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00002335 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002336 assert( pUp->eU==0 || pUp->eU==3 );
2337 json_testcase( pUp->eU==3 );
2338 VVA( pUp->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002339 if( iUp==p->i-1 ){
2340 pUp->u.iKey = 0;
2341 }else{
2342 pUp->u.iKey++;
2343 }
drh4af352d2015-08-21 20:02:48 +00002344 }
2345 }
drh505ad2c2015-08-21 17:33:11 +00002346 }else{
drh4af352d2015-08-21 20:02:48 +00002347 switch( p->eType ){
2348 case JSON_ARRAY: {
2349 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
2350 p->iRowid++;
2351 break;
2352 }
2353 case JSON_OBJECT: {
2354 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
2355 p->iRowid++;
2356 break;
2357 }
2358 default: {
2359 p->i = p->iEnd;
2360 break;
2361 }
drh505ad2c2015-08-21 17:33:11 +00002362 }
2363 }
2364 return SQLITE_OK;
2365}
2366
drh4af352d2015-08-21 20:02:48 +00002367/* Append the name of the path for element i to pStr
2368*/
2369static void jsonEachComputePath(
2370 JsonEachCursor *p, /* The cursor */
2371 JsonString *pStr, /* Write the path here */
2372 u32 i /* Path to this element */
2373){
2374 JsonNode *pNode, *pUp;
2375 u32 iUp;
2376 if( i==0 ){
2377 jsonAppendChar(pStr, '$');
2378 return;
drhcb6c6c62015-08-19 22:47:17 +00002379 }
drh4af352d2015-08-21 20:02:48 +00002380 iUp = p->sParse.aUp[i];
2381 jsonEachComputePath(p, pStr, iUp);
2382 pNode = &p->sParse.aNode[i];
2383 pUp = &p->sParse.aNode[iUp];
2384 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002385 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
2386 testcase( pUp->eU==0 );
drh4af352d2015-08-21 20:02:48 +00002387 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
2388 }else{
2389 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00002390 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00002391 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00002392 assert( pNode->jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00002393 assert( pNode->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002394 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
2395 }
drhcb6c6c62015-08-19 22:47:17 +00002396}
2397
2398/* Return the value of a column */
2399static int jsonEachColumn(
2400 sqlite3_vtab_cursor *cur, /* The cursor */
2401 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2402 int i /* Which column to return */
2403){
2404 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00002405 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00002406 switch( i ){
2407 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00002408 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00002409 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00002410 jsonReturn(pThis, ctx, 0);
2411 }else if( p->eType==JSON_ARRAY ){
2412 u32 iKey;
2413 if( p->bRecursive ){
2414 if( p->iRowid==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00002415 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
drh8784eca2015-08-23 02:42:30 +00002416 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00002417 }else{
2418 iKey = p->iRowid;
2419 }
drh6fd5c1e2015-08-21 20:37:12 +00002420 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00002421 }
2422 break;
2423 }
2424 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00002425 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002426 jsonReturn(pThis, ctx, 0);
2427 break;
2428 }
2429 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00002430 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002431 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2432 break;
2433 }
2434 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00002435 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002436 if( pThis->eType>=JSON_ARRAY ) break;
2437 jsonReturn(pThis, ctx, 0);
2438 break;
2439 }
2440 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00002441 sqlite3_result_int64(ctx,
2442 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00002443 break;
2444 }
2445 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00002446 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00002447 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00002448 }
2449 break;
2450 }
drh4af352d2015-08-21 20:02:48 +00002451 case JEACH_FULLKEY: {
2452 JsonString x;
2453 jsonInit(&x, ctx);
2454 if( p->bRecursive ){
2455 jsonEachComputePath(p, &x, p->i);
2456 }else{
drh383de692015-09-10 17:20:57 +00002457 if( p->zRoot ){
2458 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00002459 }else{
2460 jsonAppendChar(&x, '$');
2461 }
2462 if( p->eType==JSON_ARRAY ){
2463 jsonPrintf(30, &x, "[%d]", p->iRowid);
drhdd7460f2018-05-16 12:19:11 +00002464 }else if( p->eType==JSON_OBJECT ){
drh285f2ef2021-10-15 16:15:04 +00002465 assert( pThis->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002466 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2467 }
2468 }
2469 jsonResult(&x);
2470 break;
2471 }
drhcb6c6c62015-08-19 22:47:17 +00002472 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00002473 if( p->bRecursive ){
2474 JsonString x;
2475 jsonInit(&x, ctx);
2476 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2477 jsonResult(&x);
2478 break;
drh4af352d2015-08-21 20:02:48 +00002479 }
drh383de692015-09-10 17:20:57 +00002480 /* For json_each() path and root are the same so fall through
2481 ** into the root case */
drh08b92082020-08-10 14:18:00 +00002482 /* no break */ deliberate_fall_through
drh383de692015-09-10 17:20:57 +00002483 }
drh6850a632016-11-07 18:18:08 +00002484 default: {
drh383de692015-09-10 17:20:57 +00002485 const char *zRoot = p->zRoot;
drh6850a632016-11-07 18:18:08 +00002486 if( zRoot==0 ) zRoot = "$";
drh383de692015-09-10 17:20:57 +00002487 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00002488 break;
2489 }
drh3d1d2a92015-09-22 01:15:49 +00002490 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00002491 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00002492 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
2493 break;
2494 }
2495 }
2496 return SQLITE_OK;
2497}
2498
2499/* Return the current rowid value */
2500static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2501 JsonEachCursor *p = (JsonEachCursor*)cur;
2502 *pRowid = p->iRowid;
2503 return SQLITE_OK;
2504}
2505
2506/* The query strategy is to look for an equality constraint on the json
2507** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00002508** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00002509** and 0 otherwise.
2510*/
2511static int jsonEachBestIndex(
2512 sqlite3_vtab *tab,
2513 sqlite3_index_info *pIdxInfo
2514){
drh43579192018-11-16 16:04:50 +00002515 int i; /* Loop counter or computed array index */
2516 int aIdx[2]; /* Index of constraints for JSON and ROOT */
2517 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
2518 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
drhcb6c6c62015-08-19 22:47:17 +00002519 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00002520
drh43579192018-11-16 16:04:50 +00002521 /* This implementation assumes that JSON and ROOT are the last two
2522 ** columns in the table */
2523 assert( JEACH_ROOT == JEACH_JSON+1 );
drh6fd5c1e2015-08-21 20:37:12 +00002524 UNUSED_PARAM(tab);
drh43579192018-11-16 16:04:50 +00002525 aIdx[0] = aIdx[1] = -1;
drhcb6c6c62015-08-19 22:47:17 +00002526 pConstraint = pIdxInfo->aConstraint;
2527 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
drh43579192018-11-16 16:04:50 +00002528 int iCol;
2529 int iMask;
2530 if( pConstraint->iColumn < JEACH_JSON ) continue;
2531 iCol = pConstraint->iColumn - JEACH_JSON;
2532 assert( iCol==0 || iCol==1 );
drh285f2ef2021-10-15 16:15:04 +00002533 testcase( iCol==0 );
drh43579192018-11-16 16:04:50 +00002534 iMask = 1 << iCol;
2535 if( pConstraint->usable==0 ){
2536 unusableMask |= iMask;
2537 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
2538 aIdx[iCol] = i;
2539 idxMask |= iMask;
drhcb6c6c62015-08-19 22:47:17 +00002540 }
2541 }
drh43579192018-11-16 16:04:50 +00002542 if( (unusableMask & ~idxMask)!=0 ){
2543 /* If there are any unusable constraints on JSON or ROOT, then reject
2544 ** this entire plan */
2545 return SQLITE_CONSTRAINT;
2546 }
2547 if( aIdx[0]<0 ){
2548 /* No JSON input. Leave estimatedCost at the huge value that it was
2549 ** initialized to to discourage the query planner from selecting this
2550 ** plan. */
drhcb6c6c62015-08-19 22:47:17 +00002551 pIdxInfo->idxNum = 0;
drhcb6c6c62015-08-19 22:47:17 +00002552 }else{
drh505ad2c2015-08-21 17:33:11 +00002553 pIdxInfo->estimatedCost = 1.0;
drh43579192018-11-16 16:04:50 +00002554 i = aIdx[0];
2555 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
2556 pIdxInfo->aConstraintUsage[i].omit = 1;
2557 if( aIdx[1]<0 ){
2558 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
drhcb6c6c62015-08-19 22:47:17 +00002559 }else{
drh43579192018-11-16 16:04:50 +00002560 i = aIdx[1];
2561 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
2562 pIdxInfo->aConstraintUsage[i].omit = 1;
2563 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
drhcb6c6c62015-08-19 22:47:17 +00002564 }
2565 }
2566 return SQLITE_OK;
2567}
2568
2569/* Start a search on a new JSON string */
2570static int jsonEachFilter(
2571 sqlite3_vtab_cursor *cur,
2572 int idxNum, const char *idxStr,
2573 int argc, sqlite3_value **argv
2574){
2575 JsonEachCursor *p = (JsonEachCursor*)cur;
2576 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00002577 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002578 sqlite3_int64 n;
2579
drh6fd5c1e2015-08-21 20:37:12 +00002580 UNUSED_PARAM(idxStr);
2581 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002582 jsonEachCursorReset(p);
2583 if( idxNum==0 ) return SQLITE_OK;
2584 z = (const char*)sqlite3_value_text(argv[0]);
2585 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002586 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002587 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002588 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002589 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002590 if( jsonParse(&p->sParse, 0, p->zJson) ){
2591 int rc = SQLITE_NOMEM;
2592 if( p->sParse.oom==0 ){
2593 sqlite3_free(cur->pVtab->zErrMsg);
2594 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2595 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2596 }
drhcb6c6c62015-08-19 22:47:17 +00002597 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002598 return rc;
2599 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2600 jsonEachCursorReset(p);
2601 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002602 }else{
drh95677942015-09-24 01:06:37 +00002603 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002604 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002605 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002606 zRoot = (const char*)sqlite3_value_text(argv[1]);
2607 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002608 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002609 p->zRoot = sqlite3_malloc64( n+1 );
2610 if( p->zRoot==0 ) return SQLITE_NOMEM;
2611 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002612 if( zRoot[0]!='$' ){
2613 zErr = zRoot;
2614 }else{
2615 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2616 }
2617 if( zErr ){
drha7714022015-08-29 00:54:49 +00002618 sqlite3_free(cur->pVtab->zErrMsg);
2619 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002620 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002621 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2622 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002623 return SQLITE_OK;
2624 }
2625 }else{
2626 pNode = p->sParse.aNode;
2627 }
drh852944e2015-09-10 03:29:11 +00002628 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002629 p->eType = pNode->eType;
2630 if( p->eType>=JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002631 assert( pNode->eU==0 );
2632 VVA( pNode->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002633 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002634 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002635 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002636 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002637 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2638 p->i--;
2639 }
2640 }else{
2641 p->i++;
2642 }
drhcb6c6c62015-08-19 22:47:17 +00002643 }else{
2644 p->iEnd = p->i+1;
2645 }
2646 }
drha8f39a92015-09-21 22:53:16 +00002647 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002648}
2649
2650/* The methods of the json_each virtual table */
2651static sqlite3_module jsonEachModule = {
2652 0, /* iVersion */
2653 0, /* xCreate */
2654 jsonEachConnect, /* xConnect */
2655 jsonEachBestIndex, /* xBestIndex */
2656 jsonEachDisconnect, /* xDisconnect */
2657 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002658 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002659 jsonEachClose, /* xClose - close a cursor */
2660 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002661 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002662 jsonEachEof, /* xEof - check for end of scan */
2663 jsonEachColumn, /* xColumn - read data */
2664 jsonEachRowid, /* xRowid - read data */
2665 0, /* xUpdate */
2666 0, /* xBegin */
2667 0, /* xSync */
2668 0, /* xCommit */
2669 0, /* xRollback */
2670 0, /* xFindMethod */
2671 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002672 0, /* xSavepoint */
2673 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002674 0, /* xRollbackTo */
2675 0 /* xShadowName */
drhcb6c6c62015-08-19 22:47:17 +00002676};
2677
drh505ad2c2015-08-21 17:33:11 +00002678/* The methods of the json_tree virtual table. */
2679static sqlite3_module jsonTreeModule = {
2680 0, /* iVersion */
2681 0, /* xCreate */
2682 jsonEachConnect, /* xConnect */
2683 jsonEachBestIndex, /* xBestIndex */
2684 jsonEachDisconnect, /* xDisconnect */
2685 0, /* xDestroy */
2686 jsonEachOpenTree, /* xOpen - open a cursor */
2687 jsonEachClose, /* xClose - close a cursor */
2688 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002689 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002690 jsonEachEof, /* xEof - check for end of scan */
2691 jsonEachColumn, /* xColumn - read data */
2692 jsonEachRowid, /* xRowid - read data */
2693 0, /* xUpdate */
2694 0, /* xBegin */
2695 0, /* xSync */
2696 0, /* xCommit */
2697 0, /* xRollback */
2698 0, /* xFindMethod */
2699 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002700 0, /* xSavepoint */
2701 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002702 0, /* xRollbackTo */
2703 0 /* xShadowName */
drh505ad2c2015-08-21 17:33:11 +00002704};
drhd2975922015-08-29 17:22:33 +00002705#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002706
2707/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002708** The following routines are the only publically visible identifiers in this
2709** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002710** functions and the virtual table implemented by this file.
2711****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002712
drh2f20e132015-09-26 17:44:59 +00002713int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002714 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002715 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002716 static const struct {
2717 const char *zName;
drhdc60c682022-01-08 15:05:53 +00002718 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drh5fa5c102015-08-12 16:49:40 +00002719 int nArg;
drh52216ad2015-08-18 02:28:03 +00002720 int flag;
drh5fa5c102015-08-12 16:49:40 +00002721 } aFunc[] = {
drhdc60c682022-01-08 15:05:53 +00002722 { "json", jsonRemoveFunc, 1, 0 },
2723 { "json_array", jsonArrayFunc, -1, 0 },
2724 { "json_array_length", jsonArrayLengthFunc, 1, 0 },
2725 { "json_array_length", jsonArrayLengthFunc, 2, 0 },
2726 { "json_extract", jsonExtractFunc, -1, 0 },
2727 { "json_nextract", jsonExtractFunc, -1, JSON_NULLERR },
2728 { "->", jsonExtractFunc, 2, JSON_NULLERR|JSON_ABPATH },
2729 { "->>", jsonExtractFunc, 2, JSON_ABPATH },
2730 { "json_insert", jsonSetFunc, -1, 0 },
2731 { "json_object", jsonObjectFunc, -1, 0 },
2732 { "json_patch", jsonPatchFunc, 2, 0 },
2733 { "json_quote", jsonQuoteFunc, 1, 0 },
2734 { "json_remove", jsonRemoveFunc, -1, 0 },
2735 { "json_replace", jsonReplaceFunc, -1, 0 },
2736 { "json_set", jsonSetFunc, -1, JSON_ISSET },
2737 { "json_type", jsonTypeFunc, 1, 0 },
2738 { "json_ntype", jsonTypeFunc, 1, JSON_NULLERR },
2739 { "json_type", jsonTypeFunc, 2, 0 },
2740 { "json_valid", jsonValidFunc, 1, 0 },
drh987eb1f2015-08-17 15:17:37 +00002741
drh301eecc2015-08-17 20:14:19 +00002742#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002743 /* DEBUG and TESTING functions */
drhdc60c682022-01-08 15:05:53 +00002744 { "json_parse", jsonParseFunc, 1, 0 },
2745 { "json_test1", jsonTest1Func, 1, 0 },
drh301eecc2015-08-17 20:14:19 +00002746#endif
drh5fa5c102015-08-12 16:49:40 +00002747 };
drhff135ae2015-12-30 01:07:02 +00002748 static const struct {
2749 const char *zName;
2750 int nArg;
2751 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2752 void (*xFinal)(sqlite3_context*);
drh8be47a72018-07-05 20:05:29 +00002753 void (*xValue)(sqlite3_context*);
drhff135ae2015-12-30 01:07:02 +00002754 } aAgg[] = {
drh8be47a72018-07-05 20:05:29 +00002755 { "json_group_array", 1,
2756 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
2757 { "json_group_object", 2,
2758 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
drhff135ae2015-12-30 01:07:02 +00002759 };
drhd2975922015-08-29 17:22:33 +00002760#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002761 static const struct {
2762 const char *zName;
2763 sqlite3_module *pModule;
2764 } aMod[] = {
2765 { "json_each", &jsonEachModule },
2766 { "json_tree", &jsonTreeModule },
2767 };
drhd2975922015-08-29 17:22:33 +00002768#endif
drh79d5bc82020-01-04 01:43:02 +00002769 static const int enc =
2770 SQLITE_UTF8 |
2771 SQLITE_DETERMINISTIC |
2772 SQLITE_INNOCUOUS;
drh5fa5c102015-08-12 16:49:40 +00002773 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
drh79d5bc82020-01-04 01:43:02 +00002774 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
dan01a3b6b2019-09-13 17:05:48 +00002775 (void*)&aFunc[i].flag,
2776 aFunc[i].xFunc, 0, 0);
drh5fa5c102015-08-12 16:49:40 +00002777 }
mistachkin5a193dd2018-07-24 13:57:44 +00002778#ifndef SQLITE_OMIT_WINDOWFUNC
drhff135ae2015-12-30 01:07:02 +00002779 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
drh8be47a72018-07-05 20:05:29 +00002780 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
drh79d5bc82020-01-04 01:43:02 +00002781 SQLITE_SUBTYPE | enc, 0,
drh8be47a72018-07-05 20:05:29 +00002782 aAgg[i].xStep, aAgg[i].xFinal,
2783 aAgg[i].xValue, jsonGroupInverse, 0);
drhff135ae2015-12-30 01:07:02 +00002784 }
mistachkin5a193dd2018-07-24 13:57:44 +00002785#endif
drhd2975922015-08-29 17:22:33 +00002786#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002787 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2788 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002789 }
drhd2975922015-08-29 17:22:33 +00002790#endif
drh5fa5c102015-08-12 16:49:40 +00002791 return rc;
2792}
drh2f20e132015-09-26 17:44:59 +00002793
2794
dan8d32e802015-10-14 18:45:42 +00002795#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002796#ifdef _WIN32
2797__declspec(dllexport)
2798#endif
2799int sqlite3_json_init(
2800 sqlite3 *db,
2801 char **pzErrMsg,
2802 const sqlite3_api_routines *pApi
2803){
2804 SQLITE_EXTENSION_INIT2(pApi);
2805 (void)pzErrMsg; /* Unused parameter */
2806 return sqlite3Json1Init(db);
2807}
dan8d32e802015-10-14 18:45:42 +00002808#endif
drh50065652015-10-08 19:29:18 +00002809#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */