· 4 years ago · Jul 05, 2021, 03:08 PM
1/*Author disclaims copyright to this source code. In place of
2** a legal notice, here is a blessing:
3**
4** May you do good and not evil.
5** May you find forgiveness for yourself and forgive others.
6** May you share freely, never taking more than you give.
7**
8******************************************************************************
9**
10** This SQLite extension implements JSON functions. The interface is
11** modeled after MySQL JSON functions:
12**
13** https://dev.mysql.com/doc/refman/5.7/en/json.html
14**
15** For the time being, all JSON is stored as pure text. (We might add
16** a JSONB type in the future which stores a binary encoding of JSON in
17** a BLOB, but there is no support for JSONB in the current implementation.
18** This implementation parses JSON text at 250 MB/s, so it is hard to see
19** how JSONB might improve on that.)
20
21
22 Changes to do
23 1)in object_store.cc add json extension(927-930)
24 2)Modify RunSelectQuery function in object_select_op.cc
25 3)modify sqlite/CMakelists
26 4)modify poseidon/test/CMakeLists.txt
27 5)modify poseidon/test/poseidon_manager.cc
28 6)modify test files
29*/
30#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
31#if !defined(SQLITEINT_H)
32#include "sqlite3ext.h"
33#endif
34SQLITE_EXTENSION_INIT1
35#include <assert.h>
36//new code
37#include "poseidon/object_store/object_store_c.h"
38//new code
39#include <string.h>
40#include <stdlib.h>
41#include <stdarg.h>
42#include<stddef.h>
43#include<stdbool.h>
44#include <stdio.h>
45
46/* Mark a function parameter as unused, to suppress nuisance compiler
47** warnings. */
48#ifndef UNUSED_PARAM
49# define UNUSED_PARAM(X) (void)(X)
50#endif
51
52#ifndef LARGEST_INT64
53# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
54# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
55#endif
56
57#ifndef deliberate_fall_through
58# define deliberate_fall_through
59#endif
60
61/*
62** Versions of isspace(), isalnum() and isdigit() to which it is safe
63** to pass signed char values.
64*/
65#ifdef sqlite3Isdigit
66 /* Use the SQLite core versions if this routine is part of the
67 ** SQLite amalgamation */
68# define safe_isdigit(x) sqlite3Isdigit(x)
69# define safe_isalnum(x) sqlite3Isalnum(x)
70# define safe_isxdigit(x) sqlite3Isxdigit(x)
71#else
72 /* Use the standard library for separate compilation */
73#include <ctype.h> /* amalgamator: keep */
74# define safe_isdigit(x) isdigit((unsigned char)(x))
75# define safe_isalnum(x) isalnum((unsigned char)(x))
76# define safe_isxdigit(x) isxdigit((unsigned char)(x))
77#endif
78
79/*
80** Growing our own isspace() routine this way is twice as fast as
81** the library isspace() function, resulting in a 7% overall performance
82** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
83*/
84static const char jsonIsSpace[] = {
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101};
102#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
103
104#ifndef SQLITE_AMALGAMATION
105 /* Unsigned integer types. These are already defined in the sqliteInt.h,
106 ** but the definitions need to be repeated for separate compilation. */
107 typedef sqlite3_uint64 u64;
108 typedef unsigned int u32;
109 typedef unsigned short int u16;
110 typedef unsigned char u8;
111#endif
112
113/* Objects */
114typedef struct JsonString JsonString;
115typedef struct JsonNode JsonNode;
116typedef struct JsonParse JsonParse;
117
118/* An instance of this object represents a JSON string
119** under construction. Really, this is a generic string accumulator
120** that can be and is used to create strings other than JSON.
121*/
122struct JsonString {
123 sqlite3_context *pCtx; /* Function context - put error messages here */
124 char *zBuf; /* Append JSON content here */
125 u64 nAlloc; /* Bytes of storage available in zBuf[] */
126 u64 nUsed; /* Bytes of zBuf[] currently used */
127 u8 bStatic; /* True if zBuf is static space */
128 u8 bErr; /* True if an error has been encountered */
129 char zSpace[100]; /* Initial static space */
130};
131//new code
132/*
133** A macro to hint to the compiler that a function should not be
134** inlined.
135*/
136#if defined(__GNUC__)
137# define JSON_NOINLINE __attribute__((noinline))
138#elif defined(_MSC_VER) && _MSC_VER>=1310
139# define JSON_NOINLINE __declspec(noinline)
140#else
141# define JSON_NOINLINE
142#endif
143#define JSON_MXERR 200
144#define JSON_INBUFSZ 1024
145typedef struct JsonReader JsonReader;
146struct JsonReader {
147 object_handle_t *obj; /* Read the Json text from this input stream */
148 size_t iIn; /* Next unread character in the input buffer */
149 size_t nIn; /* Number of characters in the input buffer */
150 char *zIn; /* The input buffer */
151 size_t offset;
152 char zErr[JSON_MXERR]; /* Error message */
153};
154//new code
155
156/* JSON type values
157*/
158#define JSON_NULL 0
159#define JSON_TRUE 1
160#define JSON_FALSE 2
161#define JSON_INT 3
162#define JSON_REAL 4
163#define JSON_STRING 5
164#define JSON_ARRAY 6
165#define JSON_OBJECT 7
166
167/* The "subtype" set for JSON values */
168#define JSON_SUBTYPE 74 /* Ascii for "J" */
169
170/*
171** Names of the various JSON types:
172*/
173static const char * const jsonType[] = {
174 "null", "true", "false", "integer", "real", "text", "array", "object"
175};
176
177/* Bit values for the JsonNode.jnFlag field
178*/
179#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
180#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
181#define JNODE_REMOVE 0x04 /* Do not output */
182#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
183#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
184#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
185#define JNODE_LABEL 0x40 /* Is a label of an object */
186
187
188/* A single node of parsed JSON
189*/
190struct JsonNode {
191 u8 eType; /* One of the JSON_ type values */
192 u8 jnFlags; /* JNODE flags */
193 u32 n; /* Bytes of content, or number of sub-nodes */
194 union {
195 const char *zJContent; /* Content for INT, REAL, and STRING */
196 u32 iAppend; /* More terms for ARRAY and OBJECT */
197 u32 iKey; /* Key for ARRAY objects in json_tree() */
198 u32 iReplace; /* Replacement content for JNODE_REPLACE */
199 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
200 } u;
201};
202
203/* A completely parsed JSON string
204*/
205struct JsonParse {
206 u32 nNode; /* Number of slots of aNode[] used */
207 u32 nAlloc; /* Number of slots of aNode[] allocated */
208 JsonNode *aNode; /* Array of nodes containing the parse */
209 //const char *zJson; /* Original JSON string */
210 //new code
211 JsonReader *srdr;
212 //new code
213 u32 *aUp; /* Index of parent of each node */
214 u8 oom; /* Set to true if out of memory */
215 u8 nErr; /* Number of errors seen */
216 u16 iDepth; /* Nesting depth */
217 // int nJson; /* Length of the zJson string in bytes */
218 u32 iHold; /* Replace cache line with the lowest iHold value */
219};
220
221/*
222** Maximum nesting depth of JSON for this implementation.
223**
224** This limit is needed to avoid a stack overflow in the recursive
225** descent parser. A depth of 2000 is far deeper than any sane JSON
226** should go.
227*/
228#define JSON_MAX_DEPTH 2000
229
230
231//new code
232static void json_reader_init(JsonReader *p){
233 p->nIn = 0;
234 p->zIn = 0;
235 p->offset = 0;
236 p->zErr[0] = 0;
237}
238
239static void json_reader_reset(JsonReader *p){
240 if (p->obj) {
241 destroy_object_handle(p->obj);
242 sqlite3_free(p->zIn);
243 }
244 json_reader_init(p);
245}
246
247static void json_errmsg(JsonReader *p, const char *zFormat, ...){
248 va_list ap;
249 va_start(ap, zFormat);
250 sqlite3_vsnprintf(JSON_MXERR, p->zErr, zFormat, ap);
251 va_end(ap);
252}
253
254static int json_reader_open(
255 JsonReader *p, /* The reader to open */
256 const char *zBucket, /* Containing bucket */
257 const char *zObject, /* Object to read */
258 const char *zVersion /* Object version to read */
259){
260 assert(zBucket && zObject);
261 p->zIn = sqlite3_malloc( JSON_INBUFSZ +2 );
262 if( p->zIn==0 ){
263 json_errmsg(p, "out of memory");
264 return 1;
265 }
266 p->obj = create_object_handle(zBucket, zObject, zVersion);
267 if( p->obj==0 ){
268 sqlite3_free(p->zIn);
269 json_reader_reset(p);
270 json_errmsg(p, "cannot open '%s\%s' with version=%s for reading",
271 zBucket, zObject, zVersion ? zVersion : "");
272 return 1;
273 }
274 return 0;
275}
276
277static JSON_NOINLINE int json_getc_refill(JsonReader *p){
278 size_t got;
279
280 assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */
281 assert( p->obj);
282 got = read_object(p->obj, p->offset, JSON_INBUFSZ, p->zIn);
283 if( got==0 ) return EOF;
284 p->nIn = got;
285 p->offset += got;
286 p->iIn = 1;
287 return p->zIn[0];
288}
289
290static int json_getc(JsonReader *p){
291 if( p->iIn >= p->nIn ){
292 if( p->obj ) return json_getc_refill(p);
293 return EOF;
294 }
295 return ((unsigned char*)p->zIn)[p->iIn++];
296}
297
298static JSON_NOINLINE int json_getc_refill_next(JsonReader *p){
299 size_t got;
300
301 assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */
302 assert( p->obj);
303 got = read_object(p->obj, p->offset, JSON_INBUFSZ, p->zIn);
304 if( got==0 ) return EOF;
305 p->nIn = got;
306 p->offset += got;
307 p->iIn = 0;
308 return p->zIn[0];
309}
310
311static int json_getc_next(JsonReader *p){
312 if( p->iIn >= p->nIn ){
313 if( p->obj ) return json_getc_refill_next(p);
314 return EOF;
315 }
316 return ((unsigned char*)p->zIn)[p->iIn];
317}
318
319
320//high probability of error
321static JSON_NOINLINE int json_getc_refill_next_next(JsonReader *p){
322 size_t got;
323
324 assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */
325 assert( p->obj);
326 char *temp;
327 temp = sqlite3_malloc( JSON_INBUFSZ );
328 got = read_object(p->obj, p->offset, JSON_INBUFSZ, temp);
329 if( got==0 ) return EOF;
330 char *z;
331 z = sqlite3_malloc( JSON_INBUFSZ + 2);
332 strcpy(z,(p->zIn) + p->iIn);
333 strcat(z,temp);
334 p->zIn = z;
335 p->offset += got;
336 p->nIn = got + 1;
337 p->iIn = 0;
338 return p->zIn[1];
339}
340
341static int json_getc_next_next(JsonReader *p){
342 if( p->iIn +1 >= p->nIn ){
343 if( p->obj ) return json_getc_refill_next_next(p);
344 return EOF;
345 }
346 return ((unsigned char*)p->zIn)[p->iIn+1];
347}
348
349static JSON_NOINLINE int json_getc_refill_next_3(JsonReader *p){
350 size_t got;
351
352 assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */
353 assert( p->obj);
354 char *temp;
355 temp = sqlite3_malloc( JSON_INBUFSZ );
356 got = read_object(p->obj, p->offset, JSON_INBUFSZ, temp);
357 if( got==0 ) return EOF;
358 char *z;
359 z = sqlite3_malloc( JSON_INBUFSZ + 3);
360 strcpy(z,(p->zIn) + p->iIn);
361 strcat(z,temp);
362 p->zIn = z;
363 p->offset += got;
364 p->nIn = got + 2;
365 p->iIn = 0;
366 return p->zIn[2];
367}
368
369static int json_getc_next_3(JsonReader *p){
370 if( p->iIn +2 >= p->nIn ){
371 if( p->obj ) return json_getc_refill_next_3(p);
372 return EOF;
373 }
374 return ((unsigned char*)p->zIn)[p->iIn+2];
375}
376
377static JSON_NOINLINE int json_getc_refill_next_4(JsonReader *p){
378 size_t got;
379
380 assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */
381 assert( p->obj);
382 char *temp;
383 temp = sqlite3_malloc( JSON_INBUFSZ );
384 got = read_object(p->obj, p->offset, JSON_INBUFSZ, temp);
385 if( got==0 ) return EOF;
386 char *z;
387 z = sqlite3_malloc( JSON_INBUFSZ + 4);
388 strcpy(z,(p->zIn) + p->iIn);
389 strcat(z,temp);
390 p->zIn = z;
391 p->offset += got;
392 p->nIn = got + 3;
393 p->iIn = 0;
394 return p->zIn[3];
395}
396
397static int json_getc_next_4(JsonReader *p){
398 if( p->iIn +3 >= p->nIn ){
399 if( p->obj ) return json_getc_refill_next_4(p);
400 return EOF;
401 }
402 return ((unsigned char*)p->zIn)[p->iIn+3];
403}
404//new code
405
406
407/**************************************************************************
408** Writing Function to read objects
409**************************************************************************/
410
411//new code
412/*
413static void readObjContents(sqlite3_context *ctx, const char *bucket, const char *object, const char *version){
414 object_handle_t *obj = create_object_handle(bucket, object, version);
415 sqlite3_int64 nIn;
416 void *pBuf;
417 sqlite3 *db;
418 int mxBlob;
419
420 if( obj==0 ){
421 // Object does not exist or is unreadable. Leave the result set to NULL.
422 return;
423 }
424 const ObjectSystemInfoFB *object_info_fb = GetObjectSystemInfoFB(obj->obj_info->GetBufferPointer());
425 nIn = object_info_fb->length();
426 db = sqlite3_context_db_handle(ctx);
427 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
428 if( nIn>mxBlob ){
429 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
430 destroy_object_handle(obj);
431 return;
432 }
433 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
434 if( pBuf==0 ){
435 sqlite3_result_error_nomem(ctx);
436 destroy_object_handle(obj);
437 return;
438 }
439 if( nIn==(sqlite3_int64)read_object(obj, nIn, pBuf);){
440 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
441 }else{
442 sqlite3_result_error_code(ctx, SQLITE_IOERR);
443 sqlite3_free(pBuf);
444 }
445 destroy_object_handle(obj);
446}
447
448static void readobjFunc(
449 sqlite3_context *context,
450 int argc,
451 sqlite3_value **argv
452){
453 const char *bucket,*object,*version;
454 (void)(argc); // Unused parameter
455 bucket = (const char*)sqlite3_value_text(argv[0]);
456 object = (const char*)sqlite3_value_text(argv[1]);
457 version = (const char*)sqlite3_value_text(argv[2]);
458 if( bucket==0 || object==0 ) return;
459 readObjContents(context, bucket,object,version);
460}
461*/
462//new code
463
464/**************************************************************************
465** Utility routines for dealing with JsonString objects
466**************************************************************************/
467
468/* Set the JsonString object to an empty string
469*/
470static void jsonZero(JsonString *p){
471 p->zBuf = p->zSpace;
472 p->nAlloc = sizeof(p->zSpace);
473 p->nUsed = 0;
474 p->bStatic = 1;
475}
476
477/* Initialize the JsonString object
478*/
479static void jsonInit(JsonString *p, sqlite3_context *pCtx){
480 p->pCtx = pCtx;
481 p->bErr = 0;
482 jsonZero(p);
483}
484
485
486/* Free all allocated memory and reset the JsonString object back to its
487** initial state.
488*/
489static void jsonReset(JsonString *p){
490 if( !p->bStatic ) sqlite3_free(p->zBuf);
491 jsonZero(p);
492}
493
494
495/* Report an out-of-memory (OOM) condition
496*/
497static void jsonOom(JsonString *p){
498 p->bErr = 1;
499 sqlite3_result_error_nomem(p->pCtx);
500 jsonReset(p);
501}
502
503/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
504** Return zero on success. Return non-zero on an OOM error
505*/
506static int jsonGrow(JsonString *p, u32 N){
507 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
508 char *zNew;
509 if( p->bStatic ){
510 if( p->bErr ) return 1;
511 zNew = sqlite3_malloc64(nTotal);
512 if( zNew==0 ){
513 jsonOom(p);
514 return SQLITE_NOMEM;
515 }
516 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
517 p->zBuf = zNew;
518 p->bStatic = 0;
519 }else{
520 zNew = sqlite3_realloc64(p->zBuf, nTotal);
521 if( zNew==0 ){
522 jsonOom(p);
523 return SQLITE_NOMEM;
524 }
525 p->zBuf = zNew;
526 }
527 p->nAlloc = nTotal;
528 return SQLITE_OK;
529}
530
531/* Append N bytes from zIn onto the end of the JsonString string.
532*/
533static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
534 if( N==0 ) return;
535 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
536 memcpy(p->zBuf+p->nUsed, zIn, N);
537 p->nUsed += N;
538}
539
540/* Append formatted text (not to exceed N bytes) to the JsonString.
541*/
542static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
543 va_list ap;
544 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
545 va_start(ap, zFormat);
546 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
547 va_end(ap);
548 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
549}
550
551/* Append a single character
552*/
553static void jsonAppendChar(JsonString *p, char c){
554 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
555 p->zBuf[p->nUsed++] = c;
556}
557
558/* Append a comma separator to the output buffer, if the previous
559** character is not '[' or '{'.
560*/
561static void jsonAppendSeparator(JsonString *p){
562 char c;
563 if( p->nUsed==0 ) return;
564 c = p->zBuf[p->nUsed-1];
565 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
566}
567
568/* Append the N-byte string in zIn to the end of the JsonString string
569** under construction. Enclose the string in "..." and escape
570** any double-quotes or backslash characters contained within the
571** string.
572*/
573static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
574 u32 i;
575 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
576 p->zBuf[p->nUsed++] = '"';
577 for(i=0; i<N; i++){
578 unsigned char c = ((unsigned const char*)zIn)[i];
579 if( c=='"' || c=='\\' ){
580 json_simple_escape:
581 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
582 p->zBuf[p->nUsed++] = '\\';
583 }else if( c<=0x1f ){
584 static const char aSpecial[] = {
585 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
587 };
588 assert( sizeof(aSpecial)==32 );
589 assert( aSpecial['\b']=='b' );
590 assert( aSpecial['\f']=='f' );
591 assert( aSpecial['\n']=='n' );
592 assert( aSpecial['\r']=='r' );
593 assert( aSpecial['\t']=='t' );
594 if( aSpecial[c] ){
595 c = aSpecial[c];
596 goto json_simple_escape;
597 }
598 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
599 p->zBuf[p->nUsed++] = '\\';
600 p->zBuf[p->nUsed++] = 'u';
601 p->zBuf[p->nUsed++] = '0';
602 p->zBuf[p->nUsed++] = '0';
603 p->zBuf[p->nUsed++] = '0' + (c>>4);
604 c = "0123456789abcdef"[c&0xf];
605 }
606 p->zBuf[p->nUsed++] = c;
607 }
608 p->zBuf[p->nUsed++] = '"';
609 assert( p->nUsed<p->nAlloc );
610}
611
612/*
613** Append a function parameter value to the JSON string under
614** construction.
615*/
616static void jsonAppendValue(
617 JsonString *p, /* Append to this JSON string */
618 sqlite3_value *pValue /* Value to append */
619){
620 switch( sqlite3_value_type(pValue) ){
621 case SQLITE_NULL: {
622 jsonAppendRaw(p, "null", 4);
623 break;
624 }
625 case SQLITE_INTEGER:
626 case SQLITE_FLOAT: {
627 const char *z = (const char*)sqlite3_value_text(pValue);
628 u32 n = (u32)sqlite3_value_bytes(pValue);
629 jsonAppendRaw(p, z, n);
630 break;
631 }
632 case SQLITE_TEXT: {
633 const char *z = (const char*)sqlite3_value_text(pValue);
634 u32 n = (u32)sqlite3_value_bytes(pValue);
635 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
636 jsonAppendRaw(p, z, n);
637 }else{
638 jsonAppendString(p, z, n);
639 }
640 break;
641 }
642 default: {
643 if( p->bErr==0 ){
644 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
645 p->bErr = 2;
646 jsonReset(p);
647 }
648 break;
649 }
650 }
651}
652
653
654/* Make the JSON in p the result of the SQL function.
655*/
656static void jsonResult(JsonString *p){
657 if( p->bErr==0 ){
658 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
659 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
660 SQLITE_UTF8);
661 jsonZero(p);
662 }
663 assert( p->bStatic );
664}
665
666/**************************************************************************
667** Utility routines for dealing with JsonNode and JsonParse objects
668**************************************************************************/
669
670/*
671** Return the number of consecutive JsonNode slots need to represent
672** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
673** OBJECT types, the number might be larger.
674**
675** Appended elements are not counted. The value returned is the number
676** by which the JsonNode counter should increment in order to go to the
677** next peer value.
678*/
679static u32 jsonNodeSize(JsonNode *pNode){
680 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
681}
682
683/*
684** Reclaim all memory allocated by a JsonParse object. But do not
685** delete the JsonParse object itself.
686*/
687static void jsonParseReset(JsonParse *pParse){
688 sqlite3_free(pParse->aNode);
689 pParse->aNode = 0;
690 pParse->nNode = 0;
691 pParse->nAlloc = 0;
692 sqlite3_free(pParse->aUp);
693 pParse->aUp = 0;
694}
695
696/*
697** Free a JsonParse object that was obtained from sqlite3_malloc().
698*/
699static void jsonParseFree(JsonParse *pParse){
700 jsonParseReset(pParse);
701 sqlite3_free(pParse);
702}
703
704/*
705** Convert the JsonNode pNode into a pure JSON string and
706** append to pOut. Subsubstructure is also included. Return
707** the number of JsonNode objects that are encoded.
708*/
709static void jsonRenderNode(
710 JsonNode *pNode, /* The node to render */
711 JsonString *pOut, /* Write JSON here */
712 sqlite3_value **aReplace /* Replacement values */
713){
714 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
715 if( pNode->jnFlags & JNODE_REPLACE ){
716 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
717 return;
718 }
719 pNode = pNode->u.pPatch;
720 }
721 switch( pNode->eType ){
722 default: {
723 assert( pNode->eType==JSON_NULL );
724 jsonAppendRaw(pOut, "null", 4);
725 break;
726 }
727 case JSON_TRUE: {
728 jsonAppendRaw(pOut, "true", 4);
729 break;
730 }
731 case JSON_FALSE: {
732 jsonAppendRaw(pOut, "false", 5);
733 break;
734 }
735 case JSON_STRING: {
736 if( pNode->jnFlags & JNODE_RAW ){
737 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
738 break;
739 }
740 /* no break */ deliberate_fall_through
741 }
742 case JSON_REAL:
743 case JSON_INT: {
744 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
745 break;
746 }
747 case JSON_ARRAY: {
748 u32 j = 1;
749 jsonAppendChar(pOut, '[');
750 for(;;){
751 while( j<=pNode->n ){
752 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
753 jsonAppendSeparator(pOut);
754 jsonRenderNode(&pNode[j], pOut, aReplace);
755 }
756 j += jsonNodeSize(&pNode[j]);
757 }
758 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
759 pNode = &pNode[pNode->u.iAppend];
760 j = 1;
761 }
762 jsonAppendChar(pOut, ']');
763 break;
764 }
765 case JSON_OBJECT: {
766 u32 j = 1;
767 jsonAppendChar(pOut, '{');
768 for(;;){
769 while( j<=pNode->n ){
770 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
771 jsonAppendSeparator(pOut);
772 jsonRenderNode(&pNode[j], pOut, aReplace);
773 jsonAppendChar(pOut, ':');
774 jsonRenderNode(&pNode[j+1], pOut, aReplace);
775 }
776 j += 1 + jsonNodeSize(&pNode[j+1]);
777 }
778 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
779 pNode = &pNode[pNode->u.iAppend];
780 j = 1;
781 }
782 jsonAppendChar(pOut, '}');
783 break;
784 }
785 }
786}
787
788/*
789** Return a JsonNode and all its descendents as a JSON string.
790*/
791static void jsonReturnJson(
792 JsonNode *pNode, /* Node to return */
793 sqlite3_context *pCtx, /* Return value for this function */
794 sqlite3_value **aReplace /* Array of replacement values */
795){
796 JsonString s;
797 jsonInit(&s, pCtx);
798 jsonRenderNode(pNode, &s, aReplace);
799 jsonResult(&s);
800 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
801}
802
803/*
804** Translate a single byte of Hex into an integer.
805** This routine only works if h really is a valid hexadecimal
806** character: 0..9a..fA..F
807*/
808static u8 jsonHexToInt(int h){
809 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
810#ifdef SQLITE_EBCDIC
811 h += 9*(1&~(h>>4));
812#else
813 h += 9*(1&(h>>6));
814#endif
815 return (u8)(h & 0xf);
816}
817
818/*
819** Convert a 4-byte hex string into an integer
820*/
821static u32 jsonHexToInt4(const char *z){
822 u32 v;
823 assert( safe_isxdigit(z[0]) );
824 assert( safe_isxdigit(z[1]) );
825 assert( safe_isxdigit(z[2]) );
826 assert( safe_isxdigit(z[3]) );
827 v = (jsonHexToInt(z[0])<<12)
828 + (jsonHexToInt(z[1])<<8)
829 + (jsonHexToInt(z[2])<<4)
830 + jsonHexToInt(z[3]);
831 return v;
832}
833
834/*
835** Make the JsonNode the return value of the function.
836*/
837static void jsonReturn(
838 JsonNode *pNode, /* Node to return */
839 sqlite3_context *pCtx, /* Return value for this function */
840 sqlite3_value **aReplace /* Array of replacement values */
841){
842 switch( pNode->eType ){
843 default: {
844 assert( pNode->eType==JSON_NULL );
845 sqlite3_result_null(pCtx);
846 break;
847 }
848 case JSON_TRUE: {
849 sqlite3_result_int(pCtx, 1);
850 break;
851 }
852 case JSON_FALSE: {
853 sqlite3_result_int(pCtx, 0);
854 break;
855 }
856 case JSON_INT: {
857 sqlite3_int64 i = 0;
858 const char *z = pNode->u.zJContent;
859 if( z[0]=='-' ){ z++; }
860 while( z[0]>='0' && z[0]<='9' ){
861 unsigned v = *(z++) - '0';
862 if( i>=LARGEST_INT64/10 ){
863 if( i>LARGEST_INT64/10 ) goto int_as_real;
864 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
865 if( v==9 ) goto int_as_real;
866 if( v==8 ){
867 if( pNode->u.zJContent[0]=='-' ){
868 sqlite3_result_int64(pCtx, SMALLEST_INT64);
869 goto int_done;
870 }else{
871 goto int_as_real;
872 }
873 }
874 }
875 i = i*10 + v;
876 }
877 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
878 sqlite3_result_int64(pCtx, i);
879 int_done:
880 break;
881 int_as_real: i=0; /* no break */ deliberate_fall_through
882 }
883 case JSON_REAL: {
884 double r;
885#ifdef SQLITE_AMALGAMATION
886 const char *z = pNode->u.zJContent;
887 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
888#else
889 r = strtod(pNode->u.zJContent, 0);
890#endif
891 sqlite3_result_double(pCtx, r);
892 break;
893 }
894 case JSON_STRING: {
895#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
896 ** json_insert() and json_replace() and those routines do not
897 ** call jsonReturn() */
898 if( pNode->jnFlags & JNODE_RAW ){
899 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
900 SQLITE_TRANSIENT);
901 }else
902#endif
903 assert( (pNode->jnFlags & JNODE_RAW)==0 );
904 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
905 /* JSON formatted without any backslash-escapes */
906 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
907 SQLITE_TRANSIENT);
908 }else{
909 /* Translate JSON formatted string into raw text */
910 u32 i;
911 u32 n = pNode->n;
912 const char *z = pNode->u.zJContent;
913 char *zOut;
914 u32 j;
915 zOut = sqlite3_malloc( n+1 );
916 if( zOut==0 ){
917 sqlite3_result_error_nomem(pCtx);
918 break;
919 }
920 for(i=1, j=0; i<n-1; i++){
921 char c = z[i];
922 if( c!='\\' ){
923 zOut[j++] = c;
924 }else{
925 c = z[++i];
926 if( c=='u' ){
927 u32 v = jsonHexToInt4(z+i+1);
928 i += 4;
929 if( v==0 ) break;
930 if( v<=0x7f ){
931 zOut[j++] = (char)v;
932 }else if( v<=0x7ff ){
933 zOut[j++] = (char)(0xc0 | (v>>6));
934 zOut[j++] = 0x80 | (v&0x3f);
935 }else{
936 u32 vlo;
937 if( (v&0xfc00)==0xd800
938 && i<n-6
939 && z[i+1]=='\\'
940 && z[i+2]=='u'
941 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
942 ){
943 /* We have a surrogate pair */
944 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
945 i += 6;
946 zOut[j++] = 0xf0 | (v>>18);
947 zOut[j++] = 0x80 | ((v>>12)&0x3f);
948 zOut[j++] = 0x80 | ((v>>6)&0x3f);
949 zOut[j++] = 0x80 | (v&0x3f);
950 }else{
951 zOut[j++] = 0xe0 | (v>>12);
952 zOut[j++] = 0x80 | ((v>>6)&0x3f);
953 zOut[j++] = 0x80 | (v&0x3f);
954 }
955 }
956 }else{
957 if( c=='b' ){
958 c = '\b';
959 }else if( c=='f' ){
960 c = '\f';
961 }else if( c=='n' ){
962 c = '\n';
963 }else if( c=='r' ){
964 c = '\r';
965 }else if( c=='t' ){
966 c = '\t';
967 }
968 zOut[j++] = c;
969 }
970 }
971 }
972 zOut[j] = 0;
973 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
974 }
975 break;
976 }
977 case JSON_ARRAY:
978 case JSON_OBJECT: {
979 jsonReturnJson(pNode, pCtx, aReplace);
980 break;
981 }
982 }
983}
984
985/* Forward reference */
986static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
987
988
989
990static JSON_NOINLINE int jsonParseAddNodeExpand(
991 JsonParse *pParse, /* Append the node to this object */
992 u32 eType, /* Node type */
993 u32 n, /* Content size or sub-node count */
994 const char *zContent /* Content */
995){
996 u32 nNew;
997 JsonNode *pNew;
998 assert( pParse->nNode>=pParse->nAlloc );
999 if( pParse->oom ) return -1;
1000 nNew = pParse->nAlloc*2 + 10;
1001 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
1002 if( pNew==0 ){
1003 pParse->oom = 1;
1004 return -1;
1005 }
1006 pParse->nAlloc = nNew;
1007 pParse->aNode = pNew;
1008 assert( pParse->nNode<pParse->nAlloc );
1009 return jsonParseAddNode(pParse, eType, n, zContent);
1010}
1011
1012/*
1013** Create a new JsonNode instance based on the arguments and append that
1014** instance to the JsonParse. Return the index in pParse->aNode[] of the
1015** new node, or -1 if a memory allocation fails.
1016*/
1017static int jsonParseAddNode(
1018 JsonParse *pParse, /* Append the node to this object */
1019 u32 eType, /* Node type */
1020 u32 n, /* Content size or sub-node count */
1021 const char *zContent /* Content */
1022){
1023 JsonNode *p;
1024 if( pParse->nNode>=pParse->nAlloc ){
1025 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
1026 }
1027 p = &pParse->aNode[pParse->nNode];
1028 p->eType = (u8)eType;
1029 p->jnFlags = 0;
1030 p->n = n;
1031 p->u.zJContent = zContent;
1032 return pParse->nNode++;
1033}
1034
1035/*
1036** Return true if z[] begins with 4 (or more) hexadecimal digits
1037*/
1038static int jsonIs4Hex(const char *z){
1039 int i;
1040 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
1041 return 1;
1042}
1043
1044/*
1045** Parse a single JSON value which begins at pParse->zJson[i]. Return the
1046** index of the first character past the end of the value parsed.
1047**
1048** Return negative for a syntax error. Special cases: return -2 if the
1049** first non-whitespace character is '}' and return -3 if the first
1050** non-whitespace character is ']'.
1051*/
1052
1053#define standsize 100
1054typedef struct ret ret;
1055struct ret {
1056 char *zBuf; /* Append JSON content here */
1057 u64 nAlloc; /* Bytes of storage available in zBuf[] */
1058 u64 nUsed; /* Bytes of zBuf[] currently used */
1059};
1060
1061void addChar(ret *temp,char c){
1062 if(temp->nUsed>=temp->nAlloc){
1063 u32 nNew = temp->nAlloc*2 + 10;
1064 char *pNew;
1065 pNew = sqlite3_realloc64(temp->zBuf, sizeof(char)*nNew);
1066 temp->nAlloc = nNew;
1067 temp->zBuf = pNew;
1068 }
1069 temp->zBuf[temp->nUsed++]=c;
1070}
1071static int jsonParseValue(JsonParse *pParse, u32 i){
1072 char c;
1073 u32 j;
1074 int iThis;
1075 int x;
1076 JsonNode *pNode;
1077 //new code
1078 //const char *z = pParse->zJson;
1079 //while( safe_isspace(z[i]) ){ i++; }
1080 while( safe_isspace(json_getc_next(pParse->srdr) )){ json_getc(pParse->srdr); i++; }
1081 if( (c = json_getc(pParse->srdr))=='{' ){
1082 /* Parse object */
1083 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1084 if( iThis<0 ) return -1;
1085 for(j=i+1;;j++){
1086 while( safe_isspace(json_getc_next(pParse->srdr)) ){ json_getc(pParse->srdr); j++; }
1087 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
1088 x = jsonParseValue(pParse, j);
1089 if( x<0 ){
1090 pParse->iDepth--;
1091 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
1092 return -1;
1093 }
1094 if( pParse->oom ) return -1;
1095 pNode = &pParse->aNode[pParse->nNode-1];
1096 if( pNode->eType!=JSON_STRING ) return -1;
1097 pNode->jnFlags |= JNODE_LABEL;
1098 j = x;
1099 while( safe_isspace(json_getc_next(pParse->srdr)) ){ json_getc(pParse->srdr); j++; }
1100 if( json_getc(pParse->srdr)!=':' ) return -1;
1101 j++;
1102 x = jsonParseValue(pParse, j);
1103 pParse->iDepth--;
1104 if( x<0 ) return -1;
1105 j = x;
1106 while( safe_isspace(json_getc_next(pParse->srdr) )){ json_getc(pParse->srdr); j++; }
1107 c = json_getc(pParse->srdr);
1108 if( c==',' ) continue;
1109 if( c!='}' ) return -1;
1110 break;
1111 }
1112 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
1113 return j+1;
1114 }
1115 else if( c=='[' ){
1116 /* Parse array */
1117 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1118 if( iThis<0 ) return -1;
1119 for(j=i+1;;j++){
1120 while( safe_isspace(json_getc_next(pParse->srdr)) ){ json_getc(pParse->srdr); j++; }
1121 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
1122 x = jsonParseValue(pParse, j);
1123 pParse->iDepth--;
1124 if( x<0 ){
1125 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
1126 return -1;
1127 }
1128 j = x;
1129 while( safe_isspace(json_getc_next(pParse->srdr)) ){ json_getc(pParse->srdr); j++; }
1130 c = json_getc(pParse->srdr);
1131 if( c==',' ) continue;
1132 if( c!=']' ) return -1;
1133 break;
1134 }
1135 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
1136 return j+1;
1137 }
1138 else if( c=='"' ){
1139 /* Parse string */
1140 u8 jnFlags = 0;
1141 ret *temp;
1142 temp = sqlite3_malloc64( sizeof(*temp) + standsize );
1143 memset(temp, 0, sizeof(*temp));
1144 temp->zBuf = (char*)&temp[1];
1145 temp->nAlloc = standsize;
1146 temp->nUsed = 0;
1147 //strcat(ret,&c);
1148 addChar(temp,c);
1149 j = i+1;
1150 for(;;){
1151 c = json_getc(pParse->srdr);
1152 //strcat(ret,&c);
1153 addChar(temp,c);
1154 if( (c & ~0x1f)==0 ){
1155 /* Control characters are not allowed in strings */
1156 return -1;
1157 }
1158 if( c=='\\' ){
1159 j++;
1160 c = json_getc(pParse->srdr);
1161 //strcat(ret,&c);
1162 addChar(temp,c);
1163 char *temp1;
1164 temp1 = sqlite3_malloc(4);
1165 char z1=json_getc_next(pParse->srdr);
1166 strcat(temp1,&z1);
1167 z1=json_getc_next_next(pParse->srdr);
1168 strcat(temp1,&z1);
1169 z1=json_getc_next_3(pParse->srdr);
1170 strcat(temp1,&z1);
1171 z1=json_getc_next_4(pParse->srdr);
1172 strcat(temp1,&z1);
1173 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
1174 || c=='n' || c=='r' || c=='t'
1175 || (c=='u' && jsonIs4Hex(temp1)) ){ //TODO : remove z
1176 jnFlags = JNODE_ESCAPE;
1177 }else{
1178 return -1;
1179 }
1180 }else if( c=='"' ){
1181 break;
1182 }
1183 j++;
1184 }
1185 jsonParseAddNode(pParse, JSON_STRING, j+1-i, temp->zBuf); //to resolve
1186 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
1187 return j+1;
1188 }
1189 else if( c=='n' && json_getc(pParse->srdr) == 'u' && json_getc(pParse->srdr) == 'l' && json_getc(pParse->srdr) == 'l' && !safe_isalnum(json_getc_next(pParse->srdr)) ){
1190 //&& strncmp(z+i,"null",4)==0
1191 //&& !safe_isalnum(z[i+4]) ){
1192 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1193 return i+4;
1194 }
1195 else if( c=='t' && json_getc(pParse->srdr) == 'r' && json_getc(pParse->srdr) == 'u' && json_getc(pParse->srdr) == 'e' && !safe_isalnum(json_getc_next(pParse->srdr)) ){
1196 //&& strncmp(z+i,"true",4)==0
1197 //&& !safe_isalnum(z[i+4]) ){
1198 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1199 return i+4;
1200 }
1201 else if( c=='f' && json_getc(pParse->srdr) == 'a' && json_getc(pParse->srdr) == 'l' && json_getc(pParse->srdr) == 's' && !safe_isalnum(json_getc_next(pParse->srdr)) ){
1202 //&& strncmp(z+i,"false",5)==0
1203 //&& !safe_isalnum(z[i+5]) ){
1204 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
1205 return i+5;
1206 }
1207 else if( c=='-' || (c>='0' && c<='9') ){
1208 /* Parse number */
1209 u8 seenDP = 0;
1210 u8 seenE = 0;
1211 assert( '-' < '0' );
1212 char ch[3];
1213 ret *temp;
1214 temp = sqlite3_malloc64( sizeof(*temp) + standsize );
1215 memset(temp, 0, sizeof(*temp));
1216 temp->zBuf = (char*)&temp[1];
1217 temp->nAlloc = standsize;
1218 temp->nUsed = 0;
1219 ch[0]=c;
1220 addChar(temp,ch[0]);
1221 ch[1]=json_getc_next(pParse->srdr);
1222 ch[2]=json_getc_next_next(pParse->srdr);
1223 if( c<='0' ){
1224 j = c=='-' ? 1 : 0;
1225 if( ch[j]=='0' && ch[j+1]>='0' && ch[j+1]<='9' ) return -1;
1226 }
1227 j = i+1;
1228 for(;; j++){
1229 //c = z[j];
1230 ch[0]=json_getc(pParse->srdr);
1231 addChar(temp,ch[0]);
1232 ch[1]=json_getc_next(pParse->srdr);
1233 ch[2]=json_getc_next_next(pParse->srdr);
1234 c = ch[1];
1235 if( c>='0' && c<='9' ) continue;
1236 if( c=='.' ){
1237 //if( z[j-1]=='-' ) return -1;
1238 if( ch[0]=='-' ) return -1;
1239 if( seenDP ) return -1;
1240 seenDP = 1;
1241 continue;
1242 }
1243 if( c=='e' || c=='E' ){
1244 //if( z[j-1]<'0' ) return -1;
1245 if( ch[0]<'0') return -1;
1246 if( seenE ) return -1;
1247 seenDP = seenE = 1;
1248 //c = z[j+1];
1249 c = ch[2];
1250 if( c=='+' || c=='-' ){
1251 j++;
1252 ch[0]=json_getc(pParse->srdr);
1253 addChar(temp,ch[0]);
1254 ch[1]=json_getc_next(pParse->srdr);
1255 ch[2]=json_getc_next_next(pParse->srdr);
1256 //c = z[j+1];
1257 c = ch[2];
1258 }
1259 if( c<'0' || c>'9' ) return -1;
1260 continue;
1261 }
1262 break;
1263 }
1264 //if( z[j-1]<'0' ) return -1;
1265 if( ch[0]< '0')return -1;
1266 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
1267 j - i, temp->zBuf);
1268 return j;
1269 }
1270 else if( c=='}' ){
1271 return -2; /* End of {...} */
1272 }
1273 else if( c==']' ){
1274 return -3; /* End of [...] */
1275 }
1276 else if( c==0 ){
1277 return 0; /* End of file */
1278 }
1279 else{
1280 return -1; /* Syntax error */
1281 }
1282}
1283
1284/*
1285** Parse a complete JSON string. Return 0 on success or non-zero if there
1286** are any errors. If an error occurs, free all memory associated with
1287** pParse.
1288**
1289** pParse is uninitialized when this routine is called.
1290*/
1291static int jsonParse(
1292 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1293 sqlite3_context *pCtx, /* Report errors here */
1294 //new code
1295 //const char *zJson /* Input JSON text to be parsed */
1296 JsonReader *srdr
1297 //new code
1298){
1299 int i;
1300 memset(pParse, 0, sizeof(*pParse));
1301 //new code
1302 //if( zJson==0 ) return 1;
1303 //pParse->zJson = zJson;
1304 //i = jsonParseValue(pParse, 0);
1305 if( srdr == NULL)return 1;
1306 pParse->srdr = srdr;
1307 i = jsonParseValue(pParse,0);
1308 //new code
1309 if( pParse->oom ) i = -1;
1310 if( i>0 ){
1311 assert( pParse->iDepth==0 );
1312 //while( safe_isspace(zJson[i]) ) i++;
1313 while( safe_isspace(json_getc_next(pParse->srdr)) ){json_getc(pParse->srdr); i++;}
1314 if( json_getc_next(pParse->srdr) ) i = -1;
1315 }
1316 if( i<=0 ){
1317 if( pCtx!=0 ){
1318 if( pParse->oom ){
1319 sqlite3_result_error_nomem(pCtx);
1320 }else{
1321 sqlite3_result_error(pCtx, "malformed JSON", -1);
1322 }
1323 }
1324 jsonParseReset(pParse);
1325 return 1;
1326 }
1327 return 0;
1328}
1329
1330/* Mark node i of pParse as being a child of iParent. Call recursively
1331** to fill in all the descendants of node i.
1332*/
1333static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
1334 JsonNode *pNode = &pParse->aNode[i];
1335 u32 j;
1336 pParse->aUp[i] = iParent;
1337 switch( pNode->eType ){
1338 case JSON_ARRAY: {
1339 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
1340 jsonParseFillInParentage(pParse, i+j, i);
1341 }
1342 break;
1343 }
1344 case JSON_OBJECT: {
1345 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
1346 pParse->aUp[i+j] = i;
1347 jsonParseFillInParentage(pParse, i+j+1, i);
1348 }
1349 break;
1350 }
1351 default: {
1352 break;
1353 }
1354 }
1355}
1356
1357/*
1358** Compute the parentage of all nodes in a completed parse.
1359*/
1360static int jsonParseFindParents(JsonParse *pParse){
1361 u32 *aUp;
1362 assert( pParse->aUp==0 );
1363 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
1364 if( aUp==0 ){
1365 pParse->oom = 1;
1366 return SQLITE_NOMEM;
1367 }
1368 jsonParseFillInParentage(pParse, 0, 0);
1369 return SQLITE_OK;
1370}
1371
1372//new code
1373bool CheckSameJson(JsonReader *srdr1, JsonReader *srdr2){
1374
1375
1376
1377
1378
1379
1380 while(1){
1381 char c1,c2;
1382 c1=json_getc(srdr1);
1383 c2=json_getc(srdr2);
1384 if(c1!=c2)return 0;
1385 if(c1==EOF)break;
1386 }
1387 return 1;
1388}
1389
1390/*
1391** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1392*/
1393#define JSON_CACHE_ID (-429938) /* First cache entry */
1394#define JSON_CACHE_SZ 4 /* Max number of cache entries */
1395
1396/*
1397** Obtain a complete parse of the JSON found in the first argument
1398** of the argv array. Use the sqlite3_get_auxdata() cache for this
1399** parse if it is available. If the cache is not available or if it
1400** is no longer valid, parse the JSON again and return the new parse,
1401** and also register the new parse so that it will be available for
1402** future sqlite3_get_auxdata() calls.
1403*/
1404static JsonParse *jsonParseCached(
1405 sqlite3_context *pCtx,
1406 sqlite3_value **argv, //argv[0]-> bucket, argv[1] -> object, argv[2] -> version
1407 sqlite3_context *pErrCtx
1408){
1409 //new code
1410 //const char *zJson = (const char*)sqlite3_value_text(argv[0]);
1411 //int nJson = sqlite3_value_bytes(argv[0]);
1412 JsonReader srdr;
1413 memset(&srdr, 0, sizeof(srdr));
1414 #define BUCKET (const char*)sqlite3_value_text(argv[0])
1415 #define OBJECT (const char*)sqlite3_value_text(argv[1])
1416 #define VERSION (const char*)sqlite3_value_text(argv[2])
1417 json_reader_open(&srdr,BUCKET,OBJECT,VERSION);
1418 JsonParse *p;
1419 JsonParse *pMatch = 0;
1420 int iKey;
1421 int iMinKey = 0;
1422 u32 iMinHold = 0xffffffff;
1423 u32 iMaxHold = 0;
1424 if( srdr.obj==0 ) return 0;
1425 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
1426 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
1427 if( p==0 ){
1428 iMinKey = iKey;
1429 break;
1430 }
1431 if( pMatch==0
1432 && CheckSameJson(p->srdr,&srdr)
1433 ){
1434 p->nErr = 0;
1435 pMatch = p;
1436 }else if( p->iHold<iMinHold ){
1437 iMinHold = p->iHold;
1438 iMinKey = iKey;
1439 }
1440 if( p->iHold>iMaxHold ){
1441 iMaxHold = p->iHold;
1442 }
1443 }
1444 if( pMatch ){
1445 pMatch->nErr = 0;
1446 pMatch->iHold = iMaxHold+1;
1447 return pMatch;
1448 }
1449 //p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
1450 p = sqlite3_malloc64( sizeof(*p));
1451 if( p==0 ){
1452 sqlite3_result_error_nomem(pCtx);
1453 return 0;
1454 }
1455 memset(p, 0, sizeof(*p));
1456 //p->zJson = (char*)&p[1];
1457 //memcpy((char*)p->zJson, zJson, nJson+1);
1458 p->srdr = &srdr;
1459 if( jsonParse(p, pErrCtx, p->srdr) ){
1460 sqlite3_free(p);
1461 return 0;
1462 }
1463 //p->nJson = nJson;
1464 p->iHold = iMaxHold+1;
1465 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
1466 (void(*)(void*))jsonParseFree);
1467 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
1468}
1469
1470/*
1471** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1472** a match.
1473*/
1474static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
1475 if( pNode->jnFlags & JNODE_RAW ){
1476 if( pNode->n!=nKey ) return 0;
1477 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
1478 }else{
1479 if( pNode->n!=nKey+2 ) return 0;
1480 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
1481 }
1482}
1483
1484/* forward declaration */
1485static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
1486
1487/*
1488** Search along zPath to find the node specified. Return a pointer
1489** to that node, or NULL if zPath is malformed or if there is no such
1490** node.
1491**
1492** If pApnd!=0, then try to append new nodes to complete zPath if it is
1493** possible to do so and if no existing node corresponds to zPath. If
1494** new nodes are appended *pApnd is set to 1.
1495*/
1496static JsonNode *jsonLookupStep(
1497 JsonParse *pParse, /* The JSON to search */
1498 u32 iRoot, /* Begin the search at this node */
1499 const char *zPath, /* The path to search */
1500 int *pApnd, /* Append nodes to complete path if not NULL */
1501 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
1502){
1503 u32 i, j, nKey;
1504 const char *zKey;
1505 JsonNode *pRoot = &pParse->aNode[iRoot];
1506 if( zPath[0]==0 ) return pRoot;
1507 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
1508 if( zPath[0]=='.' ){
1509 if( pRoot->eType!=JSON_OBJECT ) return 0;
1510 zPath++;
1511 if( zPath[0]=='"' ){
1512 zKey = zPath + 1;
1513 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
1514 nKey = i-1;
1515 if( zPath[i] ){
1516 i++;
1517 }else{
1518 *pzErr = zPath;
1519 return 0;
1520 }
1521 }else{
1522 zKey = zPath;
1523 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
1524 nKey = i;
1525 }
1526 if( nKey==0 ){
1527 *pzErr = zPath;
1528 return 0;
1529 }
1530 j = 1;
1531 for(;;){
1532 while( j<=pRoot->n ){
1533 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
1534 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
1535 }
1536 j++;
1537 j += jsonNodeSize(&pRoot[j]);
1538 }
1539 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1540 iRoot += pRoot->u.iAppend;
1541 pRoot = &pParse->aNode[iRoot];
1542 j = 1;
1543 }
1544 if( pApnd ){
1545 u32 iStart, iLabel;
1546 JsonNode *pNode;
1547 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1548 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1549 zPath += i;
1550 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
1551 if( pParse->oom ) return 0;
1552 if( pNode ){
1553 pRoot = &pParse->aNode[iRoot];
1554 pRoot->u.iAppend = iStart - iRoot;
1555 pRoot->jnFlags |= JNODE_APPEND;
1556 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1557 }
1558 return pNode;
1559 }
1560 }else if( zPath[0]=='[' ){
1561 i = 0;
1562 j = 1;
1563 while( safe_isdigit(zPath[j]) ){
1564 i = i*10 + zPath[j] - '0';
1565 j++;
1566 }
1567 if( j<2 || zPath[j]!=']' ){
1568 if( zPath[1]=='#' ){
1569 JsonNode *pBase = pRoot;
1570 int iBase = iRoot;
1571 if( pRoot->eType!=JSON_ARRAY ) return 0;
1572 for(;;){
1573 while( j<=pBase->n ){
1574 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
1575 j += jsonNodeSize(&pBase[j]);
1576 }
1577 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
1578 iBase += pBase->u.iAppend;
1579 pBase = &pParse->aNode[iBase];
1580 j = 1;
1581 }
1582 j = 2;
1583 if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
1584 unsigned int x = 0;
1585 j = 3;
1586 do{
1587 x = x*10 + zPath[j] - '0';
1588 j++;
1589 }while( safe_isdigit(zPath[j]) );
1590 if( x>i ) return 0;
1591 i -= x;
1592 }
1593 if( zPath[j]!=']' ){
1594 *pzErr = zPath;
1595 return 0;
1596 }
1597 }else{
1598 *pzErr = zPath;
1599 return 0;
1600 }
1601 }
1602 if( pRoot->eType!=JSON_ARRAY ) return 0;
1603 zPath += j + 1;
1604 j = 1;
1605 for(;;){
1606 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1607 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
1608 j += jsonNodeSize(&pRoot[j]);
1609 }
1610 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1611 iRoot += pRoot->u.iAppend;
1612 pRoot = &pParse->aNode[iRoot];
1613 j = 1;
1614 }
1615 if( j<=pRoot->n ){
1616 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
1617 }
1618 if( i==0 && pApnd ){
1619 u32 iStart;
1620 JsonNode *pNode;
1621 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
1622 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
1623 if( pParse->oom ) return 0;
1624 if( pNode ){
1625 pRoot = &pParse->aNode[iRoot];
1626 pRoot->u.iAppend = iStart - iRoot;
1627 pRoot->jnFlags |= JNODE_APPEND;
1628 }
1629 return pNode;
1630 }
1631 }else{
1632 *pzErr = zPath;
1633 }
1634 return 0;
1635}
1636
1637/*
1638** Append content to pParse that will complete zPath. Return a pointer
1639** to the inserted node, or return NULL if the append fails.
1640*/
1641static JsonNode *jsonLookupAppend(
1642 JsonParse *pParse, /* Append content to the JSON parse */
1643 const char *zPath, /* Description of content to append */
1644 int *pApnd, /* Set this flag to 1 */
1645 const char **pzErr /* Make this point to any syntax error */
1646){
1647 *pApnd = 1;
1648 if( zPath[0]==0 ){
1649 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1650 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1651 }
1652 if( zPath[0]=='.' ){
1653 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1654 }else if( strncmp(zPath,"[0]",3)==0 ){
1655 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1656 }else{
1657 return 0;
1658 }
1659 if( pParse->oom ) return 0;
1660 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
1661}
1662
1663/*
1664** Return the text of a syntax error message on a JSON path. Space is
1665** obtained from sqlite3_malloc().
1666*/
1667static char *jsonPathSyntaxError(const char *zErr){
1668 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1669}
1670
1671/*
1672** Do a node lookup using zPath. Return a pointer to the node on success.
1673** Return NULL if not found or if there is an error.
1674**
1675** On an error, write an error message into pCtx and increment the
1676** pParse->nErr counter.
1677**
1678** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1679** nodes are appended.
1680*/
1681static JsonNode *jsonLookup(
1682 JsonParse *pParse, /* The JSON to search */
1683 const char *zPath, /* The path to search */
1684 int *pApnd, /* Append nodes to complete path if not NULL */
1685 sqlite3_context *pCtx /* Report errors here, if not NULL */
1686){
1687 const char *zErr = 0;
1688 JsonNode *pNode = 0;
1689 char *zMsg;
1690
1691 if( zPath==0 ) return 0;
1692 if( zPath[0]!='$' ){
1693 zErr = zPath;
1694 goto lookup_err;
1695 }
1696 zPath++;
1697 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
1698 if( zErr==0 ) return pNode;
1699
1700lookup_err:
1701 pParse->nErr++;
1702 assert( zErr!=0 && pCtx!=0 );
1703 zMsg = jsonPathSyntaxError(zErr);
1704 if( zMsg ){
1705 sqlite3_result_error(pCtx, zMsg, -1);
1706 sqlite3_free(zMsg);
1707 }else{
1708 sqlite3_result_error_nomem(pCtx);
1709 }
1710 return 0;
1711}
1712
1713
1714/*
1715** Report the wrong number of arguments for json_insert(), json_replace()
1716** or json_set().
1717*/
1718static void jsonWrongNumArgs(
1719 sqlite3_context *pCtx,
1720 const char *zFuncName
1721){
1722 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1723 zFuncName);
1724 sqlite3_result_error(pCtx, zMsg, -1);
1725 sqlite3_free(zMsg);
1726}
1727
1728/*
1729** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1730*/
1731static void jsonRemoveAllNulls(JsonNode *pNode){
1732 int i, n;
1733 assert( pNode->eType==JSON_OBJECT );
1734 n = pNode->n;
1735 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1736 switch( pNode[i].eType ){
1737 case JSON_NULL:
1738 pNode[i].jnFlags |= JNODE_REMOVE;
1739 break;
1740 case JSON_OBJECT:
1741 jsonRemoveAllNulls(&pNode[i]);
1742 break;
1743 }
1744 }
1745}
1746
1747
1748/****************************************************************************
1749** SQL functions used for testing and debugging
1750****************************************************************************/
1751
1752#ifdef SQLITE_DEBUG
1753/*
1754** The json_parse(JSON) function returns a string which describes
1755** a parse of the JSON provided. Or it returns NULL if JSON is not
1756** well-formed.
1757*/
1758static void jsonParseFunc(
1759 sqlite3_context *ctx,
1760 int argc,
1761 sqlite3_value **argv
1762){
1763 JsonString s; /* Output string - not real JSON */
1764 JsonParse x; /* The parse */
1765 u32 i;
1766
1767 assert( argc==3 );
1768 JsonReader srdr;
1769 memset(&srdr, 0, sizeof(srdr));
1770 json_reader_open(&srdr,BUCKET,OBJECT,VERSION);
1771 if( jsonParse(&x, ctx, &srdr) ) return;
1772 jsonParseFindParents(&x);
1773 jsonInit(&s, ctx);
1774 for(i=0; i<x.nNode; i++){
1775 const char *zType;
1776 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1777 assert( x.aNode[i].eType==JSON_STRING );
1778 zType = "label";
1779 }else{
1780 zType = jsonType[x.aNode[i].eType];
1781 }
1782 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1783 i, zType, x.aNode[i].n, x.aUp[i]);
1784 if( x.aNode[i].u.zJContent!=0 ){
1785 jsonAppendRaw(&s, " ", 1);
1786 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1787 }
1788 jsonAppendRaw(&s, "\n", 1);
1789 }
1790 jsonParseReset(&x);
1791 jsonResult(&s);
1792}
1793
1794/*
1795** The json_test1(JSON) function return true (1) if the input is JSON
1796** text generated by another json function. It returns (0) if the input
1797** is not known to be JSON.
1798*/
1799static void jsonTest1Func(
1800 sqlite3_context *ctx,
1801 int argc,
1802 sqlite3_value **argv
1803){
1804 UNUSED_PARAM(argc);
1805 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
1806}
1807#endif /* SQLITE_DEBUG */
1808
1809/****************************************************************************
1810** Scalar SQL function implementations
1811****************************************************************************/
1812
1813/*
1814** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1815** corresponding to the SQL value input. Mostly this means putting
1816** double-quotes around strings and returning the unquoted string "null"
1817** when given a NULL input.
1818*/
1819static void jsonQuoteFunc(
1820 sqlite3_context *ctx,
1821 int argc,
1822 sqlite3_value **argv
1823){
1824 JsonString jx;
1825 UNUSED_PARAM(argc);
1826
1827 jsonInit(&jx, ctx);
1828 jsonAppendValue(&jx, argv[0]);
1829 jsonResult(&jx);
1830 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1831}
1832
1833/*
1834** Implementation of the json_array(VALUE,...) function. Return a JSON
1835** array that contains all values given in arguments. Or if any argument
1836** is a BLOB, throw an error.
1837*/
1838static void jsonArrayFunc(
1839 sqlite3_context *ctx,
1840 int argc,
1841 sqlite3_value **argv
1842){
1843 int i;
1844 JsonString jx;
1845
1846 jsonInit(&jx, ctx);
1847 jsonAppendChar(&jx, '[');
1848 for(i=0; i<argc; i++){
1849 jsonAppendSeparator(&jx);
1850 jsonAppendValue(&jx, argv[i]);
1851 }
1852 jsonAppendChar(&jx, ']');
1853 jsonResult(&jx);
1854 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1855}
1856
1857
1858/*
1859** json_array_length(JSON)
1860** json_array_length(JSON, PATH)
1861**
1862** Return the number of elements in the top-level JSON array.
1863** Return 0 if the input is not a well-formed JSON array.
1864*/
1865static void jsonArrayLengthFunc(
1866 sqlite3_context *ctx,
1867 int argc,
1868 sqlite3_value **argv //argv[0] -> bucket, argv[1] -> object, argv[2] -> version
1869){
1870 JsonParse *p; /* The parse */
1871 sqlite3_int64 n = 0;
1872 u32 i;
1873 JsonNode *pNode;
1874
1875 p = jsonParseCached(ctx, argv, ctx);
1876 if( p==0 ) return;
1877 assert( p->nNode );
1878 if( argc==4 ){
1879 const char *zPath = (const char*)sqlite3_value_text(argv[3]);
1880 pNode = jsonLookup(p, zPath, 0, ctx);
1881 }else{
1882 pNode = p->aNode;
1883 }
1884 if( pNode==0 ){
1885 return;
1886 }
1887 if( pNode->eType==JSON_ARRAY ){
1888 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1889 for(i=1; i<=pNode->n; n++){
1890 i += jsonNodeSize(&pNode[i]);
1891 }
1892 }
1893 sqlite3_result_int64(ctx, n);
1894}
1895
1896/*
1897** json_extract(JSON, PATH, ...)
1898**
1899** Return the element described by PATH. Return NULL if there is no
1900** PATH element. If there are multiple PATHs, then return a JSON array
1901** with the result from each path. Throw an error if the JSON or any PATH
1902** is malformed.
1903*/
1904static void jsonExtractFunc(
1905 sqlite3_context *ctx,
1906 int argc,
1907 sqlite3_value **argv //argv[0] -> bucket, argv[1] -> object, argv[2] -> version
1908){
1909 FILE *fptr;
1910 fptr = fopen("~/debug.txt","w+");
1911 if(fptr == NULL)
1912 {
1913 printf("Error!");
1914 exit(1);
1915 }
1916 JsonParse *p; /* The parse */
1917 JsonNode *pNode;
1918 const char *zPath;
1919 JsonString jx;
1920 int i;
1921
1922 fprintf(fptr,"Hi!! In json_extract\n");
1923 if( argc<4 ) return;
1924 p = jsonParseCached(ctx, argv, ctx);
1925 if( p==0 ) return;
1926 jsonInit(&jx, ctx);
1927 jsonAppendChar(&jx, '[');
1928 for(i=3; i<argc; i++){
1929 zPath = (const char*)sqlite3_value_text(argv[i]);
1930 pNode = jsonLookup(p, zPath, 0, ctx);
1931 if( p->nErr ) break;
1932 if( argc>2 ){
1933 jsonAppendSeparator(&jx);
1934 if( pNode ){
1935 jsonRenderNode(pNode, &jx, 0);
1936 }else{
1937 jsonAppendRaw(&jx, "null", 4);
1938 }
1939 }else if( pNode ){
1940 jsonReturn(pNode, ctx, 0);
1941 }
1942 }
1943 if( argc>4 && i==argc ){
1944 jsonAppendChar(&jx, ']');
1945 jsonResult(&jx);
1946 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1947 }
1948 jsonReset(&jx);
1949}
1950
1951/* This is the RFC 7396 MergePatch algorithm.
1952*/
1953static JsonNode *jsonMergePatch(
1954 JsonParse *pParse, /* The JSON parser that contains the TARGET */
1955 u32 iTarget, /* Node of the TARGET in pParse */
1956 JsonNode *pPatch /* The PATCH */
1957){
1958 u32 i, j;
1959 u32 iRoot;
1960 JsonNode *pTarget;
1961 if( pPatch->eType!=JSON_OBJECT ){
1962 return pPatch;
1963 }
1964 assert( iTarget>=0 && iTarget<pParse->nNode );
1965 pTarget = &pParse->aNode[iTarget];
1966 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1967 if( pTarget->eType!=JSON_OBJECT ){
1968 jsonRemoveAllNulls(pPatch);
1969 return pPatch;
1970 }
1971 iRoot = iTarget;
1972 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
1973 u32 nKey;
1974 const char *zKey;
1975 assert( pPatch[i].eType==JSON_STRING );
1976 assert( pPatch[i].jnFlags & JNODE_LABEL );
1977 nKey = pPatch[i].n;
1978 zKey = pPatch[i].u.zJContent;
1979 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
1980 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1981 assert( pTarget[j].eType==JSON_STRING );
1982 assert( pTarget[j].jnFlags & JNODE_LABEL );
1983 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
1984 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1985 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
1986 if( pPatch[i+1].eType==JSON_NULL ){
1987 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1988 }else{
1989 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
1990 if( pNew==0 ) return 0;
1991 pTarget = &pParse->aNode[iTarget];
1992 if( pNew!=&pTarget[j+1] ){
1993 pTarget[j+1].u.pPatch = pNew;
1994 pTarget[j+1].jnFlags |= JNODE_PATCH;
1995 }
1996 }
1997 break;
1998 }
1999 }
2000 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
2001 int iStart, iPatch;
2002 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
2003 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
2004 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
2005 if( pParse->oom ) return 0;
2006 jsonRemoveAllNulls(pPatch);
2007 pTarget = &pParse->aNode[iTarget];
2008 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
2009 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
2010 iRoot = iStart;
2011 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
2012 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
2013 }
2014 }
2015 return pTarget;
2016}
2017
2018/*
2019** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
2020** object that is the result of running the RFC 7396 MergePatch() algorithm
2021** on the two arguments.
2022*/
2023static void jsonPatchFunc(
2024 sqlite3_context *ctx,
2025 int argc,
2026 sqlite3_value **argv
2027){
2028 JsonParse x; /* The JSON that is being patched */
2029 JsonParse y; /* The patch */
2030 JsonNode *pResult; /* The result of the merge */
2031
2032 UNUSED_PARAM(argc);
2033 JsonReader srdr1;
2034 memset(&srdr1, 0, sizeof(srdr1));
2035 json_reader_open(&srdr1,BUCKET,OBJECT,VERSION);
2036 JsonReader srdr2;
2037 memset(&srdr2, 0, sizeof(srdr2));
2038 json_reader_open(&srdr2,(const char*)sqlite3_value_text(argv[3]),(const char*)sqlite3_value_text(argv[4]),(const char*)sqlite3_value_text(argv[5]));
2039 if( jsonParse(&x, ctx, &srdr1) ) return;
2040 if( jsonParse(&y, ctx, &srdr2) ){
2041 jsonParseReset(&x);
2042 return;
2043 }
2044 pResult = jsonMergePatch(&x, 0, y.aNode);
2045 assert( pResult!=0 || x.oom );
2046 if( pResult ){
2047 jsonReturnJson(pResult, ctx, 0);
2048 }else{
2049 sqlite3_result_error_nomem(ctx);
2050 }
2051 jsonParseReset(&x);
2052 jsonParseReset(&y);
2053}
2054
2055
2056/*
2057** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
2058** object that contains all name/value given in arguments. Or if any name
2059** is not a string or if any value is a BLOB, throw an error.
2060*/
2061static void jsonObjectFunc(
2062 sqlite3_context *ctx,
2063 int argc,
2064 sqlite3_value **argv
2065){
2066 int i;
2067 JsonString jx;
2068 const char *z;
2069 u32 n;
2070
2071 if( argc&1 ){
2072 sqlite3_result_error(ctx, "json_object() requires an even number "
2073 "of arguments", -1);
2074 return;
2075 }
2076 jsonInit(&jx, ctx);
2077 jsonAppendChar(&jx, '{');
2078 for(i=0; i<argc; i+=2){
2079 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
2080 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
2081 jsonReset(&jx);
2082 return;
2083 }
2084 jsonAppendSeparator(&jx);
2085 z = (const char*)sqlite3_value_text(argv[i]);
2086 n = (u32)sqlite3_value_bytes(argv[i]);
2087 jsonAppendString(&jx, z, n);
2088 jsonAppendChar(&jx, ':');
2089 jsonAppendValue(&jx, argv[i+1]);
2090 }
2091 jsonAppendChar(&jx, '}');
2092 jsonResult(&jx);
2093 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2094}
2095
2096
2097/*
2098** json_remove(JSON, PATH, ...)
2099**
2100** Remove the named elements from JSON and return the result. malformed
2101** JSON or PATH arguments result in an error.
2102*/
2103static void jsonRemoveFunc(
2104 sqlite3_context *ctx,
2105 int argc,
2106 sqlite3_value **argv
2107){
2108 JsonParse x; /* The parse */
2109 JsonNode *pNode;
2110 const char *zPath;
2111 u32 i;
2112
2113 if( argc<3 ) return;
2114 JsonReader srdr1;
2115 memset(&srdr1, 0, sizeof(srdr1));
2116 json_reader_open(&srdr1,BUCKET,OBJECT,VERSION);
2117 if( jsonParse(&x, ctx, &srdr1) ) return;
2118 assert( x.nNode );
2119 for(i=1; i<(u32)argc; i++){
2120 zPath = (const char*)sqlite3_value_text(argv[i]);
2121 if( zPath==0 ) goto remove_done;
2122 pNode = jsonLookup(&x, zPath, 0, ctx);
2123 if( x.nErr ) goto remove_done;
2124 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
2125 }
2126 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
2127 jsonReturnJson(x.aNode, ctx, 0);
2128 }
2129remove_done:
2130 jsonParseReset(&x);
2131}
2132
2133/*
2134** json_replace(JSON, PATH, VALUE, ...)
2135**
2136** Replace the value at PATH with VALUE. If PATH does not already exist,
2137** this routine is a no-op. If JSON or PATH is malformed, throw an error.
2138*/
2139static void jsonReplaceFunc(
2140 sqlite3_context *ctx,
2141 int argc,
2142 sqlite3_value **argv
2143){
2144 JsonParse x; /* The parse */
2145 JsonNode *pNode;
2146 const char *zPath;
2147 u32 i;
2148
2149 if( argc<3 ) return;
2150 if( (argc&1)==0 ) {
2151 jsonWrongNumArgs(ctx, "replace");
2152 return;
2153 }
2154 JsonReader srdr1;
2155 memset(&srdr1, 0, sizeof(srdr1));
2156 json_reader_open(&srdr1,BUCKET,OBJECT,VERSION);
2157 if( jsonParse(&x, ctx, &srdr1) ) return;
2158 assert( x.nNode );
2159 for(i=3; i<(u32)argc; i+=2){
2160 zPath = (const char*)sqlite3_value_text(argv[i]);
2161 pNode = jsonLookup(&x, zPath, 0, ctx);
2162 if( x.nErr ) goto replace_err;
2163 if( pNode ){
2164 pNode->jnFlags |= (u8)JNODE_REPLACE;
2165 pNode->u.iReplace = i + 1;
2166 }
2167 }
2168 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
2169 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
2170 }else{
2171 jsonReturnJson(x.aNode, ctx, argv);
2172 }
2173replace_err:
2174 jsonParseReset(&x);
2175}
2176
2177/*
2178** json_set(JSON, PATH, VALUE, ...)
2179**
2180** Set the value at PATH to VALUE. Create the PATH if it does not already
2181** exist. Overwrite existing values that do exist.
2182** If JSON or PATH is malformed, throw an error.
2183**
2184** json_insert(JSON, PATH, VALUE, ...)
2185**
2186** Create PATH and initialize it to VALUE. If PATH already exists, this
2187** routine is a no-op. If JSON or PATH is malformed, throw an error.
2188*/
2189static void jsonSetFunc(
2190 sqlite3_context *ctx,
2191 int argc,
2192 sqlite3_value **argv
2193){
2194 JsonParse x; /* The parse */
2195 JsonNode *pNode;
2196 const char *zPath;
2197 u32 i;
2198 int bApnd;
2199 int bIsSet = *(int*)sqlite3_user_data(ctx);
2200
2201 if( argc<3 ) return;
2202 if( (argc&1)==0 ) {
2203 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
2204 return;
2205 }
2206 JsonReader srdr1;
2207 memset(&srdr1, 0, sizeof(srdr1));
2208 json_reader_open(&srdr1,BUCKET,OBJECT,VERSION);
2209 if( jsonParse(&x, ctx, &srdr1) ) return;
2210 assert( x.nNode );
2211 for(i=3; i<(u32)argc; i+=2){
2212 zPath = (const char*)sqlite3_value_text(argv[i]);
2213 bApnd = 0;
2214 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
2215 if( x.oom ){
2216 sqlite3_result_error_nomem(ctx);
2217 goto jsonSetDone;
2218 }else if( x.nErr ){
2219 goto jsonSetDone;
2220 }else if( pNode && (bApnd || bIsSet) ){
2221 pNode->jnFlags |= (u8)JNODE_REPLACE;
2222 pNode->u.iReplace = i + 1;
2223 }
2224 }
2225 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
2226 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
2227 }else{
2228 jsonReturnJson(x.aNode, ctx, argv);
2229 }
2230jsonSetDone:
2231 jsonParseReset(&x);
2232}
2233
2234/*
2235** json_type(JSON) -> json_type(BUCKET, OBJECT, VERSION)
2236** json_type(JSON, PATH) -> json_type(BUCKET, OBJECT, VERSION, PATH)
2237**
2238** Return the top-level "type" of a JSON string. Throw an error if
2239** either the JSON or PATH inputs are not well-formed.
2240*/
2241static void jsonTypeFunc(
2242 sqlite3_context *ctx,
2243 int argc,
2244 sqlite3_value **argv //argv[0] -> bucket, argv[1] -> object, argv[2] -> version
2245){
2246 JsonParse *p; /* The parse */
2247 const char *zPath;
2248 JsonNode *pNode;
2249
2250 p = jsonParseCached(ctx, argv, ctx);
2251 if( p==0 ) return;
2252 if( argc==4 ){
2253 zPath = (const char*)sqlite3_value_text(argv[3]);
2254 pNode = jsonLookup(p, zPath, 0, ctx);
2255 }else{
2256 pNode = p->aNode;
2257 }
2258 if( pNode ){
2259 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
2260 }
2261}
2262
2263/*
2264** json_valid(JSON) -> json_valid(BUCKET, OBJECT, VERSION)
2265**
2266** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
2267** Return 0 otherwise.
2268*/
2269static void jsonValidFunc(
2270 sqlite3_context *ctx,
2271 int argc,
2272 sqlite3_value **argv
2273){
2274 JsonParse *p; /* The parse */
2275 UNUSED_PARAM(argc);
2276 p = jsonParseCached(ctx, argv, 0);
2277 sqlite3_result_int(ctx, p!=0);
2278}
2279
2280
2281/****************************************************************************
2282** Aggregate SQL function implementations
2283****************************************************************************/
2284/*
2285** json_group_array(VALUE)
2286**
2287** Return a JSON array composed of all values in the aggregate.
2288*/
2289static void jsonArrayStep(
2290 sqlite3_context *ctx,
2291 int argc,
2292 sqlite3_value **argv
2293){
2294 JsonString *pStr;
2295 UNUSED_PARAM(argc);
2296 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2297 if( pStr ){
2298 if( pStr->zBuf==0 ){
2299 jsonInit(pStr, ctx);
2300 jsonAppendChar(pStr, '[');
2301 }else if( pStr->nUsed>1 ){
2302 jsonAppendChar(pStr, ',');
2303 }
2304 pStr->pCtx = ctx;
2305 jsonAppendValue(pStr, argv[0]);
2306 }
2307}
2308static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
2309 JsonString *pStr;
2310 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2311 if( pStr ){
2312 pStr->pCtx = ctx;
2313 jsonAppendChar(pStr, ']');
2314 if( pStr->bErr ){
2315 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
2316 assert( pStr->bStatic );
2317 }else if( isFinal ){
2318 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
2319 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2320 pStr->bStatic = 1;
2321 }else{
2322 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
2323 pStr->nUsed--;
2324 }
2325 }else{
2326 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
2327 }
2328 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2329}
2330static void jsonArrayValue(sqlite3_context *ctx){
2331 jsonArrayCompute(ctx, 0);
2332}
2333static void jsonArrayFinal(sqlite3_context *ctx){
2334 jsonArrayCompute(ctx, 1);
2335}
2336
2337#ifndef SQLITE_OMIT_WINDOWFUNC
2338/*
2339** This method works for both json_group_array() and json_group_object().
2340** It works by removing the first element of the group by searching forward
2341** to the first comma (",") that is not within a string and deleting all
2342** text through that comma.
2343*/
2344static void jsonGroupInverse(
2345 sqlite3_context *ctx,
2346 int argc,
2347 sqlite3_value **argv
2348){
2349 unsigned int i;
2350 int inStr = 0;
2351 int nNest = 0;
2352 char *z;
2353 char c;
2354 JsonString *pStr;
2355 UNUSED_PARAM(argc);
2356 UNUSED_PARAM(argv);
2357 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2358#ifdef NEVER
2359 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2360 ** always have been called to initalize it */
2361 if( NEVER(!pStr) ) return;
2362#endif
2363 z = pStr->zBuf;
2364 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
2365 if( c=='"' ){
2366 inStr = !inStr;
2367 }else if( c=='\\' ){
2368 i++;
2369 }else if( !inStr ){
2370 if( c=='{' || c=='[' ) nNest++;
2371 if( c=='}' || c==']' ) nNest--;
2372 }
2373 }
2374 if( i<pStr->nUsed ){
2375 pStr->nUsed -= i;
2376 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
2377 z[pStr->nUsed] = 0;
2378 }else{
2379 pStr->nUsed = 1;
2380 }
2381}
2382#else
2383# define jsonGroupInverse 0
2384#endif
2385
2386
2387/*
2388** json_group_obj(NAME,VALUE)
2389**
2390** Return a JSON object composed of all names and values in the aggregate.
2391*/
2392static void jsonObjectStep(
2393 sqlite3_context *ctx,
2394 int argc,
2395 sqlite3_value **argv
2396){
2397 JsonString *pStr;
2398 const char *z;
2399 u32 n;
2400 UNUSED_PARAM(argc);
2401 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2402 if( pStr ){
2403 if( pStr->zBuf==0 ){
2404 jsonInit(pStr, ctx);
2405 jsonAppendChar(pStr, '{');
2406 }else if( pStr->nUsed>1 ){
2407 jsonAppendChar(pStr, ',');
2408 pStr->pCtx = ctx;
2409 }
2410 z = (const char*)sqlite3_value_text(argv[0]);
2411 n = (u32)sqlite3_value_bytes(argv[0]);
2412 jsonAppendString(pStr, z, n);
2413 jsonAppendChar(pStr, ':');
2414 jsonAppendValue(pStr, argv[1]);
2415 }
2416}
2417static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
2418 JsonString *pStr;
2419 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2420 if( pStr ){
2421 jsonAppendChar(pStr, '}');
2422 if( pStr->bErr ){
2423 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
2424 assert( pStr->bStatic );
2425 }else if( isFinal ){
2426 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
2427 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2428 pStr->bStatic = 1;
2429 }else{
2430 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
2431 pStr->nUsed--;
2432 }
2433 }else{
2434 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
2435 }
2436 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2437}
2438static void jsonObjectValue(sqlite3_context *ctx){
2439 jsonObjectCompute(ctx, 0);
2440}
2441static void jsonObjectFinal(sqlite3_context *ctx){
2442 jsonObjectCompute(ctx, 1);
2443}
2444
2445
2446
2447#ifndef SQLITE_OMIT_VIRTUALTABLE
2448/****************************************************************************
2449** The json_each virtual table
2450****************************************************************************/
2451typedef struct JsonEachCursor JsonEachCursor;
2452struct JsonEachCursor {
2453 sqlite3_vtab_cursor base; /* Base class - must be first */
2454 u32 iRowid; /* The rowid */
2455 u32 iBegin; /* The first node of the scan */
2456 u32 i; /* Index in sParse.aNode[] of current row */
2457 u32 iEnd; /* EOF when i equals or exceeds this value */
2458 u8 eType; /* Type of top-level element */
2459 u8 bRecursive; /* True for json_tree(). False for json_each() */
2460 //char *zJson; /* Input JSON */
2461 char *zRoot; /* Path by which to filter zJson */
2462 JsonParse sParse; /* Parse of the input JSON */
2463};
2464
2465/* Constructor for the json_each virtual table */
2466static int jsonEachConnect(
2467 sqlite3 *db,
2468 void *pAux,
2469 int argc, const char *const*argv,
2470 sqlite3_vtab **ppVtab,
2471 char **pzErr
2472){
2473 sqlite3_vtab *pNew;
2474 int rc;
2475/* Column numbers */
2476#define JEACH_KEY 0
2477#define JEACH_VALUE 1
2478#define JEACH_TYPE 2
2479#define JEACH_ATOM 3
2480#define JEACH_ID 4
2481#define JEACH_PARENT 5
2482#define JEACH_FULLKEY 6
2483#define JEACH_PATH 7
2484/* The xBestIndex method assumes that the JSON and ROOT columns are
2485** the last two columns in the table. Should this ever changes, be
2486** sure to update the xBestIndex method. */
2487#define JEACH_JSON 8
2488#define JEACH_ROOT 9
2489
2490 UNUSED_PARAM(pzErr);
2491 UNUSED_PARAM(argv);
2492 UNUSED_PARAM(argc);
2493 UNUSED_PARAM(pAux);
2494 rc = sqlite3_declare_vtab(db,
2495 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2496 "json HIDDEN,root HIDDEN)");
2497 if( rc==SQLITE_OK ){
2498 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
2499 if( pNew==0 ) return SQLITE_NOMEM;
2500 memset(pNew, 0, sizeof(*pNew));
2501 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
2502 }
2503 return rc;
2504}
2505
2506/* destructor for json_each virtual table */
2507static int jsonEachDisconnect(sqlite3_vtab *pVtab){
2508 sqlite3_free(pVtab);
2509 return SQLITE_OK;
2510}
2511
2512/* constructor for a JsonEachCursor object for json_each(). */
2513static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2514 JsonEachCursor *pCur;
2515
2516 UNUSED_PARAM(p);
2517 pCur = sqlite3_malloc( sizeof(*pCur) );
2518 if( pCur==0 ) return SQLITE_NOMEM;
2519 memset(pCur, 0, sizeof(*pCur));
2520 *ppCursor = &pCur->base;
2521 return SQLITE_OK;
2522}
2523
2524/* constructor for a JsonEachCursor object for json_tree(). */
2525static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2526 int rc = jsonEachOpenEach(p, ppCursor);
2527 if( rc==SQLITE_OK ){
2528 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
2529 pCur->bRecursive = 1;
2530 }
2531 return rc;
2532}
2533
2534/* Reset a JsonEachCursor back to its original state. Free any memory
2535** held. */
2536static void jsonEachCursorReset(JsonEachCursor *p){
2537 //sqlite3_free(p->zJson);
2538 sqlite3_free(p->zRoot);
2539 jsonParseReset(&p->sParse);
2540 p->iRowid = 0;
2541 p->i = 0;
2542 p->iEnd = 0;
2543 p->eType = 0;
2544 //p->zJson = 0;
2545 p->zRoot = 0;
2546}
2547
2548/* Destructor for a jsonEachCursor object */
2549static int jsonEachClose(sqlite3_vtab_cursor *cur){
2550 JsonEachCursor *p = (JsonEachCursor*)cur;
2551 jsonEachCursorReset(p);
2552 sqlite3_free(cur);
2553 return SQLITE_OK;
2554}
2555
2556/* Return TRUE if the jsonEachCursor object has been advanced off the end
2557** of the JSON object */
2558static int jsonEachEof(sqlite3_vtab_cursor *cur){
2559 JsonEachCursor *p = (JsonEachCursor*)cur;
2560 return p->i >= p->iEnd;
2561}
2562
2563/* Advance the cursor to the next element for json_tree() */
2564static int jsonEachNext(sqlite3_vtab_cursor *cur){
2565 JsonEachCursor *p = (JsonEachCursor*)cur;
2566 if( p->bRecursive ){
2567 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
2568 p->i++;
2569 p->iRowid++;
2570 if( p->i<p->iEnd ){
2571 u32 iUp = p->sParse.aUp[p->i];
2572 JsonNode *pUp = &p->sParse.aNode[iUp];
2573 p->eType = pUp->eType;
2574 if( pUp->eType==JSON_ARRAY ){
2575 if( iUp==p->i-1 ){
2576 pUp->u.iKey = 0;
2577 }else{
2578 pUp->u.iKey++;
2579 }
2580 }
2581 }
2582 }else{
2583 switch( p->eType ){
2584 case JSON_ARRAY: {
2585 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
2586 p->iRowid++;
2587 break;
2588 }
2589 case JSON_OBJECT: {
2590 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
2591 p->iRowid++;
2592 break;
2593 }
2594 default: {
2595 p->i = p->iEnd;
2596 break;
2597 }
2598 }
2599 }
2600 return SQLITE_OK;
2601}
2602
2603/* Append the name of the path for element i to pStr
2604*/
2605static void jsonEachComputePath(
2606 JsonEachCursor *p, /* The cursor */
2607 JsonString *pStr, /* Write the path here */
2608 u32 i /* Path to this element */
2609){
2610 JsonNode *pNode, *pUp;
2611 u32 iUp;
2612 if( i==0 ){
2613 jsonAppendChar(pStr, '$');
2614 return;
2615 }
2616 iUp = p->sParse.aUp[i];
2617 jsonEachComputePath(p, pStr, iUp);
2618 pNode = &p->sParse.aNode[i];
2619 pUp = &p->sParse.aNode[iUp];
2620 if( pUp->eType==JSON_ARRAY ){
2621 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
2622 }else{
2623 assert( pUp->eType==JSON_OBJECT );
2624 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
2625 assert( pNode->eType==JSON_STRING );
2626 assert( pNode->jnFlags & JNODE_LABEL );
2627 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
2628 }
2629}
2630
2631/* Return the value of a column */
2632static int jsonEachColumn(
2633 sqlite3_vtab_cursor *cur, /* The cursor */
2634 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2635 int i /* Which column to return */
2636){
2637 JsonEachCursor *p = (JsonEachCursor*)cur;
2638 JsonNode *pThis = &p->sParse.aNode[p->i];
2639 switch( i ){
2640 case JEACH_KEY: {
2641 if( p->i==0 ) break;
2642 if( p->eType==JSON_OBJECT ){
2643 jsonReturn(pThis, ctx, 0);
2644 }else if( p->eType==JSON_ARRAY ){
2645 u32 iKey;
2646 if( p->bRecursive ){
2647 if( p->iRowid==0 ) break;
2648 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
2649 }else{
2650 iKey = p->iRowid;
2651 }
2652 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
2653 }
2654 break;
2655 }
2656 case JEACH_VALUE: {
2657 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
2658 jsonReturn(pThis, ctx, 0);
2659 break;
2660 }
2661 case JEACH_TYPE: {
2662 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
2663 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2664 break;
2665 }
2666 case JEACH_ATOM: {
2667 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
2668 if( pThis->eType>=JSON_ARRAY ) break;
2669 jsonReturn(pThis, ctx, 0);
2670 break;
2671 }
2672 case JEACH_ID: {
2673 sqlite3_result_int64(ctx,
2674 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
2675 break;
2676 }
2677 case JEACH_PARENT: {
2678 if( p->i>p->iBegin && p->bRecursive ){
2679 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
2680 }
2681 break;
2682 }
2683 case JEACH_FULLKEY: {
2684 JsonString x;
2685 jsonInit(&x, ctx);
2686 if( p->bRecursive ){
2687 jsonEachComputePath(p, &x, p->i);
2688 }else{
2689 if( p->zRoot ){
2690 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
2691 }else{
2692 jsonAppendChar(&x, '$');
2693 }
2694 if( p->eType==JSON_ARRAY ){
2695 jsonPrintf(30, &x, "[%d]", p->iRowid);
2696 }else if( p->eType==JSON_OBJECT ){
2697 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2698 }
2699 }
2700 jsonResult(&x);
2701 break;
2702 }
2703 case JEACH_PATH: {
2704 if( p->bRecursive ){
2705 JsonString x;
2706 jsonInit(&x, ctx);
2707 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2708 jsonResult(&x);
2709 break;
2710 }
2711 /* For json_each() path and root are the same so fall through
2712 ** into the root case */
2713 /* no break */ deliberate_fall_through
2714 }
2715 default: {
2716 const char *zRoot = p->zRoot;
2717 if( zRoot==0 ) zRoot = "$";
2718 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
2719 break;
2720 }
2721 case JEACH_JSON: {
2722 assert( i==JEACH_JSON );
2723 char d = json_getc_next(p->sParse.srdr);
2724 sqlite3_result_text(ctx, &d, -1, SQLITE_STATIC);
2725 break;
2726 }
2727 }
2728 return SQLITE_OK;
2729}
2730
2731/* Return the current rowid value */
2732static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2733 JsonEachCursor *p = (JsonEachCursor*)cur;
2734 *pRowid = p->iRowid;
2735 return SQLITE_OK;
2736}
2737
2738/* The query strategy is to look for an equality constraint on the json
2739** column. Without such a constraint, the table cannot operate. idxNum is
2740** 1 if the constraint is found, 3 if the constraint and zRoot are found,
2741** and 0 otherwise.
2742*/
2743static int jsonEachBestIndex(
2744 sqlite3_vtab *tab,
2745 sqlite3_index_info *pIdxInfo
2746){
2747 int i; /* Loop counter or computed array index */
2748 int aIdx[2]; /* Index of constraints for JSON and ROOT */
2749 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
2750 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
2751 const struct sqlite3_index_constraint *pConstraint;
2752
2753 /* This implementation assumes that JSON and ROOT are the last two
2754 ** columns in the table */
2755 assert( JEACH_ROOT == JEACH_JSON+1 );
2756 UNUSED_PARAM(tab);
2757 aIdx[0] = aIdx[1] = -1;
2758 pConstraint = pIdxInfo->aConstraint;
2759 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2760 int iCol;
2761 int iMask;
2762 if( pConstraint->iColumn < JEACH_JSON ) continue;
2763 iCol = pConstraint->iColumn - JEACH_JSON;
2764 assert( iCol==0 || iCol==1 );
2765 iMask = 1 << iCol;
2766 if( pConstraint->usable==0 ){
2767 unusableMask |= iMask;
2768 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
2769 aIdx[iCol] = i;
2770 idxMask |= iMask;
2771 }
2772 }
2773 if( (unusableMask & ~idxMask)!=0 ){
2774 /* If there are any unusable constraints on JSON or ROOT, then reject
2775 ** this entire plan */
2776 return SQLITE_CONSTRAINT;
2777 }
2778 if( aIdx[0]<0 ){
2779 /* No JSON input. Leave estimatedCost at the huge value that it was
2780 ** initialized to to discourage the query planner from selecting this
2781 ** plan. */
2782 pIdxInfo->idxNum = 0;
2783 }else{
2784 pIdxInfo->estimatedCost = 1.0;
2785 i = aIdx[0];
2786 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
2787 pIdxInfo->aConstraintUsage[i].omit = 1;
2788 if( aIdx[1]<0 ){
2789 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
2790 }else{
2791 i = aIdx[1];
2792 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
2793 pIdxInfo->aConstraintUsage[i].omit = 1;
2794 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
2795 }
2796 }
2797 return SQLITE_OK;
2798}
2799
2800/* Start a search on a new JSON string */
2801//new code
2802//argv[0] will be bucket, argv[1] will be object, argv[2] will be version
2803//new code
2804static int jsonEachFilter(
2805 sqlite3_vtab_cursor *cur,
2806 int idxNum, const char *idxStr,
2807 int argc, sqlite3_value **argv
2808){
2809 JsonEachCursor *p = (JsonEachCursor*)cur;
2810 // const char *z;
2811 const char *zRoot = 0;
2812 sqlite3_int64 n;
2813
2814 UNUSED_PARAM(idxStr);
2815 UNUSED_PARAM(argc);
2816 jsonEachCursorReset(p);
2817 if( idxNum==0 ) return SQLITE_OK;
2818 //new code
2819 JsonReader srdr;
2820 memset(&srdr, 0, sizeof(srdr));
2821 json_reader_open(&srdr,BUCKET,OBJECT,VERSION);
2822 //new code
2823 //TO DO -> devise a method to avoid memcpy, maybe change jsonParse function even its arguements too.
2824 /*z = (const char*)sqlite3_value_text(argv[0]);
2825 if( z==0 ) return SQLITE_OK;
2826 n = sqlite3_value_bytes(argv[0]);
2827 p->zJson = sqlite3_malloc64( n+1 );
2828 if( p->zJson==0 ) return SQLITE_NOMEM;
2829 memcpy(p->zJson, z, (size_t)n+1);
2830 if( jsonParse(&p->sParse, 0, p->zJson) ){*/
2831 if( jsonParse(&p->sParse, 0, &srdr) ){
2832 int rc = SQLITE_NOMEM;
2833 if( p->sParse.oom==0 ){
2834 sqlite3_free(cur->pVtab->zErrMsg);
2835 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2836 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2837 }
2838 jsonEachCursorReset(p);
2839 return rc;
2840 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2841 jsonEachCursorReset(p);
2842 return SQLITE_NOMEM;
2843 }else{
2844 JsonNode *pNode = 0;
2845 if( idxNum==3 ){
2846 const char *zErr = 0;
2847 zRoot = (const char*)sqlite3_value_text(argv[3]);
2848 if( zRoot==0 ) return SQLITE_OK;
2849 n = sqlite3_value_bytes(argv[3]);
2850 p->zRoot = sqlite3_malloc64( n+1 );
2851 if( p->zRoot==0 ) return SQLITE_NOMEM;
2852 memcpy(p->zRoot, zRoot, (size_t)n+1);
2853 if( zRoot[0]!='$' ){
2854 zErr = zRoot;
2855 }else{
2856 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2857 }
2858 if( zErr ){
2859 sqlite3_free(cur->pVtab->zErrMsg);
2860 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
2861 jsonEachCursorReset(p);
2862 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2863 }else if( pNode==0 ){
2864 return SQLITE_OK;
2865 }
2866 }else{
2867 pNode = p->sParse.aNode;
2868 }
2869 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
2870 p->eType = pNode->eType;
2871 if( p->eType>=JSON_ARRAY ){
2872 pNode->u.iKey = 0;
2873 p->iEnd = p->i + pNode->n + 1;
2874 if( p->bRecursive ){
2875 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
2876 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2877 p->i--;
2878 }
2879 }else{
2880 p->i++;
2881 }
2882 }else{
2883 p->iEnd = p->i+1;
2884 }
2885 }
2886 return SQLITE_OK;
2887}
2888
2889/* The methods of the json_each virtual table */
2890static sqlite3_module jsonEachModule = {
2891 0, /* iVersion */
2892 0, /* xCreate */
2893 jsonEachConnect, /* xConnect */
2894 jsonEachBestIndex, /* xBestIndex */
2895 jsonEachDisconnect, /* xDisconnect */
2896 0, /* xDestroy */
2897 jsonEachOpenEach, /* xOpen - open a cursor */
2898 jsonEachClose, /* xClose - close a cursor */
2899 jsonEachFilter, /* xFilter - configure scan constraints */
2900 jsonEachNext, /* xNext - advance a cursor */
2901 jsonEachEof, /* xEof - check for end of scan */
2902 jsonEachColumn, /* xColumn - read data */
2903 jsonEachRowid, /* xRowid - read data */
2904 0, /* xUpdate */
2905 0, /* xBegin */
2906 0, /* xSync */
2907 0, /* xCommit */
2908 0, /* xRollback */
2909 0, /* xFindMethod */
2910 0, /* xRename */
2911 0, /* xSavepoint */
2912 0, /* xRelease */
2913 0, /* xRollbackTo */
2914 0 /* xShadowName */
2915};
2916
2917/* The methods of the json_tree virtual table. */
2918static sqlite3_module jsonTreeModule = {
2919 0, /* iVersion */
2920 0, /* xCreate */
2921 jsonEachConnect, /* xConnect */
2922 jsonEachBestIndex, /* xBestIndex */
2923 jsonEachDisconnect, /* xDisconnect */
2924 0, /* xDestroy */
2925 jsonEachOpenTree, /* xOpen - open a cursor */
2926 jsonEachClose, /* xClose - close a cursor */
2927 jsonEachFilter, /* xFilter - configure scan constraints */
2928 jsonEachNext, /* xNext - advance a cursor */
2929 jsonEachEof, /* xEof - check for end of scan */
2930 jsonEachColumn, /* xColumn - read data */
2931 jsonEachRowid, /* xRowid - read data */
2932 0, /* xUpdate */
2933 0, /* xBegin */
2934 0, /* xSync */
2935 0, /* xCommit */
2936 0, /* xRollback */
2937 0, /* xFindMethod */
2938 0, /* xRename */
2939 0, /* xSavepoint */
2940 0, /* xRelease */
2941 0, /* xRollbackTo */
2942 0 /* xShadowName */
2943};
2944#endif /* SQLITE_OMIT_VIRTUALTABLE */
2945
2946/****************************************************************************
2947** The following routines are the only publically visible identifiers in this
2948** file. Call the following routines in order to register the various SQL
2949** functions and the virtual table implemented by this file.
2950****************************************************************************/
2951
2952int sqlite3Json1Init(sqlite3 *db){
2953 int rc = SQLITE_OK;
2954 unsigned int i;
2955 static const struct {
2956 const char *zName;
2957 int nArg;
2958 int flag;
2959 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2960 } aFunc[] = {
2961 { "json", 1, 0, jsonRemoveFunc },
2962 { "json_array", -1, 0, jsonArrayFunc },
2963 { "json_array_length", 3, 0, jsonArrayLengthFunc },
2964 { "json_array_length", 4, 0, jsonArrayLengthFunc },
2965 { "json_extract", -1, 0, jsonExtractFunc },
2966 { "json_insert", -1, 0, jsonSetFunc },
2967 { "json_object", -1, 0, jsonObjectFunc },
2968 { "json_patch", 6, 0, jsonPatchFunc },
2969 { "json_quote", 1, 0, jsonQuoteFunc },
2970 { "json_remove", -1, 0, jsonRemoveFunc },
2971 { "json_replace", -1, 0, jsonReplaceFunc },
2972 { "json_set", -1, 1, jsonSetFunc },
2973 { "json_type", 3, 0, jsonTypeFunc },
2974 { "json_type", 4, 0, jsonTypeFunc },
2975 { "json_valid", 3, 0, jsonValidFunc },
2976
2977#if SQLITE_DEBUG
2978 /* DEBUG and TESTING functions */
2979 { "json_parse", 1, 0, jsonParseFunc },
2980 { "json_test1", 1, 0, jsonTest1Func },
2981#endif
2982 };
2983 static const struct {
2984 const char *zName;
2985 int nArg;
2986 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2987 void (*xFinal)(sqlite3_context*);
2988 void (*xValue)(sqlite3_context*);
2989 } aAgg[] = {
2990 { "json_group_array", 1,
2991 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
2992 { "json_group_object", 2,
2993 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
2994 };
2995#ifndef SQLITE_OMIT_VIRTUALTABLE
2996 static const struct {
2997 const char *zName;
2998 sqlite3_module *pModule;
2999 } aMod[] = {
3000 { "json_each", &jsonEachModule },
3001 { "json_tree", &jsonTreeModule },
3002 };
3003#endif
3004 static const int enc =
3005 SQLITE_UTF8 |
3006 SQLITE_DETERMINISTIC |
3007 SQLITE_INNOCUOUS;
3008 //new code
3009 /*
3010 rc = sqlite3_create_function(db, "readobject", 3,
3011 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3012 readobjFunc, 0, 0);
3013 */
3014 //new code
3015 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
3016 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
3017 (void*)&aFunc[i].flag,
3018 aFunc[i].xFunc, 0, 0);
3019 }
3020#ifndef SQLITE_OMIT_WINDOWFUNC
3021 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
3022 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
3023 SQLITE_SUBTYPE | enc, 0,
3024 aAgg[i].xStep, aAgg[i].xFinal,
3025 aAgg[i].xValue, jsonGroupInverse, 0);
3026 }
3027#endif
3028#ifndef SQLITE_OMIT_VIRTUALTABLE
3029 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
3030 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
3031 }
3032#endif
3033 return rc;
3034}
3035
3036
3037#ifndef SQLITE_CORE
3038#ifdef _WIN32
3039__declspec(dllexport)
3040#endif
3041int sqlite3_json_init(
3042 sqlite3 *db,
3043 char **pzErrMsg,
3044 const sqlite3_api_routines *pApi
3045){
3046 SQLITE_EXTENSION_INIT2(pApi);
3047 (void)pzErrMsg; /* Unused parameter */
3048 return sqlite3Json1Init(db);
3049}
3050#endif
3051#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
3052