| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2015-08-12 | 2 ** 2015-08-12 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ****************************************************************************** | 11 ****************************************************************************** |
| 12 ** | 12 ** |
| 13 ** This SQLite extension implements JSON functions. The interface is | 13 ** This SQLite extension implements JSON functions. The interface is |
| 14 ** modeled after MySQL JSON functions: | 14 ** modeled after MySQL JSON functions: |
| 15 ** | 15 ** |
| 16 ** https://dev.mysql.com/doc/refman/5.7/en/json.html | 16 ** https://dev.mysql.com/doc/refman/5.7/en/json.html |
| 17 ** | 17 ** |
| 18 ** For the time being, all JSON is stored as pure text. (We might add | 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 | 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. | 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 | 21 ** This implementation parses JSON text at 250 MB/s, so it is hard to see |
| 22 ** how JSONB might improve on that.) | 22 ** how JSONB might improve on that.) |
| 23 */ | 23 */ |
| 24 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) | 24 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) |
| 25 #if !defined(_SQLITEINT_H_) | 25 #if !defined(SQLITEINT_H) |
| 26 #include "sqlite3ext.h" | 26 #include "sqlite3ext.h" |
| 27 #endif | 27 #endif |
| 28 SQLITE_EXTENSION_INIT1 | 28 SQLITE_EXTENSION_INIT1 |
| 29 #include <assert.h> | 29 #include <assert.h> |
| 30 #include <string.h> | 30 #include <string.h> |
| 31 #include <stdlib.h> | 31 #include <stdlib.h> |
| 32 #include <stdarg.h> | 32 #include <stdarg.h> |
| 33 | 33 |
| 34 #define UNUSED_PARAM(X) (void)(X) | 34 /* Mark a function parameter as unused, to suppress nuisance compiler |
| 35 ** warnings. */ |
| 36 #ifndef UNUSED_PARAM |
| 37 # define UNUSED_PARAM(X) (void)(X) |
| 38 #endif |
| 35 | 39 |
| 36 #ifndef LARGEST_INT64 | 40 #ifndef LARGEST_INT64 |
| 37 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) | 41 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) |
| 38 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) | 42 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) |
| 39 #endif | 43 #endif |
| 40 | 44 |
| 41 /* | 45 /* |
| 42 ** Versions of isspace(), isalnum() and isdigit() to which it is safe | 46 ** Versions of isspace(), isalnum() and isdigit() to which it is safe |
| 43 ** to pass signed char values. | 47 ** to pass signed char values. |
| 44 */ | 48 */ |
| 45 #ifdef sqlite3Isdigit | 49 #ifdef sqlite3Isdigit |
| 46 /* Use the SQLite core versions if this routine is part of the | 50 /* Use the SQLite core versions if this routine is part of the |
| 47 ** SQLite amalgamation */ | 51 ** SQLite amalgamation */ |
| 48 # define safe_isdigit(x) sqlite3Isdigit(x) | 52 # define safe_isdigit(x) sqlite3Isdigit(x) |
| 49 # define safe_isalnum(x) sqlite3Isalnum(x) | 53 # define safe_isalnum(x) sqlite3Isalnum(x) |
| 54 # define safe_isxdigit(x) sqlite3Isxdigit(x) |
| 50 #else | 55 #else |
| 51 /* Use the standard library for separate compilation */ | 56 /* Use the standard library for separate compilation */ |
| 52 #include <ctype.h> /* amalgamator: keep */ | 57 #include <ctype.h> /* amalgamator: keep */ |
| 53 # define safe_isdigit(x) isdigit((unsigned char)(x)) | 58 # define safe_isdigit(x) isdigit((unsigned char)(x)) |
| 54 # define safe_isalnum(x) isalnum((unsigned char)(x)) | 59 # define safe_isalnum(x) isalnum((unsigned char)(x)) |
| 60 # define safe_isxdigit(x) isxdigit((unsigned char)(x)) |
| 55 #endif | 61 #endif |
| 56 | 62 |
| 57 /* | 63 /* |
| 58 ** Growing our own isspace() routine this way is twice as fast as | 64 ** Growing our own isspace() routine this way is twice as fast as |
| 59 ** the library isspace() function, resulting in a 7% overall performance | 65 ** the library isspace() function, resulting in a 7% overall performance |
| 60 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). | 66 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). |
| 61 */ | 67 */ |
| 62 static const char jsonIsSpace[] = { | 68 static const char jsonIsSpace[] = { |
| 63 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, | 69 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, | 70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 /* Append the N-byte string in zIn to the end of the JsonString string | 275 /* Append the N-byte string in zIn to the end of the JsonString string |
| 270 ** under construction. Enclose the string in "..." and escape | 276 ** under construction. Enclose the string in "..." and escape |
| 271 ** any double-quotes or backslash characters contained within the | 277 ** any double-quotes or backslash characters contained within the |
| 272 ** string. | 278 ** string. |
| 273 */ | 279 */ |
| 274 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ | 280 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ |
| 275 u32 i; | 281 u32 i; |
| 276 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return; | 282 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return; |
| 277 p->zBuf[p->nUsed++] = '"'; | 283 p->zBuf[p->nUsed++] = '"'; |
| 278 for(i=0; i<N; i++){ | 284 for(i=0; i<N; i++){ |
| 279 char c = zIn[i]; | 285 unsigned char c = ((unsigned const char*)zIn)[i]; |
| 280 if( c=='"' || c=='\\' ){ | 286 if( c=='"' || c=='\\' ){ |
| 287 json_simple_escape: |
| 281 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return; | 288 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return; |
| 282 p->zBuf[p->nUsed++] = '\\'; | 289 p->zBuf[p->nUsed++] = '\\'; |
| 290 }else if( c<=0x1f ){ |
| 291 static const char aSpecial[] = { |
| 292 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, |
| 293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 294 }; |
| 295 assert( sizeof(aSpecial)==32 ); |
| 296 assert( aSpecial['\b']=='b' ); |
| 297 assert( aSpecial['\f']=='f' ); |
| 298 assert( aSpecial['\n']=='n' ); |
| 299 assert( aSpecial['\r']=='r' ); |
| 300 assert( aSpecial['\t']=='t' ); |
| 301 if( aSpecial[c] ){ |
| 302 c = aSpecial[c]; |
| 303 goto json_simple_escape; |
| 304 } |
| 305 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return; |
| 306 p->zBuf[p->nUsed++] = '\\'; |
| 307 p->zBuf[p->nUsed++] = 'u'; |
| 308 p->zBuf[p->nUsed++] = '0'; |
| 309 p->zBuf[p->nUsed++] = '0'; |
| 310 p->zBuf[p->nUsed++] = '0' + (c>>4); |
| 311 c = "0123456789abcdef"[c&0xf]; |
| 283 } | 312 } |
| 284 p->zBuf[p->nUsed++] = c; | 313 p->zBuf[p->nUsed++] = c; |
| 285 } | 314 } |
| 286 p->zBuf[p->nUsed++] = '"'; | 315 p->zBuf[p->nUsed++] = '"'; |
| 287 assert( p->nUsed<p->nAlloc ); | 316 assert( p->nUsed<p->nAlloc ); |
| 288 } | 317 } |
| 289 | 318 |
| 290 /* | 319 /* |
| 291 ** Append a function parameter value to the JSON string under | 320 ** Append a function parameter value to the JSON string under |
| 292 ** construction. | 321 ** construction. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 313 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ | 342 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ |
| 314 jsonAppendRaw(p, z, n); | 343 jsonAppendRaw(p, z, n); |
| 315 }else{ | 344 }else{ |
| 316 jsonAppendString(p, z, n); | 345 jsonAppendString(p, z, n); |
| 317 } | 346 } |
| 318 break; | 347 break; |
| 319 } | 348 } |
| 320 default: { | 349 default: { |
| 321 if( p->bErr==0 ){ | 350 if( p->bErr==0 ){ |
| 322 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); | 351 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); |
| 323 p->bErr = 1; | 352 p->bErr = 2; |
| 324 jsonReset(p); | 353 jsonReset(p); |
| 325 } | 354 } |
| 326 break; | 355 break; |
| 327 } | 356 } |
| 328 } | 357 } |
| 329 } | 358 } |
| 330 | 359 |
| 331 | 360 |
| 332 /* Make the JSON in p the result of the SQL function. | 361 /* Make the JSON in p the result of the SQL function. |
| 333 */ | 362 */ |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 break; | 588 break; |
| 560 } | 589 } |
| 561 for(i=1, j=0; i<n-1; i++){ | 590 for(i=1, j=0; i<n-1; i++){ |
| 562 char c = z[i]; | 591 char c = z[i]; |
| 563 if( c!='\\' ){ | 592 if( c!='\\' ){ |
| 564 zOut[j++] = c; | 593 zOut[j++] = c; |
| 565 }else{ | 594 }else{ |
| 566 c = z[++i]; | 595 c = z[++i]; |
| 567 if( c=='u' ){ | 596 if( c=='u' ){ |
| 568 u32 v = 0, k; | 597 u32 v = 0, k; |
| 569 for(k=0; k<4 && i<n-2; i++, k++){ | 598 for(k=0; k<4; i++, k++){ |
| 599 assert( i<n-2 ); |
| 570 c = z[i+1]; | 600 c = z[i+1]; |
| 571 if( c>='0' && c<='9' ) v = v*16 + c - '0'; | 601 assert( safe_isxdigit(c) ); |
| 572 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10; | 602 if( c<='9' ) v = v*16 + c - '0'; |
| 573 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10; | 603 else if( c<='F' ) v = v*16 + c - 'A' + 10; |
| 574 else break; | 604 else v = v*16 + c - 'a' + 10; |
| 575 } | 605 } |
| 576 if( v==0 ) break; | 606 if( v==0 ) break; |
| 577 if( v<=0x7f ){ | 607 if( v<=0x7f ){ |
| 578 zOut[j++] = (char)v; | 608 zOut[j++] = (char)v; |
| 579 }else if( v<=0x7ff ){ | 609 }else if( v<=0x7ff ){ |
| 580 zOut[j++] = (char)(0xc0 | (v>>6)); | 610 zOut[j++] = (char)(0xc0 | (v>>6)); |
| 581 zOut[j++] = 0x80 | (v&0x3f); | 611 zOut[j++] = 0x80 | (v&0x3f); |
| 582 }else{ | 612 }else{ |
| 583 zOut[j++] = (char)(0xe0 | (v>>12)); | 613 zOut[j++] = (char)(0xe0 | (v>>12)); |
| 584 zOut[j++] = 0x80 | ((v>>6)&0x3f); | 614 zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 p = &pParse->aNode[pParse->nNode]; | 699 p = &pParse->aNode[pParse->nNode]; |
| 670 p->eType = (u8)eType; | 700 p->eType = (u8)eType; |
| 671 p->jnFlags = 0; | 701 p->jnFlags = 0; |
| 672 p->iVal = 0; | 702 p->iVal = 0; |
| 673 p->n = n; | 703 p->n = n; |
| 674 p->u.zJContent = zContent; | 704 p->u.zJContent = zContent; |
| 675 return pParse->nNode++; | 705 return pParse->nNode++; |
| 676 } | 706 } |
| 677 | 707 |
| 678 /* | 708 /* |
| 709 ** Return true if z[] begins with 4 (or more) hexadecimal digits |
| 710 */ |
| 711 static int jsonIs4Hex(const char *z){ |
| 712 int i; |
| 713 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0; |
| 714 return 1; |
| 715 } |
| 716 |
| 717 /* |
| 679 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the | 718 ** 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. | 719 ** index of the first character past the end of the value parsed. |
| 681 ** | 720 ** |
| 682 ** Return negative for a syntax error. Special cases: return -2 if the | 721 ** Return negative for a syntax error. Special cases: return -2 if the |
| 683 ** first non-whitespace character is '}' and return -3 if the first | 722 ** first non-whitespace character is '}' and return -3 if the first |
| 684 ** non-whitespace character is ']'. | 723 ** non-whitespace character is ']'. |
| 685 */ | 724 */ |
| 686 static int jsonParseValue(JsonParse *pParse, u32 i){ | 725 static int jsonParseValue(JsonParse *pParse, u32 i){ |
| 687 char c; | 726 char c; |
| 688 u32 j; | 727 u32 j; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 return j+1; | 781 return j+1; |
| 743 }else if( c=='"' ){ | 782 }else if( c=='"' ){ |
| 744 /* Parse string */ | 783 /* Parse string */ |
| 745 u8 jnFlags = 0; | 784 u8 jnFlags = 0; |
| 746 j = i+1; | 785 j = i+1; |
| 747 for(;;){ | 786 for(;;){ |
| 748 c = pParse->zJson[j]; | 787 c = pParse->zJson[j]; |
| 749 if( c==0 ) return -1; | 788 if( c==0 ) return -1; |
| 750 if( c=='\\' ){ | 789 if( c=='\\' ){ |
| 751 c = pParse->zJson[++j]; | 790 c = pParse->zJson[++j]; |
| 752 if( c==0 ) return -1; | 791 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f' |
| 753 jnFlags = JNODE_ESCAPE; | 792 || c=='n' || c=='r' || c=='t' |
| 793 || (c=='u' && jsonIs4Hex(pParse->zJson+j+1)) ){ |
| 794 jnFlags = JNODE_ESCAPE; |
| 795 }else{ |
| 796 return -1; |
| 797 } |
| 754 }else if( c=='"' ){ | 798 }else if( c=='"' ){ |
| 755 break; | 799 break; |
| 756 } | 800 } |
| 757 j++; | 801 j++; |
| 758 } | 802 } |
| 759 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); | 803 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); |
| 760 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; | 804 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; |
| 761 return j+1; | 805 return j+1; |
| 762 }else if( c=='n' | 806 }else if( c=='n' |
| 763 && strncmp(pParse->zJson+i,"null",4)==0 | 807 && strncmp(pParse->zJson+i,"null",4)==0 |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1178 UNUSED_PARAM(argc); | 1222 UNUSED_PARAM(argc); |
| 1179 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE); | 1223 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE); |
| 1180 } | 1224 } |
| 1181 #endif /* SQLITE_DEBUG */ | 1225 #endif /* SQLITE_DEBUG */ |
| 1182 | 1226 |
| 1183 /**************************************************************************** | 1227 /**************************************************************************** |
| 1184 ** Scalar SQL function implementations | 1228 ** Scalar SQL function implementations |
| 1185 ****************************************************************************/ | 1229 ****************************************************************************/ |
| 1186 | 1230 |
| 1187 /* | 1231 /* |
| 1232 ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value |
| 1233 ** corresponding to the SQL value input. Mostly this means putting |
| 1234 ** double-quotes around strings and returning the unquoted string "null" |
| 1235 ** when given a NULL input. |
| 1236 */ |
| 1237 static void jsonQuoteFunc( |
| 1238 sqlite3_context *ctx, |
| 1239 int argc, |
| 1240 sqlite3_value **argv |
| 1241 ){ |
| 1242 JsonString jx; |
| 1243 UNUSED_PARAM(argc); |
| 1244 |
| 1245 jsonInit(&jx, ctx); |
| 1246 jsonAppendValue(&jx, argv[0]); |
| 1247 jsonResult(&jx); |
| 1248 sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 1249 } |
| 1250 |
| 1251 /* |
| 1188 ** Implementation of the json_array(VALUE,...) function. Return a JSON | 1252 ** Implementation of the json_array(VALUE,...) function. Return a JSON |
| 1189 ** array that contains all values given in arguments. Or if any argument | 1253 ** array that contains all values given in arguments. Or if any argument |
| 1190 ** is a BLOB, throw an error. | 1254 ** is a BLOB, throw an error. |
| 1191 */ | 1255 */ |
| 1192 static void jsonArrayFunc( | 1256 static void jsonArrayFunc( |
| 1193 sqlite3_context *ctx, | 1257 sqlite3_context *ctx, |
| 1194 int argc, | 1258 int argc, |
| 1195 sqlite3_value **argv | 1259 sqlite3_value **argv |
| 1196 ){ | 1260 ){ |
| 1197 int i; | 1261 int i; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1522 ** json_group_array(VALUE) | 1586 ** json_group_array(VALUE) |
| 1523 ** | 1587 ** |
| 1524 ** Return a JSON array composed of all values in the aggregate. | 1588 ** Return a JSON array composed of all values in the aggregate. |
| 1525 */ | 1589 */ |
| 1526 static void jsonArrayStep( | 1590 static void jsonArrayStep( |
| 1527 sqlite3_context *ctx, | 1591 sqlite3_context *ctx, |
| 1528 int argc, | 1592 int argc, |
| 1529 sqlite3_value **argv | 1593 sqlite3_value **argv |
| 1530 ){ | 1594 ){ |
| 1531 JsonString *pStr; | 1595 JsonString *pStr; |
| 1596 UNUSED_PARAM(argc); |
| 1532 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); | 1597 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); |
| 1533 if( pStr ){ | 1598 if( pStr ){ |
| 1534 if( pStr->zBuf==0 ){ | 1599 if( pStr->zBuf==0 ){ |
| 1535 jsonInit(pStr, ctx); | 1600 jsonInit(pStr, ctx); |
| 1536 jsonAppendChar(pStr, '['); | 1601 jsonAppendChar(pStr, '['); |
| 1537 }else{ | 1602 }else{ |
| 1538 jsonAppendChar(pStr, ','); | 1603 jsonAppendChar(pStr, ','); |
| 1539 pStr->pCtx = ctx; | 1604 pStr->pCtx = ctx; |
| 1540 } | 1605 } |
| 1541 jsonAppendValue(pStr, argv[0]); | 1606 jsonAppendValue(pStr, argv[0]); |
| 1542 } | 1607 } |
| 1543 } | 1608 } |
| 1544 static void jsonArrayFinal(sqlite3_context *ctx){ | 1609 static void jsonArrayFinal(sqlite3_context *ctx){ |
| 1545 JsonString *pStr; | 1610 JsonString *pStr; |
| 1546 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); | 1611 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 1547 if( pStr ){ | 1612 if( pStr ){ |
| 1548 pStr->pCtx = ctx; | 1613 pStr->pCtx = ctx; |
| 1549 jsonAppendChar(pStr, ']'); | 1614 jsonAppendChar(pStr, ']'); |
| 1550 if( pStr->bErr ){ | 1615 if( pStr->bErr ){ |
| 1551 sqlite3_result_error_nomem(ctx); | 1616 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx); |
| 1552 assert( pStr->bStatic ); | 1617 assert( pStr->bStatic ); |
| 1553 }else{ | 1618 }else{ |
| 1554 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, | 1619 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, |
| 1555 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); | 1620 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); |
| 1556 pStr->bStatic = 1; | 1621 pStr->bStatic = 1; |
| 1557 } | 1622 } |
| 1558 }else{ | 1623 }else{ |
| 1559 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); | 1624 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); |
| 1560 } | 1625 } |
| 1561 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | 1626 sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 1562 } | 1627 } |
| 1563 | 1628 |
| 1564 /* | 1629 /* |
| 1565 ** json_group_obj(NAME,VALUE) | 1630 ** json_group_obj(NAME,VALUE) |
| 1566 ** | 1631 ** |
| 1567 ** Return a JSON object composed of all names and values in the aggregate. | 1632 ** Return a JSON object composed of all names and values in the aggregate. |
| 1568 */ | 1633 */ |
| 1569 static void jsonObjectStep( | 1634 static void jsonObjectStep( |
| 1570 sqlite3_context *ctx, | 1635 sqlite3_context *ctx, |
| 1571 int argc, | 1636 int argc, |
| 1572 sqlite3_value **argv | 1637 sqlite3_value **argv |
| 1573 ){ | 1638 ){ |
| 1574 JsonString *pStr; | 1639 JsonString *pStr; |
| 1575 const char *z; | 1640 const char *z; |
| 1576 u32 n; | 1641 u32 n; |
| 1642 UNUSED_PARAM(argc); |
| 1577 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); | 1643 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); |
| 1578 if( pStr ){ | 1644 if( pStr ){ |
| 1579 if( pStr->zBuf==0 ){ | 1645 if( pStr->zBuf==0 ){ |
| 1580 jsonInit(pStr, ctx); | 1646 jsonInit(pStr, ctx); |
| 1581 jsonAppendChar(pStr, '{'); | 1647 jsonAppendChar(pStr, '{'); |
| 1582 }else{ | 1648 }else{ |
| 1583 jsonAppendChar(pStr, ','); | 1649 jsonAppendChar(pStr, ','); |
| 1584 pStr->pCtx = ctx; | 1650 pStr->pCtx = ctx; |
| 1585 } | 1651 } |
| 1586 z = (const char*)sqlite3_value_text(argv[0]); | 1652 z = (const char*)sqlite3_value_text(argv[0]); |
| 1587 n = (u32)sqlite3_value_bytes(argv[0]); | 1653 n = (u32)sqlite3_value_bytes(argv[0]); |
| 1588 jsonAppendString(pStr, z, n); | 1654 jsonAppendString(pStr, z, n); |
| 1589 jsonAppendChar(pStr, ':'); | 1655 jsonAppendChar(pStr, ':'); |
| 1590 jsonAppendValue(pStr, argv[1]); | 1656 jsonAppendValue(pStr, argv[1]); |
| 1591 } | 1657 } |
| 1592 } | 1658 } |
| 1593 static void jsonObjectFinal(sqlite3_context *ctx){ | 1659 static void jsonObjectFinal(sqlite3_context *ctx){ |
| 1594 JsonString *pStr; | 1660 JsonString *pStr; |
| 1595 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); | 1661 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 1596 if( pStr ){ | 1662 if( pStr ){ |
| 1597 jsonAppendChar(pStr, '}'); | 1663 jsonAppendChar(pStr, '}'); |
| 1598 if( pStr->bErr ){ | 1664 if( pStr->bErr ){ |
| 1599 sqlite3_result_error_nomem(ctx); | 1665 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx); |
| 1600 assert( pStr->bStatic ); | 1666 assert( pStr->bStatic ); |
| 1601 }else{ | 1667 }else{ |
| 1602 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, | 1668 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, |
| 1603 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); | 1669 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); |
| 1604 pStr->bStatic = 1; | 1670 pStr->bStatic = 1; |
| 1605 } | 1671 } |
| 1606 }else{ | 1672 }else{ |
| 1607 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); | 1673 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); |
| 1608 } | 1674 } |
| 1609 sqlite3_result_subtype(ctx, JSON_SUBTYPE); | 1675 sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1867 if( p->bRecursive ){ | 1933 if( p->bRecursive ){ |
| 1868 JsonString x; | 1934 JsonString x; |
| 1869 jsonInit(&x, ctx); | 1935 jsonInit(&x, ctx); |
| 1870 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]); | 1936 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]); |
| 1871 jsonResult(&x); | 1937 jsonResult(&x); |
| 1872 break; | 1938 break; |
| 1873 } | 1939 } |
| 1874 /* For json_each() path and root are the same so fall through | 1940 /* For json_each() path and root are the same so fall through |
| 1875 ** into the root case */ | 1941 ** into the root case */ |
| 1876 } | 1942 } |
| 1877 case JEACH_ROOT: { | 1943 default: { |
| 1878 const char *zRoot = p->zRoot; | 1944 const char *zRoot = p->zRoot; |
| 1879 if( zRoot==0 ) zRoot = "$"; | 1945 if( zRoot==0 ) zRoot = "$"; |
| 1880 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC); | 1946 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC); |
| 1881 break; | 1947 break; |
| 1882 } | 1948 } |
| 1883 case JEACH_JSON: { | 1949 case JEACH_JSON: { |
| 1884 assert( i==JEACH_JSON ); | 1950 assert( i==JEACH_JSON ); |
| 1885 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC); | 1951 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC); |
| 1886 break; | 1952 break; |
| 1887 } | 1953 } |
| 1888 } | 1954 } |
| 1889 return SQLITE_OK; | 1955 return SQLITE_OK; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2088 int flag; | 2154 int flag; |
| 2089 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); | 2155 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 2090 } aFunc[] = { | 2156 } aFunc[] = { |
| 2091 { "json", 1, 0, jsonRemoveFunc }, | 2157 { "json", 1, 0, jsonRemoveFunc }, |
| 2092 { "json_array", -1, 0, jsonArrayFunc }, | 2158 { "json_array", -1, 0, jsonArrayFunc }, |
| 2093 { "json_array_length", 1, 0, jsonArrayLengthFunc }, | 2159 { "json_array_length", 1, 0, jsonArrayLengthFunc }, |
| 2094 { "json_array_length", 2, 0, jsonArrayLengthFunc }, | 2160 { "json_array_length", 2, 0, jsonArrayLengthFunc }, |
| 2095 { "json_extract", -1, 0, jsonExtractFunc }, | 2161 { "json_extract", -1, 0, jsonExtractFunc }, |
| 2096 { "json_insert", -1, 0, jsonSetFunc }, | 2162 { "json_insert", -1, 0, jsonSetFunc }, |
| 2097 { "json_object", -1, 0, jsonObjectFunc }, | 2163 { "json_object", -1, 0, jsonObjectFunc }, |
| 2164 { "json_quote", 1, 0, jsonQuoteFunc }, |
| 2098 { "json_remove", -1, 0, jsonRemoveFunc }, | 2165 { "json_remove", -1, 0, jsonRemoveFunc }, |
| 2099 { "json_replace", -1, 0, jsonReplaceFunc }, | 2166 { "json_replace", -1, 0, jsonReplaceFunc }, |
| 2100 { "json_set", -1, 1, jsonSetFunc }, | 2167 { "json_set", -1, 1, jsonSetFunc }, |
| 2101 { "json_type", 1, 0, jsonTypeFunc }, | 2168 { "json_type", 1, 0, jsonTypeFunc }, |
| 2102 { "json_type", 2, 0, jsonTypeFunc }, | 2169 { "json_type", 2, 0, jsonTypeFunc }, |
| 2103 { "json_valid", 1, 0, jsonValidFunc }, | 2170 { "json_valid", 1, 0, jsonValidFunc }, |
| 2104 | 2171 |
| 2105 #if SQLITE_DEBUG | 2172 #if SQLITE_DEBUG |
| 2106 /* DEBUG and TESTING functions */ | 2173 /* DEBUG and TESTING functions */ |
| 2107 { "json_parse", 1, 0, jsonParseFunc }, | 2174 { "json_parse", 1, 0, jsonParseFunc }, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2154 sqlite3 *db, | 2221 sqlite3 *db, |
| 2155 char **pzErrMsg, | 2222 char **pzErrMsg, |
| 2156 const sqlite3_api_routines *pApi | 2223 const sqlite3_api_routines *pApi |
| 2157 ){ | 2224 ){ |
| 2158 SQLITE_EXTENSION_INIT2(pApi); | 2225 SQLITE_EXTENSION_INIT2(pApi); |
| 2159 (void)pzErrMsg; /* Unused parameter */ | 2226 (void)pzErrMsg; /* Unused parameter */ |
| 2160 return sqlite3Json1Init(db); | 2227 return sqlite3Json1Init(db); |
| 2161 } | 2228 } |
| 2162 #endif | 2229 #endif |
| 2163 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */ | 2230 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */ |
| OLD | NEW |