blob: 86a486c21c49894a184cd10688c2aa41b569c2b2 [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;
drh338b1fd2022-01-07 17:08:48 +00001600 int flags = *(int*)sqlite3_user_data(ctx);
drh3ad93bb2015-08-29 19:41:45 +00001601 JsonString jx;
drh3ad93bb2015-08-29 19:41:45 +00001602
1603 if( argc<2 ) return;
drh338b1fd2022-01-07 17:08:48 +00001604 p = jsonParseCached(ctx, argv, (flags & 1)!=0 ? 0 : ctx);
1605 if( p==0 ){
1606 /* If the form is "json_nextract(IN,'$')" and IN is not well-formed JSON,
1607 ** then return IN as a quoted JSON string. */
1608 if( (flags & 1)!=0
1609 && argc==2
1610 && (zPath = (const char*)sqlite3_value_text(argv[1]))!=0
1611 && zPath[0]=='$' && zPath[1]==0
1612 ){
1613 jsonQuoteFunc(ctx, argc, argv);
1614 }
1615 return;
1616 }
drh12b9fa92022-01-07 15:47:12 +00001617 if( argc==2 ){
1618 /* With a single PATH argument, the return is the unquoted SQL value */
1619 zPath = (const char*)sqlite3_value_text(argv[1]);
drh338b1fd2022-01-07 17:08:48 +00001620 if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & 2)!=0 ){
drh12b9fa92022-01-07 15:47:12 +00001621 /* The -> and ->> operators accept abbreviated PATH arguments:
1622 ** NUMBER ==> $[NUMBER]
1623 ** LABEL ==> $.LABEL
1624 ** [NUMBER] ==> $[NUMBER]
1625 */
1626 jsonInit(&jx, ctx);
1627 if( safe_isdigit(zPath[0]) ){
1628 jsonAppendRaw(&jx, "$[", 2);
1629 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
1630 jsonAppendRaw(&jx, "]", 2);
1631 }else{
1632 jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
1633 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
1634 jsonAppendChar(&jx, 0);
1635 }
1636 pNode = jsonLookup(p, jx.zBuf, 0, ctx);
1637 jsonReset(&jx);
1638 }else{
1639 pNode = jsonLookup(p, zPath, 0, ctx);
1640 }
1641 if( p->nErr ) return;
1642 if( pNode ) jsonReturn(pNode, ctx, 0);
1643 }else{
1644 /* Two or more PATH arguments results in a JSON array with each
1645 ** element of the array being the value selected by one of the PATHs */
1646 int i;
1647 jsonInit(&jx, ctx);
1648 jsonAppendChar(&jx, '[');
1649 for(i=1; i<argc; i++){
1650 zPath = (const char*)sqlite3_value_text(argv[i]);
1651 pNode = jsonLookup(p, zPath, 0, ctx);
1652 if( p->nErr ) break;
drh3ad93bb2015-08-29 19:41:45 +00001653 jsonAppendSeparator(&jx);
1654 if( pNode ){
1655 jsonRenderNode(pNode, &jx, 0);
1656 }else{
1657 jsonAppendRaw(&jx, "null", 4);
1658 }
drh3ad93bb2015-08-29 19:41:45 +00001659 }
drh12b9fa92022-01-07 15:47:12 +00001660 if( i==argc ){
1661 jsonAppendChar(&jx, ']');
1662 jsonResult(&jx);
1663 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1664 }
1665 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001666 }
drh987eb1f2015-08-17 15:17:37 +00001667}
1668
drh633647a2017-03-22 21:24:31 +00001669/* This is the RFC 7396 MergePatch algorithm.
1670*/
1671static JsonNode *jsonMergePatch(
1672 JsonParse *pParse, /* The JSON parser that contains the TARGET */
mistachkinb1ed7172017-04-14 14:50:34 +00001673 u32 iTarget, /* Node of the TARGET in pParse */
drh633647a2017-03-22 21:24:31 +00001674 JsonNode *pPatch /* The PATCH */
1675){
drh0002d242017-03-23 00:46:15 +00001676 u32 i, j;
1677 u32 iRoot;
drh633647a2017-03-22 21:24:31 +00001678 JsonNode *pTarget;
1679 if( pPatch->eType!=JSON_OBJECT ){
1680 return pPatch;
1681 }
1682 assert( iTarget>=0 && iTarget<pParse->nNode );
1683 pTarget = &pParse->aNode[iTarget];
1684 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1685 if( pTarget->eType!=JSON_OBJECT ){
drh29c99692017-03-24 12:35:17 +00001686 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001687 return pPatch;
1688 }
drhbb7aa2d2017-03-23 00:13:52 +00001689 iRoot = iTarget;
drh633647a2017-03-22 21:24:31 +00001690 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
drhba7cce32017-03-24 19:45:05 +00001691 u32 nKey;
drh633647a2017-03-22 21:24:31 +00001692 const char *zKey;
1693 assert( pPatch[i].eType==JSON_STRING );
1694 assert( pPatch[i].jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00001695 assert( pPatch[i].eU==1 );
drh633647a2017-03-22 21:24:31 +00001696 nKey = pPatch[i].n;
1697 zKey = pPatch[i].u.zJContent;
drhbb7aa2d2017-03-23 00:13:52 +00001698 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh633647a2017-03-22 21:24:31 +00001699 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1700 assert( pTarget[j].eType==JSON_STRING );
1701 assert( pTarget[j].jnFlags & JNODE_LABEL );
drhbb7aa2d2017-03-23 00:13:52 +00001702 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh1fe162f2017-03-23 12:56:44 +00001703 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1704 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
drh633647a2017-03-22 21:24:31 +00001705 if( pPatch[i+1].eType==JSON_NULL ){
1706 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1707 }else{
drhbb7aa2d2017-03-23 00:13:52 +00001708 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
drh633647a2017-03-22 21:24:31 +00001709 if( pNew==0 ) return 0;
1710 pTarget = &pParse->aNode[iTarget];
1711 if( pNew!=&pTarget[j+1] ){
drha2852ac2021-11-15 01:45:11 +00001712 assert( pTarget[j+1].eU==0
1713 || pTarget[j+1].eU==1
1714 || pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001715 testcase( pTarget[j+1].eU==1 );
drha2852ac2021-11-15 01:45:11 +00001716 testcase( pTarget[j+1].eU==2 );
drh285f2ef2021-10-15 16:15:04 +00001717 VVA( pTarget[j+1].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001718 pTarget[j+1].u.pPatch = pNew;
1719 pTarget[j+1].jnFlags |= JNODE_PATCH;
1720 }
1721 }
1722 break;
1723 }
1724 }
drhbb7aa2d2017-03-23 00:13:52 +00001725 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
drh633647a2017-03-22 21:24:31 +00001726 int iStart, iPatch;
1727 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1728 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1729 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1730 if( pParse->oom ) return 0;
drh29c99692017-03-24 12:35:17 +00001731 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001732 pTarget = &pParse->aNode[iTarget];
drh8b554e22021-10-20 20:22:37 +00001733 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
1734 testcase( pParse->aNode[iRoot].eU==2 );
drhbb7aa2d2017-03-23 00:13:52 +00001735 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
drh285f2ef2021-10-15 16:15:04 +00001736 VVA( pParse->aNode[iRoot].eU = 2 );
drhbb7aa2d2017-03-23 00:13:52 +00001737 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
1738 iRoot = iStart;
drh285f2ef2021-10-15 16:15:04 +00001739 assert( pParse->aNode[iPatch].eU==0 );
1740 VVA( pParse->aNode[iPatch].eU = 5 );
drh633647a2017-03-22 21:24:31 +00001741 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
1742 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
1743 }
1744 }
1745 return pTarget;
1746}
1747
1748/*
1749** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1750** object that is the result of running the RFC 7396 MergePatch() algorithm
1751** on the two arguments.
1752*/
drh37f03df2017-03-23 20:33:49 +00001753static void jsonPatchFunc(
drh633647a2017-03-22 21:24:31 +00001754 sqlite3_context *ctx,
1755 int argc,
1756 sqlite3_value **argv
1757){
1758 JsonParse x; /* The JSON that is being patched */
1759 JsonParse y; /* The patch */
drhbb7aa2d2017-03-23 00:13:52 +00001760 JsonNode *pResult; /* The result of the merge */
drh633647a2017-03-22 21:24:31 +00001761
drh2fb79e92017-03-25 12:08:11 +00001762 UNUSED_PARAM(argc);
drh633647a2017-03-22 21:24:31 +00001763 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1764 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
1765 jsonParseReset(&x);
1766 return;
1767 }
drhbb7aa2d2017-03-23 00:13:52 +00001768 pResult = jsonMergePatch(&x, 0, y.aNode);
1769 assert( pResult!=0 || x.oom );
1770 if( pResult ){
1771 jsonReturnJson(pResult, ctx, 0);
1772 }else{
1773 sqlite3_result_error_nomem(ctx);
1774 }
drh633647a2017-03-22 21:24:31 +00001775 jsonParseReset(&x);
1776 jsonParseReset(&y);
1777}
1778
1779
drh987eb1f2015-08-17 15:17:37 +00001780/*
1781** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1782** object that contains all name/value given in arguments. Or if any name
1783** is not a string or if any value is a BLOB, throw an error.
1784*/
1785static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001786 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001787 int argc,
1788 sqlite3_value **argv
1789){
1790 int i;
drh505ad2c2015-08-21 17:33:11 +00001791 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001792 const char *z;
1793 u32 n;
1794
1795 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001796 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001797 "of arguments", -1);
1798 return;
1799 }
drhbc8f0922015-08-22 19:39:04 +00001800 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001801 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001802 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001803 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001804 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001805 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001806 return;
1807 }
drhd0960592015-08-17 21:22:32 +00001808 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001809 z = (const char*)sqlite3_value_text(argv[i]);
1810 n = (u32)sqlite3_value_bytes(argv[i]);
1811 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001812 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001813 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001814 }
drhd0960592015-08-17 21:22:32 +00001815 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001816 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001817 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001818}
1819
1820
1821/*
drh301eecc2015-08-17 20:14:19 +00001822** json_remove(JSON, PATH, ...)
1823**
drh3ad93bb2015-08-29 19:41:45 +00001824** Remove the named elements from JSON and return the result. malformed
1825** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001826*/
1827static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001828 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001829 int argc,
1830 sqlite3_value **argv
1831){
1832 JsonParse x; /* The parse */
1833 JsonNode *pNode;
1834 const char *zPath;
1835 u32 i;
1836
1837 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001838 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001839 assert( x.nNode );
1840 for(i=1; i<(u32)argc; i++){
1841 zPath = (const char*)sqlite3_value_text(argv[i]);
1842 if( zPath==0 ) goto remove_done;
1843 pNode = jsonLookup(&x, zPath, 0, ctx);
1844 if( x.nErr ) goto remove_done;
1845 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1846 }
1847 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1848 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001849 }
drha7714022015-08-29 00:54:49 +00001850remove_done:
drh505ad2c2015-08-21 17:33:11 +00001851 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001852}
1853
1854/*
1855** json_replace(JSON, PATH, VALUE, ...)
1856**
1857** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001858** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001859*/
1860static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001861 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001862 int argc,
1863 sqlite3_value **argv
1864){
1865 JsonParse x; /* The parse */
1866 JsonNode *pNode;
1867 const char *zPath;
1868 u32 i;
1869
1870 if( argc<1 ) return;
1871 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001872 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001873 return;
1874 }
drhbc8f0922015-08-22 19:39:04 +00001875 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001876 assert( x.nNode );
1877 for(i=1; i<(u32)argc; i+=2){
1878 zPath = (const char*)sqlite3_value_text(argv[i]);
1879 pNode = jsonLookup(&x, zPath, 0, ctx);
1880 if( x.nErr ) goto replace_err;
1881 if( pNode ){
drh285f2ef2021-10-15 16:15:04 +00001882 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
1883 json_testcase( pNode->eU!=0 && pNode->eU!=1 );
drha8f39a92015-09-21 22:53:16 +00001884 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh285f2ef2021-10-15 16:15:04 +00001885 VVA( pNode->eU = 4 );
drh633647a2017-03-22 21:24:31 +00001886 pNode->u.iReplace = i + 1;
drhd0960592015-08-17 21:22:32 +00001887 }
drha8f39a92015-09-21 22:53:16 +00001888 }
1889 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001890 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001891 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001892 }else{
1893 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001894 }
drha7714022015-08-29 00:54:49 +00001895replace_err:
drh505ad2c2015-08-21 17:33:11 +00001896 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001897}
drh505ad2c2015-08-21 17:33:11 +00001898
drh52216ad2015-08-18 02:28:03 +00001899/*
1900** json_set(JSON, PATH, VALUE, ...)
1901**
1902** Set the value at PATH to VALUE. Create the PATH if it does not already
1903** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001904** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001905**
1906** json_insert(JSON, PATH, VALUE, ...)
1907**
1908** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001909** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001910*/
1911static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001912 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001913 int argc,
1914 sqlite3_value **argv
1915){
1916 JsonParse x; /* The parse */
1917 JsonNode *pNode;
1918 const char *zPath;
1919 u32 i;
1920 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001921 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001922
1923 if( argc<1 ) return;
1924 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001925 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001926 return;
1927 }
drhbc8f0922015-08-22 19:39:04 +00001928 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001929 assert( x.nNode );
1930 for(i=1; i<(u32)argc; i+=2){
1931 zPath = (const char*)sqlite3_value_text(argv[i]);
1932 bApnd = 0;
1933 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1934 if( x.oom ){
1935 sqlite3_result_error_nomem(ctx);
1936 goto jsonSetDone;
1937 }else if( x.nErr ){
1938 goto jsonSetDone;
1939 }else if( pNode && (bApnd || bIsSet) ){
drh285f2ef2021-10-15 16:15:04 +00001940 json_testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 );
1941 assert( pNode->eU!=3 || pNode->eU!=5 );
1942 VVA( pNode->eU = 4 );
drha8f39a92015-09-21 22:53:16 +00001943 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001944 pNode->u.iReplace = i + 1;
drh52216ad2015-08-18 02:28:03 +00001945 }
drha8f39a92015-09-21 22:53:16 +00001946 }
1947 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh285f2ef2021-10-15 16:15:04 +00001948 assert( x.aNode[0].eU==4 );
drh633647a2017-03-22 21:24:31 +00001949 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001950 }else{
1951 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001952 }
drhbc8f0922015-08-22 19:39:04 +00001953jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001954 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001955}
drh301eecc2015-08-17 20:14:19 +00001956
1957/*
drh987eb1f2015-08-17 15:17:37 +00001958** json_type(JSON)
drha4e4e182022-01-07 16:03:00 +00001959** json_ntype(JSON)
drh987eb1f2015-08-17 15:17:37 +00001960** json_type(JSON, PATH)
1961**
drha4e4e182022-01-07 16:03:00 +00001962** Return the top-level "type" of a JSON string. json_type() raises an
1963** error if either the JSON or PATH inputs are not well-formed. json_ntype()
1964** works like the one-argument version of json_type() except that it
1965** returns NULL if the JSON argument is not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001966*/
1967static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001968 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001969 int argc,
1970 sqlite3_value **argv
1971){
drhe35fc302018-08-30 01:52:10 +00001972 JsonParse *p; /* The parse */
drh987eb1f2015-08-17 15:17:37 +00001973 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001974 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001975
drha4e4e182022-01-07 16:03:00 +00001976 p = jsonParseCached(ctx, argv, *(int*)sqlite3_user_data(ctx) ? 0 : ctx);
drhe35fc302018-08-30 01:52:10 +00001977 if( p==0 ) return;
drha8f39a92015-09-21 22:53:16 +00001978 if( argc==2 ){
1979 zPath = (const char*)sqlite3_value_text(argv[1]);
drhe35fc302018-08-30 01:52:10 +00001980 pNode = jsonLookup(p, zPath, 0, ctx);
drha8f39a92015-09-21 22:53:16 +00001981 }else{
drhe35fc302018-08-30 01:52:10 +00001982 pNode = p->aNode;
drha8f39a92015-09-21 22:53:16 +00001983 }
1984 if( pNode ){
1985 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001986 }
drh987eb1f2015-08-17 15:17:37 +00001987}
drh5634cc02015-08-17 11:28:03 +00001988
drhbc8f0922015-08-22 19:39:04 +00001989/*
1990** json_valid(JSON)
1991**
drh3ad93bb2015-08-29 19:41:45 +00001992** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1993** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001994*/
1995static void jsonValidFunc(
1996 sqlite3_context *ctx,
1997 int argc,
1998 sqlite3_value **argv
1999){
drhe35fc302018-08-30 01:52:10 +00002000 JsonParse *p; /* The parse */
mistachkin16a93122015-09-11 18:05:01 +00002001 UNUSED_PARAM(argc);
drhe35fc302018-08-30 01:52:10 +00002002 p = jsonParseCached(ctx, argv, 0);
2003 sqlite3_result_int(ctx, p!=0);
drhbc8f0922015-08-22 19:39:04 +00002004}
2005
drhff135ae2015-12-30 01:07:02 +00002006
2007/****************************************************************************
2008** Aggregate SQL function implementations
2009****************************************************************************/
2010/*
2011** json_group_array(VALUE)
2012**
2013** Return a JSON array composed of all values in the aggregate.
2014*/
2015static void jsonArrayStep(
2016 sqlite3_context *ctx,
2017 int argc,
2018 sqlite3_value **argv
2019){
2020 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00002021 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002022 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2023 if( pStr ){
2024 if( pStr->zBuf==0 ){
2025 jsonInit(pStr, ctx);
2026 jsonAppendChar(pStr, '[');
drhfab5b072019-09-14 00:21:34 +00002027 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002028 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002029 }
dan8505d732021-04-14 12:11:39 +00002030 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002031 jsonAppendValue(pStr, argv[0]);
2032 }
2033}
drh8be47a72018-07-05 20:05:29 +00002034static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002035 JsonString *pStr;
2036 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2037 if( pStr ){
2038 pStr->pCtx = ctx;
2039 jsonAppendChar(pStr, ']');
2040 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00002041 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002042 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002043 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002044 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002045 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2046 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002047 }else{
mistachkined008ec2018-09-12 01:05:26 +00002048 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002049 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002050 }
2051 }else{
2052 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
2053 }
2054 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2055}
drh8be47a72018-07-05 20:05:29 +00002056static void jsonArrayValue(sqlite3_context *ctx){
2057 jsonArrayCompute(ctx, 0);
2058}
2059static void jsonArrayFinal(sqlite3_context *ctx){
2060 jsonArrayCompute(ctx, 1);
2061}
2062
2063#ifndef SQLITE_OMIT_WINDOWFUNC
2064/*
2065** This method works for both json_group_array() and json_group_object().
2066** It works by removing the first element of the group by searching forward
2067** to the first comma (",") that is not within a string and deleting all
2068** text through that comma.
2069*/
2070static void jsonGroupInverse(
2071 sqlite3_context *ctx,
2072 int argc,
2073 sqlite3_value **argv
2074){
drhe39f3882019-09-21 17:31:03 +00002075 unsigned int i;
drh8be47a72018-07-05 20:05:29 +00002076 int inStr = 0;
drhfab5b072019-09-14 00:21:34 +00002077 int nNest = 0;
drh8be47a72018-07-05 20:05:29 +00002078 char *z;
drhfab5b072019-09-14 00:21:34 +00002079 char c;
drh8be47a72018-07-05 20:05:29 +00002080 JsonString *pStr;
drhc7bf5712018-07-09 22:49:01 +00002081 UNUSED_PARAM(argc);
2082 UNUSED_PARAM(argv);
drh8be47a72018-07-05 20:05:29 +00002083 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
drh491d4c82018-07-07 20:23:46 +00002084#ifdef NEVER
drhfd4b7282018-07-07 19:47:21 +00002085 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2086 ** always have been called to initalize it */
2087 if( NEVER(!pStr) ) return;
drh491d4c82018-07-07 20:23:46 +00002088#endif
drh8be47a72018-07-05 20:05:29 +00002089 z = pStr->zBuf;
drh0e5cd342021-04-13 01:12:32 +00002090 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
drhfab5b072019-09-14 00:21:34 +00002091 if( c=='"' ){
drh8be47a72018-07-05 20:05:29 +00002092 inStr = !inStr;
drhfab5b072019-09-14 00:21:34 +00002093 }else if( c=='\\' ){
drh8be47a72018-07-05 20:05:29 +00002094 i++;
drhfab5b072019-09-14 00:21:34 +00002095 }else if( !inStr ){
2096 if( c=='{' || c=='[' ) nNest++;
2097 if( c=='}' || c==']' ) nNest--;
drh8be47a72018-07-05 20:05:29 +00002098 }
2099 }
drh0e5cd342021-04-13 01:12:32 +00002100 if( i<pStr->nUsed ){
2101 pStr->nUsed -= i;
2102 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
2103 z[pStr->nUsed] = 0;
2104 }else{
2105 pStr->nUsed = 1;
2106 }
drh8be47a72018-07-05 20:05:29 +00002107}
2108#else
2109# define jsonGroupInverse 0
2110#endif
2111
drhff135ae2015-12-30 01:07:02 +00002112
2113/*
2114** json_group_obj(NAME,VALUE)
2115**
2116** Return a JSON object composed of all names and values in the aggregate.
2117*/
2118static void jsonObjectStep(
2119 sqlite3_context *ctx,
2120 int argc,
2121 sqlite3_value **argv
2122){
2123 JsonString *pStr;
2124 const char *z;
2125 u32 n;
drhdf3a9072016-02-11 15:37:18 +00002126 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00002127 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2128 if( pStr ){
2129 if( pStr->zBuf==0 ){
2130 jsonInit(pStr, ctx);
2131 jsonAppendChar(pStr, '{');
drhfab5b072019-09-14 00:21:34 +00002132 }else if( pStr->nUsed>1 ){
drhff135ae2015-12-30 01:07:02 +00002133 jsonAppendChar(pStr, ',');
drhff135ae2015-12-30 01:07:02 +00002134 }
drhd2f55772021-05-28 12:15:19 +00002135 pStr->pCtx = ctx;
drhff135ae2015-12-30 01:07:02 +00002136 z = (const char*)sqlite3_value_text(argv[0]);
2137 n = (u32)sqlite3_value_bytes(argv[0]);
2138 jsonAppendString(pStr, z, n);
2139 jsonAppendChar(pStr, ':');
2140 jsonAppendValue(pStr, argv[1]);
2141 }
2142}
drh8be47a72018-07-05 20:05:29 +00002143static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
drhff135ae2015-12-30 01:07:02 +00002144 JsonString *pStr;
2145 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2146 if( pStr ){
2147 jsonAppendChar(pStr, '}');
2148 if( pStr->bErr ){
drh6850a632016-11-07 18:18:08 +00002149 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00002150 assert( pStr->bStatic );
drh8be47a72018-07-05 20:05:29 +00002151 }else if( isFinal ){
mistachkined008ec2018-09-12 01:05:26 +00002152 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
drhff135ae2015-12-30 01:07:02 +00002153 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2154 pStr->bStatic = 1;
drh8be47a72018-07-05 20:05:29 +00002155 }else{
mistachkined008ec2018-09-12 01:05:26 +00002156 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
drh8be47a72018-07-05 20:05:29 +00002157 pStr->nUsed--;
drhff135ae2015-12-30 01:07:02 +00002158 }
2159 }else{
2160 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
2161 }
2162 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2163}
drh8be47a72018-07-05 20:05:29 +00002164static void jsonObjectValue(sqlite3_context *ctx){
2165 jsonObjectCompute(ctx, 0);
2166}
2167static void jsonObjectFinal(sqlite3_context *ctx){
2168 jsonObjectCompute(ctx, 1);
2169}
2170
drhff135ae2015-12-30 01:07:02 +00002171
2172
drhd2975922015-08-29 17:22:33 +00002173#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00002174/****************************************************************************
2175** The json_each virtual table
2176****************************************************************************/
2177typedef struct JsonEachCursor JsonEachCursor;
2178struct JsonEachCursor {
2179 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00002180 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00002181 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00002182 u32 i; /* Index in sParse.aNode[] of current row */
2183 u32 iEnd; /* EOF when i equals or exceeds this value */
2184 u8 eType; /* Type of top-level element */
2185 u8 bRecursive; /* True for json_tree(). False for json_each() */
2186 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00002187 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00002188 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00002189};
2190
2191/* Constructor for the json_each virtual table */
2192static int jsonEachConnect(
2193 sqlite3 *db,
2194 void *pAux,
2195 int argc, const char *const*argv,
2196 sqlite3_vtab **ppVtab,
2197 char **pzErr
2198){
2199 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00002200 int rc;
drhcb6c6c62015-08-19 22:47:17 +00002201
2202/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00002203#define JEACH_KEY 0
2204#define JEACH_VALUE 1
2205#define JEACH_TYPE 2
2206#define JEACH_ATOM 3
2207#define JEACH_ID 4
2208#define JEACH_PARENT 5
2209#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00002210#define JEACH_PATH 7
drh43579192018-11-16 16:04:50 +00002211/* The xBestIndex method assumes that the JSON and ROOT columns are
2212** the last two columns in the table. Should this ever changes, be
2213** sure to update the xBestIndex method. */
drh383de692015-09-10 17:20:57 +00002214#define JEACH_JSON 8
2215#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00002216
drh6fd5c1e2015-08-21 20:37:12 +00002217 UNUSED_PARAM(pzErr);
2218 UNUSED_PARAM(argv);
2219 UNUSED_PARAM(argc);
2220 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00002221 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00002222 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2223 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00002224 if( rc==SQLITE_OK ){
2225 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
2226 if( pNew==0 ) return SQLITE_NOMEM;
2227 memset(pNew, 0, sizeof(*pNew));
drh2b1c2aa2020-01-07 19:45:40 +00002228 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
drh505ad2c2015-08-21 17:33:11 +00002229 }
2230 return rc;
drhcb6c6c62015-08-19 22:47:17 +00002231}
2232
2233/* destructor for json_each virtual table */
2234static int jsonEachDisconnect(sqlite3_vtab *pVtab){
2235 sqlite3_free(pVtab);
2236 return SQLITE_OK;
2237}
2238
drh505ad2c2015-08-21 17:33:11 +00002239/* constructor for a JsonEachCursor object for json_each(). */
2240static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00002241 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00002242
2243 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00002244 pCur = sqlite3_malloc( sizeof(*pCur) );
2245 if( pCur==0 ) return SQLITE_NOMEM;
2246 memset(pCur, 0, sizeof(*pCur));
2247 *ppCursor = &pCur->base;
2248 return SQLITE_OK;
2249}
2250
drh505ad2c2015-08-21 17:33:11 +00002251/* constructor for a JsonEachCursor object for json_tree(). */
2252static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2253 int rc = jsonEachOpenEach(p, ppCursor);
2254 if( rc==SQLITE_OK ){
2255 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
2256 pCur->bRecursive = 1;
2257 }
2258 return rc;
2259}
2260
drhcb6c6c62015-08-19 22:47:17 +00002261/* Reset a JsonEachCursor back to its original state. Free any memory
2262** held. */
2263static void jsonEachCursorReset(JsonEachCursor *p){
2264 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00002265 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00002266 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00002267 p->iRowid = 0;
2268 p->i = 0;
2269 p->iEnd = 0;
2270 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00002271 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00002272 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002273}
2274
2275/* Destructor for a jsonEachCursor object */
2276static int jsonEachClose(sqlite3_vtab_cursor *cur){
2277 JsonEachCursor *p = (JsonEachCursor*)cur;
2278 jsonEachCursorReset(p);
2279 sqlite3_free(cur);
2280 return SQLITE_OK;
2281}
2282
2283/* Return TRUE if the jsonEachCursor object has been advanced off the end
2284** of the JSON object */
2285static int jsonEachEof(sqlite3_vtab_cursor *cur){
2286 JsonEachCursor *p = (JsonEachCursor*)cur;
2287 return p->i >= p->iEnd;
2288}
2289
drh505ad2c2015-08-21 17:33:11 +00002290/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00002291static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00002292 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00002293 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00002294 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
2295 p->i++;
drh4af352d2015-08-21 20:02:48 +00002296 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00002297 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00002298 u32 iUp = p->sParse.aUp[p->i];
2299 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00002300 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00002301 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002302 assert( pUp->eU==0 || pUp->eU==3 );
2303 json_testcase( pUp->eU==3 );
2304 VVA( pUp->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002305 if( iUp==p->i-1 ){
2306 pUp->u.iKey = 0;
2307 }else{
2308 pUp->u.iKey++;
2309 }
drh4af352d2015-08-21 20:02:48 +00002310 }
2311 }
drh505ad2c2015-08-21 17:33:11 +00002312 }else{
drh4af352d2015-08-21 20:02:48 +00002313 switch( p->eType ){
2314 case JSON_ARRAY: {
2315 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
2316 p->iRowid++;
2317 break;
2318 }
2319 case JSON_OBJECT: {
2320 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
2321 p->iRowid++;
2322 break;
2323 }
2324 default: {
2325 p->i = p->iEnd;
2326 break;
2327 }
drh505ad2c2015-08-21 17:33:11 +00002328 }
2329 }
2330 return SQLITE_OK;
2331}
2332
drh4af352d2015-08-21 20:02:48 +00002333/* Append the name of the path for element i to pStr
2334*/
2335static void jsonEachComputePath(
2336 JsonEachCursor *p, /* The cursor */
2337 JsonString *pStr, /* Write the path here */
2338 u32 i /* Path to this element */
2339){
2340 JsonNode *pNode, *pUp;
2341 u32 iUp;
2342 if( i==0 ){
2343 jsonAppendChar(pStr, '$');
2344 return;
drhcb6c6c62015-08-19 22:47:17 +00002345 }
drh4af352d2015-08-21 20:02:48 +00002346 iUp = p->sParse.aUp[i];
2347 jsonEachComputePath(p, pStr, iUp);
2348 pNode = &p->sParse.aNode[i];
2349 pUp = &p->sParse.aNode[iUp];
2350 if( pUp->eType==JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002351 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
2352 testcase( pUp->eU==0 );
drh4af352d2015-08-21 20:02:48 +00002353 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
2354 }else{
2355 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00002356 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00002357 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00002358 assert( pNode->jnFlags & JNODE_LABEL );
drh285f2ef2021-10-15 16:15:04 +00002359 assert( pNode->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002360 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
2361 }
drhcb6c6c62015-08-19 22:47:17 +00002362}
2363
2364/* Return the value of a column */
2365static int jsonEachColumn(
2366 sqlite3_vtab_cursor *cur, /* The cursor */
2367 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2368 int i /* Which column to return */
2369){
2370 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00002371 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00002372 switch( i ){
2373 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00002374 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00002375 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00002376 jsonReturn(pThis, ctx, 0);
2377 }else if( p->eType==JSON_ARRAY ){
2378 u32 iKey;
2379 if( p->bRecursive ){
2380 if( p->iRowid==0 ) break;
drh285f2ef2021-10-15 16:15:04 +00002381 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
drh8784eca2015-08-23 02:42:30 +00002382 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00002383 }else{
2384 iKey = p->iRowid;
2385 }
drh6fd5c1e2015-08-21 20:37:12 +00002386 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00002387 }
2388 break;
2389 }
2390 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00002391 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002392 jsonReturn(pThis, ctx, 0);
2393 break;
2394 }
2395 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00002396 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002397 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2398 break;
2399 }
2400 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00002401 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002402 if( pThis->eType>=JSON_ARRAY ) break;
2403 jsonReturn(pThis, ctx, 0);
2404 break;
2405 }
2406 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00002407 sqlite3_result_int64(ctx,
2408 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00002409 break;
2410 }
2411 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00002412 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00002413 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00002414 }
2415 break;
2416 }
drh4af352d2015-08-21 20:02:48 +00002417 case JEACH_FULLKEY: {
2418 JsonString x;
2419 jsonInit(&x, ctx);
2420 if( p->bRecursive ){
2421 jsonEachComputePath(p, &x, p->i);
2422 }else{
drh383de692015-09-10 17:20:57 +00002423 if( p->zRoot ){
2424 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00002425 }else{
2426 jsonAppendChar(&x, '$');
2427 }
2428 if( p->eType==JSON_ARRAY ){
2429 jsonPrintf(30, &x, "[%d]", p->iRowid);
drhdd7460f2018-05-16 12:19:11 +00002430 }else if( p->eType==JSON_OBJECT ){
drh285f2ef2021-10-15 16:15:04 +00002431 assert( pThis->eU==1 );
drh4af352d2015-08-21 20:02:48 +00002432 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2433 }
2434 }
2435 jsonResult(&x);
2436 break;
2437 }
drhcb6c6c62015-08-19 22:47:17 +00002438 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00002439 if( p->bRecursive ){
2440 JsonString x;
2441 jsonInit(&x, ctx);
2442 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2443 jsonResult(&x);
2444 break;
drh4af352d2015-08-21 20:02:48 +00002445 }
drh383de692015-09-10 17:20:57 +00002446 /* For json_each() path and root are the same so fall through
2447 ** into the root case */
drh08b92082020-08-10 14:18:00 +00002448 /* no break */ deliberate_fall_through
drh383de692015-09-10 17:20:57 +00002449 }
drh6850a632016-11-07 18:18:08 +00002450 default: {
drh383de692015-09-10 17:20:57 +00002451 const char *zRoot = p->zRoot;
drh6850a632016-11-07 18:18:08 +00002452 if( zRoot==0 ) zRoot = "$";
drh383de692015-09-10 17:20:57 +00002453 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00002454 break;
2455 }
drh3d1d2a92015-09-22 01:15:49 +00002456 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00002457 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00002458 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
2459 break;
2460 }
2461 }
2462 return SQLITE_OK;
2463}
2464
2465/* Return the current rowid value */
2466static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2467 JsonEachCursor *p = (JsonEachCursor*)cur;
2468 *pRowid = p->iRowid;
2469 return SQLITE_OK;
2470}
2471
2472/* The query strategy is to look for an equality constraint on the json
2473** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00002474** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00002475** and 0 otherwise.
2476*/
2477static int jsonEachBestIndex(
2478 sqlite3_vtab *tab,
2479 sqlite3_index_info *pIdxInfo
2480){
drh43579192018-11-16 16:04:50 +00002481 int i; /* Loop counter or computed array index */
2482 int aIdx[2]; /* Index of constraints for JSON and ROOT */
2483 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
2484 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
drhcb6c6c62015-08-19 22:47:17 +00002485 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00002486
drh43579192018-11-16 16:04:50 +00002487 /* This implementation assumes that JSON and ROOT are the last two
2488 ** columns in the table */
2489 assert( JEACH_ROOT == JEACH_JSON+1 );
drh6fd5c1e2015-08-21 20:37:12 +00002490 UNUSED_PARAM(tab);
drh43579192018-11-16 16:04:50 +00002491 aIdx[0] = aIdx[1] = -1;
drhcb6c6c62015-08-19 22:47:17 +00002492 pConstraint = pIdxInfo->aConstraint;
2493 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
drh43579192018-11-16 16:04:50 +00002494 int iCol;
2495 int iMask;
2496 if( pConstraint->iColumn < JEACH_JSON ) continue;
2497 iCol = pConstraint->iColumn - JEACH_JSON;
2498 assert( iCol==0 || iCol==1 );
drh285f2ef2021-10-15 16:15:04 +00002499 testcase( iCol==0 );
drh43579192018-11-16 16:04:50 +00002500 iMask = 1 << iCol;
2501 if( pConstraint->usable==0 ){
2502 unusableMask |= iMask;
2503 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
2504 aIdx[iCol] = i;
2505 idxMask |= iMask;
drhcb6c6c62015-08-19 22:47:17 +00002506 }
2507 }
drh43579192018-11-16 16:04:50 +00002508 if( (unusableMask & ~idxMask)!=0 ){
2509 /* If there are any unusable constraints on JSON or ROOT, then reject
2510 ** this entire plan */
2511 return SQLITE_CONSTRAINT;
2512 }
2513 if( aIdx[0]<0 ){
2514 /* No JSON input. Leave estimatedCost at the huge value that it was
2515 ** initialized to to discourage the query planner from selecting this
2516 ** plan. */
drhcb6c6c62015-08-19 22:47:17 +00002517 pIdxInfo->idxNum = 0;
drhcb6c6c62015-08-19 22:47:17 +00002518 }else{
drh505ad2c2015-08-21 17:33:11 +00002519 pIdxInfo->estimatedCost = 1.0;
drh43579192018-11-16 16:04:50 +00002520 i = aIdx[0];
2521 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
2522 pIdxInfo->aConstraintUsage[i].omit = 1;
2523 if( aIdx[1]<0 ){
2524 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
drhcb6c6c62015-08-19 22:47:17 +00002525 }else{
drh43579192018-11-16 16:04:50 +00002526 i = aIdx[1];
2527 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
2528 pIdxInfo->aConstraintUsage[i].omit = 1;
2529 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
drhcb6c6c62015-08-19 22:47:17 +00002530 }
2531 }
2532 return SQLITE_OK;
2533}
2534
2535/* Start a search on a new JSON string */
2536static int jsonEachFilter(
2537 sqlite3_vtab_cursor *cur,
2538 int idxNum, const char *idxStr,
2539 int argc, sqlite3_value **argv
2540){
2541 JsonEachCursor *p = (JsonEachCursor*)cur;
2542 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00002543 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002544 sqlite3_int64 n;
2545
drh6fd5c1e2015-08-21 20:37:12 +00002546 UNUSED_PARAM(idxStr);
2547 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002548 jsonEachCursorReset(p);
2549 if( idxNum==0 ) return SQLITE_OK;
2550 z = (const char*)sqlite3_value_text(argv[0]);
2551 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002552 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002553 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002554 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002555 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002556 if( jsonParse(&p->sParse, 0, p->zJson) ){
2557 int rc = SQLITE_NOMEM;
2558 if( p->sParse.oom==0 ){
2559 sqlite3_free(cur->pVtab->zErrMsg);
2560 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2561 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2562 }
drhcb6c6c62015-08-19 22:47:17 +00002563 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002564 return rc;
2565 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2566 jsonEachCursorReset(p);
2567 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002568 }else{
drh95677942015-09-24 01:06:37 +00002569 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002570 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002571 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002572 zRoot = (const char*)sqlite3_value_text(argv[1]);
2573 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002574 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002575 p->zRoot = sqlite3_malloc64( n+1 );
2576 if( p->zRoot==0 ) return SQLITE_NOMEM;
2577 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002578 if( zRoot[0]!='$' ){
2579 zErr = zRoot;
2580 }else{
2581 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2582 }
2583 if( zErr ){
drha7714022015-08-29 00:54:49 +00002584 sqlite3_free(cur->pVtab->zErrMsg);
2585 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002586 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002587 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2588 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002589 return SQLITE_OK;
2590 }
2591 }else{
2592 pNode = p->sParse.aNode;
2593 }
drh852944e2015-09-10 03:29:11 +00002594 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002595 p->eType = pNode->eType;
2596 if( p->eType>=JSON_ARRAY ){
drh285f2ef2021-10-15 16:15:04 +00002597 assert( pNode->eU==0 );
2598 VVA( pNode->eU = 3 );
drh8784eca2015-08-23 02:42:30 +00002599 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002600 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002601 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002602 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002603 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2604 p->i--;
2605 }
2606 }else{
2607 p->i++;
2608 }
drhcb6c6c62015-08-19 22:47:17 +00002609 }else{
2610 p->iEnd = p->i+1;
2611 }
2612 }
drha8f39a92015-09-21 22:53:16 +00002613 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002614}
2615
2616/* The methods of the json_each virtual table */
2617static sqlite3_module jsonEachModule = {
2618 0, /* iVersion */
2619 0, /* xCreate */
2620 jsonEachConnect, /* xConnect */
2621 jsonEachBestIndex, /* xBestIndex */
2622 jsonEachDisconnect, /* xDisconnect */
2623 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002624 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002625 jsonEachClose, /* xClose - close a cursor */
2626 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002627 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002628 jsonEachEof, /* xEof - check for end of scan */
2629 jsonEachColumn, /* xColumn - read data */
2630 jsonEachRowid, /* xRowid - read data */
2631 0, /* xUpdate */
2632 0, /* xBegin */
2633 0, /* xSync */
2634 0, /* xCommit */
2635 0, /* xRollback */
2636 0, /* xFindMethod */
2637 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002638 0, /* xSavepoint */
2639 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002640 0, /* xRollbackTo */
2641 0 /* xShadowName */
drhcb6c6c62015-08-19 22:47:17 +00002642};
2643
drh505ad2c2015-08-21 17:33:11 +00002644/* The methods of the json_tree virtual table. */
2645static sqlite3_module jsonTreeModule = {
2646 0, /* iVersion */
2647 0, /* xCreate */
2648 jsonEachConnect, /* xConnect */
2649 jsonEachBestIndex, /* xBestIndex */
2650 jsonEachDisconnect, /* xDisconnect */
2651 0, /* xDestroy */
2652 jsonEachOpenTree, /* xOpen - open a cursor */
2653 jsonEachClose, /* xClose - close a cursor */
2654 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002655 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002656 jsonEachEof, /* xEof - check for end of scan */
2657 jsonEachColumn, /* xColumn - read data */
2658 jsonEachRowid, /* xRowid - read data */
2659 0, /* xUpdate */
2660 0, /* xBegin */
2661 0, /* xSync */
2662 0, /* xCommit */
2663 0, /* xRollback */
2664 0, /* xFindMethod */
2665 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002666 0, /* xSavepoint */
2667 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002668 0, /* xRollbackTo */
2669 0 /* xShadowName */
drh505ad2c2015-08-21 17:33:11 +00002670};
drhd2975922015-08-29 17:22:33 +00002671#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002672
2673/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002674** The following routines are the only publically visible identifiers in this
2675** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002676** functions and the virtual table implemented by this file.
2677****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002678
drh2f20e132015-09-26 17:44:59 +00002679int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002680 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002681 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002682 static const struct {
2683 const char *zName;
2684 int nArg;
drh52216ad2015-08-18 02:28:03 +00002685 int flag;
drh5fa5c102015-08-12 16:49:40 +00002686 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2687 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002688 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002689 { "json_array", -1, 0, jsonArrayFunc },
2690 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2691 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002692 { "json_extract", -1, 0, jsonExtractFunc },
drh338b1fd2022-01-07 17:08:48 +00002693 { "json_nextract", -1, 1, jsonExtractFunc },
drha3f51d72022-01-07 17:26:40 +00002694 { "->", 2, 3, jsonExtractFunc },
2695 { "->>", 2, 2, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002696 { "json_insert", -1, 0, jsonSetFunc },
drha4e4e182022-01-07 16:03:00 +00002697 { "json_ntype", 1, 1, jsonTypeFunc },
drh52216ad2015-08-18 02:28:03 +00002698 { "json_object", -1, 0, jsonObjectFunc },
drh37f03df2017-03-23 20:33:49 +00002699 { "json_patch", 2, 0, jsonPatchFunc },
drh2ad96f52016-06-17 13:01:51 +00002700 { "json_quote", 1, 0, jsonQuoteFunc },
drh52216ad2015-08-18 02:28:03 +00002701 { "json_remove", -1, 0, jsonRemoveFunc },
2702 { "json_replace", -1, 0, jsonReplaceFunc },
2703 { "json_set", -1, 1, jsonSetFunc },
2704 { "json_type", 1, 0, jsonTypeFunc },
2705 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002706 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002707
drh301eecc2015-08-17 20:14:19 +00002708#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002709 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002710 { "json_parse", 1, 0, jsonParseFunc },
2711 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002712#endif
drh5fa5c102015-08-12 16:49:40 +00002713 };
drhff135ae2015-12-30 01:07:02 +00002714 static const struct {
2715 const char *zName;
2716 int nArg;
2717 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2718 void (*xFinal)(sqlite3_context*);
drh8be47a72018-07-05 20:05:29 +00002719 void (*xValue)(sqlite3_context*);
drhff135ae2015-12-30 01:07:02 +00002720 } aAgg[] = {
drh8be47a72018-07-05 20:05:29 +00002721 { "json_group_array", 1,
2722 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
2723 { "json_group_object", 2,
2724 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
drhff135ae2015-12-30 01:07:02 +00002725 };
drhd2975922015-08-29 17:22:33 +00002726#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002727 static const struct {
2728 const char *zName;
2729 sqlite3_module *pModule;
2730 } aMod[] = {
2731 { "json_each", &jsonEachModule },
2732 { "json_tree", &jsonTreeModule },
2733 };
drhd2975922015-08-29 17:22:33 +00002734#endif
drh79d5bc82020-01-04 01:43:02 +00002735 static const int enc =
2736 SQLITE_UTF8 |
2737 SQLITE_DETERMINISTIC |
2738 SQLITE_INNOCUOUS;
drh5fa5c102015-08-12 16:49:40 +00002739 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
drh79d5bc82020-01-04 01:43:02 +00002740 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
dan01a3b6b2019-09-13 17:05:48 +00002741 (void*)&aFunc[i].flag,
2742 aFunc[i].xFunc, 0, 0);
drh5fa5c102015-08-12 16:49:40 +00002743 }
mistachkin5a193dd2018-07-24 13:57:44 +00002744#ifndef SQLITE_OMIT_WINDOWFUNC
drhff135ae2015-12-30 01:07:02 +00002745 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
drh8be47a72018-07-05 20:05:29 +00002746 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
drh79d5bc82020-01-04 01:43:02 +00002747 SQLITE_SUBTYPE | enc, 0,
drh8be47a72018-07-05 20:05:29 +00002748 aAgg[i].xStep, aAgg[i].xFinal,
2749 aAgg[i].xValue, jsonGroupInverse, 0);
drhff135ae2015-12-30 01:07:02 +00002750 }
mistachkin5a193dd2018-07-24 13:57:44 +00002751#endif
drhd2975922015-08-29 17:22:33 +00002752#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002753 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2754 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002755 }
drhd2975922015-08-29 17:22:33 +00002756#endif
drh5fa5c102015-08-12 16:49:40 +00002757 return rc;
2758}
drh2f20e132015-09-26 17:44:59 +00002759
2760
dan8d32e802015-10-14 18:45:42 +00002761#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002762#ifdef _WIN32
2763__declspec(dllexport)
2764#endif
2765int sqlite3_json_init(
2766 sqlite3 *db,
2767 char **pzErrMsg,
2768 const sqlite3_api_routines *pApi
2769){
2770 SQLITE_EXTENSION_INIT2(pApi);
2771 (void)pzErrMsg; /* Unused parameter */
2772 return sqlite3Json1Init(db);
2773}
dan8d32e802015-10-14 18:45:42 +00002774#endif
drh50065652015-10-08 19:29:18 +00002775#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */