blob: 61d013ea4b639fe078053c93cb9b7481f7277d69 [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
drh6fd5c1e2015-08-21 20:37:12 +000034#define UNUSED_PARAM(X) (void)(X)
35
drh8deb4b82015-10-09 18:21:43 +000036#ifndef LARGEST_INT64
37# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
38# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
39#endif
40
dan2e8f5512015-09-17 17:21:09 +000041/*
42** Versions of isspace(), isalnum() and isdigit() to which it is safe
43** to pass signed char values.
44*/
drh49472652015-10-16 15:35:39 +000045#ifdef sqlite3Isdigit
46 /* Use the SQLite core versions if this routine is part of the
47 ** SQLite amalgamation */
48# define safe_isdigit(x) sqlite3Isdigit(x)
49# define safe_isalnum(x) sqlite3Isalnum(x)
50#else
51 /* Use the standard library for separate compilation */
52#include <ctype.h> /* amalgamator: keep */
53# define safe_isdigit(x) isdigit((unsigned char)(x))
54# define safe_isalnum(x) isalnum((unsigned char)(x))
55#endif
dan2e8f5512015-09-17 17:21:09 +000056
drh95677942015-09-24 01:06:37 +000057/*
58** Growing our own isspace() routine this way is twice as fast as
59** the library isspace() function, resulting in a 7% overall performance
60** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
61*/
62static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000063 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000064 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 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};
80#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
81
drh9a4718f2015-10-10 14:00:37 +000082#ifndef SQLITE_AMALGAMATION
83 /* Unsigned integer types. These are already defined in the sqliteInt.h,
84 ** but the definitions need to be repeated for separate compilation. */
85 typedef sqlite3_uint64 u64;
86 typedef unsigned int u32;
87 typedef unsigned char u8;
88#endif
drh5fa5c102015-08-12 16:49:40 +000089
drh52216ad2015-08-18 02:28:03 +000090/* Objects */
drh505ad2c2015-08-21 17:33:11 +000091typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000092typedef struct JsonNode JsonNode;
93typedef struct JsonParse JsonParse;
94
drh5634cc02015-08-17 11:28:03 +000095/* An instance of this object represents a JSON string
96** under construction. Really, this is a generic string accumulator
97** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +000098*/
drh505ad2c2015-08-21 17:33:11 +000099struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000100 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000101 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000102 u64 nAlloc; /* Bytes of storage available in zBuf[] */
103 u64 nUsed; /* Bytes of zBuf[] currently used */
104 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000105 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000106 char zSpace[100]; /* Initial static space */
107};
108
drhe9c37f32015-08-15 21:25:36 +0000109/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000110*/
drhe9c37f32015-08-15 21:25:36 +0000111#define JSON_NULL 0
112#define JSON_TRUE 1
113#define JSON_FALSE 2
114#define JSON_INT 3
115#define JSON_REAL 4
116#define JSON_STRING 5
117#define JSON_ARRAY 6
118#define JSON_OBJECT 7
119
drhf5ddb9c2015-09-11 00:06:41 +0000120/* The "subtype" set for JSON values */
121#define JSON_SUBTYPE 74 /* Ascii for "J" */
122
drh987eb1f2015-08-17 15:17:37 +0000123/*
124** Names of the various JSON types:
125*/
126static const char * const jsonType[] = {
127 "null", "true", "false", "integer", "real", "text", "array", "object"
128};
129
drh301eecc2015-08-17 20:14:19 +0000130/* Bit values for the JsonNode.jnFlag field
131*/
132#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
133#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
134#define JNODE_REMOVE 0x04 /* Do not output */
drhd0960592015-08-17 21:22:32 +0000135#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh52216ad2015-08-18 02:28:03 +0000136#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
drhf5ddb9c2015-09-11 00:06:41 +0000137#define JNODE_LABEL 0x20 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000138
drh987eb1f2015-08-17 15:17:37 +0000139
drhe9c37f32015-08-15 21:25:36 +0000140/* A single node of parsed JSON
141*/
drhe9c37f32015-08-15 21:25:36 +0000142struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000143 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000144 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +0000145 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +0000146 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000147 union {
drh0042a972015-08-18 12:59:58 +0000148 const char *zJContent; /* Content for INT, REAL, and STRING */
149 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +0000150 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh52216ad2015-08-18 02:28:03 +0000151 } u;
drhe9c37f32015-08-15 21:25:36 +0000152};
153
154/* A completely parsed JSON string
155*/
drhe9c37f32015-08-15 21:25:36 +0000156struct JsonParse {
157 u32 nNode; /* Number of slots of aNode[] used */
158 u32 nAlloc; /* Number of slots of aNode[] allocated */
159 JsonNode *aNode; /* Array of nodes containing the parse */
160 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000161 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000162 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000163 u8 nErr; /* Number of errors seen */
drhe9c37f32015-08-15 21:25:36 +0000164};
165
drh505ad2c2015-08-21 17:33:11 +0000166/**************************************************************************
167** Utility routines for dealing with JsonString objects
168**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000169
drh505ad2c2015-08-21 17:33:11 +0000170/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000171*/
drh505ad2c2015-08-21 17:33:11 +0000172static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000173 p->zBuf = p->zSpace;
174 p->nAlloc = sizeof(p->zSpace);
175 p->nUsed = 0;
176 p->bStatic = 1;
177}
178
drh505ad2c2015-08-21 17:33:11 +0000179/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000180*/
drh505ad2c2015-08-21 17:33:11 +0000181static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000182 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000183 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000184 jsonZero(p);
185}
186
187
drh505ad2c2015-08-21 17:33:11 +0000188/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000189** initial state.
190*/
drh505ad2c2015-08-21 17:33:11 +0000191static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000192 if( !p->bStatic ) sqlite3_free(p->zBuf);
193 jsonZero(p);
194}
195
196
197/* Report an out-of-memory (OOM) condition
198*/
drh505ad2c2015-08-21 17:33:11 +0000199static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000200 p->bErr = 1;
201 sqlite3_result_error_nomem(p->pCtx);
202 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000203}
204
205/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
206** Return zero on success. Return non-zero on an OOM error
207*/
drh505ad2c2015-08-21 17:33:11 +0000208static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000209 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000210 char *zNew;
211 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000212 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000213 zNew = sqlite3_malloc64(nTotal);
214 if( zNew==0 ){
215 jsonOom(p);
216 return SQLITE_NOMEM;
217 }
drh6fd5c1e2015-08-21 20:37:12 +0000218 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000219 p->zBuf = zNew;
220 p->bStatic = 0;
221 }else{
222 zNew = sqlite3_realloc64(p->zBuf, nTotal);
223 if( zNew==0 ){
224 jsonOom(p);
225 return SQLITE_NOMEM;
226 }
227 p->zBuf = zNew;
228 }
229 p->nAlloc = nTotal;
230 return SQLITE_OK;
231}
232
drh505ad2c2015-08-21 17:33:11 +0000233/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000234*/
drh505ad2c2015-08-21 17:33:11 +0000235static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000236 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
237 memcpy(p->zBuf+p->nUsed, zIn, N);
238 p->nUsed += N;
239}
240
drh4af352d2015-08-21 20:02:48 +0000241/* Append formatted text (not to exceed N bytes) to the JsonString.
242*/
243static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
244 va_list ap;
245 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
246 va_start(ap, zFormat);
247 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
248 va_end(ap);
249 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
250}
251
drh5634cc02015-08-17 11:28:03 +0000252/* Append a single character
253*/
drh505ad2c2015-08-21 17:33:11 +0000254static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000255 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
256 p->zBuf[p->nUsed++] = c;
257}
258
drh301eecc2015-08-17 20:14:19 +0000259/* Append a comma separator to the output buffer, if the previous
260** character is not '[' or '{'.
261*/
drh505ad2c2015-08-21 17:33:11 +0000262static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000263 char c;
264 if( p->nUsed==0 ) return;
265 c = p->zBuf[p->nUsed-1];
266 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
267}
268
drh505ad2c2015-08-21 17:33:11 +0000269/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000270** under construction. Enclose the string in "..." and escape
271** any double-quotes or backslash characters contained within the
272** string.
273*/
drh505ad2c2015-08-21 17:33:11 +0000274static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000275 u32 i;
276 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
277 p->zBuf[p->nUsed++] = '"';
278 for(i=0; i<N; i++){
279 char c = zIn[i];
280 if( c=='"' || c=='\\' ){
drh4977ccf2015-09-19 11:57:26 +0000281 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000282 p->zBuf[p->nUsed++] = '\\';
283 }
284 p->zBuf[p->nUsed++] = c;
285 }
286 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000287 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000288}
289
drhd0960592015-08-17 21:22:32 +0000290/*
291** Append a function parameter value to the JSON string under
292** construction.
293*/
294static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000295 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000296 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000297){
298 switch( sqlite3_value_type(pValue) ){
299 case SQLITE_NULL: {
300 jsonAppendRaw(p, "null", 4);
301 break;
302 }
303 case SQLITE_INTEGER:
304 case SQLITE_FLOAT: {
305 const char *z = (const char*)sqlite3_value_text(pValue);
306 u32 n = (u32)sqlite3_value_bytes(pValue);
307 jsonAppendRaw(p, z, n);
308 break;
309 }
310 case SQLITE_TEXT: {
311 const char *z = (const char*)sqlite3_value_text(pValue);
312 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000313 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000314 jsonAppendRaw(p, z, n);
315 }else{
316 jsonAppendString(p, z, n);
317 }
drhd0960592015-08-17 21:22:32 +0000318 break;
319 }
320 default: {
321 if( p->bErr==0 ){
322 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
323 p->bErr = 1;
324 jsonReset(p);
325 }
326 break;
327 }
328 }
329}
330
331
drhbd0621b2015-08-13 13:54:59 +0000332/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000333*/
drh505ad2c2015-08-21 17:33:11 +0000334static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000335 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000336 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
337 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
338 SQLITE_UTF8);
339 jsonZero(p);
340 }
341 assert( p->bStatic );
342}
343
drh505ad2c2015-08-21 17:33:11 +0000344/**************************************************************************
345** Utility routines for dealing with JsonNode and JsonParse objects
346**************************************************************************/
347
348/*
349** Return the number of consecutive JsonNode slots need to represent
350** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
351** OBJECT types, the number might be larger.
352**
353** Appended elements are not counted. The value returned is the number
354** by which the JsonNode counter should increment in order to go to the
355** next peer value.
356*/
357static u32 jsonNodeSize(JsonNode *pNode){
358 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
359}
360
361/*
362** Reclaim all memory allocated by a JsonParse object. But do not
363** delete the JsonParse object itself.
364*/
365static void jsonParseReset(JsonParse *pParse){
366 sqlite3_free(pParse->aNode);
367 pParse->aNode = 0;
368 pParse->nNode = 0;
369 pParse->nAlloc = 0;
370 sqlite3_free(pParse->aUp);
371 pParse->aUp = 0;
372}
373
drh5634cc02015-08-17 11:28:03 +0000374/*
375** Convert the JsonNode pNode into a pure JSON string and
376** append to pOut. Subsubstructure is also included. Return
377** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000378*/
drh52216ad2015-08-18 02:28:03 +0000379static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000380 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000381 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000382 sqlite3_value **aReplace /* Replacement values */
383){
drh5634cc02015-08-17 11:28:03 +0000384 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000385 default: {
386 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000387 jsonAppendRaw(pOut, "null", 4);
388 break;
389 }
390 case JSON_TRUE: {
391 jsonAppendRaw(pOut, "true", 4);
392 break;
393 }
394 case JSON_FALSE: {
395 jsonAppendRaw(pOut, "false", 5);
396 break;
397 }
398 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000399 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000400 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000401 break;
402 }
403 /* Fall through into the next case */
404 }
405 case JSON_REAL:
406 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000407 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000408 break;
409 }
410 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000411 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000412 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000413 for(;;){
414 while( j<=pNode->n ){
415 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
416 if( pNode[j].jnFlags & JNODE_REPLACE ){
417 jsonAppendSeparator(pOut);
drhf5ddb9c2015-09-11 00:06:41 +0000418 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
drh52216ad2015-08-18 02:28:03 +0000419 }
420 }else{
drhd0960592015-08-17 21:22:32 +0000421 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000422 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000423 }
drh505ad2c2015-08-21 17:33:11 +0000424 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000425 }
drh52216ad2015-08-18 02:28:03 +0000426 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
427 pNode = &pNode[pNode->u.iAppend];
428 j = 1;
drh5634cc02015-08-17 11:28:03 +0000429 }
430 jsonAppendChar(pOut, ']');
431 break;
432 }
433 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000434 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000435 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000436 for(;;){
437 while( j<=pNode->n ){
438 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
439 jsonAppendSeparator(pOut);
440 jsonRenderNode(&pNode[j], pOut, aReplace);
441 jsonAppendChar(pOut, ':');
442 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
drhf5ddb9c2015-09-11 00:06:41 +0000443 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
drh52216ad2015-08-18 02:28:03 +0000444 }else{
445 jsonRenderNode(&pNode[j+1], pOut, aReplace);
446 }
drhd0960592015-08-17 21:22:32 +0000447 }
drh505ad2c2015-08-21 17:33:11 +0000448 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000449 }
drh52216ad2015-08-18 02:28:03 +0000450 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
451 pNode = &pNode[pNode->u.iAppend];
452 j = 1;
drh5634cc02015-08-17 11:28:03 +0000453 }
454 jsonAppendChar(pOut, '}');
455 break;
456 }
drhbd0621b2015-08-13 13:54:59 +0000457 }
drh5634cc02015-08-17 11:28:03 +0000458}
459
460/*
drhf2df7e72015-08-28 20:07:40 +0000461** Return a JsonNode and all its descendents as a JSON string.
462*/
463static void jsonReturnJson(
464 JsonNode *pNode, /* Node to return */
465 sqlite3_context *pCtx, /* Return value for this function */
466 sqlite3_value **aReplace /* Array of replacement values */
467){
468 JsonString s;
469 jsonInit(&s, pCtx);
470 jsonRenderNode(pNode, &s, aReplace);
471 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000472 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000473}
474
475/*
drh5634cc02015-08-17 11:28:03 +0000476** Make the JsonNode the return value of the function.
477*/
drhd0960592015-08-17 21:22:32 +0000478static void jsonReturn(
479 JsonNode *pNode, /* Node to return */
480 sqlite3_context *pCtx, /* Return value for this function */
481 sqlite3_value **aReplace /* Array of replacement values */
482){
drh5634cc02015-08-17 11:28:03 +0000483 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000484 default: {
485 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000486 sqlite3_result_null(pCtx);
487 break;
488 }
489 case JSON_TRUE: {
490 sqlite3_result_int(pCtx, 1);
491 break;
492 }
493 case JSON_FALSE: {
494 sqlite3_result_int(pCtx, 0);
495 break;
496 }
drh987eb1f2015-08-17 15:17:37 +0000497 case JSON_INT: {
498 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000499 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000500 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000501 while( z[0]>='0' && z[0]<='9' ){
502 unsigned v = *(z++) - '0';
503 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000504 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000505 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
506 if( v==9 ) goto int_as_real;
507 if( v==8 ){
508 if( pNode->u.zJContent[0]=='-' ){
509 sqlite3_result_int64(pCtx, SMALLEST_INT64);
510 goto int_done;
511 }else{
512 goto int_as_real;
513 }
514 }
515 }
516 i = i*10 + v;
517 }
drh52216ad2015-08-18 02:28:03 +0000518 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000519 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000520 int_done:
521 break;
522 int_as_real: /* fall through to real */;
523 }
524 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000525 double r;
526#ifdef SQLITE_AMALGAMATION
527 const char *z = pNode->u.zJContent;
528 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
529#else
530 r = strtod(pNode->u.zJContent, 0);
531#endif
drh8deb4b82015-10-09 18:21:43 +0000532 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000533 break;
534 }
drh5634cc02015-08-17 11:28:03 +0000535 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000536#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
537 ** json_insert() and json_replace() and those routines do not
538 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000539 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000540 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
541 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000542 }else
543#endif
544 assert( (pNode->jnFlags & JNODE_RAW)==0 );
545 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000546 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000547 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000548 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000549 }else{
550 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000551 u32 i;
552 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000553 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000554 char *zOut;
555 u32 j;
556 zOut = sqlite3_malloc( n+1 );
557 if( zOut==0 ){
558 sqlite3_result_error_nomem(pCtx);
559 break;
560 }
561 for(i=1, j=0; i<n-1; i++){
562 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000563 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000564 zOut[j++] = c;
565 }else{
566 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000567 if( c=='u' ){
drh987eb1f2015-08-17 15:17:37 +0000568 u32 v = 0, k;
drh80d87402015-08-24 12:42:41 +0000569 for(k=0; k<4 && i<n-2; i++, k++){
drh8784eca2015-08-23 02:42:30 +0000570 c = z[i+1];
drh987eb1f2015-08-17 15:17:37 +0000571 if( c>='0' && c<='9' ) v = v*16 + c - '0';
572 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
573 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
574 else break;
drh987eb1f2015-08-17 15:17:37 +0000575 }
drh80d87402015-08-24 12:42:41 +0000576 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000577 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000578 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000579 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000580 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000581 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000582 }else{
mistachkin16a93122015-09-11 18:05:01 +0000583 zOut[j++] = (char)(0xe0 | (v>>12));
drh987eb1f2015-08-17 15:17:37 +0000584 zOut[j++] = 0x80 | ((v>>6)&0x3f);
585 zOut[j++] = 0x80 | (v&0x3f);
drh987eb1f2015-08-17 15:17:37 +0000586 }
587 }else{
588 if( c=='b' ){
589 c = '\b';
590 }else if( c=='f' ){
591 c = '\f';
592 }else if( c=='n' ){
593 c = '\n';
594 }else if( c=='r' ){
595 c = '\r';
596 }else if( c=='t' ){
597 c = '\t';
598 }
599 zOut[j++] = c;
600 }
601 }
602 }
603 zOut[j] = 0;
604 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000605 }
606 break;
607 }
608 case JSON_ARRAY:
609 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000610 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000611 break;
612 }
613 }
drhbd0621b2015-08-13 13:54:59 +0000614}
615
drh95677942015-09-24 01:06:37 +0000616/* Forward reference */
617static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
618
619/*
620** A macro to hint to the compiler that a function should not be
621** inlined.
622*/
623#if defined(__GNUC__)
624# define JSON_NOINLINE __attribute__((noinline))
625#elif defined(_MSC_VER) && _MSC_VER>=1310
626# define JSON_NOINLINE __declspec(noinline)
627#else
628# define JSON_NOINLINE
629#endif
630
631
632static JSON_NOINLINE int jsonParseAddNodeExpand(
633 JsonParse *pParse, /* Append the node to this object */
634 u32 eType, /* Node type */
635 u32 n, /* Content size or sub-node count */
636 const char *zContent /* Content */
637){
638 u32 nNew;
639 JsonNode *pNew;
640 assert( pParse->nNode>=pParse->nAlloc );
641 if( pParse->oom ) return -1;
642 nNew = pParse->nAlloc*2 + 10;
643 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
644 if( pNew==0 ){
645 pParse->oom = 1;
646 return -1;
647 }
648 pParse->nAlloc = nNew;
649 pParse->aNode = pNew;
650 assert( pParse->nNode<pParse->nAlloc );
651 return jsonParseAddNode(pParse, eType, n, zContent);
652}
653
drh5fa5c102015-08-12 16:49:40 +0000654/*
drhe9c37f32015-08-15 21:25:36 +0000655** Create a new JsonNode instance based on the arguments and append that
656** instance to the JsonParse. Return the index in pParse->aNode[] of the
657** new node, or -1 if a memory allocation fails.
658*/
659static int jsonParseAddNode(
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 JsonNode *p;
666 if( pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000667 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000668 }
669 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000670 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000671 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000672 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000673 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000674 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000675 return pParse->nNode++;
676}
677
678/*
679** Parse a single JSON value which begins at pParse->zJson[i]. Return the
680** index of the first character past the end of the value parsed.
681**
682** Return negative for a syntax error. Special cases: return -2 if the
683** first non-whitespace character is '}' and return -3 if the first
684** non-whitespace character is ']'.
685*/
686static int jsonParseValue(JsonParse *pParse, u32 i){
687 char c;
688 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000689 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000690 int x;
drh852944e2015-09-10 03:29:11 +0000691 JsonNode *pNode;
dan2e8f5512015-09-17 17:21:09 +0000692 while( safe_isspace(pParse->zJson[i]) ){ i++; }
drh8cb15cc2015-09-24 01:40:45 +0000693 if( (c = pParse->zJson[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000694 /* Parse object */
695 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000696 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000697 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000698 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000699 x = jsonParseValue(pParse, j);
700 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000701 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000702 return -1;
703 }
drhbe9474e2015-08-22 03:05:54 +0000704 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000705 pNode = &pParse->aNode[pParse->nNode-1];
706 if( pNode->eType!=JSON_STRING ) return -1;
707 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000708 j = x;
dan2e8f5512015-09-17 17:21:09 +0000709 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000710 if( pParse->zJson[j]!=':' ) return -1;
711 j++;
712 x = jsonParseValue(pParse, j);
713 if( x<0 ) return -1;
714 j = x;
dan2e8f5512015-09-17 17:21:09 +0000715 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000716 c = pParse->zJson[j];
717 if( c==',' ) continue;
718 if( c!='}' ) return -1;
719 break;
720 }
drhbc8f0922015-08-22 19:39:04 +0000721 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000722 return j+1;
723 }else if( c=='[' ){
724 /* Parse array */
725 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000726 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000727 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000728 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000729 x = jsonParseValue(pParse, j);
730 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000731 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000732 return -1;
733 }
734 j = x;
dan2e8f5512015-09-17 17:21:09 +0000735 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000736 c = pParse->zJson[j];
737 if( c==',' ) continue;
738 if( c!=']' ) return -1;
739 break;
740 }
drhbc8f0922015-08-22 19:39:04 +0000741 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000742 return j+1;
743 }else if( c=='"' ){
744 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000745 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000746 j = i+1;
747 for(;;){
748 c = pParse->zJson[j];
749 if( c==0 ) return -1;
750 if( c=='\\' ){
751 c = pParse->zJson[++j];
752 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000753 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000754 }else if( c=='"' ){
755 break;
756 }
757 j++;
758 }
759 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drhbe9474e2015-08-22 03:05:54 +0000760 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000761 return j+1;
762 }else if( c=='n'
763 && strncmp(pParse->zJson+i,"null",4)==0
dan2e8f5512015-09-17 17:21:09 +0000764 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000765 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
766 return i+4;
767 }else if( c=='t'
768 && strncmp(pParse->zJson+i,"true",4)==0
dan2e8f5512015-09-17 17:21:09 +0000769 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000770 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
771 return i+4;
772 }else if( c=='f'
773 && strncmp(pParse->zJson+i,"false",5)==0
dan2e8f5512015-09-17 17:21:09 +0000774 && !safe_isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000775 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
776 return i+5;
777 }else if( c=='-' || (c>='0' && c<='9') ){
778 /* Parse number */
779 u8 seenDP = 0;
780 u8 seenE = 0;
781 j = i+1;
782 for(;; j++){
783 c = pParse->zJson[j];
784 if( c>='0' && c<='9' ) continue;
785 if( c=='.' ){
786 if( pParse->zJson[j-1]=='-' ) return -1;
787 if( seenDP ) return -1;
788 seenDP = 1;
789 continue;
790 }
791 if( c=='e' || c=='E' ){
792 if( pParse->zJson[j-1]<'0' ) return -1;
793 if( seenE ) return -1;
794 seenDP = seenE = 1;
795 c = pParse->zJson[j+1];
drh8784eca2015-08-23 02:42:30 +0000796 if( c=='+' || c=='-' ){
797 j++;
798 c = pParse->zJson[j+1];
799 }
drhd1f00682015-08-29 16:02:37 +0000800 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000801 continue;
802 }
803 break;
804 }
805 if( pParse->zJson[j-1]<'0' ) return -1;
806 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
807 j - i, &pParse->zJson[i]);
808 return j;
809 }else if( c=='}' ){
810 return -2; /* End of {...} */
811 }else if( c==']' ){
812 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000813 }else if( c==0 ){
814 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000815 }else{
816 return -1; /* Syntax error */
817 }
818}
819
820/*
821** Parse a complete JSON string. Return 0 on success or non-zero if there
822** are any errors. If an error occurs, free all memory associated with
823** pParse.
824**
825** pParse is uninitialized when this routine is called.
826*/
drhbc8f0922015-08-22 19:39:04 +0000827static int jsonParse(
828 JsonParse *pParse, /* Initialize and fill this JsonParse object */
829 sqlite3_context *pCtx, /* Report errors here */
830 const char *zJson /* Input JSON text to be parsed */
831){
drhe9c37f32015-08-15 21:25:36 +0000832 int i;
drhe9c37f32015-08-15 21:25:36 +0000833 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000834 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000835 pParse->zJson = zJson;
836 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000837 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000838 if( i>0 ){
dan2e8f5512015-09-17 17:21:09 +0000839 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +0000840 if( zJson[i] ) i = -1;
841 }
drhd1f00682015-08-29 16:02:37 +0000842 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +0000843 if( pCtx!=0 ){
844 if( pParse->oom ){
845 sqlite3_result_error_nomem(pCtx);
846 }else{
847 sqlite3_result_error(pCtx, "malformed JSON", -1);
848 }
849 }
drh505ad2c2015-08-21 17:33:11 +0000850 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000851 return 1;
852 }
853 return 0;
854}
drh301eecc2015-08-17 20:14:19 +0000855
drh505ad2c2015-08-21 17:33:11 +0000856/* Mark node i of pParse as being a child of iParent. Call recursively
857** to fill in all the descendants of node i.
858*/
859static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
860 JsonNode *pNode = &pParse->aNode[i];
861 u32 j;
862 pParse->aUp[i] = iParent;
863 switch( pNode->eType ){
864 case JSON_ARRAY: {
865 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
866 jsonParseFillInParentage(pParse, i+j, i);
867 }
868 break;
869 }
870 case JSON_OBJECT: {
871 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
872 pParse->aUp[i+j] = i;
873 jsonParseFillInParentage(pParse, i+j+1, i);
874 }
875 break;
876 }
877 default: {
878 break;
879 }
880 }
881}
882
883/*
884** Compute the parentage of all nodes in a completed parse.
885*/
886static int jsonParseFindParents(JsonParse *pParse){
887 u32 *aUp;
888 assert( pParse->aUp==0 );
889 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000890 if( aUp==0 ){
891 pParse->oom = 1;
892 return SQLITE_NOMEM;
893 }
drh505ad2c2015-08-21 17:33:11 +0000894 jsonParseFillInParentage(pParse, 0, 0);
895 return SQLITE_OK;
896}
897
drh8cb0c832015-09-22 00:21:03 +0000898/*
899** Compare the OBJECT label at pNode against zKey,nKey. Return true on
900** a match.
901*/
mistachkinf2c26ed2015-10-12 22:20:29 +0000902static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh8cb0c832015-09-22 00:21:03 +0000903 if( pNode->jnFlags & JNODE_RAW ){
904 if( pNode->n!=nKey ) return 0;
905 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
906 }else{
907 if( pNode->n!=nKey+2 ) return 0;
908 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
909 }
910}
911
drh52216ad2015-08-18 02:28:03 +0000912/* forward declaration */
drha7714022015-08-29 00:54:49 +0000913static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +0000914
drh987eb1f2015-08-17 15:17:37 +0000915/*
916** Search along zPath to find the node specified. Return a pointer
917** to that node, or NULL if zPath is malformed or if there is no such
918** node.
drh52216ad2015-08-18 02:28:03 +0000919**
920** If pApnd!=0, then try to append new nodes to complete zPath if it is
921** possible to do so and if no existing node corresponds to zPath. If
922** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000923*/
drha7714022015-08-29 00:54:49 +0000924static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +0000925 JsonParse *pParse, /* The JSON to search */
926 u32 iRoot, /* Begin the search at this node */
927 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +0000928 int *pApnd, /* Append nodes to complete path if not NULL */
929 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +0000930){
drhbc8f0922015-08-22 19:39:04 +0000931 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000932 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000933 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000934 if( zPath[0]==0 ) return pRoot;
935 if( zPath[0]=='.' ){
936 if( pRoot->eType!=JSON_OBJECT ) return 0;
937 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000938 if( zPath[0]=='"' ){
939 zKey = zPath + 1;
940 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
941 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +0000942 if( zPath[i] ){
943 i++;
944 }else{
945 *pzErr = zPath;
946 return 0;
947 }
drh6b43cc82015-08-19 23:02:49 +0000948 }else{
949 zKey = zPath;
950 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
951 nKey = i;
952 }
drha7714022015-08-29 00:54:49 +0000953 if( nKey==0 ){
954 *pzErr = zPath;
955 return 0;
956 }
drh987eb1f2015-08-17 15:17:37 +0000957 j = 1;
drh52216ad2015-08-18 02:28:03 +0000958 for(;;){
959 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +0000960 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +0000961 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +0000962 }
963 j++;
drh505ad2c2015-08-21 17:33:11 +0000964 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000965 }
drh52216ad2015-08-18 02:28:03 +0000966 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
967 iRoot += pRoot->u.iAppend;
968 pRoot = &pParse->aNode[iRoot];
969 j = 1;
970 }
971 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +0000972 u32 iStart, iLabel;
973 JsonNode *pNode;
974 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
975 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +0000976 zPath += i;
drha7714022015-08-29 00:54:49 +0000977 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +0000978 if( pParse->oom ) return 0;
979 if( pNode ){
980 pRoot = &pParse->aNode[iRoot];
981 pRoot->u.iAppend = iStart - iRoot;
982 pRoot->jnFlags |= JNODE_APPEND;
983 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
984 }
985 return pNode;
drh987eb1f2015-08-17 15:17:37 +0000986 }
dan2e8f5512015-09-17 17:21:09 +0000987 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
drh987eb1f2015-08-17 15:17:37 +0000988 if( pRoot->eType!=JSON_ARRAY ) return 0;
989 i = 0;
drh3d1d2a92015-09-22 01:15:49 +0000990 j = 1;
991 while( safe_isdigit(zPath[j]) ){
992 i = i*10 + zPath[j] - '0';
993 j++;
drh987eb1f2015-08-17 15:17:37 +0000994 }
drh3d1d2a92015-09-22 01:15:49 +0000995 if( zPath[j]!=']' ){
drha7714022015-08-29 00:54:49 +0000996 *pzErr = zPath;
997 return 0;
998 }
drh3d1d2a92015-09-22 01:15:49 +0000999 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001000 j = 1;
drh52216ad2015-08-18 02:28:03 +00001001 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001002 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1003 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001004 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001005 }
1006 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1007 iRoot += pRoot->u.iAppend;
1008 pRoot = &pParse->aNode[iRoot];
1009 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001010 }
1011 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001012 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001013 }
1014 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001015 u32 iStart;
1016 JsonNode *pNode;
1017 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001018 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001019 if( pParse->oom ) return 0;
1020 if( pNode ){
1021 pRoot = &pParse->aNode[iRoot];
1022 pRoot->u.iAppend = iStart - iRoot;
1023 pRoot->jnFlags |= JNODE_APPEND;
1024 }
1025 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001026 }
drh3d1d2a92015-09-22 01:15:49 +00001027 }else{
drha7714022015-08-29 00:54:49 +00001028 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001029 }
1030 return 0;
1031}
1032
drh52216ad2015-08-18 02:28:03 +00001033/*
drhbc8f0922015-08-22 19:39:04 +00001034** Append content to pParse that will complete zPath. Return a pointer
1035** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001036*/
1037static JsonNode *jsonLookupAppend(
1038 JsonParse *pParse, /* Append content to the JSON parse */
1039 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001040 int *pApnd, /* Set this flag to 1 */
1041 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001042){
1043 *pApnd = 1;
1044 if( zPath[0]==0 ){
1045 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1046 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1047 }
1048 if( zPath[0]=='.' ){
1049 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1050 }else if( strncmp(zPath,"[0]",3)==0 ){
1051 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1052 }else{
1053 return 0;
1054 }
1055 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001056 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001057}
1058
drhbc8f0922015-08-22 19:39:04 +00001059/*
drha7714022015-08-29 00:54:49 +00001060** Return the text of a syntax error message on a JSON path. Space is
1061** obtained from sqlite3_malloc().
1062*/
1063static char *jsonPathSyntaxError(const char *zErr){
1064 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1065}
1066
1067/*
1068** Do a node lookup using zPath. Return a pointer to the node on success.
1069** Return NULL if not found or if there is an error.
1070**
1071** On an error, write an error message into pCtx and increment the
1072** pParse->nErr counter.
1073**
1074** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1075** nodes are appended.
drha7714022015-08-29 00:54:49 +00001076*/
1077static JsonNode *jsonLookup(
1078 JsonParse *pParse, /* The JSON to search */
1079 const char *zPath, /* The path to search */
1080 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001081 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001082){
1083 const char *zErr = 0;
1084 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001085 char *zMsg;
drha7714022015-08-29 00:54:49 +00001086
1087 if( zPath==0 ) return 0;
1088 if( zPath[0]!='$' ){
1089 zErr = zPath;
1090 goto lookup_err;
1091 }
1092 zPath++;
drha7714022015-08-29 00:54:49 +00001093 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001094 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001095
1096lookup_err:
1097 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001098 assert( zErr!=0 && pCtx!=0 );
1099 zMsg = jsonPathSyntaxError(zErr);
1100 if( zMsg ){
1101 sqlite3_result_error(pCtx, zMsg, -1);
1102 sqlite3_free(zMsg);
1103 }else{
1104 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001105 }
drha7714022015-08-29 00:54:49 +00001106 return 0;
1107}
1108
1109
1110/*
drhbc8f0922015-08-22 19:39:04 +00001111** Report the wrong number of arguments for json_insert(), json_replace()
1112** or json_set().
1113*/
1114static void jsonWrongNumArgs(
1115 sqlite3_context *pCtx,
1116 const char *zFuncName
1117){
1118 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1119 zFuncName);
1120 sqlite3_result_error(pCtx, zMsg, -1);
1121 sqlite3_free(zMsg);
1122}
drh52216ad2015-08-18 02:28:03 +00001123
drha7714022015-08-29 00:54:49 +00001124
drh987eb1f2015-08-17 15:17:37 +00001125/****************************************************************************
1126** SQL functions used for testing and debugging
1127****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001128
drh301eecc2015-08-17 20:14:19 +00001129#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001130/*
drh5634cc02015-08-17 11:28:03 +00001131** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001132** a parse of the JSON provided. Or it returns NULL if JSON is not
1133** well-formed.
1134*/
drh5634cc02015-08-17 11:28:03 +00001135static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001136 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001137 int argc,
1138 sqlite3_value **argv
1139){
drh505ad2c2015-08-21 17:33:11 +00001140 JsonString s; /* Output string - not real JSON */
1141 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001142 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001143
1144 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001145 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001146 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001147 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001148 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001149 const char *zType;
1150 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1151 assert( x.aNode[i].eType==JSON_STRING );
1152 zType = "label";
1153 }else{
1154 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001155 }
drh852944e2015-09-10 03:29:11 +00001156 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1157 i, zType, x.aNode[i].n, x.aUp[i]);
1158 if( x.aNode[i].u.zJContent!=0 ){
1159 jsonAppendRaw(&s, " ", 1);
1160 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1161 }
1162 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001163 }
drh505ad2c2015-08-21 17:33:11 +00001164 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001165 jsonResult(&s);
1166}
1167
drh5634cc02015-08-17 11:28:03 +00001168/*
drhf5ddb9c2015-09-11 00:06:41 +00001169** The json_test1(JSON) function return true (1) if the input is JSON
1170** text generated by another json function. It returns (0) if the input
1171** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001172*/
1173static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001174 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001175 int argc,
1176 sqlite3_value **argv
1177){
mistachkin16a93122015-09-11 18:05:01 +00001178 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001179 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001180}
drh301eecc2015-08-17 20:14:19 +00001181#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001182
drh987eb1f2015-08-17 15:17:37 +00001183/****************************************************************************
1184** SQL function implementations
1185****************************************************************************/
1186
1187/*
1188** Implementation of the json_array(VALUE,...) function. Return a JSON
1189** array that contains all values given in arguments. Or if any argument
1190** is a BLOB, throw an error.
1191*/
1192static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001193 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001194 int argc,
1195 sqlite3_value **argv
1196){
1197 int i;
drh505ad2c2015-08-21 17:33:11 +00001198 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001199
drhbc8f0922015-08-22 19:39:04 +00001200 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001201 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001202 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001203 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001204 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001205 }
drhd0960592015-08-17 21:22:32 +00001206 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001207 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001208 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001209}
1210
1211
1212/*
1213** json_array_length(JSON)
1214** json_array_length(JSON, PATH)
1215**
1216** Return the number of elements in the top-level JSON array.
1217** Return 0 if the input is not a well-formed JSON array.
1218*/
1219static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001220 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001221 int argc,
1222 sqlite3_value **argv
1223){
1224 JsonParse x; /* The parse */
1225 sqlite3_int64 n = 0;
1226 u32 i;
drha8f39a92015-09-21 22:53:16 +00001227 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001228
drhf2df7e72015-08-28 20:07:40 +00001229 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001230 assert( x.nNode );
1231 if( argc==2 ){
1232 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
1233 pNode = jsonLookup(&x, zPath, 0, ctx);
1234 }else{
1235 pNode = x.aNode;
1236 }
1237 if( pNode==0 ){
1238 x.nErr = 1;
1239 }else if( pNode->eType==JSON_ARRAY ){
1240 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1241 for(i=1; i<=pNode->n; n++){
1242 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001243 }
drh987eb1f2015-08-17 15:17:37 +00001244 }
drha7714022015-08-29 00:54:49 +00001245 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001246 jsonParseReset(&x);
1247}
1248
1249/*
drh3ad93bb2015-08-29 19:41:45 +00001250** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001251**
drh3ad93bb2015-08-29 19:41:45 +00001252** Return the element described by PATH. Return NULL if there is no
1253** PATH element. If there are multiple PATHs, then return a JSON array
1254** with the result from each path. Throw an error if the JSON or any PATH
1255** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001256*/
1257static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001258 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001259 int argc,
1260 sqlite3_value **argv
1261){
1262 JsonParse x; /* The parse */
1263 JsonNode *pNode;
1264 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001265 JsonString jx;
1266 int i;
1267
1268 if( argc<2 ) return;
drhbc8f0922015-08-22 19:39:04 +00001269 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh3ad93bb2015-08-29 19:41:45 +00001270 jsonInit(&jx, ctx);
1271 jsonAppendChar(&jx, '[');
1272 for(i=1; i<argc; i++){
1273 zPath = (const char*)sqlite3_value_text(argv[i]);
drhf5ddb9c2015-09-11 00:06:41 +00001274 pNode = jsonLookup(&x, zPath, 0, ctx);
drh3ad93bb2015-08-29 19:41:45 +00001275 if( x.nErr ) break;
1276 if( argc>2 ){
1277 jsonAppendSeparator(&jx);
1278 if( pNode ){
1279 jsonRenderNode(pNode, &jx, 0);
1280 }else{
1281 jsonAppendRaw(&jx, "null", 4);
1282 }
1283 }else if( pNode ){
1284 jsonReturn(pNode, ctx, 0);
1285 }
drh987eb1f2015-08-17 15:17:37 +00001286 }
drh3ad93bb2015-08-29 19:41:45 +00001287 if( argc>2 && i==argc ){
1288 jsonAppendChar(&jx, ']');
1289 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001290 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001291 }
1292 jsonReset(&jx);
drh505ad2c2015-08-21 17:33:11 +00001293 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001294}
1295
1296/*
1297** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1298** object that contains all name/value given in arguments. Or if any name
1299** is not a string or if any value is a BLOB, throw an error.
1300*/
1301static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001302 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001303 int argc,
1304 sqlite3_value **argv
1305){
1306 int i;
drh505ad2c2015-08-21 17:33:11 +00001307 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001308 const char *z;
1309 u32 n;
1310
1311 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001312 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001313 "of arguments", -1);
1314 return;
1315 }
drhbc8f0922015-08-22 19:39:04 +00001316 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001317 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001318 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001319 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001320 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001321 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001322 return;
1323 }
drhd0960592015-08-17 21:22:32 +00001324 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001325 z = (const char*)sqlite3_value_text(argv[i]);
1326 n = (u32)sqlite3_value_bytes(argv[i]);
1327 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001328 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001329 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001330 }
drhd0960592015-08-17 21:22:32 +00001331 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001332 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001333 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001334}
1335
1336
1337/*
drh301eecc2015-08-17 20:14:19 +00001338** json_remove(JSON, PATH, ...)
1339**
drh3ad93bb2015-08-29 19:41:45 +00001340** Remove the named elements from JSON and return the result. malformed
1341** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001342*/
1343static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001344 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001345 int argc,
1346 sqlite3_value **argv
1347){
1348 JsonParse x; /* The parse */
1349 JsonNode *pNode;
1350 const char *zPath;
1351 u32 i;
1352
1353 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001354 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001355 assert( x.nNode );
1356 for(i=1; i<(u32)argc; i++){
1357 zPath = (const char*)sqlite3_value_text(argv[i]);
1358 if( zPath==0 ) goto remove_done;
1359 pNode = jsonLookup(&x, zPath, 0, ctx);
1360 if( x.nErr ) goto remove_done;
1361 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1362 }
1363 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1364 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001365 }
drha7714022015-08-29 00:54:49 +00001366remove_done:
drh505ad2c2015-08-21 17:33:11 +00001367 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001368}
1369
1370/*
1371** json_replace(JSON, PATH, VALUE, ...)
1372**
1373** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001374** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001375*/
1376static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001377 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001378 int argc,
1379 sqlite3_value **argv
1380){
1381 JsonParse x; /* The parse */
1382 JsonNode *pNode;
1383 const char *zPath;
1384 u32 i;
1385
1386 if( argc<1 ) return;
1387 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001388 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001389 return;
1390 }
drhbc8f0922015-08-22 19:39:04 +00001391 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001392 assert( x.nNode );
1393 for(i=1; i<(u32)argc; i+=2){
1394 zPath = (const char*)sqlite3_value_text(argv[i]);
1395 pNode = jsonLookup(&x, zPath, 0, ctx);
1396 if( x.nErr ) goto replace_err;
1397 if( pNode ){
1398 pNode->jnFlags |= (u8)JNODE_REPLACE;
1399 pNode->iVal = (u8)(i+1);
drhd0960592015-08-17 21:22:32 +00001400 }
drha8f39a92015-09-21 22:53:16 +00001401 }
1402 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1403 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1404 }else{
1405 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001406 }
drha7714022015-08-29 00:54:49 +00001407replace_err:
drh505ad2c2015-08-21 17:33:11 +00001408 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001409}
drh505ad2c2015-08-21 17:33:11 +00001410
drh52216ad2015-08-18 02:28:03 +00001411/*
1412** json_set(JSON, PATH, VALUE, ...)
1413**
1414** Set the value at PATH to VALUE. Create the PATH if it does not already
1415** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001416** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001417**
1418** json_insert(JSON, PATH, VALUE, ...)
1419**
1420** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001421** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001422*/
1423static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001424 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001425 int argc,
1426 sqlite3_value **argv
1427){
1428 JsonParse x; /* The parse */
1429 JsonNode *pNode;
1430 const char *zPath;
1431 u32 i;
1432 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001433 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001434
1435 if( argc<1 ) return;
1436 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001437 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001438 return;
1439 }
drhbc8f0922015-08-22 19:39:04 +00001440 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001441 assert( x.nNode );
1442 for(i=1; i<(u32)argc; i+=2){
1443 zPath = (const char*)sqlite3_value_text(argv[i]);
1444 bApnd = 0;
1445 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1446 if( x.oom ){
1447 sqlite3_result_error_nomem(ctx);
1448 goto jsonSetDone;
1449 }else if( x.nErr ){
1450 goto jsonSetDone;
1451 }else if( pNode && (bApnd || bIsSet) ){
1452 pNode->jnFlags |= (u8)JNODE_REPLACE;
1453 pNode->iVal = (u8)(i+1);
drh52216ad2015-08-18 02:28:03 +00001454 }
drha8f39a92015-09-21 22:53:16 +00001455 }
1456 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1457 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1458 }else{
1459 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001460 }
drhbc8f0922015-08-22 19:39:04 +00001461jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001462 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001463}
drh301eecc2015-08-17 20:14:19 +00001464
1465/*
drh987eb1f2015-08-17 15:17:37 +00001466** json_type(JSON)
1467** json_type(JSON, PATH)
1468**
drh3ad93bb2015-08-29 19:41:45 +00001469** Return the top-level "type" of a JSON string. Throw an error if
1470** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001471*/
1472static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001473 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001474 int argc,
1475 sqlite3_value **argv
1476){
1477 JsonParse x; /* The parse */
1478 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001479 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001480
drhbc8f0922015-08-22 19:39:04 +00001481 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001482 assert( x.nNode );
1483 if( argc==2 ){
1484 zPath = (const char*)sqlite3_value_text(argv[1]);
1485 pNode = jsonLookup(&x, zPath, 0, ctx);
1486 }else{
1487 pNode = x.aNode;
1488 }
1489 if( pNode ){
1490 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001491 }
drh505ad2c2015-08-21 17:33:11 +00001492 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001493}
drh5634cc02015-08-17 11:28:03 +00001494
drhbc8f0922015-08-22 19:39:04 +00001495/*
1496** json_valid(JSON)
1497**
drh3ad93bb2015-08-29 19:41:45 +00001498** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1499** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001500*/
1501static void jsonValidFunc(
1502 sqlite3_context *ctx,
1503 int argc,
1504 sqlite3_value **argv
1505){
1506 JsonParse x; /* The parse */
1507 int rc = 0;
1508
mistachkin16a93122015-09-11 18:05:01 +00001509 UNUSED_PARAM(argc);
drha8f39a92015-09-21 22:53:16 +00001510 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
drhbc8f0922015-08-22 19:39:04 +00001511 rc = 1;
1512 }
1513 jsonParseReset(&x);
1514 sqlite3_result_int(ctx, rc);
1515}
1516
drhd2975922015-08-29 17:22:33 +00001517#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00001518/****************************************************************************
1519** The json_each virtual table
1520****************************************************************************/
1521typedef struct JsonEachCursor JsonEachCursor;
1522struct JsonEachCursor {
1523 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001524 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00001525 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00001526 u32 i; /* Index in sParse.aNode[] of current row */
1527 u32 iEnd; /* EOF when i equals or exceeds this value */
1528 u8 eType; /* Type of top-level element */
1529 u8 bRecursive; /* True for json_tree(). False for json_each() */
1530 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00001531 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00001532 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001533};
1534
1535/* Constructor for the json_each virtual table */
1536static int jsonEachConnect(
1537 sqlite3 *db,
1538 void *pAux,
1539 int argc, const char *const*argv,
1540 sqlite3_vtab **ppVtab,
1541 char **pzErr
1542){
1543 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001544 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001545
1546/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001547#define JEACH_KEY 0
1548#define JEACH_VALUE 1
1549#define JEACH_TYPE 2
1550#define JEACH_ATOM 3
1551#define JEACH_ID 4
1552#define JEACH_PARENT 5
1553#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00001554#define JEACH_PATH 7
1555#define JEACH_JSON 8
1556#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00001557
drh6fd5c1e2015-08-21 20:37:12 +00001558 UNUSED_PARAM(pzErr);
1559 UNUSED_PARAM(argv);
1560 UNUSED_PARAM(argc);
1561 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001562 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00001563 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
1564 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001565 if( rc==SQLITE_OK ){
1566 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1567 if( pNew==0 ) return SQLITE_NOMEM;
1568 memset(pNew, 0, sizeof(*pNew));
1569 }
1570 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001571}
1572
1573/* destructor for json_each virtual table */
1574static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1575 sqlite3_free(pVtab);
1576 return SQLITE_OK;
1577}
1578
drh505ad2c2015-08-21 17:33:11 +00001579/* constructor for a JsonEachCursor object for json_each(). */
1580static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001581 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001582
1583 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001584 pCur = sqlite3_malloc( sizeof(*pCur) );
1585 if( pCur==0 ) return SQLITE_NOMEM;
1586 memset(pCur, 0, sizeof(*pCur));
1587 *ppCursor = &pCur->base;
1588 return SQLITE_OK;
1589}
1590
drh505ad2c2015-08-21 17:33:11 +00001591/* constructor for a JsonEachCursor object for json_tree(). */
1592static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1593 int rc = jsonEachOpenEach(p, ppCursor);
1594 if( rc==SQLITE_OK ){
1595 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1596 pCur->bRecursive = 1;
1597 }
1598 return rc;
1599}
1600
drhcb6c6c62015-08-19 22:47:17 +00001601/* Reset a JsonEachCursor back to its original state. Free any memory
1602** held. */
1603static void jsonEachCursorReset(JsonEachCursor *p){
1604 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00001605 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00001606 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001607 p->iRowid = 0;
1608 p->i = 0;
1609 p->iEnd = 0;
1610 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001611 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00001612 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001613}
1614
1615/* Destructor for a jsonEachCursor object */
1616static int jsonEachClose(sqlite3_vtab_cursor *cur){
1617 JsonEachCursor *p = (JsonEachCursor*)cur;
1618 jsonEachCursorReset(p);
1619 sqlite3_free(cur);
1620 return SQLITE_OK;
1621}
1622
1623/* Return TRUE if the jsonEachCursor object has been advanced off the end
1624** of the JSON object */
1625static int jsonEachEof(sqlite3_vtab_cursor *cur){
1626 JsonEachCursor *p = (JsonEachCursor*)cur;
1627 return p->i >= p->iEnd;
1628}
1629
drh505ad2c2015-08-21 17:33:11 +00001630/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001631static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001632 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001633 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00001634 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
1635 p->i++;
drh4af352d2015-08-21 20:02:48 +00001636 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00001637 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00001638 u32 iUp = p->sParse.aUp[p->i];
1639 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001640 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001641 if( pUp->eType==JSON_ARRAY ){
1642 if( iUp==p->i-1 ){
1643 pUp->u.iKey = 0;
1644 }else{
1645 pUp->u.iKey++;
1646 }
drh4af352d2015-08-21 20:02:48 +00001647 }
1648 }
drh505ad2c2015-08-21 17:33:11 +00001649 }else{
drh4af352d2015-08-21 20:02:48 +00001650 switch( p->eType ){
1651 case JSON_ARRAY: {
1652 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1653 p->iRowid++;
1654 break;
1655 }
1656 case JSON_OBJECT: {
1657 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1658 p->iRowid++;
1659 break;
1660 }
1661 default: {
1662 p->i = p->iEnd;
1663 break;
1664 }
drh505ad2c2015-08-21 17:33:11 +00001665 }
1666 }
1667 return SQLITE_OK;
1668}
1669
drh4af352d2015-08-21 20:02:48 +00001670/* Append the name of the path for element i to pStr
1671*/
1672static void jsonEachComputePath(
1673 JsonEachCursor *p, /* The cursor */
1674 JsonString *pStr, /* Write the path here */
1675 u32 i /* Path to this element */
1676){
1677 JsonNode *pNode, *pUp;
1678 u32 iUp;
1679 if( i==0 ){
1680 jsonAppendChar(pStr, '$');
1681 return;
drhcb6c6c62015-08-19 22:47:17 +00001682 }
drh4af352d2015-08-21 20:02:48 +00001683 iUp = p->sParse.aUp[i];
1684 jsonEachComputePath(p, pStr, iUp);
1685 pNode = &p->sParse.aNode[i];
1686 pUp = &p->sParse.aNode[iUp];
1687 if( pUp->eType==JSON_ARRAY ){
1688 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1689 }else{
1690 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00001691 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00001692 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00001693 assert( pNode->jnFlags & JNODE_LABEL );
drh4af352d2015-08-21 20:02:48 +00001694 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1695 }
drhcb6c6c62015-08-19 22:47:17 +00001696}
1697
1698/* Return the value of a column */
1699static int jsonEachColumn(
1700 sqlite3_vtab_cursor *cur, /* The cursor */
1701 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1702 int i /* Which column to return */
1703){
1704 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00001705 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00001706 switch( i ){
1707 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00001708 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00001709 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00001710 jsonReturn(pThis, ctx, 0);
1711 }else if( p->eType==JSON_ARRAY ){
1712 u32 iKey;
1713 if( p->bRecursive ){
1714 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00001715 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00001716 }else{
1717 iKey = p->iRowid;
1718 }
drh6fd5c1e2015-08-21 20:37:12 +00001719 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00001720 }
1721 break;
1722 }
1723 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00001724 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001725 jsonReturn(pThis, ctx, 0);
1726 break;
1727 }
1728 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00001729 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001730 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
1731 break;
1732 }
1733 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00001734 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001735 if( pThis->eType>=JSON_ARRAY ) break;
1736 jsonReturn(pThis, ctx, 0);
1737 break;
1738 }
1739 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00001740 sqlite3_result_int64(ctx,
1741 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00001742 break;
1743 }
1744 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00001745 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00001746 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00001747 }
1748 break;
1749 }
drh4af352d2015-08-21 20:02:48 +00001750 case JEACH_FULLKEY: {
1751 JsonString x;
1752 jsonInit(&x, ctx);
1753 if( p->bRecursive ){
1754 jsonEachComputePath(p, &x, p->i);
1755 }else{
drh383de692015-09-10 17:20:57 +00001756 if( p->zRoot ){
1757 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00001758 }else{
1759 jsonAppendChar(&x, '$');
1760 }
1761 if( p->eType==JSON_ARRAY ){
1762 jsonPrintf(30, &x, "[%d]", p->iRowid);
1763 }else{
1764 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
1765 }
1766 }
1767 jsonResult(&x);
1768 break;
1769 }
drhcb6c6c62015-08-19 22:47:17 +00001770 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00001771 if( p->bRecursive ){
1772 JsonString x;
1773 jsonInit(&x, ctx);
1774 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
1775 jsonResult(&x);
1776 break;
drh4af352d2015-08-21 20:02:48 +00001777 }
drh383de692015-09-10 17:20:57 +00001778 /* For json_each() path and root are the same so fall through
1779 ** into the root case */
1780 }
1781 case JEACH_ROOT: {
1782 const char *zRoot = p->zRoot;
1783 if( zRoot==0 ) zRoot = "$";
1784 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00001785 break;
1786 }
drh3d1d2a92015-09-22 01:15:49 +00001787 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00001788 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00001789 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1790 break;
1791 }
1792 }
1793 return SQLITE_OK;
1794}
1795
1796/* Return the current rowid value */
1797static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1798 JsonEachCursor *p = (JsonEachCursor*)cur;
1799 *pRowid = p->iRowid;
1800 return SQLITE_OK;
1801}
1802
1803/* The query strategy is to look for an equality constraint on the json
1804** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00001805** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00001806** and 0 otherwise.
1807*/
1808static int jsonEachBestIndex(
1809 sqlite3_vtab *tab,
1810 sqlite3_index_info *pIdxInfo
1811){
1812 int i;
1813 int jsonIdx = -1;
drh383de692015-09-10 17:20:57 +00001814 int rootIdx = -1;
drhcb6c6c62015-08-19 22:47:17 +00001815 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00001816
1817 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00001818 pConstraint = pIdxInfo->aConstraint;
1819 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1820 if( pConstraint->usable==0 ) continue;
1821 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1822 switch( pConstraint->iColumn ){
1823 case JEACH_JSON: jsonIdx = i; break;
drh383de692015-09-10 17:20:57 +00001824 case JEACH_ROOT: rootIdx = i; break;
drhcb6c6c62015-08-19 22:47:17 +00001825 default: /* no-op */ break;
1826 }
1827 }
1828 if( jsonIdx<0 ){
1829 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00001830 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00001831 }else{
drh505ad2c2015-08-21 17:33:11 +00001832 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00001833 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1834 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
drh383de692015-09-10 17:20:57 +00001835 if( rootIdx<0 ){
drhcb6c6c62015-08-19 22:47:17 +00001836 pIdxInfo->idxNum = 1;
1837 }else{
drh383de692015-09-10 17:20:57 +00001838 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
1839 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
drhcb6c6c62015-08-19 22:47:17 +00001840 pIdxInfo->idxNum = 3;
1841 }
1842 }
1843 return SQLITE_OK;
1844}
1845
1846/* Start a search on a new JSON string */
1847static int jsonEachFilter(
1848 sqlite3_vtab_cursor *cur,
1849 int idxNum, const char *idxStr,
1850 int argc, sqlite3_value **argv
1851){
1852 JsonEachCursor *p = (JsonEachCursor*)cur;
1853 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00001854 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001855 sqlite3_int64 n;
1856
drh6fd5c1e2015-08-21 20:37:12 +00001857 UNUSED_PARAM(idxStr);
1858 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00001859 jsonEachCursorReset(p);
1860 if( idxNum==0 ) return SQLITE_OK;
1861 z = (const char*)sqlite3_value_text(argv[0]);
1862 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00001863 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00001864 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00001865 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00001866 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00001867 if( jsonParse(&p->sParse, 0, p->zJson) ){
1868 int rc = SQLITE_NOMEM;
1869 if( p->sParse.oom==0 ){
1870 sqlite3_free(cur->pVtab->zErrMsg);
1871 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
1872 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
1873 }
drhcb6c6c62015-08-19 22:47:17 +00001874 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00001875 return rc;
1876 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
1877 jsonEachCursorReset(p);
1878 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00001879 }else{
drh95677942015-09-24 01:06:37 +00001880 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00001881 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00001882 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00001883 zRoot = (const char*)sqlite3_value_text(argv[1]);
1884 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00001885 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00001886 p->zRoot = sqlite3_malloc64( n+1 );
1887 if( p->zRoot==0 ) return SQLITE_NOMEM;
1888 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00001889 if( zRoot[0]!='$' ){
1890 zErr = zRoot;
1891 }else{
1892 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
1893 }
1894 if( zErr ){
drha7714022015-08-29 00:54:49 +00001895 sqlite3_free(cur->pVtab->zErrMsg);
1896 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00001897 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00001898 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
1899 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00001900 return SQLITE_OK;
1901 }
1902 }else{
1903 pNode = p->sParse.aNode;
1904 }
drh852944e2015-09-10 03:29:11 +00001905 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00001906 p->eType = pNode->eType;
1907 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00001908 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00001909 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00001910 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00001911 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00001912 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
1913 p->i--;
1914 }
1915 }else{
1916 p->i++;
1917 }
drhcb6c6c62015-08-19 22:47:17 +00001918 }else{
1919 p->iEnd = p->i+1;
1920 }
1921 }
drha8f39a92015-09-21 22:53:16 +00001922 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00001923}
1924
1925/* The methods of the json_each virtual table */
1926static sqlite3_module jsonEachModule = {
1927 0, /* iVersion */
1928 0, /* xCreate */
1929 jsonEachConnect, /* xConnect */
1930 jsonEachBestIndex, /* xBestIndex */
1931 jsonEachDisconnect, /* xDisconnect */
1932 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00001933 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00001934 jsonEachClose, /* xClose - close a cursor */
1935 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00001936 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00001937 jsonEachEof, /* xEof - check for end of scan */
1938 jsonEachColumn, /* xColumn - read data */
1939 jsonEachRowid, /* xRowid - read data */
1940 0, /* xUpdate */
1941 0, /* xBegin */
1942 0, /* xSync */
1943 0, /* xCommit */
1944 0, /* xRollback */
1945 0, /* xFindMethod */
1946 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00001947 0, /* xSavepoint */
1948 0, /* xRelease */
1949 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00001950};
1951
drh505ad2c2015-08-21 17:33:11 +00001952/* The methods of the json_tree virtual table. */
1953static sqlite3_module jsonTreeModule = {
1954 0, /* iVersion */
1955 0, /* xCreate */
1956 jsonEachConnect, /* xConnect */
1957 jsonEachBestIndex, /* xBestIndex */
1958 jsonEachDisconnect, /* xDisconnect */
1959 0, /* xDestroy */
1960 jsonEachOpenTree, /* xOpen - open a cursor */
1961 jsonEachClose, /* xClose - close a cursor */
1962 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00001963 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00001964 jsonEachEof, /* xEof - check for end of scan */
1965 jsonEachColumn, /* xColumn - read data */
1966 jsonEachRowid, /* xRowid - read data */
1967 0, /* xUpdate */
1968 0, /* xBegin */
1969 0, /* xSync */
1970 0, /* xCommit */
1971 0, /* xRollback */
1972 0, /* xFindMethod */
1973 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00001974 0, /* xSavepoint */
1975 0, /* xRelease */
1976 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00001977};
drhd2975922015-08-29 17:22:33 +00001978#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00001979
1980/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00001981** The following routines are the only publically visible identifiers in this
1982** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00001983** functions and the virtual table implemented by this file.
1984****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00001985
drh2f20e132015-09-26 17:44:59 +00001986int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00001987 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00001988 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00001989 static const struct {
1990 const char *zName;
1991 int nArg;
drh52216ad2015-08-18 02:28:03 +00001992 int flag;
drh5fa5c102015-08-12 16:49:40 +00001993 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1994 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00001995 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00001996 { "json_array", -1, 0, jsonArrayFunc },
1997 { "json_array_length", 1, 0, jsonArrayLengthFunc },
1998 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00001999 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002000 { "json_insert", -1, 0, jsonSetFunc },
2001 { "json_object", -1, 0, jsonObjectFunc },
2002 { "json_remove", -1, 0, jsonRemoveFunc },
2003 { "json_replace", -1, 0, jsonReplaceFunc },
2004 { "json_set", -1, 1, jsonSetFunc },
2005 { "json_type", 1, 0, jsonTypeFunc },
2006 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002007 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002008
drh301eecc2015-08-17 20:14:19 +00002009#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002010 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002011 { "json_parse", 1, 0, jsonParseFunc },
2012 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002013#endif
drh5fa5c102015-08-12 16:49:40 +00002014 };
drhd2975922015-08-29 17:22:33 +00002015#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002016 static const struct {
2017 const char *zName;
2018 sqlite3_module *pModule;
2019 } aMod[] = {
2020 { "json_each", &jsonEachModule },
2021 { "json_tree", &jsonTreeModule },
2022 };
drhd2975922015-08-29 17:22:33 +00002023#endif
drh5fa5c102015-08-12 16:49:40 +00002024 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
2025 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00002026 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
2027 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00002028 aFunc[i].xFunc, 0, 0);
2029 }
drhd2975922015-08-29 17:22:33 +00002030#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002031 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2032 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002033 }
drhd2975922015-08-29 17:22:33 +00002034#endif
drh5fa5c102015-08-12 16:49:40 +00002035 return rc;
2036}
drh2f20e132015-09-26 17:44:59 +00002037
2038
dan8d32e802015-10-14 18:45:42 +00002039#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002040#ifdef _WIN32
2041__declspec(dllexport)
2042#endif
2043int sqlite3_json_init(
2044 sqlite3 *db,
2045 char **pzErrMsg,
2046 const sqlite3_api_routines *pApi
2047){
2048 SQLITE_EXTENSION_INIT2(pApi);
2049 (void)pzErrMsg; /* Unused parameter */
2050 return sqlite3Json1Init(db);
2051}
dan8d32e802015-10-14 18:45:42 +00002052#endif
drh50065652015-10-08 19:29:18 +00002053#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */