blob: cb4e5ea8d37fdfeed279126000917f2cb865498a [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*/
danielk1977be718892006-06-23 08:05:19 +0000275 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000276 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000277 }
drh322f2852014-09-19 00:43:39 +0000278 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000279 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000280 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000281 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000282 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000283 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000284 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000285 if( eCurType==CURTYPE_BTREE ){
286 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000287 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000288 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000289 }
danielk197794eb6a12005-12-15 15:22:08 +0000290 }
drh4774b132004-06-12 20:12:51 +0000291 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000292}
293
danielk19773d1bfea2004-05-14 11:00:53 +0000294/*
drh8a3884e2019-05-29 21:18:27 +0000295** The string in pRec is known to look like an integer and to have a
296** floating point value of rValue. Return true and set *piValue to the
297** integer value if the string is in range to be an integer. Otherwise,
298** return false.
299*/
300static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
301 i64 iValue = (double)rValue;
302 if( sqlite3RealSameAsInt(rValue,iValue) ){
drhc285ded2019-06-10 18:33:16 +0000303 *piValue = iValue;
304 return 1;
drh8a3884e2019-05-29 21:18:27 +0000305 }
306 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
307}
308
309/*
drh29d72102006-02-09 22:13:41 +0000310** Try to convert a value into a numeric representation if we can
311** do so without loss of information. In other words, if the string
312** looks like a number, convert it into a number. If it does not
313** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000314**
315** If the bTryForInt flag is true, then extra effort is made to give
316** an integer representation. Strings that look like floating point
317** values but which have no fractional component (example: '48.00')
318** will have a MEM_Int representation when bTryForInt is true.
319**
320** If bTryForInt is false, then if the input string contains a decimal
321** point or exponential notation, the result is only MEM_Real, even
322** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000323*/
drhbd9507c2014-08-23 17:21:37 +0000324static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000325 double rValue;
drh975b4c62014-07-26 16:47:23 +0000326 u8 enc = pRec->enc;
drh8a3884e2019-05-29 21:18:27 +0000327 int rc;
drh169f0772019-05-02 21:36:26 +0000328 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
drh8a3884e2019-05-29 21:18:27 +0000329 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
drh9a278222019-06-07 22:26:08 +0000330 if( rc<=0 ) return;
drh8a3884e2019-05-29 21:18:27 +0000331 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
drh975b4c62014-07-26 16:47:23 +0000332 pRec->flags |= MEM_Int;
333 }else{
drh74eaba42014-09-18 17:52:15 +0000334 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000335 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000336 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000337 }
drh06b3bd52018-02-01 01:13:33 +0000338 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
339 ** string representation after computing a numeric equivalent, because the
340 ** string representation might not be the canonical representation for the
341 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
342 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000343}
344
345/*
drh8a512562005-11-14 22:29:05 +0000346** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000347**
drh8a512562005-11-14 22:29:05 +0000348** SQLITE_AFF_INTEGER:
349** SQLITE_AFF_REAL:
350** SQLITE_AFF_NUMERIC:
351** Try to convert pRec to an integer representation or a
352** floating-point representation if an integer representation
353** is not possible. Note that the integer representation is
354** always preferred, even if the affinity is REAL, because
355** an integer representation is more space efficient on disk.
356**
357** SQLITE_AFF_TEXT:
358** Convert pRec to a text representation.
359**
drh05883a32015-06-02 15:32:08 +0000360** SQLITE_AFF_BLOB:
drh96fb16e2019-08-06 14:37:24 +0000361** SQLITE_AFF_NONE:
drh8a512562005-11-14 22:29:05 +0000362** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000363*/
drh17435752007-08-16 04:30:38 +0000364static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000365 Mem *pRec, /* The value to apply affinity to */
366 char affinity, /* The affinity to be applied */
367 u8 enc /* Use this text encoding */
368){
drh7ea31cc2014-09-18 14:36:00 +0000369 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000370 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
371 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000372 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000373 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000374 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000375 }else{
376 sqlite3VdbeIntegerAffinity(pRec);
377 }
drh17c40292004-07-21 02:53:29 +0000378 }
drh7ea31cc2014-09-18 14:36:00 +0000379 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000380 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000381 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000382 ** representation. It would be harmless to repeat the conversion if
383 ** there is already a string rep, but it is pointless to waste those
384 ** CPU cycles. */
385 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
drh169f0772019-05-02 21:36:26 +0000386 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
drh3242c692019-05-04 01:29:13 +0000387 testcase( pRec->flags & MEM_Int );
388 testcase( pRec->flags & MEM_Real );
389 testcase( pRec->flags & MEM_IntReal );
drha3fa1402016-04-29 02:55:05 +0000390 sqlite3VdbeMemStringify(pRec, enc, 1);
391 }
danielk19773d1bfea2004-05-14 11:00:53 +0000392 }
drh169f0772019-05-02 21:36:26 +0000393 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
danielk19773d1bfea2004-05-14 11:00:53 +0000394 }
395}
396
danielk1977aee18ef2005-03-09 12:26:50 +0000397/*
drh29d72102006-02-09 22:13:41 +0000398** Try to convert the type of a function argument or a result column
399** into a numeric representation. Use either INTEGER or REAL whichever
400** is appropriate. But only do the conversion if it is possible without
401** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000402*/
403int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000404 int eType = sqlite3_value_type(pVal);
405 if( eType==SQLITE_TEXT ){
406 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000407 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000408 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000409 }
drh1b27b8c2014-02-10 03:21:57 +0000410 return eType;
drh29d72102006-02-09 22:13:41 +0000411}
412
413/*
danielk1977aee18ef2005-03-09 12:26:50 +0000414** Exported version of applyAffinity(). This one works on sqlite3_value*,
415** not the internal Mem* type.
416*/
danielk19771e536952007-08-16 10:09:01 +0000417void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000418 sqlite3_value *pVal,
419 u8 affinity,
420 u8 enc
421){
drhb21c8cd2007-08-21 19:33:56 +0000422 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000423}
424
drh3d1d90a2014-03-24 15:00:15 +0000425/*
drhf1a89ed2014-08-23 17:41:15 +0000426** pMem currently only holds a string type (or maybe a BLOB that we can
427** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000428** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000429** accordingly.
430*/
431static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
drh9a278222019-06-07 22:26:08 +0000432 int rc;
433 sqlite3_int64 ix;
drh169f0772019-05-02 21:36:26 +0000434 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
drhf1a89ed2014-08-23 17:41:15 +0000435 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh1fd1cc42021-04-10 15:34:30 +0000436 if( ExpandBlob(pMem) ){
437 pMem->u.i = 0;
438 return MEM_Int;
439 }
drh9a278222019-06-07 22:26:08 +0000440 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
441 if( rc<=0 ){
442 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
443 pMem->u.i = ix;
444 return MEM_Int;
445 }else{
446 return MEM_Real;
447 }
448 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
449 pMem->u.i = ix;
drhf1a89ed2014-08-23 17:41:15 +0000450 return MEM_Int;
451 }
452 return MEM_Real;
453}
454
455/*
drh3d1d90a2014-03-24 15:00:15 +0000456** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
457** none.
458**
459** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000460** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000461*/
462static u16 numericType(Mem *pMem){
drh169f0772019-05-02 21:36:26 +0000463 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +0000464 testcase( pMem->flags & MEM_Int );
465 testcase( pMem->flags & MEM_Real );
466 testcase( pMem->flags & MEM_IntReal );
drh169f0772019-05-02 21:36:26 +0000467 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
drh3d1d90a2014-03-24 15:00:15 +0000468 }
469 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drh3242c692019-05-04 01:29:13 +0000470 testcase( pMem->flags & MEM_Str );
471 testcase( pMem->flags & MEM_Blob );
drhf1a89ed2014-08-23 17:41:15 +0000472 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000473 }
474 return 0;
475}
476
danielk1977b5402fb2005-01-12 07:15:04 +0000477#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000478/*
danielk1977ca6b2912004-05-21 10:49:47 +0000479** Write a nice string representation of the contents of cell pMem
480** into buffer zBuf, length nBuf.
481*/
drh5ca06322020-01-06 19:23:41 +0000482void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
danielk1977ca6b2912004-05-21 10:49:47 +0000483 int f = pMem->flags;
drh57196282004-10-06 15:41:16 +0000484 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977ca6b2912004-05-21 10:49:47 +0000485 if( f&MEM_Blob ){
486 int i;
487 char c;
488 if( f & MEM_Dyn ){
489 c = 'z';
490 assert( (f & (MEM_Static|MEM_Ephem))==0 );
491 }else if( f & MEM_Static ){
492 c = 't';
493 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
494 }else if( f & MEM_Ephem ){
495 c = 'e';
496 assert( (f & (MEM_Static|MEM_Dyn))==0 );
497 }else{
498 c = 's';
499 }
drhded33cc2020-01-08 11:36:30 +0000500 sqlite3_str_appendf(pStr, "%cx[", c);
drhefb5f9a2019-08-30 21:52:13 +0000501 for(i=0; i<25 && i<pMem->n; i++){
drh5ca06322020-01-06 19:23:41 +0000502 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
danielk1977ca6b2912004-05-21 10:49:47 +0000503 }
drh5ca06322020-01-06 19:23:41 +0000504 sqlite3_str_appendf(pStr, "|");
drhefb5f9a2019-08-30 21:52:13 +0000505 for(i=0; i<25 && i<pMem->n; i++){
danielk1977ca6b2912004-05-21 10:49:47 +0000506 char z = pMem->z[i];
drh5ca06322020-01-06 19:23:41 +0000507 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
danielk1977ca6b2912004-05-21 10:49:47 +0000508 }
drh5ca06322020-01-06 19:23:41 +0000509 sqlite3_str_appendf(pStr,"]");
drhfdf972a2007-05-02 13:30:27 +0000510 if( f & MEM_Zero ){
drh5ca06322020-01-06 19:23:41 +0000511 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
drhfdf972a2007-05-02 13:30:27 +0000512 }
danielk1977b1bc9532004-05-22 03:05:33 +0000513 }else if( f & MEM_Str ){
drh5ca06322020-01-06 19:23:41 +0000514 int j;
mistachkin59171172020-01-18 19:02:20 +0000515 u8 c;
danielk1977b1bc9532004-05-22 03:05:33 +0000516 if( f & MEM_Dyn ){
drh5ca06322020-01-06 19:23:41 +0000517 c = 'z';
danielk1977b1bc9532004-05-22 03:05:33 +0000518 assert( (f & (MEM_Static|MEM_Ephem))==0 );
519 }else if( f & MEM_Static ){
drh5ca06322020-01-06 19:23:41 +0000520 c = 't';
danielk1977b1bc9532004-05-22 03:05:33 +0000521 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
522 }else if( f & MEM_Ephem ){
drh5ca06322020-01-06 19:23:41 +0000523 c = 'e';
danielk1977b1bc9532004-05-22 03:05:33 +0000524 assert( (f & (MEM_Static|MEM_Dyn))==0 );
525 }else{
drh5ca06322020-01-06 19:23:41 +0000526 c = 's';
danielk1977b1bc9532004-05-22 03:05:33 +0000527 }
drh5ca06322020-01-06 19:23:41 +0000528 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
drhefb5f9a2019-08-30 21:52:13 +0000529 for(j=0; j<25 && j<pMem->n; j++){
mistachkin59171172020-01-18 19:02:20 +0000530 c = pMem->z[j];
drh5ca06322020-01-06 19:23:41 +0000531 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
danielk1977b1bc9532004-05-22 03:05:33 +0000532 }
drh5ca06322020-01-06 19:23:41 +0000533 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
danielk1977ca6b2912004-05-21 10:49:47 +0000534 }
danielk1977ca6b2912004-05-21 10:49:47 +0000535}
536#endif
537
drh5b6afba2008-01-05 16:29:28 +0000538#ifdef SQLITE_DEBUG
539/*
540** Print the value of a register for tracing purposes:
541*/
drh84e55a82013-11-13 17:58:23 +0000542static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000543 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000544 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000545 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000546 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000547 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000548 printf(" si:%lld", p->u.i);
drh169f0772019-05-02 21:36:26 +0000549 }else if( (p->flags & (MEM_IntReal))!=0 ){
drh83a1daf2019-05-01 18:59:33 +0000550 printf(" ir:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000551 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000552 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000553#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000554 }else if( p->flags & MEM_Real ){
drhd1c472d2019-10-03 14:51:59 +0000555 printf(" r:%.17g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000556#endif
drh9d67afc2018-08-29 20:24:03 +0000557 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000558 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000559 }else{
drh5ca06322020-01-06 19:23:41 +0000560 StrAccum acc;
561 char zBuf[1000];
562 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
563 sqlite3VdbeMemPrettyPrint(p, &acc);
564 printf(" %s", sqlite3StrAccumFinish(&acc));
drh5b6afba2008-01-05 16:29:28 +0000565 }
dan5b6c8e42016-01-30 15:46:03 +0000566 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000567}
drh84e55a82013-11-13 17:58:23 +0000568static void registerTrace(int iReg, Mem *p){
drh22e95fb2020-01-02 14:42:42 +0000569 printf("R[%d] = ", iReg);
drh84e55a82013-11-13 17:58:23 +0000570 memTracePrint(p);
drh22e95fb2020-01-02 14:42:42 +0000571 if( p->pScopyFrom ){
572 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
573 }
drh84e55a82013-11-13 17:58:23 +0000574 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000575 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000576}
577#endif
578
579#ifdef SQLITE_DEBUG
drh22e95fb2020-01-02 14:42:42 +0000580/*
581** Show the values of all registers in the virtual machine. Used for
582** interactive debugging.
583*/
584void sqlite3VdbeRegisterDump(Vdbe *v){
585 int i;
586 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
587}
588#endif /* SQLITE_DEBUG */
589
590
591#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000592# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000593#else
594# define REGISTER_TRACE(R,M)
595#endif
596
danielk197784ac9d02004-05-18 09:58:06 +0000597
drh7b396862003-01-01 23:06:20 +0000598#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000599
600/*
601** hwtime.h contains inline assembler code for implementing
602** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000603*/
shane9bcbdad2008-05-29 20:22:37 +0000604#include "hwtime.h"
605
drh7b396862003-01-01 23:06:20 +0000606#endif
607
danielk1977fd7f0452008-12-17 17:30:26 +0000608#ifndef NDEBUG
609/*
610** This function is only called from within an assert() expression. It
611** checks that the sqlite3.nTransaction variable is correctly set to
612** the number of non-transaction savepoints currently in the
613** linked list starting at sqlite3.pSavepoint.
614**
615** Usage:
616**
617** assert( checkSavepointCount(db) );
618*/
619static int checkSavepointCount(sqlite3 *db){
620 int n = 0;
621 Savepoint *p;
622 for(p=db->pSavepoint; p; p=p->pNext) n++;
623 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
624 return 1;
625}
626#endif
627
drh27a348c2015-04-13 19:14:06 +0000628/*
629** Return the register of pOp->p2 after first preparing it to be
630** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000631*/
632static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
633 sqlite3VdbeMemSetNull(pOut);
634 pOut->flags = MEM_Int;
635 return pOut;
636}
drh27a348c2015-04-13 19:14:06 +0000637static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
638 Mem *pOut;
639 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000640 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000641 pOut = &p->aMem[pOp->p2];
642 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000643 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000644 return out2PrereleaseWithClear(pOut);
645 }else{
646 pOut->flags = MEM_Int;
647 return pOut;
648 }
drh27a348c2015-04-13 19:14:06 +0000649}
650
drhb9755982010-07-24 16:34:37 +0000651
652/*
drh0fd61352014-02-07 02:29:45 +0000653** Execute as much of a VDBE program as we can.
654** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000655*/
danielk19774adee202004-05-08 08:23:19 +0000656int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000657 Vdbe *p /* The VDBE */
658){
drhbbe879d2009-11-14 18:04:35 +0000659 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000660 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000661#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
662 Op *pOrigOp; /* Value of pOp at the top of the loop */
663#endif
drhb89aeb62016-01-27 15:49:32 +0000664#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000665 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000666#endif
drhb86ccfb2003-01-28 23:13:10 +0000667 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000668 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000669 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000670 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000671 int iCompare = 0; /* Result of last comparison */
drhd1d89142020-07-06 12:13:05 +0000672 u64 nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000673#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhd1d89142020-07-06 12:13:05 +0000674 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000675#endif
drha6c2ed92009-11-14 23:22:23 +0000676 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000677 Mem *pIn1 = 0; /* 1st input operand */
678 Mem *pIn2 = 0; /* 2nd input operand */
679 Mem *pIn3 = 0; /* 3rd input operand */
680 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000681#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000682 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000683#endif
drh856c1032009-06-02 15:21:42 +0000684 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000685
drh17b74812021-02-03 18:32:25 +0000686 assert( p->iVdbeMagic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000687 sqlite3VdbeEnter(p);
drh82642f82019-02-12 22:58:32 +0000688#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
689 if( db->xProgress ){
690 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
691 assert( 0 < db->nProgressOps );
692 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
693 }else{
drhd1d89142020-07-06 12:13:05 +0000694 nProgressLimit = LARGEST_UINT64;
drh82642f82019-02-12 22:58:32 +0000695 }
696#endif
danielk19772e588c72005-12-09 14:25:08 +0000697 if( p->rc==SQLITE_NOMEM ){
698 /* This happens if a malloc() inside a call to sqlite3_column_text() or
699 ** sqlite3_column_text16() failed. */
700 goto no_mem;
701 }
drhcbd8db32015-08-20 17:18:32 +0000702 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drha5f3fb32020-06-03 19:28:10 +0000703 testcase( p->rc!=SQLITE_OK );
704 p->rc = SQLITE_OK;
drh1713afb2013-06-28 01:24:57 +0000705 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000706 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000707 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000708 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000709 db->busyHandler.nBusy = 0;
dan892edb62020-03-30 13:35:05 +0000710 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000711 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000712#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000713 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000714 if( p->pc==0
715 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
716 ){
drh3c23a882007-01-09 14:01:13 +0000717 int i;
drh84e55a82013-11-13 17:58:23 +0000718 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000719 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000720 if( p->db->flags & SQLITE_VdbeListing ){
721 printf("VDBE Program Listing:\n");
722 for(i=0; i<p->nOp; i++){
723 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
724 }
drh3c23a882007-01-09 14:01:13 +0000725 }
drh84e55a82013-11-13 17:58:23 +0000726 if( p->db->flags & SQLITE_VdbeEQP ){
727 for(i=0; i<p->nOp; i++){
728 if( aOp[i].opcode==OP_Explain ){
729 if( once ) printf("VDBE Query Plan:\n");
730 printf("%s\n", aOp[i].p4.z);
731 once = 0;
732 }
733 }
734 }
735 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000736 }
danielk19772d1d86f2008-06-20 14:59:51 +0000737 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000738#endif
drh9467abf2016-02-17 18:44:11 +0000739 for(pOp=&aOp[p->pc]; 1; pOp++){
740 /* Errors are detected by individual opcodes, with an immediate
741 ** jumps to abort_due_to_error. */
742 assert( rc==SQLITE_OK );
743
drhf56fa462015-04-13 21:39:54 +0000744 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000745#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000746 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000747#endif
drhbf159fa2013-06-25 22:01:22 +0000748 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000749#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000750 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000751#endif
drh6e142f52000-06-08 13:36:40 +0000752
danielk19778b60e0f2005-01-12 09:10:39 +0000753 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000754 */
danielk19778b60e0f2005-01-12 09:10:39 +0000755#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000756 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000757 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh22e95fb2020-01-02 14:42:42 +0000758 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
drh75897232000-05-29 14:26:00 +0000759 }
drh3f7d4e42004-07-24 14:35:58 +0000760#endif
761
drh6e142f52000-06-08 13:36:40 +0000762
drhf6038712004-02-08 18:07:34 +0000763 /* Check to see if we need to simulate an interrupt. This only happens
764 ** if we have a special test build.
765 */
766#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000767 if( sqlite3_interrupt_count>0 ){
768 sqlite3_interrupt_count--;
769 if( sqlite3_interrupt_count==0 ){
770 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000771 }
772 }
773#endif
774
drh3c657212009-11-17 23:59:58 +0000775 /* Sanity checking on other operands */
776#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000777 {
778 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
779 if( (opProperty & OPFLG_IN1)!=0 ){
780 assert( pOp->p1>0 );
781 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
782 assert( memIsValid(&aMem[pOp->p1]) );
783 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
784 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
785 }
786 if( (opProperty & OPFLG_IN2)!=0 ){
787 assert( pOp->p2>0 );
788 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
789 assert( memIsValid(&aMem[pOp->p2]) );
790 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
791 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
792 }
793 if( (opProperty & OPFLG_IN3)!=0 ){
794 assert( pOp->p3>0 );
795 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
796 assert( memIsValid(&aMem[pOp->p3]) );
797 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
798 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
799 }
800 if( (opProperty & OPFLG_OUT2)!=0 ){
801 assert( pOp->p2>0 );
802 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
803 memAboutToChange(p, &aMem[pOp->p2]);
804 }
805 if( (opProperty & OPFLG_OUT3)!=0 ){
806 assert( pOp->p3>0 );
807 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
808 memAboutToChange(p, &aMem[pOp->p3]);
809 }
drh3c657212009-11-17 23:59:58 +0000810 }
811#endif
drh6dc41482015-04-16 17:31:02 +0000812#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
813 pOrigOp = pOp;
814#endif
drh93952eb2009-11-13 19:43:43 +0000815
drh75897232000-05-29 14:26:00 +0000816 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000817
drh5e00f6c2001-09-13 13:46:56 +0000818/*****************************************************************************
819** What follows is a massive switch statement where each case implements a
820** separate instruction in the virtual machine. If we follow the usual
821** indentation conventions, each case should be indented by 6 spaces. But
822** that is a lot of wasted space on the left margin. So the code within
823** the switch statement will break with convention and be flush-left. Another
824** big comment (similar to this one) will mark the point in the code where
825** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000826**
827** The formatting of each case is important. The makefile for SQLite
828** generates two C files "opcodes.h" and "opcodes.c" by scanning this
829** file looking for lines that begin with "case OP_". The opcodes.h files
830** will be filled with #defines that give unique integer values to each
831** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000832** each string is the symbolic name for the corresponding opcode. If the
833** case statement is followed by a comment of the form "/# same as ... #/"
834** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000835**
drh9cbf3422008-01-17 16:22:13 +0000836** Other keywords in the comment that follows each case are used to
837** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000838** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000839** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000840**
drhac82fcf2002-09-08 17:23:41 +0000841** Documentation about VDBE opcodes is generated by scanning this file
842** for lines of that contain "Opcode:". That line and all subsequent
843** comment lines are used in the generation of the opcode.html documentation
844** file.
845**
846** SUMMARY:
847**
848** Formatting is important to scripts that scan this file.
849** Do not deviate from the formatting style currently in use.
850**
drh5e00f6c2001-09-13 13:46:56 +0000851*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000852
drh9cbf3422008-01-17 16:22:13 +0000853/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000854**
855** An unconditional jump to address P2.
856** The next instruction executed will be
857** the one at index P2 from the beginning of
858** the program.
drhfe705102014-03-06 13:38:37 +0000859**
860** The P1 parameter is not actually used by this opcode. However, it
861** is sometimes set to 1 instead of 0 as a hint to the command-line shell
862** that this Goto is the bottom of a loop and that the lines from P2 down
863** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000864*/
drh9cbf3422008-01-17 16:22:13 +0000865case OP_Goto: { /* jump */
drhd9670ab2019-12-28 01:52:46 +0000866
867#ifdef SQLITE_DEBUG
868 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
869 ** means we should really jump back to the preceeding OP_ReleaseReg
870 ** instruction. */
871 if( pOp->p5 ){
872 assert( pOp->p2 < (int)(pOp - aOp) );
873 assert( pOp->p2 > 1 );
874 pOp = &aOp[pOp->p2 - 2];
875 assert( pOp[1].opcode==OP_ReleaseReg );
876 goto check_for_interrupt;
877 }
878#endif
879
drhf56fa462015-04-13 21:39:54 +0000880jump_to_p2_and_check_for_interrupt:
881 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000882
883 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000884 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000885 ** completion. Check to see if sqlite3_interrupt() has been called
886 ** or if the progress callback needs to be invoked.
887 **
888 ** This code uses unstructured "goto" statements and does not look clean.
889 ** But that is not due to sloppy coding habits. The code is written this
890 ** way for performance, to avoid having to run the interrupt and progress
891 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
892 ** faster according to "valgrind --tool=cachegrind" */
893check_for_interrupt:
dan892edb62020-03-30 13:35:05 +0000894 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000895#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
896 /* Call the progress callback if it is configured and the required number
897 ** of VDBE ops have been executed (either since this invocation of
898 ** sqlite3VdbeExec() or since last time the progress callback was called).
899 ** If the progress callback returns non-zero, exit the virtual machine with
900 ** a return code SQLITE_ABORT.
901 */
drhb1af9c62019-02-20 13:55:45 +0000902 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000903 assert( db->nProgressOps!=0 );
drhb1af9c62019-02-20 13:55:45 +0000904 nProgressLimit += db->nProgressOps;
drh400fcba2013-11-14 00:09:48 +0000905 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +0000906 nProgressLimit = LARGEST_UINT64;
drh49afe3a2013-07-10 03:05:14 +0000907 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000908 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000909 }
drh49afe3a2013-07-10 03:05:14 +0000910 }
911#endif
912
drh5e00f6c2001-09-13 13:46:56 +0000913 break;
914}
drh75897232000-05-29 14:26:00 +0000915
drh2eb95372008-06-06 15:04:36 +0000916/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000917**
drh2eb95372008-06-06 15:04:36 +0000918** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000919** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000920*/
drhb8475df2011-12-09 16:21:19 +0000921case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000922 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000923 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000924 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000925 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000926 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000927 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000928 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000929
930 /* Most jump operations do a goto to this spot in order to update
931 ** the pOp pointer. */
932jump_to_p2:
933 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000934 break;
935}
936
drh2eb95372008-06-06 15:04:36 +0000937/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000938**
drh81cf13e2014-02-07 18:27:53 +0000939** Jump to the next instruction after the address in register P1. After
940** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000941*/
drh2eb95372008-06-06 15:04:36 +0000942case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000943 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000944 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000945 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000946 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000947 break;
948}
949
drhed71a832014-02-07 19:18:10 +0000950/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000951**
drh5dad9a32014-07-25 18:37:42 +0000952** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000953** located at address P3.
954**
drh5dad9a32014-07-25 18:37:42 +0000955** If P2!=0 then the coroutine implementation immediately follows
956** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000957** address P2.
drh5dad9a32014-07-25 18:37:42 +0000958**
959** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000960*/
961case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000962 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000963 assert( pOp->p2>=0 && pOp->p2<p->nOp );
964 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000965 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000966 assert( !VdbeMemDynamic(pOut) );
967 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000968 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000969 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000970 break;
971}
972
973/* Opcode: EndCoroutine P1 * * * *
974**
drhbc5cf382014-08-06 01:08:07 +0000975** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000976** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000977** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000978**
979** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000980*/
981case OP_EndCoroutine: { /* in1 */
982 VdbeOp *pCaller;
983 pIn1 = &aMem[pOp->p1];
984 assert( pIn1->flags==MEM_Int );
985 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
986 pCaller = &aOp[pIn1->u.i];
987 assert( pCaller->opcode==OP_Yield );
988 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000989 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000990 pIn1->flags = MEM_Undefined;
991 break;
992}
993
994/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000995**
drh5dad9a32014-07-25 18:37:42 +0000996** Swap the program counter with the value in register P1. This
997** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000998**
drh5dad9a32014-07-25 18:37:42 +0000999** If the coroutine that is launched by this instruction ends with
1000** Yield or Return then continue to the next instruction. But if
1001** the coroutine launched by this instruction ends with
1002** EndCoroutine, then jump to P2 rather than continuing with the
1003** next instruction.
1004**
1005** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +00001006*/
drh81cf13e2014-02-07 18:27:53 +00001007case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +00001008 int pcDest;
drh3c657212009-11-17 23:59:58 +00001009 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +00001010 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +00001011 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +00001012 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +00001013 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +00001014 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +00001015 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +00001016 break;
1017}
1018
drhf9c8ce32013-11-05 13:33:55 +00001019/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00001020** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +00001021**
drhef8662b2011-06-20 21:47:58 +00001022** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +00001023** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1024** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +00001025** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +00001026*/
1027case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +00001028 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +00001029#ifdef SQLITE_DEBUG
1030 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1031#endif
drh5053a792009-02-20 03:02:23 +00001032 if( (pIn3->flags & MEM_Null)==0 ) break;
1033 /* Fall through into OP_Halt */
drh08b92082020-08-10 14:18:00 +00001034 /* no break */ deliberate_fall_through
drh5053a792009-02-20 03:02:23 +00001035}
drhe00ee6e2008-06-20 15:24:01 +00001036
drhf9c8ce32013-11-05 13:33:55 +00001037/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001038**
drh3d4501e2008-12-04 20:40:10 +00001039** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +00001040** automatically.
drhb19a2bc2001-09-16 00:13:26 +00001041**
drh92f02c32004-09-02 14:57:08 +00001042** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1043** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1044** For errors, it can be some other value. If P1!=0 then P2 will determine
1045** whether or not to rollback the current transaction. Do not rollback
1046** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1047** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +00001048** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +00001049**
drh66a51672008-01-03 00:01:23 +00001050** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +00001051**
drhf9c8ce32013-11-05 13:33:55 +00001052** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1053**
1054** 0: (no change)
1055** 1: NOT NULL contraint failed: P4
1056** 2: UNIQUE constraint failed: P4
1057** 3: CHECK constraint failed: P4
1058** 4: FOREIGN KEY constraint failed: P4
1059**
1060** If P5 is not zero and P4 is NULL, then everything after the ":" is
1061** omitted.
1062**
drh9cfcf5d2002-01-29 18:41:24 +00001063** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001064** every program. So a jump past the last instruction of the program
1065** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001066*/
drh9cbf3422008-01-17 16:22:13 +00001067case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001068 VdbeFrame *pFrame;
1069 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001070
drhf56fa462015-04-13 21:39:54 +00001071 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +00001072#ifdef SQLITE_DEBUG
1073 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1074#endif
dan165921a2009-08-28 18:53:45 +00001075 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +00001076 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001077 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001078 p->pFrame = pFrame->pParent;
1079 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001080 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001081 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001082 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001083 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001084 ** currently being halted. If the p2 instruction of this OP_Halt
1085 ** instruction is set to OE_Ignore, then the sub-program is throwing
1086 ** an IGNORE exception. In this case jump to the address specified
1087 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001088 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001089 }
drhbbe879d2009-11-14 18:04:35 +00001090 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001091 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001092 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001093 break;
1094 }
drh92f02c32004-09-02 14:57:08 +00001095 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001096 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001097 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001098 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001099 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001100 if( pOp->p5 ){
1101 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1102 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001103 testcase( pOp->p5==1 );
1104 testcase( pOp->p5==2 );
1105 testcase( pOp->p5==3 );
1106 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001107 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1108 if( pOp->p4.z ){
1109 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1110 }
drhd9b7ec92013-11-06 14:05:21 +00001111 }else{
drh22c17b82015-05-15 04:13:15 +00001112 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001113 }
drh99f5de72016-04-30 02:59:15 +00001114 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001115 }
drh92f02c32004-09-02 14:57:08 +00001116 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001117 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001118 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001119 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001120 }else{
drhd91c1a12013-02-09 13:58:25 +00001121 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001122 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001123 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001124 }
drh900b31e2007-08-28 02:27:51 +00001125 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001126}
drhc61053b2000-06-04 12:58:36 +00001127
drh4c583122008-01-04 22:01:03 +00001128/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001129** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001130**
drh9cbf3422008-01-17 16:22:13 +00001131** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001132*/
drh27a348c2015-04-13 19:14:06 +00001133case OP_Integer: { /* out2 */
1134 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001135 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001136 break;
1137}
1138
drh4c583122008-01-04 22:01:03 +00001139/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001140** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001141**
drh66a51672008-01-03 00:01:23 +00001142** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001143** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001144*/
drh27a348c2015-04-13 19:14:06 +00001145case OP_Int64: { /* out2 */
1146 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001147 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001148 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001149 break;
1150}
drh4f26d6c2004-05-26 23:25:30 +00001151
drh13573c72010-01-12 17:04:07 +00001152#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001153/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001154** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001155**
drh4c583122008-01-04 22:01:03 +00001156** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001157** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001158*/
drh27a348c2015-04-13 19:14:06 +00001159case OP_Real: { /* same as TK_FLOAT, out2 */
1160 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001161 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001162 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001163 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001164 break;
1165}
drh13573c72010-01-12 17:04:07 +00001166#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001167
drh3c84ddf2008-01-09 02:15:38 +00001168/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001169** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001170**
drh66a51672008-01-03 00:01:23 +00001171** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001172** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001173** this transformation, the length of string P4 is computed and stored
1174** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001175*/
drh27a348c2015-04-13 19:14:06 +00001176case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001177 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001178 pOut = out2Prerelease(p, pOp);
drhea678832008-12-10 19:26:22 +00001179 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001180
1181#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001182 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001183 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001184 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001185 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001186 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001187 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001188 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001189 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001190 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001191 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001192 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001193 }
drh66a51672008-01-03 00:01:23 +00001194 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001195 pOp->p4.z = pOut->z;
1196 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001197 }
danielk197793758c82005-01-21 08:13:14 +00001198#endif
drhbb4957f2008-03-20 14:03:29 +00001199 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001200 goto too_big;
1201 }
drhec722c12019-09-17 21:28:54 +00001202 pOp->opcode = OP_String;
drh2f555112016-04-30 18:10:34 +00001203 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001204 /* Fall through to the next case, OP_String */
drh08b92082020-08-10 14:18:00 +00001205 /* no break */ deliberate_fall_through
danielk1977cbb18d22004-05-28 11:37:27 +00001206}
drhf4479502004-05-27 03:12:53 +00001207
drhf07cf6e2015-03-06 16:45:16 +00001208/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001209** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001210**
drh9cbf3422008-01-17 16:22:13 +00001211** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001212**
drh44aebff2016-05-02 10:25:42 +00001213** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001214** the datatype of the register P2 is converted to BLOB. The content is
1215** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001216** of a string, as if it had been CAST. In other words:
1217**
1218** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001219*/
drh27a348c2015-04-13 19:14:06 +00001220case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001221 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001222 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001223 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1224 pOut->z = pOp->p4.z;
1225 pOut->n = pOp->p1;
1226 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001227 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001228#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001229 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001230 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001231 pIn3 = &aMem[pOp->p3];
1232 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001233 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001234 }
drh41d2e662015-12-01 21:23:07 +00001235#endif
danielk1977c572ef72004-05-27 09:28:41 +00001236 break;
1237}
1238
drh053a1282012-09-19 21:15:46 +00001239/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001240** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001241**
drhb8475df2011-12-09 16:21:19 +00001242** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001243** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001244** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001245** set to NULL.
1246**
1247** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1248** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1249** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001250*/
drh27a348c2015-04-13 19:14:06 +00001251case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001252 int cnt;
drh053a1282012-09-19 21:15:46 +00001253 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001254 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001255 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001256 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001257 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001258 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001259#ifdef SQLITE_DEBUG
1260 pOut->uTemp = 0;
1261#endif
drhb8475df2011-12-09 16:21:19 +00001262 while( cnt>0 ){
1263 pOut++;
1264 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001265 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001266 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001267 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001268 cnt--;
1269 }
drhf0863fe2005-06-12 21:35:51 +00001270 break;
1271}
1272
drh05a86c52014-02-16 01:55:49 +00001273/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001274** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001275**
1276** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1277** instruction, but do not free any string or blob memory associated with
1278** the register, so that if the value was a string or blob that was
1279** previously copied using OP_SCopy, the copies will continue to be valid.
1280*/
1281case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001282 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001283 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001284 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001285 break;
1286}
drhf0863fe2005-06-12 21:35:51 +00001287
drha5750cf2014-02-07 13:20:31 +00001288/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001289** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001290**
drh9de221d2008-01-05 06:51:30 +00001291** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001292** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001293*/
drh27a348c2015-04-13 19:14:06 +00001294case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001295 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001296 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001297 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001298 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001299 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001300 break;
1301}
1302
drheaf52d82010-05-12 13:50:23 +00001303/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001304** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001305**
drheaf52d82010-05-12 13:50:23 +00001306** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001307**
drh0fd61352014-02-07 02:29:45 +00001308** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001309** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001310*/
drh27a348c2015-04-13 19:14:06 +00001311case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001312 Mem *pVar; /* Value being transferred */
1313
drheaf52d82010-05-12 13:50:23 +00001314 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001315 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001316 pVar = &p->aVar[pOp->p1 - 1];
1317 if( sqlite3VdbeMemTooBig(pVar) ){
1318 goto too_big;
drh023ae032007-05-08 12:12:16 +00001319 }
drh7441df72017-01-09 19:27:04 +00001320 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001321 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1322 memcpy(pOut, pVar, MEMCELLSIZE);
1323 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1324 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001325 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001326 break;
1327}
danielk1977295ba552004-05-19 10:34:51 +00001328
drhb21e7c72008-06-22 12:37:57 +00001329/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001330** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001331**
drh079a3072014-03-19 14:10:55 +00001332** Move the P3 values in register P1..P1+P3-1 over into
1333** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001334** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001335** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1336** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001337*/
drhe1349cb2008-04-01 00:36:10 +00001338case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001339 int n; /* Number of registers left to copy */
1340 int p1; /* Register to copy from */
1341 int p2; /* Register to copy to */
1342
drhe09f43f2013-11-21 04:18:31 +00001343 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001344 p1 = pOp->p1;
1345 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001346 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001347 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001348
drha6c2ed92009-11-14 23:22:23 +00001349 pIn1 = &aMem[p1];
1350 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001351 do{
drh9f6168b2016-03-19 23:32:58 +00001352 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1353 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001354 assert( memIsValid(pIn1) );
1355 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001356 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001357#ifdef SQLITE_DEBUG
drh4cbd8472020-01-02 15:02:08 +00001358 pIn1->pScopyFrom = 0;
1359 { int i;
1360 for(i=1; i<p->nMem; i++){
1361 if( aMem[i].pScopyFrom==pIn1 ){
1362 aMem[i].pScopyFrom = pOut;
1363 }
1364 }
drh52043d72011-08-03 16:40:15 +00001365 }
1366#endif
drhbd6789e2015-04-28 14:00:02 +00001367 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001368 REGISTER_TRACE(p2++, pOut);
1369 pIn1++;
1370 pOut++;
drh079a3072014-03-19 14:10:55 +00001371 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001372 break;
1373}
1374
drhe8e4af72012-09-21 00:04:28 +00001375/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001376** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001377**
drhe8e4af72012-09-21 00:04:28 +00001378** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001379**
1380** This instruction makes a deep copy of the value. A duplicate
1381** is made of any string or blob constant. See also OP_SCopy.
1382*/
drhe8e4af72012-09-21 00:04:28 +00001383case OP_Copy: {
1384 int n;
1385
1386 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001387 pIn1 = &aMem[pOp->p1];
1388 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001389 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001390 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001391 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001392 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1393 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001394#ifdef SQLITE_DEBUG
1395 pOut->pScopyFrom = 0;
1396#endif
drhe8e4af72012-09-21 00:04:28 +00001397 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1398 if( (n--)==0 ) break;
1399 pOut++;
1400 pIn1++;
1401 }
drhe1349cb2008-04-01 00:36:10 +00001402 break;
1403}
1404
drhb1fdb2a2008-01-05 04:06:03 +00001405/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001406** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001407**
drh9cbf3422008-01-17 16:22:13 +00001408** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001409**
1410** This instruction makes a shallow copy of the value. If the value
1411** is a string or blob, then the copy is only a pointer to the
1412** original and hence if the original changes so will the copy.
1413** Worse, if the original is deallocated, the copy becomes invalid.
1414** Thus the program must guarantee that the original will not change
1415** during the lifetime of the copy. Use OP_Copy to make a complete
1416** copy.
1417*/
drh26198bb2013-10-31 11:15:09 +00001418case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001419 pIn1 = &aMem[pOp->p1];
1420 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001421 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001422 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001423#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001424 pOut->pScopyFrom = pIn1;
1425 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001426#endif
drh5e00f6c2001-09-13 13:46:56 +00001427 break;
1428}
drh75897232000-05-29 14:26:00 +00001429
drhfed7ac62015-10-15 18:04:59 +00001430/* Opcode: IntCopy P1 P2 * * *
1431** Synopsis: r[P2]=r[P1]
1432**
1433** Transfer the integer value held in register P1 into register P2.
1434**
1435** This is an optimized version of SCopy that works only for integer
1436** values.
1437*/
1438case OP_IntCopy: { /* out2 */
1439 pIn1 = &aMem[pOp->p1];
1440 assert( (pIn1->flags & MEM_Int)!=0 );
1441 pOut = &aMem[pOp->p2];
1442 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1443 break;
1444}
1445
drh18e56072021-01-31 15:50:36 +00001446/* Opcode: ChngCntRow P1 P2 * * *
1447** Synopsis: output=r[P1]
1448**
1449** Output value in register P1 as the chance count for a DML statement,
1450** due to the "PRAGMA count_changes=ON" setting. Or, if there was a
1451** foreign key error in the statement, trigger the error now.
1452**
1453** This opcode is a variant of OP_ResultRow that checks the foreign key
1454** immediate constraint count and throws an error if the count is
1455** non-zero. The P2 opcode must be 1.
1456*/
1457case OP_ChngCntRow: {
1458 assert( pOp->p2==1 );
1459 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){
1460 goto abort_due_to_error;
1461 }
drh0db20c42021-03-09 20:58:21 +00001462 /* Fall through to the next case, OP_ResultRow */
drh18e56072021-01-31 15:50:36 +00001463 /* no break */ deliberate_fall_through
1464}
1465
drh9cbf3422008-01-17 16:22:13 +00001466/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001467** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001468**
shane21e7feb2008-05-30 15:59:49 +00001469** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001470** results. This opcode causes the sqlite3_step() call to terminate
1471** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001472** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001473** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001474*/
drh9cbf3422008-01-17 16:22:13 +00001475case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001476 Mem *pMem;
1477 int i;
1478 assert( p->nResColumn==pOp->p2 );
drh9ce612a2021-04-05 22:30:56 +00001479 assert( pOp->p1>0 || CORRUPT_DB );
drh9f6168b2016-03-19 23:32:58 +00001480 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001481
drhd4e70eb2008-01-02 00:34:36 +00001482 /* Invalidate all ephemeral cursor row caches */
1483 p->cacheCtr = (p->cacheCtr + 2)|1;
1484
1485 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001486 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001487 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001488 */
drha6c2ed92009-11-14 23:22:23 +00001489 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001490 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001491 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001492 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001493 assert( (pMem[i].flags & MEM_Ephem)==0
1494 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001495 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001496 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drh02ff7472019-12-31 12:18:24 +00001497#ifdef SQLITE_DEBUG
1498 /* The registers in the result will not be used again when the
1499 ** prepared statement restarts. This is because sqlite3_column()
1500 ** APIs might have caused type conversions of made other changes to
1501 ** the register values. Therefore, we can go ahead and break any
1502 ** OP_SCopy dependencies. */
1503 pMem[i].pScopyFrom = 0;
1504#endif
drhd4e70eb2008-01-02 00:34:36 +00001505 }
drh28039692008-03-17 16:54:01 +00001506 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001507
drh3d2a5292016-07-13 22:55:01 +00001508 if( db->mTrace & SQLITE_TRACE_ROW ){
drh08b92082020-08-10 14:18:00 +00001509 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
drh3d2a5292016-07-13 22:55:01 +00001510 }
1511
drh02ff7472019-12-31 12:18:24 +00001512
drhd4e70eb2008-01-02 00:34:36 +00001513 /* Return SQLITE_ROW
1514 */
drhf56fa462015-04-13 21:39:54 +00001515 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001516 rc = SQLITE_ROW;
1517 goto vdbe_return;
1518}
1519
drh5b6afba2008-01-05 16:29:28 +00001520/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001521** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001522**
drh5b6afba2008-01-05 16:29:28 +00001523** Add the text in register P1 onto the end of the text in
1524** register P2 and store the result in register P3.
1525** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001526**
1527** P3 = P2 || P1
1528**
1529** It is illegal for P1 and P3 to be the same register. Sometimes,
1530** if P3 is the same register as P2, the implementation is able
1531** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001532*/
drh5b6afba2008-01-05 16:29:28 +00001533case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh8a7e11f2019-05-01 15:32:40 +00001534 i64 nByte; /* Total size of the output string or blob */
1535 u16 flags1; /* Initial flags for P1 */
1536 u16 flags2; /* Initial flags for P2 */
danielk19778a6b5412004-05-24 07:04:25 +00001537
drh3c657212009-11-17 23:59:58 +00001538 pIn1 = &aMem[pOp->p1];
1539 pIn2 = &aMem[pOp->p2];
1540 pOut = &aMem[pOp->p3];
drh8a7e11f2019-05-01 15:32:40 +00001541 testcase( pOut==pIn2 );
danielk1977a7a8e142008-02-13 18:25:27 +00001542 assert( pIn1!=pOut );
drh8a7e11f2019-05-01 15:32:40 +00001543 flags1 = pIn1->flags;
1544 testcase( flags1 & MEM_Null );
1545 testcase( pIn2->flags & MEM_Null );
1546 if( (flags1 | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001547 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001548 break;
drh5e00f6c2001-09-13 13:46:56 +00001549 }
drh8a7e11f2019-05-01 15:32:40 +00001550 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
1551 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001552 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001553 }else if( (flags1 & MEM_Zero)!=0 ){
1554 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001555 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001556 }
1557 flags2 = pIn2->flags;
1558 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
1559 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001560 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001561 }else if( (flags2 & MEM_Zero)!=0 ){
1562 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001563 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001564 }
drh5b6afba2008-01-05 16:29:28 +00001565 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001566 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001567 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001568 }
drhdf82afc2019-05-16 01:22:21 +00001569 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001570 goto no_mem;
1571 }
drhc91b2fd2014-03-01 18:13:23 +00001572 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001573 if( pOut!=pIn2 ){
1574 memcpy(pOut->z, pIn2->z, pIn2->n);
drh8a7e11f2019-05-01 15:32:40 +00001575 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
1576 pIn2->flags = flags2;
danielk1977a7a8e142008-02-13 18:25:27 +00001577 }
1578 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh8a7e11f2019-05-01 15:32:40 +00001579 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1580 pIn1->flags = flags1;
drh81316f82013-10-29 20:40:47 +00001581 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001582 pOut->z[nByte+1] = 0;
drhdf82afc2019-05-16 01:22:21 +00001583 pOut->z[nByte+2] = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001584 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001585 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001586 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001587 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001588 break;
1589}
drh75897232000-05-29 14:26:00 +00001590
drh3c84ddf2008-01-09 02:15:38 +00001591/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001592** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001593**
drh60a713c2008-01-21 16:22:45 +00001594** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001595** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001596** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001597*/
drh3c84ddf2008-01-09 02:15:38 +00001598/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001599** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001600**
drh3c84ddf2008-01-09 02:15:38 +00001601**
shane21e7feb2008-05-30 15:59:49 +00001602** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001603** and store the result in register P3.
1604** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001605*/
drh3c84ddf2008-01-09 02:15:38 +00001606/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001607** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001608**
drh60a713c2008-01-21 16:22:45 +00001609** Subtract the value in register P1 from the value in register P2
1610** and store the result in register P3.
1611** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001612*/
drh9cbf3422008-01-17 16:22:13 +00001613/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001614** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001615**
drh60a713c2008-01-21 16:22:45 +00001616** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001617** and store the result in register P3 (P3=P2/P1). If the value in
1618** register P1 is zero, then the result is NULL. If either input is
1619** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001620*/
drh9cbf3422008-01-17 16:22:13 +00001621/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001622** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001623**
drh40864a12013-11-15 18:58:37 +00001624** Compute the remainder after integer register P2 is divided by
1625** register P1 and store the result in register P3.
1626** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001627** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001628*/
drh5b6afba2008-01-05 16:29:28 +00001629case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1630case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1631case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1632case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1633case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh3d1d90a2014-03-24 15:00:15 +00001634 u16 flags; /* Combined MEM_* flags from both inputs */
1635 u16 type1; /* Numeric type of left operand */
1636 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001637 i64 iA; /* Integer value of left operand */
1638 i64 iB; /* Integer value of right operand */
1639 double rA; /* Real value of left operand */
1640 double rB; /* Real value of right operand */
1641
drh3c657212009-11-17 23:59:58 +00001642 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001643 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001644 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001645 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001646 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001647 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001648 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001649 iA = pIn1->u.i;
1650 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001651 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001652 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1653 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1654 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001655 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001656 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001657 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001658 iB /= iA;
drh75897232000-05-29 14:26:00 +00001659 break;
1660 }
drhbf4133c2001-10-13 02:59:08 +00001661 default: {
drh856c1032009-06-02 15:21:42 +00001662 if( iA==0 ) goto arithmetic_result_is_null;
1663 if( iA==-1 ) iA = 1;
1664 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001665 break;
1666 }
drh75897232000-05-29 14:26:00 +00001667 }
drh856c1032009-06-02 15:21:42 +00001668 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001669 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001670 }else if( (flags & MEM_Null)!=0 ){
1671 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001672 }else{
drh158b9cb2011-03-05 20:59:46 +00001673fp_math:
drh856c1032009-06-02 15:21:42 +00001674 rA = sqlite3VdbeRealValue(pIn1);
1675 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001676 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001677 case OP_Add: rB += rA; break;
1678 case OP_Subtract: rB -= rA; break;
1679 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001680 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001681 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001682 if( rA==(double)0 ) goto arithmetic_result_is_null;
1683 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001684 break;
1685 }
drhbf4133c2001-10-13 02:59:08 +00001686 default: {
drhe3b89d22019-01-18 17:53:50 +00001687 iA = sqlite3VdbeIntValue(pIn1);
1688 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001689 if( iA==0 ) goto arithmetic_result_is_null;
1690 if( iA==-1 ) iA = 1;
1691 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001692 break;
1693 }
drh5e00f6c2001-09-13 13:46:56 +00001694 }
drhc5a7b512010-01-13 16:25:42 +00001695#ifdef SQLITE_OMIT_FLOATING_POINT
1696 pOut->u.i = rB;
1697 MemSetTypeFlag(pOut, MEM_Int);
1698#else
drh856c1032009-06-02 15:21:42 +00001699 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001700 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001701 }
drh74eaba42014-09-18 17:52:15 +00001702 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001703 MemSetTypeFlag(pOut, MEM_Real);
drhc5a7b512010-01-13 16:25:42 +00001704#endif
drh5e00f6c2001-09-13 13:46:56 +00001705 }
1706 break;
1707
drha05a7222008-01-19 03:35:58 +00001708arithmetic_result_is_null:
1709 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001710 break;
1711}
1712
drh7a957892012-02-02 17:35:43 +00001713/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001714**
drhbb6783b2017-04-29 18:02:49 +00001715** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001716** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1717** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001718** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001719**
drh7a957892012-02-02 17:35:43 +00001720** If P1 is not zero, then it is a register that a subsequent min() or
1721** max() aggregate will set to 1 if the current row is not the minimum or
1722** maximum. The P1 register is initialized to 0 by this instruction.
1723**
danielk1977dc1bdc42004-06-11 10:51:27 +00001724** The interface used by the implementation of the aforementioned functions
1725** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001726** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001727*/
drh9cbf3422008-01-17 16:22:13 +00001728case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001729 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001730 if( pOp->p1 ){
1731 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1732 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001733 break;
1734}
1735
drh98757152008-01-09 23:04:12 +00001736/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001737** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001738**
drh98757152008-01-09 23:04:12 +00001739** Take the bit-wise AND of the values in register P1 and P2 and
1740** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001741** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001742*/
drh98757152008-01-09 23:04:12 +00001743/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001744** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001745**
drh98757152008-01-09 23:04:12 +00001746** Take the bit-wise OR of the values in register P1 and P2 and
1747** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001748** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001749*/
drh98757152008-01-09 23:04:12 +00001750/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001751** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001752**
drh98757152008-01-09 23:04:12 +00001753** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001754** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001755** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001756** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001757*/
drh98757152008-01-09 23:04:12 +00001758/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001759** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001760**
drh98757152008-01-09 23:04:12 +00001761** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001762** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001763** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001764** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001765*/
drh5b6afba2008-01-05 16:29:28 +00001766case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1767case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1768case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1769case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001770 i64 iA;
1771 u64 uA;
1772 i64 iB;
1773 u8 op;
drh6810ce62004-01-31 19:22:56 +00001774
drh3c657212009-11-17 23:59:58 +00001775 pIn1 = &aMem[pOp->p1];
1776 pIn2 = &aMem[pOp->p2];
1777 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001778 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001779 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001780 break;
1781 }
drh158b9cb2011-03-05 20:59:46 +00001782 iA = sqlite3VdbeIntValue(pIn2);
1783 iB = sqlite3VdbeIntValue(pIn1);
1784 op = pOp->opcode;
1785 if( op==OP_BitAnd ){
1786 iA &= iB;
1787 }else if( op==OP_BitOr ){
1788 iA |= iB;
1789 }else if( iB!=0 ){
1790 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1791
1792 /* If shifting by a negative amount, shift in the other direction */
1793 if( iB<0 ){
1794 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1795 op = 2*OP_ShiftLeft + 1 - op;
1796 iB = iB>(-64) ? -iB : 64;
1797 }
1798
1799 if( iB>=64 ){
1800 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1801 }else{
1802 memcpy(&uA, &iA, sizeof(uA));
1803 if( op==OP_ShiftLeft ){
1804 uA <<= iB;
1805 }else{
1806 uA >>= iB;
1807 /* Sign-extend on a right shift of a negative number */
1808 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1809 }
1810 memcpy(&iA, &uA, sizeof(iA));
1811 }
drhbf4133c2001-10-13 02:59:08 +00001812 }
drh158b9cb2011-03-05 20:59:46 +00001813 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001814 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001815 break;
1816}
1817
drh8558cde2008-01-05 05:20:10 +00001818/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001819** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001820**
danielk19770cdc0222008-06-26 18:04:03 +00001821** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001822** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001823**
drh8558cde2008-01-05 05:20:10 +00001824** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001825*/
drh9cbf3422008-01-17 16:22:13 +00001826case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001827 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001828 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001829 sqlite3VdbeMemIntegerify(pIn1);
1830 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001831 break;
1832}
1833
dane5166e02019-03-19 11:56:39 +00001834/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001835**
dane5166e02019-03-19 11:56:39 +00001836** Force the value in register P1 to be an integer. If the value
1837** in P1 is not an integer and cannot be converted into an integer
1838** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001839** raise an SQLITE_MISMATCH exception.
1840*/
drh9cbf3422008-01-17 16:22:13 +00001841case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001842 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001843 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001844 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001845 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001846 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001847 if( pOp->p2==0 ){
1848 rc = SQLITE_MISMATCH;
1849 goto abort_due_to_error;
1850 }else{
drhf56fa462015-04-13 21:39:54 +00001851 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001852 }
drh8aff1012001-12-22 14:49:24 +00001853 }
drh8aff1012001-12-22 14:49:24 +00001854 }
drhc9065332019-04-01 14:01:21 +00001855 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001856 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001857 break;
1858}
1859
drh13573c72010-01-12 17:04:07 +00001860#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001861/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001862**
drh2133d822008-01-03 18:44:59 +00001863** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001864**
drh8a512562005-11-14 22:29:05 +00001865** This opcode is used when extracting information from a column that
1866** has REAL affinity. Such column values may still be stored as
1867** integers, for space efficiency, but after extraction we want them
1868** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001869*/
drh9cbf3422008-01-17 16:22:13 +00001870case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001871 pIn1 = &aMem[pOp->p1];
drh169f0772019-05-02 21:36:26 +00001872 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +00001873 testcase( pIn1->flags & MEM_Int );
1874 testcase( pIn1->flags & MEM_IntReal );
drh8558cde2008-01-05 05:20:10 +00001875 sqlite3VdbeMemRealify(pIn1);
drhefb5f9a2019-08-30 21:52:13 +00001876 REGISTER_TRACE(pOp->p1, pIn1);
drh8a512562005-11-14 22:29:05 +00001877 }
drh487e2622005-06-25 18:42:14 +00001878 break;
1879}
drh13573c72010-01-12 17:04:07 +00001880#endif
drh487e2622005-06-25 18:42:14 +00001881
drh8df447f2005-11-01 15:48:24 +00001882#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001883/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001884** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001885**
drh4169e432014-08-25 20:11:52 +00001886** Force the value in register P1 to be the type defined by P2.
1887**
1888** <ul>
drhbb6783b2017-04-29 18:02:49 +00001889** <li> P2=='A' &rarr; BLOB
1890** <li> P2=='B' &rarr; TEXT
1891** <li> P2=='C' &rarr; NUMERIC
1892** <li> P2=='D' &rarr; INTEGER
1893** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001894** </ul>
drh487e2622005-06-25 18:42:14 +00001895**
1896** A NULL value is not changed by this routine. It remains NULL.
1897*/
drh4169e432014-08-25 20:11:52 +00001898case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001899 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001900 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001901 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001902 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1903 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1904 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001905 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001906 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001907 rc = ExpandBlob(pIn1);
drh9467abf2016-02-17 18:44:11 +00001908 if( rc ) goto abort_due_to_error;
drh0af6ddd2019-12-23 03:37:46 +00001909 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
1910 if( rc ) goto abort_due_to_error;
1911 UPDATE_MAX_BLOBSIZE(pIn1);
drh5d732722019-12-20 17:25:10 +00001912 REGISTER_TRACE(pOp->p1, pIn1);
drh487e2622005-06-25 18:42:14 +00001913 break;
1914}
drh8a512562005-11-14 22:29:05 +00001915#endif /* SQLITE_OMIT_CAST */
1916
drh79752b62016-08-13 10:02:17 +00001917/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001918** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001919**
1920** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
drh4bc20452021-03-29 18:53:47 +00001921** jump to address P2.
drh79752b62016-08-13 10:02:17 +00001922**
1923** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1924** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1925** to coerce both inputs according to this affinity before the
1926** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1927** affinity is used. Note that the affinity conversions are stored
1928** back into the input registers P1 and P3. So this opcode can cause
1929** persistent changes to registers P1 and P3.
1930**
1931** Once any conversions have taken place, and neither value is NULL,
1932** the values are compared. If both values are blobs then memcmp() is
1933** used to determine the results of the comparison. If both values
1934** are text, then the appropriate collating function specified in
1935** P4 is used to do the comparison. If P4 is not specified then
1936** memcmp() is used to compare text string. If both values are
1937** numeric, then a numeric comparison is used. If the two values
1938** are of different types, then numbers are considered less than
1939** strings and strings are considered less than blobs.
1940**
1941** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1942** true or false and is never NULL. If both operands are NULL then the result
1943** of comparison is true. If either operand is NULL then the result is false.
1944** If neither operand is NULL the result is the same as it would be if
1945** the SQLITE_NULLEQ flag were omitted from P5.
1946**
drh1f97d262021-03-29 13:47:20 +00001947** This opcode saves the result of comparison for use by the new
1948** OP_Jump opcode.
drh79752b62016-08-13 10:02:17 +00001949*/
1950/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001951** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001952**
1953** This works just like the Eq opcode except that the jump is taken if
1954** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1955** additional information.
drh79752b62016-08-13 10:02:17 +00001956*/
drh35573352008-01-08 23:54:25 +00001957/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001958** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001959**
drh35573352008-01-08 23:54:25 +00001960** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh4bc20452021-03-29 18:53:47 +00001961** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001962**
drh35573352008-01-08 23:54:25 +00001963** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001964** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001965** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001966**
drh35573352008-01-08 23:54:25 +00001967** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001968** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001969** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001970** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001971** affinity is used. Note that the affinity conversions are stored
1972** back into the input registers P1 and P3. So this opcode can cause
1973** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001974**
1975** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001976** the values are compared. If both values are blobs then memcmp() is
1977** used to determine the results of the comparison. If both values
1978** are text, then the appropriate collating function specified in
1979** P4 is used to do the comparison. If P4 is not specified then
1980** memcmp() is used to compare text string. If both values are
1981** numeric, then a numeric comparison is used. If the two values
1982** are of different types, then numbers are considered less than
1983** strings and strings are considered less than blobs.
drh1f97d262021-03-29 13:47:20 +00001984**
1985** This opcode saves the result of comparison for use by the new
1986** OP_Jump opcode.
drh5e00f6c2001-09-13 13:46:56 +00001987*/
drh9cbf3422008-01-17 16:22:13 +00001988/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001989** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001990**
drh35573352008-01-08 23:54:25 +00001991** This works just like the Lt opcode except that the jump is taken if
1992** the content of register P3 is less than or equal to the content of
1993** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001994*/
drh9cbf3422008-01-17 16:22:13 +00001995/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001996** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001997**
drh35573352008-01-08 23:54:25 +00001998** This works just like the Lt opcode except that the jump is taken if
1999** the content of register P3 is greater than the content of
2000** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002001*/
drh9cbf3422008-01-17 16:22:13 +00002002/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002003** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002004**
drh35573352008-01-08 23:54:25 +00002005** This works just like the Lt opcode except that the jump is taken if
2006** the content of register P3 is greater than or equal to the content of
2007** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002008*/
drh9cbf3422008-01-17 16:22:13 +00002009case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
2010case OP_Ne: /* same as TK_NE, jump, in1, in3 */
2011case OP_Lt: /* same as TK_LT, jump, in1, in3 */
2012case OP_Le: /* same as TK_LE, jump, in1, in3 */
2013case OP_Gt: /* same as TK_GT, jump, in1, in3 */
2014case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00002015 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00002016 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00002017 u16 flags1; /* Copy of initial value of pIn1->flags */
2018 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00002019
drh3c657212009-11-17 23:59:58 +00002020 pIn1 = &aMem[pOp->p1];
2021 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00002022 flags1 = pIn1->flags;
2023 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00002024 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00002025 /* One or both operands are NULL */
2026 if( pOp->p5 & SQLITE_NULLEQ ){
2027 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2028 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2029 ** or not both operands are null.
2030 */
drh053a1282012-09-19 21:15:46 +00002031 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00002032 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
2033 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00002034 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00002035 && (flags3&MEM_Cleared)==0
2036 ){
drh4910a762016-09-03 01:46:15 +00002037 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002038 }else{
danbdabe742019-03-18 16:51:24 +00002039 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002040 }
drh6a2fe092009-09-23 02:29:36 +00002041 }else{
2042 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2043 ** then the result is always NULL.
2044 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2045 */
drh1f97d262021-03-29 13:47:20 +00002046 iCompare = 1; /* Operands are not equal */
drh4bc20452021-03-29 18:53:47 +00002047 VdbeBranchTaken(2,3);
2048 if( pOp->p5 & SQLITE_JUMPIFNULL ){
2049 goto jump_to_p2;
drh6a2fe092009-09-23 02:29:36 +00002050 }
2051 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002052 }
drh6a2fe092009-09-23 02:29:36 +00002053 }else{
2054 /* Neither operand is NULL. Do a comparison. */
2055 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002056 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002057 if( (flags1 | flags3)&MEM_Str ){
drh169f0772019-05-02 21:36:26 +00002058 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002059 applyNumericAffinity(pIn1,0);
drh86d2de22020-06-14 13:40:13 +00002060 testcase( flags3==pIn3->flags );
drh4b37cd42016-06-25 11:43:47 +00002061 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002062 }
drh169f0772019-05-02 21:36:26 +00002063 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002064 applyNumericAffinity(pIn3,0);
2065 }
drh24a09622014-09-18 16:28:59 +00002066 }
drh64caee42016-09-09 19:33:00 +00002067 /* Handle the common case of integer comparison here, as an
2068 ** optimization, to avoid a call to sqlite3MemCompare() */
2069 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2070 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2071 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2072 res = 0;
2073 goto compare_op;
2074 }
drh24a09622014-09-18 16:28:59 +00002075 }else if( affinity==SQLITE_AFF_TEXT ){
drh169f0772019-05-02 21:36:26 +00002076 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002077 testcase( pIn1->flags & MEM_Int );
2078 testcase( pIn1->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002079 testcase( pIn1->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002080 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002081 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2082 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh9dce0ef2020-02-01 21:03:27 +00002083 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
drh24a09622014-09-18 16:28:59 +00002084 }
drhb44fec62019-12-24 21:42:22 +00002085 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002086 testcase( pIn3->flags & MEM_Int );
2087 testcase( pIn3->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002088 testcase( pIn3->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002089 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002090 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2091 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002092 }
drh6a2fe092009-09-23 02:29:36 +00002093 }
drh6a2fe092009-09-23 02:29:36 +00002094 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002095 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002096 }
drh64caee42016-09-09 19:33:00 +00002097compare_op:
drh58596362017-08-03 00:29:23 +00002098 /* At this point, res is negative, zero, or positive if reg[P1] is
2099 ** less than, equal to, or greater than reg[P3], respectively. Compute
2100 ** the answer to this operator in res2, depending on what the comparison
2101 ** operator actually is. The next block of code depends on the fact
2102 ** that the 6 comparison operators are consecutive integers in this
2103 ** order: NE, EQ, GT, LE, LT, GE */
2104 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2105 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
drh1af3fd52021-03-28 23:37:56 +00002106 if( res<0 ){
2107 res2 = sqlite3aLTb[pOp->opcode];
drh58596362017-08-03 00:29:23 +00002108 }else if( res==0 ){
drh1af3fd52021-03-28 23:37:56 +00002109 res2 = sqlite3aEQb[pOp->opcode];
drh58596362017-08-03 00:29:23 +00002110 }else{
drh1af3fd52021-03-28 23:37:56 +00002111 res2 = sqlite3aGTb[pOp->opcode];
danielk1977a37cdde2004-05-16 11:15:36 +00002112 }
drh1f97d262021-03-29 13:47:20 +00002113 iCompare = res;
danielk1977a37cdde2004-05-16 11:15:36 +00002114
drhf56fa462015-04-13 21:39:54 +00002115 /* Undo any changes made by applyAffinity() to the input registers. */
drhf56fa462015-04-13 21:39:54 +00002116 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2117 pIn3->flags = flags3;
drhb44fec62019-12-24 21:42:22 +00002118 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2119 pIn1->flags = flags1;
drhf56fa462015-04-13 21:39:54 +00002120
drh4bc20452021-03-29 18:53:47 +00002121 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2122 if( res2 ){
2123 goto jump_to_p2;
danielk1977a37cdde2004-05-16 11:15:36 +00002124 }
2125 break;
2126}
drhc9b84a12002-06-20 11:36:48 +00002127
drh4bc20452021-03-29 18:53:47 +00002128/* Opcode: ElseEq * P2 * * *
drh79752b62016-08-13 10:02:17 +00002129**
drh13d79502019-12-23 02:18:49 +00002130** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2131** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2132** opcodes are allowed to occur between this instruction and the previous
drh4bc20452021-03-29 18:53:47 +00002133** OP_Lt or OP_Gt.
drh13d79502019-12-23 02:18:49 +00002134**
2135** If result of an OP_Eq comparison on the same two operands as the
drh4bc20452021-03-29 18:53:47 +00002136** prior OP_Lt or OP_Gt would have been true, then jump to P2.
2137** If the result of an OP_Eq comparison on the two previous
2138** operands would have been false or NULL, then fall through.
drh79752b62016-08-13 10:02:17 +00002139*/
drh4bc20452021-03-29 18:53:47 +00002140case OP_ElseEq: { /* same as TK_ESCAPE, jump */
drh13d79502019-12-23 02:18:49 +00002141
2142#ifdef SQLITE_DEBUG
2143 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
drh4bc20452021-03-29 18:53:47 +00002144 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
drh13d79502019-12-23 02:18:49 +00002145 int iAddr;
2146 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
2147 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
2148 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
drh13d79502019-12-23 02:18:49 +00002149 break;
2150 }
2151#endif /* SQLITE_DEBUG */
drh4bc20452021-03-29 18:53:47 +00002152 VdbeBranchTaken(iCompare==0, 2);
2153 if( iCompare==0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002154 break;
2155}
2156
2157
drh0acb7e42008-06-25 00:12:41 +00002158/* Opcode: Permutation * * * P4 *
2159**
drhb7dab702017-01-26 18:00:00 +00002160** Set the permutation used by the OP_Compare operator in the next
2161** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002162**
drh953f7612012-12-07 22:18:54 +00002163** The permutation is only valid until the next OP_Compare that has
2164** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2165** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002166**
2167** The first integer in the P4 integer array is the length of the array
2168** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002169*/
2170case OP_Permutation: {
2171 assert( pOp->p4type==P4_INTARRAY );
2172 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002173 assert( pOp[1].opcode==OP_Compare );
2174 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002175 break;
2176}
2177
drh953f7612012-12-07 22:18:54 +00002178/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002179** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002180**
drh710c4842010-08-30 01:17:20 +00002181** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2182** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002183** the comparison for use by the next OP_Jump instruct.
2184**
drh0ca10df2012-12-08 13:26:23 +00002185** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2186** determined by the most recent OP_Permutation operator. If the
2187** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2188** order.
2189**
drh0acb7e42008-06-25 00:12:41 +00002190** P4 is a KeyInfo structure that defines collating sequences and sort
2191** orders for the comparison. The permutation applies to registers
2192** only. The KeyInfo elements are used sequentially.
2193**
2194** The comparison is a sort comparison, so NULLs compare equal,
2195** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002196** and strings are less than blobs.
2197*/
2198case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002199 int n;
2200 int i;
2201 int p1;
2202 int p2;
2203 const KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00002204 u32 idx;
drh856c1032009-06-02 15:21:42 +00002205 CollSeq *pColl; /* Collating sequence to use on this term */
2206 int bRev; /* True for DESCENDING sort order */
drhabc38152020-07-22 13:38:04 +00002207 u32 *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002208
drhb7dab702017-01-26 18:00:00 +00002209 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2210 aPermute = 0;
2211 }else{
2212 assert( pOp>aOp );
2213 assert( pOp[-1].opcode==OP_Permutation );
2214 assert( pOp[-1].p4type==P4_INTARRAY );
2215 aPermute = pOp[-1].p4.ai + 1;
2216 assert( aPermute!=0 );
2217 }
drh856c1032009-06-02 15:21:42 +00002218 n = pOp->p3;
2219 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002220 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002221 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002222 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002223 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002224#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002225 if( aPermute ){
2226 int k, mx = 0;
mistachkincec5f1d2020-08-04 16:11:37 +00002227 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002228 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2229 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002230 }else{
drh9f6168b2016-03-19 23:32:58 +00002231 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2232 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002233 }
2234#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002235 for(i=0; i<n; i++){
drh8deae5a2020-07-29 12:23:20 +00002236 idx = aPermute ? aPermute[i] : (u32)i;
drh2b4ded92010-09-27 21:09:31 +00002237 assert( memIsValid(&aMem[p1+idx]) );
2238 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002239 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2240 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002241 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002242 pColl = pKeyInfo->aColl[i];
dan6e118922019-08-12 16:36:38 +00002243 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
drha6c2ed92009-11-14 23:22:23 +00002244 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002245 if( iCompare ){
dan6e118922019-08-12 16:36:38 +00002246 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
2247 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
2248 ){
2249 iCompare = -iCompare;
2250 }
drh0acb7e42008-06-25 00:12:41 +00002251 if( bRev ) iCompare = -iCompare;
2252 break;
2253 }
drh16ee60f2008-06-20 18:13:25 +00002254 }
2255 break;
2256}
2257
2258/* Opcode: Jump P1 P2 P3 * *
2259**
2260** Jump to the instruction at address P1, P2, or P3 depending on whether
2261** in the most recent OP_Compare instruction the P1 vector was less than
2262** equal to, or greater than the P2 vector, respectively.
2263*/
drh0acb7e42008-06-25 00:12:41 +00002264case OP_Jump: { /* jump */
2265 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002266 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002267 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002268 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002269 }else{
drh7083a482018-07-10 16:04:04 +00002270 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002271 }
2272 break;
2273}
2274
drh5b6afba2008-01-05 16:29:28 +00002275/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002276** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002277**
drh5b6afba2008-01-05 16:29:28 +00002278** Take the logical AND of the values in registers P1 and P2 and
2279** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002280**
drh5b6afba2008-01-05 16:29:28 +00002281** If either P1 or P2 is 0 (false) then the result is 0 even if
2282** the other input is NULL. A NULL and true or two NULLs give
2283** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002284*/
drh5b6afba2008-01-05 16:29:28 +00002285/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002286** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002287**
2288** Take the logical OR of the values in register P1 and P2 and
2289** store the answer in register P3.
2290**
2291** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2292** even if the other input is NULL. A NULL and false or two NULLs
2293** give a NULL output.
2294*/
2295case OP_And: /* same as TK_AND, in1, in2, out3 */
2296case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002297 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2298 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002299
drh1fcfa722018-02-26 15:27:31 +00002300 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2301 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002302 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002303 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002304 v1 = and_logic[v1*3+v2];
2305 }else{
drh5b6afba2008-01-05 16:29:28 +00002306 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002307 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002308 }
drh3c657212009-11-17 23:59:58 +00002309 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002310 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002311 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002312 }else{
drh5b6afba2008-01-05 16:29:28 +00002313 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002314 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002315 }
drh5e00f6c2001-09-13 13:46:56 +00002316 break;
2317}
2318
drh8abed7b2018-02-26 18:49:05 +00002319/* Opcode: IsTrue P1 P2 P3 P4 *
2320** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2321**
2322** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2323** IS NOT FALSE operators.
2324**
drh96acafb2018-02-27 14:49:25 +00002325** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002326** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2327** NULL, then the P3 is stored in register P2. Invert the answer if P4
2328** is 1.
2329**
2330** The logic is summarized like this:
2331**
2332** <ul>
drh96acafb2018-02-27 14:49:25 +00002333** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2334** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2335** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2336** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002337** </ul>
2338*/
2339case OP_IsTrue: { /* in1, out2 */
2340 assert( pOp->p4type==P4_INT32 );
2341 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002342 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002343 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2344 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2345 break;
2346}
2347
drhe99fa2a2008-12-15 15:27:51 +00002348/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002349** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002350**
drhe99fa2a2008-12-15 15:27:51 +00002351** Interpret the value in register P1 as a boolean value. Store the
2352** boolean complement in register P2. If the value in register P1 is
2353** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002354*/
drh93952eb2009-11-13 19:43:43 +00002355case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002356 pIn1 = &aMem[pOp->p1];
2357 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002358 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002359 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002360 }else{
2361 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002362 }
drh5e00f6c2001-09-13 13:46:56 +00002363 break;
2364}
2365
drhe99fa2a2008-12-15 15:27:51 +00002366/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002367** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002368**
drhe99fa2a2008-12-15 15:27:51 +00002369** Interpret the content of register P1 as an integer. Store the
2370** ones-complement of the P1 value into register P2. If P1 holds
2371** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002372*/
drh93952eb2009-11-13 19:43:43 +00002373case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002374 pIn1 = &aMem[pOp->p1];
2375 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002376 sqlite3VdbeMemSetNull(pOut);
2377 if( (pIn1->flags & MEM_Null)==0 ){
2378 pOut->flags = MEM_Int;
2379 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002380 }
drhbf4133c2001-10-13 02:59:08 +00002381 break;
2382}
2383
drh48f2d3b2011-09-16 01:34:43 +00002384/* Opcode: Once P1 P2 * * *
2385**
drhab087d42017-03-24 17:59:56 +00002386** Fall through to the next instruction the first time this opcode is
2387** encountered on each invocation of the byte-code program. Jump to P2
2388** on the second and all subsequent encounters during the same invocation.
2389**
2390** Top-level programs determine first invocation by comparing the P1
2391** operand against the P1 operand on the OP_Init opcode at the beginning
2392** of the program. If the P1 values differ, then fall through and make
2393** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2394** the same then take the jump.
2395**
2396** For subprograms, there is a bitmask in the VdbeFrame that determines
2397** whether or not the jump should be taken. The bitmask is necessary
2398** because the self-altering code trick does not work for recursive
2399** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002400*/
dan1d8cb212011-12-09 13:24:16 +00002401case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002402 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002403 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002404 if( p->pFrame ){
2405 iAddr = (int)(pOp - p->aOp);
2406 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2407 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002408 goto jump_to_p2;
2409 }
drh18333ef2017-03-24 18:38:41 +00002410 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002411 }else{
drhab087d42017-03-24 17:59:56 +00002412 if( p->aOp[0].p1==pOp->p1 ){
2413 VdbeBranchTaken(1, 2);
2414 goto jump_to_p2;
2415 }
dan1d8cb212011-12-09 13:24:16 +00002416 }
drhab087d42017-03-24 17:59:56 +00002417 VdbeBranchTaken(0, 2);
2418 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002419 break;
2420}
2421
drh3c84ddf2008-01-09 02:15:38 +00002422/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002423**
drhef8662b2011-06-20 21:47:58 +00002424** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002425** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002426** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002427*/
drh1fcfa722018-02-26 15:27:31 +00002428case OP_If: { /* jump, in1 */
2429 int c;
2430 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2431 VdbeBranchTaken(c!=0, 2);
2432 if( c ) goto jump_to_p2;
2433 break;
2434}
2435
drh3c84ddf2008-01-09 02:15:38 +00002436/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002437**
drhef8662b2011-06-20 21:47:58 +00002438** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002439** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002440** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002441*/
drh9cbf3422008-01-17 16:22:13 +00002442case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002443 int c;
drh1fcfa722018-02-26 15:27:31 +00002444 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002445 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002446 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002447 break;
2448}
2449
drh830ecf92009-06-18 00:41:55 +00002450/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002451** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002452**
drh830ecf92009-06-18 00:41:55 +00002453** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002454*/
drh9cbf3422008-01-17 16:22:13 +00002455case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002456 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002457 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002458 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002459 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002460 }
drh477df4b2008-01-05 18:48:24 +00002461 break;
2462}
2463
drh871e7ff2021-03-29 14:40:48 +00002464/* Opcode: ZeroOrNull P1 P2 P3 * *
drh4bc20452021-03-29 18:53:47 +00002465** Synopsis: r[P2] = 0 OR NULL
drh871e7ff2021-03-29 14:40:48 +00002466**
drh4bc20452021-03-29 18:53:47 +00002467** If all both registers P1 and P3 are NOT NULL, then store a zero in
2468** register P2. If either registers P1 or P3 are NULL then put
2469** a NULL in register P2.
drh871e7ff2021-03-29 14:40:48 +00002470*/
drh4bc20452021-03-29 18:53:47 +00002471case OP_ZeroOrNull: { /* in1, in2, out2, in3 */
drh871e7ff2021-03-29 14:40:48 +00002472 if( (aMem[pOp->p1].flags & MEM_Null)!=0
2473 || (aMem[pOp->p3].flags & MEM_Null)!=0
2474 ){
2475 sqlite3VdbeMemSetNull(aMem + pOp->p2);
2476 }else{
2477 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
2478 }
2479 break;
2480}
2481
drh98757152008-01-09 23:04:12 +00002482/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002483** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002484**
drh6a288a32008-01-07 19:20:24 +00002485** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002486*/
drh9cbf3422008-01-17 16:22:13 +00002487case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002488 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002489 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002490 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002491 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002492 }
drh5e00f6c2001-09-13 13:46:56 +00002493 break;
2494}
2495
drh31d6fd52017-04-14 19:03:10 +00002496/* Opcode: IfNullRow P1 P2 P3 * *
2497** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2498**
2499** Check the cursor P1 to see if it is currently pointing at a NULL row.
2500** If it is, then set register P3 to NULL and jump immediately to P2.
2501** If P1 is not on a NULL row, then fall through without making any
2502** changes.
2503*/
2504case OP_IfNullRow: { /* jump */
2505 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002506 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002507 if( p->apCsr[pOp->p1]->nullRow ){
2508 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2509 goto jump_to_p2;
2510 }
2511 break;
2512}
2513
drh092457b2017-12-29 15:04:49 +00002514#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2515/* Opcode: Offset P1 P2 P3 * *
2516** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002517**
drh092457b2017-12-29 15:04:49 +00002518** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002519** start of the payload for the record at which that cursor P1 is currently
2520** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002521**
drh092457b2017-12-29 15:04:49 +00002522** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002523** This opcode does not use P2 itself, but the P2 value is used by the
2524** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002525** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002526**
2527** This opcode is only available if SQLite is compiled with the
2528** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002529*/
drh092457b2017-12-29 15:04:49 +00002530case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002531 VdbeCursor *pC; /* The VDBE cursor */
2532 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2533 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002534 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002535 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002536 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002537 }else{
drh092457b2017-12-29 15:04:49 +00002538 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002539 }
2540 break;
2541}
drh092457b2017-12-29 15:04:49 +00002542#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002543
drh3e9ca092009-09-08 01:14:48 +00002544/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002545** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002546**
danielk1977cfcdaef2004-05-12 07:33:33 +00002547** Interpret the data that cursor P1 points to as a structure built using
2548** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002549** information about the format of the data.) Extract the P2-th column
2550** from this record. If there are less that (P2+1)
2551** values in the record, extract a NULL.
2552**
drh9cbf3422008-01-17 16:22:13 +00002553** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002554**
drh1cc3a362017-04-03 13:17:31 +00002555** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002556** if the P4 argument is a P4_MEM use the value of the P4 argument as
2557** the result.
drh3e9ca092009-09-08 01:14:48 +00002558**
drh1cc3a362017-04-03 13:17:31 +00002559** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002560** the result is guaranteed to only be used as the argument of a length()
2561** or typeof() function, respectively. The loading of large blobs can be
2562** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002563*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002564case OP_Column: {
drhabc38152020-07-22 13:38:04 +00002565 u32 p2; /* column number to retrieve */
drh856c1032009-06-02 15:21:42 +00002566 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002567 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002568 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002569 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002570 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002571 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002572 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002573 const u8 *zData; /* Part of the record being decoded */
2574 const u8 *zHdr; /* Next unparsed byte of the header */
2575 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002576 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002577 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002578 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002579
drh8c7715d2019-12-20 14:37:56 +00002580 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
dande892d92016-01-29 19:29:45 +00002581 pC = p->apCsr[pOp->p1];
drh8c7715d2019-12-20 14:37:56 +00002582 assert( pC!=0 );
drhabc38152020-07-22 13:38:04 +00002583 p2 = (u32)pOp->p2;
dande892d92016-01-29 19:29:45 +00002584
drh170ad682017-06-02 15:44:22 +00002585 /* If the cursor cache is stale (meaning it is not currently point at
2586 ** the correct row) then bring it up-to-date by doing the necessary
2587 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002588 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002589 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002590
drh9f6168b2016-03-19 23:32:58 +00002591 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002592 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002593 memAboutToChange(p, pDest);
danielk19776c924092007-11-12 08:09:34 +00002594 assert( pC!=0 );
mistachkincec5f1d2020-08-04 16:11:37 +00002595 assert( p2<(u32)pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002596 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002597 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002598 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2599 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002600
drha43a02e2016-05-19 17:51:19 +00002601 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002602 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002603 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002604 /* For the special case of as pseudo-cursor, the seekResult field
2605 ** identifies the register that holds the record */
2606 assert( pC->seekResult>0 );
2607 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002608 assert( pReg->flags & MEM_Blob );
2609 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002610 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002611 pC->aRow = (u8*)pReg->z;
2612 }else{
drh6b5631e2014-11-05 15:57:39 +00002613 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002614 goto op_column_out;
2615 }
danielk1977192ac1d2004-05-10 07:17:30 +00002616 }else{
drh06a09a82016-11-25 17:03:03 +00002617 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002618 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002619 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002620 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2621 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002622 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2623 assert( pC->szRow<=pC->payloadSize );
2624 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2625 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002626 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002627 }
danielk1977192ac1d2004-05-10 07:17:30 +00002628 }
drhb73857f2006-03-17 00:25:59 +00002629 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002630 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002631 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002632
drhc81aa2e2014-10-11 23:31:52 +00002633
drh1f613c42017-08-16 14:16:19 +00002634 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002635 /* pC->aRow does not have to hold the entire row, but it does at least
2636 ** need to cover the header of the record. If pC->aRow does not contain
2637 ** the complete header, then set it to zero, forcing the header to be
2638 ** dynamically allocated. */
2639 pC->aRow = 0;
2640 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002641
2642 /* Make sure a corrupt database has not given us an oversize header.
2643 ** Do this now to avoid an oversize memory allocation.
2644 **
2645 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2646 ** types use so much data space that there can only be 4096 and 32 of
2647 ** them, respectively. So the maximum header length results from a
2648 ** 3-byte type for each of the maximum of 32768 columns plus three
2649 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2650 */
drh1f613c42017-08-16 14:16:19 +00002651 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002652 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002653 }
drh95b225a2017-08-16 11:04:22 +00002654 }else{
2655 /* This is an optimization. By skipping over the first few tests
2656 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2657 ** measurable performance gain.
2658 **
drh1f613c42017-08-16 14:16:19 +00002659 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002660 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002661 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002662 ** branch jumps to reads past the end of the record, but never more
2663 ** than a few bytes. Even if the record occurs at the end of the page
2664 ** content area, the "page header" comes after the page content and so
2665 ** this overread is harmless. Similar overreads can occur for a corrupt
2666 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002667 */
2668 zData = pC->aRow;
2669 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002670 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002671 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002672 }
drh399af1d2013-11-20 17:25:55 +00002673 }
drh35cd6432009-06-05 14:17:21 +00002674
drh399af1d2013-11-20 17:25:55 +00002675 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002676 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002677 */
drhc8606e42013-11-20 19:28:03 +00002678 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002679 /* If there is more header available for parsing in the record, try
2680 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002681 */
drhc8606e42013-11-20 19:28:03 +00002682 if( pC->iHdrOffset<aOffset[0] ){
2683 /* Make sure zData points to enough of the record to cover the header. */
2684 if( pC->aRow==0 ){
2685 memset(&sMem, 0, sizeof(sMem));
drh2a740062020-02-05 18:28:17 +00002686 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
drh9467abf2016-02-17 18:44:11 +00002687 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002688 zData = (u8*)sMem.z;
2689 }else{
2690 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002691 }
drhc8606e42013-11-20 19:28:03 +00002692
drh0c8f7602014-09-19 16:56:45 +00002693 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002694 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002695 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002696 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002697 zHdr = zData + pC->iHdrOffset;
2698 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002699 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002700 do{
drhc332e042019-02-12 21:04:33 +00002701 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002702 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002703 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002704 }else{
drhc8606e42013-11-20 19:28:03 +00002705 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002706 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002707 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002708 }
drhc332e042019-02-12 21:04:33 +00002709 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drh8deae5a2020-07-29 12:23:20 +00002710 }while( (u32)i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002711
drh8dd83622014-10-13 23:39:02 +00002712 /* The record is corrupt if any of the following are true:
2713 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002714 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002715 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002716 */
drhc6ce38832015-10-15 21:30:24 +00002717 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2718 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002719 ){
drh95b225a2017-08-16 11:04:22 +00002720 if( aOffset[0]==0 ){
2721 i = 0;
2722 zHdr = zEndHdr;
2723 }else{
2724 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002725 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002726 }
danielk1977dedf45b2006-01-13 17:12:01 +00002727 }
drhddb2b4a2016-03-25 12:10:32 +00002728
drh170c2762016-05-20 21:40:11 +00002729 pC->nHdrParsed = i;
2730 pC->iHdrOffset = (u32)(zHdr - zData);
2731 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002732 }else{
drh9fbc8852016-01-04 03:48:46 +00002733 t = 0;
drh9188b382004-05-14 21:12:22 +00002734 }
drhd3194f52004-05-27 19:59:32 +00002735
drhf2db3382015-04-30 20:33:25 +00002736 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002737 ** still not up to p2, that means that the record has fewer than p2
2738 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002739 */
drhc8606e42013-11-20 19:28:03 +00002740 if( pC->nHdrParsed<=p2 ){
2741 if( pOp->p4type==P4_MEM ){
2742 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2743 }else{
drh22e8d832014-10-29 00:58:38 +00002744 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002745 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002746 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002747 }
drh95fa6062015-10-16 13:50:08 +00002748 }else{
2749 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002750 }
danielk1977192ac1d2004-05-10 07:17:30 +00002751
drh380d6852013-11-20 20:58:00 +00002752 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002753 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002754 ** all valid.
drh9188b382004-05-14 21:12:22 +00002755 */
drhc8606e42013-11-20 19:28:03 +00002756 assert( p2<pC->nHdrParsed );
2757 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002758 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002759 if( VdbeMemDynamic(pDest) ){
2760 sqlite3VdbeMemSetNull(pDest);
2761 }
drh95fa6062015-10-16 13:50:08 +00002762 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002763 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002764 /* This is the common case where the desired content fits on the original
2765 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002766 zData = pC->aRow + aOffset[p2];
2767 if( t<12 ){
2768 sqlite3VdbeSerialGet(zData, t, pDest);
2769 }else{
2770 /* If the column value is a string, we need a persistent value, not
2771 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2772 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2773 */
2774 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2775 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002776 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002777 if( pDest->szMalloc < len+2 ){
2778 pDest->flags = MEM_Null;
2779 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2780 }else{
2781 pDest->z = pDest->zMalloc;
2782 }
2783 memcpy(pDest->z, zData, len);
2784 pDest->z[len] = 0;
2785 pDest->z[len+1] = 0;
2786 pDest->flags = aFlag[t&1];
2787 }
danielk197736963fd2005-02-19 08:18:05 +00002788 }else{
drha1851ef2016-05-20 19:51:28 +00002789 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002790 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002791 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2792 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2793 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002794 ){
drh2a2a6962014-09-16 18:22:44 +00002795 /* Content is irrelevant for
2796 ** 1. the typeof() function,
2797 ** 2. the length(X) function if X is a blob, and
2798 ** 3. if the content length is zero.
2799 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002800 ** content from disk.
2801 **
2802 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2803 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
drhcbae3f82020-01-06 20:48:45 +00002804 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
2805 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
2806 ** and it begins with a bunch of zeros.
dan1f9144e2017-03-17 13:59:06 +00002807 */
drhcbae3f82020-01-06 20:48:45 +00002808 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002809 }else{
drhcb3cabd2016-11-25 19:18:28 +00002810 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002811 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2812 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2813 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002814 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002815 }
drhd3194f52004-05-27 19:59:32 +00002816
danielk19773c9cc8d2005-01-17 03:40:08 +00002817op_column_out:
drhb7654112008-01-12 12:48:07 +00002818 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002819 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002820 break;
drh74588ce2017-09-13 00:13:05 +00002821
2822op_column_corrupt:
2823 if( aOp[0].p3>0 ){
2824 pOp = &aOp[aOp[0].p3-1];
2825 break;
2826 }else{
2827 rc = SQLITE_CORRUPT_BKPT;
2828 goto abort_due_to_error;
2829 }
danielk1977192ac1d2004-05-10 07:17:30 +00002830}
2831
danielk1977751de562008-04-18 09:01:15 +00002832/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002833** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002834**
2835** Apply affinities to a range of P2 registers starting with P1.
2836**
drhbb6783b2017-04-29 18:02:49 +00002837** P4 is a string that is P2 characters long. The N-th character of the
2838** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002839** memory cell in the range.
2840*/
2841case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002842 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002843
drh856c1032009-06-02 15:21:42 +00002844 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002845 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002846 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002847 assert( zAffinity[pOp->p2]==0 );
2848 pIn1 = &aMem[pOp->p1];
drh122c5142019-07-29 05:23:01 +00002849 while( 1 /*exit-by-break*/ ){
drh9f6168b2016-03-19 23:32:58 +00002850 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drhb5f62432019-12-10 02:48:41 +00002851 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
drh83a1daf2019-05-01 18:59:33 +00002852 applyAffinity(pIn1, zAffinity[0], encoding);
2853 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
drh337cc392019-07-29 06:06:53 +00002854 /* When applying REAL affinity, if the result is still an MEM_Int
2855 ** that will fit in 6 bytes, then change the type to MEM_IntReal
2856 ** so that we keep the high-resolution integer value but know that
2857 ** the type really wants to be REAL. */
2858 testcase( pIn1->u.i==140737488355328LL );
2859 testcase( pIn1->u.i==140737488355327LL );
2860 testcase( pIn1->u.i==-140737488355328LL );
2861 testcase( pIn1->u.i==-140737488355329LL );
2862 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
2863 pIn1->flags |= MEM_IntReal;
2864 pIn1->flags &= ~MEM_Int;
2865 }else{
2866 pIn1->u.r = (double)pIn1->u.i;
2867 pIn1->flags |= MEM_Real;
2868 pIn1->flags &= ~MEM_Int;
2869 }
drh83a1daf2019-05-01 18:59:33 +00002870 }
drh6fcc1ec2019-05-01 14:41:47 +00002871 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
drh83a1daf2019-05-01 18:59:33 +00002872 zAffinity++;
2873 if( zAffinity[0]==0 ) break;
drh039fc322009-11-17 18:31:47 +00002874 pIn1++;
drh83a1daf2019-05-01 18:59:33 +00002875 }
danielk1977751de562008-04-18 09:01:15 +00002876 break;
2877}
2878
drh1db639c2008-01-17 02:36:28 +00002879/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002880** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002881**
drh710c4842010-08-30 01:17:20 +00002882** Convert P2 registers beginning with P1 into the [record format]
2883** use as a data record in a database table or as a key
2884** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002885**
drhbb6783b2017-04-29 18:02:49 +00002886** P4 may be a string that is P2 characters long. The N-th character of the
2887** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002888** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002889**
drh8a512562005-11-14 22:29:05 +00002890** The mapping from character to affinity is given by the SQLITE_AFF_
2891** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002892**
drh05883a32015-06-02 15:32:08 +00002893** If P4 is NULL then all index fields have the affinity BLOB.
drhda369332020-06-29 20:09:04 +00002894**
2895** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
2896** compile-time option is enabled:
2897**
2898** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
2899** of the right-most table that can be null-trimmed.
2900**
2901** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
2902** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
2903** accept no-change records with serial_type 10. This value is
2904** only used inside an assert() and does not affect the end result.
drh7f057c92005-06-24 03:53:06 +00002905*/
drh1db639c2008-01-17 02:36:28 +00002906case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002907 Mem *pRec; /* The new record */
2908 u64 nData; /* Number of bytes of data space */
2909 int nHdr; /* Number of bytes of header space */
2910 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002911 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002912 int nVarint; /* Number of bytes in a varint */
2913 u32 serial_type; /* Type field */
2914 Mem *pData0; /* First field to be combined into the record */
2915 Mem *pLast; /* Last field of the record */
2916 int nField; /* Number of fields in the record */
2917 char *zAffinity; /* The affinity string for the record */
2918 int file_format; /* File format to use for encoding */
drhbe37c122015-10-16 14:54:17 +00002919 u32 len; /* Length of a field */
drhb70b0df2019-04-30 01:08:42 +00002920 u8 *zHdr; /* Where to write next byte of the header */
2921 u8 *zPayload; /* Where to write next byte of the payload */
drh856c1032009-06-02 15:21:42 +00002922
drhf3218fe2004-05-28 08:21:02 +00002923 /* Assuming the record contains N fields, the record format looks
2924 ** like this:
2925 **
drh7a224de2004-06-02 01:22:02 +00002926 ** ------------------------------------------------------------------------
2927 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2928 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002929 **
drh9cbf3422008-01-17 16:22:13 +00002930 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002931 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002932 **
2933 ** Each type field is a varint representing the serial type of the
2934 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002935 ** hdr-size field is also a varint which is the offset from the beginning
2936 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002937 */
drh856c1032009-06-02 15:21:42 +00002938 nData = 0; /* Number of bytes of data space */
2939 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002940 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002941 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002942 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002943 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002944 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002945 nField = pOp->p2;
2946 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002947 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002948
drh2b4ded92010-09-27 21:09:31 +00002949 /* Identify the output register */
2950 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2951 pOut = &aMem[pOp->p3];
2952 memAboutToChange(p, pOut);
2953
drh3e6c0602013-12-10 20:53:01 +00002954 /* Apply the requested affinity to all inputs
2955 */
2956 assert( pData0<=pLast );
2957 if( zAffinity ){
2958 pRec = pData0;
2959 do{
drh5ad12512019-05-09 16:22:51 +00002960 applyAffinity(pRec, zAffinity[0], encoding);
danbe812622019-05-17 15:59:11 +00002961 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
2962 pRec->flags |= MEM_IntReal;
2963 pRec->flags &= ~(MEM_Int);
2964 }
drh5ad12512019-05-09 16:22:51 +00002965 REGISTER_TRACE((int)(pRec-aMem), pRec);
2966 zAffinity++;
2967 pRec++;
drh57bf4a82014-02-17 14:59:22 +00002968 assert( zAffinity[0]==0 || pRec<=pLast );
2969 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002970 }
2971
drhd447dce2017-01-25 20:55:11 +00002972#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002973 /* NULLs can be safely trimmed from the end of the record, as long as
2974 ** as the schema format is 2 or more and none of the omitted columns
2975 ** have a non-NULL default value. Also, the record must be left with
2976 ** at least one field. If P5>0 then it will be one more than the
2977 ** index of the right-most column with a non-NULL default value */
2978 if( pOp->p5 ){
2979 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2980 pLast--;
2981 nField--;
2982 }
2983 }
drhd447dce2017-01-25 20:55:11 +00002984#endif
drh585ce192017-01-25 14:58:27 +00002985
drhf3218fe2004-05-28 08:21:02 +00002986 /* Loop through the elements that will make up the record to figure
drh76fd7be2019-07-11 19:50:18 +00002987 ** out how much space is required for the new record. After this loop,
2988 ** the Mem.uTemp field of each term should hold the serial-type that will
2989 ** be used for that term in the generated record:
2990 **
2991 ** Mem.uTemp value type
2992 ** --------------- ---------------
2993 ** 0 NULL
2994 ** 1 1-byte signed integer
2995 ** 2 2-byte signed integer
2996 ** 3 3-byte signed integer
2997 ** 4 4-byte signed integer
2998 ** 5 6-byte signed integer
2999 ** 6 8-byte signed integer
3000 ** 7 IEEE float
3001 ** 8 Integer constant 0
3002 ** 9 Integer constant 1
3003 ** 10,11 reserved for expansion
3004 ** N>=12 and even BLOB
3005 ** N>=13 and odd text
3006 **
3007 ** The following additional values are computed:
3008 ** nHdr Number of bytes needed for the record header
3009 ** nData Number of bytes of data space needed for the record
3010 ** nZero Zero bytes at the end of the record
danielk19778d059842004-05-12 11:24:02 +00003011 */
drh038b7bc2013-12-09 23:17:22 +00003012 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00003013 do{
drh2b4ded92010-09-27 21:09:31 +00003014 assert( memIsValid(pRec) );
drhc1da4392019-07-11 19:22:36 +00003015 if( pRec->flags & MEM_Null ){
3016 if( pRec->flags & MEM_Zero ){
drh41fb3672018-01-12 23:18:38 +00003017 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3018 ** table methods that never invoke sqlite3_result_xxxxx() while
3019 ** computing an unchanging column value in an UPDATE statement.
3020 ** Give such values a special internal-use-only serial-type of 10
3021 ** so that they can be passed through to xUpdate and have
3022 ** a true sqlite3_value_nochange(). */
drhda369332020-06-29 20:09:04 +00003023#ifndef SQLITE_ENABLE_NULL_TRIM
drh41fb3672018-01-12 23:18:38 +00003024 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
drhda369332020-06-29 20:09:04 +00003025#endif
drhc1da4392019-07-11 19:22:36 +00003026 pRec->uTemp = 10;
drh038b7bc2013-12-09 23:17:22 +00003027 }else{
drh76fd7be2019-07-11 19:50:18 +00003028 pRec->uTemp = 0;
drh038b7bc2013-12-09 23:17:22 +00003029 }
drhc1da4392019-07-11 19:22:36 +00003030 nHdr++;
3031 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
3032 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3033 i64 i = pRec->u.i;
drh9c3bb592019-07-30 21:00:13 +00003034 u64 uu;
drhc1da4392019-07-11 19:22:36 +00003035 testcase( pRec->flags & MEM_Int );
3036 testcase( pRec->flags & MEM_IntReal );
3037 if( i<0 ){
drh9c3bb592019-07-30 21:00:13 +00003038 uu = ~i;
drhc1da4392019-07-11 19:22:36 +00003039 }else{
drh9c3bb592019-07-30 21:00:13 +00003040 uu = i;
drhc1da4392019-07-11 19:22:36 +00003041 }
3042 nHdr++;
drh9c3bb592019-07-30 21:00:13 +00003043 testcase( uu==127 ); testcase( uu==128 );
3044 testcase( uu==32767 ); testcase( uu==32768 );
3045 testcase( uu==8388607 ); testcase( uu==8388608 );
3046 testcase( uu==2147483647 ); testcase( uu==2147483648 );
3047 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
3048 if( uu<=127 ){
drhc1da4392019-07-11 19:22:36 +00003049 if( (i&1)==i && file_format>=4 ){
drh9c3bb592019-07-30 21:00:13 +00003050 pRec->uTemp = 8+(u32)uu;
drhc1da4392019-07-11 19:22:36 +00003051 }else{
3052 nData++;
3053 pRec->uTemp = 1;
3054 }
drh9c3bb592019-07-30 21:00:13 +00003055 }else if( uu<=32767 ){
drhc1da4392019-07-11 19:22:36 +00003056 nData += 2;
3057 pRec->uTemp = 2;
drh9c3bb592019-07-30 21:00:13 +00003058 }else if( uu<=8388607 ){
drhc1da4392019-07-11 19:22:36 +00003059 nData += 3;
3060 pRec->uTemp = 3;
drh9c3bb592019-07-30 21:00:13 +00003061 }else if( uu<=2147483647 ){
drhc1da4392019-07-11 19:22:36 +00003062 nData += 4;
3063 pRec->uTemp = 4;
drh9c3bb592019-07-30 21:00:13 +00003064 }else if( uu<=140737488355327LL ){
drhc1da4392019-07-11 19:22:36 +00003065 nData += 6;
3066 pRec->uTemp = 5;
3067 }else{
3068 nData += 8;
3069 if( pRec->flags & MEM_IntReal ){
3070 /* If the value is IntReal and is going to take up 8 bytes to store
3071 ** as an integer, then we might as well make it an 8-byte floating
3072 ** point value */
3073 pRec->u.r = (double)pRec->u.i;
3074 pRec->flags &= ~MEM_IntReal;
3075 pRec->flags |= MEM_Real;
3076 pRec->uTemp = 7;
3077 }else{
3078 pRec->uTemp = 6;
3079 }
3080 }
3081 }else if( pRec->flags & MEM_Real ){
3082 nHdr++;
3083 nData += 8;
3084 pRec->uTemp = 7;
3085 }else{
3086 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
3087 assert( pRec->n>=0 );
3088 len = (u32)pRec->n;
3089 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
3090 if( pRec->flags & MEM_Zero ){
3091 serial_type += pRec->u.nZero*2;
3092 if( nData ){
3093 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
3094 len += pRec->u.nZero;
3095 }else{
3096 nZero += pRec->u.nZero;
3097 }
3098 }
3099 nData += len;
3100 nHdr += sqlite3VarintLen(serial_type);
3101 pRec->uTemp = serial_type;
drhfdf972a2007-05-02 13:30:27 +00003102 }
drh45c3c662016-04-07 14:16:16 +00003103 if( pRec==pData0 ) break;
3104 pRec--;
3105 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00003106
drh654858d2014-11-20 02:18:14 +00003107 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3108 ** which determines the total number of bytes in the header. The varint
3109 ** value is the size of the header in bytes including the size varint
3110 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00003111 testcase( nHdr==126 );
3112 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00003113 if( nHdr<=126 ){
3114 /* The common case */
3115 nHdr += 1;
3116 }else{
3117 /* Rare case of a really large header */
3118 nVarint = sqlite3VarintLen(nHdr);
3119 nHdr += nVarint;
3120 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00003121 }
drh038b7bc2013-12-09 23:17:22 +00003122 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00003123
danielk1977a7a8e142008-02-13 18:25:27 +00003124 /* Make sure the output register has a buffer large enough to store
3125 ** the new record. The output register (pOp->p3) is not allowed to
3126 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00003127 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00003128 */
drh0d7f0cc2018-09-21 13:07:14 +00003129 if( nByte+nZero<=pOut->szMalloc ){
3130 /* The output register is already large enough to hold the record.
3131 ** No error checks or buffer enlargement is required */
3132 pOut->z = pOut->zMalloc;
3133 }else{
3134 /* Need to make sure that the output is not too big and then enlarge
3135 ** the output register to hold the full result */
3136 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
3137 goto too_big;
3138 }
3139 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
3140 goto no_mem;
3141 }
danielk19778d059842004-05-12 11:24:02 +00003142 }
drh9c1905f2008-12-10 22:32:56 +00003143 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00003144 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00003145 if( nZero ){
drh8df32842008-12-09 02:51:23 +00003146 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00003147 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00003148 }
drhb7654112008-01-12 12:48:07 +00003149 UPDATE_MAX_BLOBSIZE(pOut);
drhb70b0df2019-04-30 01:08:42 +00003150 zHdr = (u8 *)pOut->z;
3151 zPayload = zHdr + nHdr;
3152
3153 /* Write the record */
3154 zHdr += putVarint32(zHdr, nHdr);
3155 assert( pData0<=pLast );
3156 pRec = pData0;
3157 do{
3158 serial_type = pRec->uTemp;
3159 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3160 ** additional varints, one per column. */
3161 zHdr += putVarint32(zHdr, serial_type); /* serial type */
3162 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
3163 ** immediately follow the header. */
3164 zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
3165 }while( (++pRec)<=pLast );
3166 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
3167 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
3168
3169 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
3170 REGISTER_TRACE(pOp->p3, pOut);
danielk19778d059842004-05-12 11:24:02 +00003171 break;
3172}
3173
drh9f274632020-03-17 17:11:23 +00003174/* Opcode: Count P1 P2 p3 * *
drh81316f82013-10-29 20:40:47 +00003175** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00003176**
3177** Store the number of entries (an integer value) in the table or index
drh9f274632020-03-17 17:11:23 +00003178** opened by cursor P1 in register P2.
3179**
3180** If P3==0, then an exact count is obtained, which involves visiting
3181** every btree page of the table. But if P3 is non-zero, an estimate
3182** is returned based on the current cursor position.
danielk1977a5533162009-02-24 10:01:51 +00003183*/
drh27a348c2015-04-13 19:14:06 +00003184case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00003185 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00003186 BtCursor *pCrsr;
3187
drhc960dcb2015-11-20 19:22:01 +00003188 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
3189 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00003190 assert( pCrsr );
drh9f274632020-03-17 17:11:23 +00003191 if( pOp->p3 ){
3192 nEntry = sqlite3BtreeRowCountEst(pCrsr);
3193 }else{
3194 nEntry = 0; /* Not needed. Only used to silence a warning. */
3195 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
3196 if( rc ) goto abort_due_to_error;
3197 }
drh27a348c2015-04-13 19:14:06 +00003198 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00003199 pOut->u.i = nEntry;
drh21f6daa2019-10-11 14:21:48 +00003200 goto check_for_interrupt;
danielk1977a5533162009-02-24 10:01:51 +00003201}
danielk1977a5533162009-02-24 10:01:51 +00003202
danielk1977fd7f0452008-12-17 17:30:26 +00003203/* Opcode: Savepoint P1 * * P4 *
3204**
3205** Open, release or rollback the savepoint named by parameter P4, depending
drh2ce9b6b2019-05-10 14:03:07 +00003206** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3207** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3208** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
danielk1977fd7f0452008-12-17 17:30:26 +00003209*/
3210case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00003211 int p1; /* Value of P1 operand */
3212 char *zName; /* Name of savepoint */
3213 int nName;
3214 Savepoint *pNew;
3215 Savepoint *pSavepoint;
3216 Savepoint *pTmp;
3217 int iSavepoint;
3218 int ii;
3219
3220 p1 = pOp->p1;
3221 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003222
3223 /* Assert that the p1 parameter is valid. Also that if there is no open
3224 ** transaction, then there cannot be any savepoints.
3225 */
3226 assert( db->pSavepoint==0 || db->autoCommit==0 );
3227 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3228 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3229 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003230 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003231
3232 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003233 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003234 /* A new savepoint cannot be created if there are active write
3235 ** statements (i.e. open read/write incremental blob handles).
3236 */
drh22c17b82015-05-15 04:13:15 +00003237 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003238 rc = SQLITE_BUSY;
3239 }else{
drh856c1032009-06-02 15:21:42 +00003240 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003241
drhbe07ec52011-06-03 12:15:26 +00003242#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003243 /* This call is Ok even if this savepoint is actually a transaction
3244 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3245 ** If this is a transaction savepoint being opened, it is guaranteed
3246 ** that the db->aVTrans[] array is empty. */
3247 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003248 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3249 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003250 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003251#endif
dand9495cd2011-04-27 12:08:04 +00003252
danielk1977fd7f0452008-12-17 17:30:26 +00003253 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003254 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003255 if( pNew ){
3256 pNew->zName = (char *)&pNew[1];
3257 memcpy(pNew->zName, zName, nName+1);
3258
3259 /* If there is no open transaction, then mark this as a special
3260 ** "transaction savepoint". */
3261 if( db->autoCommit ){
3262 db->autoCommit = 0;
3263 db->isTransactionSavepoint = 1;
3264 }else{
3265 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003266 }
dan21e8d012011-03-03 20:05:59 +00003267
danielk1977fd7f0452008-12-17 17:30:26 +00003268 /* Link the new savepoint into the database handle's list. */
3269 pNew->pNext = db->pSavepoint;
3270 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003271 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003272 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003273 }
3274 }
3275 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003276 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
drh856c1032009-06-02 15:21:42 +00003277 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003278
3279 /* Find the named savepoint. If there is no such savepoint, then an
3280 ** an error is returned to the user. */
3281 for(
drh856c1032009-06-02 15:21:42 +00003282 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003283 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003284 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003285 ){
3286 iSavepoint++;
3287 }
3288 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003289 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003290 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003291 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003292 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003293 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003294 */
drh22c17b82015-05-15 04:13:15 +00003295 sqlite3VdbeError(p, "cannot release savepoint - "
3296 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003297 rc = SQLITE_BUSY;
3298 }else{
3299
3300 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003301 ** and this is a RELEASE command, then the current transaction
3302 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003303 */
3304 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3305 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003306 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003307 goto vdbe_return;
3308 }
danielk1977fd7f0452008-12-17 17:30:26 +00003309 db->autoCommit = 1;
3310 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003311 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003312 db->autoCommit = 0;
3313 p->rc = rc = SQLITE_BUSY;
3314 goto vdbe_return;
3315 }
danielk197734cf35d2008-12-18 18:31:38 +00003316 rc = p->rc;
drh94649b62019-12-18 02:12:04 +00003317 if( rc ){
3318 db->autoCommit = 0;
3319 }else{
3320 db->isTransactionSavepoint = 0;
3321 }
danielk1977fd7f0452008-12-17 17:30:26 +00003322 }else{
drh47b7fc72014-11-11 01:33:57 +00003323 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003324 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003325 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003326 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003327 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003328 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3329 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003330 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003331 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003332 }
drh47b7fc72014-11-11 01:33:57 +00003333 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003334 assert( p1==SAVEPOINT_RELEASE );
drh47b7fc72014-11-11 01:33:57 +00003335 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003336 }
3337 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003338 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3339 if( rc!=SQLITE_OK ){
3340 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003341 }
danielk1977fd7f0452008-12-17 17:30:26 +00003342 }
drh47b7fc72014-11-11 01:33:57 +00003343 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003344 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003345 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003346 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003347 }
3348 }
drh95866af2019-12-15 00:36:33 +00003349 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003350
3351 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3352 ** savepoints nested inside of the savepoint being operated on. */
3353 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003354 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003355 db->pSavepoint = pTmp->pNext;
3356 sqlite3DbFree(db, pTmp);
3357 db->nSavepoint--;
3358 }
3359
dan1da40a32009-09-19 17:00:31 +00003360 /* If it is a RELEASE, then destroy the savepoint being operated on
3361 ** too. If it is a ROLLBACK TO, then set the number of deferred
3362 ** constraint violations present in the database to the value stored
3363 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003364 if( p1==SAVEPOINT_RELEASE ){
3365 assert( pSavepoint==db->pSavepoint );
3366 db->pSavepoint = pSavepoint->pNext;
3367 sqlite3DbFree(db, pSavepoint);
3368 if( !isTransaction ){
3369 db->nSavepoint--;
3370 }
dan1da40a32009-09-19 17:00:31 +00003371 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003372 assert( p1==SAVEPOINT_ROLLBACK );
dan1da40a32009-09-19 17:00:31 +00003373 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003374 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003375 }
dand9495cd2011-04-27 12:08:04 +00003376
danea8562e2015-04-18 16:25:54 +00003377 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003378 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3379 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3380 }
danielk1977fd7f0452008-12-17 17:30:26 +00003381 }
3382 }
drh9467abf2016-02-17 18:44:11 +00003383 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003384
3385 break;
3386}
3387
drh98757152008-01-09 23:04:12 +00003388/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003389**
3390** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003391** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003392** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3393** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003394**
3395** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003396*/
drh9cbf3422008-01-17 16:22:13 +00003397case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003398 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003399 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003400
drh856c1032009-06-02 15:21:42 +00003401 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003402 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003403 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003404 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003405 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003406 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003407
drhb0c88652016-02-01 13:21:13 +00003408 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003409 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003410 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003411 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003412 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003413 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3414 /* If this instruction implements a COMMIT and other VMs are writing
3415 ** return an error indicating that the other VMs must complete first.
3416 */
3417 sqlite3VdbeError(p, "cannot commit transaction - "
3418 "SQL statements in progress");
3419 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003420 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003421 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003422 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003423 }else{
shane7d3846a2008-12-11 02:58:26 +00003424 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003425 }
3426 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3427 p->pc = (int)(pOp - aOp);
3428 db->autoCommit = (u8)(1-desiredAutoCommit);
3429 p->rc = rc = SQLITE_BUSY;
3430 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003431 }
danielk1977fd7f0452008-12-17 17:30:26 +00003432 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003433 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003434 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003435 }else{
drh900b31e2007-08-28 02:27:51 +00003436 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003437 }
drh900b31e2007-08-28 02:27:51 +00003438 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003439 }else{
drh22c17b82015-05-15 04:13:15 +00003440 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003441 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003442 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003443 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003444
3445 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003446 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003447 }
drh8616cff2019-07-13 16:15:23 +00003448 /*NOTREACHED*/ assert(0);
drh663fc632002-02-02 18:49:19 +00003449}
3450
drhb22f7c82014-02-06 23:56:27 +00003451/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003452**
drh05a86c52014-02-16 01:55:49 +00003453** Begin a transaction on database P1 if a transaction is not already
3454** active.
3455** If P2 is non-zero, then a write-transaction is started, or if a
3456** read-transaction is already active, it is upgraded to a write-transaction.
drh1ca037f2020-10-12 13:24:00 +00003457** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3458** then an exclusive transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003459**
drh001bbcb2003-03-19 03:14:00 +00003460** P1 is the index of the database file on which the transaction is
3461** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003462** file used for temporary tables. Indices of 2 or more are used for
3463** attached databases.
drhcabb0812002-09-14 13:47:32 +00003464**
dane0af83a2009-09-08 19:15:01 +00003465** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3466** true (this flag is set if the Vdbe may modify more than one row and may
3467** throw an ABORT exception), a statement transaction may also be opened.
3468** More specifically, a statement transaction is opened iff the database
3469** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003470** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003471** VDBE to be rolled back after an error without having to roll back the
3472** entire transaction. If no error is encountered, the statement transaction
3473** will automatically commit when the VDBE halts.
3474**
drhb22f7c82014-02-06 23:56:27 +00003475** If P5!=0 then this opcode also checks the schema cookie against P3
3476** and the schema generation counter against P4.
3477** The cookie changes its value whenever the database schema changes.
3478** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003479** and that the current process needs to reread the schema. If the schema
3480** cookie in P3 differs from the schema cookie in the database header or
3481** if the schema generation counter in P4 differs from the current
3482** generation counter, then an SQLITE_SCHEMA error is raised and execution
3483** halts. The sqlite3_step() wrapper function might then reprepare the
3484** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003485*/
drh9cbf3422008-01-17 16:22:13 +00003486case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003487 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003488 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003489
drh1713afb2013-06-28 01:24:57 +00003490 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003491 assert( p->readOnly==0 || pOp->p2==0 );
drh1ca037f2020-10-12 13:24:00 +00003492 assert( pOp->p2>=0 && pOp->p2<=2 );
drh653b82a2009-06-22 11:10:47 +00003493 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003494 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003495 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3496 rc = SQLITE_READONLY;
3497 goto abort_due_to_error;
3498 }
drh653b82a2009-06-22 11:10:47 +00003499 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003500
danielk197724162fe2004-06-04 06:22:00 +00003501 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003502 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003503 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3504 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003505 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003506 if( (rc&0xff)==SQLITE_BUSY ){
3507 p->pc = (int)(pOp - aOp);
3508 p->rc = rc;
3509 goto vdbe_return;
3510 }
danielk197724162fe2004-06-04 06:22:00 +00003511 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003512 }
dane0af83a2009-09-08 19:15:01 +00003513
drh4d294482019-10-05 15:28:24 +00003514 if( p->usesStmtJournal
3515 && pOp->p2
danc0537fe2013-06-28 19:41:43 +00003516 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003517 ){
drh99744fa2020-08-25 19:09:07 +00003518 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE );
dane0af83a2009-09-08 19:15:01 +00003519 if( p->iStatement==0 ){
3520 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3521 db->nStatement++;
3522 p->iStatement = db->nSavepoint + db->nStatement;
3523 }
dana311b802011-04-26 19:21:34 +00003524
drh346506f2011-05-25 01:16:42 +00003525 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003526 if( rc==SQLITE_OK ){
3527 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3528 }
dan1da40a32009-09-19 17:00:31 +00003529
3530 /* Store the current value of the database handles deferred constraint
3531 ** counter. If the statement transaction needs to be rolled back,
3532 ** the value of this counter needs to be restored too. */
3533 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003534 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003535 }
drh397776a2018-06-06 17:45:51 +00003536 }
3537 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3538 if( pOp->p5
3539 && (iMeta!=pOp->p3
3540 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3541 ){
dand2ffc972020-12-10 19:20:15 +00003542 /*
3543 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3544 ** version is checked to ensure that the schema has not changed since the
3545 ** SQL statement was prepared.
3546 */
3547 sqlite3DbFree(db, p->zErrMsg);
3548 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
drhb22f7c82014-02-06 23:56:27 +00003549 /* If the schema-cookie from the database file matches the cookie
3550 ** stored with the in-memory representation of the schema, do
3551 ** not reload the schema from the database file.
3552 **
3553 ** If virtual-tables are in use, this is not just an optimization.
3554 ** Often, v-tables store their data in other SQLite tables, which
3555 ** are queried from within xNext() and other v-table methods using
3556 ** prepared queries. If such a query is out-of-date, we do not want to
3557 ** discard the database schema, as the user code implementing the
3558 ** v-table would have to be ready for the sqlite3_vtab structure itself
3559 ** to be invalidated whenever sqlite3_step() is called from within
3560 ** a v-table method.
3561 */
3562 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3563 sqlite3ResetOneSchema(db, pOp->p1);
3564 }
3565 p->expired = 1;
3566 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003567 }
drh9467abf2016-02-17 18:44:11 +00003568 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003569 break;
3570}
3571
drhb1fdb2a2008-01-05 04:06:03 +00003572/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003573**
drh9cbf3422008-01-17 16:22:13 +00003574** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003575** P3==1 is the schema version. P3==2 is the database format.
3576** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003577** the main database file and P1==1 is the database file used to store
3578** temporary tables.
drh4a324312001-12-21 14:30:42 +00003579**
drh50e5dad2001-09-15 00:57:28 +00003580** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003581** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003582** executing this instruction.
3583*/
drh27a348c2015-04-13 19:14:06 +00003584case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003585 int iMeta;
drh856c1032009-06-02 15:21:42 +00003586 int iDb;
3587 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003588
drh1713afb2013-06-28 01:24:57 +00003589 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003590 iDb = pOp->p1;
3591 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003592 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003593 assert( iDb>=0 && iDb<db->nDb );
3594 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003595 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003596
danielk1977602b4662009-07-02 07:47:33 +00003597 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003598 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003599 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003600 break;
3601}
3602
drhe3863b52020-07-01 16:19:14 +00003603/* Opcode: SetCookie P1 P2 P3 * P5
drh50e5dad2001-09-15 00:57:28 +00003604**
drh1861afc2016-02-01 21:48:34 +00003605** Write the integer value P3 into cookie number P2 of database P1.
3606** P2==1 is the schema version. P2==2 is the database format.
3607** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003608** size, and so forth. P1==0 is the main database file and P1==1 is the
3609** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003610**
3611** A transaction must be started before executing this opcode.
drhe3863b52020-07-01 16:19:14 +00003612**
3613** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
3614** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
3615** has P5 set to 1, so that the internal schema version will be different
3616** from the database schema version, resulting in a schema reset.
drh50e5dad2001-09-15 00:57:28 +00003617*/
drh1861afc2016-02-01 21:48:34 +00003618case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003619 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003620
3621 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003622 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003623 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003624 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003625 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003626 pDb = &db->aDb[pOp->p1];
3627 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003628 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003629 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003630 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003631 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003632 /* When the schema cookie changes, record the new cookie internally */
drhe3863b52020-07-01 16:19:14 +00003633 pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5;
drh8257aa82017-07-26 19:59:13 +00003634 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003635 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003636 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003637 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003638 }
drhfd426c62006-01-30 15:34:22 +00003639 if( pOp->p1==1 ){
3640 /* Invalidate all prepared statements whenever the TEMP database
3641 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003642 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003643 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003644 }
drh9467abf2016-02-17 18:44:11 +00003645 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003646 break;
3647}
3648
drh98757152008-01-09 23:04:12 +00003649/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003650** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003651**
drhecdc7532001-09-23 02:35:53 +00003652** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003653** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003654** P3==0 means the main database, P3==1 means the database used for
3655** temporary tables, and P3>1 means used the corresponding attached
3656** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003657** values need not be contiguous but all P1 values should be small integers.
3658** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003659**
drh8e9deb62018-06-05 13:43:02 +00003660** Allowed P5 bits:
3661** <ul>
3662** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3663** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003664** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003665** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003666**
danielk1977d336e222009-02-20 10:58:41 +00003667** The P4 value may be either an integer (P4_INT32) or a pointer to
3668** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003669** object, then table being opened must be an [index b-tree] where the
3670** KeyInfo object defines the content and collating
3671** sequence of that index b-tree. Otherwise, if P4 is an integer
3672** value, then the table being opened must be a [table b-tree] with a
3673** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003674**
drh35263192014-07-22 20:02:19 +00003675** See also: OpenWrite, ReopenIdx
3676*/
3677/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3678** Synopsis: root=P2 iDb=P3
3679**
drh8e9deb62018-06-05 13:43:02 +00003680** The ReopenIdx opcode works like OP_OpenRead except that it first
3681** checks to see if the cursor on P1 is already open on the same
3682** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003683** if the cursor is already open, do not reopen it.
3684**
drh8e9deb62018-06-05 13:43:02 +00003685** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3686** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3687** be the same as every other ReopenIdx or OpenRead for the same cursor
3688** number.
drh35263192014-07-22 20:02:19 +00003689**
drh8e9deb62018-06-05 13:43:02 +00003690** Allowed P5 bits:
3691** <ul>
3692** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3693** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003694** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003695** </ul>
3696**
3697** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003698*/
drh98757152008-01-09 23:04:12 +00003699/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003700** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003701**
3702** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003703** page is P2 (or whose root page is held in register P2 if the
3704** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003705**
danielk1977d336e222009-02-20 10:58:41 +00003706** The P4 value may be either an integer (P4_INT32) or a pointer to
3707** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003708** object, then table being opened must be an [index b-tree] where the
3709** KeyInfo object defines the content and collating
3710** sequence of that index b-tree. Otherwise, if P4 is an integer
3711** value, then the table being opened must be a [table b-tree] with a
3712** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003713**
drh8e9deb62018-06-05 13:43:02 +00003714** Allowed P5 bits:
3715** <ul>
3716** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3717** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003718** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003719** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3720** and subsequently delete entries in an index btree. This is a
3721** hint to the storage engine that the storage engine is allowed to
3722** ignore. The hint is not used by the official SQLite b*tree storage
3723** engine, but is used by COMDB2.
3724** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3725** as the root page, not the value of P2 itself.
3726** </ul>
drhf57b3392001-10-08 13:22:32 +00003727**
drh8e9deb62018-06-05 13:43:02 +00003728** This instruction works like OpenRead except that it opens the cursor
3729** in read/write mode.
3730**
3731** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003732*/
drh35263192014-07-22 20:02:19 +00003733case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003734 int nField;
3735 KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00003736 u32 p2;
drh856c1032009-06-02 15:21:42 +00003737 int iDb;
drhf57b3392001-10-08 13:22:32 +00003738 int wrFlag;
3739 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003740 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003741 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003742
drhe0997b32015-03-20 14:57:50 +00003743 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003744 assert( pOp->p4type==P4_KEYINFO );
3745 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003746 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003747 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003748 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003749 }
3750 /* If the cursor is not currently open or is open on a different
3751 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003752case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003753case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003754
drhe0997b32015-03-20 14:57:50 +00003755 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003756 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003757 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3758 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003759
drhba968db2018-07-24 22:02:12 +00003760 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003761 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003762 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003763 }
3764
drh856c1032009-06-02 15:21:42 +00003765 nField = 0;
3766 pKeyInfo = 0;
drhabc38152020-07-22 13:38:04 +00003767 p2 = (u32)pOp->p2;
drh856c1032009-06-02 15:21:42 +00003768 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003769 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003770 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003771 pDb = &db->aDb[iDb];
3772 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003773 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003774 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003775 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3776 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003777 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003778 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3779 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003780 }
3781 }else{
3782 wrFlag = 0;
3783 }
dan428c2182012-08-06 18:50:11 +00003784 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003785 assert( p2>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00003786 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003787 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003788 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003789 assert( memIsValid(pIn2) );
3790 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003791 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003792 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003793 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003794 ** that opcode will always set the p2 value to 2 or more or else fail.
3795 ** If there were a failure, the prepared statement would have halted
3796 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003797 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003798 }
danielk1977d336e222009-02-20 10:58:41 +00003799 if( pOp->p4type==P4_KEYINFO ){
3800 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003801 assert( pKeyInfo->enc==ENC(db) );
3802 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003803 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003804 }else if( pOp->p4type==P4_INT32 ){
3805 nField = pOp->p4.i;
3806 }
drh653b82a2009-06-22 11:10:47 +00003807 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003808 assert( nField>=0 );
3809 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003810 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003811 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003812 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003813 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003814 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003815#ifdef SQLITE_DEBUG
3816 pCur->wrFlag = wrFlag;
3817#endif
drhc960dcb2015-11-20 19:22:01 +00003818 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003819 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003820 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003821 ** SQLite used to check if the root-page flags were sane at this point
3822 ** and report database corruption if they were not, but this check has
3823 ** since moved into the btree layer. */
3824 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003825
3826open_cursor_set_hints:
3827 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3828 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003829 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh0403cb32015-08-14 23:57:04 +00003830 testcase( pOp->p2 & OPFLAG_SEEKEQ );
drhc960dcb2015-11-20 19:22:01 +00003831 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003832 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003833 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003834 break;
3835}
3836
drhe08e8d62017-05-01 15:15:41 +00003837/* Opcode: OpenDup P1 P2 * * *
3838**
3839** Open a new cursor P1 that points to the same ephemeral table as
3840** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3841** opcode. Only ephemeral cursors may be duplicated.
3842**
3843** Duplicate ephemeral cursors are used for self-joins of materialized views.
3844*/
3845case OP_OpenDup: {
3846 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3847 VdbeCursor *pCx; /* The new cursor */
3848
3849 pOrig = p->apCsr[pOp->p2];
dan2811ea62019-12-23 14:20:46 +00003850 assert( pOrig );
drh5a4a15f2021-03-18 15:42:59 +00003851 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
drhe08e8d62017-05-01 15:15:41 +00003852
3853 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3854 if( pCx==0 ) goto no_mem;
3855 pCx->nullRow = 1;
3856 pCx->isEphemeral = 1;
3857 pCx->pKeyInfo = pOrig->pKeyInfo;
3858 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00003859 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00003860 pCx->isOrdered = pOrig->isOrdered;
daneeee8a52021-03-18 14:31:37 +00003861 pCx->pBtx = pOrig->pBtx;
drh5a4a15f2021-03-18 15:42:59 +00003862 pCx->hasBeenDuped = 1;
3863 pOrig->hasBeenDuped = 1;
daneeee8a52021-03-18 14:31:37 +00003864 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00003865 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003866 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3867 ** opened for a database. Since there is already an open cursor when this
3868 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3869 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003870 break;
3871}
3872
3873
drh32881be2020-11-17 21:26:13 +00003874/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003875** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003876**
drhb9bb7c12006-06-11 23:41:55 +00003877** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003878** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003879** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003880** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003881**
drhdfe3b582019-01-04 12:35:50 +00003882** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00003883** is cleared (all content is erased).
3884**
drh25d3adb2010-04-05 15:11:08 +00003885** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003886** The cursor points to a BTree table if P4==0 and to a BTree index
3887** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003888** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003889**
drh2a5d9902011-08-26 00:34:45 +00003890** The P5 parameter can be a mask of the BTREE_* flags defined
3891** in btree.h. These flags control aspects of the operation of
3892** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3893** added automatically.
drh32881be2020-11-17 21:26:13 +00003894**
3895** If P3 is positive, then reg[P3] is modified slightly so that it
3896** can be used as zero-length data for OP_Insert. This is an optimization
3897** that avoids an extra OP_Blob opcode to initialize that register.
drh5e00f6c2001-09-13 13:46:56 +00003898*/
drha21a64d2010-04-06 22:33:55 +00003899/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003900** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003901**
3902** This opcode works the same as OP_OpenEphemeral. It has a
3903** different name to distinguish its use. Tables created using
3904** by this opcode will be used for automatically created transient
3905** indices in joins.
3906*/
3907case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003908case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003909 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003910 KeyInfo *pKeyInfo;
3911
drhd4187c72010-08-30 22:15:45 +00003912 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003913 SQLITE_OPEN_READWRITE |
3914 SQLITE_OPEN_CREATE |
3915 SQLITE_OPEN_EXCLUSIVE |
3916 SQLITE_OPEN_DELETEONCLOSE |
3917 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003918 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003919 assert( pOp->p2>=0 );
drh32881be2020-11-17 21:26:13 +00003920 if( pOp->p3>0 ){
3921 /* Make register reg[P3] into a value that can be used as the data
3922 ** form sqlite3BtreeInsert() where the length of the data is zero. */
3923 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
3924 assert( pOp->opcode==OP_OpenEphemeral );
3925 assert( aMem[pOp->p3].flags & MEM_Null );
3926 aMem[pOp->p3].n = 0;
3927 aMem[pOp->p3].z = "";
3928 }
drh4afdfa12018-12-31 16:36:42 +00003929 pCx = p->apCsr[pOp->p1];
drh5a4a15f2021-03-18 15:42:59 +00003930 if( pCx && !pCx->hasBeenDuped ){
3931 /* If the ephermeral table is already open and has no duplicates from
3932 ** OP_OpenDup, then erase all existing content so that the table is
3933 ** empty again, rather than creating a new table. */
dana5129722019-05-03 18:50:24 +00003934 assert( pCx->isEphemeral );
dan855b5d12019-06-26 21:04:30 +00003935 pCx->seqCount = 0;
3936 pCx->cacheStatus = CACHE_STALE;
drh1ee02a12020-01-18 13:53:46 +00003937 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00003938 }else{
3939 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3940 if( pCx==0 ) goto no_mem;
drhd0fb7962018-12-31 17:58:05 +00003941 pCx->isEphemeral = 1;
3942 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3943 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
3944 vfsFlags);
3945 if( rc==SQLITE_OK ){
3946 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
daneeee8a52021-03-18 14:31:37 +00003947 if( rc==SQLITE_OK ){
3948 /* If a transient index is required, create it by calling
3949 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3950 ** opening it. If a transient table is required, just use the
3951 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3952 */
3953 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3954 assert( pOp->p4type==P4_KEYINFO );
3955 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pCx->pgnoRoot,
3956 BTREE_BLOBKEY | pOp->p5);
3957 if( rc==SQLITE_OK ){
3958 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
3959 assert( pKeyInfo->db==db );
3960 assert( pKeyInfo->enc==ENC(db) );
3961 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
3962 pKeyInfo, pCx->uc.pCursor);
3963 }
3964 pCx->isTable = 0;
3965 }else{
3966 pCx->pgnoRoot = SCHEMA_ROOT;
3967 rc = sqlite3BtreeCursor(pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR,
3968 0, pCx->uc.pCursor);
3969 pCx->isTable = 1;
drhd0fb7962018-12-31 17:58:05 +00003970 }
daneeee8a52021-03-18 14:31:37 +00003971 }
3972 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
3973 if( rc ){
3974 sqlite3BtreeClose(pCx->pBtx);
drhd0fb7962018-12-31 17:58:05 +00003975 }
3976 }
drh5e00f6c2001-09-13 13:46:56 +00003977 }
drh9467abf2016-02-17 18:44:11 +00003978 if( rc ) goto abort_due_to_error;
dan855b5d12019-06-26 21:04:30 +00003979 pCx->nullRow = 1;
dan5134d132011-09-02 10:31:11 +00003980 break;
3981}
3982
danfad9f9a2014-04-01 18:41:51 +00003983/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003984**
3985** This opcode works like OP_OpenEphemeral except that it opens
3986** a transient index that is specifically designed to sort large
3987** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003988**
3989** If argument P3 is non-zero, then it indicates that the sorter may
3990** assume that a stable sort considering the first P3 fields of each
3991** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003992*/
drhca892a72011-09-03 00:17:51 +00003993case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003994 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003995
drh399af1d2013-11-20 17:25:55 +00003996 assert( pOp->p1>=0 );
3997 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003998 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003999 if( pCx==0 ) goto no_mem;
4000 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00004001 assert( pCx->pKeyInfo->db==db );
4002 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00004003 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00004004 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004005 break;
4006}
4007
dan78d58432014-03-25 15:04:07 +00004008/* Opcode: SequenceTest P1 P2 * * *
4009** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4010**
4011** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4012** to P2. Regardless of whether or not the jump is taken, increment the
4013** the sequence value.
4014*/
4015case OP_SequenceTest: {
4016 VdbeCursor *pC;
4017 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4018 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004019 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00004020 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00004021 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00004022 }
drh5e00f6c2001-09-13 13:46:56 +00004023 break;
4024}
4025
drh5f612292014-02-08 23:20:32 +00004026/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00004027** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00004028**
4029** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00004030** row of data. The content of that one row is the content of memory
4031** register P2. In other words, cursor P1 becomes an alias for the
4032** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00004033**
drh2d8d7ce2010-02-15 15:17:05 +00004034** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00004035** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00004036** individual columns using the OP_Column opcode. The OP_Column opcode
4037** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00004038**
4039** P3 is the number of fields in the records that will be stored by
4040** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004041*/
drh9cbf3422008-01-17 16:22:13 +00004042case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00004043 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00004044
drh653b82a2009-06-22 11:10:47 +00004045 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004046 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00004047 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00004048 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00004049 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004050 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00004051 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004052 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4053 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4054 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4055 ** which is a performance optimization */
4056 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00004057 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00004058 break;
4059}
4060
drh98757152008-01-09 23:04:12 +00004061/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00004062**
4063** Close a cursor previously opened as P1. If P1 is not
4064** currently open, this instruction is a no-op.
4065*/
drh9cbf3422008-01-17 16:22:13 +00004066case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00004067 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4068 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
4069 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004070 break;
4071}
4072
drh97bae792015-06-05 15:59:57 +00004073#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4074/* Opcode: ColumnsUsed P1 * * P4 *
4075**
4076** This opcode (which only exists if SQLite was compiled with
4077** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4078** table or index for cursor P1 are used. P4 is a 64-bit integer
4079** (P4_INT64) in which the first 63 bits are one for each of the
4080** first 63 columns of the table or index that are actually used
4081** by the cursor. The high-order bit is set if any column after
4082** the 64th is used.
4083*/
4084case OP_ColumnsUsed: {
4085 VdbeCursor *pC;
4086 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004087 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00004088 pC->maskUsed = *(u64*)pOp->p4.pI64;
4089 break;
4090}
4091#endif
4092
drh8af3f772014-07-25 18:01:06 +00004093/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004094** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004095**
danielk1977b790c6c2008-04-18 10:25:24 +00004096** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004097** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004098** to an SQL index, then P3 is the first in an array of P4 registers
4099** that are used as an unpacked index key.
4100**
4101** Reposition cursor P1 so that it points to the smallest entry that
4102** is greater than or equal to the key value. If there are no records
4103** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004104**
drhb1d607d2015-11-05 22:30:54 +00004105** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004106** opcode will either land on a record that exactly matches the key, or
4107** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4108** this opcode must be followed by an IdxLE opcode with the same arguments.
4109** The IdxGT opcode will be skipped if this opcode succeeds, but the
4110** IdxGT opcode will be used on subsequent loop iterations. The
4111** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4112** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004113**
drh8af3f772014-07-25 18:01:06 +00004114** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00004115** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004116** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004117**
drh935850e2014-05-24 17:15:15 +00004118** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004119*/
drh8af3f772014-07-25 18:01:06 +00004120/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004121** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00004122**
danielk1977b790c6c2008-04-18 10:25:24 +00004123** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004124** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004125** to an SQL index, then P3 is the first in an array of P4 registers
4126** that are used as an unpacked index key.
4127**
drh576d0a92020-03-12 17:28:27 +00004128** Reposition cursor P1 so that it points to the smallest entry that
danielk1977b790c6c2008-04-18 10:25:24 +00004129** is greater than the key value. If there are no records greater than
4130** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00004131**
drh8af3f772014-07-25 18:01:06 +00004132** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +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, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00004137*/
drh8af3f772014-07-25 18:01:06 +00004138/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004139** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +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**
4146** Reposition cursor P1 so that it points to the largest entry that
4147** is less than the key value. If there are no records less than
4148** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00004149**
drh8af3f772014-07-25 18:01:06 +00004150** This opcode leaves the cursor configured to move in reverse order,
4151** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004152** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004153**
drh935850e2014-05-24 17:15:15 +00004154** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004155*/
drh8af3f772014-07-25 18:01:06 +00004156/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004157** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +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.
danielk1977751de562008-04-18 09:01:15 +00004163**
danielk1977b790c6c2008-04-18 10:25:24 +00004164** Reposition cursor P1 so that it points to the largest entry that
4165** is less than or equal to the key value. If there are no records
4166** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +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**
drhb1d607d2015-11-05 22:30:54 +00004172** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004173** opcode will either land on a record that exactly matches the key, or
4174** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4175** this opcode must be followed by an IdxLE opcode with the same arguments.
drhb1d607d2015-11-05 22:30:54 +00004176** The IdxGE opcode will be skipped if this opcode succeeds, but the
drh576d0a92020-03-12 17:28:27 +00004177** IdxGE opcode will be used on subsequent loop iterations. The
4178** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4179** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004180**
drh935850e2014-05-24 17:15:15 +00004181** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00004182*/
mistachkin758784d2018-07-25 15:12:29 +00004183case OP_SeekLT: /* jump, in3, group */
4184case OP_SeekLE: /* jump, in3, group */
4185case OP_SeekGE: /* jump, in3, group */
4186case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00004187 int res; /* Comparison result */
4188 int oc; /* Opcode */
4189 VdbeCursor *pC; /* The cursor to seek */
4190 UnpackedRecord r; /* The key to seek for */
4191 int nField; /* Number of columns or fields in the key */
4192 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00004193 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00004194
drh653b82a2009-06-22 11:10:47 +00004195 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00004196 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00004197 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004198 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004199 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00004200 assert( OP_SeekLE == OP_SeekLT+1 );
4201 assert( OP_SeekGE == OP_SeekLT+2 );
4202 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00004203 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00004204 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004205 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00004206 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00004207 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00004208#ifdef SQLITE_DEBUG
4209 pC->seekOp = pOp->opcode;
4210#endif
drhe0997b32015-03-20 14:57:50 +00004211
dana40cb962019-05-14 20:25:22 +00004212 pC->deferredMoveto = 0;
4213 pC->cacheStatus = CACHE_STALE;
drh3da046d2013-11-11 03:24:11 +00004214 if( pC->isTable ){
drh3e364802019-08-22 00:53:16 +00004215 u16 flags3, newType;
drh576d0a92020-03-12 17:28:27 +00004216 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00004217 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
4218 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00004219
drh3da046d2013-11-11 03:24:11 +00004220 /* The input value in P3 might be of any type: integer, real, string,
4221 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00004222 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00004223 pIn3 = &aMem[pOp->p3];
drh3e364802019-08-22 00:53:16 +00004224 flags3 = pIn3->flags;
4225 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00004226 applyNumericAffinity(pIn3, 0);
4227 }
drh3e364802019-08-22 00:53:16 +00004228 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
4229 newType = pIn3->flags; /* Record the type after applying numeric affinity */
4230 pIn3->flags = flags3; /* But convert the type back to its original */
drh959403f2008-12-12 17:56:16 +00004231
drh3da046d2013-11-11 03:24:11 +00004232 /* If the P3 value could not be converted into an integer without
4233 ** loss of information, then special processing is required... */
drh3e364802019-08-22 00:53:16 +00004234 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
4235 if( (newType & MEM_Real)==0 ){
4236 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
drh8616cff2019-07-13 16:15:23 +00004237 VdbeBranchTaken(1,2);
4238 goto jump_to_p2;
dan9edd8c12019-05-08 11:42:49 +00004239 }else{
dan873b0192019-05-09 11:19:27 +00004240 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4241 if( rc!=SQLITE_OK ) goto abort_due_to_error;
dan9edd8c12019-05-08 11:42:49 +00004242 goto seek_not_found;
4243 }
4244 }else
drh959403f2008-12-12 17:56:16 +00004245
danaa1776f2013-11-26 18:22:59 +00004246 /* If the approximation iKey is larger than the actual real search
4247 ** term, substitute >= for > and < for <=. e.g. if the search term
4248 ** is 4.9 and the integer approximation 5:
4249 **
4250 ** (x > 4.9) -> (x >= 5)
4251 ** (x <= 4.9) -> (x < 5)
4252 */
drh74eaba42014-09-18 17:52:15 +00004253 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004254 assert( OP_SeekGE==(OP_SeekGT-1) );
4255 assert( OP_SeekLT==(OP_SeekLE-1) );
4256 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
4257 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00004258 }
4259
4260 /* If the approximation iKey is smaller than the actual real search
4261 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00004262 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004263 assert( OP_SeekLE==(OP_SeekLT+1) );
4264 assert( OP_SeekGT==(OP_SeekGE+1) );
4265 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4266 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004267 }
dan9edd8c12019-05-08 11:42:49 +00004268 }
drhc960dcb2015-11-20 19:22:01 +00004269 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004270 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004271 if( rc!=SQLITE_OK ){
4272 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004273 }
drhaa736092009-06-22 00:55:30 +00004274 }else{
drh576d0a92020-03-12 17:28:27 +00004275 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4276 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4277 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4278 ** with the same key.
drhd6b79462015-11-07 01:19:00 +00004279 */
drhc960dcb2015-11-20 19:22:01 +00004280 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004281 eqOnly = 1;
4282 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4283 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
drh576d0a92020-03-12 17:28:27 +00004284 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
4285 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
drhd6b79462015-11-07 01:19:00 +00004286 assert( pOp[1].p1==pOp[0].p1 );
4287 assert( pOp[1].p2==pOp[0].p2 );
4288 assert( pOp[1].p3==pOp[0].p3 );
4289 assert( pOp[1].p4.i==pOp[0].p4.i );
4290 }
4291
drh3da046d2013-11-11 03:24:11 +00004292 nField = pOp->p4.i;
4293 assert( pOp->p4type==P4_INT32 );
4294 assert( nField>0 );
4295 r.pKeyInfo = pC->pKeyInfo;
4296 r.nField = (u16)nField;
4297
4298 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004299 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004300 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004301 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004302 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004303 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004304 */
dan1fed5da2014-02-25 21:01:25 +00004305 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4306 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4307 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4308 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4309 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004310
4311 r.aMem = &aMem[pOp->p3];
4312#ifdef SQLITE_DEBUG
4313 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4314#endif
drh70528d72015-11-05 20:25:09 +00004315 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004316 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004317 if( rc!=SQLITE_OK ){
4318 goto abort_due_to_error;
4319 }
drhb1d607d2015-11-05 22:30:54 +00004320 if( eqOnly && r.eqSeen==0 ){
4321 assert( res!=0 );
4322 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004323 }
drh3da046d2013-11-11 03:24:11 +00004324 }
drh3da046d2013-11-11 03:24:11 +00004325#ifdef SQLITE_TEST
4326 sqlite3_search_count++;
4327#endif
drh4a1d3652014-02-14 15:13:36 +00004328 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4329 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004330 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004331 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4332 if( rc!=SQLITE_OK ){
4333 if( rc==SQLITE_DONE ){
4334 rc = SQLITE_OK;
4335 res = 1;
4336 }else{
4337 goto abort_due_to_error;
4338 }
4339 }
drh3da046d2013-11-11 03:24:11 +00004340 }else{
4341 res = 0;
4342 }
4343 }else{
drh4a1d3652014-02-14 15:13:36 +00004344 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4345 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004346 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004347 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4348 if( rc!=SQLITE_OK ){
4349 if( rc==SQLITE_DONE ){
4350 rc = SQLITE_OK;
4351 res = 1;
4352 }else{
4353 goto abort_due_to_error;
4354 }
4355 }
drh3da046d2013-11-11 03:24:11 +00004356 }else{
4357 /* res might be negative because the table is empty. Check to
4358 ** see if this is the case.
4359 */
drhc960dcb2015-11-20 19:22:01 +00004360 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004361 }
4362 }
drhb1d607d2015-11-05 22:30:54 +00004363seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004364 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004365 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004366 if( res ){
drhf56fa462015-04-13 21:39:54 +00004367 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004368 }else if( eqOnly ){
4369 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4370 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004371 }
drh5e00f6c2001-09-13 13:46:56 +00004372 break;
4373}
dan71c57db2016-07-09 20:23:55 +00004374
drh68cf0ac2020-09-28 19:51:54 +00004375
drh04e70ce2020-10-02 11:55:07 +00004376/* Opcode: SeekScan P1 P2 * * *
drh68cf0ac2020-09-28 19:51:54 +00004377** Synopsis: Scan-ahead up to P1 rows
4378**
drhdfbaae72020-09-29 17:29:11 +00004379** This opcode is a prefix opcode to OP_SeekGE. In other words, this
drh04e70ce2020-10-02 11:55:07 +00004380** opcode must be immediately followed by OP_SeekGE. This constraint is
drhdfbaae72020-09-29 17:29:11 +00004381** checked by assert() statements.
4382**
4383** This opcode uses the P1 through P4 operands of the subsequent
4384** OP_SeekGE. In the text that follows, the operands of the subsequent
4385** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
drh04e70ce2020-10-02 11:55:07 +00004386** the P1 and P2 operands of this opcode are also used, and are called
4387** This.P1 and This.P2.
drh68cf0ac2020-09-28 19:51:54 +00004388**
4389** This opcode helps to optimize IN operators on a multi-column index
drhdfbaae72020-09-29 17:29:11 +00004390** where the IN operator is on the later terms of the index by avoiding
4391** unnecessary seeks on the btree, substituting steps to the next row
4392** of the b-tree instead. A correct answer is obtained if this opcode
4393** is omitted or is a no-op.
drh68cf0ac2020-09-28 19:51:54 +00004394**
drhdfbaae72020-09-29 17:29:11 +00004395** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4396** is the desired entry that we want the cursor SeekGE.P1 to be pointing
4397** to. Call this SeekGE.P4/P5 row the "target".
drh68cf0ac2020-09-28 19:51:54 +00004398**
drha54e1b12020-09-29 23:52:25 +00004399** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4400** then this opcode is a no-op and control passes through into the OP_SeekGE.
drh68cf0ac2020-09-28 19:51:54 +00004401**
drhdfbaae72020-09-29 17:29:11 +00004402** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4403** might be the target row, or it might be near and slightly before the
4404** target row. This opcode attempts to position the cursor on the target
drh04e70ce2020-10-02 11:55:07 +00004405** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
drhdfbaae72020-09-29 17:29:11 +00004406** between 0 and This.P1 times.
drh68cf0ac2020-09-28 19:51:54 +00004407**
drhdfbaae72020-09-29 17:29:11 +00004408** There are three possible outcomes from this opcode:<ol>
drh68cf0ac2020-09-28 19:51:54 +00004409**
drh3c48ee92021-03-20 01:00:26 +00004410** <li> If after This.P1 steps, the cursor is still pointing to a place that
4411** is earlier in the btree than the target row, then fall through
4412** into the subsquence OP_SeekGE opcode.
drh68cf0ac2020-09-28 19:51:54 +00004413**
drhdfbaae72020-09-29 17:29:11 +00004414** <li> If the cursor is successfully moved to the target row by 0 or more
drh04e70ce2020-10-02 11:55:07 +00004415** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
drh3c48ee92021-03-20 01:00:26 +00004416** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
drhdfbaae72020-09-29 17:29:11 +00004417**
4418** <li> If the cursor ends up past the target row (indicating the the target
4419** row does not exist in the btree) then jump to SeekOP.P2.
4420** </ol>
drh68cf0ac2020-09-28 19:51:54 +00004421*/
4422case OP_SeekScan: {
drhf761d932020-09-29 01:48:46 +00004423 VdbeCursor *pC;
4424 int res;
drhdeaa6102020-10-01 15:46:21 +00004425 int nStep;
drhf761d932020-09-29 01:48:46 +00004426 UnpackedRecord r;
4427
drh68cf0ac2020-09-28 19:51:54 +00004428 assert( pOp[1].opcode==OP_SeekGE );
drh04e70ce2020-10-02 11:55:07 +00004429
4430 /* pOp->p2 points to the first instruction past the OP_IdxGT that
4431 ** follows the OP_SeekGE. */
4432 assert( pOp->p2>=(int)(pOp-aOp)+2 );
drh3c48ee92021-03-20 01:00:26 +00004433 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
4434 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
drh04e70ce2020-10-02 11:55:07 +00004435 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
4436 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
4437 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
drh04e70ce2020-10-02 11:55:07 +00004438
drh68cf0ac2020-09-28 19:51:54 +00004439 assert( pOp->p1>0 );
drhf761d932020-09-29 01:48:46 +00004440 pC = p->apCsr[pOp[1].p1];
4441 assert( pC!=0 );
4442 assert( pC->eCurType==CURTYPE_BTREE );
4443 assert( !pC->isTable );
drha54e1b12020-09-29 23:52:25 +00004444 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
drhf761d932020-09-29 01:48:46 +00004445#ifdef SQLITE_DEBUG
4446 if( db->flags&SQLITE_VdbeTrace ){
drha54e1b12020-09-29 23:52:25 +00004447 printf("... cursor not valid - fall through\n");
drhf761d932020-09-29 01:48:46 +00004448 }
4449#endif
4450 break;
4451 }
drhdeaa6102020-10-01 15:46:21 +00004452 nStep = pOp->p1;
4453 assert( nStep>=1 );
drhf761d932020-09-29 01:48:46 +00004454 r.pKeyInfo = pC->pKeyInfo;
4455 r.nField = (u16)pOp[1].p4.i;
4456 r.default_rc = 0;
4457 r.aMem = &aMem[pOp[1].p3];
4458#ifdef SQLITE_DEBUG
4459 {
4460 int i;
4461 for(i=0; i<r.nField; i++){
4462 assert( memIsValid(&r.aMem[i]) );
4463 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
4464 }
4465 }
4466#endif
4467 res = 0; /* Not needed. Only used to silence a warning. */
4468 while(1){
4469 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
4470 if( rc ) goto abort_due_to_error;
4471 if( res>0 ){
drh0b2949c2020-09-29 20:22:19 +00004472 seekscan_search_fail:
drhf761d932020-09-29 01:48:46 +00004473#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004474 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004475 printf("... %d steps and then skip\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004476 }
drhf761d932020-09-29 01:48:46 +00004477#endif
drh0b2949c2020-09-29 20:22:19 +00004478 VdbeBranchTaken(1,3);
drhf287d002020-09-30 00:10:22 +00004479 pOp++;
drhf761d932020-09-29 01:48:46 +00004480 goto jump_to_p2;
4481 }
4482 if( res==0 ){
4483#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004484 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004485 printf("... %d steps and then success\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004486 }
drhf761d932020-09-29 01:48:46 +00004487#endif
drh0b2949c2020-09-29 20:22:19 +00004488 VdbeBranchTaken(2,3);
drh04e70ce2020-10-02 11:55:07 +00004489 goto jump_to_p2;
drhf761d932020-09-29 01:48:46 +00004490 break;
4491 }
drhdeaa6102020-10-01 15:46:21 +00004492 if( nStep<=0 ){
drh0b2949c2020-09-29 20:22:19 +00004493#ifdef SQLITE_DEBUG
4494 if( db->flags&SQLITE_VdbeTrace ){
4495 printf("... fall through after %d steps\n", pOp->p1);
4496 }
4497#endif
4498 VdbeBranchTaken(0,3);
4499 break;
4500 }
drhdeaa6102020-10-01 15:46:21 +00004501 nStep--;
drhf761d932020-09-29 01:48:46 +00004502 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
drh0b2949c2020-09-29 20:22:19 +00004503 if( rc ){
4504 if( rc==SQLITE_DONE ){
4505 rc = SQLITE_OK;
4506 goto seekscan_search_fail;
4507 }else{
4508 goto abort_due_to_error;
4509 }
4510 }
drhf761d932020-09-29 01:48:46 +00004511 }
drh0b2949c2020-09-29 20:22:19 +00004512
drhf761d932020-09-29 01:48:46 +00004513 break;
drh68cf0ac2020-09-28 19:51:54 +00004514}
4515
4516
drhfa17e132020-09-01 01:52:03 +00004517/* Opcode: SeekHit P1 P2 P3 * *
4518** Synopsis: set P2<=seekHit<=P3
drh8c2b6d72018-06-05 20:45:20 +00004519**
drhfa17e132020-09-01 01:52:03 +00004520** Increase or decrease the seekHit value for cursor P1, if necessary,
4521** so that it is no less than P2 and no greater than P3.
drh8c2b6d72018-06-05 20:45:20 +00004522**
drhfa17e132020-09-01 01:52:03 +00004523** The seekHit integer represents the maximum of terms in an index for which
4524** there is known to be at least one match. If the seekHit value is smaller
4525** than the total number of equality terms in an index lookup, then the
4526** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4527** early, thus saving work. This is part of the IN-early-out optimization.
4528**
4529** P1 must be a valid b-tree cursor.
drh8c2b6d72018-06-05 20:45:20 +00004530*/
4531case OP_SeekHit: {
4532 VdbeCursor *pC;
4533 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4534 pC = p->apCsr[pOp->p1];
4535 assert( pC!=0 );
drhfa17e132020-09-01 01:52:03 +00004536 assert( pOp->p3>=pOp->p2 );
4537 if( pC->seekHit<pOp->p2 ){
4538 pC->seekHit = pOp->p2;
4539 }else if( pC->seekHit>pOp->p3 ){
4540 pC->seekHit = pOp->p3;
4541 }
drh8c2b6d72018-06-05 20:45:20 +00004542 break;
4543}
4544
dan74ebaad2020-01-04 16:55:57 +00004545/* Opcode: IfNotOpen P1 P2 * * *
4546** Synopsis: if( !csr[P1] ) goto P2
4547**
4548** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
4549*/
4550case OP_IfNotOpen: { /* jump */
4551 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh56ea69b2020-01-04 18:33:20 +00004552 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
dan74ebaad2020-01-04 16:55:57 +00004553 if( !p->apCsr[pOp->p1] ){
4554 goto jump_to_p2_and_check_for_interrupt;
4555 }
4556 break;
4557}
4558
drh8cff69d2009-11-12 19:59:44 +00004559/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004560** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004561**
drh8cff69d2009-11-12 19:59:44 +00004562** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4563** P4>0 then register P3 is the first of P4 registers that form an unpacked
4564** record.
4565**
4566** Cursor P1 is on an index btree. If the record identified by P3 and P4
4567** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004568** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004569**
drhcefc87f2014-08-01 01:40:33 +00004570** This operation leaves the cursor in a state where it can be
4571** advanced in the forward direction. The Next instruction will work,
4572** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004573**
drh6f225d02013-10-26 13:36:51 +00004574** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004575*/
drh8cff69d2009-11-12 19:59:44 +00004576/* Opcode: NotFound 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 not the prefix of any entry in P1 then a jump is made to P2. If P1
4585** does contain an entry whose prefix matches the P3/P4 record then control
4586** falls through to the next instruction and P1 is left pointing at the
4587** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004588**
drh8af3f772014-07-25 18:01:06 +00004589** This operation leaves the cursor in a state where it cannot be
4590** advanced in either direction. In other words, the Next and Prev
4591** opcodes do not work after this operation.
4592**
drh8c2b6d72018-06-05 20:45:20 +00004593** See also: Found, NotExists, NoConflict, IfNoHope
4594*/
4595/* Opcode: IfNoHope P1 P2 P3 P4 *
4596** Synopsis: key=r[P3@P4]
4597**
4598** Register P3 is the first of P4 registers that form an unpacked
drhfa17e132020-09-01 01:52:03 +00004599** record. Cursor P1 is an index btree. P2 is a jump destination.
4600** In other words, the operands to this opcode are the same as the
4601** operands to OP_NotFound and OP_IdxGT.
drh8c2b6d72018-06-05 20:45:20 +00004602**
drhfa17e132020-09-01 01:52:03 +00004603** This opcode is an optimization attempt only. If this opcode always
4604** falls through, the correct answer is still obtained, but extra works
4605** is performed.
drh8c2b6d72018-06-05 20:45:20 +00004606**
drhfa17e132020-09-01 01:52:03 +00004607** A value of N in the seekHit flag of cursor P1 means that there exists
4608** a key P3:N that will match some record in the index. We want to know
4609** if it is possible for a record P3:P4 to match some record in the
4610** index. If it is not possible, we can skips some work. So if seekHit
4611** is less than P4, attempt to find out if a match is possible by running
4612** OP_NotFound.
drh8c2b6d72018-06-05 20:45:20 +00004613**
4614** This opcode is used in IN clause processing for a multi-column key.
4615** If an IN clause is attached to an element of the key other than the
4616** left-most element, and if there are no matches on the most recent
4617** seek over the whole key, then it might be that one of the key element
4618** to the left is prohibiting a match, and hence there is "no hope" of
4619** any match regardless of how many IN clause elements are checked.
4620** In such a case, we abandon the IN clause search early, using this
4621** opcode. The opcode name comes from the fact that the
4622** jump is taken if there is "no hope" of achieving a match.
4623**
4624** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004625*/
drh6f225d02013-10-26 13:36:51 +00004626/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004627** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004628**
4629** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4630** P4>0 then register P3 is the first of P4 registers that form an unpacked
4631** record.
4632**
4633** Cursor P1 is on an index btree. If the record identified by P3 and P4
4634** contains any NULL value, jump immediately to P2. If all terms of the
4635** record are not-NULL then a check is done to determine if any row in the
4636** P1 index btree has a matching key prefix. If there are no matches, jump
4637** immediately to P2. If there is a match, fall through and leave the P1
4638** cursor pointing to the matching row.
4639**
4640** This opcode is similar to OP_NotFound with the exceptions that the
4641** branch is always taken if any part of the search key input is NULL.
4642**
drh8af3f772014-07-25 18:01:06 +00004643** This operation leaves the cursor in a state where it cannot be
4644** advanced in either direction. In other words, the Next and Prev
4645** opcodes do not work after this operation.
4646**
drh6f225d02013-10-26 13:36:51 +00004647** See also: NotFound, Found, NotExists
4648*/
drh8c2b6d72018-06-05 20:45:20 +00004649case OP_IfNoHope: { /* jump, in3 */
4650 VdbeCursor *pC;
4651 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4652 pC = p->apCsr[pOp->p1];
4653 assert( pC!=0 );
drhfa17e132020-09-01 01:52:03 +00004654 if( pC->seekHit>=pOp->p4.i ) break;
drh8c2b6d72018-06-05 20:45:20 +00004655 /* Fall through into OP_NotFound */
drh08b92082020-08-10 14:18:00 +00004656 /* no break */ deliberate_fall_through
drh8c2b6d72018-06-05 20:45:20 +00004657}
drh6f225d02013-10-26 13:36:51 +00004658case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004659case OP_NotFound: /* jump, in3 */
4660case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004661 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004662 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004663 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004664 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004665 int res;
drha582b012016-12-21 19:45:54 +00004666 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004667 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004668 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004669
dan0ff297e2009-09-25 17:03:14 +00004670#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004671 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004672#endif
4673
drhaa736092009-06-22 00:55:30 +00004674 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004675 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004676 pC = p->apCsr[pOp->p1];
4677 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004678#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004679 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004680#endif
drh3c657212009-11-17 23:59:58 +00004681 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004682 assert( pC->eCurType==CURTYPE_BTREE );
4683 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004684 assert( pC->isTable==0 );
4685 if( pOp->p4.i>0 ){
4686 r.pKeyInfo = pC->pKeyInfo;
4687 r.nField = (u16)pOp->p4.i;
4688 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004689#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004690 for(ii=0; ii<r.nField; ii++){
4691 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004692 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004693 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004694 }
drh8aaf7bc2016-09-20 01:19:18 +00004695#endif
drh3da046d2013-11-11 03:24:11 +00004696 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004697 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004698 }else{
drhe46515b2017-05-19 22:51:00 +00004699 assert( pIn3->flags & MEM_Blob );
4700 rc = ExpandBlob(pIn3);
4701 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4702 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004703 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004704 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004705 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004706 }
dan1fed5da2014-02-25 21:01:25 +00004707 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004708 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004709 if( pOp->opcode==OP_NoConflict ){
4710 /* For the OP_NoConflict opcode, take the jump if any of the
4711 ** input fields are NULL, since any key with a NULL will not
4712 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004713 for(ii=0; ii<pIdxKey->nField; ii++){
4714 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004715 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004716 break;
drh6f225d02013-10-26 13:36:51 +00004717 }
4718 }
drh5e00f6c2001-09-13 13:46:56 +00004719 }
drhc960dcb2015-11-20 19:22:01 +00004720 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004721 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004722 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004723 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004724 }
4725 pC->seekResult = res;
4726 alreadyExists = (res==0);
4727 pC->nullRow = 1-alreadyExists;
4728 pC->deferredMoveto = 0;
4729 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004730 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004731 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004732 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004733 }else{
drhf56fa462015-04-13 21:39:54 +00004734 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4735 if( takeJump || !alreadyExists ) goto jump_to_p2;
drhfa17e132020-09-01 01:52:03 +00004736 if( pOp->opcode==OP_IfNoHope ) pC->seekHit = pOp->p4.i;
drh5e00f6c2001-09-13 13:46:56 +00004737 }
drh5e00f6c2001-09-13 13:46:56 +00004738 break;
4739}
4740
drheeb95652016-05-26 20:56:38 +00004741/* Opcode: SeekRowid P1 P2 P3 * *
4742** Synopsis: intkey=r[P3]
4743**
4744** P1 is the index of a cursor open on an SQL table btree (with integer
4745** keys). If register P3 does not contain an integer or if P1 does not
4746** contain a record with rowid P3 then jump immediately to P2.
4747** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4748** a record with rowid P3 then
4749** leave the cursor pointing at that record and fall through to the next
4750** instruction.
4751**
4752** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4753** the P3 register must be guaranteed to contain an integer value. With this
4754** opcode, register P3 might not contain an integer.
4755**
4756** The OP_NotFound opcode performs the same operation on index btrees
4757** (with arbitrary multi-value keys).
4758**
4759** This opcode leaves the cursor in a state where it cannot be advanced
4760** in either direction. In other words, the Next and Prev opcodes will
4761** not work following this opcode.
4762**
4763** See also: Found, NotFound, NoConflict, SeekRowid
4764*/
drh9cbf3422008-01-17 16:22:13 +00004765/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004766** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004767**
drh261c02d2013-10-25 14:46:15 +00004768** P1 is the index of a cursor open on an SQL table btree (with integer
4769** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004770** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4771** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4772** leave the cursor pointing at that record and fall through to the next
4773** instruction.
drh6b125452002-01-28 15:53:03 +00004774**
drheeb95652016-05-26 20:56:38 +00004775** The OP_SeekRowid opcode performs the same operation but also allows the
4776** P3 register to contain a non-integer value, in which case the jump is
4777** always taken. This opcode requires that P3 always contain an integer.
4778**
drh261c02d2013-10-25 14:46:15 +00004779** The OP_NotFound opcode performs the same operation on index btrees
4780** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004781**
drh8af3f772014-07-25 18:01:06 +00004782** This opcode leaves the cursor in a state where it cannot be advanced
4783** in either direction. In other words, the Next and Prev opcodes will
4784** not work following this opcode.
4785**
drheeb95652016-05-26 20:56:38 +00004786** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004787*/
drheeb95652016-05-26 20:56:38 +00004788case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004789 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004790 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004791 int res;
4792 u64 iKey;
4793
drh3c657212009-11-17 23:59:58 +00004794 pIn3 = &aMem[pOp->p3];
drh3242c692019-05-04 01:29:13 +00004795 testcase( pIn3->flags & MEM_Int );
4796 testcase( pIn3->flags & MEM_IntReal );
drhb29ef5e2019-10-07 01:05:57 +00004797 testcase( pIn3->flags & MEM_Real );
4798 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
drh169f0772019-05-02 21:36:26 +00004799 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
drhb29ef5e2019-10-07 01:05:57 +00004800 /* If pIn3->u.i does not contain an integer, compute iKey as the
4801 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
4802 ** into an integer without loss of information. Take care to avoid
4803 ** changing the datatype of pIn3, however, as it is used by other
4804 ** parts of the prepared statement. */
4805 Mem x = pIn3[0];
4806 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
4807 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
4808 iKey = x.u.i;
4809 goto notExistsWithKey;
drheeb95652016-05-26 20:56:38 +00004810 }
4811 /* Fall through into OP_NotExists */
drh08b92082020-08-10 14:18:00 +00004812 /* no break */ deliberate_fall_through
drheeb95652016-05-26 20:56:38 +00004813case OP_NotExists: /* jump, in3 */
4814 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004815 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004816 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhb29ef5e2019-10-07 01:05:57 +00004817 iKey = pIn3->u.i;
4818notExistsWithKey:
drhaa736092009-06-22 00:55:30 +00004819 pC = p->apCsr[pOp->p1];
4820 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004821#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00004822 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004823#endif
drhaa736092009-06-22 00:55:30 +00004824 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004825 assert( pC->eCurType==CURTYPE_BTREE );
4826 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004827 assert( pCrsr!=0 );
4828 res = 0;
drh3da046d2013-11-11 03:24:11 +00004829 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004830 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004831 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004832 pC->nullRow = 0;
4833 pC->cacheStatus = CACHE_STALE;
4834 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004835 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004836 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004837 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004838 assert( rc==SQLITE_OK );
4839 if( pOp->p2==0 ){
4840 rc = SQLITE_CORRUPT_BKPT;
4841 }else{
4842 goto jump_to_p2;
4843 }
danc6157e12015-09-14 09:23:47 +00004844 }
drh9467abf2016-02-17 18:44:11 +00004845 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004846 break;
4847}
4848
drh4c583122008-01-04 22:01:03 +00004849/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004850** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004851**
drh4c583122008-01-04 22:01:03 +00004852** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004853** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004854** The sequence number on the cursor is incremented after this
4855** instruction.
drh4db38a72005-09-01 12:16:28 +00004856*/
drh27a348c2015-04-13 19:14:06 +00004857case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004858 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4859 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004860 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004861 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004862 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004863 break;
4864}
4865
4866
drh98757152008-01-09 23:04:12 +00004867/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004868** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004869**
drhf0863fe2005-06-12 21:35:51 +00004870** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004871** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004872** table that cursor P1 points to. The new record number is written
4873** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004874**
dan76d462e2009-08-30 11:42:51 +00004875** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4876** the largest previously generated record number. No new record numbers are
4877** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004878** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004879** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004880** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004881*/
drh27a348c2015-04-13 19:14:06 +00004882case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004883 i64 v; /* The new rowid */
4884 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4885 int res; /* Result of an sqlite3BtreeLast() */
4886 int cnt; /* Counter to limit the number of searches */
mistachkind6665c52021-01-18 19:28:56 +00004887#ifndef SQLITE_OMIT_AUTOINCREMENT
drhaa736092009-06-22 00:55:30 +00004888 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004889 VdbeFrame *pFrame; /* Root frame of VDBE */
mistachkind6665c52021-01-18 19:28:56 +00004890#endif
drh856c1032009-06-02 15:21:42 +00004891
drh856c1032009-06-02 15:21:42 +00004892 v = 0;
4893 res = 0;
drh27a348c2015-04-13 19:14:06 +00004894 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004895 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4896 pC = p->apCsr[pOp->p1];
4897 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004898 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004899 assert( pC->eCurType==CURTYPE_BTREE );
4900 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004901 {
drh5cf8e8c2002-02-19 22:42:05 +00004902 /* The next rowid or record number (different terms for the same
4903 ** thing) is obtained in a two-step algorithm.
4904 **
4905 ** First we attempt to find the largest existing rowid and add one
4906 ** to that. But if the largest existing rowid is already the maximum
4907 ** positive integer, we have to fall through to the second
4908 ** probabilistic algorithm
4909 **
4910 ** The second algorithm is to select a rowid at random and see if
4911 ** it already exists in the table. If it does not exist, we have
4912 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004913 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004914 */
drhaa736092009-06-22 00:55:30 +00004915 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004916
drh75f86a42005-02-17 00:03:06 +00004917#ifdef SQLITE_32BIT_ROWID
4918# define MAX_ROWID 0x7fffffff
4919#else
drhfe2093d2005-01-20 22:48:47 +00004920 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4921 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4922 ** to provide the constant while making all compilers happy.
4923 */
danielk197764202cf2008-11-17 15:31:47 +00004924# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004925#endif
drhfe2093d2005-01-20 22:48:47 +00004926
drh5cf8e8c2002-02-19 22:42:05 +00004927 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004928 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004929 if( rc!=SQLITE_OK ){
4930 goto abort_due_to_error;
4931 }
4932 if( res ){
4933 v = 1; /* IMP: R-61914-48074 */
4934 }else{
drhc960dcb2015-11-20 19:22:01 +00004935 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004936 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004937 if( v>=MAX_ROWID ){
4938 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004939 }else{
drhe0670b62014-02-12 21:31:12 +00004940 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004941 }
drh3fc190c2001-09-14 03:24:23 +00004942 }
drhe0670b62014-02-12 21:31:12 +00004943 }
drh205f48e2004-11-05 00:43:11 +00004944
4945#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004946 if( pOp->p3 ){
4947 /* Assert that P3 is a valid memory cell. */
4948 assert( pOp->p3>0 );
4949 if( p->pFrame ){
4950 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004951 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004952 assert( pOp->p3<=pFrame->nMem );
4953 pMem = &pFrame->aMem[pOp->p3];
4954 }else{
4955 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004956 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004957 pMem = &aMem[pOp->p3];
4958 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004959 }
drhe0670b62014-02-12 21:31:12 +00004960 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004961
drhe0670b62014-02-12 21:31:12 +00004962 REGISTER_TRACE(pOp->p3, pMem);
4963 sqlite3VdbeMemIntegerify(pMem);
4964 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4965 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004966 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004967 goto abort_due_to_error;
4968 }
4969 if( v<pMem->u.i+1 ){
4970 v = pMem->u.i + 1;
4971 }
4972 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004973 }
drhe0670b62014-02-12 21:31:12 +00004974#endif
drh5cf8e8c2002-02-19 22:42:05 +00004975 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004976 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004977 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004978 ** engine starts picking positive candidate ROWIDs at random until
4979 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004980 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4981 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004982 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004983 do{
4984 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004985 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004986 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004987 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004988 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004989 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004990 if( rc ) goto abort_due_to_error;
4991 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004992 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004993 goto abort_due_to_error;
4994 }
drh748a52c2010-09-01 11:50:08 +00004995 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004996 }
drha11846b2004-01-07 18:52:56 +00004997 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004998 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004999 }
drh4c583122008-01-04 22:01:03 +00005000 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005001 break;
5002}
5003
danielk19771f4aa332008-01-03 09:51:55 +00005004/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005005** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005006**
jplyon5a564222003-06-02 06:15:58 +00005007** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00005008** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00005009** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00005010** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00005011** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00005012**
danielk19771f4aa332008-01-03 09:51:55 +00005013** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5014** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00005015** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00005016** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00005017**
drheaf6ae22016-11-09 20:14:34 +00005018** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5019** run faster by avoiding an unnecessary seek on cursor P1. However,
5020** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5021** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00005022**
5023** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5024** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5025** is part of an INSERT operation. The difference is only important to
5026** the update hook.
5027**
dan319eeb72011-03-19 08:38:50 +00005028** Parameter P4 may point to a Table structure, or may be NULL. If it is
5029** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5030** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00005031**
drh93aed5a2008-01-16 17:46:38 +00005032** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5033** allocated, then ownership of P2 is transferred to the pseudo-cursor
5034** and register P2 becomes ephemeral. If the cursor is changed, the
5035** value of register P2 will then change. Make sure this does not
5036** cause any problems.)
5037**
drhf0863fe2005-06-12 21:35:51 +00005038** This instruction only works on tables. The equivalent instruction
5039** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00005040*/
drh50ef6712019-02-22 23:29:56 +00005041case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00005042 Mem *pData; /* MEM cell holding data for the record to be inserted */
5043 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00005044 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00005045 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5046 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00005047 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00005048 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00005049
drha6c2ed92009-11-14 23:22:23 +00005050 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00005051 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00005052 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00005053 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00005054 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005055 assert( pC->eCurType==CURTYPE_BTREE );
drhbe3da242019-12-29 00:52:41 +00005056 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005057 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00005058 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00005059 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00005060 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00005061 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00005062
drh50ef6712019-02-22 23:29:56 +00005063 pKey = &aMem[pOp->p3];
5064 assert( pKey->flags & MEM_Int );
5065 assert( memIsValid(pKey) );
5066 REGISTER_TRACE(pOp->p3, pKey);
5067 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00005068
drh9b1c62d2011-03-30 21:04:43 +00005069 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005070 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00005071 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005072 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00005073 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00005074 }else{
drh4ec6f3a2018-01-12 19:33:18 +00005075 pTab = 0;
drh74c33022016-03-30 12:56:55 +00005076 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00005077 }
5078
drh9b1c62d2011-03-30 21:04:43 +00005079#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005080 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00005081 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00005082 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
5083 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
5084 }
drh4ec6f3a2018-01-12 19:33:18 +00005085 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
5086 /* Prevent post-update hook from running in cases when it should not */
5087 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00005088 }
dan46c47d42011-03-01 18:42:07 +00005089 }
dancb9a3642017-01-30 19:44:53 +00005090 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00005091#endif
dan46c47d42011-03-01 18:42:07 +00005092
drha05a7222008-01-19 03:35:58 +00005093 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00005094 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
drh32881be2020-11-17 21:26:13 +00005095 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
dan21cd29a2017-10-23 16:03:54 +00005096 x.pData = pData->z;
5097 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00005098 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
5099 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00005100 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00005101 }else{
drh8eeb4462016-05-21 20:03:42 +00005102 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00005103 }
drh8eeb4462016-05-21 20:03:42 +00005104 x.pKey = 0;
5105 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00005106 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
5107 seekResult
drh3e9ca092009-09-08 01:14:48 +00005108 );
drha05a7222008-01-19 03:35:58 +00005109 pC->deferredMoveto = 0;
5110 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00005111
drha05a7222008-01-19 03:35:58 +00005112 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00005113 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00005114 if( pTab ){
5115 assert( db->xUpdateCallback!=0 );
5116 assert( pTab->aCol!=0 );
5117 db->xUpdateCallback(db->pUpdateArg,
5118 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
5119 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00005120 }
drh5e00f6c2001-09-13 13:46:56 +00005121 break;
5122}
5123
dan7aae7352020-12-10 18:06:24 +00005124/* Opcode: RowCell P1 P2 P3 * *
dand2ffc972020-12-10 19:20:15 +00005125**
5126** P1 and P2 are both open cursors. Both must be opened on the same type
5127** of table - intkey or index. This opcode is used as part of copying
5128** the current row from P2 into P1. If the cursors are opened on intkey
5129** tables, register P3 contains the rowid to use with the new record in
5130** P1. If they are opened on index tables, P3 is not used.
5131**
5132** This opcode must be followed by either an Insert or InsertIdx opcode
5133** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
dan036e0672020-12-08 20:19:07 +00005134*/
dan7aae7352020-12-10 18:06:24 +00005135case OP_RowCell: {
dan036e0672020-12-08 20:19:07 +00005136 VdbeCursor *pDest; /* Cursor to write to */
5137 VdbeCursor *pSrc; /* Cursor to read from */
5138 i64 iKey; /* Rowid value to insert with */
dan7aae7352020-12-10 18:06:24 +00005139 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
drha06eafc2020-12-29 15:06:26 +00005140 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
5141 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
dand2ffc972020-12-10 19:20:15 +00005142 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
dan036e0672020-12-08 20:19:07 +00005143 pDest = p->apCsr[pOp->p1];
5144 pSrc = p->apCsr[pOp->p2];
dancd1b2d02020-12-09 20:33:51 +00005145 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
dan7aae7352020-12-10 18:06:24 +00005146 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
dan036e0672020-12-08 20:19:07 +00005147 if( rc!=SQLITE_OK ) goto abort_due_to_error;
5148 break;
dan7aae7352020-12-10 18:06:24 +00005149};
dan036e0672020-12-08 20:19:07 +00005150
dan438b8812015-09-15 15:55:15 +00005151/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005152**
drh5edc3122001-09-13 21:53:09 +00005153** Delete the record at which the P1 cursor is currently pointing.
5154**
drhe807bdb2016-01-21 17:06:33 +00005155** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5156** the cursor will be left pointing at either the next or the previous
5157** record in the table. If it is left pointing at the next record, then
5158** the next Next instruction will be a no-op. As a result, in this case
5159** it is ok to delete a record from within a Next loop. If
5160** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5161** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00005162**
drhdef19e32016-01-27 16:26:25 +00005163** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5164** delete one of several associated with deleting a table row and all its
5165** associated index entries. Exactly one of those deletes is the "primary"
5166** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5167** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00005168**
5169** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5170** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00005171**
drh91fd4d42008-01-19 20:11:25 +00005172** P1 must not be pseudo-table. It has to be a real table with
5173** multiple rows.
5174**
drh5e769a52016-09-28 16:05:53 +00005175** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00005176** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5177** have been positioned using OP_NotFound prior to invoking this opcode in
5178** this case. Specifically, if one is configured, the pre-update hook is
5179** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5180** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00005181**
5182** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5183** of the memory cell that contains the value that the rowid of the row will
5184** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00005185*/
drh9cbf3422008-01-17 16:22:13 +00005186case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00005187 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00005188 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00005189 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00005190 int opflags;
drh91fd4d42008-01-19 20:11:25 +00005191
dan46c47d42011-03-01 18:42:07 +00005192 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00005193 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5194 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005195 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005196 assert( pC->eCurType==CURTYPE_BTREE );
5197 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00005198 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00005199 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00005200
drhb53a5a92014-10-12 22:37:22 +00005201#ifdef SQLITE_DEBUG
drh6b559f32020-01-02 19:50:50 +00005202 if( pOp->p4type==P4_TABLE
5203 && HasRowid(pOp->p4.pTab)
5204 && pOp->p5==0
5205 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
5206 ){
dan438b8812015-09-15 15:55:15 +00005207 /* If p5 is zero, the seek operation that positioned the cursor prior to
5208 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5209 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00005210 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan0971ef42019-05-16 20:13:32 +00005211 assert( CORRUPT_DB || pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00005212 }
5213#endif
drh91fd4d42008-01-19 20:11:25 +00005214
dan438b8812015-09-15 15:55:15 +00005215 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5216 ** the name of the db to pass as to it. Also set local pTab to a copy
5217 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5218 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5219 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00005220 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005221 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00005222 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00005223 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005224 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00005225 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00005226 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00005227 }
drh74c33022016-03-30 12:56:55 +00005228 }else{
5229 zDb = 0; /* Not needed. Silence a compiler warning. */
5230 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00005231 }
dan46c47d42011-03-01 18:42:07 +00005232
drh9b1c62d2011-03-30 21:04:43 +00005233#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005234 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00005235 if( db->xPreUpdateCallback && pOp->p4.pTab ){
5236 assert( !(opflags & OPFLAG_ISUPDATE)
5237 || HasRowid(pTab)==0
5238 || (aMem[pOp->p3].flags & MEM_Int)
5239 );
dan46c47d42011-03-01 18:42:07 +00005240 sqlite3VdbePreUpdateHook(p, pC,
5241 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00005242 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00005243 pOp->p3
dan46c47d42011-03-01 18:42:07 +00005244 );
5245 }
dan46c47d42011-03-01 18:42:07 +00005246 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00005247#endif
drhb53a5a92014-10-12 22:37:22 +00005248
drhdef19e32016-01-27 16:26:25 +00005249 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5250 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00005251 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00005252 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00005253
5254#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00005255 if( p->pFrame==0 ){
5256 if( pC->isEphemeral==0
5257 && (pOp->p5 & OPFLAG_AUXDELETE)==0
5258 && (pC->wrFlag & OPFLAG_FORDELETE)==0
5259 ){
5260 nExtraDelete++;
5261 }
5262 if( pOp->p2 & OPFLAG_NCHANGE ){
5263 nExtraDelete--;
5264 }
drhb89aeb62016-01-27 15:49:32 +00005265 }
5266#endif
5267
drhc960dcb2015-11-20 19:22:01 +00005268 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00005269 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005270 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00005271 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00005272
drh91fd4d42008-01-19 20:11:25 +00005273 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00005274 if( opflags & OPFLAG_NCHANGE ){
5275 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00005276 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00005277 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00005278 pC->movetoTarget);
5279 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00005280 }
drh5e00f6c2001-09-13 13:46:56 +00005281 }
dan438b8812015-09-15 15:55:15 +00005282
rdcb0c374f2004-02-20 22:53:38 +00005283 break;
5284}
drhb7f1d9a2009-09-08 02:27:58 +00005285/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00005286**
drhb7f1d9a2009-09-08 02:27:58 +00005287** The value of the change counter is copied to the database handle
5288** change counter (returned by subsequent calls to sqlite3_changes()).
5289** Then the VMs internal change counter resets to 0.
5290** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00005291*/
drh9cbf3422008-01-17 16:22:13 +00005292case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00005293 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00005294 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00005295 break;
5296}
5297
drh1153c7b2013-11-01 22:02:56 +00005298/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00005299** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00005300**
drh1153c7b2013-11-01 22:02:56 +00005301** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00005302** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00005303** the sorter cursor currently points to. Only the first P4 fields
5304** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00005305**
5306** If either P3 or the sorter contains a NULL in one of their significant
5307** fields (not counting the P4 fields at the end which are ignored) then
5308** the comparison is assumed to be equal.
5309**
5310** Fall through to next instruction if the two records compare equal to
5311** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00005312*/
5313case OP_SorterCompare: {
5314 VdbeCursor *pC;
5315 int res;
drhac502322014-07-30 13:56:48 +00005316 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00005317
5318 pC = p->apCsr[pOp->p1];
5319 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00005320 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00005321 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00005322 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00005323 res = 0;
drhac502322014-07-30 13:56:48 +00005324 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00005325 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00005326 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005327 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00005328 break;
5329};
5330
drh6cf4a7d2014-10-13 13:00:58 +00005331/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005332** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00005333**
5334** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00005335** Then clear the column header cache on cursor P3.
5336**
5337** This opcode is normally use to move a record out of the sorter and into
5338** a register that is the source for a pseudo-table cursor created using
5339** OpenPseudo. That pseudo-table cursor is the one that is identified by
5340** parameter P3. Clearing the P3 column cache as part of this opcode saves
5341** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00005342*/
5343case OP_SorterData: {
5344 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00005345
dan5134d132011-09-02 10:31:11 +00005346 pOut = &aMem[pOp->p2];
5347 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00005348 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00005349 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00005350 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00005351 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00005352 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00005353 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00005354 break;
5355}
5356
drhe7b554d2017-01-09 15:44:25 +00005357/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005358** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00005359**
drh9057fc72016-11-25 19:32:32 +00005360** Write into register P2 the complete row content for the row at
5361** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00005362** There is no interpretation of the data.
5363** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00005364** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00005365**
drh9057fc72016-11-25 19:32:32 +00005366** If cursor P1 is an index, then the content is the key of the row.
5367** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00005368**
drhde4fcfd2008-01-19 23:50:26 +00005369** If the P1 cursor must be pointing to a valid row (not a NULL row)
5370** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00005371**
drh8cdafc32018-05-31 19:00:20 +00005372** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00005373** into the database page. That means that the content of the output
5374** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00005375** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00005376** position in order that they can write to the same table. If P3==0
5377** then a copy of the data is made into memory. P3!=0 is faster, but
5378** P3==0 is safer.
5379**
5380** If P3!=0 then the content of the P2 register is unsuitable for use
5381** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00005382** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00005383** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00005384*/
danielk1977a7a8e142008-02-13 18:25:27 +00005385case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00005386 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00005387 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00005388 u32 n;
drh70ce3f02003-04-15 19:22:22 +00005389
drhe7b554d2017-01-09 15:44:25 +00005390 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00005391
drh653b82a2009-06-22 11:10:47 +00005392 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5393 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00005394 assert( pC!=0 );
5395 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00005396 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00005397 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00005398 assert( pC->uc.pCursor!=0 );
5399 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00005400
drh9057fc72016-11-25 19:32:32 +00005401 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00005402 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5403 ** that might invalidate the cursor.
5404 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00005405 ** would fail. Should this ever change (because of changes in the code
5406 ** generator) then the fix would be to insert a call to
5407 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00005408 */
5409 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00005410 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00005411
drha7c90c42016-06-04 20:37:10 +00005412 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00005413 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00005414 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00005415 }
drh722246e2014-10-07 23:02:24 +00005416 testcase( n==0 );
drh2a740062020-02-05 18:28:17 +00005417 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
drh9467abf2016-02-17 18:44:11 +00005418 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00005419 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00005420 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00005421 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00005422 break;
5423}
5424
drh2133d822008-01-03 18:44:59 +00005425/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005426** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005427**
drh2133d822008-01-03 18:44:59 +00005428** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00005429** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00005430**
5431** P1 can be either an ordinary table or a virtual table. There used to
5432** be a separate OP_VRowid opcode for use with virtual tables, but this
5433** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00005434*/
drh27a348c2015-04-13 19:14:06 +00005435case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00005436 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00005437 i64 v;
drh856c1032009-06-02 15:21:42 +00005438 sqlite3_vtab *pVtab;
5439 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00005440
drh27a348c2015-04-13 19:14:06 +00005441 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005442 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5443 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005444 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005445 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00005446 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00005447 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00005448 break;
5449 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00005450 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00005451#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00005452 }else if( pC->eCurType==CURTYPE_VTAB ){
5453 assert( pC->uc.pVCur!=0 );
5454 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00005455 pModule = pVtab->pModule;
5456 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00005457 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00005458 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00005459 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00005460#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00005461 }else{
drhc960dcb2015-11-20 19:22:01 +00005462 assert( pC->eCurType==CURTYPE_BTREE );
5463 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00005464 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00005465 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00005466 if( pC->nullRow ){
5467 pOut->flags = MEM_Null;
5468 break;
5469 }
drha7c90c42016-06-04 20:37:10 +00005470 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00005471 }
drh4c583122008-01-04 22:01:03 +00005472 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005473 break;
5474}
5475
drh9cbf3422008-01-17 16:22:13 +00005476/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005477**
5478** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005479** that occur while the cursor is on the null row will always
5480** write a NULL.
drh17f71932002-02-21 12:01:27 +00005481*/
drh9cbf3422008-01-17 16:22:13 +00005482case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005483 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005484
drh653b82a2009-06-22 11:10:47 +00005485 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5486 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005487 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00005488 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005489 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005490 if( pC->eCurType==CURTYPE_BTREE ){
5491 assert( pC->uc.pCursor!=0 );
5492 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005493 }
drhcf025a82018-06-07 18:01:21 +00005494#ifdef SQLITE_DEBUG
5495 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5496#endif
drh17f71932002-02-21 12:01:27 +00005497 break;
5498}
5499
drh86b40df2017-08-01 19:53:43 +00005500/* Opcode: SeekEnd P1 * * * *
5501**
5502** Position cursor P1 at the end of the btree for the purpose of
5503** appending a new entry onto the btree.
5504**
5505** It is assumed that the cursor is used only for appending and so
5506** if the cursor is valid, then the cursor must already be pointing
5507** at the end of the btree and so no changes are made to
5508** the cursor.
5509*/
5510/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005511**
drh8af3f772014-07-25 18:01:06 +00005512** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005513** will refer to the last entry in the database table or index.
5514** If the table or index is empty and P2>0, then jump immediately to P2.
5515** If P2 is 0 or if the table or index is not empty, fall through
5516** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005517**
5518** This opcode leaves the cursor configured to move in reverse order,
5519** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005520** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005521*/
drh86b40df2017-08-01 19:53:43 +00005522case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005523case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005524 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005525 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005526 int res;
drh9562b552002-02-19 15:00:07 +00005527
drh653b82a2009-06-22 11:10:47 +00005528 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5529 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005530 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005531 assert( pC->eCurType==CURTYPE_BTREE );
5532 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005533 res = 0;
drh3da046d2013-11-11 03:24:11 +00005534 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005535#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005536 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005537#endif
drh86b40df2017-08-01 19:53:43 +00005538 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005539 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005540 pC->seekResult = -1;
5541 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5542 break;
5543 }
5544 }
5545 rc = sqlite3BtreeLast(pCrsr, &res);
5546 pC->nullRow = (u8)res;
5547 pC->deferredMoveto = 0;
5548 pC->cacheStatus = CACHE_STALE;
5549 if( rc ) goto abort_due_to_error;
5550 if( pOp->p2>0 ){
5551 VdbeBranchTaken(res!=0,2);
5552 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005553 }
5554 break;
5555}
5556
drh5e98e832017-02-17 19:24:06 +00005557/* Opcode: IfSmaller P1 P2 P3 * *
5558**
5559** Estimate the number of rows in the table P1. Jump to P2 if that
5560** estimate is less than approximately 2**(0.1*P3).
5561*/
5562case OP_IfSmaller: { /* jump */
5563 VdbeCursor *pC;
5564 BtCursor *pCrsr;
5565 int res;
5566 i64 sz;
5567
5568 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5569 pC = p->apCsr[pOp->p1];
5570 assert( pC!=0 );
5571 pCrsr = pC->uc.pCursor;
5572 assert( pCrsr );
5573 rc = sqlite3BtreeFirst(pCrsr, &res);
5574 if( rc ) goto abort_due_to_error;
5575 if( res==0 ){
5576 sz = sqlite3BtreeRowCountEst(pCrsr);
5577 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5578 }
5579 VdbeBranchTaken(res!=0,2);
5580 if( res ) goto jump_to_p2;
5581 break;
5582}
5583
drh0342b1f2005-09-01 03:07:44 +00005584
drh6bd4dc62016-12-23 16:05:22 +00005585/* Opcode: SorterSort P1 P2 * * *
5586**
5587** After all records have been inserted into the Sorter object
5588** identified by P1, invoke this opcode to actually do the sorting.
5589** Jump to P2 if there are no records to be sorted.
5590**
5591** This opcode is an alias for OP_Sort and OP_Rewind that is used
5592** for Sorter objects.
5593*/
drh9cbf3422008-01-17 16:22:13 +00005594/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005595**
5596** This opcode does exactly the same thing as OP_Rewind except that
5597** it increments an undocumented global variable used for testing.
5598**
5599** Sorting is accomplished by writing records into a sorting index,
5600** then rewinding that index and playing it back from beginning to
5601** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5602** rewinding so that the global variable will be incremented and
5603** regression tests can determine whether or not the optimizer is
5604** correctly optimizing out sorts.
5605*/
drhc6aff302011-09-01 15:32:47 +00005606case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005607case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005608#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005609 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005610 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005611#endif
drh9b47ee32013-08-20 03:13:51 +00005612 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005613 /* Fall through into OP_Rewind */
drh08b92082020-08-10 14:18:00 +00005614 /* no break */ deliberate_fall_through
drh0342b1f2005-09-01 03:07:44 +00005615}
drh038ebf62019-03-29 15:21:22 +00005616/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005617**
drhf0863fe2005-06-12 21:35:51 +00005618** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005619** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005620** If the table or index is empty, jump immediately to P2.
5621** If the table or index is not empty, fall through to the following
5622** instruction.
drh8af3f772014-07-25 18:01:06 +00005623**
5624** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005625** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005626** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005627*/
drh9cbf3422008-01-17 16:22:13 +00005628case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005629 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005630 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005631 int res;
drh5e00f6c2001-09-13 13:46:56 +00005632
drh653b82a2009-06-22 11:10:47 +00005633 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00005634 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00005635 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005636 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005637 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005638 res = 1;
drh8af3f772014-07-25 18:01:06 +00005639#ifdef SQLITE_DEBUG
5640 pC->seekOp = OP_Rewind;
5641#endif
dan689ab892011-08-12 15:02:00 +00005642 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005643 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005644 }else{
drhc960dcb2015-11-20 19:22:01 +00005645 assert( pC->eCurType==CURTYPE_BTREE );
5646 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005647 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005648 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00005649 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005650 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005651 }
drh9467abf2016-02-17 18:44:11 +00005652 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005653 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005654 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005655 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005656 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005657 break;
5658}
5659
drh0fd61352014-02-07 02:29:45 +00005660/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005661**
5662** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005663** table or index. If there are no more key/value pairs then fall through
5664** to the following instruction. But if the cursor advance was successful,
5665** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005666**
drh5dad9a32014-07-25 18:37:42 +00005667** The Next opcode is only valid following an SeekGT, SeekGE, or
5668** OP_Rewind opcode used to position the cursor. Next is not allowed
5669** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005670**
drhf93cd942013-11-21 03:12:25 +00005671** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5672** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005673**
drhe39a7322014-02-03 14:04:11 +00005674** The P3 value is a hint to the btree implementation. If P3==1, that
5675** means P1 is an SQL index and that this instruction could have been
5676** omitted if that index had been unique. P3 is usually 0. P3 is
5677** always either 0 or 1.
5678**
dana205a482011-08-27 18:48:57 +00005679** P4 is always of type P4_ADVANCE. The function pointer points to
5680** sqlite3BtreeNext().
5681**
drhafc266a2010-03-31 17:47:44 +00005682** If P5 is positive and the jump is taken, then event counter
5683** number P5-1 in the prepared statement is incremented.
5684**
drhf1949b62018-06-07 17:32:59 +00005685** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005686*/
drh0fd61352014-02-07 02:29:45 +00005687/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005688**
5689** Back up cursor P1 so that it points to the previous key/data pair in its
5690** table or index. If there is no previous key/value pairs then fall through
5691** to the following instruction. But if the cursor backup was successful,
5692** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005693**
drh8af3f772014-07-25 18:01:06 +00005694**
drh5dad9a32014-07-25 18:37:42 +00005695** The Prev opcode is only valid following an SeekLT, SeekLE, or
5696** OP_Last opcode used to position the cursor. Prev is not allowed
5697** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005698**
drhf93cd942013-11-21 03:12:25 +00005699** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5700** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005701**
drhe39a7322014-02-03 14:04:11 +00005702** The P3 value is a hint to the btree implementation. If P3==1, that
5703** means P1 is an SQL index and that this instruction could have been
5704** omitted if that index had been unique. P3 is usually 0. P3 is
5705** always either 0 or 1.
5706**
dana205a482011-08-27 18:48:57 +00005707** P4 is always of type P4_ADVANCE. The function pointer points to
5708** sqlite3BtreePrevious().
5709**
drhafc266a2010-03-31 17:47:44 +00005710** If P5 is positive and the jump is taken, then event counter
5711** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005712*/
drh6bd4dc62016-12-23 16:05:22 +00005713/* Opcode: SorterNext P1 P2 * * P5
5714**
5715** This opcode works just like OP_Next except that P1 must be a
5716** sorter object for which the OP_SorterSort opcode has been
5717** invoked. This opcode advances the cursor to the next sorted
5718** record, or jumps to P2 if there are no more sorted records.
5719*/
drhf93cd942013-11-21 03:12:25 +00005720case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005721 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005722
drhf93cd942013-11-21 03:12:25 +00005723 pC = p->apCsr[pOp->p1];
5724 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005725 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005726 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005727case OP_Prev: /* jump */
5728case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005729 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005730 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005731 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005732 assert( pC!=0 );
5733 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005734 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005735 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5736 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005737
drhcf025a82018-06-07 18:01:21 +00005738 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005739 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005740 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005741 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drh790b37a2019-08-27 17:01:07 +00005742 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
5743 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
5744 || pC->seekOp==OP_IfNoHope);
drhf1949b62018-06-07 17:32:59 +00005745 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005746 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drh790b37a2019-08-27 17:01:07 +00005747 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
drhcf025a82018-06-07 18:01:21 +00005748 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005749
drh2ab792e2017-05-30 18:34:07 +00005750 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005751next_tail:
drha3460582008-07-11 21:02:53 +00005752 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005753 VdbeBranchTaken(rc==SQLITE_OK,2);
5754 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005755 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005756 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005757#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005758 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005759#endif
drhf56fa462015-04-13 21:39:54 +00005760 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005761 }
drh2ab792e2017-05-30 18:34:07 +00005762 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5763 rc = SQLITE_OK;
5764 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005765 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005766}
5767
drh9b4eaeb2016-11-09 00:10:33 +00005768/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005769** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005770**
drhef8662b2011-06-20 21:47:58 +00005771** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005772** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005773** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005774**
drhfb8c56f2016-11-09 01:19:25 +00005775** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005776** key of reg(P2). In that case, P3 is the index of the first register
5777** for the unpacked key. The availability of the unpacked key can sometimes
5778** be an optimization.
5779**
5780** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5781** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005782**
mistachkin21a919f2014-02-07 03:28:02 +00005783** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5784** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5785** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005786**
drheaf6ae22016-11-09 20:14:34 +00005787** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5788** run faster by avoiding an unnecessary seek on cursor P1. However,
5789** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5790** seeks on the cursor or if the most recent seek used a key equivalent
5791** to P2.
drh0fd61352014-02-07 02:29:45 +00005792**
drhf0863fe2005-06-12 21:35:51 +00005793** This instruction only works for indices. The equivalent instruction
5794** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005795*/
drh9cbf3422008-01-17 16:22:13 +00005796case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005797 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005798 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005799
drh653b82a2009-06-22 11:10:47 +00005800 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5801 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005802 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005803 assert( pC!=0 );
drhc879c4e2020-02-06 13:57:08 +00005804 assert( !isSorter(pC) );
drh3c657212009-11-17 23:59:58 +00005805 pIn2 = &aMem[pOp->p2];
dan7aae7352020-12-10 18:06:24 +00005806 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
drh6546af12013-11-04 15:23:25 +00005807 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc879c4e2020-02-06 13:57:08 +00005808 assert( pC->eCurType==CURTYPE_BTREE );
drh3da046d2013-11-11 03:24:11 +00005809 assert( pC->isTable==0 );
5810 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005811 if( rc ) goto abort_due_to_error;
drhc879c4e2020-02-06 13:57:08 +00005812 x.nKey = pIn2->n;
5813 x.pKey = pIn2->z;
5814 x.aMem = aMem + pOp->p3;
5815 x.nMem = (u16)pOp->p4.i;
5816 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00005817 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
drhc879c4e2020-02-06 13:57:08 +00005818 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5819 );
5820 assert( pC->deferredMoveto==0 );
5821 pC->cacheStatus = CACHE_STALE;
5822 if( rc) goto abort_due_to_error;
5823 break;
5824}
5825
5826/* Opcode: SorterInsert P1 P2 * * *
5827** Synopsis: key=r[P2]
5828**
5829** Register P2 holds an SQL index key made using the
5830** MakeRecord instructions. This opcode writes that key
5831** into the sorter P1. Data for the entry is nil.
5832*/
5833case OP_SorterInsert: { /* in2 */
5834 VdbeCursor *pC;
5835
5836 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5837 pC = p->apCsr[pOp->p1];
5838 sqlite3VdbeIncrWriteCounter(p, pC);
5839 assert( pC!=0 );
5840 assert( isSorter(pC) );
5841 pIn2 = &aMem[pOp->p2];
5842 assert( pIn2->flags & MEM_Blob );
5843 assert( pC->isTable==0 );
5844 rc = ExpandBlob(pIn2);
5845 if( rc ) goto abort_due_to_error;
5846 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh9467abf2016-02-17 18:44:11 +00005847 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005848 break;
5849}
5850
drh85bd3532020-05-05 18:42:49 +00005851/* Opcode: IdxDelete P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00005852** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005853**
drhe14006d2008-03-25 17:23:32 +00005854** The content of P3 registers starting at register P2 form
5855** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005856** index opened by cursor P1.
drh85bd3532020-05-05 18:42:49 +00005857**
5858** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
5859** if no matching index entry is found. This happens when running
5860** an UPDATE or DELETE statement and the index entry to be updated
5861** or deleted is not found. For some uses of IdxDelete
5862** (example: the EXCEPT operator) it does not matter that no matching
5863** entry is found. For those cases, P5 is zero.
drh5e00f6c2001-09-13 13:46:56 +00005864*/
drhe14006d2008-03-25 17:23:32 +00005865case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005866 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005867 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005868 int res;
5869 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005870
drhe14006d2008-03-25 17:23:32 +00005871 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005872 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005873 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5874 pC = p->apCsr[pOp->p1];
5875 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005876 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005877 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005878 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005879 assert( pCrsr!=0 );
drh3da046d2013-11-11 03:24:11 +00005880 r.pKeyInfo = pC->pKeyInfo;
5881 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005882 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005883 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005884 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005885 if( rc ) goto abort_due_to_error;
5886 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005887 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005888 if( rc ) goto abort_due_to_error;
drh85bd3532020-05-05 18:42:49 +00005889 }else if( pOp->p5 ){
drhe5ceaac2021-01-25 21:24:14 +00005890 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
drh85bd3532020-05-05 18:42:49 +00005891 goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005892 }
drh3da046d2013-11-11 03:24:11 +00005893 assert( pC->deferredMoveto==0 );
5894 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005895 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005896 break;
5897}
5898
drh170ad682017-06-02 15:44:22 +00005899/* Opcode: DeferredSeek P1 * P3 P4 *
5900** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005901**
5902** P1 is an open index cursor and P3 is a cursor on the corresponding
5903** table. This opcode does a deferred seek of the P3 table cursor
5904** to the row that corresponds to the current row of P1.
5905**
5906** This is a deferred seek. Nothing actually happens until
5907** the cursor is used to read a record. That way, if no reads
5908** occur, no unnecessary I/O happens.
5909**
5910** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005911** one entry for each column in the P3 table. If array entry a(i)
5912** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005913** equivalent to performing the deferred seek and then reading column i
5914** from P1. This information is stored in P3 and used to redirect
5915** reads against P3 over to P1, thus possibly avoiding the need to
5916** seek and read cursor P3.
5917*/
drh2133d822008-01-03 18:44:59 +00005918/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005919** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005920**
drh2133d822008-01-03 18:44:59 +00005921** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005922** the end of the index key pointed to by cursor P1. This integer should be
5923** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005924**
drh9437bd22009-02-01 00:29:56 +00005925** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005926*/
drh170ad682017-06-02 15:44:22 +00005927case OP_DeferredSeek:
5928case OP_IdxRowid: { /* out2 */
5929 VdbeCursor *pC; /* The P1 index cursor */
5930 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5931 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005932
drh653b82a2009-06-22 11:10:47 +00005933 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5934 pC = p->apCsr[pOp->p1];
5935 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005936 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005937 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005938 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005939 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005940 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5941
5942 /* The IdxRowid and Seek opcodes are combined because of the commonality
5943 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5944 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005945
5946 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005947 ** out from under the cursor. That will never happens for an IdxRowid
5948 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005949 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5950
drh3da046d2013-11-11 03:24:11 +00005951 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005952 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005953 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005954 if( rc!=SQLITE_OK ){
5955 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005956 }
drh170ad682017-06-02 15:44:22 +00005957 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005958 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5959 pTabCur = p->apCsr[pOp->p3];
5960 assert( pTabCur!=0 );
5961 assert( pTabCur->eCurType==CURTYPE_BTREE );
5962 assert( pTabCur->uc.pCursor!=0 );
5963 assert( pTabCur->isTable );
5964 pTabCur->nullRow = 0;
5965 pTabCur->movetoTarget = rowid;
5966 pTabCur->deferredMoveto = 1;
5967 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5968 pTabCur->aAltMap = pOp->p4.ai;
drhe44ac382021-03-18 13:19:41 +00005969 assert( !pC->isEphemeral );
5970 assert( !pTabCur->isEphemeral );
drh784c1b92016-01-30 16:59:56 +00005971 pTabCur->pAltCursor = pC;
5972 }else{
5973 pOut = out2Prerelease(p, pOp);
5974 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005975 }
5976 }else{
5977 assert( pOp->opcode==OP_IdxRowid );
5978 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005979 }
5980 break;
5981}
5982
drhbe3da242019-12-29 00:52:41 +00005983/* Opcode: FinishSeek P1 * * * *
5984**
5985** If cursor P1 was previously moved via OP_DeferredSeek, complete that
5986** seek operation now, without further delay. If the cursor seek has
5987** already occurred, this instruction is a no-op.
5988*/
5989case OP_FinishSeek: {
5990 VdbeCursor *pC; /* The P1 index cursor */
5991
5992 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5993 pC = p->apCsr[pOp->p1];
5994 if( pC->deferredMoveto ){
5995 rc = sqlite3VdbeFinishMoveto(pC);
5996 if( rc ) goto abort_due_to_error;
5997 }
5998 break;
5999}
6000
drhc51ceeb2020-08-31 12:29:03 +00006001/* Opcode: IdxGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006002** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00006003**
danielk197761dd5832008-04-18 11:31:12 +00006004** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006005** key that omits the PRIMARY KEY. Compare this key value against the index
6006** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6007** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00006008**
danielk197761dd5832008-04-18 11:31:12 +00006009** If the P1 index entry is greater than or equal to the key value
6010** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00006011*/
drhc51ceeb2020-08-31 12:29:03 +00006012/* Opcode: IdxGT P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006013** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00006014**
drh4a1d3652014-02-14 15:13:36 +00006015** The P4 register values beginning with P3 form an unpacked index
6016** key that omits the PRIMARY KEY. Compare this key value against the index
6017** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6018** fields at the end.
6019**
6020** If the P1 index entry is greater than the key value
6021** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00006022*/
drhc51ceeb2020-08-31 12:29:03 +00006023/* Opcode: IdxLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006024** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00006025**
danielk197761dd5832008-04-18 11:31:12 +00006026** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006027** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6028** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6029** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00006030**
danielk197761dd5832008-04-18 11:31:12 +00006031** If the P1 index entry is less than the key value then jump to P2.
6032** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00006033*/
drhc51ceeb2020-08-31 12:29:03 +00006034/* Opcode: IdxLE P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006035** Synopsis: key=r[P3@P4]
6036**
6037** The P4 register values beginning with P3 form an unpacked index
6038** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6039** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6040** ROWID on the P1 index.
6041**
6042** If the P1 index entry is less than or equal to the key value then jump
6043** to P2. Otherwise fall through to the next instruction.
6044*/
6045case OP_IdxLE: /* jump */
6046case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00006047case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00006048case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00006049 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00006050 int res;
6051 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00006052
drh653b82a2009-06-22 11:10:47 +00006053 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6054 pC = p->apCsr[pOp->p1];
6055 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00006056 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00006057 assert( pC->eCurType==CURTYPE_BTREE );
6058 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00006059 assert( pC->deferredMoveto==0 );
drh3da046d2013-11-11 03:24:11 +00006060 assert( pOp->p4type==P4_INT32 );
6061 r.pKeyInfo = pC->pKeyInfo;
6062 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00006063 if( pOp->opcode<OP_IdxLT ){
6064 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00006065 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00006066 }else{
drh4a1d3652014-02-14 15:13:36 +00006067 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00006068 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00006069 }
6070 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006071#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00006072 {
6073 int i;
6074 for(i=0; i<r.nField; i++){
6075 assert( memIsValid(&r.aMem[i]) );
6076 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
6077 }
6078 }
drh2b4ded92010-09-27 21:09:31 +00006079#endif
drhc40076a2020-09-29 16:05:09 +00006080
6081 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6082 {
6083 i64 nCellKey = 0;
6084 BtCursor *pCur;
6085 Mem m;
6086
6087 assert( pC->eCurType==CURTYPE_BTREE );
6088 pCur = pC->uc.pCursor;
6089 assert( sqlite3BtreeCursorIsValid(pCur) );
6090 nCellKey = sqlite3BtreePayloadSize(pCur);
6091 /* nCellKey will always be between 0 and 0xffffffff because of the way
6092 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6093 if( nCellKey<=0 || nCellKey>0x7fffffff ){
6094 rc = SQLITE_CORRUPT_BKPT;
6095 goto abort_due_to_error;
6096 }
6097 sqlite3VdbeMemInit(&m, db, 0);
6098 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
6099 if( rc ) goto abort_due_to_error;
6100 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
6101 sqlite3VdbeMemRelease(&m);
6102 }
6103 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6104
drh4a1d3652014-02-14 15:13:36 +00006105 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
6106 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
6107 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00006108 res = -res;
6109 }else{
drh4a1d3652014-02-14 15:13:36 +00006110 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00006111 res++;
6112 }
drh688852a2014-02-17 22:40:43 +00006113 VdbeBranchTaken(res>0,2);
drhc40076a2020-09-29 16:05:09 +00006114 assert( rc==SQLITE_OK );
drhf56fa462015-04-13 21:39:54 +00006115 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00006116 break;
6117}
6118
drh98757152008-01-09 23:04:12 +00006119/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00006120**
6121** Delete an entire database table or index whose root page in the database
6122** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00006123**
drh98757152008-01-09 23:04:12 +00006124** The table being destroyed is in the main database file if P3==0. If
6125** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00006126** that is used to store tables create using CREATE TEMPORARY TABLE.
6127**
drh205f48e2004-11-05 00:43:11 +00006128** If AUTOVACUUM is enabled then it is possible that another root page
6129** might be moved into the newly deleted root page in order to keep all
6130** root pages contiguous at the beginning of the database. The former
6131** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00006132** is stored in register P2. If no page movement was required (because the
6133** table being dropped was already the last one in the database) then a
6134** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6135** is stored in register P2.
6136**
6137** This opcode throws an error if there are any active reader VMs when
6138** it is invoked. This is done to avoid the difficulty associated with
6139** updating existing cursors when a root page is moved in an AUTOVACUUM
6140** database. This error is thrown even if the database is not an AUTOVACUUM
6141** db in order to avoid introducing an incompatibility between autovacuum
6142** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00006143**
drhb19a2bc2001-09-16 00:13:26 +00006144** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00006145*/
drh27a348c2015-04-13 19:14:06 +00006146case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00006147 int iMoved;
drh856c1032009-06-02 15:21:42 +00006148 int iDb;
drh3a949872012-09-18 13:20:13 +00006149
drh4031baf2018-05-28 17:31:20 +00006150 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00006151 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00006152 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00006153 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00006154 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00006155 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00006156 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00006157 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00006158 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00006159 }else{
drh856c1032009-06-02 15:21:42 +00006160 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00006161 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00006162 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00006163 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00006164 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00006165 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00006166 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00006167#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00006168 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00006169 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
6170 /* All OP_Destroy operations occur on the same btree */
6171 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
6172 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00006173 }
drh3765df42006-06-28 18:18:09 +00006174#endif
danielk1977a0bf2652004-11-04 14:30:04 +00006175 }
drh5e00f6c2001-09-13 13:46:56 +00006176 break;
6177}
6178
danielk1977c7af4842008-10-27 13:59:33 +00006179/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00006180**
6181** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00006182** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00006183** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00006184**
drhf57b3392001-10-08 13:22:32 +00006185** The table being clear is in the main database file if P2==0. If
6186** P2==1 then the table to be clear is in the auxiliary database file
6187** that is used to store tables create using CREATE TEMPORARY TABLE.
6188**
shanebe217792009-03-05 04:20:31 +00006189** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00006190** intkey table (an SQL table, not an index). In this case the row change
6191** count is incremented by the number of rows in the table being cleared.
6192** If P3 is greater than zero, then the value stored in register P3 is
6193** also incremented by the number of rows in the table being cleared.
6194**
drhb19a2bc2001-09-16 00:13:26 +00006195** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00006196*/
drh9cbf3422008-01-17 16:22:13 +00006197case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00006198 int nChange;
6199
drh4031baf2018-05-28 17:31:20 +00006200 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00006201 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00006202 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00006203 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00006204 rc = sqlite3BtreeClearTable(
drhabc38152020-07-22 13:38:04 +00006205 db->aDb[pOp->p2].pBt, (u32)pOp->p1, (pOp->p3 ? &nChange : 0)
danielk1977c7af4842008-10-27 13:59:33 +00006206 );
6207 if( pOp->p3 ){
6208 p->nChange += nChange;
6209 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00006210 assert( memIsValid(&aMem[pOp->p3]) );
6211 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00006212 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00006213 }
6214 }
drh9467abf2016-02-17 18:44:11 +00006215 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00006216 break;
6217}
6218
drh65ea12c2014-03-19 17:41:36 +00006219/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00006220**
drh65ea12c2014-03-19 17:41:36 +00006221** Delete all contents from the ephemeral table or sorter
6222** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00006223**
drh65ea12c2014-03-19 17:41:36 +00006224** This opcode only works for cursors used for sorting and
6225** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00006226*/
drh65ea12c2014-03-19 17:41:36 +00006227case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00006228 VdbeCursor *pC;
6229
6230 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6231 pC = p->apCsr[pOp->p1];
6232 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006233 if( isSorter(pC) ){
6234 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00006235 }else{
drhc960dcb2015-11-20 19:22:01 +00006236 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00006237 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00006238 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00006239 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00006240 }
drh079a3072014-03-19 14:10:55 +00006241 break;
6242}
6243
drh0f3f7662017-08-18 14:34:28 +00006244/* Opcode: CreateBtree P1 P2 P3 * *
6245** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00006246**
drh0f3f7662017-08-18 14:34:28 +00006247** Allocate a new b-tree in the main database file if P1==0 or in the
6248** TEMP database file if P1==1 or in an attached database if
6249** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00006250** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00006251** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00006252*/
drh0f3f7662017-08-18 14:34:28 +00006253case OP_CreateBtree: { /* out2 */
drhabc38152020-07-22 13:38:04 +00006254 Pgno pgno;
drh234c39d2004-07-24 03:30:47 +00006255 Db *pDb;
drh856c1032009-06-02 15:21:42 +00006256
drh4031baf2018-05-28 17:31:20 +00006257 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00006258 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00006259 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00006260 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00006261 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006262 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006263 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00006264 pDb = &db->aDb[pOp->p1];
6265 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00006266 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00006267 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00006268 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00006269 break;
6270}
6271
drh4a54bb52017-02-18 15:58:52 +00006272/* Opcode: SqlExec * * * P4 *
6273**
6274** Run the SQL statement or statements specified in the P4 string.
6275*/
6276case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00006277 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00006278 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00006279 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00006280 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00006281 if( rc ) goto abort_due_to_error;
6282 break;
6283}
6284
drh22645842011-03-24 01:34:03 +00006285/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00006286**
drh346a70c2020-06-15 20:27:35 +00006287** Read and parse all entries from the schema table of database P1
drh1595abc2018-08-14 19:27:51 +00006288** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6289** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00006290**
6291** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00006292** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00006293*/
drh9cbf3422008-01-17 16:22:13 +00006294case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00006295 int iDb;
drh067b92b2020-06-19 15:24:12 +00006296 const char *zSchema;
drh856c1032009-06-02 15:21:42 +00006297 char *zSql;
6298 InitData initData;
6299
drhbdaec522011-04-04 00:14:43 +00006300 /* Any prepared statement that invokes this opcode will hold mutexes
6301 ** on every btree. This is a prerequisite for invoking
6302 ** sqlite3InitCallback().
6303 */
6304#ifdef SQLITE_DEBUG
6305 for(iDb=0; iDb<db->nDb; iDb++){
6306 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
6307 }
6308#endif
drhbdaec522011-04-04 00:14:43 +00006309
drh856c1032009-06-02 15:21:42 +00006310 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00006311 assert( iDb>=0 && iDb<db->nDb );
drh4aab6fa2021-04-12 22:39:46 +00006312 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) || db->mallocFailed );
dane325ffe2018-08-11 13:40:20 +00006313
6314#ifndef SQLITE_OMIT_ALTERTABLE
6315 if( pOp->p4.z==0 ){
6316 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00006317 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
dan6a5a13d2021-02-17 20:08:22 +00006318 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
dane325ffe2018-08-11 13:40:20 +00006319 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00006320 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00006321 }else
6322#endif
drh1595abc2018-08-14 19:27:51 +00006323 {
drh067b92b2020-06-19 15:24:12 +00006324 zSchema = DFLT_SCHEMA_TABLE;
danielk1977a8bbef82009-03-23 17:11:26 +00006325 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00006326 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00006327 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00006328 initData.mInitFlags = 0;
drh3b3ddba2020-07-22 18:03:56 +00006329 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
danielk1977a8bbef82009-03-23 17:11:26 +00006330 zSql = sqlite3MPrintf(db,
drhc5a93d42019-08-12 00:08:07 +00006331 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
drh067b92b2020-06-19 15:24:12 +00006332 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00006333 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00006334 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00006335 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00006336 assert( db->init.busy==0 );
6337 db->init.busy = 1;
6338 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00006339 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006340 assert( !db->mallocFailed );
6341 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
6342 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00006343 if( rc==SQLITE_OK && initData.nInitRow==0 ){
6344 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6345 ** at least one SQL statement. Any less than that indicates that
drh1e32bed2020-06-19 13:33:53 +00006346 ** the sqlite_schema table is corrupt. */
drh6b86e512019-01-05 21:09:37 +00006347 rc = SQLITE_CORRUPT_BKPT;
6348 }
drhdbd6a7d2017-04-05 12:39:49 +00006349 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00006350 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006351 }
drh3c23a882007-01-09 14:01:13 +00006352 }
drh9467abf2016-02-17 18:44:11 +00006353 if( rc ){
6354 sqlite3ResetAllSchemasOfConnection(db);
6355 if( rc==SQLITE_NOMEM ){
6356 goto no_mem;
6357 }
6358 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00006359 }
drh234c39d2004-07-24 03:30:47 +00006360 break;
6361}
6362
drh8bfdf722009-06-19 14:06:03 +00006363#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00006364/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00006365**
6366** Read the sqlite_stat1 table for database P1 and load the content
6367** of that table into the internal index hash table. This will cause
6368** the analysis to be used when preparing all subsequent queries.
6369*/
drh9cbf3422008-01-17 16:22:13 +00006370case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00006371 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6372 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006373 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00006374 break;
6375}
drh8bfdf722009-06-19 14:06:03 +00006376#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00006377
drh98757152008-01-09 23:04:12 +00006378/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006379**
6380** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006381** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00006382** is dropped from disk (using the Destroy opcode) in order to keep
6383** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006384** schema consistent with what is on disk.
6385*/
drh9cbf3422008-01-17 16:22:13 +00006386case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00006387 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006388 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006389 break;
6390}
6391
drh98757152008-01-09 23:04:12 +00006392/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006393**
6394** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006395** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00006396** is dropped from disk (using the Destroy opcode)
6397** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00006398** schema consistent with what is on disk.
6399*/
drh9cbf3422008-01-17 16:22:13 +00006400case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00006401 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006402 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006403 break;
6404}
6405
drh98757152008-01-09 23:04:12 +00006406/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006407**
6408** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006409** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00006410** is dropped from disk (using the Destroy opcode) in order to keep
6411** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006412** schema consistent with what is on disk.
6413*/
drh9cbf3422008-01-17 16:22:13 +00006414case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00006415 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006416 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006417 break;
6418}
6419
drh234c39d2004-07-24 03:30:47 +00006420
drhb7f91642004-10-31 02:22:47 +00006421#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00006422/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00006423**
drh98757152008-01-09 23:04:12 +00006424** Do an analysis of the currently open database. Store in
6425** register P1 the text of an error message describing any problems.
6426** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00006427**
drh66accfc2017-02-22 18:04:42 +00006428** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00006429** At most reg(P3) errors will be reported.
6430** In other words, the analysis stops as soon as reg(P1) errors are
6431** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00006432**
drh98968b22016-03-15 22:00:39 +00006433** The root page numbers of all tables in the database are integers
6434** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00006435**
drh98757152008-01-09 23:04:12 +00006436** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00006437** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00006438**
drh1dcdbc02007-01-27 02:24:54 +00006439** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00006440*/
drhaaab5722002-02-19 13:39:21 +00006441case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00006442 int nRoot; /* Number of tables to check. (Number of root pages.) */
drhabc38152020-07-22 13:38:04 +00006443 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00006444 int nErr; /* Number of errors reported */
6445 char *z; /* Text of the error report */
6446 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00006447
drh1713afb2013-06-28 01:24:57 +00006448 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00006449 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00006450 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00006451 assert( nRoot>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00006452 assert( aRoot[0]==(Pgno)nRoot );
drh9f6168b2016-03-19 23:32:58 +00006453 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006454 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00006455 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00006456 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00006457 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00006458 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006459 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh21f6daa2019-10-11 14:21:48 +00006460 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00006461 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00006462 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00006463 if( nErr==0 ){
6464 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00006465 }else if( z==0 ){
6466 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00006467 }else{
drh66accfc2017-02-22 18:04:42 +00006468 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00006469 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00006470 }
drhb7654112008-01-12 12:48:07 +00006471 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00006472 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh21f6daa2019-10-11 14:21:48 +00006473 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006474}
drhb7f91642004-10-31 02:22:47 +00006475#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00006476
drh3d4501e2008-12-04 20:40:10 +00006477/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00006478** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006479**
drhbb6783b2017-04-29 18:02:49 +00006480** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00006481** held in register P1.
6482**
6483** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00006484*/
drh93952eb2009-11-13 19:43:43 +00006485case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00006486 pIn1 = &aMem[pOp->p1];
6487 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00006488 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00006489 if( (pIn1->flags & MEM_Blob)==0 ){
6490 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00006491 }
drh9d67afc2018-08-29 20:24:03 +00006492 assert( sqlite3VdbeMemIsRowSet(pIn1) );
6493 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00006494 break;
6495}
6496
6497/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00006498** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00006499**
drhbb6783b2017-04-29 18:02:49 +00006500** Extract the smallest value from the RowSet object in P1
6501** and put that value into register P3.
6502** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00006503** unchanged and jump to instruction P2.
6504*/
drh93952eb2009-11-13 19:43:43 +00006505case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00006506 i64 val;
drh49afe3a2013-07-10 03:05:14 +00006507
drh3c657212009-11-17 23:59:58 +00006508 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00006509 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
6510 if( (pIn1->flags & MEM_Blob)==0
6511 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00006512 ){
6513 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00006514 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00006515 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00006516 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00006517 }else{
6518 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00006519 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00006520 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00006521 }
drh49afe3a2013-07-10 03:05:14 +00006522 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006523}
6524
drh1b26c7c2009-04-22 02:15:47 +00006525/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00006526** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00006527**
drhade97602009-04-21 15:05:18 +00006528** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00006529** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00006530** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00006531** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00006532** next opcode.
danielk19771d461462009-04-21 09:02:45 +00006533**
drhbb6783b2017-04-29 18:02:49 +00006534** The RowSet object is optimized for the case where sets of integers
6535** are inserted in distinct phases, which each set contains no duplicates.
6536** Each set is identified by a unique P4 value. The first set
6537** must have P4==0, the final set must have P4==-1, and for all other sets
6538** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00006539**
6540** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006541** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006542** (b) when P4==-1 there is no need to insert the value, as it will
6543** never be tested for, and (c) when a value that is part of set X is
6544** inserted, there is no need to search to see if the same value was
6545** previously inserted as part of set X (only if it was previously
6546** inserted as part of some other set).
6547*/
drh1b26c7c2009-04-22 02:15:47 +00006548case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006549 int iSet;
6550 int exists;
6551
drh3c657212009-11-17 23:59:58 +00006552 pIn1 = &aMem[pOp->p1];
6553 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006554 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006555 assert( pIn3->flags&MEM_Int );
6556
drh1b26c7c2009-04-22 02:15:47 +00006557 /* If there is anything other than a rowset object in memory cell P1,
6558 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006559 */
drh9d67afc2018-08-29 20:24:03 +00006560 if( (pIn1->flags & MEM_Blob)==0 ){
6561 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006562 }
drh9d67afc2018-08-29 20:24:03 +00006563 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006564 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006565 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006566 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006567 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006568 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006569 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006570 }
6571 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006572 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006573 }
6574 break;
6575}
6576
drh5e00f6c2001-09-13 13:46:56 +00006577
danielk197793758c82005-01-21 08:13:14 +00006578#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006579
drh0fd61352014-02-07 02:29:45 +00006580/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006581**
dan76d462e2009-08-30 11:42:51 +00006582** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006583**
dan76d462e2009-08-30 11:42:51 +00006584** P1 contains the address of the memory cell that contains the first memory
6585** cell in an array of values used as arguments to the sub-program. P2
6586** contains the address to jump to if the sub-program throws an IGNORE
6587** exception using the RAISE() function. Register P3 contains the address
6588** of a memory cell in this (the parent) VM that is used to allocate the
6589** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006590**
6591** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006592**
6593** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006594*/
dan76d462e2009-08-30 11:42:51 +00006595case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006596 int nMem; /* Number of memory registers for sub-program */
6597 int nByte; /* Bytes of runtime space required for sub-program */
6598 Mem *pRt; /* Register to allocate runtime space */
6599 Mem *pMem; /* Used to iterate through memory cells */
6600 Mem *pEnd; /* Last memory cell in new array */
6601 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6602 SubProgram *pProgram; /* Sub-program to execute */
6603 void *t; /* Token identifying trigger */
6604
6605 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006606 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006607 assert( pProgram->nOp>0 );
6608
dan1da40a32009-09-19 17:00:31 +00006609 /* If the p5 flag is clear, then recursive invocation of triggers is
6610 ** disabled for backwards compatibility (p5 is set if this sub-program
6611 ** is really a trigger, not a foreign key action, and the flag set
6612 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006613 **
6614 ** It is recursive invocation of triggers, at the SQL level, that is
6615 ** disabled. In some cases a single trigger may generate more than one
6616 ** SubProgram (if the trigger may be executed with more than one different
6617 ** ON CONFLICT algorithm). SubProgram structures associated with a
6618 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006619 ** variable. */
6620 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006621 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006622 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6623 if( pFrame ) break;
6624 }
6625
danf5894502009-10-07 18:41:19 +00006626 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006627 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006628 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006629 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006630 }
6631
6632 /* Register pRt is used to store the memory required to save the state
6633 ** of the current program, and the memory required at runtime to execute
6634 ** the trigger program. If this trigger has been fired before, then pRt
6635 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006636 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006637 /* SubProgram.nMem is set to the number of memory cells used by the
6638 ** program stored in SubProgram.aOp. As well as these, one memory
6639 ** cell is required for each cursor used by the program. Set local
6640 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6641 */
dan65a7cd12009-09-01 12:16:01 +00006642 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006643 assert( nMem>0 );
6644 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006645 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006646 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006647 + pProgram->nCsr * sizeof(VdbeCursor*)
6648 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006649 pFrame = sqlite3DbMallocZero(db, nByte);
6650 if( !pFrame ){
6651 goto no_mem;
6652 }
6653 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006654 pRt->flags = MEM_Blob|MEM_Dyn;
6655 pRt->z = (char*)pFrame;
6656 pRt->n = nByte;
6657 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006658
6659 pFrame->v = p;
6660 pFrame->nChildMem = nMem;
6661 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006662 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006663 pFrame->aMem = p->aMem;
6664 pFrame->nMem = p->nMem;
6665 pFrame->apCsr = p->apCsr;
6666 pFrame->nCursor = p->nCursor;
6667 pFrame->aOp = p->aOp;
6668 pFrame->nOp = p->nOp;
6669 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006670#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006671 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006672#endif
drh72f56ef2018-08-29 18:47:22 +00006673#ifdef SQLITE_DEBUG
6674 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
6675#endif
dan165921a2009-08-28 18:53:45 +00006676
6677 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6678 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006679 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006680 pMem->db = db;
6681 }
6682 }else{
drh72f56ef2018-08-29 18:47:22 +00006683 pFrame = (VdbeFrame*)pRt->z;
6684 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00006685 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6686 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006687 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006688 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006689 }
6690
6691 p->nFrame++;
6692 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006693 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006694 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006695 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006696 assert( pFrame->pAuxData==0 );
6697 pFrame->pAuxData = p->pAuxData;
6698 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006699 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006700 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006701 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006702 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006703 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006704 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006705 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006706 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006707 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006708 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006709#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006710 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006711#endif
drhb2e61bc2019-01-25 19:29:01 +00006712#ifdef SQLITE_DEBUG
6713 /* Verify that second and subsequent executions of the same trigger do not
6714 ** try to reuse register values from the first use. */
6715 {
6716 int i;
6717 for(i=0; i<p->nMem; i++){
6718 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
drhf5cfe6f2020-03-03 20:48:12 +00006719 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
drhb2e61bc2019-01-25 19:29:01 +00006720 }
6721 }
6722#endif
drhf56fa462015-04-13 21:39:54 +00006723 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00006724 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00006725}
6726
dan76d462e2009-08-30 11:42:51 +00006727/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006728**
dan76d462e2009-08-30 11:42:51 +00006729** This opcode is only ever present in sub-programs called via the
6730** OP_Program instruction. Copy a value currently stored in a memory
6731** cell of the calling (parent) frame to cell P2 in the current frames
6732** address space. This is used by trigger programs to access the new.*
6733** and old.* values.
dan165921a2009-08-28 18:53:45 +00006734**
dan76d462e2009-08-30 11:42:51 +00006735** The address of the cell in the parent frame is determined by adding
6736** the value of the P1 argument to the value of the P1 argument to the
6737** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006738*/
drh27a348c2015-04-13 19:14:06 +00006739case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006740 VdbeFrame *pFrame;
6741 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006742 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006743 pFrame = p->pFrame;
6744 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006745 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6746 break;
6747}
6748
danielk197793758c82005-01-21 08:13:14 +00006749#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006750
dan1da40a32009-09-19 17:00:31 +00006751#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006752/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006753** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006754**
dan0ff297e2009-09-25 17:03:14 +00006755** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6756** If P1 is non-zero, the database constraint counter is incremented
6757** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006758** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006759*/
dan32b09f22009-09-23 17:29:59 +00006760case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006761 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006762 db->nDeferredImmCons += pOp->p2;
6763 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006764 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006765 }else{
dan0ff297e2009-09-25 17:03:14 +00006766 p->nFkConstraint += pOp->p2;
6767 }
6768 break;
6769}
6770
6771/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006772** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006773**
6774** This opcode tests if a foreign key constraint-counter is currently zero.
6775** If so, jump to instruction P2. Otherwise, fall through to the next
6776** instruction.
6777**
6778** If P1 is non-zero, then the jump is taken if the database constraint-counter
6779** is zero (the one that counts deferred constraint violations). If P1 is
6780** zero, the jump is taken if the statement constraint-counter is zero
6781** (immediate foreign key constraint violations).
6782*/
6783case OP_FkIfZero: { /* jump */
6784 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006785 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006786 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006787 }else{
drh688852a2014-02-17 22:40:43 +00006788 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006789 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006790 }
dan1da40a32009-09-19 17:00:31 +00006791 break;
6792}
6793#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6794
drh205f48e2004-11-05 00:43:11 +00006795#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006796/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006797** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006798**
dan76d462e2009-08-30 11:42:51 +00006799** P1 is a register in the root frame of this VM (the root frame is
6800** different from the current frame if this instruction is being executed
6801** within a sub-program). Set the value of register P1 to the maximum of
6802** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006803**
6804** This instruction throws an error if the memory cell is not initially
6805** an integer.
6806*/
dan76d462e2009-08-30 11:42:51 +00006807case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006808 VdbeFrame *pFrame;
6809 if( p->pFrame ){
6810 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6811 pIn1 = &pFrame->aMem[pOp->p1];
6812 }else{
drha6c2ed92009-11-14 23:22:23 +00006813 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006814 }
drh2b4ded92010-09-27 21:09:31 +00006815 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006816 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006817 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006818 sqlite3VdbeMemIntegerify(pIn2);
6819 if( pIn1->u.i<pIn2->u.i){
6820 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006821 }
6822 break;
6823}
6824#endif /* SQLITE_OMIT_AUTOINCREMENT */
6825
drh8b0cf382015-10-06 21:07:06 +00006826/* Opcode: IfPos P1 P2 P3 * *
6827** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006828**
drh16897072015-03-07 00:57:37 +00006829** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006830** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006831** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006832**
drh16897072015-03-07 00:57:37 +00006833** If the initial value of register P1 is less than 1, then the
6834** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006835*/
drh9cbf3422008-01-17 16:22:13 +00006836case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006837 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006838 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006839 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006840 if( pIn1->u.i>0 ){
6841 pIn1->u.i -= pOp->p3;
6842 goto jump_to_p2;
6843 }
drhec7429a2005-10-06 16:53:14 +00006844 break;
6845}
6846
drhcc2fa4c2016-01-25 15:57:29 +00006847/* Opcode: OffsetLimit P1 P2 P3 * *
6848** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006849**
drhcc2fa4c2016-01-25 15:57:29 +00006850** This opcode performs a commonly used computation associated with
6851** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6852** holds the offset counter. The opcode computes the combined value
6853** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6854** value computed is the total number of rows that will need to be
6855** visited in order to complete the query.
6856**
6857** If r[P3] is zero or negative, that means there is no OFFSET
6858** and r[P2] is set to be the value of the LIMIT, r[P1].
6859**
6860** if r[P1] is zero or negative, that means there is no LIMIT
6861** and r[P2] is set to -1.
6862**
6863** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006864*/
drhcc2fa4c2016-01-25 15:57:29 +00006865case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006866 i64 x;
drh3c657212009-11-17 23:59:58 +00006867 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006868 pIn3 = &aMem[pOp->p3];
6869 pOut = out2Prerelease(p, pOp);
6870 assert( pIn1->flags & MEM_Int );
6871 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006872 x = pIn1->u.i;
6873 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6874 /* If the LIMIT is less than or equal to zero, loop forever. This
6875 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6876 ** also loop forever. This is undocumented. In fact, one could argue
6877 ** that the loop should terminate. But assuming 1 billion iterations
6878 ** per second (far exceeding the capabilities of any current hardware)
6879 ** it would take nearly 300 years to actually reach the limit. So
6880 ** looping forever is a reasonable approximation. */
6881 pOut->u.i = -1;
6882 }else{
6883 pOut->u.i = x;
6884 }
drh15007a92006-01-08 18:10:17 +00006885 break;
6886}
6887
drhf99dd352016-12-18 17:42:00 +00006888/* Opcode: IfNotZero P1 P2 * * *
6889** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006890**
drh16897072015-03-07 00:57:37 +00006891** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006892** initially greater than zero, then decrement the value in register P1.
6893** If it is non-zero (negative or positive) and then also jump to P2.
6894** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006895*/
drh16897072015-03-07 00:57:37 +00006896case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006897 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006898 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006899 VdbeBranchTaken(pIn1->u.i<0, 2);
6900 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006901 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006902 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006903 }
6904 break;
6905}
6906
6907/* Opcode: DecrJumpZero P1 P2 * * *
6908** Synopsis: if (--r[P1])==0 goto P2
6909**
drhab5be2e2016-11-30 05:08:59 +00006910** Register P1 must hold an integer. Decrement the value in P1
6911** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006912*/
6913case OP_DecrJumpZero: { /* jump, in1 */
6914 pIn1 = &aMem[pOp->p1];
6915 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006916 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6917 VdbeBranchTaken(pIn1->u.i==0, 2);
6918 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006919 break;
6920}
6921
drh16897072015-03-07 00:57:37 +00006922
drh8f26da62018-07-05 21:22:57 +00006923/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006924** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006925**
drh8f26da62018-07-05 21:22:57 +00006926** Execute the xStep function for an aggregate.
6927** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006928** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006929** accumulator.
drhe5095352002-02-24 03:25:14 +00006930**
drh98757152008-01-09 23:04:12 +00006931** The P5 arguments are taken from register P2 and its
6932** successors.
drhe5095352002-02-24 03:25:14 +00006933*/
drh8f26da62018-07-05 21:22:57 +00006934/* Opcode: AggInverse * P2 P3 P4 P5
6935** Synopsis: accum=r[P3] inverse(r[P2@P5])
6936**
6937** Execute the xInverse function for an aggregate.
6938** The function has P5 arguments. P4 is a pointer to the
6939** FuncDef structure that specifies the function. Register P3 is the
6940** accumulator.
6941**
6942** The P5 arguments are taken from register P2 and its
6943** successors.
6944*/
6945/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006946** Synopsis: accum=r[P3] step(r[P2@P5])
6947**
dan9a947222018-06-14 19:06:36 +00006948** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6949** aggregate. The function has P5 arguments. P4 is a pointer to the
6950** FuncDef structure that specifies the function. Register P3 is the
6951** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006952**
6953** The P5 arguments are taken from register P2 and its
6954** successors.
6955**
6956** This opcode is initially coded as OP_AggStep0. On first evaluation,
6957** the FuncDef stored in P4 is converted into an sqlite3_context and
6958** the opcode is changed. In this way, the initialization of the
6959** sqlite3_context only happens once, instead of on each call to the
6960** step function.
6961*/
drh8f26da62018-07-05 21:22:57 +00006962case OP_AggInverse:
6963case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006964 int n;
drh9c7c9132015-06-26 18:16:52 +00006965 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006966
drh9c7c9132015-06-26 18:16:52 +00006967 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006968 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006969 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6970 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006971 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006972 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6973 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006974 if( pCtx==0 ) goto no_mem;
6975 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006976 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6977 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006978 pCtx->pFunc = pOp->p4.pFunc;
6979 pCtx->iOp = (int)(pOp - aOp);
6980 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006981 pCtx->skipFlag = 0;
6982 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006983 pCtx->argc = n;
6984 pOp->p4type = P4_FUNCCTX;
6985 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006986
6987 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006988 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006989
drh8f26da62018-07-05 21:22:57 +00006990 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006991 /* Fall through into OP_AggStep */
drh08b92082020-08-10 14:18:00 +00006992 /* no break */ deliberate_fall_through
drh9c7c9132015-06-26 18:16:52 +00006993}
drh8f26da62018-07-05 21:22:57 +00006994case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006995 int i;
6996 sqlite3_context *pCtx;
6997 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006998
6999 assert( pOp->p4type==P4_FUNCCTX );
7000 pCtx = pOp->p4.pCtx;
7001 pMem = &aMem[pOp->p3];
7002
drh2c885d02018-07-07 19:36:04 +00007003#ifdef SQLITE_DEBUG
7004 if( pOp->p1 ){
7005 /* This is an OP_AggInverse call. Verify that xStep has always
7006 ** been called at least once prior to any xInverse call. */
7007 assert( pMem->uTemp==0x1122e0e3 );
7008 }else{
7009 /* This is an OP_AggStep call. Mark it as such. */
7010 pMem->uTemp = 0x1122e0e3;
7011 }
7012#endif
7013
drh9c7c9132015-06-26 18:16:52 +00007014 /* If this function is inside of a trigger, the register array in aMem[]
7015 ** might change from one evaluation to the next. The next block of code
7016 ** checks to see if the register array has changed, and if so it
7017 ** reinitializes the relavant parts of the sqlite3_context object */
7018 if( pCtx->pMem != pMem ){
7019 pCtx->pMem = pMem;
7020 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7021 }
7022
7023#ifdef SQLITE_DEBUG
7024 for(i=0; i<pCtx->argc; i++){
7025 assert( memIsValid(pCtx->argv[i]) );
7026 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7027 }
7028#endif
7029
drhabfcea22005-09-06 20:36:48 +00007030 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00007031 assert( pCtx->pOut->flags==MEM_Null );
7032 assert( pCtx->isError==0 );
7033 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00007034#ifndef SQLITE_OMIT_WINDOWFUNC
7035 if( pOp->p1 ){
7036 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
7037 }else
7038#endif
7039 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
7040
drhf09ac0b2018-01-23 03:44:06 +00007041 if( pCtx->isError ){
7042 if( pCtx->isError>0 ){
7043 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00007044 rc = pCtx->isError;
7045 }
drhf09ac0b2018-01-23 03:44:06 +00007046 if( pCtx->skipFlag ){
7047 assert( pOp[-1].opcode==OP_CollSeq );
7048 i = pOp[-1].p1;
7049 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
7050 pCtx->skipFlag = 0;
7051 }
7052 sqlite3VdbeMemRelease(pCtx->pOut);
7053 pCtx->pOut->flags = MEM_Null;
7054 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00007055 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00007056 }
drhf09ac0b2018-01-23 03:44:06 +00007057 assert( pCtx->pOut->flags==MEM_Null );
7058 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00007059 break;
7060}
7061
drh8f26da62018-07-05 21:22:57 +00007062/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00007063** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00007064**
dan9a947222018-06-14 19:06:36 +00007065** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00007066** or window function. Execute the finalizer function
7067** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00007068**
7069** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00007070** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00007071** argument is not used by this opcode. It is only there to disambiguate
7072** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00007073** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00007074** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00007075*/
drh8f26da62018-07-05 21:22:57 +00007076/* Opcode: AggValue * P2 P3 P4 *
7077** Synopsis: r[P3]=value N=P2
7078**
7079** Invoke the xValue() function and store the result in register P3.
7080**
7081** P2 is the number of arguments that the step function takes and
7082** P4 is a pointer to the FuncDef for this function. The P2
7083** argument is not used by this opcode. It is only there to disambiguate
7084** functions that can take varying numbers of arguments. The
7085** P4 argument is only needed for the case where
7086** the step function was not previously called.
7087*/
7088case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00007089case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00007090 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00007091 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00007092 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00007093 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00007094 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00007095#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00007096 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00007097 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00007098 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00007099 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00007100 }else
7101#endif
drh8f26da62018-07-05 21:22:57 +00007102 {
7103 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
7104 }
dan67a9b8e2018-06-22 20:51:35 +00007105
drh4c8555f2009-06-25 01:47:11 +00007106 if( rc ){
drh22c17b82015-05-15 04:13:15 +00007107 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00007108 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00007109 }
drh2dca8682008-03-21 17:13:13 +00007110 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00007111 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00007112 if( sqlite3VdbeMemTooBig(pMem) ){
7113 goto too_big;
7114 }
drh5e00f6c2001-09-13 13:46:56 +00007115 break;
7116}
7117
dan5cf53532010-05-01 16:40:20 +00007118#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00007119/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007120**
7121** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00007122** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7123** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00007124** SQLITE_BUSY or not, respectively. Write the number of pages in the
7125** WAL after the checkpoint into mem[P3+1] and the number of pages
7126** in the WAL that have been checkpointed after the checkpoint
7127** completes into mem[P3+2]. However on an error, mem[P3+1] and
7128** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00007129*/
7130case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00007131 int i; /* Loop counter */
7132 int aRes[3]; /* Results */
7133 Mem *pMem; /* Write results here */
7134
drh9e92a472013-06-27 17:40:30 +00007135 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00007136 aRes[0] = 0;
7137 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00007138 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
7139 || pOp->p2==SQLITE_CHECKPOINT_FULL
7140 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00007141 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00007142 );
drh30aa3b92011-02-07 23:56:01 +00007143 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00007144 if( rc ){
7145 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00007146 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00007147 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00007148 }
drh30aa3b92011-02-07 23:56:01 +00007149 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
7150 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
7151 }
dan7c246102010-04-12 19:00:29 +00007152 break;
7153};
dan5cf53532010-05-01 16:40:20 +00007154#endif
drh5e00f6c2001-09-13 13:46:56 +00007155
drhcac29a62010-07-02 19:36:52 +00007156#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00007157/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007158**
7159** Change the journal mode of database P1 to P3. P3 must be one of the
7160** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7161** modes (delete, truncate, persist, off and memory), this is a simple
7162** operation. No IO is required.
7163**
7164** If changing into or out of WAL mode the procedure is more complicated.
7165**
7166** Write a string containing the final journal-mode to register P2.
7167*/
drh27a348c2015-04-13 19:14:06 +00007168case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00007169 Btree *pBt; /* Btree to change journal mode of */
7170 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00007171 int eNew; /* New journal mode */
7172 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00007173#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00007174 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00007175#endif
dane04dc882010-04-20 18:53:15 +00007176
drh27a348c2015-04-13 19:14:06 +00007177 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00007178 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00007179 assert( eNew==PAGER_JOURNALMODE_DELETE
7180 || eNew==PAGER_JOURNALMODE_TRUNCATE
7181 || eNew==PAGER_JOURNALMODE_PERSIST
7182 || eNew==PAGER_JOURNALMODE_OFF
7183 || eNew==PAGER_JOURNALMODE_MEMORY
7184 || eNew==PAGER_JOURNALMODE_WAL
7185 || eNew==PAGER_JOURNALMODE_QUERY
7186 );
7187 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00007188 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00007189
dane04dc882010-04-20 18:53:15 +00007190 pBt = db->aDb[pOp->p1].pBt;
7191 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00007192 eOld = sqlite3PagerGetJournalMode(pPager);
7193 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
7194 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00007195
7196#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00007197 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00007198
drhd80b2332010-05-01 00:59:37 +00007199 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00007200 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00007201 */
7202 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00007203 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00007204 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00007205 ){
drh0b9b4302010-06-11 17:01:24 +00007206 eNew = eOld;
dane180c292010-04-26 17:42:56 +00007207 }
7208
drh0b9b4302010-06-11 17:01:24 +00007209 if( (eNew!=eOld)
7210 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
7211 ){
danc0537fe2013-06-28 19:41:43 +00007212 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00007213 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00007214 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00007215 "cannot change %s wal mode from within a transaction",
7216 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
7217 );
drh9467abf2016-02-17 18:44:11 +00007218 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00007219 }else{
7220
7221 if( eOld==PAGER_JOURNALMODE_WAL ){
7222 /* If leaving WAL mode, close the log file. If successful, the call
7223 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7224 ** file. An EXCLUSIVE lock may still be held on the database file
7225 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00007226 */
dan7fb89902016-08-12 16:21:15 +00007227 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00007228 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00007229 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00007230 }
drh242c4f72010-06-22 14:49:39 +00007231 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
7232 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7233 ** as an intermediate */
7234 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00007235 }
7236
7237 /* Open a transaction on the database file. Regardless of the journal
7238 ** mode, this transaction always uses a rollback journal.
7239 */
drh99744fa2020-08-25 19:09:07 +00007240 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE );
drh0b9b4302010-06-11 17:01:24 +00007241 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00007242 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00007243 }
7244 }
7245 }
dan5cf53532010-05-01 16:40:20 +00007246#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00007247
drh9467abf2016-02-17 18:44:11 +00007248 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00007249 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00007250
dane04dc882010-04-20 18:53:15 +00007251 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00007252 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00007253 pOut->n = sqlite3Strlen30(pOut->z);
7254 pOut->enc = SQLITE_UTF8;
7255 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00007256 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00007257 break;
drhcac29a62010-07-02 19:36:52 +00007258};
7259#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00007260
drhfdbcdee2007-03-27 14:44:50 +00007261#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00007262/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00007263**
drh9ef5e772016-08-19 14:20:56 +00007264** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7265** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00007266**
drh2f6239e2018-12-08 00:43:08 +00007267** If P2 is not zero, then it is a register holding a string which is
7268** the file into which the result of vacuum should be written. When
7269** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00007270*/
drh9cbf3422008-01-17 16:22:13 +00007271case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00007272 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00007273 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
7274 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00007275 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00007276 break;
7277}
drh154d4b22006-09-21 11:02:16 +00007278#endif
drh6f8c91c2003-12-07 00:24:35 +00007279
danielk1977dddbcdc2007-04-26 14:42:34 +00007280#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00007281/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00007282**
7283** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00007284** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00007285** P2. Otherwise, fall through to the next instruction.
7286*/
drh9cbf3422008-01-17 16:22:13 +00007287case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00007288 Btree *pBt;
7289
7290 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007291 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00007292 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00007293 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00007294 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00007295 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00007296 if( rc ){
7297 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00007298 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00007299 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00007300 }
7301 break;
7302}
7303#endif
7304
drhba968db2018-07-24 22:02:12 +00007305/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00007306**
drh25df48d2014-07-22 14:58:12 +00007307** Cause precompiled statements to expire. When an expired statement
7308** is executed using sqlite3_step() it will either automatically
7309** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7310** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00007311**
7312** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00007313** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00007314**
7315** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7316** then running SQL statements are allowed to continue to run to completion.
7317** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7318** that might help the statement run faster but which does not affect the
7319** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00007320*/
drh9cbf3422008-01-17 16:22:13 +00007321case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00007322 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00007323 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00007324 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00007325 }else{
drhba968db2018-07-24 22:02:12 +00007326 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00007327 }
7328 break;
7329}
7330
drh7b14b652019-12-29 22:08:20 +00007331/* Opcode: CursorLock P1 * * * *
7332**
7333** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7334** written by an other cursor.
7335*/
7336case OP_CursorLock: {
7337 VdbeCursor *pC;
7338 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7339 pC = p->apCsr[pOp->p1];
7340 assert( pC!=0 );
7341 assert( pC->eCurType==CURTYPE_BTREE );
7342 sqlite3BtreeCursorPin(pC->uc.pCursor);
7343 break;
7344}
7345
7346/* Opcode: CursorUnlock P1 * * * *
7347**
7348** Unlock the btree to which cursor P1 is pointing so that it can be
7349** written by other cursors.
7350*/
7351case OP_CursorUnlock: {
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 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
7358 break;
7359}
7360
danielk1977c00da102006-01-07 13:21:04 +00007361#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00007362/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00007363** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00007364**
7365** Obtain a lock on a particular table. This instruction is only used when
7366** the shared-cache feature is enabled.
7367**
danielk197796d48e92009-06-29 06:00:37 +00007368** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00007369** on which the lock is acquired. A readlock is obtained if P3==0 or
7370** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00007371**
7372** P2 contains the root-page of the table to lock.
7373**
drh66a51672008-01-03 00:01:23 +00007374** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00007375** used to generate an error message if the lock cannot be obtained.
7376*/
drh9cbf3422008-01-17 16:22:13 +00007377case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00007378 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00007379 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00007380 int p1 = pOp->p1;
7381 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007382 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00007383 assert( isWriteLock==0 || isWriteLock==1 );
7384 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00007385 if( rc ){
7386 if( (rc&0xFF)==SQLITE_LOCKED ){
7387 const char *z = pOp->p4.z;
7388 sqlite3VdbeError(p, "database table is locked: %s", z);
7389 }
7390 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00007391 }
danielk1977c00da102006-01-07 13:21:04 +00007392 }
7393 break;
7394}
drhb9bb7c12006-06-11 23:41:55 +00007395#endif /* SQLITE_OMIT_SHARED_CACHE */
7396
7397#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007398/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007399**
danielk19773e3a84d2008-08-01 17:37:40 +00007400** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7401** xBegin method for that table.
7402**
7403** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00007404** within a callback to a virtual table xSync() method. If it is, the error
7405** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00007406*/
drh9cbf3422008-01-17 16:22:13 +00007407case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00007408 VTable *pVTab;
7409 pVTab = pOp->p4.pVtab;
7410 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00007411 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00007412 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00007413 break;
7414}
7415#endif /* SQLITE_OMIT_VIRTUALTABLE */
7416
7417#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00007418/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00007419**
dan73779452015-03-19 18:56:17 +00007420** P2 is a register that holds the name of a virtual table in database
7421** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00007422*/
drh9cbf3422008-01-17 16:22:13 +00007423case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00007424 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00007425 const char *zTab; /* Name of the virtual table */
7426
dan73779452015-03-19 18:56:17 +00007427 memset(&sMem, 0, sizeof(sMem));
7428 sMem.db = db;
drh47464062015-03-21 12:22:16 +00007429 /* Because P2 is always a static string, it is impossible for the
7430 ** sqlite3VdbeMemCopy() to fail */
7431 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
7432 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00007433 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00007434 assert( rc==SQLITE_OK );
7435 zTab = (const char*)sqlite3_value_text(&sMem);
7436 assert( zTab || db->mallocFailed );
7437 if( zTab ){
7438 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00007439 }
7440 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00007441 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007442 break;
7443}
7444#endif /* SQLITE_OMIT_VIRTUALTABLE */
7445
7446#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007447/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007448**
drh66a51672008-01-03 00:01:23 +00007449** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00007450** of that table.
drhb9bb7c12006-06-11 23:41:55 +00007451*/
drh9cbf3422008-01-17 16:22:13 +00007452case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00007453 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00007454 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00007455 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00007456 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00007457 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007458 break;
7459}
7460#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00007461
drh9eff6162006-06-12 21:59:13 +00007462#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007463/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00007464**
drh66a51672008-01-03 00:01:23 +00007465** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00007466** P1 is a cursor number. This opcode opens a cursor to the virtual
7467** table and stores that cursor in P1.
7468*/
drh9cbf3422008-01-17 16:22:13 +00007469case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00007470 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00007471 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00007472 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007473 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007474
drh1713afb2013-06-28 01:24:57 +00007475 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00007476 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00007477 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00007478 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007479 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7480 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007481 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007482 }
7483 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00007484 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00007485 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007486 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007487
drh9467abf2016-02-17 18:44:11 +00007488 /* Initialize sqlite3_vtab_cursor base class */
7489 pVCur->pVtab = pVtab;
7490
7491 /* Initialize vdbe cursor object */
7492 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
7493 if( pCur ){
7494 pCur->uc.pVCur = pVCur;
7495 pVtab->nRef++;
7496 }else{
7497 assert( db->mallocFailed );
7498 pModule->xClose(pVCur);
7499 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007500 }
drh9eff6162006-06-12 21:59:13 +00007501 break;
7502}
7503#endif /* SQLITE_OMIT_VIRTUALTABLE */
7504
7505#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00007506/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00007507** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00007508**
7509** P1 is a cursor opened using VOpen. P2 is an address to jump to if
7510** the filtered result set is empty.
7511**
drh66a51672008-01-03 00:01:23 +00007512** P4 is either NULL or a string that was generated by the xBestIndex
7513** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00007514** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00007515**
drh9eff6162006-06-12 21:59:13 +00007516** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00007517** by P1. The integer query plan parameter to xFilter is stored in register
7518** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00007519** xFilter method. Registers P3+2..P3+1+argc are the argc
7520** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00007521** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00007522**
danielk19776dbee812008-01-03 18:39:41 +00007523** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00007524*/
drh9cbf3422008-01-17 16:22:13 +00007525case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00007526 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00007527 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007528 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007529 Mem *pQuery;
7530 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00007531 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00007532 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00007533 VdbeCursor *pCur;
7534 int res;
7535 int i;
7536 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007537
drha6c2ed92009-11-14 23:22:23 +00007538 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00007539 pArgc = &pQuery[1];
7540 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00007541 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00007542 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00007543 assert( pCur->eCurType==CURTYPE_VTAB );
7544 pVCur = pCur->uc.pVCur;
7545 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00007546 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007547
drh9cbf3422008-01-17 16:22:13 +00007548 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00007549 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00007550 nArg = (int)pArgc->u.i;
7551 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007552
drh644a5292006-12-20 14:53:38 +00007553 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00007554 res = 0;
7555 apArg = p->apArg;
7556 for(i = 0; i<nArg; i++){
7557 apArg[i] = &pArgc[i+1];
7558 }
drhc960dcb2015-11-20 19:22:01 +00007559 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00007560 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007561 if( rc ) goto abort_due_to_error;
7562 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00007563 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00007564 VdbeBranchTaken(res!=0,2);
7565 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00007566 break;
7567}
7568#endif /* SQLITE_OMIT_VIRTUALTABLE */
7569
7570#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00007571/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007572** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007573**
drh6f390be2018-01-11 17:04:26 +00007574** Store in register P3 the value of the P2-th column of
7575** the current row of the virtual-table of cursor P1.
7576**
7577** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007578** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007579** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7580** function to return true inside the xColumn method of the virtual
7581** table implementation. The P5 column might also contain other
7582** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7583** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007584*/
7585case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007586 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007587 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007588 Mem *pDest;
7589 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007590
drhdfe88ec2008-11-03 20:55:06 +00007591 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007592 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007593 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007594 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007595 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007596 if( pCur->nullRow ){
7597 sqlite3VdbeMemSetNull(pDest);
7598 break;
7599 }
drhc960dcb2015-11-20 19:22:01 +00007600 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007601 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007602 assert( pModule->xColumn );
7603 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007604 sContext.pOut = pDest;
drh75f10762019-12-14 18:08:22 +00007605 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
drh09d00b22018-09-27 20:20:01 +00007606 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007607 sqlite3VdbeMemSetNull(pDest);
7608 pDest->flags = MEM_Null|MEM_Zero;
7609 pDest->u.nZero = 0;
7610 }else{
7611 MemSetTypeFlag(pDest, MEM_Null);
7612 }
drhc960dcb2015-11-20 19:22:01 +00007613 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007614 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007615 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007616 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007617 rc = sContext.isError;
7618 }
drh9bd038f2014-08-27 14:14:06 +00007619 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007620 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007621 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007622
drhde4fcfd2008-01-19 23:50:26 +00007623 if( sqlite3VdbeMemTooBig(pDest) ){
7624 goto too_big;
7625 }
drh9467abf2016-02-17 18:44:11 +00007626 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007627 break;
7628}
7629#endif /* SQLITE_OMIT_VIRTUALTABLE */
7630
7631#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007632/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007633**
7634** Advance virtual table P1 to the next row in its result set and
7635** jump to instruction P2. Or, if the virtual table has reached
7636** the end of its result set, then fall through to the next instruction.
7637*/
drh9cbf3422008-01-17 16:22:13 +00007638case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007639 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007640 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007641 int res;
drh856c1032009-06-02 15:21:42 +00007642 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007643
drhc54a6172009-06-02 16:06:03 +00007644 res = 0;
drh856c1032009-06-02 15:21:42 +00007645 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007646 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007647 if( pCur->nullRow ){
7648 break;
7649 }
drhc960dcb2015-11-20 19:22:01 +00007650 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007651 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007652 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007653
drhde4fcfd2008-01-19 23:50:26 +00007654 /* Invoke the xNext() method of the module. There is no way for the
7655 ** underlying implementation to return an error if one occurs during
7656 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7657 ** data is available) and the error code returned when xColumn or
7658 ** some other method is next invoked on the save virtual table cursor.
7659 */
drhc960dcb2015-11-20 19:22:01 +00007660 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007661 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007662 if( rc ) goto abort_due_to_error;
7663 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007664 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007665 if( !res ){
7666 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007667 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007668 }
drh49afe3a2013-07-10 03:05:14 +00007669 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007670}
7671#endif /* SQLITE_OMIT_VIRTUALTABLE */
7672
danielk1977182c4ba2007-06-27 15:53:34 +00007673#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007674/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007675**
drh66a51672008-01-03 00:01:23 +00007676** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007677** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007678** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007679*/
drh9cbf3422008-01-17 16:22:13 +00007680case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007681 sqlite3_vtab *pVtab;
7682 Mem *pName;
dan34566c42018-09-20 17:21:21 +00007683 int isLegacy;
7684
7685 isLegacy = (db->flags & SQLITE_LegacyAlter);
7686 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00007687 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007688 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007689 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007690 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007691 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007692 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007693 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007694 testcase( pName->enc==SQLITE_UTF8 );
7695 testcase( pName->enc==SQLITE_UTF16BE );
7696 testcase( pName->enc==SQLITE_UTF16LE );
7697 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007698 if( rc ) goto abort_due_to_error;
7699 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00007700 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00007701 sqlite3VtabImportErrmsg(p, pVtab);
7702 p->expired = 0;
7703 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007704 break;
7705}
7706#endif
drh4cbdda92006-06-14 19:00:20 +00007707
7708#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007709/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007710** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007711**
drh66a51672008-01-03 00:01:23 +00007712** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007713** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007714** are contiguous memory cells starting at P3 to pass to the xUpdate
7715** invocation. The value in register (P3+P2-1) corresponds to the
7716** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007717**
7718** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007719** The argv[0] element (which corresponds to memory cell P3)
7720** is the rowid of a row to delete. If argv[0] is NULL then no
7721** deletion occurs. The argv[1] element is the rowid of the new
7722** row. This can be NULL to have the virtual table select the new
7723** rowid for itself. The subsequent elements in the array are
7724** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007725**
7726** If P2==1 then no insert is performed. argv[0] is the rowid of
7727** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007728**
7729** P1 is a boolean flag. If it is set to true and the xUpdate call
7730** is successful, then the value returned by sqlite3_last_insert_rowid()
7731** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007732**
7733** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7734** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007735*/
drh9cbf3422008-01-17 16:22:13 +00007736case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007737 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007738 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007739 int nArg;
7740 int i;
7741 sqlite_int64 rowid;
7742 Mem **apArg;
7743 Mem *pX;
7744
danb061d052011-04-25 18:49:57 +00007745 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7746 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7747 );
drh9e92a472013-06-27 17:40:30 +00007748 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007749 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007750 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007751 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007752 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7753 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007754 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007755 }
7756 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007757 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007758 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007759 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007760 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007761 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007762 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007763 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007764 assert( memIsValid(pX) );
7765 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007766 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007767 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007768 }
danb061d052011-04-25 18:49:57 +00007769 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007770 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007771 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007772 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007773 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007774 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007775 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007776 }
drhd91c1a12013-02-09 13:58:25 +00007777 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007778 if( pOp->p5==OE_Ignore ){
7779 rc = SQLITE_OK;
7780 }else{
7781 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7782 }
7783 }else{
7784 p->nChange++;
7785 }
drh9467abf2016-02-17 18:44:11 +00007786 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007787 }
drh4cbdda92006-06-14 19:00:20 +00007788 break;
danielk1977399918f2006-06-14 13:03:23 +00007789}
7790#endif /* SQLITE_OMIT_VIRTUALTABLE */
7791
danielk197759a93792008-05-15 17:48:20 +00007792#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7793/* Opcode: Pagecount P1 P2 * * *
7794**
7795** Write the current number of pages in database P1 to memory cell P2.
7796*/
drh27a348c2015-04-13 19:14:06 +00007797case OP_Pagecount: { /* out2 */
7798 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007799 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007800 break;
7801}
7802#endif
7803
drh60ac3f42010-11-23 18:59:27 +00007804
7805#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7806/* Opcode: MaxPgcnt P1 P2 P3 * *
7807**
7808** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007809** Do not let the maximum page count fall below the current page count and
7810** do not change the maximum page count value if P3==0.
7811**
drh60ac3f42010-11-23 18:59:27 +00007812** Store the maximum page count after the change in register P2.
7813*/
drh27a348c2015-04-13 19:14:06 +00007814case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007815 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007816 Btree *pBt;
7817
drh27a348c2015-04-13 19:14:06 +00007818 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007819 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007820 newMax = 0;
7821 if( pOp->p3 ){
7822 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007823 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007824 }
7825 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007826 break;
7827}
7828#endif
7829
drh920cf592019-10-30 16:29:02 +00007830/* Opcode: Function P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00007831** Synopsis: r[P3]=func(r[P2@NP])
drh3e34eab2017-07-19 19:48:40 +00007832**
7833** Invoke a user function (P4 is a pointer to an sqlite3_context object that
drh920cf592019-10-30 16:29:02 +00007834** contains a pointer to the function to be run) with arguments taken
7835** from register P2 and successors. The number of arguments is in
7836** the sqlite3_context object that P4 points to.
7837** The result of the function is stored
drh3e34eab2017-07-19 19:48:40 +00007838** in register P3. Register P3 must not be one of the function inputs.
7839**
7840** P1 is a 32-bit bitmask indicating whether or not each argument to the
7841** function was determined to be constant at compile time. If the first
7842** argument was constant then bit 0 of P1 is set. This is used to determine
7843** whether meta data associated with a user function argument using the
7844** sqlite3_set_auxdata() API may be safely retained until the next
7845** invocation of this opcode.
7846**
drh920cf592019-10-30 16:29:02 +00007847** See also: AggStep, AggFinal, PureFunc
drh3e34eab2017-07-19 19:48:40 +00007848*/
drh920cf592019-10-30 16:29:02 +00007849/* Opcode: PureFunc P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00007850** Synopsis: r[P3]=func(r[P2@NP])
drh920cf592019-10-30 16:29:02 +00007851**
7852** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7853** contains a pointer to the function to be run) with arguments taken
7854** from register P2 and successors. The number of arguments is in
7855** the sqlite3_context object that P4 points to.
7856** The result of the function is stored
7857** in register P3. Register P3 must not be one of the function inputs.
7858**
7859** P1 is a 32-bit bitmask indicating whether or not each argument to the
7860** function was determined to be constant at compile time. If the first
7861** argument was constant then bit 0 of P1 is set. This is used to determine
7862** whether meta data associated with a user function argument using the
7863** sqlite3_set_auxdata() API may be safely retained until the next
7864** invocation of this opcode.
7865**
7866** This opcode works exactly like OP_Function. The only difference is in
7867** its name. This opcode is used in places where the function must be
7868** purely non-deterministic. Some built-in date/time functions can be
7869** either determinitic of non-deterministic, depending on their arguments.
7870** When those function are used in a non-deterministic way, they will check
7871** to see if they were called using OP_PureFunc instead of OP_Function, and
7872** if they were, they throw an error.
7873**
7874** See also: AggStep, AggFinal, Function
7875*/
mistachkin758784d2018-07-25 15:12:29 +00007876case OP_PureFunc: /* group */
7877case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007878 int i;
7879 sqlite3_context *pCtx;
7880
7881 assert( pOp->p4type==P4_FUNCCTX );
7882 pCtx = pOp->p4.pCtx;
7883
7884 /* If this function is inside of a trigger, the register array in aMem[]
7885 ** might change from one evaluation to the next. The next block of code
7886 ** checks to see if the register array has changed, and if so it
7887 ** reinitializes the relavant parts of the sqlite3_context object */
7888 pOut = &aMem[pOp->p3];
7889 if( pCtx->pOut != pOut ){
drh920cf592019-10-30 16:29:02 +00007890 pCtx->pVdbe = p;
drh3e34eab2017-07-19 19:48:40 +00007891 pCtx->pOut = pOut;
7892 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7893 }
drh920cf592019-10-30 16:29:02 +00007894 assert( pCtx->pVdbe==p );
drh3e34eab2017-07-19 19:48:40 +00007895
7896 memAboutToChange(p, pOut);
7897#ifdef SQLITE_DEBUG
7898 for(i=0; i<pCtx->argc; i++){
7899 assert( memIsValid(pCtx->argv[i]) );
7900 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7901 }
7902#endif
7903 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007904 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007905 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7906
7907 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007908 if( pCtx->isError ){
7909 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007910 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7911 rc = pCtx->isError;
7912 }
7913 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007914 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007915 if( rc ) goto abort_due_to_error;
7916 }
7917
7918 /* Copy the result of the function into register P3 */
7919 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7920 sqlite3VdbeChangeEncoding(pOut, encoding);
7921 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7922 }
7923
7924 REGISTER_TRACE(pOp->p3, pOut);
7925 UPDATE_MAX_BLOBSIZE(pOut);
7926 break;
7927}
7928
drhf259df52017-12-27 20:38:35 +00007929/* Opcode: Trace P1 P2 * P4 *
7930**
7931** Write P4 on the statement trace output if statement tracing is
7932** enabled.
7933**
7934** Operand P1 must be 0x7fffffff and P2 must positive.
7935*/
drh74588ce2017-09-13 00:13:05 +00007936/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007937** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007938**
7939** Programs contain a single instance of this opcode as the very first
7940** opcode.
drh949f9cd2008-01-12 21:35:57 +00007941**
7942** If tracing is enabled (by the sqlite3_trace()) interface, then
7943** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007944** Or if P4 is blank, use the string returned by sqlite3_sql().
7945**
7946** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007947**
7948** Increment the value of P1 so that OP_Once opcodes will jump the
7949** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007950**
7951** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7952** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007953*/
drhf259df52017-12-27 20:38:35 +00007954case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007955case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007956 int i;
drhb9f47992018-01-24 12:14:43 +00007957#ifndef SQLITE_OMIT_TRACE
7958 char *zTrace;
7959#endif
drh5fe63bf2016-07-25 02:42:22 +00007960
7961 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7962 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7963 **
7964 ** This assert() provides evidence for:
7965 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7966 ** would have been returned by the legacy sqlite3_trace() interface by
7967 ** using the X argument when X begins with "--" and invoking
7968 ** sqlite3_expanded_sql(P) otherwise.
7969 */
7970 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007971
7972 /* OP_Init is always instruction 0 */
7973 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007974
drhaceb31b2014-02-08 01:40:27 +00007975#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007976 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007977 && !p->doingRerun
7978 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7979 ){
drh3d2a5292016-07-13 22:55:01 +00007980#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007981 if( db->mTrace & SQLITE_TRACE_LEGACY ){
drh5fe63bf2016-07-25 02:42:22 +00007982 char *z = sqlite3VdbeExpandSql(p, zTrace);
drh08b92082020-08-10 14:18:00 +00007983 db->trace.xLegacy(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007984 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007985 }else
drh3d2a5292016-07-13 22:55:01 +00007986#endif
drh7adbcff2017-03-20 15:29:28 +00007987 if( db->nVdbeExec>1 ){
7988 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
drh08b92082020-08-10 14:18:00 +00007989 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
drh7adbcff2017-03-20 15:29:28 +00007990 sqlite3DbFree(db, z);
7991 }else{
drh08b92082020-08-10 14:18:00 +00007992 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007993 }
drh949f9cd2008-01-12 21:35:57 +00007994 }
drh8f8b2312013-10-18 20:03:43 +00007995#ifdef SQLITE_USE_FCNTL_TRACE
7996 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7997 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007998 int j;
7999 for(j=0; j<db->nDb; j++){
8000 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
8001 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00008002 }
8003 }
8004#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00008005#ifdef SQLITE_DEBUG
8006 if( (db->flags & SQLITE_SqlTrace)!=0
8007 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
8008 ){
8009 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
8010 }
8011#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00008012#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00008013 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00008014 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00008015 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00008016 for(i=1; i<p->nOp; i++){
8017 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
8018 }
8019 pOp->p1 = 0;
8020 }
8021 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00008022 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00008023 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00008024}
drh949f9cd2008-01-12 21:35:57 +00008025
drh28935362013-12-07 20:39:19 +00008026#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00008027/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00008028**
8029** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00008030** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8031** to values currently held in registers. TK_COLUMN terms in the P4
8032** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00008033*/
8034case OP_CursorHint: {
8035 VdbeCursor *pC;
8036
8037 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8038 assert( pOp->p4type==P4_EXPR );
8039 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00008040 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00008041 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00008042 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
8043 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00008044 }
drh28935362013-12-07 20:39:19 +00008045 break;
8046}
8047#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00008048
drh4031baf2018-05-28 17:31:20 +00008049#ifdef SQLITE_DEBUG
8050/* Opcode: Abortable * * * * *
8051**
8052** Verify that an Abort can happen. Assert if an Abort at this point
8053** might cause database corruption. This opcode only appears in debugging
8054** builds.
8055**
8056** An Abort is safe if either there have been no writes, or if there is
8057** an active statement journal.
8058*/
8059case OP_Abortable: {
8060 sqlite3VdbeAssertAbortable(p);
8061 break;
8062}
8063#endif
8064
drh13d79502019-12-23 02:18:49 +00008065#ifdef SQLITE_DEBUG
drh3aef2fb2020-01-02 17:46:02 +00008066/* Opcode: ReleaseReg P1 P2 P3 * P5
drh13d79502019-12-23 02:18:49 +00008067** Synopsis: release r[P1@P2] mask P3
8068**
8069** Release registers from service. Any content that was in the
8070** the registers is unreliable after this opcode completes.
8071**
8072** The registers released will be the P2 registers starting at P1,
8073** except if bit ii of P3 set, then do not release register P1+ii.
8074** In other words, P3 is a mask of registers to preserve.
8075**
8076** Releasing a register clears the Mem.pScopyFrom pointer. That means
8077** that if the content of the released register was set using OP_SCopy,
8078** a change to the value of the source register for the OP_SCopy will no longer
8079** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8080**
drh3aef2fb2020-01-02 17:46:02 +00008081** If P5 is set, then all released registers have their type set
8082** to MEM_Undefined so that any subsequent attempt to read the released
drh13d79502019-12-23 02:18:49 +00008083** register (before it is reinitialized) will generate an assertion fault.
drh3aef2fb2020-01-02 17:46:02 +00008084**
8085** P5 ought to be set on every call to this opcode.
8086** However, there are places in the code generator will release registers
drh13d79502019-12-23 02:18:49 +00008087** before their are used, under the (valid) assumption that the registers
8088** will not be reallocated for some other purpose before they are used and
8089** hence are safe to release.
8090**
8091** This opcode is only available in testing and debugging builds. It is
8092** not generated for release builds. The purpose of this opcode is to help
8093** validate the generated bytecode. This opcode does not actually contribute
8094** to computing an answer.
8095*/
8096case OP_ReleaseReg: {
8097 Mem *pMem;
8098 int i;
8099 u32 constMask;
8100 assert( pOp->p1>0 );
8101 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
8102 pMem = &aMem[pOp->p1];
8103 constMask = pOp->p3;
8104 for(i=0; i<pOp->p2; i++, pMem++){
drh7edce5e2019-12-23 13:24:34 +00008105 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
drh13d79502019-12-23 02:18:49 +00008106 pMem->pScopyFrom = 0;
drh3aef2fb2020-01-02 17:46:02 +00008107 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
drh13d79502019-12-23 02:18:49 +00008108 }
8109 }
8110 break;
8111}
8112#endif
8113
drh91fd4d42008-01-19 20:11:25 +00008114/* Opcode: Noop * * * * *
8115**
8116** Do nothing. This instruction is often useful as a jump
8117** destination.
drh5e00f6c2001-09-13 13:46:56 +00008118*/
drh91fd4d42008-01-19 20:11:25 +00008119/*
8120** The magic Explain opcode are only inserted when explain==2 (which
8121** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8122** This opcode records information from the optimizer. It is the
8123** the same as a no-op. This opcodesnever appears in a real VM program.
8124*/
drh4031baf2018-05-28 17:31:20 +00008125default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00008126 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00008127
drh5e00f6c2001-09-13 13:46:56 +00008128 break;
8129}
8130
8131/*****************************************************************************
8132** The cases of the switch statement above this line should all be indented
8133** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8134** readability. From this point on down, the normal indentation rules are
8135** restored.
8136*****************************************************************************/
8137 }
drh6e142f52000-06-08 13:36:40 +00008138
drh7b396862003-01-01 23:06:20 +00008139#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00008140 {
drh35043cc2018-02-12 20:27:34 +00008141 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00008142 if( endTime>start ) pOrigOp->cycles += endTime - start;
8143 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00008144 }
drh7b396862003-01-01 23:06:20 +00008145#endif
8146
drh6e142f52000-06-08 13:36:40 +00008147 /* The following code adds nothing to the actual functionality
8148 ** of the program. It is only here for testing and debugging.
8149 ** On the other hand, it does burn CPU cycles every time through
8150 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8151 */
8152#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00008153 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00008154
drhcf1023c2007-05-08 20:59:49 +00008155#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00008156 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00008157 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00008158 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00008159 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00008160 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00008161 }
drh7cc84c22016-04-11 13:36:42 +00008162 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00008163 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00008164 }
drh17aceeb2020-01-04 19:12:13 +00008165 if( opProperty==0xff ){
8166 /* Never happens. This code exists to avoid a harmless linkage
8167 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8168 ** used. */
8169 sqlite3VdbeRegisterDump(p);
8170 }
drh75897232000-05-29 14:26:00 +00008171 }
danielk1977b5402fb2005-01-12 07:15:04 +00008172#endif /* SQLITE_DEBUG */
8173#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00008174 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00008175
drha05a7222008-01-19 03:35:58 +00008176 /* If we reach this point, it means that execution is finished with
8177 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00008178 */
drh9467abf2016-02-17 18:44:11 +00008179abort_due_to_error:
drhf56a4bf2020-11-18 21:50:05 +00008180 if( db->mallocFailed ){
8181 rc = SQLITE_NOMEM_BKPT;
8182 }else if( rc==SQLITE_IOERR_CORRUPTFS ){
8183 rc = SQLITE_CORRUPT_BKPT;
8184 }
drha05a7222008-01-19 03:35:58 +00008185 assert( rc );
drh9467abf2016-02-17 18:44:11 +00008186 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
8187 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
8188 }
drha05a7222008-01-19 03:35:58 +00008189 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00008190 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00008191 testcase( sqlite3GlobalConfig.xLog!=0 );
8192 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00008193 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00008194 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00008195 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00008196 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00008197 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00008198 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00008199 }
drh900b31e2007-08-28 02:27:51 +00008200
8201 /* This is the only way out of this procedure. We have to
8202 ** release the mutexes on btrees that were acquired at the
8203 ** top. */
8204vdbe_return:
drhc332e042019-02-12 21:04:33 +00008205#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00008206 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
8207 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00008208 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +00008209 nProgressLimit = LARGEST_UINT64;
drhc332e042019-02-12 21:04:33 +00008210 rc = SQLITE_INTERRUPT;
8211 goto abort_due_to_error;
8212 }
8213 }
8214#endif
drh9b47ee32013-08-20 03:13:51 +00008215 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00008216 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00008217 assert( rc!=SQLITE_OK || nExtraDelete==0
8218 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
8219 );
drhb86ccfb2003-01-28 23:13:10 +00008220 return rc;
8221
drh023ae032007-05-08 12:12:16 +00008222 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8223 ** is encountered.
8224 */
8225too_big:
drh22c17b82015-05-15 04:13:15 +00008226 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00008227 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00008228 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00008229
drh98640a32007-06-07 19:08:32 +00008230 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00008231 */
8232no_mem:
drh4a642b62016-02-05 01:55:27 +00008233 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00008234 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00008235 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00008236 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008237
danielk19776f8a5032004-05-10 10:34:51 +00008238 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00008239 ** flag.
8240 */
8241abort_due_to_interrupt:
dan892edb62020-03-30 13:35:05 +00008242 assert( AtomicLoad(&db->u1.isInterrupted) );
drh56f18732020-06-03 15:59:22 +00008243 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00008244 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008245}