blob: a9452b019107b16bb282d39c246ed25c8a0220ae [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)
drhf2df7e72015-08-28 20:07:40 +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 */
52# define safe_isdigit(x) sqlite3Isdigit(x)
53# define safe_isalnum(x) sqlite3Isalnum(x)
54#else
55 /* Use the standard library for separate compilation */
56#include <ctype.h> /* amalgamator: keep */
57# define safe_isdigit(x) isdigit((unsigned char)(x))
58# define safe_isalnum(x) isalnum((unsigned char)(x))
59#endif
dan2e8f5512015-09-17 17:21:09 +000060
drh95677942015-09-24 01:06:37 +000061/*
62** Growing our own isspace() routine this way is twice as fast as
63** the library isspace() function, resulting in a 7% overall performance
64** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
65*/
66static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000067 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 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};
84#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
85
drh9a4718f2015-10-10 14:00:37 +000086#ifndef SQLITE_AMALGAMATION
87 /* Unsigned integer types. These are already defined in the sqliteInt.h,
88 ** but the definitions need to be repeated for separate compilation. */
89 typedef sqlite3_uint64 u64;
90 typedef unsigned int u32;
91 typedef unsigned char u8;
92#endif
drh5fa5c102015-08-12 16:49:40 +000093
drh52216ad2015-08-18 02:28:03 +000094/* Objects */
drh505ad2c2015-08-21 17:33:11 +000095typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000096typedef struct JsonNode JsonNode;
97typedef struct JsonParse JsonParse;
98
drh5634cc02015-08-17 11:28:03 +000099/* An instance of this object represents a JSON string
100** under construction. Really, this is a generic string accumulator
101** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000102*/
drh505ad2c2015-08-21 17:33:11 +0000103struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000104 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000105 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000106 u64 nAlloc; /* Bytes of storage available in zBuf[] */
107 u64 nUsed; /* Bytes of zBuf[] currently used */
108 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000109 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000110 char zSpace[100]; /* Initial static space */
111};
112
drhe9c37f32015-08-15 21:25:36 +0000113/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000114*/
drhe9c37f32015-08-15 21:25:36 +0000115#define JSON_NULL 0
116#define JSON_TRUE 1
117#define JSON_FALSE 2
118#define JSON_INT 3
119#define JSON_REAL 4
120#define JSON_STRING 5
121#define JSON_ARRAY 6
122#define JSON_OBJECT 7
123
drhf5ddb9c2015-09-11 00:06:41 +0000124/* The "subtype" set for JSON values */
125#define JSON_SUBTYPE 74 /* Ascii for "J" */
126
drh987eb1f2015-08-17 15:17:37 +0000127/*
128** Names of the various JSON types:
129*/
130static const char * const jsonType[] = {
131 "null", "true", "false", "integer", "real", "text", "array", "object"
132};
133
drh301eecc2015-08-17 20:14:19 +0000134/* Bit values for the JsonNode.jnFlag field
135*/
136#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
137#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
138#define JNODE_REMOVE 0x04 /* Do not output */
drhd0960592015-08-17 21:22:32 +0000139#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh52216ad2015-08-18 02:28:03 +0000140#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
drhf5ddb9c2015-09-11 00:06:41 +0000141#define JNODE_LABEL 0x20 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000142
drh987eb1f2015-08-17 15:17:37 +0000143
drhe9c37f32015-08-15 21:25:36 +0000144/* A single node of parsed JSON
145*/
drhe9c37f32015-08-15 21:25:36 +0000146struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000147 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000148 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +0000149 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +0000150 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000151 union {
drh0042a972015-08-18 12:59:58 +0000152 const char *zJContent; /* Content for INT, REAL, and STRING */
153 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +0000154 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh52216ad2015-08-18 02:28:03 +0000155 } u;
drhe9c37f32015-08-15 21:25:36 +0000156};
157
158/* A completely parsed JSON string
159*/
drhe9c37f32015-08-15 21:25:36 +0000160struct JsonParse {
161 u32 nNode; /* Number of slots of aNode[] used */
162 u32 nAlloc; /* Number of slots of aNode[] allocated */
163 JsonNode *aNode; /* Array of nodes containing the parse */
164 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000165 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000166 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000167 u8 nErr; /* Number of errors seen */
drhe9c37f32015-08-15 21:25:36 +0000168};
169
drh505ad2c2015-08-21 17:33:11 +0000170/**************************************************************************
171** Utility routines for dealing with JsonString objects
172**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000173
drh505ad2c2015-08-21 17:33:11 +0000174/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000175*/
drh505ad2c2015-08-21 17:33:11 +0000176static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000177 p->zBuf = p->zSpace;
178 p->nAlloc = sizeof(p->zSpace);
179 p->nUsed = 0;
180 p->bStatic = 1;
181}
182
drh505ad2c2015-08-21 17:33:11 +0000183/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000184*/
drh505ad2c2015-08-21 17:33:11 +0000185static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000186 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000187 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000188 jsonZero(p);
189}
190
191
drh505ad2c2015-08-21 17:33:11 +0000192/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000193** initial state.
194*/
drh505ad2c2015-08-21 17:33:11 +0000195static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000196 if( !p->bStatic ) sqlite3_free(p->zBuf);
197 jsonZero(p);
198}
199
200
201/* Report an out-of-memory (OOM) condition
202*/
drh505ad2c2015-08-21 17:33:11 +0000203static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000204 p->bErr = 1;
205 sqlite3_result_error_nomem(p->pCtx);
206 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000207}
208
209/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
210** Return zero on success. Return non-zero on an OOM error
211*/
drh505ad2c2015-08-21 17:33:11 +0000212static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000213 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000214 char *zNew;
215 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000216 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000217 zNew = sqlite3_malloc64(nTotal);
218 if( zNew==0 ){
219 jsonOom(p);
220 return SQLITE_NOMEM;
221 }
drh6fd5c1e2015-08-21 20:37:12 +0000222 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000223 p->zBuf = zNew;
224 p->bStatic = 0;
225 }else{
226 zNew = sqlite3_realloc64(p->zBuf, nTotal);
227 if( zNew==0 ){
228 jsonOom(p);
229 return SQLITE_NOMEM;
230 }
231 p->zBuf = zNew;
232 }
233 p->nAlloc = nTotal;
234 return SQLITE_OK;
235}
236
drh505ad2c2015-08-21 17:33:11 +0000237/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000238*/
drh505ad2c2015-08-21 17:33:11 +0000239static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000240 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
241 memcpy(p->zBuf+p->nUsed, zIn, N);
242 p->nUsed += N;
243}
244
drh4af352d2015-08-21 20:02:48 +0000245/* Append formatted text (not to exceed N bytes) to the JsonString.
246*/
247static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
248 va_list ap;
249 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
250 va_start(ap, zFormat);
251 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
252 va_end(ap);
253 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
254}
255
drh5634cc02015-08-17 11:28:03 +0000256/* Append a single character
257*/
drh505ad2c2015-08-21 17:33:11 +0000258static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000259 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
260 p->zBuf[p->nUsed++] = c;
261}
262
drh301eecc2015-08-17 20:14:19 +0000263/* Append a comma separator to the output buffer, if the previous
264** character is not '[' or '{'.
265*/
drh505ad2c2015-08-21 17:33:11 +0000266static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000267 char c;
268 if( p->nUsed==0 ) return;
269 c = p->zBuf[p->nUsed-1];
270 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
271}
272
drh505ad2c2015-08-21 17:33:11 +0000273/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000274** under construction. Enclose the string in "..." and escape
275** any double-quotes or backslash characters contained within the
276** string.
277*/
drh505ad2c2015-08-21 17:33:11 +0000278static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000279 u32 i;
280 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
281 p->zBuf[p->nUsed++] = '"';
282 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000283 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000284 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000285 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000286 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000287 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000288 }else if( c<=0x1f ){
289 static const char aSpecial[] = {
290 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
292 };
293 assert( sizeof(aSpecial)==32 );
294 assert( aSpecial['\b']=='b' );
295 assert( aSpecial['\f']=='f' );
296 assert( aSpecial['\n']=='n' );
297 assert( aSpecial['\r']=='r' );
298 assert( aSpecial['\t']=='t' );
299 if( aSpecial[c] ){
300 c = aSpecial[c];
301 goto json_simple_escape;
302 }
303 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
304 p->zBuf[p->nUsed++] = '\\';
305 p->zBuf[p->nUsed++] = 'u';
306 p->zBuf[p->nUsed++] = '0';
307 p->zBuf[p->nUsed++] = '0';
308 p->zBuf[p->nUsed++] = '0' + (c>>4);
309 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000310 }
311 p->zBuf[p->nUsed++] = c;
312 }
313 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000314 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000315}
316
drhd0960592015-08-17 21:22:32 +0000317/*
318** Append a function parameter value to the JSON string under
319** construction.
320*/
321static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000322 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000323 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000324){
325 switch( sqlite3_value_type(pValue) ){
326 case SQLITE_NULL: {
327 jsonAppendRaw(p, "null", 4);
328 break;
329 }
330 case SQLITE_INTEGER:
331 case SQLITE_FLOAT: {
332 const char *z = (const char*)sqlite3_value_text(pValue);
333 u32 n = (u32)sqlite3_value_bytes(pValue);
334 jsonAppendRaw(p, z, n);
335 break;
336 }
337 case SQLITE_TEXT: {
338 const char *z = (const char*)sqlite3_value_text(pValue);
339 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000340 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000341 jsonAppendRaw(p, z, n);
342 }else{
343 jsonAppendString(p, z, n);
344 }
drhd0960592015-08-17 21:22:32 +0000345 break;
346 }
347 default: {
348 if( p->bErr==0 ){
349 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000350 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000351 jsonReset(p);
352 }
353 break;
354 }
355 }
356}
357
358
drhbd0621b2015-08-13 13:54:59 +0000359/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000360*/
drh505ad2c2015-08-21 17:33:11 +0000361static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000362 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000363 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
364 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
365 SQLITE_UTF8);
366 jsonZero(p);
367 }
368 assert( p->bStatic );
369}
370
drh505ad2c2015-08-21 17:33:11 +0000371/**************************************************************************
372** Utility routines for dealing with JsonNode and JsonParse objects
373**************************************************************************/
374
375/*
376** Return the number of consecutive JsonNode slots need to represent
377** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
378** OBJECT types, the number might be larger.
379**
380** Appended elements are not counted. The value returned is the number
381** by which the JsonNode counter should increment in order to go to the
382** next peer value.
383*/
384static u32 jsonNodeSize(JsonNode *pNode){
385 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
386}
387
388/*
389** Reclaim all memory allocated by a JsonParse object. But do not
390** delete the JsonParse object itself.
391*/
392static void jsonParseReset(JsonParse *pParse){
393 sqlite3_free(pParse->aNode);
394 pParse->aNode = 0;
395 pParse->nNode = 0;
396 pParse->nAlloc = 0;
397 sqlite3_free(pParse->aUp);
398 pParse->aUp = 0;
399}
400
drh5634cc02015-08-17 11:28:03 +0000401/*
402** Convert the JsonNode pNode into a pure JSON string and
403** append to pOut. Subsubstructure is also included. Return
404** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000405*/
drh52216ad2015-08-18 02:28:03 +0000406static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000407 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000408 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000409 sqlite3_value **aReplace /* Replacement values */
410){
drh5634cc02015-08-17 11:28:03 +0000411 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000412 default: {
413 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000414 jsonAppendRaw(pOut, "null", 4);
415 break;
416 }
417 case JSON_TRUE: {
418 jsonAppendRaw(pOut, "true", 4);
419 break;
420 }
421 case JSON_FALSE: {
422 jsonAppendRaw(pOut, "false", 5);
423 break;
424 }
425 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000426 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000427 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000428 break;
429 }
430 /* Fall through into the next case */
431 }
432 case JSON_REAL:
433 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000434 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000435 break;
436 }
437 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000438 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000439 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000440 for(;;){
441 while( j<=pNode->n ){
442 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
443 if( pNode[j].jnFlags & JNODE_REPLACE ){
444 jsonAppendSeparator(pOut);
drhf5ddb9c2015-09-11 00:06:41 +0000445 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
drh52216ad2015-08-18 02:28:03 +0000446 }
447 }else{
drhd0960592015-08-17 21:22:32 +0000448 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000449 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000450 }
drh505ad2c2015-08-21 17:33:11 +0000451 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000452 }
drh52216ad2015-08-18 02:28:03 +0000453 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
454 pNode = &pNode[pNode->u.iAppend];
455 j = 1;
drh5634cc02015-08-17 11:28:03 +0000456 }
457 jsonAppendChar(pOut, ']');
458 break;
459 }
460 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000461 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000462 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000463 for(;;){
464 while( j<=pNode->n ){
465 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
466 jsonAppendSeparator(pOut);
467 jsonRenderNode(&pNode[j], pOut, aReplace);
468 jsonAppendChar(pOut, ':');
469 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
drhf5ddb9c2015-09-11 00:06:41 +0000470 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
drh52216ad2015-08-18 02:28:03 +0000471 }else{
472 jsonRenderNode(&pNode[j+1], pOut, aReplace);
473 }
drhd0960592015-08-17 21:22:32 +0000474 }
drh505ad2c2015-08-21 17:33:11 +0000475 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000476 }
drh52216ad2015-08-18 02:28:03 +0000477 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
478 pNode = &pNode[pNode->u.iAppend];
479 j = 1;
drh5634cc02015-08-17 11:28:03 +0000480 }
481 jsonAppendChar(pOut, '}');
482 break;
483 }
drhbd0621b2015-08-13 13:54:59 +0000484 }
drh5634cc02015-08-17 11:28:03 +0000485}
486
487/*
drhf2df7e72015-08-28 20:07:40 +0000488** Return a JsonNode and all its descendents as a JSON string.
489*/
490static void jsonReturnJson(
491 JsonNode *pNode, /* Node to return */
492 sqlite3_context *pCtx, /* Return value for this function */
493 sqlite3_value **aReplace /* Array of replacement values */
494){
495 JsonString s;
496 jsonInit(&s, pCtx);
497 jsonRenderNode(pNode, &s, aReplace);
498 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000499 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000500}
501
502/*
drh5634cc02015-08-17 11:28:03 +0000503** Make the JsonNode the return value of the function.
504*/
drhd0960592015-08-17 21:22:32 +0000505static void jsonReturn(
506 JsonNode *pNode, /* Node to return */
507 sqlite3_context *pCtx, /* Return value for this function */
508 sqlite3_value **aReplace /* Array of replacement values */
509){
drh5634cc02015-08-17 11:28:03 +0000510 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000511 default: {
512 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000513 sqlite3_result_null(pCtx);
514 break;
515 }
516 case JSON_TRUE: {
517 sqlite3_result_int(pCtx, 1);
518 break;
519 }
520 case JSON_FALSE: {
521 sqlite3_result_int(pCtx, 0);
522 break;
523 }
drh987eb1f2015-08-17 15:17:37 +0000524 case JSON_INT: {
525 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000526 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000527 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000528 while( z[0]>='0' && z[0]<='9' ){
529 unsigned v = *(z++) - '0';
530 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000531 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000532 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
533 if( v==9 ) goto int_as_real;
534 if( v==8 ){
535 if( pNode->u.zJContent[0]=='-' ){
536 sqlite3_result_int64(pCtx, SMALLEST_INT64);
537 goto int_done;
538 }else{
539 goto int_as_real;
540 }
541 }
542 }
543 i = i*10 + v;
544 }
drh52216ad2015-08-18 02:28:03 +0000545 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000546 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000547 int_done:
548 break;
549 int_as_real: /* fall through to real */;
550 }
551 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000552 double r;
553#ifdef SQLITE_AMALGAMATION
554 const char *z = pNode->u.zJContent;
555 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
556#else
557 r = strtod(pNode->u.zJContent, 0);
558#endif
drh8deb4b82015-10-09 18:21:43 +0000559 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000560 break;
561 }
drh5634cc02015-08-17 11:28:03 +0000562 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000563#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
564 ** json_insert() and json_replace() and those routines do not
565 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000566 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000567 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
568 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000569 }else
570#endif
571 assert( (pNode->jnFlags & JNODE_RAW)==0 );
572 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000573 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000574 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000575 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000576 }else{
577 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000578 u32 i;
579 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000580 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000581 char *zOut;
582 u32 j;
583 zOut = sqlite3_malloc( n+1 );
584 if( zOut==0 ){
585 sqlite3_result_error_nomem(pCtx);
586 break;
587 }
588 for(i=1, j=0; i<n-1; i++){
589 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000590 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000591 zOut[j++] = c;
592 }else{
593 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000594 if( c=='u' ){
drh987eb1f2015-08-17 15:17:37 +0000595 u32 v = 0, k;
drh80d87402015-08-24 12:42:41 +0000596 for(k=0; k<4 && i<n-2; i++, k++){
drh8784eca2015-08-23 02:42:30 +0000597 c = z[i+1];
drh987eb1f2015-08-17 15:17:37 +0000598 if( c>='0' && c<='9' ) v = v*16 + c - '0';
599 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
600 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
601 else break;
drh987eb1f2015-08-17 15:17:37 +0000602 }
drh80d87402015-08-24 12:42:41 +0000603 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000604 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000605 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000606 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000607 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000608 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000609 }else{
mistachkin16a93122015-09-11 18:05:01 +0000610 zOut[j++] = (char)(0xe0 | (v>>12));
drh987eb1f2015-08-17 15:17:37 +0000611 zOut[j++] = 0x80 | ((v>>6)&0x3f);
612 zOut[j++] = 0x80 | (v&0x3f);
drh987eb1f2015-08-17 15:17:37 +0000613 }
614 }else{
615 if( c=='b' ){
616 c = '\b';
617 }else if( c=='f' ){
618 c = '\f';
619 }else if( c=='n' ){
620 c = '\n';
621 }else if( c=='r' ){
622 c = '\r';
623 }else if( c=='t' ){
624 c = '\t';
625 }
626 zOut[j++] = c;
627 }
628 }
629 }
630 zOut[j] = 0;
631 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000632 }
633 break;
634 }
635 case JSON_ARRAY:
636 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000637 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000638 break;
639 }
640 }
drhbd0621b2015-08-13 13:54:59 +0000641}
642
drh95677942015-09-24 01:06:37 +0000643/* Forward reference */
644static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
645
646/*
647** A macro to hint to the compiler that a function should not be
648** inlined.
649*/
650#if defined(__GNUC__)
651# define JSON_NOINLINE __attribute__((noinline))
652#elif defined(_MSC_VER) && _MSC_VER>=1310
653# define JSON_NOINLINE __declspec(noinline)
654#else
655# define JSON_NOINLINE
656#endif
657
658
659static JSON_NOINLINE int jsonParseAddNodeExpand(
660 JsonParse *pParse, /* Append the node to this object */
661 u32 eType, /* Node type */
662 u32 n, /* Content size or sub-node count */
663 const char *zContent /* Content */
664){
665 u32 nNew;
666 JsonNode *pNew;
667 assert( pParse->nNode>=pParse->nAlloc );
668 if( pParse->oom ) return -1;
669 nNew = pParse->nAlloc*2 + 10;
670 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
671 if( pNew==0 ){
672 pParse->oom = 1;
673 return -1;
674 }
675 pParse->nAlloc = nNew;
676 pParse->aNode = pNew;
677 assert( pParse->nNode<pParse->nAlloc );
678 return jsonParseAddNode(pParse, eType, n, zContent);
679}
680
drh5fa5c102015-08-12 16:49:40 +0000681/*
drhe9c37f32015-08-15 21:25:36 +0000682** Create a new JsonNode instance based on the arguments and append that
683** instance to the JsonParse. Return the index in pParse->aNode[] of the
684** new node, or -1 if a memory allocation fails.
685*/
686static int jsonParseAddNode(
687 JsonParse *pParse, /* Append the node to this object */
688 u32 eType, /* Node type */
689 u32 n, /* Content size or sub-node count */
690 const char *zContent /* Content */
691){
692 JsonNode *p;
693 if( pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000694 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000695 }
696 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000697 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000698 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000699 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000700 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000701 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000702 return pParse->nNode++;
703}
704
705/*
706** Parse a single JSON value which begins at pParse->zJson[i]. Return the
707** index of the first character past the end of the value parsed.
708**
709** Return negative for a syntax error. Special cases: return -2 if the
710** first non-whitespace character is '}' and return -3 if the first
711** non-whitespace character is ']'.
712*/
713static int jsonParseValue(JsonParse *pParse, u32 i){
714 char c;
715 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000716 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000717 int x;
drh852944e2015-09-10 03:29:11 +0000718 JsonNode *pNode;
dan2e8f5512015-09-17 17:21:09 +0000719 while( safe_isspace(pParse->zJson[i]) ){ i++; }
drh8cb15cc2015-09-24 01:40:45 +0000720 if( (c = pParse->zJson[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000721 /* Parse object */
722 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000723 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000724 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000725 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000726 x = jsonParseValue(pParse, j);
727 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000728 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000729 return -1;
730 }
drhbe9474e2015-08-22 03:05:54 +0000731 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000732 pNode = &pParse->aNode[pParse->nNode-1];
733 if( pNode->eType!=JSON_STRING ) return -1;
734 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000735 j = x;
dan2e8f5512015-09-17 17:21:09 +0000736 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000737 if( pParse->zJson[j]!=':' ) return -1;
738 j++;
739 x = jsonParseValue(pParse, j);
740 if( x<0 ) return -1;
741 j = x;
dan2e8f5512015-09-17 17:21:09 +0000742 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000743 c = pParse->zJson[j];
744 if( c==',' ) continue;
745 if( c!='}' ) return -1;
746 break;
747 }
drhbc8f0922015-08-22 19:39:04 +0000748 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000749 return j+1;
750 }else if( c=='[' ){
751 /* Parse array */
752 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000753 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000754 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000755 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000756 x = jsonParseValue(pParse, j);
757 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000758 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000759 return -1;
760 }
761 j = x;
dan2e8f5512015-09-17 17:21:09 +0000762 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000763 c = pParse->zJson[j];
764 if( c==',' ) continue;
765 if( c!=']' ) return -1;
766 break;
767 }
drhbc8f0922015-08-22 19:39:04 +0000768 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000769 return j+1;
770 }else if( c=='"' ){
771 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000772 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000773 j = i+1;
774 for(;;){
775 c = pParse->zJson[j];
776 if( c==0 ) return -1;
777 if( c=='\\' ){
778 c = pParse->zJson[++j];
779 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000780 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000781 }else if( c=='"' ){
782 break;
783 }
784 j++;
785 }
786 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drhbe9474e2015-08-22 03:05:54 +0000787 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000788 return j+1;
789 }else if( c=='n'
790 && strncmp(pParse->zJson+i,"null",4)==0
dan2e8f5512015-09-17 17:21:09 +0000791 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000792 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
793 return i+4;
794 }else if( c=='t'
795 && strncmp(pParse->zJson+i,"true",4)==0
dan2e8f5512015-09-17 17:21:09 +0000796 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000797 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
798 return i+4;
799 }else if( c=='f'
800 && strncmp(pParse->zJson+i,"false",5)==0
dan2e8f5512015-09-17 17:21:09 +0000801 && !safe_isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000802 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
803 return i+5;
804 }else if( c=='-' || (c>='0' && c<='9') ){
805 /* Parse number */
806 u8 seenDP = 0;
807 u8 seenE = 0;
808 j = i+1;
809 for(;; j++){
810 c = pParse->zJson[j];
811 if( c>='0' && c<='9' ) continue;
812 if( c=='.' ){
813 if( pParse->zJson[j-1]=='-' ) return -1;
814 if( seenDP ) return -1;
815 seenDP = 1;
816 continue;
817 }
818 if( c=='e' || c=='E' ){
819 if( pParse->zJson[j-1]<'0' ) return -1;
820 if( seenE ) return -1;
821 seenDP = seenE = 1;
822 c = pParse->zJson[j+1];
drh8784eca2015-08-23 02:42:30 +0000823 if( c=='+' || c=='-' ){
824 j++;
825 c = pParse->zJson[j+1];
826 }
drhd1f00682015-08-29 16:02:37 +0000827 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000828 continue;
829 }
830 break;
831 }
832 if( pParse->zJson[j-1]<'0' ) return -1;
833 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
834 j - i, &pParse->zJson[i]);
835 return j;
836 }else if( c=='}' ){
837 return -2; /* End of {...} */
838 }else if( c==']' ){
839 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000840 }else if( c==0 ){
841 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000842 }else{
843 return -1; /* Syntax error */
844 }
845}
846
847/*
848** Parse a complete JSON string. Return 0 on success or non-zero if there
849** are any errors. If an error occurs, free all memory associated with
850** pParse.
851**
852** pParse is uninitialized when this routine is called.
853*/
drhbc8f0922015-08-22 19:39:04 +0000854static int jsonParse(
855 JsonParse *pParse, /* Initialize and fill this JsonParse object */
856 sqlite3_context *pCtx, /* Report errors here */
857 const char *zJson /* Input JSON text to be parsed */
858){
drhe9c37f32015-08-15 21:25:36 +0000859 int i;
drhe9c37f32015-08-15 21:25:36 +0000860 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000861 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000862 pParse->zJson = zJson;
863 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000864 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000865 if( i>0 ){
dan2e8f5512015-09-17 17:21:09 +0000866 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +0000867 if( zJson[i] ) i = -1;
868 }
drhd1f00682015-08-29 16:02:37 +0000869 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +0000870 if( pCtx!=0 ){
871 if( pParse->oom ){
872 sqlite3_result_error_nomem(pCtx);
873 }else{
874 sqlite3_result_error(pCtx, "malformed JSON", -1);
875 }
876 }
drh505ad2c2015-08-21 17:33:11 +0000877 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000878 return 1;
879 }
880 return 0;
881}
drh301eecc2015-08-17 20:14:19 +0000882
drh505ad2c2015-08-21 17:33:11 +0000883/* Mark node i of pParse as being a child of iParent. Call recursively
884** to fill in all the descendants of node i.
885*/
886static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
887 JsonNode *pNode = &pParse->aNode[i];
888 u32 j;
889 pParse->aUp[i] = iParent;
890 switch( pNode->eType ){
891 case JSON_ARRAY: {
892 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
893 jsonParseFillInParentage(pParse, i+j, i);
894 }
895 break;
896 }
897 case JSON_OBJECT: {
898 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
899 pParse->aUp[i+j] = i;
900 jsonParseFillInParentage(pParse, i+j+1, i);
901 }
902 break;
903 }
904 default: {
905 break;
906 }
907 }
908}
909
910/*
911** Compute the parentage of all nodes in a completed parse.
912*/
913static int jsonParseFindParents(JsonParse *pParse){
914 u32 *aUp;
915 assert( pParse->aUp==0 );
916 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000917 if( aUp==0 ){
918 pParse->oom = 1;
919 return SQLITE_NOMEM;
920 }
drh505ad2c2015-08-21 17:33:11 +0000921 jsonParseFillInParentage(pParse, 0, 0);
922 return SQLITE_OK;
923}
924
drh8cb0c832015-09-22 00:21:03 +0000925/*
926** Compare the OBJECT label at pNode against zKey,nKey. Return true on
927** a match.
928*/
mistachkinf2c26ed2015-10-12 22:20:29 +0000929static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh8cb0c832015-09-22 00:21:03 +0000930 if( pNode->jnFlags & JNODE_RAW ){
931 if( pNode->n!=nKey ) return 0;
932 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
933 }else{
934 if( pNode->n!=nKey+2 ) return 0;
935 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
936 }
937}
938
drh52216ad2015-08-18 02:28:03 +0000939/* forward declaration */
drha7714022015-08-29 00:54:49 +0000940static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +0000941
drh987eb1f2015-08-17 15:17:37 +0000942/*
943** Search along zPath to find the node specified. Return a pointer
944** to that node, or NULL if zPath is malformed or if there is no such
945** node.
drh52216ad2015-08-18 02:28:03 +0000946**
947** If pApnd!=0, then try to append new nodes to complete zPath if it is
948** possible to do so and if no existing node corresponds to zPath. If
949** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000950*/
drha7714022015-08-29 00:54:49 +0000951static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +0000952 JsonParse *pParse, /* The JSON to search */
953 u32 iRoot, /* Begin the search at this node */
954 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +0000955 int *pApnd, /* Append nodes to complete path if not NULL */
956 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +0000957){
drhbc8f0922015-08-22 19:39:04 +0000958 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000959 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000960 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000961 if( zPath[0]==0 ) return pRoot;
962 if( zPath[0]=='.' ){
963 if( pRoot->eType!=JSON_OBJECT ) return 0;
964 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000965 if( zPath[0]=='"' ){
966 zKey = zPath + 1;
967 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
968 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +0000969 if( zPath[i] ){
970 i++;
971 }else{
972 *pzErr = zPath;
973 return 0;
974 }
drh6b43cc82015-08-19 23:02:49 +0000975 }else{
976 zKey = zPath;
977 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
978 nKey = i;
979 }
drha7714022015-08-29 00:54:49 +0000980 if( nKey==0 ){
981 *pzErr = zPath;
982 return 0;
983 }
drh987eb1f2015-08-17 15:17:37 +0000984 j = 1;
drh52216ad2015-08-18 02:28:03 +0000985 for(;;){
986 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +0000987 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +0000988 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +0000989 }
990 j++;
drh505ad2c2015-08-21 17:33:11 +0000991 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000992 }
drh52216ad2015-08-18 02:28:03 +0000993 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
994 iRoot += pRoot->u.iAppend;
995 pRoot = &pParse->aNode[iRoot];
996 j = 1;
997 }
998 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +0000999 u32 iStart, iLabel;
1000 JsonNode *pNode;
1001 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1002 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +00001003 zPath += i;
drha7714022015-08-29 00:54:49 +00001004 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001005 if( pParse->oom ) return 0;
1006 if( pNode ){
1007 pRoot = &pParse->aNode[iRoot];
1008 pRoot->u.iAppend = iStart - iRoot;
1009 pRoot->jnFlags |= JNODE_APPEND;
1010 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1011 }
1012 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001013 }
dan2e8f5512015-09-17 17:21:09 +00001014 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
drh987eb1f2015-08-17 15:17:37 +00001015 if( pRoot->eType!=JSON_ARRAY ) return 0;
1016 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001017 j = 1;
1018 while( safe_isdigit(zPath[j]) ){
1019 i = i*10 + zPath[j] - '0';
1020 j++;
drh987eb1f2015-08-17 15:17:37 +00001021 }
drh3d1d2a92015-09-22 01:15:49 +00001022 if( zPath[j]!=']' ){
drha7714022015-08-29 00:54:49 +00001023 *pzErr = zPath;
1024 return 0;
1025 }
drh3d1d2a92015-09-22 01:15:49 +00001026 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001027 j = 1;
drh52216ad2015-08-18 02:28:03 +00001028 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001029 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1030 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001031 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001032 }
1033 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1034 iRoot += pRoot->u.iAppend;
1035 pRoot = &pParse->aNode[iRoot];
1036 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001037 }
1038 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001039 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001040 }
1041 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001042 u32 iStart;
1043 JsonNode *pNode;
1044 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001045 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001046 if( pParse->oom ) return 0;
1047 if( pNode ){
1048 pRoot = &pParse->aNode[iRoot];
1049 pRoot->u.iAppend = iStart - iRoot;
1050 pRoot->jnFlags |= JNODE_APPEND;
1051 }
1052 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001053 }
drh3d1d2a92015-09-22 01:15:49 +00001054 }else{
drha7714022015-08-29 00:54:49 +00001055 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001056 }
1057 return 0;
1058}
1059
drh52216ad2015-08-18 02:28:03 +00001060/*
drhbc8f0922015-08-22 19:39:04 +00001061** Append content to pParse that will complete zPath. Return a pointer
1062** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001063*/
1064static JsonNode *jsonLookupAppend(
1065 JsonParse *pParse, /* Append content to the JSON parse */
1066 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001067 int *pApnd, /* Set this flag to 1 */
1068 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001069){
1070 *pApnd = 1;
1071 if( zPath[0]==0 ){
1072 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1073 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1074 }
1075 if( zPath[0]=='.' ){
1076 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1077 }else if( strncmp(zPath,"[0]",3)==0 ){
1078 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1079 }else{
1080 return 0;
1081 }
1082 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001083 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001084}
1085
drhbc8f0922015-08-22 19:39:04 +00001086/*
drha7714022015-08-29 00:54:49 +00001087** Return the text of a syntax error message on a JSON path. Space is
1088** obtained from sqlite3_malloc().
1089*/
1090static char *jsonPathSyntaxError(const char *zErr){
1091 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1092}
1093
1094/*
1095** Do a node lookup using zPath. Return a pointer to the node on success.
1096** Return NULL if not found or if there is an error.
1097**
1098** On an error, write an error message into pCtx and increment the
1099** pParse->nErr counter.
1100**
1101** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1102** nodes are appended.
drha7714022015-08-29 00:54:49 +00001103*/
1104static JsonNode *jsonLookup(
1105 JsonParse *pParse, /* The JSON to search */
1106 const char *zPath, /* The path to search */
1107 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001108 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001109){
1110 const char *zErr = 0;
1111 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001112 char *zMsg;
drha7714022015-08-29 00:54:49 +00001113
1114 if( zPath==0 ) return 0;
1115 if( zPath[0]!='$' ){
1116 zErr = zPath;
1117 goto lookup_err;
1118 }
1119 zPath++;
drha7714022015-08-29 00:54:49 +00001120 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001121 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001122
1123lookup_err:
1124 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001125 assert( zErr!=0 && pCtx!=0 );
1126 zMsg = jsonPathSyntaxError(zErr);
1127 if( zMsg ){
1128 sqlite3_result_error(pCtx, zMsg, -1);
1129 sqlite3_free(zMsg);
1130 }else{
1131 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001132 }
drha7714022015-08-29 00:54:49 +00001133 return 0;
1134}
1135
1136
1137/*
drhbc8f0922015-08-22 19:39:04 +00001138** Report the wrong number of arguments for json_insert(), json_replace()
1139** or json_set().
1140*/
1141static void jsonWrongNumArgs(
1142 sqlite3_context *pCtx,
1143 const char *zFuncName
1144){
1145 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1146 zFuncName);
1147 sqlite3_result_error(pCtx, zMsg, -1);
1148 sqlite3_free(zMsg);
1149}
drh52216ad2015-08-18 02:28:03 +00001150
drha7714022015-08-29 00:54:49 +00001151
drh987eb1f2015-08-17 15:17:37 +00001152/****************************************************************************
1153** SQL functions used for testing and debugging
1154****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001155
drh301eecc2015-08-17 20:14:19 +00001156#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001157/*
drh5634cc02015-08-17 11:28:03 +00001158** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001159** a parse of the JSON provided. Or it returns NULL if JSON is not
1160** well-formed.
1161*/
drh5634cc02015-08-17 11:28:03 +00001162static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001163 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001164 int argc,
1165 sqlite3_value **argv
1166){
drh505ad2c2015-08-21 17:33:11 +00001167 JsonString s; /* Output string - not real JSON */
1168 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001169 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001170
1171 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001172 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001173 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001174 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001175 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001176 const char *zType;
1177 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1178 assert( x.aNode[i].eType==JSON_STRING );
1179 zType = "label";
1180 }else{
1181 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001182 }
drh852944e2015-09-10 03:29:11 +00001183 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1184 i, zType, x.aNode[i].n, x.aUp[i]);
1185 if( x.aNode[i].u.zJContent!=0 ){
1186 jsonAppendRaw(&s, " ", 1);
1187 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1188 }
1189 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001190 }
drh505ad2c2015-08-21 17:33:11 +00001191 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001192 jsonResult(&s);
1193}
1194
drh5634cc02015-08-17 11:28:03 +00001195/*
drhf5ddb9c2015-09-11 00:06:41 +00001196** The json_test1(JSON) function return true (1) if the input is JSON
1197** text generated by another json function. It returns (0) if the input
1198** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001199*/
1200static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001201 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001202 int argc,
1203 sqlite3_value **argv
1204){
mistachkin16a93122015-09-11 18:05:01 +00001205 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001206 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001207}
drh301eecc2015-08-17 20:14:19 +00001208#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001209
drh987eb1f2015-08-17 15:17:37 +00001210/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001211** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001212****************************************************************************/
1213
1214/*
drh2ad96f52016-06-17 13:01:51 +00001215** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1216** corresponding to the SQL value input. Mostly this means putting
1217** double-quotes around strings and returning the unquoted string "null"
1218** when given a NULL input.
1219*/
1220static void jsonQuoteFunc(
1221 sqlite3_context *ctx,
1222 int argc,
1223 sqlite3_value **argv
1224){
1225 JsonString jx;
1226
1227 jsonInit(&jx, ctx);
1228 jsonAppendValue(&jx, argv[0]);
1229 jsonResult(&jx);
1230 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1231}
1232
1233/*
drh987eb1f2015-08-17 15:17:37 +00001234** Implementation of the json_array(VALUE,...) function. Return a JSON
1235** array that contains all values given in arguments. Or if any argument
1236** is a BLOB, throw an error.
1237*/
1238static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001239 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001240 int argc,
1241 sqlite3_value **argv
1242){
1243 int i;
drh505ad2c2015-08-21 17:33:11 +00001244 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001245
drhbc8f0922015-08-22 19:39:04 +00001246 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001247 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001248 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001249 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001250 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001251 }
drhd0960592015-08-17 21:22:32 +00001252 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001253 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001254 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001255}
1256
1257
1258/*
1259** json_array_length(JSON)
1260** json_array_length(JSON, PATH)
1261**
1262** Return the number of elements in the top-level JSON array.
1263** Return 0 if the input is not a well-formed JSON array.
1264*/
1265static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001266 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001267 int argc,
1268 sqlite3_value **argv
1269){
1270 JsonParse x; /* The parse */
1271 sqlite3_int64 n = 0;
1272 u32 i;
drha8f39a92015-09-21 22:53:16 +00001273 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001274
drhf2df7e72015-08-28 20:07:40 +00001275 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001276 assert( x.nNode );
1277 if( argc==2 ){
1278 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
1279 pNode = jsonLookup(&x, zPath, 0, ctx);
1280 }else{
1281 pNode = x.aNode;
1282 }
1283 if( pNode==0 ){
1284 x.nErr = 1;
1285 }else if( pNode->eType==JSON_ARRAY ){
1286 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1287 for(i=1; i<=pNode->n; n++){
1288 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001289 }
drh987eb1f2015-08-17 15:17:37 +00001290 }
drha7714022015-08-29 00:54:49 +00001291 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001292 jsonParseReset(&x);
1293}
1294
1295/*
drh3ad93bb2015-08-29 19:41:45 +00001296** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001297**
drh3ad93bb2015-08-29 19:41:45 +00001298** Return the element described by PATH. Return NULL if there is no
1299** PATH element. If there are multiple PATHs, then return a JSON array
1300** with the result from each path. Throw an error if the JSON or any PATH
1301** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001302*/
1303static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001304 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001305 int argc,
1306 sqlite3_value **argv
1307){
1308 JsonParse x; /* The parse */
1309 JsonNode *pNode;
1310 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001311 JsonString jx;
1312 int i;
1313
1314 if( argc<2 ) return;
drhbc8f0922015-08-22 19:39:04 +00001315 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh3ad93bb2015-08-29 19:41:45 +00001316 jsonInit(&jx, ctx);
1317 jsonAppendChar(&jx, '[');
1318 for(i=1; i<argc; i++){
1319 zPath = (const char*)sqlite3_value_text(argv[i]);
drhf5ddb9c2015-09-11 00:06:41 +00001320 pNode = jsonLookup(&x, zPath, 0, ctx);
drh3ad93bb2015-08-29 19:41:45 +00001321 if( x.nErr ) break;
1322 if( argc>2 ){
1323 jsonAppendSeparator(&jx);
1324 if( pNode ){
1325 jsonRenderNode(pNode, &jx, 0);
1326 }else{
1327 jsonAppendRaw(&jx, "null", 4);
1328 }
1329 }else if( pNode ){
1330 jsonReturn(pNode, ctx, 0);
1331 }
drh987eb1f2015-08-17 15:17:37 +00001332 }
drh3ad93bb2015-08-29 19:41:45 +00001333 if( argc>2 && i==argc ){
1334 jsonAppendChar(&jx, ']');
1335 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001336 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001337 }
1338 jsonReset(&jx);
drh505ad2c2015-08-21 17:33:11 +00001339 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001340}
1341
1342/*
1343** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1344** object that contains all name/value given in arguments. Or if any name
1345** is not a string or if any value is a BLOB, throw an error.
1346*/
1347static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001348 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001349 int argc,
1350 sqlite3_value **argv
1351){
1352 int i;
drh505ad2c2015-08-21 17:33:11 +00001353 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001354 const char *z;
1355 u32 n;
1356
1357 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001358 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001359 "of arguments", -1);
1360 return;
1361 }
drhbc8f0922015-08-22 19:39:04 +00001362 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001363 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001364 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001365 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001366 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001367 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001368 return;
1369 }
drhd0960592015-08-17 21:22:32 +00001370 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001371 z = (const char*)sqlite3_value_text(argv[i]);
1372 n = (u32)sqlite3_value_bytes(argv[i]);
1373 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001374 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001375 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001376 }
drhd0960592015-08-17 21:22:32 +00001377 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001378 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001379 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001380}
1381
1382
1383/*
drh301eecc2015-08-17 20:14:19 +00001384** json_remove(JSON, PATH, ...)
1385**
drh3ad93bb2015-08-29 19:41:45 +00001386** Remove the named elements from JSON and return the result. malformed
1387** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001388*/
1389static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001390 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001391 int argc,
1392 sqlite3_value **argv
1393){
1394 JsonParse x; /* The parse */
1395 JsonNode *pNode;
1396 const char *zPath;
1397 u32 i;
1398
1399 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001400 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001401 assert( x.nNode );
1402 for(i=1; i<(u32)argc; i++){
1403 zPath = (const char*)sqlite3_value_text(argv[i]);
1404 if( zPath==0 ) goto remove_done;
1405 pNode = jsonLookup(&x, zPath, 0, ctx);
1406 if( x.nErr ) goto remove_done;
1407 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1408 }
1409 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1410 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001411 }
drha7714022015-08-29 00:54:49 +00001412remove_done:
drh505ad2c2015-08-21 17:33:11 +00001413 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001414}
1415
1416/*
1417** json_replace(JSON, PATH, VALUE, ...)
1418**
1419** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001420** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001421*/
1422static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001423 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001424 int argc,
1425 sqlite3_value **argv
1426){
1427 JsonParse x; /* The parse */
1428 JsonNode *pNode;
1429 const char *zPath;
1430 u32 i;
1431
1432 if( argc<1 ) return;
1433 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001434 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001435 return;
1436 }
drhbc8f0922015-08-22 19:39:04 +00001437 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001438 assert( x.nNode );
1439 for(i=1; i<(u32)argc; i+=2){
1440 zPath = (const char*)sqlite3_value_text(argv[i]);
1441 pNode = jsonLookup(&x, zPath, 0, ctx);
1442 if( x.nErr ) goto replace_err;
1443 if( pNode ){
1444 pNode->jnFlags |= (u8)JNODE_REPLACE;
1445 pNode->iVal = (u8)(i+1);
drhd0960592015-08-17 21:22:32 +00001446 }
drha8f39a92015-09-21 22:53:16 +00001447 }
1448 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1449 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1450 }else{
1451 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001452 }
drha7714022015-08-29 00:54:49 +00001453replace_err:
drh505ad2c2015-08-21 17:33:11 +00001454 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001455}
drh505ad2c2015-08-21 17:33:11 +00001456
drh52216ad2015-08-18 02:28:03 +00001457/*
1458** json_set(JSON, PATH, VALUE, ...)
1459**
1460** Set the value at PATH to VALUE. Create the PATH if it does not already
1461** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001462** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001463**
1464** json_insert(JSON, PATH, VALUE, ...)
1465**
1466** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001467** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001468*/
1469static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001470 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001471 int argc,
1472 sqlite3_value **argv
1473){
1474 JsonParse x; /* The parse */
1475 JsonNode *pNode;
1476 const char *zPath;
1477 u32 i;
1478 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001479 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001480
1481 if( argc<1 ) return;
1482 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001483 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001484 return;
1485 }
drhbc8f0922015-08-22 19:39:04 +00001486 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001487 assert( x.nNode );
1488 for(i=1; i<(u32)argc; i+=2){
1489 zPath = (const char*)sqlite3_value_text(argv[i]);
1490 bApnd = 0;
1491 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1492 if( x.oom ){
1493 sqlite3_result_error_nomem(ctx);
1494 goto jsonSetDone;
1495 }else if( x.nErr ){
1496 goto jsonSetDone;
1497 }else if( pNode && (bApnd || bIsSet) ){
1498 pNode->jnFlags |= (u8)JNODE_REPLACE;
1499 pNode->iVal = (u8)(i+1);
drh52216ad2015-08-18 02:28:03 +00001500 }
drha8f39a92015-09-21 22:53:16 +00001501 }
1502 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1503 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1504 }else{
1505 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001506 }
drhbc8f0922015-08-22 19:39:04 +00001507jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001508 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001509}
drh301eecc2015-08-17 20:14:19 +00001510
1511/*
drh987eb1f2015-08-17 15:17:37 +00001512** json_type(JSON)
1513** json_type(JSON, PATH)
1514**
drh3ad93bb2015-08-29 19:41:45 +00001515** Return the top-level "type" of a JSON string. Throw an error if
1516** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001517*/
1518static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001519 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001520 int argc,
1521 sqlite3_value **argv
1522){
1523 JsonParse x; /* The parse */
1524 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001525 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001526
drhbc8f0922015-08-22 19:39:04 +00001527 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001528 assert( x.nNode );
1529 if( argc==2 ){
1530 zPath = (const char*)sqlite3_value_text(argv[1]);
1531 pNode = jsonLookup(&x, zPath, 0, ctx);
1532 }else{
1533 pNode = x.aNode;
1534 }
1535 if( pNode ){
1536 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001537 }
drh505ad2c2015-08-21 17:33:11 +00001538 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001539}
drh5634cc02015-08-17 11:28:03 +00001540
drhbc8f0922015-08-22 19:39:04 +00001541/*
1542** json_valid(JSON)
1543**
drh3ad93bb2015-08-29 19:41:45 +00001544** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1545** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001546*/
1547static void jsonValidFunc(
1548 sqlite3_context *ctx,
1549 int argc,
1550 sqlite3_value **argv
1551){
1552 JsonParse x; /* The parse */
1553 int rc = 0;
1554
mistachkin16a93122015-09-11 18:05:01 +00001555 UNUSED_PARAM(argc);
drha8f39a92015-09-21 22:53:16 +00001556 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
drhbc8f0922015-08-22 19:39:04 +00001557 rc = 1;
1558 }
1559 jsonParseReset(&x);
1560 sqlite3_result_int(ctx, rc);
1561}
1562
drhff135ae2015-12-30 01:07:02 +00001563
1564/****************************************************************************
1565** Aggregate SQL function implementations
1566****************************************************************************/
1567/*
1568** json_group_array(VALUE)
1569**
1570** Return a JSON array composed of all values in the aggregate.
1571*/
1572static void jsonArrayStep(
1573 sqlite3_context *ctx,
1574 int argc,
1575 sqlite3_value **argv
1576){
1577 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00001578 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001579 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1580 if( pStr ){
1581 if( pStr->zBuf==0 ){
1582 jsonInit(pStr, ctx);
1583 jsonAppendChar(pStr, '[');
1584 }else{
1585 jsonAppendChar(pStr, ',');
1586 pStr->pCtx = ctx;
1587 }
1588 jsonAppendValue(pStr, argv[0]);
1589 }
1590}
1591static void jsonArrayFinal(sqlite3_context *ctx){
1592 JsonString *pStr;
1593 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1594 if( pStr ){
1595 pStr->pCtx = ctx;
1596 jsonAppendChar(pStr, ']');
1597 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001598 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001599 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001600 }else{
1601 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1602 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1603 pStr->bStatic = 1;
1604 }
1605 }else{
1606 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
1607 }
1608 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1609}
1610
1611/*
1612** json_group_obj(NAME,VALUE)
1613**
1614** Return a JSON object composed of all names and values in the aggregate.
1615*/
1616static void jsonObjectStep(
1617 sqlite3_context *ctx,
1618 int argc,
1619 sqlite3_value **argv
1620){
1621 JsonString *pStr;
1622 const char *z;
1623 u32 n;
drhdf3a9072016-02-11 15:37:18 +00001624 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001625 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1626 if( pStr ){
1627 if( pStr->zBuf==0 ){
1628 jsonInit(pStr, ctx);
1629 jsonAppendChar(pStr, '{');
1630 }else{
1631 jsonAppendChar(pStr, ',');
1632 pStr->pCtx = ctx;
1633 }
1634 z = (const char*)sqlite3_value_text(argv[0]);
1635 n = (u32)sqlite3_value_bytes(argv[0]);
1636 jsonAppendString(pStr, z, n);
1637 jsonAppendChar(pStr, ':');
1638 jsonAppendValue(pStr, argv[1]);
1639 }
1640}
1641static void jsonObjectFinal(sqlite3_context *ctx){
1642 JsonString *pStr;
1643 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1644 if( pStr ){
1645 jsonAppendChar(pStr, '}');
1646 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001647 if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001648 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001649 }else{
1650 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1651 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1652 pStr->bStatic = 1;
1653 }
1654 }else{
1655 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
1656 }
1657 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1658}
1659
1660
drhd2975922015-08-29 17:22:33 +00001661#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00001662/****************************************************************************
1663** The json_each virtual table
1664****************************************************************************/
1665typedef struct JsonEachCursor JsonEachCursor;
1666struct JsonEachCursor {
1667 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001668 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00001669 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00001670 u32 i; /* Index in sParse.aNode[] of current row */
1671 u32 iEnd; /* EOF when i equals or exceeds this value */
1672 u8 eType; /* Type of top-level element */
1673 u8 bRecursive; /* True for json_tree(). False for json_each() */
1674 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00001675 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00001676 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001677};
1678
1679/* Constructor for the json_each virtual table */
1680static int jsonEachConnect(
1681 sqlite3 *db,
1682 void *pAux,
1683 int argc, const char *const*argv,
1684 sqlite3_vtab **ppVtab,
1685 char **pzErr
1686){
1687 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001688 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001689
1690/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001691#define JEACH_KEY 0
1692#define JEACH_VALUE 1
1693#define JEACH_TYPE 2
1694#define JEACH_ATOM 3
1695#define JEACH_ID 4
1696#define JEACH_PARENT 5
1697#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00001698#define JEACH_PATH 7
1699#define JEACH_JSON 8
1700#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00001701
drh6fd5c1e2015-08-21 20:37:12 +00001702 UNUSED_PARAM(pzErr);
1703 UNUSED_PARAM(argv);
1704 UNUSED_PARAM(argc);
1705 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001706 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00001707 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
1708 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001709 if( rc==SQLITE_OK ){
1710 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1711 if( pNew==0 ) return SQLITE_NOMEM;
1712 memset(pNew, 0, sizeof(*pNew));
1713 }
1714 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001715}
1716
1717/* destructor for json_each virtual table */
1718static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1719 sqlite3_free(pVtab);
1720 return SQLITE_OK;
1721}
1722
drh505ad2c2015-08-21 17:33:11 +00001723/* constructor for a JsonEachCursor object for json_each(). */
1724static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001725 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001726
1727 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001728 pCur = sqlite3_malloc( sizeof(*pCur) );
1729 if( pCur==0 ) return SQLITE_NOMEM;
1730 memset(pCur, 0, sizeof(*pCur));
1731 *ppCursor = &pCur->base;
1732 return SQLITE_OK;
1733}
1734
drh505ad2c2015-08-21 17:33:11 +00001735/* constructor for a JsonEachCursor object for json_tree(). */
1736static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1737 int rc = jsonEachOpenEach(p, ppCursor);
1738 if( rc==SQLITE_OK ){
1739 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1740 pCur->bRecursive = 1;
1741 }
1742 return rc;
1743}
1744
drhcb6c6c62015-08-19 22:47:17 +00001745/* Reset a JsonEachCursor back to its original state. Free any memory
1746** held. */
1747static void jsonEachCursorReset(JsonEachCursor *p){
1748 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00001749 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00001750 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001751 p->iRowid = 0;
1752 p->i = 0;
1753 p->iEnd = 0;
1754 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001755 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00001756 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001757}
1758
1759/* Destructor for a jsonEachCursor object */
1760static int jsonEachClose(sqlite3_vtab_cursor *cur){
1761 JsonEachCursor *p = (JsonEachCursor*)cur;
1762 jsonEachCursorReset(p);
1763 sqlite3_free(cur);
1764 return SQLITE_OK;
1765}
1766
1767/* Return TRUE if the jsonEachCursor object has been advanced off the end
1768** of the JSON object */
1769static int jsonEachEof(sqlite3_vtab_cursor *cur){
1770 JsonEachCursor *p = (JsonEachCursor*)cur;
1771 return p->i >= p->iEnd;
1772}
1773
drh505ad2c2015-08-21 17:33:11 +00001774/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001775static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001776 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001777 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00001778 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
1779 p->i++;
drh4af352d2015-08-21 20:02:48 +00001780 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00001781 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00001782 u32 iUp = p->sParse.aUp[p->i];
1783 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001784 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001785 if( pUp->eType==JSON_ARRAY ){
1786 if( iUp==p->i-1 ){
1787 pUp->u.iKey = 0;
1788 }else{
1789 pUp->u.iKey++;
1790 }
drh4af352d2015-08-21 20:02:48 +00001791 }
1792 }
drh505ad2c2015-08-21 17:33:11 +00001793 }else{
drh4af352d2015-08-21 20:02:48 +00001794 switch( p->eType ){
1795 case JSON_ARRAY: {
1796 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1797 p->iRowid++;
1798 break;
1799 }
1800 case JSON_OBJECT: {
1801 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1802 p->iRowid++;
1803 break;
1804 }
1805 default: {
1806 p->i = p->iEnd;
1807 break;
1808 }
drh505ad2c2015-08-21 17:33:11 +00001809 }
1810 }
1811 return SQLITE_OK;
1812}
1813
drh4af352d2015-08-21 20:02:48 +00001814/* Append the name of the path for element i to pStr
1815*/
1816static void jsonEachComputePath(
1817 JsonEachCursor *p, /* The cursor */
1818 JsonString *pStr, /* Write the path here */
1819 u32 i /* Path to this element */
1820){
1821 JsonNode *pNode, *pUp;
1822 u32 iUp;
1823 if( i==0 ){
1824 jsonAppendChar(pStr, '$');
1825 return;
drhcb6c6c62015-08-19 22:47:17 +00001826 }
drh4af352d2015-08-21 20:02:48 +00001827 iUp = p->sParse.aUp[i];
1828 jsonEachComputePath(p, pStr, iUp);
1829 pNode = &p->sParse.aNode[i];
1830 pUp = &p->sParse.aNode[iUp];
1831 if( pUp->eType==JSON_ARRAY ){
1832 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1833 }else{
1834 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00001835 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00001836 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00001837 assert( pNode->jnFlags & JNODE_LABEL );
drh4af352d2015-08-21 20:02:48 +00001838 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1839 }
drhcb6c6c62015-08-19 22:47:17 +00001840}
1841
1842/* Return the value of a column */
1843static int jsonEachColumn(
1844 sqlite3_vtab_cursor *cur, /* The cursor */
1845 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1846 int i /* Which column to return */
1847){
1848 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00001849 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00001850 switch( i ){
1851 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00001852 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00001853 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00001854 jsonReturn(pThis, ctx, 0);
1855 }else if( p->eType==JSON_ARRAY ){
1856 u32 iKey;
1857 if( p->bRecursive ){
1858 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00001859 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00001860 }else{
1861 iKey = p->iRowid;
1862 }
drh6fd5c1e2015-08-21 20:37:12 +00001863 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00001864 }
1865 break;
1866 }
1867 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00001868 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001869 jsonReturn(pThis, ctx, 0);
1870 break;
1871 }
1872 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00001873 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001874 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
1875 break;
1876 }
1877 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00001878 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001879 if( pThis->eType>=JSON_ARRAY ) break;
1880 jsonReturn(pThis, ctx, 0);
1881 break;
1882 }
1883 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00001884 sqlite3_result_int64(ctx,
1885 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00001886 break;
1887 }
1888 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00001889 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00001890 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00001891 }
1892 break;
1893 }
drh4af352d2015-08-21 20:02:48 +00001894 case JEACH_FULLKEY: {
1895 JsonString x;
1896 jsonInit(&x, ctx);
1897 if( p->bRecursive ){
1898 jsonEachComputePath(p, &x, p->i);
1899 }else{
drh383de692015-09-10 17:20:57 +00001900 if( p->zRoot ){
1901 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00001902 }else{
1903 jsonAppendChar(&x, '$');
1904 }
1905 if( p->eType==JSON_ARRAY ){
1906 jsonPrintf(30, &x, "[%d]", p->iRowid);
1907 }else{
1908 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
1909 }
1910 }
1911 jsonResult(&x);
1912 break;
1913 }
drhcb6c6c62015-08-19 22:47:17 +00001914 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00001915 if( p->bRecursive ){
1916 JsonString x;
1917 jsonInit(&x, ctx);
1918 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
1919 jsonResult(&x);
1920 break;
drh4af352d2015-08-21 20:02:48 +00001921 }
drh383de692015-09-10 17:20:57 +00001922 /* For json_each() path and root are the same so fall through
1923 ** into the root case */
1924 }
1925 case JEACH_ROOT: {
1926 const char *zRoot = p->zRoot;
1927 if( zRoot==0 ) zRoot = "$";
1928 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00001929 break;
1930 }
drh3d1d2a92015-09-22 01:15:49 +00001931 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00001932 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00001933 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1934 break;
1935 }
1936 }
1937 return SQLITE_OK;
1938}
1939
1940/* Return the current rowid value */
1941static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1942 JsonEachCursor *p = (JsonEachCursor*)cur;
1943 *pRowid = p->iRowid;
1944 return SQLITE_OK;
1945}
1946
1947/* The query strategy is to look for an equality constraint on the json
1948** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00001949** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00001950** and 0 otherwise.
1951*/
1952static int jsonEachBestIndex(
1953 sqlite3_vtab *tab,
1954 sqlite3_index_info *pIdxInfo
1955){
1956 int i;
1957 int jsonIdx = -1;
drh383de692015-09-10 17:20:57 +00001958 int rootIdx = -1;
drhcb6c6c62015-08-19 22:47:17 +00001959 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00001960
1961 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00001962 pConstraint = pIdxInfo->aConstraint;
1963 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1964 if( pConstraint->usable==0 ) continue;
1965 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1966 switch( pConstraint->iColumn ){
1967 case JEACH_JSON: jsonIdx = i; break;
drh383de692015-09-10 17:20:57 +00001968 case JEACH_ROOT: rootIdx = i; break;
drhcb6c6c62015-08-19 22:47:17 +00001969 default: /* no-op */ break;
1970 }
1971 }
1972 if( jsonIdx<0 ){
1973 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00001974 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00001975 }else{
drh505ad2c2015-08-21 17:33:11 +00001976 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00001977 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1978 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
drh383de692015-09-10 17:20:57 +00001979 if( rootIdx<0 ){
drhcb6c6c62015-08-19 22:47:17 +00001980 pIdxInfo->idxNum = 1;
1981 }else{
drh383de692015-09-10 17:20:57 +00001982 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
1983 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
drhcb6c6c62015-08-19 22:47:17 +00001984 pIdxInfo->idxNum = 3;
1985 }
1986 }
1987 return SQLITE_OK;
1988}
1989
1990/* Start a search on a new JSON string */
1991static int jsonEachFilter(
1992 sqlite3_vtab_cursor *cur,
1993 int idxNum, const char *idxStr,
1994 int argc, sqlite3_value **argv
1995){
1996 JsonEachCursor *p = (JsonEachCursor*)cur;
1997 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00001998 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001999 sqlite3_int64 n;
2000
drh6fd5c1e2015-08-21 20:37:12 +00002001 UNUSED_PARAM(idxStr);
2002 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002003 jsonEachCursorReset(p);
2004 if( idxNum==0 ) return SQLITE_OK;
2005 z = (const char*)sqlite3_value_text(argv[0]);
2006 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002007 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002008 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002009 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002010 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002011 if( jsonParse(&p->sParse, 0, p->zJson) ){
2012 int rc = SQLITE_NOMEM;
2013 if( p->sParse.oom==0 ){
2014 sqlite3_free(cur->pVtab->zErrMsg);
2015 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2016 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2017 }
drhcb6c6c62015-08-19 22:47:17 +00002018 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002019 return rc;
2020 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2021 jsonEachCursorReset(p);
2022 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002023 }else{
drh95677942015-09-24 01:06:37 +00002024 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002025 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002026 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002027 zRoot = (const char*)sqlite3_value_text(argv[1]);
2028 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002029 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002030 p->zRoot = sqlite3_malloc64( n+1 );
2031 if( p->zRoot==0 ) return SQLITE_NOMEM;
2032 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002033 if( zRoot[0]!='$' ){
2034 zErr = zRoot;
2035 }else{
2036 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2037 }
2038 if( zErr ){
drha7714022015-08-29 00:54:49 +00002039 sqlite3_free(cur->pVtab->zErrMsg);
2040 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002041 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002042 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2043 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002044 return SQLITE_OK;
2045 }
2046 }else{
2047 pNode = p->sParse.aNode;
2048 }
drh852944e2015-09-10 03:29:11 +00002049 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002050 p->eType = pNode->eType;
2051 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00002052 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002053 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002054 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002055 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002056 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2057 p->i--;
2058 }
2059 }else{
2060 p->i++;
2061 }
drhcb6c6c62015-08-19 22:47:17 +00002062 }else{
2063 p->iEnd = p->i+1;
2064 }
2065 }
drha8f39a92015-09-21 22:53:16 +00002066 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002067}
2068
2069/* The methods of the json_each virtual table */
2070static sqlite3_module jsonEachModule = {
2071 0, /* iVersion */
2072 0, /* xCreate */
2073 jsonEachConnect, /* xConnect */
2074 jsonEachBestIndex, /* xBestIndex */
2075 jsonEachDisconnect, /* xDisconnect */
2076 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002077 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002078 jsonEachClose, /* xClose - close a cursor */
2079 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002080 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002081 jsonEachEof, /* xEof - check for end of scan */
2082 jsonEachColumn, /* xColumn - read data */
2083 jsonEachRowid, /* xRowid - read data */
2084 0, /* xUpdate */
2085 0, /* xBegin */
2086 0, /* xSync */
2087 0, /* xCommit */
2088 0, /* xRollback */
2089 0, /* xFindMethod */
2090 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002091 0, /* xSavepoint */
2092 0, /* xRelease */
2093 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00002094};
2095
drh505ad2c2015-08-21 17:33:11 +00002096/* The methods of the json_tree virtual table. */
2097static sqlite3_module jsonTreeModule = {
2098 0, /* iVersion */
2099 0, /* xCreate */
2100 jsonEachConnect, /* xConnect */
2101 jsonEachBestIndex, /* xBestIndex */
2102 jsonEachDisconnect, /* xDisconnect */
2103 0, /* xDestroy */
2104 jsonEachOpenTree, /* xOpen - open a cursor */
2105 jsonEachClose, /* xClose - close a cursor */
2106 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002107 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002108 jsonEachEof, /* xEof - check for end of scan */
2109 jsonEachColumn, /* xColumn - read data */
2110 jsonEachRowid, /* xRowid - read data */
2111 0, /* xUpdate */
2112 0, /* xBegin */
2113 0, /* xSync */
2114 0, /* xCommit */
2115 0, /* xRollback */
2116 0, /* xFindMethod */
2117 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002118 0, /* xSavepoint */
2119 0, /* xRelease */
2120 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00002121};
drhd2975922015-08-29 17:22:33 +00002122#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002123
2124/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002125** The following routines are the only publically visible identifiers in this
2126** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002127** functions and the virtual table implemented by this file.
2128****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002129
drh2f20e132015-09-26 17:44:59 +00002130int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002131 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002132 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002133 static const struct {
2134 const char *zName;
2135 int nArg;
drh52216ad2015-08-18 02:28:03 +00002136 int flag;
drh5fa5c102015-08-12 16:49:40 +00002137 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2138 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002139 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002140 { "json_array", -1, 0, jsonArrayFunc },
2141 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2142 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002143 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002144 { "json_insert", -1, 0, jsonSetFunc },
2145 { "json_object", -1, 0, jsonObjectFunc },
drh2ad96f52016-06-17 13:01:51 +00002146 { "json_quote", 1, 0, jsonQuoteFunc },
drh52216ad2015-08-18 02:28:03 +00002147 { "json_remove", -1, 0, jsonRemoveFunc },
2148 { "json_replace", -1, 0, jsonReplaceFunc },
2149 { "json_set", -1, 1, jsonSetFunc },
2150 { "json_type", 1, 0, jsonTypeFunc },
2151 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002152 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002153
drh301eecc2015-08-17 20:14:19 +00002154#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002155 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002156 { "json_parse", 1, 0, jsonParseFunc },
2157 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002158#endif
drh5fa5c102015-08-12 16:49:40 +00002159 };
drhff135ae2015-12-30 01:07:02 +00002160 static const struct {
2161 const char *zName;
2162 int nArg;
2163 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2164 void (*xFinal)(sqlite3_context*);
2165 } aAgg[] = {
2166 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal },
2167 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal },
2168 };
drhd2975922015-08-29 17:22:33 +00002169#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002170 static const struct {
2171 const char *zName;
2172 sqlite3_module *pModule;
2173 } aMod[] = {
2174 { "json_each", &jsonEachModule },
2175 { "json_tree", &jsonTreeModule },
2176 };
drhd2975922015-08-29 17:22:33 +00002177#endif
drh5fa5c102015-08-12 16:49:40 +00002178 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
2179 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00002180 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
2181 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00002182 aFunc[i].xFunc, 0, 0);
2183 }
drhff135ae2015-12-30 01:07:02 +00002184 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
2185 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
2186 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
2187 0, aAgg[i].xStep, aAgg[i].xFinal);
2188 }
drhd2975922015-08-29 17:22:33 +00002189#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002190 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2191 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002192 }
drhd2975922015-08-29 17:22:33 +00002193#endif
drh5fa5c102015-08-12 16:49:40 +00002194 return rc;
2195}
drh2f20e132015-09-26 17:44:59 +00002196
2197
dan8d32e802015-10-14 18:45:42 +00002198#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002199#ifdef _WIN32
2200__declspec(dllexport)
2201#endif
2202int sqlite3_json_init(
2203 sqlite3 *db,
2204 char **pzErrMsg,
2205 const sqlite3_api_routines *pApi
2206){
2207 SQLITE_EXTENSION_INIT2(pApi);
2208 (void)pzErrMsg; /* Unused parameter */
2209 return sqlite3Json1Init(db);
2210}
dan8d32e802015-10-14 18:45:42 +00002211#endif
drh50065652015-10-08 19:29:18 +00002212#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */