blob: 7fcd7342a611778b1b747cd86c1d21c65ac9a4af [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
29#include <assert.h>
30#include <string.h>
drh987eb1f2015-08-17 15:17:37 +000031#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000032#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000033
drhdf3a9072016-02-11 15:37:18 +000034/* Mark a function parameter as unused, to suppress nuisance compiler
35** warnings. */
36#ifndef UNUSED_PARAM
37# define UNUSED_PARAM(X) (void)(X)
38#endif
drh6fd5c1e2015-08-21 20:37:12 +000039
drh8deb4b82015-10-09 18:21:43 +000040#ifndef LARGEST_INT64
41# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
42# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
43#endif
44
drh08b92082020-08-10 14:18:00 +000045#ifndef deliberate_fall_through
46# define deliberate_fall_through
47#endif
48
dan2e8f5512015-09-17 17:21:09 +000049/*
50** Versions of isspace(), isalnum() and isdigit() to which it is safe
51** to pass signed char values.
52*/
drh49472652015-10-16 15:35:39 +000053#ifdef sqlite3Isdigit
54 /* Use the SQLite core versions if this routine is part of the
55 ** SQLite amalgamation */
drhad875e72016-11-07 13:37:28 +000056# define safe_isdigit(x) sqlite3Isdigit(x)
57# define safe_isalnum(x) sqlite3Isalnum(x)
58# define safe_isxdigit(x) sqlite3Isxdigit(x)
drh49472652015-10-16 15:35:39 +000059#else
60 /* Use the standard library for separate compilation */
61#include <ctype.h> /* amalgamator: keep */
drhad875e72016-11-07 13:37:28 +000062# define safe_isdigit(x) isdigit((unsigned char)(x))
63# define safe_isalnum(x) isalnum((unsigned char)(x))
64# define safe_isxdigit(x) isxdigit((unsigned char)(x))
drh49472652015-10-16 15:35:39 +000065#endif
dan2e8f5512015-09-17 17:21:09 +000066
drh95677942015-09-24 01:06:37 +000067/*
68** Growing our own isspace() routine this way is twice as fast as
69** the library isspace() function, resulting in a 7% overall performance
70** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
71*/
72static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000073 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 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};
90#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
91
drh9a4718f2015-10-10 14:00:37 +000092#ifndef SQLITE_AMALGAMATION
93 /* Unsigned integer types. These are already defined in the sqliteInt.h,
94 ** but the definitions need to be repeated for separate compilation. */
95 typedef sqlite3_uint64 u64;
96 typedef unsigned int u32;
drhff6d50e2017-04-11 18:55:05 +000097 typedef unsigned short int u16;
drh9a4718f2015-10-10 14:00:37 +000098 typedef unsigned char u8;
drh6a726fa2021-10-05 15:30:52 +000099# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
100# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
101# endif
102# if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
103# define ALWAYS(X) (1)
104# define NEVER(X) (0)
105# elif !defined(NDEBUG)
106# define ALWAYS(X) ((X)?1:(assert(0),0))
107# define NEVER(X) ((X)?(assert(0),1):0)
108# else
109# define ALWAYS(X) (X)
110# define NEVER(X) (X)
111# endif
drh285f2ef2021-10-15 16:15:04 +0000112# define testcase(X)
drh9a4718f2015-10-10 14:00:37 +0000113#endif
drh16fc5e62021-11-01 12:53:01 +0000114#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
drh285f2ef2021-10-15 16:15:04 +0000115# define VVA(X)
116#else
117# define VVA(X) X
118#endif
119
120/*
121** Some of the testcase() macros in this file are problematic for gcov
122** in that they generate false-miss errors randomly. This is a gcov problem,
123** not a problem in this case. But to work around it, we disable the
124** problematic test cases for production builds.
125*/
126#define json_testcase(X)
drh5fa5c102015-08-12 16:49:40 +0000127
drh52216ad2015-08-18 02:28:03 +0000128/* Objects */
drh505ad2c2015-08-21 17:33:11 +0000129typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +0000130typedef struct JsonNode JsonNode;
131typedef struct JsonParse JsonParse;
132
drh5634cc02015-08-17 11:28:03 +0000133/* An instance of this object represents a JSON string
134** under construction. Really, this is a generic string accumulator
135** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000136*/
drh505ad2c2015-08-21 17:33:11 +0000137struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000138 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000139 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000140 u64 nAlloc; /* Bytes of storage available in zBuf[] */
141 u64 nUsed; /* Bytes of zBuf[] currently used */
142 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000143 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000144 char zSpace[100]; /* Initial static space */
145};
146
drhe9c37f32015-08-15 21:25:36 +0000147/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000148*/
drhe9c37f32015-08-15 21:25:36 +0000149#define JSON_NULL 0
150#define JSON_TRUE 1
151#define JSON_FALSE 2
152#define JSON_INT 3
153#define JSON_REAL 4
154#define JSON_STRING 5
155#define JSON_ARRAY 6
156#define JSON_OBJECT 7
157
drhf5ddb9c2015-09-11 00:06:41 +0000158/* The "subtype" set for JSON values */
159#define JSON_SUBTYPE 74 /* Ascii for "J" */
160
drh987eb1f2015-08-17 15:17:37 +0000161/*
162** Names of the various JSON types:
163*/
164static const char * const jsonType[] = {
165 "null", "true", "false", "integer", "real", "text", "array", "object"
166};
167
drh301eecc2015-08-17 20:14:19 +0000168/* Bit values for the JsonNode.jnFlag field
169*/
170#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
171#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
172#define JNODE_REMOVE 0x04 /* Do not output */
drh633647a2017-03-22 21:24:31 +0000173#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
174#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
175#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
176#define JNODE_LABEL 0x40 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000177
drh987eb1f2015-08-17 15:17:37 +0000178
drhe9c37f32015-08-15 21:25:36 +0000179/* A single node of parsed JSON
180*/
drhe9c37f32015-08-15 21:25:36 +0000181struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000182 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000183 u8 jnFlags; /* JNODE flags */
drh285f2ef2021-10-15 16:15:04 +0000184 u8 eU; /* Which union element to use */
drhe9c37f32015-08-15 21:25:36 +0000185 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000186 union {
drh285f2ef2021-10-15 16:15:04 +0000187 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
188 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
189 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
190 u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
191 JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
drh52216ad2015-08-18 02:28:03 +0000192 } u;
drhe9c37f32015-08-15 21:25:36 +0000193};
194
195/* A completely parsed JSON string
196*/
drhe9c37f32015-08-15 21:25:36 +0000197struct JsonParse {
198 u32 nNode; /* Number of slots of aNode[] used */
199 u32 nAlloc; /* Number of slots of aNode[] allocated */
200 JsonNode *aNode; /* Array of nodes containing the parse */
201 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000202 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000203 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000204 u8 nErr; /* Number of errors seen */
drhff6d50e2017-04-11 18:55:05 +0000205 u16 iDepth; /* Nesting depth */
drh3fb153c2017-05-11 16:49:59 +0000206 int nJson; /* Length of the zJson string in bytes */
drhe35fc302018-08-30 01:52:10 +0000207 u32 iHold; /* Replace cache line with the lowest iHold value */
drhe9c37f32015-08-15 21:25:36 +0000208};
209
drhff6d50e2017-04-11 18:55:05 +0000210/*
211** Maximum nesting depth of JSON for this implementation.
212**
213** This limit is needed to avoid a stack overflow in the recursive
214** descent parser. A depth of 2000 is far deeper than any sane JSON
215** should go.
216*/
217#define JSON_MAX_DEPTH 2000
218
drh505ad2c2015-08-21 17:33:11 +0000219/**************************************************************************
220** Utility routines for dealing with JsonString objects
221**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000222
drh505ad2c2015-08-21 17:33:11 +0000223/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000224*/
drh505ad2c2015-08-21 17:33:11 +0000225static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000226 p->zBuf = p->zSpace;
227 p->nAlloc = sizeof(p->zSpace);
228 p->nUsed = 0;
229 p->bStatic = 1;
230}
231
drh505ad2c2015-08-21 17:33:11 +0000232/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000233*/
drh505ad2c2015-08-21 17:33:11 +0000234static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000235 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000236 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000237 jsonZero(p);
238}
239
240
drh505ad2c2015-08-21 17:33:11 +0000241/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000242** initial state.
243*/
drh505ad2c2015-08-21 17:33:11 +0000244static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000245 if( !p->bStatic ) sqlite3_free(p->zBuf);
246 jsonZero(p);
247}
248
249
250/* Report an out-of-memory (OOM) condition
251*/
drh505ad2c2015-08-21 17:33:11 +0000252static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000253 p->bErr = 1;
254 sqlite3_result_error_nomem(p->pCtx);
255 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000256}
257
258/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
259** Return zero on success. Return non-zero on an OOM error
260*/
drh505ad2c2015-08-21 17:33:11 +0000261static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000262 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000263 char *zNew;
264 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000265 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000266 zNew = sqlite3_malloc64(nTotal);
267 if( zNew==0 ){
268 jsonOom(p);
269 return SQLITE_NOMEM;
270 }
drh6fd5c1e2015-08-21 20:37:12 +0000271 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000272 p->zBuf = zNew;
273 p->bStatic = 0;
274 }else{
275 zNew = sqlite3_realloc64(p->zBuf, nTotal);
276 if( zNew==0 ){
277 jsonOom(p);
278 return SQLITE_NOMEM;
279 }
280 p->zBuf = zNew;
281 }
282 p->nAlloc = nTotal;
283 return SQLITE_OK;
284}
285
drh505ad2c2015-08-21 17:33:11 +0000286/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000287*/
drh505ad2c2015-08-21 17:33:11 +0000288static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drhc795e3d2020-05-17 13:47:28 +0000289 if( N==0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000290 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
291 memcpy(p->zBuf+p->nUsed, zIn, N);
292 p->nUsed += N;
293}
294
drh4af352d2015-08-21 20:02:48 +0000295/* Append formatted text (not to exceed N bytes) to the JsonString.
296*/
297static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
298 va_list ap;
299 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
300 va_start(ap, zFormat);
301 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
302 va_end(ap);
303 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
304}
305
drh5634cc02015-08-17 11:28:03 +0000306/* Append a single character
307*/
drh505ad2c2015-08-21 17:33:11 +0000308static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000309 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
310 p->zBuf[p->nUsed++] = c;
311}
312
drh301eecc2015-08-17 20:14:19 +0000313/* Append a comma separator to the output buffer, if the previous
314** character is not '[' or '{'.
315*/
drh505ad2c2015-08-21 17:33:11 +0000316static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000317 char c;
318 if( p->nUsed==0 ) return;
319 c = p->zBuf[p->nUsed-1];
320 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
321}
322
drh505ad2c2015-08-21 17:33:11 +0000323/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000324** under construction. Enclose the string in "..." and escape
325** any double-quotes or backslash characters contained within the
326** string.
327*/
drh505ad2c2015-08-21 17:33:11 +0000328static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000329 u32 i;
drh76baad92021-04-30 16:12:40 +0000330 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
drh5fa5c102015-08-12 16:49:40 +0000331 p->zBuf[p->nUsed++] = '"';
332 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000333 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000334 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000335 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000336 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000337 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000338 }else if( c<=0x1f ){
339 static const char aSpecial[] = {
340 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
341 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
342 };
343 assert( sizeof(aSpecial)==32 );
344 assert( aSpecial['\b']=='b' );
345 assert( aSpecial['\f']=='f' );
346 assert( aSpecial['\n']=='n' );
347 assert( aSpecial['\r']=='r' );
348 assert( aSpecial['\t']=='t' );
349 if( aSpecial[c] ){
350 c = aSpecial[c];
351 goto json_simple_escape;
352 }
353 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
354 p->zBuf[p->nUsed++] = '\\';
355 p->zBuf[p->nUsed++] = 'u';
356 p->zBuf[p->nUsed++] = '0';
357 p->zBuf[p->nUsed++] = '0';
358 p->zBuf[p->nUsed++] = '0' + (c>>4);
359 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000360 }
361 p->zBuf[p->nUsed++] = c;
362 }
363 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000364 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000365}
366
drhd0960592015-08-17 21:22:32 +0000367/*
368** Append a function parameter value to the JSON string under
369** construction.
370*/
371static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000372 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000373 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000374){
375 switch( sqlite3_value_type(pValue) ){
376 case SQLITE_NULL: {
377 jsonAppendRaw(p, "null", 4);
378 break;
379 }
380 case SQLITE_INTEGER:
381 case SQLITE_FLOAT: {
382 const char *z = (const char*)sqlite3_value_text(pValue);
383 u32 n = (u32)sqlite3_value_bytes(pValue);
384 jsonAppendRaw(p, z, n);
385 break;
386 }
387 case SQLITE_TEXT: {
388 const char *z = (const char*)sqlite3_value_text(pValue);
389 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000390 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000391 jsonAppendRaw(p, z, n);
392 }else{
393 jsonAppendString(p, z, n);
394 }
drhd0960592015-08-17 21:22:32 +0000395 break;
396 }
397 default: {
398 if( p->bErr==0 ){
399 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000400 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000401 jsonReset(p);
402 }
403 break;
404 }
405 }
406}
407
408
drhbd0621b2015-08-13 13:54:59 +0000409/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000410*/
drh505ad2c2015-08-21 17:33:11 +0000411static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000412 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000413 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
414 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
415 SQLITE_UTF8);
416 jsonZero(p);
417 }
418 assert( p->bStatic );
419}
420
drh505ad2c2015-08-21 17:33:11 +0000421/**************************************************************************
422** Utility routines for dealing with JsonNode and JsonParse objects
423**************************************************************************/
424
425/*
426** Return the number of consecutive JsonNode slots need to represent
427** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
428** OBJECT types, the number might be larger.
429**
430** Appended elements are not counted. The value returned is the number
431** by which the JsonNode counter should increment in order to go to the
432** next peer value.
433*/
434static u32 jsonNodeSize(JsonNode *pNode){
435 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
436}
437
438/*
439** Reclaim all memory allocated by a JsonParse object. But do not
440** delete the JsonParse object itself.
441*/
442static void jsonParseReset(JsonParse *pParse){
443 sqlite3_free(pParse->aNode);
444 pParse->aNode = 0;
445 pParse->nNode = 0;
446 pParse->nAlloc = 0;
447 sqlite3_free(pParse->aUp);
448 pParse->aUp = 0;
449}
450
drh5634cc02015-08-17 11:28:03 +0000451/*
drh3fb153c2017-05-11 16:49:59 +0000452** Free a JsonParse object that was obtained from sqlite3_malloc().
453*/
454static void jsonParseFree(JsonParse *pParse){
455 jsonParseReset(pParse);
456 sqlite3_free(pParse);
457}
458
459/*
drh5634cc02015-08-17 11:28:03 +0000460** Convert the JsonNode pNode into a pure JSON string and
461** append to pOut. Subsubstructure is also included. Return
462** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000463*/
drh52216ad2015-08-18 02:28:03 +0000464static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000465 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000466 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000467 sqlite3_value **aReplace /* Replacement values */
468){
drh7d4c94b2021-10-04 22:34:38 +0000469 assert( pNode!=0 );
drh633647a2017-03-22 21:24:31 +0000470 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
drh7d4c94b2021-10-04 22:34:38 +0000471 if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){
drh285f2ef2021-10-15 16:15:04 +0000472 assert( pNode->eU==4 );
drh633647a2017-03-22 21:24:31 +0000473 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
474 return;
475 }
drh285f2ef2021-10-15 16:15:04 +0000476 assert( pNode->eU==5 );
drh633647a2017-03-22 21:24:31 +0000477 pNode = pNode->u.pPatch;
478 }
drh5634cc02015-08-17 11:28:03 +0000479 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000480 default: {
481 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000482 jsonAppendRaw(pOut, "null", 4);
483 break;
484 }
485 case JSON_TRUE: {
486 jsonAppendRaw(pOut, "true", 4);
487 break;
488 }
489 case JSON_FALSE: {
490 jsonAppendRaw(pOut, "false", 5);
491 break;
492 }
493 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000494 if( pNode->jnFlags & JNODE_RAW ){
drh285f2ef2021-10-15 16:15:04 +0000495 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000496 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000497 break;
498 }
drh08b92082020-08-10 14:18:00 +0000499 /* no break */ deliberate_fall_through
drh5634cc02015-08-17 11:28:03 +0000500 }
501 case JSON_REAL:
502 case JSON_INT: {
drh285f2ef2021-10-15 16:15:04 +0000503 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000504 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000505 break;
506 }
507 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000508 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000509 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000510 for(;;){
511 while( j<=pNode->n ){
drh633647a2017-03-22 21:24:31 +0000512 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +0000513 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000514 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000515 }
drh505ad2c2015-08-21 17:33:11 +0000516 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000517 }
drh52216ad2015-08-18 02:28:03 +0000518 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +0000519 assert( pNode->eU==2 );
drh52216ad2015-08-18 02:28:03 +0000520 pNode = &pNode[pNode->u.iAppend];
521 j = 1;
drh5634cc02015-08-17 11:28:03 +0000522 }
523 jsonAppendChar(pOut, ']');
524 break;
525 }
526 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000527 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000528 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000529 for(;;){
530 while( j<=pNode->n ){
531 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
532 jsonAppendSeparator(pOut);
533 jsonRenderNode(&pNode[j], pOut, aReplace);
534 jsonAppendChar(pOut, ':');
drh633647a2017-03-22 21:24:31 +0000535 jsonRenderNode(&pNode[j+1], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000536 }
drh505ad2c2015-08-21 17:33:11 +0000537 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000538 }
drh52216ad2015-08-18 02:28:03 +0000539 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +0000540 assert( pNode->eU==2 );
drh52216ad2015-08-18 02:28:03 +0000541 pNode = &pNode[pNode->u.iAppend];
542 j = 1;
drh5634cc02015-08-17 11:28:03 +0000543 }
544 jsonAppendChar(pOut, '}');
545 break;
546 }
drhbd0621b2015-08-13 13:54:59 +0000547 }
drh5634cc02015-08-17 11:28:03 +0000548}
549
550/*
drhf2df7e72015-08-28 20:07:40 +0000551** Return a JsonNode and all its descendents as a JSON string.
552*/
553static void jsonReturnJson(
554 JsonNode *pNode, /* Node to return */
555 sqlite3_context *pCtx, /* Return value for this function */
556 sqlite3_value **aReplace /* Array of replacement values */
557){
558 JsonString s;
559 jsonInit(&s, pCtx);
560 jsonRenderNode(pNode, &s, aReplace);
561 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000562 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000563}
564
565/*
drh48eb03b2019-11-10 11:09:06 +0000566** Translate a single byte of Hex into an integer.
567** This routine only works if h really is a valid hexadecimal
568** character: 0..9a..fA..F
569*/
570static u8 jsonHexToInt(int h){
571 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
572#ifdef SQLITE_EBCDIC
573 h += 9*(1&~(h>>4));
574#else
575 h += 9*(1&(h>>6));
576#endif
577 return (u8)(h & 0xf);
578}
579
580/*
581** Convert a 4-byte hex string into an integer
582*/
583static u32 jsonHexToInt4(const char *z){
584 u32 v;
585 assert( safe_isxdigit(z[0]) );
586 assert( safe_isxdigit(z[1]) );
587 assert( safe_isxdigit(z[2]) );
588 assert( safe_isxdigit(z[3]) );
589 v = (jsonHexToInt(z[0])<<12)
590 + (jsonHexToInt(z[1])<<8)
591 + (jsonHexToInt(z[2])<<4)
592 + jsonHexToInt(z[3]);
593 return v;
594}
595
596/*
drh5634cc02015-08-17 11:28:03 +0000597** Make the JsonNode the return value of the function.
598*/
drhd0960592015-08-17 21:22:32 +0000599static void jsonReturn(
600 JsonNode *pNode, /* Node to return */
601 sqlite3_context *pCtx, /* Return value for this function */
602 sqlite3_value **aReplace /* Array of replacement values */
603){
drh5634cc02015-08-17 11:28:03 +0000604 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000605 default: {
606 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000607 sqlite3_result_null(pCtx);
608 break;
609 }
610 case JSON_TRUE: {
611 sqlite3_result_int(pCtx, 1);
612 break;
613 }
614 case JSON_FALSE: {
615 sqlite3_result_int(pCtx, 0);
616 break;
617 }
drh987eb1f2015-08-17 15:17:37 +0000618 case JSON_INT: {
619 sqlite3_int64 i = 0;
drh285f2ef2021-10-15 16:15:04 +0000620 const char *z;
621 assert( pNode->eU==1 );
622 z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000623 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000624 while( z[0]>='0' && z[0]<='9' ){
625 unsigned v = *(z++) - '0';
626 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000627 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000628 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
629 if( v==9 ) goto int_as_real;
630 if( v==8 ){
631 if( pNode->u.zJContent[0]=='-' ){
632 sqlite3_result_int64(pCtx, SMALLEST_INT64);
633 goto int_done;
634 }else{
635 goto int_as_real;
636 }
637 }
638 }
639 i = i*10 + v;
640 }
drh52216ad2015-08-18 02:28:03 +0000641 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000642 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000643 int_done:
644 break;
drhe85e1da2021-10-01 21:01:07 +0000645 int_as_real: ; /* no break */ deliberate_fall_through
drh8deb4b82015-10-09 18:21:43 +0000646 }
647 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000648 double r;
649#ifdef SQLITE_AMALGAMATION
drh285f2ef2021-10-15 16:15:04 +0000650 const char *z;
651 assert( pNode->eU==1 );
652 z = pNode->u.zJContent;
drh49472652015-10-16 15:35:39 +0000653 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
654#else
drh285f2ef2021-10-15 16:15:04 +0000655 assert( pNode->eU==1 );
drh49472652015-10-16 15:35:39 +0000656 r = strtod(pNode->u.zJContent, 0);
657#endif
drh8deb4b82015-10-09 18:21:43 +0000658 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000659 break;
660 }
drh5634cc02015-08-17 11:28:03 +0000661 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000662#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
663 ** json_insert() and json_replace() and those routines do not
664 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000665 if( pNode->jnFlags & JNODE_RAW ){
drh285f2ef2021-10-15 16:15:04 +0000666 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000667 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
668 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000669 }else
670#endif
671 assert( (pNode->jnFlags & JNODE_RAW)==0 );
672 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000673 /* JSON formatted without any backslash-escapes */
drh285f2ef2021-10-15 16:15:04 +0000674 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000675 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000676 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000677 }else{
678 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000679 u32 i;
680 u32 n = pNode->n;
drh285f2ef2021-10-15 16:15:04 +0000681 const char *z;
drh987eb1f2015-08-17 15:17:37 +0000682 char *zOut;
683 u32 j;
drh285f2ef2021-10-15 16:15:04 +0000684 assert( pNode->eU==1 );
685 z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000686 zOut = sqlite3_malloc( n+1 );
687 if( zOut==0 ){
688 sqlite3_result_error_nomem(pCtx);
689 break;
690 }
691 for(i=1, j=0; i<n-1; i++){
692 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000693 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000694 zOut[j++] = c;
695 }else{
696 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000697 if( c=='u' ){
drh48eb03b2019-11-10 11:09:06 +0000698 u32 v = jsonHexToInt4(z+i+1);
699 i += 4;
drh80d87402015-08-24 12:42:41 +0000700 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000701 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000702 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000703 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000704 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000705 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000706 }else{
drh48eb03b2019-11-10 11:09:06 +0000707 u32 vlo;
708 if( (v&0xfc00)==0xd800
709 && i<n-6
710 && z[i+1]=='\\'
711 && z[i+2]=='u'
712 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
713 ){
714 /* We have a surrogate pair */
715 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
716 i += 6;
717 zOut[j++] = 0xf0 | (v>>18);
718 zOut[j++] = 0x80 | ((v>>12)&0x3f);
719 zOut[j++] = 0x80 | ((v>>6)&0x3f);
720 zOut[j++] = 0x80 | (v&0x3f);
721 }else{
722 zOut[j++] = 0xe0 | (v>>12);
723 zOut[j++] = 0x80 | ((v>>6)&0x3f);
724 zOut[j++] = 0x80 | (v&0x3f);
725 }
drh987eb1f2015-08-17 15:17:37 +0000726 }
727 }else{
728 if( c=='b' ){
729 c = '\b';
730 }else if( c=='f' ){
731 c = '\f';
732 }else if( c=='n' ){
733 c = '\n';
734 }else if( c=='r' ){
735 c = '\r';
736 }else if( c=='t' ){
737 c = '\t';
738 }
739 zOut[j++] = c;
740 }
741 }
742 }
743 zOut[j] = 0;
744 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000745 }
746 break;
747 }
748 case JSON_ARRAY:
749 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000750 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000751 break;
752 }
753 }
drhbd0621b2015-08-13 13:54:59 +0000754}
755
drh95677942015-09-24 01:06:37 +0000756/* Forward reference */
757static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
758
759/*
760** A macro to hint to the compiler that a function should not be
761** inlined.
762*/
763#if defined(__GNUC__)
764# define JSON_NOINLINE __attribute__((noinline))
765#elif defined(_MSC_VER) && _MSC_VER>=1310
766# define JSON_NOINLINE __declspec(noinline)
767#else
768# define JSON_NOINLINE
769#endif
770
771
772static JSON_NOINLINE int jsonParseAddNodeExpand(
773 JsonParse *pParse, /* Append the node to this object */
774 u32 eType, /* Node type */
775 u32 n, /* Content size or sub-node count */
776 const char *zContent /* Content */
777){
778 u32 nNew;
779 JsonNode *pNew;
780 assert( pParse->nNode>=pParse->nAlloc );
781 if( pParse->oom ) return -1;
782 nNew = pParse->nAlloc*2 + 10;
drh2d77d802019-01-08 20:02:48 +0000783 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
drh95677942015-09-24 01:06:37 +0000784 if( pNew==0 ){
785 pParse->oom = 1;
786 return -1;
787 }
788 pParse->nAlloc = nNew;
789 pParse->aNode = pNew;
790 assert( pParse->nNode<pParse->nAlloc );
791 return jsonParseAddNode(pParse, eType, n, zContent);
792}
793
drh5fa5c102015-08-12 16:49:40 +0000794/*
drhe9c37f32015-08-15 21:25:36 +0000795** Create a new JsonNode instance based on the arguments and append that
796** instance to the JsonParse. Return the index in pParse->aNode[] of the
797** new node, or -1 if a memory allocation fails.
798*/
799static int jsonParseAddNode(
800 JsonParse *pParse, /* Append the node to this object */
801 u32 eType, /* Node type */
802 u32 n, /* Content size or sub-node count */
803 const char *zContent /* Content */
804){
805 JsonNode *p;
drhaa6fe5b2021-10-04 13:18:44 +0000806 if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000807 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000808 }
809 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000810 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000811 p->jnFlags = 0;
drh285f2ef2021-10-15 16:15:04 +0000812 VVA( p->eU = zContent ? 1 : 0 );
drhe9c37f32015-08-15 21:25:36 +0000813 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000814 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000815 return pParse->nNode++;
816}
817
818/*
drhad875e72016-11-07 13:37:28 +0000819** Return true if z[] begins with 4 (or more) hexadecimal digits
820*/
821static int jsonIs4Hex(const char *z){
822 int i;
823 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
824 return 1;
825}
826
827/*
drhe9c37f32015-08-15 21:25:36 +0000828** Parse a single JSON value which begins at pParse->zJson[i]. Return the
829** index of the first character past the end of the value parsed.
830**
831** Return negative for a syntax error. Special cases: return -2 if the
832** first non-whitespace character is '}' and return -3 if the first
833** non-whitespace character is ']'.
834*/
835static int jsonParseValue(JsonParse *pParse, u32 i){
836 char c;
837 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000838 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000839 int x;
drh852944e2015-09-10 03:29:11 +0000840 JsonNode *pNode;
drh9fa866a2017-04-08 18:18:22 +0000841 const char *z = pParse->zJson;
842 while( safe_isspace(z[i]) ){ i++; }
843 if( (c = z[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000844 /* Parse object */
845 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000846 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000847 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000848 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000849 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000850 x = jsonParseValue(pParse, j);
851 if( x<0 ){
drhff6d50e2017-04-11 18:55:05 +0000852 pParse->iDepth--;
drhf27cd1f2015-09-23 01:10:29 +0000853 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000854 return -1;
855 }
drhbe9474e2015-08-22 03:05:54 +0000856 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000857 pNode = &pParse->aNode[pParse->nNode-1];
858 if( pNode->eType!=JSON_STRING ) return -1;
859 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000860 j = x;
drh9fa866a2017-04-08 18:18:22 +0000861 while( safe_isspace(z[j]) ){ j++; }
862 if( z[j]!=':' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000863 j++;
864 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000865 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000866 if( x<0 ) return -1;
867 j = x;
drh9fa866a2017-04-08 18:18:22 +0000868 while( safe_isspace(z[j]) ){ j++; }
869 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000870 if( c==',' ) continue;
871 if( c!='}' ) return -1;
872 break;
873 }
drhbc8f0922015-08-22 19:39:04 +0000874 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000875 return j+1;
876 }else if( c=='[' ){
877 /* Parse array */
878 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000879 if( iThis<0 ) return -1;
drh285f2ef2021-10-15 16:15:04 +0000880 memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
drhe9c37f32015-08-15 21:25:36 +0000881 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000882 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000883 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000884 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000885 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000886 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000887 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000888 return -1;
889 }
890 j = x;
drh9fa866a2017-04-08 18:18:22 +0000891 while( safe_isspace(z[j]) ){ j++; }
892 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000893 if( c==',' ) continue;
894 if( c!=']' ) return -1;
895 break;
896 }
drhbc8f0922015-08-22 19:39:04 +0000897 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000898 return j+1;
899 }else if( c=='"' ){
900 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000901 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000902 j = i+1;
903 for(;;){
drh9fa866a2017-04-08 18:18:22 +0000904 c = z[j];
drh86715382017-04-13 00:12:32 +0000905 if( (c & ~0x1f)==0 ){
906 /* Control characters are not allowed in strings */
907 return -1;
908 }
drhe9c37f32015-08-15 21:25:36 +0000909 if( c=='\\' ){
drh9fa866a2017-04-08 18:18:22 +0000910 c = z[++j];
drhad875e72016-11-07 13:37:28 +0000911 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
912 || c=='n' || c=='r' || c=='t'
drh9fa866a2017-04-08 18:18:22 +0000913 || (c=='u' && jsonIs4Hex(z+j+1)) ){
drhad875e72016-11-07 13:37:28 +0000914 jnFlags = JNODE_ESCAPE;
915 }else{
916 return -1;
917 }
drhe9c37f32015-08-15 21:25:36 +0000918 }else if( c=='"' ){
919 break;
920 }
921 j++;
922 }
drh9fa866a2017-04-08 18:18:22 +0000923 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
drhbe9474e2015-08-22 03:05:54 +0000924 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000925 return j+1;
926 }else if( c=='n'
drh9fa866a2017-04-08 18:18:22 +0000927 && strncmp(z+i,"null",4)==0
928 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000929 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
930 return i+4;
931 }else if( c=='t'
drh9fa866a2017-04-08 18:18:22 +0000932 && strncmp(z+i,"true",4)==0
933 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000934 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
935 return i+4;
936 }else if( c=='f'
drh9fa866a2017-04-08 18:18:22 +0000937 && strncmp(z+i,"false",5)==0
938 && !safe_isalnum(z[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000939 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
940 return i+5;
941 }else if( c=='-' || (c>='0' && c<='9') ){
942 /* Parse number */
943 u8 seenDP = 0;
944 u8 seenE = 0;
drh9fa866a2017-04-08 18:18:22 +0000945 assert( '-' < '0' );
946 if( c<='0' ){
947 j = c=='-' ? i+1 : i;
948 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
949 }
drhe9c37f32015-08-15 21:25:36 +0000950 j = i+1;
951 for(;; j++){
drh9fa866a2017-04-08 18:18:22 +0000952 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000953 if( c>='0' && c<='9' ) continue;
954 if( c=='.' ){
drh9fa866a2017-04-08 18:18:22 +0000955 if( z[j-1]=='-' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000956 if( seenDP ) return -1;
957 seenDP = 1;
958 continue;
959 }
960 if( c=='e' || c=='E' ){
drh9fa866a2017-04-08 18:18:22 +0000961 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000962 if( seenE ) return -1;
963 seenDP = seenE = 1;
drh9fa866a2017-04-08 18:18:22 +0000964 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000965 if( c=='+' || c=='-' ){
966 j++;
drh9fa866a2017-04-08 18:18:22 +0000967 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000968 }
drhd1f00682015-08-29 16:02:37 +0000969 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000970 continue;
971 }
972 break;
973 }
drh9fa866a2017-04-08 18:18:22 +0000974 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000975 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
drh9fa866a2017-04-08 18:18:22 +0000976 j - i, &z[i]);
drhe9c37f32015-08-15 21:25:36 +0000977 return j;
978 }else if( c=='}' ){
979 return -2; /* End of {...} */
980 }else if( c==']' ){
981 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000982 }else if( c==0 ){
983 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000984 }else{
985 return -1; /* Syntax error */
986 }
987}
988
989/*
990** Parse a complete JSON string. Return 0 on success or non-zero if there
991** are any errors. If an error occurs, free all memory associated with
992** pParse.
993**
994** pParse is uninitialized when this routine is called.
995*/
drhbc8f0922015-08-22 19:39:04 +0000996static int jsonParse(
997 JsonParse *pParse, /* Initialize and fill this JsonParse object */
998 sqlite3_context *pCtx, /* Report errors here */
999 const char *zJson /* Input JSON text to be parsed */
1000){
drhe9c37f32015-08-15 21:25:36 +00001001 int i;
drhe9c37f32015-08-15 21:25:36 +00001002 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +00001003 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +00001004 pParse->zJson = zJson;
1005 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +00001006 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +00001007 if( i>0 ){
drhff6d50e2017-04-11 18:55:05 +00001008 assert( pParse->iDepth==0 );
dan2e8f5512015-09-17 17:21:09 +00001009 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +00001010 if( zJson[i] ) i = -1;
1011 }
drhd1f00682015-08-29 16:02:37 +00001012 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +00001013 if( pCtx!=0 ){
1014 if( pParse->oom ){
1015 sqlite3_result_error_nomem(pCtx);
1016 }else{
1017 sqlite3_result_error(pCtx, "malformed JSON", -1);
1018 }
1019 }
drh505ad2c2015-08-21 17:33:11 +00001020 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +00001021 return 1;
1022 }
1023 return 0;
1024}
drh301eecc2015-08-17 20:14:19 +00001025
drh505ad2c2015-08-21 17:33:11 +00001026/* Mark node i of pParse as being a child of iParent. Call recursively
1027** to fill in all the descendants of node i.
1028*/
1029static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
1030 JsonNode *pNode = &pParse->aNode[i];
1031 u32 j;
1032 pParse->aUp[i] = iParent;
1033 switch( pNode->eType ){
1034 case JSON_ARRAY: {
1035 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
1036 jsonParseFillInParentage(pParse, i+j, i);
1037 }
1038 break;
1039 }
1040 case JSON_OBJECT: {
1041 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
1042 pParse->aUp[i+j] = i;
1043 jsonParseFillInParentage(pParse, i+j+1, i);
1044 }
1045 break;
1046 }
1047 default: {
1048 break;
1049 }
1050 }
1051}
1052
1053/*
1054** Compute the parentage of all nodes in a completed parse.
1055*/
1056static int jsonParseFindParents(JsonParse *pParse){
1057 u32 *aUp;
1058 assert( pParse->aUp==0 );
drh2d77d802019-01-08 20:02:48 +00001059 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +00001060 if( aUp==0 ){
1061 pParse->oom = 1;
1062 return SQLITE_NOMEM;
1063 }
drh505ad2c2015-08-21 17:33:11 +00001064 jsonParseFillInParentage(pParse, 0, 0);
1065 return SQLITE_OK;
1066}
1067
drh8cb0c832015-09-22 00:21:03 +00001068/*
drh3fb153c2017-05-11 16:49:59 +00001069** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1070*/
drhe35fc302018-08-30 01:52:10 +00001071#define JSON_CACHE_ID (-429938) /* First cache entry */
1072#define JSON_CACHE_SZ 4 /* Max number of cache entries */
drh3fb153c2017-05-11 16:49:59 +00001073
1074/*
1075** Obtain a complete parse of the JSON found in the first argument
1076** of the argv array. Use the sqlite3_get_auxdata() cache for this
1077** parse if it is available. If the cache is not available or if it
1078** is no longer valid, parse the JSON again and return the new parse,
1079** and also register the new parse so that it will be available for
1080** future sqlite3_get_auxdata() calls.
1081*/
1082static JsonParse *jsonParseCached(
1083 sqlite3_context *pCtx,
drhe35fc302018-08-30 01:52:10 +00001084 sqlite3_value **argv,
1085 sqlite3_context *pErrCtx
drh3fb153c2017-05-11 16:49:59 +00001086){
1087 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
1088 int nJson = sqlite3_value_bytes(argv[0]);
1089 JsonParse *p;
drhe35fc302018-08-30 01:52:10 +00001090 JsonParse *pMatch = 0;
1091 int iKey;
1092 int iMinKey = 0;
1093 u32 iMinHold = 0xffffffff;
1094 u32 iMaxHold = 0;
drh3fb153c2017-05-11 16:49:59 +00001095 if( zJson==0 ) return 0;
drhe35fc302018-08-30 01:52:10 +00001096 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
1097 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
1098 if( p==0 ){
1099 iMinKey = iKey;
1100 break;
1101 }
1102 if( pMatch==0
1103 && p->nJson==nJson
1104 && memcmp(p->zJson,zJson,nJson)==0
1105 ){
1106 p->nErr = 0;
1107 pMatch = p;
1108 }else if( p->iHold<iMinHold ){
1109 iMinHold = p->iHold;
1110 iMinKey = iKey;
1111 }
1112 if( p->iHold>iMaxHold ){
1113 iMaxHold = p->iHold;
1114 }
1115 }
1116 if( pMatch ){
1117 pMatch->nErr = 0;
1118 pMatch->iHold = iMaxHold+1;
1119 return pMatch;
drh3fb153c2017-05-11 16:49:59 +00001120 }
drh2d77d802019-01-08 20:02:48 +00001121 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
drh3fb153c2017-05-11 16:49:59 +00001122 if( p==0 ){
1123 sqlite3_result_error_nomem(pCtx);
1124 return 0;
1125 }
1126 memset(p, 0, sizeof(*p));
1127 p->zJson = (char*)&p[1];
1128 memcpy((char*)p->zJson, zJson, nJson+1);
drhe35fc302018-08-30 01:52:10 +00001129 if( jsonParse(p, pErrCtx, p->zJson) ){
drh3fb153c2017-05-11 16:49:59 +00001130 sqlite3_free(p);
1131 return 0;
1132 }
1133 p->nJson = nJson;
drhe35fc302018-08-30 01:52:10 +00001134 p->iHold = iMaxHold+1;
1135 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
1136 (void(*)(void*))jsonParseFree);
1137 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
drh3fb153c2017-05-11 16:49:59 +00001138}
1139
1140/*
drh8cb0c832015-09-22 00:21:03 +00001141** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1142** a match.
1143*/
mistachkinf2c26ed2015-10-12 22:20:29 +00001144static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh285f2ef2021-10-15 16:15:04 +00001145 assert( pNode->eU==1 );
drh8cb0c832015-09-22 00:21:03 +00001146 if( pNode->jnFlags & JNODE_RAW ){
1147 if( pNode->n!=nKey ) return 0;
1148 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
1149 }else{
1150 if( pNode->n!=nKey+2 ) return 0;
1151 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
1152 }
1153}
1154
drh52216ad2015-08-18 02:28:03 +00001155/* forward declaration */
drha7714022015-08-29 00:54:49 +00001156static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +00001157
drh987eb1f2015-08-17 15:17:37 +00001158/*
1159** Search along zPath to find the node specified. Return a pointer
1160** to that node, or NULL if zPath is malformed or if there is no such
1161** node.
drh52216ad2015-08-18 02:28:03 +00001162**
1163** If pApnd!=0, then try to append new nodes to complete zPath if it is
1164** possible to do so and if no existing node corresponds to zPath. If
1165** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +00001166*/
drha7714022015-08-29 00:54:49 +00001167static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +00001168 JsonParse *pParse, /* The JSON to search */
1169 u32 iRoot, /* Begin the search at this node */
1170 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +00001171 int *pApnd, /* Append nodes to complete path if not NULL */
1172 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +00001173){
drhbc8f0922015-08-22 19:39:04 +00001174 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +00001175 const char *zKey;
drh52216ad2015-08-18 02:28:03 +00001176 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +00001177 if( zPath[0]==0 ) return pRoot;
drh7e35e812019-07-31 12:13:58 +00001178 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
drh987eb1f2015-08-17 15:17:37 +00001179 if( zPath[0]=='.' ){
1180 if( pRoot->eType!=JSON_OBJECT ) return 0;
1181 zPath++;
drh6b43cc82015-08-19 23:02:49 +00001182 if( zPath[0]=='"' ){
1183 zKey = zPath + 1;
1184 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
1185 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +00001186 if( zPath[i] ){
1187 i++;
1188 }else{
1189 *pzErr = zPath;
1190 return 0;
1191 }
drh6b43cc82015-08-19 23:02:49 +00001192 }else{
1193 zKey = zPath;
1194 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
1195 nKey = i;
1196 }
drha7714022015-08-29 00:54:49 +00001197 if( nKey==0 ){
1198 *pzErr = zPath;
1199 return 0;
1200 }
drh987eb1f2015-08-17 15:17:37 +00001201 j = 1;
drh52216ad2015-08-18 02:28:03 +00001202 for(;;){
1203 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +00001204 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +00001205 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001206 }
1207 j++;
drh505ad2c2015-08-21 17:33:11 +00001208 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +00001209 }
drh52216ad2015-08-18 02:28:03 +00001210 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001211 assert( pRoot->eU==2 );
drh52216ad2015-08-18 02:28:03 +00001212 iRoot += pRoot->u.iAppend;
1213 pRoot = &pParse->aNode[iRoot];
1214 j = 1;
1215 }
1216 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001217 u32 iStart, iLabel;
1218 JsonNode *pNode;
1219 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
danfe9a8322019-06-17 14:50:33 +00001220 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
drh52216ad2015-08-18 02:28:03 +00001221 zPath += i;
drha7714022015-08-29 00:54:49 +00001222 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001223 if( pParse->oom ) return 0;
1224 if( pNode ){
1225 pRoot = &pParse->aNode[iRoot];
drh285f2ef2021-10-15 16:15:04 +00001226 assert( pRoot->eU==0 );
drhbc8f0922015-08-22 19:39:04 +00001227 pRoot->u.iAppend = iStart - iRoot;
1228 pRoot->jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001229 VVA( pRoot->eU = 2 );
drhbc8f0922015-08-22 19:39:04 +00001230 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1231 }
1232 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001233 }
drh52818642019-11-22 17:37:56 +00001234 }else if( zPath[0]=='[' ){
drh987eb1f2015-08-17 15:17:37 +00001235 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001236 j = 1;
1237 while( safe_isdigit(zPath[j]) ){
1238 i = i*10 + zPath[j] - '0';
1239 j++;
drh987eb1f2015-08-17 15:17:37 +00001240 }
drh52818642019-11-22 17:37:56 +00001241 if( j<2 || zPath[j]!=']' ){
1242 if( zPath[1]=='#' ){
1243 JsonNode *pBase = pRoot;
1244 int iBase = iRoot;
1245 if( pRoot->eType!=JSON_ARRAY ) return 0;
1246 for(;;){
1247 while( j<=pBase->n ){
1248 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
1249 j += jsonNodeSize(&pBase[j]);
1250 }
1251 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001252 assert( pBase->eU==2 );
drh52818642019-11-22 17:37:56 +00001253 iBase += pBase->u.iAppend;
1254 pBase = &pParse->aNode[iBase];
1255 j = 1;
1256 }
1257 j = 2;
1258 if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
1259 unsigned int x = 0;
1260 j = 3;
1261 do{
1262 x = x*10 + zPath[j] - '0';
1263 j++;
1264 }while( safe_isdigit(zPath[j]) );
1265 if( x>i ) return 0;
1266 i -= x;
1267 }
1268 if( zPath[j]!=']' ){
1269 *pzErr = zPath;
1270 return 0;
1271 }
1272 }else{
1273 *pzErr = zPath;
1274 return 0;
1275 }
drha7714022015-08-29 00:54:49 +00001276 }
drh52818642019-11-22 17:37:56 +00001277 if( pRoot->eType!=JSON_ARRAY ) return 0;
drh3d1d2a92015-09-22 01:15:49 +00001278 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001279 j = 1;
drh52216ad2015-08-18 02:28:03 +00001280 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001281 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1282 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001283 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001284 }
1285 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001286 assert( pRoot->eU==2 );
drh52216ad2015-08-18 02:28:03 +00001287 iRoot += pRoot->u.iAppend;
1288 pRoot = &pParse->aNode[iRoot];
1289 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001290 }
1291 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001292 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001293 }
1294 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001295 u32 iStart;
1296 JsonNode *pNode;
1297 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001298 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001299 if( pParse->oom ) return 0;
1300 if( pNode ){
1301 pRoot = &pParse->aNode[iRoot];
drh285f2ef2021-10-15 16:15:04 +00001302 assert( pRoot->eU==0 );
drhbc8f0922015-08-22 19:39:04 +00001303 pRoot->u.iAppend = iStart - iRoot;
1304 pRoot->jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001305 VVA( pRoot->eU = 2 );
drhbc8f0922015-08-22 19:39:04 +00001306 }
1307 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001308 }
drh3d1d2a92015-09-22 01:15:49 +00001309 }else{
drha7714022015-08-29 00:54:49 +00001310 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001311 }
1312 return 0;
1313}
1314
drh52216ad2015-08-18 02:28:03 +00001315/*
drhbc8f0922015-08-22 19:39:04 +00001316** Append content to pParse that will complete zPath. Return a pointer
1317** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001318*/
1319static JsonNode *jsonLookupAppend(
1320 JsonParse *pParse, /* Append content to the JSON parse */
1321 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001322 int *pApnd, /* Set this flag to 1 */
1323 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001324){
1325 *pApnd = 1;
1326 if( zPath[0]==0 ){
1327 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1328 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1329 }
1330 if( zPath[0]=='.' ){
1331 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1332 }else if( strncmp(zPath,"[0]",3)==0 ){
1333 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1334 }else{
1335 return 0;
1336 }
1337 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001338 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001339}
1340
drhbc8f0922015-08-22 19:39:04 +00001341/*
drha7714022015-08-29 00:54:49 +00001342** Return the text of a syntax error message on a JSON path. Space is
1343** obtained from sqlite3_malloc().
1344*/
1345static char *jsonPathSyntaxError(const char *zErr){
1346 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1347}
1348
1349/*
1350** Do a node lookup using zPath. Return a pointer to the node on success.
1351** Return NULL if not found or if there is an error.
1352**
1353** On an error, write an error message into pCtx and increment the
1354** pParse->nErr counter.
1355**
1356** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1357** nodes are appended.
drha7714022015-08-29 00:54:49 +00001358*/
1359static JsonNode *jsonLookup(
1360 JsonParse *pParse, /* The JSON to search */
1361 const char *zPath, /* The path to search */
1362 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001363 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001364){
1365 const char *zErr = 0;
1366 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001367 char *zMsg;
drha7714022015-08-29 00:54:49 +00001368
1369 if( zPath==0 ) return 0;
1370 if( zPath[0]!='$' ){
1371 zErr = zPath;
1372 goto lookup_err;
1373 }
1374 zPath++;
drha7714022015-08-29 00:54:49 +00001375 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001376 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001377
1378lookup_err:
1379 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001380 assert( zErr!=0 && pCtx!=0 );
1381 zMsg = jsonPathSyntaxError(zErr);
1382 if( zMsg ){
1383 sqlite3_result_error(pCtx, zMsg, -1);
1384 sqlite3_free(zMsg);
1385 }else{
1386 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001387 }
drha7714022015-08-29 00:54:49 +00001388 return 0;
1389}
1390
1391
1392/*
drhbc8f0922015-08-22 19:39:04 +00001393** Report the wrong number of arguments for json_insert(), json_replace()
1394** or json_set().
1395*/
1396static void jsonWrongNumArgs(
1397 sqlite3_context *pCtx,
1398 const char *zFuncName
1399){
1400 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1401 zFuncName);
1402 sqlite3_result_error(pCtx, zMsg, -1);
1403 sqlite3_free(zMsg);
1404}
drh52216ad2015-08-18 02:28:03 +00001405
drh29c99692017-03-24 12:35:17 +00001406/*
1407** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1408*/
1409static void jsonRemoveAllNulls(JsonNode *pNode){
1410 int i, n;
1411 assert( pNode->eType==JSON_OBJECT );
1412 n = pNode->n;
1413 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1414 switch( pNode[i].eType ){
1415 case JSON_NULL:
1416 pNode[i].jnFlags |= JNODE_REMOVE;
1417 break;
1418 case JSON_OBJECT:
1419 jsonRemoveAllNulls(&pNode[i]);
1420 break;
1421 }
1422 }
1423}
1424
drha7714022015-08-29 00:54:49 +00001425
drh987eb1f2015-08-17 15:17:37 +00001426/****************************************************************************
1427** SQL functions used for testing and debugging
1428****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001429
drh301eecc2015-08-17 20:14:19 +00001430#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001431/*
drh5634cc02015-08-17 11:28:03 +00001432** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001433** a parse of the JSON provided. Or it returns NULL if JSON is not
1434** well-formed.
1435*/
drh5634cc02015-08-17 11:28:03 +00001436static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001437 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001438 int argc,
1439 sqlite3_value **argv
1440){
drh505ad2c2015-08-21 17:33:11 +00001441 JsonString s; /* Output string - not real JSON */
1442 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001443 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001444
1445 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001446 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001447 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001448 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001449 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001450 const char *zType;
1451 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1452 assert( x.aNode[i].eType==JSON_STRING );
1453 zType = "label";
1454 }else{
1455 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001456 }
drh852944e2015-09-10 03:29:11 +00001457 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1458 i, zType, x.aNode[i].n, x.aUp[i]);
drh285f2ef2021-10-15 16:15:04 +00001459 assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
drh852944e2015-09-10 03:29:11 +00001460 if( x.aNode[i].u.zJContent!=0 ){
drh285f2ef2021-10-15 16:15:04 +00001461 assert( x.aNode[i].eU==1 );
drh852944e2015-09-10 03:29:11 +00001462 jsonAppendRaw(&s, " ", 1);
1463 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
drh285f2ef2021-10-15 16:15:04 +00001464 }else{
1465 assert( x.aNode[i].eU==0 );
drh852944e2015-09-10 03:29:11 +00001466 }
1467 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001468 }
drh505ad2c2015-08-21 17:33:11 +00001469 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001470 jsonResult(&s);
1471}
1472
drh5634cc02015-08-17 11:28:03 +00001473/*
drhf5ddb9c2015-09-11 00:06:41 +00001474** The json_test1(JSON) function return true (1) if the input is JSON
1475** text generated by another json function. It returns (0) if the input
1476** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001477*/
1478static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001479 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001480 int argc,
1481 sqlite3_value **argv
1482){
mistachkin16a93122015-09-11 18:05:01 +00001483 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001484 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001485}
drh301eecc2015-08-17 20:14:19 +00001486#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001487
drh987eb1f2015-08-17 15:17:37 +00001488/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001489** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001490****************************************************************************/
1491
1492/*
drh2ad96f52016-06-17 13:01:51 +00001493** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1494** corresponding to the SQL value input. Mostly this means putting
1495** double-quotes around strings and returning the unquoted string "null"
1496** when given a NULL input.
1497*/
1498static void jsonQuoteFunc(
1499 sqlite3_context *ctx,
1500 int argc,
1501 sqlite3_value **argv
1502){
1503 JsonString jx;
drhb0df5402016-08-01 17:06:44 +00001504 UNUSED_PARAM(argc);
drh2ad96f52016-06-17 13:01:51 +00001505
1506 jsonInit(&jx, ctx);
1507 jsonAppendValue(&jx, argv[0]);
1508 jsonResult(&jx);
1509 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1510}
1511
1512/*
drh987eb1f2015-08-17 15:17:37 +00001513** Implementation of the json_array(VALUE,...) function. Return a JSON
1514** array that contains all values given in arguments. Or if any argument
1515** is a BLOB, throw an error.
1516*/
1517static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001518 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001519 int argc,
1520 sqlite3_value **argv
1521){
1522 int i;
drh505ad2c2015-08-21 17:33:11 +00001523 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001524
drhbc8f0922015-08-22 19:39:04 +00001525 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001526 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001527 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001528 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001529 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001530 }
drhd0960592015-08-17 21:22:32 +00001531 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001532 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001533 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001534}
1535
1536
1537/*
1538** json_array_length(JSON)
1539** json_array_length(JSON, PATH)
1540**
1541** Return the number of elements in the top-level JSON array.
1542** Return 0 if the input is not a well-formed JSON array.
1543*/
1544static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001545 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001546 int argc,
1547 sqlite3_value **argv
1548){
drh3fb153c2017-05-11 16:49:59 +00001549 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001550 sqlite3_int64 n = 0;
1551 u32 i;
drha8f39a92015-09-21 22:53:16 +00001552 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001553
drhe35fc302018-08-30 01:52:10 +00001554 p = jsonParseCached(ctx, argv, ctx);
drh3fb153c2017-05-11 16:49:59 +00001555 if( p==0 ) return;
1556 assert( p->nNode );
drha8f39a92015-09-21 22:53:16 +00001557 if( argc==2 ){
1558 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
drh3fb153c2017-05-11 16:49:59 +00001559 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00001560 }else{
drh3fb153c2017-05-11 16:49:59 +00001561 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00001562 }
1563 if( pNode==0 ){
drh3fb153c2017-05-11 16:49:59 +00001564 return;
1565 }
1566 if( pNode->eType==JSON_ARRAY ){
drha8f39a92015-09-21 22:53:16 +00001567 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1568 for(i=1; i<=pNode->n; n++){
1569 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001570 }
drh987eb1f2015-08-17 15:17:37 +00001571 }
drh3fb153c2017-05-11 16:49:59 +00001572 sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001573}
1574
1575/*
drh3ad93bb2015-08-29 19:41:45 +00001576** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001577**
drh3ad93bb2015-08-29 19:41:45 +00001578** Return the element described by PATH. Return NULL if there is no
1579** PATH element. If there are multiple PATHs, then return a JSON array
1580** with the result from each path. Throw an error if the JSON or any PATH
1581** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001582*/
1583static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001584 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001585 int argc,
1586 sqlite3_value **argv
1587){
drh3fb153c2017-05-11 16:49:59 +00001588 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001589 JsonNode *pNode;
1590 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001591 JsonString jx;
1592 int i;
1593
1594 if( argc<2 ) return;
drhe35fc302018-08-30 01:52:10 +00001595 p = jsonParseCached(ctx, argv, ctx);
drh3fb153c2017-05-11 16:49:59 +00001596 if( p==0 ) return;
drh3ad93bb2015-08-29 19:41:45 +00001597 jsonInit(&jx, ctx);
1598 jsonAppendChar(&jx, '[');
1599 for(i=1; i<argc; i++){
1600 zPath = (const char*)sqlite3_value_text(argv[i]);
drh3fb153c2017-05-11 16:49:59 +00001601 pNode = jsonLookup(p, zPath, 0, ctx);
1602 if( p->nErr ) break;
drh3ad93bb2015-08-29 19:41:45 +00001603 if( argc>2 ){
1604 jsonAppendSeparator(&jx);
1605 if( pNode ){
1606 jsonRenderNode(pNode, &jx, 0);
1607 }else{
1608 jsonAppendRaw(&jx, "null", 4);
1609 }
1610 }else if( pNode ){
1611 jsonReturn(pNode, ctx, 0);
1612 }
drh987eb1f2015-08-17 15:17:37 +00001613 }
drh3ad93bb2015-08-29 19:41:45 +00001614 if( argc>2 && i==argc ){
1615 jsonAppendChar(&jx, ']');
1616 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001617 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001618 }
1619 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001620}
1621
drh633647a2017-03-22 21:24:31 +00001622/* This is the RFC 7396 MergePatch algorithm.
1623*/
1624static JsonNode *jsonMergePatch(
1625 JsonParse *pParse, /* The JSON parser that contains the TARGET */
mistachkinb1ed7172017-04-14 14:50:34 +00001626 u32 iTarget, /* Node of the TARGET in pParse */
drh633647a2017-03-22 21:24:31 +00001627 JsonNode *pPatch /* The PATCH */
1628){
drh0002d242017-03-23 00:46:15 +00001629 u32 i, j;
1630 u32 iRoot;
drh633647a2017-03-22 21:24:31 +00001631 JsonNode *pTarget;
1632 if( pPatch->eType!=JSON_OBJECT ){
1633 return pPatch;
1634 }
1635 assert( iTarget>=0 && iTarget<pParse->nNode );
1636 pTarget = &pParse->aNode[iTarget];
1637 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1638 if( pTarget->eType!=JSON_OBJECT ){
drh29c99692017-03-24 12:35:17 +00001639 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001640 return pPatch;
1641 }
drhbb7aa2d2017-03-23 00:13:52 +00001642 iRoot = iTarget;
drh633647a2017-03-22 21:24:31 +00001643 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
drhba7cce32017-03-24 19:45:05 +00001644 u32 nKey;
drh633647a2017-03-22 21:24:31 +00001645 const char *zKey;
1646 assert( pPatch[i].eType==JSON_STRING );
1647 assert( pPatch[i].jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00001648 assert( pPatch[i].eU==1 );
drh633647a2017-03-22 21:24:31 +00001649 nKey = pPatch[i].n;
1650 zKey = pPatch[i].u.zJContent;
drhbb7aa2d2017-03-23 00:13:52 +00001651 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh633647a2017-03-22 21:24:31 +00001652 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1653 assert( pTarget[j].eType==JSON_STRING );
1654 assert( pTarget[j].jnFlags & JNODE_LABEL );
drhbb7aa2d2017-03-23 00:13:52 +00001655 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh1fe162f2017-03-23 12:56:44 +00001656 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1657 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
drh633647a2017-03-22 21:24:31 +00001658 if( pPatch[i+1].eType==JSON_NULL ){
1659 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1660 }else{
drhbb7aa2d2017-03-23 00:13:52 +00001661 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
drh633647a2017-03-22 21:24:31 +00001662 if( pNew==0 ) return 0;
1663 pTarget = &pParse->aNode[iTarget];
1664 if( pNew!=&pTarget[j+1] ){
drha2852ac2021-11-15 01:45:11 +00001665 assert( pTarget[j+1].eU==0
1666 || pTarget[j+1].eU==1
1667 || pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001668 testcase( pTarget[j+1].eU==1 );
drha2852ac2021-11-15 01:45:11 +00001669 testcase( pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001670 VVA( pTarget[j+1].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001671 pTarget[j+1].u.pPatch = pNew;
1672 pTarget[j+1].jnFlags |= JNODE_PATCH;
1673 }
1674 }
1675 break;
1676 }
1677 }
drhbb7aa2d2017-03-23 00:13:52 +00001678 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
drh633647a2017-03-22 21:24:31 +00001679 int iStart, iPatch;
1680 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1681 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1682 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1683 if( pParse->oom ) return 0;
drh29c99692017-03-24 12:35:17 +00001684 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001685 pTarget = &pParse->aNode[iTarget];
drh8b554e22021-10-20 20:22:37 +00001686 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
1687 testcase( pParse->aNode[iRoot].eU==2 );
drhbb7aa2d2017-03-23 00:13:52 +00001688 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001689 VVA( pParse->aNode[iRoot].eU = 2 );
drhbb7aa2d2017-03-23 00:13:52 +00001690 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
1691 iRoot = iStart;
drh285f2ef2021-10-15 16:15:04 +00001692 assert( pParse->aNode[iPatch].eU==0 );
1693 VVA( pParse->aNode[iPatch].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001694 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
1695 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
1696 }
1697 }
1698 return pTarget;
1699}
1700
1701/*
1702** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1703** object that is the result of running the RFC 7396 MergePatch() algorithm
1704** on the two arguments.
1705*/
drh37f03df2017-03-23 20:33:49 +00001706static void jsonPatchFunc(
drh633647a2017-03-22 21:24:31 +00001707 sqlite3_context *ctx,
1708 int argc,
1709 sqlite3_value **argv
1710){
1711 JsonParse x; /* The JSON that is being patched */
1712 JsonParse y; /* The patch */
drhbb7aa2d2017-03-23 00:13:52 +00001713 JsonNode *pResult; /* The result of the merge */
drh633647a2017-03-22 21:24:31 +00001714
drh2fb79e92017-03-25 12:08:11 +00001715 UNUSED_PARAM(argc);
drh633647a2017-03-22 21:24:31 +00001716 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1717 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
1718 jsonParseReset(&x);
1719 return;
1720 }
drhbb7aa2d2017-03-23 00:13:52 +00001721 pResult = jsonMergePatch(&x, 0, y.aNode);
1722 assert( pResult!=0 || x.oom );
1723 if( pResult ){
1724 jsonReturnJson(pResult, ctx, 0);
1725 }else{
1726 sqlite3_result_error_nomem(ctx);
1727 }
drh633647a2017-03-22 21:24:31 +00001728 jsonParseReset(&x);
1729 jsonParseReset(&y);
1730}
1731
1732
drh987eb1f2015-08-17 15:17:37 +00001733/*
1734** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1735** object that contains all name/value given in arguments. Or if any name
1736** is not a string or if any value is a BLOB, throw an error.
1737*/
1738static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001739 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001740 int argc,
1741 sqlite3_value **argv
1742){
1743 int i;
drh505ad2c2015-08-21 17:33:11 +00001744 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001745 const char *z;
1746 u32 n;
1747
1748 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001749 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001750 "of arguments", -1);
1751 return;
1752 }
drhbc8f0922015-08-22 19:39:04 +00001753 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001754 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001755 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001756 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001757 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001758 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001759 return;
1760 }
drhd0960592015-08-17 21:22:32 +00001761 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001762 z = (const char*)sqlite3_value_text(argv[i]);
1763 n = (u32)sqlite3_value_bytes(argv[i]);
1764 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001765 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001766 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001767 }
drhd0960592015-08-17 21:22:32 +00001768 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001769 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001770 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001771}
1772
1773
1774/*
drh301eecc2015-08-17 20:14:19 +00001775** json_remove(JSON, PATH, ...)
1776**
drh3ad93bb2015-08-29 19:41:45 +00001777** Remove the named elements from JSON and return the result. malformed
1778** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001779*/
1780static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001781 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001782 int argc,
1783 sqlite3_value **argv
1784){
1785 JsonParse x; /* The parse */
1786 JsonNode *pNode;
1787 const char *zPath;
1788 u32 i;
1789
1790 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001791 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001792 assert( x.nNode );
1793 for(i=1; i<(u32)argc; i++){
1794 zPath = (const char*)sqlite3_value_text(argv[i]);
1795 if( zPath==0 ) goto remove_done;
1796 pNode = jsonLookup(&x, zPath, 0, ctx);
1797 if( x.nErr ) goto remove_done;
1798 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1799 }
1800 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1801 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001802 }
drha7714022015-08-29 00:54:49 +00001803remove_done:
drh505ad2c2015-08-21 17:33:11 +00001804 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001805}
1806
1807/*
1808** json_replace(JSON, PATH, VALUE, ...)
1809**
1810** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001811** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001812*/
1813static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001814 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001815 int argc,
1816 sqlite3_value **argv
1817){
1818 JsonParse x; /* The parse */
1819 JsonNode *pNode;
1820 const char *zPath;
1821 u32 i;
1822
1823 if( argc<1 ) return;
1824 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001825 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001826 return;
1827 }
drhbc8f0922015-08-22 19:39:04 +00001828 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001829 assert( x.nNode );
1830 for(i=1; i<(u32)argc; i+=2){
1831 zPath = (const char*)sqlite3_value_text(argv[i]);
1832 pNode = jsonLookup(&x, zPath, 0, ctx);
1833 if( x.nErr ) goto replace_err;
1834 if( pNode ){
drh285f2ef2021-10-15 16:15:04 +00001835 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
1836 json_testcase( pNode->eU!=0 && pNode->eU!=1 );
drha8f39a92015-09-21 22:53:16 +00001837 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh285f2ef2021-10-15 16:15:04 +00001838 VVA( pNode->eU = 4 );
drh633647a2017-03-22 21:24:31 +00001839 pNode->u.iReplace = i + 1;
drhd0960592015-08-17 21:22:32 +00001840 }
drha8f39a92015-09-21 22:53:16 +00001841 }
1842 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001843 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001844 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001845 }else{
1846 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001847 }
drha7714022015-08-29 00:54:49 +00001848replace_err:
drh505ad2c2015-08-21 17:33:11 +00001849 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001850}
drh505ad2c2015-08-21 17:33:11 +00001851
drh52216ad2015-08-18 02:28:03 +00001852/*
1853** json_set(JSON, PATH, VALUE, ...)
1854**
1855** Set the value at PATH to VALUE. Create the PATH if it does not already
1856** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001857** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001858**
1859** json_insert(JSON, PATH, VALUE, ...)
1860**
1861** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001862** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001863*/
1864static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001865 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001866 int argc,
1867 sqlite3_value **argv
1868){
1869 JsonParse x; /* The parse */
1870 JsonNode *pNode;
1871 const char *zPath;
1872 u32 i;
1873 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001874 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001875
1876 if( argc<1 ) return;
1877 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001878 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001879 return;
1880 }
drhbc8f0922015-08-22 19:39:04 +00001881 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001882 assert( x.nNode );
1883 for(i=1; i<(u32)argc; i+=2){
1884 zPath = (const char*)sqlite3_value_text(argv[i]);
1885 bApnd = 0;
1886 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1887 if( x.oom ){
1888 sqlite3_result_error_nomem(ctx);
1889 goto jsonSetDone;
1890 }else if( x.nErr ){
1891 goto jsonSetDone;
1892 }else if( pNode && (bApnd || bIsSet) ){
drh285f2ef2021-10-15 16:15:04 +00001893 json_testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 );
1894 assert( pNode->eU!=3 || pNode->eU!=5 );
1895 VVA( pNode->eU = 4 );
drha8f39a92015-09-21 22:53:16 +00001896 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001897 pNode->u.iReplace = i + 1;
drh52216ad2015-08-18 02:28:03 +00001898 }
drha8f39a92015-09-21 22:53:16 +00001899 }
1900 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001901 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001902 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001903 }else{
1904 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001905 }
drhbc8f0922015-08-22 19:39:04 +00001906jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001907 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001908}
drh301eecc2015-08-17 20:14:19 +00001909
1910/*
drh987eb1f2015-08-17 15:17:37 +00001911** json_type(JSON)
1912** json_type(JSON, PATH)
1913**
drh3ad93bb2015-08-29 19:41:45 +00001914** Return the top-level "type" of a JSON string. Throw an error if
1915** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001916*/
1917static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001918 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001919 int argc,
1920 sqlite3_value **argv
1921){
drhe35fc302018-08-30 01:52:10 +00001922 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001923 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001924 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001925
drhe35fc302018-08-30 01:52:10 +00001926 p = jsonParseCached(ctx, argv, ctx);
1927 if( p==0 ) return;
drha8f39a92015-09-21 22:53:16 +00001928 if( argc==2 ){
1929 zPath = (const char*)sqlite3_value_text(argv[1]);
drhe35fc302018-08-30 01:52:10 +00001930 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00001931 }else{
drhe35fc302018-08-30 01:52:10 +00001932 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00001933 }
1934 if( pNode ){
1935 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001936 }
drh987eb1f2015-08-17 15:17:37 +00001937}
drh5634cc02015-08-17 11:28:03 +00001938
drhbc8f0922015-08-22 19:39:04 +00001939/*
1940** json_valid(JSON)
1941**
drh3ad93bb2015-08-29 19:41:45 +00001942** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1943** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001944*/
1945static void jsonValidFunc(
1946 sqlite3_context *ctx,
1947 int argc,
1948 sqlite3_value **argv
1949){
drhe35fc302018-08-30 01:52:10 +00001950 JsonParse *p; /* The parse */
mistachkin16a93122015-09-11 18:05:01 +00001951 UNUSED_PARAM(argc);
drhe35fc302018-08-30 01:52:10 +00001952 p = jsonParseCached(ctx, argv, 0);
1953 sqlite3_result_int(ctx, p!=0);
drhbc8f0922015-08-22 19:39:04 +00001954}
1955
drhff135ae2015-12-30 01:07:02 +00001956
1957/****************************************************************************
1958** Aggregate SQL function implementations
1959****************************************************************************/
1960/*
1961** json_group_array(VALUE)
1962**
1963** Return a JSON array composed of all values in the aggregate.
1964*/
1965static void jsonArrayStep(
1966 sqlite3_context *ctx,
1967 int argc,
1968 sqlite3_value **argv
1969){
1970 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00001971 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001972 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1973 if( pStr ){
1974 if( pStr->zBuf==0 ){
1975 jsonInit(pStr, ctx);
1976 jsonAppendChar(pStr, '[');
drhfab5b072019-09-14 00:21:34 +00001977 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00001978 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00001979 }
dan8505d732021-04-14 12:11:39 +00001980 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00001981 jsonAppendValue(pStr, argv[0]);
1982 }
1983}
drh8be47a72018-07-05 20:05:29 +00001984static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00001985 JsonString *pStr;
1986 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1987 if( pStr ){
1988 pStr->pCtx = ctx;
1989 jsonAppendChar(pStr, ']');
1990 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001991 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001992 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00001993 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00001994 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00001995 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1996 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00001997 }else{
mistachkined008ec2018-09-12 01:05:26 +00001998 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00001999 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002000 }
2001 }else{
2002 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
2003 }
2004 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2005}
drh8be47a72018-07-05 20:05:29 +00002006static void jsonArrayValue(sqlite3_context *ctx){
2007 jsonArrayCompute(ctx, 0);
2008}
2009static void jsonArrayFinal(sqlite3_context *ctx){
2010 jsonArrayCompute(ctx, 1);
2011}
2012
2013#ifndef SQLITE_OMIT_WINDOWFUNC
2014/*
2015** This method works for both json_group_array() and json_group_object().
2016** It works by removing the first element of the group by searching forward
2017** to the first comma (",") that is not within a string and deleting all
2018** text through that comma.
2019*/
2020static void jsonGroupInverse(
2021 sqlite3_context *ctx,
2022 int argc,
2023 sqlite3_value **argv
2024){
drhe39f3882019-09-21 17:31:03 +00002025 unsigned int i;
drh8be47a72018-07-05 20:05:29 +00002026 int inStr = 0;
drhfab5b072019-09-14 00:21:34 +00002027 int nNest = 0;
drh8be47a72018-07-05 20:05:29 +00002028 char *z;
drhfab5b072019-09-14 00:21:34 +00002029 char c;
drh8be47a72018-07-05 20:05:29 +00002030 JsonString *pStr;
drhc7bf5712018-07-09 22:49:01 +00002031 UNUSED_PARAM(argc);
2032 UNUSED_PARAM(argv);
drh8be47a72018-07-05 20:05:29 +00002033 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
drh491d4c82018-07-07 20:23:46 +00002034#ifdef NEVER
drhfd4b7282018-07-07 19:47:21 +00002035 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2036 ** always have been called to initalize it */
2037 if( NEVER(!pStr) ) return;
drh491d4c82018-07-07 20:23:46 +00002038#endif
drh8be47a72018-07-05 20:05:29 +00002039 z = pStr->zBuf;
drh0e5cd342021-04-13 01:12:32 +00002040 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
drhfab5b072019-09-14 00:21:34 +00002041 if( c=='"' ){
drh8be47a72018-07-05 20:05:29 +00002042 inStr = !inStr;
drhfab5b072019-09-14 00:21:34 +00002043 }else if( c=='\\' ){
drh8be47a72018-07-05 20:05:29 +00002044 i++;
drhfab5b072019-09-14 00:21:34 +00002045 }else if( !inStr ){
2046 if( c=='{' || c=='[' ) nNest++;
2047 if( c=='}' || c==']' ) nNest--;
drh8be47a72018-07-05 20:05:29 +00002048 }
2049 }
drh0e5cd342021-04-13 01:12:32 +00002050 if( i<pStr->nUsed ){
2051 pStr->nUsed -= i;
2052 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
2053 z[pStr->nUsed] = 0;
2054 }else{
2055 pStr->nUsed = 1;
2056 }
drh8be47a72018-07-05 20:05:29 +00002057}
2058#else
2059# define jsonGroupInverse 0
2060#endif
2061
drhff135ae2015-12-30 01:07:02 +00002062
2063/*
2064** json_group_obj(NAME,VALUE)
2065**
2066** Return a JSON object composed of all names and values in the aggregate.
2067*/
2068static void jsonObjectStep(
2069 sqlite3_context *ctx,
2070 int argc,
2071 sqlite3_value **argv
2072){
2073 JsonString *pStr;
2074 const char *z;
2075 u32 n;
drhdf3a9072016-02-11 15:37:18 +00002076 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002077 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2078 if( pStr ){
2079 if( pStr->zBuf==0 ){
2080 jsonInit(pStr, ctx);
2081 jsonAppendChar(pStr, '{');
drhfab5b072019-09-14 00:21:34 +00002082 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002083 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002084 }
drhd2f55772021-05-28 12:15:19 +00002085 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002086 z = (const char*)sqlite3_value_text(argv[0]);
2087 n = (u32)sqlite3_value_bytes(argv[0]);
2088 jsonAppendString(pStr, z, n);
2089 jsonAppendChar(pStr, ':');
2090 jsonAppendValue(pStr, argv[1]);
2091 }
2092}
drh8be47a72018-07-05 20:05:29 +00002093static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002094 JsonString *pStr;
2095 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2096 if( pStr ){
2097 jsonAppendChar(pStr, '}');
2098 if( pStr->bErr ){
drh6850a632016-11-07 18:18:08 +00002099 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002100 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002101 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002102 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002103 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2104 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002105 }else{
mistachkined008ec2018-09-12 01:05:26 +00002106 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002107 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002108 }
2109 }else{
2110 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
2111 }
2112 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2113}
drh8be47a72018-07-05 20:05:29 +00002114static void jsonObjectValue(sqlite3_context *ctx){
2115 jsonObjectCompute(ctx, 0);
2116}
2117static void jsonObjectFinal(sqlite3_context *ctx){
2118 jsonObjectCompute(ctx, 1);
2119}
2120
drhff135ae2015-12-30 01:07:02 +00002121
2122
drhd2975922015-08-29 17:22:33 +00002123#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00002124/****************************************************************************
2125** The json_each virtual table
2126****************************************************************************/
2127typedef struct JsonEachCursor JsonEachCursor;
2128struct JsonEachCursor {
2129 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00002130 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00002131 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00002132 u32 i; /* Index in sParse.aNode[] of current row */
2133 u32 iEnd; /* EOF when i equals or exceeds this value */
2134 u8 eType; /* Type of top-level element */
2135 u8 bRecursive; /* True for json_tree(). False for json_each() */
2136 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00002137 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00002138 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00002139};
2140
2141/* Constructor for the json_each virtual table */
2142static int jsonEachConnect(
2143 sqlite3 *db,
2144 void *pAux,
2145 int argc, const char *const*argv,
2146 sqlite3_vtab **ppVtab,
2147 char **pzErr
2148){
2149 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00002150 int rc;
drhcb6c6c62015-08-19 22:47:17 +00002151
2152/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00002153#define JEACH_KEY 0
2154#define JEACH_VALUE 1
2155#define JEACH_TYPE 2
2156#define JEACH_ATOM 3
2157#define JEACH_ID 4
2158#define JEACH_PARENT 5
2159#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00002160#define JEACH_PATH 7
drh43579192018-11-16 16:04:50 +00002161/* The xBestIndex method assumes that the JSON and ROOT columns are
2162** the last two columns in the table. Should this ever changes, be
2163** sure to update the xBestIndex method. */
drh383de692015-09-10 17:20:57 +00002164#define JEACH_JSON 8
2165#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00002166
drh6fd5c1e2015-08-21 20:37:12 +00002167 UNUSED_PARAM(pzErr);
2168 UNUSED_PARAM(argv);
2169 UNUSED_PARAM(argc);
2170 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00002171 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00002172 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2173 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00002174 if( rc==SQLITE_OK ){
2175 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
2176 if( pNew==0 ) return SQLITE_NOMEM;
2177 memset(pNew, 0, sizeof(*pNew));
drh2b1c2aa2020-01-07 19:45:40 +00002178 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
drh505ad2c2015-08-21 17:33:11 +00002179 }
2180 return rc;
drhcb6c6c62015-08-19 22:47:17 +00002181}
2182
2183/* destructor for json_each virtual table */
2184static int jsonEachDisconnect(sqlite3_vtab *pVtab){
2185 sqlite3_free(pVtab);
2186 return SQLITE_OK;
2187}
2188
drh505ad2c2015-08-21 17:33:11 +00002189/* constructor for a JsonEachCursor object for json_each(). */
2190static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00002191 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00002192
2193 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00002194 pCur = sqlite3_malloc( sizeof(*pCur) );
2195 if( pCur==0 ) return SQLITE_NOMEM;
2196 memset(pCur, 0, sizeof(*pCur));
2197 *ppCursor = &pCur->base;
2198 return SQLITE_OK;
2199}
2200
drh505ad2c2015-08-21 17:33:11 +00002201/* constructor for a JsonEachCursor object for json_tree(). */
2202static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2203 int rc = jsonEachOpenEach(p, ppCursor);
2204 if( rc==SQLITE_OK ){
2205 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
2206 pCur->bRecursive = 1;
2207 }
2208 return rc;
2209}
2210
drhcb6c6c62015-08-19 22:47:17 +00002211/* Reset a JsonEachCursor back to its original state. Free any memory
2212** held. */
2213static void jsonEachCursorReset(JsonEachCursor *p){
2214 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00002215 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00002216 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00002217 p->iRowid = 0;
2218 p->i = 0;
2219 p->iEnd = 0;
2220 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00002221 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00002222 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002223}
2224
2225/* Destructor for a jsonEachCursor object */
2226static int jsonEachClose(sqlite3_vtab_cursor *cur){
2227 JsonEachCursor *p = (JsonEachCursor*)cur;
2228 jsonEachCursorReset(p);
2229 sqlite3_free(cur);
2230 return SQLITE_OK;
2231}
2232
2233/* Return TRUE if the jsonEachCursor object has been advanced off the end
2234** of the JSON object */
2235static int jsonEachEof(sqlite3_vtab_cursor *cur){
2236 JsonEachCursor *p = (JsonEachCursor*)cur;
2237 return p->i >= p->iEnd;
2238}
2239
drh505ad2c2015-08-21 17:33:11 +00002240/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00002241static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00002242 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00002243 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00002244 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
2245 p->i++;
drh4af352d2015-08-21 20:02:48 +00002246 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00002247 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00002248 u32 iUp = p->sParse.aUp[p->i];
2249 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00002250 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00002251 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002252 assert( pUp->eU==0 || pUp->eU==3 );
2253 json_testcase( pUp->eU==3 );
2254 VVA( pUp->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002255 if( iUp==p->i-1 ){
2256 pUp->u.iKey = 0;
2257 }else{
2258 pUp->u.iKey++;
2259 }
drh4af352d2015-08-21 20:02:48 +00002260 }
2261 }
drh505ad2c2015-08-21 17:33:11 +00002262 }else{
drh4af352d2015-08-21 20:02:48 +00002263 switch( p->eType ){
2264 case JSON_ARRAY: {
2265 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
2266 p->iRowid++;
2267 break;
2268 }
2269 case JSON_OBJECT: {
2270 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
2271 p->iRowid++;
2272 break;
2273 }
2274 default: {
2275 p->i = p->iEnd;
2276 break;
2277 }
drh505ad2c2015-08-21 17:33:11 +00002278 }
2279 }
2280 return SQLITE_OK;
2281}
2282
drh4af352d2015-08-21 20:02:48 +00002283/* Append the name of the path for element i to pStr
2284*/
2285static void jsonEachComputePath(
2286 JsonEachCursor *p, /* The cursor */
2287 JsonString *pStr, /* Write the path here */
2288 u32 i /* Path to this element */
2289){
2290 JsonNode *pNode, *pUp;
2291 u32 iUp;
2292 if( i==0 ){
2293 jsonAppendChar(pStr, '$');
2294 return;
drhcb6c6c62015-08-19 22:47:17 +00002295 }
drh4af352d2015-08-21 20:02:48 +00002296 iUp = p->sParse.aUp[i];
2297 jsonEachComputePath(p, pStr, iUp);
2298 pNode = &p->sParse.aNode[i];
2299 pUp = &p->sParse.aNode[iUp];
2300 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002301 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
2302 testcase( pUp->eU==0 );
drh4af352d2015-08-21 20:02:48 +00002303 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
2304 }else{
2305 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00002306 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00002307 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00002308 assert( pNode->jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00002309 assert( pNode->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002310 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
2311 }
drhcb6c6c62015-08-19 22:47:17 +00002312}
2313
2314/* Return the value of a column */
2315static int jsonEachColumn(
2316 sqlite3_vtab_cursor *cur, /* The cursor */
2317 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2318 int i /* Which column to return */
2319){
2320 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00002321 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00002322 switch( i ){
2323 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00002324 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00002325 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00002326 jsonReturn(pThis, ctx, 0);
2327 }else if( p->eType==JSON_ARRAY ){
2328 u32 iKey;
2329 if( p->bRecursive ){
2330 if( p->iRowid==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00002331 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
drh8784eca2015-08-23 02:42:30 +00002332 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00002333 }else{
2334 iKey = p->iRowid;
2335 }
drh6fd5c1e2015-08-21 20:37:12 +00002336 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00002337 }
2338 break;
2339 }
2340 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00002341 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002342 jsonReturn(pThis, ctx, 0);
2343 break;
2344 }
2345 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00002346 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002347 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2348 break;
2349 }
2350 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00002351 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002352 if( pThis->eType>=JSON_ARRAY ) break;
2353 jsonReturn(pThis, ctx, 0);
2354 break;
2355 }
2356 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00002357 sqlite3_result_int64(ctx,
2358 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00002359 break;
2360 }
2361 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00002362 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00002363 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00002364 }
2365 break;
2366 }
drh4af352d2015-08-21 20:02:48 +00002367 case JEACH_FULLKEY: {
2368 JsonString x;
2369 jsonInit(&x, ctx);
2370 if( p->bRecursive ){
2371 jsonEachComputePath(p, &x, p->i);
2372 }else{
drh383de692015-09-10 17:20:57 +00002373 if( p->zRoot ){
2374 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00002375 }else{
2376 jsonAppendChar(&x, '$');
2377 }
2378 if( p->eType==JSON_ARRAY ){
2379 jsonPrintf(30, &x, "[%d]", p->iRowid);
drhdd7460f2018-05-16 12:19:11 +00002380 }else if( p->eType==JSON_OBJECT ){
drh285f2ef2021-10-15 16:15:04 +00002381 assert( pThis->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002382 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2383 }
2384 }
2385 jsonResult(&x);
2386 break;
2387 }
drhcb6c6c62015-08-19 22:47:17 +00002388 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00002389 if( p->bRecursive ){
2390 JsonString x;
2391 jsonInit(&x, ctx);
2392 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2393 jsonResult(&x);
2394 break;
drh4af352d2015-08-21 20:02:48 +00002395 }
drh383de692015-09-10 17:20:57 +00002396 /* For json_each() path and root are the same so fall through
2397 ** into the root case */
drh08b92082020-08-10 14:18:00 +00002398 /* no break */ deliberate_fall_through
drh383de692015-09-10 17:20:57 +00002399 }
drh6850a632016-11-07 18:18:08 +00002400 default: {
drh383de692015-09-10 17:20:57 +00002401 const char *zRoot = p->zRoot;
drh6850a632016-11-07 18:18:08 +00002402 if( zRoot==0 ) zRoot = "$";
drh383de692015-09-10 17:20:57 +00002403 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00002404 break;
2405 }
drh3d1d2a92015-09-22 01:15:49 +00002406 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00002407 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00002408 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
2409 break;
2410 }
2411 }
2412 return SQLITE_OK;
2413}
2414
2415/* Return the current rowid value */
2416static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2417 JsonEachCursor *p = (JsonEachCursor*)cur;
2418 *pRowid = p->iRowid;
2419 return SQLITE_OK;
2420}
2421
2422/* The query strategy is to look for an equality constraint on the json
2423** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00002424** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00002425** and 0 otherwise.
2426*/
2427static int jsonEachBestIndex(
2428 sqlite3_vtab *tab,
2429 sqlite3_index_info *pIdxInfo
2430){
drh43579192018-11-16 16:04:50 +00002431 int i; /* Loop counter or computed array index */
2432 int aIdx[2]; /* Index of constraints for JSON and ROOT */
2433 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
2434 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
drhcb6c6c62015-08-19 22:47:17 +00002435 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00002436
drh43579192018-11-16 16:04:50 +00002437 /* This implementation assumes that JSON and ROOT are the last two
2438 ** columns in the table */
2439 assert( JEACH_ROOT == JEACH_JSON+1 );
drh6fd5c1e2015-08-21 20:37:12 +00002440 UNUSED_PARAM(tab);
drh43579192018-11-16 16:04:50 +00002441 aIdx[0] = aIdx[1] = -1;
drhcb6c6c62015-08-19 22:47:17 +00002442 pConstraint = pIdxInfo->aConstraint;
2443 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
drh43579192018-11-16 16:04:50 +00002444 int iCol;
2445 int iMask;
2446 if( pConstraint->iColumn < JEACH_JSON ) continue;
2447 iCol = pConstraint->iColumn - JEACH_JSON;
2448 assert( iCol==0 || iCol==1 );
drh285f2ef2021-10-15 16:15:04 +00002449 testcase( iCol==0 );
drh43579192018-11-16 16:04:50 +00002450 iMask = 1 << iCol;
2451 if( pConstraint->usable==0 ){
2452 unusableMask |= iMask;
2453 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
2454 aIdx[iCol] = i;
2455 idxMask |= iMask;
drhcb6c6c62015-08-19 22:47:17 +00002456 }
2457 }
drh43579192018-11-16 16:04:50 +00002458 if( (unusableMask & ~idxMask)!=0 ){
2459 /* If there are any unusable constraints on JSON or ROOT, then reject
2460 ** this entire plan */
2461 return SQLITE_CONSTRAINT;
2462 }
2463 if( aIdx[0]<0 ){
2464 /* No JSON input. Leave estimatedCost at the huge value that it was
2465 ** initialized to to discourage the query planner from selecting this
2466 ** plan. */
drhcb6c6c62015-08-19 22:47:17 +00002467 pIdxInfo->idxNum = 0;
drhcb6c6c62015-08-19 22:47:17 +00002468 }else{
drh505ad2c2015-08-21 17:33:11 +00002469 pIdxInfo->estimatedCost = 1.0;
drh43579192018-11-16 16:04:50 +00002470 i = aIdx[0];
2471 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
2472 pIdxInfo->aConstraintUsage[i].omit = 1;
2473 if( aIdx[1]<0 ){
2474 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
drhcb6c6c62015-08-19 22:47:17 +00002475 }else{
drh43579192018-11-16 16:04:50 +00002476 i = aIdx[1];
2477 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
2478 pIdxInfo->aConstraintUsage[i].omit = 1;
2479 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
drhcb6c6c62015-08-19 22:47:17 +00002480 }
2481 }
2482 return SQLITE_OK;
2483}
2484
2485/* Start a search on a new JSON string */
2486static int jsonEachFilter(
2487 sqlite3_vtab_cursor *cur,
2488 int idxNum, const char *idxStr,
2489 int argc, sqlite3_value **argv
2490){
2491 JsonEachCursor *p = (JsonEachCursor*)cur;
2492 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00002493 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002494 sqlite3_int64 n;
2495
drh6fd5c1e2015-08-21 20:37:12 +00002496 UNUSED_PARAM(idxStr);
2497 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002498 jsonEachCursorReset(p);
2499 if( idxNum==0 ) return SQLITE_OK;
2500 z = (const char*)sqlite3_value_text(argv[0]);
2501 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002502 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002503 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002504 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002505 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002506 if( jsonParse(&p->sParse, 0, p->zJson) ){
2507 int rc = SQLITE_NOMEM;
2508 if( p->sParse.oom==0 ){
2509 sqlite3_free(cur->pVtab->zErrMsg);
2510 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2511 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2512 }
drhcb6c6c62015-08-19 22:47:17 +00002513 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002514 return rc;
2515 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2516 jsonEachCursorReset(p);
2517 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002518 }else{
drh95677942015-09-24 01:06:37 +00002519 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002520 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002521 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002522 zRoot = (const char*)sqlite3_value_text(argv[1]);
2523 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002524 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002525 p->zRoot = sqlite3_malloc64( n+1 );
2526 if( p->zRoot==0 ) return SQLITE_NOMEM;
2527 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002528 if( zRoot[0]!='$' ){
2529 zErr = zRoot;
2530 }else{
2531 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2532 }
2533 if( zErr ){
drha7714022015-08-29 00:54:49 +00002534 sqlite3_free(cur->pVtab->zErrMsg);
2535 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002536 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002537 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2538 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002539 return SQLITE_OK;
2540 }
2541 }else{
2542 pNode = p->sParse.aNode;
2543 }
drh852944e2015-09-10 03:29:11 +00002544 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002545 p->eType = pNode->eType;
2546 if( p->eType>=JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002547 assert( pNode->eU==0 );
2548 VVA( pNode->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002549 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002550 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002551 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002552 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002553 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2554 p->i--;
2555 }
2556 }else{
2557 p->i++;
2558 }
drhcb6c6c62015-08-19 22:47:17 +00002559 }else{
2560 p->iEnd = p->i+1;
2561 }
2562 }
drha8f39a92015-09-21 22:53:16 +00002563 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002564}
2565
2566/* The methods of the json_each virtual table */
2567static sqlite3_module jsonEachModule = {
2568 0, /* iVersion */
2569 0, /* xCreate */
2570 jsonEachConnect, /* xConnect */
2571 jsonEachBestIndex, /* xBestIndex */
2572 jsonEachDisconnect, /* xDisconnect */
2573 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002574 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002575 jsonEachClose, /* xClose - close a cursor */
2576 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002577 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002578 jsonEachEof, /* xEof - check for end of scan */
2579 jsonEachColumn, /* xColumn - read data */
2580 jsonEachRowid, /* xRowid - read data */
2581 0, /* xUpdate */
2582 0, /* xBegin */
2583 0, /* xSync */
2584 0, /* xCommit */
2585 0, /* xRollback */
2586 0, /* xFindMethod */
2587 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002588 0, /* xSavepoint */
2589 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002590 0, /* xRollbackTo */
2591 0 /* xShadowName */
drhcb6c6c62015-08-19 22:47:17 +00002592};
2593
drh505ad2c2015-08-21 17:33:11 +00002594/* The methods of the json_tree virtual table. */
2595static sqlite3_module jsonTreeModule = {
2596 0, /* iVersion */
2597 0, /* xCreate */
2598 jsonEachConnect, /* xConnect */
2599 jsonEachBestIndex, /* xBestIndex */
2600 jsonEachDisconnect, /* xDisconnect */
2601 0, /* xDestroy */
2602 jsonEachOpenTree, /* xOpen - open a cursor */
2603 jsonEachClose, /* xClose - close a cursor */
2604 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002605 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002606 jsonEachEof, /* xEof - check for end of scan */
2607 jsonEachColumn, /* xColumn - read data */
2608 jsonEachRowid, /* xRowid - read data */
2609 0, /* xUpdate */
2610 0, /* xBegin */
2611 0, /* xSync */
2612 0, /* xCommit */
2613 0, /* xRollback */
2614 0, /* xFindMethod */
2615 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002616 0, /* xSavepoint */
2617 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002618 0, /* xRollbackTo */
2619 0 /* xShadowName */
drh505ad2c2015-08-21 17:33:11 +00002620};
drhd2975922015-08-29 17:22:33 +00002621#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002622
2623/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002624** The following routines are the only publically visible identifiers in this
2625** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002626** functions and the virtual table implemented by this file.
2627****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002628
drh2f20e132015-09-26 17:44:59 +00002629int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002630 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002631 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002632 static const struct {
2633 const char *zName;
2634 int nArg;
drh52216ad2015-08-18 02:28:03 +00002635 int flag;
drh5fa5c102015-08-12 16:49:40 +00002636 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2637 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002638 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002639 { "json_array", -1, 0, jsonArrayFunc },
2640 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2641 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002642 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002643 { "json_insert", -1, 0, jsonSetFunc },
2644 { "json_object", -1, 0, jsonObjectFunc },
drh37f03df2017-03-23 20:33:49 +00002645 { "json_patch", 2, 0, jsonPatchFunc },
drh2ad96f52016-06-17 13:01:51 +00002646 { "json_quote", 1, 0, jsonQuoteFunc },
drh52216ad2015-08-18 02:28:03 +00002647 { "json_remove", -1, 0, jsonRemoveFunc },
2648 { "json_replace", -1, 0, jsonReplaceFunc },
2649 { "json_set", -1, 1, jsonSetFunc },
2650 { "json_type", 1, 0, jsonTypeFunc },
2651 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002652 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002653
drh301eecc2015-08-17 20:14:19 +00002654#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002655 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002656 { "json_parse", 1, 0, jsonParseFunc },
2657 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002658#endif
drh5fa5c102015-08-12 16:49:40 +00002659 };
drhff135ae2015-12-30 01:07:02 +00002660 static const struct {
2661 const char *zName;
2662 int nArg;
2663 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2664 void (*xFinal)(sqlite3_context*);
drh8be47a72018-07-05 20:05:29 +00002665 void (*xValue)(sqlite3_context*);
drhff135ae2015-12-30 01:07:02 +00002666 } aAgg[] = {
drh8be47a72018-07-05 20:05:29 +00002667 { "json_group_array", 1,
2668 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
2669 { "json_group_object", 2,
2670 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
drhff135ae2015-12-30 01:07:02 +00002671 };
drhd2975922015-08-29 17:22:33 +00002672#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002673 static const struct {
2674 const char *zName;
2675 sqlite3_module *pModule;
2676 } aMod[] = {
2677 { "json_each", &jsonEachModule },
2678 { "json_tree", &jsonTreeModule },
2679 };
drhd2975922015-08-29 17:22:33 +00002680#endif
drh79d5bc82020-01-04 01:43:02 +00002681 static const int enc =
2682 SQLITE_UTF8 |
2683 SQLITE_DETERMINISTIC |
2684 SQLITE_INNOCUOUS;
drh5fa5c102015-08-12 16:49:40 +00002685 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
drh79d5bc82020-01-04 01:43:02 +00002686 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
dan01a3b6b2019-09-13 17:05:48 +00002687 (void*)&aFunc[i].flag,
2688 aFunc[i].xFunc, 0, 0);
drh5fa5c102015-08-12 16:49:40 +00002689 }
mistachkin5a193dd2018-07-24 13:57:44 +00002690#ifndef SQLITE_OMIT_WINDOWFUNC
drhff135ae2015-12-30 01:07:02 +00002691 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
drh8be47a72018-07-05 20:05:29 +00002692 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
drh79d5bc82020-01-04 01:43:02 +00002693 SQLITE_SUBTYPE | enc, 0,
drh8be47a72018-07-05 20:05:29 +00002694 aAgg[i].xStep, aAgg[i].xFinal,
2695 aAgg[i].xValue, jsonGroupInverse, 0);
drhff135ae2015-12-30 01:07:02 +00002696 }
mistachkin5a193dd2018-07-24 13:57:44 +00002697#endif
drhd2975922015-08-29 17:22:33 +00002698#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002699 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2700 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002701 }
drhd2975922015-08-29 17:22:33 +00002702#endif
drh5fa5c102015-08-12 16:49:40 +00002703 return rc;
2704}
drh2f20e132015-09-26 17:44:59 +00002705
2706
dan8d32e802015-10-14 18:45:42 +00002707#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002708#ifdef _WIN32
2709__declspec(dllexport)
2710#endif
2711int sqlite3_json_init(
2712 sqlite3 *db,
2713 char **pzErrMsg,
2714 const sqlite3_api_routines *pApi
2715){
2716 SQLITE_EXTENSION_INIT2(pApi);
2717 (void)pzErrMsg; /* Unused parameter */
2718 return sqlite3Json1Init(db);
2719}
dan8d32e802015-10-14 18:45:42 +00002720#endif
drh50065652015-10-08 19:29:18 +00002721#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */