blob: 0be31b95b713d1df4e6a0dedf30ef72396446bc9 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drh0fd61352014-02-07 02:29:45 +000012** The code in this file implements the function that runs the
13** bytecode of a prepared statement.
drh75897232000-05-29 14:26:00 +000014**
drhac82fcf2002-09-08 17:23:41 +000015** 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.
drh75897232000-05-29 14:26:00 +000020*/
21#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000022#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000023
24/*
drh2b4ded92010-09-27 21:09:31 +000025** Invoke this macro on memory cells just prior to changing the
26** value of the cell. This macro verifies that shallow copies are
drh0fd61352014-02-07 02:29:45 +000027** 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
drhb6e8fd12014-03-06 01:56:33 +000031** like that ever happens.
drh2b4ded92010-09-27 21:09:31 +000032*/
33#ifdef SQLITE_DEBUG
drhe4c88c02012-01-04 12:57:45 +000034# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
drh2b4ded92010-09-27 21:09:31 +000035#else
36# define memAboutToChange(P,M)
37#endif
38
39/*
drh487ab3c2001-11-08 00:45:21 +000040** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000041** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000042** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000043** working correctly. This variable has no function other than to
44** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000045*/
drh0f7eb612006-08-08 13:51:43 +000046#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000047int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000048#endif
drh487ab3c2001-11-08 00:45:21 +000049
drhf6038712004-02-08 18:07:34 +000050/*
51** When this global variable is positive, it gets decremented once before
drhe4c88c02012-01-04 12:57:45 +000052** 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.
drhf6038712004-02-08 18:07:34 +000054**
55** This facility is used for testing purposes only. It does not function
56** in an ordinary build.
57*/
drh0f7eb612006-08-08 13:51:43 +000058#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000060#endif
drh1350b032002-02-27 19:00:20 +000061
danielk19777e18c252004-05-25 11:47:24 +000062/*
drh6bf89572004-11-03 16:27:01 +000063** 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
shane21e7feb2008-05-30 15:59:49 +000065** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000066** has no function other than to help verify the correct operation of the
67** library.
68*/
drh0f7eb612006-08-08 13:51:43 +000069#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000070int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000071#endif
drh6bf89572004-11-03 16:27:01 +000072
73/*
drhae7e1512007-05-02 16:51:59 +000074** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000075** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000076** 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.
79*/
80#ifdef SQLITE_TEST
81int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000082static void updateMaxBlobsize(Mem *p){
83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
84 sqlite3_max_blobsize = p->n;
85 }
86}
drhae7e1512007-05-02 16:51:59 +000087#endif
88
89/*
drh9b1c62d2011-03-30 21:04:43 +000090** This macro evaluates to true if either the update hook or the preupdate
91** hook are enabled for database connect DB.
92*/
93#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh74c33022016-03-30 12:56:55 +000094# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000095#else
drh74c33022016-03-30 12:56:55 +000096# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000097#endif
98
99/*
drh0fd61352014-02-07 02:29:45 +0000100** The next global variable is incremented each time the OP_Found opcode
dan0ff297e2009-09-25 17:03:14 +0000101** 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
104** library.
105*/
106#ifdef SQLITE_TEST
107int sqlite3_found_count = 0;
108#endif
109
110/*
drhb7654112008-01-12 12:48:07 +0000111** Test a register to see if it exceeds the current maximum blob size.
112** If it does, record the new maximum blob size.
113*/
drhd12602a2016-12-07 15:49:02 +0000114#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
drhca48c902008-01-18 14:08:24 +0000115# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000116#else
117# define UPDATE_MAX_BLOBSIZE(P)
118#endif
119
drh52f11b82020-01-02 13:26:49 +0000120#ifdef SQLITE_DEBUG
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:
125**
126** break test_trace_breakpoint if pc=22
127**
128** Other useful labels for breakpoints include:
129** test_addop_breakpoint(pc,pOp)
130** sqlite3CorruptError(lineno)
131** sqlite3MisuseError(lineno)
132** sqlite3CantopenError(lineno)
133*/
drh22e95fb2020-01-02 14:42:42 +0000134static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
drh52f11b82020-01-02 13:26:49 +0000135 static int n = 0;
136 n++;
137}
138#endif
139
drhb7654112008-01-12 12:48:07 +0000140/*
drh5655c542014-02-19 19:14:34 +0000141** Invoke the VDBE coverage callback, if that callback is defined. This
142** feature is used for test suite validation only and does not appear an
143** production builds.
144**
drhc9065332019-04-01 14:01:21 +0000145** M is the type of branch. I is the direction taken for this instance of
146** the branch.
147**
148** M: 2 - two-way branch (I=0: fall-thru 1: jump )
149** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
150** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
151**
152** In other words, if M is 2, then I is either 0 (for fall-through) or
153** 1 (for when the branch is taken). If M is 3, the I is 0 for an
154** ordinary fall-through, I is 1 if the branch was taken, and I is 2
155** if the result of comparison is NULL. For M=3, I=2 the jump may or
156** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
157** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
158** depending on if the operands are less than, equal, or greater than.
drh4336b0e2014-08-05 00:53:51 +0000159**
160** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000161** generated the VDBE instruction combined with flag bits. The source
162** code line number is in the lower 24 bits of iSrcLine and the upper
163** 8 bytes are flags. The lower three bits of the flags indicate
164** values for I that should never occur. For example, if the branch is
165** always taken, the flags should be 0x05 since the fall-through and
166** alternate branch are never taken. If a branch is never taken then
167** flags should be 0x06 since only the fall-through approach is allowed.
168**
drhc9065332019-04-01 14:01:21 +0000169** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
drh7083a482018-07-10 16:04:04 +0000170** interested in equal or not-equal. In other words, I==0 and I==2
drhc9065332019-04-01 14:01:21 +0000171** should be treated as equivalent
drh7083a482018-07-10 16:04:04 +0000172**
173** Since only a line number is retained, not the filename, this macro
174** only works for amalgamation builds. But that is ok, since these macros
175** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000176*/
177#if !defined(SQLITE_VDBE_COVERAGE)
178# define VdbeBranchTaken(I,M)
179#else
drh5655c542014-02-19 19:14:34 +0000180# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000181 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
182 u8 mNever;
183 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
184 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
185 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
186 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
187 I = 1<<I;
188 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
189 ** the flags indicate directions that the branch can never go. If
190 ** a branch really does go in one of those directions, assert right
191 ** away. */
192 mNever = iSrcLine >> 24;
193 assert( (I & mNever)==0 );
194 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
drhc9065332019-04-01 14:01:21 +0000195 /* Invoke the branch coverage callback with three arguments:
196 ** iSrcLine - the line number of the VdbeCoverage() macro, with
197 ** flags removed.
198 ** I - Mask of bits 0x07 indicating which cases are are
199 ** fulfilled by this instance of the jump. 0x01 means
200 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
201 ** impossible cases (ex: if the comparison is never NULL)
202 ** are filled in automatically so that the coverage
203 ** measurement logic does not flag those impossible cases
204 ** as missed coverage.
205 ** M - Type of jump. Same as M argument above
206 */
drh7083a482018-07-10 16:04:04 +0000207 I |= mNever;
208 if( M==2 ) I |= 0x04;
209 if( M==4 ){
210 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000211 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000212 }
drh7083a482018-07-10 16:04:04 +0000213 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
214 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000215 }
drh688852a2014-02-17 22:40:43 +0000216#endif
217
218/*
danielk1977bd7e4602004-05-24 07:34:48 +0000219** An ephemeral string value (signified by the MEM_Ephem flag) contains
220** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000221** is responsible for deallocating that string. Because the register
222** does not control the string, it might be deleted without the register
223** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000224**
225** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000226** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000227** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000228*/
drhb21c8cd2007-08-21 19:33:56 +0000229#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000230 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000231 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000232
dan689ab892011-08-12 15:02:00 +0000233/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000234#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000235
236/*
drhdfe88ec2008-11-03 20:55:06 +0000237** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000238** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000239*/
drhdfe88ec2008-11-03 20:55:06 +0000240static VdbeCursor *allocateCursor(
241 Vdbe *p, /* The virtual machine */
242 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000243 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000244 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000245 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000246){
247 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000248 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000249 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000250 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000251 **
252 ** * Sometimes cursor numbers are used for a couple of different
253 ** purposes in a vdbe program. The different uses might require
254 ** different sized allocations. Memory cells provide growable
255 ** allocations.
256 **
257 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
258 ** be freed lazily via the sqlite3_release_memory() API. This
259 ** minimizes the number of malloc calls made by the system.
260 **
drh3cdce922016-03-21 00:30:40 +0000261 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000262 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
263 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000264 */
drh9f6168b2016-03-19 23:32:58 +0000265 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000266
danielk19775f096132008-03-28 15:44:09 +0000267 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000268 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000269 nByte =
drh5cc10232013-11-21 01:04:02 +0000270 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000271 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000272
drh9f6168b2016-03-19 23:32:58 +0000273 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000274 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
dan97c8cb32019-01-01 18:00:17 +0000275 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
276 ** is clear. Otherwise, if this is an ephemeral cursor created by
277 ** OP_OpenDup, the cursor will not be closed and will still be part
278 ** of a BtShared.pCursor list. */
dana5129722019-05-03 18:50:24 +0000279 if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
danielk1977be718892006-06-23 08:05:19 +0000280 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000281 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000282 }
drh322f2852014-09-19 00:43:39 +0000283 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000284 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000285 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000286 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000287 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000288 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000289 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000290 if( eCurType==CURTYPE_BTREE ){
291 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000292 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000293 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000294 }
danielk197794eb6a12005-12-15 15:22:08 +0000295 }
drh4774b132004-06-12 20:12:51 +0000296 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000297}
298
danielk19773d1bfea2004-05-14 11:00:53 +0000299/*
drh8a3884e2019-05-29 21:18:27 +0000300** The string in pRec is known to look like an integer and to have a
301** floating point value of rValue. Return true and set *piValue to the
302** integer value if the string is in range to be an integer. Otherwise,
303** return false.
304*/
305static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
306 i64 iValue = (double)rValue;
307 if( sqlite3RealSameAsInt(rValue,iValue) ){
drhc285ded2019-06-10 18:33:16 +0000308 *piValue = iValue;
309 return 1;
drh8a3884e2019-05-29 21:18:27 +0000310 }
311 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
312}
313
314/*
drh29d72102006-02-09 22:13:41 +0000315** Try to convert a value into a numeric representation if we can
316** do so without loss of information. In other words, if the string
317** looks like a number, convert it into a number. If it does not
318** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000319**
320** If the bTryForInt flag is true, then extra effort is made to give
321** an integer representation. Strings that look like floating point
322** values but which have no fractional component (example: '48.00')
323** will have a MEM_Int representation when bTryForInt is true.
324**
325** If bTryForInt is false, then if the input string contains a decimal
326** point or exponential notation, the result is only MEM_Real, even
327** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000328*/
drhbd9507c2014-08-23 17:21:37 +0000329static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000330 double rValue;
drh975b4c62014-07-26 16:47:23 +0000331 u8 enc = pRec->enc;
drh8a3884e2019-05-29 21:18:27 +0000332 int rc;
drh169f0772019-05-02 21:36:26 +0000333 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
drh8a3884e2019-05-29 21:18:27 +0000334 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
drh9a278222019-06-07 22:26:08 +0000335 if( rc<=0 ) return;
drh8a3884e2019-05-29 21:18:27 +0000336 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
drh975b4c62014-07-26 16:47:23 +0000337 pRec->flags |= MEM_Int;
338 }else{
drh74eaba42014-09-18 17:52:15 +0000339 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000340 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000341 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000342 }
drh06b3bd52018-02-01 01:13:33 +0000343 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
344 ** string representation after computing a numeric equivalent, because the
345 ** string representation might not be the canonical representation for the
346 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
347 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000348}
349
350/*
drh8a512562005-11-14 22:29:05 +0000351** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000352**
drh8a512562005-11-14 22:29:05 +0000353** SQLITE_AFF_INTEGER:
354** SQLITE_AFF_REAL:
355** SQLITE_AFF_NUMERIC:
356** Try to convert pRec to an integer representation or a
357** floating-point representation if an integer representation
358** is not possible. Note that the integer representation is
359** always preferred, even if the affinity is REAL, because
360** an integer representation is more space efficient on disk.
361**
362** SQLITE_AFF_TEXT:
363** Convert pRec to a text representation.
364**
drh05883a32015-06-02 15:32:08 +0000365** SQLITE_AFF_BLOB:
drh96fb16e2019-08-06 14:37:24 +0000366** SQLITE_AFF_NONE:
drh8a512562005-11-14 22:29:05 +0000367** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000368*/
drh17435752007-08-16 04:30:38 +0000369static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000370 Mem *pRec, /* The value to apply affinity to */
371 char affinity, /* The affinity to be applied */
372 u8 enc /* Use this text encoding */
373){
drh7ea31cc2014-09-18 14:36:00 +0000374 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000375 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
376 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000377 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000378 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000379 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000380 }else{
381 sqlite3VdbeIntegerAffinity(pRec);
382 }
drh17c40292004-07-21 02:53:29 +0000383 }
drh7ea31cc2014-09-18 14:36:00 +0000384 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000385 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000386 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000387 ** representation. It would be harmless to repeat the conversion if
388 ** there is already a string rep, but it is pointless to waste those
389 ** CPU cycles. */
390 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
drh169f0772019-05-02 21:36:26 +0000391 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
drh3242c692019-05-04 01:29:13 +0000392 testcase( pRec->flags & MEM_Int );
393 testcase( pRec->flags & MEM_Real );
394 testcase( pRec->flags & MEM_IntReal );
drha3fa1402016-04-29 02:55:05 +0000395 sqlite3VdbeMemStringify(pRec, enc, 1);
396 }
danielk19773d1bfea2004-05-14 11:00:53 +0000397 }
drh169f0772019-05-02 21:36:26 +0000398 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
danielk19773d1bfea2004-05-14 11:00:53 +0000399 }
400}
401
danielk1977aee18ef2005-03-09 12:26:50 +0000402/*
drh29d72102006-02-09 22:13:41 +0000403** Try to convert the type of a function argument or a result column
404** into a numeric representation. Use either INTEGER or REAL whichever
405** is appropriate. But only do the conversion if it is possible without
406** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000407*/
408int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000409 int eType = sqlite3_value_type(pVal);
410 if( eType==SQLITE_TEXT ){
411 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000412 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000413 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000414 }
drh1b27b8c2014-02-10 03:21:57 +0000415 return eType;
drh29d72102006-02-09 22:13:41 +0000416}
417
418/*
danielk1977aee18ef2005-03-09 12:26:50 +0000419** Exported version of applyAffinity(). This one works on sqlite3_value*,
420** not the internal Mem* type.
421*/
danielk19771e536952007-08-16 10:09:01 +0000422void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000423 sqlite3_value *pVal,
424 u8 affinity,
425 u8 enc
426){
drhb21c8cd2007-08-21 19:33:56 +0000427 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000428}
429
drh3d1d90a2014-03-24 15:00:15 +0000430/*
drhf1a89ed2014-08-23 17:41:15 +0000431** pMem currently only holds a string type (or maybe a BLOB that we can
432** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000433** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000434** accordingly.
435*/
436static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
drh9a278222019-06-07 22:26:08 +0000437 int rc;
438 sqlite3_int64 ix;
drh169f0772019-05-02 21:36:26 +0000439 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
drhf1a89ed2014-08-23 17:41:15 +0000440 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh0814acd2019-01-25 20:09:04 +0000441 ExpandBlob(pMem);
drh9a278222019-06-07 22:26:08 +0000442 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
443 if( rc<=0 ){
444 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
445 pMem->u.i = ix;
446 return MEM_Int;
447 }else{
448 return MEM_Real;
449 }
450 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
451 pMem->u.i = ix;
drhf1a89ed2014-08-23 17:41:15 +0000452 return MEM_Int;
453 }
454 return MEM_Real;
455}
456
457/*
drh3d1d90a2014-03-24 15:00:15 +0000458** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
459** none.
460**
461** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000462** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000463*/
464static u16 numericType(Mem *pMem){
drh169f0772019-05-02 21:36:26 +0000465 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +0000466 testcase( pMem->flags & MEM_Int );
467 testcase( pMem->flags & MEM_Real );
468 testcase( pMem->flags & MEM_IntReal );
drh169f0772019-05-02 21:36:26 +0000469 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
drh3d1d90a2014-03-24 15:00:15 +0000470 }
471 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drh3242c692019-05-04 01:29:13 +0000472 testcase( pMem->flags & MEM_Str );
473 testcase( pMem->flags & MEM_Blob );
drhf1a89ed2014-08-23 17:41:15 +0000474 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000475 }
476 return 0;
477}
478
danielk1977b5402fb2005-01-12 07:15:04 +0000479#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000480/*
danielk1977ca6b2912004-05-21 10:49:47 +0000481** Write a nice string representation of the contents of cell pMem
482** into buffer zBuf, length nBuf.
483*/
drh5ca06322020-01-06 19:23:41 +0000484void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
danielk1977ca6b2912004-05-21 10:49:47 +0000485 int f = pMem->flags;
drh57196282004-10-06 15:41:16 +0000486 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977ca6b2912004-05-21 10:49:47 +0000487 if( f&MEM_Blob ){
488 int i;
489 char c;
490 if( f & MEM_Dyn ){
491 c = 'z';
492 assert( (f & (MEM_Static|MEM_Ephem))==0 );
493 }else if( f & MEM_Static ){
494 c = 't';
495 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
496 }else if( f & MEM_Ephem ){
497 c = 'e';
498 assert( (f & (MEM_Static|MEM_Dyn))==0 );
499 }else{
500 c = 's';
501 }
drhded33cc2020-01-08 11:36:30 +0000502 sqlite3_str_appendf(pStr, "%cx[", c);
drhefb5f9a2019-08-30 21:52:13 +0000503 for(i=0; i<25 && i<pMem->n; i++){
drh5ca06322020-01-06 19:23:41 +0000504 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
danielk1977ca6b2912004-05-21 10:49:47 +0000505 }
drh5ca06322020-01-06 19:23:41 +0000506 sqlite3_str_appendf(pStr, "|");
drhefb5f9a2019-08-30 21:52:13 +0000507 for(i=0; i<25 && i<pMem->n; i++){
danielk1977ca6b2912004-05-21 10:49:47 +0000508 char z = pMem->z[i];
drh5ca06322020-01-06 19:23:41 +0000509 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
danielk1977ca6b2912004-05-21 10:49:47 +0000510 }
drh5ca06322020-01-06 19:23:41 +0000511 sqlite3_str_appendf(pStr,"]");
drhfdf972a2007-05-02 13:30:27 +0000512 if( f & MEM_Zero ){
drh5ca06322020-01-06 19:23:41 +0000513 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
drhfdf972a2007-05-02 13:30:27 +0000514 }
danielk1977b1bc9532004-05-22 03:05:33 +0000515 }else if( f & MEM_Str ){
drh5ca06322020-01-06 19:23:41 +0000516 int j;
mistachkin59171172020-01-18 19:02:20 +0000517 u8 c;
danielk1977b1bc9532004-05-22 03:05:33 +0000518 if( f & MEM_Dyn ){
drh5ca06322020-01-06 19:23:41 +0000519 c = 'z';
danielk1977b1bc9532004-05-22 03:05:33 +0000520 assert( (f & (MEM_Static|MEM_Ephem))==0 );
521 }else if( f & MEM_Static ){
drh5ca06322020-01-06 19:23:41 +0000522 c = 't';
danielk1977b1bc9532004-05-22 03:05:33 +0000523 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
524 }else if( f & MEM_Ephem ){
drh5ca06322020-01-06 19:23:41 +0000525 c = 'e';
danielk1977b1bc9532004-05-22 03:05:33 +0000526 assert( (f & (MEM_Static|MEM_Dyn))==0 );
527 }else{
drh5ca06322020-01-06 19:23:41 +0000528 c = 's';
danielk1977b1bc9532004-05-22 03:05:33 +0000529 }
drh5ca06322020-01-06 19:23:41 +0000530 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
drhefb5f9a2019-08-30 21:52:13 +0000531 for(j=0; j<25 && j<pMem->n; j++){
mistachkin59171172020-01-18 19:02:20 +0000532 c = pMem->z[j];
drh5ca06322020-01-06 19:23:41 +0000533 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
danielk1977b1bc9532004-05-22 03:05:33 +0000534 }
drh5ca06322020-01-06 19:23:41 +0000535 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
danielk1977ca6b2912004-05-21 10:49:47 +0000536 }
danielk1977ca6b2912004-05-21 10:49:47 +0000537}
538#endif
539
drh5b6afba2008-01-05 16:29:28 +0000540#ifdef SQLITE_DEBUG
541/*
542** Print the value of a register for tracing purposes:
543*/
drh84e55a82013-11-13 17:58:23 +0000544static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000545 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000546 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000547 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000548 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000549 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000550 printf(" si:%lld", p->u.i);
drh169f0772019-05-02 21:36:26 +0000551 }else if( (p->flags & (MEM_IntReal))!=0 ){
drh83a1daf2019-05-01 18:59:33 +0000552 printf(" ir:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000553 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000554 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000555#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000556 }else if( p->flags & MEM_Real ){
drhd1c472d2019-10-03 14:51:59 +0000557 printf(" r:%.17g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000558#endif
drh9d67afc2018-08-29 20:24:03 +0000559 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000560 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000561 }else{
drh5ca06322020-01-06 19:23:41 +0000562 StrAccum acc;
563 char zBuf[1000];
564 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
565 sqlite3VdbeMemPrettyPrint(p, &acc);
566 printf(" %s", sqlite3StrAccumFinish(&acc));
drh5b6afba2008-01-05 16:29:28 +0000567 }
dan5b6c8e42016-01-30 15:46:03 +0000568 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000569}
drh84e55a82013-11-13 17:58:23 +0000570static void registerTrace(int iReg, Mem *p){
drh22e95fb2020-01-02 14:42:42 +0000571 printf("R[%d] = ", iReg);
drh84e55a82013-11-13 17:58:23 +0000572 memTracePrint(p);
drh22e95fb2020-01-02 14:42:42 +0000573 if( p->pScopyFrom ){
574 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
575 }
drh84e55a82013-11-13 17:58:23 +0000576 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000577 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000578}
579#endif
580
581#ifdef SQLITE_DEBUG
drh22e95fb2020-01-02 14:42:42 +0000582/*
583** Show the values of all registers in the virtual machine. Used for
584** interactive debugging.
585*/
586void sqlite3VdbeRegisterDump(Vdbe *v){
587 int i;
588 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
589}
590#endif /* SQLITE_DEBUG */
591
592
593#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000594# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000595#else
596# define REGISTER_TRACE(R,M)
597#endif
598
danielk197784ac9d02004-05-18 09:58:06 +0000599
drh7b396862003-01-01 23:06:20 +0000600#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000601
602/*
603** hwtime.h contains inline assembler code for implementing
604** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000605*/
shane9bcbdad2008-05-29 20:22:37 +0000606#include "hwtime.h"
607
drh7b396862003-01-01 23:06:20 +0000608#endif
609
danielk1977fd7f0452008-12-17 17:30:26 +0000610#ifndef NDEBUG
611/*
612** This function is only called from within an assert() expression. It
613** checks that the sqlite3.nTransaction variable is correctly set to
614** the number of non-transaction savepoints currently in the
615** linked list starting at sqlite3.pSavepoint.
616**
617** Usage:
618**
619** assert( checkSavepointCount(db) );
620*/
621static int checkSavepointCount(sqlite3 *db){
622 int n = 0;
623 Savepoint *p;
624 for(p=db->pSavepoint; p; p=p->pNext) n++;
625 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
626 return 1;
627}
628#endif
629
drh27a348c2015-04-13 19:14:06 +0000630/*
631** Return the register of pOp->p2 after first preparing it to be
632** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000633*/
634static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
635 sqlite3VdbeMemSetNull(pOut);
636 pOut->flags = MEM_Int;
637 return pOut;
638}
drh27a348c2015-04-13 19:14:06 +0000639static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
640 Mem *pOut;
641 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000642 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000643 pOut = &p->aMem[pOp->p2];
644 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000645 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000646 return out2PrereleaseWithClear(pOut);
647 }else{
648 pOut->flags = MEM_Int;
649 return pOut;
650 }
drh27a348c2015-04-13 19:14:06 +0000651}
652
drhb9755982010-07-24 16:34:37 +0000653
654/*
drh0fd61352014-02-07 02:29:45 +0000655** Execute as much of a VDBE program as we can.
656** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000657*/
danielk19774adee202004-05-08 08:23:19 +0000658int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000659 Vdbe *p /* The VDBE */
660){
drhbbe879d2009-11-14 18:04:35 +0000661 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000662 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000663#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
664 Op *pOrigOp; /* Value of pOp at the top of the loop */
665#endif
drhb89aeb62016-01-27 15:49:32 +0000666#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000667 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000668#endif
drhb86ccfb2003-01-28 23:13:10 +0000669 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000670 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000671 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000672 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000673 int iCompare = 0; /* Result of last comparison */
drhd1d89142020-07-06 12:13:05 +0000674 u64 nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000675#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhd1d89142020-07-06 12:13:05 +0000676 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000677#endif
drha6c2ed92009-11-14 23:22:23 +0000678 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000679 Mem *pIn1 = 0; /* 1st input operand */
680 Mem *pIn2 = 0; /* 2nd input operand */
681 Mem *pIn3 = 0; /* 3rd input operand */
682 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000683#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000684 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000685#endif
drh856c1032009-06-02 15:21:42 +0000686 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000687
drh17b74812021-02-03 18:32:25 +0000688 assert( p->iVdbeMagic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000689 sqlite3VdbeEnter(p);
drh82642f82019-02-12 22:58:32 +0000690#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
691 if( db->xProgress ){
692 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
693 assert( 0 < db->nProgressOps );
694 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
695 }else{
drhd1d89142020-07-06 12:13:05 +0000696 nProgressLimit = LARGEST_UINT64;
drh82642f82019-02-12 22:58:32 +0000697 }
698#endif
danielk19772e588c72005-12-09 14:25:08 +0000699 if( p->rc==SQLITE_NOMEM ){
700 /* This happens if a malloc() inside a call to sqlite3_column_text() or
701 ** sqlite3_column_text16() failed. */
702 goto no_mem;
703 }
drhcbd8db32015-08-20 17:18:32 +0000704 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drha5f3fb32020-06-03 19:28:10 +0000705 testcase( p->rc!=SQLITE_OK );
706 p->rc = SQLITE_OK;
drh1713afb2013-06-28 01:24:57 +0000707 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000708 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000709 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000710 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000711 db->busyHandler.nBusy = 0;
dan892edb62020-03-30 13:35:05 +0000712 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000713 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000714#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000715 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000716 if( p->pc==0
717 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
718 ){
drh3c23a882007-01-09 14:01:13 +0000719 int i;
drh84e55a82013-11-13 17:58:23 +0000720 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000721 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000722 if( p->db->flags & SQLITE_VdbeListing ){
723 printf("VDBE Program Listing:\n");
724 for(i=0; i<p->nOp; i++){
725 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
726 }
drh3c23a882007-01-09 14:01:13 +0000727 }
drh84e55a82013-11-13 17:58:23 +0000728 if( p->db->flags & SQLITE_VdbeEQP ){
729 for(i=0; i<p->nOp; i++){
730 if( aOp[i].opcode==OP_Explain ){
731 if( once ) printf("VDBE Query Plan:\n");
732 printf("%s\n", aOp[i].p4.z);
733 once = 0;
734 }
735 }
736 }
737 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000738 }
danielk19772d1d86f2008-06-20 14:59:51 +0000739 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000740#endif
drh9467abf2016-02-17 18:44:11 +0000741 for(pOp=&aOp[p->pc]; 1; pOp++){
742 /* Errors are detected by individual opcodes, with an immediate
743 ** jumps to abort_due_to_error. */
744 assert( rc==SQLITE_OK );
745
drhf56fa462015-04-13 21:39:54 +0000746 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000747#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000748 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000749#endif
drhbf159fa2013-06-25 22:01:22 +0000750 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000751#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000752 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000753#endif
drh6e142f52000-06-08 13:36:40 +0000754
danielk19778b60e0f2005-01-12 09:10:39 +0000755 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000756 */
danielk19778b60e0f2005-01-12 09:10:39 +0000757#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000758 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000759 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh22e95fb2020-01-02 14:42:42 +0000760 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
drh75897232000-05-29 14:26:00 +0000761 }
drh3f7d4e42004-07-24 14:35:58 +0000762#endif
763
drh6e142f52000-06-08 13:36:40 +0000764
drhf6038712004-02-08 18:07:34 +0000765 /* Check to see if we need to simulate an interrupt. This only happens
766 ** if we have a special test build.
767 */
768#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000769 if( sqlite3_interrupt_count>0 ){
770 sqlite3_interrupt_count--;
771 if( sqlite3_interrupt_count==0 ){
772 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000773 }
774 }
775#endif
776
drh3c657212009-11-17 23:59:58 +0000777 /* Sanity checking on other operands */
778#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000779 {
780 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
781 if( (opProperty & OPFLG_IN1)!=0 ){
782 assert( pOp->p1>0 );
783 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
784 assert( memIsValid(&aMem[pOp->p1]) );
785 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
786 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
787 }
788 if( (opProperty & OPFLG_IN2)!=0 ){
789 assert( pOp->p2>0 );
790 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
791 assert( memIsValid(&aMem[pOp->p2]) );
792 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
793 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
794 }
795 if( (opProperty & OPFLG_IN3)!=0 ){
796 assert( pOp->p3>0 );
797 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
798 assert( memIsValid(&aMem[pOp->p3]) );
799 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
800 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
801 }
802 if( (opProperty & OPFLG_OUT2)!=0 ){
803 assert( pOp->p2>0 );
804 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
805 memAboutToChange(p, &aMem[pOp->p2]);
806 }
807 if( (opProperty & OPFLG_OUT3)!=0 ){
808 assert( pOp->p3>0 );
809 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
810 memAboutToChange(p, &aMem[pOp->p3]);
811 }
drh3c657212009-11-17 23:59:58 +0000812 }
813#endif
drh6dc41482015-04-16 17:31:02 +0000814#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
815 pOrigOp = pOp;
816#endif
drh93952eb2009-11-13 19:43:43 +0000817
drh75897232000-05-29 14:26:00 +0000818 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000819
drh5e00f6c2001-09-13 13:46:56 +0000820/*****************************************************************************
821** What follows is a massive switch statement where each case implements a
822** separate instruction in the virtual machine. If we follow the usual
823** indentation conventions, each case should be indented by 6 spaces. But
824** that is a lot of wasted space on the left margin. So the code within
825** the switch statement will break with convention and be flush-left. Another
826** big comment (similar to this one) will mark the point in the code where
827** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000828**
829** The formatting of each case is important. The makefile for SQLite
830** generates two C files "opcodes.h" and "opcodes.c" by scanning this
831** file looking for lines that begin with "case OP_". The opcodes.h files
832** will be filled with #defines that give unique integer values to each
833** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000834** each string is the symbolic name for the corresponding opcode. If the
835** case statement is followed by a comment of the form "/# same as ... #/"
836** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000837**
drh9cbf3422008-01-17 16:22:13 +0000838** Other keywords in the comment that follows each case are used to
839** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000840** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000841** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000842**
drhac82fcf2002-09-08 17:23:41 +0000843** Documentation about VDBE opcodes is generated by scanning this file
844** for lines of that contain "Opcode:". That line and all subsequent
845** comment lines are used in the generation of the opcode.html documentation
846** file.
847**
848** SUMMARY:
849**
850** Formatting is important to scripts that scan this file.
851** Do not deviate from the formatting style currently in use.
852**
drh5e00f6c2001-09-13 13:46:56 +0000853*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000854
drh9cbf3422008-01-17 16:22:13 +0000855/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000856**
857** An unconditional jump to address P2.
858** The next instruction executed will be
859** the one at index P2 from the beginning of
860** the program.
drhfe705102014-03-06 13:38:37 +0000861**
862** The P1 parameter is not actually used by this opcode. However, it
863** is sometimes set to 1 instead of 0 as a hint to the command-line shell
864** that this Goto is the bottom of a loop and that the lines from P2 down
865** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000866*/
drh9cbf3422008-01-17 16:22:13 +0000867case OP_Goto: { /* jump */
drhd9670ab2019-12-28 01:52:46 +0000868
869#ifdef SQLITE_DEBUG
870 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
871 ** means we should really jump back to the preceeding OP_ReleaseReg
872 ** instruction. */
873 if( pOp->p5 ){
874 assert( pOp->p2 < (int)(pOp - aOp) );
875 assert( pOp->p2 > 1 );
876 pOp = &aOp[pOp->p2 - 2];
877 assert( pOp[1].opcode==OP_ReleaseReg );
878 goto check_for_interrupt;
879 }
880#endif
881
drhf56fa462015-04-13 21:39:54 +0000882jump_to_p2_and_check_for_interrupt:
883 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000884
885 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000886 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000887 ** completion. Check to see if sqlite3_interrupt() has been called
888 ** or if the progress callback needs to be invoked.
889 **
890 ** This code uses unstructured "goto" statements and does not look clean.
891 ** But that is not due to sloppy coding habits. The code is written this
892 ** way for performance, to avoid having to run the interrupt and progress
893 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
894 ** faster according to "valgrind --tool=cachegrind" */
895check_for_interrupt:
dan892edb62020-03-30 13:35:05 +0000896 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000897#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
898 /* Call the progress callback if it is configured and the required number
899 ** of VDBE ops have been executed (either since this invocation of
900 ** sqlite3VdbeExec() or since last time the progress callback was called).
901 ** If the progress callback returns non-zero, exit the virtual machine with
902 ** a return code SQLITE_ABORT.
903 */
drhb1af9c62019-02-20 13:55:45 +0000904 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000905 assert( db->nProgressOps!=0 );
drhb1af9c62019-02-20 13:55:45 +0000906 nProgressLimit += db->nProgressOps;
drh400fcba2013-11-14 00:09:48 +0000907 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +0000908 nProgressLimit = LARGEST_UINT64;
drh49afe3a2013-07-10 03:05:14 +0000909 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000910 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000911 }
drh49afe3a2013-07-10 03:05:14 +0000912 }
913#endif
914
drh5e00f6c2001-09-13 13:46:56 +0000915 break;
916}
drh75897232000-05-29 14:26:00 +0000917
drh2eb95372008-06-06 15:04:36 +0000918/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000919**
drh2eb95372008-06-06 15:04:36 +0000920** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000921** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000922*/
drhb8475df2011-12-09 16:21:19 +0000923case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000924 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000925 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000926 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000927 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000928 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000929 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000930 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000931
932 /* Most jump operations do a goto to this spot in order to update
933 ** the pOp pointer. */
934jump_to_p2:
935 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000936 break;
937}
938
drh2eb95372008-06-06 15:04:36 +0000939/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000940**
drh81cf13e2014-02-07 18:27:53 +0000941** Jump to the next instruction after the address in register P1. After
942** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000943*/
drh2eb95372008-06-06 15:04:36 +0000944case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000945 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000946 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000947 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000948 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000949 break;
950}
951
drhed71a832014-02-07 19:18:10 +0000952/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000953**
drh5dad9a32014-07-25 18:37:42 +0000954** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000955** located at address P3.
956**
drh5dad9a32014-07-25 18:37:42 +0000957** If P2!=0 then the coroutine implementation immediately follows
958** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000959** address P2.
drh5dad9a32014-07-25 18:37:42 +0000960**
961** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000962*/
963case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000964 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000965 assert( pOp->p2>=0 && pOp->p2<p->nOp );
966 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000967 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000968 assert( !VdbeMemDynamic(pOut) );
969 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000970 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000971 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000972 break;
973}
974
975/* Opcode: EndCoroutine P1 * * * *
976**
drhbc5cf382014-08-06 01:08:07 +0000977** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000978** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000979** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000980**
981** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000982*/
983case OP_EndCoroutine: { /* in1 */
984 VdbeOp *pCaller;
985 pIn1 = &aMem[pOp->p1];
986 assert( pIn1->flags==MEM_Int );
987 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
988 pCaller = &aOp[pIn1->u.i];
989 assert( pCaller->opcode==OP_Yield );
990 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000991 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000992 pIn1->flags = MEM_Undefined;
993 break;
994}
995
996/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000997**
drh5dad9a32014-07-25 18:37:42 +0000998** Swap the program counter with the value in register P1. This
999** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +00001000**
drh5dad9a32014-07-25 18:37:42 +00001001** If the coroutine that is launched by this instruction ends with
1002** Yield or Return then continue to the next instruction. But if
1003** the coroutine launched by this instruction ends with
1004** EndCoroutine, then jump to P2 rather than continuing with the
1005** next instruction.
1006**
1007** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +00001008*/
drh81cf13e2014-02-07 18:27:53 +00001009case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +00001010 int pcDest;
drh3c657212009-11-17 23:59:58 +00001011 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +00001012 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +00001013 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +00001014 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +00001015 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +00001016 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +00001017 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +00001018 break;
1019}
1020
drhf9c8ce32013-11-05 13:33:55 +00001021/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00001022** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +00001023**
drhef8662b2011-06-20 21:47:58 +00001024** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +00001025** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1026** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +00001027** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +00001028*/
1029case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +00001030 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +00001031#ifdef SQLITE_DEBUG
1032 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1033#endif
drh5053a792009-02-20 03:02:23 +00001034 if( (pIn3->flags & MEM_Null)==0 ) break;
1035 /* Fall through into OP_Halt */
drh08b92082020-08-10 14:18:00 +00001036 /* no break */ deliberate_fall_through
drh5053a792009-02-20 03:02:23 +00001037}
drhe00ee6e2008-06-20 15:24:01 +00001038
drhf9c8ce32013-11-05 13:33:55 +00001039/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001040**
drh3d4501e2008-12-04 20:40:10 +00001041** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +00001042** automatically.
drhb19a2bc2001-09-16 00:13:26 +00001043**
drh92f02c32004-09-02 14:57:08 +00001044** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1045** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1046** For errors, it can be some other value. If P1!=0 then P2 will determine
1047** whether or not to rollback the current transaction. Do not rollback
1048** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1049** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +00001050** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +00001051**
drh66a51672008-01-03 00:01:23 +00001052** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +00001053**
drhf9c8ce32013-11-05 13:33:55 +00001054** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1055**
1056** 0: (no change)
1057** 1: NOT NULL contraint failed: P4
1058** 2: UNIQUE constraint failed: P4
1059** 3: CHECK constraint failed: P4
1060** 4: FOREIGN KEY constraint failed: P4
1061**
1062** If P5 is not zero and P4 is NULL, then everything after the ":" is
1063** omitted.
1064**
drh9cfcf5d2002-01-29 18:41:24 +00001065** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001066** every program. So a jump past the last instruction of the program
1067** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001068*/
drh9cbf3422008-01-17 16:22:13 +00001069case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001070 VdbeFrame *pFrame;
1071 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001072
drhf56fa462015-04-13 21:39:54 +00001073 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +00001074#ifdef SQLITE_DEBUG
1075 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1076#endif
dan165921a2009-08-28 18:53:45 +00001077 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +00001078 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001079 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001080 p->pFrame = pFrame->pParent;
1081 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001082 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001083 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001084 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001085 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001086 ** currently being halted. If the p2 instruction of this OP_Halt
1087 ** instruction is set to OE_Ignore, then the sub-program is throwing
1088 ** an IGNORE exception. In this case jump to the address specified
1089 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001090 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001091 }
drhbbe879d2009-11-14 18:04:35 +00001092 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001093 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001094 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001095 break;
1096 }
drh92f02c32004-09-02 14:57:08 +00001097 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001098 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001099 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001100 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001101 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001102 if( pOp->p5 ){
1103 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1104 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001105 testcase( pOp->p5==1 );
1106 testcase( pOp->p5==2 );
1107 testcase( pOp->p5==3 );
1108 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001109 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1110 if( pOp->p4.z ){
1111 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1112 }
drhd9b7ec92013-11-06 14:05:21 +00001113 }else{
drh22c17b82015-05-15 04:13:15 +00001114 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001115 }
drh99f5de72016-04-30 02:59:15 +00001116 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001117 }
drh92f02c32004-09-02 14:57:08 +00001118 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001119 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001120 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001121 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001122 }else{
drhd91c1a12013-02-09 13:58:25 +00001123 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001124 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001125 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001126 }
drh900b31e2007-08-28 02:27:51 +00001127 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001128}
drhc61053b2000-06-04 12:58:36 +00001129
drh4c583122008-01-04 22:01:03 +00001130/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001131** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001132**
drh9cbf3422008-01-17 16:22:13 +00001133** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001134*/
drh27a348c2015-04-13 19:14:06 +00001135case OP_Integer: { /* out2 */
1136 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001137 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001138 break;
1139}
1140
drh4c583122008-01-04 22:01:03 +00001141/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001142** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001143**
drh66a51672008-01-03 00:01:23 +00001144** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001145** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001146*/
drh27a348c2015-04-13 19:14:06 +00001147case OP_Int64: { /* out2 */
1148 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001149 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001150 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001151 break;
1152}
drh4f26d6c2004-05-26 23:25:30 +00001153
drh13573c72010-01-12 17:04:07 +00001154#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001155/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001156** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001157**
drh4c583122008-01-04 22:01:03 +00001158** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001159** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001160*/
drh27a348c2015-04-13 19:14:06 +00001161case OP_Real: { /* same as TK_FLOAT, out2 */
1162 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001163 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001164 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001165 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001166 break;
1167}
drh13573c72010-01-12 17:04:07 +00001168#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001169
drh3c84ddf2008-01-09 02:15:38 +00001170/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001171** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001172**
drh66a51672008-01-03 00:01:23 +00001173** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001174** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001175** this transformation, the length of string P4 is computed and stored
1176** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001177*/
drh27a348c2015-04-13 19:14:06 +00001178case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001179 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001180 pOut = out2Prerelease(p, pOp);
drhea678832008-12-10 19:26:22 +00001181 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001182
1183#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001184 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001185 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001186 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001187 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001188 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001189 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001190 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001191 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001192 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001193 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001194 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001195 }
drh66a51672008-01-03 00:01:23 +00001196 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001197 pOp->p4.z = pOut->z;
1198 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001199 }
danielk197793758c82005-01-21 08:13:14 +00001200#endif
drhbb4957f2008-03-20 14:03:29 +00001201 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001202 goto too_big;
1203 }
drhec722c12019-09-17 21:28:54 +00001204 pOp->opcode = OP_String;
drh2f555112016-04-30 18:10:34 +00001205 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001206 /* Fall through to the next case, OP_String */
drh08b92082020-08-10 14:18:00 +00001207 /* no break */ deliberate_fall_through
danielk1977cbb18d22004-05-28 11:37:27 +00001208}
drhf4479502004-05-27 03:12:53 +00001209
drhf07cf6e2015-03-06 16:45:16 +00001210/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001211** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001212**
drh9cbf3422008-01-17 16:22:13 +00001213** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001214**
drh44aebff2016-05-02 10:25:42 +00001215** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001216** the datatype of the register P2 is converted to BLOB. The content is
1217** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001218** of a string, as if it had been CAST. In other words:
1219**
1220** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001221*/
drh27a348c2015-04-13 19:14:06 +00001222case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001223 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001224 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001225 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1226 pOut->z = pOp->p4.z;
1227 pOut->n = pOp->p1;
1228 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001229 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001230#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001231 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001232 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001233 pIn3 = &aMem[pOp->p3];
1234 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001235 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001236 }
drh41d2e662015-12-01 21:23:07 +00001237#endif
danielk1977c572ef72004-05-27 09:28:41 +00001238 break;
1239}
1240
drh053a1282012-09-19 21:15:46 +00001241/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001242** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001243**
drhb8475df2011-12-09 16:21:19 +00001244** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001245** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001246** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001247** set to NULL.
1248**
1249** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1250** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1251** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001252*/
drh27a348c2015-04-13 19:14:06 +00001253case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001254 int cnt;
drh053a1282012-09-19 21:15:46 +00001255 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001256 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001257 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001258 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001259 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001260 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001261#ifdef SQLITE_DEBUG
1262 pOut->uTemp = 0;
1263#endif
drhb8475df2011-12-09 16:21:19 +00001264 while( cnt>0 ){
1265 pOut++;
1266 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001267 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001268 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001269 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001270 cnt--;
1271 }
drhf0863fe2005-06-12 21:35:51 +00001272 break;
1273}
1274
drh05a86c52014-02-16 01:55:49 +00001275/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001276** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001277**
1278** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1279** instruction, but do not free any string or blob memory associated with
1280** the register, so that if the value was a string or blob that was
1281** previously copied using OP_SCopy, the copies will continue to be valid.
1282*/
1283case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001284 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001285 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001286 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001287 break;
1288}
drhf0863fe2005-06-12 21:35:51 +00001289
drha5750cf2014-02-07 13:20:31 +00001290/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001291** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001292**
drh9de221d2008-01-05 06:51:30 +00001293** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001294** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001295*/
drh27a348c2015-04-13 19:14:06 +00001296case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001297 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001298 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001299 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001300 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001301 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001302 break;
1303}
1304
drheaf52d82010-05-12 13:50:23 +00001305/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001306** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001307**
drheaf52d82010-05-12 13:50:23 +00001308** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001309**
drh0fd61352014-02-07 02:29:45 +00001310** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001311** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001312*/
drh27a348c2015-04-13 19:14:06 +00001313case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001314 Mem *pVar; /* Value being transferred */
1315
drheaf52d82010-05-12 13:50:23 +00001316 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001317 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001318 pVar = &p->aVar[pOp->p1 - 1];
1319 if( sqlite3VdbeMemTooBig(pVar) ){
1320 goto too_big;
drh023ae032007-05-08 12:12:16 +00001321 }
drh7441df72017-01-09 19:27:04 +00001322 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001323 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1324 memcpy(pOut, pVar, MEMCELLSIZE);
1325 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1326 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001327 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001328 break;
1329}
danielk1977295ba552004-05-19 10:34:51 +00001330
drhb21e7c72008-06-22 12:37:57 +00001331/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001332** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001333**
drh079a3072014-03-19 14:10:55 +00001334** Move the P3 values in register P1..P1+P3-1 over into
1335** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001336** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001337** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1338** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001339*/
drhe1349cb2008-04-01 00:36:10 +00001340case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001341 int n; /* Number of registers left to copy */
1342 int p1; /* Register to copy from */
1343 int p2; /* Register to copy to */
1344
drhe09f43f2013-11-21 04:18:31 +00001345 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001346 p1 = pOp->p1;
1347 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001348 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001349 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001350
drha6c2ed92009-11-14 23:22:23 +00001351 pIn1 = &aMem[p1];
1352 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001353 do{
drh9f6168b2016-03-19 23:32:58 +00001354 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1355 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001356 assert( memIsValid(pIn1) );
1357 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001358 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001359#ifdef SQLITE_DEBUG
drh4cbd8472020-01-02 15:02:08 +00001360 pIn1->pScopyFrom = 0;
1361 { int i;
1362 for(i=1; i<p->nMem; i++){
1363 if( aMem[i].pScopyFrom==pIn1 ){
1364 aMem[i].pScopyFrom = pOut;
1365 }
1366 }
drh52043d72011-08-03 16:40:15 +00001367 }
1368#endif
drhbd6789e2015-04-28 14:00:02 +00001369 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001370 REGISTER_TRACE(p2++, pOut);
1371 pIn1++;
1372 pOut++;
drh079a3072014-03-19 14:10:55 +00001373 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001374 break;
1375}
1376
drhe8e4af72012-09-21 00:04:28 +00001377/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001378** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001379**
drhe8e4af72012-09-21 00:04:28 +00001380** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001381**
1382** This instruction makes a deep copy of the value. A duplicate
1383** is made of any string or blob constant. See also OP_SCopy.
1384*/
drhe8e4af72012-09-21 00:04:28 +00001385case OP_Copy: {
1386 int n;
1387
1388 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001389 pIn1 = &aMem[pOp->p1];
1390 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001391 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001392 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001393 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001394 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1395 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001396#ifdef SQLITE_DEBUG
1397 pOut->pScopyFrom = 0;
1398#endif
drhe8e4af72012-09-21 00:04:28 +00001399 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1400 if( (n--)==0 ) break;
1401 pOut++;
1402 pIn1++;
1403 }
drhe1349cb2008-04-01 00:36:10 +00001404 break;
1405}
1406
drhb1fdb2a2008-01-05 04:06:03 +00001407/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001408** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001409**
drh9cbf3422008-01-17 16:22:13 +00001410** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001411**
1412** This instruction makes a shallow copy of the value. If the value
1413** is a string or blob, then the copy is only a pointer to the
1414** original and hence if the original changes so will the copy.
1415** Worse, if the original is deallocated, the copy becomes invalid.
1416** Thus the program must guarantee that the original will not change
1417** during the lifetime of the copy. Use OP_Copy to make a complete
1418** copy.
1419*/
drh26198bb2013-10-31 11:15:09 +00001420case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001421 pIn1 = &aMem[pOp->p1];
1422 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001423 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001424 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001425#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001426 pOut->pScopyFrom = pIn1;
1427 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001428#endif
drh5e00f6c2001-09-13 13:46:56 +00001429 break;
1430}
drh75897232000-05-29 14:26:00 +00001431
drhfed7ac62015-10-15 18:04:59 +00001432/* Opcode: IntCopy P1 P2 * * *
1433** Synopsis: r[P2]=r[P1]
1434**
1435** Transfer the integer value held in register P1 into register P2.
1436**
1437** This is an optimized version of SCopy that works only for integer
1438** values.
1439*/
1440case OP_IntCopy: { /* out2 */
1441 pIn1 = &aMem[pOp->p1];
1442 assert( (pIn1->flags & MEM_Int)!=0 );
1443 pOut = &aMem[pOp->p2];
1444 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1445 break;
1446}
1447
drh18e56072021-01-31 15:50:36 +00001448/* Opcode: ChngCntRow P1 P2 * * *
1449** Synopsis: output=r[P1]
1450**
1451** Output value in register P1 as the chance count for a DML statement,
1452** due to the "PRAGMA count_changes=ON" setting. Or, if there was a
1453** foreign key error in the statement, trigger the error now.
1454**
1455** This opcode is a variant of OP_ResultRow that checks the foreign key
1456** immediate constraint count and throws an error if the count is
1457** non-zero. The P2 opcode must be 1.
1458*/
1459case OP_ChngCntRow: {
1460 assert( pOp->p2==1 );
1461 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){
1462 goto abort_due_to_error;
1463 }
1464 /* Fall through to the next case, OP_String */
1465 /* no break */ deliberate_fall_through
1466}
1467
drh9cbf3422008-01-17 16:22:13 +00001468/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001469** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001470**
shane21e7feb2008-05-30 15:59:49 +00001471** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001472** results. This opcode causes the sqlite3_step() call to terminate
1473** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001474** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001475** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001476*/
drh9cbf3422008-01-17 16:22:13 +00001477case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001478 Mem *pMem;
1479 int i;
1480 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001481 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001482 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001483
drhd4e70eb2008-01-02 00:34:36 +00001484 /* Invalidate all ephemeral cursor row caches */
1485 p->cacheCtr = (p->cacheCtr + 2)|1;
1486
1487 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001488 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001489 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001490 */
drha6c2ed92009-11-14 23:22:23 +00001491 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001492 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001493 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001494 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001495 assert( (pMem[i].flags & MEM_Ephem)==0
1496 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001497 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001498 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drh02ff7472019-12-31 12:18:24 +00001499#ifdef SQLITE_DEBUG
1500 /* The registers in the result will not be used again when the
1501 ** prepared statement restarts. This is because sqlite3_column()
1502 ** APIs might have caused type conversions of made other changes to
1503 ** the register values. Therefore, we can go ahead and break any
1504 ** OP_SCopy dependencies. */
1505 pMem[i].pScopyFrom = 0;
1506#endif
drhd4e70eb2008-01-02 00:34:36 +00001507 }
drh28039692008-03-17 16:54:01 +00001508 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001509
drh3d2a5292016-07-13 22:55:01 +00001510 if( db->mTrace & SQLITE_TRACE_ROW ){
drh08b92082020-08-10 14:18:00 +00001511 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
drh3d2a5292016-07-13 22:55:01 +00001512 }
1513
drh02ff7472019-12-31 12:18:24 +00001514
drhd4e70eb2008-01-02 00:34:36 +00001515 /* Return SQLITE_ROW
1516 */
drhf56fa462015-04-13 21:39:54 +00001517 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001518 rc = SQLITE_ROW;
1519 goto vdbe_return;
1520}
1521
drh5b6afba2008-01-05 16:29:28 +00001522/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001523** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001524**
drh5b6afba2008-01-05 16:29:28 +00001525** Add the text in register P1 onto the end of the text in
1526** register P2 and store the result in register P3.
1527** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001528**
1529** P3 = P2 || P1
1530**
1531** It is illegal for P1 and P3 to be the same register. Sometimes,
1532** if P3 is the same register as P2, the implementation is able
1533** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001534*/
drh5b6afba2008-01-05 16:29:28 +00001535case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh8a7e11f2019-05-01 15:32:40 +00001536 i64 nByte; /* Total size of the output string or blob */
1537 u16 flags1; /* Initial flags for P1 */
1538 u16 flags2; /* Initial flags for P2 */
danielk19778a6b5412004-05-24 07:04:25 +00001539
drh3c657212009-11-17 23:59:58 +00001540 pIn1 = &aMem[pOp->p1];
1541 pIn2 = &aMem[pOp->p2];
1542 pOut = &aMem[pOp->p3];
drh8a7e11f2019-05-01 15:32:40 +00001543 testcase( pOut==pIn2 );
danielk1977a7a8e142008-02-13 18:25:27 +00001544 assert( pIn1!=pOut );
drh8a7e11f2019-05-01 15:32:40 +00001545 flags1 = pIn1->flags;
1546 testcase( flags1 & MEM_Null );
1547 testcase( pIn2->flags & MEM_Null );
1548 if( (flags1 | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001549 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001550 break;
drh5e00f6c2001-09-13 13:46:56 +00001551 }
drh8a7e11f2019-05-01 15:32:40 +00001552 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
1553 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001554 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001555 }else if( (flags1 & MEM_Zero)!=0 ){
1556 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001557 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001558 }
1559 flags2 = pIn2->flags;
1560 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
1561 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001562 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001563 }else if( (flags2 & MEM_Zero)!=0 ){
1564 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001565 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001566 }
drh5b6afba2008-01-05 16:29:28 +00001567 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001568 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001569 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001570 }
drhdf82afc2019-05-16 01:22:21 +00001571 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001572 goto no_mem;
1573 }
drhc91b2fd2014-03-01 18:13:23 +00001574 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001575 if( pOut!=pIn2 ){
1576 memcpy(pOut->z, pIn2->z, pIn2->n);
drh8a7e11f2019-05-01 15:32:40 +00001577 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
1578 pIn2->flags = flags2;
danielk1977a7a8e142008-02-13 18:25:27 +00001579 }
1580 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh8a7e11f2019-05-01 15:32:40 +00001581 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1582 pIn1->flags = flags1;
drh81316f82013-10-29 20:40:47 +00001583 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001584 pOut->z[nByte+1] = 0;
drhdf82afc2019-05-16 01:22:21 +00001585 pOut->z[nByte+2] = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001586 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001587 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001588 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001589 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001590 break;
1591}
drh75897232000-05-29 14:26:00 +00001592
drh3c84ddf2008-01-09 02:15:38 +00001593/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001594** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001595**
drh60a713c2008-01-21 16:22:45 +00001596** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001597** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001598** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001599*/
drh3c84ddf2008-01-09 02:15:38 +00001600/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001601** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001602**
drh3c84ddf2008-01-09 02:15:38 +00001603**
shane21e7feb2008-05-30 15:59:49 +00001604** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001605** and store the result in register P3.
1606** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001607*/
drh3c84ddf2008-01-09 02:15:38 +00001608/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001609** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001610**
drh60a713c2008-01-21 16:22:45 +00001611** Subtract the value in register P1 from the value in register P2
1612** and store the result in register P3.
1613** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001614*/
drh9cbf3422008-01-17 16:22:13 +00001615/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001616** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001617**
drh60a713c2008-01-21 16:22:45 +00001618** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001619** and store the result in register P3 (P3=P2/P1). If the value in
1620** register P1 is zero, then the result is NULL. If either input is
1621** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001622*/
drh9cbf3422008-01-17 16:22:13 +00001623/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001624** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001625**
drh40864a12013-11-15 18:58:37 +00001626** Compute the remainder after integer register P2 is divided by
1627** register P1 and store the result in register P3.
1628** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001629** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001630*/
drh5b6afba2008-01-05 16:29:28 +00001631case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1632case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1633case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1634case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1635case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh3d1d90a2014-03-24 15:00:15 +00001636 u16 flags; /* Combined MEM_* flags from both inputs */
1637 u16 type1; /* Numeric type of left operand */
1638 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001639 i64 iA; /* Integer value of left operand */
1640 i64 iB; /* Integer value of right operand */
1641 double rA; /* Real value of left operand */
1642 double rB; /* Real value of right operand */
1643
drh3c657212009-11-17 23:59:58 +00001644 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001645 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001646 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001647 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001648 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001649 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001650 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001651 iA = pIn1->u.i;
1652 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001653 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001654 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1655 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1656 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001657 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001658 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001659 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001660 iB /= iA;
drh75897232000-05-29 14:26:00 +00001661 break;
1662 }
drhbf4133c2001-10-13 02:59:08 +00001663 default: {
drh856c1032009-06-02 15:21:42 +00001664 if( iA==0 ) goto arithmetic_result_is_null;
1665 if( iA==-1 ) iA = 1;
1666 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001667 break;
1668 }
drh75897232000-05-29 14:26:00 +00001669 }
drh856c1032009-06-02 15:21:42 +00001670 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001671 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001672 }else if( (flags & MEM_Null)!=0 ){
1673 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001674 }else{
drh158b9cb2011-03-05 20:59:46 +00001675fp_math:
drh856c1032009-06-02 15:21:42 +00001676 rA = sqlite3VdbeRealValue(pIn1);
1677 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001678 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001679 case OP_Add: rB += rA; break;
1680 case OP_Subtract: rB -= rA; break;
1681 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001682 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001683 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001684 if( rA==(double)0 ) goto arithmetic_result_is_null;
1685 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001686 break;
1687 }
drhbf4133c2001-10-13 02:59:08 +00001688 default: {
drhe3b89d22019-01-18 17:53:50 +00001689 iA = sqlite3VdbeIntValue(pIn1);
1690 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001691 if( iA==0 ) goto arithmetic_result_is_null;
1692 if( iA==-1 ) iA = 1;
1693 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001694 break;
1695 }
drh5e00f6c2001-09-13 13:46:56 +00001696 }
drhc5a7b512010-01-13 16:25:42 +00001697#ifdef SQLITE_OMIT_FLOATING_POINT
1698 pOut->u.i = rB;
1699 MemSetTypeFlag(pOut, MEM_Int);
1700#else
drh856c1032009-06-02 15:21:42 +00001701 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001702 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001703 }
drh74eaba42014-09-18 17:52:15 +00001704 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001705 MemSetTypeFlag(pOut, MEM_Real);
drhc5a7b512010-01-13 16:25:42 +00001706#endif
drh5e00f6c2001-09-13 13:46:56 +00001707 }
1708 break;
1709
drha05a7222008-01-19 03:35:58 +00001710arithmetic_result_is_null:
1711 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001712 break;
1713}
1714
drh7a957892012-02-02 17:35:43 +00001715/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001716**
drhbb6783b2017-04-29 18:02:49 +00001717** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001718** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1719** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001720** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001721**
drh7a957892012-02-02 17:35:43 +00001722** If P1 is not zero, then it is a register that a subsequent min() or
1723** max() aggregate will set to 1 if the current row is not the minimum or
1724** maximum. The P1 register is initialized to 0 by this instruction.
1725**
danielk1977dc1bdc42004-06-11 10:51:27 +00001726** The interface used by the implementation of the aforementioned functions
1727** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001728** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001729*/
drh9cbf3422008-01-17 16:22:13 +00001730case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001731 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001732 if( pOp->p1 ){
1733 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1734 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001735 break;
1736}
1737
drh98757152008-01-09 23:04:12 +00001738/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001739** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001740**
drh98757152008-01-09 23:04:12 +00001741** Take the bit-wise AND of the values in register P1 and P2 and
1742** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001743** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001744*/
drh98757152008-01-09 23:04:12 +00001745/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001746** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001747**
drh98757152008-01-09 23:04:12 +00001748** Take the bit-wise OR of the values in register P1 and P2 and
1749** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001750** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001751*/
drh98757152008-01-09 23:04:12 +00001752/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001753** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001754**
drh98757152008-01-09 23:04:12 +00001755** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001756** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001757** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001758** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001759*/
drh98757152008-01-09 23:04:12 +00001760/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001761** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001762**
drh98757152008-01-09 23:04:12 +00001763** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001764** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001765** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001766** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001767*/
drh5b6afba2008-01-05 16:29:28 +00001768case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1769case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1770case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1771case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001772 i64 iA;
1773 u64 uA;
1774 i64 iB;
1775 u8 op;
drh6810ce62004-01-31 19:22:56 +00001776
drh3c657212009-11-17 23:59:58 +00001777 pIn1 = &aMem[pOp->p1];
1778 pIn2 = &aMem[pOp->p2];
1779 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001780 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001781 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001782 break;
1783 }
drh158b9cb2011-03-05 20:59:46 +00001784 iA = sqlite3VdbeIntValue(pIn2);
1785 iB = sqlite3VdbeIntValue(pIn1);
1786 op = pOp->opcode;
1787 if( op==OP_BitAnd ){
1788 iA &= iB;
1789 }else if( op==OP_BitOr ){
1790 iA |= iB;
1791 }else if( iB!=0 ){
1792 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1793
1794 /* If shifting by a negative amount, shift in the other direction */
1795 if( iB<0 ){
1796 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1797 op = 2*OP_ShiftLeft + 1 - op;
1798 iB = iB>(-64) ? -iB : 64;
1799 }
1800
1801 if( iB>=64 ){
1802 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1803 }else{
1804 memcpy(&uA, &iA, sizeof(uA));
1805 if( op==OP_ShiftLeft ){
1806 uA <<= iB;
1807 }else{
1808 uA >>= iB;
1809 /* Sign-extend on a right shift of a negative number */
1810 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1811 }
1812 memcpy(&iA, &uA, sizeof(iA));
1813 }
drhbf4133c2001-10-13 02:59:08 +00001814 }
drh158b9cb2011-03-05 20:59:46 +00001815 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001816 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001817 break;
1818}
1819
drh8558cde2008-01-05 05:20:10 +00001820/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001821** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001822**
danielk19770cdc0222008-06-26 18:04:03 +00001823** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001824** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001825**
drh8558cde2008-01-05 05:20:10 +00001826** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001827*/
drh9cbf3422008-01-17 16:22:13 +00001828case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001829 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001830 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001831 sqlite3VdbeMemIntegerify(pIn1);
1832 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001833 break;
1834}
1835
dane5166e02019-03-19 11:56:39 +00001836/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001837**
dane5166e02019-03-19 11:56:39 +00001838** Force the value in register P1 to be an integer. If the value
1839** in P1 is not an integer and cannot be converted into an integer
1840** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001841** raise an SQLITE_MISMATCH exception.
1842*/
drh9cbf3422008-01-17 16:22:13 +00001843case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001844 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001845 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001846 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001847 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001848 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001849 if( pOp->p2==0 ){
1850 rc = SQLITE_MISMATCH;
1851 goto abort_due_to_error;
1852 }else{
drhf56fa462015-04-13 21:39:54 +00001853 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001854 }
drh8aff1012001-12-22 14:49:24 +00001855 }
drh8aff1012001-12-22 14:49:24 +00001856 }
drhc9065332019-04-01 14:01:21 +00001857 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001858 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001859 break;
1860}
1861
drh13573c72010-01-12 17:04:07 +00001862#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001863/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001864**
drh2133d822008-01-03 18:44:59 +00001865** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001866**
drh8a512562005-11-14 22:29:05 +00001867** This opcode is used when extracting information from a column that
1868** has REAL affinity. Such column values may still be stored as
1869** integers, for space efficiency, but after extraction we want them
1870** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001871*/
drh9cbf3422008-01-17 16:22:13 +00001872case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001873 pIn1 = &aMem[pOp->p1];
drh169f0772019-05-02 21:36:26 +00001874 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +00001875 testcase( pIn1->flags & MEM_Int );
1876 testcase( pIn1->flags & MEM_IntReal );
drh8558cde2008-01-05 05:20:10 +00001877 sqlite3VdbeMemRealify(pIn1);
drhefb5f9a2019-08-30 21:52:13 +00001878 REGISTER_TRACE(pOp->p1, pIn1);
drh8a512562005-11-14 22:29:05 +00001879 }
drh487e2622005-06-25 18:42:14 +00001880 break;
1881}
drh13573c72010-01-12 17:04:07 +00001882#endif
drh487e2622005-06-25 18:42:14 +00001883
drh8df447f2005-11-01 15:48:24 +00001884#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001885/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001886** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001887**
drh4169e432014-08-25 20:11:52 +00001888** Force the value in register P1 to be the type defined by P2.
1889**
1890** <ul>
drhbb6783b2017-04-29 18:02:49 +00001891** <li> P2=='A' &rarr; BLOB
1892** <li> P2=='B' &rarr; TEXT
1893** <li> P2=='C' &rarr; NUMERIC
1894** <li> P2=='D' &rarr; INTEGER
1895** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001896** </ul>
drh487e2622005-06-25 18:42:14 +00001897**
1898** A NULL value is not changed by this routine. It remains NULL.
1899*/
drh4169e432014-08-25 20:11:52 +00001900case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001901 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001902 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001903 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001904 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1905 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1906 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001907 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001908 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001909 rc = ExpandBlob(pIn1);
drh9467abf2016-02-17 18:44:11 +00001910 if( rc ) goto abort_due_to_error;
drh0af6ddd2019-12-23 03:37:46 +00001911 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
1912 if( rc ) goto abort_due_to_error;
1913 UPDATE_MAX_BLOBSIZE(pIn1);
drh5d732722019-12-20 17:25:10 +00001914 REGISTER_TRACE(pOp->p1, pIn1);
drh487e2622005-06-25 18:42:14 +00001915 break;
1916}
drh8a512562005-11-14 22:29:05 +00001917#endif /* SQLITE_OMIT_CAST */
1918
drh79752b62016-08-13 10:02:17 +00001919/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001920** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001921**
1922** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1923** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1924** store the result of comparison in register P2.
1925**
1926** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1927** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1928** to coerce both inputs according to this affinity before the
1929** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1930** affinity is used. Note that the affinity conversions are stored
1931** back into the input registers P1 and P3. So this opcode can cause
1932** persistent changes to registers P1 and P3.
1933**
1934** Once any conversions have taken place, and neither value is NULL,
1935** the values are compared. If both values are blobs then memcmp() is
1936** used to determine the results of the comparison. If both values
1937** are text, then the appropriate collating function specified in
1938** P4 is used to do the comparison. If P4 is not specified then
1939** memcmp() is used to compare text string. If both values are
1940** numeric, then a numeric comparison is used. If the two values
1941** are of different types, then numbers are considered less than
1942** strings and strings are considered less than blobs.
1943**
1944** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1945** true or false and is never NULL. If both operands are NULL then the result
1946** of comparison is true. If either operand is NULL then the result is false.
1947** If neither operand is NULL the result is the same as it would be if
1948** the SQLITE_NULLEQ flag were omitted from P5.
1949**
1950** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001951** content of r[P2] is only changed if the new value is NULL or 0 (false).
1952** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001953*/
1954/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001955** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001956**
1957** This works just like the Eq opcode except that the jump is taken if
1958** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1959** additional information.
1960**
1961** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001962** content of r[P2] is only changed if the new value is NULL or 1 (true).
1963** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001964*/
drh35573352008-01-08 23:54:25 +00001965/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001966** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001967**
drh35573352008-01-08 23:54:25 +00001968** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001969** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1970** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001971**
drh35573352008-01-08 23:54:25 +00001972** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001973** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001974** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001975**
drh35573352008-01-08 23:54:25 +00001976** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001977** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001978** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001979** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001980** affinity is used. Note that the affinity conversions are stored
1981** back into the input registers P1 and P3. So this opcode can cause
1982** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001983**
1984** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001985** the values are compared. If both values are blobs then memcmp() is
1986** used to determine the results of the comparison. If both values
1987** are text, then the appropriate collating function specified in
1988** P4 is used to do the comparison. If P4 is not specified then
1989** memcmp() is used to compare text string. If both values are
1990** numeric, then a numeric comparison is used. If the two values
1991** are of different types, then numbers are considered less than
1992** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001993*/
drh9cbf3422008-01-17 16:22:13 +00001994/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001995** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001996**
drh35573352008-01-08 23:54:25 +00001997** This works just like the Lt opcode except that the jump is taken if
1998** the content of register P3 is less than or equal to the content of
1999** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002000*/
drh9cbf3422008-01-17 16:22:13 +00002001/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002002** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002003**
drh35573352008-01-08 23:54:25 +00002004** This works just like the Lt opcode except that the jump is taken if
2005** the content of register P3 is greater than the content of
2006** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002007*/
drh9cbf3422008-01-17 16:22:13 +00002008/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002009** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002010**
drh35573352008-01-08 23:54:25 +00002011** This works just like the Lt opcode except that the jump is taken if
2012** the content of register P3 is greater than or equal to the content of
2013** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002014*/
drh9cbf3422008-01-17 16:22:13 +00002015case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
2016case OP_Ne: /* same as TK_NE, jump, in1, in3 */
2017case OP_Lt: /* same as TK_LT, jump, in1, in3 */
2018case OP_Le: /* same as TK_LE, jump, in1, in3 */
2019case OP_Gt: /* same as TK_GT, jump, in1, in3 */
2020case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00002021 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00002022 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00002023 u16 flags1; /* Copy of initial value of pIn1->flags */
2024 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00002025
drh3c657212009-11-17 23:59:58 +00002026 pIn1 = &aMem[pOp->p1];
2027 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00002028 flags1 = pIn1->flags;
2029 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00002030 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00002031 /* One or both operands are NULL */
2032 if( pOp->p5 & SQLITE_NULLEQ ){
2033 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2034 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2035 ** or not both operands are null.
2036 */
drh053a1282012-09-19 21:15:46 +00002037 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00002038 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
2039 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00002040 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00002041 && (flags3&MEM_Cleared)==0
2042 ){
drh4910a762016-09-03 01:46:15 +00002043 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002044 }else{
danbdabe742019-03-18 16:51:24 +00002045 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002046 }
drh6a2fe092009-09-23 02:29:36 +00002047 }else{
2048 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2049 ** then the result is always NULL.
2050 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2051 */
drh688852a2014-02-17 22:40:43 +00002052 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002053 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002054 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00002055 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00002056 MemSetTypeFlag(pOut, MEM_Null);
2057 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002058 }else{
drhf4345e42014-02-18 11:31:59 +00002059 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00002060 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00002061 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002062 }
drh6a2fe092009-09-23 02:29:36 +00002063 }
2064 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002065 }
drh6a2fe092009-09-23 02:29:36 +00002066 }else{
2067 /* Neither operand is NULL. Do a comparison. */
2068 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002069 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002070 if( (flags1 | flags3)&MEM_Str ){
drh169f0772019-05-02 21:36:26 +00002071 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002072 applyNumericAffinity(pIn1,0);
drh86d2de22020-06-14 13:40:13 +00002073 testcase( flags3==pIn3->flags );
drh4b37cd42016-06-25 11:43:47 +00002074 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002075 }
drh169f0772019-05-02 21:36:26 +00002076 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002077 applyNumericAffinity(pIn3,0);
2078 }
drh24a09622014-09-18 16:28:59 +00002079 }
drh64caee42016-09-09 19:33:00 +00002080 /* Handle the common case of integer comparison here, as an
2081 ** optimization, to avoid a call to sqlite3MemCompare() */
2082 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2083 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2084 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2085 res = 0;
2086 goto compare_op;
2087 }
drh24a09622014-09-18 16:28:59 +00002088 }else if( affinity==SQLITE_AFF_TEXT ){
drh169f0772019-05-02 21:36:26 +00002089 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002090 testcase( pIn1->flags & MEM_Int );
2091 testcase( pIn1->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002092 testcase( pIn1->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002093 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002094 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2095 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh9dce0ef2020-02-01 21:03:27 +00002096 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
drh24a09622014-09-18 16:28:59 +00002097 }
drhb44fec62019-12-24 21:42:22 +00002098 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002099 testcase( pIn3->flags & MEM_Int );
2100 testcase( pIn3->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002101 testcase( pIn3->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002102 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002103 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2104 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002105 }
drh6a2fe092009-09-23 02:29:36 +00002106 }
drh6a2fe092009-09-23 02:29:36 +00002107 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002108 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002109 }
drh64caee42016-09-09 19:33:00 +00002110compare_op:
drh58596362017-08-03 00:29:23 +00002111 /* At this point, res is negative, zero, or positive if reg[P1] is
2112 ** less than, equal to, or greater than reg[P3], respectively. Compute
2113 ** the answer to this operator in res2, depending on what the comparison
2114 ** operator actually is. The next block of code depends on the fact
2115 ** that the 6 comparison operators are consecutive integers in this
2116 ** order: NE, EQ, GT, LE, LT, GE */
2117 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2118 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2119 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2120 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2121 res2 = aLTb[pOp->opcode - OP_Ne];
2122 }else if( res==0 ){
2123 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2124 res2 = aEQb[pOp->opcode - OP_Ne];
2125 }else{
2126 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2127 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002128 }
2129
drhf56fa462015-04-13 21:39:54 +00002130 /* Undo any changes made by applyAffinity() to the input registers. */
drhf56fa462015-04-13 21:39:54 +00002131 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2132 pIn3->flags = flags3;
drhb44fec62019-12-24 21:42:22 +00002133 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2134 pIn1->flags = flags1;
drhf56fa462015-04-13 21:39:54 +00002135
drh35573352008-01-08 23:54:25 +00002136 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002137 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002138 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002139 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002140 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002141 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2142 ** is only used in contexts where either:
2143 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2144 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2145 ** Therefore it is not necessary to check the content of r[P2] for
2146 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002147 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002148 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002149 testcase( res2==0 && pOp->opcode==OP_Eq );
2150 testcase( res2==1 && pOp->opcode==OP_Eq );
2151 testcase( res2==0 && pOp->opcode==OP_Ne );
2152 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002153 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002154 }
drh2b4ded92010-09-27 21:09:31 +00002155 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002156 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002157 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002158 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002159 }else{
drh6cbbcd82019-04-01 13:06:19 +00002160 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002161 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002162 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002163 }
danielk1977a37cdde2004-05-16 11:15:36 +00002164 }
2165 break;
2166}
drhc9b84a12002-06-20 11:36:48 +00002167
drh79752b62016-08-13 10:02:17 +00002168/* Opcode: ElseNotEq * P2 * * *
2169**
drh13d79502019-12-23 02:18:49 +00002170** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2171** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2172** opcodes are allowed to occur between this instruction and the previous
2173** OP_Lt or OP_Gt. Furthermore, the prior OP_Lt or OP_Gt must have the
2174** SQLITE_STOREP2 bit set in the P5 field.
2175**
2176** If result of an OP_Eq comparison on the same two operands as the
2177** prior OP_Lt or OP_Gt would have been NULL or false (0), then then
2178** jump to P2. If the result of an OP_Eq comparison on the two previous
2179** operands would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002180*/
2181case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
drh13d79502019-12-23 02:18:49 +00002182
2183#ifdef SQLITE_DEBUG
2184 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
2185 ** OP_Gt with the SQLITE_STOREP2 flag set, with zero or more intervening
2186 ** OP_ReleaseReg opcodes */
2187 int iAddr;
2188 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
2189 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
2190 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
2191 assert( aOp[iAddr].p5 & SQLITE_STOREP2 );
2192 break;
2193 }
2194#endif /* SQLITE_DEBUG */
drh0f825a72016-08-13 14:17:02 +00002195 VdbeBranchTaken(iCompare!=0, 2);
2196 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002197 break;
2198}
2199
2200
drh0acb7e42008-06-25 00:12:41 +00002201/* Opcode: Permutation * * * P4 *
2202**
drhb7dab702017-01-26 18:00:00 +00002203** Set the permutation used by the OP_Compare operator in the next
2204** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002205**
drh953f7612012-12-07 22:18:54 +00002206** The permutation is only valid until the next OP_Compare that has
2207** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2208** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002209**
2210** The first integer in the P4 integer array is the length of the array
2211** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002212*/
2213case OP_Permutation: {
2214 assert( pOp->p4type==P4_INTARRAY );
2215 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002216 assert( pOp[1].opcode==OP_Compare );
2217 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002218 break;
2219}
2220
drh953f7612012-12-07 22:18:54 +00002221/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002222** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002223**
drh710c4842010-08-30 01:17:20 +00002224** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2225** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002226** the comparison for use by the next OP_Jump instruct.
2227**
drh0ca10df2012-12-08 13:26:23 +00002228** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2229** determined by the most recent OP_Permutation operator. If the
2230** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2231** order.
2232**
drh0acb7e42008-06-25 00:12:41 +00002233** P4 is a KeyInfo structure that defines collating sequences and sort
2234** orders for the comparison. The permutation applies to registers
2235** only. The KeyInfo elements are used sequentially.
2236**
2237** The comparison is a sort comparison, so NULLs compare equal,
2238** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002239** and strings are less than blobs.
2240*/
2241case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002242 int n;
2243 int i;
2244 int p1;
2245 int p2;
2246 const KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00002247 u32 idx;
drh856c1032009-06-02 15:21:42 +00002248 CollSeq *pColl; /* Collating sequence to use on this term */
2249 int bRev; /* True for DESCENDING sort order */
drhabc38152020-07-22 13:38:04 +00002250 u32 *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002251
drhb7dab702017-01-26 18:00:00 +00002252 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2253 aPermute = 0;
2254 }else{
2255 assert( pOp>aOp );
2256 assert( pOp[-1].opcode==OP_Permutation );
2257 assert( pOp[-1].p4type==P4_INTARRAY );
2258 aPermute = pOp[-1].p4.ai + 1;
2259 assert( aPermute!=0 );
2260 }
drh856c1032009-06-02 15:21:42 +00002261 n = pOp->p3;
2262 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002263 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002264 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002265 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002266 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002267#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002268 if( aPermute ){
2269 int k, mx = 0;
mistachkincec5f1d2020-08-04 16:11:37 +00002270 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002271 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2272 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002273 }else{
drh9f6168b2016-03-19 23:32:58 +00002274 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2275 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002276 }
2277#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002278 for(i=0; i<n; i++){
drh8deae5a2020-07-29 12:23:20 +00002279 idx = aPermute ? aPermute[i] : (u32)i;
drh2b4ded92010-09-27 21:09:31 +00002280 assert( memIsValid(&aMem[p1+idx]) );
2281 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002282 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2283 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002284 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002285 pColl = pKeyInfo->aColl[i];
dan6e118922019-08-12 16:36:38 +00002286 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
drha6c2ed92009-11-14 23:22:23 +00002287 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002288 if( iCompare ){
dan6e118922019-08-12 16:36:38 +00002289 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
2290 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
2291 ){
2292 iCompare = -iCompare;
2293 }
drh0acb7e42008-06-25 00:12:41 +00002294 if( bRev ) iCompare = -iCompare;
2295 break;
2296 }
drh16ee60f2008-06-20 18:13:25 +00002297 }
2298 break;
2299}
2300
2301/* Opcode: Jump P1 P2 P3 * *
2302**
2303** Jump to the instruction at address P1, P2, or P3 depending on whether
2304** in the most recent OP_Compare instruction the P1 vector was less than
2305** equal to, or greater than the P2 vector, respectively.
2306*/
drh0acb7e42008-06-25 00:12:41 +00002307case OP_Jump: { /* jump */
2308 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002309 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002310 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002311 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002312 }else{
drh7083a482018-07-10 16:04:04 +00002313 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002314 }
2315 break;
2316}
2317
drh5b6afba2008-01-05 16:29:28 +00002318/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002319** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002320**
drh5b6afba2008-01-05 16:29:28 +00002321** Take the logical AND of the values in registers P1 and P2 and
2322** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002323**
drh5b6afba2008-01-05 16:29:28 +00002324** If either P1 or P2 is 0 (false) then the result is 0 even if
2325** the other input is NULL. A NULL and true or two NULLs give
2326** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002327*/
drh5b6afba2008-01-05 16:29:28 +00002328/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002329** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002330**
2331** Take the logical OR of the values in register P1 and P2 and
2332** store the answer in register P3.
2333**
2334** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2335** even if the other input is NULL. A NULL and false or two NULLs
2336** give a NULL output.
2337*/
2338case OP_And: /* same as TK_AND, in1, in2, out3 */
2339case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002340 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2341 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002342
drh1fcfa722018-02-26 15:27:31 +00002343 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2344 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002345 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002346 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002347 v1 = and_logic[v1*3+v2];
2348 }else{
drh5b6afba2008-01-05 16:29:28 +00002349 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002350 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002351 }
drh3c657212009-11-17 23:59:58 +00002352 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002353 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002354 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002355 }else{
drh5b6afba2008-01-05 16:29:28 +00002356 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002357 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002358 }
drh5e00f6c2001-09-13 13:46:56 +00002359 break;
2360}
2361
drh8abed7b2018-02-26 18:49:05 +00002362/* Opcode: IsTrue P1 P2 P3 P4 *
2363** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2364**
2365** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2366** IS NOT FALSE operators.
2367**
drh96acafb2018-02-27 14:49:25 +00002368** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002369** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2370** NULL, then the P3 is stored in register P2. Invert the answer if P4
2371** is 1.
2372**
2373** The logic is summarized like this:
2374**
2375** <ul>
drh96acafb2018-02-27 14:49:25 +00002376** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2377** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2378** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2379** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002380** </ul>
2381*/
2382case OP_IsTrue: { /* in1, out2 */
2383 assert( pOp->p4type==P4_INT32 );
2384 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002385 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002386 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2387 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2388 break;
2389}
2390
drhe99fa2a2008-12-15 15:27:51 +00002391/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002392** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002393**
drhe99fa2a2008-12-15 15:27:51 +00002394** Interpret the value in register P1 as a boolean value. Store the
2395** boolean complement in register P2. If the value in register P1 is
2396** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002397*/
drh93952eb2009-11-13 19:43:43 +00002398case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002399 pIn1 = &aMem[pOp->p1];
2400 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002401 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002402 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002403 }else{
2404 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002405 }
drh5e00f6c2001-09-13 13:46:56 +00002406 break;
2407}
2408
drhe99fa2a2008-12-15 15:27:51 +00002409/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002410** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002411**
drhe99fa2a2008-12-15 15:27:51 +00002412** Interpret the content of register P1 as an integer. Store the
2413** ones-complement of the P1 value into register P2. If P1 holds
2414** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002415*/
drh93952eb2009-11-13 19:43:43 +00002416case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002417 pIn1 = &aMem[pOp->p1];
2418 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002419 sqlite3VdbeMemSetNull(pOut);
2420 if( (pIn1->flags & MEM_Null)==0 ){
2421 pOut->flags = MEM_Int;
2422 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002423 }
drhbf4133c2001-10-13 02:59:08 +00002424 break;
2425}
2426
drh48f2d3b2011-09-16 01:34:43 +00002427/* Opcode: Once P1 P2 * * *
2428**
drhab087d42017-03-24 17:59:56 +00002429** Fall through to the next instruction the first time this opcode is
2430** encountered on each invocation of the byte-code program. Jump to P2
2431** on the second and all subsequent encounters during the same invocation.
2432**
2433** Top-level programs determine first invocation by comparing the P1
2434** operand against the P1 operand on the OP_Init opcode at the beginning
2435** of the program. If the P1 values differ, then fall through and make
2436** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2437** the same then take the jump.
2438**
2439** For subprograms, there is a bitmask in the VdbeFrame that determines
2440** whether or not the jump should be taken. The bitmask is necessary
2441** because the self-altering code trick does not work for recursive
2442** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002443*/
dan1d8cb212011-12-09 13:24:16 +00002444case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002445 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002446 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002447 if( p->pFrame ){
2448 iAddr = (int)(pOp - p->aOp);
2449 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2450 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002451 goto jump_to_p2;
2452 }
drh18333ef2017-03-24 18:38:41 +00002453 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002454 }else{
drhab087d42017-03-24 17:59:56 +00002455 if( p->aOp[0].p1==pOp->p1 ){
2456 VdbeBranchTaken(1, 2);
2457 goto jump_to_p2;
2458 }
dan1d8cb212011-12-09 13:24:16 +00002459 }
drhab087d42017-03-24 17:59:56 +00002460 VdbeBranchTaken(0, 2);
2461 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002462 break;
2463}
2464
drh3c84ddf2008-01-09 02:15:38 +00002465/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002466**
drhef8662b2011-06-20 21:47:58 +00002467** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002468** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002469** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002470*/
drh1fcfa722018-02-26 15:27:31 +00002471case OP_If: { /* jump, in1 */
2472 int c;
2473 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2474 VdbeBranchTaken(c!=0, 2);
2475 if( c ) goto jump_to_p2;
2476 break;
2477}
2478
drh3c84ddf2008-01-09 02:15:38 +00002479/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002480**
drhef8662b2011-06-20 21:47:58 +00002481** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002482** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002483** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002484*/
drh9cbf3422008-01-17 16:22:13 +00002485case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002486 int c;
drh1fcfa722018-02-26 15:27:31 +00002487 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002488 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002489 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002490 break;
2491}
2492
drh830ecf92009-06-18 00:41:55 +00002493/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002494** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002495**
drh830ecf92009-06-18 00:41:55 +00002496** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002497*/
drh9cbf3422008-01-17 16:22:13 +00002498case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002499 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002500 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002501 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002502 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002503 }
drh477df4b2008-01-05 18:48:24 +00002504 break;
2505}
2506
drh98757152008-01-09 23:04:12 +00002507/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002508** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002509**
drh6a288a32008-01-07 19:20:24 +00002510** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002511*/
drh9cbf3422008-01-17 16:22:13 +00002512case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002513 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002514 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002515 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002516 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002517 }
drh5e00f6c2001-09-13 13:46:56 +00002518 break;
2519}
2520
drh31d6fd52017-04-14 19:03:10 +00002521/* Opcode: IfNullRow P1 P2 P3 * *
2522** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2523**
2524** Check the cursor P1 to see if it is currently pointing at a NULL row.
2525** If it is, then set register P3 to NULL and jump immediately to P2.
2526** If P1 is not on a NULL row, then fall through without making any
2527** changes.
2528*/
2529case OP_IfNullRow: { /* jump */
2530 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002531 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002532 if( p->apCsr[pOp->p1]->nullRow ){
2533 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2534 goto jump_to_p2;
2535 }
2536 break;
2537}
2538
drh092457b2017-12-29 15:04:49 +00002539#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2540/* Opcode: Offset P1 P2 P3 * *
2541** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002542**
drh092457b2017-12-29 15:04:49 +00002543** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002544** start of the payload for the record at which that cursor P1 is currently
2545** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002546**
drh092457b2017-12-29 15:04:49 +00002547** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002548** This opcode does not use P2 itself, but the P2 value is used by the
2549** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002550** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002551**
2552** This opcode is only available if SQLite is compiled with the
2553** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002554*/
drh092457b2017-12-29 15:04:49 +00002555case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002556 VdbeCursor *pC; /* The VDBE cursor */
2557 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2558 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002559 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002560 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002561 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002562 }else{
drh092457b2017-12-29 15:04:49 +00002563 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002564 }
2565 break;
2566}
drh092457b2017-12-29 15:04:49 +00002567#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002568
drh3e9ca092009-09-08 01:14:48 +00002569/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002570** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002571**
danielk1977cfcdaef2004-05-12 07:33:33 +00002572** Interpret the data that cursor P1 points to as a structure built using
2573** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002574** information about the format of the data.) Extract the P2-th column
2575** from this record. If there are less that (P2+1)
2576** values in the record, extract a NULL.
2577**
drh9cbf3422008-01-17 16:22:13 +00002578** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002579**
drh1cc3a362017-04-03 13:17:31 +00002580** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002581** if the P4 argument is a P4_MEM use the value of the P4 argument as
2582** the result.
drh3e9ca092009-09-08 01:14:48 +00002583**
drh1cc3a362017-04-03 13:17:31 +00002584** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002585** the result is guaranteed to only be used as the argument of a length()
2586** or typeof() function, respectively. The loading of large blobs can be
2587** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002588*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002589case OP_Column: {
drhabc38152020-07-22 13:38:04 +00002590 u32 p2; /* column number to retrieve */
drh856c1032009-06-02 15:21:42 +00002591 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002592 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002593 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002594 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002595 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002596 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002597 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002598 const u8 *zData; /* Part of the record being decoded */
2599 const u8 *zHdr; /* Next unparsed byte of the header */
2600 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002601 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002602 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002603 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002604
drh8c7715d2019-12-20 14:37:56 +00002605 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
dande892d92016-01-29 19:29:45 +00002606 pC = p->apCsr[pOp->p1];
drh8c7715d2019-12-20 14:37:56 +00002607 assert( pC!=0 );
drhabc38152020-07-22 13:38:04 +00002608 p2 = (u32)pOp->p2;
dande892d92016-01-29 19:29:45 +00002609
drh170ad682017-06-02 15:44:22 +00002610 /* If the cursor cache is stale (meaning it is not currently point at
2611 ** the correct row) then bring it up-to-date by doing the necessary
2612 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002613 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002614 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002615
drh9f6168b2016-03-19 23:32:58 +00002616 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002617 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002618 memAboutToChange(p, pDest);
danielk19776c924092007-11-12 08:09:34 +00002619 assert( pC!=0 );
mistachkincec5f1d2020-08-04 16:11:37 +00002620 assert( p2<(u32)pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002621 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002622 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002623 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2624 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002625
drha43a02e2016-05-19 17:51:19 +00002626 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002627 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002628 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002629 /* For the special case of as pseudo-cursor, the seekResult field
2630 ** identifies the register that holds the record */
2631 assert( pC->seekResult>0 );
2632 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002633 assert( pReg->flags & MEM_Blob );
2634 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002635 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002636 pC->aRow = (u8*)pReg->z;
2637 }else{
drh6b5631e2014-11-05 15:57:39 +00002638 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002639 goto op_column_out;
2640 }
danielk1977192ac1d2004-05-10 07:17:30 +00002641 }else{
drh06a09a82016-11-25 17:03:03 +00002642 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002643 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002644 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002645 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2646 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002647 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2648 assert( pC->szRow<=pC->payloadSize );
2649 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2650 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002651 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002652 }
danielk1977192ac1d2004-05-10 07:17:30 +00002653 }
drhb73857f2006-03-17 00:25:59 +00002654 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002655 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002656 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002657
drhc81aa2e2014-10-11 23:31:52 +00002658
drh1f613c42017-08-16 14:16:19 +00002659 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002660 /* pC->aRow does not have to hold the entire row, but it does at least
2661 ** need to cover the header of the record. If pC->aRow does not contain
2662 ** the complete header, then set it to zero, forcing the header to be
2663 ** dynamically allocated. */
2664 pC->aRow = 0;
2665 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002666
2667 /* Make sure a corrupt database has not given us an oversize header.
2668 ** Do this now to avoid an oversize memory allocation.
2669 **
2670 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2671 ** types use so much data space that there can only be 4096 and 32 of
2672 ** them, respectively. So the maximum header length results from a
2673 ** 3-byte type for each of the maximum of 32768 columns plus three
2674 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2675 */
drh1f613c42017-08-16 14:16:19 +00002676 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002677 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002678 }
drh95b225a2017-08-16 11:04:22 +00002679 }else{
2680 /* This is an optimization. By skipping over the first few tests
2681 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2682 ** measurable performance gain.
2683 **
drh1f613c42017-08-16 14:16:19 +00002684 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002685 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002686 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002687 ** branch jumps to reads past the end of the record, but never more
2688 ** than a few bytes. Even if the record occurs at the end of the page
2689 ** content area, the "page header" comes after the page content and so
2690 ** this overread is harmless. Similar overreads can occur for a corrupt
2691 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002692 */
2693 zData = pC->aRow;
2694 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002695 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002696 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002697 }
drh399af1d2013-11-20 17:25:55 +00002698 }
drh35cd6432009-06-05 14:17:21 +00002699
drh399af1d2013-11-20 17:25:55 +00002700 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002701 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002702 */
drhc8606e42013-11-20 19:28:03 +00002703 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002704 /* If there is more header available for parsing in the record, try
2705 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002706 */
drhc8606e42013-11-20 19:28:03 +00002707 if( pC->iHdrOffset<aOffset[0] ){
2708 /* Make sure zData points to enough of the record to cover the header. */
2709 if( pC->aRow==0 ){
2710 memset(&sMem, 0, sizeof(sMem));
drh2a740062020-02-05 18:28:17 +00002711 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
drh9467abf2016-02-17 18:44:11 +00002712 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002713 zData = (u8*)sMem.z;
2714 }else{
2715 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002716 }
drhc8606e42013-11-20 19:28:03 +00002717
drh0c8f7602014-09-19 16:56:45 +00002718 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002719 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002720 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002721 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002722 zHdr = zData + pC->iHdrOffset;
2723 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002724 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002725 do{
drhc332e042019-02-12 21:04:33 +00002726 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002727 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002728 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002729 }else{
drhc8606e42013-11-20 19:28:03 +00002730 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002731 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002732 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002733 }
drhc332e042019-02-12 21:04:33 +00002734 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drh8deae5a2020-07-29 12:23:20 +00002735 }while( (u32)i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002736
drh8dd83622014-10-13 23:39:02 +00002737 /* The record is corrupt if any of the following are true:
2738 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002739 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002740 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002741 */
drhc6ce38832015-10-15 21:30:24 +00002742 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2743 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002744 ){
drh95b225a2017-08-16 11:04:22 +00002745 if( aOffset[0]==0 ){
2746 i = 0;
2747 zHdr = zEndHdr;
2748 }else{
2749 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002750 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002751 }
danielk1977dedf45b2006-01-13 17:12:01 +00002752 }
drhddb2b4a2016-03-25 12:10:32 +00002753
drh170c2762016-05-20 21:40:11 +00002754 pC->nHdrParsed = i;
2755 pC->iHdrOffset = (u32)(zHdr - zData);
2756 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002757 }else{
drh9fbc8852016-01-04 03:48:46 +00002758 t = 0;
drh9188b382004-05-14 21:12:22 +00002759 }
drhd3194f52004-05-27 19:59:32 +00002760
drhf2db3382015-04-30 20:33:25 +00002761 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002762 ** still not up to p2, that means that the record has fewer than p2
2763 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002764 */
drhc8606e42013-11-20 19:28:03 +00002765 if( pC->nHdrParsed<=p2 ){
2766 if( pOp->p4type==P4_MEM ){
2767 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2768 }else{
drh22e8d832014-10-29 00:58:38 +00002769 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002770 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002771 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002772 }
drh95fa6062015-10-16 13:50:08 +00002773 }else{
2774 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002775 }
danielk1977192ac1d2004-05-10 07:17:30 +00002776
drh380d6852013-11-20 20:58:00 +00002777 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002778 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002779 ** all valid.
drh9188b382004-05-14 21:12:22 +00002780 */
drhc8606e42013-11-20 19:28:03 +00002781 assert( p2<pC->nHdrParsed );
2782 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002783 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002784 if( VdbeMemDynamic(pDest) ){
2785 sqlite3VdbeMemSetNull(pDest);
2786 }
drh95fa6062015-10-16 13:50:08 +00002787 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002788 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002789 /* This is the common case where the desired content fits on the original
2790 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002791 zData = pC->aRow + aOffset[p2];
2792 if( t<12 ){
2793 sqlite3VdbeSerialGet(zData, t, pDest);
2794 }else{
2795 /* If the column value is a string, we need a persistent value, not
2796 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2797 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2798 */
2799 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2800 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002801 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002802 if( pDest->szMalloc < len+2 ){
2803 pDest->flags = MEM_Null;
2804 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2805 }else{
2806 pDest->z = pDest->zMalloc;
2807 }
2808 memcpy(pDest->z, zData, len);
2809 pDest->z[len] = 0;
2810 pDest->z[len+1] = 0;
2811 pDest->flags = aFlag[t&1];
2812 }
danielk197736963fd2005-02-19 08:18:05 +00002813 }else{
drha1851ef2016-05-20 19:51:28 +00002814 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002815 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002816 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2817 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2818 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002819 ){
drh2a2a6962014-09-16 18:22:44 +00002820 /* Content is irrelevant for
2821 ** 1. the typeof() function,
2822 ** 2. the length(X) function if X is a blob, and
2823 ** 3. if the content length is zero.
2824 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002825 ** content from disk.
2826 **
2827 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2828 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
drhcbae3f82020-01-06 20:48:45 +00002829 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
2830 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
2831 ** and it begins with a bunch of zeros.
dan1f9144e2017-03-17 13:59:06 +00002832 */
drhcbae3f82020-01-06 20:48:45 +00002833 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002834 }else{
drhcb3cabd2016-11-25 19:18:28 +00002835 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002836 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2837 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2838 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002839 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002840 }
drhd3194f52004-05-27 19:59:32 +00002841
danielk19773c9cc8d2005-01-17 03:40:08 +00002842op_column_out:
drhb7654112008-01-12 12:48:07 +00002843 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002844 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002845 break;
drh74588ce2017-09-13 00:13:05 +00002846
2847op_column_corrupt:
2848 if( aOp[0].p3>0 ){
2849 pOp = &aOp[aOp[0].p3-1];
2850 break;
2851 }else{
2852 rc = SQLITE_CORRUPT_BKPT;
2853 goto abort_due_to_error;
2854 }
danielk1977192ac1d2004-05-10 07:17:30 +00002855}
2856
danielk1977751de562008-04-18 09:01:15 +00002857/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002858** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002859**
2860** Apply affinities to a range of P2 registers starting with P1.
2861**
drhbb6783b2017-04-29 18:02:49 +00002862** P4 is a string that is P2 characters long. The N-th character of the
2863** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002864** memory cell in the range.
2865*/
2866case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002867 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002868
drh856c1032009-06-02 15:21:42 +00002869 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002870 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002871 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002872 assert( zAffinity[pOp->p2]==0 );
2873 pIn1 = &aMem[pOp->p1];
drh122c5142019-07-29 05:23:01 +00002874 while( 1 /*exit-by-break*/ ){
drh9f6168b2016-03-19 23:32:58 +00002875 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drhb5f62432019-12-10 02:48:41 +00002876 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
drh83a1daf2019-05-01 18:59:33 +00002877 applyAffinity(pIn1, zAffinity[0], encoding);
2878 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
drh337cc392019-07-29 06:06:53 +00002879 /* When applying REAL affinity, if the result is still an MEM_Int
2880 ** that will fit in 6 bytes, then change the type to MEM_IntReal
2881 ** so that we keep the high-resolution integer value but know that
2882 ** the type really wants to be REAL. */
2883 testcase( pIn1->u.i==140737488355328LL );
2884 testcase( pIn1->u.i==140737488355327LL );
2885 testcase( pIn1->u.i==-140737488355328LL );
2886 testcase( pIn1->u.i==-140737488355329LL );
2887 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
2888 pIn1->flags |= MEM_IntReal;
2889 pIn1->flags &= ~MEM_Int;
2890 }else{
2891 pIn1->u.r = (double)pIn1->u.i;
2892 pIn1->flags |= MEM_Real;
2893 pIn1->flags &= ~MEM_Int;
2894 }
drh83a1daf2019-05-01 18:59:33 +00002895 }
drh6fcc1ec2019-05-01 14:41:47 +00002896 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
drh83a1daf2019-05-01 18:59:33 +00002897 zAffinity++;
2898 if( zAffinity[0]==0 ) break;
drh039fc322009-11-17 18:31:47 +00002899 pIn1++;
drh83a1daf2019-05-01 18:59:33 +00002900 }
danielk1977751de562008-04-18 09:01:15 +00002901 break;
2902}
2903
drh1db639c2008-01-17 02:36:28 +00002904/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002905** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002906**
drh710c4842010-08-30 01:17:20 +00002907** Convert P2 registers beginning with P1 into the [record format]
2908** use as a data record in a database table or as a key
2909** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002910**
drhbb6783b2017-04-29 18:02:49 +00002911** P4 may be a string that is P2 characters long. The N-th character of the
2912** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002913** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002914**
drh8a512562005-11-14 22:29:05 +00002915** The mapping from character to affinity is given by the SQLITE_AFF_
2916** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002917**
drh05883a32015-06-02 15:32:08 +00002918** If P4 is NULL then all index fields have the affinity BLOB.
drhda369332020-06-29 20:09:04 +00002919**
2920** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
2921** compile-time option is enabled:
2922**
2923** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
2924** of the right-most table that can be null-trimmed.
2925**
2926** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
2927** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
2928** accept no-change records with serial_type 10. This value is
2929** only used inside an assert() and does not affect the end result.
drh7f057c92005-06-24 03:53:06 +00002930*/
drh1db639c2008-01-17 02:36:28 +00002931case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002932 Mem *pRec; /* The new record */
2933 u64 nData; /* Number of bytes of data space */
2934 int nHdr; /* Number of bytes of header space */
2935 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002936 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002937 int nVarint; /* Number of bytes in a varint */
2938 u32 serial_type; /* Type field */
2939 Mem *pData0; /* First field to be combined into the record */
2940 Mem *pLast; /* Last field of the record */
2941 int nField; /* Number of fields in the record */
2942 char *zAffinity; /* The affinity string for the record */
2943 int file_format; /* File format to use for encoding */
drhbe37c122015-10-16 14:54:17 +00002944 u32 len; /* Length of a field */
drhb70b0df2019-04-30 01:08:42 +00002945 u8 *zHdr; /* Where to write next byte of the header */
2946 u8 *zPayload; /* Where to write next byte of the payload */
drh856c1032009-06-02 15:21:42 +00002947
drhf3218fe2004-05-28 08:21:02 +00002948 /* Assuming the record contains N fields, the record format looks
2949 ** like this:
2950 **
drh7a224de2004-06-02 01:22:02 +00002951 ** ------------------------------------------------------------------------
2952 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2953 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002954 **
drh9cbf3422008-01-17 16:22:13 +00002955 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002956 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002957 **
2958 ** Each type field is a varint representing the serial type of the
2959 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002960 ** hdr-size field is also a varint which is the offset from the beginning
2961 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002962 */
drh856c1032009-06-02 15:21:42 +00002963 nData = 0; /* Number of bytes of data space */
2964 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002965 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002966 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002967 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002968 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002969 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002970 nField = pOp->p2;
2971 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002972 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002973
drh2b4ded92010-09-27 21:09:31 +00002974 /* Identify the output register */
2975 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2976 pOut = &aMem[pOp->p3];
2977 memAboutToChange(p, pOut);
2978
drh3e6c0602013-12-10 20:53:01 +00002979 /* Apply the requested affinity to all inputs
2980 */
2981 assert( pData0<=pLast );
2982 if( zAffinity ){
2983 pRec = pData0;
2984 do{
drh5ad12512019-05-09 16:22:51 +00002985 applyAffinity(pRec, zAffinity[0], encoding);
danbe812622019-05-17 15:59:11 +00002986 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
2987 pRec->flags |= MEM_IntReal;
2988 pRec->flags &= ~(MEM_Int);
2989 }
drh5ad12512019-05-09 16:22:51 +00002990 REGISTER_TRACE((int)(pRec-aMem), pRec);
2991 zAffinity++;
2992 pRec++;
drh57bf4a82014-02-17 14:59:22 +00002993 assert( zAffinity[0]==0 || pRec<=pLast );
2994 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002995 }
2996
drhd447dce2017-01-25 20:55:11 +00002997#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002998 /* NULLs can be safely trimmed from the end of the record, as long as
2999 ** as the schema format is 2 or more and none of the omitted columns
3000 ** have a non-NULL default value. Also, the record must be left with
3001 ** at least one field. If P5>0 then it will be one more than the
3002 ** index of the right-most column with a non-NULL default value */
3003 if( pOp->p5 ){
3004 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
3005 pLast--;
3006 nField--;
3007 }
3008 }
drhd447dce2017-01-25 20:55:11 +00003009#endif
drh585ce192017-01-25 14:58:27 +00003010
drhf3218fe2004-05-28 08:21:02 +00003011 /* Loop through the elements that will make up the record to figure
drh76fd7be2019-07-11 19:50:18 +00003012 ** out how much space is required for the new record. After this loop,
3013 ** the Mem.uTemp field of each term should hold the serial-type that will
3014 ** be used for that term in the generated record:
3015 **
3016 ** Mem.uTemp value type
3017 ** --------------- ---------------
3018 ** 0 NULL
3019 ** 1 1-byte signed integer
3020 ** 2 2-byte signed integer
3021 ** 3 3-byte signed integer
3022 ** 4 4-byte signed integer
3023 ** 5 6-byte signed integer
3024 ** 6 8-byte signed integer
3025 ** 7 IEEE float
3026 ** 8 Integer constant 0
3027 ** 9 Integer constant 1
3028 ** 10,11 reserved for expansion
3029 ** N>=12 and even BLOB
3030 ** N>=13 and odd text
3031 **
3032 ** The following additional values are computed:
3033 ** nHdr Number of bytes needed for the record header
3034 ** nData Number of bytes of data space needed for the record
3035 ** nZero Zero bytes at the end of the record
danielk19778d059842004-05-12 11:24:02 +00003036 */
drh038b7bc2013-12-09 23:17:22 +00003037 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00003038 do{
drh2b4ded92010-09-27 21:09:31 +00003039 assert( memIsValid(pRec) );
drhc1da4392019-07-11 19:22:36 +00003040 if( pRec->flags & MEM_Null ){
3041 if( pRec->flags & MEM_Zero ){
drh41fb3672018-01-12 23:18:38 +00003042 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3043 ** table methods that never invoke sqlite3_result_xxxxx() while
3044 ** computing an unchanging column value in an UPDATE statement.
3045 ** Give such values a special internal-use-only serial-type of 10
3046 ** so that they can be passed through to xUpdate and have
3047 ** a true sqlite3_value_nochange(). */
drhda369332020-06-29 20:09:04 +00003048#ifndef SQLITE_ENABLE_NULL_TRIM
drh41fb3672018-01-12 23:18:38 +00003049 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
drhda369332020-06-29 20:09:04 +00003050#endif
drhc1da4392019-07-11 19:22:36 +00003051 pRec->uTemp = 10;
drh038b7bc2013-12-09 23:17:22 +00003052 }else{
drh76fd7be2019-07-11 19:50:18 +00003053 pRec->uTemp = 0;
drh038b7bc2013-12-09 23:17:22 +00003054 }
drhc1da4392019-07-11 19:22:36 +00003055 nHdr++;
3056 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
3057 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3058 i64 i = pRec->u.i;
drh9c3bb592019-07-30 21:00:13 +00003059 u64 uu;
drhc1da4392019-07-11 19:22:36 +00003060 testcase( pRec->flags & MEM_Int );
3061 testcase( pRec->flags & MEM_IntReal );
3062 if( i<0 ){
drh9c3bb592019-07-30 21:00:13 +00003063 uu = ~i;
drhc1da4392019-07-11 19:22:36 +00003064 }else{
drh9c3bb592019-07-30 21:00:13 +00003065 uu = i;
drhc1da4392019-07-11 19:22:36 +00003066 }
3067 nHdr++;
drh9c3bb592019-07-30 21:00:13 +00003068 testcase( uu==127 ); testcase( uu==128 );
3069 testcase( uu==32767 ); testcase( uu==32768 );
3070 testcase( uu==8388607 ); testcase( uu==8388608 );
3071 testcase( uu==2147483647 ); testcase( uu==2147483648 );
3072 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
3073 if( uu<=127 ){
drhc1da4392019-07-11 19:22:36 +00003074 if( (i&1)==i && file_format>=4 ){
drh9c3bb592019-07-30 21:00:13 +00003075 pRec->uTemp = 8+(u32)uu;
drhc1da4392019-07-11 19:22:36 +00003076 }else{
3077 nData++;
3078 pRec->uTemp = 1;
3079 }
drh9c3bb592019-07-30 21:00:13 +00003080 }else if( uu<=32767 ){
drhc1da4392019-07-11 19:22:36 +00003081 nData += 2;
3082 pRec->uTemp = 2;
drh9c3bb592019-07-30 21:00:13 +00003083 }else if( uu<=8388607 ){
drhc1da4392019-07-11 19:22:36 +00003084 nData += 3;
3085 pRec->uTemp = 3;
drh9c3bb592019-07-30 21:00:13 +00003086 }else if( uu<=2147483647 ){
drhc1da4392019-07-11 19:22:36 +00003087 nData += 4;
3088 pRec->uTemp = 4;
drh9c3bb592019-07-30 21:00:13 +00003089 }else if( uu<=140737488355327LL ){
drhc1da4392019-07-11 19:22:36 +00003090 nData += 6;
3091 pRec->uTemp = 5;
3092 }else{
3093 nData += 8;
3094 if( pRec->flags & MEM_IntReal ){
3095 /* If the value is IntReal and is going to take up 8 bytes to store
3096 ** as an integer, then we might as well make it an 8-byte floating
3097 ** point value */
3098 pRec->u.r = (double)pRec->u.i;
3099 pRec->flags &= ~MEM_IntReal;
3100 pRec->flags |= MEM_Real;
3101 pRec->uTemp = 7;
3102 }else{
3103 pRec->uTemp = 6;
3104 }
3105 }
3106 }else if( pRec->flags & MEM_Real ){
3107 nHdr++;
3108 nData += 8;
3109 pRec->uTemp = 7;
3110 }else{
3111 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
3112 assert( pRec->n>=0 );
3113 len = (u32)pRec->n;
3114 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
3115 if( pRec->flags & MEM_Zero ){
3116 serial_type += pRec->u.nZero*2;
3117 if( nData ){
3118 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
3119 len += pRec->u.nZero;
3120 }else{
3121 nZero += pRec->u.nZero;
3122 }
3123 }
3124 nData += len;
3125 nHdr += sqlite3VarintLen(serial_type);
3126 pRec->uTemp = serial_type;
drhfdf972a2007-05-02 13:30:27 +00003127 }
drh45c3c662016-04-07 14:16:16 +00003128 if( pRec==pData0 ) break;
3129 pRec--;
3130 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00003131
drh654858d2014-11-20 02:18:14 +00003132 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3133 ** which determines the total number of bytes in the header. The varint
3134 ** value is the size of the header in bytes including the size varint
3135 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00003136 testcase( nHdr==126 );
3137 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00003138 if( nHdr<=126 ){
3139 /* The common case */
3140 nHdr += 1;
3141 }else{
3142 /* Rare case of a really large header */
3143 nVarint = sqlite3VarintLen(nHdr);
3144 nHdr += nVarint;
3145 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00003146 }
drh038b7bc2013-12-09 23:17:22 +00003147 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00003148
danielk1977a7a8e142008-02-13 18:25:27 +00003149 /* Make sure the output register has a buffer large enough to store
3150 ** the new record. The output register (pOp->p3) is not allowed to
3151 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00003152 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00003153 */
drh0d7f0cc2018-09-21 13:07:14 +00003154 if( nByte+nZero<=pOut->szMalloc ){
3155 /* The output register is already large enough to hold the record.
3156 ** No error checks or buffer enlargement is required */
3157 pOut->z = pOut->zMalloc;
3158 }else{
3159 /* Need to make sure that the output is not too big and then enlarge
3160 ** the output register to hold the full result */
3161 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
3162 goto too_big;
3163 }
3164 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
3165 goto no_mem;
3166 }
danielk19778d059842004-05-12 11:24:02 +00003167 }
drh9c1905f2008-12-10 22:32:56 +00003168 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00003169 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00003170 if( nZero ){
drh8df32842008-12-09 02:51:23 +00003171 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00003172 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00003173 }
drhb7654112008-01-12 12:48:07 +00003174 UPDATE_MAX_BLOBSIZE(pOut);
drhb70b0df2019-04-30 01:08:42 +00003175 zHdr = (u8 *)pOut->z;
3176 zPayload = zHdr + nHdr;
3177
3178 /* Write the record */
3179 zHdr += putVarint32(zHdr, nHdr);
3180 assert( pData0<=pLast );
3181 pRec = pData0;
3182 do{
3183 serial_type = pRec->uTemp;
3184 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3185 ** additional varints, one per column. */
3186 zHdr += putVarint32(zHdr, serial_type); /* serial type */
3187 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
3188 ** immediately follow the header. */
3189 zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
3190 }while( (++pRec)<=pLast );
3191 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
3192 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
3193
3194 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
3195 REGISTER_TRACE(pOp->p3, pOut);
danielk19778d059842004-05-12 11:24:02 +00003196 break;
3197}
3198
drh9f274632020-03-17 17:11:23 +00003199/* Opcode: Count P1 P2 p3 * *
drh81316f82013-10-29 20:40:47 +00003200** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00003201**
3202** Store the number of entries (an integer value) in the table or index
drh9f274632020-03-17 17:11:23 +00003203** opened by cursor P1 in register P2.
3204**
3205** If P3==0, then an exact count is obtained, which involves visiting
3206** every btree page of the table. But if P3 is non-zero, an estimate
3207** is returned based on the current cursor position.
danielk1977a5533162009-02-24 10:01:51 +00003208*/
drh27a348c2015-04-13 19:14:06 +00003209case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00003210 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00003211 BtCursor *pCrsr;
3212
drhc960dcb2015-11-20 19:22:01 +00003213 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
3214 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00003215 assert( pCrsr );
drh9f274632020-03-17 17:11:23 +00003216 if( pOp->p3 ){
3217 nEntry = sqlite3BtreeRowCountEst(pCrsr);
3218 }else{
3219 nEntry = 0; /* Not needed. Only used to silence a warning. */
3220 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
3221 if( rc ) goto abort_due_to_error;
3222 }
drh27a348c2015-04-13 19:14:06 +00003223 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00003224 pOut->u.i = nEntry;
drh21f6daa2019-10-11 14:21:48 +00003225 goto check_for_interrupt;
danielk1977a5533162009-02-24 10:01:51 +00003226}
danielk1977a5533162009-02-24 10:01:51 +00003227
danielk1977fd7f0452008-12-17 17:30:26 +00003228/* Opcode: Savepoint P1 * * P4 *
3229**
3230** Open, release or rollback the savepoint named by parameter P4, depending
drh2ce9b6b2019-05-10 14:03:07 +00003231** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3232** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3233** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
danielk1977fd7f0452008-12-17 17:30:26 +00003234*/
3235case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00003236 int p1; /* Value of P1 operand */
3237 char *zName; /* Name of savepoint */
3238 int nName;
3239 Savepoint *pNew;
3240 Savepoint *pSavepoint;
3241 Savepoint *pTmp;
3242 int iSavepoint;
3243 int ii;
3244
3245 p1 = pOp->p1;
3246 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003247
3248 /* Assert that the p1 parameter is valid. Also that if there is no open
3249 ** transaction, then there cannot be any savepoints.
3250 */
3251 assert( db->pSavepoint==0 || db->autoCommit==0 );
3252 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3253 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3254 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003255 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003256
3257 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003258 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003259 /* A new savepoint cannot be created if there are active write
3260 ** statements (i.e. open read/write incremental blob handles).
3261 */
drh22c17b82015-05-15 04:13:15 +00003262 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003263 rc = SQLITE_BUSY;
3264 }else{
drh856c1032009-06-02 15:21:42 +00003265 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003266
drhbe07ec52011-06-03 12:15:26 +00003267#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003268 /* This call is Ok even if this savepoint is actually a transaction
3269 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3270 ** If this is a transaction savepoint being opened, it is guaranteed
3271 ** that the db->aVTrans[] array is empty. */
3272 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003273 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3274 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003275 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003276#endif
dand9495cd2011-04-27 12:08:04 +00003277
danielk1977fd7f0452008-12-17 17:30:26 +00003278 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003279 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003280 if( pNew ){
3281 pNew->zName = (char *)&pNew[1];
3282 memcpy(pNew->zName, zName, nName+1);
3283
3284 /* If there is no open transaction, then mark this as a special
3285 ** "transaction savepoint". */
3286 if( db->autoCommit ){
3287 db->autoCommit = 0;
3288 db->isTransactionSavepoint = 1;
3289 }else{
3290 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003291 }
dan21e8d012011-03-03 20:05:59 +00003292
danielk1977fd7f0452008-12-17 17:30:26 +00003293 /* Link the new savepoint into the database handle's list. */
3294 pNew->pNext = db->pSavepoint;
3295 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003296 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003297 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003298 }
3299 }
3300 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003301 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
drh856c1032009-06-02 15:21:42 +00003302 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003303
3304 /* Find the named savepoint. If there is no such savepoint, then an
3305 ** an error is returned to the user. */
3306 for(
drh856c1032009-06-02 15:21:42 +00003307 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003308 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003309 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003310 ){
3311 iSavepoint++;
3312 }
3313 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003314 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003315 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003316 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003317 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003318 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003319 */
drh22c17b82015-05-15 04:13:15 +00003320 sqlite3VdbeError(p, "cannot release savepoint - "
3321 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003322 rc = SQLITE_BUSY;
3323 }else{
3324
3325 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003326 ** and this is a RELEASE command, then the current transaction
3327 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003328 */
3329 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3330 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003331 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003332 goto vdbe_return;
3333 }
danielk1977fd7f0452008-12-17 17:30:26 +00003334 db->autoCommit = 1;
3335 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003336 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003337 db->autoCommit = 0;
3338 p->rc = rc = SQLITE_BUSY;
3339 goto vdbe_return;
3340 }
danielk197734cf35d2008-12-18 18:31:38 +00003341 rc = p->rc;
drh94649b62019-12-18 02:12:04 +00003342 if( rc ){
3343 db->autoCommit = 0;
3344 }else{
3345 db->isTransactionSavepoint = 0;
3346 }
danielk1977fd7f0452008-12-17 17:30:26 +00003347 }else{
drh47b7fc72014-11-11 01:33:57 +00003348 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003349 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003350 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003351 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003352 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003353 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3354 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003355 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003356 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003357 }
drh47b7fc72014-11-11 01:33:57 +00003358 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003359 assert( p1==SAVEPOINT_RELEASE );
drh47b7fc72014-11-11 01:33:57 +00003360 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003361 }
3362 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003363 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3364 if( rc!=SQLITE_OK ){
3365 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003366 }
danielk1977fd7f0452008-12-17 17:30:26 +00003367 }
drh47b7fc72014-11-11 01:33:57 +00003368 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003369 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003370 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003371 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003372 }
3373 }
drh95866af2019-12-15 00:36:33 +00003374 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003375
3376 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3377 ** savepoints nested inside of the savepoint being operated on. */
3378 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003379 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003380 db->pSavepoint = pTmp->pNext;
3381 sqlite3DbFree(db, pTmp);
3382 db->nSavepoint--;
3383 }
3384
dan1da40a32009-09-19 17:00:31 +00003385 /* If it is a RELEASE, then destroy the savepoint being operated on
3386 ** too. If it is a ROLLBACK TO, then set the number of deferred
3387 ** constraint violations present in the database to the value stored
3388 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003389 if( p1==SAVEPOINT_RELEASE ){
3390 assert( pSavepoint==db->pSavepoint );
3391 db->pSavepoint = pSavepoint->pNext;
3392 sqlite3DbFree(db, pSavepoint);
3393 if( !isTransaction ){
3394 db->nSavepoint--;
3395 }
dan1da40a32009-09-19 17:00:31 +00003396 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003397 assert( p1==SAVEPOINT_ROLLBACK );
dan1da40a32009-09-19 17:00:31 +00003398 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003399 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003400 }
dand9495cd2011-04-27 12:08:04 +00003401
danea8562e2015-04-18 16:25:54 +00003402 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003403 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3404 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3405 }
danielk1977fd7f0452008-12-17 17:30:26 +00003406 }
3407 }
drh9467abf2016-02-17 18:44:11 +00003408 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003409
3410 break;
3411}
3412
drh98757152008-01-09 23:04:12 +00003413/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003414**
3415** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003416** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003417** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3418** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003419**
3420** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003421*/
drh9cbf3422008-01-17 16:22:13 +00003422case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003423 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003424 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003425
drh856c1032009-06-02 15:21:42 +00003426 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003427 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003428 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003429 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003430 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003431 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003432
drhb0c88652016-02-01 13:21:13 +00003433 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003434 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003435 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003436 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003437 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003438 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3439 /* If this instruction implements a COMMIT and other VMs are writing
3440 ** return an error indicating that the other VMs must complete first.
3441 */
3442 sqlite3VdbeError(p, "cannot commit transaction - "
3443 "SQL statements in progress");
3444 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003445 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003446 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003447 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003448 }else{
shane7d3846a2008-12-11 02:58:26 +00003449 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003450 }
3451 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3452 p->pc = (int)(pOp - aOp);
3453 db->autoCommit = (u8)(1-desiredAutoCommit);
3454 p->rc = rc = SQLITE_BUSY;
3455 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003456 }
danielk1977fd7f0452008-12-17 17:30:26 +00003457 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003458 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003459 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003460 }else{
drh900b31e2007-08-28 02:27:51 +00003461 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003462 }
drh900b31e2007-08-28 02:27:51 +00003463 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003464 }else{
drh22c17b82015-05-15 04:13:15 +00003465 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003466 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003467 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003468 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003469
3470 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003471 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003472 }
drh8616cff2019-07-13 16:15:23 +00003473 /*NOTREACHED*/ assert(0);
drh663fc632002-02-02 18:49:19 +00003474}
3475
drhb22f7c82014-02-06 23:56:27 +00003476/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003477**
drh05a86c52014-02-16 01:55:49 +00003478** Begin a transaction on database P1 if a transaction is not already
3479** active.
3480** If P2 is non-zero, then a write-transaction is started, or if a
3481** read-transaction is already active, it is upgraded to a write-transaction.
drh1ca037f2020-10-12 13:24:00 +00003482** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3483** then an exclusive transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003484**
drh001bbcb2003-03-19 03:14:00 +00003485** P1 is the index of the database file on which the transaction is
3486** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003487** file used for temporary tables. Indices of 2 or more are used for
3488** attached databases.
drhcabb0812002-09-14 13:47:32 +00003489**
dane0af83a2009-09-08 19:15:01 +00003490** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3491** true (this flag is set if the Vdbe may modify more than one row and may
3492** throw an ABORT exception), a statement transaction may also be opened.
3493** More specifically, a statement transaction is opened iff the database
3494** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003495** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003496** VDBE to be rolled back after an error without having to roll back the
3497** entire transaction. If no error is encountered, the statement transaction
3498** will automatically commit when the VDBE halts.
3499**
drhb22f7c82014-02-06 23:56:27 +00003500** If P5!=0 then this opcode also checks the schema cookie against P3
3501** and the schema generation counter against P4.
3502** The cookie changes its value whenever the database schema changes.
3503** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003504** and that the current process needs to reread the schema. If the schema
3505** cookie in P3 differs from the schema cookie in the database header or
3506** if the schema generation counter in P4 differs from the current
3507** generation counter, then an SQLITE_SCHEMA error is raised and execution
3508** halts. The sqlite3_step() wrapper function might then reprepare the
3509** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003510*/
drh9cbf3422008-01-17 16:22:13 +00003511case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003512 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003513 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003514
drh1713afb2013-06-28 01:24:57 +00003515 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003516 assert( p->readOnly==0 || pOp->p2==0 );
drh1ca037f2020-10-12 13:24:00 +00003517 assert( pOp->p2>=0 && pOp->p2<=2 );
drh653b82a2009-06-22 11:10:47 +00003518 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003519 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003520 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3521 rc = SQLITE_READONLY;
3522 goto abort_due_to_error;
3523 }
drh653b82a2009-06-22 11:10:47 +00003524 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003525
danielk197724162fe2004-06-04 06:22:00 +00003526 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003527 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003528 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3529 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003530 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003531 if( (rc&0xff)==SQLITE_BUSY ){
3532 p->pc = (int)(pOp - aOp);
3533 p->rc = rc;
3534 goto vdbe_return;
3535 }
danielk197724162fe2004-06-04 06:22:00 +00003536 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003537 }
dane0af83a2009-09-08 19:15:01 +00003538
drh4d294482019-10-05 15:28:24 +00003539 if( p->usesStmtJournal
3540 && pOp->p2
danc0537fe2013-06-28 19:41:43 +00003541 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003542 ){
drh99744fa2020-08-25 19:09:07 +00003543 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE );
dane0af83a2009-09-08 19:15:01 +00003544 if( p->iStatement==0 ){
3545 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3546 db->nStatement++;
3547 p->iStatement = db->nSavepoint + db->nStatement;
3548 }
dana311b802011-04-26 19:21:34 +00003549
drh346506f2011-05-25 01:16:42 +00003550 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003551 if( rc==SQLITE_OK ){
3552 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3553 }
dan1da40a32009-09-19 17:00:31 +00003554
3555 /* Store the current value of the database handles deferred constraint
3556 ** counter. If the statement transaction needs to be rolled back,
3557 ** the value of this counter needs to be restored too. */
3558 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003559 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003560 }
drh397776a2018-06-06 17:45:51 +00003561 }
3562 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3563 if( pOp->p5
3564 && (iMeta!=pOp->p3
3565 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3566 ){
dand2ffc972020-12-10 19:20:15 +00003567 /*
3568 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3569 ** version is checked to ensure that the schema has not changed since the
3570 ** SQL statement was prepared.
3571 */
3572 sqlite3DbFree(db, p->zErrMsg);
3573 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
drhb22f7c82014-02-06 23:56:27 +00003574 /* If the schema-cookie from the database file matches the cookie
3575 ** stored with the in-memory representation of the schema, do
3576 ** not reload the schema from the database file.
3577 **
3578 ** If virtual-tables are in use, this is not just an optimization.
3579 ** Often, v-tables store their data in other SQLite tables, which
3580 ** are queried from within xNext() and other v-table methods using
3581 ** prepared queries. If such a query is out-of-date, we do not want to
3582 ** discard the database schema, as the user code implementing the
3583 ** v-table would have to be ready for the sqlite3_vtab structure itself
3584 ** to be invalidated whenever sqlite3_step() is called from within
3585 ** a v-table method.
3586 */
3587 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3588 sqlite3ResetOneSchema(db, pOp->p1);
3589 }
3590 p->expired = 1;
3591 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003592 }
drh9467abf2016-02-17 18:44:11 +00003593 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003594 break;
3595}
3596
drhb1fdb2a2008-01-05 04:06:03 +00003597/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003598**
drh9cbf3422008-01-17 16:22:13 +00003599** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003600** P3==1 is the schema version. P3==2 is the database format.
3601** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003602** the main database file and P1==1 is the database file used to store
3603** temporary tables.
drh4a324312001-12-21 14:30:42 +00003604**
drh50e5dad2001-09-15 00:57:28 +00003605** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003606** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003607** executing this instruction.
3608*/
drh27a348c2015-04-13 19:14:06 +00003609case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003610 int iMeta;
drh856c1032009-06-02 15:21:42 +00003611 int iDb;
3612 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003613
drh1713afb2013-06-28 01:24:57 +00003614 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003615 iDb = pOp->p1;
3616 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003617 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003618 assert( iDb>=0 && iDb<db->nDb );
3619 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003620 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003621
danielk1977602b4662009-07-02 07:47:33 +00003622 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003623 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003624 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003625 break;
3626}
3627
drhe3863b52020-07-01 16:19:14 +00003628/* Opcode: SetCookie P1 P2 P3 * P5
drh50e5dad2001-09-15 00:57:28 +00003629**
drh1861afc2016-02-01 21:48:34 +00003630** Write the integer value P3 into cookie number P2 of database P1.
3631** P2==1 is the schema version. P2==2 is the database format.
3632** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003633** size, and so forth. P1==0 is the main database file and P1==1 is the
3634** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003635**
3636** A transaction must be started before executing this opcode.
drhe3863b52020-07-01 16:19:14 +00003637**
3638** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
3639** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
3640** has P5 set to 1, so that the internal schema version will be different
3641** from the database schema version, resulting in a schema reset.
drh50e5dad2001-09-15 00:57:28 +00003642*/
drh1861afc2016-02-01 21:48:34 +00003643case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003644 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003645
3646 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003647 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003648 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003649 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003650 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003651 pDb = &db->aDb[pOp->p1];
3652 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003653 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003654 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003655 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003656 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003657 /* When the schema cookie changes, record the new cookie internally */
drhe3863b52020-07-01 16:19:14 +00003658 pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5;
drh8257aa82017-07-26 19:59:13 +00003659 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003660 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003661 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003662 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003663 }
drhfd426c62006-01-30 15:34:22 +00003664 if( pOp->p1==1 ){
3665 /* Invalidate all prepared statements whenever the TEMP database
3666 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003667 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003668 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003669 }
drh9467abf2016-02-17 18:44:11 +00003670 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003671 break;
3672}
3673
drh98757152008-01-09 23:04:12 +00003674/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003675** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003676**
drhecdc7532001-09-23 02:35:53 +00003677** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003678** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003679** P3==0 means the main database, P3==1 means the database used for
3680** temporary tables, and P3>1 means used the corresponding attached
3681** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003682** values need not be contiguous but all P1 values should be small integers.
3683** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003684**
drh8e9deb62018-06-05 13:43:02 +00003685** Allowed P5 bits:
3686** <ul>
3687** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3688** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003689** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003690** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003691**
danielk1977d336e222009-02-20 10:58:41 +00003692** The P4 value may be either an integer (P4_INT32) or a pointer to
3693** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003694** object, then table being opened must be an [index b-tree] where the
3695** KeyInfo object defines the content and collating
3696** sequence of that index b-tree. Otherwise, if P4 is an integer
3697** value, then the table being opened must be a [table b-tree] with a
3698** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003699**
drh35263192014-07-22 20:02:19 +00003700** See also: OpenWrite, ReopenIdx
3701*/
3702/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3703** Synopsis: root=P2 iDb=P3
3704**
drh8e9deb62018-06-05 13:43:02 +00003705** The ReopenIdx opcode works like OP_OpenRead except that it first
3706** checks to see if the cursor on P1 is already open on the same
3707** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003708** if the cursor is already open, do not reopen it.
3709**
drh8e9deb62018-06-05 13:43:02 +00003710** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3711** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3712** be the same as every other ReopenIdx or OpenRead for the same cursor
3713** number.
drh35263192014-07-22 20:02:19 +00003714**
drh8e9deb62018-06-05 13:43:02 +00003715** Allowed P5 bits:
3716** <ul>
3717** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3718** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003719** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003720** </ul>
3721**
3722** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003723*/
drh98757152008-01-09 23:04:12 +00003724/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003725** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003726**
3727** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003728** page is P2 (or whose root page is held in register P2 if the
3729** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003730**
danielk1977d336e222009-02-20 10:58:41 +00003731** The P4 value may be either an integer (P4_INT32) or a pointer to
3732** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003733** object, then table being opened must be an [index b-tree] where the
3734** KeyInfo object defines the content and collating
3735** sequence of that index b-tree. Otherwise, if P4 is an integer
3736** value, then the table being opened must be a [table b-tree] with a
3737** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003738**
drh8e9deb62018-06-05 13:43:02 +00003739** Allowed P5 bits:
3740** <ul>
3741** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3742** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003743** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003744** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3745** and subsequently delete entries in an index btree. This is a
3746** hint to the storage engine that the storage engine is allowed to
3747** ignore. The hint is not used by the official SQLite b*tree storage
3748** engine, but is used by COMDB2.
3749** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3750** as the root page, not the value of P2 itself.
3751** </ul>
drhf57b3392001-10-08 13:22:32 +00003752**
drh8e9deb62018-06-05 13:43:02 +00003753** This instruction works like OpenRead except that it opens the cursor
3754** in read/write mode.
3755**
3756** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003757*/
drh35263192014-07-22 20:02:19 +00003758case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003759 int nField;
3760 KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00003761 u32 p2;
drh856c1032009-06-02 15:21:42 +00003762 int iDb;
drhf57b3392001-10-08 13:22:32 +00003763 int wrFlag;
3764 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003765 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003766 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003767
drhe0997b32015-03-20 14:57:50 +00003768 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003769 assert( pOp->p4type==P4_KEYINFO );
3770 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003771 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003772 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003773 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003774 }
3775 /* If the cursor is not currently open or is open on a different
3776 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003777case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003778case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003779
drhe0997b32015-03-20 14:57:50 +00003780 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003781 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003782 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3783 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003784
drhba968db2018-07-24 22:02:12 +00003785 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003786 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003787 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003788 }
3789
drh856c1032009-06-02 15:21:42 +00003790 nField = 0;
3791 pKeyInfo = 0;
drhabc38152020-07-22 13:38:04 +00003792 p2 = (u32)pOp->p2;
drh856c1032009-06-02 15:21:42 +00003793 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003794 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003795 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003796 pDb = &db->aDb[iDb];
3797 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003798 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003799 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003800 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3801 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003802 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003803 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3804 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003805 }
3806 }else{
3807 wrFlag = 0;
3808 }
dan428c2182012-08-06 18:50:11 +00003809 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003810 assert( p2>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00003811 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003812 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003813 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003814 assert( memIsValid(pIn2) );
3815 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003816 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003817 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003818 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003819 ** that opcode will always set the p2 value to 2 or more or else fail.
3820 ** If there were a failure, the prepared statement would have halted
3821 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003822 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003823 }
danielk1977d336e222009-02-20 10:58:41 +00003824 if( pOp->p4type==P4_KEYINFO ){
3825 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003826 assert( pKeyInfo->enc==ENC(db) );
3827 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003828 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003829 }else if( pOp->p4type==P4_INT32 ){
3830 nField = pOp->p4.i;
3831 }
drh653b82a2009-06-22 11:10:47 +00003832 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003833 assert( nField>=0 );
3834 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003835 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003836 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003837 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003838 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003839 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003840#ifdef SQLITE_DEBUG
3841 pCur->wrFlag = wrFlag;
3842#endif
drhc960dcb2015-11-20 19:22:01 +00003843 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003844 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003845 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003846 ** SQLite used to check if the root-page flags were sane at this point
3847 ** and report database corruption if they were not, but this check has
3848 ** since moved into the btree layer. */
3849 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003850
3851open_cursor_set_hints:
3852 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3853 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003854 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh0403cb32015-08-14 23:57:04 +00003855 testcase( pOp->p2 & OPFLAG_SEEKEQ );
drhc960dcb2015-11-20 19:22:01 +00003856 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003857 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003858 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003859 break;
3860}
3861
drhe08e8d62017-05-01 15:15:41 +00003862/* Opcode: OpenDup P1 P2 * * *
3863**
3864** Open a new cursor P1 that points to the same ephemeral table as
3865** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3866** opcode. Only ephemeral cursors may be duplicated.
3867**
3868** Duplicate ephemeral cursors are used for self-joins of materialized views.
3869*/
3870case OP_OpenDup: {
3871 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3872 VdbeCursor *pCx; /* The new cursor */
3873
3874 pOrig = p->apCsr[pOp->p2];
dan2811ea62019-12-23 14:20:46 +00003875 assert( pOrig );
drhe08e8d62017-05-01 15:15:41 +00003876 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3877
3878 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3879 if( pCx==0 ) goto no_mem;
3880 pCx->nullRow = 1;
3881 pCx->isEphemeral = 1;
3882 pCx->pKeyInfo = pOrig->pKeyInfo;
3883 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00003884 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00003885 pCx->isOrdered = pOrig->isOrdered;
drh2c041312018-12-24 02:34:49 +00003886 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00003887 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003888 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3889 ** opened for a database. Since there is already an open cursor when this
3890 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3891 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003892 break;
3893}
3894
3895
drh32881be2020-11-17 21:26:13 +00003896/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003897** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003898**
drhb9bb7c12006-06-11 23:41:55 +00003899** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003900** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003901** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003902** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003903**
drhdfe3b582019-01-04 12:35:50 +00003904** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00003905** is cleared (all content is erased).
3906**
drh25d3adb2010-04-05 15:11:08 +00003907** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003908** The cursor points to a BTree table if P4==0 and to a BTree index
3909** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003910** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003911**
drh2a5d9902011-08-26 00:34:45 +00003912** The P5 parameter can be a mask of the BTREE_* flags defined
3913** in btree.h. These flags control aspects of the operation of
3914** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3915** added automatically.
drh32881be2020-11-17 21:26:13 +00003916**
3917** If P3 is positive, then reg[P3] is modified slightly so that it
3918** can be used as zero-length data for OP_Insert. This is an optimization
3919** that avoids an extra OP_Blob opcode to initialize that register.
drh5e00f6c2001-09-13 13:46:56 +00003920*/
drha21a64d2010-04-06 22:33:55 +00003921/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003922** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003923**
3924** This opcode works the same as OP_OpenEphemeral. It has a
3925** different name to distinguish its use. Tables created using
3926** by this opcode will be used for automatically created transient
3927** indices in joins.
3928*/
3929case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003930case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003931 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003932 KeyInfo *pKeyInfo;
3933
drhd4187c72010-08-30 22:15:45 +00003934 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003935 SQLITE_OPEN_READWRITE |
3936 SQLITE_OPEN_CREATE |
3937 SQLITE_OPEN_EXCLUSIVE |
3938 SQLITE_OPEN_DELETEONCLOSE |
3939 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003940 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003941 assert( pOp->p2>=0 );
drh32881be2020-11-17 21:26:13 +00003942 if( pOp->p3>0 ){
3943 /* Make register reg[P3] into a value that can be used as the data
3944 ** form sqlite3BtreeInsert() where the length of the data is zero. */
3945 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
3946 assert( pOp->opcode==OP_OpenEphemeral );
3947 assert( aMem[pOp->p3].flags & MEM_Null );
3948 aMem[pOp->p3].n = 0;
3949 aMem[pOp->p3].z = "";
3950 }
drh4afdfa12018-12-31 16:36:42 +00003951 pCx = p->apCsr[pOp->p1];
drh606411b2021-02-22 14:25:40 +00003952 if( pCx && ALWAYS(pCx->pBtx) ){
drh4afdfa12018-12-31 16:36:42 +00003953 /* If the ephermeral table is already open, erase all existing content
3954 ** so that the table is empty again, rather than creating a new table. */
dana5129722019-05-03 18:50:24 +00003955 assert( pCx->isEphemeral );
dan855b5d12019-06-26 21:04:30 +00003956 pCx->seqCount = 0;
3957 pCx->cacheStatus = CACHE_STALE;
drh1ee02a12020-01-18 13:53:46 +00003958 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00003959 }else{
3960 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3961 if( pCx==0 ) goto no_mem;
drhd0fb7962018-12-31 17:58:05 +00003962 pCx->isEphemeral = 1;
3963 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3964 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
3965 vfsFlags);
3966 if( rc==SQLITE_OK ){
3967 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drhc6b52df2002-01-04 03:09:29 +00003968 }
drhd0fb7962018-12-31 17:58:05 +00003969 if( rc==SQLITE_OK ){
3970 /* If a transient index is required, create it by calling
3971 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3972 ** opening it. If a transient table is required, just use the
3973 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3974 */
3975 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3976 assert( pOp->p4type==P4_KEYINFO );
drhabc38152020-07-22 13:38:04 +00003977 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pCx->pgnoRoot,
drhd0fb7962018-12-31 17:58:05 +00003978 BTREE_BLOBKEY | pOp->p5);
3979 if( rc==SQLITE_OK ){
drh346a70c2020-06-15 20:27:35 +00003980 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
drhd0fb7962018-12-31 17:58:05 +00003981 assert( pKeyInfo->db==db );
3982 assert( pKeyInfo->enc==ENC(db) );
3983 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
3984 pKeyInfo, pCx->uc.pCursor);
3985 }
3986 pCx->isTable = 0;
3987 }else{
drh346a70c2020-06-15 20:27:35 +00003988 pCx->pgnoRoot = SCHEMA_ROOT;
3989 rc = sqlite3BtreeCursor(pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR,
drhd0fb7962018-12-31 17:58:05 +00003990 0, pCx->uc.pCursor);
3991 pCx->isTable = 1;
3992 }
3993 }
3994 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drh5e00f6c2001-09-13 13:46:56 +00003995 }
drh9467abf2016-02-17 18:44:11 +00003996 if( rc ) goto abort_due_to_error;
dan855b5d12019-06-26 21:04:30 +00003997 pCx->nullRow = 1;
dan5134d132011-09-02 10:31:11 +00003998 break;
3999}
4000
danfad9f9a2014-04-01 18:41:51 +00004001/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00004002**
4003** This opcode works like OP_OpenEphemeral except that it opens
4004** a transient index that is specifically designed to sort large
4005** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00004006**
4007** If argument P3 is non-zero, then it indicates that the sorter may
4008** assume that a stable sort considering the first P3 fields of each
4009** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00004010*/
drhca892a72011-09-03 00:17:51 +00004011case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00004012 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00004013
drh399af1d2013-11-20 17:25:55 +00004014 assert( pOp->p1>=0 );
4015 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00004016 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00004017 if( pCx==0 ) goto no_mem;
4018 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00004019 assert( pCx->pKeyInfo->db==db );
4020 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00004021 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00004022 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004023 break;
4024}
4025
dan78d58432014-03-25 15:04:07 +00004026/* Opcode: SequenceTest P1 P2 * * *
4027** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4028**
4029** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4030** to P2. Regardless of whether or not the jump is taken, increment the
4031** the sequence value.
4032*/
4033case OP_SequenceTest: {
4034 VdbeCursor *pC;
4035 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4036 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004037 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00004038 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00004039 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00004040 }
drh5e00f6c2001-09-13 13:46:56 +00004041 break;
4042}
4043
drh5f612292014-02-08 23:20:32 +00004044/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00004045** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00004046**
4047** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00004048** row of data. The content of that one row is the content of memory
4049** register P2. In other words, cursor P1 becomes an alias for the
4050** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00004051**
drh2d8d7ce2010-02-15 15:17:05 +00004052** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00004053** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00004054** individual columns using the OP_Column opcode. The OP_Column opcode
4055** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00004056**
4057** P3 is the number of fields in the records that will be stored by
4058** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004059*/
drh9cbf3422008-01-17 16:22:13 +00004060case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00004061 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00004062
drh653b82a2009-06-22 11:10:47 +00004063 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004064 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00004065 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00004066 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00004067 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004068 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00004069 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004070 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4071 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4072 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4073 ** which is a performance optimization */
4074 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00004075 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00004076 break;
4077}
4078
drh98757152008-01-09 23:04:12 +00004079/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00004080**
4081** Close a cursor previously opened as P1. If P1 is not
4082** currently open, this instruction is a no-op.
4083*/
drh9cbf3422008-01-17 16:22:13 +00004084case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00004085 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4086 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
4087 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004088 break;
4089}
4090
drh97bae792015-06-05 15:59:57 +00004091#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4092/* Opcode: ColumnsUsed P1 * * P4 *
4093**
4094** This opcode (which only exists if SQLite was compiled with
4095** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4096** table or index for cursor P1 are used. P4 is a 64-bit integer
4097** (P4_INT64) in which the first 63 bits are one for each of the
4098** first 63 columns of the table or index that are actually used
4099** by the cursor. The high-order bit is set if any column after
4100** the 64th is used.
4101*/
4102case OP_ColumnsUsed: {
4103 VdbeCursor *pC;
4104 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004105 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00004106 pC->maskUsed = *(u64*)pOp->p4.pI64;
4107 break;
4108}
4109#endif
4110
drh8af3f772014-07-25 18:01:06 +00004111/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004112** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004113**
danielk1977b790c6c2008-04-18 10:25:24 +00004114** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004115** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004116** to an SQL index, then P3 is the first in an array of P4 registers
4117** that are used as an unpacked index key.
4118**
4119** Reposition cursor P1 so that it points to the smallest entry that
4120** is greater than or equal to the key value. If there are no records
4121** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004122**
drhb1d607d2015-11-05 22:30:54 +00004123** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004124** opcode will either land on a record that exactly matches the key, or
4125** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4126** this opcode must be followed by an IdxLE opcode with the same arguments.
4127** The IdxGT opcode will be skipped if this opcode succeeds, but the
4128** IdxGT opcode will be used on subsequent loop iterations. The
4129** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4130** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004131**
drh8af3f772014-07-25 18:01:06 +00004132** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00004133** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004134** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004135**
drh935850e2014-05-24 17:15:15 +00004136** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004137*/
drh8af3f772014-07-25 18:01:06 +00004138/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004139** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00004140**
danielk1977b790c6c2008-04-18 10:25:24 +00004141** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004142** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004143** to an SQL index, then P3 is the first in an array of P4 registers
4144** that are used as an unpacked index key.
4145**
drh576d0a92020-03-12 17:28:27 +00004146** Reposition cursor P1 so that it points to the smallest entry that
danielk1977b790c6c2008-04-18 10:25:24 +00004147** is greater than the key value. If there are no records greater than
4148** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00004149**
drh8af3f772014-07-25 18:01:06 +00004150** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004151** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004152** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004153**
drh935850e2014-05-24 17:15:15 +00004154** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00004155*/
drh8af3f772014-07-25 18:01:06 +00004156/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004157** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004158**
danielk1977b790c6c2008-04-18 10:25:24 +00004159** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004160** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004161** to an SQL index, then P3 is the first in an array of P4 registers
4162** that are used as an unpacked index key.
4163**
4164** Reposition cursor P1 so that it points to the largest entry that
4165** is less than the key value. If there are no records less than
4166** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00004167**
drh8af3f772014-07-25 18:01:06 +00004168** This opcode leaves the cursor configured to move in reverse order,
4169** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004170** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004171**
drh935850e2014-05-24 17:15:15 +00004172** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004173*/
drh8af3f772014-07-25 18:01:06 +00004174/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004175** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00004176**
danielk1977b790c6c2008-04-18 10:25:24 +00004177** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004178** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004179** to an SQL index, then P3 is the first in an array of P4 registers
4180** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00004181**
danielk1977b790c6c2008-04-18 10:25:24 +00004182** Reposition cursor P1 so that it points to the largest entry that
4183** is less than or equal to the key value. If there are no records
4184** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004185**
drh8af3f772014-07-25 18:01:06 +00004186** This opcode leaves the cursor configured to move in reverse order,
4187** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004188** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004189**
drhb1d607d2015-11-05 22:30:54 +00004190** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004191** opcode will either land on a record that exactly matches the key, or
4192** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4193** this opcode must be followed by an IdxLE opcode with the same arguments.
drhb1d607d2015-11-05 22:30:54 +00004194** The IdxGE opcode will be skipped if this opcode succeeds, but the
drh576d0a92020-03-12 17:28:27 +00004195** IdxGE opcode will be used on subsequent loop iterations. The
4196** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4197** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004198**
drh935850e2014-05-24 17:15:15 +00004199** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00004200*/
mistachkin758784d2018-07-25 15:12:29 +00004201case OP_SeekLT: /* jump, in3, group */
4202case OP_SeekLE: /* jump, in3, group */
4203case OP_SeekGE: /* jump, in3, group */
4204case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00004205 int res; /* Comparison result */
4206 int oc; /* Opcode */
4207 VdbeCursor *pC; /* The cursor to seek */
4208 UnpackedRecord r; /* The key to seek for */
4209 int nField; /* Number of columns or fields in the key */
4210 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00004211 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00004212
drh653b82a2009-06-22 11:10:47 +00004213 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00004214 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00004215 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004216 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004217 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00004218 assert( OP_SeekLE == OP_SeekLT+1 );
4219 assert( OP_SeekGE == OP_SeekLT+2 );
4220 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00004221 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00004222 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004223 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00004224 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00004225 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00004226#ifdef SQLITE_DEBUG
4227 pC->seekOp = pOp->opcode;
4228#endif
drhe0997b32015-03-20 14:57:50 +00004229
dana40cb962019-05-14 20:25:22 +00004230 pC->deferredMoveto = 0;
4231 pC->cacheStatus = CACHE_STALE;
drh3da046d2013-11-11 03:24:11 +00004232 if( pC->isTable ){
drh3e364802019-08-22 00:53:16 +00004233 u16 flags3, newType;
drh576d0a92020-03-12 17:28:27 +00004234 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00004235 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
4236 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00004237
drh3da046d2013-11-11 03:24:11 +00004238 /* The input value in P3 might be of any type: integer, real, string,
4239 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00004240 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00004241 pIn3 = &aMem[pOp->p3];
drh3e364802019-08-22 00:53:16 +00004242 flags3 = pIn3->flags;
4243 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00004244 applyNumericAffinity(pIn3, 0);
4245 }
drh3e364802019-08-22 00:53:16 +00004246 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
4247 newType = pIn3->flags; /* Record the type after applying numeric affinity */
4248 pIn3->flags = flags3; /* But convert the type back to its original */
drh959403f2008-12-12 17:56:16 +00004249
drh3da046d2013-11-11 03:24:11 +00004250 /* If the P3 value could not be converted into an integer without
4251 ** loss of information, then special processing is required... */
drh3e364802019-08-22 00:53:16 +00004252 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
4253 if( (newType & MEM_Real)==0 ){
4254 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
drh8616cff2019-07-13 16:15:23 +00004255 VdbeBranchTaken(1,2);
4256 goto jump_to_p2;
dan9edd8c12019-05-08 11:42:49 +00004257 }else{
dan873b0192019-05-09 11:19:27 +00004258 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4259 if( rc!=SQLITE_OK ) goto abort_due_to_error;
dan9edd8c12019-05-08 11:42:49 +00004260 goto seek_not_found;
4261 }
4262 }else
drh959403f2008-12-12 17:56:16 +00004263
danaa1776f2013-11-26 18:22:59 +00004264 /* If the approximation iKey is larger than the actual real search
4265 ** term, substitute >= for > and < for <=. e.g. if the search term
4266 ** is 4.9 and the integer approximation 5:
4267 **
4268 ** (x > 4.9) -> (x >= 5)
4269 ** (x <= 4.9) -> (x < 5)
4270 */
drh74eaba42014-09-18 17:52:15 +00004271 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004272 assert( OP_SeekGE==(OP_SeekGT-1) );
4273 assert( OP_SeekLT==(OP_SeekLE-1) );
4274 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
4275 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00004276 }
4277
4278 /* If the approximation iKey is smaller than the actual real search
4279 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00004280 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004281 assert( OP_SeekLE==(OP_SeekLT+1) );
4282 assert( OP_SeekGT==(OP_SeekGE+1) );
4283 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4284 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004285 }
dan9edd8c12019-05-08 11:42:49 +00004286 }
drhc960dcb2015-11-20 19:22:01 +00004287 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004288 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004289 if( rc!=SQLITE_OK ){
4290 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004291 }
drhaa736092009-06-22 00:55:30 +00004292 }else{
drh576d0a92020-03-12 17:28:27 +00004293 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4294 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4295 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4296 ** with the same key.
drhd6b79462015-11-07 01:19:00 +00004297 */
drhc960dcb2015-11-20 19:22:01 +00004298 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004299 eqOnly = 1;
4300 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4301 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
drh576d0a92020-03-12 17:28:27 +00004302 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
4303 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
drhd6b79462015-11-07 01:19:00 +00004304 assert( pOp[1].p1==pOp[0].p1 );
4305 assert( pOp[1].p2==pOp[0].p2 );
4306 assert( pOp[1].p3==pOp[0].p3 );
4307 assert( pOp[1].p4.i==pOp[0].p4.i );
4308 }
4309
drh3da046d2013-11-11 03:24:11 +00004310 nField = pOp->p4.i;
4311 assert( pOp->p4type==P4_INT32 );
4312 assert( nField>0 );
4313 r.pKeyInfo = pC->pKeyInfo;
4314 r.nField = (u16)nField;
4315
4316 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004317 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004318 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004319 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004320 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004321 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004322 */
dan1fed5da2014-02-25 21:01:25 +00004323 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4324 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4325 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4326 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4327 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004328
4329 r.aMem = &aMem[pOp->p3];
4330#ifdef SQLITE_DEBUG
4331 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4332#endif
drh70528d72015-11-05 20:25:09 +00004333 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004334 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004335 if( rc!=SQLITE_OK ){
4336 goto abort_due_to_error;
4337 }
drhb1d607d2015-11-05 22:30:54 +00004338 if( eqOnly && r.eqSeen==0 ){
4339 assert( res!=0 );
4340 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004341 }
drh3da046d2013-11-11 03:24:11 +00004342 }
drh3da046d2013-11-11 03:24:11 +00004343#ifdef SQLITE_TEST
4344 sqlite3_search_count++;
4345#endif
drh4a1d3652014-02-14 15:13:36 +00004346 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4347 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004348 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004349 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4350 if( rc!=SQLITE_OK ){
4351 if( rc==SQLITE_DONE ){
4352 rc = SQLITE_OK;
4353 res = 1;
4354 }else{
4355 goto abort_due_to_error;
4356 }
4357 }
drh3da046d2013-11-11 03:24:11 +00004358 }else{
4359 res = 0;
4360 }
4361 }else{
drh4a1d3652014-02-14 15:13:36 +00004362 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4363 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004364 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004365 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4366 if( rc!=SQLITE_OK ){
4367 if( rc==SQLITE_DONE ){
4368 rc = SQLITE_OK;
4369 res = 1;
4370 }else{
4371 goto abort_due_to_error;
4372 }
4373 }
drh3da046d2013-11-11 03:24:11 +00004374 }else{
4375 /* res might be negative because the table is empty. Check to
4376 ** see if this is the case.
4377 */
drhc960dcb2015-11-20 19:22:01 +00004378 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004379 }
4380 }
drhb1d607d2015-11-05 22:30:54 +00004381seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004382 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004383 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004384 if( res ){
drhf56fa462015-04-13 21:39:54 +00004385 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004386 }else if( eqOnly ){
4387 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4388 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004389 }
drh5e00f6c2001-09-13 13:46:56 +00004390 break;
4391}
dan71c57db2016-07-09 20:23:55 +00004392
drh68cf0ac2020-09-28 19:51:54 +00004393
drh04e70ce2020-10-02 11:55:07 +00004394/* Opcode: SeekScan P1 P2 * * *
drh68cf0ac2020-09-28 19:51:54 +00004395** Synopsis: Scan-ahead up to P1 rows
4396**
drhdfbaae72020-09-29 17:29:11 +00004397** This opcode is a prefix opcode to OP_SeekGE. In other words, this
drh04e70ce2020-10-02 11:55:07 +00004398** opcode must be immediately followed by OP_SeekGE. This constraint is
drhdfbaae72020-09-29 17:29:11 +00004399** checked by assert() statements.
4400**
4401** This opcode uses the P1 through P4 operands of the subsequent
4402** OP_SeekGE. In the text that follows, the operands of the subsequent
4403** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
drh04e70ce2020-10-02 11:55:07 +00004404** the P1 and P2 operands of this opcode are also used, and are called
4405** This.P1 and This.P2.
drh68cf0ac2020-09-28 19:51:54 +00004406**
4407** This opcode helps to optimize IN operators on a multi-column index
drhdfbaae72020-09-29 17:29:11 +00004408** where the IN operator is on the later terms of the index by avoiding
4409** unnecessary seeks on the btree, substituting steps to the next row
4410** of the b-tree instead. A correct answer is obtained if this opcode
4411** is omitted or is a no-op.
drh68cf0ac2020-09-28 19:51:54 +00004412**
drhdfbaae72020-09-29 17:29:11 +00004413** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4414** is the desired entry that we want the cursor SeekGE.P1 to be pointing
4415** to. Call this SeekGE.P4/P5 row the "target".
drh68cf0ac2020-09-28 19:51:54 +00004416**
drha54e1b12020-09-29 23:52:25 +00004417** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4418** then this opcode is a no-op and control passes through into the OP_SeekGE.
drh68cf0ac2020-09-28 19:51:54 +00004419**
drhdfbaae72020-09-29 17:29:11 +00004420** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4421** might be the target row, or it might be near and slightly before the
4422** target row. This opcode attempts to position the cursor on the target
drh04e70ce2020-10-02 11:55:07 +00004423** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
drhdfbaae72020-09-29 17:29:11 +00004424** between 0 and This.P1 times.
drh68cf0ac2020-09-28 19:51:54 +00004425**
drhdfbaae72020-09-29 17:29:11 +00004426** There are three possible outcomes from this opcode:<ol>
drh68cf0ac2020-09-28 19:51:54 +00004427**
drhdfbaae72020-09-29 17:29:11 +00004428** <li> If after This.P1 steps, the cursor is still point to a place that
4429** is earlier in the btree than the target row,
4430** then fall through into the subsquence OP_SeekGE opcode.
drh68cf0ac2020-09-28 19:51:54 +00004431**
drhdfbaae72020-09-29 17:29:11 +00004432** <li> If the cursor is successfully moved to the target row by 0 or more
drh04e70ce2020-10-02 11:55:07 +00004433** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
4434** past the OP_IdxGT opcode that follows the OP_SeekGE.
drhdfbaae72020-09-29 17:29:11 +00004435**
4436** <li> If the cursor ends up past the target row (indicating the the target
4437** row does not exist in the btree) then jump to SeekOP.P2.
4438** </ol>
drh68cf0ac2020-09-28 19:51:54 +00004439*/
4440case OP_SeekScan: {
drhf761d932020-09-29 01:48:46 +00004441 VdbeCursor *pC;
4442 int res;
drhdeaa6102020-10-01 15:46:21 +00004443 int nStep;
drhf761d932020-09-29 01:48:46 +00004444 UnpackedRecord r;
4445
drh68cf0ac2020-09-28 19:51:54 +00004446 assert( pOp[1].opcode==OP_SeekGE );
drh04e70ce2020-10-02 11:55:07 +00004447
4448 /* pOp->p2 points to the first instruction past the OP_IdxGT that
4449 ** follows the OP_SeekGE. */
4450 assert( pOp->p2>=(int)(pOp-aOp)+2 );
4451 assert( aOp[pOp->p2-1].opcode==OP_IdxGT );
4452 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
4453 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
4454 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
drh04e70ce2020-10-02 11:55:07 +00004455
drh68cf0ac2020-09-28 19:51:54 +00004456 assert( pOp->p1>0 );
drhf761d932020-09-29 01:48:46 +00004457 pC = p->apCsr[pOp[1].p1];
4458 assert( pC!=0 );
4459 assert( pC->eCurType==CURTYPE_BTREE );
4460 assert( !pC->isTable );
drha54e1b12020-09-29 23:52:25 +00004461 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
drhf761d932020-09-29 01:48:46 +00004462#ifdef SQLITE_DEBUG
4463 if( db->flags&SQLITE_VdbeTrace ){
drha54e1b12020-09-29 23:52:25 +00004464 printf("... cursor not valid - fall through\n");
drhf761d932020-09-29 01:48:46 +00004465 }
4466#endif
4467 break;
4468 }
drhdeaa6102020-10-01 15:46:21 +00004469 nStep = pOp->p1;
4470 assert( nStep>=1 );
drhf761d932020-09-29 01:48:46 +00004471 r.pKeyInfo = pC->pKeyInfo;
4472 r.nField = (u16)pOp[1].p4.i;
4473 r.default_rc = 0;
4474 r.aMem = &aMem[pOp[1].p3];
4475#ifdef SQLITE_DEBUG
4476 {
4477 int i;
4478 for(i=0; i<r.nField; i++){
4479 assert( memIsValid(&r.aMem[i]) );
4480 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
4481 }
4482 }
4483#endif
4484 res = 0; /* Not needed. Only used to silence a warning. */
4485 while(1){
4486 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
4487 if( rc ) goto abort_due_to_error;
4488 if( res>0 ){
drh0b2949c2020-09-29 20:22:19 +00004489 seekscan_search_fail:
drhf761d932020-09-29 01:48:46 +00004490#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004491 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004492 printf("... %d steps and then skip\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004493 }
drhf761d932020-09-29 01:48:46 +00004494#endif
drh0b2949c2020-09-29 20:22:19 +00004495 VdbeBranchTaken(1,3);
drhf287d002020-09-30 00:10:22 +00004496 pOp++;
drhf761d932020-09-29 01:48:46 +00004497 goto jump_to_p2;
4498 }
4499 if( res==0 ){
4500#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004501 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004502 printf("... %d steps and then success\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004503 }
drhf761d932020-09-29 01:48:46 +00004504#endif
drh0b2949c2020-09-29 20:22:19 +00004505 VdbeBranchTaken(2,3);
drh04e70ce2020-10-02 11:55:07 +00004506 goto jump_to_p2;
drhf761d932020-09-29 01:48:46 +00004507 break;
4508 }
drhdeaa6102020-10-01 15:46:21 +00004509 if( nStep<=0 ){
drh0b2949c2020-09-29 20:22:19 +00004510#ifdef SQLITE_DEBUG
4511 if( db->flags&SQLITE_VdbeTrace ){
4512 printf("... fall through after %d steps\n", pOp->p1);
4513 }
4514#endif
4515 VdbeBranchTaken(0,3);
4516 break;
4517 }
drhdeaa6102020-10-01 15:46:21 +00004518 nStep--;
drhf761d932020-09-29 01:48:46 +00004519 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
drh0b2949c2020-09-29 20:22:19 +00004520 if( rc ){
4521 if( rc==SQLITE_DONE ){
4522 rc = SQLITE_OK;
4523 goto seekscan_search_fail;
4524 }else{
4525 goto abort_due_to_error;
4526 }
4527 }
drhf761d932020-09-29 01:48:46 +00004528 }
drh0b2949c2020-09-29 20:22:19 +00004529
drhf761d932020-09-29 01:48:46 +00004530 break;
drh68cf0ac2020-09-28 19:51:54 +00004531}
4532
4533
drhfa17e132020-09-01 01:52:03 +00004534/* Opcode: SeekHit P1 P2 P3 * *
4535** Synopsis: set P2<=seekHit<=P3
drh8c2b6d72018-06-05 20:45:20 +00004536**
drhfa17e132020-09-01 01:52:03 +00004537** Increase or decrease the seekHit value for cursor P1, if necessary,
4538** so that it is no less than P2 and no greater than P3.
drh8c2b6d72018-06-05 20:45:20 +00004539**
drhfa17e132020-09-01 01:52:03 +00004540** The seekHit integer represents the maximum of terms in an index for which
4541** there is known to be at least one match. If the seekHit value is smaller
4542** than the total number of equality terms in an index lookup, then the
4543** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4544** early, thus saving work. This is part of the IN-early-out optimization.
4545**
4546** P1 must be a valid b-tree cursor.
drh8c2b6d72018-06-05 20:45:20 +00004547*/
4548case OP_SeekHit: {
4549 VdbeCursor *pC;
4550 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4551 pC = p->apCsr[pOp->p1];
4552 assert( pC!=0 );
drhfa17e132020-09-01 01:52:03 +00004553 assert( pOp->p3>=pOp->p2 );
4554 if( pC->seekHit<pOp->p2 ){
4555 pC->seekHit = pOp->p2;
4556 }else if( pC->seekHit>pOp->p3 ){
4557 pC->seekHit = pOp->p3;
4558 }
drh8c2b6d72018-06-05 20:45:20 +00004559 break;
4560}
4561
dan74ebaad2020-01-04 16:55:57 +00004562/* Opcode: IfNotOpen P1 P2 * * *
4563** Synopsis: if( !csr[P1] ) goto P2
4564**
4565** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
4566*/
4567case OP_IfNotOpen: { /* jump */
4568 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh56ea69b2020-01-04 18:33:20 +00004569 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
dan74ebaad2020-01-04 16:55:57 +00004570 if( !p->apCsr[pOp->p1] ){
4571 goto jump_to_p2_and_check_for_interrupt;
4572 }
4573 break;
4574}
4575
drh8cff69d2009-11-12 19:59:44 +00004576/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004577** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004578**
drh8cff69d2009-11-12 19:59:44 +00004579** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4580** P4>0 then register P3 is the first of P4 registers that form an unpacked
4581** record.
4582**
4583** Cursor P1 is on an index btree. If the record identified by P3 and P4
4584** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004585** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004586**
drhcefc87f2014-08-01 01:40:33 +00004587** This operation leaves the cursor in a state where it can be
4588** advanced in the forward direction. The Next instruction will work,
4589** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004590**
drh6f225d02013-10-26 13:36:51 +00004591** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004592*/
drh8cff69d2009-11-12 19:59:44 +00004593/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004594** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004595**
drh8cff69d2009-11-12 19:59:44 +00004596** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4597** P4>0 then register P3 is the first of P4 registers that form an unpacked
4598** record.
4599**
4600** Cursor P1 is on an index btree. If the record identified by P3 and P4
4601** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4602** does contain an entry whose prefix matches the P3/P4 record then control
4603** falls through to the next instruction and P1 is left pointing at the
4604** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004605**
drh8af3f772014-07-25 18:01:06 +00004606** This operation leaves the cursor in a state where it cannot be
4607** advanced in either direction. In other words, the Next and Prev
4608** opcodes do not work after this operation.
4609**
drh8c2b6d72018-06-05 20:45:20 +00004610** See also: Found, NotExists, NoConflict, IfNoHope
4611*/
4612/* Opcode: IfNoHope P1 P2 P3 P4 *
4613** Synopsis: key=r[P3@P4]
4614**
4615** Register P3 is the first of P4 registers that form an unpacked
drhfa17e132020-09-01 01:52:03 +00004616** record. Cursor P1 is an index btree. P2 is a jump destination.
4617** In other words, the operands to this opcode are the same as the
4618** operands to OP_NotFound and OP_IdxGT.
drh8c2b6d72018-06-05 20:45:20 +00004619**
drhfa17e132020-09-01 01:52:03 +00004620** This opcode is an optimization attempt only. If this opcode always
4621** falls through, the correct answer is still obtained, but extra works
4622** is performed.
drh8c2b6d72018-06-05 20:45:20 +00004623**
drhfa17e132020-09-01 01:52:03 +00004624** A value of N in the seekHit flag of cursor P1 means that there exists
4625** a key P3:N that will match some record in the index. We want to know
4626** if it is possible for a record P3:P4 to match some record in the
4627** index. If it is not possible, we can skips some work. So if seekHit
4628** is less than P4, attempt to find out if a match is possible by running
4629** OP_NotFound.
drh8c2b6d72018-06-05 20:45:20 +00004630**
4631** This opcode is used in IN clause processing for a multi-column key.
4632** If an IN clause is attached to an element of the key other than the
4633** left-most element, and if there are no matches on the most recent
4634** seek over the whole key, then it might be that one of the key element
4635** to the left is prohibiting a match, and hence there is "no hope" of
4636** any match regardless of how many IN clause elements are checked.
4637** In such a case, we abandon the IN clause search early, using this
4638** opcode. The opcode name comes from the fact that the
4639** jump is taken if there is "no hope" of achieving a match.
4640**
4641** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004642*/
drh6f225d02013-10-26 13:36:51 +00004643/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004644** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004645**
4646** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4647** P4>0 then register P3 is the first of P4 registers that form an unpacked
4648** record.
4649**
4650** Cursor P1 is on an index btree. If the record identified by P3 and P4
4651** contains any NULL value, jump immediately to P2. If all terms of the
4652** record are not-NULL then a check is done to determine if any row in the
4653** P1 index btree has a matching key prefix. If there are no matches, jump
4654** immediately to P2. If there is a match, fall through and leave the P1
4655** cursor pointing to the matching row.
4656**
4657** This opcode is similar to OP_NotFound with the exceptions that the
4658** branch is always taken if any part of the search key input is NULL.
4659**
drh8af3f772014-07-25 18:01:06 +00004660** This operation leaves the cursor in a state where it cannot be
4661** advanced in either direction. In other words, the Next and Prev
4662** opcodes do not work after this operation.
4663**
drh6f225d02013-10-26 13:36:51 +00004664** See also: NotFound, Found, NotExists
4665*/
drh8c2b6d72018-06-05 20:45:20 +00004666case OP_IfNoHope: { /* jump, in3 */
4667 VdbeCursor *pC;
4668 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4669 pC = p->apCsr[pOp->p1];
4670 assert( pC!=0 );
drhfa17e132020-09-01 01:52:03 +00004671 if( pC->seekHit>=pOp->p4.i ) break;
drh8c2b6d72018-06-05 20:45:20 +00004672 /* Fall through into OP_NotFound */
drh08b92082020-08-10 14:18:00 +00004673 /* no break */ deliberate_fall_through
drh8c2b6d72018-06-05 20:45:20 +00004674}
drh6f225d02013-10-26 13:36:51 +00004675case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004676case OP_NotFound: /* jump, in3 */
4677case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004678 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004679 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004680 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004681 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004682 int res;
drha582b012016-12-21 19:45:54 +00004683 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004684 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004685 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004686
dan0ff297e2009-09-25 17:03:14 +00004687#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004688 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004689#endif
4690
drhaa736092009-06-22 00:55:30 +00004691 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004692 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004693 pC = p->apCsr[pOp->p1];
4694 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004695#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004696 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004697#endif
drh3c657212009-11-17 23:59:58 +00004698 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004699 assert( pC->eCurType==CURTYPE_BTREE );
4700 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004701 assert( pC->isTable==0 );
4702 if( pOp->p4.i>0 ){
4703 r.pKeyInfo = pC->pKeyInfo;
4704 r.nField = (u16)pOp->p4.i;
4705 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004706#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004707 for(ii=0; ii<r.nField; ii++){
4708 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004709 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004710 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004711 }
drh8aaf7bc2016-09-20 01:19:18 +00004712#endif
drh3da046d2013-11-11 03:24:11 +00004713 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004714 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004715 }else{
drhe46515b2017-05-19 22:51:00 +00004716 assert( pIn3->flags & MEM_Blob );
4717 rc = ExpandBlob(pIn3);
4718 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4719 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004720 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004721 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004722 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004723 }
dan1fed5da2014-02-25 21:01:25 +00004724 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004725 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004726 if( pOp->opcode==OP_NoConflict ){
4727 /* For the OP_NoConflict opcode, take the jump if any of the
4728 ** input fields are NULL, since any key with a NULL will not
4729 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004730 for(ii=0; ii<pIdxKey->nField; ii++){
4731 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004732 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004733 break;
drh6f225d02013-10-26 13:36:51 +00004734 }
4735 }
drh5e00f6c2001-09-13 13:46:56 +00004736 }
drhc960dcb2015-11-20 19:22:01 +00004737 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004738 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004739 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004740 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004741 }
4742 pC->seekResult = res;
4743 alreadyExists = (res==0);
4744 pC->nullRow = 1-alreadyExists;
4745 pC->deferredMoveto = 0;
4746 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004747 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004748 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004749 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004750 }else{
drhf56fa462015-04-13 21:39:54 +00004751 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4752 if( takeJump || !alreadyExists ) goto jump_to_p2;
drhfa17e132020-09-01 01:52:03 +00004753 if( pOp->opcode==OP_IfNoHope ) pC->seekHit = pOp->p4.i;
drh5e00f6c2001-09-13 13:46:56 +00004754 }
drh5e00f6c2001-09-13 13:46:56 +00004755 break;
4756}
4757
drheeb95652016-05-26 20:56:38 +00004758/* Opcode: SeekRowid P1 P2 P3 * *
4759** Synopsis: intkey=r[P3]
4760**
4761** P1 is the index of a cursor open on an SQL table btree (with integer
4762** keys). If register P3 does not contain an integer or if P1 does not
4763** contain a record with rowid P3 then jump immediately to P2.
4764** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4765** a record with rowid P3 then
4766** leave the cursor pointing at that record and fall through to the next
4767** instruction.
4768**
4769** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4770** the P3 register must be guaranteed to contain an integer value. With this
4771** opcode, register P3 might not contain an integer.
4772**
4773** The OP_NotFound opcode performs the same operation on index btrees
4774** (with arbitrary multi-value keys).
4775**
4776** This opcode leaves the cursor in a state where it cannot be advanced
4777** in either direction. In other words, the Next and Prev opcodes will
4778** not work following this opcode.
4779**
4780** See also: Found, NotFound, NoConflict, SeekRowid
4781*/
drh9cbf3422008-01-17 16:22:13 +00004782/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004783** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004784**
drh261c02d2013-10-25 14:46:15 +00004785** P1 is the index of a cursor open on an SQL table btree (with integer
4786** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004787** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4788** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4789** leave the cursor pointing at that record and fall through to the next
4790** instruction.
drh6b125452002-01-28 15:53:03 +00004791**
drheeb95652016-05-26 20:56:38 +00004792** The OP_SeekRowid opcode performs the same operation but also allows the
4793** P3 register to contain a non-integer value, in which case the jump is
4794** always taken. This opcode requires that P3 always contain an integer.
4795**
drh261c02d2013-10-25 14:46:15 +00004796** The OP_NotFound opcode performs the same operation on index btrees
4797** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004798**
drh8af3f772014-07-25 18:01:06 +00004799** This opcode leaves the cursor in a state where it cannot be advanced
4800** in either direction. In other words, the Next and Prev opcodes will
4801** not work following this opcode.
4802**
drheeb95652016-05-26 20:56:38 +00004803** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004804*/
drheeb95652016-05-26 20:56:38 +00004805case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004806 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004807 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004808 int res;
4809 u64 iKey;
4810
drh3c657212009-11-17 23:59:58 +00004811 pIn3 = &aMem[pOp->p3];
drh3242c692019-05-04 01:29:13 +00004812 testcase( pIn3->flags & MEM_Int );
4813 testcase( pIn3->flags & MEM_IntReal );
drhb29ef5e2019-10-07 01:05:57 +00004814 testcase( pIn3->flags & MEM_Real );
4815 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
drh169f0772019-05-02 21:36:26 +00004816 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
drhb29ef5e2019-10-07 01:05:57 +00004817 /* If pIn3->u.i does not contain an integer, compute iKey as the
4818 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
4819 ** into an integer without loss of information. Take care to avoid
4820 ** changing the datatype of pIn3, however, as it is used by other
4821 ** parts of the prepared statement. */
4822 Mem x = pIn3[0];
4823 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
4824 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
4825 iKey = x.u.i;
4826 goto notExistsWithKey;
drheeb95652016-05-26 20:56:38 +00004827 }
4828 /* Fall through into OP_NotExists */
drh08b92082020-08-10 14:18:00 +00004829 /* no break */ deliberate_fall_through
drheeb95652016-05-26 20:56:38 +00004830case OP_NotExists: /* jump, in3 */
4831 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004832 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004833 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhb29ef5e2019-10-07 01:05:57 +00004834 iKey = pIn3->u.i;
4835notExistsWithKey:
drhaa736092009-06-22 00:55:30 +00004836 pC = p->apCsr[pOp->p1];
4837 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004838#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00004839 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004840#endif
drhaa736092009-06-22 00:55:30 +00004841 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004842 assert( pC->eCurType==CURTYPE_BTREE );
4843 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004844 assert( pCrsr!=0 );
4845 res = 0;
drh3da046d2013-11-11 03:24:11 +00004846 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004847 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004848 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004849 pC->nullRow = 0;
4850 pC->cacheStatus = CACHE_STALE;
4851 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004852 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004853 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004854 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004855 assert( rc==SQLITE_OK );
4856 if( pOp->p2==0 ){
4857 rc = SQLITE_CORRUPT_BKPT;
4858 }else{
4859 goto jump_to_p2;
4860 }
danc6157e12015-09-14 09:23:47 +00004861 }
drh9467abf2016-02-17 18:44:11 +00004862 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004863 break;
4864}
4865
drh4c583122008-01-04 22:01:03 +00004866/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004867** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004868**
drh4c583122008-01-04 22:01:03 +00004869** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004870** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004871** The sequence number on the cursor is incremented after this
4872** instruction.
drh4db38a72005-09-01 12:16:28 +00004873*/
drh27a348c2015-04-13 19:14:06 +00004874case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004875 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4876 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004877 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004878 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004879 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004880 break;
4881}
4882
4883
drh98757152008-01-09 23:04:12 +00004884/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004885** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004886**
drhf0863fe2005-06-12 21:35:51 +00004887** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004888** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004889** table that cursor P1 points to. The new record number is written
4890** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004891**
dan76d462e2009-08-30 11:42:51 +00004892** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4893** the largest previously generated record number. No new record numbers are
4894** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004895** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004896** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004897** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004898*/
drh27a348c2015-04-13 19:14:06 +00004899case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004900 i64 v; /* The new rowid */
4901 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4902 int res; /* Result of an sqlite3BtreeLast() */
4903 int cnt; /* Counter to limit the number of searches */
mistachkind6665c52021-01-18 19:28:56 +00004904#ifndef SQLITE_OMIT_AUTOINCREMENT
drhaa736092009-06-22 00:55:30 +00004905 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004906 VdbeFrame *pFrame; /* Root frame of VDBE */
mistachkind6665c52021-01-18 19:28:56 +00004907#endif
drh856c1032009-06-02 15:21:42 +00004908
drh856c1032009-06-02 15:21:42 +00004909 v = 0;
4910 res = 0;
drh27a348c2015-04-13 19:14:06 +00004911 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004912 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4913 pC = p->apCsr[pOp->p1];
4914 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004915 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004916 assert( pC->eCurType==CURTYPE_BTREE );
4917 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004918 {
drh5cf8e8c2002-02-19 22:42:05 +00004919 /* The next rowid or record number (different terms for the same
4920 ** thing) is obtained in a two-step algorithm.
4921 **
4922 ** First we attempt to find the largest existing rowid and add one
4923 ** to that. But if the largest existing rowid is already the maximum
4924 ** positive integer, we have to fall through to the second
4925 ** probabilistic algorithm
4926 **
4927 ** The second algorithm is to select a rowid at random and see if
4928 ** it already exists in the table. If it does not exist, we have
4929 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004930 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004931 */
drhaa736092009-06-22 00:55:30 +00004932 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004933
drh75f86a42005-02-17 00:03:06 +00004934#ifdef SQLITE_32BIT_ROWID
4935# define MAX_ROWID 0x7fffffff
4936#else
drhfe2093d2005-01-20 22:48:47 +00004937 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4938 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4939 ** to provide the constant while making all compilers happy.
4940 */
danielk197764202cf2008-11-17 15:31:47 +00004941# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004942#endif
drhfe2093d2005-01-20 22:48:47 +00004943
drh5cf8e8c2002-02-19 22:42:05 +00004944 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004945 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004946 if( rc!=SQLITE_OK ){
4947 goto abort_due_to_error;
4948 }
4949 if( res ){
4950 v = 1; /* IMP: R-61914-48074 */
4951 }else{
drhc960dcb2015-11-20 19:22:01 +00004952 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004953 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004954 if( v>=MAX_ROWID ){
4955 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004956 }else{
drhe0670b62014-02-12 21:31:12 +00004957 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004958 }
drh3fc190c2001-09-14 03:24:23 +00004959 }
drhe0670b62014-02-12 21:31:12 +00004960 }
drh205f48e2004-11-05 00:43:11 +00004961
4962#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004963 if( pOp->p3 ){
4964 /* Assert that P3 is a valid memory cell. */
4965 assert( pOp->p3>0 );
4966 if( p->pFrame ){
4967 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004968 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004969 assert( pOp->p3<=pFrame->nMem );
4970 pMem = &pFrame->aMem[pOp->p3];
4971 }else{
4972 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004973 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004974 pMem = &aMem[pOp->p3];
4975 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004976 }
drhe0670b62014-02-12 21:31:12 +00004977 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004978
drhe0670b62014-02-12 21:31:12 +00004979 REGISTER_TRACE(pOp->p3, pMem);
4980 sqlite3VdbeMemIntegerify(pMem);
4981 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4982 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004983 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004984 goto abort_due_to_error;
4985 }
4986 if( v<pMem->u.i+1 ){
4987 v = pMem->u.i + 1;
4988 }
4989 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004990 }
drhe0670b62014-02-12 21:31:12 +00004991#endif
drh5cf8e8c2002-02-19 22:42:05 +00004992 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004993 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004994 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004995 ** engine starts picking positive candidate ROWIDs at random until
4996 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004997 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4998 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004999 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00005000 do{
5001 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00005002 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00005003 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00005004 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00005005 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00005006 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00005007 if( rc ) goto abort_due_to_error;
5008 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00005009 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00005010 goto abort_due_to_error;
5011 }
drh748a52c2010-09-01 11:50:08 +00005012 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00005013 }
drha11846b2004-01-07 18:52:56 +00005014 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005015 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005016 }
drh4c583122008-01-04 22:01:03 +00005017 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005018 break;
5019}
5020
danielk19771f4aa332008-01-03 09:51:55 +00005021/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005022** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005023**
jplyon5a564222003-06-02 06:15:58 +00005024** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00005025** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00005026** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00005027** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00005028** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00005029**
danielk19771f4aa332008-01-03 09:51:55 +00005030** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5031** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00005032** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00005033** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00005034**
drheaf6ae22016-11-09 20:14:34 +00005035** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5036** run faster by avoiding an unnecessary seek on cursor P1. However,
5037** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5038** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00005039**
5040** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5041** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5042** is part of an INSERT operation. The difference is only important to
5043** the update hook.
5044**
dan319eeb72011-03-19 08:38:50 +00005045** Parameter P4 may point to a Table structure, or may be NULL. If it is
5046** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5047** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00005048**
drh93aed5a2008-01-16 17:46:38 +00005049** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5050** allocated, then ownership of P2 is transferred to the pseudo-cursor
5051** and register P2 becomes ephemeral. If the cursor is changed, the
5052** value of register P2 will then change. Make sure this does not
5053** cause any problems.)
5054**
drhf0863fe2005-06-12 21:35:51 +00005055** This instruction only works on tables. The equivalent instruction
5056** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00005057*/
drh50ef6712019-02-22 23:29:56 +00005058case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00005059 Mem *pData; /* MEM cell holding data for the record to be inserted */
5060 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00005061 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00005062 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5063 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00005064 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00005065 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00005066
drha6c2ed92009-11-14 23:22:23 +00005067 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00005068 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00005069 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00005070 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00005071 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005072 assert( pC->eCurType==CURTYPE_BTREE );
drhbe3da242019-12-29 00:52:41 +00005073 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005074 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00005075 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00005076 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00005077 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00005078 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00005079
drh50ef6712019-02-22 23:29:56 +00005080 pKey = &aMem[pOp->p3];
5081 assert( pKey->flags & MEM_Int );
5082 assert( memIsValid(pKey) );
5083 REGISTER_TRACE(pOp->p3, pKey);
5084 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00005085
drh9b1c62d2011-03-30 21:04:43 +00005086 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005087 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00005088 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005089 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00005090 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00005091 }else{
drh4ec6f3a2018-01-12 19:33:18 +00005092 pTab = 0;
drh74c33022016-03-30 12:56:55 +00005093 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00005094 }
5095
drh9b1c62d2011-03-30 21:04:43 +00005096#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005097 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00005098 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00005099 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
5100 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
5101 }
drh4ec6f3a2018-01-12 19:33:18 +00005102 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
5103 /* Prevent post-update hook from running in cases when it should not */
5104 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00005105 }
dan46c47d42011-03-01 18:42:07 +00005106 }
dancb9a3642017-01-30 19:44:53 +00005107 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00005108#endif
dan46c47d42011-03-01 18:42:07 +00005109
drha05a7222008-01-19 03:35:58 +00005110 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00005111 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
drh32881be2020-11-17 21:26:13 +00005112 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
dan21cd29a2017-10-23 16:03:54 +00005113 x.pData = pData->z;
5114 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00005115 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
5116 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00005117 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00005118 }else{
drh8eeb4462016-05-21 20:03:42 +00005119 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00005120 }
drh8eeb4462016-05-21 20:03:42 +00005121 x.pKey = 0;
5122 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00005123 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
5124 seekResult
drh3e9ca092009-09-08 01:14:48 +00005125 );
drha05a7222008-01-19 03:35:58 +00005126 pC->deferredMoveto = 0;
5127 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00005128
drha05a7222008-01-19 03:35:58 +00005129 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00005130 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00005131 if( pTab ){
5132 assert( db->xUpdateCallback!=0 );
5133 assert( pTab->aCol!=0 );
5134 db->xUpdateCallback(db->pUpdateArg,
5135 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
5136 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00005137 }
drh5e00f6c2001-09-13 13:46:56 +00005138 break;
5139}
5140
dan7aae7352020-12-10 18:06:24 +00005141/* Opcode: RowCell P1 P2 P3 * *
dand2ffc972020-12-10 19:20:15 +00005142**
5143** P1 and P2 are both open cursors. Both must be opened on the same type
5144** of table - intkey or index. This opcode is used as part of copying
5145** the current row from P2 into P1. If the cursors are opened on intkey
5146** tables, register P3 contains the rowid to use with the new record in
5147** P1. If they are opened on index tables, P3 is not used.
5148**
5149** This opcode must be followed by either an Insert or InsertIdx opcode
5150** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
dan036e0672020-12-08 20:19:07 +00005151*/
dan7aae7352020-12-10 18:06:24 +00005152case OP_RowCell: {
dan036e0672020-12-08 20:19:07 +00005153 VdbeCursor *pDest; /* Cursor to write to */
5154 VdbeCursor *pSrc; /* Cursor to read from */
5155 i64 iKey; /* Rowid value to insert with */
dan7aae7352020-12-10 18:06:24 +00005156 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
drha06eafc2020-12-29 15:06:26 +00005157 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
5158 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
dand2ffc972020-12-10 19:20:15 +00005159 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
dan036e0672020-12-08 20:19:07 +00005160 pDest = p->apCsr[pOp->p1];
5161 pSrc = p->apCsr[pOp->p2];
dancd1b2d02020-12-09 20:33:51 +00005162 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
dan7aae7352020-12-10 18:06:24 +00005163 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
dan036e0672020-12-08 20:19:07 +00005164 if( rc!=SQLITE_OK ) goto abort_due_to_error;
5165 break;
dan7aae7352020-12-10 18:06:24 +00005166};
dan036e0672020-12-08 20:19:07 +00005167
dan438b8812015-09-15 15:55:15 +00005168/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005169**
drh5edc3122001-09-13 21:53:09 +00005170** Delete the record at which the P1 cursor is currently pointing.
5171**
drhe807bdb2016-01-21 17:06:33 +00005172** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5173** the cursor will be left pointing at either the next or the previous
5174** record in the table. If it is left pointing at the next record, then
5175** the next Next instruction will be a no-op. As a result, in this case
5176** it is ok to delete a record from within a Next loop. If
5177** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5178** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00005179**
drhdef19e32016-01-27 16:26:25 +00005180** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5181** delete one of several associated with deleting a table row and all its
5182** associated index entries. Exactly one of those deletes is the "primary"
5183** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5184** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00005185**
5186** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5187** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00005188**
drh91fd4d42008-01-19 20:11:25 +00005189** P1 must not be pseudo-table. It has to be a real table with
5190** multiple rows.
5191**
drh5e769a52016-09-28 16:05:53 +00005192** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00005193** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5194** have been positioned using OP_NotFound prior to invoking this opcode in
5195** this case. Specifically, if one is configured, the pre-update hook is
5196** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5197** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00005198**
5199** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5200** of the memory cell that contains the value that the rowid of the row will
5201** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00005202*/
drh9cbf3422008-01-17 16:22:13 +00005203case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00005204 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00005205 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00005206 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00005207 int opflags;
drh91fd4d42008-01-19 20:11:25 +00005208
dan46c47d42011-03-01 18:42:07 +00005209 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00005210 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5211 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005212 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005213 assert( pC->eCurType==CURTYPE_BTREE );
5214 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00005215 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00005216 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00005217
drhb53a5a92014-10-12 22:37:22 +00005218#ifdef SQLITE_DEBUG
drh6b559f32020-01-02 19:50:50 +00005219 if( pOp->p4type==P4_TABLE
5220 && HasRowid(pOp->p4.pTab)
5221 && pOp->p5==0
5222 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
5223 ){
dan438b8812015-09-15 15:55:15 +00005224 /* If p5 is zero, the seek operation that positioned the cursor prior to
5225 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5226 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00005227 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan0971ef42019-05-16 20:13:32 +00005228 assert( CORRUPT_DB || pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00005229 }
5230#endif
drh91fd4d42008-01-19 20:11:25 +00005231
dan438b8812015-09-15 15:55:15 +00005232 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5233 ** the name of the db to pass as to it. Also set local pTab to a copy
5234 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5235 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5236 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00005237 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005238 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00005239 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00005240 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005241 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00005242 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00005243 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00005244 }
drh74c33022016-03-30 12:56:55 +00005245 }else{
5246 zDb = 0; /* Not needed. Silence a compiler warning. */
5247 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00005248 }
dan46c47d42011-03-01 18:42:07 +00005249
drh9b1c62d2011-03-30 21:04:43 +00005250#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005251 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00005252 if( db->xPreUpdateCallback && pOp->p4.pTab ){
5253 assert( !(opflags & OPFLAG_ISUPDATE)
5254 || HasRowid(pTab)==0
5255 || (aMem[pOp->p3].flags & MEM_Int)
5256 );
dan46c47d42011-03-01 18:42:07 +00005257 sqlite3VdbePreUpdateHook(p, pC,
5258 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00005259 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00005260 pOp->p3
dan46c47d42011-03-01 18:42:07 +00005261 );
5262 }
dan46c47d42011-03-01 18:42:07 +00005263 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00005264#endif
drhb53a5a92014-10-12 22:37:22 +00005265
drhdef19e32016-01-27 16:26:25 +00005266 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5267 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00005268 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00005269 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00005270
5271#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00005272 if( p->pFrame==0 ){
5273 if( pC->isEphemeral==0
5274 && (pOp->p5 & OPFLAG_AUXDELETE)==0
5275 && (pC->wrFlag & OPFLAG_FORDELETE)==0
5276 ){
5277 nExtraDelete++;
5278 }
5279 if( pOp->p2 & OPFLAG_NCHANGE ){
5280 nExtraDelete--;
5281 }
drhb89aeb62016-01-27 15:49:32 +00005282 }
5283#endif
5284
drhc960dcb2015-11-20 19:22:01 +00005285 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00005286 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005287 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00005288 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00005289
drh91fd4d42008-01-19 20:11:25 +00005290 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00005291 if( opflags & OPFLAG_NCHANGE ){
5292 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00005293 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00005294 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00005295 pC->movetoTarget);
5296 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00005297 }
drh5e00f6c2001-09-13 13:46:56 +00005298 }
dan438b8812015-09-15 15:55:15 +00005299
rdcb0c374f2004-02-20 22:53:38 +00005300 break;
5301}
drhb7f1d9a2009-09-08 02:27:58 +00005302/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00005303**
drhb7f1d9a2009-09-08 02:27:58 +00005304** The value of the change counter is copied to the database handle
5305** change counter (returned by subsequent calls to sqlite3_changes()).
5306** Then the VMs internal change counter resets to 0.
5307** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00005308*/
drh9cbf3422008-01-17 16:22:13 +00005309case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00005310 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00005311 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00005312 break;
5313}
5314
drh1153c7b2013-11-01 22:02:56 +00005315/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00005316** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00005317**
drh1153c7b2013-11-01 22:02:56 +00005318** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00005319** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00005320** the sorter cursor currently points to. Only the first P4 fields
5321** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00005322**
5323** If either P3 or the sorter contains a NULL in one of their significant
5324** fields (not counting the P4 fields at the end which are ignored) then
5325** the comparison is assumed to be equal.
5326**
5327** Fall through to next instruction if the two records compare equal to
5328** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00005329*/
5330case OP_SorterCompare: {
5331 VdbeCursor *pC;
5332 int res;
drhac502322014-07-30 13:56:48 +00005333 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00005334
5335 pC = p->apCsr[pOp->p1];
5336 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00005337 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00005338 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00005339 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00005340 res = 0;
drhac502322014-07-30 13:56:48 +00005341 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00005342 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00005343 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005344 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00005345 break;
5346};
5347
drh6cf4a7d2014-10-13 13:00:58 +00005348/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005349** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00005350**
5351** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00005352** Then clear the column header cache on cursor P3.
5353**
5354** This opcode is normally use to move a record out of the sorter and into
5355** a register that is the source for a pseudo-table cursor created using
5356** OpenPseudo. That pseudo-table cursor is the one that is identified by
5357** parameter P3. Clearing the P3 column cache as part of this opcode saves
5358** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00005359*/
5360case OP_SorterData: {
5361 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00005362
dan5134d132011-09-02 10:31:11 +00005363 pOut = &aMem[pOp->p2];
5364 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00005365 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00005366 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00005367 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00005368 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00005369 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00005370 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00005371 break;
5372}
5373
drhe7b554d2017-01-09 15:44:25 +00005374/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005375** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00005376**
drh9057fc72016-11-25 19:32:32 +00005377** Write into register P2 the complete row content for the row at
5378** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00005379** There is no interpretation of the data.
5380** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00005381** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00005382**
drh9057fc72016-11-25 19:32:32 +00005383** If cursor P1 is an index, then the content is the key of the row.
5384** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00005385**
drhde4fcfd2008-01-19 23:50:26 +00005386** If the P1 cursor must be pointing to a valid row (not a NULL row)
5387** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00005388**
drh8cdafc32018-05-31 19:00:20 +00005389** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00005390** into the database page. That means that the content of the output
5391** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00005392** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00005393** position in order that they can write to the same table. If P3==0
5394** then a copy of the data is made into memory. P3!=0 is faster, but
5395** P3==0 is safer.
5396**
5397** If P3!=0 then the content of the P2 register is unsuitable for use
5398** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00005399** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00005400** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00005401*/
danielk1977a7a8e142008-02-13 18:25:27 +00005402case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00005403 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00005404 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00005405 u32 n;
drh70ce3f02003-04-15 19:22:22 +00005406
drhe7b554d2017-01-09 15:44:25 +00005407 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00005408
drh653b82a2009-06-22 11:10:47 +00005409 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5410 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00005411 assert( pC!=0 );
5412 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00005413 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00005414 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00005415 assert( pC->uc.pCursor!=0 );
5416 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00005417
drh9057fc72016-11-25 19:32:32 +00005418 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00005419 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5420 ** that might invalidate the cursor.
5421 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00005422 ** would fail. Should this ever change (because of changes in the code
5423 ** generator) then the fix would be to insert a call to
5424 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00005425 */
5426 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00005427 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00005428
drha7c90c42016-06-04 20:37:10 +00005429 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00005430 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00005431 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00005432 }
drh722246e2014-10-07 23:02:24 +00005433 testcase( n==0 );
drh2a740062020-02-05 18:28:17 +00005434 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
drh9467abf2016-02-17 18:44:11 +00005435 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00005436 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00005437 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00005438 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00005439 break;
5440}
5441
drh2133d822008-01-03 18:44:59 +00005442/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005443** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005444**
drh2133d822008-01-03 18:44:59 +00005445** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00005446** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00005447**
5448** P1 can be either an ordinary table or a virtual table. There used to
5449** be a separate OP_VRowid opcode for use with virtual tables, but this
5450** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00005451*/
drh27a348c2015-04-13 19:14:06 +00005452case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00005453 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00005454 i64 v;
drh856c1032009-06-02 15:21:42 +00005455 sqlite3_vtab *pVtab;
5456 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00005457
drh27a348c2015-04-13 19:14:06 +00005458 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005459 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5460 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005461 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005462 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00005463 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00005464 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00005465 break;
5466 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00005467 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00005468#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00005469 }else if( pC->eCurType==CURTYPE_VTAB ){
5470 assert( pC->uc.pVCur!=0 );
5471 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00005472 pModule = pVtab->pModule;
5473 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00005474 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00005475 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00005476 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00005477#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00005478 }else{
drhc960dcb2015-11-20 19:22:01 +00005479 assert( pC->eCurType==CURTYPE_BTREE );
5480 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00005481 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00005482 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00005483 if( pC->nullRow ){
5484 pOut->flags = MEM_Null;
5485 break;
5486 }
drha7c90c42016-06-04 20:37:10 +00005487 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00005488 }
drh4c583122008-01-04 22:01:03 +00005489 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005490 break;
5491}
5492
drh9cbf3422008-01-17 16:22:13 +00005493/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005494**
5495** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005496** that occur while the cursor is on the null row will always
5497** write a NULL.
drh17f71932002-02-21 12:01:27 +00005498*/
drh9cbf3422008-01-17 16:22:13 +00005499case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005500 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005501
drh653b82a2009-06-22 11:10:47 +00005502 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5503 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005504 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00005505 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005506 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005507 if( pC->eCurType==CURTYPE_BTREE ){
5508 assert( pC->uc.pCursor!=0 );
5509 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005510 }
drhcf025a82018-06-07 18:01:21 +00005511#ifdef SQLITE_DEBUG
5512 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5513#endif
drh17f71932002-02-21 12:01:27 +00005514 break;
5515}
5516
drh86b40df2017-08-01 19:53:43 +00005517/* Opcode: SeekEnd P1 * * * *
5518**
5519** Position cursor P1 at the end of the btree for the purpose of
5520** appending a new entry onto the btree.
5521**
5522** It is assumed that the cursor is used only for appending and so
5523** if the cursor is valid, then the cursor must already be pointing
5524** at the end of the btree and so no changes are made to
5525** the cursor.
5526*/
5527/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005528**
drh8af3f772014-07-25 18:01:06 +00005529** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005530** will refer to the last entry in the database table or index.
5531** If the table or index is empty and P2>0, then jump immediately to P2.
5532** If P2 is 0 or if the table or index is not empty, fall through
5533** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005534**
5535** This opcode leaves the cursor configured to move in reverse order,
5536** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005537** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005538*/
drh86b40df2017-08-01 19:53:43 +00005539case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005540case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005541 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005542 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005543 int res;
drh9562b552002-02-19 15:00:07 +00005544
drh653b82a2009-06-22 11:10:47 +00005545 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5546 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005547 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005548 assert( pC->eCurType==CURTYPE_BTREE );
5549 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005550 res = 0;
drh3da046d2013-11-11 03:24:11 +00005551 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005552#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005553 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005554#endif
drh86b40df2017-08-01 19:53:43 +00005555 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005556 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005557 pC->seekResult = -1;
5558 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5559 break;
5560 }
5561 }
5562 rc = sqlite3BtreeLast(pCrsr, &res);
5563 pC->nullRow = (u8)res;
5564 pC->deferredMoveto = 0;
5565 pC->cacheStatus = CACHE_STALE;
5566 if( rc ) goto abort_due_to_error;
5567 if( pOp->p2>0 ){
5568 VdbeBranchTaken(res!=0,2);
5569 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005570 }
5571 break;
5572}
5573
drh5e98e832017-02-17 19:24:06 +00005574/* Opcode: IfSmaller P1 P2 P3 * *
5575**
5576** Estimate the number of rows in the table P1. Jump to P2 if that
5577** estimate is less than approximately 2**(0.1*P3).
5578*/
5579case OP_IfSmaller: { /* jump */
5580 VdbeCursor *pC;
5581 BtCursor *pCrsr;
5582 int res;
5583 i64 sz;
5584
5585 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5586 pC = p->apCsr[pOp->p1];
5587 assert( pC!=0 );
5588 pCrsr = pC->uc.pCursor;
5589 assert( pCrsr );
5590 rc = sqlite3BtreeFirst(pCrsr, &res);
5591 if( rc ) goto abort_due_to_error;
5592 if( res==0 ){
5593 sz = sqlite3BtreeRowCountEst(pCrsr);
5594 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5595 }
5596 VdbeBranchTaken(res!=0,2);
5597 if( res ) goto jump_to_p2;
5598 break;
5599}
5600
drh0342b1f2005-09-01 03:07:44 +00005601
drh6bd4dc62016-12-23 16:05:22 +00005602/* Opcode: SorterSort P1 P2 * * *
5603**
5604** After all records have been inserted into the Sorter object
5605** identified by P1, invoke this opcode to actually do the sorting.
5606** Jump to P2 if there are no records to be sorted.
5607**
5608** This opcode is an alias for OP_Sort and OP_Rewind that is used
5609** for Sorter objects.
5610*/
drh9cbf3422008-01-17 16:22:13 +00005611/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005612**
5613** This opcode does exactly the same thing as OP_Rewind except that
5614** it increments an undocumented global variable used for testing.
5615**
5616** Sorting is accomplished by writing records into a sorting index,
5617** then rewinding that index and playing it back from beginning to
5618** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5619** rewinding so that the global variable will be incremented and
5620** regression tests can determine whether or not the optimizer is
5621** correctly optimizing out sorts.
5622*/
drhc6aff302011-09-01 15:32:47 +00005623case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005624case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005625#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005626 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005627 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005628#endif
drh9b47ee32013-08-20 03:13:51 +00005629 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005630 /* Fall through into OP_Rewind */
drh08b92082020-08-10 14:18:00 +00005631 /* no break */ deliberate_fall_through
drh0342b1f2005-09-01 03:07:44 +00005632}
drh038ebf62019-03-29 15:21:22 +00005633/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005634**
drhf0863fe2005-06-12 21:35:51 +00005635** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005636** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005637** If the table or index is empty, jump immediately to P2.
5638** If the table or index is not empty, fall through to the following
5639** instruction.
drh8af3f772014-07-25 18:01:06 +00005640**
5641** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005642** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005643** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005644*/
drh9cbf3422008-01-17 16:22:13 +00005645case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005646 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005647 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005648 int res;
drh5e00f6c2001-09-13 13:46:56 +00005649
drh653b82a2009-06-22 11:10:47 +00005650 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00005651 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00005652 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005653 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005654 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005655 res = 1;
drh8af3f772014-07-25 18:01:06 +00005656#ifdef SQLITE_DEBUG
5657 pC->seekOp = OP_Rewind;
5658#endif
dan689ab892011-08-12 15:02:00 +00005659 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005660 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005661 }else{
drhc960dcb2015-11-20 19:22:01 +00005662 assert( pC->eCurType==CURTYPE_BTREE );
5663 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005664 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005665 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00005666 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005667 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005668 }
drh9467abf2016-02-17 18:44:11 +00005669 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005670 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005671 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005672 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005673 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005674 break;
5675}
5676
drh0fd61352014-02-07 02:29:45 +00005677/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005678**
5679** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005680** table or index. If there are no more key/value pairs then fall through
5681** to the following instruction. But if the cursor advance was successful,
5682** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005683**
drh5dad9a32014-07-25 18:37:42 +00005684** The Next opcode is only valid following an SeekGT, SeekGE, or
5685** OP_Rewind opcode used to position the cursor. Next is not allowed
5686** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005687**
drhf93cd942013-11-21 03:12:25 +00005688** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5689** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005690**
drhe39a7322014-02-03 14:04:11 +00005691** The P3 value is a hint to the btree implementation. If P3==1, that
5692** means P1 is an SQL index and that this instruction could have been
5693** omitted if that index had been unique. P3 is usually 0. P3 is
5694** always either 0 or 1.
5695**
dana205a482011-08-27 18:48:57 +00005696** P4 is always of type P4_ADVANCE. The function pointer points to
5697** sqlite3BtreeNext().
5698**
drhafc266a2010-03-31 17:47:44 +00005699** If P5 is positive and the jump is taken, then event counter
5700** number P5-1 in the prepared statement is incremented.
5701**
drhf1949b62018-06-07 17:32:59 +00005702** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005703*/
drh0fd61352014-02-07 02:29:45 +00005704/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005705**
5706** Back up cursor P1 so that it points to the previous key/data pair in its
5707** table or index. If there is no previous key/value pairs then fall through
5708** to the following instruction. But if the cursor backup was successful,
5709** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005710**
drh8af3f772014-07-25 18:01:06 +00005711**
drh5dad9a32014-07-25 18:37:42 +00005712** The Prev opcode is only valid following an SeekLT, SeekLE, or
5713** OP_Last opcode used to position the cursor. Prev is not allowed
5714** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005715**
drhf93cd942013-11-21 03:12:25 +00005716** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5717** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005718**
drhe39a7322014-02-03 14:04:11 +00005719** The P3 value is a hint to the btree implementation. If P3==1, that
5720** means P1 is an SQL index and that this instruction could have been
5721** omitted if that index had been unique. P3 is usually 0. P3 is
5722** always either 0 or 1.
5723**
dana205a482011-08-27 18:48:57 +00005724** P4 is always of type P4_ADVANCE. The function pointer points to
5725** sqlite3BtreePrevious().
5726**
drhafc266a2010-03-31 17:47:44 +00005727** If P5 is positive and the jump is taken, then event counter
5728** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005729*/
drh6bd4dc62016-12-23 16:05:22 +00005730/* Opcode: SorterNext P1 P2 * * P5
5731**
5732** This opcode works just like OP_Next except that P1 must be a
5733** sorter object for which the OP_SorterSort opcode has been
5734** invoked. This opcode advances the cursor to the next sorted
5735** record, or jumps to P2 if there are no more sorted records.
5736*/
drhf93cd942013-11-21 03:12:25 +00005737case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005738 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005739
drhf93cd942013-11-21 03:12:25 +00005740 pC = p->apCsr[pOp->p1];
5741 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005742 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005743 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005744case OP_Prev: /* jump */
5745case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005746 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005747 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005748 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005749 assert( pC!=0 );
5750 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005751 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005752 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5753 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005754
drhcf025a82018-06-07 18:01:21 +00005755 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005756 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005757 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005758 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drh790b37a2019-08-27 17:01:07 +00005759 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
5760 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
5761 || pC->seekOp==OP_IfNoHope);
drhf1949b62018-06-07 17:32:59 +00005762 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005763 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drh790b37a2019-08-27 17:01:07 +00005764 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
drhcf025a82018-06-07 18:01:21 +00005765 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005766
drh2ab792e2017-05-30 18:34:07 +00005767 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005768next_tail:
drha3460582008-07-11 21:02:53 +00005769 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005770 VdbeBranchTaken(rc==SQLITE_OK,2);
5771 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005772 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005773 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005774#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005775 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005776#endif
drhf56fa462015-04-13 21:39:54 +00005777 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005778 }
drh2ab792e2017-05-30 18:34:07 +00005779 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5780 rc = SQLITE_OK;
5781 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005782 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005783}
5784
drh9b4eaeb2016-11-09 00:10:33 +00005785/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005786** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005787**
drhef8662b2011-06-20 21:47:58 +00005788** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005789** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005790** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005791**
drhfb8c56f2016-11-09 01:19:25 +00005792** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005793** key of reg(P2). In that case, P3 is the index of the first register
5794** for the unpacked key. The availability of the unpacked key can sometimes
5795** be an optimization.
5796**
5797** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5798** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005799**
mistachkin21a919f2014-02-07 03:28:02 +00005800** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5801** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5802** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005803**
drheaf6ae22016-11-09 20:14:34 +00005804** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5805** run faster by avoiding an unnecessary seek on cursor P1. However,
5806** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5807** seeks on the cursor or if the most recent seek used a key equivalent
5808** to P2.
drh0fd61352014-02-07 02:29:45 +00005809**
drhf0863fe2005-06-12 21:35:51 +00005810** This instruction only works for indices. The equivalent instruction
5811** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005812*/
drh9cbf3422008-01-17 16:22:13 +00005813case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005814 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005815 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005816
drh653b82a2009-06-22 11:10:47 +00005817 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5818 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005819 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005820 assert( pC!=0 );
drhc879c4e2020-02-06 13:57:08 +00005821 assert( !isSorter(pC) );
drh3c657212009-11-17 23:59:58 +00005822 pIn2 = &aMem[pOp->p2];
dan7aae7352020-12-10 18:06:24 +00005823 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
drh6546af12013-11-04 15:23:25 +00005824 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc879c4e2020-02-06 13:57:08 +00005825 assert( pC->eCurType==CURTYPE_BTREE );
drh3da046d2013-11-11 03:24:11 +00005826 assert( pC->isTable==0 );
5827 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005828 if( rc ) goto abort_due_to_error;
drhc879c4e2020-02-06 13:57:08 +00005829 x.nKey = pIn2->n;
5830 x.pKey = pIn2->z;
5831 x.aMem = aMem + pOp->p3;
5832 x.nMem = (u16)pOp->p4.i;
5833 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00005834 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
drhc879c4e2020-02-06 13:57:08 +00005835 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5836 );
5837 assert( pC->deferredMoveto==0 );
5838 pC->cacheStatus = CACHE_STALE;
5839 if( rc) goto abort_due_to_error;
5840 break;
5841}
5842
5843/* Opcode: SorterInsert P1 P2 * * *
5844** Synopsis: key=r[P2]
5845**
5846** Register P2 holds an SQL index key made using the
5847** MakeRecord instructions. This opcode writes that key
5848** into the sorter P1. Data for the entry is nil.
5849*/
5850case OP_SorterInsert: { /* in2 */
5851 VdbeCursor *pC;
5852
5853 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5854 pC = p->apCsr[pOp->p1];
5855 sqlite3VdbeIncrWriteCounter(p, pC);
5856 assert( pC!=0 );
5857 assert( isSorter(pC) );
5858 pIn2 = &aMem[pOp->p2];
5859 assert( pIn2->flags & MEM_Blob );
5860 assert( pC->isTable==0 );
5861 rc = ExpandBlob(pIn2);
5862 if( rc ) goto abort_due_to_error;
5863 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh9467abf2016-02-17 18:44:11 +00005864 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005865 break;
5866}
5867
drh85bd3532020-05-05 18:42:49 +00005868/* Opcode: IdxDelete P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00005869** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005870**
drhe14006d2008-03-25 17:23:32 +00005871** The content of P3 registers starting at register P2 form
5872** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005873** index opened by cursor P1.
drh85bd3532020-05-05 18:42:49 +00005874**
5875** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
5876** if no matching index entry is found. This happens when running
5877** an UPDATE or DELETE statement and the index entry to be updated
5878** or deleted is not found. For some uses of IdxDelete
5879** (example: the EXCEPT operator) it does not matter that no matching
5880** entry is found. For those cases, P5 is zero.
drh5e00f6c2001-09-13 13:46:56 +00005881*/
drhe14006d2008-03-25 17:23:32 +00005882case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005883 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005884 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005885 int res;
5886 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005887
drhe14006d2008-03-25 17:23:32 +00005888 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005889 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005890 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5891 pC = p->apCsr[pOp->p1];
5892 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005893 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005894 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005895 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005896 assert( pCrsr!=0 );
drh3da046d2013-11-11 03:24:11 +00005897 r.pKeyInfo = pC->pKeyInfo;
5898 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005899 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005900 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005901 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005902 if( rc ) goto abort_due_to_error;
5903 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005904 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005905 if( rc ) goto abort_due_to_error;
drh85bd3532020-05-05 18:42:49 +00005906 }else if( pOp->p5 ){
drhe5ceaac2021-01-25 21:24:14 +00005907 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
drh85bd3532020-05-05 18:42:49 +00005908 goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005909 }
drh3da046d2013-11-11 03:24:11 +00005910 assert( pC->deferredMoveto==0 );
5911 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005912 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005913 break;
5914}
5915
drh170ad682017-06-02 15:44:22 +00005916/* Opcode: DeferredSeek P1 * P3 P4 *
5917** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005918**
5919** P1 is an open index cursor and P3 is a cursor on the corresponding
5920** table. This opcode does a deferred seek of the P3 table cursor
5921** to the row that corresponds to the current row of P1.
5922**
5923** This is a deferred seek. Nothing actually happens until
5924** the cursor is used to read a record. That way, if no reads
5925** occur, no unnecessary I/O happens.
5926**
5927** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005928** one entry for each column in the P3 table. If array entry a(i)
5929** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005930** equivalent to performing the deferred seek and then reading column i
5931** from P1. This information is stored in P3 and used to redirect
5932** reads against P3 over to P1, thus possibly avoiding the need to
5933** seek and read cursor P3.
5934*/
drh2133d822008-01-03 18:44:59 +00005935/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005936** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005937**
drh2133d822008-01-03 18:44:59 +00005938** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005939** the end of the index key pointed to by cursor P1. This integer should be
5940** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005941**
drh9437bd22009-02-01 00:29:56 +00005942** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005943*/
drh170ad682017-06-02 15:44:22 +00005944case OP_DeferredSeek:
5945case OP_IdxRowid: { /* out2 */
5946 VdbeCursor *pC; /* The P1 index cursor */
5947 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5948 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005949
drh653b82a2009-06-22 11:10:47 +00005950 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5951 pC = p->apCsr[pOp->p1];
5952 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005953 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005954 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005955 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005956 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005957 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5958
5959 /* The IdxRowid and Seek opcodes are combined because of the commonality
5960 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5961 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005962
5963 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005964 ** out from under the cursor. That will never happens for an IdxRowid
5965 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005966 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5967
drh3da046d2013-11-11 03:24:11 +00005968 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005969 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005970 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005971 if( rc!=SQLITE_OK ){
5972 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005973 }
drh170ad682017-06-02 15:44:22 +00005974 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005975 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5976 pTabCur = p->apCsr[pOp->p3];
5977 assert( pTabCur!=0 );
5978 assert( pTabCur->eCurType==CURTYPE_BTREE );
5979 assert( pTabCur->uc.pCursor!=0 );
5980 assert( pTabCur->isTable );
5981 pTabCur->nullRow = 0;
5982 pTabCur->movetoTarget = rowid;
5983 pTabCur->deferredMoveto = 1;
5984 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5985 pTabCur->aAltMap = pOp->p4.ai;
5986 pTabCur->pAltCursor = pC;
5987 }else{
5988 pOut = out2Prerelease(p, pOp);
5989 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005990 }
5991 }else{
5992 assert( pOp->opcode==OP_IdxRowid );
5993 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005994 }
5995 break;
5996}
5997
drhbe3da242019-12-29 00:52:41 +00005998/* Opcode: FinishSeek P1 * * * *
5999**
6000** If cursor P1 was previously moved via OP_DeferredSeek, complete that
6001** seek operation now, without further delay. If the cursor seek has
6002** already occurred, this instruction is a no-op.
6003*/
6004case OP_FinishSeek: {
6005 VdbeCursor *pC; /* The P1 index cursor */
6006
6007 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6008 pC = p->apCsr[pOp->p1];
6009 if( pC->deferredMoveto ){
6010 rc = sqlite3VdbeFinishMoveto(pC);
6011 if( rc ) goto abort_due_to_error;
6012 }
6013 break;
6014}
6015
drhc51ceeb2020-08-31 12:29:03 +00006016/* Opcode: IdxGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006017** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00006018**
danielk197761dd5832008-04-18 11:31:12 +00006019** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006020** key that omits the PRIMARY KEY. Compare this key value against the index
6021** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6022** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00006023**
danielk197761dd5832008-04-18 11:31:12 +00006024** If the P1 index entry is greater than or equal to the key value
6025** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00006026*/
drhc51ceeb2020-08-31 12:29:03 +00006027/* Opcode: IdxGT P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006028** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00006029**
drh4a1d3652014-02-14 15:13:36 +00006030** The P4 register values beginning with P3 form an unpacked index
6031** key that omits the PRIMARY KEY. Compare this key value against the index
6032** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6033** fields at the end.
6034**
6035** If the P1 index entry is greater than the key value
6036** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00006037*/
drhc51ceeb2020-08-31 12:29:03 +00006038/* Opcode: IdxLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006039** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00006040**
danielk197761dd5832008-04-18 11:31:12 +00006041** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006042** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6043** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6044** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00006045**
danielk197761dd5832008-04-18 11:31:12 +00006046** If the P1 index entry is less than the key value then jump to P2.
6047** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00006048*/
drhc51ceeb2020-08-31 12:29:03 +00006049/* Opcode: IdxLE P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006050** Synopsis: key=r[P3@P4]
6051**
6052** The P4 register values beginning with P3 form an unpacked index
6053** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6054** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6055** ROWID on the P1 index.
6056**
6057** If the P1 index entry is less than or equal to the key value then jump
6058** to P2. Otherwise fall through to the next instruction.
6059*/
6060case OP_IdxLE: /* jump */
6061case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00006062case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00006063case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00006064 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00006065 int res;
6066 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00006067
drh653b82a2009-06-22 11:10:47 +00006068 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6069 pC = p->apCsr[pOp->p1];
6070 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00006071 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00006072 assert( pC->eCurType==CURTYPE_BTREE );
6073 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00006074 assert( pC->deferredMoveto==0 );
drh3da046d2013-11-11 03:24:11 +00006075 assert( pOp->p4type==P4_INT32 );
6076 r.pKeyInfo = pC->pKeyInfo;
6077 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00006078 if( pOp->opcode<OP_IdxLT ){
6079 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00006080 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00006081 }else{
drh4a1d3652014-02-14 15:13:36 +00006082 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00006083 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00006084 }
6085 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006086#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00006087 {
6088 int i;
6089 for(i=0; i<r.nField; i++){
6090 assert( memIsValid(&r.aMem[i]) );
6091 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
6092 }
6093 }
drh2b4ded92010-09-27 21:09:31 +00006094#endif
drhc40076a2020-09-29 16:05:09 +00006095
6096 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6097 {
6098 i64 nCellKey = 0;
6099 BtCursor *pCur;
6100 Mem m;
6101
6102 assert( pC->eCurType==CURTYPE_BTREE );
6103 pCur = pC->uc.pCursor;
6104 assert( sqlite3BtreeCursorIsValid(pCur) );
6105 nCellKey = sqlite3BtreePayloadSize(pCur);
6106 /* nCellKey will always be between 0 and 0xffffffff because of the way
6107 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6108 if( nCellKey<=0 || nCellKey>0x7fffffff ){
6109 rc = SQLITE_CORRUPT_BKPT;
6110 goto abort_due_to_error;
6111 }
6112 sqlite3VdbeMemInit(&m, db, 0);
6113 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
6114 if( rc ) goto abort_due_to_error;
6115 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
6116 sqlite3VdbeMemRelease(&m);
6117 }
6118 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6119
drh4a1d3652014-02-14 15:13:36 +00006120 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
6121 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
6122 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00006123 res = -res;
6124 }else{
drh4a1d3652014-02-14 15:13:36 +00006125 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00006126 res++;
6127 }
drh688852a2014-02-17 22:40:43 +00006128 VdbeBranchTaken(res>0,2);
drhc40076a2020-09-29 16:05:09 +00006129 assert( rc==SQLITE_OK );
drhf56fa462015-04-13 21:39:54 +00006130 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00006131 break;
6132}
6133
drh98757152008-01-09 23:04:12 +00006134/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00006135**
6136** Delete an entire database table or index whose root page in the database
6137** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00006138**
drh98757152008-01-09 23:04:12 +00006139** The table being destroyed is in the main database file if P3==0. If
6140** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00006141** that is used to store tables create using CREATE TEMPORARY TABLE.
6142**
drh205f48e2004-11-05 00:43:11 +00006143** If AUTOVACUUM is enabled then it is possible that another root page
6144** might be moved into the newly deleted root page in order to keep all
6145** root pages contiguous at the beginning of the database. The former
6146** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00006147** is stored in register P2. If no page movement was required (because the
6148** table being dropped was already the last one in the database) then a
6149** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6150** is stored in register P2.
6151**
6152** This opcode throws an error if there are any active reader VMs when
6153** it is invoked. This is done to avoid the difficulty associated with
6154** updating existing cursors when a root page is moved in an AUTOVACUUM
6155** database. This error is thrown even if the database is not an AUTOVACUUM
6156** db in order to avoid introducing an incompatibility between autovacuum
6157** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00006158**
drhb19a2bc2001-09-16 00:13:26 +00006159** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00006160*/
drh27a348c2015-04-13 19:14:06 +00006161case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00006162 int iMoved;
drh856c1032009-06-02 15:21:42 +00006163 int iDb;
drh3a949872012-09-18 13:20:13 +00006164
drh4031baf2018-05-28 17:31:20 +00006165 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00006166 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00006167 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00006168 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00006169 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00006170 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00006171 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00006172 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00006173 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00006174 }else{
drh856c1032009-06-02 15:21:42 +00006175 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00006176 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00006177 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00006178 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00006179 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00006180 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00006181 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00006182#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00006183 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00006184 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
6185 /* All OP_Destroy operations occur on the same btree */
6186 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
6187 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00006188 }
drh3765df42006-06-28 18:18:09 +00006189#endif
danielk1977a0bf2652004-11-04 14:30:04 +00006190 }
drh5e00f6c2001-09-13 13:46:56 +00006191 break;
6192}
6193
danielk1977c7af4842008-10-27 13:59:33 +00006194/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00006195**
6196** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00006197** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00006198** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00006199**
drhf57b3392001-10-08 13:22:32 +00006200** The table being clear is in the main database file if P2==0. If
6201** P2==1 then the table to be clear is in the auxiliary database file
6202** that is used to store tables create using CREATE TEMPORARY TABLE.
6203**
shanebe217792009-03-05 04:20:31 +00006204** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00006205** intkey table (an SQL table, not an index). In this case the row change
6206** count is incremented by the number of rows in the table being cleared.
6207** If P3 is greater than zero, then the value stored in register P3 is
6208** also incremented by the number of rows in the table being cleared.
6209**
drhb19a2bc2001-09-16 00:13:26 +00006210** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00006211*/
drh9cbf3422008-01-17 16:22:13 +00006212case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00006213 int nChange;
6214
drh4031baf2018-05-28 17:31:20 +00006215 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00006216 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00006217 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00006218 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00006219 rc = sqlite3BtreeClearTable(
drhabc38152020-07-22 13:38:04 +00006220 db->aDb[pOp->p2].pBt, (u32)pOp->p1, (pOp->p3 ? &nChange : 0)
danielk1977c7af4842008-10-27 13:59:33 +00006221 );
6222 if( pOp->p3 ){
6223 p->nChange += nChange;
6224 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00006225 assert( memIsValid(&aMem[pOp->p3]) );
6226 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00006227 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00006228 }
6229 }
drh9467abf2016-02-17 18:44:11 +00006230 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00006231 break;
6232}
6233
drh65ea12c2014-03-19 17:41:36 +00006234/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00006235**
drh65ea12c2014-03-19 17:41:36 +00006236** Delete all contents from the ephemeral table or sorter
6237** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00006238**
drh65ea12c2014-03-19 17:41:36 +00006239** This opcode only works for cursors used for sorting and
6240** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00006241*/
drh65ea12c2014-03-19 17:41:36 +00006242case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00006243 VdbeCursor *pC;
6244
6245 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6246 pC = p->apCsr[pOp->p1];
6247 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006248 if( isSorter(pC) ){
6249 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00006250 }else{
drhc960dcb2015-11-20 19:22:01 +00006251 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00006252 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00006253 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00006254 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00006255 }
drh079a3072014-03-19 14:10:55 +00006256 break;
6257}
6258
drh0f3f7662017-08-18 14:34:28 +00006259/* Opcode: CreateBtree P1 P2 P3 * *
6260** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00006261**
drh0f3f7662017-08-18 14:34:28 +00006262** Allocate a new b-tree in the main database file if P1==0 or in the
6263** TEMP database file if P1==1 or in an attached database if
6264** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00006265** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00006266** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00006267*/
drh0f3f7662017-08-18 14:34:28 +00006268case OP_CreateBtree: { /* out2 */
drhabc38152020-07-22 13:38:04 +00006269 Pgno pgno;
drh234c39d2004-07-24 03:30:47 +00006270 Db *pDb;
drh856c1032009-06-02 15:21:42 +00006271
drh4031baf2018-05-28 17:31:20 +00006272 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00006273 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00006274 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00006275 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00006276 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006277 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006278 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00006279 pDb = &db->aDb[pOp->p1];
6280 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00006281 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00006282 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00006283 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00006284 break;
6285}
6286
drh4a54bb52017-02-18 15:58:52 +00006287/* Opcode: SqlExec * * * P4 *
6288**
6289** Run the SQL statement or statements specified in the P4 string.
6290*/
6291case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00006292 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00006293 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00006294 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00006295 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00006296 if( rc ) goto abort_due_to_error;
6297 break;
6298}
6299
drh22645842011-03-24 01:34:03 +00006300/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00006301**
drh346a70c2020-06-15 20:27:35 +00006302** Read and parse all entries from the schema table of database P1
drh1595abc2018-08-14 19:27:51 +00006303** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6304** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00006305**
6306** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00006307** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00006308*/
drh9cbf3422008-01-17 16:22:13 +00006309case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00006310 int iDb;
drh067b92b2020-06-19 15:24:12 +00006311 const char *zSchema;
drh856c1032009-06-02 15:21:42 +00006312 char *zSql;
6313 InitData initData;
6314
drhbdaec522011-04-04 00:14:43 +00006315 /* Any prepared statement that invokes this opcode will hold mutexes
6316 ** on every btree. This is a prerequisite for invoking
6317 ** sqlite3InitCallback().
6318 */
6319#ifdef SQLITE_DEBUG
6320 for(iDb=0; iDb<db->nDb; iDb++){
6321 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
6322 }
6323#endif
drhbdaec522011-04-04 00:14:43 +00006324
drh856c1032009-06-02 15:21:42 +00006325 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00006326 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00006327 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
dane325ffe2018-08-11 13:40:20 +00006328
6329#ifndef SQLITE_OMIT_ALTERTABLE
6330 if( pOp->p4.z==0 ){
6331 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00006332 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
dan6a5a13d2021-02-17 20:08:22 +00006333 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
dane325ffe2018-08-11 13:40:20 +00006334 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00006335 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00006336 }else
6337#endif
drh1595abc2018-08-14 19:27:51 +00006338 {
drh067b92b2020-06-19 15:24:12 +00006339 zSchema = DFLT_SCHEMA_TABLE;
danielk1977a8bbef82009-03-23 17:11:26 +00006340 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00006341 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00006342 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00006343 initData.mInitFlags = 0;
drh3b3ddba2020-07-22 18:03:56 +00006344 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
danielk1977a8bbef82009-03-23 17:11:26 +00006345 zSql = sqlite3MPrintf(db,
drhc5a93d42019-08-12 00:08:07 +00006346 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
drh067b92b2020-06-19 15:24:12 +00006347 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00006348 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00006349 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00006350 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00006351 assert( db->init.busy==0 );
6352 db->init.busy = 1;
6353 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00006354 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006355 assert( !db->mallocFailed );
6356 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
6357 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00006358 if( rc==SQLITE_OK && initData.nInitRow==0 ){
6359 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6360 ** at least one SQL statement. Any less than that indicates that
drh1e32bed2020-06-19 13:33:53 +00006361 ** the sqlite_schema table is corrupt. */
drh6b86e512019-01-05 21:09:37 +00006362 rc = SQLITE_CORRUPT_BKPT;
6363 }
drhdbd6a7d2017-04-05 12:39:49 +00006364 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00006365 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006366 }
drh3c23a882007-01-09 14:01:13 +00006367 }
drh9467abf2016-02-17 18:44:11 +00006368 if( rc ){
6369 sqlite3ResetAllSchemasOfConnection(db);
6370 if( rc==SQLITE_NOMEM ){
6371 goto no_mem;
6372 }
6373 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00006374 }
drh234c39d2004-07-24 03:30:47 +00006375 break;
6376}
6377
drh8bfdf722009-06-19 14:06:03 +00006378#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00006379/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00006380**
6381** Read the sqlite_stat1 table for database P1 and load the content
6382** of that table into the internal index hash table. This will cause
6383** the analysis to be used when preparing all subsequent queries.
6384*/
drh9cbf3422008-01-17 16:22:13 +00006385case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00006386 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6387 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006388 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00006389 break;
6390}
drh8bfdf722009-06-19 14:06:03 +00006391#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00006392
drh98757152008-01-09 23:04:12 +00006393/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006394**
6395** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006396** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00006397** is dropped from disk (using the Destroy opcode) in order to keep
6398** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006399** schema consistent with what is on disk.
6400*/
drh9cbf3422008-01-17 16:22:13 +00006401case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00006402 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006403 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006404 break;
6405}
6406
drh98757152008-01-09 23:04:12 +00006407/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006408**
6409** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006410** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00006411** is dropped from disk (using the Destroy opcode)
6412** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00006413** schema consistent with what is on disk.
6414*/
drh9cbf3422008-01-17 16:22:13 +00006415case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00006416 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006417 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006418 break;
6419}
6420
drh98757152008-01-09 23:04:12 +00006421/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006422**
6423** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006424** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00006425** is dropped from disk (using the Destroy opcode) in order to keep
6426** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006427** schema consistent with what is on disk.
6428*/
drh9cbf3422008-01-17 16:22:13 +00006429case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00006430 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006431 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006432 break;
6433}
6434
drh234c39d2004-07-24 03:30:47 +00006435
drhb7f91642004-10-31 02:22:47 +00006436#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00006437/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00006438**
drh98757152008-01-09 23:04:12 +00006439** Do an analysis of the currently open database. Store in
6440** register P1 the text of an error message describing any problems.
6441** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00006442**
drh66accfc2017-02-22 18:04:42 +00006443** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00006444** At most reg(P3) errors will be reported.
6445** In other words, the analysis stops as soon as reg(P1) errors are
6446** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00006447**
drh98968b22016-03-15 22:00:39 +00006448** The root page numbers of all tables in the database are integers
6449** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00006450**
drh98757152008-01-09 23:04:12 +00006451** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00006452** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00006453**
drh1dcdbc02007-01-27 02:24:54 +00006454** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00006455*/
drhaaab5722002-02-19 13:39:21 +00006456case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00006457 int nRoot; /* Number of tables to check. (Number of root pages.) */
drhabc38152020-07-22 13:38:04 +00006458 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00006459 int nErr; /* Number of errors reported */
6460 char *z; /* Text of the error report */
6461 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00006462
drh1713afb2013-06-28 01:24:57 +00006463 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00006464 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00006465 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00006466 assert( nRoot>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00006467 assert( aRoot[0]==(Pgno)nRoot );
drh9f6168b2016-03-19 23:32:58 +00006468 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006469 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00006470 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00006471 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00006472 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00006473 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006474 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh21f6daa2019-10-11 14:21:48 +00006475 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00006476 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00006477 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00006478 if( nErr==0 ){
6479 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00006480 }else if( z==0 ){
6481 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00006482 }else{
drh66accfc2017-02-22 18:04:42 +00006483 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00006484 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00006485 }
drhb7654112008-01-12 12:48:07 +00006486 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00006487 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh21f6daa2019-10-11 14:21:48 +00006488 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006489}
drhb7f91642004-10-31 02:22:47 +00006490#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00006491
drh3d4501e2008-12-04 20:40:10 +00006492/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00006493** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006494**
drhbb6783b2017-04-29 18:02:49 +00006495** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00006496** held in register P1.
6497**
6498** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00006499*/
drh93952eb2009-11-13 19:43:43 +00006500case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00006501 pIn1 = &aMem[pOp->p1];
6502 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00006503 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00006504 if( (pIn1->flags & MEM_Blob)==0 ){
6505 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00006506 }
drh9d67afc2018-08-29 20:24:03 +00006507 assert( sqlite3VdbeMemIsRowSet(pIn1) );
6508 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00006509 break;
6510}
6511
6512/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00006513** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00006514**
drhbb6783b2017-04-29 18:02:49 +00006515** Extract the smallest value from the RowSet object in P1
6516** and put that value into register P3.
6517** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00006518** unchanged and jump to instruction P2.
6519*/
drh93952eb2009-11-13 19:43:43 +00006520case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00006521 i64 val;
drh49afe3a2013-07-10 03:05:14 +00006522
drh3c657212009-11-17 23:59:58 +00006523 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00006524 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
6525 if( (pIn1->flags & MEM_Blob)==0
6526 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00006527 ){
6528 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00006529 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00006530 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00006531 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00006532 }else{
6533 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00006534 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00006535 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00006536 }
drh49afe3a2013-07-10 03:05:14 +00006537 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006538}
6539
drh1b26c7c2009-04-22 02:15:47 +00006540/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00006541** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00006542**
drhade97602009-04-21 15:05:18 +00006543** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00006544** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00006545** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00006546** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00006547** next opcode.
danielk19771d461462009-04-21 09:02:45 +00006548**
drhbb6783b2017-04-29 18:02:49 +00006549** The RowSet object is optimized for the case where sets of integers
6550** are inserted in distinct phases, which each set contains no duplicates.
6551** Each set is identified by a unique P4 value. The first set
6552** must have P4==0, the final set must have P4==-1, and for all other sets
6553** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00006554**
6555** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006556** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006557** (b) when P4==-1 there is no need to insert the value, as it will
6558** never be tested for, and (c) when a value that is part of set X is
6559** inserted, there is no need to search to see if the same value was
6560** previously inserted as part of set X (only if it was previously
6561** inserted as part of some other set).
6562*/
drh1b26c7c2009-04-22 02:15:47 +00006563case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006564 int iSet;
6565 int exists;
6566
drh3c657212009-11-17 23:59:58 +00006567 pIn1 = &aMem[pOp->p1];
6568 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006569 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006570 assert( pIn3->flags&MEM_Int );
6571
drh1b26c7c2009-04-22 02:15:47 +00006572 /* If there is anything other than a rowset object in memory cell P1,
6573 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006574 */
drh9d67afc2018-08-29 20:24:03 +00006575 if( (pIn1->flags & MEM_Blob)==0 ){
6576 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006577 }
drh9d67afc2018-08-29 20:24:03 +00006578 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006579 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006580 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006581 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006582 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006583 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006584 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006585 }
6586 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006587 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006588 }
6589 break;
6590}
6591
drh5e00f6c2001-09-13 13:46:56 +00006592
danielk197793758c82005-01-21 08:13:14 +00006593#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006594
drh0fd61352014-02-07 02:29:45 +00006595/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006596**
dan76d462e2009-08-30 11:42:51 +00006597** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006598**
dan76d462e2009-08-30 11:42:51 +00006599** P1 contains the address of the memory cell that contains the first memory
6600** cell in an array of values used as arguments to the sub-program. P2
6601** contains the address to jump to if the sub-program throws an IGNORE
6602** exception using the RAISE() function. Register P3 contains the address
6603** of a memory cell in this (the parent) VM that is used to allocate the
6604** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006605**
6606** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006607**
6608** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006609*/
dan76d462e2009-08-30 11:42:51 +00006610case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006611 int nMem; /* Number of memory registers for sub-program */
6612 int nByte; /* Bytes of runtime space required for sub-program */
6613 Mem *pRt; /* Register to allocate runtime space */
6614 Mem *pMem; /* Used to iterate through memory cells */
6615 Mem *pEnd; /* Last memory cell in new array */
6616 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6617 SubProgram *pProgram; /* Sub-program to execute */
6618 void *t; /* Token identifying trigger */
6619
6620 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006621 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006622 assert( pProgram->nOp>0 );
6623
dan1da40a32009-09-19 17:00:31 +00006624 /* If the p5 flag is clear, then recursive invocation of triggers is
6625 ** disabled for backwards compatibility (p5 is set if this sub-program
6626 ** is really a trigger, not a foreign key action, and the flag set
6627 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006628 **
6629 ** It is recursive invocation of triggers, at the SQL level, that is
6630 ** disabled. In some cases a single trigger may generate more than one
6631 ** SubProgram (if the trigger may be executed with more than one different
6632 ** ON CONFLICT algorithm). SubProgram structures associated with a
6633 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006634 ** variable. */
6635 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006636 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006637 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6638 if( pFrame ) break;
6639 }
6640
danf5894502009-10-07 18:41:19 +00006641 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006642 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006643 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006644 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006645 }
6646
6647 /* Register pRt is used to store the memory required to save the state
6648 ** of the current program, and the memory required at runtime to execute
6649 ** the trigger program. If this trigger has been fired before, then pRt
6650 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006651 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006652 /* SubProgram.nMem is set to the number of memory cells used by the
6653 ** program stored in SubProgram.aOp. As well as these, one memory
6654 ** cell is required for each cursor used by the program. Set local
6655 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6656 */
dan65a7cd12009-09-01 12:16:01 +00006657 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006658 assert( nMem>0 );
6659 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006660 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006661 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006662 + pProgram->nCsr * sizeof(VdbeCursor*)
6663 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006664 pFrame = sqlite3DbMallocZero(db, nByte);
6665 if( !pFrame ){
6666 goto no_mem;
6667 }
6668 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006669 pRt->flags = MEM_Blob|MEM_Dyn;
6670 pRt->z = (char*)pFrame;
6671 pRt->n = nByte;
6672 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006673
6674 pFrame->v = p;
6675 pFrame->nChildMem = nMem;
6676 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006677 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006678 pFrame->aMem = p->aMem;
6679 pFrame->nMem = p->nMem;
6680 pFrame->apCsr = p->apCsr;
6681 pFrame->nCursor = p->nCursor;
6682 pFrame->aOp = p->aOp;
6683 pFrame->nOp = p->nOp;
6684 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006685#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006686 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006687#endif
drh72f56ef2018-08-29 18:47:22 +00006688#ifdef SQLITE_DEBUG
6689 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
6690#endif
dan165921a2009-08-28 18:53:45 +00006691
6692 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6693 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006694 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006695 pMem->db = db;
6696 }
6697 }else{
drh72f56ef2018-08-29 18:47:22 +00006698 pFrame = (VdbeFrame*)pRt->z;
6699 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00006700 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6701 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006702 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006703 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006704 }
6705
6706 p->nFrame++;
6707 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006708 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006709 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006710 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006711 assert( pFrame->pAuxData==0 );
6712 pFrame->pAuxData = p->pAuxData;
6713 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006714 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006715 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006716 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006717 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006718 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006719 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006720 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006721 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006722 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006723 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006724#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006725 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006726#endif
drhb2e61bc2019-01-25 19:29:01 +00006727#ifdef SQLITE_DEBUG
6728 /* Verify that second and subsequent executions of the same trigger do not
6729 ** try to reuse register values from the first use. */
6730 {
6731 int i;
6732 for(i=0; i<p->nMem; i++){
6733 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
drhf5cfe6f2020-03-03 20:48:12 +00006734 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
drhb2e61bc2019-01-25 19:29:01 +00006735 }
6736 }
6737#endif
drhf56fa462015-04-13 21:39:54 +00006738 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00006739 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00006740}
6741
dan76d462e2009-08-30 11:42:51 +00006742/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006743**
dan76d462e2009-08-30 11:42:51 +00006744** This opcode is only ever present in sub-programs called via the
6745** OP_Program instruction. Copy a value currently stored in a memory
6746** cell of the calling (parent) frame to cell P2 in the current frames
6747** address space. This is used by trigger programs to access the new.*
6748** and old.* values.
dan165921a2009-08-28 18:53:45 +00006749**
dan76d462e2009-08-30 11:42:51 +00006750** The address of the cell in the parent frame is determined by adding
6751** the value of the P1 argument to the value of the P1 argument to the
6752** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006753*/
drh27a348c2015-04-13 19:14:06 +00006754case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006755 VdbeFrame *pFrame;
6756 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006757 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006758 pFrame = p->pFrame;
6759 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006760 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6761 break;
6762}
6763
danielk197793758c82005-01-21 08:13:14 +00006764#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006765
dan1da40a32009-09-19 17:00:31 +00006766#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006767/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006768** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006769**
dan0ff297e2009-09-25 17:03:14 +00006770** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6771** If P1 is non-zero, the database constraint counter is incremented
6772** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006773** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006774*/
dan32b09f22009-09-23 17:29:59 +00006775case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006776 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006777 db->nDeferredImmCons += pOp->p2;
6778 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006779 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006780 }else{
dan0ff297e2009-09-25 17:03:14 +00006781 p->nFkConstraint += pOp->p2;
6782 }
6783 break;
6784}
6785
6786/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006787** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006788**
6789** This opcode tests if a foreign key constraint-counter is currently zero.
6790** If so, jump to instruction P2. Otherwise, fall through to the next
6791** instruction.
6792**
6793** If P1 is non-zero, then the jump is taken if the database constraint-counter
6794** is zero (the one that counts deferred constraint violations). If P1 is
6795** zero, the jump is taken if the statement constraint-counter is zero
6796** (immediate foreign key constraint violations).
6797*/
6798case OP_FkIfZero: { /* jump */
6799 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006800 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006801 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006802 }else{
drh688852a2014-02-17 22:40:43 +00006803 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006804 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006805 }
dan1da40a32009-09-19 17:00:31 +00006806 break;
6807}
6808#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6809
drh205f48e2004-11-05 00:43:11 +00006810#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006811/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006812** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006813**
dan76d462e2009-08-30 11:42:51 +00006814** P1 is a register in the root frame of this VM (the root frame is
6815** different from the current frame if this instruction is being executed
6816** within a sub-program). Set the value of register P1 to the maximum of
6817** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006818**
6819** This instruction throws an error if the memory cell is not initially
6820** an integer.
6821*/
dan76d462e2009-08-30 11:42:51 +00006822case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006823 VdbeFrame *pFrame;
6824 if( p->pFrame ){
6825 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6826 pIn1 = &pFrame->aMem[pOp->p1];
6827 }else{
drha6c2ed92009-11-14 23:22:23 +00006828 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006829 }
drh2b4ded92010-09-27 21:09:31 +00006830 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006831 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006832 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006833 sqlite3VdbeMemIntegerify(pIn2);
6834 if( pIn1->u.i<pIn2->u.i){
6835 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006836 }
6837 break;
6838}
6839#endif /* SQLITE_OMIT_AUTOINCREMENT */
6840
drh8b0cf382015-10-06 21:07:06 +00006841/* Opcode: IfPos P1 P2 P3 * *
6842** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006843**
drh16897072015-03-07 00:57:37 +00006844** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006845** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006846** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006847**
drh16897072015-03-07 00:57:37 +00006848** If the initial value of register P1 is less than 1, then the
6849** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006850*/
drh9cbf3422008-01-17 16:22:13 +00006851case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006852 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006853 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006854 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006855 if( pIn1->u.i>0 ){
6856 pIn1->u.i -= pOp->p3;
6857 goto jump_to_p2;
6858 }
drhec7429a2005-10-06 16:53:14 +00006859 break;
6860}
6861
drhcc2fa4c2016-01-25 15:57:29 +00006862/* Opcode: OffsetLimit P1 P2 P3 * *
6863** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006864**
drhcc2fa4c2016-01-25 15:57:29 +00006865** This opcode performs a commonly used computation associated with
6866** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6867** holds the offset counter. The opcode computes the combined value
6868** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6869** value computed is the total number of rows that will need to be
6870** visited in order to complete the query.
6871**
6872** If r[P3] is zero or negative, that means there is no OFFSET
6873** and r[P2] is set to be the value of the LIMIT, r[P1].
6874**
6875** if r[P1] is zero or negative, that means there is no LIMIT
6876** and r[P2] is set to -1.
6877**
6878** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006879*/
drhcc2fa4c2016-01-25 15:57:29 +00006880case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006881 i64 x;
drh3c657212009-11-17 23:59:58 +00006882 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006883 pIn3 = &aMem[pOp->p3];
6884 pOut = out2Prerelease(p, pOp);
6885 assert( pIn1->flags & MEM_Int );
6886 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006887 x = pIn1->u.i;
6888 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6889 /* If the LIMIT is less than or equal to zero, loop forever. This
6890 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6891 ** also loop forever. This is undocumented. In fact, one could argue
6892 ** that the loop should terminate. But assuming 1 billion iterations
6893 ** per second (far exceeding the capabilities of any current hardware)
6894 ** it would take nearly 300 years to actually reach the limit. So
6895 ** looping forever is a reasonable approximation. */
6896 pOut->u.i = -1;
6897 }else{
6898 pOut->u.i = x;
6899 }
drh15007a92006-01-08 18:10:17 +00006900 break;
6901}
6902
drhf99dd352016-12-18 17:42:00 +00006903/* Opcode: IfNotZero P1 P2 * * *
6904** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006905**
drh16897072015-03-07 00:57:37 +00006906** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006907** initially greater than zero, then decrement the value in register P1.
6908** If it is non-zero (negative or positive) and then also jump to P2.
6909** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006910*/
drh16897072015-03-07 00:57:37 +00006911case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006912 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006913 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006914 VdbeBranchTaken(pIn1->u.i<0, 2);
6915 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006916 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006917 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006918 }
6919 break;
6920}
6921
6922/* Opcode: DecrJumpZero P1 P2 * * *
6923** Synopsis: if (--r[P1])==0 goto P2
6924**
drhab5be2e2016-11-30 05:08:59 +00006925** Register P1 must hold an integer. Decrement the value in P1
6926** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006927*/
6928case OP_DecrJumpZero: { /* jump, in1 */
6929 pIn1 = &aMem[pOp->p1];
6930 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006931 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6932 VdbeBranchTaken(pIn1->u.i==0, 2);
6933 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006934 break;
6935}
6936
drh16897072015-03-07 00:57:37 +00006937
drh8f26da62018-07-05 21:22:57 +00006938/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006939** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006940**
drh8f26da62018-07-05 21:22:57 +00006941** Execute the xStep function for an aggregate.
6942** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006943** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006944** accumulator.
drhe5095352002-02-24 03:25:14 +00006945**
drh98757152008-01-09 23:04:12 +00006946** The P5 arguments are taken from register P2 and its
6947** successors.
drhe5095352002-02-24 03:25:14 +00006948*/
drh8f26da62018-07-05 21:22:57 +00006949/* Opcode: AggInverse * P2 P3 P4 P5
6950** Synopsis: accum=r[P3] inverse(r[P2@P5])
6951**
6952** Execute the xInverse function for an aggregate.
6953** The function has P5 arguments. P4 is a pointer to the
6954** FuncDef structure that specifies the function. Register P3 is the
6955** accumulator.
6956**
6957** The P5 arguments are taken from register P2 and its
6958** successors.
6959*/
6960/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006961** Synopsis: accum=r[P3] step(r[P2@P5])
6962**
dan9a947222018-06-14 19:06:36 +00006963** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6964** aggregate. The function has P5 arguments. P4 is a pointer to the
6965** FuncDef structure that specifies the function. Register P3 is the
6966** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006967**
6968** The P5 arguments are taken from register P2 and its
6969** successors.
6970**
6971** This opcode is initially coded as OP_AggStep0. On first evaluation,
6972** the FuncDef stored in P4 is converted into an sqlite3_context and
6973** the opcode is changed. In this way, the initialization of the
6974** sqlite3_context only happens once, instead of on each call to the
6975** step function.
6976*/
drh8f26da62018-07-05 21:22:57 +00006977case OP_AggInverse:
6978case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006979 int n;
drh9c7c9132015-06-26 18:16:52 +00006980 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006981
drh9c7c9132015-06-26 18:16:52 +00006982 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006983 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006984 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6985 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006986 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006987 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6988 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006989 if( pCtx==0 ) goto no_mem;
6990 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006991 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6992 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006993 pCtx->pFunc = pOp->p4.pFunc;
6994 pCtx->iOp = (int)(pOp - aOp);
6995 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006996 pCtx->skipFlag = 0;
6997 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006998 pCtx->argc = n;
6999 pOp->p4type = P4_FUNCCTX;
7000 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00007001
7002 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00007003 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00007004
drh8f26da62018-07-05 21:22:57 +00007005 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00007006 /* Fall through into OP_AggStep */
drh08b92082020-08-10 14:18:00 +00007007 /* no break */ deliberate_fall_through
drh9c7c9132015-06-26 18:16:52 +00007008}
drh8f26da62018-07-05 21:22:57 +00007009case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00007010 int i;
7011 sqlite3_context *pCtx;
7012 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00007013
7014 assert( pOp->p4type==P4_FUNCCTX );
7015 pCtx = pOp->p4.pCtx;
7016 pMem = &aMem[pOp->p3];
7017
drh2c885d02018-07-07 19:36:04 +00007018#ifdef SQLITE_DEBUG
7019 if( pOp->p1 ){
7020 /* This is an OP_AggInverse call. Verify that xStep has always
7021 ** been called at least once prior to any xInverse call. */
7022 assert( pMem->uTemp==0x1122e0e3 );
7023 }else{
7024 /* This is an OP_AggStep call. Mark it as such. */
7025 pMem->uTemp = 0x1122e0e3;
7026 }
7027#endif
7028
drh9c7c9132015-06-26 18:16:52 +00007029 /* If this function is inside of a trigger, the register array in aMem[]
7030 ** might change from one evaluation to the next. The next block of code
7031 ** checks to see if the register array has changed, and if so it
7032 ** reinitializes the relavant parts of the sqlite3_context object */
7033 if( pCtx->pMem != pMem ){
7034 pCtx->pMem = pMem;
7035 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7036 }
7037
7038#ifdef SQLITE_DEBUG
7039 for(i=0; i<pCtx->argc; i++){
7040 assert( memIsValid(pCtx->argv[i]) );
7041 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7042 }
7043#endif
7044
drhabfcea22005-09-06 20:36:48 +00007045 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00007046 assert( pCtx->pOut->flags==MEM_Null );
7047 assert( pCtx->isError==0 );
7048 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00007049#ifndef SQLITE_OMIT_WINDOWFUNC
7050 if( pOp->p1 ){
7051 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
7052 }else
7053#endif
7054 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
7055
drhf09ac0b2018-01-23 03:44:06 +00007056 if( pCtx->isError ){
7057 if( pCtx->isError>0 ){
7058 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00007059 rc = pCtx->isError;
7060 }
drhf09ac0b2018-01-23 03:44:06 +00007061 if( pCtx->skipFlag ){
7062 assert( pOp[-1].opcode==OP_CollSeq );
7063 i = pOp[-1].p1;
7064 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
7065 pCtx->skipFlag = 0;
7066 }
7067 sqlite3VdbeMemRelease(pCtx->pOut);
7068 pCtx->pOut->flags = MEM_Null;
7069 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00007070 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00007071 }
drhf09ac0b2018-01-23 03:44:06 +00007072 assert( pCtx->pOut->flags==MEM_Null );
7073 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00007074 break;
7075}
7076
drh8f26da62018-07-05 21:22:57 +00007077/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00007078** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00007079**
dan9a947222018-06-14 19:06:36 +00007080** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00007081** or window function. Execute the finalizer function
7082** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00007083**
7084** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00007085** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00007086** argument is not used by this opcode. It is only there to disambiguate
7087** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00007088** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00007089** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00007090*/
drh8f26da62018-07-05 21:22:57 +00007091/* Opcode: AggValue * P2 P3 P4 *
7092** Synopsis: r[P3]=value N=P2
7093**
7094** Invoke the xValue() function and store the result in register P3.
7095**
7096** P2 is the number of arguments that the step function takes and
7097** P4 is a pointer to the FuncDef for this function. The P2
7098** argument is not used by this opcode. It is only there to disambiguate
7099** functions that can take varying numbers of arguments. The
7100** P4 argument is only needed for the case where
7101** the step function was not previously called.
7102*/
7103case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00007104case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00007105 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00007106 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00007107 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00007108 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00007109 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00007110#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00007111 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00007112 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00007113 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00007114 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00007115 }else
7116#endif
drh8f26da62018-07-05 21:22:57 +00007117 {
7118 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
7119 }
dan67a9b8e2018-06-22 20:51:35 +00007120
drh4c8555f2009-06-25 01:47:11 +00007121 if( rc ){
drh22c17b82015-05-15 04:13:15 +00007122 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00007123 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00007124 }
drh2dca8682008-03-21 17:13:13 +00007125 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00007126 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00007127 if( sqlite3VdbeMemTooBig(pMem) ){
7128 goto too_big;
7129 }
drh5e00f6c2001-09-13 13:46:56 +00007130 break;
7131}
7132
dan5cf53532010-05-01 16:40:20 +00007133#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00007134/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007135**
7136** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00007137** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7138** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00007139** SQLITE_BUSY or not, respectively. Write the number of pages in the
7140** WAL after the checkpoint into mem[P3+1] and the number of pages
7141** in the WAL that have been checkpointed after the checkpoint
7142** completes into mem[P3+2]. However on an error, mem[P3+1] and
7143** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00007144*/
7145case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00007146 int i; /* Loop counter */
7147 int aRes[3]; /* Results */
7148 Mem *pMem; /* Write results here */
7149
drh9e92a472013-06-27 17:40:30 +00007150 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00007151 aRes[0] = 0;
7152 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00007153 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
7154 || pOp->p2==SQLITE_CHECKPOINT_FULL
7155 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00007156 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00007157 );
drh30aa3b92011-02-07 23:56:01 +00007158 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00007159 if( rc ){
7160 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00007161 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00007162 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00007163 }
drh30aa3b92011-02-07 23:56:01 +00007164 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
7165 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
7166 }
dan7c246102010-04-12 19:00:29 +00007167 break;
7168};
dan5cf53532010-05-01 16:40:20 +00007169#endif
drh5e00f6c2001-09-13 13:46:56 +00007170
drhcac29a62010-07-02 19:36:52 +00007171#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00007172/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007173**
7174** Change the journal mode of database P1 to P3. P3 must be one of the
7175** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7176** modes (delete, truncate, persist, off and memory), this is a simple
7177** operation. No IO is required.
7178**
7179** If changing into or out of WAL mode the procedure is more complicated.
7180**
7181** Write a string containing the final journal-mode to register P2.
7182*/
drh27a348c2015-04-13 19:14:06 +00007183case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00007184 Btree *pBt; /* Btree to change journal mode of */
7185 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00007186 int eNew; /* New journal mode */
7187 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00007188#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00007189 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00007190#endif
dane04dc882010-04-20 18:53:15 +00007191
drh27a348c2015-04-13 19:14:06 +00007192 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00007193 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00007194 assert( eNew==PAGER_JOURNALMODE_DELETE
7195 || eNew==PAGER_JOURNALMODE_TRUNCATE
7196 || eNew==PAGER_JOURNALMODE_PERSIST
7197 || eNew==PAGER_JOURNALMODE_OFF
7198 || eNew==PAGER_JOURNALMODE_MEMORY
7199 || eNew==PAGER_JOURNALMODE_WAL
7200 || eNew==PAGER_JOURNALMODE_QUERY
7201 );
7202 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00007203 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00007204
dane04dc882010-04-20 18:53:15 +00007205 pBt = db->aDb[pOp->p1].pBt;
7206 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00007207 eOld = sqlite3PagerGetJournalMode(pPager);
7208 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
7209 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00007210
7211#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00007212 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00007213
drhd80b2332010-05-01 00:59:37 +00007214 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00007215 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00007216 */
7217 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00007218 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00007219 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00007220 ){
drh0b9b4302010-06-11 17:01:24 +00007221 eNew = eOld;
dane180c292010-04-26 17:42:56 +00007222 }
7223
drh0b9b4302010-06-11 17:01:24 +00007224 if( (eNew!=eOld)
7225 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
7226 ){
danc0537fe2013-06-28 19:41:43 +00007227 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00007228 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00007229 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00007230 "cannot change %s wal mode from within a transaction",
7231 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
7232 );
drh9467abf2016-02-17 18:44:11 +00007233 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00007234 }else{
7235
7236 if( eOld==PAGER_JOURNALMODE_WAL ){
7237 /* If leaving WAL mode, close the log file. If successful, the call
7238 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7239 ** file. An EXCLUSIVE lock may still be held on the database file
7240 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00007241 */
dan7fb89902016-08-12 16:21:15 +00007242 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00007243 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00007244 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00007245 }
drh242c4f72010-06-22 14:49:39 +00007246 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
7247 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7248 ** as an intermediate */
7249 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00007250 }
7251
7252 /* Open a transaction on the database file. Regardless of the journal
7253 ** mode, this transaction always uses a rollback journal.
7254 */
drh99744fa2020-08-25 19:09:07 +00007255 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE );
drh0b9b4302010-06-11 17:01:24 +00007256 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00007257 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00007258 }
7259 }
7260 }
dan5cf53532010-05-01 16:40:20 +00007261#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00007262
drh9467abf2016-02-17 18:44:11 +00007263 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00007264 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00007265
dane04dc882010-04-20 18:53:15 +00007266 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00007267 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00007268 pOut->n = sqlite3Strlen30(pOut->z);
7269 pOut->enc = SQLITE_UTF8;
7270 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00007271 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00007272 break;
drhcac29a62010-07-02 19:36:52 +00007273};
7274#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00007275
drhfdbcdee2007-03-27 14:44:50 +00007276#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00007277/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00007278**
drh9ef5e772016-08-19 14:20:56 +00007279** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7280** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00007281**
drh2f6239e2018-12-08 00:43:08 +00007282** If P2 is not zero, then it is a register holding a string which is
7283** the file into which the result of vacuum should be written. When
7284** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00007285*/
drh9cbf3422008-01-17 16:22:13 +00007286case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00007287 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00007288 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
7289 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00007290 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00007291 break;
7292}
drh154d4b22006-09-21 11:02:16 +00007293#endif
drh6f8c91c2003-12-07 00:24:35 +00007294
danielk1977dddbcdc2007-04-26 14:42:34 +00007295#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00007296/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00007297**
7298** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00007299** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00007300** P2. Otherwise, fall through to the next instruction.
7301*/
drh9cbf3422008-01-17 16:22:13 +00007302case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00007303 Btree *pBt;
7304
7305 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007306 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00007307 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00007308 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00007309 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00007310 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00007311 if( rc ){
7312 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00007313 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00007314 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00007315 }
7316 break;
7317}
7318#endif
7319
drhba968db2018-07-24 22:02:12 +00007320/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00007321**
drh25df48d2014-07-22 14:58:12 +00007322** Cause precompiled statements to expire. When an expired statement
7323** is executed using sqlite3_step() it will either automatically
7324** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7325** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00007326**
7327** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00007328** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00007329**
7330** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7331** then running SQL statements are allowed to continue to run to completion.
7332** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7333** that might help the statement run faster but which does not affect the
7334** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00007335*/
drh9cbf3422008-01-17 16:22:13 +00007336case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00007337 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00007338 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00007339 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00007340 }else{
drhba968db2018-07-24 22:02:12 +00007341 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00007342 }
7343 break;
7344}
7345
drh7b14b652019-12-29 22:08:20 +00007346/* Opcode: CursorLock P1 * * * *
7347**
7348** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7349** written by an other cursor.
7350*/
7351case OP_CursorLock: {
7352 VdbeCursor *pC;
7353 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7354 pC = p->apCsr[pOp->p1];
7355 assert( pC!=0 );
7356 assert( pC->eCurType==CURTYPE_BTREE );
7357 sqlite3BtreeCursorPin(pC->uc.pCursor);
7358 break;
7359}
7360
7361/* Opcode: CursorUnlock P1 * * * *
7362**
7363** Unlock the btree to which cursor P1 is pointing so that it can be
7364** written by other cursors.
7365*/
7366case OP_CursorUnlock: {
7367 VdbeCursor *pC;
7368 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7369 pC = p->apCsr[pOp->p1];
7370 assert( pC!=0 );
7371 assert( pC->eCurType==CURTYPE_BTREE );
7372 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
7373 break;
7374}
7375
danielk1977c00da102006-01-07 13:21:04 +00007376#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00007377/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00007378** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00007379**
7380** Obtain a lock on a particular table. This instruction is only used when
7381** the shared-cache feature is enabled.
7382**
danielk197796d48e92009-06-29 06:00:37 +00007383** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00007384** on which the lock is acquired. A readlock is obtained if P3==0 or
7385** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00007386**
7387** P2 contains the root-page of the table to lock.
7388**
drh66a51672008-01-03 00:01:23 +00007389** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00007390** used to generate an error message if the lock cannot be obtained.
7391*/
drh9cbf3422008-01-17 16:22:13 +00007392case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00007393 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00007394 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00007395 int p1 = pOp->p1;
7396 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007397 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00007398 assert( isWriteLock==0 || isWriteLock==1 );
7399 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00007400 if( rc ){
7401 if( (rc&0xFF)==SQLITE_LOCKED ){
7402 const char *z = pOp->p4.z;
7403 sqlite3VdbeError(p, "database table is locked: %s", z);
7404 }
7405 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00007406 }
danielk1977c00da102006-01-07 13:21:04 +00007407 }
7408 break;
7409}
drhb9bb7c12006-06-11 23:41:55 +00007410#endif /* SQLITE_OMIT_SHARED_CACHE */
7411
7412#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007413/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007414**
danielk19773e3a84d2008-08-01 17:37:40 +00007415** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7416** xBegin method for that table.
7417**
7418** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00007419** within a callback to a virtual table xSync() method. If it is, the error
7420** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00007421*/
drh9cbf3422008-01-17 16:22:13 +00007422case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00007423 VTable *pVTab;
7424 pVTab = pOp->p4.pVtab;
7425 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00007426 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00007427 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00007428 break;
7429}
7430#endif /* SQLITE_OMIT_VIRTUALTABLE */
7431
7432#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00007433/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00007434**
dan73779452015-03-19 18:56:17 +00007435** P2 is a register that holds the name of a virtual table in database
7436** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00007437*/
drh9cbf3422008-01-17 16:22:13 +00007438case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00007439 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00007440 const char *zTab; /* Name of the virtual table */
7441
dan73779452015-03-19 18:56:17 +00007442 memset(&sMem, 0, sizeof(sMem));
7443 sMem.db = db;
drh47464062015-03-21 12:22:16 +00007444 /* Because P2 is always a static string, it is impossible for the
7445 ** sqlite3VdbeMemCopy() to fail */
7446 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
7447 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00007448 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00007449 assert( rc==SQLITE_OK );
7450 zTab = (const char*)sqlite3_value_text(&sMem);
7451 assert( zTab || db->mallocFailed );
7452 if( zTab ){
7453 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00007454 }
7455 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00007456 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007457 break;
7458}
7459#endif /* SQLITE_OMIT_VIRTUALTABLE */
7460
7461#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007462/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007463**
drh66a51672008-01-03 00:01:23 +00007464** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00007465** of that table.
drhb9bb7c12006-06-11 23:41:55 +00007466*/
drh9cbf3422008-01-17 16:22:13 +00007467case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00007468 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00007469 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00007470 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00007471 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00007472 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007473 break;
7474}
7475#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00007476
drh9eff6162006-06-12 21:59:13 +00007477#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007478/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00007479**
drh66a51672008-01-03 00:01:23 +00007480** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00007481** P1 is a cursor number. This opcode opens a cursor to the virtual
7482** table and stores that cursor in P1.
7483*/
drh9cbf3422008-01-17 16:22:13 +00007484case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00007485 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00007486 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00007487 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007488 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007489
drh1713afb2013-06-28 01:24:57 +00007490 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00007491 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00007492 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00007493 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007494 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7495 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007496 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007497 }
7498 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00007499 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00007500 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007501 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007502
drh9467abf2016-02-17 18:44:11 +00007503 /* Initialize sqlite3_vtab_cursor base class */
7504 pVCur->pVtab = pVtab;
7505
7506 /* Initialize vdbe cursor object */
7507 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
7508 if( pCur ){
7509 pCur->uc.pVCur = pVCur;
7510 pVtab->nRef++;
7511 }else{
7512 assert( db->mallocFailed );
7513 pModule->xClose(pVCur);
7514 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007515 }
drh9eff6162006-06-12 21:59:13 +00007516 break;
7517}
7518#endif /* SQLITE_OMIT_VIRTUALTABLE */
7519
7520#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00007521/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00007522** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00007523**
7524** P1 is a cursor opened using VOpen. P2 is an address to jump to if
7525** the filtered result set is empty.
7526**
drh66a51672008-01-03 00:01:23 +00007527** P4 is either NULL or a string that was generated by the xBestIndex
7528** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00007529** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00007530**
drh9eff6162006-06-12 21:59:13 +00007531** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00007532** by P1. The integer query plan parameter to xFilter is stored in register
7533** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00007534** xFilter method. Registers P3+2..P3+1+argc are the argc
7535** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00007536** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00007537**
danielk19776dbee812008-01-03 18:39:41 +00007538** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00007539*/
drh9cbf3422008-01-17 16:22:13 +00007540case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00007541 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00007542 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007543 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007544 Mem *pQuery;
7545 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00007546 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00007547 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00007548 VdbeCursor *pCur;
7549 int res;
7550 int i;
7551 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007552
drha6c2ed92009-11-14 23:22:23 +00007553 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00007554 pArgc = &pQuery[1];
7555 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00007556 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00007557 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00007558 assert( pCur->eCurType==CURTYPE_VTAB );
7559 pVCur = pCur->uc.pVCur;
7560 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00007561 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007562
drh9cbf3422008-01-17 16:22:13 +00007563 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00007564 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00007565 nArg = (int)pArgc->u.i;
7566 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007567
drh644a5292006-12-20 14:53:38 +00007568 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00007569 res = 0;
7570 apArg = p->apArg;
7571 for(i = 0; i<nArg; i++){
7572 apArg[i] = &pArgc[i+1];
7573 }
drhc960dcb2015-11-20 19:22:01 +00007574 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00007575 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007576 if( rc ) goto abort_due_to_error;
7577 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00007578 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00007579 VdbeBranchTaken(res!=0,2);
7580 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00007581 break;
7582}
7583#endif /* SQLITE_OMIT_VIRTUALTABLE */
7584
7585#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00007586/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007587** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007588**
drh6f390be2018-01-11 17:04:26 +00007589** Store in register P3 the value of the P2-th column of
7590** the current row of the virtual-table of cursor P1.
7591**
7592** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007593** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007594** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7595** function to return true inside the xColumn method of the virtual
7596** table implementation. The P5 column might also contain other
7597** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7598** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007599*/
7600case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007601 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007602 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007603 Mem *pDest;
7604 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007605
drhdfe88ec2008-11-03 20:55:06 +00007606 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007607 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007608 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007609 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007610 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007611 if( pCur->nullRow ){
7612 sqlite3VdbeMemSetNull(pDest);
7613 break;
7614 }
drhc960dcb2015-11-20 19:22:01 +00007615 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007616 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007617 assert( pModule->xColumn );
7618 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007619 sContext.pOut = pDest;
drh75f10762019-12-14 18:08:22 +00007620 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
drh09d00b22018-09-27 20:20:01 +00007621 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007622 sqlite3VdbeMemSetNull(pDest);
7623 pDest->flags = MEM_Null|MEM_Zero;
7624 pDest->u.nZero = 0;
7625 }else{
7626 MemSetTypeFlag(pDest, MEM_Null);
7627 }
drhc960dcb2015-11-20 19:22:01 +00007628 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007629 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007630 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007631 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007632 rc = sContext.isError;
7633 }
drh9bd038f2014-08-27 14:14:06 +00007634 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007635 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007636 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007637
drhde4fcfd2008-01-19 23:50:26 +00007638 if( sqlite3VdbeMemTooBig(pDest) ){
7639 goto too_big;
7640 }
drh9467abf2016-02-17 18:44:11 +00007641 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007642 break;
7643}
7644#endif /* SQLITE_OMIT_VIRTUALTABLE */
7645
7646#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007647/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007648**
7649** Advance virtual table P1 to the next row in its result set and
7650** jump to instruction P2. Or, if the virtual table has reached
7651** the end of its result set, then fall through to the next instruction.
7652*/
drh9cbf3422008-01-17 16:22:13 +00007653case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007654 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007655 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007656 int res;
drh856c1032009-06-02 15:21:42 +00007657 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007658
drhc54a6172009-06-02 16:06:03 +00007659 res = 0;
drh856c1032009-06-02 15:21:42 +00007660 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007661 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007662 if( pCur->nullRow ){
7663 break;
7664 }
drhc960dcb2015-11-20 19:22:01 +00007665 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007666 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007667 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007668
drhde4fcfd2008-01-19 23:50:26 +00007669 /* Invoke the xNext() method of the module. There is no way for the
7670 ** underlying implementation to return an error if one occurs during
7671 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7672 ** data is available) and the error code returned when xColumn or
7673 ** some other method is next invoked on the save virtual table cursor.
7674 */
drhc960dcb2015-11-20 19:22:01 +00007675 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007676 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007677 if( rc ) goto abort_due_to_error;
7678 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007679 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007680 if( !res ){
7681 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007682 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007683 }
drh49afe3a2013-07-10 03:05:14 +00007684 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007685}
7686#endif /* SQLITE_OMIT_VIRTUALTABLE */
7687
danielk1977182c4ba2007-06-27 15:53:34 +00007688#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007689/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007690**
drh66a51672008-01-03 00:01:23 +00007691** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007692** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007693** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007694*/
drh9cbf3422008-01-17 16:22:13 +00007695case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007696 sqlite3_vtab *pVtab;
7697 Mem *pName;
dan34566c42018-09-20 17:21:21 +00007698 int isLegacy;
7699
7700 isLegacy = (db->flags & SQLITE_LegacyAlter);
7701 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00007702 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007703 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007704 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007705 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007706 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007707 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007708 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007709 testcase( pName->enc==SQLITE_UTF8 );
7710 testcase( pName->enc==SQLITE_UTF16BE );
7711 testcase( pName->enc==SQLITE_UTF16LE );
7712 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007713 if( rc ) goto abort_due_to_error;
7714 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00007715 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00007716 sqlite3VtabImportErrmsg(p, pVtab);
7717 p->expired = 0;
7718 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007719 break;
7720}
7721#endif
drh4cbdda92006-06-14 19:00:20 +00007722
7723#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007724/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007725** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007726**
drh66a51672008-01-03 00:01:23 +00007727** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007728** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007729** are contiguous memory cells starting at P3 to pass to the xUpdate
7730** invocation. The value in register (P3+P2-1) corresponds to the
7731** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007732**
7733** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007734** The argv[0] element (which corresponds to memory cell P3)
7735** is the rowid of a row to delete. If argv[0] is NULL then no
7736** deletion occurs. The argv[1] element is the rowid of the new
7737** row. This can be NULL to have the virtual table select the new
7738** rowid for itself. The subsequent elements in the array are
7739** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007740**
7741** If P2==1 then no insert is performed. argv[0] is the rowid of
7742** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007743**
7744** P1 is a boolean flag. If it is set to true and the xUpdate call
7745** is successful, then the value returned by sqlite3_last_insert_rowid()
7746** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007747**
7748** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7749** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007750*/
drh9cbf3422008-01-17 16:22:13 +00007751case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007752 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007753 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007754 int nArg;
7755 int i;
7756 sqlite_int64 rowid;
7757 Mem **apArg;
7758 Mem *pX;
7759
danb061d052011-04-25 18:49:57 +00007760 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7761 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7762 );
drh9e92a472013-06-27 17:40:30 +00007763 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007764 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007765 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007766 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007767 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7768 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007769 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007770 }
7771 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007772 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007773 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007774 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007775 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007776 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007777 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007778 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007779 assert( memIsValid(pX) );
7780 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007781 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007782 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007783 }
danb061d052011-04-25 18:49:57 +00007784 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007785 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007786 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007787 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007788 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007789 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007790 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007791 }
drhd91c1a12013-02-09 13:58:25 +00007792 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007793 if( pOp->p5==OE_Ignore ){
7794 rc = SQLITE_OK;
7795 }else{
7796 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7797 }
7798 }else{
7799 p->nChange++;
7800 }
drh9467abf2016-02-17 18:44:11 +00007801 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007802 }
drh4cbdda92006-06-14 19:00:20 +00007803 break;
danielk1977399918f2006-06-14 13:03:23 +00007804}
7805#endif /* SQLITE_OMIT_VIRTUALTABLE */
7806
danielk197759a93792008-05-15 17:48:20 +00007807#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7808/* Opcode: Pagecount P1 P2 * * *
7809**
7810** Write the current number of pages in database P1 to memory cell P2.
7811*/
drh27a348c2015-04-13 19:14:06 +00007812case OP_Pagecount: { /* out2 */
7813 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007814 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007815 break;
7816}
7817#endif
7818
drh60ac3f42010-11-23 18:59:27 +00007819
7820#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7821/* Opcode: MaxPgcnt P1 P2 P3 * *
7822**
7823** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007824** Do not let the maximum page count fall below the current page count and
7825** do not change the maximum page count value if P3==0.
7826**
drh60ac3f42010-11-23 18:59:27 +00007827** Store the maximum page count after the change in register P2.
7828*/
drh27a348c2015-04-13 19:14:06 +00007829case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007830 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007831 Btree *pBt;
7832
drh27a348c2015-04-13 19:14:06 +00007833 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007834 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007835 newMax = 0;
7836 if( pOp->p3 ){
7837 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007838 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007839 }
7840 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007841 break;
7842}
7843#endif
7844
drh920cf592019-10-30 16:29:02 +00007845/* Opcode: Function P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00007846** Synopsis: r[P3]=func(r[P2@NP])
drh3e34eab2017-07-19 19:48:40 +00007847**
7848** Invoke a user function (P4 is a pointer to an sqlite3_context object that
drh920cf592019-10-30 16:29:02 +00007849** contains a pointer to the function to be run) with arguments taken
7850** from register P2 and successors. The number of arguments is in
7851** the sqlite3_context object that P4 points to.
7852** The result of the function is stored
drh3e34eab2017-07-19 19:48:40 +00007853** in register P3. Register P3 must not be one of the function inputs.
7854**
7855** P1 is a 32-bit bitmask indicating whether or not each argument to the
7856** function was determined to be constant at compile time. If the first
7857** argument was constant then bit 0 of P1 is set. This is used to determine
7858** whether meta data associated with a user function argument using the
7859** sqlite3_set_auxdata() API may be safely retained until the next
7860** invocation of this opcode.
7861**
drh920cf592019-10-30 16:29:02 +00007862** See also: AggStep, AggFinal, PureFunc
drh3e34eab2017-07-19 19:48:40 +00007863*/
drh920cf592019-10-30 16:29:02 +00007864/* Opcode: PureFunc P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00007865** Synopsis: r[P3]=func(r[P2@NP])
drh920cf592019-10-30 16:29:02 +00007866**
7867** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7868** contains a pointer to the function to be run) with arguments taken
7869** from register P2 and successors. The number of arguments is in
7870** the sqlite3_context object that P4 points to.
7871** The result of the function is stored
7872** in register P3. Register P3 must not be one of the function inputs.
7873**
7874** P1 is a 32-bit bitmask indicating whether or not each argument to the
7875** function was determined to be constant at compile time. If the first
7876** argument was constant then bit 0 of P1 is set. This is used to determine
7877** whether meta data associated with a user function argument using the
7878** sqlite3_set_auxdata() API may be safely retained until the next
7879** invocation of this opcode.
7880**
7881** This opcode works exactly like OP_Function. The only difference is in
7882** its name. This opcode is used in places where the function must be
7883** purely non-deterministic. Some built-in date/time functions can be
7884** either determinitic of non-deterministic, depending on their arguments.
7885** When those function are used in a non-deterministic way, they will check
7886** to see if they were called using OP_PureFunc instead of OP_Function, and
7887** if they were, they throw an error.
7888**
7889** See also: AggStep, AggFinal, Function
7890*/
mistachkin758784d2018-07-25 15:12:29 +00007891case OP_PureFunc: /* group */
7892case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007893 int i;
7894 sqlite3_context *pCtx;
7895
7896 assert( pOp->p4type==P4_FUNCCTX );
7897 pCtx = pOp->p4.pCtx;
7898
7899 /* If this function is inside of a trigger, the register array in aMem[]
7900 ** might change from one evaluation to the next. The next block of code
7901 ** checks to see if the register array has changed, and if so it
7902 ** reinitializes the relavant parts of the sqlite3_context object */
7903 pOut = &aMem[pOp->p3];
7904 if( pCtx->pOut != pOut ){
drh920cf592019-10-30 16:29:02 +00007905 pCtx->pVdbe = p;
drh3e34eab2017-07-19 19:48:40 +00007906 pCtx->pOut = pOut;
7907 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7908 }
drh920cf592019-10-30 16:29:02 +00007909 assert( pCtx->pVdbe==p );
drh3e34eab2017-07-19 19:48:40 +00007910
7911 memAboutToChange(p, pOut);
7912#ifdef SQLITE_DEBUG
7913 for(i=0; i<pCtx->argc; i++){
7914 assert( memIsValid(pCtx->argv[i]) );
7915 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7916 }
7917#endif
7918 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007919 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007920 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7921
7922 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007923 if( pCtx->isError ){
7924 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007925 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7926 rc = pCtx->isError;
7927 }
7928 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007929 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007930 if( rc ) goto abort_due_to_error;
7931 }
7932
7933 /* Copy the result of the function into register P3 */
7934 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7935 sqlite3VdbeChangeEncoding(pOut, encoding);
7936 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7937 }
7938
7939 REGISTER_TRACE(pOp->p3, pOut);
7940 UPDATE_MAX_BLOBSIZE(pOut);
7941 break;
7942}
7943
drhf259df52017-12-27 20:38:35 +00007944/* Opcode: Trace P1 P2 * P4 *
7945**
7946** Write P4 on the statement trace output if statement tracing is
7947** enabled.
7948**
7949** Operand P1 must be 0x7fffffff and P2 must positive.
7950*/
drh74588ce2017-09-13 00:13:05 +00007951/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007952** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007953**
7954** Programs contain a single instance of this opcode as the very first
7955** opcode.
drh949f9cd2008-01-12 21:35:57 +00007956**
7957** If tracing is enabled (by the sqlite3_trace()) interface, then
7958** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007959** Or if P4 is blank, use the string returned by sqlite3_sql().
7960**
7961** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007962**
7963** Increment the value of P1 so that OP_Once opcodes will jump the
7964** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007965**
7966** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7967** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007968*/
drhf259df52017-12-27 20:38:35 +00007969case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007970case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007971 int i;
drhb9f47992018-01-24 12:14:43 +00007972#ifndef SQLITE_OMIT_TRACE
7973 char *zTrace;
7974#endif
drh5fe63bf2016-07-25 02:42:22 +00007975
7976 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7977 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7978 **
7979 ** This assert() provides evidence for:
7980 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7981 ** would have been returned by the legacy sqlite3_trace() interface by
7982 ** using the X argument when X begins with "--" and invoking
7983 ** sqlite3_expanded_sql(P) otherwise.
7984 */
7985 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007986
7987 /* OP_Init is always instruction 0 */
7988 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007989
drhaceb31b2014-02-08 01:40:27 +00007990#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007991 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007992 && !p->doingRerun
7993 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7994 ){
drh3d2a5292016-07-13 22:55:01 +00007995#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007996 if( db->mTrace & SQLITE_TRACE_LEGACY ){
drh5fe63bf2016-07-25 02:42:22 +00007997 char *z = sqlite3VdbeExpandSql(p, zTrace);
drh08b92082020-08-10 14:18:00 +00007998 db->trace.xLegacy(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007999 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00008000 }else
drh3d2a5292016-07-13 22:55:01 +00008001#endif
drh7adbcff2017-03-20 15:29:28 +00008002 if( db->nVdbeExec>1 ){
8003 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
drh08b92082020-08-10 14:18:00 +00008004 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
drh7adbcff2017-03-20 15:29:28 +00008005 sqlite3DbFree(db, z);
8006 }else{
drh08b92082020-08-10 14:18:00 +00008007 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00008008 }
drh949f9cd2008-01-12 21:35:57 +00008009 }
drh8f8b2312013-10-18 20:03:43 +00008010#ifdef SQLITE_USE_FCNTL_TRACE
8011 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
8012 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00008013 int j;
8014 for(j=0; j<db->nDb; j++){
8015 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
8016 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00008017 }
8018 }
8019#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00008020#ifdef SQLITE_DEBUG
8021 if( (db->flags & SQLITE_SqlTrace)!=0
8022 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
8023 ){
8024 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
8025 }
8026#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00008027#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00008028 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00008029 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00008030 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00008031 for(i=1; i<p->nOp; i++){
8032 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
8033 }
8034 pOp->p1 = 0;
8035 }
8036 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00008037 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00008038 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00008039}
drh949f9cd2008-01-12 21:35:57 +00008040
drh28935362013-12-07 20:39:19 +00008041#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00008042/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00008043**
8044** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00008045** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8046** to values currently held in registers. TK_COLUMN terms in the P4
8047** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00008048*/
8049case OP_CursorHint: {
8050 VdbeCursor *pC;
8051
8052 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8053 assert( pOp->p4type==P4_EXPR );
8054 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00008055 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00008056 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00008057 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
8058 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00008059 }
drh28935362013-12-07 20:39:19 +00008060 break;
8061}
8062#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00008063
drh4031baf2018-05-28 17:31:20 +00008064#ifdef SQLITE_DEBUG
8065/* Opcode: Abortable * * * * *
8066**
8067** Verify that an Abort can happen. Assert if an Abort at this point
8068** might cause database corruption. This opcode only appears in debugging
8069** builds.
8070**
8071** An Abort is safe if either there have been no writes, or if there is
8072** an active statement journal.
8073*/
8074case OP_Abortable: {
8075 sqlite3VdbeAssertAbortable(p);
8076 break;
8077}
8078#endif
8079
drh13d79502019-12-23 02:18:49 +00008080#ifdef SQLITE_DEBUG
drh3aef2fb2020-01-02 17:46:02 +00008081/* Opcode: ReleaseReg P1 P2 P3 * P5
drh13d79502019-12-23 02:18:49 +00008082** Synopsis: release r[P1@P2] mask P3
8083**
8084** Release registers from service. Any content that was in the
8085** the registers is unreliable after this opcode completes.
8086**
8087** The registers released will be the P2 registers starting at P1,
8088** except if bit ii of P3 set, then do not release register P1+ii.
8089** In other words, P3 is a mask of registers to preserve.
8090**
8091** Releasing a register clears the Mem.pScopyFrom pointer. That means
8092** that if the content of the released register was set using OP_SCopy,
8093** a change to the value of the source register for the OP_SCopy will no longer
8094** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8095**
drh3aef2fb2020-01-02 17:46:02 +00008096** If P5 is set, then all released registers have their type set
8097** to MEM_Undefined so that any subsequent attempt to read the released
drh13d79502019-12-23 02:18:49 +00008098** register (before it is reinitialized) will generate an assertion fault.
drh3aef2fb2020-01-02 17:46:02 +00008099**
8100** P5 ought to be set on every call to this opcode.
8101** However, there are places in the code generator will release registers
drh13d79502019-12-23 02:18:49 +00008102** before their are used, under the (valid) assumption that the registers
8103** will not be reallocated for some other purpose before they are used and
8104** hence are safe to release.
8105**
8106** This opcode is only available in testing and debugging builds. It is
8107** not generated for release builds. The purpose of this opcode is to help
8108** validate the generated bytecode. This opcode does not actually contribute
8109** to computing an answer.
8110*/
8111case OP_ReleaseReg: {
8112 Mem *pMem;
8113 int i;
8114 u32 constMask;
8115 assert( pOp->p1>0 );
8116 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
8117 pMem = &aMem[pOp->p1];
8118 constMask = pOp->p3;
8119 for(i=0; i<pOp->p2; i++, pMem++){
drh7edce5e2019-12-23 13:24:34 +00008120 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
drh13d79502019-12-23 02:18:49 +00008121 pMem->pScopyFrom = 0;
drh3aef2fb2020-01-02 17:46:02 +00008122 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
drh13d79502019-12-23 02:18:49 +00008123 }
8124 }
8125 break;
8126}
8127#endif
8128
drh91fd4d42008-01-19 20:11:25 +00008129/* Opcode: Noop * * * * *
8130**
8131** Do nothing. This instruction is often useful as a jump
8132** destination.
drh5e00f6c2001-09-13 13:46:56 +00008133*/
drh91fd4d42008-01-19 20:11:25 +00008134/*
8135** The magic Explain opcode are only inserted when explain==2 (which
8136** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8137** This opcode records information from the optimizer. It is the
8138** the same as a no-op. This opcodesnever appears in a real VM program.
8139*/
drh4031baf2018-05-28 17:31:20 +00008140default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00008141 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00008142
drh5e00f6c2001-09-13 13:46:56 +00008143 break;
8144}
8145
8146/*****************************************************************************
8147** The cases of the switch statement above this line should all be indented
8148** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8149** readability. From this point on down, the normal indentation rules are
8150** restored.
8151*****************************************************************************/
8152 }
drh6e142f52000-06-08 13:36:40 +00008153
drh7b396862003-01-01 23:06:20 +00008154#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00008155 {
drh35043cc2018-02-12 20:27:34 +00008156 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00008157 if( endTime>start ) pOrigOp->cycles += endTime - start;
8158 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00008159 }
drh7b396862003-01-01 23:06:20 +00008160#endif
8161
drh6e142f52000-06-08 13:36:40 +00008162 /* The following code adds nothing to the actual functionality
8163 ** of the program. It is only here for testing and debugging.
8164 ** On the other hand, it does burn CPU cycles every time through
8165 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8166 */
8167#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00008168 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00008169
drhcf1023c2007-05-08 20:59:49 +00008170#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00008171 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00008172 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00008173 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00008174 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00008175 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00008176 }
drh7cc84c22016-04-11 13:36:42 +00008177 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00008178 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00008179 }
drh17aceeb2020-01-04 19:12:13 +00008180 if( opProperty==0xff ){
8181 /* Never happens. This code exists to avoid a harmless linkage
8182 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8183 ** used. */
8184 sqlite3VdbeRegisterDump(p);
8185 }
drh75897232000-05-29 14:26:00 +00008186 }
danielk1977b5402fb2005-01-12 07:15:04 +00008187#endif /* SQLITE_DEBUG */
8188#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00008189 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00008190
drha05a7222008-01-19 03:35:58 +00008191 /* If we reach this point, it means that execution is finished with
8192 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00008193 */
drh9467abf2016-02-17 18:44:11 +00008194abort_due_to_error:
drhf56a4bf2020-11-18 21:50:05 +00008195 if( db->mallocFailed ){
8196 rc = SQLITE_NOMEM_BKPT;
8197 }else if( rc==SQLITE_IOERR_CORRUPTFS ){
8198 rc = SQLITE_CORRUPT_BKPT;
8199 }
drha05a7222008-01-19 03:35:58 +00008200 assert( rc );
drh9467abf2016-02-17 18:44:11 +00008201 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
8202 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
8203 }
drha05a7222008-01-19 03:35:58 +00008204 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00008205 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00008206 testcase( sqlite3GlobalConfig.xLog!=0 );
8207 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00008208 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00008209 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00008210 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00008211 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00008212 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00008213 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00008214 }
drh900b31e2007-08-28 02:27:51 +00008215
8216 /* This is the only way out of this procedure. We have to
8217 ** release the mutexes on btrees that were acquired at the
8218 ** top. */
8219vdbe_return:
drhc332e042019-02-12 21:04:33 +00008220#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00008221 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
8222 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00008223 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +00008224 nProgressLimit = LARGEST_UINT64;
drhc332e042019-02-12 21:04:33 +00008225 rc = SQLITE_INTERRUPT;
8226 goto abort_due_to_error;
8227 }
8228 }
8229#endif
drh9b47ee32013-08-20 03:13:51 +00008230 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00008231 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00008232 assert( rc!=SQLITE_OK || nExtraDelete==0
8233 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
8234 );
drhb86ccfb2003-01-28 23:13:10 +00008235 return rc;
8236
drh023ae032007-05-08 12:12:16 +00008237 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8238 ** is encountered.
8239 */
8240too_big:
drh22c17b82015-05-15 04:13:15 +00008241 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00008242 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00008243 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00008244
drh98640a32007-06-07 19:08:32 +00008245 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00008246 */
8247no_mem:
drh4a642b62016-02-05 01:55:27 +00008248 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00008249 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00008250 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00008251 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008252
danielk19776f8a5032004-05-10 10:34:51 +00008253 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00008254 ** flag.
8255 */
8256abort_due_to_interrupt:
dan892edb62020-03-30 13:35:05 +00008257 assert( AtomicLoad(&db->u1.isInterrupted) );
drh56f18732020-06-03 15:59:22 +00008258 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00008259 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008260}