| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.44.2. By combining all the individual C code files into this |
| 3 | +** version 3.45.1. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -16,11 +16,11 @@ |
| 16 | 16 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 17 | 17 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 18 | 18 | ** separate file. This file contains only code for the core SQLite library. |
| 19 | 19 | ** |
| 20 | 20 | ** The content in this amalgamation comes from Fossil check-in |
| 21 | | -** ebead0e7230cd33bcec9f95d2183069565b9. |
| 21 | +** e876e51a0ed5c5b3126f52e532044363a014. |
| 22 | 22 | */ |
| 23 | 23 | #define SQLITE_CORE 1 |
| 24 | 24 | #define SQLITE_AMALGAMATION 1 |
| 25 | 25 | #ifndef SQLITE_PRIVATE |
| 26 | 26 | # define SQLITE_PRIVATE static |
| | @@ -457,13 +457,13 @@ |
| 457 | 457 | ** |
| 458 | 458 | ** See also: [sqlite3_libversion()], |
| 459 | 459 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 460 | 460 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 461 | 461 | */ |
| 462 | | -#define SQLITE_VERSION "3.44.2" |
| 463 | | -#define SQLITE_VERSION_NUMBER 3044002 |
| 464 | | -#define SQLITE_SOURCE_ID "2023-11-24 11:41:44 ebead0e7230cd33bcec9f95d2183069565b9e709bf745c9b5db65cc0cbf92c0f" |
| 462 | +#define SQLITE_VERSION "3.45.1" |
| 463 | +#define SQLITE_VERSION_NUMBER 3045001 |
| 464 | +#define SQLITE_SOURCE_ID "2024-01-30 16:01:20 e876e51a0ed5c5b3126f52e532044363a014bc594cfefa87ffb5b82257cc467a" |
| 465 | 465 | |
| 466 | 466 | /* |
| 467 | 467 | ** CAPI3REF: Run-Time Library Version Numbers |
| 468 | 468 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 469 | 469 | ** |
| | @@ -4265,19 +4265,21 @@ |
| 4265 | 4265 | ** <li> sqlite3_errmsg16() |
| 4266 | 4266 | ** <li> sqlite3_error_offset() |
| 4267 | 4267 | ** </ul> |
| 4268 | 4268 | ** |
| 4269 | 4269 | ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language |
| 4270 | | -** text that describes the error, as either UTF-8 or UTF-16 respectively. |
| 4270 | +** text that describes the error, as either UTF-8 or UTF-16 respectively, |
| 4271 | +** or NULL if no error message is available. |
| 4271 | 4272 | ** (See how SQLite handles [invalid UTF] for exceptions to this rule.) |
| 4272 | 4273 | ** ^(Memory to hold the error message string is managed internally. |
| 4273 | 4274 | ** The application does not need to worry about freeing the result. |
| 4274 | 4275 | ** However, the error string might be overwritten or deallocated by |
| 4275 | 4276 | ** subsequent calls to other SQLite interface functions.)^ |
| 4276 | 4277 | ** |
| 4277 | | -** ^The sqlite3_errstr() interface returns the English-language text |
| 4278 | | -** that describes the [result code], as UTF-8. |
| 4278 | +** ^The sqlite3_errstr(E) interface returns the English-language text |
| 4279 | +** that describes the [result code] E, as UTF-8, or NULL if E is not an |
| 4280 | +** result code for which a text error message is available. |
| 4279 | 4281 | ** ^(Memory to hold the error message string is managed internally |
| 4280 | 4282 | ** and must not be freed by the application)^. |
| 4281 | 4283 | ** |
| 4282 | 4284 | ** ^If the most recent error references a specific token in the input |
| 4283 | 4285 | ** SQL, the sqlite3_error_offset() interface returns the byte offset |
| | @@ -8348,13 +8350,15 @@ |
| 8348 | 8350 | ** can enter.)^ If the same thread tries to enter any mutex other |
| 8349 | 8351 | ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. |
| 8350 | 8352 | ** |
| 8351 | 8353 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 8352 | 8354 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 8353 | | -** will always return SQLITE_BUSY. The SQLite core only ever uses |
| 8354 | | -** sqlite3_mutex_try() as an optimization so this is acceptable |
| 8355 | | -** behavior.)^ |
| 8355 | +** will always return SQLITE_BUSY. In most cases the SQLite core only uses |
| 8356 | +** sqlite3_mutex_try() as an optimization, so this is acceptable |
| 8357 | +** behavior. The exceptions are unix builds that set the |
| 8358 | +** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working |
| 8359 | +** sqlite3_mutex_try() is required.)^ |
| 8356 | 8360 | ** |
| 8357 | 8361 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 8358 | 8362 | ** previously entered by the same thread. The behavior |
| 8359 | 8363 | ** is undefined if the mutex is not currently entered by the |
| 8360 | 8364 | ** calling thread or is not currently allocated. |
| | @@ -8609,10 +8613,11 @@ |
| 8609 | 8613 | #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10 |
| 8610 | 8614 | #define SQLITE_TESTCTRL_PENDING_BYTE 11 |
| 8611 | 8615 | #define SQLITE_TESTCTRL_ASSERT 12 |
| 8612 | 8616 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 8613 | 8617 | #define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */ |
| 8618 | +#define SQLITE_TESTCTRL_JSON_SELFCHECK 14 |
| 8614 | 8619 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 8615 | 8620 | #define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */ |
| 8616 | 8621 | #define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */ |
| 8617 | 8622 | #define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17 |
| 8618 | 8623 | #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 |
| | @@ -13122,23 +13127,28 @@ |
| 13122 | 13127 | ** |
| 13123 | 13128 | ** This function may be quite inefficient if used with an FTS5 table |
| 13124 | 13129 | ** created with the "columnsize=0" option. |
| 13125 | 13130 | ** |
| 13126 | 13131 | ** xColumnText: |
| 13127 | | -** This function attempts to retrieve the text of column iCol of the |
| 13128 | | -** current document. If successful, (*pz) is set to point to a buffer |
| 13132 | +** If parameter iCol is less than zero, or greater than or equal to the |
| 13133 | +** number of columns in the table, SQLITE_RANGE is returned. |
| 13134 | +** |
| 13135 | +** Otherwise, this function attempts to retrieve the text of column iCol of |
| 13136 | +** the current document. If successful, (*pz) is set to point to a buffer |
| 13129 | 13137 | ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes |
| 13130 | 13138 | ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, |
| 13131 | 13139 | ** if an error occurs, an SQLite error code is returned and the final values |
| 13132 | 13140 | ** of (*pz) and (*pn) are undefined. |
| 13133 | 13141 | ** |
| 13134 | 13142 | ** xPhraseCount: |
| 13135 | 13143 | ** Returns the number of phrases in the current query expression. |
| 13136 | 13144 | ** |
| 13137 | 13145 | ** xPhraseSize: |
| 13138 | | -** Returns the number of tokens in phrase iPhrase of the query. Phrases |
| 13139 | | -** are numbered starting from zero. |
| 13146 | +** If parameter iCol is less than zero, or greater than or equal to the |
| 13147 | +** number of phrases in the current query, as returned by xPhraseCount, |
| 13148 | +** 0 is returned. Otherwise, this function returns the number of tokens in |
| 13149 | +** phrase iPhrase of the query. Phrases are numbered starting from zero. |
| 13140 | 13150 | ** |
| 13141 | 13151 | ** xInstCount: |
| 13142 | 13152 | ** Set *pnInst to the total number of occurrences of all phrases within |
| 13143 | 13153 | ** the query within the current row. Return SQLITE_OK if successful, or |
| 13144 | 13154 | ** an error code (i.e. SQLITE_NOMEM) if an error occurs. |
| | @@ -13150,16 +13160,17 @@ |
| 13150 | 13160 | ** |
| 13151 | 13161 | ** xInst: |
| 13152 | 13162 | ** Query for the details of phrase match iIdx within the current row. |
| 13153 | 13163 | ** Phrase matches are numbered starting from zero, so the iIdx argument |
| 13154 | 13164 | ** should be greater than or equal to zero and smaller than the value |
| 13155 | | -** output by xInstCount(). |
| 13165 | +** output by xInstCount(). If iIdx is less than zero or greater than |
| 13166 | +** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned. |
| 13156 | 13167 | ** |
| 13157 | | -** Usually, output parameter *piPhrase is set to the phrase number, *piCol |
| 13168 | +** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol |
| 13158 | 13169 | ** to the column in which it occurs and *piOff the token offset of the |
| 13159 | | -** first token of the phrase. Returns SQLITE_OK if successful, or an error |
| 13160 | | -** code (i.e. SQLITE_NOMEM) if an error occurs. |
| 13170 | +** first token of the phrase. SQLITE_OK is returned if successful, or an |
| 13171 | +** error code (i.e. SQLITE_NOMEM) if an error occurs. |
| 13161 | 13172 | ** |
| 13162 | 13173 | ** This API can be quite slow if used with an FTS5 table created with the |
| 13163 | 13174 | ** "detail=none" or "detail=column" option. |
| 13164 | 13175 | ** |
| 13165 | 13176 | ** xRowid: |
| | @@ -13180,10 +13191,14 @@ |
| 13180 | 13191 | ** row visited, the callback function passed as the fourth argument |
| 13181 | 13192 | ** is invoked. The context and API objects passed to the callback |
| 13182 | 13193 | ** function may be used to access the properties of each matched row. |
| 13183 | 13194 | ** Invoking Api.xUserData() returns a copy of the pointer passed as |
| 13184 | 13195 | ** the third argument to pUserData. |
| 13196 | +** |
| 13197 | +** If parameter iPhrase is less than zero, or greater than or equal to |
| 13198 | +** the number of phrases in the query, as returned by xPhraseCount(), |
| 13199 | +** this function returns SQLITE_RANGE. |
| 13185 | 13200 | ** |
| 13186 | 13201 | ** If the callback function returns any value other than SQLITE_OK, the |
| 13187 | 13202 | ** query is abandoned and the xQueryPhrase function returns immediately. |
| 13188 | 13203 | ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK. |
| 13189 | 13204 | ** Otherwise, the error code is propagated upwards. |
| | @@ -13295,13 +13310,46 @@ |
| 13295 | 13310 | ** significantly more efficient than those alternatives when used with |
| 13296 | 13311 | ** "detail=column" tables. |
| 13297 | 13312 | ** |
| 13298 | 13313 | ** xPhraseNextColumn() |
| 13299 | 13314 | ** See xPhraseFirstColumn above. |
| 13315 | +** |
| 13316 | +** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken) |
| 13317 | +** This is used to access token iToken of phrase iPhrase of the current |
| 13318 | +** query. Before returning, output parameter *ppToken is set to point |
| 13319 | +** to a buffer containing the requested token, and *pnToken to the |
| 13320 | +** size of this buffer in bytes. |
| 13321 | +** |
| 13322 | +** If iPhrase or iToken are less than zero, or if iPhrase is greater than |
| 13323 | +** or equal to the number of phrases in the query as reported by |
| 13324 | +** xPhraseCount(), or if iToken is equal to or greater than the number of |
| 13325 | +** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken |
| 13326 | + are both zeroed. |
| 13327 | +** |
| 13328 | +** The output text is not a copy of the query text that specified the |
| 13329 | +** token. It is the output of the tokenizer module. For tokendata=1 |
| 13330 | +** tables, this includes any embedded 0x00 and trailing data. |
| 13331 | +** |
| 13332 | +** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken) |
| 13333 | +** This is used to access token iToken of phrase hit iIdx within the |
| 13334 | +** current row. If iIdx is less than zero or greater than or equal to the |
| 13335 | +** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise, |
| 13336 | +** output variable (*ppToken) is set to point to a buffer containing the |
| 13337 | +** matching document token, and (*pnToken) to the size of that buffer in |
| 13338 | +** bytes. This API is not available if the specified token matches a |
| 13339 | +** prefix query term. In that case both output variables are always set |
| 13340 | +** to 0. |
| 13341 | +** |
| 13342 | +** The output text is not a copy of the document text that was tokenized. |
| 13343 | +** It is the output of the tokenizer module. For tokendata=1 tables, this |
| 13344 | +** includes any embedded 0x00 and trailing data. |
| 13345 | +** |
| 13346 | +** This API can be quite slow if used with an FTS5 table created with the |
| 13347 | +** "detail=none" or "detail=column" option. |
| 13300 | 13348 | */ |
| 13301 | 13349 | struct Fts5ExtensionApi { |
| 13302 | | - int iVersion; /* Currently always set to 2 */ |
| 13350 | + int iVersion; /* Currently always set to 3 */ |
| 13303 | 13351 | |
| 13304 | 13352 | void *(*xUserData)(Fts5Context*); |
| 13305 | 13353 | |
| 13306 | 13354 | int (*xColumnCount)(Fts5Context*); |
| 13307 | 13355 | int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow); |
| | @@ -13332,10 +13380,17 @@ |
| 13332 | 13380 | int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*); |
| 13333 | 13381 | void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff); |
| 13334 | 13382 | |
| 13335 | 13383 | int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*); |
| 13336 | 13384 | void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol); |
| 13385 | + |
| 13386 | + /* Below this point are iVersion>=3 only */ |
| 13387 | + int (*xQueryToken)(Fts5Context*, |
| 13388 | + int iPhrase, int iToken, |
| 13389 | + const char **ppToken, int *pnToken |
| 13390 | + ); |
| 13391 | + int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*); |
| 13337 | 13392 | }; |
| 13338 | 13393 | |
| 13339 | 13394 | /* |
| 13340 | 13395 | ** CUSTOM AUXILIARY FUNCTIONS |
| 13341 | 13396 | *************************************************************************/ |
| | @@ -13818,11 +13873,11 @@ |
| 13818 | 13873 | ** This is really just the default value for the max_page_count pragma. |
| 13819 | 13874 | ** This value can be lowered (or raised) at run-time using that the |
| 13820 | 13875 | ** max_page_count macro. |
| 13821 | 13876 | */ |
| 13822 | 13877 | #ifndef SQLITE_MAX_PAGE_COUNT |
| 13823 | | -# define SQLITE_MAX_PAGE_COUNT 1073741823 |
| 13878 | +# define SQLITE_MAX_PAGE_COUNT 0xfffffffe /* 4294967294 */ |
| 13824 | 13879 | #endif |
| 13825 | 13880 | |
| 13826 | 13881 | /* |
| 13827 | 13882 | ** Maximum length (in bytes) of the pattern in a LIKE or GLOB |
| 13828 | 13883 | ** operator. |
| | @@ -13956,10 +14011,23 @@ |
| 13956 | 14011 | #if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH) |
| 13957 | 14012 | # define SQLITE_USE_SEH 1 |
| 13958 | 14013 | #else |
| 13959 | 14014 | # undef SQLITE_USE_SEH |
| 13960 | 14015 | #endif |
| 14016 | + |
| 14017 | +/* |
| 14018 | +** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly |
| 14019 | +** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0 |
| 14020 | +*/ |
| 14021 | +#if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1 |
| 14022 | + /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */ |
| 14023 | +# undef SQLITE_DIRECT_OVERFLOW_READ |
| 14024 | +#else |
| 14025 | + /* In all other cases, enable */ |
| 14026 | +# define SQLITE_DIRECT_OVERFLOW_READ 1 |
| 14027 | +#endif |
| 14028 | + |
| 13961 | 14029 | |
| 13962 | 14030 | /* |
| 13963 | 14031 | ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. |
| 13964 | 14032 | ** 0 means mutexes are permanently disable and the library is never |
| 13965 | 14033 | ** threadsafe. 1 means the library is serialized which is the highest |
| | @@ -15839,11 +15907,11 @@ |
| 15839 | 15907 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); |
| 15840 | 15908 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*); |
| 15841 | 15909 | SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); |
| 15842 | 15910 | SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); |
| 15843 | 15911 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); |
| 15844 | | -SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *); |
| 15912 | +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, u64*); |
| 15845 | 15913 | SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*); |
| 15846 | 15914 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 15847 | 15915 | |
| 15848 | 15916 | /* Functions used to truncate the database file. */ |
| 15849 | 15917 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| | @@ -16426,10 +16494,11 @@ |
| 16426 | 16494 | #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */ |
| 16427 | 16495 | #define P4_REAL (-12) /* P4 is a 64-bit floating point value */ |
| 16428 | 16496 | #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ |
| 16429 | 16497 | #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */ |
| 16430 | 16498 | #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */ |
| 16499 | +#define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */ |
| 16431 | 16500 | |
| 16432 | 16501 | /* Error message codes for OP_Halt */ |
| 16433 | 16502 | #define P5_ConstraintNotNull 1 |
| 16434 | 16503 | #define P5_ConstraintUnique 2 |
| 16435 | 16504 | #define P5_ConstraintCheck 3 |
| | @@ -16648,17 +16717,19 @@ |
| 16648 | 16717 | #define OP_VColumn 176 /* synopsis: r[P3]=vcolumn(P2) */ |
| 16649 | 16718 | #define OP_VRename 177 |
| 16650 | 16719 | #define OP_Pagecount 178 |
| 16651 | 16720 | #define OP_MaxPgcnt 179 |
| 16652 | 16721 | #define OP_ClrSubtype 180 /* synopsis: r[P1].subtype = 0 */ |
| 16653 | | -#define OP_FilterAdd 181 /* synopsis: filter(P1) += key(P3@P4) */ |
| 16654 | | -#define OP_Trace 182 |
| 16655 | | -#define OP_CursorHint 183 |
| 16656 | | -#define OP_ReleaseReg 184 /* synopsis: release r[P1@P2] mask P3 */ |
| 16657 | | -#define OP_Noop 185 |
| 16658 | | -#define OP_Explain 186 |
| 16659 | | -#define OP_Abortable 187 |
| 16722 | +#define OP_GetSubtype 181 /* synopsis: r[P2] = r[P1].subtype */ |
| 16723 | +#define OP_SetSubtype 182 /* synopsis: r[P2].subtype = r[P1] */ |
| 16724 | +#define OP_FilterAdd 183 /* synopsis: filter(P1) += key(P3@P4) */ |
| 16725 | +#define OP_Trace 184 |
| 16726 | +#define OP_CursorHint 185 |
| 16727 | +#define OP_ReleaseReg 186 /* synopsis: release r[P1@P2] mask P3 */ |
| 16728 | +#define OP_Noop 187 |
| 16729 | +#define OP_Explain 188 |
| 16730 | +#define OP_Abortable 189 |
| 16660 | 16731 | |
| 16661 | 16732 | /* Properties such as "out2" or "jump" that are specified in |
| 16662 | 16733 | ** comments following the "case" for each opcode in the vdbe.c |
| 16663 | 16734 | ** are encoded into bitvectors as follows: |
| 16664 | 16735 | */ |
| | @@ -16690,12 +16761,12 @@ |
| 16690 | 16761 | /* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\ |
| 16691 | 16762 | /* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\ |
| 16692 | 16763 | /* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\ |
| 16693 | 16764 | /* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 16694 | 16765 | /* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x50,\ |
| 16695 | | -/* 176 */ 0x40, 0x00, 0x10, 0x10, 0x02, 0x00, 0x00, 0x00,\ |
| 16696 | | -/* 184 */ 0x00, 0x00, 0x00, 0x00,} |
| 16766 | +/* 176 */ 0x40, 0x00, 0x10, 0x10, 0x02, 0x12, 0x12, 0x00,\ |
| 16767 | +/* 184 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,} |
| 16697 | 16768 | |
| 16698 | 16769 | /* The resolve3P2Values() routine is able to run faster if it knows |
| 16699 | 16770 | ** the value of the largest JUMP opcode. The smaller the maximum |
| 16700 | 16771 | ** JUMP opcode the better, so the mkopcodeh.tcl script that |
| 16701 | 16772 | ** generated this include file strives to group all JUMP opcodes |
| | @@ -17952,15 +18023,15 @@ |
| 17952 | 18023 | {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \ |
| 17953 | 18024 | SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } |
| 17954 | 18025 | #define MFUNCTION(zName, nArg, xPtr, xFunc) \ |
| 17955 | 18026 | {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \ |
| 17956 | 18027 | xPtr, 0, xFunc, 0, 0, 0, #zName, {0} } |
| 17957 | | -#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, iArg, xFunc) \ |
| 18028 | +#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, bJsonB, iArg, xFunc) \ |
| 17958 | 18029 | {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\ |
| 17959 | 18030 | SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\ |
| 17960 | 18031 | ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \ |
| 17961 | | - SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } |
| 18032 | + SQLITE_INT_TO_PTR(iArg|((bJsonB)*JSON_BLOB)),0,xFunc,0, 0, 0, #zName, {0} } |
| 17962 | 18033 | #define INLINE_FUNC(zName, nArg, iArg, mFlags) \ |
| 17963 | 18034 | {nArg, SQLITE_FUNC_BUILTIN|\ |
| 17964 | 18035 | SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \ |
| 17965 | 18036 | SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} } |
| 17966 | 18037 | #define TEST_FUNC(zName, nArg, iArg, mFlags) \ |
| | @@ -18591,10 +18662,11 @@ |
| 18591 | 18662 | unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 18592 | 18663 | unsigned isResized:1; /* True if resizeIndexObject() has been called */ |
| 18593 | 18664 | unsigned isCovering:1; /* True if this is a covering index */ |
| 18594 | 18665 | unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ |
| 18595 | 18666 | unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */ |
| 18667 | + unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */ |
| 18596 | 18668 | unsigned bNoQuery:1; /* Do not use this index to optimize queries */ |
| 18597 | 18669 | unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */ |
| 18598 | 18670 | unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */ |
| 18599 | 18671 | unsigned bHasExpr:1; /* Index contains an expression, either a literal |
| 18600 | 18672 | ** expression, or a reference to a VIRTUAL column */ |
| | @@ -18704,10 +18776,11 @@ |
| 18704 | 18776 | int iDistinct; /* Ephemeral table used to enforce DISTINCT */ |
| 18705 | 18777 | int iDistAddr; /* Address of OP_OpenEphemeral */ |
| 18706 | 18778 | int iOBTab; /* Ephemeral table to implement ORDER BY */ |
| 18707 | 18779 | u8 bOBPayload; /* iOBTab has payload columns separate from key */ |
| 18708 | 18780 | u8 bOBUnique; /* Enforce uniqueness on iOBTab keys */ |
| 18781 | + u8 bUseSubtype; /* Transfer subtype info through sorter */ |
| 18709 | 18782 | } *aFunc; |
| 18710 | 18783 | int nFunc; /* Number of entries in aFunc[] */ |
| 18711 | 18784 | u32 selId; /* Select to which this AggInfo belongs */ |
| 18712 | 18785 | #ifdef SQLITE_DEBUG |
| 18713 | 18786 | Select *pSelect; /* SELECT statement that this AggInfo supports */ |
| | @@ -19237,10 +19310,11 @@ |
| 19237 | 19310 | } uNC; |
| 19238 | 19311 | NameContext *pNext; /* Next outer name context. NULL for outermost */ |
| 19239 | 19312 | int nRef; /* Number of names resolved by this context */ |
| 19240 | 19313 | int nNcErr; /* Number of errors encountered while resolving names */ |
| 19241 | 19314 | int ncFlags; /* Zero or more NC_* flags defined below */ |
| 19315 | + u32 nNestedSelect; /* Number of nested selects using this NC */ |
| 19242 | 19316 | Select *pWinSelect; /* SELECT statement for any window functions */ |
| 19243 | 19317 | }; |
| 19244 | 19318 | |
| 19245 | 19319 | /* |
| 19246 | 19320 | ** Allowed values for the NameContext, ncFlags field. |
| | @@ -19953,10 +20027,13 @@ |
| 19953 | 20027 | ** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than |
| 19954 | 20028 | ** sqlite3_free() |
| 19955 | 20029 | ** |
| 19956 | 20030 | ** 3. Make a (read-only) copy of a read-only RCStr string using |
| 19957 | 20031 | ** sqlite3RCStrRef(). |
| 20032 | +** |
| 20033 | +** "String" is in the name, but an RCStr object can also be used to hold |
| 20034 | +** binary data. |
| 19958 | 20035 | */ |
| 19959 | 20036 | struct RCStr { |
| 19960 | 20037 | u64 nRCRef; /* Number of references */ |
| 19961 | 20038 | /* Total structure size should be a multiple of 8 bytes for alignment */ |
| 19962 | 20039 | }; |
| | @@ -20011,10 +20088,13 @@ |
| 20011 | 20088 | u8 bOpenUri; /* True to interpret filenames as URIs */ |
| 20012 | 20089 | u8 bUseCis; /* Use covering indices for full-scans */ |
| 20013 | 20090 | u8 bSmallMalloc; /* Avoid large memory allocations if true */ |
| 20014 | 20091 | u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */ |
| 20015 | 20092 | u8 bUseLongDouble; /* Make use of long double */ |
| 20093 | +#ifdef SQLITE_DEBUG |
| 20094 | + u8 bJsonSelfcheck; /* Double-check JSON parsing */ |
| 20095 | +#endif |
| 20016 | 20096 | int mxStrlen; /* Maximum string length */ |
| 20017 | 20097 | int neverCorrupt; /* Database is always well-formed */ |
| 20018 | 20098 | int szLookaside; /* Default lookaside buffer size */ |
| 20019 | 20099 | int nLookaside; /* Default lookaside buffer count */ |
| 20020 | 20100 | int nStmtSpill; /* Stmt-journal spill-to-disk threshold */ |
| | @@ -20637,19 +20717,21 @@ |
| 20637 | 20717 | SQLITE_PRIVATE void sqlite3ExprAddFunctionOrderBy(Parse*,Expr*,ExprList*); |
| 20638 | 20718 | SQLITE_PRIVATE void sqlite3ExprOrderByAggregateError(Parse*,Expr*); |
| 20639 | 20719 | SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*); |
| 20640 | 20720 | SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32); |
| 20641 | 20721 | SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*); |
| 20722 | +SQLITE_PRIVATE void sqlite3ExprDeleteGeneric(sqlite3*,void*); |
| 20642 | 20723 | SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse*, Expr*); |
| 20643 | 20724 | SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*); |
| 20644 | 20725 | SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*); |
| 20645 | 20726 | SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*); |
| 20646 | 20727 | SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse*, int, ExprList*); |
| 20647 | 20728 | SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int); |
| 20648 | 20729 | SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int); |
| 20649 | 20730 | SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*); |
| 20650 | 20731 | SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*); |
| 20732 | +SQLITE_PRIVATE void sqlite3ExprListDeleteGeneric(sqlite3*,void*); |
| 20651 | 20733 | SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*); |
| 20652 | 20734 | SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*); |
| 20653 | 20735 | SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**); |
| 20654 | 20736 | SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**); |
| 20655 | 20737 | SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32); |
| | @@ -20736,10 +20818,11 @@ |
| 20736 | 20818 | SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask); |
| 20737 | 20819 | #endif |
| 20738 | 20820 | SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); |
| 20739 | 20821 | SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); |
| 20740 | 20822 | SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); |
| 20823 | +SQLITE_PRIVATE void sqlite3DeleteTableGeneric(sqlite3*, void*); |
| 20741 | 20824 | SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*); |
| 20742 | 20825 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 20743 | 20826 | SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); |
| 20744 | 20827 | SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); |
| 20745 | 20828 | #else |
| | @@ -20772,10 +20855,11 @@ |
| 20772 | 20855 | SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int); |
| 20773 | 20856 | SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); |
| 20774 | 20857 | SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, |
| 20775 | 20858 | Expr*,ExprList*,u32,Expr*); |
| 20776 | 20859 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*); |
| 20860 | +SQLITE_PRIVATE void sqlite3SelectDeleteGeneric(sqlite3*,void*); |
| 20777 | 20861 | SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*); |
| 20778 | 20862 | SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, Trigger*); |
| 20779 | 20863 | SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); |
| 20780 | 20864 | #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
| 20781 | 20865 | SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*); |
| | @@ -20998,10 +21082,11 @@ |
| 20998 | 21082 | #ifndef SQLITE_OMIT_UTF16 |
| 20999 | 21083 | SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar); |
| 21000 | 21084 | #endif |
| 21001 | 21085 | SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte); |
| 21002 | 21086 | SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**); |
| 21087 | +SQLITE_PRIVATE int sqlite3Utf8ReadLimited(const u8*, int, u32*); |
| 21003 | 21088 | SQLITE_PRIVATE LogEst sqlite3LogEst(u64); |
| 21004 | 21089 | SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst); |
| 21005 | 21090 | SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double); |
| 21006 | 21091 | SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst); |
| 21007 | 21092 | SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int); |
| | @@ -21344,10 +21429,11 @@ |
| 21344 | 21429 | #ifndef SQLITE_OMIT_CTE |
| 21345 | 21430 | SQLITE_PRIVATE Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8); |
| 21346 | 21431 | SQLITE_PRIVATE void sqlite3CteDelete(sqlite3*,Cte*); |
| 21347 | 21432 | SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Cte*); |
| 21348 | 21433 | SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*); |
| 21434 | +SQLITE_PRIVATE void sqlite3WithDeleteGeneric(sqlite3*,void*); |
| 21349 | 21435 | SQLITE_PRIVATE With *sqlite3WithPush(Parse*, With*, u8); |
| 21350 | 21436 | #else |
| 21351 | 21437 | # define sqlite3CteNew(P,T,E,S) ((void*)0) |
| 21352 | 21438 | # define sqlite3CteDelete(D,C) |
| 21353 | 21439 | # define sqlite3CteWithAdd(P,W,C) ((void*)0) |
| | @@ -22721,10 +22807,13 @@ |
| 22721 | 22807 | SQLITE_USE_URI, /* bOpenUri */ |
| 22722 | 22808 | SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */ |
| 22723 | 22809 | 0, /* bSmallMalloc */ |
| 22724 | 22810 | 1, /* bExtraSchemaChecks */ |
| 22725 | 22811 | sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */ |
| 22812 | +#ifdef SQLITE_DEBUG |
| 22813 | + 0, /* bJsonSelfcheck */ |
| 22814 | +#endif |
| 22726 | 22815 | 0x7ffffffe, /* mxStrlen */ |
| 22727 | 22816 | 0, /* neverCorrupt */ |
| 22728 | 22817 | SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */ |
| 22729 | 22818 | SQLITE_STMTJRNL_SPILL, /* nStmtSpill */ |
| 22730 | 22819 | {0,0,0,0,0,0,0,0}, /* m */ |
| | @@ -23973,11 +24062,11 @@ |
| 23973 | 24062 | /* no break */ deliberate_fall_through |
| 23974 | 24063 | case SQLITE_DBSTATUS_CACHE_HIT: |
| 23975 | 24064 | case SQLITE_DBSTATUS_CACHE_MISS: |
| 23976 | 24065 | case SQLITE_DBSTATUS_CACHE_WRITE:{ |
| 23977 | 24066 | int i; |
| 23978 | | - int nRet = 0; |
| 24067 | + u64 nRet = 0; |
| 23979 | 24068 | assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); |
| 23980 | 24069 | assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); |
| 23981 | 24070 | |
| 23982 | 24071 | for(i=0; i<db->nDb; i++){ |
| 23983 | 24072 | if( db->aDb[i].pBt ){ |
| | @@ -23986,11 +24075,11 @@ |
| 23986 | 24075 | } |
| 23987 | 24076 | } |
| 23988 | 24077 | *pHighwater = 0; /* IMP: R-42420-56072 */ |
| 23989 | 24078 | /* IMP: R-54100-20147 */ |
| 23990 | 24079 | /* IMP: R-29431-39229 */ |
| 23991 | | - *pCurrent = nRet; |
| 24080 | + *pCurrent = (int)nRet & 0x7fffffff; |
| 23992 | 24081 | break; |
| 23993 | 24082 | } |
| 23994 | 24083 | |
| 23995 | 24084 | /* Set *pCurrent to non-zero if there are unresolved deferred foreign |
| 23996 | 24085 | ** key constraints. Set *pCurrent to zero if all foreign key constraints |
| | @@ -25055,10 +25144,16 @@ |
| 25055 | 25144 | n = sqlite3_value_bytes(argv[i]); |
| 25056 | 25145 | if( z==0 || parseModifier(context, (char*)z, n, p, i) ) return 1; |
| 25057 | 25146 | } |
| 25058 | 25147 | computeJD(p); |
| 25059 | 25148 | if( p->isError || !validJulianDay(p->iJD) ) return 1; |
| 25149 | + if( argc==1 && p->validYMD && p->D>28 ){ |
| 25150 | + /* Make sure a YYYY-MM-DD is normalized. |
| 25151 | + ** Example: 2023-02-31 -> 2023-03-03 */ |
| 25152 | + assert( p->validJD ); |
| 25153 | + p->validYMD = 0; |
| 25154 | + } |
| 25060 | 25155 | return 0; |
| 25061 | 25156 | } |
| 25062 | 25157 | |
| 25063 | 25158 | |
| 25064 | 25159 | /* |
| | @@ -32083,11 +32178,11 @@ |
| 32083 | 32178 | va_end(ap); |
| 32084 | 32179 | } |
| 32085 | 32180 | |
| 32086 | 32181 | |
| 32087 | 32182 | /***************************************************************************** |
| 32088 | | -** Reference counted string storage |
| 32183 | +** Reference counted string/blob storage |
| 32089 | 32184 | *****************************************************************************/ |
| 32090 | 32185 | |
| 32091 | 32186 | /* |
| 32092 | 32187 | ** Increase the reference count of the string by one. |
| 32093 | 32188 | ** |
| | @@ -32935,11 +33030,11 @@ |
| 32935 | 33030 | pX = pExpr->pLeft; |
| 32936 | 33031 | assert( ExprUseXList(pExpr) ); |
| 32937 | 33032 | assert( pExpr->x.pList->nExpr==2 ); |
| 32938 | 33033 | pY = pExpr->x.pList->a[0].pExpr; |
| 32939 | 33034 | pZ = pExpr->x.pList->a[1].pExpr; |
| 32940 | | - sqlite3TreeViewLine(pView, "BETWEEN"); |
| 33035 | + sqlite3TreeViewLine(pView, "BETWEEN%s", zFlgs); |
| 32941 | 33036 | sqlite3TreeViewExpr(pView, pX, 1); |
| 32942 | 33037 | sqlite3TreeViewExpr(pView, pY, 1); |
| 32943 | 33038 | sqlite3TreeViewExpr(pView, pZ, 0); |
| 32944 | 33039 | break; |
| 32945 | 33040 | } |
| | @@ -34070,11 +34165,42 @@ |
| 34070 | 34165 | || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } |
| 34071 | 34166 | } |
| 34072 | 34167 | return c; |
| 34073 | 34168 | } |
| 34074 | 34169 | |
| 34075 | | - |
| 34170 | +/* |
| 34171 | +** Read a single UTF8 character out of buffer z[], but reading no |
| 34172 | +** more than n characters from the buffer. z[] is not zero-terminated. |
| 34173 | +** |
| 34174 | +** Return the number of bytes used to construct the character. |
| 34175 | +** |
| 34176 | +** Invalid UTF8 might generate a strange result. No effort is made |
| 34177 | +** to detect invalid UTF8. |
| 34178 | +** |
| 34179 | +** At most 4 bytes will be read out of z[]. The return value will always |
| 34180 | +** be between 1 and 4. |
| 34181 | +*/ |
| 34182 | +SQLITE_PRIVATE int sqlite3Utf8ReadLimited( |
| 34183 | + const u8 *z, |
| 34184 | + int n, |
| 34185 | + u32 *piOut |
| 34186 | +){ |
| 34187 | + u32 c; |
| 34188 | + int i = 1; |
| 34189 | + assert( n>0 ); |
| 34190 | + c = z[0]; |
| 34191 | + if( c>=0xc0 ){ |
| 34192 | + c = sqlite3Utf8Trans1[c-0xc0]; |
| 34193 | + if( n>4 ) n = 4; |
| 34194 | + while( i<n && (z[i] & 0xc0)==0x80 ){ |
| 34195 | + c = (c<<6) + (0x3f & z[i]); |
| 34196 | + i++; |
| 34197 | + } |
| 34198 | + } |
| 34199 | + *piOut = c; |
| 34200 | + return i; |
| 34201 | +} |
| 34076 | 34202 | |
| 34077 | 34203 | |
| 34078 | 34204 | /* |
| 34079 | 34205 | ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is |
| 34080 | 34206 | ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate(). |
| | @@ -34585,11 +34711,11 @@ |
| 34585 | 34711 | ** Load the sqlite3.iSysErrno field if that is an appropriate thing |
| 34586 | 34712 | ** to do based on the SQLite error code in rc. |
| 34587 | 34713 | */ |
| 34588 | 34714 | SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){ |
| 34589 | 34715 | if( rc==SQLITE_IOERR_NOMEM ) return; |
| 34590 | | -#ifdef SQLITE_USE_SEH |
| 34716 | +#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) |
| 34591 | 34717 | if( rc==SQLITE_IOERR_IN_PAGE ){ |
| 34592 | 34718 | int ii; |
| 34593 | 34719 | int iErr; |
| 34594 | 34720 | sqlite3BtreeEnterAll(db); |
| 34595 | 34721 | for(ii=0; ii<db->nDb; ii++){ |
| | @@ -36839,17 +36965,19 @@ |
| 36839 | 36965 | /* 176 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 36840 | 36966 | /* 177 */ "VRename" OpHelp(""), |
| 36841 | 36967 | /* 178 */ "Pagecount" OpHelp(""), |
| 36842 | 36968 | /* 179 */ "MaxPgcnt" OpHelp(""), |
| 36843 | 36969 | /* 180 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"), |
| 36844 | | - /* 181 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 36845 | | - /* 182 */ "Trace" OpHelp(""), |
| 36846 | | - /* 183 */ "CursorHint" OpHelp(""), |
| 36847 | | - /* 184 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 36848 | | - /* 185 */ "Noop" OpHelp(""), |
| 36849 | | - /* 186 */ "Explain" OpHelp(""), |
| 36850 | | - /* 187 */ "Abortable" OpHelp(""), |
| 36970 | + /* 181 */ "GetSubtype" OpHelp("r[P2] = r[P1].subtype"), |
| 36971 | + /* 182 */ "SetSubtype" OpHelp("r[P2].subtype = r[P1]"), |
| 36972 | + /* 183 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 36973 | + /* 184 */ "Trace" OpHelp(""), |
| 36974 | + /* 185 */ "CursorHint" OpHelp(""), |
| 36975 | + /* 186 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 36976 | + /* 187 */ "Noop" OpHelp(""), |
| 36977 | + /* 188 */ "Explain" OpHelp(""), |
| 36978 | + /* 189 */ "Abortable" OpHelp(""), |
| 36851 | 36979 | }; |
| 36852 | 36980 | return azName[i]; |
| 36853 | 36981 | } |
| 36854 | 36982 | #endif |
| 36855 | 36983 | |
| | @@ -41891,11 +42019,17 @@ |
| 41891 | 42019 | return SQLITE_OK; |
| 41892 | 42020 | } |
| 41893 | 42021 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 41894 | 42022 | case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 41895 | 42023 | int iOld = pFile->iBusyTimeout; |
| 42024 | +#if SQLITE_ENABLE_SETLK_TIMEOUT==1 |
| 41896 | 42025 | pFile->iBusyTimeout = *(int*)pArg; |
| 42026 | +#elif SQLITE_ENABLE_SETLK_TIMEOUT==2 |
| 42027 | + pFile->iBusyTimeout = !!(*(int*)pArg); |
| 42028 | +#else |
| 42029 | +# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2" |
| 42030 | +#endif |
| 41897 | 42031 | *(int*)pArg = iOld; |
| 41898 | 42032 | return SQLITE_OK; |
| 41899 | 42033 | } |
| 41900 | 42034 | #endif |
| 41901 | 42035 | #if SQLITE_MAX_MMAP_SIZE>0 |
| | @@ -42144,10 +42278,29 @@ |
| 42144 | 42278 | ** zFilename |
| 42145 | 42279 | ** |
| 42146 | 42280 | ** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and |
| 42147 | 42281 | ** unixMutexHeld() is true when reading or writing any other field |
| 42148 | 42282 | ** in this structure. |
| 42283 | +** |
| 42284 | +** aLock[SQLITE_SHM_NLOCK]: |
| 42285 | +** This array records the various locks held by clients on each of the |
| 42286 | +** SQLITE_SHM_NLOCK slots. If the aLock[] entry is set to 0, then no |
| 42287 | +** locks are held by the process on this slot. If it is set to -1, then |
| 42288 | +** some client holds an EXCLUSIVE lock on the locking slot. If the aLock[] |
| 42289 | +** value is set to a positive value, then it is the number of shared |
| 42290 | +** locks currently held on the slot. |
| 42291 | +** |
| 42292 | +** aMutex[SQLITE_SHM_NLOCK]: |
| 42293 | +** Normally, when SQLITE_ENABLE_SETLK_TIMEOUT is not defined, mutex |
| 42294 | +** pShmMutex is used to protect the aLock[] array and the right to |
| 42295 | +** call fcntl() on unixShmNode.hShm to obtain or release locks. |
| 42296 | +** |
| 42297 | +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined though, we use an array |
| 42298 | +** of mutexes - one for each locking slot. To read or write locking |
| 42299 | +** slot aLock[iSlot], the caller must hold the corresponding mutex |
| 42300 | +** aMutex[iSlot]. Similarly, to call fcntl() to obtain or release a |
| 42301 | +** lock corresponding to slot iSlot, mutex aMutex[iSlot] must be held. |
| 42149 | 42302 | */ |
| 42150 | 42303 | struct unixShmNode { |
| 42151 | 42304 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
| 42152 | 42305 | sqlite3_mutex *pShmMutex; /* Mutex to access this object */ |
| 42153 | 42306 | char *zFilename; /* Name of the mmapped file */ |
| | @@ -42157,14 +42310,15 @@ |
| 42157 | 42310 | u8 isReadonly; /* True if read-only */ |
| 42158 | 42311 | u8 isUnlocked; /* True if no DMS lock held */ |
| 42159 | 42312 | char **apRegion; /* Array of mapped shared-memory regions */ |
| 42160 | 42313 | int nRef; /* Number of unixShm objects pointing to this */ |
| 42161 | 42314 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
| 42315 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42316 | + sqlite3_mutex *aMutex[SQLITE_SHM_NLOCK]; |
| 42317 | +#endif |
| 42162 | 42318 | int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */ |
| 42163 | 42319 | #ifdef SQLITE_DEBUG |
| 42164 | | - u8 exclMask; /* Mask of exclusive locks held */ |
| 42165 | | - u8 sharedMask; /* Mask of shared locks held */ |
| 42166 | 42320 | u8 nextShmId; /* Next available unixShm.id value */ |
| 42167 | 42321 | #endif |
| 42168 | 42322 | }; |
| 42169 | 42323 | |
| 42170 | 42324 | /* |
| | @@ -42243,20 +42397,39 @@ |
| 42243 | 42397 | ){ |
| 42244 | 42398 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 42245 | 42399 | struct flock f; /* The posix advisory locking structure */ |
| 42246 | 42400 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
| 42247 | 42401 | |
| 42248 | | - /* Access to the unixShmNode object is serialized by the caller */ |
| 42249 | 42402 | pShmNode = pFile->pInode->pShmNode; |
| 42250 | | - assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); |
| 42251 | | - assert( pShmNode->nRef>0 || unixMutexHeld() ); |
| 42403 | + |
| 42404 | + /* Assert that the parameters are within expected range and that the |
| 42405 | + ** correct mutex or mutexes are held. */ |
| 42406 | + assert( pShmNode->nRef>=0 ); |
| 42407 | + assert( (ofst==UNIX_SHM_DMS && n==1) |
| 42408 | + || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK)) |
| 42409 | + ); |
| 42410 | + if( ofst==UNIX_SHM_DMS ){ |
| 42411 | + assert( pShmNode->nRef>0 || unixMutexHeld() ); |
| 42412 | + assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); |
| 42413 | + }else{ |
| 42414 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42415 | + int ii; |
| 42416 | + for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){ |
| 42417 | + assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) ); |
| 42418 | + } |
| 42419 | +#else |
| 42420 | + assert( sqlite3_mutex_held(pShmNode->pShmMutex) ); |
| 42421 | + assert( pShmNode->nRef>0 ); |
| 42422 | +#endif |
| 42423 | + } |
| 42252 | 42424 | |
| 42253 | 42425 | /* Shared locks never span more than one byte */ |
| 42254 | 42426 | assert( n==1 || lockType!=F_RDLCK ); |
| 42255 | 42427 | |
| 42256 | 42428 | /* Locks are within range */ |
| 42257 | 42429 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 42430 | + assert( ofst>=UNIX_SHM_BASE && ofst<=(UNIX_SHM_DMS+SQLITE_SHM_NLOCK) ); |
| 42258 | 42431 | |
| 42259 | 42432 | if( pShmNode->hShm>=0 ){ |
| 42260 | 42433 | int res; |
| 42261 | 42434 | /* Initialize the locking parameters */ |
| 42262 | 42435 | f.l_type = lockType; |
| | @@ -42263,50 +42436,39 @@ |
| 42263 | 42436 | f.l_whence = SEEK_SET; |
| 42264 | 42437 | f.l_start = ofst; |
| 42265 | 42438 | f.l_len = n; |
| 42266 | 42439 | res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 42267 | 42440 | if( res==-1 ){ |
| 42268 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42441 | +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && SQLITE_ENABLE_SETLK_TIMEOUT==1 |
| 42269 | 42442 | rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); |
| 42270 | 42443 | #else |
| 42271 | 42444 | rc = SQLITE_BUSY; |
| 42272 | 42445 | #endif |
| 42273 | 42446 | } |
| 42274 | 42447 | } |
| 42275 | 42448 | |
| 42276 | | - /* Update the global lock state and do debug tracing */ |
| 42277 | | -#ifdef SQLITE_DEBUG |
| 42278 | | - { u16 mask; |
| 42279 | | - OSTRACE(("SHM-LOCK ")); |
| 42280 | | - mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
| 42281 | | - if( rc==SQLITE_OK ){ |
| 42282 | | - if( lockType==F_UNLCK ){ |
| 42283 | | - OSTRACE(("unlock %d ok", ofst)); |
| 42284 | | - pShmNode->exclMask &= ~mask; |
| 42285 | | - pShmNode->sharedMask &= ~mask; |
| 42286 | | - }else if( lockType==F_RDLCK ){ |
| 42287 | | - OSTRACE(("read-lock %d ok", ofst)); |
| 42288 | | - pShmNode->exclMask &= ~mask; |
| 42289 | | - pShmNode->sharedMask |= mask; |
| 42290 | | - }else{ |
| 42291 | | - assert( lockType==F_WRLCK ); |
| 42292 | | - OSTRACE(("write-lock %d ok", ofst)); |
| 42293 | | - pShmNode->exclMask |= mask; |
| 42294 | | - pShmNode->sharedMask &= ~mask; |
| 42295 | | - } |
| 42296 | | - }else{ |
| 42297 | | - if( lockType==F_UNLCK ){ |
| 42298 | | - OSTRACE(("unlock %d failed", ofst)); |
| 42299 | | - }else if( lockType==F_RDLCK ){ |
| 42300 | | - OSTRACE(("read-lock failed")); |
| 42301 | | - }else{ |
| 42302 | | - assert( lockType==F_WRLCK ); |
| 42303 | | - OSTRACE(("write-lock %d failed", ofst)); |
| 42304 | | - } |
| 42305 | | - } |
| 42306 | | - OSTRACE((" - afterwards %03x,%03x\n", |
| 42307 | | - pShmNode->sharedMask, pShmNode->exclMask)); |
| 42449 | + /* Do debug tracing */ |
| 42450 | +#ifdef SQLITE_DEBUG |
| 42451 | + OSTRACE(("SHM-LOCK ")); |
| 42452 | + if( rc==SQLITE_OK ){ |
| 42453 | + if( lockType==F_UNLCK ){ |
| 42454 | + OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1)); |
| 42455 | + }else if( lockType==F_RDLCK ){ |
| 42456 | + OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1)); |
| 42457 | + }else{ |
| 42458 | + assert( lockType==F_WRLCK ); |
| 42459 | + OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1)); |
| 42460 | + } |
| 42461 | + }else{ |
| 42462 | + if( lockType==F_UNLCK ){ |
| 42463 | + OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1)); |
| 42464 | + }else if( lockType==F_RDLCK ){ |
| 42465 | + OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1)); |
| 42466 | + }else{ |
| 42467 | + assert( lockType==F_WRLCK ); |
| 42468 | + OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1)); |
| 42469 | + } |
| 42308 | 42470 | } |
| 42309 | 42471 | #endif |
| 42310 | 42472 | |
| 42311 | 42473 | return rc; |
| 42312 | 42474 | } |
| | @@ -42340,10 +42502,15 @@ |
| 42340 | 42502 | if( p && ALWAYS(p->nRef==0) ){ |
| 42341 | 42503 | int nShmPerMap = unixShmRegionPerMap(); |
| 42342 | 42504 | int i; |
| 42343 | 42505 | assert( p->pInode==pFd->pInode ); |
| 42344 | 42506 | sqlite3_mutex_free(p->pShmMutex); |
| 42507 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42508 | + for(i=0; i<SQLITE_SHM_NLOCK; i++){ |
| 42509 | + sqlite3_mutex_free(p->aMutex[i]); |
| 42510 | + } |
| 42511 | +#endif |
| 42345 | 42512 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
| 42346 | 42513 | if( p->hShm>=0 ){ |
| 42347 | 42514 | osMunmap(p->apRegion[i], p->szRegion); |
| 42348 | 42515 | }else{ |
| 42349 | 42516 | sqlite3_free(p->apRegion[i]); |
| | @@ -42399,11 +42566,24 @@ |
| 42399 | 42566 | }else if( lock.l_type==F_UNLCK ){ |
| 42400 | 42567 | if( pShmNode->isReadonly ){ |
| 42401 | 42568 | pShmNode->isUnlocked = 1; |
| 42402 | 42569 | rc = SQLITE_READONLY_CANTINIT; |
| 42403 | 42570 | }else{ |
| 42571 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42572 | + /* Do not use a blocking lock here. If the lock cannot be obtained |
| 42573 | + ** immediately, it means some other connection is truncating the |
| 42574 | + ** *-shm file. And after it has done so, it will not release its |
| 42575 | + ** lock, but only downgrade it to a shared lock. So no point in |
| 42576 | + ** blocking here. The call below to obtain the shared DMS lock may |
| 42577 | + ** use a blocking lock. */ |
| 42578 | + int iSaveTimeout = pDbFd->iBusyTimeout; |
| 42579 | + pDbFd->iBusyTimeout = 0; |
| 42580 | +#endif |
| 42404 | 42581 | rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); |
| 42582 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42583 | + pDbFd->iBusyTimeout = iSaveTimeout; |
| 42584 | +#endif |
| 42405 | 42585 | /* The first connection to attach must truncate the -shm file. We |
| 42406 | 42586 | ** truncate to 3 bytes (an arbitrary small number, less than the |
| 42407 | 42587 | ** -shm header size) rather than 0 as a system debugging aid, to |
| 42408 | 42588 | ** help detect if a -shm file truncation is legitimate or is the work |
| 42409 | 42589 | ** or a rogue process. */ |
| | @@ -42520,10 +42700,22 @@ |
| 42520 | 42700 | pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 42521 | 42701 | if( pShmNode->pShmMutex==0 ){ |
| 42522 | 42702 | rc = SQLITE_NOMEM_BKPT; |
| 42523 | 42703 | goto shm_open_err; |
| 42524 | 42704 | } |
| 42705 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42706 | + { |
| 42707 | + int ii; |
| 42708 | + for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){ |
| 42709 | + pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 42710 | + if( pShmNode->aMutex[ii]==0 ){ |
| 42711 | + rc = SQLITE_NOMEM_BKPT; |
| 42712 | + goto shm_open_err; |
| 42713 | + } |
| 42714 | + } |
| 42715 | + } |
| 42716 | +#endif |
| 42525 | 42717 | } |
| 42526 | 42718 | |
| 42527 | 42719 | if( pInode->bProcessLock==0 ){ |
| 42528 | 42720 | if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
| 42529 | 42721 | pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW, |
| | @@ -42741,13 +42933,15 @@ |
| 42741 | 42933 | ** |
| 42742 | 42934 | ** assert( assertLockingArrayOk(pShmNode) ); |
| 42743 | 42935 | */ |
| 42744 | 42936 | #ifdef SQLITE_DEBUG |
| 42745 | 42937 | static int assertLockingArrayOk(unixShmNode *pShmNode){ |
| 42938 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42939 | + return 1; |
| 42940 | +#else |
| 42746 | 42941 | unixShm *pX; |
| 42747 | 42942 | int aLock[SQLITE_SHM_NLOCK]; |
| 42748 | | - assert( sqlite3_mutex_held(pShmNode->pShmMutex) ); |
| 42749 | 42943 | |
| 42750 | 42944 | memset(aLock, 0, sizeof(aLock)); |
| 42751 | 42945 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 42752 | 42946 | int i; |
| 42753 | 42947 | for(i=0; i<SQLITE_SHM_NLOCK; i++){ |
| | @@ -42761,17 +42955,18 @@ |
| 42761 | 42955 | } |
| 42762 | 42956 | } |
| 42763 | 42957 | |
| 42764 | 42958 | assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) ); |
| 42765 | 42959 | return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0); |
| 42960 | +#endif |
| 42766 | 42961 | } |
| 42767 | 42962 | #endif |
| 42768 | 42963 | |
| 42769 | 42964 | /* |
| 42770 | 42965 | ** Change the lock state for a shared-memory segment. |
| 42771 | 42966 | ** |
| 42772 | | -** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 42967 | +** Note that the relationship between SHARED and EXCLUSIVE locks is a little |
| 42773 | 42968 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 42774 | 42969 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 42775 | 42970 | ** not go from shared to exclusive or from exclusive to shared. |
| 42776 | 42971 | */ |
| 42777 | 42972 | static int unixShmLock( |
| | @@ -42782,11 +42977,11 @@ |
| 42782 | 42977 | ){ |
| 42783 | 42978 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 42784 | 42979 | unixShm *p; /* The shared memory being locked */ |
| 42785 | 42980 | unixShmNode *pShmNode; /* The underlying file iNode */ |
| 42786 | 42981 | int rc = SQLITE_OK; /* Result code */ |
| 42787 | | - u16 mask; /* Mask of locks to take or release */ |
| 42982 | + u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */ |
| 42788 | 42983 | int *aLock; |
| 42789 | 42984 | |
| 42790 | 42985 | p = pDbFd->pShm; |
| 42791 | 42986 | if( p==0 ) return SQLITE_IOERR_SHMLOCK; |
| 42792 | 42987 | pShmNode = p->pShmNode; |
| | @@ -42817,92 +43012,155 @@ |
| 42817 | 43012 | ** held. |
| 42818 | 43013 | ** |
| 42819 | 43014 | ** It is not permitted to block on the RECOVER lock. |
| 42820 | 43015 | */ |
| 42821 | 43016 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 42822 | | - assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 42823 | | - (ofst!=2) /* not RECOVER */ |
| 42824 | | - && (ofst!=1 || (p->exclMask|p->sharedMask)==0) |
| 42825 | | - && (ofst!=0 || (p->exclMask|p->sharedMask)<3) |
| 42826 | | - && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst)) |
| 42827 | | - )); |
| 42828 | | -#endif |
| 42829 | | - |
| 42830 | | - mask = (1<<(ofst+n)) - (1<<ofst); |
| 42831 | | - assert( n>1 || mask==(1<<ofst) ); |
| 42832 | | - sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 42833 | | - assert( assertLockingArrayOk(pShmNode) ); |
| 42834 | | - if( flags & SQLITE_SHM_UNLOCK ){ |
| 42835 | | - if( (p->exclMask|p->sharedMask) & mask ){ |
| 42836 | | - int ii; |
| 42837 | | - int bUnlock = 1; |
| 42838 | | - |
| 42839 | | - for(ii=ofst; ii<ofst+n; ii++){ |
| 42840 | | - if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){ |
| 42841 | | - bUnlock = 0; |
| 42842 | | - } |
| 42843 | | - } |
| 42844 | | - |
| 42845 | | - if( bUnlock ){ |
| 42846 | | - rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 42847 | | - if( rc==SQLITE_OK ){ |
| 42848 | | - memset(&aLock[ofst], 0, sizeof(int)*n); |
| 42849 | | - } |
| 42850 | | - }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){ |
| 42851 | | - assert( n==1 && aLock[ofst]>1 ); |
| 42852 | | - aLock[ofst]--; |
| 42853 | | - } |
| 42854 | | - |
| 42855 | | - /* Undo the local locks */ |
| 42856 | | - if( rc==SQLITE_OK ){ |
| 42857 | | - p->exclMask &= ~mask; |
| 42858 | | - p->sharedMask &= ~mask; |
| 42859 | | - } |
| 42860 | | - } |
| 42861 | | - }else if( flags & SQLITE_SHM_SHARED ){ |
| 42862 | | - assert( n==1 ); |
| 42863 | | - assert( (p->exclMask & (1<<ofst))==0 ); |
| 42864 | | - if( (p->sharedMask & mask)==0 ){ |
| 42865 | | - if( aLock[ofst]<0 ){ |
| 42866 | | - rc = SQLITE_BUSY; |
| 42867 | | - }else if( aLock[ofst]==0 ){ |
| 42868 | | - rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 42869 | | - } |
| 42870 | | - |
| 42871 | | - /* Get the local shared locks */ |
| 42872 | | - if( rc==SQLITE_OK ){ |
| 42873 | | - p->sharedMask |= mask; |
| 42874 | | - aLock[ofst]++; |
| 42875 | | - } |
| 42876 | | - } |
| 42877 | | - }else{ |
| 42878 | | - /* Make sure no sibling connections hold locks that will block this |
| 42879 | | - ** lock. If any do, return SQLITE_BUSY right away. */ |
| 42880 | | - int ii; |
| 42881 | | - for(ii=ofst; ii<ofst+n; ii++){ |
| 42882 | | - assert( (p->sharedMask & mask)==0 ); |
| 42883 | | - if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){ |
| 42884 | | - rc = SQLITE_BUSY; |
| 42885 | | - break; |
| 42886 | | - } |
| 42887 | | - } |
| 42888 | | - |
| 42889 | | - /* Get the exclusive locks at the system level. Then if successful |
| 42890 | | - ** also update the in-memory values. */ |
| 42891 | | - if( rc==SQLITE_OK ){ |
| 42892 | | - rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 42893 | | - if( rc==SQLITE_OK ){ |
| 42894 | | - assert( (p->sharedMask & mask)==0 ); |
| 42895 | | - p->exclMask |= mask; |
| 42896 | | - for(ii=ofst; ii<ofst+n; ii++){ |
| 42897 | | - aLock[ii] = -1; |
| 42898 | | - } |
| 42899 | | - } |
| 42900 | | - } |
| 42901 | | - } |
| 42902 | | - assert( assertLockingArrayOk(pShmNode) ); |
| 42903 | | - sqlite3_mutex_leave(pShmNode->pShmMutex); |
| 43017 | + { |
| 43018 | + u16 lockMask = (p->exclMask|p->sharedMask); |
| 43019 | + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 43020 | + (ofst!=2) /* not RECOVER */ |
| 43021 | + && (ofst!=1 || lockMask==0 || lockMask==2) |
| 43022 | + && (ofst!=0 || lockMask<3) |
| 43023 | + && (ofst<3 || lockMask<(1<<ofst)) |
| 43024 | + )); |
| 43025 | + } |
| 43026 | +#endif |
| 43027 | + |
| 43028 | + /* Check if there is any work to do. There are three cases: |
| 43029 | + ** |
| 43030 | + ** a) An unlock operation where there are locks to unlock, |
| 43031 | + ** b) An shared lock where the requested lock is not already held |
| 43032 | + ** c) An exclusive lock where the requested lock is not already held |
| 43033 | + ** |
| 43034 | + ** The SQLite core never requests an exclusive lock that it already holds. |
| 43035 | + ** This is assert()ed below. |
| 43036 | + */ |
| 43037 | + assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK) |
| 43038 | + || 0==(p->exclMask & mask) |
| 43039 | + ); |
| 43040 | + if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask)) |
| 43041 | + || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask)) |
| 43042 | + || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)) |
| 43043 | + ){ |
| 43044 | + |
| 43045 | + /* Take the required mutexes. In SETLK_TIMEOUT mode (blocking locks), if |
| 43046 | + ** this is an attempt on an exclusive lock use sqlite3_mutex_try(). If any |
| 43047 | + ** other thread is holding this mutex, then it is either holding or about |
| 43048 | + ** to hold a lock exclusive to the one being requested, and we may |
| 43049 | + ** therefore return SQLITE_BUSY to the caller. |
| 43050 | + ** |
| 43051 | + ** Doing this prevents some deadlock scenarios. For example, thread 1 may |
| 43052 | + ** be a checkpointer blocked waiting on the WRITER lock. And thread 2 |
| 43053 | + ** may be a normal SQL client upgrading to a write transaction. In this |
| 43054 | + ** case thread 2 does a non-blocking request for the WRITER lock. But - |
| 43055 | + ** if it were to use sqlite3_mutex_enter() then it would effectively |
| 43056 | + ** become a (doomed) blocking request, as thread 2 would block until thread |
| 43057 | + ** 1 obtained WRITER and released the mutex. Since thread 2 already holds |
| 43058 | + ** a lock on a read-locking slot at this point, this breaks the |
| 43059 | + ** anti-deadlock rules (see above). */ |
| 43060 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 43061 | + int iMutex; |
| 43062 | + for(iMutex=ofst; iMutex<ofst+n; iMutex++){ |
| 43063 | + if( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) ){ |
| 43064 | + rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]); |
| 43065 | + if( rc!=SQLITE_OK ) goto leave_shmnode_mutexes; |
| 43066 | + }else{ |
| 43067 | + sqlite3_mutex_enter(pShmNode->aMutex[iMutex]); |
| 43068 | + } |
| 43069 | + } |
| 43070 | +#else |
| 43071 | + sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 43072 | +#endif |
| 43073 | + |
| 43074 | + if( ALWAYS(rc==SQLITE_OK) ){ |
| 43075 | + if( flags & SQLITE_SHM_UNLOCK ){ |
| 43076 | + /* Case (a) - unlock. */ |
| 43077 | + int bUnlock = 1; |
| 43078 | + assert( (p->exclMask & p->sharedMask)==0 ); |
| 43079 | + assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask ); |
| 43080 | + assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask ); |
| 43081 | + |
| 43082 | + /* If this is a SHARED lock being unlocked, it is possible that other |
| 43083 | + ** clients within this process are holding the same SHARED lock. In |
| 43084 | + ** this case, set bUnlock to 0 so that the posix lock is not removed |
| 43085 | + ** from the file-descriptor below. */ |
| 43086 | + if( flags & SQLITE_SHM_SHARED ){ |
| 43087 | + assert( n==1 ); |
| 43088 | + assert( aLock[ofst]>=1 ); |
| 43089 | + if( aLock[ofst]>1 ){ |
| 43090 | + bUnlock = 0; |
| 43091 | + aLock[ofst]--; |
| 43092 | + p->sharedMask &= ~mask; |
| 43093 | + } |
| 43094 | + } |
| 43095 | + |
| 43096 | + if( bUnlock ){ |
| 43097 | + rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 43098 | + if( rc==SQLITE_OK ){ |
| 43099 | + memset(&aLock[ofst], 0, sizeof(int)*n); |
| 43100 | + p->sharedMask &= ~mask; |
| 43101 | + p->exclMask &= ~mask; |
| 43102 | + } |
| 43103 | + } |
| 43104 | + }else if( flags & SQLITE_SHM_SHARED ){ |
| 43105 | + /* Case (b) - a shared lock. */ |
| 43106 | + |
| 43107 | + if( aLock[ofst]<0 ){ |
| 43108 | + /* An exclusive lock is held by some other connection. BUSY. */ |
| 43109 | + rc = SQLITE_BUSY; |
| 43110 | + }else if( aLock[ofst]==0 ){ |
| 43111 | + rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 43112 | + } |
| 43113 | + |
| 43114 | + /* Get the local shared locks */ |
| 43115 | + if( rc==SQLITE_OK ){ |
| 43116 | + p->sharedMask |= mask; |
| 43117 | + aLock[ofst]++; |
| 43118 | + } |
| 43119 | + }else{ |
| 43120 | + /* Case (c) - an exclusive lock. */ |
| 43121 | + int ii; |
| 43122 | + |
| 43123 | + assert( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) ); |
| 43124 | + assert( (p->sharedMask & mask)==0 ); |
| 43125 | + assert( (p->exclMask & mask)==0 ); |
| 43126 | + |
| 43127 | + /* Make sure no sibling connections hold locks that will block this |
| 43128 | + ** lock. If any do, return SQLITE_BUSY right away. */ |
| 43129 | + for(ii=ofst; ii<ofst+n; ii++){ |
| 43130 | + if( aLock[ii] ){ |
| 43131 | + rc = SQLITE_BUSY; |
| 43132 | + break; |
| 43133 | + } |
| 43134 | + } |
| 43135 | + |
| 43136 | + /* Get the exclusive locks at the system level. Then if successful |
| 43137 | + ** also update the in-memory values. */ |
| 43138 | + if( rc==SQLITE_OK ){ |
| 43139 | + rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 43140 | + if( rc==SQLITE_OK ){ |
| 43141 | + p->exclMask |= mask; |
| 43142 | + for(ii=ofst; ii<ofst+n; ii++){ |
| 43143 | + aLock[ii] = -1; |
| 43144 | + } |
| 43145 | + } |
| 43146 | + } |
| 43147 | + } |
| 43148 | + assert( assertLockingArrayOk(pShmNode) ); |
| 43149 | + } |
| 43150 | + |
| 43151 | + /* Drop the mutexes acquired above. */ |
| 43152 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 43153 | + leave_shmnode_mutexes: |
| 43154 | + for(iMutex--; iMutex>=ofst; iMutex--){ |
| 43155 | + sqlite3_mutex_leave(pShmNode->aMutex[iMutex]); |
| 43156 | + } |
| 43157 | +#else |
| 43158 | + sqlite3_mutex_leave(pShmNode->pShmMutex); |
| 43159 | +#endif |
| 43160 | + } |
| 43161 | + |
| 42904 | 43162 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 42905 | 43163 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
| 42906 | 43164 | return rc; |
| 42907 | 43165 | } |
| 42908 | 43166 | |
| | @@ -43148,15 +43406,20 @@ |
| 43148 | 43406 | #endif |
| 43149 | 43407 | *pp = 0; |
| 43150 | 43408 | |
| 43151 | 43409 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 43152 | 43410 | if( pFd->mmapSizeMax>0 ){ |
| 43411 | + /* Ensure that there is always at least a 256 byte buffer of addressable |
| 43412 | + ** memory following the returned page. If the database is corrupt, |
| 43413 | + ** SQLite may overread the page slightly (in practice only a few bytes, |
| 43414 | + ** but 256 is safe, round, number). */ |
| 43415 | + const int nEofBuffer = 256; |
| 43153 | 43416 | if( pFd->pMapRegion==0 ){ |
| 43154 | 43417 | int rc = unixMapfile(pFd, -1); |
| 43155 | 43418 | if( rc!=SQLITE_OK ) return rc; |
| 43156 | 43419 | } |
| 43157 | | - if( pFd->mmapSize >= iOff+nAmt ){ |
| 43420 | + if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ |
| 43158 | 43421 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 43159 | 43422 | pFd->nFetchOut++; |
| 43160 | 43423 | } |
| 43161 | 43424 | } |
| 43162 | 43425 | #endif |
| | @@ -50505,19 +50768,24 @@ |
| 50505 | 50768 | OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n", |
| 50506 | 50769 | osGetCurrentProcessId(), fd, iOff, nAmt, pp)); |
| 50507 | 50770 | |
| 50508 | 50771 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 50509 | 50772 | if( pFd->mmapSizeMax>0 ){ |
| 50773 | + /* Ensure that there is always at least a 256 byte buffer of addressable |
| 50774 | + ** memory following the returned page. If the database is corrupt, |
| 50775 | + ** SQLite may overread the page slightly (in practice only a few bytes, |
| 50776 | + ** but 256 is safe, round, number). */ |
| 50777 | + const int nEofBuffer = 256; |
| 50510 | 50778 | if( pFd->pMapRegion==0 ){ |
| 50511 | 50779 | int rc = winMapfile(pFd, -1); |
| 50512 | 50780 | if( rc!=SQLITE_OK ){ |
| 50513 | 50781 | OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n", |
| 50514 | 50782 | osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
| 50515 | 50783 | return rc; |
| 50516 | 50784 | } |
| 50517 | 50785 | } |
| 50518 | | - if( pFd->mmapSize >= iOff+nAmt ){ |
| 50786 | + if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ |
| 50519 | 50787 | assert( pFd->pMapRegion!=0 ); |
| 50520 | 50788 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 50521 | 50789 | pFd->nFetchOut++; |
| 50522 | 50790 | } |
| 50523 | 50791 | } |
| | @@ -57116,11 +57384,11 @@ |
| 57116 | 57384 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 57117 | 57385 | char *zFilename; /* Name of the database file */ |
| 57118 | 57386 | char *zJournal; /* Name of the journal file */ |
| 57119 | 57387 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 57120 | 57388 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
| 57121 | | - int aStat[4]; /* Total cache hits, misses, writes, spills */ |
| 57389 | + u32 aStat[4]; /* Total cache hits, misses, writes, spills */ |
| 57122 | 57390 | #ifdef SQLITE_TEST |
| 57123 | 57391 | int nRead; /* Database pages read */ |
| 57124 | 57392 | #endif |
| 57125 | 57393 | void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ |
| 57126 | 57394 | int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */ |
| | @@ -57246,13 +57514,12 @@ |
| 57246 | 57514 | if( pPager->fd->pMethods==0 ) return 0; |
| 57247 | 57515 | if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; |
| 57248 | 57516 | #ifndef SQLITE_OMIT_WAL |
| 57249 | 57517 | if( pPager->pWal ){ |
| 57250 | 57518 | u32 iRead = 0; |
| 57251 | | - int rc; |
| 57252 | | - rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 57253 | | - return (rc==SQLITE_OK && iRead==0); |
| 57519 | + (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 57520 | + return iRead==0; |
| 57254 | 57521 | } |
| 57255 | 57522 | #endif |
| 57256 | 57523 | return 1; |
| 57257 | 57524 | } |
| 57258 | 57525 | #endif |
| | @@ -63260,15 +63527,15 @@ |
| 63260 | 63527 | a[1] = sqlite3PcachePagecount(pPager->pPCache); |
| 63261 | 63528 | a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); |
| 63262 | 63529 | a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize; |
| 63263 | 63530 | a[4] = pPager->eState; |
| 63264 | 63531 | a[5] = pPager->errCode; |
| 63265 | | - a[6] = pPager->aStat[PAGER_STAT_HIT]; |
| 63266 | | - a[7] = pPager->aStat[PAGER_STAT_MISS]; |
| 63532 | + a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff; |
| 63533 | + a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff; |
| 63267 | 63534 | a[8] = 0; /* Used to be pPager->nOvfl */ |
| 63268 | 63535 | a[9] = pPager->nRead; |
| 63269 | | - a[10] = pPager->aStat[PAGER_STAT_WRITE]; |
| 63536 | + a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff; |
| 63270 | 63537 | return a; |
| 63271 | 63538 | } |
| 63272 | 63539 | #endif |
| 63273 | 63540 | |
| 63274 | 63541 | /* |
| | @@ -63280,11 +63547,11 @@ |
| 63280 | 63547 | ** Before returning, *pnVal is incremented by the |
| 63281 | 63548 | ** current cache hit or miss count, according to the value of eStat. If the |
| 63282 | 63549 | ** reset parameter is non-zero, the cache hit or miss count is zeroed before |
| 63283 | 63550 | ** returning. |
| 63284 | 63551 | */ |
| 63285 | | -SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){ |
| 63552 | +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){ |
| 63286 | 63553 | |
| 63287 | 63554 | assert( eStat==SQLITE_DBSTATUS_CACHE_HIT |
| 63288 | 63555 | || eStat==SQLITE_DBSTATUS_CACHE_MISS |
| 63289 | 63556 | || eStat==SQLITE_DBSTATUS_CACHE_WRITE |
| 63290 | 63557 | || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1 |
| | @@ -64220,11 +64487,11 @@ |
| 64220 | 64487 | assert( pPager->eState>=PAGER_READER ); |
| 64221 | 64488 | return sqlite3WalFramesize(pPager->pWal); |
| 64222 | 64489 | } |
| 64223 | 64490 | #endif |
| 64224 | 64491 | |
| 64225 | | -#ifdef SQLITE_USE_SEH |
| 64492 | +#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) |
| 64226 | 64493 | SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){ |
| 64227 | 64494 | return sqlite3WalSystemErrno(pPager->pWal); |
| 64228 | 64495 | } |
| 64229 | 64496 | #endif |
| 64230 | 64497 | |
| | @@ -66236,10 +66503,23 @@ |
| 66236 | 66503 | *pp = p; |
| 66237 | 66504 | return rc; |
| 66238 | 66505 | } |
| 66239 | 66506 | |
| 66240 | 66507 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 66508 | + |
| 66509 | + |
| 66510 | +/* |
| 66511 | +** Attempt to enable blocking locks that block for nMs ms. Return 1 if |
| 66512 | +** blocking locks are successfully enabled, or 0 otherwise. |
| 66513 | +*/ |
| 66514 | +static int walEnableBlockingMs(Wal *pWal, int nMs){ |
| 66515 | + int rc = sqlite3OsFileControl( |
| 66516 | + pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&nMs |
| 66517 | + ); |
| 66518 | + return (rc==SQLITE_OK); |
| 66519 | +} |
| 66520 | + |
| 66241 | 66521 | /* |
| 66242 | 66522 | ** Attempt to enable blocking locks. Blocking locks are enabled only if (a) |
| 66243 | 66523 | ** they are supported by the VFS, and (b) the database handle is configured |
| 66244 | 66524 | ** with a busy-timeout. Return 1 if blocking locks are successfully enabled, |
| 66245 | 66525 | ** or 0 otherwise. |
| | @@ -66247,15 +66527,11 @@ |
| 66247 | 66527 | static int walEnableBlocking(Wal *pWal){ |
| 66248 | 66528 | int res = 0; |
| 66249 | 66529 | if( pWal->db ){ |
| 66250 | 66530 | int tmout = pWal->db->busyTimeout; |
| 66251 | 66531 | if( tmout ){ |
| 66252 | | - int rc; |
| 66253 | | - rc = sqlite3OsFileControl( |
| 66254 | | - pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout |
| 66255 | | - ); |
| 66256 | | - res = (rc==SQLITE_OK); |
| 66532 | + res = walEnableBlockingMs(pWal, tmout); |
| 66257 | 66533 | } |
| 66258 | 66534 | } |
| 66259 | 66535 | return res; |
| 66260 | 66536 | } |
| 66261 | 66537 | |
| | @@ -66300,24 +66576,14 @@ |
| 66300 | 66576 | */ |
| 66301 | 66577 | SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){ |
| 66302 | 66578 | pWal->db = db; |
| 66303 | 66579 | } |
| 66304 | 66580 | |
| 66305 | | -/* |
| 66306 | | -** Take an exclusive WRITE lock. Blocking if so configured. |
| 66307 | | -*/ |
| 66308 | | -static int walLockWriter(Wal *pWal){ |
| 66309 | | - int rc; |
| 66310 | | - walEnableBlocking(pWal); |
| 66311 | | - rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 66312 | | - walDisableBlocking(pWal); |
| 66313 | | - return rc; |
| 66314 | | -} |
| 66315 | 66581 | #else |
| 66316 | 66582 | # define walEnableBlocking(x) 0 |
| 66317 | 66583 | # define walDisableBlocking(x) |
| 66318 | | -# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1) |
| 66584 | +# define walEnableBlockingMs(pWal, ms) 0 |
| 66319 | 66585 | # define sqlite3WalDb(pWal, db) |
| 66320 | 66586 | #endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 66321 | 66587 | |
| 66322 | 66588 | |
| 66323 | 66589 | /* |
| | @@ -66914,19 +67180,22 @@ |
| 66914 | 67180 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 66915 | 67181 | rc = SQLITE_READONLY_RECOVERY; |
| 66916 | 67182 | } |
| 66917 | 67183 | }else{ |
| 66918 | 67184 | int bWriteLock = pWal->writeLock; |
| 66919 | | - if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){ |
| 67185 | + if( bWriteLock |
| 67186 | + || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) |
| 67187 | + ){ |
| 66920 | 67188 | pWal->writeLock = 1; |
| 66921 | 67189 | if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 66922 | 67190 | badHdr = walIndexTryHdr(pWal, pChanged); |
| 66923 | 67191 | if( badHdr ){ |
| 66924 | 67192 | /* If the wal-index header is still malformed even while holding |
| 66925 | 67193 | ** a WRITE lock, it can only mean that the header is corrupted and |
| 66926 | 67194 | ** needs to be reconstructed. So run recovery to do exactly that. |
| 66927 | | - */ |
| 67195 | + ** Disable blocking locks first. */ |
| 67196 | + walDisableBlocking(pWal); |
| 66928 | 67197 | rc = walIndexRecover(pWal); |
| 66929 | 67198 | *pChanged = 1; |
| 66930 | 67199 | } |
| 66931 | 67200 | } |
| 66932 | 67201 | if( bWriteLock==0 ){ |
| | @@ -67132,10 +67401,41 @@ |
| 67132 | 67401 | *pChanged = 1; |
| 67133 | 67402 | } |
| 67134 | 67403 | return rc; |
| 67135 | 67404 | } |
| 67136 | 67405 | |
| 67406 | +/* |
| 67407 | +** The final argument passed to walTryBeginRead() is of type (int*). The |
| 67408 | +** caller should invoke walTryBeginRead as follows: |
| 67409 | +** |
| 67410 | +** int cnt = 0; |
| 67411 | +** do { |
| 67412 | +** rc = walTryBeginRead(..., &cnt); |
| 67413 | +** }while( rc==WAL_RETRY ); |
| 67414 | +** |
| 67415 | +** The final value of "cnt" is of no use to the caller. It is used by |
| 67416 | +** the implementation of walTryBeginRead() as follows: |
| 67417 | +** |
| 67418 | +** + Each time walTryBeginRead() is called, it is incremented. Once |
| 67419 | +** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead() |
| 67420 | +** has many times been invoked and failed with WAL_RETRY - walTryBeginRead() |
| 67421 | +** returns SQLITE_PROTOCOL. |
| 67422 | +** |
| 67423 | +** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed |
| 67424 | +** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS |
| 67425 | +** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case |
| 67426 | +** the next invocation of walTryBeginRead() may omit an expected call to |
| 67427 | +** sqlite3OsSleep(). There has already been a delay when the previous call |
| 67428 | +** waited on a lock. |
| 67429 | +*/ |
| 67430 | +#define WAL_RETRY_PROTOCOL_LIMIT 100 |
| 67431 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67432 | +# define WAL_RETRY_BLOCKED_MASK 0x10000000 |
| 67433 | +#else |
| 67434 | +# define WAL_RETRY_BLOCKED_MASK 0 |
| 67435 | +#endif |
| 67436 | + |
| 67137 | 67437 | /* |
| 67138 | 67438 | ** Attempt to start a read transaction. This might fail due to a race or |
| 67139 | 67439 | ** other transient condition. When that happens, it returns WAL_RETRY to |
| 67140 | 67440 | ** indicate to the caller that it is safe to retry immediately. |
| 67141 | 67441 | ** |
| | @@ -67182,17 +67482,20 @@ |
| 67182 | 67482 | ** checkpoint process do as much work as possible. This routine might |
| 67183 | 67483 | ** update values of the aReadMark[] array in the header, but if it does |
| 67184 | 67484 | ** so it takes care to hold an exclusive lock on the corresponding |
| 67185 | 67485 | ** WAL_READ_LOCK() while changing values. |
| 67186 | 67486 | */ |
| 67187 | | -static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ |
| 67487 | +static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ |
| 67188 | 67488 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 67189 | 67489 | u32 mxReadMark; /* Largest aReadMark[] value */ |
| 67190 | 67490 | int mxI; /* Index of largest aReadMark[] value */ |
| 67191 | 67491 | int i; /* Loop counter */ |
| 67192 | 67492 | int rc = SQLITE_OK; /* Return code */ |
| 67193 | 67493 | u32 mxFrame; /* Wal frame to lock to */ |
| 67494 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67495 | + int nBlockTmout = 0; |
| 67496 | +#endif |
| 67194 | 67497 | |
| 67195 | 67498 | assert( pWal->readLock<0 ); /* Not currently locked */ |
| 67196 | 67499 | |
| 67197 | 67500 | /* useWal may only be set for read/write connections */ |
| 67198 | 67501 | assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 ); |
| | @@ -67212,25 +67515,52 @@ |
| 67212 | 67515 | ** is more of a scheduler yield than an actual delay. But on the 10th |
| 67213 | 67516 | ** an subsequent retries, the delays start becoming longer and longer, |
| 67214 | 67517 | ** so that on the 100th (and last) RETRY we delay for 323 milliseconds. |
| 67215 | 67518 | ** The total delay time before giving up is less than 10 seconds. |
| 67216 | 67519 | */ |
| 67217 | | - if( cnt>5 ){ |
| 67520 | + (*pCnt)++; |
| 67521 | + if( *pCnt>5 ){ |
| 67218 | 67522 | int nDelay = 1; /* Pause time in microseconds */ |
| 67219 | | - if( cnt>100 ){ |
| 67523 | + int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK); |
| 67524 | + if( cnt>WAL_RETRY_PROTOCOL_LIMIT ){ |
| 67220 | 67525 | VVA_ONLY( pWal->lockError = 1; ) |
| 67221 | 67526 | return SQLITE_PROTOCOL; |
| 67222 | 67527 | } |
| 67223 | | - if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; |
| 67528 | + if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; |
| 67529 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67530 | + /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor |
| 67531 | + ** to block for locks for approximately nDelay us. This affects three |
| 67532 | + ** locks: (a) the shared lock taken on the DMS slot in os_unix.c (if |
| 67533 | + ** using os_unix.c), (b) the WRITER lock taken in walIndexReadHdr() if the |
| 67534 | + ** first attempted read fails, and (c) the shared lock taken on the |
| 67535 | + ** read-mark. |
| 67536 | + ** |
| 67537 | + ** If the previous call failed due to an SQLITE_BUSY_TIMEOUT error, |
| 67538 | + ** then sleep for the minimum of 1us. The previous call already provided |
| 67539 | + ** an extra delay while it was blocking on the lock. |
| 67540 | + */ |
| 67541 | + nBlockTmout = (nDelay+998) / 1000; |
| 67542 | + if( !useWal && walEnableBlockingMs(pWal, nBlockTmout) ){ |
| 67543 | + if( *pCnt & WAL_RETRY_BLOCKED_MASK ) nDelay = 1; |
| 67544 | + } |
| 67545 | +#endif |
| 67224 | 67546 | sqlite3OsSleep(pWal->pVfs, nDelay); |
| 67547 | + *pCnt &= ~WAL_RETRY_BLOCKED_MASK; |
| 67225 | 67548 | } |
| 67226 | 67549 | |
| 67227 | 67550 | if( !useWal ){ |
| 67228 | 67551 | assert( rc==SQLITE_OK ); |
| 67229 | 67552 | if( pWal->bShmUnreliable==0 ){ |
| 67230 | 67553 | rc = walIndexReadHdr(pWal, pChanged); |
| 67231 | 67554 | } |
| 67555 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67556 | + walDisableBlocking(pWal); |
| 67557 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 67558 | + rc = SQLITE_BUSY; |
| 67559 | + *pCnt |= WAL_RETRY_BLOCKED_MASK; |
| 67560 | + } |
| 67561 | +#endif |
| 67232 | 67562 | if( rc==SQLITE_BUSY ){ |
| 67233 | 67563 | /* If there is not a recovery running in another thread or process |
| 67234 | 67564 | ** then convert BUSY errors to WAL_RETRY. If recovery is known to |
| 67235 | 67565 | ** be running, convert BUSY to BUSY_RECOVERY. There is a race here |
| 67236 | 67566 | ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY |
| | @@ -67341,13 +67671,23 @@ |
| 67341 | 67671 | if( mxI==0 ){ |
| 67342 | 67672 | assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); |
| 67343 | 67673 | return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT; |
| 67344 | 67674 | } |
| 67345 | 67675 | |
| 67676 | + (void)walEnableBlockingMs(pWal, nBlockTmout); |
| 67346 | 67677 | rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 67678 | + walDisableBlocking(pWal); |
| 67347 | 67679 | if( rc ){ |
| 67348 | | - return rc==SQLITE_BUSY ? WAL_RETRY : rc; |
| 67680 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67681 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 67682 | + *pCnt |= WAL_RETRY_BLOCKED_MASK; |
| 67683 | + } |
| 67684 | +#else |
| 67685 | + assert( rc!=SQLITE_BUSY_TIMEOUT ); |
| 67686 | +#endif |
| 67687 | + assert( (rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT ); |
| 67688 | + return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc; |
| 67349 | 67689 | } |
| 67350 | 67690 | /* Now that the read-lock has been obtained, check that neither the |
| 67351 | 67691 | ** value in the aReadMark[] array or the contents of the wal-index |
| 67352 | 67692 | ** header have changed. |
| 67353 | 67693 | ** |
| | @@ -67531,11 +67871,11 @@ |
| 67531 | 67871 | ckptLock = 1; |
| 67532 | 67872 | } |
| 67533 | 67873 | #endif |
| 67534 | 67874 | |
| 67535 | 67875 | do{ |
| 67536 | | - rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 67876 | + rc = walTryBeginRead(pWal, pChanged, 0, &cnt); |
| 67537 | 67877 | }while( rc==WAL_RETRY ); |
| 67538 | 67878 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 67539 | 67879 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 67540 | 67880 | testcase( rc==SQLITE_PROTOCOL ); |
| 67541 | 67881 | testcase( rc==SQLITE_OK ); |
| | @@ -67712,10 +68052,11 @@ |
| 67712 | 68052 | if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){ |
| 67713 | 68053 | assert( iFrame>iRead || CORRUPT_DB ); |
| 67714 | 68054 | iRead = iFrame; |
| 67715 | 68055 | } |
| 67716 | 68056 | if( (nCollide--)==0 ){ |
| 68057 | + *piRead = 0; |
| 67717 | 68058 | return SQLITE_CORRUPT_BKPT; |
| 67718 | 68059 | } |
| 67719 | 68060 | iKey = walNextHash(iKey); |
| 67720 | 68061 | } |
| 67721 | 68062 | if( iRead ) break; |
| | @@ -68015,11 +68356,11 @@ |
| 68015 | 68356 | walUnlockShared(pWal, WAL_READ_LOCK(0)); |
| 68016 | 68357 | pWal->readLock = -1; |
| 68017 | 68358 | cnt = 0; |
| 68018 | 68359 | do{ |
| 68019 | 68360 | int notUsed; |
| 68020 | | - rc = walTryBeginRead(pWal, ¬Used, 1, ++cnt); |
| 68361 | + rc = walTryBeginRead(pWal, ¬Used, 1, &cnt); |
| 68021 | 68362 | }while( rc==WAL_RETRY ); |
| 68022 | 68363 | assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */ |
| 68023 | 68364 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 68024 | 68365 | testcase( rc==SQLITE_PROTOCOL ); |
| 68025 | 68366 | testcase( rc==SQLITE_OK ); |
| | @@ -68436,14 +68777,13 @@ |
| 68436 | 68777 | assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); |
| 68437 | 68778 | |
| 68438 | 68779 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 68439 | 68780 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 68440 | 68781 | |
| 68441 | | - /* Enable blocking locks, if possible. If blocking locks are successfully |
| 68442 | | - ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */ |
| 68782 | + /* Enable blocking locks, if possible. */ |
| 68443 | 68783 | sqlite3WalDb(pWal, db); |
| 68444 | | - (void)walEnableBlocking(pWal); |
| 68784 | + if( xBusy2 ) (void)walEnableBlocking(pWal); |
| 68445 | 68785 | |
| 68446 | 68786 | /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 68447 | 68787 | ** "checkpoint" lock on the database file. |
| 68448 | 68788 | ** EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 68449 | 68789 | ** checkpoint operation at the same time, the lock cannot be obtained and |
| | @@ -68480,13 +68820,18 @@ |
| 68480 | 68820 | |
| 68481 | 68821 | |
| 68482 | 68822 | /* Read the wal-index header. */ |
| 68483 | 68823 | SEH_TRY { |
| 68484 | 68824 | if( rc==SQLITE_OK ){ |
| 68825 | + /* For a passive checkpoint, do not re-enable blocking locks after |
| 68826 | + ** reading the wal-index header. A passive checkpoint should not block |
| 68827 | + ** or invoke the busy handler. The only lock such a checkpoint may |
| 68828 | + ** attempt to obtain is a lock on a read-slot, and it should give up |
| 68829 | + ** immediately and do a partial checkpoint if it cannot obtain it. */ |
| 68485 | 68830 | walDisableBlocking(pWal); |
| 68486 | 68831 | rc = walIndexReadHdr(pWal, &isChanged); |
| 68487 | | - (void)walEnableBlocking(pWal); |
| 68832 | + if( eMode2!=SQLITE_CHECKPOINT_PASSIVE ) (void)walEnableBlocking(pWal); |
| 68488 | 68833 | if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ |
| 68489 | 68834 | sqlite3OsUnfetch(pWal->pDbFd, 0, 0); |
| 68490 | 68835 | } |
| 68491 | 68836 | } |
| 68492 | 68837 | |
| | @@ -68819,11 +69164,11 @@ |
| 68819 | 69164 | ** 20 1 Bytes of unused space at the end of each page |
| 68820 | 69165 | ** 21 1 Max embedded payload fraction (must be 64) |
| 68821 | 69166 | ** 22 1 Min embedded payload fraction (must be 32) |
| 68822 | 69167 | ** 23 1 Min leaf payload fraction (must be 32) |
| 68823 | 69168 | ** 24 4 File change counter |
| 68824 | | -** 28 4 Reserved for future use |
| 69169 | +** 28 4 The size of the database in pages |
| 68825 | 69170 | ** 32 4 First freelist page |
| 68826 | 69171 | ** 36 4 Number of freelist pages in the file |
| 68827 | 69172 | ** 40 60 15 4-byte meta values passed to higher layers |
| 68828 | 69173 | ** |
| 68829 | 69174 | ** 40 4 Schema cookie |
| | @@ -74946,11 +75291,10 @@ |
| 74946 | 75291 | u8 aSave[4]; |
| 74947 | 75292 | u8 *aWrite = &pBuf[-4]; |
| 74948 | 75293 | assert( aWrite>=pBufStart ); /* due to (6) */ |
| 74949 | 75294 | memcpy(aSave, aWrite, 4); |
| 74950 | 75295 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
| 74951 | | - if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT; |
| 74952 | 75296 | nextPage = get4byte(aWrite); |
| 74953 | 75297 | memcpy(aWrite, aSave, 4); |
| 74954 | 75298 | }else |
| 74955 | 75299 | #endif |
| 74956 | 75300 | |
| | @@ -76066,11 +76410,14 @@ |
| 76066 | 76410 | if( pCur->skipNext<0 ) return SQLITE_OK; |
| 76067 | 76411 | } |
| 76068 | 76412 | } |
| 76069 | 76413 | |
| 76070 | 76414 | pPage = pCur->pPage; |
| 76071 | | - assert( pPage->isInit ); |
| 76415 | + if( sqlite3FaultSim(412) ) pPage->isInit = 0; |
| 76416 | + if( !pPage->isInit ){ |
| 76417 | + return SQLITE_CORRUPT_BKPT; |
| 76418 | + } |
| 76072 | 76419 | if( !pPage->leaf ){ |
| 76073 | 76420 | int idx = pCur->ix; |
| 76074 | 76421 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
| 76075 | 76422 | if( rc ) return rc; |
| 76076 | 76423 | rc = moveToRightmost(pCur); |
| | @@ -85370,10 +85717,14 @@ |
| 85370 | 85717 | } |
| 85371 | 85718 | case P4_VTAB : { |
| 85372 | 85719 | if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); |
| 85373 | 85720 | break; |
| 85374 | 85721 | } |
| 85722 | + case P4_TABLEREF: { |
| 85723 | + if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4); |
| 85724 | + break; |
| 85725 | + } |
| 85375 | 85726 | } |
| 85376 | 85727 | } |
| 85377 | 85728 | |
| 85378 | 85729 | /* |
| 85379 | 85730 | ** Free the space allocated for aOp and any p4 values allocated for the |
| | @@ -85497,11 +85848,11 @@ |
| 85497 | 85848 | Op *pOp, |
| 85498 | 85849 | const char *zP4, |
| 85499 | 85850 | int n |
| 85500 | 85851 | ){ |
| 85501 | 85852 | if( pOp->p4type ){ |
| 85502 | | - freeP4(p->db, pOp->p4type, pOp->p4.p); |
| 85853 | + assert( pOp->p4type > P4_FREE_IF_LE ); |
| 85503 | 85854 | pOp->p4type = 0; |
| 85504 | 85855 | pOp->p4.p = 0; |
| 85505 | 85856 | } |
| 85506 | 85857 | if( n<0 ){ |
| 85507 | 85858 | sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n); |
| | @@ -89620,11 +89971,19 @@ |
| 89620 | 89971 | SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ |
| 89621 | 89972 | int i; |
| 89622 | 89973 | int rc = SQLITE_OK; |
| 89623 | 89974 | Vdbe *p = (Vdbe*)pStmt; |
| 89624 | 89975 | #if SQLITE_THREADSAFE |
| 89625 | | - sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; |
| 89976 | + sqlite3_mutex *mutex; |
| 89977 | +#endif |
| 89978 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 89979 | + if( pStmt==0 ){ |
| 89980 | + return SQLITE_MISUSE_BKPT; |
| 89981 | + } |
| 89982 | +#endif |
| 89983 | +#if SQLITE_THREADSAFE |
| 89984 | + mutex = p->db->mutex; |
| 89626 | 89985 | #endif |
| 89627 | 89986 | sqlite3_mutex_enter(mutex); |
| 89628 | 89987 | for(i=0; i<p->nVar; i++){ |
| 89629 | 89988 | sqlite3VdbeMemRelease(&p->aVar[i]); |
| 89630 | 89989 | p->aVar[i].flags = MEM_Null; |
| | @@ -90410,13 +90769,12 @@ |
| 90410 | 90769 | ** pointer to it. |
| 90411 | 90770 | */ |
| 90412 | 90771 | SQLITE_API void *sqlite3_user_data(sqlite3_context *p){ |
| 90413 | 90772 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 90414 | 90773 | if( p==0 ) return 0; |
| 90415 | | -#else |
| 90774 | +#endif |
| 90416 | 90775 | assert( p && p->pFunc ); |
| 90417 | | -#endif |
| 90418 | 90776 | return p->pFunc->pUserData; |
| 90419 | 90777 | } |
| 90420 | 90778 | |
| 90421 | 90779 | /* |
| 90422 | 90780 | ** Extract the user data from a sqlite3_context structure and return a |
| | @@ -94231,11 +94589,11 @@ |
| 94231 | 94589 | */ |
| 94232 | 94590 | case OP_AddImm: { /* in1 */ |
| 94233 | 94591 | pIn1 = &aMem[pOp->p1]; |
| 94234 | 94592 | memAboutToChange(p, pIn1); |
| 94235 | 94593 | sqlite3VdbeMemIntegerify(pIn1); |
| 94236 | | - pIn1->u.i += pOp->p2; |
| 94594 | + *(u64*)&pIn1->u.i += (u64)pOp->p2; |
| 94237 | 94595 | break; |
| 94238 | 94596 | } |
| 94239 | 94597 | |
| 94240 | 94598 | /* Opcode: MustBeInt P1 P2 * * * |
| 94241 | 94599 | ** |
| | @@ -100377,28 +100735,27 @@ |
| 100377 | 100735 | const sqlite3_module *pModule; |
| 100378 | 100736 | char *zErr = 0; |
| 100379 | 100737 | |
| 100380 | 100738 | pOut = &aMem[pOp->p2]; |
| 100381 | 100739 | sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */ |
| 100382 | | - assert( pOp->p4type==P4_TABLE ); |
| 100740 | + assert( pOp->p4type==P4_TABLEREF ); |
| 100383 | 100741 | pTab = pOp->p4.pTab; |
| 100384 | 100742 | assert( pTab!=0 ); |
| 100743 | + assert( pTab->nTabRef>0 ); |
| 100385 | 100744 | assert( IsVirtual(pTab) ); |
| 100386 | 100745 | if( pTab->u.vtab.p==0 ) break; |
| 100387 | 100746 | pVtab = pTab->u.vtab.p->pVtab; |
| 100388 | 100747 | assert( pVtab!=0 ); |
| 100389 | 100748 | pModule = pVtab->pModule; |
| 100390 | 100749 | assert( pModule!=0 ); |
| 100391 | 100750 | assert( pModule->iVersion>=4 ); |
| 100392 | 100751 | assert( pModule->xIntegrity!=0 ); |
| 100393 | | - pTab->nTabRef++; |
| 100394 | 100752 | sqlite3VtabLock(pTab->u.vtab.p); |
| 100395 | 100753 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 100396 | 100754 | rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName, |
| 100397 | 100755 | pOp->p3, &zErr); |
| 100398 | 100756 | sqlite3VtabUnlock(pTab->u.vtab.p); |
| 100399 | | - sqlite3DeleteTable(db, pTab); |
| 100400 | 100757 | if( rc ){ |
| 100401 | 100758 | sqlite3_free(zErr); |
| 100402 | 100759 | goto abort_due_to_error; |
| 100403 | 100760 | } |
| 100404 | 100761 | if( zErr ){ |
| | @@ -100519,10 +100876,11 @@ |
| 100519 | 100876 | case OP_VColumn: { /* ncycle */ |
| 100520 | 100877 | sqlite3_vtab *pVtab; |
| 100521 | 100878 | const sqlite3_module *pModule; |
| 100522 | 100879 | Mem *pDest; |
| 100523 | 100880 | sqlite3_context sContext; |
| 100881 | + FuncDef nullFunc; |
| 100524 | 100882 | |
| 100525 | 100883 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
| 100526 | 100884 | assert( pCur!=0 ); |
| 100527 | 100885 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 100528 | 100886 | pDest = &aMem[pOp->p3]; |
| | @@ -100536,10 +100894,13 @@ |
| 100536 | 100894 | pModule = pVtab->pModule; |
| 100537 | 100895 | assert( pModule->xColumn ); |
| 100538 | 100896 | memset(&sContext, 0, sizeof(sContext)); |
| 100539 | 100897 | sContext.pOut = pDest; |
| 100540 | 100898 | sContext.enc = encoding; |
| 100899 | + nullFunc.pUserData = 0; |
| 100900 | + nullFunc.funcFlags = SQLITE_RESULT_SUBTYPE; |
| 100901 | + sContext.pFunc = &nullFunc; |
| 100541 | 100902 | assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); |
| 100542 | 100903 | if( pOp->p5 & OPFLAG_NOCHNG ){ |
| 100543 | 100904 | sqlite3VdbeMemSetNull(pDest); |
| 100544 | 100905 | pDest->flags = MEM_Null|MEM_Zero; |
| 100545 | 100906 | pDest->u.nZero = 0; |
| | @@ -100867,10 +101228,46 @@ |
| 100867 | 101228 | case OP_ClrSubtype: { /* in1 */ |
| 100868 | 101229 | pIn1 = &aMem[pOp->p1]; |
| 100869 | 101230 | pIn1->flags &= ~MEM_Subtype; |
| 100870 | 101231 | break; |
| 100871 | 101232 | } |
| 101233 | + |
| 101234 | +/* Opcode: GetSubtype P1 P2 * * * |
| 101235 | +** Synopsis: r[P2] = r[P1].subtype |
| 101236 | +** |
| 101237 | +** Extract the subtype value from register P1 and write that subtype |
| 101238 | +** into register P2. If P1 has no subtype, then P1 gets a NULL. |
| 101239 | +*/ |
| 101240 | +case OP_GetSubtype: { /* in1 out2 */ |
| 101241 | + pIn1 = &aMem[pOp->p1]; |
| 101242 | + pOut = &aMem[pOp->p2]; |
| 101243 | + if( pIn1->flags & MEM_Subtype ){ |
| 101244 | + sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype); |
| 101245 | + }else{ |
| 101246 | + sqlite3VdbeMemSetNull(pOut); |
| 101247 | + } |
| 101248 | + break; |
| 101249 | +} |
| 101250 | + |
| 101251 | +/* Opcode: SetSubtype P1 P2 * * * |
| 101252 | +** Synopsis: r[P2].subtype = r[P1] |
| 101253 | +** |
| 101254 | +** Set the subtype value of register P2 to the integer from register P1. |
| 101255 | +** If P1 is NULL, clear the subtype from p2. |
| 101256 | +*/ |
| 101257 | +case OP_SetSubtype: { /* in1 out2 */ |
| 101258 | + pIn1 = &aMem[pOp->p1]; |
| 101259 | + pOut = &aMem[pOp->p2]; |
| 101260 | + if( pIn1->flags & MEM_Null ){ |
| 101261 | + pOut->flags &= ~MEM_Subtype; |
| 101262 | + }else{ |
| 101263 | + assert( pIn1->flags & MEM_Int ); |
| 101264 | + pOut->flags |= MEM_Subtype; |
| 101265 | + pOut->eSubtype = (u8)(pIn1->u.i & 0xff); |
| 101266 | + } |
| 101267 | + break; |
| 101268 | +} |
| 100872 | 101269 | |
| 100873 | 101270 | /* Opcode: FilterAdd P1 * P3 P4 * |
| 100874 | 101271 | ** Synopsis: filter(P1) += key(P3@P4) |
| 100875 | 101272 | ** |
| 100876 | 101273 | ** Compute a hash on the P4 registers starting with r[P3] and |
| | @@ -105916,10 +106313,11 @@ |
| 105916 | 106313 | |
| 105917 | 106314 | n = pExpr->iColumn; |
| 105918 | 106315 | assert( ExprUseYTab(pExpr) ); |
| 105919 | 106316 | pExTab = pExpr->y.pTab; |
| 105920 | 106317 | assert( pExTab!=0 ); |
| 106318 | + assert( n < pExTab->nCol ); |
| 105921 | 106319 | if( (pExTab->tabFlags & TF_HasGenerated)!=0 |
| 105922 | 106320 | && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0 |
| 105923 | 106321 | ){ |
| 105924 | 106322 | testcase( pExTab->nCol==BMS-1 ); |
| 105925 | 106323 | testcase( pExTab->nCol==BMS ); |
| | @@ -106492,10 +106890,11 @@ |
| 106492 | 106890 | sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); |
| 106493 | 106891 | } |
| 106494 | 106892 | sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
| 106495 | 106893 | pParse->checkSchema = 1; |
| 106496 | 106894 | pTopNC->nNcErr++; |
| 106895 | + eNewExprOp = TK_NULL; |
| 106497 | 106896 | } |
| 106498 | 106897 | assert( pFJMatch==0 ); |
| 106499 | 106898 | |
| 106500 | 106899 | /* Remove all substructure from pExpr */ |
| 106501 | 106900 | if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ |
| | @@ -106518,11 +106917,11 @@ |
| 106518 | 106917 | ** (See ticket [b92e5e8ec2cdbaa1]). |
| 106519 | 106918 | ** |
| 106520 | 106919 | ** If a generated column is referenced, set bits for every column |
| 106521 | 106920 | ** of the table. |
| 106522 | 106921 | */ |
| 106523 | | - if( pExpr->iColumn>=0 && pMatch!=0 ){ |
| 106922 | + if( pExpr->iColumn>=0 && cnt==1 && pMatch!=0 ){ |
| 106524 | 106923 | pMatch->colUsed |= sqlite3ExprColUsed(pExpr); |
| 106525 | 106924 | } |
| 106526 | 106925 | |
| 106527 | 106926 | pExpr->op = eNewExprOp; |
| 106528 | 106927 | lookupname_end: |
| | @@ -106983,15 +107382,16 @@ |
| 106983 | 107382 | #endif |
| 106984 | 107383 | pNC2 = pNC; |
| 106985 | 107384 | while( pNC2 |
| 106986 | 107385 | && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 |
| 106987 | 107386 | ){ |
| 106988 | | - pExpr->op2++; |
| 107387 | + pExpr->op2 += (1 + pNC2->nNestedSelect); |
| 106989 | 107388 | pNC2 = pNC2->pNext; |
| 106990 | 107389 | } |
| 106991 | 107390 | assert( pDef!=0 || IN_RENAME_OBJECT ); |
| 106992 | 107391 | if( pNC2 && pDef ){ |
| 107392 | + pExpr->op2 += pNC2->nNestedSelect; |
| 106993 | 107393 | assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); |
| 106994 | 107394 | assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg ); |
| 106995 | 107395 | testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); |
| 106996 | 107396 | testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 ); |
| 106997 | 107397 | pNC2->ncFlags |= NC_HasAgg |
| | @@ -107546,10 +107946,11 @@ |
| 107546 | 107946 | p->pOrderBy = 0; |
| 107547 | 107947 | } |
| 107548 | 107948 | |
| 107549 | 107949 | /* Recursively resolve names in all subqueries in the FROM clause |
| 107550 | 107950 | */ |
| 107951 | + if( pOuterNC ) pOuterNC->nNestedSelect++; |
| 107551 | 107952 | for(i=0; i<p->pSrc->nSrc; i++){ |
| 107552 | 107953 | SrcItem *pItem = &p->pSrc->a[i]; |
| 107553 | 107954 | if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){ |
| 107554 | 107955 | int nRef = pOuterNC ? pOuterNC->nRef : 0; |
| 107555 | 107956 | const char *zSavedContext = pParse->zAuthContext; |
| | @@ -107569,10 +107970,13 @@ |
| 107569 | 107970 | if( pOuterNC ){ |
| 107570 | 107971 | assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef ); |
| 107571 | 107972 | pItem->fg.isCorrelated = (pOuterNC->nRef>nRef); |
| 107572 | 107973 | } |
| 107573 | 107974 | } |
| 107975 | + } |
| 107976 | + if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0) ){ |
| 107977 | + pOuterNC->nNestedSelect--; |
| 107574 | 107978 | } |
| 107575 | 107979 | |
| 107576 | 107980 | /* Set up the local name-context to pass to sqlite3ResolveExprNames() to |
| 107577 | 107981 | ** resolve the result-set expression list. |
| 107578 | 107982 | */ |
| | @@ -109157,13 +109561,11 @@ |
| 109157 | 109561 | assert( pExpr->op==TK_FUNCTION ); |
| 109158 | 109562 | assert( pExpr->pLeft==0 ); |
| 109159 | 109563 | assert( ExprUseXList(pExpr) ); |
| 109160 | 109564 | if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){ |
| 109161 | 109565 | /* Ignore ORDER BY on zero-argument aggregates */ |
| 109162 | | - sqlite3ParserAddCleanup(pParse, |
| 109163 | | - (void(*)(sqlite3*,void*))sqlite3ExprListDelete, |
| 109164 | | - pOrderBy); |
| 109566 | + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pOrderBy); |
| 109165 | 109567 | return; |
| 109166 | 109568 | } |
| 109167 | 109569 | if( IsWindowFunc(pExpr) ){ |
| 109168 | 109570 | sqlite3ExprOrderByAggregateError(pParse, pExpr); |
| 109169 | 109571 | sqlite3ExprListDelete(db, pOrderBy); |
| | @@ -109340,10 +109742,13 @@ |
| 109340 | 109742 | } |
| 109341 | 109743 | } |
| 109342 | 109744 | SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
| 109343 | 109745 | if( p ) sqlite3ExprDeleteNN(db, p); |
| 109344 | 109746 | } |
| 109747 | +SQLITE_PRIVATE void sqlite3ExprDeleteGeneric(sqlite3 *db, void *p){ |
| 109748 | + if( ALWAYS(p) ) sqlite3ExprDeleteNN(db, (Expr*)p); |
| 109749 | +} |
| 109345 | 109750 | |
| 109346 | 109751 | /* |
| 109347 | 109752 | ** Clear both elements of an OnOrUsing object |
| 109348 | 109753 | */ |
| 109349 | 109754 | SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){ |
| | @@ -109365,13 +109770,11 @@ |
| 109365 | 109770 | ** |
| 109366 | 109771 | ** The deferred delete is (currently) implemented by adding the |
| 109367 | 109772 | ** pExpr to the pParse->pConstExpr list with a register number of 0. |
| 109368 | 109773 | */ |
| 109369 | 109774 | SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){ |
| 109370 | | - sqlite3ParserAddCleanup(pParse, |
| 109371 | | - (void(*)(sqlite3*,void*))sqlite3ExprDelete, |
| 109372 | | - pExpr); |
| 109775 | + sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr); |
| 109373 | 109776 | } |
| 109374 | 109777 | |
| 109375 | 109778 | /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the |
| 109376 | 109779 | ** expression. |
| 109377 | 109780 | */ |
| | @@ -110173,10 +110576,13 @@ |
| 110173 | 110576 | sqlite3DbNNFreeNN(db, pList); |
| 110174 | 110577 | } |
| 110175 | 110578 | SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
| 110176 | 110579 | if( pList ) exprListDeleteNN(db, pList); |
| 110177 | 110580 | } |
| 110581 | +SQLITE_PRIVATE void sqlite3ExprListDeleteGeneric(sqlite3 *db, void *pList){ |
| 110582 | + if( ALWAYS(pList) ) exprListDeleteNN(db, (ExprList*)pList); |
| 110583 | +} |
| 110178 | 110584 | |
| 110179 | 110585 | /* |
| 110180 | 110586 | ** Return the bitwise-OR of all Expr.flags fields in the given |
| 110181 | 110587 | ** ExprList. |
| 110182 | 110588 | */ |
| | @@ -110672,13 +111078,14 @@ |
| 110672 | 111078 | case TK_BLOB: |
| 110673 | 111079 | return 0; |
| 110674 | 111080 | case TK_COLUMN: |
| 110675 | 111081 | assert( ExprUseYTab(p) ); |
| 110676 | 111082 | return ExprHasProperty(p, EP_CanBeNull) || |
| 110677 | | - p->y.pTab==0 || /* Reference to column of index on expression */ |
| 111083 | + NEVER(p->y.pTab==0) || /* Reference to column of index on expr */ |
| 110678 | 111084 | (p->iColumn>=0 |
| 110679 | 111085 | && p->y.pTab->aCol!=0 /* Possible due to prior error */ |
| 111086 | + && ALWAYS(p->iColumn<p->y.pTab->nCol) |
| 110680 | 111087 | && p->y.pTab->aCol[p->iColumn].notNull==0); |
| 110681 | 111088 | default: |
| 110682 | 111089 | return 1; |
| 110683 | 111090 | } |
| 110684 | 111091 | } |
| | @@ -113256,12 +113663,14 @@ |
| 113256 | 113663 | assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); |
| 113257 | 113664 | if( pParse->pVdbe==0 ) return; |
| 113258 | 113665 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 113259 | 113666 | if( inReg!=target ){ |
| 113260 | 113667 | u8 op; |
| 113261 | | - if( ALWAYS(pExpr) |
| 113262 | | - && (ExprHasProperty(pExpr,EP_Subquery) || pExpr->op==TK_REGISTER) |
| 113668 | + Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr); |
| 113669 | + testcase( pX!=pExpr ); |
| 113670 | + if( ALWAYS(pX) |
| 113671 | + && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER) |
| 113263 | 113672 | ){ |
| 113264 | 113673 | op = OP_Copy; |
| 113265 | 113674 | }else{ |
| 113266 | 113675 | op = OP_SCopy; |
| 113267 | 113676 | } |
| | @@ -114703,17 +115112,18 @@ |
| 114703 | 115112 | return WRC_Continue; |
| 114704 | 115113 | } |
| 114705 | 115114 | case TK_AGG_FUNCTION: { |
| 114706 | 115115 | if( (pNC->ncFlags & NC_InAggFunc)==0 |
| 114707 | 115116 | && pWalker->walkerDepth==pExpr->op2 |
| 115117 | + && pExpr->pAggInfo==0 |
| 114708 | 115118 | ){ |
| 114709 | 115119 | /* Check to see if pExpr is a duplicate of another aggregate |
| 114710 | 115120 | ** function that is already in the pAggInfo structure |
| 114711 | 115121 | */ |
| 114712 | 115122 | struct AggInfo_func *pItem = pAggInfo->aFunc; |
| 114713 | 115123 | for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
| 114714 | | - if( pItem->pFExpr==pExpr ) break; |
| 115124 | + if( NEVER(pItem->pFExpr==pExpr) ) break; |
| 114715 | 115125 | if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){ |
| 114716 | 115126 | break; |
| 114717 | 115127 | } |
| 114718 | 115128 | } |
| 114719 | 115129 | if( i>=pAggInfo->nFunc ){ |
| | @@ -114752,10 +115162,12 @@ |
| 114752 | 115162 | pItem->bOBPayload = 0; |
| 114753 | 115163 | pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct); |
| 114754 | 115164 | }else{ |
| 114755 | 115165 | pItem->bOBPayload = 1; |
| 114756 | 115166 | } |
| 115167 | + pItem->bUseSubtype = |
| 115168 | + (pItem->pFunc->funcFlags & SQLITE_SUBTYPE)!=0; |
| 114757 | 115169 | }else{ |
| 114758 | 115170 | pItem->iOBTab = -1; |
| 114759 | 115171 | } |
| 114760 | 115172 | if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){ |
| 114761 | 115173 | pItem->iDistinct = pParse->nTab++; |
| | @@ -117518,13 +117930,13 @@ |
| 117518 | 117930 | ** information. |
| 117519 | 117931 | */ |
| 117520 | 117932 | typedef struct StatAccum StatAccum; |
| 117521 | 117933 | typedef struct StatSample StatSample; |
| 117522 | 117934 | struct StatSample { |
| 117523 | | - tRowcnt *anEq; /* sqlite_stat4.nEq */ |
| 117524 | 117935 | tRowcnt *anDLt; /* sqlite_stat4.nDLt */ |
| 117525 | 117936 | #ifdef SQLITE_ENABLE_STAT4 |
| 117937 | + tRowcnt *anEq; /* sqlite_stat4.nEq */ |
| 117526 | 117938 | tRowcnt *anLt; /* sqlite_stat4.nLt */ |
| 117527 | 117939 | union { |
| 117528 | 117940 | i64 iRowid; /* Rowid in main table of the key */ |
| 117529 | 117941 | u8 *aRowid; /* Key for WITHOUT ROWID tables */ |
| 117530 | 117942 | } u; |
| | @@ -117678,13 +118090,13 @@ |
| 117678 | 118090 | assert( nKeyCol<=nCol ); |
| 117679 | 118091 | assert( nKeyCol>0 ); |
| 117680 | 118092 | |
| 117681 | 118093 | /* Allocate the space required for the StatAccum object */ |
| 117682 | 118094 | n = sizeof(*p) |
| 117683 | | - + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */ |
| 117684 | | - + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */ |
| 118095 | + + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */ |
| 117685 | 118096 | #ifdef SQLITE_ENABLE_STAT4 |
| 118097 | + n += sizeof(tRowcnt)*nColUp; /* StatAccum.anEq */ |
| 117686 | 118098 | if( mxSample ){ |
| 117687 | 118099 | n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */ |
| 117688 | 118100 | + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */ |
| 117689 | 118101 | + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample); |
| 117690 | 118102 | } |
| | @@ -117701,13 +118113,13 @@ |
| 117701 | 118113 | p->nLimit = sqlite3_value_int64(argv[3]); |
| 117702 | 118114 | p->nCol = nCol; |
| 117703 | 118115 | p->nKeyCol = nKeyCol; |
| 117704 | 118116 | p->nSkipAhead = 0; |
| 117705 | 118117 | p->current.anDLt = (tRowcnt*)&p[1]; |
| 117706 | | - p->current.anEq = &p->current.anDLt[nColUp]; |
| 117707 | 118118 | |
| 117708 | 118119 | #ifdef SQLITE_ENABLE_STAT4 |
| 118120 | + p->current.anEq = &p->current.anDLt[nColUp]; |
| 117709 | 118121 | p->mxSample = p->nLimit==0 ? mxSample : 0; |
| 117710 | 118122 | if( mxSample ){ |
| 117711 | 118123 | u8 *pSpace; /* Allocated space not yet assigned */ |
| 117712 | 118124 | int i; /* Used to iterate through p->aSample[] */ |
| 117713 | 118125 | |
| | @@ -117970,28 +118382,32 @@ |
| 117970 | 118382 | assert( p->nCol>0 ); |
| 117971 | 118383 | assert( iChng<p->nCol ); |
| 117972 | 118384 | |
| 117973 | 118385 | if( p->nRow==0 ){ |
| 117974 | 118386 | /* This is the first call to this function. Do initialization. */ |
| 118387 | +#ifdef SQLITE_ENABLE_STAT4 |
| 117975 | 118388 | for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1; |
| 118389 | +#endif |
| 117976 | 118390 | }else{ |
| 117977 | 118391 | /* Second and subsequent calls get processed here */ |
| 117978 | 118392 | #ifdef SQLITE_ENABLE_STAT4 |
| 117979 | 118393 | if( p->mxSample ) samplePushPrevious(p, iChng); |
| 117980 | 118394 | #endif |
| 117981 | 118395 | |
| 117982 | 118396 | /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply |
| 117983 | 118397 | ** to the current row of the index. */ |
| 118398 | +#ifdef SQLITE_ENABLE_STAT4 |
| 117984 | 118399 | for(i=0; i<iChng; i++){ |
| 117985 | 118400 | p->current.anEq[i]++; |
| 117986 | 118401 | } |
| 118402 | +#endif |
| 117987 | 118403 | for(i=iChng; i<p->nCol; i++){ |
| 117988 | 118404 | p->current.anDLt[i]++; |
| 117989 | 118405 | #ifdef SQLITE_ENABLE_STAT4 |
| 117990 | 118406 | if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i]; |
| 117991 | | -#endif |
| 117992 | 118407 | p->current.anEq[i] = 1; |
| 118408 | +#endif |
| 117993 | 118409 | } |
| 117994 | 118410 | } |
| 117995 | 118411 | |
| 117996 | 118412 | p->nRow++; |
| 117997 | 118413 | #ifdef SQLITE_ENABLE_STAT4 |
| | @@ -118121,11 +118537,13 @@ |
| 118121 | 118537 | for(i=0; i<p->nKeyCol; i++){ |
| 118122 | 118538 | u64 nDistinct = p->current.anDLt[i] + 1; |
| 118123 | 118539 | u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; |
| 118124 | 118540 | if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1; |
| 118125 | 118541 | sqlite3_str_appendf(&sStat, " %llu", iVal); |
| 118542 | +#ifdef SQLITE_ENABLE_STAT4 |
| 118126 | 118543 | assert( p->current.anEq[i] ); |
| 118544 | +#endif |
| 118127 | 118545 | } |
| 118128 | 118546 | sqlite3ResultStrAccum(context, &sStat); |
| 118129 | 118547 | } |
| 118130 | 118548 | #ifdef SQLITE_ENABLE_STAT4 |
| 118131 | 118549 | else if( eCall==STAT_GET_ROWID ){ |
| | @@ -118810,10 +119228,20 @@ |
| 118810 | 119228 | } |
| 118811 | 119229 | #endif |
| 118812 | 119230 | while( z[0]!=0 && z[0]!=' ' ) z++; |
| 118813 | 119231 | while( z[0]==' ' ) z++; |
| 118814 | 119232 | } |
| 119233 | + |
| 119234 | + /* Set the bLowQual flag if the peak number of rows obtained |
| 119235 | + ** from a full equality match is so large that a full table scan |
| 119236 | + ** seems likely to be faster than using the index. |
| 119237 | + */ |
| 119238 | + if( aLog[0] > 66 /* Index has more than 100 rows */ |
| 119239 | + && aLog[0] <= aLog[nOut-1] /* And only a single value seen */ |
| 119240 | + ){ |
| 119241 | + pIndex->bLowQual = 1; |
| 119242 | + } |
| 118815 | 119243 | } |
| 118816 | 119244 | } |
| 118817 | 119245 | |
| 118818 | 119246 | /* |
| 118819 | 119247 | ** This callback is invoked once for each index when reading the |
| | @@ -120856,11 +121284,11 @@ |
| 120856 | 121284 | ** the DEFAULT clause or the AS clause of a generated column. |
| 120857 | 121285 | ** Return NULL if the column has no associated expression. |
| 120858 | 121286 | */ |
| 120859 | 121287 | SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){ |
| 120860 | 121288 | if( pCol->iDflt==0 ) return 0; |
| 120861 | | - if( NEVER(!IsOrdinaryTable(pTab)) ) return 0; |
| 121289 | + if( !IsOrdinaryTable(pTab) ) return 0; |
| 120862 | 121290 | if( NEVER(pTab->u.tab.pDfltList==0) ) return 0; |
| 120863 | 121291 | if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0; |
| 120864 | 121292 | return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
| 120865 | 121293 | } |
| 120866 | 121294 | |
| | @@ -121008,10 +121436,13 @@ |
| 121008 | 121436 | /* Do not delete the table until the reference count reaches zero. */ |
| 121009 | 121437 | assert( db!=0 ); |
| 121010 | 121438 | if( !pTable ) return; |
| 121011 | 121439 | if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return; |
| 121012 | 121440 | deleteTable(db, pTable); |
| 121441 | +} |
| 121442 | +SQLITE_PRIVATE void sqlite3DeleteTableGeneric(sqlite3 *db, void *pTable){ |
| 121443 | + sqlite3DeleteTable(db, (Table*)pTable); |
| 121013 | 121444 | } |
| 121014 | 121445 | |
| 121015 | 121446 | |
| 121016 | 121447 | /* |
| 121017 | 121448 | ** Unlink the given table from the hash tables and the delete the |
| | @@ -121546,11 +121977,12 @@ |
| 121546 | 121977 | #endif |
| 121547 | 121978 | |
| 121548 | 121979 | /* |
| 121549 | 121980 | ** Clean up the data structures associated with the RETURNING clause. |
| 121550 | 121981 | */ |
| 121551 | | -static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){ |
| 121982 | +static void sqlite3DeleteReturning(sqlite3 *db, void *pArg){ |
| 121983 | + Returning *pRet = (Returning*)pArg; |
| 121552 | 121984 | Hash *pHash; |
| 121553 | 121985 | pHash = &(db->aDb[1].pSchema->trigHash); |
| 121554 | 121986 | sqlite3HashInsert(pHash, pRet->zName, 0); |
| 121555 | 121987 | sqlite3ExprListDelete(db, pRet->pReturnEL); |
| 121556 | 121988 | sqlite3DbFree(db, pRet); |
| | @@ -121588,12 +122020,11 @@ |
| 121588 | 122020 | return; |
| 121589 | 122021 | } |
| 121590 | 122022 | pParse->u1.pReturning = pRet; |
| 121591 | 122023 | pRet->pParse = pParse; |
| 121592 | 122024 | pRet->pReturnEL = pList; |
| 121593 | | - sqlite3ParserAddCleanup(pParse, |
| 121594 | | - (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet); |
| 122025 | + sqlite3ParserAddCleanup(pParse, sqlite3DeleteReturning, pRet); |
| 121595 | 122026 | testcase( pParse->earlyCleanup ); |
| 121596 | 122027 | if( db->mallocFailed ) return; |
| 121597 | 122028 | sqlite3_snprintf(sizeof(pRet->zName), pRet->zName, |
| 121598 | 122029 | "sqlite_returning_%p", pParse); |
| 121599 | 122030 | pRet->retTrig.zName = pRet->zName; |
| | @@ -121788,11 +122219,12 @@ |
| 121788 | 122219 | char aff = SQLITE_AFF_NUMERIC; |
| 121789 | 122220 | const char *zChar = 0; |
| 121790 | 122221 | |
| 121791 | 122222 | assert( zIn!=0 ); |
| 121792 | 122223 | while( zIn[0] ){ |
| 121793 | | - h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
| 122224 | + u8 x = *(u8*)zIn; |
| 122225 | + h = (h<<8) + sqlite3UpperToLower[x]; |
| 121794 | 122226 | zIn++; |
| 121795 | 122227 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
| 121796 | 122228 | aff = SQLITE_AFF_TEXT; |
| 121797 | 122229 | zChar = zIn; |
| 121798 | 122230 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| | @@ -125653,20 +126085,21 @@ |
| 125653 | 126085 | } |
| 125654 | 126086 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 125655 | 126087 | if( iDb<0 ) return; |
| 125656 | 126088 | z = sqlite3NameFromToken(db, pObjName); |
| 125657 | 126089 | if( z==0 ) return; |
| 125658 | | - zDb = db->aDb[iDb].zDbSName; |
| 126090 | + zDb = pName2->n ? db->aDb[iDb].zDbSName : 0; |
| 125659 | 126091 | pTab = sqlite3FindTable(db, z, zDb); |
| 125660 | 126092 | if( pTab ){ |
| 125661 | 126093 | reindexTable(pParse, pTab, 0); |
| 125662 | 126094 | sqlite3DbFree(db, z); |
| 125663 | 126095 | return; |
| 125664 | 126096 | } |
| 125665 | 126097 | pIndex = sqlite3FindIndex(db, z, zDb); |
| 125666 | 126098 | sqlite3DbFree(db, z); |
| 125667 | 126099 | if( pIndex ){ |
| 126100 | + iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema); |
| 125668 | 126101 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 125669 | 126102 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 125670 | 126103 | return; |
| 125671 | 126104 | } |
| 125672 | 126105 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| | @@ -125827,10 +126260,13 @@ |
| 125827 | 126260 | for(i=0; i<pWith->nCte; i++){ |
| 125828 | 126261 | cteClear(db, &pWith->a[i]); |
| 125829 | 126262 | } |
| 125830 | 126263 | sqlite3DbFree(db, pWith); |
| 125831 | 126264 | } |
| 126265 | +} |
| 126266 | +SQLITE_PRIVATE void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){ |
| 126267 | + sqlite3WithDelete(db, (With*)pWith); |
| 125832 | 126268 | } |
| 125833 | 126269 | #endif /* !defined(SQLITE_OMIT_CTE) */ |
| 125834 | 126270 | |
| 125835 | 126271 | /************** End of build.c ***********************************************/ |
| 125836 | 126272 | /************** Begin file callback.c ****************************************/ |
| | @@ -139053,11 +139489,12 @@ |
| 139053 | 139489 | if( NEVER(pVTab==0) ) continue; |
| 139054 | 139490 | if( NEVER(pVTab->pModule==0) ) continue; |
| 139055 | 139491 | if( pVTab->pModule->iVersion<4 ) continue; |
| 139056 | 139492 | if( pVTab->pModule->xIntegrity==0 ) continue; |
| 139057 | 139493 | sqlite3VdbeAddOp3(v, OP_VCheck, i, 3, isQuick); |
| 139058 | | - sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
| 139494 | + pTab->nTabRef++; |
| 139495 | + sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF); |
| 139059 | 139496 | a1 = sqlite3VdbeAddOp1(v, OP_IsNull, 3); VdbeCoverage(v); |
| 139060 | 139497 | integrityCheckResultRow(v); |
| 139061 | 139498 | sqlite3VdbeJumpHere(v, a1); |
| 139062 | 139499 | #endif |
| 139063 | 139500 | continue; |
| | @@ -141080,10 +141517,11 @@ |
| 141080 | 141517 | sqlite3BtreeLeaveAll(db); |
| 141081 | 141518 | rc = sqlite3ApiExit(db, rc); |
| 141082 | 141519 | assert( (rc&db->errMask)==rc ); |
| 141083 | 141520 | db->busyHandler.nBusy = 0; |
| 141084 | 141521 | sqlite3_mutex_leave(db->mutex); |
| 141522 | + assert( rc==SQLITE_OK || (*ppStmt)==0 ); |
| 141085 | 141523 | return rc; |
| 141086 | 141524 | } |
| 141087 | 141525 | |
| 141088 | 141526 | |
| 141089 | 141527 | /* |
| | @@ -141476,10 +141914,13 @@ |
| 141476 | 141914 | /* |
| 141477 | 141915 | ** Delete the given Select structure and all of its substructures. |
| 141478 | 141916 | */ |
| 141479 | 141917 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){ |
| 141480 | 141918 | if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1); |
| 141919 | +} |
| 141920 | +SQLITE_PRIVATE void sqlite3SelectDeleteGeneric(sqlite3 *db, void *p){ |
| 141921 | + if( ALWAYS(p) ) clearSelect(db, (Select*)p, 1); |
| 141481 | 141922 | } |
| 141482 | 141923 | |
| 141483 | 141924 | /* |
| 141484 | 141925 | ** Return a pointer to the right-most SELECT statement in a compound. |
| 141485 | 141926 | */ |
| | @@ -144497,13 +144938,11 @@ |
| 144497 | 144938 | |
| 144498 | 144939 | multi_select_end: |
| 144499 | 144940 | pDest->iSdst = dest.iSdst; |
| 144500 | 144941 | pDest->nSdst = dest.nSdst; |
| 144501 | 144942 | if( pDelete ){ |
| 144502 | | - sqlite3ParserAddCleanup(pParse, |
| 144503 | | - (void(*)(sqlite3*,void*))sqlite3SelectDelete, |
| 144504 | | - pDelete); |
| 144943 | + sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pDelete); |
| 144505 | 144944 | } |
| 144506 | 144945 | return rc; |
| 144507 | 144946 | } |
| 144508 | 144947 | #endif /* SQLITE_OMIT_COMPOUND_SELECT */ |
| 144509 | 144948 | |
| | @@ -145050,12 +145489,11 @@ |
| 145050 | 145489 | sqlite3VdbeResolveLabel(v, labelEnd); |
| 145051 | 145490 | |
| 145052 | 145491 | /* Make arrangements to free the 2nd and subsequent arms of the compound |
| 145053 | 145492 | ** after the parse has finished */ |
| 145054 | 145493 | if( pSplit->pPrior ){ |
| 145055 | | - sqlite3ParserAddCleanup(pParse, |
| 145056 | | - (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior); |
| 145494 | + sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior); |
| 145057 | 145495 | } |
| 145058 | 145496 | pSplit->pPrior = pPrior; |
| 145059 | 145497 | pPrior->pNext = pSplit; |
| 145060 | 145498 | sqlite3ExprListDelete(db, pPrior->pOrderBy); |
| 145061 | 145499 | pPrior->pOrderBy = 0; |
| | @@ -145872,13 +146310,11 @@ |
| 145872 | 146310 | */ |
| 145873 | 146311 | if( ALWAYS(pSubitem->pTab!=0) ){ |
| 145874 | 146312 | Table *pTabToDel = pSubitem->pTab; |
| 145875 | 146313 | if( pTabToDel->nTabRef==1 ){ |
| 145876 | 146314 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 145877 | | - sqlite3ParserAddCleanup(pToplevel, |
| 145878 | | - (void(*)(sqlite3*,void*))sqlite3DeleteTable, |
| 145879 | | - pTabToDel); |
| 146315 | + sqlite3ParserAddCleanup(pToplevel, sqlite3DeleteTableGeneric, pTabToDel); |
| 145880 | 146316 | testcase( pToplevel->earlyCleanup ); |
| 145881 | 146317 | }else{ |
| 145882 | 146318 | pTabToDel->nTabRef--; |
| 145883 | 146319 | } |
| 145884 | 146320 | pSubitem->pTab = 0; |
| | @@ -146921,12 +147357,11 @@ |
| 146921 | 147357 | ** calling this routine, Instead, use only the return value. |
| 146922 | 147358 | */ |
| 146923 | 147359 | SQLITE_PRIVATE With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){ |
| 146924 | 147360 | if( pWith ){ |
| 146925 | 147361 | if( bFree ){ |
| 146926 | | - pWith = (With*)sqlite3ParserAddCleanup(pParse, |
| 146927 | | - (void(*)(sqlite3*,void*))sqlite3WithDelete, |
| 147362 | + pWith = (With*)sqlite3ParserAddCleanup(pParse, sqlite3WithDeleteGeneric, |
| 146928 | 147363 | pWith); |
| 146929 | 147364 | if( pWith==0 ) return 0; |
| 146930 | 147365 | } |
| 146931 | 147366 | if( pParse->nErr==0 ){ |
| 146932 | 147367 | assert( pParse->pWith!=pWith ); |
| | @@ -147955,19 +148390,23 @@ |
| 147955 | 148390 | KeyInfo *pKeyInfo; |
| 147956 | 148391 | int nExtra = 0; |
| 147957 | 148392 | assert( pFunc->pFExpr->pLeft!=0 ); |
| 147958 | 148393 | assert( pFunc->pFExpr->pLeft->op==TK_ORDER ); |
| 147959 | 148394 | assert( ExprUseXList(pFunc->pFExpr->pLeft) ); |
| 148395 | + assert( pFunc->pFunc!=0 ); |
| 147960 | 148396 | pOBList = pFunc->pFExpr->pLeft->x.pList; |
| 147961 | 148397 | if( !pFunc->bOBUnique ){ |
| 147962 | 148398 | nExtra++; /* One extra column for the OP_Sequence */ |
| 147963 | 148399 | } |
| 147964 | 148400 | if( pFunc->bOBPayload ){ |
| 147965 | 148401 | /* extra columns for the function arguments */ |
| 147966 | 148402 | assert( ExprUseXList(pFunc->pFExpr) ); |
| 147967 | 148403 | nExtra += pFunc->pFExpr->x.pList->nExpr; |
| 147968 | 148404 | } |
| 148405 | + if( pFunc->bUseSubtype ){ |
| 148406 | + nExtra += pFunc->pFExpr->x.pList->nExpr; |
| 148407 | + } |
| 147969 | 148408 | pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOBList, 0, nExtra); |
| 147970 | 148409 | if( !pFunc->bOBUnique && pParse->nErr==0 ){ |
| 147971 | 148410 | pKeyInfo->nKeyField++; |
| 147972 | 148411 | } |
| 147973 | 148412 | sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
| | @@ -147990,20 +148429,21 @@ |
| 147990 | 148429 | for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ |
| 147991 | 148430 | ExprList *pList; |
| 147992 | 148431 | assert( ExprUseXList(pF->pFExpr) ); |
| 147993 | 148432 | pList = pF->pFExpr->x.pList; |
| 147994 | 148433 | if( pF->iOBTab>=0 ){ |
| 147995 | | - /* For an ORDER BY aggregate, calls to OP_AggStep where deferred and |
| 147996 | | - ** all content was stored in emphermal table pF->iOBTab. Extract that |
| 147997 | | - ** content now (in ORDER BY order) and make all calls to OP_AggStep |
| 148434 | + /* For an ORDER BY aggregate, calls to OP_AggStep were deferred. Inputs |
| 148435 | + ** were stored in emphermal table pF->iOBTab. Here, we extract those |
| 148436 | + ** inputs (in ORDER BY order) and make all calls to OP_AggStep |
| 147998 | 148437 | ** before doing the OP_AggFinal call. */ |
| 147999 | 148438 | int iTop; /* Start of loop for extracting columns */ |
| 148000 | 148439 | int nArg; /* Number of columns to extract */ |
| 148001 | 148440 | int nKey; /* Key columns to be skipped */ |
| 148002 | 148441 | int regAgg; /* Extract into this array */ |
| 148003 | 148442 | int j; /* Loop counter */ |
| 148004 | 148443 | |
| 148444 | + assert( pF->pFunc!=0 ); |
| 148005 | 148445 | nArg = pList->nExpr; |
| 148006 | 148446 | regAgg = sqlite3GetTempRange(pParse, nArg); |
| 148007 | 148447 | |
| 148008 | 148448 | if( pF->bOBPayload==0 ){ |
| 148009 | 148449 | nKey = 0; |
| | @@ -148016,10 +148456,19 @@ |
| 148016 | 148456 | } |
| 148017 | 148457 | iTop = sqlite3VdbeAddOp1(v, OP_Rewind, pF->iOBTab); VdbeCoverage(v); |
| 148018 | 148458 | for(j=nArg-1; j>=0; j--){ |
| 148019 | 148459 | sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, nKey+j, regAgg+j); |
| 148020 | 148460 | } |
| 148461 | + if( pF->bUseSubtype ){ |
| 148462 | + int regSubtype = sqlite3GetTempReg(pParse); |
| 148463 | + int iBaseCol = nKey + nArg + (pF->bOBPayload==0 && pF->bOBUnique==0); |
| 148464 | + for(j=nArg-1; j>=0; j--){ |
| 148465 | + sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, iBaseCol+j, regSubtype); |
| 148466 | + sqlite3VdbeAddOp2(v, OP_SetSubtype, regSubtype, regAgg+j); |
| 148467 | + } |
| 148468 | + sqlite3ReleaseTempReg(pParse, regSubtype); |
| 148469 | + } |
| 148021 | 148470 | sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i)); |
| 148022 | 148471 | sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
| 148023 | 148472 | sqlite3VdbeChangeP5(v, (u8)nArg); |
| 148024 | 148473 | sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v); |
| 148025 | 148474 | sqlite3VdbeJumpHere(v, iTop); |
| | @@ -148070,10 +148519,11 @@ |
| 148070 | 148519 | int regAggSz = 0; |
| 148071 | 148520 | int regDistinct = 0; |
| 148072 | 148521 | ExprList *pList; |
| 148073 | 148522 | assert( ExprUseXList(pF->pFExpr) ); |
| 148074 | 148523 | assert( !IsWindowFunc(pF->pFExpr) ); |
| 148524 | + assert( pF->pFunc!=0 ); |
| 148075 | 148525 | pList = pF->pFExpr->x.pList; |
| 148076 | 148526 | if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){ |
| 148077 | 148527 | Expr *pFilter = pF->pFExpr->y.pWin->pFilter; |
| 148078 | 148528 | if( pAggInfo->nAccumulator |
| 148079 | 148529 | && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) |
| | @@ -148113,10 +148563,13 @@ |
| 148113 | 148563 | if( !pF->bOBUnique ){ |
| 148114 | 148564 | regAggSz++; /* One register for OP_Sequence */ |
| 148115 | 148565 | } |
| 148116 | 148566 | if( pF->bOBPayload ){ |
| 148117 | 148567 | regAggSz += nArg; |
| 148568 | + } |
| 148569 | + if( pF->bUseSubtype ){ |
| 148570 | + regAggSz += nArg; |
| 148118 | 148571 | } |
| 148119 | 148572 | regAggSz++; /* One extra register to hold result of MakeRecord */ |
| 148120 | 148573 | regAgg = sqlite3GetTempRange(pParse, regAggSz); |
| 148121 | 148574 | regDistinct = regAgg; |
| 148122 | 148575 | sqlite3ExprCodeExprList(pParse, pOBList, regAgg, 0, SQLITE_ECEL_DUP); |
| | @@ -148126,10 +148579,18 @@ |
| 148126 | 148579 | jj++; |
| 148127 | 148580 | } |
| 148128 | 148581 | if( pF->bOBPayload ){ |
| 148129 | 148582 | regDistinct = regAgg+jj; |
| 148130 | 148583 | sqlite3ExprCodeExprList(pParse, pList, regDistinct, 0, SQLITE_ECEL_DUP); |
| 148584 | + jj += nArg; |
| 148585 | + } |
| 148586 | + if( pF->bUseSubtype ){ |
| 148587 | + int kk; |
| 148588 | + int regBase = pF->bOBPayload ? regDistinct : regAgg; |
| 148589 | + for(kk=0; kk<nArg; kk++, jj++){ |
| 148590 | + sqlite3VdbeAddOp2(v, OP_GetSubtype, regBase+kk, regAgg+jj); |
| 148591 | + } |
| 148131 | 148592 | } |
| 148132 | 148593 | }else if( pList ){ |
| 148133 | 148594 | nArg = pList->nExpr; |
| 148134 | 148595 | regAgg = sqlite3GetTempRange(pParse, nArg); |
| 148135 | 148596 | regDistinct = regAgg; |
| | @@ -148330,11 +148791,12 @@ |
| 148330 | 148791 | } |
| 148331 | 148792 | |
| 148332 | 148793 | /* |
| 148333 | 148794 | ** Deallocate a single AggInfo object |
| 148334 | 148795 | */ |
| 148335 | | -static void agginfoFree(sqlite3 *db, AggInfo *p){ |
| 148796 | +static void agginfoFree(sqlite3 *db, void *pArg){ |
| 148797 | + AggInfo *p = (AggInfo*)pArg; |
| 148336 | 148798 | sqlite3DbFree(db, p->aCol); |
| 148337 | 148799 | sqlite3DbFree(db, p->aFunc); |
| 148338 | 148800 | sqlite3DbFreeNN(db, p); |
| 148339 | 148801 | } |
| 148340 | 148802 | |
| | @@ -148404,11 +148866,11 @@ |
| 148404 | 148866 | pSub->pPrior = 0; |
| 148405 | 148867 | pSub->pNext = 0; |
| 148406 | 148868 | pSub->selFlags |= SF_Aggregate; |
| 148407 | 148869 | pSub->selFlags &= ~SF_Compound; |
| 148408 | 148870 | pSub->nSelectRow = 0; |
| 148409 | | - sqlite3ExprListDelete(db, pSub->pEList); |
| 148871 | + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList); |
| 148410 | 148872 | pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount; |
| 148411 | 148873 | pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm); |
| 148412 | 148874 | pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0); |
| 148413 | 148875 | sqlite3PExprAddSelect(pParse, pTerm, pSub); |
| 148414 | 148876 | if( pExpr==0 ){ |
| | @@ -148584,13 +149046,12 @@ |
| 148584 | 149046 | TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n")); |
| 148585 | 149047 | if( sqlite3TreeTrace & 0x800 ){ |
| 148586 | 149048 | sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY"); |
| 148587 | 149049 | } |
| 148588 | 149050 | #endif |
| 148589 | | - sqlite3ParserAddCleanup(pParse, |
| 148590 | | - (void(*)(sqlite3*,void*))sqlite3ExprListDelete, |
| 148591 | | - p->pOrderBy); |
| 149051 | + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, |
| 149052 | + p->pOrderBy); |
| 148592 | 149053 | testcase( pParse->earlyCleanup ); |
| 148593 | 149054 | p->pOrderBy = 0; |
| 148594 | 149055 | } |
| 148595 | 149056 | p->selFlags &= ~SF_Distinct; |
| 148596 | 149057 | p->selFlags |= SF_NoopOrderBy; |
| | @@ -148778,13 +149239,12 @@ |
| 148778 | 149239 | && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */ |
| 148779 | 149240 | && OptimizationEnabled(db, SQLITE_OmitOrderBy) |
| 148780 | 149241 | ){ |
| 148781 | 149242 | TREETRACE(0x800,pParse,p, |
| 148782 | 149243 | ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1)); |
| 148783 | | - sqlite3ParserAddCleanup(pParse, |
| 148784 | | - (void(*)(sqlite3*,void*))sqlite3ExprListDelete, |
| 148785 | | - pSub->pOrderBy); |
| 149244 | + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, |
| 149245 | + pSub->pOrderBy); |
| 148786 | 149246 | pSub->pOrderBy = 0; |
| 148787 | 149247 | } |
| 148788 | 149248 | |
| 148789 | 149249 | /* If the outer query contains a "complex" result set (that is, |
| 148790 | 149250 | ** if the result set of the outer query uses functions or subqueries) |
| | @@ -149309,12 +149769,11 @@ |
| 149309 | 149769 | ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the |
| 149310 | 149770 | ** SELECT statement. |
| 149311 | 149771 | */ |
| 149312 | 149772 | pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) ); |
| 149313 | 149773 | if( pAggInfo ){ |
| 149314 | | - sqlite3ParserAddCleanup(pParse, |
| 149315 | | - (void(*)(sqlite3*,void*))agginfoFree, pAggInfo); |
| 149774 | + sqlite3ParserAddCleanup(pParse, agginfoFree, pAggInfo); |
| 149316 | 149775 | testcase( pParse->earlyCleanup ); |
| 149317 | 149776 | } |
| 149318 | 149777 | if( db->mallocFailed ){ |
| 149319 | 149778 | goto select_end; |
| 149320 | 149779 | } |
| | @@ -153959,11 +154418,10 @@ |
| 153959 | 154418 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 153960 | 154419 | assert( sqlite3_mutex_held(db->mutex) ); |
| 153961 | 154420 | |
| 153962 | 154421 | if( p ){ |
| 153963 | 154422 | db->pDisconnect = 0; |
| 153964 | | - sqlite3ExpirePreparedStatements(db, 0); |
| 153965 | 154423 | do { |
| 153966 | 154424 | VTable *pNext = p->pNext; |
| 153967 | 154425 | sqlite3VtabUnlock(p); |
| 153968 | 154426 | p = pNext; |
| 153969 | 154427 | }while( p ); |
| | @@ -155525,11 +155983,11 @@ |
| 155525 | 155983 | */ |
| 155526 | 155984 | SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); |
| 155527 | 155985 | #ifdef WHERETRACE_ENABLED |
| 155528 | 155986 | SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC); |
| 155529 | 155987 | SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm); |
| 155530 | | -SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC); |
| 155988 | +SQLITE_PRIVATE void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC); |
| 155531 | 155989 | #endif |
| 155532 | 155990 | SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( |
| 155533 | 155991 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 155534 | 155992 | int iCur, /* Cursor number of LHS */ |
| 155535 | 155993 | int iColumn, /* Column number of LHS */ |
| | @@ -160987,16 +161445,26 @@ |
| 160987 | 161445 | int iEnd = sqlite3VdbeCurrentAddr(v); |
| 160988 | 161446 | if( pParse->db->mallocFailed ) return; |
| 160989 | 161447 | for(; iStart<iEnd; iStart++, pOp++){ |
| 160990 | 161448 | if( pOp->p1!=iTabCur ) continue; |
| 160991 | 161449 | if( pOp->opcode==OP_Column ){ |
| 161450 | +#ifdef SQLITE_DEBUG |
| 161451 | + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
| 161452 | + printf("TRANSLATE OP_Column to OP_Copy at %d\n", iStart); |
| 161453 | + } |
| 161454 | +#endif |
| 160992 | 161455 | pOp->opcode = OP_Copy; |
| 160993 | 161456 | pOp->p1 = pOp->p2 + iRegister; |
| 160994 | 161457 | pOp->p2 = pOp->p3; |
| 160995 | 161458 | pOp->p3 = 0; |
| 160996 | 161459 | pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */ |
| 160997 | 161460 | }else if( pOp->opcode==OP_Rowid ){ |
| 161461 | +#ifdef SQLITE_DEBUG |
| 161462 | + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
| 161463 | + printf("TRANSLATE OP_Rowid to OP_Sequence at %d\n", iStart); |
| 161464 | + } |
| 161465 | +#endif |
| 160998 | 161466 | pOp->opcode = OP_Sequence; |
| 160999 | 161467 | pOp->p1 = iAutoidxCur; |
| 161000 | 161468 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 161001 | 161469 | if( iAutoidxCur==0 ){ |
| 161002 | 161470 | pOp->opcode = OP_Null; |
| | @@ -162319,11 +162787,12 @@ |
| 162319 | 162787 | nNew = sqlite3LogEst(iUpper - iLower); |
| 162320 | 162788 | /* TUNING: If both iUpper and iLower are derived from the same |
| 162321 | 162789 | ** sample, then assume they are 4x more selective. This brings |
| 162322 | 162790 | ** the estimated selectivity more in line with what it would be |
| 162323 | 162791 | ** if estimated without the use of STAT4 tables. */ |
| 162324 | | - if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
| 162792 | + if( iLwrIdx==iUprIdx ){ nNew -= 20; } |
| 162793 | + assert( 20==sqlite3LogEst(4) ); |
| 162325 | 162794 | }else{ |
| 162326 | 162795 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 162327 | 162796 | } |
| 162328 | 162797 | if( nNew<nOut ){ |
| 162329 | 162798 | nOut = nNew; |
| | @@ -162543,21 +163012,38 @@ |
| 162543 | 163012 | #endif |
| 162544 | 163013 | |
| 162545 | 163014 | #ifdef WHERETRACE_ENABLED |
| 162546 | 163015 | /* |
| 162547 | 163016 | ** Print a WhereLoop object for debugging purposes |
| 163017 | +** |
| 163018 | +** Format example: |
| 163019 | +** |
| 163020 | +** .--- Position in WHERE clause rSetup, rRun, nOut ---. |
| 163021 | +** | | |
| 163022 | +** | .--- selfMask nTerm ------. | |
| 163023 | +** | | | | |
| 163024 | +** | | .-- prereq Idx wsFlags----. | | |
| 163025 | +** | | | Name | | | |
| 163026 | +** | | | __|__ nEq ---. ___|__ | __|__ |
| 163027 | +** | / \ / \ / \ | / \ / \ / \ |
| 163028 | +** 1.002.001 t2.t2xy 2 f 010241 N 2 cost 0,56,31 |
| 162548 | 163029 | */ |
| 162549 | | -SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 162550 | | - WhereInfo *pWInfo = pWC->pWInfo; |
| 162551 | | - int nb = 1+(pWInfo->pTabList->nSrc+3)/4; |
| 162552 | | - SrcItem *pItem = pWInfo->pTabList->a + p->iTab; |
| 162553 | | - Table *pTab = pItem->pTab; |
| 162554 | | - Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; |
| 162555 | | - sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
| 162556 | | - p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); |
| 162557 | | - sqlite3DebugPrintf(" %12s", |
| 162558 | | - pItem->zAlias ? pItem->zAlias : pTab->zName); |
| 163030 | +SQLITE_PRIVATE void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC){ |
| 163031 | + if( pWC ){ |
| 163032 | + WhereInfo *pWInfo = pWC->pWInfo; |
| 163033 | + int nb = 1+(pWInfo->pTabList->nSrc+3)/4; |
| 163034 | + SrcItem *pItem = pWInfo->pTabList->a + p->iTab; |
| 163035 | + Table *pTab = pItem->pTab; |
| 163036 | + Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; |
| 163037 | + sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
| 163038 | + p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); |
| 163039 | + sqlite3DebugPrintf(" %12s", |
| 163040 | + pItem->zAlias ? pItem->zAlias : pTab->zName); |
| 163041 | + }else{ |
| 163042 | + sqlite3DebugPrintf("%c%2d.%03llx.%03llx %c%d", |
| 163043 | + p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab); |
| 163044 | + } |
| 162559 | 163045 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 162560 | 163046 | const char *zName; |
| 162561 | 163047 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
| 162562 | 163048 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 162563 | 163049 | int i = sqlite3Strlen30(zName) - 1; |
| | @@ -162589,10 +163075,19 @@ |
| 162589 | 163075 | int i; |
| 162590 | 163076 | for(i=0; i<p->nLTerm; i++){ |
| 162591 | 163077 | sqlite3WhereTermPrint(p->aLTerm[i], i); |
| 162592 | 163078 | } |
| 162593 | 163079 | } |
| 163080 | +} |
| 163081 | +SQLITE_PRIVATE void sqlite3ShowWhereLoop(const WhereLoop *p){ |
| 163082 | + if( p ) sqlite3WhereLoopPrint(p, 0); |
| 163083 | +} |
| 163084 | +SQLITE_PRIVATE void sqlite3ShowWhereLoopList(const WhereLoop *p){ |
| 163085 | + while( p ){ |
| 163086 | + sqlite3ShowWhereLoop(p); |
| 163087 | + p = p->pNextLoop; |
| 163088 | + } |
| 162594 | 163089 | } |
| 162595 | 163090 | #endif |
| 162596 | 163091 | |
| 162597 | 163092 | /* |
| 162598 | 163093 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| | @@ -162702,50 +163197,64 @@ |
| 162702 | 163197 | } |
| 162703 | 163198 | sqlite3DbNNFreeNN(db, pWInfo); |
| 162704 | 163199 | } |
| 162705 | 163200 | |
| 162706 | 163201 | /* |
| 162707 | | -** Return TRUE if all of the following are true: |
| 162708 | | -** |
| 162709 | | -** (1) X has the same or lower cost, or returns the same or fewer rows, |
| 162710 | | -** than Y. |
| 162711 | | -** (2) X uses fewer WHERE clause terms than Y |
| 162712 | | -** (3) Every WHERE clause term used by X is also used by Y |
| 162713 | | -** (4) X skips at least as many columns as Y |
| 162714 | | -** (5) If X is a covering index, than Y is too |
| 162715 | | -** |
| 162716 | | -** Conditions (2) and (3) mean that X is a "proper subset" of Y. |
| 162717 | | -** If X is a proper subset of Y then Y is a better choice and ought |
| 162718 | | -** to have a lower cost. This routine returns TRUE when that cost |
| 162719 | | -** relationship is inverted and needs to be adjusted. Constraint (4) |
| 162720 | | -** was added because if X uses skip-scan less than Y it still might |
| 162721 | | -** deserve a lower cost even if it is a proper subset of Y. Constraint (5) |
| 162722 | | -** was added because a covering index probably deserves to have a lower cost |
| 162723 | | -** than a non-covering index even if it is a proper subset. |
| 163202 | +** Return TRUE if X is a proper subset of Y but is of equal or less cost. |
| 163203 | +** In other words, return true if all constraints of X are also part of Y |
| 163204 | +** and Y has additional constraints that might speed the search that X lacks |
| 163205 | +** but the cost of running X is not more than the cost of running Y. |
| 163206 | +** |
| 163207 | +** In other words, return true if the cost relationwship between X and Y |
| 163208 | +** is inverted and needs to be adjusted. |
| 163209 | +** |
| 163210 | +** Case 1: |
| 163211 | +** |
| 163212 | +** (1a) X and Y use the same index. |
| 163213 | +** (1b) X has fewer == terms than Y |
| 163214 | +** (1c) Neither X nor Y use skip-scan |
| 163215 | +** (1d) X does not have a a greater cost than Y |
| 163216 | +** |
| 163217 | +** Case 2: |
| 163218 | +** |
| 163219 | +** (2a) X has the same or lower cost, or returns the same or fewer rows, |
| 163220 | +** than Y. |
| 163221 | +** (2b) X uses fewer WHERE clause terms than Y |
| 163222 | +** (2c) Every WHERE clause term used by X is also used by Y |
| 163223 | +** (2d) X skips at least as many columns as Y |
| 163224 | +** (2e) If X is a covering index, than Y is too |
| 162724 | 163225 | */ |
| 162725 | 163226 | static int whereLoopCheaperProperSubset( |
| 162726 | 163227 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 162727 | 163228 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 162728 | 163229 | ){ |
| 162729 | 163230 | int i, j; |
| 163231 | + if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */ |
| 163232 | + assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 163233 | + assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 163234 | + if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */ |
| 163235 | + && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */ |
| 163236 | + && pX->nSkip==0 && pY->nSkip==0 /* (1c) */ |
| 163237 | + ){ |
| 163238 | + return 1; /* Case 1 is true */ |
| 163239 | + } |
| 162730 | 163240 | if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 162731 | | - return 0; /* X is not a subset of Y */ |
| 163241 | + return 0; /* (2b) */ |
| 162732 | 163242 | } |
| 162733 | | - if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; |
| 162734 | | - if( pY->nSkip > pX->nSkip ) return 0; |
| 163243 | + if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */ |
| 162735 | 163244 | for(i=pX->nLTerm-1; i>=0; i--){ |
| 162736 | 163245 | if( pX->aLTerm[i]==0 ) continue; |
| 162737 | 163246 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 162738 | 163247 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 162739 | 163248 | } |
| 162740 | | - if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 163249 | + if( j<0 ) return 0; /* (2c) */ |
| 162741 | 163250 | } |
| 162742 | 163251 | if( (pX->wsFlags&WHERE_IDX_ONLY)!=0 |
| 162743 | 163252 | && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){ |
| 162744 | | - return 0; /* Constraint (5) */ |
| 163253 | + return 0; /* (2e) */ |
| 162745 | 163254 | } |
| 162746 | | - return 1; /* All conditions meet */ |
| 163255 | + return 1; /* Case 2 is true */ |
| 162747 | 163256 | } |
| 162748 | 163257 | |
| 162749 | 163258 | /* |
| 162750 | 163259 | ** Try to adjust the cost and number of output rows of WhereLoop pTemplate |
| 162751 | 163260 | ** upwards or downwards so that: |
| | @@ -163231,11 +163740,14 @@ |
| 163231 | 163740 | opMask = WO_LT|WO_LE; |
| 163232 | 163741 | }else{ |
| 163233 | 163742 | assert( pNew->u.btree.nBtm==0 ); |
| 163234 | 163743 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; |
| 163235 | 163744 | } |
| 163236 | | - if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 163745 | + if( pProbe->bUnordered || pProbe->bLowQual ){ |
| 163746 | + if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 163747 | + if( pProbe->bLowQual ) opMask &= ~(WO_EQ|WO_IN|WO_IS); |
| 163748 | + } |
| 163237 | 163749 | |
| 163238 | 163750 | assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 163239 | 163751 | assert( pNew->u.btree.nEq<pProbe->nKeyCol |
| 163240 | 163752 | || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY ); |
| 163241 | 163753 | |
| | @@ -166311,11 +166823,14 @@ |
| 166311 | 166823 | db = pParse->db; |
| 166312 | 166824 | memset(&sWLB, 0, sizeof(sWLB)); |
| 166313 | 166825 | |
| 166314 | 166826 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 166315 | 166827 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 166316 | | - if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
| 166828 | + if( pOrderBy && pOrderBy->nExpr>=BMS ){ |
| 166829 | + pOrderBy = 0; |
| 166830 | + wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 166831 | + } |
| 166317 | 166832 | |
| 166318 | 166833 | /* The number of tables in the FROM clause is limited by the number of |
| 166319 | 166834 | ** bits in a Bitmask |
| 166320 | 166835 | */ |
| 166321 | 166836 | testcase( pTabList->nSrc==BMS ); |
| | @@ -166336,11 +166851,14 @@ |
| 166336 | 166851 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 166337 | 166852 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 166338 | 166853 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 166339 | 166854 | ** some architectures. Hence the ROUND8() below. |
| 166340 | 166855 | */ |
| 166341 | | - nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 166856 | + nByteWInfo = ROUND8P(sizeof(WhereInfo)); |
| 166857 | + if( nTabList>1 ){ |
| 166858 | + nByteWInfo = ROUND8P(nByteWInfo + (nTabList-1)*sizeof(WhereLevel)); |
| 166859 | + } |
| 166342 | 166860 | pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop)); |
| 166343 | 166861 | if( db->mallocFailed ){ |
| 166344 | 166862 | sqlite3DbFree(db, pWInfo); |
| 166345 | 166863 | pWInfo = 0; |
| 166346 | 166864 | goto whereBeginError; |
| | @@ -166898,10 +167416,15 @@ |
| 166898 | 167416 | whereBeginError: |
| 166899 | 167417 | if( pWInfo ){ |
| 166900 | 167418 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 166901 | 167419 | whereInfoFree(db, pWInfo); |
| 166902 | 167420 | } |
| 167421 | +#ifdef WHERETRACE_ENABLED |
| 167422 | + /* Prevent harmless compiler warnings about debugging routines |
| 167423 | + ** being declared but never used */ |
| 167424 | + sqlite3ShowWhereLoopList(0); |
| 167425 | +#endif /* WHERETRACE_ENABLED */ |
| 166903 | 167426 | return 0; |
| 166904 | 167427 | } |
| 166905 | 167428 | |
| 166906 | 167429 | /* |
| 166907 | 167430 | ** Part of sqlite3WhereEnd() will rewrite opcodes to reference the |
| | @@ -182234,10 +182757,32 @@ |
| 182234 | 182757 | rc = SQLITE_NOTFOUND; |
| 182235 | 182758 | } |
| 182236 | 182759 | break; |
| 182237 | 182760 | } |
| 182238 | 182761 | #endif |
| 182762 | + |
| 182763 | + /* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff); |
| 182764 | + ** |
| 182765 | + ** Activate or deactivate validation of JSONB that is generated from |
| 182766 | + ** text. Off by default, as the validation is slow. Validation is |
| 182767 | + ** only available if compiled using SQLITE_DEBUG. |
| 182768 | + ** |
| 182769 | + ** If onOff is initially 1, then turn it on. If onOff is initially |
| 182770 | + ** off, turn it off. If onOff is initially -1, then change onOff |
| 182771 | + ** to be the current setting. |
| 182772 | + */ |
| 182773 | + case SQLITE_TESTCTRL_JSON_SELFCHECK: { |
| 182774 | +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) |
| 182775 | + int *pOnOff = va_arg(ap, int*); |
| 182776 | + if( *pOnOff<0 ){ |
| 182777 | + *pOnOff = sqlite3Config.bJsonSelfcheck; |
| 182778 | + }else{ |
| 182779 | + sqlite3Config.bJsonSelfcheck = (u8)((*pOnOff)&0xff); |
| 182780 | + } |
| 182781 | +#endif |
| 182782 | + break; |
| 182783 | + } |
| 182239 | 182784 | } |
| 182240 | 182785 | va_end(ap); |
| 182241 | 182786 | #endif /* SQLITE_UNTESTABLE */ |
| 182242 | 182787 | return rc; |
| 182243 | 182788 | } |
| | @@ -184217,10 +184762,12 @@ |
| 184217 | 184762 | SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int); |
| 184218 | 184763 | SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int); |
| 184219 | 184764 | #endif |
| 184220 | 184765 | |
| 184221 | 184766 | SQLITE_PRIVATE int sqlite3Fts3ExprIterate(Fts3Expr*, int (*x)(Fts3Expr*,int,void*), void*); |
| 184767 | + |
| 184768 | +SQLITE_PRIVATE int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk); |
| 184222 | 184769 | |
| 184223 | 184770 | #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */ |
| 184224 | 184771 | #endif /* _FTSINT_H */ |
| 184225 | 184772 | |
| 184226 | 184773 | /************** End of fts3Int.h *********************************************/ |
| | @@ -187940,42 +188487,33 @@ |
| 187940 | 188487 | |
| 187941 | 188488 | /* |
| 187942 | 188489 | ** Implementation of the xIntegrity() method on the FTS3/FTS4 virtual |
| 187943 | 188490 | ** table. |
| 187944 | 188491 | */ |
| 187945 | | -static int fts3Integrity( |
| 188492 | +static int fts3IntegrityMethod( |
| 187946 | 188493 | sqlite3_vtab *pVtab, /* The virtual table to be checked */ |
| 187947 | 188494 | const char *zSchema, /* Name of schema in which pVtab lives */ |
| 187948 | 188495 | const char *zTabname, /* Name of the pVTab table */ |
| 187949 | 188496 | int isQuick, /* True if this is a quick_check */ |
| 187950 | 188497 | char **pzErr /* Write error message here */ |
| 187951 | 188498 | ){ |
| 187952 | 188499 | Fts3Table *p = (Fts3Table*)pVtab; |
| 187953 | | - char *zSql; |
| 187954 | 188500 | int rc; |
| 187955 | | - char *zErr = 0; |
| 188501 | + int bOk = 0; |
| 187956 | 188502 | |
| 187957 | | - assert( pzErr!=0 ); |
| 187958 | | - assert( *pzErr==0 ); |
| 187959 | 188503 | UNUSED_PARAMETER(isQuick); |
| 187960 | | - zSql = sqlite3_mprintf( |
| 187961 | | - "INSERT INTO \"%w\".\"%w\"(\"%w\") VALUES('integrity-check');", |
| 187962 | | - zSchema, zTabname, zTabname); |
| 187963 | | - if( zSql==0 ){ |
| 187964 | | - return SQLITE_NOMEM; |
| 187965 | | - } |
| 187966 | | - rc = sqlite3_exec(p->db, zSql, 0, 0, &zErr); |
| 187967 | | - sqlite3_free(zSql); |
| 187968 | | - if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 188504 | + rc = sqlite3Fts3IntegrityCheck(p, &bOk); |
| 188505 | + assert( rc!=SQLITE_CORRUPT_VTAB || bOk==0 ); |
| 188506 | + if( rc!=SQLITE_OK && rc!=SQLITE_CORRUPT_VTAB ){ |
| 188507 | + *pzErr = sqlite3_mprintf("unable to validate the inverted index for" |
| 188508 | + " FTS%d table %s.%s: %s", |
| 188509 | + p->bFts4 ? 4 : 3, zSchema, zTabname, sqlite3_errstr(rc)); |
| 188510 | + }else if( bOk==0 ){ |
| 187969 | 188511 | *pzErr = sqlite3_mprintf("malformed inverted index for FTS%d table %s.%s", |
| 187970 | 188512 | p->bFts4 ? 4 : 3, zSchema, zTabname); |
| 187971 | | - }else if( rc!=SQLITE_OK ){ |
| 187972 | | - *pzErr = sqlite3_mprintf("unable to validate the inverted index for" |
| 187973 | | - " FTS%d table %s.%s: %s", |
| 187974 | | - p->bFts4 ? 4 : 3, zSchema, zTabname, zErr); |
| 187975 | 188513 | } |
| 187976 | | - sqlite3_free(zErr); |
| 188514 | + sqlite3Fts3SegmentsClose(p); |
| 187977 | 188515 | return SQLITE_OK; |
| 187978 | 188516 | } |
| 187979 | 188517 | |
| 187980 | 188518 | |
| 187981 | 188519 | |
| | @@ -188002,11 +188540,11 @@ |
| 188002 | 188540 | /* xRename */ fts3RenameMethod, |
| 188003 | 188541 | /* xSavepoint */ fts3SavepointMethod, |
| 188004 | 188542 | /* xRelease */ fts3ReleaseMethod, |
| 188005 | 188543 | /* xRollbackTo */ fts3RollbackToMethod, |
| 188006 | 188544 | /* xShadowName */ fts3ShadowName, |
| 188007 | | - /* xIntegrity */ fts3Integrity, |
| 188545 | + /* xIntegrity */ fts3IntegrityMethod, |
| 188008 | 188546 | }; |
| 188009 | 188547 | |
| 188010 | 188548 | /* |
| 188011 | 188549 | ** This function is registered as the module destructor (called when an |
| 188012 | 188550 | ** FTS3 enabled database connection is closed). It frees the memory |
| | @@ -199556,11 +200094,11 @@ |
| 199556 | 200094 | ** to false before returning. |
| 199557 | 200095 | ** |
| 199558 | 200096 | ** If an error occurs (e.g. an OOM or IO error), return an SQLite error |
| 199559 | 200097 | ** code. The final value of *pbOk is undefined in this case. |
| 199560 | 200098 | */ |
| 199561 | | -static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){ |
| 200099 | +SQLITE_PRIVATE int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk){ |
| 199562 | 200100 | int rc = SQLITE_OK; /* Return code */ |
| 199563 | 200101 | u64 cksum1 = 0; /* Checksum based on FTS index contents */ |
| 199564 | 200102 | u64 cksum2 = 0; /* Checksum based on %_content contents */ |
| 199565 | 200103 | sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */ |
| 199566 | 200104 | |
| | @@ -199634,11 +200172,11 @@ |
| 199634 | 200172 | } |
| 199635 | 200173 | |
| 199636 | 200174 | sqlite3_finalize(pStmt); |
| 199637 | 200175 | } |
| 199638 | 200176 | |
| 199639 | | - *pbOk = (cksum1==cksum2); |
| 200177 | + *pbOk = (rc==SQLITE_OK && cksum1==cksum2); |
| 199640 | 200178 | return rc; |
| 199641 | 200179 | } |
| 199642 | 200180 | |
| 199643 | 200181 | /* |
| 199644 | 200182 | ** Run the integrity-check. If no error occurs and the current contents of |
| | @@ -199674,11 +200212,11 @@ |
| 199674 | 200212 | static int fts3DoIntegrityCheck( |
| 199675 | 200213 | Fts3Table *p /* FTS3 table handle */ |
| 199676 | 200214 | ){ |
| 199677 | 200215 | int rc; |
| 199678 | 200216 | int bOk = 0; |
| 199679 | | - rc = fts3IntegrityCheck(p, &bOk); |
| 200217 | + rc = sqlite3Fts3IntegrityCheck(p, &bOk); |
| 199680 | 200218 | if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB; |
| 199681 | 200219 | return rc; |
| 199682 | 200220 | } |
| 199683 | 200221 | |
| 199684 | 200222 | /* |
| | @@ -202648,28 +203186,149 @@ |
| 202648 | 203186 | ** May you find forgiveness for yourself and forgive others. |
| 202649 | 203187 | ** May you share freely, never taking more than you give. |
| 202650 | 203188 | ** |
| 202651 | 203189 | ****************************************************************************** |
| 202652 | 203190 | ** |
| 202653 | | -** This SQLite JSON functions. |
| 203191 | +** SQLite JSON functions. |
| 202654 | 203192 | ** |
| 202655 | 203193 | ** This file began as an extension in ext/misc/json1.c in 2015. That |
| 202656 | 203194 | ** extension proved so useful that it has now been moved into the core. |
| 202657 | 203195 | ** |
| 202658 | | -** For the time being, all JSON is stored as pure text. (We might add |
| 202659 | | -** a JSONB type in the future which stores a binary encoding of JSON in |
| 202660 | | -** a BLOB, but there is no support for JSONB in the current implementation. |
| 202661 | | -** This implementation parses JSON text at 250 MB/s, so it is hard to see |
| 202662 | | -** how JSONB might improve on that.) |
| 203196 | +** The original design stored all JSON as pure text, canonical RFC-8259. |
| 203197 | +** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16). |
| 203198 | +** All generated JSON text still conforms strictly to RFC-8259, but text |
| 203199 | +** with JSON-5 extensions is accepted as input. |
| 203200 | +** |
| 203201 | +** Beginning with version 3.45.0 (circa 2024-01-01), these routines also |
| 203202 | +** accept BLOB values that have JSON encoded using a binary representation |
| 203203 | +** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk |
| 203204 | +** format SQLite JSONB is completely different and incompatible with |
| 203205 | +** PostgreSQL JSONB. |
| 203206 | +** |
| 203207 | +** Decoding and interpreting JSONB is still O(N) where N is the size of |
| 203208 | +** the input, the same as text JSON. However, the constant of proportionality |
| 203209 | +** for JSONB is much smaller due to faster parsing. The size of each |
| 203210 | +** element in JSONB is encoded in its header, so there is no need to search |
| 203211 | +** for delimiters using persnickety syntax rules. JSONB seems to be about |
| 203212 | +** 3x faster than text JSON as a result. JSONB is also tends to be slightly |
| 203213 | +** smaller than text JSON, by 5% or 10%, but there are corner cases where |
| 203214 | +** JSONB can be slightly larger. So you are not far mistaken to say that |
| 203215 | +** a JSONB blob is the same size as the equivalent RFC-8259 text. |
| 203216 | +** |
| 203217 | +** |
| 203218 | +** THE JSONB ENCODING: |
| 203219 | +** |
| 203220 | +** Every JSON element is encoded in JSONB as a header and a payload. |
| 203221 | +** The header is between 1 and 9 bytes in size. The payload is zero |
| 203222 | +** or more bytes. |
| 203223 | +** |
| 203224 | +** The lower 4 bits of the first byte of the header determines the |
| 203225 | +** element type: |
| 203226 | +** |
| 203227 | +** 0: NULL |
| 203228 | +** 1: TRUE |
| 203229 | +** 2: FALSE |
| 203230 | +** 3: INT -- RFC-8259 integer literal |
| 203231 | +** 4: INT5 -- JSON5 integer literal |
| 203232 | +** 5: FLOAT -- RFC-8259 floating point literal |
| 203233 | +** 6: FLOAT5 -- JSON5 floating point literal |
| 203234 | +** 7: TEXT -- Text literal acceptable to both SQL and JSON |
| 203235 | +** 8: TEXTJ -- Text containing RFC-8259 escapes |
| 203236 | +** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes |
| 203237 | +** 10: TEXTRAW -- Text containing unescaped syntax characters |
| 203238 | +** 11: ARRAY |
| 203239 | +** 12: OBJECT |
| 203240 | +** |
| 203241 | +** The other three possible values (13-15) are reserved for future |
| 203242 | +** enhancements. |
| 203243 | +** |
| 203244 | +** The upper 4 bits of the first byte determine the size of the header |
| 203245 | +** and sometimes also the size of the payload. If X is the first byte |
| 203246 | +** of the element and if X>>4 is between 0 and 11, then the payload |
| 203247 | +** will be that many bytes in size and the header is exactly one byte |
| 203248 | +** in size. Other four values for X>>4 (12-15) indicate that the header |
| 203249 | +** is more than one byte in size and that the payload size is determined |
| 203250 | +** by the remainder of the header, interpreted as a unsigned big-endian |
| 203251 | +** integer. |
| 203252 | +** |
| 203253 | +** Value of X>>4 Size integer Total header size |
| 203254 | +** ------------- -------------------- ----------------- |
| 203255 | +** 12 1 byte (0-255) 2 |
| 203256 | +** 13 2 byte (0-65535) 3 |
| 203257 | +** 14 4 byte (0-4294967295) 5 |
| 203258 | +** 15 8 byte (0-1.8e19) 9 |
| 203259 | +** |
| 203260 | +** The payload size need not be expressed in its minimal form. For example, |
| 203261 | +** if the payload size is 10, the size can be expressed in any of 5 different |
| 203262 | +** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by on 0x0a byte, |
| 203263 | +** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by |
| 203264 | +** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and |
| 203265 | +** a single byte of 0x0a. The shorter forms are preferred, of course, but |
| 203266 | +** sometimes when generating JSONB, the payload size is not known in advance |
| 203267 | +** and it is convenient to reserve sufficient header space to cover the |
| 203268 | +** largest possible payload size and then come back later and patch up |
| 203269 | +** the size when it becomes known, resulting in a non-minimal encoding. |
| 203270 | +** |
| 203271 | +** The value (X>>4)==15 is not actually used in the current implementation |
| 203272 | +** (as SQLite is currently unable handle BLOBs larger than about 2GB) |
| 203273 | +** but is included in the design to allow for future enhancements. |
| 203274 | +** |
| 203275 | +** The payload follows the header. NULL, TRUE, and FALSE have no payload and |
| 203276 | +** their payload size must always be zero. The payload for INT, INT5, |
| 203277 | +** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the |
| 203278 | +** "..." or '...' delimiters are omitted from the various text encodings. |
| 203279 | +** The payload for ARRAY and OBJECT is a list of additional elements that |
| 203280 | +** are the content for the array or object. The payload for an OBJECT |
| 203281 | +** must be an even number of elements. The first element of each pair is |
| 203282 | +** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW. |
| 203283 | +** |
| 203284 | +** A valid JSONB blob consists of a single element, as described above. |
| 203285 | +** Usually this will be an ARRAY or OBJECT element which has many more |
| 203286 | +** elements as its content. But the overall blob is just a single element. |
| 203287 | +** |
| 203288 | +** Input validation for JSONB blobs simply checks that the element type |
| 203289 | +** code is between 0 and 12 and that the total size of the element |
| 203290 | +** (header plus payload) is the same as the size of the BLOB. If those |
| 203291 | +** checks are true, the BLOB is assumed to be JSONB and processing continues. |
| 203292 | +** Errors are only raised if some other miscoding is discovered during |
| 203293 | +** processing. |
| 203294 | +** |
| 203295 | +** Additional information can be found in the doc/jsonb.md file of the |
| 203296 | +** canonical SQLite source tree. |
| 202663 | 203297 | */ |
| 202664 | 203298 | #ifndef SQLITE_OMIT_JSON |
| 202665 | 203299 | /* #include "sqliteInt.h" */ |
| 202666 | 203300 | |
| 203301 | +/* JSONB element types |
| 203302 | +*/ |
| 203303 | +#define JSONB_NULL 0 /* "null" */ |
| 203304 | +#define JSONB_TRUE 1 /* "true" */ |
| 203305 | +#define JSONB_FALSE 2 /* "false" */ |
| 203306 | +#define JSONB_INT 3 /* integer acceptable to JSON and SQL */ |
| 203307 | +#define JSONB_INT5 4 /* integer in 0x000 notation */ |
| 203308 | +#define JSONB_FLOAT 5 /* float acceptable to JSON and SQL */ |
| 203309 | +#define JSONB_FLOAT5 6 /* float with JSON5 extensions */ |
| 203310 | +#define JSONB_TEXT 7 /* Text compatible with both JSON and SQL */ |
| 203311 | +#define JSONB_TEXTJ 8 /* Text with JSON escapes */ |
| 203312 | +#define JSONB_TEXT5 9 /* Text with JSON-5 escape */ |
| 203313 | +#define JSONB_TEXTRAW 10 /* SQL text that needs escaping for JSON */ |
| 203314 | +#define JSONB_ARRAY 11 /* An array */ |
| 203315 | +#define JSONB_OBJECT 12 /* An object */ |
| 203316 | + |
| 203317 | +/* Human-readable names for the JSONB values. The index for each |
| 203318 | +** string must correspond to the JSONB_* integer above. |
| 203319 | +*/ |
| 203320 | +static const char * const jsonbType[] = { |
| 203321 | + "null", "true", "false", "integer", "integer", |
| 203322 | + "real", "real", "text", "text", "text", |
| 203323 | + "text", "array", "object", "", "", "", "" |
| 203324 | +}; |
| 203325 | + |
| 202667 | 203326 | /* |
| 202668 | 203327 | ** Growing our own isspace() routine this way is twice as fast as |
| 202669 | 203328 | ** the library isspace() function, resulting in a 7% overall performance |
| 202670 | | -** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). |
| 203329 | +** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). |
| 202671 | 203330 | */ |
| 202672 | 203331 | static const char jsonIsSpace[] = { |
| 202673 | 203332 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, |
| 202674 | 203333 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202675 | 203334 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| | @@ -202686,15 +203345,23 @@ |
| 202686 | 203345 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202687 | 203346 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202688 | 203347 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202689 | 203348 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202690 | 203349 | }; |
| 202691 | | -#define fast_isspace(x) (jsonIsSpace[(unsigned char)x]) |
| 203350 | +#define jsonIsspace(x) (jsonIsSpace[(unsigned char)x]) |
| 202692 | 203351 | |
| 202693 | 203352 | /* |
| 202694 | | -** Characters that are special to JSON. Control charaters, |
| 202695 | | -** '"' and '\\'. |
| 203353 | +** The set of all space characters recognized by jsonIsspace(). |
| 203354 | +** Useful as the second argument to strspn(). |
| 203355 | +*/ |
| 203356 | +static const char jsonSpaces[] = "\011\012\015\040"; |
| 203357 | + |
| 203358 | +/* |
| 203359 | +** Characters that are special to JSON. Control characters, |
| 203360 | +** '"' and '\\' and '\''. Actually, '\'' is not special to |
| 203361 | +** canonical JSON, but it is special in JSON-5, so we include |
| 203362 | +** it in the set of special characters. |
| 202696 | 203363 | */ |
| 202697 | 203364 | static const char jsonIsOk[256] = { |
| 202698 | 203365 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202699 | 203366 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 202700 | 203367 | 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, |
| | @@ -202712,247 +203379,364 @@ |
| 202712 | 203379 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 202713 | 203380 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 202714 | 203381 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 |
| 202715 | 203382 | }; |
| 202716 | 203383 | |
| 202717 | | - |
| 202718 | | -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST) |
| 202719 | | -# define VVA(X) |
| 202720 | | -#else |
| 202721 | | -# define VVA(X) X |
| 202722 | | -#endif |
| 202723 | | - |
| 202724 | 203384 | /* Objects */ |
| 203385 | +typedef struct JsonCache JsonCache; |
| 202725 | 203386 | typedef struct JsonString JsonString; |
| 202726 | | -typedef struct JsonNode JsonNode; |
| 202727 | 203387 | typedef struct JsonParse JsonParse; |
| 202728 | | -typedef struct JsonCleanup JsonCleanup; |
| 203388 | + |
| 203389 | +/* |
| 203390 | +** Magic number used for the JSON parse cache in sqlite3_get_auxdata() |
| 203391 | +*/ |
| 203392 | +#define JSON_CACHE_ID (-429938) /* Cache entry */ |
| 203393 | +#define JSON_CACHE_SIZE 4 /* Max number of cache entries */ |
| 203394 | + |
| 203395 | +/* |
| 203396 | +** jsonUnescapeOneChar() returns this invalid code point if it encounters |
| 203397 | +** a syntax error. |
| 203398 | +*/ |
| 203399 | +#define JSON_INVALID_CHAR 0x99999 |
| 203400 | + |
| 203401 | +/* A cache mapping JSON text into JSONB blobs. |
| 203402 | +** |
| 203403 | +** Each cache entry is a JsonParse object with the following restrictions: |
| 203404 | +** |
| 203405 | +** * The bReadOnly flag must be set |
| 203406 | +** |
| 203407 | +** * The aBlob[] array must be owned by the JsonParse object. In other |
| 203408 | +** words, nBlobAlloc must be non-zero. |
| 203409 | +** |
| 203410 | +** * eEdit and delta must be zero. |
| 203411 | +** |
| 203412 | +** * zJson must be an RCStr. In other words bJsonIsRCStr must be true. |
| 203413 | +*/ |
| 203414 | +struct JsonCache { |
| 203415 | + sqlite3 *db; /* Database connection */ |
| 203416 | + int nUsed; /* Number of active entries in the cache */ |
| 203417 | + JsonParse *a[JSON_CACHE_SIZE]; /* One line for each cache entry */ |
| 203418 | +}; |
| 202729 | 203419 | |
| 202730 | 203420 | /* An instance of this object represents a JSON string |
| 202731 | 203421 | ** under construction. Really, this is a generic string accumulator |
| 202732 | 203422 | ** that can be and is used to create strings other than JSON. |
| 203423 | +** |
| 203424 | +** If the generated string is longer than will fit into the zSpace[] buffer, |
| 203425 | +** then it will be an RCStr string. This aids with caching of large |
| 203426 | +** JSON strings. |
| 202733 | 203427 | */ |
| 202734 | 203428 | struct JsonString { |
| 202735 | 203429 | sqlite3_context *pCtx; /* Function context - put error messages here */ |
| 202736 | 203430 | char *zBuf; /* Append JSON content here */ |
| 202737 | 203431 | u64 nAlloc; /* Bytes of storage available in zBuf[] */ |
| 202738 | 203432 | u64 nUsed; /* Bytes of zBuf[] currently used */ |
| 202739 | 203433 | u8 bStatic; /* True if zBuf is static space */ |
| 202740 | | - u8 bErr; /* True if an error has been encountered */ |
| 203434 | + u8 eErr; /* True if an error has been encountered */ |
| 202741 | 203435 | char zSpace[100]; /* Initial static space */ |
| 202742 | 203436 | }; |
| 202743 | 203437 | |
| 202744 | | -/* A deferred cleanup task. A list of JsonCleanup objects might be |
| 202745 | | -** run when the JsonParse object is destroyed. |
| 202746 | | -*/ |
| 202747 | | -struct JsonCleanup { |
| 202748 | | - JsonCleanup *pJCNext; /* Next in a list */ |
| 202749 | | - void (*xOp)(void*); /* Routine to run */ |
| 202750 | | - void *pArg; /* Argument to xOp() */ |
| 202751 | | -}; |
| 202752 | | - |
| 202753 | | -/* JSON type values |
| 202754 | | -*/ |
| 202755 | | -#define JSON_SUBST 0 /* Special edit node. Uses u.iPrev */ |
| 202756 | | -#define JSON_NULL 1 |
| 202757 | | -#define JSON_TRUE 2 |
| 202758 | | -#define JSON_FALSE 3 |
| 202759 | | -#define JSON_INT 4 |
| 202760 | | -#define JSON_REAL 5 |
| 202761 | | -#define JSON_STRING 6 |
| 202762 | | -#define JSON_ARRAY 7 |
| 202763 | | -#define JSON_OBJECT 8 |
| 202764 | | - |
| 202765 | | -/* The "subtype" set for JSON values */ |
| 203438 | +/* Allowed values for JsonString.eErr */ |
| 203439 | +#define JSTRING_OOM 0x01 /* Out of memory */ |
| 203440 | +#define JSTRING_MALFORMED 0x02 /* Malformed JSONB */ |
| 203441 | +#define JSTRING_ERR 0x04 /* Error already sent to sqlite3_result */ |
| 203442 | + |
| 203443 | +/* The "subtype" set for text JSON values passed through using |
| 203444 | +** sqlite3_result_subtype() and sqlite3_value_subtype(). |
| 203445 | +*/ |
| 202766 | 203446 | #define JSON_SUBTYPE 74 /* Ascii for "J" */ |
| 202767 | 203447 | |
| 202768 | 203448 | /* |
| 202769 | | -** Names of the various JSON types: |
| 202770 | | -*/ |
| 202771 | | -static const char * const jsonType[] = { |
| 202772 | | - "subst", |
| 202773 | | - "null", "true", "false", "integer", "real", "text", "array", "object" |
| 202774 | | -}; |
| 202775 | | - |
| 202776 | | -/* Bit values for the JsonNode.jnFlag field |
| 202777 | | -*/ |
| 202778 | | -#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */ |
| 202779 | | -#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */ |
| 202780 | | -#define JNODE_REMOVE 0x04 /* Do not output */ |
| 202781 | | -#define JNODE_REPLACE 0x08 /* Target of a JSON_SUBST node */ |
| 202782 | | -#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */ |
| 202783 | | -#define JNODE_LABEL 0x20 /* Is a label of an object */ |
| 202784 | | -#define JNODE_JSON5 0x40 /* Node contains JSON5 enhancements */ |
| 202785 | | - |
| 202786 | | - |
| 202787 | | -/* A single node of parsed JSON. An array of these nodes describes |
| 202788 | | -** a parse of JSON + edits. |
| 202789 | | -** |
| 202790 | | -** Use the json_parse() SQL function (available when compiled with |
| 202791 | | -** -DSQLITE_DEBUG) to see a dump of complete JsonParse objects, including |
| 202792 | | -** a complete listing and decoding of the array of JsonNodes. |
| 202793 | | -*/ |
| 202794 | | -struct JsonNode { |
| 202795 | | - u8 eType; /* One of the JSON_ type values */ |
| 202796 | | - u8 jnFlags; /* JNODE flags */ |
| 202797 | | - u8 eU; /* Which union element to use */ |
| 202798 | | - u32 n; /* Bytes of content for INT, REAL or STRING |
| 202799 | | - ** Number of sub-nodes for ARRAY and OBJECT |
| 202800 | | - ** Node that SUBST applies to */ |
| 202801 | | - union { |
| 202802 | | - const char *zJContent; /* 1: Content for INT, REAL, and STRING */ |
| 202803 | | - u32 iAppend; /* 2: More terms for ARRAY and OBJECT */ |
| 202804 | | - u32 iKey; /* 3: Key for ARRAY objects in json_tree() */ |
| 202805 | | - u32 iPrev; /* 4: Previous SUBST node, or 0 */ |
| 202806 | | - } u; |
| 202807 | | -}; |
| 202808 | | - |
| 202809 | | - |
| 202810 | | -/* A parsed and possibly edited JSON string. Lifecycle: |
| 202811 | | -** |
| 202812 | | -** 1. JSON comes in and is parsed into an array aNode[]. The original |
| 202813 | | -** JSON text is stored in zJson. |
| 202814 | | -** |
| 202815 | | -** 2. Zero or more changes are made (via json_remove() or json_replace() |
| 202816 | | -** or similar) to the aNode[] array. |
| 202817 | | -** |
| 202818 | | -** 3. A new, edited and mimified JSON string is generated from aNode |
| 202819 | | -** and stored in zAlt. The JsonParse object always owns zAlt. |
| 202820 | | -** |
| 202821 | | -** Step 1 always happens. Step 2 and 3 may or may not happen, depending |
| 202822 | | -** on the operation. |
| 202823 | | -** |
| 202824 | | -** aNode[].u.zJContent entries typically point into zJson. Hence zJson |
| 202825 | | -** must remain valid for the lifespan of the parse. For edits, |
| 202826 | | -** aNode[].u.zJContent might point to malloced space other than zJson. |
| 202827 | | -** Entries in pClup are responsible for freeing that extra malloced space. |
| 202828 | | -** |
| 202829 | | -** When walking the parse tree in aNode[], edits are ignored if useMod is |
| 202830 | | -** false. |
| 203449 | +** Bit values for the flags passed into various SQL function implementations |
| 203450 | +** via the sqlite3_user_data() value. |
| 203451 | +*/ |
| 203452 | +#define JSON_JSON 0x01 /* Result is always JSON */ |
| 203453 | +#define JSON_SQL 0x02 /* Result is always SQL */ |
| 203454 | +#define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */ |
| 203455 | +#define JSON_ISSET 0x04 /* json_set(), not json_insert() */ |
| 203456 | +#define JSON_BLOB 0x08 /* Use the BLOB output format */ |
| 203457 | + |
| 203458 | + |
| 203459 | +/* A parsed JSON value. Lifecycle: |
| 203460 | +** |
| 203461 | +** 1. JSON comes in and is parsed into a JSONB value in aBlob. The |
| 203462 | +** original text is stored in zJson. This step is skipped if the |
| 203463 | +** input is JSONB instead of text JSON. |
| 203464 | +** |
| 203465 | +** 2. The aBlob[] array is searched using the JSON path notation, if needed. |
| 203466 | +** |
| 203467 | +** 3. Zero or more changes are made to aBlob[] (via json_remove() or |
| 203468 | +** json_replace() or json_patch() or similar). |
| 203469 | +** |
| 203470 | +** 4. New JSON text is generated from the aBlob[] for output. This step |
| 203471 | +** is skipped if the function is one of the jsonb_* functions that |
| 203472 | +** returns JSONB instead of text JSON. |
| 202831 | 203473 | */ |
| 202832 | 203474 | struct JsonParse { |
| 202833 | | - u32 nNode; /* Number of slots of aNode[] used */ |
| 202834 | | - u32 nAlloc; /* Number of slots of aNode[] allocated */ |
| 202835 | | - JsonNode *aNode; /* Array of nodes containing the parse */ |
| 202836 | | - char *zJson; /* Original JSON string (before edits) */ |
| 202837 | | - char *zAlt; /* Revised and/or mimified JSON */ |
| 202838 | | - u32 *aUp; /* Index of parent of each node */ |
| 202839 | | - JsonCleanup *pClup;/* Cleanup operations prior to freeing this object */ |
| 203475 | + u8 *aBlob; /* JSONB representation of JSON value */ |
| 203476 | + u32 nBlob; /* Bytes of aBlob[] actually used */ |
| 203477 | + u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */ |
| 203478 | + char *zJson; /* Json text used for parsing */ |
| 203479 | + sqlite3 *db; /* The database connection to which this object belongs */ |
| 203480 | + int nJson; /* Length of the zJson string in bytes */ |
| 203481 | + u32 nJPRef; /* Number of references to this object */ |
| 203482 | + u32 iErr; /* Error location in zJson[] */ |
| 202840 | 203483 | u16 iDepth; /* Nesting depth */ |
| 202841 | 203484 | u8 nErr; /* Number of errors seen */ |
| 202842 | 203485 | u8 oom; /* Set to true if out of memory */ |
| 202843 | 203486 | u8 bJsonIsRCStr; /* True if zJson is an RCStr */ |
| 202844 | 203487 | u8 hasNonstd; /* True if input uses non-standard features like JSON5 */ |
| 202845 | | - u8 useMod; /* Actually use the edits contain inside aNode */ |
| 202846 | | - u8 hasMod; /* aNode contains edits from the original zJson */ |
| 202847 | | - u32 nJPRef; /* Number of references to this object */ |
| 202848 | | - int nJson; /* Length of the zJson string in bytes */ |
| 202849 | | - int nAlt; /* Length of alternative JSON string zAlt, in bytes */ |
| 202850 | | - u32 iErr; /* Error location in zJson[] */ |
| 202851 | | - u32 iSubst; /* Last JSON_SUBST entry in aNode[] */ |
| 202852 | | - u32 iHold; /* Age of this entry in the cache for LRU replacement */ |
| 203488 | + u8 bReadOnly; /* Do not modify. */ |
| 203489 | + /* Search and edit information. See jsonLookupStep() */ |
| 203490 | + u8 eEdit; /* Edit operation to apply */ |
| 203491 | + int delta; /* Size change due to the edit */ |
| 203492 | + u32 nIns; /* Number of bytes to insert */ |
| 203493 | + u32 iLabel; /* Location of label if search landed on an object value */ |
| 203494 | + u8 *aIns; /* Content to be inserted */ |
| 202853 | 203495 | }; |
| 202854 | 203496 | |
| 203497 | +/* Allowed values for JsonParse.eEdit */ |
| 203498 | +#define JEDIT_DEL 1 /* Delete if exists */ |
| 203499 | +#define JEDIT_REPL 2 /* Overwrite if exists */ |
| 203500 | +#define JEDIT_INS 3 /* Insert if not exists */ |
| 203501 | +#define JEDIT_SET 4 /* Insert or overwrite */ |
| 203502 | + |
| 202855 | 203503 | /* |
| 202856 | 203504 | ** Maximum nesting depth of JSON for this implementation. |
| 202857 | 203505 | ** |
| 202858 | 203506 | ** This limit is needed to avoid a stack overflow in the recursive |
| 202859 | 203507 | ** descent parser. A depth of 1000 is far deeper than any sane JSON |
| 202860 | 203508 | ** should go. Historical note: This limit was 2000 prior to version 3.42.0 |
| 202861 | 203509 | */ |
| 202862 | | -#define JSON_MAX_DEPTH 1000 |
| 203510 | +#ifndef SQLITE_JSON_MAX_DEPTH |
| 203511 | +# define JSON_MAX_DEPTH 1000 |
| 203512 | +#else |
| 203513 | +# define JSON_MAX_DEPTH SQLITE_JSON_MAX_DEPTH |
| 203514 | +#endif |
| 203515 | + |
| 203516 | +/* |
| 203517 | +** Allowed values for the flgs argument to jsonParseFuncArg(); |
| 203518 | +*/ |
| 203519 | +#define JSON_EDITABLE 0x01 /* Generate a writable JsonParse object */ |
| 203520 | +#define JSON_KEEPERROR 0x02 /* Return non-NULL even if there is an error */ |
| 203521 | + |
| 203522 | +/************************************************************************** |
| 203523 | +** Forward references |
| 203524 | +**************************************************************************/ |
| 203525 | +static void jsonReturnStringAsBlob(JsonString*); |
| 203526 | +static int jsonFuncArgMightBeBinary(sqlite3_value *pJson); |
| 203527 | +static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*); |
| 203528 | +static void jsonReturnParse(sqlite3_context*,JsonParse*); |
| 203529 | +static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32); |
| 203530 | +static void jsonParseFree(JsonParse*); |
| 203531 | +static u32 jsonbPayloadSize(const JsonParse*, u32, u32*); |
| 203532 | +static u32 jsonUnescapeOneChar(const char*, u32, u32*); |
| 203533 | + |
| 203534 | +/************************************************************************** |
| 203535 | +** Utility routines for dealing with JsonCache objects |
| 203536 | +**************************************************************************/ |
| 203537 | + |
| 203538 | +/* |
| 203539 | +** Free a JsonCache object. |
| 203540 | +*/ |
| 203541 | +static void jsonCacheDelete(JsonCache *p){ |
| 203542 | + int i; |
| 203543 | + for(i=0; i<p->nUsed; i++){ |
| 203544 | + jsonParseFree(p->a[i]); |
| 203545 | + } |
| 203546 | + sqlite3DbFree(p->db, p); |
| 203547 | +} |
| 203548 | +static void jsonCacheDeleteGeneric(void *p){ |
| 203549 | + jsonCacheDelete((JsonCache*)p); |
| 203550 | +} |
| 203551 | + |
| 203552 | +/* |
| 203553 | +** Insert a new entry into the cache. If the cache is full, expel |
| 203554 | +** the least recently used entry. Return SQLITE_OK on success or a |
| 203555 | +** result code otherwise. |
| 203556 | +** |
| 203557 | +** Cache entries are stored in age order, oldest first. |
| 203558 | +*/ |
| 203559 | +static int jsonCacheInsert( |
| 203560 | + sqlite3_context *ctx, /* The SQL statement context holding the cache */ |
| 203561 | + JsonParse *pParse /* The parse object to be added to the cache */ |
| 203562 | +){ |
| 203563 | + JsonCache *p; |
| 203564 | + |
| 203565 | + assert( pParse->zJson!=0 ); |
| 203566 | + assert( pParse->bJsonIsRCStr ); |
| 203567 | + assert( pParse->delta==0 ); |
| 203568 | + p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); |
| 203569 | + if( p==0 ){ |
| 203570 | + sqlite3 *db = sqlite3_context_db_handle(ctx); |
| 203571 | + p = sqlite3DbMallocZero(db, sizeof(*p)); |
| 203572 | + if( p==0 ) return SQLITE_NOMEM; |
| 203573 | + p->db = db; |
| 203574 | + sqlite3_set_auxdata(ctx, JSON_CACHE_ID, p, jsonCacheDeleteGeneric); |
| 203575 | + p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); |
| 203576 | + if( p==0 ) return SQLITE_NOMEM; |
| 203577 | + } |
| 203578 | + if( p->nUsed >= JSON_CACHE_SIZE ){ |
| 203579 | + jsonParseFree(p->a[0]); |
| 203580 | + memmove(p->a, &p->a[1], (JSON_CACHE_SIZE-1)*sizeof(p->a[0])); |
| 203581 | + p->nUsed = JSON_CACHE_SIZE-1; |
| 203582 | + } |
| 203583 | + assert( pParse->nBlobAlloc>0 ); |
| 203584 | + pParse->eEdit = 0; |
| 203585 | + pParse->nJPRef++; |
| 203586 | + pParse->bReadOnly = 1; |
| 203587 | + p->a[p->nUsed] = pParse; |
| 203588 | + p->nUsed++; |
| 203589 | + return SQLITE_OK; |
| 203590 | +} |
| 203591 | + |
| 203592 | +/* |
| 203593 | +** Search for a cached translation the json text supplied by pArg. Return |
| 203594 | +** the JsonParse object if found. Return NULL if not found. |
| 203595 | +** |
| 203596 | +** When a match if found, the matching entry is moved to become the |
| 203597 | +** most-recently used entry if it isn't so already. |
| 203598 | +** |
| 203599 | +** The JsonParse object returned still belongs to the Cache and might |
| 203600 | +** be deleted at any moment. If the caller whants the JsonParse to |
| 203601 | +** linger, it needs to increment the nPJRef reference counter. |
| 203602 | +*/ |
| 203603 | +static JsonParse *jsonCacheSearch( |
| 203604 | + sqlite3_context *ctx, /* The SQL statement context holding the cache */ |
| 203605 | + sqlite3_value *pArg /* Function argument containing SQL text */ |
| 203606 | +){ |
| 203607 | + JsonCache *p; |
| 203608 | + int i; |
| 203609 | + const char *zJson; |
| 203610 | + int nJson; |
| 203611 | + |
| 203612 | + if( sqlite3_value_type(pArg)!=SQLITE_TEXT ){ |
| 203613 | + return 0; |
| 203614 | + } |
| 203615 | + zJson = (const char*)sqlite3_value_text(pArg); |
| 203616 | + if( zJson==0 ) return 0; |
| 203617 | + nJson = sqlite3_value_bytes(pArg); |
| 203618 | + |
| 203619 | + p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); |
| 203620 | + if( p==0 ){ |
| 203621 | + return 0; |
| 203622 | + } |
| 203623 | + for(i=0; i<p->nUsed; i++){ |
| 203624 | + if( p->a[i]->zJson==zJson ) break; |
| 203625 | + } |
| 203626 | + if( i>=p->nUsed ){ |
| 203627 | + for(i=0; i<p->nUsed; i++){ |
| 203628 | + if( p->a[i]->nJson!=nJson ) continue; |
| 203629 | + if( memcmp(p->a[i]->zJson, zJson, nJson)==0 ) break; |
| 203630 | + } |
| 203631 | + } |
| 203632 | + if( i<p->nUsed ){ |
| 203633 | + if( i<p->nUsed-1 ){ |
| 203634 | + /* Make the matching entry the most recently used entry */ |
| 203635 | + JsonParse *tmp = p->a[i]; |
| 203636 | + memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp)); |
| 203637 | + p->a[p->nUsed-1] = tmp; |
| 203638 | + i = p->nUsed - 1; |
| 203639 | + } |
| 203640 | + assert( p->a[i]->delta==0 ); |
| 203641 | + return p->a[i]; |
| 203642 | + }else{ |
| 203643 | + return 0; |
| 203644 | + } |
| 203645 | +} |
| 202863 | 203646 | |
| 202864 | 203647 | /************************************************************************** |
| 202865 | 203648 | ** Utility routines for dealing with JsonString objects |
| 202866 | 203649 | **************************************************************************/ |
| 202867 | 203650 | |
| 202868 | | -/* Set the JsonString object to an empty string |
| 203651 | +/* Turn uninitialized bulk memory into a valid JsonString object |
| 203652 | +** holding a zero-length string. |
| 202869 | 203653 | */ |
| 202870 | | -static void jsonZero(JsonString *p){ |
| 203654 | +static void jsonStringZero(JsonString *p){ |
| 202871 | 203655 | p->zBuf = p->zSpace; |
| 202872 | 203656 | p->nAlloc = sizeof(p->zSpace); |
| 202873 | 203657 | p->nUsed = 0; |
| 202874 | 203658 | p->bStatic = 1; |
| 202875 | 203659 | } |
| 202876 | 203660 | |
| 202877 | 203661 | /* Initialize the JsonString object |
| 202878 | 203662 | */ |
| 202879 | | -static void jsonInit(JsonString *p, sqlite3_context *pCtx){ |
| 203663 | +static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){ |
| 202880 | 203664 | p->pCtx = pCtx; |
| 202881 | | - p->bErr = 0; |
| 202882 | | - jsonZero(p); |
| 203665 | + p->eErr = 0; |
| 203666 | + jsonStringZero(p); |
| 202883 | 203667 | } |
| 202884 | 203668 | |
| 202885 | 203669 | /* Free all allocated memory and reset the JsonString object back to its |
| 202886 | 203670 | ** initial state. |
| 202887 | 203671 | */ |
| 202888 | | -static void jsonReset(JsonString *p){ |
| 203672 | +static void jsonStringReset(JsonString *p){ |
| 202889 | 203673 | if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf); |
| 202890 | | - jsonZero(p); |
| 203674 | + jsonStringZero(p); |
| 202891 | 203675 | } |
| 202892 | 203676 | |
| 202893 | 203677 | /* Report an out-of-memory (OOM) condition |
| 202894 | 203678 | */ |
| 202895 | | -static void jsonOom(JsonString *p){ |
| 202896 | | - p->bErr = 1; |
| 202897 | | - sqlite3_result_error_nomem(p->pCtx); |
| 202898 | | - jsonReset(p); |
| 203679 | +static void jsonStringOom(JsonString *p){ |
| 203680 | + p->eErr |= JSTRING_OOM; |
| 203681 | + if( p->pCtx ) sqlite3_result_error_nomem(p->pCtx); |
| 203682 | + jsonStringReset(p); |
| 202899 | 203683 | } |
| 202900 | 203684 | |
| 202901 | 203685 | /* Enlarge pJson->zBuf so that it can hold at least N more bytes. |
| 202902 | 203686 | ** Return zero on success. Return non-zero on an OOM error |
| 202903 | 203687 | */ |
| 202904 | | -static int jsonGrow(JsonString *p, u32 N){ |
| 203688 | +static int jsonStringGrow(JsonString *p, u32 N){ |
| 202905 | 203689 | u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10; |
| 202906 | 203690 | char *zNew; |
| 202907 | 203691 | if( p->bStatic ){ |
| 202908 | | - if( p->bErr ) return 1; |
| 203692 | + if( p->eErr ) return 1; |
| 202909 | 203693 | zNew = sqlite3RCStrNew(nTotal); |
| 202910 | 203694 | if( zNew==0 ){ |
| 202911 | | - jsonOom(p); |
| 203695 | + jsonStringOom(p); |
| 202912 | 203696 | return SQLITE_NOMEM; |
| 202913 | 203697 | } |
| 202914 | 203698 | memcpy(zNew, p->zBuf, (size_t)p->nUsed); |
| 202915 | 203699 | p->zBuf = zNew; |
| 202916 | 203700 | p->bStatic = 0; |
| 202917 | 203701 | }else{ |
| 202918 | 203702 | p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal); |
| 202919 | 203703 | if( p->zBuf==0 ){ |
| 202920 | | - p->bErr = 1; |
| 202921 | | - jsonZero(p); |
| 203704 | + p->eErr |= JSTRING_OOM; |
| 203705 | + jsonStringZero(p); |
| 202922 | 203706 | return SQLITE_NOMEM; |
| 202923 | 203707 | } |
| 202924 | 203708 | } |
| 202925 | 203709 | p->nAlloc = nTotal; |
| 202926 | 203710 | return SQLITE_OK; |
| 202927 | 203711 | } |
| 202928 | 203712 | |
| 202929 | 203713 | /* Append N bytes from zIn onto the end of the JsonString string. |
| 202930 | 203714 | */ |
| 202931 | | -static SQLITE_NOINLINE void jsonAppendExpand( |
| 203715 | +static SQLITE_NOINLINE void jsonStringExpandAndAppend( |
| 202932 | 203716 | JsonString *p, |
| 202933 | 203717 | const char *zIn, |
| 202934 | 203718 | u32 N |
| 202935 | 203719 | ){ |
| 202936 | 203720 | assert( N>0 ); |
| 202937 | | - if( jsonGrow(p,N) ) return; |
| 203721 | + if( jsonStringGrow(p,N) ) return; |
| 202938 | 203722 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 202939 | 203723 | p->nUsed += N; |
| 202940 | 203724 | } |
| 202941 | 203725 | static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ |
| 202942 | 203726 | if( N==0 ) return; |
| 202943 | 203727 | if( N+p->nUsed >= p->nAlloc ){ |
| 202944 | | - jsonAppendExpand(p,zIn,N); |
| 203728 | + jsonStringExpandAndAppend(p,zIn,N); |
| 202945 | 203729 | }else{ |
| 202946 | 203730 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 202947 | 203731 | p->nUsed += N; |
| 202948 | 203732 | } |
| 202949 | 203733 | } |
| 202950 | 203734 | static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){ |
| 202951 | 203735 | assert( N>0 ); |
| 202952 | 203736 | if( N+p->nUsed >= p->nAlloc ){ |
| 202953 | | - jsonAppendExpand(p,zIn,N); |
| 203737 | + jsonStringExpandAndAppend(p,zIn,N); |
| 202954 | 203738 | }else{ |
| 202955 | 203739 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 202956 | 203740 | p->nUsed += N; |
| 202957 | 203741 | } |
| 202958 | 203742 | } |
| | @@ -202960,21 +203744,21 @@ |
| 202960 | 203744 | |
| 202961 | 203745 | /* Append formatted text (not to exceed N bytes) to the JsonString. |
| 202962 | 203746 | */ |
| 202963 | 203747 | static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){ |
| 202964 | 203748 | va_list ap; |
| 202965 | | - if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return; |
| 203749 | + if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return; |
| 202966 | 203750 | va_start(ap, zFormat); |
| 202967 | 203751 | sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap); |
| 202968 | 203752 | va_end(ap); |
| 202969 | 203753 | p->nUsed += (int)strlen(p->zBuf+p->nUsed); |
| 202970 | 203754 | } |
| 202971 | 203755 | |
| 202972 | 203756 | /* Append a single character |
| 202973 | 203757 | */ |
| 202974 | 203758 | static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){ |
| 202975 | | - if( jsonGrow(p,1) ) return; |
| 203759 | + if( jsonStringGrow(p,1) ) return; |
| 202976 | 203760 | p->zBuf[p->nUsed++] = c; |
| 202977 | 203761 | } |
| 202978 | 203762 | static void jsonAppendChar(JsonString *p, char c){ |
| 202979 | 203763 | if( p->nUsed>=p->nAlloc ){ |
| 202980 | 203764 | jsonAppendCharExpand(p,c); |
| | @@ -202981,27 +203765,30 @@ |
| 202981 | 203765 | }else{ |
| 202982 | 203766 | p->zBuf[p->nUsed++] = c; |
| 202983 | 203767 | } |
| 202984 | 203768 | } |
| 202985 | 203769 | |
| 202986 | | -/* Try to force the string to be a zero-terminated RCStr string. |
| 203770 | +/* Remove a single character from the end of the string |
| 203771 | +*/ |
| 203772 | +static void jsonStringTrimOneChar(JsonString *p){ |
| 203773 | + if( p->eErr==0 ){ |
| 203774 | + assert( p->nUsed>0 ); |
| 203775 | + p->nUsed--; |
| 203776 | + } |
| 203777 | +} |
| 203778 | + |
| 203779 | + |
| 203780 | +/* Make sure there is a zero terminator on p->zBuf[] |
| 202987 | 203781 | ** |
| 202988 | 203782 | ** Return true on success. Return false if an OOM prevents this |
| 202989 | 203783 | ** from happening. |
| 202990 | 203784 | */ |
| 202991 | | -static int jsonForceRCStr(JsonString *p){ |
| 203785 | +static int jsonStringTerminate(JsonString *p){ |
| 202992 | 203786 | jsonAppendChar(p, 0); |
| 202993 | | - if( p->bErr ) return 0; |
| 202994 | | - p->nUsed--; |
| 202995 | | - if( p->bStatic==0 ) return 1; |
| 202996 | | - p->nAlloc = 0; |
| 202997 | | - p->nUsed++; |
| 202998 | | - jsonGrow(p, p->nUsed); |
| 202999 | | - p->nUsed--; |
| 203000 | | - return p->bStatic==0; |
| 203001 | | -} |
| 203002 | | - |
| 203787 | + jsonStringTrimOneChar(p); |
| 203788 | + return p->eErr==0; |
| 203789 | +} |
| 203003 | 203790 | |
| 203004 | 203791 | /* Append a comma separator to the output buffer, if the previous |
| 203005 | 203792 | ** character is not '[' or '{'. |
| 203006 | 203793 | */ |
| 203007 | 203794 | static void jsonAppendSeparator(JsonString *p){ |
| | @@ -203011,25 +203798,70 @@ |
| 203011 | 203798 | if( c=='[' || c=='{' ) return; |
| 203012 | 203799 | jsonAppendChar(p, ','); |
| 203013 | 203800 | } |
| 203014 | 203801 | |
| 203015 | 203802 | /* Append the N-byte string in zIn to the end of the JsonString string |
| 203016 | | -** under construction. Enclose the string in "..." and escape |
| 203017 | | -** any double-quotes or backslash characters contained within the |
| 203803 | +** under construction. Enclose the string in double-quotes ("...") and |
| 203804 | +** escape any double-quotes or backslash characters contained within the |
| 203018 | 203805 | ** string. |
| 203806 | +** |
| 203807 | +** This routine is a high-runner. There is a measurable performance |
| 203808 | +** increase associated with unwinding the jsonIsOk[] loop. |
| 203019 | 203809 | */ |
| 203020 | 203810 | static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ |
| 203021 | | - u32 i; |
| 203022 | | - if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return; |
| 203811 | + u32 k; |
| 203812 | + u8 c; |
| 203813 | + const u8 *z = (const u8*)zIn; |
| 203814 | + if( z==0 ) return; |
| 203815 | + if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return; |
| 203023 | 203816 | p->zBuf[p->nUsed++] = '"'; |
| 203024 | | - for(i=0; i<N; i++){ |
| 203025 | | - unsigned char c = ((unsigned const char*)zIn)[i]; |
| 203026 | | - if( jsonIsOk[c] ){ |
| 203027 | | - p->zBuf[p->nUsed++] = c; |
| 203028 | | - }else if( c=='"' || c=='\\' ){ |
| 203817 | + while( 1 /*exit-by-break*/ ){ |
| 203818 | + k = 0; |
| 203819 | + /* The following while() is the 4-way unwound equivalent of |
| 203820 | + ** |
| 203821 | + ** while( k<N && jsonIsOk[z[k]] ){ k++; } |
| 203822 | + */ |
| 203823 | + while( 1 /* Exit by break */ ){ |
| 203824 | + if( k+3>=N ){ |
| 203825 | + while( k<N && jsonIsOk[z[k]] ){ k++; } |
| 203826 | + break; |
| 203827 | + } |
| 203828 | + if( !jsonIsOk[z[k]] ){ |
| 203829 | + break; |
| 203830 | + } |
| 203831 | + if( !jsonIsOk[z[k+1]] ){ |
| 203832 | + k += 1; |
| 203833 | + break; |
| 203834 | + } |
| 203835 | + if( !jsonIsOk[z[k+2]] ){ |
| 203836 | + k += 2; |
| 203837 | + break; |
| 203838 | + } |
| 203839 | + if( !jsonIsOk[z[k+3]] ){ |
| 203840 | + k += 3; |
| 203841 | + break; |
| 203842 | + }else{ |
| 203843 | + k += 4; |
| 203844 | + } |
| 203845 | + } |
| 203846 | + if( k>=N ){ |
| 203847 | + if( k>0 ){ |
| 203848 | + memcpy(&p->zBuf[p->nUsed], z, k); |
| 203849 | + p->nUsed += k; |
| 203850 | + } |
| 203851 | + break; |
| 203852 | + } |
| 203853 | + if( k>0 ){ |
| 203854 | + memcpy(&p->zBuf[p->nUsed], z, k); |
| 203855 | + p->nUsed += k; |
| 203856 | + z += k; |
| 203857 | + N -= k; |
| 203858 | + } |
| 203859 | + c = z[0]; |
| 203860 | + if( c=='"' || c=='\\' ){ |
| 203029 | 203861 | json_simple_escape: |
| 203030 | | - if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return; |
| 203862 | + if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return; |
| 203031 | 203863 | p->zBuf[p->nUsed++] = '\\'; |
| 203032 | 203864 | p->zBuf[p->nUsed++] = c; |
| 203033 | 203865 | }else if( c=='\'' ){ |
| 203034 | 203866 | p->zBuf[p->nUsed++] = c; |
| 203035 | 203867 | }else{ |
| | @@ -203046,158 +203878,30 @@ |
| 203046 | 203878 | assert( c>=0 && c<sizeof(aSpecial) ); |
| 203047 | 203879 | if( aSpecial[c] ){ |
| 203048 | 203880 | c = aSpecial[c]; |
| 203049 | 203881 | goto json_simple_escape; |
| 203050 | 203882 | } |
| 203051 | | - if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return; |
| 203883 | + if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return; |
| 203052 | 203884 | p->zBuf[p->nUsed++] = '\\'; |
| 203053 | 203885 | p->zBuf[p->nUsed++] = 'u'; |
| 203054 | 203886 | p->zBuf[p->nUsed++] = '0'; |
| 203055 | 203887 | p->zBuf[p->nUsed++] = '0'; |
| 203056 | 203888 | p->zBuf[p->nUsed++] = "0123456789abcdef"[c>>4]; |
| 203057 | 203889 | p->zBuf[p->nUsed++] = "0123456789abcdef"[c&0xf]; |
| 203058 | 203890 | } |
| 203891 | + z++; |
| 203892 | + N--; |
| 203059 | 203893 | } |
| 203060 | 203894 | p->zBuf[p->nUsed++] = '"'; |
| 203061 | 203895 | assert( p->nUsed<p->nAlloc ); |
| 203062 | 203896 | } |
| 203063 | 203897 | |
| 203064 | 203898 | /* |
| 203065 | | -** The zIn[0..N] string is a JSON5 string literal. Append to p a translation |
| 203066 | | -** of the string literal that standard JSON and that omits all JSON5 |
| 203067 | | -** features. |
| 203068 | | -*/ |
| 203069 | | -static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){ |
| 203070 | | - u32 i; |
| 203071 | | - jsonAppendChar(p, '"'); |
| 203072 | | - zIn++; |
| 203073 | | - N -= 2; |
| 203074 | | - while( N>0 ){ |
| 203075 | | - for(i=0; i<N && zIn[i]!='\\' && zIn[i]!='"'; i++){} |
| 203076 | | - if( i>0 ){ |
| 203077 | | - jsonAppendRawNZ(p, zIn, i); |
| 203078 | | - zIn += i; |
| 203079 | | - N -= i; |
| 203080 | | - if( N==0 ) break; |
| 203081 | | - } |
| 203082 | | - if( zIn[0]=='"' ){ |
| 203083 | | - jsonAppendRawNZ(p, "\\\"", 2); |
| 203084 | | - zIn++; |
| 203085 | | - N--; |
| 203086 | | - continue; |
| 203087 | | - } |
| 203088 | | - assert( zIn[0]=='\\' ); |
| 203089 | | - switch( (u8)zIn[1] ){ |
| 203090 | | - case '\'': |
| 203091 | | - jsonAppendChar(p, '\''); |
| 203092 | | - break; |
| 203093 | | - case 'v': |
| 203094 | | - jsonAppendRawNZ(p, "\\u0009", 6); |
| 203095 | | - break; |
| 203096 | | - case 'x': |
| 203097 | | - jsonAppendRawNZ(p, "\\u00", 4); |
| 203098 | | - jsonAppendRawNZ(p, &zIn[2], 2); |
| 203099 | | - zIn += 2; |
| 203100 | | - N -= 2; |
| 203101 | | - break; |
| 203102 | | - case '0': |
| 203103 | | - jsonAppendRawNZ(p, "\\u0000", 6); |
| 203104 | | - break; |
| 203105 | | - case '\r': |
| 203106 | | - if( zIn[2]=='\n' ){ |
| 203107 | | - zIn++; |
| 203108 | | - N--; |
| 203109 | | - } |
| 203110 | | - break; |
| 203111 | | - case '\n': |
| 203112 | | - break; |
| 203113 | | - case 0xe2: |
| 203114 | | - assert( N>=4 ); |
| 203115 | | - assert( 0x80==(u8)zIn[2] ); |
| 203116 | | - assert( 0xa8==(u8)zIn[3] || 0xa9==(u8)zIn[3] ); |
| 203117 | | - zIn += 2; |
| 203118 | | - N -= 2; |
| 203119 | | - break; |
| 203120 | | - default: |
| 203121 | | - jsonAppendRawNZ(p, zIn, 2); |
| 203122 | | - break; |
| 203123 | | - } |
| 203124 | | - zIn += 2; |
| 203125 | | - N -= 2; |
| 203126 | | - } |
| 203127 | | - jsonAppendChar(p, '"'); |
| 203128 | | -} |
| 203129 | | - |
| 203130 | | -/* |
| 203131 | | -** The zIn[0..N] string is a JSON5 integer literal. Append to p a translation |
| 203132 | | -** of the string literal that standard JSON and that omits all JSON5 |
| 203133 | | -** features. |
| 203134 | | -*/ |
| 203135 | | -static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){ |
| 203136 | | - if( zIn[0]=='+' ){ |
| 203137 | | - zIn++; |
| 203138 | | - N--; |
| 203139 | | - }else if( zIn[0]=='-' ){ |
| 203140 | | - jsonAppendChar(p, '-'); |
| 203141 | | - zIn++; |
| 203142 | | - N--; |
| 203143 | | - } |
| 203144 | | - if( zIn[0]=='0' && (zIn[1]=='x' || zIn[1]=='X') ){ |
| 203145 | | - sqlite3_int64 i = 0; |
| 203146 | | - int rc = sqlite3DecOrHexToI64(zIn, &i); |
| 203147 | | - if( rc<=1 ){ |
| 203148 | | - jsonPrintf(100,p,"%lld",i); |
| 203149 | | - }else{ |
| 203150 | | - assert( rc==2 ); |
| 203151 | | - jsonAppendRawNZ(p, "9.0e999", 7); |
| 203152 | | - } |
| 203153 | | - return; |
| 203154 | | - } |
| 203155 | | - assert( N>0 ); |
| 203156 | | - jsonAppendRawNZ(p, zIn, N); |
| 203157 | | -} |
| 203158 | | - |
| 203159 | | -/* |
| 203160 | | -** The zIn[0..N] string is a JSON5 real literal. Append to p a translation |
| 203161 | | -** of the string literal that standard JSON and that omits all JSON5 |
| 203162 | | -** features. |
| 203163 | | -*/ |
| 203164 | | -static void jsonAppendNormalizedReal(JsonString *p, const char *zIn, u32 N){ |
| 203165 | | - u32 i; |
| 203166 | | - if( zIn[0]=='+' ){ |
| 203167 | | - zIn++; |
| 203168 | | - N--; |
| 203169 | | - }else if( zIn[0]=='-' ){ |
| 203170 | | - jsonAppendChar(p, '-'); |
| 203171 | | - zIn++; |
| 203172 | | - N--; |
| 203173 | | - } |
| 203174 | | - if( zIn[0]=='.' ){ |
| 203175 | | - jsonAppendChar(p, '0'); |
| 203176 | | - } |
| 203177 | | - for(i=0; i<N; i++){ |
| 203178 | | - if( zIn[i]=='.' && (i+1==N || !sqlite3Isdigit(zIn[i+1])) ){ |
| 203179 | | - i++; |
| 203180 | | - jsonAppendRaw(p, zIn, i); |
| 203181 | | - zIn += i; |
| 203182 | | - N -= i; |
| 203183 | | - jsonAppendChar(p, '0'); |
| 203184 | | - break; |
| 203185 | | - } |
| 203186 | | - } |
| 203187 | | - if( N>0 ){ |
| 203188 | | - jsonAppendRawNZ(p, zIn, N); |
| 203189 | | - } |
| 203190 | | -} |
| 203191 | | - |
| 203192 | | - |
| 203193 | | - |
| 203194 | | -/* |
| 203195 | | -** Append a function parameter value to the JSON string under |
| 203196 | | -** construction. |
| 203197 | | -*/ |
| 203198 | | -static void jsonAppendValue( |
| 203899 | +** Append an sqlite3_value (such as a function parameter) to the JSON |
| 203900 | +** string under construction in p. |
| 203901 | +*/ |
| 203902 | +static void jsonAppendSqlValue( |
| 203199 | 203903 | JsonString *p, /* Append to this JSON string */ |
| 203200 | 203904 | sqlite3_value *pValue /* Value to append */ |
| 203201 | 203905 | ){ |
| 203202 | 203906 | switch( sqlite3_value_type(pValue) ){ |
| 203203 | 203907 | case SQLITE_NULL: { |
| | @@ -203223,591 +203927,147 @@ |
| 203223 | 203927 | jsonAppendString(p, z, n); |
| 203224 | 203928 | } |
| 203225 | 203929 | break; |
| 203226 | 203930 | } |
| 203227 | 203931 | default: { |
| 203228 | | - if( p->bErr==0 ){ |
| 203932 | + if( jsonFuncArgMightBeBinary(pValue) ){ |
| 203933 | + JsonParse px; |
| 203934 | + memset(&px, 0, sizeof(px)); |
| 203935 | + px.aBlob = (u8*)sqlite3_value_blob(pValue); |
| 203936 | + px.nBlob = sqlite3_value_bytes(pValue); |
| 203937 | + jsonTranslateBlobToText(&px, 0, p); |
| 203938 | + }else if( p->eErr==0 ){ |
| 203229 | 203939 | sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); |
| 203230 | | - p->bErr = 2; |
| 203231 | | - jsonReset(p); |
| 203940 | + p->eErr = JSTRING_ERR; |
| 203941 | + jsonStringReset(p); |
| 203232 | 203942 | } |
| 203233 | 203943 | break; |
| 203234 | 203944 | } |
| 203235 | 203945 | } |
| 203236 | 203946 | } |
| 203237 | 203947 | |
| 203238 | | - |
| 203239 | | -/* Make the JSON in p the result of the SQL function. |
| 203948 | +/* Make the text in p (which is probably a generated JSON text string) |
| 203949 | +** the result of the SQL function. |
| 203240 | 203950 | ** |
| 203241 | | -** The JSON string is reset. |
| 203951 | +** The JsonString is reset. |
| 203952 | +** |
| 203953 | +** If pParse and ctx are both non-NULL, then the SQL string in p is |
| 203954 | +** loaded into the zJson field of the pParse object as a RCStr and the |
| 203955 | +** pParse is added to the cache. |
| 203242 | 203956 | */ |
| 203243 | | -static void jsonResult(JsonString *p){ |
| 203244 | | - if( p->bErr==0 ){ |
| 203245 | | - if( p->bStatic ){ |
| 203957 | +static void jsonReturnString( |
| 203958 | + JsonString *p, /* String to return */ |
| 203959 | + JsonParse *pParse, /* JSONB source or NULL */ |
| 203960 | + sqlite3_context *ctx /* Where to cache */ |
| 203961 | +){ |
| 203962 | + assert( (pParse!=0)==(ctx!=0) ); |
| 203963 | + assert( ctx==0 || ctx==p->pCtx ); |
| 203964 | + if( p->eErr==0 ){ |
| 203965 | + int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx)); |
| 203966 | + if( flags & JSON_BLOB ){ |
| 203967 | + jsonReturnStringAsBlob(p); |
| 203968 | + }else if( p->bStatic ){ |
| 203246 | 203969 | sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, |
| 203247 | 203970 | SQLITE_TRANSIENT, SQLITE_UTF8); |
| 203248 | | - }else if( jsonForceRCStr(p) ){ |
| 203249 | | - sqlite3RCStrRef(p->zBuf); |
| 203250 | | - sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, |
| 203971 | + }else if( jsonStringTerminate(p) ){ |
| 203972 | + if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){ |
| 203973 | + int rc; |
| 203974 | + pParse->zJson = sqlite3RCStrRef(p->zBuf); |
| 203975 | + pParse->nJson = p->nUsed; |
| 203976 | + pParse->bJsonIsRCStr = 1; |
| 203977 | + rc = jsonCacheInsert(ctx, pParse); |
| 203978 | + if( rc==SQLITE_NOMEM ){ |
| 203979 | + sqlite3_result_error_nomem(ctx); |
| 203980 | + jsonStringReset(p); |
| 203981 | + return; |
| 203982 | + } |
| 203983 | + } |
| 203984 | + sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed, |
| 203251 | 203985 | sqlite3RCStrUnref, |
| 203252 | 203986 | SQLITE_UTF8); |
| 203987 | + }else{ |
| 203988 | + sqlite3_result_error_nomem(p->pCtx); |
| 203253 | 203989 | } |
| 203254 | | - } |
| 203255 | | - if( p->bErr==1 ){ |
| 203990 | + }else if( p->eErr & JSTRING_OOM ){ |
| 203256 | 203991 | sqlite3_result_error_nomem(p->pCtx); |
| 203992 | + }else if( p->eErr & JSTRING_MALFORMED ){ |
| 203993 | + sqlite3_result_error(p->pCtx, "malformed JSON", -1); |
| 203257 | 203994 | } |
| 203258 | | - jsonReset(p); |
| 203995 | + jsonStringReset(p); |
| 203259 | 203996 | } |
| 203260 | 203997 | |
| 203261 | 203998 | /************************************************************************** |
| 203262 | | -** Utility routines for dealing with JsonNode and JsonParse objects |
| 203999 | +** Utility routines for dealing with JsonParse objects |
| 203263 | 204000 | **************************************************************************/ |
| 203264 | 204001 | |
| 203265 | | -/* |
| 203266 | | -** Return the number of consecutive JsonNode slots need to represent |
| 203267 | | -** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and |
| 203268 | | -** OBJECT types, the number might be larger. |
| 203269 | | -** |
| 203270 | | -** Appended elements are not counted. The value returned is the number |
| 203271 | | -** by which the JsonNode counter should increment in order to go to the |
| 203272 | | -** next peer value. |
| 203273 | | -*/ |
| 203274 | | -static u32 jsonNodeSize(JsonNode *pNode){ |
| 203275 | | - return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1; |
| 203276 | | -} |
| 203277 | | - |
| 203278 | 204002 | /* |
| 203279 | 204003 | ** Reclaim all memory allocated by a JsonParse object. But do not |
| 203280 | 204004 | ** delete the JsonParse object itself. |
| 203281 | 204005 | */ |
| 203282 | 204006 | static void jsonParseReset(JsonParse *pParse){ |
| 203283 | | - while( pParse->pClup ){ |
| 203284 | | - JsonCleanup *pTask = pParse->pClup; |
| 203285 | | - pParse->pClup = pTask->pJCNext; |
| 203286 | | - pTask->xOp(pTask->pArg); |
| 203287 | | - sqlite3_free(pTask); |
| 203288 | | - } |
| 203289 | 204007 | assert( pParse->nJPRef<=1 ); |
| 203290 | | - if( pParse->aNode ){ |
| 203291 | | - sqlite3_free(pParse->aNode); |
| 203292 | | - pParse->aNode = 0; |
| 203293 | | - } |
| 203294 | | - pParse->nNode = 0; |
| 203295 | | - pParse->nAlloc = 0; |
| 203296 | | - if( pParse->aUp ){ |
| 203297 | | - sqlite3_free(pParse->aUp); |
| 203298 | | - pParse->aUp = 0; |
| 203299 | | - } |
| 203300 | 204008 | if( pParse->bJsonIsRCStr ){ |
| 203301 | 204009 | sqlite3RCStrUnref(pParse->zJson); |
| 203302 | 204010 | pParse->zJson = 0; |
| 204011 | + pParse->nJson = 0; |
| 203303 | 204012 | pParse->bJsonIsRCStr = 0; |
| 203304 | 204013 | } |
| 203305 | | - if( pParse->zAlt ){ |
| 203306 | | - sqlite3RCStrUnref(pParse->zAlt); |
| 203307 | | - pParse->zAlt = 0; |
| 204014 | + if( pParse->nBlobAlloc ){ |
| 204015 | + sqlite3DbFree(pParse->db, pParse->aBlob); |
| 204016 | + pParse->aBlob = 0; |
| 204017 | + pParse->nBlob = 0; |
| 204018 | + pParse->nBlobAlloc = 0; |
| 203308 | 204019 | } |
| 203309 | 204020 | } |
| 203310 | 204021 | |
| 203311 | 204022 | /* |
| 203312 | | -** Free a JsonParse object that was obtained from sqlite3_malloc(). |
| 203313 | | -** |
| 203314 | | -** Note that destroying JsonParse might call sqlite3RCStrUnref() to |
| 203315 | | -** destroy the zJson value. The RCStr object might recursively invoke |
| 203316 | | -** JsonParse to destroy this pParse object again. Take care to ensure |
| 203317 | | -** that this recursive destructor sequence terminates harmlessly. |
| 204023 | +** Decrement the reference count on the JsonParse object. When the |
| 204024 | +** count reaches zero, free the object. |
| 203318 | 204025 | */ |
| 203319 | 204026 | static void jsonParseFree(JsonParse *pParse){ |
| 203320 | | - if( pParse->nJPRef>1 ){ |
| 203321 | | - pParse->nJPRef--; |
| 203322 | | - }else{ |
| 203323 | | - jsonParseReset(pParse); |
| 203324 | | - sqlite3_free(pParse); |
| 203325 | | - } |
| 203326 | | -} |
| 203327 | | - |
| 203328 | | -/* |
| 203329 | | -** Add a cleanup task to the JsonParse object. |
| 203330 | | -** |
| 203331 | | -** If an OOM occurs, the cleanup operation happens immediately |
| 203332 | | -** and this function returns SQLITE_NOMEM. |
| 203333 | | -*/ |
| 203334 | | -static int jsonParseAddCleanup( |
| 203335 | | - JsonParse *pParse, /* Add the cleanup task to this parser */ |
| 203336 | | - void(*xOp)(void*), /* The cleanup task */ |
| 203337 | | - void *pArg /* Argument to the cleanup */ |
| 203338 | | -){ |
| 203339 | | - JsonCleanup *pTask = sqlite3_malloc64( sizeof(*pTask) ); |
| 203340 | | - if( pTask==0 ){ |
| 203341 | | - pParse->oom = 1; |
| 203342 | | - xOp(pArg); |
| 203343 | | - return SQLITE_ERROR; |
| 203344 | | - } |
| 203345 | | - pTask->pJCNext = pParse->pClup; |
| 203346 | | - pParse->pClup = pTask; |
| 203347 | | - pTask->xOp = xOp; |
| 203348 | | - pTask->pArg = pArg; |
| 203349 | | - return SQLITE_OK; |
| 203350 | | -} |
| 203351 | | - |
| 203352 | | -/* |
| 203353 | | -** Convert the JsonNode pNode into a pure JSON string and |
| 203354 | | -** append to pOut. Subsubstructure is also included. Return |
| 203355 | | -** the number of JsonNode objects that are encoded. |
| 203356 | | -*/ |
| 203357 | | -static void jsonRenderNode( |
| 203358 | | - JsonParse *pParse, /* the complete parse of the JSON */ |
| 203359 | | - JsonNode *pNode, /* The node to render */ |
| 203360 | | - JsonString *pOut /* Write JSON here */ |
| 203361 | | -){ |
| 203362 | | - assert( pNode!=0 ); |
| 203363 | | - while( (pNode->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){ |
| 203364 | | - u32 idx = (u32)(pNode - pParse->aNode); |
| 203365 | | - u32 i = pParse->iSubst; |
| 203366 | | - while( 1 /*exit-by-break*/ ){ |
| 203367 | | - assert( i<pParse->nNode ); |
| 203368 | | - assert( pParse->aNode[i].eType==JSON_SUBST ); |
| 203369 | | - assert( pParse->aNode[i].eU==4 ); |
| 203370 | | - assert( pParse->aNode[i].u.iPrev<i ); |
| 203371 | | - if( pParse->aNode[i].n==idx ){ |
| 203372 | | - pNode = &pParse->aNode[i+1]; |
| 203373 | | - break; |
| 203374 | | - } |
| 203375 | | - i = pParse->aNode[i].u.iPrev; |
| 203376 | | - } |
| 203377 | | - } |
| 203378 | | - switch( pNode->eType ){ |
| 203379 | | - default: { |
| 203380 | | - assert( pNode->eType==JSON_NULL ); |
| 203381 | | - jsonAppendRawNZ(pOut, "null", 4); |
| 203382 | | - break; |
| 203383 | | - } |
| 203384 | | - case JSON_TRUE: { |
| 203385 | | - jsonAppendRawNZ(pOut, "true", 4); |
| 203386 | | - break; |
| 203387 | | - } |
| 203388 | | - case JSON_FALSE: { |
| 203389 | | - jsonAppendRawNZ(pOut, "false", 5); |
| 203390 | | - break; |
| 203391 | | - } |
| 203392 | | - case JSON_STRING: { |
| 203393 | | - assert( pNode->eU==1 ); |
| 203394 | | - if( pNode->jnFlags & JNODE_RAW ){ |
| 203395 | | - if( pNode->jnFlags & JNODE_LABEL ){ |
| 203396 | | - jsonAppendChar(pOut, '"'); |
| 203397 | | - jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n); |
| 203398 | | - jsonAppendChar(pOut, '"'); |
| 203399 | | - }else{ |
| 203400 | | - jsonAppendString(pOut, pNode->u.zJContent, pNode->n); |
| 203401 | | - } |
| 203402 | | - }else if( pNode->jnFlags & JNODE_JSON5 ){ |
| 203403 | | - jsonAppendNormalizedString(pOut, pNode->u.zJContent, pNode->n); |
| 203404 | | - }else{ |
| 203405 | | - assert( pNode->n>0 ); |
| 203406 | | - jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n); |
| 203407 | | - } |
| 203408 | | - break; |
| 203409 | | - } |
| 203410 | | - case JSON_REAL: { |
| 203411 | | - assert( pNode->eU==1 ); |
| 203412 | | - if( pNode->jnFlags & JNODE_JSON5 ){ |
| 203413 | | - jsonAppendNormalizedReal(pOut, pNode->u.zJContent, pNode->n); |
| 203414 | | - }else{ |
| 203415 | | - assert( pNode->n>0 ); |
| 203416 | | - jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n); |
| 203417 | | - } |
| 203418 | | - break; |
| 203419 | | - } |
| 203420 | | - case JSON_INT: { |
| 203421 | | - assert( pNode->eU==1 ); |
| 203422 | | - if( pNode->jnFlags & JNODE_JSON5 ){ |
| 203423 | | - jsonAppendNormalizedInt(pOut, pNode->u.zJContent, pNode->n); |
| 203424 | | - }else{ |
| 203425 | | - assert( pNode->n>0 ); |
| 203426 | | - jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n); |
| 203427 | | - } |
| 203428 | | - break; |
| 203429 | | - } |
| 203430 | | - case JSON_ARRAY: { |
| 203431 | | - u32 j = 1; |
| 203432 | | - jsonAppendChar(pOut, '['); |
| 203433 | | - for(;;){ |
| 203434 | | - while( j<=pNode->n ){ |
| 203435 | | - if( (pNode[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){ |
| 203436 | | - jsonAppendSeparator(pOut); |
| 203437 | | - jsonRenderNode(pParse, &pNode[j], pOut); |
| 203438 | | - } |
| 203439 | | - j += jsonNodeSize(&pNode[j]); |
| 203440 | | - } |
| 203441 | | - if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; |
| 203442 | | - if( pParse->useMod==0 ) break; |
| 203443 | | - assert( pNode->eU==2 ); |
| 203444 | | - pNode = &pParse->aNode[pNode->u.iAppend]; |
| 203445 | | - j = 1; |
| 203446 | | - } |
| 203447 | | - jsonAppendChar(pOut, ']'); |
| 203448 | | - break; |
| 203449 | | - } |
| 203450 | | - case JSON_OBJECT: { |
| 203451 | | - u32 j = 1; |
| 203452 | | - jsonAppendChar(pOut, '{'); |
| 203453 | | - for(;;){ |
| 203454 | | - while( j<=pNode->n ){ |
| 203455 | | - if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){ |
| 203456 | | - jsonAppendSeparator(pOut); |
| 203457 | | - jsonRenderNode(pParse, &pNode[j], pOut); |
| 203458 | | - jsonAppendChar(pOut, ':'); |
| 203459 | | - jsonRenderNode(pParse, &pNode[j+1], pOut); |
| 203460 | | - } |
| 203461 | | - j += 1 + jsonNodeSize(&pNode[j+1]); |
| 203462 | | - } |
| 203463 | | - if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; |
| 203464 | | - if( pParse->useMod==0 ) break; |
| 203465 | | - assert( pNode->eU==2 ); |
| 203466 | | - pNode = &pParse->aNode[pNode->u.iAppend]; |
| 203467 | | - j = 1; |
| 203468 | | - } |
| 203469 | | - jsonAppendChar(pOut, '}'); |
| 203470 | | - break; |
| 203471 | | - } |
| 203472 | | - } |
| 203473 | | -} |
| 203474 | | - |
| 203475 | | -/* |
| 203476 | | -** Return a JsonNode and all its descendants as a JSON string. |
| 203477 | | -*/ |
| 203478 | | -static void jsonReturnJson( |
| 203479 | | - JsonParse *pParse, /* The complete JSON */ |
| 203480 | | - JsonNode *pNode, /* Node to return */ |
| 203481 | | - sqlite3_context *pCtx, /* Return value for this function */ |
| 203482 | | - int bGenerateAlt, /* Also store the rendered text in zAlt */ |
| 203483 | | - int omitSubtype /* Do not call sqlite3_result_subtype() */ |
| 203484 | | -){ |
| 203485 | | - JsonString s; |
| 203486 | | - if( pParse->oom ){ |
| 203487 | | - sqlite3_result_error_nomem(pCtx); |
| 203488 | | - return; |
| 203489 | | - } |
| 203490 | | - if( pParse->nErr==0 ){ |
| 203491 | | - jsonInit(&s, pCtx); |
| 203492 | | - jsonRenderNode(pParse, pNode, &s); |
| 203493 | | - if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){ |
| 203494 | | - pParse->zAlt = sqlite3RCStrRef(s.zBuf); |
| 203495 | | - pParse->nAlt = s.nUsed; |
| 203496 | | - } |
| 203497 | | - jsonResult(&s); |
| 203498 | | - if( !omitSubtype ) sqlite3_result_subtype(pCtx, JSON_SUBTYPE); |
| 203499 | | - } |
| 203500 | | -} |
| 204027 | + if( pParse ){ |
| 204028 | + if( pParse->nJPRef>1 ){ |
| 204029 | + pParse->nJPRef--; |
| 204030 | + }else{ |
| 204031 | + jsonParseReset(pParse); |
| 204032 | + sqlite3DbFree(pParse->db, pParse); |
| 204033 | + } |
| 204034 | + } |
| 204035 | +} |
| 204036 | + |
| 204037 | +/************************************************************************** |
| 204038 | +** Utility routines for the JSON text parser |
| 204039 | +**************************************************************************/ |
| 203501 | 204040 | |
| 203502 | 204041 | /* |
| 203503 | 204042 | ** Translate a single byte of Hex into an integer. |
| 203504 | | -** This routine only works if h really is a valid hexadecimal |
| 203505 | | -** character: 0..9a..fA..F |
| 204043 | +** This routine only gives a correct answer if h really is a valid hexadecimal |
| 204044 | +** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not |
| 204045 | +** assert() if the digit is not hex. |
| 203506 | 204046 | */ |
| 203507 | 204047 | static u8 jsonHexToInt(int h){ |
| 203508 | | - assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 204048 | +#ifdef SQLITE_ASCII |
| 204049 | + h += 9*(1&(h>>6)); |
| 204050 | +#endif |
| 203509 | 204051 | #ifdef SQLITE_EBCDIC |
| 203510 | 204052 | h += 9*(1&~(h>>4)); |
| 203511 | | -#else |
| 203512 | | - h += 9*(1&(h>>6)); |
| 203513 | 204053 | #endif |
| 203514 | 204054 | return (u8)(h & 0xf); |
| 203515 | 204055 | } |
| 203516 | 204056 | |
| 203517 | 204057 | /* |
| 203518 | 204058 | ** Convert a 4-byte hex string into an integer |
| 203519 | 204059 | */ |
| 203520 | 204060 | static u32 jsonHexToInt4(const char *z){ |
| 203521 | 204061 | u32 v; |
| 203522 | | - assert( sqlite3Isxdigit(z[0]) ); |
| 203523 | | - assert( sqlite3Isxdigit(z[1]) ); |
| 203524 | | - assert( sqlite3Isxdigit(z[2]) ); |
| 203525 | | - assert( sqlite3Isxdigit(z[3]) ); |
| 203526 | 204062 | v = (jsonHexToInt(z[0])<<12) |
| 203527 | 204063 | + (jsonHexToInt(z[1])<<8) |
| 203528 | 204064 | + (jsonHexToInt(z[2])<<4) |
| 203529 | 204065 | + jsonHexToInt(z[3]); |
| 203530 | 204066 | return v; |
| 203531 | 204067 | } |
| 203532 | 204068 | |
| 203533 | | -/* |
| 203534 | | -** Make the JsonNode the return value of the function. |
| 203535 | | -*/ |
| 203536 | | -static void jsonReturn( |
| 203537 | | - JsonParse *pParse, /* Complete JSON parse tree */ |
| 203538 | | - JsonNode *pNode, /* Node to return */ |
| 203539 | | - sqlite3_context *pCtx, /* Return value for this function */ |
| 203540 | | - int omitSubtype /* Do not call sqlite3_result_subtype() */ |
| 203541 | | -){ |
| 203542 | | - switch( pNode->eType ){ |
| 203543 | | - default: { |
| 203544 | | - assert( pNode->eType==JSON_NULL ); |
| 203545 | | - sqlite3_result_null(pCtx); |
| 203546 | | - break; |
| 203547 | | - } |
| 203548 | | - case JSON_TRUE: { |
| 203549 | | - sqlite3_result_int(pCtx, 1); |
| 203550 | | - break; |
| 203551 | | - } |
| 203552 | | - case JSON_FALSE: { |
| 203553 | | - sqlite3_result_int(pCtx, 0); |
| 203554 | | - break; |
| 203555 | | - } |
| 203556 | | - case JSON_INT: { |
| 203557 | | - sqlite3_int64 i = 0; |
| 203558 | | - int rc; |
| 203559 | | - int bNeg = 0; |
| 203560 | | - const char *z; |
| 203561 | | - |
| 203562 | | - assert( pNode->eU==1 ); |
| 203563 | | - z = pNode->u.zJContent; |
| 203564 | | - if( z[0]=='-' ){ z++; bNeg = 1; } |
| 203565 | | - else if( z[0]=='+' ){ z++; } |
| 203566 | | - rc = sqlite3DecOrHexToI64(z, &i); |
| 203567 | | - if( rc<=1 ){ |
| 203568 | | - sqlite3_result_int64(pCtx, bNeg ? -i : i); |
| 203569 | | - }else if( rc==3 && bNeg ){ |
| 203570 | | - sqlite3_result_int64(pCtx, SMALLEST_INT64); |
| 203571 | | - }else{ |
| 203572 | | - goto to_double; |
| 203573 | | - } |
| 203574 | | - break; |
| 203575 | | - } |
| 203576 | | - case JSON_REAL: { |
| 203577 | | - double r; |
| 203578 | | - const char *z; |
| 203579 | | - assert( pNode->eU==1 ); |
| 203580 | | - to_double: |
| 203581 | | - z = pNode->u.zJContent; |
| 203582 | | - sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8); |
| 203583 | | - sqlite3_result_double(pCtx, r); |
| 203584 | | - break; |
| 203585 | | - } |
| 203586 | | - case JSON_STRING: { |
| 203587 | | - if( pNode->jnFlags & JNODE_RAW ){ |
| 203588 | | - assert( pNode->eU==1 ); |
| 203589 | | - sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n, |
| 203590 | | - SQLITE_TRANSIENT); |
| 203591 | | - }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){ |
| 203592 | | - /* JSON formatted without any backslash-escapes */ |
| 203593 | | - assert( pNode->eU==1 ); |
| 203594 | | - sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2, |
| 203595 | | - SQLITE_TRANSIENT); |
| 203596 | | - }else{ |
| 203597 | | - /* Translate JSON formatted string into raw text */ |
| 203598 | | - u32 i; |
| 203599 | | - u32 n = pNode->n; |
| 203600 | | - const char *z; |
| 203601 | | - char *zOut; |
| 203602 | | - u32 j; |
| 203603 | | - u32 nOut = n; |
| 203604 | | - assert( pNode->eU==1 ); |
| 203605 | | - z = pNode->u.zJContent; |
| 203606 | | - zOut = sqlite3_malloc( nOut+1 ); |
| 203607 | | - if( zOut==0 ){ |
| 203608 | | - sqlite3_result_error_nomem(pCtx); |
| 203609 | | - break; |
| 203610 | | - } |
| 203611 | | - for(i=1, j=0; i<n-1; i++){ |
| 203612 | | - char c = z[i]; |
| 203613 | | - if( c=='\\' ){ |
| 203614 | | - c = z[++i]; |
| 203615 | | - if( c=='u' ){ |
| 203616 | | - u32 v = jsonHexToInt4(z+i+1); |
| 203617 | | - i += 4; |
| 203618 | | - if( v==0 ) break; |
| 203619 | | - if( v<=0x7f ){ |
| 203620 | | - zOut[j++] = (char)v; |
| 203621 | | - }else if( v<=0x7ff ){ |
| 203622 | | - zOut[j++] = (char)(0xc0 | (v>>6)); |
| 203623 | | - zOut[j++] = 0x80 | (v&0x3f); |
| 203624 | | - }else{ |
| 203625 | | - u32 vlo; |
| 203626 | | - if( (v&0xfc00)==0xd800 |
| 203627 | | - && i<n-6 |
| 203628 | | - && z[i+1]=='\\' |
| 203629 | | - && z[i+2]=='u' |
| 203630 | | - && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00 |
| 203631 | | - ){ |
| 203632 | | - /* We have a surrogate pair */ |
| 203633 | | - v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000; |
| 203634 | | - i += 6; |
| 203635 | | - zOut[j++] = 0xf0 | (v>>18); |
| 203636 | | - zOut[j++] = 0x80 | ((v>>12)&0x3f); |
| 203637 | | - zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 203638 | | - zOut[j++] = 0x80 | (v&0x3f); |
| 203639 | | - }else{ |
| 203640 | | - zOut[j++] = 0xe0 | (v>>12); |
| 203641 | | - zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 203642 | | - zOut[j++] = 0x80 | (v&0x3f); |
| 203643 | | - } |
| 203644 | | - } |
| 203645 | | - continue; |
| 203646 | | - }else if( c=='b' ){ |
| 203647 | | - c = '\b'; |
| 203648 | | - }else if( c=='f' ){ |
| 203649 | | - c = '\f'; |
| 203650 | | - }else if( c=='n' ){ |
| 203651 | | - c = '\n'; |
| 203652 | | - }else if( c=='r' ){ |
| 203653 | | - c = '\r'; |
| 203654 | | - }else if( c=='t' ){ |
| 203655 | | - c = '\t'; |
| 203656 | | - }else if( c=='v' ){ |
| 203657 | | - c = '\v'; |
| 203658 | | - }else if( c=='\'' || c=='"' || c=='/' || c=='\\' ){ |
| 203659 | | - /* pass through unchanged */ |
| 203660 | | - }else if( c=='0' ){ |
| 203661 | | - c = 0; |
| 203662 | | - }else if( c=='x' ){ |
| 203663 | | - c = (jsonHexToInt(z[i+1])<<4) | jsonHexToInt(z[i+2]); |
| 203664 | | - i += 2; |
| 203665 | | - }else if( c=='\r' && z[i+1]=='\n' ){ |
| 203666 | | - i++; |
| 203667 | | - continue; |
| 203668 | | - }else if( 0xe2==(u8)c ){ |
| 203669 | | - assert( 0x80==(u8)z[i+1] ); |
| 203670 | | - assert( 0xa8==(u8)z[i+2] || 0xa9==(u8)z[i+2] ); |
| 203671 | | - i += 2; |
| 203672 | | - continue; |
| 203673 | | - }else{ |
| 203674 | | - continue; |
| 203675 | | - } |
| 203676 | | - } /* end if( c=='\\' ) */ |
| 203677 | | - zOut[j++] = c; |
| 203678 | | - } /* end for() */ |
| 203679 | | - zOut[j] = 0; |
| 203680 | | - sqlite3_result_text(pCtx, zOut, j, sqlite3_free); |
| 203681 | | - } |
| 203682 | | - break; |
| 203683 | | - } |
| 203684 | | - case JSON_ARRAY: |
| 203685 | | - case JSON_OBJECT: { |
| 203686 | | - jsonReturnJson(pParse, pNode, pCtx, 0, omitSubtype); |
| 203687 | | - break; |
| 203688 | | - } |
| 203689 | | - } |
| 203690 | | -} |
| 203691 | | - |
| 203692 | | -/* Forward reference */ |
| 203693 | | -static int jsonParseAddNode(JsonParse*,u32,u32,const char*); |
| 203694 | | - |
| 203695 | | -/* |
| 203696 | | -** A macro to hint to the compiler that a function should not be |
| 203697 | | -** inlined. |
| 203698 | | -*/ |
| 203699 | | -#if defined(__GNUC__) |
| 203700 | | -# define JSON_NOINLINE __attribute__((noinline)) |
| 203701 | | -#elif defined(_MSC_VER) && _MSC_VER>=1310 |
| 203702 | | -# define JSON_NOINLINE __declspec(noinline) |
| 203703 | | -#else |
| 203704 | | -# define JSON_NOINLINE |
| 203705 | | -#endif |
| 203706 | | - |
| 203707 | | - |
| 203708 | | -/* |
| 203709 | | -** Add a single node to pParse->aNode after first expanding the |
| 203710 | | -** size of the aNode array. Return the index of the new node. |
| 203711 | | -** |
| 203712 | | -** If an OOM error occurs, set pParse->oom and return -1. |
| 203713 | | -*/ |
| 203714 | | -static JSON_NOINLINE int jsonParseAddNodeExpand( |
| 203715 | | - JsonParse *pParse, /* Append the node to this object */ |
| 203716 | | - u32 eType, /* Node type */ |
| 203717 | | - u32 n, /* Content size or sub-node count */ |
| 203718 | | - const char *zContent /* Content */ |
| 203719 | | -){ |
| 203720 | | - u32 nNew; |
| 203721 | | - JsonNode *pNew; |
| 203722 | | - assert( pParse->nNode>=pParse->nAlloc ); |
| 203723 | | - if( pParse->oom ) return -1; |
| 203724 | | - nNew = pParse->nAlloc*2 + 10; |
| 203725 | | - pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew); |
| 203726 | | - if( pNew==0 ){ |
| 203727 | | - pParse->oom = 1; |
| 203728 | | - return -1; |
| 203729 | | - } |
| 203730 | | - pParse->nAlloc = sqlite3_msize(pNew)/sizeof(JsonNode); |
| 203731 | | - pParse->aNode = pNew; |
| 203732 | | - assert( pParse->nNode<pParse->nAlloc ); |
| 203733 | | - return jsonParseAddNode(pParse, eType, n, zContent); |
| 203734 | | -} |
| 203735 | | - |
| 203736 | | -/* |
| 203737 | | -** Create a new JsonNode instance based on the arguments and append that |
| 203738 | | -** instance to the JsonParse. Return the index in pParse->aNode[] of the |
| 203739 | | -** new node, or -1 if a memory allocation fails. |
| 203740 | | -*/ |
| 203741 | | -static int jsonParseAddNode( |
| 203742 | | - JsonParse *pParse, /* Append the node to this object */ |
| 203743 | | - u32 eType, /* Node type */ |
| 203744 | | - u32 n, /* Content size or sub-node count */ |
| 203745 | | - const char *zContent /* Content */ |
| 203746 | | -){ |
| 203747 | | - JsonNode *p; |
| 203748 | | - assert( pParse->aNode!=0 || pParse->nNode>=pParse->nAlloc ); |
| 203749 | | - if( pParse->nNode>=pParse->nAlloc ){ |
| 203750 | | - return jsonParseAddNodeExpand(pParse, eType, n, zContent); |
| 203751 | | - } |
| 203752 | | - assert( pParse->aNode!=0 ); |
| 203753 | | - p = &pParse->aNode[pParse->nNode]; |
| 203754 | | - assert( p!=0 ); |
| 203755 | | - p->eType = (u8)(eType & 0xff); |
| 203756 | | - p->jnFlags = (u8)(eType >> 8); |
| 203757 | | - VVA( p->eU = zContent ? 1 : 0 ); |
| 203758 | | - p->n = n; |
| 203759 | | - p->u.zJContent = zContent; |
| 203760 | | - return pParse->nNode++; |
| 203761 | | -} |
| 203762 | | - |
| 203763 | | -/* |
| 203764 | | -** Add an array of new nodes to the current pParse->aNode array. |
| 203765 | | -** Return the index of the first node added. |
| 203766 | | -** |
| 203767 | | -** If an OOM error occurs, set pParse->oom. |
| 203768 | | -*/ |
| 203769 | | -static void jsonParseAddNodeArray( |
| 203770 | | - JsonParse *pParse, /* Append the node to this object */ |
| 203771 | | - JsonNode *aNode, /* Array of nodes to add */ |
| 203772 | | - u32 nNode /* Number of elements in aNew */ |
| 203773 | | -){ |
| 203774 | | - assert( aNode!=0 ); |
| 203775 | | - assert( nNode>=1 ); |
| 203776 | | - if( pParse->nNode + nNode > pParse->nAlloc ){ |
| 203777 | | - u32 nNew = pParse->nNode + nNode; |
| 203778 | | - JsonNode *aNew = sqlite3_realloc64(pParse->aNode, nNew*sizeof(JsonNode)); |
| 203779 | | - if( aNew==0 ){ |
| 203780 | | - pParse->oom = 1; |
| 203781 | | - return; |
| 203782 | | - } |
| 203783 | | - pParse->nAlloc = sqlite3_msize(aNew)/sizeof(JsonNode); |
| 203784 | | - pParse->aNode = aNew; |
| 203785 | | - } |
| 203786 | | - memcpy(&pParse->aNode[pParse->nNode], aNode, nNode*sizeof(JsonNode)); |
| 203787 | | - pParse->nNode += nNode; |
| 203788 | | -} |
| 203789 | | - |
| 203790 | | -/* |
| 203791 | | -** Add a new JSON_SUBST node. The node immediately following |
| 203792 | | -** this new node will be the substitute content for iNode. |
| 203793 | | -*/ |
| 203794 | | -static int jsonParseAddSubstNode( |
| 203795 | | - JsonParse *pParse, /* Add the JSON_SUBST here */ |
| 203796 | | - u32 iNode /* References this node */ |
| 203797 | | -){ |
| 203798 | | - int idx = jsonParseAddNode(pParse, JSON_SUBST, iNode, 0); |
| 203799 | | - if( pParse->oom ) return -1; |
| 203800 | | - pParse->aNode[iNode].jnFlags |= JNODE_REPLACE; |
| 203801 | | - pParse->aNode[idx].eU = 4; |
| 203802 | | - pParse->aNode[idx].u.iPrev = pParse->iSubst; |
| 203803 | | - pParse->iSubst = idx; |
| 203804 | | - pParse->hasMod = 1; |
| 203805 | | - pParse->useMod = 1; |
| 203806 | | - return idx; |
| 203807 | | -} |
| 203808 | | - |
| 203809 | 204069 | /* |
| 203810 | 204070 | ** Return true if z[] begins with 2 (or more) hexadecimal digits |
| 203811 | 204071 | */ |
| 203812 | 204072 | static int jsonIs2Hex(const char *z){ |
| 203813 | 204073 | return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]); |
| | @@ -203957,101 +204217,538 @@ |
| 203957 | 204217 | char eType; |
| 203958 | 204218 | char nRepl; |
| 203959 | 204219 | char *zMatch; |
| 203960 | 204220 | char *zRepl; |
| 203961 | 204221 | } aNanInfName[] = { |
| 203962 | | - { 'i', 'I', 3, JSON_REAL, 7, "inf", "9.0e999" }, |
| 203963 | | - { 'i', 'I', 8, JSON_REAL, 7, "infinity", "9.0e999" }, |
| 203964 | | - { 'n', 'N', 3, JSON_NULL, 4, "NaN", "null" }, |
| 203965 | | - { 'q', 'Q', 4, JSON_NULL, 4, "QNaN", "null" }, |
| 203966 | | - { 's', 'S', 4, JSON_NULL, 4, "SNaN", "null" }, |
| 203967 | | -}; |
| 203968 | | - |
| 203969 | | -/* |
| 203970 | | -** Parse a single JSON value which begins at pParse->zJson[i]. Return the |
| 203971 | | -** index of the first character past the end of the value parsed. |
| 203972 | | -** |
| 203973 | | -** Special return values: |
| 204222 | + { 'i', 'I', 3, JSONB_FLOAT, 7, "inf", "9.0e999" }, |
| 204223 | + { 'i', 'I', 8, JSONB_FLOAT, 7, "infinity", "9.0e999" }, |
| 204224 | + { 'n', 'N', 3, JSONB_NULL, 4, "NaN", "null" }, |
| 204225 | + { 'q', 'Q', 4, JSONB_NULL, 4, "QNaN", "null" }, |
| 204226 | + { 's', 'S', 4, JSONB_NULL, 4, "SNaN", "null" }, |
| 204227 | +}; |
| 204228 | + |
| 204229 | + |
| 204230 | +/* |
| 204231 | +** Report the wrong number of arguments for json_insert(), json_replace() |
| 204232 | +** or json_set(). |
| 204233 | +*/ |
| 204234 | +static void jsonWrongNumArgs( |
| 204235 | + sqlite3_context *pCtx, |
| 204236 | + const char *zFuncName |
| 204237 | +){ |
| 204238 | + char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments", |
| 204239 | + zFuncName); |
| 204240 | + sqlite3_result_error(pCtx, zMsg, -1); |
| 204241 | + sqlite3_free(zMsg); |
| 204242 | +} |
| 204243 | + |
| 204244 | +/**************************************************************************** |
| 204245 | +** Utility routines for dealing with the binary BLOB representation of JSON |
| 204246 | +****************************************************************************/ |
| 204247 | + |
| 204248 | +/* |
| 204249 | +** Expand pParse->aBlob so that it holds at least N bytes. |
| 204250 | +** |
| 204251 | +** Return the number of errors. |
| 204252 | +*/ |
| 204253 | +static int jsonBlobExpand(JsonParse *pParse, u32 N){ |
| 204254 | + u8 *aNew; |
| 204255 | + u32 t; |
| 204256 | + assert( N>pParse->nBlobAlloc ); |
| 204257 | + if( pParse->nBlobAlloc==0 ){ |
| 204258 | + t = 100; |
| 204259 | + }else{ |
| 204260 | + t = pParse->nBlobAlloc*2; |
| 204261 | + } |
| 204262 | + if( t<N ) t = N+100; |
| 204263 | + aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t); |
| 204264 | + if( aNew==0 ){ pParse->oom = 1; return 1; } |
| 204265 | + pParse->aBlob = aNew; |
| 204266 | + pParse->nBlobAlloc = t; |
| 204267 | + return 0; |
| 204268 | +} |
| 204269 | + |
| 204270 | +/* |
| 204271 | +** If pParse->aBlob is not previously editable (because it is taken |
| 204272 | +** from sqlite3_value_blob(), as indicated by the fact that |
| 204273 | +** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable |
| 204274 | +** by making a copy into space obtained from malloc. |
| 204275 | +** |
| 204276 | +** Return true on success. Return false on OOM. |
| 204277 | +*/ |
| 204278 | +static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){ |
| 204279 | + u8 *aOld; |
| 204280 | + u32 nSize; |
| 204281 | + assert( !pParse->bReadOnly ); |
| 204282 | + if( pParse->oom ) return 0; |
| 204283 | + if( pParse->nBlobAlloc>0 ) return 1; |
| 204284 | + aOld = pParse->aBlob; |
| 204285 | + nSize = pParse->nBlob + nExtra; |
| 204286 | + pParse->aBlob = 0; |
| 204287 | + if( jsonBlobExpand(pParse, nSize) ){ |
| 204288 | + return 0; |
| 204289 | + } |
| 204290 | + assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra ); |
| 204291 | + memcpy(pParse->aBlob, aOld, pParse->nBlob); |
| 204292 | + return 1; |
| 204293 | +} |
| 204294 | + |
| 204295 | +/* Expand pParse->aBlob and append one bytes. |
| 204296 | +*/ |
| 204297 | +static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte( |
| 204298 | + JsonParse *pParse, |
| 204299 | + u8 c |
| 204300 | +){ |
| 204301 | + jsonBlobExpand(pParse, pParse->nBlob+1); |
| 204302 | + if( pParse->oom==0 ){ |
| 204303 | + assert( pParse->nBlob+1<=pParse->nBlobAlloc ); |
| 204304 | + pParse->aBlob[pParse->nBlob++] = c; |
| 204305 | + } |
| 204306 | +} |
| 204307 | + |
| 204308 | +/* Append a single character. |
| 204309 | +*/ |
| 204310 | +static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){ |
| 204311 | + if( pParse->nBlob >= pParse->nBlobAlloc ){ |
| 204312 | + jsonBlobExpandAndAppendOneByte(pParse, c); |
| 204313 | + }else{ |
| 204314 | + pParse->aBlob[pParse->nBlob++] = c; |
| 204315 | + } |
| 204316 | +} |
| 204317 | + |
| 204318 | +/* Slow version of jsonBlobAppendNode() that first resizes the |
| 204319 | +** pParse->aBlob structure. |
| 204320 | +*/ |
| 204321 | +static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*); |
| 204322 | +static SQLITE_NOINLINE void jsonBlobExpandAndAppendNode( |
| 204323 | + JsonParse *pParse, |
| 204324 | + u8 eType, |
| 204325 | + u32 szPayload, |
| 204326 | + const void *aPayload |
| 204327 | +){ |
| 204328 | + if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return; |
| 204329 | + jsonBlobAppendNode(pParse, eType, szPayload, aPayload); |
| 204330 | +} |
| 204331 | + |
| 204332 | + |
| 204333 | +/* Append an node type byte together with the payload size and |
| 204334 | +** possibly also the payload. |
| 204335 | +** |
| 204336 | +** If aPayload is not NULL, then it is a pointer to the payload which |
| 204337 | +** is also appended. If aPayload is NULL, the pParse->aBlob[] array |
| 204338 | +** is resized (if necessary) so that it is big enough to hold the |
| 204339 | +** payload, but the payload is not appended and pParse->nBlob is left |
| 204340 | +** pointing to where the first byte of payload will eventually be. |
| 204341 | +*/ |
| 204342 | +static void jsonBlobAppendNode( |
| 204343 | + JsonParse *pParse, /* The JsonParse object under construction */ |
| 204344 | + u8 eType, /* Node type. One of JSONB_* */ |
| 204345 | + u32 szPayload, /* Number of bytes of payload */ |
| 204346 | + const void *aPayload /* The payload. Might be NULL */ |
| 204347 | +){ |
| 204348 | + u8 *a; |
| 204349 | + if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){ |
| 204350 | + jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload); |
| 204351 | + return; |
| 204352 | + } |
| 204353 | + assert( pParse->aBlob!=0 ); |
| 204354 | + a = &pParse->aBlob[pParse->nBlob]; |
| 204355 | + if( szPayload<=11 ){ |
| 204356 | + a[0] = eType | (szPayload<<4); |
| 204357 | + pParse->nBlob += 1; |
| 204358 | + }else if( szPayload<=0xff ){ |
| 204359 | + a[0] = eType | 0xc0; |
| 204360 | + a[1] = szPayload & 0xff; |
| 204361 | + pParse->nBlob += 2; |
| 204362 | + }else if( szPayload<=0xffff ){ |
| 204363 | + a[0] = eType | 0xd0; |
| 204364 | + a[1] = (szPayload >> 8) & 0xff; |
| 204365 | + a[2] = szPayload & 0xff; |
| 204366 | + pParse->nBlob += 3; |
| 204367 | + }else{ |
| 204368 | + a[0] = eType | 0xe0; |
| 204369 | + a[1] = (szPayload >> 24) & 0xff; |
| 204370 | + a[2] = (szPayload >> 16) & 0xff; |
| 204371 | + a[3] = (szPayload >> 8) & 0xff; |
| 204372 | + a[4] = szPayload & 0xff; |
| 204373 | + pParse->nBlob += 5; |
| 204374 | + } |
| 204375 | + if( aPayload ){ |
| 204376 | + pParse->nBlob += szPayload; |
| 204377 | + memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload); |
| 204378 | + } |
| 204379 | +} |
| 204380 | + |
| 204381 | +/* Change the payload size for the node at index i to be szPayload. |
| 204382 | +*/ |
| 204383 | +static int jsonBlobChangePayloadSize( |
| 204384 | + JsonParse *pParse, |
| 204385 | + u32 i, |
| 204386 | + u32 szPayload |
| 204387 | +){ |
| 204388 | + u8 *a; |
| 204389 | + u8 szType; |
| 204390 | + u8 nExtra; |
| 204391 | + u8 nNeeded; |
| 204392 | + int delta; |
| 204393 | + if( pParse->oom ) return 0; |
| 204394 | + a = &pParse->aBlob[i]; |
| 204395 | + szType = a[0]>>4; |
| 204396 | + if( szType<=11 ){ |
| 204397 | + nExtra = 0; |
| 204398 | + }else if( szType==12 ){ |
| 204399 | + nExtra = 1; |
| 204400 | + }else if( szType==13 ){ |
| 204401 | + nExtra = 2; |
| 204402 | + }else{ |
| 204403 | + nExtra = 4; |
| 204404 | + } |
| 204405 | + if( szPayload<=11 ){ |
| 204406 | + nNeeded = 0; |
| 204407 | + }else if( szPayload<=0xff ){ |
| 204408 | + nNeeded = 1; |
| 204409 | + }else if( szPayload<=0xffff ){ |
| 204410 | + nNeeded = 2; |
| 204411 | + }else{ |
| 204412 | + nNeeded = 4; |
| 204413 | + } |
| 204414 | + delta = nNeeded - nExtra; |
| 204415 | + if( delta ){ |
| 204416 | + u32 newSize = pParse->nBlob + delta; |
| 204417 | + if( delta>0 ){ |
| 204418 | + if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){ |
| 204419 | + return 0; /* OOM error. Error state recorded in pParse->oom. */ |
| 204420 | + } |
| 204421 | + a = &pParse->aBlob[i]; |
| 204422 | + memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1)); |
| 204423 | + }else{ |
| 204424 | + memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta)); |
| 204425 | + } |
| 204426 | + pParse->nBlob = newSize; |
| 204427 | + } |
| 204428 | + if( nNeeded==0 ){ |
| 204429 | + a[0] = (a[0] & 0x0f) | (szPayload<<4); |
| 204430 | + }else if( nNeeded==1 ){ |
| 204431 | + a[0] = (a[0] & 0x0f) | 0xc0; |
| 204432 | + a[1] = szPayload & 0xff; |
| 204433 | + }else if( nNeeded==2 ){ |
| 204434 | + a[0] = (a[0] & 0x0f) | 0xd0; |
| 204435 | + a[1] = (szPayload >> 8) & 0xff; |
| 204436 | + a[2] = szPayload & 0xff; |
| 204437 | + }else{ |
| 204438 | + a[0] = (a[0] & 0x0f) | 0xe0; |
| 204439 | + a[1] = (szPayload >> 24) & 0xff; |
| 204440 | + a[2] = (szPayload >> 16) & 0xff; |
| 204441 | + a[3] = (szPayload >> 8) & 0xff; |
| 204442 | + a[4] = szPayload & 0xff; |
| 204443 | + } |
| 204444 | + return delta; |
| 204445 | +} |
| 204446 | + |
| 204447 | +/* |
| 204448 | +** If z[0] is 'u' and is followed by exactly 4 hexadecimal character, |
| 204449 | +** then set *pOp to JSONB_TEXTJ and return true. If not, do not make |
| 204450 | +** any changes to *pOp and return false. |
| 204451 | +*/ |
| 204452 | +static int jsonIs4HexB(const char *z, int *pOp){ |
| 204453 | + if( z[0]!='u' ) return 0; |
| 204454 | + if( !jsonIs4Hex(&z[1]) ) return 0; |
| 204455 | + *pOp = JSONB_TEXTJ; |
| 204456 | + return 1; |
| 204457 | +} |
| 204458 | + |
| 204459 | +/* |
| 204460 | +** Check a single element of the JSONB in pParse for validity. |
| 204461 | +** |
| 204462 | +** The element to be checked starts at offset i and must end at on the |
| 204463 | +** last byte before iEnd. |
| 204464 | +** |
| 204465 | +** Return 0 if everything is correct. Return the 1-based byte offset of the |
| 204466 | +** error if a problem is detected. (In other words, if the error is at offset |
| 204467 | +** 0, return 1). |
| 204468 | +*/ |
| 204469 | +static u32 jsonbValidityCheck( |
| 204470 | + const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */ |
| 204471 | + u32 i, /* Start of element as pParse->aBlob[i] */ |
| 204472 | + u32 iEnd, /* One more than the last byte of the element */ |
| 204473 | + u32 iDepth /* Current nesting depth */ |
| 204474 | +){ |
| 204475 | + u32 n, sz, j, k; |
| 204476 | + const u8 *z; |
| 204477 | + u8 x; |
| 204478 | + if( iDepth>JSON_MAX_DEPTH ) return i+1; |
| 204479 | + sz = 0; |
| 204480 | + n = jsonbPayloadSize(pParse, i, &sz); |
| 204481 | + if( NEVER(n==0) ) return i+1; /* Checked by caller */ |
| 204482 | + if( NEVER(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */ |
| 204483 | + z = pParse->aBlob; |
| 204484 | + x = z[i] & 0x0f; |
| 204485 | + switch( x ){ |
| 204486 | + case JSONB_NULL: |
| 204487 | + case JSONB_TRUE: |
| 204488 | + case JSONB_FALSE: { |
| 204489 | + return n+sz==1 ? 0 : i+1; |
| 204490 | + } |
| 204491 | + case JSONB_INT: { |
| 204492 | + if( sz<1 ) return i+1; |
| 204493 | + j = i+n; |
| 204494 | + if( z[j]=='-' ){ |
| 204495 | + j++; |
| 204496 | + if( sz<2 ) return i+1; |
| 204497 | + } |
| 204498 | + k = i+n+sz; |
| 204499 | + while( j<k ){ |
| 204500 | + if( sqlite3Isdigit(z[j]) ){ |
| 204501 | + j++; |
| 204502 | + }else{ |
| 204503 | + return j+1; |
| 204504 | + } |
| 204505 | + } |
| 204506 | + return 0; |
| 204507 | + } |
| 204508 | + case JSONB_INT5: { |
| 204509 | + if( sz<3 ) return i+1; |
| 204510 | + j = i+n; |
| 204511 | + if( z[j]=='-' ){ |
| 204512 | + if( sz<4 ) return i+1; |
| 204513 | + j++; |
| 204514 | + } |
| 204515 | + if( z[j]!='0' ) return i+1; |
| 204516 | + if( z[j+1]!='x' && z[j+1]!='X' ) return j+2; |
| 204517 | + j += 2; |
| 204518 | + k = i+n+sz; |
| 204519 | + while( j<k ){ |
| 204520 | + if( sqlite3Isxdigit(z[j]) ){ |
| 204521 | + j++; |
| 204522 | + }else{ |
| 204523 | + return j+1; |
| 204524 | + } |
| 204525 | + } |
| 204526 | + return 0; |
| 204527 | + } |
| 204528 | + case JSONB_FLOAT: |
| 204529 | + case JSONB_FLOAT5: { |
| 204530 | + u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */ |
| 204531 | + if( sz<2 ) return i+1; |
| 204532 | + j = i+n; |
| 204533 | + k = j+sz; |
| 204534 | + if( z[j]=='-' ){ |
| 204535 | + j++; |
| 204536 | + if( sz<3 ) return i+1; |
| 204537 | + } |
| 204538 | + if( z[j]=='.' ){ |
| 204539 | + if( x==JSONB_FLOAT ) return j+1; |
| 204540 | + if( !sqlite3Isdigit(z[j+1]) ) return j+1; |
| 204541 | + j += 2; |
| 204542 | + seen = 1; |
| 204543 | + }else if( z[j]=='0' && x==JSONB_FLOAT ){ |
| 204544 | + if( j+3>k ) return j+1; |
| 204545 | + if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1; |
| 204546 | + j++; |
| 204547 | + } |
| 204548 | + for(; j<k; j++){ |
| 204549 | + if( sqlite3Isdigit(z[j]) ) continue; |
| 204550 | + if( z[j]=='.' ){ |
| 204551 | + if( seen>0 ) return j+1; |
| 204552 | + if( x==JSONB_FLOAT && (j==k-1 || !sqlite3Isdigit(z[j+1])) ){ |
| 204553 | + return j+1; |
| 204554 | + } |
| 204555 | + seen = 1; |
| 204556 | + continue; |
| 204557 | + } |
| 204558 | + if( z[j]=='e' || z[j]=='E' ){ |
| 204559 | + if( seen==2 ) return j+1; |
| 204560 | + if( j==k-1 ) return j+1; |
| 204561 | + if( z[j+1]=='+' || z[j+1]=='-' ){ |
| 204562 | + j++; |
| 204563 | + if( j==k-1 ) return j+1; |
| 204564 | + } |
| 204565 | + seen = 2; |
| 204566 | + continue; |
| 204567 | + } |
| 204568 | + return j+1; |
| 204569 | + } |
| 204570 | + if( seen==0 ) return i+1; |
| 204571 | + return 0; |
| 204572 | + } |
| 204573 | + case JSONB_TEXT: { |
| 204574 | + j = i+n; |
| 204575 | + k = j+sz; |
| 204576 | + while( j<k ){ |
| 204577 | + if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1; |
| 204578 | + j++; |
| 204579 | + } |
| 204580 | + return 0; |
| 204581 | + } |
| 204582 | + case JSONB_TEXTJ: |
| 204583 | + case JSONB_TEXT5: { |
| 204584 | + j = i+n; |
| 204585 | + k = j+sz; |
| 204586 | + while( j<k ){ |
| 204587 | + if( !jsonIsOk[z[j]] && z[j]!='\'' ){ |
| 204588 | + if( z[j]=='"' ){ |
| 204589 | + if( x==JSONB_TEXTJ ) return j+1; |
| 204590 | + }else if( z[j]!='\\' || j+1>=k ){ |
| 204591 | + return j+1; |
| 204592 | + }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){ |
| 204593 | + j++; |
| 204594 | + }else if( z[j+1]=='u' ){ |
| 204595 | + if( j+5>=k ) return j+1; |
| 204596 | + if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1; |
| 204597 | + j++; |
| 204598 | + }else if( x!=JSONB_TEXT5 ){ |
| 204599 | + return j+1; |
| 204600 | + }else{ |
| 204601 | + u32 c = 0; |
| 204602 | + u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c); |
| 204603 | + if( c==JSON_INVALID_CHAR ) return j+1; |
| 204604 | + j += szC - 1; |
| 204605 | + } |
| 204606 | + } |
| 204607 | + j++; |
| 204608 | + } |
| 204609 | + return 0; |
| 204610 | + } |
| 204611 | + case JSONB_TEXTRAW: { |
| 204612 | + return 0; |
| 204613 | + } |
| 204614 | + case JSONB_ARRAY: { |
| 204615 | + u32 sub; |
| 204616 | + j = i+n; |
| 204617 | + k = j+sz; |
| 204618 | + while( j<k ){ |
| 204619 | + sz = 0; |
| 204620 | + n = jsonbPayloadSize(pParse, j, &sz); |
| 204621 | + if( n==0 ) return j+1; |
| 204622 | + if( j+n+sz>k ) return j+1; |
| 204623 | + sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1); |
| 204624 | + if( sub ) return sub; |
| 204625 | + j += n + sz; |
| 204626 | + } |
| 204627 | + assert( j==k ); |
| 204628 | + return 0; |
| 204629 | + } |
| 204630 | + case JSONB_OBJECT: { |
| 204631 | + u32 cnt = 0; |
| 204632 | + u32 sub; |
| 204633 | + j = i+n; |
| 204634 | + k = j+sz; |
| 204635 | + while( j<k ){ |
| 204636 | + sz = 0; |
| 204637 | + n = jsonbPayloadSize(pParse, j, &sz); |
| 204638 | + if( n==0 ) return j+1; |
| 204639 | + if( j+n+sz>k ) return j+1; |
| 204640 | + if( (cnt & 1)==0 ){ |
| 204641 | + x = z[j] & 0x0f; |
| 204642 | + if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return j+1; |
| 204643 | + } |
| 204644 | + sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1); |
| 204645 | + if( sub ) return sub; |
| 204646 | + cnt++; |
| 204647 | + j += n + sz; |
| 204648 | + } |
| 204649 | + assert( j==k ); |
| 204650 | + if( (cnt & 1)!=0 ) return j+1; |
| 204651 | + return 0; |
| 204652 | + } |
| 204653 | + default: { |
| 204654 | + return i+1; |
| 204655 | + } |
| 204656 | + } |
| 204657 | +} |
| 204658 | + |
| 204659 | +/* |
| 204660 | +** Translate a single element of JSON text at pParse->zJson[i] into |
| 204661 | +** its equivalent binary JSONB representation. Append the translation into |
| 204662 | +** pParse->aBlob[] beginning at pParse->nBlob. The size of |
| 204663 | +** pParse->aBlob[] is increased as necessary. |
| 204664 | +** |
| 204665 | +** Return the index of the first character past the end of the element parsed, |
| 204666 | +** or one of the following special result codes: |
| 203974 | 204667 | ** |
| 203975 | 204668 | ** 0 End of input |
| 203976 | | -** -1 Syntax error |
| 203977 | | -** -2 '}' seen |
| 203978 | | -** -3 ']' seen |
| 203979 | | -** -4 ',' seen |
| 203980 | | -** -5 ':' seen |
| 204669 | +** -1 Syntax error or OOM |
| 204670 | +** -2 '}' seen \ |
| 204671 | +** -3 ']' seen \___ For these returns, pParse->iErr is set to |
| 204672 | +** -4 ',' seen / the index in zJson[] of the seen character |
| 204673 | +** -5 ':' seen / |
| 203981 | 204674 | */ |
| 203982 | | -static int jsonParseValue(JsonParse *pParse, u32 i){ |
| 204675 | +static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){ |
| 203983 | 204676 | char c; |
| 203984 | 204677 | u32 j; |
| 203985 | | - int iThis; |
| 204678 | + u32 iThis, iStart; |
| 203986 | 204679 | int x; |
| 203987 | | - JsonNode *pNode; |
| 204680 | + u8 t; |
| 203988 | 204681 | const char *z = pParse->zJson; |
| 203989 | 204682 | json_parse_restart: |
| 203990 | 204683 | switch( (u8)z[i] ){ |
| 203991 | 204684 | case '{': { |
| 203992 | 204685 | /* Parse object */ |
| 203993 | | - iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
| 203994 | | - if( iThis<0 ) return -1; |
| 204686 | + iThis = pParse->nBlob; |
| 204687 | + jsonBlobAppendNode(pParse, JSONB_OBJECT, pParse->nJson-i, 0); |
| 203995 | 204688 | if( ++pParse->iDepth > JSON_MAX_DEPTH ){ |
| 203996 | 204689 | pParse->iErr = i; |
| 203997 | 204690 | return -1; |
| 203998 | 204691 | } |
| 204692 | + iStart = pParse->nBlob; |
| 203999 | 204693 | for(j=i+1;;j++){ |
| 204000 | | - u32 nNode = pParse->nNode; |
| 204001 | | - x = jsonParseValue(pParse, j); |
| 204694 | + u32 iBlob = pParse->nBlob; |
| 204695 | + x = jsonTranslateTextToBlob(pParse, j); |
| 204002 | 204696 | if( x<=0 ){ |
| 204697 | + int op; |
| 204003 | 204698 | if( x==(-2) ){ |
| 204004 | 204699 | j = pParse->iErr; |
| 204005 | | - if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1; |
| 204700 | + if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1; |
| 204006 | 204701 | break; |
| 204007 | 204702 | } |
| 204008 | 204703 | j += json5Whitespace(&z[j]); |
| 204704 | + op = JSONB_TEXT; |
| 204009 | 204705 | if( sqlite3JsonId1(z[j]) |
| 204010 | | - || (z[j]=='\\' && z[j+1]=='u' && jsonIs4Hex(&z[j+2])) |
| 204706 | + || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op)) |
| 204011 | 204707 | ){ |
| 204012 | 204708 | int k = j+1; |
| 204013 | 204709 | while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0) |
| 204014 | | - || (z[k]=='\\' && z[k+1]=='u' && jsonIs4Hex(&z[k+2])) |
| 204710 | + || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op)) |
| 204015 | 204711 | ){ |
| 204016 | 204712 | k++; |
| 204017 | 204713 | } |
| 204018 | | - jsonParseAddNode(pParse, JSON_STRING | (JNODE_RAW<<8), k-j, &z[j]); |
| 204714 | + assert( iBlob==pParse->nBlob ); |
| 204715 | + jsonBlobAppendNode(pParse, op, k-j, &z[j]); |
| 204019 | 204716 | pParse->hasNonstd = 1; |
| 204020 | 204717 | x = k; |
| 204021 | 204718 | }else{ |
| 204022 | 204719 | if( x!=-1 ) pParse->iErr = j; |
| 204023 | 204720 | return -1; |
| 204024 | 204721 | } |
| 204025 | 204722 | } |
| 204026 | 204723 | if( pParse->oom ) return -1; |
| 204027 | | - pNode = &pParse->aNode[nNode]; |
| 204028 | | - if( pNode->eType!=JSON_STRING ){ |
| 204724 | + t = pParse->aBlob[iBlob] & 0x0f; |
| 204725 | + if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){ |
| 204029 | 204726 | pParse->iErr = j; |
| 204030 | 204727 | return -1; |
| 204031 | 204728 | } |
| 204032 | | - pNode->jnFlags |= JNODE_LABEL; |
| 204033 | 204729 | j = x; |
| 204034 | 204730 | if( z[j]==':' ){ |
| 204035 | 204731 | j++; |
| 204036 | 204732 | }else{ |
| 204037 | | - if( fast_isspace(z[j]) ){ |
| 204038 | | - do{ j++; }while( fast_isspace(z[j]) ); |
| 204733 | + if( jsonIsspace(z[j]) ){ |
| 204734 | + /* strspn() is not helpful here */ |
| 204735 | + do{ j++; }while( jsonIsspace(z[j]) ); |
| 204039 | 204736 | if( z[j]==':' ){ |
| 204040 | 204737 | j++; |
| 204041 | 204738 | goto parse_object_value; |
| 204042 | 204739 | } |
| 204043 | 204740 | } |
| 204044 | | - x = jsonParseValue(pParse, j); |
| 204741 | + x = jsonTranslateTextToBlob(pParse, j); |
| 204045 | 204742 | if( x!=(-5) ){ |
| 204046 | 204743 | if( x!=(-1) ) pParse->iErr = j; |
| 204047 | 204744 | return -1; |
| 204048 | 204745 | } |
| 204049 | 204746 | j = pParse->iErr+1; |
| 204050 | 204747 | } |
| 204051 | 204748 | parse_object_value: |
| 204052 | | - x = jsonParseValue(pParse, j); |
| 204749 | + x = jsonTranslateTextToBlob(pParse, j); |
| 204053 | 204750 | if( x<=0 ){ |
| 204054 | 204751 | if( x!=(-1) ) pParse->iErr = j; |
| 204055 | 204752 | return -1; |
| 204056 | 204753 | } |
| 204057 | 204754 | j = x; |
| | @@ -204058,19 +204755,19 @@ |
| 204058 | 204755 | if( z[j]==',' ){ |
| 204059 | 204756 | continue; |
| 204060 | 204757 | }else if( z[j]=='}' ){ |
| 204061 | 204758 | break; |
| 204062 | 204759 | }else{ |
| 204063 | | - if( fast_isspace(z[j]) ){ |
| 204064 | | - do{ j++; }while( fast_isspace(z[j]) ); |
| 204760 | + if( jsonIsspace(z[j]) ){ |
| 204761 | + j += 1 + (u32)strspn(&z[j+1], jsonSpaces); |
| 204065 | 204762 | if( z[j]==',' ){ |
| 204066 | 204763 | continue; |
| 204067 | 204764 | }else if( z[j]=='}' ){ |
| 204068 | 204765 | break; |
| 204069 | 204766 | } |
| 204070 | 204767 | } |
| 204071 | | - x = jsonParseValue(pParse, j); |
| 204768 | + x = jsonTranslateTextToBlob(pParse, j); |
| 204072 | 204769 | if( x==(-4) ){ |
| 204073 | 204770 | j = pParse->iErr; |
| 204074 | 204771 | continue; |
| 204075 | 204772 | } |
| 204076 | 204773 | if( x==(-2) ){ |
| | @@ -204079,29 +204776,30 @@ |
| 204079 | 204776 | } |
| 204080 | 204777 | } |
| 204081 | 204778 | pParse->iErr = j; |
| 204082 | 204779 | return -1; |
| 204083 | 204780 | } |
| 204084 | | - pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; |
| 204781 | + jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart); |
| 204085 | 204782 | pParse->iDepth--; |
| 204086 | 204783 | return j+1; |
| 204087 | 204784 | } |
| 204088 | 204785 | case '[': { |
| 204089 | 204786 | /* Parse array */ |
| 204090 | | - iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
| 204091 | | - if( iThis<0 ) return -1; |
| 204787 | + iThis = pParse->nBlob; |
| 204788 | + jsonBlobAppendNode(pParse, JSONB_ARRAY, pParse->nJson - i, 0); |
| 204789 | + iStart = pParse->nBlob; |
| 204790 | + if( pParse->oom ) return -1; |
| 204092 | 204791 | if( ++pParse->iDepth > JSON_MAX_DEPTH ){ |
| 204093 | 204792 | pParse->iErr = i; |
| 204094 | 204793 | return -1; |
| 204095 | 204794 | } |
| 204096 | | - memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u)); |
| 204097 | 204795 | for(j=i+1;;j++){ |
| 204098 | | - x = jsonParseValue(pParse, j); |
| 204796 | + x = jsonTranslateTextToBlob(pParse, j); |
| 204099 | 204797 | if( x<=0 ){ |
| 204100 | 204798 | if( x==(-3) ){ |
| 204101 | 204799 | j = pParse->iErr; |
| 204102 | | - if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1; |
| 204800 | + if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1; |
| 204103 | 204801 | break; |
| 204104 | 204802 | } |
| 204105 | 204803 | if( x!=(-1) ) pParse->iErr = j; |
| 204106 | 204804 | return -1; |
| 204107 | 204805 | } |
| | @@ -204109,19 +204807,19 @@ |
| 204109 | 204807 | if( z[j]==',' ){ |
| 204110 | 204808 | continue; |
| 204111 | 204809 | }else if( z[j]==']' ){ |
| 204112 | 204810 | break; |
| 204113 | 204811 | }else{ |
| 204114 | | - if( fast_isspace(z[j]) ){ |
| 204115 | | - do{ j++; }while( fast_isspace(z[j]) ); |
| 204812 | + if( jsonIsspace(z[j]) ){ |
| 204813 | + j += 1 + (u32)strspn(&z[j+1], jsonSpaces); |
| 204116 | 204814 | if( z[j]==',' ){ |
| 204117 | 204815 | continue; |
| 204118 | 204816 | }else if( z[j]==']' ){ |
| 204119 | 204817 | break; |
| 204120 | 204818 | } |
| 204121 | 204819 | } |
| 204122 | | - x = jsonParseValue(pParse, j); |
| 204820 | + x = jsonTranslateTextToBlob(pParse, j); |
| 204123 | 204821 | if( x==(-4) ){ |
| 204124 | 204822 | j = pParse->iErr; |
| 204125 | 204823 | continue; |
| 204126 | 204824 | } |
| 204127 | 204825 | if( x==(-3) ){ |
| | @@ -204130,86 +204828,98 @@ |
| 204130 | 204828 | } |
| 204131 | 204829 | } |
| 204132 | 204830 | pParse->iErr = j; |
| 204133 | 204831 | return -1; |
| 204134 | 204832 | } |
| 204135 | | - pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; |
| 204833 | + jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart); |
| 204136 | 204834 | pParse->iDepth--; |
| 204137 | 204835 | return j+1; |
| 204138 | 204836 | } |
| 204139 | 204837 | case '\'': { |
| 204140 | | - u8 jnFlags; |
| 204838 | + u8 opcode; |
| 204141 | 204839 | char cDelim; |
| 204142 | 204840 | pParse->hasNonstd = 1; |
| 204143 | | - jnFlags = JNODE_JSON5; |
| 204841 | + opcode = JSONB_TEXT; |
| 204144 | 204842 | goto parse_string; |
| 204145 | 204843 | case '"': |
| 204146 | 204844 | /* Parse string */ |
| 204147 | | - jnFlags = 0; |
| 204845 | + opcode = JSONB_TEXT; |
| 204148 | 204846 | parse_string: |
| 204149 | 204847 | cDelim = z[i]; |
| 204150 | | - for(j=i+1; 1; j++){ |
| 204151 | | - if( jsonIsOk[(unsigned char)z[j]] ) continue; |
| 204848 | + j = i+1; |
| 204849 | + while( 1 /*exit-by-break*/ ){ |
| 204850 | + if( jsonIsOk[(u8)z[j]] ){ |
| 204851 | + if( !jsonIsOk[(u8)z[j+1]] ){ |
| 204852 | + j += 1; |
| 204853 | + }else if( !jsonIsOk[(u8)z[j+2]] ){ |
| 204854 | + j += 2; |
| 204855 | + }else{ |
| 204856 | + j += 3; |
| 204857 | + continue; |
| 204858 | + } |
| 204859 | + } |
| 204152 | 204860 | c = z[j]; |
| 204153 | 204861 | if( c==cDelim ){ |
| 204154 | 204862 | break; |
| 204155 | 204863 | }else if( c=='\\' ){ |
| 204156 | 204864 | c = z[++j]; |
| 204157 | 204865 | if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f' |
| 204158 | 204866 | || c=='n' || c=='r' || c=='t' |
| 204159 | 204867 | || (c=='u' && jsonIs4Hex(&z[j+1])) ){ |
| 204160 | | - jnFlags |= JNODE_ESCAPE; |
| 204868 | + if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ; |
| 204161 | 204869 | }else if( c=='\'' || c=='0' || c=='v' || c=='\n' |
| 204162 | 204870 | || (0xe2==(u8)c && 0x80==(u8)z[j+1] |
| 204163 | 204871 | && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])) |
| 204164 | 204872 | || (c=='x' && jsonIs2Hex(&z[j+1])) ){ |
| 204165 | | - jnFlags |= (JNODE_ESCAPE|JNODE_JSON5); |
| 204873 | + opcode = JSONB_TEXT5; |
| 204166 | 204874 | pParse->hasNonstd = 1; |
| 204167 | 204875 | }else if( c=='\r' ){ |
| 204168 | 204876 | if( z[j+1]=='\n' ) j++; |
| 204169 | | - jnFlags |= (JNODE_ESCAPE|JNODE_JSON5); |
| 204877 | + opcode = JSONB_TEXT5; |
| 204170 | 204878 | pParse->hasNonstd = 1; |
| 204171 | 204879 | }else{ |
| 204172 | 204880 | pParse->iErr = j; |
| 204173 | 204881 | return -1; |
| 204174 | 204882 | } |
| 204175 | 204883 | }else if( c<=0x1f ){ |
| 204176 | 204884 | /* Control characters are not allowed in strings */ |
| 204177 | 204885 | pParse->iErr = j; |
| 204178 | 204886 | return -1; |
| 204887 | + }else if( c=='"' ){ |
| 204888 | + opcode = JSONB_TEXT5; |
| 204179 | 204889 | } |
| 204890 | + j++; |
| 204180 | 204891 | } |
| 204181 | | - jsonParseAddNode(pParse, JSON_STRING | (jnFlags<<8), j+1-i, &z[i]); |
| 204892 | + jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]); |
| 204182 | 204893 | return j+1; |
| 204183 | 204894 | } |
| 204184 | 204895 | case 't': { |
| 204185 | 204896 | if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){ |
| 204186 | | - jsonParseAddNode(pParse, JSON_TRUE, 0, 0); |
| 204897 | + jsonBlobAppendOneByte(pParse, JSONB_TRUE); |
| 204187 | 204898 | return i+4; |
| 204188 | 204899 | } |
| 204189 | 204900 | pParse->iErr = i; |
| 204190 | 204901 | return -1; |
| 204191 | 204902 | } |
| 204192 | 204903 | case 'f': { |
| 204193 | 204904 | if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){ |
| 204194 | | - jsonParseAddNode(pParse, JSON_FALSE, 0, 0); |
| 204905 | + jsonBlobAppendOneByte(pParse, JSONB_FALSE); |
| 204195 | 204906 | return i+5; |
| 204196 | 204907 | } |
| 204197 | 204908 | pParse->iErr = i; |
| 204198 | 204909 | return -1; |
| 204199 | 204910 | } |
| 204200 | 204911 | case '+': { |
| 204201 | | - u8 seenDP, seenE, jnFlags; |
| 204912 | + u8 seenE; |
| 204202 | 204913 | pParse->hasNonstd = 1; |
| 204203 | | - jnFlags = JNODE_JSON5; |
| 204914 | + t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ |
| 204204 | 204915 | goto parse_number; |
| 204205 | 204916 | case '.': |
| 204206 | 204917 | if( sqlite3Isdigit(z[i+1]) ){ |
| 204207 | 204918 | pParse->hasNonstd = 1; |
| 204208 | | - jnFlags = JNODE_JSON5; |
| 204919 | + t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ |
| 204209 | 204920 | seenE = 0; |
| 204210 | | - seenDP = JSON_REAL; |
| 204211 | 204921 | goto parse_number_2; |
| 204212 | 204922 | } |
| 204213 | 204923 | pParse->iErr = i; |
| 204214 | 204924 | return -1; |
| 204215 | 204925 | case '-': |
| | @@ -204222,25 +204932,24 @@ |
| 204222 | 204932 | case '6': |
| 204223 | 204933 | case '7': |
| 204224 | 204934 | case '8': |
| 204225 | 204935 | case '9': |
| 204226 | 204936 | /* Parse number */ |
| 204227 | | - jnFlags = 0; |
| 204937 | + t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ |
| 204228 | 204938 | parse_number: |
| 204229 | | - seenDP = JSON_INT; |
| 204230 | 204939 | seenE = 0; |
| 204231 | 204940 | assert( '-' < '0' ); |
| 204232 | 204941 | assert( '+' < '0' ); |
| 204233 | 204942 | assert( '.' < '0' ); |
| 204234 | 204943 | c = z[i]; |
| 204235 | 204944 | |
| 204236 | 204945 | if( c<='0' ){ |
| 204237 | 204946 | if( c=='0' ){ |
| 204238 | 204947 | if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){ |
| 204239 | | - assert( seenDP==JSON_INT ); |
| 204948 | + assert( t==0x00 ); |
| 204240 | 204949 | pParse->hasNonstd = 1; |
| 204241 | | - jnFlags |= JNODE_JSON5; |
| 204950 | + t = 0x01; |
| 204242 | 204951 | for(j=i+3; sqlite3Isxdigit(z[j]); j++){} |
| 204243 | 204952 | goto parse_number_finish; |
| 204244 | 204953 | }else if( sqlite3Isdigit(z[i+1]) ){ |
| 204245 | 204954 | pParse->iErr = i+1; |
| 204246 | 204955 | return -1; |
| | @@ -204253,19 +204962,19 @@ |
| 204253 | 204962 | if( (z[i+1]=='I' || z[i+1]=='i') |
| 204254 | 204963 | && sqlite3StrNICmp(&z[i+1], "inf",3)==0 |
| 204255 | 204964 | ){ |
| 204256 | 204965 | pParse->hasNonstd = 1; |
| 204257 | 204966 | if( z[i]=='-' ){ |
| 204258 | | - jsonParseAddNode(pParse, JSON_REAL, 8, "-9.0e999"); |
| 204967 | + jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999"); |
| 204259 | 204968 | }else{ |
| 204260 | | - jsonParseAddNode(pParse, JSON_REAL, 7, "9.0e999"); |
| 204969 | + jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); |
| 204261 | 204970 | } |
| 204262 | 204971 | return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4); |
| 204263 | 204972 | } |
| 204264 | 204973 | if( z[i+1]=='.' ){ |
| 204265 | 204974 | pParse->hasNonstd = 1; |
| 204266 | | - jnFlags |= JNODE_JSON5; |
| 204975 | + t |= 0x01; |
| 204267 | 204976 | goto parse_number_2; |
| 204268 | 204977 | } |
| 204269 | 204978 | pParse->iErr = i; |
| 204270 | 204979 | return -1; |
| 204271 | 204980 | } |
| | @@ -204273,44 +204982,45 @@ |
| 204273 | 204982 | if( sqlite3Isdigit(z[i+2]) ){ |
| 204274 | 204983 | pParse->iErr = i+1; |
| 204275 | 204984 | return -1; |
| 204276 | 204985 | }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){ |
| 204277 | 204986 | pParse->hasNonstd = 1; |
| 204278 | | - jnFlags |= JNODE_JSON5; |
| 204987 | + t |= 0x01; |
| 204279 | 204988 | for(j=i+4; sqlite3Isxdigit(z[j]); j++){} |
| 204280 | 204989 | goto parse_number_finish; |
| 204281 | 204990 | } |
| 204282 | 204991 | } |
| 204283 | 204992 | } |
| 204284 | 204993 | } |
| 204994 | + |
| 204285 | 204995 | parse_number_2: |
| 204286 | 204996 | for(j=i+1;; j++){ |
| 204287 | 204997 | c = z[j]; |
| 204288 | 204998 | if( sqlite3Isdigit(c) ) continue; |
| 204289 | 204999 | if( c=='.' ){ |
| 204290 | | - if( seenDP==JSON_REAL ){ |
| 205000 | + if( (t & 0x02)!=0 ){ |
| 204291 | 205001 | pParse->iErr = j; |
| 204292 | 205002 | return -1; |
| 204293 | 205003 | } |
| 204294 | | - seenDP = JSON_REAL; |
| 205004 | + t |= 0x02; |
| 204295 | 205005 | continue; |
| 204296 | 205006 | } |
| 204297 | 205007 | if( c=='e' || c=='E' ){ |
| 204298 | 205008 | if( z[j-1]<'0' ){ |
| 204299 | 205009 | if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){ |
| 204300 | 205010 | pParse->hasNonstd = 1; |
| 204301 | | - jnFlags |= JNODE_JSON5; |
| 205011 | + t |= 0x01; |
| 204302 | 205012 | }else{ |
| 204303 | 205013 | pParse->iErr = j; |
| 204304 | 205014 | return -1; |
| 204305 | 205015 | } |
| 204306 | 205016 | } |
| 204307 | 205017 | if( seenE ){ |
| 204308 | 205018 | pParse->iErr = j; |
| 204309 | 205019 | return -1; |
| 204310 | 205020 | } |
| 204311 | | - seenDP = JSON_REAL; |
| 205021 | + t |= 0x02; |
| 204312 | 205022 | seenE = 1; |
| 204313 | 205023 | c = z[j+1]; |
| 204314 | 205024 | if( c=='+' || c=='-' ){ |
| 204315 | 205025 | j++; |
| 204316 | 205026 | c = z[j+1]; |
| | @@ -204324,18 +205034,22 @@ |
| 204324 | 205034 | break; |
| 204325 | 205035 | } |
| 204326 | 205036 | if( z[j-1]<'0' ){ |
| 204327 | 205037 | if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){ |
| 204328 | 205038 | pParse->hasNonstd = 1; |
| 204329 | | - jnFlags |= JNODE_JSON5; |
| 205039 | + t |= 0x01; |
| 204330 | 205040 | }else{ |
| 204331 | 205041 | pParse->iErr = j; |
| 204332 | 205042 | return -1; |
| 204333 | 205043 | } |
| 204334 | 205044 | } |
| 204335 | 205045 | parse_number_finish: |
| 204336 | | - jsonParseAddNode(pParse, seenDP | (jnFlags<<8), j - i, &z[i]); |
| 205046 | + assert( JSONB_INT+0x01==JSONB_INT5 ); |
| 205047 | + assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 ); |
| 205048 | + assert( JSONB_INT+0x02==JSONB_FLOAT ); |
| 205049 | + if( z[i]=='+' ) i++; |
| 205050 | + jsonBlobAppendNode(pParse, JSONB_INT+t, j-i, &z[i]); |
| 204337 | 205051 | return j; |
| 204338 | 205052 | } |
| 204339 | 205053 | case '}': { |
| 204340 | 205054 | pParse->iErr = i; |
| 204341 | 205055 | return -2; /* End of {...} */ |
| | @@ -204357,13 +205071,11 @@ |
| 204357 | 205071 | } |
| 204358 | 205072 | case 0x09: |
| 204359 | 205073 | case 0x0a: |
| 204360 | 205074 | case 0x0d: |
| 204361 | 205075 | case 0x20: { |
| 204362 | | - do{ |
| 204363 | | - i++; |
| 204364 | | - }while( fast_isspace(z[i]) ); |
| 205076 | + i += 1 + (u32)strspn(&z[i+1], jsonSpaces); |
| 204365 | 205077 | goto json_parse_restart; |
| 204366 | 205078 | } |
| 204367 | 205079 | case 0x0b: |
| 204368 | 205080 | case 0x0c: |
| 204369 | 205081 | case '/': |
| | @@ -204381,11 +205093,11 @@ |
| 204381 | 205093 | pParse->iErr = i; |
| 204382 | 205094 | return -1; |
| 204383 | 205095 | } |
| 204384 | 205096 | case 'n': { |
| 204385 | 205097 | if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){ |
| 204386 | | - jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 205098 | + jsonBlobAppendOneByte(pParse, JSONB_NULL); |
| 204387 | 205099 | return i+4; |
| 204388 | 205100 | } |
| 204389 | 205101 | /* fall-through into the default case that checks for NaN */ |
| 204390 | 205102 | } |
| 204391 | 205103 | default: { |
| | @@ -204397,43 +205109,53 @@ |
| 204397 | 205109 | nn = aNanInfName[k].n; |
| 204398 | 205110 | if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){ |
| 204399 | 205111 | continue; |
| 204400 | 205112 | } |
| 204401 | 205113 | if( sqlite3Isalnum(z[i+nn]) ) continue; |
| 204402 | | - jsonParseAddNode(pParse, aNanInfName[k].eType, |
| 204403 | | - aNanInfName[k].nRepl, aNanInfName[k].zRepl); |
| 205114 | + if( aNanInfName[k].eType==JSONB_FLOAT ){ |
| 205115 | + jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); |
| 205116 | + }else{ |
| 205117 | + jsonBlobAppendOneByte(pParse, JSONB_NULL); |
| 205118 | + } |
| 204404 | 205119 | pParse->hasNonstd = 1; |
| 204405 | 205120 | return i + nn; |
| 204406 | 205121 | } |
| 204407 | 205122 | pParse->iErr = i; |
| 204408 | 205123 | return -1; /* Syntax error */ |
| 204409 | 205124 | } |
| 204410 | 205125 | } /* End switch(z[i]) */ |
| 204411 | 205126 | } |
| 205127 | + |
| 204412 | 205128 | |
| 204413 | 205129 | /* |
| 204414 | 205130 | ** Parse a complete JSON string. Return 0 on success or non-zero if there |
| 204415 | 205131 | ** are any errors. If an error occurs, free all memory held by pParse, |
| 204416 | 205132 | ** but not pParse itself. |
| 204417 | 205133 | ** |
| 204418 | 205134 | ** pParse must be initialized to an empty parse object prior to calling |
| 204419 | 205135 | ** this routine. |
| 204420 | 205136 | */ |
| 204421 | | -static int jsonParse( |
| 205137 | +static int jsonConvertTextToBlob( |
| 204422 | 205138 | JsonParse *pParse, /* Initialize and fill this JsonParse object */ |
| 204423 | 205139 | sqlite3_context *pCtx /* Report errors here */ |
| 204424 | 205140 | ){ |
| 204425 | 205141 | int i; |
| 204426 | 205142 | const char *zJson = pParse->zJson; |
| 204427 | | - i = jsonParseValue(pParse, 0); |
| 205143 | + i = jsonTranslateTextToBlob(pParse, 0); |
| 204428 | 205144 | if( pParse->oom ) i = -1; |
| 204429 | 205145 | if( i>0 ){ |
| 205146 | +#ifdef SQLITE_DEBUG |
| 204430 | 205147 | assert( pParse->iDepth==0 ); |
| 204431 | | - while( fast_isspace(zJson[i]) ) i++; |
| 205148 | + if( sqlite3Config.bJsonSelfcheck ){ |
| 205149 | + assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 ); |
| 205150 | + } |
| 205151 | +#endif |
| 205152 | + while( jsonIsspace(zJson[i]) ) i++; |
| 204432 | 205153 | if( zJson[i] ){ |
| 204433 | 205154 | i += json5Whitespace(&zJson[i]); |
| 204434 | 205155 | if( zJson[i] ){ |
| 205156 | + if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1); |
| 204435 | 205157 | jsonParseReset(pParse); |
| 204436 | 205158 | return 1; |
| 204437 | 205159 | } |
| 204438 | 205160 | pParse->hasNonstd = 1; |
| 204439 | 205161 | } |
| | @@ -204450,617 +205172,1579 @@ |
| 204450 | 205172 | return 1; |
| 204451 | 205173 | } |
| 204452 | 205174 | return 0; |
| 204453 | 205175 | } |
| 204454 | 205176 | |
| 204455 | | - |
| 204456 | | -/* Mark node i of pParse as being a child of iParent. Call recursively |
| 204457 | | -** to fill in all the descendants of node i. |
| 204458 | | -*/ |
| 204459 | | -static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){ |
| 204460 | | - JsonNode *pNode = &pParse->aNode[i]; |
| 204461 | | - u32 j; |
| 204462 | | - pParse->aUp[i] = iParent; |
| 204463 | | - switch( pNode->eType ){ |
| 204464 | | - case JSON_ARRAY: { |
| 204465 | | - for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){ |
| 204466 | | - jsonParseFillInParentage(pParse, i+j, i); |
| 204467 | | - } |
| 204468 | | - break; |
| 204469 | | - } |
| 204470 | | - case JSON_OBJECT: { |
| 204471 | | - for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){ |
| 204472 | | - pParse->aUp[i+j] = i; |
| 204473 | | - jsonParseFillInParentage(pParse, i+j+1, i); |
| 204474 | | - } |
| 204475 | | - break; |
| 204476 | | - } |
| 204477 | | - default: { |
| 204478 | | - break; |
| 204479 | | - } |
| 204480 | | - } |
| 204481 | | -} |
| 204482 | | - |
| 204483 | | -/* |
| 204484 | | -** Compute the parentage of all nodes in a completed parse. |
| 204485 | | -*/ |
| 204486 | | -static int jsonParseFindParents(JsonParse *pParse){ |
| 204487 | | - u32 *aUp; |
| 204488 | | - assert( pParse->aUp==0 ); |
| 204489 | | - aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode ); |
| 204490 | | - if( aUp==0 ){ |
| 204491 | | - pParse->oom = 1; |
| 204492 | | - return SQLITE_NOMEM; |
| 204493 | | - } |
| 204494 | | - jsonParseFillInParentage(pParse, 0, 0); |
| 204495 | | - return SQLITE_OK; |
| 204496 | | -} |
| 204497 | | - |
| 204498 | | -/* |
| 204499 | | -** Magic number used for the JSON parse cache in sqlite3_get_auxdata() |
| 204500 | | -*/ |
| 204501 | | -#define JSON_CACHE_ID (-429938) /* First cache entry */ |
| 204502 | | -#define JSON_CACHE_SZ 4 /* Max number of cache entries */ |
| 204503 | | - |
| 204504 | | -/* |
| 204505 | | -** Obtain a complete parse of the JSON found in the pJson argument |
| 204506 | | -** |
| 204507 | | -** Use the sqlite3_get_auxdata() cache to find a preexisting parse |
| 204508 | | -** if it is available. If the cache is not available or if it |
| 204509 | | -** is no longer valid, parse the JSON again and return the new parse. |
| 204510 | | -** Also register the new parse so that it will be available for |
| 204511 | | -** future sqlite3_get_auxdata() calls. |
| 204512 | | -** |
| 204513 | | -** If an error occurs and pErrCtx!=0 then report the error on pErrCtx |
| 204514 | | -** and return NULL. |
| 204515 | | -** |
| 204516 | | -** The returned pointer (if it is not NULL) is owned by the cache in |
| 204517 | | -** most cases, not the caller. The caller does NOT need to invoke |
| 204518 | | -** jsonParseFree(), in most cases. |
| 204519 | | -** |
| 204520 | | -** Except, if an error occurs and pErrCtx==0 then return the JsonParse |
| 204521 | | -** object with JsonParse.nErr non-zero and the caller will own the JsonParse |
| 204522 | | -** object. In that case, it will be the responsibility of the caller to |
| 204523 | | -** invoke jsonParseFree(). To summarize: |
| 204524 | | -** |
| 204525 | | -** pErrCtx!=0 || p->nErr==0 ==> Return value p is owned by the |
| 204526 | | -** cache. Call does not need to |
| 204527 | | -** free it. |
| 204528 | | -** |
| 204529 | | -** pErrCtx==0 && p->nErr!=0 ==> Return value is owned by the caller |
| 204530 | | -** and so the caller must free it. |
| 204531 | | -*/ |
| 204532 | | -static JsonParse *jsonParseCached( |
| 204533 | | - sqlite3_context *pCtx, /* Context to use for cache search */ |
| 204534 | | - sqlite3_value *pJson, /* Function param containing JSON text */ |
| 204535 | | - sqlite3_context *pErrCtx, /* Write parse errors here if not NULL */ |
| 204536 | | - int bUnedited /* No prior edits allowed */ |
| 204537 | | -){ |
| 204538 | | - char *zJson = (char*)sqlite3_value_text(pJson); |
| 204539 | | - int nJson = sqlite3_value_bytes(pJson); |
| 204540 | | - JsonParse *p; |
| 204541 | | - JsonParse *pMatch = 0; |
| 204542 | | - int iKey; |
| 204543 | | - int iMinKey = 0; |
| 204544 | | - u32 iMinHold = 0xffffffff; |
| 204545 | | - u32 iMaxHold = 0; |
| 204546 | | - int bJsonRCStr; |
| 204547 | | - |
| 204548 | | - if( zJson==0 ) return 0; |
| 204549 | | - for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){ |
| 204550 | | - p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey); |
| 204551 | | - if( p==0 ){ |
| 204552 | | - iMinKey = iKey; |
| 204553 | | - break; |
| 204554 | | - } |
| 204555 | | - if( pMatch==0 |
| 204556 | | - && p->nJson==nJson |
| 204557 | | - && (p->hasMod==0 || bUnedited==0) |
| 204558 | | - && (p->zJson==zJson || memcmp(p->zJson,zJson,nJson)==0) |
| 204559 | | - ){ |
| 204560 | | - p->nErr = 0; |
| 204561 | | - p->useMod = 0; |
| 204562 | | - pMatch = p; |
| 204563 | | - }else |
| 204564 | | - if( pMatch==0 |
| 204565 | | - && p->zAlt!=0 |
| 204566 | | - && bUnedited==0 |
| 204567 | | - && p->nAlt==nJson |
| 204568 | | - && memcmp(p->zAlt, zJson, nJson)==0 |
| 204569 | | - ){ |
| 204570 | | - p->nErr = 0; |
| 204571 | | - p->useMod = 1; |
| 204572 | | - pMatch = p; |
| 204573 | | - }else if( p->iHold<iMinHold ){ |
| 204574 | | - iMinHold = p->iHold; |
| 204575 | | - iMinKey = iKey; |
| 204576 | | - } |
| 204577 | | - if( p->iHold>iMaxHold ){ |
| 204578 | | - iMaxHold = p->iHold; |
| 204579 | | - } |
| 204580 | | - } |
| 204581 | | - if( pMatch ){ |
| 204582 | | - /* The input JSON text was found in the cache. Use the preexisting |
| 204583 | | - ** parse of this JSON */ |
| 204584 | | - pMatch->nErr = 0; |
| 204585 | | - pMatch->iHold = iMaxHold+1; |
| 204586 | | - assert( pMatch->nJPRef>0 ); /* pMatch is owned by the cache */ |
| 204587 | | - return pMatch; |
| 204588 | | - } |
| 204589 | | - |
| 204590 | | - /* The input JSON was not found anywhere in the cache. We will need |
| 204591 | | - ** to parse it ourselves and generate a new JsonParse object. |
| 204592 | | - */ |
| 204593 | | - bJsonRCStr = sqlite3ValueIsOfClass(pJson,sqlite3RCStrUnref); |
| 204594 | | - p = sqlite3_malloc64( sizeof(*p) + (bJsonRCStr ? 0 : nJson+1) ); |
| 204595 | | - if( p==0 ){ |
| 204596 | | - sqlite3_result_error_nomem(pCtx); |
| 204597 | | - return 0; |
| 204598 | | - } |
| 204599 | | - memset(p, 0, sizeof(*p)); |
| 204600 | | - if( bJsonRCStr ){ |
| 204601 | | - p->zJson = sqlite3RCStrRef(zJson); |
| 204602 | | - p->bJsonIsRCStr = 1; |
| 204603 | | - }else{ |
| 204604 | | - p->zJson = (char*)&p[1]; |
| 204605 | | - memcpy(p->zJson, zJson, nJson+1); |
| 204606 | | - } |
| 204607 | | - p->nJPRef = 1; |
| 204608 | | - if( jsonParse(p, pErrCtx) ){ |
| 204609 | | - if( pErrCtx==0 ){ |
| 204610 | | - p->nErr = 1; |
| 204611 | | - assert( p->nJPRef==1 ); /* Caller will own the new JsonParse object p */ |
| 204612 | | - return p; |
| 204613 | | - } |
| 204614 | | - jsonParseFree(p); |
| 204615 | | - return 0; |
| 204616 | | - } |
| 204617 | | - p->nJson = nJson; |
| 204618 | | - p->iHold = iMaxHold+1; |
| 204619 | | - /* Transfer ownership of the new JsonParse to the cache */ |
| 204620 | | - sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p, |
| 204621 | | - (void(*)(void*))jsonParseFree); |
| 204622 | | - return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey); |
| 204623 | | -} |
| 204624 | | - |
| 204625 | | -/* |
| 204626 | | -** Compare the OBJECT label at pNode against zKey,nKey. Return true on |
| 204627 | | -** a match. |
| 204628 | | -*/ |
| 204629 | | -static int jsonLabelCompare(const JsonNode *pNode, const char *zKey, u32 nKey){ |
| 204630 | | - assert( pNode->eU==1 ); |
| 204631 | | - if( pNode->jnFlags & JNODE_RAW ){ |
| 204632 | | - if( pNode->n!=nKey ) return 0; |
| 204633 | | - return strncmp(pNode->u.zJContent, zKey, nKey)==0; |
| 204634 | | - }else{ |
| 204635 | | - if( pNode->n!=nKey+2 ) return 0; |
| 204636 | | - return strncmp(pNode->u.zJContent+1, zKey, nKey)==0; |
| 204637 | | - } |
| 204638 | | -} |
| 204639 | | -static int jsonSameLabel(const JsonNode *p1, const JsonNode *p2){ |
| 204640 | | - if( p1->jnFlags & JNODE_RAW ){ |
| 204641 | | - return jsonLabelCompare(p2, p1->u.zJContent, p1->n); |
| 204642 | | - }else if( p2->jnFlags & JNODE_RAW ){ |
| 204643 | | - return jsonLabelCompare(p1, p2->u.zJContent, p2->n); |
| 204644 | | - }else{ |
| 204645 | | - return p1->n==p2->n && strncmp(p1->u.zJContent,p2->u.zJContent,p1->n)==0; |
| 204646 | | - } |
| 204647 | | -} |
| 204648 | | - |
| 204649 | | -/* forward declaration */ |
| 204650 | | -static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**); |
| 204651 | | - |
| 204652 | | -/* |
| 204653 | | -** Search along zPath to find the node specified. Return a pointer |
| 204654 | | -** to that node, or NULL if zPath is malformed or if there is no such |
| 204655 | | -** node. |
| 204656 | | -** |
| 204657 | | -** If pApnd!=0, then try to append new nodes to complete zPath if it is |
| 204658 | | -** possible to do so and if no existing node corresponds to zPath. If |
| 204659 | | -** new nodes are appended *pApnd is set to 1. |
| 204660 | | -*/ |
| 204661 | | -static JsonNode *jsonLookupStep( |
| 204662 | | - JsonParse *pParse, /* The JSON to search */ |
| 204663 | | - u32 iRoot, /* Begin the search at this node */ |
| 204664 | | - const char *zPath, /* The path to search */ |
| 204665 | | - int *pApnd, /* Append nodes to complete path if not NULL */ |
| 204666 | | - const char **pzErr /* Make *pzErr point to any syntax error in zPath */ |
| 204667 | | -){ |
| 204668 | | - u32 i, j, nKey; |
| 204669 | | - const char *zKey; |
| 204670 | | - JsonNode *pRoot; |
| 204671 | | - if( pParse->oom ) return 0; |
| 204672 | | - pRoot = &pParse->aNode[iRoot]; |
| 204673 | | - if( pRoot->jnFlags & (JNODE_REPLACE|JNODE_REMOVE) && pParse->useMod ){ |
| 204674 | | - while( (pRoot->jnFlags & JNODE_REPLACE)!=0 ){ |
| 204675 | | - u32 idx = (u32)(pRoot - pParse->aNode); |
| 204676 | | - i = pParse->iSubst; |
| 204677 | | - while( 1 /*exit-by-break*/ ){ |
| 204678 | | - assert( i<pParse->nNode ); |
| 204679 | | - assert( pParse->aNode[i].eType==JSON_SUBST ); |
| 204680 | | - assert( pParse->aNode[i].eU==4 ); |
| 204681 | | - assert( pParse->aNode[i].u.iPrev<i ); |
| 204682 | | - if( pParse->aNode[i].n==idx ){ |
| 204683 | | - pRoot = &pParse->aNode[i+1]; |
| 204684 | | - iRoot = i+1; |
| 204685 | | - break; |
| 204686 | | - } |
| 204687 | | - i = pParse->aNode[i].u.iPrev; |
| 204688 | | - } |
| 204689 | | - } |
| 204690 | | - if( pRoot->jnFlags & JNODE_REMOVE ){ |
| 204691 | | - return 0; |
| 204692 | | - } |
| 204693 | | - } |
| 204694 | | - if( zPath[0]==0 ) return pRoot; |
| 204695 | | - if( zPath[0]=='.' ){ |
| 204696 | | - if( pRoot->eType!=JSON_OBJECT ) return 0; |
| 205177 | +/* |
| 205178 | +** The input string pStr is a well-formed JSON text string. Convert |
| 205179 | +** this into the JSONB format and make it the return value of the |
| 205180 | +** SQL function. |
| 205181 | +*/ |
| 205182 | +static void jsonReturnStringAsBlob(JsonString *pStr){ |
| 205183 | + JsonParse px; |
| 205184 | + memset(&px, 0, sizeof(px)); |
| 205185 | + jsonStringTerminate(pStr); |
| 205186 | + px.zJson = pStr->zBuf; |
| 205187 | + px.nJson = pStr->nUsed; |
| 205188 | + px.db = sqlite3_context_db_handle(pStr->pCtx); |
| 205189 | + (void)jsonTranslateTextToBlob(&px, 0); |
| 205190 | + if( px.oom ){ |
| 205191 | + sqlite3DbFree(px.db, px.aBlob); |
| 205192 | + sqlite3_result_error_nomem(pStr->pCtx); |
| 205193 | + }else{ |
| 205194 | + assert( px.nBlobAlloc>0 ); |
| 205195 | + assert( !px.bReadOnly ); |
| 205196 | + sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC); |
| 205197 | + } |
| 205198 | +} |
| 205199 | + |
| 205200 | +/* The byte at index i is a node type-code. This routine |
| 205201 | +** determines the payload size for that node and writes that |
| 205202 | +** payload size in to *pSz. It returns the offset from i to the |
| 205203 | +** beginning of the payload. Return 0 on error. |
| 205204 | +*/ |
| |