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
) ){
687 if( p
->flags
& MEM_Zero
) h
+= p
->u
.nZero
;
694 ** Return the symbolic name for the data type of a pMem
696 static const char *vdbeMemTypeName(Mem
*pMem
){
697 static const char *azTypes
[] = {
698 /* SQLITE_INTEGER */ "INT",
699 /* SQLITE_FLOAT */ "REAL",
700 /* SQLITE_TEXT */ "TEXT",
701 /* SQLITE_BLOB */ "BLOB",
702 /* SQLITE_NULL */ "NULL"
704 return azTypes
[sqlite3_value_type(pMem
)-1];
708 ** Execute as much of a VDBE program as we can.
709 ** This is the core of sqlite3_step().
712 Vdbe
*p
/* The VDBE */
714 Op
*aOp
= p
->aOp
; /* Copy of p->aOp */
715 Op
*pOp
= aOp
; /* Current operation */
717 Op
*pOrigOp
; /* Value of pOp at the top of the loop */
718 int nExtraDelete
= 0; /* Verifies FORDELETE and AUXDELETE flags */
719 u8 iCompareIsInit
= 0; /* iCompare is initialized */
721 int rc
= SQLITE_OK
; /* Value to return */
722 sqlite3
*db
= p
->db
; /* The database */
723 u8 resetSchemaOnFault
= 0; /* Reset schema after an error if positive */
724 u8 encoding
= ENC(db
); /* The database encoding */
725 int iCompare
= 0; /* Result of last comparison */
726 u64 nVmStep
= 0; /* Number of virtual machine steps */
727 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
728 u64 nProgressLimit
; /* Invoke xProgress() when nVmStep reaches this */
730 Mem
*aMem
= p
->aMem
; /* Copy of p->aMem */
731 Mem
*pIn1
= 0; /* 1st input operand */
732 Mem
*pIn2
= 0; /* 2nd input operand */
733 Mem
*pIn3
= 0; /* 3rd input operand */
734 Mem
*pOut
= 0; /* Output operand */
735 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
738 /*** INSERT STACK UNION HERE ***/
740 assert( p
->eVdbeState
==VDBE_RUN_STATE
); /* sqlite3_step() verifies this */
741 if( DbMaskNonZero(p
->lockMask
) ){
744 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
746 u32 iPrior
= p
->aCounter
[SQLITE_STMTSTATUS_VM_STEP
];
747 assert( 0 < db
->nProgressOps
);
748 nProgressLimit
= db
->nProgressOps
- (iPrior
% db
->nProgressOps
);
750 nProgressLimit
= LARGEST_UINT64
;
753 if( p
->rc
==SQLITE_NOMEM
){
754 /* This happens if a malloc() inside a call to sqlite3_column_text() or
755 ** sqlite3_column_text16() failed. */
758 assert( p
->rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_BUSY
);
759 testcase( p
->rc
!=SQLITE_OK
);
761 assert( p
->bIsReader
|| p
->readOnly
!=0 );
763 assert( p
->explain
==0 );
764 db
->busyHandler
.nBusy
= 0;
765 if( AtomicLoad(&db
->u1
.isInterrupted
) ) goto abort_due_to_interrupt
;
766 sqlite3VdbeIOTraceSql(p
);
768 sqlite3BeginBenignMalloc();
770 && (p
->db
->flags
& (SQLITE_VdbeListing
|SQLITE_VdbeEQP
|SQLITE_VdbeTrace
))!=0
774 sqlite3VdbePrintSql(p
);
775 if( p
->db
->flags
& SQLITE_VdbeListing
){
776 printf("VDBE Program Listing:\n");
777 for(i
=0; i
<p
->nOp
; i
++){
778 sqlite3VdbePrintOp(stdout
, i
, &aOp
[i
]);
781 if( p
->db
->flags
& SQLITE_VdbeEQP
){
782 for(i
=0; i
<p
->nOp
; i
++){
783 if( aOp
[i
].opcode
==OP_Explain
){
784 if( once
) printf("VDBE Query Plan:\n");
785 printf("%s\n", aOp
[i
].p4
.z
);
790 if( p
->db
->flags
& SQLITE_VdbeTrace
) printf("VDBE Trace:\n");
792 sqlite3EndBenignMalloc();
794 for(pOp
=&aOp
[p
->pc
]; 1; pOp
++){
795 /* Errors are detected by individual opcodes, with an immediate
796 ** jumps to abort_due_to_error. */
797 assert( rc
==SQLITE_OK
);
799 assert( pOp
>=aOp
&& pOp
<&aOp
[p
->nOp
]);
801 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
803 pnCycle
= &pOp
->nCycle
;
805 if( sqlite3NProfileCnt
==0 )
807 *pnCycle
-= sqlite3Hwtime();
810 /* Only allow tracing if SQLITE_DEBUG is defined.
813 if( db
->flags
& SQLITE_VdbeTrace
){
814 sqlite3VdbePrintOp(stdout
, (int)(pOp
- aOp
), pOp
);
815 test_trace_breakpoint((int)(pOp
- aOp
),pOp
,p
);
820 /* Check to see if we need to simulate an interrupt. This only happens
821 ** if we have a special test build.
824 if( sqlite3_interrupt_count
>0 ){
825 sqlite3_interrupt_count
--;
826 if( sqlite3_interrupt_count
==0 ){
827 sqlite3_interrupt(db
);
832 /* Sanity checking on other operands */
835 u8 opProperty
= sqlite3OpcodeProperty
[pOp
->opcode
];
836 if( (opProperty
& OPFLG_IN1
)!=0 ){
838 assert( pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
839 assert( memIsValid(&aMem
[pOp
->p1
]) );
840 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p1
]) );
841 REGISTER_TRACE(pOp
->p1
, &aMem
[pOp
->p1
]);
843 if( (opProperty
& OPFLG_IN2
)!=0 ){
845 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
846 assert( memIsValid(&aMem
[pOp
->p2
]) );
847 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p2
]) );
848 REGISTER_TRACE(pOp
->p2
, &aMem
[pOp
->p2
]);
850 if( (opProperty
& OPFLG_IN3
)!=0 ){
852 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
853 assert( memIsValid(&aMem
[pOp
->p3
]) );
854 assert( sqlite3VdbeCheckMemInvariants(&aMem
[pOp
->p3
]) );
855 REGISTER_TRACE(pOp
->p3
, &aMem
[pOp
->p3
]);
857 if( (opProperty
& OPFLG_OUT2
)!=0 ){
859 assert( pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
) );
860 memAboutToChange(p
, &aMem
[pOp
->p2
]);
862 if( (opProperty
& OPFLG_OUT3
)!=0 ){
864 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
865 memAboutToChange(p
, &aMem
[pOp
->p3
]);
873 switch( pOp
->opcode
){
875 /*****************************************************************************
876 ** What follows is a massive switch statement where each case implements a
877 ** separate instruction in the virtual machine. If we follow the usual
878 ** indentation conventions, each case should be indented by 6 spaces. But
879 ** that is a lot of wasted space on the left margin. So the code within
880 ** the switch statement will break with convention and be flush-left. Another
881 ** big comment (similar to this one) will mark the point in the code where
882 ** we transition back to normal indentation.
884 ** The formatting of each case is important. The makefile for SQLite
885 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
886 ** file looking for lines that begin with "case OP_". The opcodes.h files
887 ** will be filled with #defines that give unique integer values to each
888 ** opcode and the opcodes.c file is filled with an array of strings where
889 ** each string is the symbolic name for the corresponding opcode. If the
890 ** case statement is followed by a comment of the form "/# same as ... #/"
891 ** that comment is used to determine the particular value of the opcode.
893 ** Other keywords in the comment that follows each case are used to
894 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
895 ** Keywords include: in1, in2, in3, out2, out3. See
896 ** the mkopcodeh.awk script for additional information.
898 ** Documentation about VDBE opcodes is generated by scanning this file
899 ** for lines of that contain "Opcode:". That line and all subsequent
900 ** comment lines are used in the generation of the opcode.html documentation
905 ** Formatting is important to scripts that scan this file.
906 ** Do not deviate from the formatting style currently in use.
908 *****************************************************************************/
910 /* Opcode: Goto * P2 * * *
912 ** An unconditional jump to address P2.
913 ** The next instruction executed will be
914 ** the one at index P2 from the beginning of
917 ** The P1 parameter is not actually used by this opcode. However, it
918 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
919 ** that this Goto is the bottom of a loop and that the lines from P2 down
920 ** to the current line should be indented for EXPLAIN output.
922 case OP_Goto
: { /* jump */
925 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
926 ** means we should really jump back to the preceeding OP_ReleaseReg
929 assert( pOp
->p2
< (int)(pOp
- aOp
) );
930 assert( pOp
->p2
> 1 );
931 pOp
= &aOp
[pOp
->p2
- 2];
932 assert( pOp
[1].opcode
==OP_ReleaseReg
);
933 goto check_for_interrupt
;
937 jump_to_p2_and_check_for_interrupt
:
938 pOp
= &aOp
[pOp
->p2
- 1];
940 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
941 ** OP_VNext, or OP_SorterNext) all jump here upon
942 ** completion. Check to see if sqlite3_interrupt() has been called
943 ** or if the progress callback needs to be invoked.
945 ** This code uses unstructured "goto" statements and does not look clean.
946 ** But that is not due to sloppy coding habits. The code is written this
947 ** way for performance, to avoid having to run the interrupt and progress
948 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
949 ** faster according to "valgrind --tool=cachegrind" */
951 if( AtomicLoad(&db
->u1
.isInterrupted
) ) goto abort_due_to_interrupt
;
952 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
953 /* Call the progress callback if it is configured and the required number
954 ** of VDBE ops have been executed (either since this invocation of
955 ** sqlite3VdbeExec() or since last time the progress callback was called).
956 ** If the progress callback returns non-zero, exit the virtual machine with
957 ** a return code SQLITE_ABORT.
959 while( nVmStep
>=nProgressLimit
&& db
->xProgress
!=0 ){
960 assert( db
->nProgressOps
!=0 );
961 nProgressLimit
+= db
->nProgressOps
;
962 if( db
->xProgress(db
->pProgressArg
) ){
963 nProgressLimit
= LARGEST_UINT64
;
964 rc
= SQLITE_INTERRUPT
;
965 goto abort_due_to_error
;
973 /* Opcode: Gosub P1 P2 * * *
975 ** Write the current address onto register P1
976 ** and then jump to address P2.
978 case OP_Gosub
: { /* jump */
979 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
980 pIn1
= &aMem
[pOp
->p1
];
981 assert( VdbeMemDynamic(pIn1
)==0 );
982 memAboutToChange(p
, pIn1
);
983 pIn1
->flags
= MEM_Int
;
984 pIn1
->u
.i
= (int)(pOp
-aOp
);
985 REGISTER_TRACE(pOp
->p1
, pIn1
);
986 goto jump_to_p2_and_check_for_interrupt
;
989 /* Opcode: Return P1 P2 P3 * *
991 ** Jump to the address stored in register P1. If P1 is a return address
992 ** register, then this accomplishes a return from a subroutine.
994 ** If P3 is 1, then the jump is only taken if register P1 holds an integer
995 ** values, otherwise execution falls through to the next opcode, and the
996 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
997 ** integer or else an assert() is raised. P3 should be set to 1 when
998 ** this opcode is used in combination with OP_BeginSubrtn, and set to 0
1001 ** The value in register P1 is unchanged by this opcode.
1003 ** P2 is not used by the byte-code engine. However, if P2 is positive
1004 ** and also less than the current address, then the "EXPLAIN" output
1005 ** formatter in the CLI will indent all opcodes from the P2 opcode up
1006 ** to be not including the current Return. P2 should be the first opcode
1007 ** in the subroutine from which this opcode is returning. Thus the P2
1008 ** value is a byte-code indentation hint. See tag-20220407a in
1009 ** wherecode.c and shell.c.
1011 case OP_Return
: { /* in1 */
1012 pIn1
= &aMem
[pOp
->p1
];
1013 if( pIn1
->flags
& MEM_Int
){
1014 if( pOp
->p3
){ VdbeBranchTaken(1, 2); }
1015 pOp
= &aOp
[pIn1
->u
.i
];
1016 }else if( ALWAYS(pOp
->p3
) ){
1017 VdbeBranchTaken(0, 2);
1022 /* Opcode: InitCoroutine P1 P2 P3 * *
1024 ** Set up register P1 so that it will Yield to the coroutine
1025 ** located at address P3.
1027 ** If P2!=0 then the coroutine implementation immediately follows
1028 ** this opcode. So jump over the coroutine implementation to
1031 ** See also: EndCoroutine
1033 case OP_InitCoroutine
: { /* jump */
1034 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
1035 assert( pOp
->p2
>=0 && pOp
->p2
<p
->nOp
);
1036 assert( pOp
->p3
>=0 && pOp
->p3
<p
->nOp
);
1037 pOut
= &aMem
[pOp
->p1
];
1038 assert( !VdbeMemDynamic(pOut
) );
1039 pOut
->u
.i
= pOp
->p3
- 1;
1040 pOut
->flags
= MEM_Int
;
1041 if( pOp
->p2
==0 ) break;
1043 /* Most jump operations do a goto to this spot in order to update
1044 ** the pOp pointer. */
1046 assert( pOp
->p2
>0 ); /* There are never any jumps to instruction 0 */
1047 assert( pOp
->p2
<p
->nOp
); /* Jumps must be in range */
1048 pOp
= &aOp
[pOp
->p2
- 1];
1052 /* Opcode: EndCoroutine P1 * * * *
1054 ** The instruction at the address in register P1 is a Yield.
1055 ** Jump to the P2 parameter of that Yield.
1056 ** After the jump, register P1 becomes undefined.
1058 ** See also: InitCoroutine
1060 case OP_EndCoroutine
: { /* in1 */
1062 pIn1
= &aMem
[pOp
->p1
];
1063 assert( pIn1
->flags
==MEM_Int
);
1064 assert( pIn1
->u
.i
>=0 && pIn1
->u
.i
<p
->nOp
);
1065 pCaller
= &aOp
[pIn1
->u
.i
];
1066 assert( pCaller
->opcode
==OP_Yield
);
1067 assert( pCaller
->p2
>=0 && pCaller
->p2
<p
->nOp
);
1068 pOp
= &aOp
[pCaller
->p2
- 1];
1069 pIn1
->flags
= MEM_Undefined
;
1073 /* Opcode: Yield P1 P2 * * *
1075 ** Swap the program counter with the value in register P1. This
1076 ** has the effect of yielding to a coroutine.
1078 ** If the coroutine that is launched by this instruction ends with
1079 ** Yield or Return then continue to the next instruction. But if
1080 ** the coroutine launched by this instruction ends with
1081 ** EndCoroutine, then jump to P2 rather than continuing with the
1082 ** next instruction.
1084 ** See also: InitCoroutine
1086 case OP_Yield
: { /* in1, jump */
1088 pIn1
= &aMem
[pOp
->p1
];
1089 assert( VdbeMemDynamic(pIn1
)==0 );
1090 pIn1
->flags
= MEM_Int
;
1091 pcDest
= (int)pIn1
->u
.i
;
1092 pIn1
->u
.i
= (int)(pOp
- aOp
);
1093 REGISTER_TRACE(pOp
->p1
, pIn1
);
1098 /* Opcode: HaltIfNull P1 P2 P3 P4 P5
1099 ** Synopsis: if r[P3]=null halt
1101 ** Check the value in register P3. If it is NULL then Halt using
1102 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1103 ** value in register P3 is not NULL, then this routine is a no-op.
1104 ** The P5 parameter should be 1.
1106 case OP_HaltIfNull
: { /* in3 */
1107 pIn3
= &aMem
[pOp
->p3
];
1109 if( pOp
->p2
==OE_Abort
){ sqlite3VdbeAssertAbortable(p
); }
1111 if( (pIn3
->flags
& MEM_Null
)==0 ) break;
1112 /* Fall through into OP_Halt */
1113 /* no break */ deliberate_fall_through
1116 /* Opcode: Halt P1 P2 * P4 P5
1118 ** Exit immediately. All open cursors, etc are closed
1121 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1122 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1123 ** For errors, it can be some other value. If P1!=0 then P2 will determine
1124 ** whether or not to rollback the current transaction. Do not rollback
1125 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1126 ** then back out all changes that have occurred during this execution of the
1127 ** VDBE, but do not rollback the transaction.
1129 ** If P4 is not null then it is an error message string.
1131 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1134 ** 1: NOT NULL contraint failed: P4
1135 ** 2: UNIQUE constraint failed: P4
1136 ** 3: CHECK constraint failed: P4
1137 ** 4: FOREIGN KEY constraint failed: P4
1139 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
1142 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
1143 ** every program. So a jump past the last instruction of the program
1144 ** is the same as executing Halt.
1151 if( pOp
->p2
==OE_Abort
){ sqlite3VdbeAssertAbortable(p
); }
1154 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates
1155 ** something is wrong with the code generator. Raise an assertion in order
1156 ** to bring this to the attention of fuzzers and other testing tools. */
1157 assert( pOp
->p1
!=SQLITE_INTERNAL
);
1159 if( p
->pFrame
&& pOp
->p1
==SQLITE_OK
){
1160 /* Halt the sub-program. Return control to the parent frame. */
1162 p
->pFrame
= pFrame
->pParent
;
1164 sqlite3VdbeSetChanges(db
, p
->nChange
);
1165 pcx
= sqlite3VdbeFrameRestore(pFrame
);
1166 if( pOp
->p2
==OE_Ignore
){
1167 /* Instruction pcx is the OP_Program that invoked the sub-program
1168 ** currently being halted. If the p2 instruction of this OP_Halt
1169 ** instruction is set to OE_Ignore, then the sub-program is throwing
1170 ** an IGNORE exception. In this case jump to the address specified
1171 ** as the p2 of the calling OP_Program. */
1172 pcx
= p
->aOp
[pcx
].p2
-1;
1180 p
->errorAction
= (u8
)pOp
->p2
;
1181 assert( pOp
->p5
<=4 );
1184 static const char * const azType
[] = { "NOT NULL", "UNIQUE", "CHECK",
1186 testcase( pOp
->p5
==1 );
1187 testcase( pOp
->p5
==2 );
1188 testcase( pOp
->p5
==3 );
1189 testcase( pOp
->p5
==4 );
1190 sqlite3VdbeError(p
, "%s constraint failed", azType
[pOp
->p5
-1]);
1192 p
->zErrMsg
= sqlite3MPrintf(db
, "%z: %s", p
->zErrMsg
, pOp
->p4
.z
);
1195 sqlite3VdbeError(p
, "%s", pOp
->p4
.z
);
1197 pcx
= (int)(pOp
- aOp
);
1198 sqlite3_log(pOp
->p1
, "abort at %d in [%s]: %s", pcx
, p
->zSql
, p
->zErrMsg
);
1200 rc
= sqlite3VdbeHalt(p
);
1201 assert( rc
==SQLITE_BUSY
|| rc
==SQLITE_OK
|| rc
==SQLITE_ERROR
);
1202 if( rc
==SQLITE_BUSY
){
1203 p
->rc
= SQLITE_BUSY
;
1205 assert( rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_CONSTRAINT
);
1206 assert( rc
==SQLITE_OK
|| db
->nDeferredCons
>0 || db
->nDeferredImmCons
>0 );
1207 rc
= p
->rc
? SQLITE_ERROR
: SQLITE_DONE
;
1212 /* Opcode: Integer P1 P2 * * *
1213 ** Synopsis: r[P2]=P1
1215 ** The 32-bit integer value P1 is written into register P2.
1217 case OP_Integer
: { /* out2 */
1218 pOut
= out2Prerelease(p
, pOp
);
1219 pOut
->u
.i
= pOp
->p1
;
1223 /* Opcode: Int64 * P2 * P4 *
1224 ** Synopsis: r[P2]=P4
1226 ** P4 is a pointer to a 64-bit integer value.
1227 ** Write that value into register P2.
1229 case OP_Int64
: { /* out2 */
1230 pOut
= out2Prerelease(p
, pOp
);
1231 assert( pOp
->p4
.pI64
!=0 );
1232 pOut
->u
.i
= *pOp
->p4
.pI64
;
1236 #ifndef SQLITE_OMIT_FLOATING_POINT
1237 /* Opcode: Real * P2 * P4 *
1238 ** Synopsis: r[P2]=P4
1240 ** P4 is a pointer to a 64-bit floating point value.
1241 ** Write that value into register P2.
1243 case OP_Real
: { /* same as TK_FLOAT, out2 */
1244 pOut
= out2Prerelease(p
, pOp
);
1245 pOut
->flags
= MEM_Real
;
1246 assert( !sqlite3IsNaN(*pOp
->p4
.pReal
) );
1247 pOut
->u
.r
= *pOp
->p4
.pReal
;
1252 /* Opcode: String8 * P2 * P4 *
1253 ** Synopsis: r[P2]='P4'
1255 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
1256 ** into a String opcode before it is executed for the first time. During
1257 ** this transformation, the length of string P4 is computed and stored
1258 ** as the P1 parameter.
1260 case OP_String8
: { /* same as TK_STRING, out2 */
1261 assert( pOp
->p4
.z
!=0 );
1262 pOut
= out2Prerelease(p
, pOp
);
1263 pOp
->p1
= sqlite3Strlen30(pOp
->p4
.z
);
1265 #ifndef SQLITE_OMIT_UTF16
1266 if( encoding
!=SQLITE_UTF8
){
1267 rc
= sqlite3VdbeMemSetStr(pOut
, pOp
->p4
.z
, -1, SQLITE_UTF8
, SQLITE_STATIC
);
1268 assert( rc
==SQLITE_OK
|| rc
==SQLITE_TOOBIG
);
1269 if( rc
) goto too_big
;
1270 if( SQLITE_OK
!=sqlite3VdbeChangeEncoding(pOut
, encoding
) ) goto no_mem
;
1271 assert( pOut
->szMalloc
>0 && pOut
->zMalloc
==pOut
->z
);
1272 assert( VdbeMemDynamic(pOut
)==0 );
1274 pOut
->flags
|= MEM_Static
;
1275 if( pOp
->p4type
==P4_DYNAMIC
){
1276 sqlite3DbFree(db
, pOp
->p4
.z
);
1278 pOp
->p4type
= P4_DYNAMIC
;
1279 pOp
->p4
.z
= pOut
->z
;
1283 if( pOp
->p1
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1286 pOp
->opcode
= OP_String
;
1287 assert( rc
==SQLITE_OK
);
1288 /* Fall through to the next case, OP_String */
1289 /* no break */ deliberate_fall_through
1292 /* Opcode: String P1 P2 P3 P4 P5
1293 ** Synopsis: r[P2]='P4' (len=P1)
1295 ** The string value P4 of length P1 (bytes) is stored in register P2.
1297 ** If P3 is not zero and the content of register P3 is equal to P5, then
1298 ** the datatype of the register P2 is converted to BLOB. The content is
1299 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
1300 ** of a string, as if it had been CAST. In other words:
1302 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
1304 case OP_String
: { /* out2 */
1305 assert( pOp
->p4
.z
!=0 );
1306 pOut
= out2Prerelease(p
, pOp
);
1307 pOut
->flags
= MEM_Str
|MEM_Static
|MEM_Term
;
1308 pOut
->z
= pOp
->p4
.z
;
1310 pOut
->enc
= encoding
;
1311 UPDATE_MAX_BLOBSIZE(pOut
);
1312 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
1314 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
1315 pIn3
= &aMem
[pOp
->p3
];
1316 assert( pIn3
->flags
& MEM_Int
);
1317 if( pIn3
->u
.i
==pOp
->p5
) pOut
->flags
= MEM_Blob
|MEM_Static
|MEM_Term
;
1323 /* Opcode: BeginSubrtn * P2 * * *
1324 ** Synopsis: r[P2]=NULL
1326 ** Mark the beginning of a subroutine that can be entered in-line
1327 ** or that can be called using OP_Gosub. The subroutine should
1328 ** be terminated by an OP_Return instruction that has a P1 operand that
1329 ** is the same as the P2 operand to this opcode and that has P3 set to 1.
1330 ** If the subroutine is entered in-line, then the OP_Return will simply
1331 ** fall through. But if the subroutine is entered using OP_Gosub, then
1332 ** the OP_Return will jump back to the first instruction after the OP_Gosub.
1334 ** This routine works by loading a NULL into the P2 register. When the
1335 ** return address register contains a NULL, the OP_Return instruction is
1336 ** a no-op that simply falls through to the next instruction (assuming that
1337 ** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
1338 ** entered in-line, then the OP_Return will cause in-line execution to
1339 ** continue. But if the subroutine is entered via OP_Gosub, then the
1340 ** OP_Return will cause a return to the address following the OP_Gosub.
1342 ** This opcode is identical to OP_Null. It has a different name
1343 ** only to make the byte code easier to read and verify.
1345 /* Opcode: Null P1 P2 P3 * *
1346 ** Synopsis: r[P2..P3]=NULL
1348 ** Write a NULL into registers P2. If P3 greater than P2, then also write
1349 ** NULL into register P3 and every register in between P2 and P3. If P3
1350 ** is less than P2 (typically P3 is zero) then only register P2 is
1353 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1354 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1357 case OP_BeginSubrtn
:
1358 case OP_Null
: { /* out2 */
1361 pOut
= out2Prerelease(p
, pOp
);
1362 cnt
= pOp
->p3
-pOp
->p2
;
1363 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
1364 pOut
->flags
= nullFlag
= pOp
->p1
? (MEM_Null
|MEM_Cleared
) : MEM_Null
;
1371 memAboutToChange(p
, pOut
);
1372 sqlite3VdbeMemSetNull(pOut
);
1373 pOut
->flags
= nullFlag
;
1380 /* Opcode: SoftNull P1 * * * *
1381 ** Synopsis: r[P1]=NULL
1383 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1384 ** instruction, but do not free any string or blob memory associated with
1385 ** the register, so that if the value was a string or blob that was
1386 ** previously copied using OP_SCopy, the copies will continue to be valid.
1389 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
1390 pOut
= &aMem
[pOp
->p1
];
1391 pOut
->flags
= (pOut
->flags
&~(MEM_Undefined
|MEM_AffMask
))|MEM_Null
;
1395 /* Opcode: Blob P1 P2 * P4 *
1396 ** Synopsis: r[P2]=P4 (len=P1)
1398 ** P4 points to a blob of data P1 bytes long. Store this
1399 ** blob in register P2. If P4 is a NULL pointer, then construct
1400 ** a zero-filled blob that is P1 bytes long in P2.
1402 case OP_Blob
: { /* out2 */
1403 assert( pOp
->p1
<= SQLITE_MAX_LENGTH
);
1404 pOut
= out2Prerelease(p
, pOp
);
1406 sqlite3VdbeMemSetZeroBlob(pOut
, pOp
->p1
);
1407 if( sqlite3VdbeMemExpandBlob(pOut
) ) goto no_mem
;
1409 sqlite3VdbeMemSetStr(pOut
, pOp
->p4
.z
, pOp
->p1
, 0, 0);
1411 pOut
->enc
= encoding
;
1412 UPDATE_MAX_BLOBSIZE(pOut
);
1416 /* Opcode: Variable P1 P2 * P4 *
1417 ** Synopsis: r[P2]=parameter(P1,P4)
1419 ** Transfer the values of bound parameter P1 into register P2
1421 ** If the parameter is named, then its name appears in P4.
1422 ** The P4 value is used by sqlite3_bind_parameter_name().
1424 case OP_Variable
: { /* out2 */
1425 Mem
*pVar
; /* Value being transferred */
1427 assert( pOp
->p1
>0 && pOp
->p1
<=p
->nVar
);
1428 assert( pOp
->p4
.z
==0 || pOp
->p4
.z
==sqlite3VListNumToName(p
->pVList
,pOp
->p1
) );
1429 pVar
= &p
->aVar
[pOp
->p1
- 1];
1430 if( sqlite3VdbeMemTooBig(pVar
) ){
1433 pOut
= &aMem
[pOp
->p2
];
1434 if( VdbeMemDynamic(pOut
) ) sqlite3VdbeMemSetNull(pOut
);
1435 memcpy(pOut
, pVar
, MEMCELLSIZE
);
1436 pOut
->flags
&= ~(MEM_Dyn
|MEM_Ephem
);
1437 pOut
->flags
|= MEM_Static
|MEM_FromBind
;
1438 UPDATE_MAX_BLOBSIZE(pOut
);
1442 /* Opcode: Move P1 P2 P3 * *
1443 ** Synopsis: r[P2@P3]=r[P1@P3]
1445 ** Move the P3 values in register P1..P1+P3-1 over into
1446 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
1447 ** left holding a NULL. It is an error for register ranges
1448 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1449 ** for P3 to be less than 1.
1452 int n
; /* Number of registers left to copy */
1453 int p1
; /* Register to copy from */
1454 int p2
; /* Register to copy to */
1459 assert( n
>0 && p1
>0 && p2
>0 );
1460 assert( p1
+n
<=p2
|| p2
+n
<=p1
);
1465 assert( pOut
<=&aMem
[(p
->nMem
+1 - p
->nCursor
)] );
1466 assert( pIn1
<=&aMem
[(p
->nMem
+1 - p
->nCursor
)] );
1467 assert( memIsValid(pIn1
) );
1468 memAboutToChange(p
, pOut
);
1469 sqlite3VdbeMemMove(pOut
, pIn1
);
1471 pIn1
->pScopyFrom
= 0;
1473 for(i
=1; i
<p
->nMem
; i
++){
1474 if( aMem
[i
].pScopyFrom
==pIn1
){
1475 aMem
[i
].pScopyFrom
= pOut
;
1480 Deephemeralize(pOut
);
1481 REGISTER_TRACE(p2
++, pOut
);
1488 /* Opcode: Copy P1 P2 P3 * P5
1489 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
1491 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
1493 ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
1494 ** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
1495 ** be merged. The 0x0001 bit is used by the query planner and does not
1496 ** come into play during query execution.
1498 ** This instruction makes a deep copy of the value. A duplicate
1499 ** is made of any string or blob constant. See also OP_SCopy.
1505 pIn1
= &aMem
[pOp
->p1
];
1506 pOut
= &aMem
[pOp
->p2
];
1507 assert( pOut
!=pIn1
);
1509 memAboutToChange(p
, pOut
);
1510 sqlite3VdbeMemShallowCopy(pOut
, pIn1
, MEM_Ephem
);
1511 Deephemeralize(pOut
);
1512 if( (pOut
->flags
& MEM_Subtype
)!=0 && (pOp
->p5
& 0x0002)!=0 ){
1513 pOut
->flags
&= ~MEM_Subtype
;
1516 pOut
->pScopyFrom
= 0;
1518 REGISTER_TRACE(pOp
->p2
+pOp
->p3
-n
, pOut
);
1519 if( (n
--)==0 ) break;
1526 /* Opcode: SCopy P1 P2 * * *
1527 ** Synopsis: r[P2]=r[P1]
1529 ** Make a shallow copy of register P1 into register P2.
1531 ** This instruction makes a shallow copy of the value. If the value
1532 ** is a string or blob, then the copy is only a pointer to the
1533 ** original and hence if the original changes so will the copy.
1534 ** Worse, if the original is deallocated, the copy becomes invalid.
1535 ** Thus the program must guarantee that the original will not change
1536 ** during the lifetime of the copy. Use OP_Copy to make a complete
1539 case OP_SCopy
: { /* out2 */
1540 pIn1
= &aMem
[pOp
->p1
];
1541 pOut
= &aMem
[pOp
->p2
];
1542 assert( pOut
!=pIn1
);
1543 sqlite3VdbeMemShallowCopy(pOut
, pIn1
, MEM_Ephem
);
1545 pOut
->pScopyFrom
= pIn1
;
1546 pOut
->mScopyFlags
= pIn1
->flags
;
1551 /* Opcode: IntCopy P1 P2 * * *
1552 ** Synopsis: r[P2]=r[P1]
1554 ** Transfer the integer value held in register P1 into register P2.
1556 ** This is an optimized version of SCopy that works only for integer
1559 case OP_IntCopy
: { /* out2 */
1560 pIn1
= &aMem
[pOp
->p1
];
1561 assert( (pIn1
->flags
& MEM_Int
)!=0 );
1562 pOut
= &aMem
[pOp
->p2
];
1563 sqlite3VdbeMemSetInt64(pOut
, pIn1
->u
.i
);
1567 /* Opcode: FkCheck * * * * *
1569 ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
1570 ** foreign key constraint violations. If there are no foreign key
1571 ** constraint violations, this is a no-op.
1573 ** FK constraint violations are also checked when the prepared statement
1574 ** exits. This opcode is used to raise foreign key constraint errors prior
1575 ** to returning results such as a row change count or the result of a
1576 ** RETURNING clause.
1579 if( (rc
= sqlite3VdbeCheckFk(p
,0))!=SQLITE_OK
){
1580 goto abort_due_to_error
;
1585 /* Opcode: ResultRow P1 P2 * * *
1586 ** Synopsis: output=r[P1@P2]
1588 ** The registers P1 through P1+P2-1 contain a single row of
1589 ** results. This opcode causes the sqlite3_step() call to terminate
1590 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1591 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
1594 case OP_ResultRow
: {
1595 assert( p
->nResColumn
==pOp
->p2
);
1596 assert( pOp
->p1
>0 || CORRUPT_DB
);
1597 assert( pOp
->p1
+pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
)+1 );
1599 p
->cacheCtr
= (p
->cacheCtr
+ 2)|1;
1600 p
->pResultRow
= &aMem
[pOp
->p1
];
1603 Mem
*pMem
= p
->pResultRow
;
1605 for(i
=0; i
<pOp
->p2
; i
++){
1606 assert( memIsValid(&pMem
[i
]) );
1607 REGISTER_TRACE(pOp
->p1
+i
, &pMem
[i
]);
1608 /* The registers in the result will not be used again when the
1609 ** prepared statement restarts. This is because sqlite3_column()
1610 ** APIs might have caused type conversions of made other changes to
1611 ** the register values. Therefore, we can go ahead and break any
1612 ** OP_SCopy dependencies. */
1613 pMem
[i
].pScopyFrom
= 0;
1617 if( db
->mallocFailed
) goto no_mem
;
1618 if( db
->mTrace
& SQLITE_TRACE_ROW
){
1619 db
->trace
.xV2(SQLITE_TRACE_ROW
, db
->pTraceArg
, p
, 0);
1621 p
->pc
= (int)(pOp
- aOp
) + 1;
1626 /* Opcode: Concat P1 P2 P3 * *
1627 ** Synopsis: r[P3]=r[P2]+r[P1]
1629 ** Add the text in register P1 onto the end of the text in
1630 ** register P2 and store the result in register P3.
1631 ** If either the P1 or P2 text are NULL then store NULL in P3.
1635 ** It is illegal for P1 and P3 to be the same register. Sometimes,
1636 ** if P3 is the same register as P2, the implementation is able
1637 ** to avoid a memcpy().
1639 case OP_Concat
: { /* same as TK_CONCAT, in1, in2, out3 */
1640 i64 nByte
; /* Total size of the output string or blob */
1641 u16 flags1
; /* Initial flags for P1 */
1642 u16 flags2
; /* Initial flags for P2 */
1644 pIn1
= &aMem
[pOp
->p1
];
1645 pIn2
= &aMem
[pOp
->p2
];
1646 pOut
= &aMem
[pOp
->p3
];
1647 testcase( pOut
==pIn2
);
1648 assert( pIn1
!=pOut
);
1649 flags1
= pIn1
->flags
;
1650 testcase( flags1
& MEM_Null
);
1651 testcase( pIn2
->flags
& MEM_Null
);
1652 if( (flags1
| pIn2
->flags
) & MEM_Null
){
1653 sqlite3VdbeMemSetNull(pOut
);
1656 if( (flags1
& (MEM_Str
|MEM_Blob
))==0 ){
1657 if( sqlite3VdbeMemStringify(pIn1
,encoding
,0) ) goto no_mem
;
1658 flags1
= pIn1
->flags
& ~MEM_Str
;
1659 }else if( (flags1
& MEM_Zero
)!=0 ){
1660 if( sqlite3VdbeMemExpandBlob(pIn1
) ) goto no_mem
;
1661 flags1
= pIn1
->flags
& ~MEM_Str
;
1663 flags2
= pIn2
->flags
;
1664 if( (flags2
& (MEM_Str
|MEM_Blob
))==0 ){
1665 if( sqlite3VdbeMemStringify(pIn2
,encoding
,0) ) goto no_mem
;
1666 flags2
= pIn2
->flags
& ~MEM_Str
;
1667 }else if( (flags2
& MEM_Zero
)!=0 ){
1668 if( sqlite3VdbeMemExpandBlob(pIn2
) ) goto no_mem
;
1669 flags2
= pIn2
->flags
& ~MEM_Str
;
1671 nByte
= pIn1
->n
+ pIn2
->n
;
1672 if( nByte
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
1675 if( sqlite3VdbeMemGrow(pOut
, (int)nByte
+2, pOut
==pIn2
) ){
1678 MemSetTypeFlag(pOut
, MEM_Str
);
1680 memcpy(pOut
->z
, pIn2
->z
, pIn2
->n
);
1681 assert( (pIn2
->flags
& MEM_Dyn
) == (flags2
& MEM_Dyn
) );
1682 pIn2
->flags
= flags2
;
1684 memcpy(&pOut
->z
[pIn2
->n
], pIn1
->z
, pIn1
->n
);
1685 assert( (pIn1
->flags
& MEM_Dyn
) == (flags1
& MEM_Dyn
) );
1686 pIn1
->flags
= flags1
;
1687 if( encoding
>SQLITE_UTF8
) nByte
&= ~1;
1689 pOut
->z
[nByte
+1] = 0;
1690 pOut
->flags
|= MEM_Term
;
1691 pOut
->n
= (int)nByte
;
1692 pOut
->enc
= encoding
;
1693 UPDATE_MAX_BLOBSIZE(pOut
);
1697 /* Opcode: Add P1 P2 P3 * *
1698 ** Synopsis: r[P3]=r[P1]+r[P2]
1700 ** Add the value in register P1 to the value in register P2
1701 ** and store the result in register P3.
1702 ** If either input is NULL, the result is NULL.
1704 /* Opcode: Multiply P1 P2 P3 * *
1705 ** Synopsis: r[P3]=r[P1]*r[P2]
1708 ** Multiply the value in register P1 by the value in register P2
1709 ** and store the result in register P3.
1710 ** If either input is NULL, the result is NULL.
1712 /* Opcode: Subtract P1 P2 P3 * *
1713 ** Synopsis: r[P3]=r[P2]-r[P1]
1715 ** Subtract the value in register P1 from the value in register P2
1716 ** and store the result in register P3.
1717 ** If either input is NULL, the result is NULL.
1719 /* Opcode: Divide P1 P2 P3 * *
1720 ** Synopsis: r[P3]=r[P2]/r[P1]
1722 ** Divide the value in register P1 by the value in register P2
1723 ** and store the result in register P3 (P3=P2/P1). If the value in
1724 ** register P1 is zero, then the result is NULL. If either input is
1725 ** NULL, the result is NULL.
1727 /* Opcode: Remainder P1 P2 P3 * *
1728 ** Synopsis: r[P3]=r[P2]%r[P1]
1730 ** Compute the remainder after integer register P2 is divided by
1731 ** register P1 and store the result in register P3.
1732 ** If the value in register P1 is zero the result is NULL.
1733 ** If either operand is NULL, the result is NULL.
1735 case OP_Add
: /* same as TK_PLUS, in1, in2, out3 */
1736 case OP_Subtract
: /* same as TK_MINUS, in1, in2, out3 */
1737 case OP_Multiply
: /* same as TK_STAR, in1, in2, out3 */
1738 case OP_Divide
: /* same as TK_SLASH, in1, in2, out3 */
1739 case OP_Remainder
: { /* same as TK_REM, in1, in2, out3 */
1740 u16 type1
; /* Numeric type of left operand */
1741 u16 type2
; /* Numeric type of right operand */
1742 i64 iA
; /* Integer value of left operand */
1743 i64 iB
; /* Integer value of right operand */
1744 double rA
; /* Real value of left operand */
1745 double rB
; /* Real value of right operand */
1747 pIn1
= &aMem
[pOp
->p1
];
1748 type1
= pIn1
->flags
;
1749 pIn2
= &aMem
[pOp
->p2
];
1750 type2
= pIn2
->flags
;
1751 pOut
= &aMem
[pOp
->p3
];
1752 if( (type1
& type2
& MEM_Int
)!=0 ){
1756 switch( pOp
->opcode
){
1757 case OP_Add
: if( sqlite3AddInt64(&iB
,iA
) ) goto fp_math
; break;
1758 case OP_Subtract
: if( sqlite3SubInt64(&iB
,iA
) ) goto fp_math
; break;
1759 case OP_Multiply
: if( sqlite3MulInt64(&iB
,iA
) ) goto fp_math
; break;
1761 if( iA
==0 ) goto arithmetic_result_is_null
;
1762 if( iA
==-1 && iB
==SMALLEST_INT64
) goto fp_math
;
1767 if( iA
==0 ) goto arithmetic_result_is_null
;
1768 if( iA
==-1 ) iA
= 1;
1774 MemSetTypeFlag(pOut
, MEM_Int
);
1775 }else if( ((type1
| type2
) & MEM_Null
)!=0 ){
1776 goto arithmetic_result_is_null
;
1778 type1
= numericType(pIn1
);
1779 type2
= numericType(pIn2
);
1780 if( (type1
& type2
& MEM_Int
)!=0 ) goto int_math
;
1782 rA
= sqlite3VdbeRealValue(pIn1
);
1783 rB
= sqlite3VdbeRealValue(pIn2
);
1784 switch( pOp
->opcode
){
1785 case OP_Add
: rB
+= rA
; break;
1786 case OP_Subtract
: rB
-= rA
; break;
1787 case OP_Multiply
: rB
*= rA
; break;
1789 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
1790 if( rA
==(double)0 ) goto arithmetic_result_is_null
;
1795 iA
= sqlite3VdbeIntValue(pIn1
);
1796 iB
= sqlite3VdbeIntValue(pIn2
);
1797 if( iA
==0 ) goto arithmetic_result_is_null
;
1798 if( iA
==-1 ) iA
= 1;
1799 rB
= (double)(iB
% iA
);
1803 #ifdef SQLITE_OMIT_FLOATING_POINT
1805 MemSetTypeFlag(pOut
, MEM_Int
);
1807 if( sqlite3IsNaN(rB
) ){
1808 goto arithmetic_result_is_null
;
1811 MemSetTypeFlag(pOut
, MEM_Real
);
1816 arithmetic_result_is_null
:
1817 sqlite3VdbeMemSetNull(pOut
);
1821 /* Opcode: CollSeq P1 * * P4
1823 ** P4 is a pointer to a CollSeq object. If the next call to a user function
1824 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1825 ** be returned. This is used by the built-in min(), max() and nullif()
1828 ** If P1 is not zero, then it is a register that a subsequent min() or
1829 ** max() aggregate will set to 1 if the current row is not the minimum or
1830 ** maximum. The P1 register is initialized to 0 by this instruction.
1832 ** The interface used by the implementation of the aforementioned functions
1833 ** to retrieve the collation sequence set by this opcode is not available
1834 ** publicly. Only built-in functions have access to this feature.
1837 assert( pOp
->p4type
==P4_COLLSEQ
);
1839 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p1
], 0);
1844 /* Opcode: BitAnd P1 P2 P3 * *
1845 ** Synopsis: r[P3]=r[P1]&r[P2]
1847 ** Take the bit-wise AND of the values in register P1 and P2 and
1848 ** store the result in register P3.
1849 ** If either input is NULL, the result is NULL.
1851 /* Opcode: BitOr P1 P2 P3 * *
1852 ** Synopsis: r[P3]=r[P1]|r[P2]
1854 ** Take the bit-wise OR of the values in register P1 and P2 and
1855 ** store the result in register P3.
1856 ** If either input is NULL, the result is NULL.
1858 /* Opcode: ShiftLeft P1 P2 P3 * *
1859 ** Synopsis: r[P3]=r[P2]<<r[P1]
1861 ** Shift the integer value in register P2 to the left by the
1862 ** number of bits specified by the integer in register P1.
1863 ** Store the result in register P3.
1864 ** If either input is NULL, the result is NULL.
1866 /* Opcode: ShiftRight P1 P2 P3 * *
1867 ** Synopsis: r[P3]=r[P2]>>r[P1]
1869 ** Shift the integer value in register P2 to the right by the
1870 ** number of bits specified by the integer in register P1.
1871 ** Store the result in register P3.
1872 ** If either input is NULL, the result is NULL.
1874 case OP_BitAnd
: /* same as TK_BITAND, in1, in2, out3 */
1875 case OP_BitOr
: /* same as TK_BITOR, in1, in2, out3 */
1876 case OP_ShiftLeft
: /* same as TK_LSHIFT, in1, in2, out3 */
1877 case OP_ShiftRight
: { /* same as TK_RSHIFT, in1, in2, out3 */
1883 pIn1
= &aMem
[pOp
->p1
];
1884 pIn2
= &aMem
[pOp
->p2
];
1885 pOut
= &aMem
[pOp
->p3
];
1886 if( (pIn1
->flags
| pIn2
->flags
) & MEM_Null
){
1887 sqlite3VdbeMemSetNull(pOut
);
1890 iA
= sqlite3VdbeIntValue(pIn2
);
1891 iB
= sqlite3VdbeIntValue(pIn1
);
1893 if( op
==OP_BitAnd
){
1895 }else if( op
==OP_BitOr
){
1898 assert( op
==OP_ShiftRight
|| op
==OP_ShiftLeft
);
1900 /* If shifting by a negative amount, shift in the other direction */
1902 assert( OP_ShiftRight
==OP_ShiftLeft
+1 );
1903 op
= 2*OP_ShiftLeft
+ 1 - op
;
1904 iB
= iB
>(-64) ? -iB
: 64;
1908 iA
= (iA
>=0 || op
==OP_ShiftLeft
) ? 0 : -1;
1910 memcpy(&uA
, &iA
, sizeof(uA
));
1911 if( op
==OP_ShiftLeft
){
1915 /* Sign-extend on a right shift of a negative number */
1916 if( iA
<0 ) uA
|= ((((u64
)0xffffffff)<<32)|0xffffffff) << (64-iB
);
1918 memcpy(&iA
, &uA
, sizeof(iA
));
1922 MemSetTypeFlag(pOut
, MEM_Int
);
1926 /* Opcode: AddImm P1 P2 * * *
1927 ** Synopsis: r[P1]=r[P1]+P2
1929 ** Add the constant P2 to the value in register P1.
1930 ** The result is always an integer.
1932 ** To force any register to be an integer, just add 0.
1934 case OP_AddImm
: { /* in1 */
1935 pIn1
= &aMem
[pOp
->p1
];
1936 memAboutToChange(p
, pIn1
);
1937 sqlite3VdbeMemIntegerify(pIn1
);
1938 pIn1
->u
.i
+= pOp
->p2
;
1942 /* Opcode: MustBeInt P1 P2 * * *
1944 ** Force the value in register P1 to be an integer. If the value
1945 ** in P1 is not an integer and cannot be converted into an integer
1946 ** without data loss, then jump immediately to P2, or if P2==0
1947 ** raise an SQLITE_MISMATCH exception.
1949 case OP_MustBeInt
: { /* jump, in1 */
1950 pIn1
= &aMem
[pOp
->p1
];
1951 if( (pIn1
->flags
& MEM_Int
)==0 ){
1952 applyAffinity(pIn1
, SQLITE_AFF_NUMERIC
, encoding
);
1953 if( (pIn1
->flags
& MEM_Int
)==0 ){
1954 VdbeBranchTaken(1, 2);
1956 rc
= SQLITE_MISMATCH
;
1957 goto abort_due_to_error
;
1963 VdbeBranchTaken(0, 2);
1964 MemSetTypeFlag(pIn1
, MEM_Int
);
1968 #ifndef SQLITE_OMIT_FLOATING_POINT
1969 /* Opcode: RealAffinity P1 * * * *
1971 ** If register P1 holds an integer convert it to a real value.
1973 ** This opcode is used when extracting information from a column that
1974 ** has REAL affinity. Such column values may still be stored as
1975 ** integers, for space efficiency, but after extraction we want them
1976 ** to have only a real value.
1978 case OP_RealAffinity
: { /* in1 */
1979 pIn1
= &aMem
[pOp
->p1
];
1980 if( pIn1
->flags
& (MEM_Int
|MEM_IntReal
) ){
1981 testcase( pIn1
->flags
& MEM_Int
);
1982 testcase( pIn1
->flags
& MEM_IntReal
);
1983 sqlite3VdbeMemRealify(pIn1
);
1984 REGISTER_TRACE(pOp
->p1
, pIn1
);
1990 #ifndef SQLITE_OMIT_CAST
1991 /* Opcode: Cast P1 P2 * * *
1992 ** Synopsis: affinity(r[P1])
1994 ** Force the value in register P1 to be the type defined by P2.
1997 ** <li> P2=='A' → BLOB
1998 ** <li> P2=='B' → TEXT
1999 ** <li> P2=='C' → NUMERIC
2000 ** <li> P2=='D' → INTEGER
2001 ** <li> P2=='E' → REAL
2004 ** A NULL value is not changed by this routine. It remains NULL.
2006 case OP_Cast
: { /* in1 */
2007 assert( pOp
->p2
>=SQLITE_AFF_BLOB
&& pOp
->p2
<=SQLITE_AFF_REAL
);
2008 testcase( pOp
->p2
==SQLITE_AFF_TEXT
);
2009 testcase( pOp
->p2
==SQLITE_AFF_BLOB
);
2010 testcase( pOp
->p2
==SQLITE_AFF_NUMERIC
);
2011 testcase( pOp
->p2
==SQLITE_AFF_INTEGER
);
2012 testcase( pOp
->p2
==SQLITE_AFF_REAL
);
2013 pIn1
= &aMem
[pOp
->p1
];
2014 memAboutToChange(p
, pIn1
);
2015 rc
= ExpandBlob(pIn1
);
2016 if( rc
) goto abort_due_to_error
;
2017 rc
= sqlite3VdbeMemCast(pIn1
, pOp
->p2
, encoding
);
2018 if( rc
) goto abort_due_to_error
;
2019 UPDATE_MAX_BLOBSIZE(pIn1
);
2020 REGISTER_TRACE(pOp
->p1
, pIn1
);
2023 #endif /* SQLITE_OMIT_CAST */
2025 /* Opcode: Eq P1 P2 P3 P4 P5
2026 ** Synopsis: IF r[P3]==r[P1]
2028 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
2029 ** jump to address P2.
2031 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
2032 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
2033 ** to coerce both inputs according to this affinity before the
2034 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2035 ** affinity is used. Note that the affinity conversions are stored
2036 ** back into the input registers P1 and P3. So this opcode can cause
2037 ** persistent changes to registers P1 and P3.
2039 ** Once any conversions have taken place, and neither value is NULL,
2040 ** the values are compared. If both values are blobs then memcmp() is
2041 ** used to determine the results of the comparison. If both values
2042 ** are text, then the appropriate collating function specified in
2043 ** P4 is used to do the comparison. If P4 is not specified then
2044 ** memcmp() is used to compare text string. If both values are
2045 ** numeric, then a numeric comparison is used. If the two values
2046 ** are of different types, then numbers are considered less than
2047 ** strings and strings are considered less than blobs.
2049 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
2050 ** true or false and is never NULL. If both operands are NULL then the result
2051 ** of comparison is true. If either operand is NULL then the result is false.
2052 ** If neither operand is NULL the result is the same as it would be if
2053 ** the SQLITE_NULLEQ flag were omitted from P5.
2055 ** This opcode saves the result of comparison for use by the new
2058 /* Opcode: Ne P1 P2 P3 P4 P5
2059 ** Synopsis: IF r[P3]!=r[P1]
2061 ** This works just like the Eq opcode except that the jump is taken if
2062 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for
2063 ** additional information.
2065 /* Opcode: Lt P1 P2 P3 P4 P5
2066 ** Synopsis: IF r[P3]<r[P1]
2068 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
2069 ** jump to address P2.
2071 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
2072 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
2073 ** bit is clear then fall through if either operand is NULL.
2075 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
2076 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
2077 ** to coerce both inputs according to this affinity before the
2078 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2079 ** affinity is used. Note that the affinity conversions are stored
2080 ** back into the input registers P1 and P3. So this opcode can cause
2081 ** persistent changes to registers P1 and P3.
2083 ** Once any conversions have taken place, and neither value is NULL,
2084 ** the values are compared. If both values are blobs then memcmp() is
2085 ** used to determine the results of the comparison. If both values
2086 ** are text, then the appropriate collating function specified in
2087 ** P4 is used to do the comparison. If P4 is not specified then
2088 ** memcmp() is used to compare text string. If both values are
2089 ** numeric, then a numeric comparison is used. If the two values
2090 ** are of different types, then numbers are considered less than
2091 ** strings and strings are considered less than blobs.
2093 ** This opcode saves the result of comparison for use by the new
2096 /* Opcode: Le P1 P2 P3 P4 P5
2097 ** Synopsis: IF r[P3]<=r[P1]
2099 ** This works just like the Lt opcode except that the jump is taken if
2100 ** the content of register P3 is less than or equal to the content of
2101 ** register P1. See the Lt opcode for additional information.
2103 /* Opcode: Gt P1 P2 P3 P4 P5
2104 ** Synopsis: IF r[P3]>r[P1]
2106 ** This works just like the Lt opcode except that the jump is taken if
2107 ** the content of register P3 is greater than the content of
2108 ** register P1. See the Lt opcode for additional information.
2110 /* Opcode: Ge P1 P2 P3 P4 P5
2111 ** Synopsis: IF r[P3]>=r[P1]
2113 ** This works just like the Lt opcode except that the jump is taken if
2114 ** the content of register P3 is greater than or equal to the content of
2115 ** register P1. See the Lt opcode for additional information.
2117 case OP_Eq
: /* same as TK_EQ, jump, in1, in3 */
2118 case OP_Ne
: /* same as TK_NE, jump, in1, in3 */
2119 case OP_Lt
: /* same as TK_LT, jump, in1, in3 */
2120 case OP_Le
: /* same as TK_LE, jump, in1, in3 */
2121 case OP_Gt
: /* same as TK_GT, jump, in1, in3 */
2122 case OP_Ge
: { /* same as TK_GE, jump, in1, in3 */
2123 int res
, res2
; /* Result of the comparison of pIn1 against pIn3 */
2124 char affinity
; /* Affinity to use for comparison */
2125 u16 flags1
; /* Copy of initial value of pIn1->flags */
2126 u16 flags3
; /* Copy of initial value of pIn3->flags */
2128 pIn1
= &aMem
[pOp
->p1
];
2129 pIn3
= &aMem
[pOp
->p3
];
2130 flags1
= pIn1
->flags
;
2131 flags3
= pIn3
->flags
;
2132 if( (flags1
& flags3
& MEM_Int
)!=0 ){
2133 /* Common case of comparison of two integers */
2134 if( pIn3
->u
.i
> pIn1
->u
.i
){
2135 if( sqlite3aGTb
[pOp
->opcode
] ){
2136 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2140 VVA_ONLY( iCompareIsInit
= 1; )
2141 }else if( pIn3
->u
.i
< pIn1
->u
.i
){
2142 if( sqlite3aLTb
[pOp
->opcode
] ){
2143 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2147 VVA_ONLY( iCompareIsInit
= 1; )
2149 if( sqlite3aEQb
[pOp
->opcode
] ){
2150 VdbeBranchTaken(1, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2154 VVA_ONLY( iCompareIsInit
= 1; )
2156 VdbeBranchTaken(0, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2159 if( (flags1
| flags3
)&MEM_Null
){
2160 /* One or both operands are NULL */
2161 if( pOp
->p5
& SQLITE_NULLEQ
){
2162 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2163 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2164 ** or not both operands are null.
2166 assert( (flags1
& MEM_Cleared
)==0 );
2167 assert( (pOp
->p5
& SQLITE_JUMPIFNULL
)==0 || CORRUPT_DB
);
2168 testcase( (pOp
->p5
& SQLITE_JUMPIFNULL
)!=0 );
2169 if( (flags1
&flags3
&MEM_Null
)!=0
2170 && (flags3
&MEM_Cleared
)==0
2172 res
= 0; /* Operands are equal */
2174 res
= ((flags3
& MEM_Null
) ? -1 : +1); /* Operands are not equal */
2177 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2178 ** then the result is always NULL.
2179 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2181 VdbeBranchTaken(2,3);
2182 if( pOp
->p5
& SQLITE_JUMPIFNULL
){
2185 iCompare
= 1; /* Operands are not equal */
2186 VVA_ONLY( iCompareIsInit
= 1; )
2190 /* Neither operand is NULL and we couldn't do the special high-speed
2191 ** integer comparison case. So do a general-case comparison. */
2192 affinity
= pOp
->p5
& SQLITE_AFF_MASK
;
2193 if( affinity
>=SQLITE_AFF_NUMERIC
){
2194 if( (flags1
| flags3
)&MEM_Str
){
2195 if( (flags1
& (MEM_Int
|MEM_IntReal
|MEM_Real
|MEM_Str
))==MEM_Str
){
2196 applyNumericAffinity(pIn1
,0);
2197 assert( flags3
==pIn3
->flags
|| CORRUPT_DB
);
2198 flags3
= pIn3
->flags
;
2200 if( (flags3
& (MEM_Int
|MEM_IntReal
|MEM_Real
|MEM_Str
))==MEM_Str
){
2201 applyNumericAffinity(pIn3
,0);
2204 }else if( affinity
==SQLITE_AFF_TEXT
&& ((flags1
| flags3
) & MEM_Str
)!=0 ){
2205 if( (flags1
& MEM_Str
)==0 && (flags1
&(MEM_Int
|MEM_Real
|MEM_IntReal
))!=0 ){
2206 testcase( pIn1
->flags
& MEM_Int
);
2207 testcase( pIn1
->flags
& MEM_Real
);
2208 testcase( pIn1
->flags
& MEM_IntReal
);
2209 sqlite3VdbeMemStringify(pIn1
, encoding
, 1);
2210 testcase( (flags1
&MEM_Dyn
) != (pIn1
->flags
&MEM_Dyn
) );
2211 flags1
= (pIn1
->flags
& ~MEM_TypeMask
) | (flags1
& MEM_TypeMask
);
2212 if( NEVER(pIn1
==pIn3
) ) flags3
= flags1
| MEM_Str
;
2214 if( (flags3
& MEM_Str
)==0 && (flags3
&(MEM_Int
|MEM_Real
|MEM_IntReal
))!=0 ){
2215 testcase( pIn3
->flags
& MEM_Int
);
2216 testcase( pIn3
->flags
& MEM_Real
);
2217 testcase( pIn3
->flags
& MEM_IntReal
);
2218 sqlite3VdbeMemStringify(pIn3
, encoding
, 1);
2219 testcase( (flags3
&MEM_Dyn
) != (pIn3
->flags
&MEM_Dyn
) );
2220 flags3
= (pIn3
->flags
& ~MEM_TypeMask
) | (flags3
& MEM_TypeMask
);
2223 assert( pOp
->p4type
==P4_COLLSEQ
|| pOp
->p4
.pColl
==0 );
2224 res
= sqlite3MemCompare(pIn3
, pIn1
, pOp
->p4
.pColl
);
2227 /* At this point, res is negative, zero, or positive if reg[P1] is
2228 ** less than, equal to, or greater than reg[P3], respectively. Compute
2229 ** the answer to this operator in res2, depending on what the comparison
2230 ** operator actually is. The next block of code depends on the fact
2231 ** that the 6 comparison operators are consecutive integers in this
2232 ** order: NE, EQ, GT, LE, LT, GE */
2233 assert( OP_Eq
==OP_Ne
+1 ); assert( OP_Gt
==OP_Ne
+2 ); assert( OP_Le
==OP_Ne
+3 );
2234 assert( OP_Lt
==OP_Ne
+4 ); assert( OP_Ge
==OP_Ne
+5 );
2236 res2
= sqlite3aLTb
[pOp
->opcode
];
2238 res2
= sqlite3aEQb
[pOp
->opcode
];
2240 res2
= sqlite3aGTb
[pOp
->opcode
];
2243 VVA_ONLY( iCompareIsInit
= 1; )
2245 /* Undo any changes made by applyAffinity() to the input registers. */
2246 assert( (pIn3
->flags
& MEM_Dyn
) == (flags3
& MEM_Dyn
) );
2247 pIn3
->flags
= flags3
;
2248 assert( (pIn1
->flags
& MEM_Dyn
) == (flags1
& MEM_Dyn
) );
2249 pIn1
->flags
= flags1
;
2251 VdbeBranchTaken(res2
!=0, (pOp
->p5
& SQLITE_NULLEQ
)?2:3);
2258 /* Opcode: ElseEq * P2 * * *
2260 ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2261 ** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2262 ** opcodes are allowed to occur between this instruction and the previous
2265 ** If result of an OP_Eq comparison on the same two operands as the
2266 ** prior OP_Lt or OP_Gt would have been true, then jump to P2.
2267 ** If the result of an OP_Eq comparison on the two previous
2268 ** operands would have been false or NULL, then fall through.
2270 case OP_ElseEq
: { /* same as TK_ESCAPE, jump */
2273 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
2274 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
2276 for(iAddr
= (int)(pOp
- aOp
) - 1; ALWAYS(iAddr
>=0); iAddr
--){
2277 if( aOp
[iAddr
].opcode
==OP_ReleaseReg
) continue;
2278 assert( aOp
[iAddr
].opcode
==OP_Lt
|| aOp
[iAddr
].opcode
==OP_Gt
);
2281 #endif /* SQLITE_DEBUG */
2282 assert( iCompareIsInit
);
2283 VdbeBranchTaken(iCompare
==0, 2);
2284 if( iCompare
==0 ) goto jump_to_p2
;
2289 /* Opcode: Permutation * * * P4 *
2291 ** Set the permutation used by the OP_Compare operator in the next
2292 ** instruction. The permutation is stored in the P4 operand.
2294 ** The permutation is only valid for the next opcode which must be
2295 ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
2297 ** The first integer in the P4 integer array is the length of the array
2298 ** and does not become part of the permutation.
2300 case OP_Permutation
: {
2301 assert( pOp
->p4type
==P4_INTARRAY
);
2302 assert( pOp
->p4
.ai
);
2303 assert( pOp
[1].opcode
==OP_Compare
);
2304 assert( pOp
[1].p5
& OPFLAG_PERMUTE
);
2308 /* Opcode: Compare P1 P2 P3 P4 P5
2309 ** Synopsis: r[P1@P3] <-> r[P2@P3]
2311 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2312 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
2313 ** the comparison for use by the next OP_Jump instruct.
2315 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2316 ** determined by the most recent OP_Permutation operator. If the
2317 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2320 ** P4 is a KeyInfo structure that defines collating sequences and sort
2321 ** orders for the comparison. The permutation applies to registers
2322 ** only. The KeyInfo elements are used sequentially.
2324 ** The comparison is a sort comparison, so NULLs compare equal,
2325 ** NULLs are less than numbers, numbers are less than strings,
2326 ** and strings are less than blobs.
2328 ** This opcode must be immediately followed by an OP_Jump opcode.
2335 const KeyInfo
*pKeyInfo
;
2337 CollSeq
*pColl
; /* Collating sequence to use on this term */
2338 int bRev
; /* True for DESCENDING sort order */
2339 u32
*aPermute
; /* The permutation */
2341 if( (pOp
->p5
& OPFLAG_PERMUTE
)==0 ){
2345 assert( pOp
[-1].opcode
==OP_Permutation
);
2346 assert( pOp
[-1].p4type
==P4_INTARRAY
);
2347 aPermute
= pOp
[-1].p4
.ai
+ 1;
2348 assert( aPermute
!=0 );
2351 pKeyInfo
= pOp
->p4
.pKeyInfo
;
2353 assert( pKeyInfo
!=0 );
2359 for(k
=0; k
<n
; k
++) if( aPermute
[k
]>(u32
)mx
) mx
= aPermute
[k
];
2360 assert( p1
>0 && p1
+mx
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2361 assert( p2
>0 && p2
+mx
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2363 assert( p1
>0 && p1
+n
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2364 assert( p2
>0 && p2
+n
<=(p
->nMem
+1 - p
->nCursor
)+1 );
2366 #endif /* SQLITE_DEBUG */
2368 idx
= aPermute
? aPermute
[i
] : (u32
)i
;
2369 assert( memIsValid(&aMem
[p1
+idx
]) );
2370 assert( memIsValid(&aMem
[p2
+idx
]) );
2371 REGISTER_TRACE(p1
+idx
, &aMem
[p1
+idx
]);
2372 REGISTER_TRACE(p2
+idx
, &aMem
[p2
+idx
]);
2373 assert( i
<pKeyInfo
->nKeyField
);
2374 pColl
= pKeyInfo
->aColl
[i
];
2375 bRev
= (pKeyInfo
->aSortFlags
[i
] & KEYINFO_ORDER_DESC
);
2376 iCompare
= sqlite3MemCompare(&aMem
[p1
+idx
], &aMem
[p2
+idx
], pColl
);
2377 VVA_ONLY( iCompareIsInit
= 1; )
2379 if( (pKeyInfo
->aSortFlags
[i
] & KEYINFO_ORDER_BIGNULL
)
2380 && ((aMem
[p1
+idx
].flags
& MEM_Null
) || (aMem
[p2
+idx
].flags
& MEM_Null
))
2382 iCompare
= -iCompare
;
2384 if( bRev
) iCompare
= -iCompare
;
2388 assert( pOp
[1].opcode
==OP_Jump
);
2392 /* Opcode: Jump P1 P2 P3 * *
2394 ** Jump to the instruction at address P1, P2, or P3 depending on whether
2395 ** in the most recent OP_Compare instruction the P1 vector was less than
2396 ** equal to, or greater than the P2 vector, respectively.
2398 ** This opcode must immediately follow an OP_Compare opcode.
2400 case OP_Jump
: { /* jump */
2401 assert( pOp
>aOp
&& pOp
[-1].opcode
==OP_Compare
);
2402 assert( iCompareIsInit
);
2404 VdbeBranchTaken(0,4); pOp
= &aOp
[pOp
->p1
- 1];
2405 }else if( iCompare
==0 ){
2406 VdbeBranchTaken(1,4); pOp
= &aOp
[pOp
->p2
- 1];
2408 VdbeBranchTaken(2,4); pOp
= &aOp
[pOp
->p3
- 1];
2413 /* Opcode: And P1 P2 P3 * *
2414 ** Synopsis: r[P3]=(r[P1] && r[P2])
2416 ** Take the logical AND of the values in registers P1 and P2 and
2417 ** write the result into register P3.
2419 ** If either P1 or P2 is 0 (false) then the result is 0 even if
2420 ** the other input is NULL. A NULL and true or two NULLs give
2423 /* Opcode: Or P1 P2 P3 * *
2424 ** Synopsis: r[P3]=(r[P1] || r[P2])
2426 ** Take the logical OR of the values in register P1 and P2 and
2427 ** store the answer in register P3.
2429 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2430 ** even if the other input is NULL. A NULL and false or two NULLs
2431 ** give a NULL output.
2433 case OP_And
: /* same as TK_AND, in1, in2, out3 */
2434 case OP_Or
: { /* same as TK_OR, in1, in2, out3 */
2435 int v1
; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2436 int v2
; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2438 v1
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], 2);
2439 v2
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p2
], 2);
2440 if( pOp
->opcode
==OP_And
){
2441 static const unsigned char and_logic
[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
2442 v1
= and_logic
[v1
*3+v2
];
2444 static const unsigned char or_logic
[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
2445 v1
= or_logic
[v1
*3+v2
];
2447 pOut
= &aMem
[pOp
->p3
];
2449 MemSetTypeFlag(pOut
, MEM_Null
);
2452 MemSetTypeFlag(pOut
, MEM_Int
);
2457 /* Opcode: IsTrue P1 P2 P3 P4 *
2458 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2460 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2461 ** IS NOT FALSE operators.
2463 ** Interpret the value in register P1 as a boolean value. Store that
2464 ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2465 ** NULL, then the P3 is stored in register P2. Invert the answer if P4
2468 ** The logic is summarized like this:
2471 ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2472 ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2473 ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2474 ** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
2477 case OP_IsTrue
: { /* in1, out2 */
2478 assert( pOp
->p4type
==P4_INT32
);
2479 assert( pOp
->p4
.i
==0 || pOp
->p4
.i
==1 );
2480 assert( pOp
->p3
==0 || pOp
->p3
==1 );
2481 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p2
],
2482 sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], pOp
->p3
) ^ pOp
->p4
.i
);
2486 /* Opcode: Not P1 P2 * * *
2487 ** Synopsis: r[P2]= !r[P1]
2489 ** Interpret the value in register P1 as a boolean value. Store the
2490 ** boolean complement in register P2. If the value in register P1 is
2491 ** NULL, then a NULL is stored in P2.
2493 case OP_Not
: { /* same as TK_NOT, in1, out2 */
2494 pIn1
= &aMem
[pOp
->p1
];
2495 pOut
= &aMem
[pOp
->p2
];
2496 if( (pIn1
->flags
& MEM_Null
)==0 ){
2497 sqlite3VdbeMemSetInt64(pOut
, !sqlite3VdbeBooleanValue(pIn1
,0));
2499 sqlite3VdbeMemSetNull(pOut
);
2504 /* Opcode: BitNot P1 P2 * * *
2505 ** Synopsis: r[P2]= ~r[P1]
2507 ** Interpret the content of register P1 as an integer. Store the
2508 ** ones-complement of the P1 value into register P2. If P1 holds
2509 ** a NULL then store a NULL in P2.
2511 case OP_BitNot
: { /* same as TK_BITNOT, in1, out2 */
2512 pIn1
= &aMem
[pOp
->p1
];
2513 pOut
= &aMem
[pOp
->p2
];
2514 sqlite3VdbeMemSetNull(pOut
);
2515 if( (pIn1
->flags
& MEM_Null
)==0 ){
2516 pOut
->flags
= MEM_Int
;
2517 pOut
->u
.i
= ~sqlite3VdbeIntValue(pIn1
);
2522 /* Opcode: Once P1 P2 * * *
2524 ** Fall through to the next instruction the first time this opcode is
2525 ** encountered on each invocation of the byte-code program. Jump to P2
2526 ** on the second and all subsequent encounters during the same invocation.
2528 ** Top-level programs determine first invocation by comparing the P1
2529 ** operand against the P1 operand on the OP_Init opcode at the beginning
2530 ** of the program. If the P1 values differ, then fall through and make
2531 ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2532 ** the same then take the jump.
2534 ** For subprograms, there is a bitmask in the VdbeFrame that determines
2535 ** whether or not the jump should be taken. The bitmask is necessary
2536 ** because the self-altering code trick does not work for recursive
2539 case OP_Once
: { /* jump */
2540 u32 iAddr
; /* Address of this instruction */
2541 assert( p
->aOp
[0].opcode
==OP_Init
);
2543 iAddr
= (int)(pOp
- p
->aOp
);
2544 if( (p
->pFrame
->aOnce
[iAddr
/8] & (1<<(iAddr
& 7)))!=0 ){
2545 VdbeBranchTaken(1, 2);
2548 p
->pFrame
->aOnce
[iAddr
/8] |= 1<<(iAddr
& 7);
2550 if( p
->aOp
[0].p1
==pOp
->p1
){
2551 VdbeBranchTaken(1, 2);
2555 VdbeBranchTaken(0, 2);
2556 pOp
->p1
= p
->aOp
[0].p1
;
2560 /* Opcode: If P1 P2 P3 * *
2562 ** Jump to P2 if the value in register P1 is true. The value
2563 ** is considered true if it is numeric and non-zero. If the value
2564 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2566 case OP_If
: { /* jump, in1 */
2568 c
= sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], pOp
->p3
);
2569 VdbeBranchTaken(c
!=0, 2);
2570 if( c
) goto jump_to_p2
;
2574 /* Opcode: IfNot P1 P2 P3 * *
2576 ** Jump to P2 if the value in register P1 is False. The value
2577 ** is considered false if it has a numeric value of zero. If the value
2578 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2580 case OP_IfNot
: { /* jump, in1 */
2582 c
= !sqlite3VdbeBooleanValue(&aMem
[pOp
->p1
], !pOp
->p3
);
2583 VdbeBranchTaken(c
!=0, 2);
2584 if( c
) goto jump_to_p2
;
2588 /* Opcode: IsNull P1 P2 * * *
2589 ** Synopsis: if r[P1]==NULL goto P2
2591 ** Jump to P2 if the value in register P1 is NULL.
2593 case OP_IsNull
: { /* same as TK_ISNULL, jump, in1 */
2594 pIn1
= &aMem
[pOp
->p1
];
2595 VdbeBranchTaken( (pIn1
->flags
& MEM_Null
)!=0, 2);
2596 if( (pIn1
->flags
& MEM_Null
)!=0 ){
2602 /* Opcode: IsType P1 P2 P3 P4 P5
2603 ** Synopsis: if typeof(P1.P3) in P5 goto P2
2605 ** Jump to P2 if the type of a column in a btree is one of the types specified
2606 ** by the P5 bitmask.
2608 ** P1 is normally a cursor on a btree for which the row decode cache is
2609 ** valid through at least column P3. In other words, there should have been
2610 ** a prior OP_Column for column P3 or greater. If the cursor is not valid,
2611 ** then this opcode might give spurious results.
2612 ** The the btree row has fewer than P3 columns, then use P4 as the
2615 ** If P1 is -1, then P3 is a register number and the datatype is taken
2616 ** from the value in that register.
2618 ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
2619 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
2620 ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
2622 ** Take the jump to address P2 if and only if the datatype of the
2623 ** value determined by P1 and P3 corresponds to one of the bits in the
2627 case OP_IsType
: { /* jump */
2632 assert( pOp
->p1
>=(-1) && pOp
->p1
<p
->nCursor
);
2633 assert( pOp
->p1
>=0 || (pOp
->p3
>=0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
)) );
2635 pC
= p
->apCsr
[pOp
->p1
];
2637 assert( pOp
->p3
>=0 );
2638 if( pOp
->p3
<pC
->nHdrParsed
){
2639 serialType
= pC
->aType
[pOp
->p3
];
2640 if( serialType
>=12 ){
2642 typeMask
= 0x04; /* SQLITE_TEXT */
2644 typeMask
= 0x08; /* SQLITE_BLOB */
2647 static const unsigned char aMask
[] = {
2648 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
2649 0x01, 0x01, 0x10, 0x10
2651 testcase( serialType
==0 );
2652 testcase( serialType
==1 );
2653 testcase( serialType
==2 );
2654 testcase( serialType
==3 );
2655 testcase( serialType
==4 );
2656 testcase( serialType
==5 );
2657 testcase( serialType
==6 );
2658 testcase( serialType
==7 );
2659 testcase( serialType
==8 );
2660 testcase( serialType
==9 );
2661 testcase( serialType
==10 );
2662 testcase( serialType
==11 );
2663 typeMask
= aMask
[serialType
];
2666 typeMask
= 1 << (pOp
->p4
.i
- 1);
2667 testcase( typeMask
==0x01 );
2668 testcase( typeMask
==0x02 );
2669 testcase( typeMask
==0x04 );
2670 testcase( typeMask
==0x08 );
2671 testcase( typeMask
==0x10 );
2674 assert( memIsValid(&aMem
[pOp
->p3
]) );
2675 typeMask
= 1 << (sqlite3_value_type((sqlite3_value
*)&aMem
[pOp
->p3
])-1);
2676 testcase( typeMask
==0x01 );
2677 testcase( typeMask
==0x02 );
2678 testcase( typeMask
==0x04 );
2679 testcase( typeMask
==0x08 );
2680 testcase( typeMask
==0x10 );
2682 VdbeBranchTaken( (typeMask
& pOp
->p5
)!=0, 2);
2683 if( typeMask
& pOp
->p5
){
2689 /* Opcode: ZeroOrNull P1 P2 P3 * *
2690 ** Synopsis: r[P2] = 0 OR NULL
2692 ** If all both registers P1 and P3 are NOT NULL, then store a zero in
2693 ** register P2. If either registers P1 or P3 are NULL then put
2694 ** a NULL in register P2.
2696 case OP_ZeroOrNull
: { /* in1, in2, out2, in3 */
2697 if( (aMem
[pOp
->p1
].flags
& MEM_Null
)!=0
2698 || (aMem
[pOp
->p3
].flags
& MEM_Null
)!=0
2700 sqlite3VdbeMemSetNull(aMem
+ pOp
->p2
);
2702 sqlite3VdbeMemSetInt64(aMem
+ pOp
->p2
, 0);
2707 /* Opcode: NotNull P1 P2 * * *
2708 ** Synopsis: if r[P1]!=NULL goto P2
2710 ** Jump to P2 if the value in register P1 is not NULL.
2712 case OP_NotNull
: { /* same as TK_NOTNULL, jump, in1 */
2713 pIn1
= &aMem
[pOp
->p1
];
2714 VdbeBranchTaken( (pIn1
->flags
& MEM_Null
)==0, 2);
2715 if( (pIn1
->flags
& MEM_Null
)==0 ){
2721 /* Opcode: IfNullRow P1 P2 P3 * *
2722 ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2724 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
2725 ** If it is, then set register P3 to NULL and jump immediately to P2.
2726 ** If P1 is not on a NULL row, then fall through without making any
2729 ** If P1 is not an open cursor, then this opcode is a no-op.
2731 case OP_IfNullRow
: { /* jump */
2733 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2734 pC
= p
->apCsr
[pOp
->p1
];
2735 if( ALWAYS(pC
) && pC
->nullRow
){
2736 sqlite3VdbeMemSetNull(aMem
+ pOp
->p3
);
2742 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2743 /* Opcode: Offset P1 P2 P3 * *
2744 ** Synopsis: r[P3] = sqlite_offset(P1)
2746 ** Store in register r[P3] the byte offset into the database file that is the
2747 ** start of the payload for the record at which that cursor P1 is currently
2750 ** P2 is the column number for the argument to the sqlite_offset() function.
2751 ** This opcode does not use P2 itself, but the P2 value is used by the
2752 ** code generator. The P1, P2, and P3 operands to this opcode are the
2753 ** same as for OP_Column.
2755 ** This opcode is only available if SQLite is compiled with the
2756 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
2758 case OP_Offset
: { /* out3 */
2759 VdbeCursor
*pC
; /* The VDBE cursor */
2760 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2761 pC
= p
->apCsr
[pOp
->p1
];
2762 pOut
= &p
->aMem
[pOp
->p3
];
2763 if( pC
==0 || pC
->eCurType
!=CURTYPE_BTREE
){
2764 sqlite3VdbeMemSetNull(pOut
);
2766 if( pC
->deferredMoveto
){
2767 rc
= sqlite3VdbeFinishMoveto(pC
);
2768 if( rc
) goto abort_due_to_error
;
2770 if( sqlite3BtreeEof(pC
->uc
.pCursor
) ){
2771 sqlite3VdbeMemSetNull(pOut
);
2773 sqlite3VdbeMemSetInt64(pOut
, sqlite3BtreeOffset(pC
->uc
.pCursor
));
2778 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
2780 /* Opcode: Column P1 P2 P3 P4 P5
2781 ** Synopsis: r[P3]=PX cursor P1 column P2
2783 ** Interpret the data that cursor P1 points to as a structure built using
2784 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
2785 ** information about the format of the data.) Extract the P2-th column
2786 ** from this record. If there are less than (P2+1)
2787 ** values in the record, extract a NULL.
2789 ** The value extracted is stored in register P3.
2791 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
2792 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
2795 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
2796 ** to only be used by the length() function or the equivalent. The content
2797 ** of large blobs is not loaded, thus saving CPU cycles. If the
2798 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
2799 ** typeof() function or the IS NULL or IS NOT NULL operators or the
2800 ** equivalent. In this case, all content loading can be omitted.
2802 case OP_Column
: { /* ncycle */
2803 u32 p2
; /* column number to retrieve */
2804 VdbeCursor
*pC
; /* The VDBE cursor */
2805 BtCursor
*pCrsr
; /* The B-Tree cursor corresponding to pC */
2806 u32
*aOffset
; /* aOffset[i] is offset to start of data for i-th column */
2807 int len
; /* The length of the serialized data for the column */
2808 int i
; /* Loop counter */
2809 Mem
*pDest
; /* Where to write the extracted value */
2810 Mem sMem
; /* For storing the record being decoded */
2811 const u8
*zData
; /* Part of the record being decoded */
2812 const u8
*zHdr
; /* Next unparsed byte of the header */
2813 const u8
*zEndHdr
; /* Pointer to first byte after the header */
2814 u64 offset64
; /* 64-bit offset */
2815 u32 t
; /* A type code from the record header */
2816 Mem
*pReg
; /* PseudoTable input register */
2818 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
2819 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
2820 pC
= p
->apCsr
[pOp
->p1
];
2825 assert( p2
<(u32
)pC
->nField
2826 || (pC
->eCurType
==CURTYPE_PSEUDO
&& pC
->seekResult
==0) );
2827 aOffset
= pC
->aOffset
;
2828 assert( aOffset
==pC
->aType
+pC
->nField
);
2829 assert( pC
->eCurType
!=CURTYPE_VTAB
);
2830 assert( pC
->eCurType
!=CURTYPE_PSEUDO
|| pC
->nullRow
);
2831 assert( pC
->eCurType
!=CURTYPE_SORTER
);
2833 if( pC
->cacheStatus
!=p
->cacheCtr
){ /*OPTIMIZATION-IF-FALSE*/
2835 if( pC
->eCurType
==CURTYPE_PSEUDO
&& pC
->seekResult
>0 ){
2836 /* For the special case of as pseudo-cursor, the seekResult field
2837 ** identifies the register that holds the record */
2838 pReg
= &aMem
[pC
->seekResult
];
2839 assert( pReg
->flags
& MEM_Blob
);
2840 assert( memIsValid(pReg
) );
2841 pC
->payloadSize
= pC
->szRow
= pReg
->n
;
2842 pC
->aRow
= (u8
*)pReg
->z
;
2844 pDest
= &aMem
[pOp
->p3
];
2845 memAboutToChange(p
, pDest
);
2846 sqlite3VdbeMemSetNull(pDest
);
2850 pCrsr
= pC
->uc
.pCursor
;
2851 if( pC
->deferredMoveto
){
2853 assert( !pC
->isEphemeral
);
2854 if( pC
->ub
.aAltMap
&& (iMap
= pC
->ub
.aAltMap
[1+p2
])>0 ){
2855 pC
= pC
->pAltCursor
;
2857 goto op_column_restart
;
2859 rc
= sqlite3VdbeFinishMoveto(pC
);
2860 if( rc
) goto abort_due_to_error
;
2861 }else if( sqlite3BtreeCursorHasMoved(pCrsr
) ){
2862 rc
= sqlite3VdbeHandleMovedCursor(pC
);
2863 if( rc
) goto abort_due_to_error
;
2864 goto op_column_restart
;
2866 assert( pC
->eCurType
==CURTYPE_BTREE
);
2868 assert( sqlite3BtreeCursorIsValid(pCrsr
) );
2869 pC
->payloadSize
= sqlite3BtreePayloadSize(pCrsr
);
2870 pC
->aRow
= sqlite3BtreePayloadFetch(pCrsr
, &pC
->szRow
);
2871 assert( pC
->szRow
<=pC
->payloadSize
);
2872 assert( pC
->szRow
<=65536 ); /* Maximum page size is 64KiB */
2874 pC
->cacheStatus
= p
->cacheCtr
;
2875 if( (aOffset
[0] = pC
->aRow
[0])<0x80 ){
2878 pC
->iHdrOffset
= sqlite3GetVarint32(pC
->aRow
, aOffset
);
2882 if( pC
->szRow
<aOffset
[0] ){ /*OPTIMIZATION-IF-FALSE*/
2883 /* pC->aRow does not have to hold the entire row, but it does at least
2884 ** need to cover the header of the record. If pC->aRow does not contain
2885 ** the complete header, then set it to zero, forcing the header to be
2886 ** dynamically allocated. */
2890 /* Make sure a corrupt database has not given us an oversize header.
2891 ** Do this now to avoid an oversize memory allocation.
2893 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2894 ** types use so much data space that there can only be 4096 and 32 of
2895 ** them, respectively. So the maximum header length results from a
2896 ** 3-byte type for each of the maximum of 32768 columns plus three
2897 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2899 if( aOffset
[0] > 98307 || aOffset
[0] > pC
->payloadSize
){
2900 goto op_column_corrupt
;
2903 /* This is an optimization. By skipping over the first few tests
2904 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2905 ** measurable performance gain.
2907 ** This branch is taken even if aOffset[0]==0. Such a record is never
2908 ** generated by SQLite, and could be considered corruption, but we
2909 ** accept it for historical reasons. When aOffset[0]==0, the code this
2910 ** branch jumps to reads past the end of the record, but never more
2911 ** than a few bytes. Even if the record occurs at the end of the page
2912 ** content area, the "page header" comes after the page content and so
2913 ** this overread is harmless. Similar overreads can occur for a corrupt
2917 assert( pC
->nHdrParsed
<=p2
); /* Conditional skipped */
2918 testcase( aOffset
[0]==0 );
2919 goto op_column_read_header
;
2921 }else if( sqlite3BtreeCursorHasMoved(pC
->uc
.pCursor
) ){
2922 rc
= sqlite3VdbeHandleMovedCursor(pC
);
2923 if( rc
) goto abort_due_to_error
;
2924 goto op_column_restart
;
2927 /* Make sure at least the first p2+1 entries of the header have been
2928 ** parsed and valid information is in aOffset[] and pC->aType[].
2930 if( pC
->nHdrParsed
<=p2
){
2931 /* If there is more header available for parsing in the record, try
2932 ** to extract additional fields up through the p2+1-th field
2934 if( pC
->iHdrOffset
<aOffset
[0] ){
2935 /* Make sure zData points to enough of the record to cover the header. */
2937 memset(&sMem
, 0, sizeof(sMem
));
2938 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pC
->uc
.pCursor
,aOffset
[0],&sMem
);
2939 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
2940 zData
= (u8
*)sMem
.z
;
2945 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
2946 op_column_read_header
:
2948 offset64
= aOffset
[i
];
2949 zHdr
= zData
+ pC
->iHdrOffset
;
2950 zEndHdr
= zData
+ aOffset
[0];
2951 testcase( zHdr
>=zEndHdr
);
2953 if( (pC
->aType
[i
] = t
= zHdr
[0])<0x80 ){
2955 offset64
+= sqlite3VdbeOneByteSerialTypeLen(t
);
2957 zHdr
+= sqlite3GetVarint32(zHdr
, &t
);
2959 offset64
+= sqlite3VdbeSerialTypeLen(t
);
2961 aOffset
[++i
] = (u32
)(offset64
& 0xffffffff);
2962 }while( (u32
)i
<=p2
&& zHdr
<zEndHdr
);
2964 /* The record is corrupt if any of the following are true:
2965 ** (1) the bytes of the header extend past the declared header size
2966 ** (2) the entire header was used but not all data was used
2967 ** (3) the end of the data extends beyond the end of the record.
2969 if( (zHdr
>=zEndHdr
&& (zHdr
>zEndHdr
|| offset64
!=pC
->payloadSize
))
2970 || (offset64
> pC
->payloadSize
)
2972 if( aOffset
[0]==0 ){
2976 if( pC
->aRow
==0 ) sqlite3VdbeMemRelease(&sMem
);
2977 goto op_column_corrupt
;
2982 pC
->iHdrOffset
= (u32
)(zHdr
- zData
);
2983 if( pC
->aRow
==0 ) sqlite3VdbeMemRelease(&sMem
);
2988 /* If after trying to extract new entries from the header, nHdrParsed is
2989 ** still not up to p2, that means that the record has fewer than p2
2990 ** columns. So the result will be either the default value or a NULL.
2992 if( pC
->nHdrParsed
<=p2
){
2993 pDest
= &aMem
[pOp
->p3
];
2994 memAboutToChange(p
, pDest
);
2995 if( pOp
->p4type
==P4_MEM
){
2996 sqlite3VdbeMemShallowCopy(pDest
, pOp
->p4
.pMem
, MEM_Static
);
2998 sqlite3VdbeMemSetNull(pDest
);
3006 /* Extract the content for the p2+1-th column. Control can only
3007 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
3010 assert( p2
<pC
->nHdrParsed
);
3011 assert( rc
==SQLITE_OK
);
3012 pDest
= &aMem
[pOp
->p3
];
3013 memAboutToChange(p
, pDest
);
3014 assert( sqlite3VdbeCheckMemInvariants(pDest
) );
3015 if( VdbeMemDynamic(pDest
) ){
3016 sqlite3VdbeMemSetNull(pDest
);
3018 assert( t
==pC
->aType
[p2
] );
3019 if( pC
->szRow
>=aOffset
[p2
+1] ){
3020 /* This is the common case where the desired content fits on the original
3021 ** page - where the content is not on an overflow page */
3022 zData
= pC
->aRow
+ aOffset
[p2
];
3024 sqlite3VdbeSerialGet(zData
, t
, pDest
);
3026 /* If the column value is a string, we need a persistent value, not
3027 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
3028 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
3030 static const u16 aFlag
[] = { MEM_Blob
, MEM_Str
|MEM_Term
};
3031 pDest
->n
= len
= (t
-12)/2;
3032 pDest
->enc
= encoding
;
3033 if( pDest
->szMalloc
< len
+2 ){
3034 if( len
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ) goto too_big
;
3035 pDest
->flags
= MEM_Null
;
3036 if( sqlite3VdbeMemGrow(pDest
, len
+2, 0) ) goto no_mem
;
3038 pDest
->z
= pDest
->zMalloc
;
3040 memcpy(pDest
->z
, zData
, len
);
3042 pDest
->z
[len
+1] = 0;
3043 pDest
->flags
= aFlag
[t
&1];
3046 pDest
->enc
= encoding
;
3047 /* This branch happens only when content is on overflow pages */
3048 if( ((pOp
->p5
& (OPFLAG_LENGTHARG
|OPFLAG_TYPEOFARG
))!=0
3049 && ((t
>=12 && (t
&1)==0) || (pOp
->p5
& OPFLAG_TYPEOFARG
)!=0))
3050 || (len
= sqlite3VdbeSerialTypeLen(t
))==0
3052 /* Content is irrelevant for
3053 ** 1. the typeof() function,
3054 ** 2. the length(X) function if X is a blob, and
3055 ** 3. if the content length is zero.
3056 ** So we might as well use bogus content rather than reading
3057 ** content from disk.
3059 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
3060 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
3061 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
3062 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
3063 ** and it begins with a bunch of zeros.
3065 sqlite3VdbeSerialGet((u8
*)sqlite3CtypeMap
, t
, pDest
);
3067 if( len
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ) goto too_big
;
3068 rc
= sqlite3VdbeMemFromBtree(pC
->uc
.pCursor
, aOffset
[p2
], len
, pDest
);
3069 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3070 sqlite3VdbeSerialGet((const u8
*)pDest
->z
, t
, pDest
);
3071 pDest
->flags
&= ~MEM_Ephem
;
3076 UPDATE_MAX_BLOBSIZE(pDest
);
3077 REGISTER_TRACE(pOp
->p3
, pDest
);
3082 pOp
= &aOp
[aOp
[0].p3
-1];
3085 rc
= SQLITE_CORRUPT_BKPT
;
3086 goto abort_due_to_error
;
3090 /* Opcode: TypeCheck P1 P2 P3 P4 *
3091 ** Synopsis: typecheck(r[P1@P2])
3093 ** Apply affinities to the range of P2 registers beginning with P1.
3094 ** Take the affinities from the Table object in P4. If any value
3095 ** cannot be coerced into the correct type, then raise an error.
3097 ** This opcode is similar to OP_Affinity except that this opcode
3098 ** forces the register type to the Table column type. This is used
3099 ** to implement "strict affinity".
3101 ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
3102 ** is zero. When P3 is non-zero, no type checking occurs for
3103 ** static generated columns. Virtual columns are computed at query time
3104 ** and so they are never checked.
3109 ** <li> P2 should be the number of non-virtual columns in the
3111 ** <li> Table P4 should be a STRICT table.
3114 ** If any precondition is false, an assertion fault occurs.
3116 case OP_TypeCheck
: {
3121 assert( pOp
->p4type
==P4_TABLE
);
3122 pTab
= pOp
->p4
.pTab
;
3123 assert( pTab
->tabFlags
& TF_Strict
);
3124 assert( pTab
->nNVCol
==pOp
->p2
);
3126 pIn1
= &aMem
[pOp
->p1
];
3127 for(i
=0; i
<pTab
->nCol
; i
++){
3128 if( aCol
[i
].colFlags
& COLFLAG_GENERATED
){
3129 if( aCol
[i
].colFlags
& COLFLAG_VIRTUAL
) continue;
3130 if( pOp
->p3
){ pIn1
++; continue; }
3132 assert( pIn1
< &aMem
[pOp
->p1
+pOp
->p2
] );
3133 applyAffinity(pIn1
, aCol
[i
].affinity
, encoding
);
3134 if( (pIn1
->flags
& MEM_Null
)==0 ){
3135 switch( aCol
[i
].eCType
){
3136 case COLTYPE_BLOB
: {
3137 if( (pIn1
->flags
& MEM_Blob
)==0 ) goto vdbe_type_error
;
3140 case COLTYPE_INTEGER
:
3142 if( (pIn1
->flags
& MEM_Int
)==0 ) goto vdbe_type_error
;
3145 case COLTYPE_TEXT
: {
3146 if( (pIn1
->flags
& MEM_Str
)==0 ) goto vdbe_type_error
;
3149 case COLTYPE_REAL
: {
3150 testcase( (pIn1
->flags
& (MEM_Real
|MEM_IntReal
))==MEM_Real
);
3151 assert( (pIn1
->flags
& MEM_IntReal
)==0 );
3152 if( pIn1
->flags
& MEM_Int
){
3153 /* When applying REAL affinity, if the result is still an MEM_Int
3154 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3155 ** so that we keep the high-resolution integer value but know that
3156 ** the type really wants to be REAL. */
3157 testcase( pIn1
->u
.i
==140737488355328LL );
3158 testcase( pIn1
->u
.i
==140737488355327LL );
3159 testcase( pIn1
->u
.i
==-140737488355328LL );
3160 testcase( pIn1
->u
.i
==-140737488355329LL );
3161 if( pIn1
->u
.i
<=140737488355327LL && pIn1
->u
.i
>=-140737488355328LL){
3162 pIn1
->flags
|= MEM_IntReal
;
3163 pIn1
->flags
&= ~MEM_Int
;
3165 pIn1
->u
.r
= (double)pIn1
->u
.i
;
3166 pIn1
->flags
|= MEM_Real
;
3167 pIn1
->flags
&= ~MEM_Int
;
3169 }else if( (pIn1
->flags
& (MEM_Real
|MEM_IntReal
))==0 ){
3170 goto vdbe_type_error
;
3175 /* COLTYPE_ANY. Accept anything. */
3180 REGISTER_TRACE((int)(pIn1
-aMem
), pIn1
);
3183 assert( pIn1
== &aMem
[pOp
->p1
+pOp
->p2
] );
3187 sqlite3VdbeError(p
, "cannot store %s value in %s column %s.%s",
3188 vdbeMemTypeName(pIn1
), sqlite3StdType
[aCol
[i
].eCType
-1],
3189 pTab
->zName
, aCol
[i
].zCnName
);
3190 rc
= SQLITE_CONSTRAINT_DATATYPE
;
3191 goto abort_due_to_error
;
3194 /* Opcode: Affinity P1 P2 * P4 *
3195 ** Synopsis: affinity(r[P1@P2])
3197 ** Apply affinities to a range of P2 registers starting with P1.
3199 ** P4 is a string that is P2 characters long. The N-th character of the
3200 ** string indicates the column affinity that should be used for the N-th
3201 ** memory cell in the range.
3204 const char *zAffinity
; /* The affinity to be applied */
3206 zAffinity
= pOp
->p4
.z
;
3207 assert( zAffinity
!=0 );
3208 assert( pOp
->p2
>0 );
3209 assert( zAffinity
[pOp
->p2
]==0 );
3210 pIn1
= &aMem
[pOp
->p1
];
3211 while( 1 /*exit-by-break*/ ){
3212 assert( pIn1
<= &p
->aMem
[(p
->nMem
+1 - p
->nCursor
)] );
3213 assert( zAffinity
[0]==SQLITE_AFF_NONE
|| memIsValid(pIn1
) );
3214 applyAffinity(pIn1
, zAffinity
[0], encoding
);
3215 if( zAffinity
[0]==SQLITE_AFF_REAL
&& (pIn1
->flags
& MEM_Int
)!=0 ){
3216 /* When applying REAL affinity, if the result is still an MEM_Int
3217 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3218 ** so that we keep the high-resolution integer value but know that
3219 ** the type really wants to be REAL. */
3220 testcase( pIn1
->u
.i
==140737488355328LL );
3221 testcase( pIn1
->u
.i
==140737488355327LL );
3222 testcase( pIn1
->u
.i
==-140737488355328LL );
3223 testcase( pIn1
->u
.i
==-140737488355329LL );
3224 if( pIn1
->u
.i
<=140737488355327LL && pIn1
->u
.i
>=-140737488355328LL ){
3225 pIn1
->flags
|= MEM_IntReal
;
3226 pIn1
->flags
&= ~MEM_Int
;
3228 pIn1
->u
.r
= (double)pIn1
->u
.i
;
3229 pIn1
->flags
|= MEM_Real
;
3230 pIn1
->flags
&= ~MEM_Int
;
3233 REGISTER_TRACE((int)(pIn1
-aMem
), pIn1
);
3235 if( zAffinity
[0]==0 ) break;
3241 /* Opcode: MakeRecord P1 P2 P3 P4 *
3242 ** Synopsis: r[P3]=mkrec(r[P1@P2])
3244 ** Convert P2 registers beginning with P1 into the [record format]
3245 ** use as a data record in a database table or as a key
3246 ** in an index. The OP_Column opcode can decode the record later.
3248 ** P4 may be a string that is P2 characters long. The N-th character of the
3249 ** string indicates the column affinity that should be used for the N-th
3250 ** field of the index key.
3252 ** The mapping from character to affinity is given by the SQLITE_AFF_
3253 ** macros defined in sqliteInt.h.
3255 ** If P4 is NULL then all index fields have the affinity BLOB.
3257 ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
3258 ** compile-time option is enabled:
3260 ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
3261 ** of the right-most table that can be null-trimmed.
3263 ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
3264 ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
3265 ** accept no-change records with serial_type 10. This value is
3266 ** only used inside an assert() and does not affect the end result.
3268 case OP_MakeRecord
: {
3269 Mem
*pRec
; /* The new record */
3270 u64 nData
; /* Number of bytes of data space */
3271 int nHdr
; /* Number of bytes of header space */
3272 i64 nByte
; /* Data space required for this record */
3273 i64 nZero
; /* Number of zero bytes at the end of the record */
3274 int nVarint
; /* Number of bytes in a varint */
3275 u32 serial_type
; /* Type field */
3276 Mem
*pData0
; /* First field to be combined into the record */
3277 Mem
*pLast
; /* Last field of the record */
3278 int nField
; /* Number of fields in the record */
3279 char *zAffinity
; /* The affinity string for the record */
3280 u32 len
; /* Length of a field */
3281 u8
*zHdr
; /* Where to write next byte of the header */
3282 u8
*zPayload
; /* Where to write next byte of the payload */
3284 /* Assuming the record contains N fields, the record format looks
3287 ** ------------------------------------------------------------------------
3288 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
3289 ** ------------------------------------------------------------------------
3291 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
3294 ** Each type field is a varint representing the serial type of the
3295 ** corresponding data element (see sqlite3VdbeSerialType()). The
3296 ** hdr-size field is also a varint which is the offset from the beginning
3297 ** of the record to data0.
3299 nData
= 0; /* Number of bytes of data space */
3300 nHdr
= 0; /* Number of bytes of header space */
3301 nZero
= 0; /* Number of zero bytes at the end of the record */
3303 zAffinity
= pOp
->p4
.z
;
3304 assert( nField
>0 && pOp
->p2
>0 && pOp
->p2
+nField
<=(p
->nMem
+1 - p
->nCursor
)+1 );
3305 pData0
= &aMem
[nField
];
3307 pLast
= &pData0
[nField
-1];
3309 /* Identify the output register */
3310 assert( pOp
->p3
<pOp
->p1
|| pOp
->p3
>=pOp
->p1
+pOp
->p2
);
3311 pOut
= &aMem
[pOp
->p3
];
3312 memAboutToChange(p
, pOut
);
3314 /* Apply the requested affinity to all inputs
3316 assert( pData0
<=pLast
);
3320 applyAffinity(pRec
, zAffinity
[0], encoding
);
3321 if( zAffinity
[0]==SQLITE_AFF_REAL
&& (pRec
->flags
& MEM_Int
) ){
3322 pRec
->flags
|= MEM_IntReal
;
3323 pRec
->flags
&= ~(MEM_Int
);
3325 REGISTER_TRACE((int)(pRec
-aMem
), pRec
);
3328 assert( zAffinity
[0]==0 || pRec
<=pLast
);
3329 }while( zAffinity
[0] );
3332 #ifdef SQLITE_ENABLE_NULL_TRIM
3333 /* NULLs can be safely trimmed from the end of the record, as long as
3334 ** as the schema format is 2 or more and none of the omitted columns
3335 ** have a non-NULL default value. Also, the record must be left with
3336 ** at least one field. If P5>0 then it will be one more than the
3337 ** index of the right-most column with a non-NULL default value */
3339 while( (pLast
->flags
& MEM_Null
)!=0 && nField
>pOp
->p5
){
3346 /* Loop through the elements that will make up the record to figure
3347 ** out how much space is required for the new record. After this loop,
3348 ** the Mem.uTemp field of each term should hold the serial-type that will
3349 ** be used for that term in the generated record:
3351 ** Mem.uTemp value type
3352 ** --------------- ---------------
3354 ** 1 1-byte signed integer
3355 ** 2 2-byte signed integer
3356 ** 3 3-byte signed integer
3357 ** 4 4-byte signed integer
3358 ** 5 6-byte signed integer
3359 ** 6 8-byte signed integer
3361 ** 8 Integer constant 0
3362 ** 9 Integer constant 1
3363 ** 10,11 reserved for expansion
3364 ** N>=12 and even BLOB
3365 ** N>=13 and odd text
3367 ** The following additional values are computed:
3368 ** nHdr Number of bytes needed for the record header
3369 ** nData Number of bytes of data space needed for the record
3370 ** nZero Zero bytes at the end of the record
3374 assert( memIsValid(pRec
) );
3375 if( pRec
->flags
& MEM_Null
){
3376 if( pRec
->flags
& MEM_Zero
){
3377 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3378 ** table methods that never invoke sqlite3_result_xxxxx() while
3379 ** computing an unchanging column value in an UPDATE statement.
3380 ** Give such values a special internal-use-only serial-type of 10
3381 ** so that they can be passed through to xUpdate and have
3382 ** a true sqlite3_value_nochange(). */
3383 #ifndef SQLITE_ENABLE_NULL_TRIM
3384 assert( pOp
->p5
==OPFLAG_NOCHNG_MAGIC
|| CORRUPT_DB
);
3391 }else if( pRec
->flags
& (MEM_Int
|MEM_IntReal
) ){
3392 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3395 testcase( pRec
->flags
& MEM_Int
);
3396 testcase( pRec
->flags
& MEM_IntReal
);
3403 testcase( uu
==127 ); testcase( uu
==128 );
3404 testcase( uu
==32767 ); testcase( uu
==32768 );
3405 testcase( uu
==8388607 ); testcase( uu
==8388608 );
3406 testcase( uu
==2147483647 ); testcase( uu
==2147483648LL );
3407 testcase( uu
==140737488355327LL ); testcase( uu
==140737488355328LL );
3409 if( (i
&1)==i
&& p
->minWriteFileFormat
>=4 ){
3410 pRec
->uTemp
= 8+(u32
)uu
;
3415 }else if( uu
<=32767 ){
3418 }else if( uu
<=8388607 ){
3421 }else if( uu
<=2147483647 ){
3424 }else if( uu
<=140737488355327LL ){
3429 if( pRec
->flags
& MEM_IntReal
){
3430 /* If the value is IntReal and is going to take up 8 bytes to store
3431 ** as an integer, then we might as well make it an 8-byte floating
3433 pRec
->u
.r
= (double)pRec
->u
.i
;
3434 pRec
->flags
&= ~MEM_IntReal
;
3435 pRec
->flags
|= MEM_Real
;
3441 }else if( pRec
->flags
& MEM_Real
){
3446 assert( db
->mallocFailed
|| pRec
->flags
&(MEM_Str
|MEM_Blob
) );
3447 assert( pRec
->n
>=0 );
3449 serial_type
= (len
*2) + 12 + ((pRec
->flags
& MEM_Str
)!=0);
3450 if( pRec
->flags
& MEM_Zero
){
3451 serial_type
+= pRec
->u
.nZero
*2;
3453 if( sqlite3VdbeMemExpandBlob(pRec
) ) goto no_mem
;
3454 len
+= pRec
->u
.nZero
;
3456 nZero
+= pRec
->u
.nZero
;
3460 nHdr
+= sqlite3VarintLen(serial_type
);
3461 pRec
->uTemp
= serial_type
;
3463 if( pRec
==pData0
) break;
3467 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3468 ** which determines the total number of bytes in the header. The varint
3469 ** value is the size of the header in bytes including the size varint
3471 testcase( nHdr
==126 );
3472 testcase( nHdr
==127 );
3474 /* The common case */
3477 /* Rare case of a really large header */
3478 nVarint
= sqlite3VarintLen(nHdr
);
3480 if( nVarint
<sqlite3VarintLen(nHdr
) ) nHdr
++;
3484 /* Make sure the output register has a buffer large enough to store
3485 ** the new record. The output register (pOp->p3) is not allowed to
3486 ** be one of the input registers (because the following call to
3487 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
3489 if( nByte
+nZero
<=pOut
->szMalloc
){
3490 /* The output register is already large enough to hold the record.
3491 ** No error checks or buffer enlargement is required */
3492 pOut
->z
= pOut
->zMalloc
;
3494 /* Need to make sure that the output is not too big and then enlarge
3495 ** the output register to hold the full result */
3496 if( nByte
+nZero
>db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
3499 if( sqlite3VdbeMemClearAndResize(pOut
, (int)nByte
) ){
3503 pOut
->n
= (int)nByte
;
3504 pOut
->flags
= MEM_Blob
;
3506 pOut
->u
.nZero
= nZero
;
3507 pOut
->flags
|= MEM_Zero
;
3509 UPDATE_MAX_BLOBSIZE(pOut
);
3510 zHdr
= (u8
*)pOut
->z
;
3511 zPayload
= zHdr
+ nHdr
;
3513 /* Write the record */
3517 zHdr
+= sqlite3PutVarint(zHdr
,nHdr
);
3519 assert( pData0
<=pLast
);
3521 while( 1 /*exit-by-break*/ ){
3522 serial_type
= pRec
->uTemp
;
3523 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3524 ** additional varints, one per column.
3525 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
3526 ** immediately follow the header. */
3527 if( serial_type
<=7 ){
3528 *(zHdr
++) = serial_type
;
3529 if( serial_type
==0 ){
3530 /* NULL value. No change in zPayload */
3534 if( serial_type
==7 ){
3535 assert( sizeof(v
)==sizeof(pRec
->u
.r
) );
3536 memcpy(&v
, &pRec
->u
.r
, sizeof(v
));
3537 swapMixedEndianFloat(v
);
3541 len
= i
= sqlite3SmallTypeSizes
[serial_type
];
3543 while( 1 /*exit-by-break*/ ){
3544 zPayload
[--i
] = (u8
)(v
&0xFF);
3550 }else if( serial_type
<0x80 ){
3551 *(zHdr
++) = serial_type
;
3552 if( serial_type
>=14 && pRec
->n
>0 ){
3553 assert( pRec
->z
!=0 );
3554 memcpy(zPayload
, pRec
->z
, pRec
->n
);
3555 zPayload
+= pRec
->n
;
3558 zHdr
+= sqlite3PutVarint(zHdr
, serial_type
);
3560 assert( pRec
->z
!=0 );
3561 memcpy(zPayload
, pRec
->z
, pRec
->n
);
3562 zPayload
+= pRec
->n
;
3565 if( pRec
==pLast
) break;
3568 assert( nHdr
==(int)(zHdr
- (u8
*)pOut
->z
) );
3569 assert( nByte
==(int)(zPayload
- (u8
*)pOut
->z
) );
3571 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
3572 REGISTER_TRACE(pOp
->p3
, pOut
);
3576 /* Opcode: Count P1 P2 P3 * *
3577 ** Synopsis: r[P2]=count()
3579 ** Store the number of entries (an integer value) in the table or index
3580 ** opened by cursor P1 in register P2.
3582 ** If P3==0, then an exact count is obtained, which involves visiting
3583 ** every btree page of the table. But if P3 is non-zero, an estimate
3584 ** is returned based on the current cursor position.
3586 case OP_Count
: { /* out2 */
3590 assert( p
->apCsr
[pOp
->p1
]->eCurType
==CURTYPE_BTREE
);
3591 pCrsr
= p
->apCsr
[pOp
->p1
]->uc
.pCursor
;
3594 nEntry
= sqlite3BtreeRowCountEst(pCrsr
);
3596 nEntry
= 0; /* Not needed. Only used to silence a warning. */
3597 rc
= sqlite3BtreeCount(db
, pCrsr
, &nEntry
);
3598 if( rc
) goto abort_due_to_error
;
3600 pOut
= out2Prerelease(p
, pOp
);
3602 goto check_for_interrupt
;
3605 /* Opcode: Savepoint P1 * * P4 *
3607 ** Open, release or rollback the savepoint named by parameter P4, depending
3608 ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3609 ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3610 ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
3612 case OP_Savepoint
: {
3613 int p1
; /* Value of P1 operand */
3614 char *zName
; /* Name of savepoint */
3617 Savepoint
*pSavepoint
;
3625 /* Assert that the p1 parameter is valid. Also that if there is no open
3626 ** transaction, then there cannot be any savepoints.
3628 assert( db
->pSavepoint
==0 || db
->autoCommit
==0 );
3629 assert( p1
==SAVEPOINT_BEGIN
||p1
==SAVEPOINT_RELEASE
||p1
==SAVEPOINT_ROLLBACK
);
3630 assert( db
->pSavepoint
|| db
->isTransactionSavepoint
==0 );
3631 assert( checkSavepointCount(db
) );
3632 assert( p
->bIsReader
);
3634 if( p1
==SAVEPOINT_BEGIN
){
3635 if( db
->nVdbeWrite
>0 ){
3636 /* A new savepoint cannot be created if there are active write
3637 ** statements (i.e. open read/write incremental blob handles).
3639 sqlite3VdbeError(p
, "cannot open savepoint - SQL statements in progress");
3642 nName
= sqlite3Strlen30(zName
);
3644 #ifndef SQLITE_OMIT_VIRTUALTABLE
3645 /* This call is Ok even if this savepoint is actually a transaction
3646 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3647 ** If this is a transaction savepoint being opened, it is guaranteed
3648 ** that the db->aVTrans[] array is empty. */
3649 assert( db
->autoCommit
==0 || db
->nVTrans
==0 );
3650 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_BEGIN
,
3651 db
->nStatement
+db
->nSavepoint
);
3652 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3655 /* Create a new savepoint structure. */
3656 pNew
= sqlite3DbMallocRawNN(db
, sizeof(Savepoint
)+nName
+1);
3658 pNew
->zName
= (char *)&pNew
[1];
3659 memcpy(pNew
->zName
, zName
, nName
+1);
3661 /* If there is no open transaction, then mark this as a special
3662 ** "transaction savepoint". */
3663 if( db
->autoCommit
){
3665 db
->isTransactionSavepoint
= 1;
3670 /* Link the new savepoint into the database handle's list. */
3671 pNew
->pNext
= db
->pSavepoint
;
3672 db
->pSavepoint
= pNew
;
3673 pNew
->nDeferredCons
= db
->nDeferredCons
;
3674 pNew
->nDeferredImmCons
= db
->nDeferredImmCons
;
3678 assert( p1
==SAVEPOINT_RELEASE
|| p1
==SAVEPOINT_ROLLBACK
);
3681 /* Find the named savepoint. If there is no such savepoint, then an
3682 ** an error is returned to the user. */
3684 pSavepoint
= db
->pSavepoint
;
3685 pSavepoint
&& sqlite3StrICmp(pSavepoint
->zName
, zName
);
3686 pSavepoint
= pSavepoint
->pNext
3691 sqlite3VdbeError(p
, "no such savepoint: %s", zName
);
3693 }else if( db
->nVdbeWrite
>0 && p1
==SAVEPOINT_RELEASE
){
3694 /* It is not possible to release (commit) a savepoint if there are
3695 ** active write statements.
3697 sqlite3VdbeError(p
, "cannot release savepoint - "
3698 "SQL statements in progress");
3702 /* Determine whether or not this is a transaction savepoint. If so,
3703 ** and this is a RELEASE command, then the current transaction
3706 int isTransaction
= pSavepoint
->pNext
==0 && db
->isTransactionSavepoint
;
3707 if( isTransaction
&& p1
==SAVEPOINT_RELEASE
){
3708 if( (rc
= sqlite3VdbeCheckFk(p
, 1))!=SQLITE_OK
){
3712 if( sqlite3VdbeHalt(p
)==SQLITE_BUSY
){
3713 p
->pc
= (int)(pOp
- aOp
);
3715 p
->rc
= rc
= SQLITE_BUSY
;
3722 db
->isTransactionSavepoint
= 0;
3726 iSavepoint
= db
->nSavepoint
- iSavepoint
- 1;
3727 if( p1
==SAVEPOINT_ROLLBACK
){
3728 isSchemaChange
= (db
->mDbFlags
& DBFLAG_SchemaChange
)!=0;
3729 for(ii
=0; ii
<db
->nDb
; ii
++){
3730 rc
= sqlite3BtreeTripAllCursors(db
->aDb
[ii
].pBt
,
3731 SQLITE_ABORT_ROLLBACK
,
3733 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3736 assert( p1
==SAVEPOINT_RELEASE
);
3739 for(ii
=0; ii
<db
->nDb
; ii
++){
3740 rc
= sqlite3BtreeSavepoint(db
->aDb
[ii
].pBt
, p1
, iSavepoint
);
3741 if( rc
!=SQLITE_OK
){
3742 goto abort_due_to_error
;
3745 if( isSchemaChange
){
3746 sqlite3ExpirePreparedStatements(db
, 0);
3747 sqlite3ResetAllSchemasOfConnection(db
);
3748 db
->mDbFlags
|= DBFLAG_SchemaChange
;
3751 if( rc
) goto abort_due_to_error
;
3753 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3754 ** savepoints nested inside of the savepoint being operated on. */
3755 while( db
->pSavepoint
!=pSavepoint
){
3756 pTmp
= db
->pSavepoint
;
3757 db
->pSavepoint
= pTmp
->pNext
;
3758 sqlite3DbFree(db
, pTmp
);
3762 /* If it is a RELEASE, then destroy the savepoint being operated on
3763 ** too. If it is a ROLLBACK TO, then set the number of deferred
3764 ** constraint violations present in the database to the value stored
3765 ** when the savepoint was created. */
3766 if( p1
==SAVEPOINT_RELEASE
){
3767 assert( pSavepoint
==db
->pSavepoint
);
3768 db
->pSavepoint
= pSavepoint
->pNext
;
3769 sqlite3DbFree(db
, pSavepoint
);
3770 if( !isTransaction
){
3774 assert( p1
==SAVEPOINT_ROLLBACK
);
3775 db
->nDeferredCons
= pSavepoint
->nDeferredCons
;
3776 db
->nDeferredImmCons
= pSavepoint
->nDeferredImmCons
;
3779 if( !isTransaction
|| p1
==SAVEPOINT_ROLLBACK
){
3780 rc
= sqlite3VtabSavepoint(db
, p1
, iSavepoint
);
3781 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
3785 if( rc
) goto abort_due_to_error
;
3786 if( p
->eVdbeState
==VDBE_HALT_STATE
){
3793 /* Opcode: AutoCommit P1 P2 * * *
3795 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
3796 ** back any currently active btree transactions. If there are any active
3797 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3798 ** there are active writing VMs or active VMs that use shared cache.
3800 ** This instruction causes the VM to halt.
3802 case OP_AutoCommit
: {
3803 int desiredAutoCommit
;
3806 desiredAutoCommit
= pOp
->p1
;
3807 iRollback
= pOp
->p2
;
3808 assert( desiredAutoCommit
==1 || desiredAutoCommit
==0 );
3809 assert( desiredAutoCommit
==1 || iRollback
==0 );
3810 assert( db
->nVdbeActive
>0 ); /* At least this one VM is active */
3811 assert( p
->bIsReader
);
3813 if( desiredAutoCommit
!=db
->autoCommit
){
3815 assert( desiredAutoCommit
==1 );
3816 sqlite3RollbackAll(db
, SQLITE_ABORT_ROLLBACK
);
3818 }else if( desiredAutoCommit
&& db
->nVdbeWrite
>0 ){
3819 /* If this instruction implements a COMMIT and other VMs are writing
3820 ** return an error indicating that the other VMs must complete first.
3822 sqlite3VdbeError(p
, "cannot commit transaction - "
3823 "SQL statements in progress");
3825 goto abort_due_to_error
;
3826 }else if( (rc
= sqlite3VdbeCheckFk(p
, 1))!=SQLITE_OK
){
3829 db
->autoCommit
= (u8
)desiredAutoCommit
;
3831 if( sqlite3VdbeHalt(p
)==SQLITE_BUSY
){
3832 p
->pc
= (int)(pOp
- aOp
);
3833 db
->autoCommit
= (u8
)(1-desiredAutoCommit
);
3834 p
->rc
= rc
= SQLITE_BUSY
;
3837 sqlite3CloseSavepoints(db
);
3838 if( p
->rc
==SQLITE_OK
){
3846 (!desiredAutoCommit
)?"cannot start a transaction within a transaction":(
3847 (iRollback
)?"cannot rollback - no transaction is active":
3848 "cannot commit - no transaction is active"));
3851 goto abort_due_to_error
;
3853 /*NOTREACHED*/ assert(0);
3856 /* Opcode: Transaction P1 P2 P3 P4 P5
3858 ** Begin a transaction on database P1 if a transaction is not already
3860 ** If P2 is non-zero, then a write-transaction is started, or if a
3861 ** read-transaction is already active, it is upgraded to a write-transaction.
3862 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3863 ** then an exclusive transaction is started.
3865 ** P1 is the index of the database file on which the transaction is
3866 ** started. Index 0 is the main database file and index 1 is the
3867 ** file used for temporary tables. Indices of 2 or more are used for
3868 ** attached databases.
3870 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3871 ** true (this flag is set if the Vdbe may modify more than one row and may
3872 ** throw an ABORT exception), a statement transaction may also be opened.
3873 ** More specifically, a statement transaction is opened iff the database
3874 ** connection is currently not in autocommit mode, or if there are other
3875 ** active statements. A statement transaction allows the changes made by this
3876 ** VDBE to be rolled back after an error without having to roll back the
3877 ** entire transaction. If no error is encountered, the statement transaction
3878 ** will automatically commit when the VDBE halts.
3880 ** If P5!=0 then this opcode also checks the schema cookie against P3
3881 ** and the schema generation counter against P4.
3882 ** The cookie changes its value whenever the database schema changes.
3883 ** This operation is used to detect when that the cookie has changed
3884 ** and that the current process needs to reread the schema. If the schema
3885 ** cookie in P3 differs from the schema cookie in the database header or
3886 ** if the schema generation counter in P4 differs from the current
3887 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
3888 ** halts. The sqlite3_step() wrapper function might then reprepare the
3889 ** statement and rerun it from the beginning.
3891 case OP_Transaction
: {
3896 assert( p
->bIsReader
);
3897 assert( p
->readOnly
==0 || pOp
->p2
==0 );
3898 assert( pOp
->p2
>=0 && pOp
->p2
<=2 );
3899 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
3900 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
3901 assert( rc
==SQLITE_OK
);
3902 if( pOp
->p2
&& (db
->flags
& (SQLITE_QueryOnly
|SQLITE_CorruptRdOnly
))!=0 ){
3903 if( db
->flags
& SQLITE_QueryOnly
){
3904 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
3905 rc
= SQLITE_READONLY
;
3907 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
3909 rc
= SQLITE_CORRUPT
;
3911 goto abort_due_to_error
;
3913 pDb
= &db
->aDb
[pOp
->p1
];
3917 rc
= sqlite3BtreeBeginTrans(pBt
, pOp
->p2
, &iMeta
);
3918 testcase( rc
==SQLITE_BUSY_SNAPSHOT
);
3919 testcase( rc
==SQLITE_BUSY_RECOVERY
);
3920 if( rc
!=SQLITE_OK
){
3921 if( (rc
&0xff)==SQLITE_BUSY
){
3922 p
->pc
= (int)(pOp
- aOp
);
3926 goto abort_due_to_error
;
3929 if( p
->usesStmtJournal
3931 && (db
->autoCommit
==0 || db
->nVdbeRead
>1)
3933 assert( sqlite3BtreeTxnState(pBt
)==SQLITE_TXN_WRITE
);
3934 if( p
->iStatement
==0 ){
3935 assert( db
->nStatement
>=0 && db
->nSavepoint
>=0 );
3937 p
->iStatement
= db
->nSavepoint
+ db
->nStatement
;
3940 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_BEGIN
, p
->iStatement
-1);
3941 if( rc
==SQLITE_OK
){
3942 rc
= sqlite3BtreeBeginStmt(pBt
, p
->iStatement
);
3945 /* Store the current value of the database handles deferred constraint
3946 ** counter. If the statement transaction needs to be rolled back,
3947 ** the value of this counter needs to be restored too. */
3948 p
->nStmtDefCons
= db
->nDeferredCons
;
3949 p
->nStmtDefImmCons
= db
->nDeferredImmCons
;
3952 assert( pOp
->p5
==0 || pOp
->p4type
==P4_INT32
);
3955 && (iMeta
!=pOp
->p3
|| pDb
->pSchema
->iGeneration
!=pOp
->p4
.i
)
3958 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3959 ** version is checked to ensure that the schema has not changed since the
3960 ** SQL statement was prepared.
3962 sqlite3DbFree(db
, p
->zErrMsg
);
3963 p
->zErrMsg
= sqlite3DbStrDup(db
, "database schema has changed");
3964 /* If the schema-cookie from the database file matches the cookie
3965 ** stored with the in-memory representation of the schema, do
3966 ** not reload the schema from the database file.
3968 ** If virtual-tables are in use, this is not just an optimization.
3969 ** Often, v-tables store their data in other SQLite tables, which
3970 ** are queried from within xNext() and other v-table methods using
3971 ** prepared queries. If such a query is out-of-date, we do not want to
3972 ** discard the database schema, as the user code implementing the
3973 ** v-table would have to be ready for the sqlite3_vtab structure itself
3974 ** to be invalidated whenever sqlite3_step() is called from within
3975 ** a v-table method.
3977 if( db
->aDb
[pOp
->p1
].pSchema
->schema_cookie
!=iMeta
){
3978 sqlite3ResetOneSchema(db
, pOp
->p1
);
3983 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
3984 ** from being modified in sqlite3VdbeHalt(). If this statement is
3985 ** reprepared, changeCntOn will be set again. */
3988 if( rc
) goto abort_due_to_error
;
3992 /* Opcode: ReadCookie P1 P2 P3 * *
3994 ** Read cookie number P3 from database P1 and write it into register P2.
3995 ** P3==1 is the schema version. P3==2 is the database format.
3996 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
3997 ** the main database file and P1==1 is the database file used to store
3998 ** temporary tables.
4000 ** There must be a read-lock on the database (either a transaction
4001 ** must be started or there must be an open cursor) before
4002 ** executing this instruction.
4004 case OP_ReadCookie
: { /* out2 */
4009 assert( p
->bIsReader
);
4012 assert( pOp
->p3
<SQLITE_N_BTREE_META
);
4013 assert( iDb
>=0 && iDb
<db
->nDb
);
4014 assert( db
->aDb
[iDb
].pBt
!=0 );
4015 assert( DbMaskTest(p
->btreeMask
, iDb
) );
4017 sqlite3BtreeGetMeta(db
->aDb
[iDb
].pBt
, iCookie
, (u32
*)&iMeta
);
4018 pOut
= out2Prerelease(p
, pOp
);
4023 /* Opcode: SetCookie P1 P2 P3 * P5
4025 ** Write the integer value P3 into cookie number P2 of database P1.
4026 ** P2==1 is the schema version. P2==2 is the database format.
4027 ** P2==3 is the recommended pager cache
4028 ** size, and so forth. P1==0 is the main database file and P1==1 is the
4029 ** database file used to store temporary tables.
4031 ** A transaction must be started before executing this opcode.
4033 ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
4034 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
4035 ** has P5 set to 1, so that the internal schema version will be different
4036 ** from the database schema version, resulting in a schema reset.
4038 case OP_SetCookie
: {
4041 sqlite3VdbeIncrWriteCounter(p
, 0);
4042 assert( pOp
->p2
<SQLITE_N_BTREE_META
);
4043 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
4044 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
4045 assert( p
->readOnly
==0 );
4046 pDb
= &db
->aDb
[pOp
->p1
];
4047 assert( pDb
->pBt
!=0 );
4048 assert( sqlite3SchemaMutexHeld(db
, pOp
->p1
, 0) );
4049 /* See note about index shifting on OP_ReadCookie */
4050 rc
= sqlite3BtreeUpdateMeta(pDb
->pBt
, pOp
->p2
, pOp
->p3
);
4051 if( pOp
->p2
==BTREE_SCHEMA_VERSION
){
4052 /* When the schema cookie changes, record the new cookie internally */
4053 *(u32
*)&pDb
->pSchema
->schema_cookie
= *(u32
*)&pOp
->p3
- pOp
->p5
;
4054 db
->mDbFlags
|= DBFLAG_SchemaChange
;
4055 sqlite3FkClearTriggerCache(db
, pOp
->p1
);
4056 }else if( pOp
->p2
==BTREE_FILE_FORMAT
){
4057 /* Record changes in the file format */
4058 pDb
->pSchema
->file_format
= pOp
->p3
;
4061 /* Invalidate all prepared statements whenever the TEMP database
4062 ** schema is changed. Ticket #1644 */
4063 sqlite3ExpirePreparedStatements(db
, 0);
4066 if( rc
) goto abort_due_to_error
;
4070 /* Opcode: OpenRead P1 P2 P3 P4 P5
4071 ** Synopsis: root=P2 iDb=P3
4073 ** Open a read-only cursor for the database table whose root page is
4074 ** P2 in a database file. The database file is determined by P3.
4075 ** P3==0 means the main database, P3==1 means the database used for
4076 ** temporary tables, and P3>1 means used the corresponding attached
4077 ** database. Give the new cursor an identifier of P1. The P1
4078 ** values need not be contiguous but all P1 values should be small integers.
4079 ** It is an error for P1 to be negative.
4083 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4084 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
4085 ** of OP_SeekLE/OP_IdxLT)
4088 ** The P4 value may be either an integer (P4_INT32) or a pointer to
4089 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
4090 ** object, then table being opened must be an [index b-tree] where the
4091 ** KeyInfo object defines the content and collating
4092 ** sequence of that index b-tree. Otherwise, if P4 is an integer
4093 ** value, then the table being opened must be a [table b-tree] with a
4094 ** number of columns no less than the value of P4.
4096 ** See also: OpenWrite, ReopenIdx
4098 /* Opcode: ReopenIdx P1 P2 P3 P4 P5
4099 ** Synopsis: root=P2 iDb=P3
4101 ** The ReopenIdx opcode works like OP_OpenRead except that it first
4102 ** checks to see if the cursor on P1 is already open on the same
4103 ** b-tree and if it is this opcode becomes a no-op. In other words,
4104 ** if the cursor is already open, do not reopen it.
4106 ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
4107 ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
4108 ** be the same as every other ReopenIdx or OpenRead for the same cursor
4113 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4114 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
4115 ** of OP_SeekLE/OP_IdxLT)
4118 ** See also: OP_OpenRead, OP_OpenWrite
4120 /* Opcode: OpenWrite P1 P2 P3 P4 P5
4121 ** Synopsis: root=P2 iDb=P3
4123 ** Open a read/write cursor named P1 on the table or index whose root
4124 ** page is P2 (or whose root page is held in register P2 if the
4125 ** OPFLAG_P2ISREG bit is set in P5 - see below).
4127 ** The P4 value may be either an integer (P4_INT32) or a pointer to
4128 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
4129 ** object, then table being opened must be an [index b-tree] where the
4130 ** KeyInfo object defines the content and collating
4131 ** sequence of that index b-tree. Otherwise, if P4 is an integer
4132 ** value, then the table being opened must be a [table b-tree] with a
4133 ** number of columns no less than the value of P4.
4137 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4138 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
4139 ** of OP_SeekLE/OP_IdxLT)
4140 ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
4141 ** and subsequently delete entries in an index btree. This is a
4142 ** hint to the storage engine that the storage engine is allowed to
4143 ** ignore. The hint is not used by the official SQLite b*tree storage
4144 ** engine, but is used by COMDB2.
4145 ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
4146 ** as the root page, not the value of P2 itself.
4149 ** This instruction works like OpenRead except that it opens the cursor
4150 ** in read/write mode.
4152 ** See also: OP_OpenRead, OP_ReopenIdx
4154 case OP_ReopenIdx
: { /* ncycle */
4164 assert( pOp
->p5
==0 || pOp
->p5
==OPFLAG_SEEKEQ
);
4165 assert( pOp
->p4type
==P4_KEYINFO
);
4166 pCur
= p
->apCsr
[pOp
->p1
];
4167 if( pCur
&& pCur
->pgnoRoot
==(u32
)pOp
->p2
){
4168 assert( pCur
->iDb
==pOp
->p3
); /* Guaranteed by the code generator */
4169 assert( pCur
->eCurType
==CURTYPE_BTREE
);
4170 sqlite3BtreeClearCursor(pCur
->uc
.pCursor
);
4171 goto open_cursor_set_hints
;
4173 /* If the cursor is not currently open or is open on a different
4174 ** index, then fall through into OP_OpenRead to force a reopen */
4175 case OP_OpenRead
: /* ncycle */
4178 assert( pOp
->opcode
==OP_OpenWrite
|| pOp
->p5
==0 || pOp
->p5
==OPFLAG_SEEKEQ
);
4179 assert( p
->bIsReader
);
4180 assert( pOp
->opcode
==OP_OpenRead
|| pOp
->opcode
==OP_ReopenIdx
4181 || p
->readOnly
==0 );
4183 if( p
->expired
==1 ){
4184 rc
= SQLITE_ABORT_ROLLBACK
;
4185 goto abort_due_to_error
;
4192 assert( iDb
>=0 && iDb
<db
->nDb
);
4193 assert( DbMaskTest(p
->btreeMask
, iDb
) );
4194 pDb
= &db
->aDb
[iDb
];
4197 if( pOp
->opcode
==OP_OpenWrite
){
4198 assert( OPFLAG_FORDELETE
==BTREE_FORDELETE
);
4199 wrFlag
= BTREE_WRCSR
| (pOp
->p5
& OPFLAG_FORDELETE
);
4200 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
4201 if( pDb
->pSchema
->file_format
< p
->minWriteFileFormat
){
4202 p
->minWriteFileFormat
= pDb
->pSchema
->file_format
;
4207 if( pOp
->p5
& OPFLAG_P2ISREG
){
4209 assert( p2
<=(u32
)(p
->nMem
+1 - p
->nCursor
) );
4210 assert( pOp
->opcode
==OP_OpenWrite
);
4212 assert( memIsValid(pIn2
) );
4213 assert( (pIn2
->flags
& MEM_Int
)!=0 );
4214 sqlite3VdbeMemIntegerify(pIn2
);
4215 p2
= (int)pIn2
->u
.i
;
4216 /* The p2 value always comes from a prior OP_CreateBtree opcode and
4217 ** that opcode will always set the p2 value to 2 or more or else fail.
4218 ** If there were a failure, the prepared statement would have halted
4219 ** before reaching this instruction. */
4222 if( pOp
->p4type
==P4_KEYINFO
){
4223 pKeyInfo
= pOp
->p4
.pKeyInfo
;
4224 assert( pKeyInfo
->enc
==ENC(db
) );
4225 assert( pKeyInfo
->db
==db
);
4226 nField
= pKeyInfo
->nAllField
;
4227 }else if( pOp
->p4type
==P4_INT32
){
4230 assert( pOp
->p1
>=0 );
4231 assert( nField
>=0 );
4232 testcase( nField
==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
4233 pCur
= allocateCursor(p
, pOp
->p1
, nField
, CURTYPE_BTREE
);
4234 if( pCur
==0 ) goto no_mem
;
4237 pCur
->isOrdered
= 1;
4238 pCur
->pgnoRoot
= p2
;
4240 pCur
->wrFlag
= wrFlag
;
4242 rc
= sqlite3BtreeCursor(pX
, p2
, wrFlag
, pKeyInfo
, pCur
->uc
.pCursor
);
4243 pCur
->pKeyInfo
= pKeyInfo
;
4244 /* Set the VdbeCursor.isTable variable. Previous versions of
4245 ** SQLite used to check if the root-page flags were sane at this point
4246 ** and report database corruption if they were not, but this check has
4247 ** since moved into the btree layer. */
4248 pCur
->isTable
= pOp
->p4type
!=P4_KEYINFO
;
4250 open_cursor_set_hints
:
4251 assert( OPFLAG_BULKCSR
==BTREE_BULKLOAD
);
4252 assert( OPFLAG_SEEKEQ
==BTREE_SEEK_EQ
);
4253 testcase( pOp
->p5
& OPFLAG_BULKCSR
);
4254 testcase( pOp
->p2
& OPFLAG_SEEKEQ
);
4255 sqlite3BtreeCursorHintFlags(pCur
->uc
.pCursor
,
4256 (pOp
->p5
& (OPFLAG_BULKCSR
|OPFLAG_SEEKEQ
)));
4257 if( rc
) goto abort_due_to_error
;
4261 /* Opcode: OpenDup P1 P2 * * *
4263 ** Open a new cursor P1 that points to the same ephemeral table as
4264 ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
4265 ** opcode. Only ephemeral cursors may be duplicated.
4267 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
4269 case OP_OpenDup
: { /* ncycle */
4270 VdbeCursor
*pOrig
; /* The original cursor to be duplicated */
4271 VdbeCursor
*pCx
; /* The new cursor */
4273 pOrig
= p
->apCsr
[pOp
->p2
];
4275 assert( pOrig
->isEphemeral
); /* Only ephemeral cursors can be duplicated */
4277 pCx
= allocateCursor(p
, pOp
->p1
, pOrig
->nField
, CURTYPE_BTREE
);
4278 if( pCx
==0 ) goto no_mem
;
4280 pCx
->isEphemeral
= 1;
4281 pCx
->pKeyInfo
= pOrig
->pKeyInfo
;
4282 pCx
->isTable
= pOrig
->isTable
;
4283 pCx
->pgnoRoot
= pOrig
->pgnoRoot
;
4284 pCx
->isOrdered
= pOrig
->isOrdered
;
4285 pCx
->ub
.pBtx
= pOrig
->ub
.pBtx
;
4288 rc
= sqlite3BtreeCursor(pCx
->ub
.pBtx
, pCx
->pgnoRoot
, BTREE_WRCSR
,
4289 pCx
->pKeyInfo
, pCx
->uc
.pCursor
);
4290 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
4291 ** opened for a database. Since there is already an open cursor when this
4292 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
4293 assert( rc
==SQLITE_OK
);
4298 /* Opcode: OpenEphemeral P1 P2 P3 P4 P5
4299 ** Synopsis: nColumn=P2
4301 ** Open a new cursor P1 to a transient table.
4302 ** The cursor is always opened read/write even if
4303 ** the main database is read-only. The ephemeral
4304 ** table is deleted automatically when the cursor is closed.
4306 ** If the cursor P1 is already opened on an ephemeral table, the table
4307 ** is cleared (all content is erased).
4309 ** P2 is the number of columns in the ephemeral table.
4310 ** The cursor points to a BTree table if P4==0 and to a BTree index
4311 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
4312 ** that defines the format of keys in the index.
4314 ** The P5 parameter can be a mask of the BTREE_* flags defined
4315 ** in btree.h. These flags control aspects of the operation of
4316 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
4317 ** added automatically.
4319 ** If P3 is positive, then reg[P3] is modified slightly so that it
4320 ** can be used as zero-length data for OP_Insert. This is an optimization
4321 ** that avoids an extra OP_Blob opcode to initialize that register.
4323 /* Opcode: OpenAutoindex P1 P2 * P4 *
4324 ** Synopsis: nColumn=P2
4326 ** This opcode works the same as OP_OpenEphemeral. It has a
4327 ** different name to distinguish its use. Tables created using
4328 ** by this opcode will be used for automatically created transient
4329 ** indices in joins.
4331 case OP_OpenAutoindex
: /* ncycle */
4332 case OP_OpenEphemeral
: { /* ncycle */
4336 static const int vfsFlags
=
4337 SQLITE_OPEN_READWRITE
|
4338 SQLITE_OPEN_CREATE
|
4339 SQLITE_OPEN_EXCLUSIVE
|
4340 SQLITE_OPEN_DELETEONCLOSE
|
4341 SQLITE_OPEN_TRANSIENT_DB
;
4342 assert( pOp
->p1
>=0 );
4343 assert( pOp
->p2
>=0 );
4345 /* Make register reg[P3] into a value that can be used as the data
4346 ** form sqlite3BtreeInsert() where the length of the data is zero. */
4347 assert( pOp
->p2
==0 ); /* Only used when number of columns is zero */
4348 assert( pOp
->opcode
==OP_OpenEphemeral
);
4349 assert( aMem
[pOp
->p3
].flags
& MEM_Null
);
4350 aMem
[pOp
->p3
].n
= 0;
4351 aMem
[pOp
->p3
].z
= "";
4353 pCx
= p
->apCsr
[pOp
->p1
];
4354 if( pCx
&& !pCx
->noReuse
&& ALWAYS(pOp
->p2
<=pCx
->nField
) ){
4355 /* If the ephermeral table is already open and has no duplicates from
4356 ** OP_OpenDup, then erase all existing content so that the table is
4357 ** empty again, rather than creating a new table. */
4358 assert( pCx
->isEphemeral
);
4360 pCx
->cacheStatus
= CACHE_STALE
;
4361 rc
= sqlite3BtreeClearTable(pCx
->ub
.pBtx
, pCx
->pgnoRoot
, 0);
4363 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p2
, CURTYPE_BTREE
);
4364 if( pCx
==0 ) goto no_mem
;
4365 pCx
->isEphemeral
= 1;
4366 rc
= sqlite3BtreeOpen(db
->pVfs
, 0, db
, &pCx
->ub
.pBtx
,
4367 BTREE_OMIT_JOURNAL
| BTREE_SINGLE
| pOp
->p5
,
4369 if( rc
==SQLITE_OK
){
4370 rc
= sqlite3BtreeBeginTrans(pCx
->ub
.pBtx
, 1, 0);
4371 if( rc
==SQLITE_OK
){
4372 /* If a transient index is required, create it by calling
4373 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
4374 ** opening it. If a transient table is required, just use the
4375 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
4377 if( (pCx
->pKeyInfo
= pKeyInfo
= pOp
->p4
.pKeyInfo
)!=0 ){
4378 assert( pOp
->p4type
==P4_KEYINFO
);
4379 rc
= sqlite3BtreeCreateTable(pCx
->ub
.pBtx
, &pCx
->pgnoRoot
,
4380 BTREE_BLOBKEY
| pOp
->p5
);
4381 if( rc
==SQLITE_OK
){
4382 assert( pCx
->pgnoRoot
==SCHEMA_ROOT
+1 );
4383 assert( pKeyInfo
->db
==db
);
4384 assert( pKeyInfo
->enc
==ENC(db
) );
4385 rc
= sqlite3BtreeCursor(pCx
->ub
.pBtx
, pCx
->pgnoRoot
, BTREE_WRCSR
,
4386 pKeyInfo
, pCx
->uc
.pCursor
);
4390 pCx
->pgnoRoot
= SCHEMA_ROOT
;
4391 rc
= sqlite3BtreeCursor(pCx
->ub
.pBtx
, SCHEMA_ROOT
, BTREE_WRCSR
,
4392 0, pCx
->uc
.pCursor
);
4396 pCx
->isOrdered
= (pOp
->p5
!=BTREE_UNORDERED
);
4398 sqlite3BtreeClose(pCx
->ub
.pBtx
);
4402 if( rc
) goto abort_due_to_error
;
4407 /* Opcode: SorterOpen P1 P2 P3 P4 *
4409 ** This opcode works like OP_OpenEphemeral except that it opens
4410 ** a transient index that is specifically designed to sort large
4411 ** tables using an external merge-sort algorithm.
4413 ** If argument P3 is non-zero, then it indicates that the sorter may
4414 ** assume that a stable sort considering the first P3 fields of each
4415 ** key is sufficient to produce the required results.
4417 case OP_SorterOpen
: {
4420 assert( pOp
->p1
>=0 );
4421 assert( pOp
->p2
>=0 );
4422 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p2
, CURTYPE_SORTER
);
4423 if( pCx
==0 ) goto no_mem
;
4424 pCx
->pKeyInfo
= pOp
->p4
.pKeyInfo
;
4425 assert( pCx
->pKeyInfo
->db
==db
);
4426 assert( pCx
->pKeyInfo
->enc
==ENC(db
) );
4427 rc
= sqlite3VdbeSorterInit(db
, pOp
->p3
, pCx
);
4428 if( rc
) goto abort_due_to_error
;
4432 /* Opcode: SequenceTest P1 P2 * * *
4433 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4435 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4436 ** to P2. Regardless of whether or not the jump is taken, increment the
4437 ** the sequence value.
4439 case OP_SequenceTest
: {
4441 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4442 pC
= p
->apCsr
[pOp
->p1
];
4443 assert( isSorter(pC
) );
4444 if( (pC
->seqCount
++)==0 ){
4450 /* Opcode: OpenPseudo P1 P2 P3 * *
4451 ** Synopsis: P3 columns in r[P2]
4453 ** Open a new cursor that points to a fake table that contains a single
4454 ** row of data. The content of that one row is the content of memory
4455 ** register P2. In other words, cursor P1 becomes an alias for the
4456 ** MEM_Blob content contained in register P2.
4458 ** A pseudo-table created by this opcode is used to hold a single
4459 ** row output from the sorter so that the row can be decomposed into
4460 ** individual columns using the OP_Column opcode. The OP_Column opcode
4461 ** is the only cursor opcode that works with a pseudo-table.
4463 ** P3 is the number of fields in the records that will be stored by
4464 ** the pseudo-table.
4466 case OP_OpenPseudo
: {
4469 assert( pOp
->p1
>=0 );
4470 assert( pOp
->p3
>=0 );
4471 pCx
= allocateCursor(p
, pOp
->p1
, pOp
->p3
, CURTYPE_PSEUDO
);
4472 if( pCx
==0 ) goto no_mem
;
4474 pCx
->seekResult
= pOp
->p2
;
4476 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4477 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4478 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4479 ** which is a performance optimization */
4480 pCx
->uc
.pCursor
= sqlite3BtreeFakeValidCursor();
4481 assert( pOp
->p5
==0 );
4485 /* Opcode: Close P1 * * * *
4487 ** Close a cursor previously opened as P1. If P1 is not
4488 ** currently open, this instruction is a no-op.
4490 case OP_Close
: { /* ncycle */
4491 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4492 sqlite3VdbeFreeCursor(p
, p
->apCsr
[pOp
->p1
]);
4493 p
->apCsr
[pOp
->p1
] = 0;
4497 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4498 /* Opcode: ColumnsUsed P1 * * P4 *
4500 ** This opcode (which only exists if SQLite was compiled with
4501 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4502 ** table or index for cursor P1 are used. P4 is a 64-bit integer
4503 ** (P4_INT64) in which the first 63 bits are one for each of the
4504 ** first 63 columns of the table or index that are actually used
4505 ** by the cursor. The high-order bit is set if any column after
4506 ** the 64th is used.
4508 case OP_ColumnsUsed
: {
4510 pC
= p
->apCsr
[pOp
->p1
];
4511 assert( pC
->eCurType
==CURTYPE_BTREE
);
4512 pC
->maskUsed
= *(u64
*)pOp
->p4
.pI64
;
4517 /* Opcode: SeekGE P1 P2 P3 P4 *
4518 ** Synopsis: key=r[P3@P4]
4520 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4521 ** use the value in register P3 as the key. If cursor P1 refers
4522 ** to an SQL index, then P3 is the first in an array of P4 registers
4523 ** that are used as an unpacked index key.
4525 ** Reposition cursor P1 so that it points to the smallest entry that
4526 ** is greater than or equal to the key value. If there are no records
4527 ** greater than or equal to the key and P2 is not zero, then jump to P2.
4529 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4530 ** opcode will either land on a record that exactly matches the key, or
4531 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4532 ** this opcode must be followed by an IdxLE opcode with the same arguments.
4533 ** The IdxGT opcode will be skipped if this opcode succeeds, but the
4534 ** IdxGT opcode will be used on subsequent loop iterations. The
4535 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4536 ** is an equality search.
4538 ** This opcode leaves the cursor configured to move in forward order,
4539 ** from the beginning toward the end. In other words, the cursor is
4540 ** configured to use Next, not Prev.
4542 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
4544 /* Opcode: SeekGT P1 P2 P3 P4 *
4545 ** Synopsis: key=r[P3@P4]
4547 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4548 ** use the value in register P3 as a key. If cursor P1 refers
4549 ** to an SQL index, then P3 is the first in an array of P4 registers
4550 ** that are used as an unpacked index key.
4552 ** Reposition cursor P1 so that it points to the smallest entry that
4553 ** is greater than the key value. If there are no records greater than
4554 ** the key and P2 is not zero, then jump to P2.
4556 ** This opcode leaves the cursor configured to move in forward order,
4557 ** from the beginning toward the end. In other words, the cursor is
4558 ** configured to use Next, not Prev.
4560 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
4562 /* Opcode: SeekLT P1 P2 P3 P4 *
4563 ** Synopsis: key=r[P3@P4]
4565 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4566 ** use the value in register P3 as a key. If cursor P1 refers
4567 ** to an SQL index, then P3 is the first in an array of P4 registers
4568 ** that are used as an unpacked index key.
4570 ** Reposition cursor P1 so that it points to the largest entry that
4571 ** is less than the key value. If there are no records less than
4572 ** the key and P2 is not zero, then jump to P2.
4574 ** This opcode leaves the cursor configured to move in reverse order,
4575 ** from the end toward the beginning. In other words, the cursor is
4576 ** configured to use Prev, not Next.
4578 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
4580 /* Opcode: SeekLE P1 P2 P3 P4 *
4581 ** Synopsis: key=r[P3@P4]
4583 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
4584 ** use the value in register P3 as a key. If cursor P1 refers
4585 ** to an SQL index, then P3 is the first in an array of P4 registers
4586 ** that are used as an unpacked index key.
4588 ** Reposition cursor P1 so that it points to the largest entry that
4589 ** is less than or equal to the key value. If there are no records
4590 ** less than or equal to the key and P2 is not zero, then jump to P2.
4592 ** This opcode leaves the cursor configured to move in reverse order,
4593 ** from the end toward the beginning. In other words, the cursor is
4594 ** configured to use Prev, not Next.
4596 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4597 ** opcode will either land on a record that exactly matches the key, or
4598 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4599 ** this opcode must be followed by an IdxLE opcode with the same arguments.
4600 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
4601 ** IdxGE opcode will be used on subsequent loop iterations. The
4602 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4603 ** is an equality search.
4605 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
4607 case OP_SeekLT
: /* jump, in3, group, ncycle */
4608 case OP_SeekLE
: /* jump, in3, group, ncycle */
4609 case OP_SeekGE
: /* jump, in3, group, ncycle */
4610 case OP_SeekGT
: { /* jump, in3, group, ncycle */
4611 int res
; /* Comparison result */
4612 int oc
; /* Opcode */
4613 VdbeCursor
*pC
; /* The cursor to seek */
4614 UnpackedRecord r
; /* The key to seek for */
4615 int nField
; /* Number of columns or fields in the key */
4616 i64 iKey
; /* The rowid we are to seek to */
4617 int eqOnly
; /* Only interested in == results */
4619 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
4620 assert( pOp
->p2
!=0 );
4621 pC
= p
->apCsr
[pOp
->p1
];
4623 assert( pC
->eCurType
==CURTYPE_BTREE
);
4624 assert( OP_SeekLE
== OP_SeekLT
+1 );
4625 assert( OP_SeekGE
== OP_SeekLT
+2 );
4626 assert( OP_SeekGT
== OP_SeekLT
+3 );
4627 assert( pC
->isOrdered
);
4628 assert( pC
->uc
.pCursor
!=0 );
4633 pC
->seekOp
= pOp
->opcode
;
4636 pC
->deferredMoveto
= 0;
4637 pC
->cacheStatus
= CACHE_STALE
;
4639 u16 flags3
, newType
;
4640 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
4641 assert( sqlite3BtreeCursorHasHint(pC
->uc
.pCursor
, BTREE_SEEK_EQ
)==0
4644 /* The input value in P3 might be of any type: integer, real, string,
4645 ** blob, or NULL. But it needs to be an integer before we can do
4646 ** the seek, so convert it. */
4647 pIn3
= &aMem
[pOp
->p3
];
4648 flags3
= pIn3
->flags
;
4649 if( (flags3
& (MEM_Int
|MEM_Real
|MEM_IntReal
|MEM_Str
))==MEM_Str
){
4650 applyNumericAffinity(pIn3
, 0);
4652 iKey
= sqlite3VdbeIntValue(pIn3
); /* Get the integer key value */
4653 newType
= pIn3
->flags
; /* Record the type after applying numeric affinity */
4654 pIn3
->flags
= flags3
; /* But convert the type back to its original */
4656 /* If the P3 value could not be converted into an integer without
4657 ** loss of information, then special processing is required... */
4658 if( (newType
& (MEM_Int
|MEM_IntReal
))==0 ){
4660 if( (newType
& MEM_Real
)==0 ){
4661 if( (newType
& MEM_Null
) || oc
>=OP_SeekGE
){
4662 VdbeBranchTaken(1,2);
4665 rc
= sqlite3BtreeLast(pC
->uc
.pCursor
, &res
);
4666 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
4667 goto seek_not_found
;
4670 c
= sqlite3IntFloatCompare(iKey
, pIn3
->u
.r
);
4672 /* If the approximation iKey is larger than the actual real search
4673 ** term, substitute >= for > and < for <=. e.g. if the search term
4674 ** is 4.9 and the integer approximation 5:
4676 ** (x > 4.9) -> (x >= 5)
4677 ** (x <= 4.9) -> (x < 5)
4680 assert( OP_SeekGE
==(OP_SeekGT
-1) );
4681 assert( OP_SeekLT
==(OP_SeekLE
-1) );
4682 assert( (OP_SeekLE
& 0x0001)==(OP_SeekGT
& 0x0001) );
4683 if( (oc
& 0x0001)==(OP_SeekGT
& 0x0001) ) oc
--;
4686 /* If the approximation iKey is smaller than the actual real search
4687 ** term, substitute <= for < and > for >=. */
4689 assert( OP_SeekLE
==(OP_SeekLT
+1) );
4690 assert( OP_SeekGT
==(OP_SeekGE
+1) );
4691 assert( (OP_SeekLT
& 0x0001)==(OP_SeekGE
& 0x0001) );
4692 if( (oc
& 0x0001)==(OP_SeekLT
& 0x0001) ) oc
++;
4695 rc
= sqlite3BtreeTableMoveto(pC
->uc
.pCursor
, (u64
)iKey
, 0, &res
);
4696 pC
->movetoTarget
= iKey
; /* Used by OP_Delete */
4697 if( rc
!=SQLITE_OK
){
4698 goto abort_due_to_error
;
4701 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4702 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4703 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4704 ** with the same key.
4706 if( sqlite3BtreeCursorHasHint(pC
->uc
.pCursor
, BTREE_SEEK_EQ
) ){
4708 assert( pOp
->opcode
==OP_SeekGE
|| pOp
->opcode
==OP_SeekLE
);
4709 assert( pOp
[1].opcode
==OP_IdxLT
|| pOp
[1].opcode
==OP_IdxGT
);
4710 assert( pOp
->opcode
==OP_SeekGE
|| pOp
[1].opcode
==OP_IdxLT
);
4711 assert( pOp
->opcode
==OP_SeekLE
|| pOp
[1].opcode
==OP_IdxGT
);
4712 assert( pOp
[1].p1
==pOp
[0].p1
);
4713 assert( pOp
[1].p2
==pOp
[0].p2
);
4714 assert( pOp
[1].p3
==pOp
[0].p3
);
4715 assert( pOp
[1].p4
.i
==pOp
[0].p4
.i
);
4719 assert( pOp
->p4type
==P4_INT32
);
4721 r
.pKeyInfo
= pC
->pKeyInfo
;
4722 r
.nField
= (u16
)nField
;
4724 /* The next line of code computes as follows, only faster:
4725 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
4726 ** r.default_rc = -1;
4728 ** r.default_rc = +1;
4731 r
.default_rc
= ((1 & (oc
- OP_SeekLT
)) ? -1 : +1);
4732 assert( oc
!=OP_SeekGT
|| r
.default_rc
==-1 );
4733 assert( oc
!=OP_SeekLE
|| r
.default_rc
==-1 );
4734 assert( oc
!=OP_SeekGE
|| r
.default_rc
==+1 );
4735 assert( oc
!=OP_SeekLT
|| r
.default_rc
==+1 );
4737 r
.aMem
= &aMem
[pOp
->p3
];
4741 for(i
=0; i
<r
.nField
; i
++){
4742 assert( memIsValid(&r
.aMem
[i
]) );
4743 if( i
>0 ) REGISTER_TRACE(pOp
->p3
+i
, &r
.aMem
[i
]);
4748 rc
= sqlite3BtreeIndexMoveto(pC
->uc
.pCursor
, &r
, &res
);
4749 if( rc
!=SQLITE_OK
){
4750 goto abort_due_to_error
;
4752 if( eqOnly
&& r
.eqSeen
==0 ){
4754 goto seek_not_found
;
4758 sqlite3_search_count
++;
4760 if( oc
>=OP_SeekGE
){ assert( oc
==OP_SeekGE
|| oc
==OP_SeekGT
);
4761 if( res
<0 || (res
==0 && oc
==OP_SeekGT
) ){
4763 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, 0);
4764 if( rc
!=SQLITE_OK
){
4765 if( rc
==SQLITE_DONE
){
4769 goto abort_due_to_error
;
4776 assert( oc
==OP_SeekLT
|| oc
==OP_SeekLE
);
4777 if( res
>0 || (res
==0 && oc
==OP_SeekLT
) ){
4779 rc
= sqlite3BtreePrevious(pC
->uc
.pCursor
, 0);
4780 if( rc
!=SQLITE_OK
){
4781 if( rc
==SQLITE_DONE
){
4785 goto abort_due_to_error
;
4789 /* res might be negative because the table is empty. Check to
4790 ** see if this is the case.
4792 res
= sqlite3BtreeEof(pC
->uc
.pCursor
);
4796 assert( pOp
->p2
>0 );
4797 VdbeBranchTaken(res
!=0,2);
4801 assert( pOp
[1].opcode
==OP_IdxLT
|| pOp
[1].opcode
==OP_IdxGT
);
4802 pOp
++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
4808 /* Opcode: SeekScan P1 P2 * * P5
4809 ** Synopsis: Scan-ahead up to P1 rows
4811 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this
4812 ** opcode must be immediately followed by OP_SeekGE. This constraint is
4813 ** checked by assert() statements.
4815 ** This opcode uses the P1 through P4 operands of the subsequent
4816 ** OP_SeekGE. In the text that follows, the operands of the subsequent
4817 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
4818 ** the P1, P2 and P5 operands of this opcode are also used, and are called
4819 ** This.P1, This.P2 and This.P5.
4821 ** This opcode helps to optimize IN operators on a multi-column index
4822 ** where the IN operator is on the later terms of the index by avoiding
4823 ** unnecessary seeks on the btree, substituting steps to the next row
4824 ** of the b-tree instead. A correct answer is obtained if this opcode
4825 ** is omitted or is a no-op.
4827 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4828 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
4829 ** to. Call this SeekGE.P3/P4 row the "target".
4831 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4832 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
4834 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4835 ** might be the target row, or it might be near and slightly before the
4836 ** target row, or it might be after the target row. If the cursor is
4837 ** currently before the target row, then this opcode attempts to position
4838 ** the cursor on or after the target row by invoking sqlite3BtreeStep()
4839 ** on the cursor between 1 and This.P1 times.
4841 ** The This.P5 parameter is a flag that indicates what to do if the
4842 ** cursor ends up pointing at a valid row that is past the target
4843 ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
4844 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
4845 ** case occurs when there are no inequality constraints to the right of
4846 ** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
4847 ** occurs when there are inequality constraints to the right of the IN
4848 ** operator. In that case, the This.P2 will point either directly to or
4849 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
4852 ** Possible outcomes from this opcode:<ol>
4854 ** <li> If the cursor is initally not pointed to any valid row, then
4855 ** fall through into the subsequent OP_SeekGE opcode.
4857 ** <li> If the cursor is left pointing to a row that is before the target
4858 ** row, even after making as many as This.P1 calls to
4859 ** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
4861 ** <li> If the cursor is left pointing at the target row, either because it
4862 ** was at the target row to begin with or because one or more
4863 ** sqlite3BtreeNext() calls moved the cursor to the target row,
4864 ** then jump to This.P2..,
4866 ** <li> If the cursor started out before the target row and a call to
4867 ** to sqlite3BtreeNext() moved the cursor off the end of the index
4868 ** (indicating that the target row definitely does not exist in the
4869 ** btree) then jump to SeekGE.P2, ending the loop.
4871 ** <li> If the cursor ends up on a valid row that is past the target row
4872 ** (indicating that the target row does not exist in the btree) then
4873 ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
4876 case OP_SeekScan
: { /* ncycle */
4882 assert( pOp
[1].opcode
==OP_SeekGE
);
4884 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
4885 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
4886 ** opcode past the OP_SeekGE itself. */
4887 assert( pOp
->p2
>=(int)(pOp
-aOp
)+2 );
4890 /* There are no inequality constraints following the IN constraint. */
4891 assert( pOp
[1].p1
==aOp
[pOp
->p2
-1].p1
);
4892 assert( pOp
[1].p2
==aOp
[pOp
->p2
-1].p2
);
4893 assert( pOp
[1].p3
==aOp
[pOp
->p2
-1].p3
);
4894 assert( aOp
[pOp
->p2
-1].opcode
==OP_IdxGT
4895 || aOp
[pOp
->p2
-1].opcode
==OP_IdxGE
);
4896 testcase( aOp
[pOp
->p2
-1].opcode
==OP_IdxGE
);
4898 /* There are inequality constraints. */
4899 assert( pOp
->p2
==(int)(pOp
-aOp
)+2 );
4900 assert( aOp
[pOp
->p2
-1].opcode
==OP_SeekGE
);
4904 assert( pOp
->p1
>0 );
4905 pC
= p
->apCsr
[pOp
[1].p1
];
4907 assert( pC
->eCurType
==CURTYPE_BTREE
);
4908 assert( !pC
->isTable
);
4909 if( !sqlite3BtreeCursorIsValidNN(pC
->uc
.pCursor
) ){
4911 if( db
->flags
&SQLITE_VdbeTrace
){
4912 printf("... cursor not valid - fall through\n");
4919 r
.pKeyInfo
= pC
->pKeyInfo
;
4920 r
.nField
= (u16
)pOp
[1].p4
.i
;
4922 r
.aMem
= &aMem
[pOp
[1].p3
];
4926 for(i
=0; i
<r
.nField
; i
++){
4927 assert( memIsValid(&r
.aMem
[i
]) );
4928 REGISTER_TRACE(pOp
[1].p3
+i
, &aMem
[pOp
[1].p3
+i
]);
4932 res
= 0; /* Not needed. Only used to silence a warning. */
4934 rc
= sqlite3VdbeIdxKeyCompare(db
, pC
, &r
, &res
);
4935 if( rc
) goto abort_due_to_error
;
4936 if( res
>0 && pOp
->p5
==0 ){
4937 seekscan_search_fail
:
4938 /* Jump to SeekGE.P2, ending the loop */
4940 if( db
->flags
&SQLITE_VdbeTrace
){
4941 printf("... %d steps and then skip\n", pOp
->p1
- nStep
);
4944 VdbeBranchTaken(1,3);
4949 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
4951 if( db
->flags
&SQLITE_VdbeTrace
){
4952 printf("... %d steps and then success\n", pOp
->p1
- nStep
);
4955 VdbeBranchTaken(2,3);
4961 if( db
->flags
&SQLITE_VdbeTrace
){
4962 printf("... fall through after %d steps\n", pOp
->p1
);
4965 VdbeBranchTaken(0,3);
4969 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, 0);
4971 if( rc
==SQLITE_DONE
){
4973 goto seekscan_search_fail
;
4975 goto abort_due_to_error
;
4984 /* Opcode: SeekHit P1 P2 P3 * *
4985 ** Synopsis: set P2<=seekHit<=P3
4987 ** Increase or decrease the seekHit value for cursor P1, if necessary,
4988 ** so that it is no less than P2 and no greater than P3.
4990 ** The seekHit integer represents the maximum of terms in an index for which
4991 ** there is known to be at least one match. If the seekHit value is smaller
4992 ** than the total number of equality terms in an index lookup, then the
4993 ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4994 ** early, thus saving work. This is part of the IN-early-out optimization.
4996 ** P1 must be a valid b-tree cursor.
4998 case OP_SeekHit
: { /* ncycle */
5000 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5001 pC
= p
->apCsr
[pOp
->p1
];
5003 assert( pOp
->p3
>=pOp
->p2
);
5004 if( pC
->seekHit
<pOp
->p2
){
5006 if( db
->flags
&SQLITE_VdbeTrace
){
5007 printf("seekHit changes from %d to %d\n", pC
->seekHit
, pOp
->p2
);
5010 pC
->seekHit
= pOp
->p2
;
5011 }else if( pC
->seekHit
>pOp
->p3
){
5013 if( db
->flags
&SQLITE_VdbeTrace
){
5014 printf("seekHit changes from %d to %d\n", pC
->seekHit
, pOp
->p3
);
5017 pC
->seekHit
= pOp
->p3
;
5022 /* Opcode: IfNotOpen P1 P2 * * *
5023 ** Synopsis: if( !csr[P1] ) goto P2
5025 ** If cursor P1 is not open or if P1 is set to a NULL row using the
5026 ** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
5028 case OP_IfNotOpen
: { /* jump */
5031 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5032 pCur
= p
->apCsr
[pOp
->p1
];
5033 VdbeBranchTaken(pCur
==0 || pCur
->nullRow
, 2);
5034 if( pCur
==0 || pCur
->nullRow
){
5035 goto jump_to_p2_and_check_for_interrupt
;
5040 /* Opcode: Found P1 P2 P3 P4 *
5041 ** Synopsis: key=r[P3@P4]
5043 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5044 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
5047 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
5048 ** is a prefix of any entry in P1 then a jump is made to P2 and
5049 ** P1 is left pointing at the matching entry.
5051 ** This operation leaves the cursor in a state where it can be
5052 ** advanced in the forward direction. The Next instruction will work,
5053 ** but not the Prev instruction.
5055 ** See also: NotFound, NoConflict, NotExists. SeekGe
5057 /* Opcode: NotFound P1 P2 P3 P4 *
5058 ** Synopsis: key=r[P3@P4]
5060 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5061 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
5064 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
5065 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
5066 ** does contain an entry whose prefix matches the P3/P4 record then control
5067 ** falls through to the next instruction and P1 is left pointing at the
5070 ** This operation leaves the cursor in a state where it cannot be
5071 ** advanced in either direction. In other words, the Next and Prev
5072 ** opcodes do not work after this operation.
5074 ** See also: Found, NotExists, NoConflict, IfNoHope
5076 /* Opcode: IfNoHope P1 P2 P3 P4 *
5077 ** Synopsis: key=r[P3@P4]
5079 ** Register P3 is the first of P4 registers that form an unpacked
5080 ** record. Cursor P1 is an index btree. P2 is a jump destination.
5081 ** In other words, the operands to this opcode are the same as the
5082 ** operands to OP_NotFound and OP_IdxGT.
5084 ** This opcode is an optimization attempt only. If this opcode always
5085 ** falls through, the correct answer is still obtained, but extra works
5088 ** A value of N in the seekHit flag of cursor P1 means that there exists
5089 ** a key P3:N that will match some record in the index. We want to know
5090 ** if it is possible for a record P3:P4 to match some record in the
5091 ** index. If it is not possible, we can skips some work. So if seekHit
5092 ** is less than P4, attempt to find out if a match is possible by running
5095 ** This opcode is used in IN clause processing for a multi-column key.
5096 ** If an IN clause is attached to an element of the key other than the
5097 ** left-most element, and if there are no matches on the most recent
5098 ** seek over the whole key, then it might be that one of the key element
5099 ** to the left is prohibiting a match, and hence there is "no hope" of
5100 ** any match regardless of how many IN clause elements are checked.
5101 ** In such a case, we abandon the IN clause search early, using this
5102 ** opcode. The opcode name comes from the fact that the
5103 ** jump is taken if there is "no hope" of achieving a match.
5105 ** See also: NotFound, SeekHit
5107 /* Opcode: NoConflict P1 P2 P3 P4 *
5108 ** Synopsis: key=r[P3@P4]
5110 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5111 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
5114 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
5115 ** contains any NULL value, jump immediately to P2. If all terms of the
5116 ** record are not-NULL then a check is done to determine if any row in the
5117 ** P1 index btree has a matching key prefix. If there are no matches, jump
5118 ** immediately to P2. If there is a match, fall through and leave the P1
5119 ** cursor pointing to the matching row.
5121 ** This opcode is similar to OP_NotFound with the exceptions that the
5122 ** branch is always taken if any part of the search key input is NULL.
5124 ** This operation leaves the cursor in a state where it cannot be
5125 ** advanced in either direction. In other words, the Next and Prev
5126 ** opcodes do not work after this operation.
5128 ** See also: NotFound, Found, NotExists
5130 case OP_IfNoHope
: { /* jump, in3, ncycle */
5132 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5133 pC
= p
->apCsr
[pOp
->p1
];
5136 if( db
->flags
&SQLITE_VdbeTrace
){
5137 printf("seekHit is %d\n", pC
->seekHit
);
5140 if( pC
->seekHit
>=pOp
->p4
.i
) break;
5141 /* Fall through into OP_NotFound */
5142 /* no break */ deliberate_fall_through
5144 case OP_NoConflict
: /* jump, in3, ncycle */
5145 case OP_NotFound
: /* jump, in3, ncycle */
5146 case OP_Found
: { /* jump, in3, ncycle */
5150 UnpackedRecord
*pIdxKey
;
5154 if( pOp
->opcode
!=OP_NoConflict
) sqlite3_found_count
++;
5157 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5158 assert( pOp
->p4type
==P4_INT32
);
5159 pC
= p
->apCsr
[pOp
->p1
];
5162 pC
->seekOp
= pOp
->opcode
;
5164 r
.aMem
= &aMem
[pOp
->p3
];
5165 assert( pC
->eCurType
==CURTYPE_BTREE
);
5166 assert( pC
->uc
.pCursor
!=0 );
5167 assert( pC
->isTable
==0 );
5168 r
.nField
= (u16
)pOp
->p4
.i
;
5170 /* Key values in an array of registers */
5171 r
.pKeyInfo
= pC
->pKeyInfo
;
5174 for(ii
=0; ii
<r
.nField
; ii
++){
5175 assert( memIsValid(&r
.aMem
[ii
]) );
5176 assert( (r
.aMem
[ii
].flags
& MEM_Zero
)==0 || r
.aMem
[ii
].n
==0 );
5177 if( ii
) REGISTER_TRACE(pOp
->p3
+ii
, &r
.aMem
[ii
]);
5180 rc
= sqlite3BtreeIndexMoveto(pC
->uc
.pCursor
, &r
, &pC
->seekResult
);
5182 /* Composite key generated by OP_MakeRecord */
5183 assert( r
.aMem
->flags
& MEM_Blob
);
5184 assert( pOp
->opcode
!=OP_NoConflict
);
5185 rc
= ExpandBlob(r
.aMem
);
5186 assert( rc
==SQLITE_OK
|| rc
==SQLITE_NOMEM
);
5187 if( rc
) goto no_mem
;
5188 pIdxKey
= sqlite3VdbeAllocUnpackedRecord(pC
->pKeyInfo
);
5189 if( pIdxKey
==0 ) goto no_mem
;
5190 sqlite3VdbeRecordUnpack(pC
->pKeyInfo
, r
.aMem
->n
, r
.aMem
->z
, pIdxKey
);
5191 pIdxKey
->default_rc
= 0;
5192 rc
= sqlite3BtreeIndexMoveto(pC
->uc
.pCursor
, pIdxKey
, &pC
->seekResult
);
5193 sqlite3DbFreeNN(db
, pIdxKey
);
5195 if( rc
!=SQLITE_OK
){
5196 goto abort_due_to_error
;
5198 alreadyExists
= (pC
->seekResult
==0);
5199 pC
->nullRow
= 1-alreadyExists
;
5200 pC
->deferredMoveto
= 0;
5201 pC
->cacheStatus
= CACHE_STALE
;
5202 if( pOp
->opcode
==OP_Found
){
5203 VdbeBranchTaken(alreadyExists
!=0,2);
5204 if( alreadyExists
) goto jump_to_p2
;
5206 if( !alreadyExists
){
5207 VdbeBranchTaken(1,2);
5210 if( pOp
->opcode
==OP_NoConflict
){
5211 /* For the OP_NoConflict opcode, take the jump if any of the
5212 ** input fields are NULL, since any key with a NULL will not
5214 for(ii
=0; ii
<r
.nField
; ii
++){
5215 if( r
.aMem
[ii
].flags
& MEM_Null
){
5216 VdbeBranchTaken(1,2);
5221 VdbeBranchTaken(0,2);
5222 if( pOp
->opcode
==OP_IfNoHope
){
5223 pC
->seekHit
= pOp
->p4
.i
;
5229 /* Opcode: SeekRowid P1 P2 P3 * *
5230 ** Synopsis: intkey=r[P3]
5232 ** P1 is the index of a cursor open on an SQL table btree (with integer
5233 ** keys). If register P3 does not contain an integer or if P1 does not
5234 ** contain a record with rowid P3 then jump immediately to P2.
5235 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
5236 ** a record with rowid P3 then
5237 ** leave the cursor pointing at that record and fall through to the next
5240 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists
5241 ** the P3 register must be guaranteed to contain an integer value. With this
5242 ** opcode, register P3 might not contain an integer.
5244 ** The OP_NotFound opcode performs the same operation on index btrees
5245 ** (with arbitrary multi-value keys).
5247 ** This opcode leaves the cursor in a state where it cannot be advanced
5248 ** in either direction. In other words, the Next and Prev opcodes will
5249 ** not work following this opcode.
5251 ** See also: Found, NotFound, NoConflict, SeekRowid
5253 /* Opcode: NotExists P1 P2 P3 * *
5254 ** Synopsis: intkey=r[P3]
5256 ** P1 is the index of a cursor open on an SQL table btree (with integer
5257 ** keys). P3 is an integer rowid. If P1 does not contain a record with
5258 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
5259 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
5260 ** leave the cursor pointing at that record and fall through to the next
5263 ** The OP_SeekRowid opcode performs the same operation but also allows the
5264 ** P3 register to contain a non-integer value, in which case the jump is
5265 ** always taken. This opcode requires that P3 always contain an integer.
5267 ** The OP_NotFound opcode performs the same operation on index btrees
5268 ** (with arbitrary multi-value keys).
5270 ** This opcode leaves the cursor in a state where it cannot be advanced
5271 ** in either direction. In other words, the Next and Prev opcodes will
5272 ** not work following this opcode.
5274 ** See also: Found, NotFound, NoConflict, SeekRowid
5276 case OP_SeekRowid
: { /* jump, in3, ncycle */
5282 pIn3
= &aMem
[pOp
->p3
];
5283 testcase( pIn3
->flags
& MEM_Int
);
5284 testcase( pIn3
->flags
& MEM_IntReal
);
5285 testcase( pIn3
->flags
& MEM_Real
);
5286 testcase( (pIn3
->flags
& (MEM_Str
|MEM_Int
))==MEM_Str
);
5287 if( (pIn3
->flags
& (MEM_Int
|MEM_IntReal
))==0 ){
5288 /* If pIn3->u.i does not contain an integer, compute iKey as the
5289 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
5290 ** into an integer without loss of information. Take care to avoid
5291 ** changing the datatype of pIn3, however, as it is used by other
5292 ** parts of the prepared statement. */
5294 applyAffinity(&x
, SQLITE_AFF_NUMERIC
, encoding
);
5295 if( (x
.flags
& MEM_Int
)==0 ) goto jump_to_p2
;
5297 goto notExistsWithKey
;
5299 /* Fall through into OP_NotExists */
5300 /* no break */ deliberate_fall_through
5301 case OP_NotExists
: /* jump, in3, ncycle */
5302 pIn3
= &aMem
[pOp
->p3
];
5303 assert( (pIn3
->flags
& MEM_Int
)!=0 || pOp
->opcode
==OP_SeekRowid
);
5304 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5307 pC
= p
->apCsr
[pOp
->p1
];
5310 if( pOp
->opcode
==OP_SeekRowid
) pC
->seekOp
= OP_SeekRowid
;
5312 assert( pC
->isTable
);
5313 assert( pC
->eCurType
==CURTYPE_BTREE
);
5314 pCrsr
= pC
->uc
.pCursor
;
5317 rc
= sqlite3BtreeTableMoveto(pCrsr
, iKey
, 0, &res
);
5318 assert( rc
==SQLITE_OK
|| res
==0 );
5319 pC
->movetoTarget
= iKey
; /* Used by OP_Delete */
5321 pC
->cacheStatus
= CACHE_STALE
;
5322 pC
->deferredMoveto
= 0;
5323 VdbeBranchTaken(res
!=0,2);
5324 pC
->seekResult
= res
;
5326 assert( rc
==SQLITE_OK
);
5328 rc
= SQLITE_CORRUPT_BKPT
;
5333 if( rc
) goto abort_due_to_error
;
5337 /* Opcode: Sequence P1 P2 * * *
5338 ** Synopsis: r[P2]=cursor[P1].ctr++
5340 ** Find the next available sequence number for cursor P1.
5341 ** Write the sequence number into register P2.
5342 ** The sequence number on the cursor is incremented after this
5345 case OP_Sequence
: { /* out2 */
5346 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5347 assert( p
->apCsr
[pOp
->p1
]!=0 );
5348 assert( p
->apCsr
[pOp
->p1
]->eCurType
!=CURTYPE_VTAB
);
5349 pOut
= out2Prerelease(p
, pOp
);
5350 pOut
->u
.i
= p
->apCsr
[pOp
->p1
]->seqCount
++;
5355 /* Opcode: NewRowid P1 P2 P3 * *
5356 ** Synopsis: r[P2]=rowid
5358 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
5359 ** The record number is not previously used as a key in the database
5360 ** table that cursor P1 points to. The new record number is written
5361 ** written to register P2.
5363 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
5364 ** the largest previously generated record number. No new record numbers are
5365 ** allowed to be less than this value. When this value reaches its maximum,
5366 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
5367 ** generated record number. This P3 mechanism is used to help implement the
5368 ** AUTOINCREMENT feature.
5370 case OP_NewRowid
: { /* out2 */
5371 i64 v
; /* The new rowid */
5372 VdbeCursor
*pC
; /* Cursor of table to get the new rowid */
5373 int res
; /* Result of an sqlite3BtreeLast() */
5374 int cnt
; /* Counter to limit the number of searches */
5375 #ifndef SQLITE_OMIT_AUTOINCREMENT
5376 Mem
*pMem
; /* Register holding largest rowid for AUTOINCREMENT */
5377 VdbeFrame
*pFrame
; /* Root frame of VDBE */
5382 pOut
= out2Prerelease(p
, pOp
);
5383 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5384 pC
= p
->apCsr
[pOp
->p1
];
5386 assert( pC
->isTable
);
5387 assert( pC
->eCurType
==CURTYPE_BTREE
);
5388 assert( pC
->uc
.pCursor
!=0 );
5390 /* The next rowid or record number (different terms for the same
5391 ** thing) is obtained in a two-step algorithm.
5393 ** First we attempt to find the largest existing rowid and add one
5394 ** to that. But if the largest existing rowid is already the maximum
5395 ** positive integer, we have to fall through to the second
5396 ** probabilistic algorithm
5398 ** The second algorithm is to select a rowid at random and see if
5399 ** it already exists in the table. If it does not exist, we have
5400 ** succeeded. If the random rowid does exist, we select a new one
5401 ** and try again, up to 100 times.
5403 assert( pC
->isTable
);
5405 #ifdef SQLITE_32BIT_ROWID
5406 # define MAX_ROWID 0x7fffffff
5408 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
5409 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
5410 ** to provide the constant while making all compilers happy.
5412 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
5415 if( !pC
->useRandomRowid
){
5416 rc
= sqlite3BtreeLast(pC
->uc
.pCursor
, &res
);
5417 if( rc
!=SQLITE_OK
){
5418 goto abort_due_to_error
;
5421 v
= 1; /* IMP: R-61914-48074 */
5423 assert( sqlite3BtreeCursorIsValid(pC
->uc
.pCursor
) );
5424 v
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5426 pC
->useRandomRowid
= 1;
5428 v
++; /* IMP: R-29538-34987 */
5433 #ifndef SQLITE_OMIT_AUTOINCREMENT
5435 /* Assert that P3 is a valid memory cell. */
5436 assert( pOp
->p3
>0 );
5438 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
5439 /* Assert that P3 is a valid memory cell. */
5440 assert( pOp
->p3
<=pFrame
->nMem
);
5441 pMem
= &pFrame
->aMem
[pOp
->p3
];
5443 /* Assert that P3 is a valid memory cell. */
5444 assert( pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
5445 pMem
= &aMem
[pOp
->p3
];
5446 memAboutToChange(p
, pMem
);
5448 assert( memIsValid(pMem
) );
5450 REGISTER_TRACE(pOp
->p3
, pMem
);
5451 sqlite3VdbeMemIntegerify(pMem
);
5452 assert( (pMem
->flags
& MEM_Int
)!=0 ); /* mem(P3) holds an integer */
5453 if( pMem
->u
.i
==MAX_ROWID
|| pC
->useRandomRowid
){
5454 rc
= SQLITE_FULL
; /* IMP: R-17817-00630 */
5455 goto abort_due_to_error
;
5457 if( v
<pMem
->u
.i
+1 ){
5463 if( pC
->useRandomRowid
){
5464 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
5465 ** largest possible integer (9223372036854775807) then the database
5466 ** engine starts picking positive candidate ROWIDs at random until
5467 ** it finds one that is not previously used. */
5468 assert( pOp
->p3
==0 ); /* We cannot be in random rowid mode if this is
5469 ** an AUTOINCREMENT table. */
5472 sqlite3_randomness(sizeof(v
), &v
);
5473 v
&= (MAX_ROWID
>>1); v
++; /* Ensure that v is greater than zero */
5474 }while( ((rc
= sqlite3BtreeTableMoveto(pC
->uc
.pCursor
, (u64
)v
,
5475 0, &res
))==SQLITE_OK
)
5478 if( rc
) goto abort_due_to_error
;
5480 rc
= SQLITE_FULL
; /* IMP: R-38219-53002 */
5481 goto abort_due_to_error
;
5483 assert( v
>0 ); /* EV: R-40812-03570 */
5485 pC
->deferredMoveto
= 0;
5486 pC
->cacheStatus
= CACHE_STALE
;
5492 /* Opcode: Insert P1 P2 P3 P4 P5
5493 ** Synopsis: intkey=r[P3] data=r[P2]
5495 ** Write an entry into the table of cursor P1. A new entry is
5496 ** created if it doesn't already exist or the data for an existing
5497 ** entry is overwritten. The data is the value MEM_Blob stored in register
5498 ** number P2. The key is stored in register P3. The key must
5501 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5502 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
5503 ** then rowid is stored for subsequent return by the
5504 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
5506 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5507 ** run faster by avoiding an unnecessary seek on cursor P1. However,
5508 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5509 ** seeks on the cursor or if the most recent seek used a key equal to P3.
5511 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5512 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5513 ** is part of an INSERT operation. The difference is only important to
5516 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
5517 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5518 ** following a successful insert.
5520 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5521 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
5522 ** and register P2 becomes ephemeral. If the cursor is changed, the
5523 ** value of register P2 will then change. Make sure this does not
5524 ** cause any problems.)
5526 ** This instruction only works on tables. The equivalent instruction
5527 ** for indices is OP_IdxInsert.
5530 Mem
*pData
; /* MEM cell holding data for the record to be inserted */
5531 Mem
*pKey
; /* MEM cell holding key for the record */
5532 VdbeCursor
*pC
; /* Cursor to table into which insert is written */
5533 int seekResult
; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5534 const char *zDb
; /* database name - used by the update hook */
5535 Table
*pTab
; /* Table structure - used by update and pre-update hooks */
5536 BtreePayload x
; /* Payload to be inserted */
5538 pData
= &aMem
[pOp
->p2
];
5539 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5540 assert( memIsValid(pData
) );
5541 pC
= p
->apCsr
[pOp
->p1
];
5543 assert( pC
->eCurType
==CURTYPE_BTREE
);
5544 assert( pC
->deferredMoveto
==0 );
5545 assert( pC
->uc
.pCursor
!=0 );
5546 assert( (pOp
->p5
& OPFLAG_ISNOOP
) || pC
->isTable
);
5547 assert( pOp
->p4type
==P4_TABLE
|| pOp
->p4type
>=P4_STATIC
);
5548 REGISTER_TRACE(pOp
->p2
, pData
);
5549 sqlite3VdbeIncrWriteCounter(p
, pC
);
5551 pKey
= &aMem
[pOp
->p3
];
5552 assert( pKey
->flags
& MEM_Int
);
5553 assert( memIsValid(pKey
) );
5554 REGISTER_TRACE(pOp
->p3
, pKey
);
5557 if( pOp
->p4type
==P4_TABLE
&& HAS_UPDATE_HOOK(db
) ){
5558 assert( pC
->iDb
>=0 );
5559 zDb
= db
->aDb
[pC
->iDb
].zDbSName
;
5560 pTab
= pOp
->p4
.pTab
;
5561 assert( (pOp
->p5
& OPFLAG_ISNOOP
) || HasRowid(pTab
) );
5567 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
5568 /* Invoke the pre-update hook, if any */
5570 if( db
->xPreUpdateCallback
&& !(pOp
->p5
& OPFLAG_ISUPDATE
) ){
5571 sqlite3VdbePreUpdateHook(p
,pC
,SQLITE_INSERT
,zDb
,pTab
,x
.nKey
,pOp
->p2
,-1);
5573 if( db
->xUpdateCallback
==0 || pTab
->aCol
==0 ){
5574 /* Prevent post-update hook from running in cases when it should not */
5578 if( pOp
->p5
& OPFLAG_ISNOOP
) break;
5581 assert( (pOp
->p5
& OPFLAG_LASTROWID
)==0 || (pOp
->p5
& OPFLAG_NCHANGE
)!=0 );
5582 if( pOp
->p5
& OPFLAG_NCHANGE
){
5584 if( pOp
->p5
& OPFLAG_LASTROWID
) db
->lastRowid
= x
.nKey
;
5586 assert( (pData
->flags
& (MEM_Blob
|MEM_Str
))!=0 || pData
->n
==0 );
5589 seekResult
= ((pOp
->p5
& OPFLAG_USESEEKRESULT
) ? pC
->seekResult
: 0);
5590 if( pData
->flags
& MEM_Zero
){
5591 x
.nZero
= pData
->u
.nZero
;
5596 assert( BTREE_PREFORMAT
==OPFLAG_PREFORMAT
);
5597 rc
= sqlite3BtreeInsert(pC
->uc
.pCursor
, &x
,
5598 (pOp
->p5
& (OPFLAG_APPEND
|OPFLAG_SAVEPOSITION
|OPFLAG_PREFORMAT
)),
5601 pC
->deferredMoveto
= 0;
5602 pC
->cacheStatus
= CACHE_STALE
;
5604 /* Invoke the update-hook if required. */
5605 if( rc
) goto abort_due_to_error
;
5607 assert( db
->xUpdateCallback
!=0 );
5608 assert( pTab
->aCol
!=0 );
5609 db
->xUpdateCallback(db
->pUpdateArg
,
5610 (pOp
->p5
& OPFLAG_ISUPDATE
) ? SQLITE_UPDATE
: SQLITE_INSERT
,
5611 zDb
, pTab
->zName
, x
.nKey
);
5616 /* Opcode: RowCell P1 P2 P3 * *
5618 ** P1 and P2 are both open cursors. Both must be opened on the same type
5619 ** of table - intkey or index. This opcode is used as part of copying
5620 ** the current row from P2 into P1. If the cursors are opened on intkey
5621 ** tables, register P3 contains the rowid to use with the new record in
5622 ** P1. If they are opened on index tables, P3 is not used.
5624 ** This opcode must be followed by either an Insert or InsertIdx opcode
5625 ** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
5628 VdbeCursor
*pDest
; /* Cursor to write to */
5629 VdbeCursor
*pSrc
; /* Cursor to read from */
5630 i64 iKey
; /* Rowid value to insert with */
5631 assert( pOp
[1].opcode
==OP_Insert
|| pOp
[1].opcode
==OP_IdxInsert
);
5632 assert( pOp
[1].opcode
==OP_Insert
|| pOp
->p3
==0 );
5633 assert( pOp
[1].opcode
==OP_IdxInsert
|| pOp
->p3
>0 );
5634 assert( pOp
[1].p5
& OPFLAG_PREFORMAT
);
5635 pDest
= p
->apCsr
[pOp
->p1
];
5636 pSrc
= p
->apCsr
[pOp
->p2
];
5637 iKey
= pOp
->p3
? aMem
[pOp
->p3
].u
.i
: 0;
5638 rc
= sqlite3BtreeTransferRow(pDest
->uc
.pCursor
, pSrc
->uc
.pCursor
, iKey
);
5639 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
5643 /* Opcode: Delete P1 P2 P3 P4 P5
5645 ** Delete the record at which the P1 cursor is currently pointing.
5647 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5648 ** the cursor will be left pointing at either the next or the previous
5649 ** record in the table. If it is left pointing at the next record, then
5650 ** the next Next instruction will be a no-op. As a result, in this case
5651 ** it is ok to delete a record from within a Next loop. If
5652 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5653 ** left in an undefined state.
5655 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5656 ** delete one of several associated with deleting a table row and all its
5657 ** associated index entries. Exactly one of those deletes is the "primary"
5658 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5659 ** marked with the AUXDELETE flag.
5661 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5662 ** change count is incremented (otherwise not).
5664 ** P1 must not be pseudo-table. It has to be a real table with
5667 ** If P4 is not NULL then it points to a Table object. In this case either
5668 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5669 ** have been positioned using OP_NotFound prior to invoking this opcode in
5670 ** this case. Specifically, if one is configured, the pre-update hook is
5671 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5672 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
5674 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5675 ** of the memory cell that contains the value that the rowid of the row will
5676 ** be set to by the update.
5685 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5686 pC
= p
->apCsr
[pOp
->p1
];
5688 assert( pC
->eCurType
==CURTYPE_BTREE
);
5689 assert( pC
->uc
.pCursor
!=0 );
5690 assert( pC
->deferredMoveto
==0 );
5691 sqlite3VdbeIncrWriteCounter(p
, pC
);
5694 if( pOp
->p4type
==P4_TABLE
5695 && HasRowid(pOp
->p4
.pTab
)
5697 && sqlite3BtreeCursorIsValidNN(pC
->uc
.pCursor
)
5699 /* If p5 is zero, the seek operation that positioned the cursor prior to
5700 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5701 ** the row that is being deleted */
5702 i64 iKey
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5703 assert( CORRUPT_DB
|| pC
->movetoTarget
==iKey
);
5707 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5708 ** the name of the db to pass as to it. Also set local pTab to a copy
5709 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5710 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5711 ** VdbeCursor.movetoTarget to the current rowid. */
5712 if( pOp
->p4type
==P4_TABLE
&& HAS_UPDATE_HOOK(db
) ){
5713 assert( pC
->iDb
>=0 );
5714 assert( pOp
->p4
.pTab
!=0 );
5715 zDb
= db
->aDb
[pC
->iDb
].zDbSName
;
5716 pTab
= pOp
->p4
.pTab
;
5717 if( (pOp
->p5
& OPFLAG_SAVEPOSITION
)!=0 && pC
->isTable
){
5718 pC
->movetoTarget
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5725 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
5726 /* Invoke the pre-update-hook if required. */
5727 assert( db
->xPreUpdateCallback
==0 || pTab
==pOp
->p4
.pTab
);
5728 if( db
->xPreUpdateCallback
&& pTab
){
5729 assert( !(opflags
& OPFLAG_ISUPDATE
)
5730 || HasRowid(pTab
)==0
5731 || (aMem
[pOp
->p3
].flags
& MEM_Int
)
5733 sqlite3VdbePreUpdateHook(p
, pC
,
5734 (opflags
& OPFLAG_ISUPDATE
) ? SQLITE_UPDATE
: SQLITE_DELETE
,
5735 zDb
, pTab
, pC
->movetoTarget
,
5739 if( opflags
& OPFLAG_ISNOOP
) break;
5742 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5743 assert( (pOp
->p5
& ~(OPFLAG_SAVEPOSITION
|OPFLAG_AUXDELETE
))==0 );
5744 assert( OPFLAG_SAVEPOSITION
==BTREE_SAVEPOSITION
);
5745 assert( OPFLAG_AUXDELETE
==BTREE_AUXDELETE
);
5749 if( pC
->isEphemeral
==0
5750 && (pOp
->p5
& OPFLAG_AUXDELETE
)==0
5751 && (pC
->wrFlag
& OPFLAG_FORDELETE
)==0
5755 if( pOp
->p2
& OPFLAG_NCHANGE
){
5761 rc
= sqlite3BtreeDelete(pC
->uc
.pCursor
, pOp
->p5
);
5762 pC
->cacheStatus
= CACHE_STALE
;
5764 if( rc
) goto abort_due_to_error
;
5766 /* Invoke the update-hook if required. */
5767 if( opflags
& OPFLAG_NCHANGE
){
5769 if( db
->xUpdateCallback
&& ALWAYS(pTab
!=0) && HasRowid(pTab
) ){
5770 db
->xUpdateCallback(db
->pUpdateArg
, SQLITE_DELETE
, zDb
, pTab
->zName
,
5772 assert( pC
->iDb
>=0 );
5778 /* Opcode: ResetCount * * * * *
5780 ** The value of the change counter is copied to the database handle
5781 ** change counter (returned by subsequent calls to sqlite3_changes()).
5782 ** Then the VMs internal change counter resets to 0.
5783 ** This is used by trigger programs.
5785 case OP_ResetCount
: {
5786 sqlite3VdbeSetChanges(db
, p
->nChange
);
5791 /* Opcode: SorterCompare P1 P2 P3 P4
5792 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
5794 ** P1 is a sorter cursor. This instruction compares a prefix of the
5795 ** record blob in register P3 against a prefix of the entry that
5796 ** the sorter cursor currently points to. Only the first P4 fields
5797 ** of r[P3] and the sorter record are compared.
5799 ** If either P3 or the sorter contains a NULL in one of their significant
5800 ** fields (not counting the P4 fields at the end which are ignored) then
5801 ** the comparison is assumed to be equal.
5803 ** Fall through to next instruction if the two records compare equal to
5804 ** each other. Jump to P2 if they are different.
5806 case OP_SorterCompare
: {
5811 pC
= p
->apCsr
[pOp
->p1
];
5812 assert( isSorter(pC
) );
5813 assert( pOp
->p4type
==P4_INT32
);
5814 pIn3
= &aMem
[pOp
->p3
];
5815 nKeyCol
= pOp
->p4
.i
;
5817 rc
= sqlite3VdbeSorterCompare(pC
, pIn3
, nKeyCol
, &res
);
5818 VdbeBranchTaken(res
!=0,2);
5819 if( rc
) goto abort_due_to_error
;
5820 if( res
) goto jump_to_p2
;
5824 /* Opcode: SorterData P1 P2 P3 * *
5825 ** Synopsis: r[P2]=data
5827 ** Write into register P2 the current sorter data for sorter cursor P1.
5828 ** Then clear the column header cache on cursor P3.
5830 ** This opcode is normally use to move a record out of the sorter and into
5831 ** a register that is the source for a pseudo-table cursor created using
5832 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
5833 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
5834 ** us from having to issue a separate NullRow instruction to clear that cache.
5836 case OP_SorterData
: {
5839 pOut
= &aMem
[pOp
->p2
];
5840 pC
= p
->apCsr
[pOp
->p1
];
5841 assert( isSorter(pC
) );
5842 rc
= sqlite3VdbeSorterRowkey(pC
, pOut
);
5843 assert( rc
!=SQLITE_OK
|| (pOut
->flags
& MEM_Blob
) );
5844 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5845 if( rc
) goto abort_due_to_error
;
5846 p
->apCsr
[pOp
->p3
]->cacheStatus
= CACHE_STALE
;
5850 /* Opcode: RowData P1 P2 P3 * *
5851 ** Synopsis: r[P2]=data
5853 ** Write into register P2 the complete row content for the row at
5854 ** which cursor P1 is currently pointing.
5855 ** There is no interpretation of the data.
5856 ** It is just copied onto the P2 register exactly as
5857 ** it is found in the database file.
5859 ** If cursor P1 is an index, then the content is the key of the row.
5860 ** If cursor P2 is a table, then the content extracted is the data.
5862 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
5863 ** of a real table, not a pseudo-table.
5865 ** If P3!=0 then this opcode is allowed to make an ephemeral pointer
5866 ** into the database page. That means that the content of the output
5867 ** register will be invalidated as soon as the cursor moves - including
5868 ** moves caused by other cursors that "save" the current cursors
5869 ** position in order that they can write to the same table. If P3==0
5870 ** then a copy of the data is made into memory. P3!=0 is faster, but
5873 ** If P3!=0 then the content of the P2 register is unsuitable for use
5874 ** in OP_Result and any OP_Result will invalidate the P2 register content.
5875 ** The P2 register content is invalidated by opcodes like OP_Function or
5876 ** by any use of another cursor pointing to the same table.
5883 pOut
= out2Prerelease(p
, pOp
);
5885 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5886 pC
= p
->apCsr
[pOp
->p1
];
5888 assert( pC
->eCurType
==CURTYPE_BTREE
);
5889 assert( isSorter(pC
)==0 );
5890 assert( pC
->nullRow
==0 );
5891 assert( pC
->uc
.pCursor
!=0 );
5892 pCrsr
= pC
->uc
.pCursor
;
5894 /* The OP_RowData opcodes always follow OP_NotExists or
5895 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5896 ** that might invalidate the cursor.
5897 ** If this where not the case, on of the following assert()s
5898 ** would fail. Should this ever change (because of changes in the code
5899 ** generator) then the fix would be to insert a call to
5900 ** sqlite3VdbeCursorMoveto().
5902 assert( pC
->deferredMoveto
==0 );
5903 assert( sqlite3BtreeCursorIsValid(pCrsr
) );
5905 n
= sqlite3BtreePayloadSize(pCrsr
);
5906 if( n
>(u32
)db
->aLimit
[SQLITE_LIMIT_LENGTH
] ){
5910 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pCrsr
, n
, pOut
);
5911 if( rc
) goto abort_due_to_error
;
5912 if( !pOp
->p3
) Deephemeralize(pOut
);
5913 UPDATE_MAX_BLOBSIZE(pOut
);
5914 REGISTER_TRACE(pOp
->p2
, pOut
);
5918 /* Opcode: Rowid P1 P2 * * *
5919 ** Synopsis: r[P2]=PX rowid of P1
5921 ** Store in register P2 an integer which is the key of the table entry that
5922 ** P1 is currently point to.
5924 ** P1 can be either an ordinary table or a virtual table. There used to
5925 ** be a separate OP_VRowid opcode for use with virtual tables, but this
5926 ** one opcode now works for both table types.
5928 case OP_Rowid
: { /* out2, ncycle */
5931 sqlite3_vtab
*pVtab
;
5932 const sqlite3_module
*pModule
;
5934 pOut
= out2Prerelease(p
, pOp
);
5935 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5936 pC
= p
->apCsr
[pOp
->p1
];
5938 assert( pC
->eCurType
!=CURTYPE_PSEUDO
|| pC
->nullRow
);
5940 pOut
->flags
= MEM_Null
;
5942 }else if( pC
->deferredMoveto
){
5943 v
= pC
->movetoTarget
;
5944 #ifndef SQLITE_OMIT_VIRTUALTABLE
5945 }else if( pC
->eCurType
==CURTYPE_VTAB
){
5946 assert( pC
->uc
.pVCur
!=0 );
5947 pVtab
= pC
->uc
.pVCur
->pVtab
;
5948 pModule
= pVtab
->pModule
;
5949 assert( pModule
->xRowid
);
5950 rc
= pModule
->xRowid(pC
->uc
.pVCur
, &v
);
5951 sqlite3VtabImportErrmsg(p
, pVtab
);
5952 if( rc
) goto abort_due_to_error
;
5953 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5955 assert( pC
->eCurType
==CURTYPE_BTREE
);
5956 assert( pC
->uc
.pCursor
!=0 );
5957 rc
= sqlite3VdbeCursorRestore(pC
);
5958 if( rc
) goto abort_due_to_error
;
5960 pOut
->flags
= MEM_Null
;
5963 v
= sqlite3BtreeIntegerKey(pC
->uc
.pCursor
);
5969 /* Opcode: NullRow P1 * * * *
5971 ** Move the cursor P1 to a null row. Any OP_Column operations
5972 ** that occur while the cursor is on the null row will always
5975 ** If cursor P1 is not previously opened, open it now to a special
5976 ** pseudo-cursor that always returns NULL for every column.
5981 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
5982 pC
= p
->apCsr
[pOp
->p1
];
5984 /* If the cursor is not already open, create a special kind of
5985 ** pseudo-cursor that always gives null rows. */
5986 pC
= allocateCursor(p
, pOp
->p1
, 1, CURTYPE_PSEUDO
);
5987 if( pC
==0 ) goto no_mem
;
5991 pC
->uc
.pCursor
= sqlite3BtreeFakeValidCursor();
5994 pC
->cacheStatus
= CACHE_STALE
;
5995 if( pC
->eCurType
==CURTYPE_BTREE
){
5996 assert( pC
->uc
.pCursor
!=0 );
5997 sqlite3BtreeClearCursor(pC
->uc
.pCursor
);
6000 if( pC
->seekOp
==0 ) pC
->seekOp
= OP_NullRow
;
6005 /* Opcode: SeekEnd P1 * * * *
6007 ** Position cursor P1 at the end of the btree for the purpose of
6008 ** appending a new entry onto the btree.
6010 ** It is assumed that the cursor is used only for appending and so
6011 ** if the cursor is valid, then the cursor must already be pointing
6012 ** at the end of the btree and so no changes are made to
6015 /* Opcode: Last P1 P2 * * *
6017 ** The next use of the Rowid or Column or Prev instruction for P1
6018 ** will refer to the last entry in the database table or index.
6019 ** If the table or index is empty and P2>0, then jump immediately to P2.
6020 ** If P2 is 0 or if the table or index is not empty, fall through
6021 ** to the following instruction.
6023 ** This opcode leaves the cursor configured to move in reverse order,
6024 ** from the end toward the beginning. In other words, the cursor is
6025 ** configured to use Prev, not Next.
6027 case OP_SeekEnd
: /* ncycle */
6028 case OP_Last
: { /* jump, ncycle */
6033 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6034 pC
= p
->apCsr
[pOp
->p1
];
6036 assert( pC
->eCurType
==CURTYPE_BTREE
);
6037 pCrsr
= pC
->uc
.pCursor
;
6041 pC
->seekOp
= pOp
->opcode
;
6043 if( pOp
->opcode
==OP_SeekEnd
){
6044 assert( pOp
->p2
==0 );
6045 pC
->seekResult
= -1;
6046 if( sqlite3BtreeCursorIsValidNN(pCrsr
) ){
6050 rc
= sqlite3BtreeLast(pCrsr
, &res
);
6051 pC
->nullRow
= (u8
)res
;
6052 pC
->deferredMoveto
= 0;
6053 pC
->cacheStatus
= CACHE_STALE
;
6054 if( rc
) goto abort_due_to_error
;
6056 VdbeBranchTaken(res
!=0,2);
6057 if( res
) goto jump_to_p2
;
6062 /* Opcode: IfSmaller P1 P2 P3 * *
6064 ** Estimate the number of rows in the table P1. Jump to P2 if that
6065 ** estimate is less than approximately 2**(0.1*P3).
6067 case OP_IfSmaller
: { /* jump */
6073 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6074 pC
= p
->apCsr
[pOp
->p1
];
6076 pCrsr
= pC
->uc
.pCursor
;
6078 rc
= sqlite3BtreeFirst(pCrsr
, &res
);
6079 if( rc
) goto abort_due_to_error
;
6081 sz
= sqlite3BtreeRowCountEst(pCrsr
);
6082 if( ALWAYS(sz
>=0) && sqlite3LogEst((u64
)sz
)<pOp
->p3
) res
= 1;
6084 VdbeBranchTaken(res
!=0,2);
6085 if( res
) goto jump_to_p2
;
6090 /* Opcode: SorterSort P1 P2 * * *
6092 ** After all records have been inserted into the Sorter object
6093 ** identified by P1, invoke this opcode to actually do the sorting.
6094 ** Jump to P2 if there are no records to be sorted.
6096 ** This opcode is an alias for OP_Sort and OP_Rewind that is used
6097 ** for Sorter objects.
6099 /* Opcode: Sort P1 P2 * * *
6101 ** This opcode does exactly the same thing as OP_Rewind except that
6102 ** it increments an undocumented global variable used for testing.
6104 ** Sorting is accomplished by writing records into a sorting index,
6105 ** then rewinding that index and playing it back from beginning to
6106 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
6107 ** rewinding so that the global variable will be incremented and
6108 ** regression tests can determine whether or not the optimizer is
6109 ** correctly optimizing out sorts.
6111 case OP_SorterSort
: /* jump */
6112 case OP_Sort
: { /* jump */
6114 sqlite3_sort_count
++;
6115 sqlite3_search_count
--;
6117 p
->aCounter
[SQLITE_STMTSTATUS_SORT
]++;
6118 /* Fall through into OP_Rewind */
6119 /* no break */ deliberate_fall_through
6121 /* Opcode: Rewind P1 P2 * * *
6123 ** The next use of the Rowid or Column or Next instruction for P1
6124 ** will refer to the first entry in the database table or index.
6125 ** If the table or index is empty, jump immediately to P2.
6126 ** If the table or index is not empty, fall through to the following
6129 ** If P2 is zero, that is an assertion that the P1 table is never
6130 ** empty and hence the jump will never be taken.
6132 ** This opcode leaves the cursor configured to move in forward order,
6133 ** from the beginning toward the end. In other words, the cursor is
6134 ** configured to use Next, not Prev.
6136 case OP_Rewind
: { /* jump, ncycle */
6141 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6142 assert( pOp
->p5
==0 );
6143 assert( pOp
->p2
>=0 && pOp
->p2
<p
->nOp
);
6145 pC
= p
->apCsr
[pOp
->p1
];
6147 assert( isSorter(pC
)==(pOp
->opcode
==OP_SorterSort
) );
6150 pC
->seekOp
= OP_Rewind
;
6153 rc
= sqlite3VdbeSorterRewind(pC
, &res
);
6155 assert( pC
->eCurType
==CURTYPE_BTREE
);
6156 pCrsr
= pC
->uc
.pCursor
;
6158 rc
= sqlite3BtreeFirst(pCrsr
, &res
);
6159 pC
->deferredMoveto
= 0;
6160 pC
->cacheStatus
= CACHE_STALE
;
6162 if( rc
) goto abort_due_to_error
;
6163 pC
->nullRow
= (u8
)res
;
6165 VdbeBranchTaken(res
!=0,2);
6166 if( res
) goto jump_to_p2
;
6171 /* Opcode: Next P1 P2 P3 * P5
6173 ** Advance cursor P1 so that it points to the next key/data pair in its
6174 ** table or index. If there are no more key/value pairs then fall through
6175 ** to the following instruction. But if the cursor advance was successful,
6176 ** jump immediately to P2.
6178 ** The Next opcode is only valid following an SeekGT, SeekGE, or
6179 ** OP_Rewind opcode used to position the cursor. Next is not allowed
6180 ** to follow SeekLT, SeekLE, or OP_Last.
6182 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
6183 ** been opened prior to this opcode or the program will segfault.
6185 ** The P3 value is a hint to the btree implementation. If P3==1, that
6186 ** means P1 is an SQL index and that this instruction could have been
6187 ** omitted if that index had been unique. P3 is usually 0. P3 is
6188 ** always either 0 or 1.
6190 ** If P5 is positive and the jump is taken, then event counter
6191 ** number P5-1 in the prepared statement is incremented.
6195 /* Opcode: Prev P1 P2 P3 * P5
6197 ** Back up cursor P1 so that it points to the previous key/data pair in its
6198 ** table or index. If there is no previous key/value pairs then fall through
6199 ** to the following instruction. But if the cursor backup was successful,
6200 ** jump immediately to P2.
6203 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
6204 ** OP_Last opcode used to position the cursor. Prev is not allowed
6205 ** to follow SeekGT, SeekGE, or OP_Rewind.
6207 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
6208 ** not open then the behavior is undefined.
6210 ** The P3 value is a hint to the btree implementation. If P3==1, that
6211 ** means P1 is an SQL index and that this instruction could have been
6212 ** omitted if that index had been unique. P3 is usually 0. P3 is
6213 ** always either 0 or 1.
6215 ** If P5 is positive and the jump is taken, then event counter
6216 ** number P5-1 in the prepared statement is incremented.
6218 /* Opcode: SorterNext P1 P2 * * P5
6220 ** This opcode works just like OP_Next except that P1 must be a
6221 ** sorter object for which the OP_SorterSort opcode has been
6222 ** invoked. This opcode advances the cursor to the next sorted
6223 ** record, or jumps to P2 if there are no more sorted records.
6225 case OP_SorterNext
: { /* jump */
6228 pC
= p
->apCsr
[pOp
->p1
];
6229 assert( isSorter(pC
) );
6230 rc
= sqlite3VdbeSorterNext(db
, pC
);
6233 case OP_Prev
: /* jump, ncycle */
6234 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6236 || pOp
->p5
==SQLITE_STMTSTATUS_FULLSCAN_STEP
6237 || pOp
->p5
==SQLITE_STMTSTATUS_AUTOINDEX
);
6238 pC
= p
->apCsr
[pOp
->p1
];
6240 assert( pC
->deferredMoveto
==0 );
6241 assert( pC
->eCurType
==CURTYPE_BTREE
);
6242 assert( pC
->seekOp
==OP_SeekLT
|| pC
->seekOp
==OP_SeekLE
6243 || pC
->seekOp
==OP_Last
|| pC
->seekOp
==OP_IfNoHope
6244 || pC
->seekOp
==OP_NullRow
);
6245 rc
= sqlite3BtreePrevious(pC
->uc
.pCursor
, pOp
->p3
);
6248 case OP_Next
: /* jump, ncycle */
6249 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6251 || pOp
->p5
==SQLITE_STMTSTATUS_FULLSCAN_STEP
6252 || pOp
->p5
==SQLITE_STMTSTATUS_AUTOINDEX
);
6253 pC
= p
->apCsr
[pOp
->p1
];
6255 assert( pC
->deferredMoveto
==0 );
6256 assert( pC
->eCurType
==CURTYPE_BTREE
);
6257 assert( pC
->seekOp
==OP_SeekGT
|| pC
->seekOp
==OP_SeekGE
6258 || pC
->seekOp
==OP_Rewind
|| pC
->seekOp
==OP_Found
6259 || pC
->seekOp
==OP_NullRow
|| pC
->seekOp
==OP_SeekRowid
6260 || pC
->seekOp
==OP_IfNoHope
);
6261 rc
= sqlite3BtreeNext(pC
->uc
.pCursor
, pOp
->p3
);
6264 pC
->cacheStatus
= CACHE_STALE
;
6265 VdbeBranchTaken(rc
==SQLITE_OK
,2);
6266 if( rc
==SQLITE_OK
){
6268 p
->aCounter
[pOp
->p5
]++;
6270 sqlite3_search_count
++;
6272 goto jump_to_p2_and_check_for_interrupt
;
6274 if( rc
!=SQLITE_DONE
) goto abort_due_to_error
;
6277 goto check_for_interrupt
;
6280 /* Opcode: IdxInsert P1 P2 P3 P4 P5
6281 ** Synopsis: key=r[P2]
6283 ** Register P2 holds an SQL index key made using the
6284 ** MakeRecord instructions. This opcode writes that key
6285 ** into the index P1. Data for the entry is nil.
6287 ** If P4 is not zero, then it is the number of values in the unpacked
6288 ** key of reg(P2). In that case, P3 is the index of the first register
6289 ** for the unpacked key. The availability of the unpacked key can sometimes
6290 ** be an optimization.
6292 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
6293 ** that this insert is likely to be an append.
6295 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
6296 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
6297 ** then the change counter is unchanged.
6299 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
6300 ** run faster by avoiding an unnecessary seek on cursor P1. However,
6301 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
6302 ** seeks on the cursor or if the most recent seek used a key equivalent
6305 ** This instruction only works for indices. The equivalent instruction
6306 ** for tables is OP_Insert.
6308 case OP_IdxInsert
: { /* in2 */
6312 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6313 pC
= p
->apCsr
[pOp
->p1
];
6314 sqlite3VdbeIncrWriteCounter(p
, pC
);
6316 assert( !isSorter(pC
) );
6317 pIn2
= &aMem
[pOp
->p2
];
6318 assert( (pIn2
->flags
& MEM_Blob
) || (pOp
->p5
& OPFLAG_PREFORMAT
) );
6319 if( pOp
->p5
& OPFLAG_NCHANGE
) p
->nChange
++;
6320 assert( pC
->eCurType
==CURTYPE_BTREE
);
6321 assert( pC
->isTable
==0 );
6322 rc
= ExpandBlob(pIn2
);
6323 if( rc
) goto abort_due_to_error
;
6326 x
.aMem
= aMem
+ pOp
->p3
;
6327 x
.nMem
= (u16
)pOp
->p4
.i
;
6328 rc
= sqlite3BtreeInsert(pC
->uc
.pCursor
, &x
,
6329 (pOp
->p5
& (OPFLAG_APPEND
|OPFLAG_SAVEPOSITION
|OPFLAG_PREFORMAT
)),
6330 ((pOp
->p5
& OPFLAG_USESEEKRESULT
) ? pC
->seekResult
: 0)
6332 assert( pC
->deferredMoveto
==0 );
6333 pC
->cacheStatus
= CACHE_STALE
;
6334 if( rc
) goto abort_due_to_error
;
6338 /* Opcode: SorterInsert P1 P2 * * *
6339 ** Synopsis: key=r[P2]
6341 ** Register P2 holds an SQL index key made using the
6342 ** MakeRecord instructions. This opcode writes that key
6343 ** into the sorter P1. Data for the entry is nil.
6345 case OP_SorterInsert
: { /* in2 */
6348 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6349 pC
= p
->apCsr
[pOp
->p1
];
6350 sqlite3VdbeIncrWriteCounter(p
, pC
);
6352 assert( isSorter(pC
) );
6353 pIn2
= &aMem
[pOp
->p2
];
6354 assert( pIn2
->flags
& MEM_Blob
);
6355 assert( pC
->isTable
==0 );
6356 rc
= ExpandBlob(pIn2
);
6357 if( rc
) goto abort_due_to_error
;
6358 rc
= sqlite3VdbeSorterWrite(pC
, pIn2
);
6359 if( rc
) goto abort_due_to_error
;
6363 /* Opcode: IdxDelete P1 P2 P3 * P5
6364 ** Synopsis: key=r[P2@P3]
6366 ** The content of P3 registers starting at register P2 form
6367 ** an unpacked index key. This opcode removes that entry from the
6368 ** index opened by cursor P1.
6370 ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
6371 ** if no matching index entry is found. This happens when running
6372 ** an UPDATE or DELETE statement and the index entry to be updated
6373 ** or deleted is not found. For some uses of IdxDelete
6374 ** (example: the EXCEPT operator) it does not matter that no matching
6375 ** entry is found. For those cases, P5 is zero. Also, do not raise
6376 ** this (self-correcting and non-critical) error if in writable_schema mode.
6378 case OP_IdxDelete
: {
6384 assert( pOp
->p3
>0 );
6385 assert( pOp
->p2
>0 && pOp
->p2
+pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
)+1 );
6386 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6387 pC
= p
->apCsr
[pOp
->p1
];
6389 assert( pC
->eCurType
==CURTYPE_BTREE
);
6390 sqlite3VdbeIncrWriteCounter(p
, pC
);
6391 pCrsr
= pC
->uc
.pCursor
;
6393 r
.pKeyInfo
= pC
->pKeyInfo
;
6394 r
.nField
= (u16
)pOp
->p3
;
6396 r
.aMem
= &aMem
[pOp
->p2
];
6397 rc
= sqlite3BtreeIndexMoveto(pCrsr
, &r
, &res
);
6398 if( rc
) goto abort_due_to_error
;
6400 rc
= sqlite3BtreeDelete(pCrsr
, BTREE_AUXDELETE
);
6401 if( rc
) goto abort_due_to_error
;
6402 }else if( pOp
->p5
&& !sqlite3WritableSchema(db
) ){
6403 rc
= sqlite3ReportError(SQLITE_CORRUPT_INDEX
, __LINE__
, "index corruption");
6404 goto abort_due_to_error
;
6406 assert( pC
->deferredMoveto
==0 );
6407 pC
->cacheStatus
= CACHE_STALE
;
6412 /* Opcode: DeferredSeek P1 * P3 P4 *
6413 ** Synopsis: Move P3 to P1.rowid if needed
6415 ** P1 is an open index cursor and P3 is a cursor on the corresponding
6416 ** table. This opcode does a deferred seek of the P3 table cursor
6417 ** to the row that corresponds to the current row of P1.
6419 ** This is a deferred seek. Nothing actually happens until
6420 ** the cursor is used to read a record. That way, if no reads
6421 ** occur, no unnecessary I/O happens.
6423 ** P4 may be an array of integers (type P4_INTARRAY) containing
6424 ** one entry for each column in the P3 table. If array entry a(i)
6425 ** is non-zero, then reading column a(i)-1 from cursor P3 is
6426 ** equivalent to performing the deferred seek and then reading column i
6427 ** from P1. This information is stored in P3 and used to redirect
6428 ** reads against P3 over to P1, thus possibly avoiding the need to
6429 ** seek and read cursor P3.
6431 /* Opcode: IdxRowid P1 P2 * * *
6432 ** Synopsis: r[P2]=rowid
6434 ** Write into register P2 an integer which is the last entry in the record at
6435 ** the end of the index key pointed to by cursor P1. This integer should be
6436 ** the rowid of the table entry to which this index entry points.
6438 ** See also: Rowid, MakeRecord.
6440 case OP_DeferredSeek
: /* ncycle */
6441 case OP_IdxRowid
: { /* out2, ncycle */
6442 VdbeCursor
*pC
; /* The P1 index cursor */
6443 VdbeCursor
*pTabCur
; /* The P2 table cursor (OP_DeferredSeek only) */
6444 i64 rowid
; /* Rowid that P1 current points to */
6446 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6447 pC
= p
->apCsr
[pOp
->p1
];
6449 assert( pC
->eCurType
==CURTYPE_BTREE
|| IsNullCursor(pC
) );
6450 assert( pC
->uc
.pCursor
!=0 );
6451 assert( pC
->isTable
==0 || IsNullCursor(pC
) );
6452 assert( pC
->deferredMoveto
==0 );
6453 assert( !pC
->nullRow
|| pOp
->opcode
==OP_IdxRowid
);
6455 /* The IdxRowid and Seek opcodes are combined because of the commonality
6456 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
6457 rc
= sqlite3VdbeCursorRestore(pC
);
6459 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
6460 ** since it was last positioned and an error (e.g. OOM or an IO error)
6461 ** occurs while trying to reposition it. */
6462 if( rc
!=SQLITE_OK
) goto abort_due_to_error
;
6465 rowid
= 0; /* Not needed. Only used to silence a warning. */
6466 rc
= sqlite3VdbeIdxRowid(db
, pC
->uc
.pCursor
, &rowid
);
6467 if( rc
!=SQLITE_OK
){
6468 goto abort_due_to_error
;
6470 if( pOp
->opcode
==OP_DeferredSeek
){
6471 assert( pOp
->p3
>=0 && pOp
->p3
<p
->nCursor
);
6472 pTabCur
= p
->apCsr
[pOp
->p3
];
6473 assert( pTabCur
!=0 );
6474 assert( pTabCur
->eCurType
==CURTYPE_BTREE
);
6475 assert( pTabCur
->uc
.pCursor
!=0 );
6476 assert( pTabCur
->isTable
);
6477 pTabCur
->nullRow
= 0;
6478 pTabCur
->movetoTarget
= rowid
;
6479 pTabCur
->deferredMoveto
= 1;
6480 pTabCur
->cacheStatus
= CACHE_STALE
;
6481 assert( pOp
->p4type
==P4_INTARRAY
|| pOp
->p4
.ai
==0 );
6482 assert( !pTabCur
->isEphemeral
);
6483 pTabCur
->ub
.aAltMap
= pOp
->p4
.ai
;
6484 assert( !pC
->isEphemeral
);
6485 pTabCur
->pAltCursor
= pC
;
6487 pOut
= out2Prerelease(p
, pOp
);
6491 assert( pOp
->opcode
==OP_IdxRowid
);
6492 sqlite3VdbeMemSetNull(&aMem
[pOp
->p2
]);
6497 /* Opcode: FinishSeek P1 * * * *
6499 ** If cursor P1 was previously moved via OP_DeferredSeek, complete that
6500 ** seek operation now, without further delay. If the cursor seek has
6501 ** already occurred, this instruction is a no-op.
6503 case OP_FinishSeek
: { /* ncycle */
6504 VdbeCursor
*pC
; /* The P1 index cursor */
6506 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6507 pC
= p
->apCsr
[pOp
->p1
];
6508 if( pC
->deferredMoveto
){
6509 rc
= sqlite3VdbeFinishMoveto(pC
);
6510 if( rc
) goto abort_due_to_error
;
6515 /* Opcode: IdxGE P1 P2 P3 P4 *
6516 ** Synopsis: key=r[P3@P4]
6518 ** The P4 register values beginning with P3 form an unpacked index
6519 ** key that omits the PRIMARY KEY. Compare this key value against the index
6520 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6521 ** fields at the end.
6523 ** If the P1 index entry is greater than or equal to the key value
6524 ** then jump to P2. Otherwise fall through to the next instruction.
6526 /* Opcode: IdxGT P1 P2 P3 P4 *
6527 ** Synopsis: key=r[P3@P4]
6529 ** The P4 register values beginning with P3 form an unpacked index
6530 ** key that omits the PRIMARY KEY. Compare this key value against the index
6531 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6532 ** fields at the end.
6534 ** If the P1 index entry is greater than the key value
6535 ** then jump to P2. Otherwise fall through to the next instruction.
6537 /* Opcode: IdxLT P1 P2 P3 P4 *
6538 ** Synopsis: key=r[P3@P4]
6540 ** The P4 register values beginning with P3 form an unpacked index
6541 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6542 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6543 ** ROWID on the P1 index.
6545 ** If the P1 index entry is less than the key value then jump to P2.
6546 ** Otherwise fall through to the next instruction.
6548 /* Opcode: IdxLE P1 P2 P3 P4 *
6549 ** Synopsis: key=r[P3@P4]
6551 ** The P4 register values beginning with P3 form an unpacked index
6552 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6553 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6554 ** ROWID on the P1 index.
6556 ** If the P1 index entry is less than or equal to the key value then jump
6557 ** to P2. Otherwise fall through to the next instruction.
6559 case OP_IdxLE
: /* jump, ncycle */
6560 case OP_IdxGT
: /* jump, ncycle */
6561 case OP_IdxLT
: /* jump, ncycle */
6562 case OP_IdxGE
: { /* jump, ncycle */
6567 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6568 pC
= p
->apCsr
[pOp
->p1
];
6570 assert( pC
->isOrdered
);
6571 assert( pC
->eCurType
==CURTYPE_BTREE
);
6572 assert( pC
->uc
.pCursor
!=0);
6573 assert( pC
->deferredMoveto
==0 );
6574 assert( pOp
->p4type
==P4_INT32
);
6575 r
.pKeyInfo
= pC
->pKeyInfo
;
6576 r
.nField
= (u16
)pOp
->p4
.i
;
6577 if( pOp
->opcode
<OP_IdxLT
){
6578 assert( pOp
->opcode
==OP_IdxLE
|| pOp
->opcode
==OP_IdxGT
);
6581 assert( pOp
->opcode
==OP_IdxGE
|| pOp
->opcode
==OP_IdxLT
);
6584 r
.aMem
= &aMem
[pOp
->p3
];
6588 for(i
=0; i
<r
.nField
; i
++){
6589 assert( memIsValid(&r
.aMem
[i
]) );
6590 REGISTER_TRACE(pOp
->p3
+i
, &aMem
[pOp
->p3
+i
]);
6595 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6601 assert( pC
->eCurType
==CURTYPE_BTREE
);
6602 pCur
= pC
->uc
.pCursor
;
6603 assert( sqlite3BtreeCursorIsValid(pCur
) );
6604 nCellKey
= sqlite3BtreePayloadSize(pCur
);
6605 /* nCellKey will always be between 0 and 0xffffffff because of the way
6606 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6607 if( nCellKey
<=0 || nCellKey
>0x7fffffff ){
6608 rc
= SQLITE_CORRUPT_BKPT
;
6609 goto abort_due_to_error
;
6611 sqlite3VdbeMemInit(&m
, db
, 0);
6612 rc
= sqlite3VdbeMemFromBtreeZeroOffset(pCur
, (u32
)nCellKey
, &m
);
6613 if( rc
) goto abort_due_to_error
;
6614 res
= sqlite3VdbeRecordCompareWithSkip(m
.n
, m
.z
, &r
, 0);
6615 sqlite3VdbeMemReleaseMalloc(&m
);
6617 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6619 assert( (OP_IdxLE
&1)==(OP_IdxLT
&1) && (OP_IdxGE
&1)==(OP_IdxGT
&1) );
6620 if( (pOp
->opcode
&1)==(OP_IdxLT
&1) ){
6621 assert( pOp
->opcode
==OP_IdxLE
|| pOp
->opcode
==OP_IdxLT
);
6624 assert( pOp
->opcode
==OP_IdxGE
|| pOp
->opcode
==OP_IdxGT
);
6627 VdbeBranchTaken(res
>0,2);
6628 assert( rc
==SQLITE_OK
);
6629 if( res
>0 ) goto jump_to_p2
;
6633 /* Opcode: Destroy P1 P2 P3 * *
6635 ** Delete an entire database table or index whose root page in the database
6636 ** file is given by P1.
6638 ** The table being destroyed is in the main database file if P3==0. If
6639 ** P3==1 then the table to be clear is in the auxiliary database file
6640 ** that is used to store tables create using CREATE TEMPORARY TABLE.
6642 ** If AUTOVACUUM is enabled then it is possible that another root page
6643 ** might be moved into the newly deleted root page in order to keep all
6644 ** root pages contiguous at the beginning of the database. The former
6645 ** value of the root page that moved - its value before the move occurred -
6646 ** is stored in register P2. If no page movement was required (because the
6647 ** table being dropped was already the last one in the database) then a
6648 ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6649 ** is stored in register P2.
6651 ** This opcode throws an error if there are any active reader VMs when
6652 ** it is invoked. This is done to avoid the difficulty associated with
6653 ** updating existing cursors when a root page is moved in an AUTOVACUUM
6654 ** database. This error is thrown even if the database is not an AUTOVACUUM
6655 ** db in order to avoid introducing an incompatibility between autovacuum
6656 ** and non-autovacuum modes.
6660 case OP_Destroy
: { /* out2 */
6664 sqlite3VdbeIncrWriteCounter(p
, 0);
6665 assert( p
->readOnly
==0 );
6666 assert( pOp
->p1
>1 );
6667 pOut
= out2Prerelease(p
, pOp
);
6668 pOut
->flags
= MEM_Null
;
6669 if( db
->nVdbeRead
> db
->nVDestroy
+1 ){
6671 p
->errorAction
= OE_Abort
;
6672 goto abort_due_to_error
;
6675 assert( DbMaskTest(p
->btreeMask
, iDb
) );
6676 iMoved
= 0; /* Not needed. Only to silence a warning. */
6677 rc
= sqlite3BtreeDropTable(db
->aDb
[iDb
].pBt
, pOp
->p1
, &iMoved
);
6678 pOut
->flags
= MEM_Int
;
6680 if( rc
) goto abort_due_to_error
;
6681 #ifndef SQLITE_OMIT_AUTOVACUUM
6683 sqlite3RootPageMoved(db
, iDb
, iMoved
, pOp
->p1
);
6684 /* All OP_Destroy operations occur on the same btree */
6685 assert( resetSchemaOnFault
==0 || resetSchemaOnFault
==iDb
+1 );
6686 resetSchemaOnFault
= iDb
+1;
6693 /* Opcode: Clear P1 P2 P3
6695 ** Delete all contents of the database table or index whose root page
6696 ** in the database file is given by P1. But, unlike Destroy, do not
6697 ** remove the table or index from the database file.
6699 ** The table being clear is in the main database file if P2==0. If
6700 ** P2==1 then the table to be clear is in the auxiliary database file
6701 ** that is used to store tables create using CREATE TEMPORARY TABLE.
6703 ** If the P3 value is non-zero, then the row change count is incremented
6704 ** by the number of rows in the table being cleared. If P3 is greater
6705 ** than zero, then the value stored in register P3 is also incremented
6706 ** by the number of rows in the table being cleared.
6708 ** See also: Destroy
6713 sqlite3VdbeIncrWriteCounter(p
, 0);
6715 assert( p
->readOnly
==0 );
6716 assert( DbMaskTest(p
->btreeMask
, pOp
->p2
) );
6717 rc
= sqlite3BtreeClearTable(db
->aDb
[pOp
->p2
].pBt
, (u32
)pOp
->p1
, &nChange
);
6719 p
->nChange
+= nChange
;
6721 assert( memIsValid(&aMem
[pOp
->p3
]) );
6722 memAboutToChange(p
, &aMem
[pOp
->p3
]);
6723 aMem
[pOp
->p3
].u
.i
+= nChange
;
6726 if( rc
) goto abort_due_to_error
;
6730 /* Opcode: ResetSorter P1 * * * *
6732 ** Delete all contents from the ephemeral table or sorter
6733 ** that is open on cursor P1.
6735 ** This opcode only works for cursors used for sorting and
6736 ** opened with OP_OpenEphemeral or OP_SorterOpen.
6738 case OP_ResetSorter
: {
6741 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
6742 pC
= p
->apCsr
[pOp
->p1
];
6745 sqlite3VdbeSorterReset(db
, pC
->uc
.pSorter
);
6747 assert( pC
->eCurType
==CURTYPE_BTREE
);
6748 assert( pC
->isEphemeral
);
6749 rc
= sqlite3BtreeClearTableOfCursor(pC
->uc
.pCursor
);
6750 if( rc
) goto abort_due_to_error
;
6755 /* Opcode: CreateBtree P1 P2 P3 * *
6756 ** Synopsis: r[P2]=root iDb=P1 flags=P3
6758 ** Allocate a new b-tree in the main database file if P1==0 or in the
6759 ** TEMP database file if P1==1 or in an attached database if
6760 ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
6761 ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
6762 ** The root page number of the new b-tree is stored in register P2.
6764 case OP_CreateBtree
: { /* out2 */
6768 sqlite3VdbeIncrWriteCounter(p
, 0);
6769 pOut
= out2Prerelease(p
, pOp
);
6771 assert( pOp
->p3
==BTREE_INTKEY
|| pOp
->p3
==BTREE_BLOBKEY
);
6772 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
6773 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
6774 assert( p
->readOnly
==0 );
6775 pDb
= &db
->aDb
[pOp
->p1
];
6776 assert( pDb
->pBt
!=0 );
6777 rc
= sqlite3BtreeCreateTable(pDb
->pBt
, &pgno
, pOp
->p3
);
6778 if( rc
) goto abort_due_to_error
;
6783 /* Opcode: SqlExec * * * P4 *
6785 ** Run the SQL statement or statements specified in the P4 string.
6788 sqlite3VdbeIncrWriteCounter(p
, 0);
6790 rc
= sqlite3_exec(db
, pOp
->p4
.z
, 0, 0, 0);
6792 if( rc
) goto abort_due_to_error
;
6796 /* Opcode: ParseSchema P1 * * P4 *
6798 ** Read and parse all entries from the schema table of database P1
6799 ** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6800 ** entire schema for P1 is reparsed.
6802 ** This opcode invokes the parser to create a new virtual machine,
6803 ** then runs the new virtual machine. It is thus a re-entrant opcode.
6805 case OP_ParseSchema
: {
6807 const char *zSchema
;
6811 /* Any prepared statement that invokes this opcode will hold mutexes
6812 ** on every btree. This is a prerequisite for invoking
6813 ** sqlite3InitCallback().
6816 for(iDb
=0; iDb
<db
->nDb
; iDb
++){
6817 assert( iDb
==1 || sqlite3BtreeHoldsMutex(db
->aDb
[iDb
].pBt
) );
6822 assert( iDb
>=0 && iDb
<db
->nDb
);
6823 assert( DbHasProperty(db
, iDb
, DB_SchemaLoaded
)
6825 || (CORRUPT_DB
&& (db
->flags
& SQLITE_NoSchemaError
)!=0) );
6827 #ifndef SQLITE_OMIT_ALTERTABLE
6829 sqlite3SchemaClear(db
->aDb
[iDb
].pSchema
);
6830 db
->mDbFlags
&= ~DBFLAG_SchemaKnownOk
;
6831 rc
= sqlite3InitOne(db
, iDb
, &p
->zErrMsg
, pOp
->p5
);
6832 db
->mDbFlags
|= DBFLAG_SchemaChange
;
6837 zSchema
= LEGACY_SCHEMA_TABLE
;
6840 initData
.pzErrMsg
= &p
->zErrMsg
;
6841 initData
.mInitFlags
= 0;
6842 initData
.mxPage
= sqlite3BtreeLastPage(db
->aDb
[iDb
].pBt
);
6843 zSql
= sqlite3MPrintf(db
,
6844 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
6845 db
->aDb
[iDb
].zDbSName
, zSchema
, pOp
->p4
.z
);
6847 rc
= SQLITE_NOMEM_BKPT
;
6849 assert( db
->init
.busy
==0 );
6851 initData
.rc
= SQLITE_OK
;
6852 initData
.nInitRow
= 0;
6853 assert( !db
->mallocFailed
);
6854 rc
= sqlite3_exec(db
, zSql
, sqlite3InitCallback
, &initData
, 0);
6855 if( rc
==SQLITE_OK
) rc
= initData
.rc
;
6856 if( rc
==SQLITE_OK
&& initData
.nInitRow
==0 ){
6857 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6858 ** at least one SQL statement. Any less than that indicates that
6859 ** the sqlite_schema table is corrupt. */
6860 rc
= SQLITE_CORRUPT_BKPT
;
6862 sqlite3DbFreeNN(db
, zSql
);
6867 sqlite3ResetAllSchemasOfConnection(db
);
6868 if( rc
==SQLITE_NOMEM
){
6871 goto abort_due_to_error
;
6876 #if !defined(SQLITE_OMIT_ANALYZE)
6877 /* Opcode: LoadAnalysis P1 * * * *
6879 ** Read the sqlite_stat1 table for database P1 and load the content
6880 ** of that table into the internal index hash table. This will cause
6881 ** the analysis to be used when preparing all subsequent queries.
6883 case OP_LoadAnalysis
: {
6884 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
6885 rc
= sqlite3AnalysisLoad(db
, pOp
->p1
);
6886 if( rc
) goto abort_due_to_error
;
6889 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
6891 /* Opcode: DropTable P1 * * P4 *
6893 ** Remove the internal (in-memory) data structures that describe
6894 ** the table named P4 in database P1. This is called after a table
6895 ** is dropped from disk (using the Destroy opcode) in order to keep
6896 ** the internal representation of the
6897 ** schema consistent with what is on disk.
6899 case OP_DropTable
: {
6900 sqlite3VdbeIncrWriteCounter(p
, 0);
6901 sqlite3UnlinkAndDeleteTable(db
, pOp
->p1
, pOp
->p4
.z
);
6905 /* Opcode: DropIndex P1 * * P4 *
6907 ** Remove the internal (in-memory) data structures that describe
6908 ** the index named P4 in database P1. This is called after an index
6909 ** is dropped from disk (using the Destroy opcode)
6910 ** in order to keep the internal representation of the
6911 ** schema consistent with what is on disk.
6913 case OP_DropIndex
: {
6914 sqlite3VdbeIncrWriteCounter(p
, 0);
6915 sqlite3UnlinkAndDeleteIndex(db
, pOp
->p1
, pOp
->p4
.z
);
6919 /* Opcode: DropTrigger P1 * * P4 *
6921 ** Remove the internal (in-memory) data structures that describe
6922 ** the trigger named P4 in database P1. This is called after a trigger
6923 ** is dropped from disk (using the Destroy opcode) in order to keep
6924 ** the internal representation of the
6925 ** schema consistent with what is on disk.
6927 case OP_DropTrigger
: {
6928 sqlite3VdbeIncrWriteCounter(p
, 0);
6929 sqlite3UnlinkAndDeleteTrigger(db
, pOp
->p1
, pOp
->p4
.z
);
6934 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
6935 /* Opcode: IntegrityCk P1 P2 P3 P4 P5
6937 ** Do an analysis of the currently open database. Store in
6938 ** register P1 the text of an error message describing any problems.
6939 ** If no problems are found, store a NULL in register P1.
6941 ** The register P3 contains one less than the maximum number of allowed errors.
6942 ** At most reg(P3) errors will be reported.
6943 ** In other words, the analysis stops as soon as reg(P1) errors are
6944 ** seen. Reg(P1) is updated with the number of errors remaining.
6946 ** The root page numbers of all tables in the database are integers
6947 ** stored in P4_INTARRAY argument.
6949 ** If P5 is not zero, the check is done on the auxiliary database
6950 ** file, not the main database file.
6952 ** This opcode is used to implement the integrity_check pragma.
6954 case OP_IntegrityCk
: {
6955 int nRoot
; /* Number of tables to check. (Number of root pages.) */
6956 Pgno
*aRoot
; /* Array of rootpage numbers for tables to be checked */
6957 int nErr
; /* Number of errors reported */
6958 char *z
; /* Text of the error report */
6959 Mem
*pnErr
; /* Register keeping track of errors remaining */
6961 assert( p
->bIsReader
);
6965 assert( aRoot
[0]==(Pgno
)nRoot
);
6966 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
6967 pnErr
= &aMem
[pOp
->p3
];
6968 assert( (pnErr
->flags
& MEM_Int
)!=0 );
6969 assert( (pnErr
->flags
& (MEM_Str
|MEM_Blob
))==0 );
6970 pIn1
= &aMem
[pOp
->p1
];
6971 assert( pOp
->p5
<db
->nDb
);
6972 assert( DbMaskTest(p
->btreeMask
, pOp
->p5
) );
6973 rc
= sqlite3BtreeIntegrityCheck(db
, db
->aDb
[pOp
->p5
].pBt
, &aRoot
[1], nRoot
,
6974 (int)pnErr
->u
.i
+1, &nErr
, &z
);
6975 sqlite3VdbeMemSetNull(pIn1
);
6980 goto abort_due_to_error
;
6982 pnErr
->u
.i
-= nErr
-1;
6983 sqlite3VdbeMemSetStr(pIn1
, z
, -1, SQLITE_UTF8
, sqlite3_free
);
6985 UPDATE_MAX_BLOBSIZE(pIn1
);
6986 sqlite3VdbeChangeEncoding(pIn1
, encoding
);
6987 goto check_for_interrupt
;
6989 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6991 /* Opcode: RowSetAdd P1 P2 * * *
6992 ** Synopsis: rowset(P1)=r[P2]
6994 ** Insert the integer value held by register P2 into a RowSet object
6995 ** held in register P1.
6997 ** An assertion fails if P2 is not an integer.
6999 case OP_RowSetAdd
: { /* in1, in2 */
7000 pIn1
= &aMem
[pOp
->p1
];
7001 pIn2
= &aMem
[pOp
->p2
];
7002 assert( (pIn2
->flags
& MEM_Int
)!=0 );
7003 if( (pIn1
->flags
& MEM_Blob
)==0 ){
7004 if( sqlite3VdbeMemSetRowSet(pIn1
) ) goto no_mem
;
7006 assert( sqlite3VdbeMemIsRowSet(pIn1
) );
7007 sqlite3RowSetInsert((RowSet
*)pIn1
->z
, pIn2
->u
.i
);
7011 /* Opcode: RowSetRead P1 P2 P3 * *
7012 ** Synopsis: r[P3]=rowset(P1)
7014 ** Extract the smallest value from the RowSet object in P1
7015 ** and put that value into register P3.
7016 ** Or, if RowSet object P1 is initially empty, leave P3
7017 ** unchanged and jump to instruction P2.
7019 case OP_RowSetRead
: { /* jump, in1, out3 */
7022 pIn1
= &aMem
[pOp
->p1
];
7023 assert( (pIn1
->flags
& MEM_Blob
)==0 || sqlite3VdbeMemIsRowSet(pIn1
) );
7024 if( (pIn1
->flags
& MEM_Blob
)==0
7025 || sqlite3RowSetNext((RowSet
*)pIn1
->z
, &val
)==0
7027 /* The boolean index is empty */
7028 sqlite3VdbeMemSetNull(pIn1
);
7029 VdbeBranchTaken(1,2);
7030 goto jump_to_p2_and_check_for_interrupt
;
7032 /* A value was pulled from the index */
7033 VdbeBranchTaken(0,2);
7034 sqlite3VdbeMemSetInt64(&aMem
[pOp
->p3
], val
);
7036 goto check_for_interrupt
;
7039 /* Opcode: RowSetTest P1 P2 P3 P4
7040 ** Synopsis: if r[P3] in rowset(P1) goto P2
7042 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
7043 ** contains a RowSet object and that RowSet object contains
7044 ** the value held in P3, jump to register P2. Otherwise, insert the
7045 ** integer in P3 into the RowSet and continue on to the
7048 ** The RowSet object is optimized for the case where sets of integers
7049 ** are inserted in distinct phases, which each set contains no duplicates.
7050 ** Each set is identified by a unique P4 value. The first set
7051 ** must have P4==0, the final set must have P4==-1, and for all other sets
7054 ** This allows optimizations: (a) when P4==0 there is no need to test
7055 ** the RowSet object for P3, as it is guaranteed not to contain it,
7056 ** (b) when P4==-1 there is no need to insert the value, as it will
7057 ** never be tested for, and (c) when a value that is part of set X is
7058 ** inserted, there is no need to search to see if the same value was
7059 ** previously inserted as part of set X (only if it was previously
7060 ** inserted as part of some other set).
7062 case OP_RowSetTest
: { /* jump, in1, in3 */
7066 pIn1
= &aMem
[pOp
->p1
];
7067 pIn3
= &aMem
[pOp
->p3
];
7069 assert( pIn3
->flags
&MEM_Int
);
7071 /* If there is anything other than a rowset object in memory cell P1,
7072 ** delete it now and initialize P1 with an empty rowset
7074 if( (pIn1
->flags
& MEM_Blob
)==0 ){
7075 if( sqlite3VdbeMemSetRowSet(pIn1
) ) goto no_mem
;
7077 assert( sqlite3VdbeMemIsRowSet(pIn1
) );
7078 assert( pOp
->p4type
==P4_INT32
);
7079 assert( iSet
==-1 || iSet
>=0 );
7081 exists
= sqlite3RowSetTest((RowSet
*)pIn1
->z
, iSet
, pIn3
->u
.i
);
7082 VdbeBranchTaken(exists
!=0,2);
7083 if( exists
) goto jump_to_p2
;
7086 sqlite3RowSetInsert((RowSet
*)pIn1
->z
, pIn3
->u
.i
);
7092 #ifndef SQLITE_OMIT_TRIGGER
7094 /* Opcode: Program P1 P2 P3 P4 P5
7096 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
7098 ** P1 contains the address of the memory cell that contains the first memory
7099 ** cell in an array of values used as arguments to the sub-program. P2
7100 ** contains the address to jump to if the sub-program throws an IGNORE
7101 ** exception using the RAISE() function. Register P3 contains the address
7102 ** of a memory cell in this (the parent) VM that is used to allocate the
7103 ** memory required by the sub-vdbe at runtime.
7105 ** P4 is a pointer to the VM containing the trigger program.
7107 ** If P5 is non-zero, then recursive program invocation is enabled.
7109 case OP_Program
: { /* jump */
7110 int nMem
; /* Number of memory registers for sub-program */
7111 int nByte
; /* Bytes of runtime space required for sub-program */
7112 Mem
*pRt
; /* Register to allocate runtime space */
7113 Mem
*pMem
; /* Used to iterate through memory cells */
7114 Mem
*pEnd
; /* Last memory cell in new array */
7115 VdbeFrame
*pFrame
; /* New vdbe frame to execute in */
7116 SubProgram
*pProgram
; /* Sub-program to execute */
7117 void *t
; /* Token identifying trigger */
7119 pProgram
= pOp
->p4
.pProgram
;
7120 pRt
= &aMem
[pOp
->p3
];
7121 assert( pProgram
->nOp
>0 );
7123 /* If the p5 flag is clear, then recursive invocation of triggers is
7124 ** disabled for backwards compatibility (p5 is set if this sub-program
7125 ** is really a trigger, not a foreign key action, and the flag set
7126 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
7128 ** It is recursive invocation of triggers, at the SQL level, that is
7129 ** disabled. In some cases a single trigger may generate more than one
7130 ** SubProgram (if the trigger may be executed with more than one different
7131 ** ON CONFLICT algorithm). SubProgram structures associated with a
7132 ** single trigger all have the same value for the SubProgram.token
7135 t
= pProgram
->token
;
7136 for(pFrame
=p
->pFrame
; pFrame
&& pFrame
->token
!=t
; pFrame
=pFrame
->pParent
);
7140 if( p
->nFrame
>=db
->aLimit
[SQLITE_LIMIT_TRIGGER_DEPTH
] ){
7142 sqlite3VdbeError(p
, "too many levels of trigger recursion");
7143 goto abort_due_to_error
;
7146 /* Register pRt is used to store the memory required to save the state
7147 ** of the current program, and the memory required at runtime to execute
7148 ** the trigger program. If this trigger has been fired before, then pRt
7149 ** is already allocated. Otherwise, it must be initialized. */
7150 if( (pRt
->flags
&MEM_Blob
)==0 ){
7151 /* SubProgram.nMem is set to the number of memory cells used by the
7152 ** program stored in SubProgram.aOp. As well as these, one memory
7153 ** cell is required for each cursor used by the program. Set local
7154 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
7156 nMem
= pProgram
->nMem
+ pProgram
->nCsr
;
7158 if( pProgram
->nCsr
==0 ) nMem
++;
7159 nByte
= ROUND8(sizeof(VdbeFrame
))
7160 + nMem
* sizeof(Mem
)
7161 + pProgram
->nCsr
* sizeof(VdbeCursor
*)
7162 + (pProgram
->nOp
+ 7)/8;
7163 pFrame
= sqlite3DbMallocZero(db
, nByte
);
7167 sqlite3VdbeMemRelease(pRt
);
7168 pRt
->flags
= MEM_Blob
|MEM_Dyn
;
7169 pRt
->z
= (char*)pFrame
;
7171 pRt
->xDel
= sqlite3VdbeFrameMemDel
;
7174 pFrame
->nChildMem
= nMem
;
7175 pFrame
->nChildCsr
= pProgram
->nCsr
;
7176 pFrame
->pc
= (int)(pOp
- aOp
);
7177 pFrame
->aMem
= p
->aMem
;
7178 pFrame
->nMem
= p
->nMem
;
7179 pFrame
->apCsr
= p
->apCsr
;
7180 pFrame
->nCursor
= p
->nCursor
;
7181 pFrame
->aOp
= p
->aOp
;
7182 pFrame
->nOp
= p
->nOp
;
7183 pFrame
->token
= pProgram
->token
;
7185 pFrame
->iFrameMagic
= SQLITE_FRAME_MAGIC
;
7188 pEnd
= &VdbeFrameMem(pFrame
)[pFrame
->nChildMem
];
7189 for(pMem
=VdbeFrameMem(pFrame
); pMem
!=pEnd
; pMem
++){
7190 pMem
->flags
= MEM_Undefined
;
7194 pFrame
= (VdbeFrame
*)pRt
->z
;
7195 assert( pRt
->xDel
==sqlite3VdbeFrameMemDel
);
7196 assert( pProgram
->nMem
+pProgram
->nCsr
==pFrame
->nChildMem
7197 || (pProgram
->nCsr
==0 && pProgram
->nMem
+1==pFrame
->nChildMem
) );
7198 assert( pProgram
->nCsr
==pFrame
->nChildCsr
);
7199 assert( (int)(pOp
- aOp
)==pFrame
->pc
);
7203 pFrame
->pParent
= p
->pFrame
;
7204 pFrame
->lastRowid
= db
->lastRowid
;
7205 pFrame
->nChange
= p
->nChange
;
7206 pFrame
->nDbChange
= p
->db
->nChange
;
7207 assert( pFrame
->pAuxData
==0 );
7208 pFrame
->pAuxData
= p
->pAuxData
;
7212 p
->aMem
= aMem
= VdbeFrameMem(pFrame
);
7213 p
->nMem
= pFrame
->nChildMem
;
7214 p
->nCursor
= (u16
)pFrame
->nChildCsr
;
7215 p
->apCsr
= (VdbeCursor
**)&aMem
[p
->nMem
];
7216 pFrame
->aOnce
= (u8
*)&p
->apCsr
[pProgram
->nCsr
];
7217 memset(pFrame
->aOnce
, 0, (pProgram
->nOp
+ 7)/8);
7218 p
->aOp
= aOp
= pProgram
->aOp
;
7219 p
->nOp
= pProgram
->nOp
;
7221 /* Verify that second and subsequent executions of the same trigger do not
7222 ** try to reuse register values from the first use. */
7225 for(i
=0; i
<p
->nMem
; i
++){
7226 aMem
[i
].pScopyFrom
= 0; /* Prevent false-positive AboutToChange() errs */
7227 MemSetTypeFlag(&aMem
[i
], MEM_Undefined
); /* Fault if this reg is reused */
7232 goto check_for_interrupt
;
7235 /* Opcode: Param P1 P2 * * *
7237 ** This opcode is only ever present in sub-programs called via the
7238 ** OP_Program instruction. Copy a value currently stored in a memory
7239 ** cell of the calling (parent) frame to cell P2 in the current frames
7240 ** address space. This is used by trigger programs to access the new.*
7241 ** and old.* values.
7243 ** The address of the cell in the parent frame is determined by adding
7244 ** the value of the P1 argument to the value of the P1 argument to the
7245 ** calling OP_Program instruction.
7247 case OP_Param
: { /* out2 */
7250 pOut
= out2Prerelease(p
, pOp
);
7252 pIn
= &pFrame
->aMem
[pOp
->p1
+ pFrame
->aOp
[pFrame
->pc
].p1
];
7253 sqlite3VdbeMemShallowCopy(pOut
, pIn
, MEM_Ephem
);
7257 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
7259 #ifndef SQLITE_OMIT_FOREIGN_KEY
7260 /* Opcode: FkCounter P1 P2 * * *
7261 ** Synopsis: fkctr[P1]+=P2
7263 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
7264 ** If P1 is non-zero, the database constraint counter is incremented
7265 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
7266 ** statement counter is incremented (immediate foreign key constraints).
7268 case OP_FkCounter
: {
7269 if( db
->flags
& SQLITE_DeferFKs
){
7270 db
->nDeferredImmCons
+= pOp
->p2
;
7271 }else if( pOp
->p1
){
7272 db
->nDeferredCons
+= pOp
->p2
;
7274 p
->nFkConstraint
+= pOp
->p2
;
7279 /* Opcode: FkIfZero P1 P2 * * *
7280 ** Synopsis: if fkctr[P1]==0 goto P2
7282 ** This opcode tests if a foreign key constraint-counter is currently zero.
7283 ** If so, jump to instruction P2. Otherwise, fall through to the next
7286 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
7287 ** is zero (the one that counts deferred constraint violations). If P1 is
7288 ** zero, the jump is taken if the statement constraint-counter is zero
7289 ** (immediate foreign key constraint violations).
7291 case OP_FkIfZero
: { /* jump */
7293 VdbeBranchTaken(db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0, 2);
7294 if( db
->nDeferredCons
==0 && db
->nDeferredImmCons
==0 ) goto jump_to_p2
;
7296 VdbeBranchTaken(p
->nFkConstraint
==0 && db
->nDeferredImmCons
==0, 2);
7297 if( p
->nFkConstraint
==0 && db
->nDeferredImmCons
==0 ) goto jump_to_p2
;
7301 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
7303 #ifndef SQLITE_OMIT_AUTOINCREMENT
7304 /* Opcode: MemMax P1 P2 * * *
7305 ** Synopsis: r[P1]=max(r[P1],r[P2])
7307 ** P1 is a register in the root frame of this VM (the root frame is
7308 ** different from the current frame if this instruction is being executed
7309 ** within a sub-program). Set the value of register P1 to the maximum of
7310 ** its current value and the value in register P2.
7312 ** This instruction throws an error if the memory cell is not initially
7315 case OP_MemMax
: { /* in2 */
7318 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
7319 pIn1
= &pFrame
->aMem
[pOp
->p1
];
7321 pIn1
= &aMem
[pOp
->p1
];
7323 assert( memIsValid(pIn1
) );
7324 sqlite3VdbeMemIntegerify(pIn1
);
7325 pIn2
= &aMem
[pOp
->p2
];
7326 sqlite3VdbeMemIntegerify(pIn2
);
7327 if( pIn1
->u
.i
<pIn2
->u
.i
){
7328 pIn1
->u
.i
= pIn2
->u
.i
;
7332 #endif /* SQLITE_OMIT_AUTOINCREMENT */
7334 /* Opcode: IfPos P1 P2 P3 * *
7335 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
7337 ** Register P1 must contain an integer.
7338 ** If the value of register P1 is 1 or greater, subtract P3 from the
7339 ** value in P1 and jump to P2.
7341 ** If the initial value of register P1 is less than 1, then the
7342 ** value is unchanged and control passes through to the next instruction.
7344 case OP_IfPos
: { /* jump, in1 */
7345 pIn1
= &aMem
[pOp
->p1
];
7346 assert( pIn1
->flags
&MEM_Int
);
7347 VdbeBranchTaken( pIn1
->u
.i
>0, 2);
7349 pIn1
->u
.i
-= pOp
->p3
;
7355 /* Opcode: OffsetLimit P1 P2 P3 * *
7356 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
7358 ** This opcode performs a commonly used computation associated with
7359 ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
7360 ** holds the offset counter. The opcode computes the combined value
7361 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
7362 ** value computed is the total number of rows that will need to be
7363 ** visited in order to complete the query.
7365 ** If r[P3] is zero or negative, that means there is no OFFSET
7366 ** and r[P2] is set to be the value of the LIMIT, r[P1].
7368 ** if r[P1] is zero or negative, that means there is no LIMIT
7369 ** and r[P2] is set to -1.
7371 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
7373 case OP_OffsetLimit
: { /* in1, out2, in3 */
7375 pIn1
= &aMem
[pOp
->p1
];
7376 pIn3
= &aMem
[pOp
->p3
];
7377 pOut
= out2Prerelease(p
, pOp
);
7378 assert( pIn1
->flags
& MEM_Int
);
7379 assert( pIn3
->flags
& MEM_Int
);
7381 if( x
<=0 || sqlite3AddInt64(&x
, pIn3
->u
.i
>0?pIn3
->u
.i
:0) ){
7382 /* If the LIMIT is less than or equal to zero, loop forever. This
7383 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
7384 ** also loop forever. This is undocumented. In fact, one could argue
7385 ** that the loop should terminate. But assuming 1 billion iterations
7386 ** per second (far exceeding the capabilities of any current hardware)
7387 ** it would take nearly 300 years to actually reach the limit. So
7388 ** looping forever is a reasonable approximation. */
7396 /* Opcode: IfNotZero P1 P2 * * *
7397 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
7399 ** Register P1 must contain an integer. If the content of register P1 is
7400 ** initially greater than zero, then decrement the value in register P1.
7401 ** If it is non-zero (negative or positive) and then also jump to P2.
7402 ** If register P1 is initially zero, leave it unchanged and fall through.
7404 case OP_IfNotZero
: { /* jump, in1 */
7405 pIn1
= &aMem
[pOp
->p1
];
7406 assert( pIn1
->flags
&MEM_Int
);
7407 VdbeBranchTaken(pIn1
->u
.i
<0, 2);
7409 if( pIn1
->u
.i
>0 ) pIn1
->u
.i
--;
7415 /* Opcode: DecrJumpZero P1 P2 * * *
7416 ** Synopsis: if (--r[P1])==0 goto P2
7418 ** Register P1 must hold an integer. Decrement the value in P1
7419 ** and jump to P2 if the new value is exactly zero.
7421 case OP_DecrJumpZero
: { /* jump, in1 */
7422 pIn1
= &aMem
[pOp
->p1
];
7423 assert( pIn1
->flags
&MEM_Int
);
7424 if( pIn1
->u
.i
>SMALLEST_INT64
) pIn1
->u
.i
--;
7425 VdbeBranchTaken(pIn1
->u
.i
==0, 2);
7426 if( pIn1
->u
.i
==0 ) goto jump_to_p2
;
7431 /* Opcode: AggStep * P2 P3 P4 P5
7432 ** Synopsis: accum=r[P3] step(r[P2@P5])
7434 ** Execute the xStep function for an aggregate.
7435 ** The function has P5 arguments. P4 is a pointer to the
7436 ** FuncDef structure that specifies the function. Register P3 is the
7439 ** The P5 arguments are taken from register P2 and its
7442 /* Opcode: AggInverse * P2 P3 P4 P5
7443 ** Synopsis: accum=r[P3] inverse(r[P2@P5])
7445 ** Execute the xInverse function for an aggregate.
7446 ** The function has P5 arguments. P4 is a pointer to the
7447 ** FuncDef structure that specifies the function. Register P3 is the
7450 ** The P5 arguments are taken from register P2 and its
7453 /* Opcode: AggStep1 P1 P2 P3 P4 P5
7454 ** Synopsis: accum=r[P3] step(r[P2@P5])
7456 ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
7457 ** aggregate. The function has P5 arguments. P4 is a pointer to the
7458 ** FuncDef structure that specifies the function. Register P3 is the
7461 ** The P5 arguments are taken from register P2 and its
7464 ** This opcode is initially coded as OP_AggStep0. On first evaluation,
7465 ** the FuncDef stored in P4 is converted into an sqlite3_context and
7466 ** the opcode is changed. In this way, the initialization of the
7467 ** sqlite3_context only happens once, instead of on each call to the
7473 sqlite3_context
*pCtx
;
7475 assert( pOp
->p4type
==P4_FUNCDEF
);
7477 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
7478 assert( n
==0 || (pOp
->p2
>0 && pOp
->p2
+n
<=(p
->nMem
+1 - p
->nCursor
)+1) );
7479 assert( pOp
->p3
<pOp
->p2
|| pOp
->p3
>=pOp
->p2
+n
);
7480 pCtx
= sqlite3DbMallocRawNN(db
, n
*sizeof(sqlite3_value
*) +
7481 (sizeof(pCtx
[0]) + sizeof(Mem
) - sizeof(sqlite3_value
*)));
7482 if( pCtx
==0 ) goto no_mem
;
7484 pCtx
->pOut
= (Mem
*)&(pCtx
->argv
[n
]);
7485 sqlite3VdbeMemInit(pCtx
->pOut
, db
, MEM_Null
);
7486 pCtx
->pFunc
= pOp
->p4
.pFunc
;
7487 pCtx
->iOp
= (int)(pOp
- aOp
);
7491 pCtx
->enc
= encoding
;
7493 pOp
->p4type
= P4_FUNCCTX
;
7494 pOp
->p4
.pCtx
= pCtx
;
7496 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
7497 assert( pOp
->p1
==(pOp
->opcode
==OP_AggInverse
) );
7499 pOp
->opcode
= OP_AggStep1
;
7500 /* Fall through into OP_AggStep */
7501 /* no break */ deliberate_fall_through
7505 sqlite3_context
*pCtx
;
7508 assert( pOp
->p4type
==P4_FUNCCTX
);
7509 pCtx
= pOp
->p4
.pCtx
;
7510 pMem
= &aMem
[pOp
->p3
];
7514 /* This is an OP_AggInverse call. Verify that xStep has always
7515 ** been called at least once prior to any xInverse call. */
7516 assert( pMem
->uTemp
==0x1122e0e3 );
7518 /* This is an OP_AggStep call. Mark it as such. */
7519 pMem
->uTemp
= 0x1122e0e3;
7523 /* If this function is inside of a trigger, the register array in aMem[]
7524 ** might change from one evaluation to the next. The next block of code
7525 ** checks to see if the register array has changed, and if so it
7526 ** reinitializes the relavant parts of the sqlite3_context object */
7527 if( pCtx
->pMem
!= pMem
){
7529 for(i
=pCtx
->argc
-1; i
>=0; i
--) pCtx
->argv
[i
] = &aMem
[pOp
->p2
+i
];
7533 for(i
=0; i
<pCtx
->argc
; i
++){
7534 assert( memIsValid(pCtx
->argv
[i
]) );
7535 REGISTER_TRACE(pOp
->p2
+i
, pCtx
->argv
[i
]);
7540 assert( pCtx
->pOut
->flags
==MEM_Null
);
7541 assert( pCtx
->isError
==0 );
7542 assert( pCtx
->skipFlag
==0 );
7543 #ifndef SQLITE_OMIT_WINDOWFUNC
7545 (pCtx
->pFunc
->xInverse
)(pCtx
,pCtx
->argc
,pCtx
->argv
);
7548 (pCtx
->pFunc
->xSFunc
)(pCtx
,pCtx
->argc
,pCtx
->argv
); /* IMP: R-24505-23230 */
7550 if( pCtx
->isError
){
7551 if( pCtx
->isError
>0 ){
7552 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pCtx
->pOut
));
7555 if( pCtx
->skipFlag
){
7556 assert( pOp
[-1].opcode
==OP_CollSeq
);
7558 if( i
) sqlite3VdbeMemSetInt64(&aMem
[i
], 1);
7561 sqlite3VdbeMemRelease(pCtx
->pOut
);
7562 pCtx
->pOut
->flags
= MEM_Null
;
7564 if( rc
) goto abort_due_to_error
;
7566 assert( pCtx
->pOut
->flags
==MEM_Null
);
7567 assert( pCtx
->skipFlag
==0 );
7571 /* Opcode: AggFinal P1 P2 * P4 *
7572 ** Synopsis: accum=r[P1] N=P2
7574 ** P1 is the memory location that is the accumulator for an aggregate
7575 ** or window function. Execute the finalizer function
7576 ** for an aggregate and store the result in P1.
7578 ** P2 is the number of arguments that the step function takes and
7579 ** P4 is a pointer to the FuncDef for this function. The P2
7580 ** argument is not used by this opcode. It is only there to disambiguate
7581 ** functions that can take varying numbers of arguments. The
7582 ** P4 argument is only needed for the case where
7583 ** the step function was not previously called.
7585 /* Opcode: AggValue * P2 P3 P4 *
7586 ** Synopsis: r[P3]=value N=P2
7588 ** Invoke the xValue() function and store the result in register P3.
7590 ** P2 is the number of arguments that the step function takes and
7591 ** P4 is a pointer to the FuncDef for this function. The P2
7592 ** argument is not used by this opcode. It is only there to disambiguate
7593 ** functions that can take varying numbers of arguments. The
7594 ** P4 argument is only needed for the case where
7595 ** the step function was not previously called.
7600 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
7601 assert( pOp
->p3
==0 || pOp
->opcode
==OP_AggValue
);
7602 pMem
= &aMem
[pOp
->p1
];
7603 assert( (pMem
->flags
& ~(MEM_Null
|MEM_Agg
))==0 );
7604 #ifndef SQLITE_OMIT_WINDOWFUNC
7606 memAboutToChange(p
, &aMem
[pOp
->p3
]);
7607 rc
= sqlite3VdbeMemAggValue(pMem
, &aMem
[pOp
->p3
], pOp
->p4
.pFunc
);
7608 pMem
= &aMem
[pOp
->p3
];
7612 rc
= sqlite3VdbeMemFinalize(pMem
, pOp
->p4
.pFunc
);
7616 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pMem
));
7617 goto abort_due_to_error
;
7619 sqlite3VdbeChangeEncoding(pMem
, encoding
);
7620 UPDATE_MAX_BLOBSIZE(pMem
);
7624 #ifndef SQLITE_OMIT_WAL
7625 /* Opcode: Checkpoint P1 P2 P3 * *
7627 ** Checkpoint database P1. This is a no-op if P1 is not currently in
7628 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7629 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
7630 ** SQLITE_BUSY or not, respectively. Write the number of pages in the
7631 ** WAL after the checkpoint into mem[P3+1] and the number of pages
7632 ** in the WAL that have been checkpointed after the checkpoint
7633 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
7634 ** mem[P3+2] are initialized to -1.
7636 case OP_Checkpoint
: {
7637 int i
; /* Loop counter */
7638 int aRes
[3]; /* Results */
7639 Mem
*pMem
; /* Write results here */
7641 assert( p
->readOnly
==0 );
7643 aRes
[1] = aRes
[2] = -1;
7644 assert( pOp
->p2
==SQLITE_CHECKPOINT_PASSIVE
7645 || pOp
->p2
==SQLITE_CHECKPOINT_FULL
7646 || pOp
->p2
==SQLITE_CHECKPOINT_RESTART
7647 || pOp
->p2
==SQLITE_CHECKPOINT_TRUNCATE
7649 rc
= sqlite3Checkpoint(db
, pOp
->p1
, pOp
->p2
, &aRes
[1], &aRes
[2]);
7651 if( rc
!=SQLITE_BUSY
) goto abort_due_to_error
;
7655 for(i
=0, pMem
= &aMem
[pOp
->p3
]; i
<3; i
++, pMem
++){
7656 sqlite3VdbeMemSetInt64(pMem
, (i64
)aRes
[i
]);
7662 #ifndef SQLITE_OMIT_PRAGMA
7663 /* Opcode: JournalMode P1 P2 P3 * *
7665 ** Change the journal mode of database P1 to P3. P3 must be one of the
7666 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7667 ** modes (delete, truncate, persist, off and memory), this is a simple
7668 ** operation. No IO is required.
7670 ** If changing into or out of WAL mode the procedure is more complicated.
7672 ** Write a string containing the final journal-mode to register P2.
7674 case OP_JournalMode
: { /* out2 */
7675 Btree
*pBt
; /* Btree to change journal mode of */
7676 Pager
*pPager
; /* Pager associated with pBt */
7677 int eNew
; /* New journal mode */
7678 int eOld
; /* The old journal mode */
7679 #ifndef SQLITE_OMIT_WAL
7680 const char *zFilename
; /* Name of database file for pPager */
7683 pOut
= out2Prerelease(p
, pOp
);
7685 assert( eNew
==PAGER_JOURNALMODE_DELETE
7686 || eNew
==PAGER_JOURNALMODE_TRUNCATE
7687 || eNew
==PAGER_JOURNALMODE_PERSIST
7688 || eNew
==PAGER_JOURNALMODE_OFF
7689 || eNew
==PAGER_JOURNALMODE_MEMORY
7690 || eNew
==PAGER_JOURNALMODE_WAL
7691 || eNew
==PAGER_JOURNALMODE_QUERY
7693 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
7694 assert( p
->readOnly
==0 );
7696 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7697 pPager
= sqlite3BtreePager(pBt
);
7698 eOld
= sqlite3PagerGetJournalMode(pPager
);
7699 if( eNew
==PAGER_JOURNALMODE_QUERY
) eNew
= eOld
;
7700 assert( sqlite3BtreeHoldsMutex(pBt
) );
7701 if( !sqlite3PagerOkToChangeJournalMode(pPager
) ) eNew
= eOld
;
7703 #ifndef SQLITE_OMIT_WAL
7704 zFilename
= sqlite3PagerFilename(pPager
, 1);
7706 /* Do not allow a transition to journal_mode=WAL for a database
7707 ** in temporary storage or if the VFS does not support shared memory
7709 if( eNew
==PAGER_JOURNALMODE_WAL
7710 && (sqlite3Strlen30(zFilename
)==0 /* Temp file */
7711 || !sqlite3PagerWalSupported(pPager
)) /* No shared-memory support */
7717 && (eOld
==PAGER_JOURNALMODE_WAL
|| eNew
==PAGER_JOURNALMODE_WAL
)
7719 if( !db
->autoCommit
|| db
->nVdbeRead
>1 ){
7722 "cannot change %s wal mode from within a transaction",
7723 (eNew
==PAGER_JOURNALMODE_WAL
? "into" : "out of")
7725 goto abort_due_to_error
;
7728 if( eOld
==PAGER_JOURNALMODE_WAL
){
7729 /* If leaving WAL mode, close the log file. If successful, the call
7730 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7731 ** file. An EXCLUSIVE lock may still be held on the database file
7732 ** after a successful return.
7734 rc
= sqlite3PagerCloseWal(pPager
, db
);
7735 if( rc
==SQLITE_OK
){
7736 sqlite3PagerSetJournalMode(pPager
, eNew
);
7738 }else if( eOld
==PAGER_JOURNALMODE_MEMORY
){
7739 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7740 ** as an intermediate */
7741 sqlite3PagerSetJournalMode(pPager
, PAGER_JOURNALMODE_OFF
);
7744 /* Open a transaction on the database file. Regardless of the journal
7745 ** mode, this transaction always uses a rollback journal.
7747 assert( sqlite3BtreeTxnState(pBt
)!=SQLITE_TXN_WRITE
);
7748 if( rc
==SQLITE_OK
){
7749 rc
= sqlite3BtreeSetVersion(pBt
, (eNew
==PAGER_JOURNALMODE_WAL
? 2 : 1));
7753 #endif /* ifndef SQLITE_OMIT_WAL */
7755 if( rc
) eNew
= eOld
;
7756 eNew
= sqlite3PagerSetJournalMode(pPager
, eNew
);
7758 pOut
->flags
= MEM_Str
|MEM_Static
|MEM_Term
;
7759 pOut
->z
= (char *)sqlite3JournalModename(eNew
);
7760 pOut
->n
= sqlite3Strlen30(pOut
->z
);
7761 pOut
->enc
= SQLITE_UTF8
;
7762 sqlite3VdbeChangeEncoding(pOut
, encoding
);
7763 if( rc
) goto abort_due_to_error
;
7766 #endif /* SQLITE_OMIT_PRAGMA */
7768 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
7769 /* Opcode: Vacuum P1 P2 * * *
7771 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7772 ** for an attached database. The "temp" database may not be vacuumed.
7774 ** If P2 is not zero, then it is a register holding a string which is
7775 ** the file into which the result of vacuum should be written. When
7776 ** P2 is zero, the vacuum overwrites the original database.
7779 assert( p
->readOnly
==0 );
7780 rc
= sqlite3RunVacuum(&p
->zErrMsg
, db
, pOp
->p1
,
7781 pOp
->p2
? &aMem
[pOp
->p2
] : 0);
7782 if( rc
) goto abort_due_to_error
;
7787 #if !defined(SQLITE_OMIT_AUTOVACUUM)
7788 /* Opcode: IncrVacuum P1 P2 * * *
7790 ** Perform a single step of the incremental vacuum procedure on
7791 ** the P1 database. If the vacuum has finished, jump to instruction
7792 ** P2. Otherwise, fall through to the next instruction.
7794 case OP_IncrVacuum
: { /* jump */
7797 assert( pOp
->p1
>=0 && pOp
->p1
<db
->nDb
);
7798 assert( DbMaskTest(p
->btreeMask
, pOp
->p1
) );
7799 assert( p
->readOnly
==0 );
7800 pBt
= db
->aDb
[pOp
->p1
].pBt
;
7801 rc
= sqlite3BtreeIncrVacuum(pBt
);
7802 VdbeBranchTaken(rc
==SQLITE_DONE
,2);
7804 if( rc
!=SQLITE_DONE
) goto abort_due_to_error
;
7812 /* Opcode: Expire P1 P2 * * *
7814 ** Cause precompiled statements to expire. When an expired statement
7815 ** is executed using sqlite3_step() it will either automatically
7816 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7817 ** or it will fail with SQLITE_SCHEMA.
7819 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
7820 ** then only the currently executing statement is expired.
7822 ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7823 ** then running SQL statements are allowed to continue to run to completion.
7824 ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7825 ** that might help the statement run faster but which does not affect the
7826 ** correctness of operation.
7829 assert( pOp
->p2
==0 || pOp
->p2
==1 );
7831 sqlite3ExpirePreparedStatements(db
, pOp
->p2
);
7833 p
->expired
= pOp
->p2
+1;
7838 /* Opcode: CursorLock P1 * * * *
7840 ** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7841 ** written by an other cursor.
7843 case OP_CursorLock
: {
7845 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
7846 pC
= p
->apCsr
[pOp
->p1
];
7848 assert( pC
->eCurType
==CURTYPE_BTREE
);
7849 sqlite3BtreeCursorPin(pC
->uc
.pCursor
);
7853 /* Opcode: CursorUnlock P1 * * * *
7855 ** Unlock the btree to which cursor P1 is pointing so that it can be
7856 ** written by other cursors.
7858 case OP_CursorUnlock
: {
7860 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
7861 pC
= p
->apCsr
[pOp
->p1
];
7863 assert( pC
->eCurType
==CURTYPE_BTREE
);
7864 sqlite3BtreeCursorUnpin(pC
->uc
.pCursor
);
7868 #ifndef SQLITE_OMIT_SHARED_CACHE
7869 /* Opcode: TableLock P1 P2 P3 P4 *
7870 ** Synopsis: iDb=P1 root=P2 write=P3
7872 ** Obtain a lock on a particular table. This instruction is only used when
7873 ** the shared-cache feature is enabled.
7875 ** P1 is the index of the database in sqlite3.aDb[] of the database
7876 ** on which the lock is acquired. A readlock is obtained if P3==0 or
7877 ** a write lock if P3==1.
7879 ** P2 contains the root-page of the table to lock.
7881 ** P4 contains a pointer to the name of the table being locked. This is only
7882 ** used to generate an error message if the lock cannot be obtained.
7884 case OP_TableLock
: {
7885 u8 isWriteLock
= (u8
)pOp
->p3
;
7886 if( isWriteLock
|| 0==(db
->flags
&SQLITE_ReadUncommit
) ){
7888 assert( p1
>=0 && p1
<db
->nDb
);
7889 assert( DbMaskTest(p
->btreeMask
, p1
) );
7890 assert( isWriteLock
==0 || isWriteLock
==1 );
7891 rc
= sqlite3BtreeLockTable(db
->aDb
[p1
].pBt
, pOp
->p2
, isWriteLock
);
7893 if( (rc
&0xFF)==SQLITE_LOCKED
){
7894 const char *z
= pOp
->p4
.z
;
7895 sqlite3VdbeError(p
, "database table is locked: %s", z
);
7897 goto abort_due_to_error
;
7902 #endif /* SQLITE_OMIT_SHARED_CACHE */
7904 #ifndef SQLITE_OMIT_VIRTUALTABLE
7905 /* Opcode: VBegin * * * P4 *
7907 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7908 ** xBegin method for that table.
7910 ** Also, whether or not P4 is set, check that this is not being called from
7911 ** within a callback to a virtual table xSync() method. If it is, the error
7912 ** code will be set to SQLITE_LOCKED.
7916 pVTab
= pOp
->p4
.pVtab
;
7917 rc
= sqlite3VtabBegin(db
, pVTab
);
7918 if( pVTab
) sqlite3VtabImportErrmsg(p
, pVTab
->pVtab
);
7919 if( rc
) goto abort_due_to_error
;
7922 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7924 #ifndef SQLITE_OMIT_VIRTUALTABLE
7925 /* Opcode: VCreate P1 P2 * * *
7927 ** P2 is a register that holds the name of a virtual table in database
7928 ** P1. Call the xCreate method for that table.
7931 Mem sMem
; /* For storing the record being decoded */
7932 const char *zTab
; /* Name of the virtual table */
7934 memset(&sMem
, 0, sizeof(sMem
));
7936 /* Because P2 is always a static string, it is impossible for the
7937 ** sqlite3VdbeMemCopy() to fail */
7938 assert( (aMem
[pOp
->p2
].flags
& MEM_Str
)!=0 );
7939 assert( (aMem
[pOp
->p2
].flags
& MEM_Static
)!=0 );
7940 rc
= sqlite3VdbeMemCopy(&sMem
, &aMem
[pOp
->p2
]);
7941 assert( rc
==SQLITE_OK
);
7942 zTab
= (const char*)sqlite3_value_text(&sMem
);
7943 assert( zTab
|| db
->mallocFailed
);
7945 rc
= sqlite3VtabCallCreate(db
, pOp
->p1
, zTab
, &p
->zErrMsg
);
7947 sqlite3VdbeMemRelease(&sMem
);
7948 if( rc
) goto abort_due_to_error
;
7951 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7953 #ifndef SQLITE_OMIT_VIRTUALTABLE
7954 /* Opcode: VDestroy P1 * * P4 *
7956 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
7961 rc
= sqlite3VtabCallDestroy(db
, pOp
->p1
, pOp
->p4
.z
);
7963 assert( p
->errorAction
==OE_Abort
&& p
->usesStmtJournal
);
7964 if( rc
) goto abort_due_to_error
;
7967 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7969 #ifndef SQLITE_OMIT_VIRTUALTABLE
7970 /* Opcode: VOpen P1 * * P4 *
7972 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
7973 ** P1 is a cursor number. This opcode opens a cursor to the virtual
7974 ** table and stores that cursor in P1.
7976 case OP_VOpen
: { /* ncycle */
7978 sqlite3_vtab_cursor
*pVCur
;
7979 sqlite3_vtab
*pVtab
;
7980 const sqlite3_module
*pModule
;
7982 assert( p
->bIsReader
);
7985 pVtab
= pOp
->p4
.pVtab
->pVtab
;
7986 if( pVtab
==0 || NEVER(pVtab
->pModule
==0) ){
7988 goto abort_due_to_error
;
7990 pModule
= pVtab
->pModule
;
7991 rc
= pModule
->xOpen(pVtab
, &pVCur
);
7992 sqlite3VtabImportErrmsg(p
, pVtab
);
7993 if( rc
) goto abort_due_to_error
;
7995 /* Initialize sqlite3_vtab_cursor base class */
7996 pVCur
->pVtab
= pVtab
;
7998 /* Initialize vdbe cursor object */
7999 pCur
= allocateCursor(p
, pOp
->p1
, 0, CURTYPE_VTAB
);
8001 pCur
->uc
.pVCur
= pVCur
;
8004 assert( db
->mallocFailed
);
8005 pModule
->xClose(pVCur
);
8010 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8012 #ifndef SQLITE_OMIT_VIRTUALTABLE
8013 /* Opcode: VInitIn P1 P2 P3 * *
8014 ** Synopsis: r[P2]=ValueList(P1,P3)
8016 ** Set register P2 to be a pointer to a ValueList object for cursor P1
8017 ** with cache register P3 and output register P3+1. This ValueList object
8018 ** can be used as the first argument to sqlite3_vtab_in_first() and
8019 ** sqlite3_vtab_in_next() to extract all of the values stored in the P1
8020 ** cursor. Register P3 is used to hold the values returned by
8021 ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
8023 case OP_VInitIn
: { /* out2, ncycle */
8024 VdbeCursor
*pC
; /* The cursor containing the RHS values */
8025 ValueList
*pRhs
; /* New ValueList object to put in reg[P2] */
8027 pC
= p
->apCsr
[pOp
->p1
];
8028 pRhs
= sqlite3_malloc64( sizeof(*pRhs
) );
8029 if( pRhs
==0 ) goto no_mem
;
8030 pRhs
->pCsr
= pC
->uc
.pCursor
;
8031 pRhs
->pOut
= &aMem
[pOp
->p3
];
8032 pOut
= out2Prerelease(p
, pOp
);
8033 pOut
->flags
= MEM_Null
;
8034 sqlite3VdbeMemSetPointer(pOut
, pRhs
, "ValueList", sqlite3VdbeValueListFree
);
8037 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8040 #ifndef SQLITE_OMIT_VIRTUALTABLE
8041 /* Opcode: VFilter P1 P2 P3 P4 *
8042 ** Synopsis: iplan=r[P3] zplan='P4'
8044 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
8045 ** the filtered result set is empty.
8047 ** P4 is either NULL or a string that was generated by the xBestIndex
8048 ** method of the module. The interpretation of the P4 string is left
8049 ** to the module implementation.
8051 ** This opcode invokes the xFilter method on the virtual table specified
8052 ** by P1. The integer query plan parameter to xFilter is stored in register
8053 ** P3. Register P3+1 stores the argc parameter to be passed to the
8054 ** xFilter method. Registers P3+2..P3+1+argc are the argc
8055 ** additional parameters which are passed to
8056 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
8058 ** A jump is made to P2 if the result set after filtering would be empty.
8060 case OP_VFilter
: { /* jump, ncycle */
8063 const sqlite3_module
*pModule
;
8066 sqlite3_vtab_cursor
*pVCur
;
8067 sqlite3_vtab
*pVtab
;
8073 pQuery
= &aMem
[pOp
->p3
];
8075 pCur
= p
->apCsr
[pOp
->p1
];
8076 assert( memIsValid(pQuery
) );
8077 REGISTER_TRACE(pOp
->p3
, pQuery
);
8079 assert( pCur
->eCurType
==CURTYPE_VTAB
);
8080 pVCur
= pCur
->uc
.pVCur
;
8081 pVtab
= pVCur
->pVtab
;
8082 pModule
= pVtab
->pModule
;
8084 /* Grab the index number and argc parameters */
8085 assert( (pQuery
->flags
&MEM_Int
)!=0 && pArgc
->flags
==MEM_Int
);
8086 nArg
= (int)pArgc
->u
.i
;
8087 iQuery
= (int)pQuery
->u
.i
;
8089 /* Invoke the xFilter method */
8091 for(i
= 0; i
<nArg
; i
++){
8092 apArg
[i
] = &pArgc
[i
+1];
8094 rc
= pModule
->xFilter(pVCur
, iQuery
, pOp
->p4
.z
, nArg
, apArg
);
8095 sqlite3VtabImportErrmsg(p
, pVtab
);
8096 if( rc
) goto abort_due_to_error
;
8097 res
= pModule
->xEof(pVCur
);
8099 VdbeBranchTaken(res
!=0,2);
8100 if( res
) goto jump_to_p2
;
8103 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8105 #ifndef SQLITE_OMIT_VIRTUALTABLE
8106 /* Opcode: VColumn P1 P2 P3 * P5
8107 ** Synopsis: r[P3]=vcolumn(P2)
8109 ** Store in register P3 the value of the P2-th column of
8110 ** the current row of the virtual-table of cursor P1.
8112 ** If the VColumn opcode is being used to fetch the value of
8113 ** an unchanging column during an UPDATE operation, then the P5
8114 ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
8115 ** function to return true inside the xColumn method of the virtual
8116 ** table implementation. The P5 column might also contain other
8117 ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
8118 ** unused by OP_VColumn.
8120 case OP_VColumn
: { /* ncycle */
8121 sqlite3_vtab
*pVtab
;
8122 const sqlite3_module
*pModule
;
8124 sqlite3_context sContext
;
8126 VdbeCursor
*pCur
= p
->apCsr
[pOp
->p1
];
8128 assert( pOp
->p3
>0 && pOp
->p3
<=(p
->nMem
+1 - p
->nCursor
) );
8129 pDest
= &aMem
[pOp
->p3
];
8130 memAboutToChange(p
, pDest
);
8131 if( pCur
->nullRow
){
8132 sqlite3VdbeMemSetNull(pDest
);
8135 assert( pCur
->eCurType
==CURTYPE_VTAB
);
8136 pVtab
= pCur
->uc
.pVCur
->pVtab
;
8137 pModule
= pVtab
->pModule
;
8138 assert( pModule
->xColumn
);
8139 memset(&sContext
, 0, sizeof(sContext
));
8140 sContext
.pOut
= pDest
;
8141 sContext
.enc
= encoding
;
8142 assert( pOp
->p5
==OPFLAG_NOCHNG
|| pOp
->p5
==0 );
8143 if( pOp
->p5
& OPFLAG_NOCHNG
){
8144 sqlite3VdbeMemSetNull(pDest
);
8145 pDest
->flags
= MEM_Null
|MEM_Zero
;
8148 MemSetTypeFlag(pDest
, MEM_Null
);
8150 rc
= pModule
->xColumn(pCur
->uc
.pVCur
, &sContext
, pOp
->p2
);
8151 sqlite3VtabImportErrmsg(p
, pVtab
);
8152 if( sContext
.isError
>0 ){
8153 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pDest
));
8154 rc
= sContext
.isError
;
8156 sqlite3VdbeChangeEncoding(pDest
, encoding
);
8157 REGISTER_TRACE(pOp
->p3
, pDest
);
8158 UPDATE_MAX_BLOBSIZE(pDest
);
8160 if( rc
) goto abort_due_to_error
;
8163 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8165 #ifndef SQLITE_OMIT_VIRTUALTABLE
8166 /* Opcode: VNext P1 P2 * * *
8168 ** Advance virtual table P1 to the next row in its result set and
8169 ** jump to instruction P2. Or, if the virtual table has reached
8170 ** the end of its result set, then fall through to the next instruction.
8172 case OP_VNext
: { /* jump, ncycle */
8173 sqlite3_vtab
*pVtab
;
8174 const sqlite3_module
*pModule
;
8178 pCur
= p
->apCsr
[pOp
->p1
];
8180 assert( pCur
->eCurType
==CURTYPE_VTAB
);
8181 if( pCur
->nullRow
){
8184 pVtab
= pCur
->uc
.pVCur
->pVtab
;
8185 pModule
= pVtab
->pModule
;
8186 assert( pModule
->xNext
);
8188 /* Invoke the xNext() method of the module. There is no way for the
8189 ** underlying implementation to return an error if one occurs during
8190 ** xNext(). Instead, if an error occurs, true is returned (indicating that
8191 ** data is available) and the error code returned when xColumn or
8192 ** some other method is next invoked on the save virtual table cursor.
8194 rc
= pModule
->xNext(pCur
->uc
.pVCur
);
8195 sqlite3VtabImportErrmsg(p
, pVtab
);
8196 if( rc
) goto abort_due_to_error
;
8197 res
= pModule
->xEof(pCur
->uc
.pVCur
);
8198 VdbeBranchTaken(!res
,2);
8200 /* If there is data, jump to P2 */
8201 goto jump_to_p2_and_check_for_interrupt
;
8203 goto check_for_interrupt
;
8205 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8207 #ifndef SQLITE_OMIT_VIRTUALTABLE
8208 /* Opcode: VRename P1 * * P4 *
8210 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
8211 ** This opcode invokes the corresponding xRename method. The value
8212 ** in register P1 is passed as the zName argument to the xRename method.
8215 sqlite3_vtab
*pVtab
;
8219 isLegacy
= (db
->flags
& SQLITE_LegacyAlter
);
8220 db
->flags
|= SQLITE_LegacyAlter
;
8221 pVtab
= pOp
->p4
.pVtab
->pVtab
;
8222 pName
= &aMem
[pOp
->p1
];
8223 assert( pVtab
->pModule
->xRename
);
8224 assert( memIsValid(pName
) );
8225 assert( p
->readOnly
==0 );
8226 REGISTER_TRACE(pOp
->p1
, pName
);
8227 assert( pName
->flags
& MEM_Str
);
8228 testcase( pName
->enc
==SQLITE_UTF8
);
8229 testcase( pName
->enc
==SQLITE_UTF16BE
);
8230 testcase( pName
->enc
==SQLITE_UTF16LE
);
8231 rc
= sqlite3VdbeChangeEncoding(pName
, SQLITE_UTF8
);
8232 if( rc
) goto abort_due_to_error
;
8233 rc
= pVtab
->pModule
->xRename(pVtab
, pName
->z
);
8234 if( isLegacy
==0 ) db
->flags
&= ~(u64
)SQLITE_LegacyAlter
;
8235 sqlite3VtabImportErrmsg(p
, pVtab
);
8237 if( rc
) goto abort_due_to_error
;
8242 #ifndef SQLITE_OMIT_VIRTUALTABLE
8243 /* Opcode: VUpdate P1 P2 P3 P4 P5
8244 ** Synopsis: data=r[P3@P2]
8246 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
8247 ** This opcode invokes the corresponding xUpdate method. P2 values
8248 ** are contiguous memory cells starting at P3 to pass to the xUpdate
8249 ** invocation. The value in register (P3+P2-1) corresponds to the
8250 ** p2th element of the argv array passed to xUpdate.
8252 ** The xUpdate method will do a DELETE or an INSERT or both.
8253 ** The argv[0] element (which corresponds to memory cell P3)
8254 ** is the rowid of a row to delete. If argv[0] is NULL then no
8255 ** deletion occurs. The argv[1] element is the rowid of the new
8256 ** row. This can be NULL to have the virtual table select the new
8257 ** rowid for itself. The subsequent elements in the array are
8258 ** the values of columns in the new row.
8260 ** If P2==1 then no insert is performed. argv[0] is the rowid of
8263 ** P1 is a boolean flag. If it is set to true and the xUpdate call
8264 ** is successful, then the value returned by sqlite3_last_insert_rowid()
8265 ** is set to the value of the rowid for the row just inserted.
8267 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
8268 ** apply in the case of a constraint failure on an insert or update.
8271 sqlite3_vtab
*pVtab
;
8272 const sqlite3_module
*pModule
;
8275 sqlite_int64 rowid
= 0;
8279 assert( pOp
->p2
==1 || pOp
->p5
==OE_Fail
|| pOp
->p5
==OE_Rollback
8280 || pOp
->p5
==OE_Abort
|| pOp
->p5
==OE_Ignore
|| pOp
->p5
==OE_Replace
8282 assert( p
->readOnly
==0 );
8283 if( db
->mallocFailed
) goto no_mem
;
8284 sqlite3VdbeIncrWriteCounter(p
, 0);
8285 pVtab
= pOp
->p4
.pVtab
->pVtab
;
8286 if( pVtab
==0 || NEVER(pVtab
->pModule
==0) ){
8288 goto abort_due_to_error
;
8290 pModule
= pVtab
->pModule
;
8292 assert( pOp
->p4type
==P4_VTAB
);
8293 if( ALWAYS(pModule
->xUpdate
) ){
8294 u8 vtabOnConflict
= db
->vtabOnConflict
;
8296 pX
= &aMem
[pOp
->p3
];
8297 for(i
=0; i
<nArg
; i
++){
8298 assert( memIsValid(pX
) );
8299 memAboutToChange(p
, pX
);
8303 db
->vtabOnConflict
= pOp
->p5
;
8304 rc
= pModule
->xUpdate(pVtab
, nArg
, apArg
, &rowid
);
8305 db
->vtabOnConflict
= vtabOnConflict
;
8306 sqlite3VtabImportErrmsg(p
, pVtab
);
8307 if( rc
==SQLITE_OK
&& pOp
->p1
){
8308 assert( nArg
>1 && apArg
[0] && (apArg
[0]->flags
&MEM_Null
) );
8309 db
->lastRowid
= rowid
;
8311 if( (rc
&0xff)==SQLITE_CONSTRAINT
&& pOp
->p4
.pVtab
->bConstraint
){
8312 if( pOp
->p5
==OE_Ignore
){
8315 p
->errorAction
= ((pOp
->p5
==OE_Replace
) ? OE_Abort
: pOp
->p5
);
8320 if( rc
) goto abort_due_to_error
;
8324 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8326 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
8327 /* Opcode: Pagecount P1 P2 * * *
8329 ** Write the current number of pages in database P1 to memory cell P2.
8331 case OP_Pagecount
: { /* out2 */
8332 pOut
= out2Prerelease(p
, pOp
);
8333 pOut
->u
.i
= sqlite3BtreeLastPage(db
->aDb
[pOp
->p1
].pBt
);
8339 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
8340 /* Opcode: MaxPgcnt P1 P2 P3 * *
8342 ** Try to set the maximum page count for database P1 to the value in P3.
8343 ** Do not let the maximum page count fall below the current page count and
8344 ** do not change the maximum page count value if P3==0.
8346 ** Store the maximum page count after the change in register P2.
8348 case OP_MaxPgcnt
: { /* out2 */
8349 unsigned int newMax
;
8352 pOut
= out2Prerelease(p
, pOp
);
8353 pBt
= db
->aDb
[pOp
->p1
].pBt
;
8356 newMax
= sqlite3BtreeLastPage(pBt
);
8357 if( newMax
< (unsigned)pOp
->p3
) newMax
= (unsigned)pOp
->p3
;
8359 pOut
->u
.i
= sqlite3BtreeMaxPageCount(pBt
, newMax
);
8364 /* Opcode: Function P1 P2 P3 P4 *
8365 ** Synopsis: r[P3]=func(r[P2@NP])
8367 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
8368 ** contains a pointer to the function to be run) with arguments taken
8369 ** from register P2 and successors. The number of arguments is in
8370 ** the sqlite3_context object that P4 points to.
8371 ** The result of the function is stored
8372 ** in register P3. Register P3 must not be one of the function inputs.
8374 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
8375 ** function was determined to be constant at compile time. If the first
8376 ** argument was constant then bit 0 of P1 is set. This is used to determine
8377 ** whether meta data associated with a user function argument using the
8378 ** sqlite3_set_auxdata() API may be safely retained until the next
8379 ** invocation of this opcode.
8381 ** See also: AggStep, AggFinal, PureFunc
8383 /* Opcode: PureFunc P1 P2 P3 P4 *
8384 ** Synopsis: r[P3]=func(r[P2@NP])
8386 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
8387 ** contains a pointer to the function to be run) with arguments taken
8388 ** from register P2 and successors. The number of arguments is in
8389 ** the sqlite3_context object that P4 points to.
8390 ** The result of the function is stored
8391 ** in register P3. Register P3 must not be one of the function inputs.
8393 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
8394 ** function was determined to be constant at compile time. If the first
8395 ** argument was constant then bit 0 of P1 is set. This is used to determine
8396 ** whether meta data associated with a user function argument using the
8397 ** sqlite3_set_auxdata() API may be safely retained until the next
8398 ** invocation of this opcode.
8400 ** This opcode works exactly like OP_Function. The only difference is in
8401 ** its name. This opcode is used in places where the function must be
8402 ** purely non-deterministic. Some built-in date/time functions can be
8403 ** either determinitic of non-deterministic, depending on their arguments.
8404 ** When those function are used in a non-deterministic way, they will check
8405 ** to see if they were called using OP_PureFunc instead of OP_Function, and
8406 ** if they were, they throw an error.
8408 ** See also: AggStep, AggFinal, Function
8410 case OP_PureFunc
: /* group */
8411 case OP_Function
: { /* group */
8413 sqlite3_context
*pCtx
;
8415 assert( pOp
->p4type
==P4_FUNCCTX
);
8416 pCtx
= pOp
->p4
.pCtx
;
8418 /* If this function is inside of a trigger, the register array in aMem[]
8419 ** might change from one evaluation to the next. The next block of code
8420 ** checks to see if the register array has changed, and if so it
8421 ** reinitializes the relavant parts of the sqlite3_context object */
8422 pOut
= &aMem
[pOp
->p3
];
8423 if( pCtx
->pOut
!= pOut
){
8426 pCtx
->enc
= encoding
;
8427 for(i
=pCtx
->argc
-1; i
>=0; i
--) pCtx
->argv
[i
] = &aMem
[pOp
->p2
+i
];
8429 assert( pCtx
->pVdbe
==p
);
8431 memAboutToChange(p
, pOut
);
8433 for(i
=0; i
<pCtx
->argc
; i
++){
8434 assert( memIsValid(pCtx
->argv
[i
]) );
8435 REGISTER_TRACE(pOp
->p2
+i
, pCtx
->argv
[i
]);
8438 MemSetTypeFlag(pOut
, MEM_Null
);
8439 assert( pCtx
->isError
==0 );
8440 (*pCtx
->pFunc
->xSFunc
)(pCtx
, pCtx
->argc
, pCtx
->argv
);/* IMP: R-24505-23230 */
8442 /* If the function returned an error, throw an exception */
8443 if( pCtx
->isError
){
8444 if( pCtx
->isError
>0 ){
8445 sqlite3VdbeError(p
, "%s", sqlite3_value_text(pOut
));
8448 sqlite3VdbeDeleteAuxData(db
, &p
->pAuxData
, pCtx
->iOp
, pOp
->p1
);
8450 if( rc
) goto abort_due_to_error
;
8453 assert( (pOut
->flags
&MEM_Str
)==0
8454 || pOut
->enc
==encoding
8455 || db
->mallocFailed
);
8456 assert( !sqlite3VdbeMemTooBig(pOut
) );
8458 REGISTER_TRACE(pOp
->p3
, pOut
);
8459 UPDATE_MAX_BLOBSIZE(pOut
);
8463 /* Opcode: ClrSubtype P1 * * * *
8464 ** Synopsis: r[P1].subtype = 0
8466 ** Clear the subtype from register P1.
8468 case OP_ClrSubtype
: { /* in1 */
8469 pIn1
= &aMem
[pOp
->p1
];
8470 pIn1
->flags
&= ~MEM_Subtype
;
8474 /* Opcode: FilterAdd P1 * P3 P4 *
8475 ** Synopsis: filter(P1) += key(P3@P4)
8477 ** Compute a hash on the P4 registers starting with r[P3] and
8478 ** add that hash to the bloom filter contained in r[P1].
8480 case OP_FilterAdd
: {
8483 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
8484 pIn1
= &aMem
[pOp
->p1
];
8485 assert( pIn1
->flags
& MEM_Blob
);
8486 assert( pIn1
->n
>0 );
8487 h
= filterHash(aMem
, pOp
);
8489 if( db
->flags
&SQLITE_VdbeTrace
){
8491 for(ii
=pOp
->p3
; ii
<pOp
->p3
+pOp
->p4
.i
; ii
++){
8492 registerTrace(ii
, &aMem
[ii
]);
8494 printf("hash: %llu modulo %d -> %u\n", h
, pIn1
->n
, (int)(h
%pIn1
->n
));
8498 pIn1
->z
[h
/8] |= 1<<(h
&7);
8502 /* Opcode: Filter P1 P2 P3 P4 *
8503 ** Synopsis: if key(P3@P4) not in filter(P1) goto P2
8505 ** Compute a hash on the key contained in the P4 registers starting
8506 ** with r[P3]. Check to see if that hash is found in the
8507 ** bloom filter hosted by register P1. If it is not present then
8508 ** maybe jump to P2. Otherwise fall through.
8510 ** False negatives are harmless. It is always safe to fall through,
8511 ** even if the value is in the bloom filter. A false negative causes
8512 ** more CPU cycles to be used, but it should still yield the correct
8513 ** answer. However, an incorrect answer may well arise from a
8514 ** false positive - if the jump is taken when it should fall through.
8516 case OP_Filter
: { /* jump */
8519 assert( pOp
->p1
>0 && pOp
->p1
<=(p
->nMem
+1 - p
->nCursor
) );
8520 pIn1
= &aMem
[pOp
->p1
];
8521 assert( (pIn1
->flags
& MEM_Blob
)!=0 );
8522 assert( pIn1
->n
>= 1 );
8523 h
= filterHash(aMem
, pOp
);
8525 if( db
->flags
&SQLITE_VdbeTrace
){
8527 for(ii
=pOp
->p3
; ii
<pOp
->p3
+pOp
->p4
.i
; ii
++){
8528 registerTrace(ii
, &aMem
[ii
]);
8530 printf("hash: %llu modulo %d -> %u\n", h
, pIn1
->n
, (int)(h
%pIn1
->n
));
8534 if( (pIn1
->z
[h
/8] & (1<<(h
&7)))==0 ){
8535 VdbeBranchTaken(1, 2);
8536 p
->aCounter
[SQLITE_STMTSTATUS_FILTER_HIT
]++;
8539 p
->aCounter
[SQLITE_STMTSTATUS_FILTER_MISS
]++;
8540 VdbeBranchTaken(0, 2);
8545 /* Opcode: Trace P1 P2 * P4 *
8547 ** Write P4 on the statement trace output if statement tracing is
8550 ** Operand P1 must be 0x7fffffff and P2 must positive.
8552 /* Opcode: Init P1 P2 P3 P4 *
8553 ** Synopsis: Start at P2
8555 ** Programs contain a single instance of this opcode as the very first
8558 ** If tracing is enabled (by the sqlite3_trace()) interface, then
8559 ** the UTF-8 string contained in P4 is emitted on the trace callback.
8560 ** Or if P4 is blank, use the string returned by sqlite3_sql().
8562 ** If P2 is not zero, jump to instruction P2.
8564 ** Increment the value of P1 so that OP_Once opcodes will jump the
8565 ** first time they are evaluated for this run.
8567 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
8568 ** error is encountered.
8571 case OP_Init
: { /* jump */
8573 #ifndef SQLITE_OMIT_TRACE
8577 /* If the P4 argument is not NULL, then it must be an SQL comment string.
8578 ** The "--" string is broken up to prevent false-positives with srcck1.c.
8580 ** This assert() provides evidence for:
8581 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
8582 ** would have been returned by the legacy sqlite3_trace() interface by
8583 ** using the X argument when X begins with "--" and invoking
8584 ** sqlite3_expanded_sql(P) otherwise.
8586 assert( pOp
->p4
.z
==0 || strncmp(pOp
->p4
.z
, "-" "- ", 3)==0 );
8588 /* OP_Init is always instruction 0 */
8589 assert( pOp
==p
->aOp
|| pOp
->opcode
==OP_Trace
);
8591 #ifndef SQLITE_OMIT_TRACE
8592 if( (db
->mTrace
& (SQLITE_TRACE_STMT
|SQLITE_TRACE_LEGACY
))!=0
8593 && p
->minWriteFileFormat
!=254 /* tag-20220401a */
8594 && (zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
))!=0
8596 #ifndef SQLITE_OMIT_DEPRECATED
8597 if( db
->mTrace
& SQLITE_TRACE_LEGACY
){
8598 char *z
= sqlite3VdbeExpandSql(p
, zTrace
);
8599 db
->trace
.xLegacy(db
->pTraceArg
, z
);
8603 if( db
->nVdbeExec
>1 ){
8604 char *z
= sqlite3MPrintf(db
, "-- %s", zTrace
);
8605 (void)db
->trace
.xV2(SQLITE_TRACE_STMT
, db
->pTraceArg
, p
, z
);
8606 sqlite3DbFree(db
, z
);
8608 (void)db
->trace
.xV2(SQLITE_TRACE_STMT
, db
->pTraceArg
, p
, zTrace
);
8611 #ifdef SQLITE_USE_FCNTL_TRACE
8612 zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
);
8615 for(j
=0; j
<db
->nDb
; j
++){
8616 if( DbMaskTest(p
->btreeMask
, j
)==0 ) continue;
8617 sqlite3_file_control(db
, db
->aDb
[j
].zDbSName
, SQLITE_FCNTL_TRACE
, zTrace
);
8620 #endif /* SQLITE_USE_FCNTL_TRACE */
8622 if( (db
->flags
& SQLITE_SqlTrace
)!=0
8623 && (zTrace
= (pOp
->p4
.z
? pOp
->p4
.z
: p
->zSql
))!=0
8625 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace
);
8627 #endif /* SQLITE_DEBUG */
8628 #endif /* SQLITE_OMIT_TRACE */
8629 assert( pOp
->p2
>0 );
8630 if( pOp
->p1
>=sqlite3GlobalConfig
.iOnceResetThreshold
){
8631 if( pOp
->opcode
==OP_Trace
) break;
8632 for(i
=1; i
<p
->nOp
; i
++){
8633 if( p
->aOp
[i
].opcode
==OP_Once
) p
->aOp
[i
].p1
= 0;
8638 p
->aCounter
[SQLITE_STMTSTATUS_RUN
]++;
8642 #ifdef SQLITE_ENABLE_CURSOR_HINTS
8643 /* Opcode: CursorHint P1 * * P4 *
8645 ** Provide a hint to cursor P1 that it only needs to return rows that
8646 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8647 ** to values currently held in registers. TK_COLUMN terms in the P4
8648 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
8650 case OP_CursorHint
: {
8653 assert( pOp
->p1
>=0 && pOp
->p1
<p
->nCursor
);
8654 assert( pOp
->p4type
==P4_EXPR
);
8655 pC
= p
->apCsr
[pOp
->p1
];
8657 assert( pC
->eCurType
==CURTYPE_BTREE
);
8658 sqlite3BtreeCursorHint(pC
->uc
.pCursor
, BTREE_HINT_RANGE
,
8659 pOp
->p4
.pExpr
, aMem
);
8663 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
8666 /* Opcode: Abortable * * * * *
8668 ** Verify that an Abort can happen. Assert if an Abort at this point
8669 ** might cause database corruption. This opcode only appears in debugging
8672 ** An Abort is safe if either there have been no writes, or if there is
8673 ** an active statement journal.
8675 case OP_Abortable
: {
8676 sqlite3VdbeAssertAbortable(p
);
8682 /* Opcode: ReleaseReg P1 P2 P3 * P5
8683 ** Synopsis: release r[P1@P2] mask P3
8685 ** Release registers from service. Any content that was in the
8686 ** the registers is unreliable after this opcode completes.
8688 ** The registers released will be the P2 registers starting at P1,
8689 ** except if bit ii of P3 set, then do not release register P1+ii.
8690 ** In other words, P3 is a mask of registers to preserve.
8692 ** Releasing a register clears the Mem.pScopyFrom pointer. That means
8693 ** that if the content of the released register was set using OP_SCopy,
8694 ** a change to the value of the source register for the OP_SCopy will no longer
8695 ** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8697 ** If P5 is set, then all released registers have their type set
8698 ** to MEM_Undefined so that any subsequent attempt to read the released
8699 ** register (before it is reinitialized) will generate an assertion fault.
8701 ** P5 ought to be set on every call to this opcode.
8702 ** However, there are places in the code generator will release registers
8703 ** before their are used, under the (valid) assumption that the registers
8704 ** will not be reallocated for some other purpose before they are used and
8705 ** hence are safe to release.
8707 ** This opcode is only available in testing and debugging builds. It is
8708 ** not generated for release builds. The purpose of this opcode is to help
8709 ** validate the generated bytecode. This opcode does not actually contribute
8710 ** to computing an answer.
8712 case OP_ReleaseReg
: {
8716 assert( pOp
->p1
>0 );
8717 assert( pOp
->p1
+pOp
->p2
<=(p
->nMem
+1 - p
->nCursor
)+1 );
8718 pMem
= &aMem
[pOp
->p1
];
8719 constMask
= pOp
->p3
;
8720 for(i
=0; i
<pOp
->p2
; i
++, pMem
++){
8721 if( i
>=32 || (constMask
& MASKBIT32(i
))==0 ){
8722 pMem
->pScopyFrom
= 0;
8723 if( i
<32 && pOp
->p5
) MemSetTypeFlag(pMem
, MEM_Undefined
);
8730 /* Opcode: Noop * * * * *
8732 ** Do nothing. This instruction is often useful as a jump
8736 ** The magic Explain opcode are only inserted when explain==2 (which
8737 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8738 ** This opcode records information from the optimizer. It is the
8739 ** the same as a no-op. This opcodesnever appears in a real VM program.
8741 default: { /* This is really OP_Noop, OP_Explain */
8742 assert( pOp
->opcode
==OP_Noop
|| pOp
->opcode
==OP_Explain
);
8747 /*****************************************************************************
8748 ** The cases of the switch statement above this line should all be indented
8749 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8750 ** readability. From this point on down, the normal indentation rules are
8752 *****************************************************************************/
8755 #if defined(VDBE_PROFILE)
8756 *pnCycle
+= sqlite3NProfileCnt
? sqlite3NProfileCnt
: sqlite3Hwtime();
8758 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
8759 *pnCycle
+= sqlite3Hwtime();
8763 /* The following code adds nothing to the actual functionality
8764 ** of the program. It is only here for testing and debugging.
8765 ** On the other hand, it does burn CPU cycles every time through
8766 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8769 assert( pOp
>=&aOp
[-1] && pOp
<&aOp
[p
->nOp
-1] );
8772 if( db
->flags
& SQLITE_VdbeTrace
){
8773 u8 opProperty
= sqlite3OpcodeProperty
[pOrigOp
->opcode
];
8774 if( rc
!=0 ) printf("rc=%d\n",rc
);
8775 if( opProperty
& (OPFLG_OUT2
) ){
8776 registerTrace(pOrigOp
->p2
, &aMem
[pOrigOp
->p2
]);
8778 if( opProperty
& OPFLG_OUT3
){
8779 registerTrace(pOrigOp
->p3
, &aMem
[pOrigOp
->p3
]);
8781 if( opProperty
==0xff ){
8782 /* Never happens. This code exists to avoid a harmless linkage
8783 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8785 sqlite3VdbeRegisterDump(p
);
8788 #endif /* SQLITE_DEBUG */
8790 } /* The end of the for(;;) loop the loops through opcodes */
8792 /* If we reach this point, it means that execution is finished with
8793 ** an error of some kind.
8796 if( db
->mallocFailed
){
8797 rc
= SQLITE_NOMEM_BKPT
;
8798 }else if( rc
==SQLITE_IOERR_CORRUPTFS
){
8799 rc
= SQLITE_CORRUPT_BKPT
;
8803 if( db
->flags
& SQLITE_VdbeTrace
){
8804 const char *zTrace
= p
->zSql
;
8806 if( aOp
[0].opcode
==OP_Trace
){
8807 zTrace
= aOp
[0].p4
.z
;
8809 if( zTrace
==0 ) zTrace
= "???";
8811 printf("ABORT-due-to-error (rc=%d): %s\n", rc
, zTrace
);
8814 if( p
->zErrMsg
==0 && rc
!=SQLITE_IOERR_NOMEM
){
8815 sqlite3VdbeError(p
, "%s", sqlite3ErrStr(rc
));
8818 sqlite3SystemError(db
, rc
);
8819 testcase( sqlite3GlobalConfig
.xLog
!=0 );
8820 sqlite3_log(rc
, "statement aborts at %d: [%s] %s",
8821 (int)(pOp
- aOp
), p
->zSql
, p
->zErrMsg
);
8822 if( p
->eVdbeState
==VDBE_RUN_STATE
) sqlite3VdbeHalt(p
);
8823 if( rc
==SQLITE_IOERR_NOMEM
) sqlite3OomFault(db
);
8824 if( rc
==SQLITE_CORRUPT
&& db
->autoCommit
==0 ){
8825 db
->flags
|= SQLITE_CorruptRdOnly
;
8828 if( resetSchemaOnFault
>0 ){
8829 sqlite3ResetOneSchema(db
, resetSchemaOnFault
-1);
8832 /* This is the only way out of this procedure. We have to
8833 ** release the mutexes on btrees that were acquired at the
8836 #if defined(VDBE_PROFILE)
8838 *pnCycle
+= sqlite3NProfileCnt
? sqlite3NProfileCnt
: sqlite3Hwtime();
8841 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
8843 *pnCycle
+= sqlite3Hwtime();
8848 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8849 while( nVmStep
>=nProgressLimit
&& db
->xProgress
!=0 ){
8850 nProgressLimit
+= db
->nProgressOps
;
8851 if( db
->xProgress(db
->pProgressArg
) ){
8852 nProgressLimit
= LARGEST_UINT64
;
8853 rc
= SQLITE_INTERRUPT
;
8854 goto abort_due_to_error
;
8858 p
->aCounter
[SQLITE_STMTSTATUS_VM_STEP
] += (int)nVmStep
;
8859 if( DbMaskNonZero(p
->lockMask
) ){
8860 sqlite3VdbeLeave(p
);
8862 assert( rc
!=SQLITE_OK
|| nExtraDelete
==0
8863 || sqlite3_strlike("DELETE%",p
->zSql
,0)!=0
8867 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8871 sqlite3VdbeError(p
, "string or blob too big");
8873 goto abort_due_to_error
;
8875 /* Jump to here if a malloc() fails.
8878 sqlite3OomFault(db
);
8879 sqlite3VdbeError(p
, "out of memory");
8880 rc
= SQLITE_NOMEM_BKPT
;
8881 goto abort_due_to_error
;
8883 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
8886 abort_due_to_interrupt
:
8887 assert( AtomicLoad(&db
->u1
.isInterrupted
) );
8888 rc
= SQLITE_INTERRUPT
;
8889 goto abort_due_to_error
;