blob: 926f0a8b6832b1bf28277bca4b579cc883c38ed8 [file] [log] [blame]
drh5fa5c102015-08-12 16:49:40 +00001/*
2** 2015-08-12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This SQLite extension implements JSON functions. The interface is
14** modeled after MySQL JSON functions:
15**
16** https://dev.mysql.com/doc/refman/5.7/en/json.html
17**
drh5634cc02015-08-17 11:28:03 +000018** For the time being, all JSON is stored as pure text. (We might add
19** a JSONB type in the future which stores a binary encoding of JSON in
drhcb6c6c62015-08-19 22:47:17 +000020** a BLOB, but there is no support for JSONB in the current implementation.
21** This implementation parses JSON text at 250 MB/s, so it is hard to see
22** how JSONB might improve on that.)
drh5fa5c102015-08-12 16:49:40 +000023*/
drh50065652015-10-08 19:29:18 +000024#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
drh666d34c2017-01-25 13:54:27 +000025#if !defined(SQLITEINT_H)
drh5fa5c102015-08-12 16:49:40 +000026#include "sqlite3ext.h"
drhf2df7e72015-08-28 20:07:40 +000027#endif
drh5fa5c102015-08-12 16:49:40 +000028SQLITE_EXTENSION_INIT1
drhf3c33c62021-12-20 23:46:44 +000029
30/* If compiling this extension separately (why would anybody do that when
31** it is built into the amalgamation?) we must set NDEBUG if SQLITE_DEBUG
32** is not defined *before* including <assert.h>, in order to disable asserts().
33*/
34#if !defined(SQLITE_AMALGAMATION) && !defined(SQLITE_DEBUG)
35# define NDEBUG 1
36#endif
37
drh5fa5c102015-08-12 16:49:40 +000038#include <assert.h>
39#include <string.h>
drh987eb1f2015-08-17 15:17:37 +000040#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000041#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000042
drhdf3a9072016-02-11 15:37:18 +000043/* Mark a function parameter as unused, to suppress nuisance compiler
44** warnings. */
45#ifndef UNUSED_PARAM
46# define UNUSED_PARAM(X) (void)(X)
47#endif
drh6fd5c1e2015-08-21 20:37:12 +000048
drh8deb4b82015-10-09 18:21:43 +000049#ifndef LARGEST_INT64
50# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
51# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
52#endif
53
drh08b92082020-08-10 14:18:00 +000054#ifndef deliberate_fall_through
55# define deliberate_fall_through
56#endif
57
dan2e8f5512015-09-17 17:21:09 +000058/*
59** Versions of isspace(), isalnum() and isdigit() to which it is safe
60** to pass signed char values.
61*/
drh49472652015-10-16 15:35:39 +000062#ifdef sqlite3Isdigit
63 /* Use the SQLite core versions if this routine is part of the
64 ** SQLite amalgamation */
drhad875e72016-11-07 13:37:28 +000065# define safe_isdigit(x) sqlite3Isdigit(x)
66# define safe_isalnum(x) sqlite3Isalnum(x)
67# define safe_isxdigit(x) sqlite3Isxdigit(x)
drh49472652015-10-16 15:35:39 +000068#else
69 /* Use the standard library for separate compilation */
70#include <ctype.h> /* amalgamator: keep */
drhad875e72016-11-07 13:37:28 +000071# define safe_isdigit(x) isdigit((unsigned char)(x))
72# define safe_isalnum(x) isalnum((unsigned char)(x))
73# define safe_isxdigit(x) isxdigit((unsigned char)(x))
drh49472652015-10-16 15:35:39 +000074#endif
dan2e8f5512015-09-17 17:21:09 +000075
drh95677942015-09-24 01:06:37 +000076/*
77** Growing our own isspace() routine this way is twice as fast as
78** the library isspace() function, resulting in a 7% overall performance
79** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
80*/
81static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000082 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98};
99#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
100
drh9a4718f2015-10-10 14:00:37 +0000101#ifndef SQLITE_AMALGAMATION
102 /* Unsigned integer types. These are already defined in the sqliteInt.h,
103 ** but the definitions need to be repeated for separate compilation. */
104 typedef sqlite3_uint64 u64;
105 typedef unsigned int u32;
drhff6d50e2017-04-11 18:55:05 +0000106 typedef unsigned short int u16;
drh9a4718f2015-10-10 14:00:37 +0000107 typedef unsigned char u8;
drh6a726fa2021-10-05 15:30:52 +0000108# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
109# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
110# endif
111# if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
112# define ALWAYS(X) (1)
113# define NEVER(X) (0)
114# elif !defined(NDEBUG)
115# define ALWAYS(X) ((X)?1:(assert(0),0))
116# define NEVER(X) ((X)?(assert(0),1):0)
117# else
118# define ALWAYS(X) (X)
119# define NEVER(X) (X)
120# endif
drh285f2ef2021-10-15 16:15:04 +0000121# define testcase(X)
drh9a4718f2015-10-10 14:00:37 +0000122#endif
drh16fc5e62021-11-01 12:53:01 +0000123#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
drh285f2ef2021-10-15 16:15:04 +0000124# define VVA(X)
125#else
126# define VVA(X) X
127#endif
128
129/*
130** Some of the testcase() macros in this file are problematic for gcov
131** in that they generate false-miss errors randomly. This is a gcov problem,
132** not a problem in this case. But to work around it, we disable the
133** problematic test cases for production builds.
134*/
135#define json_testcase(X)
drh5fa5c102015-08-12 16:49:40 +0000136
drh52216ad2015-08-18 02:28:03 +0000137/* Objects */
drh505ad2c2015-08-21 17:33:11 +0000138typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +0000139typedef struct JsonNode JsonNode;
140typedef struct JsonParse JsonParse;
141
drh5634cc02015-08-17 11:28:03 +0000142/* An instance of this object represents a JSON string
143** under construction. Really, this is a generic string accumulator
144** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000145*/
drh505ad2c2015-08-21 17:33:11 +0000146struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000147 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000148 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000149 u64 nAlloc; /* Bytes of storage available in zBuf[] */
150 u64 nUsed; /* Bytes of zBuf[] currently used */
151 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000152 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000153 char zSpace[100]; /* Initial static space */
154};
155
drhe9c37f32015-08-15 21:25:36 +0000156/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000157*/
drhe9c37f32015-08-15 21:25:36 +0000158#define JSON_NULL 0
159#define JSON_TRUE 1
160#define JSON_FALSE 2
161#define JSON_INT 3
162#define JSON_REAL 4
163#define JSON_STRING 5
164#define JSON_ARRAY 6
165#define JSON_OBJECT 7
166
drhf5ddb9c2015-09-11 00:06:41 +0000167/* The "subtype" set for JSON values */
168#define JSON_SUBTYPE 74 /* Ascii for "J" */
169
drh987eb1f2015-08-17 15:17:37 +0000170/*
171** Names of the various JSON types:
172*/
173static const char * const jsonType[] = {
174 "null", "true", "false", "integer", "real", "text", "array", "object"
175};
176
drh301eecc2015-08-17 20:14:19 +0000177/* Bit values for the JsonNode.jnFlag field
178*/
179#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
180#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
181#define JNODE_REMOVE 0x04 /* Do not output */
drh633647a2017-03-22 21:24:31 +0000182#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
183#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
184#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
185#define JNODE_LABEL 0x40 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000186
drh987eb1f2015-08-17 15:17:37 +0000187
drhe9c37f32015-08-15 21:25:36 +0000188/* A single node of parsed JSON
189*/
drhe9c37f32015-08-15 21:25:36 +0000190struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000191 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000192 u8 jnFlags; /* JNODE flags */
drh285f2ef2021-10-15 16:15:04 +0000193 u8 eU; /* Which union element to use */
drhe9c37f32015-08-15 21:25:36 +0000194 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000195 union {
drh285f2ef2021-10-15 16:15:04 +0000196 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
197 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
198 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
199 u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
200 JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
drh52216ad2015-08-18 02:28:03 +0000201 } u;
drhe9c37f32015-08-15 21:25:36 +0000202};
203
204/* A completely parsed JSON string
205*/
drhe9c37f32015-08-15 21:25:36 +0000206struct JsonParse {
207 u32 nNode; /* Number of slots of aNode[] used */
208 u32 nAlloc; /* Number of slots of aNode[] allocated */
209 JsonNode *aNode; /* Array of nodes containing the parse */
210 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000211 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000212 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000213 u8 nErr; /* Number of errors seen */
drhff6d50e2017-04-11 18:55:05 +0000214 u16 iDepth; /* Nesting depth */
drh3fb153c2017-05-11 16:49:59 +0000215 int nJson; /* Length of the zJson string in bytes */
drhe35fc302018-08-30 01:52:10 +0000216 u32 iHold; /* Replace cache line with the lowest iHold value */
drhe9c37f32015-08-15 21:25:36 +0000217};
218
drhff6d50e2017-04-11 18:55:05 +0000219/*
220** Maximum nesting depth of JSON for this implementation.
221**
222** This limit is needed to avoid a stack overflow in the recursive
223** descent parser. A depth of 2000 is far deeper than any sane JSON
224** should go.
225*/
226#define JSON_MAX_DEPTH 2000
227
drh505ad2c2015-08-21 17:33:11 +0000228/**************************************************************************
229** Utility routines for dealing with JsonString objects
230**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000231
drh505ad2c2015-08-21 17:33:11 +0000232/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000233*/
drh505ad2c2015-08-21 17:33:11 +0000234static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000235 p->zBuf = p->zSpace;
236 p->nAlloc = sizeof(p->zSpace);
237 p->nUsed = 0;
238 p->bStatic = 1;
239}
240
drh505ad2c2015-08-21 17:33:11 +0000241/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000242*/
drh505ad2c2015-08-21 17:33:11 +0000243static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000244 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000245 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000246 jsonZero(p);
247}
248
249
drh505ad2c2015-08-21 17:33:11 +0000250/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000251** initial state.
252*/
drh505ad2c2015-08-21 17:33:11 +0000253static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000254 if( !p->bStatic ) sqlite3_free(p->zBuf);
255 jsonZero(p);
256}
257
258
259/* Report an out-of-memory (OOM) condition
260*/
drh505ad2c2015-08-21 17:33:11 +0000261static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000262 p->bErr = 1;
263 sqlite3_result_error_nomem(p->pCtx);
264 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000265}
266
267/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
268** Return zero on success. Return non-zero on an OOM error
269*/
drh505ad2c2015-08-21 17:33:11 +0000270static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000271 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000272 char *zNew;
273 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000274 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000275 zNew = sqlite3_malloc64(nTotal);
276 if( zNew==0 ){
277 jsonOom(p);
278 return SQLITE_NOMEM;
279 }
drh6fd5c1e2015-08-21 20:37:12 +0000280 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000281 p->zBuf = zNew;
282 p->bStatic = 0;
283 }else{
284 zNew = sqlite3_realloc64(p->zBuf, nTotal);
285 if( zNew==0 ){
286 jsonOom(p);
287 return SQLITE_NOMEM;
288 }
289 p->zBuf = zNew;
290 }
291 p->nAlloc = nTotal;
292 return SQLITE_OK;
293}
294
drh505ad2c2015-08-21 17:33:11 +0000295/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000296*/
drh505ad2c2015-08-21 17:33:11 +0000297static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drhc795e3d2020-05-17 13:47:28 +0000298 if( N==0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000299 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
300 memcpy(p->zBuf+p->nUsed, zIn, N);
301 p->nUsed += N;
302}
303
drh4af352d2015-08-21 20:02:48 +0000304/* Append formatted text (not to exceed N bytes) to the JsonString.
305*/
306static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
307 va_list ap;
308 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
309 va_start(ap, zFormat);
310 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
311 va_end(ap);
312 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
313}
314
drh5634cc02015-08-17 11:28:03 +0000315/* Append a single character
316*/
drh505ad2c2015-08-21 17:33:11 +0000317static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000318 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
319 p->zBuf[p->nUsed++] = c;
320}
321
drh301eecc2015-08-17 20:14:19 +0000322/* Append a comma separator to the output buffer, if the previous
323** character is not '[' or '{'.
324*/
drh505ad2c2015-08-21 17:33:11 +0000325static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000326 char c;
327 if( p->nUsed==0 ) return;
328 c = p->zBuf[p->nUsed-1];
329 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
330}
331
drh505ad2c2015-08-21 17:33:11 +0000332/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000333** under construction. Enclose the string in "..." and escape
334** any double-quotes or backslash characters contained within the
335** string.
336*/
drh505ad2c2015-08-21 17:33:11 +0000337static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000338 u32 i;
drh76baad92021-04-30 16:12:40 +0000339 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
drh5fa5c102015-08-12 16:49:40 +0000340 p->zBuf[p->nUsed++] = '"';
341 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000342 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000343 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000344 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000345 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000346 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000347 }else if( c<=0x1f ){
348 static const char aSpecial[] = {
349 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
351 };
352 assert( sizeof(aSpecial)==32 );
353 assert( aSpecial['\b']=='b' );
354 assert( aSpecial['\f']=='f' );
355 assert( aSpecial['\n']=='n' );
356 assert( aSpecial['\r']=='r' );
357 assert( aSpecial['\t']=='t' );
358 if( aSpecial[c] ){
359 c = aSpecial[c];
360 goto json_simple_escape;
361 }
362 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
363 p->zBuf[p->nUsed++] = '\\';
364 p->zBuf[p->nUsed++] = 'u';
365 p->zBuf[p->nUsed++] = '0';
366 p->zBuf[p->nUsed++] = '0';
367 p->zBuf[p->nUsed++] = '0' + (c>>4);
368 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000369 }
370 p->zBuf[p->nUsed++] = c;
371 }
372 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000373 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000374}
375
drhd0960592015-08-17 21:22:32 +0000376/*
377** Append a function parameter value to the JSON string under
378** construction.
379*/
380static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000381 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000382 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000383){
384 switch( sqlite3_value_type(pValue) ){
385 case SQLITE_NULL: {
386 jsonAppendRaw(p, "null", 4);
387 break;
388 }
389 case SQLITE_INTEGER:
390 case SQLITE_FLOAT: {
391 const char *z = (const char*)sqlite3_value_text(pValue);
392 u32 n = (u32)sqlite3_value_bytes(pValue);
393 jsonAppendRaw(p, z, n);
394 break;
395 }
396 case SQLITE_TEXT: {
397 const char *z = (const char*)sqlite3_value_text(pValue);
398 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000399 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000400 jsonAppendRaw(p, z, n);
401 }else{
402 jsonAppendString(p, z, n);
403 }
drhd0960592015-08-17 21:22:32 +0000404 break;
405 }
406 default: {
407 if( p->bErr==0 ){
408 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000409 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000410 jsonReset(p);
411 }
412 break;
413 }
414 }
415}
416
417
drhbd0621b2015-08-13 13:54:59 +0000418/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000419*/
drh505ad2c2015-08-21 17:33:11 +0000420static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000421 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000422 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
423 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
424 SQLITE_UTF8);
425 jsonZero(p);
426 }
427 assert( p->bStatic );
428}
429
drh505ad2c2015-08-21 17:33:11 +0000430/**************************************************************************
431** Utility routines for dealing with JsonNode and JsonParse objects
432**************************************************************************/
433
434/*
435** Return the number of consecutive JsonNode slots need to represent
436** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
437** OBJECT types, the number might be larger.
438**
439** Appended elements are not counted. The value returned is the number
440** by which the JsonNode counter should increment in order to go to the
441** next peer value.
442*/
443static u32 jsonNodeSize(JsonNode *pNode){
444 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
445}
446
447/*
448** Reclaim all memory allocated by a JsonParse object. But do not
449** delete the JsonParse object itself.
450*/
451static void jsonParseReset(JsonParse *pParse){
452 sqlite3_free(pParse->aNode);
453 pParse->aNode = 0;
454 pParse->nNode = 0;
455 pParse->nAlloc = 0;
456 sqlite3_free(pParse->aUp);
457 pParse->aUp = 0;
458}
459
drh5634cc02015-08-17 11:28:03 +0000460/*
drh3fb153c2017-05-11 16:49:59 +0000461** Free a JsonParse object that was obtained from sqlite3_malloc().
462*/
463static void jsonParseFree(JsonParse *pParse){
464 jsonParseReset(pParse);
465 sqlite3_free(pParse);
466}
467
468/*
drh5634cc02015-08-17 11:28:03 +0000469** Convert the JsonNode pNode into a pure JSON string and
470** append to pOut. Subsubstructure is also included. Return
471** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000472*/
drh52216ad2015-08-18 02:28:03 +0000473static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000474 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000475 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000476 sqlite3_value **aReplace /* Replacement values */
477){
drh7d4c94b2021-10-04 22:34:38 +0000478 assert( pNode!=0 );
drh633647a2017-03-22 21:24:31 +0000479 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
drh7d4c94b2021-10-04 22:34:38 +0000480 if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){
drh285f2ef2021-10-15 16:15:04 +0000481 assert( pNode->eU==4 );
drh633647a2017-03-22 21:24:31 +0000482 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
483 return;
484 }
drh285f2ef2021-10-15 16:15:04 +0000485 assert( pNode->eU==5 );
drh633647a2017-03-22 21:24:31 +0000486 pNode = pNode->u.pPatch;
487 }
drh5634cc02015-08-17 11:28:03 +0000488 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000489 default: {
490 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000491 jsonAppendRaw(pOut, "null", 4);
492 break;
493 }
494 case JSON_TRUE: {
495 jsonAppendRaw(pOut, "true", 4);
496 break;
497 }
498 case JSON_FALSE: {
499 jsonAppendRaw(pOut, "false", 5);
500 break;
501 }
502 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000503 if( pNode->jnFlags & JNODE_RAW ){
drh285f2ef2021-10-15 16:15:04 +0000504 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000505 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000506 break;
507 }
drh08b92082020-08-10 14:18:00 +0000508 /* no break */ deliberate_fall_through
drh5634cc02015-08-17 11:28:03 +0000509 }
510 case JSON_REAL:
511 case JSON_INT: {
drh285f2ef2021-10-15 16:15:04 +0000512 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000513 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000514 break;
515 }
516 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000517 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000518 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000519 for(;;){
520 while( j<=pNode->n ){
drh633647a2017-03-22 21:24:31 +0000521 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +0000522 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000523 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000524 }
drh505ad2c2015-08-21 17:33:11 +0000525 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000526 }
drh52216ad2015-08-18 02:28:03 +0000527 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +0000528 assert( pNode->eU==2 );
drh52216ad2015-08-18 02:28:03 +0000529 pNode = &pNode[pNode->u.iAppend];
530 j = 1;
drh5634cc02015-08-17 11:28:03 +0000531 }
532 jsonAppendChar(pOut, ']');
533 break;
534 }
535 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000536 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000537 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000538 for(;;){
539 while( j<=pNode->n ){
540 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
541 jsonAppendSeparator(pOut);
542 jsonRenderNode(&pNode[j], pOut, aReplace);
543 jsonAppendChar(pOut, ':');
drh633647a2017-03-22 21:24:31 +0000544 jsonRenderNode(&pNode[j+1], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000545 }
drh505ad2c2015-08-21 17:33:11 +0000546 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000547 }
drh52216ad2015-08-18 02:28:03 +0000548 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +0000549 assert( pNode->eU==2 );
drh52216ad2015-08-18 02:28:03 +0000550 pNode = &pNode[pNode->u.iAppend];
551 j = 1;
drh5634cc02015-08-17 11:28:03 +0000552 }
553 jsonAppendChar(pOut, '}');
554 break;
555 }
drhbd0621b2015-08-13 13:54:59 +0000556 }
drh5634cc02015-08-17 11:28:03 +0000557}
558
559/*
drhf2df7e72015-08-28 20:07:40 +0000560** Return a JsonNode and all its descendents as a JSON string.
561*/
562static void jsonReturnJson(
563 JsonNode *pNode, /* Node to return */
564 sqlite3_context *pCtx, /* Return value for this function */
565 sqlite3_value **aReplace /* Array of replacement values */
566){
567 JsonString s;
568 jsonInit(&s, pCtx);
569 jsonRenderNode(pNode, &s, aReplace);
570 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000571 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000572}
573
574/*
drh48eb03b2019-11-10 11:09:06 +0000575** Translate a single byte of Hex into an integer.
576** This routine only works if h really is a valid hexadecimal
577** character: 0..9a..fA..F
578*/
579static u8 jsonHexToInt(int h){
580 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
581#ifdef SQLITE_EBCDIC
582 h += 9*(1&~(h>>4));
583#else
584 h += 9*(1&(h>>6));
585#endif
586 return (u8)(h & 0xf);
587}
588
589/*
590** Convert a 4-byte hex string into an integer
591*/
592static u32 jsonHexToInt4(const char *z){
593 u32 v;
594 assert( safe_isxdigit(z[0]) );
595 assert( safe_isxdigit(z[1]) );
596 assert( safe_isxdigit(z[2]) );
597 assert( safe_isxdigit(z[3]) );
598 v = (jsonHexToInt(z[0])<<12)
599 + (jsonHexToInt(z[1])<<8)
600 + (jsonHexToInt(z[2])<<4)
601 + jsonHexToInt(z[3]);
602 return v;
603}
604
605/*
drh5634cc02015-08-17 11:28:03 +0000606** Make the JsonNode the return value of the function.
607*/
drhd0960592015-08-17 21:22:32 +0000608static void jsonReturn(
609 JsonNode *pNode, /* Node to return */
610 sqlite3_context *pCtx, /* Return value for this function */
611 sqlite3_value **aReplace /* Array of replacement values */
612){
drh5634cc02015-08-17 11:28:03 +0000613 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000614 default: {
615 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000616 sqlite3_result_null(pCtx);
617 break;
618 }
619 case JSON_TRUE: {
620 sqlite3_result_int(pCtx, 1);
621 break;
622 }
623 case JSON_FALSE: {
624 sqlite3_result_int(pCtx, 0);
625 break;
626 }
drh987eb1f2015-08-17 15:17:37 +0000627 case JSON_INT: {
628 sqlite3_int64 i = 0;
drh285f2ef2021-10-15 16:15:04 +0000629 const char *z;
630 assert( pNode->eU==1 );
631 z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000632 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000633 while( z[0]>='0' && z[0]<='9' ){
634 unsigned v = *(z++) - '0';
635 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000636 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000637 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
638 if( v==9 ) goto int_as_real;
639 if( v==8 ){
640 if( pNode->u.zJContent[0]=='-' ){
641 sqlite3_result_int64(pCtx, SMALLEST_INT64);
642 goto int_done;
643 }else{
644 goto int_as_real;
645 }
646 }
647 }
648 i = i*10 + v;
649 }
drh52216ad2015-08-18 02:28:03 +0000650 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000651 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000652 int_done:
653 break;
drhe85e1da2021-10-01 21:01:07 +0000654 int_as_real: ; /* no break */ deliberate_fall_through
drh8deb4b82015-10-09 18:21:43 +0000655 }
656 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000657 double r;
658#ifdef SQLITE_AMALGAMATION
drh285f2ef2021-10-15 16:15:04 +0000659 const char *z;
660 assert( pNode->eU==1 );
661 z = pNode->u.zJContent;
drh49472652015-10-16 15:35:39 +0000662 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
663#else
drh285f2ef2021-10-15 16:15:04 +0000664 assert( pNode->eU==1 );
drh49472652015-10-16 15:35:39 +0000665 r = strtod(pNode->u.zJContent, 0);
666#endif
drh8deb4b82015-10-09 18:21:43 +0000667 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000668 break;
669 }
drh5634cc02015-08-17 11:28:03 +0000670 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000671#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
672 ** json_insert() and json_replace() and those routines do not
673 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000674 if( pNode->jnFlags & JNODE_RAW ){
drh285f2ef2021-10-15 16:15:04 +0000675 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000676 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
677 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000678 }else
679#endif
680 assert( (pNode->jnFlags & JNODE_RAW)==0 );
681 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000682 /* JSON formatted without any backslash-escapes */
drh285f2ef2021-10-15 16:15:04 +0000683 assert( pNode->eU==1 );
drh52216ad2015-08-18 02:28:03 +0000684 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000685 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000686 }else{
687 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000688 u32 i;
689 u32 n = pNode->n;
drh285f2ef2021-10-15 16:15:04 +0000690 const char *z;
drh987eb1f2015-08-17 15:17:37 +0000691 char *zOut;
692 u32 j;
drh285f2ef2021-10-15 16:15:04 +0000693 assert( pNode->eU==1 );
694 z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000695 zOut = sqlite3_malloc( n+1 );
696 if( zOut==0 ){
697 sqlite3_result_error_nomem(pCtx);
698 break;
699 }
700 for(i=1, j=0; i<n-1; i++){
701 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000702 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000703 zOut[j++] = c;
704 }else{
705 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000706 if( c=='u' ){
drh48eb03b2019-11-10 11:09:06 +0000707 u32 v = jsonHexToInt4(z+i+1);
708 i += 4;
drh80d87402015-08-24 12:42:41 +0000709 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000710 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000711 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000712 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000713 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000714 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000715 }else{
drh48eb03b2019-11-10 11:09:06 +0000716 u32 vlo;
717 if( (v&0xfc00)==0xd800
718 && i<n-6
719 && z[i+1]=='\\'
720 && z[i+2]=='u'
721 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
722 ){
723 /* We have a surrogate pair */
724 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
725 i += 6;
726 zOut[j++] = 0xf0 | (v>>18);
727 zOut[j++] = 0x80 | ((v>>12)&0x3f);
728 zOut[j++] = 0x80 | ((v>>6)&0x3f);
729 zOut[j++] = 0x80 | (v&0x3f);
730 }else{
731 zOut[j++] = 0xe0 | (v>>12);
732 zOut[j++] = 0x80 | ((v>>6)&0x3f);
733 zOut[j++] = 0x80 | (v&0x3f);
734 }
drh987eb1f2015-08-17 15:17:37 +0000735 }
736 }else{
737 if( c=='b' ){
738 c = '\b';
739 }else if( c=='f' ){
740 c = '\f';
741 }else if( c=='n' ){
742 c = '\n';
743 }else if( c=='r' ){
744 c = '\r';
745 }else if( c=='t' ){
746 c = '\t';
747 }
748 zOut[j++] = c;
749 }
750 }
751 }
752 zOut[j] = 0;
753 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000754 }
755 break;
756 }
757 case JSON_ARRAY:
758 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000759 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000760 break;
761 }
762 }
drhbd0621b2015-08-13 13:54:59 +0000763}
764
drh95677942015-09-24 01:06:37 +0000765/* Forward reference */
766static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
767
768/*
769** A macro to hint to the compiler that a function should not be
770** inlined.
771*/
772#if defined(__GNUC__)
773# define JSON_NOINLINE __attribute__((noinline))
774#elif defined(_MSC_VER) && _MSC_VER>=1310
775# define JSON_NOINLINE __declspec(noinline)
776#else
777# define JSON_NOINLINE
778#endif
779
780
781static JSON_NOINLINE int jsonParseAddNodeExpand(
782 JsonParse *pParse, /* Append the node to this object */
783 u32 eType, /* Node type */
784 u32 n, /* Content size or sub-node count */
785 const char *zContent /* Content */
786){
787 u32 nNew;
788 JsonNode *pNew;
789 assert( pParse->nNode>=pParse->nAlloc );
790 if( pParse->oom ) return -1;
791 nNew = pParse->nAlloc*2 + 10;
drh2d77d802019-01-08 20:02:48 +0000792 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
drh95677942015-09-24 01:06:37 +0000793 if( pNew==0 ){
794 pParse->oom = 1;
795 return -1;
796 }
797 pParse->nAlloc = nNew;
798 pParse->aNode = pNew;
799 assert( pParse->nNode<pParse->nAlloc );
800 return jsonParseAddNode(pParse, eType, n, zContent);
801}
802
drh5fa5c102015-08-12 16:49:40 +0000803/*
drhe9c37f32015-08-15 21:25:36 +0000804** Create a new JsonNode instance based on the arguments and append that
805** instance to the JsonParse. Return the index in pParse->aNode[] of the
806** new node, or -1 if a memory allocation fails.
807*/
808static int jsonParseAddNode(
809 JsonParse *pParse, /* Append the node to this object */
810 u32 eType, /* Node type */
811 u32 n, /* Content size or sub-node count */
812 const char *zContent /* Content */
813){
814 JsonNode *p;
drhaa6fe5b2021-10-04 13:18:44 +0000815 if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000816 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000817 }
818 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000819 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000820 p->jnFlags = 0;
drh285f2ef2021-10-15 16:15:04 +0000821 VVA( p->eU = zContent ? 1 : 0 );
drhe9c37f32015-08-15 21:25:36 +0000822 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000823 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000824 return pParse->nNode++;
825}
826
827/*
drhad875e72016-11-07 13:37:28 +0000828** Return true if z[] begins with 4 (or more) hexadecimal digits
829*/
830static int jsonIs4Hex(const char *z){
831 int i;
832 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
833 return 1;
834}
835
836/*
drhe9c37f32015-08-15 21:25:36 +0000837** Parse a single JSON value which begins at pParse->zJson[i]. Return the
838** index of the first character past the end of the value parsed.
839**
840** Return negative for a syntax error. Special cases: return -2 if the
841** first non-whitespace character is '}' and return -3 if the first
842** non-whitespace character is ']'.
843*/
844static int jsonParseValue(JsonParse *pParse, u32 i){
845 char c;
846 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000847 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000848 int x;
drh852944e2015-09-10 03:29:11 +0000849 JsonNode *pNode;
drh9fa866a2017-04-08 18:18:22 +0000850 const char *z = pParse->zJson;
851 while( safe_isspace(z[i]) ){ i++; }
852 if( (c = z[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000853 /* Parse object */
854 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000855 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000856 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000857 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000858 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000859 x = jsonParseValue(pParse, j);
860 if( x<0 ){
drhff6d50e2017-04-11 18:55:05 +0000861 pParse->iDepth--;
drhf27cd1f2015-09-23 01:10:29 +0000862 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000863 return -1;
864 }
drhbe9474e2015-08-22 03:05:54 +0000865 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000866 pNode = &pParse->aNode[pParse->nNode-1];
867 if( pNode->eType!=JSON_STRING ) return -1;
868 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000869 j = x;
drh9fa866a2017-04-08 18:18:22 +0000870 while( safe_isspace(z[j]) ){ j++; }
871 if( z[j]!=':' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000872 j++;
873 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000874 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000875 if( x<0 ) return -1;
876 j = x;
drh9fa866a2017-04-08 18:18:22 +0000877 while( safe_isspace(z[j]) ){ j++; }
878 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000879 if( c==',' ) continue;
880 if( c!='}' ) return -1;
881 break;
882 }
drhbc8f0922015-08-22 19:39:04 +0000883 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000884 return j+1;
885 }else if( c=='[' ){
886 /* Parse array */
887 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000888 if( iThis<0 ) return -1;
drh285f2ef2021-10-15 16:15:04 +0000889 memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
drhe9c37f32015-08-15 21:25:36 +0000890 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000891 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000892 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000893 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000894 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000895 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000896 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000897 return -1;
898 }
899 j = x;
drh9fa866a2017-04-08 18:18:22 +0000900 while( safe_isspace(z[j]) ){ j++; }
901 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000902 if( c==',' ) continue;
903 if( c!=']' ) return -1;
904 break;
905 }
drhbc8f0922015-08-22 19:39:04 +0000906 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000907 return j+1;
908 }else if( c=='"' ){
909 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000910 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000911 j = i+1;
912 for(;;){
drh9fa866a2017-04-08 18:18:22 +0000913 c = z[j];
drh86715382017-04-13 00:12:32 +0000914 if( (c & ~0x1f)==0 ){
915 /* Control characters are not allowed in strings */
916 return -1;
917 }
drhe9c37f32015-08-15 21:25:36 +0000918 if( c=='\\' ){
drh9fa866a2017-04-08 18:18:22 +0000919 c = z[++j];
drhad875e72016-11-07 13:37:28 +0000920 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
921 || c=='n' || c=='r' || c=='t'
drh9fa866a2017-04-08 18:18:22 +0000922 || (c=='u' && jsonIs4Hex(z+j+1)) ){
drhad875e72016-11-07 13:37:28 +0000923 jnFlags = JNODE_ESCAPE;
924 }else{
925 return -1;
926 }
drhe9c37f32015-08-15 21:25:36 +0000927 }else if( c=='"' ){
928 break;
929 }
930 j++;
931 }
drh9fa866a2017-04-08 18:18:22 +0000932 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
drhbe9474e2015-08-22 03:05:54 +0000933 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000934 return j+1;
935 }else if( c=='n'
drh9fa866a2017-04-08 18:18:22 +0000936 && strncmp(z+i,"null",4)==0
937 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000938 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
939 return i+4;
940 }else if( c=='t'
drh9fa866a2017-04-08 18:18:22 +0000941 && strncmp(z+i,"true",4)==0
942 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000943 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
944 return i+4;
945 }else if( c=='f'
drh9fa866a2017-04-08 18:18:22 +0000946 && strncmp(z+i,"false",5)==0
947 && !safe_isalnum(z[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000948 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
949 return i+5;
950 }else if( c=='-' || (c>='0' && c<='9') ){
951 /* Parse number */
952 u8 seenDP = 0;
953 u8 seenE = 0;
drh9fa866a2017-04-08 18:18:22 +0000954 assert( '-' < '0' );
955 if( c<='0' ){
956 j = c=='-' ? i+1 : i;
957 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
958 }
drhe9c37f32015-08-15 21:25:36 +0000959 j = i+1;
960 for(;; j++){
drh9fa866a2017-04-08 18:18:22 +0000961 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000962 if( c>='0' && c<='9' ) continue;
963 if( c=='.' ){
drh9fa866a2017-04-08 18:18:22 +0000964 if( z[j-1]=='-' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000965 if( seenDP ) return -1;
966 seenDP = 1;
967 continue;
968 }
969 if( c=='e' || c=='E' ){
drh9fa866a2017-04-08 18:18:22 +0000970 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000971 if( seenE ) return -1;
972 seenDP = seenE = 1;
drh9fa866a2017-04-08 18:18:22 +0000973 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000974 if( c=='+' || c=='-' ){
975 j++;
drh9fa866a2017-04-08 18:18:22 +0000976 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000977 }
drhd1f00682015-08-29 16:02:37 +0000978 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000979 continue;
980 }
981 break;
982 }
drh9fa866a2017-04-08 18:18:22 +0000983 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000984 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
drh9fa866a2017-04-08 18:18:22 +0000985 j - i, &z[i]);
drhe9c37f32015-08-15 21:25:36 +0000986 return j;
987 }else if( c=='}' ){
988 return -2; /* End of {...} */
989 }else if( c==']' ){
990 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000991 }else if( c==0 ){
992 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000993 }else{
994 return -1; /* Syntax error */
995 }
996}
997
998/*
999** Parse a complete JSON string. Return 0 on success or non-zero if there
1000** are any errors. If an error occurs, free all memory associated with
1001** pParse.
1002**
1003** pParse is uninitialized when this routine is called.
1004*/
drhbc8f0922015-08-22 19:39:04 +00001005static int jsonParse(
1006 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1007 sqlite3_context *pCtx, /* Report errors here */
1008 const char *zJson /* Input JSON text to be parsed */
1009){
drhe9c37f32015-08-15 21:25:36 +00001010 int i;
drhe9c37f32015-08-15 21:25:36 +00001011 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +00001012 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +00001013 pParse->zJson = zJson;
1014 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +00001015 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +00001016 if( i>0 ){
drhff6d50e2017-04-11 18:55:05 +00001017 assert( pParse->iDepth==0 );
dan2e8f5512015-09-17 17:21:09 +00001018 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +00001019 if( zJson[i] ) i = -1;
1020 }
drhd1f00682015-08-29 16:02:37 +00001021 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +00001022 if( pCtx!=0 ){
1023 if( pParse->oom ){
1024 sqlite3_result_error_nomem(pCtx);
1025 }else{
1026 sqlite3_result_error(pCtx, "malformed JSON", -1);
1027 }
1028 }
drh505ad2c2015-08-21 17:33:11 +00001029 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +00001030 return 1;
1031 }
1032 return 0;
1033}
drh301eecc2015-08-17 20:14:19 +00001034
drh505ad2c2015-08-21 17:33:11 +00001035/* Mark node i of pParse as being a child of iParent. Call recursively
1036** to fill in all the descendants of node i.
1037*/
1038static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
1039 JsonNode *pNode = &pParse->aNode[i];
1040 u32 j;
1041 pParse->aUp[i] = iParent;
1042 switch( pNode->eType ){
1043 case JSON_ARRAY: {
1044 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
1045 jsonParseFillInParentage(pParse, i+j, i);
1046 }
1047 break;
1048 }
1049 case JSON_OBJECT: {
1050 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
1051 pParse->aUp[i+j] = i;
1052 jsonParseFillInParentage(pParse, i+j+1, i);
1053 }
1054 break;
1055 }
1056 default: {
1057 break;
1058 }
1059 }
1060}
1061
1062/*
1063** Compute the parentage of all nodes in a completed parse.
1064*/
1065static int jsonParseFindParents(JsonParse *pParse){
1066 u32 *aUp;
1067 assert( pParse->aUp==0 );
drh2d77d802019-01-08 20:02:48 +00001068 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +00001069 if( aUp==0 ){
1070 pParse->oom = 1;
1071 return SQLITE_NOMEM;
1072 }
drh505ad2c2015-08-21 17:33:11 +00001073 jsonParseFillInParentage(pParse, 0, 0);
1074 return SQLITE_OK;
1075}
1076
drh8cb0c832015-09-22 00:21:03 +00001077/*
drh3fb153c2017-05-11 16:49:59 +00001078** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1079*/
drhe35fc302018-08-30 01:52:10 +00001080#define JSON_CACHE_ID (-429938) /* First cache entry */
1081#define JSON_CACHE_SZ 4 /* Max number of cache entries */
drh3fb153c2017-05-11 16:49:59 +00001082
1083/*
1084** Obtain a complete parse of the JSON found in the first argument
1085** of the argv array. Use the sqlite3_get_auxdata() cache for this
1086** parse if it is available. If the cache is not available or if it
1087** is no longer valid, parse the JSON again and return the new parse,
1088** and also register the new parse so that it will be available for
1089** future sqlite3_get_auxdata() calls.
1090*/
1091static JsonParse *jsonParseCached(
1092 sqlite3_context *pCtx,
drhe35fc302018-08-30 01:52:10 +00001093 sqlite3_value **argv,
1094 sqlite3_context *pErrCtx
drh3fb153c2017-05-11 16:49:59 +00001095){
1096 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
1097 int nJson = sqlite3_value_bytes(argv[0]);
1098 JsonParse *p;
drhe35fc302018-08-30 01:52:10 +00001099 JsonParse *pMatch = 0;
1100 int iKey;
1101 int iMinKey = 0;
1102 u32 iMinHold = 0xffffffff;
1103 u32 iMaxHold = 0;
drh3fb153c2017-05-11 16:49:59 +00001104 if( zJson==0 ) return 0;
drhe35fc302018-08-30 01:52:10 +00001105 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
1106 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
1107 if( p==0 ){
1108 iMinKey = iKey;
1109 break;
1110 }
1111 if( pMatch==0
1112 && p->nJson==nJson
1113 && memcmp(p->zJson,zJson,nJson)==0
1114 ){
1115 p->nErr = 0;
1116 pMatch = p;
1117 }else if( p->iHold<iMinHold ){
1118 iMinHold = p->iHold;
1119 iMinKey = iKey;
1120 }
1121 if( p->iHold>iMaxHold ){
1122 iMaxHold = p->iHold;
1123 }
1124 }
1125 if( pMatch ){
1126 pMatch->nErr = 0;
1127 pMatch->iHold = iMaxHold+1;
1128 return pMatch;
drh3fb153c2017-05-11 16:49:59 +00001129 }
drh2d77d802019-01-08 20:02:48 +00001130 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
drh3fb153c2017-05-11 16:49:59 +00001131 if( p==0 ){
1132 sqlite3_result_error_nomem(pCtx);
1133 return 0;
1134 }
1135 memset(p, 0, sizeof(*p));
1136 p->zJson = (char*)&p[1];
1137 memcpy((char*)p->zJson, zJson, nJson+1);
drhe35fc302018-08-30 01:52:10 +00001138 if( jsonParse(p, pErrCtx, p->zJson) ){
drh3fb153c2017-05-11 16:49:59 +00001139 sqlite3_free(p);
1140 return 0;
1141 }
1142 p->nJson = nJson;
drhe35fc302018-08-30 01:52:10 +00001143 p->iHold = iMaxHold+1;
1144 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
1145 (void(*)(void*))jsonParseFree);
1146 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
drh3fb153c2017-05-11 16:49:59 +00001147}
1148
1149/*
drh8cb0c832015-09-22 00:21:03 +00001150** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1151** a match.
1152*/
mistachkinf2c26ed2015-10-12 22:20:29 +00001153static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh285f2ef2021-10-15 16:15:04 +00001154 assert( pNode->eU==1 );
drh8cb0c832015-09-22 00:21:03 +00001155 if( pNode->jnFlags & JNODE_RAW ){
1156 if( pNode->n!=nKey ) return 0;
1157 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
1158 }else{
1159 if( pNode->n!=nKey+2 ) return 0;
1160 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
1161 }
1162}
1163
drh52216ad2015-08-18 02:28:03 +00001164/* forward declaration */
drha7714022015-08-29 00:54:49 +00001165static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +00001166
drh987eb1f2015-08-17 15:17:37 +00001167/*
1168** Search along zPath to find the node specified. Return a pointer
1169** to that node, or NULL if zPath is malformed or if there is no such
1170** node.
drh52216ad2015-08-18 02:28:03 +00001171**
1172** If pApnd!=0, then try to append new nodes to complete zPath if it is
1173** possible to do so and if no existing node corresponds to zPath. If
1174** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +00001175*/
drha7714022015-08-29 00:54:49 +00001176static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +00001177 JsonParse *pParse, /* The JSON to search */
1178 u32 iRoot, /* Begin the search at this node */
1179 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +00001180 int *pApnd, /* Append nodes to complete path if not NULL */
1181 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +00001182){
drhbc8f0922015-08-22 19:39:04 +00001183 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +00001184 const char *zKey;
drh52216ad2015-08-18 02:28:03 +00001185 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +00001186 if( zPath[0]==0 ) return pRoot;
drh7e35e812019-07-31 12:13:58 +00001187 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
drh987eb1f2015-08-17 15:17:37 +00001188 if( zPath[0]=='.' ){
1189 if( pRoot->eType!=JSON_OBJECT ) return 0;
1190 zPath++;
drh6b43cc82015-08-19 23:02:49 +00001191 if( zPath[0]=='"' ){
1192 zKey = zPath + 1;
1193 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
1194 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +00001195 if( zPath[i] ){
1196 i++;
1197 }else{
1198 *pzErr = zPath;
1199 return 0;
1200 }
drh6b43cc82015-08-19 23:02:49 +00001201 }else{
1202 zKey = zPath;
1203 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
1204 nKey = i;
1205 }
drha7714022015-08-29 00:54:49 +00001206 if( nKey==0 ){
1207 *pzErr = zPath;
1208 return 0;
1209 }
drh987eb1f2015-08-17 15:17:37 +00001210 j = 1;
drh52216ad2015-08-18 02:28:03 +00001211 for(;;){
1212 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +00001213 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +00001214 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001215 }
1216 j++;
drh505ad2c2015-08-21 17:33:11 +00001217 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +00001218 }
drh52216ad2015-08-18 02:28:03 +00001219 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001220 assert( pRoot->eU==2 );
drh52216ad2015-08-18 02:28:03 +00001221 iRoot += pRoot->u.iAppend;
1222 pRoot = &pParse->aNode[iRoot];
1223 j = 1;
1224 }
1225 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001226 u32 iStart, iLabel;
1227 JsonNode *pNode;
1228 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
danfe9a8322019-06-17 14:50:33 +00001229 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
drh52216ad2015-08-18 02:28:03 +00001230 zPath += i;
drha7714022015-08-29 00:54:49 +00001231 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001232 if( pParse->oom ) return 0;
1233 if( pNode ){
1234 pRoot = &pParse->aNode[iRoot];
drh285f2ef2021-10-15 16:15:04 +00001235 assert( pRoot->eU==0 );
drhbc8f0922015-08-22 19:39:04 +00001236 pRoot->u.iAppend = iStart - iRoot;
1237 pRoot->jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001238 VVA( pRoot->eU = 2 );
drhbc8f0922015-08-22 19:39:04 +00001239 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1240 }
1241 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001242 }
drh52818642019-11-22 17:37:56 +00001243 }else if( zPath[0]=='[' ){
drh987eb1f2015-08-17 15:17:37 +00001244 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001245 j = 1;
1246 while( safe_isdigit(zPath[j]) ){
1247 i = i*10 + zPath[j] - '0';
1248 j++;
drh987eb1f2015-08-17 15:17:37 +00001249 }
drh52818642019-11-22 17:37:56 +00001250 if( j<2 || zPath[j]!=']' ){
1251 if( zPath[1]=='#' ){
1252 JsonNode *pBase = pRoot;
1253 int iBase = iRoot;
1254 if( pRoot->eType!=JSON_ARRAY ) return 0;
1255 for(;;){
1256 while( j<=pBase->n ){
1257 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
1258 j += jsonNodeSize(&pBase[j]);
1259 }
1260 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001261 assert( pBase->eU==2 );
drh52818642019-11-22 17:37:56 +00001262 iBase += pBase->u.iAppend;
1263 pBase = &pParse->aNode[iBase];
1264 j = 1;
1265 }
1266 j = 2;
1267 if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
1268 unsigned int x = 0;
1269 j = 3;
1270 do{
1271 x = x*10 + zPath[j] - '0';
1272 j++;
1273 }while( safe_isdigit(zPath[j]) );
1274 if( x>i ) return 0;
1275 i -= x;
1276 }
1277 if( zPath[j]!=']' ){
1278 *pzErr = zPath;
1279 return 0;
1280 }
1281 }else{
1282 *pzErr = zPath;
1283 return 0;
1284 }
drha7714022015-08-29 00:54:49 +00001285 }
drh52818642019-11-22 17:37:56 +00001286 if( pRoot->eType!=JSON_ARRAY ) return 0;
drh3d1d2a92015-09-22 01:15:49 +00001287 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001288 j = 1;
drh52216ad2015-08-18 02:28:03 +00001289 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001290 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1291 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001292 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001293 }
1294 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00001295 assert( pRoot->eU==2 );
drh52216ad2015-08-18 02:28:03 +00001296 iRoot += pRoot->u.iAppend;
1297 pRoot = &pParse->aNode[iRoot];
1298 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001299 }
1300 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001301 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001302 }
1303 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001304 u32 iStart;
1305 JsonNode *pNode;
1306 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001307 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001308 if( pParse->oom ) return 0;
1309 if( pNode ){
1310 pRoot = &pParse->aNode[iRoot];
drh285f2ef2021-10-15 16:15:04 +00001311 assert( pRoot->eU==0 );
drhbc8f0922015-08-22 19:39:04 +00001312 pRoot->u.iAppend = iStart - iRoot;
1313 pRoot->jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001314 VVA( pRoot->eU = 2 );
drhbc8f0922015-08-22 19:39:04 +00001315 }
1316 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001317 }
drh3d1d2a92015-09-22 01:15:49 +00001318 }else{
drha7714022015-08-29 00:54:49 +00001319 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001320 }
1321 return 0;
1322}
1323
drh52216ad2015-08-18 02:28:03 +00001324/*
drhbc8f0922015-08-22 19:39:04 +00001325** Append content to pParse that will complete zPath. Return a pointer
1326** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001327*/
1328static JsonNode *jsonLookupAppend(
1329 JsonParse *pParse, /* Append content to the JSON parse */
1330 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001331 int *pApnd, /* Set this flag to 1 */
1332 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001333){
1334 *pApnd = 1;
1335 if( zPath[0]==0 ){
1336 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1337 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1338 }
1339 if( zPath[0]=='.' ){
1340 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1341 }else if( strncmp(zPath,"[0]",3)==0 ){
1342 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1343 }else{
1344 return 0;
1345 }
1346 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001347 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001348}
1349
drhbc8f0922015-08-22 19:39:04 +00001350/*
drha7714022015-08-29 00:54:49 +00001351** Return the text of a syntax error message on a JSON path. Space is
1352** obtained from sqlite3_malloc().
1353*/
1354static char *jsonPathSyntaxError(const char *zErr){
1355 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1356}
1357
1358/*
1359** Do a node lookup using zPath. Return a pointer to the node on success.
1360** Return NULL if not found or if there is an error.
1361**
1362** On an error, write an error message into pCtx and increment the
1363** pParse->nErr counter.
1364**
1365** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1366** nodes are appended.
drha7714022015-08-29 00:54:49 +00001367*/
1368static JsonNode *jsonLookup(
1369 JsonParse *pParse, /* The JSON to search */
1370 const char *zPath, /* The path to search */
1371 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001372 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001373){
1374 const char *zErr = 0;
1375 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001376 char *zMsg;
drha7714022015-08-29 00:54:49 +00001377
1378 if( zPath==0 ) return 0;
1379 if( zPath[0]!='$' ){
1380 zErr = zPath;
1381 goto lookup_err;
1382 }
1383 zPath++;
drha7714022015-08-29 00:54:49 +00001384 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001385 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001386
1387lookup_err:
1388 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001389 assert( zErr!=0 && pCtx!=0 );
1390 zMsg = jsonPathSyntaxError(zErr);
1391 if( zMsg ){
1392 sqlite3_result_error(pCtx, zMsg, -1);
1393 sqlite3_free(zMsg);
1394 }else{
1395 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001396 }
drha7714022015-08-29 00:54:49 +00001397 return 0;
1398}
1399
1400
1401/*
drhbc8f0922015-08-22 19:39:04 +00001402** Report the wrong number of arguments for json_insert(), json_replace()
1403** or json_set().
1404*/
1405static void jsonWrongNumArgs(
1406 sqlite3_context *pCtx,
1407 const char *zFuncName
1408){
1409 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1410 zFuncName);
1411 sqlite3_result_error(pCtx, zMsg, -1);
1412 sqlite3_free(zMsg);
1413}
drh52216ad2015-08-18 02:28:03 +00001414
drh29c99692017-03-24 12:35:17 +00001415/*
1416** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1417*/
1418static void jsonRemoveAllNulls(JsonNode *pNode){
1419 int i, n;
1420 assert( pNode->eType==JSON_OBJECT );
1421 n = pNode->n;
1422 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1423 switch( pNode[i].eType ){
1424 case JSON_NULL:
1425 pNode[i].jnFlags |= JNODE_REMOVE;
1426 break;
1427 case JSON_OBJECT:
1428 jsonRemoveAllNulls(&pNode[i]);
1429 break;
1430 }
1431 }
1432}
1433
drha7714022015-08-29 00:54:49 +00001434
drh987eb1f2015-08-17 15:17:37 +00001435/****************************************************************************
1436** SQL functions used for testing and debugging
1437****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001438
drh301eecc2015-08-17 20:14:19 +00001439#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001440/*
drh5634cc02015-08-17 11:28:03 +00001441** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001442** a parse of the JSON provided. Or it returns NULL if JSON is not
1443** well-formed.
1444*/
drh5634cc02015-08-17 11:28:03 +00001445static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001446 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001447 int argc,
1448 sqlite3_value **argv
1449){
drh505ad2c2015-08-21 17:33:11 +00001450 JsonString s; /* Output string - not real JSON */
1451 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001452 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001453
1454 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001455 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001456 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001457 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001458 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001459 const char *zType;
1460 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1461 assert( x.aNode[i].eType==JSON_STRING );
1462 zType = "label";
1463 }else{
1464 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001465 }
drh852944e2015-09-10 03:29:11 +00001466 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1467 i, zType, x.aNode[i].n, x.aUp[i]);
drh285f2ef2021-10-15 16:15:04 +00001468 assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
drh852944e2015-09-10 03:29:11 +00001469 if( x.aNode[i].u.zJContent!=0 ){
drh285f2ef2021-10-15 16:15:04 +00001470 assert( x.aNode[i].eU==1 );
drh852944e2015-09-10 03:29:11 +00001471 jsonAppendRaw(&s, " ", 1);
1472 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
drh285f2ef2021-10-15 16:15:04 +00001473 }else{
1474 assert( x.aNode[i].eU==0 );
drh852944e2015-09-10 03:29:11 +00001475 }
1476 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001477 }
drh505ad2c2015-08-21 17:33:11 +00001478 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001479 jsonResult(&s);
1480}
1481
drh5634cc02015-08-17 11:28:03 +00001482/*
drhf5ddb9c2015-09-11 00:06:41 +00001483** The json_test1(JSON) function return true (1) if the input is JSON
1484** text generated by another json function. It returns (0) if the input
1485** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001486*/
1487static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001488 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001489 int argc,
1490 sqlite3_value **argv
1491){
mistachkin16a93122015-09-11 18:05:01 +00001492 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001493 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001494}
drh301eecc2015-08-17 20:14:19 +00001495#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001496
drh987eb1f2015-08-17 15:17:37 +00001497/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001498** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001499****************************************************************************/
1500
1501/*
drh2ad96f52016-06-17 13:01:51 +00001502** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1503** corresponding to the SQL value input. Mostly this means putting
1504** double-quotes around strings and returning the unquoted string "null"
1505** when given a NULL input.
1506*/
1507static void jsonQuoteFunc(
1508 sqlite3_context *ctx,
1509 int argc,
1510 sqlite3_value **argv
1511){
1512 JsonString jx;
drhb0df5402016-08-01 17:06:44 +00001513 UNUSED_PARAM(argc);
drh2ad96f52016-06-17 13:01:51 +00001514
1515 jsonInit(&jx, ctx);
1516 jsonAppendValue(&jx, argv[0]);
1517 jsonResult(&jx);
1518 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1519}
1520
1521/*
drh987eb1f2015-08-17 15:17:37 +00001522** Implementation of the json_array(VALUE,...) function. Return a JSON
1523** array that contains all values given in arguments. Or if any argument
1524** is a BLOB, throw an error.
1525*/
1526static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001527 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001528 int argc,
1529 sqlite3_value **argv
1530){
1531 int i;
drh505ad2c2015-08-21 17:33:11 +00001532 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001533
drhbc8f0922015-08-22 19:39:04 +00001534 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001535 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001536 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001537 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001538 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001539 }
drhd0960592015-08-17 21:22:32 +00001540 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001541 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001542 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001543}
1544
1545
1546/*
1547** json_array_length(JSON)
1548** json_array_length(JSON, PATH)
1549**
1550** Return the number of elements in the top-level JSON array.
1551** Return 0 if the input is not a well-formed JSON array.
1552*/
1553static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001554 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001555 int argc,
1556 sqlite3_value **argv
1557){
drh3fb153c2017-05-11 16:49:59 +00001558 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001559 sqlite3_int64 n = 0;
1560 u32 i;
drha8f39a92015-09-21 22:53:16 +00001561 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001562
drhe35fc302018-08-30 01:52:10 +00001563 p = jsonParseCached(ctx, argv, ctx);
drh3fb153c2017-05-11 16:49:59 +00001564 if( p==0 ) return;
1565 assert( p->nNode );
drha8f39a92015-09-21 22:53:16 +00001566 if( argc==2 ){
1567 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
drh3fb153c2017-05-11 16:49:59 +00001568 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00001569 }else{
drh3fb153c2017-05-11 16:49:59 +00001570 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00001571 }
1572 if( pNode==0 ){
drh3fb153c2017-05-11 16:49:59 +00001573 return;
1574 }
1575 if( pNode->eType==JSON_ARRAY ){
drha8f39a92015-09-21 22:53:16 +00001576 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1577 for(i=1; i<=pNode->n; n++){
1578 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001579 }
drh987eb1f2015-08-17 15:17:37 +00001580 }
drh3fb153c2017-05-11 16:49:59 +00001581 sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001582}
1583
1584/*
drh3ad93bb2015-08-29 19:41:45 +00001585** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001586**
drh3ad93bb2015-08-29 19:41:45 +00001587** Return the element described by PATH. Return NULL if there is no
1588** PATH element. If there are multiple PATHs, then return a JSON array
1589** with the result from each path. Throw an error if the JSON or any PATH
1590** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001591*/
1592static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001593 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001594 int argc,
1595 sqlite3_value **argv
1596){
drh3fb153c2017-05-11 16:49:59 +00001597 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001598 JsonNode *pNode;
1599 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001600 JsonString jx;
drh3ad93bb2015-08-29 19:41:45 +00001601
1602 if( argc<2 ) return;
drhe35fc302018-08-30 01:52:10 +00001603 p = jsonParseCached(ctx, argv, ctx);
drh3fb153c2017-05-11 16:49:59 +00001604 if( p==0 ) return;
drh12b9fa92022-01-07 15:47:12 +00001605 if( argc==2 ){
1606 /* With a single PATH argument, the return is the unquoted SQL value */
1607 zPath = (const char*)sqlite3_value_text(argv[1]);
1608 if( zPath && zPath[0]!='$' && zPath[0]!=0
1609 && *(int*)sqlite3_user_data(ctx)
1610 ){
1611 /* The -> and ->> operators accept abbreviated PATH arguments:
1612 ** NUMBER ==> $[NUMBER]
1613 ** LABEL ==> $.LABEL
1614 ** [NUMBER] ==> $[NUMBER]
1615 */
1616 jsonInit(&jx, ctx);
1617 if( safe_isdigit(zPath[0]) ){
1618 jsonAppendRaw(&jx, "$[", 2);
1619 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
1620 jsonAppendRaw(&jx, "]", 2);
1621 }else{
1622 jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
1623 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
1624 jsonAppendChar(&jx, 0);
1625 }
1626 pNode = jsonLookup(p, jx.zBuf, 0, ctx);
1627 jsonReset(&jx);
1628 }else{
1629 pNode = jsonLookup(p, zPath, 0, ctx);
1630 }
1631 if( p->nErr ) return;
1632 if( pNode ) jsonReturn(pNode, ctx, 0);
1633 }else{
1634 /* Two or more PATH arguments results in a JSON array with each
1635 ** element of the array being the value selected by one of the PATHs */
1636 int i;
1637 jsonInit(&jx, ctx);
1638 jsonAppendChar(&jx, '[');
1639 for(i=1; i<argc; i++){
1640 zPath = (const char*)sqlite3_value_text(argv[i]);
1641 pNode = jsonLookup(p, zPath, 0, ctx);
1642 if( p->nErr ) break;
drh3ad93bb2015-08-29 19:41:45 +00001643 jsonAppendSeparator(&jx);
1644 if( pNode ){
1645 jsonRenderNode(pNode, &jx, 0);
1646 }else{
1647 jsonAppendRaw(&jx, "null", 4);
1648 }
drh3ad93bb2015-08-29 19:41:45 +00001649 }
drh12b9fa92022-01-07 15:47:12 +00001650 if( i==argc ){
1651 jsonAppendChar(&jx, ']');
1652 jsonResult(&jx);
1653 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1654 }
1655 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001656 }
drh987eb1f2015-08-17 15:17:37 +00001657}
1658
drh633647a2017-03-22 21:24:31 +00001659/* This is the RFC 7396 MergePatch algorithm.
1660*/
1661static JsonNode *jsonMergePatch(
1662 JsonParse *pParse, /* The JSON parser that contains the TARGET */
mistachkinb1ed7172017-04-14 14:50:34 +00001663 u32 iTarget, /* Node of the TARGET in pParse */
drh633647a2017-03-22 21:24:31 +00001664 JsonNode *pPatch /* The PATCH */
1665){
drh0002d242017-03-23 00:46:15 +00001666 u32 i, j;
1667 u32 iRoot;
drh633647a2017-03-22 21:24:31 +00001668 JsonNode *pTarget;
1669 if( pPatch->eType!=JSON_OBJECT ){
1670 return pPatch;
1671 }
1672 assert( iTarget>=0 && iTarget<pParse->nNode );
1673 pTarget = &pParse->aNode[iTarget];
1674 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1675 if( pTarget->eType!=JSON_OBJECT ){
drh29c99692017-03-24 12:35:17 +00001676 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001677 return pPatch;
1678 }
drhbb7aa2d2017-03-23 00:13:52 +00001679 iRoot = iTarget;
drh633647a2017-03-22 21:24:31 +00001680 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
drhba7cce32017-03-24 19:45:05 +00001681 u32 nKey;
drh633647a2017-03-22 21:24:31 +00001682 const char *zKey;
1683 assert( pPatch[i].eType==JSON_STRING );
1684 assert( pPatch[i].jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00001685 assert( pPatch[i].eU==1 );
drh633647a2017-03-22 21:24:31 +00001686 nKey = pPatch[i].n;
1687 zKey = pPatch[i].u.zJContent;
drhbb7aa2d2017-03-23 00:13:52 +00001688 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh633647a2017-03-22 21:24:31 +00001689 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1690 assert( pTarget[j].eType==JSON_STRING );
1691 assert( pTarget[j].jnFlags & JNODE_LABEL );
drhbb7aa2d2017-03-23 00:13:52 +00001692 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh1fe162f2017-03-23 12:56:44 +00001693 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1694 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
drh633647a2017-03-22 21:24:31 +00001695 if( pPatch[i+1].eType==JSON_NULL ){
1696 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1697 }else{
drhbb7aa2d2017-03-23 00:13:52 +00001698 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
drh633647a2017-03-22 21:24:31 +00001699 if( pNew==0 ) return 0;
1700 pTarget = &pParse->aNode[iTarget];
1701 if( pNew!=&pTarget[j+1] ){
drha2852ac2021-11-15 01:45:11 +00001702 assert( pTarget[j+1].eU==0
1703 || pTarget[j+1].eU==1
1704 || pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001705 testcase( pTarget[j+1].eU==1 );
drha2852ac2021-11-15 01:45:11 +00001706 testcase( pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001707 VVA( pTarget[j+1].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001708 pTarget[j+1].u.pPatch = pNew;
1709 pTarget[j+1].jnFlags |= JNODE_PATCH;
1710 }
1711 }
1712 break;
1713 }
1714 }
drhbb7aa2d2017-03-23 00:13:52 +00001715 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
drh633647a2017-03-22 21:24:31 +00001716 int iStart, iPatch;
1717 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1718 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1719 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1720 if( pParse->oom ) return 0;
drh29c99692017-03-24 12:35:17 +00001721 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001722 pTarget = &pParse->aNode[iTarget];
drh8b554e22021-10-20 20:22:37 +00001723 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
1724 testcase( pParse->aNode[iRoot].eU==2 );
drhbb7aa2d2017-03-23 00:13:52 +00001725 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001726 VVA( pParse->aNode[iRoot].eU = 2 );
drhbb7aa2d2017-03-23 00:13:52 +00001727 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
1728 iRoot = iStart;
drh285f2ef2021-10-15 16:15:04 +00001729 assert( pParse->aNode[iPatch].eU==0 );
1730 VVA( pParse->aNode[iPatch].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001731 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
1732 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
1733 }
1734 }
1735 return pTarget;
1736}
1737
1738/*
1739** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1740** object that is the result of running the RFC 7396 MergePatch() algorithm
1741** on the two arguments.
1742*/
drh37f03df2017-03-23 20:33:49 +00001743static void jsonPatchFunc(
drh633647a2017-03-22 21:24:31 +00001744 sqlite3_context *ctx,
1745 int argc,
1746 sqlite3_value **argv
1747){
1748 JsonParse x; /* The JSON that is being patched */
1749 JsonParse y; /* The patch */
drhbb7aa2d2017-03-23 00:13:52 +00001750 JsonNode *pResult; /* The result of the merge */
drh633647a2017-03-22 21:24:31 +00001751
drh2fb79e92017-03-25 12:08:11 +00001752 UNUSED_PARAM(argc);
drh633647a2017-03-22 21:24:31 +00001753 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1754 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
1755 jsonParseReset(&x);
1756 return;
1757 }
drhbb7aa2d2017-03-23 00:13:52 +00001758 pResult = jsonMergePatch(&x, 0, y.aNode);
1759 assert( pResult!=0 || x.oom );
1760 if( pResult ){
1761 jsonReturnJson(pResult, ctx, 0);
1762 }else{
1763 sqlite3_result_error_nomem(ctx);
1764 }
drh633647a2017-03-22 21:24:31 +00001765 jsonParseReset(&x);
1766 jsonParseReset(&y);
1767}
1768
1769
drh987eb1f2015-08-17 15:17:37 +00001770/*
1771** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1772** object that contains all name/value given in arguments. Or if any name
1773** is not a string or if any value is a BLOB, throw an error.
1774*/
1775static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001776 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001777 int argc,
1778 sqlite3_value **argv
1779){
1780 int i;
drh505ad2c2015-08-21 17:33:11 +00001781 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001782 const char *z;
1783 u32 n;
1784
1785 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001786 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001787 "of arguments", -1);
1788 return;
1789 }
drhbc8f0922015-08-22 19:39:04 +00001790 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001791 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001792 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001793 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001794 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001795 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001796 return;
1797 }
drhd0960592015-08-17 21:22:32 +00001798 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001799 z = (const char*)sqlite3_value_text(argv[i]);
1800 n = (u32)sqlite3_value_bytes(argv[i]);
1801 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001802 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001803 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001804 }
drhd0960592015-08-17 21:22:32 +00001805 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001806 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001807 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001808}
1809
1810
1811/*
drh301eecc2015-08-17 20:14:19 +00001812** json_remove(JSON, PATH, ...)
1813**
drh3ad93bb2015-08-29 19:41:45 +00001814** Remove the named elements from JSON and return the result. malformed
1815** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001816*/
1817static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001818 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001819 int argc,
1820 sqlite3_value **argv
1821){
1822 JsonParse x; /* The parse */
1823 JsonNode *pNode;
1824 const char *zPath;
1825 u32 i;
1826
1827 if( argc<1 ) return;
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++){
1831 zPath = (const char*)sqlite3_value_text(argv[i]);
1832 if( zPath==0 ) goto remove_done;
1833 pNode = jsonLookup(&x, zPath, 0, ctx);
1834 if( x.nErr ) goto remove_done;
1835 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1836 }
1837 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1838 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001839 }
drha7714022015-08-29 00:54:49 +00001840remove_done:
drh505ad2c2015-08-21 17:33:11 +00001841 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001842}
1843
1844/*
1845** json_replace(JSON, PATH, VALUE, ...)
1846**
1847** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001848** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001849*/
1850static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001851 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001852 int argc,
1853 sqlite3_value **argv
1854){
1855 JsonParse x; /* The parse */
1856 JsonNode *pNode;
1857 const char *zPath;
1858 u32 i;
1859
1860 if( argc<1 ) return;
1861 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001862 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001863 return;
1864 }
drhbc8f0922015-08-22 19:39:04 +00001865 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001866 assert( x.nNode );
1867 for(i=1; i<(u32)argc; i+=2){
1868 zPath = (const char*)sqlite3_value_text(argv[i]);
1869 pNode = jsonLookup(&x, zPath, 0, ctx);
1870 if( x.nErr ) goto replace_err;
1871 if( pNode ){
drh285f2ef2021-10-15 16:15:04 +00001872 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
1873 json_testcase( pNode->eU!=0 && pNode->eU!=1 );
drha8f39a92015-09-21 22:53:16 +00001874 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh285f2ef2021-10-15 16:15:04 +00001875 VVA( pNode->eU = 4 );
drh633647a2017-03-22 21:24:31 +00001876 pNode->u.iReplace = i + 1;
drhd0960592015-08-17 21:22:32 +00001877 }
drha8f39a92015-09-21 22:53:16 +00001878 }
1879 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001880 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001881 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001882 }else{
1883 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001884 }
drha7714022015-08-29 00:54:49 +00001885replace_err:
drh505ad2c2015-08-21 17:33:11 +00001886 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001887}
drh505ad2c2015-08-21 17:33:11 +00001888
drh52216ad2015-08-18 02:28:03 +00001889/*
1890** json_set(JSON, PATH, VALUE, ...)
1891**
1892** Set the value at PATH to VALUE. Create the PATH if it does not already
1893** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001894** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001895**
1896** json_insert(JSON, PATH, VALUE, ...)
1897**
1898** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001899** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001900*/
1901static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001902 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001903 int argc,
1904 sqlite3_value **argv
1905){
1906 JsonParse x; /* The parse */
1907 JsonNode *pNode;
1908 const char *zPath;
1909 u32 i;
1910 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001911 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001912
1913 if( argc<1 ) return;
1914 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001915 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001916 return;
1917 }
drhbc8f0922015-08-22 19:39:04 +00001918 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001919 assert( x.nNode );
1920 for(i=1; i<(u32)argc; i+=2){
1921 zPath = (const char*)sqlite3_value_text(argv[i]);
1922 bApnd = 0;
1923 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1924 if( x.oom ){
1925 sqlite3_result_error_nomem(ctx);
1926 goto jsonSetDone;
1927 }else if( x.nErr ){
1928 goto jsonSetDone;
1929 }else if( pNode && (bApnd || bIsSet) ){
drh285f2ef2021-10-15 16:15:04 +00001930 json_testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 );
1931 assert( pNode->eU!=3 || pNode->eU!=5 );
1932 VVA( pNode->eU = 4 );
drha8f39a92015-09-21 22:53:16 +00001933 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001934 pNode->u.iReplace = i + 1;
drh52216ad2015-08-18 02:28:03 +00001935 }
drha8f39a92015-09-21 22:53:16 +00001936 }
1937 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001938 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001939 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001940 }else{
1941 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001942 }
drhbc8f0922015-08-22 19:39:04 +00001943jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001944 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001945}
drh301eecc2015-08-17 20:14:19 +00001946
1947/*
drh987eb1f2015-08-17 15:17:37 +00001948** json_type(JSON)
drha4e4e182022-01-07 16:03:00 +00001949** json_ntype(JSON)
drh987eb1f2015-08-17 15:17:37 +00001950** json_type(JSON, PATH)
1951**
drha4e4e182022-01-07 16:03:00 +00001952** Return the top-level "type" of a JSON string. json_type() raises an
1953** error if either the JSON or PATH inputs are not well-formed. json_ntype()
1954** works like the one-argument version of json_type() except that it
1955** returns NULL if the JSON argument is not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001956*/
1957static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001958 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001959 int argc,
1960 sqlite3_value **argv
1961){
drhe35fc302018-08-30 01:52:10 +00001962 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001963 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001964 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001965
drha4e4e182022-01-07 16:03:00 +00001966 p = jsonParseCached(ctx, argv, *(int*)sqlite3_user_data(ctx) ? 0 : ctx);
drhe35fc302018-08-30 01:52:10 +00001967 if( p==0 ) return;
drha8f39a92015-09-21 22:53:16 +00001968 if( argc==2 ){
1969 zPath = (const char*)sqlite3_value_text(argv[1]);
drhe35fc302018-08-30 01:52:10 +00001970 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00001971 }else{
drhe35fc302018-08-30 01:52:10 +00001972 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00001973 }
1974 if( pNode ){
1975 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001976 }
drh987eb1f2015-08-17 15:17:37 +00001977}
drh5634cc02015-08-17 11:28:03 +00001978
drhbc8f0922015-08-22 19:39:04 +00001979/*
1980** json_valid(JSON)
1981**
drh3ad93bb2015-08-29 19:41:45 +00001982** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1983** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001984*/
1985static void jsonValidFunc(
1986 sqlite3_context *ctx,
1987 int argc,
1988 sqlite3_value **argv
1989){
drhe35fc302018-08-30 01:52:10 +00001990 JsonParse *p; /* The parse */
mistachkin16a93122015-09-11 18:05:01 +00001991 UNUSED_PARAM(argc);
drhe35fc302018-08-30 01:52:10 +00001992 p = jsonParseCached(ctx, argv, 0);
1993 sqlite3_result_int(ctx, p!=0);
drhbc8f0922015-08-22 19:39:04 +00001994}
1995
drhff135ae2015-12-30 01:07:02 +00001996
1997/****************************************************************************
1998** Aggregate SQL function implementations
1999****************************************************************************/
2000/*
2001** json_group_array(VALUE)
2002**
2003** Return a JSON array composed of all values in the aggregate.
2004*/
2005static void jsonArrayStep(
2006 sqlite3_context *ctx,
2007 int argc,
2008 sqlite3_value **argv
2009){
2010 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00002011 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002012 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2013 if( pStr ){
2014 if( pStr->zBuf==0 ){
2015 jsonInit(pStr, ctx);
2016 jsonAppendChar(pStr, '[');
drhfab5b072019-09-14 00:21:34 +00002017 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002018 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002019 }
dan8505d732021-04-14 12:11:39 +00002020 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002021 jsonAppendValue(pStr, argv[0]);
2022 }
2023}
drh8be47a72018-07-05 20:05:29 +00002024static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002025 JsonString *pStr;
2026 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2027 if( pStr ){
2028 pStr->pCtx = ctx;
2029 jsonAppendChar(pStr, ']');
2030 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00002031 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002032 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002033 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002034 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002035 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2036 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002037 }else{
mistachkined008ec2018-09-12 01:05:26 +00002038 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002039 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002040 }
2041 }else{
2042 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
2043 }
2044 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2045}
drh8be47a72018-07-05 20:05:29 +00002046static void jsonArrayValue(sqlite3_context *ctx){
2047 jsonArrayCompute(ctx, 0);
2048}
2049static void jsonArrayFinal(sqlite3_context *ctx){
2050 jsonArrayCompute(ctx, 1);
2051}
2052
2053#ifndef SQLITE_OMIT_WINDOWFUNC
2054/*
2055** This method works for both json_group_array() and json_group_object().
2056** It works by removing the first element of the group by searching forward
2057** to the first comma (",") that is not within a string and deleting all
2058** text through that comma.
2059*/
2060static void jsonGroupInverse(
2061 sqlite3_context *ctx,
2062 int argc,
2063 sqlite3_value **argv
2064){
drhe39f3882019-09-21 17:31:03 +00002065 unsigned int i;
drh8be47a72018-07-05 20:05:29 +00002066 int inStr = 0;
drhfab5b072019-09-14 00:21:34 +00002067 int nNest = 0;
drh8be47a72018-07-05 20:05:29 +00002068 char *z;
drhfab5b072019-09-14 00:21:34 +00002069 char c;
drh8be47a72018-07-05 20:05:29 +00002070 JsonString *pStr;
drhc7bf5712018-07-09 22:49:01 +00002071 UNUSED_PARAM(argc);
2072 UNUSED_PARAM(argv);
drh8be47a72018-07-05 20:05:29 +00002073 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
drh491d4c82018-07-07 20:23:46 +00002074#ifdef NEVER
drhfd4b7282018-07-07 19:47:21 +00002075 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2076 ** always have been called to initalize it */
2077 if( NEVER(!pStr) ) return;
drh491d4c82018-07-07 20:23:46 +00002078#endif
drh8be47a72018-07-05 20:05:29 +00002079 z = pStr->zBuf;
drh0e5cd342021-04-13 01:12:32 +00002080 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
drhfab5b072019-09-14 00:21:34 +00002081 if( c=='"' ){
drh8be47a72018-07-05 20:05:29 +00002082 inStr = !inStr;
drhfab5b072019-09-14 00:21:34 +00002083 }else if( c=='\\' ){
drh8be47a72018-07-05 20:05:29 +00002084 i++;
drhfab5b072019-09-14 00:21:34 +00002085 }else if( !inStr ){
2086 if( c=='{' || c=='[' ) nNest++;
2087 if( c=='}' || c==']' ) nNest--;
drh8be47a72018-07-05 20:05:29 +00002088 }
2089 }
drh0e5cd342021-04-13 01:12:32 +00002090 if( i<pStr->nUsed ){
2091 pStr->nUsed -= i;
2092 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
2093 z[pStr->nUsed] = 0;
2094 }else{
2095 pStr->nUsed = 1;
2096 }
drh8be47a72018-07-05 20:05:29 +00002097}
2098#else
2099# define jsonGroupInverse 0
2100#endif
2101
drhff135ae2015-12-30 01:07:02 +00002102
2103/*
2104** json_group_obj(NAME,VALUE)
2105**
2106** Return a JSON object composed of all names and values in the aggregate.
2107*/
2108static void jsonObjectStep(
2109 sqlite3_context *ctx,
2110 int argc,
2111 sqlite3_value **argv
2112){
2113 JsonString *pStr;
2114 const char *z;
2115 u32 n;
drhdf3a9072016-02-11 15:37:18 +00002116 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002117 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2118 if( pStr ){
2119 if( pStr->zBuf==0 ){
2120 jsonInit(pStr, ctx);
2121 jsonAppendChar(pStr, '{');
drhfab5b072019-09-14 00:21:34 +00002122 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002123 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002124 }
drhd2f55772021-05-28 12:15:19 +00002125 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002126 z = (const char*)sqlite3_value_text(argv[0]);
2127 n = (u32)sqlite3_value_bytes(argv[0]);
2128 jsonAppendString(pStr, z, n);
2129 jsonAppendChar(pStr, ':');
2130 jsonAppendValue(pStr, argv[1]);
2131 }
2132}
drh8be47a72018-07-05 20:05:29 +00002133static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002134 JsonString *pStr;
2135 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2136 if( pStr ){
2137 jsonAppendChar(pStr, '}');
2138 if( pStr->bErr ){
drh6850a632016-11-07 18:18:08 +00002139 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002140 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002141 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002142 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002143 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2144 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002145 }else{
mistachkined008ec2018-09-12 01:05:26 +00002146 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002147 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002148 }
2149 }else{
2150 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
2151 }
2152 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2153}
drh8be47a72018-07-05 20:05:29 +00002154static void jsonObjectValue(sqlite3_context *ctx){
2155 jsonObjectCompute(ctx, 0);
2156}
2157static void jsonObjectFinal(sqlite3_context *ctx){
2158 jsonObjectCompute(ctx, 1);
2159}
2160
drhff135ae2015-12-30 01:07:02 +00002161
2162
drhd2975922015-08-29 17:22:33 +00002163#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00002164/****************************************************************************
2165** The json_each virtual table
2166****************************************************************************/
2167typedef struct JsonEachCursor JsonEachCursor;
2168struct JsonEachCursor {
2169 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00002170 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00002171 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00002172 u32 i; /* Index in sParse.aNode[] of current row */
2173 u32 iEnd; /* EOF when i equals or exceeds this value */
2174 u8 eType; /* Type of top-level element */
2175 u8 bRecursive; /* True for json_tree(). False for json_each() */
2176 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00002177 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00002178 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00002179};
2180
2181/* Constructor for the json_each virtual table */
2182static int jsonEachConnect(
2183 sqlite3 *db,
2184 void *pAux,
2185 int argc, const char *const*argv,
2186 sqlite3_vtab **ppVtab,
2187 char **pzErr
2188){
2189 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00002190 int rc;
drhcb6c6c62015-08-19 22:47:17 +00002191
2192/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00002193#define JEACH_KEY 0
2194#define JEACH_VALUE 1
2195#define JEACH_TYPE 2
2196#define JEACH_ATOM 3
2197#define JEACH_ID 4
2198#define JEACH_PARENT 5
2199#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00002200#define JEACH_PATH 7
drh43579192018-11-16 16:04:50 +00002201/* The xBestIndex method assumes that the JSON and ROOT columns are
2202** the last two columns in the table. Should this ever changes, be
2203** sure to update the xBestIndex method. */
drh383de692015-09-10 17:20:57 +00002204#define JEACH_JSON 8
2205#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00002206
drh6fd5c1e2015-08-21 20:37:12 +00002207 UNUSED_PARAM(pzErr);
2208 UNUSED_PARAM(argv);
2209 UNUSED_PARAM(argc);
2210 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00002211 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00002212 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2213 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00002214 if( rc==SQLITE_OK ){
2215 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
2216 if( pNew==0 ) return SQLITE_NOMEM;
2217 memset(pNew, 0, sizeof(*pNew));
drh2b1c2aa2020-01-07 19:45:40 +00002218 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
drh505ad2c2015-08-21 17:33:11 +00002219 }
2220 return rc;
drhcb6c6c62015-08-19 22:47:17 +00002221}
2222
2223/* destructor for json_each virtual table */
2224static int jsonEachDisconnect(sqlite3_vtab *pVtab){
2225 sqlite3_free(pVtab);
2226 return SQLITE_OK;
2227}
2228
drh505ad2c2015-08-21 17:33:11 +00002229/* constructor for a JsonEachCursor object for json_each(). */
2230static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00002231 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00002232
2233 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00002234 pCur = sqlite3_malloc( sizeof(*pCur) );
2235 if( pCur==0 ) return SQLITE_NOMEM;
2236 memset(pCur, 0, sizeof(*pCur));
2237 *ppCursor = &pCur->base;
2238 return SQLITE_OK;
2239}
2240
drh505ad2c2015-08-21 17:33:11 +00002241/* constructor for a JsonEachCursor object for json_tree(). */
2242static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2243 int rc = jsonEachOpenEach(p, ppCursor);
2244 if( rc==SQLITE_OK ){
2245 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
2246 pCur->bRecursive = 1;
2247 }
2248 return rc;
2249}
2250
drhcb6c6c62015-08-19 22:47:17 +00002251/* Reset a JsonEachCursor back to its original state. Free any memory
2252** held. */
2253static void jsonEachCursorReset(JsonEachCursor *p){
2254 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00002255 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00002256 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00002257 p->iRowid = 0;
2258 p->i = 0;
2259 p->iEnd = 0;
2260 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00002261 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00002262 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002263}
2264
2265/* Destructor for a jsonEachCursor object */
2266static int jsonEachClose(sqlite3_vtab_cursor *cur){
2267 JsonEachCursor *p = (JsonEachCursor*)cur;
2268 jsonEachCursorReset(p);
2269 sqlite3_free(cur);
2270 return SQLITE_OK;
2271}
2272
2273/* Return TRUE if the jsonEachCursor object has been advanced off the end
2274** of the JSON object */
2275static int jsonEachEof(sqlite3_vtab_cursor *cur){
2276 JsonEachCursor *p = (JsonEachCursor*)cur;
2277 return p->i >= p->iEnd;
2278}
2279
drh505ad2c2015-08-21 17:33:11 +00002280/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00002281static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00002282 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00002283 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00002284 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
2285 p->i++;
drh4af352d2015-08-21 20:02:48 +00002286 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00002287 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00002288 u32 iUp = p->sParse.aUp[p->i];
2289 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00002290 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00002291 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002292 assert( pUp->eU==0 || pUp->eU==3 );
2293 json_testcase( pUp->eU==3 );
2294 VVA( pUp->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002295 if( iUp==p->i-1 ){
2296 pUp->u.iKey = 0;
2297 }else{
2298 pUp->u.iKey++;
2299 }
drh4af352d2015-08-21 20:02:48 +00002300 }
2301 }
drh505ad2c2015-08-21 17:33:11 +00002302 }else{
drh4af352d2015-08-21 20:02:48 +00002303 switch( p->eType ){
2304 case JSON_ARRAY: {
2305 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
2306 p->iRowid++;
2307 break;
2308 }
2309 case JSON_OBJECT: {
2310 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
2311 p->iRowid++;
2312 break;
2313 }
2314 default: {
2315 p->i = p->iEnd;
2316 break;
2317 }
drh505ad2c2015-08-21 17:33:11 +00002318 }
2319 }
2320 return SQLITE_OK;
2321}
2322
drh4af352d2015-08-21 20:02:48 +00002323/* Append the name of the path for element i to pStr
2324*/
2325static void jsonEachComputePath(
2326 JsonEachCursor *p, /* The cursor */
2327 JsonString *pStr, /* Write the path here */
2328 u32 i /* Path to this element */
2329){
2330 JsonNode *pNode, *pUp;
2331 u32 iUp;
2332 if( i==0 ){
2333 jsonAppendChar(pStr, '$');
2334 return;
drhcb6c6c62015-08-19 22:47:17 +00002335 }
drh4af352d2015-08-21 20:02:48 +00002336 iUp = p->sParse.aUp[i];
2337 jsonEachComputePath(p, pStr, iUp);
2338 pNode = &p->sParse.aNode[i];
2339 pUp = &p->sParse.aNode[iUp];
2340 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002341 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
2342 testcase( pUp->eU==0 );
drh4af352d2015-08-21 20:02:48 +00002343 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
2344 }else{
2345 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00002346 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00002347 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00002348 assert( pNode->jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00002349 assert( pNode->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002350 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
2351 }
drhcb6c6c62015-08-19 22:47:17 +00002352}
2353
2354/* Return the value of a column */
2355static int jsonEachColumn(
2356 sqlite3_vtab_cursor *cur, /* The cursor */
2357 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2358 int i /* Which column to return */
2359){
2360 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00002361 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00002362 switch( i ){
2363 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00002364 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00002365 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00002366 jsonReturn(pThis, ctx, 0);
2367 }else if( p->eType==JSON_ARRAY ){
2368 u32 iKey;
2369 if( p->bRecursive ){
2370 if( p->iRowid==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00002371 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
drh8784eca2015-08-23 02:42:30 +00002372 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00002373 }else{
2374 iKey = p->iRowid;
2375 }
drh6fd5c1e2015-08-21 20:37:12 +00002376 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00002377 }
2378 break;
2379 }
2380 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00002381 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002382 jsonReturn(pThis, ctx, 0);
2383 break;
2384 }
2385 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00002386 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002387 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2388 break;
2389 }
2390 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00002391 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002392 if( pThis->eType>=JSON_ARRAY ) break;
2393 jsonReturn(pThis, ctx, 0);
2394 break;
2395 }
2396 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00002397 sqlite3_result_int64(ctx,
2398 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00002399 break;
2400 }
2401 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00002402 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00002403 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00002404 }
2405 break;
2406 }
drh4af352d2015-08-21 20:02:48 +00002407 case JEACH_FULLKEY: {
2408 JsonString x;
2409 jsonInit(&x, ctx);
2410 if( p->bRecursive ){
2411 jsonEachComputePath(p, &x, p->i);
2412 }else{
drh383de692015-09-10 17:20:57 +00002413 if( p->zRoot ){
2414 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00002415 }else{
2416 jsonAppendChar(&x, '$');
2417 }
2418 if( p->eType==JSON_ARRAY ){
2419 jsonPrintf(30, &x, "[%d]", p->iRowid);
drhdd7460f2018-05-16 12:19:11 +00002420 }else if( p->eType==JSON_OBJECT ){
drh285f2ef2021-10-15 16:15:04 +00002421 assert( pThis->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002422 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2423 }
2424 }
2425 jsonResult(&x);
2426 break;
2427 }
drhcb6c6c62015-08-19 22:47:17 +00002428 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00002429 if( p->bRecursive ){
2430 JsonString x;
2431 jsonInit(&x, ctx);
2432 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2433 jsonResult(&x);
2434 break;
drh4af352d2015-08-21 20:02:48 +00002435 }
drh383de692015-09-10 17:20:57 +00002436 /* For json_each() path and root are the same so fall through
2437 ** into the root case */
drh08b92082020-08-10 14:18:00 +00002438 /* no break */ deliberate_fall_through
drh383de692015-09-10 17:20:57 +00002439 }
drh6850a632016-11-07 18:18:08 +00002440 default: {
drh383de692015-09-10 17:20:57 +00002441 const char *zRoot = p->zRoot;
drh6850a632016-11-07 18:18:08 +00002442 if( zRoot==0 ) zRoot = "$";
drh383de692015-09-10 17:20:57 +00002443 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00002444 break;
2445 }
drh3d1d2a92015-09-22 01:15:49 +00002446 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00002447 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00002448 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
2449 break;
2450 }
2451 }
2452 return SQLITE_OK;
2453}
2454
2455/* Return the current rowid value */
2456static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2457 JsonEachCursor *p = (JsonEachCursor*)cur;
2458 *pRowid = p->iRowid;
2459 return SQLITE_OK;
2460}
2461
2462/* The query strategy is to look for an equality constraint on the json
2463** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00002464** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00002465** and 0 otherwise.
2466*/
2467static int jsonEachBestIndex(
2468 sqlite3_vtab *tab,
2469 sqlite3_index_info *pIdxInfo
2470){
drh43579192018-11-16 16:04:50 +00002471 int i; /* Loop counter or computed array index */
2472 int aIdx[2]; /* Index of constraints for JSON and ROOT */
2473 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
2474 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
drhcb6c6c62015-08-19 22:47:17 +00002475 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00002476
drh43579192018-11-16 16:04:50 +00002477 /* This implementation assumes that JSON and ROOT are the last two
2478 ** columns in the table */
2479 assert( JEACH_ROOT == JEACH_JSON+1 );
drh6fd5c1e2015-08-21 20:37:12 +00002480 UNUSED_PARAM(tab);
drh43579192018-11-16 16:04:50 +00002481 aIdx[0] = aIdx[1] = -1;
drhcb6c6c62015-08-19 22:47:17 +00002482 pConstraint = pIdxInfo->aConstraint;
2483 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
drh43579192018-11-16 16:04:50 +00002484 int iCol;
2485 int iMask;
2486 if( pConstraint->iColumn < JEACH_JSON ) continue;
2487 iCol = pConstraint->iColumn - JEACH_JSON;
2488 assert( iCol==0 || iCol==1 );
drh285f2ef2021-10-15 16:15:04 +00002489 testcase( iCol==0 );
drh43579192018-11-16 16:04:50 +00002490 iMask = 1 << iCol;
2491 if( pConstraint->usable==0 ){
2492 unusableMask |= iMask;
2493 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
2494 aIdx[iCol] = i;
2495 idxMask |= iMask;
drhcb6c6c62015-08-19 22:47:17 +00002496 }
2497 }
drh43579192018-11-16 16:04:50 +00002498 if( (unusableMask & ~idxMask)!=0 ){
2499 /* If there are any unusable constraints on JSON or ROOT, then reject
2500 ** this entire plan */
2501 return SQLITE_CONSTRAINT;
2502 }
2503 if( aIdx[0]<0 ){
2504 /* No JSON input. Leave estimatedCost at the huge value that it was
2505 ** initialized to to discourage the query planner from selecting this
2506 ** plan. */
drhcb6c6c62015-08-19 22:47:17 +00002507 pIdxInfo->idxNum = 0;
drhcb6c6c62015-08-19 22:47:17 +00002508 }else{
drh505ad2c2015-08-21 17:33:11 +00002509 pIdxInfo->estimatedCost = 1.0;
drh43579192018-11-16 16:04:50 +00002510 i = aIdx[0];
2511 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
2512 pIdxInfo->aConstraintUsage[i].omit = 1;
2513 if( aIdx[1]<0 ){
2514 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
drhcb6c6c62015-08-19 22:47:17 +00002515 }else{
drh43579192018-11-16 16:04:50 +00002516 i = aIdx[1];
2517 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
2518 pIdxInfo->aConstraintUsage[i].omit = 1;
2519 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
drhcb6c6c62015-08-19 22:47:17 +00002520 }
2521 }
2522 return SQLITE_OK;
2523}
2524
2525/* Start a search on a new JSON string */
2526static int jsonEachFilter(
2527 sqlite3_vtab_cursor *cur,
2528 int idxNum, const char *idxStr,
2529 int argc, sqlite3_value **argv
2530){
2531 JsonEachCursor *p = (JsonEachCursor*)cur;
2532 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00002533 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002534 sqlite3_int64 n;
2535
drh6fd5c1e2015-08-21 20:37:12 +00002536 UNUSED_PARAM(idxStr);
2537 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002538 jsonEachCursorReset(p);
2539 if( idxNum==0 ) return SQLITE_OK;
2540 z = (const char*)sqlite3_value_text(argv[0]);
2541 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002542 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002543 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002544 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002545 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002546 if( jsonParse(&p->sParse, 0, p->zJson) ){
2547 int rc = SQLITE_NOMEM;
2548 if( p->sParse.oom==0 ){
2549 sqlite3_free(cur->pVtab->zErrMsg);
2550 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2551 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2552 }
drhcb6c6c62015-08-19 22:47:17 +00002553 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002554 return rc;
2555 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2556 jsonEachCursorReset(p);
2557 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002558 }else{
drh95677942015-09-24 01:06:37 +00002559 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002560 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002561 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002562 zRoot = (const char*)sqlite3_value_text(argv[1]);
2563 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002564 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002565 p->zRoot = sqlite3_malloc64( n+1 );
2566 if( p->zRoot==0 ) return SQLITE_NOMEM;
2567 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002568 if( zRoot[0]!='$' ){
2569 zErr = zRoot;
2570 }else{
2571 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2572 }
2573 if( zErr ){
drha7714022015-08-29 00:54:49 +00002574 sqlite3_free(cur->pVtab->zErrMsg);
2575 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002576 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002577 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2578 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002579 return SQLITE_OK;
2580 }
2581 }else{
2582 pNode = p->sParse.aNode;
2583 }
drh852944e2015-09-10 03:29:11 +00002584 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002585 p->eType = pNode->eType;
2586 if( p->eType>=JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002587 assert( pNode->eU==0 );
2588 VVA( pNode->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002589 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002590 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002591 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002592 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002593 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2594 p->i--;
2595 }
2596 }else{
2597 p->i++;
2598 }
drhcb6c6c62015-08-19 22:47:17 +00002599 }else{
2600 p->iEnd = p->i+1;
2601 }
2602 }
drha8f39a92015-09-21 22:53:16 +00002603 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002604}
2605
2606/* The methods of the json_each virtual table */
2607static sqlite3_module jsonEachModule = {
2608 0, /* iVersion */
2609 0, /* xCreate */
2610 jsonEachConnect, /* xConnect */
2611 jsonEachBestIndex, /* xBestIndex */
2612 jsonEachDisconnect, /* xDisconnect */
2613 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002614 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002615 jsonEachClose, /* xClose - close a cursor */
2616 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002617 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002618 jsonEachEof, /* xEof - check for end of scan */
2619 jsonEachColumn, /* xColumn - read data */
2620 jsonEachRowid, /* xRowid - read data */
2621 0, /* xUpdate */
2622 0, /* xBegin */
2623 0, /* xSync */
2624 0, /* xCommit */
2625 0, /* xRollback */
2626 0, /* xFindMethod */
2627 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002628 0, /* xSavepoint */
2629 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002630 0, /* xRollbackTo */
2631 0 /* xShadowName */
drhcb6c6c62015-08-19 22:47:17 +00002632};
2633
drh505ad2c2015-08-21 17:33:11 +00002634/* The methods of the json_tree virtual table. */
2635static sqlite3_module jsonTreeModule = {
2636 0, /* iVersion */
2637 0, /* xCreate */
2638 jsonEachConnect, /* xConnect */
2639 jsonEachBestIndex, /* xBestIndex */
2640 jsonEachDisconnect, /* xDisconnect */
2641 0, /* xDestroy */
2642 jsonEachOpenTree, /* xOpen - open a cursor */
2643 jsonEachClose, /* xClose - close a cursor */
2644 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002645 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002646 jsonEachEof, /* xEof - check for end of scan */
2647 jsonEachColumn, /* xColumn - read data */
2648 jsonEachRowid, /* xRowid - read data */
2649 0, /* xUpdate */
2650 0, /* xBegin */
2651 0, /* xSync */
2652 0, /* xCommit */
2653 0, /* xRollback */
2654 0, /* xFindMethod */
2655 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002656 0, /* xSavepoint */
2657 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002658 0, /* xRollbackTo */
2659 0 /* xShadowName */
drh505ad2c2015-08-21 17:33:11 +00002660};
drhd2975922015-08-29 17:22:33 +00002661#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002662
2663/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002664** The following routines are the only publically visible identifiers in this
2665** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002666** functions and the virtual table implemented by this file.
2667****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002668
drh2f20e132015-09-26 17:44:59 +00002669int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002670 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002671 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002672 static const struct {
2673 const char *zName;
2674 int nArg;
drh52216ad2015-08-18 02:28:03 +00002675 int flag;
drh5fa5c102015-08-12 16:49:40 +00002676 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2677 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002678 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002679 { "json_array", -1, 0, jsonArrayFunc },
2680 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2681 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002682 { "json_extract", -1, 0, jsonExtractFunc },
drhd5326c32022-01-07 14:58:47 +00002683 { "->", 2, 1, jsonExtractFunc },
2684 { "->>", 2, 1, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002685 { "json_insert", -1, 0, jsonSetFunc },
drha4e4e182022-01-07 16:03:00 +00002686 { "json_ntype", 1, 1, jsonTypeFunc },
drh52216ad2015-08-18 02:28:03 +00002687 { "json_object", -1, 0, jsonObjectFunc },
drh37f03df2017-03-23 20:33:49 +00002688 { "json_patch", 2, 0, jsonPatchFunc },
drh2ad96f52016-06-17 13:01:51 +00002689 { "json_quote", 1, 0, jsonQuoteFunc },
drh52216ad2015-08-18 02:28:03 +00002690 { "json_remove", -1, 0, jsonRemoveFunc },
2691 { "json_replace", -1, 0, jsonReplaceFunc },
2692 { "json_set", -1, 1, jsonSetFunc },
2693 { "json_type", 1, 0, jsonTypeFunc },
2694 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002695 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002696
drh301eecc2015-08-17 20:14:19 +00002697#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002698 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002699 { "json_parse", 1, 0, jsonParseFunc },
2700 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002701#endif
drh5fa5c102015-08-12 16:49:40 +00002702 };
drhff135ae2015-12-30 01:07:02 +00002703 static const struct {
2704 const char *zName;
2705 int nArg;
2706 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2707 void (*xFinal)(sqlite3_context*);
drh8be47a72018-07-05 20:05:29 +00002708 void (*xValue)(sqlite3_context*);
drhff135ae2015-12-30 01:07:02 +00002709 } aAgg[] = {
drh8be47a72018-07-05 20:05:29 +00002710 { "json_group_array", 1,
2711 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
2712 { "json_group_object", 2,
2713 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
drhff135ae2015-12-30 01:07:02 +00002714 };
drhd2975922015-08-29 17:22:33 +00002715#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002716 static const struct {
2717 const char *zName;
2718 sqlite3_module *pModule;
2719 } aMod[] = {
2720 { "json_each", &jsonEachModule },
2721 { "json_tree", &jsonTreeModule },
2722 };
drhd2975922015-08-29 17:22:33 +00002723#endif
drh79d5bc82020-01-04 01:43:02 +00002724 static const int enc =
2725 SQLITE_UTF8 |
2726 SQLITE_DETERMINISTIC |
2727 SQLITE_INNOCUOUS;
drh5fa5c102015-08-12 16:49:40 +00002728 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
drh79d5bc82020-01-04 01:43:02 +00002729 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
dan01a3b6b2019-09-13 17:05:48 +00002730 (void*)&aFunc[i].flag,
2731 aFunc[i].xFunc, 0, 0);
drh5fa5c102015-08-12 16:49:40 +00002732 }
mistachkin5a193dd2018-07-24 13:57:44 +00002733#ifndef SQLITE_OMIT_WINDOWFUNC
drhff135ae2015-12-30 01:07:02 +00002734 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
drh8be47a72018-07-05 20:05:29 +00002735 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
drh79d5bc82020-01-04 01:43:02 +00002736 SQLITE_SUBTYPE | enc, 0,
drh8be47a72018-07-05 20:05:29 +00002737 aAgg[i].xStep, aAgg[i].xFinal,
2738 aAgg[i].xValue, jsonGroupInverse, 0);
drhff135ae2015-12-30 01:07:02 +00002739 }
mistachkin5a193dd2018-07-24 13:57:44 +00002740#endif
drhd2975922015-08-29 17:22:33 +00002741#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002742 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2743 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002744 }
drhd2975922015-08-29 17:22:33 +00002745#endif
drh5fa5c102015-08-12 16:49:40 +00002746 return rc;
2747}
drh2f20e132015-09-26 17:44:59 +00002748
2749
dan8d32e802015-10-14 18:45:42 +00002750#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002751#ifdef _WIN32
2752__declspec(dllexport)
2753#endif
2754int sqlite3_json_init(
2755 sqlite3 *db,
2756 char **pzErrMsg,
2757 const sqlite3_api_routines *pApi
2758){
2759 SQLITE_EXTENSION_INIT2(pApi);
2760 (void)pzErrMsg; /* Unused parameter */
2761 return sqlite3Json1Init(db);
2762}
dan8d32e802015-10-14 18:45:42 +00002763#endif
drh50065652015-10-08 19:29:18 +00002764#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */