4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** The code in this file implements the function that runs the
13 ** bytecode of a prepared statement.
15 ** Various scripts scan this source file in order to generate HTML
16 ** documentation, headers files, or other derived files. The formatting
17 ** of the code in this file is, therefore, important. See other comments
18 ** in this file for details. If in doubt, do not deviate from existing
19 ** commenting and indentation practices when changing or adding code.
21 #include "sqliteInt.h"
25 ** Invoke this macro on memory cells just prior to changing the
26 ** value of the cell. This macro verifies that shallow copies are
27 ** not misused. A shallow copy of a string or blob just copies a
28 ** pointer to the string or blob, not the content. If the original
29 ** is changed while the copy is still in use, the string or blob might
30 ** be changed out from under the copy. This macro verifies that nothing
31 ** like that ever happens.
34 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
36 # define memAboutToChange(P,M)
40 ** The following global variable is incremented every time a cursor
41 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
42 ** procedures use this information to make sure that indices are
43 ** working correctly. This variable has no function other than to
44 ** help verify the correct operation of the library.
47 int sqlite3_search_count
= 0;
51 ** When this global variable is positive, it gets decremented once before
52 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
53 ** field of the sqlite3 structure is set in order to simulate an interrupt.
55 ** This facility is used for testing purposes only. It does not function
56 ** in an ordinary build.
59 int sqlite3_interrupt_count
= 0;
63 ** The next global variable is incremented each type the OP_Sort opcode
64 ** is executed. The test procedures use this information to make sure that
65 ** sorting is occurring or not occurring at appropriate times. This variable
66 ** has no function other than to help verify the correct operation of the
70 int sqlite3_sort_count
= 0;
74 ** The next global variable records the size of the largest MEM_Blob
75 ** or MEM_Str that has been used by a VDBE opcode. The test procedures
76 ** use this information to make sure that the zero-blob functionality
77 ** is working correctly. This variable has no function other than to
78 ** help verify the correct operation of the library.
81 int sqlite3_max_blobsize
= 0;
82 static void updateMaxBlobsize(Mem
*p
){
83 if( (p
->flags
& (MEM_Str
|MEM_Blob
))!=0 && p
->n
>sqlite3_max_blobsize
){
84 sqlite3_max_blobsize
= p
->n
;
90 ** This macro evaluates to true if either the update hook or the preupdate
91 ** hook are enabled for database connect DB.
93 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
94 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
96 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
100 ** The next global variable is incremented each time the OP_Found opcode
101 ** is executed. This is used to test whether or not the foreign key
102 ** operation implemented using OP_FkIsZero is working. This variable
103 ** has no function other than to help verify the correct operation of the
107 int sqlite3_found_count
= 0;
111 ** Test a register to see if it exceeds the current maximum blob size.
112 ** If it does, record the new maximum blob size.
114 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
115 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
117 # define UPDATE_MAX_BLOBSIZE(P)
121 /* This routine provides a convenient place to set a breakpoint during
122 ** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
123 ** each opcode is printed. Variables "pc" (program counter) and pOp are
124 ** available to add conditionals to the breakpoint. GDB example:
126 ** break test_trace_breakpoint if pc=22
128 ** Other useful labels for breakpoints include:
129 ** test_addop_breakpoint(pc,pOp)
130 ** sqlite3CorruptError(lineno)
131 ** sqlite3MisuseError(lineno)
132 ** sqlite3CantopenError(lineno)
134 static void test_trace_breakpoint(int pc
, Op
*pOp
, Vdbe
*v
){
141 ** Invoke the VDBE coverage callback, if that callback is defined. This
142 ** feature is used for test suite validation only and does not appear an
143 ** production builds.
145 ** M is the type of branch. I is the direction taken for this instance of
148 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
149 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
150 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
152 ** In other words, if M is 2, then I is either 0 (for fall-through) or
153 ** 1 (for when the branch is taken). If M is 3, the I is 0 for an
154 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
155 ** if the result of comparison is NULL. For M=3, I=2 the jump may or
156 ** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
157 ** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
158 ** depending on if the operands are less than, equal, or greater than.
160 ** iSrcLine is the source code line (from the __LINE__ macro) that
161 ** generated the VDBE instruction combined with flag bits. The source
162 ** code line number is in the lower 24 bits of iSrcLine and the upper
163 ** 8 bytes are flags. The lower three bits of the flags indicate
164 ** values for I that should never occur. For example, if the branch is
165 ** always taken, the flags should be 0x05 since the fall-through and
166 ** alternate branch are never taken. If a branch is never taken then
167 ** flags should be 0x06 since only the fall-through approach is allowed.
169 ** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
170 ** interested in equal or not-equal. In other words, I==0 and I==2
171 ** should be treated as equivalent
173 ** Since only a line number is retained, not the filename, this macro
174 ** only works for amalgamation builds. But that is ok, since these macros
175 ** should be no-ops except for special builds used to measure test coverage.
177 #if !defined(SQLITE_VDBE_COVERAGE)
178 # define VdbeBranchTaken(I,M)
180 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
181 static void vdbeTakeBranch(u32 iSrcLine
, u8 I
, u8 M
){
183 assert( I
<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
184 assert( M
<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
185 assert( I
<M
); /* I can only be 2 if M is 3 or 4 */
186 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
188 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
189 ** the flags indicate directions that the branch can never go. If
190 ** a branch really does go in one of those directions, assert right
192 mNever
= iSrcLine
>> 24;
193 assert( (I
& mNever
)==0 );
194 if( sqlite3GlobalConfig
.xVdbeBranch
==0 ) return; /*NO_TEST*/
195 /* Invoke the branch coverage callback with three arguments:
196 ** iSrcLine - the line number of the VdbeCoverage() macro, with
198 ** I - Mask of bits 0x07 indicating which cases are are
199 ** fulfilled by this instance of the jump. 0x01 means
200 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
201 ** impossible cases (ex: if the comparison is never NULL)
202 ** are filled in automatically so that the coverage
203 ** measurement logic does not flag those impossible cases
204 ** as missed coverage.
205 ** M - Type of jump. Same as M argument above
208 if( M
==2 ) I
|= 0x04;
211 if( (mNever
&0x08)!=0 && (I
&0x05)!=0) I
|= 0x05; /*NO_TEST*/
213 sqlite3GlobalConfig
.xVdbeBranch(sqlite3GlobalConfig
.pVdbeBranchArg
,
214 iSrcLine
&0xffffff, I
, M
);
219 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
220 ** a pointer to a dynamically allocated string where some other entity
221 ** is responsible for deallocating that string. Because the register
222 ** does not control the string, it might be deleted without the register
225 ** This routine converts an ephemeral string into a dynamically allocated
226 ** string that the register itself controls. In other words, it
227 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
229 #define Deephemeralize(P) \
230 if( ((P)->flags&MEM_Ephem)!=0 \
231 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
233 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
234 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
237 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
238 ** if we run out of memory.
240 static VdbeCursor
*allocateCursor(
241 Vdbe
*p
, /* The virtual machine */
242 int iCur
, /* Index of the new VdbeCursor */
243 int nField
, /* Number of fields in the table or index */
244 int iDb
, /* Database the cursor belongs to, or -1 */
245 u8 eCurType
/* Type of the new cursor */
247 /* Find the memory cell that will be used to store the blob of memory
248 ** required for this VdbeCursor structure. It is convenient to use a
249 ** vdbe memory cell to manage the memory allocation required for a
250 ** VdbeCursor structure for the following reasons:
252 ** * Sometimes cursor numbers are used for a couple of different
253 ** purposes in a vdbe program. The different uses might require
254 ** different sized allocations. Memory cells provide growable
257 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
258 ** be freed lazily via the sqlite3_release_memory() API. This
259 ** minimizes the number of malloc calls made by the system.
261 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
262 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
263 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
265 Mem
*pMem
= iCur
>0 ? &p
->aMem
[p
->nMem
-iCur
] : p
->aMem
;
270 ROUND8(sizeof(VdbeCursor
)) + 2*sizeof(u32
)*nField
+
271 (eCurType
==CURTYPE_BTREE
?sqlite3BtreeCursorSize():0);
273 assert( iCur
>=0 && iCur
<p
->nCursor
);
274 if( p
->apCsr
[iCur
] ){ /*OPTIMIZATION-IF-FALSE*/
275 sqlite3VdbeFreeCursor(p
, p
->apCsr
[iCur
]);
279 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
280 ** the pMem used to hold space for the cursor has enough storage available
281 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
282 ** to hold cursors, it is faster to in-line the logic. */
283 assert( pMem
->flags
==MEM_Undefined
);
284 assert( (pMem
->flags
& MEM_Dyn
)==0 );
285 assert( pMem
->szMalloc
==0 || pMem
->z
==pMem
->zMalloc
);
286 if( pMem
->szMalloc
<nByte
){
287 if( pMem
->szMalloc
>0 ){
288 sqlite3DbFreeNN(pMem
->db
, pMem
->zMalloc
);
290 pMem
->z
= pMem
->zMalloc
= sqlite3DbMallocRaw(pMem
->db
, nByte
);
291 if( pMem
->zMalloc
==0 ){
295 pMem
->szMalloc
= nByte
;
298 p
->apCsr
[iCur
] = pCx
= (VdbeCursor
*)pMem
->zMalloc
;
299 memset(pCx
, 0, offsetof(VdbeCursor
,pAltCursor
));
300 pCx
->eCurType
= eCurType
;
302 pCx
->nField
= nField
;
303 pCx
->aOffset
= &pCx
->aType
[nField
];
304 if( eCurType
==CURTYPE_BTREE
){
305 pCx
->uc
.pCursor
= (BtCursor
*)
306 &pMem
->z
[ROUND8(sizeof(VdbeCursor
))+2*sizeof(u32
)*nField
];
307 sqlite3BtreeCursorZero(pCx
->uc
.pCursor
);
313 ** The string in pRec is known to look like an integer and to have a
314 ** floating point value of rValue. Return true and set *piValue to the
315 ** integer value if the string is in range to be an integer. Otherwise,
318 static int alsoAnInt(Mem
*pRec
, double rValue
, i64
*piValue
){
319 i64 iValue
= (double)rValue
;
320 if( sqlite3RealSameAsInt(rValue
,iValue
) ){
324 return 0==sqlite3Atoi64(pRec
->z
, piValue
, pRec
->n
, pRec
->enc
);
328 ** Try to convert a value into a numeric representation if we can
329 ** do so without loss of information. In other words, if the string
330 ** looks like a number, convert it into a number. If it does not
331 ** look like a number, leave it alone.
333 ** If the bTryForInt flag is true, then extra effort is made to give
334 ** an integer representation. Strings that look like floating point
335 ** values but which have no fractional component (example: '48.00')
336 ** will have a MEM_Int representation when bTryForInt is true.
338 ** If bTryForInt is false, then if the input string contains a decimal
339 ** point or exponential notation, the result is only MEM_Real, even
340 ** if there is an exact integer representation of the quantity.
342 static void applyNumericAffinity(Mem
*pRec
, int bTryForInt
){
346 assert( (pRec
->flags
& (MEM_Str
|MEM_Int
|MEM_Real
|MEM_IntReal
))==MEM_Str
);
347 rc
= sqlite3AtoF(pRec
->z
, &rValue
, pRec
->n
, enc
);
349 if( rc
==1 && alsoAnInt(pRec
, rValue
, &pRec
->u
.i
) ){
350 pRec
->flags
|= MEM_Int
;
353 pRec
->flags
|= MEM_Real
;
354 if( bTryForInt
) sqlite3VdbeIntegerAffinity(pRec
);
356 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
357 ** string representation after computing a numeric equivalent, because the
358 ** string representation might not be the canonical representation for the
359 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
360 pRec
->flags
&= ~MEM_Str
;
364 ** Processing is determine by the affinity parameter:
366 ** SQLITE_AFF_INTEGER:
368 ** SQLITE_AFF_NUMERIC:
369 ** Try to convert pRec to an integer representation or a
370 ** floating-point representation if an integer representation
371 ** is not possible. Note that the integer representation is
372 ** always preferred, even if the affinity is REAL, because
373 ** an integer representation is more space efficient on disk.
376 ** Convert pRec to a text representation.
380 ** No-op. pRec is unchanged.
382 static void applyAffinity(
383 Mem
*pRec
, /* The value to apply affinity to */
384 char affinity
, /* The affinity to be applied */
385 u8 enc
/* Use this text encoding */
387 if( affinity
>=SQLITE_AFF_NUMERIC
){
388 assert( affinity
==SQLITE_AFF_INTEGER
|| affinity
==SQLITE_AFF_REAL
389 || affinity
==SQLITE_AFF_NUMERIC
);
390 if( (pRec
->flags
& MEM_Int
)==0 ){ /*OPTIMIZATION-IF-FALSE*/
391 if( (pRec
->flags
& MEM_Real
)==0 ){
392 if( pRec
->flags
& MEM_Str
) applyNumericAffinity(pRec
,1);
394 sqlite3VdbeIntegerAffinity(pRec
);
397 }else if( affinity
==SQLITE_AFF_TEXT
){
398 /* Only attempt the conversion to TEXT if there is an integer or real
399 ** representation (blob and NULL do not get converted) but no string
400 ** representation. It would be harmless to repeat the conversion if
401 ** there is already a string rep, but it is pointless to waste those
403 if( 0==(pRec
->flags
&MEM_Str
) ){ /*OPTIMIZATION-IF-FALSE*/
404 if( (pRec
->flags
&(MEM_Real
|MEM_Int
|MEM_IntReal
)) ){
405 testcase( pRec
->flags
& MEM_Int
);
406 testcase( pRec
->flags
& MEM_Real
);
407 testcase( pRec
->flags
& MEM_IntReal
);
408 sqlite3VdbeMemStringify(pRec
, enc
, 1);
411 pRec
->flags
&= ~(MEM_Real
|MEM_Int
|MEM_IntReal
);
416 ** Try to convert the type of a function argument or a result column
417 ** into a numeric representation. Use either INTEGER or REAL whichever
418 ** is appropriate. But only do the conversion if it is possible without
419 ** loss of information and return the revised type of the argument.
421 int sqlite3_value_numeric_type(sqlite3_value
*pVal
){
422 int eType
= sqlite3_value_type(pVal
);
423 if( eType
==SQLITE_TEXT
){
424 Mem
*pMem
= (Mem
*)pVal
;
425 applyNumericAffinity(pMem
, 0);
426 eType
= sqlite3_value_type(pVal
);
432 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
433 ** not the internal Mem* type.
435 void sqlite3ValueApplyAffinity(
440 applyAffinity((Mem
*)pVal
, affinity
, enc
);
444 ** pMem currently only holds a string type (or maybe a BLOB that we can
445 ** interpret as a string if we want to). Compute its corresponding
446 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
449 static u16 SQLITE_NOINLINE
computeNumericType(Mem
*pMem
){
452 assert( (pMem
->flags
& (MEM_Int
|MEM_Real
|MEM_IntReal
))==0 );
453 assert( (pMem
->flags
& (MEM_Str
|MEM_Blob
))!=0 );
454 if( ExpandBlob(pMem
) ){
458 rc
= sqlite3AtoF(pMem
->z
, &pMem
->u
.r
, pMem
->n
, pMem
->enc
);
460 if( rc
==0 && sqlite3Atoi64(pMem
->z
, &ix
, pMem
->n
, pMem
->enc
)<=1 ){
466 }else if( rc
==1 && sqlite3Atoi64(pMem
->z
, &ix
, pMem
->n
, pMem
->enc
)==0 ){
474 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
477 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
478 ** But it does set pMem->u.r and pMem->u.i appropriately.
480 static u16
numericType(Mem
*pMem
){
481 if( pMem
->flags
& (MEM_Int
|MEM_Real
|MEM_IntReal
) ){
482 testcase( pMem
->flags
& MEM_Int
);
483 testcase( pMem
->flags
& MEM_Real
);
484 testcase( pMem
->flags
& MEM_IntReal
);
485 return pMem
->flags
& (MEM_Int
|MEM_Real
|MEM_IntReal
);
487 if( pMem
->flags
& (MEM_Str
|MEM_Blob
) ){
488 testcase( pMem
->flags
& MEM_Str
);
489 testcase( pMem
->flags
& MEM_Blob
);
490 return computeNumericType(pMem
);
497 ** Write a nice string representation of the contents of cell pMem
498 ** into buffer zBuf, length nBuf.
500 void sqlite3VdbeMemPrettyPrint(Mem
*pMem
, StrAccum
*pStr
){
502 static const char *const encnames
[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
508 assert( (f
& (MEM_Static
|MEM_Ephem
))==0 );
509 }else if( f
& MEM_Static
){
511 assert( (f
& (MEM_Dyn
|MEM_Ephem
))==0 );
512 }else if( f
& MEM_Ephem
){
514 assert( (f
& (MEM_Static
|MEM_Dyn
))==0 );
518 sqlite3_str_appendf(pStr
, "%cx[", c
);
519 for(i
=0; i
<25 && i
<pMem
->n
; i
++){
520 sqlite3_str_appendf(pStr
, "%02X", ((int)pMem
->z
[i
] & 0xFF));
522 sqlite3_str_appendf(pStr
, "|");
523 for(i
=0; i
<25 && i
<pMem
->n
; i
++){
525 sqlite3_str_appendchar(pStr
, 1, (z
<32||z
>126)?'.':z
);
527 sqlite3_str_appendf(pStr
,"]");
529 sqlite3_str_appendf(pStr
, "+%dz",pMem
->u
.nZero
);
531 }else if( f
& MEM_Str
){
536 assert( (f
& (MEM_Static
|MEM_Ephem
))==0 );
537 }else if( f
& MEM_Static
){
539 assert( (f
& (MEM_Dyn
|MEM_Ephem
))==0 );
540 }else if( f
& MEM_Ephem
){
542 assert( (f
& (MEM_Static
|MEM_Dyn
))==0 );
546 sqlite3_str_appendf(pStr
, " %c%d[", c
, pMem
->n
);
547 for(j
=0; j
<25 && j
<pMem
->n
; j
++){
549 sqlite3_str_appendchar(pStr
, 1, (c
>=0x20&&c
<=0x7f) ? c
: '.');
551 sqlite3_str_appendf(pStr
, "]%s", encnames
[pMem
->enc
]);
558 ** Print the value of a register for tracing purposes:
560 static void memTracePrint(Mem
*p
){
561 if( p
->flags
& MEM_Undefined
){
562 printf(" undefined");
563 }else if( p
->flags
& MEM_Null
){
564 printf(p
->flags
& MEM_Zero
? " NULL-nochng" : " NULL");
565 }else if( (p
->flags
& (MEM_Int
|MEM_Str
))==(MEM_Int
|MEM_Str
) ){
566 printf(" si:%lld", p
->u
.i
);
567 }else if( (p
->flags
& (MEM_IntReal
))!=0 ){
568 printf(" ir:%lld", p
->u
.i
);
569 }else if( p
->flags
& MEM_Int
){
570 printf(" i:%lld", p
->u
.i
);
571 #ifndef SQLITE_OMIT_FLOATING_POINT
572 }else if( p
->flags
& MEM_Real
){
573 printf(" r:%.17g", p
->u
.r
);
575 }else if( sqlite3VdbeMemIsRowSet(p
) ){
580 sqlite3StrAccumInit(&acc
, 0, zBuf
, sizeof(zBuf
), 0);
581 sqlite3VdbeMemPrettyPrint(p
, &acc
);
582 printf(" %s", sqlite3StrAccumFinish(&acc
));
584 if( p
->flags
& MEM_Subtype
) printf(" subtype=0x%02x", p
->eSubtype
);
586 static void registerTrace(int iReg
, Mem
*p
){
587 printf("R[%d] = ", iReg
);
590 printf(" <== R[%d]", (int)(p
->pScopyFrom
- &p
[-iReg
]));
593 sqlite3VdbeCheckMemInvariants(p
);
595 /**/ void sqlite3PrintMem(Mem
*pMem
){
604 ** Show the values of all registers in the virtual machine. Used for
605 ** interactive debugging.
607 void sqlite3VdbeRegisterDump(Vdbe
*v
){
609 for(i
=1; i
<v
->nMem
; i
++) registerTrace(i
, v
->aMem
+i
);
611 #endif /* SQLITE_DEBUG */
615 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
617 # define REGISTER_TRACE(R,M)
624 ** hwtime.h contains inline assembler code for implementing
625 ** high-performance timing routines.
633 ** This function is only called from within an assert() expression. It
634 ** checks that the sqlite3.nTransaction variable is correctly set to
635 ** the number of non-transaction savepoints currently in the
636 ** linked list starting at sqlite3.pSavepoint.
640 ** assert( checkSavepointCount(db) );
642 static int checkSavepointCount(sqlite3
*db
){
645 for(p
=db
->pSavepoint
; p
; p
=p
->pNext
) n
++;
646 assert( n
==(db
->nSavepoint
+ db
->isTransactionSavepoint
) );
652 ** Return the register of pOp->p2 after first preparing it to be
653 ** overwritten with an integer value.
655 static SQLITE_NOINLINE Mem
*out2PrereleaseWithClear(Mem
*pOut
){
656 sqlite3VdbeMemSetNull(pOut
);
657 pOut
->flags
= MEM_Int
;
660 static Mem
*out2Prerelease(Vdbe
*p
, VdbeOp
*pOp
){
663 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
664 pOut
= &p
->aMem
[pOp
->p2
];
665 memAboutToChange(p
, pOut
);
666 if( VdbeMemDynamic(pOut
) ){ /*OPTIMIZATION-IF-FALSE*/
667 return out2PrereleaseWithClear(pOut
);
669 pOut
->flags
= MEM_Int
;
676 ** Execute as much of a VDBE program as we can.
677 ** This is the core of sqlite3_step().
680 Vdbe
*p
/* The VDBE */
682 Op
*aOp
= p
->aOp
; /* Copy of p->aOp */
683 Op
*pOp
= aOp
; /* Current operation */
684 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
685 Op
*pOrigOp
; /* Value of pOp at the top of the loop */
688 int nExtraDelete
= 0; /* Verifies FORDELETE and AUXDELETE flags */
690 int rc
= SQLITE_OK
; /* Value to return */
691 sqlite3
*db
= p
->db
; /* The database */
692 u8 resetSchemaOnFault
= 0; /* Reset schema after an error if positive */
693 u8 encoding
= ENC(db
); /* The database encoding */
694 int iCompare
= 0; /* Result of last comparison */
695 u64 nVmStep
= 0; /* Number of virtual machine steps */
696 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
697 u64 nProgressLimit
; /* Invoke xProgress() when nVmStep reaches this */
699 Mem
*aMem
= p
->aMem
; /* Copy of p->aMem */
700 Mem
*pIn1
= 0; /* 1st input operand */
701 Mem
*pIn2
= 0; /* 2nd input operand */
702 Mem
*pIn3
= 0; /* 3rd input operand */
703 Mem
*pOut
= 0; /* Output operand */
705 u64 start
; /* CPU clock count at start of opcode */
707 /*** INSERT STACK UNION HERE ***/
709 assert( p
->iVdbeMagic
==VDBE_MAGIC_RUN
); /* sqlite3_step() verifies this */
711 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
713 u32 iPrior
= p
->aCounter
[SQLITE_STMTSTATUS_VM_STEP
];
714 assert( 0 < db
->nProgressOps
);
715 nProgressLimit
= db
->nProgressOps
- (iPrior
% db
->nProgressOps
);
717 nProgressLimit
= LARGEST_UINT64
;
720 if( p
->rc
==SQLITE_NOMEM
){
721 /* This happens if a malloc() inside a call to sqlite3_column_text() or
722 ** sqlite3_column_text16() failed. */
725 assert( p
->rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_BUSY
);
726 testcase( p
->rc
!=SQLITE_OK
);
728 assert( p
->bIsReader
|| p
->readOnly
!=0 );
730 assert( p
->explain
==0 );
732 db
->busyHandler
.nBusy
= 0;
733 if( AtomicLoad(&db
->u1
.isInterrupted
) ) goto abort_due_to_interrupt
;
734 sqlite3VdbeIOTraceSql(p
);
736 sqlite3BeginBenignMalloc();
738 && (p
->db
->flags
& (SQLITE_VdbeListing
|SQLITE_VdbeEQP
|SQLITE_VdbeTrace
))!=0
742 sqlite3VdbePrintSql(p
);
743 if( p
->db
->flags
& SQLITE_VdbeListing
){
744 printf("VDBE Program Listing:\n");
745 for(i
=0; i
<p
->nOp
; i
++){
746 sqlite3VdbePrintOp(stdout
, i
, &aOp
[i
]);
749 if( p
->db
->flags
& SQLITE_VdbeEQP
){
750 for(i
=0; i
<p
->nOp
; i
++){
751 if( aOp
[i
].opcode
==OP_Explain
){
752 if( once
) printf("VDBE Query Plan:\n");
753 printf("%s\n", aOp
[i
].p4
.z
);
758 if( p
->db
->flags
& SQLITE_VdbeTrace
) printf("VDBE Trace:\n");
760 sqlite3EndBenignMalloc();
762 for(pOp
=&aOp
[p
->pc
]; 1; pOp
++){
763 /* Errors are detected by individual opcodes, with an immediate
764 ** jumps to abort_due_to_error. */
765 assert( rc
==SQLITE_OK
);
767 assert( pOp
>=aOp
&& pOp
<&aOp
[p
->nOp
]);
769 start
= sqlite3NProfileCnt
? sqlite3NProfileCnt
: sqlite3Hwtime();
772 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
773 if( p
->anExec
) p
->anExec
[(int)(pOp
-aOp
)]++;
776 /* Only allow tracing if SQLITE_DEBUG is defined.
779 if( db
->flags
& SQLITE_VdbeTrace
){
780 sqlite3VdbePrintOp(stdout
, (int)(pOp
- aOp
), pOp
);
781 test_trace_breakpoint((int)(pOp
- aOp
),pOp
,p
);
786 /* Check to see if we need to simulate an interrupt. This only happens
787 ** if we have a special test build.
790 if( sqlite3_interrupt_count
>0 ){
791 sqlite3_interrupt_count
--;
792 if( sqlite3_interrupt_count
==0 ){
793 sqlite3_interrupt(db
);
798 /* Sanity checking on other operands */
801 u8 opProperty
= sqlite3OpcodeProperty
[pOp
->opcode
];
802 if( (opProperty
& OPFLG_IN1
)!=0 ){
804 assert( pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
805 assert( memIsValid(&aMem
[pOp
->p1
]) );
806 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p1
]) );
807 REGISTER_TRACE(pOp
->p1
, &aMem
[pOp
->p1
]);
809 if( (opProperty
& OPFLG_IN2
)!=0 ){
811 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
812 assert( memIsValid(&aMem
[pOp
->p2
]) );
813 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p2
]) );
814 REGISTER_TRACE(pOp
->p2
, &aMem
[pOp
->p2
]);
816 if( (opProperty
& OPFLG_IN3
)!=0 ){
818 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
819 assert( memIsValid(&aMem
[pOp
->p3
]) );
820 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p3
]) );
821 REGISTER_TRACE(pOp
->p3
, &aMem
[pOp
->p3
]);
823 if( (opProperty
& OPFLG_OUT2
)!=0 ){
825 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
826 memAboutToChange(p
, &aMem
[pOp
->p2
]);
828 if( (opProperty
& OPFLG_OUT3
)!=0 ){
830 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
831 memAboutToChange(p
, &aMem
[pOp
->p3
]);
835 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
839 switch( pOp
->opcode
){
841 /*****************************************************************************
842 ** What follows is a massive switch statement where each case implements a
843 ** separate instruction in the virtual machine. If we follow the usual
844 ** indentation conventions, each case should be indented by 6 spaces. But
845 ** that is a lot of wasted space on the left margin. So the code within
846 ** the switch statement will break with convention and be flush-left. Another
847 ** big comment (similar to this one) will mark the point in the code where
848 ** we transition back to normal indentation.
850 ** The formatting of each case is important. The makefile for SQLite
851 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
852 ** file looking for lines that begin with "case OP_". The opcodes.h files
853 ** will be filled with #defines that give unique integer values to each
854 ** opcode and the opcodes.c file is filled with an array of strings where
855 ** each string is the symbolic name for the corresponding opcode. If the
856 ** case statement is followed by a comment of the form "/# same as ... #/"
857 ** that comment is used to determine the particular value of the opcode.
859 ** Other keywords in the comment that follows each case are used to
860 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
861 ** Keywords include: in1, in2, in3, out2, out3. See
862 ** the mkopcodeh.awk script for additional information.
864 ** Documentation about VDBE opcodes is generated by scanning this file
865 ** for lines of that contain "Opcode:". That line and all subsequent
866 ** comment lines are used in the generation of the opcode.html documentation
871 ** Formatting is important to scripts that scan this file.
872 ** Do not deviate from the formatting style currently in use.
874 *****************************************************************************/
876 /* Opcode: Goto * P2 * * *
878 ** An unconditional jump to address P2.
879 ** The next instruction executed will be
880 ** the one at index P2 from the beginning of
883 ** The P1 parameter is not actually used by this opcode. However, it
884 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
885 ** that this Goto is the bottom of a loop and that the lines from P2 down
886 ** to the current line should be indented for EXPLAIN output.
888 case OP_Goto
: { /* jump */
891 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
892 ** means we should really jump back to the preceeding OP_ReleaseReg
895 assert( pOp
->p2
< (int)(pOp
- aOp
) );
896 assert( pOp
->p2
> 1 );
897 pOp
= &aOp
[pOp
->p2
- 2];
898 assert( pOp
[1].opcode
==OP_ReleaseReg
);
899 goto check_for_interrupt
;
903 jump_to_p2_and_check_for_interrupt
:
904 pOp
= &aOp
[pOp
->p2
- 1];
906 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
907 ** OP_VNext, or OP_SorterNext) all jump here upon
908 ** completion. Check to see if sqlite3_interrupt() has been called
909 ** or if the progress callback needs to be invoked.
911 ** This code uses unstructured "goto" statements and does not look clean.
912 ** But that is not due to sloppy coding habits. The code is written this
913 ** way for performance, to avoid having to run the interrupt and progress
914 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
915 ** faster according to "valgrind --tool=cachegrind" */
917 if( AtomicLoad(&db
->u1
.isInterrupted
) ) goto abort_due_to_interrupt
;
918 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
919 /* Call the progress callback if it is configured and the required number
920 ** of VDBE ops have been executed (either since this invocation of
921 ** sqlite3VdbeExec() or since last time the progress callback was called).
922 ** If the progress callback returns non-zero, exit the virtual machine with
923 ** a return code SQLITE_ABORT.
925 while( nVmStep
>=nProgressLimit
&& db
->xProgress
!=0 ){
926 assert( db
->nProgressOps
!=0 );
927 nProgressLimit
+= db
->nProgressOps
;
928 if( db
->xProgress(db
->pProgressArg
) ){
929 nProgressLimit
= LARGEST_UINT64
;
930 rc
= SQLITE_INTERRUPT
;
931 goto abort_due_to_error
;
939 /* Opcode: Gosub P1 P2 * * *
941 ** Write the current address onto register P1
942 ** and then jump to address P2.
944 case OP_Gosub
: { /* jump */
945 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
946 pIn1
= &aMem
[pOp
->p1
];
947 assert( VdbeMemDynamic(pIn1
)==0 );
948 memAboutToChange(p
, pIn1
);
949 pIn1
->flags
= MEM_Int
;
950 pIn1
->u
.i
= (int)(pOp
-aOp
);
951 REGISTER_TRACE(pOp
->p1
, pIn1
);
953 /* Most jump operations do a goto to this spot in order to update
954 ** the pOp pointer. */
956 pOp
= &aOp
[pOp
->p2
- 1];
960 /* Opcode: Return P1 * * * *
962 ** Jump to the next instruction after the address in register P1. After
963 ** the jump, register P1 becomes undefined.
965 case OP_Return
: { /* in1 */
966 pIn1
= &aMem
[pOp
->p1
];
967 assert( pIn1
->flags
==MEM_Int
);
968 pOp
= &aOp
[pIn1
->u
.i
];
969 pIn1
->flags
= MEM_Undefined
;
973 /* Opcode: InitCoroutine P1 P2 P3 * *
975 ** Set up register P1 so that it will Yield to the coroutine
976 ** located at address P3.
978 ** If P2!=0 then the coroutine implementation immediately follows
979 ** this opcode. So jump over the coroutine implementation to
982 ** See also: EndCoroutine
984 case OP_InitCoroutine
: { /* jump */
985 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
986 assert( pOp
->p2
>=0 && pOp
->p2
<p
->nOp
);
987 assert( pOp
->p3
>=0 && pOp
->p3
<p
->nOp
);
988 pOut
= &aMem
[pOp
->p1
];
989 assert( !VdbeMemDynamic(pOut
) );
990 pOut
->u
.i
= pOp
->p3
- 1;
991 pOut
->flags
= MEM_Int
;
992 if( pOp
->p2
) goto jump_to_p2
;
996 /* Opcode: EndCoroutine P1 * * * *
998 ** The instruction at the address in register P1 is a Yield.
999 ** Jump to the P2 parameter of that Yield.
1000 ** After the jump, register P1 becomes undefined.
1002 ** See also: InitCoroutine
1004 case OP_EndCoroutine
: { /* in1 */
1006 pIn1
= &aMem
[pOp
->p1
];
1007 assert( pIn1
->flags
==MEM_Int
);
1008 assert( pIn1
->u
.i
>=0 && pIn1
->u
.i
<p
->nOp
);
1009 pCaller
= &aOp
[pIn1
->u
.i
];
1010 assert( pCaller
->opcode
==OP_Yield
);
1011 assert( pCaller
->p2
>=0 && pCaller
->p2
<p
->nOp
);
1012 pOp
= &aOp
[pCaller
->p2
- 1];
1013 pIn1
->flags
= MEM_Undefined
;
1017 /* Opcode: Yield P1 P2 * * *
1019 ** Swap the program counter with the value in register P1. This
1020 ** has the effect of yielding to a coroutine.
1022 ** If the coroutine that is launched by this instruction ends with
1023 ** Yield or Return then continue to the next instruction. But if
1024 ** the coroutine launched by this instruction ends with
1025 ** EndCoroutine, then jump to P2 rather than continuing with the
1026 ** next instruction.
1028 ** See also: InitCoroutine
1030 case OP_Yield
: { /* in1, jump */
1032 pIn1
= &aMem
[pOp
->p1
];
1033 assert( VdbeMemDynamic(pIn1
)==0 );
1034 pIn1
->flags
= MEM_Int
;
1035 pcDest
= (int)pIn1
->u
.i
;
1036 pIn1
->u
.i
= (int)(pOp
- aOp
);
1037 REGISTER_TRACE(pOp
->p1
, pIn1
);
1042 /* Opcode: HaltIfNull P1 P2 P3 P4 P5
1043 ** Synopsis: if r[P3]=null halt
1045 ** Check the value in register P3. If it is NULL then Halt using
1046 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1047 ** value in register P3 is not NULL, then this routine is a no-op.
1048 ** The P5 parameter should be 1.
1050 case OP_HaltIfNull
: { /* in3 */
1051 pIn3
= &aMem
[pOp
->p3
];
1053 if( pOp
->p2
==OE_Abort
){ sqlite3VdbeAssertAbortable(p
); }
1055 if( (pIn3
->flags
& MEM_Null
)==0 ) break;
1056 /* Fall through into OP_Halt */
1057 /* no break */ deliberate_fall_through
1060 /* Opcode: Halt P1 P2 * P4 P5
1062 ** Exit immediately. All open cursors, etc are closed
1065 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1066 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1067 ** For errors, it can be some other value. If P1!=0 then P2 will determine
1068 ** whether or not to rollback the current transaction. Do not rollback
1069 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1070 ** then back out all changes that have occurred during this execution of the
1071 ** VDBE, but do not rollback the transaction.
1073 ** If P4 is not null then it is an error message string.
1075 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1078 ** 1: NOT NULL contraint failed: P4
1079 ** 2: UNIQUE constraint failed: P4
1080 ** 3: CHECK constraint failed: P4
1081 ** 4: FOREIGN KEY constraint failed: P4
1083 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
1086 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
1087 ** every program. So a jump past the last instruction of the program
1088 ** is the same as executing Halt.
1094 pcx
= (int)(pOp
- aOp
);
1096 if( pOp
->p2
==OE_Abort
){ sqlite3VdbeAssertAbortable(p
); }
1098 if( pOp
->p1
==SQLITE_OK
&& p
->pFrame
){
1099 /* Halt the sub-program. Return control to the parent frame. */
1101 p
->pFrame
= pFrame
->pParent
;
1103 sqlite3VdbeSetChanges(db
, p
->nChange
);
1104 pcx
= sqlite3VdbeFrameRestore(pFrame
);
1105 if( pOp
->p2
==OE_Ignore
){
1106 /* Instruction pcx is the OP_Program that invoked the sub-program
1107 ** currently being halted. If the p2 instruction of this OP_Halt
1108 ** instruction is set to OE_Ignore, then the sub-program is throwing
1109 ** an IGNORE exception. In this case jump to the address specified
1110 ** as the p2 of the calling OP_Program. */
1111 pcx
= p
->aOp
[pcx
].p2
-1;
1119 p
->errorAction
= (u8
)pOp
->p2
;
1121 assert( pOp
->p5
<=4 );
1124 static const char * const azType
[] = { "NOT NULL", "UNIQUE", "CHECK",
1126 testcase( pOp
->p5
==1 );
1127 testcase( pOp
->p5
==2 );
1128 testcase( pOp
->p5
==3 );
1129 testcase( pOp
->p5
==4 );
1130 sqlite3VdbeError(p
, "%s constraint failed", azType
[pOp
->p5
-1]);
1132 p
->zErrMsg
= sqlite3MPrintf(db
, "%z: %s", p
->zErrMsg
, pOp
->p4
.z
);
1135 sqlite3VdbeError(p
, "%s", pOp
->p4
.z
);
1137 sqlite3_log(pOp
->p1
, "abort at %d in [%s]: %s", pcx
, p
->zSql
, p
->zErrMsg
);
1139 rc
= sqlite3VdbeHalt(p
);
1140 assert( rc
==SQLITE_BUSY
|| rc
==SQLITE_OK
|| rc
==SQLITE_ERROR
);
1141 if( rc
==SQLITE_BUSY
){
1142 p
->rc
= SQLITE_BUSY
;
1144 assert( rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_CONSTRAINT
);
1145 assert( rc
==SQLITE_OK
|| db
->nDeferredCons
>0 || db
->nDeferredImmCons
>0 );
1146 rc
= p
->rc
? SQLITE_ERROR
: SQLITE_DONE
;
1151 /* Opcode: Integer P1 P2 * * *
1152 ** Synopsis: r[P2]=P1
1154 ** The 32-bit integer value P1 is written into register P2.
1156 case OP_Integer
: { /* out2 */
1157 pOut
= out2Prerelease(p
, pOp
);
1158 pOut
->u
.i
= pOp
->p1
;
1162 /* Opcode: Int64 * P2 * P4 *
1163 ** Synopsis: r[P2]=P4
1165 ** P4 is a pointer to a 64-bit integer value.
1166 ** Write that value into register P2.
1168 case OP_Int64
: { /* out2 */
1169 pOut
= out2Prerelease(p
, pOp
);
1170 assert( pOp
->p4
.pI64
!=0 );
1171 pOut
->u
.i
= *pOp
->p4
.pI64
;
1175 #ifndef SQLITE_OMIT_FLOATING_POINT
1176 /* Opcode: Real * P2 * P4 *
1177 ** Synopsis: r[P2]=P4
1179 ** P4 is a pointer to a 64-bit floating point value.
1180 ** Write that value into register P2.
1182 case OP_Real
: { /* same as TK_FLOAT, out2 */
1183 pOut
= out2Prerelease(p
, pOp
);
1184 pOut
->flags
= MEM_Real
;
1185 assert( !sqlite3IsNaN(*pOp
->p4
.pReal
) );
1186 pOut
->u
.r
= *pOp
->p4
.pReal
;
1191 /* Opcode: String8 * P2 * P4 *
1192 ** Synopsis: r[P2]='P4'
1194 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
1195 ** into a String opcode before it is executed for the first time. During
1196 ** this transformation, the length of string P4 is computed and stored
1197 ** as the P1 parameter.
1199 case OP_String8
: { /* same as TK_STRING, out2 */
1200 assert( pOp
->p4
.z
!=0 );
1201 pOut
= out2Prerelease(p
, pOp
);
1202 pOp
->p1
= sqlite3Strlen30(pOp
->p4
.z
);
1204 #ifndef SQLITE_OMIT_UTF16
1205 if( encoding
!=SQLITE_UTF8
){
1206 rc
= sqlite3VdbeMemSetStr(pOut
, pOp
->p4
.z
, -1, SQLITE_UTF8
, SQLITE_STATIC
);
1207 assert( rc
==SQLITE_OK
|| rc
==SQLITE_TOOBIG
);
1208 if( rc
) goto too_big
;
1209 if( SQLITE_OK
!=sqlite3VdbeChangeEncoding(pOut
, encoding
) ) goto no_mem
;
1210 assert( pOut
->szMalloc
>0 && pOut
->zMalloc
==pOut
->z
);
1211 assert( VdbeMemDynamic(pOut
)==0 );
1213 pOut
->flags
|= MEM_Static
;
1214 if( pOp
->p4type
==P4_DYNAMIC
){
1215 sqlite3DbFree(db
, pOp
->p4
.z
);
1217 pOp
->p4type
= P4_DYNAMIC
;
1218 pOp
->p4
.z
= pOut
->z
;
1222 if( pOp
->p1
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1225 pOp
->opcode
= OP_String
;
1226 assert( rc
==SQLITE_OK
);
1227 /* Fall through to the next case, OP_String */
1228 /* no break */ deliberate_fall_through
1231 /* Opcode: String P1 P2 P3 P4 P5
1232 ** Synopsis: r[P2]='P4' (len=P1)
1234 ** The string value P4 of length P1 (bytes) is stored in register P2.
1236 ** If P3 is not zero and the content of register P3 is equal to P5, then
1237 ** the datatype of the register P2 is converted to BLOB. The content is
1238 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
1239 ** of a string, as if it had been CAST. In other words:
1241 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
1243 case OP_String
: { /* out2 */
1244 assert( pOp
->p4
.z
!=0 );
1245 pOut
= out2Prerelease(p
, pOp
);
1246 pOut
->flags
= MEM_Str
|MEM_Static
|MEM_Term
;
1247 pOut
->z
= pOp
->p4
.z
;
1249 pOut
->enc
= encoding
;
1250 UPDATE_MAX_BLOBSIZE(pOut
);
1251 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
1253 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
1254 pIn3
= &aMem
[pOp
->p3
];
1255 assert( pIn3
->flags
& MEM_Int
);
1256 if( pIn3
->u
.i
==pOp
->p5
) pOut
->flags
= MEM_Blob
|MEM_Static
|MEM_Term
;
1262 /* Opcode: Null P1 P2 P3 * *
1263 ** Synopsis: r[P2..P3]=NULL
1265 ** Write a NULL into registers P2. If P3 greater than P2, then also write
1266 ** NULL into register P3 and every register in between P2 and P3. If P3
1267 ** is less than P2 (typically P3 is zero) then only register P2 is
1270 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1271 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1274 case OP_Null
: { /* out2 */
1277 pOut
= out2Prerelease(p
, pOp
);
1278 cnt
= pOp
->p3
-pOp
->p2
;
1279 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
1280 pOut
->flags
= nullFlag
= pOp
->p1
? (MEM_Null
|MEM_Cleared
) : MEM_Null
;
1287 memAboutToChange(p
, pOut
);
1288 sqlite3VdbeMemSetNull(pOut
);
1289 pOut
->flags
= nullFlag
;
1296 /* Opcode: SoftNull P1 * * * *
1297 ** Synopsis: r[P1]=NULL
1299 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1300 ** instruction, but do not free any string or blob memory associated with
1301 ** the register, so that if the value was a string or blob that was
1302 ** previously copied using OP_SCopy, the copies will continue to be valid.
1305 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
1306 pOut
= &aMem
[pOp
->p1
];
1307 pOut
->flags
= (pOut
->flags
&~(MEM_Undefined
|MEM_AffMask
))|MEM_Null
;
1311 /* Opcode: Blob P1 P2 * P4 *
1312 ** Synopsis: r[P2]=P4 (len=P1)
1314 ** P4 points to a blob of data P1 bytes long. Store this
1315 ** blob in register P2.
1317 case OP_Blob
: { /* out2 */
1318 assert( pOp
->p1
<= SQLITE_MAX_LENGTH
);
1319 pOut
= out2Prerelease(p
, pOp
);
1320 sqlite3VdbeMemSetStr(pOut
, pOp
->p4
.z
, pOp
->p1
, 0, 0);
1321 pOut
->enc
= encoding
;
1322 UPDATE_MAX_BLOBSIZE(pOut
);
1326 /* Opcode: Variable P1 P2 * P4 *
1327 ** Synopsis: r[P2]=parameter(P1,P4)
1329 ** Transfer the values of bound parameter P1 into register P2
1331 ** If the parameter is named, then its name appears in P4.
1332 ** The P4 value is used by sqlite3_bind_parameter_name().
1334 case OP_Variable
: { /* out2 */
1335 Mem
*pVar
; /* Value being transferred */
1337 assert( pOp
->p1
>0 && pOp
->p1
<=p
->nVar
);
1338 assert( pOp
->p4
.z
==0 || pOp
->p4
.z
==sqlite3VListNumToName(p
->pVList
,pOp
->p1
) );
1339 pVar
= &p
->aVar
[pOp
->p1
- 1];
1340 if( sqlite3VdbeMemTooBig(pVar
) ){
1343 pOut
= &aMem
[pOp
->p2
];
1344 if( VdbeMemDynamic(pOut
) ) sqlite3VdbeMemSetNull(pOut
);
1345 memcpy(pOut
, pVar
, MEMCELLSIZE
);
1346 pOut
->flags
&= ~(MEM_Dyn
|MEM_Ephem
);
1347 pOut
->flags
|= MEM_Static
|MEM_FromBind
;
1348 UPDATE_MAX_BLOBSIZE(pOut
);
1352 /* Opcode: Move P1 P2 P3 * *
1353 ** Synopsis: r[P2@P3]=r[P1@P3]
1355 ** Move the P3 values in register P1..P1+P3-1 over into
1356 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
1357 ** left holding a NULL. It is an error for register ranges
1358 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1359 ** for P3 to be less than 1.
1362 int n
; /* Number of registers left to copy */
1363 int p1
; /* Register to copy from */
1364 int p2
; /* Register to copy to */
1369 assert( n
>0 && p1
>0 && p2
>0 );
1370 assert( p1
+n
<=p2
|| p2
+n
<=p1
);
1375 assert( pOut
<=&aMem
[(p
->nMem
+1 - p
->nCursor
)] );
1376 assert( pIn1
<=&aMem
[(p
->nMem
+1 - p
->nCursor
)] );
1377 assert( memIsValid(pIn1
) );
1378 memAboutToChange(p
, pOut
);
1379 sqlite3VdbeMemMove(pOut
, pIn1
);
1381 pIn1
->pScopyFrom
= 0;
1383 for(i
=1; i
<p
->nMem
; i
++){
1384 if( aMem
[i
].pScopyFrom
==pIn1
){
1385 aMem
[i
].pScopyFrom
= pOut
;
1390 Deephemeralize(pOut
);
1391 REGISTER_TRACE(p2
++, pOut
);
1398 /* Opcode: Copy P1 P2 P3 * *
1399 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
1401 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
1403 ** This instruction makes a deep copy of the value. A duplicate
1404 ** is made of any string or blob constant. See also OP_SCopy.
1410 pIn1
= &aMem
[pOp
->p1
];
1411 pOut
= &aMem
[pOp
->p2
];
1412 assert( pOut
!=pIn1
);
1414 memAboutToChange(p
, pOut
);
1415 sqlite3VdbeMemShallowCopy(pOut
, pIn1
, MEM_Ephem
);
1416 Deephemeralize(pOut
);
1418 pOut
->pScopyFrom
= 0;
1420 REGISTER_TRACE(pOp
->p2
+pOp
->p3
-n
, pOut
);
1421 if( (n
--)==0 ) break;
1428 /* Opcode: SCopy P1 P2 * * *
1429 ** Synopsis: r[P2]=r[P1]
1431 ** Make a shallow copy of register P1 into register P2.
1433 ** This instruction makes a shallow copy of the value. If the value
1434 ** is a string or blob, then the copy is only a pointer to the
1435 ** original and hence if the original changes so will the copy.
1436 ** Worse, if the original is deallocated, the copy becomes invalid.
1437 ** Thus the program must guarantee that the original will not change
1438 ** during the lifetime of the copy. Use OP_Copy to make a complete
1441 case OP_SCopy
: { /* out2 */
1442 pIn1
= &aMem
[pOp
->p1
];
1443 pOut
= &aMem
[pOp
->p2
];
1444 assert( pOut
!=pIn1
);
1445 sqlite3VdbeMemShallowCopy(pOut
, pIn1
, MEM_Ephem
);
1447 pOut
->pScopyFrom
= pIn1
;
1448 pOut
->mScopyFlags
= pIn1
->flags
;
1453 /* Opcode: IntCopy P1 P2 * * *
1454 ** Synopsis: r[P2]=r[P1]
1456 ** Transfer the integer value held in register P1 into register P2.
1458 ** This is an optimized version of SCopy that works only for integer
1461 case OP_IntCopy
: { /* out2 */
1462 pIn1
= &aMem
[pOp
->p1
];
1463 assert( (pIn1
->flags
& MEM_Int
)!=0 );
1464 pOut
= &aMem
[pOp
->p2
];
1465 sqlite3VdbeMemSetInt64(pOut
, pIn1
->u
.i
);
1469 /* Opcode: ChngCntRow P1 P2 * * *
1470 ** Synopsis: output=r[P1]
1472 ** Output value in register P1 as the chance count for a DML statement,
1473 ** due to the "PRAGMA count_changes=ON" setting. Or, if there was a
1474 ** foreign key error in the statement, trigger the error now.
1476 ** This opcode is a variant of OP_ResultRow that checks the foreign key
1477 ** immediate constraint count and throws an error if the count is
1478 ** non-zero. The P2 opcode must be 1.
1480 case OP_ChngCntRow
: {
1481 assert( pOp
->p2
==1 );
1482 if( (rc
= sqlite3VdbeCheckFk(p
,0))!=SQLITE_OK
){
1483 goto abort_due_to_error
;
1485 /* Fall through to the next case, OP_ResultRow */
1486 /* no break */ deliberate_fall_through
1489 /* Opcode: ResultRow P1 P2 * * *
1490 ** Synopsis: output=r[P1@P2]
1492 ** The registers P1 through P1+P2-1 contain a single row of
1493 ** results. This opcode causes the sqlite3_step() call to terminate
1494 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1495 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
1498 case OP_ResultRow
: {
1501 assert( p
->nResColumn
==pOp
->p2
);
1502 assert( pOp
->p1
>0 || CORRUPT_DB
);
1503 assert( pOp
->p1
+pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
)+1 );
1505 /* Invalidate all ephemeral cursor row caches */
1506 p
->cacheCtr
= (p
->cacheCtr
+ 2)|1;
1508 /* Make sure the results of the current row are \000 terminated
1509 ** and have an assigned type. The results are de-ephemeralized as
1512 pMem
= p
->pResultSet
= &aMem
[pOp
->p1
];
1513 for(i
=0; i
<pOp
->p2
; i
++){
1514 assert( memIsValid(&pMem
[i
]) );
1515 Deephemeralize(&pMem
[i
]);
1516 assert( (pMem
[i
].flags
& MEM_Ephem
)==0
1517 || (pMem
[i
].flags
& (MEM_Str
|MEM_Blob
))==0 );
1518 sqlite3VdbeMemNulTerminate(&pMem
[i
]);
1519 REGISTER_TRACE(pOp
->p1
+i
, &pMem
[i
]);
1521 /* The registers in the result will not be used again when the
1522 ** prepared statement restarts. This is because sqlite3_column()
1523 ** APIs might have caused type conversions of made other changes to
1524 ** the register values. Therefore, we can go ahead and break any
1525 ** OP_SCopy dependencies. */
1526 pMem
[i
].pScopyFrom
= 0;
1529 if( db
->mallocFailed
) goto no_mem
;
1531 if( db
->mTrace
& SQLITE_TRACE_ROW
){
1532 db
->trace
.xV2(SQLITE_TRACE_ROW
, db
->pTraceArg
, p
, 0);
1536 /* Return SQLITE_ROW
1538 p
->pc
= (int)(pOp
- aOp
) + 1;
1543 /* Opcode: Concat P1 P2 P3 * *
1544 ** Synopsis: r[P3]=r[P2]+r[P1]
1546 ** Add the text in register P1 onto the end of the text in
1547 ** register P2 and store the result in register P3.
1548 ** If either the P1 or P2 text are NULL then store NULL in P3.
1552 ** It is illegal for P1 and P3 to be the same register. Sometimes,
1553 ** if P3 is the same register as P2, the implementation is able
1554 ** to avoid a memcpy().
1556 case OP_Concat
: { /* same as TK_CONCAT, in1, in2, out3 */
1557 i64 nByte
; /* Total size of the output string or blob */
1558 u16 flags1
; /* Initial flags for P1 */
1559 u16 flags2
; /* Initial flags for P2 */
1561 pIn1
= &aMem
[pOp
->p1
];
1562 pIn2
= &aMem
[pOp
->p2
];
1563 pOut
= &aMem
[pOp
->p3
];
1564 testcase( pOut
==pIn2
);
1565 assert( pIn1
!=pOut
);
1566 flags1
= pIn1
->flags
;
1567 testcase( flags1
& MEM_Null
);
1568 testcase( pIn2
->flags
& MEM_Null
);
1569 if( (flags1
| pIn2
->flags
) & MEM_Null
){
1570 sqlite3VdbeMemSetNull(pOut
);
1573 if( (flags1
& (MEM_Str
|MEM_Blob
))==0 ){
1574 if( sqlite3VdbeMemStringify(pIn1
,encoding
,0) ) goto no_mem
;
1575 flags1
= pIn1
->flags
& ~MEM_Str
;
1576 }else if( (flags1
& MEM_Zero
)!=0 ){
1577 if( sqlite3VdbeMemExpandBlob(pIn1
) ) goto no_mem
;
1578 flags1
= pIn1
->flags
& ~MEM_Str
;
1580 flags2
= pIn2
->flags
;
1581 if( (flags2
& (MEM_Str
|MEM_Blob
))==0 ){
1582 if( sqlite3VdbeMemStringify(pIn2
,encoding
,0) ) goto no_mem
;
1583 flags2
= pIn2
->flags
& ~MEM_Str
;
1584 }else if( (flags2
& MEM_Zero
)!=0 ){
1585 if( sqlite3VdbeMemExpandBlob(pIn2
) ) goto no_mem
;
1586 flags2
= pIn2
->flags
& ~MEM_Str
;
1588 nByte
= pIn1
->n
+ pIn2
->n
;
1589 if( nByte
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1592 if( sqlite3VdbeMemGrow(pOut
, (int)nByte
+3, pOut
==pIn2
) ){
1595 MemSetTypeFlag(pOut
, MEM_Str
);
1597 memcpy(pOut
->z
, pIn2
->z
, pIn2
->n
);
1598 assert( (pIn2
->flags
& MEM_Dyn
) == (flags2
& MEM_Dyn
) );
1599 pIn2
->flags
= flags2
;
1601 memcpy(&pOut
->z
[pIn2
->n
], pIn1
->z
, pIn1
->n
);
1602 assert( (pIn1
->flags
& MEM_Dyn
) == (flags1
& MEM_Dyn
) );
1603 pIn1
->flags
= flags1
;
1605 pOut
->z
[nByte
+1] = 0;
1606 pOut
->z
[nByte
+2] = 0;
1607 pOut
->flags
|= MEM_Term
;
1608 pOut
->n
= (int)nByte
;
1609 pOut
->enc
= encoding
;
1610 UPDATE_MAX_BLOBSIZE(pOut
);
1614 /* Opcode: Add P1 P2 P3 * *
1615 ** Synopsis: r[P3]=r[P1]+r[P2]
1617 ** Add the value in register P1 to the value in register P2
1618 ** and store the result in register P3.
1619 ** If either input is NULL, the result is NULL.
1621 /* Opcode: Multiply P1 P2 P3 * *
1622 ** Synopsis: r[P3]=r[P1]*r[P2]
1625 ** Multiply the value in register P1 by the value in register P2
1626 ** and store the result in register P3.
1627 ** If either input is NULL, the result is NULL.
1629 /* Opcode: Subtract P1 P2 P3 * *
1630 ** Synopsis: r[P3]=r[P2]-r[P1]
1632 ** Subtract the value in register P1 from the value in register P2
1633 ** and store the result in register P3.
1634 ** If either input is NULL, the result is NULL.
1636 /* Opcode: Divide P1 P2 P3 * *
1637 ** Synopsis: r[P3]=r[P2]/r[P1]
1639 ** Divide the value in register P1 by the value in register P2
1640 ** and store the result in register P3 (P3=P2/P1). If the value in
1641 ** register P1 is zero, then the result is NULL. If either input is
1642 ** NULL, the result is NULL.
1644 /* Opcode: Remainder P1 P2 P3 * *
1645 ** Synopsis: r[P3]=r[P2]%r[P1]
1647 ** Compute the remainder after integer register P2 is divided by
1648 ** register P1 and store the result in register P3.
1649 ** If the value in register P1 is zero the result is NULL.
1650 ** If either operand is NULL, the result is NULL.
1652 case OP_Add
: /* same as TK_PLUS, in1, in2, out3 */
1653 case OP_Subtract
: /* same as TK_MINUS, in1, in2, out3 */
1654 case OP_Multiply
: /* same as TK_STAR, in1, in2, out3 */
1655 case OP_Divide
: /* same as TK_SLASH, in1, in2, out3 */
1656 case OP_Remainder
: { /* same as TK_REM, in1, in2, out3 */
1657 u16 flags
; /* Combined MEM_* flags from both inputs */
1658 u16 type1
; /* Numeric type of left operand */
1659 u16 type2
; /* Numeric type of right operand */
1660 i64 iA
; /* Integer value of left operand */
1661 i64 iB
; /* Integer value of right operand */
1662 double rA
; /* Real value of left operand */
1663 double rB
; /* Real value of right operand */
1665 pIn1
= &aMem
[pOp
->p1
];
1666 type1
= numericType(pIn1
);
1667 pIn2
= &aMem
[pOp
->p2
];
1668 type2
= numericType(pIn2
);
1669 pOut
= &aMem
[pOp
->p3
];
1670 flags
= pIn1
->flags
| pIn2
->flags
;
1671 if( (type1
& type2
& MEM_Int
)!=0 ){
1674 switch( pOp
->opcode
){
1675 case OP_Add
: if( sqlite3AddInt64(&iB
,iA
) ) goto fp_math
; break;
1676 case OP_Subtract
: if( sqlite3SubInt64(&iB
,iA
) ) goto fp_math
; break;
1677 case OP_Multiply
: if( sqlite3MulInt64(&iB
,iA
) ) goto fp_math
; break;
1679 if( iA
==0 ) goto arithmetic_result_is_null
;
1680 if( iA
==-1 && iB
==SMALLEST_INT64
) goto fp_math
;
1685 if( iA
==0 ) goto arithmetic_result_is_null
;
1686 if( iA
==-1 ) iA
= 1;
1692 MemSetTypeFlag(pOut
, MEM_Int
);
1693 }else if( (flags
& MEM_Null
)!=0 ){
1694 goto arithmetic_result_is_null
;
1697 rA
= sqlite3VdbeRealValue(pIn1
);
1698 rB
= sqlite3VdbeRealValue(pIn2
);
1699 switch( pOp
->opcode
){
1700 case OP_Add
: rB
+= rA
; break;
1701 case OP_Subtract
: rB
-= rA
; break;
1702 case OP_Multiply
: rB
*= rA
; break;
1704 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
1705 if( rA
==(double)0 ) goto arithmetic_result_is_null
;
1710 iA
= sqlite3VdbeIntValue(pIn1
);
1711 iB
= sqlite3VdbeIntValue(pIn2
);
1712 if( iA
==0 ) goto arithmetic_result_is_null
;
1713 if( iA
==-1 ) iA
= 1;
1714 rB
= (double)(iB
% iA
);
1718 #ifdef SQLITE_OMIT_FLOATING_POINT
1720 MemSetTypeFlag(pOut
, MEM_Int
);
1722 if( sqlite3IsNaN(rB
) ){
1723 goto arithmetic_result_is_null
;
1726 MemSetTypeFlag(pOut
, MEM_Real
);
1731 arithmetic_result_is_null
:
1732 sqlite3VdbeMemSetNull(pOut
);
1736 /* Opcode: CollSeq P1 * * P4
1738 ** P4 is a pointer to a CollSeq object. If the next call to a user function
1739 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1740 ** be returned. This is used by the built-in min(), max() and nullif()
1743 ** If P1 is not zero, then it is a register that a subsequent min() or
1744 ** max() aggregate will set to 1 if the current row is not the minimum or
1745 ** maximum. The P1 register is initialized to 0 by this instruction.
1747 ** The interface used by the implementation of the aforementioned functions
1748 ** to retrieve the collation sequence set by this opcode is not available
1749 ** publicly. Only built-in functions have access to this feature.
1752 assert( pOp
->p4type
==P4_COLLSEQ
);
1754 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p1
], 0);
1759 /* Opcode: BitAnd P1 P2 P3 * *
1760 ** Synopsis: r[P3]=r[P1]&r[P2]
1762 ** Take the bit-wise AND of the values in register P1 and P2 and
1763 ** store the result in register P3.
1764 ** If either input is NULL, the result is NULL.
1766 /* Opcode: BitOr P1 P2 P3 * *
1767 ** Synopsis: r[P3]=r[P1]|r[P2]
1769 ** Take the bit-wise OR of the values in register P1 and P2 and
1770 ** store the result in register P3.
1771 ** If either input is NULL, the result is NULL.
1773 /* Opcode: ShiftLeft P1 P2 P3 * *
1774 ** Synopsis: r[P3]=r[P2]<<r[P1]
1776 ** Shift the integer value in register P2 to the left by the
1777 ** number of bits specified by the integer in register P1.
1778 ** Store the result in register P3.
1779 ** If either input is NULL, the result is NULL.
1781 /* Opcode: ShiftRight P1 P2 P3 * *
1782 ** Synopsis: r[P3]=r[P2]>>r[P1]
1784 ** Shift the integer value in register P2 to the right by the
1785 ** number of bits specified by the integer in register P1.
1786 ** Store the result in register P3.
1787 ** If either input is NULL, the result is NULL.
1789 case OP_BitAnd
: /* same as TK_BITAND, in1, in2, out3 */
1790 case OP_BitOr
: /* same as TK_BITOR, in1, in2, out3 */
1791 case OP_ShiftLeft
: /* same as TK_LSHIFT, in1, in2, out3 */
1792 case OP_ShiftRight
: { /* same as TK_RSHIFT, in1, in2, out3 */
1798 pIn1
= &aMem
[pOp
->p1
];
1799 pIn2
= &aMem
[pOp
->p2
];
1800 pOut
= &aMem
[pOp
->p3
];
1801 if( (pIn1
->flags
| pIn2
->flags
) & MEM_Null
){
1802 sqlite3VdbeMemSetNull(pOut
);
1805 iA
= sqlite3VdbeIntValue(pIn2
);
1806 iB
= sqlite3VdbeIntValue(pIn1
);
1808 if( op
==OP_BitAnd
){
1810 }else if( op
==OP_BitOr
){
1813 assert( op
==OP_ShiftRight
|| op
==OP_ShiftLeft
);
1815 /* If shifting by a negative amount, shift in the other direction */
1817 assert( OP_ShiftRight
==OP_ShiftLeft
+1 );
1818 op
= 2*OP_ShiftLeft
+ 1 - op
;
1819 iB
= iB
>(-64) ? -iB
: 64;
1823 iA
= (iA
>=0 || op
==OP_ShiftLeft
) ? 0 : -1;
1825 memcpy(&uA
, &iA
, sizeof(uA
));
1826 if( op
==OP_ShiftLeft
){
1830 /* Sign-extend on a right shift of a negative number */
1831 if( iA
<0 ) uA
|= ((((u64
)0xffffffff)<<32)|0xffffffff) << (64-iB
);
1833 memcpy(&iA
, &uA
, sizeof(iA
));
1837 MemSetTypeFlag(pOut
, MEM_Int
);
1841 /* Opcode: AddImm P1 P2 * * *
1842 ** Synopsis: r[P1]=r[P1]+P2
1844 ** Add the constant P2 to the value in register P1.
1845 ** The result is always an integer.
1847 ** To force any register to be an integer, just add 0.
1849 case OP_AddImm
: { /* in1 */
1850 pIn1
= &aMem
[pOp
->p1
];
1851 memAboutToChange(p
, pIn1
);
1852 sqlite3VdbeMemIntegerify(pIn1
);
1853 pIn1
->u
.i
+= pOp
->p2
;
1857 /* Opcode: MustBeInt P1 P2 * * *
1859 ** Force the value in register P1 to be an integer. If the value
1860 ** in P1 is not an integer and cannot be converted into an integer
1861 ** without data loss, then jump immediately to P2, or if P2==0
1862 ** raise an SQLITE_MISMATCH exception.
1864 case OP_MustBeInt
: { /* jump, in1 */
1865 pIn1
= &aMem
[pOp
->p1
];
1866 if( (pIn1
->flags
& MEM_Int
)==0 ){
1867 applyAffinity(pIn1
, SQLITE_AFF_NUMERIC
, encoding
);
1868 if( (pIn1
->flags
& MEM_Int
)==0 ){
1869 VdbeBranchTaken(1, 2);
1871 rc
= SQLITE_MISMATCH
;
1872 goto abort_due_to_error
;
1878 VdbeBranchTaken(0, 2);
1879 MemSetTypeFlag(pIn1
, MEM_Int
);
1883 #ifndef SQLITE_OMIT_FLOATING_POINT
1884 /* Opcode: RealAffinity P1 * * * *
1886 ** If register P1 holds an integer convert it to a real value.
1888 ** This opcode is used when extracting information from a column that
1889 ** has REAL affinity. Such column values may still be stored as
1890 ** integers, for space efficiency, but after extraction we want them
1891 ** to have only a real value.
1893 case OP_RealAffinity
: { /* in1 */
1894 pIn1
= &aMem
[pOp
->p1
];
1895 if( pIn1
->flags
& (MEM_Int
|MEM_IntReal
) ){
1896 testcase( pIn1
->flags
& MEM_Int
);
1897 testcase( pIn1
->flags
& MEM_IntReal
);
1898 sqlite3VdbeMemRealify(pIn1
);
1899 REGISTER_TRACE(pOp
->p1
, pIn1
);
1905 #ifndef SQLITE_OMIT_CAST
1906 /* Opcode: Cast P1 P2 * * *
1907 ** Synopsis: affinity(r[P1])
1909 ** Force the value in register P1 to be the type defined by P2.
1912 ** <li> P2=='A' → BLOB
1913 ** <li> P2=='B' → TEXT
1914 ** <li> P2=='C' → NUMERIC
1915 ** <li> P2=='D' → INTEGER
1916 ** <li> P2=='E' → REAL
1919 ** A NULL value is not changed by this routine. It remains NULL.
1921 case OP_Cast
: { /* in1 */
1922 assert( pOp
->p2
>=SQLITE_AFF_BLOB
&& pOp
->p2
<=SQLITE_AFF_REAL
);
1923 testcase( pOp
->p2
==SQLITE_AFF_TEXT
);
1924 testcase( pOp
->p2
==SQLITE_AFF_BLOB
);
1925 testcase( pOp
->p2
==SQLITE_AFF_NUMERIC
);
1926 testcase( pOp
->p2
==SQLITE_AFF_INTEGER
);
1927 testcase( pOp
->p2
==SQLITE_AFF_REAL
);
1928 pIn1
= &aMem
[pOp
->p1
];
1929 memAboutToChange(p
, pIn1
);
1930 rc
= ExpandBlob(pIn1
);
1931 if( rc
) goto abort_due_to_error
;
1932 rc
= sqlite3VdbeMemCast(pIn1
, pOp
->p2
, encoding
);
1933 if( rc
) goto abort_due_to_error
;
1934 UPDATE_MAX_BLOBSIZE(pIn1
);
1935 REGISTER_TRACE(pOp
->p1
, pIn1
);
1938 #endif /* SQLITE_OMIT_CAST */
1940 /* Opcode: Eq P1 P2 P3 P4 P5
1941 ** Synopsis: IF r[P3]==r[P1]
1943 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1944 ** jump to address P2.
1946 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1947 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1948 ** to coerce both inputs according to this affinity before the
1949 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1950 ** affinity is used. Note that the affinity conversions are stored
1951 ** back into the input registers P1 and P3. So this opcode can cause
1952 ** persistent changes to registers P1 and P3.
1954 ** Once any conversions have taken place, and neither value is NULL,
1955 ** the values are compared. If both values are blobs then memcmp() is
1956 ** used to determine the results of the comparison. If both values
1957 ** are text, then the appropriate collating function specified in
1958 ** P4 is used to do the comparison. If P4 is not specified then
1959 ** memcmp() is used to compare text string. If both values are
1960 ** numeric, then a numeric comparison is used. If the two values
1961 ** are of different types, then numbers are considered less than
1962 ** strings and strings are considered less than blobs.
1964 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1965 ** true or false and is never NULL. If both operands are NULL then the result
1966 ** of comparison is true. If either operand is NULL then the result is false.
1967 ** If neither operand is NULL the result is the same as it would be if
1968 ** the SQLITE_NULLEQ flag were omitted from P5.
1970 ** This opcode saves the result of comparison for use by the new
1973 /* Opcode: Ne P1 P2 P3 P4 P5
1974 ** Synopsis: IF r[P3]!=r[P1]
1976 ** This works just like the Eq opcode except that the jump is taken if
1977 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1978 ** additional information.
1980 /* Opcode: Lt P1 P2 P3 P4 P5
1981 ** Synopsis: IF r[P3]<r[P1]
1983 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1984 ** jump to address P2.
1986 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1987 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
1988 ** bit is clear then fall through if either operand is NULL.
1990 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1991 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1992 ** to coerce both inputs according to this affinity before the
1993 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1994 ** affinity is used. Note that the affinity conversions are stored
1995 ** back into the input registers P1 and P3. So this opcode can cause
1996 ** persistent changes to registers P1 and P3.
1998 ** Once any conversions have taken place, and neither value is NULL,
1999 ** the values are compared. If both values are blobs then memcmp() is
2000 ** used to determine the results of the comparison. If both values
2001 ** are text, then the appropriate collating function specified in
2002 ** P4 is used to do the comparison. If P4 is not specified then
2003 ** memcmp() is used to compare text string. If both values are
2004 ** numeric, then a numeric comparison is used. If the two values
2005 ** are of different types, then numbers are considered less than
2006 ** strings and strings are considered less than blobs.
2008 ** This opcode saves the result of comparison for use by the new
2011 /* Opcode: Le P1 P2 P3 P4 P5
2012 ** Synopsis: IF r[P3]<=r[P1]
2014 ** This works just like the Lt opcode except that the jump is taken if
2015 ** the content of register P3 is less than or equal to the content of
2016 ** register P1. See the Lt opcode for additional information.
2018 /* Opcode: Gt P1 P2 P3 P4 P5
2019 ** Synopsis: IF r[P3]>r[P1]
2021 ** This works just like the Lt opcode except that the jump is taken if
2022 ** the content of register P3 is greater than the content of
2023 ** register P1. See the Lt opcode for additional information.
2025 /* Opcode: Ge P1 P2 P3 P4 P5
2026 ** Synopsis: IF r[P3]>=r[P1]
2028 ** This works just like the Lt opcode except that the jump is taken if
2029 ** the content of register P3 is greater than or equal to the content of
2030 ** register P1. See the Lt opcode for additional information.
2032 case OP_Eq
: /* same as TK_EQ, jump, in1, in3 */
2033 case OP_Ne
: /* same as TK_NE, jump, in1, in3 */
2034 case OP_Lt
: /* same as TK_LT, jump, in1, in3 */
2035 case OP_Le
: /* same as TK_LE, jump, in1, in3 */
2036 case OP_Gt
: /* same as TK_GT, jump, in1, in3 */
2037 case OP_Ge
: { /* same as TK_GE, jump, in1, in3 */
2038 int res
, res2
; /* Result of the comparison of pIn1 against pIn3 */
2039 char affinity
; /* Affinity to use for comparison */
2040 u16 flags1
; /* Copy of initial value of pIn1->flags */
2041 u16 flags3
; /* Copy of initial value of pIn3->flags */
2043 pIn1
= &aMem
[pOp
->p1
];
2044 pIn3
= &aMem
[pOp
->p3
];
2045 flags1
= pIn1
->flags
;
2046 flags3
= pIn3
->flags
;
2047 if( (flags1
& flags3
& MEM_Int
)!=0 ){
2048 assert( (pOp
->p5
& SQLITE_AFF_MASK
)!=SQLITE_AFF_TEXT
|| CORRUPT_DB
);
2049 /* Common case of comparison of two integers */
2050 if( pIn3
->u
.i
> pIn1
->u
.i
){
2052 if( sqlite3aGTb
[pOp
->opcode
] ){
2053 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2056 }else if( pIn3
->u
.i
< pIn1
->u
.i
){
2058 if( sqlite3aLTb
[pOp
->opcode
] ){
2059 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2064 if( sqlite3aEQb
[pOp
->opcode
] ){
2065 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2069 VdbeBranchTaken(0, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2072 if( (flags1
| flags3
)&MEM_Null
){
2073 /* One or both operands are NULL */
2074 if( pOp
->p5
& SQLITE_NULLEQ
){
2075 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2076 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2077 ** or not both operands are null.
2079 assert( (flags1
& MEM_Cleared
)==0 );
2080 assert( (pOp
->p5
& SQLITE_JUMPIFNULL
)==0 || CORRUPT_DB
);
2081 testcase( (pOp
->p5
& SQLITE_JUMPIFNULL
)!=0 );
2082 if( (flags1
&flags3
&MEM_Null
)!=0
2083 && (flags3
&MEM_Cleared
)==0
2085 res
= 0; /* Operands are equal */
2087 res
= ((flags3
& MEM_Null
) ? -1 : +1); /* Operands are not equal */
2090 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2091 ** then the result is always NULL.
2092 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2094 iCompare
= 1; /* Operands are not equal */
2095 VdbeBranchTaken(2,3);
2096 if( pOp
->p5
& SQLITE_JUMPIFNULL
){
2102 /* Neither operand is NULL and we couldn't do the special high-speed
2103 ** integer comparison case. So do a general-case comparison. */
2104 affinity
= pOp
->p5
& SQLITE_AFF_MASK
;
2105 if( affinity
>=SQLITE_AFF_NUMERIC
){
2106 if( (flags1
| flags3
)&MEM_Str
){
2107 if( (flags1
& (MEM_Int
|MEM_IntReal
|MEM_Real
|MEM_Str
))==MEM_Str
){
2108 applyNumericAffinity(pIn1
,0);
2109 testcase( flags3
==pIn3
->flags
);
2110 flags3
= pIn3
->flags
;
2112 if( (flags3
& (MEM_Int
|MEM_IntReal
|MEM_Real
|MEM_Str
))==MEM_Str
){
2113 applyNumericAffinity(pIn3
,0);
2116 }else if( affinity
==SQLITE_AFF_TEXT
){
2117 if( (flags1
& MEM_Str
)==0 && (flags1
&(MEM_Int
|MEM_Real
|MEM_IntReal
))!=0 ){
2118 testcase( pIn1
->flags
& MEM_Int
);
2119 testcase( pIn1
->flags
& MEM_Real
);
2120 testcase( pIn1
->flags
& MEM_IntReal
);
2121 sqlite3VdbeMemStringify(pIn1
, encoding
, 1);
2122 testcase( (flags1
&MEM_Dyn
) != (pIn1
->flags
&MEM_Dyn
) );
2123 flags1
= (pIn1
->flags
& ~MEM_TypeMask
) | (flags1
& MEM_TypeMask
);
2124 if( NEVER(pIn1
==pIn3
) ) flags3
= flags1
| MEM_Str
;
2126 if( (flags3
& MEM_Str
)==0 && (flags3
&(MEM_Int
|MEM_Real
|MEM_IntReal
))!=0 ){
2127 testcase( pIn3
->flags
& MEM_Int
);
2128 testcase( pIn3
->flags
& MEM_Real
);
2129 testcase( pIn3
->flags
& MEM_IntReal
);
2130 sqlite3VdbeMemStringify(pIn3
, encoding
, 1);
2131 testcase( (flags3
&MEM_Dyn
) != (pIn3
->flags
&MEM_Dyn
) );
2132 flags3
= (pIn3
->flags
& ~MEM_TypeMask
) | (flags3
& MEM_TypeMask
);
2135 assert( pOp
->p4type
==P4_COLLSEQ
|| pOp
->p4
.pColl
==0 );
2136 res
= sqlite3MemCompare(pIn3
, pIn1
, pOp
->p4
.pColl
);
2139 /* At this point, res is negative, zero, or positive if reg[P1] is
2140 ** less than, equal to, or greater than reg[P3], respectively. Compute
2141 ** the answer to this operator in res2, depending on what the comparison
2142 ** operator actually is. The next block of code depends on the fact
2143 ** that the 6 comparison operators are consecutive integers in this
2144 ** order: NE, EQ, GT, LE, LT, GE */
2145 assert( OP_Eq
==OP_Ne
+1 ); assert( OP_Gt
==OP_Ne
+2 ); assert( OP_Le
==OP_Ne
+3 );
2146 assert( OP_Lt
==OP_Ne
+4 ); assert( OP_Ge
==OP_Ne
+5 );
2148 res2
= sqlite3aLTb
[pOp
->opcode
];
2150 res2
= sqlite3aEQb
[pOp
->opcode
];
2152 res2
= sqlite3aGTb
[pOp
->opcode
];
2156 /* Undo any changes made by applyAffinity() to the input registers. */
2157 assert( (pIn3
->flags
& MEM_Dyn
) == (flags3
& MEM_Dyn
) );
2158 pIn3
->flags
= flags3
;
2159 assert( (pIn1
->flags
& MEM_Dyn
) == (flags1
& MEM_Dyn
) );
2160 pIn1
->flags
= flags1
;
2162 VdbeBranchTaken(res2
!=0, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2169 /* Opcode: ElseEq * P2 * * *
2171 ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2172 ** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2173 ** opcodes are allowed to occur between this instruction and the previous
2176 ** If result of an OP_Eq comparison on the same two operands as the
2177 ** prior OP_Lt or OP_Gt would have been true, then jump to P2.
2178 ** If the result of an OP_Eq comparison on the two previous
2179 ** operands would have been false or NULL, then fall through.
2181 case OP_ElseEq
: { /* same as TK_ESCAPE, jump */
2184 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
2185 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
2187 for(iAddr
= (int)(pOp
- aOp
) - 1; ALWAYS(iAddr
>=0); iAddr
--){
2188 if( aOp
[iAddr
].opcode
==OP_ReleaseReg
) continue;
2189 assert( aOp
[iAddr
].opcode
==OP_Lt
|| aOp
[iAddr
].opcode
==OP_Gt
);
2192 #endif /* SQLITE_DEBUG */
2193 VdbeBranchTaken(iCompare
==0, 2);
2194 if( iCompare
==0 ) goto jump_to_p2
;
2199 /* Opcode: Permutation * * * P4 *
2201 ** Set the permutation used by the OP_Compare operator in the next
2202 ** instruction. The permutation is stored in the P4 operand.
2204 ** The permutation is only valid until the next OP_Compare that has
2205 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2206 ** occur immediately prior to the OP_Compare.
2208 ** The first integer in the P4 integer array is the length of the array
2209 ** and does not become part of the permutation.
2211 case OP_Permutation
: {
2212 assert( pOp
->p4type
==P4_INTARRAY
);
2213 assert( pOp
->p4
.ai
);
2214 assert( pOp
[1].opcode
==OP_Compare
);
2215 assert( pOp
[1].p5
& OPFLAG_PERMUTE
);
2219 /* Opcode: Compare P1 P2 P3 P4 P5
2220 ** Synopsis: r[P1@P3] <-> r[P2@P3]
2222 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2223 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
2224 ** the comparison for use by the next OP_Jump instruct.
2226 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2227 ** determined by the most recent OP_Permutation operator. If the
2228 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2231 ** P4 is a KeyInfo structure that defines collating sequences and sort
2232 ** orders for the comparison. The permutation applies to registers
2233 ** only. The KeyInfo elements are used sequentially.
2235 ** The comparison is a sort comparison, so NULLs compare equal,
2236 ** NULLs are less than numbers, numbers are less than strings,
2237 ** and strings are less than blobs.
2244 const KeyInfo
*pKeyInfo
;
2246 CollSeq
*pColl
; /* Collating sequence to use on this term */
2247 int bRev
; /* True for DESCENDING sort order */
2248 u32
*aPermute
; /* The permutation */
2250 if( (pOp
->p5
& OPFLAG_PERMUTE
)==0 ){
2254 assert( pOp
[-1].opcode
==OP_Permutation
);
2255 assert( pOp
[-1].p4type
==P4_INTARRAY
);
2256 aPermute
= pOp
[-1].p4
.ai
+ 1;
2257 assert( aPermute
!=0 );
2260 pKeyInfo
= pOp
->p4
.pKeyInfo
;
2262 assert( pKeyInfo
!=0 );
2268 for(k
=0; k
<n
; k
++) if( aPermute
[k
]>(u32
)mx
) mx
= aPermute
[k
];
2269 assert( p1
>0 && p1
+mx
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2270 assert( p2
>0 && p2
+mx
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2272 assert( p1
>0 && p1
+n
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2273 assert( p2
>0 && p2
+n
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2275 #endif /* SQLITE_DEBUG */
2277 idx
= aPermute
? aPermute
[i
] : (u32
)i
;
2278 assert( memIsValid(&aMem
[p1
+idx
]) );
2279 assert( memIsValid(&aMem
[p2
+idx
]) );
2280 REGISTER_TRACE(p1
+idx
, &aMem
[p1
+idx
]);
2281 REGISTER_TRACE(p2
+idx
, &aMem
[p2
+idx
]);
2282 assert( i
<pKeyInfo
->nKeyField
);
2283 pColl
= pKeyInfo
->aColl
[i
];
2284 bRev
= (pKeyInfo
->aSortFlags
[i
] & KEYINFO_ORDER_DESC
);
2285 iCompare
= sqlite3MemCompare(&aMem
[p1
+idx
], &aMem
[p2
+idx
], pColl
);
2287 if( (pKeyInfo
->aSortFlags
[i
] & KEYINFO_ORDER_BIGNULL
)
2288 && ((aMem
[p1
+idx
].flags
& MEM_Null
) || (aMem
[p2
+idx
].flags
& MEM_Null
))
2290 iCompare
= -iCompare
;
2292 if( bRev
) iCompare
= -iCompare
;
2299 /* Opcode: Jump P1 P2 P3 * *
2301 ** Jump to the instruction at address P1, P2, or P3 depending on whether
2302 ** in the most recent OP_Compare instruction the P1 vector was less than
2303 ** equal to, or greater than the P2 vector, respectively.
2305 case OP_Jump
: { /* jump */
2307 VdbeBranchTaken(0,4); pOp
= &aOp
[pOp
->p1
- 1];
2308 }else if( iCompare
==0 ){
2309 VdbeBranchTaken(1,4); pOp
= &aOp
[pOp
->p2
- 1];
2311 VdbeBranchTaken(2,4); pOp
= &aOp
[pOp
->p3
- 1];
2316 /* Opcode: And P1 P2 P3 * *
2317 ** Synopsis: r[P3]=(r[P1] && r[P2])
2319 ** Take the logical AND of the values in registers P1 and P2 and
2320 ** write the result into register P3.
2322 ** If either P1 or P2 is 0 (false) then the result is 0 even if
2323 ** the other input is NULL. A NULL and true or two NULLs give
2326 /* Opcode: Or P1 P2 P3 * *
2327 ** Synopsis: r[P3]=(r[P1] || r[P2])
2329 ** Take the logical OR of the values in register P1 and P2 and
2330 ** store the answer in register P3.
2332 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2333 ** even if the other input is NULL. A NULL and false or two NULLs
2334 ** give a NULL output.
2336 case OP_And
: /* same as TK_AND, in1, in2, out3 */
2337 case OP_Or
: { /* same as TK_OR, in1, in2, out3 */
2338 int v1
; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2339 int v2
; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2341 v1
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], 2);
2342 v2
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p2
], 2);
2343 if( pOp
->opcode
==OP_And
){
2344 static const unsigned char and_logic
[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
2345 v1
= and_logic
[v1
*3+v2
];
2347 static const unsigned char or_logic
[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
2348 v1
= or_logic
[v1
*3+v2
];
2350 pOut
= &aMem
[pOp
->p3
];
2352 MemSetTypeFlag(pOut
, MEM_Null
);
2355 MemSetTypeFlag(pOut
, MEM_Int
);
2360 /* Opcode: IsTrue P1 P2 P3 P4 *
2361 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2363 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2364 ** IS NOT FALSE operators.
2366 ** Interpret the value in register P1 as a boolean value. Store that
2367 ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2368 ** NULL, then the P3 is stored in register P2. Invert the answer if P4
2371 ** The logic is summarized like this:
2374 ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2375 ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2376 ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2377 ** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
2380 case OP_IsTrue
: { /* in1, out2 */
2381 assert( pOp
->p4type
==P4_INT32
);
2382 assert( pOp
->p4
.i
==0 || pOp
->p4
.i
==1 );
2383 assert( pOp
->p3
==0 || pOp
->p3
==1 );
2384 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p2
],
2385 sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], pOp
->p3
) ^ pOp
->p4
.i
);
2389 /* Opcode: Not P1 P2 * * *
2390 ** Synopsis: r[P2]= !r[P1]
2392 ** Interpret the value in register P1 as a boolean value. Store the
2393 ** boolean complement in register P2. If the value in register P1 is
2394 ** NULL, then a NULL is stored in P2.
2396 case OP_Not
: { /* same as TK_NOT, in1, out2 */
2397 pIn1
= &aMem
[pOp
->p1
];
2398 pOut
= &aMem
[pOp
->p2
];
2399 if( (pIn1
->flags
& MEM_Null
)==0 ){
2400 sqlite3VdbeMemSetInt64(pOut
, !sqlite3VdbeBooleanValue(pIn1
,0));
2402 sqlite3VdbeMemSetNull(pOut
);
2407 /* Opcode: BitNot P1 P2 * * *
2408 ** Synopsis: r[P2]= ~r[P1]
2410 ** Interpret the content of register P1 as an integer. Store the
2411 ** ones-complement of the P1 value into register P2. If P1 holds
2412 ** a NULL then store a NULL in P2.
2414 case OP_BitNot
: { /* same as TK_BITNOT, in1, out2 */
2415 pIn1
= &aMem
[pOp
->p1
];
2416 pOut
= &aMem
[pOp
->p2
];
2417 sqlite3VdbeMemSetNull(pOut
);
2418 if( (pIn1
->flags
& MEM_Null
)==0 ){
2419 pOut
->flags
= MEM_Int
;
2420 pOut
->u
.i
= ~sqlite3VdbeIntValue(pIn1
);
2425 /* Opcode: Once P1 P2 * * *
2427 ** Fall through to the next instruction the first time this opcode is
2428 ** encountered on each invocation of the byte-code program. Jump to P2
2429 ** on the second and all subsequent encounters during the same invocation.
2431 ** Top-level programs determine first invocation by comparing the P1
2432 ** operand against the P1 operand on the OP_Init opcode at the beginning
2433 ** of the program. If the P1 values differ, then fall through and make
2434 ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2435 ** the same then take the jump.
2437 ** For subprograms, there is a bitmask in the VdbeFrame that determines
2438 ** whether or not the jump should be taken. The bitmask is necessary
2439 ** because the self-altering code trick does not work for recursive
2442 case OP_Once
: { /* jump */
2443 u32 iAddr
; /* Address of this instruction */
2444 assert( p
->aOp
[0].opcode
==OP_Init
);
2446 iAddr
= (int)(pOp
- p
->aOp
);
2447 if( (p
->pFrame
->aOnce
[iAddr
/8] & (1<<(iAddr
& 7)))!=0 ){
2448 VdbeBranchTaken(1, 2);
2451 p
->pFrame
->aOnce
[iAddr
/8] |= 1<<(iAddr
& 7);
2453 if( p
->aOp
[0].p1
==pOp
->p1
){
2454 VdbeBranchTaken(1, 2);
2458 VdbeBranchTaken(0, 2);
2459 pOp
->p1
= p
->aOp
[0].p1
;
2463 /* Opcode: If P1 P2 P3 * *
2465 ** Jump to P2 if the value in register P1 is true. The value
2466 ** is considered true if it is numeric and non-zero. If the value
2467 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2469 case OP_If
: { /* jump, in1 */
2471 c
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], pOp
->p3
);
2472 VdbeBranchTaken(c
!=0, 2);
2473 if( c
) goto jump_to_p2
;
2477 /* Opcode: IfNot P1 P2 P3 * *
2479 ** Jump to P2 if the value in register P1 is False. The value
2480 ** is considered false if it has a numeric value of zero. If the value
2481 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2483 case OP_IfNot
: { /* jump, in1 */
2485 c
= !sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], !pOp
->p3
);
2486 VdbeBranchTaken(c
!=0, 2);
2487 if( c
) goto jump_to_p2
;
2491 /* Opcode: IsNull P1 P2 * * *
2492 ** Synopsis: if r[P1]==NULL goto P2
2494 ** Jump to P2 if the value in register P1 is NULL.
2496 case OP_IsNull
: { /* same as TK_ISNULL, jump, in1 */
2497 pIn1
= &aMem
[pOp
->p1
];
2498 VdbeBranchTaken( (pIn1
->flags
& MEM_Null
)!=0, 2);
2499 if( (pIn1
->flags
& MEM_Null
)!=0 ){
2505 /* Opcode: ZeroOrNull P1 P2 P3 * *
2506 ** Synopsis: r[P2] = 0 OR NULL
2508 ** If all both registers P1 and P3 are NOT NULL, then store a zero in
2509 ** register P2. If either registers P1 or P3 are NULL then put
2510 ** a NULL in register P2.
2512 case OP_ZeroOrNull
: { /* in1, in2, out2, in3 */
2513 if( (aMem
[pOp
->p1
].flags
& MEM_Null
)!=0
2514 || (aMem
[pOp
->p3
].flags
& MEM_Null
)!=0
2516 sqlite3VdbeMemSetNull(aMem
+ pOp
->p2
);
2518 sqlite3VdbeMemSetInt64(aMem
+ pOp
->p2
, 0);
2523 /* Opcode: NotNull P1 P2 * * *
2524 ** Synopsis: if r[P1]!=NULL goto P2
2526 ** Jump to P2 if the value in register P1 is not NULL.
2528 case OP_NotNull
: { /* same as TK_NOTNULL, jump, in1 */
2529 pIn1
= &aMem
[pOp
->p1
];
2530 VdbeBranchTaken( (pIn1
->flags
& MEM_Null
)==0, 2);
2531 if( (pIn1
->flags
& MEM_Null
)==0 ){
2537 /* Opcode: IfNullRow P1 P2 P3 * *
2538 ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2540 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
2541 ** If it is, then set register P3 to NULL and jump immediately to P2.
2542 ** If P1 is not on a NULL row, then fall through without making any
2545 case OP_IfNullRow
: { /* jump */
2546 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2547 assert( p
->apCsr
[pOp
->p1
]!=0 );
2548 if( p
->apCsr
[pOp
->p1
]->nullRow
){
2549 sqlite3VdbeMemSetNull(aMem
+ pOp
->p3
);
2555 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2556 /* Opcode: Offset P1 P2 P3 * *
2557 ** Synopsis: r[P3] = sqlite_offset(P1)
2559 ** Store in register r[P3] the byte offset into the database file that is the
2560 ** start of the payload for the record at which that cursor P1 is currently
2563 ** P2 is the column number for the argument to the sqlite_offset() function.
2564 ** This opcode does not use P2 itself, but the P2 value is used by the
2565 ** code generator. The P1, P2, and P3 operands to this opcode are the
2566 ** same as for OP_Column.
2568 ** This opcode is only available if SQLite is compiled with the
2569 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
2571 case OP_Offset
: { /* out3 */
2572 VdbeCursor
*pC
; /* The VDBE cursor */
2573 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2574 pC
= p
->apCsr
[pOp
->p1
];
2575 pOut
= &p
->aMem
[pOp
->p3
];
2576 if( NEVER(pC
==0) || pC
->eCurType
!=CURTYPE_BTREE
){
2577 sqlite3VdbeMemSetNull(pOut
);
2579 sqlite3VdbeMemSetInt64(pOut
, sqlite3BtreeOffset(pC
->uc
.pCursor
));
2583 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
2585 /* Opcode: Column P1 P2 P3 P4 P5
2586 ** Synopsis: r[P3]=PX
2588 ** Interpret the data that cursor P1 points to as a structure built using
2589 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
2590 ** information about the format of the data.) Extract the P2-th column
2591 ** from this record. If there are less that (P2+1)
2592 ** values in the record, extract a NULL.
2594 ** The value extracted is stored in register P3.
2596 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
2597 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
2600 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
2601 ** the result is guaranteed to only be used as the argument of a length()
2602 ** or typeof() function, respectively. The loading of large blobs can be
2603 ** skipped for length() and all content loading can be skipped for typeof().
2606 u32 p2
; /* column number to retrieve */
2607 VdbeCursor
*pC
; /* The VDBE cursor */
2608 BtCursor
*pCrsr
; /* The BTree cursor */
2609 u32
*aOffset
; /* aOffset[i] is offset to start of data for i-th column */
2610 int len
; /* The length of the serialized data for the column */
2611 int i
; /* Loop counter */
2612 Mem
*pDest
; /* Where to write the extracted value */
2613 Mem sMem
; /* For storing the record being decoded */
2614 const u8
*zData
; /* Part of the record being decoded */
2615 const u8
*zHdr
; /* Next unparsed byte of the header */
2616 const u8
*zEndHdr
; /* Pointer to first byte after the header */
2617 u64 offset64
; /* 64-bit offset */
2618 u32 t
; /* A type code from the record header */
2619 Mem
*pReg
; /* PseudoTable input register */
2621 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2622 pC
= p
->apCsr
[pOp
->p1
];
2626 /* If the cursor cache is stale (meaning it is not currently point at
2627 ** the correct row) then bring it up-to-date by doing the necessary
2629 rc
= sqlite3VdbeCursorMoveto(&pC
, &p2
);
2630 if( rc
) goto abort_due_to_error
;
2632 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
2633 pDest
= &aMem
[pOp
->p3
];
2634 memAboutToChange(p
, pDest
);
2636 assert( p2
<(u32
)pC
->nField
);
2637 aOffset
= pC
->aOffset
;
2638 assert( pC
->eCurType
!=CURTYPE_VTAB
);
2639 assert( pC
->eCurType
!=CURTYPE_PSEUDO
|| pC
->nullRow
);
2640 assert( pC
->eCurType
!=CURTYPE_SORTER
);
2642 if( pC
->cacheStatus
!=p
->cacheCtr
){ /*OPTIMIZATION-IF-FALSE*/
2644 if( pC
->eCurType
==CURTYPE_PSEUDO
){
2645 /* For the special case of as pseudo-cursor, the seekResult field
2646 ** identifies the register that holds the record */
2647 assert( pC
->seekResult
>0 );
2648 pReg
= &aMem
[pC
->seekResult
];
2649 assert( pReg
->flags
& MEM_Blob
);
2650 assert( memIsValid(pReg
) );
2651 pC
->payloadSize
= pC
->szRow
= pReg
->n
;
2652 pC
->aRow
= (u8
*)pReg
->z
;
2654 sqlite3VdbeMemSetNull(pDest
);
2658 pCrsr
= pC
->uc
.pCursor
;
2659 assert( pC
->eCurType
==CURTYPE_BTREE
);
2661 assert( sqlite3BtreeCursorIsValid(pCrsr
) );
2662 pC
->payloadSize
= sqlite3BtreePayloadSize(pCrsr
);
2663 pC
->aRow
= sqlite3BtreePayloadFetch(pCrsr
, &pC
->szRow
);
2664 assert( pC
->szRow
<=pC
->payloadSize
);
2665 assert( pC
->szRow
<=65536 ); /* Maximum page size is 64KiB */
2666 if( pC
->payloadSize
> (u32
)db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
2670 pC
->cacheStatus
= p
->cacheCtr
;
2671 pC
->iHdrOffset
= getVarint32(pC
->aRow
, aOffset
[0]);
2675 if( pC
->szRow
<aOffset
[0] ){ /*OPTIMIZATION-IF-FALSE*/
2676 /* pC->aRow does not have to hold the entire row, but it does at least
2677 ** need to cover the header of the record. If pC->aRow does not contain
2678 ** the complete header, then set it to zero, forcing the header to be
2679 ** dynamically allocated. */
2683 /* Make sure a corrupt database has not given us an oversize header.
2684 ** Do this now to avoid an oversize memory allocation.
2686 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2687 ** types use so much data space that there can only be 4096 and 32 of
2688 ** them, respectively. So the maximum header length results from a
2689 ** 3-byte type for each of the maximum of 32768 columns plus three
2690 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2692 if( aOffset
[0] > 98307 || aOffset
[0] > pC
->payloadSize
){
2693 goto op_column_corrupt
;
2696 /* This is an optimization. By skipping over the first few tests
2697 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2698 ** measurable performance gain.
2700 ** This branch is taken even if aOffset[0]==0. Such a record is never
2701 ** generated by SQLite, and could be considered corruption, but we
2702 ** accept it for historical reasons. When aOffset[0]==0, the code this
2703 ** branch jumps to reads past the end of the record, but never more
2704 ** than a few bytes. Even if the record occurs at the end of the page
2705 ** content area, the "page header" comes after the page content and so
2706 ** this overread is harmless. Similar overreads can occur for a corrupt
2710 assert( pC
->nHdrParsed
<=p2
); /* Conditional skipped */
2711 testcase( aOffset
[0]==0 );
2712 goto op_column_read_header
;
2716 /* Make sure at least the first p2+1 entries of the header have been
2717 ** parsed and valid information is in aOffset[] and pC->aType[].
2719 if( pC
->nHdrParsed
<=p2
){
2720 /* If there is more header available for parsing in the record, try
2721 ** to extract additional fields up through the p2+1-th field
2723 if( pC
->iHdrOffset
<aOffset
[0] ){
2724 /* Make sure zData points to enough of the record to cover the header. */
2726 memset(&sMem
, 0, sizeof(sMem
));
2727 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pC
->uc
.pCursor
,aOffset
[0],&sMem
);
2728 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
2729 zData
= (u8
*)sMem
.z
;
2734 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
2735 op_column_read_header
:
2737 offset64
= aOffset
[i
];
2738 zHdr
= zData
+ pC
->iHdrOffset
;
2739 zEndHdr
= zData
+ aOffset
[0];
2740 testcase( zHdr
>=zEndHdr
);
2742 if( (pC
->aType
[i
] = t
= zHdr
[0])<0x80 ){
2744 offset64
+= sqlite3VdbeOneByteSerialTypeLen(t
);
2746 zHdr
+= sqlite3GetVarint32(zHdr
, &t
);
2748 offset64
+= sqlite3VdbeSerialTypeLen(t
);
2750 aOffset
[++i
] = (u32
)(offset64
& 0xffffffff);
2751 }while( (u32
)i
<=p2
&& zHdr
<zEndHdr
);
2753 /* The record is corrupt if any of the following are true:
2754 ** (1) the bytes of the header extend past the declared header size
2755 ** (2) the entire header was used but not all data was used
2756 ** (3) the end of the data extends beyond the end of the record.
2758 if( (zHdr
>=zEndHdr
&& (zHdr
>zEndHdr
|| offset64
!=pC
->payloadSize
))
2759 || (offset64
> pC
->payloadSize
)
2761 if( aOffset
[0]==0 ){
2765 if( pC
->aRow
==0 ) sqlite3VdbeMemRelease(&sMem
);
2766 goto op_column_corrupt
;
2771 pC
->iHdrOffset
= (u32
)(zHdr
- zData
);
2772 if( pC
->aRow
==0 ) sqlite3VdbeMemRelease(&sMem
);
2777 /* If after trying to extract new entries from the header, nHdrParsed is
2778 ** still not up to p2, that means that the record has fewer than p2
2779 ** columns. So the result will be either the default value or a NULL.
2781 if( pC
->nHdrParsed
<=p2
){
2782 if( pOp
->p4type
==P4_MEM
){
2783 sqlite3VdbeMemShallowCopy(pDest
, pOp
->p4
.pMem
, MEM_Static
);
2785 sqlite3VdbeMemSetNull(pDest
);
2793 /* Extract the content for the p2+1-th column. Control can only
2794 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
2797 assert( p2
<pC
->nHdrParsed
);
2798 assert( rc
==SQLITE_OK
);
2799 assert( sqlite3VdbeCheckMemInvariants(pDest
) );
2800 if( VdbeMemDynamic(pDest
) ){
2801 sqlite3VdbeMemSetNull(pDest
);
2803 assert( t
==pC
->aType
[p2
] );
2804 if( pC
->szRow
>=aOffset
[p2
+1] ){
2805 /* This is the common case where the desired content fits on the original
2806 ** page - where the content is not on an overflow page */
2807 zData
= pC
->aRow
+ aOffset
[p2
];
2809 sqlite3VdbeSerialGet(zData
, t
, pDest
);
2811 /* If the column value is a string, we need a persistent value, not
2812 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2813 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2815 static const u16 aFlag
[] = { MEM_Blob
, MEM_Str
|MEM_Term
};
2816 pDest
->n
= len
= (t
-12)/2;
2817 pDest
->enc
= encoding
;
2818 if( pDest
->szMalloc
< len
+2 ){
2819 pDest
->flags
= MEM_Null
;
2820 if( sqlite3VdbeMemGrow(pDest
, len
+2, 0) ) goto no_mem
;
2822 pDest
->z
= pDest
->zMalloc
;
2824 memcpy(pDest
->z
, zData
, len
);
2826 pDest
->z
[len
+1] = 0;
2827 pDest
->flags
= aFlag
[t
&1];
2830 pDest
->enc
= encoding
;
2831 /* This branch happens only when content is on overflow pages */
2832 if( ((pOp
->p5
& (OPFLAG_LENGTHARG
|OPFLAG_TYPEOFARG
))!=0
2833 && ((t
>=12 && (t
&1)==0) || (pOp
->p5
& OPFLAG_TYPEOFARG
)!=0))
2834 || (len
= sqlite3VdbeSerialTypeLen(t
))==0
2836 /* Content is irrelevant for
2837 ** 1. the typeof() function,
2838 ** 2. the length(X) function if X is a blob, and
2839 ** 3. if the content length is zero.
2840 ** So we might as well use bogus content rather than reading
2841 ** content from disk.
2843 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2844 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2845 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
2846 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
2847 ** and it begins with a bunch of zeros.
2849 sqlite3VdbeSerialGet((u8
*)sqlite3CtypeMap
, t
, pDest
);
2851 rc
= sqlite3VdbeMemFromBtree(pC
->uc
.pCursor
, aOffset
[p2
], len
, pDest
);
2852 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
2853 sqlite3VdbeSerialGet((const u8
*)pDest
->z
, t
, pDest
);
2854 pDest
->flags
&= ~MEM_Ephem
;
2859 UPDATE_MAX_BLOBSIZE(pDest
);
2860 REGISTER_TRACE(pOp
->p3
, pDest
);
2865 pOp
= &aOp
[aOp
[0].p3
-1];
2868 rc
= SQLITE_CORRUPT_BKPT
;
2869 goto abort_due_to_error
;
2873 /* Opcode: Affinity P1 P2 * P4 *
2874 ** Synopsis: affinity(r[P1@P2])
2876 ** Apply affinities to a range of P2 registers starting with P1.
2878 ** P4 is a string that is P2 characters long. The N-th character of the
2879 ** string indicates the column affinity that should be used for the N-th
2880 ** memory cell in the range.
2883 const char *zAffinity
; /* The affinity to be applied */
2885 zAffinity
= pOp
->p4
.z
;
2886 assert( zAffinity
!=0 );
2887 assert( pOp
->p2
>0 );
2888 assert( zAffinity
[pOp
->p2
]==0 );
2889 pIn1
= &aMem
[pOp
->p1
];
2890 while( 1 /*exit-by-break*/ ){
2891 assert( pIn1
<= &p
->aMem
[(p
->nMem
+1 - p
->nCursor
)] );
2892 assert( zAffinity
[0]==SQLITE_AFF_NONE
|| memIsValid(pIn1
) );
2893 applyAffinity(pIn1
, zAffinity
[0], encoding
);
2894 if( zAffinity
[0]==SQLITE_AFF_REAL
&& (pIn1
->flags
& MEM_Int
)!=0 ){
2895 /* When applying REAL affinity, if the result is still an MEM_Int
2896 ** that will fit in 6 bytes, then change the type to MEM_IntReal
2897 ** so that we keep the high-resolution integer value but know that
2898 ** the type really wants to be REAL. */
2899 testcase( pIn1
->u
.i
==140737488355328LL );
2900 testcase( pIn1
->u
.i
==140737488355327LL );
2901 testcase( pIn1
->u
.i
==-140737488355328LL );
2902 testcase( pIn1
->u
.i
==-140737488355329LL );
2903 if( pIn1
->u
.i
<=140737488355327LL && pIn1
->u
.i
>=-140737488355328LL ){
2904 pIn1
->flags
|= MEM_IntReal
;
2905 pIn1
->flags
&= ~MEM_Int
;
2907 pIn1
->u
.r
= (double)pIn1
->u
.i
;
2908 pIn1
->flags
|= MEM_Real
;
2909 pIn1
->flags
&= ~MEM_Int
;
2912 REGISTER_TRACE((int)(pIn1
-aMem
), pIn1
);
2914 if( zAffinity
[0]==0 ) break;
2920 /* Opcode: MakeRecord P1 P2 P3 P4 *
2921 ** Synopsis: r[P3]=mkrec(r[P1@P2])
2923 ** Convert P2 registers beginning with P1 into the [record format]
2924 ** use as a data record in a database table or as a key
2925 ** in an index. The OP_Column opcode can decode the record later.
2927 ** P4 may be a string that is P2 characters long. The N-th character of the
2928 ** string indicates the column affinity that should be used for the N-th
2929 ** field of the index key.
2931 ** The mapping from character to affinity is given by the SQLITE_AFF_
2932 ** macros defined in sqliteInt.h.
2934 ** If P4 is NULL then all index fields have the affinity BLOB.
2936 ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
2937 ** compile-time option is enabled:
2939 ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
2940 ** of the right-most table that can be null-trimmed.
2942 ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
2943 ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
2944 ** accept no-change records with serial_type 10. This value is
2945 ** only used inside an assert() and does not affect the end result.
2947 case OP_MakeRecord
: {
2948 Mem
*pRec
; /* The new record */
2949 u64 nData
; /* Number of bytes of data space */
2950 int nHdr
; /* Number of bytes of header space */
2951 i64 nByte
; /* Data space required for this record */
2952 i64 nZero
; /* Number of zero bytes at the end of the record */
2953 int nVarint
; /* Number of bytes in a varint */
2954 u32 serial_type
; /* Type field */
2955 Mem
*pData0
; /* First field to be combined into the record */
2956 Mem
*pLast
; /* Last field of the record */
2957 int nField
; /* Number of fields in the record */
2958 char *zAffinity
; /* The affinity string for the record */
2959 int file_format
; /* File format to use for encoding */
2960 u32 len
; /* Length of a field */
2961 u8
*zHdr
; /* Where to write next byte of the header */
2962 u8
*zPayload
; /* Where to write next byte of the payload */
2964 /* Assuming the record contains N fields, the record format looks
2967 ** ------------------------------------------------------------------------
2968 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2969 ** ------------------------------------------------------------------------
2971 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2974 ** Each type field is a varint representing the serial type of the
2975 ** corresponding data element (see sqlite3VdbeSerialType()). The
2976 ** hdr-size field is also a varint which is the offset from the beginning
2977 ** of the record to data0.
2979 nData
= 0; /* Number of bytes of data space */
2980 nHdr
= 0; /* Number of bytes of header space */
2981 nZero
= 0; /* Number of zero bytes at the end of the record */
2983 zAffinity
= pOp
->p4
.z
;
2984 assert( nField
>0 && pOp
->p2
>0 && pOp
->p2
+nField
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2985 pData0
= &aMem
[nField
];
2987 pLast
= &pData0
[nField
-1];
2988 file_format
= p
->minWriteFileFormat
;
2990 /* Identify the output register */
2991 assert( pOp
->p3
<pOp
->p1
|| pOp
->p3
>=pOp
->p1
+pOp
->p2
);
2992 pOut
= &aMem
[pOp
->p3
];
2993 memAboutToChange(p
, pOut
);
2995 /* Apply the requested affinity to all inputs
2997 assert( pData0
<=pLast
);
3001 applyAffinity(pRec
, zAffinity
[0], encoding
);
3002 if( zAffinity
[0]==SQLITE_AFF_REAL
&& (pRec
->flags
& MEM_Int
) ){
3003 pRec
->flags
|= MEM_IntReal
;
3004 pRec
->flags
&= ~(MEM_Int
);
3006 REGISTER_TRACE((int)(pRec
-aMem
), pRec
);
3009 assert( zAffinity
[0]==0 || pRec
<=pLast
);
3010 }while( zAffinity
[0] );
3013 #ifdef SQLITE_ENABLE_NULL_TRIM
3014 /* NULLs can be safely trimmed from the end of the record, as long as
3015 ** as the schema format is 2 or more and none of the omitted columns
3016 ** have a non-NULL default value. Also, the record must be left with
3017 ** at least one field. If P5>0 then it will be one more than the
3018 ** index of the right-most column with a non-NULL default value */
3020 while( (pLast
->flags
& MEM_Null
)!=0 && nField
>pOp
->p5
){
3027 /* Loop through the elements that will make up the record to figure
3028 ** out how much space is required for the new record. After this loop,
3029 ** the Mem.uTemp field of each term should hold the serial-type that will
3030 ** be used for that term in the generated record:
3032 ** Mem.uTemp value type
3033 ** --------------- ---------------
3035 ** 1 1-byte signed integer
3036 ** 2 2-byte signed integer
3037 ** 3 3-byte signed integer
3038 ** 4 4-byte signed integer
3039 ** 5 6-byte signed integer
3040 ** 6 8-byte signed integer
3042 ** 8 Integer constant 0
3043 ** 9 Integer constant 1
3044 ** 10,11 reserved for expansion
3045 ** N>=12 and even BLOB
3046 ** N>=13 and odd text
3048 ** The following additional values are computed:
3049 ** nHdr Number of bytes needed for the record header
3050 ** nData Number of bytes of data space needed for the record
3051 ** nZero Zero bytes at the end of the record
3055 assert( memIsValid(pRec
) );
3056 if( pRec
->flags
& MEM_Null
){
3057 if( pRec
->flags
& MEM_Zero
){
3058 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3059 ** table methods that never invoke sqlite3_result_xxxxx() while
3060 ** computing an unchanging column value in an UPDATE statement.
3061 ** Give such values a special internal-use-only serial-type of 10
3062 ** so that they can be passed through to xUpdate and have
3063 ** a true sqlite3_value_nochange(). */
3064 #ifndef SQLITE_ENABLE_NULL_TRIM
3065 assert( pOp
->p5
==OPFLAG_NOCHNG_MAGIC
|| CORRUPT_DB
);
3072 }else if( pRec
->flags
& (MEM_Int
|MEM_IntReal
) ){
3073 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3076 testcase( pRec
->flags
& MEM_Int
);
3077 testcase( pRec
->flags
& MEM_IntReal
);
3084 testcase( uu
==127 ); testcase( uu
==128 );
3085 testcase( uu
==32767 ); testcase( uu
==32768 );
3086 testcase( uu
==8388607 ); testcase( uu
==8388608 );
3087 testcase( uu
==2147483647 ); testcase( uu
==2147483648 );
3088 testcase( uu
==140737488355327LL ); testcase( uu
==140737488355328LL );
3090 if( (i
&1)==i
&& file_format
>=4 ){
3091 pRec
->uTemp
= 8+(u32
)uu
;
3096 }else if( uu
<=32767 ){
3099 }else if( uu
<=8388607 ){
3102 }else if( uu
<=2147483647 ){
3105 }else if( uu
<=140737488355327LL ){
3110 if( pRec
->flags
& MEM_IntReal
){
3111 /* If the value is IntReal and is going to take up 8 bytes to store
3112 ** as an integer, then we might as well make it an 8-byte floating
3114 pRec
->u
.r
= (double)pRec
->u
.i
;
3115 pRec
->flags
&= ~MEM_IntReal
;
3116 pRec
->flags
|= MEM_Real
;
3122 }else if( pRec
->flags
& MEM_Real
){
3127 assert( db
->mallocFailed
|| pRec
->flags
&(MEM_Str
|MEM_Blob
) );
3128 assert( pRec
->n
>=0 );
3130 serial_type
= (len
*2) + 12 + ((pRec
->flags
& MEM_Str
)!=0);
3131 if( pRec
->flags
& MEM_Zero
){
3132 serial_type
+= pRec
->u
.nZero
*2;
3134 if( sqlite3VdbeMemExpandBlob(pRec
) ) goto no_mem
;
3135 len
+= pRec
->u
.nZero
;
3137 nZero
+= pRec
->u
.nZero
;
3141 nHdr
+= sqlite3VarintLen(serial_type
);
3142 pRec
->uTemp
= serial_type
;
3144 if( pRec
==pData0
) break;
3148 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3149 ** which determines the total number of bytes in the header. The varint
3150 ** value is the size of the header in bytes including the size varint
3152 testcase( nHdr
==126 );
3153 testcase( nHdr
==127 );
3155 /* The common case */
3158 /* Rare case of a really large header */
3159 nVarint
= sqlite3VarintLen(nHdr
);
3161 if( nVarint
<sqlite3VarintLen(nHdr
) ) nHdr
++;
3165 /* Make sure the output register has a buffer large enough to store
3166 ** the new record. The output register (pOp->p3) is not allowed to
3167 ** be one of the input registers (because the following call to
3168 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
3170 if( nByte
+nZero
<=pOut
->szMalloc
){
3171 /* The output register is already large enough to hold the record.
3172 ** No error checks or buffer enlargement is required */
3173 pOut
->z
= pOut
->zMalloc
;
3175 /* Need to make sure that the output is not too big and then enlarge
3176 ** the output register to hold the full result */
3177 if( nByte
+nZero
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
3180 if( sqlite3VdbeMemClearAndResize(pOut
, (int)nByte
) ){
3184 pOut
->n
= (int)nByte
;
3185 pOut
->flags
= MEM_Blob
;
3187 pOut
->u
.nZero
= nZero
;
3188 pOut
->flags
|= MEM_Zero
;
3190 UPDATE_MAX_BLOBSIZE(pOut
);
3191 zHdr
= (u8
*)pOut
->z
;
3192 zPayload
= zHdr
+ nHdr
;
3194 /* Write the record */
3195 zHdr
+= putVarint32(zHdr
, nHdr
);
3196 assert( pData0
<=pLast
);
3199 serial_type
= pRec
->uTemp
;
3200 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3201 ** additional varints, one per column. */
3202 zHdr
+= putVarint32(zHdr
, serial_type
); /* serial type */
3203 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
3204 ** immediately follow the header. */
3205 zPayload
+= sqlite3VdbeSerialPut(zPayload
, pRec
, serial_type
); /* content */
3206 }while( (++pRec
)<=pLast
);
3207 assert( nHdr
==(int)(zHdr
- (u8
*)pOut
->z
) );
3208 assert( nByte
==(int)(zPayload
- (u8
*)pOut
->z
) );
3210 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
3211 REGISTER_TRACE(pOp
->p3
, pOut
);
3215 /* Opcode: Count P1 P2 p3 * *
3216 ** Synopsis: r[P2]=count()
3218 ** Store the number of entries (an integer value) in the table or index
3219 ** opened by cursor P1 in register P2.
3221 ** If P3==0, then an exact count is obtained, which involves visiting
3222 ** every btree page of the table. But if P3 is non-zero, an estimate
3223 ** is returned based on the current cursor position.
3225 case OP_Count
: { /* out2 */
3229 assert( p
->apCsr
[pOp
->p1
]->eCurType
==CURTYPE_BTREE
);
3230 pCrsr
= p
->apCsr
[pOp
->p1
]->uc
.pCursor
;
3233 nEntry
= sqlite3BtreeRowCountEst(pCrsr
);
3235 nEntry
= 0; /* Not needed. Only used to silence a warning. */
3236 rc
= sqlite3BtreeCount(db
, pCrsr
, &nEntry
);
3237 if( rc
) goto abort_due_to_error
;
3239 pOut
= out2Prerelease(p
, pOp
);
3241 goto check_for_interrupt
;
3244 /* Opcode: Savepoint P1 * * P4 *
3246 ** Open, release or rollback the savepoint named by parameter P4, depending
3247 ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3248 ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3249 ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
3251 case OP_Savepoint
: {
3252 int p1
; /* Value of P1 operand */
3253 char *zName
; /* Name of savepoint */
3256 Savepoint
*pSavepoint
;
3264 /* Assert that the p1 parameter is valid. Also that if there is no open
3265 ** transaction, then there cannot be any savepoints.
3267 assert( db
->pSavepoint
==0 || db
->autoCommit
==0 );
3268 assert( p1
==SAVEPOINT_BEGIN
||p1
==SAVEPOINT_RELEASE
||p1
==SAVEPOINT_ROLLBACK
);
3269 assert( db
->pSavepoint
|| db
->isTransactionSavepoint
==0 );
3270 assert( checkSavepointCount(db
) );
3271 assert( p
->bIsReader
);
3273 if( p1
==SAVEPOINT_BEGIN
){
3274 if( db
->nVdbeWrite
>0 ){
3275 /* A new savepoint cannot be created if there are active write
3276 ** statements (i.e. open read/write incremental blob handles).
3278 sqlite3VdbeError(p
, "cannot open savepoint - SQL statements in progress");
3281 nName
= sqlite3Strlen30(zName
);
3283 #ifndef SQLITE_OMIT_VIRTUALTABLE
3284 /* This call is Ok even if this savepoint is actually a transaction
3285 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3286 ** If this is a transaction savepoint being opened, it is guaranteed
3287 ** that the db->aVTrans[] array is empty. */
3288 assert( db
->autoCommit
==0 || db
->nVTrans
==0 );
3289 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_BEGIN
,
3290 db
->nStatement
+db
->nSavepoint
);
3291 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3294 /* Create a new savepoint structure. */
3295 pNew
= sqlite3DbMallocRawNN(db
, sizeof(Savepoint
)+nName
+1);
3297 pNew
->zName
= (char *)&pNew
[1];
3298 memcpy(pNew
->zName
, zName
, nName
+1);
3300 /* If there is no open transaction, then mark this as a special
3301 ** "transaction savepoint". */
3302 if( db
->autoCommit
){
3304 db
->isTransactionSavepoint
= 1;
3309 /* Link the new savepoint into the database handle's list. */
3310 pNew
->pNext
= db
->pSavepoint
;
3311 db
->pSavepoint
= pNew
;
3312 pNew
->nDeferredCons
= db
->nDeferredCons
;
3313 pNew
->nDeferredImmCons
= db
->nDeferredImmCons
;
3317 assert( p1
==SAVEPOINT_RELEASE
|| p1
==SAVEPOINT_ROLLBACK
);
3320 /* Find the named savepoint. If there is no such savepoint, then an
3321 ** an error is returned to the user. */
3323 pSavepoint
= db
->pSavepoint
;
3324 pSavepoint
&& sqlite3StrICmp(pSavepoint
->zName
, zName
);
3325 pSavepoint
= pSavepoint
->pNext
3330 sqlite3VdbeError(p
, "no such savepoint: %s", zName
);
3332 }else if( db
->nVdbeWrite
>0 && p1
==SAVEPOINT_RELEASE
){
3333 /* It is not possible to release (commit) a savepoint if there are
3334 ** active write statements.
3336 sqlite3VdbeError(p
, "cannot release savepoint - "
3337 "SQL statements in progress");
3341 /* Determine whether or not this is a transaction savepoint. If so,
3342 ** and this is a RELEASE command, then the current transaction
3345 int isTransaction
= pSavepoint
->pNext
==0 && db
->isTransactionSavepoint
;
3346 if( isTransaction
&& p1
==SAVEPOINT_RELEASE
){
3347 if( (rc
= sqlite3VdbeCheckFk(p
, 1))!=SQLITE_OK
){
3351 if( sqlite3VdbeHalt(p
)==SQLITE_BUSY
){
3352 p
->pc
= (int)(pOp
- aOp
);
3354 p
->rc
= rc
= SQLITE_BUSY
;
3361 db
->isTransactionSavepoint
= 0;
3365 iSavepoint
= db
->nSavepoint
- iSavepoint
- 1;
3366 if( p1
==SAVEPOINT_ROLLBACK
){
3367 isSchemaChange
= (db
->mDbFlags
& DBFLAG_SchemaChange
)!=0;
3368 for(ii
=0; ii
<db
->nDb
; ii
++){
3369 rc
= sqlite3BtreeTripAllCursors(db
->aDb
[ii
].pBt
,
3370 SQLITE_ABORT_ROLLBACK
,
3372 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3375 assert( p1
==SAVEPOINT_RELEASE
);
3378 for(ii
=0; ii
<db
->nDb
; ii
++){
3379 rc
= sqlite3BtreeSavepoint(db
->aDb
[ii
].pBt
, p1
, iSavepoint
);
3380 if( rc
!=SQLITE_OK
){
3381 goto abort_due_to_error
;
3384 if( isSchemaChange
){
3385 sqlite3ExpirePreparedStatements(db
, 0);
3386 sqlite3ResetAllSchemasOfConnection(db
);
3387 db
->mDbFlags
|= DBFLAG_SchemaChange
;
3390 if( rc
) goto abort_due_to_error
;
3392 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3393 ** savepoints nested inside of the savepoint being operated on. */
3394 while( db
->pSavepoint
!=pSavepoint
){
3395 pTmp
= db
->pSavepoint
;
3396 db
->pSavepoint
= pTmp
->pNext
;
3397 sqlite3DbFree(db
, pTmp
);
3401 /* If it is a RELEASE, then destroy the savepoint being operated on
3402 ** too. If it is a ROLLBACK TO, then set the number of deferred
3403 ** constraint violations present in the database to the value stored
3404 ** when the savepoint was created. */
3405 if( p1
==SAVEPOINT_RELEASE
){
3406 assert( pSavepoint
==db
->pSavepoint
);
3407 db
->pSavepoint
= pSavepoint
->pNext
;
3408 sqlite3DbFree(db
, pSavepoint
);
3409 if( !isTransaction
){
3413 assert( p1
==SAVEPOINT_ROLLBACK
);
3414 db
->nDeferredCons
= pSavepoint
->nDeferredCons
;
3415 db
->nDeferredImmCons
= pSavepoint
->nDeferredImmCons
;
3418 if( !isTransaction
|| p1
==SAVEPOINT_ROLLBACK
){
3419 rc
= sqlite3VtabSavepoint(db
, p1
, iSavepoint
);
3420 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3424 if( rc
) goto abort_due_to_error
;
3429 /* Opcode: AutoCommit P1 P2 * * *
3431 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
3432 ** back any currently active btree transactions. If there are any active
3433 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3434 ** there are active writing VMs or active VMs that use shared cache.
3436 ** This instruction causes the VM to halt.
3438 case OP_AutoCommit
: {
3439 int desiredAutoCommit
;
3442 desiredAutoCommit
= pOp
->p1
;
3443 iRollback
= pOp
->p2
;
3444 assert( desiredAutoCommit
==1 || desiredAutoCommit
==0 );
3445 assert( desiredAutoCommit
==1 || iRollback
==0 );
3446 assert( db
->nVdbeActive
>0 ); /* At least this one VM is active */
3447 assert( p
->bIsReader
);
3449 if( desiredAutoCommit
!=db
->autoCommit
){
3451 assert( desiredAutoCommit
==1 );
3452 sqlite3RollbackAll(db
, SQLITE_ABORT_ROLLBACK
);
3454 }else if( desiredAutoCommit
&& db
->nVdbeWrite
>0 ){
3455 /* If this instruction implements a COMMIT and other VMs are writing
3456 ** return an error indicating that the other VMs must complete first.
3458 sqlite3VdbeError(p
, "cannot commit transaction - "
3459 "SQL statements in progress");
3461 goto abort_due_to_error
;
3462 }else if( (rc
= sqlite3VdbeCheckFk(p
, 1))!=SQLITE_OK
){
3465 db
->autoCommit
= (u8
)desiredAutoCommit
;
3467 if( sqlite3VdbeHalt(p
)==SQLITE_BUSY
){
3468 p
->pc
= (int)(pOp
- aOp
);
3469 db
->autoCommit
= (u8
)(1-desiredAutoCommit
);
3470 p
->rc
= rc
= SQLITE_BUSY
;
3473 sqlite3CloseSavepoints(db
);
3474 if( p
->rc
==SQLITE_OK
){
3482 (!desiredAutoCommit
)?"cannot start a transaction within a transaction":(
3483 (iRollback
)?"cannot rollback - no transaction is active":
3484 "cannot commit - no transaction is active"));
3487 goto abort_due_to_error
;
3489 /*NOTREACHED*/ assert(0);
3492 /* Opcode: Transaction P1 P2 P3 P4 P5
3494 ** Begin a transaction on database P1 if a transaction is not already
3496 ** If P2 is non-zero, then a write-transaction is started, or if a
3497 ** read-transaction is already active, it is upgraded to a write-transaction.
3498 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3499 ** then an exclusive transaction is started.
3501 ** P1 is the index of the database file on which the transaction is
3502 ** started. Index 0 is the main database file and index 1 is the
3503 ** file used for temporary tables. Indices of 2 or more are used for
3504 ** attached databases.
3506 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3507 ** true (this flag is set if the Vdbe may modify more than one row and may
3508 ** throw an ABORT exception), a statement transaction may also be opened.
3509 ** More specifically, a statement transaction is opened iff the database
3510 ** connection is currently not in autocommit mode, or if there are other
3511 ** active statements. A statement transaction allows the changes made by this
3512 ** VDBE to be rolled back after an error without having to roll back the
3513 ** entire transaction. If no error is encountered, the statement transaction
3514 ** will automatically commit when the VDBE halts.
3516 ** If P5!=0 then this opcode also checks the schema cookie against P3
3517 ** and the schema generation counter against P4.
3518 ** The cookie changes its value whenever the database schema changes.
3519 ** This operation is used to detect when that the cookie has changed
3520 ** and that the current process needs to reread the schema. If the schema
3521 ** cookie in P3 differs from the schema cookie in the database header or
3522 ** if the schema generation counter in P4 differs from the current
3523 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
3524 ** halts. The sqlite3_step() wrapper function might then reprepare the
3525 ** statement and rerun it from the beginning.
3527 case OP_Transaction
: {
3531 assert( p
->bIsReader
);
3532 assert( p
->readOnly
==0 || pOp
->p2
==0 );
3533 assert( pOp
->p2
>=0 && pOp
->p2
<=2 );
3534 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
3535 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
3536 if( pOp
->p2
&& (db
->flags
& SQLITE_QueryOnly
)!=0 ){
3537 rc
= SQLITE_READONLY
;
3538 goto abort_due_to_error
;
3540 pBt
= db
->aDb
[pOp
->p1
].pBt
;
3543 rc
= sqlite3BtreeBeginTrans(pBt
, pOp
->p2
, &iMeta
);
3544 testcase( rc
==SQLITE_BUSY_SNAPSHOT
);
3545 testcase( rc
==SQLITE_BUSY_RECOVERY
);
3546 if( rc
!=SQLITE_OK
){
3547 if( (rc
&0xff)==SQLITE_BUSY
){
3548 p
->pc
= (int)(pOp
- aOp
);
3552 goto abort_due_to_error
;
3555 if( p
->usesStmtJournal
3557 && (db
->autoCommit
==0 || db
->nVdbeRead
>1)
3559 assert( sqlite3BtreeTxnState(pBt
)==SQLITE_TXN_WRITE
);
3560 if( p
->iStatement
==0 ){
3561 assert( db
->nStatement
>=0 && db
->nSavepoint
>=0 );
3563 p
->iStatement
= db
->nSavepoint
+ db
->nStatement
;
3566 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_BEGIN
, p
->iStatement
-1);
3567 if( rc
==SQLITE_OK
){
3568 rc
= sqlite3BtreeBeginStmt(pBt
, p
->iStatement
);
3571 /* Store the current value of the database handles deferred constraint
3572 ** counter. If the statement transaction needs to be rolled back,
3573 ** the value of this counter needs to be restored too. */
3574 p
->nStmtDefCons
= db
->nDeferredCons
;
3575 p
->nStmtDefImmCons
= db
->nDeferredImmCons
;
3578 assert( pOp
->p5
==0 || pOp
->p4type
==P4_INT32
);
3581 || db
->aDb
[pOp
->p1
].pSchema
->iGeneration
!=pOp
->p4
.i
)
3584 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3585 ** version is checked to ensure that the schema has not changed since the
3586 ** SQL statement was prepared.
3588 sqlite3DbFree(db
, p
->zErrMsg
);
3589 p
->zErrMsg
= sqlite3DbStrDup(db
, "database schema has changed");
3590 /* If the schema-cookie from the database file matches the cookie
3591 ** stored with the in-memory representation of the schema, do
3592 ** not reload the schema from the database file.
3594 ** If virtual-tables are in use, this is not just an optimization.
3595 ** Often, v-tables store their data in other SQLite tables, which
3596 ** are queried from within xNext() and other v-table methods using
3597 ** prepared queries. If such a query is out-of-date, we do not want to
3598 ** discard the database schema, as the user code implementing the
3599 ** v-table would have to be ready for the sqlite3_vtab structure itself
3600 ** to be invalidated whenever sqlite3_step() is called from within
3601 ** a v-table method.
3603 if( db
->aDb
[pOp
->p1
].pSchema
->schema_cookie
!=iMeta
){
3604 sqlite3ResetOneSchema(db
, pOp
->p1
);
3609 if( rc
) goto abort_due_to_error
;
3613 /* Opcode: ReadCookie P1 P2 P3 * *
3615 ** Read cookie number P3 from database P1 and write it into register P2.
3616 ** P3==1 is the schema version. P3==2 is the database format.
3617 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
3618 ** the main database file and P1==1 is the database file used to store
3619 ** temporary tables.
3621 ** There must be a read-lock on the database (either a transaction
3622 ** must be started or there must be an open cursor) before
3623 ** executing this instruction.
3625 case OP_ReadCookie
: { /* out2 */
3630 assert( p
->bIsReader
);
3633 assert( pOp
->p3
<SQLITE_N_BTREE_META
);
3634 assert( iDb
>=0 && iDb
<db
->nDb
);
3635 assert( db
->aDb
[iDb
].pBt
!=0 );
3636 assert( DbMaskTest(p
->btreeMask
, iDb
) );
3638 sqlite3BtreeGetMeta(db
->aDb
[iDb
].pBt
, iCookie
, (u32
*)&iMeta
);
3639 pOut
= out2Prerelease(p
, pOp
);
3644 /* Opcode: SetCookie P1 P2 P3 * P5
3646 ** Write the integer value P3 into cookie number P2 of database P1.
3647 ** P2==1 is the schema version. P2==2 is the database format.
3648 ** P2==3 is the recommended pager cache
3649 ** size, and so forth. P1==0 is the main database file and P1==1 is the
3650 ** database file used to store temporary tables.
3652 ** A transaction must be started before executing this opcode.
3654 ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
3655 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
3656 ** has P5 set to 1, so that the internal schema version will be different
3657 ** from the database schema version, resulting in a schema reset.
3659 case OP_SetCookie
: {
3662 sqlite3VdbeIncrWriteCounter(p
, 0);
3663 assert( pOp
->p2
<SQLITE_N_BTREE_META
);
3664 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
3665 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
3666 assert( p
->readOnly
==0 );
3667 pDb
= &db
->aDb
[pOp
->p1
];
3668 assert( pDb
->pBt
!=0 );
3669 assert( sqlite3SchemaMutexHeld(db
, pOp
->p1
, 0) );
3670 /* See note about index shifting on OP_ReadCookie */
3671 rc
= sqlite3BtreeUpdateMeta(pDb
->pBt
, pOp
->p2
, pOp
->p3
);
3672 if( pOp
->p2
==BTREE_SCHEMA_VERSION
){
3673 /* When the schema cookie changes, record the new cookie internally */
3674 pDb
->pSchema
->schema_cookie
= pOp
->p3
- pOp
->p5
;
3675 db
->mDbFlags
|= DBFLAG_SchemaChange
;
3676 }else if( pOp
->p2
==BTREE_FILE_FORMAT
){
3677 /* Record changes in the file format */
3678 pDb
->pSchema
->file_format
= pOp
->p3
;
3681 /* Invalidate all prepared statements whenever the TEMP database
3682 ** schema is changed. Ticket #1644 */
3683 sqlite3ExpirePreparedStatements(db
, 0);
3686 if( rc
) goto abort_due_to_error
;
3690 /* Opcode: OpenRead P1 P2 P3 P4 P5
3691 ** Synopsis: root=P2 iDb=P3
3693 ** Open a read-only cursor for the database table whose root page is
3694 ** P2 in a database file. The database file is determined by P3.
3695 ** P3==0 means the main database, P3==1 means the database used for
3696 ** temporary tables, and P3>1 means used the corresponding attached
3697 ** database. Give the new cursor an identifier of P1. The P1
3698 ** values need not be contiguous but all P1 values should be small integers.
3699 ** It is an error for P1 to be negative.
3703 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3704 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3705 ** of OP_SeekLE/OP_IdxLT)
3708 ** The P4 value may be either an integer (P4_INT32) or a pointer to
3709 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3710 ** object, then table being opened must be an [index b-tree] where the
3711 ** KeyInfo object defines the content and collating
3712 ** sequence of that index b-tree. Otherwise, if P4 is an integer
3713 ** value, then the table being opened must be a [table b-tree] with a
3714 ** number of columns no less than the value of P4.
3716 ** See also: OpenWrite, ReopenIdx
3718 /* Opcode: ReopenIdx P1 P2 P3 P4 P5
3719 ** Synopsis: root=P2 iDb=P3
3721 ** The ReopenIdx opcode works like OP_OpenRead except that it first
3722 ** checks to see if the cursor on P1 is already open on the same
3723 ** b-tree and if it is this opcode becomes a no-op. In other words,
3724 ** if the cursor is already open, do not reopen it.
3726 ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3727 ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3728 ** be the same as every other ReopenIdx or OpenRead for the same cursor
3733 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3734 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3735 ** of OP_SeekLE/OP_IdxLT)
3738 ** See also: OP_OpenRead, OP_OpenWrite
3740 /* Opcode: OpenWrite P1 P2 P3 P4 P5
3741 ** Synopsis: root=P2 iDb=P3
3743 ** Open a read/write cursor named P1 on the table or index whose root
3744 ** page is P2 (or whose root page is held in register P2 if the
3745 ** OPFLAG_P2ISREG bit is set in P5 - see below).
3747 ** The P4 value may be either an integer (P4_INT32) or a pointer to
3748 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3749 ** object, then table being opened must be an [index b-tree] where the
3750 ** KeyInfo object defines the content and collating
3751 ** sequence of that index b-tree. Otherwise, if P4 is an integer
3752 ** value, then the table being opened must be a [table b-tree] with a
3753 ** number of columns no less than the value of P4.
3757 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3758 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3759 ** of OP_SeekLE/OP_IdxLT)
3760 ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3761 ** and subsequently delete entries in an index btree. This is a
3762 ** hint to the storage engine that the storage engine is allowed to
3763 ** ignore. The hint is not used by the official SQLite b*tree storage
3764 ** engine, but is used by COMDB2.
3765 ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3766 ** as the root page, not the value of P2 itself.
3769 ** This instruction works like OpenRead except that it opens the cursor
3770 ** in read/write mode.
3772 ** See also: OP_OpenRead, OP_ReopenIdx
3774 case OP_ReopenIdx
: {
3784 assert( pOp
->p5
==0 || pOp
->p5
==OPFLAG_SEEKEQ
);
3785 assert( pOp
->p4type
==P4_KEYINFO
);
3786 pCur
= p
->apCsr
[pOp
->p1
];
3787 if( pCur
&& pCur
->pgnoRoot
==(u32
)pOp
->p2
){
3788 assert( pCur
->iDb
==pOp
->p3
); /* Guaranteed by the code generator */
3789 goto open_cursor_set_hints
;
3791 /* If the cursor is not currently open or is open on a different
3792 ** index, then fall through into OP_OpenRead to force a reopen */
3796 assert( pOp
->opcode
==OP_OpenWrite
|| pOp
->p5
==0 || pOp
->p5
==OPFLAG_SEEKEQ
);
3797 assert( p
->bIsReader
);
3798 assert( pOp
->opcode
==OP_OpenRead
|| pOp
->opcode
==OP_ReopenIdx
3799 || p
->readOnly
==0 );
3801 if( p
->expired
==1 ){
3802 rc
= SQLITE_ABORT_ROLLBACK
;
3803 goto abort_due_to_error
;
3810 assert( iDb
>=0 && iDb
<db
->nDb
);
3811 assert( DbMaskTest(p
->btreeMask
, iDb
) );
3812 pDb
= &db
->aDb
[iDb
];
3815 if( pOp
->opcode
==OP_OpenWrite
){
3816 assert( OPFLAG_FORDELETE
==BTREE_FORDELETE
);
3817 wrFlag
= BTREE_WRCSR
| (pOp
->p5
& OPFLAG_FORDELETE
);
3818 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
3819 if( pDb
->pSchema
->file_format
< p
->minWriteFileFormat
){
3820 p
->minWriteFileFormat
= pDb
->pSchema
->file_format
;
3825 if( pOp
->p5
& OPFLAG_P2ISREG
){
3827 assert( p2
<=(u32
)(p
->nMem
+1 - p
->nCursor
) );
3828 assert( pOp
->opcode
==OP_OpenWrite
);
3830 assert( memIsValid(pIn2
) );
3831 assert( (pIn2
->flags
& MEM_Int
)!=0 );
3832 sqlite3VdbeMemIntegerify(pIn2
);
3833 p2
= (int)pIn2
->u
.i
;
3834 /* The p2 value always comes from a prior OP_CreateBtree opcode and
3835 ** that opcode will always set the p2 value to 2 or more or else fail.
3836 ** If there were a failure, the prepared statement would have halted
3837 ** before reaching this instruction. */
3840 if( pOp
->p4type
==P4_KEYINFO
){
3841 pKeyInfo
= pOp
->p4
.pKeyInfo
;
3842 assert( pKeyInfo
->enc
==ENC(db
) );
3843 assert( pKeyInfo
->db
==db
);
3844 nField
= pKeyInfo
->nAllField
;
3845 }else if( pOp
->p4type
==P4_INT32
){
3848 assert( pOp
->p1
>=0 );
3849 assert( nField
>=0 );
3850 testcase( nField
==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
3851 pCur
= allocateCursor(p
, pOp
->p1
, nField
, iDb
, CURTYPE_BTREE
);
3852 if( pCur
==0 ) goto no_mem
;
3854 pCur
->isOrdered
= 1;
3855 pCur
->pgnoRoot
= p2
;
3857 pCur
->wrFlag
= wrFlag
;
3859 rc
= sqlite3BtreeCursor(pX
, p2
, wrFlag
, pKeyInfo
, pCur
->uc
.pCursor
);
3860 pCur
->pKeyInfo
= pKeyInfo
;
3861 /* Set the VdbeCursor.isTable variable. Previous versions of
3862 ** SQLite used to check if the root-page flags were sane at this point
3863 ** and report database corruption if they were not, but this check has
3864 ** since moved into the btree layer. */
3865 pCur
->isTable
= pOp
->p4type
!=P4_KEYINFO
;
3867 open_cursor_set_hints
:
3868 assert( OPFLAG_BULKCSR
==BTREE_BULKLOAD
);
3869 assert( OPFLAG_SEEKEQ
==BTREE_SEEK_EQ
);
3870 testcase( pOp
->p5
& OPFLAG_BULKCSR
);
3871 testcase( pOp
->p2
& OPFLAG_SEEKEQ
);
3872 sqlite3BtreeCursorHintFlags(pCur
->uc
.pCursor
,
3873 (pOp
->p5
& (OPFLAG_BULKCSR
|OPFLAG_SEEKEQ
)));
3874 if( rc
) goto abort_due_to_error
;
3878 /* Opcode: OpenDup P1 P2 * * *
3880 ** Open a new cursor P1 that points to the same ephemeral table as
3881 ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3882 ** opcode. Only ephemeral cursors may be duplicated.
3884 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
3887 VdbeCursor
*pOrig
; /* The original cursor to be duplicated */
3888 VdbeCursor
*pCx
; /* The new cursor */
3890 pOrig
= p
->apCsr
[pOp
->p2
];
3892 assert( pOrig
->isEphemeral
); /* Only ephemeral cursors can be duplicated */
3894 pCx
= allocateCursor(p
, pOp
->p1
, pOrig
->nField
, -1, CURTYPE_BTREE
);
3895 if( pCx
==0 ) goto no_mem
;
3897 pCx
->isEphemeral
= 1;
3898 pCx
->pKeyInfo
= pOrig
->pKeyInfo
;
3899 pCx
->isTable
= pOrig
->isTable
;
3900 pCx
->pgnoRoot
= pOrig
->pgnoRoot
;
3901 pCx
->isOrdered
= pOrig
->isOrdered
;
3902 pCx
->pBtx
= pOrig
->pBtx
;
3903 pCx
->hasBeenDuped
= 1;
3904 pOrig
->hasBeenDuped
= 1;
3905 rc
= sqlite3BtreeCursor(pCx
->pBtx
, pCx
->pgnoRoot
, BTREE_WRCSR
,
3906 pCx
->pKeyInfo
, pCx
->uc
.pCursor
);
3907 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3908 ** opened for a database. Since there is already an open cursor when this
3909 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3910 assert( rc
==SQLITE_OK
);
3915 /* Opcode: OpenEphemeral P1 P2 P3 P4 P5
3916 ** Synopsis: nColumn=P2
3918 ** Open a new cursor P1 to a transient table.
3919 ** The cursor is always opened read/write even if
3920 ** the main database is read-only. The ephemeral
3921 ** table is deleted automatically when the cursor is closed.
3923 ** If the cursor P1 is already opened on an ephemeral table, the table
3924 ** is cleared (all content is erased).
3926 ** P2 is the number of columns in the ephemeral table.
3927 ** The cursor points to a BTree table if P4==0 and to a BTree index
3928 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
3929 ** that defines the format of keys in the index.
3931 ** The P5 parameter can be a mask of the BTREE_* flags defined
3932 ** in btree.h. These flags control aspects of the operation of
3933 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3934 ** added automatically.
3936 ** If P3 is positive, then reg[P3] is modified slightly so that it
3937 ** can be used as zero-length data for OP_Insert. This is an optimization
3938 ** that avoids an extra OP_Blob opcode to initialize that register.
3940 /* Opcode: OpenAutoindex P1 P2 * P4 *
3941 ** Synopsis: nColumn=P2
3943 ** This opcode works the same as OP_OpenEphemeral. It has a
3944 ** different name to distinguish its use. Tables created using
3945 ** by this opcode will be used for automatically created transient
3946 ** indices in joins.
3948 case OP_OpenAutoindex
:
3949 case OP_OpenEphemeral
: {
3953 static const int vfsFlags
=
3954 SQLITE_OPEN_READWRITE
|
3955 SQLITE_OPEN_CREATE
|
3956 SQLITE_OPEN_EXCLUSIVE
|
3957 SQLITE_OPEN_DELETEONCLOSE
|
3958 SQLITE_OPEN_TRANSIENT_DB
;
3959 assert( pOp
->p1
>=0 );
3960 assert( pOp
->p2
>=0 );
3962 /* Make register reg[P3] into a value that can be used as the data
3963 ** form sqlite3BtreeInsert() where the length of the data is zero. */
3964 assert( pOp
->p2
==0 ); /* Only used when number of columns is zero */
3965 assert( pOp
->opcode
==OP_OpenEphemeral
);
3966 assert( aMem
[pOp
->p3
].flags
& MEM_Null
);
3967 aMem
[pOp
->p3
].n
= 0;
3968 aMem
[pOp
->p3
].z
= "";
3970 pCx
= p
->apCsr
[pOp
->p1
];
3971 if( pCx
&& !pCx
->hasBeenDuped
){
3972 /* If the ephermeral table is already open and has no duplicates from
3973 ** OP_OpenDup, then erase all existing content so that the table is
3974 ** empty again, rather than creating a new table. */
3975 assert( pCx
->isEphemeral
);
3977 pCx
->cacheStatus
= CACHE_STALE
;
3978 rc
= sqlite3BtreeClearTable(pCx
->pBtx
, pCx
->pgnoRoot
, 0);
3980 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p2
, -1, CURTYPE_BTREE
);
3981 if( pCx
==0 ) goto no_mem
;
3982 pCx
->isEphemeral
= 1;
3983 rc
= sqlite3BtreeOpen(db
->pVfs
, 0, db
, &pCx
->pBtx
,
3984 BTREE_OMIT_JOURNAL
| BTREE_SINGLE
| pOp
->p5
,
3986 if( rc
==SQLITE_OK
){
3987 rc
= sqlite3BtreeBeginTrans(pCx
->pBtx
, 1, 0);
3988 if( rc
==SQLITE_OK
){
3989 /* If a transient index is required, create it by calling
3990 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3991 ** opening it. If a transient table is required, just use the
3992 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3994 if( (pCx
->pKeyInfo
= pKeyInfo
= pOp
->p4
.pKeyInfo
)!=0 ){
3995 assert( pOp
->p4type
==P4_KEYINFO
);
3996 rc
= sqlite3BtreeCreateTable(pCx
->pBtx
, &pCx
->pgnoRoot
,
3997 BTREE_BLOBKEY
| pOp
->p5
);
3998 if( rc
==SQLITE_OK
){
3999 assert( pCx
->pgnoRoot
==SCHEMA_ROOT
+1 );
4000 assert( pKeyInfo
->db
==db
);
4001 assert( pKeyInfo
->enc
==ENC(db
) );
4002 rc
= sqlite3BtreeCursor(pCx
->pBtx
, pCx
->pgnoRoot
, BTREE_WRCSR
,
4003 pKeyInfo
, pCx
->uc
.pCursor
);
4007 pCx
->pgnoRoot
= SCHEMA_ROOT
;
4008 rc
= sqlite3BtreeCursor(pCx
->pBtx
, SCHEMA_ROOT
, BTREE_WRCSR
,
4009 0, pCx
->uc
.pCursor
);
4013 pCx
->isOrdered
= (pOp
->p5
!=BTREE_UNORDERED
);
4015 sqlite3BtreeClose(pCx
->pBtx
);
4019 if( rc
) goto abort_due_to_error
;
4024 /* Opcode: SorterOpen P1 P2 P3 P4 *
4026 ** This opcode works like OP_OpenEphemeral except that it opens
4027 ** a transient index that is specifically designed to sort large
4028 ** tables using an external merge-sort algorithm.
4030 ** If argument P3 is non-zero, then it indicates that the sorter may
4031 ** assume that a stable sort considering the first P3 fields of each
4032 ** key is sufficient to produce the required results.
4034 case OP_SorterOpen
: {
4037 assert( pOp
->p1
>=0 );
4038 assert( pOp
->p2
>=0 );
4039 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p2
, -1, CURTYPE_SORTER
);
4040 if( pCx
==0 ) goto no_mem
;
4041 pCx
->pKeyInfo
= pOp
->p4
.pKeyInfo
;
4042 assert( pCx
->pKeyInfo
->db
==db
);
4043 assert( pCx
->pKeyInfo
->enc
==ENC(db
) );
4044 rc
= sqlite3VdbeSorterInit(db
, pOp
->p3
, pCx
);
4045 if( rc
) goto abort_due_to_error
;
4049 /* Opcode: SequenceTest P1 P2 * * *
4050 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4052 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4053 ** to P2. Regardless of whether or not the jump is taken, increment the
4054 ** the sequence value.
4056 case OP_SequenceTest
: {
4058 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4059 pC
= p
->apCsr
[pOp
->p1
];
4060 assert( isSorter(pC
) );
4061 if( (pC
->seqCount
++)==0 ){
4067 /* Opcode: OpenPseudo P1 P2 P3 * *
4068 ** Synopsis: P3 columns in r[P2]
4070 ** Open a new cursor that points to a fake table that contains a single
4071 ** row of data. The content of that one row is the content of memory
4072 ** register P2. In other words, cursor P1 becomes an alias for the
4073 ** MEM_Blob content contained in register P2.
4075 ** A pseudo-table created by this opcode is used to hold a single
4076 ** row output from the sorter so that the row can be decomposed into
4077 ** individual columns using the OP_Column opcode. The OP_Column opcode
4078 ** is the only cursor opcode that works with a pseudo-table.
4080 ** P3 is the number of fields in the records that will be stored by
4081 ** the pseudo-table.
4083 case OP_OpenPseudo
: {
4086 assert( pOp
->p1
>=0 );
4087 assert( pOp
->p3
>=0 );
4088 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p3
, -1, CURTYPE_PSEUDO
);
4089 if( pCx
==0 ) goto no_mem
;
4091 pCx
->seekResult
= pOp
->p2
;
4093 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4094 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4095 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4096 ** which is a performance optimization */
4097 pCx
->uc
.pCursor
= sqlite3BtreeFakeValidCursor();
4098 assert( pOp
->p5
==0 );
4102 /* Opcode: Close P1 * * * *
4104 ** Close a cursor previously opened as P1. If P1 is not
4105 ** currently open, this instruction is a no-op.
4108 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4109 sqlite3VdbeFreeCursor(p
, p
->apCsr
[pOp
->p1
]);
4110 p
->apCsr
[pOp
->p1
] = 0;
4114 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4115 /* Opcode: ColumnsUsed P1 * * P4 *
4117 ** This opcode (which only exists if SQLite was compiled with
4118 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4119 ** table or index for cursor P1 are used. P4 is a 64-bit integer
4120 ** (P4_INT64) in which the first 63 bits are one for each of the
4121 ** first 63 columns of the table or index that are actually used
4122 ** by the cursor. The high-order bit is set if any column after
4123 ** the 64th is used.
4125 case OP_ColumnsUsed
: {
4127 pC
= p
->apCsr
[pOp
->p1
];
4128 assert( pC
->eCurType
==CURTYPE_BTREE
);
4129 pC
->maskUsed
= *(u64
*)pOp
->p4
.pI64
;
4134 /* Opcode: SeekGE P1 P2 P3 P4 *
4135 ** Synopsis: key=r[P3@P4]
4137 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4138 ** use the value in register P3 as the key. If cursor P1 refers
4139 ** to an SQL index, then P3 is the first in an array of P4 registers
4140 ** that are used as an unpacked index key.
4142 ** Reposition cursor P1 so that it points to the smallest entry that
4143 ** is greater than or equal to the key value. If there are no records
4144 ** greater than or equal to the key and P2 is not zero, then jump to P2.
4146 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4147 ** opcode will either land on a record that exactly matches the key, or
4148 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4149 ** this opcode must be followed by an IdxLE opcode with the same arguments.
4150 ** The IdxGT opcode will be skipped if this opcode succeeds, but the
4151 ** IdxGT opcode will be used on subsequent loop iterations. The
4152 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4153 ** is an equality search.
4155 ** This opcode leaves the cursor configured to move in forward order,
4156 ** from the beginning toward the end. In other words, the cursor is
4157 ** configured to use Next, not Prev.
4159 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
4161 /* Opcode: SeekGT P1 P2 P3 P4 *
4162 ** Synopsis: key=r[P3@P4]
4164 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4165 ** use the value in register P3 as a key. If cursor P1 refers
4166 ** to an SQL index, then P3 is the first in an array of P4 registers
4167 ** that are used as an unpacked index key.
4169 ** Reposition cursor P1 so that it points to the smallest entry that
4170 ** is greater than the key value. If there are no records greater than
4171 ** the key and P2 is not zero, then jump to P2.
4173 ** This opcode leaves the cursor configured to move in forward order,
4174 ** from the beginning toward the end. In other words, the cursor is
4175 ** configured to use Next, not Prev.
4177 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
4179 /* Opcode: SeekLT P1 P2 P3 P4 *
4180 ** Synopsis: key=r[P3@P4]
4182 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4183 ** use the value in register P3 as a key. If cursor P1 refers
4184 ** to an SQL index, then P3 is the first in an array of P4 registers
4185 ** that are used as an unpacked index key.
4187 ** Reposition cursor P1 so that it points to the largest entry that
4188 ** is less than the key value. If there are no records less than
4189 ** the key and P2 is not zero, then jump to P2.
4191 ** This opcode leaves the cursor configured to move in reverse order,
4192 ** from the end toward the beginning. In other words, the cursor is
4193 ** configured to use Prev, not Next.
4195 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
4197 /* Opcode: SeekLE P1 P2 P3 P4 *
4198 ** Synopsis: key=r[P3@P4]
4200 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4201 ** use the value in register P3 as a key. If cursor P1 refers
4202 ** to an SQL index, then P3 is the first in an array of P4 registers
4203 ** that are used as an unpacked index key.
4205 ** Reposition cursor P1 so that it points to the largest entry that
4206 ** is less than or equal to the key value. If there are no records
4207 ** less than or equal to the key and P2 is not zero, then jump to P2.
4209 ** This opcode leaves the cursor configured to move in reverse order,
4210 ** from the end toward the beginning. In other words, the cursor is
4211 ** configured to use Prev, not Next.
4213 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4214 ** opcode will either land on a record that exactly matches the key, or
4215 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4216 ** this opcode must be followed by an IdxLE opcode with the same arguments.
4217 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
4218 ** IdxGE opcode will be used on subsequent loop iterations. The
4219 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4220 ** is an equality search.
4222 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
4224 case OP_SeekLT
: /* jump, in3, group */
4225 case OP_SeekLE
: /* jump, in3, group */
4226 case OP_SeekGE
: /* jump, in3, group */
4227 case OP_SeekGT
: { /* jump, in3, group */
4228 int res
; /* Comparison result */
4229 int oc
; /* Opcode */
4230 VdbeCursor
*pC
; /* The cursor to seek */
4231 UnpackedRecord r
; /* The key to seek for */
4232 int nField
; /* Number of columns or fields in the key */
4233 i64 iKey
; /* The rowid we are to seek to */
4234 int eqOnly
; /* Only interested in == results */
4236 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4237 assert( pOp
->p2
!=0 );
4238 pC
= p
->apCsr
[pOp
->p1
];
4240 assert( pC
->eCurType
==CURTYPE_BTREE
);
4241 assert( OP_SeekLE
== OP_SeekLT
+1 );
4242 assert( OP_SeekGE
== OP_SeekLT
+2 );
4243 assert( OP_SeekGT
== OP_SeekLT
+3 );
4244 assert( pC
->isOrdered
);
4245 assert( pC
->uc
.pCursor
!=0 );
4250 pC
->seekOp
= pOp
->opcode
;
4253 pC
->deferredMoveto
= 0;
4254 pC
->cacheStatus
= CACHE_STALE
;
4256 u16 flags3
, newType
;
4257 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
4258 assert( sqlite3BtreeCursorHasHint(pC
->uc
.pCursor
, BTREE_SEEK_EQ
)==0
4261 /* The input value in P3 might be of any type: integer, real, string,
4262 ** blob, or NULL. But it needs to be an integer before we can do
4263 ** the seek, so convert it. */
4264 pIn3
= &aMem
[pOp
->p3
];
4265 flags3
= pIn3
->flags
;
4266 if( (flags3
& (MEM_Int
|MEM_Real
|MEM_IntReal
|MEM_Str
))==MEM_Str
){
4267 applyNumericAffinity(pIn3
, 0);
4269 iKey
= sqlite3VdbeIntValue(pIn3
); /* Get the integer key value */
4270 newType
= pIn3
->flags
; /* Record the type after applying numeric affinity */
4271 pIn3
->flags
= flags3
; /* But convert the type back to its original */
4273 /* If the P3 value could not be converted into an integer without
4274 ** loss of information, then special processing is required... */
4275 if( (newType
& (MEM_Int
|MEM_IntReal
))==0 ){
4276 if( (newType
& MEM_Real
)==0 ){
4277 if( (newType
& MEM_Null
) || oc
>=OP_SeekGE
){
4278 VdbeBranchTaken(1,2);
4281 rc
= sqlite3BtreeLast(pC
->uc
.pCursor
, &res
);
4282 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
4283 goto seek_not_found
;
4287 /* If the approximation iKey is larger than the actual real search
4288 ** term, substitute >= for > and < for <=. e.g. if the search term
4289 ** is 4.9 and the integer approximation 5:
4291 ** (x > 4.9) -> (x >= 5)
4292 ** (x <= 4.9) -> (x < 5)
4294 if( pIn3
->u
.r
<(double)iKey
){
4295 assert( OP_SeekGE
==(OP_SeekGT
-1) );
4296 assert( OP_SeekLT
==(OP_SeekLE
-1) );
4297 assert( (OP_SeekLE
& 0x0001)==(OP_SeekGT
& 0x0001) );
4298 if( (oc
& 0x0001)==(OP_SeekGT
& 0x0001) ) oc
--;
4301 /* If the approximation iKey is smaller than the actual real search
4302 ** term, substitute <= for < and > for >=. */
4303 else if( pIn3
->u
.r
>(double)iKey
){
4304 assert( OP_SeekLE
==(OP_SeekLT
+1) );
4305 assert( OP_SeekGT
==(OP_SeekGE
+1) );
4306 assert( (OP_SeekLT
& 0x0001)==(OP_SeekGE
& 0x0001) );
4307 if( (oc
& 0x0001)==(OP_SeekLT
& 0x0001) ) oc
++;
4310 rc
= sqlite3BtreeMovetoUnpacked(pC
->uc
.pCursor
, 0, (u64
)iKey
, 0, &res
);
4311 pC
->movetoTarget
= iKey
; /* Used by OP_Delete */
4312 if( rc
!=SQLITE_OK
){
4313 goto abort_due_to_error
;
4316 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4317 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4318 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4319 ** with the same key.
4321 if( sqlite3BtreeCursorHasHint(pC
->uc
.pCursor
, BTREE_SEEK_EQ
) ){
4323 assert( pOp
->opcode
==OP_SeekGE
|| pOp
->opcode
==OP_SeekLE
);
4324 assert( pOp
[1].opcode
==OP_IdxLT
|| pOp
[1].opcode
==OP_IdxGT
);
4325 assert( pOp
->opcode
==OP_SeekGE
|| pOp
[1].opcode
==OP_IdxLT
);
4326 assert( pOp
->opcode
==OP_SeekLE
|| pOp
[1].opcode
==OP_IdxGT
);
4327 assert( pOp
[1].p1
==pOp
[0].p1
);
4328 assert( pOp
[1].p2
==pOp
[0].p2
);
4329 assert( pOp
[1].p3
==pOp
[0].p3
);
4330 assert( pOp
[1].p4
.i
==pOp
[0].p4
.i
);
4334 assert( pOp
->p4type
==P4_INT32
);
4336 r
.pKeyInfo
= pC
->pKeyInfo
;
4337 r
.nField
= (u16
)nField
;
4339 /* The next line of code computes as follows, only faster:
4340 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
4341 ** r.default_rc = -1;
4343 ** r.default_rc = +1;
4346 r
.default_rc
= ((1 & (oc
- OP_SeekLT
)) ? -1 : +1);
4347 assert( oc
!=OP_SeekGT
|| r
.default_rc
==-1 );
4348 assert( oc
!=OP_SeekLE
|| r
.default_rc
==-1 );
4349 assert( oc
!=OP_SeekGE
|| r
.default_rc
==+1 );
4350 assert( oc
!=OP_SeekLT
|| r
.default_rc
==+1 );
4352 r
.aMem
= &aMem
[pOp
->p3
];
4354 { int i
; for(i
=0; i
<r
.nField
; i
++) assert( memIsValid(&r
.aMem
[i
]) ); }
4357 rc
= sqlite3BtreeMovetoUnpacked(pC
->uc
.pCursor
, &r
, 0, 0, &res
);
4358 if( rc
!=SQLITE_OK
){
4359 goto abort_due_to_error
;
4361 if( eqOnly
&& r
.eqSeen
==0 ){
4363 goto seek_not_found
;
4367 sqlite3_search_count
++;
4369 if( oc
>=OP_SeekGE
){ assert( oc
==OP_SeekGE
|| oc
==OP_SeekGT
);
4370 if( res
<0 || (res
==0 && oc
==OP_SeekGT
) ){
4372 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, 0);
4373 if( rc
!=SQLITE_OK
){
4374 if( rc
==SQLITE_DONE
){
4378 goto abort_due_to_error
;
4385 assert( oc
==OP_SeekLT
|| oc
==OP_SeekLE
);
4386 if( res
>0 || (res
==0 && oc
==OP_SeekLT
) ){
4388 rc
= sqlite3BtreePrevious(pC
->uc
.pCursor
, 0);
4389 if( rc
!=SQLITE_OK
){
4390 if( rc
==SQLITE_DONE
){
4394 goto abort_due_to_error
;
4398 /* res might be negative because the table is empty. Check to
4399 ** see if this is the case.
4401 res
= sqlite3BtreeEof(pC
->uc
.pCursor
);
4405 assert( pOp
->p2
>0 );
4406 VdbeBranchTaken(res
!=0,2);
4410 assert( pOp
[1].opcode
==OP_IdxLT
|| pOp
[1].opcode
==OP_IdxGT
);
4411 pOp
++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
4417 /* Opcode: SeekScan P1 P2 * * *
4418 ** Synopsis: Scan-ahead up to P1 rows
4420 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this
4421 ** opcode must be immediately followed by OP_SeekGE. This constraint is
4422 ** checked by assert() statements.
4424 ** This opcode uses the P1 through P4 operands of the subsequent
4425 ** OP_SeekGE. In the text that follows, the operands of the subsequent
4426 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
4427 ** the P1 and P2 operands of this opcode are also used, and are called
4428 ** This.P1 and This.P2.
4430 ** This opcode helps to optimize IN operators on a multi-column index
4431 ** where the IN operator is on the later terms of the index by avoiding
4432 ** unnecessary seeks on the btree, substituting steps to the next row
4433 ** of the b-tree instead. A correct answer is obtained if this opcode
4434 ** is omitted or is a no-op.
4436 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4437 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
4438 ** to. Call this SeekGE.P4/P5 row the "target".
4440 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4441 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
4443 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4444 ** might be the target row, or it might be near and slightly before the
4445 ** target row. This opcode attempts to position the cursor on the target
4446 ** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
4447 ** between 0 and This.P1 times.
4449 ** There are three possible outcomes from this opcode:<ol>
4451 ** <li> If after This.P1 steps, the cursor is still pointing to a place that
4452 ** is earlier in the btree than the target row, then fall through
4453 ** into the subsquence OP_SeekGE opcode.
4455 ** <li> If the cursor is successfully moved to the target row by 0 or more
4456 ** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
4457 ** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
4459 ** <li> If the cursor ends up past the target row (indicating the the target
4460 ** row does not exist in the btree) then jump to SeekOP.P2.
4469 assert( pOp
[1].opcode
==OP_SeekGE
);
4471 /* pOp->p2 points to the first instruction past the OP_IdxGT that
4472 ** follows the OP_SeekGE. */
4473 assert( pOp
->p2
>=(int)(pOp
-aOp
)+2 );
4474 assert( aOp
[pOp
->p2
-1].opcode
==OP_IdxGT
|| aOp
[pOp
->p2
-1].opcode
==OP_IdxGE
);
4475 testcase( aOp
[pOp
->p2
-1].opcode
==OP_IdxGE
);
4476 assert( pOp
[1].p1
==aOp
[pOp
->p2
-1].p1
);
4477 assert( pOp
[1].p2
==aOp
[pOp
->p2
-1].p2
);
4478 assert( pOp
[1].p3
==aOp
[pOp
->p2
-1].p3
);
4480 assert( pOp
->p1
>0 );
4481 pC
= p
->apCsr
[pOp
[1].p1
];
4483 assert( pC
->eCurType
==CURTYPE_BTREE
);
4484 assert( !pC
->isTable
);
4485 if( !sqlite3BtreeCursorIsValidNN(pC
->uc
.pCursor
) ){
4487 if( db
->flags
&SQLITE_VdbeTrace
){
4488 printf("... cursor not valid - fall through\n");
4495 r
.pKeyInfo
= pC
->pKeyInfo
;
4496 r
.nField
= (u16
)pOp
[1].p4
.i
;
4498 r
.aMem
= &aMem
[pOp
[1].p3
];
4502 for(i
=0; i
<r
.nField
; i
++){
4503 assert( memIsValid(&r
.aMem
[i
]) );
4504 REGISTER_TRACE(pOp
[1].p3
+i
, &aMem
[pOp
[1].p3
+i
]);
4508 res
= 0; /* Not needed. Only used to silence a warning. */
4510 rc
= sqlite3VdbeIdxKeyCompare(db
, pC
, &r
, &res
);
4511 if( rc
) goto abort_due_to_error
;
4513 seekscan_search_fail
:
4515 if( db
->flags
&SQLITE_VdbeTrace
){
4516 printf("... %d steps and then skip\n", pOp
->p1
- nStep
);
4519 VdbeBranchTaken(1,3);
4525 if( db
->flags
&SQLITE_VdbeTrace
){
4526 printf("... %d steps and then success\n", pOp
->p1
- nStep
);
4529 VdbeBranchTaken(2,3);
4535 if( db
->flags
&SQLITE_VdbeTrace
){
4536 printf("... fall through after %d steps\n", pOp
->p1
);
4539 VdbeBranchTaken(0,3);
4543 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, 0);
4545 if( rc
==SQLITE_DONE
){
4547 goto seekscan_search_fail
;
4549 goto abort_due_to_error
;
4558 /* Opcode: SeekHit P1 P2 P3 * *
4559 ** Synopsis: set P2<=seekHit<=P3
4561 ** Increase or decrease the seekHit value for cursor P1, if necessary,
4562 ** so that it is no less than P2 and no greater than P3.
4564 ** The seekHit integer represents the maximum of terms in an index for which
4565 ** there is known to be at least one match. If the seekHit value is smaller
4566 ** than the total number of equality terms in an index lookup, then the
4567 ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4568 ** early, thus saving work. This is part of the IN-early-out optimization.
4570 ** P1 must be a valid b-tree cursor.
4574 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4575 pC
= p
->apCsr
[pOp
->p1
];
4577 assert( pOp
->p3
>=pOp
->p2
);
4578 if( pC
->seekHit
<pOp
->p2
){
4580 if( db
->flags
&SQLITE_VdbeTrace
){
4581 printf("seekHit changes from %d to %d\n", pC
->seekHit
, pOp
->p2
);
4584 pC
->seekHit
= pOp
->p2
;
4585 }else if( pC
->seekHit
>pOp
->p3
){
4587 if( db
->flags
&SQLITE_VdbeTrace
){
4588 printf("seekHit changes from %d to %d\n", pC
->seekHit
, pOp
->p3
);
4591 pC
->seekHit
= pOp
->p3
;
4596 /* Opcode: IfNotOpen P1 P2 * * *
4597 ** Synopsis: if( !csr[P1] ) goto P2
4599 ** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
4601 case OP_IfNotOpen
: { /* jump */
4602 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4603 VdbeBranchTaken(p
->apCsr
[pOp
->p1
]==0, 2);
4604 if( !p
->apCsr
[pOp
->p1
] ){
4605 goto jump_to_p2_and_check_for_interrupt
;
4610 /* Opcode: Found P1 P2 P3 P4 *
4611 ** Synopsis: key=r[P3@P4]
4613 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4614 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
4617 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
4618 ** is a prefix of any entry in P1 then a jump is made to P2 and
4619 ** P1 is left pointing at the matching entry.
4621 ** This operation leaves the cursor in a state where it can be
4622 ** advanced in the forward direction. The Next instruction will work,
4623 ** but not the Prev instruction.
4625 ** See also: NotFound, NoConflict, NotExists. SeekGe
4627 /* Opcode: NotFound P1 P2 P3 P4 *
4628 ** Synopsis: key=r[P3@P4]
4630 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4631 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
4634 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
4635 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4636 ** does contain an entry whose prefix matches the P3/P4 record then control
4637 ** falls through to the next instruction and P1 is left pointing at the
4640 ** This operation leaves the cursor in a state where it cannot be
4641 ** advanced in either direction. In other words, the Next and Prev
4642 ** opcodes do not work after this operation.
4644 ** See also: Found, NotExists, NoConflict, IfNoHope
4646 /* Opcode: IfNoHope P1 P2 P3 P4 *
4647 ** Synopsis: key=r[P3@P4]
4649 ** Register P3 is the first of P4 registers that form an unpacked
4650 ** record. Cursor P1 is an index btree. P2 is a jump destination.
4651 ** In other words, the operands to this opcode are the same as the
4652 ** operands to OP_NotFound and OP_IdxGT.
4654 ** This opcode is an optimization attempt only. If this opcode always
4655 ** falls through, the correct answer is still obtained, but extra works
4658 ** A value of N in the seekHit flag of cursor P1 means that there exists
4659 ** a key P3:N that will match some record in the index. We want to know
4660 ** if it is possible for a record P3:P4 to match some record in the
4661 ** index. If it is not possible, we can skips some work. So if seekHit
4662 ** is less than P4, attempt to find out if a match is possible by running
4665 ** This opcode is used in IN clause processing for a multi-column key.
4666 ** If an IN clause is attached to an element of the key other than the
4667 ** left-most element, and if there are no matches on the most recent
4668 ** seek over the whole key, then it might be that one of the key element
4669 ** to the left is prohibiting a match, and hence there is "no hope" of
4670 ** any match regardless of how many IN clause elements are checked.
4671 ** In such a case, we abandon the IN clause search early, using this
4672 ** opcode. The opcode name comes from the fact that the
4673 ** jump is taken if there is "no hope" of achieving a match.
4675 ** See also: NotFound, SeekHit
4677 /* Opcode: NoConflict P1 P2 P3 P4 *
4678 ** Synopsis: key=r[P3@P4]
4680 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4681 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
4684 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
4685 ** contains any NULL value, jump immediately to P2. If all terms of the
4686 ** record are not-NULL then a check is done to determine if any row in the
4687 ** P1 index btree has a matching key prefix. If there are no matches, jump
4688 ** immediately to P2. If there is a match, fall through and leave the P1
4689 ** cursor pointing to the matching row.
4691 ** This opcode is similar to OP_NotFound with the exceptions that the
4692 ** branch is always taken if any part of the search key input is NULL.
4694 ** This operation leaves the cursor in a state where it cannot be
4695 ** advanced in either direction. In other words, the Next and Prev
4696 ** opcodes do not work after this operation.
4698 ** See also: NotFound, Found, NotExists
4700 case OP_IfNoHope
: { /* jump, in3 */
4702 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4703 pC
= p
->apCsr
[pOp
->p1
];
4706 if( db
->flags
&SQLITE_VdbeTrace
){
4707 printf("seekHit is %d\n", pC
->seekHit
);
4710 if( pC
->seekHit
>=pOp
->p4
.i
) break;
4711 /* Fall through into OP_NotFound */
4712 /* no break */ deliberate_fall_through
4714 case OP_NoConflict
: /* jump, in3 */
4715 case OP_NotFound
: /* jump, in3 */
4716 case OP_Found
: { /* jump, in3 */
4722 UnpackedRecord
*pFree
;
4723 UnpackedRecord
*pIdxKey
;
4727 if( pOp
->opcode
!=OP_NoConflict
) sqlite3_found_count
++;
4730 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4731 assert( pOp
->p4type
==P4_INT32
);
4732 pC
= p
->apCsr
[pOp
->p1
];
4735 pC
->seekOp
= pOp
->opcode
;
4737 pIn3
= &aMem
[pOp
->p3
];
4738 assert( pC
->eCurType
==CURTYPE_BTREE
);
4739 assert( pC
->uc
.pCursor
!=0 );
4740 assert( pC
->isTable
==0 );
4742 r
.pKeyInfo
= pC
->pKeyInfo
;
4743 r
.nField
= (u16
)pOp
->p4
.i
;
4746 for(ii
=0; ii
<r
.nField
; ii
++){
4747 assert( memIsValid(&r
.aMem
[ii
]) );
4748 assert( (r
.aMem
[ii
].flags
& MEM_Zero
)==0 || r
.aMem
[ii
].n
==0 );
4749 if( ii
) REGISTER_TRACE(pOp
->p3
+ii
, &r
.aMem
[ii
]);
4755 assert( pIn3
->flags
& MEM_Blob
);
4756 rc
= ExpandBlob(pIn3
);
4757 assert( rc
==SQLITE_OK
|| rc
==SQLITE_NOMEM
);
4758 if( rc
) goto no_mem
;
4759 pFree
= pIdxKey
= sqlite3VdbeAllocUnpackedRecord(pC
->pKeyInfo
);
4760 if( pIdxKey
==0 ) goto no_mem
;
4761 sqlite3VdbeRecordUnpack(pC
->pKeyInfo
, pIn3
->n
, pIn3
->z
, pIdxKey
);
4763 pIdxKey
->default_rc
= 0;
4765 if( pOp
->opcode
==OP_NoConflict
){
4766 /* For the OP_NoConflict opcode, take the jump if any of the
4767 ** input fields are NULL, since any key with a NULL will not
4769 for(ii
=0; ii
<pIdxKey
->nField
; ii
++){
4770 if( pIdxKey
->aMem
[ii
].flags
& MEM_Null
){
4776 rc
= sqlite3BtreeMovetoUnpacked(pC
->uc
.pCursor
, pIdxKey
, 0, 0, &res
);
4777 if( pFree
) sqlite3DbFreeNN(db
, pFree
);
4778 if( rc
!=SQLITE_OK
){
4779 goto abort_due_to_error
;
4781 pC
->seekResult
= res
;
4782 alreadyExists
= (res
==0);
4783 pC
->nullRow
= 1-alreadyExists
;
4784 pC
->deferredMoveto
= 0;
4785 pC
->cacheStatus
= CACHE_STALE
;
4786 if( pOp
->opcode
==OP_Found
){
4787 VdbeBranchTaken(alreadyExists
!=0,2);
4788 if( alreadyExists
) goto jump_to_p2
;
4790 VdbeBranchTaken(takeJump
||alreadyExists
==0,2);
4791 if( takeJump
|| !alreadyExists
) goto jump_to_p2
;
4792 if( pOp
->opcode
==OP_IfNoHope
) pC
->seekHit
= pOp
->p4
.i
;
4797 /* Opcode: SeekRowid P1 P2 P3 * *
4798 ** Synopsis: intkey=r[P3]
4800 ** P1 is the index of a cursor open on an SQL table btree (with integer
4801 ** keys). If register P3 does not contain an integer or if P1 does not
4802 ** contain a record with rowid P3 then jump immediately to P2.
4803 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4804 ** a record with rowid P3 then
4805 ** leave the cursor pointing at that record and fall through to the next
4808 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4809 ** the P3 register must be guaranteed to contain an integer value. With this
4810 ** opcode, register P3 might not contain an integer.
4812 ** The OP_NotFound opcode performs the same operation on index btrees
4813 ** (with arbitrary multi-value keys).
4815 ** This opcode leaves the cursor in a state where it cannot be advanced
4816 ** in either direction. In other words, the Next and Prev opcodes will
4817 ** not work following this opcode.
4819 ** See also: Found, NotFound, NoConflict, SeekRowid
4821 /* Opcode: NotExists P1 P2 P3 * *
4822 ** Synopsis: intkey=r[P3]
4824 ** P1 is the index of a cursor open on an SQL table btree (with integer
4825 ** keys). P3 is an integer rowid. If P1 does not contain a record with
4826 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4827 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4828 ** leave the cursor pointing at that record and fall through to the next
4831 ** The OP_SeekRowid opcode performs the same operation but also allows the
4832 ** P3 register to contain a non-integer value, in which case the jump is
4833 ** always taken. This opcode requires that P3 always contain an integer.
4835 ** The OP_NotFound opcode performs the same operation on index btrees
4836 ** (with arbitrary multi-value keys).
4838 ** This opcode leaves the cursor in a state where it cannot be advanced
4839 ** in either direction. In other words, the Next and Prev opcodes will
4840 ** not work following this opcode.
4842 ** See also: Found, NotFound, NoConflict, SeekRowid
4844 case OP_SeekRowid
: { /* jump, in3 */
4850 pIn3
= &aMem
[pOp
->p3
];
4851 testcase( pIn3
->flags
& MEM_Int
);
4852 testcase( pIn3
->flags
& MEM_IntReal
);
4853 testcase( pIn3
->flags
& MEM_Real
);
4854 testcase( (pIn3
->flags
& (MEM_Str
|MEM_Int
))==MEM_Str
);
4855 if( (pIn3
->flags
& (MEM_Int
|MEM_IntReal
))==0 ){
4856 /* If pIn3->u.i does not contain an integer, compute iKey as the
4857 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
4858 ** into an integer without loss of information. Take care to avoid
4859 ** changing the datatype of pIn3, however, as it is used by other
4860 ** parts of the prepared statement. */
4862 applyAffinity(&x
, SQLITE_AFF_NUMERIC
, encoding
);
4863 if( (x
.flags
& MEM_Int
)==0 ) goto jump_to_p2
;
4865 goto notExistsWithKey
;
4867 /* Fall through into OP_NotExists */
4868 /* no break */ deliberate_fall_through
4869 case OP_NotExists
: /* jump, in3 */
4870 pIn3
= &aMem
[pOp
->p3
];
4871 assert( (pIn3
->flags
& MEM_Int
)!=0 || pOp
->opcode
==OP_SeekRowid
);
4872 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4875 pC
= p
->apCsr
[pOp
->p1
];
4878 if( pOp
->opcode
==OP_SeekRowid
) pC
->seekOp
= OP_SeekRowid
;
4880 assert( pC
->isTable
);
4881 assert( pC
->eCurType
==CURTYPE_BTREE
);
4882 pCrsr
= pC
->uc
.pCursor
;
4885 rc
= sqlite3BtreeMovetoUnpacked(pCrsr
, 0, iKey
, 0, &res
);
4886 assert( rc
==SQLITE_OK
|| res
==0 );
4887 pC
->movetoTarget
= iKey
; /* Used by OP_Delete */
4889 pC
->cacheStatus
= CACHE_STALE
;
4890 pC
->deferredMoveto
= 0;
4891 VdbeBranchTaken(res
!=0,2);
4892 pC
->seekResult
= res
;
4894 assert( rc
==SQLITE_OK
);
4896 rc
= SQLITE_CORRUPT_BKPT
;
4901 if( rc
) goto abort_due_to_error
;
4905 /* Opcode: Sequence P1 P2 * * *
4906 ** Synopsis: r[P2]=cursor[P1].ctr++
4908 ** Find the next available sequence number for cursor P1.
4909 ** Write the sequence number into register P2.
4910 ** The sequence number on the cursor is incremented after this
4913 case OP_Sequence
: { /* out2 */
4914 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4915 assert( p
->apCsr
[pOp
->p1
]!=0 );
4916 assert( p
->apCsr
[pOp
->p1
]->eCurType
!=CURTYPE_VTAB
);
4917 pOut
= out2Prerelease(p
, pOp
);
4918 pOut
->u
.i
= p
->apCsr
[pOp
->p1
]->seqCount
++;
4923 /* Opcode: NewRowid P1 P2 P3 * *
4924 ** Synopsis: r[P2]=rowid
4926 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
4927 ** The record number is not previously used as a key in the database
4928 ** table that cursor P1 points to. The new record number is written
4929 ** written to register P2.
4931 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4932 ** the largest previously generated record number. No new record numbers are
4933 ** allowed to be less than this value. When this value reaches its maximum,
4934 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
4935 ** generated record number. This P3 mechanism is used to help implement the
4936 ** AUTOINCREMENT feature.
4938 case OP_NewRowid
: { /* out2 */
4939 i64 v
; /* The new rowid */
4940 VdbeCursor
*pC
; /* Cursor of table to get the new rowid */
4941 int res
; /* Result of an sqlite3BtreeLast() */
4942 int cnt
; /* Counter to limit the number of searches */
4943 #ifndef SQLITE_OMIT_AUTOINCREMENT
4944 Mem
*pMem
; /* Register holding largest rowid for AUTOINCREMENT */
4945 VdbeFrame
*pFrame
; /* Root frame of VDBE */
4950 pOut
= out2Prerelease(p
, pOp
);
4951 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4952 pC
= p
->apCsr
[pOp
->p1
];
4954 assert( pC
->isTable
);
4955 assert( pC
->eCurType
==CURTYPE_BTREE
);
4956 assert( pC
->uc
.pCursor
!=0 );
4958 /* The next rowid or record number (different terms for the same
4959 ** thing) is obtained in a two-step algorithm.
4961 ** First we attempt to find the largest existing rowid and add one
4962 ** to that. But if the largest existing rowid is already the maximum
4963 ** positive integer, we have to fall through to the second
4964 ** probabilistic algorithm
4966 ** The second algorithm is to select a rowid at random and see if
4967 ** it already exists in the table. If it does not exist, we have
4968 ** succeeded. If the random rowid does exist, we select a new one
4969 ** and try again, up to 100 times.
4971 assert( pC
->isTable
);
4973 #ifdef SQLITE_32BIT_ROWID
4974 # define MAX_ROWID 0x7fffffff
4976 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4977 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4978 ** to provide the constant while making all compilers happy.
4980 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
4983 if( !pC
->useRandomRowid
){
4984 rc
= sqlite3BtreeLast(pC
->uc
.pCursor
, &res
);
4985 if( rc
!=SQLITE_OK
){
4986 goto abort_due_to_error
;
4989 v
= 1; /* IMP: R-61914-48074 */
4991 assert( sqlite3BtreeCursorIsValid(pC
->uc
.pCursor
) );
4992 v
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
4994 pC
->useRandomRowid
= 1;
4996 v
++; /* IMP: R-29538-34987 */
5001 #ifndef SQLITE_OMIT_AUTOINCREMENT
5003 /* Assert that P3 is a valid memory cell. */
5004 assert( pOp
->p3
>0 );
5006 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
5007 /* Assert that P3 is a valid memory cell. */
5008 assert( pOp
->p3
<=pFrame
->nMem
);
5009 pMem
= &pFrame
->aMem
[pOp
->p3
];
5011 /* Assert that P3 is a valid memory cell. */
5012 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
5013 pMem
= &aMem
[pOp
->p3
];
5014 memAboutToChange(p
, pMem
);
5016 assert( memIsValid(pMem
) );
5018 REGISTER_TRACE(pOp
->p3
, pMem
);
5019 sqlite3VdbeMemIntegerify(pMem
);
5020 assert( (pMem
->flags
& MEM_Int
)!=0 ); /* mem(P3) holds an integer */
5021 if( pMem
->u
.i
==MAX_ROWID
|| pC
->useRandomRowid
){
5022 rc
= SQLITE_FULL
; /* IMP: R-17817-00630 */
5023 goto abort_due_to_error
;
5025 if( v
<pMem
->u
.i
+1 ){
5031 if( pC
->useRandomRowid
){
5032 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
5033 ** largest possible integer (9223372036854775807) then the database
5034 ** engine starts picking positive candidate ROWIDs at random until
5035 ** it finds one that is not previously used. */
5036 assert( pOp
->p3
==0 ); /* We cannot be in random rowid mode if this is
5037 ** an AUTOINCREMENT table. */
5040 sqlite3_randomness(sizeof(v
), &v
);
5041 v
&= (MAX_ROWID
>>1); v
++; /* Ensure that v is greater than zero */
5042 }while( ((rc
= sqlite3BtreeMovetoUnpacked(pC
->uc
.pCursor
, 0, (u64
)v
,
5043 0, &res
))==SQLITE_OK
)
5046 if( rc
) goto abort_due_to_error
;
5048 rc
= SQLITE_FULL
; /* IMP: R-38219-53002 */
5049 goto abort_due_to_error
;
5051 assert( v
>0 ); /* EV: R-40812-03570 */
5053 pC
->deferredMoveto
= 0;
5054 pC
->cacheStatus
= CACHE_STALE
;
5060 /* Opcode: Insert P1 P2 P3 P4 P5
5061 ** Synopsis: intkey=r[P3] data=r[P2]
5063 ** Write an entry into the table of cursor P1. A new entry is
5064 ** created if it doesn't already exist or the data for an existing
5065 ** entry is overwritten. The data is the value MEM_Blob stored in register
5066 ** number P2. The key is stored in register P3. The key must
5069 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5070 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
5071 ** then rowid is stored for subsequent return by the
5072 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
5074 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5075 ** run faster by avoiding an unnecessary seek on cursor P1. However,
5076 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5077 ** seeks on the cursor or if the most recent seek used a key equal to P3.
5079 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5080 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5081 ** is part of an INSERT operation. The difference is only important to
5084 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
5085 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5086 ** following a successful insert.
5088 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5089 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
5090 ** and register P2 becomes ephemeral. If the cursor is changed, the
5091 ** value of register P2 will then change. Make sure this does not
5092 ** cause any problems.)
5094 ** This instruction only works on tables. The equivalent instruction
5095 ** for indices is OP_IdxInsert.
5098 Mem
*pData
; /* MEM cell holding data for the record to be inserted */
5099 Mem
*pKey
; /* MEM cell holding key for the record */
5100 VdbeCursor
*pC
; /* Cursor to table into which insert is written */
5101 int seekResult
; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5102 const char *zDb
; /* database name - used by the update hook */
5103 Table
*pTab
; /* Table structure - used by update and pre-update hooks */
5104 BtreePayload x
; /* Payload to be inserted */
5106 pData
= &aMem
[pOp
->p2
];
5107 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5108 assert( memIsValid(pData
) );
5109 pC
= p
->apCsr
[pOp
->p1
];
5111 assert( pC
->eCurType
==CURTYPE_BTREE
);
5112 assert( pC
->deferredMoveto
==0 );
5113 assert( pC
->uc
.pCursor
!=0 );
5114 assert( (pOp
->p5
& OPFLAG_ISNOOP
) || pC
->isTable
);
5115 assert( pOp
->p4type
==P4_TABLE
|| pOp
->p4type
>=P4_STATIC
);
5116 REGISTER_TRACE(pOp
->p2
, pData
);
5117 sqlite3VdbeIncrWriteCounter(p
, pC
);
5119 pKey
= &aMem
[pOp
->p3
];
5120 assert( pKey
->flags
& MEM_Int
);
5121 assert( memIsValid(pKey
) );
5122 REGISTER_TRACE(pOp
->p3
, pKey
);
5125 if( pOp
->p4type
==P4_TABLE
&& HAS_UPDATE_HOOK(db
) ){
5126 assert( pC
->iDb
>=0 );
5127 zDb
= db
->aDb
[pC
->iDb
].zDbSName
;
5128 pTab
= pOp
->p4
.pTab
;
5129 assert( (pOp
->p5
& OPFLAG_ISNOOP
) || HasRowid(pTab
) );
5132 zDb
= 0; /* Not needed. Silence a compiler warning. */
5135 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
5136 /* Invoke the pre-update hook, if any */
5138 if( db
->xPreUpdateCallback
&& !(pOp
->p5
& OPFLAG_ISUPDATE
) ){
5139 sqlite3VdbePreUpdateHook(p
,pC
,SQLITE_INSERT
,zDb
,pTab
,x
.nKey
,pOp
->p2
,-1);
5141 if( db
->xUpdateCallback
==0 || pTab
->aCol
==0 ){
5142 /* Prevent post-update hook from running in cases when it should not */
5146 if( pOp
->p5
& OPFLAG_ISNOOP
) break;
5149 if( pOp
->p5
& OPFLAG_NCHANGE
) p
->nChange
++;
5150 if( pOp
->p5
& OPFLAG_LASTROWID
) db
->lastRowid
= x
.nKey
;
5151 assert( (pData
->flags
& (MEM_Blob
|MEM_Str
))!=0 || pData
->n
==0 );
5154 seekResult
= ((pOp
->p5
& OPFLAG_USESEEKRESULT
) ? pC
->seekResult
: 0);
5155 if( pData
->flags
& MEM_Zero
){
5156 x
.nZero
= pData
->u
.nZero
;
5161 rc
= sqlite3BtreeInsert(pC
->uc
.pCursor
, &x
,
5162 (pOp
->p5
& (OPFLAG_APPEND
|OPFLAG_SAVEPOSITION
|OPFLAG_PREFORMAT
)),
5165 pC
->deferredMoveto
= 0;
5166 pC
->cacheStatus
= CACHE_STALE
;
5168 /* Invoke the update-hook if required. */
5169 if( rc
) goto abort_due_to_error
;
5171 assert( db
->xUpdateCallback
!=0 );
5172 assert( pTab
->aCol
!=0 );
5173 db
->xUpdateCallback(db
->pUpdateArg
,
5174 (pOp
->p5
& OPFLAG_ISUPDATE
) ? SQLITE_UPDATE
: SQLITE_INSERT
,
5175 zDb
, pTab
->zName
, x
.nKey
);
5180 /* Opcode: RowCell P1 P2 P3 * *
5182 ** P1 and P2 are both open cursors. Both must be opened on the same type
5183 ** of table - intkey or index. This opcode is used as part of copying
5184 ** the current row from P2 into P1. If the cursors are opened on intkey
5185 ** tables, register P3 contains the rowid to use with the new record in
5186 ** P1. If they are opened on index tables, P3 is not used.
5188 ** This opcode must be followed by either an Insert or InsertIdx opcode
5189 ** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
5192 VdbeCursor
*pDest
; /* Cursor to write to */
5193 VdbeCursor
*pSrc
; /* Cursor to read from */
5194 i64 iKey
; /* Rowid value to insert with */
5195 assert( pOp
[1].opcode
==OP_Insert
|| pOp
[1].opcode
==OP_IdxInsert
);
5196 assert( pOp
[1].opcode
==OP_Insert
|| pOp
->p3
==0 );
5197 assert( pOp
[1].opcode
==OP_IdxInsert
|| pOp
->p3
>0 );
5198 assert( pOp
[1].p5
& OPFLAG_PREFORMAT
);
5199 pDest
= p
->apCsr
[pOp
->p1
];
5200 pSrc
= p
->apCsr
[pOp
->p2
];
5201 iKey
= pOp
->p3
? aMem
[pOp
->p3
].u
.i
: 0;
5202 rc
= sqlite3BtreeTransferRow(pDest
->uc
.pCursor
, pSrc
->uc
.pCursor
, iKey
);
5203 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
5207 /* Opcode: Delete P1 P2 P3 P4 P5
5209 ** Delete the record at which the P1 cursor is currently pointing.
5211 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5212 ** the cursor will be left pointing at either the next or the previous
5213 ** record in the table. If it is left pointing at the next record, then
5214 ** the next Next instruction will be a no-op. As a result, in this case
5215 ** it is ok to delete a record from within a Next loop. If
5216 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5217 ** left in an undefined state.
5219 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5220 ** delete one of several associated with deleting a table row and all its
5221 ** associated index entries. Exactly one of those deletes is the "primary"
5222 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5223 ** marked with the AUXDELETE flag.
5225 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5226 ** change count is incremented (otherwise not).
5228 ** P1 must not be pseudo-table. It has to be a real table with
5231 ** If P4 is not NULL then it points to a Table object. In this case either
5232 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5233 ** have been positioned using OP_NotFound prior to invoking this opcode in
5234 ** this case. Specifically, if one is configured, the pre-update hook is
5235 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5236 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
5238 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5239 ** of the memory cell that contains the value that the rowid of the row will
5240 ** be set to by the update.
5249 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5250 pC
= p
->apCsr
[pOp
->p1
];
5252 assert( pC
->eCurType
==CURTYPE_BTREE
);
5253 assert( pC
->uc
.pCursor
!=0 );
5254 assert( pC
->deferredMoveto
==0 );
5255 sqlite3VdbeIncrWriteCounter(p
, pC
);
5258 if( pOp
->p4type
==P4_TABLE
5259 && HasRowid(pOp
->p4
.pTab
)
5261 && sqlite3BtreeCursorIsValidNN(pC
->uc
.pCursor
)
5263 /* If p5 is zero, the seek operation that positioned the cursor prior to
5264 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5265 ** the row that is being deleted */
5266 i64 iKey
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5267 assert( CORRUPT_DB
|| pC
->movetoTarget
==iKey
);
5271 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5272 ** the name of the db to pass as to it. Also set local pTab to a copy
5273 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5274 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5275 ** VdbeCursor.movetoTarget to the current rowid. */
5276 if( pOp
->p4type
==P4_TABLE
&& HAS_UPDATE_HOOK(db
) ){
5277 assert( pC
->iDb
>=0 );
5278 assert( pOp
->p4
.pTab
!=0 );
5279 zDb
= db
->aDb
[pC
->iDb
].zDbSName
;
5280 pTab
= pOp
->p4
.pTab
;
5281 if( (pOp
->p5
& OPFLAG_SAVEPOSITION
)!=0 && pC
->isTable
){
5282 pC
->movetoTarget
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5285 zDb
= 0; /* Not needed. Silence a compiler warning. */
5286 pTab
= 0; /* Not needed. Silence a compiler warning. */
5289 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
5290 /* Invoke the pre-update-hook if required. */
5291 if( db
->xPreUpdateCallback
&& pOp
->p4
.pTab
){
5292 assert( !(opflags
& OPFLAG_ISUPDATE
)
5293 || HasRowid(pTab
)==0
5294 || (aMem
[pOp
->p3
].flags
& MEM_Int
)
5296 sqlite3VdbePreUpdateHook(p
, pC
,
5297 (opflags
& OPFLAG_ISUPDATE
) ? SQLITE_UPDATE
: SQLITE_DELETE
,
5298 zDb
, pTab
, pC
->movetoTarget
,
5302 if( opflags
& OPFLAG_ISNOOP
) break;
5305 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5306 assert( (pOp
->p5
& ~(OPFLAG_SAVEPOSITION
|OPFLAG_AUXDELETE
))==0 );
5307 assert( OPFLAG_SAVEPOSITION
==BTREE_SAVEPOSITION
);
5308 assert( OPFLAG_AUXDELETE
==BTREE_AUXDELETE
);
5312 if( pC
->isEphemeral
==0
5313 && (pOp
->p5
& OPFLAG_AUXDELETE
)==0
5314 && (pC
->wrFlag
& OPFLAG_FORDELETE
)==0
5318 if( pOp
->p2
& OPFLAG_NCHANGE
){
5324 rc
= sqlite3BtreeDelete(pC
->uc
.pCursor
, pOp
->p5
);
5325 pC
->cacheStatus
= CACHE_STALE
;
5327 if( rc
) goto abort_due_to_error
;
5329 /* Invoke the update-hook if required. */
5330 if( opflags
& OPFLAG_NCHANGE
){
5332 if( db
->xUpdateCallback
&& HasRowid(pTab
) ){
5333 db
->xUpdateCallback(db
->pUpdateArg
, SQLITE_DELETE
, zDb
, pTab
->zName
,
5335 assert( pC
->iDb
>=0 );
5341 /* Opcode: ResetCount * * * * *
5343 ** The value of the change counter is copied to the database handle
5344 ** change counter (returned by subsequent calls to sqlite3_changes()).
5345 ** Then the VMs internal change counter resets to 0.
5346 ** This is used by trigger programs.
5348 case OP_ResetCount
: {
5349 sqlite3VdbeSetChanges(db
, p
->nChange
);
5354 /* Opcode: SorterCompare P1 P2 P3 P4
5355 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
5357 ** P1 is a sorter cursor. This instruction compares a prefix of the
5358 ** record blob in register P3 against a prefix of the entry that
5359 ** the sorter cursor currently points to. Only the first P4 fields
5360 ** of r[P3] and the sorter record are compared.
5362 ** If either P3 or the sorter contains a NULL in one of their significant
5363 ** fields (not counting the P4 fields at the end which are ignored) then
5364 ** the comparison is assumed to be equal.
5366 ** Fall through to next instruction if the two records compare equal to
5367 ** each other. Jump to P2 if they are different.
5369 case OP_SorterCompare
: {
5374 pC
= p
->apCsr
[pOp
->p1
];
5375 assert( isSorter(pC
) );
5376 assert( pOp
->p4type
==P4_INT32
);
5377 pIn3
= &aMem
[pOp
->p3
];
5378 nKeyCol
= pOp
->p4
.i
;
5380 rc
= sqlite3VdbeSorterCompare(pC
, pIn3
, nKeyCol
, &res
);
5381 VdbeBranchTaken(res
!=0,2);
5382 if( rc
) goto abort_due_to_error
;
5383 if( res
) goto jump_to_p2
;
5387 /* Opcode: SorterData P1 P2 P3 * *
5388 ** Synopsis: r[P2]=data
5390 ** Write into register P2 the current sorter data for sorter cursor P1.
5391 ** Then clear the column header cache on cursor P3.
5393 ** This opcode is normally use to move a record out of the sorter and into
5394 ** a register that is the source for a pseudo-table cursor created using
5395 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
5396 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
5397 ** us from having to issue a separate NullRow instruction to clear that cache.
5399 case OP_SorterData
: {
5402 pOut
= &aMem
[pOp
->p2
];
5403 pC
= p
->apCsr
[pOp
->p1
];
5404 assert( isSorter(pC
) );
5405 rc
= sqlite3VdbeSorterRowkey(pC
, pOut
);
5406 assert( rc
!=SQLITE_OK
|| (pOut
->flags
& MEM_Blob
) );
5407 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5408 if( rc
) goto abort_due_to_error
;
5409 p
->apCsr
[pOp
->p3
]->cacheStatus
= CACHE_STALE
;
5413 /* Opcode: RowData P1 P2 P3 * *
5414 ** Synopsis: r[P2]=data
5416 ** Write into register P2 the complete row content for the row at
5417 ** which cursor P1 is currently pointing.
5418 ** There is no interpretation of the data.
5419 ** It is just copied onto the P2 register exactly as
5420 ** it is found in the database file.
5422 ** If cursor P1 is an index, then the content is the key of the row.
5423 ** If cursor P2 is a table, then the content extracted is the data.
5425 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
5426 ** of a real table, not a pseudo-table.
5428 ** If P3!=0 then this opcode is allowed to make an ephemeral pointer
5429 ** into the database page. That means that the content of the output
5430 ** register will be invalidated as soon as the cursor moves - including
5431 ** moves caused by other cursors that "save" the current cursors
5432 ** position in order that they can write to the same table. If P3==0
5433 ** then a copy of the data is made into memory. P3!=0 is faster, but
5436 ** If P3!=0 then the content of the P2 register is unsuitable for use
5437 ** in OP_Result and any OP_Result will invalidate the P2 register content.
5438 ** The P2 register content is invalidated by opcodes like OP_Function or
5439 ** by any use of another cursor pointing to the same table.
5446 pOut
= out2Prerelease(p
, pOp
);
5448 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5449 pC
= p
->apCsr
[pOp
->p1
];
5451 assert( pC
->eCurType
==CURTYPE_BTREE
);
5452 assert( isSorter(pC
)==0 );
5453 assert( pC
->nullRow
==0 );
5454 assert( pC
->uc
.pCursor
!=0 );
5455 pCrsr
= pC
->uc
.pCursor
;
5457 /* The OP_RowData opcodes always follow OP_NotExists or
5458 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5459 ** that might invalidate the cursor.
5460 ** If this where not the case, on of the following assert()s
5461 ** would fail. Should this ever change (because of changes in the code
5462 ** generator) then the fix would be to insert a call to
5463 ** sqlite3VdbeCursorMoveto().
5465 assert( pC
->deferredMoveto
==0 );
5466 assert( sqlite3BtreeCursorIsValid(pCrsr
) );
5468 n
= sqlite3BtreePayloadSize(pCrsr
);
5469 if( n
>(u32
)db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
5473 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pCrsr
, n
, pOut
);
5474 if( rc
) goto abort_due_to_error
;
5475 if( !pOp
->p3
) Deephemeralize(pOut
);
5476 UPDATE_MAX_BLOBSIZE(pOut
);
5477 REGISTER_TRACE(pOp
->p2
, pOut
);
5481 /* Opcode: Rowid P1 P2 * * *
5482 ** Synopsis: r[P2]=rowid
5484 ** Store in register P2 an integer which is the key of the table entry that
5485 ** P1 is currently point to.
5487 ** P1 can be either an ordinary table or a virtual table. There used to
5488 ** be a separate OP_VRowid opcode for use with virtual tables, but this
5489 ** one opcode now works for both table types.
5491 case OP_Rowid
: { /* out2 */
5494 sqlite3_vtab
*pVtab
;
5495 const sqlite3_module
*pModule
;
5497 pOut
= out2Prerelease(p
, pOp
);
5498 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5499 pC
= p
->apCsr
[pOp
->p1
];
5501 assert( pC
->eCurType
!=CURTYPE_PSEUDO
|| pC
->nullRow
);
5503 pOut
->flags
= MEM_Null
;
5505 }else if( pC
->deferredMoveto
){
5506 v
= pC
->movetoTarget
;
5507 #ifndef SQLITE_OMIT_VIRTUALTABLE
5508 }else if( pC
->eCurType
==CURTYPE_VTAB
){
5509 assert( pC
->uc
.pVCur
!=0 );
5510 pVtab
= pC
->uc
.pVCur
->pVtab
;
5511 pModule
= pVtab
->pModule
;
5512 assert( pModule
->xRowid
);
5513 rc
= pModule
->xRowid(pC
->uc
.pVCur
, &v
);
5514 sqlite3VtabImportErrmsg(p
, pVtab
);
5515 if( rc
) goto abort_due_to_error
;
5516 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5518 assert( pC
->eCurType
==CURTYPE_BTREE
);
5519 assert( pC
->uc
.pCursor
!=0 );
5520 rc
= sqlite3VdbeCursorRestore(pC
);
5521 if( rc
) goto abort_due_to_error
;
5523 pOut
->flags
= MEM_Null
;
5526 v
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5532 /* Opcode: NullRow P1 * * * *
5534 ** Move the cursor P1 to a null row. Any OP_Column operations
5535 ** that occur while the cursor is on the null row will always
5541 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5542 pC
= p
->apCsr
[pOp
->p1
];
5545 pC
->cacheStatus
= CACHE_STALE
;
5546 if( pC
->eCurType
==CURTYPE_BTREE
){
5547 assert( pC
->uc
.pCursor
!=0 );
5548 sqlite3BtreeClearCursor(pC
->uc
.pCursor
);
5551 if( pC
->seekOp
==0 ) pC
->seekOp
= OP_NullRow
;
5556 /* Opcode: SeekEnd P1 * * * *
5558 ** Position cursor P1 at the end of the btree for the purpose of
5559 ** appending a new entry onto the btree.
5561 ** It is assumed that the cursor is used only for appending and so
5562 ** if the cursor is valid, then the cursor must already be pointing
5563 ** at the end of the btree and so no changes are made to
5566 /* Opcode: Last P1 P2 * * *
5568 ** The next use of the Rowid or Column or Prev instruction for P1
5569 ** will refer to the last entry in the database table or index.
5570 ** If the table or index is empty and P2>0, then jump immediately to P2.
5571 ** If P2 is 0 or if the table or index is not empty, fall through
5572 ** to the following instruction.
5574 ** This opcode leaves the cursor configured to move in reverse order,
5575 ** from the end toward the beginning. In other words, the cursor is
5576 ** configured to use Prev, not Next.
5579 case OP_Last
: { /* jump */
5584 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5585 pC
= p
->apCsr
[pOp
->p1
];
5587 assert( pC
->eCurType
==CURTYPE_BTREE
);
5588 pCrsr
= pC
->uc
.pCursor
;
5592 pC
->seekOp
= pOp
->opcode
;
5594 if( pOp
->opcode
==OP_SeekEnd
){
5595 assert( pOp
->p2
==0 );
5596 pC
->seekResult
= -1;
5597 if( sqlite3BtreeCursorIsValidNN(pCrsr
) ){
5601 rc
= sqlite3BtreeLast(pCrsr
, &res
);
5602 pC
->nullRow
= (u8
)res
;
5603 pC
->deferredMoveto
= 0;
5604 pC
->cacheStatus
= CACHE_STALE
;
5605 if( rc
) goto abort_due_to_error
;
5607 VdbeBranchTaken(res
!=0,2);
5608 if( res
) goto jump_to_p2
;
5613 /* Opcode: IfSmaller P1 P2 P3 * *
5615 ** Estimate the number of rows in the table P1. Jump to P2 if that
5616 ** estimate is less than approximately 2**(0.1*P3).
5618 case OP_IfSmaller
: { /* jump */
5624 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5625 pC
= p
->apCsr
[pOp
->p1
];
5627 pCrsr
= pC
->uc
.pCursor
;
5629 rc
= sqlite3BtreeFirst(pCrsr
, &res
);
5630 if( rc
) goto abort_due_to_error
;
5632 sz
= sqlite3BtreeRowCountEst(pCrsr
);
5633 if( ALWAYS(sz
>=0) && sqlite3LogEst((u64
)sz
)<pOp
->p3
) res
= 1;
5635 VdbeBranchTaken(res
!=0,2);
5636 if( res
) goto jump_to_p2
;
5641 /* Opcode: SorterSort P1 P2 * * *
5643 ** After all records have been inserted into the Sorter object
5644 ** identified by P1, invoke this opcode to actually do the sorting.
5645 ** Jump to P2 if there are no records to be sorted.
5647 ** This opcode is an alias for OP_Sort and OP_Rewind that is used
5648 ** for Sorter objects.
5650 /* Opcode: Sort P1 P2 * * *
5652 ** This opcode does exactly the same thing as OP_Rewind except that
5653 ** it increments an undocumented global variable used for testing.
5655 ** Sorting is accomplished by writing records into a sorting index,
5656 ** then rewinding that index and playing it back from beginning to
5657 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5658 ** rewinding so that the global variable will be incremented and
5659 ** regression tests can determine whether or not the optimizer is
5660 ** correctly optimizing out sorts.
5662 case OP_SorterSort
: /* jump */
5663 case OP_Sort
: { /* jump */
5665 sqlite3_sort_count
++;
5666 sqlite3_search_count
--;
5668 p
->aCounter
[SQLITE_STMTSTATUS_SORT
]++;
5669 /* Fall through into OP_Rewind */
5670 /* no break */ deliberate_fall_through
5672 /* Opcode: Rewind P1 P2 * * *
5674 ** The next use of the Rowid or Column or Next instruction for P1
5675 ** will refer to the first entry in the database table or index.
5676 ** If the table or index is empty, jump immediately to P2.
5677 ** If the table or index is not empty, fall through to the following
5680 ** This opcode leaves the cursor configured to move in forward order,
5681 ** from the beginning toward the end. In other words, the cursor is
5682 ** configured to use Next, not Prev.
5684 case OP_Rewind
: { /* jump */
5689 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5690 assert( pOp
->p5
==0 );
5691 pC
= p
->apCsr
[pOp
->p1
];
5693 assert( isSorter(pC
)==(pOp
->opcode
==OP_SorterSort
) );
5696 pC
->seekOp
= OP_Rewind
;
5699 rc
= sqlite3VdbeSorterRewind(pC
, &res
);
5701 assert( pC
->eCurType
==CURTYPE_BTREE
);
5702 pCrsr
= pC
->uc
.pCursor
;
5704 rc
= sqlite3BtreeFirst(pCrsr
, &res
);
5705 pC
->deferredMoveto
= 0;
5706 pC
->cacheStatus
= CACHE_STALE
;
5708 if( rc
) goto abort_due_to_error
;
5709 pC
->nullRow
= (u8
)res
;
5710 assert( pOp
->p2
>0 && pOp
->p2
<p
->nOp
);
5711 VdbeBranchTaken(res
!=0,2);
5712 if( res
) goto jump_to_p2
;
5716 /* Opcode: Next P1 P2 P3 P4 P5
5718 ** Advance cursor P1 so that it points to the next key/data pair in its
5719 ** table or index. If there are no more key/value pairs then fall through
5720 ** to the following instruction. But if the cursor advance was successful,
5721 ** jump immediately to P2.
5723 ** The Next opcode is only valid following an SeekGT, SeekGE, or
5724 ** OP_Rewind opcode used to position the cursor. Next is not allowed
5725 ** to follow SeekLT, SeekLE, or OP_Last.
5727 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5728 ** been opened prior to this opcode or the program will segfault.
5730 ** The P3 value is a hint to the btree implementation. If P3==1, that
5731 ** means P1 is an SQL index and that this instruction could have been
5732 ** omitted if that index had been unique. P3 is usually 0. P3 is
5733 ** always either 0 or 1.
5735 ** P4 is always of type P4_ADVANCE. The function pointer points to
5736 ** sqlite3BtreeNext().
5738 ** If P5 is positive and the jump is taken, then event counter
5739 ** number P5-1 in the prepared statement is incremented.
5743 /* Opcode: Prev P1 P2 P3 P4 P5
5745 ** Back up cursor P1 so that it points to the previous key/data pair in its
5746 ** table or index. If there is no previous key/value pairs then fall through
5747 ** to the following instruction. But if the cursor backup was successful,
5748 ** jump immediately to P2.
5751 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
5752 ** OP_Last opcode used to position the cursor. Prev is not allowed
5753 ** to follow SeekGT, SeekGE, or OP_Rewind.
5755 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5756 ** not open then the behavior is undefined.
5758 ** The P3 value is a hint to the btree implementation. If P3==1, that
5759 ** means P1 is an SQL index and that this instruction could have been
5760 ** omitted if that index had been unique. P3 is usually 0. P3 is
5761 ** always either 0 or 1.
5763 ** P4 is always of type P4_ADVANCE. The function pointer points to
5764 ** sqlite3BtreePrevious().
5766 ** If P5 is positive and the jump is taken, then event counter
5767 ** number P5-1 in the prepared statement is incremented.
5769 /* Opcode: SorterNext P1 P2 * * P5
5771 ** This opcode works just like OP_Next except that P1 must be a
5772 ** sorter object for which the OP_SorterSort opcode has been
5773 ** invoked. This opcode advances the cursor to the next sorted
5774 ** record, or jumps to P2 if there are no more sorted records.
5776 case OP_SorterNext
: { /* jump */
5779 pC
= p
->apCsr
[pOp
->p1
];
5780 assert( isSorter(pC
) );
5781 rc
= sqlite3VdbeSorterNext(db
, pC
);
5783 case OP_Prev
: /* jump */
5784 case OP_Next
: /* jump */
5785 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5786 assert( pOp
->p5
<ArraySize(p
->aCounter
) );
5787 pC
= p
->apCsr
[pOp
->p1
];
5789 assert( pC
->deferredMoveto
==0 );
5790 assert( pC
->eCurType
==CURTYPE_BTREE
);
5791 assert( pOp
->opcode
!=OP_Next
|| pOp
->p4
.xAdvance
==sqlite3BtreeNext
);
5792 assert( pOp
->opcode
!=OP_Prev
|| pOp
->p4
.xAdvance
==sqlite3BtreePrevious
);
5794 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
5795 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
5796 assert( pOp
->opcode
!=OP_Next
5797 || pC
->seekOp
==OP_SeekGT
|| pC
->seekOp
==OP_SeekGE
5798 || pC
->seekOp
==OP_Rewind
|| pC
->seekOp
==OP_Found
5799 || pC
->seekOp
==OP_NullRow
|| pC
->seekOp
==OP_SeekRowid
5800 || pC
->seekOp
==OP_IfNoHope
);
5801 assert( pOp
->opcode
!=OP_Prev
5802 || pC
->seekOp
==OP_SeekLT
|| pC
->seekOp
==OP_SeekLE
5803 || pC
->seekOp
==OP_Last
|| pC
->seekOp
==OP_IfNoHope
5804 || pC
->seekOp
==OP_NullRow
);
5806 rc
= pOp
->p4
.xAdvance(pC
->uc
.pCursor
, pOp
->p3
);
5808 pC
->cacheStatus
= CACHE_STALE
;
5809 VdbeBranchTaken(rc
==SQLITE_OK
,2);
5810 if( rc
==SQLITE_OK
){
5812 p
->aCounter
[pOp
->p5
]++;
5814 sqlite3_search_count
++;
5816 goto jump_to_p2_and_check_for_interrupt
;
5818 if( rc
!=SQLITE_DONE
) goto abort_due_to_error
;
5821 goto check_for_interrupt
;
5824 /* Opcode: IdxInsert P1 P2 P3 P4 P5
5825 ** Synopsis: key=r[P2]
5827 ** Register P2 holds an SQL index key made using the
5828 ** MakeRecord instructions. This opcode writes that key
5829 ** into the index P1. Data for the entry is nil.
5831 ** If P4 is not zero, then it is the number of values in the unpacked
5832 ** key of reg(P2). In that case, P3 is the index of the first register
5833 ** for the unpacked key. The availability of the unpacked key can sometimes
5834 ** be an optimization.
5836 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5837 ** that this insert is likely to be an append.
5839 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5840 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5841 ** then the change counter is unchanged.
5843 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5844 ** run faster by avoiding an unnecessary seek on cursor P1. However,
5845 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5846 ** seeks on the cursor or if the most recent seek used a key equivalent
5849 ** This instruction only works for indices. The equivalent instruction
5850 ** for tables is OP_Insert.
5852 case OP_IdxInsert
: { /* in2 */
5856 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5857 pC
= p
->apCsr
[pOp
->p1
];
5858 sqlite3VdbeIncrWriteCounter(p
, pC
);
5860 assert( !isSorter(pC
) );
5861 pIn2
= &aMem
[pOp
->p2
];
5862 assert( (pIn2
->flags
& MEM_Blob
) || (pOp
->p5
& OPFLAG_PREFORMAT
) );
5863 if( pOp
->p5
& OPFLAG_NCHANGE
) p
->nChange
++;
5864 assert( pC
->eCurType
==CURTYPE_BTREE
);
5865 assert( pC
->isTable
==0 );
5866 rc
= ExpandBlob(pIn2
);
5867 if( rc
) goto abort_due_to_error
;
5870 x
.aMem
= aMem
+ pOp
->p3
;
5871 x
.nMem
= (u16
)pOp
->p4
.i
;
5872 rc
= sqlite3BtreeInsert(pC
->uc
.pCursor
, &x
,
5873 (pOp
->p5
& (OPFLAG_APPEND
|OPFLAG_SAVEPOSITION
|OPFLAG_PREFORMAT
)),
5874 ((pOp
->p5
& OPFLAG_USESEEKRESULT
) ? pC
->seekResult
: 0)
5876 assert( pC
->deferredMoveto
==0 );
5877 pC
->cacheStatus
= CACHE_STALE
;
5878 if( rc
) goto abort_due_to_error
;
5882 /* Opcode: SorterInsert P1 P2 * * *
5883 ** Synopsis: key=r[P2]
5885 ** Register P2 holds an SQL index key made using the
5886 ** MakeRecord instructions. This opcode writes that key
5887 ** into the sorter P1. Data for the entry is nil.
5889 case OP_SorterInsert
: { /* in2 */
5892 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5893 pC
= p
->apCsr
[pOp
->p1
];
5894 sqlite3VdbeIncrWriteCounter(p
, pC
);
5896 assert( isSorter(pC
) );
5897 pIn2
= &aMem
[pOp
->p2
];
5898 assert( pIn2
->flags
& MEM_Blob
);
5899 assert( pC
->isTable
==0 );
5900 rc
= ExpandBlob(pIn2
);
5901 if( rc
) goto abort_due_to_error
;
5902 rc
= sqlite3VdbeSorterWrite(pC
, pIn2
);
5903 if( rc
) goto abort_due_to_error
;
5907 /* Opcode: IdxDelete P1 P2 P3 * P5
5908 ** Synopsis: key=r[P2@P3]
5910 ** The content of P3 registers starting at register P2 form
5911 ** an unpacked index key. This opcode removes that entry from the
5912 ** index opened by cursor P1.
5914 ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
5915 ** if no matching index entry is found. This happens when running
5916 ** an UPDATE or DELETE statement and the index entry to be updated
5917 ** or deleted is not found. For some uses of IdxDelete
5918 ** (example: the EXCEPT operator) it does not matter that no matching
5919 ** entry is found. For those cases, P5 is zero.
5921 case OP_IdxDelete
: {
5927 assert( pOp
->p3
>0 );
5928 assert( pOp
->p2
>0 && pOp
->p2
+pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
)+1 );
5929 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5930 pC
= p
->apCsr
[pOp
->p1
];
5932 assert( pC
->eCurType
==CURTYPE_BTREE
);
5933 sqlite3VdbeIncrWriteCounter(p
, pC
);
5934 pCrsr
= pC
->uc
.pCursor
;
5936 r
.pKeyInfo
= pC
->pKeyInfo
;
5937 r
.nField
= (u16
)pOp
->p3
;
5939 r
.aMem
= &aMem
[pOp
->p2
];
5940 rc
= sqlite3BtreeMovetoUnpacked(pCrsr
, &r
, 0, 0, &res
);
5941 if( rc
) goto abort_due_to_error
;
5943 rc
= sqlite3BtreeDelete(pCrsr
, BTREE_AUXDELETE
);
5944 if( rc
) goto abort_due_to_error
;
5945 }else if( pOp
->p5
){
5946 rc
= sqlite3ReportError(SQLITE_CORRUPT_INDEX
, __LINE__
, "index corruption");
5947 goto abort_due_to_error
;
5949 assert( pC
->deferredMoveto
==0 );
5950 pC
->cacheStatus
= CACHE_STALE
;
5955 /* Opcode: DeferredSeek P1 * P3 P4 *
5956 ** Synopsis: Move P3 to P1.rowid if needed
5958 ** P1 is an open index cursor and P3 is a cursor on the corresponding
5959 ** table. This opcode does a deferred seek of the P3 table cursor
5960 ** to the row that corresponds to the current row of P1.
5962 ** This is a deferred seek. Nothing actually happens until
5963 ** the cursor is used to read a record. That way, if no reads
5964 ** occur, no unnecessary I/O happens.
5966 ** P4 may be an array of integers (type P4_INTARRAY) containing
5967 ** one entry for each column in the P3 table. If array entry a(i)
5968 ** is non-zero, then reading column a(i)-1 from cursor P3 is
5969 ** equivalent to performing the deferred seek and then reading column i
5970 ** from P1. This information is stored in P3 and used to redirect
5971 ** reads against P3 over to P1, thus possibly avoiding the need to
5972 ** seek and read cursor P3.
5974 /* Opcode: IdxRowid P1 P2 * * *
5975 ** Synopsis: r[P2]=rowid
5977 ** Write into register P2 an integer which is the last entry in the record at
5978 ** the end of the index key pointed to by cursor P1. This integer should be
5979 ** the rowid of the table entry to which this index entry points.
5981 ** See also: Rowid, MakeRecord.
5983 case OP_DeferredSeek
:
5984 case OP_IdxRowid
: { /* out2 */
5985 VdbeCursor
*pC
; /* The P1 index cursor */
5986 VdbeCursor
*pTabCur
; /* The P2 table cursor (OP_DeferredSeek only) */
5987 i64 rowid
; /* Rowid that P1 current points to */
5989 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5990 pC
= p
->apCsr
[pOp
->p1
];
5992 assert( pC
->eCurType
==CURTYPE_BTREE
);
5993 assert( pC
->uc
.pCursor
!=0 );
5994 assert( pC
->isTable
==0 );
5995 assert( pC
->deferredMoveto
==0 );
5996 assert( !pC
->nullRow
|| pOp
->opcode
==OP_IdxRowid
);
5998 /* The IdxRowid and Seek opcodes are combined because of the commonality
5999 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
6000 rc
= sqlite3VdbeCursorRestore(pC
);
6002 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
6003 ** out from under the cursor. That will never happens for an IdxRowid
6004 ** or Seek opcode */
6005 if( NEVER(rc
!=SQLITE_OK
) ) goto abort_due_to_error
;
6008 rowid
= 0; /* Not needed. Only used to silence a warning. */
6009 rc
= sqlite3VdbeIdxRowid(db
, pC
->uc
.pCursor
, &rowid
);
6010 if( rc
!=SQLITE_OK
){
6011 goto abort_due_to_error
;
6013 if( pOp
->opcode
==OP_DeferredSeek
){
6014 assert( pOp
->p3
>=0 && pOp
->p3
<p
->nCursor
);
6015 pTabCur
= p
->apCsr
[pOp
->p3
];
6016 assert( pTabCur
!=0 );
6017 assert( pTabCur
->eCurType
==CURTYPE_BTREE
);
6018 assert( pTabCur
->uc
.pCursor
!=0 );
6019 assert( pTabCur
->isTable
);
6020 pTabCur
->nullRow
= 0;
6021 pTabCur
->movetoTarget
= rowid
;
6022 pTabCur
->deferredMoveto
= 1;
6023 assert( pOp
->p4type
==P4_INTARRAY
|| pOp
->p4
.ai
==0 );
6024 pTabCur
->aAltMap
= pOp
->p4
.ai
;
6025 assert( !pC
->isEphemeral
);
6026 assert( !pTabCur
->isEphemeral
);
6027 pTabCur
->pAltCursor
= pC
;
6029 pOut
= out2Prerelease(p
, pOp
);
6033 assert( pOp
->opcode
==OP_IdxRowid
);
6034 sqlite3VdbeMemSetNull(&aMem
[pOp
->p2
]);
6039 /* Opcode: FinishSeek P1 * * * *
6041 ** If cursor P1 was previously moved via OP_DeferredSeek, complete that
6042 ** seek operation now, without further delay. If the cursor seek has
6043 ** already occurred, this instruction is a no-op.
6045 case OP_FinishSeek
: {
6046 VdbeCursor
*pC
; /* The P1 index cursor */
6048 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6049 pC
= p
->apCsr
[pOp
->p1
];
6050 if( pC
->deferredMoveto
){
6051 rc
= sqlite3VdbeFinishMoveto(pC
);
6052 if( rc
) goto abort_due_to_error
;
6057 /* Opcode: IdxGE P1 P2 P3 P4 *
6058 ** Synopsis: key=r[P3@P4]
6060 ** The P4 register values beginning with P3 form an unpacked index
6061 ** key that omits the PRIMARY KEY. Compare this key value against the index
6062 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6063 ** fields at the end.
6065 ** If the P1 index entry is greater than or equal to the key value
6066 ** then jump to P2. Otherwise fall through to the next instruction.
6068 /* Opcode: IdxGT P1 P2 P3 P4 *
6069 ** Synopsis: key=r[P3@P4]
6071 ** The P4 register values beginning with P3 form an unpacked index
6072 ** key that omits the PRIMARY KEY. Compare this key value against the index
6073 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6074 ** fields at the end.
6076 ** If the P1 index entry is greater than the key value
6077 ** then jump to P2. Otherwise fall through to the next instruction.
6079 /* Opcode: IdxLT P1 P2 P3 P4 *
6080 ** Synopsis: key=r[P3@P4]
6082 ** The P4 register values beginning with P3 form an unpacked index
6083 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6084 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6085 ** ROWID on the P1 index.
6087 ** If the P1 index entry is less than the key value then jump to P2.
6088 ** Otherwise fall through to the next instruction.
6090 /* Opcode: IdxLE P1 P2 P3 P4 *
6091 ** Synopsis: key=r[P3@P4]
6093 ** The P4 register values beginning with P3 form an unpacked index
6094 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6095 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6096 ** ROWID on the P1 index.
6098 ** If the P1 index entry is less than or equal to the key value then jump
6099 ** to P2. Otherwise fall through to the next instruction.
6101 case OP_IdxLE
: /* jump */
6102 case OP_IdxGT
: /* jump */
6103 case OP_IdxLT
: /* jump */
6104 case OP_IdxGE
: { /* jump */
6109 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6110 pC
= p
->apCsr
[pOp
->p1
];
6112 assert( pC
->isOrdered
);
6113 assert( pC
->eCurType
==CURTYPE_BTREE
);
6114 assert( pC
->uc
.pCursor
!=0);
6115 assert( pC
->deferredMoveto
==0 );
6116 assert( pOp
->p4type
==P4_INT32
);
6117 r
.pKeyInfo
= pC
->pKeyInfo
;
6118 r
.nField
= (u16
)pOp
->p4
.i
;
6119 if( pOp
->opcode
<OP_IdxLT
){
6120 assert( pOp
->opcode
==OP_IdxLE
|| pOp
->opcode
==OP_IdxGT
);
6123 assert( pOp
->opcode
==OP_IdxGE
|| pOp
->opcode
==OP_IdxLT
);
6126 r
.aMem
= &aMem
[pOp
->p3
];
6130 for(i
=0; i
<r
.nField
; i
++){
6131 assert( memIsValid(&r
.aMem
[i
]) );
6132 REGISTER_TRACE(pOp
->p3
+i
, &aMem
[pOp
->p3
+i
]);
6137 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6143 assert( pC
->eCurType
==CURTYPE_BTREE
);
6144 pCur
= pC
->uc
.pCursor
;
6145 assert( sqlite3BtreeCursorIsValid(pCur
) );
6146 nCellKey
= sqlite3BtreePayloadSize(pCur
);
6147 /* nCellKey will always be between 0 and 0xffffffff because of the way
6148 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6149 if( nCellKey
<=0 || nCellKey
>0x7fffffff ){
6150 rc
= SQLITE_CORRUPT_BKPT
;
6151 goto abort_due_to_error
;
6153 sqlite3VdbeMemInit(&m
, db
, 0);
6154 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pCur
, (u32
)nCellKey
, &m
);
6155 if( rc
) goto abort_due_to_error
;
6156 res
= sqlite3VdbeRecordCompareWithSkip(m
.n
, m
.z
, &r
, 0);
6157 sqlite3VdbeMemRelease(&m
);
6159 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6161 assert( (OP_IdxLE
&1)==(OP_IdxLT
&1) && (OP_IdxGE
&1)==(OP_IdxGT
&1) );
6162 if( (pOp
->opcode
&1)==(OP_IdxLT
&1) ){
6163 assert( pOp
->opcode
==OP_IdxLE
|| pOp
->opcode
==OP_IdxLT
);
6166 assert( pOp
->opcode
==OP_IdxGE
|| pOp
->opcode
==OP_IdxGT
);
6169 VdbeBranchTaken(res
>0,2);
6170 assert( rc
==SQLITE_OK
);
6171 if( res
>0 ) goto jump_to_p2
;
6175 /* Opcode: Destroy P1 P2 P3 * *
6177 ** Delete an entire database table or index whose root page in the database
6178 ** file is given by P1.
6180 ** The table being destroyed is in the main database file if P3==0. If
6181 ** P3==1 then the table to be clear is in the auxiliary database file
6182 ** that is used to store tables create using CREATE TEMPORARY TABLE.
6184 ** If AUTOVACUUM is enabled then it is possible that another root page
6185 ** might be moved into the newly deleted root page in order to keep all
6186 ** root pages contiguous at the beginning of the database. The former
6187 ** value of the root page that moved - its value before the move occurred -
6188 ** is stored in register P2. If no page movement was required (because the
6189 ** table being dropped was already the last one in the database) then a
6190 ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6191 ** is stored in register P2.
6193 ** This opcode throws an error if there are any active reader VMs when
6194 ** it is invoked. This is done to avoid the difficulty associated with
6195 ** updating existing cursors when a root page is moved in an AUTOVACUUM
6196 ** database. This error is thrown even if the database is not an AUTOVACUUM
6197 ** db in order to avoid introducing an incompatibility between autovacuum
6198 ** and non-autovacuum modes.
6202 case OP_Destroy
: { /* out2 */
6206 sqlite3VdbeIncrWriteCounter(p
, 0);
6207 assert( p
->readOnly
==0 );
6208 assert( pOp
->p1
>1 );
6209 pOut
= out2Prerelease(p
, pOp
);
6210 pOut
->flags
= MEM_Null
;
6211 if( db
->nVdbeRead
> db
->nVDestroy
+1 ){
6213 p
->errorAction
= OE_Abort
;
6214 goto abort_due_to_error
;
6217 assert( DbMaskTest(p
->btreeMask
, iDb
) );
6218 iMoved
= 0; /* Not needed. Only to silence a warning. */
6219 rc
= sqlite3BtreeDropTable(db
->aDb
[iDb
].pBt
, pOp
->p1
, &iMoved
);
6220 pOut
->flags
= MEM_Int
;
6222 if( rc
) goto abort_due_to_error
;
6223 #ifndef SQLITE_OMIT_AUTOVACUUM
6225 sqlite3RootPageMoved(db
, iDb
, iMoved
, pOp
->p1
);
6226 /* All OP_Destroy operations occur on the same btree */
6227 assert( resetSchemaOnFault
==0 || resetSchemaOnFault
==iDb
+1 );
6228 resetSchemaOnFault
= iDb
+1;
6235 /* Opcode: Clear P1 P2 P3
6237 ** Delete all contents of the database table or index whose root page
6238 ** in the database file is given by P1. But, unlike Destroy, do not
6239 ** remove the table or index from the database file.
6241 ** The table being clear is in the main database file if P2==0. If
6242 ** P2==1 then the table to be clear is in the auxiliary database file
6243 ** that is used to store tables create using CREATE TEMPORARY TABLE.
6245 ** If the P3 value is non-zero, then the row change count is incremented
6246 ** by the number of rows in the table being cleared. If P3 is greater
6247 ** than zero, then the value stored in register P3 is also incremented
6248 ** by the number of rows in the table being cleared.
6250 ** See also: Destroy
6255 sqlite3VdbeIncrWriteCounter(p
, 0);
6257 assert( p
->readOnly
==0 );
6258 assert( DbMaskTest(p
->btreeMask
, pOp
->p2
) );
6259 rc
= sqlite3BtreeClearTable(db
->aDb
[pOp
->p2
].pBt
, (u32
)pOp
->p1
, &nChange
);
6261 p
->nChange
+= nChange
;
6263 assert( memIsValid(&aMem
[pOp
->p3
]) );
6264 memAboutToChange(p
, &aMem
[pOp
->p3
]);
6265 aMem
[pOp
->p3
].u
.i
+= nChange
;
6268 if( rc
) goto abort_due_to_error
;
6272 /* Opcode: ResetSorter P1 * * * *
6274 ** Delete all contents from the ephemeral table or sorter
6275 ** that is open on cursor P1.
6277 ** This opcode only works for cursors used for sorting and
6278 ** opened with OP_OpenEphemeral or OP_SorterOpen.
6280 case OP_ResetSorter
: {
6283 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6284 pC
= p
->apCsr
[pOp
->p1
];
6287 sqlite3VdbeSorterReset(db
, pC
->uc
.pSorter
);
6289 assert( pC
->eCurType
==CURTYPE_BTREE
);
6290 assert( pC
->isEphemeral
);
6291 rc
= sqlite3BtreeClearTableOfCursor(pC
->uc
.pCursor
);
6292 if( rc
) goto abort_due_to_error
;
6297 /* Opcode: CreateBtree P1 P2 P3 * *
6298 ** Synopsis: r[P2]=root iDb=P1 flags=P3
6300 ** Allocate a new b-tree in the main database file if P1==0 or in the
6301 ** TEMP database file if P1==1 or in an attached database if
6302 ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
6303 ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
6304 ** The root page number of the new b-tree is stored in register P2.
6306 case OP_CreateBtree
: { /* out2 */
6310 sqlite3VdbeIncrWriteCounter(p
, 0);
6311 pOut
= out2Prerelease(p
, pOp
);
6313 assert( pOp
->p3
==BTREE_INTKEY
|| pOp
->p3
==BTREE_BLOBKEY
);
6314 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
6315 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
6316 assert( p
->readOnly
==0 );
6317 pDb
= &db
->aDb
[pOp
->p1
];
6318 assert( pDb
->pBt
!=0 );
6319 rc
= sqlite3BtreeCreateTable(pDb
->pBt
, &pgno
, pOp
->p3
);
6320 if( rc
) goto abort_due_to_error
;
6325 /* Opcode: SqlExec * * * P4 *
6327 ** Run the SQL statement or statements specified in the P4 string.
6330 sqlite3VdbeIncrWriteCounter(p
, 0);
6332 rc
= sqlite3_exec(db
, pOp
->p4
.z
, 0, 0, 0);
6334 if( rc
) goto abort_due_to_error
;
6338 /* Opcode: ParseSchema P1 * * P4 *
6340 ** Read and parse all entries from the schema table of database P1
6341 ** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6342 ** entire schema for P1 is reparsed.
6344 ** This opcode invokes the parser to create a new virtual machine,
6345 ** then runs the new virtual machine. It is thus a re-entrant opcode.
6347 case OP_ParseSchema
: {
6349 const char *zSchema
;
6353 /* Any prepared statement that invokes this opcode will hold mutexes
6354 ** on every btree. This is a prerequisite for invoking
6355 ** sqlite3InitCallback().
6358 for(iDb
=0; iDb
<db
->nDb
; iDb
++){
6359 assert( iDb
==1 || sqlite3BtreeHoldsMutex(db
->aDb
[iDb
].pBt
) );
6364 assert( iDb
>=0 && iDb
<db
->nDb
);
6365 assert( DbHasProperty(db
, iDb
, DB_SchemaLoaded
)
6367 || (CORRUPT_DB
&& (db
->flags
& SQLITE_NoSchemaError
)!=0) );
6369 #ifndef SQLITE_OMIT_ALTERTABLE
6371 sqlite3SchemaClear(db
->aDb
[iDb
].pSchema
);
6372 db
->mDbFlags
&= ~DBFLAG_SchemaKnownOk
;
6373 rc
= sqlite3InitOne(db
, iDb
, &p
->zErrMsg
, pOp
->p5
);
6374 db
->mDbFlags
|= DBFLAG_SchemaChange
;
6379 zSchema
= DFLT_SCHEMA_TABLE
;
6382 initData
.pzErrMsg
= &p
->zErrMsg
;
6383 initData
.mInitFlags
= 0;
6384 initData
.mxPage
= sqlite3BtreeLastPage(db
->aDb
[iDb
].pBt
);
6385 zSql
= sqlite3MPrintf(db
,
6386 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
6387 db
->aDb
[iDb
].zDbSName
, zSchema
, pOp
->p4
.z
);
6389 rc
= SQLITE_NOMEM_BKPT
;
6391 assert( db
->init
.busy
==0 );
6393 initData
.rc
= SQLITE_OK
;
6394 initData
.nInitRow
= 0;
6395 assert( !db
->mallocFailed
);
6396 rc
= sqlite3_exec(db
, zSql
, sqlite3InitCallback
, &initData
, 0);
6397 if( rc
==SQLITE_OK
) rc
= initData
.rc
;
6398 if( rc
==SQLITE_OK
&& initData
.nInitRow
==0 ){
6399 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6400 ** at least one SQL statement. Any less than that indicates that
6401 ** the sqlite_schema table is corrupt. */
6402 rc
= SQLITE_CORRUPT_BKPT
;
6404 sqlite3DbFreeNN(db
, zSql
);
6409 sqlite3ResetAllSchemasOfConnection(db
);
6410 if( rc
==SQLITE_NOMEM
){
6413 goto abort_due_to_error
;
6418 #if !defined(SQLITE_OMIT_ANALYZE)
6419 /* Opcode: LoadAnalysis P1 * * * *
6421 ** Read the sqlite_stat1 table for database P1 and load the content
6422 ** of that table into the internal index hash table. This will cause
6423 ** the analysis to be used when preparing all subsequent queries.
6425 case OP_LoadAnalysis
: {
6426 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
6427 rc
= sqlite3AnalysisLoad(db
, pOp
->p1
);
6428 if( rc
) goto abort_due_to_error
;
6431 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
6433 /* Opcode: DropTable P1 * * P4 *
6435 ** Remove the internal (in-memory) data structures that describe
6436 ** the table named P4 in database P1. This is called after a table
6437 ** is dropped from disk (using the Destroy opcode) in order to keep
6438 ** the internal representation of the
6439 ** schema consistent with what is on disk.
6441 case OP_DropTable
: {
6442 sqlite3VdbeIncrWriteCounter(p
, 0);
6443 sqlite3UnlinkAndDeleteTable(db
, pOp
->p1
, pOp
->p4
.z
);
6447 /* Opcode: DropIndex P1 * * P4 *
6449 ** Remove the internal (in-memory) data structures that describe
6450 ** the index named P4 in database P1. This is called after an index
6451 ** is dropped from disk (using the Destroy opcode)
6452 ** in order to keep the internal representation of the
6453 ** schema consistent with what is on disk.
6455 case OP_DropIndex
: {
6456 sqlite3VdbeIncrWriteCounter(p
, 0);
6457 sqlite3UnlinkAndDeleteIndex(db
, pOp
->p1
, pOp
->p4
.z
);
6461 /* Opcode: DropTrigger P1 * * P4 *
6463 ** Remove the internal (in-memory) data structures that describe
6464 ** the trigger named P4 in database P1. This is called after a trigger
6465 ** is dropped from disk (using the Destroy opcode) in order to keep
6466 ** the internal representation of the
6467 ** schema consistent with what is on disk.
6469 case OP_DropTrigger
: {
6470 sqlite3VdbeIncrWriteCounter(p
, 0);
6471 sqlite3UnlinkAndDeleteTrigger(db
, pOp
->p1
, pOp
->p4
.z
);
6476 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
6477 /* Opcode: IntegrityCk P1 P2 P3 P4 P5
6479 ** Do an analysis of the currently open database. Store in
6480 ** register P1 the text of an error message describing any problems.
6481 ** If no problems are found, store a NULL in register P1.
6483 ** The register P3 contains one less than the maximum number of allowed errors.
6484 ** At most reg(P3) errors will be reported.
6485 ** In other words, the analysis stops as soon as reg(P1) errors are
6486 ** seen. Reg(P1) is updated with the number of errors remaining.
6488 ** The root page numbers of all tables in the database are integers
6489 ** stored in P4_INTARRAY argument.
6491 ** If P5 is not zero, the check is done on the auxiliary database
6492 ** file, not the main database file.
6494 ** This opcode is used to implement the integrity_check pragma.
6496 case OP_IntegrityCk
: {
6497 int nRoot
; /* Number of tables to check. (Number of root pages.) */
6498 Pgno
*aRoot
; /* Array of rootpage numbers for tables to be checked */
6499 int nErr
; /* Number of errors reported */
6500 char *z
; /* Text of the error report */
6501 Mem
*pnErr
; /* Register keeping track of errors remaining */
6503 assert( p
->bIsReader
);
6507 assert( aRoot
[0]==(Pgno
)nRoot
);
6508 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
6509 pnErr
= &aMem
[pOp
->p3
];
6510 assert( (pnErr
->flags
& MEM_Int
)!=0 );
6511 assert( (pnErr
->flags
& (MEM_Str
|MEM_Blob
))==0 );
6512 pIn1
= &aMem
[pOp
->p1
];
6513 assert( pOp
->p5
<db
->nDb
);
6514 assert( DbMaskTest(p
->btreeMask
, pOp
->p5
) );
6515 z
= sqlite3BtreeIntegrityCheck(db
, db
->aDb
[pOp
->p5
].pBt
, &aRoot
[1], nRoot
,
6516 (int)pnErr
->u
.i
+1, &nErr
);
6517 sqlite3VdbeMemSetNull(pIn1
);
6523 pnErr
->u
.i
-= nErr
-1;
6524 sqlite3VdbeMemSetStr(pIn1
, z
, -1, SQLITE_UTF8
, sqlite3_free
);
6526 UPDATE_MAX_BLOBSIZE(pIn1
);
6527 sqlite3VdbeChangeEncoding(pIn1
, encoding
);
6528 goto check_for_interrupt
;
6530 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6532 /* Opcode: RowSetAdd P1 P2 * * *
6533 ** Synopsis: rowset(P1)=r[P2]
6535 ** Insert the integer value held by register P2 into a RowSet object
6536 ** held in register P1.
6538 ** An assertion fails if P2 is not an integer.
6540 case OP_RowSetAdd
: { /* in1, in2 */
6541 pIn1
= &aMem
[pOp
->p1
];
6542 pIn2
= &aMem
[pOp
->p2
];
6543 assert( (pIn2
->flags
& MEM_Int
)!=0 );
6544 if( (pIn1
->flags
& MEM_Blob
)==0 ){
6545 if( sqlite3VdbeMemSetRowSet(pIn1
) ) goto no_mem
;
6547 assert( sqlite3VdbeMemIsRowSet(pIn1
) );
6548 sqlite3RowSetInsert((RowSet
*)pIn1
->z
, pIn2
->u
.i
);
6552 /* Opcode: RowSetRead P1 P2 P3 * *
6553 ** Synopsis: r[P3]=rowset(P1)
6555 ** Extract the smallest value from the RowSet object in P1
6556 ** and put that value into register P3.
6557 ** Or, if RowSet object P1 is initially empty, leave P3
6558 ** unchanged and jump to instruction P2.
6560 case OP_RowSetRead
: { /* jump, in1, out3 */
6563 pIn1
= &aMem
[pOp
->p1
];
6564 assert( (pIn1
->flags
& MEM_Blob
)==0 || sqlite3VdbeMemIsRowSet(pIn1
) );
6565 if( (pIn1
->flags
& MEM_Blob
)==0
6566 || sqlite3RowSetNext((RowSet
*)pIn1
->z
, &val
)==0
6568 /* The boolean index is empty */
6569 sqlite3VdbeMemSetNull(pIn1
);
6570 VdbeBranchTaken(1,2);
6571 goto jump_to_p2_and_check_for_interrupt
;
6573 /* A value was pulled from the index */
6574 VdbeBranchTaken(0,2);
6575 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p3
], val
);
6577 goto check_for_interrupt
;
6580 /* Opcode: RowSetTest P1 P2 P3 P4
6581 ** Synopsis: if r[P3] in rowset(P1) goto P2
6583 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
6584 ** contains a RowSet object and that RowSet object contains
6585 ** the value held in P3, jump to register P2. Otherwise, insert the
6586 ** integer in P3 into the RowSet and continue on to the
6589 ** The RowSet object is optimized for the case where sets of integers
6590 ** are inserted in distinct phases, which each set contains no duplicates.
6591 ** Each set is identified by a unique P4 value. The first set
6592 ** must have P4==0, the final set must have P4==-1, and for all other sets
6595 ** This allows optimizations: (a) when P4==0 there is no need to test
6596 ** the RowSet object for P3, as it is guaranteed not to contain it,
6597 ** (b) when P4==-1 there is no need to insert the value, as it will
6598 ** never be tested for, and (c) when a value that is part of set X is
6599 ** inserted, there is no need to search to see if the same value was
6600 ** previously inserted as part of set X (only if it was previously
6601 ** inserted as part of some other set).
6603 case OP_RowSetTest
: { /* jump, in1, in3 */
6607 pIn1
= &aMem
[pOp
->p1
];
6608 pIn3
= &aMem
[pOp
->p3
];
6610 assert( pIn3
->flags
&MEM_Int
);
6612 /* If there is anything other than a rowset object in memory cell P1,
6613 ** delete it now and initialize P1 with an empty rowset
6615 if( (pIn1
->flags
& MEM_Blob
)==0 ){
6616 if( sqlite3VdbeMemSetRowSet(pIn1
) ) goto no_mem
;
6618 assert( sqlite3VdbeMemIsRowSet(pIn1
) );
6619 assert( pOp
->p4type
==P4_INT32
);
6620 assert( iSet
==-1 || iSet
>=0 );
6622 exists
= sqlite3RowSetTest((RowSet
*)pIn1
->z
, iSet
, pIn3
->u
.i
);
6623 VdbeBranchTaken(exists
!=0,2);
6624 if( exists
) goto jump_to_p2
;
6627 sqlite3RowSetInsert((RowSet
*)pIn1
->z
, pIn3
->u
.i
);
6633 #ifndef SQLITE_OMIT_TRIGGER
6635 /* Opcode: Program P1 P2 P3 P4 P5
6637 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
6639 ** P1 contains the address of the memory cell that contains the first memory
6640 ** cell in an array of values used as arguments to the sub-program. P2
6641 ** contains the address to jump to if the sub-program throws an IGNORE
6642 ** exception using the RAISE() function. Register P3 contains the address
6643 ** of a memory cell in this (the parent) VM that is used to allocate the
6644 ** memory required by the sub-vdbe at runtime.
6646 ** P4 is a pointer to the VM containing the trigger program.
6648 ** If P5 is non-zero, then recursive program invocation is enabled.
6650 case OP_Program
: { /* jump */
6651 int nMem
; /* Number of memory registers for sub-program */
6652 int nByte
; /* Bytes of runtime space required for sub-program */
6653 Mem
*pRt
; /* Register to allocate runtime space */
6654 Mem
*pMem
; /* Used to iterate through memory cells */
6655 Mem
*pEnd
; /* Last memory cell in new array */
6656 VdbeFrame
*pFrame
; /* New vdbe frame to execute in */
6657 SubProgram
*pProgram
; /* Sub-program to execute */
6658 void *t
; /* Token identifying trigger */
6660 pProgram
= pOp
->p4
.pProgram
;
6661 pRt
= &aMem
[pOp
->p3
];
6662 assert( pProgram
->nOp
>0 );
6664 /* If the p5 flag is clear, then recursive invocation of triggers is
6665 ** disabled for backwards compatibility (p5 is set if this sub-program
6666 ** is really a trigger, not a foreign key action, and the flag set
6667 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
6669 ** It is recursive invocation of triggers, at the SQL level, that is
6670 ** disabled. In some cases a single trigger may generate more than one
6671 ** SubProgram (if the trigger may be executed with more than one different
6672 ** ON CONFLICT algorithm). SubProgram structures associated with a
6673 ** single trigger all have the same value for the SubProgram.token
6676 t
= pProgram
->token
;
6677 for(pFrame
=p
->pFrame
; pFrame
&& pFrame
->token
!=t
; pFrame
=pFrame
->pParent
);
6681 if( p
->nFrame
>=db
->aLimit
[SQLITE_LIMIT_TRIGGER_DEPTH
] ){
6683 sqlite3VdbeError(p
, "too many levels of trigger recursion");
6684 goto abort_due_to_error
;
6687 /* Register pRt is used to store the memory required to save the state
6688 ** of the current program, and the memory required at runtime to execute
6689 ** the trigger program. If this trigger has been fired before, then pRt
6690 ** is already allocated. Otherwise, it must be initialized. */
6691 if( (pRt
->flags
&MEM_Blob
)==0 ){
6692 /* SubProgram.nMem is set to the number of memory cells used by the
6693 ** program stored in SubProgram.aOp. As well as these, one memory
6694 ** cell is required for each cursor used by the program. Set local
6695 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6697 nMem
= pProgram
->nMem
+ pProgram
->nCsr
;
6699 if( pProgram
->nCsr
==0 ) nMem
++;
6700 nByte
= ROUND8(sizeof(VdbeFrame
))
6701 + nMem
* sizeof(Mem
)
6702 + pProgram
->nCsr
* sizeof(VdbeCursor
*)
6703 + (pProgram
->nOp
+ 7)/8;
6704 pFrame
= sqlite3DbMallocZero(db
, nByte
);
6708 sqlite3VdbeMemRelease(pRt
);
6709 pRt
->flags
= MEM_Blob
|MEM_Dyn
;
6710 pRt
->z
= (char*)pFrame
;
6712 pRt
->xDel
= sqlite3VdbeFrameMemDel
;
6715 pFrame
->nChildMem
= nMem
;
6716 pFrame
->nChildCsr
= pProgram
->nCsr
;
6717 pFrame
->pc
= (int)(pOp
- aOp
);
6718 pFrame
->aMem
= p
->aMem
;
6719 pFrame
->nMem
= p
->nMem
;
6720 pFrame
->apCsr
= p
->apCsr
;
6721 pFrame
->nCursor
= p
->nCursor
;
6722 pFrame
->aOp
= p
->aOp
;
6723 pFrame
->nOp
= p
->nOp
;
6724 pFrame
->token
= pProgram
->token
;
6725 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
6726 pFrame
->anExec
= p
->anExec
;
6729 pFrame
->iFrameMagic
= SQLITE_FRAME_MAGIC
;
6732 pEnd
= &VdbeFrameMem(pFrame
)[pFrame
->nChildMem
];
6733 for(pMem
=VdbeFrameMem(pFrame
); pMem
!=pEnd
; pMem
++){
6734 pMem
->flags
= MEM_Undefined
;
6738 pFrame
= (VdbeFrame
*)pRt
->z
;
6739 assert( pRt
->xDel
==sqlite3VdbeFrameMemDel
);
6740 assert( pProgram
->nMem
+pProgram
->nCsr
==pFrame
->nChildMem
6741 || (pProgram
->nCsr
==0 && pProgram
->nMem
+1==pFrame
->nChildMem
) );
6742 assert( pProgram
->nCsr
==pFrame
->nChildCsr
);
6743 assert( (int)(pOp
- aOp
)==pFrame
->pc
);
6747 pFrame
->pParent
= p
->pFrame
;
6748 pFrame
->lastRowid
= db
->lastRowid
;
6749 pFrame
->nChange
= p
->nChange
;
6750 pFrame
->nDbChange
= p
->db
->nChange
;
6751 assert( pFrame
->pAuxData
==0 );
6752 pFrame
->pAuxData
= p
->pAuxData
;
6756 p
->aMem
= aMem
= VdbeFrameMem(pFrame
);
6757 p
->nMem
= pFrame
->nChildMem
;
6758 p
->nCursor
= (u16
)pFrame
->nChildCsr
;
6759 p
->apCsr
= (VdbeCursor
**)&aMem
[p
->nMem
];
6760 pFrame
->aOnce
= (u8
*)&p
->apCsr
[pProgram
->nCsr
];
6761 memset(pFrame
->aOnce
, 0, (pProgram
->nOp
+ 7)/8);
6762 p
->aOp
= aOp
= pProgram
->aOp
;
6763 p
->nOp
= pProgram
->nOp
;
6764 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
6768 /* Verify that second and subsequent executions of the same trigger do not
6769 ** try to reuse register values from the first use. */
6772 for(i
=0; i
<p
->nMem
; i
++){
6773 aMem
[i
].pScopyFrom
= 0; /* Prevent false-positive AboutToChange() errs */
6774 MemSetTypeFlag(&aMem
[i
], MEM_Undefined
); /* Fault if this reg is reused */
6779 goto check_for_interrupt
;
6782 /* Opcode: Param P1 P2 * * *
6784 ** This opcode is only ever present in sub-programs called via the
6785 ** OP_Program instruction. Copy a value currently stored in a memory
6786 ** cell of the calling (parent) frame to cell P2 in the current frames
6787 ** address space. This is used by trigger programs to access the new.*
6788 ** and old.* values.
6790 ** The address of the cell in the parent frame is determined by adding
6791 ** the value of the P1 argument to the value of the P1 argument to the
6792 ** calling OP_Program instruction.
6794 case OP_Param
: { /* out2 */
6797 pOut
= out2Prerelease(p
, pOp
);
6799 pIn
= &pFrame
->aMem
[pOp
->p1
+ pFrame
->aOp
[pFrame
->pc
].p1
];
6800 sqlite3VdbeMemShallowCopy(pOut
, pIn
, MEM_Ephem
);
6804 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
6806 #ifndef SQLITE_OMIT_FOREIGN_KEY
6807 /* Opcode: FkCounter P1 P2 * * *
6808 ** Synopsis: fkctr[P1]+=P2
6810 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6811 ** If P1 is non-zero, the database constraint counter is incremented
6812 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
6813 ** statement counter is incremented (immediate foreign key constraints).
6815 case OP_FkCounter
: {
6816 if( db
->flags
& SQLITE_DeferFKs
){
6817 db
->nDeferredImmCons
+= pOp
->p2
;
6818 }else if( pOp
->p1
){
6819 db
->nDeferredCons
+= pOp
->p2
;
6821 p
->nFkConstraint
+= pOp
->p2
;
6826 /* Opcode: FkIfZero P1 P2 * * *
6827 ** Synopsis: if fkctr[P1]==0 goto P2
6829 ** This opcode tests if a foreign key constraint-counter is currently zero.
6830 ** If so, jump to instruction P2. Otherwise, fall through to the next
6833 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
6834 ** is zero (the one that counts deferred constraint violations). If P1 is
6835 ** zero, the jump is taken if the statement constraint-counter is zero
6836 ** (immediate foreign key constraint violations).
6838 case OP_FkIfZero
: { /* jump */
6840 VdbeBranchTaken(db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0, 2);
6841 if( db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0 ) goto jump_to_p2
;
6843 VdbeBranchTaken(p
->nFkConstraint
==0 && db
->nDeferredImmCons
==0, 2);
6844 if( p
->nFkConstraint
==0 && db
->nDeferredImmCons
==0 ) goto jump_to_p2
;
6848 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6850 #ifndef SQLITE_OMIT_AUTOINCREMENT
6851 /* Opcode: MemMax P1 P2 * * *
6852 ** Synopsis: r[P1]=max(r[P1],r[P2])
6854 ** P1 is a register in the root frame of this VM (the root frame is
6855 ** different from the current frame if this instruction is being executed
6856 ** within a sub-program). Set the value of register P1 to the maximum of
6857 ** its current value and the value in register P2.
6859 ** This instruction throws an error if the memory cell is not initially
6862 case OP_MemMax
: { /* in2 */
6865 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
6866 pIn1
= &pFrame
->aMem
[pOp
->p1
];
6868 pIn1
= &aMem
[pOp
->p1
];
6870 assert( memIsValid(pIn1
) );
6871 sqlite3VdbeMemIntegerify(pIn1
);
6872 pIn2
= &aMem
[pOp
->p2
];
6873 sqlite3VdbeMemIntegerify(pIn2
);
6874 if( pIn1
->u
.i
<pIn2
->u
.i
){
6875 pIn1
->u
.i
= pIn2
->u
.i
;
6879 #endif /* SQLITE_OMIT_AUTOINCREMENT */
6881 /* Opcode: IfPos P1 P2 P3 * *
6882 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
6884 ** Register P1 must contain an integer.
6885 ** If the value of register P1 is 1 or greater, subtract P3 from the
6886 ** value in P1 and jump to P2.
6888 ** If the initial value of register P1 is less than 1, then the
6889 ** value is unchanged and control passes through to the next instruction.
6891 case OP_IfPos
: { /* jump, in1 */
6892 pIn1
= &aMem
[pOp
->p1
];
6893 assert( pIn1
->flags
&MEM_Int
);
6894 VdbeBranchTaken( pIn1
->u
.i
>0, 2);
6896 pIn1
->u
.i
-= pOp
->p3
;
6902 /* Opcode: OffsetLimit P1 P2 P3 * *
6903 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
6905 ** This opcode performs a commonly used computation associated with
6906 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6907 ** holds the offset counter. The opcode computes the combined value
6908 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6909 ** value computed is the total number of rows that will need to be
6910 ** visited in order to complete the query.
6912 ** If r[P3] is zero or negative, that means there is no OFFSET
6913 ** and r[P2] is set to be the value of the LIMIT, r[P1].
6915 ** if r[P1] is zero or negative, that means there is no LIMIT
6916 ** and r[P2] is set to -1.
6918 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
6920 case OP_OffsetLimit
: { /* in1, out2, in3 */
6922 pIn1
= &aMem
[pOp
->p1
];
6923 pIn3
= &aMem
[pOp
->p3
];
6924 pOut
= out2Prerelease(p
, pOp
);
6925 assert( pIn1
->flags
& MEM_Int
);
6926 assert( pIn3
->flags
& MEM_Int
);
6928 if( x
<=0 || sqlite3AddInt64(&x
, pIn3
->u
.i
>0?pIn3
->u
.i
:0) ){
6929 /* If the LIMIT is less than or equal to zero, loop forever. This
6930 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6931 ** also loop forever. This is undocumented. In fact, one could argue
6932 ** that the loop should terminate. But assuming 1 billion iterations
6933 ** per second (far exceeding the capabilities of any current hardware)
6934 ** it would take nearly 300 years to actually reach the limit. So
6935 ** looping forever is a reasonable approximation. */
6943 /* Opcode: IfNotZero P1 P2 * * *
6944 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
6946 ** Register P1 must contain an integer. If the content of register P1 is
6947 ** initially greater than zero, then decrement the value in register P1.
6948 ** If it is non-zero (negative or positive) and then also jump to P2.
6949 ** If register P1 is initially zero, leave it unchanged and fall through.
6951 case OP_IfNotZero
: { /* jump, in1 */
6952 pIn1
= &aMem
[pOp
->p1
];
6953 assert( pIn1
->flags
&MEM_Int
);
6954 VdbeBranchTaken(pIn1
->u
.i
<0, 2);
6956 if( pIn1
->u
.i
>0 ) pIn1
->u
.i
--;
6962 /* Opcode: DecrJumpZero P1 P2 * * *
6963 ** Synopsis: if (--r[P1])==0 goto P2
6965 ** Register P1 must hold an integer. Decrement the value in P1
6966 ** and jump to P2 if the new value is exactly zero.
6968 case OP_DecrJumpZero
: { /* jump, in1 */
6969 pIn1
= &aMem
[pOp
->p1
];
6970 assert( pIn1
->flags
&MEM_Int
);
6971 if( pIn1
->u
.i
>SMALLEST_INT64
) pIn1
->u
.i
--;
6972 VdbeBranchTaken(pIn1
->u
.i
==0, 2);
6973 if( pIn1
->u
.i
==0 ) goto jump_to_p2
;
6978 /* Opcode: AggStep * P2 P3 P4 P5
6979 ** Synopsis: accum=r[P3] step(r[P2@P5])
6981 ** Execute the xStep function for an aggregate.
6982 ** The function has P5 arguments. P4 is a pointer to the
6983 ** FuncDef structure that specifies the function. Register P3 is the
6986 ** The P5 arguments are taken from register P2 and its
6989 /* Opcode: AggInverse * P2 P3 P4 P5
6990 ** Synopsis: accum=r[P3] inverse(r[P2@P5])
6992 ** Execute the xInverse function for an aggregate.
6993 ** The function has P5 arguments. P4 is a pointer to the
6994 ** FuncDef structure that specifies the function. Register P3 is the
6997 ** The P5 arguments are taken from register P2 and its
7000 /* Opcode: AggStep1 P1 P2 P3 P4 P5
7001 ** Synopsis: accum=r[P3] step(r[P2@P5])
7003 ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
7004 ** aggregate. The function has P5 arguments. P4 is a pointer to the
7005 ** FuncDef structure that specifies the function. Register P3 is the
7008 ** The P5 arguments are taken from register P2 and its
7011 ** This opcode is initially coded as OP_AggStep0. On first evaluation,
7012 ** the FuncDef stored in P4 is converted into an sqlite3_context and
7013 ** the opcode is changed. In this way, the initialization of the
7014 ** sqlite3_context only happens once, instead of on each call to the
7020 sqlite3_context
*pCtx
;
7022 assert( pOp
->p4type
==P4_FUNCDEF
);
7024 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
7025 assert( n
==0 || (pOp
->p2
>0 && pOp
->p2
+n
<=(p
->nMem
+1 - p
->nCursor
)+1) );
7026 assert( pOp
->p3
<pOp
->p2
|| pOp
->p3
>=pOp
->p2
+n
);
7027 pCtx
= sqlite3DbMallocRawNN(db
, n
*sizeof(sqlite3_value
*) +
7028 (sizeof(pCtx
[0]) + sizeof(Mem
) - sizeof(sqlite3_value
*)));
7029 if( pCtx
==0 ) goto no_mem
;
7031 pCtx
->pOut
= (Mem
*)&(pCtx
->argv
[n
]);
7032 sqlite3VdbeMemInit(pCtx
->pOut
, db
, MEM_Null
);
7033 pCtx
->pFunc
= pOp
->p4
.pFunc
;
7034 pCtx
->iOp
= (int)(pOp
- aOp
);
7039 pOp
->p4type
= P4_FUNCCTX
;
7040 pOp
->p4
.pCtx
= pCtx
;
7042 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
7043 assert( pOp
->p1
==(pOp
->opcode
==OP_AggInverse
) );
7045 pOp
->opcode
= OP_AggStep1
;
7046 /* Fall through into OP_AggStep */
7047 /* no break */ deliberate_fall_through
7051 sqlite3_context
*pCtx
;
7054 assert( pOp
->p4type
==P4_FUNCCTX
);
7055 pCtx
= pOp
->p4
.pCtx
;
7056 pMem
= &aMem
[pOp
->p3
];
7060 /* This is an OP_AggInverse call. Verify that xStep has always
7061 ** been called at least once prior to any xInverse call. */
7062 assert( pMem
->uTemp
==0x1122e0e3 );
7064 /* This is an OP_AggStep call. Mark it as such. */
7065 pMem
->uTemp
= 0x1122e0e3;
7069 /* If this function is inside of a trigger, the register array in aMem[]
7070 ** might change from one evaluation to the next. The next block of code
7071 ** checks to see if the register array has changed, and if so it
7072 ** reinitializes the relavant parts of the sqlite3_context object */
7073 if( pCtx
->pMem
!= pMem
){
7075 for(i
=pCtx
->argc
-1; i
>=0; i
--) pCtx
->argv
[i
] = &aMem
[pOp
->p2
+i
];
7079 for(i
=0; i
<pCtx
->argc
; i
++){
7080 assert( memIsValid(pCtx
->argv
[i
]) );
7081 REGISTER_TRACE(pOp
->p2
+i
, pCtx
->argv
[i
]);
7086 assert( pCtx
->pOut
->flags
==MEM_Null
);
7087 assert( pCtx
->isError
==0 );
7088 assert( pCtx
->skipFlag
==0 );
7089 #ifndef SQLITE_OMIT_WINDOWFUNC
7091 (pCtx
->pFunc
->xInverse
)(pCtx
,pCtx
->argc
,pCtx
->argv
);
7094 (pCtx
->pFunc
->xSFunc
)(pCtx
,pCtx
->argc
,pCtx
->argv
); /* IMP: R-24505-23230 */
7096 if( pCtx
->isError
){
7097 if( pCtx
->isError
>0 ){
7098 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pCtx
->pOut
));
7101 if( pCtx
->skipFlag
){
7102 assert( pOp
[-1].opcode
==OP_CollSeq
);
7104 if( i
) sqlite3VdbeMemSetInt64(&aMem
[i
], 1);
7107 sqlite3VdbeMemRelease(pCtx
->pOut
);
7108 pCtx
->pOut
->flags
= MEM_Null
;
7110 if( rc
) goto abort_due_to_error
;
7112 assert( pCtx
->pOut
->flags
==MEM_Null
);
7113 assert( pCtx
->skipFlag
==0 );
7117 /* Opcode: AggFinal P1 P2 * P4 *
7118 ** Synopsis: accum=r[P1] N=P2
7120 ** P1 is the memory location that is the accumulator for an aggregate
7121 ** or window function. Execute the finalizer function
7122 ** for an aggregate and store the result in P1.
7124 ** P2 is the number of arguments that the step function takes and
7125 ** P4 is a pointer to the FuncDef for this function. The P2
7126 ** argument is not used by this opcode. It is only there to disambiguate
7127 ** functions that can take varying numbers of arguments. The
7128 ** P4 argument is only needed for the case where
7129 ** the step function was not previously called.
7131 /* Opcode: AggValue * P2 P3 P4 *
7132 ** Synopsis: r[P3]=value N=P2
7134 ** Invoke the xValue() function and store the result in register P3.
7136 ** P2 is the number of arguments that the step function takes and
7137 ** P4 is a pointer to the FuncDef for this function. The P2
7138 ** argument is not used by this opcode. It is only there to disambiguate
7139 ** functions that can take varying numbers of arguments. The
7140 ** P4 argument is only needed for the case where
7141 ** the step function was not previously called.
7146 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
7147 assert( pOp
->p3
==0 || pOp
->opcode
==OP_AggValue
);
7148 pMem
= &aMem
[pOp
->p1
];
7149 assert( (pMem
->flags
& ~(MEM_Null
|MEM_Agg
))==0 );
7150 #ifndef SQLITE_OMIT_WINDOWFUNC
7152 memAboutToChange(p
, &aMem
[pOp
->p3
]);
7153 rc
= sqlite3VdbeMemAggValue(pMem
, &aMem
[pOp
->p3
], pOp
->p4
.pFunc
);
7154 pMem
= &aMem
[pOp
->p3
];
7158 rc
= sqlite3VdbeMemFinalize(pMem
, pOp
->p4
.pFunc
);
7162 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pMem
));
7163 goto abort_due_to_error
;
7165 sqlite3VdbeChangeEncoding(pMem
, encoding
);
7166 UPDATE_MAX_BLOBSIZE(pMem
);
7167 if( sqlite3VdbeMemTooBig(pMem
) ){
7173 #ifndef SQLITE_OMIT_WAL
7174 /* Opcode: Checkpoint P1 P2 P3 * *
7176 ** Checkpoint database P1. This is a no-op if P1 is not currently in
7177 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7178 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
7179 ** SQLITE_BUSY or not, respectively. Write the number of pages in the
7180 ** WAL after the checkpoint into mem[P3+1] and the number of pages
7181 ** in the WAL that have been checkpointed after the checkpoint
7182 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
7183 ** mem[P3+2] are initialized to -1.
7185 case OP_Checkpoint
: {
7186 int i
; /* Loop counter */
7187 int aRes
[3]; /* Results */
7188 Mem
*pMem
; /* Write results here */
7190 assert( p
->readOnly
==0 );
7192 aRes
[1] = aRes
[2] = -1;
7193 assert( pOp
->p2
==SQLITE_CHECKPOINT_PASSIVE
7194 || pOp
->p2
==SQLITE_CHECKPOINT_FULL
7195 || pOp
->p2
==SQLITE_CHECKPOINT_RESTART
7196 || pOp
->p2
==SQLITE_CHECKPOINT_TRUNCATE
7198 rc
= sqlite3Checkpoint(db
, pOp
->p1
, pOp
->p2
, &aRes
[1], &aRes
[2]);
7200 if( rc
!=SQLITE_BUSY
) goto abort_due_to_error
;
7204 for(i
=0, pMem
= &aMem
[pOp
->p3
]; i
<3; i
++, pMem
++){
7205 sqlite3VdbeMemSetInt64(pMem
, (i64
)aRes
[i
]);
7211 #ifndef SQLITE_OMIT_PRAGMA
7212 /* Opcode: JournalMode P1 P2 P3 * *
7214 ** Change the journal mode of database P1 to P3. P3 must be one of the
7215 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7216 ** modes (delete, truncate, persist, off and memory), this is a simple
7217 ** operation. No IO is required.
7219 ** If changing into or out of WAL mode the procedure is more complicated.
7221 ** Write a string containing the final journal-mode to register P2.
7223 case OP_JournalMode
: { /* out2 */
7224 Btree
*pBt
; /* Btree to change journal mode of */
7225 Pager
*pPager
; /* Pager associated with pBt */
7226 int eNew
; /* New journal mode */
7227 int eOld
; /* The old journal mode */
7228 #ifndef SQLITE_OMIT_WAL
7229 const char *zFilename
; /* Name of database file for pPager */
7232 pOut
= out2Prerelease(p
, pOp
);
7234 assert( eNew
==PAGER_JOURNALMODE_DELETE
7235 || eNew
==PAGER_JOURNALMODE_TRUNCATE
7236 || eNew
==PAGER_JOURNALMODE_PERSIST
7237 || eNew
==PAGER_JOURNALMODE_OFF
7238 || eNew
==PAGER_JOURNALMODE_MEMORY
7239 || eNew
==PAGER_JOURNALMODE_WAL
7240 || eNew
==PAGER_JOURNALMODE_QUERY
7242 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
7243 assert( p
->readOnly
==0 );
7245 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7246 pPager
= sqlite3BtreePager(pBt
);
7247 eOld
= sqlite3PagerGetJournalMode(pPager
);
7248 if( eNew
==PAGER_JOURNALMODE_QUERY
) eNew
= eOld
;
7249 assert( sqlite3BtreeHoldsMutex(pBt
) );
7250 if( !sqlite3PagerOkToChangeJournalMode(pPager
) ) eNew
= eOld
;
7252 #ifndef SQLITE_OMIT_WAL
7253 zFilename
= sqlite3PagerFilename(pPager
, 1);
7255 /* Do not allow a transition to journal_mode=WAL for a database
7256 ** in temporary storage or if the VFS does not support shared memory
7258 if( eNew
==PAGER_JOURNALMODE_WAL
7259 && (sqlite3Strlen30(zFilename
)==0 /* Temp file */
7260 || !sqlite3PagerWalSupported(pPager
)) /* No shared-memory support */
7266 && (eOld
==PAGER_JOURNALMODE_WAL
|| eNew
==PAGER_JOURNALMODE_WAL
)
7268 if( !db
->autoCommit
|| db
->nVdbeRead
>1 ){
7271 "cannot change %s wal mode from within a transaction",
7272 (eNew
==PAGER_JOURNALMODE_WAL
? "into" : "out of")
7274 goto abort_due_to_error
;
7277 if( eOld
==PAGER_JOURNALMODE_WAL
){
7278 /* If leaving WAL mode, close the log file. If successful, the call
7279 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7280 ** file. An EXCLUSIVE lock may still be held on the database file
7281 ** after a successful return.
7283 rc
= sqlite3PagerCloseWal(pPager
, db
);
7284 if( rc
==SQLITE_OK
){
7285 sqlite3PagerSetJournalMode(pPager
, eNew
);
7287 }else if( eOld
==PAGER_JOURNALMODE_MEMORY
){
7288 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7289 ** as an intermediate */
7290 sqlite3PagerSetJournalMode(pPager
, PAGER_JOURNALMODE_OFF
);
7293 /* Open a transaction on the database file. Regardless of the journal
7294 ** mode, this transaction always uses a rollback journal.
7296 assert( sqlite3BtreeTxnState(pBt
)!=SQLITE_TXN_WRITE
);
7297 if( rc
==SQLITE_OK
){
7298 rc
= sqlite3BtreeSetVersion(pBt
, (eNew
==PAGER_JOURNALMODE_WAL
? 2 : 1));
7302 #endif /* ifndef SQLITE_OMIT_WAL */
7304 if( rc
) eNew
= eOld
;
7305 eNew
= sqlite3PagerSetJournalMode(pPager
, eNew
);
7307 pOut
->flags
= MEM_Str
|MEM_Static
|MEM_Term
;
7308 pOut
->z
= (char *)sqlite3JournalModename(eNew
);
7309 pOut
->n
= sqlite3Strlen30(pOut
->z
);
7310 pOut
->enc
= SQLITE_UTF8
;
7311 sqlite3VdbeChangeEncoding(pOut
, encoding
);
7312 if( rc
) goto abort_due_to_error
;
7315 #endif /* SQLITE_OMIT_PRAGMA */
7317 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
7318 /* Opcode: Vacuum P1 P2 * * *
7320 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7321 ** for an attached database. The "temp" database may not be vacuumed.
7323 ** If P2 is not zero, then it is a register holding a string which is
7324 ** the file into which the result of vacuum should be written. When
7325 ** P2 is zero, the vacuum overwrites the original database.
7328 assert( p
->readOnly
==0 );
7329 rc
= sqlite3RunVacuum(&p
->zErrMsg
, db
, pOp
->p1
,
7330 pOp
->p2
? &aMem
[pOp
->p2
] : 0);
7331 if( rc
) goto abort_due_to_error
;
7336 #if !defined(SQLITE_OMIT_AUTOVACUUM)
7337 /* Opcode: IncrVacuum P1 P2 * * *
7339 ** Perform a single step of the incremental vacuum procedure on
7340 ** the P1 database. If the vacuum has finished, jump to instruction
7341 ** P2. Otherwise, fall through to the next instruction.
7343 case OP_IncrVacuum
: { /* jump */
7346 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
7347 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
7348 assert( p
->readOnly
==0 );
7349 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7350 rc
= sqlite3BtreeIncrVacuum(pBt
);
7351 VdbeBranchTaken(rc
==SQLITE_DONE
,2);
7353 if( rc
!=SQLITE_DONE
) goto abort_due_to_error
;
7361 /* Opcode: Expire P1 P2 * * *
7363 ** Cause precompiled statements to expire. When an expired statement
7364 ** is executed using sqlite3_step() it will either automatically
7365 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7366 ** or it will fail with SQLITE_SCHEMA.
7368 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
7369 ** then only the currently executing statement is expired.
7371 ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7372 ** then running SQL statements are allowed to continue to run to completion.
7373 ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7374 ** that might help the statement run faster but which does not affect the
7375 ** correctness of operation.
7378 assert( pOp
->p2
==0 || pOp
->p2
==1 );
7380 sqlite3ExpirePreparedStatements(db
, pOp
->p2
);
7382 p
->expired
= pOp
->p2
+1;
7387 /* Opcode: CursorLock P1 * * * *
7389 ** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7390 ** written by an other cursor.
7392 case OP_CursorLock
: {
7394 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
7395 pC
= p
->apCsr
[pOp
->p1
];
7397 assert( pC
->eCurType
==CURTYPE_BTREE
);
7398 sqlite3BtreeCursorPin(pC
->uc
.pCursor
);
7402 /* Opcode: CursorUnlock P1 * * * *
7404 ** Unlock the btree to which cursor P1 is pointing so that it can be
7405 ** written by other cursors.
7407 case OP_CursorUnlock
: {
7409 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
7410 pC
= p
->apCsr
[pOp
->p1
];
7412 assert( pC
->eCurType
==CURTYPE_BTREE
);
7413 sqlite3BtreeCursorUnpin(pC
->uc
.pCursor
);
7417 #ifndef SQLITE_OMIT_SHARED_CACHE
7418 /* Opcode: TableLock P1 P2 P3 P4 *
7419 ** Synopsis: iDb=P1 root=P2 write=P3
7421 ** Obtain a lock on a particular table. This instruction is only used when
7422 ** the shared-cache feature is enabled.
7424 ** P1 is the index of the database in sqlite3.aDb[] of the database
7425 ** on which the lock is acquired. A readlock is obtained if P3==0 or
7426 ** a write lock if P3==1.
7428 ** P2 contains the root-page of the table to lock.
7430 ** P4 contains a pointer to the name of the table being locked. This is only
7431 ** used to generate an error message if the lock cannot be obtained.
7433 case OP_TableLock
: {
7434 u8 isWriteLock
= (u8
)pOp
->p3
;
7435 if( isWriteLock
|| 0==(db
->flags
&SQLITE_ReadUncommit
) ){
7437 assert( p1
>=0 && p1
<db
->nDb
);
7438 assert( DbMaskTest(p
->btreeMask
, p1
) );
7439 assert( isWriteLock
==0 || isWriteLock
==1 );
7440 rc
= sqlite3BtreeLockTable(db
->aDb
[p1
].pBt
, pOp
->p2
, isWriteLock
);
7442 if( (rc
&0xFF)==SQLITE_LOCKED
){
7443 const char *z
= pOp
->p4
.z
;
7444 sqlite3VdbeError(p
, "database table is locked: %s", z
);
7446 goto abort_due_to_error
;
7451 #endif /* SQLITE_OMIT_SHARED_CACHE */
7453 #ifndef SQLITE_OMIT_VIRTUALTABLE
7454 /* Opcode: VBegin * * * P4 *
7456 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7457 ** xBegin method for that table.
7459 ** Also, whether or not P4 is set, check that this is not being called from
7460 ** within a callback to a virtual table xSync() method. If it is, the error
7461 ** code will be set to SQLITE_LOCKED.
7465 pVTab
= pOp
->p4
.pVtab
;
7466 rc
= sqlite3VtabBegin(db
, pVTab
);
7467 if( pVTab
) sqlite3VtabImportErrmsg(p
, pVTab
->pVtab
);
7468 if( rc
) goto abort_due_to_error
;
7471 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7473 #ifndef SQLITE_OMIT_VIRTUALTABLE
7474 /* Opcode: VCreate P1 P2 * * *
7476 ** P2 is a register that holds the name of a virtual table in database
7477 ** P1. Call the xCreate method for that table.
7480 Mem sMem
; /* For storing the record being decoded */
7481 const char *zTab
; /* Name of the virtual table */
7483 memset(&sMem
, 0, sizeof(sMem
));
7485 /* Because P2 is always a static string, it is impossible for the
7486 ** sqlite3VdbeMemCopy() to fail */
7487 assert( (aMem
[pOp
->p2
].flags
& MEM_Str
)!=0 );
7488 assert( (aMem
[pOp
->p2
].flags
& MEM_Static
)!=0 );
7489 rc
= sqlite3VdbeMemCopy(&sMem
, &aMem
[pOp
->p2
]);
7490 assert( rc
==SQLITE_OK
);
7491 zTab
= (const char*)sqlite3_value_text(&sMem
);
7492 assert( zTab
|| db
->mallocFailed
);
7494 rc
= sqlite3VtabCallCreate(db
, pOp
->p1
, zTab
, &p
->zErrMsg
);
7496 sqlite3VdbeMemRelease(&sMem
);
7497 if( rc
) goto abort_due_to_error
;
7500 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7502 #ifndef SQLITE_OMIT_VIRTUALTABLE
7503 /* Opcode: VDestroy P1 * * P4 *
7505 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
7510 rc
= sqlite3VtabCallDestroy(db
, pOp
->p1
, pOp
->p4
.z
);
7512 assert( p
->errorAction
==OE_Abort
&& p
->usesStmtJournal
);
7513 if( rc
) goto abort_due_to_error
;
7516 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7518 #ifndef SQLITE_OMIT_VIRTUALTABLE
7519 /* Opcode: VOpen P1 * * P4 *
7521 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
7522 ** P1 is a cursor number. This opcode opens a cursor to the virtual
7523 ** table and stores that cursor in P1.
7527 sqlite3_vtab_cursor
*pVCur
;
7528 sqlite3_vtab
*pVtab
;
7529 const sqlite3_module
*pModule
;
7531 assert( p
->bIsReader
);
7534 pVtab
= pOp
->p4
.pVtab
->pVtab
;
7535 if( pVtab
==0 || NEVER(pVtab
->pModule
==0) ){
7537 goto abort_due_to_error
;
7539 pModule
= pVtab
->pModule
;
7540 rc
= pModule
->xOpen(pVtab
, &pVCur
);
7541 sqlite3VtabImportErrmsg(p
, pVtab
);
7542 if( rc
) goto abort_due_to_error
;
7544 /* Initialize sqlite3_vtab_cursor base class */
7545 pVCur
->pVtab
= pVtab
;
7547 /* Initialize vdbe cursor object */
7548 pCur
= allocateCursor(p
, pOp
->p1
, 0, -1, CURTYPE_VTAB
);
7550 pCur
->uc
.pVCur
= pVCur
;
7553 assert( db
->mallocFailed
);
7554 pModule
->xClose(pVCur
);
7559 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7561 #ifndef SQLITE_OMIT_VIRTUALTABLE
7562 /* Opcode: VFilter P1 P2 P3 P4 *
7563 ** Synopsis: iplan=r[P3] zplan='P4'
7565 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
7566 ** the filtered result set is empty.
7568 ** P4 is either NULL or a string that was generated by the xBestIndex
7569 ** method of the module. The interpretation of the P4 string is left
7570 ** to the module implementation.
7572 ** This opcode invokes the xFilter method on the virtual table specified
7573 ** by P1. The integer query plan parameter to xFilter is stored in register
7574 ** P3. Register P3+1 stores the argc parameter to be passed to the
7575 ** xFilter method. Registers P3+2..P3+1+argc are the argc
7576 ** additional parameters which are passed to
7577 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
7579 ** A jump is made to P2 if the result set after filtering would be empty.
7581 case OP_VFilter
: { /* jump */
7584 const sqlite3_module
*pModule
;
7587 sqlite3_vtab_cursor
*pVCur
;
7588 sqlite3_vtab
*pVtab
;
7594 pQuery
= &aMem
[pOp
->p3
];
7596 pCur
= p
->apCsr
[pOp
->p1
];
7597 assert( memIsValid(pQuery
) );
7598 REGISTER_TRACE(pOp
->p3
, pQuery
);
7599 assert( pCur
->eCurType
==CURTYPE_VTAB
);
7600 pVCur
= pCur
->uc
.pVCur
;
7601 pVtab
= pVCur
->pVtab
;
7602 pModule
= pVtab
->pModule
;
7604 /* Grab the index number and argc parameters */
7605 assert( (pQuery
->flags
&MEM_Int
)!=0 && pArgc
->flags
==MEM_Int
);
7606 nArg
= (int)pArgc
->u
.i
;
7607 iQuery
= (int)pQuery
->u
.i
;
7609 /* Invoke the xFilter method */
7612 for(i
= 0; i
<nArg
; i
++){
7613 apArg
[i
] = &pArgc
[i
+1];
7615 rc
= pModule
->xFilter(pVCur
, iQuery
, pOp
->p4
.z
, nArg
, apArg
);
7616 sqlite3VtabImportErrmsg(p
, pVtab
);
7617 if( rc
) goto abort_due_to_error
;
7618 res
= pModule
->xEof(pVCur
);
7620 VdbeBranchTaken(res
!=0,2);
7621 if( res
) goto jump_to_p2
;
7624 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7626 #ifndef SQLITE_OMIT_VIRTUALTABLE
7627 /* Opcode: VColumn P1 P2 P3 * P5
7628 ** Synopsis: r[P3]=vcolumn(P2)
7630 ** Store in register P3 the value of the P2-th column of
7631 ** the current row of the virtual-table of cursor P1.
7633 ** If the VColumn opcode is being used to fetch the value of
7634 ** an unchanging column during an UPDATE operation, then the P5
7635 ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7636 ** function to return true inside the xColumn method of the virtual
7637 ** table implementation. The P5 column might also contain other
7638 ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7639 ** unused by OP_VColumn.
7642 sqlite3_vtab
*pVtab
;
7643 const sqlite3_module
*pModule
;
7645 sqlite3_context sContext
;
7647 VdbeCursor
*pCur
= p
->apCsr
[pOp
->p1
];
7648 assert( pCur
->eCurType
==CURTYPE_VTAB
);
7649 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
7650 pDest
= &aMem
[pOp
->p3
];
7651 memAboutToChange(p
, pDest
);
7652 if( pCur
->nullRow
){
7653 sqlite3VdbeMemSetNull(pDest
);
7656 pVtab
= pCur
->uc
.pVCur
->pVtab
;
7657 pModule
= pVtab
->pModule
;
7658 assert( pModule
->xColumn
);
7659 memset(&sContext
, 0, sizeof(sContext
));
7660 sContext
.pOut
= pDest
;
7661 assert( pOp
->p5
==OPFLAG_NOCHNG
|| pOp
->p5
==0 );
7662 if( pOp
->p5
& OPFLAG_NOCHNG
){
7663 sqlite3VdbeMemSetNull(pDest
);
7664 pDest
->flags
= MEM_Null
|MEM_Zero
;
7667 MemSetTypeFlag(pDest
, MEM_Null
);
7669 rc
= pModule
->xColumn(pCur
->uc
.pVCur
, &sContext
, pOp
->p2
);
7670 sqlite3VtabImportErrmsg(p
, pVtab
);
7671 if( sContext
.isError
>0 ){
7672 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pDest
));
7673 rc
= sContext
.isError
;
7675 sqlite3VdbeChangeEncoding(pDest
, encoding
);
7676 REGISTER_TRACE(pOp
->p3
, pDest
);
7677 UPDATE_MAX_BLOBSIZE(pDest
);
7679 if( sqlite3VdbeMemTooBig(pDest
) ){
7682 if( rc
) goto abort_due_to_error
;
7685 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7687 #ifndef SQLITE_OMIT_VIRTUALTABLE
7688 /* Opcode: VNext P1 P2 * * *
7690 ** Advance virtual table P1 to the next row in its result set and
7691 ** jump to instruction P2. Or, if the virtual table has reached
7692 ** the end of its result set, then fall through to the next instruction.
7694 case OP_VNext
: { /* jump */
7695 sqlite3_vtab
*pVtab
;
7696 const sqlite3_module
*pModule
;
7701 pCur
= p
->apCsr
[pOp
->p1
];
7702 assert( pCur
->eCurType
==CURTYPE_VTAB
);
7703 if( pCur
->nullRow
){
7706 pVtab
= pCur
->uc
.pVCur
->pVtab
;
7707 pModule
= pVtab
->pModule
;
7708 assert( pModule
->xNext
);
7710 /* Invoke the xNext() method of the module. There is no way for the
7711 ** underlying implementation to return an error if one occurs during
7712 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7713 ** data is available) and the error code returned when xColumn or
7714 ** some other method is next invoked on the save virtual table cursor.
7716 rc
= pModule
->xNext(pCur
->uc
.pVCur
);
7717 sqlite3VtabImportErrmsg(p
, pVtab
);
7718 if( rc
) goto abort_due_to_error
;
7719 res
= pModule
->xEof(pCur
->uc
.pVCur
);
7720 VdbeBranchTaken(!res
,2);
7722 /* If there is data, jump to P2 */
7723 goto jump_to_p2_and_check_for_interrupt
;
7725 goto check_for_interrupt
;
7727 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7729 #ifndef SQLITE_OMIT_VIRTUALTABLE
7730 /* Opcode: VRename P1 * * P4 *
7732 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
7733 ** This opcode invokes the corresponding xRename method. The value
7734 ** in register P1 is passed as the zName argument to the xRename method.
7737 sqlite3_vtab
*pVtab
;
7741 isLegacy
= (db
->flags
& SQLITE_LegacyAlter
);
7742 db
->flags
|= SQLITE_LegacyAlter
;
7743 pVtab
= pOp
->p4
.pVtab
->pVtab
;
7744 pName
= &aMem
[pOp
->p1
];
7745 assert( pVtab
->pModule
->xRename
);
7746 assert( memIsValid(pName
) );
7747 assert( p
->readOnly
==0 );
7748 REGISTER_TRACE(pOp
->p1
, pName
);
7749 assert( pName
->flags
& MEM_Str
);
7750 testcase( pName
->enc
==SQLITE_UTF8
);
7751 testcase( pName
->enc
==SQLITE_UTF16BE
);
7752 testcase( pName
->enc
==SQLITE_UTF16LE
);
7753 rc
= sqlite3VdbeChangeEncoding(pName
, SQLITE_UTF8
);
7754 if( rc
) goto abort_due_to_error
;
7755 rc
= pVtab
->pModule
->xRename(pVtab
, pName
->z
);
7756 if( isLegacy
==0 ) db
->flags
&= ~(u64
)SQLITE_LegacyAlter
;
7757 sqlite3VtabImportErrmsg(p
, pVtab
);
7759 if( rc
) goto abort_due_to_error
;
7764 #ifndef SQLITE_OMIT_VIRTUALTABLE
7765 /* Opcode: VUpdate P1 P2 P3 P4 P5
7766 ** Synopsis: data=r[P3@P2]
7768 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
7769 ** This opcode invokes the corresponding xUpdate method. P2 values
7770 ** are contiguous memory cells starting at P3 to pass to the xUpdate
7771 ** invocation. The value in register (P3+P2-1) corresponds to the
7772 ** p2th element of the argv array passed to xUpdate.
7774 ** The xUpdate method will do a DELETE or an INSERT or both.
7775 ** The argv[0] element (which corresponds to memory cell P3)
7776 ** is the rowid of a row to delete. If argv[0] is NULL then no
7777 ** deletion occurs. The argv[1] element is the rowid of the new
7778 ** row. This can be NULL to have the virtual table select the new
7779 ** rowid for itself. The subsequent elements in the array are
7780 ** the values of columns in the new row.
7782 ** If P2==1 then no insert is performed. argv[0] is the rowid of
7785 ** P1 is a boolean flag. If it is set to true and the xUpdate call
7786 ** is successful, then the value returned by sqlite3_last_insert_rowid()
7787 ** is set to the value of the rowid for the row just inserted.
7789 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7790 ** apply in the case of a constraint failure on an insert or update.
7793 sqlite3_vtab
*pVtab
;
7794 const sqlite3_module
*pModule
;
7801 assert( pOp
->p2
==1 || pOp
->p5
==OE_Fail
|| pOp
->p5
==OE_Rollback
7802 || pOp
->p5
==OE_Abort
|| pOp
->p5
==OE_Ignore
|| pOp
->p5
==OE_Replace
7804 assert( p
->readOnly
==0 );
7805 if( db
->mallocFailed
) goto no_mem
;
7806 sqlite3VdbeIncrWriteCounter(p
, 0);
7807 pVtab
= pOp
->p4
.pVtab
->pVtab
;
7808 if( pVtab
==0 || NEVER(pVtab
->pModule
==0) ){
7810 goto abort_due_to_error
;
7812 pModule
= pVtab
->pModule
;
7814 assert( pOp
->p4type
==P4_VTAB
);
7815 if( ALWAYS(pModule
->xUpdate
) ){
7816 u8 vtabOnConflict
= db
->vtabOnConflict
;
7818 pX
= &aMem
[pOp
->p3
];
7819 for(i
=0; i
<nArg
; i
++){
7820 assert( memIsValid(pX
) );
7821 memAboutToChange(p
, pX
);
7825 db
->vtabOnConflict
= pOp
->p5
;
7826 rc
= pModule
->xUpdate(pVtab
, nArg
, apArg
, &rowid
);
7827 db
->vtabOnConflict
= vtabOnConflict
;
7828 sqlite3VtabImportErrmsg(p
, pVtab
);
7829 if( rc
==SQLITE_OK
&& pOp
->p1
){
7830 assert( nArg
>1 && apArg
[0] && (apArg
[0]->flags
&MEM_Null
) );
7831 db
->lastRowid
= rowid
;
7833 if( (rc
&0xff)==SQLITE_CONSTRAINT
&& pOp
->p4
.pVtab
->bConstraint
){
7834 if( pOp
->p5
==OE_Ignore
){
7837 p
->errorAction
= ((pOp
->p5
==OE_Replace
) ? OE_Abort
: pOp
->p5
);
7842 if( rc
) goto abort_due_to_error
;
7846 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7848 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
7849 /* Opcode: Pagecount P1 P2 * * *
7851 ** Write the current number of pages in database P1 to memory cell P2.
7853 case OP_Pagecount
: { /* out2 */
7854 pOut
= out2Prerelease(p
, pOp
);
7855 pOut
->u
.i
= sqlite3BtreeLastPage(db
->aDb
[pOp
->p1
].pBt
);
7861 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
7862 /* Opcode: MaxPgcnt P1 P2 P3 * *
7864 ** Try to set the maximum page count for database P1 to the value in P3.
7865 ** Do not let the maximum page count fall below the current page count and
7866 ** do not change the maximum page count value if P3==0.
7868 ** Store the maximum page count after the change in register P2.
7870 case OP_MaxPgcnt
: { /* out2 */
7871 unsigned int newMax
;
7874 pOut
= out2Prerelease(p
, pOp
);
7875 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7878 newMax
= sqlite3BtreeLastPage(pBt
);
7879 if( newMax
< (unsigned)pOp
->p3
) newMax
= (unsigned)pOp
->p3
;
7881 pOut
->u
.i
= sqlite3BtreeMaxPageCount(pBt
, newMax
);
7886 /* Opcode: Function P1 P2 P3 P4 *
7887 ** Synopsis: r[P3]=func(r[P2@NP])
7889 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7890 ** contains a pointer to the function to be run) with arguments taken
7891 ** from register P2 and successors. The number of arguments is in
7892 ** the sqlite3_context object that P4 points to.
7893 ** The result of the function is stored
7894 ** in register P3. Register P3 must not be one of the function inputs.
7896 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
7897 ** function was determined to be constant at compile time. If the first
7898 ** argument was constant then bit 0 of P1 is set. This is used to determine
7899 ** whether meta data associated with a user function argument using the
7900 ** sqlite3_set_auxdata() API may be safely retained until the next
7901 ** invocation of this opcode.
7903 ** See also: AggStep, AggFinal, PureFunc
7905 /* Opcode: PureFunc P1 P2 P3 P4 *
7906 ** Synopsis: r[P3]=func(r[P2@NP])
7908 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7909 ** contains a pointer to the function to be run) with arguments taken
7910 ** from register P2 and successors. The number of arguments is in
7911 ** the sqlite3_context object that P4 points to.
7912 ** The result of the function is stored
7913 ** in register P3. Register P3 must not be one of the function inputs.
7915 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
7916 ** function was determined to be constant at compile time. If the first
7917 ** argument was constant then bit 0 of P1 is set. This is used to determine
7918 ** whether meta data associated with a user function argument using the
7919 ** sqlite3_set_auxdata() API may be safely retained until the next
7920 ** invocation of this opcode.
7922 ** This opcode works exactly like OP_Function. The only difference is in
7923 ** its name. This opcode is used in places where the function must be
7924 ** purely non-deterministic. Some built-in date/time functions can be
7925 ** either determinitic of non-deterministic, depending on their arguments.
7926 ** When those function are used in a non-deterministic way, they will check
7927 ** to see if they were called using OP_PureFunc instead of OP_Function, and
7928 ** if they were, they throw an error.
7930 ** See also: AggStep, AggFinal, Function
7932 case OP_PureFunc
: /* group */
7933 case OP_Function
: { /* group */
7935 sqlite3_context
*pCtx
;
7937 assert( pOp
->p4type
==P4_FUNCCTX
);
7938 pCtx
= pOp
->p4
.pCtx
;
7940 /* If this function is inside of a trigger, the register array in aMem[]
7941 ** might change from one evaluation to the next. The next block of code
7942 ** checks to see if the register array has changed, and if so it
7943 ** reinitializes the relavant parts of the sqlite3_context object */
7944 pOut
= &aMem
[pOp
->p3
];
7945 if( pCtx
->pOut
!= pOut
){
7948 for(i
=pCtx
->argc
-1; i
>=0; i
--) pCtx
->argv
[i
] = &aMem
[pOp
->p2
+i
];
7950 assert( pCtx
->pVdbe
==p
);
7952 memAboutToChange(p
, pOut
);
7954 for(i
=0; i
<pCtx
->argc
; i
++){
7955 assert( memIsValid(pCtx
->argv
[i
]) );
7956 REGISTER_TRACE(pOp
->p2
+i
, pCtx
->argv
[i
]);
7959 MemSetTypeFlag(pOut
, MEM_Null
);
7960 assert( pCtx
->isError
==0 );
7961 (*pCtx
->pFunc
->xSFunc
)(pCtx
, pCtx
->argc
, pCtx
->argv
);/* IMP: R-24505-23230 */
7963 /* If the function returned an error, throw an exception */
7964 if( pCtx
->isError
){
7965 if( pCtx
->isError
>0 ){
7966 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pOut
));
7969 sqlite3VdbeDeleteAuxData(db
, &p
->pAuxData
, pCtx
->iOp
, pOp
->p1
);
7971 if( rc
) goto abort_due_to_error
;
7974 /* Copy the result of the function into register P3 */
7975 if( pOut
->flags
& (MEM_Str
|MEM_Blob
) ){
7976 sqlite3VdbeChangeEncoding(pOut
, encoding
);
7977 if( sqlite3VdbeMemTooBig(pOut
) ) goto too_big
;
7980 REGISTER_TRACE(pOp
->p3
, pOut
);
7981 UPDATE_MAX_BLOBSIZE(pOut
);
7985 /* Opcode: Trace P1 P2 * P4 *
7987 ** Write P4 on the statement trace output if statement tracing is
7990 ** Operand P1 must be 0x7fffffff and P2 must positive.
7992 /* Opcode: Init P1 P2 P3 P4 *
7993 ** Synopsis: Start at P2
7995 ** Programs contain a single instance of this opcode as the very first
7998 ** If tracing is enabled (by the sqlite3_trace()) interface, then
7999 ** the UTF-8 string contained in P4 is emitted on the trace callback.
8000 ** Or if P4 is blank, use the string returned by sqlite3_sql().
8002 ** If P2 is not zero, jump to instruction P2.
8004 ** Increment the value of P1 so that OP_Once opcodes will jump the
8005 ** first time they are evaluated for this run.
8007 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
8008 ** error is encountered.
8011 case OP_Init
: { /* jump */
8013 #ifndef SQLITE_OMIT_TRACE
8017 /* If the P4 argument is not NULL, then it must be an SQL comment string.
8018 ** The "--" string is broken up to prevent false-positives with srcck1.c.
8020 ** This assert() provides evidence for:
8021 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
8022 ** would have been returned by the legacy sqlite3_trace() interface by
8023 ** using the X argument when X begins with "--" and invoking
8024 ** sqlite3_expanded_sql(P) otherwise.
8026 assert( pOp
->p4
.z
==0 || strncmp(pOp
->p4
.z
, "-" "- ", 3)==0 );
8028 /* OP_Init is always instruction 0 */
8029 assert( pOp
==p
->aOp
|| pOp
->opcode
==OP_Trace
);
8031 #ifndef SQLITE_OMIT_TRACE
8032 if( (db
->mTrace
& (SQLITE_TRACE_STMT
|SQLITE_TRACE_LEGACY
))!=0
8034 && (zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
))!=0
8036 #ifndef SQLITE_OMIT_DEPRECATED
8037 if( db
->mTrace
& SQLITE_TRACE_LEGACY
){
8038 char *z
= sqlite3VdbeExpandSql(p
, zTrace
);
8039 db
->trace
.xLegacy(db
->pTraceArg
, z
);
8043 if( db
->nVdbeExec
>1 ){
8044 char *z
= sqlite3MPrintf(db
, "-- %s", zTrace
);
8045 (void)db
->trace
.xV2(SQLITE_TRACE_STMT
, db
->pTraceArg
, p
, z
);
8046 sqlite3DbFree(db
, z
);
8048 (void)db
->trace
.xV2(SQLITE_TRACE_STMT
, db
->pTraceArg
, p
, zTrace
);
8051 #ifdef SQLITE_USE_FCNTL_TRACE
8052 zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
);
8055 for(j
=0; j
<db
->nDb
; j
++){
8056 if( DbMaskTest(p
->btreeMask
, j
)==0 ) continue;
8057 sqlite3_file_control(db
, db
->aDb
[j
].zDbSName
, SQLITE_FCNTL_TRACE
, zTrace
);
8060 #endif /* SQLITE_USE_FCNTL_TRACE */
8062 if( (db
->flags
& SQLITE_SqlTrace
)!=0
8063 && (zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
))!=0
8065 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace
);
8067 #endif /* SQLITE_DEBUG */
8068 #endif /* SQLITE_OMIT_TRACE */
8069 assert( pOp
->p2
>0 );
8070 if( pOp
->p1
>=sqlite3GlobalConfig
.iOnceResetThreshold
){
8071 if( pOp
->opcode
==OP_Trace
) break;
8072 for(i
=1; i
<p
->nOp
; i
++){
8073 if( p
->aOp
[i
].opcode
==OP_Once
) p
->aOp
[i
].p1
= 0;
8078 p
->aCounter
[SQLITE_STMTSTATUS_RUN
]++;
8082 #ifdef SQLITE_ENABLE_CURSOR_HINTS
8083 /* Opcode: CursorHint P1 * * P4 *
8085 ** Provide a hint to cursor P1 that it only needs to return rows that
8086 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8087 ** to values currently held in registers. TK_COLUMN terms in the P4
8088 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
8090 case OP_CursorHint
: {
8093 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
8094 assert( pOp
->p4type
==P4_EXPR
);
8095 pC
= p
->apCsr
[pOp
->p1
];
8097 assert( pC
->eCurType
==CURTYPE_BTREE
);
8098 sqlite3BtreeCursorHint(pC
->uc
.pCursor
, BTREE_HINT_RANGE
,
8099 pOp
->p4
.pExpr
, aMem
);
8103 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
8106 /* Opcode: Abortable * * * * *
8108 ** Verify that an Abort can happen. Assert if an Abort at this point
8109 ** might cause database corruption. This opcode only appears in debugging
8112 ** An Abort is safe if either there have been no writes, or if there is
8113 ** an active statement journal.
8115 case OP_Abortable
: {
8116 sqlite3VdbeAssertAbortable(p
);
8122 /* Opcode: ReleaseReg P1 P2 P3 * P5
8123 ** Synopsis: release r[P1@P2] mask P3
8125 ** Release registers from service. Any content that was in the
8126 ** the registers is unreliable after this opcode completes.
8128 ** The registers released will be the P2 registers starting at P1,
8129 ** except if bit ii of P3 set, then do not release register P1+ii.
8130 ** In other words, P3 is a mask of registers to preserve.
8132 ** Releasing a register clears the Mem.pScopyFrom pointer. That means
8133 ** that if the content of the released register was set using OP_SCopy,
8134 ** a change to the value of the source register for the OP_SCopy will no longer
8135 ** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8137 ** If P5 is set, then all released registers have their type set
8138 ** to MEM_Undefined so that any subsequent attempt to read the released
8139 ** register (before it is reinitialized) will generate an assertion fault.
8141 ** P5 ought to be set on every call to this opcode.
8142 ** However, there are places in the code generator will release registers
8143 ** before their are used, under the (valid) assumption that the registers
8144 ** will not be reallocated for some other purpose before they are used and
8145 ** hence are safe to release.
8147 ** This opcode is only available in testing and debugging builds. It is
8148 ** not generated for release builds. The purpose of this opcode is to help
8149 ** validate the generated bytecode. This opcode does not actually contribute
8150 ** to computing an answer.
8152 case OP_ReleaseReg
: {
8156 assert( pOp
->p1
>0 );
8157 assert( pOp
->p1
+pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
)+1 );
8158 pMem
= &aMem
[pOp
->p1
];
8159 constMask
= pOp
->p3
;
8160 for(i
=0; i
<pOp
->p2
; i
++, pMem
++){
8161 if( i
>=32 || (constMask
& MASKBIT32(i
))==0 ){
8162 pMem
->pScopyFrom
= 0;
8163 if( i
<32 && pOp
->p5
) MemSetTypeFlag(pMem
, MEM_Undefined
);
8170 /* Opcode: Noop * * * * *
8172 ** Do nothing. This instruction is often useful as a jump
8176 ** The magic Explain opcode are only inserted when explain==2 (which
8177 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8178 ** This opcode records information from the optimizer. It is the
8179 ** the same as a no-op. This opcodesnever appears in a real VM program.
8181 default: { /* This is really OP_Noop, OP_Explain */
8182 assert( pOp
->opcode
==OP_Noop
|| pOp
->opcode
==OP_Explain
);
8187 /*****************************************************************************
8188 ** The cases of the switch statement above this line should all be indented
8189 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8190 ** readability. From this point on down, the normal indentation rules are
8192 *****************************************************************************/
8197 u64 endTime
= sqlite3NProfileCnt
? sqlite3NProfileCnt
: sqlite3Hwtime();
8198 if( endTime
>start
) pOrigOp
->cycles
+= endTime
- start
;
8203 /* The following code adds nothing to the actual functionality
8204 ** of the program. It is only here for testing and debugging.
8205 ** On the other hand, it does burn CPU cycles every time through
8206 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8209 assert( pOp
>=&aOp
[-1] && pOp
<&aOp
[p
->nOp
-1] );
8212 if( db
->flags
& SQLITE_VdbeTrace
){
8213 u8 opProperty
= sqlite3OpcodeProperty
[pOrigOp
->opcode
];
8214 if( rc
!=0 ) printf("rc=%d\n",rc
);
8215 if( opProperty
& (OPFLG_OUT2
) ){
8216 registerTrace(pOrigOp
->p2
, &aMem
[pOrigOp
->p2
]);
8218 if( opProperty
& OPFLG_OUT3
){
8219 registerTrace(pOrigOp
->p3
, &aMem
[pOrigOp
->p3
]);
8221 if( opProperty
==0xff ){
8222 /* Never happens. This code exists to avoid a harmless linkage
8223 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8225 sqlite3VdbeRegisterDump(p
);
8228 #endif /* SQLITE_DEBUG */
8230 } /* The end of the for(;;) loop the loops through opcodes */
8232 /* If we reach this point, it means that execution is finished with
8233 ** an error of some kind.
8236 if( db
->mallocFailed
){
8237 rc
= SQLITE_NOMEM_BKPT
;
8238 }else if( rc
==SQLITE_IOERR_CORRUPTFS
){
8239 rc
= SQLITE_CORRUPT_BKPT
;
8242 if( p
->zErrMsg
==0 && rc
!=SQLITE_IOERR_NOMEM
){
8243 sqlite3VdbeError(p
, "%s", sqlite3ErrStr(rc
));
8246 sqlite3SystemError(db
, rc
);
8247 testcase( sqlite3GlobalConfig
.xLog
!=0 );
8248 sqlite3_log(rc
, "statement aborts at %d: [%s] %s",
8249 (int)(pOp
- aOp
), p
->zSql
, p
->zErrMsg
);
8251 if( rc
==SQLITE_IOERR_NOMEM
) sqlite3OomFault(db
);
8253 if( resetSchemaOnFault
>0 ){
8254 sqlite3ResetOneSchema(db
, resetSchemaOnFault
-1);
8257 /* This is the only way out of this procedure. We have to
8258 ** release the mutexes on btrees that were acquired at the
8261 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8262 while( nVmStep
>=nProgressLimit
&& db
->xProgress
!=0 ){
8263 nProgressLimit
+= db
->nProgressOps
;
8264 if( db
->xProgress(db
->pProgressArg
) ){
8265 nProgressLimit
= LARGEST_UINT64
;
8266 rc
= SQLITE_INTERRUPT
;
8267 goto abort_due_to_error
;
8271 p
->aCounter
[SQLITE_STMTSTATUS_VM_STEP
] += (int)nVmStep
;
8272 sqlite3VdbeLeave(p
);
8273 assert( rc
!=SQLITE_OK
|| nExtraDelete
==0
8274 || sqlite3_strlike("DELETE%",p
->zSql
,0)!=0
8278 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8282 sqlite3VdbeError(p
, "string or blob too big");
8284 goto abort_due_to_error
;
8286 /* Jump to here if a malloc() fails.
8289 sqlite3OomFault(db
);
8290 sqlite3VdbeError(p
, "out of memory");
8291 rc
= SQLITE_NOMEM_BKPT
;
8292 goto abort_due_to_error
;
8294 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
8297 abort_due_to_interrupt
:
8298 assert( AtomicLoad(&db
->u1
.isInterrupted
) );
8299 rc
= SQLITE_INTERRUPT
;
8300 goto abort_due_to_error
;