| | @@ -381,15 +381,15 @@ |
| 381 | 381 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 382 | 382 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 383 | 383 | */ |
| 384 | 384 | #define SQLITE_VERSION "3.16.0" |
| 385 | 385 | #define SQLITE_VERSION_NUMBER 3016000 |
| 386 | | -#define SQLITE_SOURCE_ID "2016-11-02 14:50:19 3028845329c9b7acdec2ec8b01d00d782347454c" |
| 386 | +#define SQLITE_SOURCE_ID "2016-11-22 20:29:05 bee2859b953c935c413de2917588159d03c672d9" |
| 387 | 387 | |
| 388 | 388 | /* |
| 389 | 389 | ** CAPI3REF: Run-Time Library Version Numbers |
| 390 | | -** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 390 | +** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 391 | 391 | ** |
| 392 | 392 | ** These interfaces provide the same information as the [SQLITE_VERSION], |
| 393 | 393 | ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros |
| 394 | 394 | ** but are associated with the library instead of the header file. ^(Cautious |
| 395 | 395 | ** programmers might include assert() statements in their application to |
| | @@ -8483,11 +8483,12 @@ |
| 8483 | 8483 | ** triggers; or 2 for changes resulting from triggers called by top-level |
| 8484 | 8484 | ** triggers; and so forth. |
| 8485 | 8485 | ** |
| 8486 | 8486 | ** See also: [sqlite3_update_hook()] |
| 8487 | 8487 | */ |
| 8488 | | -SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_preupdate_hook( |
| 8488 | +#if defined(SQLITE_ENABLE_PREUPDATE_HOOK) |
| 8489 | +SQLITE_API void *sqlite3_preupdate_hook( |
| 8489 | 8490 | sqlite3 *db, |
| 8490 | 8491 | void(*xPreUpdate)( |
| 8491 | 8492 | void *pCtx, /* Copy of third arg to preupdate_hook() */ |
| 8492 | 8493 | sqlite3 *db, /* Database handle */ |
| 8493 | 8494 | int op, /* SQLITE_UPDATE, DELETE or INSERT */ |
| | @@ -8496,14 +8497,15 @@ |
| 8496 | 8497 | sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ |
| 8497 | 8498 | sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ |
| 8498 | 8499 | ), |
| 8499 | 8500 | void* |
| 8500 | 8501 | ); |
| 8501 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); |
| 8502 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_preupdate_count(sqlite3 *); |
| 8503 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_preupdate_depth(sqlite3 *); |
| 8504 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); |
| 8502 | +SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); |
| 8503 | +SQLITE_API int sqlite3_preupdate_count(sqlite3 *); |
| 8504 | +SQLITE_API int sqlite3_preupdate_depth(sqlite3 *); |
| 8505 | +SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); |
| 8506 | +#endif |
| 8505 | 8507 | |
| 8506 | 8508 | /* |
| 8507 | 8509 | ** CAPI3REF: Low-level system error code |
| 8508 | 8510 | ** |
| 8509 | 8511 | ** ^Attempt to return the underlying operating system error code or error |
| | @@ -12307,10 +12309,12 @@ |
| 12307 | 12309 | */ |
| 12308 | 12310 | struct BtreePayload { |
| 12309 | 12311 | const void *pKey; /* Key content for indexes. NULL for tables */ |
| 12310 | 12312 | sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */ |
| 12311 | 12313 | const void *pData; /* Data for tables. NULL for indexes */ |
| 12314 | + struct Mem *aMem; /* First of nMem value in the unpacked pKey */ |
| 12315 | + u16 nMem; /* Number of aMem[] value. Might be zero */ |
| 12312 | 12316 | int nData; /* Size of pData. 0 if none. */ |
| 12313 | 12317 | int nZero; /* Extra zero data appended after pData,nData */ |
| 12314 | 12318 | }; |
| 12315 | 12319 | |
| 12316 | 12320 | SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload, |
| | @@ -12340,10 +12344,11 @@ |
| 12340 | 12344 | SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); |
| 12341 | 12345 | |
| 12342 | 12346 | #ifndef NDEBUG |
| 12343 | 12347 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 12344 | 12348 | #endif |
| 12349 | +SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*); |
| 12345 | 12350 | |
| 12346 | 12351 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 12347 | 12352 | SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *); |
| 12348 | 12353 | #endif |
| 12349 | 12354 | |
| | @@ -15596,19 +15601,19 @@ |
| 15596 | 15601 | int iReg; /* Reg with value of this column. 0 means none. */ |
| 15597 | 15602 | int lru; /* Least recently used entry has the smallest value */ |
| 15598 | 15603 | } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */ |
| 15599 | 15604 | int aTempReg[8]; /* Holding area for temporary registers */ |
| 15600 | 15605 | Token sNameToken; /* Token with unqualified schema object name */ |
| 15601 | | - Token sLastToken; /* The last token parsed */ |
| 15602 | 15606 | |
| 15603 | 15607 | /************************************************************************ |
| 15604 | 15608 | ** Above is constant between recursions. Below is reset before and after |
| 15605 | 15609 | ** each recursion. The boundary between these two regions is determined |
| 15606 | | - ** using offsetof(Parse,nVar) so the nVar field must be the first field |
| 15607 | | - ** in the recursive region. |
| 15610 | + ** using offsetof(Parse,sLastToken) so the sLastToken field must be the |
| 15611 | + ** first field in the recursive region. |
| 15608 | 15612 | ************************************************************************/ |
| 15609 | 15613 | |
| 15614 | + Token sLastToken; /* The last token parsed */ |
| 15610 | 15615 | ynVar nVar; /* Number of '?' variables seen in the SQL so far */ |
| 15611 | 15616 | int nzVar; /* Number of available slots in azVar[] */ |
| 15612 | 15617 | u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */ |
| 15613 | 15618 | u8 explain; /* True if the EXPLAIN flag is found on the query */ |
| 15614 | 15619 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -15638,11 +15643,11 @@ |
| 15638 | 15643 | |
| 15639 | 15644 | /* |
| 15640 | 15645 | ** Sizes and pointers of various parts of the Parse object. |
| 15641 | 15646 | */ |
| 15642 | 15647 | #define PARSE_HDR_SZ offsetof(Parse,aColCache) /* Recursive part w/o aColCache*/ |
| 15643 | | -#define PARSE_RECURSE_SZ offsetof(Parse,nVar) /* Recursive part */ |
| 15648 | +#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */ |
| 15644 | 15649 | #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */ |
| 15645 | 15650 | #define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */ |
| 15646 | 15651 | |
| 15647 | 15652 | /* |
| 15648 | 15653 | ** Return true if currently inside an sqlite3_declare_vtab() call. |
| | @@ -16355,10 +16360,11 @@ |
| 16355 | 16360 | SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int); |
| 16356 | 16361 | SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8); |
| 16357 | 16362 | #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ |
| 16358 | 16363 | #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */ |
| 16359 | 16364 | #define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */ |
| 16365 | +#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */ |
| 16360 | 16366 | SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int); |
| 16361 | 16367 | SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int); |
| 16362 | 16368 | SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); |
| 16363 | 16369 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); |
| 16364 | 16370 | #define LOCATE_VIEW 0x01 |
| | @@ -17802,11 +17808,14 @@ |
| 17802 | 17808 | int pseudoTableReg; /* CURTYPE_PSEUDO. Reg holding content. */ |
| 17803 | 17809 | VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */ |
| 17804 | 17810 | } uc; |
| 17805 | 17811 | Btree *pBt; /* Separate file holding temporary table */ |
| 17806 | 17812 | KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ |
| 17807 | | - int seekResult; /* Result of previous sqlite3BtreeMoveto() */ |
| 17813 | + int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0 |
| 17814 | + ** if there have been no prior seeks on the cursor. */ |
| 17815 | + /* NB: seekResult does not distinguish between "no seeks have ever occurred |
| 17816 | + ** on this cursor" and "the most recent seek was an exact match". */ |
| 17808 | 17817 | i64 seqCount; /* Sequence counter */ |
| 17809 | 17818 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 17810 | 17819 | VdbeCursor *pAltCursor; /* Associated index cursor from which to read */ |
| 17811 | 17820 | int *aAltMap; /* Mapping from table to index column numbers */ |
| 17812 | 17821 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| | @@ -25889,10 +25898,11 @@ |
| 25889 | 25898 | sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
| 25890 | 25899 | } |
| 25891 | 25900 | va_start(ap, zFormat); |
| 25892 | 25901 | sqlite3VXPrintf(&acc, zFormat, ap); |
| 25893 | 25902 | va_end(ap); |
| 25903 | + assert( acc.nChar>0 ); |
| 25894 | 25904 | if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); |
| 25895 | 25905 | sqlite3StrAccumFinish(&acc); |
| 25896 | 25906 | fprintf(stdout,"%s", zBuf); |
| 25897 | 25907 | fflush(stdout); |
| 25898 | 25908 | } |
| | @@ -50417,11 +50427,14 @@ |
| 50417 | 50427 | sqlite3BeginBenignMalloc(); |
| 50418 | 50428 | pagerFreeMapHdrs(pPager); |
| 50419 | 50429 | /* pPager->errCode = 0; */ |
| 50420 | 50430 | pPager->exclusiveMode = 0; |
| 50421 | 50431 | #ifndef SQLITE_OMIT_WAL |
| 50422 | | - sqlite3WalClose(pPager->pWal,db,pPager->ckptSyncFlags,pPager->pageSize,pTmp); |
| 50432 | + assert( db || pPager->pWal==0 ); |
| 50433 | + sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags, pPager->pageSize, |
| 50434 | + (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp) |
| 50435 | + ); |
| 50423 | 50436 | pPager->pWal = 0; |
| 50424 | 50437 | #endif |
| 50425 | 50438 | pager_reset(pPager); |
| 50426 | 50439 | if( MEMDB ){ |
| 50427 | 50440 | pager_unlock(pPager); |
| | @@ -55752,11 +55765,11 @@ |
| 55752 | 55765 | ** the database. In this case checkpoint the database and unlink both |
| 55753 | 55766 | ** the wal and wal-index files. |
| 55754 | 55767 | ** |
| 55755 | 55768 | ** The EXCLUSIVE lock is not released before returning. |
| 55756 | 55769 | */ |
| 55757 | | - if( (db->flags & SQLITE_NoCkptOnClose)==0 |
| 55770 | + if( zBuf!=0 |
| 55758 | 55771 | && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE)) |
| 55759 | 55772 | ){ |
| 55760 | 55773 | if( pWal->exclusiveMode==WAL_NORMAL_MODE ){ |
| 55761 | 55774 | pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; |
| 55762 | 55775 | } |
| | @@ -62555,10 +62568,14 @@ |
| 62555 | 62568 | */ |
| 62556 | 62569 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){ |
| 62557 | 62570 | return pCur && pCur->eState==CURSOR_VALID; |
| 62558 | 62571 | } |
| 62559 | 62572 | #endif /* NDEBUG */ |
| 62573 | +SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){ |
| 62574 | + assert( pCur!=0 ); |
| 62575 | + return pCur->eState==CURSOR_VALID; |
| 62576 | +} |
| 62560 | 62577 | |
| 62561 | 62578 | /* |
| 62562 | 62579 | ** Return the value of the integer key or "rowid" for a table btree. |
| 62563 | 62580 | ** This routine is only valid for a cursor that is pointing into a |
| 62564 | 62581 | ** ordinary table btree. If the cursor points to an index btree or |
| | @@ -63442,20 +63459,20 @@ |
| 63442 | 63459 | }else if( nCellKey>intKey ){ |
| 63443 | 63460 | upr = idx-1; |
| 63444 | 63461 | if( lwr>upr ){ c = +1; break; } |
| 63445 | 63462 | }else{ |
| 63446 | 63463 | assert( nCellKey==intKey ); |
| 63447 | | - pCur->curFlags |= BTCF_ValidNKey; |
| 63448 | | - pCur->info.nKey = nCellKey; |
| 63449 | 63464 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| 63450 | 63465 | if( !pPage->leaf ){ |
| 63451 | 63466 | lwr = idx; |
| 63452 | 63467 | goto moveto_next_layer; |
| 63453 | 63468 | }else{ |
| 63469 | + pCur->curFlags |= BTCF_ValidNKey; |
| 63470 | + pCur->info.nKey = nCellKey; |
| 63471 | + pCur->info.nSize = 0; |
| 63454 | 63472 | *pRes = 0; |
| 63455 | | - rc = SQLITE_OK; |
| 63456 | | - goto moveto_finish; |
| 63473 | + return SQLITE_OK; |
| 63457 | 63474 | } |
| 63458 | 63475 | } |
| 63459 | 63476 | assert( lwr+upr>=0 ); |
| 63460 | 63477 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ |
| 63461 | 63478 | } |
| | @@ -63562,11 +63579,11 @@ |
| 63562 | 63579 | rc = moveToChild(pCur, chldPg); |
| 63563 | 63580 | if( rc ) break; |
| 63564 | 63581 | } |
| 63565 | 63582 | moveto_finish: |
| 63566 | 63583 | pCur->info.nSize = 0; |
| 63567 | | - pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 63584 | + assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); |
| 63568 | 63585 | return rc; |
| 63569 | 63586 | } |
| 63570 | 63587 | |
| 63571 | 63588 | |
| 63572 | 63589 | /* |
| | @@ -63760,11 +63777,11 @@ |
| 63760 | 63777 | return SQLITE_OK; |
| 63761 | 63778 | } |
| 63762 | 63779 | moveToParent(pCur); |
| 63763 | 63780 | } |
| 63764 | 63781 | assert( pCur->info.nSize==0 ); |
| 63765 | | - assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 ); |
| 63782 | + assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 ); |
| 63766 | 63783 | |
| 63767 | 63784 | pCur->aiIdx[pCur->iPage]--; |
| 63768 | 63785 | pPage = pCur->apPage[pCur->iPage]; |
| 63769 | 63786 | if( pPage->intKey && !pPage->leaf ){ |
| 63770 | 63787 | rc = sqlite3BtreePrevious(pCur, pRes); |
| | @@ -66217,21 +66234,23 @@ |
| 66217 | 66234 | ** For an index btree (used for indexes and WITHOUT ROWID tables), the |
| 66218 | 66235 | ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The |
| 66219 | 66236 | ** pX.pData,nData,nZero fields must be zero. |
| 66220 | 66237 | ** |
| 66221 | 66238 | ** If the seekResult parameter is non-zero, then a successful call to |
| 66222 | | -** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already |
| 66223 | | -** been performed. seekResult is the search result returned (a negative |
| 66224 | | -** number if pCur points at an entry that is smaller than (pKey, nKey), or |
| 66225 | | -** a positive value if pCur points at an entry that is larger than |
| 66226 | | -** (pKey, nKey)). |
| 66239 | +** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already |
| 66240 | +** been performed. In other words, if seekResult!=0 then the cursor |
| 66241 | +** is currently pointing to a cell that will be adjacent to the cell |
| 66242 | +** to be inserted. If seekResult<0 then pCur points to a cell that is |
| 66243 | +** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell |
| 66244 | +** that is larger than (pKey,nKey). |
| 66227 | 66245 | ** |
| 66228 | | -** If the seekResult parameter is non-zero, then the caller guarantees that |
| 66229 | | -** cursor pCur is pointing at the existing copy of a row that is to be |
| 66230 | | -** overwritten. If the seekResult parameter is 0, then cursor pCur may |
| 66231 | | -** point to any entry or to no entry at all and so this function has to seek |
| 66232 | | -** the cursor before the new key can be inserted. |
| 66246 | +** If seekResult==0, that means pCur is pointing at some unknown location. |
| 66247 | +** In that case, this routine must seek the cursor to the correct insertion |
| 66248 | +** point for (pKey,nKey) before doing the insertion. For index btrees, |
| 66249 | +** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked |
| 66250 | +** key values and pX->aMem can be used instead of pX->pKey to avoid having |
| 66251 | +** to decode the key. |
| 66233 | 66252 | */ |
| 66234 | 66253 | SQLITE_PRIVATE int sqlite3BtreeInsert( |
| 66235 | 66254 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
| 66236 | 66255 | const BtreePayload *pX, /* Content of the row to be inserted */ |
| 66237 | 66256 | int appendBias, /* True if this is likely an append */ |
| | @@ -66288,19 +66307,30 @@ |
| 66288 | 66307 | invalidateIncrblobCursors(p, pX->nKey, 0); |
| 66289 | 66308 | |
| 66290 | 66309 | /* If the cursor is currently on the last row and we are appending a |
| 66291 | 66310 | ** new row onto the end, set the "loc" to avoid an unnecessary |
| 66292 | 66311 | ** btreeMoveto() call */ |
| 66293 | | - if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0 |
| 66294 | | - && pCur->info.nKey==pX->nKey-1 ){ |
| 66295 | | - loc = -1; |
| 66312 | + if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){ |
| 66313 | + loc = 0; |
| 66314 | + }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0 |
| 66315 | + && pCur->info.nKey==pX->nKey-1 ){ |
| 66316 | + loc = -1; |
| 66296 | 66317 | }else if( loc==0 ){ |
| 66297 | 66318 | rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc); |
| 66298 | 66319 | if( rc ) return rc; |
| 66299 | 66320 | } |
| 66300 | 66321 | }else if( loc==0 ){ |
| 66301 | | - rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc); |
| 66322 | + if( pX->nMem ){ |
| 66323 | + UnpackedRecord r; |
| 66324 | + memset(&r, 0, sizeof(r)); |
| 66325 | + r.pKeyInfo = pCur->pKeyInfo; |
| 66326 | + r.aMem = pX->aMem; |
| 66327 | + r.nField = pX->nMem; |
| 66328 | + rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc); |
| 66329 | + }else{ |
| 66330 | + rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc); |
| 66331 | + } |
| 66302 | 66332 | if( rc ) return rc; |
| 66303 | 66333 | } |
| 66304 | 66334 | assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); |
| 66305 | 66335 | |
| 66306 | 66336 | pPage = pCur->apPage[pCur->iPage]; |
| | @@ -66838,31 +66868,11 @@ |
| 66838 | 66868 | MemPage *pPage = 0; |
| 66839 | 66869 | BtShared *pBt = p->pBt; |
| 66840 | 66870 | |
| 66841 | 66871 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 66842 | 66872 | assert( p->inTrans==TRANS_WRITE ); |
| 66843 | | - |
| 66844 | | - /* It is illegal to drop a table if any cursors are open on the |
| 66845 | | - ** database. This is because in auto-vacuum mode the backend may |
| 66846 | | - ** need to move another root-page to fill a gap left by the deleted |
| 66847 | | - ** root page. If an open cursor was using this page a problem would |
| 66848 | | - ** occur. |
| 66849 | | - ** |
| 66850 | | - ** This error is caught long before control reaches this point. |
| 66851 | | - */ |
| 66852 | | - if( NEVER(pBt->pCursor) ){ |
| 66853 | | - sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); |
| 66854 | | - return SQLITE_LOCKED_SHAREDCACHE; |
| 66855 | | - } |
| 66856 | | - |
| 66857 | | - /* |
| 66858 | | - ** It is illegal to drop the sqlite_master table on page 1. But again, |
| 66859 | | - ** this error is caught long before reaching this point. |
| 66860 | | - */ |
| 66861 | | - if( NEVER(iTable<2) ){ |
| 66862 | | - return SQLITE_CORRUPT_BKPT; |
| 66863 | | - } |
| 66873 | + assert( iTable>=2 ); |
| 66864 | 66874 | |
| 66865 | 66875 | rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
| 66866 | 66876 | if( rc ) return rc; |
| 66867 | 66877 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
| 66868 | 66878 | if( rc ){ |
| | @@ -81675,19 +81685,14 @@ |
| 81675 | 81685 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 81676 | 81686 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
| 81677 | 81687 | ** then rowid is stored for subsequent return by the |
| 81678 | 81688 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
| 81679 | 81689 | ** |
| 81680 | | -** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of |
| 81681 | | -** the last seek operation (OP_NotExists or OP_SeekRowid) was a success, |
| 81682 | | -** then this |
| 81683 | | -** operation will not attempt to find the appropriate row before doing |
| 81684 | | -** the insert but will instead overwrite the row that the cursor is |
| 81685 | | -** currently pointing to. Presumably, the prior OP_NotExists or |
| 81686 | | -** OP_SeekRowid opcode |
| 81687 | | -** has already positioned the cursor correctly. This is an optimization |
| 81688 | | -** that boosts performance by avoiding redundant seeks. |
| 81690 | +** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 81691 | +** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 81692 | +** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 81693 | +** seeks on the cursor or if the most recent seek used a key equal to P3. |
| 81689 | 81694 | ** |
| 81690 | 81695 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 81691 | 81696 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 81692 | 81697 | ** is part of an INSERT operation. The difference is only important to |
| 81693 | 81698 | ** the update hook. |
| | @@ -81907,10 +81912,11 @@ |
| 81907 | 81912 | } |
| 81908 | 81913 | #endif |
| 81909 | 81914 | |
| 81910 | 81915 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
| 81911 | 81916 | pC->cacheStatus = CACHE_STALE; |
| 81917 | + pC->seekResult = 0; |
| 81912 | 81918 | if( rc ) goto abort_due_to_error; |
| 81913 | 81919 | |
| 81914 | 81920 | /* Invoke the update-hook if required. */ |
| 81915 | 81921 | if( opflags & OPFLAG_NCHANGE ){ |
| 81916 | 81922 | p->nChange++; |
| | @@ -82156,10 +82162,17 @@ |
| 82156 | 82162 | ** to the following instruction. |
| 82157 | 82163 | ** |
| 82158 | 82164 | ** This opcode leaves the cursor configured to move in reverse order, |
| 82159 | 82165 | ** from the end toward the beginning. In other words, the cursor is |
| 82160 | 82166 | ** configured to use Prev, not Next. |
| 82167 | +** |
| 82168 | +** If P3 is -1, then the cursor is positioned at the end of the btree |
| 82169 | +** for the purpose of appending a new entry onto the btree. In that |
| 82170 | +** case P2 must be 0. It is assumed that the cursor is used only for |
| 82171 | +** appending and so if the cursor is valid, then the cursor must already |
| 82172 | +** be pointing at the end of the btree and so no changes are made to |
| 82173 | +** the cursor. |
| 82161 | 82174 | */ |
| 82162 | 82175 | case OP_Last: { /* jump */ |
| 82163 | 82176 | VdbeCursor *pC; |
| 82164 | 82177 | BtCursor *pCrsr; |
| 82165 | 82178 | int res; |
| | @@ -82169,22 +82182,26 @@ |
| 82169 | 82182 | assert( pC!=0 ); |
| 82170 | 82183 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 82171 | 82184 | pCrsr = pC->uc.pCursor; |
| 82172 | 82185 | res = 0; |
| 82173 | 82186 | assert( pCrsr!=0 ); |
| 82174 | | - rc = sqlite3BtreeLast(pCrsr, &res); |
| 82175 | | - pC->nullRow = (u8)res; |
| 82176 | | - pC->deferredMoveto = 0; |
| 82177 | | - pC->cacheStatus = CACHE_STALE; |
| 82178 | 82187 | pC->seekResult = pOp->p3; |
| 82179 | 82188 | #ifdef SQLITE_DEBUG |
| 82180 | 82189 | pC->seekOp = OP_Last; |
| 82181 | 82190 | #endif |
| 82182 | | - if( rc ) goto abort_due_to_error; |
| 82183 | | - if( pOp->p2>0 ){ |
| 82184 | | - VdbeBranchTaken(res!=0,2); |
| 82185 | | - if( res ) goto jump_to_p2; |
| 82191 | + if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
| 82192 | + rc = sqlite3BtreeLast(pCrsr, &res); |
| 82193 | + pC->nullRow = (u8)res; |
| 82194 | + pC->deferredMoveto = 0; |
| 82195 | + pC->cacheStatus = CACHE_STALE; |
| 82196 | + if( rc ) goto abort_due_to_error; |
| 82197 | + if( pOp->p2>0 ){ |
| 82198 | + VdbeBranchTaken(res!=0,2); |
| 82199 | + if( res ) goto jump_to_p2; |
| 82200 | + } |
| 82201 | + }else{ |
| 82202 | + assert( pOp->p2==0 ); |
| 82186 | 82203 | } |
| 82187 | 82204 | break; |
| 82188 | 82205 | } |
| 82189 | 82206 | |
| 82190 | 82207 | |
| | @@ -82369,27 +82386,34 @@ |
| 82369 | 82386 | pC->nullRow = 1; |
| 82370 | 82387 | } |
| 82371 | 82388 | goto check_for_interrupt; |
| 82372 | 82389 | } |
| 82373 | 82390 | |
| 82374 | | -/* Opcode: IdxInsert P1 P2 P3 * P5 |
| 82391 | +/* Opcode: IdxInsert P1 P2 P3 P4 P5 |
| 82375 | 82392 | ** Synopsis: key=r[P2] |
| 82376 | 82393 | ** |
| 82377 | 82394 | ** Register P2 holds an SQL index key made using the |
| 82378 | 82395 | ** MakeRecord instructions. This opcode writes that key |
| 82379 | 82396 | ** into the index P1. Data for the entry is nil. |
| 82380 | 82397 | ** |
| 82381 | | -** P3 is a flag that provides a hint to the b-tree layer that this |
| 82382 | | -** insert is likely to be an append. |
| 82398 | +** If P4 is not zero, then it is the number of values in the unpacked |
| 82399 | +** key of reg(P2). In that case, P3 is the index of the first register |
| 82400 | +** for the unpacked key. The availability of the unpacked key can sometimes |
| 82401 | +** be an optimization. |
| 82402 | +** |
| 82403 | +** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
| 82404 | +** that this insert is likely to be an append. |
| 82383 | 82405 | ** |
| 82384 | 82406 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 82385 | 82407 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 82386 | 82408 | ** then the change counter is unchanged. |
| 82387 | 82409 | ** |
| 82388 | | -** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have |
| 82389 | | -** just done a seek to the spot where the new entry is to be inserted. |
| 82390 | | -** This flag avoids doing an extra seek. |
| 82410 | +** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 82411 | +** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 82412 | +** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 82413 | +** seeks on the cursor or if the most recent seek used a key equivalent |
| 82414 | +** to P2. |
| 82391 | 82415 | ** |
| 82392 | 82416 | ** This instruction only works for indices. The equivalent instruction |
| 82393 | 82417 | ** for tables is OP_Insert. |
| 82394 | 82418 | */ |
| 82395 | 82419 | /* Opcode: SorterInsert P1 P2 * * * |
| | @@ -82418,11 +82442,14 @@ |
| 82418 | 82442 | if( pOp->opcode==OP_SorterInsert ){ |
| 82419 | 82443 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 82420 | 82444 | }else{ |
| 82421 | 82445 | x.nKey = pIn2->n; |
| 82422 | 82446 | x.pKey = pIn2->z; |
| 82423 | | - rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, pOp->p3, |
| 82447 | + x.aMem = aMem + pOp->p3; |
| 82448 | + x.nMem = (u16)pOp->p4.i; |
| 82449 | + rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
| 82450 | + (pOp->p5 & OPFLAG_APPEND)!=0, |
| 82424 | 82451 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 82425 | 82452 | ); |
| 82426 | 82453 | assert( pC->deferredMoveto==0 ); |
| 82427 | 82454 | pC->cacheStatus = CACHE_STALE; |
| 82428 | 82455 | } |
| | @@ -82462,10 +82489,11 @@ |
| 82462 | 82489 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
| 82463 | 82490 | if( rc ) goto abort_due_to_error; |
| 82464 | 82491 | } |
| 82465 | 82492 | assert( pC->deferredMoveto==0 ); |
| 82466 | 82493 | pC->cacheStatus = CACHE_STALE; |
| 82494 | + pC->seekResult = 0; |
| 82467 | 82495 | break; |
| 82468 | 82496 | } |
| 82469 | 82497 | |
| 82470 | 82498 | /* Opcode: Seek P1 * P3 P4 * |
| 82471 | 82499 | ** Synopsis: Move P3 to P1.rowid |
| | @@ -84648,12 +84676,11 @@ |
| 84648 | 84676 | {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */ |
| 84649 | 84677 | {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */ |
| 84650 | 84678 | {OP_Column, 0, 0, 1}, /* 4 */ |
| 84651 | 84679 | {OP_ResultRow, 1, 0, 0}, /* 5 */ |
| 84652 | 84680 | {OP_Goto, 0, 2, 0}, /* 6 */ |
| 84653 | | - {OP_Close, 0, 0, 0}, /* 7 */ |
| 84654 | | - {OP_Halt, 0, 0, 0}, /* 8 */ |
| 84681 | + {OP_Halt, 0, 0, 0}, /* 7 */ |
| 84655 | 84682 | }; |
| 84656 | 84683 | Vdbe *v = (Vdbe *)pBlob->pStmt; |
| 84657 | 84684 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 84658 | 84685 | VdbeOp *aOp; |
| 84659 | 84686 | |
| | @@ -88636,10 +88663,14 @@ |
| 88636 | 88663 | assert( pExpr->x.pSelect==0 ); |
| 88637 | 88664 | pOrig = pEList->a[j].pExpr; |
| 88638 | 88665 | if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){ |
| 88639 | 88666 | sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); |
| 88640 | 88667 | return WRC_Abort; |
| 88668 | + } |
| 88669 | + if( sqlite3ExprVectorSize(pOrig)!=1 ){ |
| 88670 | + sqlite3ErrorMsg(pParse, "row value misused"); |
| 88671 | + return WRC_Abort; |
| 88641 | 88672 | } |
| 88642 | 88673 | resolveAlias(pParse, pEList, j, pExpr, "", nSubquery); |
| 88643 | 88674 | cnt = 1; |
| 88644 | 88675 | pMatch = 0; |
| 88645 | 88676 | assert( zTab==0 && zDb==0 ); |
| | @@ -89013,10 +89044,11 @@ |
| 89013 | 89044 | } |
| 89014 | 89045 | case TK_VARIABLE: { |
| 89015 | 89046 | notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr); |
| 89016 | 89047 | break; |
| 89017 | 89048 | } |
| 89049 | + case TK_BETWEEN: |
| 89018 | 89050 | case TK_EQ: |
| 89019 | 89051 | case TK_NE: |
| 89020 | 89052 | case TK_LT: |
| 89021 | 89053 | case TK_LE: |
| 89022 | 89054 | case TK_GT: |
| | @@ -89023,23 +89055,31 @@ |
| 89023 | 89055 | case TK_GE: |
| 89024 | 89056 | case TK_IS: |
| 89025 | 89057 | case TK_ISNOT: { |
| 89026 | 89058 | int nLeft, nRight; |
| 89027 | 89059 | if( pParse->db->mallocFailed ) break; |
| 89028 | | - assert( pExpr->pRight!=0 ); |
| 89029 | 89060 | assert( pExpr->pLeft!=0 ); |
| 89030 | 89061 | nLeft = sqlite3ExprVectorSize(pExpr->pLeft); |
| 89031 | | - nRight = sqlite3ExprVectorSize(pExpr->pRight); |
| 89062 | + if( pExpr->op==TK_BETWEEN ){ |
| 89063 | + nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr); |
| 89064 | + if( nRight==nLeft ){ |
| 89065 | + nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr); |
| 89066 | + } |
| 89067 | + }else{ |
| 89068 | + assert( pExpr->pRight!=0 ); |
| 89069 | + nRight = sqlite3ExprVectorSize(pExpr->pRight); |
| 89070 | + } |
| 89032 | 89071 | if( nLeft!=nRight ){ |
| 89033 | 89072 | testcase( pExpr->op==TK_EQ ); |
| 89034 | 89073 | testcase( pExpr->op==TK_NE ); |
| 89035 | 89074 | testcase( pExpr->op==TK_LT ); |
| 89036 | 89075 | testcase( pExpr->op==TK_LE ); |
| 89037 | 89076 | testcase( pExpr->op==TK_GT ); |
| 89038 | 89077 | testcase( pExpr->op==TK_GE ); |
| 89039 | 89078 | testcase( pExpr->op==TK_IS ); |
| 89040 | 89079 | testcase( pExpr->op==TK_ISNOT ); |
| 89080 | + testcase( pExpr->op==TK_BETWEEN ); |
| 89041 | 89081 | sqlite3ErrorMsg(pParse, "row value misused"); |
| 89042 | 89082 | } |
| 89043 | 89083 | break; |
| 89044 | 89084 | } |
| 89045 | 89085 | } |
| | @@ -92306,11 +92346,11 @@ |
| 92306 | 92346 | VdbeCoverage(v); |
| 92307 | 92347 | sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); |
| 92308 | 92348 | }else{ |
| 92309 | 92349 | sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); |
| 92310 | 92350 | sqlite3ExprCacheAffinityChange(pParse, r3, 1); |
| 92311 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); |
| 92351 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1); |
| 92312 | 92352 | } |
| 92313 | 92353 | } |
| 92314 | 92354 | } |
| 92315 | 92355 | sqlite3ReleaseTempReg(pParse, r1); |
| 92316 | 92356 | sqlite3ReleaseTempReg(pParse, r2); |
| | @@ -93854,12 +93894,17 @@ |
| 93854 | 93894 | assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ |
| 93855 | 93895 | n = pList->nExpr; |
| 93856 | 93896 | if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; |
| 93857 | 93897 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
| 93858 | 93898 | Expr *pExpr = pItem->pExpr; |
| 93859 | | - if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ |
| 93860 | | - sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); |
| 93899 | + if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){ |
| 93900 | + if( flags & SQLITE_ECEL_OMITREF ){ |
| 93901 | + i--; |
| 93902 | + n--; |
| 93903 | + }else{ |
| 93904 | + sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); |
| 93905 | + } |
| 93861 | 93906 | }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ |
| 93862 | 93907 | sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); |
| 93863 | 93908 | }else{ |
| 93864 | 93909 | int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
| 93865 | 93910 | if( inReg!=target+i ){ |
| | @@ -97815,10 +97860,11 @@ |
| 97815 | 97860 | NameContext sName; |
| 97816 | 97861 | Vdbe *v; |
| 97817 | 97862 | sqlite3* db = pParse->db; |
| 97818 | 97863 | int regArgs; |
| 97819 | 97864 | |
| 97865 | + if( pParse->nErr ) goto attach_end; |
| 97820 | 97866 | memset(&sName, 0, sizeof(NameContext)); |
| 97821 | 97867 | sName.pParse = pParse; |
| 97822 | 97868 | |
| 97823 | 97869 | if( |
| 97824 | 97870 | SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || |
| | @@ -98397,10 +98443,12 @@ |
| 98397 | 98443 | int i; |
| 98398 | 98444 | int nBytes; |
| 98399 | 98445 | TableLock *p; |
| 98400 | 98446 | assert( iDb>=0 ); |
| 98401 | 98447 | |
| 98448 | + if( iDb==1 ) return; |
| 98449 | + if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; |
| 98402 | 98450 | for(i=0; i<pToplevel->nTableLock; i++){ |
| 98403 | 98451 | p = &pToplevel->aTableLock[i]; |
| 98404 | 98452 | if( p->iDb==iDb && p->iTab==iTab ){ |
| 98405 | 98453 | p->isWriteLock = (p->isWriteLock || isWriteLock); |
| 98406 | 98454 | return; |
| | @@ -101154,11 +101202,11 @@ |
| 101154 | 101202 | }else{ |
| 101155 | 101203 | addr2 = sqlite3VdbeCurrentAddr(v); |
| 101156 | 101204 | } |
| 101157 | 101205 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
| 101158 | 101206 | sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); |
| 101159 | | - sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); |
| 101207 | + sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
| 101160 | 101208 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 101161 | 101209 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 101162 | 101210 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
| 101163 | 101211 | sqlite3VdbeJumpHere(v, addr1); |
| 101164 | 101212 | |
| | @@ -103702,11 +103750,11 @@ |
| 103702 | 103750 | /* Add the PK key for this row to the temporary table */ |
| 103703 | 103751 | iKey = ++pParse->nMem; |
| 103704 | 103752 | nKey = 0; /* Zero tells OP_Found to use a composite key */ |
| 103705 | 103753 | sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey, |
| 103706 | 103754 | sqlite3IndexAffinityStr(pParse->db, pPk), nPk); |
| 103707 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey); |
| 103755 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk); |
| 103708 | 103756 | }else{ |
| 103709 | 103757 | /* Add the rowid of the row to be deleted to the RowSet */ |
| 103710 | 103758 | nKey = 1; /* OP_Seek always uses a single rowid */ |
| 103711 | 103759 | sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey); |
| 103712 | 103760 | } |
| | @@ -103791,18 +103839,10 @@ |
| 103791 | 103839 | sqlite3VdbeJumpHere(v, addrLoop); |
| 103792 | 103840 | }else{ |
| 103793 | 103841 | sqlite3VdbeGoto(v, addrLoop); |
| 103794 | 103842 | sqlite3VdbeJumpHere(v, addrLoop); |
| 103795 | 103843 | } |
| 103796 | | - |
| 103797 | | - /* Close the cursors open on the table and its indexes. */ |
| 103798 | | - if( !isView && !IsVirtual(pTab) ){ |
| 103799 | | - if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
| 103800 | | - for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 103801 | | - sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i); |
| 103802 | | - } |
| 103803 | | - } |
| 103804 | 103844 | } /* End non-truncate path */ |
| 103805 | 103845 | |
| 103806 | 103846 | /* Update the sqlite_sequence table by storing the content of the |
| 103807 | 103847 | ** maximum rowid counter values recorded while inserting into |
| 103808 | 103848 | ** autoincrement tables. |
| | @@ -104360,10 +104400,12 @@ |
| 104360 | 104400 | isText = 0; |
| 104361 | 104401 | }else{ |
| 104362 | 104402 | zHaystack = sqlite3_value_text(argv[0]); |
| 104363 | 104403 | zNeedle = sqlite3_value_text(argv[1]); |
| 104364 | 104404 | isText = 1; |
| 104405 | + if( zNeedle==0 ) return; |
| 104406 | + assert( zHaystack ); |
| 104365 | 104407 | } |
| 104366 | 104408 | while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){ |
| 104367 | 104409 | N++; |
| 104368 | 104410 | do{ |
| 104369 | 104411 | nHaystack--; |
| | @@ -107898,11 +107940,11 @@ |
| 107898 | 107940 | int onError /* How to handle constraint errors */ |
| 107899 | 107941 | ){ |
| 107900 | 107942 | sqlite3 *db; /* The main database structure */ |
| 107901 | 107943 | Table *pTab; /* The table to insert into. aka TABLE */ |
| 107902 | 107944 | char *zTab; /* Name of the table into which we are inserting */ |
| 107903 | | - int i, j, idx; /* Loop counters */ |
| 107945 | + int i, j; /* Loop counters */ |
| 107904 | 107946 | Vdbe *v; /* Generate code into this virtual machine */ |
| 107905 | 107947 | Index *pIdx; /* For looping over indices of the table */ |
| 107906 | 107948 | int nColumn; /* Number of columns in the data */ |
| 107907 | 107949 | int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ |
| 107908 | 107950 | int iDataCur = 0; /* VDBE cursor that is the main data repository */ |
| | @@ -108205,12 +108247,14 @@ |
| 108205 | 108247 | &iDataCur, &iIdxCur); |
| 108206 | 108248 | aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1)); |
| 108207 | 108249 | if( aRegIdx==0 ){ |
| 108208 | 108250 | goto insert_cleanup; |
| 108209 | 108251 | } |
| 108210 | | - for(i=0; i<nIdx; i++){ |
| 108252 | + for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){ |
| 108253 | + assert( pIdx ); |
| 108211 | 108254 | aRegIdx[i] = ++pParse->nMem; |
| 108255 | + pParse->nMem += pIdx->nColumn; |
| 108212 | 108256 | } |
| 108213 | 108257 | } |
| 108214 | 108258 | |
| 108215 | 108259 | /* This is the top of the main insertion loop */ |
| 108216 | 108260 | if( useTempTable ){ |
| | @@ -108408,16 +108452,30 @@ |
| 108408 | 108452 | sqlite3MayAbort(pParse); |
| 108409 | 108453 | }else |
| 108410 | 108454 | #endif |
| 108411 | 108455 | { |
| 108412 | 108456 | int isReplace; /* Set to true if constraints may cause a replace */ |
| 108457 | + int bUseSeek; /* True to use OPFLAG_SEEKRESULT */ |
| 108413 | 108458 | sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
| 108414 | 108459 | regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0 |
| 108415 | 108460 | ); |
| 108416 | 108461 | sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); |
| 108462 | + |
| 108463 | + /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE |
| 108464 | + ** constraints or (b) there are no triggers and this table is not a |
| 108465 | + ** parent table in a foreign key constraint. It is safe to set the |
| 108466 | + ** flag in the second case as if any REPLACE constraint is hit, an |
| 108467 | + ** OP_Delete or OP_IdxDelete instruction will be executed on each |
| 108468 | + ** cursor that is disturbed. And these instructions both clear the |
| 108469 | + ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT |
| 108470 | + ** functionality. */ |
| 108471 | + bUseSeek = (isReplace==0 || (pTrigger==0 && |
| 108472 | + ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0) |
| 108473 | + )); |
| 108417 | 108474 | sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
| 108418 | | - regIns, aRegIdx, 0, appendFlag, isReplace==0); |
| 108475 | + regIns, aRegIdx, 0, appendFlag, bUseSeek |
| 108476 | + ); |
| 108419 | 108477 | } |
| 108420 | 108478 | } |
| 108421 | 108479 | |
| 108422 | 108480 | /* Update the count of rows that are inserted |
| 108423 | 108481 | */ |
| | @@ -108442,18 +108500,10 @@ |
| 108442 | 108500 | }else if( pSelect ){ |
| 108443 | 108501 | sqlite3VdbeGoto(v, addrCont); |
| 108444 | 108502 | sqlite3VdbeJumpHere(v, addrInsTop); |
| 108445 | 108503 | } |
| 108446 | 108504 | |
| 108447 | | - if( !IsVirtual(pTab) && !isView ){ |
| 108448 | | - /* Close all tables opened */ |
| 108449 | | - if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
| 108450 | | - for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 108451 | | - sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur); |
| 108452 | | - } |
| 108453 | | - } |
| 108454 | | - |
| 108455 | 108505 | insert_end: |
| 108456 | 108506 | /* Update the sqlite_sequence table by storing the content of the |
| 108457 | 108507 | ** maximum rowid counter values recorded while inserting into |
| 108458 | 108508 | ** autoincrement tables. |
| 108459 | 108509 | */ |
| | @@ -108656,11 +108706,10 @@ |
| 108656 | 108706 | int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ |
| 108657 | 108707 | int ipkTop = 0; /* Top of the rowid change constraint check */ |
| 108658 | 108708 | int ipkBottom = 0; /* Bottom of the rowid change constraint check */ |
| 108659 | 108709 | u8 isUpdate; /* True if this is an UPDATE operation */ |
| 108660 | 108710 | u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ |
| 108661 | | - int regRowid = -1; /* Register holding ROWID value */ |
| 108662 | 108711 | |
| 108663 | 108712 | isUpdate = regOldData!=0; |
| 108664 | 108713 | db = pParse->db; |
| 108665 | 108714 | v = sqlite3GetVdbe(pParse); |
| 108666 | 108715 | assert( v!=0 ); |
| | @@ -108776,11 +108825,11 @@ |
| 108776 | 108825 | }else if( onError==OE_Default ){ |
| 108777 | 108826 | onError = OE_Abort; |
| 108778 | 108827 | } |
| 108779 | 108828 | |
| 108780 | 108829 | if( isUpdate ){ |
| 108781 | | - /* pkChng!=0 does not mean that the rowid has change, only that |
| 108830 | + /* pkChng!=0 does not mean that the rowid has changed, only that |
| 108782 | 108831 | ** it might have changed. Skip the conflict logic below if the rowid |
| 108783 | 108832 | ** is unchanged. */ |
| 108784 | 108833 | sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); |
| 108785 | 108834 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 108786 | 108835 | VdbeCoverage(v); |
| | @@ -108911,11 +108960,11 @@ |
| 108911 | 108960 | } |
| 108912 | 108961 | |
| 108913 | 108962 | /* Create a record for this index entry as it should appear after |
| 108914 | 108963 | ** the insert or update. Store that record in the aRegIdx[ix] register |
| 108915 | 108964 | */ |
| 108916 | | - regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn); |
| 108965 | + regIdx = aRegIdx[ix]+1; |
| 108917 | 108966 | for(i=0; i<pIdx->nColumn; i++){ |
| 108918 | 108967 | int iField = pIdx->aiColumn[i]; |
| 108919 | 108968 | int x; |
| 108920 | 108969 | if( iField==XN_EXPR ){ |
| 108921 | 108970 | pParse->ckBase = regNewData+1; |
| | @@ -108922,23 +108971,20 @@ |
| 108922 | 108971 | sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); |
| 108923 | 108972 | pParse->ckBase = 0; |
| 108924 | 108973 | VdbeComment((v, "%s column %d", pIdx->zName, i)); |
| 108925 | 108974 | }else{ |
| 108926 | 108975 | if( iField==XN_ROWID || iField==pTab->iPKey ){ |
| 108927 | | - if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */ |
| 108928 | 108976 | x = regNewData; |
| 108929 | | - regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i; |
| 108930 | 108977 | }else{ |
| 108931 | 108978 | x = iField + regNewData + 1; |
| 108932 | 108979 | } |
| 108933 | 108980 | sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i); |
| 108934 | 108981 | VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); |
| 108935 | 108982 | } |
| 108936 | 108983 | } |
| 108937 | 108984 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); |
| 108938 | 108985 | VdbeComment((v, "for %s", pIdx->zName)); |
| 108939 | | - sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn); |
| 108940 | 108986 | |
| 108941 | 108987 | /* In an UPDATE operation, if this index is the PRIMARY KEY index |
| 108942 | 108988 | ** of a WITHOUT ROWID table and there has been no change the |
| 108943 | 108989 | ** primary key, then no collision is possible. The collision detection |
| 108944 | 108990 | ** logic below can all be skipped. */ |
| | @@ -108948,19 +108994,24 @@ |
| 108948 | 108994 | } |
| 108949 | 108995 | |
| 108950 | 108996 | /* Find out what action to take in case there is a uniqueness conflict */ |
| 108951 | 108997 | onError = pIdx->onError; |
| 108952 | 108998 | if( onError==OE_None ){ |
| 108953 | | - sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); |
| 108954 | 108999 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 108955 | 109000 | continue; /* pIdx is not a UNIQUE index */ |
| 108956 | 109001 | } |
| 108957 | 109002 | if( overrideError!=OE_Default ){ |
| 108958 | 109003 | onError = overrideError; |
| 108959 | 109004 | }else if( onError==OE_Default ){ |
| 108960 | 109005 | onError = OE_Abort; |
| 108961 | 109006 | } |
| 109007 | + |
| 109008 | + if( ix==0 && pPk==pIdx && onError==OE_Replace && pPk->pNext==0 ){ |
| 109009 | + sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 109010 | + continue; |
| 109011 | + } |
| 109012 | + |
| 108962 | 109013 | |
| 108963 | 109014 | /* Check to see if the new index entry will be unique */ |
| 108964 | 109015 | sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, |
| 108965 | 109016 | regIdx, pIdx->nKeyCol); VdbeCoverage(v); |
| 108966 | 109017 | |
| | @@ -109047,11 +109098,10 @@ |
| 109047 | 109098 | seenReplace = 1; |
| 109048 | 109099 | break; |
| 109049 | 109100 | } |
| 109050 | 109101 | } |
| 109051 | 109102 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 109052 | | - sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); |
| 109053 | 109103 | if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); |
| 109054 | 109104 | } |
| 109055 | 109105 | if( ipkTop ){ |
| 109056 | 109106 | sqlite3VdbeGoto(v, ipkTop+1); |
| 109057 | 109107 | sqlite3VdbeJumpHere(v, ipkBottom); |
| | @@ -109097,11 +109147,13 @@ |
| 109097 | 109147 | bAffinityDone = 1; |
| 109098 | 109148 | if( pIdx->pPartIdxWhere ){ |
| 109099 | 109149 | sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); |
| 109100 | 109150 | VdbeCoverage(v); |
| 109101 | 109151 | } |
| 109102 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]); |
| 109152 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], |
| 109153 | + aRegIdx[i]+1, |
| 109154 | + pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
| 109103 | 109155 | pik_flags = 0; |
| 109104 | 109156 | if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT; |
| 109105 | 109157 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
| 109106 | 109158 | assert( pParse->nested==0 ); |
| 109107 | 109159 | pik_flags |= OPFLAG_NCHANGE; |
| | @@ -109110,12 +109162,14 @@ |
| 109110 | 109162 | } |
| 109111 | 109163 | if( !HasRowid(pTab) ) return; |
| 109112 | 109164 | regData = regNewData + 1; |
| 109113 | 109165 | regRec = sqlite3GetTempReg(pParse); |
| 109114 | 109166 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); |
| 109115 | | - if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0); |
| 109116 | | - sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); |
| 109167 | + if( !bAffinityDone ){ |
| 109168 | + sqlite3TableAffinity(v, pTab, 0); |
| 109169 | + sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); |
| 109170 | + } |
| 109117 | 109171 | if( pParse->nested ){ |
| 109118 | 109172 | pik_flags = 0; |
| 109119 | 109173 | }else{ |
| 109120 | 109174 | pik_flags = OPFLAG_NCHANGE; |
| 109121 | 109175 | pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); |
| | @@ -109509,10 +109563,11 @@ |
| 109509 | 109563 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); |
| 109510 | 109564 | emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); |
| 109511 | 109565 | sqlite3VdbeJumpHere(v, addr1); |
| 109512 | 109566 | } |
| 109513 | 109567 | if( HasRowid(pSrc) ){ |
| 109568 | + u8 insFlags; |
| 109514 | 109569 | sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); |
| 109515 | 109570 | emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
| 109516 | 109571 | if( pDest->iPKey>=0 ){ |
| 109517 | 109572 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 109518 | 109573 | addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); |
| | @@ -109525,13 +109580,20 @@ |
| 109525 | 109580 | }else{ |
| 109526 | 109581 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 109527 | 109582 | assert( (pDest->tabFlags & TF_Autoincrement)==0 ); |
| 109528 | 109583 | } |
| 109529 | 109584 | sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); |
| 109585 | + if( db->flags & SQLITE_Vacuum ){ |
| 109586 | + sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); |
| 109587 | + insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID| |
| 109588 | + OPFLAG_APPEND|OPFLAG_USESEEKRESULT; |
| 109589 | + }else{ |
| 109590 | + insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND; |
| 109591 | + } |
| 109530 | 109592 | sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid, |
| 109531 | 109593 | (char*)pDest, P4_TABLE); |
| 109532 | | - sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); |
| 109594 | + sqlite3VdbeChangeP5(v, insFlags); |
| 109533 | 109595 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); |
| 109534 | 109596 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 109535 | 109597 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
| 109536 | 109598 | }else{ |
| 109537 | 109599 | sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); |
| | @@ -109579,12 +109641,12 @@ |
| 109579 | 109641 | } |
| 109580 | 109642 | } |
| 109581 | 109643 | if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){ |
| 109582 | 109644 | idxInsFlags |= OPFLAG_NCHANGE; |
| 109583 | 109645 | } |
| 109584 | | - sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); |
| 109585 | | - sqlite3VdbeChangeP5(v, idxInsFlags); |
| 109646 | + sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); |
| 109647 | + sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND); |
| 109586 | 109648 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); |
| 109587 | 109649 | sqlite3VdbeJumpHere(v, addr1); |
| 109588 | 109650 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 109589 | 109651 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
| 109590 | 109652 | } |
| | @@ -114945,11 +115007,11 @@ |
| 114945 | 115007 | int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ |
| 114946 | 115008 | int op; /* Opcode to add sorter record to sorter */ |
| 114947 | 115009 | int iLimit; /* LIMIT counter */ |
| 114948 | 115010 | |
| 114949 | 115011 | assert( bSeq==0 || bSeq==1 ); |
| 114950 | | - assert( nData==1 || regData==regOrigData ); |
| 115012 | + assert( nData==1 || regData==regOrigData || regOrigData==0 ); |
| 114951 | 115013 | if( nPrefixReg ){ |
| 114952 | 115014 | assert( nPrefixReg==nExpr+bSeq ); |
| 114953 | 115015 | regBase = regData - nExpr - bSeq; |
| 114954 | 115016 | }else{ |
| 114955 | 115017 | regBase = pParse->nMem + 1; |
| | @@ -114957,15 +115019,15 @@ |
| 114957 | 115019 | } |
| 114958 | 115020 | assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); |
| 114959 | 115021 | iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; |
| 114960 | 115022 | pSort->labelDone = sqlite3VdbeMakeLabel(v); |
| 114961 | 115023 | sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, |
| 114962 | | - SQLITE_ECEL_DUP|SQLITE_ECEL_REF); |
| 115024 | + SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0)); |
| 114963 | 115025 | if( bSeq ){ |
| 114964 | 115026 | sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); |
| 114965 | 115027 | } |
| 114966 | | - if( nPrefixReg==0 ){ |
| 115028 | + if( nPrefixReg==0 && nData>0 ){ |
| 114967 | 115029 | sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); |
| 114968 | 115030 | } |
| 114969 | 115031 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord); |
| 114970 | 115032 | if( nOBSat>0 ){ |
| 114971 | 115033 | int regPrevKey; /* The first nOBSat columns of the previous row */ |
| | @@ -115011,11 +115073,12 @@ |
| 115011 | 115073 | if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
| 115012 | 115074 | op = OP_SorterInsert; |
| 115013 | 115075 | }else{ |
| 115014 | 115076 | op = OP_IdxInsert; |
| 115015 | 115077 | } |
| 115016 | | - sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord); |
| 115078 | + sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord, |
| 115079 | + regBase+nOBSat, nBase-nOBSat); |
| 115017 | 115080 | if( iLimit ){ |
| 115018 | 115081 | int addr; |
| 115019 | 115082 | int r1 = 0; |
| 115020 | 115083 | /* Fill the sorter until it contains LIMIT+OFFSET entries. (The iLimit |
| 115021 | 115084 | ** register is initialized with value of LIMIT+OFFSET.) After the sorter |
| | @@ -115079,11 +115142,11 @@ |
| 115079 | 115142 | |
| 115080 | 115143 | v = pParse->pVdbe; |
| 115081 | 115144 | r1 = sqlite3GetTempReg(pParse); |
| 115082 | 115145 | sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v); |
| 115083 | 115146 | sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); |
| 115084 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); |
| 115147 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N); |
| 115085 | 115148 | sqlite3ReleaseTempReg(pParse, r1); |
| 115086 | 115149 | } |
| 115087 | 115150 | |
| 115088 | 115151 | /* |
| 115089 | 115152 | ** This routine generates the code for the inside of the inner loop |
| | @@ -115090,11 +115153,11 @@ |
| 115090 | 115153 | ** of a SELECT. |
| 115091 | 115154 | ** |
| 115092 | 115155 | ** If srcTab is negative, then the pEList expressions |
| 115093 | 115156 | ** are evaluated in order to get the data for this row. If srcTab is |
| 115094 | 115157 | ** zero or more, then data is pulled from srcTab and pEList is used only |
| 115095 | | -** to get number columns and the datatype for each column. |
| 115158 | +** to get the number of columns and the collation sequence for each column. |
| 115096 | 115159 | */ |
| 115097 | 115160 | static void selectInnerLoop( |
| 115098 | 115161 | Parse *pParse, /* The parser context */ |
| 115099 | 115162 | Select *p, /* The complete select statement being coded */ |
| 115100 | 115163 | ExprList *pEList, /* List of values being extracted */ |
| | @@ -115105,17 +115168,24 @@ |
| 115105 | 115168 | int iContinue, /* Jump here to continue with next row */ |
| 115106 | 115169 | int iBreak /* Jump here to break out of the inner loop */ |
| 115107 | 115170 | ){ |
| 115108 | 115171 | Vdbe *v = pParse->pVdbe; |
| 115109 | 115172 | int i; |
| 115110 | | - int hasDistinct; /* True if the DISTINCT keyword is present */ |
| 115111 | | - int regResult; /* Start of memory holding result set */ |
| 115173 | + int hasDistinct; /* True if the DISTINCT keyword is present */ |
| 115112 | 115174 | int eDest = pDest->eDest; /* How to dispose of results */ |
| 115113 | 115175 | int iParm = pDest->iSDParm; /* First argument to disposal method */ |
| 115114 | 115176 | int nResultCol; /* Number of result columns */ |
| 115115 | 115177 | int nPrefixReg = 0; /* Number of extra registers before regResult */ |
| 115116 | 115178 | |
| 115179 | + /* Usually, regResult is the first cell in an array of memory cells |
| 115180 | + ** containing the current result row. In this case regOrig is set to the |
| 115181 | + ** same value. However, if the results are being sent to the sorter, the |
| 115182 | + ** values for any expressions that are also part of the sort-key are omitted |
| 115183 | + ** from this array. In this case regOrig is set to zero. */ |
| 115184 | + int regResult; /* Start of memory holding current results */ |
| 115185 | + int regOrig; /* Start of memory holding full result (or 0) */ |
| 115186 | + |
| 115117 | 115187 | assert( v ); |
| 115118 | 115188 | assert( pEList!=0 ); |
| 115119 | 115189 | hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP; |
| 115120 | 115190 | if( pSort && pSort->pOrderBy==0 ) pSort = 0; |
| 115121 | 115191 | if( pSort==0 && !hasDistinct ){ |
| | @@ -115142,11 +115212,11 @@ |
| 115142 | 115212 | ** and reported later. But we need to make sure enough memory is allocated |
| 115143 | 115213 | ** to avoid other spurious errors in the meantime. */ |
| 115144 | 115214 | pParse->nMem += nResultCol; |
| 115145 | 115215 | } |
| 115146 | 115216 | pDest->nSdst = nResultCol; |
| 115147 | | - regResult = pDest->iSdst; |
| 115217 | + regOrig = regResult = pDest->iSdst; |
| 115148 | 115218 | if( srcTab>=0 ){ |
| 115149 | 115219 | for(i=0; i<nResultCol; i++){ |
| 115150 | 115220 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i); |
| 115151 | 115221 | VdbeComment((v, "%s", pEList->a[i].zName)); |
| 115152 | 115222 | } |
| | @@ -115158,11 +115228,30 @@ |
| 115158 | 115228 | if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ |
| 115159 | 115229 | ecelFlags = SQLITE_ECEL_DUP; |
| 115160 | 115230 | }else{ |
| 115161 | 115231 | ecelFlags = 0; |
| 115162 | 115232 | } |
| 115163 | | - sqlite3ExprCodeExprList(pParse, pEList, regResult, 0, ecelFlags); |
| 115233 | + assert( eDest!=SRT_Table || pSort==0 ); |
| 115234 | + if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab ){ |
| 115235 | + /* For each expression in pEList that is a copy of an expression in |
| 115236 | + ** the ORDER BY clause (pSort->pOrderBy), set the associated |
| 115237 | + ** iOrderByCol value to one more than the index of the ORDER BY |
| 115238 | + ** expression within the sort-key that pushOntoSorter() will generate. |
| 115239 | + ** This allows the pEList field to be omitted from the sorted record, |
| 115240 | + ** saving space and CPU cycles. */ |
| 115241 | + ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF); |
| 115242 | + for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){ |
| 115243 | + int j; |
| 115244 | + if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){ |
| 115245 | + pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat; |
| 115246 | + } |
| 115247 | + } |
| 115248 | + regOrig = 0; |
| 115249 | + assert( eDest==SRT_Set || eDest==SRT_Mem |
| 115250 | + || eDest==SRT_Coroutine || eDest==SRT_Output ); |
| 115251 | + } |
| 115252 | + nResultCol = sqlite3ExprCodeExprList(pParse,pEList,regResult,0,ecelFlags); |
| 115164 | 115253 | } |
| 115165 | 115254 | |
| 115166 | 115255 | /* If the DISTINCT keyword was present on the SELECT statement |
| 115167 | 115256 | ** and this row has been seen before, then do not make this row |
| 115168 | 115257 | ** part of the result. |
| | @@ -115232,11 +115321,11 @@ |
| 115232 | 115321 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 115233 | 115322 | case SRT_Union: { |
| 115234 | 115323 | int r1; |
| 115235 | 115324 | r1 = sqlite3GetTempReg(pParse); |
| 115236 | 115325 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1); |
| 115237 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); |
| 115326 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
| 115238 | 115327 | sqlite3ReleaseTempReg(pParse, r1); |
| 115239 | 115328 | break; |
| 115240 | 115329 | } |
| 115241 | 115330 | |
| 115242 | 115331 | /* Construct a record from the query result, but instead of |
| | @@ -115269,11 +115358,11 @@ |
| 115269 | 115358 | ** current row to the index and proceed with writing it to the |
| 115270 | 115359 | ** output table as well. */ |
| 115271 | 115360 | int addr = sqlite3VdbeCurrentAddr(v) + 4; |
| 115272 | 115361 | sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); |
| 115273 | 115362 | VdbeCoverage(v); |
| 115274 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1); |
| 115363 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol); |
| 115275 | 115364 | assert( pSort==0 ); |
| 115276 | 115365 | } |
| 115277 | 115366 | #endif |
| 115278 | 115367 | if( pSort ){ |
| 115279 | 115368 | pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg); |
| | @@ -115298,18 +115387,18 @@ |
| 115298 | 115387 | /* At first glance you would think we could optimize out the |
| 115299 | 115388 | ** ORDER BY in this case since the order of entries in the set |
| 115300 | 115389 | ** does not matter. But there might be a LIMIT clause, in which |
| 115301 | 115390 | ** case the order does matter */ |
| 115302 | 115391 | pushOntoSorter( |
| 115303 | | - pParse, pSort, p, regResult, regResult, nResultCol, nPrefixReg); |
| 115392 | + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
| 115304 | 115393 | }else{ |
| 115305 | 115394 | int r1 = sqlite3GetTempReg(pParse); |
| 115306 | 115395 | assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol ); |
| 115307 | 115396 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, |
| 115308 | 115397 | r1, pDest->zAffSdst, nResultCol); |
| 115309 | 115398 | sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol); |
| 115310 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); |
| 115399 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
| 115311 | 115400 | sqlite3ReleaseTempReg(pParse, r1); |
| 115312 | 115401 | } |
| 115313 | 115402 | break; |
| 115314 | 115403 | } |
| 115315 | 115404 | |
| | @@ -115324,15 +115413,16 @@ |
| 115324 | 115413 | /* If this is a scalar select that is part of an expression, then |
| 115325 | 115414 | ** store the results in the appropriate memory cell or array of |
| 115326 | 115415 | ** memory cells and break out of the scan loop. |
| 115327 | 115416 | */ |
| 115328 | 115417 | case SRT_Mem: { |
| 115329 | | - assert( nResultCol==pDest->nSdst ); |
| 115330 | 115418 | if( pSort ){ |
| 115419 | + assert( nResultCol<=pDest->nSdst ); |
| 115331 | 115420 | pushOntoSorter( |
| 115332 | | - pParse, pSort, p, regResult, regResult, nResultCol, nPrefixReg); |
| 115421 | + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
| 115333 | 115422 | }else{ |
| 115423 | + assert( nResultCol==pDest->nSdst ); |
| 115334 | 115424 | assert( regResult==iParm ); |
| 115335 | 115425 | /* The LIMIT clause will jump out of the loop for us */ |
| 115336 | 115426 | } |
| 115337 | 115427 | break; |
| 115338 | 115428 | } |
| | @@ -115341,11 +115431,11 @@ |
| 115341 | 115431 | case SRT_Coroutine: /* Send data to a co-routine */ |
| 115342 | 115432 | case SRT_Output: { /* Return the results */ |
| 115343 | 115433 | testcase( eDest==SRT_Coroutine ); |
| 115344 | 115434 | testcase( eDest==SRT_Output ); |
| 115345 | 115435 | if( pSort ){ |
| 115346 | | - pushOntoSorter(pParse, pSort, p, regResult, regResult, nResultCol, |
| 115436 | + pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol, |
| 115347 | 115437 | nPrefixReg); |
| 115348 | 115438 | }else if( eDest==SRT_Coroutine ){ |
| 115349 | 115439 | sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
| 115350 | 115440 | }else{ |
| 115351 | 115441 | sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol); |
| | @@ -115391,11 +115481,11 @@ |
| 115391 | 115481 | regResult + pSO->a[i].u.x.iOrderByCol - 1, |
| 115392 | 115482 | r2+i); |
| 115393 | 115483 | } |
| 115394 | 115484 | sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey); |
| 115395 | 115485 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1); |
| 115396 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); |
| 115486 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2); |
| 115397 | 115487 | if( addrTest ) sqlite3VdbeJumpHere(v, addrTest); |
| 115398 | 115488 | sqlite3ReleaseTempReg(pParse, r1); |
| 115399 | 115489 | sqlite3ReleaseTempRange(pParse, r2, nKey+2); |
| 115400 | 115490 | break; |
| 115401 | 115491 | } |
| | @@ -115626,18 +115716,17 @@ |
| 115626 | 115716 | ExprList *pOrderBy = pSort->pOrderBy; |
| 115627 | 115717 | int eDest = pDest->eDest; |
| 115628 | 115718 | int iParm = pDest->iSDParm; |
| 115629 | 115719 | int regRow; |
| 115630 | 115720 | int regRowid; |
| 115721 | + int iCol; |
| 115631 | 115722 | int nKey; |
| 115632 | 115723 | int iSortTab; /* Sorter cursor to read from */ |
| 115633 | 115724 | int nSortData; /* Trailing values to read from sorter */ |
| 115634 | 115725 | int i; |
| 115635 | 115726 | int bSeq; /* True if sorter record includes seq. no. */ |
| 115636 | | -#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 115637 | 115727 | struct ExprList_item *aOutEx = p->pEList->a; |
| 115638 | | -#endif |
| 115639 | 115728 | |
| 115640 | 115729 | assert( addrBreak<0 ); |
| 115641 | 115730 | if( pSort->labelBkOut ){ |
| 115642 | 115731 | sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); |
| 115643 | 115732 | sqlite3VdbeGoto(v, addrBreak); |
| | @@ -115671,12 +115760,18 @@ |
| 115671 | 115760 | addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); |
| 115672 | 115761 | codeOffset(v, p->iOffset, addrContinue); |
| 115673 | 115762 | iSortTab = iTab; |
| 115674 | 115763 | bSeq = 1; |
| 115675 | 115764 | } |
| 115676 | | - for(i=0; i<nSortData; i++){ |
| 115677 | | - sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i); |
| 115765 | + for(i=0, iCol=nKey+bSeq; i<nSortData; i++){ |
| 115766 | + int iRead; |
| 115767 | + if( aOutEx[i].u.x.iOrderByCol ){ |
| 115768 | + iRead = aOutEx[i].u.x.iOrderByCol-1; |
| 115769 | + }else{ |
| 115770 | + iRead = iCol++; |
| 115771 | + } |
| 115772 | + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i); |
| 115678 | 115773 | VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); |
| 115679 | 115774 | } |
| 115680 | 115775 | switch( eDest ){ |
| 115681 | 115776 | case SRT_EphemTab: { |
| 115682 | 115777 | sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); |
| | @@ -115688,11 +115783,11 @@ |
| 115688 | 115783 | case SRT_Set: { |
| 115689 | 115784 | assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) ); |
| 115690 | 115785 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid, |
| 115691 | 115786 | pDest->zAffSdst, nColumn); |
| 115692 | 115787 | sqlite3ExprCacheAffinityChange(pParse, regRow, nColumn); |
| 115693 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid); |
| 115788 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn); |
| 115694 | 115789 | break; |
| 115695 | 115790 | } |
| 115696 | 115791 | case SRT_Mem: { |
| 115697 | 115792 | /* The LIMIT clause will terminate the loop for us */ |
| 115698 | 115793 | break; |
| | @@ -117064,11 +117159,12 @@ |
| 117064 | 117159 | testcase( pIn->nSdst>1 ); |
| 117065 | 117160 | r1 = sqlite3GetTempReg(pParse); |
| 117066 | 117161 | sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, |
| 117067 | 117162 | r1, pDest->zAffSdst, pIn->nSdst); |
| 117068 | 117163 | sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst); |
| 117069 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1); |
| 117164 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1, |
| 117165 | + pIn->iSdst, pIn->nSdst); |
| 117070 | 117166 | sqlite3ReleaseTempReg(pParse, r1); |
| 117071 | 117167 | break; |
| 117072 | 117168 | } |
| 117073 | 117169 | |
| 117074 | 117170 | /* If this is a scalar select that is part of an expression, then |
| | @@ -121687,16 +121783,18 @@ |
| 121687 | 121783 | */ |
| 121688 | 121784 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 121689 | 121785 | int reg; |
| 121690 | 121786 | if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ |
| 121691 | 121787 | reg = ++pParse->nMem; |
| 121788 | + pParse->nMem += pIdx->nColumn; |
| 121692 | 121789 | }else{ |
| 121693 | 121790 | reg = 0; |
| 121694 | 121791 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 121695 | 121792 | i16 iIdxCol = pIdx->aiColumn[i]; |
| 121696 | 121793 | if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){ |
| 121697 | 121794 | reg = ++pParse->nMem; |
| 121795 | + pParse->nMem += pIdx->nColumn; |
| 121698 | 121796 | break; |
| 121699 | 121797 | } |
| 121700 | 121798 | } |
| 121701 | 121799 | } |
| 121702 | 121800 | if( reg==0 ) aToOpen[j+1] = 0; |
| | @@ -121803,11 +121901,11 @@ |
| 121803 | 121901 | nKey = nPk; |
| 121804 | 121902 | regKey = iPk; |
| 121805 | 121903 | }else{ |
| 121806 | 121904 | sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, |
| 121807 | 121905 | sqlite3IndexAffinityStr(db, pPk), nPk); |
| 121808 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey); |
| 121906 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk); |
| 121809 | 121907 | } |
| 121810 | 121908 | sqlite3WhereEnd(pWInfo); |
| 121811 | 121909 | } |
| 121812 | 121910 | |
| 121813 | 121911 | /* Initialize the count of updated rows |
| | @@ -122062,19 +122160,10 @@ |
| 122062 | 122160 | }else{ |
| 122063 | 122161 | sqlite3VdbeGoto(v, labelContinue); |
| 122064 | 122162 | } |
| 122065 | 122163 | sqlite3VdbeResolveLabel(v, labelBreak); |
| 122066 | 122164 | |
| 122067 | | - /* Close all tables */ |
| 122068 | | - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 122069 | | - assert( aRegIdx ); |
| 122070 | | - if( aToOpen[i+1] ){ |
| 122071 | | - sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); |
| 122072 | | - } |
| 122073 | | - } |
| 122074 | | - if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0); |
| 122075 | | - |
| 122076 | 122165 | /* Update the sqlite_sequence table by storing the content of the |
| 122077 | 122166 | ** maximum rowid counter values recorded while inserting into |
| 122078 | 122167 | ** autoincrement tables. |
| 122079 | 122168 | */ |
| 122080 | 122169 | if( pParse->nested==0 && pParse->pTriggerTab==0 ){ |
| | @@ -124822,10 +124911,11 @@ |
| 124822 | 124911 | if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){ |
| 124823 | 124912 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0); |
| 124824 | 124913 | }else{ |
| 124825 | 124914 | Select *pSelect = pX->x.pSelect; |
| 124826 | 124915 | sqlite3 *db = pParse->db; |
| 124916 | + u16 savedDbOptFlags = db->dbOptFlags; |
| 124827 | 124917 | ExprList *pOrigRhs = pSelect->pEList; |
| 124828 | 124918 | ExprList *pOrigLhs = pX->pLeft->x.pList; |
| 124829 | 124919 | ExprList *pRhs = 0; /* New Select.pEList for RHS */ |
| 124830 | 124920 | ExprList *pLhs = 0; /* New pX->pLeft vector */ |
| 124831 | 124921 | |
| | @@ -124865,11 +124955,13 @@ |
| 124865 | 124955 | pLeft->x.pList = pLhs; |
| 124866 | 124956 | aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int) * nEq); |
| 124867 | 124957 | testcase( aiMap==0 ); |
| 124868 | 124958 | } |
| 124869 | 124959 | pSelect->pEList = pRhs; |
| 124960 | + db->dbOptFlags |= SQLITE_QueryFlattener; |
| 124870 | 124961 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap); |
| 124962 | + db->dbOptFlags = savedDbOptFlags; |
| 124871 | 124963 | testcase( aiMap!=0 && aiMap[0]!=0 ); |
| 124872 | 124964 | pSelect->pEList = pOrigRhs; |
| 124873 | 124965 | pLeft->x.pList = pOrigLhs; |
| 124874 | 124966 | pX->pLeft = pLeft; |
| 124875 | 124967 | } |
| | @@ -126220,11 +126312,12 @@ |
| 126220 | 126312 | jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
| 126221 | 126313 | VdbeCoverage(v); |
| 126222 | 126314 | } |
| 126223 | 126315 | if( iSet>=0 ){ |
| 126224 | 126316 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 126225 | | - sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); |
| 126317 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid, |
| 126318 | + r, nPk); |
| 126226 | 126319 | if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 126227 | 126320 | } |
| 126228 | 126321 | |
| 126229 | 126322 | /* Release the array of temp registers */ |
| 126230 | 126323 | sqlite3ReleaseTempRange(pParse, r, nPk); |
| | @@ -127684,10 +127777,12 @@ |
| 127684 | 127777 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 127685 | 127778 | |
| 127686 | 127779 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 127687 | 127780 | ** an index for tables to the left of the join. |
| 127688 | 127781 | */ |
| 127782 | + testcase( pTerm!=&pWC->a[idxTerm] ); |
| 127783 | + pTerm = &pWC->a[idxTerm]; |
| 127689 | 127784 | pTerm->prereqRight |= extraRight; |
| 127690 | 127785 | } |
| 127691 | 127786 | |
| 127692 | 127787 | /*************************************************************************** |
| 127693 | 127788 | ** Routines with file scope above. Interface to the rest of the where.c |
| | @@ -132767,31 +132862,10 @@ |
| 132767 | 132862 | translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, |
| 132768 | 132863 | pTabItem->regResult, 0); |
| 132769 | 132864 | continue; |
| 132770 | 132865 | } |
| 132771 | 132866 | |
| 132772 | | - /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 132773 | | - ** Except, do not close cursors that will be reused by the OR optimization |
| 132774 | | - ** (WHERE_OR_SUBCLAUSE). And do not close the OP_OpenWrite cursors |
| 132775 | | - ** created for the ONEPASS optimization. |
| 132776 | | - */ |
| 132777 | | - if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 132778 | | - && pTab->pSelect==0 |
| 132779 | | - && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 132780 | | - ){ |
| 132781 | | - int ws = pLoop->wsFlags; |
| 132782 | | - if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){ |
| 132783 | | - sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 132784 | | - } |
| 132785 | | - if( (ws & WHERE_INDEXED)!=0 |
| 132786 | | - && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 |
| 132787 | | - && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] |
| 132788 | | - ){ |
| 132789 | | - sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 132790 | | - } |
| 132791 | | - } |
| 132792 | | - |
| 132793 | 132867 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 132794 | 132868 | ** from the index instead of from the table where possible. In some cases |
| 132795 | 132869 | ** this optimization prevents the table from ever being read, which can |
| 132796 | 132870 | ** yield a significant performance boost. |
| 132797 | 132871 | ** |
| | @@ -165455,24 +165529,24 @@ |
| 165455 | 165529 | int nArg; /* Number of arguments */ |
| 165456 | 165530 | int enc; /* Optimal text encoding */ |
| 165457 | 165531 | void *pContext; /* sqlite3_user_data() context */ |
| 165458 | 165532 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 165459 | 165533 | } scalars[] = { |
| 165460 | | - {"regexp", 2, SQLITE_ANY, 0, icuRegexpFunc}, |
| 165461 | | - |
| 165462 | | - {"lower", 1, SQLITE_UTF16, 0, icuCaseFunc16}, |
| 165463 | | - {"lower", 2, SQLITE_UTF16, 0, icuCaseFunc16}, |
| 165464 | | - {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16}, |
| 165465 | | - {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16}, |
| 165466 | | - |
| 165467 | | - {"lower", 1, SQLITE_UTF8, 0, icuCaseFunc16}, |
| 165468 | | - {"lower", 2, SQLITE_UTF8, 0, icuCaseFunc16}, |
| 165469 | | - {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16}, |
| 165470 | | - {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16}, |
| 165471 | | - |
| 165472 | | - {"like", 2, SQLITE_UTF8, 0, icuLikeFunc}, |
| 165473 | | - {"like", 3, SQLITE_UTF8, 0, icuLikeFunc}, |
| 165534 | + {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc}, |
| 165535 | + |
| 165536 | + {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 165537 | + {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 165538 | + {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 165539 | + {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 165540 | + |
| 165541 | + {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 165542 | + {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 165543 | + {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 165544 | + {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 165545 | + |
| 165546 | + {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 165547 | + {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 165474 | 165548 | |
| 165475 | 165549 | {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation}, |
| 165476 | 165550 | }; |
| 165477 | 165551 | |
| 165478 | 165552 | int rc = SQLITE_OK; |
| | @@ -176486,17 +176560,19 @@ |
| 176486 | 176560 | ** to pass signed char values. |
| 176487 | 176561 | */ |
| 176488 | 176562 | #ifdef sqlite3Isdigit |
| 176489 | 176563 | /* Use the SQLite core versions if this routine is part of the |
| 176490 | 176564 | ** SQLite amalgamation */ |
| 176491 | | -# define safe_isdigit(x) sqlite3Isdigit(x) |
| 176492 | | -# define safe_isalnum(x) sqlite3Isalnum(x) |
| 176565 | +# define safe_isdigit(x) sqlite3Isdigit(x) |
| 176566 | +# define safe_isalnum(x) sqlite3Isalnum(x) |
| 176567 | +# define safe_isxdigit(x) sqlite3Isxdigit(x) |
| 176493 | 176568 | #else |
| 176494 | 176569 | /* Use the standard library for separate compilation */ |
| 176495 | 176570 | #include <ctype.h> /* amalgamator: keep */ |
| 176496 | | -# define safe_isdigit(x) isdigit((unsigned char)(x)) |
| 176497 | | -# define safe_isalnum(x) isalnum((unsigned char)(x)) |
| 176571 | +# define safe_isdigit(x) isdigit((unsigned char)(x)) |
| 176572 | +# define safe_isalnum(x) isalnum((unsigned char)(x)) |
| 176573 | +# define safe_isxdigit(x) isxdigit((unsigned char)(x)) |
| 176498 | 176574 | #endif |
| 176499 | 176575 | |
| 176500 | 176576 | /* |
| 176501 | 176577 | ** Growing our own isspace() routine this way is twice as fast as |
| 176502 | 176578 | ** the library isspace() function, resulting in a 7% overall performance |
| | @@ -177030,16 +177106,17 @@ |
| 177030 | 177106 | zOut[j++] = c; |
| 177031 | 177107 | }else{ |
| 177032 | 177108 | c = z[++i]; |
| 177033 | 177109 | if( c=='u' ){ |
| 177034 | 177110 | u32 v = 0, k; |
| 177035 | | - for(k=0; k<4 && i<n-2; i++, k++){ |
| 177111 | + for(k=0; k<4; i++, k++){ |
| 177112 | + assert( i<n-2 ); |
| 177036 | 177113 | c = z[i+1]; |
| 177037 | | - if( c>='0' && c<='9' ) v = v*16 + c - '0'; |
| 177038 | | - else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10; |
| 177039 | | - else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10; |
| 177040 | | - else break; |
| 177114 | + assert( safe_isxdigit(c) ); |
| 177115 | + if( c<='9' ) v = v*16 + c - '0'; |
| 177116 | + else if( c<='F' ) v = v*16 + c - 'A' + 10; |
| 177117 | + else v = v*16 + c - 'a' + 10; |
| 177041 | 177118 | } |
| 177042 | 177119 | if( v==0 ) break; |
| 177043 | 177120 | if( v<=0x7f ){ |
| 177044 | 177121 | zOut[j++] = (char)v; |
| 177045 | 177122 | }else if( v<=0x7ff ){ |
| | @@ -177138,10 +177215,19 @@ |
| 177138 | 177215 | p->iVal = 0; |
| 177139 | 177216 | p->n = n; |
| 177140 | 177217 | p->u.zJContent = zContent; |
| 177141 | 177218 | return pParse->nNode++; |
| 177142 | 177219 | } |
| 177220 | + |
| 177221 | +/* |
| 177222 | +** Return true if z[] begins with 4 (or more) hexadecimal digits |
| 177223 | +*/ |
| 177224 | +static int jsonIs4Hex(const char *z){ |
| 177225 | + int i; |
| 177226 | + for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0; |
| 177227 | + return 1; |
| 177228 | +} |
| 177143 | 177229 | |
| 177144 | 177230 | /* |
| 177145 | 177231 | ** Parse a single JSON value which begins at pParse->zJson[i]. Return the |
| 177146 | 177232 | ** index of the first character past the end of the value parsed. |
| 177147 | 177233 | ** |
| | @@ -177213,12 +177299,17 @@ |
| 177213 | 177299 | for(;;){ |
| 177214 | 177300 | c = pParse->zJson[j]; |
| 177215 | 177301 | if( c==0 ) return -1; |
| 177216 | 177302 | if( c=='\\' ){ |
| 177217 | 177303 | c = pParse->zJson[++j]; |
| 177218 | | - if( c==0 ) return -1; |
| 177219 | | - jnFlags = JNODE_ESCAPE; |
| 177304 | + if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f' |
| 177305 | + || c=='n' || c=='r' || c=='t' |
| 177306 | + || (c=='u' && jsonIs4Hex(pParse->zJson+j+1)) ){ |
| 177307 | + jnFlags = JNODE_ESCAPE; |
| 177308 | + }else{ |
| 177309 | + return -1; |
| 177310 | + } |
| 177220 | 177311 | }else if( c=='"' ){ |
| 177221 | 177312 | break; |
| 177222 | 177313 | } |
| 177223 | 177314 | j++; |
| 177224 | 177315 | } |
| | @@ -178082,11 +178173,11 @@ |
| 178082 | 178173 | JsonString *pStr; |
| 178083 | 178174 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 178084 | 178175 | if( pStr ){ |
| 178085 | 178176 | jsonAppendChar(pStr, '}'); |
| 178086 | 178177 | if( pStr->bErr ){ |
| 178087 | | - if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx); |
| 178178 | + if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx); |
| 178088 | 178179 | assert( pStr->bStatic ); |
| 178089 | 178180 | }else{ |
| 178090 | 178181 | sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, |
| 178091 | 178182 | pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); |
| 178092 | 178183 | pStr->bStatic = 1; |
| | @@ -178360,13 +178451,13 @@ |
| 178360 | 178451 | break; |
| 178361 | 178452 | } |
| 178362 | 178453 | /* For json_each() path and root are the same so fall through |
| 178363 | 178454 | ** into the root case */ |
| 178364 | 178455 | } |
| 178365 | | - case JEACH_ROOT: { |
| 178456 | + default: { |
| 178366 | 178457 | const char *zRoot = p->zRoot; |
| 178367 | | - if( zRoot==0 ) zRoot = "$"; |
| 178458 | + if( zRoot==0 ) zRoot = "$"; |
| 178368 | 178459 | sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC); |
| 178369 | 178460 | break; |
| 178370 | 178461 | } |
| 178371 | 178462 | case JEACH_JSON: { |
| 178372 | 178463 | assert( i==JEACH_JSON ); |
| | @@ -184289,11 +184380,11 @@ |
| 184289 | 184380 | pNode->bEof = 1; |
| 184290 | 184381 | return rc; |
| 184291 | 184382 | } |
| 184292 | 184383 | }else{ |
| 184293 | 184384 | Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter; |
| 184294 | | - if( pIter->iRowid==iLast ) continue; |
| 184385 | + if( pIter->iRowid==iLast || pIter->bEof ) continue; |
| 184295 | 184386 | bMatch = 0; |
| 184296 | 184387 | if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){ |
| 184297 | 184388 | return rc; |
| 184298 | 184389 | } |
| 184299 | 184390 | } |
| | @@ -189429,10 +189520,11 @@ |
| 189429 | 189520 | Fts5Iter *pIter, |
| 189430 | 189521 | int bFrom, /* True if argument iFrom is valid */ |
| 189431 | 189522 | i64 iFrom /* Advance at least as far as this */ |
| 189432 | 189523 | ){ |
| 189433 | 189524 | int bUseFrom = bFrom; |
| 189525 | + assert( pIter->base.bEof==0 ); |
| 189434 | 189526 | while( p->rc==SQLITE_OK ){ |
| 189435 | 189527 | int iFirst = pIter->aFirst[1].iFirst; |
| 189436 | 189528 | int bNewTerm = 0; |
| 189437 | 189529 | Fts5SegIter *pSeg = &pIter->aSeg[iFirst]; |
| 189438 | 189530 | assert( p->rc==SQLITE_OK ); |
| | @@ -195693,11 +195785,11 @@ |
| 195693 | 195785 | int nArg, /* Number of args */ |
| 195694 | 195786 | sqlite3_value **apUnused /* Function arguments */ |
| 195695 | 195787 | ){ |
| 195696 | 195788 | assert( nArg==0 ); |
| 195697 | 195789 | UNUSED_PARAM2(nArg, apUnused); |
| 195698 | | - sqlite3_result_text(pCtx, "fts5: 2016-10-26 16:05:10 ec9dab8054c71d112c68f58a45821b38c2a45677", -1, SQLITE_TRANSIENT); |
| 195790 | + sqlite3_result_text(pCtx, "fts5: 2016-11-19 18:31:37 28393c413cc4505b94411730e728583c5d4baaae", -1, SQLITE_TRANSIENT); |
| 195699 | 195791 | } |
| 195700 | 195792 | |
| 195701 | 195793 | static int fts5Init(sqlite3 *db){ |
| 195702 | 195794 | static const sqlite3_module fts5Mod = { |
| 195703 | 195795 | /* iVersion */ 2, |
| 195704 | 195796 | |