blob: 4945826b93b8b22b4f34e4f4adc3751f0df26ab2 [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*/
24#include "sqlite3ext.h"
25SQLITE_EXTENSION_INIT1
26#include <assert.h>
27#include <string.h>
drhe9c37f32015-08-15 21:25:36 +000028#include <ctype.h>
drh987eb1f2015-08-17 15:17:37 +000029#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000030#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000031
drh6fd5c1e2015-08-21 20:37:12 +000032#define UNUSED_PARAM(X) (void)(X)
33
drh5fa5c102015-08-12 16:49:40 +000034/* Unsigned integer types */
35typedef sqlite3_uint64 u64;
36typedef unsigned int u32;
37typedef unsigned char u8;
38
drh52216ad2015-08-18 02:28:03 +000039/* Objects */
drh505ad2c2015-08-21 17:33:11 +000040typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000041typedef struct JsonNode JsonNode;
42typedef struct JsonParse JsonParse;
43
drh5634cc02015-08-17 11:28:03 +000044/* An instance of this object represents a JSON string
45** under construction. Really, this is a generic string accumulator
46** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +000047*/
drh505ad2c2015-08-21 17:33:11 +000048struct JsonString {
drh5fa5c102015-08-12 16:49:40 +000049 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +000050 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +000051 u64 nAlloc; /* Bytes of storage available in zBuf[] */
52 u64 nUsed; /* Bytes of zBuf[] currently used */
53 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +000054 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +000055 char zSpace[100]; /* Initial static space */
56};
57
drhe9c37f32015-08-15 21:25:36 +000058/* JSON type values
drhbd0621b2015-08-13 13:54:59 +000059*/
drhe9c37f32015-08-15 21:25:36 +000060#define JSON_NULL 0
61#define JSON_TRUE 1
62#define JSON_FALSE 2
63#define JSON_INT 3
64#define JSON_REAL 4
65#define JSON_STRING 5
66#define JSON_ARRAY 6
67#define JSON_OBJECT 7
68
drh987eb1f2015-08-17 15:17:37 +000069/*
70** Names of the various JSON types:
71*/
72static const char * const jsonType[] = {
73 "null", "true", "false", "integer", "real", "text", "array", "object"
74};
75
drh301eecc2015-08-17 20:14:19 +000076/* Bit values for the JsonNode.jnFlag field
77*/
78#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
79#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
80#define JNODE_REMOVE 0x04 /* Do not output */
drhd0960592015-08-17 21:22:32 +000081#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh52216ad2015-08-18 02:28:03 +000082#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
drh301eecc2015-08-17 20:14:19 +000083
drh987eb1f2015-08-17 15:17:37 +000084
drhe9c37f32015-08-15 21:25:36 +000085/* A single node of parsed JSON
86*/
drhe9c37f32015-08-15 21:25:36 +000087struct JsonNode {
drh5634cc02015-08-17 11:28:03 +000088 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +000089 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +000090 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +000091 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +000092 union {
drh0042a972015-08-18 12:59:58 +000093 const char *zJContent; /* Content for INT, REAL, and STRING */
94 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +000095 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh52216ad2015-08-18 02:28:03 +000096 } u;
drhe9c37f32015-08-15 21:25:36 +000097};
98
99/* A completely parsed JSON string
100*/
drhe9c37f32015-08-15 21:25:36 +0000101struct JsonParse {
102 u32 nNode; /* Number of slots of aNode[] used */
103 u32 nAlloc; /* Number of slots of aNode[] allocated */
104 JsonNode *aNode; /* Array of nodes containing the parse */
105 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000106 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000107 u8 oom; /* Set to true if out of memory */
108};
109
drh505ad2c2015-08-21 17:33:11 +0000110/**************************************************************************
111** Utility routines for dealing with JsonString objects
112**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000113
drh505ad2c2015-08-21 17:33:11 +0000114/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000115*/
drh505ad2c2015-08-21 17:33:11 +0000116static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000117 p->zBuf = p->zSpace;
118 p->nAlloc = sizeof(p->zSpace);
119 p->nUsed = 0;
120 p->bStatic = 1;
121}
122
drh505ad2c2015-08-21 17:33:11 +0000123/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000124*/
drh505ad2c2015-08-21 17:33:11 +0000125static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000126 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000127 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000128 jsonZero(p);
129}
130
131
drh505ad2c2015-08-21 17:33:11 +0000132/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000133** initial state.
134*/
drh505ad2c2015-08-21 17:33:11 +0000135static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000136 if( !p->bStatic ) sqlite3_free(p->zBuf);
137 jsonZero(p);
138}
139
140
141/* Report an out-of-memory (OOM) condition
142*/
drh505ad2c2015-08-21 17:33:11 +0000143static void jsonOom(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000144 if( !p->bErr ){
145 p->bErr = 1;
146 sqlite3_result_error_nomem(p->pCtx);
147 jsonReset(p);
148 }
drh5fa5c102015-08-12 16:49:40 +0000149}
150
151/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
152** Return zero on success. Return non-zero on an OOM error
153*/
drh505ad2c2015-08-21 17:33:11 +0000154static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000155 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000156 char *zNew;
157 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000158 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000159 zNew = sqlite3_malloc64(nTotal);
160 if( zNew==0 ){
161 jsonOom(p);
162 return SQLITE_NOMEM;
163 }
drh6fd5c1e2015-08-21 20:37:12 +0000164 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000165 p->zBuf = zNew;
166 p->bStatic = 0;
167 }else{
168 zNew = sqlite3_realloc64(p->zBuf, nTotal);
169 if( zNew==0 ){
170 jsonOom(p);
171 return SQLITE_NOMEM;
172 }
173 p->zBuf = zNew;
174 }
175 p->nAlloc = nTotal;
176 return SQLITE_OK;
177}
178
drh505ad2c2015-08-21 17:33:11 +0000179/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000180*/
drh505ad2c2015-08-21 17:33:11 +0000181static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000182 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
183 memcpy(p->zBuf+p->nUsed, zIn, N);
184 p->nUsed += N;
185}
186
drh4af352d2015-08-21 20:02:48 +0000187/* Append formatted text (not to exceed N bytes) to the JsonString.
188*/
189static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
190 va_list ap;
191 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
192 va_start(ap, zFormat);
193 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
194 va_end(ap);
195 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
196}
197
drh5634cc02015-08-17 11:28:03 +0000198/* Append a single character
199*/
drh505ad2c2015-08-21 17:33:11 +0000200static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000201 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
202 p->zBuf[p->nUsed++] = c;
203}
204
drh301eecc2015-08-17 20:14:19 +0000205/* Append a comma separator to the output buffer, if the previous
206** character is not '[' or '{'.
207*/
drh505ad2c2015-08-21 17:33:11 +0000208static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000209 char c;
210 if( p->nUsed==0 ) return;
211 c = p->zBuf[p->nUsed-1];
212 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
213}
214
drh505ad2c2015-08-21 17:33:11 +0000215/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000216** under construction. Enclose the string in "..." and escape
217** any double-quotes or backslash characters contained within the
218** string.
219*/
drh505ad2c2015-08-21 17:33:11 +0000220static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000221 u32 i;
222 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
223 p->zBuf[p->nUsed++] = '"';
224 for(i=0; i<N; i++){
225 char c = zIn[i];
226 if( c=='"' || c=='\\' ){
227 if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return;
228 p->zBuf[p->nUsed++] = '\\';
229 }
230 p->zBuf[p->nUsed++] = c;
231 }
232 p->zBuf[p->nUsed++] = '"';
233}
234
drhd0960592015-08-17 21:22:32 +0000235/*
236** Append a function parameter value to the JSON string under
237** construction.
238*/
239static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000240 JsonString *p, /* Append to this JSON string */
drhd0960592015-08-17 21:22:32 +0000241 sqlite3_value *pValue /* Value to append */
242){
243 switch( sqlite3_value_type(pValue) ){
244 case SQLITE_NULL: {
245 jsonAppendRaw(p, "null", 4);
246 break;
247 }
248 case SQLITE_INTEGER:
249 case SQLITE_FLOAT: {
250 const char *z = (const char*)sqlite3_value_text(pValue);
251 u32 n = (u32)sqlite3_value_bytes(pValue);
252 jsonAppendRaw(p, z, n);
253 break;
254 }
255 case SQLITE_TEXT: {
256 const char *z = (const char*)sqlite3_value_text(pValue);
257 u32 n = (u32)sqlite3_value_bytes(pValue);
258 jsonAppendString(p, z, n);
259 break;
260 }
261 default: {
262 if( p->bErr==0 ){
263 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
264 p->bErr = 1;
265 jsonReset(p);
266 }
267 break;
268 }
269 }
270}
271
272
drhbd0621b2015-08-13 13:54:59 +0000273/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000274*/
drh505ad2c2015-08-21 17:33:11 +0000275static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000276 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000277 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
278 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
279 SQLITE_UTF8);
280 jsonZero(p);
281 }
282 assert( p->bStatic );
283}
284
drh505ad2c2015-08-21 17:33:11 +0000285/**************************************************************************
286** Utility routines for dealing with JsonNode and JsonParse objects
287**************************************************************************/
288
289/*
290** Return the number of consecutive JsonNode slots need to represent
291** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
292** OBJECT types, the number might be larger.
293**
294** Appended elements are not counted. The value returned is the number
295** by which the JsonNode counter should increment in order to go to the
296** next peer value.
297*/
298static u32 jsonNodeSize(JsonNode *pNode){
299 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
300}
301
302/*
303** Reclaim all memory allocated by a JsonParse object. But do not
304** delete the JsonParse object itself.
305*/
306static void jsonParseReset(JsonParse *pParse){
307 sqlite3_free(pParse->aNode);
308 pParse->aNode = 0;
309 pParse->nNode = 0;
310 pParse->nAlloc = 0;
311 sqlite3_free(pParse->aUp);
312 pParse->aUp = 0;
313}
314
drh5634cc02015-08-17 11:28:03 +0000315/*
316** Convert the JsonNode pNode into a pure JSON string and
317** append to pOut. Subsubstructure is also included. Return
318** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000319*/
drh52216ad2015-08-18 02:28:03 +0000320static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000321 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000322 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000323 sqlite3_value **aReplace /* Replacement values */
324){
drh5634cc02015-08-17 11:28:03 +0000325 switch( pNode->eType ){
326 case JSON_NULL: {
327 jsonAppendRaw(pOut, "null", 4);
328 break;
329 }
330 case JSON_TRUE: {
331 jsonAppendRaw(pOut, "true", 4);
332 break;
333 }
334 case JSON_FALSE: {
335 jsonAppendRaw(pOut, "false", 5);
336 break;
337 }
338 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000339 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000340 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000341 break;
342 }
343 /* Fall through into the next case */
344 }
345 case JSON_REAL:
346 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000347 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000348 break;
349 }
350 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000351 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000352 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000353 for(;;){
354 while( j<=pNode->n ){
355 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
356 if( pNode[j].jnFlags & JNODE_REPLACE ){
357 jsonAppendSeparator(pOut);
358 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
359 }
360 }else{
drhd0960592015-08-17 21:22:32 +0000361 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000362 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000363 }
drh505ad2c2015-08-21 17:33:11 +0000364 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000365 }
drh52216ad2015-08-18 02:28:03 +0000366 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
367 pNode = &pNode[pNode->u.iAppend];
368 j = 1;
drh5634cc02015-08-17 11:28:03 +0000369 }
370 jsonAppendChar(pOut, ']');
371 break;
372 }
373 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000374 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000375 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000376 for(;;){
377 while( j<=pNode->n ){
378 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
379 jsonAppendSeparator(pOut);
380 jsonRenderNode(&pNode[j], pOut, aReplace);
381 jsonAppendChar(pOut, ':');
382 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
383 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
384 }else{
385 jsonRenderNode(&pNode[j+1], pOut, aReplace);
386 }
drhd0960592015-08-17 21:22:32 +0000387 }
drh505ad2c2015-08-21 17:33:11 +0000388 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000389 }
drh52216ad2015-08-18 02:28:03 +0000390 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
391 pNode = &pNode[pNode->u.iAppend];
392 j = 1;
drh5634cc02015-08-17 11:28:03 +0000393 }
394 jsonAppendChar(pOut, '}');
395 break;
396 }
drhbd0621b2015-08-13 13:54:59 +0000397 }
drh5634cc02015-08-17 11:28:03 +0000398}
399
400/*
401** Make the JsonNode the return value of the function.
402*/
drhd0960592015-08-17 21:22:32 +0000403static void jsonReturn(
404 JsonNode *pNode, /* Node to return */
405 sqlite3_context *pCtx, /* Return value for this function */
406 sqlite3_value **aReplace /* Array of replacement values */
407){
drh5634cc02015-08-17 11:28:03 +0000408 switch( pNode->eType ){
409 case JSON_NULL: {
410 sqlite3_result_null(pCtx);
411 break;
412 }
413 case JSON_TRUE: {
414 sqlite3_result_int(pCtx, 1);
415 break;
416 }
417 case JSON_FALSE: {
418 sqlite3_result_int(pCtx, 0);
419 break;
420 }
drh987eb1f2015-08-17 15:17:37 +0000421 case JSON_REAL: {
drh52216ad2015-08-18 02:28:03 +0000422 double r = strtod(pNode->u.zJContent, 0);
drh987eb1f2015-08-17 15:17:37 +0000423 sqlite3_result_double(pCtx, r);
drh5634cc02015-08-17 11:28:03 +0000424 break;
425 }
drh987eb1f2015-08-17 15:17:37 +0000426 case JSON_INT: {
427 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000428 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000429 if( z[0]=='-' ){ z++; }
430 while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
drh52216ad2015-08-18 02:28:03 +0000431 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000432 sqlite3_result_int64(pCtx, i);
433 break;
434 }
drh5634cc02015-08-17 11:28:03 +0000435 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000436 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000437 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
438 SQLITE_TRANSIENT);
drh301eecc2015-08-17 20:14:19 +0000439 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000440 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000441 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000442 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000443 }else{
444 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000445 u32 i;
446 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000447 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000448 char *zOut;
449 u32 j;
450 zOut = sqlite3_malloc( n+1 );
451 if( zOut==0 ){
452 sqlite3_result_error_nomem(pCtx);
453 break;
454 }
455 for(i=1, j=0; i<n-1; i++){
456 char c = z[i];
457 if( c!='\\' && z[i+1] ){
458 zOut[j++] = c;
459 }else{
460 c = z[++i];
461 if( c=='u' && z[1] ){
462 u32 v = 0, k;
drh8784eca2015-08-23 02:42:30 +0000463 for(k=0; k<4 && z[i+1]; i++, k++){
464 c = z[i+1];
drh987eb1f2015-08-17 15:17:37 +0000465 if( c>='0' && c<='9' ) v = v*16 + c - '0';
466 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
467 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
468 else break;
drh987eb1f2015-08-17 15:17:37 +0000469 }
470 if( v<=0x7f ){
471 zOut[j++] = v;
472 }else if( v<=0x7ff ){
473 zOut[j++] = 0xc0 | (v>>6);
474 zOut[j++] = 0x80 | (v&0x3f);
475 }else if( v<=0xffff ){
476 zOut[j++] = 0xe0 | (v>>12);
477 zOut[j++] = 0x80 | ((v>>6)&0x3f);
478 zOut[j++] = 0x80 | (v&0x3f);
479 }else if( v<=0x10ffff ){
480 zOut[j++] = 0xf0 | (v>>18);
481 zOut[j++] = 0x80 | ((v>>12)&0x3f);
482 zOut[j++] = 0x80 | ((v>>6)&0x3f);
483 zOut[j++] = 0x80 | (v&0x3f);
484 }
485 }else{
486 if( c=='b' ){
487 c = '\b';
488 }else if( c=='f' ){
489 c = '\f';
490 }else if( c=='n' ){
491 c = '\n';
492 }else if( c=='r' ){
493 c = '\r';
494 }else if( c=='t' ){
495 c = '\t';
496 }
497 zOut[j++] = c;
498 }
499 }
500 }
501 zOut[j] = 0;
502 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000503 }
504 break;
505 }
506 case JSON_ARRAY:
507 case JSON_OBJECT: {
drh505ad2c2015-08-21 17:33:11 +0000508 JsonString s;
drh5634cc02015-08-17 11:28:03 +0000509 jsonInit(&s, pCtx);
drhd0960592015-08-17 21:22:32 +0000510 jsonRenderNode(pNode, &s, aReplace);
drh5634cc02015-08-17 11:28:03 +0000511 jsonResult(&s);
512 break;
513 }
514 }
drhbd0621b2015-08-13 13:54:59 +0000515}
516
drh5fa5c102015-08-12 16:49:40 +0000517/*
drhe9c37f32015-08-15 21:25:36 +0000518** Create a new JsonNode instance based on the arguments and append that
519** instance to the JsonParse. Return the index in pParse->aNode[] of the
520** new node, or -1 if a memory allocation fails.
521*/
522static int jsonParseAddNode(
523 JsonParse *pParse, /* Append the node to this object */
524 u32 eType, /* Node type */
525 u32 n, /* Content size or sub-node count */
526 const char *zContent /* Content */
527){
528 JsonNode *p;
529 if( pParse->nNode>=pParse->nAlloc ){
530 u32 nNew;
531 JsonNode *pNew;
532 if( pParse->oom ) return -1;
533 nNew = pParse->nAlloc*2 + 10;
534 if( nNew<=pParse->nNode ){
535 pParse->oom = 1;
536 return -1;
537 }
538 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
539 if( pNew==0 ){
540 pParse->oom = 1;
541 return -1;
542 }
543 pParse->nAlloc = nNew;
544 pParse->aNode = pNew;
545 }
546 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000547 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000548 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000549 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000550 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000551 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000552 return pParse->nNode++;
553}
554
555/*
556** Parse a single JSON value which begins at pParse->zJson[i]. Return the
557** index of the first character past the end of the value parsed.
558**
559** Return negative for a syntax error. Special cases: return -2 if the
560** first non-whitespace character is '}' and return -3 if the first
561** non-whitespace character is ']'.
562*/
563static int jsonParseValue(JsonParse *pParse, u32 i){
564 char c;
565 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000566 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000567 int x;
568 while( isspace(pParse->zJson[i]) ){ i++; }
569 if( (c = pParse->zJson[i])==0 ) return 0;
570 if( c=='{' ){
571 /* Parse object */
572 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000573 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000574 for(j=i+1;;j++){
575 while( isspace(pParse->zJson[j]) ){ j++; }
576 x = jsonParseValue(pParse, j);
577 if( x<0 ){
drhbc8f0922015-08-22 19:39:04 +0000578 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000579 return -1;
580 }
drhbe9474e2015-08-22 03:05:54 +0000581 if( pParse->oom ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000582 if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1;
583 j = x;
584 while( isspace(pParse->zJson[j]) ){ j++; }
585 if( pParse->zJson[j]!=':' ) return -1;
586 j++;
587 x = jsonParseValue(pParse, j);
588 if( x<0 ) return -1;
589 j = x;
590 while( isspace(pParse->zJson[j]) ){ j++; }
591 c = pParse->zJson[j];
592 if( c==',' ) continue;
593 if( c!='}' ) return -1;
594 break;
595 }
drhbc8f0922015-08-22 19:39:04 +0000596 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000597 return j+1;
598 }else if( c=='[' ){
599 /* Parse array */
600 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000601 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000602 for(j=i+1;;j++){
603 while( isspace(pParse->zJson[j]) ){ j++; }
604 x = jsonParseValue(pParse, j);
605 if( x<0 ){
drhbc8f0922015-08-22 19:39:04 +0000606 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000607 return -1;
608 }
609 j = x;
610 while( isspace(pParse->zJson[j]) ){ j++; }
611 c = pParse->zJson[j];
612 if( c==',' ) continue;
613 if( c!=']' ) return -1;
614 break;
615 }
drhbc8f0922015-08-22 19:39:04 +0000616 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000617 return j+1;
618 }else if( c=='"' ){
619 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000620 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000621 j = i+1;
622 for(;;){
623 c = pParse->zJson[j];
624 if( c==0 ) return -1;
625 if( c=='\\' ){
626 c = pParse->zJson[++j];
627 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000628 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000629 }else if( c=='"' ){
630 break;
631 }
632 j++;
633 }
634 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drhbe9474e2015-08-22 03:05:54 +0000635 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000636 return j+1;
637 }else if( c=='n'
638 && strncmp(pParse->zJson+i,"null",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000639 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000640 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
641 return i+4;
642 }else if( c=='t'
643 && strncmp(pParse->zJson+i,"true",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000644 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000645 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
646 return i+4;
647 }else if( c=='f'
648 && strncmp(pParse->zJson+i,"false",5)==0
drhb2cd10e2015-08-15 21:29:14 +0000649 && !isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000650 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
651 return i+5;
652 }else if( c=='-' || (c>='0' && c<='9') ){
653 /* Parse number */
654 u8 seenDP = 0;
655 u8 seenE = 0;
656 j = i+1;
657 for(;; j++){
658 c = pParse->zJson[j];
659 if( c>='0' && c<='9' ) continue;
660 if( c=='.' ){
661 if( pParse->zJson[j-1]=='-' ) return -1;
662 if( seenDP ) return -1;
663 seenDP = 1;
664 continue;
665 }
666 if( c=='e' || c=='E' ){
667 if( pParse->zJson[j-1]<'0' ) return -1;
668 if( seenE ) return -1;
669 seenDP = seenE = 1;
670 c = pParse->zJson[j+1];
drh8784eca2015-08-23 02:42:30 +0000671 if( c=='+' || c=='-' ){
672 j++;
673 c = pParse->zJson[j+1];
674 }
675 if( c<'0' || c>'0' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000676 continue;
677 }
678 break;
679 }
680 if( pParse->zJson[j-1]<'0' ) return -1;
681 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
682 j - i, &pParse->zJson[i]);
683 return j;
684 }else if( c=='}' ){
685 return -2; /* End of {...} */
686 }else if( c==']' ){
687 return -3; /* End of [...] */
688 }else{
689 return -1; /* Syntax error */
690 }
691}
692
693/*
694** Parse a complete JSON string. Return 0 on success or non-zero if there
695** are any errors. If an error occurs, free all memory associated with
696** pParse.
697**
698** pParse is uninitialized when this routine is called.
699*/
drhbc8f0922015-08-22 19:39:04 +0000700static int jsonParse(
701 JsonParse *pParse, /* Initialize and fill this JsonParse object */
702 sqlite3_context *pCtx, /* Report errors here */
703 const char *zJson /* Input JSON text to be parsed */
704){
drhe9c37f32015-08-15 21:25:36 +0000705 int i;
drhe9c37f32015-08-15 21:25:36 +0000706 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000707 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000708 pParse->zJson = zJson;
709 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000710 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000711 if( i>0 ){
712 while( isspace(zJson[i]) ) i++;
713 if( zJson[i] ) i = -1;
714 }
715 if( i<0 ){
drhbc8f0922015-08-22 19:39:04 +0000716 if( pParse->oom && pCtx!=0 ) sqlite3_result_error_nomem(pCtx);
drh505ad2c2015-08-21 17:33:11 +0000717 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000718 return 1;
719 }
720 return 0;
721}
drh301eecc2015-08-17 20:14:19 +0000722
drh505ad2c2015-08-21 17:33:11 +0000723/* Mark node i of pParse as being a child of iParent. Call recursively
724** to fill in all the descendants of node i.
725*/
726static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
727 JsonNode *pNode = &pParse->aNode[i];
728 u32 j;
729 pParse->aUp[i] = iParent;
730 switch( pNode->eType ){
731 case JSON_ARRAY: {
732 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
733 jsonParseFillInParentage(pParse, i+j, i);
734 }
735 break;
736 }
737 case JSON_OBJECT: {
738 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
739 pParse->aUp[i+j] = i;
740 jsonParseFillInParentage(pParse, i+j+1, i);
741 }
742 break;
743 }
744 default: {
745 break;
746 }
747 }
748}
749
750/*
751** Compute the parentage of all nodes in a completed parse.
752*/
753static int jsonParseFindParents(JsonParse *pParse){
754 u32 *aUp;
755 assert( pParse->aUp==0 );
756 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000757 if( aUp==0 ){
758 pParse->oom = 1;
759 return SQLITE_NOMEM;
760 }
drh505ad2c2015-08-21 17:33:11 +0000761 jsonParseFillInParentage(pParse, 0, 0);
762 return SQLITE_OK;
763}
764
drh52216ad2015-08-18 02:28:03 +0000765/* forward declaration */
766static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*);
767
drh987eb1f2015-08-17 15:17:37 +0000768/*
769** Search along zPath to find the node specified. Return a pointer
770** to that node, or NULL if zPath is malformed or if there is no such
771** node.
drh52216ad2015-08-18 02:28:03 +0000772**
773** If pApnd!=0, then try to append new nodes to complete zPath if it is
774** possible to do so and if no existing node corresponds to zPath. If
775** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000776*/
drh52216ad2015-08-18 02:28:03 +0000777static JsonNode *jsonLookup(
778 JsonParse *pParse, /* The JSON to search */
779 u32 iRoot, /* Begin the search at this node */
780 const char *zPath, /* The path to search */
781 int *pApnd /* Append nodes to complete path if not NULL */
782){
drhbc8f0922015-08-22 19:39:04 +0000783 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000784 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000785 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000786 if( zPath[0]==0 ) return pRoot;
787 if( zPath[0]=='.' ){
788 if( pRoot->eType!=JSON_OBJECT ) return 0;
789 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000790 if( zPath[0]=='"' ){
791 zKey = zPath + 1;
792 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
793 nKey = i-1;
794 if( zPath[i] ) i++;
795 }else{
796 zKey = zPath;
797 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
798 nKey = i;
799 }
800 if( nKey==0 ) return 0;
drh987eb1f2015-08-17 15:17:37 +0000801 j = 1;
drh52216ad2015-08-18 02:28:03 +0000802 for(;;){
803 while( j<=pRoot->n ){
drh6b43cc82015-08-19 23:02:49 +0000804 if( pRoot[j].n==nKey+2
805 && strncmp(&pRoot[j].u.zJContent[1],zKey,nKey)==0
drh52216ad2015-08-18 02:28:03 +0000806 ){
807 return jsonLookup(pParse, iRoot+j+1, &zPath[i], pApnd);
808 }
809 j++;
drh505ad2c2015-08-21 17:33:11 +0000810 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000811 }
drh52216ad2015-08-18 02:28:03 +0000812 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
813 iRoot += pRoot->u.iAppend;
814 pRoot = &pParse->aNode[iRoot];
815 j = 1;
816 }
817 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +0000818 u32 iStart, iLabel;
819 JsonNode *pNode;
820 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
821 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +0000822 zPath += i;
drhbc8f0922015-08-22 19:39:04 +0000823 pNode = jsonLookupAppend(pParse, zPath, pApnd);
824 if( pParse->oom ) return 0;
825 if( pNode ){
826 pRoot = &pParse->aNode[iRoot];
827 pRoot->u.iAppend = iStart - iRoot;
828 pRoot->jnFlags |= JNODE_APPEND;
829 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
830 }
831 return pNode;
drh987eb1f2015-08-17 15:17:37 +0000832 }
833 }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
834 if( pRoot->eType!=JSON_ARRAY ) return 0;
835 i = 0;
836 zPath++;
837 while( isdigit(zPath[0]) ){
drh8784eca2015-08-23 02:42:30 +0000838 i = i*10 + zPath[0] - '0';
drh987eb1f2015-08-17 15:17:37 +0000839 zPath++;
840 }
841 if( zPath[0]!=']' ) return 0;
842 zPath++;
843 j = 1;
drh52216ad2015-08-18 02:28:03 +0000844 for(;;){
drhbc8f0922015-08-22 19:39:04 +0000845 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
846 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +0000847 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +0000848 }
849 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
850 iRoot += pRoot->u.iAppend;
851 pRoot = &pParse->aNode[iRoot];
852 j = 1;
drh987eb1f2015-08-17 15:17:37 +0000853 }
854 if( j<=pRoot->n ){
drh52216ad2015-08-18 02:28:03 +0000855 return jsonLookup(pParse, iRoot+j, zPath, pApnd);
856 }
857 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +0000858 u32 iStart;
859 JsonNode *pNode;
860 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
861 pNode = jsonLookupAppend(pParse, zPath, pApnd);
862 if( pParse->oom ) return 0;
863 if( pNode ){
864 pRoot = &pParse->aNode[iRoot];
865 pRoot->u.iAppend = iStart - iRoot;
866 pRoot->jnFlags |= JNODE_APPEND;
867 }
868 return pNode;
drh987eb1f2015-08-17 15:17:37 +0000869 }
870 }
871 return 0;
872}
873
drh52216ad2015-08-18 02:28:03 +0000874/*
drhbc8f0922015-08-22 19:39:04 +0000875** Append content to pParse that will complete zPath. Return a pointer
876** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +0000877*/
878static JsonNode *jsonLookupAppend(
879 JsonParse *pParse, /* Append content to the JSON parse */
880 const char *zPath, /* Description of content to append */
881 int *pApnd /* Set this flag to 1 */
882){
883 *pApnd = 1;
884 if( zPath[0]==0 ){
885 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
886 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
887 }
888 if( zPath[0]=='.' ){
889 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
890 }else if( strncmp(zPath,"[0]",3)==0 ){
891 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
892 }else{
893 return 0;
894 }
895 if( pParse->oom ) return 0;
896 return jsonLookup(pParse, pParse->nNode-1, zPath, pApnd);
897}
898
drhbc8f0922015-08-22 19:39:04 +0000899/*
900** Report the wrong number of arguments for json_insert(), json_replace()
901** or json_set().
902*/
903static void jsonWrongNumArgs(
904 sqlite3_context *pCtx,
905 const char *zFuncName
906){
907 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
908 zFuncName);
909 sqlite3_result_error(pCtx, zMsg, -1);
910 sqlite3_free(zMsg);
911}
drh52216ad2015-08-18 02:28:03 +0000912
drh987eb1f2015-08-17 15:17:37 +0000913/****************************************************************************
914** SQL functions used for testing and debugging
915****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +0000916
drh301eecc2015-08-17 20:14:19 +0000917#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000918/*
drh5634cc02015-08-17 11:28:03 +0000919** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +0000920** a parse of the JSON provided. Or it returns NULL if JSON is not
921** well-formed.
922*/
drh5634cc02015-08-17 11:28:03 +0000923static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +0000924 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +0000925 int argc,
926 sqlite3_value **argv
927){
drh505ad2c2015-08-21 17:33:11 +0000928 JsonString s; /* Output string - not real JSON */
929 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +0000930 u32 i;
drhe9c37f32015-08-15 21:25:36 +0000931
932 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +0000933 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +0000934 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +0000935 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +0000936 for(i=0; i<x.nNode; i++){
drh8784eca2015-08-23 02:42:30 +0000937 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%d\n",
938 i, jsonType[x.aNode[i].eType], x.aNode[i].n, x.aUp[i]);
drh52216ad2015-08-18 02:28:03 +0000939 if( x.aNode[i].u.zJContent!=0 ){
drh301eecc2015-08-17 20:14:19 +0000940 jsonAppendRaw(&s, " text: ", 10);
drh52216ad2015-08-18 02:28:03 +0000941 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000942 jsonAppendRaw(&s, "\n", 1);
943 }
944 }
drh505ad2c2015-08-21 17:33:11 +0000945 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +0000946 jsonResult(&s);
947}
948
drh5634cc02015-08-17 11:28:03 +0000949/*
950** The json_test1(JSON) function parses and rebuilds the JSON string.
951*/
952static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +0000953 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +0000954 int argc,
955 sqlite3_value **argv
956){
957 JsonParse x; /* The parse */
drhbc8f0922015-08-22 19:39:04 +0000958 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
959 jsonReturn(x.aNode, ctx, 0);
drh505ad2c2015-08-21 17:33:11 +0000960 jsonParseReset(&x);
drh5634cc02015-08-17 11:28:03 +0000961}
962
963/*
964** The json_nodecount(JSON) function returns the number of nodes in the
965** input JSON string.
966*/
967static void jsonNodeCountFunc(
drhbc8f0922015-08-22 19:39:04 +0000968 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +0000969 int argc,
970 sqlite3_value **argv
971){
972 JsonParse x; /* The parse */
drhbc8f0922015-08-22 19:39:04 +0000973 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
974 sqlite3_result_int64(ctx, (sqlite3_int64)x.nNode);
drh505ad2c2015-08-21 17:33:11 +0000975 jsonParseReset(&x);
drh5634cc02015-08-17 11:28:03 +0000976}
drh301eecc2015-08-17 20:14:19 +0000977#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +0000978
drh987eb1f2015-08-17 15:17:37 +0000979/****************************************************************************
980** SQL function implementations
981****************************************************************************/
982
983/*
984** Implementation of the json_array(VALUE,...) function. Return a JSON
985** array that contains all values given in arguments. Or if any argument
986** is a BLOB, throw an error.
987*/
988static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +0000989 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +0000990 int argc,
991 sqlite3_value **argv
992){
993 int i;
drh505ad2c2015-08-21 17:33:11 +0000994 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +0000995
drhbc8f0922015-08-22 19:39:04 +0000996 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +0000997 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +0000998 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +0000999 jsonAppendSeparator(&jx);
1000 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001001 }
drhd0960592015-08-17 21:22:32 +00001002 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001003 jsonResult(&jx);
1004}
1005
1006
1007/*
1008** json_array_length(JSON)
1009** json_array_length(JSON, PATH)
1010**
1011** Return the number of elements in the top-level JSON array.
1012** Return 0 if the input is not a well-formed JSON array.
1013*/
1014static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001015 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001016 int argc,
1017 sqlite3_value **argv
1018){
1019 JsonParse x; /* The parse */
1020 sqlite3_int64 n = 0;
1021 u32 i;
1022 const char *zPath;
1023
1024 if( argc==2 ){
1025 zPath = (const char*)sqlite3_value_text(argv[1]);
1026 if( zPath==0 ) return;
1027 if( zPath[0]!='$' ) return;
1028 zPath++;
1029 }else{
1030 zPath = 0;
1031 }
drhbc8f0922015-08-22 19:39:04 +00001032 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0]))==0 ){
drh987eb1f2015-08-17 15:17:37 +00001033 if( x.nNode ){
1034 JsonNode *pNode = x.aNode;
drh52216ad2015-08-18 02:28:03 +00001035 if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +00001036 if( pNode->eType==JSON_ARRAY ){
drh52216ad2015-08-18 02:28:03 +00001037 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
drh301eecc2015-08-17 20:14:19 +00001038 for(i=1; i<=pNode->n; n++){
drh505ad2c2015-08-21 17:33:11 +00001039 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001040 }
1041 }
1042 }
drh505ad2c2015-08-21 17:33:11 +00001043 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001044 }
drhbc8f0922015-08-22 19:39:04 +00001045 if( !x.oom ) sqlite3_result_int64(ctx, n);
drh987eb1f2015-08-17 15:17:37 +00001046}
1047
1048/*
1049** json_extract(JSON, PATH)
1050**
1051** Return the element described by PATH. Return NULL if JSON is not
1052** valid JSON or if there is no PATH element or if PATH is malformed.
1053*/
1054static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001055 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001056 int argc,
1057 sqlite3_value **argv
1058){
1059 JsonParse x; /* The parse */
1060 JsonNode *pNode;
1061 const char *zPath;
1062 assert( argc==2 );
1063 zPath = (const char*)sqlite3_value_text(argv[1]);
1064 if( zPath==0 ) return;
1065 if( zPath[0]!='$' ) return;
1066 zPath++;
drhbc8f0922015-08-22 19:39:04 +00001067 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh52216ad2015-08-18 02:28:03 +00001068 pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +00001069 if( pNode ){
drhbc8f0922015-08-22 19:39:04 +00001070 jsonReturn(pNode, ctx, 0);
drh987eb1f2015-08-17 15:17:37 +00001071 }
drh505ad2c2015-08-21 17:33:11 +00001072 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001073}
1074
1075/*
1076** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1077** object that contains all name/value given in arguments. Or if any name
1078** is not a string or if any value is a BLOB, throw an error.
1079*/
1080static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001081 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001082 int argc,
1083 sqlite3_value **argv
1084){
1085 int i;
drh505ad2c2015-08-21 17:33:11 +00001086 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001087 const char *z;
1088 u32 n;
1089
1090 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001091 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001092 "of arguments", -1);
1093 return;
1094 }
drhbc8f0922015-08-22 19:39:04 +00001095 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001096 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001097 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001098 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001099 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drh987eb1f2015-08-17 15:17:37 +00001100 jsonZero(&jx);
1101 return;
1102 }
drhd0960592015-08-17 21:22:32 +00001103 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001104 z = (const char*)sqlite3_value_text(argv[i]);
1105 n = (u32)sqlite3_value_bytes(argv[i]);
1106 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001107 jsonAppendChar(&jx, ':');
1108 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001109 }
drhd0960592015-08-17 21:22:32 +00001110 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001111 jsonResult(&jx);
1112}
1113
1114
1115/*
drh301eecc2015-08-17 20:14:19 +00001116** json_remove(JSON, PATH, ...)
1117**
1118** Remove the named elements from JSON and return the result. Ill-formed
1119** PATH arguments are silently ignored. If JSON is ill-formed, then NULL
1120** is returned.
1121*/
1122static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001123 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001124 int argc,
1125 sqlite3_value **argv
1126){
1127 JsonParse x; /* The parse */
1128 JsonNode *pNode;
1129 const char *zPath;
1130 u32 i;
1131
1132 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001133 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh301eecc2015-08-17 20:14:19 +00001134 if( x.nNode ){
drh6fd5c1e2015-08-21 20:37:12 +00001135 for(i=1; i<(u32)argc; i++){
drh301eecc2015-08-17 20:14:19 +00001136 zPath = (const char*)sqlite3_value_text(argv[i]);
1137 if( zPath==0 ) continue;
1138 if( zPath[0]!='$' ) continue;
drh52216ad2015-08-18 02:28:03 +00001139 pNode = jsonLookup(&x, 0, &zPath[1], 0);
drh301eecc2015-08-17 20:14:19 +00001140 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1141 }
1142 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
drhbc8f0922015-08-22 19:39:04 +00001143 jsonReturn(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001144 }
1145 }
drh505ad2c2015-08-21 17:33:11 +00001146 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001147}
1148
1149/*
1150** json_replace(JSON, PATH, VALUE, ...)
1151**
1152** Replace the value at PATH with VALUE. If PATH does not already exist,
1153** this routine is a no-op. If JSON is ill-formed, return NULL.
1154*/
1155static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001156 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001157 int argc,
1158 sqlite3_value **argv
1159){
1160 JsonParse x; /* The parse */
1161 JsonNode *pNode;
1162 const char *zPath;
1163 u32 i;
1164
1165 if( argc<1 ) return;
1166 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001167 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001168 return;
1169 }
drhbc8f0922015-08-22 19:39:04 +00001170 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drhd0960592015-08-17 21:22:32 +00001171 if( x.nNode ){
drh6fd5c1e2015-08-21 20:37:12 +00001172 for(i=1; i<(u32)argc; i+=2){
drhd0960592015-08-17 21:22:32 +00001173 zPath = (const char*)sqlite3_value_text(argv[i]);
1174 if( zPath==0 ) continue;
1175 if( zPath[0]!='$' ) continue;
drh52216ad2015-08-18 02:28:03 +00001176 pNode = jsonLookup(&x, 0, &zPath[1], 0);
drhd0960592015-08-17 21:22:32 +00001177 if( pNode ){
1178 pNode->jnFlags |= JNODE_REPLACE;
1179 pNode->iVal = i+1;
1180 }
1181 }
1182 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drhbc8f0922015-08-22 19:39:04 +00001183 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
drhd0960592015-08-17 21:22:32 +00001184 }else{
drhbc8f0922015-08-22 19:39:04 +00001185 jsonReturn(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001186 }
1187 }
drh505ad2c2015-08-21 17:33:11 +00001188 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001189}
drh505ad2c2015-08-21 17:33:11 +00001190
drh52216ad2015-08-18 02:28:03 +00001191/*
1192** json_set(JSON, PATH, VALUE, ...)
1193**
1194** Set the value at PATH to VALUE. Create the PATH if it does not already
1195** exist. Overwrite existing values that do exist.
1196** If JSON is ill-formed, return NULL.
1197**
1198** json_insert(JSON, PATH, VALUE, ...)
1199**
1200** Create PATH and initialize it to VALUE. If PATH already exists, this
1201** routine is a no-op. If JSON is ill-formed, return NULL.
1202*/
1203static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001204 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001205 int argc,
1206 sqlite3_value **argv
1207){
1208 JsonParse x; /* The parse */
1209 JsonNode *pNode;
1210 const char *zPath;
1211 u32 i;
1212 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001213 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001214
1215 if( argc<1 ) return;
1216 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001217 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001218 return;
1219 }
drhbc8f0922015-08-22 19:39:04 +00001220 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh52216ad2015-08-18 02:28:03 +00001221 if( x.nNode ){
drh6fd5c1e2015-08-21 20:37:12 +00001222 for(i=1; i<(u32)argc; i+=2){
drh52216ad2015-08-18 02:28:03 +00001223 zPath = (const char*)sqlite3_value_text(argv[i]);
1224 if( zPath==0 ) continue;
1225 if( zPath[0]!='$' ) continue;
1226 bApnd = 0;
1227 pNode = jsonLookup(&x, 0, &zPath[1], &bApnd);
drhbc8f0922015-08-22 19:39:04 +00001228 if( x.oom ){
1229 sqlite3_result_error_nomem(ctx);
1230 goto jsonSetDone;
1231 }else if( pNode && (bApnd || bIsSet) ){
drh52216ad2015-08-18 02:28:03 +00001232 pNode->jnFlags |= JNODE_REPLACE;
1233 pNode->iVal = i+1;
1234 }
1235 }
1236 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drhbc8f0922015-08-22 19:39:04 +00001237 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
drh52216ad2015-08-18 02:28:03 +00001238 }else{
drhbc8f0922015-08-22 19:39:04 +00001239 jsonReturn(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001240 }
1241 }
drhbc8f0922015-08-22 19:39:04 +00001242jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001243 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001244}
drh301eecc2015-08-17 20:14:19 +00001245
1246/*
drh987eb1f2015-08-17 15:17:37 +00001247** json_type(JSON)
1248** json_type(JSON, PATH)
1249**
1250** Return the top-level "type" of a JSON string. Return NULL if the
1251** input is not a well-formed JSON string.
1252*/
1253static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001254 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001255 int argc,
1256 sqlite3_value **argv
1257){
1258 JsonParse x; /* The parse */
1259 const char *zPath;
1260
1261 if( argc==2 ){
1262 zPath = (const char*)sqlite3_value_text(argv[1]);
1263 if( zPath==0 ) return;
1264 if( zPath[0]!='$' ) return;
1265 zPath++;
1266 }else{
1267 zPath = 0;
1268 }
drhbc8f0922015-08-22 19:39:04 +00001269 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh987eb1f2015-08-17 15:17:37 +00001270 if( x.nNode ){
1271 JsonNode *pNode = x.aNode;
drh52216ad2015-08-18 02:28:03 +00001272 if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
drhbc8f0922015-08-22 19:39:04 +00001273 if( pNode ){
1274 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
1275 }
drh987eb1f2015-08-17 15:17:37 +00001276 }
drh505ad2c2015-08-21 17:33:11 +00001277 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001278}
drh5634cc02015-08-17 11:28:03 +00001279
drhbc8f0922015-08-22 19:39:04 +00001280/*
1281** json_valid(JSON)
1282**
1283** Return 1 if JSON is a valid JSON string. Return 0 otherwise.
1284*/
1285static void jsonValidFunc(
1286 sqlite3_context *ctx,
1287 int argc,
1288 sqlite3_value **argv
1289){
1290 JsonParse x; /* The parse */
1291 int rc = 0;
1292
1293 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0]))==0
1294 && x.nNode>0
1295 ){
1296 rc = 1;
1297 }
1298 jsonParseReset(&x);
1299 sqlite3_result_int(ctx, rc);
1300}
1301
drhcb6c6c62015-08-19 22:47:17 +00001302/****************************************************************************
1303** The json_each virtual table
1304****************************************************************************/
1305typedef struct JsonEachCursor JsonEachCursor;
1306struct JsonEachCursor {
1307 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001308 u32 iRowid; /* The rowid */
1309 u32 i; /* Index in sParse.aNode[] of current row */
1310 u32 iEnd; /* EOF when i equals or exceeds this value */
1311 u8 eType; /* Type of top-level element */
1312 u8 bRecursive; /* True for json_tree(). False for json_each() */
1313 char *zJson; /* Input JSON */
1314 char *zPath; /* Path by which to filter zJson */
1315 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001316};
1317
1318/* Constructor for the json_each virtual table */
1319static int jsonEachConnect(
1320 sqlite3 *db,
1321 void *pAux,
1322 int argc, const char *const*argv,
1323 sqlite3_vtab **ppVtab,
1324 char **pzErr
1325){
1326 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001327 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001328
1329/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001330#define JEACH_KEY 0
1331#define JEACH_VALUE 1
1332#define JEACH_TYPE 2
1333#define JEACH_ATOM 3
1334#define JEACH_ID 4
1335#define JEACH_PARENT 5
1336#define JEACH_FULLKEY 6
1337#define JEACH_JSON 7
1338#define JEACH_PATH 8
drhcb6c6c62015-08-19 22:47:17 +00001339
drh6fd5c1e2015-08-21 20:37:12 +00001340 UNUSED_PARAM(pzErr);
1341 UNUSED_PARAM(argv);
1342 UNUSED_PARAM(argc);
1343 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001344 rc = sqlite3_declare_vtab(db,
drh4af352d2015-08-21 20:02:48 +00001345 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,"
1346 "json HIDDEN,path HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001347 if( rc==SQLITE_OK ){
1348 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1349 if( pNew==0 ) return SQLITE_NOMEM;
1350 memset(pNew, 0, sizeof(*pNew));
1351 }
1352 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001353}
1354
1355/* destructor for json_each virtual table */
1356static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1357 sqlite3_free(pVtab);
1358 return SQLITE_OK;
1359}
1360
drh505ad2c2015-08-21 17:33:11 +00001361/* constructor for a JsonEachCursor object for json_each(). */
1362static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001363 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001364
1365 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001366 pCur = sqlite3_malloc( sizeof(*pCur) );
1367 if( pCur==0 ) return SQLITE_NOMEM;
1368 memset(pCur, 0, sizeof(*pCur));
1369 *ppCursor = &pCur->base;
1370 return SQLITE_OK;
1371}
1372
drh505ad2c2015-08-21 17:33:11 +00001373/* constructor for a JsonEachCursor object for json_tree(). */
1374static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1375 int rc = jsonEachOpenEach(p, ppCursor);
1376 if( rc==SQLITE_OK ){
1377 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1378 pCur->bRecursive = 1;
1379 }
1380 return rc;
1381}
1382
drhcb6c6c62015-08-19 22:47:17 +00001383/* Reset a JsonEachCursor back to its original state. Free any memory
1384** held. */
1385static void jsonEachCursorReset(JsonEachCursor *p){
1386 sqlite3_free(p->zJson);
1387 sqlite3_free(p->zPath);
drh505ad2c2015-08-21 17:33:11 +00001388 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001389 p->iRowid = 0;
1390 p->i = 0;
1391 p->iEnd = 0;
1392 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001393 p->zJson = 0;
1394 p->zPath = 0;
1395}
1396
1397/* Destructor for a jsonEachCursor object */
1398static int jsonEachClose(sqlite3_vtab_cursor *cur){
1399 JsonEachCursor *p = (JsonEachCursor*)cur;
1400 jsonEachCursorReset(p);
1401 sqlite3_free(cur);
1402 return SQLITE_OK;
1403}
1404
1405/* Return TRUE if the jsonEachCursor object has been advanced off the end
1406** of the JSON object */
1407static int jsonEachEof(sqlite3_vtab_cursor *cur){
1408 JsonEachCursor *p = (JsonEachCursor*)cur;
1409 return p->i >= p->iEnd;
1410}
1411
drh505ad2c2015-08-21 17:33:11 +00001412/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001413static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001414 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001415 if( p->bRecursive ){
1416 if( p->i==0 ){
1417 p->i = 1;
drh4af352d2015-08-21 20:02:48 +00001418 }else{
drh8784eca2015-08-23 02:42:30 +00001419 u32 iUp = p->sParse.aUp[p->i];
1420 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001421 p->i++;
drh8784eca2015-08-23 02:42:30 +00001422 if( pUp->eType==JSON_OBJECT && (pUp->n + iUp >= p->i) ) p->i++;
drh4af352d2015-08-21 20:02:48 +00001423 }
1424 p->iRowid++;
1425 if( p->i<p->sParse.nNode ){
drh8784eca2015-08-23 02:42:30 +00001426 u32 iUp = p->sParse.aUp[p->i];
1427 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001428 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001429 if( pUp->eType==JSON_ARRAY ){
1430 if( iUp==p->i-1 ){
1431 pUp->u.iKey = 0;
1432 }else{
1433 pUp->u.iKey++;
1434 }
drh4af352d2015-08-21 20:02:48 +00001435 }
1436 }
drh505ad2c2015-08-21 17:33:11 +00001437 }else{
drh4af352d2015-08-21 20:02:48 +00001438 switch( p->eType ){
1439 case JSON_ARRAY: {
1440 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1441 p->iRowid++;
1442 break;
1443 }
1444 case JSON_OBJECT: {
1445 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1446 p->iRowid++;
1447 break;
1448 }
1449 default: {
1450 p->i = p->iEnd;
1451 break;
1452 }
drh505ad2c2015-08-21 17:33:11 +00001453 }
1454 }
1455 return SQLITE_OK;
1456}
1457
drh4af352d2015-08-21 20:02:48 +00001458/* Append the name of the path for element i to pStr
1459*/
1460static void jsonEachComputePath(
1461 JsonEachCursor *p, /* The cursor */
1462 JsonString *pStr, /* Write the path here */
1463 u32 i /* Path to this element */
1464){
1465 JsonNode *pNode, *pUp;
1466 u32 iUp;
1467 if( i==0 ){
1468 jsonAppendChar(pStr, '$');
1469 return;
drhcb6c6c62015-08-19 22:47:17 +00001470 }
drh4af352d2015-08-21 20:02:48 +00001471 iUp = p->sParse.aUp[i];
1472 jsonEachComputePath(p, pStr, iUp);
1473 pNode = &p->sParse.aNode[i];
1474 pUp = &p->sParse.aNode[iUp];
1475 if( pUp->eType==JSON_ARRAY ){
1476 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1477 }else{
1478 assert( pUp->eType==JSON_OBJECT );
1479 if( pNode->eType>=JSON_ARRAY ) pNode--;
1480 assert( pNode->eType==JSON_STRING );
1481 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1482 }
drhcb6c6c62015-08-19 22:47:17 +00001483}
1484
1485/* Return the value of a column */
1486static int jsonEachColumn(
1487 sqlite3_vtab_cursor *cur, /* The cursor */
1488 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1489 int i /* Which column to return */
1490){
1491 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00001492 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00001493 switch( i ){
1494 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00001495 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00001496 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00001497 jsonReturn(pThis, ctx, 0);
1498 }else if( p->eType==JSON_ARRAY ){
1499 u32 iKey;
1500 if( p->bRecursive ){
1501 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00001502 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00001503 }else{
1504 iKey = p->iRowid;
1505 }
drh6fd5c1e2015-08-21 20:37:12 +00001506 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00001507 }
1508 break;
1509 }
1510 case JEACH_VALUE: {
drh8784eca2015-08-23 02:42:30 +00001511 if( p->eType==JSON_OBJECT && p->i>0 ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001512 jsonReturn(pThis, ctx, 0);
1513 break;
1514 }
1515 case JEACH_TYPE: {
drh442a7c62015-08-24 02:32:04 +00001516 if( p->eType==JSON_OBJECT && p->i>0 ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001517 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
1518 break;
1519 }
1520 case JEACH_ATOM: {
drh442a7c62015-08-24 02:32:04 +00001521 if( p->eType==JSON_OBJECT && p->i>0 ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001522 if( pThis->eType>=JSON_ARRAY ) break;
1523 jsonReturn(pThis, ctx, 0);
1524 break;
1525 }
1526 case JEACH_ID: {
drh6fd5c1e2015-08-21 20:37:12 +00001527 sqlite3_result_int64(ctx, (sqlite3_int64)p->i + (p->eType==JSON_OBJECT));
drh505ad2c2015-08-21 17:33:11 +00001528 break;
1529 }
1530 case JEACH_PARENT: {
1531 if( p->i>0 && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00001532 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00001533 }
1534 break;
1535 }
drh4af352d2015-08-21 20:02:48 +00001536 case JEACH_FULLKEY: {
1537 JsonString x;
1538 jsonInit(&x, ctx);
1539 if( p->bRecursive ){
1540 jsonEachComputePath(p, &x, p->i);
1541 }else{
1542 if( p->zPath ){
1543 jsonAppendRaw(&x, p->zPath, (int)strlen(p->zPath));
1544 }else{
1545 jsonAppendChar(&x, '$');
1546 }
1547 if( p->eType==JSON_ARRAY ){
1548 jsonPrintf(30, &x, "[%d]", p->iRowid);
1549 }else{
1550 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
1551 }
1552 }
1553 jsonResult(&x);
1554 break;
1555 }
drhcb6c6c62015-08-19 22:47:17 +00001556 case JEACH_PATH: {
1557 const char *zPath = p->zPath;
drh4af352d2015-08-21 20:02:48 +00001558 if( zPath==0 ){
1559 if( p->bRecursive ){
1560 JsonString x;
1561 jsonInit(&x, ctx);
1562 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
1563 jsonResult(&x);
1564 break;
1565 }
1566 zPath = "$";
1567 }
drhcb6c6c62015-08-19 22:47:17 +00001568 sqlite3_result_text(ctx, zPath, -1, SQLITE_STATIC);
1569 break;
1570 }
1571 default: {
drh505ad2c2015-08-21 17:33:11 +00001572 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00001573 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1574 break;
1575 }
1576 }
1577 return SQLITE_OK;
1578}
1579
1580/* Return the current rowid value */
1581static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1582 JsonEachCursor *p = (JsonEachCursor*)cur;
1583 *pRowid = p->iRowid;
1584 return SQLITE_OK;
1585}
1586
1587/* The query strategy is to look for an equality constraint on the json
1588** column. Without such a constraint, the table cannot operate. idxNum is
1589** 1 if the constraint is found, 3 if the constraint and zPath are found,
1590** and 0 otherwise.
1591*/
1592static int jsonEachBestIndex(
1593 sqlite3_vtab *tab,
1594 sqlite3_index_info *pIdxInfo
1595){
1596 int i;
1597 int jsonIdx = -1;
1598 int pathIdx = -1;
1599 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00001600
1601 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00001602 pConstraint = pIdxInfo->aConstraint;
1603 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1604 if( pConstraint->usable==0 ) continue;
1605 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1606 switch( pConstraint->iColumn ){
1607 case JEACH_JSON: jsonIdx = i; break;
1608 case JEACH_PATH: pathIdx = i; break;
1609 default: /* no-op */ break;
1610 }
1611 }
1612 if( jsonIdx<0 ){
1613 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00001614 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00001615 }else{
drh505ad2c2015-08-21 17:33:11 +00001616 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00001617 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1618 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
1619 if( pathIdx<0 ){
1620 pIdxInfo->idxNum = 1;
1621 }else{
1622 pIdxInfo->aConstraintUsage[pathIdx].argvIndex = 2;
1623 pIdxInfo->aConstraintUsage[pathIdx].omit = 1;
1624 pIdxInfo->idxNum = 3;
1625 }
1626 }
1627 return SQLITE_OK;
1628}
1629
1630/* Start a search on a new JSON string */
1631static int jsonEachFilter(
1632 sqlite3_vtab_cursor *cur,
1633 int idxNum, const char *idxStr,
1634 int argc, sqlite3_value **argv
1635){
1636 JsonEachCursor *p = (JsonEachCursor*)cur;
1637 const char *z;
1638 const char *zPath;
1639 sqlite3_int64 n;
1640
drh6fd5c1e2015-08-21 20:37:12 +00001641 UNUSED_PARAM(idxStr);
1642 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00001643 jsonEachCursorReset(p);
1644 if( idxNum==0 ) return SQLITE_OK;
1645 z = (const char*)sqlite3_value_text(argv[0]);
1646 if( z==0 ) return SQLITE_OK;
1647 if( idxNum&2 ){
1648 zPath = (const char*)sqlite3_value_text(argv[1]);
1649 if( zPath==0 || zPath[0]!='$' ) return SQLITE_OK;
1650 }
1651 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00001652 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00001653 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00001654 memcpy(p->zJson, z, (size_t)n+1);
drhbc8f0922015-08-22 19:39:04 +00001655 if( jsonParse(&p->sParse, 0, p->zJson)
drh505ad2c2015-08-21 17:33:11 +00001656 || (p->bRecursive && jsonParseFindParents(&p->sParse))
1657 ){
drhcb6c6c62015-08-19 22:47:17 +00001658 jsonEachCursorReset(p);
1659 }else{
1660 JsonNode *pNode;
1661 if( idxNum==3 ){
drh4af352d2015-08-21 20:02:48 +00001662 p->bRecursive = 0;
drhcb6c6c62015-08-19 22:47:17 +00001663 n = sqlite3_value_bytes(argv[1]);
drh6fd5c1e2015-08-21 20:37:12 +00001664 p->zPath = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00001665 if( p->zPath==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00001666 memcpy(p->zPath, zPath, (size_t)n+1);
drhcb6c6c62015-08-19 22:47:17 +00001667 pNode = jsonLookup(&p->sParse, 0, p->zPath+1, 0);
1668 if( pNode==0 ){
1669 jsonEachCursorReset(p);
1670 return SQLITE_OK;
1671 }
1672 }else{
1673 pNode = p->sParse.aNode;
1674 }
1675 p->i = (int)(pNode - p->sParse.aNode);
1676 p->eType = pNode->eType;
1677 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00001678 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00001679 p->iEnd = p->i + pNode->n + 1;
drh8784eca2015-08-23 02:42:30 +00001680 if( !p->bRecursive ) p->i++;
drhcb6c6c62015-08-19 22:47:17 +00001681 }else{
1682 p->iEnd = p->i+1;
1683 }
1684 }
drhbc8f0922015-08-22 19:39:04 +00001685 return p->sParse.oom ? SQLITE_NOMEM : SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00001686}
1687
1688/* The methods of the json_each virtual table */
1689static sqlite3_module jsonEachModule = {
1690 0, /* iVersion */
1691 0, /* xCreate */
1692 jsonEachConnect, /* xConnect */
1693 jsonEachBestIndex, /* xBestIndex */
1694 jsonEachDisconnect, /* xDisconnect */
1695 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00001696 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00001697 jsonEachClose, /* xClose - close a cursor */
1698 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00001699 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00001700 jsonEachEof, /* xEof - check for end of scan */
1701 jsonEachColumn, /* xColumn - read data */
1702 jsonEachRowid, /* xRowid - read data */
1703 0, /* xUpdate */
1704 0, /* xBegin */
1705 0, /* xSync */
1706 0, /* xCommit */
1707 0, /* xRollback */
1708 0, /* xFindMethod */
1709 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00001710 0, /* xSavepoint */
1711 0, /* xRelease */
1712 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00001713};
1714
drh505ad2c2015-08-21 17:33:11 +00001715/* The methods of the json_tree virtual table. */
1716static sqlite3_module jsonTreeModule = {
1717 0, /* iVersion */
1718 0, /* xCreate */
1719 jsonEachConnect, /* xConnect */
1720 jsonEachBestIndex, /* xBestIndex */
1721 jsonEachDisconnect, /* xDisconnect */
1722 0, /* xDestroy */
1723 jsonEachOpenTree, /* xOpen - open a cursor */
1724 jsonEachClose, /* xClose - close a cursor */
1725 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00001726 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00001727 jsonEachEof, /* xEof - check for end of scan */
1728 jsonEachColumn, /* xColumn - read data */
1729 jsonEachRowid, /* xRowid - read data */
1730 0, /* xUpdate */
1731 0, /* xBegin */
1732 0, /* xSync */
1733 0, /* xCommit */
1734 0, /* xRollback */
1735 0, /* xFindMethod */
1736 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00001737 0, /* xSavepoint */
1738 0, /* xRelease */
1739 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00001740};
1741
1742/****************************************************************************
1743** The following routine is the only publically visible identifier in this
1744** file. Call the following routine in order to register the various SQL
1745** functions and the virtual table implemented by this file.
1746****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00001747
drh5fa5c102015-08-12 16:49:40 +00001748#ifdef _WIN32
1749__declspec(dllexport)
1750#endif
1751int sqlite3_json_init(
1752 sqlite3 *db,
1753 char **pzErrMsg,
1754 const sqlite3_api_routines *pApi
1755){
1756 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00001757 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00001758 static const struct {
1759 const char *zName;
1760 int nArg;
drh52216ad2015-08-18 02:28:03 +00001761 int flag;
drh5fa5c102015-08-12 16:49:40 +00001762 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1763 } aFunc[] = {
drh52216ad2015-08-18 02:28:03 +00001764 { "json_array", -1, 0, jsonArrayFunc },
1765 { "json_array_length", 1, 0, jsonArrayLengthFunc },
1766 { "json_array_length", 2, 0, jsonArrayLengthFunc },
1767 { "json_extract", 2, 0, jsonExtractFunc },
1768 { "json_insert", -1, 0, jsonSetFunc },
1769 { "json_object", -1, 0, jsonObjectFunc },
1770 { "json_remove", -1, 0, jsonRemoveFunc },
1771 { "json_replace", -1, 0, jsonReplaceFunc },
1772 { "json_set", -1, 1, jsonSetFunc },
1773 { "json_type", 1, 0, jsonTypeFunc },
1774 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00001775 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00001776
drh301eecc2015-08-17 20:14:19 +00001777#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00001778 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00001779 { "json_parse", 1, 0, jsonParseFunc },
1780 { "json_test1", 1, 0, jsonTest1Func },
1781 { "json_nodecount", 1, 0, jsonNodeCountFunc },
drh301eecc2015-08-17 20:14:19 +00001782#endif
drh5fa5c102015-08-12 16:49:40 +00001783 };
drh505ad2c2015-08-21 17:33:11 +00001784 static const struct {
1785 const char *zName;
1786 sqlite3_module *pModule;
1787 } aMod[] = {
1788 { "json_each", &jsonEachModule },
1789 { "json_tree", &jsonTreeModule },
1790 };
drh5fa5c102015-08-12 16:49:40 +00001791 SQLITE_EXTENSION_INIT2(pApi);
1792 (void)pzErrMsg; /* Unused parameter */
1793 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
1794 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00001795 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1796 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00001797 aFunc[i].xFunc, 0, 0);
1798 }
drh505ad2c2015-08-21 17:33:11 +00001799 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
1800 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00001801 }
drh5fa5c102015-08-12 16:49:40 +00001802 return rc;
1803}