| OLD | NEW |
| (Empty) |
| 1 /* | |
| 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 ** | |
| 18 ** 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 | |
| 20 ** 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.) | |
| 23 */ | |
| 24 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) | |
| 25 #if !defined(_SQLITEINT_H_) | |
| 26 #include "sqlite3ext.h" | |
| 27 #endif | |
| 28 SQLITE_EXTENSION_INIT1 | |
| 29 #include <assert.h> | |
| 30 #include <string.h> | |
| 31 #include <stdlib.h> | |
| 32 #include <stdarg.h> | |
| 33 | |
| 34 #define UNUSED_PARAM(X) (void)(X) | |
| 35 | |
| 36 #ifndef LARGEST_INT64 | |
| 37 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) | |
| 38 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) | |
| 39 #endif | |
| 40 | |
| 41 /* | |
| 42 ** Versions of isspace(), isalnum() and isdigit() to which it is safe | |
| 43 ** to pass signed char values. | |
| 44 */ | |
| 45 #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 | |
| 56 | |
| 57 /* | |
| 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 */ | |
| 62 static const char jsonIsSpace[] = { | |
| 63 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, | |
| 64 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 | |
| 82 #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 | |
| 89 | |
| 90 /* Objects */ | |
| 91 typedef struct JsonString JsonString; | |
| 92 typedef struct JsonNode JsonNode; | |
| 93 typedef struct JsonParse JsonParse; | |
| 94 | |
| 95 /* 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. | |
| 98 */ | |
| 99 struct JsonString { | |
| 100 sqlite3_context *pCtx; /* Function context - put error messages here */ | |
| 101 char *zBuf; /* Append JSON content here */ | |
| 102 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 */ | |
| 105 u8 bErr; /* True if an error has been encountered */ | |
| 106 char zSpace[100]; /* Initial static space */ | |
| 107 }; | |
| 108 | |
| 109 /* JSON type values | |
| 110 */ | |
| 111 #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 | |
| 120 /* The "subtype" set for JSON values */ | |
| 121 #define JSON_SUBTYPE 74 /* Ascii for "J" */ | |
| 122 | |
| 123 /* | |
| 124 ** Names of the various JSON types: | |
| 125 */ | |
| 126 static const char * const jsonType[] = { | |
| 127 "null", "true", "false", "integer", "real", "text", "array", "object" | |
| 128 }; | |
| 129 | |
| 130 /* 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 */ | |
| 135 #define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */ | |
| 136 #define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */ | |
| 137 #define JNODE_LABEL 0x20 /* Is a label of an object */ | |
| 138 | |
| 139 | |
| 140 /* A single node of parsed JSON | |
| 141 */ | |
| 142 struct JsonNode { | |
| 143 u8 eType; /* One of the JSON_ type values */ | |
| 144 u8 jnFlags; /* JNODE flags */ | |
| 145 u8 iVal; /* Replacement value when JNODE_REPLACE */ | |
| 146 u32 n; /* Bytes of content, or number of sub-nodes */ | |
| 147 union { | |
| 148 const char *zJContent; /* Content for INT, REAL, and STRING */ | |
| 149 u32 iAppend; /* More terms for ARRAY and OBJECT */ | |
| 150 u32 iKey; /* Key for ARRAY objects in json_tree() */ | |
| 151 } u; | |
| 152 }; | |
| 153 | |
| 154 /* A completely parsed JSON string | |
| 155 */ | |
| 156 struct 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 */ | |
| 161 u32 *aUp; /* Index of parent of each node */ | |
| 162 u8 oom; /* Set to true if out of memory */ | |
| 163 u8 nErr; /* Number of errors seen */ | |
| 164 }; | |
| 165 | |
| 166 /************************************************************************** | |
| 167 ** Utility routines for dealing with JsonString objects | |
| 168 **************************************************************************/ | |
| 169 | |
| 170 /* Set the JsonString object to an empty string | |
| 171 */ | |
| 172 static void jsonZero(JsonString *p){ | |
| 173 p->zBuf = p->zSpace; | |
| 174 p->nAlloc = sizeof(p->zSpace); | |
| 175 p->nUsed = 0; | |
| 176 p->bStatic = 1; | |
| 177 } | |
| 178 | |
| 179 /* Initialize the JsonString object | |
| 180 */ | |
| 181 static void jsonInit(JsonString *p, sqlite3_context *pCtx){ | |
| 182 p->pCtx = pCtx; | |
| 183 p->bErr = 0; | |
| 184 jsonZero(p); | |
| 185 } | |
| 186 | |
| 187 | |
| 188 /* Free all allocated memory and reset the JsonString object back to its | |
| 189 ** initial state. | |
| 190 */ | |
| 191 static void jsonReset(JsonString *p){ | |
| 192 if( !p->bStatic ) sqlite3_free(p->zBuf); | |
| 193 jsonZero(p); | |
| 194 } | |
| 195 | |
| 196 | |
| 197 /* Report an out-of-memory (OOM) condition | |
| 198 */ | |
| 199 static void jsonOom(JsonString *p){ | |
| 200 p->bErr = 1; | |
| 201 sqlite3_result_error_nomem(p->pCtx); | |
| 202 jsonReset(p); | |
| 203 } | |
| 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 */ | |
| 208 static int jsonGrow(JsonString *p, u32 N){ | |
| 209 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10; | |
| 210 char *zNew; | |
| 211 if( p->bStatic ){ | |
| 212 if( p->bErr ) return 1; | |
| 213 zNew = sqlite3_malloc64(nTotal); | |
| 214 if( zNew==0 ){ | |
| 215 jsonOom(p); | |
| 216 return SQLITE_NOMEM; | |
| 217 } | |
| 218 memcpy(zNew, p->zBuf, (size_t)p->nUsed); | |
| 219 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 | |
| 233 /* Append N bytes from zIn onto the end of the JsonString string. | |
| 234 */ | |
| 235 static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ | |
| 236 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 | |
| 241 /* Append formatted text (not to exceed N bytes) to the JsonString. | |
| 242 */ | |
| 243 static 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 | |
| 252 /* Append a single character | |
| 253 */ | |
| 254 static void jsonAppendChar(JsonString *p, char c){ | |
| 255 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return; | |
| 256 p->zBuf[p->nUsed++] = c; | |
| 257 } | |
| 258 | |
| 259 /* Append a comma separator to the output buffer, if the previous | |
| 260 ** character is not '[' or '{'. | |
| 261 */ | |
| 262 static void jsonAppendSeparator(JsonString *p){ | |
| 263 char c; | |
| 264 if( p->nUsed==0 ) return; | |
| 265 c = p->zBuf[p->nUsed-1]; | |
| 266 if( c!='[' && c!='{' ) jsonAppendChar(p, ','); | |
| 267 } | |
| 268 | |
| 269 /* Append the N-byte string in zIn to the end of the JsonString string | |
| 270 ** under construction. Enclose the string in "..." and escape | |
| 271 ** any double-quotes or backslash characters contained within the | |
| 272 ** string. | |
| 273 */ | |
| 274 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ | |
| 275 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=='\\' ){ | |
| 281 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return; | |
| 282 p->zBuf[p->nUsed++] = '\\'; | |
| 283 } | |
| 284 p->zBuf[p->nUsed++] = c; | |
| 285 } | |
| 286 p->zBuf[p->nUsed++] = '"'; | |
| 287 assert( p->nUsed<p->nAlloc ); | |
| 288 } | |
| 289 | |
| 290 /* | |
| 291 ** Append a function parameter value to the JSON string under | |
| 292 ** construction. | |
| 293 */ | |
| 294 static void jsonAppendValue( | |
| 295 JsonString *p, /* Append to this JSON string */ | |
| 296 sqlite3_value *pValue /* Value to append */ | |
| 297 ){ | |
| 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); | |
| 313 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ | |
| 314 jsonAppendRaw(p, z, n); | |
| 315 }else{ | |
| 316 jsonAppendString(p, z, n); | |
| 317 } | |
| 318 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 | |
| 332 /* Make the JSON in p the result of the SQL function. | |
| 333 */ | |
| 334 static void jsonResult(JsonString *p){ | |
| 335 if( p->bErr==0 ){ | |
| 336 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 | |
| 344 /************************************************************************** | |
| 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 */ | |
| 357 static 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 */ | |
| 365 static 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 | |
| 374 /* | |
| 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. | |
| 378 */ | |
| 379 static void jsonRenderNode( | |
| 380 JsonNode *pNode, /* The node to render */ | |
| 381 JsonString *pOut, /* Write JSON here */ | |
| 382 sqlite3_value **aReplace /* Replacement values */ | |
| 383 ){ | |
| 384 switch( pNode->eType ){ | |
| 385 default: { | |
| 386 assert( pNode->eType==JSON_NULL ); | |
| 387 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: { | |
| 399 if( pNode->jnFlags & JNODE_RAW ){ | |
| 400 jsonAppendString(pOut, pNode->u.zJContent, pNode->n); | |
| 401 break; | |
| 402 } | |
| 403 /* Fall through into the next case */ | |
| 404 } | |
| 405 case JSON_REAL: | |
| 406 case JSON_INT: { | |
| 407 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n); | |
| 408 break; | |
| 409 } | |
| 410 case JSON_ARRAY: { | |
| 411 u32 j = 1; | |
| 412 jsonAppendChar(pOut, '['); | |
| 413 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); | |
| 418 jsonAppendValue(pOut, aReplace[pNode[j].iVal]); | |
| 419 } | |
| 420 }else{ | |
| 421 jsonAppendSeparator(pOut); | |
| 422 jsonRenderNode(&pNode[j], pOut, aReplace); | |
| 423 } | |
| 424 j += jsonNodeSize(&pNode[j]); | |
| 425 } | |
| 426 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; | |
| 427 pNode = &pNode[pNode->u.iAppend]; | |
| 428 j = 1; | |
| 429 } | |
| 430 jsonAppendChar(pOut, ']'); | |
| 431 break; | |
| 432 } | |
| 433 case JSON_OBJECT: { | |
| 434 u32 j = 1; | |
| 435 jsonAppendChar(pOut, '{'); | |
| 436 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 ){ | |
| 443 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]); | |
| 444 }else{ | |
| 445 jsonRenderNode(&pNode[j+1], pOut, aReplace); | |
| 446 } | |
| 447 } | |
| 448 j += 1 + jsonNodeSize(&pNode[j+1]); | |
| 449 } | |
| 450 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; | |
| 451 pNode = &pNode[pNode->u.iAppend]; | |
| 452 j = 1; | |
| 453 } | |
| 454 jsonAppendChar(pOut, '}'); | |
| 455 break; | |
| 456 } | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 /* | |
| 461 ** Return a JsonNode and all its descendents as a JSON string. | |
| 462 */ | |
| 463 static 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); | |
| 472 sqlite3_result_subtype(pCtx, JSON_SUBTYPE); | |
| 473 } | |
| 474 | |
| 475 /* | |
| 476 ** Make the JsonNode the return value of the function. | |
| 477 */ | |
| 478 static 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 ){ | |
| 483 switch( pNode->eType ){ | |
| 484 default: { | |
| 485 assert( pNode->eType==JSON_NULL ); | |
| 486 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 } | |
| 497 case JSON_INT: { | |
| 498 sqlite3_int64 i = 0; | |
| 499 const char *z = pNode->u.zJContent; | |
| 500 if( z[0]=='-' ){ z++; } | |
| 501 while( z[0]>='0' && z[0]<='9' ){ | |
| 502 unsigned v = *(z++) - '0'; | |
| 503 if( i>=LARGEST_INT64/10 ){ | |
| 504 if( i>LARGEST_INT64/10 ) goto int_as_real; | |
| 505 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 } | |
| 518 if( pNode->u.zJContent[0]=='-' ){ i = -i; } | |
| 519 sqlite3_result_int64(pCtx, i); | |
| 520 int_done: | |
| 521 break; | |
| 522 int_as_real: /* fall through to real */; | |
| 523 } | |
| 524 case JSON_REAL: { | |
| 525 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 | |
| 532 sqlite3_result_double(pCtx, r); | |
| 533 break; | |
| 534 } | |
| 535 case JSON_STRING: { | |
| 536 #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() */ | |
| 539 if( pNode->jnFlags & JNODE_RAW ){ | |
| 540 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n, | |
| 541 SQLITE_TRANSIENT); | |
| 542 }else | |
| 543 #endif | |
| 544 assert( (pNode->jnFlags & JNODE_RAW)==0 ); | |
| 545 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){ | |
| 546 /* JSON formatted without any backslash-escapes */ | |
| 547 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2, | |
| 548 SQLITE_TRANSIENT); | |
| 549 }else{ | |
| 550 /* Translate JSON formatted string into raw text */ | |
| 551 u32 i; | |
| 552 u32 n = pNode->n; | |
| 553 const char *z = pNode->u.zJContent; | |
| 554 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]; | |
| 563 if( c!='\\' ){ | |
| 564 zOut[j++] = c; | |
| 565 }else{ | |
| 566 c = z[++i]; | |
| 567 if( c=='u' ){ | |
| 568 u32 v = 0, k; | |
| 569 for(k=0; k<4 && i<n-2; i++, k++){ | |
| 570 c = z[i+1]; | |
| 571 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; | |
| 575 } | |
| 576 if( v==0 ) break; | |
| 577 if( v<=0x7f ){ | |
| 578 zOut[j++] = (char)v; | |
| 579 }else if( v<=0x7ff ){ | |
| 580 zOut[j++] = (char)(0xc0 | (v>>6)); | |
| 581 zOut[j++] = 0x80 | (v&0x3f); | |
| 582 }else{ | |
| 583 zOut[j++] = (char)(0xe0 | (v>>12)); | |
| 584 zOut[j++] = 0x80 | ((v>>6)&0x3f); | |
| 585 zOut[j++] = 0x80 | (v&0x3f); | |
| 586 } | |
| 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); | |
| 605 } | |
| 606 break; | |
| 607 } | |
| 608 case JSON_ARRAY: | |
| 609 case JSON_OBJECT: { | |
| 610 jsonReturnJson(pNode, pCtx, aReplace); | |
| 611 break; | |
| 612 } | |
| 613 } | |
| 614 } | |
| 615 | |
| 616 /* Forward reference */ | |
| 617 static 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 | |
| 632 static 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 | |
| 654 /* | |
| 655 ** 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 */ | |
| 659 static 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 ){ | |
| 667 return jsonParseAddNodeExpand(pParse, eType, n, zContent); | |
| 668 } | |
| 669 p = &pParse->aNode[pParse->nNode]; | |
| 670 p->eType = (u8)eType; | |
| 671 p->jnFlags = 0; | |
| 672 p->iVal = 0; | |
| 673 p->n = n; | |
| 674 p->u.zJContent = zContent; | |
| 675 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 */ | |
| 686 static int jsonParseValue(JsonParse *pParse, u32 i){ | |
| 687 char c; | |
| 688 u32 j; | |
| 689 int iThis; | |
| 690 int x; | |
| 691 JsonNode *pNode; | |
| 692 while( safe_isspace(pParse->zJson[i]) ){ i++; } | |
| 693 if( (c = pParse->zJson[i])=='{' ){ | |
| 694 /* Parse object */ | |
| 695 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); | |
| 696 if( iThis<0 ) return -1; | |
| 697 for(j=i+1;;j++){ | |
| 698 while( safe_isspace(pParse->zJson[j]) ){ j++; } | |
| 699 x = jsonParseValue(pParse, j); | |
| 700 if( x<0 ){ | |
| 701 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1; | |
| 702 return -1; | |
| 703 } | |
| 704 if( pParse->oom ) return -1; | |
| 705 pNode = &pParse->aNode[pParse->nNode-1]; | |
| 706 if( pNode->eType!=JSON_STRING ) return -1; | |
| 707 pNode->jnFlags |= JNODE_LABEL; | |
| 708 j = x; | |
| 709 while( safe_isspace(pParse->zJson[j]) ){ j++; } | |
| 710 if( pParse->zJson[j]!=':' ) return -1; | |
| 711 j++; | |
| 712 x = jsonParseValue(pParse, j); | |
| 713 if( x<0 ) return -1; | |
| 714 j = x; | |
| 715 while( safe_isspace(pParse->zJson[j]) ){ j++; } | |
| 716 c = pParse->zJson[j]; | |
| 717 if( c==',' ) continue; | |
| 718 if( c!='}' ) return -1; | |
| 719 break; | |
| 720 } | |
| 721 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; | |
| 722 return j+1; | |
| 723 }else if( c=='[' ){ | |
| 724 /* Parse array */ | |
| 725 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); | |
| 726 if( iThis<0 ) return -1; | |
| 727 for(j=i+1;;j++){ | |
| 728 while( safe_isspace(pParse->zJson[j]) ){ j++; } | |
| 729 x = jsonParseValue(pParse, j); | |
| 730 if( x<0 ){ | |
| 731 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1; | |
| 732 return -1; | |
| 733 } | |
| 734 j = x; | |
| 735 while( safe_isspace(pParse->zJson[j]) ){ j++; } | |
| 736 c = pParse->zJson[j]; | |
| 737 if( c==',' ) continue; | |
| 738 if( c!=']' ) return -1; | |
| 739 break; | |
| 740 } | |
| 741 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; | |
| 742 return j+1; | |
| 743 }else if( c=='"' ){ | |
| 744 /* Parse string */ | |
| 745 u8 jnFlags = 0; | |
| 746 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; | |
| 753 jnFlags = JNODE_ESCAPE; | |
| 754 }else if( c=='"' ){ | |
| 755 break; | |
| 756 } | |
| 757 j++; | |
| 758 } | |
| 759 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); | |
| 760 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; | |
| 761 return j+1; | |
| 762 }else if( c=='n' | |
| 763 && strncmp(pParse->zJson+i,"null",4)==0 | |
| 764 && !safe_isalnum(pParse->zJson[i+4]) ){ | |
| 765 jsonParseAddNode(pParse, JSON_NULL, 0, 0); | |
| 766 return i+4; | |
| 767 }else if( c=='t' | |
| 768 && strncmp(pParse->zJson+i,"true",4)==0 | |
| 769 && !safe_isalnum(pParse->zJson[i+4]) ){ | |
| 770 jsonParseAddNode(pParse, JSON_TRUE, 0, 0); | |
| 771 return i+4; | |
| 772 }else if( c=='f' | |
| 773 && strncmp(pParse->zJson+i,"false",5)==0 | |
| 774 && !safe_isalnum(pParse->zJson[i+5]) ){ | |
| 775 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]; | |
| 796 if( c=='+' || c=='-' ){ | |
| 797 j++; | |
| 798 c = pParse->zJson[j+1]; | |
| 799 } | |
| 800 if( c<'0' || c>'9' ) return -1; | |
| 801 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 [...] */ | |
| 813 }else if( c==0 ){ | |
| 814 return 0; /* End of file */ | |
| 815 }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 */ | |
| 827 static 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 ){ | |
| 832 int i; | |
| 833 memset(pParse, 0, sizeof(*pParse)); | |
| 834 if( zJson==0 ) return 1; | |
| 835 pParse->zJson = zJson; | |
| 836 i = jsonParseValue(pParse, 0); | |
| 837 if( pParse->oom ) i = -1; | |
| 838 if( i>0 ){ | |
| 839 while( safe_isspace(zJson[i]) ) i++; | |
| 840 if( zJson[i] ) i = -1; | |
| 841 } | |
| 842 if( i<=0 ){ | |
| 843 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 } | |
| 850 jsonParseReset(pParse); | |
| 851 return 1; | |
| 852 } | |
| 853 return 0; | |
| 854 } | |
| 855 | |
| 856 /* Mark node i of pParse as being a child of iParent. Call recursively | |
| 857 ** to fill in all the descendants of node i. | |
| 858 */ | |
| 859 static 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 */ | |
| 886 static int jsonParseFindParents(JsonParse *pParse){ | |
| 887 u32 *aUp; | |
| 888 assert( pParse->aUp==0 ); | |
| 889 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode ); | |
| 890 if( aUp==0 ){ | |
| 891 pParse->oom = 1; | |
| 892 return SQLITE_NOMEM; | |
| 893 } | |
| 894 jsonParseFillInParentage(pParse, 0, 0); | |
| 895 return SQLITE_OK; | |
| 896 } | |
| 897 | |
| 898 /* | |
| 899 ** Compare the OBJECT label at pNode against zKey,nKey. Return true on | |
| 900 ** a match. | |
| 901 */ | |
| 902 static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){ | |
| 903 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 | |
| 912 /* forward declaration */ | |
| 913 static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**); | |
| 914 | |
| 915 /* | |
| 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. | |
| 919 ** | |
| 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. | |
| 923 */ | |
| 924 static JsonNode *jsonLookupStep( | |
| 925 JsonParse *pParse, /* The JSON to search */ | |
| 926 u32 iRoot, /* Begin the search at this node */ | |
| 927 const char *zPath, /* The path to search */ | |
| 928 int *pApnd, /* Append nodes to complete path if not NULL */ | |
| 929 const char **pzErr /* Make *pzErr point to any syntax error in zPath */ | |
| 930 ){ | |
| 931 u32 i, j, nKey; | |
| 932 const char *zKey; | |
| 933 JsonNode *pRoot = &pParse->aNode[iRoot]; | |
| 934 if( zPath[0]==0 ) return pRoot; | |
| 935 if( zPath[0]=='.' ){ | |
| 936 if( pRoot->eType!=JSON_OBJECT ) return 0; | |
| 937 zPath++; | |
| 938 if( zPath[0]=='"' ){ | |
| 939 zKey = zPath + 1; | |
| 940 for(i=1; zPath[i] && zPath[i]!='"'; i++){} | |
| 941 nKey = i-1; | |
| 942 if( zPath[i] ){ | |
| 943 i++; | |
| 944 }else{ | |
| 945 *pzErr = zPath; | |
| 946 return 0; | |
| 947 } | |
| 948 }else{ | |
| 949 zKey = zPath; | |
| 950 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){} | |
| 951 nKey = i; | |
| 952 } | |
| 953 if( nKey==0 ){ | |
| 954 *pzErr = zPath; | |
| 955 return 0; | |
| 956 } | |
| 957 j = 1; | |
| 958 for(;;){ | |
| 959 while( j<=pRoot->n ){ | |
| 960 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){ | |
| 961 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr); | |
| 962 } | |
| 963 j++; | |
| 964 j += jsonNodeSize(&pRoot[j]); | |
| 965 } | |
| 966 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; | |
| 967 iRoot += pRoot->u.iAppend; | |
| 968 pRoot = &pParse->aNode[iRoot]; | |
| 969 j = 1; | |
| 970 } | |
| 971 if( pApnd ){ | |
| 972 u32 iStart, iLabel; | |
| 973 JsonNode *pNode; | |
| 974 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0); | |
| 975 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath); | |
| 976 zPath += i; | |
| 977 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr); | |
| 978 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; | |
| 986 } | |
| 987 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){ | |
| 988 if( pRoot->eType!=JSON_ARRAY ) return 0; | |
| 989 i = 0; | |
| 990 j = 1; | |
| 991 while( safe_isdigit(zPath[j]) ){ | |
| 992 i = i*10 + zPath[j] - '0'; | |
| 993 j++; | |
| 994 } | |
| 995 if( zPath[j]!=']' ){ | |
| 996 *pzErr = zPath; | |
| 997 return 0; | |
| 998 } | |
| 999 zPath += j + 1; | |
| 1000 j = 1; | |
| 1001 for(;;){ | |
| 1002 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){ | |
| 1003 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--; | |
| 1004 j += jsonNodeSize(&pRoot[j]); | |
| 1005 } | |
| 1006 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; | |
| 1007 iRoot += pRoot->u.iAppend; | |
| 1008 pRoot = &pParse->aNode[iRoot]; | |
| 1009 j = 1; | |
| 1010 } | |
| 1011 if( j<=pRoot->n ){ | |
| 1012 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr); | |
| 1013 } | |
| 1014 if( i==0 && pApnd ){ | |
| 1015 u32 iStart; | |
| 1016 JsonNode *pNode; | |
| 1017 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0); | |
| 1018 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr); | |
| 1019 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; | |
| 1026 } | |
| 1027 }else{ | |
| 1028 *pzErr = zPath; | |
| 1029 } | |
| 1030 return 0; | |
| 1031 } | |
| 1032 | |
| 1033 /* | |
| 1034 ** Append content to pParse that will complete zPath. Return a pointer | |
| 1035 ** to the inserted node, or return NULL if the append fails. | |
| 1036 */ | |
| 1037 static JsonNode *jsonLookupAppend( | |
| 1038 JsonParse *pParse, /* Append content to the JSON parse */ | |
| 1039 const char *zPath, /* Description of content to append */ | |
| 1040 int *pApnd, /* Set this flag to 1 */ | |
| 1041 const char **pzErr /* Make this point to any syntax error */ | |
| 1042 ){ | |
| 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; | |
| 1056 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr); | |
| 1057 } | |
| 1058 | |
| 1059 /* | |
| 1060 ** Return the text of a syntax error message on a JSON path. Space is | |
| 1061 ** obtained from sqlite3_malloc(). | |
| 1062 */ | |
| 1063 static 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. | |
| 1076 */ | |
| 1077 static 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 */ | |
| 1081 sqlite3_context *pCtx /* Report errors here, if not NULL */ | |
| 1082 ){ | |
| 1083 const char *zErr = 0; | |
| 1084 JsonNode *pNode = 0; | |
| 1085 char *zMsg; | |
| 1086 | |
| 1087 if( zPath==0 ) return 0; | |
| 1088 if( zPath[0]!='$' ){ | |
| 1089 zErr = zPath; | |
| 1090 goto lookup_err; | |
| 1091 } | |
| 1092 zPath++; | |
| 1093 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr); | |
| 1094 if( zErr==0 ) return pNode; | |
| 1095 | |
| 1096 lookup_err: | |
| 1097 pParse->nErr++; | |
| 1098 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); | |
| 1105 } | |
| 1106 return 0; | |
| 1107 } | |
| 1108 | |
| 1109 | |
| 1110 /* | |
| 1111 ** Report the wrong number of arguments for json_insert(), json_replace() | |
| 1112 ** or json_set(). | |
| 1113 */ | |
| 1114 static 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 } | |
| 1123 | |
| 1124 | |
| 1125 /**************************************************************************** | |
| 1126 ** SQL functions used for testing and debugging | |
| 1127 ****************************************************************************/ | |
| 1128 | |
| 1129 #ifdef SQLITE_DEBUG | |
| 1130 /* | |
| 1131 ** The json_parse(JSON) function returns a string which describes | |
| 1132 ** a parse of the JSON provided. Or it returns NULL if JSON is not | |
| 1133 ** well-formed. | |
| 1134 */ | |
| 1135 static void jsonParseFunc( | |
| 1136 sqlite3_context *ctx, | |
| 1137 int argc, | |
| 1138 sqlite3_value **argv | |
| 1139 ){ | |
| 1140 JsonString s; /* Output string - not real JSON */ | |
| 1141 JsonParse x; /* The parse */ | |
| 1142 u32 i; | |
| 1143 | |
| 1144 assert( argc==1 ); | |
| 1145 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1146 jsonParseFindParents(&x); | |
| 1147 jsonInit(&s, ctx); | |
| 1148 for(i=0; i<x.nNode; i++){ | |
| 1149 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]; | |
| 1155 } | |
| 1156 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); | |
| 1163 } | |
| 1164 jsonParseReset(&x); | |
| 1165 jsonResult(&s); | |
| 1166 } | |
| 1167 | |
| 1168 /* | |
| 1169 ** 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. | |
| 1172 */ | |
| 1173 static void jsonTest1Func( | |
| 1174 sqlite3_context *ctx, | |
| 1175 int argc, | |
| 1176 sqlite3_value **argv | |
| 1177 ){ | |
| 1178 UNUSED_PARAM(argc); | |
| 1179 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE); | |
| 1180 } | |
| 1181 #endif /* SQLITE_DEBUG */ | |
| 1182 | |
| 1183 /**************************************************************************** | |
| 1184 ** Scalar 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 */ | |
| 1192 static void jsonArrayFunc( | |
| 1193 sqlite3_context *ctx, | |
| 1194 int argc, | |
| 1195 sqlite3_value **argv | |
| 1196 ){ | |
| 1197 int i; | |
| 1198 JsonString jx; | |
| 1199 | |
| 1200 jsonInit(&jx, ctx); | |
| 1201 jsonAppendChar(&jx, '['); | |
| 1202 for(i=0; i<argc; i++){ | |
| 1203 jsonAppendSeparator(&jx); | |
| 1204 jsonAppendValue(&jx, argv[i]); | |
| 1205 } | |
| 1206 jsonAppendChar(&jx, ']'); | |
| 1207 jsonResult(&jx); | |
| 1208 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | |
| 1209 } | |
| 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 */ | |
| 1219 static void jsonArrayLengthFunc( | |
| 1220 sqlite3_context *ctx, | |
| 1221 int argc, | |
| 1222 sqlite3_value **argv | |
| 1223 ){ | |
| 1224 JsonParse x; /* The parse */ | |
| 1225 sqlite3_int64 n = 0; | |
| 1226 u32 i; | |
| 1227 JsonNode *pNode; | |
| 1228 | |
| 1229 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1230 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]); | |
| 1243 } | |
| 1244 } | |
| 1245 if( x.nErr==0 ) sqlite3_result_int64(ctx, n); | |
| 1246 jsonParseReset(&x); | |
| 1247 } | |
| 1248 | |
| 1249 /* | |
| 1250 ** json_extract(JSON, PATH, ...) | |
| 1251 ** | |
| 1252 ** 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. | |
| 1256 */ | |
| 1257 static void jsonExtractFunc( | |
| 1258 sqlite3_context *ctx, | |
| 1259 int argc, | |
| 1260 sqlite3_value **argv | |
| 1261 ){ | |
| 1262 JsonParse x; /* The parse */ | |
| 1263 JsonNode *pNode; | |
| 1264 const char *zPath; | |
| 1265 JsonString jx; | |
| 1266 int i; | |
| 1267 | |
| 1268 if( argc<2 ) return; | |
| 1269 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1270 jsonInit(&jx, ctx); | |
| 1271 jsonAppendChar(&jx, '['); | |
| 1272 for(i=1; i<argc; i++){ | |
| 1273 zPath = (const char*)sqlite3_value_text(argv[i]); | |
| 1274 pNode = jsonLookup(&x, zPath, 0, ctx); | |
| 1275 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 } | |
| 1286 } | |
| 1287 if( argc>2 && i==argc ){ | |
| 1288 jsonAppendChar(&jx, ']'); | |
| 1289 jsonResult(&jx); | |
| 1290 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | |
| 1291 } | |
| 1292 jsonReset(&jx); | |
| 1293 jsonParseReset(&x); | |
| 1294 } | |
| 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 */ | |
| 1301 static void jsonObjectFunc( | |
| 1302 sqlite3_context *ctx, | |
| 1303 int argc, | |
| 1304 sqlite3_value **argv | |
| 1305 ){ | |
| 1306 int i; | |
| 1307 JsonString jx; | |
| 1308 const char *z; | |
| 1309 u32 n; | |
| 1310 | |
| 1311 if( argc&1 ){ | |
| 1312 sqlite3_result_error(ctx, "json_object() requires an even number " | |
| 1313 "of arguments", -1); | |
| 1314 return; | |
| 1315 } | |
| 1316 jsonInit(&jx, ctx); | |
| 1317 jsonAppendChar(&jx, '{'); | |
| 1318 for(i=0; i<argc; i+=2){ | |
| 1319 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){ | |
| 1320 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1); | |
| 1321 jsonReset(&jx); | |
| 1322 return; | |
| 1323 } | |
| 1324 jsonAppendSeparator(&jx); | |
| 1325 z = (const char*)sqlite3_value_text(argv[i]); | |
| 1326 n = (u32)sqlite3_value_bytes(argv[i]); | |
| 1327 jsonAppendString(&jx, z, n); | |
| 1328 jsonAppendChar(&jx, ':'); | |
| 1329 jsonAppendValue(&jx, argv[i+1]); | |
| 1330 } | |
| 1331 jsonAppendChar(&jx, '}'); | |
| 1332 jsonResult(&jx); | |
| 1333 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | |
| 1334 } | |
| 1335 | |
| 1336 | |
| 1337 /* | |
| 1338 ** json_remove(JSON, PATH, ...) | |
| 1339 ** | |
| 1340 ** Remove the named elements from JSON and return the result. malformed | |
| 1341 ** JSON or PATH arguments result in an error. | |
| 1342 */ | |
| 1343 static void jsonRemoveFunc( | |
| 1344 sqlite3_context *ctx, | |
| 1345 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; | |
| 1354 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1355 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); | |
| 1365 } | |
| 1366 remove_done: | |
| 1367 jsonParseReset(&x); | |
| 1368 } | |
| 1369 | |
| 1370 /* | |
| 1371 ** json_replace(JSON, PATH, VALUE, ...) | |
| 1372 ** | |
| 1373 ** Replace the value at PATH with VALUE. If PATH does not already exist, | |
| 1374 ** this routine is a no-op. If JSON or PATH is malformed, throw an error. | |
| 1375 */ | |
| 1376 static void jsonReplaceFunc( | |
| 1377 sqlite3_context *ctx, | |
| 1378 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 ) { | |
| 1388 jsonWrongNumArgs(ctx, "replace"); | |
| 1389 return; | |
| 1390 } | |
| 1391 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1392 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); | |
| 1400 } | |
| 1401 } | |
| 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); | |
| 1406 } | |
| 1407 replace_err: | |
| 1408 jsonParseReset(&x); | |
| 1409 } | |
| 1410 | |
| 1411 /* | |
| 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. | |
| 1416 ** If JSON or PATH is malformed, throw an error. | |
| 1417 ** | |
| 1418 ** json_insert(JSON, PATH, VALUE, ...) | |
| 1419 ** | |
| 1420 ** Create PATH and initialize it to VALUE. If PATH already exists, this | |
| 1421 ** routine is a no-op. If JSON or PATH is malformed, throw an error. | |
| 1422 */ | |
| 1423 static void jsonSetFunc( | |
| 1424 sqlite3_context *ctx, | |
| 1425 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; | |
| 1433 int bIsSet = *(int*)sqlite3_user_data(ctx); | |
| 1434 | |
| 1435 if( argc<1 ) return; | |
| 1436 if( (argc&1)==0 ) { | |
| 1437 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); | |
| 1438 return; | |
| 1439 } | |
| 1440 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1441 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); | |
| 1454 } | |
| 1455 } | |
| 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); | |
| 1460 } | |
| 1461 jsonSetDone: | |
| 1462 jsonParseReset(&x); | |
| 1463 } | |
| 1464 | |
| 1465 /* | |
| 1466 ** json_type(JSON) | |
| 1467 ** json_type(JSON, PATH) | |
| 1468 ** | |
| 1469 ** Return the top-level "type" of a JSON string. Throw an error if | |
| 1470 ** either the JSON or PATH inputs are not well-formed. | |
| 1471 */ | |
| 1472 static void jsonTypeFunc( | |
| 1473 sqlite3_context *ctx, | |
| 1474 int argc, | |
| 1475 sqlite3_value **argv | |
| 1476 ){ | |
| 1477 JsonParse x; /* The parse */ | |
| 1478 const char *zPath; | |
| 1479 JsonNode *pNode; | |
| 1480 | |
| 1481 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | |
| 1482 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); | |
| 1491 } | |
| 1492 jsonParseReset(&x); | |
| 1493 } | |
| 1494 | |
| 1495 /* | |
| 1496 ** json_valid(JSON) | |
| 1497 ** | |
| 1498 ** Return 1 if JSON is a well-formed JSON string according to RFC-7159. | |
| 1499 ** Return 0 otherwise. | |
| 1500 */ | |
| 1501 static void jsonValidFunc( | |
| 1502 sqlite3_context *ctx, | |
| 1503 int argc, | |
| 1504 sqlite3_value **argv | |
| 1505 ){ | |
| 1506 JsonParse x; /* The parse */ | |
| 1507 int rc = 0; | |
| 1508 | |
| 1509 UNUSED_PARAM(argc); | |
| 1510 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){ | |
| 1511 rc = 1; | |
| 1512 } | |
| 1513 jsonParseReset(&x); | |
| 1514 sqlite3_result_int(ctx, rc); | |
| 1515 } | |
| 1516 | |
| 1517 | |
| 1518 /**************************************************************************** | |
| 1519 ** Aggregate SQL function implementations | |
| 1520 ****************************************************************************/ | |
| 1521 /* | |
| 1522 ** json_group_array(VALUE) | |
| 1523 ** | |
| 1524 ** Return a JSON array composed of all values in the aggregate. | |
| 1525 */ | |
| 1526 static void jsonArrayStep( | |
| 1527 sqlite3_context *ctx, | |
| 1528 int argc, | |
| 1529 sqlite3_value **argv | |
| 1530 ){ | |
| 1531 JsonString *pStr; | |
| 1532 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); | |
| 1533 if( pStr ){ | |
| 1534 if( pStr->zBuf==0 ){ | |
| 1535 jsonInit(pStr, ctx); | |
| 1536 jsonAppendChar(pStr, '['); | |
| 1537 }else{ | |
| 1538 jsonAppendChar(pStr, ','); | |
| 1539 pStr->pCtx = ctx; | |
| 1540 } | |
| 1541 jsonAppendValue(pStr, argv[0]); | |
| 1542 } | |
| 1543 } | |
| 1544 static void jsonArrayFinal(sqlite3_context *ctx){ | |
| 1545 JsonString *pStr; | |
| 1546 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); | |
| 1547 if( pStr ){ | |
| 1548 pStr->pCtx = ctx; | |
| 1549 jsonAppendChar(pStr, ']'); | |
| 1550 if( pStr->bErr ){ | |
| 1551 sqlite3_result_error_nomem(ctx); | |
| 1552 assert( pStr->bStatic ); | |
| 1553 }else{ | |
| 1554 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, | |
| 1555 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); | |
| 1556 pStr->bStatic = 1; | |
| 1557 } | |
| 1558 }else{ | |
| 1559 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); | |
| 1560 } | |
| 1561 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | |
| 1562 } | |
| 1563 | |
| 1564 /* | |
| 1565 ** json_group_obj(NAME,VALUE) | |
| 1566 ** | |
| 1567 ** Return a JSON object composed of all names and values in the aggregate. | |
| 1568 */ | |
| 1569 static void jsonObjectStep( | |
| 1570 sqlite3_context *ctx, | |
| 1571 int argc, | |
| 1572 sqlite3_value **argv | |
| 1573 ){ | |
| 1574 JsonString *pStr; | |
| 1575 const char *z; | |
| 1576 u32 n; | |
| 1577 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); | |
| 1578 if( pStr ){ | |
| 1579 if( pStr->zBuf==0 ){ | |
| 1580 jsonInit(pStr, ctx); | |
| 1581 jsonAppendChar(pStr, '{'); | |
| 1582 }else{ | |
| 1583 jsonAppendChar(pStr, ','); | |
| 1584 pStr->pCtx = ctx; | |
| 1585 } | |
| 1586 z = (const char*)sqlite3_value_text(argv[0]); | |
| 1587 n = (u32)sqlite3_value_bytes(argv[0]); | |
| 1588 jsonAppendString(pStr, z, n); | |
| 1589 jsonAppendChar(pStr, ':'); | |
| 1590 jsonAppendValue(pStr, argv[1]); | |
| 1591 } | |
| 1592 } | |
| 1593 static void jsonObjectFinal(sqlite3_context *ctx){ | |
| 1594 JsonString *pStr; | |
| 1595 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); | |
| 1596 if( pStr ){ | |
| 1597 jsonAppendChar(pStr, '}'); | |
| 1598 if( pStr->bErr ){ | |
| 1599 sqlite3_result_error_nomem(ctx); | |
| 1600 assert( pStr->bStatic ); | |
| 1601 }else{ | |
| 1602 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, | |
| 1603 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); | |
| 1604 pStr->bStatic = 1; | |
| 1605 } | |
| 1606 }else{ | |
| 1607 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); | |
| 1608 } | |
| 1609 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | |
| 1610 } | |
| 1611 | |
| 1612 | |
| 1613 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 1614 /**************************************************************************** | |
| 1615 ** The json_each virtual table | |
| 1616 ****************************************************************************/ | |
| 1617 typedef struct JsonEachCursor JsonEachCursor; | |
| 1618 struct JsonEachCursor { | |
| 1619 sqlite3_vtab_cursor base; /* Base class - must be first */ | |
| 1620 u32 iRowid; /* The rowid */ | |
| 1621 u32 iBegin; /* The first node of the scan */ | |
| 1622 u32 i; /* Index in sParse.aNode[] of current row */ | |
| 1623 u32 iEnd; /* EOF when i equals or exceeds this value */ | |
| 1624 u8 eType; /* Type of top-level element */ | |
| 1625 u8 bRecursive; /* True for json_tree(). False for json_each() */ | |
| 1626 char *zJson; /* Input JSON */ | |
| 1627 char *zRoot; /* Path by which to filter zJson */ | |
| 1628 JsonParse sParse; /* Parse of the input JSON */ | |
| 1629 }; | |
| 1630 | |
| 1631 /* Constructor for the json_each virtual table */ | |
| 1632 static int jsonEachConnect( | |
| 1633 sqlite3 *db, | |
| 1634 void *pAux, | |
| 1635 int argc, const char *const*argv, | |
| 1636 sqlite3_vtab **ppVtab, | |
| 1637 char **pzErr | |
| 1638 ){ | |
| 1639 sqlite3_vtab *pNew; | |
| 1640 int rc; | |
| 1641 | |
| 1642 /* Column numbers */ | |
| 1643 #define JEACH_KEY 0 | |
| 1644 #define JEACH_VALUE 1 | |
| 1645 #define JEACH_TYPE 2 | |
| 1646 #define JEACH_ATOM 3 | |
| 1647 #define JEACH_ID 4 | |
| 1648 #define JEACH_PARENT 5 | |
| 1649 #define JEACH_FULLKEY 6 | |
| 1650 #define JEACH_PATH 7 | |
| 1651 #define JEACH_JSON 8 | |
| 1652 #define JEACH_ROOT 9 | |
| 1653 | |
| 1654 UNUSED_PARAM(pzErr); | |
| 1655 UNUSED_PARAM(argv); | |
| 1656 UNUSED_PARAM(argc); | |
| 1657 UNUSED_PARAM(pAux); | |
| 1658 rc = sqlite3_declare_vtab(db, | |
| 1659 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path," | |
| 1660 "json HIDDEN,root HIDDEN)"); | |
| 1661 if( rc==SQLITE_OK ){ | |
| 1662 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); | |
| 1663 if( pNew==0 ) return SQLITE_NOMEM; | |
| 1664 memset(pNew, 0, sizeof(*pNew)); | |
| 1665 } | |
| 1666 return rc; | |
| 1667 } | |
| 1668 | |
| 1669 /* destructor for json_each virtual table */ | |
| 1670 static int jsonEachDisconnect(sqlite3_vtab *pVtab){ | |
| 1671 sqlite3_free(pVtab); | |
| 1672 return SQLITE_OK; | |
| 1673 } | |
| 1674 | |
| 1675 /* constructor for a JsonEachCursor object for json_each(). */ | |
| 1676 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ | |
| 1677 JsonEachCursor *pCur; | |
| 1678 | |
| 1679 UNUSED_PARAM(p); | |
| 1680 pCur = sqlite3_malloc( sizeof(*pCur) ); | |
| 1681 if( pCur==0 ) return SQLITE_NOMEM; | |
| 1682 memset(pCur, 0, sizeof(*pCur)); | |
| 1683 *ppCursor = &pCur->base; | |
| 1684 return SQLITE_OK; | |
| 1685 } | |
| 1686 | |
| 1687 /* constructor for a JsonEachCursor object for json_tree(). */ | |
| 1688 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ | |
| 1689 int rc = jsonEachOpenEach(p, ppCursor); | |
| 1690 if( rc==SQLITE_OK ){ | |
| 1691 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor; | |
| 1692 pCur->bRecursive = 1; | |
| 1693 } | |
| 1694 return rc; | |
| 1695 } | |
| 1696 | |
| 1697 /* Reset a JsonEachCursor back to its original state. Free any memory | |
| 1698 ** held. */ | |
| 1699 static void jsonEachCursorReset(JsonEachCursor *p){ | |
| 1700 sqlite3_free(p->zJson); | |
| 1701 sqlite3_free(p->zRoot); | |
| 1702 jsonParseReset(&p->sParse); | |
| 1703 p->iRowid = 0; | |
| 1704 p->i = 0; | |
| 1705 p->iEnd = 0; | |
| 1706 p->eType = 0; | |
| 1707 p->zJson = 0; | |
| 1708 p->zRoot = 0; | |
| 1709 } | |
| 1710 | |
| 1711 /* Destructor for a jsonEachCursor object */ | |
| 1712 static int jsonEachClose(sqlite3_vtab_cursor *cur){ | |
| 1713 JsonEachCursor *p = (JsonEachCursor*)cur; | |
| 1714 jsonEachCursorReset(p); | |
| 1715 sqlite3_free(cur); | |
| 1716 return SQLITE_OK; | |
| 1717 } | |
| 1718 | |
| 1719 /* Return TRUE if the jsonEachCursor object has been advanced off the end | |
| 1720 ** of the JSON object */ | |
| 1721 static int jsonEachEof(sqlite3_vtab_cursor *cur){ | |
| 1722 JsonEachCursor *p = (JsonEachCursor*)cur; | |
| 1723 return p->i >= p->iEnd; | |
| 1724 } | |
| 1725 | |
| 1726 /* Advance the cursor to the next element for json_tree() */ | |
| 1727 static int jsonEachNext(sqlite3_vtab_cursor *cur){ | |
| 1728 JsonEachCursor *p = (JsonEachCursor*)cur; | |
| 1729 if( p->bRecursive ){ | |
| 1730 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++; | |
| 1731 p->i++; | |
| 1732 p->iRowid++; | |
| 1733 if( p->i<p->iEnd ){ | |
| 1734 u32 iUp = p->sParse.aUp[p->i]; | |
| 1735 JsonNode *pUp = &p->sParse.aNode[iUp]; | |
| 1736 p->eType = pUp->eType; | |
| 1737 if( pUp->eType==JSON_ARRAY ){ | |
| 1738 if( iUp==p->i-1 ){ | |
| 1739 pUp->u.iKey = 0; | |
| 1740 }else{ | |
| 1741 pUp->u.iKey++; | |
| 1742 } | |
| 1743 } | |
| 1744 } | |
| 1745 }else{ | |
| 1746 switch( p->eType ){ | |
| 1747 case JSON_ARRAY: { | |
| 1748 p->i += jsonNodeSize(&p->sParse.aNode[p->i]); | |
| 1749 p->iRowid++; | |
| 1750 break; | |
| 1751 } | |
| 1752 case JSON_OBJECT: { | |
| 1753 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]); | |
| 1754 p->iRowid++; | |
| 1755 break; | |
| 1756 } | |
| 1757 default: { | |
| 1758 p->i = p->iEnd; | |
| 1759 break; | |
| 1760 } | |
| 1761 } | |
| 1762 } | |
| 1763 return SQLITE_OK; | |
| 1764 } | |
| 1765 | |
| 1766 /* Append the name of the path for element i to pStr | |
| 1767 */ | |
| 1768 static void jsonEachComputePath( | |
| 1769 JsonEachCursor *p, /* The cursor */ | |
| 1770 JsonString *pStr, /* Write the path here */ | |
| 1771 u32 i /* Path to this element */ | |
| 1772 ){ | |
| 1773 JsonNode *pNode, *pUp; | |
| 1774 u32 iUp; | |
| 1775 if( i==0 ){ | |
| 1776 jsonAppendChar(pStr, '$'); | |
| 1777 return; | |
| 1778 } | |
| 1779 iUp = p->sParse.aUp[i]; | |
| 1780 jsonEachComputePath(p, pStr, iUp); | |
| 1781 pNode = &p->sParse.aNode[i]; | |
| 1782 pUp = &p->sParse.aNode[iUp]; | |
| 1783 if( pUp->eType==JSON_ARRAY ){ | |
| 1784 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey); | |
| 1785 }else{ | |
| 1786 assert( pUp->eType==JSON_OBJECT ); | |
| 1787 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--; | |
| 1788 assert( pNode->eType==JSON_STRING ); | |
| 1789 assert( pNode->jnFlags & JNODE_LABEL ); | |
| 1790 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1); | |
| 1791 } | |
| 1792 } | |
| 1793 | |
| 1794 /* Return the value of a column */ | |
| 1795 static int jsonEachColumn( | |
| 1796 sqlite3_vtab_cursor *cur, /* The cursor */ | |
| 1797 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ | |
| 1798 int i /* Which column to return */ | |
| 1799 ){ | |
| 1800 JsonEachCursor *p = (JsonEachCursor*)cur; | |
| 1801 JsonNode *pThis = &p->sParse.aNode[p->i]; | |
| 1802 switch( i ){ | |
| 1803 case JEACH_KEY: { | |
| 1804 if( p->i==0 ) break; | |
| 1805 if( p->eType==JSON_OBJECT ){ | |
| 1806 jsonReturn(pThis, ctx, 0); | |
| 1807 }else if( p->eType==JSON_ARRAY ){ | |
| 1808 u32 iKey; | |
| 1809 if( p->bRecursive ){ | |
| 1810 if( p->iRowid==0 ) break; | |
| 1811 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey; | |
| 1812 }else{ | |
| 1813 iKey = p->iRowid; | |
| 1814 } | |
| 1815 sqlite3_result_int64(ctx, (sqlite3_int64)iKey); | |
| 1816 } | |
| 1817 break; | |
| 1818 } | |
| 1819 case JEACH_VALUE: { | |
| 1820 if( pThis->jnFlags & JNODE_LABEL ) pThis++; | |
| 1821 jsonReturn(pThis, ctx, 0); | |
| 1822 break; | |
| 1823 } | |
| 1824 case JEACH_TYPE: { | |
| 1825 if( pThis->jnFlags & JNODE_LABEL ) pThis++; | |
| 1826 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC); | |
| 1827 break; | |
| 1828 } | |
| 1829 case JEACH_ATOM: { | |
| 1830 if( pThis->jnFlags & JNODE_LABEL ) pThis++; | |
| 1831 if( pThis->eType>=JSON_ARRAY ) break; | |
| 1832 jsonReturn(pThis, ctx, 0); | |
| 1833 break; | |
| 1834 } | |
| 1835 case JEACH_ID: { | |
| 1836 sqlite3_result_int64(ctx, | |
| 1837 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0)); | |
| 1838 break; | |
| 1839 } | |
| 1840 case JEACH_PARENT: { | |
| 1841 if( p->i>p->iBegin && p->bRecursive ){ | |
| 1842 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]); | |
| 1843 } | |
| 1844 break; | |
| 1845 } | |
| 1846 case JEACH_FULLKEY: { | |
| 1847 JsonString x; | |
| 1848 jsonInit(&x, ctx); | |
| 1849 if( p->bRecursive ){ | |
| 1850 jsonEachComputePath(p, &x, p->i); | |
| 1851 }else{ | |
| 1852 if( p->zRoot ){ | |
| 1853 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot)); | |
| 1854 }else{ | |
| 1855 jsonAppendChar(&x, '$'); | |
| 1856 } | |
| 1857 if( p->eType==JSON_ARRAY ){ | |
| 1858 jsonPrintf(30, &x, "[%d]", p->iRowid); | |
| 1859 }else{ | |
| 1860 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1); | |
| 1861 } | |
| 1862 } | |
| 1863 jsonResult(&x); | |
| 1864 break; | |
| 1865 } | |
| 1866 case JEACH_PATH: { | |
| 1867 if( p->bRecursive ){ | |
| 1868 JsonString x; | |
| 1869 jsonInit(&x, ctx); | |
| 1870 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]); | |
| 1871 jsonResult(&x); | |
| 1872 break; | |
| 1873 } | |
| 1874 /* For json_each() path and root are the same so fall through | |
| 1875 ** into the root case */ | |
| 1876 } | |
| 1877 case JEACH_ROOT: { | |
| 1878 const char *zRoot = p->zRoot; | |
| 1879 if( zRoot==0 ) zRoot = "$"; | |
| 1880 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC); | |
| 1881 break; | |
| 1882 } | |
| 1883 case JEACH_JSON: { | |
| 1884 assert( i==JEACH_JSON ); | |
| 1885 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC); | |
| 1886 break; | |
| 1887 } | |
| 1888 } | |
| 1889 return SQLITE_OK; | |
| 1890 } | |
| 1891 | |
| 1892 /* Return the current rowid value */ | |
| 1893 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ | |
| 1894 JsonEachCursor *p = (JsonEachCursor*)cur; | |
| 1895 *pRowid = p->iRowid; | |
| 1896 return SQLITE_OK; | |
| 1897 } | |
| 1898 | |
| 1899 /* The query strategy is to look for an equality constraint on the json | |
| 1900 ** column. Without such a constraint, the table cannot operate. idxNum is | |
| 1901 ** 1 if the constraint is found, 3 if the constraint and zRoot are found, | |
| 1902 ** and 0 otherwise. | |
| 1903 */ | |
| 1904 static int jsonEachBestIndex( | |
| 1905 sqlite3_vtab *tab, | |
| 1906 sqlite3_index_info *pIdxInfo | |
| 1907 ){ | |
| 1908 int i; | |
| 1909 int jsonIdx = -1; | |
| 1910 int rootIdx = -1; | |
| 1911 const struct sqlite3_index_constraint *pConstraint; | |
| 1912 | |
| 1913 UNUSED_PARAM(tab); | |
| 1914 pConstraint = pIdxInfo->aConstraint; | |
| 1915 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ | |
| 1916 if( pConstraint->usable==0 ) continue; | |
| 1917 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; | |
| 1918 switch( pConstraint->iColumn ){ | |
| 1919 case JEACH_JSON: jsonIdx = i; break; | |
| 1920 case JEACH_ROOT: rootIdx = i; break; | |
| 1921 default: /* no-op */ break; | |
| 1922 } | |
| 1923 } | |
| 1924 if( jsonIdx<0 ){ | |
| 1925 pIdxInfo->idxNum = 0; | |
| 1926 pIdxInfo->estimatedCost = 1e99; | |
| 1927 }else{ | |
| 1928 pIdxInfo->estimatedCost = 1.0; | |
| 1929 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1; | |
| 1930 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1; | |
| 1931 if( rootIdx<0 ){ | |
| 1932 pIdxInfo->idxNum = 1; | |
| 1933 }else{ | |
| 1934 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2; | |
| 1935 pIdxInfo->aConstraintUsage[rootIdx].omit = 1; | |
| 1936 pIdxInfo->idxNum = 3; | |
| 1937 } | |
| 1938 } | |
| 1939 return SQLITE_OK; | |
| 1940 } | |
| 1941 | |
| 1942 /* Start a search on a new JSON string */ | |
| 1943 static int jsonEachFilter( | |
| 1944 sqlite3_vtab_cursor *cur, | |
| 1945 int idxNum, const char *idxStr, | |
| 1946 int argc, sqlite3_value **argv | |
| 1947 ){ | |
| 1948 JsonEachCursor *p = (JsonEachCursor*)cur; | |
| 1949 const char *z; | |
| 1950 const char *zRoot = 0; | |
| 1951 sqlite3_int64 n; | |
| 1952 | |
| 1953 UNUSED_PARAM(idxStr); | |
| 1954 UNUSED_PARAM(argc); | |
| 1955 jsonEachCursorReset(p); | |
| 1956 if( idxNum==0 ) return SQLITE_OK; | |
| 1957 z = (const char*)sqlite3_value_text(argv[0]); | |
| 1958 if( z==0 ) return SQLITE_OK; | |
| 1959 n = sqlite3_value_bytes(argv[0]); | |
| 1960 p->zJson = sqlite3_malloc64( n+1 ); | |
| 1961 if( p->zJson==0 ) return SQLITE_NOMEM; | |
| 1962 memcpy(p->zJson, z, (size_t)n+1); | |
| 1963 if( jsonParse(&p->sParse, 0, p->zJson) ){ | |
| 1964 int rc = SQLITE_NOMEM; | |
| 1965 if( p->sParse.oom==0 ){ | |
| 1966 sqlite3_free(cur->pVtab->zErrMsg); | |
| 1967 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON"); | |
| 1968 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR; | |
| 1969 } | |
| 1970 jsonEachCursorReset(p); | |
| 1971 return rc; | |
| 1972 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){ | |
| 1973 jsonEachCursorReset(p); | |
| 1974 return SQLITE_NOMEM; | |
| 1975 }else{ | |
| 1976 JsonNode *pNode = 0; | |
| 1977 if( idxNum==3 ){ | |
| 1978 const char *zErr = 0; | |
| 1979 zRoot = (const char*)sqlite3_value_text(argv[1]); | |
| 1980 if( zRoot==0 ) return SQLITE_OK; | |
| 1981 n = sqlite3_value_bytes(argv[1]); | |
| 1982 p->zRoot = sqlite3_malloc64( n+1 ); | |
| 1983 if( p->zRoot==0 ) return SQLITE_NOMEM; | |
| 1984 memcpy(p->zRoot, zRoot, (size_t)n+1); | |
| 1985 if( zRoot[0]!='$' ){ | |
| 1986 zErr = zRoot; | |
| 1987 }else{ | |
| 1988 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr); | |
| 1989 } | |
| 1990 if( zErr ){ | |
| 1991 sqlite3_free(cur->pVtab->zErrMsg); | |
| 1992 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr); | |
| 1993 jsonEachCursorReset(p); | |
| 1994 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; | |
| 1995 }else if( pNode==0 ){ | |
| 1996 return SQLITE_OK; | |
| 1997 } | |
| 1998 }else{ | |
| 1999 pNode = p->sParse.aNode; | |
| 2000 } | |
| 2001 p->iBegin = p->i = (int)(pNode - p->sParse.aNode); | |
| 2002 p->eType = pNode->eType; | |
| 2003 if( p->eType>=JSON_ARRAY ){ | |
| 2004 pNode->u.iKey = 0; | |
| 2005 p->iEnd = p->i + pNode->n + 1; | |
| 2006 if( p->bRecursive ){ | |
| 2007 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType; | |
| 2008 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){ | |
| 2009 p->i--; | |
| 2010 } | |
| 2011 }else{ | |
| 2012 p->i++; | |
| 2013 } | |
| 2014 }else{ | |
| 2015 p->iEnd = p->i+1; | |
| 2016 } | |
| 2017 } | |
| 2018 return SQLITE_OK; | |
| 2019 } | |
| 2020 | |
| 2021 /* The methods of the json_each virtual table */ | |
| 2022 static sqlite3_module jsonEachModule = { | |
| 2023 0, /* iVersion */ | |
| 2024 0, /* xCreate */ | |
| 2025 jsonEachConnect, /* xConnect */ | |
| 2026 jsonEachBestIndex, /* xBestIndex */ | |
| 2027 jsonEachDisconnect, /* xDisconnect */ | |
| 2028 0, /* xDestroy */ | |
| 2029 jsonEachOpenEach, /* xOpen - open a cursor */ | |
| 2030 jsonEachClose, /* xClose - close a cursor */ | |
| 2031 jsonEachFilter, /* xFilter - configure scan constraints */ | |
| 2032 jsonEachNext, /* xNext - advance a cursor */ | |
| 2033 jsonEachEof, /* xEof - check for end of scan */ | |
| 2034 jsonEachColumn, /* xColumn - read data */ | |
| 2035 jsonEachRowid, /* xRowid - read data */ | |
| 2036 0, /* xUpdate */ | |
| 2037 0, /* xBegin */ | |
| 2038 0, /* xSync */ | |
| 2039 0, /* xCommit */ | |
| 2040 0, /* xRollback */ | |
| 2041 0, /* xFindMethod */ | |
| 2042 0, /* xRename */ | |
| 2043 0, /* xSavepoint */ | |
| 2044 0, /* xRelease */ | |
| 2045 0 /* xRollbackTo */ | |
| 2046 }; | |
| 2047 | |
| 2048 /* The methods of the json_tree virtual table. */ | |
| 2049 static sqlite3_module jsonTreeModule = { | |
| 2050 0, /* iVersion */ | |
| 2051 0, /* xCreate */ | |
| 2052 jsonEachConnect, /* xConnect */ | |
| 2053 jsonEachBestIndex, /* xBestIndex */ | |
| 2054 jsonEachDisconnect, /* xDisconnect */ | |
| 2055 0, /* xDestroy */ | |
| 2056 jsonEachOpenTree, /* xOpen - open a cursor */ | |
| 2057 jsonEachClose, /* xClose - close a cursor */ | |
| 2058 jsonEachFilter, /* xFilter - configure scan constraints */ | |
| 2059 jsonEachNext, /* xNext - advance a cursor */ | |
| 2060 jsonEachEof, /* xEof - check for end of scan */ | |
| 2061 jsonEachColumn, /* xColumn - read data */ | |
| 2062 jsonEachRowid, /* xRowid - read data */ | |
| 2063 0, /* xUpdate */ | |
| 2064 0, /* xBegin */ | |
| 2065 0, /* xSync */ | |
| 2066 0, /* xCommit */ | |
| 2067 0, /* xRollback */ | |
| 2068 0, /* xFindMethod */ | |
| 2069 0, /* xRename */ | |
| 2070 0, /* xSavepoint */ | |
| 2071 0, /* xRelease */ | |
| 2072 0 /* xRollbackTo */ | |
| 2073 }; | |
| 2074 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
| 2075 | |
| 2076 /**************************************************************************** | |
| 2077 ** The following routines are the only publically visible identifiers in this | |
| 2078 ** file. Call the following routines in order to register the various SQL | |
| 2079 ** functions and the virtual table implemented by this file. | |
| 2080 ****************************************************************************/ | |
| 2081 | |
| 2082 int sqlite3Json1Init(sqlite3 *db){ | |
| 2083 int rc = SQLITE_OK; | |
| 2084 unsigned int i; | |
| 2085 static const struct { | |
| 2086 const char *zName; | |
| 2087 int nArg; | |
| 2088 int flag; | |
| 2089 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); | |
| 2090 } aFunc[] = { | |
| 2091 { "json", 1, 0, jsonRemoveFunc }, | |
| 2092 { "json_array", -1, 0, jsonArrayFunc }, | |
| 2093 { "json_array_length", 1, 0, jsonArrayLengthFunc }, | |
| 2094 { "json_array_length", 2, 0, jsonArrayLengthFunc }, | |
| 2095 { "json_extract", -1, 0, jsonExtractFunc }, | |
| 2096 { "json_insert", -1, 0, jsonSetFunc }, | |
| 2097 { "json_object", -1, 0, jsonObjectFunc }, | |
| 2098 { "json_remove", -1, 0, jsonRemoveFunc }, | |
| 2099 { "json_replace", -1, 0, jsonReplaceFunc }, | |
| 2100 { "json_set", -1, 1, jsonSetFunc }, | |
| 2101 { "json_type", 1, 0, jsonTypeFunc }, | |
| 2102 { "json_type", 2, 0, jsonTypeFunc }, | |
| 2103 { "json_valid", 1, 0, jsonValidFunc }, | |
| 2104 | |
| 2105 #if SQLITE_DEBUG | |
| 2106 /* DEBUG and TESTING functions */ | |
| 2107 { "json_parse", 1, 0, jsonParseFunc }, | |
| 2108 { "json_test1", 1, 0, jsonTest1Func }, | |
| 2109 #endif | |
| 2110 }; | |
| 2111 static const struct { | |
| 2112 const char *zName; | |
| 2113 int nArg; | |
| 2114 void (*xStep)(sqlite3_context*,int,sqlite3_value**); | |
| 2115 void (*xFinal)(sqlite3_context*); | |
| 2116 } aAgg[] = { | |
| 2117 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal }, | |
| 2118 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal }, | |
| 2119 }; | |
| 2120 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 2121 static const struct { | |
| 2122 const char *zName; | |
| 2123 sqlite3_module *pModule; | |
| 2124 } aMod[] = { | |
| 2125 { "json_each", &jsonEachModule }, | |
| 2126 { "json_tree", &jsonTreeModule }, | |
| 2127 }; | |
| 2128 #endif | |
| 2129 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ | |
| 2130 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, | |
| 2131 SQLITE_UTF8 | SQLITE_DETERMINISTIC, | |
| 2132 (void*)&aFunc[i].flag, | |
| 2133 aFunc[i].xFunc, 0, 0); | |
| 2134 } | |
| 2135 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){ | |
| 2136 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg, | |
| 2137 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0, | |
| 2138 0, aAgg[i].xStep, aAgg[i].xFinal); | |
| 2139 } | |
| 2140 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 2141 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){ | |
| 2142 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0); | |
| 2143 } | |
| 2144 #endif | |
| 2145 return rc; | |
| 2146 } | |
| 2147 | |
| 2148 | |
| 2149 #ifndef SQLITE_CORE | |
| 2150 #ifdef _WIN32 | |
| 2151 __declspec(dllexport) | |
| 2152 #endif | |
| 2153 int sqlite3_json_init( | |
| 2154 sqlite3 *db, | |
| 2155 char **pzErrMsg, | |
| 2156 const sqlite3_api_routines *pApi | |
| 2157 ){ | |
| 2158 SQLITE_EXTENSION_INIT2(pApi); | |
| 2159 (void)pzErrMsg; /* Unused parameter */ | |
| 2160 return sqlite3Json1Init(db); | |
| 2161 } | |
| 2162 #endif | |
| 2163 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */ | |
| OLD | NEW |