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
){
144 ** Invoke the VDBE coverage callback, if that callback is defined. This
145 ** feature is used for test suite validation only and does not appear an
146 ** production builds.
148 ** M is the type of branch. I is the direction taken for this instance of
151 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
152 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
153 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
155 ** In other words, if M is 2, then I is either 0 (for fall-through) or
156 ** 1 (for when the branch is taken). If M is 3, the I is 0 for an
157 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
158 ** if the result of comparison is NULL. For M=3, I=2 the jump may or
159 ** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
160 ** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
161 ** depending on if the operands are less than, equal, or greater than.
163 ** iSrcLine is the source code line (from the __LINE__ macro) that
164 ** generated the VDBE instruction combined with flag bits. The source
165 ** code line number is in the lower 24 bits of iSrcLine and the upper
166 ** 8 bytes are flags. The lower three bits of the flags indicate
167 ** values for I that should never occur. For example, if the branch is
168 ** always taken, the flags should be 0x05 since the fall-through and
169 ** alternate branch are never taken. If a branch is never taken then
170 ** flags should be 0x06 since only the fall-through approach is allowed.
172 ** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
173 ** interested in equal or not-equal. In other words, I==0 and I==2
174 ** should be treated as equivalent
176 ** Since only a line number is retained, not the filename, this macro
177 ** only works for amalgamation builds. But that is ok, since these macros
178 ** should be no-ops except for special builds used to measure test coverage.
180 #if !defined(SQLITE_VDBE_COVERAGE)
181 # define VdbeBranchTaken(I,M)
183 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
184 static void vdbeTakeBranch(u32 iSrcLine
, u8 I
, u8 M
){
186 assert( I
<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
187 assert( M
<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
188 assert( I
<M
); /* I can only be 2 if M is 3 or 4 */
189 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
191 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
192 ** the flags indicate directions that the branch can never go. If
193 ** a branch really does go in one of those directions, assert right
195 mNever
= iSrcLine
>> 24;
196 assert( (I
& mNever
)==0 );
197 if( sqlite3GlobalConfig
.xVdbeBranch
==0 ) return; /*NO_TEST*/
198 /* Invoke the branch coverage callback with three arguments:
199 ** iSrcLine - the line number of the VdbeCoverage() macro, with
201 ** I - Mask of bits 0x07 indicating which cases are are
202 ** fulfilled by this instance of the jump. 0x01 means
203 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
204 ** impossible cases (ex: if the comparison is never NULL)
205 ** are filled in automatically so that the coverage
206 ** measurement logic does not flag those impossible cases
207 ** as missed coverage.
208 ** M - Type of jump. Same as M argument above
211 if( M
==2 ) I
|= 0x04;
214 if( (mNever
&0x08)!=0 && (I
&0x05)!=0) I
|= 0x05; /*NO_TEST*/
216 sqlite3GlobalConfig
.xVdbeBranch(sqlite3GlobalConfig
.pVdbeBranchArg
,
217 iSrcLine
&0xffffff, I
, M
);
222 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
223 ** a pointer to a dynamically allocated string where some other entity
224 ** is responsible for deallocating that string. Because the register
225 ** does not control the string, it might be deleted without the register
228 ** This routine converts an ephemeral string into a dynamically allocated
229 ** string that the register itself controls. In other words, it
230 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
232 #define Deephemeralize(P) \
233 if( ((P)->flags&MEM_Ephem)!=0 \
234 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
236 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
237 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
240 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
241 ** if we run out of memory.
243 static VdbeCursor
*allocateCursor(
244 Vdbe
*p
, /* The virtual machine */
245 int iCur
, /* Index of the new VdbeCursor */
246 int nField
, /* Number of fields in the table or index */
247 u8 eCurType
/* Type of the new cursor */
249 /* Find the memory cell that will be used to store the blob of memory
250 ** required for this VdbeCursor structure. It is convenient to use a
251 ** vdbe memory cell to manage the memory allocation required for a
252 ** VdbeCursor structure for the following reasons:
254 ** * Sometimes cursor numbers are used for a couple of different
255 ** purposes in a vdbe program. The different uses might require
256 ** different sized allocations. Memory cells provide growable
259 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
260 ** be freed lazily via the sqlite3_release_memory() API. This
261 ** minimizes the number of malloc calls made by the system.
263 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
264 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
265 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
267 Mem
*pMem
= iCur
>0 ? &p
->aMem
[p
->nMem
-iCur
] : p
->aMem
;
272 ROUND8P(sizeof(VdbeCursor
)) + 2*sizeof(u32
)*nField
+
273 (eCurType
==CURTYPE_BTREE
?sqlite3BtreeCursorSize():0);
275 assert( iCur
>=0 && iCur
<p
->nCursor
);
276 if( p
->apCsr
[iCur
] ){ /*OPTIMIZATION-IF-FALSE*/
277 sqlite3VdbeFreeCursorNN(p
, p
->apCsr
[iCur
]);
281 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
282 ** the pMem used to hold space for the cursor has enough storage available
283 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
284 ** to hold cursors, it is faster to in-line the logic. */
285 assert( pMem
->flags
==MEM_Undefined
);
286 assert( (pMem
->flags
& MEM_Dyn
)==0 );
287 assert( pMem
->szMalloc
==0 || pMem
->z
==pMem
->zMalloc
);
288 if( pMem
->szMalloc
<nByte
){
289 if( pMem
->szMalloc
>0 ){
290 sqlite3DbFreeNN(pMem
->db
, pMem
->zMalloc
);
292 pMem
->z
= pMem
->zMalloc
= sqlite3DbMallocRaw(pMem
->db
, nByte
);
293 if( pMem
->zMalloc
==0 ){
297 pMem
->szMalloc
= nByte
;
300 p
->apCsr
[iCur
] = pCx
= (VdbeCursor
*)pMem
->zMalloc
;
301 memset(pCx
, 0, offsetof(VdbeCursor
,pAltCursor
));
302 pCx
->eCurType
= eCurType
;
303 pCx
->nField
= nField
;
304 pCx
->aOffset
= &pCx
->aType
[nField
];
305 if( eCurType
==CURTYPE_BTREE
){
306 pCx
->uc
.pCursor
= (BtCursor
*)
307 &pMem
->z
[ROUND8P(sizeof(VdbeCursor
))+2*sizeof(u32
)*nField
];
308 sqlite3BtreeCursorZero(pCx
->uc
.pCursor
);
314 ** The string in pRec is known to look like an integer and to have a
315 ** floating point value of rValue. Return true and set *piValue to the
316 ** integer value if the string is in range to be an integer. Otherwise,
319 static int alsoAnInt(Mem
*pRec
, double rValue
, i64
*piValue
){
321 iValue
= sqlite3RealToI64(rValue
);
322 if( sqlite3RealSameAsInt(rValue
,iValue
) ){
326 return 0==sqlite3Atoi64(pRec
->z
, piValue
, pRec
->n
, pRec
->enc
);
330 ** Try to convert a value into a numeric representation if we can
331 ** do so without loss of information. In other words, if the string
332 ** looks like a number, convert it into a number. If it does not
333 ** look like a number, leave it alone.
335 ** If the bTryForInt flag is true, then extra effort is made to give
336 ** an integer representation. Strings that look like floating point
337 ** values but which have no fractional component (example: '48.00')
338 ** will have a MEM_Int representation when bTryForInt is true.
340 ** If bTryForInt is false, then if the input string contains a decimal
341 ** point or exponential notation, the result is only MEM_Real, even
342 ** if there is an exact integer representation of the quantity.
344 static void applyNumericAffinity(Mem
*pRec
, int bTryForInt
){
348 assert( (pRec
->flags
& (MEM_Str
|MEM_Int
|MEM_Real
|MEM_IntReal
))==MEM_Str
);
349 rc
= sqlite3AtoF(pRec
->z
, &rValue
, pRec
->n
, enc
);
351 if( rc
==1 && alsoAnInt(pRec
, rValue
, &pRec
->u
.i
) ){
352 pRec
->flags
|= MEM_Int
;
355 pRec
->flags
|= MEM_Real
;
356 if( bTryForInt
) sqlite3VdbeIntegerAffinity(pRec
);
358 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
359 ** string representation after computing a numeric equivalent, because the
360 ** string representation might not be the canonical representation for the
361 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
362 pRec
->flags
&= ~MEM_Str
;
366 ** Processing is determine by the affinity parameter:
368 ** SQLITE_AFF_INTEGER:
370 ** SQLITE_AFF_NUMERIC:
371 ** Try to convert pRec to an integer representation or a
372 ** floating-point representation if an integer representation
373 ** is not possible. Note that the integer representation is
374 ** always preferred, even if the affinity is REAL, because
375 ** an integer representation is more space efficient on disk.
377 ** SQLITE_AFF_FLEXNUM:
378 ** If the value is text, then try to convert it into a number of
379 ** some kind (integer or real) but do not make any other changes.
382 ** Convert pRec to a text representation.
386 ** No-op. pRec is unchanged.
388 static void applyAffinity(
389 Mem
*pRec
, /* The value to apply affinity to */
390 char affinity
, /* The affinity to be applied */
391 u8 enc
/* Use this text encoding */
393 if( affinity
>=SQLITE_AFF_NUMERIC
){
394 assert( affinity
==SQLITE_AFF_INTEGER
|| affinity
==SQLITE_AFF_REAL
395 || affinity
==SQLITE_AFF_NUMERIC
|| affinity
==SQLITE_AFF_FLEXNUM
);
396 if( (pRec
->flags
& MEM_Int
)==0 ){ /*OPTIMIZATION-IF-FALSE*/
397 if( (pRec
->flags
& (MEM_Real
|MEM_IntReal
))==0 ){
398 if( pRec
->flags
& MEM_Str
) applyNumericAffinity(pRec
,1);
399 }else if( affinity
<=SQLITE_AFF_REAL
){
400 sqlite3VdbeIntegerAffinity(pRec
);
403 }else if( affinity
==SQLITE_AFF_TEXT
){
404 /* Only attempt the conversion to TEXT if there is an integer or real
405 ** representation (blob and NULL do not get converted) but no string
406 ** representation. It would be harmless to repeat the conversion if
407 ** there is already a string rep, but it is pointless to waste those
409 if( 0==(pRec
->flags
&MEM_Str
) ){ /*OPTIMIZATION-IF-FALSE*/
410 if( (pRec
->flags
&(MEM_Real
|MEM_Int
|MEM_IntReal
)) ){
411 testcase( pRec
->flags
& MEM_Int
);
412 testcase( pRec
->flags
& MEM_Real
);
413 testcase( pRec
->flags
& MEM_IntReal
);
414 sqlite3VdbeMemStringify(pRec
, enc
, 1);
417 pRec
->flags
&= ~(MEM_Real
|MEM_Int
|MEM_IntReal
);
422 ** Try to convert the type of a function argument or a result column
423 ** into a numeric representation. Use either INTEGER or REAL whichever
424 ** is appropriate. But only do the conversion if it is possible without
425 ** loss of information and return the revised type of the argument.
427 int sqlite3_value_numeric_type(sqlite3_value
*pVal
){
428 int eType
= sqlite3_value_type(pVal
);
429 if( eType
==SQLITE_TEXT
){
430 Mem
*pMem
= (Mem
*)pVal
;
431 applyNumericAffinity(pMem
, 0);
432 eType
= sqlite3_value_type(pVal
);
438 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
439 ** not the internal Mem* type.
441 void sqlite3ValueApplyAffinity(
446 applyAffinity((Mem
*)pVal
, affinity
, enc
);
450 ** pMem currently only holds a string type (or maybe a BLOB that we can
451 ** interpret as a string if we want to). Compute its corresponding
452 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
455 static u16 SQLITE_NOINLINE
computeNumericType(Mem
*pMem
){
458 assert( (pMem
->flags
& (MEM_Int
|MEM_Real
|MEM_IntReal
))==0 );
459 assert( (pMem
->flags
& (MEM_Str
|MEM_Blob
))!=0 );
460 if( ExpandBlob(pMem
) ){
464 rc
= sqlite3AtoF(pMem
->z
, &pMem
->u
.r
, pMem
->n
, pMem
->enc
);
466 if( rc
==0 && sqlite3Atoi64(pMem
->z
, &ix
, pMem
->n
, pMem
->enc
)<=1 ){
472 }else if( rc
==1 && sqlite3Atoi64(pMem
->z
, &ix
, pMem
->n
, pMem
->enc
)==0 ){
480 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
483 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
484 ** But it does set pMem->u.r and pMem->u.i appropriately.
486 static u16
numericType(Mem
*pMem
){
487 assert( (pMem
->flags
& MEM_Null
)==0
488 || pMem
->db
==0 || pMem
->db
->mallocFailed
);
489 if( pMem
->flags
& (MEM_Int
|MEM_Real
|MEM_IntReal
|MEM_Null
) ){
490 testcase( pMem
->flags
& MEM_Int
);
491 testcase( pMem
->flags
& MEM_Real
);
492 testcase( pMem
->flags
& MEM_IntReal
);
493 return pMem
->flags
& (MEM_Int
|MEM_Real
|MEM_IntReal
|MEM_Null
);
495 assert( pMem
->flags
& (MEM_Str
|MEM_Blob
) );
496 testcase( pMem
->flags
& MEM_Str
);
497 testcase( pMem
->flags
& MEM_Blob
);
498 return computeNumericType(pMem
);
504 ** Write a nice string representation of the contents of cell pMem
505 ** into buffer zBuf, length nBuf.
507 void sqlite3VdbeMemPrettyPrint(Mem
*pMem
, StrAccum
*pStr
){
509 static const char *const encnames
[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
515 assert( (f
& (MEM_Static
|MEM_Ephem
))==0 );
516 }else if( f
& MEM_Static
){
518 assert( (f
& (MEM_Dyn
|MEM_Ephem
))==0 );
519 }else if( f
& MEM_Ephem
){
521 assert( (f
& (MEM_Static
|MEM_Dyn
))==0 );
525 sqlite3_str_appendf(pStr
, "%cx[", c
);
526 for(i
=0; i
<25 && i
<pMem
->n
; i
++){
527 sqlite3_str_appendf(pStr
, "%02X", ((int)pMem
->z
[i
] & 0xFF));
529 sqlite3_str_appendf(pStr
, "|");
530 for(i
=0; i
<25 && i
<pMem
->n
; i
++){
532 sqlite3_str_appendchar(pStr
, 1, (z
<32||z
>126)?'.':z
);
534 sqlite3_str_appendf(pStr
,"]");
536 sqlite3_str_appendf(pStr
, "+%dz",pMem
->u
.nZero
);
538 }else if( f
& MEM_Str
){
543 assert( (f
& (MEM_Static
|MEM_Ephem
))==0 );
544 }else if( f
& MEM_Static
){
546 assert( (f
& (MEM_Dyn
|MEM_Ephem
))==0 );
547 }else if( f
& MEM_Ephem
){
549 assert( (f
& (MEM_Static
|MEM_Dyn
))==0 );
553 sqlite3_str_appendf(pStr
, " %c%d[", c
, pMem
->n
);
554 for(j
=0; j
<25 && j
<pMem
->n
; j
++){
556 sqlite3_str_appendchar(pStr
, 1, (c
>=0x20&&c
<=0x7f) ? c
: '.');
558 sqlite3_str_appendf(pStr
, "]%s", encnames
[pMem
->enc
]);
565 ** Print the value of a register for tracing purposes:
567 static void memTracePrint(Mem
*p
){
568 if( p
->flags
& MEM_Undefined
){
569 printf(" undefined");
570 }else if( p
->flags
& MEM_Null
){
571 printf(p
->flags
& MEM_Zero
? " NULL-nochng" : " NULL");
572 }else if( (p
->flags
& (MEM_Int
|MEM_Str
))==(MEM_Int
|MEM_Str
) ){
573 printf(" si:%lld", p
->u
.i
);
574 }else if( (p
->flags
& (MEM_IntReal
))!=0 ){
575 printf(" ir:%lld", p
->u
.i
);
576 }else if( p
->flags
& MEM_Int
){
577 printf(" i:%lld", p
->u
.i
);
578 #ifndef SQLITE_OMIT_FLOATING_POINT
579 }else if( p
->flags
& MEM_Real
){
580 printf(" r:%.17g", p
->u
.r
);
582 }else if( sqlite3VdbeMemIsRowSet(p
) ){
587 sqlite3StrAccumInit(&acc
, 0, zBuf
, sizeof(zBuf
), 0);
588 sqlite3VdbeMemPrettyPrint(p
, &acc
);
589 printf(" %s", sqlite3StrAccumFinish(&acc
));
591 if( p
->flags
& MEM_Subtype
) printf(" subtype=0x%02x", p
->eSubtype
);
593 static void registerTrace(int iReg
, Mem
*p
){
594 printf("R[%d] = ", iReg
);
597 printf(" <== R[%d]", (int)(p
->pScopyFrom
- &p
[-iReg
]));
600 sqlite3VdbeCheckMemInvariants(p
);
602 /**/ void sqlite3PrintMem(Mem
*pMem
){
611 ** Show the values of all registers in the virtual machine. Used for
612 ** interactive debugging.
614 void sqlite3VdbeRegisterDump(Vdbe
*v
){
616 for(i
=1; i
<v
->nMem
; i
++) registerTrace(i
, v
->aMem
+i
);
618 #endif /* SQLITE_DEBUG */
622 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
624 # define REGISTER_TRACE(R,M)
629 ** This function is only called from within an assert() expression. It
630 ** checks that the sqlite3.nTransaction variable is correctly set to
631 ** the number of non-transaction savepoints currently in the
632 ** linked list starting at sqlite3.pSavepoint.
636 ** assert( checkSavepointCount(db) );
638 static int checkSavepointCount(sqlite3
*db
){
641 for(p
=db
->pSavepoint
; p
; p
=p
->pNext
) n
++;
642 assert( n
==(db
->nSavepoint
+ db
->isTransactionSavepoint
) );
648 ** Return the register of pOp->p2 after first preparing it to be
649 ** overwritten with an integer value.
651 static SQLITE_NOINLINE Mem
*out2PrereleaseWithClear(Mem
*pOut
){
652 sqlite3VdbeMemSetNull(pOut
);
653 pOut
->flags
= MEM_Int
;
656 static Mem
*out2Prerelease(Vdbe
*p
, VdbeOp
*pOp
){
659 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
660 pOut
= &p
->aMem
[pOp
->p2
];
661 memAboutToChange(p
, pOut
);
662 if( VdbeMemDynamic(pOut
) ){ /*OPTIMIZATION-IF-FALSE*/
663 return out2PrereleaseWithClear(pOut
);
665 pOut
->flags
= MEM_Int
;
671 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
672 ** with pOp->p3. Return the hash.
674 static u64
filterHash(const Mem
*aMem
, const Op
*pOp
){
678 assert( pOp
->p4type
==P4_INT32
);
679 for(i
=pOp
->p3
, mx
=i
+pOp
->p4
.i
; i
<mx
; i
++){
680 const Mem
*p
= &aMem
[i
];
681 if( p
->flags
& (MEM_Int
|MEM_IntReal
) ){
683 }else if( p
->flags
& MEM_Real
){
684 h
+= sqlite3VdbeIntValue(p
);
685 }else if( p
->flags
& (MEM_Str
|MEM_Blob
) ){
693 ** Return the symbolic name for the data type of a pMem
695 static const char *vdbeMemTypeName(Mem
*pMem
){
696 static const char *azTypes
[] = {
697 /* SQLITE_INTEGER */ "INT",
698 /* SQLITE_FLOAT */ "REAL",
699 /* SQLITE_TEXT */ "TEXT",
700 /* SQLITE_BLOB */ "BLOB",
701 /* SQLITE_NULL */ "NULL"
703 return azTypes
[sqlite3_value_type(pMem
)-1];
707 ** Execute as much of a VDBE program as we can.
708 ** This is the core of sqlite3_step().
711 Vdbe
*p
/* The VDBE */
713 Op
*aOp
= p
->aOp
; /* Copy of p->aOp */
714 Op
*pOp
= aOp
; /* Current operation */
716 Op
*pOrigOp
; /* Value of pOp at the top of the loop */
717 int nExtraDelete
= 0; /* Verifies FORDELETE and AUXDELETE flags */
718 u8 iCompareIsInit
= 0; /* iCompare is initialized */
720 int rc
= SQLITE_OK
; /* Value to return */
721 sqlite3
*db
= p
->db
; /* The database */
722 u8 resetSchemaOnFault
= 0; /* Reset schema after an error if positive */
723 u8 encoding
= ENC(db
); /* The database encoding */
724 int iCompare
= 0; /* Result of last comparison */
725 u64 nVmStep
= 0; /* Number of virtual machine steps */
726 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
727 u64 nProgressLimit
; /* Invoke xProgress() when nVmStep reaches this */
729 Mem
*aMem
= p
->aMem
; /* Copy of p->aMem */
730 Mem
*pIn1
= 0; /* 1st input operand */
731 Mem
*pIn2
= 0; /* 2nd input operand */
732 Mem
*pIn3
= 0; /* 3rd input operand */
733 Mem
*pOut
= 0; /* Output operand */
734 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
737 /*** INSERT STACK UNION HERE ***/
739 assert( p
->eVdbeState
==VDBE_RUN_STATE
); /* sqlite3_step() verifies this */
740 if( DbMaskNonZero(p
->lockMask
) ){
743 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
745 u32 iPrior
= p
->aCounter
[SQLITE_STMTSTATUS_VM_STEP
];
746 assert( 0 < db
->nProgressOps
);
747 nProgressLimit
= db
->nProgressOps
- (iPrior
% db
->nProgressOps
);
749 nProgressLimit
= LARGEST_UINT64
;
752 if( p
->rc
==SQLITE_NOMEM
){
753 /* This happens if a malloc() inside a call to sqlite3_column_text() or
754 ** sqlite3_column_text16() failed. */
757 assert( p
->rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_BUSY
);
758 testcase( p
->rc
!=SQLITE_OK
);
760 assert( p
->bIsReader
|| p
->readOnly
!=0 );
762 assert( p
->explain
==0 );
763 db
->busyHandler
.nBusy
= 0;
764 if( AtomicLoad(&db
->u1
.isInterrupted
) ) goto abort_due_to_interrupt
;
765 sqlite3VdbeIOTraceSql(p
);
767 sqlite3BeginBenignMalloc();
769 && (p
->db
->flags
& (SQLITE_VdbeListing
|SQLITE_VdbeEQP
|SQLITE_VdbeTrace
))!=0
773 sqlite3VdbePrintSql(p
);
774 if( p
->db
->flags
& SQLITE_VdbeListing
){
775 printf("VDBE Program Listing:\n");
776 for(i
=0; i
<p
->nOp
; i
++){
777 sqlite3VdbePrintOp(stdout
, i
, &aOp
[i
]);
780 if( p
->db
->flags
& SQLITE_VdbeEQP
){
781 for(i
=0; i
<p
->nOp
; i
++){
782 if( aOp
[i
].opcode
==OP_Explain
){
783 if( once
) printf("VDBE Query Plan:\n");
784 printf("%s\n", aOp
[i
].p4
.z
);
789 if( p
->db
->flags
& SQLITE_VdbeTrace
) printf("VDBE Trace:\n");
791 sqlite3EndBenignMalloc();
793 for(pOp
=&aOp
[p
->pc
]; 1; pOp
++){
794 /* Errors are detected by individual opcodes, with an immediate
795 ** jumps to abort_due_to_error. */
796 assert( rc
==SQLITE_OK
);
798 assert( pOp
>=aOp
&& pOp
<&aOp
[p
->nOp
]);
800 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
802 pnCycle
= &pOp
->nCycle
;
804 if( sqlite3NProfileCnt
==0 )
806 *pnCycle
-= sqlite3Hwtime();
809 /* Only allow tracing if SQLITE_DEBUG is defined.
812 if( db
->flags
& SQLITE_VdbeTrace
){
813 sqlite3VdbePrintOp(stdout
, (int)(pOp
- aOp
), pOp
);
814 test_trace_breakpoint((int)(pOp
- aOp
),pOp
,p
);
819 /* Check to see if we need to simulate an interrupt. This only happens
820 ** if we have a special test build.
823 if( sqlite3_interrupt_count
>0 ){
824 sqlite3_interrupt_count
--;
825 if( sqlite3_interrupt_count
==0 ){
826 sqlite3_interrupt(db
);
831 /* Sanity checking on other operands */
834 u8 opProperty
= sqlite3OpcodeProperty
[pOp
->opcode
];
835 if( (opProperty
& OPFLG_IN1
)!=0 ){
837 assert( pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
838 assert( memIsValid(&aMem
[pOp
->p1
]) );
839 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p1
]) );
840 REGISTER_TRACE(pOp
->p1
, &aMem
[pOp
->p1
]);
842 if( (opProperty
& OPFLG_IN2
)!=0 ){
844 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
845 assert( memIsValid(&aMem
[pOp
->p2
]) );
846 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p2
]) );
847 REGISTER_TRACE(pOp
->p2
, &aMem
[pOp
->p2
]);
849 if( (opProperty
& OPFLG_IN3
)!=0 ){
851 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
852 assert( memIsValid(&aMem
[pOp
->p3
]) );
853 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p3
]) );
854 REGISTER_TRACE(pOp
->p3
, &aMem
[pOp
->p3
]);
856 if( (opProperty
& OPFLG_OUT2
)!=0 ){
858 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
859 memAboutToChange(p
, &aMem
[pOp
->p2
]);
861 if( (opProperty
& OPFLG_OUT3
)!=0 ){
863 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
864 memAboutToChange(p
, &aMem
[pOp
->p3
]);
872 switch( pOp
->opcode
){
874 /*****************************************************************************
875 ** What follows is a massive switch statement where each case implements a
876 ** separate instruction in the virtual machine. If we follow the usual
877 ** indentation conventions, each case should be indented by 6 spaces. But
878 ** that is a lot of wasted space on the left margin. So the code within
879 ** the switch statement will break with convention and be flush-left. Another
880 ** big comment (similar to this one) will mark the point in the code where
881 ** we transition back to normal indentation.
883 ** The formatting of each case is important. The makefile for SQLite
884 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
885 ** file looking for lines that begin with "case OP_". The opcodes.h files
886 ** will be filled with #defines that give unique integer values to each
887 ** opcode and the opcodes.c file is filled with an array of strings where
888 ** each string is the symbolic name for the corresponding opcode. If the
889 ** case statement is followed by a comment of the form "/# same as ... #/"
890 ** that comment is used to determine the particular value of the opcode.
892 ** Other keywords in the comment that follows each case are used to
893 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
894 ** Keywords include: in1, in2, in3, out2, out3. See
895 ** the mkopcodeh.awk script for additional information.
897 ** Documentation about VDBE opcodes is generated by scanning this file
898 ** for lines of that contain "Opcode:". That line and all subsequent
899 ** comment lines are used in the generation of the opcode.html documentation
904 ** Formatting is important to scripts that scan this file.
905 ** Do not deviate from the formatting style currently in use.
907 *****************************************************************************/
909 /* Opcode: Goto * P2 * * *
911 ** An unconditional jump to address P2.
912 ** The next instruction executed will be
913 ** the one at index P2 from the beginning of
916 ** The P1 parameter is not actually used by this opcode. However, it
917 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
918 ** that this Goto is the bottom of a loop and that the lines from P2 down
919 ** to the current line should be indented for EXPLAIN output.
921 case OP_Goto
: { /* jump */
924 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
925 ** means we should really jump back to the preceeding OP_ReleaseReg
928 assert( pOp
->p2
< (int)(pOp
- aOp
) );
929 assert( pOp
->p2
> 1 );
930 pOp
= &aOp
[pOp
->p2
- 2];
931 assert( pOp
[1].opcode
==OP_ReleaseReg
);
932 goto check_for_interrupt
;
936 jump_to_p2_and_check_for_interrupt
:
937 pOp
= &aOp
[pOp
->p2
- 1];
939 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
940 ** OP_VNext, or OP_SorterNext) all jump here upon
941 ** completion. Check to see if sqlite3_interrupt() has been called
942 ** or if the progress callback needs to be invoked.
944 ** This code uses unstructured "goto" statements and does not look clean.
945 ** But that is not due to sloppy coding habits. The code is written this
946 ** way for performance, to avoid having to run the interrupt and progress
947 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
948 ** faster according to "valgrind --tool=cachegrind" */
950 if( AtomicLoad(&db
->u1
.isInterrupted
) ) goto abort_due_to_interrupt
;
951 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
952 /* Call the progress callback if it is configured and the required number
953 ** of VDBE ops have been executed (either since this invocation of
954 ** sqlite3VdbeExec() or since last time the progress callback was called).
955 ** If the progress callback returns non-zero, exit the virtual machine with
956 ** a return code SQLITE_ABORT.
958 while( nVmStep
>=nProgressLimit
&& db
->xProgress
!=0 ){
959 assert( db
->nProgressOps
!=0 );
960 nProgressLimit
+= db
->nProgressOps
;
961 if( db
->xProgress(db
->pProgressArg
) ){
962 nProgressLimit
= LARGEST_UINT64
;
963 rc
= SQLITE_INTERRUPT
;
964 goto abort_due_to_error
;
972 /* Opcode: Gosub P1 P2 * * *
974 ** Write the current address onto register P1
975 ** and then jump to address P2.
977 case OP_Gosub
: { /* jump */
978 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
979 pIn1
= &aMem
[pOp
->p1
];
980 assert( VdbeMemDynamic(pIn1
)==0 );
981 memAboutToChange(p
, pIn1
);
982 pIn1
->flags
= MEM_Int
;
983 pIn1
->u
.i
= (int)(pOp
-aOp
);
984 REGISTER_TRACE(pOp
->p1
, pIn1
);
985 goto jump_to_p2_and_check_for_interrupt
;
988 /* Opcode: Return P1 P2 P3 * *
990 ** Jump to the address stored in register P1. If P1 is a return address
991 ** register, then this accomplishes a return from a subroutine.
993 ** If P3 is 1, then the jump is only taken if register P1 holds an integer
994 ** values, otherwise execution falls through to the next opcode, and the
995 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
996 ** integer or else an assert() is raised. P3 should be set to 1 when
997 ** this opcode is used in combination with OP_BeginSubrtn, and set to 0
1000 ** The value in register P1 is unchanged by this opcode.
1002 ** P2 is not used by the byte-code engine. However, if P2 is positive
1003 ** and also less than the current address, then the "EXPLAIN" output
1004 ** formatter in the CLI will indent all opcodes from the P2 opcode up
1005 ** to be not including the current Return. P2 should be the first opcode
1006 ** in the subroutine from which this opcode is returning. Thus the P2
1007 ** value is a byte-code indentation hint. See tag-20220407a in
1008 ** wherecode.c and shell.c.
1010 case OP_Return
: { /* in1 */
1011 pIn1
= &aMem
[pOp
->p1
];
1012 if( pIn1
->flags
& MEM_Int
){
1013 if( pOp
->p3
){ VdbeBranchTaken(1, 2); }
1014 pOp
= &aOp
[pIn1
->u
.i
];
1015 }else if( ALWAYS(pOp
->p3
) ){
1016 VdbeBranchTaken(0, 2);
1021 /* Opcode: InitCoroutine P1 P2 P3 * *
1023 ** Set up register P1 so that it will Yield to the coroutine
1024 ** located at address P3.
1026 ** If P2!=0 then the coroutine implementation immediately follows
1027 ** this opcode. So jump over the coroutine implementation to
1030 ** See also: EndCoroutine
1032 case OP_InitCoroutine
: { /* jump */
1033 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
1034 assert( pOp
->p2
>=0 && pOp
->p2
<p
->nOp
);
1035 assert( pOp
->p3
>=0 && pOp
->p3
<p
->nOp
);
1036 pOut
= &aMem
[pOp
->p1
];
1037 assert( !VdbeMemDynamic(pOut
) );
1038 pOut
->u
.i
= pOp
->p3
- 1;
1039 pOut
->flags
= MEM_Int
;
1040 if( pOp
->p2
==0 ) break;
1042 /* Most jump operations do a goto to this spot in order to update
1043 ** the pOp pointer. */
1045 assert( pOp
->p2
>0 ); /* There are never any jumps to instruction 0 */
1046 assert( pOp
->p2
<p
->nOp
); /* Jumps must be in range */
1047 pOp
= &aOp
[pOp
->p2
- 1];
1051 /* Opcode: EndCoroutine P1 * * * *
1053 ** The instruction at the address in register P1 is a Yield.
1054 ** Jump to the P2 parameter of that Yield.
1055 ** After the jump, register P1 becomes undefined.
1057 ** See also: InitCoroutine
1059 case OP_EndCoroutine
: { /* in1 */
1061 pIn1
= &aMem
[pOp
->p1
];
1062 assert( pIn1
->flags
==MEM_Int
);
1063 assert( pIn1
->u
.i
>=0 && pIn1
->u
.i
<p
->nOp
);
1064 pCaller
= &aOp
[pIn1
->u
.i
];
1065 assert( pCaller
->opcode
==OP_Yield
);
1066 assert( pCaller
->p2
>=0 && pCaller
->p2
<p
->nOp
);
1067 pOp
= &aOp
[pCaller
->p2
- 1];
1068 pIn1
->flags
= MEM_Undefined
;
1072 /* Opcode: Yield P1 P2 * * *
1074 ** Swap the program counter with the value in register P1. This
1075 ** has the effect of yielding to a coroutine.
1077 ** If the coroutine that is launched by this instruction ends with
1078 ** Yield or Return then continue to the next instruction. But if
1079 ** the coroutine launched by this instruction ends with
1080 ** EndCoroutine, then jump to P2 rather than continuing with the
1081 ** next instruction.
1083 ** See also: InitCoroutine
1085 case OP_Yield
: { /* in1, jump */
1087 pIn1
= &aMem
[pOp
->p1
];
1088 assert( VdbeMemDynamic(pIn1
)==0 );
1089 pIn1
->flags
= MEM_Int
;
1090 pcDest
= (int)pIn1
->u
.i
;
1091 pIn1
->u
.i
= (int)(pOp
- aOp
);
1092 REGISTER_TRACE(pOp
->p1
, pIn1
);
1097 /* Opcode: HaltIfNull P1 P2 P3 P4 P5
1098 ** Synopsis: if r[P3]=null halt
1100 ** Check the value in register P3. If it is NULL then Halt using
1101 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1102 ** value in register P3 is not NULL, then this routine is a no-op.
1103 ** The P5 parameter should be 1.
1105 case OP_HaltIfNull
: { /* in3 */
1106 pIn3
= &aMem
[pOp
->p3
];
1108 if( pOp
->p2
==OE_Abort
){ sqlite3VdbeAssertAbortable(p
); }
1110 if( (pIn3
->flags
& MEM_Null
)==0 ) break;
1111 /* Fall through into OP_Halt */
1112 /* no break */ deliberate_fall_through
1115 /* Opcode: Halt P1 P2 * P4 P5
1117 ** Exit immediately. All open cursors, etc are closed
1120 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1121 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1122 ** For errors, it can be some other value. If P1!=0 then P2 will determine
1123 ** whether or not to rollback the current transaction. Do not rollback
1124 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1125 ** then back out all changes that have occurred during this execution of the
1126 ** VDBE, but do not rollback the transaction.
1128 ** If P4 is not null then it is an error message string.
1130 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1133 ** 1: NOT NULL contraint failed: P4
1134 ** 2: UNIQUE constraint failed: P4
1135 ** 3: CHECK constraint failed: P4
1136 ** 4: FOREIGN KEY constraint failed: P4
1138 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
1141 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
1142 ** every program. So a jump past the last instruction of the program
1143 ** is the same as executing Halt.
1150 if( pOp
->p2
==OE_Abort
){ sqlite3VdbeAssertAbortable(p
); }
1153 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates
1154 ** something is wrong with the code generator. Raise an assertion in order
1155 ** to bring this to the attention of fuzzers and other testing tools. */
1156 assert( pOp
->p1
!=SQLITE_INTERNAL
);
1158 if( p
->pFrame
&& pOp
->p1
==SQLITE_OK
){
1159 /* Halt the sub-program. Return control to the parent frame. */
1161 p
->pFrame
= pFrame
->pParent
;
1163 sqlite3VdbeSetChanges(db
, p
->nChange
);
1164 pcx
= sqlite3VdbeFrameRestore(pFrame
);
1165 if( pOp
->p2
==OE_Ignore
){
1166 /* Instruction pcx is the OP_Program that invoked the sub-program
1167 ** currently being halted. If the p2 instruction of this OP_Halt
1168 ** instruction is set to OE_Ignore, then the sub-program is throwing
1169 ** an IGNORE exception. In this case jump to the address specified
1170 ** as the p2 of the calling OP_Program. */
1171 pcx
= p
->aOp
[pcx
].p2
-1;
1179 p
->errorAction
= (u8
)pOp
->p2
;
1180 assert( pOp
->p5
<=4 );
1183 static const char * const azType
[] = { "NOT NULL", "UNIQUE", "CHECK",
1185 testcase( pOp
->p5
==1 );
1186 testcase( pOp
->p5
==2 );
1187 testcase( pOp
->p5
==3 );
1188 testcase( pOp
->p5
==4 );
1189 sqlite3VdbeError(p
, "%s constraint failed", azType
[pOp
->p5
-1]);
1191 p
->zErrMsg
= sqlite3MPrintf(db
, "%z: %s", p
->zErrMsg
, pOp
->p4
.z
);
1194 sqlite3VdbeError(p
, "%s", pOp
->p4
.z
);
1196 pcx
= (int)(pOp
- aOp
);
1197 sqlite3_log(pOp
->p1
, "abort at %d in [%s]: %s", pcx
, p
->zSql
, p
->zErrMsg
);
1199 rc
= sqlite3VdbeHalt(p
);
1200 assert( rc
==SQLITE_BUSY
|| rc
==SQLITE_OK
|| rc
==SQLITE_ERROR
);
1201 if( rc
==SQLITE_BUSY
){
1202 p
->rc
= SQLITE_BUSY
;
1204 assert( rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_CONSTRAINT
);
1205 assert( rc
==SQLITE_OK
|| db
->nDeferredCons
>0 || db
->nDeferredImmCons
>0 );
1206 rc
= p
->rc
? SQLITE_ERROR
: SQLITE_DONE
;
1211 /* Opcode: Integer P1 P2 * * *
1212 ** Synopsis: r[P2]=P1
1214 ** The 32-bit integer value P1 is written into register P2.
1216 case OP_Integer
: { /* out2 */
1217 pOut
= out2Prerelease(p
, pOp
);
1218 pOut
->u
.i
= pOp
->p1
;
1222 /* Opcode: Int64 * P2 * P4 *
1223 ** Synopsis: r[P2]=P4
1225 ** P4 is a pointer to a 64-bit integer value.
1226 ** Write that value into register P2.
1228 case OP_Int64
: { /* out2 */
1229 pOut
= out2Prerelease(p
, pOp
);
1230 assert( pOp
->p4
.pI64
!=0 );
1231 pOut
->u
.i
= *pOp
->p4
.pI64
;
1235 #ifndef SQLITE_OMIT_FLOATING_POINT
1236 /* Opcode: Real * P2 * P4 *
1237 ** Synopsis: r[P2]=P4
1239 ** P4 is a pointer to a 64-bit floating point value.
1240 ** Write that value into register P2.
1242 case OP_Real
: { /* same as TK_FLOAT, out2 */
1243 pOut
= out2Prerelease(p
, pOp
);
1244 pOut
->flags
= MEM_Real
;
1245 assert( !sqlite3IsNaN(*pOp
->p4
.pReal
) );
1246 pOut
->u
.r
= *pOp
->p4
.pReal
;
1251 /* Opcode: String8 * P2 * P4 *
1252 ** Synopsis: r[P2]='P4'
1254 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
1255 ** into a String opcode before it is executed for the first time. During
1256 ** this transformation, the length of string P4 is computed and stored
1257 ** as the P1 parameter.
1259 case OP_String8
: { /* same as TK_STRING, out2 */
1260 assert( pOp
->p4
.z
!=0 );
1261 pOut
= out2Prerelease(p
, pOp
);
1262 pOp
->p1
= sqlite3Strlen30(pOp
->p4
.z
);
1264 #ifndef SQLITE_OMIT_UTF16
1265 if( encoding
!=SQLITE_UTF8
){
1266 rc
= sqlite3VdbeMemSetStr(pOut
, pOp
->p4
.z
, -1, SQLITE_UTF8
, SQLITE_STATIC
);
1267 assert( rc
==SQLITE_OK
|| rc
==SQLITE_TOOBIG
);
1268 if( rc
) goto too_big
;
1269 if( SQLITE_OK
!=sqlite3VdbeChangeEncoding(pOut
, encoding
) ) goto no_mem
;
1270 assert( pOut
->szMalloc
>0 && pOut
->zMalloc
==pOut
->z
);
1271 assert( VdbeMemDynamic(pOut
)==0 );
1273 pOut
->flags
|= MEM_Static
;
1274 if( pOp
->p4type
==P4_DYNAMIC
){
1275 sqlite3DbFree(db
, pOp
->p4
.z
);
1277 pOp
->p4type
= P4_DYNAMIC
;
1278 pOp
->p4
.z
= pOut
->z
;
1282 if( pOp
->p1
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1285 pOp
->opcode
= OP_String
;
1286 assert( rc
==SQLITE_OK
);
1287 /* Fall through to the next case, OP_String */
1288 /* no break */ deliberate_fall_through
1291 /* Opcode: String P1 P2 P3 P4 P5
1292 ** Synopsis: r[P2]='P4' (len=P1)
1294 ** The string value P4 of length P1 (bytes) is stored in register P2.
1296 ** If P3 is not zero and the content of register P3 is equal to P5, then
1297 ** the datatype of the register P2 is converted to BLOB. The content is
1298 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
1299 ** of a string, as if it had been CAST. In other words:
1301 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
1303 case OP_String
: { /* out2 */
1304 assert( pOp
->p4
.z
!=0 );
1305 pOut
= out2Prerelease(p
, pOp
);
1306 pOut
->flags
= MEM_Str
|MEM_Static
|MEM_Term
;
1307 pOut
->z
= pOp
->p4
.z
;
1309 pOut
->enc
= encoding
;
1310 UPDATE_MAX_BLOBSIZE(pOut
);
1311 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
1313 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
1314 pIn3
= &aMem
[pOp
->p3
];
1315 assert( pIn3
->flags
& MEM_Int
);
1316 if( pIn3
->u
.i
==pOp
->p5
) pOut
->flags
= MEM_Blob
|MEM_Static
|MEM_Term
;
1322 /* Opcode: BeginSubrtn * P2 * * *
1323 ** Synopsis: r[P2]=NULL
1325 ** Mark the beginning of a subroutine that can be entered in-line
1326 ** or that can be called using OP_Gosub. The subroutine should
1327 ** be terminated by an OP_Return instruction that has a P1 operand that
1328 ** is the same as the P2 operand to this opcode and that has P3 set to 1.
1329 ** If the subroutine is entered in-line, then the OP_Return will simply
1330 ** fall through. But if the subroutine is entered using OP_Gosub, then
1331 ** the OP_Return will jump back to the first instruction after the OP_Gosub.
1333 ** This routine works by loading a NULL into the P2 register. When the
1334 ** return address register contains a NULL, the OP_Return instruction is
1335 ** a no-op that simply falls through to the next instruction (assuming that
1336 ** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
1337 ** entered in-line, then the OP_Return will cause in-line execution to
1338 ** continue. But if the subroutine is entered via OP_Gosub, then the
1339 ** OP_Return will cause a return to the address following the OP_Gosub.
1341 ** This opcode is identical to OP_Null. It has a different name
1342 ** only to make the byte code easier to read and verify.
1344 /* Opcode: Null P1 P2 P3 * *
1345 ** Synopsis: r[P2..P3]=NULL
1347 ** Write a NULL into registers P2. If P3 greater than P2, then also write
1348 ** NULL into register P3 and every register in between P2 and P3. If P3
1349 ** is less than P2 (typically P3 is zero) then only register P2 is
1352 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1353 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1356 case OP_BeginSubrtn
:
1357 case OP_Null
: { /* out2 */
1360 pOut
= out2Prerelease(p
, pOp
);
1361 cnt
= pOp
->p3
-pOp
->p2
;
1362 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
1363 pOut
->flags
= nullFlag
= pOp
->p1
? (MEM_Null
|MEM_Cleared
) : MEM_Null
;
1370 memAboutToChange(p
, pOut
);
1371 sqlite3VdbeMemSetNull(pOut
);
1372 pOut
->flags
= nullFlag
;
1379 /* Opcode: SoftNull P1 * * * *
1380 ** Synopsis: r[P1]=NULL
1382 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1383 ** instruction, but do not free any string or blob memory associated with
1384 ** the register, so that if the value was a string or blob that was
1385 ** previously copied using OP_SCopy, the copies will continue to be valid.
1388 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
1389 pOut
= &aMem
[pOp
->p1
];
1390 pOut
->flags
= (pOut
->flags
&~(MEM_Undefined
|MEM_AffMask
))|MEM_Null
;
1394 /* Opcode: Blob P1 P2 * P4 *
1395 ** Synopsis: r[P2]=P4 (len=P1)
1397 ** P4 points to a blob of data P1 bytes long. Store this
1398 ** blob in register P2. If P4 is a NULL pointer, then construct
1399 ** a zero-filled blob that is P1 bytes long in P2.
1401 case OP_Blob
: { /* out2 */
1402 assert( pOp
->p1
<= SQLITE_MAX_LENGTH
);
1403 pOut
= out2Prerelease(p
, pOp
);
1405 sqlite3VdbeMemSetZeroBlob(pOut
, pOp
->p1
);
1406 if( sqlite3VdbeMemExpandBlob(pOut
) ) goto no_mem
;
1408 sqlite3VdbeMemSetStr(pOut
, pOp
->p4
.z
, pOp
->p1
, 0, 0);
1410 pOut
->enc
= encoding
;
1411 UPDATE_MAX_BLOBSIZE(pOut
);
1415 /* Opcode: Variable P1 P2 * P4 *
1416 ** Synopsis: r[P2]=parameter(P1,P4)
1418 ** Transfer the values of bound parameter P1 into register P2
1420 ** If the parameter is named, then its name appears in P4.
1421 ** The P4 value is used by sqlite3_bind_parameter_name().
1423 case OP_Variable
: { /* out2 */
1424 Mem
*pVar
; /* Value being transferred */
1426 assert( pOp
->p1
>0 && pOp
->p1
<=p
->nVar
);
1427 assert( pOp
->p4
.z
==0 || pOp
->p4
.z
==sqlite3VListNumToName(p
->pVList
,pOp
->p1
) );
1428 pVar
= &p
->aVar
[pOp
->p1
- 1];
1429 if( sqlite3VdbeMemTooBig(pVar
) ){
1432 pOut
= &aMem
[pOp
->p2
];
1433 if( VdbeMemDynamic(pOut
) ) sqlite3VdbeMemSetNull(pOut
);
1434 memcpy(pOut
, pVar
, MEMCELLSIZE
);
1435 pOut
->flags
&= ~(MEM_Dyn
|MEM_Ephem
);
1436 pOut
->flags
|= MEM_Static
|MEM_FromBind
;
1437 UPDATE_MAX_BLOBSIZE(pOut
);
1441 /* Opcode: Move P1 P2 P3 * *
1442 ** Synopsis: r[P2@P3]=r[P1@P3]
1444 ** Move the P3 values in register P1..P1+P3-1 over into
1445 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
1446 ** left holding a NULL. It is an error for register ranges
1447 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1448 ** for P3 to be less than 1.
1451 int n
; /* Number of registers left to copy */
1452 int p1
; /* Register to copy from */
1453 int p2
; /* Register to copy to */
1458 assert( n
>0 && p1
>0 && p2
>0 );
1459 assert( p1
+n
<=p2
|| p2
+n
<=p1
);
1464 assert( pOut
<=&aMem
[(p
->nMem
+1 - p
->nCursor
)] );
1465 assert( pIn1
<=&aMem
[(p
->nMem
+1 - p
->nCursor
)] );
1466 assert( memIsValid(pIn1
) );
1467 memAboutToChange(p
, pOut
);
1468 sqlite3VdbeMemMove(pOut
, pIn1
);
1470 pIn1
->pScopyFrom
= 0;
1472 for(i
=1; i
<p
->nMem
; i
++){
1473 if( aMem
[i
].pScopyFrom
==pIn1
){
1474 aMem
[i
].pScopyFrom
= pOut
;
1479 Deephemeralize(pOut
);
1480 REGISTER_TRACE(p2
++, pOut
);
1487 /* Opcode: Copy P1 P2 P3 * P5
1488 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
1490 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
1492 ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
1493 ** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
1494 ** be merged. The 0x0001 bit is used by the query planner and does not
1495 ** come into play during query execution.
1497 ** This instruction makes a deep copy of the value. A duplicate
1498 ** is made of any string or blob constant. See also OP_SCopy.
1504 pIn1
= &aMem
[pOp
->p1
];
1505 pOut
= &aMem
[pOp
->p2
];
1506 assert( pOut
!=pIn1
);
1508 memAboutToChange(p
, pOut
);
1509 sqlite3VdbeMemShallowCopy(pOut
, pIn1
, MEM_Ephem
);
1510 Deephemeralize(pOut
);
1511 if( (pOut
->flags
& MEM_Subtype
)!=0 && (pOp
->p5
& 0x0002)!=0 ){
1512 pOut
->flags
&= ~MEM_Subtype
;
1515 pOut
->pScopyFrom
= 0;
1517 REGISTER_TRACE(pOp
->p2
+pOp
->p3
-n
, pOut
);
1518 if( (n
--)==0 ) break;
1525 /* Opcode: SCopy P1 P2 * * *
1526 ** Synopsis: r[P2]=r[P1]
1528 ** Make a shallow copy of register P1 into register P2.
1530 ** This instruction makes a shallow copy of the value. If the value
1531 ** is a string or blob, then the copy is only a pointer to the
1532 ** original and hence if the original changes so will the copy.
1533 ** Worse, if the original is deallocated, the copy becomes invalid.
1534 ** Thus the program must guarantee that the original will not change
1535 ** during the lifetime of the copy. Use OP_Copy to make a complete
1538 case OP_SCopy
: { /* out2 */
1539 pIn1
= &aMem
[pOp
->p1
];
1540 pOut
= &aMem
[pOp
->p2
];
1541 assert( pOut
!=pIn1
);
1542 sqlite3VdbeMemShallowCopy(pOut
, pIn1
, MEM_Ephem
);
1544 pOut
->pScopyFrom
= pIn1
;
1545 pOut
->mScopyFlags
= pIn1
->flags
;
1550 /* Opcode: IntCopy P1 P2 * * *
1551 ** Synopsis: r[P2]=r[P1]
1553 ** Transfer the integer value held in register P1 into register P2.
1555 ** This is an optimized version of SCopy that works only for integer
1558 case OP_IntCopy
: { /* out2 */
1559 pIn1
= &aMem
[pOp
->p1
];
1560 assert( (pIn1
->flags
& MEM_Int
)!=0 );
1561 pOut
= &aMem
[pOp
->p2
];
1562 sqlite3VdbeMemSetInt64(pOut
, pIn1
->u
.i
);
1566 /* Opcode: FkCheck * * * * *
1568 ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
1569 ** foreign key constraint violations. If there are no foreign key
1570 ** constraint violations, this is a no-op.
1572 ** FK constraint violations are also checked when the prepared statement
1573 ** exits. This opcode is used to raise foreign key constraint errors prior
1574 ** to returning results such as a row change count or the result of a
1575 ** RETURNING clause.
1578 if( (rc
= sqlite3VdbeCheckFk(p
,0))!=SQLITE_OK
){
1579 goto abort_due_to_error
;
1584 /* Opcode: ResultRow P1 P2 * * *
1585 ** Synopsis: output=r[P1@P2]
1587 ** The registers P1 through P1+P2-1 contain a single row of
1588 ** results. This opcode causes the sqlite3_step() call to terminate
1589 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1590 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
1593 case OP_ResultRow
: {
1594 assert( p
->nResColumn
==pOp
->p2
);
1595 assert( pOp
->p1
>0 || CORRUPT_DB
);
1596 assert( pOp
->p1
+pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
)+1 );
1598 p
->cacheCtr
= (p
->cacheCtr
+ 2)|1;
1599 p
->pResultRow
= &aMem
[pOp
->p1
];
1602 Mem
*pMem
= p
->pResultRow
;
1604 for(i
=0; i
<pOp
->p2
; i
++){
1605 assert( memIsValid(&pMem
[i
]) );
1606 REGISTER_TRACE(pOp
->p1
+i
, &pMem
[i
]);
1607 /* The registers in the result will not be used again when the
1608 ** prepared statement restarts. This is because sqlite3_column()
1609 ** APIs might have caused type conversions of made other changes to
1610 ** the register values. Therefore, we can go ahead and break any
1611 ** OP_SCopy dependencies. */
1612 pMem
[i
].pScopyFrom
= 0;
1616 if( db
->mallocFailed
) goto no_mem
;
1617 if( db
->mTrace
& SQLITE_TRACE_ROW
){
1618 db
->trace
.xV2(SQLITE_TRACE_ROW
, db
->pTraceArg
, p
, 0);
1620 p
->pc
= (int)(pOp
- aOp
) + 1;
1625 /* Opcode: Concat P1 P2 P3 * *
1626 ** Synopsis: r[P3]=r[P2]+r[P1]
1628 ** Add the text in register P1 onto the end of the text in
1629 ** register P2 and store the result in register P3.
1630 ** If either the P1 or P2 text are NULL then store NULL in P3.
1634 ** It is illegal for P1 and P3 to be the same register. Sometimes,
1635 ** if P3 is the same register as P2, the implementation is able
1636 ** to avoid a memcpy().
1638 case OP_Concat
: { /* same as TK_CONCAT, in1, in2, out3 */
1639 i64 nByte
; /* Total size of the output string or blob */
1640 u16 flags1
; /* Initial flags for P1 */
1641 u16 flags2
; /* Initial flags for P2 */
1643 pIn1
= &aMem
[pOp
->p1
];
1644 pIn2
= &aMem
[pOp
->p2
];
1645 pOut
= &aMem
[pOp
->p3
];
1646 testcase( pOut
==pIn2
);
1647 assert( pIn1
!=pOut
);
1648 flags1
= pIn1
->flags
;
1649 testcase( flags1
& MEM_Null
);
1650 testcase( pIn2
->flags
& MEM_Null
);
1651 if( (flags1
| pIn2
->flags
) & MEM_Null
){
1652 sqlite3VdbeMemSetNull(pOut
);
1655 if( (flags1
& (MEM_Str
|MEM_Blob
))==0 ){
1656 if( sqlite3VdbeMemStringify(pIn1
,encoding
,0) ) goto no_mem
;
1657 flags1
= pIn1
->flags
& ~MEM_Str
;
1658 }else if( (flags1
& MEM_Zero
)!=0 ){
1659 if( sqlite3VdbeMemExpandBlob(pIn1
) ) goto no_mem
;
1660 flags1
= pIn1
->flags
& ~MEM_Str
;
1662 flags2
= pIn2
->flags
;
1663 if( (flags2
& (MEM_Str
|MEM_Blob
))==0 ){
1664 if( sqlite3VdbeMemStringify(pIn2
,encoding
,0) ) goto no_mem
;
1665 flags2
= pIn2
->flags
& ~MEM_Str
;
1666 }else if( (flags2
& MEM_Zero
)!=0 ){
1667 if( sqlite3VdbeMemExpandBlob(pIn2
) ) goto no_mem
;
1668 flags2
= pIn2
->flags
& ~MEM_Str
;
1670 nByte
= pIn1
->n
+ pIn2
->n
;
1671 if( nByte
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1674 if( sqlite3VdbeMemGrow(pOut
, (int)nByte
+2, pOut
==pIn2
) ){
1677 MemSetTypeFlag(pOut
, MEM_Str
);
1679 memcpy(pOut
->z
, pIn2
->z
, pIn2
->n
);
1680 assert( (pIn2
->flags
& MEM_Dyn
) == (flags2
& MEM_Dyn
) );
1681 pIn2
->flags
= flags2
;
1683 memcpy(&pOut
->z
[pIn2
->n
], pIn1
->z
, pIn1
->n
);
1684 assert( (pIn1
->flags
& MEM_Dyn
) == (flags1
& MEM_Dyn
) );
1685 pIn1
->flags
= flags1
;
1686 if( encoding
>SQLITE_UTF8
) nByte
&= ~1;
1688 pOut
->z
[nByte
+1] = 0;
1689 pOut
->flags
|= MEM_Term
;
1690 pOut
->n
= (int)nByte
;
1691 pOut
->enc
= encoding
;
1692 UPDATE_MAX_BLOBSIZE(pOut
);
1696 /* Opcode: Add P1 P2 P3 * *
1697 ** Synopsis: r[P3]=r[P1]+r[P2]
1699 ** Add the value in register P1 to the value in register P2
1700 ** and store the result in register P3.
1701 ** If either input is NULL, the result is NULL.
1703 /* Opcode: Multiply P1 P2 P3 * *
1704 ** Synopsis: r[P3]=r[P1]*r[P2]
1707 ** Multiply the value in register P1 by the value in register P2
1708 ** and store the result in register P3.
1709 ** If either input is NULL, the result is NULL.
1711 /* Opcode: Subtract P1 P2 P3 * *
1712 ** Synopsis: r[P3]=r[P2]-r[P1]
1714 ** Subtract the value in register P1 from the value in register P2
1715 ** and store the result in register P3.
1716 ** If either input is NULL, the result is NULL.
1718 /* Opcode: Divide P1 P2 P3 * *
1719 ** Synopsis: r[P3]=r[P2]/r[P1]
1721 ** Divide the value in register P1 by the value in register P2
1722 ** and store the result in register P3 (P3=P2/P1). If the value in
1723 ** register P1 is zero, then the result is NULL. If either input is
1724 ** NULL, the result is NULL.
1726 /* Opcode: Remainder P1 P2 P3 * *
1727 ** Synopsis: r[P3]=r[P2]%r[P1]
1729 ** Compute the remainder after integer register P2 is divided by
1730 ** register P1 and store the result in register P3.
1731 ** If the value in register P1 is zero the result is NULL.
1732 ** If either operand is NULL, the result is NULL.
1734 case OP_Add
: /* same as TK_PLUS, in1, in2, out3 */
1735 case OP_Subtract
: /* same as TK_MINUS, in1, in2, out3 */
1736 case OP_Multiply
: /* same as TK_STAR, in1, in2, out3 */
1737 case OP_Divide
: /* same as TK_SLASH, in1, in2, out3 */
1738 case OP_Remainder
: { /* same as TK_REM, in1, in2, out3 */
1739 u16 type1
; /* Numeric type of left operand */
1740 u16 type2
; /* Numeric type of right operand */
1741 i64 iA
; /* Integer value of left operand */
1742 i64 iB
; /* Integer value of right operand */
1743 double rA
; /* Real value of left operand */
1744 double rB
; /* Real value of right operand */
1746 pIn1
= &aMem
[pOp
->p1
];
1747 type1
= pIn1
->flags
;
1748 pIn2
= &aMem
[pOp
->p2
];
1749 type2
= pIn2
->flags
;
1750 pOut
= &aMem
[pOp
->p3
];
1751 if( (type1
& type2
& MEM_Int
)!=0 ){
1755 switch( pOp
->opcode
){
1756 case OP_Add
: if( sqlite3AddInt64(&iB
,iA
) ) goto fp_math
; break;
1757 case OP_Subtract
: if( sqlite3SubInt64(&iB
,iA
) ) goto fp_math
; break;
1758 case OP_Multiply
: if( sqlite3MulInt64(&iB
,iA
) ) goto fp_math
; break;
1760 if( iA
==0 ) goto arithmetic_result_is_null
;
1761 if( iA
==-1 && iB
==SMALLEST_INT64
) goto fp_math
;
1766 if( iA
==0 ) goto arithmetic_result_is_null
;
1767 if( iA
==-1 ) iA
= 1;
1773 MemSetTypeFlag(pOut
, MEM_Int
);
1774 }else if( ((type1
| type2
) & MEM_Null
)!=0 ){
1775 goto arithmetic_result_is_null
;
1777 type1
= numericType(pIn1
);
1778 type2
= numericType(pIn2
);
1779 if( (type1
& type2
& MEM_Int
)!=0 ) goto int_math
;
1781 rA
= sqlite3VdbeRealValue(pIn1
);
1782 rB
= sqlite3VdbeRealValue(pIn2
);
1783 switch( pOp
->opcode
){
1784 case OP_Add
: rB
+= rA
; break;
1785 case OP_Subtract
: rB
-= rA
; break;
1786 case OP_Multiply
: rB
*= rA
; break;
1788 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
1789 if( rA
==(double)0 ) goto arithmetic_result_is_null
;
1794 iA
= sqlite3VdbeIntValue(pIn1
);
1795 iB
= sqlite3VdbeIntValue(pIn2
);
1796 if( iA
==0 ) goto arithmetic_result_is_null
;
1797 if( iA
==-1 ) iA
= 1;
1798 rB
= (double)(iB
% iA
);
1802 #ifdef SQLITE_OMIT_FLOATING_POINT
1804 MemSetTypeFlag(pOut
, MEM_Int
);
1806 if( sqlite3IsNaN(rB
) ){
1807 goto arithmetic_result_is_null
;
1810 MemSetTypeFlag(pOut
, MEM_Real
);
1815 arithmetic_result_is_null
:
1816 sqlite3VdbeMemSetNull(pOut
);
1820 /* Opcode: CollSeq P1 * * P4
1822 ** P4 is a pointer to a CollSeq object. If the next call to a user function
1823 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1824 ** be returned. This is used by the built-in min(), max() and nullif()
1827 ** If P1 is not zero, then it is a register that a subsequent min() or
1828 ** max() aggregate will set to 1 if the current row is not the minimum or
1829 ** maximum. The P1 register is initialized to 0 by this instruction.
1831 ** The interface used by the implementation of the aforementioned functions
1832 ** to retrieve the collation sequence set by this opcode is not available
1833 ** publicly. Only built-in functions have access to this feature.
1836 assert( pOp
->p4type
==P4_COLLSEQ
);
1838 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p1
], 0);
1843 /* Opcode: BitAnd P1 P2 P3 * *
1844 ** Synopsis: r[P3]=r[P1]&r[P2]
1846 ** Take the bit-wise AND of the values in register P1 and P2 and
1847 ** store the result in register P3.
1848 ** If either input is NULL, the result is NULL.
1850 /* Opcode: BitOr P1 P2 P3 * *
1851 ** Synopsis: r[P3]=r[P1]|r[P2]
1853 ** Take the bit-wise OR of the values in register P1 and P2 and
1854 ** store the result in register P3.
1855 ** If either input is NULL, the result is NULL.
1857 /* Opcode: ShiftLeft P1 P2 P3 * *
1858 ** Synopsis: r[P3]=r[P2]<<r[P1]
1860 ** Shift the integer value in register P2 to the left by the
1861 ** number of bits specified by the integer in register P1.
1862 ** Store the result in register P3.
1863 ** If either input is NULL, the result is NULL.
1865 /* Opcode: ShiftRight P1 P2 P3 * *
1866 ** Synopsis: r[P3]=r[P2]>>r[P1]
1868 ** Shift the integer value in register P2 to the right by the
1869 ** number of bits specified by the integer in register P1.
1870 ** Store the result in register P3.
1871 ** If either input is NULL, the result is NULL.
1873 case OP_BitAnd
: /* same as TK_BITAND, in1, in2, out3 */
1874 case OP_BitOr
: /* same as TK_BITOR, in1, in2, out3 */
1875 case OP_ShiftLeft
: /* same as TK_LSHIFT, in1, in2, out3 */
1876 case OP_ShiftRight
: { /* same as TK_RSHIFT, in1, in2, out3 */
1882 pIn1
= &aMem
[pOp
->p1
];
1883 pIn2
= &aMem
[pOp
->p2
];
1884 pOut
= &aMem
[pOp
->p3
];
1885 if( (pIn1
->flags
| pIn2
->flags
) & MEM_Null
){
1886 sqlite3VdbeMemSetNull(pOut
);
1889 iA
= sqlite3VdbeIntValue(pIn2
);
1890 iB
= sqlite3VdbeIntValue(pIn1
);
1892 if( op
==OP_BitAnd
){
1894 }else if( op
==OP_BitOr
){
1897 assert( op
==OP_ShiftRight
|| op
==OP_ShiftLeft
);
1899 /* If shifting by a negative amount, shift in the other direction */
1901 assert( OP_ShiftRight
==OP_ShiftLeft
+1 );
1902 op
= 2*OP_ShiftLeft
+ 1 - op
;
1903 iB
= iB
>(-64) ? -iB
: 64;
1907 iA
= (iA
>=0 || op
==OP_ShiftLeft
) ? 0 : -1;
1909 memcpy(&uA
, &iA
, sizeof(uA
));
1910 if( op
==OP_ShiftLeft
){
1914 /* Sign-extend on a right shift of a negative number */
1915 if( iA
<0 ) uA
|= ((((u64
)0xffffffff)<<32)|0xffffffff) << (64-iB
);
1917 memcpy(&iA
, &uA
, sizeof(iA
));
1921 MemSetTypeFlag(pOut
, MEM_Int
);
1925 /* Opcode: AddImm P1 P2 * * *
1926 ** Synopsis: r[P1]=r[P1]+P2
1928 ** Add the constant P2 to the value in register P1.
1929 ** The result is always an integer.
1931 ** To force any register to be an integer, just add 0.
1933 case OP_AddImm
: { /* in1 */
1934 pIn1
= &aMem
[pOp
->p1
];
1935 memAboutToChange(p
, pIn1
);
1936 sqlite3VdbeMemIntegerify(pIn1
);
1937 pIn1
->u
.i
+= pOp
->p2
;
1941 /* Opcode: MustBeInt P1 P2 * * *
1943 ** Force the value in register P1 to be an integer. If the value
1944 ** in P1 is not an integer and cannot be converted into an integer
1945 ** without data loss, then jump immediately to P2, or if P2==0
1946 ** raise an SQLITE_MISMATCH exception.
1948 case OP_MustBeInt
: { /* jump, in1 */
1949 pIn1
= &aMem
[pOp
->p1
];
1950 if( (pIn1
->flags
& MEM_Int
)==0 ){
1951 applyAffinity(pIn1
, SQLITE_AFF_NUMERIC
, encoding
);
1952 if( (pIn1
->flags
& MEM_Int
)==0 ){
1953 VdbeBranchTaken(1, 2);
1955 rc
= SQLITE_MISMATCH
;
1956 goto abort_due_to_error
;
1962 VdbeBranchTaken(0, 2);
1963 MemSetTypeFlag(pIn1
, MEM_Int
);
1967 #ifndef SQLITE_OMIT_FLOATING_POINT
1968 /* Opcode: RealAffinity P1 * * * *
1970 ** If register P1 holds an integer convert it to a real value.
1972 ** This opcode is used when extracting information from a column that
1973 ** has REAL affinity. Such column values may still be stored as
1974 ** integers, for space efficiency, but after extraction we want them
1975 ** to have only a real value.
1977 case OP_RealAffinity
: { /* in1 */
1978 pIn1
= &aMem
[pOp
->p1
];
1979 if( pIn1
->flags
& (MEM_Int
|MEM_IntReal
) ){
1980 testcase( pIn1
->flags
& MEM_Int
);
1981 testcase( pIn1
->flags
& MEM_IntReal
);
1982 sqlite3VdbeMemRealify(pIn1
);
1983 REGISTER_TRACE(pOp
->p1
, pIn1
);
1989 #ifndef SQLITE_OMIT_CAST
1990 /* Opcode: Cast P1 P2 * * *
1991 ** Synopsis: affinity(r[P1])
1993 ** Force the value in register P1 to be the type defined by P2.
1996 ** <li> P2=='A' → BLOB
1997 ** <li> P2=='B' → TEXT
1998 ** <li> P2=='C' → NUMERIC
1999 ** <li> P2=='D' → INTEGER
2000 ** <li> P2=='E' → REAL
2003 ** A NULL value is not changed by this routine. It remains NULL.
2005 case OP_Cast
: { /* in1 */
2006 assert( pOp
->p2
>=SQLITE_AFF_BLOB
&& pOp
->p2
<=SQLITE_AFF_REAL
);
2007 testcase( pOp
->p2
==SQLITE_AFF_TEXT
);
2008 testcase( pOp
->p2
==SQLITE_AFF_BLOB
);
2009 testcase( pOp
->p2
==SQLITE_AFF_NUMERIC
);
2010 testcase( pOp
->p2
==SQLITE_AFF_INTEGER
);
2011 testcase( pOp
->p2
==SQLITE_AFF_REAL
);
2012 pIn1
= &aMem
[pOp
->p1
];
2013 memAboutToChange(p
, pIn1
);
2014 rc
= ExpandBlob(pIn1
);
2015 if( rc
) goto abort_due_to_error
;
2016 rc
= sqlite3VdbeMemCast(pIn1
, pOp
->p2
, encoding
);
2017 if( rc
) goto abort_due_to_error
;
2018 UPDATE_MAX_BLOBSIZE(pIn1
);
2019 REGISTER_TRACE(pOp
->p1
, pIn1
);
2022 #endif /* SQLITE_OMIT_CAST */
2024 /* Opcode: Eq P1 P2 P3 P4 P5
2025 ** Synopsis: IF r[P3]==r[P1]
2027 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
2028 ** jump to address P2.
2030 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
2031 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
2032 ** to coerce both inputs according to this affinity before the
2033 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2034 ** affinity is used. Note that the affinity conversions are stored
2035 ** back into the input registers P1 and P3. So this opcode can cause
2036 ** persistent changes to registers P1 and P3.
2038 ** Once any conversions have taken place, and neither value is NULL,
2039 ** the values are compared. If both values are blobs then memcmp() is
2040 ** used to determine the results of the comparison. If both values
2041 ** are text, then the appropriate collating function specified in
2042 ** P4 is used to do the comparison. If P4 is not specified then
2043 ** memcmp() is used to compare text string. If both values are
2044 ** numeric, then a numeric comparison is used. If the two values
2045 ** are of different types, then numbers are considered less than
2046 ** strings and strings are considered less than blobs.
2048 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
2049 ** true or false and is never NULL. If both operands are NULL then the result
2050 ** of comparison is true. If either operand is NULL then the result is false.
2051 ** If neither operand is NULL the result is the same as it would be if
2052 ** the SQLITE_NULLEQ flag were omitted from P5.
2054 ** This opcode saves the result of comparison for use by the new
2057 /* Opcode: Ne P1 P2 P3 P4 P5
2058 ** Synopsis: IF r[P3]!=r[P1]
2060 ** This works just like the Eq opcode except that the jump is taken if
2061 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for
2062 ** additional information.
2064 /* Opcode: Lt P1 P2 P3 P4 P5
2065 ** Synopsis: IF r[P3]<r[P1]
2067 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
2068 ** jump to address P2.
2070 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
2071 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
2072 ** bit is clear then fall through if either operand is NULL.
2074 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
2075 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
2076 ** to coerce both inputs according to this affinity before the
2077 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2078 ** affinity is used. Note that the affinity conversions are stored
2079 ** back into the input registers P1 and P3. So this opcode can cause
2080 ** persistent changes to registers P1 and P3.
2082 ** Once any conversions have taken place, and neither value is NULL,
2083 ** the values are compared. If both values are blobs then memcmp() is
2084 ** used to determine the results of the comparison. If both values
2085 ** are text, then the appropriate collating function specified in
2086 ** P4 is used to do the comparison. If P4 is not specified then
2087 ** memcmp() is used to compare text string. If both values are
2088 ** numeric, then a numeric comparison is used. If the two values
2089 ** are of different types, then numbers are considered less than
2090 ** strings and strings are considered less than blobs.
2092 ** This opcode saves the result of comparison for use by the new
2095 /* Opcode: Le P1 P2 P3 P4 P5
2096 ** Synopsis: IF r[P3]<=r[P1]
2098 ** This works just like the Lt opcode except that the jump is taken if
2099 ** the content of register P3 is less than or equal to the content of
2100 ** register P1. See the Lt opcode for additional information.
2102 /* Opcode: Gt P1 P2 P3 P4 P5
2103 ** Synopsis: IF r[P3]>r[P1]
2105 ** This works just like the Lt opcode except that the jump is taken if
2106 ** the content of register P3 is greater than the content of
2107 ** register P1. See the Lt opcode for additional information.
2109 /* Opcode: Ge P1 P2 P3 P4 P5
2110 ** Synopsis: IF r[P3]>=r[P1]
2112 ** This works just like the Lt opcode except that the jump is taken if
2113 ** the content of register P3 is greater than or equal to the content of
2114 ** register P1. See the Lt opcode for additional information.
2116 case OP_Eq
: /* same as TK_EQ, jump, in1, in3 */
2117 case OP_Ne
: /* same as TK_NE, jump, in1, in3 */
2118 case OP_Lt
: /* same as TK_LT, jump, in1, in3 */
2119 case OP_Le
: /* same as TK_LE, jump, in1, in3 */
2120 case OP_Gt
: /* same as TK_GT, jump, in1, in3 */
2121 case OP_Ge
: { /* same as TK_GE, jump, in1, in3 */
2122 int res
, res2
; /* Result of the comparison of pIn1 against pIn3 */
2123 char affinity
; /* Affinity to use for comparison */
2124 u16 flags1
; /* Copy of initial value of pIn1->flags */
2125 u16 flags3
; /* Copy of initial value of pIn3->flags */
2127 pIn1
= &aMem
[pOp
->p1
];
2128 pIn3
= &aMem
[pOp
->p3
];
2129 flags1
= pIn1
->flags
;
2130 flags3
= pIn3
->flags
;
2131 if( (flags1
& flags3
& MEM_Int
)!=0 ){
2132 /* Common case of comparison of two integers */
2133 if( pIn3
->u
.i
> pIn1
->u
.i
){
2134 if( sqlite3aGTb
[pOp
->opcode
] ){
2135 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2139 VVA_ONLY( iCompareIsInit
= 1; )
2140 }else if( pIn3
->u
.i
< pIn1
->u
.i
){
2141 if( sqlite3aLTb
[pOp
->opcode
] ){
2142 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2146 VVA_ONLY( iCompareIsInit
= 1; )
2148 if( sqlite3aEQb
[pOp
->opcode
] ){
2149 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2153 VVA_ONLY( iCompareIsInit
= 1; )
2155 VdbeBranchTaken(0, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2158 if( (flags1
| flags3
)&MEM_Null
){
2159 /* One or both operands are NULL */
2160 if( pOp
->p5
& SQLITE_NULLEQ
){
2161 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2162 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2163 ** or not both operands are null.
2165 assert( (flags1
& MEM_Cleared
)==0 );
2166 assert( (pOp
->p5
& SQLITE_JUMPIFNULL
)==0 || CORRUPT_DB
);
2167 testcase( (pOp
->p5
& SQLITE_JUMPIFNULL
)!=0 );
2168 if( (flags1
&flags3
&MEM_Null
)!=0
2169 && (flags3
&MEM_Cleared
)==0
2171 res
= 0; /* Operands are equal */
2173 res
= ((flags3
& MEM_Null
) ? -1 : +1); /* Operands are not equal */
2176 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2177 ** then the result is always NULL.
2178 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2180 VdbeBranchTaken(2,3);
2181 if( pOp
->p5
& SQLITE_JUMPIFNULL
){
2184 iCompare
= 1; /* Operands are not equal */
2185 VVA_ONLY( iCompareIsInit
= 1; )
2189 /* Neither operand is NULL and we couldn't do the special high-speed
2190 ** integer comparison case. So do a general-case comparison. */
2191 affinity
= pOp
->p5
& SQLITE_AFF_MASK
;
2192 if( affinity
>=SQLITE_AFF_NUMERIC
){
2193 if( (flags1
| flags3
)&MEM_Str
){
2194 if( (flags1
& (MEM_Int
|MEM_IntReal
|MEM_Real
|MEM_Str
))==MEM_Str
){
2195 applyNumericAffinity(pIn1
,0);
2196 assert( flags3
==pIn3
->flags
|| CORRUPT_DB
);
2197 flags3
= pIn3
->flags
;
2199 if( (flags3
& (MEM_Int
|MEM_IntReal
|MEM_Real
|MEM_Str
))==MEM_Str
){
2200 applyNumericAffinity(pIn3
,0);
2203 }else if( affinity
==SQLITE_AFF_TEXT
&& ((flags1
| flags3
) & MEM_Str
)!=0 ){
2204 if( (flags1
& MEM_Str
)==0 && (flags1
&(MEM_Int
|MEM_Real
|MEM_IntReal
))!=0 ){
2205 testcase( pIn1
->flags
& MEM_Int
);
2206 testcase( pIn1
->flags
& MEM_Real
);
2207 testcase( pIn1
->flags
& MEM_IntReal
);
2208 sqlite3VdbeMemStringify(pIn1
, encoding
, 1);
2209 testcase( (flags1
&MEM_Dyn
) != (pIn1
->flags
&MEM_Dyn
) );
2210 flags1
= (pIn1
->flags
& ~MEM_TypeMask
) | (flags1
& MEM_TypeMask
);
2211 if( NEVER(pIn1
==pIn3
) ) flags3
= flags1
| MEM_Str
;
2213 if( (flags3
& MEM_Str
)==0 && (flags3
&(MEM_Int
|MEM_Real
|MEM_IntReal
))!=0 ){
2214 testcase( pIn3
->flags
& MEM_Int
);
2215 testcase( pIn3
->flags
& MEM_Real
);
2216 testcase( pIn3
->flags
& MEM_IntReal
);
2217 sqlite3VdbeMemStringify(pIn3
, encoding
, 1);
2218 testcase( (flags3
&MEM_Dyn
) != (pIn3
->flags
&MEM_Dyn
) );
2219 flags3
= (pIn3
->flags
& ~MEM_TypeMask
) | (flags3
& MEM_TypeMask
);
2222 assert( pOp
->p4type
==P4_COLLSEQ
|| pOp
->p4
.pColl
==0 );
2223 res
= sqlite3MemCompare(pIn3
, pIn1
, pOp
->p4
.pColl
);
2226 /* At this point, res is negative, zero, or positive if reg[P1] is
2227 ** less than, equal to, or greater than reg[P3], respectively. Compute
2228 ** the answer to this operator in res2, depending on what the comparison
2229 ** operator actually is. The next block of code depends on the fact
2230 ** that the 6 comparison operators are consecutive integers in this
2231 ** order: NE, EQ, GT, LE, LT, GE */
2232 assert( OP_Eq
==OP_Ne
+1 ); assert( OP_Gt
==OP_Ne
+2 ); assert( OP_Le
==OP_Ne
+3 );
2233 assert( OP_Lt
==OP_Ne
+4 ); assert( OP_Ge
==OP_Ne
+5 );
2235 res2
= sqlite3aLTb
[pOp
->opcode
];
2237 res2
= sqlite3aEQb
[pOp
->opcode
];
2239 res2
= sqlite3aGTb
[pOp
->opcode
];
2242 VVA_ONLY( iCompareIsInit
= 1; )
2244 /* Undo any changes made by applyAffinity() to the input registers. */
2245 assert( (pIn3
->flags
& MEM_Dyn
) == (flags3
& MEM_Dyn
) );
2246 pIn3
->flags
= flags3
;
2247 assert( (pIn1
->flags
& MEM_Dyn
) == (flags1
& MEM_Dyn
) );
2248 pIn1
->flags
= flags1
;
2250 VdbeBranchTaken(res2
!=0, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2257 /* Opcode: ElseEq * P2 * * *
2259 ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2260 ** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2261 ** opcodes are allowed to occur between this instruction and the previous
2264 ** If result of an OP_Eq comparison on the same two operands as the
2265 ** prior OP_Lt or OP_Gt would have been true, then jump to P2.
2266 ** If the result of an OP_Eq comparison on the two previous
2267 ** operands would have been false or NULL, then fall through.
2269 case OP_ElseEq
: { /* same as TK_ESCAPE, jump */
2272 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
2273 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
2275 for(iAddr
= (int)(pOp
- aOp
) - 1; ALWAYS(iAddr
>=0); iAddr
--){
2276 if( aOp
[iAddr
].opcode
==OP_ReleaseReg
) continue;
2277 assert( aOp
[iAddr
].opcode
==OP_Lt
|| aOp
[iAddr
].opcode
==OP_Gt
);
2280 #endif /* SQLITE_DEBUG */
2281 assert( iCompareIsInit
);
2282 VdbeBranchTaken(iCompare
==0, 2);
2283 if( iCompare
==0 ) goto jump_to_p2
;
2288 /* Opcode: Permutation * * * P4 *
2290 ** Set the permutation used by the OP_Compare operator in the next
2291 ** instruction. The permutation is stored in the P4 operand.
2293 ** The permutation is only valid for the next opcode which must be
2294 ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
2296 ** The first integer in the P4 integer array is the length of the array
2297 ** and does not become part of the permutation.
2299 case OP_Permutation
: {
2300 assert( pOp
->p4type
==P4_INTARRAY
);
2301 assert( pOp
->p4
.ai
);
2302 assert( pOp
[1].opcode
==OP_Compare
);
2303 assert( pOp
[1].p5
& OPFLAG_PERMUTE
);
2307 /* Opcode: Compare P1 P2 P3 P4 P5
2308 ** Synopsis: r[P1@P3] <-> r[P2@P3]
2310 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2311 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
2312 ** the comparison for use by the next OP_Jump instruct.
2314 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2315 ** determined by the most recent OP_Permutation operator. If the
2316 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2319 ** P4 is a KeyInfo structure that defines collating sequences and sort
2320 ** orders for the comparison. The permutation applies to registers
2321 ** only. The KeyInfo elements are used sequentially.
2323 ** The comparison is a sort comparison, so NULLs compare equal,
2324 ** NULLs are less than numbers, numbers are less than strings,
2325 ** and strings are less than blobs.
2327 ** This opcode must be immediately followed by an OP_Jump opcode.
2334 const KeyInfo
*pKeyInfo
;
2336 CollSeq
*pColl
; /* Collating sequence to use on this term */
2337 int bRev
; /* True for DESCENDING sort order */
2338 u32
*aPermute
; /* The permutation */
2340 if( (pOp
->p5
& OPFLAG_PERMUTE
)==0 ){
2344 assert( pOp
[-1].opcode
==OP_Permutation
);
2345 assert( pOp
[-1].p4type
==P4_INTARRAY
);
2346 aPermute
= pOp
[-1].p4
.ai
+ 1;
2347 assert( aPermute
!=0 );
2350 pKeyInfo
= pOp
->p4
.pKeyInfo
;
2352 assert( pKeyInfo
!=0 );
2358 for(k
=0; k
<n
; k
++) if( aPermute
[k
]>(u32
)mx
) mx
= aPermute
[k
];
2359 assert( p1
>0 && p1
+mx
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2360 assert( p2
>0 && p2
+mx
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2362 assert( p1
>0 && p1
+n
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2363 assert( p2
>0 && p2
+n
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2365 #endif /* SQLITE_DEBUG */
2367 idx
= aPermute
? aPermute
[i
] : (u32
)i
;
2368 assert( memIsValid(&aMem
[p1
+idx
]) );
2369 assert( memIsValid(&aMem
[p2
+idx
]) );
2370 REGISTER_TRACE(p1
+idx
, &aMem
[p1
+idx
]);
2371 REGISTER_TRACE(p2
+idx
, &aMem
[p2
+idx
]);
2372 assert( i
<pKeyInfo
->nKeyField
);
2373 pColl
= pKeyInfo
->aColl
[i
];
2374 bRev
= (pKeyInfo
->aSortFlags
[i
] & KEYINFO_ORDER_DESC
);
2375 iCompare
= sqlite3MemCompare(&aMem
[p1
+idx
], &aMem
[p2
+idx
], pColl
);
2376 VVA_ONLY( iCompareIsInit
= 1; )
2378 if( (pKeyInfo
->aSortFlags
[i
] & KEYINFO_ORDER_BIGNULL
)
2379 && ((aMem
[p1
+idx
].flags
& MEM_Null
) || (aMem
[p2
+idx
].flags
& MEM_Null
))
2381 iCompare
= -iCompare
;
2383 if( bRev
) iCompare
= -iCompare
;
2387 assert( pOp
[1].opcode
==OP_Jump
);
2391 /* Opcode: Jump P1 P2 P3 * *
2393 ** Jump to the instruction at address P1, P2, or P3 depending on whether
2394 ** in the most recent OP_Compare instruction the P1 vector was less than
2395 ** equal to, or greater than the P2 vector, respectively.
2397 ** This opcode must immediately follow an OP_Compare opcode.
2399 case OP_Jump
: { /* jump */
2400 assert( pOp
>aOp
&& pOp
[-1].opcode
==OP_Compare
);
2401 assert( iCompareIsInit
);
2403 VdbeBranchTaken(0,4); pOp
= &aOp
[pOp
->p1
- 1];
2404 }else if( iCompare
==0 ){
2405 VdbeBranchTaken(1,4); pOp
= &aOp
[pOp
->p2
- 1];
2407 VdbeBranchTaken(2,4); pOp
= &aOp
[pOp
->p3
- 1];
2412 /* Opcode: And P1 P2 P3 * *
2413 ** Synopsis: r[P3]=(r[P1] && r[P2])
2415 ** Take the logical AND of the values in registers P1 and P2 and
2416 ** write the result into register P3.
2418 ** If either P1 or P2 is 0 (false) then the result is 0 even if
2419 ** the other input is NULL. A NULL and true or two NULLs give
2422 /* Opcode: Or P1 P2 P3 * *
2423 ** Synopsis: r[P3]=(r[P1] || r[P2])
2425 ** Take the logical OR of the values in register P1 and P2 and
2426 ** store the answer in register P3.
2428 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2429 ** even if the other input is NULL. A NULL and false or two NULLs
2430 ** give a NULL output.
2432 case OP_And
: /* same as TK_AND, in1, in2, out3 */
2433 case OP_Or
: { /* same as TK_OR, in1, in2, out3 */
2434 int v1
; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2435 int v2
; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2437 v1
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], 2);
2438 v2
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p2
], 2);
2439 if( pOp
->opcode
==OP_And
){
2440 static const unsigned char and_logic
[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
2441 v1
= and_logic
[v1
*3+v2
];
2443 static const unsigned char or_logic
[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
2444 v1
= or_logic
[v1
*3+v2
];
2446 pOut
= &aMem
[pOp
->p3
];
2448 MemSetTypeFlag(pOut
, MEM_Null
);
2451 MemSetTypeFlag(pOut
, MEM_Int
);
2456 /* Opcode: IsTrue P1 P2 P3 P4 *
2457 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2459 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2460 ** IS NOT FALSE operators.
2462 ** Interpret the value in register P1 as a boolean value. Store that
2463 ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2464 ** NULL, then the P3 is stored in register P2. Invert the answer if P4
2467 ** The logic is summarized like this:
2470 ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2471 ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2472 ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2473 ** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
2476 case OP_IsTrue
: { /* in1, out2 */
2477 assert( pOp
->p4type
==P4_INT32
);
2478 assert( pOp
->p4
.i
==0 || pOp
->p4
.i
==1 );
2479 assert( pOp
->p3
==0 || pOp
->p3
==1 );
2480 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p2
],
2481 sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], pOp
->p3
) ^ pOp
->p4
.i
);
2485 /* Opcode: Not P1 P2 * * *
2486 ** Synopsis: r[P2]= !r[P1]
2488 ** Interpret the value in register P1 as a boolean value. Store the
2489 ** boolean complement in register P2. If the value in register P1 is
2490 ** NULL, then a NULL is stored in P2.
2492 case OP_Not
: { /* same as TK_NOT, in1, out2 */
2493 pIn1
= &aMem
[pOp
->p1
];
2494 pOut
= &aMem
[pOp
->p2
];
2495 if( (pIn1
->flags
& MEM_Null
)==0 ){
2496 sqlite3VdbeMemSetInt64(pOut
, !sqlite3VdbeBooleanValue(pIn1
,0));
2498 sqlite3VdbeMemSetNull(pOut
);
2503 /* Opcode: BitNot P1 P2 * * *
2504 ** Synopsis: r[P2]= ~r[P1]
2506 ** Interpret the content of register P1 as an integer. Store the
2507 ** ones-complement of the P1 value into register P2. If P1 holds
2508 ** a NULL then store a NULL in P2.
2510 case OP_BitNot
: { /* same as TK_BITNOT, in1, out2 */
2511 pIn1
= &aMem
[pOp
->p1
];
2512 pOut
= &aMem
[pOp
->p2
];
2513 sqlite3VdbeMemSetNull(pOut
);
2514 if( (pIn1
->flags
& MEM_Null
)==0 ){
2515 pOut
->flags
= MEM_Int
;
2516 pOut
->u
.i
= ~sqlite3VdbeIntValue(pIn1
);
2521 /* Opcode: Once P1 P2 * * *
2523 ** Fall through to the next instruction the first time this opcode is
2524 ** encountered on each invocation of the byte-code program. Jump to P2
2525 ** on the second and all subsequent encounters during the same invocation.
2527 ** Top-level programs determine first invocation by comparing the P1
2528 ** operand against the P1 operand on the OP_Init opcode at the beginning
2529 ** of the program. If the P1 values differ, then fall through and make
2530 ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2531 ** the same then take the jump.
2533 ** For subprograms, there is a bitmask in the VdbeFrame that determines
2534 ** whether or not the jump should be taken. The bitmask is necessary
2535 ** because the self-altering code trick does not work for recursive
2538 case OP_Once
: { /* jump */
2539 u32 iAddr
; /* Address of this instruction */
2540 assert( p
->aOp
[0].opcode
==OP_Init
);
2542 iAddr
= (int)(pOp
- p
->aOp
);
2543 if( (p
->pFrame
->aOnce
[iAddr
/8] & (1<<(iAddr
& 7)))!=0 ){
2544 VdbeBranchTaken(1, 2);
2547 p
->pFrame
->aOnce
[iAddr
/8] |= 1<<(iAddr
& 7);
2549 if( p
->aOp
[0].p1
==pOp
->p1
){
2550 VdbeBranchTaken(1, 2);
2554 VdbeBranchTaken(0, 2);
2555 pOp
->p1
= p
->aOp
[0].p1
;
2559 /* Opcode: If P1 P2 P3 * *
2561 ** Jump to P2 if the value in register P1 is true. The value
2562 ** is considered true if it is numeric and non-zero. If the value
2563 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2565 case OP_If
: { /* jump, in1 */
2567 c
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], pOp
->p3
);
2568 VdbeBranchTaken(c
!=0, 2);
2569 if( c
) goto jump_to_p2
;
2573 /* Opcode: IfNot P1 P2 P3 * *
2575 ** Jump to P2 if the value in register P1 is False. The value
2576 ** is considered false if it has a numeric value of zero. If the value
2577 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2579 case OP_IfNot
: { /* jump, in1 */
2581 c
= !sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], !pOp
->p3
);
2582 VdbeBranchTaken(c
!=0, 2);
2583 if( c
) goto jump_to_p2
;
2587 /* Opcode: IsNull P1 P2 * * *
2588 ** Synopsis: if r[P1]==NULL goto P2
2590 ** Jump to P2 if the value in register P1 is NULL.
2592 case OP_IsNull
: { /* same as TK_ISNULL, jump, in1 */
2593 pIn1
= &aMem
[pOp
->p1
];
2594 VdbeBranchTaken( (pIn1
->flags
& MEM_Null
)!=0, 2);
2595 if( (pIn1
->flags
& MEM_Null
)!=0 ){
2601 /* Opcode: IsType P1 P2 P3 P4 P5
2602 ** Synopsis: if typeof(P1.P3) in P5 goto P2
2604 ** Jump to P2 if the type of a column in a btree is one of the types specified
2605 ** by the P5 bitmask.
2607 ** P1 is normally a cursor on a btree for which the row decode cache is
2608 ** valid through at least column P3. In other words, there should have been
2609 ** a prior OP_Column for column P3 or greater. If the cursor is not valid,
2610 ** then this opcode might give spurious results.
2611 ** The the btree row has fewer than P3 columns, then use P4 as the
2614 ** If P1 is -1, then P3 is a register number and the datatype is taken
2615 ** from the value in that register.
2617 ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
2618 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
2619 ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
2621 ** Take the jump to address P2 if and only if the datatype of the
2622 ** value determined by P1 and P3 corresponds to one of the bits in the
2626 case OP_IsType
: { /* jump */
2631 assert( pOp
->p1
>=(-1) && pOp
->p1
<p
->nCursor
);
2632 assert( pOp
->p1
>=0 || (pOp
->p3
>=0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
)) );
2634 pC
= p
->apCsr
[pOp
->p1
];
2636 assert( pOp
->p3
>=0 );
2637 if( pOp
->p3
<pC
->nHdrParsed
){
2638 serialType
= pC
->aType
[pOp
->p3
];
2639 if( serialType
>=12 ){
2641 typeMask
= 0x04; /* SQLITE_TEXT */
2643 typeMask
= 0x08; /* SQLITE_BLOB */
2646 static const unsigned char aMask
[] = {
2647 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
2648 0x01, 0x01, 0x10, 0x10
2650 testcase( serialType
==0 );
2651 testcase( serialType
==1 );
2652 testcase( serialType
==2 );
2653 testcase( serialType
==3 );
2654 testcase( serialType
==4 );
2655 testcase( serialType
==5 );
2656 testcase( serialType
==6 );
2657 testcase( serialType
==7 );
2658 testcase( serialType
==8 );
2659 testcase( serialType
==9 );
2660 testcase( serialType
==10 );
2661 testcase( serialType
==11 );
2662 typeMask
= aMask
[serialType
];
2665 typeMask
= 1 << (pOp
->p4
.i
- 1);
2666 testcase( typeMask
==0x01 );
2667 testcase( typeMask
==0x02 );
2668 testcase( typeMask
==0x04 );
2669 testcase( typeMask
==0x08 );
2670 testcase( typeMask
==0x10 );
2673 assert( memIsValid(&aMem
[pOp
->p3
]) );
2674 typeMask
= 1 << (sqlite3_value_type((sqlite3_value
*)&aMem
[pOp
->p3
])-1);
2675 testcase( typeMask
==0x01 );
2676 testcase( typeMask
==0x02 );
2677 testcase( typeMask
==0x04 );
2678 testcase( typeMask
==0x08 );
2679 testcase( typeMask
==0x10 );
2681 VdbeBranchTaken( (typeMask
& pOp
->p5
)!=0, 2);
2682 if( typeMask
& pOp
->p5
){
2688 /* Opcode: ZeroOrNull P1 P2 P3 * *
2689 ** Synopsis: r[P2] = 0 OR NULL
2691 ** If all both registers P1 and P3 are NOT NULL, then store a zero in
2692 ** register P2. If either registers P1 or P3 are NULL then put
2693 ** a NULL in register P2.
2695 case OP_ZeroOrNull
: { /* in1, in2, out2, in3 */
2696 if( (aMem
[pOp
->p1
].flags
& MEM_Null
)!=0
2697 || (aMem
[pOp
->p3
].flags
& MEM_Null
)!=0
2699 sqlite3VdbeMemSetNull(aMem
+ pOp
->p2
);
2701 sqlite3VdbeMemSetInt64(aMem
+ pOp
->p2
, 0);
2706 /* Opcode: NotNull P1 P2 * * *
2707 ** Synopsis: if r[P1]!=NULL goto P2
2709 ** Jump to P2 if the value in register P1 is not NULL.
2711 case OP_NotNull
: { /* same as TK_NOTNULL, jump, in1 */
2712 pIn1
= &aMem
[pOp
->p1
];
2713 VdbeBranchTaken( (pIn1
->flags
& MEM_Null
)==0, 2);
2714 if( (pIn1
->flags
& MEM_Null
)==0 ){
2720 /* Opcode: IfNullRow P1 P2 P3 * *
2721 ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2723 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
2724 ** If it is, then set register P3 to NULL and jump immediately to P2.
2725 ** If P1 is not on a NULL row, then fall through without making any
2728 ** If P1 is not an open cursor, then this opcode is a no-op.
2730 case OP_IfNullRow
: { /* jump */
2732 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2733 pC
= p
->apCsr
[pOp
->p1
];
2734 if( ALWAYS(pC
) && pC
->nullRow
){
2735 sqlite3VdbeMemSetNull(aMem
+ pOp
->p3
);
2741 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2742 /* Opcode: Offset P1 P2 P3 * *
2743 ** Synopsis: r[P3] = sqlite_offset(P1)
2745 ** Store in register r[P3] the byte offset into the database file that is the
2746 ** start of the payload for the record at which that cursor P1 is currently
2749 ** P2 is the column number for the argument to the sqlite_offset() function.
2750 ** This opcode does not use P2 itself, but the P2 value is used by the
2751 ** code generator. The P1, P2, and P3 operands to this opcode are the
2752 ** same as for OP_Column.
2754 ** This opcode is only available if SQLite is compiled with the
2755 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
2757 case OP_Offset
: { /* out3 */
2758 VdbeCursor
*pC
; /* The VDBE cursor */
2759 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2760 pC
= p
->apCsr
[pOp
->p1
];
2761 pOut
= &p
->aMem
[pOp
->p3
];
2762 if( pC
==0 || pC
->eCurType
!=CURTYPE_BTREE
){
2763 sqlite3VdbeMemSetNull(pOut
);
2765 if( pC
->deferredMoveto
){
2766 rc
= sqlite3VdbeFinishMoveto(pC
);
2767 if( rc
) goto abort_due_to_error
;
2769 if( sqlite3BtreeEof(pC
->uc
.pCursor
) ){
2770 sqlite3VdbeMemSetNull(pOut
);
2772 sqlite3VdbeMemSetInt64(pOut
, sqlite3BtreeOffset(pC
->uc
.pCursor
));
2777 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
2779 /* Opcode: Column P1 P2 P3 P4 P5
2780 ** Synopsis: r[P3]=PX cursor P1 column P2
2782 ** Interpret the data that cursor P1 points to as a structure built using
2783 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
2784 ** information about the format of the data.) Extract the P2-th column
2785 ** from this record. If there are less than (P2+1)
2786 ** values in the record, extract a NULL.
2788 ** The value extracted is stored in register P3.
2790 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
2791 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
2794 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
2795 ** to only be used by the length() function or the equivalent. The content
2796 ** of large blobs is not loaded, thus saving CPU cycles. If the
2797 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
2798 ** typeof() function or the IS NULL or IS NOT NULL operators or the
2799 ** equivalent. In this case, all content loading can be omitted.
2801 case OP_Column
: { /* ncycle */
2802 u32 p2
; /* column number to retrieve */
2803 VdbeCursor
*pC
; /* The VDBE cursor */
2804 BtCursor
*pCrsr
; /* The B-Tree cursor corresponding to pC */
2805 u32
*aOffset
; /* aOffset[i] is offset to start of data for i-th column */
2806 int len
; /* The length of the serialized data for the column */
2807 int i
; /* Loop counter */
2808 Mem
*pDest
; /* Where to write the extracted value */
2809 Mem sMem
; /* For storing the record being decoded */
2810 const u8
*zData
; /* Part of the record being decoded */
2811 const u8
*zHdr
; /* Next unparsed byte of the header */
2812 const u8
*zEndHdr
; /* Pointer to first byte after the header */
2813 u64 offset64
; /* 64-bit offset */
2814 u32 t
; /* A type code from the record header */
2815 Mem
*pReg
; /* PseudoTable input register */
2817 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2818 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
2819 pC
= p
->apCsr
[pOp
->p1
];
2824 assert( p2
<(u32
)pC
->nField
2825 || (pC
->eCurType
==CURTYPE_PSEUDO
&& pC
->seekResult
==0) );
2826 aOffset
= pC
->aOffset
;
2827 assert( aOffset
==pC
->aType
+pC
->nField
);
2828 assert( pC
->eCurType
!=CURTYPE_VTAB
);
2829 assert( pC
->eCurType
!=CURTYPE_PSEUDO
|| pC
->nullRow
);
2830 assert( pC
->eCurType
!=CURTYPE_SORTER
);
2832 if( pC
->cacheStatus
!=p
->cacheCtr
){ /*OPTIMIZATION-IF-FALSE*/
2834 if( pC
->eCurType
==CURTYPE_PSEUDO
&& pC
->seekResult
>0 ){
2835 /* For the special case of as pseudo-cursor, the seekResult field
2836 ** identifies the register that holds the record */
2837 pReg
= &aMem
[pC
->seekResult
];
2838 assert( pReg
->flags
& MEM_Blob
);
2839 assert( memIsValid(pReg
) );
2840 pC
->payloadSize
= pC
->szRow
= pReg
->n
;
2841 pC
->aRow
= (u8
*)pReg
->z
;
2843 pDest
= &aMem
[pOp
->p3
];
2844 memAboutToChange(p
, pDest
);
2845 sqlite3VdbeMemSetNull(pDest
);
2849 pCrsr
= pC
->uc
.pCursor
;
2850 if( pC
->deferredMoveto
){
2852 assert( !pC
->isEphemeral
);
2853 if( pC
->ub
.aAltMap
&& (iMap
= pC
->ub
.aAltMap
[1+p2
])>0 ){
2854 pC
= pC
->pAltCursor
;
2856 goto op_column_restart
;
2858 rc
= sqlite3VdbeFinishMoveto(pC
);
2859 if( rc
) goto abort_due_to_error
;
2860 }else if( sqlite3BtreeCursorHasMoved(pCrsr
) ){
2861 rc
= sqlite3VdbeHandleMovedCursor(pC
);
2862 if( rc
) goto abort_due_to_error
;
2863 goto op_column_restart
;
2865 assert( pC
->eCurType
==CURTYPE_BTREE
);
2867 assert( sqlite3BtreeCursorIsValid(pCrsr
) );
2868 pC
->payloadSize
= sqlite3BtreePayloadSize(pCrsr
);
2869 pC
->aRow
= sqlite3BtreePayloadFetch(pCrsr
, &pC
->szRow
);
2870 assert( pC
->szRow
<=pC
->payloadSize
);
2871 assert( pC
->szRow
<=65536 ); /* Maximum page size is 64KiB */
2873 pC
->cacheStatus
= p
->cacheCtr
;
2874 if( (aOffset
[0] = pC
->aRow
[0])<0x80 ){
2877 pC
->iHdrOffset
= sqlite3GetVarint32(pC
->aRow
, aOffset
);
2881 if( pC
->szRow
<aOffset
[0] ){ /*OPTIMIZATION-IF-FALSE*/
2882 /* pC->aRow does not have to hold the entire row, but it does at least
2883 ** need to cover the header of the record. If pC->aRow does not contain
2884 ** the complete header, then set it to zero, forcing the header to be
2885 ** dynamically allocated. */
2889 /* Make sure a corrupt database has not given us an oversize header.
2890 ** Do this now to avoid an oversize memory allocation.
2892 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2893 ** types use so much data space that there can only be 4096 and 32 of
2894 ** them, respectively. So the maximum header length results from a
2895 ** 3-byte type for each of the maximum of 32768 columns plus three
2896 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2898 if( aOffset
[0] > 98307 || aOffset
[0] > pC
->payloadSize
){
2899 goto op_column_corrupt
;
2902 /* This is an optimization. By skipping over the first few tests
2903 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2904 ** measurable performance gain.
2906 ** This branch is taken even if aOffset[0]==0. Such a record is never
2907 ** generated by SQLite, and could be considered corruption, but we
2908 ** accept it for historical reasons. When aOffset[0]==0, the code this
2909 ** branch jumps to reads past the end of the record, but never more
2910 ** than a few bytes. Even if the record occurs at the end of the page
2911 ** content area, the "page header" comes after the page content and so
2912 ** this overread is harmless. Similar overreads can occur for a corrupt
2916 assert( pC
->nHdrParsed
<=p2
); /* Conditional skipped */
2917 testcase( aOffset
[0]==0 );
2918 goto op_column_read_header
;
2920 }else if( sqlite3BtreeCursorHasMoved(pC
->uc
.pCursor
) ){
2921 rc
= sqlite3VdbeHandleMovedCursor(pC
);
2922 if( rc
) goto abort_due_to_error
;
2923 goto op_column_restart
;
2926 /* Make sure at least the first p2+1 entries of the header have been
2927 ** parsed and valid information is in aOffset[] and pC->aType[].
2929 if( pC
->nHdrParsed
<=p2
){
2930 /* If there is more header available for parsing in the record, try
2931 ** to extract additional fields up through the p2+1-th field
2933 if( pC
->iHdrOffset
<aOffset
[0] ){
2934 /* Make sure zData points to enough of the record to cover the header. */
2936 memset(&sMem
, 0, sizeof(sMem
));
2937 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pC
->uc
.pCursor
,aOffset
[0],&sMem
);
2938 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
2939 zData
= (u8
*)sMem
.z
;
2944 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
2945 op_column_read_header
:
2947 offset64
= aOffset
[i
];
2948 zHdr
= zData
+ pC
->iHdrOffset
;
2949 zEndHdr
= zData
+ aOffset
[0];
2950 testcase( zHdr
>=zEndHdr
);
2952 if( (pC
->aType
[i
] = t
= zHdr
[0])<0x80 ){
2954 offset64
+= sqlite3VdbeOneByteSerialTypeLen(t
);
2956 zHdr
+= sqlite3GetVarint32(zHdr
, &t
);
2958 offset64
+= sqlite3VdbeSerialTypeLen(t
);
2960 aOffset
[++i
] = (u32
)(offset64
& 0xffffffff);
2961 }while( (u32
)i
<=p2
&& zHdr
<zEndHdr
);
2963 /* The record is corrupt if any of the following are true:
2964 ** (1) the bytes of the header extend past the declared header size
2965 ** (2) the entire header was used but not all data was used
2966 ** (3) the end of the data extends beyond the end of the record.
2968 if( (zHdr
>=zEndHdr
&& (zHdr
>zEndHdr
|| offset64
!=pC
->payloadSize
))
2969 || (offset64
> pC
->payloadSize
)
2971 if( aOffset
[0]==0 ){
2975 if( pC
->aRow
==0 ) sqlite3VdbeMemRelease(&sMem
);
2976 goto op_column_corrupt
;
2981 pC
->iHdrOffset
= (u32
)(zHdr
- zData
);
2982 if( pC
->aRow
==0 ) sqlite3VdbeMemRelease(&sMem
);
2987 /* If after trying to extract new entries from the header, nHdrParsed is
2988 ** still not up to p2, that means that the record has fewer than p2
2989 ** columns. So the result will be either the default value or a NULL.
2991 if( pC
->nHdrParsed
<=p2
){
2992 pDest
= &aMem
[pOp
->p3
];
2993 memAboutToChange(p
, pDest
);
2994 if( pOp
->p4type
==P4_MEM
){
2995 sqlite3VdbeMemShallowCopy(pDest
, pOp
->p4
.pMem
, MEM_Static
);
2997 sqlite3VdbeMemSetNull(pDest
);
3005 /* Extract the content for the p2+1-th column. Control can only
3006 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
3009 assert( p2
<pC
->nHdrParsed
);
3010 assert( rc
==SQLITE_OK
);
3011 pDest
= &aMem
[pOp
->p3
];
3012 memAboutToChange(p
, pDest
);
3013 assert( sqlite3VdbeCheckMemInvariants(pDest
) );
3014 if( VdbeMemDynamic(pDest
) ){
3015 sqlite3VdbeMemSetNull(pDest
);
3017 assert( t
==pC
->aType
[p2
] );
3018 if( pC
->szRow
>=aOffset
[p2
+1] ){
3019 /* This is the common case where the desired content fits on the original
3020 ** page - where the content is not on an overflow page */
3021 zData
= pC
->aRow
+ aOffset
[p2
];
3023 sqlite3VdbeSerialGet(zData
, t
, pDest
);
3025 /* If the column value is a string, we need a persistent value, not
3026 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
3027 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
3029 static const u16 aFlag
[] = { MEM_Blob
, MEM_Str
|MEM_Term
};
3030 pDest
->n
= len
= (t
-12)/2;
3031 pDest
->enc
= encoding
;
3032 if( pDest
->szMalloc
< len
+2 ){
3033 if( len
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ) goto too_big
;
3034 pDest
->flags
= MEM_Null
;
3035 if( sqlite3VdbeMemGrow(pDest
, len
+2, 0) ) goto no_mem
;
3037 pDest
->z
= pDest
->zMalloc
;
3039 memcpy(pDest
->z
, zData
, len
);
3041 pDest
->z
[len
+1] = 0;
3042 pDest
->flags
= aFlag
[t
&1];
3045 pDest
->enc
= encoding
;
3046 /* This branch happens only when content is on overflow pages */
3047 if( ((pOp
->p5
& (OPFLAG_LENGTHARG
|OPFLAG_TYPEOFARG
))!=0
3048 && ((t
>=12 && (t
&1)==0) || (pOp
->p5
& OPFLAG_TYPEOFARG
)!=0))
3049 || (len
= sqlite3VdbeSerialTypeLen(t
))==0
3051 /* Content is irrelevant for
3052 ** 1. the typeof() function,
3053 ** 2. the length(X) function if X is a blob, and
3054 ** 3. if the content length is zero.
3055 ** So we might as well use bogus content rather than reading
3056 ** content from disk.
3058 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
3059 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
3060 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
3061 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
3062 ** and it begins with a bunch of zeros.
3064 sqlite3VdbeSerialGet((u8
*)sqlite3CtypeMap
, t
, pDest
);
3066 if( len
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ) goto too_big
;
3067 rc
= sqlite3VdbeMemFromBtree(pC
->uc
.pCursor
, aOffset
[p2
], len
, pDest
);
3068 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3069 sqlite3VdbeSerialGet((const u8
*)pDest
->z
, t
, pDest
);
3070 pDest
->flags
&= ~MEM_Ephem
;
3075 UPDATE_MAX_BLOBSIZE(pDest
);
3076 REGISTER_TRACE(pOp
->p3
, pDest
);
3081 pOp
= &aOp
[aOp
[0].p3
-1];
3084 rc
= SQLITE_CORRUPT_BKPT
;
3085 goto abort_due_to_error
;
3089 /* Opcode: TypeCheck P1 P2 P3 P4 *
3090 ** Synopsis: typecheck(r[P1@P2])
3092 ** Apply affinities to the range of P2 registers beginning with P1.
3093 ** Take the affinities from the Table object in P4. If any value
3094 ** cannot be coerced into the correct type, then raise an error.
3096 ** This opcode is similar to OP_Affinity except that this opcode
3097 ** forces the register type to the Table column type. This is used
3098 ** to implement "strict affinity".
3100 ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
3101 ** is zero. When P3 is non-zero, no type checking occurs for
3102 ** static generated columns. Virtual columns are computed at query time
3103 ** and so they are never checked.
3108 ** <li> P2 should be the number of non-virtual columns in the
3110 ** <li> Table P4 should be a STRICT table.
3113 ** If any precondition is false, an assertion fault occurs.
3115 case OP_TypeCheck
: {
3120 assert( pOp
->p4type
==P4_TABLE
);
3121 pTab
= pOp
->p4
.pTab
;
3122 assert( pTab
->tabFlags
& TF_Strict
);
3123 assert( pTab
->nNVCol
==pOp
->p2
);
3125 pIn1
= &aMem
[pOp
->p1
];
3126 for(i
=0; i
<pTab
->nCol
; i
++){
3127 if( aCol
[i
].colFlags
& COLFLAG_GENERATED
){
3128 if( aCol
[i
].colFlags
& COLFLAG_VIRTUAL
) continue;
3129 if( pOp
->p3
){ pIn1
++; continue; }
3131 assert( pIn1
< &aMem
[pOp
->p1
+pOp
->p2
] );
3132 applyAffinity(pIn1
, aCol
[i
].affinity
, encoding
);
3133 if( (pIn1
->flags
& MEM_Null
)==0 ){
3134 switch( aCol
[i
].eCType
){
3135 case COLTYPE_BLOB
: {
3136 if( (pIn1
->flags
& MEM_Blob
)==0 ) goto vdbe_type_error
;
3139 case COLTYPE_INTEGER
:
3141 if( (pIn1
->flags
& MEM_Int
)==0 ) goto vdbe_type_error
;
3144 case COLTYPE_TEXT
: {
3145 if( (pIn1
->flags
& MEM_Str
)==0 ) goto vdbe_type_error
;
3148 case COLTYPE_REAL
: {
3149 testcase( (pIn1
->flags
& (MEM_Real
|MEM_IntReal
))==MEM_Real
);
3150 assert( (pIn1
->flags
& MEM_IntReal
)==0 );
3151 if( pIn1
->flags
& MEM_Int
){
3152 /* When applying REAL affinity, if the result is still an MEM_Int
3153 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3154 ** so that we keep the high-resolution integer value but know that
3155 ** the type really wants to be REAL. */
3156 testcase( pIn1
->u
.i
==140737488355328LL );
3157 testcase( pIn1
->u
.i
==140737488355327LL );
3158 testcase( pIn1
->u
.i
==-140737488355328LL );
3159 testcase( pIn1
->u
.i
==-140737488355329LL );
3160 if( pIn1
->u
.i
<=140737488355327LL && pIn1
->u
.i
>=-140737488355328LL){
3161 pIn1
->flags
|= MEM_IntReal
;
3162 pIn1
->flags
&= ~MEM_Int
;
3164 pIn1
->u
.r
= (double)pIn1
->u
.i
;
3165 pIn1
->flags
|= MEM_Real
;
3166 pIn1
->flags
&= ~MEM_Int
;
3168 }else if( (pIn1
->flags
& (MEM_Real
|MEM_IntReal
))==0 ){
3169 goto vdbe_type_error
;
3174 /* COLTYPE_ANY. Accept anything. */
3179 REGISTER_TRACE((int)(pIn1
-aMem
), pIn1
);
3182 assert( pIn1
== &aMem
[pOp
->p1
+pOp
->p2
] );
3186 sqlite3VdbeError(p
, "cannot store %s value in %s column %s.%s",
3187 vdbeMemTypeName(pIn1
), sqlite3StdType
[aCol
[i
].eCType
-1],
3188 pTab
->zName
, aCol
[i
].zCnName
);
3189 rc
= SQLITE_CONSTRAINT_DATATYPE
;
3190 goto abort_due_to_error
;
3193 /* Opcode: Affinity P1 P2 * P4 *
3194 ** Synopsis: affinity(r[P1@P2])
3196 ** Apply affinities to a range of P2 registers starting with P1.
3198 ** P4 is a string that is P2 characters long. The N-th character of the
3199 ** string indicates the column affinity that should be used for the N-th
3200 ** memory cell in the range.
3203 const char *zAffinity
; /* The affinity to be applied */
3205 zAffinity
= pOp
->p4
.z
;
3206 assert( zAffinity
!=0 );
3207 assert( pOp
->p2
>0 );
3208 assert( zAffinity
[pOp
->p2
]==0 );
3209 pIn1
= &aMem
[pOp
->p1
];
3210 while( 1 /*exit-by-break*/ ){
3211 assert( pIn1
<= &p
->aMem
[(p
->nMem
+1 - p
->nCursor
)] );
3212 assert( zAffinity
[0]==SQLITE_AFF_NONE
|| memIsValid(pIn1
) );
3213 applyAffinity(pIn1
, zAffinity
[0], encoding
);
3214 if( zAffinity
[0]==SQLITE_AFF_REAL
&& (pIn1
->flags
& MEM_Int
)!=0 ){
3215 /* When applying REAL affinity, if the result is still an MEM_Int
3216 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3217 ** so that we keep the high-resolution integer value but know that
3218 ** the type really wants to be REAL. */
3219 testcase( pIn1
->u
.i
==140737488355328LL );
3220 testcase( pIn1
->u
.i
==140737488355327LL );
3221 testcase( pIn1
->u
.i
==-140737488355328LL );
3222 testcase( pIn1
->u
.i
==-140737488355329LL );
3223 if( pIn1
->u
.i
<=140737488355327LL && pIn1
->u
.i
>=-140737488355328LL ){
3224 pIn1
->flags
|= MEM_IntReal
;
3225 pIn1
->flags
&= ~MEM_Int
;
3227 pIn1
->u
.r
= (double)pIn1
->u
.i
;
3228 pIn1
->flags
|= MEM_Real
;
3229 pIn1
->flags
&= ~MEM_Int
;
3232 REGISTER_TRACE((int)(pIn1
-aMem
), pIn1
);
3234 if( zAffinity
[0]==0 ) break;
3240 /* Opcode: MakeRecord P1 P2 P3 P4 *
3241 ** Synopsis: r[P3]=mkrec(r[P1@P2])
3243 ** Convert P2 registers beginning with P1 into the [record format]
3244 ** use as a data record in a database table or as a key
3245 ** in an index. The OP_Column opcode can decode the record later.
3247 ** P4 may be a string that is P2 characters long. The N-th character of the
3248 ** string indicates the column affinity that should be used for the N-th
3249 ** field of the index key.
3251 ** The mapping from character to affinity is given by the SQLITE_AFF_
3252 ** macros defined in sqliteInt.h.
3254 ** If P4 is NULL then all index fields have the affinity BLOB.
3256 ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
3257 ** compile-time option is enabled:
3259 ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
3260 ** of the right-most table that can be null-trimmed.
3262 ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
3263 ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
3264 ** accept no-change records with serial_type 10. This value is
3265 ** only used inside an assert() and does not affect the end result.
3267 case OP_MakeRecord
: {
3268 Mem
*pRec
; /* The new record */
3269 u64 nData
; /* Number of bytes of data space */
3270 int nHdr
; /* Number of bytes of header space */
3271 i64 nByte
; /* Data space required for this record */
3272 i64 nZero
; /* Number of zero bytes at the end of the record */
3273 int nVarint
; /* Number of bytes in a varint */
3274 u32 serial_type
; /* Type field */
3275 Mem
*pData0
; /* First field to be combined into the record */
3276 Mem
*pLast
; /* Last field of the record */
3277 int nField
; /* Number of fields in the record */
3278 char *zAffinity
; /* The affinity string for the record */
3279 u32 len
; /* Length of a field */
3280 u8
*zHdr
; /* Where to write next byte of the header */
3281 u8
*zPayload
; /* Where to write next byte of the payload */
3283 /* Assuming the record contains N fields, the record format looks
3286 ** ------------------------------------------------------------------------
3287 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
3288 ** ------------------------------------------------------------------------
3290 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
3293 ** Each type field is a varint representing the serial type of the
3294 ** corresponding data element (see sqlite3VdbeSerialType()). The
3295 ** hdr-size field is also a varint which is the offset from the beginning
3296 ** of the record to data0.
3298 nData
= 0; /* Number of bytes of data space */
3299 nHdr
= 0; /* Number of bytes of header space */
3300 nZero
= 0; /* Number of zero bytes at the end of the record */
3302 zAffinity
= pOp
->p4
.z
;
3303 assert( nField
>0 && pOp
->p2
>0 && pOp
->p2
+nField
<=(p
->nMem
+1 - p
->nCursor
)+1 );
3304 pData0
= &aMem
[nField
];
3306 pLast
= &pData0
[nField
-1];
3308 /* Identify the output register */
3309 assert( pOp
->p3
<pOp
->p1
|| pOp
->p3
>=pOp
->p1
+pOp
->p2
);
3310 pOut
= &aMem
[pOp
->p3
];
3311 memAboutToChange(p
, pOut
);
3313 /* Apply the requested affinity to all inputs
3315 assert( pData0
<=pLast
);
3319 applyAffinity(pRec
, zAffinity
[0], encoding
);
3320 if( zAffinity
[0]==SQLITE_AFF_REAL
&& (pRec
->flags
& MEM_Int
) ){
3321 pRec
->flags
|= MEM_IntReal
;
3322 pRec
->flags
&= ~(MEM_Int
);
3324 REGISTER_TRACE((int)(pRec
-aMem
), pRec
);
3327 assert( zAffinity
[0]==0 || pRec
<=pLast
);
3328 }while( zAffinity
[0] );
3331 #ifdef SQLITE_ENABLE_NULL_TRIM
3332 /* NULLs can be safely trimmed from the end of the record, as long as
3333 ** as the schema format is 2 or more and none of the omitted columns
3334 ** have a non-NULL default value. Also, the record must be left with
3335 ** at least one field. If P5>0 then it will be one more than the
3336 ** index of the right-most column with a non-NULL default value */
3338 while( (pLast
->flags
& MEM_Null
)!=0 && nField
>pOp
->p5
){
3345 /* Loop through the elements that will make up the record to figure
3346 ** out how much space is required for the new record. After this loop,
3347 ** the Mem.uTemp field of each term should hold the serial-type that will
3348 ** be used for that term in the generated record:
3350 ** Mem.uTemp value type
3351 ** --------------- ---------------
3353 ** 1 1-byte signed integer
3354 ** 2 2-byte signed integer
3355 ** 3 3-byte signed integer
3356 ** 4 4-byte signed integer
3357 ** 5 6-byte signed integer
3358 ** 6 8-byte signed integer
3360 ** 8 Integer constant 0
3361 ** 9 Integer constant 1
3362 ** 10,11 reserved for expansion
3363 ** N>=12 and even BLOB
3364 ** N>=13 and odd text
3366 ** The following additional values are computed:
3367 ** nHdr Number of bytes needed for the record header
3368 ** nData Number of bytes of data space needed for the record
3369 ** nZero Zero bytes at the end of the record
3373 assert( memIsValid(pRec
) );
3374 if( pRec
->flags
& MEM_Null
){
3375 if( pRec
->flags
& MEM_Zero
){
3376 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3377 ** table methods that never invoke sqlite3_result_xxxxx() while
3378 ** computing an unchanging column value in an UPDATE statement.
3379 ** Give such values a special internal-use-only serial-type of 10
3380 ** so that they can be passed through to xUpdate and have
3381 ** a true sqlite3_value_nochange(). */
3382 #ifndef SQLITE_ENABLE_NULL_TRIM
3383 assert( pOp
->p5
==OPFLAG_NOCHNG_MAGIC
|| CORRUPT_DB
);
3390 }else if( pRec
->flags
& (MEM_Int
|MEM_IntReal
) ){
3391 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3394 testcase( pRec
->flags
& MEM_Int
);
3395 testcase( pRec
->flags
& MEM_IntReal
);
3402 testcase( uu
==127 ); testcase( uu
==128 );
3403 testcase( uu
==32767 ); testcase( uu
==32768 );
3404 testcase( uu
==8388607 ); testcase( uu
==8388608 );
3405 testcase( uu
==2147483647 ); testcase( uu
==2147483648LL );
3406 testcase( uu
==140737488355327LL ); testcase( uu
==140737488355328LL );
3408 if( (i
&1)==i
&& p
->minWriteFileFormat
>=4 ){
3409 pRec
->uTemp
= 8+(u32
)uu
;
3414 }else if( uu
<=32767 ){
3417 }else if( uu
<=8388607 ){
3420 }else if( uu
<=2147483647 ){
3423 }else if( uu
<=140737488355327LL ){
3428 if( pRec
->flags
& MEM_IntReal
){
3429 /* If the value is IntReal and is going to take up 8 bytes to store
3430 ** as an integer, then we might as well make it an 8-byte floating
3432 pRec
->u
.r
= (double)pRec
->u
.i
;
3433 pRec
->flags
&= ~MEM_IntReal
;
3434 pRec
->flags
|= MEM_Real
;
3440 }else if( pRec
->flags
& MEM_Real
){
3445 assert( db
->mallocFailed
|| pRec
->flags
&(MEM_Str
|MEM_Blob
) );
3446 assert( pRec
->n
>=0 );
3448 serial_type
= (len
*2) + 12 + ((pRec
->flags
& MEM_Str
)!=0);
3449 if( pRec
->flags
& MEM_Zero
){
3450 serial_type
+= pRec
->u
.nZero
*2;
3452 if( sqlite3VdbeMemExpandBlob(pRec
) ) goto no_mem
;
3453 len
+= pRec
->u
.nZero
;
3455 nZero
+= pRec
->u
.nZero
;
3459 nHdr
+= sqlite3VarintLen(serial_type
);
3460 pRec
->uTemp
= serial_type
;
3462 if( pRec
==pData0
) break;
3466 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3467 ** which determines the total number of bytes in the header. The varint
3468 ** value is the size of the header in bytes including the size varint
3470 testcase( nHdr
==126 );
3471 testcase( nHdr
==127 );
3473 /* The common case */
3476 /* Rare case of a really large header */
3477 nVarint
= sqlite3VarintLen(nHdr
);
3479 if( nVarint
<sqlite3VarintLen(nHdr
) ) nHdr
++;
3483 /* Make sure the output register has a buffer large enough to store
3484 ** the new record. The output register (pOp->p3) is not allowed to
3485 ** be one of the input registers (because the following call to
3486 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
3488 if( nByte
+nZero
<=pOut
->szMalloc
){
3489 /* The output register is already large enough to hold the record.
3490 ** No error checks or buffer enlargement is required */
3491 pOut
->z
= pOut
->zMalloc
;
3493 /* Need to make sure that the output is not too big and then enlarge
3494 ** the output register to hold the full result */
3495 if( nByte
+nZero
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
3498 if( sqlite3VdbeMemClearAndResize(pOut
, (int)nByte
) ){
3502 pOut
->n
= (int)nByte
;
3503 pOut
->flags
= MEM_Blob
;
3505 pOut
->u
.nZero
= nZero
;
3506 pOut
->flags
|= MEM_Zero
;
3508 UPDATE_MAX_BLOBSIZE(pOut
);
3509 zHdr
= (u8
*)pOut
->z
;
3510 zPayload
= zHdr
+ nHdr
;
3512 /* Write the record */
3516 zHdr
+= sqlite3PutVarint(zHdr
,nHdr
);
3518 assert( pData0
<=pLast
);
3520 while( 1 /*exit-by-break*/ ){
3521 serial_type
= pRec
->uTemp
;
3522 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3523 ** additional varints, one per column.
3524 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
3525 ** immediately follow the header. */
3526 if( serial_type
<=7 ){
3527 *(zHdr
++) = serial_type
;
3528 if( serial_type
==0 ){
3529 /* NULL value. No change in zPayload */
3533 if( serial_type
==7 ){
3534 assert( sizeof(v
)==sizeof(pRec
->u
.r
) );
3535 memcpy(&v
, &pRec
->u
.r
, sizeof(v
));
3536 swapMixedEndianFloat(v
);
3540 len
= i
= sqlite3SmallTypeSizes
[serial_type
];
3542 while( 1 /*exit-by-break*/ ){
3543 zPayload
[--i
] = (u8
)(v
&0xFF);
3549 }else if( serial_type
<0x80 ){
3550 *(zHdr
++) = serial_type
;
3551 if( serial_type
>=14 && pRec
->n
>0 ){
3552 assert( pRec
->z
!=0 );
3553 memcpy(zPayload
, pRec
->z
, pRec
->n
);
3554 zPayload
+= pRec
->n
;
3557 zHdr
+= sqlite3PutVarint(zHdr
, serial_type
);
3559 assert( pRec
->z
!=0 );
3560 memcpy(zPayload
, pRec
->z
, pRec
->n
);
3561 zPayload
+= pRec
->n
;
3564 if( pRec
==pLast
) break;
3567 assert( nHdr
==(int)(zHdr
- (u8
*)pOut
->z
) );
3568 assert( nByte
==(int)(zPayload
- (u8
*)pOut
->z
) );
3570 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
3571 REGISTER_TRACE(pOp
->p3
, pOut
);
3575 /* Opcode: Count P1 P2 P3 * *
3576 ** Synopsis: r[P2]=count()
3578 ** Store the number of entries (an integer value) in the table or index
3579 ** opened by cursor P1 in register P2.
3581 ** If P3==0, then an exact count is obtained, which involves visiting
3582 ** every btree page of the table. But if P3 is non-zero, an estimate
3583 ** is returned based on the current cursor position.
3585 case OP_Count
: { /* out2 */
3589 assert( p
->apCsr
[pOp
->p1
]->eCurType
==CURTYPE_BTREE
);
3590 pCrsr
= p
->apCsr
[pOp
->p1
]->uc
.pCursor
;
3593 nEntry
= sqlite3BtreeRowCountEst(pCrsr
);
3595 nEntry
= 0; /* Not needed. Only used to silence a warning. */
3596 rc
= sqlite3BtreeCount(db
, pCrsr
, &nEntry
);
3597 if( rc
) goto abort_due_to_error
;
3599 pOut
= out2Prerelease(p
, pOp
);
3601 goto check_for_interrupt
;
3604 /* Opcode: Savepoint P1 * * P4 *
3606 ** Open, release or rollback the savepoint named by parameter P4, depending
3607 ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3608 ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3609 ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
3611 case OP_Savepoint
: {
3612 int p1
; /* Value of P1 operand */
3613 char *zName
; /* Name of savepoint */
3616 Savepoint
*pSavepoint
;
3624 /* Assert that the p1 parameter is valid. Also that if there is no open
3625 ** transaction, then there cannot be any savepoints.
3627 assert( db
->pSavepoint
==0 || db
->autoCommit
==0 );
3628 assert( p1
==SAVEPOINT_BEGIN
||p1
==SAVEPOINT_RELEASE
||p1
==SAVEPOINT_ROLLBACK
);
3629 assert( db
->pSavepoint
|| db
->isTransactionSavepoint
==0 );
3630 assert( checkSavepointCount(db
) );
3631 assert( p
->bIsReader
);
3633 if( p1
==SAVEPOINT_BEGIN
){
3634 if( db
->nVdbeWrite
>0 ){
3635 /* A new savepoint cannot be created if there are active write
3636 ** statements (i.e. open read/write incremental blob handles).
3638 sqlite3VdbeError(p
, "cannot open savepoint - SQL statements in progress");
3641 nName
= sqlite3Strlen30(zName
);
3643 #ifndef SQLITE_OMIT_VIRTUALTABLE
3644 /* This call is Ok even if this savepoint is actually a transaction
3645 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3646 ** If this is a transaction savepoint being opened, it is guaranteed
3647 ** that the db->aVTrans[] array is empty. */
3648 assert( db
->autoCommit
==0 || db
->nVTrans
==0 );
3649 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_BEGIN
,
3650 db
->nStatement
+db
->nSavepoint
);
3651 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3654 /* Create a new savepoint structure. */
3655 pNew
= sqlite3DbMallocRawNN(db
, sizeof(Savepoint
)+nName
+1);
3657 pNew
->zName
= (char *)&pNew
[1];
3658 memcpy(pNew
->zName
, zName
, nName
+1);
3660 /* If there is no open transaction, then mark this as a special
3661 ** "transaction savepoint". */
3662 if( db
->autoCommit
){
3664 db
->isTransactionSavepoint
= 1;
3669 /* Link the new savepoint into the database handle's list. */
3670 pNew
->pNext
= db
->pSavepoint
;
3671 db
->pSavepoint
= pNew
;
3672 pNew
->nDeferredCons
= db
->nDeferredCons
;
3673 pNew
->nDeferredImmCons
= db
->nDeferredImmCons
;
3677 assert( p1
==SAVEPOINT_RELEASE
|| p1
==SAVEPOINT_ROLLBACK
);
3680 /* Find the named savepoint. If there is no such savepoint, then an
3681 ** an error is returned to the user. */
3683 pSavepoint
= db
->pSavepoint
;
3684 pSavepoint
&& sqlite3StrICmp(pSavepoint
->zName
, zName
);
3685 pSavepoint
= pSavepoint
->pNext
3690 sqlite3VdbeError(p
, "no such savepoint: %s", zName
);
3692 }else if( db
->nVdbeWrite
>0 && p1
==SAVEPOINT_RELEASE
){
3693 /* It is not possible to release (commit) a savepoint if there are
3694 ** active write statements.
3696 sqlite3VdbeError(p
, "cannot release savepoint - "
3697 "SQL statements in progress");
3701 /* Determine whether or not this is a transaction savepoint. If so,
3702 ** and this is a RELEASE command, then the current transaction
3705 int isTransaction
= pSavepoint
->pNext
==0 && db
->isTransactionSavepoint
;
3706 if( isTransaction
&& p1
==SAVEPOINT_RELEASE
){
3707 if( (rc
= sqlite3VdbeCheckFk(p
, 1))!=SQLITE_OK
){
3711 if( sqlite3VdbeHalt(p
)==SQLITE_BUSY
){
3712 p
->pc
= (int)(pOp
- aOp
);
3714 p
->rc
= rc
= SQLITE_BUSY
;
3721 db
->isTransactionSavepoint
= 0;
3725 iSavepoint
= db
->nSavepoint
- iSavepoint
- 1;
3726 if( p1
==SAVEPOINT_ROLLBACK
){
3727 isSchemaChange
= (db
->mDbFlags
& DBFLAG_SchemaChange
)!=0;
3728 for(ii
=0; ii
<db
->nDb
; ii
++){
3729 rc
= sqlite3BtreeTripAllCursors(db
->aDb
[ii
].pBt
,
3730 SQLITE_ABORT_ROLLBACK
,
3732 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3735 assert( p1
==SAVEPOINT_RELEASE
);
3738 for(ii
=0; ii
<db
->nDb
; ii
++){
3739 rc
= sqlite3BtreeSavepoint(db
->aDb
[ii
].pBt
, p1
, iSavepoint
);
3740 if( rc
!=SQLITE_OK
){
3741 goto abort_due_to_error
;
3744 if( isSchemaChange
){
3745 sqlite3ExpirePreparedStatements(db
, 0);
3746 sqlite3ResetAllSchemasOfConnection(db
);
3747 db
->mDbFlags
|= DBFLAG_SchemaChange
;
3750 if( rc
) goto abort_due_to_error
;
3752 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3753 ** savepoints nested inside of the savepoint being operated on. */
3754 while( db
->pSavepoint
!=pSavepoint
){
3755 pTmp
= db
->pSavepoint
;
3756 db
->pSavepoint
= pTmp
->pNext
;
3757 sqlite3DbFree(db
, pTmp
);
3761 /* If it is a RELEASE, then destroy the savepoint being operated on
3762 ** too. If it is a ROLLBACK TO, then set the number of deferred
3763 ** constraint violations present in the database to the value stored
3764 ** when the savepoint was created. */
3765 if( p1
==SAVEPOINT_RELEASE
){
3766 assert( pSavepoint
==db
->pSavepoint
);
3767 db
->pSavepoint
= pSavepoint
->pNext
;
3768 sqlite3DbFree(db
, pSavepoint
);
3769 if( !isTransaction
){
3773 assert( p1
==SAVEPOINT_ROLLBACK
);
3774 db
->nDeferredCons
= pSavepoint
->nDeferredCons
;
3775 db
->nDeferredImmCons
= pSavepoint
->nDeferredImmCons
;
3778 if( !isTransaction
|| p1
==SAVEPOINT_ROLLBACK
){
3779 rc
= sqlite3VtabSavepoint(db
, p1
, iSavepoint
);
3780 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3784 if( rc
) goto abort_due_to_error
;
3785 if( p
->eVdbeState
==VDBE_HALT_STATE
){
3792 /* Opcode: AutoCommit P1 P2 * * *
3794 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
3795 ** back any currently active btree transactions. If there are any active
3796 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3797 ** there are active writing VMs or active VMs that use shared cache.
3799 ** This instruction causes the VM to halt.
3801 case OP_AutoCommit
: {
3802 int desiredAutoCommit
;
3805 desiredAutoCommit
= pOp
->p1
;
3806 iRollback
= pOp
->p2
;
3807 assert( desiredAutoCommit
==1 || desiredAutoCommit
==0 );
3808 assert( desiredAutoCommit
==1 || iRollback
==0 );
3809 assert( db
->nVdbeActive
>0 ); /* At least this one VM is active */
3810 assert( p
->bIsReader
);
3812 if( desiredAutoCommit
!=db
->autoCommit
){
3814 assert( desiredAutoCommit
==1 );
3815 sqlite3RollbackAll(db
, SQLITE_ABORT_ROLLBACK
);
3817 }else if( desiredAutoCommit
&& db
->nVdbeWrite
>0 ){
3818 /* If this instruction implements a COMMIT and other VMs are writing
3819 ** return an error indicating that the other VMs must complete first.
3821 sqlite3VdbeError(p
, "cannot commit transaction - "
3822 "SQL statements in progress");
3824 goto abort_due_to_error
;
3825 }else if( (rc
= sqlite3VdbeCheckFk(p
, 1))!=SQLITE_OK
){
3828 db
->autoCommit
= (u8
)desiredAutoCommit
;
3830 if( sqlite3VdbeHalt(p
)==SQLITE_BUSY
){
3831 p
->pc
= (int)(pOp
- aOp
);
3832 db
->autoCommit
= (u8
)(1-desiredAutoCommit
);
3833 p
->rc
= rc
= SQLITE_BUSY
;
3836 sqlite3CloseSavepoints(db
);
3837 if( p
->rc
==SQLITE_OK
){
3845 (!desiredAutoCommit
)?"cannot start a transaction within a transaction":(
3846 (iRollback
)?"cannot rollback - no transaction is active":
3847 "cannot commit - no transaction is active"));
3850 goto abort_due_to_error
;
3852 /*NOTREACHED*/ assert(0);
3855 /* Opcode: Transaction P1 P2 P3 P4 P5
3857 ** Begin a transaction on database P1 if a transaction is not already
3859 ** If P2 is non-zero, then a write-transaction is started, or if a
3860 ** read-transaction is already active, it is upgraded to a write-transaction.
3861 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3862 ** then an exclusive transaction is started.
3864 ** P1 is the index of the database file on which the transaction is
3865 ** started. Index 0 is the main database file and index 1 is the
3866 ** file used for temporary tables. Indices of 2 or more are used for
3867 ** attached databases.
3869 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3870 ** true (this flag is set if the Vdbe may modify more than one row and may
3871 ** throw an ABORT exception), a statement transaction may also be opened.
3872 ** More specifically, a statement transaction is opened iff the database
3873 ** connection is currently not in autocommit mode, or if there are other
3874 ** active statements. A statement transaction allows the changes made by this
3875 ** VDBE to be rolled back after an error without having to roll back the
3876 ** entire transaction. If no error is encountered, the statement transaction
3877 ** will automatically commit when the VDBE halts.
3879 ** If P5!=0 then this opcode also checks the schema cookie against P3
3880 ** and the schema generation counter against P4.
3881 ** The cookie changes its value whenever the database schema changes.
3882 ** This operation is used to detect when that the cookie has changed
3883 ** and that the current process needs to reread the schema. If the schema
3884 ** cookie in P3 differs from the schema cookie in the database header or
3885 ** if the schema generation counter in P4 differs from the current
3886 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
3887 ** halts. The sqlite3_step() wrapper function might then reprepare the
3888 ** statement and rerun it from the beginning.
3890 case OP_Transaction
: {
3895 assert( p
->bIsReader
);
3896 assert( p
->readOnly
==0 || pOp
->p2
==0 );
3897 assert( pOp
->p2
>=0 && pOp
->p2
<=2 );
3898 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
3899 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
3900 assert( rc
==SQLITE_OK
);
3901 if( pOp
->p2
&& (db
->flags
& (SQLITE_QueryOnly
|SQLITE_CorruptRdOnly
))!=0 ){
3902 if( db
->flags
& SQLITE_QueryOnly
){
3903 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
3904 rc
= SQLITE_READONLY
;
3906 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
3908 rc
= SQLITE_CORRUPT
;
3910 goto abort_due_to_error
;
3912 pDb
= &db
->aDb
[pOp
->p1
];
3916 rc
= sqlite3BtreeBeginTrans(pBt
, pOp
->p2
, &iMeta
);
3917 testcase( rc
==SQLITE_BUSY_SNAPSHOT
);
3918 testcase( rc
==SQLITE_BUSY_RECOVERY
);
3919 if( rc
!=SQLITE_OK
){
3920 if( (rc
&0xff)==SQLITE_BUSY
){
3921 p
->pc
= (int)(pOp
- aOp
);
3925 goto abort_due_to_error
;
3928 if( p
->usesStmtJournal
3930 && (db
->autoCommit
==0 || db
->nVdbeRead
>1)
3932 assert( sqlite3BtreeTxnState(pBt
)==SQLITE_TXN_WRITE
);
3933 if( p
->iStatement
==0 ){
3934 assert( db
->nStatement
>=0 && db
->nSavepoint
>=0 );
3936 p
->iStatement
= db
->nSavepoint
+ db
->nStatement
;
3939 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_BEGIN
, p
->iStatement
-1);
3940 if( rc
==SQLITE_OK
){
3941 rc
= sqlite3BtreeBeginStmt(pBt
, p
->iStatement
);
3944 /* Store the current value of the database handles deferred constraint
3945 ** counter. If the statement transaction needs to be rolled back,
3946 ** the value of this counter needs to be restored too. */
3947 p
->nStmtDefCons
= db
->nDeferredCons
;
3948 p
->nStmtDefImmCons
= db
->nDeferredImmCons
;
3951 assert( pOp
->p5
==0 || pOp
->p4type
==P4_INT32
);
3954 && (iMeta
!=pOp
->p3
|| pDb
->pSchema
->iGeneration
!=pOp
->p4
.i
)
3957 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3958 ** version is checked to ensure that the schema has not changed since the
3959 ** SQL statement was prepared.
3961 sqlite3DbFree(db
, p
->zErrMsg
);
3962 p
->zErrMsg
= sqlite3DbStrDup(db
, "database schema has changed");
3963 /* If the schema-cookie from the database file matches the cookie
3964 ** stored with the in-memory representation of the schema, do
3965 ** not reload the schema from the database file.
3967 ** If virtual-tables are in use, this is not just an optimization.
3968 ** Often, v-tables store their data in other SQLite tables, which
3969 ** are queried from within xNext() and other v-table methods using
3970 ** prepared queries. If such a query is out-of-date, we do not want to
3971 ** discard the database schema, as the user code implementing the
3972 ** v-table would have to be ready for the sqlite3_vtab structure itself
3973 ** to be invalidated whenever sqlite3_step() is called from within
3974 ** a v-table method.
3976 if( db
->aDb
[pOp
->p1
].pSchema
->schema_cookie
!=iMeta
){
3977 sqlite3ResetOneSchema(db
, pOp
->p1
);
3982 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
3983 ** from being modified in sqlite3VdbeHalt(). If this statement is
3984 ** reprepared, changeCntOn will be set again. */
3987 if( rc
) goto abort_due_to_error
;
3991 /* Opcode: ReadCookie P1 P2 P3 * *
3993 ** Read cookie number P3 from database P1 and write it into register P2.
3994 ** P3==1 is the schema version. P3==2 is the database format.
3995 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
3996 ** the main database file and P1==1 is the database file used to store
3997 ** temporary tables.
3999 ** There must be a read-lock on the database (either a transaction
4000 ** must be started or there must be an open cursor) before
4001 ** executing this instruction.
4003 case OP_ReadCookie
: { /* out2 */
4008 assert( p
->bIsReader
);
4011 assert( pOp
->p3
<SQLITE_N_BTREE_META
);
4012 assert( iDb
>=0 && iDb
<db
->nDb
);
4013 assert( db
->aDb
[iDb
].pBt
!=0 );
4014 assert( DbMaskTest(p
->btreeMask
, iDb
) );
4016 sqlite3BtreeGetMeta(db
->aDb
[iDb
].pBt
, iCookie
, (u32
*)&iMeta
);
4017 pOut
= out2Prerelease(p
, pOp
);
4022 /* Opcode: SetCookie P1 P2 P3 * P5
4024 ** Write the integer value P3 into cookie number P2 of database P1.
4025 ** P2==1 is the schema version. P2==2 is the database format.
4026 ** P2==3 is the recommended pager cache
4027 ** size, and so forth. P1==0 is the main database file and P1==1 is the
4028 ** database file used to store temporary tables.
4030 ** A transaction must be started before executing this opcode.
4032 ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
4033 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
4034 ** has P5 set to 1, so that the internal schema version will be different
4035 ** from the database schema version, resulting in a schema reset.
4037 case OP_SetCookie
: {
4040 sqlite3VdbeIncrWriteCounter(p
, 0);
4041 assert( pOp
->p2
<SQLITE_N_BTREE_META
);
4042 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
4043 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
4044 assert( p
->readOnly
==0 );
4045 pDb
= &db
->aDb
[pOp
->p1
];
4046 assert( pDb
->pBt
!=0 );
4047 assert( sqlite3SchemaMutexHeld(db
, pOp
->p1
, 0) );
4048 /* See note about index shifting on OP_ReadCookie */
4049 rc
= sqlite3BtreeUpdateMeta(pDb
->pBt
, pOp
->p2
, pOp
->p3
);
4050 if( pOp
->p2
==BTREE_SCHEMA_VERSION
){
4051 /* When the schema cookie changes, record the new cookie internally */
4052 *(u32
*)&pDb
->pSchema
->schema_cookie
= *(u32
*)&pOp
->p3
- pOp
->p5
;
4053 db
->mDbFlags
|= DBFLAG_SchemaChange
;
4054 sqlite3FkClearTriggerCache(db
, pOp
->p1
);
4055 }else if( pOp
->p2
==BTREE_FILE_FORMAT
){
4056 /* Record changes in the file format */
4057 pDb
->pSchema
->file_format
= pOp
->p3
;
4060 /* Invalidate all prepared statements whenever the TEMP database
4061 ** schema is changed. Ticket #1644 */
4062 sqlite3ExpirePreparedStatements(db
, 0);
4065 if( rc
) goto abort_due_to_error
;
4069 /* Opcode: OpenRead P1 P2 P3 P4 P5
4070 ** Synopsis: root=P2 iDb=P3
4072 ** Open a read-only cursor for the database table whose root page is
4073 ** P2 in a database file. The database file is determined by P3.
4074 ** P3==0 means the main database, P3==1 means the database used for
4075 ** temporary tables, and P3>1 means used the corresponding attached
4076 ** database. Give the new cursor an identifier of P1. The P1
4077 ** values need not be contiguous but all P1 values should be small integers.
4078 ** It is an error for P1 to be negative.
4082 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4083 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
4084 ** of OP_SeekLE/OP_IdxLT)
4087 ** The P4 value may be either an integer (P4_INT32) or a pointer to
4088 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
4089 ** object, then table being opened must be an [index b-tree] where the
4090 ** KeyInfo object defines the content and collating
4091 ** sequence of that index b-tree. Otherwise, if P4 is an integer
4092 ** value, then the table being opened must be a [table b-tree] with a
4093 ** number of columns no less than the value of P4.
4095 ** See also: OpenWrite, ReopenIdx
4097 /* Opcode: ReopenIdx P1 P2 P3 P4 P5
4098 ** Synopsis: root=P2 iDb=P3
4100 ** The ReopenIdx opcode works like OP_OpenRead except that it first
4101 ** checks to see if the cursor on P1 is already open on the same
4102 ** b-tree and if it is this opcode becomes a no-op. In other words,
4103 ** if the cursor is already open, do not reopen it.
4105 ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
4106 ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
4107 ** be the same as every other ReopenIdx or OpenRead for the same cursor
4112 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4113 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
4114 ** of OP_SeekLE/OP_IdxLT)
4117 ** See also: OP_OpenRead, OP_OpenWrite
4119 /* Opcode: OpenWrite P1 P2 P3 P4 P5
4120 ** Synopsis: root=P2 iDb=P3
4122 ** Open a read/write cursor named P1 on the table or index whose root
4123 ** page is P2 (or whose root page is held in register P2 if the
4124 ** OPFLAG_P2ISREG bit is set in P5 - see below).
4126 ** The P4 value may be either an integer (P4_INT32) or a pointer to
4127 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
4128 ** object, then table being opened must be an [index b-tree] where the
4129 ** KeyInfo object defines the content and collating
4130 ** sequence of that index b-tree. Otherwise, if P4 is an integer
4131 ** value, then the table being opened must be a [table b-tree] with a
4132 ** number of columns no less than the value of P4.
4136 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4137 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
4138 ** of OP_SeekLE/OP_IdxLT)
4139 ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
4140 ** and subsequently delete entries in an index btree. This is a
4141 ** hint to the storage engine that the storage engine is allowed to
4142 ** ignore. The hint is not used by the official SQLite b*tree storage
4143 ** engine, but is used by COMDB2.
4144 ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
4145 ** as the root page, not the value of P2 itself.
4148 ** This instruction works like OpenRead except that it opens the cursor
4149 ** in read/write mode.
4151 ** See also: OP_OpenRead, OP_ReopenIdx
4153 case OP_ReopenIdx
: { /* ncycle */
4163 assert( pOp
->p5
==0 || pOp
->p5
==OPFLAG_SEEKEQ
);
4164 assert( pOp
->p4type
==P4_KEYINFO
);
4165 pCur
= p
->apCsr
[pOp
->p1
];
4166 if( pCur
&& pCur
->pgnoRoot
==(u32
)pOp
->p2
){
4167 assert( pCur
->iDb
==pOp
->p3
); /* Guaranteed by the code generator */
4168 assert( pCur
->eCurType
==CURTYPE_BTREE
);
4169 sqlite3BtreeClearCursor(pCur
->uc
.pCursor
);
4170 goto open_cursor_set_hints
;
4172 /* If the cursor is not currently open or is open on a different
4173 ** index, then fall through into OP_OpenRead to force a reopen */
4174 case OP_OpenRead
: /* ncycle */
4177 assert( pOp
->opcode
==OP_OpenWrite
|| pOp
->p5
==0 || pOp
->p5
==OPFLAG_SEEKEQ
);
4178 assert( p
->bIsReader
);
4179 assert( pOp
->opcode
==OP_OpenRead
|| pOp
->opcode
==OP_ReopenIdx
4180 || p
->readOnly
==0 );
4182 if( p
->expired
==1 ){
4183 rc
= SQLITE_ABORT_ROLLBACK
;
4184 goto abort_due_to_error
;
4191 assert( iDb
>=0 && iDb
<db
->nDb
);
4192 assert( DbMaskTest(p
->btreeMask
, iDb
) );
4193 pDb
= &db
->aDb
[iDb
];
4196 if( pOp
->opcode
==OP_OpenWrite
){
4197 assert( OPFLAG_FORDELETE
==BTREE_FORDELETE
);
4198 wrFlag
= BTREE_WRCSR
| (pOp
->p5
& OPFLAG_FORDELETE
);
4199 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
4200 if( pDb
->pSchema
->file_format
< p
->minWriteFileFormat
){
4201 p
->minWriteFileFormat
= pDb
->pSchema
->file_format
;
4206 if( pOp
->p5
& OPFLAG_P2ISREG
){
4208 assert( p2
<=(u32
)(p
->nMem
+1 - p
->nCursor
) );
4209 assert( pOp
->opcode
==OP_OpenWrite
);
4211 assert( memIsValid(pIn2
) );
4212 assert( (pIn2
->flags
& MEM_Int
)!=0 );
4213 sqlite3VdbeMemIntegerify(pIn2
);
4214 p2
= (int)pIn2
->u
.i
;
4215 /* The p2 value always comes from a prior OP_CreateBtree opcode and
4216 ** that opcode will always set the p2 value to 2 or more or else fail.
4217 ** If there were a failure, the prepared statement would have halted
4218 ** before reaching this instruction. */
4221 if( pOp
->p4type
==P4_KEYINFO
){
4222 pKeyInfo
= pOp
->p4
.pKeyInfo
;
4223 assert( pKeyInfo
->enc
==ENC(db
) );
4224 assert( pKeyInfo
->db
==db
);
4225 nField
= pKeyInfo
->nAllField
;
4226 }else if( pOp
->p4type
==P4_INT32
){
4229 assert( pOp
->p1
>=0 );
4230 assert( nField
>=0 );
4231 testcase( nField
==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
4232 pCur
= allocateCursor(p
, pOp
->p1
, nField
, CURTYPE_BTREE
);
4233 if( pCur
==0 ) goto no_mem
;
4236 pCur
->isOrdered
= 1;
4237 pCur
->pgnoRoot
= p2
;
4239 pCur
->wrFlag
= wrFlag
;
4241 rc
= sqlite3BtreeCursor(pX
, p2
, wrFlag
, pKeyInfo
, pCur
->uc
.pCursor
);
4242 pCur
->pKeyInfo
= pKeyInfo
;
4243 /* Set the VdbeCursor.isTable variable. Previous versions of
4244 ** SQLite used to check if the root-page flags were sane at this point
4245 ** and report database corruption if they were not, but this check has
4246 ** since moved into the btree layer. */
4247 pCur
->isTable
= pOp
->p4type
!=P4_KEYINFO
;
4249 open_cursor_set_hints
:
4250 assert( OPFLAG_BULKCSR
==BTREE_BULKLOAD
);
4251 assert( OPFLAG_SEEKEQ
==BTREE_SEEK_EQ
);
4252 testcase( pOp
->p5
& OPFLAG_BULKCSR
);
4253 testcase( pOp
->p2
& OPFLAG_SEEKEQ
);
4254 sqlite3BtreeCursorHintFlags(pCur
->uc
.pCursor
,
4255 (pOp
->p5
& (OPFLAG_BULKCSR
|OPFLAG_SEEKEQ
)));
4256 if( rc
) goto abort_due_to_error
;
4260 /* Opcode: OpenDup P1 P2 * * *
4262 ** Open a new cursor P1 that points to the same ephemeral table as
4263 ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
4264 ** opcode. Only ephemeral cursors may be duplicated.
4266 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
4268 case OP_OpenDup
: { /* ncycle */
4269 VdbeCursor
*pOrig
; /* The original cursor to be duplicated */
4270 VdbeCursor
*pCx
; /* The new cursor */
4272 pOrig
= p
->apCsr
[pOp
->p2
];
4274 assert( pOrig
->isEphemeral
); /* Only ephemeral cursors can be duplicated */
4276 pCx
= allocateCursor(p
, pOp
->p1
, pOrig
->nField
, CURTYPE_BTREE
);
4277 if( pCx
==0 ) goto no_mem
;
4279 pCx
->isEphemeral
= 1;
4280 pCx
->pKeyInfo
= pOrig
->pKeyInfo
;
4281 pCx
->isTable
= pOrig
->isTable
;
4282 pCx
->pgnoRoot
= pOrig
->pgnoRoot
;
4283 pCx
->isOrdered
= pOrig
->isOrdered
;
4284 pCx
->ub
.pBtx
= pOrig
->ub
.pBtx
;
4287 rc
= sqlite3BtreeCursor(pCx
->ub
.pBtx
, pCx
->pgnoRoot
, BTREE_WRCSR
,
4288 pCx
->pKeyInfo
, pCx
->uc
.pCursor
);
4289 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
4290 ** opened for a database. Since there is already an open cursor when this
4291 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
4292 assert( rc
==SQLITE_OK
);
4297 /* Opcode: OpenEphemeral P1 P2 P3 P4 P5
4298 ** Synopsis: nColumn=P2
4300 ** Open a new cursor P1 to a transient table.
4301 ** The cursor is always opened read/write even if
4302 ** the main database is read-only. The ephemeral
4303 ** table is deleted automatically when the cursor is closed.
4305 ** If the cursor P1 is already opened on an ephemeral table, the table
4306 ** is cleared (all content is erased).
4308 ** P2 is the number of columns in the ephemeral table.
4309 ** The cursor points to a BTree table if P4==0 and to a BTree index
4310 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
4311 ** that defines the format of keys in the index.
4313 ** The P5 parameter can be a mask of the BTREE_* flags defined
4314 ** in btree.h. These flags control aspects of the operation of
4315 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
4316 ** added automatically.
4318 ** If P3 is positive, then reg[P3] is modified slightly so that it
4319 ** can be used as zero-length data for OP_Insert. This is an optimization
4320 ** that avoids an extra OP_Blob opcode to initialize that register.
4322 /* Opcode: OpenAutoindex P1 P2 * P4 *
4323 ** Synopsis: nColumn=P2
4325 ** This opcode works the same as OP_OpenEphemeral. It has a
4326 ** different name to distinguish its use. Tables created using
4327 ** by this opcode will be used for automatically created transient
4328 ** indices in joins.
4330 case OP_OpenAutoindex
: /* ncycle */
4331 case OP_OpenEphemeral
: { /* ncycle */
4335 static const int vfsFlags
=
4336 SQLITE_OPEN_READWRITE
|
4337 SQLITE_OPEN_CREATE
|
4338 SQLITE_OPEN_EXCLUSIVE
|
4339 SQLITE_OPEN_DELETEONCLOSE
|
4340 SQLITE_OPEN_TRANSIENT_DB
;
4341 assert( pOp
->p1
>=0 );
4342 assert( pOp
->p2
>=0 );
4344 /* Make register reg[P3] into a value that can be used as the data
4345 ** form sqlite3BtreeInsert() where the length of the data is zero. */
4346 assert( pOp
->p2
==0 ); /* Only used when number of columns is zero */
4347 assert( pOp
->opcode
==OP_OpenEphemeral
);
4348 assert( aMem
[pOp
->p3
].flags
& MEM_Null
);
4349 aMem
[pOp
->p3
].n
= 0;
4350 aMem
[pOp
->p3
].z
= "";
4352 pCx
= p
->apCsr
[pOp
->p1
];
4353 if( pCx
&& !pCx
->noReuse
&& ALWAYS(pOp
->p2
<=pCx
->nField
) ){
4354 /* If the ephermeral table is already open and has no duplicates from
4355 ** OP_OpenDup, then erase all existing content so that the table is
4356 ** empty again, rather than creating a new table. */
4357 assert( pCx
->isEphemeral
);
4359 pCx
->cacheStatus
= CACHE_STALE
;
4360 rc
= sqlite3BtreeClearTable(pCx
->ub
.pBtx
, pCx
->pgnoRoot
, 0);
4362 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p2
, CURTYPE_BTREE
);
4363 if( pCx
==0 ) goto no_mem
;
4364 pCx
->isEphemeral
= 1;
4365 rc
= sqlite3BtreeOpen(db
->pVfs
, 0, db
, &pCx
->ub
.pBtx
,
4366 BTREE_OMIT_JOURNAL
| BTREE_SINGLE
| pOp
->p5
,
4368 if( rc
==SQLITE_OK
){
4369 rc
= sqlite3BtreeBeginTrans(pCx
->ub
.pBtx
, 1, 0);
4370 if( rc
==SQLITE_OK
){
4371 /* If a transient index is required, create it by calling
4372 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
4373 ** opening it. If a transient table is required, just use the
4374 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
4376 if( (pCx
->pKeyInfo
= pKeyInfo
= pOp
->p4
.pKeyInfo
)!=0 ){
4377 assert( pOp
->p4type
==P4_KEYINFO
);
4378 rc
= sqlite3BtreeCreateTable(pCx
->ub
.pBtx
, &pCx
->pgnoRoot
,
4379 BTREE_BLOBKEY
| pOp
->p5
);
4380 if( rc
==SQLITE_OK
){
4381 assert( pCx
->pgnoRoot
==SCHEMA_ROOT
+1 );
4382 assert( pKeyInfo
->db
==db
);
4383 assert( pKeyInfo
->enc
==ENC(db
) );
4384 rc
= sqlite3BtreeCursor(pCx
->ub
.pBtx
, pCx
->pgnoRoot
, BTREE_WRCSR
,
4385 pKeyInfo
, pCx
->uc
.pCursor
);
4389 pCx
->pgnoRoot
= SCHEMA_ROOT
;
4390 rc
= sqlite3BtreeCursor(pCx
->ub
.pBtx
, SCHEMA_ROOT
, BTREE_WRCSR
,
4391 0, pCx
->uc
.pCursor
);
4395 pCx
->isOrdered
= (pOp
->p5
!=BTREE_UNORDERED
);
4397 sqlite3BtreeClose(pCx
->ub
.pBtx
);
4401 if( rc
) goto abort_due_to_error
;
4406 /* Opcode: SorterOpen P1 P2 P3 P4 *
4408 ** This opcode works like OP_OpenEphemeral except that it opens
4409 ** a transient index that is specifically designed to sort large
4410 ** tables using an external merge-sort algorithm.
4412 ** If argument P3 is non-zero, then it indicates that the sorter may
4413 ** assume that a stable sort considering the first P3 fields of each
4414 ** key is sufficient to produce the required results.
4416 case OP_SorterOpen
: {
4419 assert( pOp
->p1
>=0 );
4420 assert( pOp
->p2
>=0 );
4421 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p2
, CURTYPE_SORTER
);
4422 if( pCx
==0 ) goto no_mem
;
4423 pCx
->pKeyInfo
= pOp
->p4
.pKeyInfo
;
4424 assert( pCx
->pKeyInfo
->db
==db
);
4425 assert( pCx
->pKeyInfo
->enc
==ENC(db
) );
4426 rc
= sqlite3VdbeSorterInit(db
, pOp
->p3
, pCx
);
4427 if( rc
) goto abort_due_to_error
;
4431 /* Opcode: SequenceTest P1 P2 * * *
4432 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4434 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4435 ** to P2. Regardless of whether or not the jump is taken, increment the
4436 ** the sequence value.
4438 case OP_SequenceTest
: {
4440 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4441 pC
= p
->apCsr
[pOp
->p1
];
4442 assert( isSorter(pC
) );
4443 if( (pC
->seqCount
++)==0 ){
4449 /* Opcode: OpenPseudo P1 P2 P3 * *
4450 ** Synopsis: P3 columns in r[P2]
4452 ** Open a new cursor that points to a fake table that contains a single
4453 ** row of data. The content of that one row is the content of memory
4454 ** register P2. In other words, cursor P1 becomes an alias for the
4455 ** MEM_Blob content contained in register P2.
4457 ** A pseudo-table created by this opcode is used to hold a single
4458 ** row output from the sorter so that the row can be decomposed into
4459 ** individual columns using the OP_Column opcode. The OP_Column opcode
4460 ** is the only cursor opcode that works with a pseudo-table.
4462 ** P3 is the number of fields in the records that will be stored by
4463 ** the pseudo-table.
4465 case OP_OpenPseudo
: {
4468 assert( pOp
->p1
>=0 );
4469 assert( pOp
->p3
>=0 );
4470 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p3
, CURTYPE_PSEUDO
);
4471 if( pCx
==0 ) goto no_mem
;
4473 pCx
->seekResult
= pOp
->p2
;
4475 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4476 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4477 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4478 ** which is a performance optimization */
4479 pCx
->uc
.pCursor
= sqlite3BtreeFakeValidCursor();
4480 assert( pOp
->p5
==0 );
4484 /* Opcode: Close P1 * * * *
4486 ** Close a cursor previously opened as P1. If P1 is not
4487 ** currently open, this instruction is a no-op.
4489 case OP_Close
: { /* ncycle */
4490 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4491 sqlite3VdbeFreeCursor(p
, p
->apCsr
[pOp
->p1
]);
4492 p
->apCsr
[pOp
->p1
] = 0;
4496 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4497 /* Opcode: ColumnsUsed P1 * * P4 *
4499 ** This opcode (which only exists if SQLite was compiled with
4500 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4501 ** table or index for cursor P1 are used. P4 is a 64-bit integer
4502 ** (P4_INT64) in which the first 63 bits are one for each of the
4503 ** first 63 columns of the table or index that are actually used
4504 ** by the cursor. The high-order bit is set if any column after
4505 ** the 64th is used.
4507 case OP_ColumnsUsed
: {
4509 pC
= p
->apCsr
[pOp
->p1
];
4510 assert( pC
->eCurType
==CURTYPE_BTREE
);
4511 pC
->maskUsed
= *(u64
*)pOp
->p4
.pI64
;
4516 /* Opcode: SeekGE P1 P2 P3 P4 *
4517 ** Synopsis: key=r[P3@P4]
4519 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4520 ** use the value in register P3 as the key. If cursor P1 refers
4521 ** to an SQL index, then P3 is the first in an array of P4 registers
4522 ** that are used as an unpacked index key.
4524 ** Reposition cursor P1 so that it points to the smallest entry that
4525 ** is greater than or equal to the key value. If there are no records
4526 ** greater than or equal to the key and P2 is not zero, then jump to P2.
4528 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4529 ** opcode will either land on a record that exactly matches the key, or
4530 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4531 ** this opcode must be followed by an IdxLE opcode with the same arguments.
4532 ** The IdxGT opcode will be skipped if this opcode succeeds, but the
4533 ** IdxGT opcode will be used on subsequent loop iterations. The
4534 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4535 ** is an equality search.
4537 ** This opcode leaves the cursor configured to move in forward order,
4538 ** from the beginning toward the end. In other words, the cursor is
4539 ** configured to use Next, not Prev.
4541 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
4543 /* Opcode: SeekGT P1 P2 P3 P4 *
4544 ** Synopsis: key=r[P3@P4]
4546 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4547 ** use the value in register P3 as a key. If cursor P1 refers
4548 ** to an SQL index, then P3 is the first in an array of P4 registers
4549 ** that are used as an unpacked index key.
4551 ** Reposition cursor P1 so that it points to the smallest entry that
4552 ** is greater than the key value. If there are no records greater than
4553 ** the key and P2 is not zero, then jump to P2.
4555 ** This opcode leaves the cursor configured to move in forward order,
4556 ** from the beginning toward the end. In other words, the cursor is
4557 ** configured to use Next, not Prev.
4559 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
4561 /* Opcode: SeekLT P1 P2 P3 P4 *
4562 ** Synopsis: key=r[P3@P4]
4564 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4565 ** use the value in register P3 as a key. If cursor P1 refers
4566 ** to an SQL index, then P3 is the first in an array of P4 registers
4567 ** that are used as an unpacked index key.
4569 ** Reposition cursor P1 so that it points to the largest entry that
4570 ** is less than the key value. If there are no records less than
4571 ** the key and P2 is not zero, then jump to P2.
4573 ** This opcode leaves the cursor configured to move in reverse order,
4574 ** from the end toward the beginning. In other words, the cursor is
4575 ** configured to use Prev, not Next.
4577 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
4579 /* Opcode: SeekLE P1 P2 P3 P4 *
4580 ** Synopsis: key=r[P3@P4]
4582 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4583 ** use the value in register P3 as a key. If cursor P1 refers
4584 ** to an SQL index, then P3 is the first in an array of P4 registers
4585 ** that are used as an unpacked index key.
4587 ** Reposition cursor P1 so that it points to the largest entry that
4588 ** is less than or equal to the key value. If there are no records
4589 ** less than or equal to the key and P2 is not zero, then jump to P2.
4591 ** This opcode leaves the cursor configured to move in reverse order,
4592 ** from the end toward the beginning. In other words, the cursor is
4593 ** configured to use Prev, not Next.
4595 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4596 ** opcode will either land on a record that exactly matches the key, or
4597 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4598 ** this opcode must be followed by an IdxLE opcode with the same arguments.
4599 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
4600 ** IdxGE opcode will be used on subsequent loop iterations. The
4601 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4602 ** is an equality search.
4604 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
4606 case OP_SeekLT
: /* jump, in3, group, ncycle */
4607 case OP_SeekLE
: /* jump, in3, group, ncycle */
4608 case OP_SeekGE
: /* jump, in3, group, ncycle */
4609 case OP_SeekGT
: { /* jump, in3, group, ncycle */
4610 int res
; /* Comparison result */
4611 int oc
; /* Opcode */
4612 VdbeCursor
*pC
; /* The cursor to seek */
4613 UnpackedRecord r
; /* The key to seek for */
4614 int nField
; /* Number of columns or fields in the key */
4615 i64 iKey
; /* The rowid we are to seek to */
4616 int eqOnly
; /* Only interested in == results */
4618 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4619 assert( pOp
->p2
!=0 );
4620 pC
= p
->apCsr
[pOp
->p1
];
4622 assert( pC
->eCurType
==CURTYPE_BTREE
);
4623 assert( OP_SeekLE
== OP_SeekLT
+1 );
4624 assert( OP_SeekGE
== OP_SeekLT
+2 );
4625 assert( OP_SeekGT
== OP_SeekLT
+3 );
4626 assert( pC
->isOrdered
);
4627 assert( pC
->uc
.pCursor
!=0 );
4632 pC
->seekOp
= pOp
->opcode
;
4635 pC
->deferredMoveto
= 0;
4636 pC
->cacheStatus
= CACHE_STALE
;
4638 u16 flags3
, newType
;
4639 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
4640 assert( sqlite3BtreeCursorHasHint(pC
->uc
.pCursor
, BTREE_SEEK_EQ
)==0
4643 /* The input value in P3 might be of any type: integer, real, string,
4644 ** blob, or NULL. But it needs to be an integer before we can do
4645 ** the seek, so convert it. */
4646 pIn3
= &aMem
[pOp
->p3
];
4647 flags3
= pIn3
->flags
;
4648 if( (flags3
& (MEM_Int
|MEM_Real
|MEM_IntReal
|MEM_Str
))==MEM_Str
){
4649 applyNumericAffinity(pIn3
, 0);
4651 iKey
= sqlite3VdbeIntValue(pIn3
); /* Get the integer key value */
4652 newType
= pIn3
->flags
; /* Record the type after applying numeric affinity */
4653 pIn3
->flags
= flags3
; /* But convert the type back to its original */
4655 /* If the P3 value could not be converted into an integer without
4656 ** loss of information, then special processing is required... */
4657 if( (newType
& (MEM_Int
|MEM_IntReal
))==0 ){
4659 if( (newType
& MEM_Real
)==0 ){
4660 if( (newType
& MEM_Null
) || oc
>=OP_SeekGE
){
4661 VdbeBranchTaken(1,2);
4664 rc
= sqlite3BtreeLast(pC
->uc
.pCursor
, &res
);
4665 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
4666 goto seek_not_found
;
4669 c
= sqlite3IntFloatCompare(iKey
, pIn3
->u
.r
);
4671 /* If the approximation iKey is larger than the actual real search
4672 ** term, substitute >= for > and < for <=. e.g. if the search term
4673 ** is 4.9 and the integer approximation 5:
4675 ** (x > 4.9) -> (x >= 5)
4676 ** (x <= 4.9) -> (x < 5)
4679 assert( OP_SeekGE
==(OP_SeekGT
-1) );
4680 assert( OP_SeekLT
==(OP_SeekLE
-1) );
4681 assert( (OP_SeekLE
& 0x0001)==(OP_SeekGT
& 0x0001) );
4682 if( (oc
& 0x0001)==(OP_SeekGT
& 0x0001) ) oc
--;
4685 /* If the approximation iKey is smaller than the actual real search
4686 ** term, substitute <= for < and > for >=. */
4688 assert( OP_SeekLE
==(OP_SeekLT
+1) );
4689 assert( OP_SeekGT
==(OP_SeekGE
+1) );
4690 assert( (OP_SeekLT
& 0x0001)==(OP_SeekGE
& 0x0001) );
4691 if( (oc
& 0x0001)==(OP_SeekLT
& 0x0001) ) oc
++;
4694 rc
= sqlite3BtreeTableMoveto(pC
->uc
.pCursor
, (u64
)iKey
, 0, &res
);
4695 pC
->movetoTarget
= iKey
; /* Used by OP_Delete */
4696 if( rc
!=SQLITE_OK
){
4697 goto abort_due_to_error
;
4700 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4701 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4702 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4703 ** with the same key.
4705 if( sqlite3BtreeCursorHasHint(pC
->uc
.pCursor
, BTREE_SEEK_EQ
) ){
4707 assert( pOp
->opcode
==OP_SeekGE
|| pOp
->opcode
==OP_SeekLE
);
4708 assert( pOp
[1].opcode
==OP_IdxLT
|| pOp
[1].opcode
==OP_IdxGT
);
4709 assert( pOp
->opcode
==OP_SeekGE
|| pOp
[1].opcode
==OP_IdxLT
);
4710 assert( pOp
->opcode
==OP_SeekLE
|| pOp
[1].opcode
==OP_IdxGT
);
4711 assert( pOp
[1].p1
==pOp
[0].p1
);
4712 assert( pOp
[1].p2
==pOp
[0].p2
);
4713 assert( pOp
[1].p3
==pOp
[0].p3
);
4714 assert( pOp
[1].p4
.i
==pOp
[0].p4
.i
);
4718 assert( pOp
->p4type
==P4_INT32
);
4720 r
.pKeyInfo
= pC
->pKeyInfo
;
4721 r
.nField
= (u16
)nField
;
4723 /* The next line of code computes as follows, only faster:
4724 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
4725 ** r.default_rc = -1;
4727 ** r.default_rc = +1;
4730 r
.default_rc
= ((1 & (oc
- OP_SeekLT
)) ? -1 : +1);
4731 assert( oc
!=OP_SeekGT
|| r
.default_rc
==-1 );
4732 assert( oc
!=OP_SeekLE
|| r
.default_rc
==-1 );
4733 assert( oc
!=OP_SeekGE
|| r
.default_rc
==+1 );
4734 assert( oc
!=OP_SeekLT
|| r
.default_rc
==+1 );
4736 r
.aMem
= &aMem
[pOp
->p3
];
4740 for(i
=0; i
<r
.nField
; i
++){
4741 assert( memIsValid(&r
.aMem
[i
]) );
4742 if( i
>0 ) REGISTER_TRACE(pOp
->p3
+i
, &r
.aMem
[i
]);
4747 rc
= sqlite3BtreeIndexMoveto(pC
->uc
.pCursor
, &r
, &res
);
4748 if( rc
!=SQLITE_OK
){
4749 goto abort_due_to_error
;
4751 if( eqOnly
&& r
.eqSeen
==0 ){
4753 goto seek_not_found
;
4757 sqlite3_search_count
++;
4759 if( oc
>=OP_SeekGE
){ assert( oc
==OP_SeekGE
|| oc
==OP_SeekGT
);
4760 if( res
<0 || (res
==0 && oc
==OP_SeekGT
) ){
4762 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, 0);
4763 if( rc
!=SQLITE_OK
){
4764 if( rc
==SQLITE_DONE
){
4768 goto abort_due_to_error
;
4775 assert( oc
==OP_SeekLT
|| oc
==OP_SeekLE
);
4776 if( res
>0 || (res
==0 && oc
==OP_SeekLT
) ){
4778 rc
= sqlite3BtreePrevious(pC
->uc
.pCursor
, 0);
4779 if( rc
!=SQLITE_OK
){
4780 if( rc
==SQLITE_DONE
){
4784 goto abort_due_to_error
;
4788 /* res might be negative because the table is empty. Check to
4789 ** see if this is the case.
4791 res
= sqlite3BtreeEof(pC
->uc
.pCursor
);
4795 assert( pOp
->p2
>0 );
4796 VdbeBranchTaken(res
!=0,2);
4800 assert( pOp
[1].opcode
==OP_IdxLT
|| pOp
[1].opcode
==OP_IdxGT
);
4801 pOp
++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
4807 /* Opcode: SeekScan P1 P2 * * P5
4808 ** Synopsis: Scan-ahead up to P1 rows
4810 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this
4811 ** opcode must be immediately followed by OP_SeekGE. This constraint is
4812 ** checked by assert() statements.
4814 ** This opcode uses the P1 through P4 operands of the subsequent
4815 ** OP_SeekGE. In the text that follows, the operands of the subsequent
4816 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
4817 ** the P1, P2 and P5 operands of this opcode are also used, and are called
4818 ** This.P1, This.P2 and This.P5.
4820 ** This opcode helps to optimize IN operators on a multi-column index
4821 ** where the IN operator is on the later terms of the index by avoiding
4822 ** unnecessary seeks on the btree, substituting steps to the next row
4823 ** of the b-tree instead. A correct answer is obtained if this opcode
4824 ** is omitted or is a no-op.
4826 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4827 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
4828 ** to. Call this SeekGE.P3/P4 row the "target".
4830 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4831 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
4833 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4834 ** might be the target row, or it might be near and slightly before the
4835 ** target row, or it might be after the target row. If the cursor is
4836 ** currently before the target row, then this opcode attempts to position
4837 ** the cursor on or after the target row by invoking sqlite3BtreeStep()
4838 ** on the cursor between 1 and This.P1 times.
4840 ** The This.P5 parameter is a flag that indicates what to do if the
4841 ** cursor ends up pointing at a valid row that is past the target
4842 ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
4843 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
4844 ** case occurs when there are no inequality constraints to the right of
4845 ** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
4846 ** occurs when there are inequality constraints to the right of the IN
4847 ** operator. In that case, the This.P2 will point either directly to or
4848 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
4851 ** Possible outcomes from this opcode:<ol>
4853 ** <li> If the cursor is initally not pointed to any valid row, then
4854 ** fall through into the subsequent OP_SeekGE opcode.
4856 ** <li> If the cursor is left pointing to a row that is before the target
4857 ** row, even after making as many as This.P1 calls to
4858 ** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
4860 ** <li> If the cursor is left pointing at the target row, either because it
4861 ** was at the target row to begin with or because one or more
4862 ** sqlite3BtreeNext() calls moved the cursor to the target row,
4863 ** then jump to This.P2..,
4865 ** <li> If the cursor started out before the target row and a call to
4866 ** to sqlite3BtreeNext() moved the cursor off the end of the index
4867 ** (indicating that the target row definitely does not exist in the
4868 ** btree) then jump to SeekGE.P2, ending the loop.
4870 ** <li> If the cursor ends up on a valid row that is past the target row
4871 ** (indicating that the target row does not exist in the btree) then
4872 ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
4875 case OP_SeekScan
: { /* ncycle */
4881 assert( pOp
[1].opcode
==OP_SeekGE
);
4883 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
4884 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
4885 ** opcode past the OP_SeekGE itself. */
4886 assert( pOp
->p2
>=(int)(pOp
-aOp
)+2 );
4889 /* There are no inequality constraints following the IN constraint. */
4890 assert( pOp
[1].p1
==aOp
[pOp
->p2
-1].p1
);
4891 assert( pOp
[1].p2
==aOp
[pOp
->p2
-1].p2
);
4892 assert( pOp
[1].p3
==aOp
[pOp
->p2
-1].p3
);
4893 assert( aOp
[pOp
->p2
-1].opcode
==OP_IdxGT
4894 || aOp
[pOp
->p2
-1].opcode
==OP_IdxGE
);
4895 testcase( aOp
[pOp
->p2
-1].opcode
==OP_IdxGE
);
4897 /* There are inequality constraints. */
4898 assert( pOp
->p2
==(int)(pOp
-aOp
)+2 );
4899 assert( aOp
[pOp
->p2
-1].opcode
==OP_SeekGE
);
4903 assert( pOp
->p1
>0 );
4904 pC
= p
->apCsr
[pOp
[1].p1
];
4906 assert( pC
->eCurType
==CURTYPE_BTREE
);
4907 assert( !pC
->isTable
);
4908 if( !sqlite3BtreeCursorIsValidNN(pC
->uc
.pCursor
) ){
4910 if( db
->flags
&SQLITE_VdbeTrace
){
4911 printf("... cursor not valid - fall through\n");
4918 r
.pKeyInfo
= pC
->pKeyInfo
;
4919 r
.nField
= (u16
)pOp
[1].p4
.i
;
4921 r
.aMem
= &aMem
[pOp
[1].p3
];
4925 for(i
=0; i
<r
.nField
; i
++){
4926 assert( memIsValid(&r
.aMem
[i
]) );
4927 REGISTER_TRACE(pOp
[1].p3
+i
, &aMem
[pOp
[1].p3
+i
]);
4931 res
= 0; /* Not needed. Only used to silence a warning. */
4933 rc
= sqlite3VdbeIdxKeyCompare(db
, pC
, &r
, &res
);
4934 if( rc
) goto abort_due_to_error
;
4935 if( res
>0 && pOp
->p5
==0 ){
4936 seekscan_search_fail
:
4937 /* Jump to SeekGE.P2, ending the loop */
4939 if( db
->flags
&SQLITE_VdbeTrace
){
4940 printf("... %d steps and then skip\n", pOp
->p1
- nStep
);
4943 VdbeBranchTaken(1,3);
4948 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
4950 if( db
->flags
&SQLITE_VdbeTrace
){
4951 printf("... %d steps and then success\n", pOp
->p1
- nStep
);
4954 VdbeBranchTaken(2,3);
4960 if( db
->flags
&SQLITE_VdbeTrace
){
4961 printf("... fall through after %d steps\n", pOp
->p1
);
4964 VdbeBranchTaken(0,3);
4968 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, 0);
4970 if( rc
==SQLITE_DONE
){
4972 goto seekscan_search_fail
;
4974 goto abort_due_to_error
;
4983 /* Opcode: SeekHit P1 P2 P3 * *
4984 ** Synopsis: set P2<=seekHit<=P3
4986 ** Increase or decrease the seekHit value for cursor P1, if necessary,
4987 ** so that it is no less than P2 and no greater than P3.
4989 ** The seekHit integer represents the maximum of terms in an index for which
4990 ** there is known to be at least one match. If the seekHit value is smaller
4991 ** than the total number of equality terms in an index lookup, then the
4992 ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4993 ** early, thus saving work. This is part of the IN-early-out optimization.
4995 ** P1 must be a valid b-tree cursor.
4997 case OP_SeekHit
: { /* ncycle */
4999 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5000 pC
= p
->apCsr
[pOp
->p1
];
5002 assert( pOp
->p3
>=pOp
->p2
);
5003 if( pC
->seekHit
<pOp
->p2
){
5005 if( db
->flags
&SQLITE_VdbeTrace
){
5006 printf("seekHit changes from %d to %d\n", pC
->seekHit
, pOp
->p2
);
5009 pC
->seekHit
= pOp
->p2
;
5010 }else if( pC
->seekHit
>pOp
->p3
){
5012 if( db
->flags
&SQLITE_VdbeTrace
){
5013 printf("seekHit changes from %d to %d\n", pC
->seekHit
, pOp
->p3
);
5016 pC
->seekHit
= pOp
->p3
;
5021 /* Opcode: IfNotOpen P1 P2 * * *
5022 ** Synopsis: if( !csr[P1] ) goto P2
5024 ** If cursor P1 is not open or if P1 is set to a NULL row using the
5025 ** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
5027 case OP_IfNotOpen
: { /* jump */
5030 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5031 pCur
= p
->apCsr
[pOp
->p1
];
5032 VdbeBranchTaken(pCur
==0 || pCur
->nullRow
, 2);
5033 if( pCur
==0 || pCur
->nullRow
){
5034 goto jump_to_p2_and_check_for_interrupt
;
5039 /* Opcode: Found P1 P2 P3 P4 *
5040 ** Synopsis: key=r[P3@P4]
5042 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5043 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
5046 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
5047 ** is a prefix of any entry in P1 then a jump is made to P2 and
5048 ** P1 is left pointing at the matching entry.
5050 ** This operation leaves the cursor in a state where it can be
5051 ** advanced in the forward direction. The Next instruction will work,
5052 ** but not the Prev instruction.
5054 ** See also: NotFound, NoConflict, NotExists. SeekGe
5056 /* Opcode: NotFound P1 P2 P3 P4 *
5057 ** Synopsis: key=r[P3@P4]
5059 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5060 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
5063 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
5064 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
5065 ** does contain an entry whose prefix matches the P3/P4 record then control
5066 ** falls through to the next instruction and P1 is left pointing at the
5069 ** This operation leaves the cursor in a state where it cannot be
5070 ** advanced in either direction. In other words, the Next and Prev
5071 ** opcodes do not work after this operation.
5073 ** See also: Found, NotExists, NoConflict, IfNoHope
5075 /* Opcode: IfNoHope P1 P2 P3 P4 *
5076 ** Synopsis: key=r[P3@P4]
5078 ** Register P3 is the first of P4 registers that form an unpacked
5079 ** record. Cursor P1 is an index btree. P2 is a jump destination.
5080 ** In other words, the operands to this opcode are the same as the
5081 ** operands to OP_NotFound and OP_IdxGT.
5083 ** This opcode is an optimization attempt only. If this opcode always
5084 ** falls through, the correct answer is still obtained, but extra works
5087 ** A value of N in the seekHit flag of cursor P1 means that there exists
5088 ** a key P3:N that will match some record in the index. We want to know
5089 ** if it is possible for a record P3:P4 to match some record in the
5090 ** index. If it is not possible, we can skips some work. So if seekHit
5091 ** is less than P4, attempt to find out if a match is possible by running
5094 ** This opcode is used in IN clause processing for a multi-column key.
5095 ** If an IN clause is attached to an element of the key other than the
5096 ** left-most element, and if there are no matches on the most recent
5097 ** seek over the whole key, then it might be that one of the key element
5098 ** to the left is prohibiting a match, and hence there is "no hope" of
5099 ** any match regardless of how many IN clause elements are checked.
5100 ** In such a case, we abandon the IN clause search early, using this
5101 ** opcode. The opcode name comes from the fact that the
5102 ** jump is taken if there is "no hope" of achieving a match.
5104 ** See also: NotFound, SeekHit
5106 /* Opcode: NoConflict P1 P2 P3 P4 *
5107 ** Synopsis: key=r[P3@P4]
5109 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5110 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
5113 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
5114 ** contains any NULL value, jump immediately to P2. If all terms of the
5115 ** record are not-NULL then a check is done to determine if any row in the
5116 ** P1 index btree has a matching key prefix. If there are no matches, jump
5117 ** immediately to P2. If there is a match, fall through and leave the P1
5118 ** cursor pointing to the matching row.
5120 ** This opcode is similar to OP_NotFound with the exceptions that the
5121 ** branch is always taken if any part of the search key input is NULL.
5123 ** This operation leaves the cursor in a state where it cannot be
5124 ** advanced in either direction. In other words, the Next and Prev
5125 ** opcodes do not work after this operation.
5127 ** See also: NotFound, Found, NotExists
5129 case OP_IfNoHope
: { /* jump, in3, ncycle */
5131 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5132 pC
= p
->apCsr
[pOp
->p1
];
5135 if( db
->flags
&SQLITE_VdbeTrace
){
5136 printf("seekHit is %d\n", pC
->seekHit
);
5139 if( pC
->seekHit
>=pOp
->p4
.i
) break;
5140 /* Fall through into OP_NotFound */
5141 /* no break */ deliberate_fall_through
5143 case OP_NoConflict
: /* jump, in3, ncycle */
5144 case OP_NotFound
: /* jump, in3, ncycle */
5145 case OP_Found
: { /* jump, in3, ncycle */
5149 UnpackedRecord
*pIdxKey
;
5153 if( pOp
->opcode
!=OP_NoConflict
) sqlite3_found_count
++;
5156 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5157 assert( pOp
->p4type
==P4_INT32
);
5158 pC
= p
->apCsr
[pOp
->p1
];
5161 pC
->seekOp
= pOp
->opcode
;
5163 r
.aMem
= &aMem
[pOp
->p3
];
5164 assert( pC
->eCurType
==CURTYPE_BTREE
);
5165 assert( pC
->uc
.pCursor
!=0 );
5166 assert( pC
->isTable
==0 );
5167 r
.nField
= (u16
)pOp
->p4
.i
;
5169 /* Key values in an array of registers */
5170 r
.pKeyInfo
= pC
->pKeyInfo
;
5173 for(ii
=0; ii
<r
.nField
; ii
++){
5174 assert( memIsValid(&r
.aMem
[ii
]) );
5175 assert( (r
.aMem
[ii
].flags
& MEM_Zero
)==0 || r
.aMem
[ii
].n
==0 );
5176 if( ii
) REGISTER_TRACE(pOp
->p3
+ii
, &r
.aMem
[ii
]);
5179 rc
= sqlite3BtreeIndexMoveto(pC
->uc
.pCursor
, &r
, &pC
->seekResult
);
5181 /* Composite key generated by OP_MakeRecord */
5182 assert( r
.aMem
->flags
& MEM_Blob
);
5183 assert( pOp
->opcode
!=OP_NoConflict
);
5184 rc
= ExpandBlob(r
.aMem
);
5185 assert( rc
==SQLITE_OK
|| rc
==SQLITE_NOMEM
);
5186 if( rc
) goto no_mem
;
5187 pIdxKey
= sqlite3VdbeAllocUnpackedRecord(pC
->pKeyInfo
);
5188 if( pIdxKey
==0 ) goto no_mem
;
5189 sqlite3VdbeRecordUnpack(pC
->pKeyInfo
, r
.aMem
->n
, r
.aMem
->z
, pIdxKey
);
5190 pIdxKey
->default_rc
= 0;
5191 rc
= sqlite3BtreeIndexMoveto(pC
->uc
.pCursor
, pIdxKey
, &pC
->seekResult
);
5192 sqlite3DbFreeNN(db
, pIdxKey
);
5194 if( rc
!=SQLITE_OK
){
5195 goto abort_due_to_error
;
5197 alreadyExists
= (pC
->seekResult
==0);
5198 pC
->nullRow
= 1-alreadyExists
;
5199 pC
->deferredMoveto
= 0;
5200 pC
->cacheStatus
= CACHE_STALE
;
5201 if( pOp
->opcode
==OP_Found
){
5202 VdbeBranchTaken(alreadyExists
!=0,2);
5203 if( alreadyExists
) goto jump_to_p2
;
5205 if( !alreadyExists
){
5206 VdbeBranchTaken(1,2);
5209 if( pOp
->opcode
==OP_NoConflict
){
5210 /* For the OP_NoConflict opcode, take the jump if any of the
5211 ** input fields are NULL, since any key with a NULL will not
5213 for(ii
=0; ii
<r
.nField
; ii
++){
5214 if( r
.aMem
[ii
].flags
& MEM_Null
){
5215 VdbeBranchTaken(1,2);
5220 VdbeBranchTaken(0,2);
5221 if( pOp
->opcode
==OP_IfNoHope
){
5222 pC
->seekHit
= pOp
->p4
.i
;
5228 /* Opcode: SeekRowid P1 P2 P3 * *
5229 ** Synopsis: intkey=r[P3]
5231 ** P1 is the index of a cursor open on an SQL table btree (with integer
5232 ** keys). If register P3 does not contain an integer or if P1 does not
5233 ** contain a record with rowid P3 then jump immediately to P2.
5234 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
5235 ** a record with rowid P3 then
5236 ** leave the cursor pointing at that record and fall through to the next
5239 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists
5240 ** the P3 register must be guaranteed to contain an integer value. With this
5241 ** opcode, register P3 might not contain an integer.
5243 ** The OP_NotFound opcode performs the same operation on index btrees
5244 ** (with arbitrary multi-value keys).
5246 ** This opcode leaves the cursor in a state where it cannot be advanced
5247 ** in either direction. In other words, the Next and Prev opcodes will
5248 ** not work following this opcode.
5250 ** See also: Found, NotFound, NoConflict, SeekRowid
5252 /* Opcode: NotExists P1 P2 P3 * *
5253 ** Synopsis: intkey=r[P3]
5255 ** P1 is the index of a cursor open on an SQL table btree (with integer
5256 ** keys). P3 is an integer rowid. If P1 does not contain a record with
5257 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
5258 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
5259 ** leave the cursor pointing at that record and fall through to the next
5262 ** The OP_SeekRowid opcode performs the same operation but also allows the
5263 ** P3 register to contain a non-integer value, in which case the jump is
5264 ** always taken. This opcode requires that P3 always contain an integer.
5266 ** The OP_NotFound opcode performs the same operation on index btrees
5267 ** (with arbitrary multi-value keys).
5269 ** This opcode leaves the cursor in a state where it cannot be advanced
5270 ** in either direction. In other words, the Next and Prev opcodes will
5271 ** not work following this opcode.
5273 ** See also: Found, NotFound, NoConflict, SeekRowid
5275 case OP_SeekRowid
: { /* jump, in3, ncycle */
5281 pIn3
= &aMem
[pOp
->p3
];
5282 testcase( pIn3
->flags
& MEM_Int
);
5283 testcase( pIn3
->flags
& MEM_IntReal
);
5284 testcase( pIn3
->flags
& MEM_Real
);
5285 testcase( (pIn3
->flags
& (MEM_Str
|MEM_Int
))==MEM_Str
);
5286 if( (pIn3
->flags
& (MEM_Int
|MEM_IntReal
))==0 ){
5287 /* If pIn3->u.i does not contain an integer, compute iKey as the
5288 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
5289 ** into an integer without loss of information. Take care to avoid
5290 ** changing the datatype of pIn3, however, as it is used by other
5291 ** parts of the prepared statement. */
5293 applyAffinity(&x
, SQLITE_AFF_NUMERIC
, encoding
);
5294 if( (x
.flags
& MEM_Int
)==0 ) goto jump_to_p2
;
5296 goto notExistsWithKey
;
5298 /* Fall through into OP_NotExists */
5299 /* no break */ deliberate_fall_through
5300 case OP_NotExists
: /* jump, in3, ncycle */
5301 pIn3
= &aMem
[pOp
->p3
];
5302 assert( (pIn3
->flags
& MEM_Int
)!=0 || pOp
->opcode
==OP_SeekRowid
);
5303 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5306 pC
= p
->apCsr
[pOp
->p1
];
5309 if( pOp
->opcode
==OP_SeekRowid
) pC
->seekOp
= OP_SeekRowid
;
5311 assert( pC
->isTable
);
5312 assert( pC
->eCurType
==CURTYPE_BTREE
);
5313 pCrsr
= pC
->uc
.pCursor
;
5316 rc
= sqlite3BtreeTableMoveto(pCrsr
, iKey
, 0, &res
);
5317 assert( rc
==SQLITE_OK
|| res
==0 );
5318 pC
->movetoTarget
= iKey
; /* Used by OP_Delete */
5320 pC
->cacheStatus
= CACHE_STALE
;
5321 pC
->deferredMoveto
= 0;
5322 VdbeBranchTaken(res
!=0,2);
5323 pC
->seekResult
= res
;
5325 assert( rc
==SQLITE_OK
);
5327 rc
= SQLITE_CORRUPT_BKPT
;
5332 if( rc
) goto abort_due_to_error
;
5336 /* Opcode: Sequence P1 P2 * * *
5337 ** Synopsis: r[P2]=cursor[P1].ctr++
5339 ** Find the next available sequence number for cursor P1.
5340 ** Write the sequence number into register P2.
5341 ** The sequence number on the cursor is incremented after this
5344 case OP_Sequence
: { /* out2 */
5345 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5346 assert( p
->apCsr
[pOp
->p1
]!=0 );
5347 assert( p
->apCsr
[pOp
->p1
]->eCurType
!=CURTYPE_VTAB
);
5348 pOut
= out2Prerelease(p
, pOp
);
5349 pOut
->u
.i
= p
->apCsr
[pOp
->p1
]->seqCount
++;
5354 /* Opcode: NewRowid P1 P2 P3 * *
5355 ** Synopsis: r[P2]=rowid
5357 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
5358 ** The record number is not previously used as a key in the database
5359 ** table that cursor P1 points to. The new record number is written
5360 ** written to register P2.
5362 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
5363 ** the largest previously generated record number. No new record numbers are
5364 ** allowed to be less than this value. When this value reaches its maximum,
5365 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
5366 ** generated record number. This P3 mechanism is used to help implement the
5367 ** AUTOINCREMENT feature.
5369 case OP_NewRowid
: { /* out2 */
5370 i64 v
; /* The new rowid */
5371 VdbeCursor
*pC
; /* Cursor of table to get the new rowid */
5372 int res
; /* Result of an sqlite3BtreeLast() */
5373 int cnt
; /* Counter to limit the number of searches */
5374 #ifndef SQLITE_OMIT_AUTOINCREMENT
5375 Mem
*pMem
; /* Register holding largest rowid for AUTOINCREMENT */
5376 VdbeFrame
*pFrame
; /* Root frame of VDBE */
5381 pOut
= out2Prerelease(p
, pOp
);
5382 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5383 pC
= p
->apCsr
[pOp
->p1
];
5385 assert( pC
->isTable
);
5386 assert( pC
->eCurType
==CURTYPE_BTREE
);
5387 assert( pC
->uc
.pCursor
!=0 );
5389 /* The next rowid or record number (different terms for the same
5390 ** thing) is obtained in a two-step algorithm.
5392 ** First we attempt to find the largest existing rowid and add one
5393 ** to that. But if the largest existing rowid is already the maximum
5394 ** positive integer, we have to fall through to the second
5395 ** probabilistic algorithm
5397 ** The second algorithm is to select a rowid at random and see if
5398 ** it already exists in the table. If it does not exist, we have
5399 ** succeeded. If the random rowid does exist, we select a new one
5400 ** and try again, up to 100 times.
5402 assert( pC
->isTable
);
5404 #ifdef SQLITE_32BIT_ROWID
5405 # define MAX_ROWID 0x7fffffff
5407 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
5408 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
5409 ** to provide the constant while making all compilers happy.
5411 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
5414 if( !pC
->useRandomRowid
){
5415 rc
= sqlite3BtreeLast(pC
->uc
.pCursor
, &res
);
5416 if( rc
!=SQLITE_OK
){
5417 goto abort_due_to_error
;
5420 v
= 1; /* IMP: R-61914-48074 */
5422 assert( sqlite3BtreeCursorIsValid(pC
->uc
.pCursor
) );
5423 v
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5425 pC
->useRandomRowid
= 1;
5427 v
++; /* IMP: R-29538-34987 */
5432 #ifndef SQLITE_OMIT_AUTOINCREMENT
5434 /* Assert that P3 is a valid memory cell. */
5435 assert( pOp
->p3
>0 );
5437 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
5438 /* Assert that P3 is a valid memory cell. */
5439 assert( pOp
->p3
<=pFrame
->nMem
);
5440 pMem
= &pFrame
->aMem
[pOp
->p3
];
5442 /* Assert that P3 is a valid memory cell. */
5443 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
5444 pMem
= &aMem
[pOp
->p3
];
5445 memAboutToChange(p
, pMem
);
5447 assert( memIsValid(pMem
) );
5449 REGISTER_TRACE(pOp
->p3
, pMem
);
5450 sqlite3VdbeMemIntegerify(pMem
);
5451 assert( (pMem
->flags
& MEM_Int
)!=0 ); /* mem(P3) holds an integer */
5452 if( pMem
->u
.i
==MAX_ROWID
|| pC
->useRandomRowid
){
5453 rc
= SQLITE_FULL
; /* IMP: R-17817-00630 */
5454 goto abort_due_to_error
;
5456 if( v
<pMem
->u
.i
+1 ){
5462 if( pC
->useRandomRowid
){
5463 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
5464 ** largest possible integer (9223372036854775807) then the database
5465 ** engine starts picking positive candidate ROWIDs at random until
5466 ** it finds one that is not previously used. */
5467 assert( pOp
->p3
==0 ); /* We cannot be in random rowid mode if this is
5468 ** an AUTOINCREMENT table. */
5471 sqlite3_randomness(sizeof(v
), &v
);
5472 v
&= (MAX_ROWID
>>1); v
++; /* Ensure that v is greater than zero */
5473 }while( ((rc
= sqlite3BtreeTableMoveto(pC
->uc
.pCursor
, (u64
)v
,
5474 0, &res
))==SQLITE_OK
)
5477 if( rc
) goto abort_due_to_error
;
5479 rc
= SQLITE_FULL
; /* IMP: R-38219-53002 */
5480 goto abort_due_to_error
;
5482 assert( v
>0 ); /* EV: R-40812-03570 */
5484 pC
->deferredMoveto
= 0;
5485 pC
->cacheStatus
= CACHE_STALE
;
5491 /* Opcode: Insert P1 P2 P3 P4 P5
5492 ** Synopsis: intkey=r[P3] data=r[P2]
5494 ** Write an entry into the table of cursor P1. A new entry is
5495 ** created if it doesn't already exist or the data for an existing
5496 ** entry is overwritten. The data is the value MEM_Blob stored in register
5497 ** number P2. The key is stored in register P3. The key must
5500 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5501 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
5502 ** then rowid is stored for subsequent return by the
5503 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
5505 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5506 ** run faster by avoiding an unnecessary seek on cursor P1. However,
5507 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5508 ** seeks on the cursor or if the most recent seek used a key equal to P3.
5510 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5511 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5512 ** is part of an INSERT operation. The difference is only important to
5515 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
5516 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5517 ** following a successful insert.
5519 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5520 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
5521 ** and register P2 becomes ephemeral. If the cursor is changed, the
5522 ** value of register P2 will then change. Make sure this does not
5523 ** cause any problems.)
5525 ** This instruction only works on tables. The equivalent instruction
5526 ** for indices is OP_IdxInsert.
5529 Mem
*pData
; /* MEM cell holding data for the record to be inserted */
5530 Mem
*pKey
; /* MEM cell holding key for the record */
5531 VdbeCursor
*pC
; /* Cursor to table into which insert is written */
5532 int seekResult
; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5533 const char *zDb
; /* database name - used by the update hook */
5534 Table
*pTab
; /* Table structure - used by update and pre-update hooks */
5535 BtreePayload x
; /* Payload to be inserted */
5537 pData
= &aMem
[pOp
->p2
];
5538 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5539 assert( memIsValid(pData
) );
5540 pC
= p
->apCsr
[pOp
->p1
];
5542 assert( pC
->eCurType
==CURTYPE_BTREE
);
5543 assert( pC
->deferredMoveto
==0 );
5544 assert( pC
->uc
.pCursor
!=0 );
5545 assert( (pOp
->p5
& OPFLAG_ISNOOP
) || pC
->isTable
);
5546 assert( pOp
->p4type
==P4_TABLE
|| pOp
->p4type
>=P4_STATIC
);
5547 REGISTER_TRACE(pOp
->p2
, pData
);
5548 sqlite3VdbeIncrWriteCounter(p
, pC
);
5550 pKey
= &aMem
[pOp
->p3
];
5551 assert( pKey
->flags
& MEM_Int
);
5552 assert( memIsValid(pKey
) );
5553 REGISTER_TRACE(pOp
->p3
, pKey
);
5556 if( pOp
->p4type
==P4_TABLE
&& HAS_UPDATE_HOOK(db
) ){
5557 assert( pC
->iDb
>=0 );
5558 zDb
= db
->aDb
[pC
->iDb
].zDbSName
;
5559 pTab
= pOp
->p4
.pTab
;
5560 assert( (pOp
->p5
& OPFLAG_ISNOOP
) || HasRowid(pTab
) );
5566 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
5567 /* Invoke the pre-update hook, if any */
5569 if( db
->xPreUpdateCallback
&& !(pOp
->p5
& OPFLAG_ISUPDATE
) ){
5570 sqlite3VdbePreUpdateHook(p
,pC
,SQLITE_INSERT
,zDb
,pTab
,x
.nKey
,pOp
->p2
,-1);
5572 if( db
->xUpdateCallback
==0 || pTab
->aCol
==0 ){
5573 /* Prevent post-update hook from running in cases when it should not */
5577 if( pOp
->p5
& OPFLAG_ISNOOP
) break;
5580 assert( (pOp
->p5
& OPFLAG_LASTROWID
)==0 || (pOp
->p5
& OPFLAG_NCHANGE
)!=0 );
5581 if( pOp
->p5
& OPFLAG_NCHANGE
){
5583 if( pOp
->p5
& OPFLAG_LASTROWID
) db
->lastRowid
= x
.nKey
;
5585 assert( (pData
->flags
& (MEM_Blob
|MEM_Str
))!=0 || pData
->n
==0 );
5588 seekResult
= ((pOp
->p5
& OPFLAG_USESEEKRESULT
) ? pC
->seekResult
: 0);
5589 if( pData
->flags
& MEM_Zero
){
5590 x
.nZero
= pData
->u
.nZero
;
5595 assert( BTREE_PREFORMAT
==OPFLAG_PREFORMAT
);
5596 rc
= sqlite3BtreeInsert(pC
->uc
.pCursor
, &x
,
5597 (pOp
->p5
& (OPFLAG_APPEND
|OPFLAG_SAVEPOSITION
|OPFLAG_PREFORMAT
)),
5600 pC
->deferredMoveto
= 0;
5601 pC
->cacheStatus
= CACHE_STALE
;
5603 /* Invoke the update-hook if required. */
5604 if( rc
) goto abort_due_to_error
;
5606 assert( db
->xUpdateCallback
!=0 );
5607 assert( pTab
->aCol
!=0 );
5608 db
->xUpdateCallback(db
->pUpdateArg
,
5609 (pOp
->p5
& OPFLAG_ISUPDATE
) ? SQLITE_UPDATE
: SQLITE_INSERT
,
5610 zDb
, pTab
->zName
, x
.nKey
);
5615 /* Opcode: RowCell P1 P2 P3 * *
5617 ** P1 and P2 are both open cursors. Both must be opened on the same type
5618 ** of table - intkey or index. This opcode is used as part of copying
5619 ** the current row from P2 into P1. If the cursors are opened on intkey
5620 ** tables, register P3 contains the rowid to use with the new record in
5621 ** P1. If they are opened on index tables, P3 is not used.
5623 ** This opcode must be followed by either an Insert or InsertIdx opcode
5624 ** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
5627 VdbeCursor
*pDest
; /* Cursor to write to */
5628 VdbeCursor
*pSrc
; /* Cursor to read from */
5629 i64 iKey
; /* Rowid value to insert with */
5630 assert( pOp
[1].opcode
==OP_Insert
|| pOp
[1].opcode
==OP_IdxInsert
);
5631 assert( pOp
[1].opcode
==OP_Insert
|| pOp
->p3
==0 );
5632 assert( pOp
[1].opcode
==OP_IdxInsert
|| pOp
->p3
>0 );
5633 assert( pOp
[1].p5
& OPFLAG_PREFORMAT
);
5634 pDest
= p
->apCsr
[pOp
->p1
];
5635 pSrc
= p
->apCsr
[pOp
->p2
];
5636 iKey
= pOp
->p3
? aMem
[pOp
->p3
].u
.i
: 0;
5637 rc
= sqlite3BtreeTransferRow(pDest
->uc
.pCursor
, pSrc
->uc
.pCursor
, iKey
);
5638 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
5642 /* Opcode: Delete P1 P2 P3 P4 P5
5644 ** Delete the record at which the P1 cursor is currently pointing.
5646 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5647 ** the cursor will be left pointing at either the next or the previous
5648 ** record in the table. If it is left pointing at the next record, then
5649 ** the next Next instruction will be a no-op. As a result, in this case
5650 ** it is ok to delete a record from within a Next loop. If
5651 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5652 ** left in an undefined state.
5654 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5655 ** delete one of several associated with deleting a table row and all its
5656 ** associated index entries. Exactly one of those deletes is the "primary"
5657 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5658 ** marked with the AUXDELETE flag.
5660 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5661 ** change count is incremented (otherwise not).
5663 ** P1 must not be pseudo-table. It has to be a real table with
5666 ** If P4 is not NULL then it points to a Table object. In this case either
5667 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5668 ** have been positioned using OP_NotFound prior to invoking this opcode in
5669 ** this case. Specifically, if one is configured, the pre-update hook is
5670 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5671 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
5673 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5674 ** of the memory cell that contains the value that the rowid of the row will
5675 ** be set to by the update.
5684 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5685 pC
= p
->apCsr
[pOp
->p1
];
5687 assert( pC
->eCurType
==CURTYPE_BTREE
);
5688 assert( pC
->uc
.pCursor
!=0 );
5689 assert( pC
->deferredMoveto
==0 );
5690 sqlite3VdbeIncrWriteCounter(p
, pC
);
5693 if( pOp
->p4type
==P4_TABLE
5694 && HasRowid(pOp
->p4
.pTab
)
5696 && sqlite3BtreeCursorIsValidNN(pC
->uc
.pCursor
)
5698 /* If p5 is zero, the seek operation that positioned the cursor prior to
5699 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5700 ** the row that is being deleted */
5701 i64 iKey
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5702 assert( CORRUPT_DB
|| pC
->movetoTarget
==iKey
);
5706 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5707 ** the name of the db to pass as to it. Also set local pTab to a copy
5708 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5709 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5710 ** VdbeCursor.movetoTarget to the current rowid. */
5711 if( pOp
->p4type
==P4_TABLE
&& HAS_UPDATE_HOOK(db
) ){
5712 assert( pC
->iDb
>=0 );
5713 assert( pOp
->p4
.pTab
!=0 );
5714 zDb
= db
->aDb
[pC
->iDb
].zDbSName
;
5715 pTab
= pOp
->p4
.pTab
;
5716 if( (pOp
->p5
& OPFLAG_SAVEPOSITION
)!=0 && pC
->isTable
){
5717 pC
->movetoTarget
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5724 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
5725 /* Invoke the pre-update-hook if required. */
5726 assert( db
->xPreUpdateCallback
==0 || pTab
==pOp
->p4
.pTab
);
5727 if( db
->xPreUpdateCallback
&& pTab
){
5728 assert( !(opflags
& OPFLAG_ISUPDATE
)
5729 || HasRowid(pTab
)==0
5730 || (aMem
[pOp
->p3
].flags
& MEM_Int
)
5732 sqlite3VdbePreUpdateHook(p
, pC
,
5733 (opflags
& OPFLAG_ISUPDATE
) ? SQLITE_UPDATE
: SQLITE_DELETE
,
5734 zDb
, pTab
, pC
->movetoTarget
,
5738 if( opflags
& OPFLAG_ISNOOP
) break;
5741 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5742 assert( (pOp
->p5
& ~(OPFLAG_SAVEPOSITION
|OPFLAG_AUXDELETE
))==0 );
5743 assert( OPFLAG_SAVEPOSITION
==BTREE_SAVEPOSITION
);
5744 assert( OPFLAG_AUXDELETE
==BTREE_AUXDELETE
);
5748 if( pC
->isEphemeral
==0
5749 && (pOp
->p5
& OPFLAG_AUXDELETE
)==0
5750 && (pC
->wrFlag
& OPFLAG_FORDELETE
)==0
5754 if( pOp
->p2
& OPFLAG_NCHANGE
){
5760 rc
= sqlite3BtreeDelete(pC
->uc
.pCursor
, pOp
->p5
);
5761 pC
->cacheStatus
= CACHE_STALE
;
5763 if( rc
) goto abort_due_to_error
;
5765 /* Invoke the update-hook if required. */
5766 if( opflags
& OPFLAG_NCHANGE
){
5768 if( db
->xUpdateCallback
&& ALWAYS(pTab
!=0) && HasRowid(pTab
) ){
5769 db
->xUpdateCallback(db
->pUpdateArg
, SQLITE_DELETE
, zDb
, pTab
->zName
,
5771 assert( pC
->iDb
>=0 );
5777 /* Opcode: ResetCount * * * * *
5779 ** The value of the change counter is copied to the database handle
5780 ** change counter (returned by subsequent calls to sqlite3_changes()).
5781 ** Then the VMs internal change counter resets to 0.
5782 ** This is used by trigger programs.
5784 case OP_ResetCount
: {
5785 sqlite3VdbeSetChanges(db
, p
->nChange
);
5790 /* Opcode: SorterCompare P1 P2 P3 P4
5791 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
5793 ** P1 is a sorter cursor. This instruction compares a prefix of the
5794 ** record blob in register P3 against a prefix of the entry that
5795 ** the sorter cursor currently points to. Only the first P4 fields
5796 ** of r[P3] and the sorter record are compared.
5798 ** If either P3 or the sorter contains a NULL in one of their significant
5799 ** fields (not counting the P4 fields at the end which are ignored) then
5800 ** the comparison is assumed to be equal.
5802 ** Fall through to next instruction if the two records compare equal to
5803 ** each other. Jump to P2 if they are different.
5805 case OP_SorterCompare
: {
5810 pC
= p
->apCsr
[pOp
->p1
];
5811 assert( isSorter(pC
) );
5812 assert( pOp
->p4type
==P4_INT32
);
5813 pIn3
= &aMem
[pOp
->p3
];
5814 nKeyCol
= pOp
->p4
.i
;
5816 rc
= sqlite3VdbeSorterCompare(pC
, pIn3
, nKeyCol
, &res
);
5817 VdbeBranchTaken(res
!=0,2);
5818 if( rc
) goto abort_due_to_error
;
5819 if( res
) goto jump_to_p2
;
5823 /* Opcode: SorterData P1 P2 P3 * *
5824 ** Synopsis: r[P2]=data
5826 ** Write into register P2 the current sorter data for sorter cursor P1.
5827 ** Then clear the column header cache on cursor P3.
5829 ** This opcode is normally use to move a record out of the sorter and into
5830 ** a register that is the source for a pseudo-table cursor created using
5831 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
5832 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
5833 ** us from having to issue a separate NullRow instruction to clear that cache.
5835 case OP_SorterData
: {
5838 pOut
= &aMem
[pOp
->p2
];
5839 pC
= p
->apCsr
[pOp
->p1
];
5840 assert( isSorter(pC
) );
5841 rc
= sqlite3VdbeSorterRowkey(pC
, pOut
);
5842 assert( rc
!=SQLITE_OK
|| (pOut
->flags
& MEM_Blob
) );
5843 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5844 if( rc
) goto abort_due_to_error
;
5845 p
->apCsr
[pOp
->p3
]->cacheStatus
= CACHE_STALE
;
5849 /* Opcode: RowData P1 P2 P3 * *
5850 ** Synopsis: r[P2]=data
5852 ** Write into register P2 the complete row content for the row at
5853 ** which cursor P1 is currently pointing.
5854 ** There is no interpretation of the data.
5855 ** It is just copied onto the P2 register exactly as
5856 ** it is found in the database file.
5858 ** If cursor P1 is an index, then the content is the key of the row.
5859 ** If cursor P2 is a table, then the content extracted is the data.
5861 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
5862 ** of a real table, not a pseudo-table.
5864 ** If P3!=0 then this opcode is allowed to make an ephemeral pointer
5865 ** into the database page. That means that the content of the output
5866 ** register will be invalidated as soon as the cursor moves - including
5867 ** moves caused by other cursors that "save" the current cursors
5868 ** position in order that they can write to the same table. If P3==0
5869 ** then a copy of the data is made into memory. P3!=0 is faster, but
5872 ** If P3!=0 then the content of the P2 register is unsuitable for use
5873 ** in OP_Result and any OP_Result will invalidate the P2 register content.
5874 ** The P2 register content is invalidated by opcodes like OP_Function or
5875 ** by any use of another cursor pointing to the same table.
5882 pOut
= out2Prerelease(p
, pOp
);
5884 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5885 pC
= p
->apCsr
[pOp
->p1
];
5887 assert( pC
->eCurType
==CURTYPE_BTREE
);
5888 assert( isSorter(pC
)==0 );
5889 assert( pC
->nullRow
==0 );
5890 assert( pC
->uc
.pCursor
!=0 );
5891 pCrsr
= pC
->uc
.pCursor
;
5893 /* The OP_RowData opcodes always follow OP_NotExists or
5894 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5895 ** that might invalidate the cursor.
5896 ** If this where not the case, on of the following assert()s
5897 ** would fail. Should this ever change (because of changes in the code
5898 ** generator) then the fix would be to insert a call to
5899 ** sqlite3VdbeCursorMoveto().
5901 assert( pC
->deferredMoveto
==0 );
5902 assert( sqlite3BtreeCursorIsValid(pCrsr
) );
5904 n
= sqlite3BtreePayloadSize(pCrsr
);
5905 if( n
>(u32
)db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
5909 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pCrsr
, n
, pOut
);
5910 if( rc
) goto abort_due_to_error
;
5911 if( !pOp
->p3
) Deephemeralize(pOut
);
5912 UPDATE_MAX_BLOBSIZE(pOut
);
5913 REGISTER_TRACE(pOp
->p2
, pOut
);
5917 /* Opcode: Rowid P1 P2 * * *
5918 ** Synopsis: r[P2]=PX rowid of P1
5920 ** Store in register P2 an integer which is the key of the table entry that
5921 ** P1 is currently point to.
5923 ** P1 can be either an ordinary table or a virtual table. There used to
5924 ** be a separate OP_VRowid opcode for use with virtual tables, but this
5925 ** one opcode now works for both table types.
5927 case OP_Rowid
: { /* out2, ncycle */
5930 sqlite3_vtab
*pVtab
;
5931 const sqlite3_module
*pModule
;
5933 pOut
= out2Prerelease(p
, pOp
);
5934 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5935 pC
= p
->apCsr
[pOp
->p1
];
5937 assert( pC
->eCurType
!=CURTYPE_PSEUDO
|| pC
->nullRow
);
5939 pOut
->flags
= MEM_Null
;
5941 }else if( pC
->deferredMoveto
){
5942 v
= pC
->movetoTarget
;
5943 #ifndef SQLITE_OMIT_VIRTUALTABLE
5944 }else if( pC
->eCurType
==CURTYPE_VTAB
){
5945 assert( pC
->uc
.pVCur
!=0 );
5946 pVtab
= pC
->uc
.pVCur
->pVtab
;
5947 pModule
= pVtab
->pModule
;
5948 assert( pModule
->xRowid
);
5949 rc
= pModule
->xRowid(pC
->uc
.pVCur
, &v
);
5950 sqlite3VtabImportErrmsg(p
, pVtab
);
5951 if( rc
) goto abort_due_to_error
;
5952 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5954 assert( pC
->eCurType
==CURTYPE_BTREE
);
5955 assert( pC
->uc
.pCursor
!=0 );
5956 rc
= sqlite3VdbeCursorRestore(pC
);
5957 if( rc
) goto abort_due_to_error
;
5959 pOut
->flags
= MEM_Null
;
5962 v
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5968 /* Opcode: NullRow P1 * * * *
5970 ** Move the cursor P1 to a null row. Any OP_Column operations
5971 ** that occur while the cursor is on the null row will always
5974 ** If cursor P1 is not previously opened, open it now to a special
5975 ** pseudo-cursor that always returns NULL for every column.
5980 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5981 pC
= p
->apCsr
[pOp
->p1
];
5983 /* If the cursor is not already open, create a special kind of
5984 ** pseudo-cursor that always gives null rows. */
5985 pC
= allocateCursor(p
, pOp
->p1
, 1, CURTYPE_PSEUDO
);
5986 if( pC
==0 ) goto no_mem
;
5990 pC
->uc
.pCursor
= sqlite3BtreeFakeValidCursor();
5993 pC
->cacheStatus
= CACHE_STALE
;
5994 if( pC
->eCurType
==CURTYPE_BTREE
){
5995 assert( pC
->uc
.pCursor
!=0 );
5996 sqlite3BtreeClearCursor(pC
->uc
.pCursor
);
5999 if( pC
->seekOp
==0 ) pC
->seekOp
= OP_NullRow
;
6004 /* Opcode: SeekEnd P1 * * * *
6006 ** Position cursor P1 at the end of the btree for the purpose of
6007 ** appending a new entry onto the btree.
6009 ** It is assumed that the cursor is used only for appending and so
6010 ** if the cursor is valid, then the cursor must already be pointing
6011 ** at the end of the btree and so no changes are made to
6014 /* Opcode: Last P1 P2 * * *
6016 ** The next use of the Rowid or Column or Prev instruction for P1
6017 ** will refer to the last entry in the database table or index.
6018 ** If the table or index is empty and P2>0, then jump immediately to P2.
6019 ** If P2 is 0 or if the table or index is not empty, fall through
6020 ** to the following instruction.
6022 ** This opcode leaves the cursor configured to move in reverse order,
6023 ** from the end toward the beginning. In other words, the cursor is
6024 ** configured to use Prev, not Next.
6026 case OP_SeekEnd
: /* ncycle */
6027 case OP_Last
: { /* jump, ncycle */
6032 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6033 pC
= p
->apCsr
[pOp
->p1
];
6035 assert( pC
->eCurType
==CURTYPE_BTREE
);
6036 pCrsr
= pC
->uc
.pCursor
;
6040 pC
->seekOp
= pOp
->opcode
;
6042 if( pOp
->opcode
==OP_SeekEnd
){
6043 assert( pOp
->p2
==0 );
6044 pC
->seekResult
= -1;
6045 if( sqlite3BtreeCursorIsValidNN(pCrsr
) ){
6049 rc
= sqlite3BtreeLast(pCrsr
, &res
);
6050 pC
->nullRow
= (u8
)res
;
6051 pC
->deferredMoveto
= 0;
6052 pC
->cacheStatus
= CACHE_STALE
;
6053 if( rc
) goto abort_due_to_error
;
6055 VdbeBranchTaken(res
!=0,2);
6056 if( res
) goto jump_to_p2
;
6061 /* Opcode: IfSmaller P1 P2 P3 * *
6063 ** Estimate the number of rows in the table P1. Jump to P2 if that
6064 ** estimate is less than approximately 2**(0.1*P3).
6066 case OP_IfSmaller
: { /* jump */
6072 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6073 pC
= p
->apCsr
[pOp
->p1
];
6075 pCrsr
= pC
->uc
.pCursor
;
6077 rc
= sqlite3BtreeFirst(pCrsr
, &res
);
6078 if( rc
) goto abort_due_to_error
;
6080 sz
= sqlite3BtreeRowCountEst(pCrsr
);
6081 if( ALWAYS(sz
>=0) && sqlite3LogEst((u64
)sz
)<pOp
->p3
) res
= 1;
6083 VdbeBranchTaken(res
!=0,2);
6084 if( res
) goto jump_to_p2
;
6089 /* Opcode: SorterSort P1 P2 * * *
6091 ** After all records have been inserted into the Sorter object
6092 ** identified by P1, invoke this opcode to actually do the sorting.
6093 ** Jump to P2 if there are no records to be sorted.
6095 ** This opcode is an alias for OP_Sort and OP_Rewind that is used
6096 ** for Sorter objects.
6098 /* Opcode: Sort P1 P2 * * *
6100 ** This opcode does exactly the same thing as OP_Rewind except that
6101 ** it increments an undocumented global variable used for testing.
6103 ** Sorting is accomplished by writing records into a sorting index,
6104 ** then rewinding that index and playing it back from beginning to
6105 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
6106 ** rewinding so that the global variable will be incremented and
6107 ** regression tests can determine whether or not the optimizer is
6108 ** correctly optimizing out sorts.
6110 case OP_SorterSort
: /* jump */
6111 case OP_Sort
: { /* jump */
6113 sqlite3_sort_count
++;
6114 sqlite3_search_count
--;
6116 p
->aCounter
[SQLITE_STMTSTATUS_SORT
]++;
6117 /* Fall through into OP_Rewind */
6118 /* no break */ deliberate_fall_through
6120 /* Opcode: Rewind P1 P2 * * *
6122 ** The next use of the Rowid or Column or Next instruction for P1
6123 ** will refer to the first entry in the database table or index.
6124 ** If the table or index is empty, jump immediately to P2.
6125 ** If the table or index is not empty, fall through to the following
6128 ** If P2 is zero, that is an assertion that the P1 table is never
6129 ** empty and hence the jump will never be taken.
6131 ** This opcode leaves the cursor configured to move in forward order,
6132 ** from the beginning toward the end. In other words, the cursor is
6133 ** configured to use Next, not Prev.
6135 case OP_Rewind
: { /* jump, ncycle */
6140 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6141 assert( pOp
->p5
==0 );
6142 assert( pOp
->p2
>=0 && pOp
->p2
<p
->nOp
);
6144 pC
= p
->apCsr
[pOp
->p1
];
6146 assert( isSorter(pC
)==(pOp
->opcode
==OP_SorterSort
) );
6149 pC
->seekOp
= OP_Rewind
;
6152 rc
= sqlite3VdbeSorterRewind(pC
, &res
);
6154 assert( pC
->eCurType
==CURTYPE_BTREE
);
6155 pCrsr
= pC
->uc
.pCursor
;
6157 rc
= sqlite3BtreeFirst(pCrsr
, &res
);
6158 pC
->deferredMoveto
= 0;
6159 pC
->cacheStatus
= CACHE_STALE
;
6161 if( rc
) goto abort_due_to_error
;
6162 pC
->nullRow
= (u8
)res
;
6164 VdbeBranchTaken(res
!=0,2);
6165 if( res
) goto jump_to_p2
;
6170 /* Opcode: Next P1 P2 P3 * P5
6172 ** Advance cursor P1 so that it points to the next key/data pair in its
6173 ** table or index. If there are no more key/value pairs then fall through
6174 ** to the following instruction. But if the cursor advance was successful,
6175 ** jump immediately to P2.
6177 ** The Next opcode is only valid following an SeekGT, SeekGE, or
6178 ** OP_Rewind opcode used to position the cursor. Next is not allowed
6179 ** to follow SeekLT, SeekLE, or OP_Last.
6181 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
6182 ** been opened prior to this opcode or the program will segfault.
6184 ** The P3 value is a hint to the btree implementation. If P3==1, that
6185 ** means P1 is an SQL index and that this instruction could have been
6186 ** omitted if that index had been unique. P3 is usually 0. P3 is
6187 ** always either 0 or 1.
6189 ** If P5 is positive and the jump is taken, then event counter
6190 ** number P5-1 in the prepared statement is incremented.
6194 /* Opcode: Prev P1 P2 P3 * P5
6196 ** Back up cursor P1 so that it points to the previous key/data pair in its
6197 ** table or index. If there is no previous key/value pairs then fall through
6198 ** to the following instruction. But if the cursor backup was successful,
6199 ** jump immediately to P2.
6202 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
6203 ** OP_Last opcode used to position the cursor. Prev is not allowed
6204 ** to follow SeekGT, SeekGE, or OP_Rewind.
6206 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
6207 ** not open then the behavior is undefined.
6209 ** The P3 value is a hint to the btree implementation. If P3==1, that
6210 ** means P1 is an SQL index and that this instruction could have been
6211 ** omitted if that index had been unique. P3 is usually 0. P3 is
6212 ** always either 0 or 1.
6214 ** If P5 is positive and the jump is taken, then event counter
6215 ** number P5-1 in the prepared statement is incremented.
6217 /* Opcode: SorterNext P1 P2 * * P5
6219 ** This opcode works just like OP_Next except that P1 must be a
6220 ** sorter object for which the OP_SorterSort opcode has been
6221 ** invoked. This opcode advances the cursor to the next sorted
6222 ** record, or jumps to P2 if there are no more sorted records.
6224 case OP_SorterNext
: { /* jump */
6227 pC
= p
->apCsr
[pOp
->p1
];
6228 assert( isSorter(pC
) );
6229 rc
= sqlite3VdbeSorterNext(db
, pC
);
6232 case OP_Prev
: /* jump, ncycle */
6233 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6235 || pOp
->p5
==SQLITE_STMTSTATUS_FULLSCAN_STEP
6236 || pOp
->p5
==SQLITE_STMTSTATUS_AUTOINDEX
);
6237 pC
= p
->apCsr
[pOp
->p1
];
6239 assert( pC
->deferredMoveto
==0 );
6240 assert( pC
->eCurType
==CURTYPE_BTREE
);
6241 assert( pC
->seekOp
==OP_SeekLT
|| pC
->seekOp
==OP_SeekLE
6242 || pC
->seekOp
==OP_Last
|| pC
->seekOp
==OP_IfNoHope
6243 || pC
->seekOp
==OP_NullRow
);
6244 rc
= sqlite3BtreePrevious(pC
->uc
.pCursor
, pOp
->p3
);
6247 case OP_Next
: /* jump, ncycle */
6248 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6250 || pOp
->p5
==SQLITE_STMTSTATUS_FULLSCAN_STEP
6251 || pOp
->p5
==SQLITE_STMTSTATUS_AUTOINDEX
);
6252 pC
= p
->apCsr
[pOp
->p1
];
6254 assert( pC
->deferredMoveto
==0 );
6255 assert( pC
->eCurType
==CURTYPE_BTREE
);
6256 assert( pC
->seekOp
==OP_SeekGT
|| pC
->seekOp
==OP_SeekGE
6257 || pC
->seekOp
==OP_Rewind
|| pC
->seekOp
==OP_Found
6258 || pC
->seekOp
==OP_NullRow
|| pC
->seekOp
==OP_SeekRowid
6259 || pC
->seekOp
==OP_IfNoHope
);
6260 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, pOp
->p3
);
6263 pC
->cacheStatus
= CACHE_STALE
;
6264 VdbeBranchTaken(rc
==SQLITE_OK
,2);
6265 if( rc
==SQLITE_OK
){
6267 p
->aCounter
[pOp
->p5
]++;
6269 sqlite3_search_count
++;
6271 goto jump_to_p2_and_check_for_interrupt
;
6273 if( rc
!=SQLITE_DONE
) goto abort_due_to_error
;
6276 goto check_for_interrupt
;
6279 /* Opcode: IdxInsert P1 P2 P3 P4 P5
6280 ** Synopsis: key=r[P2]
6282 ** Register P2 holds an SQL index key made using the
6283 ** MakeRecord instructions. This opcode writes that key
6284 ** into the index P1. Data for the entry is nil.
6286 ** If P4 is not zero, then it is the number of values in the unpacked
6287 ** key of reg(P2). In that case, P3 is the index of the first register
6288 ** for the unpacked key. The availability of the unpacked key can sometimes
6289 ** be an optimization.
6291 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
6292 ** that this insert is likely to be an append.
6294 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
6295 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
6296 ** then the change counter is unchanged.
6298 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
6299 ** run faster by avoiding an unnecessary seek on cursor P1. However,
6300 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
6301 ** seeks on the cursor or if the most recent seek used a key equivalent
6304 ** This instruction only works for indices. The equivalent instruction
6305 ** for tables is OP_Insert.
6307 case OP_IdxInsert
: { /* in2 */
6311 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6312 pC
= p
->apCsr
[pOp
->p1
];
6313 sqlite3VdbeIncrWriteCounter(p
, pC
);
6315 assert( !isSorter(pC
) );
6316 pIn2
= &aMem
[pOp
->p2
];
6317 assert( (pIn2
->flags
& MEM_Blob
) || (pOp
->p5
& OPFLAG_PREFORMAT
) );
6318 if( pOp
->p5
& OPFLAG_NCHANGE
) p
->nChange
++;
6319 assert( pC
->eCurType
==CURTYPE_BTREE
);
6320 assert( pC
->isTable
==0 );
6321 rc
= ExpandBlob(pIn2
);
6322 if( rc
) goto abort_due_to_error
;
6325 x
.aMem
= aMem
+ pOp
->p3
;
6326 x
.nMem
= (u16
)pOp
->p4
.i
;
6327 rc
= sqlite3BtreeInsert(pC
->uc
.pCursor
, &x
,
6328 (pOp
->p5
& (OPFLAG_APPEND
|OPFLAG_SAVEPOSITION
|OPFLAG_PREFORMAT
)),
6329 ((pOp
->p5
& OPFLAG_USESEEKRESULT
) ? pC
->seekResult
: 0)
6331 assert( pC
->deferredMoveto
==0 );
6332 pC
->cacheStatus
= CACHE_STALE
;
6333 if( rc
) goto abort_due_to_error
;
6337 /* Opcode: SorterInsert P1 P2 * * *
6338 ** Synopsis: key=r[P2]
6340 ** Register P2 holds an SQL index key made using the
6341 ** MakeRecord instructions. This opcode writes that key
6342 ** into the sorter P1. Data for the entry is nil.
6344 case OP_SorterInsert
: { /* in2 */
6347 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6348 pC
= p
->apCsr
[pOp
->p1
];
6349 sqlite3VdbeIncrWriteCounter(p
, pC
);
6351 assert( isSorter(pC
) );
6352 pIn2
= &aMem
[pOp
->p2
];
6353 assert( pIn2
->flags
& MEM_Blob
);
6354 assert( pC
->isTable
==0 );
6355 rc
= ExpandBlob(pIn2
);
6356 if( rc
) goto abort_due_to_error
;
6357 rc
= sqlite3VdbeSorterWrite(pC
, pIn2
);
6358 if( rc
) goto abort_due_to_error
;
6362 /* Opcode: IdxDelete P1 P2 P3 * P5
6363 ** Synopsis: key=r[P2@P3]
6365 ** The content of P3 registers starting at register P2 form
6366 ** an unpacked index key. This opcode removes that entry from the
6367 ** index opened by cursor P1.
6369 ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
6370 ** if no matching index entry is found. This happens when running
6371 ** an UPDATE or DELETE statement and the index entry to be updated
6372 ** or deleted is not found. For some uses of IdxDelete
6373 ** (example: the EXCEPT operator) it does not matter that no matching
6374 ** entry is found. For those cases, P5 is zero. Also, do not raise
6375 ** this (self-correcting and non-critical) error if in writable_schema mode.
6377 case OP_IdxDelete
: {
6383 assert( pOp
->p3
>0 );
6384 assert( pOp
->p2
>0 && pOp
->p2
+pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
)+1 );
6385 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6386 pC
= p
->apCsr
[pOp
->p1
];
6388 assert( pC
->eCurType
==CURTYPE_BTREE
);
6389 sqlite3VdbeIncrWriteCounter(p
, pC
);
6390 pCrsr
= pC
->uc
.pCursor
;
6392 r
.pKeyInfo
= pC
->pKeyInfo
;
6393 r
.nField
= (u16
)pOp
->p3
;
6395 r
.aMem
= &aMem
[pOp
->p2
];
6396 rc
= sqlite3BtreeIndexMoveto(pCrsr
, &r
, &res
);
6397 if( rc
) goto abort_due_to_error
;
6399 rc
= sqlite3BtreeDelete(pCrsr
, BTREE_AUXDELETE
);
6400 if( rc
) goto abort_due_to_error
;
6401 }else if( pOp
->p5
&& !sqlite3WritableSchema(db
) ){
6402 rc
= sqlite3ReportError(SQLITE_CORRUPT_INDEX
, __LINE__
, "index corruption");
6403 goto abort_due_to_error
;
6405 assert( pC
->deferredMoveto
==0 );
6406 pC
->cacheStatus
= CACHE_STALE
;
6411 /* Opcode: DeferredSeek P1 * P3 P4 *
6412 ** Synopsis: Move P3 to P1.rowid if needed
6414 ** P1 is an open index cursor and P3 is a cursor on the corresponding
6415 ** table. This opcode does a deferred seek of the P3 table cursor
6416 ** to the row that corresponds to the current row of P1.
6418 ** This is a deferred seek. Nothing actually happens until
6419 ** the cursor is used to read a record. That way, if no reads
6420 ** occur, no unnecessary I/O happens.
6422 ** P4 may be an array of integers (type P4_INTARRAY) containing
6423 ** one entry for each column in the P3 table. If array entry a(i)
6424 ** is non-zero, then reading column a(i)-1 from cursor P3 is
6425 ** equivalent to performing the deferred seek and then reading column i
6426 ** from P1. This information is stored in P3 and used to redirect
6427 ** reads against P3 over to P1, thus possibly avoiding the need to
6428 ** seek and read cursor P3.
6430 /* Opcode: IdxRowid P1 P2 * * *
6431 ** Synopsis: r[P2]=rowid
6433 ** Write into register P2 an integer which is the last entry in the record at
6434 ** the end of the index key pointed to by cursor P1. This integer should be
6435 ** the rowid of the table entry to which this index entry points.
6437 ** See also: Rowid, MakeRecord.
6439 case OP_DeferredSeek
: /* ncycle */
6440 case OP_IdxRowid
: { /* out2, ncycle */
6441 VdbeCursor
*pC
; /* The P1 index cursor */
6442 VdbeCursor
*pTabCur
; /* The P2 table cursor (OP_DeferredSeek only) */
6443 i64 rowid
; /* Rowid that P1 current points to */
6445 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6446 pC
= p
->apCsr
[pOp
->p1
];
6448 assert( pC
->eCurType
==CURTYPE_BTREE
|| IsNullCursor(pC
) );
6449 assert( pC
->uc
.pCursor
!=0 );
6450 assert( pC
->isTable
==0 || IsNullCursor(pC
) );
6451 assert( pC
->deferredMoveto
==0 );
6452 assert( !pC
->nullRow
|| pOp
->opcode
==OP_IdxRowid
);
6454 /* The IdxRowid and Seek opcodes are combined because of the commonality
6455 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
6456 rc
= sqlite3VdbeCursorRestore(pC
);
6458 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
6459 ** since it was last positioned and an error (e.g. OOM or an IO error)
6460 ** occurs while trying to reposition it. */
6461 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
6464 rowid
= 0; /* Not needed. Only used to silence a warning. */
6465 rc
= sqlite3VdbeIdxRowid(db
, pC
->uc
.pCursor
, &rowid
);
6466 if( rc
!=SQLITE_OK
){
6467 goto abort_due_to_error
;
6469 if( pOp
->opcode
==OP_DeferredSeek
){
6470 assert( pOp
->p3
>=0 && pOp
->p3
<p
->nCursor
);
6471 pTabCur
= p
->apCsr
[pOp
->p3
];
6472 assert( pTabCur
!=0 );
6473 assert( pTabCur
->eCurType
==CURTYPE_BTREE
);
6474 assert( pTabCur
->uc
.pCursor
!=0 );
6475 assert( pTabCur
->isTable
);
6476 pTabCur
->nullRow
= 0;
6477 pTabCur
->movetoTarget
= rowid
;
6478 pTabCur
->deferredMoveto
= 1;
6479 pTabCur
->cacheStatus
= CACHE_STALE
;
6480 assert( pOp
->p4type
==P4_INTARRAY
|| pOp
->p4
.ai
==0 );
6481 assert( !pTabCur
->isEphemeral
);
6482 pTabCur
->ub
.aAltMap
= pOp
->p4
.ai
;
6483 assert( !pC
->isEphemeral
);
6484 pTabCur
->pAltCursor
= pC
;
6486 pOut
= out2Prerelease(p
, pOp
);
6490 assert( pOp
->opcode
==OP_IdxRowid
);
6491 sqlite3VdbeMemSetNull(&aMem
[pOp
->p2
]);
6496 /* Opcode: FinishSeek P1 * * * *
6498 ** If cursor P1 was previously moved via OP_DeferredSeek, complete that
6499 ** seek operation now, without further delay. If the cursor seek has
6500 ** already occurred, this instruction is a no-op.
6502 case OP_FinishSeek
: { /* ncycle */
6503 VdbeCursor
*pC
; /* The P1 index cursor */
6505 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6506 pC
= p
->apCsr
[pOp
->p1
];
6507 if( pC
->deferredMoveto
){
6508 rc
= sqlite3VdbeFinishMoveto(pC
);
6509 if( rc
) goto abort_due_to_error
;
6514 /* Opcode: IdxGE P1 P2 P3 P4 *
6515 ** Synopsis: key=r[P3@P4]
6517 ** The P4 register values beginning with P3 form an unpacked index
6518 ** key that omits the PRIMARY KEY. Compare this key value against the index
6519 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6520 ** fields at the end.
6522 ** If the P1 index entry is greater than or equal to the key value
6523 ** then jump to P2. Otherwise fall through to the next instruction.
6525 /* Opcode: IdxGT P1 P2 P3 P4 *
6526 ** Synopsis: key=r[P3@P4]
6528 ** The P4 register values beginning with P3 form an unpacked index
6529 ** key that omits the PRIMARY KEY. Compare this key value against the index
6530 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6531 ** fields at the end.
6533 ** If the P1 index entry is greater than the key value
6534 ** then jump to P2. Otherwise fall through to the next instruction.
6536 /* Opcode: IdxLT P1 P2 P3 P4 *
6537 ** Synopsis: key=r[P3@P4]
6539 ** The P4 register values beginning with P3 form an unpacked index
6540 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6541 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6542 ** ROWID on the P1 index.
6544 ** If the P1 index entry is less than the key value then jump to P2.
6545 ** Otherwise fall through to the next instruction.
6547 /* Opcode: IdxLE P1 P2 P3 P4 *
6548 ** Synopsis: key=r[P3@P4]
6550 ** The P4 register values beginning with P3 form an unpacked index
6551 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6552 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6553 ** ROWID on the P1 index.
6555 ** If the P1 index entry is less than or equal to the key value then jump
6556 ** to P2. Otherwise fall through to the next instruction.
6558 case OP_IdxLE
: /* jump, ncycle */
6559 case OP_IdxGT
: /* jump, ncycle */
6560 case OP_IdxLT
: /* jump, ncycle */
6561 case OP_IdxGE
: { /* jump, ncycle */
6566 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6567 pC
= p
->apCsr
[pOp
->p1
];
6569 assert( pC
->isOrdered
);
6570 assert( pC
->eCurType
==CURTYPE_BTREE
);
6571 assert( pC
->uc
.pCursor
!=0);
6572 assert( pC
->deferredMoveto
==0 );
6573 assert( pOp
->p4type
==P4_INT32
);
6574 r
.pKeyInfo
= pC
->pKeyInfo
;
6575 r
.nField
= (u16
)pOp
->p4
.i
;
6576 if( pOp
->opcode
<OP_IdxLT
){
6577 assert( pOp
->opcode
==OP_IdxLE
|| pOp
->opcode
==OP_IdxGT
);
6580 assert( pOp
->opcode
==OP_IdxGE
|| pOp
->opcode
==OP_IdxLT
);
6583 r
.aMem
= &aMem
[pOp
->p3
];
6587 for(i
=0; i
<r
.nField
; i
++){
6588 assert( memIsValid(&r
.aMem
[i
]) );
6589 REGISTER_TRACE(pOp
->p3
+i
, &aMem
[pOp
->p3
+i
]);
6594 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6600 assert( pC
->eCurType
==CURTYPE_BTREE
);
6601 pCur
= pC
->uc
.pCursor
;
6602 assert( sqlite3BtreeCursorIsValid(pCur
) );
6603 nCellKey
= sqlite3BtreePayloadSize(pCur
);
6604 /* nCellKey will always be between 0 and 0xffffffff because of the way
6605 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6606 if( nCellKey
<=0 || nCellKey
>0x7fffffff ){
6607 rc
= SQLITE_CORRUPT_BKPT
;
6608 goto abort_due_to_error
;
6610 sqlite3VdbeMemInit(&m
, db
, 0);
6611 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pCur
, (u32
)nCellKey
, &m
);
6612 if( rc
) goto abort_due_to_error
;
6613 res
= sqlite3VdbeRecordCompareWithSkip(m
.n
, m
.z
, &r
, 0);
6614 sqlite3VdbeMemReleaseMalloc(&m
);
6616 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6618 assert( (OP_IdxLE
&1)==(OP_IdxLT
&1) && (OP_IdxGE
&1)==(OP_IdxGT
&1) );
6619 if( (pOp
->opcode
&1)==(OP_IdxLT
&1) ){
6620 assert( pOp
->opcode
==OP_IdxLE
|| pOp
->opcode
==OP_IdxLT
);
6623 assert( pOp
->opcode
==OP_IdxGE
|| pOp
->opcode
==OP_IdxGT
);
6626 VdbeBranchTaken(res
>0,2);
6627 assert( rc
==SQLITE_OK
);
6628 if( res
>0 ) goto jump_to_p2
;
6632 /* Opcode: Destroy P1 P2 P3 * *
6634 ** Delete an entire database table or index whose root page in the database
6635 ** file is given by P1.
6637 ** The table being destroyed is in the main database file if P3==0. If
6638 ** P3==1 then the table to be clear is in the auxiliary database file
6639 ** that is used to store tables create using CREATE TEMPORARY TABLE.
6641 ** If AUTOVACUUM is enabled then it is possible that another root page
6642 ** might be moved into the newly deleted root page in order to keep all
6643 ** root pages contiguous at the beginning of the database. The former
6644 ** value of the root page that moved - its value before the move occurred -
6645 ** is stored in register P2. If no page movement was required (because the
6646 ** table being dropped was already the last one in the database) then a
6647 ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6648 ** is stored in register P2.
6650 ** This opcode throws an error if there are any active reader VMs when
6651 ** it is invoked. This is done to avoid the difficulty associated with
6652 ** updating existing cursors when a root page is moved in an AUTOVACUUM
6653 ** database. This error is thrown even if the database is not an AUTOVACUUM
6654 ** db in order to avoid introducing an incompatibility between autovacuum
6655 ** and non-autovacuum modes.
6659 case OP_Destroy
: { /* out2 */
6663 sqlite3VdbeIncrWriteCounter(p
, 0);
6664 assert( p
->readOnly
==0 );
6665 assert( pOp
->p1
>1 );
6666 pOut
= out2Prerelease(p
, pOp
);
6667 pOut
->flags
= MEM_Null
;
6668 if( db
->nVdbeRead
> db
->nVDestroy
+1 ){
6670 p
->errorAction
= OE_Abort
;
6671 goto abort_due_to_error
;
6674 assert( DbMaskTest(p
->btreeMask
, iDb
) );
6675 iMoved
= 0; /* Not needed. Only to silence a warning. */
6676 rc
= sqlite3BtreeDropTable(db
->aDb
[iDb
].pBt
, pOp
->p1
, &iMoved
);
6677 pOut
->flags
= MEM_Int
;
6679 if( rc
) goto abort_due_to_error
;
6680 #ifndef SQLITE_OMIT_AUTOVACUUM
6682 sqlite3RootPageMoved(db
, iDb
, iMoved
, pOp
->p1
);
6683 /* All OP_Destroy operations occur on the same btree */
6684 assert( resetSchemaOnFault
==0 || resetSchemaOnFault
==iDb
+1 );
6685 resetSchemaOnFault
= iDb
+1;
6692 /* Opcode: Clear P1 P2 P3
6694 ** Delete all contents of the database table or index whose root page
6695 ** in the database file is given by P1. But, unlike Destroy, do not
6696 ** remove the table or index from the database file.
6698 ** The table being clear is in the main database file if P2==0. If
6699 ** P2==1 then the table to be clear is in the auxiliary database file
6700 ** that is used to store tables create using CREATE TEMPORARY TABLE.
6702 ** If the P3 value is non-zero, then the row change count is incremented
6703 ** by the number of rows in the table being cleared. If P3 is greater
6704 ** than zero, then the value stored in register P3 is also incremented
6705 ** by the number of rows in the table being cleared.
6707 ** See also: Destroy
6712 sqlite3VdbeIncrWriteCounter(p
, 0);
6714 assert( p
->readOnly
==0 );
6715 assert( DbMaskTest(p
->btreeMask
, pOp
->p2
) );
6716 rc
= sqlite3BtreeClearTable(db
->aDb
[pOp
->p2
].pBt
, (u32
)pOp
->p1
, &nChange
);
6718 p
->nChange
+= nChange
;
6720 assert( memIsValid(&aMem
[pOp
->p3
]) );
6721 memAboutToChange(p
, &aMem
[pOp
->p3
]);
6722 aMem
[pOp
->p3
].u
.i
+= nChange
;
6725 if( rc
) goto abort_due_to_error
;
6729 /* Opcode: ResetSorter P1 * * * *
6731 ** Delete all contents from the ephemeral table or sorter
6732 ** that is open on cursor P1.
6734 ** This opcode only works for cursors used for sorting and
6735 ** opened with OP_OpenEphemeral or OP_SorterOpen.
6737 case OP_ResetSorter
: {
6740 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6741 pC
= p
->apCsr
[pOp
->p1
];
6744 sqlite3VdbeSorterReset(db
, pC
->uc
.pSorter
);
6746 assert( pC
->eCurType
==CURTYPE_BTREE
);
6747 assert( pC
->isEphemeral
);
6748 rc
= sqlite3BtreeClearTableOfCursor(pC
->uc
.pCursor
);
6749 if( rc
) goto abort_due_to_error
;
6754 /* Opcode: CreateBtree P1 P2 P3 * *
6755 ** Synopsis: r[P2]=root iDb=P1 flags=P3
6757 ** Allocate a new b-tree in the main database file if P1==0 or in the
6758 ** TEMP database file if P1==1 or in an attached database if
6759 ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
6760 ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
6761 ** The root page number of the new b-tree is stored in register P2.
6763 case OP_CreateBtree
: { /* out2 */
6767 sqlite3VdbeIncrWriteCounter(p
, 0);
6768 pOut
= out2Prerelease(p
, pOp
);
6770 assert( pOp
->p3
==BTREE_INTKEY
|| pOp
->p3
==BTREE_BLOBKEY
);
6771 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
6772 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
6773 assert( p
->readOnly
==0 );
6774 pDb
= &db
->aDb
[pOp
->p1
];
6775 assert( pDb
->pBt
!=0 );
6776 rc
= sqlite3BtreeCreateTable(pDb
->pBt
, &pgno
, pOp
->p3
);
6777 if( rc
) goto abort_due_to_error
;
6782 /* Opcode: SqlExec * * * P4 *
6784 ** Run the SQL statement or statements specified in the P4 string.
6787 sqlite3VdbeIncrWriteCounter(p
, 0);
6789 rc
= sqlite3_exec(db
, pOp
->p4
.z
, 0, 0, 0);
6791 if( rc
) goto abort_due_to_error
;
6795 /* Opcode: ParseSchema P1 * * P4 *
6797 ** Read and parse all entries from the schema table of database P1
6798 ** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6799 ** entire schema for P1 is reparsed.
6801 ** This opcode invokes the parser to create a new virtual machine,
6802 ** then runs the new virtual machine. It is thus a re-entrant opcode.
6804 case OP_ParseSchema
: {
6806 const char *zSchema
;
6810 /* Any prepared statement that invokes this opcode will hold mutexes
6811 ** on every btree. This is a prerequisite for invoking
6812 ** sqlite3InitCallback().
6815 for(iDb
=0; iDb
<db
->nDb
; iDb
++){
6816 assert( iDb
==1 || sqlite3BtreeHoldsMutex(db
->aDb
[iDb
].pBt
) );
6821 assert( iDb
>=0 && iDb
<db
->nDb
);
6822 assert( DbHasProperty(db
, iDb
, DB_SchemaLoaded
)
6824 || (CORRUPT_DB
&& (db
->flags
& SQLITE_NoSchemaError
)!=0) );
6826 #ifndef SQLITE_OMIT_ALTERTABLE
6828 sqlite3SchemaClear(db
->aDb
[iDb
].pSchema
);
6829 db
->mDbFlags
&= ~DBFLAG_SchemaKnownOk
;
6830 rc
= sqlite3InitOne(db
, iDb
, &p
->zErrMsg
, pOp
->p5
);
6831 db
->mDbFlags
|= DBFLAG_SchemaChange
;
6836 zSchema
= LEGACY_SCHEMA_TABLE
;
6839 initData
.pzErrMsg
= &p
->zErrMsg
;
6840 initData
.mInitFlags
= 0;
6841 initData
.mxPage
= sqlite3BtreeLastPage(db
->aDb
[iDb
].pBt
);
6842 zSql
= sqlite3MPrintf(db
,
6843 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
6844 db
->aDb
[iDb
].zDbSName
, zSchema
, pOp
->p4
.z
);
6846 rc
= SQLITE_NOMEM_BKPT
;
6848 assert( db
->init
.busy
==0 );
6850 initData
.rc
= SQLITE_OK
;
6851 initData
.nInitRow
= 0;
6852 assert( !db
->mallocFailed
);
6853 rc
= sqlite3_exec(db
, zSql
, sqlite3InitCallback
, &initData
, 0);
6854 if( rc
==SQLITE_OK
) rc
= initData
.rc
;
6855 if( rc
==SQLITE_OK
&& initData
.nInitRow
==0 ){
6856 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6857 ** at least one SQL statement. Any less than that indicates that
6858 ** the sqlite_schema table is corrupt. */
6859 rc
= SQLITE_CORRUPT_BKPT
;
6861 sqlite3DbFreeNN(db
, zSql
);
6866 sqlite3ResetAllSchemasOfConnection(db
);
6867 if( rc
==SQLITE_NOMEM
){
6870 goto abort_due_to_error
;
6875 #if !defined(SQLITE_OMIT_ANALYZE)
6876 /* Opcode: LoadAnalysis P1 * * * *
6878 ** Read the sqlite_stat1 table for database P1 and load the content
6879 ** of that table into the internal index hash table. This will cause
6880 ** the analysis to be used when preparing all subsequent queries.
6882 case OP_LoadAnalysis
: {
6883 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
6884 rc
= sqlite3AnalysisLoad(db
, pOp
->p1
);
6885 if( rc
) goto abort_due_to_error
;
6888 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
6890 /* Opcode: DropTable P1 * * P4 *
6892 ** Remove the internal (in-memory) data structures that describe
6893 ** the table named P4 in database P1. This is called after a table
6894 ** is dropped from disk (using the Destroy opcode) in order to keep
6895 ** the internal representation of the
6896 ** schema consistent with what is on disk.
6898 case OP_DropTable
: {
6899 sqlite3VdbeIncrWriteCounter(p
, 0);
6900 sqlite3UnlinkAndDeleteTable(db
, pOp
->p1
, pOp
->p4
.z
);
6904 /* Opcode: DropIndex P1 * * P4 *
6906 ** Remove the internal (in-memory) data structures that describe
6907 ** the index named P4 in database P1. This is called after an index
6908 ** is dropped from disk (using the Destroy opcode)
6909 ** in order to keep the internal representation of the
6910 ** schema consistent with what is on disk.
6912 case OP_DropIndex
: {
6913 sqlite3VdbeIncrWriteCounter(p
, 0);
6914 sqlite3UnlinkAndDeleteIndex(db
, pOp
->p1
, pOp
->p4
.z
);
6918 /* Opcode: DropTrigger P1 * * P4 *
6920 ** Remove the internal (in-memory) data structures that describe
6921 ** the trigger named P4 in database P1. This is called after a trigger
6922 ** is dropped from disk (using the Destroy opcode) in order to keep
6923 ** the internal representation of the
6924 ** schema consistent with what is on disk.
6926 case OP_DropTrigger
: {
6927 sqlite3VdbeIncrWriteCounter(p
, 0);
6928 sqlite3UnlinkAndDeleteTrigger(db
, pOp
->p1
, pOp
->p4
.z
);
6933 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
6934 /* Opcode: IntegrityCk P1 P2 P3 P4 P5
6936 ** Do an analysis of the currently open database. Store in
6937 ** register P1 the text of an error message describing any problems.
6938 ** If no problems are found, store a NULL in register P1.
6940 ** The register P3 contains one less than the maximum number of allowed errors.
6941 ** At most reg(P3) errors will be reported.
6942 ** In other words, the analysis stops as soon as reg(P1) errors are
6943 ** seen. Reg(P1) is updated with the number of errors remaining.
6945 ** The root page numbers of all tables in the database are integers
6946 ** stored in P4_INTARRAY argument.
6948 ** If P5 is not zero, the check is done on the auxiliary database
6949 ** file, not the main database file.
6951 ** This opcode is used to implement the integrity_check pragma.
6953 case OP_IntegrityCk
: {
6954 int nRoot
; /* Number of tables to check. (Number of root pages.) */
6955 Pgno
*aRoot
; /* Array of rootpage numbers for tables to be checked */
6956 int nErr
; /* Number of errors reported */
6957 char *z
; /* Text of the error report */
6958 Mem
*pnErr
; /* Register keeping track of errors remaining */
6960 assert( p
->bIsReader
);
6964 assert( aRoot
[0]==(Pgno
)nRoot
);
6965 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
6966 pnErr
= &aMem
[pOp
->p3
];
6967 assert( (pnErr
->flags
& MEM_Int
)!=0 );
6968 assert( (pnErr
->flags
& (MEM_Str
|MEM_Blob
))==0 );
6969 pIn1
= &aMem
[pOp
->p1
];
6970 assert( pOp
->p5
<db
->nDb
);
6971 assert( DbMaskTest(p
->btreeMask
, pOp
->p5
) );
6972 rc
= sqlite3BtreeIntegrityCheck(db
, db
->aDb
[pOp
->p5
].pBt
, &aRoot
[1], nRoot
,
6973 (int)pnErr
->u
.i
+1, &nErr
, &z
);
6974 sqlite3VdbeMemSetNull(pIn1
);
6979 goto abort_due_to_error
;
6981 pnErr
->u
.i
-= nErr
-1;
6982 sqlite3VdbeMemSetStr(pIn1
, z
, -1, SQLITE_UTF8
, sqlite3_free
);
6984 UPDATE_MAX_BLOBSIZE(pIn1
);
6985 sqlite3VdbeChangeEncoding(pIn1
, encoding
);
6986 goto check_for_interrupt
;
6988 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6990 /* Opcode: RowSetAdd P1 P2 * * *
6991 ** Synopsis: rowset(P1)=r[P2]
6993 ** Insert the integer value held by register P2 into a RowSet object
6994 ** held in register P1.
6996 ** An assertion fails if P2 is not an integer.
6998 case OP_RowSetAdd
: { /* in1, in2 */
6999 pIn1
= &aMem
[pOp
->p1
];
7000 pIn2
= &aMem
[pOp
->p2
];
7001 assert( (pIn2
->flags
& MEM_Int
)!=0 );
7002 if( (pIn1
->flags
& MEM_Blob
)==0 ){
7003 if( sqlite3VdbeMemSetRowSet(pIn1
) ) goto no_mem
;
7005 assert( sqlite3VdbeMemIsRowSet(pIn1
) );
7006 sqlite3RowSetInsert((RowSet
*)pIn1
->z
, pIn2
->u
.i
);
7010 /* Opcode: RowSetRead P1 P2 P3 * *
7011 ** Synopsis: r[P3]=rowset(P1)
7013 ** Extract the smallest value from the RowSet object in P1
7014 ** and put that value into register P3.
7015 ** Or, if RowSet object P1 is initially empty, leave P3
7016 ** unchanged and jump to instruction P2.
7018 case OP_RowSetRead
: { /* jump, in1, out3 */
7021 pIn1
= &aMem
[pOp
->p1
];
7022 assert( (pIn1
->flags
& MEM_Blob
)==0 || sqlite3VdbeMemIsRowSet(pIn1
) );
7023 if( (pIn1
->flags
& MEM_Blob
)==0
7024 || sqlite3RowSetNext((RowSet
*)pIn1
->z
, &val
)==0
7026 /* The boolean index is empty */
7027 sqlite3VdbeMemSetNull(pIn1
);
7028 VdbeBranchTaken(1,2);
7029 goto jump_to_p2_and_check_for_interrupt
;
7031 /* A value was pulled from the index */
7032 VdbeBranchTaken(0,2);
7033 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p3
], val
);
7035 goto check_for_interrupt
;
7038 /* Opcode: RowSetTest P1 P2 P3 P4
7039 ** Synopsis: if r[P3] in rowset(P1) goto P2
7041 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
7042 ** contains a RowSet object and that RowSet object contains
7043 ** the value held in P3, jump to register P2. Otherwise, insert the
7044 ** integer in P3 into the RowSet and continue on to the
7047 ** The RowSet object is optimized for the case where sets of integers
7048 ** are inserted in distinct phases, which each set contains no duplicates.
7049 ** Each set is identified by a unique P4 value. The first set
7050 ** must have P4==0, the final set must have P4==-1, and for all other sets
7053 ** This allows optimizations: (a) when P4==0 there is no need to test
7054 ** the RowSet object for P3, as it is guaranteed not to contain it,
7055 ** (b) when P4==-1 there is no need to insert the value, as it will
7056 ** never be tested for, and (c) when a value that is part of set X is
7057 ** inserted, there is no need to search to see if the same value was
7058 ** previously inserted as part of set X (only if it was previously
7059 ** inserted as part of some other set).
7061 case OP_RowSetTest
: { /* jump, in1, in3 */
7065 pIn1
= &aMem
[pOp
->p1
];
7066 pIn3
= &aMem
[pOp
->p3
];
7068 assert( pIn3
->flags
&MEM_Int
);
7070 /* If there is anything other than a rowset object in memory cell P1,
7071 ** delete it now and initialize P1 with an empty rowset
7073 if( (pIn1
->flags
& MEM_Blob
)==0 ){
7074 if( sqlite3VdbeMemSetRowSet(pIn1
) ) goto no_mem
;
7076 assert( sqlite3VdbeMemIsRowSet(pIn1
) );
7077 assert( pOp
->p4type
==P4_INT32
);
7078 assert( iSet
==-1 || iSet
>=0 );
7080 exists
= sqlite3RowSetTest((RowSet
*)pIn1
->z
, iSet
, pIn3
->u
.i
);
7081 VdbeBranchTaken(exists
!=0,2);
7082 if( exists
) goto jump_to_p2
;
7085 sqlite3RowSetInsert((RowSet
*)pIn1
->z
, pIn3
->u
.i
);
7091 #ifndef SQLITE_OMIT_TRIGGER
7093 /* Opcode: Program P1 P2 P3 P4 P5
7095 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
7097 ** P1 contains the address of the memory cell that contains the first memory
7098 ** cell in an array of values used as arguments to the sub-program. P2
7099 ** contains the address to jump to if the sub-program throws an IGNORE
7100 ** exception using the RAISE() function. Register P3 contains the address
7101 ** of a memory cell in this (the parent) VM that is used to allocate the
7102 ** memory required by the sub-vdbe at runtime.
7104 ** P4 is a pointer to the VM containing the trigger program.
7106 ** If P5 is non-zero, then recursive program invocation is enabled.
7108 case OP_Program
: { /* jump */
7109 int nMem
; /* Number of memory registers for sub-program */
7110 int nByte
; /* Bytes of runtime space required for sub-program */
7111 Mem
*pRt
; /* Register to allocate runtime space */
7112 Mem
*pMem
; /* Used to iterate through memory cells */
7113 Mem
*pEnd
; /* Last memory cell in new array */
7114 VdbeFrame
*pFrame
; /* New vdbe frame to execute in */
7115 SubProgram
*pProgram
; /* Sub-program to execute */
7116 void *t
; /* Token identifying trigger */
7118 pProgram
= pOp
->p4
.pProgram
;
7119 pRt
= &aMem
[pOp
->p3
];
7120 assert( pProgram
->nOp
>0 );
7122 /* If the p5 flag is clear, then recursive invocation of triggers is
7123 ** disabled for backwards compatibility (p5 is set if this sub-program
7124 ** is really a trigger, not a foreign key action, and the flag set
7125 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
7127 ** It is recursive invocation of triggers, at the SQL level, that is
7128 ** disabled. In some cases a single trigger may generate more than one
7129 ** SubProgram (if the trigger may be executed with more than one different
7130 ** ON CONFLICT algorithm). SubProgram structures associated with a
7131 ** single trigger all have the same value for the SubProgram.token
7134 t
= pProgram
->token
;
7135 for(pFrame
=p
->pFrame
; pFrame
&& pFrame
->token
!=t
; pFrame
=pFrame
->pParent
);
7139 if( p
->nFrame
>=db
->aLimit
[SQLITE_LIMIT_TRIGGER_DEPTH
] ){
7141 sqlite3VdbeError(p
, "too many levels of trigger recursion");
7142 goto abort_due_to_error
;
7145 /* Register pRt is used to store the memory required to save the state
7146 ** of the current program, and the memory required at runtime to execute
7147 ** the trigger program. If this trigger has been fired before, then pRt
7148 ** is already allocated. Otherwise, it must be initialized. */
7149 if( (pRt
->flags
&MEM_Blob
)==0 ){
7150 /* SubProgram.nMem is set to the number of memory cells used by the
7151 ** program stored in SubProgram.aOp. As well as these, one memory
7152 ** cell is required for each cursor used by the program. Set local
7153 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
7155 nMem
= pProgram
->nMem
+ pProgram
->nCsr
;
7157 if( pProgram
->nCsr
==0 ) nMem
++;
7158 nByte
= ROUND8(sizeof(VdbeFrame
))
7159 + nMem
* sizeof(Mem
)
7160 + pProgram
->nCsr
* sizeof(VdbeCursor
*)
7161 + (pProgram
->nOp
+ 7)/8;
7162 pFrame
= sqlite3DbMallocZero(db
, nByte
);
7166 sqlite3VdbeMemRelease(pRt
);
7167 pRt
->flags
= MEM_Blob
|MEM_Dyn
;
7168 pRt
->z
= (char*)pFrame
;
7170 pRt
->xDel
= sqlite3VdbeFrameMemDel
;
7173 pFrame
->nChildMem
= nMem
;
7174 pFrame
->nChildCsr
= pProgram
->nCsr
;
7175 pFrame
->pc
= (int)(pOp
- aOp
);
7176 pFrame
->aMem
= p
->aMem
;
7177 pFrame
->nMem
= p
->nMem
;
7178 pFrame
->apCsr
= p
->apCsr
;
7179 pFrame
->nCursor
= p
->nCursor
;
7180 pFrame
->aOp
= p
->aOp
;
7181 pFrame
->nOp
= p
->nOp
;
7182 pFrame
->token
= pProgram
->token
;
7184 pFrame
->iFrameMagic
= SQLITE_FRAME_MAGIC
;
7187 pEnd
= &VdbeFrameMem(pFrame
)[pFrame
->nChildMem
];
7188 for(pMem
=VdbeFrameMem(pFrame
); pMem
!=pEnd
; pMem
++){
7189 pMem
->flags
= MEM_Undefined
;
7193 pFrame
= (VdbeFrame
*)pRt
->z
;
7194 assert( pRt
->xDel
==sqlite3VdbeFrameMemDel
);
7195 assert( pProgram
->nMem
+pProgram
->nCsr
==pFrame
->nChildMem
7196 || (pProgram
->nCsr
==0 && pProgram
->nMem
+1==pFrame
->nChildMem
) );
7197 assert( pProgram
->nCsr
==pFrame
->nChildCsr
);
7198 assert( (int)(pOp
- aOp
)==pFrame
->pc
);
7202 pFrame
->pParent
= p
->pFrame
;
7203 pFrame
->lastRowid
= db
->lastRowid
;
7204 pFrame
->nChange
= p
->nChange
;
7205 pFrame
->nDbChange
= p
->db
->nChange
;
7206 assert( pFrame
->pAuxData
==0 );
7207 pFrame
->pAuxData
= p
->pAuxData
;
7211 p
->aMem
= aMem
= VdbeFrameMem(pFrame
);
7212 p
->nMem
= pFrame
->nChildMem
;
7213 p
->nCursor
= (u16
)pFrame
->nChildCsr
;
7214 p
->apCsr
= (VdbeCursor
**)&aMem
[p
->nMem
];
7215 pFrame
->aOnce
= (u8
*)&p
->apCsr
[pProgram
->nCsr
];
7216 memset(pFrame
->aOnce
, 0, (pProgram
->nOp
+ 7)/8);
7217 p
->aOp
= aOp
= pProgram
->aOp
;
7218 p
->nOp
= pProgram
->nOp
;
7220 /* Verify that second and subsequent executions of the same trigger do not
7221 ** try to reuse register values from the first use. */
7224 for(i
=0; i
<p
->nMem
; i
++){
7225 aMem
[i
].pScopyFrom
= 0; /* Prevent false-positive AboutToChange() errs */
7226 MemSetTypeFlag(&aMem
[i
], MEM_Undefined
); /* Fault if this reg is reused */
7231 goto check_for_interrupt
;
7234 /* Opcode: Param P1 P2 * * *
7236 ** This opcode is only ever present in sub-programs called via the
7237 ** OP_Program instruction. Copy a value currently stored in a memory
7238 ** cell of the calling (parent) frame to cell P2 in the current frames
7239 ** address space. This is used by trigger programs to access the new.*
7240 ** and old.* values.
7242 ** The address of the cell in the parent frame is determined by adding
7243 ** the value of the P1 argument to the value of the P1 argument to the
7244 ** calling OP_Program instruction.
7246 case OP_Param
: { /* out2 */
7249 pOut
= out2Prerelease(p
, pOp
);
7251 pIn
= &pFrame
->aMem
[pOp
->p1
+ pFrame
->aOp
[pFrame
->pc
].p1
];
7252 sqlite3VdbeMemShallowCopy(pOut
, pIn
, MEM_Ephem
);
7256 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
7258 #ifndef SQLITE_OMIT_FOREIGN_KEY
7259 /* Opcode: FkCounter P1 P2 * * *
7260 ** Synopsis: fkctr[P1]+=P2
7262 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
7263 ** If P1 is non-zero, the database constraint counter is incremented
7264 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
7265 ** statement counter is incremented (immediate foreign key constraints).
7267 case OP_FkCounter
: {
7268 if( db
->flags
& SQLITE_DeferFKs
){
7269 db
->nDeferredImmCons
+= pOp
->p2
;
7270 }else if( pOp
->p1
){
7271 db
->nDeferredCons
+= pOp
->p2
;
7273 p
->nFkConstraint
+= pOp
->p2
;
7278 /* Opcode: FkIfZero P1 P2 * * *
7279 ** Synopsis: if fkctr[P1]==0 goto P2
7281 ** This opcode tests if a foreign key constraint-counter is currently zero.
7282 ** If so, jump to instruction P2. Otherwise, fall through to the next
7285 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
7286 ** is zero (the one that counts deferred constraint violations). If P1 is
7287 ** zero, the jump is taken if the statement constraint-counter is zero
7288 ** (immediate foreign key constraint violations).
7290 case OP_FkIfZero
: { /* jump */
7292 VdbeBranchTaken(db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0, 2);
7293 if( db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0 ) goto jump_to_p2
;
7295 VdbeBranchTaken(p
->nFkConstraint
==0 && db
->nDeferredImmCons
==0, 2);
7296 if( p
->nFkConstraint
==0 && db
->nDeferredImmCons
==0 ) goto jump_to_p2
;
7300 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
7302 #ifndef SQLITE_OMIT_AUTOINCREMENT
7303 /* Opcode: MemMax P1 P2 * * *
7304 ** Synopsis: r[P1]=max(r[P1],r[P2])
7306 ** P1 is a register in the root frame of this VM (the root frame is
7307 ** different from the current frame if this instruction is being executed
7308 ** within a sub-program). Set the value of register P1 to the maximum of
7309 ** its current value and the value in register P2.
7311 ** This instruction throws an error if the memory cell is not initially
7314 case OP_MemMax
: { /* in2 */
7317 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
7318 pIn1
= &pFrame
->aMem
[pOp
->p1
];
7320 pIn1
= &aMem
[pOp
->p1
];
7322 assert( memIsValid(pIn1
) );
7323 sqlite3VdbeMemIntegerify(pIn1
);
7324 pIn2
= &aMem
[pOp
->p2
];
7325 sqlite3VdbeMemIntegerify(pIn2
);
7326 if( pIn1
->u
.i
<pIn2
->u
.i
){
7327 pIn1
->u
.i
= pIn2
->u
.i
;
7331 #endif /* SQLITE_OMIT_AUTOINCREMENT */
7333 /* Opcode: IfPos P1 P2 P3 * *
7334 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
7336 ** Register P1 must contain an integer.
7337 ** If the value of register P1 is 1 or greater, subtract P3 from the
7338 ** value in P1 and jump to P2.
7340 ** If the initial value of register P1 is less than 1, then the
7341 ** value is unchanged and control passes through to the next instruction.
7343 case OP_IfPos
: { /* jump, in1 */
7344 pIn1
= &aMem
[pOp
->p1
];
7345 assert( pIn1
->flags
&MEM_Int
);
7346 VdbeBranchTaken( pIn1
->u
.i
>0, 2);
7348 pIn1
->u
.i
-= pOp
->p3
;
7354 /* Opcode: OffsetLimit P1 P2 P3 * *
7355 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
7357 ** This opcode performs a commonly used computation associated with
7358 ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
7359 ** holds the offset counter. The opcode computes the combined value
7360 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
7361 ** value computed is the total number of rows that will need to be
7362 ** visited in order to complete the query.
7364 ** If r[P3] is zero or negative, that means there is no OFFSET
7365 ** and r[P2] is set to be the value of the LIMIT, r[P1].
7367 ** if r[P1] is zero or negative, that means there is no LIMIT
7368 ** and r[P2] is set to -1.
7370 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
7372 case OP_OffsetLimit
: { /* in1, out2, in3 */
7374 pIn1
= &aMem
[pOp
->p1
];
7375 pIn3
= &aMem
[pOp
->p3
];
7376 pOut
= out2Prerelease(p
, pOp
);
7377 assert( pIn1
->flags
& MEM_Int
);
7378 assert( pIn3
->flags
& MEM_Int
);
7380 if( x
<=0 || sqlite3AddInt64(&x
, pIn3
->u
.i
>0?pIn3
->u
.i
:0) ){
7381 /* If the LIMIT is less than or equal to zero, loop forever. This
7382 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
7383 ** also loop forever. This is undocumented. In fact, one could argue
7384 ** that the loop should terminate. But assuming 1 billion iterations
7385 ** per second (far exceeding the capabilities of any current hardware)
7386 ** it would take nearly 300 years to actually reach the limit. So
7387 ** looping forever is a reasonable approximation. */
7395 /* Opcode: IfNotZero P1 P2 * * *
7396 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
7398 ** Register P1 must contain an integer. If the content of register P1 is
7399 ** initially greater than zero, then decrement the value in register P1.
7400 ** If it is non-zero (negative or positive) and then also jump to P2.
7401 ** If register P1 is initially zero, leave it unchanged and fall through.
7403 case OP_IfNotZero
: { /* jump, in1 */
7404 pIn1
= &aMem
[pOp
->p1
];
7405 assert( pIn1
->flags
&MEM_Int
);
7406 VdbeBranchTaken(pIn1
->u
.i
<0, 2);
7408 if( pIn1
->u
.i
>0 ) pIn1
->u
.i
--;
7414 /* Opcode: DecrJumpZero P1 P2 * * *
7415 ** Synopsis: if (--r[P1])==0 goto P2
7417 ** Register P1 must hold an integer. Decrement the value in P1
7418 ** and jump to P2 if the new value is exactly zero.
7420 case OP_DecrJumpZero
: { /* jump, in1 */
7421 pIn1
= &aMem
[pOp
->p1
];
7422 assert( pIn1
->flags
&MEM_Int
);
7423 if( pIn1
->u
.i
>SMALLEST_INT64
) pIn1
->u
.i
--;
7424 VdbeBranchTaken(pIn1
->u
.i
==0, 2);
7425 if( pIn1
->u
.i
==0 ) goto jump_to_p2
;
7430 /* Opcode: AggStep * P2 P3 P4 P5
7431 ** Synopsis: accum=r[P3] step(r[P2@P5])
7433 ** Execute the xStep function for an aggregate.
7434 ** The function has P5 arguments. P4 is a pointer to the
7435 ** FuncDef structure that specifies the function. Register P3 is the
7438 ** The P5 arguments are taken from register P2 and its
7441 /* Opcode: AggInverse * P2 P3 P4 P5
7442 ** Synopsis: accum=r[P3] inverse(r[P2@P5])
7444 ** Execute the xInverse function for an aggregate.
7445 ** The function has P5 arguments. P4 is a pointer to the
7446 ** FuncDef structure that specifies the function. Register P3 is the
7449 ** The P5 arguments are taken from register P2 and its
7452 /* Opcode: AggStep1 P1 P2 P3 P4 P5
7453 ** Synopsis: accum=r[P3] step(r[P2@P5])
7455 ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
7456 ** aggregate. The function has P5 arguments. P4 is a pointer to the
7457 ** FuncDef structure that specifies the function. Register P3 is the
7460 ** The P5 arguments are taken from register P2 and its
7463 ** This opcode is initially coded as OP_AggStep0. On first evaluation,
7464 ** the FuncDef stored in P4 is converted into an sqlite3_context and
7465 ** the opcode is changed. In this way, the initialization of the
7466 ** sqlite3_context only happens once, instead of on each call to the
7472 sqlite3_context
*pCtx
;
7474 assert( pOp
->p4type
==P4_FUNCDEF
);
7476 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
7477 assert( n
==0 || (pOp
->p2
>0 && pOp
->p2
+n
<=(p
->nMem
+1 - p
->nCursor
)+1) );
7478 assert( pOp
->p3
<pOp
->p2
|| pOp
->p3
>=pOp
->p2
+n
);
7479 pCtx
= sqlite3DbMallocRawNN(db
, n
*sizeof(sqlite3_value
*) +
7480 (sizeof(pCtx
[0]) + sizeof(Mem
) - sizeof(sqlite3_value
*)));
7481 if( pCtx
==0 ) goto no_mem
;
7483 pCtx
->pOut
= (Mem
*)&(pCtx
->argv
[n
]);
7484 sqlite3VdbeMemInit(pCtx
->pOut
, db
, MEM_Null
);
7485 pCtx
->pFunc
= pOp
->p4
.pFunc
;
7486 pCtx
->iOp
= (int)(pOp
- aOp
);
7490 pCtx
->enc
= encoding
;
7492 pOp
->p4type
= P4_FUNCCTX
;
7493 pOp
->p4
.pCtx
= pCtx
;
7495 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
7496 assert( pOp
->p1
==(pOp
->opcode
==OP_AggInverse
) );
7498 pOp
->opcode
= OP_AggStep1
;
7499 /* Fall through into OP_AggStep */
7500 /* no break */ deliberate_fall_through
7504 sqlite3_context
*pCtx
;
7507 assert( pOp
->p4type
==P4_FUNCCTX
);
7508 pCtx
= pOp
->p4
.pCtx
;
7509 pMem
= &aMem
[pOp
->p3
];
7513 /* This is an OP_AggInverse call. Verify that xStep has always
7514 ** been called at least once prior to any xInverse call. */
7515 assert( pMem
->uTemp
==0x1122e0e3 );
7517 /* This is an OP_AggStep call. Mark it as such. */
7518 pMem
->uTemp
= 0x1122e0e3;
7522 /* If this function is inside of a trigger, the register array in aMem[]
7523 ** might change from one evaluation to the next. The next block of code
7524 ** checks to see if the register array has changed, and if so it
7525 ** reinitializes the relavant parts of the sqlite3_context object */
7526 if( pCtx
->pMem
!= pMem
){
7528 for(i
=pCtx
->argc
-1; i
>=0; i
--) pCtx
->argv
[i
] = &aMem
[pOp
->p2
+i
];
7532 for(i
=0; i
<pCtx
->argc
; i
++){
7533 assert( memIsValid(pCtx
->argv
[i
]) );
7534 REGISTER_TRACE(pOp
->p2
+i
, pCtx
->argv
[i
]);
7539 assert( pCtx
->pOut
->flags
==MEM_Null
);
7540 assert( pCtx
->isError
==0 );
7541 assert( pCtx
->skipFlag
==0 );
7542 #ifndef SQLITE_OMIT_WINDOWFUNC
7544 (pCtx
->pFunc
->xInverse
)(pCtx
,pCtx
->argc
,pCtx
->argv
);
7547 (pCtx
->pFunc
->xSFunc
)(pCtx
,pCtx
->argc
,pCtx
->argv
); /* IMP: R-24505-23230 */
7549 if( pCtx
->isError
){
7550 if( pCtx
->isError
>0 ){
7551 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pCtx
->pOut
));
7554 if( pCtx
->skipFlag
){
7555 assert( pOp
[-1].opcode
==OP_CollSeq
);
7557 if( i
) sqlite3VdbeMemSetInt64(&aMem
[i
], 1);
7560 sqlite3VdbeMemRelease(pCtx
->pOut
);
7561 pCtx
->pOut
->flags
= MEM_Null
;
7563 if( rc
) goto abort_due_to_error
;
7565 assert( pCtx
->pOut
->flags
==MEM_Null
);
7566 assert( pCtx
->skipFlag
==0 );
7570 /* Opcode: AggFinal P1 P2 * P4 *
7571 ** Synopsis: accum=r[P1] N=P2
7573 ** P1 is the memory location that is the accumulator for an aggregate
7574 ** or window function. Execute the finalizer function
7575 ** for an aggregate and store the result in P1.
7577 ** P2 is the number of arguments that the step function takes and
7578 ** P4 is a pointer to the FuncDef for this function. The P2
7579 ** argument is not used by this opcode. It is only there to disambiguate
7580 ** functions that can take varying numbers of arguments. The
7581 ** P4 argument is only needed for the case where
7582 ** the step function was not previously called.
7584 /* Opcode: AggValue * P2 P3 P4 *
7585 ** Synopsis: r[P3]=value N=P2
7587 ** Invoke the xValue() function and store the result in register P3.
7589 ** P2 is the number of arguments that the step function takes and
7590 ** P4 is a pointer to the FuncDef for this function. The P2
7591 ** argument is not used by this opcode. It is only there to disambiguate
7592 ** functions that can take varying numbers of arguments. The
7593 ** P4 argument is only needed for the case where
7594 ** the step function was not previously called.
7599 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
7600 assert( pOp
->p3
==0 || pOp
->opcode
==OP_AggValue
);
7601 pMem
= &aMem
[pOp
->p1
];
7602 assert( (pMem
->flags
& ~(MEM_Null
|MEM_Agg
))==0 );
7603 #ifndef SQLITE_OMIT_WINDOWFUNC
7605 memAboutToChange(p
, &aMem
[pOp
->p3
]);
7606 rc
= sqlite3VdbeMemAggValue(pMem
, &aMem
[pOp
->p3
], pOp
->p4
.pFunc
);
7607 pMem
= &aMem
[pOp
->p3
];
7611 rc
= sqlite3VdbeMemFinalize(pMem
, pOp
->p4
.pFunc
);
7615 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pMem
));
7616 goto abort_due_to_error
;
7618 sqlite3VdbeChangeEncoding(pMem
, encoding
);
7619 UPDATE_MAX_BLOBSIZE(pMem
);
7623 #ifndef SQLITE_OMIT_WAL
7624 /* Opcode: Checkpoint P1 P2 P3 * *
7626 ** Checkpoint database P1. This is a no-op if P1 is not currently in
7627 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7628 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
7629 ** SQLITE_BUSY or not, respectively. Write the number of pages in the
7630 ** WAL after the checkpoint into mem[P3+1] and the number of pages
7631 ** in the WAL that have been checkpointed after the checkpoint
7632 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
7633 ** mem[P3+2] are initialized to -1.
7635 case OP_Checkpoint
: {
7636 int i
; /* Loop counter */
7637 int aRes
[3]; /* Results */
7638 Mem
*pMem
; /* Write results here */
7640 assert( p
->readOnly
==0 );
7642 aRes
[1] = aRes
[2] = -1;
7643 assert( pOp
->p2
==SQLITE_CHECKPOINT_PASSIVE
7644 || pOp
->p2
==SQLITE_CHECKPOINT_FULL
7645 || pOp
->p2
==SQLITE_CHECKPOINT_RESTART
7646 || pOp
->p2
==SQLITE_CHECKPOINT_TRUNCATE
7648 rc
= sqlite3Checkpoint(db
, pOp
->p1
, pOp
->p2
, &aRes
[1], &aRes
[2]);
7650 if( rc
!=SQLITE_BUSY
) goto abort_due_to_error
;
7654 for(i
=0, pMem
= &aMem
[pOp
->p3
]; i
<3; i
++, pMem
++){
7655 sqlite3VdbeMemSetInt64(pMem
, (i64
)aRes
[i
]);
7661 #ifndef SQLITE_OMIT_PRAGMA
7662 /* Opcode: JournalMode P1 P2 P3 * *
7664 ** Change the journal mode of database P1 to P3. P3 must be one of the
7665 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7666 ** modes (delete, truncate, persist, off and memory), this is a simple
7667 ** operation. No IO is required.
7669 ** If changing into or out of WAL mode the procedure is more complicated.
7671 ** Write a string containing the final journal-mode to register P2.
7673 case OP_JournalMode
: { /* out2 */
7674 Btree
*pBt
; /* Btree to change journal mode of */
7675 Pager
*pPager
; /* Pager associated with pBt */
7676 int eNew
; /* New journal mode */
7677 int eOld
; /* The old journal mode */
7678 #ifndef SQLITE_OMIT_WAL
7679 const char *zFilename
; /* Name of database file for pPager */
7682 pOut
= out2Prerelease(p
, pOp
);
7684 assert( eNew
==PAGER_JOURNALMODE_DELETE
7685 || eNew
==PAGER_JOURNALMODE_TRUNCATE
7686 || eNew
==PAGER_JOURNALMODE_PERSIST
7687 || eNew
==PAGER_JOURNALMODE_OFF
7688 || eNew
==PAGER_JOURNALMODE_MEMORY
7689 || eNew
==PAGER_JOURNALMODE_WAL
7690 || eNew
==PAGER_JOURNALMODE_QUERY
7692 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
7693 assert( p
->readOnly
==0 );
7695 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7696 pPager
= sqlite3BtreePager(pBt
);
7697 eOld
= sqlite3PagerGetJournalMode(pPager
);
7698 if( eNew
==PAGER_JOURNALMODE_QUERY
) eNew
= eOld
;
7699 assert( sqlite3BtreeHoldsMutex(pBt
) );
7700 if( !sqlite3PagerOkToChangeJournalMode(pPager
) ) eNew
= eOld
;
7702 #ifndef SQLITE_OMIT_WAL
7703 zFilename
= sqlite3PagerFilename(pPager
, 1);
7705 /* Do not allow a transition to journal_mode=WAL for a database
7706 ** in temporary storage or if the VFS does not support shared memory
7708 if( eNew
==PAGER_JOURNALMODE_WAL
7709 && (sqlite3Strlen30(zFilename
)==0 /* Temp file */
7710 || !sqlite3PagerWalSupported(pPager
)) /* No shared-memory support */
7716 && (eOld
==PAGER_JOURNALMODE_WAL
|| eNew
==PAGER_JOURNALMODE_WAL
)
7718 if( !db
->autoCommit
|| db
->nVdbeRead
>1 ){
7721 "cannot change %s wal mode from within a transaction",
7722 (eNew
==PAGER_JOURNALMODE_WAL
? "into" : "out of")
7724 goto abort_due_to_error
;
7727 if( eOld
==PAGER_JOURNALMODE_WAL
){
7728 /* If leaving WAL mode, close the log file. If successful, the call
7729 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7730 ** file. An EXCLUSIVE lock may still be held on the database file
7731 ** after a successful return.
7733 rc
= sqlite3PagerCloseWal(pPager
, db
);
7734 if( rc
==SQLITE_OK
){
7735 sqlite3PagerSetJournalMode(pPager
, eNew
);
7737 }else if( eOld
==PAGER_JOURNALMODE_MEMORY
){
7738 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7739 ** as an intermediate */
7740 sqlite3PagerSetJournalMode(pPager
, PAGER_JOURNALMODE_OFF
);
7743 /* Open a transaction on the database file. Regardless of the journal
7744 ** mode, this transaction always uses a rollback journal.
7746 assert( sqlite3BtreeTxnState(pBt
)!=SQLITE_TXN_WRITE
);
7747 if( rc
==SQLITE_OK
){
7748 rc
= sqlite3BtreeSetVersion(pBt
, (eNew
==PAGER_JOURNALMODE_WAL
? 2 : 1));
7752 #endif /* ifndef SQLITE_OMIT_WAL */
7754 if( rc
) eNew
= eOld
;
7755 eNew
= sqlite3PagerSetJournalMode(pPager
, eNew
);
7757 pOut
->flags
= MEM_Str
|MEM_Static
|MEM_Term
;
7758 pOut
->z
= (char *)sqlite3JournalModename(eNew
);
7759 pOut
->n
= sqlite3Strlen30(pOut
->z
);
7760 pOut
->enc
= SQLITE_UTF8
;
7761 sqlite3VdbeChangeEncoding(pOut
, encoding
);
7762 if( rc
) goto abort_due_to_error
;
7765 #endif /* SQLITE_OMIT_PRAGMA */
7767 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
7768 /* Opcode: Vacuum P1 P2 * * *
7770 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7771 ** for an attached database. The "temp" database may not be vacuumed.
7773 ** If P2 is not zero, then it is a register holding a string which is
7774 ** the file into which the result of vacuum should be written. When
7775 ** P2 is zero, the vacuum overwrites the original database.
7778 assert( p
->readOnly
==0 );
7779 rc
= sqlite3RunVacuum(&p
->zErrMsg
, db
, pOp
->p1
,
7780 pOp
->p2
? &aMem
[pOp
->p2
] : 0);
7781 if( rc
) goto abort_due_to_error
;
7786 #if !defined(SQLITE_OMIT_AUTOVACUUM)
7787 /* Opcode: IncrVacuum P1 P2 * * *
7789 ** Perform a single step of the incremental vacuum procedure on
7790 ** the P1 database. If the vacuum has finished, jump to instruction
7791 ** P2. Otherwise, fall through to the next instruction.
7793 case OP_IncrVacuum
: { /* jump */
7796 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
7797 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
7798 assert( p
->readOnly
==0 );
7799 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7800 rc
= sqlite3BtreeIncrVacuum(pBt
);
7801 VdbeBranchTaken(rc
==SQLITE_DONE
,2);
7803 if( rc
!=SQLITE_DONE
) goto abort_due_to_error
;
7811 /* Opcode: Expire P1 P2 * * *
7813 ** Cause precompiled statements to expire. When an expired statement
7814 ** is executed using sqlite3_step() it will either automatically
7815 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7816 ** or it will fail with SQLITE_SCHEMA.
7818 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
7819 ** then only the currently executing statement is expired.
7821 ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7822 ** then running SQL statements are allowed to continue to run to completion.
7823 ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7824 ** that might help the statement run faster but which does not affect the
7825 ** correctness of operation.
7828 assert( pOp
->p2
==0 || pOp
->p2
==1 );
7830 sqlite3ExpirePreparedStatements(db
, pOp
->p2
);
7832 p
->expired
= pOp
->p2
+1;
7837 /* Opcode: CursorLock P1 * * * *
7839 ** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7840 ** written by an other cursor.
7842 case OP_CursorLock
: {
7844 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
7845 pC
= p
->apCsr
[pOp
->p1
];
7847 assert( pC
->eCurType
==CURTYPE_BTREE
);
7848 sqlite3BtreeCursorPin(pC
->uc
.pCursor
);
7852 /* Opcode: CursorUnlock P1 * * * *
7854 ** Unlock the btree to which cursor P1 is pointing so that it can be
7855 ** written by other cursors.
7857 case OP_CursorUnlock
: {
7859 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
7860 pC
= p
->apCsr
[pOp
->p1
];
7862 assert( pC
->eCurType
==CURTYPE_BTREE
);
7863 sqlite3BtreeCursorUnpin(pC
->uc
.pCursor
);
7867 #ifndef SQLITE_OMIT_SHARED_CACHE
7868 /* Opcode: TableLock P1 P2 P3 P4 *
7869 ** Synopsis: iDb=P1 root=P2 write=P3
7871 ** Obtain a lock on a particular table. This instruction is only used when
7872 ** the shared-cache feature is enabled.
7874 ** P1 is the index of the database in sqlite3.aDb[] of the database
7875 ** on which the lock is acquired. A readlock is obtained if P3==0 or
7876 ** a write lock if P3==1.
7878 ** P2 contains the root-page of the table to lock.
7880 ** P4 contains a pointer to the name of the table being locked. This is only
7881 ** used to generate an error message if the lock cannot be obtained.
7883 case OP_TableLock
: {
7884 u8 isWriteLock
= (u8
)pOp
->p3
;
7885 if( isWriteLock
|| 0==(db
->flags
&SQLITE_ReadUncommit
) ){
7887 assert( p1
>=0 && p1
<db
->nDb
);
7888 assert( DbMaskTest(p
->btreeMask
, p1
) );
7889 assert( isWriteLock
==0 || isWriteLock
==1 );
7890 rc
= sqlite3BtreeLockTable(db
->aDb
[p1
].pBt
, pOp
->p2
, isWriteLock
);
7892 if( (rc
&0xFF)==SQLITE_LOCKED
){
7893 const char *z
= pOp
->p4
.z
;
7894 sqlite3VdbeError(p
, "database table is locked: %s", z
);
7896 goto abort_due_to_error
;
7901 #endif /* SQLITE_OMIT_SHARED_CACHE */
7903 #ifndef SQLITE_OMIT_VIRTUALTABLE
7904 /* Opcode: VBegin * * * P4 *
7906 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7907 ** xBegin method for that table.
7909 ** Also, whether or not P4 is set, check that this is not being called from
7910 ** within a callback to a virtual table xSync() method. If it is, the error
7911 ** code will be set to SQLITE_LOCKED.
7915 pVTab
= pOp
->p4
.pVtab
;
7916 rc
= sqlite3VtabBegin(db
, pVTab
);
7917 if( pVTab
) sqlite3VtabImportErrmsg(p
, pVTab
->pVtab
);
7918 if( rc
) goto abort_due_to_error
;
7921 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7923 #ifndef SQLITE_OMIT_VIRTUALTABLE
7924 /* Opcode: VCreate P1 P2 * * *
7926 ** P2 is a register that holds the name of a virtual table in database
7927 ** P1. Call the xCreate method for that table.
7930 Mem sMem
; /* For storing the record being decoded */
7931 const char *zTab
; /* Name of the virtual table */
7933 memset(&sMem
, 0, sizeof(sMem
));
7935 /* Because P2 is always a static string, it is impossible for the
7936 ** sqlite3VdbeMemCopy() to fail */
7937 assert( (aMem
[pOp
->p2
].flags
& MEM_Str
)!=0 );
7938 assert( (aMem
[pOp
->p2
].flags
& MEM_Static
)!=0 );
7939 rc
= sqlite3VdbeMemCopy(&sMem
, &aMem
[pOp
->p2
]);
7940 assert( rc
==SQLITE_OK
);
7941 zTab
= (const char*)sqlite3_value_text(&sMem
);
7942 assert( zTab
|| db
->mallocFailed
);
7944 rc
= sqlite3VtabCallCreate(db
, pOp
->p1
, zTab
, &p
->zErrMsg
);
7946 sqlite3VdbeMemRelease(&sMem
);
7947 if( rc
) goto abort_due_to_error
;
7950 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7952 #ifndef SQLITE_OMIT_VIRTUALTABLE
7953 /* Opcode: VDestroy P1 * * P4 *
7955 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
7960 rc
= sqlite3VtabCallDestroy(db
, pOp
->p1
, pOp
->p4
.z
);
7962 assert( p
->errorAction
==OE_Abort
&& p
->usesStmtJournal
);
7963 if( rc
) goto abort_due_to_error
;
7966 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7968 #ifndef SQLITE_OMIT_VIRTUALTABLE
7969 /* Opcode: VOpen P1 * * P4 *
7971 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
7972 ** P1 is a cursor number. This opcode opens a cursor to the virtual
7973 ** table and stores that cursor in P1.
7975 case OP_VOpen
: { /* ncycle */
7977 sqlite3_vtab_cursor
*pVCur
;
7978 sqlite3_vtab
*pVtab
;
7979 const sqlite3_module
*pModule
;
7981 assert( p
->bIsReader
);
7984 pVtab
= pOp
->p4
.pVtab
->pVtab
;
7985 if( pVtab
==0 || NEVER(pVtab
->pModule
==0) ){
7987 goto abort_due_to_error
;
7989 pModule
= pVtab
->pModule
;
7990 rc
= pModule
->xOpen(pVtab
, &pVCur
);
7991 sqlite3VtabImportErrmsg(p
, pVtab
);
7992 if( rc
) goto abort_due_to_error
;
7994 /* Initialize sqlite3_vtab_cursor base class */
7995 pVCur
->pVtab
= pVtab
;
7997 /* Initialize vdbe cursor object */
7998 pCur
= allocateCursor(p
, pOp
->p1
, 0, CURTYPE_VTAB
);
8000 pCur
->uc
.pVCur
= pVCur
;
8003 assert( db
->mallocFailed
);
8004 pModule
->xClose(pVCur
);
8009 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8011 #ifndef SQLITE_OMIT_VIRTUALTABLE
8012 /* Opcode: VInitIn P1 P2 P3 * *
8013 ** Synopsis: r[P2]=ValueList(P1,P3)
8015 ** Set register P2 to be a pointer to a ValueList object for cursor P1
8016 ** with cache register P3 and output register P3+1. This ValueList object
8017 ** can be used as the first argument to sqlite3_vtab_in_first() and
8018 ** sqlite3_vtab_in_next() to extract all of the values stored in the P1
8019 ** cursor. Register P3 is used to hold the values returned by
8020 ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
8022 case OP_VInitIn
: { /* out2, ncycle */
8023 VdbeCursor
*pC
; /* The cursor containing the RHS values */
8024 ValueList
*pRhs
; /* New ValueList object to put in reg[P2] */
8026 pC
= p
->apCsr
[pOp
->p1
];
8027 pRhs
= sqlite3_malloc64( sizeof(*pRhs
) );
8028 if( pRhs
==0 ) goto no_mem
;
8029 pRhs
->pCsr
= pC
->uc
.pCursor
;
8030 pRhs
->pOut
= &aMem
[pOp
->p3
];
8031 pOut
= out2Prerelease(p
, pOp
);
8032 pOut
->flags
= MEM_Null
;
8033 sqlite3VdbeMemSetPointer(pOut
, pRhs
, "ValueList", sqlite3VdbeValueListFree
);
8036 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8039 #ifndef SQLITE_OMIT_VIRTUALTABLE
8040 /* Opcode: VFilter P1 P2 P3 P4 *
8041 ** Synopsis: iplan=r[P3] zplan='P4'
8043 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
8044 ** the filtered result set is empty.
8046 ** P4 is either NULL or a string that was generated by the xBestIndex
8047 ** method of the module. The interpretation of the P4 string is left
8048 ** to the module implementation.
8050 ** This opcode invokes the xFilter method on the virtual table specified
8051 ** by P1. The integer query plan parameter to xFilter is stored in register
8052 ** P3. Register P3+1 stores the argc parameter to be passed to the
8053 ** xFilter method. Registers P3+2..P3+1+argc are the argc
8054 ** additional parameters which are passed to
8055 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
8057 ** A jump is made to P2 if the result set after filtering would be empty.
8059 case OP_VFilter
: { /* jump, ncycle */
8062 const sqlite3_module
*pModule
;
8065 sqlite3_vtab_cursor
*pVCur
;
8066 sqlite3_vtab
*pVtab
;
8072 pQuery
= &aMem
[pOp
->p3
];
8074 pCur
= p
->apCsr
[pOp
->p1
];
8075 assert( memIsValid(pQuery
) );
8076 REGISTER_TRACE(pOp
->p3
, pQuery
);
8078 assert( pCur
->eCurType
==CURTYPE_VTAB
);
8079 pVCur
= pCur
->uc
.pVCur
;
8080 pVtab
= pVCur
->pVtab
;
8081 pModule
= pVtab
->pModule
;
8083 /* Grab the index number and argc parameters */
8084 assert( (pQuery
->flags
&MEM_Int
)!=0 && pArgc
->flags
==MEM_Int
);
8085 nArg
= (int)pArgc
->u
.i
;
8086 iQuery
= (int)pQuery
->u
.i
;
8088 /* Invoke the xFilter method */
8090 for(i
= 0; i
<nArg
; i
++){
8091 apArg
[i
] = &pArgc
[i
+1];
8093 rc
= pModule
->xFilter(pVCur
, iQuery
, pOp
->p4
.z
, nArg
, apArg
);
8094 sqlite3VtabImportErrmsg(p
, pVtab
);
8095 if( rc
) goto abort_due_to_error
;
8096 res
= pModule
->xEof(pVCur
);
8098 VdbeBranchTaken(res
!=0,2);
8099 if( res
) goto jump_to_p2
;
8102 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8104 #ifndef SQLITE_OMIT_VIRTUALTABLE
8105 /* Opcode: VColumn P1 P2 P3 * P5
8106 ** Synopsis: r[P3]=vcolumn(P2)
8108 ** Store in register P3 the value of the P2-th column of
8109 ** the current row of the virtual-table of cursor P1.
8111 ** If the VColumn opcode is being used to fetch the value of
8112 ** an unchanging column during an UPDATE operation, then the P5
8113 ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
8114 ** function to return true inside the xColumn method of the virtual
8115 ** table implementation. The P5 column might also contain other
8116 ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
8117 ** unused by OP_VColumn.
8119 case OP_VColumn
: { /* ncycle */
8120 sqlite3_vtab
*pVtab
;
8121 const sqlite3_module
*pModule
;
8123 sqlite3_context sContext
;
8125 VdbeCursor
*pCur
= p
->apCsr
[pOp
->p1
];
8127 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
8128 pDest
= &aMem
[pOp
->p3
];
8129 memAboutToChange(p
, pDest
);
8130 if( pCur
->nullRow
){
8131 sqlite3VdbeMemSetNull(pDest
);
8134 assert( pCur
->eCurType
==CURTYPE_VTAB
);
8135 pVtab
= pCur
->uc
.pVCur
->pVtab
;
8136 pModule
= pVtab
->pModule
;
8137 assert( pModule
->xColumn
);
8138 memset(&sContext
, 0, sizeof(sContext
));
8139 sContext
.pOut
= pDest
;
8140 sContext
.enc
= encoding
;
8141 assert( pOp
->p5
==OPFLAG_NOCHNG
|| pOp
->p5
==0 );
8142 if( pOp
->p5
& OPFLAG_NOCHNG
){
8143 sqlite3VdbeMemSetNull(pDest
);
8144 pDest
->flags
= MEM_Null
|MEM_Zero
;
8147 MemSetTypeFlag(pDest
, MEM_Null
);
8149 rc
= pModule
->xColumn(pCur
->uc
.pVCur
, &sContext
, pOp
->p2
);
8150 sqlite3VtabImportErrmsg(p
, pVtab
);
8151 if( sContext
.isError
>0 ){
8152 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pDest
));
8153 rc
= sContext
.isError
;
8155 sqlite3VdbeChangeEncoding(pDest
, encoding
);
8156 REGISTER_TRACE(pOp
->p3
, pDest
);
8157 UPDATE_MAX_BLOBSIZE(pDest
);
8159 if( rc
) goto abort_due_to_error
;
8162 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8164 #ifndef SQLITE_OMIT_VIRTUALTABLE
8165 /* Opcode: VNext P1 P2 * * *
8167 ** Advance virtual table P1 to the next row in its result set and
8168 ** jump to instruction P2. Or, if the virtual table has reached
8169 ** the end of its result set, then fall through to the next instruction.
8171 case OP_VNext
: { /* jump, ncycle */
8172 sqlite3_vtab
*pVtab
;
8173 const sqlite3_module
*pModule
;
8177 pCur
= p
->apCsr
[pOp
->p1
];
8179 assert( pCur
->eCurType
==CURTYPE_VTAB
);
8180 if( pCur
->nullRow
){
8183 pVtab
= pCur
->uc
.pVCur
->pVtab
;
8184 pModule
= pVtab
->pModule
;
8185 assert( pModule
->xNext
);
8187 /* Invoke the xNext() method of the module. There is no way for the
8188 ** underlying implementation to return an error if one occurs during
8189 ** xNext(). Instead, if an error occurs, true is returned (indicating that
8190 ** data is available) and the error code returned when xColumn or
8191 ** some other method is next invoked on the save virtual table cursor.
8193 rc
= pModule
->xNext(pCur
->uc
.pVCur
);
8194 sqlite3VtabImportErrmsg(p
, pVtab
);
8195 if( rc
) goto abort_due_to_error
;
8196 res
= pModule
->xEof(pCur
->uc
.pVCur
);
8197 VdbeBranchTaken(!res
,2);
8199 /* If there is data, jump to P2 */
8200 goto jump_to_p2_and_check_for_interrupt
;
8202 goto check_for_interrupt
;
8204 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8206 #ifndef SQLITE_OMIT_VIRTUALTABLE
8207 /* Opcode: VRename P1 * * P4 *
8209 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
8210 ** This opcode invokes the corresponding xRename method. The value
8211 ** in register P1 is passed as the zName argument to the xRename method.
8214 sqlite3_vtab
*pVtab
;
8218 isLegacy
= (db
->flags
& SQLITE_LegacyAlter
);
8219 db
->flags
|= SQLITE_LegacyAlter
;
8220 pVtab
= pOp
->p4
.pVtab
->pVtab
;
8221 pName
= &aMem
[pOp
->p1
];
8222 assert( pVtab
->pModule
->xRename
);
8223 assert( memIsValid(pName
) );
8224 assert( p
->readOnly
==0 );
8225 REGISTER_TRACE(pOp
->p1
, pName
);
8226 assert( pName
->flags
& MEM_Str
);
8227 testcase( pName
->enc
==SQLITE_UTF8
);
8228 testcase( pName
->enc
==SQLITE_UTF16BE
);
8229 testcase( pName
->enc
==SQLITE_UTF16LE
);
8230 rc
= sqlite3VdbeChangeEncoding(pName
, SQLITE_UTF8
);
8231 if( rc
) goto abort_due_to_error
;
8232 rc
= pVtab
->pModule
->xRename(pVtab
, pName
->z
);
8233 if( isLegacy
==0 ) db
->flags
&= ~(u64
)SQLITE_LegacyAlter
;
8234 sqlite3VtabImportErrmsg(p
, pVtab
);
8236 if( rc
) goto abort_due_to_error
;
8241 #ifndef SQLITE_OMIT_VIRTUALTABLE
8242 /* Opcode: VUpdate P1 P2 P3 P4 P5
8243 ** Synopsis: data=r[P3@P2]
8245 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
8246 ** This opcode invokes the corresponding xUpdate method. P2 values
8247 ** are contiguous memory cells starting at P3 to pass to the xUpdate
8248 ** invocation. The value in register (P3+P2-1) corresponds to the
8249 ** p2th element of the argv array passed to xUpdate.
8251 ** The xUpdate method will do a DELETE or an INSERT or both.
8252 ** The argv[0] element (which corresponds to memory cell P3)
8253 ** is the rowid of a row to delete. If argv[0] is NULL then no
8254 ** deletion occurs. The argv[1] element is the rowid of the new
8255 ** row. This can be NULL to have the virtual table select the new
8256 ** rowid for itself. The subsequent elements in the array are
8257 ** the values of columns in the new row.
8259 ** If P2==1 then no insert is performed. argv[0] is the rowid of
8262 ** P1 is a boolean flag. If it is set to true and the xUpdate call
8263 ** is successful, then the value returned by sqlite3_last_insert_rowid()
8264 ** is set to the value of the rowid for the row just inserted.
8266 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
8267 ** apply in the case of a constraint failure on an insert or update.
8270 sqlite3_vtab
*pVtab
;
8271 const sqlite3_module
*pModule
;
8274 sqlite_int64 rowid
= 0;
8278 assert( pOp
->p2
==1 || pOp
->p5
==OE_Fail
|| pOp
->p5
==OE_Rollback
8279 || pOp
->p5
==OE_Abort
|| pOp
->p5
==OE_Ignore
|| pOp
->p5
==OE_Replace
8281 assert( p
->readOnly
==0 );
8282 if( db
->mallocFailed
) goto no_mem
;
8283 sqlite3VdbeIncrWriteCounter(p
, 0);
8284 pVtab
= pOp
->p4
.pVtab
->pVtab
;
8285 if( pVtab
==0 || NEVER(pVtab
->pModule
==0) ){
8287 goto abort_due_to_error
;
8289 pModule
= pVtab
->pModule
;
8291 assert( pOp
->p4type
==P4_VTAB
);
8292 if( ALWAYS(pModule
->xUpdate
) ){
8293 u8 vtabOnConflict
= db
->vtabOnConflict
;
8295 pX
= &aMem
[pOp
->p3
];
8296 for(i
=0; i
<nArg
; i
++){
8297 assert( memIsValid(pX
) );
8298 memAboutToChange(p
, pX
);
8302 db
->vtabOnConflict
= pOp
->p5
;
8303 rc
= pModule
->xUpdate(pVtab
, nArg
, apArg
, &rowid
);
8304 db
->vtabOnConflict
= vtabOnConflict
;
8305 sqlite3VtabImportErrmsg(p
, pVtab
);
8306 if( rc
==SQLITE_OK
&& pOp
->p1
){
8307 assert( nArg
>1 && apArg
[0] && (apArg
[0]->flags
&MEM_Null
) );
8308 db
->lastRowid
= rowid
;
8310 if( (rc
&0xff)==SQLITE_CONSTRAINT
&& pOp
->p4
.pVtab
->bConstraint
){
8311 if( pOp
->p5
==OE_Ignore
){
8314 p
->errorAction
= ((pOp
->p5
==OE_Replace
) ? OE_Abort
: pOp
->p5
);
8319 if( rc
) goto abort_due_to_error
;
8323 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8325 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
8326 /* Opcode: Pagecount P1 P2 * * *
8328 ** Write the current number of pages in database P1 to memory cell P2.
8330 case OP_Pagecount
: { /* out2 */
8331 pOut
= out2Prerelease(p
, pOp
);
8332 pOut
->u
.i
= sqlite3BtreeLastPage(db
->aDb
[pOp
->p1
].pBt
);
8338 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
8339 /* Opcode: MaxPgcnt P1 P2 P3 * *
8341 ** Try to set the maximum page count for database P1 to the value in P3.
8342 ** Do not let the maximum page count fall below the current page count and
8343 ** do not change the maximum page count value if P3==0.
8345 ** Store the maximum page count after the change in register P2.
8347 case OP_MaxPgcnt
: { /* out2 */
8348 unsigned int newMax
;
8351 pOut
= out2Prerelease(p
, pOp
);
8352 pBt
= db
->aDb
[pOp
->p1
].pBt
;
8355 newMax
= sqlite3BtreeLastPage(pBt
);
8356 if( newMax
< (unsigned)pOp
->p3
) newMax
= (unsigned)pOp
->p3
;
8358 pOut
->u
.i
= sqlite3BtreeMaxPageCount(pBt
, newMax
);
8363 /* Opcode: Function P1 P2 P3 P4 *
8364 ** Synopsis: r[P3]=func(r[P2@NP])
8366 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
8367 ** contains a pointer to the function to be run) with arguments taken
8368 ** from register P2 and successors. The number of arguments is in
8369 ** the sqlite3_context object that P4 points to.
8370 ** The result of the function is stored
8371 ** in register P3. Register P3 must not be one of the function inputs.
8373 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
8374 ** function was determined to be constant at compile time. If the first
8375 ** argument was constant then bit 0 of P1 is set. This is used to determine
8376 ** whether meta data associated with a user function argument using the
8377 ** sqlite3_set_auxdata() API may be safely retained until the next
8378 ** invocation of this opcode.
8380 ** See also: AggStep, AggFinal, PureFunc
8382 /* Opcode: PureFunc P1 P2 P3 P4 *
8383 ** Synopsis: r[P3]=func(r[P2@NP])
8385 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
8386 ** contains a pointer to the function to be run) with arguments taken
8387 ** from register P2 and successors. The number of arguments is in
8388 ** the sqlite3_context object that P4 points to.
8389 ** The result of the function is stored
8390 ** in register P3. Register P3 must not be one of the function inputs.
8392 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
8393 ** function was determined to be constant at compile time. If the first
8394 ** argument was constant then bit 0 of P1 is set. This is used to determine
8395 ** whether meta data associated with a user function argument using the
8396 ** sqlite3_set_auxdata() API may be safely retained until the next
8397 ** invocation of this opcode.
8399 ** This opcode works exactly like OP_Function. The only difference is in
8400 ** its name. This opcode is used in places where the function must be
8401 ** purely non-deterministic. Some built-in date/time functions can be
8402 ** either determinitic of non-deterministic, depending on their arguments.
8403 ** When those function are used in a non-deterministic way, they will check
8404 ** to see if they were called using OP_PureFunc instead of OP_Function, and
8405 ** if they were, they throw an error.
8407 ** See also: AggStep, AggFinal, Function
8409 case OP_PureFunc
: /* group */
8410 case OP_Function
: { /* group */
8412 sqlite3_context
*pCtx
;
8414 assert( pOp
->p4type
==P4_FUNCCTX
);
8415 pCtx
= pOp
->p4
.pCtx
;
8417 /* If this function is inside of a trigger, the register array in aMem[]
8418 ** might change from one evaluation to the next. The next block of code
8419 ** checks to see if the register array has changed, and if so it
8420 ** reinitializes the relavant parts of the sqlite3_context object */
8421 pOut
= &aMem
[pOp
->p3
];
8422 if( pCtx
->pOut
!= pOut
){
8425 pCtx
->enc
= encoding
;
8426 for(i
=pCtx
->argc
-1; i
>=0; i
--) pCtx
->argv
[i
] = &aMem
[pOp
->p2
+i
];
8428 assert( pCtx
->pVdbe
==p
);
8430 memAboutToChange(p
, pOut
);
8432 for(i
=0; i
<pCtx
->argc
; i
++){
8433 assert( memIsValid(pCtx
->argv
[i
]) );
8434 REGISTER_TRACE(pOp
->p2
+i
, pCtx
->argv
[i
]);
8437 MemSetTypeFlag(pOut
, MEM_Null
);
8438 assert( pCtx
->isError
==0 );
8439 (*pCtx
->pFunc
->xSFunc
)(pCtx
, pCtx
->argc
, pCtx
->argv
);/* IMP: R-24505-23230 */
8441 /* If the function returned an error, throw an exception */
8442 if( pCtx
->isError
){
8443 if( pCtx
->isError
>0 ){
8444 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pOut
));
8447 sqlite3VdbeDeleteAuxData(db
, &p
->pAuxData
, pCtx
->iOp
, pOp
->p1
);
8449 if( rc
) goto abort_due_to_error
;
8452 assert( (pOut
->flags
&MEM_Str
)==0
8453 || pOut
->enc
==encoding
8454 || db
->mallocFailed
);
8455 assert( !sqlite3VdbeMemTooBig(pOut
) );
8457 REGISTER_TRACE(pOp
->p3
, pOut
);
8458 UPDATE_MAX_BLOBSIZE(pOut
);
8462 /* Opcode: ClrSubtype P1 * * * *
8463 ** Synopsis: r[P1].subtype = 0
8465 ** Clear the subtype from register P1.
8467 case OP_ClrSubtype
: { /* in1 */
8468 pIn1
= &aMem
[pOp
->p1
];
8469 pIn1
->flags
&= ~MEM_Subtype
;
8473 /* Opcode: FilterAdd P1 * P3 P4 *
8474 ** Synopsis: filter(P1) += key(P3@P4)
8476 ** Compute a hash on the P4 registers starting with r[P3] and
8477 ** add that hash to the bloom filter contained in r[P1].
8479 case OP_FilterAdd
: {
8482 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
8483 pIn1
= &aMem
[pOp
->p1
];
8484 assert( pIn1
->flags
& MEM_Blob
);
8485 assert( pIn1
->n
>0 );
8486 h
= filterHash(aMem
, pOp
);
8488 if( db
->flags
&SQLITE_VdbeTrace
){
8490 for(ii
=pOp
->p3
; ii
<pOp
->p3
+pOp
->p4
.i
; ii
++){
8491 registerTrace(ii
, &aMem
[ii
]);
8493 printf("hash: %llu modulo %d -> %u\n", h
, pIn1
->n
, (int)(h
%pIn1
->n
));
8497 pIn1
->z
[h
/8] |= 1<<(h
&7);
8501 /* Opcode: Filter P1 P2 P3 P4 *
8502 ** Synopsis: if key(P3@P4) not in filter(P1) goto P2
8504 ** Compute a hash on the key contained in the P4 registers starting
8505 ** with r[P3]. Check to see if that hash is found in the
8506 ** bloom filter hosted by register P1. If it is not present then
8507 ** maybe jump to P2. Otherwise fall through.
8509 ** False negatives are harmless. It is always safe to fall through,
8510 ** even if the value is in the bloom filter. A false negative causes
8511 ** more CPU cycles to be used, but it should still yield the correct
8512 ** answer. However, an incorrect answer may well arise from a
8513 ** false positive - if the jump is taken when it should fall through.
8515 case OP_Filter
: { /* jump */
8518 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
8519 pIn1
= &aMem
[pOp
->p1
];
8520 assert( (pIn1
->flags
& MEM_Blob
)!=0 );
8521 assert( pIn1
->n
>= 1 );
8522 h
= filterHash(aMem
, pOp
);
8524 if( db
->flags
&SQLITE_VdbeTrace
){
8526 for(ii
=pOp
->p3
; ii
<pOp
->p3
+pOp
->p4
.i
; ii
++){
8527 registerTrace(ii
, &aMem
[ii
]);
8529 printf("hash: %llu modulo %d -> %u\n", h
, pIn1
->n
, (int)(h
%pIn1
->n
));
8533 if( (pIn1
->z
[h
/8] & (1<<(h
&7)))==0 ){
8534 VdbeBranchTaken(1, 2);
8535 p
->aCounter
[SQLITE_STMTSTATUS_FILTER_HIT
]++;
8538 p
->aCounter
[SQLITE_STMTSTATUS_FILTER_MISS
]++;
8539 VdbeBranchTaken(0, 2);
8544 /* Opcode: Trace P1 P2 * P4 *
8546 ** Write P4 on the statement trace output if statement tracing is
8549 ** Operand P1 must be 0x7fffffff and P2 must positive.
8551 /* Opcode: Init P1 P2 P3 P4 *
8552 ** Synopsis: Start at P2
8554 ** Programs contain a single instance of this opcode as the very first
8557 ** If tracing is enabled (by the sqlite3_trace()) interface, then
8558 ** the UTF-8 string contained in P4 is emitted on the trace callback.
8559 ** Or if P4 is blank, use the string returned by sqlite3_sql().
8561 ** If P2 is not zero, jump to instruction P2.
8563 ** Increment the value of P1 so that OP_Once opcodes will jump the
8564 ** first time they are evaluated for this run.
8566 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
8567 ** error is encountered.
8570 case OP_Init
: { /* jump */
8572 #ifndef SQLITE_OMIT_TRACE
8576 /* If the P4 argument is not NULL, then it must be an SQL comment string.
8577 ** The "--" string is broken up to prevent false-positives with srcck1.c.
8579 ** This assert() provides evidence for:
8580 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
8581 ** would have been returned by the legacy sqlite3_trace() interface by
8582 ** using the X argument when X begins with "--" and invoking
8583 ** sqlite3_expanded_sql(P) otherwise.
8585 assert( pOp
->p4
.z
==0 || strncmp(pOp
->p4
.z
, "-" "- ", 3)==0 );
8587 /* OP_Init is always instruction 0 */
8588 assert( pOp
==p
->aOp
|| pOp
->opcode
==OP_Trace
);
8590 #ifndef SQLITE_OMIT_TRACE
8591 if( (db
->mTrace
& (SQLITE_TRACE_STMT
|SQLITE_TRACE_LEGACY
))!=0
8592 && p
->minWriteFileFormat
!=254 /* tag-20220401a */
8593 && (zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
))!=0
8595 #ifndef SQLITE_OMIT_DEPRECATED
8596 if( db
->mTrace
& SQLITE_TRACE_LEGACY
){
8597 char *z
= sqlite3VdbeExpandSql(p
, zTrace
);
8598 db
->trace
.xLegacy(db
->pTraceArg
, z
);
8602 if( db
->nVdbeExec
>1 ){
8603 char *z
= sqlite3MPrintf(db
, "-- %s", zTrace
);
8604 (void)db
->trace
.xV2(SQLITE_TRACE_STMT
, db
->pTraceArg
, p
, z
);
8605 sqlite3DbFree(db
, z
);
8607 (void)db
->trace
.xV2(SQLITE_TRACE_STMT
, db
->pTraceArg
, p
, zTrace
);
8610 #ifdef SQLITE_USE_FCNTL_TRACE
8611 zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
);
8614 for(j
=0; j
<db
->nDb
; j
++){
8615 if( DbMaskTest(p
->btreeMask
, j
)==0 ) continue;
8616 sqlite3_file_control(db
, db
->aDb
[j
].zDbSName
, SQLITE_FCNTL_TRACE
, zTrace
);
8619 #endif /* SQLITE_USE_FCNTL_TRACE */
8621 if( (db
->flags
& SQLITE_SqlTrace
)!=0
8622 && (zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
))!=0
8624 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace
);
8626 #endif /* SQLITE_DEBUG */
8627 #endif /* SQLITE_OMIT_TRACE */
8628 assert( pOp
->p2
>0 );
8629 if( pOp
->p1
>=sqlite3GlobalConfig
.iOnceResetThreshold
){
8630 if( pOp
->opcode
==OP_Trace
) break;
8631 for(i
=1; i
<p
->nOp
; i
++){
8632 if( p
->aOp
[i
].opcode
==OP_Once
) p
->aOp
[i
].p1
= 0;
8637 p
->aCounter
[SQLITE_STMTSTATUS_RUN
]++;
8641 #ifdef SQLITE_ENABLE_CURSOR_HINTS
8642 /* Opcode: CursorHint P1 * * P4 *
8644 ** Provide a hint to cursor P1 that it only needs to return rows that
8645 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8646 ** to values currently held in registers. TK_COLUMN terms in the P4
8647 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
8649 case OP_CursorHint
: {
8652 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
8653 assert( pOp
->p4type
==P4_EXPR
);
8654 pC
= p
->apCsr
[pOp
->p1
];
8656 assert( pC
->eCurType
==CURTYPE_BTREE
);
8657 sqlite3BtreeCursorHint(pC
->uc
.pCursor
, BTREE_HINT_RANGE
,
8658 pOp
->p4
.pExpr
, aMem
);
8662 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
8665 /* Opcode: Abortable * * * * *
8667 ** Verify that an Abort can happen. Assert if an Abort at this point
8668 ** might cause database corruption. This opcode only appears in debugging
8671 ** An Abort is safe if either there have been no writes, or if there is
8672 ** an active statement journal.
8674 case OP_Abortable
: {
8675 sqlite3VdbeAssertAbortable(p
);
8681 /* Opcode: ReleaseReg P1 P2 P3 * P5
8682 ** Synopsis: release r[P1@P2] mask P3
8684 ** Release registers from service. Any content that was in the
8685 ** the registers is unreliable after this opcode completes.
8687 ** The registers released will be the P2 registers starting at P1,
8688 ** except if bit ii of P3 set, then do not release register P1+ii.
8689 ** In other words, P3 is a mask of registers to preserve.
8691 ** Releasing a register clears the Mem.pScopyFrom pointer. That means
8692 ** that if the content of the released register was set using OP_SCopy,
8693 ** a change to the value of the source register for the OP_SCopy will no longer
8694 ** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8696 ** If P5 is set, then all released registers have their type set
8697 ** to MEM_Undefined so that any subsequent attempt to read the released
8698 ** register (before it is reinitialized) will generate an assertion fault.
8700 ** P5 ought to be set on every call to this opcode.
8701 ** However, there are places in the code generator will release registers
8702 ** before their are used, under the (valid) assumption that the registers
8703 ** will not be reallocated for some other purpose before they are used and
8704 ** hence are safe to release.
8706 ** This opcode is only available in testing and debugging builds. It is
8707 ** not generated for release builds. The purpose of this opcode is to help
8708 ** validate the generated bytecode. This opcode does not actually contribute
8709 ** to computing an answer.
8711 case OP_ReleaseReg
: {
8715 assert( pOp
->p1
>0 );
8716 assert( pOp
->p1
+pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
)+1 );
8717 pMem
= &aMem
[pOp
->p1
];
8718 constMask
= pOp
->p3
;
8719 for(i
=0; i
<pOp
->p2
; i
++, pMem
++){
8720 if( i
>=32 || (constMask
& MASKBIT32(i
))==0 ){
8721 pMem
->pScopyFrom
= 0;
8722 if( i
<32 && pOp
->p5
) MemSetTypeFlag(pMem
, MEM_Undefined
);
8729 /* Opcode: Noop * * * * *
8731 ** Do nothing. This instruction is often useful as a jump
8735 ** The magic Explain opcode are only inserted when explain==2 (which
8736 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8737 ** This opcode records information from the optimizer. It is the
8738 ** the same as a no-op. This opcodesnever appears in a real VM program.
8740 default: { /* This is really OP_Noop, OP_Explain */
8741 assert( pOp
->opcode
==OP_Noop
|| pOp
->opcode
==OP_Explain
);
8746 /*****************************************************************************
8747 ** The cases of the switch statement above this line should all be indented
8748 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8749 ** readability. From this point on down, the normal indentation rules are
8751 *****************************************************************************/
8754 #if defined(VDBE_PROFILE)
8755 *pnCycle
+= sqlite3NProfileCnt
? sqlite3NProfileCnt
: sqlite3Hwtime();
8757 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
8758 *pnCycle
+= sqlite3Hwtime();
8762 /* The following code adds nothing to the actual functionality
8763 ** of the program. It is only here for testing and debugging.
8764 ** On the other hand, it does burn CPU cycles every time through
8765 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8768 assert( pOp
>=&aOp
[-1] && pOp
<&aOp
[p
->nOp
-1] );
8771 if( db
->flags
& SQLITE_VdbeTrace
){
8772 u8 opProperty
= sqlite3OpcodeProperty
[pOrigOp
->opcode
];
8773 if( rc
!=0 ) printf("rc=%d\n",rc
);
8774 if( opProperty
& (OPFLG_OUT2
) ){
8775 registerTrace(pOrigOp
->p2
, &aMem
[pOrigOp
->p2
]);
8777 if( opProperty
& OPFLG_OUT3
){
8778 registerTrace(pOrigOp
->p3
, &aMem
[pOrigOp
->p3
]);
8780 if( opProperty
==0xff ){
8781 /* Never happens. This code exists to avoid a harmless linkage
8782 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8784 sqlite3VdbeRegisterDump(p
);
8787 #endif /* SQLITE_DEBUG */
8789 } /* The end of the for(;;) loop the loops through opcodes */
8791 /* If we reach this point, it means that execution is finished with
8792 ** an error of some kind.
8795 if( db
->mallocFailed
){
8796 rc
= SQLITE_NOMEM_BKPT
;
8797 }else if( rc
==SQLITE_IOERR_CORRUPTFS
){
8798 rc
= SQLITE_CORRUPT_BKPT
;
8802 if( db
->flags
& SQLITE_VdbeTrace
){
8803 const char *zTrace
= p
->zSql
;
8805 if( aOp
[0].opcode
==OP_Trace
){
8806 zTrace
= aOp
[0].p4
.z
;
8808 if( zTrace
==0 ) zTrace
= "???";
8810 printf("ABORT-due-to-error (rc=%d): %s\n", rc
, zTrace
);
8813 if( p
->zErrMsg
==0 && rc
!=SQLITE_IOERR_NOMEM
){
8814 sqlite3VdbeError(p
, "%s", sqlite3ErrStr(rc
));
8817 sqlite3SystemError(db
, rc
);
8818 testcase( sqlite3GlobalConfig
.xLog
!=0 );
8819 sqlite3_log(rc
, "statement aborts at %d: [%s] %s",
8820 (int)(pOp
- aOp
), p
->zSql
, p
->zErrMsg
);
8821 if( p
->eVdbeState
==VDBE_RUN_STATE
) sqlite3VdbeHalt(p
);
8822 if( rc
==SQLITE_IOERR_NOMEM
) sqlite3OomFault(db
);
8823 if( rc
==SQLITE_CORRUPT
&& db
->autoCommit
==0 ){
8824 db
->flags
|= SQLITE_CorruptRdOnly
;
8827 if( resetSchemaOnFault
>0 ){
8828 sqlite3ResetOneSchema(db
, resetSchemaOnFault
-1);
8831 /* This is the only way out of this procedure. We have to
8832 ** release the mutexes on btrees that were acquired at the
8835 #if defined(VDBE_PROFILE)
8837 *pnCycle
+= sqlite3NProfileCnt
? sqlite3NProfileCnt
: sqlite3Hwtime();
8840 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
8842 *pnCycle
+= sqlite3Hwtime();
8847 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8848 while( nVmStep
>=nProgressLimit
&& db
->xProgress
!=0 ){
8849 nProgressLimit
+= db
->nProgressOps
;
8850 if( db
->xProgress(db
->pProgressArg
) ){
8851 nProgressLimit
= LARGEST_UINT64
;
8852 rc
= SQLITE_INTERRUPT
;
8853 goto abort_due_to_error
;
8857 p
->aCounter
[SQLITE_STMTSTATUS_VM_STEP
] += (int)nVmStep
;
8858 if( DbMaskNonZero(p
->lockMask
) ){
8859 sqlite3VdbeLeave(p
);
8861 assert( rc
!=SQLITE_OK
|| nExtraDelete
==0
8862 || sqlite3_strlike("DELETE%",p
->zSql
,0)!=0
8866 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8870 sqlite3VdbeError(p
, "string or blob too big");
8872 goto abort_due_to_error
;
8874 /* Jump to here if a malloc() fails.
8877 sqlite3OomFault(db
);
8878 sqlite3VdbeError(p
, "out of memory");
8879 rc
= SQLITE_NOMEM_BKPT
;
8880 goto abort_due_to_error
;
8882 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
8885 abort_due_to_interrupt
:
8886 assert( AtomicLoad(&db
->u1
.isInterrupted
) );
8887 rc
= SQLITE_INTERRUPT
;
8888 goto abort_due_to_error
;