blob: 486eaa428515360cd9f97a13e891964165f81a44 [file] [log] [blame]
drh5fa5c102015-08-12 16:49:40 +00001/*
2** 2015-08-12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This SQLite extension implements JSON functions. The interface is
14** modeled after MySQL JSON functions:
15**
16** https://dev.mysql.com/doc/refman/5.7/en/json.html
17**
drh5634cc02015-08-17 11:28:03 +000018** For the time being, all JSON is stored as pure text. (We might add
19** a JSONB type in the future which stores a binary encoding of JSON in
drhcb6c6c62015-08-19 22:47:17 +000020** a BLOB, but there is no support for JSONB in the current implementation.
21** This implementation parses JSON text at 250 MB/s, so it is hard to see
22** how JSONB might improve on that.)
drh5fa5c102015-08-12 16:49:40 +000023*/
drh50065652015-10-08 19:29:18 +000024#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
drh666d34c2017-01-25 13:54:27 +000025#if !defined(SQLITEINT_H)
drh5fa5c102015-08-12 16:49:40 +000026#include "sqlite3ext.h"
drhf2df7e72015-08-28 20:07:40 +000027#endif
drh5fa5c102015-08-12 16:49:40 +000028SQLITE_EXTENSION_INIT1
29#include <assert.h>
30#include <string.h>
drh987eb1f2015-08-17 15:17:37 +000031#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000032#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000033
drhdf3a9072016-02-11 15:37:18 +000034/* Mark a function parameter as unused, to suppress nuisance compiler
35** warnings. */
36#ifndef UNUSED_PARAM
37# define UNUSED_PARAM(X) (void)(X)
38#endif
drh6fd5c1e2015-08-21 20:37:12 +000039
drh8deb4b82015-10-09 18:21:43 +000040#ifndef LARGEST_INT64
41# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
42# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
43#endif
44
dan2e8f5512015-09-17 17:21:09 +000045/*
46** Versions of isspace(), isalnum() and isdigit() to which it is safe
47** to pass signed char values.
48*/
drh49472652015-10-16 15:35:39 +000049#ifdef sqlite3Isdigit
50 /* Use the SQLite core versions if this routine is part of the
51 ** SQLite amalgamation */
drhad875e72016-11-07 13:37:28 +000052# define safe_isdigit(x) sqlite3Isdigit(x)
53# define safe_isalnum(x) sqlite3Isalnum(x)
54# define safe_isxdigit(x) sqlite3Isxdigit(x)
drh49472652015-10-16 15:35:39 +000055#else
56 /* Use the standard library for separate compilation */
57#include <ctype.h> /* amalgamator: keep */
drhad875e72016-11-07 13:37:28 +000058# define safe_isdigit(x) isdigit((unsigned char)(x))
59# define safe_isalnum(x) isalnum((unsigned char)(x))
60# define safe_isxdigit(x) isxdigit((unsigned char)(x))
drh49472652015-10-16 15:35:39 +000061#endif
dan2e8f5512015-09-17 17:21:09 +000062
drh95677942015-09-24 01:06:37 +000063/*
64** Growing our own isspace() routine this way is twice as fast as
65** the library isspace() function, resulting in a 7% overall performance
66** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
67*/
68static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000069 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000070 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85};
86#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
87
drh9a4718f2015-10-10 14:00:37 +000088#ifndef SQLITE_AMALGAMATION
89 /* Unsigned integer types. These are already defined in the sqliteInt.h,
90 ** but the definitions need to be repeated for separate compilation. */
91 typedef sqlite3_uint64 u64;
92 typedef unsigned int u32;
drhff6d50e2017-04-11 18:55:05 +000093 typedef unsigned short int u16;
drh9a4718f2015-10-10 14:00:37 +000094 typedef unsigned char u8;
95#endif
drh5fa5c102015-08-12 16:49:40 +000096
drh52216ad2015-08-18 02:28:03 +000097/* Objects */
drh505ad2c2015-08-21 17:33:11 +000098typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000099typedef struct JsonNode JsonNode;
100typedef struct JsonParse JsonParse;
101
drh5634cc02015-08-17 11:28:03 +0000102/* An instance of this object represents a JSON string
103** under construction. Really, this is a generic string accumulator
104** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000105*/
drh505ad2c2015-08-21 17:33:11 +0000106struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000107 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000108 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000109 u64 nAlloc; /* Bytes of storage available in zBuf[] */
110 u64 nUsed; /* Bytes of zBuf[] currently used */
111 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000112 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000113 char zSpace[100]; /* Initial static space */
114};
115
drhe9c37f32015-08-15 21:25:36 +0000116/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000117*/
drhe9c37f32015-08-15 21:25:36 +0000118#define JSON_NULL 0
119#define JSON_TRUE 1
120#define JSON_FALSE 2
121#define JSON_INT 3
122#define JSON_REAL 4
123#define JSON_STRING 5
124#define JSON_ARRAY 6
125#define JSON_OBJECT 7
126
drhf5ddb9c2015-09-11 00:06:41 +0000127/* The "subtype" set for JSON values */
128#define JSON_SUBTYPE 74 /* Ascii for "J" */
129
drh987eb1f2015-08-17 15:17:37 +0000130/*
131** Names of the various JSON types:
132*/
133static const char * const jsonType[] = {
134 "null", "true", "false", "integer", "real", "text", "array", "object"
135};
136
drh301eecc2015-08-17 20:14:19 +0000137/* Bit values for the JsonNode.jnFlag field
138*/
139#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
140#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
141#define JNODE_REMOVE 0x04 /* Do not output */
drh633647a2017-03-22 21:24:31 +0000142#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
143#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
144#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
145#define JNODE_LABEL 0x40 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000146
drh987eb1f2015-08-17 15:17:37 +0000147
drhe9c37f32015-08-15 21:25:36 +0000148/* A single node of parsed JSON
149*/
drhe9c37f32015-08-15 21:25:36 +0000150struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000151 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000152 u8 jnFlags; /* JNODE flags */
drhe9c37f32015-08-15 21:25:36 +0000153 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000154 union {
drh0042a972015-08-18 12:59:58 +0000155 const char *zJContent; /* Content for INT, REAL, and STRING */
156 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +0000157 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh633647a2017-03-22 21:24:31 +0000158 u32 iReplace; /* Replacement content for JNODE_REPLACE */
159 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
drh52216ad2015-08-18 02:28:03 +0000160 } u;
drhe9c37f32015-08-15 21:25:36 +0000161};
162
163/* A completely parsed JSON string
164*/
drhe9c37f32015-08-15 21:25:36 +0000165struct JsonParse {
166 u32 nNode; /* Number of slots of aNode[] used */
167 u32 nAlloc; /* Number of slots of aNode[] allocated */
168 JsonNode *aNode; /* Array of nodes containing the parse */
169 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000170 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000171 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000172 u8 nErr; /* Number of errors seen */
drhff6d50e2017-04-11 18:55:05 +0000173 u16 iDepth; /* Nesting depth */
drhe9c37f32015-08-15 21:25:36 +0000174};
175
drhff6d50e2017-04-11 18:55:05 +0000176/*
177** Maximum nesting depth of JSON for this implementation.
178**
179** This limit is needed to avoid a stack overflow in the recursive
180** descent parser. A depth of 2000 is far deeper than any sane JSON
181** should go.
182*/
183#define JSON_MAX_DEPTH 2000
184
drh505ad2c2015-08-21 17:33:11 +0000185/**************************************************************************
186** Utility routines for dealing with JsonString objects
187**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000188
drh505ad2c2015-08-21 17:33:11 +0000189/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000190*/
drh505ad2c2015-08-21 17:33:11 +0000191static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000192 p->zBuf = p->zSpace;
193 p->nAlloc = sizeof(p->zSpace);
194 p->nUsed = 0;
195 p->bStatic = 1;
196}
197
drh505ad2c2015-08-21 17:33:11 +0000198/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000199*/
drh505ad2c2015-08-21 17:33:11 +0000200static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000201 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000202 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000203 jsonZero(p);
204}
205
206
drh505ad2c2015-08-21 17:33:11 +0000207/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000208** initial state.
209*/
drh505ad2c2015-08-21 17:33:11 +0000210static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000211 if( !p->bStatic ) sqlite3_free(p->zBuf);
212 jsonZero(p);
213}
214
215
216/* Report an out-of-memory (OOM) condition
217*/
drh505ad2c2015-08-21 17:33:11 +0000218static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000219 p->bErr = 1;
220 sqlite3_result_error_nomem(p->pCtx);
221 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000222}
223
224/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
225** Return zero on success. Return non-zero on an OOM error
226*/
drh505ad2c2015-08-21 17:33:11 +0000227static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000228 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000229 char *zNew;
230 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000231 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000232 zNew = sqlite3_malloc64(nTotal);
233 if( zNew==0 ){
234 jsonOom(p);
235 return SQLITE_NOMEM;
236 }
drh6fd5c1e2015-08-21 20:37:12 +0000237 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000238 p->zBuf = zNew;
239 p->bStatic = 0;
240 }else{
241 zNew = sqlite3_realloc64(p->zBuf, nTotal);
242 if( zNew==0 ){
243 jsonOom(p);
244 return SQLITE_NOMEM;
245 }
246 p->zBuf = zNew;
247 }
248 p->nAlloc = nTotal;
249 return SQLITE_OK;
250}
251
drh505ad2c2015-08-21 17:33:11 +0000252/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000253*/
drh505ad2c2015-08-21 17:33:11 +0000254static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000255 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
256 memcpy(p->zBuf+p->nUsed, zIn, N);
257 p->nUsed += N;
258}
259
drh4af352d2015-08-21 20:02:48 +0000260/* Append formatted text (not to exceed N bytes) to the JsonString.
261*/
262static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
263 va_list ap;
264 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
265 va_start(ap, zFormat);
266 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
267 va_end(ap);
268 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
269}
270
drh5634cc02015-08-17 11:28:03 +0000271/* Append a single character
272*/
drh505ad2c2015-08-21 17:33:11 +0000273static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000274 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
275 p->zBuf[p->nUsed++] = c;
276}
277
drh301eecc2015-08-17 20:14:19 +0000278/* Append a comma separator to the output buffer, if the previous
279** character is not '[' or '{'.
280*/
drh505ad2c2015-08-21 17:33:11 +0000281static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000282 char c;
283 if( p->nUsed==0 ) return;
284 c = p->zBuf[p->nUsed-1];
285 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
286}
287
drh505ad2c2015-08-21 17:33:11 +0000288/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000289** under construction. Enclose the string in "..." and escape
290** any double-quotes or backslash characters contained within the
291** string.
292*/
drh505ad2c2015-08-21 17:33:11 +0000293static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000294 u32 i;
295 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
296 p->zBuf[p->nUsed++] = '"';
297 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000298 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000299 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000300 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000301 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000302 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000303 }else if( c<=0x1f ){
304 static const char aSpecial[] = {
305 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
307 };
308 assert( sizeof(aSpecial)==32 );
309 assert( aSpecial['\b']=='b' );
310 assert( aSpecial['\f']=='f' );
311 assert( aSpecial['\n']=='n' );
312 assert( aSpecial['\r']=='r' );
313 assert( aSpecial['\t']=='t' );
314 if( aSpecial[c] ){
315 c = aSpecial[c];
316 goto json_simple_escape;
317 }
318 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
319 p->zBuf[p->nUsed++] = '\\';
320 p->zBuf[p->nUsed++] = 'u';
321 p->zBuf[p->nUsed++] = '0';
322 p->zBuf[p->nUsed++] = '0';
323 p->zBuf[p->nUsed++] = '0' + (c>>4);
324 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000325 }
326 p->zBuf[p->nUsed++] = c;
327 }
328 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000329 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000330}
331
drhd0960592015-08-17 21:22:32 +0000332/*
333** Append a function parameter value to the JSON string under
334** construction.
335*/
336static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000337 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000338 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000339){
340 switch( sqlite3_value_type(pValue) ){
341 case SQLITE_NULL: {
342 jsonAppendRaw(p, "null", 4);
343 break;
344 }
345 case SQLITE_INTEGER:
346 case SQLITE_FLOAT: {
347 const char *z = (const char*)sqlite3_value_text(pValue);
348 u32 n = (u32)sqlite3_value_bytes(pValue);
349 jsonAppendRaw(p, z, n);
350 break;
351 }
352 case SQLITE_TEXT: {
353 const char *z = (const char*)sqlite3_value_text(pValue);
354 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000355 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000356 jsonAppendRaw(p, z, n);
357 }else{
358 jsonAppendString(p, z, n);
359 }
drhd0960592015-08-17 21:22:32 +0000360 break;
361 }
362 default: {
363 if( p->bErr==0 ){
364 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000365 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000366 jsonReset(p);
367 }
368 break;
369 }
370 }
371}
372
373
drhbd0621b2015-08-13 13:54:59 +0000374/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000375*/
drh505ad2c2015-08-21 17:33:11 +0000376static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000377 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000378 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
379 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
380 SQLITE_UTF8);
381 jsonZero(p);
382 }
383 assert( p->bStatic );
384}
385
drh505ad2c2015-08-21 17:33:11 +0000386/**************************************************************************
387** Utility routines for dealing with JsonNode and JsonParse objects
388**************************************************************************/
389
390/*
391** Return the number of consecutive JsonNode slots need to represent
392** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
393** OBJECT types, the number might be larger.
394**
395** Appended elements are not counted. The value returned is the number
396** by which the JsonNode counter should increment in order to go to the
397** next peer value.
398*/
399static u32 jsonNodeSize(JsonNode *pNode){
400 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
401}
402
403/*
404** Reclaim all memory allocated by a JsonParse object. But do not
405** delete the JsonParse object itself.
406*/
407static void jsonParseReset(JsonParse *pParse){
408 sqlite3_free(pParse->aNode);
409 pParse->aNode = 0;
410 pParse->nNode = 0;
411 pParse->nAlloc = 0;
412 sqlite3_free(pParse->aUp);
413 pParse->aUp = 0;
414}
415
drh5634cc02015-08-17 11:28:03 +0000416/*
417** Convert the JsonNode pNode into a pure JSON string and
418** append to pOut. Subsubstructure is also included. Return
419** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000420*/
drh52216ad2015-08-18 02:28:03 +0000421static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000422 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000423 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000424 sqlite3_value **aReplace /* Replacement values */
425){
drh633647a2017-03-22 21:24:31 +0000426 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
427 if( pNode->jnFlags & JNODE_REPLACE ){
428 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
429 return;
430 }
431 pNode = pNode->u.pPatch;
432 }
drh5634cc02015-08-17 11:28:03 +0000433 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000434 default: {
435 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000436 jsonAppendRaw(pOut, "null", 4);
437 break;
438 }
439 case JSON_TRUE: {
440 jsonAppendRaw(pOut, "true", 4);
441 break;
442 }
443 case JSON_FALSE: {
444 jsonAppendRaw(pOut, "false", 5);
445 break;
446 }
447 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000448 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000449 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000450 break;
451 }
452 /* Fall through into the next case */
453 }
454 case JSON_REAL:
455 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000456 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000457 break;
458 }
459 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000460 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000461 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000462 for(;;){
463 while( j<=pNode->n ){
drh633647a2017-03-22 21:24:31 +0000464 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +0000465 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000466 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000467 }
drh505ad2c2015-08-21 17:33:11 +0000468 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000469 }
drh52216ad2015-08-18 02:28:03 +0000470 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
471 pNode = &pNode[pNode->u.iAppend];
472 j = 1;
drh5634cc02015-08-17 11:28:03 +0000473 }
474 jsonAppendChar(pOut, ']');
475 break;
476 }
477 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000478 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000479 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000480 for(;;){
481 while( j<=pNode->n ){
482 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
483 jsonAppendSeparator(pOut);
484 jsonRenderNode(&pNode[j], pOut, aReplace);
485 jsonAppendChar(pOut, ':');
drh633647a2017-03-22 21:24:31 +0000486 jsonRenderNode(&pNode[j+1], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000487 }
drh505ad2c2015-08-21 17:33:11 +0000488 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000489 }
drh52216ad2015-08-18 02:28:03 +0000490 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
491 pNode = &pNode[pNode->u.iAppend];
492 j = 1;
drh5634cc02015-08-17 11:28:03 +0000493 }
494 jsonAppendChar(pOut, '}');
495 break;
496 }
drhbd0621b2015-08-13 13:54:59 +0000497 }
drh5634cc02015-08-17 11:28:03 +0000498}
499
500/*
drhf2df7e72015-08-28 20:07:40 +0000501** Return a JsonNode and all its descendents as a JSON string.
502*/
503static void jsonReturnJson(
504 JsonNode *pNode, /* Node to return */
505 sqlite3_context *pCtx, /* Return value for this function */
506 sqlite3_value **aReplace /* Array of replacement values */
507){
508 JsonString s;
509 jsonInit(&s, pCtx);
510 jsonRenderNode(pNode, &s, aReplace);
511 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000512 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000513}
514
515/*
drh5634cc02015-08-17 11:28:03 +0000516** Make the JsonNode the return value of the function.
517*/
drhd0960592015-08-17 21:22:32 +0000518static void jsonReturn(
519 JsonNode *pNode, /* Node to return */
520 sqlite3_context *pCtx, /* Return value for this function */
521 sqlite3_value **aReplace /* Array of replacement values */
522){
drh5634cc02015-08-17 11:28:03 +0000523 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000524 default: {
525 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000526 sqlite3_result_null(pCtx);
527 break;
528 }
529 case JSON_TRUE: {
530 sqlite3_result_int(pCtx, 1);
531 break;
532 }
533 case JSON_FALSE: {
534 sqlite3_result_int(pCtx, 0);
535 break;
536 }
drh987eb1f2015-08-17 15:17:37 +0000537 case JSON_INT: {
538 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000539 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000540 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000541 while( z[0]>='0' && z[0]<='9' ){
542 unsigned v = *(z++) - '0';
543 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000544 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000545 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
546 if( v==9 ) goto int_as_real;
547 if( v==8 ){
548 if( pNode->u.zJContent[0]=='-' ){
549 sqlite3_result_int64(pCtx, SMALLEST_INT64);
550 goto int_done;
551 }else{
552 goto int_as_real;
553 }
554 }
555 }
556 i = i*10 + v;
557 }
drh52216ad2015-08-18 02:28:03 +0000558 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000559 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000560 int_done:
561 break;
562 int_as_real: /* fall through to real */;
563 }
564 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000565 double r;
566#ifdef SQLITE_AMALGAMATION
567 const char *z = pNode->u.zJContent;
568 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
569#else
570 r = strtod(pNode->u.zJContent, 0);
571#endif
drh8deb4b82015-10-09 18:21:43 +0000572 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000573 break;
574 }
drh5634cc02015-08-17 11:28:03 +0000575 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000576#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
577 ** json_insert() and json_replace() and those routines do not
578 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000579 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000580 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
581 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000582 }else
583#endif
584 assert( (pNode->jnFlags & JNODE_RAW)==0 );
585 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000586 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000587 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000588 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000589 }else{
590 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000591 u32 i;
592 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000593 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000594 char *zOut;
595 u32 j;
596 zOut = sqlite3_malloc( n+1 );
597 if( zOut==0 ){
598 sqlite3_result_error_nomem(pCtx);
599 break;
600 }
601 for(i=1, j=0; i<n-1; i++){
602 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000603 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000604 zOut[j++] = c;
605 }else{
606 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000607 if( c=='u' ){
drh987eb1f2015-08-17 15:17:37 +0000608 u32 v = 0, k;
drh27b2d1b2016-11-07 15:15:42 +0000609 for(k=0; k<4; i++, k++){
610 assert( i<n-2 );
drh8784eca2015-08-23 02:42:30 +0000611 c = z[i+1];
drh27b2d1b2016-11-07 15:15:42 +0000612 assert( safe_isxdigit(c) );
613 if( c<='9' ) v = v*16 + c - '0';
614 else if( c<='F' ) v = v*16 + c - 'A' + 10;
615 else v = v*16 + c - 'a' + 10;
drh987eb1f2015-08-17 15:17:37 +0000616 }
drh80d87402015-08-24 12:42:41 +0000617 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000618 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000619 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000620 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000621 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000622 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000623 }else{
mistachkin16a93122015-09-11 18:05:01 +0000624 zOut[j++] = (char)(0xe0 | (v>>12));
drh987eb1f2015-08-17 15:17:37 +0000625 zOut[j++] = 0x80 | ((v>>6)&0x3f);
626 zOut[j++] = 0x80 | (v&0x3f);
drh987eb1f2015-08-17 15:17:37 +0000627 }
628 }else{
629 if( c=='b' ){
630 c = '\b';
631 }else if( c=='f' ){
632 c = '\f';
633 }else if( c=='n' ){
634 c = '\n';
635 }else if( c=='r' ){
636 c = '\r';
637 }else if( c=='t' ){
638 c = '\t';
639 }
640 zOut[j++] = c;
641 }
642 }
643 }
644 zOut[j] = 0;
645 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000646 }
647 break;
648 }
649 case JSON_ARRAY:
650 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000651 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000652 break;
653 }
654 }
drhbd0621b2015-08-13 13:54:59 +0000655}
656
drh95677942015-09-24 01:06:37 +0000657/* Forward reference */
658static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
659
660/*
661** A macro to hint to the compiler that a function should not be
662** inlined.
663*/
664#if defined(__GNUC__)
665# define JSON_NOINLINE __attribute__((noinline))
666#elif defined(_MSC_VER) && _MSC_VER>=1310
667# define JSON_NOINLINE __declspec(noinline)
668#else
669# define JSON_NOINLINE
670#endif
671
672
673static JSON_NOINLINE int jsonParseAddNodeExpand(
674 JsonParse *pParse, /* Append the node to this object */
675 u32 eType, /* Node type */
676 u32 n, /* Content size or sub-node count */
677 const char *zContent /* Content */
678){
679 u32 nNew;
680 JsonNode *pNew;
681 assert( pParse->nNode>=pParse->nAlloc );
682 if( pParse->oom ) return -1;
683 nNew = pParse->nAlloc*2 + 10;
684 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
685 if( pNew==0 ){
686 pParse->oom = 1;
687 return -1;
688 }
689 pParse->nAlloc = nNew;
690 pParse->aNode = pNew;
691 assert( pParse->nNode<pParse->nAlloc );
692 return jsonParseAddNode(pParse, eType, n, zContent);
693}
694
drh5fa5c102015-08-12 16:49:40 +0000695/*
drhe9c37f32015-08-15 21:25:36 +0000696** Create a new JsonNode instance based on the arguments and append that
697** instance to the JsonParse. Return the index in pParse->aNode[] of the
698** new node, or -1 if a memory allocation fails.
699*/
700static int jsonParseAddNode(
701 JsonParse *pParse, /* Append the node to this object */
702 u32 eType, /* Node type */
703 u32 n, /* Content size or sub-node count */
704 const char *zContent /* Content */
705){
706 JsonNode *p;
707 if( pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000708 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000709 }
710 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000711 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000712 p->jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000713 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000714 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000715 return pParse->nNode++;
716}
717
718/*
drhad875e72016-11-07 13:37:28 +0000719** Return true if z[] begins with 4 (or more) hexadecimal digits
720*/
721static int jsonIs4Hex(const char *z){
722 int i;
723 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
724 return 1;
725}
726
727/*
drhe9c37f32015-08-15 21:25:36 +0000728** Parse a single JSON value which begins at pParse->zJson[i]. Return the
729** index of the first character past the end of the value parsed.
730**
731** Return negative for a syntax error. Special cases: return -2 if the
732** first non-whitespace character is '}' and return -3 if the first
733** non-whitespace character is ']'.
734*/
735static int jsonParseValue(JsonParse *pParse, u32 i){
736 char c;
737 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000738 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000739 int x;
drh852944e2015-09-10 03:29:11 +0000740 JsonNode *pNode;
drh9fa866a2017-04-08 18:18:22 +0000741 const char *z = pParse->zJson;
742 while( safe_isspace(z[i]) ){ i++; }
743 if( (c = z[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000744 /* Parse object */
745 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000746 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000747 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000748 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000749 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000750 x = jsonParseValue(pParse, j);
751 if( x<0 ){
drhff6d50e2017-04-11 18:55:05 +0000752 pParse->iDepth--;
drhf27cd1f2015-09-23 01:10:29 +0000753 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000754 return -1;
755 }
drhbe9474e2015-08-22 03:05:54 +0000756 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000757 pNode = &pParse->aNode[pParse->nNode-1];
758 if( pNode->eType!=JSON_STRING ) return -1;
759 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000760 j = x;
drh9fa866a2017-04-08 18:18:22 +0000761 while( safe_isspace(z[j]) ){ j++; }
762 if( z[j]!=':' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000763 j++;
764 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000765 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000766 if( x<0 ) return -1;
767 j = x;
drh9fa866a2017-04-08 18:18:22 +0000768 while( safe_isspace(z[j]) ){ j++; }
769 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000770 if( c==',' ) continue;
771 if( c!='}' ) return -1;
772 break;
773 }
drhbc8f0922015-08-22 19:39:04 +0000774 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000775 return j+1;
776 }else if( c=='[' ){
777 /* Parse array */
778 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000779 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000780 for(j=i+1;;j++){
drh9fa866a2017-04-08 18:18:22 +0000781 while( safe_isspace(z[j]) ){ j++; }
drhff6d50e2017-04-11 18:55:05 +0000782 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000783 x = jsonParseValue(pParse, j);
drhff6d50e2017-04-11 18:55:05 +0000784 pParse->iDepth--;
drhe9c37f32015-08-15 21:25:36 +0000785 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000786 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000787 return -1;
788 }
789 j = x;
drh9fa866a2017-04-08 18:18:22 +0000790 while( safe_isspace(z[j]) ){ j++; }
791 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000792 if( c==',' ) continue;
793 if( c!=']' ) return -1;
794 break;
795 }
drhbc8f0922015-08-22 19:39:04 +0000796 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000797 return j+1;
798 }else if( c=='"' ){
799 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000800 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000801 j = i+1;
802 for(;;){
drh9fa866a2017-04-08 18:18:22 +0000803 c = z[j];
drhe12e24d2017-04-10 12:25:05 +0000804 if( c<=0x1f ) return -1; /* Control characters not allowed in strings */
drhe9c37f32015-08-15 21:25:36 +0000805 if( c=='\\' ){
drh9fa866a2017-04-08 18:18:22 +0000806 c = z[++j];
drhad875e72016-11-07 13:37:28 +0000807 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
808 || c=='n' || c=='r' || c=='t'
drh9fa866a2017-04-08 18:18:22 +0000809 || (c=='u' && jsonIs4Hex(z+j+1)) ){
drhad875e72016-11-07 13:37:28 +0000810 jnFlags = JNODE_ESCAPE;
811 }else{
812 return -1;
813 }
drhe9c37f32015-08-15 21:25:36 +0000814 }else if( c=='"' ){
815 break;
816 }
817 j++;
818 }
drh9fa866a2017-04-08 18:18:22 +0000819 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
drhbe9474e2015-08-22 03:05:54 +0000820 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000821 return j+1;
822 }else if( c=='n'
drh9fa866a2017-04-08 18:18:22 +0000823 && strncmp(z+i,"null",4)==0
824 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000825 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
826 return i+4;
827 }else if( c=='t'
drh9fa866a2017-04-08 18:18:22 +0000828 && strncmp(z+i,"true",4)==0
829 && !safe_isalnum(z[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000830 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
831 return i+4;
832 }else if( c=='f'
drh9fa866a2017-04-08 18:18:22 +0000833 && strncmp(z+i,"false",5)==0
834 && !safe_isalnum(z[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000835 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
836 return i+5;
837 }else if( c=='-' || (c>='0' && c<='9') ){
838 /* Parse number */
839 u8 seenDP = 0;
840 u8 seenE = 0;
drh9fa866a2017-04-08 18:18:22 +0000841 assert( '-' < '0' );
842 if( c<='0' ){
843 j = c=='-' ? i+1 : i;
844 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
845 }
drhe9c37f32015-08-15 21:25:36 +0000846 j = i+1;
847 for(;; j++){
drh9fa866a2017-04-08 18:18:22 +0000848 c = z[j];
drhe9c37f32015-08-15 21:25:36 +0000849 if( c>='0' && c<='9' ) continue;
850 if( c=='.' ){
drh9fa866a2017-04-08 18:18:22 +0000851 if( z[j-1]=='-' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000852 if( seenDP ) return -1;
853 seenDP = 1;
854 continue;
855 }
856 if( c=='e' || c=='E' ){
drh9fa866a2017-04-08 18:18:22 +0000857 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000858 if( seenE ) return -1;
859 seenDP = seenE = 1;
drh9fa866a2017-04-08 18:18:22 +0000860 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000861 if( c=='+' || c=='-' ){
862 j++;
drh9fa866a2017-04-08 18:18:22 +0000863 c = z[j+1];
drh8784eca2015-08-23 02:42:30 +0000864 }
drhd1f00682015-08-29 16:02:37 +0000865 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000866 continue;
867 }
868 break;
869 }
drh9fa866a2017-04-08 18:18:22 +0000870 if( z[j-1]<'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000871 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
drh9fa866a2017-04-08 18:18:22 +0000872 j - i, &z[i]);
drhe9c37f32015-08-15 21:25:36 +0000873 return j;
874 }else if( c=='}' ){
875 return -2; /* End of {...} */
876 }else if( c==']' ){
877 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000878 }else if( c==0 ){
879 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000880 }else{
881 return -1; /* Syntax error */
882 }
883}
884
885/*
886** Parse a complete JSON string. Return 0 on success or non-zero if there
887** are any errors. If an error occurs, free all memory associated with
888** pParse.
889**
890** pParse is uninitialized when this routine is called.
891*/
drhbc8f0922015-08-22 19:39:04 +0000892static int jsonParse(
893 JsonParse *pParse, /* Initialize and fill this JsonParse object */
894 sqlite3_context *pCtx, /* Report errors here */
895 const char *zJson /* Input JSON text to be parsed */
896){
drhe9c37f32015-08-15 21:25:36 +0000897 int i;
drhe9c37f32015-08-15 21:25:36 +0000898 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000899 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000900 pParse->zJson = zJson;
901 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000902 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000903 if( i>0 ){
drhff6d50e2017-04-11 18:55:05 +0000904 assert( pParse->iDepth==0 );
dan2e8f5512015-09-17 17:21:09 +0000905 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +0000906 if( zJson[i] ) i = -1;
907 }
drhd1f00682015-08-29 16:02:37 +0000908 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +0000909 if( pCtx!=0 ){
910 if( pParse->oom ){
911 sqlite3_result_error_nomem(pCtx);
912 }else{
913 sqlite3_result_error(pCtx, "malformed JSON", -1);
914 }
915 }
drh505ad2c2015-08-21 17:33:11 +0000916 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000917 return 1;
918 }
919 return 0;
920}
drh301eecc2015-08-17 20:14:19 +0000921
drh505ad2c2015-08-21 17:33:11 +0000922/* Mark node i of pParse as being a child of iParent. Call recursively
923** to fill in all the descendants of node i.
924*/
925static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
926 JsonNode *pNode = &pParse->aNode[i];
927 u32 j;
928 pParse->aUp[i] = iParent;
929 switch( pNode->eType ){
930 case JSON_ARRAY: {
931 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
932 jsonParseFillInParentage(pParse, i+j, i);
933 }
934 break;
935 }
936 case JSON_OBJECT: {
937 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
938 pParse->aUp[i+j] = i;
939 jsonParseFillInParentage(pParse, i+j+1, i);
940 }
941 break;
942 }
943 default: {
944 break;
945 }
946 }
947}
948
949/*
950** Compute the parentage of all nodes in a completed parse.
951*/
952static int jsonParseFindParents(JsonParse *pParse){
953 u32 *aUp;
954 assert( pParse->aUp==0 );
955 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000956 if( aUp==0 ){
957 pParse->oom = 1;
958 return SQLITE_NOMEM;
959 }
drh505ad2c2015-08-21 17:33:11 +0000960 jsonParseFillInParentage(pParse, 0, 0);
961 return SQLITE_OK;
962}
963
drh8cb0c832015-09-22 00:21:03 +0000964/*
965** Compare the OBJECT label at pNode against zKey,nKey. Return true on
966** a match.
967*/
mistachkinf2c26ed2015-10-12 22:20:29 +0000968static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh8cb0c832015-09-22 00:21:03 +0000969 if( pNode->jnFlags & JNODE_RAW ){
970 if( pNode->n!=nKey ) return 0;
971 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
972 }else{
973 if( pNode->n!=nKey+2 ) return 0;
974 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
975 }
976}
977
drh52216ad2015-08-18 02:28:03 +0000978/* forward declaration */
drha7714022015-08-29 00:54:49 +0000979static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +0000980
drh987eb1f2015-08-17 15:17:37 +0000981/*
982** Search along zPath to find the node specified. Return a pointer
983** to that node, or NULL if zPath is malformed or if there is no such
984** node.
drh52216ad2015-08-18 02:28:03 +0000985**
986** If pApnd!=0, then try to append new nodes to complete zPath if it is
987** possible to do so and if no existing node corresponds to zPath. If
988** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000989*/
drha7714022015-08-29 00:54:49 +0000990static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +0000991 JsonParse *pParse, /* The JSON to search */
992 u32 iRoot, /* Begin the search at this node */
993 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +0000994 int *pApnd, /* Append nodes to complete path if not NULL */
995 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +0000996){
drhbc8f0922015-08-22 19:39:04 +0000997 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000998 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000999 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +00001000 if( zPath[0]==0 ) return pRoot;
1001 if( zPath[0]=='.' ){
1002 if( pRoot->eType!=JSON_OBJECT ) return 0;
1003 zPath++;
drh6b43cc82015-08-19 23:02:49 +00001004 if( zPath[0]=='"' ){
1005 zKey = zPath + 1;
1006 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
1007 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +00001008 if( zPath[i] ){
1009 i++;
1010 }else{
1011 *pzErr = zPath;
1012 return 0;
1013 }
drh6b43cc82015-08-19 23:02:49 +00001014 }else{
1015 zKey = zPath;
1016 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
1017 nKey = i;
1018 }
drha7714022015-08-29 00:54:49 +00001019 if( nKey==0 ){
1020 *pzErr = zPath;
1021 return 0;
1022 }
drh987eb1f2015-08-17 15:17:37 +00001023 j = 1;
drh52216ad2015-08-18 02:28:03 +00001024 for(;;){
1025 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +00001026 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +00001027 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001028 }
1029 j++;
drh505ad2c2015-08-21 17:33:11 +00001030 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +00001031 }
drh52216ad2015-08-18 02:28:03 +00001032 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1033 iRoot += pRoot->u.iAppend;
1034 pRoot = &pParse->aNode[iRoot];
1035 j = 1;
1036 }
1037 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001038 u32 iStart, iLabel;
1039 JsonNode *pNode;
1040 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1041 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +00001042 zPath += i;
drha7714022015-08-29 00:54:49 +00001043 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001044 if( pParse->oom ) return 0;
1045 if( pNode ){
1046 pRoot = &pParse->aNode[iRoot];
1047 pRoot->u.iAppend = iStart - iRoot;
1048 pRoot->jnFlags |= JNODE_APPEND;
1049 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1050 }
1051 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001052 }
dan2e8f5512015-09-17 17:21:09 +00001053 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
drh987eb1f2015-08-17 15:17:37 +00001054 if( pRoot->eType!=JSON_ARRAY ) return 0;
1055 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001056 j = 1;
1057 while( safe_isdigit(zPath[j]) ){
1058 i = i*10 + zPath[j] - '0';
1059 j++;
drh987eb1f2015-08-17 15:17:37 +00001060 }
drh3d1d2a92015-09-22 01:15:49 +00001061 if( zPath[j]!=']' ){
drha7714022015-08-29 00:54:49 +00001062 *pzErr = zPath;
1063 return 0;
1064 }
drh3d1d2a92015-09-22 01:15:49 +00001065 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001066 j = 1;
drh52216ad2015-08-18 02:28:03 +00001067 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001068 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1069 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001070 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001071 }
1072 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1073 iRoot += pRoot->u.iAppend;
1074 pRoot = &pParse->aNode[iRoot];
1075 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001076 }
1077 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001078 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001079 }
1080 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001081 u32 iStart;
1082 JsonNode *pNode;
1083 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001084 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001085 if( pParse->oom ) return 0;
1086 if( pNode ){
1087 pRoot = &pParse->aNode[iRoot];
1088 pRoot->u.iAppend = iStart - iRoot;
1089 pRoot->jnFlags |= JNODE_APPEND;
1090 }
1091 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001092 }
drh3d1d2a92015-09-22 01:15:49 +00001093 }else{
drha7714022015-08-29 00:54:49 +00001094 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001095 }
1096 return 0;
1097}
1098
drh52216ad2015-08-18 02:28:03 +00001099/*
drhbc8f0922015-08-22 19:39:04 +00001100** Append content to pParse that will complete zPath. Return a pointer
1101** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001102*/
1103static JsonNode *jsonLookupAppend(
1104 JsonParse *pParse, /* Append content to the JSON parse */
1105 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001106 int *pApnd, /* Set this flag to 1 */
1107 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001108){
1109 *pApnd = 1;
1110 if( zPath[0]==0 ){
1111 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1112 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1113 }
1114 if( zPath[0]=='.' ){
1115 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1116 }else if( strncmp(zPath,"[0]",3)==0 ){
1117 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1118 }else{
1119 return 0;
1120 }
1121 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001122 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001123}
1124
drhbc8f0922015-08-22 19:39:04 +00001125/*
drha7714022015-08-29 00:54:49 +00001126** Return the text of a syntax error message on a JSON path. Space is
1127** obtained from sqlite3_malloc().
1128*/
1129static char *jsonPathSyntaxError(const char *zErr){
1130 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1131}
1132
1133/*
1134** Do a node lookup using zPath. Return a pointer to the node on success.
1135** Return NULL if not found or if there is an error.
1136**
1137** On an error, write an error message into pCtx and increment the
1138** pParse->nErr counter.
1139**
1140** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1141** nodes are appended.
drha7714022015-08-29 00:54:49 +00001142*/
1143static JsonNode *jsonLookup(
1144 JsonParse *pParse, /* The JSON to search */
1145 const char *zPath, /* The path to search */
1146 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001147 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001148){
1149 const char *zErr = 0;
1150 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001151 char *zMsg;
drha7714022015-08-29 00:54:49 +00001152
1153 if( zPath==0 ) return 0;
1154 if( zPath[0]!='$' ){
1155 zErr = zPath;
1156 goto lookup_err;
1157 }
1158 zPath++;
drha7714022015-08-29 00:54:49 +00001159 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001160 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001161
1162lookup_err:
1163 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001164 assert( zErr!=0 && pCtx!=0 );
1165 zMsg = jsonPathSyntaxError(zErr);
1166 if( zMsg ){
1167 sqlite3_result_error(pCtx, zMsg, -1);
1168 sqlite3_free(zMsg);
1169 }else{
1170 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001171 }
drha7714022015-08-29 00:54:49 +00001172 return 0;
1173}
1174
1175
1176/*
drhbc8f0922015-08-22 19:39:04 +00001177** Report the wrong number of arguments for json_insert(), json_replace()
1178** or json_set().
1179*/
1180static void jsonWrongNumArgs(
1181 sqlite3_context *pCtx,
1182 const char *zFuncName
1183){
1184 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1185 zFuncName);
1186 sqlite3_result_error(pCtx, zMsg, -1);
1187 sqlite3_free(zMsg);
1188}
drh52216ad2015-08-18 02:28:03 +00001189
drh29c99692017-03-24 12:35:17 +00001190/*
1191** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1192*/
1193static void jsonRemoveAllNulls(JsonNode *pNode){
1194 int i, n;
1195 assert( pNode->eType==JSON_OBJECT );
1196 n = pNode->n;
1197 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1198 switch( pNode[i].eType ){
1199 case JSON_NULL:
1200 pNode[i].jnFlags |= JNODE_REMOVE;
1201 break;
1202 case JSON_OBJECT:
1203 jsonRemoveAllNulls(&pNode[i]);
1204 break;
1205 }
1206 }
1207}
1208
drha7714022015-08-29 00:54:49 +00001209
drh987eb1f2015-08-17 15:17:37 +00001210/****************************************************************************
1211** SQL functions used for testing and debugging
1212****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001213
drh301eecc2015-08-17 20:14:19 +00001214#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001215/*
drh5634cc02015-08-17 11:28:03 +00001216** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001217** a parse of the JSON provided. Or it returns NULL if JSON is not
1218** well-formed.
1219*/
drh5634cc02015-08-17 11:28:03 +00001220static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001221 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001222 int argc,
1223 sqlite3_value **argv
1224){
drh505ad2c2015-08-21 17:33:11 +00001225 JsonString s; /* Output string - not real JSON */
1226 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001227 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001228
1229 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001230 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001231 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001232 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001233 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001234 const char *zType;
1235 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1236 assert( x.aNode[i].eType==JSON_STRING );
1237 zType = "label";
1238 }else{
1239 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001240 }
drh852944e2015-09-10 03:29:11 +00001241 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1242 i, zType, x.aNode[i].n, x.aUp[i]);
1243 if( x.aNode[i].u.zJContent!=0 ){
1244 jsonAppendRaw(&s, " ", 1);
1245 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1246 }
1247 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001248 }
drh505ad2c2015-08-21 17:33:11 +00001249 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001250 jsonResult(&s);
1251}
1252
drh5634cc02015-08-17 11:28:03 +00001253/*
drhf5ddb9c2015-09-11 00:06:41 +00001254** The json_test1(JSON) function return true (1) if the input is JSON
1255** text generated by another json function. It returns (0) if the input
1256** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001257*/
1258static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001259 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001260 int argc,
1261 sqlite3_value **argv
1262){
mistachkin16a93122015-09-11 18:05:01 +00001263 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001264 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001265}
drh301eecc2015-08-17 20:14:19 +00001266#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001267
drh987eb1f2015-08-17 15:17:37 +00001268/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001269** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001270****************************************************************************/
1271
1272/*
drh2ad96f52016-06-17 13:01:51 +00001273** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1274** corresponding to the SQL value input. Mostly this means putting
1275** double-quotes around strings and returning the unquoted string "null"
1276** when given a NULL input.
1277*/
1278static void jsonQuoteFunc(
1279 sqlite3_context *ctx,
1280 int argc,
1281 sqlite3_value **argv
1282){
1283 JsonString jx;
drhb0df5402016-08-01 17:06:44 +00001284 UNUSED_PARAM(argc);
drh2ad96f52016-06-17 13:01:51 +00001285
1286 jsonInit(&jx, ctx);
1287 jsonAppendValue(&jx, argv[0]);
1288 jsonResult(&jx);
1289 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1290}
1291
1292/*
drh987eb1f2015-08-17 15:17:37 +00001293** Implementation of the json_array(VALUE,...) function. Return a JSON
1294** array that contains all values given in arguments. Or if any argument
1295** is a BLOB, throw an error.
1296*/
1297static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001298 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001299 int argc,
1300 sqlite3_value **argv
1301){
1302 int i;
drh505ad2c2015-08-21 17:33:11 +00001303 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001304
drhbc8f0922015-08-22 19:39:04 +00001305 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001306 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001307 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001308 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001309 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001310 }
drhd0960592015-08-17 21:22:32 +00001311 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001312 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001313 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001314}
1315
1316
1317/*
1318** json_array_length(JSON)
1319** json_array_length(JSON, PATH)
1320**
1321** Return the number of elements in the top-level JSON array.
1322** Return 0 if the input is not a well-formed JSON array.
1323*/
1324static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001325 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001326 int argc,
1327 sqlite3_value **argv
1328){
1329 JsonParse x; /* The parse */
1330 sqlite3_int64 n = 0;
1331 u32 i;
drha8f39a92015-09-21 22:53:16 +00001332 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001333
drhf2df7e72015-08-28 20:07:40 +00001334 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001335 assert( x.nNode );
1336 if( argc==2 ){
1337 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
1338 pNode = jsonLookup(&x, zPath, 0, ctx);
1339 }else{
1340 pNode = x.aNode;
1341 }
1342 if( pNode==0 ){
1343 x.nErr = 1;
1344 }else if( pNode->eType==JSON_ARRAY ){
1345 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1346 for(i=1; i<=pNode->n; n++){
1347 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001348 }
drh987eb1f2015-08-17 15:17:37 +00001349 }
drha7714022015-08-29 00:54:49 +00001350 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001351 jsonParseReset(&x);
1352}
1353
1354/*
drh3ad93bb2015-08-29 19:41:45 +00001355** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001356**
drh3ad93bb2015-08-29 19:41:45 +00001357** Return the element described by PATH. Return NULL if there is no
1358** PATH element. If there are multiple PATHs, then return a JSON array
1359** with the result from each path. Throw an error if the JSON or any PATH
1360** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001361*/
1362static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001363 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001364 int argc,
1365 sqlite3_value **argv
1366){
1367 JsonParse x; /* The parse */
1368 JsonNode *pNode;
1369 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001370 JsonString jx;
1371 int i;
1372
1373 if( argc<2 ) return;
drhbc8f0922015-08-22 19:39:04 +00001374 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh3ad93bb2015-08-29 19:41:45 +00001375 jsonInit(&jx, ctx);
1376 jsonAppendChar(&jx, '[');
1377 for(i=1; i<argc; i++){
1378 zPath = (const char*)sqlite3_value_text(argv[i]);
drhf5ddb9c2015-09-11 00:06:41 +00001379 pNode = jsonLookup(&x, zPath, 0, ctx);
drh3ad93bb2015-08-29 19:41:45 +00001380 if( x.nErr ) break;
1381 if( argc>2 ){
1382 jsonAppendSeparator(&jx);
1383 if( pNode ){
1384 jsonRenderNode(pNode, &jx, 0);
1385 }else{
1386 jsonAppendRaw(&jx, "null", 4);
1387 }
1388 }else if( pNode ){
1389 jsonReturn(pNode, ctx, 0);
1390 }
drh987eb1f2015-08-17 15:17:37 +00001391 }
drh3ad93bb2015-08-29 19:41:45 +00001392 if( argc>2 && i==argc ){
1393 jsonAppendChar(&jx, ']');
1394 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001395 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001396 }
1397 jsonReset(&jx);
drh505ad2c2015-08-21 17:33:11 +00001398 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001399}
1400
drh633647a2017-03-22 21:24:31 +00001401/* This is the RFC 7396 MergePatch algorithm.
1402*/
1403static JsonNode *jsonMergePatch(
1404 JsonParse *pParse, /* The JSON parser that contains the TARGET */
1405 int iTarget, /* Node of the TARGET in pParse */
1406 JsonNode *pPatch /* The PATCH */
1407){
drh0002d242017-03-23 00:46:15 +00001408 u32 i, j;
1409 u32 iRoot;
drh633647a2017-03-22 21:24:31 +00001410 JsonNode *pTarget;
1411 if( pPatch->eType!=JSON_OBJECT ){
1412 return pPatch;
1413 }
1414 assert( iTarget>=0 && iTarget<pParse->nNode );
1415 pTarget = &pParse->aNode[iTarget];
1416 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1417 if( pTarget->eType!=JSON_OBJECT ){
drh29c99692017-03-24 12:35:17 +00001418 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001419 return pPatch;
1420 }
drhbb7aa2d2017-03-23 00:13:52 +00001421 iRoot = iTarget;
drh633647a2017-03-22 21:24:31 +00001422 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
drhba7cce32017-03-24 19:45:05 +00001423 u32 nKey;
drh633647a2017-03-22 21:24:31 +00001424 const char *zKey;
1425 assert( pPatch[i].eType==JSON_STRING );
1426 assert( pPatch[i].jnFlags & JNODE_LABEL );
1427 nKey = pPatch[i].n;
1428 zKey = pPatch[i].u.zJContent;
drhbb7aa2d2017-03-23 00:13:52 +00001429 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh633647a2017-03-22 21:24:31 +00001430 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1431 assert( pTarget[j].eType==JSON_STRING );
1432 assert( pTarget[j].jnFlags & JNODE_LABEL );
drhbb7aa2d2017-03-23 00:13:52 +00001433 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh1fe162f2017-03-23 12:56:44 +00001434 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1435 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
drh633647a2017-03-22 21:24:31 +00001436 if( pPatch[i+1].eType==JSON_NULL ){
1437 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1438 }else{
drhbb7aa2d2017-03-23 00:13:52 +00001439 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
drh633647a2017-03-22 21:24:31 +00001440 if( pNew==0 ) return 0;
1441 pTarget = &pParse->aNode[iTarget];
1442 if( pNew!=&pTarget[j+1] ){
1443 pTarget[j+1].u.pPatch = pNew;
1444 pTarget[j+1].jnFlags |= JNODE_PATCH;
1445 }
1446 }
1447 break;
1448 }
1449 }
drhbb7aa2d2017-03-23 00:13:52 +00001450 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
drh633647a2017-03-22 21:24:31 +00001451 int iStart, iPatch;
1452 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1453 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1454 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1455 if( pParse->oom ) return 0;
drh29c99692017-03-24 12:35:17 +00001456 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001457 pTarget = &pParse->aNode[iTarget];
drhbb7aa2d2017-03-23 00:13:52 +00001458 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
1459 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
1460 iRoot = iStart;
drh633647a2017-03-22 21:24:31 +00001461 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
1462 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
1463 }
1464 }
1465 return pTarget;
1466}
1467
1468/*
1469** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1470** object that is the result of running the RFC 7396 MergePatch() algorithm
1471** on the two arguments.
1472*/
drh37f03df2017-03-23 20:33:49 +00001473static void jsonPatchFunc(
drh633647a2017-03-22 21:24:31 +00001474 sqlite3_context *ctx,
1475 int argc,
1476 sqlite3_value **argv
1477){
1478 JsonParse x; /* The JSON that is being patched */
1479 JsonParse y; /* The patch */
drhbb7aa2d2017-03-23 00:13:52 +00001480 JsonNode *pResult; /* The result of the merge */
drh633647a2017-03-22 21:24:31 +00001481
drh2fb79e92017-03-25 12:08:11 +00001482 UNUSED_PARAM(argc);
drh633647a2017-03-22 21:24:31 +00001483 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1484 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
1485 jsonParseReset(&x);
1486 return;
1487 }
drhbb7aa2d2017-03-23 00:13:52 +00001488 pResult = jsonMergePatch(&x, 0, y.aNode);
1489 assert( pResult!=0 || x.oom );
1490 if( pResult ){
1491 jsonReturnJson(pResult, ctx, 0);
1492 }else{
1493 sqlite3_result_error_nomem(ctx);
1494 }
drh633647a2017-03-22 21:24:31 +00001495 jsonParseReset(&x);
1496 jsonParseReset(&y);
1497}
1498
1499
drh987eb1f2015-08-17 15:17:37 +00001500/*
1501** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1502** object that contains all name/value given in arguments. Or if any name
1503** is not a string or if any value is a BLOB, throw an error.
1504*/
1505static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001506 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001507 int argc,
1508 sqlite3_value **argv
1509){
1510 int i;
drh505ad2c2015-08-21 17:33:11 +00001511 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001512 const char *z;
1513 u32 n;
1514
1515 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001516 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001517 "of arguments", -1);
1518 return;
1519 }
drhbc8f0922015-08-22 19:39:04 +00001520 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001521 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001522 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001523 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001524 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001525 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001526 return;
1527 }
drhd0960592015-08-17 21:22:32 +00001528 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001529 z = (const char*)sqlite3_value_text(argv[i]);
1530 n = (u32)sqlite3_value_bytes(argv[i]);
1531 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001532 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001533 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001534 }
drhd0960592015-08-17 21:22:32 +00001535 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001536 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001537 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001538}
1539
1540
1541/*
drh301eecc2015-08-17 20:14:19 +00001542** json_remove(JSON, PATH, ...)
1543**
drh3ad93bb2015-08-29 19:41:45 +00001544** Remove the named elements from JSON and return the result. malformed
1545** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001546*/
1547static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001548 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001549 int argc,
1550 sqlite3_value **argv
1551){
1552 JsonParse x; /* The parse */
1553 JsonNode *pNode;
1554 const char *zPath;
1555 u32 i;
1556
1557 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001558 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001559 assert( x.nNode );
1560 for(i=1; i<(u32)argc; i++){
1561 zPath = (const char*)sqlite3_value_text(argv[i]);
1562 if( zPath==0 ) goto remove_done;
1563 pNode = jsonLookup(&x, zPath, 0, ctx);
1564 if( x.nErr ) goto remove_done;
1565 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1566 }
1567 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1568 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001569 }
drha7714022015-08-29 00:54:49 +00001570remove_done:
drh505ad2c2015-08-21 17:33:11 +00001571 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001572}
1573
1574/*
1575** json_replace(JSON, PATH, VALUE, ...)
1576**
1577** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001578** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001579*/
1580static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001581 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001582 int argc,
1583 sqlite3_value **argv
1584){
1585 JsonParse x; /* The parse */
1586 JsonNode *pNode;
1587 const char *zPath;
1588 u32 i;
1589
1590 if( argc<1 ) return;
1591 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001592 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001593 return;
1594 }
drhbc8f0922015-08-22 19:39:04 +00001595 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001596 assert( x.nNode );
1597 for(i=1; i<(u32)argc; i+=2){
1598 zPath = (const char*)sqlite3_value_text(argv[i]);
1599 pNode = jsonLookup(&x, zPath, 0, ctx);
1600 if( x.nErr ) goto replace_err;
1601 if( pNode ){
1602 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001603 pNode->u.iReplace = i + 1;
drhd0960592015-08-17 21:22:32 +00001604 }
drha8f39a92015-09-21 22:53:16 +00001605 }
1606 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh633647a2017-03-22 21:24:31 +00001607 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001608 }else{
1609 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001610 }
drha7714022015-08-29 00:54:49 +00001611replace_err:
drh505ad2c2015-08-21 17:33:11 +00001612 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001613}
drh505ad2c2015-08-21 17:33:11 +00001614
drh52216ad2015-08-18 02:28:03 +00001615/*
1616** json_set(JSON, PATH, VALUE, ...)
1617**
1618** Set the value at PATH to VALUE. Create the PATH if it does not already
1619** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001620** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001621**
1622** json_insert(JSON, PATH, VALUE, ...)
1623**
1624** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001625** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001626*/
1627static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001628 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001629 int argc,
1630 sqlite3_value **argv
1631){
1632 JsonParse x; /* The parse */
1633 JsonNode *pNode;
1634 const char *zPath;
1635 u32 i;
1636 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001637 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001638
1639 if( argc<1 ) return;
1640 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001641 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001642 return;
1643 }
drhbc8f0922015-08-22 19:39:04 +00001644 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001645 assert( x.nNode );
1646 for(i=1; i<(u32)argc; i+=2){
1647 zPath = (const char*)sqlite3_value_text(argv[i]);
1648 bApnd = 0;
1649 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1650 if( x.oom ){
1651 sqlite3_result_error_nomem(ctx);
1652 goto jsonSetDone;
1653 }else if( x.nErr ){
1654 goto jsonSetDone;
1655 }else if( pNode && (bApnd || bIsSet) ){
1656 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001657 pNode->u.iReplace = i + 1;
drh52216ad2015-08-18 02:28:03 +00001658 }
drha8f39a92015-09-21 22:53:16 +00001659 }
1660 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh633647a2017-03-22 21:24:31 +00001661 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001662 }else{
1663 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001664 }
drhbc8f0922015-08-22 19:39:04 +00001665jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001666 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001667}
drh301eecc2015-08-17 20:14:19 +00001668
1669/*
drh987eb1f2015-08-17 15:17:37 +00001670** json_type(JSON)
1671** json_type(JSON, PATH)
1672**
drh3ad93bb2015-08-29 19:41:45 +00001673** Return the top-level "type" of a JSON string. Throw an error if
1674** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001675*/
1676static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001677 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001678 int argc,
1679 sqlite3_value **argv
1680){
1681 JsonParse x; /* The parse */
1682 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001683 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001684
drhbc8f0922015-08-22 19:39:04 +00001685 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001686 assert( x.nNode );
1687 if( argc==2 ){
1688 zPath = (const char*)sqlite3_value_text(argv[1]);
1689 pNode = jsonLookup(&x, zPath, 0, ctx);
1690 }else{
1691 pNode = x.aNode;
1692 }
1693 if( pNode ){
1694 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001695 }
drh505ad2c2015-08-21 17:33:11 +00001696 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001697}
drh5634cc02015-08-17 11:28:03 +00001698
drhbc8f0922015-08-22 19:39:04 +00001699/*
1700** json_valid(JSON)
1701**
drh3ad93bb2015-08-29 19:41:45 +00001702** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1703** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001704*/
1705static void jsonValidFunc(
1706 sqlite3_context *ctx,
1707 int argc,
1708 sqlite3_value **argv
1709){
1710 JsonParse x; /* The parse */
1711 int rc = 0;
1712
mistachkin16a93122015-09-11 18:05:01 +00001713 UNUSED_PARAM(argc);
drha8f39a92015-09-21 22:53:16 +00001714 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
drhbc8f0922015-08-22 19:39:04 +00001715 rc = 1;
1716 }
1717 jsonParseReset(&x);
1718 sqlite3_result_int(ctx, rc);
1719}
1720
drhff135ae2015-12-30 01:07:02 +00001721
1722/****************************************************************************
1723** Aggregate SQL function implementations
1724****************************************************************************/
1725/*
1726** json_group_array(VALUE)
1727**
1728** Return a JSON array composed of all values in the aggregate.
1729*/
1730static void jsonArrayStep(
1731 sqlite3_context *ctx,
1732 int argc,
1733 sqlite3_value **argv
1734){
1735 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00001736 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001737 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1738 if( pStr ){
1739 if( pStr->zBuf==0 ){
1740 jsonInit(pStr, ctx);
1741 jsonAppendChar(pStr, '[');
1742 }else{
1743 jsonAppendChar(pStr, ',');
1744 pStr->pCtx = ctx;
1745 }
1746 jsonAppendValue(pStr, argv[0]);
1747 }
1748}
1749static void jsonArrayFinal(sqlite3_context *ctx){
1750 JsonString *pStr;
1751 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1752 if( pStr ){
1753 pStr->pCtx = ctx;
1754 jsonAppendChar(pStr, ']');
1755 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001756 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001757 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001758 }else{
1759 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1760 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1761 pStr->bStatic = 1;
1762 }
1763 }else{
1764 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
1765 }
1766 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1767}
1768
1769/*
1770** json_group_obj(NAME,VALUE)
1771**
1772** Return a JSON object composed of all names and values in the aggregate.
1773*/
1774static void jsonObjectStep(
1775 sqlite3_context *ctx,
1776 int argc,
1777 sqlite3_value **argv
1778){
1779 JsonString *pStr;
1780 const char *z;
1781 u32 n;
drhdf3a9072016-02-11 15:37:18 +00001782 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001783 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1784 if( pStr ){
1785 if( pStr->zBuf==0 ){
1786 jsonInit(pStr, ctx);
1787 jsonAppendChar(pStr, '{');
1788 }else{
1789 jsonAppendChar(pStr, ',');
1790 pStr->pCtx = ctx;
1791 }
1792 z = (const char*)sqlite3_value_text(argv[0]);
1793 n = (u32)sqlite3_value_bytes(argv[0]);
1794 jsonAppendString(pStr, z, n);
1795 jsonAppendChar(pStr, ':');
1796 jsonAppendValue(pStr, argv[1]);
1797 }
1798}
1799static void jsonObjectFinal(sqlite3_context *ctx){
1800 JsonString *pStr;
1801 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1802 if( pStr ){
1803 jsonAppendChar(pStr, '}');
1804 if( pStr->bErr ){
drh6850a632016-11-07 18:18:08 +00001805 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001806 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001807 }else{
1808 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1809 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1810 pStr->bStatic = 1;
1811 }
1812 }else{
1813 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
1814 }
1815 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1816}
1817
1818
drhd2975922015-08-29 17:22:33 +00001819#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00001820/****************************************************************************
1821** The json_each virtual table
1822****************************************************************************/
1823typedef struct JsonEachCursor JsonEachCursor;
1824struct JsonEachCursor {
1825 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001826 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00001827 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00001828 u32 i; /* Index in sParse.aNode[] of current row */
1829 u32 iEnd; /* EOF when i equals or exceeds this value */
1830 u8 eType; /* Type of top-level element */
1831 u8 bRecursive; /* True for json_tree(). False for json_each() */
1832 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00001833 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00001834 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001835};
1836
1837/* Constructor for the json_each virtual table */
1838static int jsonEachConnect(
1839 sqlite3 *db,
1840 void *pAux,
1841 int argc, const char *const*argv,
1842 sqlite3_vtab **ppVtab,
1843 char **pzErr
1844){
1845 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001846 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001847
1848/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001849#define JEACH_KEY 0
1850#define JEACH_VALUE 1
1851#define JEACH_TYPE 2
1852#define JEACH_ATOM 3
1853#define JEACH_ID 4
1854#define JEACH_PARENT 5
1855#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00001856#define JEACH_PATH 7
1857#define JEACH_JSON 8
1858#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00001859
drh6fd5c1e2015-08-21 20:37:12 +00001860 UNUSED_PARAM(pzErr);
1861 UNUSED_PARAM(argv);
1862 UNUSED_PARAM(argc);
1863 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001864 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00001865 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
1866 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001867 if( rc==SQLITE_OK ){
1868 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1869 if( pNew==0 ) return SQLITE_NOMEM;
1870 memset(pNew, 0, sizeof(*pNew));
1871 }
1872 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001873}
1874
1875/* destructor for json_each virtual table */
1876static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1877 sqlite3_free(pVtab);
1878 return SQLITE_OK;
1879}
1880
drh505ad2c2015-08-21 17:33:11 +00001881/* constructor for a JsonEachCursor object for json_each(). */
1882static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001883 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001884
1885 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001886 pCur = sqlite3_malloc( sizeof(*pCur) );
1887 if( pCur==0 ) return SQLITE_NOMEM;
1888 memset(pCur, 0, sizeof(*pCur));
1889 *ppCursor = &pCur->base;
1890 return SQLITE_OK;
1891}
1892
drh505ad2c2015-08-21 17:33:11 +00001893/* constructor for a JsonEachCursor object for json_tree(). */
1894static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1895 int rc = jsonEachOpenEach(p, ppCursor);
1896 if( rc==SQLITE_OK ){
1897 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1898 pCur->bRecursive = 1;
1899 }
1900 return rc;
1901}
1902
drhcb6c6c62015-08-19 22:47:17 +00001903/* Reset a JsonEachCursor back to its original state. Free any memory
1904** held. */
1905static void jsonEachCursorReset(JsonEachCursor *p){
1906 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00001907 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00001908 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001909 p->iRowid = 0;
1910 p->i = 0;
1911 p->iEnd = 0;
1912 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001913 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00001914 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001915}
1916
1917/* Destructor for a jsonEachCursor object */
1918static int jsonEachClose(sqlite3_vtab_cursor *cur){
1919 JsonEachCursor *p = (JsonEachCursor*)cur;
1920 jsonEachCursorReset(p);
1921 sqlite3_free(cur);
1922 return SQLITE_OK;
1923}
1924
1925/* Return TRUE if the jsonEachCursor object has been advanced off the end
1926** of the JSON object */
1927static int jsonEachEof(sqlite3_vtab_cursor *cur){
1928 JsonEachCursor *p = (JsonEachCursor*)cur;
1929 return p->i >= p->iEnd;
1930}
1931
drh505ad2c2015-08-21 17:33:11 +00001932/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001933static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001934 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001935 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00001936 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
1937 p->i++;
drh4af352d2015-08-21 20:02:48 +00001938 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00001939 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00001940 u32 iUp = p->sParse.aUp[p->i];
1941 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001942 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001943 if( pUp->eType==JSON_ARRAY ){
1944 if( iUp==p->i-1 ){
1945 pUp->u.iKey = 0;
1946 }else{
1947 pUp->u.iKey++;
1948 }
drh4af352d2015-08-21 20:02:48 +00001949 }
1950 }
drh505ad2c2015-08-21 17:33:11 +00001951 }else{
drh4af352d2015-08-21 20:02:48 +00001952 switch( p->eType ){
1953 case JSON_ARRAY: {
1954 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1955 p->iRowid++;
1956 break;
1957 }
1958 case JSON_OBJECT: {
1959 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1960 p->iRowid++;
1961 break;
1962 }
1963 default: {
1964 p->i = p->iEnd;
1965 break;
1966 }
drh505ad2c2015-08-21 17:33:11 +00001967 }
1968 }
1969 return SQLITE_OK;
1970}
1971
drh4af352d2015-08-21 20:02:48 +00001972/* Append the name of the path for element i to pStr
1973*/
1974static void jsonEachComputePath(
1975 JsonEachCursor *p, /* The cursor */
1976 JsonString *pStr, /* Write the path here */
1977 u32 i /* Path to this element */
1978){
1979 JsonNode *pNode, *pUp;
1980 u32 iUp;
1981 if( i==0 ){
1982 jsonAppendChar(pStr, '$');
1983 return;
drhcb6c6c62015-08-19 22:47:17 +00001984 }
drh4af352d2015-08-21 20:02:48 +00001985 iUp = p->sParse.aUp[i];
1986 jsonEachComputePath(p, pStr, iUp);
1987 pNode = &p->sParse.aNode[i];
1988 pUp = &p->sParse.aNode[iUp];
1989 if( pUp->eType==JSON_ARRAY ){
1990 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1991 }else{
1992 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00001993 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00001994 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00001995 assert( pNode->jnFlags & JNODE_LABEL );
drh4af352d2015-08-21 20:02:48 +00001996 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1997 }
drhcb6c6c62015-08-19 22:47:17 +00001998}
1999
2000/* Return the value of a column */
2001static int jsonEachColumn(
2002 sqlite3_vtab_cursor *cur, /* The cursor */
2003 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2004 int i /* Which column to return */
2005){
2006 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00002007 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00002008 switch( i ){
2009 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00002010 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00002011 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00002012 jsonReturn(pThis, ctx, 0);
2013 }else if( p->eType==JSON_ARRAY ){
2014 u32 iKey;
2015 if( p->bRecursive ){
2016 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00002017 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00002018 }else{
2019 iKey = p->iRowid;
2020 }
drh6fd5c1e2015-08-21 20:37:12 +00002021 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00002022 }
2023 break;
2024 }
2025 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00002026 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002027 jsonReturn(pThis, ctx, 0);
2028 break;
2029 }
2030 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00002031 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002032 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2033 break;
2034 }
2035 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00002036 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002037 if( pThis->eType>=JSON_ARRAY ) break;
2038 jsonReturn(pThis, ctx, 0);
2039 break;
2040 }
2041 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00002042 sqlite3_result_int64(ctx,
2043 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00002044 break;
2045 }
2046 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00002047 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00002048 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00002049 }
2050 break;
2051 }
drh4af352d2015-08-21 20:02:48 +00002052 case JEACH_FULLKEY: {
2053 JsonString x;
2054 jsonInit(&x, ctx);
2055 if( p->bRecursive ){
2056 jsonEachComputePath(p, &x, p->i);
2057 }else{
drh383de692015-09-10 17:20:57 +00002058 if( p->zRoot ){
2059 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00002060 }else{
2061 jsonAppendChar(&x, '$');
2062 }
2063 if( p->eType==JSON_ARRAY ){
2064 jsonPrintf(30, &x, "[%d]", p->iRowid);
2065 }else{
2066 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2067 }
2068 }
2069 jsonResult(&x);
2070 break;
2071 }
drhcb6c6c62015-08-19 22:47:17 +00002072 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00002073 if( p->bRecursive ){
2074 JsonString x;
2075 jsonInit(&x, ctx);
2076 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2077 jsonResult(&x);
2078 break;
drh4af352d2015-08-21 20:02:48 +00002079 }
drh383de692015-09-10 17:20:57 +00002080 /* For json_each() path and root are the same so fall through
2081 ** into the root case */
2082 }
drh6850a632016-11-07 18:18:08 +00002083 default: {
drh383de692015-09-10 17:20:57 +00002084 const char *zRoot = p->zRoot;
drh6850a632016-11-07 18:18:08 +00002085 if( zRoot==0 ) zRoot = "$";
drh383de692015-09-10 17:20:57 +00002086 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00002087 break;
2088 }
drh3d1d2a92015-09-22 01:15:49 +00002089 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00002090 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00002091 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
2092 break;
2093 }
2094 }
2095 return SQLITE_OK;
2096}
2097
2098/* Return the current rowid value */
2099static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2100 JsonEachCursor *p = (JsonEachCursor*)cur;
2101 *pRowid = p->iRowid;
2102 return SQLITE_OK;
2103}
2104
2105/* The query strategy is to look for an equality constraint on the json
2106** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00002107** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00002108** and 0 otherwise.
2109*/
2110static int jsonEachBestIndex(
2111 sqlite3_vtab *tab,
2112 sqlite3_index_info *pIdxInfo
2113){
2114 int i;
2115 int jsonIdx = -1;
drh383de692015-09-10 17:20:57 +00002116 int rootIdx = -1;
drhcb6c6c62015-08-19 22:47:17 +00002117 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00002118
2119 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00002120 pConstraint = pIdxInfo->aConstraint;
2121 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2122 if( pConstraint->usable==0 ) continue;
2123 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2124 switch( pConstraint->iColumn ){
2125 case JEACH_JSON: jsonIdx = i; break;
drh383de692015-09-10 17:20:57 +00002126 case JEACH_ROOT: rootIdx = i; break;
drhcb6c6c62015-08-19 22:47:17 +00002127 default: /* no-op */ break;
2128 }
2129 }
2130 if( jsonIdx<0 ){
2131 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00002132 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00002133 }else{
drh505ad2c2015-08-21 17:33:11 +00002134 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00002135 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
2136 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
drh383de692015-09-10 17:20:57 +00002137 if( rootIdx<0 ){
drhcb6c6c62015-08-19 22:47:17 +00002138 pIdxInfo->idxNum = 1;
2139 }else{
drh383de692015-09-10 17:20:57 +00002140 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
2141 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
drhcb6c6c62015-08-19 22:47:17 +00002142 pIdxInfo->idxNum = 3;
2143 }
2144 }
2145 return SQLITE_OK;
2146}
2147
2148/* Start a search on a new JSON string */
2149static int jsonEachFilter(
2150 sqlite3_vtab_cursor *cur,
2151 int idxNum, const char *idxStr,
2152 int argc, sqlite3_value **argv
2153){
2154 JsonEachCursor *p = (JsonEachCursor*)cur;
2155 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00002156 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002157 sqlite3_int64 n;
2158
drh6fd5c1e2015-08-21 20:37:12 +00002159 UNUSED_PARAM(idxStr);
2160 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002161 jsonEachCursorReset(p);
2162 if( idxNum==0 ) return SQLITE_OK;
2163 z = (const char*)sqlite3_value_text(argv[0]);
2164 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002165 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002166 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002167 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002168 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002169 if( jsonParse(&p->sParse, 0, p->zJson) ){
2170 int rc = SQLITE_NOMEM;
2171 if( p->sParse.oom==0 ){
2172 sqlite3_free(cur->pVtab->zErrMsg);
2173 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2174 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2175 }
drhcb6c6c62015-08-19 22:47:17 +00002176 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002177 return rc;
2178 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2179 jsonEachCursorReset(p);
2180 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002181 }else{
drh95677942015-09-24 01:06:37 +00002182 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002183 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002184 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002185 zRoot = (const char*)sqlite3_value_text(argv[1]);
2186 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002187 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002188 p->zRoot = sqlite3_malloc64( n+1 );
2189 if( p->zRoot==0 ) return SQLITE_NOMEM;
2190 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002191 if( zRoot[0]!='$' ){
2192 zErr = zRoot;
2193 }else{
2194 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2195 }
2196 if( zErr ){
drha7714022015-08-29 00:54:49 +00002197 sqlite3_free(cur->pVtab->zErrMsg);
2198 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002199 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002200 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2201 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002202 return SQLITE_OK;
2203 }
2204 }else{
2205 pNode = p->sParse.aNode;
2206 }
drh852944e2015-09-10 03:29:11 +00002207 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002208 p->eType = pNode->eType;
2209 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00002210 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002211 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002212 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002213 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002214 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2215 p->i--;
2216 }
2217 }else{
2218 p->i++;
2219 }
drhcb6c6c62015-08-19 22:47:17 +00002220 }else{
2221 p->iEnd = p->i+1;
2222 }
2223 }
drha8f39a92015-09-21 22:53:16 +00002224 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002225}
2226
2227/* The methods of the json_each virtual table */
2228static sqlite3_module jsonEachModule = {
2229 0, /* iVersion */
2230 0, /* xCreate */
2231 jsonEachConnect, /* xConnect */
2232 jsonEachBestIndex, /* xBestIndex */
2233 jsonEachDisconnect, /* xDisconnect */
2234 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002235 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002236 jsonEachClose, /* xClose - close a cursor */
2237 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002238 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002239 jsonEachEof, /* xEof - check for end of scan */
2240 jsonEachColumn, /* xColumn - read data */
2241 jsonEachRowid, /* xRowid - read data */
2242 0, /* xUpdate */
2243 0, /* xBegin */
2244 0, /* xSync */
2245 0, /* xCommit */
2246 0, /* xRollback */
2247 0, /* xFindMethod */
2248 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002249 0, /* xSavepoint */
2250 0, /* xRelease */
2251 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00002252};
2253
drh505ad2c2015-08-21 17:33:11 +00002254/* The methods of the json_tree virtual table. */
2255static sqlite3_module jsonTreeModule = {
2256 0, /* iVersion */
2257 0, /* xCreate */
2258 jsonEachConnect, /* xConnect */
2259 jsonEachBestIndex, /* xBestIndex */
2260 jsonEachDisconnect, /* xDisconnect */
2261 0, /* xDestroy */
2262 jsonEachOpenTree, /* xOpen - open a cursor */
2263 jsonEachClose, /* xClose - close a cursor */
2264 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002265 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002266 jsonEachEof, /* xEof - check for end of scan */
2267 jsonEachColumn, /* xColumn - read data */
2268 jsonEachRowid, /* xRowid - read data */
2269 0, /* xUpdate */
2270 0, /* xBegin */
2271 0, /* xSync */
2272 0, /* xCommit */
2273 0, /* xRollback */
2274 0, /* xFindMethod */
2275 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002276 0, /* xSavepoint */
2277 0, /* xRelease */
2278 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00002279};
drhd2975922015-08-29 17:22:33 +00002280#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002281
2282/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002283** The following routines are the only publically visible identifiers in this
2284** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002285** functions and the virtual table implemented by this file.
2286****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002287
drh2f20e132015-09-26 17:44:59 +00002288int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002289 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002290 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002291 static const struct {
2292 const char *zName;
2293 int nArg;
drh52216ad2015-08-18 02:28:03 +00002294 int flag;
drh5fa5c102015-08-12 16:49:40 +00002295 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2296 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002297 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002298 { "json_array", -1, 0, jsonArrayFunc },
2299 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2300 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002301 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002302 { "json_insert", -1, 0, jsonSetFunc },
2303 { "json_object", -1, 0, jsonObjectFunc },
drh37f03df2017-03-23 20:33:49 +00002304 { "json_patch", 2, 0, jsonPatchFunc },
drh2ad96f52016-06-17 13:01:51 +00002305 { "json_quote", 1, 0, jsonQuoteFunc },
drh52216ad2015-08-18 02:28:03 +00002306 { "json_remove", -1, 0, jsonRemoveFunc },
2307 { "json_replace", -1, 0, jsonReplaceFunc },
2308 { "json_set", -1, 1, jsonSetFunc },
2309 { "json_type", 1, 0, jsonTypeFunc },
2310 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002311 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002312
drh301eecc2015-08-17 20:14:19 +00002313#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002314 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002315 { "json_parse", 1, 0, jsonParseFunc },
2316 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002317#endif
drh5fa5c102015-08-12 16:49:40 +00002318 };
drhff135ae2015-12-30 01:07:02 +00002319 static const struct {
2320 const char *zName;
2321 int nArg;
2322 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2323 void (*xFinal)(sqlite3_context*);
2324 } aAgg[] = {
2325 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal },
2326 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal },
2327 };
drhd2975922015-08-29 17:22:33 +00002328#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002329 static const struct {
2330 const char *zName;
2331 sqlite3_module *pModule;
2332 } aMod[] = {
2333 { "json_each", &jsonEachModule },
2334 { "json_tree", &jsonTreeModule },
2335 };
drhd2975922015-08-29 17:22:33 +00002336#endif
drh5fa5c102015-08-12 16:49:40 +00002337 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
2338 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00002339 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
2340 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00002341 aFunc[i].xFunc, 0, 0);
2342 }
drhff135ae2015-12-30 01:07:02 +00002343 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
2344 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
2345 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
2346 0, aAgg[i].xStep, aAgg[i].xFinal);
2347 }
drhd2975922015-08-29 17:22:33 +00002348#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002349 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2350 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002351 }
drhd2975922015-08-29 17:22:33 +00002352#endif
drh5fa5c102015-08-12 16:49:40 +00002353 return rc;
2354}
drh2f20e132015-09-26 17:44:59 +00002355
2356
dan8d32e802015-10-14 18:45:42 +00002357#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002358#ifdef _WIN32
2359__declspec(dllexport)
2360#endif
2361int sqlite3_json_init(
2362 sqlite3 *db,
2363 char **pzErrMsg,
2364 const sqlite3_api_routines *pApi
2365){
2366 SQLITE_EXTENSION_INIT2(pApi);
2367 (void)pzErrMsg; /* Unused parameter */
2368 return sqlite3Json1Init(db);
2369}
dan8d32e802015-10-14 18:45:42 +00002370#endif
drh50065652015-10-08 19:29:18 +00002371#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */