Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: third_party/sqlite/src/src/vdbe.c

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/sqlite/src/src/vdbe.h ('k') | third_party/sqlite/src/src/vdbeInt.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 #ifdef SQLITE_TEST 80 #ifdef SQLITE_TEST
81 int sqlite3_max_blobsize = 0; 81 int sqlite3_max_blobsize = 0;
82 static void updateMaxBlobsize(Mem *p){ 82 static void updateMaxBlobsize(Mem *p){
83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ 83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
84 sqlite3_max_blobsize = p->n; 84 sqlite3_max_blobsize = p->n;
85 } 85 }
86 } 86 }
87 #endif 87 #endif
88 88
89 /* 89 /*
90 ** This macro evaluates to true if either the update hook or the preupdate
91 ** hook are enabled for database connect DB.
92 */
93 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
94 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
95 #else
96 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
97 #endif
98
99 /*
90 ** The next global variable is incremented each time the OP_Found opcode 100 ** The next global variable is incremented each time the OP_Found opcode
91 ** is executed. This is used to test whether or not the foreign key 101 ** is executed. This is used to test whether or not the foreign key
92 ** operation implemented using OP_FkIsZero is working. This variable 102 ** operation implemented using OP_FkIsZero is working. This variable
93 ** has no function other than to help verify the correct operation of the 103 ** has no function other than to help verify the correct operation of the
94 ** library. 104 ** library.
95 */ 105 */
96 #ifdef SQLITE_TEST 106 #ifdef SQLITE_TEST
97 int sqlite3_found_count = 0; 107 int sqlite3_found_count = 0;
98 #endif 108 #endif
99 109
100 /* 110 /*
101 ** Test a register to see if it exceeds the current maximum blob size. 111 ** Test a register to see if it exceeds the current maximum blob size.
102 ** If it does, record the new maximum blob size. 112 ** If it does, record the new maximum blob size.
103 */ 113 */
104 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) 114 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
105 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) 115 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
106 #else 116 #else
107 # define UPDATE_MAX_BLOBSIZE(P) 117 # define UPDATE_MAX_BLOBSIZE(P)
108 #endif 118 #endif
109 119
110 /* 120 /*
111 ** Invoke the VDBE coverage callback, if that callback is defined. This 121 ** Invoke the VDBE coverage callback, if that callback is defined. This
112 ** feature is used for test suite validation only and does not appear an 122 ** feature is used for test suite validation only and does not appear an
113 ** production builds. 123 ** production builds.
114 ** 124 **
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 ** 195 **
186 ** * Sometimes cursor numbers are used for a couple of different 196 ** * Sometimes cursor numbers are used for a couple of different
187 ** purposes in a vdbe program. The different uses might require 197 ** purposes in a vdbe program. The different uses might require
188 ** different sized allocations. Memory cells provide growable 198 ** different sized allocations. Memory cells provide growable
189 ** allocations. 199 ** allocations.
190 ** 200 **
191 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can 201 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
192 ** be freed lazily via the sqlite3_release_memory() API. This 202 ** be freed lazily via the sqlite3_release_memory() API. This
193 ** minimizes the number of malloc calls made by the system. 203 ** minimizes the number of malloc calls made by the system.
194 ** 204 **
195 ** Memory cells for cursors are allocated at the top of the address 205 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
196 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for 206 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
197 ** cursor 1 is managed by memory cell (p->nMem-1), etc. 207 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
198 */ 208 */
199 Mem *pMem = &p->aMem[p->nMem-iCur]; 209 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
200 210
201 int nByte; 211 int nByte;
202 VdbeCursor *pCx = 0; 212 VdbeCursor *pCx = 0;
203 nByte = 213 nByte =
204 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 214 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
205 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); 215 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
206 216
207 assert( iCur<p->nCursor ); 217 assert( iCur>=0 && iCur<p->nCursor );
208 if( p->apCsr[iCur] ){ 218 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
209 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); 219 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
210 p->apCsr[iCur] = 0; 220 p->apCsr[iCur] = 0;
211 } 221 }
212 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ 222 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
213 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; 223 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
214 memset(pCx, 0, sizeof(VdbeCursor)); 224 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
215 pCx->eCurType = eCurType; 225 pCx->eCurType = eCurType;
216 pCx->iDb = iDb; 226 pCx->iDb = iDb;
217 pCx->nField = nField; 227 pCx->nField = nField;
218 pCx->aOffset = &pCx->aType[nField]; 228 pCx->aOffset = &pCx->aType[nField];
219 if( eCurType==CURTYPE_BTREE ){ 229 if( eCurType==CURTYPE_BTREE ){
220 pCx->uc.pCursor = (BtCursor*) 230 pCx->uc.pCursor = (BtCursor*)
221 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; 231 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
222 sqlite3BtreeCursorZero(pCx->uc.pCursor); 232 sqlite3BtreeCursorZero(pCx->uc.pCursor);
223 } 233 }
224 } 234 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 ** No-op. pRec is unchanged. 285 ** No-op. pRec is unchanged.
276 */ 286 */
277 static void applyAffinity( 287 static void applyAffinity(
278 Mem *pRec, /* The value to apply affinity to */ 288 Mem *pRec, /* The value to apply affinity to */
279 char affinity, /* The affinity to be applied */ 289 char affinity, /* The affinity to be applied */
280 u8 enc /* Use this text encoding */ 290 u8 enc /* Use this text encoding */
281 ){ 291 ){
282 if( affinity>=SQLITE_AFF_NUMERIC ){ 292 if( affinity>=SQLITE_AFF_NUMERIC ){
283 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL 293 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
284 || affinity==SQLITE_AFF_NUMERIC ); 294 || affinity==SQLITE_AFF_NUMERIC );
285 if( (pRec->flags & MEM_Int)==0 ){ 295 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
286 if( (pRec->flags & MEM_Real)==0 ){ 296 if( (pRec->flags & MEM_Real)==0 ){
287 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); 297 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
288 }else{ 298 }else{
289 sqlite3VdbeIntegerAffinity(pRec); 299 sqlite3VdbeIntegerAffinity(pRec);
290 } 300 }
291 } 301 }
292 }else if( affinity==SQLITE_AFF_TEXT ){ 302 }else if( affinity==SQLITE_AFF_TEXT ){
293 /* Only attempt the conversion to TEXT if there is an integer or real 303 /* Only attempt the conversion to TEXT if there is an integer or real
294 ** representation (blob and NULL do not get converted) but no string 304 ** representation (blob and NULL do not get converted) but no string
295 ** representation. 305 ** representation. It would be harmless to repeat the conversion if
296 */ 306 ** there is already a string rep, but it is pointless to waste those
297 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ 307 ** CPU cycles. */
298 sqlite3VdbeMemStringify(pRec, enc, 1); 308 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
309 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
310 sqlite3VdbeMemStringify(pRec, enc, 1);
311 }
299 } 312 }
300 pRec->flags &= ~(MEM_Real|MEM_Int); 313 pRec->flags &= ~(MEM_Real|MEM_Int);
301 } 314 }
302 } 315 }
303 316
304 /* 317 /*
305 ** Try to convert the type of a function argument or a result column 318 ** Try to convert the type of a function argument or a result column
306 ** into a numeric representation. Use either INTEGER or REAL whichever 319 ** into a numeric representation. Use either INTEGER or REAL whichever
307 ** is appropriate. But only do the conversion if it is possible without 320 ** is appropriate. But only do the conversion if it is possible without
308 ** loss of information and return the revised type of the argument. 321 ** loss of information and return the revised type of the argument.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 }else if( p->flags & MEM_Real ){ 477 }else if( p->flags & MEM_Real ){
465 printf(" r:%g", p->u.r); 478 printf(" r:%g", p->u.r);
466 #endif 479 #endif
467 }else if( p->flags & MEM_RowSet ){ 480 }else if( p->flags & MEM_RowSet ){
468 printf(" (rowset)"); 481 printf(" (rowset)");
469 }else{ 482 }else{
470 char zBuf[200]; 483 char zBuf[200];
471 sqlite3VdbeMemPrettyPrint(p, zBuf); 484 sqlite3VdbeMemPrettyPrint(p, zBuf);
472 printf(" %s", zBuf); 485 printf(" %s", zBuf);
473 } 486 }
487 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
474 } 488 }
475 static void registerTrace(int iReg, Mem *p){ 489 static void registerTrace(int iReg, Mem *p){
476 printf("REG[%d] = ", iReg); 490 printf("REG[%d] = ", iReg);
477 memTracePrint(p); 491 memTracePrint(p);
478 printf("\n"); 492 printf("\n");
479 } 493 }
480 #endif 494 #endif
481 495
482 #ifdef SQLITE_DEBUG 496 #ifdef SQLITE_DEBUG
483 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) 497 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 ** overwritten with an integer value. 535 ** overwritten with an integer value.
522 */ 536 */
523 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ 537 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
524 sqlite3VdbeMemSetNull(pOut); 538 sqlite3VdbeMemSetNull(pOut);
525 pOut->flags = MEM_Int; 539 pOut->flags = MEM_Int;
526 return pOut; 540 return pOut;
527 } 541 }
528 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ 542 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
529 Mem *pOut; 543 Mem *pOut;
530 assert( pOp->p2>0 ); 544 assert( pOp->p2>0 );
531 assert( pOp->p2<=(p->nMem-p->nCursor) ); 545 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
532 pOut = &p->aMem[pOp->p2]; 546 pOut = &p->aMem[pOp->p2];
533 memAboutToChange(p, pOut); 547 memAboutToChange(p, pOut);
534 if( VdbeMemDynamic(pOut) ){ 548 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
535 return out2PrereleaseWithClear(pOut); 549 return out2PrereleaseWithClear(pOut);
536 }else{ 550 }else{
537 pOut->flags = MEM_Int; 551 pOut->flags = MEM_Int;
538 return pOut; 552 return pOut;
539 } 553 }
540 } 554 }
541 555
542 556
543 /* 557 /*
544 ** Execute as much of a VDBE program as we can. 558 ** Execute as much of a VDBE program as we can.
545 ** This is the core of sqlite3_step(). 559 ** This is the core of sqlite3_step().
546 */ 560 */
547 int sqlite3VdbeExec( 561 int sqlite3VdbeExec(
548 Vdbe *p /* The VDBE */ 562 Vdbe *p /* The VDBE */
549 ){ 563 ){
550 Op *aOp = p->aOp; /* Copy of p->aOp */ 564 Op *aOp = p->aOp; /* Copy of p->aOp */
551 Op *pOp = aOp; /* Current operation */ 565 Op *pOp = aOp; /* Current operation */
552 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 566 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
553 Op *pOrigOp; /* Value of pOp at the top of the loop */ 567 Op *pOrigOp; /* Value of pOp at the top of the loop */
554 #endif 568 #endif
569 #ifdef SQLITE_DEBUG
570 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
571 #endif
555 int rc = SQLITE_OK; /* Value to return */ 572 int rc = SQLITE_OK; /* Value to return */
556 sqlite3 *db = p->db; /* The database */ 573 sqlite3 *db = p->db; /* The database */
557 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ 574 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
558 u8 encoding = ENC(db); /* The database encoding */ 575 u8 encoding = ENC(db); /* The database encoding */
559 int iCompare = 0; /* Result of last OP_Compare operation */ 576 int iCompare = 0; /* Result of last comparison */
560 unsigned nVmStep = 0; /* Number of virtual machine steps */ 577 unsigned nVmStep = 0; /* Number of virtual machine steps */
561 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 578 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
562 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ 579 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
563 #endif 580 #endif
564 Mem *aMem = p->aMem; /* Copy of p->aMem */ 581 Mem *aMem = p->aMem; /* Copy of p->aMem */
565 Mem *pIn1 = 0; /* 1st input operand */ 582 Mem *pIn1 = 0; /* 1st input operand */
566 Mem *pIn2 = 0; /* 2nd input operand */ 583 Mem *pIn2 = 0; /* 2nd input operand */
567 Mem *pIn3 = 0; /* 3rd input operand */ 584 Mem *pIn3 = 0; /* 3rd input operand */
568 Mem *pOut = 0; /* Output operand */ 585 Mem *pOut = 0; /* Output operand */
569 int *aPermute = 0; /* Permutation of columns for OP_Compare */
570 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
571 #ifdef VDBE_PROFILE 586 #ifdef VDBE_PROFILE
572 u64 start; /* CPU clock count at start of opcode */ 587 u64 start; /* CPU clock count at start of opcode */
573 #endif 588 #endif
574 /*** INSERT STACK UNION HERE ***/ 589 /*** INSERT STACK UNION HERE ***/
575 590
576 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ 591 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
577 sqlite3VdbeEnter(p); 592 sqlite3VdbeEnter(p);
578 if( p->rc==SQLITE_NOMEM ){ 593 if( p->rc==SQLITE_NOMEM ){
579 /* This happens if a malloc() inside a call to sqlite3_column_text() or 594 /* This happens if a malloc() inside a call to sqlite3_column_text() or
580 ** sqlite3_column_text16() failed. */ 595 ** sqlite3_column_text16() failed. */
581 goto no_mem; 596 goto no_mem;
582 } 597 }
583 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); 598 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
584 assert( p->bIsReader || p->readOnly!=0 ); 599 assert( p->bIsReader || p->readOnly!=0 );
585 p->rc = SQLITE_OK;
586 p->iCurrentTime = 0; 600 p->iCurrentTime = 0;
587 assert( p->explain==0 ); 601 assert( p->explain==0 );
588 p->pResultSet = 0; 602 p->pResultSet = 0;
589 db->busyHandler.nBusy = 0; 603 db->busyHandler.nBusy = 0;
590 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; 604 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
591 sqlite3VdbeIOTraceSql(p); 605 sqlite3VdbeIOTraceSql(p);
592 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 606 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
593 if( db->xProgress ){ 607 if( db->xProgress ){
594 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; 608 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
595 assert( 0 < db->nProgressOps ); 609 assert( 0 < db->nProgressOps );
(...skipping 20 matching lines...) Expand all
616 if( once ) printf("VDBE Query Plan:\n"); 630 if( once ) printf("VDBE Query Plan:\n");
617 printf("%s\n", aOp[i].p4.z); 631 printf("%s\n", aOp[i].p4.z);
618 once = 0; 632 once = 0;
619 } 633 }
620 } 634 }
621 } 635 }
622 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); 636 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
623 } 637 }
624 sqlite3EndBenignMalloc(); 638 sqlite3EndBenignMalloc();
625 #endif 639 #endif
626 for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){ 640 for(pOp=&aOp[p->pc]; 1; pOp++){
641 /* Errors are detected by individual opcodes, with an immediate
642 ** jumps to abort_due_to_error. */
643 assert( rc==SQLITE_OK );
644
627 assert( pOp>=aOp && pOp<&aOp[p->nOp]); 645 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
628 if( db->mallocFailed ) goto no_mem;
629 #ifdef VDBE_PROFILE 646 #ifdef VDBE_PROFILE
630 start = sqlite3Hwtime(); 647 start = sqlite3Hwtime();
631 #endif 648 #endif
632 nVmStep++; 649 nVmStep++;
633 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 650 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
634 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; 651 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
635 #endif 652 #endif
636 653
637 /* Only allow tracing if SQLITE_DEBUG is defined. 654 /* Only allow tracing if SQLITE_DEBUG is defined.
638 */ 655 */
(...skipping 11 matching lines...) Expand all
650 if( sqlite3_interrupt_count>0 ){ 667 if( sqlite3_interrupt_count>0 ){
651 sqlite3_interrupt_count--; 668 sqlite3_interrupt_count--;
652 if( sqlite3_interrupt_count==0 ){ 669 if( sqlite3_interrupt_count==0 ){
653 sqlite3_interrupt(db); 670 sqlite3_interrupt(db);
654 } 671 }
655 } 672 }
656 #endif 673 #endif
657 674
658 /* Sanity checking on other operands */ 675 /* Sanity checking on other operands */
659 #ifdef SQLITE_DEBUG 676 #ifdef SQLITE_DEBUG
660 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); 677 {
661 if( (pOp->opflags & OPFLG_IN1)!=0 ){ 678 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
662 assert( pOp->p1>0 ); 679 if( (opProperty & OPFLG_IN1)!=0 ){
663 assert( pOp->p1<=(p->nMem-p->nCursor) ); 680 assert( pOp->p1>0 );
664 assert( memIsValid(&aMem[pOp->p1]) ); 681 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
665 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); 682 assert( memIsValid(&aMem[pOp->p1]) );
666 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); 683 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
667 } 684 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
668 if( (pOp->opflags & OPFLG_IN2)!=0 ){ 685 }
669 assert( pOp->p2>0 ); 686 if( (opProperty & OPFLG_IN2)!=0 ){
670 assert( pOp->p2<=(p->nMem-p->nCursor) ); 687 assert( pOp->p2>0 );
671 assert( memIsValid(&aMem[pOp->p2]) ); 688 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
672 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); 689 assert( memIsValid(&aMem[pOp->p2]) );
673 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); 690 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
674 } 691 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
675 if( (pOp->opflags & OPFLG_IN3)!=0 ){ 692 }
676 assert( pOp->p3>0 ); 693 if( (opProperty & OPFLG_IN3)!=0 ){
677 assert( pOp->p3<=(p->nMem-p->nCursor) ); 694 assert( pOp->p3>0 );
678 assert( memIsValid(&aMem[pOp->p3]) ); 695 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
679 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); 696 assert( memIsValid(&aMem[pOp->p3]) );
680 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); 697 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
681 } 698 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
682 if( (pOp->opflags & OPFLG_OUT2)!=0 ){ 699 }
683 assert( pOp->p2>0 ); 700 if( (opProperty & OPFLG_OUT2)!=0 ){
684 assert( pOp->p2<=(p->nMem-p->nCursor) ); 701 assert( pOp->p2>0 );
685 memAboutToChange(p, &aMem[pOp->p2]); 702 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
686 } 703 memAboutToChange(p, &aMem[pOp->p2]);
687 if( (pOp->opflags & OPFLG_OUT3)!=0 ){ 704 }
688 assert( pOp->p3>0 ); 705 if( (opProperty & OPFLG_OUT3)!=0 ){
689 assert( pOp->p3<=(p->nMem-p->nCursor) ); 706 assert( pOp->p3>0 );
690 memAboutToChange(p, &aMem[pOp->p3]); 707 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
708 memAboutToChange(p, &aMem[pOp->p3]);
709 }
691 } 710 }
692 #endif 711 #endif
693 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 712 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
694 pOrigOp = pOp; 713 pOrigOp = pOp;
695 #endif 714 #endif
696 715
697 switch( pOp->opcode ){ 716 switch( pOp->opcode ){
698 717
699 /***************************************************************************** 718 /*****************************************************************************
700 ** What follows is a massive switch statement where each case implements a 719 ** What follows is a massive switch statement where each case implements a
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 ** of VDBE ops have been executed (either since this invocation of 783 ** of VDBE ops have been executed (either since this invocation of
765 ** sqlite3VdbeExec() or since last time the progress callback was called). 784 ** sqlite3VdbeExec() or since last time the progress callback was called).
766 ** If the progress callback returns non-zero, exit the virtual machine with 785 ** If the progress callback returns non-zero, exit the virtual machine with
767 ** a return code SQLITE_ABORT. 786 ** a return code SQLITE_ABORT.
768 */ 787 */
769 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ 788 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
770 assert( db->nProgressOps!=0 ); 789 assert( db->nProgressOps!=0 );
771 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); 790 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
772 if( db->xProgress(db->pProgressArg) ){ 791 if( db->xProgress(db->pProgressArg) ){
773 rc = SQLITE_INTERRUPT; 792 rc = SQLITE_INTERRUPT;
774 goto vdbe_error_halt; 793 goto abort_due_to_error;
775 } 794 }
776 } 795 }
777 #endif 796 #endif
778 797
779 break; 798 break;
780 } 799 }
781 800
782 /* Opcode: Gosub P1 P2 * * * 801 /* Opcode: Gosub P1 P2 * * *
783 ** 802 **
784 ** Write the current address onto register P1 803 ** Write the current address onto register P1
785 ** and then jump to address P2. 804 ** and then jump to address P2.
786 */ 805 */
787 case OP_Gosub: { /* jump */ 806 case OP_Gosub: { /* jump */
788 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); 807 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
789 pIn1 = &aMem[pOp->p1]; 808 pIn1 = &aMem[pOp->p1];
790 assert( VdbeMemDynamic(pIn1)==0 ); 809 assert( VdbeMemDynamic(pIn1)==0 );
791 memAboutToChange(p, pIn1); 810 memAboutToChange(p, pIn1);
792 pIn1->flags = MEM_Int; 811 pIn1->flags = MEM_Int;
793 pIn1->u.i = (int)(pOp-aOp); 812 pIn1->u.i = (int)(pOp-aOp);
794 REGISTER_TRACE(pOp->p1, pIn1); 813 REGISTER_TRACE(pOp->p1, pIn1);
795 814
796 /* Most jump operations do a goto to this spot in order to update 815 /* Most jump operations do a goto to this spot in order to update
797 ** the pOp pointer. */ 816 ** the pOp pointer. */
798 jump_to_p2: 817 jump_to_p2:
(...skipping 19 matching lines...) Expand all
818 ** Set up register P1 so that it will Yield to the coroutine 837 ** Set up register P1 so that it will Yield to the coroutine
819 ** located at address P3. 838 ** located at address P3.
820 ** 839 **
821 ** If P2!=0 then the coroutine implementation immediately follows 840 ** If P2!=0 then the coroutine implementation immediately follows
822 ** this opcode. So jump over the coroutine implementation to 841 ** this opcode. So jump over the coroutine implementation to
823 ** address P2. 842 ** address P2.
824 ** 843 **
825 ** See also: EndCoroutine 844 ** See also: EndCoroutine
826 */ 845 */
827 case OP_InitCoroutine: { /* jump */ 846 case OP_InitCoroutine: { /* jump */
828 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); 847 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
829 assert( pOp->p2>=0 && pOp->p2<p->nOp ); 848 assert( pOp->p2>=0 && pOp->p2<p->nOp );
830 assert( pOp->p3>=0 && pOp->p3<p->nOp ); 849 assert( pOp->p3>=0 && pOp->p3<p->nOp );
831 pOut = &aMem[pOp->p1]; 850 pOut = &aMem[pOp->p1];
832 assert( !VdbeMemDynamic(pOut) ); 851 assert( !VdbeMemDynamic(pOut) );
833 pOut->u.i = pOp->p3 - 1; 852 pOut->u.i = pOp->p3 - 1;
834 pOut->flags = MEM_Int; 853 pOut->flags = MEM_Int;
835 if( pOp->p2 ) goto jump_to_p2; 854 if( pOp->p2 ) goto jump_to_p2;
836 break; 855 break;
837 } 856 }
838 857
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 assert( VdbeMemDynamic(pIn1)==0 ); 895 assert( VdbeMemDynamic(pIn1)==0 );
877 pIn1->flags = MEM_Int; 896 pIn1->flags = MEM_Int;
878 pcDest = (int)pIn1->u.i; 897 pcDest = (int)pIn1->u.i;
879 pIn1->u.i = (int)(pOp - aOp); 898 pIn1->u.i = (int)(pOp - aOp);
880 REGISTER_TRACE(pOp->p1, pIn1); 899 REGISTER_TRACE(pOp->p1, pIn1);
881 pOp = &aOp[pcDest]; 900 pOp = &aOp[pcDest];
882 break; 901 break;
883 } 902 }
884 903
885 /* Opcode: HaltIfNull P1 P2 P3 P4 P5 904 /* Opcode: HaltIfNull P1 P2 P3 P4 P5
886 ** Synopsis: if r[P3]=null halt 905 ** Synopsis: if r[P3]=null halt
887 ** 906 **
888 ** Check the value in register P3. If it is NULL then Halt using 907 ** Check the value in register P3. If it is NULL then Halt using
889 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the 908 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
890 ** value in register P3 is not NULL, then this routine is a no-op. 909 ** value in register P3 is not NULL, then this routine is a no-op.
891 ** The P5 parameter should be 1. 910 ** The P5 parameter should be 1.
892 */ 911 */
893 case OP_HaltIfNull: { /* in3 */ 912 case OP_HaltIfNull: { /* in3 */
894 pIn3 = &aMem[pOp->p3]; 913 pIn3 = &aMem[pOp->p3];
895 if( (pIn3->flags & MEM_Null)==0 ) break; 914 if( (pIn3->flags & MEM_Null)==0 ) break;
896 /* Fall through into OP_Halt */ 915 /* Fall through into OP_Halt */
(...skipping 23 matching lines...) Expand all
920 ** 4: FOREIGN KEY constraint failed: P4 939 ** 4: FOREIGN KEY constraint failed: P4
921 ** 940 **
922 ** If P5 is not zero and P4 is NULL, then everything after the ":" is 941 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
923 ** omitted. 942 ** omitted.
924 ** 943 **
925 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of 944 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
926 ** every program. So a jump past the last instruction of the program 945 ** every program. So a jump past the last instruction of the program
927 ** is the same as executing Halt. 946 ** is the same as executing Halt.
928 */ 947 */
929 case OP_Halt: { 948 case OP_Halt: {
930 const char *zType;
931 const char *zLogFmt;
932 VdbeFrame *pFrame; 949 VdbeFrame *pFrame;
933 int pcx; 950 int pcx;
934 951
935 pcx = (int)(pOp - aOp); 952 pcx = (int)(pOp - aOp);
936 if( pOp->p1==SQLITE_OK && p->pFrame ){ 953 if( pOp->p1==SQLITE_OK && p->pFrame ){
937 /* Halt the sub-program. Return control to the parent frame. */ 954 /* Halt the sub-program. Return control to the parent frame. */
938 pFrame = p->pFrame; 955 pFrame = p->pFrame;
939 p->pFrame = pFrame->pParent; 956 p->pFrame = pFrame->pParent;
940 p->nFrame--; 957 p->nFrame--;
941 sqlite3VdbeSetChanges(db, p->nChange); 958 sqlite3VdbeSetChanges(db, p->nChange);
942 pcx = sqlite3VdbeFrameRestore(pFrame); 959 pcx = sqlite3VdbeFrameRestore(pFrame);
943 lastRowid = db->lastRowid;
944 if( pOp->p2==OE_Ignore ){ 960 if( pOp->p2==OE_Ignore ){
945 /* Instruction pcx is the OP_Program that invoked the sub-program 961 /* Instruction pcx is the OP_Program that invoked the sub-program
946 ** currently being halted. If the p2 instruction of this OP_Halt 962 ** currently being halted. If the p2 instruction of this OP_Halt
947 ** instruction is set to OE_Ignore, then the sub-program is throwing 963 ** instruction is set to OE_Ignore, then the sub-program is throwing
948 ** an IGNORE exception. In this case jump to the address specified 964 ** an IGNORE exception. In this case jump to the address specified
949 ** as the p2 of the calling OP_Program. */ 965 ** as the p2 of the calling OP_Program. */
950 pcx = p->aOp[pcx].p2-1; 966 pcx = p->aOp[pcx].p2-1;
951 } 967 }
952 aOp = p->aOp; 968 aOp = p->aOp;
953 aMem = p->aMem; 969 aMem = p->aMem;
954 pOp = &aOp[pcx]; 970 pOp = &aOp[pcx];
955 break; 971 break;
956 } 972 }
957 p->rc = pOp->p1; 973 p->rc = pOp->p1;
958 p->errorAction = (u8)pOp->p2; 974 p->errorAction = (u8)pOp->p2;
959 p->pc = pcx; 975 p->pc = pcx;
976 assert( pOp->p5<=4 );
960 if( p->rc ){ 977 if( p->rc ){
961 if( pOp->p5 ){ 978 if( pOp->p5 ){
962 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", 979 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
963 "FOREIGN KEY" }; 980 "FOREIGN KEY" };
964 assert( pOp->p5>=1 && pOp->p5<=4 );
965 testcase( pOp->p5==1 ); 981 testcase( pOp->p5==1 );
966 testcase( pOp->p5==2 ); 982 testcase( pOp->p5==2 );
967 testcase( pOp->p5==3 ); 983 testcase( pOp->p5==3 );
968 testcase( pOp->p5==4 ); 984 testcase( pOp->p5==4 );
969 zType = azType[pOp->p5-1]; 985 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
986 if( pOp->p4.z ){
987 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
988 }
970 }else{ 989 }else{
971 zType = 0; 990 sqlite3VdbeError(p, "%s", pOp->p4.z);
972 } 991 }
973 assert( zType!=0 || pOp->p4.z!=0 ); 992 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
974 zLogFmt = "abort at %d in [%s]: %s";
975 if( zType && pOp->p4.z ){
976 sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
977 }else if( pOp->p4.z ){
978 sqlite3VdbeError(p, "%s", pOp->p4.z);
979 }else{
980 sqlite3VdbeError(p, "%s constraint failed", zType);
981 }
982 sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
983 } 993 }
984 rc = sqlite3VdbeHalt(p); 994 rc = sqlite3VdbeHalt(p);
985 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); 995 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
986 if( rc==SQLITE_BUSY ){ 996 if( rc==SQLITE_BUSY ){
987 p->rc = rc = SQLITE_BUSY; 997 p->rc = SQLITE_BUSY;
988 }else{ 998 }else{
989 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); 999 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
990 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); 1000 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
991 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; 1001 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
992 } 1002 }
993 goto vdbe_return; 1003 goto vdbe_return;
994 } 1004 }
995 1005
996 /* Opcode: Integer P1 P2 * * * 1006 /* Opcode: Integer P1 P2 * * *
997 ** Synopsis: r[P2]=P1 1007 ** Synopsis: r[P2]=P1
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 */ 1053 */
1044 case OP_String8: { /* same as TK_STRING, out2 */ 1054 case OP_String8: { /* same as TK_STRING, out2 */
1045 assert( pOp->p4.z!=0 ); 1055 assert( pOp->p4.z!=0 );
1046 pOut = out2Prerelease(p, pOp); 1056 pOut = out2Prerelease(p, pOp);
1047 pOp->opcode = OP_String; 1057 pOp->opcode = OP_String;
1048 pOp->p1 = sqlite3Strlen30(pOp->p4.z); 1058 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
1049 1059
1050 #ifndef SQLITE_OMIT_UTF16 1060 #ifndef SQLITE_OMIT_UTF16
1051 if( encoding!=SQLITE_UTF8 ){ 1061 if( encoding!=SQLITE_UTF8 ){
1052 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); 1062 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
1053 if( rc==SQLITE_TOOBIG ) goto too_big; 1063 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
1054 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; 1064 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
1055 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); 1065 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
1056 assert( VdbeMemDynamic(pOut)==0 ); 1066 assert( VdbeMemDynamic(pOut)==0 );
1057 pOut->szMalloc = 0; 1067 pOut->szMalloc = 0;
1058 pOut->flags |= MEM_Static; 1068 pOut->flags |= MEM_Static;
1059 if( pOp->p4type==P4_DYNAMIC ){ 1069 if( pOp->p4type==P4_DYNAMIC ){
1060 sqlite3DbFree(db, pOp->p4.z); 1070 sqlite3DbFree(db, pOp->p4.z);
1061 } 1071 }
1062 pOp->p4type = P4_DYNAMIC; 1072 pOp->p4type = P4_DYNAMIC;
1063 pOp->p4.z = pOut->z; 1073 pOp->p4.z = pOut->z;
1064 pOp->p1 = pOut->n; 1074 pOp->p1 = pOut->n;
1065 } 1075 }
1076 testcase( rc==SQLITE_TOOBIG );
1066 #endif 1077 #endif
1067 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1078 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1068 goto too_big; 1079 goto too_big;
1069 } 1080 }
1081 assert( rc==SQLITE_OK );
1070 /* Fall through to the next case, OP_String */ 1082 /* Fall through to the next case, OP_String */
1071 } 1083 }
1072 1084
1073 /* Opcode: String P1 P2 P3 P4 P5 1085 /* Opcode: String P1 P2 P3 P4 P5
1074 ** Synopsis: r[P2]='P4' (len=P1) 1086 ** Synopsis: r[P2]='P4' (len=P1)
1075 ** 1087 **
1076 ** The string value P4 of length P1 (bytes) is stored in register P2. 1088 ** The string value P4 of length P1 (bytes) is stored in register P2.
1077 ** 1089 **
1078 ** If P5!=0 and the content of register P3 is greater than zero, then 1090 ** If P3 is not zero and the content of register P3 is equal to P5, then
1079 ** the datatype of the register P2 is converted to BLOB. The content is 1091 ** the datatype of the register P2 is converted to BLOB. The content is
1080 ** the same sequence of bytes, it is merely interpreted as a BLOB instead 1092 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
1081 ** of a string, as if it had been CAST. 1093 ** of a string, as if it had been CAST. In other words:
1094 **
1095 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
1082 */ 1096 */
1083 case OP_String: { /* out2 */ 1097 case OP_String: { /* out2 */
1084 assert( pOp->p4.z!=0 ); 1098 assert( pOp->p4.z!=0 );
1085 pOut = out2Prerelease(p, pOp); 1099 pOut = out2Prerelease(p, pOp);
1086 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 1100 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1087 pOut->z = pOp->p4.z; 1101 pOut->z = pOp->p4.z;
1088 pOut->n = pOp->p1; 1102 pOut->n = pOp->p1;
1089 pOut->enc = encoding; 1103 pOut->enc = encoding;
1090 UPDATE_MAX_BLOBSIZE(pOut); 1104 UPDATE_MAX_BLOBSIZE(pOut);
1091 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS 1105 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
1092 if( pOp->p5 ){ 1106 if( pOp->p3>0 ){
1093 assert( pOp->p3>0 ); 1107 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
1094 assert( pOp->p3<=(p->nMem-p->nCursor) );
1095 pIn3 = &aMem[pOp->p3]; 1108 pIn3 = &aMem[pOp->p3];
1096 assert( pIn3->flags & MEM_Int ); 1109 assert( pIn3->flags & MEM_Int );
1097 if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; 1110 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
1098 } 1111 }
1099 #endif 1112 #endif
1100 break; 1113 break;
1101 } 1114 }
1102 1115
1103 /* Opcode: Null P1 P2 P3 * * 1116 /* Opcode: Null P1 P2 P3 * *
1104 ** Synopsis: r[P2..P3]=NULL 1117 ** Synopsis: r[P2..P3]=NULL
1105 ** 1118 **
1106 ** Write a NULL into registers P2. If P3 greater than P2, then also write 1119 ** Write a NULL into registers P2. If P3 greater than P2, then also write
1107 ** NULL into register P3 and every register in between P2 and P3. If P3 1120 ** NULL into register P3 and every register in between P2 and P3. If P3
1108 ** is less than P2 (typically P3 is zero) then only register P2 is 1121 ** is less than P2 (typically P3 is zero) then only register P2 is
1109 ** set to NULL. 1122 ** set to NULL.
1110 ** 1123 **
1111 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that 1124 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1112 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on 1125 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1113 ** OP_Ne or OP_Eq. 1126 ** OP_Ne or OP_Eq.
1114 */ 1127 */
1115 case OP_Null: { /* out2 */ 1128 case OP_Null: { /* out2 */
1116 int cnt; 1129 int cnt;
1117 u16 nullFlag; 1130 u16 nullFlag;
1118 pOut = out2Prerelease(p, pOp); 1131 pOut = out2Prerelease(p, pOp);
1119 cnt = pOp->p3-pOp->p2; 1132 cnt = pOp->p3-pOp->p2;
1120 assert( pOp->p3<=(p->nMem-p->nCursor) ); 1133 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
1121 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; 1134 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
1135 pOut->n = 0;
1122 while( cnt>0 ){ 1136 while( cnt>0 ){
1123 pOut++; 1137 pOut++;
1124 memAboutToChange(p, pOut); 1138 memAboutToChange(p, pOut);
1125 sqlite3VdbeMemSetNull(pOut); 1139 sqlite3VdbeMemSetNull(pOut);
1126 pOut->flags = nullFlag; 1140 pOut->flags = nullFlag;
1141 pOut->n = 0;
1127 cnt--; 1142 cnt--;
1128 } 1143 }
1129 break; 1144 break;
1130 } 1145 }
1131 1146
1132 /* Opcode: SoftNull P1 * * * * 1147 /* Opcode: SoftNull P1 * * * *
1133 ** Synopsis: r[P1]=NULL 1148 ** Synopsis: r[P1]=NULL
1134 ** 1149 **
1135 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord 1150 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1136 ** instruction, but do not free any string or blob memory associated with 1151 ** instruction, but do not free any string or blob memory associated with
1137 ** the register, so that if the value was a string or blob that was 1152 ** the register, so that if the value was a string or blob that was
1138 ** previously copied using OP_SCopy, the copies will continue to be valid. 1153 ** previously copied using OP_SCopy, the copies will continue to be valid.
1139 */ 1154 */
1140 case OP_SoftNull: { 1155 case OP_SoftNull: {
1141 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); 1156 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
1142 pOut = &aMem[pOp->p1]; 1157 pOut = &aMem[pOp->p1];
1143 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; 1158 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1144 break; 1159 break;
1145 } 1160 }
1146 1161
1147 /* Opcode: Blob P1 P2 * P4 * 1162 /* Opcode: Blob P1 P2 * P4 *
1148 ** Synopsis: r[P2]=P4 (len=P1) 1163 ** Synopsis: r[P2]=P4 (len=P1)
1149 ** 1164 **
1150 ** P4 points to a blob of data P1 bytes long. Store this 1165 ** P4 points to a blob of data P1 bytes long. Store this
1151 ** blob in register P2. 1166 ** blob in register P2.
(...skipping 12 matching lines...) Expand all
1164 ** 1179 **
1165 ** Transfer the values of bound parameter P1 into register P2 1180 ** Transfer the values of bound parameter P1 into register P2
1166 ** 1181 **
1167 ** If the parameter is named, then its name appears in P4. 1182 ** If the parameter is named, then its name appears in P4.
1168 ** The P4 value is used by sqlite3_bind_parameter_name(). 1183 ** The P4 value is used by sqlite3_bind_parameter_name().
1169 */ 1184 */
1170 case OP_Variable: { /* out2 */ 1185 case OP_Variable: { /* out2 */
1171 Mem *pVar; /* Value being transferred */ 1186 Mem *pVar; /* Value being transferred */
1172 1187
1173 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 1188 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1174 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); 1189 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
1175 pVar = &p->aVar[pOp->p1 - 1]; 1190 pVar = &p->aVar[pOp->p1 - 1];
1176 if( sqlite3VdbeMemTooBig(pVar) ){ 1191 if( sqlite3VdbeMemTooBig(pVar) ){
1177 goto too_big; 1192 goto too_big;
1178 } 1193 }
1179 pOut = out2Prerelease(p, pOp); 1194 pOut = &aMem[pOp->p2];
1180 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); 1195 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1181 UPDATE_MAX_BLOBSIZE(pOut); 1196 UPDATE_MAX_BLOBSIZE(pOut);
1182 break; 1197 break;
1183 } 1198 }
1184 1199
1185 /* Opcode: Move P1 P2 P3 * * 1200 /* Opcode: Move P1 P2 P3 * *
1186 ** Synopsis: r[P2@P3]=r[P1@P3] 1201 ** Synopsis: r[P2@P3]=r[P1@P3]
1187 ** 1202 **
1188 ** Move the P3 values in register P1..P1+P3-1 over into 1203 ** Move the P3 values in register P1..P1+P3-1 over into
1189 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are 1204 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
1190 ** left holding a NULL. It is an error for register ranges 1205 ** left holding a NULL. It is an error for register ranges
1191 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error 1206 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1192 ** for P3 to be less than 1. 1207 ** for P3 to be less than 1.
1193 */ 1208 */
1194 case OP_Move: { 1209 case OP_Move: {
1195 int n; /* Number of registers left to copy */ 1210 int n; /* Number of registers left to copy */
1196 int p1; /* Register to copy from */ 1211 int p1; /* Register to copy from */
1197 int p2; /* Register to copy to */ 1212 int p2; /* Register to copy to */
1198 1213
1199 n = pOp->p3; 1214 n = pOp->p3;
1200 p1 = pOp->p1; 1215 p1 = pOp->p1;
1201 p2 = pOp->p2; 1216 p2 = pOp->p2;
1202 assert( n>0 && p1>0 && p2>0 ); 1217 assert( n>0 && p1>0 && p2>0 );
1203 assert( p1+n<=p2 || p2+n<=p1 ); 1218 assert( p1+n<=p2 || p2+n<=p1 );
1204 1219
1205 pIn1 = &aMem[p1]; 1220 pIn1 = &aMem[p1];
1206 pOut = &aMem[p2]; 1221 pOut = &aMem[p2];
1207 do{ 1222 do{
1208 assert( pOut<=&aMem[(p->nMem-p->nCursor)] ); 1223 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1209 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] ); 1224 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
1210 assert( memIsValid(pIn1) ); 1225 assert( memIsValid(pIn1) );
1211 memAboutToChange(p, pOut); 1226 memAboutToChange(p, pOut);
1212 sqlite3VdbeMemMove(pOut, pIn1); 1227 sqlite3VdbeMemMove(pOut, pIn1);
1213 #ifdef SQLITE_DEBUG 1228 #ifdef SQLITE_DEBUG
1214 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ 1229 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
1215 pOut->pScopyFrom += pOp->p2 - p1; 1230 pOut->pScopyFrom += pOp->p2 - p1;
1216 } 1231 }
1217 #endif 1232 #endif
1218 Deephemeralize(pOut); 1233 Deephemeralize(pOut);
1219 REGISTER_TRACE(p2++, pOut); 1234 REGISTER_TRACE(p2++, pOut);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1286 */ 1301 */
1287 case OP_IntCopy: { /* out2 */ 1302 case OP_IntCopy: { /* out2 */
1288 pIn1 = &aMem[pOp->p1]; 1303 pIn1 = &aMem[pOp->p1];
1289 assert( (pIn1->flags & MEM_Int)!=0 ); 1304 assert( (pIn1->flags & MEM_Int)!=0 );
1290 pOut = &aMem[pOp->p2]; 1305 pOut = &aMem[pOp->p2];
1291 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); 1306 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1292 break; 1307 break;
1293 } 1308 }
1294 1309
1295 /* Opcode: ResultRow P1 P2 * * * 1310 /* Opcode: ResultRow P1 P2 * * *
1296 ** Synopsis: output=r[P1@P2] 1311 ** Synopsis: output=r[P1@P2]
1297 ** 1312 **
1298 ** The registers P1 through P1+P2-1 contain a single row of 1313 ** The registers P1 through P1+P2-1 contain a single row of
1299 ** results. This opcode causes the sqlite3_step() call to terminate 1314 ** results. This opcode causes the sqlite3_step() call to terminate
1300 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt 1315 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1301 ** structure to provide access to the r(P1)..r(P1+P2-1) values as 1316 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
1302 ** the result row. 1317 ** the result row.
1303 */ 1318 */
1304 case OP_ResultRow: { 1319 case OP_ResultRow: {
1305 Mem *pMem; 1320 Mem *pMem;
1306 int i; 1321 int i;
1307 assert( p->nResColumn==pOp->p2 ); 1322 assert( p->nResColumn==pOp->p2 );
1308 assert( pOp->p1>0 ); 1323 assert( pOp->p1>0 );
1309 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 ); 1324 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
1310 1325
1311 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1326 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1312 /* Run the progress counter just before returning. 1327 /* Run the progress counter just before returning.
1313 */ 1328 */
1314 if( db->xProgress!=0 1329 if( db->xProgress!=0
1315 && nVmStep>=nProgressLimit 1330 && nVmStep>=nProgressLimit
1316 && db->xProgress(db->pProgressArg)!=0 1331 && db->xProgress(db->pProgressArg)!=0
1317 ){ 1332 ){
1318 rc = SQLITE_INTERRUPT; 1333 rc = SQLITE_INTERRUPT;
1319 goto vdbe_error_halt; 1334 goto abort_due_to_error;
1320 } 1335 }
1321 #endif 1336 #endif
1322 1337
1323 /* If this statement has violated immediate foreign key constraints, do 1338 /* If this statement has violated immediate foreign key constraints, do
1324 ** not return the number of rows modified. And do not RELEASE the statement 1339 ** not return the number of rows modified. And do not RELEASE the statement
1325 ** transaction. It needs to be rolled back. */ 1340 ** transaction. It needs to be rolled back. */
1326 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ 1341 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1327 assert( db->flags&SQLITE_CountRows ); 1342 assert( db->flags&SQLITE_CountRows );
1328 assert( p->usesStmtJournal ); 1343 assert( p->usesStmtJournal );
1329 break; 1344 goto abort_due_to_error;
1330 } 1345 }
1331 1346
1332 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then 1347 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1333 ** DML statements invoke this opcode to return the number of rows 1348 ** DML statements invoke this opcode to return the number of rows
1334 ** modified to the user. This is the only way that a VM that 1349 ** modified to the user. This is the only way that a VM that
1335 ** opens a statement transaction may invoke this opcode. 1350 ** opens a statement transaction may invoke this opcode.
1336 ** 1351 **
1337 ** In case this is such a statement, close any statement transaction 1352 ** In case this is such a statement, close any statement transaction
1338 ** opened by this VM before returning control to the user. This is to 1353 ** opened by this VM before returning control to the user. This is to
1339 ** ensure that statement-transactions are always nested, not overlapping. 1354 ** ensure that statement-transactions are always nested, not overlapping.
1340 ** If the open statement-transaction is not closed here, then the user 1355 ** If the open statement-transaction is not closed here, then the user
1341 ** may step another VM that opens its own statement transaction. This 1356 ** may step another VM that opens its own statement transaction. This
1342 ** may lead to overlapping statement transactions. 1357 ** may lead to overlapping statement transactions.
1343 ** 1358 **
1344 ** The statement transaction is never a top-level transaction. Hence 1359 ** The statement transaction is never a top-level transaction. Hence
1345 ** the RELEASE call below can never fail. 1360 ** the RELEASE call below can never fail.
1346 */ 1361 */
1347 assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); 1362 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
1348 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); 1363 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1349 if( NEVER(rc!=SQLITE_OK) ){ 1364 assert( rc==SQLITE_OK );
1350 break;
1351 }
1352 1365
1353 /* Invalidate all ephemeral cursor row caches */ 1366 /* Invalidate all ephemeral cursor row caches */
1354 p->cacheCtr = (p->cacheCtr + 2)|1; 1367 p->cacheCtr = (p->cacheCtr + 2)|1;
1355 1368
1356 /* Make sure the results of the current row are \000 terminated 1369 /* Make sure the results of the current row are \000 terminated
1357 ** and have an assigned type. The results are de-ephemeralized as 1370 ** and have an assigned type. The results are de-ephemeralized as
1358 ** a side effect. 1371 ** a side effect.
1359 */ 1372 */
1360 pMem = p->pResultSet = &aMem[pOp->p1]; 1373 pMem = p->pResultSet = &aMem[pOp->p1];
1361 for(i=0; i<pOp->p2; i++){ 1374 for(i=0; i<pOp->p2; i++){
1362 assert( memIsValid(&pMem[i]) ); 1375 assert( memIsValid(&pMem[i]) );
1363 Deephemeralize(&pMem[i]); 1376 Deephemeralize(&pMem[i]);
1364 assert( (pMem[i].flags & MEM_Ephem)==0 1377 assert( (pMem[i].flags & MEM_Ephem)==0
1365 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); 1378 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
1366 sqlite3VdbeMemNulTerminate(&pMem[i]); 1379 sqlite3VdbeMemNulTerminate(&pMem[i]);
1367 REGISTER_TRACE(pOp->p1+i, &pMem[i]); 1380 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
1368 } 1381 }
1369 if( db->mallocFailed ) goto no_mem; 1382 if( db->mallocFailed ) goto no_mem;
1370 1383
1384 if( db->mTrace & SQLITE_TRACE_ROW ){
1385 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1386 }
1387
1371 /* Return SQLITE_ROW 1388 /* Return SQLITE_ROW
1372 */ 1389 */
1373 p->pc = (int)(pOp - aOp) + 1; 1390 p->pc = (int)(pOp - aOp) + 1;
1374 rc = SQLITE_ROW; 1391 rc = SQLITE_ROW;
1375 goto vdbe_return; 1392 goto vdbe_return;
1376 } 1393 }
1377 1394
1378 /* Opcode: Concat P1 P2 P3 * * 1395 /* Opcode: Concat P1 P2 P3 * *
1379 ** Synopsis: r[P3]=r[P2]+r[P1] 1396 ** Synopsis: r[P3]=r[P2]+r[P1]
1380 ** 1397 **
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1417 pOut->z[nByte]=0; 1434 pOut->z[nByte]=0;
1418 pOut->z[nByte+1] = 0; 1435 pOut->z[nByte+1] = 0;
1419 pOut->flags |= MEM_Term; 1436 pOut->flags |= MEM_Term;
1420 pOut->n = (int)nByte; 1437 pOut->n = (int)nByte;
1421 pOut->enc = encoding; 1438 pOut->enc = encoding;
1422 UPDATE_MAX_BLOBSIZE(pOut); 1439 UPDATE_MAX_BLOBSIZE(pOut);
1423 break; 1440 break;
1424 } 1441 }
1425 1442
1426 /* Opcode: Add P1 P2 P3 * * 1443 /* Opcode: Add P1 P2 P3 * *
1427 ** Synopsis: r[P3]=r[P1]+r[P2] 1444 ** Synopsis: r[P3]=r[P1]+r[P2]
1428 ** 1445 **
1429 ** Add the value in register P1 to the value in register P2 1446 ** Add the value in register P1 to the value in register P2
1430 ** and store the result in register P3. 1447 ** and store the result in register P3.
1431 ** If either input is NULL, the result is NULL. 1448 ** If either input is NULL, the result is NULL.
1432 */ 1449 */
1433 /* Opcode: Multiply P1 P2 P3 * * 1450 /* Opcode: Multiply P1 P2 P3 * *
1434 ** Synopsis: r[P3]=r[P1]*r[P2] 1451 ** Synopsis: r[P3]=r[P1]*r[P2]
1435 ** 1452 **
1436 ** 1453 **
1437 ** Multiply the value in register P1 by the value in register P2 1454 ** Multiply the value in register P1 by the value in register P2
1438 ** and store the result in register P3. 1455 ** and store the result in register P3.
1439 ** If either input is NULL, the result is NULL. 1456 ** If either input is NULL, the result is NULL.
1440 */ 1457 */
1441 /* Opcode: Subtract P1 P2 P3 * * 1458 /* Opcode: Subtract P1 P2 P3 * *
1442 ** Synopsis: r[P3]=r[P2]-r[P1] 1459 ** Synopsis: r[P3]=r[P2]-r[P1]
1443 ** 1460 **
1444 ** Subtract the value in register P1 from the value in register P2 1461 ** Subtract the value in register P1 from the value in register P2
1445 ** and store the result in register P3. 1462 ** and store the result in register P3.
1446 ** If either input is NULL, the result is NULL. 1463 ** If either input is NULL, the result is NULL.
1447 */ 1464 */
1448 /* Opcode: Divide P1 P2 P3 * * 1465 /* Opcode: Divide P1 P2 P3 * *
1449 ** Synopsis: r[P3]=r[P2]/r[P1] 1466 ** Synopsis: r[P3]=r[P2]/r[P1]
1450 ** 1467 **
1451 ** Divide the value in register P1 by the value in register P2 1468 ** Divide the value in register P1 by the value in register P2
1452 ** and store the result in register P3 (P3=P2/P1). If the value in 1469 ** and store the result in register P3 (P3=P2/P1). If the value in
1453 ** register P1 is zero, then the result is NULL. If either input is 1470 ** register P1 is zero, then the result is NULL. If either input is
1454 ** NULL, the result is NULL. 1471 ** NULL, the result is NULL.
1455 */ 1472 */
1456 /* Opcode: Remainder P1 P2 P3 * * 1473 /* Opcode: Remainder P1 P2 P3 * *
1457 ** Synopsis: r[P3]=r[P2]%r[P1] 1474 ** Synopsis: r[P3]=r[P2]%r[P1]
1458 ** 1475 **
1459 ** Compute the remainder after integer register P2 is divided by 1476 ** Compute the remainder after integer register P2 is divided by
1460 ** register P1 and store the result in register P3. 1477 ** register P1 and store the result in register P3.
1461 ** If the value in register P1 is zero the result is NULL. 1478 ** If the value in register P1 is zero the result is NULL.
1462 ** If either operand is NULL, the result is NULL. 1479 ** If either operand is NULL, the result is NULL.
1463 */ 1480 */
1464 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ 1481 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1465 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ 1482 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1466 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ 1483 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1467 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ 1484 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
1613 ** evaluation of the function. 1630 ** evaluation of the function.
1614 ** 1631 **
1615 ** See also: Function0, AggStep, AggFinal 1632 ** See also: Function0, AggStep, AggFinal
1616 */ 1633 */
1617 case OP_Function0: { 1634 case OP_Function0: {
1618 int n; 1635 int n;
1619 sqlite3_context *pCtx; 1636 sqlite3_context *pCtx;
1620 1637
1621 assert( pOp->p4type==P4_FUNCDEF ); 1638 assert( pOp->p4type==P4_FUNCDEF );
1622 n = pOp->p5; 1639 n = pOp->p5;
1623 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); 1640 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
1624 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); 1641 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
1625 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 1642 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
1626 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); 1643 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
1627 if( pCtx==0 ) goto no_mem; 1644 if( pCtx==0 ) goto no_mem;
1628 pCtx->pOut = 0; 1645 pCtx->pOut = 0;
1629 pCtx->pFunc = pOp->p4.pFunc; 1646 pCtx->pFunc = pOp->p4.pFunc;
1630 pCtx->iOp = (int)(pOp - aOp); 1647 pCtx->iOp = (int)(pOp - aOp);
1631 pCtx->pVdbe = p; 1648 pCtx->pVdbe = p;
1632 pCtx->argc = n; 1649 pCtx->argc = n;
1633 pOp->p4type = P4_FUNCCTX; 1650 pOp->p4type = P4_FUNCCTX;
1634 pOp->p4.pCtx = pCtx; 1651 pOp->p4.pCtx = pCtx;
1635 pOp->opcode = OP_Function; 1652 pOp->opcode = OP_Function;
1636 /* Fall through into OP_Function */ 1653 /* Fall through into OP_Function */
(...skipping 17 matching lines...) Expand all
1654 1671
1655 memAboutToChange(p, pCtx->pOut); 1672 memAboutToChange(p, pCtx->pOut);
1656 #ifdef SQLITE_DEBUG 1673 #ifdef SQLITE_DEBUG
1657 for(i=0; i<pCtx->argc; i++){ 1674 for(i=0; i<pCtx->argc; i++){
1658 assert( memIsValid(pCtx->argv[i]) ); 1675 assert( memIsValid(pCtx->argv[i]) );
1659 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 1676 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
1660 } 1677 }
1661 #endif 1678 #endif
1662 MemSetTypeFlag(pCtx->pOut, MEM_Null); 1679 MemSetTypeFlag(pCtx->pOut, MEM_Null);
1663 pCtx->fErrorOrAux = 0; 1680 pCtx->fErrorOrAux = 0;
1664 db->lastRowid = lastRowid; 1681 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
1665 (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
1666 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
1667 1682
1668 /* If the function returned an error, throw an exception */ 1683 /* If the function returned an error, throw an exception */
1669 if( pCtx->fErrorOrAux ){ 1684 if( pCtx->fErrorOrAux ){
1670 if( pCtx->isError ){ 1685 if( pCtx->isError ){
1671 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); 1686 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
1672 rc = pCtx->isError; 1687 rc = pCtx->isError;
1673 } 1688 }
1674 sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1); 1689 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
1690 if( rc ) goto abort_due_to_error;
1675 } 1691 }
1676 1692
1677 /* Copy the result of the function into register P3 */ 1693 /* Copy the result of the function into register P3 */
1678 if( pOut->flags & (MEM_Str|MEM_Blob) ){ 1694 if( pOut->flags & (MEM_Str|MEM_Blob) ){
1679 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); 1695 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
1680 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; 1696 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
1681 } 1697 }
1682 1698
1683 REGISTER_TRACE(pOp->p3, pCtx->pOut); 1699 REGISTER_TRACE(pOp->p3, pCtx->pOut);
1684 UPDATE_MAX_BLOBSIZE(pCtx->pOut); 1700 UPDATE_MAX_BLOBSIZE(pCtx->pOut);
1685 break; 1701 break;
1686 } 1702 }
1687 1703
1688 /* Opcode: BitAnd P1 P2 P3 * * 1704 /* Opcode: BitAnd P1 P2 P3 * *
1689 ** Synopsis: r[P3]=r[P1]&r[P2] 1705 ** Synopsis: r[P3]=r[P1]&r[P2]
1690 ** 1706 **
1691 ** Take the bit-wise AND of the values in register P1 and P2 and 1707 ** Take the bit-wise AND of the values in register P1 and P2 and
1692 ** store the result in register P3. 1708 ** store the result in register P3.
1693 ** If either input is NULL, the result is NULL. 1709 ** If either input is NULL, the result is NULL.
1694 */ 1710 */
1695 /* Opcode: BitOr P1 P2 P3 * * 1711 /* Opcode: BitOr P1 P2 P3 * *
1696 ** Synopsis: r[P3]=r[P1]|r[P2] 1712 ** Synopsis: r[P3]=r[P1]|r[P2]
1697 ** 1713 **
1698 ** Take the bit-wise OR of the values in register P1 and P2 and 1714 ** Take the bit-wise OR of the values in register P1 and P2 and
1699 ** store the result in register P3. 1715 ** store the result in register P3.
1700 ** If either input is NULL, the result is NULL. 1716 ** If either input is NULL, the result is NULL.
1701 */ 1717 */
1702 /* Opcode: ShiftLeft P1 P2 P3 * * 1718 /* Opcode: ShiftLeft P1 P2 P3 * *
1703 ** Synopsis: r[P3]=r[P2]<<r[P1] 1719 ** Synopsis: r[P3]=r[P2]<<r[P1]
1704 ** 1720 **
1705 ** Shift the integer value in register P2 to the left by the 1721 ** Shift the integer value in register P2 to the left by the
1706 ** number of bits specified by the integer in register P1. 1722 ** number of bits specified by the integer in register P1.
1707 ** Store the result in register P3. 1723 ** Store the result in register P3.
1708 ** If either input is NULL, the result is NULL. 1724 ** If either input is NULL, the result is NULL.
1709 */ 1725 */
1710 /* Opcode: ShiftRight P1 P2 P3 * * 1726 /* Opcode: ShiftRight P1 P2 P3 * *
1711 ** Synopsis: r[P3]=r[P2]>>r[P1] 1727 ** Synopsis: r[P3]=r[P2]>>r[P1]
1712 ** 1728 **
1713 ** Shift the integer value in register P2 to the right by the 1729 ** Shift the integer value in register P2 to the right by the
1714 ** number of bits specified by the integer in register P1. 1730 ** number of bits specified by the integer in register P1.
1715 ** Store the result in register P3. 1731 ** Store the result in register P3.
1716 ** If either input is NULL, the result is NULL. 1732 ** If either input is NULL, the result is NULL.
1717 */ 1733 */
1718 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ 1734 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1719 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ 1735 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1720 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ 1736 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1721 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ 1737 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1761 } 1777 }
1762 memcpy(&iA, &uA, sizeof(iA)); 1778 memcpy(&iA, &uA, sizeof(iA));
1763 } 1779 }
1764 } 1780 }
1765 pOut->u.i = iA; 1781 pOut->u.i = iA;
1766 MemSetTypeFlag(pOut, MEM_Int); 1782 MemSetTypeFlag(pOut, MEM_Int);
1767 break; 1783 break;
1768 } 1784 }
1769 1785
1770 /* Opcode: AddImm P1 P2 * * * 1786 /* Opcode: AddImm P1 P2 * * *
1771 ** Synopsis: r[P1]=r[P1]+P2 1787 ** Synopsis: r[P1]=r[P1]+P2
1772 ** 1788 **
1773 ** Add the constant P2 to the value in register P1. 1789 ** Add the constant P2 to the value in register P1.
1774 ** The result is always an integer. 1790 ** The result is always an integer.
1775 ** 1791 **
1776 ** To force any register to be an integer, just add 0. 1792 ** To force any register to be an integer, just add 0.
1777 */ 1793 */
1778 case OP_AddImm: { /* in1 */ 1794 case OP_AddImm: { /* in1 */
1779 pIn1 = &aMem[pOp->p1]; 1795 pIn1 = &aMem[pOp->p1];
1780 memAboutToChange(p, pIn1); 1796 memAboutToChange(p, pIn1);
1781 sqlite3VdbeMemIntegerify(pIn1); 1797 sqlite3VdbeMemIntegerify(pIn1);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1848 testcase( pOp->p2==SQLITE_AFF_TEXT ); 1864 testcase( pOp->p2==SQLITE_AFF_TEXT );
1849 testcase( pOp->p2==SQLITE_AFF_BLOB ); 1865 testcase( pOp->p2==SQLITE_AFF_BLOB );
1850 testcase( pOp->p2==SQLITE_AFF_NUMERIC ); 1866 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1851 testcase( pOp->p2==SQLITE_AFF_INTEGER ); 1867 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1852 testcase( pOp->p2==SQLITE_AFF_REAL ); 1868 testcase( pOp->p2==SQLITE_AFF_REAL );
1853 pIn1 = &aMem[pOp->p1]; 1869 pIn1 = &aMem[pOp->p1];
1854 memAboutToChange(p, pIn1); 1870 memAboutToChange(p, pIn1);
1855 rc = ExpandBlob(pIn1); 1871 rc = ExpandBlob(pIn1);
1856 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); 1872 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
1857 UPDATE_MAX_BLOBSIZE(pIn1); 1873 UPDATE_MAX_BLOBSIZE(pIn1);
1874 if( rc ) goto abort_due_to_error;
1858 break; 1875 break;
1859 } 1876 }
1860 #endif /* SQLITE_OMIT_CAST */ 1877 #endif /* SQLITE_OMIT_CAST */
1861 1878
1862 /* Opcode: Lt P1 P2 P3 P4 P5 1879 /* Opcode: Eq P1 P2 P3 P4 P5
1863 ** Synopsis: if r[P1]<r[P3] goto P2 1880 ** Synopsis: IF r[P3]==r[P1]
1864 ** 1881 **
1865 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then 1882 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1866 ** jump to address P2. 1883 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1867 ** 1884 ** store the result of comparison in register P2.
1868 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1869 ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1870 ** bit is clear then fall through if either operand is NULL.
1871 ** 1885 **
1872 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 1886 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1873 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 1887 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1874 ** to coerce both inputs according to this affinity before the 1888 ** to coerce both inputs according to this affinity before the
1875 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 1889 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1876 ** affinity is used. Note that the affinity conversions are stored 1890 ** affinity is used. Note that the affinity conversions are stored
1877 ** back into the input registers P1 and P3. So this opcode can cause 1891 ** back into the input registers P1 and P3. So this opcode can cause
1878 ** persistent changes to registers P1 and P3. 1892 ** persistent changes to registers P1 and P3.
1879 ** 1893 **
1880 ** Once any conversions have taken place, and neither value is NULL, 1894 ** Once any conversions have taken place, and neither value is NULL,
1881 ** the values are compared. If both values are blobs then memcmp() is 1895 ** the values are compared. If both values are blobs then memcmp() is
1882 ** used to determine the results of the comparison. If both values 1896 ** used to determine the results of the comparison. If both values
1883 ** are text, then the appropriate collating function specified in 1897 ** are text, then the appropriate collating function specified in
1898 ** P4 is used to do the comparison. If P4 is not specified then
1899 ** memcmp() is used to compare text string. If both values are
1900 ** numeric, then a numeric comparison is used. If the two values
1901 ** are of different types, then numbers are considered less than
1902 ** strings and strings are considered less than blobs.
1903 **
1904 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1905 ** true or false and is never NULL. If both operands are NULL then the result
1906 ** of comparison is true. If either operand is NULL then the result is false.
1907 ** If neither operand is NULL the result is the same as it would be if
1908 ** the SQLITE_NULLEQ flag were omitted from P5.
1909 **
1910 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
1911 ** content of r[P2] is only changed if the new value is NULL or 0 (false).
1912 ** In other words, a prior r[P2] value will not be overwritten by 1 (true).
1913 */
1914 /* Opcode: Ne P1 P2 P3 P4 P5
1915 ** Synopsis: IF r[P3]!=r[P1]
1916 **
1917 ** This works just like the Eq opcode except that the jump is taken if
1918 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1919 ** additional information.
1920 **
1921 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
1922 ** content of r[P2] is only changed if the new value is NULL or 1 (true).
1923 ** In other words, a prior r[P2] value will not be overwritten by 0 (false).
1924 */
1925 /* Opcode: Lt P1 P2 P3 P4 P5
1926 ** Synopsis: IF r[P3]<r[P1]
1927 **
1928 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1929 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1930 ** the result of comparison (0 or 1 or NULL) into register P2.
1931 **
1932 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1933 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
1934 ** bit is clear then fall through if either operand is NULL.
1935 **
1936 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1937 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1938 ** to coerce both inputs according to this affinity before the
1939 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1940 ** affinity is used. Note that the affinity conversions are stored
1941 ** back into the input registers P1 and P3. So this opcode can cause
1942 ** persistent changes to registers P1 and P3.
1943 **
1944 ** Once any conversions have taken place, and neither value is NULL,
1945 ** the values are compared. If both values are blobs then memcmp() is
1946 ** used to determine the results of the comparison. If both values
1947 ** are text, then the appropriate collating function specified in
1884 ** P4 is used to do the comparison. If P4 is not specified then 1948 ** P4 is used to do the comparison. If P4 is not specified then
1885 ** memcmp() is used to compare text string. If both values are 1949 ** memcmp() is used to compare text string. If both values are
1886 ** numeric, then a numeric comparison is used. If the two values 1950 ** numeric, then a numeric comparison is used. If the two values
1887 ** are of different types, then numbers are considered less than 1951 ** are of different types, then numbers are considered less than
1888 ** strings and strings are considered less than blobs. 1952 ** strings and strings are considered less than blobs.
1889 **
1890 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1891 ** store a boolean result (either 0, or 1, or NULL) in register P2.
1892 **
1893 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1894 ** equal to one another, provided that they do not have their MEM_Cleared
1895 ** bit set.
1896 */
1897 /* Opcode: Ne P1 P2 P3 P4 P5
1898 ** Synopsis: if r[P1]!=r[P3] goto P2
1899 **
1900 ** This works just like the Lt opcode except that the jump is taken if
1901 ** the operands in registers P1 and P3 are not equal. See the Lt opcode for
1902 ** additional information.
1903 **
1904 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1905 ** true or false and is never NULL. If both operands are NULL then the result
1906 ** of comparison is false. If either operand is NULL then the result is true.
1907 ** If neither operand is NULL the result is the same as it would be if
1908 ** the SQLITE_NULLEQ flag were omitted from P5.
1909 */
1910 /* Opcode: Eq P1 P2 P3 P4 P5
1911 ** Synopsis: if r[P1]==r[P3] goto P2
1912 **
1913 ** This works just like the Lt opcode except that the jump is taken if
1914 ** the operands in registers P1 and P3 are equal.
1915 ** See the Lt opcode for additional information.
1916 **
1917 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1918 ** true or false and is never NULL. If both operands are NULL then the result
1919 ** of comparison is true. If either operand is NULL then the result is false.
1920 ** If neither operand is NULL the result is the same as it would be if
1921 ** the SQLITE_NULLEQ flag were omitted from P5.
1922 */ 1953 */
1923 /* Opcode: Le P1 P2 P3 P4 P5 1954 /* Opcode: Le P1 P2 P3 P4 P5
1924 ** Synopsis: if r[P1]<=r[P3] goto P2 1955 ** Synopsis: IF r[P3]<=r[P1]
1925 ** 1956 **
1926 ** This works just like the Lt opcode except that the jump is taken if 1957 ** This works just like the Lt opcode except that the jump is taken if
1927 ** the content of register P3 is less than or equal to the content of 1958 ** the content of register P3 is less than or equal to the content of
1928 ** register P1. See the Lt opcode for additional information. 1959 ** register P1. See the Lt opcode for additional information.
1929 */ 1960 */
1930 /* Opcode: Gt P1 P2 P3 P4 P5 1961 /* Opcode: Gt P1 P2 P3 P4 P5
1931 ** Synopsis: if r[P1]>r[P3] goto P2 1962 ** Synopsis: IF r[P3]>r[P1]
1932 ** 1963 **
1933 ** This works just like the Lt opcode except that the jump is taken if 1964 ** This works just like the Lt opcode except that the jump is taken if
1934 ** the content of register P3 is greater than the content of 1965 ** the content of register P3 is greater than the content of
1935 ** register P1. See the Lt opcode for additional information. 1966 ** register P1. See the Lt opcode for additional information.
1936 */ 1967 */
1937 /* Opcode: Ge P1 P2 P3 P4 P5 1968 /* Opcode: Ge P1 P2 P3 P4 P5
1938 ** Synopsis: if r[P1]>=r[P3] goto P2 1969 ** Synopsis: IF r[P3]>=r[P1]
1939 ** 1970 **
1940 ** This works just like the Lt opcode except that the jump is taken if 1971 ** This works just like the Lt opcode except that the jump is taken if
1941 ** the content of register P3 is greater than or equal to the content of 1972 ** the content of register P3 is greater than or equal to the content of
1942 ** register P1. See the Lt opcode for additional information. 1973 ** register P1. See the Lt opcode for additional information.
1943 */ 1974 */
1944 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ 1975 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1945 case OP_Ne: /* same as TK_NE, jump, in1, in3 */ 1976 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1946 case OP_Lt: /* same as TK_LT, jump, in1, in3 */ 1977 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1947 case OP_Le: /* same as TK_LE, jump, in1, in3 */ 1978 case OP_Le: /* same as TK_LE, jump, in1, in3 */
1948 case OP_Gt: /* same as TK_GT, jump, in1, in3 */ 1979 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1949 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ 1980 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
1950 int res; /* Result of the comparison of pIn1 against pIn3 */ 1981 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
1951 char affinity; /* Affinity to use for comparison */ 1982 char affinity; /* Affinity to use for comparison */
1952 u16 flags1; /* Copy of initial value of pIn1->flags */ 1983 u16 flags1; /* Copy of initial value of pIn1->flags */
1953 u16 flags3; /* Copy of initial value of pIn3->flags */ 1984 u16 flags3; /* Copy of initial value of pIn3->flags */
1954 1985
1955 pIn1 = &aMem[pOp->p1]; 1986 pIn1 = &aMem[pOp->p1];
1956 pIn3 = &aMem[pOp->p3]; 1987 pIn3 = &aMem[pOp->p3];
1957 flags1 = pIn1->flags; 1988 flags1 = pIn1->flags;
1958 flags3 = pIn3->flags; 1989 flags3 = pIn3->flags;
1959 if( (flags1 | flags3)&MEM_Null ){ 1990 if( (flags1 | flags3)&MEM_Null ){
1960 /* One or both operands are NULL */ 1991 /* One or both operands are NULL */
1961 if( pOp->p5 & SQLITE_NULLEQ ){ 1992 if( pOp->p5 & SQLITE_NULLEQ ){
1962 /* If SQLITE_NULLEQ is set (which will only happen if the operator is 1993 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1963 ** OP_Eq or OP_Ne) then take the jump or not depending on whether 1994 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1964 ** or not both operands are null. 1995 ** or not both operands are null.
1965 */ 1996 */
1966 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); 1997 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
1967 assert( (flags1 & MEM_Cleared)==0 ); 1998 assert( (flags1 & MEM_Cleared)==0 );
1968 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); 1999 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
1969 if( (flags1&MEM_Null)!=0 2000 if( (flags1&flags3&MEM_Null)!=0
1970 && (flags3&MEM_Null)!=0
1971 && (flags3&MEM_Cleared)==0 2001 && (flags3&MEM_Cleared)==0
1972 ){ 2002 ){
1973 res = 0; /* Results are equal */ 2003 res = 0; /* Operands are equal */
1974 }else{ 2004 }else{
1975 res = 1; /* Results are not equal */ 2005 res = 1; /* Operands are not equal */
1976 } 2006 }
1977 }else{ 2007 }else{
1978 /* SQLITE_NULLEQ is clear and at least one operand is NULL, 2008 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1979 ** then the result is always NULL. 2009 ** then the result is always NULL.
1980 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. 2010 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1981 */ 2011 */
1982 if( pOp->p5 & SQLITE_STOREP2 ){ 2012 if( pOp->p5 & SQLITE_STOREP2 ){
1983 pOut = &aMem[pOp->p2]; 2013 pOut = &aMem[pOp->p2];
2014 iCompare = 1; /* Operands are not equal */
1984 memAboutToChange(p, pOut); 2015 memAboutToChange(p, pOut);
1985 MemSetTypeFlag(pOut, MEM_Null); 2016 MemSetTypeFlag(pOut, MEM_Null);
1986 REGISTER_TRACE(pOp->p2, pOut); 2017 REGISTER_TRACE(pOp->p2, pOut);
1987 }else{ 2018 }else{
1988 VdbeBranchTaken(2,3); 2019 VdbeBranchTaken(2,3);
1989 if( pOp->p5 & SQLITE_JUMPIFNULL ){ 2020 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1990 goto jump_to_p2; 2021 goto jump_to_p2;
1991 } 2022 }
1992 } 2023 }
1993 break; 2024 break;
1994 } 2025 }
1995 }else{ 2026 }else{
1996 /* Neither operand is NULL. Do a comparison. */ 2027 /* Neither operand is NULL. Do a comparison. */
1997 affinity = pOp->p5 & SQLITE_AFF_MASK; 2028 affinity = pOp->p5 & SQLITE_AFF_MASK;
1998 if( affinity>=SQLITE_AFF_NUMERIC ){ 2029 if( affinity>=SQLITE_AFF_NUMERIC ){
1999 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 2030 if( (flags1 | flags3)&MEM_Str ){
2000 applyNumericAffinity(pIn1,0); 2031 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
2032 applyNumericAffinity(pIn1,0);
2033 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
2034 flags3 = pIn3->flags;
2035 }
2036 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
2037 applyNumericAffinity(pIn3,0);
2038 }
2001 } 2039 }
2002 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 2040 /* Handle the common case of integer comparison here, as an
2003 applyNumericAffinity(pIn3,0); 2041 ** optimization, to avoid a call to sqlite3MemCompare() */
2042 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2043 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2044 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2045 res = 0;
2046 goto compare_op;
2004 } 2047 }
2005 }else if( affinity==SQLITE_AFF_TEXT ){ 2048 }else if( affinity==SQLITE_AFF_TEXT ){
2006 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ 2049 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
2007 testcase( pIn1->flags & MEM_Int ); 2050 testcase( pIn1->flags & MEM_Int );
2008 testcase( pIn1->flags & MEM_Real ); 2051 testcase( pIn1->flags & MEM_Real );
2009 sqlite3VdbeMemStringify(pIn1, encoding, 1); 2052 sqlite3VdbeMemStringify(pIn1, encoding, 1);
2010 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); 2053 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2011 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); 2054 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
2055 assert( pIn1!=pIn3 );
2012 } 2056 }
2013 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ 2057 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
2014 testcase( pIn3->flags & MEM_Int ); 2058 testcase( pIn3->flags & MEM_Int );
2015 testcase( pIn3->flags & MEM_Real ); 2059 testcase( pIn3->flags & MEM_Real );
2016 sqlite3VdbeMemStringify(pIn3, encoding, 1); 2060 sqlite3VdbeMemStringify(pIn3, encoding, 1);
2017 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); 2061 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2018 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); 2062 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
2019 } 2063 }
2020 } 2064 }
2021 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); 2065 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
2022 if( flags1 & MEM_Zero ){
2023 sqlite3VdbeMemExpandBlob(pIn1);
2024 flags1 &= ~MEM_Zero;
2025 }
2026 if( flags3 & MEM_Zero ){
2027 sqlite3VdbeMemExpandBlob(pIn3);
2028 flags3 &= ~MEM_Zero;
2029 }
2030 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); 2066 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
2031 } 2067 }
2068 compare_op:
2032 switch( pOp->opcode ){ 2069 switch( pOp->opcode ){
2033 case OP_Eq: res = res==0; break; 2070 case OP_Eq: res2 = res==0; break;
2034 case OP_Ne: res = res!=0; break; 2071 case OP_Ne: res2 = res; break;
2035 case OP_Lt: res = res<0; break; 2072 case OP_Lt: res2 = res<0; break;
2036 case OP_Le: res = res<=0; break; 2073 case OP_Le: res2 = res<=0; break;
2037 case OP_Gt: res = res>0; break; 2074 case OP_Gt: res2 = res>0; break;
2038 default: res = res>=0; break; 2075 default: res2 = res>=0; break;
2039 } 2076 }
2040 2077
2041 /* Undo any changes made by applyAffinity() to the input registers. */ 2078 /* Undo any changes made by applyAffinity() to the input registers. */
2042 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 2079 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2043 pIn1->flags = flags1; 2080 pIn1->flags = flags1;
2044 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); 2081 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2045 pIn3->flags = flags3; 2082 pIn3->flags = flags3;
2046 2083
2047 if( pOp->p5 & SQLITE_STOREP2 ){ 2084 if( pOp->p5 & SQLITE_STOREP2 ){
2048 pOut = &aMem[pOp->p2]; 2085 pOut = &aMem[pOp->p2];
2086 iCompare = res;
2087 res2 = res2!=0; /* For this path res2 must be exactly 0 or 1 */
2088 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
2089 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
2090 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2091 ** is only used in contexts where either:
2092 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2093 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2094 ** Therefore it is not necessary to check the content of r[P2] for
2095 ** NULL. */
2096 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
2097 assert( res2==0 || res2==1 );
2098 testcase( res2==0 && pOp->opcode==OP_Eq );
2099 testcase( res2==1 && pOp->opcode==OP_Eq );
2100 testcase( res2==0 && pOp->opcode==OP_Ne );
2101 testcase( res2==1 && pOp->opcode==OP_Ne );
2102 if( (pOp->opcode==OP_Eq)==res2 ) break;
2103 }
2049 memAboutToChange(p, pOut); 2104 memAboutToChange(p, pOut);
2050 MemSetTypeFlag(pOut, MEM_Int); 2105 MemSetTypeFlag(pOut, MEM_Int);
2051 pOut->u.i = res; 2106 pOut->u.i = res2;
2052 REGISTER_TRACE(pOp->p2, pOut); 2107 REGISTER_TRACE(pOp->p2, pOut);
2053 }else{ 2108 }else{
2054 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2109 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2055 if( res ){ 2110 if( res2 ){
2056 goto jump_to_p2; 2111 goto jump_to_p2;
2057 } 2112 }
2058 } 2113 }
2059 break; 2114 break;
2060 } 2115 }
2061 2116
2117 /* Opcode: ElseNotEq * P2 * * *
2118 **
2119 ** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2120 ** If result of an OP_Eq comparison on the same two operands
2121 ** would have be NULL or false (0), then then jump to P2.
2122 ** If the result of an OP_Eq comparison on the two previous operands
2123 ** would have been true (1), then fall through.
2124 */
2125 case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2126 assert( pOp>aOp );
2127 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
2128 assert( pOp[-1].p5 & SQLITE_STOREP2 );
2129 VdbeBranchTaken(iCompare!=0, 2);
2130 if( iCompare!=0 ) goto jump_to_p2;
2131 break;
2132 }
2133
2134
2062 /* Opcode: Permutation * * * P4 * 2135 /* Opcode: Permutation * * * P4 *
2063 ** 2136 **
2064 ** Set the permutation used by the OP_Compare operator to be the array 2137 ** Set the permutation used by the OP_Compare operator in the next
2065 ** of integers in P4. 2138 ** instruction. The permutation is stored in the P4 operand.
2066 ** 2139 **
2067 ** The permutation is only valid until the next OP_Compare that has 2140 ** The permutation is only valid until the next OP_Compare that has
2068 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 2141 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2069 ** occur immediately prior to the OP_Compare. 2142 ** occur immediately prior to the OP_Compare.
2143 **
2144 ** The first integer in the P4 integer array is the length of the array
2145 ** and does not become part of the permutation.
2070 */ 2146 */
2071 case OP_Permutation: { 2147 case OP_Permutation: {
2072 assert( pOp->p4type==P4_INTARRAY ); 2148 assert( pOp->p4type==P4_INTARRAY );
2073 assert( pOp->p4.ai ); 2149 assert( pOp->p4.ai );
2074 aPermute = pOp->p4.ai; 2150 assert( pOp[1].opcode==OP_Compare );
2151 assert( pOp[1].p5 & OPFLAG_PERMUTE );
2075 break; 2152 break;
2076 } 2153 }
2077 2154
2078 /* Opcode: Compare P1 P2 P3 P4 P5 2155 /* Opcode: Compare P1 P2 P3 P4 P5
2079 ** Synopsis: r[P1@P3] <-> r[P2@P3] 2156 ** Synopsis: r[P1@P3] <-> r[P2@P3]
2080 ** 2157 **
2081 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this 2158 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2082 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of 2159 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
2083 ** the comparison for use by the next OP_Jump instruct. 2160 ** the comparison for use by the next OP_Jump instruct.
2084 ** 2161 **
(...skipping 12 matching lines...) Expand all
2097 */ 2174 */
2098 case OP_Compare: { 2175 case OP_Compare: {
2099 int n; 2176 int n;
2100 int i; 2177 int i;
2101 int p1; 2178 int p1;
2102 int p2; 2179 int p2;
2103 const KeyInfo *pKeyInfo; 2180 const KeyInfo *pKeyInfo;
2104 int idx; 2181 int idx;
2105 CollSeq *pColl; /* Collating sequence to use on this term */ 2182 CollSeq *pColl; /* Collating sequence to use on this term */
2106 int bRev; /* True for DESCENDING sort order */ 2183 int bRev; /* True for DESCENDING sort order */
2184 int *aPermute; /* The permutation */
2107 2185
2108 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; 2186 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2187 aPermute = 0;
2188 }else{
2189 assert( pOp>aOp );
2190 assert( pOp[-1].opcode==OP_Permutation );
2191 assert( pOp[-1].p4type==P4_INTARRAY );
2192 aPermute = pOp[-1].p4.ai + 1;
2193 assert( aPermute!=0 );
2194 }
2109 n = pOp->p3; 2195 n = pOp->p3;
2110 pKeyInfo = pOp->p4.pKeyInfo; 2196 pKeyInfo = pOp->p4.pKeyInfo;
2111 assert( n>0 ); 2197 assert( n>0 );
2112 assert( pKeyInfo!=0 ); 2198 assert( pKeyInfo!=0 );
2113 p1 = pOp->p1; 2199 p1 = pOp->p1;
2114 p2 = pOp->p2; 2200 p2 = pOp->p2;
2115 #if SQLITE_DEBUG 2201 #if SQLITE_DEBUG
2116 if( aPermute ){ 2202 if( aPermute ){
2117 int k, mx = 0; 2203 int k, mx = 0;
2118 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; 2204 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
2119 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 ); 2205 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2120 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 ); 2206 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
2121 }else{ 2207 }else{
2122 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 ); 2208 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2123 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 ); 2209 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
2124 } 2210 }
2125 #endif /* SQLITE_DEBUG */ 2211 #endif /* SQLITE_DEBUG */
2126 for(i=0; i<n; i++){ 2212 for(i=0; i<n; i++){
2127 idx = aPermute ? aPermute[i] : i; 2213 idx = aPermute ? aPermute[i] : i;
2128 assert( memIsValid(&aMem[p1+idx]) ); 2214 assert( memIsValid(&aMem[p1+idx]) );
2129 assert( memIsValid(&aMem[p2+idx]) ); 2215 assert( memIsValid(&aMem[p2+idx]) );
2130 REGISTER_TRACE(p1+idx, &aMem[p1+idx]); 2216 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2131 REGISTER_TRACE(p2+idx, &aMem[p2+idx]); 2217 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
2132 assert( i<pKeyInfo->nField ); 2218 assert( i<pKeyInfo->nField );
2133 pColl = pKeyInfo->aColl[i]; 2219 pColl = pKeyInfo->aColl[i];
2134 bRev = pKeyInfo->aSortOrder[i]; 2220 bRev = pKeyInfo->aSortOrder[i];
2135 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); 2221 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
2136 if( iCompare ){ 2222 if( iCompare ){
2137 if( bRev ) iCompare = -iCompare; 2223 if( bRev ) iCompare = -iCompare;
2138 break; 2224 break;
2139 } 2225 }
2140 } 2226 }
2141 aPermute = 0;
2142 break; 2227 break;
2143 } 2228 }
2144 2229
2145 /* Opcode: Jump P1 P2 P3 * * 2230 /* Opcode: Jump P1 P2 P3 * *
2146 ** 2231 **
2147 ** Jump to the instruction at address P1, P2, or P3 depending on whether 2232 ** Jump to the instruction at address P1, P2, or P3 depending on whether
2148 ** in the most recent OP_Compare instruction the P1 vector was less than 2233 ** in the most recent OP_Compare instruction the P1 vector was less than
2149 ** equal to, or greater than the P2 vector, respectively. 2234 ** equal to, or greater than the P2 vector, respectively.
2150 */ 2235 */
2151 case OP_Jump: { /* jump */ 2236 case OP_Jump: { /* jump */
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
2244 sqlite3VdbeMemSetNull(pOut); 2329 sqlite3VdbeMemSetNull(pOut);
2245 if( (pIn1->flags & MEM_Null)==0 ){ 2330 if( (pIn1->flags & MEM_Null)==0 ){
2246 pOut->flags = MEM_Int; 2331 pOut->flags = MEM_Int;
2247 pOut->u.i = ~sqlite3VdbeIntValue(pIn1); 2332 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
2248 } 2333 }
2249 break; 2334 break;
2250 } 2335 }
2251 2336
2252 /* Opcode: Once P1 P2 * * * 2337 /* Opcode: Once P1 P2 * * *
2253 ** 2338 **
2254 ** Check the "once" flag number P1. If it is set, jump to instruction P2. 2339 ** If the P1 value is equal to the P1 value on the OP_Init opcode at
2255 ** Otherwise, set the flag and fall through to the next instruction. 2340 ** instruction 0, then jump to P2. If the two P1 values differ, then
2256 ** In other words, this opcode causes all following opcodes up through P2 2341 ** set the P1 value on this opcode to equal the P1 value on the OP_Init
2257 ** (but not including P2) to run just once and to be skipped on subsequent 2342 ** and fall through.
2258 ** times through the loop.
2259 **
2260 ** All "once" flags are initially cleared whenever a prepared statement
2261 ** first begins to run.
2262 */ 2343 */
2263 case OP_Once: { /* jump */ 2344 case OP_Once: { /* jump */
2264 assert( pOp->p1<p->nOnceFlag ); 2345 assert( p->aOp[0].opcode==OP_Init );
2265 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2); 2346 VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2);
2266 if( p->aOnceFlag[pOp->p1] ){ 2347 if( p->aOp[0].p1==pOp->p1 ){
2267 goto jump_to_p2; 2348 goto jump_to_p2;
2268 }else{ 2349 }else{
2269 p->aOnceFlag[pOp->p1] = 1; 2350 pOp->p1 = p->aOp[0].p1;
2270 } 2351 }
2271 break; 2352 break;
2272 } 2353 }
2273 2354
2274 /* Opcode: If P1 P2 P3 * * 2355 /* Opcode: If P1 P2 P3 * *
2275 ** 2356 **
2276 ** Jump to P2 if the value in register P1 is true. The value 2357 ** Jump to P2 if the value in register P1 is true. The value
2277 ** is considered true if it is numeric and non-zero. If the value 2358 ** is considered true if it is numeric and non-zero. If the value
2278 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 2359 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2279 */ 2360 */
(...skipping 18 matching lines...) Expand all
2298 if( pOp->opcode==OP_IfNot ) c = !c; 2379 if( pOp->opcode==OP_IfNot ) c = !c;
2299 } 2380 }
2300 VdbeBranchTaken(c!=0, 2); 2381 VdbeBranchTaken(c!=0, 2);
2301 if( c ){ 2382 if( c ){
2302 goto jump_to_p2; 2383 goto jump_to_p2;
2303 } 2384 }
2304 break; 2385 break;
2305 } 2386 }
2306 2387
2307 /* Opcode: IsNull P1 P2 * * * 2388 /* Opcode: IsNull P1 P2 * * *
2308 ** Synopsis: if r[P1]==NULL goto P2 2389 ** Synopsis: if r[P1]==NULL goto P2
2309 ** 2390 **
2310 ** Jump to P2 if the value in register P1 is NULL. 2391 ** Jump to P2 if the value in register P1 is NULL.
2311 */ 2392 */
2312 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ 2393 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
2313 pIn1 = &aMem[pOp->p1]; 2394 pIn1 = &aMem[pOp->p1];
2314 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); 2395 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
2315 if( (pIn1->flags & MEM_Null)!=0 ){ 2396 if( (pIn1->flags & MEM_Null)!=0 ){
2316 goto jump_to_p2; 2397 goto jump_to_p2;
2317 } 2398 }
2318 break; 2399 break;
2319 } 2400 }
2320 2401
2321 /* Opcode: NotNull P1 P2 * * * 2402 /* Opcode: NotNull P1 P2 * * *
2322 ** Synopsis: if r[P1]!=NULL goto P2 2403 ** Synopsis: if r[P1]!=NULL goto P2
2323 ** 2404 **
2324 ** Jump to P2 if the value in register P1 is not NULL. 2405 ** Jump to P2 if the value in register P1 is not NULL.
2325 */ 2406 */
2326 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ 2407 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
2327 pIn1 = &aMem[pOp->p1]; 2408 pIn1 = &aMem[pOp->p1];
2328 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); 2409 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
2329 if( (pIn1->flags & MEM_Null)==0 ){ 2410 if( (pIn1->flags & MEM_Null)==0 ){
2330 goto jump_to_p2; 2411 goto jump_to_p2;
2331 } 2412 }
2332 break; 2413 break;
2333 } 2414 }
2334 2415
2335 /* Opcode: Column P1 P2 P3 P4 P5 2416 /* Opcode: Column P1 P2 P3 P4 P5
2336 ** Synopsis: r[P3]=PX 2417 ** Synopsis: r[P3]=PX
2337 ** 2418 **
2338 ** Interpret the data that cursor P1 points to as a structure built using 2419 ** Interpret the data that cursor P1 points to as a structure built using
2339 ** the MakeRecord instruction. (See the MakeRecord opcode for additional 2420 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
2340 ** information about the format of the data.) Extract the P2-th column 2421 ** information about the format of the data.) Extract the P2-th column
2341 ** from this record. If there are less that (P2+1) 2422 ** from this record. If there are less that (P2+1)
2342 ** values in the record, extract a NULL. 2423 ** values in the record, extract a NULL.
2343 ** 2424 **
2344 ** The value extracted is stored in register P3. 2425 ** The value extracted is stored in register P3.
2345 ** 2426 **
2346 ** If the column contains fewer than P2 fields, then extract a NULL. Or, 2427 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
2347 ** if the P4 argument is a P4_MEM use the value of the P4 argument as 2428 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
2348 ** the result. 2429 ** the result.
2349 ** 2430 **
2350 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, 2431 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2351 ** then the cache of the cursor is reset prior to extracting the column. 2432 ** then the cache of the cursor is reset prior to extracting the column.
2352 ** The first OP_Column against a pseudo-table after the value of the content 2433 ** The first OP_Column against a pseudo-table after the value of the content
2353 ** register has changed should have this bit set. 2434 ** register has changed should have this bit set.
2354 ** 2435 **
2355 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when 2436 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2356 ** the result is guaranteed to only be used as the argument of a length() 2437 ** the result is guaranteed to only be used as the argument of a length()
2357 ** or typeof() function, respectively. The loading of large blobs can be 2438 ** or typeof() function, respectively. The loading of large blobs can be
2358 ** skipped for length() and all content loading can be skipped for typeof(). 2439 ** skipped for length() and all content loading can be skipped for typeof().
2359 */ 2440 */
2360 case OP_Column: { 2441 case OP_Column: {
2361 i64 payloadSize64; /* Number of bytes in the record */
2362 int p2; /* column number to retrieve */ 2442 int p2; /* column number to retrieve */
2363 VdbeCursor *pC; /* The VDBE cursor */ 2443 VdbeCursor *pC; /* The VDBE cursor */
2364 BtCursor *pCrsr; /* The BTree cursor */ 2444 BtCursor *pCrsr; /* The BTree cursor */
2365 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ 2445 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
2366 int len; /* The length of the serialized data for the column */ 2446 int len; /* The length of the serialized data for the column */
2367 int i; /* Loop counter */ 2447 int i; /* Loop counter */
2368 Mem *pDest; /* Where to write the extracted value */ 2448 Mem *pDest; /* Where to write the extracted value */
2369 Mem sMem; /* For storing the record being decoded */ 2449 Mem sMem; /* For storing the record being decoded */
2370 const u8 *zData; /* Part of the record being decoded */ 2450 const u8 *zData; /* Part of the record being decoded */
2371 const u8 *zHdr; /* Next unparsed byte of the header */ 2451 const u8 *zHdr; /* Next unparsed byte of the header */
2372 const u8 *zEndHdr; /* Pointer to first byte after the header */ 2452 const u8 *zEndHdr; /* Pointer to first byte after the header */
2373 u32 offset; /* Offset into the data */ 2453 u32 offset; /* Offset into the data */
2374 u64 offset64; /* 64-bit offset */ 2454 u64 offset64; /* 64-bit offset */
2375 u32 avail; /* Number of bytes of available data */ 2455 u32 avail; /* Number of bytes of available data */
2376 u32 t; /* A type code from the record header */ 2456 u32 t; /* A type code from the record header */
2377 u16 fx; /* pDest->flags value */
2378 Mem *pReg; /* PseudoTable input register */ 2457 Mem *pReg; /* PseudoTable input register */
2379 2458
2459 pC = p->apCsr[pOp->p1];
2380 p2 = pOp->p2; 2460 p2 = pOp->p2;
2381 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); 2461
2462 /* If the cursor cache is stale, bring it up-to-date */
2463 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
2464 if( rc ) goto abort_due_to_error;
2465
2466 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
2382 pDest = &aMem[pOp->p3]; 2467 pDest = &aMem[pOp->p3];
2383 memAboutToChange(p, pDest); 2468 memAboutToChange(p, pDest);
2384 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 2469 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2385 pC = p->apCsr[pOp->p1];
2386 assert( pC!=0 ); 2470 assert( pC!=0 );
2387 assert( p2<pC->nField ); 2471 assert( p2<pC->nField );
2388 aOffset = pC->aOffset; 2472 aOffset = pC->aOffset;
2389 assert( pC->eCurType!=CURTYPE_VTAB ); 2473 assert( pC->eCurType!=CURTYPE_VTAB );
2390 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 2474 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2391 assert( pC->eCurType!=CURTYPE_SORTER ); 2475 assert( pC->eCurType!=CURTYPE_SORTER );
2392 pCrsr = pC->uc.pCursor;
2393 2476
2394 /* If the cursor cache is stale, bring it up-to-date */ 2477 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
2395 rc = sqlite3VdbeCursorMoveto(pC);
2396 if( rc ) goto abort_due_to_error;
2397 if( pC->cacheStatus!=p->cacheCtr ){
2398 if( pC->nullRow ){ 2478 if( pC->nullRow ){
2399 if( pC->eCurType==CURTYPE_PSEUDO ){ 2479 if( pC->eCurType==CURTYPE_PSEUDO ){
2400 assert( pC->uc.pseudoTableReg>0 ); 2480 assert( pC->uc.pseudoTableReg>0 );
2401 pReg = &aMem[pC->uc.pseudoTableReg]; 2481 pReg = &aMem[pC->uc.pseudoTableReg];
2402 assert( pReg->flags & MEM_Blob ); 2482 assert( pReg->flags & MEM_Blob );
2403 assert( memIsValid(pReg) ); 2483 assert( memIsValid(pReg) );
2404 pC->payloadSize = pC->szRow = avail = pReg->n; 2484 pC->payloadSize = pC->szRow = avail = pReg->n;
2405 pC->aRow = (u8*)pReg->z; 2485 pC->aRow = (u8*)pReg->z;
2406 }else{ 2486 }else{
2407 sqlite3VdbeMemSetNull(pDest); 2487 sqlite3VdbeMemSetNull(pDest);
2408 goto op_column_out; 2488 goto op_column_out;
2409 } 2489 }
2410 }else{ 2490 }else{
2491 pCrsr = pC->uc.pCursor;
2411 assert( pC->eCurType==CURTYPE_BTREE ); 2492 assert( pC->eCurType==CURTYPE_BTREE );
2412 assert( pCrsr ); 2493 assert( pCrsr );
2413 if( pC->isTable==0 ){ 2494 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2414 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 2495 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
2415 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64); 2496 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail);
2416 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2417 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2418 ** payload size, so it is impossible for payloadSize64 to be
2419 ** larger than 32 bits. */
2420 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2421 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2422 pC->payloadSize = (u32)payloadSize64;
2423 }else{
2424 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2425 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2426 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2427 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
2428 }
2429 assert( avail<=65536 ); /* Maximum page size is 64KiB */ 2497 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2430 if( pC->payloadSize <= (u32)avail ){ 2498 if( pC->payloadSize <= (u32)avail ){
2431 pC->szRow = pC->payloadSize; 2499 pC->szRow = pC->payloadSize;
2432 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 2500 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2433 goto too_big; 2501 goto too_big;
2434 }else{ 2502 }else{
2435 pC->szRow = avail; 2503 pC->szRow = avail;
2436 } 2504 }
2437 } 2505 }
2438 pC->cacheStatus = p->cacheCtr; 2506 pC->cacheStatus = p->cacheCtr;
2439 pC->iHdrOffset = getVarint32(pC->aRow, offset); 2507 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2440 pC->nHdrParsed = 0; 2508 pC->nHdrParsed = 0;
2441 aOffset[0] = offset; 2509 aOffset[0] = offset;
2442 2510
2443 2511
2444 if( avail<offset ){ 2512 if( avail<offset ){ /*OPTIMIZATION-IF-FALSE*/
2445 /* pC->aRow does not have to hold the entire row, but it does at least 2513 /* pC->aRow does not have to hold the entire row, but it does at least
2446 ** need to cover the header of the record. If pC->aRow does not contain 2514 ** need to cover the header of the record. If pC->aRow does not contain
2447 ** the complete header, then set it to zero, forcing the header to be 2515 ** the complete header, then set it to zero, forcing the header to be
2448 ** dynamically allocated. */ 2516 ** dynamically allocated. */
2449 pC->aRow = 0; 2517 pC->aRow = 0;
2450 pC->szRow = 0; 2518 pC->szRow = 0;
2451 2519
2452 /* Make sure a corrupt database has not given us an oversize header. 2520 /* Make sure a corrupt database has not given us an oversize header.
2453 ** Do this now to avoid an oversize memory allocation. 2521 ** Do this now to avoid an oversize memory allocation.
2454 ** 2522 **
2455 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte 2523 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2456 ** types use so much data space that there can only be 4096 and 32 of 2524 ** types use so much data space that there can only be 4096 and 32 of
2457 ** them, respectively. So the maximum header length results from a 2525 ** them, respectively. So the maximum header length results from a
2458 ** 3-byte type for each of the maximum of 32768 columns plus three 2526 ** 3-byte type for each of the maximum of 32768 columns plus three
2459 ** extra bytes for the header length itself. 32768*3 + 3 = 98307. 2527 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2460 */ 2528 */
2461 if( offset > 98307 || offset > pC->payloadSize ){ 2529 if( offset > 98307 || offset > pC->payloadSize ){
2462 rc = SQLITE_CORRUPT_BKPT; 2530 rc = SQLITE_CORRUPT_BKPT;
2463 goto op_column_error; 2531 goto abort_due_to_error;
2464 } 2532 }
2533 }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/
2534 /* The following goto is an optimization. It can be omitted and
2535 ** everything will still work. But OP_Column is measurably faster
2536 ** by skipping the subsequent conditional, which is always true.
2537 */
2538 zData = pC->aRow;
2539 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2540 goto op_column_read_header;
2465 } 2541 }
2466
2467 /* The following goto is an optimization. It can be omitted and
2468 ** everything will still work. But OP_Column is measurably faster
2469 ** by skipping the subsequent conditional, which is always true.
2470 */
2471 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2472 goto op_column_read_header;
2473 } 2542 }
2474 2543
2475 /* Make sure at least the first p2+1 entries of the header have been 2544 /* Make sure at least the first p2+1 entries of the header have been
2476 ** parsed and valid information is in aOffset[] and pC->aType[]. 2545 ** parsed and valid information is in aOffset[] and pC->aType[].
2477 */ 2546 */
2478 if( pC->nHdrParsed<=p2 ){ 2547 if( pC->nHdrParsed<=p2 ){
2479 /* If there is more header available for parsing in the record, try 2548 /* If there is more header available for parsing in the record, try
2480 ** to extract additional fields up through the p2+1-th field 2549 ** to extract additional fields up through the p2+1-th field
2481 */ 2550 */
2482 op_column_read_header:
2483 if( pC->iHdrOffset<aOffset[0] ){ 2551 if( pC->iHdrOffset<aOffset[0] ){
2484 /* Make sure zData points to enough of the record to cover the header. */ 2552 /* Make sure zData points to enough of the record to cover the header. */
2485 if( pC->aRow==0 ){ 2553 if( pC->aRow==0 ){
2486 memset(&sMem, 0, sizeof(sMem)); 2554 memset(&sMem, 0, sizeof(sMem));
2487 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem); 2555 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
2488 if( rc!=SQLITE_OK ) goto op_column_error; 2556 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2489 zData = (u8*)sMem.z; 2557 zData = (u8*)sMem.z;
2490 }else{ 2558 }else{
2491 zData = pC->aRow; 2559 zData = pC->aRow;
2492 } 2560 }
2493 2561
2494 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ 2562 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
2563 op_column_read_header:
2495 i = pC->nHdrParsed; 2564 i = pC->nHdrParsed;
2496 offset64 = aOffset[i]; 2565 offset64 = aOffset[i];
2497 zHdr = zData + pC->iHdrOffset; 2566 zHdr = zData + pC->iHdrOffset;
2498 zEndHdr = zData + aOffset[0]; 2567 zEndHdr = zData + aOffset[0];
2499 assert( i<=p2 && zHdr<zEndHdr );
2500 do{ 2568 do{
2501 if( (t = zHdr[0])<0x80 ){ 2569 if( (t = zHdr[0])<0x80 ){
2502 zHdr++; 2570 zHdr++;
2503 offset64 += sqlite3VdbeOneByteSerialTypeLen(t); 2571 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
2504 }else{ 2572 }else{
2505 zHdr += sqlite3GetVarint32(zHdr, &t); 2573 zHdr += sqlite3GetVarint32(zHdr, &t);
2506 offset64 += sqlite3VdbeSerialTypeLen(t); 2574 offset64 += sqlite3VdbeSerialTypeLen(t);
2507 } 2575 }
2508 pC->aType[i++] = t; 2576 pC->aType[i++] = t;
2509 aOffset[i] = (u32)(offset64 & 0xffffffff); 2577 aOffset[i] = (u32)(offset64 & 0xffffffff);
2510 }while( i<=p2 && zHdr<zEndHdr ); 2578 }while( i<=p2 && zHdr<zEndHdr );
2511 pC->nHdrParsed = i; 2579
2512 pC->iHdrOffset = (u32)(zHdr - zData);
2513 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
2514
2515 /* The record is corrupt if any of the following are true: 2580 /* The record is corrupt if any of the following are true:
2516 ** (1) the bytes of the header extend past the declared header size 2581 ** (1) the bytes of the header extend past the declared header size
2517 ** (2) the entire header was used but not all data was used 2582 ** (2) the entire header was used but not all data was used
2518 ** (3) the end of the data extends beyond the end of the record. 2583 ** (3) the end of the data extends beyond the end of the record.
2519 */ 2584 */
2520 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) 2585 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2521 || (offset64 > pC->payloadSize) 2586 || (offset64 > pC->payloadSize)
2522 ){ 2587 ){
2588 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
2523 rc = SQLITE_CORRUPT_BKPT; 2589 rc = SQLITE_CORRUPT_BKPT;
2524 goto op_column_error; 2590 goto abort_due_to_error;
2525 } 2591 }
2592
2593 pC->nHdrParsed = i;
2594 pC->iHdrOffset = (u32)(zHdr - zData);
2595 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
2526 }else{ 2596 }else{
2527 t = 0; 2597 t = 0;
2528 } 2598 }
2529 2599
2530 /* If after trying to extract new entries from the header, nHdrParsed is 2600 /* If after trying to extract new entries from the header, nHdrParsed is
2531 ** still not up to p2, that means that the record has fewer than p2 2601 ** still not up to p2, that means that the record has fewer than p2
2532 ** columns. So the result will be either the default value or a NULL. 2602 ** columns. So the result will be either the default value or a NULL.
2533 */ 2603 */
2534 if( pC->nHdrParsed<=p2 ){ 2604 if( pC->nHdrParsed<=p2 ){
2535 if( pOp->p4type==P4_MEM ){ 2605 if( pOp->p4type==P4_MEM ){
2536 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); 2606 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2537 }else{ 2607 }else{
2538 sqlite3VdbeMemSetNull(pDest); 2608 sqlite3VdbeMemSetNull(pDest);
2539 } 2609 }
2540 goto op_column_out; 2610 goto op_column_out;
2541 } 2611 }
2542 }else{ 2612 }else{
2543 t = pC->aType[p2]; 2613 t = pC->aType[p2];
2544 } 2614 }
2545 2615
2546 /* Extract the content for the p2+1-th column. Control can only 2616 /* Extract the content for the p2+1-th column. Control can only
2547 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are 2617 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
2548 ** all valid. 2618 ** all valid.
2549 */ 2619 */
2550 assert( p2<pC->nHdrParsed ); 2620 assert( p2<pC->nHdrParsed );
2551 assert( rc==SQLITE_OK ); 2621 assert( rc==SQLITE_OK );
2552 assert( sqlite3VdbeCheckMemInvariants(pDest) ); 2622 assert( sqlite3VdbeCheckMemInvariants(pDest) );
2553 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest); 2623 if( VdbeMemDynamic(pDest) ){
2624 sqlite3VdbeMemSetNull(pDest);
2625 }
2554 assert( t==pC->aType[p2] ); 2626 assert( t==pC->aType[p2] );
2555 if( pC->szRow>=aOffset[p2+1] ){ 2627 if( pC->szRow>=aOffset[p2+1] ){
2556 /* This is the common case where the desired content fits on the original 2628 /* This is the common case where the desired content fits on the original
2557 ** page - where the content is not on an overflow page */ 2629 ** page - where the content is not on an overflow page */
2558 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest); 2630 zData = pC->aRow + aOffset[p2];
2631 if( t<12 ){
2632 sqlite3VdbeSerialGet(zData, t, pDest);
2633 }else{
2634 /* If the column value is a string, we need a persistent value, not
2635 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2636 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2637 */
2638 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2639 pDest->n = len = (t-12)/2;
2640 pDest->enc = encoding;
2641 if( pDest->szMalloc < len+2 ){
2642 pDest->flags = MEM_Null;
2643 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2644 }else{
2645 pDest->z = pDest->zMalloc;
2646 }
2647 memcpy(pDest->z, zData, len);
2648 pDest->z[len] = 0;
2649 pDest->z[len+1] = 0;
2650 pDest->flags = aFlag[t&1];
2651 }
2559 }else{ 2652 }else{
2653 pDest->enc = encoding;
2560 /* This branch happens only when content is on overflow pages */ 2654 /* This branch happens only when content is on overflow pages */
2561 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 2655 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2562 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) 2656 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2563 || (len = sqlite3VdbeSerialTypeLen(t))==0 2657 || (len = sqlite3VdbeSerialTypeLen(t))==0
2564 ){ 2658 ){
2565 /* Content is irrelevant for 2659 /* Content is irrelevant for
2566 ** 1. the typeof() function, 2660 ** 1. the typeof() function,
2567 ** 2. the length(X) function if X is a blob, and 2661 ** 2. the length(X) function if X is a blob, and
2568 ** 3. if the content length is zero. 2662 ** 3. if the content length is zero.
2569 ** So we might as well use bogus content rather than reading 2663 ** So we might as well use bogus content rather than reading
2570 ** content from disk. NULL will work for the value for strings 2664 ** content from disk. */
2571 ** and blobs and whatever is in the payloadSize64 variable 2665 static u8 aZero[8]; /* This is the bogus content */
2572 ** will work for everything else. */ 2666 sqlite3VdbeSerialGet(aZero, t, pDest);
2573 sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
2574 }else{ 2667 }else{
2575 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable, 2668 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
2576 pDest); 2669 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2577 if( rc!=SQLITE_OK ){
2578 goto op_column_error;
2579 }
2580 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); 2670 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2581 pDest->flags &= ~MEM_Ephem; 2671 pDest->flags &= ~MEM_Ephem;
2582 } 2672 }
2583 } 2673 }
2584 pDest->enc = encoding;
2585 2674
2586 op_column_out: 2675 op_column_out:
2587 /* If the column value is an ephemeral string, go ahead and persist
2588 ** that string in case the cursor moves before the column value is
2589 ** used. The following code does the equivalent of Deephemeralize()
2590 ** but does it faster. */
2591 if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
2592 fx = pDest->flags & (MEM_Str|MEM_Blob);
2593 assert( fx!=0 );
2594 zData = (const u8*)pDest->z;
2595 len = pDest->n;
2596 if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
2597 memcpy(pDest->z, zData, len);
2598 pDest->z[len] = 0;
2599 pDest->z[len+1] = 0;
2600 pDest->flags = fx|MEM_Term;
2601 }
2602 op_column_error:
2603 UPDATE_MAX_BLOBSIZE(pDest); 2676 UPDATE_MAX_BLOBSIZE(pDest);
2604 REGISTER_TRACE(pOp->p3, pDest); 2677 REGISTER_TRACE(pOp->p3, pDest);
2605 break; 2678 break;
2606 } 2679 }
2607 2680
2608 /* Opcode: Affinity P1 P2 * P4 * 2681 /* Opcode: Affinity P1 P2 * P4 *
2609 ** Synopsis: affinity(r[P1@P2]) 2682 ** Synopsis: affinity(r[P1@P2])
2610 ** 2683 **
2611 ** Apply affinities to a range of P2 registers starting with P1. 2684 ** Apply affinities to a range of P2 registers starting with P1.
2612 ** 2685 **
2613 ** P4 is a string that is P2 characters long. The nth character of the 2686 ** P4 is a string that is P2 characters long. The nth character of the
2614 ** string indicates the column affinity that should be used for the nth 2687 ** string indicates the column affinity that should be used for the nth
2615 ** memory cell in the range. 2688 ** memory cell in the range.
2616 */ 2689 */
2617 case OP_Affinity: { 2690 case OP_Affinity: {
2618 const char *zAffinity; /* The affinity to be applied */ 2691 const char *zAffinity; /* The affinity to be applied */
2619 char cAff; /* A single character of affinity */ 2692 char cAff; /* A single character of affinity */
2620 2693
2621 zAffinity = pOp->p4.z; 2694 zAffinity = pOp->p4.z;
2622 assert( zAffinity!=0 ); 2695 assert( zAffinity!=0 );
2623 assert( zAffinity[pOp->p2]==0 ); 2696 assert( zAffinity[pOp->p2]==0 );
2624 pIn1 = &aMem[pOp->p1]; 2697 pIn1 = &aMem[pOp->p1];
2625 while( (cAff = *(zAffinity++))!=0 ){ 2698 while( (cAff = *(zAffinity++))!=0 ){
2626 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] ); 2699 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
2627 assert( memIsValid(pIn1) ); 2700 assert( memIsValid(pIn1) );
2628 applyAffinity(pIn1, cAff, encoding); 2701 applyAffinity(pIn1, cAff, encoding);
2629 pIn1++; 2702 pIn1++;
2630 } 2703 }
2631 break; 2704 break;
2632 } 2705 }
2633 2706
2634 /* Opcode: MakeRecord P1 P2 P3 P4 * 2707 /* Opcode: MakeRecord P1 P2 P3 P4 *
2635 ** Synopsis: r[P3]=mkrec(r[P1@P2]) 2708 ** Synopsis: r[P3]=mkrec(r[P1@P2])
2636 ** 2709 **
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2678 ** Each type field is a varint representing the serial type of the 2751 ** Each type field is a varint representing the serial type of the
2679 ** corresponding data element (see sqlite3VdbeSerialType()). The 2752 ** corresponding data element (see sqlite3VdbeSerialType()). The
2680 ** hdr-size field is also a varint which is the offset from the beginning 2753 ** hdr-size field is also a varint which is the offset from the beginning
2681 ** of the record to data0. 2754 ** of the record to data0.
2682 */ 2755 */
2683 nData = 0; /* Number of bytes of data space */ 2756 nData = 0; /* Number of bytes of data space */
2684 nHdr = 0; /* Number of bytes of header space */ 2757 nHdr = 0; /* Number of bytes of header space */
2685 nZero = 0; /* Number of zero bytes at the end of the record */ 2758 nZero = 0; /* Number of zero bytes at the end of the record */
2686 nField = pOp->p1; 2759 nField = pOp->p1;
2687 zAffinity = pOp->p4.z; 2760 zAffinity = pOp->p4.z;
2688 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 ); 2761 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
2689 pData0 = &aMem[nField]; 2762 pData0 = &aMem[nField];
2690 nField = pOp->p2; 2763 nField = pOp->p2;
2691 pLast = &pData0[nField-1]; 2764 pLast = &pData0[nField-1];
2692 file_format = p->minWriteFileFormat; 2765 file_format = p->minWriteFileFormat;
2693 2766
2694 /* Identify the output register */ 2767 /* Identify the output register */
2695 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); 2768 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2696 pOut = &aMem[pOp->p3]; 2769 pOut = &aMem[pOp->p3];
2697 memAboutToChange(p, pOut); 2770 memAboutToChange(p, pOut);
2698 2771
2699 /* Apply the requested affinity to all inputs 2772 /* Apply the requested affinity to all inputs
2700 */ 2773 */
2701 assert( pData0<=pLast ); 2774 assert( pData0<=pLast );
2702 if( zAffinity ){ 2775 if( zAffinity ){
2703 pRec = pData0; 2776 pRec = pData0;
2704 do{ 2777 do{
2705 applyAffinity(pRec++, *(zAffinity++), encoding); 2778 applyAffinity(pRec++, *(zAffinity++), encoding);
2706 assert( zAffinity[0]==0 || pRec<=pLast ); 2779 assert( zAffinity[0]==0 || pRec<=pLast );
2707 }while( zAffinity[0] ); 2780 }while( zAffinity[0] );
2708 } 2781 }
2709 2782
2783 #ifdef SQLITE_ENABLE_NULL_TRIM
2784 /* NULLs can be safely trimmed from the end of the record, as long as
2785 ** as the schema format is 2 or more and none of the omitted columns
2786 ** have a non-NULL default value. Also, the record must be left with
2787 ** at least one field. If P5>0 then it will be one more than the
2788 ** index of the right-most column with a non-NULL default value */
2789 if( pOp->p5 ){
2790 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2791 pLast--;
2792 nField--;
2793 }
2794 }
2795 #endif
2796
2710 /* Loop through the elements that will make up the record to figure 2797 /* Loop through the elements that will make up the record to figure
2711 ** out how much space is required for the new record. 2798 ** out how much space is required for the new record.
2712 */ 2799 */
2713 pRec = pLast; 2800 pRec = pLast;
2714 do{ 2801 do{
2715 assert( memIsValid(pRec) ); 2802 assert( memIsValid(pRec) );
2716 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); 2803 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
2717 if( pRec->flags & MEM_Zero ){ 2804 if( pRec->flags & MEM_Zero ){
2718 if( nData ){ 2805 if( nData ){
2719 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; 2806 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
2720 }else{ 2807 }else{
2721 nZero += pRec->u.nZero; 2808 nZero += pRec->u.nZero;
2722 len -= pRec->u.nZero; 2809 len -= pRec->u.nZero;
2723 } 2810 }
2724 } 2811 }
2725 nData += len; 2812 nData += len;
2726 testcase( serial_type==127 ); 2813 testcase( serial_type==127 );
2727 testcase( serial_type==128 ); 2814 testcase( serial_type==128 );
2728 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); 2815 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
2729 }while( (--pRec)>=pData0 ); 2816 if( pRec==pData0 ) break;
2817 pRec--;
2818 }while(1);
2730 2819
2731 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint 2820 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2732 ** which determines the total number of bytes in the header. The varint 2821 ** which determines the total number of bytes in the header. The varint
2733 ** value is the size of the header in bytes including the size varint 2822 ** value is the size of the header in bytes including the size varint
2734 ** itself. */ 2823 ** itself. */
2735 testcase( nHdr==126 ); 2824 testcase( nHdr==126 );
2736 testcase( nHdr==127 ); 2825 testcase( nHdr==127 );
2737 if( nHdr<=126 ){ 2826 if( nHdr<=126 ){
2738 /* The common case */ 2827 /* The common case */
2739 nHdr += 1; 2828 nHdr += 1;
(...skipping 28 matching lines...) Expand all
2768 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more 2857 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2769 ** additional varints, one per column. */ 2858 ** additional varints, one per column. */
2770 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ 2859 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
2771 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record 2860 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2772 ** immediately follow the header. */ 2861 ** immediately follow the header. */
2773 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ 2862 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
2774 }while( (++pRec)<=pLast ); 2863 }while( (++pRec)<=pLast );
2775 assert( i==nHdr ); 2864 assert( i==nHdr );
2776 assert( j==nByte ); 2865 assert( j==nByte );
2777 2866
2778 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); 2867 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
2779 pOut->n = (int)nByte; 2868 pOut->n = (int)nByte;
2780 pOut->flags = MEM_Blob; 2869 pOut->flags = MEM_Blob;
2781 if( nZero ){ 2870 if( nZero ){
2782 pOut->u.nZero = nZero; 2871 pOut->u.nZero = nZero;
2783 pOut->flags |= MEM_Zero; 2872 pOut->flags |= MEM_Zero;
2784 } 2873 }
2785 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ 2874 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
2786 REGISTER_TRACE(pOp->p3, pOut); 2875 REGISTER_TRACE(pOp->p3, pOut);
2787 UPDATE_MAX_BLOBSIZE(pOut); 2876 UPDATE_MAX_BLOBSIZE(pOut);
2788 break; 2877 break;
2789 } 2878 }
2790 2879
2791 /* Opcode: Count P1 P2 * * * 2880 /* Opcode: Count P1 P2 * * *
2792 ** Synopsis: r[P2]=count() 2881 ** Synopsis: r[P2]=count()
2793 ** 2882 **
2794 ** Store the number of entries (an integer value) in the table or index 2883 ** Store the number of entries (an integer value) in the table or index
2795 ** opened by cursor P1 in register P2 2884 ** opened by cursor P1 in register P2
2796 */ 2885 */
2797 #ifndef SQLITE_OMIT_BTREECOUNT 2886 #ifndef SQLITE_OMIT_BTREECOUNT
2798 case OP_Count: { /* out2 */ 2887 case OP_Count: { /* out2 */
2799 i64 nEntry; 2888 i64 nEntry;
2800 BtCursor *pCrsr; 2889 BtCursor *pCrsr;
2801 2890
2802 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); 2891 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2803 pCrsr = p->apCsr[pOp->p1]->uc.pCursor; 2892 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
2804 assert( pCrsr ); 2893 assert( pCrsr );
2805 nEntry = 0; /* Not needed. Only used to silence a warning. */ 2894 nEntry = 0; /* Not needed. Only used to silence a warning. */
2806 rc = sqlite3BtreeCount(pCrsr, &nEntry); 2895 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2896 if( rc ) goto abort_due_to_error;
2807 pOut = out2Prerelease(p, pOp); 2897 pOut = out2Prerelease(p, pOp);
2808 pOut->u.i = nEntry; 2898 pOut->u.i = nEntry;
2809 break; 2899 break;
2810 } 2900 }
2811 #endif 2901 #endif
2812 2902
2813 /* Opcode: Savepoint P1 * * P4 * 2903 /* Opcode: Savepoint P1 * * P4 *
2814 ** 2904 **
2815 ** Open, release or rollback the savepoint named by parameter P4, depending 2905 ** Open, release or rollback the savepoint named by parameter P4, depending
2816 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an 2906 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2853 ** savepoint (and therefore should not prompt xSavepoint()) callbacks. 2943 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2854 ** If this is a transaction savepoint being opened, it is guaranteed 2944 ** If this is a transaction savepoint being opened, it is guaranteed
2855 ** that the db->aVTrans[] array is empty. */ 2945 ** that the db->aVTrans[] array is empty. */
2856 assert( db->autoCommit==0 || db->nVTrans==0 ); 2946 assert( db->autoCommit==0 || db->nVTrans==0 );
2857 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, 2947 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2858 db->nStatement+db->nSavepoint); 2948 db->nStatement+db->nSavepoint);
2859 if( rc!=SQLITE_OK ) goto abort_due_to_error; 2949 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2860 #endif 2950 #endif
2861 2951
2862 /* Create a new savepoint structure. */ 2952 /* Create a new savepoint structure. */
2863 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1); 2953 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
2864 if( pNew ){ 2954 if( pNew ){
2865 pNew->zName = (char *)&pNew[1]; 2955 pNew->zName = (char *)&pNew[1];
2866 memcpy(pNew->zName, zName, nName+1); 2956 memcpy(pNew->zName, zName, nName+1);
2867 2957
2868 /* If there is no open transaction, then mark this as a special 2958 /* If there is no open transaction, then mark this as a special
2869 ** "transaction savepoint". */ 2959 ** "transaction savepoint". */
2870 if( db->autoCommit ){ 2960 if( db->autoCommit ){
2871 db->autoCommit = 0; 2961 db->autoCommit = 0;
2872 db->isTransactionSavepoint = 1; 2962 db->isTransactionSavepoint = 1;
2873 }else{ 2963 }else{
2874 db->nSavepoint++; 2964 db->nSavepoint++;
2875 } 2965 }
2876 2966
2877 /* Link the new savepoint into the database handle's list. */ 2967 /* Link the new savepoint into the database handle's list. */
2878 pNew->pNext = db->pSavepoint; 2968 pNew->pNext = db->pSavepoint;
2879 db->pSavepoint = pNew; 2969 db->pSavepoint = pNew;
2880 pNew->nDeferredCons = db->nDeferredCons; 2970 pNew->nDeferredCons = db->nDeferredCons;
2881 pNew->nDeferredImmCons = db->nDeferredImmCons; 2971 pNew->nDeferredImmCons = db->nDeferredImmCons;
2882 } 2972 }
2883 } 2973 }
2884 }else{ 2974 }else{
2885 iSavepoint = 0; 2975 iSavepoint = 0;
2886 2976
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2974 db->nDeferredCons = pSavepoint->nDeferredCons; 3064 db->nDeferredCons = pSavepoint->nDeferredCons;
2975 db->nDeferredImmCons = pSavepoint->nDeferredImmCons; 3065 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
2976 } 3066 }
2977 3067
2978 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ 3068 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
2979 rc = sqlite3VtabSavepoint(db, p1, iSavepoint); 3069 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2980 if( rc!=SQLITE_OK ) goto abort_due_to_error; 3070 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2981 } 3071 }
2982 } 3072 }
2983 } 3073 }
3074 if( rc ) goto abort_due_to_error;
2984 3075
2985 break; 3076 break;
2986 } 3077 }
2987 3078
2988 /* Opcode: AutoCommit P1 P2 * * * 3079 /* Opcode: AutoCommit P1 P2 * * *
2989 ** 3080 **
2990 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll 3081 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
2991 ** back any currently active btree transactions. If there are any active 3082 ** back any currently active btree transactions. If there are any active
2992 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if 3083 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2993 ** there are active writing VMs or active VMs that use shared cache. 3084 ** there are active writing VMs or active VMs that use shared cache.
2994 ** 3085 **
2995 ** This instruction causes the VM to halt. 3086 ** This instruction causes the VM to halt.
2996 */ 3087 */
2997 case OP_AutoCommit: { 3088 case OP_AutoCommit: {
2998 int desiredAutoCommit; 3089 int desiredAutoCommit;
2999 int iRollback; 3090 int iRollback;
3000 int turnOnAC;
3001 3091
3002 desiredAutoCommit = pOp->p1; 3092 desiredAutoCommit = pOp->p1;
3003 iRollback = pOp->p2; 3093 iRollback = pOp->p2;
3004 turnOnAC = desiredAutoCommit && !db->autoCommit;
3005 assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); 3094 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
3006 assert( desiredAutoCommit==1 || iRollback==0 ); 3095 assert( desiredAutoCommit==1 || iRollback==0 );
3007 assert( db->nVdbeActive>0 ); /* At least this one VM is active */ 3096 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
3008 assert( p->bIsReader ); 3097 assert( p->bIsReader );
3009 3098
3010 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){ 3099 if( desiredAutoCommit!=db->autoCommit ){
3011 /* If this instruction implements a COMMIT and other VMs are writing
3012 ** return an error indicating that the other VMs must complete first.
3013 */
3014 sqlite3VdbeError(p, "cannot commit transaction - "
3015 "SQL statements in progress");
3016 rc = SQLITE_BUSY;
3017 }else if( desiredAutoCommit!=db->autoCommit ){
3018 if( iRollback ){ 3100 if( iRollback ){
3019 assert( desiredAutoCommit==1 ); 3101 assert( desiredAutoCommit==1 );
3020 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 3102 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
3021 db->autoCommit = 1; 3103 db->autoCommit = 1;
3104 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3105 /* If this instruction implements a COMMIT and other VMs are writing
3106 ** return an error indicating that the other VMs must complete first.
3107 */
3108 sqlite3VdbeError(p, "cannot commit transaction - "
3109 "SQL statements in progress");
3110 rc = SQLITE_BUSY;
3111 goto abort_due_to_error;
3022 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 3112 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
3023 goto vdbe_return; 3113 goto vdbe_return;
3024 }else{ 3114 }else{
3025 db->autoCommit = (u8)desiredAutoCommit; 3115 db->autoCommit = (u8)desiredAutoCommit;
3026 } 3116 }
3027 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 3117 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3028 p->pc = (int)(pOp - aOp); 3118 p->pc = (int)(pOp - aOp);
3029 db->autoCommit = (u8)(1-desiredAutoCommit); 3119 db->autoCommit = (u8)(1-desiredAutoCommit);
3030 p->rc = rc = SQLITE_BUSY; 3120 p->rc = rc = SQLITE_BUSY;
3031 goto vdbe_return; 3121 goto vdbe_return;
3032 } 3122 }
3033 assert( db->nStatement==0 ); 3123 assert( db->nStatement==0 );
3034 sqlite3CloseSavepoints(db); 3124 sqlite3CloseSavepoints(db);
3035 if( p->rc==SQLITE_OK ){ 3125 if( p->rc==SQLITE_OK ){
3036 rc = SQLITE_DONE; 3126 rc = SQLITE_DONE;
3037 }else{ 3127 }else{
3038 rc = SQLITE_ERROR; 3128 rc = SQLITE_ERROR;
3039 } 3129 }
3040 goto vdbe_return; 3130 goto vdbe_return;
3041 }else{ 3131 }else{
3042 sqlite3VdbeError(p, 3132 sqlite3VdbeError(p,
3043 (!desiredAutoCommit)?"cannot start a transaction within a transaction":( 3133 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
3044 (iRollback)?"cannot rollback - no transaction is active": 3134 (iRollback)?"cannot rollback - no transaction is active":
3045 "cannot commit - no transaction is active")); 3135 "cannot commit - no transaction is active"));
3046 3136
3047 rc = SQLITE_ERROR; 3137 rc = SQLITE_ERROR;
3138 goto abort_due_to_error;
3048 } 3139 }
3049 break; 3140 break;
3050 } 3141 }
3051 3142
3052 /* Opcode: Transaction P1 P2 P3 P4 P5 3143 /* Opcode: Transaction P1 P2 P3 P4 P5
3053 ** 3144 **
3054 ** Begin a transaction on database P1 if a transaction is not already 3145 ** Begin a transaction on database P1 if a transaction is not already
3055 ** active. 3146 ** active.
3056 ** If P2 is non-zero, then a write-transaction is started, or if a 3147 ** If P2 is non-zero, then a write-transaction is started, or if a
3057 ** read-transaction is already active, it is upgraded to a write-transaction. 3148 ** read-transaction is already active, it is upgraded to a write-transaction.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
3095 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ 3186 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3096 rc = SQLITE_READONLY; 3187 rc = SQLITE_READONLY;
3097 goto abort_due_to_error; 3188 goto abort_due_to_error;
3098 } 3189 }
3099 pBt = db->aDb[pOp->p1].pBt; 3190 pBt = db->aDb[pOp->p1].pBt;
3100 3191
3101 if( pBt ){ 3192 if( pBt ){
3102 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); 3193 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
3103 testcase( rc==SQLITE_BUSY_SNAPSHOT ); 3194 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3104 testcase( rc==SQLITE_BUSY_RECOVERY ); 3195 testcase( rc==SQLITE_BUSY_RECOVERY );
3105 if( (rc&0xff)==SQLITE_BUSY ){
3106 p->pc = (int)(pOp - aOp);
3107 p->rc = rc;
3108 goto vdbe_return;
3109 }
3110 if( rc!=SQLITE_OK ){ 3196 if( rc!=SQLITE_OK ){
3197 if( (rc&0xff)==SQLITE_BUSY ){
3198 p->pc = (int)(pOp - aOp);
3199 p->rc = rc;
3200 goto vdbe_return;
3201 }
3111 goto abort_due_to_error; 3202 goto abort_due_to_error;
3112 } 3203 }
3113 3204
3114 if( pOp->p2 && p->usesStmtJournal 3205 if( pOp->p2 && p->usesStmtJournal
3115 && (db->autoCommit==0 || db->nVdbeRead>1) 3206 && (db->autoCommit==0 || db->nVdbeRead>1)
3116 ){ 3207 ){
3117 assert( sqlite3BtreeIsInTrans(pBt) ); 3208 assert( sqlite3BtreeIsInTrans(pBt) );
3118 if( p->iStatement==0 ){ 3209 if( p->iStatement==0 ){
3119 assert( db->nStatement>=0 && db->nSavepoint>=0 ); 3210 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3120 db->nStatement++; 3211 db->nStatement++;
3121 p->iStatement = db->nSavepoint + db->nStatement; 3212 p->iStatement = db->nSavepoint + db->nStatement;
3122 } 3213 }
3123 3214
3124 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); 3215 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
3125 if( rc==SQLITE_OK ){ 3216 if( rc==SQLITE_OK ){
3126 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); 3217 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3127 } 3218 }
3128 3219
3129 /* Store the current value of the database handles deferred constraint 3220 /* Store the current value of the database handles deferred constraint
3130 ** counter. If the statement transaction needs to be rolled back, 3221 ** counter. If the statement transaction needs to be rolled back,
3131 ** the value of this counter needs to be restored too. */ 3222 ** the value of this counter needs to be restored too. */
3132 p->nStmtDefCons = db->nDeferredCons; 3223 p->nStmtDefCons = db->nDeferredCons;
3133 p->nStmtDefImmCons = db->nDeferredImmCons; 3224 p->nStmtDefImmCons = db->nDeferredImmCons;
3134 } 3225 }
3135 3226
3136 /* Gather the schema version number for checking: 3227 /* Gather the schema version number for checking:
3137 ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite 3228 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3138 ** each time a query is executed to ensure that the internal cache of the 3229 ** version is checked to ensure that the schema has not changed since the
3139 ** schema used when compiling the SQL query matches the schema of the 3230 ** SQL statement was prepared.
3140 ** database against which the compiled query is actually executed.
3141 */ 3231 */
3142 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); 3232 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3143 iGen = db->aDb[pOp->p1].pSchema->iGeneration; 3233 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3144 }else{ 3234 }else{
3145 iGen = iMeta = 0; 3235 iGen = iMeta = 0;
3146 } 3236 }
3147 assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); 3237 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3148 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ 3238 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3149 sqlite3DbFree(db, p->zErrMsg); 3239 sqlite3DbFree(db, p->zErrMsg);
3150 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); 3240 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3151 /* If the schema-cookie from the database file matches the cookie 3241 /* If the schema-cookie from the database file matches the cookie
3152 ** stored with the in-memory representation of the schema, do 3242 ** stored with the in-memory representation of the schema, do
3153 ** not reload the schema from the database file. 3243 ** not reload the schema from the database file.
3154 ** 3244 **
3155 ** If virtual-tables are in use, this is not just an optimization. 3245 ** If virtual-tables are in use, this is not just an optimization.
3156 ** Often, v-tables store their data in other SQLite tables, which 3246 ** Often, v-tables store their data in other SQLite tables, which
3157 ** are queried from within xNext() and other v-table methods using 3247 ** are queried from within xNext() and other v-table methods using
3158 ** prepared queries. If such a query is out-of-date, we do not want to 3248 ** prepared queries. If such a query is out-of-date, we do not want to
3159 ** discard the database schema, as the user code implementing the 3249 ** discard the database schema, as the user code implementing the
3160 ** v-table would have to be ready for the sqlite3_vtab structure itself 3250 ** v-table would have to be ready for the sqlite3_vtab structure itself
3161 ** to be invalidated whenever sqlite3_step() is called from within 3251 ** to be invalidated whenever sqlite3_step() is called from within
3162 ** a v-table method. 3252 ** a v-table method.
3163 */ 3253 */
3164 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ 3254 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3165 sqlite3ResetOneSchema(db, pOp->p1); 3255 sqlite3ResetOneSchema(db, pOp->p1);
3166 } 3256 }
3167 p->expired = 1; 3257 p->expired = 1;
3168 rc = SQLITE_SCHEMA; 3258 rc = SQLITE_SCHEMA;
3169 } 3259 }
3260 if( rc ) goto abort_due_to_error;
3170 break; 3261 break;
3171 } 3262 }
3172 3263
3173 /* Opcode: ReadCookie P1 P2 P3 * * 3264 /* Opcode: ReadCookie P1 P2 P3 * *
3174 ** 3265 **
3175 ** Read cookie number P3 from database P1 and write it into register P2. 3266 ** Read cookie number P3 from database P1 and write it into register P2.
3176 ** P3==1 is the schema version. P3==2 is the database format. 3267 ** P3==1 is the schema version. P3==2 is the database format.
3177 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is 3268 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
3178 ** the main database file and P1==1 is the database file used to store 3269 ** the main database file and P1==1 is the database file used to store
3179 ** temporary tables. 3270 ** temporary tables.
(...skipping 16 matching lines...) Expand all
3196 assert( DbMaskTest(p->btreeMask, iDb) ); 3287 assert( DbMaskTest(p->btreeMask, iDb) );
3197 3288
3198 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); 3289 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
3199 pOut = out2Prerelease(p, pOp); 3290 pOut = out2Prerelease(p, pOp);
3200 pOut->u.i = iMeta; 3291 pOut->u.i = iMeta;
3201 break; 3292 break;
3202 } 3293 }
3203 3294
3204 /* Opcode: SetCookie P1 P2 P3 * * 3295 /* Opcode: SetCookie P1 P2 P3 * *
3205 ** 3296 **
3206 ** Write the content of register P3 (interpreted as an integer) 3297 ** Write the integer value P3 into cookie number P2 of database P1.
3207 ** into cookie number P2 of database P1. P2==1 is the schema version. 3298 ** P2==1 is the schema version. P2==2 is the database format.
3208 ** P2==2 is the database format. P2==3 is the recommended pager cache 3299 ** P2==3 is the recommended pager cache
3209 ** size, and so forth. P1==0 is the main database file and P1==1 is the 3300 ** size, and so forth. P1==0 is the main database file and P1==1 is the
3210 ** database file used to store temporary tables. 3301 ** database file used to store temporary tables.
3211 ** 3302 **
3212 ** A transaction must be started before executing this opcode. 3303 ** A transaction must be started before executing this opcode.
3213 */ 3304 */
3214 case OP_SetCookie: { /* in3 */ 3305 case OP_SetCookie: {
3215 Db *pDb; 3306 Db *pDb;
3216 assert( pOp->p2<SQLITE_N_BTREE_META ); 3307 assert( pOp->p2<SQLITE_N_BTREE_META );
3217 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 3308 assert( pOp->p1>=0 && pOp->p1<db->nDb );
3218 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 3309 assert( DbMaskTest(p->btreeMask, pOp->p1) );
3219 assert( p->readOnly==0 ); 3310 assert( p->readOnly==0 );
3220 pDb = &db->aDb[pOp->p1]; 3311 pDb = &db->aDb[pOp->p1];
3221 assert( pDb->pBt!=0 ); 3312 assert( pDb->pBt!=0 );
3222 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); 3313 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
3223 pIn3 = &aMem[pOp->p3];
3224 sqlite3VdbeMemIntegerify(pIn3);
3225 /* See note about index shifting on OP_ReadCookie */ 3314 /* See note about index shifting on OP_ReadCookie */
3226 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i); 3315 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
3227 if( pOp->p2==BTREE_SCHEMA_VERSION ){ 3316 if( pOp->p2==BTREE_SCHEMA_VERSION ){
3228 /* When the schema cookie changes, record the new cookie internally */ 3317 /* When the schema cookie changes, record the new cookie internally */
3229 pDb->pSchema->schema_cookie = (int)pIn3->u.i; 3318 pDb->pSchema->schema_cookie = pOp->p3;
3230 db->flags |= SQLITE_InternChanges; 3319 db->flags |= SQLITE_InternChanges;
3231 }else if( pOp->p2==BTREE_FILE_FORMAT ){ 3320 }else if( pOp->p2==BTREE_FILE_FORMAT ){
3232 /* Record changes in the file format */ 3321 /* Record changes in the file format */
3233 pDb->pSchema->file_format = (u8)pIn3->u.i; 3322 pDb->pSchema->file_format = pOp->p3;
3234 } 3323 }
3235 if( pOp->p1==1 ){ 3324 if( pOp->p1==1 ){
3236 /* Invalidate all prepared statements whenever the TEMP database 3325 /* Invalidate all prepared statements whenever the TEMP database
3237 ** schema is changed. Ticket #1644 */ 3326 ** schema is changed. Ticket #1644 */
3238 sqlite3ExpirePreparedStatements(db); 3327 sqlite3ExpirePreparedStatements(db);
3239 p->expired = 0; 3328 p->expired = 0;
3240 } 3329 }
3330 if( rc ) goto abort_due_to_error;
3241 break; 3331 break;
3242 } 3332 }
3243 3333
3244 /* Opcode: OpenRead P1 P2 P3 P4 P5 3334 /* Opcode: OpenRead P1 P2 P3 P4 P5
3245 ** Synopsis: root=P2 iDb=P3 3335 ** Synopsis: root=P2 iDb=P3
3246 ** 3336 **
3247 ** Open a read-only cursor for the database table whose root page is 3337 ** Open a read-only cursor for the database table whose root page is
3248 ** P2 in a database file. The database file is determined by P3. 3338 ** P2 in a database file. The database file is determined by P3.
3249 ** P3==0 means the main database, P3==1 means the database used for 3339 ** P3==0 means the main database, P3==1 means the database used for
3250 ** temporary tables, and P3>1 means used the corresponding attached 3340 ** temporary tables, and P3>1 means used the corresponding attached
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
3328 case OP_OpenRead: 3418 case OP_OpenRead:
3329 case OP_OpenWrite: 3419 case OP_OpenWrite:
3330 3420
3331 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 3421 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
3332 assert( p->bIsReader ); 3422 assert( p->bIsReader );
3333 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx 3423 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3334 || p->readOnly==0 ); 3424 || p->readOnly==0 );
3335 3425
3336 if( p->expired ){ 3426 if( p->expired ){
3337 rc = SQLITE_ABORT_ROLLBACK; 3427 rc = SQLITE_ABORT_ROLLBACK;
3338 break; 3428 goto abort_due_to_error;
3339 } 3429 }
3340 3430
3341 nField = 0; 3431 nField = 0;
3342 pKeyInfo = 0; 3432 pKeyInfo = 0;
3343 p2 = pOp->p2; 3433 p2 = pOp->p2;
3344 iDb = pOp->p3; 3434 iDb = pOp->p3;
3345 assert( iDb>=0 && iDb<db->nDb ); 3435 assert( iDb>=0 && iDb<db->nDb );
3346 assert( DbMaskTest(p->btreeMask, iDb) ); 3436 assert( DbMaskTest(p->btreeMask, iDb) );
3347 pDb = &db->aDb[iDb]; 3437 pDb = &db->aDb[iDb];
3348 pX = pDb->pBt; 3438 pX = pDb->pBt;
3349 assert( pX!=0 ); 3439 assert( pX!=0 );
3350 if( pOp->opcode==OP_OpenWrite ){ 3440 if( pOp->opcode==OP_OpenWrite ){
3351 assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); 3441 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3352 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); 3442 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
3353 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 3443 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3354 if( pDb->pSchema->file_format < p->minWriteFileFormat ){ 3444 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3355 p->minWriteFileFormat = pDb->pSchema->file_format; 3445 p->minWriteFileFormat = pDb->pSchema->file_format;
3356 } 3446 }
3357 }else{ 3447 }else{
3358 wrFlag = 0; 3448 wrFlag = 0;
3359 } 3449 }
3360 if( pOp->p5 & OPFLAG_P2ISREG ){ 3450 if( pOp->p5 & OPFLAG_P2ISREG ){
3361 assert( p2>0 ); 3451 assert( p2>0 );
3362 assert( p2<=(p->nMem-p->nCursor) ); 3452 assert( p2<=(p->nMem+1 - p->nCursor) );
3363 pIn2 = &aMem[p2]; 3453 pIn2 = &aMem[p2];
3364 assert( memIsValid(pIn2) ); 3454 assert( memIsValid(pIn2) );
3365 assert( (pIn2->flags & MEM_Int)!=0 ); 3455 assert( (pIn2->flags & MEM_Int)!=0 );
3366 sqlite3VdbeMemIntegerify(pIn2); 3456 sqlite3VdbeMemIntegerify(pIn2);
3367 p2 = (int)pIn2->u.i; 3457 p2 = (int)pIn2->u.i;
3368 /* The p2 value always comes from a prior OP_CreateTable opcode and 3458 /* The p2 value always comes from a prior OP_CreateTable opcode and
3369 ** that opcode will always set the p2 value to 2 or more or else fail. 3459 ** that opcode will always set the p2 value to 2 or more or else fail.
3370 ** If there were a failure, the prepared statement would have halted 3460 ** If there were a failure, the prepared statement would have halted
3371 ** before reaching this instruction. */ 3461 ** before reaching this instruction. */
3372 if( NEVER(p2<2) ) { 3462 assert( p2>=2 );
3373 rc = SQLITE_CORRUPT_BKPT;
3374 goto abort_due_to_error;
3375 }
3376 } 3463 }
3377 if( pOp->p4type==P4_KEYINFO ){ 3464 if( pOp->p4type==P4_KEYINFO ){
3378 pKeyInfo = pOp->p4.pKeyInfo; 3465 pKeyInfo = pOp->p4.pKeyInfo;
3379 assert( pKeyInfo->enc==ENC(db) ); 3466 assert( pKeyInfo->enc==ENC(db) );
3380 assert( pKeyInfo->db==db ); 3467 assert( pKeyInfo->db==db );
3381 nField = pKeyInfo->nField+pKeyInfo->nXField; 3468 nField = pKeyInfo->nField+pKeyInfo->nXField;
3382 }else if( pOp->p4type==P4_INT32 ){ 3469 }else if( pOp->p4type==P4_INT32 ){
3383 nField = pOp->p4.i; 3470 nField = pOp->p4.i;
3384 } 3471 }
3385 assert( pOp->p1>=0 ); 3472 assert( pOp->p1>=0 );
3386 assert( nField>=0 ); 3473 assert( nField>=0 );
3387 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ 3474 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
3388 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); 3475 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
3389 if( pCur==0 ) goto no_mem; 3476 if( pCur==0 ) goto no_mem;
3390 pCur->nullRow = 1; 3477 pCur->nullRow = 1;
3391 pCur->isOrdered = 1; 3478 pCur->isOrdered = 1;
3392 pCur->pgnoRoot = p2; 3479 pCur->pgnoRoot = p2;
3480 #ifdef SQLITE_DEBUG
3481 pCur->wrFlag = wrFlag;
3482 #endif
3393 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); 3483 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
3394 pCur->pKeyInfo = pKeyInfo; 3484 pCur->pKeyInfo = pKeyInfo;
3395 /* Set the VdbeCursor.isTable variable. Previous versions of 3485 /* Set the VdbeCursor.isTable variable. Previous versions of
3396 ** SQLite used to check if the root-page flags were sane at this point 3486 ** SQLite used to check if the root-page flags were sane at this point
3397 ** and report database corruption if they were not, but this check has 3487 ** and report database corruption if they were not, but this check has
3398 ** since moved into the btree layer. */ 3488 ** since moved into the btree layer. */
3399 pCur->isTable = pOp->p4type!=P4_KEYINFO; 3489 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3400 3490
3401 open_cursor_set_hints: 3491 open_cursor_set_hints:
3402 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); 3492 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3403 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); 3493 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
3404 testcase( pOp->p5 & OPFLAG_BULKCSR ); 3494 testcase( pOp->p5 & OPFLAG_BULKCSR );
3405 #ifdef SQLITE_ENABLE_CURSOR_HINTS 3495 #ifdef SQLITE_ENABLE_CURSOR_HINTS
3406 testcase( pOp->p2 & OPFLAG_SEEKEQ ); 3496 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3407 #endif 3497 #endif
3408 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, 3498 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
3409 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); 3499 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
3500 if( rc ) goto abort_due_to_error;
3410 break; 3501 break;
3411 } 3502 }
3412 3503
3413 /* Opcode: OpenEphemeral P1 P2 * P4 P5 3504 /* Opcode: OpenEphemeral P1 P2 * P4 P5
3414 ** Synopsis: nColumn=P2 3505 ** Synopsis: nColumn=P2
3415 ** 3506 **
3416 ** Open a new cursor P1 to a transient table. 3507 ** Open a new cursor P1 to a transient table.
3417 ** The cursor is always opened read/write even if 3508 ** The cursor is always opened read/write even if
3418 ** the main database is read-only. The ephemeral 3509 ** the main database is read-only. The ephemeral
3419 ** table is deleted automatically when the cursor is closed. 3510 ** table is deleted automatically when the cursor is closed.
(...skipping 26 matching lines...) Expand all
3446 SQLITE_OPEN_CREATE | 3537 SQLITE_OPEN_CREATE |
3447 SQLITE_OPEN_EXCLUSIVE | 3538 SQLITE_OPEN_EXCLUSIVE |
3448 SQLITE_OPEN_DELETEONCLOSE | 3539 SQLITE_OPEN_DELETEONCLOSE |
3449 SQLITE_OPEN_TRANSIENT_DB; 3540 SQLITE_OPEN_TRANSIENT_DB;
3450 assert( pOp->p1>=0 ); 3541 assert( pOp->p1>=0 );
3451 assert( pOp->p2>=0 ); 3542 assert( pOp->p2>=0 );
3452 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); 3543 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3453 if( pCx==0 ) goto no_mem; 3544 if( pCx==0 ) goto no_mem;
3454 pCx->nullRow = 1; 3545 pCx->nullRow = 1;
3455 pCx->isEphemeral = 1; 3546 pCx->isEphemeral = 1;
3456 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 3547 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3457 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); 3548 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
3458 if( rc==SQLITE_OK ){ 3549 if( rc==SQLITE_OK ){
3459 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); 3550 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
3460 } 3551 }
3461 if( rc==SQLITE_OK ){ 3552 if( rc==SQLITE_OK ){
3462 /* If a transient index is required, create it by calling 3553 /* If a transient index is required, create it by calling
3463 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before 3554 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3464 ** opening it. If a transient table is required, just use the 3555 ** opening it. If a transient table is required, just use the
3465 ** automatically created table with root-page 1 (an BLOB_INTKEY table). 3556 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3466 */ 3557 */
3467 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ 3558 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3468 int pgno; 3559 int pgno;
3469 assert( pOp->p4type==P4_KEYINFO ); 3560 assert( pOp->p4type==P4_KEYINFO );
3470 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); 3561 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
3471 if( rc==SQLITE_OK ){ 3562 if( rc==SQLITE_OK ){
3472 assert( pgno==MASTER_ROOT+1 ); 3563 assert( pgno==MASTER_ROOT+1 );
3473 assert( pKeyInfo->db==db ); 3564 assert( pKeyInfo->db==db );
3474 assert( pKeyInfo->enc==ENC(db) ); 3565 assert( pKeyInfo->enc==ENC(db) );
3475 pCx->pKeyInfo = pKeyInfo; 3566 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
3476 rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR,
3477 pKeyInfo, pCx->uc.pCursor); 3567 pKeyInfo, pCx->uc.pCursor);
3478 } 3568 }
3479 pCx->isTable = 0; 3569 pCx->isTable = 0;
3480 }else{ 3570 }else{
3481 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR, 3571 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
3482 0, pCx->uc.pCursor); 3572 0, pCx->uc.pCursor);
3483 pCx->isTable = 1; 3573 pCx->isTable = 1;
3484 } 3574 }
3485 } 3575 }
3576 if( rc ) goto abort_due_to_error;
3486 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); 3577 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
3487 break; 3578 break;
3488 } 3579 }
3489 3580
3490 /* Opcode: SorterOpen P1 P2 P3 P4 * 3581 /* Opcode: SorterOpen P1 P2 P3 P4 *
3491 ** 3582 **
3492 ** This opcode works like OP_OpenEphemeral except that it opens 3583 ** This opcode works like OP_OpenEphemeral except that it opens
3493 ** a transient index that is specifically designed to sort large 3584 ** a transient index that is specifically designed to sort large
3494 ** tables using an external merge-sort algorithm. 3585 ** tables using an external merge-sort algorithm.
3495 ** 3586 **
3496 ** If argument P3 is non-zero, then it indicates that the sorter may 3587 ** If argument P3 is non-zero, then it indicates that the sorter may
3497 ** assume that a stable sort considering the first P3 fields of each 3588 ** assume that a stable sort considering the first P3 fields of each
3498 ** key is sufficient to produce the required results. 3589 ** key is sufficient to produce the required results.
3499 */ 3590 */
3500 case OP_SorterOpen: { 3591 case OP_SorterOpen: {
3501 VdbeCursor *pCx; 3592 VdbeCursor *pCx;
3502 3593
3503 assert( pOp->p1>=0 ); 3594 assert( pOp->p1>=0 );
3504 assert( pOp->p2>=0 ); 3595 assert( pOp->p2>=0 );
3505 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); 3596 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
3506 if( pCx==0 ) goto no_mem; 3597 if( pCx==0 ) goto no_mem;
3507 pCx->pKeyInfo = pOp->p4.pKeyInfo; 3598 pCx->pKeyInfo = pOp->p4.pKeyInfo;
3508 assert( pCx->pKeyInfo->db==db ); 3599 assert( pCx->pKeyInfo->db==db );
3509 assert( pCx->pKeyInfo->enc==ENC(db) ); 3600 assert( pCx->pKeyInfo->enc==ENC(db) );
3510 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); 3601 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
3602 if( rc ) goto abort_due_to_error;
3511 break; 3603 break;
3512 } 3604 }
3513 3605
3514 /* Opcode: SequenceTest P1 P2 * * * 3606 /* Opcode: SequenceTest P1 P2 * * *
3515 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 3607 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3516 ** 3608 **
3517 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump 3609 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3518 ** to P2. Regardless of whether or not the jump is taken, increment the 3610 ** to P2. Regardless of whether or not the jump is taken, increment the
3519 ** the sequence value. 3611 ** the sequence value.
3520 */ 3612 */
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
3701 assert( pC->uc.pCursor!=0 ); 3793 assert( pC->uc.pCursor!=0 );
3702 oc = pOp->opcode; 3794 oc = pOp->opcode;
3703 eqOnly = 0; 3795 eqOnly = 0;
3704 pC->nullRow = 0; 3796 pC->nullRow = 0;
3705 #ifdef SQLITE_DEBUG 3797 #ifdef SQLITE_DEBUG
3706 pC->seekOp = pOp->opcode; 3798 pC->seekOp = pOp->opcode;
3707 #endif 3799 #endif
3708 3800
3709 if( pC->isTable ){ 3801 if( pC->isTable ){
3710 /* The BTREE_SEEK_EQ flag is only set on index cursors */ 3802 /* The BTREE_SEEK_EQ flag is only set on index cursors */
3711 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 ); 3803 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3804 || CORRUPT_DB );
3712 3805
3713 /* The input value in P3 might be of any type: integer, real, string, 3806 /* The input value in P3 might be of any type: integer, real, string,
3714 ** blob, or NULL. But it needs to be an integer before we can do 3807 ** blob, or NULL. But it needs to be an integer before we can do
3715 ** the seek, so convert it. */ 3808 ** the seek, so convert it. */
3716 pIn3 = &aMem[pOp->p3]; 3809 pIn3 = &aMem[pOp->p3];
3717 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 3810 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
3718 applyNumericAffinity(pIn3, 0); 3811 applyNumericAffinity(pIn3, 0);
3719 } 3812 }
3720 iKey = sqlite3VdbeIntValue(pIn3); 3813 iKey = sqlite3VdbeIntValue(pIn3);
3721 3814
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
3788 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); 3881 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3789 assert( oc!=OP_SeekGT || r.default_rc==-1 ); 3882 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3790 assert( oc!=OP_SeekLE || r.default_rc==-1 ); 3883 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3791 assert( oc!=OP_SeekGE || r.default_rc==+1 ); 3884 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3792 assert( oc!=OP_SeekLT || r.default_rc==+1 ); 3885 assert( oc!=OP_SeekLT || r.default_rc==+1 );
3793 3886
3794 r.aMem = &aMem[pOp->p3]; 3887 r.aMem = &aMem[pOp->p3];
3795 #ifdef SQLITE_DEBUG 3888 #ifdef SQLITE_DEBUG
3796 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } 3889 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3797 #endif 3890 #endif
3798 ExpandBlob(r.aMem);
3799 r.eqSeen = 0; 3891 r.eqSeen = 0;
3800 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); 3892 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
3801 if( rc!=SQLITE_OK ){ 3893 if( rc!=SQLITE_OK ){
3802 goto abort_due_to_error; 3894 goto abort_due_to_error;
3803 } 3895 }
3804 if( eqOnly && r.eqSeen==0 ){ 3896 if( eqOnly && r.eqSeen==0 ){
3805 assert( res!=0 ); 3897 assert( res!=0 );
3806 goto seek_not_found; 3898 goto seek_not_found;
3807 } 3899 }
3808 } 3900 }
(...skipping 28 matching lines...) Expand all
3837 VdbeBranchTaken(res!=0,2); 3929 VdbeBranchTaken(res!=0,2);
3838 if( res ){ 3930 if( res ){
3839 goto jump_to_p2; 3931 goto jump_to_p2;
3840 }else if( eqOnly ){ 3932 }else if( eqOnly ){
3841 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 3933 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3842 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ 3934 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
3843 } 3935 }
3844 break; 3936 break;
3845 } 3937 }
3846 3938
3847 /* Opcode: Seek P1 P2 * * *
3848 ** Synopsis: intkey=r[P2]
3849 **
3850 ** P1 is an open table cursor and P2 is a rowid integer. Arrange
3851 ** for P1 to move so that it points to the rowid given by P2.
3852 **
3853 ** This is actually a deferred seek. Nothing actually happens until
3854 ** the cursor is used to read a record. That way, if no reads
3855 ** occur, no unnecessary I/O happens.
3856 */
3857 case OP_Seek: { /* in2 */
3858 VdbeCursor *pC;
3859
3860 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3861 pC = p->apCsr[pOp->p1];
3862 assert( pC!=0 );
3863 assert( pC->eCurType==CURTYPE_BTREE );
3864 assert( pC->uc.pCursor!=0 );
3865 assert( pC->isTable );
3866 pC->nullRow = 0;
3867 pIn2 = &aMem[pOp->p2];
3868 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3869 pC->deferredMoveto = 1;
3870 break;
3871 }
3872
3873
3874 /* Opcode: Found P1 P2 P3 P4 * 3939 /* Opcode: Found P1 P2 P3 P4 *
3875 ** Synopsis: key=r[P3@P4] 3940 ** Synopsis: key=r[P3@P4]
3876 ** 3941 **
3877 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 3942 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3878 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 3943 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
3879 ** record. 3944 ** record.
3880 ** 3945 **
3881 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 3946 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
3882 ** is a prefix of any entry in P1 then a jump is made to P2 and 3947 ** is a prefix of any entry in P1 then a jump is made to P2 and
3883 ** P1 is left pointing at the matching entry. 3948 ** P1 is left pointing at the matching entry.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
3931 ** See also: NotFound, Found, NotExists 3996 ** See also: NotFound, Found, NotExists
3932 */ 3997 */
3933 case OP_NoConflict: /* jump, in3 */ 3998 case OP_NoConflict: /* jump, in3 */
3934 case OP_NotFound: /* jump, in3 */ 3999 case OP_NotFound: /* jump, in3 */
3935 case OP_Found: { /* jump, in3 */ 4000 case OP_Found: { /* jump, in3 */
3936 int alreadyExists; 4001 int alreadyExists;
3937 int takeJump; 4002 int takeJump;
3938 int ii; 4003 int ii;
3939 VdbeCursor *pC; 4004 VdbeCursor *pC;
3940 int res; 4005 int res;
3941 char *pFree; 4006 UnpackedRecord *pFree;
3942 UnpackedRecord *pIdxKey; 4007 UnpackedRecord *pIdxKey;
3943 UnpackedRecord r; 4008 UnpackedRecord r;
3944 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
3945 4009
3946 #ifdef SQLITE_TEST 4010 #ifdef SQLITE_TEST
3947 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; 4011 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
3948 #endif 4012 #endif
3949 4013
3950 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4014 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3951 assert( pOp->p4type==P4_INT32 ); 4015 assert( pOp->p4type==P4_INT32 );
3952 pC = p->apCsr[pOp->p1]; 4016 pC = p->apCsr[pOp->p1];
3953 assert( pC!=0 ); 4017 assert( pC!=0 );
3954 #ifdef SQLITE_DEBUG 4018 #ifdef SQLITE_DEBUG
3955 pC->seekOp = pOp->opcode; 4019 pC->seekOp = pOp->opcode;
3956 #endif 4020 #endif
3957 pIn3 = &aMem[pOp->p3]; 4021 pIn3 = &aMem[pOp->p3];
3958 assert( pC->eCurType==CURTYPE_BTREE ); 4022 assert( pC->eCurType==CURTYPE_BTREE );
3959 assert( pC->uc.pCursor!=0 ); 4023 assert( pC->uc.pCursor!=0 );
3960 assert( pC->isTable==0 ); 4024 assert( pC->isTable==0 );
3961 pFree = 0;
3962 if( pOp->p4.i>0 ){ 4025 if( pOp->p4.i>0 ){
3963 r.pKeyInfo = pC->pKeyInfo; 4026 r.pKeyInfo = pC->pKeyInfo;
3964 r.nField = (u16)pOp->p4.i; 4027 r.nField = (u16)pOp->p4.i;
3965 r.aMem = pIn3; 4028 r.aMem = pIn3;
4029 #ifdef SQLITE_DEBUG
3966 for(ii=0; ii<r.nField; ii++){ 4030 for(ii=0; ii<r.nField; ii++){
3967 assert( memIsValid(&r.aMem[ii]) ); 4031 assert( memIsValid(&r.aMem[ii]) );
3968 ExpandBlob(&r.aMem[ii]); 4032 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
3969 #ifdef SQLITE_DEBUG
3970 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); 4033 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
4034 }
3971 #endif 4035 #endif
3972 }
3973 pIdxKey = &r; 4036 pIdxKey = &r;
4037 pFree = 0;
3974 }else{ 4038 }else{
3975 pIdxKey = sqlite3VdbeAllocUnpackedRecord( 4039 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
3976 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3977 );
3978 if( pIdxKey==0 ) goto no_mem; 4040 if( pIdxKey==0 ) goto no_mem;
3979 assert( pIn3->flags & MEM_Blob ); 4041 assert( pIn3->flags & MEM_Blob );
3980 ExpandBlob(pIn3); 4042 (void)ExpandBlob(pIn3);
3981 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); 4043 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
3982 } 4044 }
3983 pIdxKey->default_rc = 0; 4045 pIdxKey->default_rc = 0;
3984 takeJump = 0; 4046 takeJump = 0;
3985 if( pOp->opcode==OP_NoConflict ){ 4047 if( pOp->opcode==OP_NoConflict ){
3986 /* For the OP_NoConflict opcode, take the jump if any of the 4048 /* For the OP_NoConflict opcode, take the jump if any of the
3987 ** input fields are NULL, since any key with a NULL will not 4049 ** input fields are NULL, since any key with a NULL will not
3988 ** conflict */ 4050 ** conflict */
3989 for(ii=0; ii<pIdxKey->nField; ii++){ 4051 for(ii=0; ii<pIdxKey->nField; ii++){
3990 if( pIdxKey->aMem[ii].flags & MEM_Null ){ 4052 if( pIdxKey->aMem[ii].flags & MEM_Null ){
3991 takeJump = 1; 4053 takeJump = 1;
3992 break; 4054 break;
3993 } 4055 }
3994 } 4056 }
3995 } 4057 }
3996 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); 4058 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
3997 sqlite3DbFree(db, pFree); 4059 if( pFree ) sqlite3DbFree(db, pFree);
3998 if( rc!=SQLITE_OK ){ 4060 if( rc!=SQLITE_OK ){
3999 break; 4061 goto abort_due_to_error;
4000 } 4062 }
4001 pC->seekResult = res; 4063 pC->seekResult = res;
4002 alreadyExists = (res==0); 4064 alreadyExists = (res==0);
4003 pC->nullRow = 1-alreadyExists; 4065 pC->nullRow = 1-alreadyExists;
4004 pC->deferredMoveto = 0; 4066 pC->deferredMoveto = 0;
4005 pC->cacheStatus = CACHE_STALE; 4067 pC->cacheStatus = CACHE_STALE;
4006 if( pOp->opcode==OP_Found ){ 4068 if( pOp->opcode==OP_Found ){
4007 VdbeBranchTaken(alreadyExists!=0,2); 4069 VdbeBranchTaken(alreadyExists!=0,2);
4008 if( alreadyExists ) goto jump_to_p2; 4070 if( alreadyExists ) goto jump_to_p2;
4009 }else{ 4071 }else{
4010 VdbeBranchTaken(takeJump||alreadyExists==0,2); 4072 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4011 if( takeJump || !alreadyExists ) goto jump_to_p2; 4073 if( takeJump || !alreadyExists ) goto jump_to_p2;
4012 } 4074 }
4013 break; 4075 break;
4014 } 4076 }
4015 4077
4078 /* Opcode: SeekRowid P1 P2 P3 * *
4079 ** Synopsis: intkey=r[P3]
4080 **
4081 ** P1 is the index of a cursor open on an SQL table btree (with integer
4082 ** keys). If register P3 does not contain an integer or if P1 does not
4083 ** contain a record with rowid P3 then jump immediately to P2.
4084 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4085 ** a record with rowid P3 then
4086 ** leave the cursor pointing at that record and fall through to the next
4087 ** instruction.
4088 **
4089 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4090 ** the P3 register must be guaranteed to contain an integer value. With this
4091 ** opcode, register P3 might not contain an integer.
4092 **
4093 ** The OP_NotFound opcode performs the same operation on index btrees
4094 ** (with arbitrary multi-value keys).
4095 **
4096 ** This opcode leaves the cursor in a state where it cannot be advanced
4097 ** in either direction. In other words, the Next and Prev opcodes will
4098 ** not work following this opcode.
4099 **
4100 ** See also: Found, NotFound, NoConflict, SeekRowid
4101 */
4016 /* Opcode: NotExists P1 P2 P3 * * 4102 /* Opcode: NotExists P1 P2 P3 * *
4017 ** Synopsis: intkey=r[P3] 4103 ** Synopsis: intkey=r[P3]
4018 ** 4104 **
4019 ** P1 is the index of a cursor open on an SQL table btree (with integer 4105 ** P1 is the index of a cursor open on an SQL table btree (with integer
4020 ** keys). P3 is an integer rowid. If P1 does not contain a record with 4106 ** keys). P3 is an integer rowid. If P1 does not contain a record with
4021 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an 4107 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4022 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 4108 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4023 ** leave the cursor pointing at that record and fall through to the next 4109 ** leave the cursor pointing at that record and fall through to the next
4024 ** instruction. 4110 ** instruction.
4025 ** 4111 **
4112 ** The OP_SeekRowid opcode performs the same operation but also allows the
4113 ** P3 register to contain a non-integer value, in which case the jump is
4114 ** always taken. This opcode requires that P3 always contain an integer.
4115 **
4026 ** The OP_NotFound opcode performs the same operation on index btrees 4116 ** The OP_NotFound opcode performs the same operation on index btrees
4027 ** (with arbitrary multi-value keys). 4117 ** (with arbitrary multi-value keys).
4028 ** 4118 **
4029 ** This opcode leaves the cursor in a state where it cannot be advanced 4119 ** This opcode leaves the cursor in a state where it cannot be advanced
4030 ** in either direction. In other words, the Next and Prev opcodes will 4120 ** in either direction. In other words, the Next and Prev opcodes will
4031 ** not work following this opcode. 4121 ** not work following this opcode.
4032 ** 4122 **
4033 ** See also: Found, NotFound, NoConflict 4123 ** See also: Found, NotFound, NoConflict, SeekRowid
4034 */ 4124 */
4035 case OP_NotExists: { /* jump, in3 */ 4125 case OP_SeekRowid: { /* jump, in3 */
4036 VdbeCursor *pC; 4126 VdbeCursor *pC;
4037 BtCursor *pCrsr; 4127 BtCursor *pCrsr;
4038 int res; 4128 int res;
4039 u64 iKey; 4129 u64 iKey;
4040 4130
4041 pIn3 = &aMem[pOp->p3]; 4131 pIn3 = &aMem[pOp->p3];
4132 if( (pIn3->flags & MEM_Int)==0 ){
4133 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
4134 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
4135 }
4136 /* Fall through into OP_NotExists */
4137 case OP_NotExists: /* jump, in3 */
4138 pIn3 = &aMem[pOp->p3];
4042 assert( pIn3->flags & MEM_Int ); 4139 assert( pIn3->flags & MEM_Int );
4043 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4140 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4044 pC = p->apCsr[pOp->p1]; 4141 pC = p->apCsr[pOp->p1];
4045 assert( pC!=0 ); 4142 assert( pC!=0 );
4046 #ifdef SQLITE_DEBUG 4143 #ifdef SQLITE_DEBUG
4047 pC->seekOp = 0; 4144 pC->seekOp = 0;
4048 #endif 4145 #endif
4049 assert( pC->isTable ); 4146 assert( pC->isTable );
4050 assert( pC->eCurType==CURTYPE_BTREE ); 4147 assert( pC->eCurType==CURTYPE_BTREE );
4051 pCrsr = pC->uc.pCursor; 4148 pCrsr = pC->uc.pCursor;
4052 assert( pCrsr!=0 ); 4149 assert( pCrsr!=0 );
4053 res = 0; 4150 res = 0;
4054 iKey = pIn3->u.i; 4151 iKey = pIn3->u.i;
4055 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); 4152 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
4056 assert( rc==SQLITE_OK || res==0 ); 4153 assert( rc==SQLITE_OK || res==0 );
4057 pC->movetoTarget = iKey; /* Used by OP_Delete */ 4154 pC->movetoTarget = iKey; /* Used by OP_Delete */
4058 pC->nullRow = 0; 4155 pC->nullRow = 0;
4059 pC->cacheStatus = CACHE_STALE; 4156 pC->cacheStatus = CACHE_STALE;
4060 pC->deferredMoveto = 0; 4157 pC->deferredMoveto = 0;
4061 VdbeBranchTaken(res!=0,2); 4158 VdbeBranchTaken(res!=0,2);
4062 pC->seekResult = res; 4159 pC->seekResult = res;
4063 if( res!=0 ){ 4160 if( res!=0 ){
4064 assert( rc==SQLITE_OK ); 4161 assert( rc==SQLITE_OK );
4065 if( pOp->p2==0 ){ 4162 if( pOp->p2==0 ){
4066 rc = SQLITE_CORRUPT_BKPT; 4163 rc = SQLITE_CORRUPT_BKPT;
4067 }else{ 4164 }else{
4068 goto jump_to_p2; 4165 goto jump_to_p2;
4069 } 4166 }
4070 } 4167 }
4168 if( rc ) goto abort_due_to_error;
4071 break; 4169 break;
4072 } 4170 }
4073 4171
4074 /* Opcode: Sequence P1 P2 * * * 4172 /* Opcode: Sequence P1 P2 * * *
4075 ** Synopsis: r[P2]=cursor[P1].ctr++ 4173 ** Synopsis: r[P2]=cursor[P1].ctr++
4076 ** 4174 **
4077 ** Find the next available sequence number for cursor P1. 4175 ** Find the next available sequence number for cursor P1.
4078 ** Write the sequence number into register P2. 4176 ** Write the sequence number into register P2.
4079 ** The sequence number on the cursor is incremented after this 4177 ** The sequence number on the cursor is incremented after this
4080 ** instruction. 4178 ** instruction.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
4148 4246
4149 if( !pC->useRandomRowid ){ 4247 if( !pC->useRandomRowid ){
4150 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 4248 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4151 if( rc!=SQLITE_OK ){ 4249 if( rc!=SQLITE_OK ){
4152 goto abort_due_to_error; 4250 goto abort_due_to_error;
4153 } 4251 }
4154 if( res ){ 4252 if( res ){
4155 v = 1; /* IMP: R-61914-48074 */ 4253 v = 1; /* IMP: R-61914-48074 */
4156 }else{ 4254 }else{
4157 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); 4255 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
4158 rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); 4256 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4159 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
4160 if( v>=MAX_ROWID ){ 4257 if( v>=MAX_ROWID ){
4161 pC->useRandomRowid = 1; 4258 pC->useRandomRowid = 1;
4162 }else{ 4259 }else{
4163 v++; /* IMP: R-29538-34987 */ 4260 v++; /* IMP: R-29538-34987 */
4164 } 4261 }
4165 } 4262 }
4166 } 4263 }
4167 4264
4168 #ifndef SQLITE_OMIT_AUTOINCREMENT 4265 #ifndef SQLITE_OMIT_AUTOINCREMENT
4169 if( pOp->p3 ){ 4266 if( pOp->p3 ){
4170 /* Assert that P3 is a valid memory cell. */ 4267 /* Assert that P3 is a valid memory cell. */
4171 assert( pOp->p3>0 ); 4268 assert( pOp->p3>0 );
4172 if( p->pFrame ){ 4269 if( p->pFrame ){
4173 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 4270 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
4174 /* Assert that P3 is a valid memory cell. */ 4271 /* Assert that P3 is a valid memory cell. */
4175 assert( pOp->p3<=pFrame->nMem ); 4272 assert( pOp->p3<=pFrame->nMem );
4176 pMem = &pFrame->aMem[pOp->p3]; 4273 pMem = &pFrame->aMem[pOp->p3];
4177 }else{ 4274 }else{
4178 /* Assert that P3 is a valid memory cell. */ 4275 /* Assert that P3 is a valid memory cell. */
4179 assert( pOp->p3<=(p->nMem-p->nCursor) ); 4276 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
4180 pMem = &aMem[pOp->p3]; 4277 pMem = &aMem[pOp->p3];
4181 memAboutToChange(p, pMem); 4278 memAboutToChange(p, pMem);
4182 } 4279 }
4183 assert( memIsValid(pMem) ); 4280 assert( memIsValid(pMem) );
4184 4281
4185 REGISTER_TRACE(pOp->p3, pMem); 4282 REGISTER_TRACE(pOp->p3, pMem);
4186 sqlite3VdbeMemIntegerify(pMem); 4283 sqlite3VdbeMemIntegerify(pMem);
4187 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ 4284 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4188 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ 4285 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
4189 rc = SQLITE_FULL; /* IMP: R-12275-61338 */ 4286 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
4190 goto abort_due_to_error; 4287 goto abort_due_to_error;
4191 } 4288 }
4192 if( v<pMem->u.i+1 ){ 4289 if( v<pMem->u.i+1 ){
4193 v = pMem->u.i + 1; 4290 v = pMem->u.i + 1;
4194 } 4291 }
4195 pMem->u.i = v; 4292 pMem->u.i = v;
4196 } 4293 }
4197 #endif 4294 #endif
4198 if( pC->useRandomRowid ){ 4295 if( pC->useRandomRowid ){
4199 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the 4296 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
4200 ** largest possible integer (9223372036854775807) then the database 4297 ** largest possible integer (9223372036854775807) then the database
4201 ** engine starts picking positive candidate ROWIDs at random until 4298 ** engine starts picking positive candidate ROWIDs at random until
4202 ** it finds one that is not previously used. */ 4299 ** it finds one that is not previously used. */
4203 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is 4300 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4204 ** an AUTOINCREMENT table. */ 4301 ** an AUTOINCREMENT table. */
4205 cnt = 0; 4302 cnt = 0;
4206 do{ 4303 do{
4207 sqlite3_randomness(sizeof(v), &v); 4304 sqlite3_randomness(sizeof(v), &v);
4208 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ 4305 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
4209 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, 4306 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
4210 0, &res))==SQLITE_OK) 4307 0, &res))==SQLITE_OK)
4211 && (res==0) 4308 && (res==0)
4212 && (++cnt<100)); 4309 && (++cnt<100));
4213 if( rc==SQLITE_OK && res==0 ){ 4310 if( rc ) goto abort_due_to_error;
4311 if( res==0 ){
4214 rc = SQLITE_FULL; /* IMP: R-38219-53002 */ 4312 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
4215 goto abort_due_to_error; 4313 goto abort_due_to_error;
4216 } 4314 }
4217 assert( v>0 ); /* EV: R-40812-03570 */ 4315 assert( v>0 ); /* EV: R-40812-03570 */
4218 } 4316 }
4219 pC->deferredMoveto = 0; 4317 pC->deferredMoveto = 0;
4220 pC->cacheStatus = CACHE_STALE; 4318 pC->cacheStatus = CACHE_STALE;
4221 } 4319 }
4222 pOut->u.i = v; 4320 pOut->u.i = v;
4223 break; 4321 break;
4224 } 4322 }
4225 4323
4226 /* Opcode: Insert P1 P2 P3 P4 P5 4324 /* Opcode: Insert P1 P2 P3 P4 P5
4227 ** Synopsis: intkey=r[P3] data=r[P2] 4325 ** Synopsis: intkey=r[P3] data=r[P2]
4228 ** 4326 **
4229 ** Write an entry into the table of cursor P1. A new entry is 4327 ** Write an entry into the table of cursor P1. A new entry is
4230 ** created if it doesn't already exist or the data for an existing 4328 ** created if it doesn't already exist or the data for an existing
4231 ** entry is overwritten. The data is the value MEM_Blob stored in register 4329 ** entry is overwritten. The data is the value MEM_Blob stored in register
4232 ** number P2. The key is stored in register P3. The key must 4330 ** number P2. The key is stored in register P3. The key must
4233 ** be a MEM_Int. 4331 ** be a MEM_Int.
4234 ** 4332 **
4235 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is 4333 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4236 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, 4334 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
4237 ** then rowid is stored for subsequent return by the 4335 ** then rowid is stored for subsequent return by the
4238 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). 4336 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
4239 ** 4337 **
4240 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of 4338 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4241 ** the last seek operation (OP_NotExists) was a success, then this 4339 ** run faster by avoiding an unnecessary seek on cursor P1. However,
4242 ** operation will not attempt to find the appropriate row before doing 4340 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4243 ** the insert but will instead overwrite the row that the cursor is 4341 ** seeks on the cursor or if the most recent seek used a key equal to P3.
4244 ** currently pointing to. Presumably, the prior OP_NotExists opcode
4245 ** has already positioned the cursor correctly. This is an optimization
4246 ** that boosts performance by avoiding redundant seeks.
4247 ** 4342 **
4248 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an 4343 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4249 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode 4344 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4250 ** is part of an INSERT operation. The difference is only important to 4345 ** is part of an INSERT operation. The difference is only important to
4251 ** the update hook. 4346 ** the update hook.
4252 ** 4347 **
4253 ** Parameter P4 may point to a string containing the table-name, or 4348 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
4254 ** may be NULL. If it is not NULL, then the update-hook 4349 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4255 ** (sqlite3.xUpdateCallback) is invoked following a successful insert. 4350 ** following a successful insert.
4256 ** 4351 **
4257 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically 4352 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4258 ** allocated, then ownership of P2 is transferred to the pseudo-cursor 4353 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
4259 ** and register P2 becomes ephemeral. If the cursor is changed, the 4354 ** and register P2 becomes ephemeral. If the cursor is changed, the
4260 ** value of register P2 will then change. Make sure this does not 4355 ** value of register P2 will then change. Make sure this does not
4261 ** cause any problems.) 4356 ** cause any problems.)
4262 ** 4357 **
4263 ** This instruction only works on tables. The equivalent instruction 4358 ** This instruction only works on tables. The equivalent instruction
4264 ** for indices is OP_IdxInsert. 4359 ** for indices is OP_IdxInsert.
4265 */ 4360 */
4266 /* Opcode: InsertInt P1 P2 P3 P4 P5 4361 /* Opcode: InsertInt P1 P2 P3 P4 P5
4267 ** Synopsis: intkey=P3 data=r[P2] 4362 ** Synopsis: intkey=P3 data=r[P2]
4268 ** 4363 **
4269 ** This works exactly like OP_Insert except that the key is the 4364 ** This works exactly like OP_Insert except that the key is the
4270 ** integer value P3, not the value of the integer stored in register P3. 4365 ** integer value P3, not the value of the integer stored in register P3.
4271 */ 4366 */
4272 case OP_Insert: 4367 case OP_Insert:
4273 case OP_InsertInt: { 4368 case OP_InsertInt: {
4274 Mem *pData; /* MEM cell holding data for the record to be inserted */ 4369 Mem *pData; /* MEM cell holding data for the record to be inserted */
4275 Mem *pKey; /* MEM cell holding key for the record */ 4370 Mem *pKey; /* MEM cell holding key for the record */
4276 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4277 VdbeCursor *pC; /* Cursor to table into which insert is written */ 4371 VdbeCursor *pC; /* Cursor to table into which insert is written */
4278 int nZero; /* Number of zero-bytes to append */
4279 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ 4372 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4280 const char *zDb; /* database name - used by the update hook */ 4373 const char *zDb; /* database name - used by the update hook */
4281 const char *zTbl; /* Table name - used by the opdate hook */ 4374 Table *pTab; /* Table structure - used by update and pre-update hooks */
4282 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ 4375 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
4376 BtreePayload x; /* Payload to be inserted */
4283 4377
4378 op = 0;
4284 pData = &aMem[pOp->p2]; 4379 pData = &aMem[pOp->p2];
4285 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4380 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4286 assert( memIsValid(pData) ); 4381 assert( memIsValid(pData) );
4287 pC = p->apCsr[pOp->p1]; 4382 pC = p->apCsr[pOp->p1];
4288 assert( pC!=0 ); 4383 assert( pC!=0 );
4289 assert( pC->eCurType==CURTYPE_BTREE ); 4384 assert( pC->eCurType==CURTYPE_BTREE );
4290 assert( pC->uc.pCursor!=0 ); 4385 assert( pC->uc.pCursor!=0 );
4291 assert( pC->isTable ); 4386 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
4387 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
4292 REGISTER_TRACE(pOp->p2, pData); 4388 REGISTER_TRACE(pOp->p2, pData);
4293 4389
4294 if( pOp->opcode==OP_Insert ){ 4390 if( pOp->opcode==OP_Insert ){
4295 pKey = &aMem[pOp->p3]; 4391 pKey = &aMem[pOp->p3];
4296 assert( pKey->flags & MEM_Int ); 4392 assert( pKey->flags & MEM_Int );
4297 assert( memIsValid(pKey) ); 4393 assert( memIsValid(pKey) );
4298 REGISTER_TRACE(pOp->p3, pKey); 4394 REGISTER_TRACE(pOp->p3, pKey);
4299 iKey = pKey->u.i; 4395 x.nKey = pKey->u.i;
4300 }else{ 4396 }else{
4301 assert( pOp->opcode==OP_InsertInt ); 4397 assert( pOp->opcode==OP_InsertInt );
4302 iKey = pOp->p3; 4398 x.nKey = pOp->p3;
4303 } 4399 }
4304 4400
4401 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
4402 assert( pC->iDb>=0 );
4403 zDb = db->aDb[pC->iDb].zDbSName;
4404 pTab = pOp->p4.pTab;
4405 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
4406 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
4407 }else{
4408 pTab = 0; /* Not needed. Silence a compiler warning. */
4409 zDb = 0; /* Not needed. Silence a compiler warning. */
4410 }
4411
4412 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4413 /* Invoke the pre-update hook, if any */
4414 if( db->xPreUpdateCallback
4415 && pOp->p4type==P4_TABLE
4416 && !(pOp->p5 & OPFLAG_ISUPDATE)
4417 ){
4418 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
4419 }
4420 if( pOp->p5 & OPFLAG_ISNOOP ) break;
4421 #endif
4422
4305 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 4423 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
4306 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey; 4424 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
4307 if( pData->flags & MEM_Null ){ 4425 if( pData->flags & MEM_Null ){
4308 pData->z = 0; 4426 x.pData = 0;
4309 pData->n = 0; 4427 x.nData = 0;
4310 }else{ 4428 }else{
4311 assert( pData->flags & (MEM_Blob|MEM_Str) ); 4429 assert( pData->flags & (MEM_Blob|MEM_Str) );
4430 x.pData = pData->z;
4431 x.nData = pData->n;
4312 } 4432 }
4313 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); 4433 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4314 if( pData->flags & MEM_Zero ){ 4434 if( pData->flags & MEM_Zero ){
4315 nZero = pData->u.nZero; 4435 x.nZero = pData->u.nZero;
4316 }else{ 4436 }else{
4317 nZero = 0; 4437 x.nZero = 0;
4318 } 4438 }
4319 rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey, 4439 x.pKey = 0;
4320 pData->z, pData->n, nZero, 4440 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
4321 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult 4441 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
4322 ); 4442 );
4323 pC->deferredMoveto = 0; 4443 pC->deferredMoveto = 0;
4324 pC->cacheStatus = CACHE_STALE; 4444 pC->cacheStatus = CACHE_STALE;
4325 4445
4326 /* Invoke the update-hook if required. */ 4446 /* Invoke the update-hook if required. */
4327 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ 4447 if( rc ) goto abort_due_to_error;
4328 zDb = db->aDb[pC->iDb].zName; 4448 if( db->xUpdateCallback && op ){
4329 zTbl = pOp->p4.z; 4449 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
4330 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
4331 assert( pC->isTable );
4332 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4333 assert( pC->iDb>=0 );
4334 } 4450 }
4335 break; 4451 break;
4336 } 4452 }
4337 4453
4338 /* Opcode: Delete P1 P2 * P4 P5 4454 /* Opcode: Delete P1 P2 P3 P4 P5
4339 ** 4455 **
4340 ** Delete the record at which the P1 cursor is currently pointing. 4456 ** Delete the record at which the P1 cursor is currently pointing.
4341 ** 4457 **
4342 ** If the P5 parameter is non-zero, the cursor will be left pointing at 4458 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4343 ** either the next or the previous record in the table. If it is left 4459 ** the cursor will be left pointing at either the next or the previous
4344 ** pointing at the next record, then the next Next instruction will be a 4460 ** record in the table. If it is left pointing at the next record, then
4345 ** no-op. As a result, in this case it is OK to delete a record from within a 4461 ** the next Next instruction will be a no-op. As a result, in this case
4346 ** Next loop. If P5 is zero, then the cursor is left in an undefined state. 4462 ** it is ok to delete a record from within a Next loop. If
4463 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4464 ** left in an undefined state.
4347 ** 4465 **
4348 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is 4466 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4349 ** incremented (otherwise not). 4467 ** delete one of several associated with deleting a table row and all its
4468 ** associated index entries. Exactly one of those deletes is the "primary"
4469 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4470 ** marked with the AUXDELETE flag.
4471 **
4472 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4473 ** change count is incremented (otherwise not).
4350 ** 4474 **
4351 ** P1 must not be pseudo-table. It has to be a real table with 4475 ** P1 must not be pseudo-table. It has to be a real table with
4352 ** multiple rows. 4476 ** multiple rows.
4353 ** 4477 **
4354 ** If P4 is not NULL, then it is the name of the table that P1 is 4478 ** If P4 is not NULL then it points to a Table object. In this case either
4355 ** pointing to. The update hook will be invoked, if it exists. 4479 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4356 ** If P4 is not NULL then the P1 cursor must have been positioned 4480 ** have been positioned using OP_NotFound prior to invoking this opcode in
4357 ** using OP_NotFound prior to invoking this opcode. 4481 ** this case. Specifically, if one is configured, the pre-update hook is
4482 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4483 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
4484 **
4485 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4486 ** of the memory cell that contains the value that the rowid of the row will
4487 ** be set to by the update.
4358 */ 4488 */
4359 case OP_Delete: { 4489 case OP_Delete: {
4360 VdbeCursor *pC; 4490 VdbeCursor *pC;
4361 u8 hasUpdateCallback; 4491 const char *zDb;
4492 Table *pTab;
4493 int opflags;
4362 4494
4495 opflags = pOp->p2;
4363 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4496 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4364 pC = p->apCsr[pOp->p1]; 4497 pC = p->apCsr[pOp->p1];
4365 assert( pC!=0 ); 4498 assert( pC!=0 );
4366 assert( pC->eCurType==CURTYPE_BTREE ); 4499 assert( pC->eCurType==CURTYPE_BTREE );
4367 assert( pC->uc.pCursor!=0 ); 4500 assert( pC->uc.pCursor!=0 );
4368 assert( pC->deferredMoveto==0 ); 4501 assert( pC->deferredMoveto==0 );
4369 4502
4370 hasUpdateCallback = db->xUpdateCallback && pOp->p4.z && pC->isTable; 4503 #ifdef SQLITE_DEBUG
4371 if( pOp->p5 && hasUpdateCallback ){ 4504 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4372 sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget); 4505 /* If p5 is zero, the seek operation that positioned the cursor prior to
4506 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4507 ** the row that is being deleted */
4508 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4509 assert( pC->movetoTarget==iKey );
4510 }
4511 #endif
4512
4513 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4514 ** the name of the db to pass as to it. Also set local pTab to a copy
4515 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4516 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4517 ** VdbeCursor.movetoTarget to the current rowid. */
4518 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
4519 assert( pC->iDb>=0 );
4520 assert( pOp->p4.pTab!=0 );
4521 zDb = db->aDb[pC->iDb].zDbSName;
4522 pTab = pOp->p4.pTab;
4523 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
4524 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4525 }
4526 }else{
4527 zDb = 0; /* Not needed. Silence a compiler warning. */
4528 pTab = 0; /* Not needed. Silence a compiler warning. */
4373 } 4529 }
4374 4530
4531 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4532 /* Invoke the pre-update-hook if required. */
4533 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4534 assert( !(opflags & OPFLAG_ISUPDATE)
4535 || HasRowid(pTab)==0
4536 || (aMem[pOp->p3].flags & MEM_Int)
4537 );
4538 sqlite3VdbePreUpdateHook(p, pC,
4539 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
4540 zDb, pTab, pC->movetoTarget,
4541 pOp->p3
4542 );
4543 }
4544 if( opflags & OPFLAG_ISNOOP ) break;
4545 #endif
4546
4547 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4548 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
4549 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
4550 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
4551
4375 #ifdef SQLITE_DEBUG 4552 #ifdef SQLITE_DEBUG
4376 /* The seek operation that positioned the cursor prior to OP_Delete will 4553 if( p->pFrame==0 ){
4377 ** have also set the pC->movetoTarget field to the rowid of the row that 4554 if( pC->isEphemeral==0
4378 ** is being deleted */ 4555 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4379 if( pOp->p4.z && pC->isTable && pOp->p5==0 ){ 4556 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4380 i64 iKey = 0; 4557 ){
4381 sqlite3BtreeKeySize(pC->uc.pCursor, &iKey); 4558 nExtraDelete++;
4382 assert( pC->movetoTarget==iKey ); 4559 }
4560 if( pOp->p2 & OPFLAG_NCHANGE ){
4561 nExtraDelete--;
4562 }
4383 } 4563 }
4384 #endif 4564 #endif
4385 4565
4386 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); 4566 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
4387 pC->cacheStatus = CACHE_STALE; 4567 pC->cacheStatus = CACHE_STALE;
4568 pC->seekResult = 0;
4569 if( rc ) goto abort_due_to_error;
4388 4570
4389 /* Invoke the update-hook if required. */ 4571 /* Invoke the update-hook if required. */
4390 if( rc==SQLITE_OK && hasUpdateCallback ){ 4572 if( opflags & OPFLAG_NCHANGE ){
4391 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, 4573 p->nChange++;
4392 db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget); 4574 if( db->xUpdateCallback && HasRowid(pTab) ){
4393 assert( pC->iDb>=0 ); 4575 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
4576 pC->movetoTarget);
4577 assert( pC->iDb>=0 );
4578 }
4394 } 4579 }
4395 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; 4580
4396 break; 4581 break;
4397 } 4582 }
4398 /* Opcode: ResetCount * * * * * 4583 /* Opcode: ResetCount * * * * *
4399 ** 4584 **
4400 ** The value of the change counter is copied to the database handle 4585 ** The value of the change counter is copied to the database handle
4401 ** change counter (returned by subsequent calls to sqlite3_changes()). 4586 ** change counter (returned by subsequent calls to sqlite3_changes()).
4402 ** Then the VMs internal change counter resets to 0. 4587 ** Then the VMs internal change counter resets to 0.
4403 ** This is used by trigger programs. 4588 ** This is used by trigger programs.
4404 */ 4589 */
4405 case OP_ResetCount: { 4590 case OP_ResetCount: {
4406 sqlite3VdbeSetChanges(db, p->nChange); 4591 sqlite3VdbeSetChanges(db, p->nChange);
4407 p->nChange = 0; 4592 p->nChange = 0;
4408 break; 4593 break;
4409 } 4594 }
4410 4595
4411 /* Opcode: SorterCompare P1 P2 P3 P4 4596 /* Opcode: SorterCompare P1 P2 P3 P4
4412 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 4597 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
4413 ** 4598 **
4414 ** P1 is a sorter cursor. This instruction compares a prefix of the 4599 ** P1 is a sorter cursor. This instruction compares a prefix of the
4415 ** record blob in register P3 against a prefix of the entry that 4600 ** record blob in register P3 against a prefix of the entry that
4416 ** the sorter cursor currently points to. Only the first P4 fields 4601 ** the sorter cursor currently points to. Only the first P4 fields
4417 ** of r[P3] and the sorter record are compared. 4602 ** of r[P3] and the sorter record are compared.
4418 ** 4603 **
4419 ** If either P3 or the sorter contains a NULL in one of their significant 4604 ** If either P3 or the sorter contains a NULL in one of their significant
4420 ** fields (not counting the P4 fields at the end which are ignored) then 4605 ** fields (not counting the P4 fields at the end which are ignored) then
4421 ** the comparison is assumed to be equal. 4606 ** the comparison is assumed to be equal.
4422 ** 4607 **
4423 ** Fall through to next instruction if the two records compare equal to 4608 ** Fall through to next instruction if the two records compare equal to
4424 ** each other. Jump to P2 if they are different. 4609 ** each other. Jump to P2 if they are different.
4425 */ 4610 */
4426 case OP_SorterCompare: { 4611 case OP_SorterCompare: {
4427 VdbeCursor *pC; 4612 VdbeCursor *pC;
4428 int res; 4613 int res;
4429 int nKeyCol; 4614 int nKeyCol;
4430 4615
4431 pC = p->apCsr[pOp->p1]; 4616 pC = p->apCsr[pOp->p1];
4432 assert( isSorter(pC) ); 4617 assert( isSorter(pC) );
4433 assert( pOp->p4type==P4_INT32 ); 4618 assert( pOp->p4type==P4_INT32 );
4434 pIn3 = &aMem[pOp->p3]; 4619 pIn3 = &aMem[pOp->p3];
4435 nKeyCol = pOp->p4.i; 4620 nKeyCol = pOp->p4.i;
4436 res = 0; 4621 res = 0;
4437 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); 4622 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
4438 VdbeBranchTaken(res!=0,2); 4623 VdbeBranchTaken(res!=0,2);
4624 if( rc ) goto abort_due_to_error;
4439 if( res ) goto jump_to_p2; 4625 if( res ) goto jump_to_p2;
4440 break; 4626 break;
4441 }; 4627 };
4442 4628
4443 /* Opcode: SorterData P1 P2 P3 * * 4629 /* Opcode: SorterData P1 P2 P3 * *
4444 ** Synopsis: r[P2]=data 4630 ** Synopsis: r[P2]=data
4445 ** 4631 **
4446 ** Write into register P2 the current sorter data for sorter cursor P1. 4632 ** Write into register P2 the current sorter data for sorter cursor P1.
4447 ** Then clear the column header cache on cursor P3. 4633 ** Then clear the column header cache on cursor P3.
4448 ** 4634 **
4449 ** This opcode is normally use to move a record out of the sorter and into 4635 ** This opcode is normally use to move a record out of the sorter and into
4450 ** a register that is the source for a pseudo-table cursor created using 4636 ** a register that is the source for a pseudo-table cursor created using
4451 ** OpenPseudo. That pseudo-table cursor is the one that is identified by 4637 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
4452 ** parameter P3. Clearing the P3 column cache as part of this opcode saves 4638 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
4453 ** us from having to issue a separate NullRow instruction to clear that cache. 4639 ** us from having to issue a separate NullRow instruction to clear that cache.
4454 */ 4640 */
4455 case OP_SorterData: { 4641 case OP_SorterData: {
4456 VdbeCursor *pC; 4642 VdbeCursor *pC;
4457 4643
4458 pOut = &aMem[pOp->p2]; 4644 pOut = &aMem[pOp->p2];
4459 pC = p->apCsr[pOp->p1]; 4645 pC = p->apCsr[pOp->p1];
4460 assert( isSorter(pC) ); 4646 assert( isSorter(pC) );
4461 rc = sqlite3VdbeSorterRowkey(pC, pOut); 4647 rc = sqlite3VdbeSorterRowkey(pC, pOut);
4462 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); 4648 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
4463 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4649 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4650 if( rc ) goto abort_due_to_error;
4464 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; 4651 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
4465 break; 4652 break;
4466 } 4653 }
4467 4654
4468 /* Opcode: RowData P1 P2 * * * 4655 /* Opcode: RowData P1 P2 P3 * *
4469 ** Synopsis: r[P2]=data 4656 ** Synopsis: r[P2]=data
4470 ** 4657 **
4471 ** Write into register P2 the complete row data for cursor P1. 4658 ** Write into register P2 the complete row content for the row at
4659 ** which cursor P1 is currently pointing.
4472 ** There is no interpretation of the data. 4660 ** There is no interpretation of the data.
4473 ** It is just copied onto the P2 register exactly as 4661 ** It is just copied onto the P2 register exactly as
4474 ** it is found in the database file. 4662 ** it is found in the database file.
4475 ** 4663 **
4476 ** If the P1 cursor must be pointing to a valid row (not a NULL row) 4664 ** If cursor P1 is an index, then the content is the key of the row.
4477 ** of a real table, not a pseudo-table. 4665 ** If cursor P2 is a table, then the content extracted is the data.
4478 */
4479 /* Opcode: RowKey P1 P2 * * *
4480 ** Synopsis: r[P2]=key
4481 **
4482 ** Write into register P2 the complete row key for cursor P1.
4483 ** There is no interpretation of the data.
4484 ** The key is copied onto the P2 register exactly as
4485 ** it is found in the database file.
4486 ** 4666 **
4487 ** If the P1 cursor must be pointing to a valid row (not a NULL row) 4667 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
4488 ** of a real table, not a pseudo-table. 4668 ** of a real table, not a pseudo-table.
4669 **
4670 ** If P3!=0 then this opcode is allowed to make an ephermeral pointer
4671 ** into the database page. That means that the content of the output
4672 ** register will be invalidated as soon as the cursor moves - including
4673 ** moves caused by other cursors that "save" the the current cursors
4674 ** position in order that they can write to the same table. If P3==0
4675 ** then a copy of the data is made into memory. P3!=0 is faster, but
4676 ** P3==0 is safer.
4677 **
4678 ** If P3!=0 then the content of the P2 register is unsuitable for use
4679 ** in OP_Result and any OP_Result will invalidate the P2 register content.
4680 ** The P2 register content is invalidated by opcodes like OP_Function or
4681 ** by any use of another cursor pointing to the same table.
4489 */ 4682 */
4490 case OP_RowKey:
4491 case OP_RowData: { 4683 case OP_RowData: {
4492 VdbeCursor *pC; 4684 VdbeCursor *pC;
4493 BtCursor *pCrsr; 4685 BtCursor *pCrsr;
4494 u32 n; 4686 u32 n;
4495 i64 n64;
4496 4687
4497 pOut = &aMem[pOp->p2]; 4688 pOut = out2Prerelease(p, pOp);
4498 memAboutToChange(p, pOut);
4499 4689
4500 /* Note that RowKey and RowData are really exactly the same instruction */
4501 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4690 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4502 pC = p->apCsr[pOp->p1]; 4691 pC = p->apCsr[pOp->p1];
4503 assert( pC!=0 ); 4692 assert( pC!=0 );
4504 assert( pC->eCurType==CURTYPE_BTREE ); 4693 assert( pC->eCurType==CURTYPE_BTREE );
4505 assert( isSorter(pC)==0 ); 4694 assert( isSorter(pC)==0 );
4506 assert( pC->isTable || pOp->opcode!=OP_RowData );
4507 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
4508 assert( pC->nullRow==0 ); 4695 assert( pC->nullRow==0 );
4509 assert( pC->uc.pCursor!=0 ); 4696 assert( pC->uc.pCursor!=0 );
4510 pCrsr = pC->uc.pCursor; 4697 pCrsr = pC->uc.pCursor;
4511 4698
4512 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or 4699 /* The OP_RowData opcodes always follow OP_NotExists or
4513 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate 4700 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4514 ** the cursor. If this where not the case, on of the following assert()s 4701 ** that might invalidate the cursor.
4702 ** If this where not the case, on of the following assert()s
4515 ** would fail. Should this ever change (because of changes in the code 4703 ** would fail. Should this ever change (because of changes in the code
4516 ** generator) then the fix would be to insert a call to 4704 ** generator) then the fix would be to insert a call to
4517 ** sqlite3VdbeCursorMoveto(). 4705 ** sqlite3VdbeCursorMoveto().
4518 */ 4706 */
4519 assert( pC->deferredMoveto==0 ); 4707 assert( pC->deferredMoveto==0 );
4520 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 4708 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4521 #if 0 /* Not required due to the previous to assert() statements */ 4709 #if 0 /* Not required due to the previous to assert() statements */
4522 rc = sqlite3VdbeCursorMoveto(pC); 4710 rc = sqlite3VdbeCursorMoveto(pC);
4523 if( rc!=SQLITE_OK ) goto abort_due_to_error; 4711 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4524 #endif 4712 #endif
4525 4713
4526 if( pC->isTable==0 ){ 4714 n = sqlite3BtreePayloadSize(pCrsr);
4527 assert( !pC->isTable ); 4715 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
4528 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); 4716 goto too_big;
4529 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
4530 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
4531 goto too_big;
4532 }
4533 n = (u32)n64;
4534 }else{
4535 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
4536 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
4537 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
4538 goto too_big;
4539 }
4540 } 4717 }
4541 testcase( n==0 ); 4718 testcase( n==0 );
4542 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ 4719 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
4543 goto no_mem; 4720 if( rc ) goto abort_due_to_error;
4544 } 4721 if( !pOp->p3 ) Deephemeralize(pOut);
4545 pOut->n = n;
4546 MemSetTypeFlag(pOut, MEM_Blob);
4547 if( pC->isTable==0 ){
4548 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4549 }else{
4550 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
4551 }
4552 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
4553 UPDATE_MAX_BLOBSIZE(pOut); 4722 UPDATE_MAX_BLOBSIZE(pOut);
4554 REGISTER_TRACE(pOp->p2, pOut); 4723 REGISTER_TRACE(pOp->p2, pOut);
4555 break; 4724 break;
4556 } 4725 }
4557 4726
4558 /* Opcode: Rowid P1 P2 * * * 4727 /* Opcode: Rowid P1 P2 * * *
4559 ** Synopsis: r[P2]=rowid 4728 ** Synopsis: r[P2]=rowid
4560 ** 4729 **
4561 ** Store in register P2 an integer which is the key of the table entry that 4730 ** Store in register P2 an integer which is the key of the table entry that
4562 ** P1 is currently point to. 4731 ** P1 is currently point to.
(...skipping 19 matching lines...) Expand all
4582 }else if( pC->deferredMoveto ){ 4751 }else if( pC->deferredMoveto ){
4583 v = pC->movetoTarget; 4752 v = pC->movetoTarget;
4584 #ifndef SQLITE_OMIT_VIRTUALTABLE 4753 #ifndef SQLITE_OMIT_VIRTUALTABLE
4585 }else if( pC->eCurType==CURTYPE_VTAB ){ 4754 }else if( pC->eCurType==CURTYPE_VTAB ){
4586 assert( pC->uc.pVCur!=0 ); 4755 assert( pC->uc.pVCur!=0 );
4587 pVtab = pC->uc.pVCur->pVtab; 4756 pVtab = pC->uc.pVCur->pVtab;
4588 pModule = pVtab->pModule; 4757 pModule = pVtab->pModule;
4589 assert( pModule->xRowid ); 4758 assert( pModule->xRowid );
4590 rc = pModule->xRowid(pC->uc.pVCur, &v); 4759 rc = pModule->xRowid(pC->uc.pVCur, &v);
4591 sqlite3VtabImportErrmsg(p, pVtab); 4760 sqlite3VtabImportErrmsg(p, pVtab);
4761 if( rc ) goto abort_due_to_error;
4592 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 4762 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4593 }else{ 4763 }else{
4594 assert( pC->eCurType==CURTYPE_BTREE ); 4764 assert( pC->eCurType==CURTYPE_BTREE );
4595 assert( pC->uc.pCursor!=0 ); 4765 assert( pC->uc.pCursor!=0 );
4596 rc = sqlite3VdbeCursorRestore(pC); 4766 rc = sqlite3VdbeCursorRestore(pC);
4597 if( rc ) goto abort_due_to_error; 4767 if( rc ) goto abort_due_to_error;
4598 if( pC->nullRow ){ 4768 if( pC->nullRow ){
4599 pOut->flags = MEM_Null; 4769 pOut->flags = MEM_Null;
4600 break; 4770 break;
4601 } 4771 }
4602 rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); 4772 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4603 assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */
4604 } 4773 }
4605 pOut->u.i = v; 4774 pOut->u.i = v;
4606 break; 4775 break;
4607 } 4776 }
4608 4777
4609 /* Opcode: NullRow P1 * * * * 4778 /* Opcode: NullRow P1 * * * *
4610 ** 4779 **
4611 ** Move the cursor P1 to a null row. Any OP_Column operations 4780 ** Move the cursor P1 to a null row. Any OP_Column operations
4612 ** that occur while the cursor is on the null row will always 4781 ** that occur while the cursor is on the null row will always
4613 ** write a NULL. 4782 ** write a NULL.
(...skipping 17 matching lines...) Expand all
4631 ** 4800 **
4632 ** The next use of the Rowid or Column or Prev instruction for P1 4801 ** The next use of the Rowid or Column or Prev instruction for P1
4633 ** will refer to the last entry in the database table or index. 4802 ** will refer to the last entry in the database table or index.
4634 ** If the table or index is empty and P2>0, then jump immediately to P2. 4803 ** If the table or index is empty and P2>0, then jump immediately to P2.
4635 ** If P2 is 0 or if the table or index is not empty, fall through 4804 ** If P2 is 0 or if the table or index is not empty, fall through
4636 ** to the following instruction. 4805 ** to the following instruction.
4637 ** 4806 **
4638 ** This opcode leaves the cursor configured to move in reverse order, 4807 ** This opcode leaves the cursor configured to move in reverse order,
4639 ** from the end toward the beginning. In other words, the cursor is 4808 ** from the end toward the beginning. In other words, the cursor is
4640 ** configured to use Prev, not Next. 4809 ** configured to use Prev, not Next.
4810 **
4811 ** If P3 is -1, then the cursor is positioned at the end of the btree
4812 ** for the purpose of appending a new entry onto the btree. In that
4813 ** case P2 must be 0. It is assumed that the cursor is used only for
4814 ** appending and so if the cursor is valid, then the cursor must already
4815 ** be pointing at the end of the btree and so no changes are made to
4816 ** the cursor.
4641 */ 4817 */
4642 case OP_Last: { /* jump */ 4818 case OP_Last: { /* jump */
4643 VdbeCursor *pC; 4819 VdbeCursor *pC;
4644 BtCursor *pCrsr; 4820 BtCursor *pCrsr;
4645 int res; 4821 int res;
4646 4822
4647 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4823 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4648 pC = p->apCsr[pOp->p1]; 4824 pC = p->apCsr[pOp->p1];
4649 assert( pC!=0 ); 4825 assert( pC!=0 );
4650 assert( pC->eCurType==CURTYPE_BTREE ); 4826 assert( pC->eCurType==CURTYPE_BTREE );
4651 pCrsr = pC->uc.pCursor; 4827 pCrsr = pC->uc.pCursor;
4652 res = 0; 4828 res = 0;
4653 assert( pCrsr!=0 ); 4829 assert( pCrsr!=0 );
4654 rc = sqlite3BtreeLast(pCrsr, &res);
4655 pC->nullRow = (u8)res;
4656 pC->deferredMoveto = 0;
4657 pC->cacheStatus = CACHE_STALE;
4658 pC->seekResult = pOp->p3; 4830 pC->seekResult = pOp->p3;
4659 #ifdef SQLITE_DEBUG 4831 #ifdef SQLITE_DEBUG
4660 pC->seekOp = OP_Last; 4832 pC->seekOp = OP_Last;
4661 #endif 4833 #endif
4662 if( pOp->p2>0 ){ 4834 if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){
4663 VdbeBranchTaken(res!=0,2); 4835 rc = sqlite3BtreeLast(pCrsr, &res);
4664 if( res ) goto jump_to_p2; 4836 pC->nullRow = (u8)res;
4837 pC->deferredMoveto = 0;
4838 pC->cacheStatus = CACHE_STALE;
4839 if( rc ) goto abort_due_to_error;
4840 if( pOp->p2>0 ){
4841 VdbeBranchTaken(res!=0,2);
4842 if( res ) goto jump_to_p2;
4843 }
4844 }else{
4845 assert( pOp->p2==0 );
4665 } 4846 }
4666 break; 4847 break;
4667 } 4848 }
4668 4849
4669 4850
4851 /* Opcode: SorterSort P1 P2 * * *
4852 **
4853 ** After all records have been inserted into the Sorter object
4854 ** identified by P1, invoke this opcode to actually do the sorting.
4855 ** Jump to P2 if there are no records to be sorted.
4856 **
4857 ** This opcode is an alias for OP_Sort and OP_Rewind that is used
4858 ** for Sorter objects.
4859 */
4670 /* Opcode: Sort P1 P2 * * * 4860 /* Opcode: Sort P1 P2 * * *
4671 ** 4861 **
4672 ** This opcode does exactly the same thing as OP_Rewind except that 4862 ** This opcode does exactly the same thing as OP_Rewind except that
4673 ** it increments an undocumented global variable used for testing. 4863 ** it increments an undocumented global variable used for testing.
4674 ** 4864 **
4675 ** Sorting is accomplished by writing records into a sorting index, 4865 ** Sorting is accomplished by writing records into a sorting index,
4676 ** then rewinding that index and playing it back from beginning to 4866 ** then rewinding that index and playing it back from beginning to
4677 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the 4867 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4678 ** rewinding so that the global variable will be incremented and 4868 ** rewinding so that the global variable will be incremented and
4679 ** regression tests can determine whether or not the optimizer is 4869 ** regression tests can determine whether or not the optimizer is
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
4716 if( isSorter(pC) ){ 4906 if( isSorter(pC) ){
4717 rc = sqlite3VdbeSorterRewind(pC, &res); 4907 rc = sqlite3VdbeSorterRewind(pC, &res);
4718 }else{ 4908 }else{
4719 assert( pC->eCurType==CURTYPE_BTREE ); 4909 assert( pC->eCurType==CURTYPE_BTREE );
4720 pCrsr = pC->uc.pCursor; 4910 pCrsr = pC->uc.pCursor;
4721 assert( pCrsr ); 4911 assert( pCrsr );
4722 rc = sqlite3BtreeFirst(pCrsr, &res); 4912 rc = sqlite3BtreeFirst(pCrsr, &res);
4723 pC->deferredMoveto = 0; 4913 pC->deferredMoveto = 0;
4724 pC->cacheStatus = CACHE_STALE; 4914 pC->cacheStatus = CACHE_STALE;
4725 } 4915 }
4916 if( rc ) goto abort_due_to_error;
4726 pC->nullRow = (u8)res; 4917 pC->nullRow = (u8)res;
4727 assert( pOp->p2>0 && pOp->p2<p->nOp ); 4918 assert( pOp->p2>0 && pOp->p2<p->nOp );
4728 VdbeBranchTaken(res!=0,2); 4919 VdbeBranchTaken(res!=0,2);
4729 if( res ) goto jump_to_p2; 4920 if( res ) goto jump_to_p2;
4730 break; 4921 break;
4731 } 4922 }
4732 4923
4733 /* Opcode: Next P1 P2 P3 P4 P5 4924 /* Opcode: Next P1 P2 P3 P4 P5
4734 ** 4925 **
4735 ** Advance cursor P1 so that it points to the next key/data pair in its 4926 ** Advance cursor P1 so that it points to the next key/data pair in its
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
4786 ** sqlite3BtreePrevious(). 4977 ** sqlite3BtreePrevious().
4787 ** 4978 **
4788 ** If P5 is positive and the jump is taken, then event counter 4979 ** If P5 is positive and the jump is taken, then event counter
4789 ** number P5-1 in the prepared statement is incremented. 4980 ** number P5-1 in the prepared statement is incremented.
4790 */ 4981 */
4791 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5 4982 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
4792 ** 4983 **
4793 ** This opcode works just like Prev except that if cursor P1 is not 4984 ** This opcode works just like Prev except that if cursor P1 is not
4794 ** open it behaves a no-op. 4985 ** open it behaves a no-op.
4795 */ 4986 */
4987 /* Opcode: SorterNext P1 P2 * * P5
4988 **
4989 ** This opcode works just like OP_Next except that P1 must be a
4990 ** sorter object for which the OP_SorterSort opcode has been
4991 ** invoked. This opcode advances the cursor to the next sorted
4992 ** record, or jumps to P2 if there are no more sorted records.
4993 */
4796 case OP_SorterNext: { /* jump */ 4994 case OP_SorterNext: { /* jump */
4797 VdbeCursor *pC; 4995 VdbeCursor *pC;
4798 int res; 4996 int res;
4799 4997
4800 pC = p->apCsr[pOp->p1]; 4998 pC = p->apCsr[pOp->p1];
4801 assert( isSorter(pC) ); 4999 assert( isSorter(pC) );
4802 res = 0; 5000 res = 0;
4803 rc = sqlite3VdbeSorterNext(db, pC, &res); 5001 rc = sqlite3VdbeSorterNext(db, pC, &res);
4804 goto next_tail; 5002 goto next_tail;
4805 case OP_PrevIfOpen: /* jump */ 5003 case OP_PrevIfOpen: /* jump */
(...skipping 22 matching lines...) Expand all
4828 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE 5026 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
4829 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); 5027 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
4830 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen 5028 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
4831 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE 5029 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
4832 || pC->seekOp==OP_Last ); 5030 || pC->seekOp==OP_Last );
4833 5031
4834 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); 5032 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
4835 next_tail: 5033 next_tail:
4836 pC->cacheStatus = CACHE_STALE; 5034 pC->cacheStatus = CACHE_STALE;
4837 VdbeBranchTaken(res==0,2); 5035 VdbeBranchTaken(res==0,2);
5036 if( rc ) goto abort_due_to_error;
4838 if( res==0 ){ 5037 if( res==0 ){
4839 pC->nullRow = 0; 5038 pC->nullRow = 0;
4840 p->aCounter[pOp->p5]++; 5039 p->aCounter[pOp->p5]++;
4841 #ifdef SQLITE_TEST 5040 #ifdef SQLITE_TEST
4842 sqlite3_search_count++; 5041 sqlite3_search_count++;
4843 #endif 5042 #endif
4844 goto jump_to_p2_and_check_for_interrupt; 5043 goto jump_to_p2_and_check_for_interrupt;
4845 }else{ 5044 }else{
4846 pC->nullRow = 1; 5045 pC->nullRow = 1;
4847 } 5046 }
4848 goto check_for_interrupt; 5047 goto check_for_interrupt;
4849 } 5048 }
4850 5049
4851 /* Opcode: IdxInsert P1 P2 P3 * P5 5050 /* Opcode: IdxInsert P1 P2 P3 P4 P5
4852 ** Synopsis: key=r[P2] 5051 ** Synopsis: key=r[P2]
4853 ** 5052 **
4854 ** Register P2 holds an SQL index key made using the 5053 ** Register P2 holds an SQL index key made using the
4855 ** MakeRecord instructions. This opcode writes that key 5054 ** MakeRecord instructions. This opcode writes that key
4856 ** into the index P1. Data for the entry is nil. 5055 ** into the index P1. Data for the entry is nil.
4857 ** 5056 **
4858 ** P3 is a flag that provides a hint to the b-tree layer that this 5057 ** If P4 is not zero, then it is the number of values in the unpacked
4859 ** insert is likely to be an append. 5058 ** key of reg(P2). In that case, P3 is the index of the first register
5059 ** for the unpacked key. The availability of the unpacked key can sometimes
5060 ** be an optimization.
5061 **
5062 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5063 ** that this insert is likely to be an append.
4860 ** 5064 **
4861 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is 5065 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
4862 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, 5066 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
4863 ** then the change counter is unchanged. 5067 ** then the change counter is unchanged.
4864 ** 5068 **
4865 ** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have 5069 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4866 ** just done a seek to the spot where the new entry is to be inserted. 5070 ** run faster by avoiding an unnecessary seek on cursor P1. However,
4867 ** This flag avoids doing an extra seek. 5071 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5072 ** seeks on the cursor or if the most recent seek used a key equivalent
5073 ** to P2.
4868 ** 5074 **
4869 ** This instruction only works for indices. The equivalent instruction 5075 ** This instruction only works for indices. The equivalent instruction
4870 ** for tables is OP_Insert. 5076 ** for tables is OP_Insert.
4871 */ 5077 */
5078 /* Opcode: SorterInsert P1 P2 * * *
5079 ** Synopsis: key=r[P2]
5080 **
5081 ** Register P2 holds an SQL index key made using the
5082 ** MakeRecord instructions. This opcode writes that key
5083 ** into the sorter P1. Data for the entry is nil.
5084 */
4872 case OP_SorterInsert: /* in2 */ 5085 case OP_SorterInsert: /* in2 */
4873 case OP_IdxInsert: { /* in2 */ 5086 case OP_IdxInsert: { /* in2 */
4874 VdbeCursor *pC; 5087 VdbeCursor *pC;
4875 int nKey; 5088 BtreePayload x;
4876 const char *zKey;
4877 5089
4878 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5090 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4879 pC = p->apCsr[pOp->p1]; 5091 pC = p->apCsr[pOp->p1];
4880 assert( pC!=0 ); 5092 assert( pC!=0 );
4881 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); 5093 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
4882 pIn2 = &aMem[pOp->p2]; 5094 pIn2 = &aMem[pOp->p2];
4883 assert( pIn2->flags & MEM_Blob ); 5095 assert( pIn2->flags & MEM_Blob );
4884 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 5096 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
4885 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); 5097 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
4886 assert( pC->isTable==0 ); 5098 assert( pC->isTable==0 );
4887 rc = ExpandBlob(pIn2); 5099 rc = ExpandBlob(pIn2);
4888 if( rc==SQLITE_OK ){ 5100 if( rc ) goto abort_due_to_error;
4889 if( pOp->opcode==OP_SorterInsert ){ 5101 if( pOp->opcode==OP_SorterInsert ){
4890 rc = sqlite3VdbeSorterWrite(pC, pIn2); 5102 rc = sqlite3VdbeSorterWrite(pC, pIn2);
4891 }else{ 5103 }else{
4892 nKey = pIn2->n; 5104 x.nKey = pIn2->n;
4893 zKey = pIn2->z; 5105 x.pKey = pIn2->z;
4894 rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, 5106 x.aMem = aMem + pOp->p3;
4895 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) 5107 x.nMem = (u16)pOp->p4.i;
4896 ); 5108 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
4897 assert( pC->deferredMoveto==0 ); 5109 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
4898 pC->cacheStatus = CACHE_STALE; 5110 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4899 } 5111 );
5112 assert( pC->deferredMoveto==0 );
5113 pC->cacheStatus = CACHE_STALE;
4900 } 5114 }
5115 if( rc) goto abort_due_to_error;
4901 break; 5116 break;
4902 } 5117 }
4903 5118
4904 /* Opcode: IdxDelete P1 P2 P3 * * 5119 /* Opcode: IdxDelete P1 P2 P3 * *
4905 ** Synopsis: key=r[P2@P3] 5120 ** Synopsis: key=r[P2@P3]
4906 ** 5121 **
4907 ** The content of P3 registers starting at register P2 form 5122 ** The content of P3 registers starting at register P2 form
4908 ** an unpacked index key. This opcode removes that entry from the 5123 ** an unpacked index key. This opcode removes that entry from the
4909 ** index opened by cursor P1. 5124 ** index opened by cursor P1.
4910 */ 5125 */
4911 case OP_IdxDelete: { 5126 case OP_IdxDelete: {
4912 VdbeCursor *pC; 5127 VdbeCursor *pC;
4913 BtCursor *pCrsr; 5128 BtCursor *pCrsr;
4914 int res; 5129 int res;
4915 UnpackedRecord r; 5130 UnpackedRecord r;
4916 5131
4917 assert( pOp->p3>0 ); 5132 assert( pOp->p3>0 );
4918 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 ); 5133 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
4919 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5134 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4920 pC = p->apCsr[pOp->p1]; 5135 pC = p->apCsr[pOp->p1];
4921 assert( pC!=0 ); 5136 assert( pC!=0 );
4922 assert( pC->eCurType==CURTYPE_BTREE ); 5137 assert( pC->eCurType==CURTYPE_BTREE );
4923 pCrsr = pC->uc.pCursor; 5138 pCrsr = pC->uc.pCursor;
4924 assert( pCrsr!=0 ); 5139 assert( pCrsr!=0 );
4925 assert( pOp->p5==0 ); 5140 assert( pOp->p5==0 );
4926 r.pKeyInfo = pC->pKeyInfo; 5141 r.pKeyInfo = pC->pKeyInfo;
4927 r.nField = (u16)pOp->p3; 5142 r.nField = (u16)pOp->p3;
4928 r.default_rc = 0; 5143 r.default_rc = 0;
4929 r.aMem = &aMem[pOp->p2]; 5144 r.aMem = &aMem[pOp->p2];
4930 #ifdef SQLITE_DEBUG
4931 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4932 #endif
4933 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); 5145 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
4934 if( rc==SQLITE_OK && res==0 ){ 5146 if( rc ) goto abort_due_to_error;
4935 rc = sqlite3BtreeDelete(pCrsr, 0); 5147 if( res==0 ){
5148 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
5149 if( rc ) goto abort_due_to_error;
4936 } 5150 }
4937 assert( pC->deferredMoveto==0 ); 5151 assert( pC->deferredMoveto==0 );
4938 pC->cacheStatus = CACHE_STALE; 5152 pC->cacheStatus = CACHE_STALE;
5153 pC->seekResult = 0;
4939 break; 5154 break;
4940 } 5155 }
4941 5156
5157 /* Opcode: Seek P1 * P3 P4 *
5158 ** Synopsis: Move P3 to P1.rowid
5159 **
5160 ** P1 is an open index cursor and P3 is a cursor on the corresponding
5161 ** table. This opcode does a deferred seek of the P3 table cursor
5162 ** to the row that corresponds to the current row of P1.
5163 **
5164 ** This is a deferred seek. Nothing actually happens until
5165 ** the cursor is used to read a record. That way, if no reads
5166 ** occur, no unnecessary I/O happens.
5167 **
5168 ** P4 may be an array of integers (type P4_INTARRAY) containing
5169 ** one entry for each column in the P3 table. If array entry a(i)
5170 ** is non-zero, then reading column a(i)-1 from cursor P3 is
5171 ** equivalent to performing the deferred seek and then reading column i
5172 ** from P1. This information is stored in P3 and used to redirect
5173 ** reads against P3 over to P1, thus possibly avoiding the need to
5174 ** seek and read cursor P3.
5175 */
4942 /* Opcode: IdxRowid P1 P2 * * * 5176 /* Opcode: IdxRowid P1 P2 * * *
4943 ** Synopsis: r[P2]=rowid 5177 ** Synopsis: r[P2]=rowid
4944 ** 5178 **
4945 ** Write into register P2 an integer which is the last entry in the record at 5179 ** Write into register P2 an integer which is the last entry in the record at
4946 ** the end of the index key pointed to by cursor P1. This integer should be 5180 ** the end of the index key pointed to by cursor P1. This integer should be
4947 ** the rowid of the table entry to which this index entry points. 5181 ** the rowid of the table entry to which this index entry points.
4948 ** 5182 **
4949 ** See also: Rowid, MakeRecord. 5183 ** See also: Rowid, MakeRecord.
4950 */ 5184 */
5185 case OP_Seek:
4951 case OP_IdxRowid: { /* out2 */ 5186 case OP_IdxRowid: { /* out2 */
4952 BtCursor *pCrsr; 5187 VdbeCursor *pC; /* The P1 index cursor */
4953 VdbeCursor *pC; 5188 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */
4954 i64 rowid; 5189 i64 rowid; /* Rowid that P1 current points to */
4955 5190
4956 pOut = out2Prerelease(p, pOp);
4957 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5191 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4958 pC = p->apCsr[pOp->p1]; 5192 pC = p->apCsr[pOp->p1];
4959 assert( pC!=0 ); 5193 assert( pC!=0 );
4960 assert( pC->eCurType==CURTYPE_BTREE ); 5194 assert( pC->eCurType==CURTYPE_BTREE );
4961 pCrsr = pC->uc.pCursor; 5195 assert( pC->uc.pCursor!=0 );
4962 assert( pCrsr!=0 );
4963 pOut->flags = MEM_Null;
4964 assert( pC->isTable==0 ); 5196 assert( pC->isTable==0 );
4965 assert( pC->deferredMoveto==0 ); 5197 assert( pC->deferredMoveto==0 );
5198 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5199
5200 /* The IdxRowid and Seek opcodes are combined because of the commonality
5201 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5202 rc = sqlite3VdbeCursorRestore(pC);
4966 5203
4967 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted 5204 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
4968 ** out from under the cursor. That will never happend for an IdxRowid 5205 ** out from under the cursor. That will never happens for an IdxRowid
4969 ** opcode, hence the NEVER() arround the check of the return value. 5206 ** or Seek opcode */
4970 */
4971 rc = sqlite3VdbeCursorRestore(pC);
4972 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; 5207 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4973 5208
4974 if( !pC->nullRow ){ 5209 if( !pC->nullRow ){
4975 rowid = 0; /* Not needed. Only used to silence a warning. */ 5210 rowid = 0; /* Not needed. Only used to silence a warning. */
4976 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid); 5211 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
4977 if( rc!=SQLITE_OK ){ 5212 if( rc!=SQLITE_OK ){
4978 goto abort_due_to_error; 5213 goto abort_due_to_error;
4979 } 5214 }
4980 pOut->u.i = rowid; 5215 if( pOp->opcode==OP_Seek ){
4981 pOut->flags = MEM_Int; 5216 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5217 pTabCur = p->apCsr[pOp->p3];
5218 assert( pTabCur!=0 );
5219 assert( pTabCur->eCurType==CURTYPE_BTREE );
5220 assert( pTabCur->uc.pCursor!=0 );
5221 assert( pTabCur->isTable );
5222 pTabCur->nullRow = 0;
5223 pTabCur->movetoTarget = rowid;
5224 pTabCur->deferredMoveto = 1;
5225 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5226 pTabCur->aAltMap = pOp->p4.ai;
5227 pTabCur->pAltCursor = pC;
5228 }else{
5229 pOut = out2Prerelease(p, pOp);
5230 pOut->u.i = rowid;
5231 }
5232 }else{
5233 assert( pOp->opcode==OP_IdxRowid );
5234 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
4982 } 5235 }
4983 break; 5236 break;
4984 } 5237 }
4985 5238
4986 /* Opcode: IdxGE P1 P2 P3 P4 P5 5239 /* Opcode: IdxGE P1 P2 P3 P4 P5
4987 ** Synopsis: key=r[P3@P4] 5240 ** Synopsis: key=r[P3@P4]
4988 ** 5241 **
4989 ** The P4 register values beginning with P3 form an unpacked index 5242 ** The P4 register values beginning with P3 form an unpacked index
4990 ** key that omits the PRIMARY KEY. Compare this key value against the index 5243 ** key that omits the PRIMARY KEY. Compare this key value against the index
4991 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 5244 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
5061 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); 5314 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
5062 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); 5315 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5063 if( (pOp->opcode&1)==(OP_IdxLT&1) ){ 5316 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5064 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); 5317 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
5065 res = -res; 5318 res = -res;
5066 }else{ 5319 }else{
5067 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); 5320 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
5068 res++; 5321 res++;
5069 } 5322 }
5070 VdbeBranchTaken(res>0,2); 5323 VdbeBranchTaken(res>0,2);
5324 if( rc ) goto abort_due_to_error;
5071 if( res>0 ) goto jump_to_p2; 5325 if( res>0 ) goto jump_to_p2;
5072 break; 5326 break;
5073 } 5327 }
5074 5328
5075 /* Opcode: Destroy P1 P2 P3 * * 5329 /* Opcode: Destroy P1 P2 P3 * *
5076 ** 5330 **
5077 ** Delete an entire database table or index whose root page in the database 5331 ** Delete an entire database table or index whose root page in the database
5078 ** file is given by P1. 5332 ** file is given by P1.
5079 ** 5333 **
5080 ** The table being destroyed is in the main database file if P3==0. If 5334 ** The table being destroyed is in the main database file if P3==0. If
5081 ** P3==1 then the table to be clear is in the auxiliary database file 5335 ** P3==1 then the table to be clear is in the auxiliary database file
5082 ** that is used to store tables create using CREATE TEMPORARY TABLE. 5336 ** that is used to store tables create using CREATE TEMPORARY TABLE.
5083 ** 5337 **
5084 ** If AUTOVACUUM is enabled then it is possible that another root page 5338 ** If AUTOVACUUM is enabled then it is possible that another root page
5085 ** might be moved into the newly deleted root page in order to keep all 5339 ** might be moved into the newly deleted root page in order to keep all
5086 ** root pages contiguous at the beginning of the database. The former 5340 ** root pages contiguous at the beginning of the database. The former
5087 ** value of the root page that moved - its value before the move occurred - 5341 ** value of the root page that moved - its value before the move occurred -
5088 ** is stored in register P2. If no page 5342 ** is stored in register P2. If no page
5089 ** movement was required (because the table being dropped was already 5343 ** movement was required (because the table being dropped was already
5090 ** the last one in the database) then a zero is stored in register P2. 5344 ** the last one in the database) then a zero is stored in register P2.
5091 ** If AUTOVACUUM is disabled then a zero is stored in register P2. 5345 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
5092 ** 5346 **
5093 ** See also: Clear 5347 ** See also: Clear
5094 */ 5348 */
5095 case OP_Destroy: { /* out2 */ 5349 case OP_Destroy: { /* out2 */
5096 int iMoved; 5350 int iMoved;
5097 int iDb; 5351 int iDb;
5098 5352
5099 assert( p->readOnly==0 ); 5353 assert( p->readOnly==0 );
5354 assert( pOp->p1>1 );
5100 pOut = out2Prerelease(p, pOp); 5355 pOut = out2Prerelease(p, pOp);
5101 pOut->flags = MEM_Null; 5356 pOut->flags = MEM_Null;
5102 if( db->nVdbeRead > db->nVDestroy+1 ){ 5357 if( db->nVdbeRead > db->nVDestroy+1 ){
5103 rc = SQLITE_LOCKED; 5358 rc = SQLITE_LOCKED;
5104 p->errorAction = OE_Abort; 5359 p->errorAction = OE_Abort;
5360 goto abort_due_to_error;
5105 }else{ 5361 }else{
5106 iDb = pOp->p3; 5362 iDb = pOp->p3;
5107 assert( DbMaskTest(p->btreeMask, iDb) ); 5363 assert( DbMaskTest(p->btreeMask, iDb) );
5108 iMoved = 0; /* Not needed. Only to silence a warning. */ 5364 iMoved = 0; /* Not needed. Only to silence a warning. */
5109 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); 5365 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
5110 pOut->flags = MEM_Int; 5366 pOut->flags = MEM_Int;
5111 pOut->u.i = iMoved; 5367 pOut->u.i = iMoved;
5368 if( rc ) goto abort_due_to_error;
5112 #ifndef SQLITE_OMIT_AUTOVACUUM 5369 #ifndef SQLITE_OMIT_AUTOVACUUM
5113 if( rc==SQLITE_OK && iMoved!=0 ){ 5370 if( iMoved!=0 ){
5114 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); 5371 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5115 /* All OP_Destroy operations occur on the same btree */ 5372 /* All OP_Destroy operations occur on the same btree */
5116 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); 5373 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5117 resetSchemaOnFault = iDb+1; 5374 resetSchemaOnFault = iDb+1;
5118 } 5375 }
5119 #endif 5376 #endif
5120 } 5377 }
5121 break; 5378 break;
5122 } 5379 }
5123 5380
(...skipping 25 matching lines...) Expand all
5149 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) 5406 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5150 ); 5407 );
5151 if( pOp->p3 ){ 5408 if( pOp->p3 ){
5152 p->nChange += nChange; 5409 p->nChange += nChange;
5153 if( pOp->p3>0 ){ 5410 if( pOp->p3>0 ){
5154 assert( memIsValid(&aMem[pOp->p3]) ); 5411 assert( memIsValid(&aMem[pOp->p3]) );
5155 memAboutToChange(p, &aMem[pOp->p3]); 5412 memAboutToChange(p, &aMem[pOp->p3]);
5156 aMem[pOp->p3].u.i += nChange; 5413 aMem[pOp->p3].u.i += nChange;
5157 } 5414 }
5158 } 5415 }
5416 if( rc ) goto abort_due_to_error;
5159 break; 5417 break;
5160 } 5418 }
5161 5419
5162 /* Opcode: ResetSorter P1 * * * * 5420 /* Opcode: ResetSorter P1 * * * *
5163 ** 5421 **
5164 ** Delete all contents from the ephemeral table or sorter 5422 ** Delete all contents from the ephemeral table or sorter
5165 ** that is open on cursor P1. 5423 ** that is open on cursor P1.
5166 ** 5424 **
5167 ** This opcode only works for cursors used for sorting and 5425 ** This opcode only works for cursors used for sorting and
5168 ** opened with OP_OpenEphemeral or OP_SorterOpen. 5426 ** opened with OP_OpenEphemeral or OP_SorterOpen.
5169 */ 5427 */
5170 case OP_ResetSorter: { 5428 case OP_ResetSorter: {
5171 VdbeCursor *pC; 5429 VdbeCursor *pC;
5172 5430
5173 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5431 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5174 pC = p->apCsr[pOp->p1]; 5432 pC = p->apCsr[pOp->p1];
5175 assert( pC!=0 ); 5433 assert( pC!=0 );
5176 if( isSorter(pC) ){ 5434 if( isSorter(pC) ){
5177 sqlite3VdbeSorterReset(db, pC->uc.pSorter); 5435 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
5178 }else{ 5436 }else{
5179 assert( pC->eCurType==CURTYPE_BTREE ); 5437 assert( pC->eCurType==CURTYPE_BTREE );
5180 assert( pC->isEphemeral ); 5438 assert( pC->isEphemeral );
5181 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); 5439 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
5440 if( rc ) goto abort_due_to_error;
5182 } 5441 }
5183 break; 5442 break;
5184 } 5443 }
5185 5444
5186 /* Opcode: CreateTable P1 P2 * * * 5445 /* Opcode: CreateTable P1 P2 * * *
5187 ** Synopsis: r[P2]=root iDb=P1 5446 ** Synopsis: r[P2]=root iDb=P1
5188 ** 5447 **
5189 ** Allocate a new table in the main database file if P1==0 or in the 5448 ** Allocate a new table in the main database file if P1==0 or in the
5190 ** auxiliary database file if P1==1 or in an attached database if 5449 ** auxiliary database file if P1==1 or in an attached database if
5191 ** P1>1. Write the root page number of the new table into 5450 ** P1>1. Write the root page number of the new table into
(...skipping 28 matching lines...) Expand all
5220 assert( p->readOnly==0 ); 5479 assert( p->readOnly==0 );
5221 pDb = &db->aDb[pOp->p1]; 5480 pDb = &db->aDb[pOp->p1];
5222 assert( pDb->pBt!=0 ); 5481 assert( pDb->pBt!=0 );
5223 if( pOp->opcode==OP_CreateTable ){ 5482 if( pOp->opcode==OP_CreateTable ){
5224 /* flags = BTREE_INTKEY; */ 5483 /* flags = BTREE_INTKEY; */
5225 flags = BTREE_INTKEY; 5484 flags = BTREE_INTKEY;
5226 }else{ 5485 }else{
5227 flags = BTREE_BLOBKEY; 5486 flags = BTREE_BLOBKEY;
5228 } 5487 }
5229 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); 5488 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
5489 if( rc ) goto abort_due_to_error;
5230 pOut->u.i = pgno; 5490 pOut->u.i = pgno;
5231 break; 5491 break;
5232 } 5492 }
5233 5493
5234 /* Opcode: ParseSchema P1 * * P4 * 5494 /* Opcode: ParseSchema P1 * * P4 *
5235 ** 5495 **
5236 ** Read and parse all entries from the SQLITE_MASTER table of database P1 5496 ** Read and parse all entries from the SQLITE_MASTER table of database P1
5237 ** that match the WHERE clause P4. 5497 ** that match the WHERE clause P4.
5238 ** 5498 **
5239 ** This opcode invokes the parser to create a new virtual machine, 5499 ** This opcode invokes the parser to create a new virtual machine,
(...skipping 12 matching lines...) Expand all
5252 #ifdef SQLITE_DEBUG 5512 #ifdef SQLITE_DEBUG
5253 for(iDb=0; iDb<db->nDb; iDb++){ 5513 for(iDb=0; iDb<db->nDb; iDb++){
5254 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); 5514 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5255 } 5515 }
5256 #endif 5516 #endif
5257 5517
5258 iDb = pOp->p1; 5518 iDb = pOp->p1;
5259 assert( iDb>=0 && iDb<db->nDb ); 5519 assert( iDb>=0 && iDb<db->nDb );
5260 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); 5520 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
5261 /* Used to be a conditional */ { 5521 /* Used to be a conditional */ {
5262 zMaster = SCHEMA_TABLE(iDb); 5522 zMaster = MASTER_NAME;
5263 initData.db = db; 5523 initData.db = db;
5264 initData.iDb = pOp->p1; 5524 initData.iDb = pOp->p1;
5265 initData.pzErrMsg = &p->zErrMsg; 5525 initData.pzErrMsg = &p->zErrMsg;
5266 zSql = sqlite3MPrintf(db, 5526 zSql = sqlite3MPrintf(db,
5267 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", 5527 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
5268 db->aDb[iDb].zName, zMaster, pOp->p4.z); 5528 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
5269 if( zSql==0 ){ 5529 if( zSql==0 ){
5270 rc = SQLITE_NOMEM; 5530 rc = SQLITE_NOMEM_BKPT;
5271 }else{ 5531 }else{
5272 assert( db->init.busy==0 ); 5532 assert( db->init.busy==0 );
5273 db->init.busy = 1; 5533 db->init.busy = 1;
5274 initData.rc = SQLITE_OK; 5534 initData.rc = SQLITE_OK;
5275 assert( !db->mallocFailed ); 5535 assert( !db->mallocFailed );
5276 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); 5536 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5277 if( rc==SQLITE_OK ) rc = initData.rc; 5537 if( rc==SQLITE_OK ) rc = initData.rc;
5278 sqlite3DbFree(db, zSql); 5538 sqlite3DbFree(db, zSql);
5279 db->init.busy = 0; 5539 db->init.busy = 0;
5280 } 5540 }
5281 } 5541 }
5282 if( rc ) sqlite3ResetAllSchemasOfConnection(db); 5542 if( rc ){
5283 if( rc==SQLITE_NOMEM ){ 5543 sqlite3ResetAllSchemasOfConnection(db);
5284 goto no_mem; 5544 if( rc==SQLITE_NOMEM ){
5545 goto no_mem;
5546 }
5547 goto abort_due_to_error;
5285 } 5548 }
5286 break; 5549 break;
5287 } 5550 }
5288 5551
5289 #if !defined(SQLITE_OMIT_ANALYZE) 5552 #if !defined(SQLITE_OMIT_ANALYZE)
5290 /* Opcode: LoadAnalysis P1 * * * * 5553 /* Opcode: LoadAnalysis P1 * * * *
5291 ** 5554 **
5292 ** Read the sqlite_stat1 table for database P1 and load the content 5555 ** Read the sqlite_stat1 table for database P1 and load the content
5293 ** of that table into the internal index hash table. This will cause 5556 ** of that table into the internal index hash table. This will cause
5294 ** the analysis to be used when preparing all subsequent queries. 5557 ** the analysis to be used when preparing all subsequent queries.
5295 */ 5558 */
5296 case OP_LoadAnalysis: { 5559 case OP_LoadAnalysis: {
5297 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 5560 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5298 rc = sqlite3AnalysisLoad(db, pOp->p1); 5561 rc = sqlite3AnalysisLoad(db, pOp->p1);
5562 if( rc ) goto abort_due_to_error;
5299 break; 5563 break;
5300 } 5564 }
5301 #endif /* !defined(SQLITE_OMIT_ANALYZE) */ 5565 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
5302 5566
5303 /* Opcode: DropTable P1 * * P4 * 5567 /* Opcode: DropTable P1 * * P4 *
5304 ** 5568 **
5305 ** Remove the internal (in-memory) data structures that describe 5569 ** Remove the internal (in-memory) data structures that describe
5306 ** the table named P4 in database P1. This is called after a table 5570 ** the table named P4 in database P1. This is called after a table
5307 ** is dropped from disk (using the Destroy opcode) in order to keep 5571 ** is dropped from disk (using the Destroy opcode) in order to keep
5308 ** the internal representation of the 5572 ** the internal representation of the
(...skipping 25 matching lines...) Expand all
5334 ** the internal representation of the 5598 ** the internal representation of the
5335 ** schema consistent with what is on disk. 5599 ** schema consistent with what is on disk.
5336 */ 5600 */
5337 case OP_DropTrigger: { 5601 case OP_DropTrigger: {
5338 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); 5602 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
5339 break; 5603 break;
5340 } 5604 }
5341 5605
5342 5606
5343 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 5607 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
5344 /* Opcode: IntegrityCk P1 P2 P3 * P5 5608 /* Opcode: IntegrityCk P1 P2 P3 P4 P5
5345 ** 5609 **
5346 ** Do an analysis of the currently open database. Store in 5610 ** Do an analysis of the currently open database. Store in
5347 ** register P1 the text of an error message describing any problems. 5611 ** register P1 the text of an error message describing any problems.
5348 ** If no problems are found, store a NULL in register P1. 5612 ** If no problems are found, store a NULL in register P1.
5349 ** 5613 **
5350 ** The register P3 contains the maximum number of allowed errors. 5614 ** The register P3 contains the maximum number of allowed errors.
5351 ** At most reg(P3) errors will be reported. 5615 ** At most reg(P3) errors will be reported.
5352 ** In other words, the analysis stops as soon as reg(P1) errors are 5616 ** In other words, the analysis stops as soon as reg(P1) errors are
5353 ** seen. Reg(P1) is updated with the number of errors remaining. 5617 ** seen. Reg(P1) is updated with the number of errors remaining.
5354 ** 5618 **
5355 ** The root page numbers of all tables in the database are integer 5619 ** The root page numbers of all tables in the database are integers
5356 ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables 5620 ** stored in P4_INTARRAY argument.
5357 ** total.
5358 ** 5621 **
5359 ** If P5 is not zero, the check is done on the auxiliary database 5622 ** If P5 is not zero, the check is done on the auxiliary database
5360 ** file, not the main database file. 5623 ** file, not the main database file.
5361 ** 5624 **
5362 ** This opcode is used to implement the integrity_check pragma. 5625 ** This opcode is used to implement the integrity_check pragma.
5363 */ 5626 */
5364 case OP_IntegrityCk: { 5627 case OP_IntegrityCk: {
5365 int nRoot; /* Number of tables to check. (Number of root pages.) */ 5628 int nRoot; /* Number of tables to check. (Number of root pages.) */
5366 int *aRoot; /* Array of rootpage numbers for tables to be checked */ 5629 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5367 int j; /* Loop counter */
5368 int nErr; /* Number of errors reported */ 5630 int nErr; /* Number of errors reported */
5369 char *z; /* Text of the error report */ 5631 char *z; /* Text of the error report */
5370 Mem *pnErr; /* Register keeping track of errors remaining */ 5632 Mem *pnErr; /* Register keeping track of errors remaining */
5371 5633
5372 assert( p->bIsReader ); 5634 assert( p->bIsReader );
5373 nRoot = pOp->p2; 5635 nRoot = pOp->p2;
5636 aRoot = pOp->p4.ai;
5374 assert( nRoot>0 ); 5637 assert( nRoot>0 );
5375 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); 5638 assert( aRoot[nRoot]==0 );
5376 if( aRoot==0 ) goto no_mem; 5639 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
5377 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
5378 pnErr = &aMem[pOp->p3]; 5640 pnErr = &aMem[pOp->p3];
5379 assert( (pnErr->flags & MEM_Int)!=0 ); 5641 assert( (pnErr->flags & MEM_Int)!=0 );
5380 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); 5642 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
5381 pIn1 = &aMem[pOp->p1]; 5643 pIn1 = &aMem[pOp->p1];
5382 for(j=0; j<nRoot; j++){
5383 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
5384 }
5385 aRoot[j] = 0;
5386 assert( pOp->p5<db->nDb ); 5644 assert( pOp->p5<db->nDb );
5387 assert( DbMaskTest(p->btreeMask, pOp->p5) ); 5645 assert( DbMaskTest(p->btreeMask, pOp->p5) );
5388 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, 5646 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
5389 (int)pnErr->u.i, &nErr); 5647 (int)pnErr->u.i, &nErr);
5390 sqlite3DbFree(db, aRoot);
5391 pnErr->u.i -= nErr; 5648 pnErr->u.i -= nErr;
5392 sqlite3VdbeMemSetNull(pIn1); 5649 sqlite3VdbeMemSetNull(pIn1);
5393 if( nErr==0 ){ 5650 if( nErr==0 ){
5394 assert( z==0 ); 5651 assert( z==0 );
5395 }else if( z==0 ){ 5652 }else if( z==0 ){
5396 goto no_mem; 5653 goto no_mem;
5397 }else{ 5654 }else{
5398 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); 5655 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
5399 } 5656 }
5400 UPDATE_MAX_BLOBSIZE(pIn1); 5657 UPDATE_MAX_BLOBSIZE(pIn1);
5401 sqlite3VdbeChangeEncoding(pIn1, encoding); 5658 sqlite3VdbeChangeEncoding(pIn1, encoding);
5402 break; 5659 break;
5403 } 5660 }
5404 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 5661 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
5405 5662
5406 /* Opcode: RowSetAdd P1 P2 * * * 5663 /* Opcode: RowSetAdd P1 P2 * * *
5407 ** Synopsis: rowset(P1)=r[P2] 5664 ** Synopsis: rowset(P1)=r[P2]
5408 ** 5665 **
5409 ** Insert the integer value held by register P2 into a boolean index 5666 ** Insert the integer value held by register P2 into a boolean index
5410 ** held in register P1. 5667 ** held in register P1.
5411 ** 5668 **
5412 ** An assertion fails if P2 is not an integer. 5669 ** An assertion fails if P2 is not an integer.
5413 */ 5670 */
5414 case OP_RowSetAdd: { /* in1, in2 */ 5671 case OP_RowSetAdd: { /* in1, in2 */
5415 pIn1 = &aMem[pOp->p1]; 5672 pIn1 = &aMem[pOp->p1];
5416 pIn2 = &aMem[pOp->p2]; 5673 pIn2 = &aMem[pOp->p2];
5417 assert( (pIn2->flags & MEM_Int)!=0 ); 5674 assert( (pIn2->flags & MEM_Int)!=0 );
5418 if( (pIn1->flags & MEM_RowSet)==0 ){ 5675 if( (pIn1->flags & MEM_RowSet)==0 ){
5419 sqlite3VdbeMemSetRowSet(pIn1); 5676 sqlite3VdbeMemSetRowSet(pIn1);
5420 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; 5677 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
5421 } 5678 }
5422 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); 5679 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
5423 break; 5680 break;
5424 } 5681 }
5425 5682
5426 /* Opcode: RowSetRead P1 P2 P3 * * 5683 /* Opcode: RowSetRead P1 P2 P3 * *
5427 ** Synopsis: r[P3]=rowset(P1) 5684 ** Synopsis: r[P3]=rowset(P1)
5428 ** 5685 **
5429 ** Extract the smallest value from boolean index P1 and put that value into 5686 ** Extract the smallest value from boolean index P1 and put that value into
5430 ** register P3. Or, if boolean index P1 is initially empty, leave P3 5687 ** register P3. Or, if boolean index P1 is initially empty, leave P3
5431 ** unchanged and jump to instruction P2. 5688 ** unchanged and jump to instruction P2.
5432 */ 5689 */
5433 case OP_RowSetRead: { /* jump, in1, out3 */ 5690 case OP_RowSetRead: { /* jump, in1, out3 */
5434 i64 val; 5691 i64 val;
5435 5692
5436 pIn1 = &aMem[pOp->p1]; 5693 pIn1 = &aMem[pOp->p1];
5437 if( (pIn1->flags & MEM_RowSet)==0 5694 if( (pIn1->flags & MEM_RowSet)==0
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
5548 ** variable. */ 5805 ** variable. */
5549 if( pOp->p5 ){ 5806 if( pOp->p5 ){
5550 t = pProgram->token; 5807 t = pProgram->token;
5551 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); 5808 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5552 if( pFrame ) break; 5809 if( pFrame ) break;
5553 } 5810 }
5554 5811
5555 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ 5812 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
5556 rc = SQLITE_ERROR; 5813 rc = SQLITE_ERROR;
5557 sqlite3VdbeError(p, "too many levels of trigger recursion"); 5814 sqlite3VdbeError(p, "too many levels of trigger recursion");
5558 break; 5815 goto abort_due_to_error;
5559 } 5816 }
5560 5817
5561 /* Register pRt is used to store the memory required to save the state 5818 /* Register pRt is used to store the memory required to save the state
5562 ** of the current program, and the memory required at runtime to execute 5819 ** of the current program, and the memory required at runtime to execute
5563 ** the trigger program. If this trigger has been fired before, then pRt 5820 ** the trigger program. If this trigger has been fired before, then pRt
5564 ** is already allocated. Otherwise, it must be initialized. */ 5821 ** is already allocated. Otherwise, it must be initialized. */
5565 if( (pRt->flags&MEM_Frame)==0 ){ 5822 if( (pRt->flags&MEM_Frame)==0 ){
5566 /* SubProgram.nMem is set to the number of memory cells used by the 5823 /* SubProgram.nMem is set to the number of memory cells used by the
5567 ** program stored in SubProgram.aOp. As well as these, one memory 5824 ** program stored in SubProgram.aOp. As well as these, one memory
5568 ** cell is required for each cursor used by the program. Set local 5825 ** cell is required for each cursor used by the program. Set local
5569 ** variable nMem (and later, VdbeFrame.nChildMem) to this value. 5826 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5570 */ 5827 */
5571 nMem = pProgram->nMem + pProgram->nCsr; 5828 nMem = pProgram->nMem + pProgram->nCsr;
5829 assert( nMem>0 );
5830 if( pProgram->nCsr==0 ) nMem++;
5572 nByte = ROUND8(sizeof(VdbeFrame)) 5831 nByte = ROUND8(sizeof(VdbeFrame))
5573 + nMem * sizeof(Mem) 5832 + nMem * sizeof(Mem)
5574 + pProgram->nCsr * sizeof(VdbeCursor *) 5833 + pProgram->nCsr * sizeof(VdbeCursor *);
5575 + pProgram->nOnce * sizeof(u8);
5576 pFrame = sqlite3DbMallocZero(db, nByte); 5834 pFrame = sqlite3DbMallocZero(db, nByte);
5577 if( !pFrame ){ 5835 if( !pFrame ){
5578 goto no_mem; 5836 goto no_mem;
5579 } 5837 }
5580 sqlite3VdbeMemRelease(pRt); 5838 sqlite3VdbeMemRelease(pRt);
5581 pRt->flags = MEM_Frame; 5839 pRt->flags = MEM_Frame;
5582 pRt->u.pFrame = pFrame; 5840 pRt->u.pFrame = pFrame;
5583 5841
5584 pFrame->v = p; 5842 pFrame->v = p;
5585 pFrame->nChildMem = nMem; 5843 pFrame->nChildMem = nMem;
5586 pFrame->nChildCsr = pProgram->nCsr; 5844 pFrame->nChildCsr = pProgram->nCsr;
5587 pFrame->pc = (int)(pOp - aOp); 5845 pFrame->pc = (int)(pOp - aOp);
5588 pFrame->aMem = p->aMem; 5846 pFrame->aMem = p->aMem;
5589 pFrame->nMem = p->nMem; 5847 pFrame->nMem = p->nMem;
5590 pFrame->apCsr = p->apCsr; 5848 pFrame->apCsr = p->apCsr;
5591 pFrame->nCursor = p->nCursor; 5849 pFrame->nCursor = p->nCursor;
5592 pFrame->aOp = p->aOp; 5850 pFrame->aOp = p->aOp;
5593 pFrame->nOp = p->nOp; 5851 pFrame->nOp = p->nOp;
5594 pFrame->token = pProgram->token; 5852 pFrame->token = pProgram->token;
5595 pFrame->aOnceFlag = p->aOnceFlag;
5596 pFrame->nOnceFlag = p->nOnceFlag;
5597 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 5853 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
5598 pFrame->anExec = p->anExec; 5854 pFrame->anExec = p->anExec;
5599 #endif 5855 #endif
5600 5856
5601 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; 5857 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5602 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ 5858 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
5603 pMem->flags = MEM_Undefined; 5859 pMem->flags = MEM_Undefined;
5604 pMem->db = db; 5860 pMem->db = db;
5605 } 5861 }
5606 }else{ 5862 }else{
5607 pFrame = pRt->u.pFrame; 5863 pFrame = pRt->u.pFrame;
5608 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem ); 5864 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
5865 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
5609 assert( pProgram->nCsr==pFrame->nChildCsr ); 5866 assert( pProgram->nCsr==pFrame->nChildCsr );
5610 assert( (int)(pOp - aOp)==pFrame->pc ); 5867 assert( (int)(pOp - aOp)==pFrame->pc );
5611 } 5868 }
5612 5869
5613 p->nFrame++; 5870 p->nFrame++;
5614 pFrame->pParent = p->pFrame; 5871 pFrame->pParent = p->pFrame;
5615 pFrame->lastRowid = lastRowid; 5872 pFrame->lastRowid = db->lastRowid;
5616 pFrame->nChange = p->nChange; 5873 pFrame->nChange = p->nChange;
5617 pFrame->nDbChange = p->db->nChange; 5874 pFrame->nDbChange = p->db->nChange;
5875 assert( pFrame->pAuxData==0 );
5876 pFrame->pAuxData = p->pAuxData;
5877 p->pAuxData = 0;
5618 p->nChange = 0; 5878 p->nChange = 0;
5619 p->pFrame = pFrame; 5879 p->pFrame = pFrame;
5620 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; 5880 p->aMem = aMem = VdbeFrameMem(pFrame);
5621 p->nMem = pFrame->nChildMem; 5881 p->nMem = pFrame->nChildMem;
5622 p->nCursor = (u16)pFrame->nChildCsr; 5882 p->nCursor = (u16)pFrame->nChildCsr;
5623 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; 5883 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
5624 p->aOp = aOp = pProgram->aOp; 5884 p->aOp = aOp = pProgram->aOp;
5625 p->nOp = pProgram->nOp; 5885 p->nOp = pProgram->nOp;
5626 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5627 p->nOnceFlag = pProgram->nOnce;
5628 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 5886 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
5629 p->anExec = 0; 5887 p->anExec = 0;
5630 #endif 5888 #endif
5631 pOp = &aOp[-1]; 5889 pOp = &aOp[-1];
5632 memset(p->aOnceFlag, 0, p->nOnceFlag);
5633 5890
5634 break; 5891 break;
5635 } 5892 }
5636 5893
5637 /* Opcode: Param P1 P2 * * * 5894 /* Opcode: Param P1 P2 * * *
5638 ** 5895 **
5639 ** This opcode is only ever present in sub-programs called via the 5896 ** This opcode is only ever present in sub-programs called via the
5640 ** OP_Program instruction. Copy a value currently stored in a memory 5897 ** OP_Program instruction. Copy a value currently stored in a memory
5641 ** cell of the calling (parent) frame to cell P2 in the current frames 5898 ** cell of the calling (parent) frame to cell P2 in the current frames
5642 ** address space. This is used by trigger programs to access the new.* 5899 ** address space. This is used by trigger programs to access the new.*
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
5747 pIn1 = &aMem[pOp->p1]; 6004 pIn1 = &aMem[pOp->p1];
5748 assert( pIn1->flags&MEM_Int ); 6005 assert( pIn1->flags&MEM_Int );
5749 VdbeBranchTaken( pIn1->u.i>0, 2); 6006 VdbeBranchTaken( pIn1->u.i>0, 2);
5750 if( pIn1->u.i>0 ){ 6007 if( pIn1->u.i>0 ){
5751 pIn1->u.i -= pOp->p3; 6008 pIn1->u.i -= pOp->p3;
5752 goto jump_to_p2; 6009 goto jump_to_p2;
5753 } 6010 }
5754 break; 6011 break;
5755 } 6012 }
5756 6013
5757 /* Opcode: SetIfNotPos P1 P2 P3 * * 6014 /* Opcode: OffsetLimit P1 P2 P3 * *
5758 ** Synopsis: if r[P1]<=0 then r[P2]=P3 6015 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
5759 ** 6016 **
5760 ** Register P1 must contain an integer. 6017 ** This opcode performs a commonly used computation associated with
5761 ** If the value of register P1 is not positive (if it is less than 1) then 6018 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
5762 ** set the value of register P2 to be the integer P3. 6019 ** holds the offset counter. The opcode computes the combined value
6020 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6021 ** value computed is the total number of rows that will need to be
6022 ** visited in order to complete the query.
6023 **
6024 ** If r[P3] is zero or negative, that means there is no OFFSET
6025 ** and r[P2] is set to be the value of the LIMIT, r[P1].
6026 **
6027 ** if r[P1] is zero or negative, that means there is no LIMIT
6028 ** and r[P2] is set to -1.
6029 **
6030 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
5763 */ 6031 */
5764 case OP_SetIfNotPos: { /* in1, in2 */ 6032 case OP_OffsetLimit: { /* in1, out2, in3 */
6033 i64 x;
5765 pIn1 = &aMem[pOp->p1]; 6034 pIn1 = &aMem[pOp->p1];
5766 assert( pIn1->flags&MEM_Int ); 6035 pIn3 = &aMem[pOp->p3];
5767 if( pIn1->u.i<=0 ){ 6036 pOut = out2Prerelease(p, pOp);
5768 pOut = out2Prerelease(p, pOp); 6037 assert( pIn1->flags & MEM_Int );
5769 pOut->u.i = pOp->p3; 6038 assert( pIn3->flags & MEM_Int );
6039 x = pIn1->u.i;
6040 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6041 /* If the LIMIT is less than or equal to zero, loop forever. This
6042 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6043 ** also loop forever. This is undocumented. In fact, one could argue
6044 ** that the loop should terminate. But assuming 1 billion iterations
6045 ** per second (far exceeding the capabilities of any current hardware)
6046 ** it would take nearly 300 years to actually reach the limit. So
6047 ** looping forever is a reasonable approximation. */
6048 pOut->u.i = -1;
6049 }else{
6050 pOut->u.i = x;
5770 } 6051 }
5771 break; 6052 break;
5772 } 6053 }
5773 6054
5774 /* Opcode: IfNotZero P1 P2 P3 * * 6055 /* Opcode: IfNotZero P1 P2 * * *
5775 ** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 6056 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
5776 ** 6057 **
5777 ** Register P1 must contain an integer. If the content of register P1 is 6058 ** Register P1 must contain an integer. If the content of register P1 is
5778 ** initially nonzero, then subtract P3 from the value in register P1 and 6059 ** initially greater than zero, then decrement the value in register P1.
5779 ** jump to P2. If register P1 is initially zero, leave it unchanged 6060 ** If it is non-zero (negative or positive) and then also jump to P2.
5780 ** and fall through. 6061 ** If register P1 is initially zero, leave it unchanged and fall through.
5781 */ 6062 */
5782 case OP_IfNotZero: { /* jump, in1 */ 6063 case OP_IfNotZero: { /* jump, in1 */
5783 pIn1 = &aMem[pOp->p1]; 6064 pIn1 = &aMem[pOp->p1];
5784 assert( pIn1->flags&MEM_Int ); 6065 assert( pIn1->flags&MEM_Int );
5785 VdbeBranchTaken(pIn1->u.i<0, 2); 6066 VdbeBranchTaken(pIn1->u.i<0, 2);
5786 if( pIn1->u.i ){ 6067 if( pIn1->u.i ){
5787 pIn1->u.i -= pOp->p3; 6068 if( pIn1->u.i>0 ) pIn1->u.i--;
5788 goto jump_to_p2; 6069 goto jump_to_p2;
5789 } 6070 }
5790 break; 6071 break;
5791 } 6072 }
5792 6073
5793 /* Opcode: DecrJumpZero P1 P2 * * * 6074 /* Opcode: DecrJumpZero P1 P2 * * *
5794 ** Synopsis: if (--r[P1])==0 goto P2 6075 ** Synopsis: if (--r[P1])==0 goto P2
5795 ** 6076 **
5796 ** Register P1 must hold an integer. Decrement the value in register P1 6077 ** Register P1 must hold an integer. Decrement the value in P1
5797 ** then jump to P2 if the new value is exactly zero. 6078 ** and jump to P2 if the new value is exactly zero.
5798 */ 6079 */
5799 case OP_DecrJumpZero: { /* jump, in1 */ 6080 case OP_DecrJumpZero: { /* jump, in1 */
5800 pIn1 = &aMem[pOp->p1]; 6081 pIn1 = &aMem[pOp->p1];
5801 assert( pIn1->flags&MEM_Int ); 6082 assert( pIn1->flags&MEM_Int );
5802 pIn1->u.i--; 6083 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
5803 VdbeBranchTaken(pIn1->u.i==0, 2); 6084 VdbeBranchTaken(pIn1->u.i==0, 2);
5804 if( pIn1->u.i==0 ) goto jump_to_p2; 6085 if( pIn1->u.i==0 ) goto jump_to_p2;
5805 break; 6086 break;
5806 } 6087 }
5807 6088
5808 6089
5809 /* Opcode: JumpZeroIncr P1 P2 * * *
5810 ** Synopsis: if (r[P1]++)==0 ) goto P2
5811 **
5812 ** The register P1 must contain an integer. If register P1 is initially
5813 ** zero, then jump to P2. Increment register P1 regardless of whether or
5814 ** not the jump is taken.
5815 */
5816 case OP_JumpZeroIncr: { /* jump, in1 */
5817 pIn1 = &aMem[pOp->p1];
5818 assert( pIn1->flags&MEM_Int );
5819 VdbeBranchTaken(pIn1->u.i==0, 2);
5820 if( (pIn1->u.i++)==0 ) goto jump_to_p2;
5821 break;
5822 }
5823
5824 /* Opcode: AggStep0 * P2 P3 P4 P5 6090 /* Opcode: AggStep0 * P2 P3 P4 P5
5825 ** Synopsis: accum=r[P3] step(r[P2@P5]) 6091 ** Synopsis: accum=r[P3] step(r[P2@P5])
5826 ** 6092 **
5827 ** Execute the step function for an aggregate. The 6093 ** Execute the step function for an aggregate. The
5828 ** function has P5 arguments. P4 is a pointer to the FuncDef 6094 ** function has P5 arguments. P4 is a pointer to the FuncDef
5829 ** structure that specifies the function. Register P3 is the 6095 ** structure that specifies the function. Register P3 is the
5830 ** accumulator. 6096 ** accumulator.
5831 ** 6097 **
5832 ** The P5 arguments are taken from register P2 and its 6098 ** The P5 arguments are taken from register P2 and its
5833 ** successors. 6099 ** successors.
(...skipping 14 matching lines...) Expand all
5848 ** the opcode is changed. In this way, the initialization of the 6114 ** the opcode is changed. In this way, the initialization of the
5849 ** sqlite3_context only happens once, instead of on each call to the 6115 ** sqlite3_context only happens once, instead of on each call to the
5850 ** step function. 6116 ** step function.
5851 */ 6117 */
5852 case OP_AggStep0: { 6118 case OP_AggStep0: {
5853 int n; 6119 int n;
5854 sqlite3_context *pCtx; 6120 sqlite3_context *pCtx;
5855 6121
5856 assert( pOp->p4type==P4_FUNCDEF ); 6122 assert( pOp->p4type==P4_FUNCDEF );
5857 n = pOp->p5; 6123 n = pOp->p5;
5858 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); 6124 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
5859 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); 6125 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
5860 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 6126 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
5861 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); 6127 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
5862 if( pCtx==0 ) goto no_mem; 6128 if( pCtx==0 ) goto no_mem;
5863 pCtx->pMem = 0; 6129 pCtx->pMem = 0;
5864 pCtx->pFunc = pOp->p4.pFunc; 6130 pCtx->pFunc = pOp->p4.pFunc;
5865 pCtx->iOp = (int)(pOp - aOp); 6131 pCtx->iOp = (int)(pOp - aOp);
5866 pCtx->pVdbe = p; 6132 pCtx->pVdbe = p;
5867 pCtx->argc = n; 6133 pCtx->argc = n;
5868 pOp->p4type = P4_FUNCCTX; 6134 pOp->p4type = P4_FUNCCTX;
5869 pOp->p4.pCtx = pCtx; 6135 pOp->p4.pCtx = pCtx;
5870 pOp->opcode = OP_AggStep; 6136 pOp->opcode = OP_AggStep;
5871 /* Fall through into OP_AggStep */ 6137 /* Fall through into OP_AggStep */
(...skipping 22 matching lines...) Expand all
5894 assert( memIsValid(pCtx->argv[i]) ); 6160 assert( memIsValid(pCtx->argv[i]) );
5895 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 6161 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
5896 } 6162 }
5897 #endif 6163 #endif
5898 6164
5899 pMem->n++; 6165 pMem->n++;
5900 sqlite3VdbeMemInit(&t, db, MEM_Null); 6166 sqlite3VdbeMemInit(&t, db, MEM_Null);
5901 pCtx->pOut = &t; 6167 pCtx->pOut = &t;
5902 pCtx->fErrorOrAux = 0; 6168 pCtx->fErrorOrAux = 0;
5903 pCtx->skipFlag = 0; 6169 pCtx->skipFlag = 0;
5904 (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ 6170 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
5905 if( pCtx->fErrorOrAux ){ 6171 if( pCtx->fErrorOrAux ){
5906 if( pCtx->isError ){ 6172 if( pCtx->isError ){
5907 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); 6173 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
5908 rc = pCtx->isError; 6174 rc = pCtx->isError;
5909 } 6175 }
5910 sqlite3VdbeMemRelease(&t); 6176 sqlite3VdbeMemRelease(&t);
6177 if( rc ) goto abort_due_to_error;
5911 }else{ 6178 }else{
5912 assert( t.flags==MEM_Null ); 6179 assert( t.flags==MEM_Null );
5913 } 6180 }
5914 if( pCtx->skipFlag ){ 6181 if( pCtx->skipFlag ){
5915 assert( pOp[-1].opcode==OP_CollSeq ); 6182 assert( pOp[-1].opcode==OP_CollSeq );
5916 i = pOp[-1].p1; 6183 i = pOp[-1].p1;
5917 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); 6184 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5918 } 6185 }
5919 break; 6186 break;
5920 } 6187 }
5921 6188
5922 /* Opcode: AggFinal P1 P2 * P4 * 6189 /* Opcode: AggFinal P1 P2 * P4 *
5923 ** Synopsis: accum=r[P1] N=P2 6190 ** Synopsis: accum=r[P1] N=P2
5924 ** 6191 **
5925 ** Execute the finalizer function for an aggregate. P1 is 6192 ** Execute the finalizer function for an aggregate. P1 is
5926 ** the memory location that is the accumulator for the aggregate. 6193 ** the memory location that is the accumulator for the aggregate.
5927 ** 6194 **
5928 ** P2 is the number of arguments that the step function takes and 6195 ** P2 is the number of arguments that the step function takes and
5929 ** P4 is a pointer to the FuncDef for this function. The P2 6196 ** P4 is a pointer to the FuncDef for this function. The P2
5930 ** argument is not used by this opcode. It is only there to disambiguate 6197 ** argument is not used by this opcode. It is only there to disambiguate
5931 ** functions that can take varying numbers of arguments. The 6198 ** functions that can take varying numbers of arguments. The
5932 ** P4 argument is only needed for the degenerate case where 6199 ** P4 argument is only needed for the degenerate case where
5933 ** the step function was not previously called. 6200 ** the step function was not previously called.
5934 */ 6201 */
5935 case OP_AggFinal: { 6202 case OP_AggFinal: {
5936 Mem *pMem; 6203 Mem *pMem;
5937 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); 6204 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
5938 pMem = &aMem[pOp->p1]; 6205 pMem = &aMem[pOp->p1];
5939 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); 6206 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
5940 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); 6207 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
5941 if( rc ){ 6208 if( rc ){
5942 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); 6209 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
6210 goto abort_due_to_error;
5943 } 6211 }
5944 sqlite3VdbeChangeEncoding(pMem, encoding); 6212 sqlite3VdbeChangeEncoding(pMem, encoding);
5945 UPDATE_MAX_BLOBSIZE(pMem); 6213 UPDATE_MAX_BLOBSIZE(pMem);
5946 if( sqlite3VdbeMemTooBig(pMem) ){ 6214 if( sqlite3VdbeMemTooBig(pMem) ){
5947 goto too_big; 6215 goto too_big;
5948 } 6216 }
5949 break; 6217 break;
5950 } 6218 }
5951 6219
5952 #ifndef SQLITE_OMIT_WAL 6220 #ifndef SQLITE_OMIT_WAL
(...skipping 15 matching lines...) Expand all
5968 6236
5969 assert( p->readOnly==0 ); 6237 assert( p->readOnly==0 );
5970 aRes[0] = 0; 6238 aRes[0] = 0;
5971 aRes[1] = aRes[2] = -1; 6239 aRes[1] = aRes[2] = -1;
5972 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE 6240 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5973 || pOp->p2==SQLITE_CHECKPOINT_FULL 6241 || pOp->p2==SQLITE_CHECKPOINT_FULL
5974 || pOp->p2==SQLITE_CHECKPOINT_RESTART 6242 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5975 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE 6243 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
5976 ); 6244 );
5977 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); 6245 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
5978 if( rc==SQLITE_BUSY ){ 6246 if( rc ){
6247 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
5979 rc = SQLITE_OK; 6248 rc = SQLITE_OK;
5980 aRes[0] = 1; 6249 aRes[0] = 1;
5981 } 6250 }
5982 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ 6251 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5983 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); 6252 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5984 } 6253 }
5985 break; 6254 break;
5986 }; 6255 };
5987 #endif 6256 #endif
5988 6257
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
6041 6310
6042 if( (eNew!=eOld) 6311 if( (eNew!=eOld)
6043 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) 6312 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6044 ){ 6313 ){
6045 if( !db->autoCommit || db->nVdbeRead>1 ){ 6314 if( !db->autoCommit || db->nVdbeRead>1 ){
6046 rc = SQLITE_ERROR; 6315 rc = SQLITE_ERROR;
6047 sqlite3VdbeError(p, 6316 sqlite3VdbeError(p,
6048 "cannot change %s wal mode from within a transaction", 6317 "cannot change %s wal mode from within a transaction",
6049 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") 6318 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6050 ); 6319 );
6051 break; 6320 goto abort_due_to_error;
6052 }else{ 6321 }else{
6053 6322
6054 if( eOld==PAGER_JOURNALMODE_WAL ){ 6323 if( eOld==PAGER_JOURNALMODE_WAL ){
6055 /* If leaving WAL mode, close the log file. If successful, the call 6324 /* If leaving WAL mode, close the log file. If successful, the call
6056 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 6325 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6057 ** file. An EXCLUSIVE lock may still be held on the database file 6326 ** file. An EXCLUSIVE lock may still be held on the database file
6058 ** after a successful return. 6327 ** after a successful return.
6059 */ 6328 */
6060 rc = sqlite3PagerCloseWal(pPager); 6329 rc = sqlite3PagerCloseWal(pPager, db);
6061 if( rc==SQLITE_OK ){ 6330 if( rc==SQLITE_OK ){
6062 sqlite3PagerSetJournalMode(pPager, eNew); 6331 sqlite3PagerSetJournalMode(pPager, eNew);
6063 } 6332 }
6064 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ 6333 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6065 /* Cannot transition directly from MEMORY to WAL. Use mode OFF 6334 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6066 ** as an intermediate */ 6335 ** as an intermediate */
6067 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); 6336 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
6068 } 6337 }
6069 6338
6070 /* Open a transaction on the database file. Regardless of the journal 6339 /* Open a transaction on the database file. Regardless of the journal
6071 ** mode, this transaction always uses a rollback journal. 6340 ** mode, this transaction always uses a rollback journal.
6072 */ 6341 */
6073 assert( sqlite3BtreeIsInTrans(pBt)==0 ); 6342 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6074 if( rc==SQLITE_OK ){ 6343 if( rc==SQLITE_OK ){
6075 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); 6344 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
6076 } 6345 }
6077 } 6346 }
6078 } 6347 }
6079 #endif /* ifndef SQLITE_OMIT_WAL */ 6348 #endif /* ifndef SQLITE_OMIT_WAL */
6080 6349
6081 if( rc ){ 6350 if( rc ) eNew = eOld;
6082 eNew = eOld;
6083 }
6084 eNew = sqlite3PagerSetJournalMode(pPager, eNew); 6351 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
6085 6352
6086 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 6353 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
6087 pOut->z = (char *)sqlite3JournalModename(eNew); 6354 pOut->z = (char *)sqlite3JournalModename(eNew);
6088 pOut->n = sqlite3Strlen30(pOut->z); 6355 pOut->n = sqlite3Strlen30(pOut->z);
6089 pOut->enc = SQLITE_UTF8; 6356 pOut->enc = SQLITE_UTF8;
6090 sqlite3VdbeChangeEncoding(pOut, encoding); 6357 sqlite3VdbeChangeEncoding(pOut, encoding);
6358 if( rc ) goto abort_due_to_error;
6091 break; 6359 break;
6092 }; 6360 };
6093 #endif /* SQLITE_OMIT_PRAGMA */ 6361 #endif /* SQLITE_OMIT_PRAGMA */
6094 6362
6095 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) 6363 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
6096 /* Opcode: Vacuum * * * * * 6364 /* Opcode: Vacuum P1 * * * *
6097 ** 6365 **
6098 ** Vacuum the entire database. This opcode will cause other virtual 6366 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6099 ** machines to be created and run. It may not be called from within 6367 ** for an attached database. The "temp" database may not be vacuumed.
6100 ** a transaction.
6101 */ 6368 */
6102 case OP_Vacuum: { 6369 case OP_Vacuum: {
6103 assert( p->readOnly==0 ); 6370 assert( p->readOnly==0 );
6104 rc = sqlite3RunVacuum(&p->zErrMsg, db); 6371 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
6372 if( rc ) goto abort_due_to_error;
6105 break; 6373 break;
6106 } 6374 }
6107 #endif 6375 #endif
6108 6376
6109 #if !defined(SQLITE_OMIT_AUTOVACUUM) 6377 #if !defined(SQLITE_OMIT_AUTOVACUUM)
6110 /* Opcode: IncrVacuum P1 P2 * * * 6378 /* Opcode: IncrVacuum P1 P2 * * *
6111 ** 6379 **
6112 ** Perform a single step of the incremental vacuum procedure on 6380 ** Perform a single step of the incremental vacuum procedure on
6113 ** the P1 database. If the vacuum has finished, jump to instruction 6381 ** the P1 database. If the vacuum has finished, jump to instruction
6114 ** P2. Otherwise, fall through to the next instruction. 6382 ** P2. Otherwise, fall through to the next instruction.
6115 */ 6383 */
6116 case OP_IncrVacuum: { /* jump */ 6384 case OP_IncrVacuum: { /* jump */
6117 Btree *pBt; 6385 Btree *pBt;
6118 6386
6119 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 6387 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6120 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 6388 assert( DbMaskTest(p->btreeMask, pOp->p1) );
6121 assert( p->readOnly==0 ); 6389 assert( p->readOnly==0 );
6122 pBt = db->aDb[pOp->p1].pBt; 6390 pBt = db->aDb[pOp->p1].pBt;
6123 rc = sqlite3BtreeIncrVacuum(pBt); 6391 rc = sqlite3BtreeIncrVacuum(pBt);
6124 VdbeBranchTaken(rc==SQLITE_DONE,2); 6392 VdbeBranchTaken(rc==SQLITE_DONE,2);
6125 if( rc==SQLITE_DONE ){ 6393 if( rc ){
6394 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
6126 rc = SQLITE_OK; 6395 rc = SQLITE_OK;
6127 goto jump_to_p2; 6396 goto jump_to_p2;
6128 } 6397 }
6129 break; 6398 break;
6130 } 6399 }
6131 #endif 6400 #endif
6132 6401
6133 /* Opcode: Expire P1 * * * * 6402 /* Opcode: Expire P1 * * * *
6134 ** 6403 **
6135 ** Cause precompiled statements to expire. When an expired statement 6404 ** Cause precompiled statements to expire. When an expired statement
(...skipping 30 matching lines...) Expand all
6166 ** used to generate an error message if the lock cannot be obtained. 6435 ** used to generate an error message if the lock cannot be obtained.
6167 */ 6436 */
6168 case OP_TableLock: { 6437 case OP_TableLock: {
6169 u8 isWriteLock = (u8)pOp->p3; 6438 u8 isWriteLock = (u8)pOp->p3;
6170 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ 6439 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
6171 int p1 = pOp->p1; 6440 int p1 = pOp->p1;
6172 assert( p1>=0 && p1<db->nDb ); 6441 assert( p1>=0 && p1<db->nDb );
6173 assert( DbMaskTest(p->btreeMask, p1) ); 6442 assert( DbMaskTest(p->btreeMask, p1) );
6174 assert( isWriteLock==0 || isWriteLock==1 ); 6443 assert( isWriteLock==0 || isWriteLock==1 );
6175 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); 6444 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
6176 if( (rc&0xFF)==SQLITE_LOCKED ){ 6445 if( rc ){
6177 const char *z = pOp->p4.z; 6446 if( (rc&0xFF)==SQLITE_LOCKED ){
6178 sqlite3VdbeError(p, "database table is locked: %s", z); 6447 const char *z = pOp->p4.z;
6448 sqlite3VdbeError(p, "database table is locked: %s", z);
6449 }
6450 goto abort_due_to_error;
6179 } 6451 }
6180 } 6452 }
6181 break; 6453 break;
6182 } 6454 }
6183 #endif /* SQLITE_OMIT_SHARED_CACHE */ 6455 #endif /* SQLITE_OMIT_SHARED_CACHE */
6184 6456
6185 #ifndef SQLITE_OMIT_VIRTUALTABLE 6457 #ifndef SQLITE_OMIT_VIRTUALTABLE
6186 /* Opcode: VBegin * * * P4 * 6458 /* Opcode: VBegin * * * P4 *
6187 ** 6459 **
6188 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 6460 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6189 ** xBegin method for that table. 6461 ** xBegin method for that table.
6190 ** 6462 **
6191 ** Also, whether or not P4 is set, check that this is not being called from 6463 ** Also, whether or not P4 is set, check that this is not being called from
6192 ** within a callback to a virtual table xSync() method. If it is, the error 6464 ** within a callback to a virtual table xSync() method. If it is, the error
6193 ** code will be set to SQLITE_LOCKED. 6465 ** code will be set to SQLITE_LOCKED.
6194 */ 6466 */
6195 case OP_VBegin: { 6467 case OP_VBegin: {
6196 VTable *pVTab; 6468 VTable *pVTab;
6197 pVTab = pOp->p4.pVtab; 6469 pVTab = pOp->p4.pVtab;
6198 rc = sqlite3VtabBegin(db, pVTab); 6470 rc = sqlite3VtabBegin(db, pVTab);
6199 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); 6471 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
6472 if( rc ) goto abort_due_to_error;
6200 break; 6473 break;
6201 } 6474 }
6202 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6475 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6203 6476
6204 #ifndef SQLITE_OMIT_VIRTUALTABLE 6477 #ifndef SQLITE_OMIT_VIRTUALTABLE
6205 /* Opcode: VCreate P1 P2 * * * 6478 /* Opcode: VCreate P1 P2 * * *
6206 ** 6479 **
6207 ** P2 is a register that holds the name of a virtual table in database 6480 ** P2 is a register that holds the name of a virtual table in database
6208 ** P1. Call the xCreate method for that table. 6481 ** P1. Call the xCreate method for that table.
6209 */ 6482 */
6210 case OP_VCreate: { 6483 case OP_VCreate: {
6211 Mem sMem; /* For storing the record being decoded */ 6484 Mem sMem; /* For storing the record being decoded */
6212 const char *zTab; /* Name of the virtual table */ 6485 const char *zTab; /* Name of the virtual table */
6213 6486
6214 memset(&sMem, 0, sizeof(sMem)); 6487 memset(&sMem, 0, sizeof(sMem));
6215 sMem.db = db; 6488 sMem.db = db;
6216 /* Because P2 is always a static string, it is impossible for the 6489 /* Because P2 is always a static string, it is impossible for the
6217 ** sqlite3VdbeMemCopy() to fail */ 6490 ** sqlite3VdbeMemCopy() to fail */
6218 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); 6491 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6219 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); 6492 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
6220 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); 6493 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
6221 assert( rc==SQLITE_OK ); 6494 assert( rc==SQLITE_OK );
6222 zTab = (const char*)sqlite3_value_text(&sMem); 6495 zTab = (const char*)sqlite3_value_text(&sMem);
6223 assert( zTab || db->mallocFailed ); 6496 assert( zTab || db->mallocFailed );
6224 if( zTab ){ 6497 if( zTab ){
6225 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); 6498 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
6226 } 6499 }
6227 sqlite3VdbeMemRelease(&sMem); 6500 sqlite3VdbeMemRelease(&sMem);
6501 if( rc ) goto abort_due_to_error;
6228 break; 6502 break;
6229 } 6503 }
6230 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6504 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6231 6505
6232 #ifndef SQLITE_OMIT_VIRTUALTABLE 6506 #ifndef SQLITE_OMIT_VIRTUALTABLE
6233 /* Opcode: VDestroy P1 * * P4 * 6507 /* Opcode: VDestroy P1 * * P4 *
6234 ** 6508 **
6235 ** P4 is the name of a virtual table in database P1. Call the xDestroy method 6509 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
6236 ** of that table. 6510 ** of that table.
6237 */ 6511 */
6238 case OP_VDestroy: { 6512 case OP_VDestroy: {
6239 db->nVDestroy++; 6513 db->nVDestroy++;
6240 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); 6514 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
6241 db->nVDestroy--; 6515 db->nVDestroy--;
6516 if( rc ) goto abort_due_to_error;
6242 break; 6517 break;
6243 } 6518 }
6244 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6519 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6245 6520
6246 #ifndef SQLITE_OMIT_VIRTUALTABLE 6521 #ifndef SQLITE_OMIT_VIRTUALTABLE
6247 /* Opcode: VOpen P1 * * P4 * 6522 /* Opcode: VOpen P1 * * P4 *
6248 ** 6523 **
6249 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 6524 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6250 ** P1 is a cursor number. This opcode opens a cursor to the virtual 6525 ** P1 is a cursor number. This opcode opens a cursor to the virtual
6251 ** table and stores that cursor in P1. 6526 ** table and stores that cursor in P1.
6252 */ 6527 */
6253 case OP_VOpen: { 6528 case OP_VOpen: {
6254 VdbeCursor *pCur; 6529 VdbeCursor *pCur;
6255 sqlite3_vtab_cursor *pVCur; 6530 sqlite3_vtab_cursor *pVCur;
6256 sqlite3_vtab *pVtab; 6531 sqlite3_vtab *pVtab;
6257 const sqlite3_module *pModule; 6532 const sqlite3_module *pModule;
6258 6533
6259 assert( p->bIsReader ); 6534 assert( p->bIsReader );
6260 pCur = 0; 6535 pCur = 0;
6261 pVCur = 0; 6536 pVCur = 0;
6262 pVtab = pOp->p4.pVtab->pVtab; 6537 pVtab = pOp->p4.pVtab->pVtab;
6263 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 6538 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6264 rc = SQLITE_LOCKED; 6539 rc = SQLITE_LOCKED;
6265 break; 6540 goto abort_due_to_error;
6266 } 6541 }
6267 pModule = pVtab->pModule; 6542 pModule = pVtab->pModule;
6268 rc = pModule->xOpen(pVtab, &pVCur); 6543 rc = pModule->xOpen(pVtab, &pVCur);
6269 sqlite3VtabImportErrmsg(p, pVtab); 6544 sqlite3VtabImportErrmsg(p, pVtab);
6270 if( SQLITE_OK==rc ){ 6545 if( rc ) goto abort_due_to_error;
6271 /* Initialize sqlite3_vtab_cursor base class */
6272 pVCur->pVtab = pVtab;
6273 6546
6274 /* Initialize vdbe cursor object */ 6547 /* Initialize sqlite3_vtab_cursor base class */
6275 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); 6548 pVCur->pVtab = pVtab;
6276 if( pCur ){ 6549
6277 pCur->uc.pVCur = pVCur; 6550 /* Initialize vdbe cursor object */
6278 pVtab->nRef++; 6551 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6279 }else{ 6552 if( pCur ){
6280 assert( db->mallocFailed ); 6553 pCur->uc.pVCur = pVCur;
6281 pModule->xClose(pVCur); 6554 pVtab->nRef++;
6282 goto no_mem; 6555 }else{
6283 } 6556 assert( db->mallocFailed );
6557 pModule->xClose(pVCur);
6558 goto no_mem;
6284 } 6559 }
6285 break; 6560 break;
6286 } 6561 }
6287 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6562 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6288 6563
6289 #ifndef SQLITE_OMIT_VIRTUALTABLE 6564 #ifndef SQLITE_OMIT_VIRTUALTABLE
6290 /* Opcode: VFilter P1 P2 P3 P4 * 6565 /* Opcode: VFilter P1 P2 P3 P4 *
6291 ** Synopsis: iplan=r[P3] zplan='P4' 6566 ** Synopsis: iplan=r[P3] zplan='P4'
6292 ** 6567 **
6293 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if 6568 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
6335 iQuery = (int)pQuery->u.i; 6610 iQuery = (int)pQuery->u.i;
6336 6611
6337 /* Invoke the xFilter method */ 6612 /* Invoke the xFilter method */
6338 res = 0; 6613 res = 0;
6339 apArg = p->apArg; 6614 apArg = p->apArg;
6340 for(i = 0; i<nArg; i++){ 6615 for(i = 0; i<nArg; i++){
6341 apArg[i] = &pArgc[i+1]; 6616 apArg[i] = &pArgc[i+1];
6342 } 6617 }
6343 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); 6618 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
6344 sqlite3VtabImportErrmsg(p, pVtab); 6619 sqlite3VtabImportErrmsg(p, pVtab);
6345 if( rc==SQLITE_OK ){ 6620 if( rc ) goto abort_due_to_error;
6346 res = pModule->xEof(pVCur); 6621 res = pModule->xEof(pVCur);
6347 }
6348 pCur->nullRow = 0; 6622 pCur->nullRow = 0;
6349 VdbeBranchTaken(res!=0,2); 6623 VdbeBranchTaken(res!=0,2);
6350 if( res ) goto jump_to_p2; 6624 if( res ) goto jump_to_p2;
6351 break; 6625 break;
6352 } 6626 }
6353 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6627 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6354 6628
6355 #ifndef SQLITE_OMIT_VIRTUALTABLE 6629 #ifndef SQLITE_OMIT_VIRTUALTABLE
6356 /* Opcode: VColumn P1 P2 P3 * * 6630 /* Opcode: VColumn P1 P2 P3 * *
6357 ** Synopsis: r[P3]=vcolumn(P2) 6631 ** Synopsis: r[P3]=vcolumn(P2)
6358 ** 6632 **
6359 ** Store the value of the P2-th column of 6633 ** Store the value of the P2-th column of
6360 ** the row of the virtual-table that the 6634 ** the row of the virtual-table that the
6361 ** P1 cursor is pointing to into register P3. 6635 ** P1 cursor is pointing to into register P3.
6362 */ 6636 */
6363 case OP_VColumn: { 6637 case OP_VColumn: {
6364 sqlite3_vtab *pVtab; 6638 sqlite3_vtab *pVtab;
6365 const sqlite3_module *pModule; 6639 const sqlite3_module *pModule;
6366 Mem *pDest; 6640 Mem *pDest;
6367 sqlite3_context sContext; 6641 sqlite3_context sContext;
6368 6642
6369 VdbeCursor *pCur = p->apCsr[pOp->p1]; 6643 VdbeCursor *pCur = p->apCsr[pOp->p1];
6370 assert( pCur->eCurType==CURTYPE_VTAB ); 6644 assert( pCur->eCurType==CURTYPE_VTAB );
6371 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); 6645 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6372 pDest = &aMem[pOp->p3]; 6646 pDest = &aMem[pOp->p3];
6373 memAboutToChange(p, pDest); 6647 memAboutToChange(p, pDest);
6374 if( pCur->nullRow ){ 6648 if( pCur->nullRow ){
6375 sqlite3VdbeMemSetNull(pDest); 6649 sqlite3VdbeMemSetNull(pDest);
6376 break; 6650 break;
6377 } 6651 }
6378 pVtab = pCur->uc.pVCur->pVtab; 6652 pVtab = pCur->uc.pVCur->pVtab;
6379 pModule = pVtab->pModule; 6653 pModule = pVtab->pModule;
6380 assert( pModule->xColumn ); 6654 assert( pModule->xColumn );
6381 memset(&sContext, 0, sizeof(sContext)); 6655 memset(&sContext, 0, sizeof(sContext));
6382 sContext.pOut = pDest; 6656 sContext.pOut = pDest;
6383 MemSetTypeFlag(pDest, MEM_Null); 6657 MemSetTypeFlag(pDest, MEM_Null);
6384 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); 6658 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
6385 sqlite3VtabImportErrmsg(p, pVtab); 6659 sqlite3VtabImportErrmsg(p, pVtab);
6386 if( sContext.isError ){ 6660 if( sContext.isError ){
6387 rc = sContext.isError; 6661 rc = sContext.isError;
6388 } 6662 }
6389 sqlite3VdbeChangeEncoding(pDest, encoding); 6663 sqlite3VdbeChangeEncoding(pDest, encoding);
6390 REGISTER_TRACE(pOp->p3, pDest); 6664 REGISTER_TRACE(pOp->p3, pDest);
6391 UPDATE_MAX_BLOBSIZE(pDest); 6665 UPDATE_MAX_BLOBSIZE(pDest);
6392 6666
6393 if( sqlite3VdbeMemTooBig(pDest) ){ 6667 if( sqlite3VdbeMemTooBig(pDest) ){
6394 goto too_big; 6668 goto too_big;
6395 } 6669 }
6670 if( rc ) goto abort_due_to_error;
6396 break; 6671 break;
6397 } 6672 }
6398 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6673 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6399 6674
6400 #ifndef SQLITE_OMIT_VIRTUALTABLE 6675 #ifndef SQLITE_OMIT_VIRTUALTABLE
6401 /* Opcode: VNext P1 P2 * * * 6676 /* Opcode: VNext P1 P2 * * *
6402 ** 6677 **
6403 ** Advance virtual table P1 to the next row in its result set and 6678 ** Advance virtual table P1 to the next row in its result set and
6404 ** jump to instruction P2. Or, if the virtual table has reached 6679 ** jump to instruction P2. Or, if the virtual table has reached
6405 ** the end of its result set, then fall through to the next instruction. 6680 ** the end of its result set, then fall through to the next instruction.
(...skipping 15 matching lines...) Expand all
6421 assert( pModule->xNext ); 6696 assert( pModule->xNext );
6422 6697
6423 /* Invoke the xNext() method of the module. There is no way for the 6698 /* Invoke the xNext() method of the module. There is no way for the
6424 ** underlying implementation to return an error if one occurs during 6699 ** underlying implementation to return an error if one occurs during
6425 ** xNext(). Instead, if an error occurs, true is returned (indicating that 6700 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6426 ** data is available) and the error code returned when xColumn or 6701 ** data is available) and the error code returned when xColumn or
6427 ** some other method is next invoked on the save virtual table cursor. 6702 ** some other method is next invoked on the save virtual table cursor.
6428 */ 6703 */
6429 rc = pModule->xNext(pCur->uc.pVCur); 6704 rc = pModule->xNext(pCur->uc.pVCur);
6430 sqlite3VtabImportErrmsg(p, pVtab); 6705 sqlite3VtabImportErrmsg(p, pVtab);
6431 if( rc==SQLITE_OK ){ 6706 if( rc ) goto abort_due_to_error;
6432 res = pModule->xEof(pCur->uc.pVCur); 6707 res = pModule->xEof(pCur->uc.pVCur);
6433 }
6434 VdbeBranchTaken(!res,2); 6708 VdbeBranchTaken(!res,2);
6435 if( !res ){ 6709 if( !res ){
6436 /* If there is data, jump to P2 */ 6710 /* If there is data, jump to P2 */
6437 goto jump_to_p2_and_check_for_interrupt; 6711 goto jump_to_p2_and_check_for_interrupt;
6438 } 6712 }
6439 goto check_for_interrupt; 6713 goto check_for_interrupt;
6440 } 6714 }
6441 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6715 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6442 6716
6443 #ifndef SQLITE_OMIT_VIRTUALTABLE 6717 #ifndef SQLITE_OMIT_VIRTUALTABLE
(...skipping 11 matching lines...) Expand all
6455 pName = &aMem[pOp->p1]; 6729 pName = &aMem[pOp->p1];
6456 assert( pVtab->pModule->xRename ); 6730 assert( pVtab->pModule->xRename );
6457 assert( memIsValid(pName) ); 6731 assert( memIsValid(pName) );
6458 assert( p->readOnly==0 ); 6732 assert( p->readOnly==0 );
6459 REGISTER_TRACE(pOp->p1, pName); 6733 REGISTER_TRACE(pOp->p1, pName);
6460 assert( pName->flags & MEM_Str ); 6734 assert( pName->flags & MEM_Str );
6461 testcase( pName->enc==SQLITE_UTF8 ); 6735 testcase( pName->enc==SQLITE_UTF8 );
6462 testcase( pName->enc==SQLITE_UTF16BE ); 6736 testcase( pName->enc==SQLITE_UTF16BE );
6463 testcase( pName->enc==SQLITE_UTF16LE ); 6737 testcase( pName->enc==SQLITE_UTF16LE );
6464 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); 6738 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6465 if( rc==SQLITE_OK ){ 6739 if( rc ) goto abort_due_to_error;
6466 rc = pVtab->pModule->xRename(pVtab, pName->z); 6740 rc = pVtab->pModule->xRename(pVtab, pName->z);
6467 sqlite3VtabImportErrmsg(p, pVtab); 6741 sqlite3VtabImportErrmsg(p, pVtab);
6468 p->expired = 0; 6742 p->expired = 0;
6469 } 6743 if( rc ) goto abort_due_to_error;
6470 break; 6744 break;
6471 } 6745 }
6472 #endif 6746 #endif
6473 6747
6474 #ifndef SQLITE_OMIT_VIRTUALTABLE 6748 #ifndef SQLITE_OMIT_VIRTUALTABLE
6475 /* Opcode: VUpdate P1 P2 P3 P4 P5 6749 /* Opcode: VUpdate P1 P2 P3 P4 P5
6476 ** Synopsis: data=r[P3@P2] 6750 ** Synopsis: data=r[P3@P2]
6477 ** 6751 **
6478 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 6752 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6479 ** This opcode invokes the corresponding xUpdate method. P2 values 6753 ** This opcode invokes the corresponding xUpdate method. P2 values
(...skipping 28 matching lines...) Expand all
6508 Mem **apArg; 6782 Mem **apArg;
6509 Mem *pX; 6783 Mem *pX;
6510 6784
6511 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback 6785 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6512 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace 6786 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6513 ); 6787 );
6514 assert( p->readOnly==0 ); 6788 assert( p->readOnly==0 );
6515 pVtab = pOp->p4.pVtab->pVtab; 6789 pVtab = pOp->p4.pVtab->pVtab;
6516 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 6790 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6517 rc = SQLITE_LOCKED; 6791 rc = SQLITE_LOCKED;
6518 break; 6792 goto abort_due_to_error;
6519 } 6793 }
6520 pModule = pVtab->pModule; 6794 pModule = pVtab->pModule;
6521 nArg = pOp->p2; 6795 nArg = pOp->p2;
6522 assert( pOp->p4type==P4_VTAB ); 6796 assert( pOp->p4type==P4_VTAB );
6523 if( ALWAYS(pModule->xUpdate) ){ 6797 if( ALWAYS(pModule->xUpdate) ){
6524 u8 vtabOnConflict = db->vtabOnConflict; 6798 u8 vtabOnConflict = db->vtabOnConflict;
6525 apArg = p->apArg; 6799 apArg = p->apArg;
6526 pX = &aMem[pOp->p3]; 6800 pX = &aMem[pOp->p3];
6527 for(i=0; i<nArg; i++){ 6801 for(i=0; i<nArg; i++){
6528 assert( memIsValid(pX) ); 6802 assert( memIsValid(pX) );
6529 memAboutToChange(p, pX); 6803 memAboutToChange(p, pX);
6530 apArg[i] = pX; 6804 apArg[i] = pX;
6531 pX++; 6805 pX++;
6532 } 6806 }
6533 db->vtabOnConflict = pOp->p5; 6807 db->vtabOnConflict = pOp->p5;
6534 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); 6808 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
6535 db->vtabOnConflict = vtabOnConflict; 6809 db->vtabOnConflict = vtabOnConflict;
6536 sqlite3VtabImportErrmsg(p, pVtab); 6810 sqlite3VtabImportErrmsg(p, pVtab);
6537 if( rc==SQLITE_OK && pOp->p1 ){ 6811 if( rc==SQLITE_OK && pOp->p1 ){
6538 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); 6812 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
6539 db->lastRowid = lastRowid = rowid; 6813 db->lastRowid = rowid;
6540 } 6814 }
6541 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ 6815 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
6542 if( pOp->p5==OE_Ignore ){ 6816 if( pOp->p5==OE_Ignore ){
6543 rc = SQLITE_OK; 6817 rc = SQLITE_OK;
6544 }else{ 6818 }else{
6545 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); 6819 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6546 } 6820 }
6547 }else{ 6821 }else{
6548 p->nChange++; 6822 p->nChange++;
6549 } 6823 }
6824 if( rc ) goto abort_due_to_error;
6550 } 6825 }
6551 break; 6826 break;
6552 } 6827 }
6553 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6828 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6554 6829
6555 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 6830 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
6556 /* Opcode: Pagecount P1 P2 * * * 6831 /* Opcode: Pagecount P1 P2 * * *
6557 ** 6832 **
6558 ** Write the current number of pages in database P1 to memory cell P2. 6833 ** Write the current number of pages in database P1 to memory cell P2.
6559 */ 6834 */
(...skipping 24 matching lines...) Expand all
6584 if( pOp->p3 ){ 6859 if( pOp->p3 ){
6585 newMax = sqlite3BtreeLastPage(pBt); 6860 newMax = sqlite3BtreeLastPage(pBt);
6586 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; 6861 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
6587 } 6862 }
6588 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); 6863 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
6589 break; 6864 break;
6590 } 6865 }
6591 #endif 6866 #endif
6592 6867
6593 6868
6594 /* Opcode: Init * P2 * P4 * 6869 /* Opcode: Init P1 P2 * P4 *
6595 ** Synopsis: Start at P2 6870 ** Synopsis: Start at P2
6596 ** 6871 **
6597 ** Programs contain a single instance of this opcode as the very first 6872 ** Programs contain a single instance of this opcode as the very first
6598 ** opcode. 6873 ** opcode.
6599 ** 6874 **
6600 ** If tracing is enabled (by the sqlite3_trace()) interface, then 6875 ** If tracing is enabled (by the sqlite3_trace()) interface, then
6601 ** the UTF-8 string contained in P4 is emitted on the trace callback. 6876 ** the UTF-8 string contained in P4 is emitted on the trace callback.
6602 ** Or if P4 is blank, use the string returned by sqlite3_sql(). 6877 ** Or if P4 is blank, use the string returned by sqlite3_sql().
6603 ** 6878 **
6604 ** If P2 is not zero, jump to instruction P2. 6879 ** If P2 is not zero, jump to instruction P2.
6880 **
6881 ** Increment the value of P1 so that OP_Once opcodes will jump the
6882 ** first time they are evaluated for this run.
6605 */ 6883 */
6606 case OP_Init: { /* jump */ 6884 case OP_Init: { /* jump */
6607 char *zTrace; 6885 char *zTrace;
6608 char *z; 6886 int i;
6887
6888 /* If the P4 argument is not NULL, then it must be an SQL comment string.
6889 ** The "--" string is broken up to prevent false-positives with srcck1.c.
6890 **
6891 ** This assert() provides evidence for:
6892 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
6893 ** would have been returned by the legacy sqlite3_trace() interface by
6894 ** using the X argument when X begins with "--" and invoking
6895 ** sqlite3_expanded_sql(P) otherwise.
6896 */
6897 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
6898 assert( pOp==p->aOp ); /* Always instruction 0 */
6609 6899
6610 #ifndef SQLITE_OMIT_TRACE 6900 #ifndef SQLITE_OMIT_TRACE
6611 if( db->xTrace 6901 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
6612 && !p->doingRerun 6902 && !p->doingRerun
6613 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 6903 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6614 ){ 6904 ){
6615 z = sqlite3VdbeExpandSql(p, zTrace); 6905 #ifndef SQLITE_OMIT_DEPRECATED
6616 db->xTrace(db->pTraceArg, z); 6906 if( db->mTrace & SQLITE_TRACE_LEGACY ){
6617 sqlite3DbFree(db, z); 6907 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
6908 char *z = sqlite3VdbeExpandSql(p, zTrace);
6909 x(db->pTraceArg, z);
6910 sqlite3_free(z);
6911 }else
6912 #endif
6913 {
6914 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
6915 }
6618 } 6916 }
6619 #ifdef SQLITE_USE_FCNTL_TRACE 6917 #ifdef SQLITE_USE_FCNTL_TRACE
6620 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); 6918 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6621 if( zTrace ){ 6919 if( zTrace ){
6622 int i; 6920 int j;
6623 for(i=0; i<db->nDb; i++){ 6921 for(j=0; j<db->nDb; j++){
6624 if( DbMaskTest(p->btreeMask, i)==0 ) continue; 6922 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
6625 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace); 6923 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
6626 } 6924 }
6627 } 6925 }
6628 #endif /* SQLITE_USE_FCNTL_TRACE */ 6926 #endif /* SQLITE_USE_FCNTL_TRACE */
6629 #ifdef SQLITE_DEBUG 6927 #ifdef SQLITE_DEBUG
6630 if( (db->flags & SQLITE_SqlTrace)!=0 6928 if( (db->flags & SQLITE_SqlTrace)!=0
6631 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 6929 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6632 ){ 6930 ){
6633 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); 6931 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6634 } 6932 }
6635 #endif /* SQLITE_DEBUG */ 6933 #endif /* SQLITE_DEBUG */
6636 #endif /* SQLITE_OMIT_TRACE */ 6934 #endif /* SQLITE_OMIT_TRACE */
6637 if( pOp->p2 ) goto jump_to_p2; 6935 assert( pOp->p2>0 );
6638 break; 6936 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
6937 for(i=1; i<p->nOp; i++){
6938 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
6939 }
6940 pOp->p1 = 0;
6941 }
6942 pOp->p1++;
6943 goto jump_to_p2;
6639 } 6944 }
6640 6945
6641 #ifdef SQLITE_ENABLE_CURSOR_HINTS 6946 #ifdef SQLITE_ENABLE_CURSOR_HINTS
6642 /* Opcode: CursorHint P1 * * P4 * 6947 /* Opcode: CursorHint P1 * * P4 *
6643 ** 6948 **
6644 ** Provide a hint to cursor P1 that it only needs to return rows that 6949 ** Provide a hint to cursor P1 that it only needs to return rows that
6645 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer 6950 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
6646 ** to values currently held in registers. TK_COLUMN terms in the P4 6951 ** to values currently held in registers. TK_COLUMN terms in the P4
6647 ** expression refer to columns in the b-tree to which cursor P1 is pointing. 6952 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
6648 */ 6953 */
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
6696 /* The following code adds nothing to the actual functionality 7001 /* The following code adds nothing to the actual functionality
6697 ** of the program. It is only here for testing and debugging. 7002 ** of the program. It is only here for testing and debugging.
6698 ** On the other hand, it does burn CPU cycles every time through 7003 ** On the other hand, it does burn CPU cycles every time through
6699 ** the evaluator loop. So we can leave it out when NDEBUG is defined. 7004 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6700 */ 7005 */
6701 #ifndef NDEBUG 7006 #ifndef NDEBUG
6702 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); 7007 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
6703 7008
6704 #ifdef SQLITE_DEBUG 7009 #ifdef SQLITE_DEBUG
6705 if( db->flags & SQLITE_VdbeTrace ){ 7010 if( db->flags & SQLITE_VdbeTrace ){
7011 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
6706 if( rc!=0 ) printf("rc=%d\n",rc); 7012 if( rc!=0 ) printf("rc=%d\n",rc);
6707 if( pOrigOp->opflags & (OPFLG_OUT2) ){ 7013 if( opProperty & (OPFLG_OUT2) ){
6708 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); 7014 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
6709 } 7015 }
6710 if( pOrigOp->opflags & OPFLG_OUT3 ){ 7016 if( opProperty & OPFLG_OUT3 ){
6711 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); 7017 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
6712 } 7018 }
6713 } 7019 }
6714 #endif /* SQLITE_DEBUG */ 7020 #endif /* SQLITE_DEBUG */
6715 #endif /* NDEBUG */ 7021 #endif /* NDEBUG */
6716 } /* The end of the for(;;) loop the loops through opcodes */ 7022 } /* The end of the for(;;) loop the loops through opcodes */
6717 7023
6718 /* If we reach this point, it means that execution is finished with 7024 /* If we reach this point, it means that execution is finished with
6719 ** an error of some kind. 7025 ** an error of some kind.
6720 */ 7026 */
6721 vdbe_error_halt: 7027 abort_due_to_error:
7028 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
6722 assert( rc ); 7029 assert( rc );
7030 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7031 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7032 }
6723 p->rc = rc; 7033 p->rc = rc;
7034 sqlite3SystemError(db, rc);
6724 testcase( sqlite3GlobalConfig.xLog!=0 ); 7035 testcase( sqlite3GlobalConfig.xLog!=0 );
6725 sqlite3_log(rc, "statement aborts at %d: [%s] %s", 7036 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6726 (int)(pOp - aOp), p->zSql, p->zErrMsg); 7037 (int)(pOp - aOp), p->zSql, p->zErrMsg);
6727 sqlite3VdbeHalt(p); 7038 sqlite3VdbeHalt(p);
6728 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; 7039 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
6729 rc = SQLITE_ERROR; 7040 rc = SQLITE_ERROR;
6730 if( resetSchemaOnFault>0 ){ 7041 if( resetSchemaOnFault>0 ){
6731 sqlite3ResetOneSchema(db, resetSchemaOnFault-1); 7042 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
6732 } 7043 }
6733 7044
6734 /* This is the only way out of this procedure. We have to 7045 /* This is the only way out of this procedure. We have to
6735 ** release the mutexes on btrees that were acquired at the 7046 ** release the mutexes on btrees that were acquired at the
6736 ** top. */ 7047 ** top. */
6737 vdbe_return: 7048 vdbe_return:
6738 db->lastRowid = lastRowid;
6739 testcase( nVmStep>0 ); 7049 testcase( nVmStep>0 );
6740 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; 7050 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
6741 sqlite3VdbeLeave(p); 7051 sqlite3VdbeLeave(p);
7052 assert( rc!=SQLITE_OK || nExtraDelete==0
7053 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7054 );
6742 return rc; 7055 return rc;
6743 7056
6744 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH 7057 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6745 ** is encountered. 7058 ** is encountered.
6746 */ 7059 */
6747 too_big: 7060 too_big:
6748 sqlite3VdbeError(p, "string or blob too big"); 7061 sqlite3VdbeError(p, "string or blob too big");
6749 rc = SQLITE_TOOBIG; 7062 rc = SQLITE_TOOBIG;
6750 goto vdbe_error_halt; 7063 goto abort_due_to_error;
6751 7064
6752 /* Jump to here if a malloc() fails. 7065 /* Jump to here if a malloc() fails.
6753 */ 7066 */
6754 no_mem: 7067 no_mem:
6755 db->mallocFailed = 1; 7068 sqlite3OomFault(db);
6756 sqlite3VdbeError(p, "out of memory"); 7069 sqlite3VdbeError(p, "out of memory");
6757 rc = SQLITE_NOMEM; 7070 rc = SQLITE_NOMEM_BKPT;
6758 goto vdbe_error_halt; 7071 goto abort_due_to_error;
6759
6760 /* Jump to here for any other kind of fatal error. The "rc" variable
6761 ** should hold the error number.
6762 */
6763 abort_due_to_error:
6764 assert( p->zErrMsg==0 );
6765 if( db->mallocFailed ) rc = SQLITE_NOMEM;
6766 if( rc!=SQLITE_IOERR_NOMEM ){
6767 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
6768 }
6769 goto vdbe_error_halt;
6770 7072
6771 /* Jump to here if the sqlite3_interrupt() API sets the interrupt 7073 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
6772 ** flag. 7074 ** flag.
6773 */ 7075 */
6774 abort_due_to_interrupt: 7076 abort_due_to_interrupt:
6775 assert( db->u1.isInterrupted ); 7077 assert( db->u1.isInterrupted );
6776 rc = SQLITE_INTERRUPT; 7078 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
6777 p->rc = rc; 7079 p->rc = rc;
6778 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 7080 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
6779 goto vdbe_error_halt; 7081 goto abort_due_to_error;
6780 } 7082 }
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/vdbe.h ('k') | third_party/sqlite/src/src/vdbeInt.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698