blob: c7ba320ba4d049728b2cf46e67f24a826feb2a52 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drh0fd61352014-02-07 02:29:45 +000012** The code in this file implements the function that runs the
13** bytecode of a prepared statement.
drh75897232000-05-29 14:26:00 +000014**
drhac82fcf2002-09-08 17:23:41 +000015** Various scripts scan this source file in order to generate HTML
16** documentation, headers files, or other derived files. The formatting
17** of the code in this file is, therefore, important. See other comments
18** in this file for details. If in doubt, do not deviate from existing
19** commenting and indentation practices when changing or adding code.
drh75897232000-05-29 14:26:00 +000020*/
21#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000022#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000023
24/*
drh2b4ded92010-09-27 21:09:31 +000025** Invoke this macro on memory cells just prior to changing the
26** value of the cell. This macro verifies that shallow copies are
drh0fd61352014-02-07 02:29:45 +000027** not misused. A shallow copy of a string or blob just copies a
28** pointer to the string or blob, not the content. If the original
29** is changed while the copy is still in use, the string or blob might
30** be changed out from under the copy. This macro verifies that nothing
drhb6e8fd12014-03-06 01:56:33 +000031** like that ever happens.
drh2b4ded92010-09-27 21:09:31 +000032*/
33#ifdef SQLITE_DEBUG
drhe4c88c02012-01-04 12:57:45 +000034# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
drh2b4ded92010-09-27 21:09:31 +000035#else
36# define memAboutToChange(P,M)
37#endif
38
39/*
drh487ab3c2001-11-08 00:45:21 +000040** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000041** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000042** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000043** working correctly. This variable has no function other than to
44** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000045*/
drh0f7eb612006-08-08 13:51:43 +000046#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000047int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000048#endif
drh487ab3c2001-11-08 00:45:21 +000049
drhf6038712004-02-08 18:07:34 +000050/*
51** When this global variable is positive, it gets decremented once before
drhe4c88c02012-01-04 12:57:45 +000052** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
53** field of the sqlite3 structure is set in order to simulate an interrupt.
drhf6038712004-02-08 18:07:34 +000054**
55** This facility is used for testing purposes only. It does not function
56** in an ordinary build.
57*/
drh0f7eb612006-08-08 13:51:43 +000058#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000060#endif
drh1350b032002-02-27 19:00:20 +000061
danielk19777e18c252004-05-25 11:47:24 +000062/*
drh6bf89572004-11-03 16:27:01 +000063** The next global variable is incremented each type the OP_Sort opcode
64** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000065** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000066** has no function other than to help verify the correct operation of the
67** library.
68*/
drh0f7eb612006-08-08 13:51:43 +000069#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000070int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000071#endif
drh6bf89572004-11-03 16:27:01 +000072
73/*
drhae7e1512007-05-02 16:51:59 +000074** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000075** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000076** use this information to make sure that the zero-blob functionality
77** is working correctly. This variable has no function other than to
78** help verify the correct operation of the library.
79*/
80#ifdef SQLITE_TEST
81int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000082static void updateMaxBlobsize(Mem *p){
83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
84 sqlite3_max_blobsize = p->n;
85 }
86}
drhae7e1512007-05-02 16:51:59 +000087#endif
88
89/*
drh9b1c62d2011-03-30 21:04:43 +000090** This macro evaluates to true if either the update hook or the preupdate
91** hook are enabled for database connect DB.
92*/
93#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh74c33022016-03-30 12:56:55 +000094# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000095#else
drh74c33022016-03-30 12:56:55 +000096# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000097#endif
98
99/*
drh0fd61352014-02-07 02:29:45 +0000100** The next global variable is incremented each time the OP_Found opcode
dan0ff297e2009-09-25 17:03:14 +0000101** is executed. This is used to test whether or not the foreign key
102** operation implemented using OP_FkIsZero is working. This variable
103** has no function other than to help verify the correct operation of the
104** library.
105*/
106#ifdef SQLITE_TEST
107int sqlite3_found_count = 0;
108#endif
109
110/*
drhb7654112008-01-12 12:48:07 +0000111** Test a register to see if it exceeds the current maximum blob size.
112** If it does, record the new maximum blob size.
113*/
drhd12602a2016-12-07 15:49:02 +0000114#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
drhca48c902008-01-18 14:08:24 +0000115# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000116#else
117# define UPDATE_MAX_BLOBSIZE(P)
118#endif
119
drh52f11b82020-01-02 13:26:49 +0000120#ifdef SQLITE_DEBUG
121/* This routine provides a convenient place to set a breakpoint during
122** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
123** each opcode is printed. Variables "pc" (program counter) and pOp are
124** available to add conditionals to the breakpoint. GDB example:
125**
126** break test_trace_breakpoint if pc=22
127**
128** Other useful labels for breakpoints include:
129** test_addop_breakpoint(pc,pOp)
130** sqlite3CorruptError(lineno)
131** sqlite3MisuseError(lineno)
132** sqlite3CantopenError(lineno)
133*/
drh22e95fb2020-01-02 14:42:42 +0000134static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
drh52f11b82020-01-02 13:26:49 +0000135 static int n = 0;
136 n++;
137}
138#endif
139
drhb7654112008-01-12 12:48:07 +0000140/*
drh5655c542014-02-19 19:14:34 +0000141** Invoke the VDBE coverage callback, if that callback is defined. This
142** feature is used for test suite validation only and does not appear an
143** production builds.
144**
drhc9065332019-04-01 14:01:21 +0000145** M is the type of branch. I is the direction taken for this instance of
146** the branch.
147**
148** M: 2 - two-way branch (I=0: fall-thru 1: jump )
149** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
150** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
151**
152** In other words, if M is 2, then I is either 0 (for fall-through) or
153** 1 (for when the branch is taken). If M is 3, the I is 0 for an
154** ordinary fall-through, I is 1 if the branch was taken, and I is 2
155** if the result of comparison is NULL. For M=3, I=2 the jump may or
156** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
157** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
158** depending on if the operands are less than, equal, or greater than.
drh4336b0e2014-08-05 00:53:51 +0000159**
160** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000161** generated the VDBE instruction combined with flag bits. The source
162** code line number is in the lower 24 bits of iSrcLine and the upper
163** 8 bytes are flags. The lower three bits of the flags indicate
164** values for I that should never occur. For example, if the branch is
165** always taken, the flags should be 0x05 since the fall-through and
166** alternate branch are never taken. If a branch is never taken then
167** flags should be 0x06 since only the fall-through approach is allowed.
168**
drhc9065332019-04-01 14:01:21 +0000169** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
drh7083a482018-07-10 16:04:04 +0000170** interested in equal or not-equal. In other words, I==0 and I==2
drhc9065332019-04-01 14:01:21 +0000171** should be treated as equivalent
drh7083a482018-07-10 16:04:04 +0000172**
173** Since only a line number is retained, not the filename, this macro
174** only works for amalgamation builds. But that is ok, since these macros
175** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000176*/
177#if !defined(SQLITE_VDBE_COVERAGE)
178# define VdbeBranchTaken(I,M)
179#else
drh5655c542014-02-19 19:14:34 +0000180# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000181 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
182 u8 mNever;
183 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
184 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
185 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
186 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
187 I = 1<<I;
188 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
189 ** the flags indicate directions that the branch can never go. If
190 ** a branch really does go in one of those directions, assert right
191 ** away. */
192 mNever = iSrcLine >> 24;
193 assert( (I & mNever)==0 );
194 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
drhc9065332019-04-01 14:01:21 +0000195 /* Invoke the branch coverage callback with three arguments:
196 ** iSrcLine - the line number of the VdbeCoverage() macro, with
197 ** flags removed.
198 ** I - Mask of bits 0x07 indicating which cases are are
199 ** fulfilled by this instance of the jump. 0x01 means
200 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
201 ** impossible cases (ex: if the comparison is never NULL)
202 ** are filled in automatically so that the coverage
203 ** measurement logic does not flag those impossible cases
204 ** as missed coverage.
205 ** M - Type of jump. Same as M argument above
206 */
drh7083a482018-07-10 16:04:04 +0000207 I |= mNever;
208 if( M==2 ) I |= 0x04;
209 if( M==4 ){
210 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000211 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000212 }
drh7083a482018-07-10 16:04:04 +0000213 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
214 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000215 }
drh688852a2014-02-17 22:40:43 +0000216#endif
217
218/*
danielk1977bd7e4602004-05-24 07:34:48 +0000219** An ephemeral string value (signified by the MEM_Ephem flag) contains
220** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000221** is responsible for deallocating that string. Because the register
222** does not control the string, it might be deleted without the register
223** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000224**
225** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000226** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000227** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000228*/
drhb21c8cd2007-08-21 19:33:56 +0000229#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000230 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000231 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000232
dan689ab892011-08-12 15:02:00 +0000233/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000234#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000235
236/*
drhdfe88ec2008-11-03 20:55:06 +0000237** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000238** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000239*/
drhdfe88ec2008-11-03 20:55:06 +0000240static VdbeCursor *allocateCursor(
241 Vdbe *p, /* The virtual machine */
242 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000243 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000244 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000245 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000246){
247 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000248 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000249 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000250 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000251 **
252 ** * Sometimes cursor numbers are used for a couple of different
253 ** purposes in a vdbe program. The different uses might require
254 ** different sized allocations. Memory cells provide growable
255 ** allocations.
256 **
257 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
258 ** be freed lazily via the sqlite3_release_memory() API. This
259 ** minimizes the number of malloc calls made by the system.
260 **
drh3cdce922016-03-21 00:30:40 +0000261 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000262 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
263 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000264 */
drh9f6168b2016-03-19 23:32:58 +0000265 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000266
danielk19775f096132008-03-28 15:44:09 +0000267 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000268 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000269 nByte =
drh5cc10232013-11-21 01:04:02 +0000270 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000271 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000272
drh9f6168b2016-03-19 23:32:58 +0000273 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000274 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
dan97c8cb32019-01-01 18:00:17 +0000275 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
276 ** is clear. Otherwise, if this is an ephemeral cursor created by
277 ** OP_OpenDup, the cursor will not be closed and will still be part
278 ** of a BtShared.pCursor list. */
dana5129722019-05-03 18:50:24 +0000279 if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
danielk1977be718892006-06-23 08:05:19 +0000280 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000281 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000282 }
drh322f2852014-09-19 00:43:39 +0000283 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000284 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000285 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000286 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000287 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000288 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000289 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000290 if( eCurType==CURTYPE_BTREE ){
291 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000292 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000293 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000294 }
danielk197794eb6a12005-12-15 15:22:08 +0000295 }
drh4774b132004-06-12 20:12:51 +0000296 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000297}
298
danielk19773d1bfea2004-05-14 11:00:53 +0000299/*
drh8a3884e2019-05-29 21:18:27 +0000300** The string in pRec is known to look like an integer and to have a
301** floating point value of rValue. Return true and set *piValue to the
302** integer value if the string is in range to be an integer. Otherwise,
303** return false.
304*/
305static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
306 i64 iValue = (double)rValue;
307 if( sqlite3RealSameAsInt(rValue,iValue) ){
drhc285ded2019-06-10 18:33:16 +0000308 *piValue = iValue;
309 return 1;
drh8a3884e2019-05-29 21:18:27 +0000310 }
311 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
312}
313
314/*
drh29d72102006-02-09 22:13:41 +0000315** Try to convert a value into a numeric representation if we can
316** do so without loss of information. In other words, if the string
317** looks like a number, convert it into a number. If it does not
318** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000319**
320** If the bTryForInt flag is true, then extra effort is made to give
321** an integer representation. Strings that look like floating point
322** values but which have no fractional component (example: '48.00')
323** will have a MEM_Int representation when bTryForInt is true.
324**
325** If bTryForInt is false, then if the input string contains a decimal
326** point or exponential notation, the result is only MEM_Real, even
327** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000328*/
drhbd9507c2014-08-23 17:21:37 +0000329static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000330 double rValue;
drh975b4c62014-07-26 16:47:23 +0000331 u8 enc = pRec->enc;
drh8a3884e2019-05-29 21:18:27 +0000332 int rc;
drh169f0772019-05-02 21:36:26 +0000333 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
drh8a3884e2019-05-29 21:18:27 +0000334 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
drh9a278222019-06-07 22:26:08 +0000335 if( rc<=0 ) return;
drh8a3884e2019-05-29 21:18:27 +0000336 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
drh975b4c62014-07-26 16:47:23 +0000337 pRec->flags |= MEM_Int;
338 }else{
drh74eaba42014-09-18 17:52:15 +0000339 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000340 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000341 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000342 }
drh06b3bd52018-02-01 01:13:33 +0000343 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
344 ** string representation after computing a numeric equivalent, because the
345 ** string representation might not be the canonical representation for the
346 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
347 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000348}
349
350/*
drh8a512562005-11-14 22:29:05 +0000351** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000352**
drh8a512562005-11-14 22:29:05 +0000353** SQLITE_AFF_INTEGER:
354** SQLITE_AFF_REAL:
355** SQLITE_AFF_NUMERIC:
356** Try to convert pRec to an integer representation or a
357** floating-point representation if an integer representation
358** is not possible. Note that the integer representation is
359** always preferred, even if the affinity is REAL, because
360** an integer representation is more space efficient on disk.
361**
362** SQLITE_AFF_TEXT:
363** Convert pRec to a text representation.
364**
drh05883a32015-06-02 15:32:08 +0000365** SQLITE_AFF_BLOB:
drh96fb16e2019-08-06 14:37:24 +0000366** SQLITE_AFF_NONE:
drh8a512562005-11-14 22:29:05 +0000367** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000368*/
drh17435752007-08-16 04:30:38 +0000369static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000370 Mem *pRec, /* The value to apply affinity to */
371 char affinity, /* The affinity to be applied */
372 u8 enc /* Use this text encoding */
373){
drh7ea31cc2014-09-18 14:36:00 +0000374 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000375 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
376 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000377 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000378 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000379 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000380 }else{
381 sqlite3VdbeIntegerAffinity(pRec);
382 }
drh17c40292004-07-21 02:53:29 +0000383 }
drh7ea31cc2014-09-18 14:36:00 +0000384 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000385 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000386 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000387 ** representation. It would be harmless to repeat the conversion if
388 ** there is already a string rep, but it is pointless to waste those
389 ** CPU cycles. */
390 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
drh169f0772019-05-02 21:36:26 +0000391 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
drh3242c692019-05-04 01:29:13 +0000392 testcase( pRec->flags & MEM_Int );
393 testcase( pRec->flags & MEM_Real );
394 testcase( pRec->flags & MEM_IntReal );
drha3fa1402016-04-29 02:55:05 +0000395 sqlite3VdbeMemStringify(pRec, enc, 1);
396 }
danielk19773d1bfea2004-05-14 11:00:53 +0000397 }
drh169f0772019-05-02 21:36:26 +0000398 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
danielk19773d1bfea2004-05-14 11:00:53 +0000399 }
400}
401
danielk1977aee18ef2005-03-09 12:26:50 +0000402/*
drh29d72102006-02-09 22:13:41 +0000403** Try to convert the type of a function argument or a result column
404** into a numeric representation. Use either INTEGER or REAL whichever
405** is appropriate. But only do the conversion if it is possible without
406** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000407*/
408int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000409 int eType = sqlite3_value_type(pVal);
410 if( eType==SQLITE_TEXT ){
411 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000412 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000413 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000414 }
drh1b27b8c2014-02-10 03:21:57 +0000415 return eType;
drh29d72102006-02-09 22:13:41 +0000416}
417
418/*
danielk1977aee18ef2005-03-09 12:26:50 +0000419** Exported version of applyAffinity(). This one works on sqlite3_value*,
420** not the internal Mem* type.
421*/
danielk19771e536952007-08-16 10:09:01 +0000422void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000423 sqlite3_value *pVal,
424 u8 affinity,
425 u8 enc
426){
drhb21c8cd2007-08-21 19:33:56 +0000427 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000428}
429
drh3d1d90a2014-03-24 15:00:15 +0000430/*
drhf1a89ed2014-08-23 17:41:15 +0000431** pMem currently only holds a string type (or maybe a BLOB that we can
432** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000433** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000434** accordingly.
435*/
436static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
drh9a278222019-06-07 22:26:08 +0000437 int rc;
438 sqlite3_int64 ix;
drh169f0772019-05-02 21:36:26 +0000439 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
drhf1a89ed2014-08-23 17:41:15 +0000440 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh0814acd2019-01-25 20:09:04 +0000441 ExpandBlob(pMem);
drh9a278222019-06-07 22:26:08 +0000442 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
443 if( rc<=0 ){
444 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
445 pMem->u.i = ix;
446 return MEM_Int;
447 }else{
448 return MEM_Real;
449 }
450 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
451 pMem->u.i = ix;
drhf1a89ed2014-08-23 17:41:15 +0000452 return MEM_Int;
453 }
454 return MEM_Real;
455}
456
457/*
drh3d1d90a2014-03-24 15:00:15 +0000458** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
459** none.
460**
461** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000462** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000463*/
464static u16 numericType(Mem *pMem){
drh169f0772019-05-02 21:36:26 +0000465 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +0000466 testcase( pMem->flags & MEM_Int );
467 testcase( pMem->flags & MEM_Real );
468 testcase( pMem->flags & MEM_IntReal );
drh169f0772019-05-02 21:36:26 +0000469 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
drh3d1d90a2014-03-24 15:00:15 +0000470 }
471 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drh3242c692019-05-04 01:29:13 +0000472 testcase( pMem->flags & MEM_Str );
473 testcase( pMem->flags & MEM_Blob );
drhf1a89ed2014-08-23 17:41:15 +0000474 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000475 }
476 return 0;
477}
478
danielk1977b5402fb2005-01-12 07:15:04 +0000479#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000480/*
danielk1977ca6b2912004-05-21 10:49:47 +0000481** Write a nice string representation of the contents of cell pMem
482** into buffer zBuf, length nBuf.
483*/
drh5ca06322020-01-06 19:23:41 +0000484void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
danielk1977ca6b2912004-05-21 10:49:47 +0000485 int f = pMem->flags;
drh57196282004-10-06 15:41:16 +0000486 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977ca6b2912004-05-21 10:49:47 +0000487 if( f&MEM_Blob ){
488 int i;
489 char c;
490 if( f & MEM_Dyn ){
491 c = 'z';
492 assert( (f & (MEM_Static|MEM_Ephem))==0 );
493 }else if( f & MEM_Static ){
494 c = 't';
495 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
496 }else if( f & MEM_Ephem ){
497 c = 'e';
498 assert( (f & (MEM_Static|MEM_Dyn))==0 );
499 }else{
500 c = 's';
501 }
drhded33cc2020-01-08 11:36:30 +0000502 sqlite3_str_appendf(pStr, "%cx[", c);
drhefb5f9a2019-08-30 21:52:13 +0000503 for(i=0; i<25 && i<pMem->n; i++){
drh5ca06322020-01-06 19:23:41 +0000504 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
danielk1977ca6b2912004-05-21 10:49:47 +0000505 }
drh5ca06322020-01-06 19:23:41 +0000506 sqlite3_str_appendf(pStr, "|");
drhefb5f9a2019-08-30 21:52:13 +0000507 for(i=0; i<25 && i<pMem->n; i++){
danielk1977ca6b2912004-05-21 10:49:47 +0000508 char z = pMem->z[i];
drh5ca06322020-01-06 19:23:41 +0000509 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
danielk1977ca6b2912004-05-21 10:49:47 +0000510 }
drh5ca06322020-01-06 19:23:41 +0000511 sqlite3_str_appendf(pStr,"]");
drhfdf972a2007-05-02 13:30:27 +0000512 if( f & MEM_Zero ){
drh5ca06322020-01-06 19:23:41 +0000513 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
drhfdf972a2007-05-02 13:30:27 +0000514 }
danielk1977b1bc9532004-05-22 03:05:33 +0000515 }else if( f & MEM_Str ){
drh5ca06322020-01-06 19:23:41 +0000516 int j;
mistachkin59171172020-01-18 19:02:20 +0000517 u8 c;
danielk1977b1bc9532004-05-22 03:05:33 +0000518 if( f & MEM_Dyn ){
drh5ca06322020-01-06 19:23:41 +0000519 c = 'z';
danielk1977b1bc9532004-05-22 03:05:33 +0000520 assert( (f & (MEM_Static|MEM_Ephem))==0 );
521 }else if( f & MEM_Static ){
drh5ca06322020-01-06 19:23:41 +0000522 c = 't';
danielk1977b1bc9532004-05-22 03:05:33 +0000523 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
524 }else if( f & MEM_Ephem ){
drh5ca06322020-01-06 19:23:41 +0000525 c = 'e';
danielk1977b1bc9532004-05-22 03:05:33 +0000526 assert( (f & (MEM_Static|MEM_Dyn))==0 );
527 }else{
drh5ca06322020-01-06 19:23:41 +0000528 c = 's';
danielk1977b1bc9532004-05-22 03:05:33 +0000529 }
drh5ca06322020-01-06 19:23:41 +0000530 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
drhefb5f9a2019-08-30 21:52:13 +0000531 for(j=0; j<25 && j<pMem->n; j++){
mistachkin59171172020-01-18 19:02:20 +0000532 c = pMem->z[j];
drh5ca06322020-01-06 19:23:41 +0000533 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
danielk1977b1bc9532004-05-22 03:05:33 +0000534 }
drh5ca06322020-01-06 19:23:41 +0000535 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
danielk1977ca6b2912004-05-21 10:49:47 +0000536 }
danielk1977ca6b2912004-05-21 10:49:47 +0000537}
538#endif
539
drh5b6afba2008-01-05 16:29:28 +0000540#ifdef SQLITE_DEBUG
541/*
drhcc6408f2020-03-20 16:13:41 +0000542** Print the value of a Mem object on standard output.
543** Used for tracing and for interactive debugging only.
drh5b6afba2008-01-05 16:29:28 +0000544*/
drhcc6408f2020-03-20 16:13:41 +0000545static void memPrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000546 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000547 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000548 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000549 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000550 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000551 printf(" si:%lld", p->u.i);
drh169f0772019-05-02 21:36:26 +0000552 }else if( (p->flags & (MEM_IntReal))!=0 ){
drh83a1daf2019-05-01 18:59:33 +0000553 printf(" ir:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000554 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000555 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000556#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000557 }else if( p->flags & MEM_Real ){
drhd1c472d2019-10-03 14:51:59 +0000558 printf(" r:%.17g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000559#endif
drh9d67afc2018-08-29 20:24:03 +0000560 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000561 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000562 }else{
drh5ca06322020-01-06 19:23:41 +0000563 StrAccum acc;
564 char zBuf[1000];
565 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
566 sqlite3VdbeMemPrettyPrint(p, &acc);
567 printf(" %s", sqlite3StrAccumFinish(&acc));
drh5b6afba2008-01-05 16:29:28 +0000568 }
dan5b6c8e42016-01-30 15:46:03 +0000569 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000570}
drhcc6408f2020-03-20 16:13:41 +0000571/* Print N Mem objects beginning with p. Used for interactive debugging */
572void sqlite3MemPrint(Mem *p, int N){
573 int i;
574 for(i=0; i<N; i++){
575 if( N>1 ) printf("%3d:", i);
576 memPrint(p+i);
577 printf("\n");
578 }
579 fflush(stdout);
580}
drh84e55a82013-11-13 17:58:23 +0000581static void registerTrace(int iReg, Mem *p){
drh22e95fb2020-01-02 14:42:42 +0000582 printf("R[%d] = ", iReg);
drhcc6408f2020-03-20 16:13:41 +0000583 memPrint(p);
drh22e95fb2020-01-02 14:42:42 +0000584 if( p->pScopyFrom ){
585 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
586 }
drh84e55a82013-11-13 17:58:23 +0000587 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000588 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000589}
590#endif
591
592#ifdef SQLITE_DEBUG
drh22e95fb2020-01-02 14:42:42 +0000593/*
594** Show the values of all registers in the virtual machine. Used for
595** interactive debugging.
596*/
597void sqlite3VdbeRegisterDump(Vdbe *v){
598 int i;
599 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
600}
601#endif /* SQLITE_DEBUG */
602
603
604#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000605# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000606#else
607# define REGISTER_TRACE(R,M)
608#endif
609
danielk197784ac9d02004-05-18 09:58:06 +0000610
drh7b396862003-01-01 23:06:20 +0000611#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000612
613/*
614** hwtime.h contains inline assembler code for implementing
615** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000616*/
shane9bcbdad2008-05-29 20:22:37 +0000617#include "hwtime.h"
618
drh7b396862003-01-01 23:06:20 +0000619#endif
620
danielk1977fd7f0452008-12-17 17:30:26 +0000621#ifndef NDEBUG
622/*
623** This function is only called from within an assert() expression. It
624** checks that the sqlite3.nTransaction variable is correctly set to
625** the number of non-transaction savepoints currently in the
626** linked list starting at sqlite3.pSavepoint.
627**
628** Usage:
629**
630** assert( checkSavepointCount(db) );
631*/
632static int checkSavepointCount(sqlite3 *db){
633 int n = 0;
634 Savepoint *p;
635 for(p=db->pSavepoint; p; p=p->pNext) n++;
636 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
637 return 1;
638}
639#endif
640
drh27a348c2015-04-13 19:14:06 +0000641/*
642** Return the register of pOp->p2 after first preparing it to be
643** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000644*/
645static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
646 sqlite3VdbeMemSetNull(pOut);
647 pOut->flags = MEM_Int;
648 return pOut;
649}
drh27a348c2015-04-13 19:14:06 +0000650static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
651 Mem *pOut;
652 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000653 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000654 pOut = &p->aMem[pOp->p2];
655 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000656 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000657 return out2PrereleaseWithClear(pOut);
658 }else{
659 pOut->flags = MEM_Int;
660 return pOut;
661 }
drh27a348c2015-04-13 19:14:06 +0000662}
663
drhb9755982010-07-24 16:34:37 +0000664
665/*
drh0fd61352014-02-07 02:29:45 +0000666** Execute as much of a VDBE program as we can.
667** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000668*/
danielk19774adee202004-05-08 08:23:19 +0000669int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000670 Vdbe *p /* The VDBE */
671){
drhbbe879d2009-11-14 18:04:35 +0000672 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000673 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000674#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
675 Op *pOrigOp; /* Value of pOp at the top of the loop */
676#endif
drhb89aeb62016-01-27 15:49:32 +0000677#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000678 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000679#endif
drhb86ccfb2003-01-28 23:13:10 +0000680 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000681 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000682 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000683 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000684 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000685 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000686#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000687 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000688#endif
drha6c2ed92009-11-14 23:22:23 +0000689 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000690 Mem *pIn1 = 0; /* 1st input operand */
691 Mem *pIn2 = 0; /* 2nd input operand */
692 Mem *pIn3 = 0; /* 3rd input operand */
693 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000694#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000695 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000696#endif
drh856c1032009-06-02 15:21:42 +0000697 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000698
drhca48c902008-01-18 14:08:24 +0000699 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000700 sqlite3VdbeEnter(p);
drh82642f82019-02-12 22:58:32 +0000701#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
702 if( db->xProgress ){
703 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
704 assert( 0 < db->nProgressOps );
705 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
706 }else{
707 nProgressLimit = 0xffffffff;
708 }
709#endif
danielk19772e588c72005-12-09 14:25:08 +0000710 if( p->rc==SQLITE_NOMEM ){
711 /* This happens if a malloc() inside a call to sqlite3_column_text() or
712 ** sqlite3_column_text16() failed. */
713 goto no_mem;
714 }
drhcbd8db32015-08-20 17:18:32 +0000715 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000716 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000717 p->iCurrentTime = 0;
drh8e8c2f12020-03-19 21:17:11 +0000718 assert( p->explain==SQLITE_STMTMODE_RUN );
drhcc6408f2020-03-20 16:13:41 +0000719 p->nRes = 0;
drha4afb652005-07-09 02:16:02 +0000720 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000721 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000722 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000723#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000724 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000725 if( p->pc==0
726 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
727 ){
drh3c23a882007-01-09 14:01:13 +0000728 int i;
drh84e55a82013-11-13 17:58:23 +0000729 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000730 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000731 if( p->db->flags & SQLITE_VdbeListing ){
732 printf("VDBE Program Listing:\n");
733 for(i=0; i<p->nOp; i++){
734 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
735 }
drh3c23a882007-01-09 14:01:13 +0000736 }
drh84e55a82013-11-13 17:58:23 +0000737 if( p->db->flags & SQLITE_VdbeEQP ){
738 for(i=0; i<p->nOp; i++){
739 if( aOp[i].opcode==OP_Explain ){
740 if( once ) printf("VDBE Query Plan:\n");
741 printf("%s\n", aOp[i].p4.z);
742 once = 0;
743 }
744 }
745 }
746 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000747 }
danielk19772d1d86f2008-06-20 14:59:51 +0000748 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000749#endif
drh9467abf2016-02-17 18:44:11 +0000750 for(pOp=&aOp[p->pc]; 1; pOp++){
751 /* Errors are detected by individual opcodes, with an immediate
752 ** jumps to abort_due_to_error. */
753 assert( rc==SQLITE_OK );
754
drhf56fa462015-04-13 21:39:54 +0000755 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000756#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000757 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000758#endif
drhbf159fa2013-06-25 22:01:22 +0000759 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000760#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000761 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000762#endif
drh6e142f52000-06-08 13:36:40 +0000763
danielk19778b60e0f2005-01-12 09:10:39 +0000764 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000765 */
danielk19778b60e0f2005-01-12 09:10:39 +0000766#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000767 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000768 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh22e95fb2020-01-02 14:42:42 +0000769 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
drh75897232000-05-29 14:26:00 +0000770 }
drh3f7d4e42004-07-24 14:35:58 +0000771#endif
772
drh6e142f52000-06-08 13:36:40 +0000773
drhf6038712004-02-08 18:07:34 +0000774 /* Check to see if we need to simulate an interrupt. This only happens
775 ** if we have a special test build.
776 */
777#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000778 if( sqlite3_interrupt_count>0 ){
779 sqlite3_interrupt_count--;
780 if( sqlite3_interrupt_count==0 ){
781 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000782 }
783 }
784#endif
785
drh3c657212009-11-17 23:59:58 +0000786 /* Sanity checking on other operands */
787#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000788 {
789 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
790 if( (opProperty & OPFLG_IN1)!=0 ){
791 assert( pOp->p1>0 );
792 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
793 assert( memIsValid(&aMem[pOp->p1]) );
794 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
795 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
796 }
797 if( (opProperty & OPFLG_IN2)!=0 ){
798 assert( pOp->p2>0 );
799 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
800 assert( memIsValid(&aMem[pOp->p2]) );
801 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
802 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
803 }
804 if( (opProperty & OPFLG_IN3)!=0 ){
805 assert( pOp->p3>0 );
806 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
807 assert( memIsValid(&aMem[pOp->p3]) );
808 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
809 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
810 }
811 if( (opProperty & OPFLG_OUT2)!=0 ){
812 assert( pOp->p2>0 );
813 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
814 memAboutToChange(p, &aMem[pOp->p2]);
815 }
816 if( (opProperty & OPFLG_OUT3)!=0 ){
817 assert( pOp->p3>0 );
818 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
819 memAboutToChange(p, &aMem[pOp->p3]);
820 }
drh3c657212009-11-17 23:59:58 +0000821 }
822#endif
drh6dc41482015-04-16 17:31:02 +0000823#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
824 pOrigOp = pOp;
825#endif
drh93952eb2009-11-13 19:43:43 +0000826
drh75897232000-05-29 14:26:00 +0000827 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000828
drh5e00f6c2001-09-13 13:46:56 +0000829/*****************************************************************************
830** What follows is a massive switch statement where each case implements a
831** separate instruction in the virtual machine. If we follow the usual
832** indentation conventions, each case should be indented by 6 spaces. But
833** that is a lot of wasted space on the left margin. So the code within
834** the switch statement will break with convention and be flush-left. Another
835** big comment (similar to this one) will mark the point in the code where
836** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000837**
838** The formatting of each case is important. The makefile for SQLite
839** generates two C files "opcodes.h" and "opcodes.c" by scanning this
840** file looking for lines that begin with "case OP_". The opcodes.h files
841** will be filled with #defines that give unique integer values to each
842** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000843** each string is the symbolic name for the corresponding opcode. If the
844** case statement is followed by a comment of the form "/# same as ... #/"
845** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000846**
drh9cbf3422008-01-17 16:22:13 +0000847** Other keywords in the comment that follows each case are used to
848** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000849** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000850** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000851**
drhac82fcf2002-09-08 17:23:41 +0000852** Documentation about VDBE opcodes is generated by scanning this file
853** for lines of that contain "Opcode:". That line and all subsequent
854** comment lines are used in the generation of the opcode.html documentation
855** file.
856**
857** SUMMARY:
858**
859** Formatting is important to scripts that scan this file.
860** Do not deviate from the formatting style currently in use.
861**
drh5e00f6c2001-09-13 13:46:56 +0000862*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000863
drh9cbf3422008-01-17 16:22:13 +0000864/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000865**
866** An unconditional jump to address P2.
867** The next instruction executed will be
868** the one at index P2 from the beginning of
869** the program.
drhfe705102014-03-06 13:38:37 +0000870**
871** The P1 parameter is not actually used by this opcode. However, it
872** is sometimes set to 1 instead of 0 as a hint to the command-line shell
873** that this Goto is the bottom of a loop and that the lines from P2 down
874** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000875*/
drh9cbf3422008-01-17 16:22:13 +0000876case OP_Goto: { /* jump */
drhd9670ab2019-12-28 01:52:46 +0000877
878#ifdef SQLITE_DEBUG
879 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
880 ** means we should really jump back to the preceeding OP_ReleaseReg
881 ** instruction. */
882 if( pOp->p5 ){
883 assert( pOp->p2 < (int)(pOp - aOp) );
884 assert( pOp->p2 > 1 );
885 pOp = &aOp[pOp->p2 - 2];
886 assert( pOp[1].opcode==OP_ReleaseReg );
887 goto check_for_interrupt;
888 }
889#endif
890
drhf56fa462015-04-13 21:39:54 +0000891jump_to_p2_and_check_for_interrupt:
892 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000893
894 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000895 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000896 ** completion. Check to see if sqlite3_interrupt() has been called
897 ** or if the progress callback needs to be invoked.
898 **
899 ** This code uses unstructured "goto" statements and does not look clean.
900 ** But that is not due to sloppy coding habits. The code is written this
901 ** way for performance, to avoid having to run the interrupt and progress
902 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
903 ** faster according to "valgrind --tool=cachegrind" */
904check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000905 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000906#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
907 /* Call the progress callback if it is configured and the required number
908 ** of VDBE ops have been executed (either since this invocation of
909 ** sqlite3VdbeExec() or since last time the progress callback was called).
910 ** If the progress callback returns non-zero, exit the virtual machine with
911 ** a return code SQLITE_ABORT.
912 */
drhb1af9c62019-02-20 13:55:45 +0000913 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000914 assert( db->nProgressOps!=0 );
drhb1af9c62019-02-20 13:55:45 +0000915 nProgressLimit += db->nProgressOps;
drh400fcba2013-11-14 00:09:48 +0000916 if( db->xProgress(db->pProgressArg) ){
drhc332e042019-02-12 21:04:33 +0000917 nProgressLimit = 0xffffffff;
drh49afe3a2013-07-10 03:05:14 +0000918 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000919 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000920 }
drh49afe3a2013-07-10 03:05:14 +0000921 }
922#endif
923
drh5e00f6c2001-09-13 13:46:56 +0000924 break;
925}
drh75897232000-05-29 14:26:00 +0000926
drh2eb95372008-06-06 15:04:36 +0000927/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000928**
drh2eb95372008-06-06 15:04:36 +0000929** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000930** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000931*/
drhb8475df2011-12-09 16:21:19 +0000932case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000933 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000934 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000935 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000936 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000937 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000938 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000939 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000940
941 /* Most jump operations do a goto to this spot in order to update
942 ** the pOp pointer. */
943jump_to_p2:
944 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000945 break;
946}
947
drh2eb95372008-06-06 15:04:36 +0000948/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000949**
drh81cf13e2014-02-07 18:27:53 +0000950** Jump to the next instruction after the address in register P1. After
951** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000952*/
drh2eb95372008-06-06 15:04:36 +0000953case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000954 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000955 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000956 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000957 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000958 break;
959}
960
drhed71a832014-02-07 19:18:10 +0000961/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000962**
drh5dad9a32014-07-25 18:37:42 +0000963** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000964** located at address P3.
965**
drh5dad9a32014-07-25 18:37:42 +0000966** If P2!=0 then the coroutine implementation immediately follows
967** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000968** address P2.
drh5dad9a32014-07-25 18:37:42 +0000969**
970** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000971*/
972case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000973 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000974 assert( pOp->p2>=0 && pOp->p2<p->nOp );
975 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000976 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000977 assert( !VdbeMemDynamic(pOut) );
978 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000979 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000980 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000981 break;
982}
983
984/* Opcode: EndCoroutine P1 * * * *
985**
drhbc5cf382014-08-06 01:08:07 +0000986** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000987** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000988** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000989**
990** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000991*/
992case OP_EndCoroutine: { /* in1 */
993 VdbeOp *pCaller;
994 pIn1 = &aMem[pOp->p1];
995 assert( pIn1->flags==MEM_Int );
996 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
997 pCaller = &aOp[pIn1->u.i];
998 assert( pCaller->opcode==OP_Yield );
999 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +00001000 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +00001001 pIn1->flags = MEM_Undefined;
1002 break;
1003}
1004
1005/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +00001006**
drh5dad9a32014-07-25 18:37:42 +00001007** Swap the program counter with the value in register P1. This
1008** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +00001009**
drh5dad9a32014-07-25 18:37:42 +00001010** If the coroutine that is launched by this instruction ends with
1011** Yield or Return then continue to the next instruction. But if
1012** the coroutine launched by this instruction ends with
1013** EndCoroutine, then jump to P2 rather than continuing with the
1014** next instruction.
1015**
1016** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +00001017*/
drh81cf13e2014-02-07 18:27:53 +00001018case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +00001019 int pcDest;
drh3c657212009-11-17 23:59:58 +00001020 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +00001021 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +00001022 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +00001023 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +00001024 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +00001025 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +00001026 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +00001027 break;
1028}
1029
drhf9c8ce32013-11-05 13:33:55 +00001030/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00001031** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +00001032**
drhef8662b2011-06-20 21:47:58 +00001033** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +00001034** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1035** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +00001036** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +00001037*/
1038case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +00001039 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +00001040#ifdef SQLITE_DEBUG
1041 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1042#endif
drh5053a792009-02-20 03:02:23 +00001043 if( (pIn3->flags & MEM_Null)==0 ) break;
1044 /* Fall through into OP_Halt */
1045}
drhe00ee6e2008-06-20 15:24:01 +00001046
drhf9c8ce32013-11-05 13:33:55 +00001047/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001048**
drh3d4501e2008-12-04 20:40:10 +00001049** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +00001050** automatically.
drhb19a2bc2001-09-16 00:13:26 +00001051**
drh92f02c32004-09-02 14:57:08 +00001052** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1053** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1054** For errors, it can be some other value. If P1!=0 then P2 will determine
1055** whether or not to rollback the current transaction. Do not rollback
1056** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1057** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +00001058** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +00001059**
drh66a51672008-01-03 00:01:23 +00001060** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +00001061**
drhf9c8ce32013-11-05 13:33:55 +00001062** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1063**
1064** 0: (no change)
1065** 1: NOT NULL contraint failed: P4
1066** 2: UNIQUE constraint failed: P4
1067** 3: CHECK constraint failed: P4
1068** 4: FOREIGN KEY constraint failed: P4
1069**
1070** If P5 is not zero and P4 is NULL, then everything after the ":" is
1071** omitted.
1072**
drh9cfcf5d2002-01-29 18:41:24 +00001073** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001074** every program. So a jump past the last instruction of the program
1075** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001076*/
drh9cbf3422008-01-17 16:22:13 +00001077case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001078 VdbeFrame *pFrame;
1079 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001080
drhf56fa462015-04-13 21:39:54 +00001081 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +00001082#ifdef SQLITE_DEBUG
1083 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1084#endif
dan165921a2009-08-28 18:53:45 +00001085 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +00001086 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001087 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001088 p->pFrame = pFrame->pParent;
1089 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001090 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001091 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001092 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001093 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001094 ** currently being halted. If the p2 instruction of this OP_Halt
1095 ** instruction is set to OE_Ignore, then the sub-program is throwing
1096 ** an IGNORE exception. In this case jump to the address specified
1097 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001098 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001099 }
drhbbe879d2009-11-14 18:04:35 +00001100 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001101 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001102 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001103 break;
1104 }
drh92f02c32004-09-02 14:57:08 +00001105 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001106 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001107 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001108 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001109 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001110 if( pOp->p5 ){
1111 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1112 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001113 testcase( pOp->p5==1 );
1114 testcase( pOp->p5==2 );
1115 testcase( pOp->p5==3 );
1116 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001117 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1118 if( pOp->p4.z ){
1119 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1120 }
drhd9b7ec92013-11-06 14:05:21 +00001121 }else{
drh22c17b82015-05-15 04:13:15 +00001122 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001123 }
drh99f5de72016-04-30 02:59:15 +00001124 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001125 }
drh92f02c32004-09-02 14:57:08 +00001126 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001127 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001128 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001129 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001130 }else{
drhd91c1a12013-02-09 13:58:25 +00001131 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001132 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001133 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001134 }
drh900b31e2007-08-28 02:27:51 +00001135 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001136}
drhc61053b2000-06-04 12:58:36 +00001137
drh4c583122008-01-04 22:01:03 +00001138/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001139** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001140**
drh9cbf3422008-01-17 16:22:13 +00001141** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001142*/
drh27a348c2015-04-13 19:14:06 +00001143case OP_Integer: { /* out2 */
1144 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001145 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001146 break;
1147}
1148
drh4c583122008-01-04 22:01:03 +00001149/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001150** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001151**
drh66a51672008-01-03 00:01:23 +00001152** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001153** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001154*/
drh27a348c2015-04-13 19:14:06 +00001155case OP_Int64: { /* out2 */
1156 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001157 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001158 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001159 break;
1160}
drh4f26d6c2004-05-26 23:25:30 +00001161
drh13573c72010-01-12 17:04:07 +00001162#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001163/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001164** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001165**
drh4c583122008-01-04 22:01:03 +00001166** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001167** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001168*/
drh27a348c2015-04-13 19:14:06 +00001169case OP_Real: { /* same as TK_FLOAT, out2 */
1170 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001171 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001172 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001173 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001174 break;
1175}
drh13573c72010-01-12 17:04:07 +00001176#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001177
drh3c84ddf2008-01-09 02:15:38 +00001178/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001179** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001180**
drh66a51672008-01-03 00:01:23 +00001181** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001182** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001183** this transformation, the length of string P4 is computed and stored
1184** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001185*/
drh27a348c2015-04-13 19:14:06 +00001186case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001187 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001188 pOut = out2Prerelease(p, pOp);
drhea678832008-12-10 19:26:22 +00001189 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001190
1191#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001192 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001193 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001194 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001195 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001196 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001197 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001198 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001199 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001200 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001201 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001202 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001203 }
drh66a51672008-01-03 00:01:23 +00001204 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001205 pOp->p4.z = pOut->z;
1206 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001207 }
danielk197793758c82005-01-21 08:13:14 +00001208#endif
drhbb4957f2008-03-20 14:03:29 +00001209 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001210 goto too_big;
1211 }
drhec722c12019-09-17 21:28:54 +00001212 pOp->opcode = OP_String;
drh2f555112016-04-30 18:10:34 +00001213 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001214 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001215}
drhf4479502004-05-27 03:12:53 +00001216
drhf07cf6e2015-03-06 16:45:16 +00001217/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001218** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001219**
drh9cbf3422008-01-17 16:22:13 +00001220** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001221**
drh44aebff2016-05-02 10:25:42 +00001222** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001223** the datatype of the register P2 is converted to BLOB. The content is
1224** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001225** of a string, as if it had been CAST. In other words:
1226**
1227** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001228*/
drh27a348c2015-04-13 19:14:06 +00001229case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001230 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001231 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001232 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1233 pOut->z = pOp->p4.z;
1234 pOut->n = pOp->p1;
1235 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001236 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001237#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001238 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001239 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001240 pIn3 = &aMem[pOp->p3];
1241 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001242 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001243 }
drh41d2e662015-12-01 21:23:07 +00001244#endif
danielk1977c572ef72004-05-27 09:28:41 +00001245 break;
1246}
1247
drh053a1282012-09-19 21:15:46 +00001248/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001249** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001250**
drhb8475df2011-12-09 16:21:19 +00001251** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001252** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001253** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001254** set to NULL.
1255**
1256** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1257** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1258** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001259*/
drh27a348c2015-04-13 19:14:06 +00001260case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001261 int cnt;
drh053a1282012-09-19 21:15:46 +00001262 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001263 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001264 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001265 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001266 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001267 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001268#ifdef SQLITE_DEBUG
1269 pOut->uTemp = 0;
1270#endif
drhb8475df2011-12-09 16:21:19 +00001271 while( cnt>0 ){
1272 pOut++;
1273 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001274 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001275 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001276 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001277 cnt--;
1278 }
drhf0863fe2005-06-12 21:35:51 +00001279 break;
1280}
1281
drh05a86c52014-02-16 01:55:49 +00001282/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001283** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001284**
1285** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1286** instruction, but do not free any string or blob memory associated with
1287** the register, so that if the value was a string or blob that was
1288** previously copied using OP_SCopy, the copies will continue to be valid.
1289*/
1290case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001291 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001292 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001293 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001294 break;
1295}
drhf0863fe2005-06-12 21:35:51 +00001296
drha5750cf2014-02-07 13:20:31 +00001297/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001298** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001299**
drh9de221d2008-01-05 06:51:30 +00001300** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001301** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001302*/
drh27a348c2015-04-13 19:14:06 +00001303case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001304 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001305 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001306 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001307 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001308 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001309 break;
1310}
1311
drheaf52d82010-05-12 13:50:23 +00001312/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001313** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001314**
drheaf52d82010-05-12 13:50:23 +00001315** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001316**
drh0fd61352014-02-07 02:29:45 +00001317** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001318** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001319*/
drh27a348c2015-04-13 19:14:06 +00001320case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001321 Mem *pVar; /* Value being transferred */
1322
drheaf52d82010-05-12 13:50:23 +00001323 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001324 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001325 pVar = &p->aVar[pOp->p1 - 1];
1326 if( sqlite3VdbeMemTooBig(pVar) ){
1327 goto too_big;
drh023ae032007-05-08 12:12:16 +00001328 }
drh7441df72017-01-09 19:27:04 +00001329 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001330 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1331 memcpy(pOut, pVar, MEMCELLSIZE);
1332 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1333 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001334 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001335 break;
1336}
danielk1977295ba552004-05-19 10:34:51 +00001337
drhb21e7c72008-06-22 12:37:57 +00001338/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001339** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001340**
drh079a3072014-03-19 14:10:55 +00001341** Move the P3 values in register P1..P1+P3-1 over into
1342** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001343** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001344** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1345** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001346*/
drhe1349cb2008-04-01 00:36:10 +00001347case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001348 int n; /* Number of registers left to copy */
1349 int p1; /* Register to copy from */
1350 int p2; /* Register to copy to */
1351
drhe09f43f2013-11-21 04:18:31 +00001352 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001353 p1 = pOp->p1;
1354 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001355 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001356 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001357
drha6c2ed92009-11-14 23:22:23 +00001358 pIn1 = &aMem[p1];
1359 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001360 do{
drh9f6168b2016-03-19 23:32:58 +00001361 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1362 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001363 assert( memIsValid(pIn1) );
1364 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001365 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001366#ifdef SQLITE_DEBUG
drh4cbd8472020-01-02 15:02:08 +00001367 pIn1->pScopyFrom = 0;
1368 { int i;
1369 for(i=1; i<p->nMem; i++){
1370 if( aMem[i].pScopyFrom==pIn1 ){
1371 aMem[i].pScopyFrom = pOut;
1372 }
1373 }
drh52043d72011-08-03 16:40:15 +00001374 }
1375#endif
drhbd6789e2015-04-28 14:00:02 +00001376 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001377 REGISTER_TRACE(p2++, pOut);
1378 pIn1++;
1379 pOut++;
drh079a3072014-03-19 14:10:55 +00001380 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001381 break;
1382}
1383
drhe8e4af72012-09-21 00:04:28 +00001384/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001385** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001386**
drhe8e4af72012-09-21 00:04:28 +00001387** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001388**
1389** This instruction makes a deep copy of the value. A duplicate
1390** is made of any string or blob constant. See also OP_SCopy.
1391*/
drhe8e4af72012-09-21 00:04:28 +00001392case OP_Copy: {
1393 int n;
1394
1395 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001396 pIn1 = &aMem[pOp->p1];
1397 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001398 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001399 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001400 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001401 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1402 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001403#ifdef SQLITE_DEBUG
1404 pOut->pScopyFrom = 0;
1405#endif
drhe8e4af72012-09-21 00:04:28 +00001406 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1407 if( (n--)==0 ) break;
1408 pOut++;
1409 pIn1++;
1410 }
drhe1349cb2008-04-01 00:36:10 +00001411 break;
1412}
1413
drhb1fdb2a2008-01-05 04:06:03 +00001414/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001415** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001416**
drh9cbf3422008-01-17 16:22:13 +00001417** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001418**
1419** This instruction makes a shallow copy of the value. If the value
1420** is a string or blob, then the copy is only a pointer to the
1421** original and hence if the original changes so will the copy.
1422** Worse, if the original is deallocated, the copy becomes invalid.
1423** Thus the program must guarantee that the original will not change
1424** during the lifetime of the copy. Use OP_Copy to make a complete
1425** copy.
1426*/
drh26198bb2013-10-31 11:15:09 +00001427case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001428 pIn1 = &aMem[pOp->p1];
1429 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001430 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001431 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001432#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001433 pOut->pScopyFrom = pIn1;
1434 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001435#endif
drh5e00f6c2001-09-13 13:46:56 +00001436 break;
1437}
drh75897232000-05-29 14:26:00 +00001438
drhfed7ac62015-10-15 18:04:59 +00001439/* Opcode: IntCopy P1 P2 * * *
1440** Synopsis: r[P2]=r[P1]
1441**
1442** Transfer the integer value held in register P1 into register P2.
1443**
1444** This is an optimized version of SCopy that works only for integer
1445** values.
1446*/
1447case OP_IntCopy: { /* out2 */
1448 pIn1 = &aMem[pOp->p1];
1449 assert( (pIn1->flags & MEM_Int)!=0 );
1450 pOut = &aMem[pOp->p2];
1451 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1452 break;
1453}
1454
drh9cbf3422008-01-17 16:22:13 +00001455/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001456** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001457**
shane21e7feb2008-05-30 15:59:49 +00001458** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001459** results. This opcode causes the sqlite3_step() call to terminate
1460** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001461** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001462** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001463*/
drh9cbf3422008-01-17 16:22:13 +00001464case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001465 Mem *pMem;
1466 int i;
1467 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001468 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001469 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001470
dan32b09f22009-09-23 17:29:59 +00001471 /* If this statement has violated immediate foreign key constraints, do
1472 ** not return the number of rows modified. And do not RELEASE the statement
1473 ** transaction. It needs to be rolled back. */
1474 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1475 assert( db->flags&SQLITE_CountRows );
1476 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001477 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001478 }
1479
danielk1977bd434552009-03-18 10:33:00 +00001480 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1481 ** DML statements invoke this opcode to return the number of rows
1482 ** modified to the user. This is the only way that a VM that
1483 ** opens a statement transaction may invoke this opcode.
1484 **
1485 ** In case this is such a statement, close any statement transaction
1486 ** opened by this VM before returning control to the user. This is to
1487 ** ensure that statement-transactions are always nested, not overlapping.
1488 ** If the open statement-transaction is not closed here, then the user
1489 ** may step another VM that opens its own statement transaction. This
1490 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001491 **
1492 ** The statement transaction is never a top-level transaction. Hence
1493 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001494 */
1495 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001496 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001497 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001498
drhd4e70eb2008-01-02 00:34:36 +00001499 /* Invalidate all ephemeral cursor row caches */
1500 p->cacheCtr = (p->cacheCtr + 2)|1;
1501
1502 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001503 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001504 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001505 */
drha6c2ed92009-11-14 23:22:23 +00001506 pMem = p->pResultSet = &aMem[pOp->p1];
drhcc6408f2020-03-20 16:13:41 +00001507 p->nRes = pOp->p2;
drhd4e70eb2008-01-02 00:34:36 +00001508 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001509 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001510 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001511 assert( (pMem[i].flags & MEM_Ephem)==0
1512 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001513 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001514 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drh02ff7472019-12-31 12:18:24 +00001515#ifdef SQLITE_DEBUG
1516 /* The registers in the result will not be used again when the
1517 ** prepared statement restarts. This is because sqlite3_column()
1518 ** APIs might have caused type conversions of made other changes to
1519 ** the register values. Therefore, we can go ahead and break any
1520 ** OP_SCopy dependencies. */
1521 pMem[i].pScopyFrom = 0;
1522#endif
drhd4e70eb2008-01-02 00:34:36 +00001523 }
drh28039692008-03-17 16:54:01 +00001524 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001525
drh3d2a5292016-07-13 22:55:01 +00001526 if( db->mTrace & SQLITE_TRACE_ROW ){
1527 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1528 }
1529
drh02ff7472019-12-31 12:18:24 +00001530
drhd4e70eb2008-01-02 00:34:36 +00001531 /* Return SQLITE_ROW
1532 */
drhf56fa462015-04-13 21:39:54 +00001533 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001534 rc = SQLITE_ROW;
1535 goto vdbe_return;
1536}
1537
drh5b6afba2008-01-05 16:29:28 +00001538/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001539** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001540**
drh5b6afba2008-01-05 16:29:28 +00001541** Add the text in register P1 onto the end of the text in
1542** register P2 and store the result in register P3.
1543** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001544**
1545** P3 = P2 || P1
1546**
1547** It is illegal for P1 and P3 to be the same register. Sometimes,
1548** if P3 is the same register as P2, the implementation is able
1549** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001550*/
drh5b6afba2008-01-05 16:29:28 +00001551case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh8a7e11f2019-05-01 15:32:40 +00001552 i64 nByte; /* Total size of the output string or blob */
1553 u16 flags1; /* Initial flags for P1 */
1554 u16 flags2; /* Initial flags for P2 */
danielk19778a6b5412004-05-24 07:04:25 +00001555
drh3c657212009-11-17 23:59:58 +00001556 pIn1 = &aMem[pOp->p1];
1557 pIn2 = &aMem[pOp->p2];
1558 pOut = &aMem[pOp->p3];
drh8a7e11f2019-05-01 15:32:40 +00001559 testcase( pOut==pIn2 );
danielk1977a7a8e142008-02-13 18:25:27 +00001560 assert( pIn1!=pOut );
drh8a7e11f2019-05-01 15:32:40 +00001561 flags1 = pIn1->flags;
1562 testcase( flags1 & MEM_Null );
1563 testcase( pIn2->flags & MEM_Null );
1564 if( (flags1 | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001565 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001566 break;
drh5e00f6c2001-09-13 13:46:56 +00001567 }
drh8a7e11f2019-05-01 15:32:40 +00001568 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
1569 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001570 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001571 }else if( (flags1 & MEM_Zero)!=0 ){
1572 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001573 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001574 }
1575 flags2 = pIn2->flags;
1576 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
1577 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001578 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001579 }else if( (flags2 & MEM_Zero)!=0 ){
1580 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001581 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001582 }
drh5b6afba2008-01-05 16:29:28 +00001583 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001584 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001585 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001586 }
drhdf82afc2019-05-16 01:22:21 +00001587 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001588 goto no_mem;
1589 }
drhc91b2fd2014-03-01 18:13:23 +00001590 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001591 if( pOut!=pIn2 ){
1592 memcpy(pOut->z, pIn2->z, pIn2->n);
drh8a7e11f2019-05-01 15:32:40 +00001593 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
1594 pIn2->flags = flags2;
danielk1977a7a8e142008-02-13 18:25:27 +00001595 }
1596 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh8a7e11f2019-05-01 15:32:40 +00001597 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1598 pIn1->flags = flags1;
drh81316f82013-10-29 20:40:47 +00001599 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001600 pOut->z[nByte+1] = 0;
drhdf82afc2019-05-16 01:22:21 +00001601 pOut->z[nByte+2] = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001602 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001603 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001604 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001605 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001606 break;
1607}
drh75897232000-05-29 14:26:00 +00001608
drh3c84ddf2008-01-09 02:15:38 +00001609/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001610** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001611**
drh60a713c2008-01-21 16:22:45 +00001612** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001613** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001614** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001615*/
drh3c84ddf2008-01-09 02:15:38 +00001616/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001617** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001618**
drh3c84ddf2008-01-09 02:15:38 +00001619**
shane21e7feb2008-05-30 15:59:49 +00001620** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001621** and store the result in register P3.
1622** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001623*/
drh3c84ddf2008-01-09 02:15:38 +00001624/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001625** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001626**
drh60a713c2008-01-21 16:22:45 +00001627** Subtract the value in register P1 from the value in register P2
1628** and store the result in register P3.
1629** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001630*/
drh9cbf3422008-01-17 16:22:13 +00001631/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001632** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001633**
drh60a713c2008-01-21 16:22:45 +00001634** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001635** and store the result in register P3 (P3=P2/P1). If the value in
1636** register P1 is zero, then the result is NULL. If either input is
1637** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001638*/
drh9cbf3422008-01-17 16:22:13 +00001639/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001640** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001641**
drh40864a12013-11-15 18:58:37 +00001642** Compute the remainder after integer register P2 is divided by
1643** register P1 and store the result in register P3.
1644** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001645** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001646*/
drh5b6afba2008-01-05 16:29:28 +00001647case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1648case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1649case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1650case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1651case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh3d1d90a2014-03-24 15:00:15 +00001652 u16 flags; /* Combined MEM_* flags from both inputs */
1653 u16 type1; /* Numeric type of left operand */
1654 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001655 i64 iA; /* Integer value of left operand */
1656 i64 iB; /* Integer value of right operand */
1657 double rA; /* Real value of left operand */
1658 double rB; /* Real value of right operand */
1659
drh3c657212009-11-17 23:59:58 +00001660 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001661 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001662 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001663 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001664 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001665 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001666 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001667 iA = pIn1->u.i;
1668 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001669 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001670 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1671 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1672 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001673 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001674 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001675 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001676 iB /= iA;
drh75897232000-05-29 14:26:00 +00001677 break;
1678 }
drhbf4133c2001-10-13 02:59:08 +00001679 default: {
drh856c1032009-06-02 15:21:42 +00001680 if( iA==0 ) goto arithmetic_result_is_null;
1681 if( iA==-1 ) iA = 1;
1682 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001683 break;
1684 }
drh75897232000-05-29 14:26:00 +00001685 }
drh856c1032009-06-02 15:21:42 +00001686 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001687 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001688 }else if( (flags & MEM_Null)!=0 ){
1689 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001690 }else{
drh158b9cb2011-03-05 20:59:46 +00001691fp_math:
drh856c1032009-06-02 15:21:42 +00001692 rA = sqlite3VdbeRealValue(pIn1);
1693 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001694 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001695 case OP_Add: rB += rA; break;
1696 case OP_Subtract: rB -= rA; break;
1697 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001698 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001699 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001700 if( rA==(double)0 ) goto arithmetic_result_is_null;
1701 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001702 break;
1703 }
drhbf4133c2001-10-13 02:59:08 +00001704 default: {
drhe3b89d22019-01-18 17:53:50 +00001705 iA = sqlite3VdbeIntValue(pIn1);
1706 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001707 if( iA==0 ) goto arithmetic_result_is_null;
1708 if( iA==-1 ) iA = 1;
1709 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001710 break;
1711 }
drh5e00f6c2001-09-13 13:46:56 +00001712 }
drhc5a7b512010-01-13 16:25:42 +00001713#ifdef SQLITE_OMIT_FLOATING_POINT
1714 pOut->u.i = rB;
1715 MemSetTypeFlag(pOut, MEM_Int);
1716#else
drh856c1032009-06-02 15:21:42 +00001717 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001718 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001719 }
drh74eaba42014-09-18 17:52:15 +00001720 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001721 MemSetTypeFlag(pOut, MEM_Real);
drhc5a7b512010-01-13 16:25:42 +00001722#endif
drh5e00f6c2001-09-13 13:46:56 +00001723 }
1724 break;
1725
drha05a7222008-01-19 03:35:58 +00001726arithmetic_result_is_null:
1727 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001728 break;
1729}
1730
drh7a957892012-02-02 17:35:43 +00001731/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001732**
drhbb6783b2017-04-29 18:02:49 +00001733** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001734** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1735** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001736** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001737**
drh7a957892012-02-02 17:35:43 +00001738** If P1 is not zero, then it is a register that a subsequent min() or
1739** max() aggregate will set to 1 if the current row is not the minimum or
1740** maximum. The P1 register is initialized to 0 by this instruction.
1741**
danielk1977dc1bdc42004-06-11 10:51:27 +00001742** The interface used by the implementation of the aforementioned functions
1743** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001744** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001745*/
drh9cbf3422008-01-17 16:22:13 +00001746case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001747 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001748 if( pOp->p1 ){
1749 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1750 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001751 break;
1752}
1753
drh98757152008-01-09 23:04:12 +00001754/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001755** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001756**
drh98757152008-01-09 23:04:12 +00001757** Take the bit-wise AND of the values in register P1 and P2 and
1758** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001759** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001760*/
drh98757152008-01-09 23:04:12 +00001761/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001762** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001763**
drh98757152008-01-09 23:04:12 +00001764** Take the bit-wise OR of the values in register P1 and P2 and
1765** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001766** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001767*/
drh98757152008-01-09 23:04:12 +00001768/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001769** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001770**
drh98757152008-01-09 23:04:12 +00001771** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001772** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001773** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001774** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001775*/
drh98757152008-01-09 23:04:12 +00001776/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001777** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001778**
drh98757152008-01-09 23:04:12 +00001779** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001780** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001781** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001782** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001783*/
drh5b6afba2008-01-05 16:29:28 +00001784case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1785case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1786case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1787case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001788 i64 iA;
1789 u64 uA;
1790 i64 iB;
1791 u8 op;
drh6810ce62004-01-31 19:22:56 +00001792
drh3c657212009-11-17 23:59:58 +00001793 pIn1 = &aMem[pOp->p1];
1794 pIn2 = &aMem[pOp->p2];
1795 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001796 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001797 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001798 break;
1799 }
drh158b9cb2011-03-05 20:59:46 +00001800 iA = sqlite3VdbeIntValue(pIn2);
1801 iB = sqlite3VdbeIntValue(pIn1);
1802 op = pOp->opcode;
1803 if( op==OP_BitAnd ){
1804 iA &= iB;
1805 }else if( op==OP_BitOr ){
1806 iA |= iB;
1807 }else if( iB!=0 ){
1808 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1809
1810 /* If shifting by a negative amount, shift in the other direction */
1811 if( iB<0 ){
1812 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1813 op = 2*OP_ShiftLeft + 1 - op;
1814 iB = iB>(-64) ? -iB : 64;
1815 }
1816
1817 if( iB>=64 ){
1818 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1819 }else{
1820 memcpy(&uA, &iA, sizeof(uA));
1821 if( op==OP_ShiftLeft ){
1822 uA <<= iB;
1823 }else{
1824 uA >>= iB;
1825 /* Sign-extend on a right shift of a negative number */
1826 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1827 }
1828 memcpy(&iA, &uA, sizeof(iA));
1829 }
drhbf4133c2001-10-13 02:59:08 +00001830 }
drh158b9cb2011-03-05 20:59:46 +00001831 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001832 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001833 break;
1834}
1835
drh8558cde2008-01-05 05:20:10 +00001836/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001837** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001838**
danielk19770cdc0222008-06-26 18:04:03 +00001839** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001840** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001841**
drh8558cde2008-01-05 05:20:10 +00001842** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001843*/
drh9cbf3422008-01-17 16:22:13 +00001844case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001845 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001846 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001847 sqlite3VdbeMemIntegerify(pIn1);
1848 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001849 break;
1850}
1851
dane5166e02019-03-19 11:56:39 +00001852/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001853**
dane5166e02019-03-19 11:56:39 +00001854** Force the value in register P1 to be an integer. If the value
1855** in P1 is not an integer and cannot be converted into an integer
1856** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001857** raise an SQLITE_MISMATCH exception.
1858*/
drh9cbf3422008-01-17 16:22:13 +00001859case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001860 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001861 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001862 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001863 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001864 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001865 if( pOp->p2==0 ){
1866 rc = SQLITE_MISMATCH;
1867 goto abort_due_to_error;
1868 }else{
drhf56fa462015-04-13 21:39:54 +00001869 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001870 }
drh8aff1012001-12-22 14:49:24 +00001871 }
drh8aff1012001-12-22 14:49:24 +00001872 }
drhc9065332019-04-01 14:01:21 +00001873 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001874 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001875 break;
1876}
1877
drh13573c72010-01-12 17:04:07 +00001878#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001879/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001880**
drh2133d822008-01-03 18:44:59 +00001881** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001882**
drh8a512562005-11-14 22:29:05 +00001883** This opcode is used when extracting information from a column that
1884** has REAL affinity. Such column values may still be stored as
1885** integers, for space efficiency, but after extraction we want them
1886** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001887*/
drh9cbf3422008-01-17 16:22:13 +00001888case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001889 pIn1 = &aMem[pOp->p1];
drh169f0772019-05-02 21:36:26 +00001890 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +00001891 testcase( pIn1->flags & MEM_Int );
1892 testcase( pIn1->flags & MEM_IntReal );
drh8558cde2008-01-05 05:20:10 +00001893 sqlite3VdbeMemRealify(pIn1);
drhefb5f9a2019-08-30 21:52:13 +00001894 REGISTER_TRACE(pOp->p1, pIn1);
drh8a512562005-11-14 22:29:05 +00001895 }
drh487e2622005-06-25 18:42:14 +00001896 break;
1897}
drh13573c72010-01-12 17:04:07 +00001898#endif
drh487e2622005-06-25 18:42:14 +00001899
drh8df447f2005-11-01 15:48:24 +00001900#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001901/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001902** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001903**
drh4169e432014-08-25 20:11:52 +00001904** Force the value in register P1 to be the type defined by P2.
1905**
1906** <ul>
drhbb6783b2017-04-29 18:02:49 +00001907** <li> P2=='A' &rarr; BLOB
1908** <li> P2=='B' &rarr; TEXT
1909** <li> P2=='C' &rarr; NUMERIC
1910** <li> P2=='D' &rarr; INTEGER
1911** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001912** </ul>
drh487e2622005-06-25 18:42:14 +00001913**
1914** A NULL value is not changed by this routine. It remains NULL.
1915*/
drh4169e432014-08-25 20:11:52 +00001916case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001917 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001918 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001919 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001920 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1921 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1922 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001923 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001924 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001925 rc = ExpandBlob(pIn1);
drh9467abf2016-02-17 18:44:11 +00001926 if( rc ) goto abort_due_to_error;
drh0af6ddd2019-12-23 03:37:46 +00001927 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
1928 if( rc ) goto abort_due_to_error;
1929 UPDATE_MAX_BLOBSIZE(pIn1);
drh5d732722019-12-20 17:25:10 +00001930 REGISTER_TRACE(pOp->p1, pIn1);
drh487e2622005-06-25 18:42:14 +00001931 break;
1932}
drh8a512562005-11-14 22:29:05 +00001933#endif /* SQLITE_OMIT_CAST */
1934
drh79752b62016-08-13 10:02:17 +00001935/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001936** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001937**
1938** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1939** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1940** store the result of comparison in register P2.
1941**
1942** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1943** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1944** to coerce both inputs according to this affinity before the
1945** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1946** affinity is used. Note that the affinity conversions are stored
1947** back into the input registers P1 and P3. So this opcode can cause
1948** persistent changes to registers P1 and P3.
1949**
1950** Once any conversions have taken place, and neither value is NULL,
1951** the values are compared. If both values are blobs then memcmp() is
1952** used to determine the results of the comparison. If both values
1953** are text, then the appropriate collating function specified in
1954** P4 is used to do the comparison. If P4 is not specified then
1955** memcmp() is used to compare text string. If both values are
1956** numeric, then a numeric comparison is used. If the two values
1957** are of different types, then numbers are considered less than
1958** strings and strings are considered less than blobs.
1959**
1960** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1961** true or false and is never NULL. If both operands are NULL then the result
1962** of comparison is true. If either operand is NULL then the result is false.
1963** If neither operand is NULL the result is the same as it would be if
1964** the SQLITE_NULLEQ flag were omitted from P5.
1965**
1966** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001967** content of r[P2] is only changed if the new value is NULL or 0 (false).
1968** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001969*/
1970/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001971** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001972**
1973** This works just like the Eq opcode except that the jump is taken if
1974** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1975** additional information.
1976**
1977** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001978** content of r[P2] is only changed if the new value is NULL or 1 (true).
1979** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001980*/
drh35573352008-01-08 23:54:25 +00001981/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001982** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001983**
drh35573352008-01-08 23:54:25 +00001984** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001985** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1986** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001987**
drh35573352008-01-08 23:54:25 +00001988** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001989** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001990** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001991**
drh35573352008-01-08 23:54:25 +00001992** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001993** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001994** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001995** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001996** affinity is used. Note that the affinity conversions are stored
1997** back into the input registers P1 and P3. So this opcode can cause
1998** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001999**
2000** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00002001** the values are compared. If both values are blobs then memcmp() is
2002** used to determine the results of the comparison. If both values
2003** are text, then the appropriate collating function specified in
2004** P4 is used to do the comparison. If P4 is not specified then
2005** memcmp() is used to compare text string. If both values are
2006** numeric, then a numeric comparison is used. If the two values
2007** are of different types, then numbers are considered less than
2008** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00002009*/
drh9cbf3422008-01-17 16:22:13 +00002010/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002011** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002012**
drh35573352008-01-08 23:54:25 +00002013** This works just like the Lt opcode except that the jump is taken if
2014** the content of register P3 is less than or equal to the content of
2015** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002016*/
drh9cbf3422008-01-17 16:22:13 +00002017/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002018** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002019**
drh35573352008-01-08 23:54:25 +00002020** This works just like the Lt opcode except that the jump is taken if
2021** the content of register P3 is greater than the content of
2022** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002023*/
drh9cbf3422008-01-17 16:22:13 +00002024/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002025** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002026**
drh35573352008-01-08 23:54:25 +00002027** This works just like the Lt opcode except that the jump is taken if
2028** the content of register P3 is greater than or equal to the content of
2029** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002030*/
drh9cbf3422008-01-17 16:22:13 +00002031case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
2032case OP_Ne: /* same as TK_NE, jump, in1, in3 */
2033case OP_Lt: /* same as TK_LT, jump, in1, in3 */
2034case OP_Le: /* same as TK_LE, jump, in1, in3 */
2035case OP_Gt: /* same as TK_GT, jump, in1, in3 */
2036case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00002037 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00002038 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00002039 u16 flags1; /* Copy of initial value of pIn1->flags */
2040 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00002041
drh3c657212009-11-17 23:59:58 +00002042 pIn1 = &aMem[pOp->p1];
2043 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00002044 flags1 = pIn1->flags;
2045 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00002046 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00002047 /* One or both operands are NULL */
2048 if( pOp->p5 & SQLITE_NULLEQ ){
2049 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2050 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2051 ** or not both operands are null.
2052 */
drh053a1282012-09-19 21:15:46 +00002053 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00002054 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
2055 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00002056 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00002057 && (flags3&MEM_Cleared)==0
2058 ){
drh4910a762016-09-03 01:46:15 +00002059 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002060 }else{
danbdabe742019-03-18 16:51:24 +00002061 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002062 }
drh6a2fe092009-09-23 02:29:36 +00002063 }else{
2064 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2065 ** then the result is always NULL.
2066 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2067 */
drh688852a2014-02-17 22:40:43 +00002068 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002069 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002070 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00002071 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00002072 MemSetTypeFlag(pOut, MEM_Null);
2073 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002074 }else{
drhf4345e42014-02-18 11:31:59 +00002075 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00002076 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00002077 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002078 }
drh6a2fe092009-09-23 02:29:36 +00002079 }
2080 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002081 }
drh6a2fe092009-09-23 02:29:36 +00002082 }else{
2083 /* Neither operand is NULL. Do a comparison. */
2084 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002085 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002086 if( (flags1 | flags3)&MEM_Str ){
drh169f0772019-05-02 21:36:26 +00002087 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002088 applyNumericAffinity(pIn1,0);
drh9dce0ef2020-02-01 21:03:27 +00002089 assert( flags3==pIn3->flags );
drh4b37cd42016-06-25 11:43:47 +00002090 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002091 }
drh169f0772019-05-02 21:36:26 +00002092 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002093 applyNumericAffinity(pIn3,0);
2094 }
drh24a09622014-09-18 16:28:59 +00002095 }
drh64caee42016-09-09 19:33:00 +00002096 /* Handle the common case of integer comparison here, as an
2097 ** optimization, to avoid a call to sqlite3MemCompare() */
2098 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2099 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2100 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2101 res = 0;
2102 goto compare_op;
2103 }
drh24a09622014-09-18 16:28:59 +00002104 }else if( affinity==SQLITE_AFF_TEXT ){
drh169f0772019-05-02 21:36:26 +00002105 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002106 testcase( pIn1->flags & MEM_Int );
2107 testcase( pIn1->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002108 testcase( pIn1->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002109 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002110 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2111 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh9dce0ef2020-02-01 21:03:27 +00002112 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
drh24a09622014-09-18 16:28:59 +00002113 }
drhb44fec62019-12-24 21:42:22 +00002114 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002115 testcase( pIn3->flags & MEM_Int );
2116 testcase( pIn3->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002117 testcase( pIn3->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002118 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002119 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2120 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002121 }
drh6a2fe092009-09-23 02:29:36 +00002122 }
drh6a2fe092009-09-23 02:29:36 +00002123 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002124 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002125 }
drh64caee42016-09-09 19:33:00 +00002126compare_op:
drh58596362017-08-03 00:29:23 +00002127 /* At this point, res is negative, zero, or positive if reg[P1] is
2128 ** less than, equal to, or greater than reg[P3], respectively. Compute
2129 ** the answer to this operator in res2, depending on what the comparison
2130 ** operator actually is. The next block of code depends on the fact
2131 ** that the 6 comparison operators are consecutive integers in this
2132 ** order: NE, EQ, GT, LE, LT, GE */
2133 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2134 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2135 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2136 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2137 res2 = aLTb[pOp->opcode - OP_Ne];
2138 }else if( res==0 ){
2139 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2140 res2 = aEQb[pOp->opcode - OP_Ne];
2141 }else{
2142 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2143 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002144 }
2145
drhf56fa462015-04-13 21:39:54 +00002146 /* Undo any changes made by applyAffinity() to the input registers. */
drhf56fa462015-04-13 21:39:54 +00002147 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2148 pIn3->flags = flags3;
drhb44fec62019-12-24 21:42:22 +00002149 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2150 pIn1->flags = flags1;
drhf56fa462015-04-13 21:39:54 +00002151
drh35573352008-01-08 23:54:25 +00002152 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002153 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002154 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002155 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002156 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002157 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2158 ** is only used in contexts where either:
2159 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2160 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2161 ** Therefore it is not necessary to check the content of r[P2] for
2162 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002163 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002164 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002165 testcase( res2==0 && pOp->opcode==OP_Eq );
2166 testcase( res2==1 && pOp->opcode==OP_Eq );
2167 testcase( res2==0 && pOp->opcode==OP_Ne );
2168 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002169 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002170 }
drh2b4ded92010-09-27 21:09:31 +00002171 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002172 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002173 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002174 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002175 }else{
drh6cbbcd82019-04-01 13:06:19 +00002176 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002177 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002178 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002179 }
danielk1977a37cdde2004-05-16 11:15:36 +00002180 }
2181 break;
2182}
drhc9b84a12002-06-20 11:36:48 +00002183
drh79752b62016-08-13 10:02:17 +00002184/* Opcode: ElseNotEq * P2 * * *
2185**
drh13d79502019-12-23 02:18:49 +00002186** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2187** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2188** opcodes are allowed to occur between this instruction and the previous
2189** OP_Lt or OP_Gt. Furthermore, the prior OP_Lt or OP_Gt must have the
2190** SQLITE_STOREP2 bit set in the P5 field.
2191**
2192** If result of an OP_Eq comparison on the same two operands as the
2193** prior OP_Lt or OP_Gt would have been NULL or false (0), then then
2194** jump to P2. If the result of an OP_Eq comparison on the two previous
2195** operands would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002196*/
2197case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
drh13d79502019-12-23 02:18:49 +00002198
2199#ifdef SQLITE_DEBUG
2200 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
2201 ** OP_Gt with the SQLITE_STOREP2 flag set, with zero or more intervening
2202 ** OP_ReleaseReg opcodes */
2203 int iAddr;
2204 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
2205 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
2206 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
2207 assert( aOp[iAddr].p5 & SQLITE_STOREP2 );
2208 break;
2209 }
2210#endif /* SQLITE_DEBUG */
drh0f825a72016-08-13 14:17:02 +00002211 VdbeBranchTaken(iCompare!=0, 2);
2212 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002213 break;
2214}
2215
2216
drh0acb7e42008-06-25 00:12:41 +00002217/* Opcode: Permutation * * * P4 *
2218**
drhb7dab702017-01-26 18:00:00 +00002219** Set the permutation used by the OP_Compare operator in the next
2220** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002221**
drh953f7612012-12-07 22:18:54 +00002222** The permutation is only valid until the next OP_Compare that has
2223** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2224** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002225**
2226** The first integer in the P4 integer array is the length of the array
2227** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002228*/
2229case OP_Permutation: {
2230 assert( pOp->p4type==P4_INTARRAY );
2231 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002232 assert( pOp[1].opcode==OP_Compare );
2233 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002234 break;
2235}
2236
drh953f7612012-12-07 22:18:54 +00002237/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002238** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002239**
drh710c4842010-08-30 01:17:20 +00002240** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2241** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002242** the comparison for use by the next OP_Jump instruct.
2243**
drh0ca10df2012-12-08 13:26:23 +00002244** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2245** determined by the most recent OP_Permutation operator. If the
2246** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2247** order.
2248**
drh0acb7e42008-06-25 00:12:41 +00002249** P4 is a KeyInfo structure that defines collating sequences and sort
2250** orders for the comparison. The permutation applies to registers
2251** only. The KeyInfo elements are used sequentially.
2252**
2253** The comparison is a sort comparison, so NULLs compare equal,
2254** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002255** and strings are less than blobs.
2256*/
2257case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002258 int n;
2259 int i;
2260 int p1;
2261 int p2;
2262 const KeyInfo *pKeyInfo;
2263 int idx;
2264 CollSeq *pColl; /* Collating sequence to use on this term */
2265 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002266 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002267
drhb7dab702017-01-26 18:00:00 +00002268 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2269 aPermute = 0;
2270 }else{
2271 assert( pOp>aOp );
2272 assert( pOp[-1].opcode==OP_Permutation );
2273 assert( pOp[-1].p4type==P4_INTARRAY );
2274 aPermute = pOp[-1].p4.ai + 1;
2275 assert( aPermute!=0 );
2276 }
drh856c1032009-06-02 15:21:42 +00002277 n = pOp->p3;
2278 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002279 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002280 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002281 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002282 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002283#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002284 if( aPermute ){
2285 int k, mx = 0;
2286 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002287 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2288 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002289 }else{
drh9f6168b2016-03-19 23:32:58 +00002290 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2291 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002292 }
2293#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002294 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002295 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002296 assert( memIsValid(&aMem[p1+idx]) );
2297 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002298 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2299 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002300 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002301 pColl = pKeyInfo->aColl[i];
dan6e118922019-08-12 16:36:38 +00002302 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
drha6c2ed92009-11-14 23:22:23 +00002303 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002304 if( iCompare ){
dan6e118922019-08-12 16:36:38 +00002305 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
2306 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
2307 ){
2308 iCompare = -iCompare;
2309 }
drh0acb7e42008-06-25 00:12:41 +00002310 if( bRev ) iCompare = -iCompare;
2311 break;
2312 }
drh16ee60f2008-06-20 18:13:25 +00002313 }
2314 break;
2315}
2316
2317/* Opcode: Jump P1 P2 P3 * *
2318**
2319** Jump to the instruction at address P1, P2, or P3 depending on whether
2320** in the most recent OP_Compare instruction the P1 vector was less than
2321** equal to, or greater than the P2 vector, respectively.
2322*/
drh0acb7e42008-06-25 00:12:41 +00002323case OP_Jump: { /* jump */
2324 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002325 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002326 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002327 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002328 }else{
drh7083a482018-07-10 16:04:04 +00002329 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002330 }
2331 break;
2332}
2333
drh5b6afba2008-01-05 16:29:28 +00002334/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002335** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002336**
drh5b6afba2008-01-05 16:29:28 +00002337** Take the logical AND of the values in registers P1 and P2 and
2338** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002339**
drh5b6afba2008-01-05 16:29:28 +00002340** If either P1 or P2 is 0 (false) then the result is 0 even if
2341** the other input is NULL. A NULL and true or two NULLs give
2342** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002343*/
drh5b6afba2008-01-05 16:29:28 +00002344/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002345** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002346**
2347** Take the logical OR of the values in register P1 and P2 and
2348** store the answer in register P3.
2349**
2350** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2351** even if the other input is NULL. A NULL and false or two NULLs
2352** give a NULL output.
2353*/
2354case OP_And: /* same as TK_AND, in1, in2, out3 */
2355case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002356 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2357 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002358
drh1fcfa722018-02-26 15:27:31 +00002359 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2360 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002361 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002362 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002363 v1 = and_logic[v1*3+v2];
2364 }else{
drh5b6afba2008-01-05 16:29:28 +00002365 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002366 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002367 }
drh3c657212009-11-17 23:59:58 +00002368 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002369 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002370 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002371 }else{
drh5b6afba2008-01-05 16:29:28 +00002372 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002373 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002374 }
drh5e00f6c2001-09-13 13:46:56 +00002375 break;
2376}
2377
drh8abed7b2018-02-26 18:49:05 +00002378/* Opcode: IsTrue P1 P2 P3 P4 *
2379** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2380**
2381** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2382** IS NOT FALSE operators.
2383**
drh96acafb2018-02-27 14:49:25 +00002384** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002385** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2386** NULL, then the P3 is stored in register P2. Invert the answer if P4
2387** is 1.
2388**
2389** The logic is summarized like this:
2390**
2391** <ul>
drh96acafb2018-02-27 14:49:25 +00002392** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2393** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2394** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2395** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002396** </ul>
2397*/
2398case OP_IsTrue: { /* in1, out2 */
2399 assert( pOp->p4type==P4_INT32 );
2400 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002401 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002402 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2403 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2404 break;
2405}
2406
drhe99fa2a2008-12-15 15:27:51 +00002407/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002408** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002409**
drhe99fa2a2008-12-15 15:27:51 +00002410** Interpret the value in register P1 as a boolean value. Store the
2411** boolean complement in register P2. If the value in register P1 is
2412** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002413*/
drh93952eb2009-11-13 19:43:43 +00002414case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002415 pIn1 = &aMem[pOp->p1];
2416 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002417 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002418 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002419 }else{
2420 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002421 }
drh5e00f6c2001-09-13 13:46:56 +00002422 break;
2423}
2424
drhe99fa2a2008-12-15 15:27:51 +00002425/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002426** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002427**
drhe99fa2a2008-12-15 15:27:51 +00002428** Interpret the content of register P1 as an integer. Store the
2429** ones-complement of the P1 value into register P2. If P1 holds
2430** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002431*/
drh93952eb2009-11-13 19:43:43 +00002432case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002433 pIn1 = &aMem[pOp->p1];
2434 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002435 sqlite3VdbeMemSetNull(pOut);
2436 if( (pIn1->flags & MEM_Null)==0 ){
2437 pOut->flags = MEM_Int;
2438 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002439 }
drhbf4133c2001-10-13 02:59:08 +00002440 break;
2441}
2442
drh48f2d3b2011-09-16 01:34:43 +00002443/* Opcode: Once P1 P2 * * *
2444**
drhab087d42017-03-24 17:59:56 +00002445** Fall through to the next instruction the first time this opcode is
2446** encountered on each invocation of the byte-code program. Jump to P2
2447** on the second and all subsequent encounters during the same invocation.
2448**
2449** Top-level programs determine first invocation by comparing the P1
2450** operand against the P1 operand on the OP_Init opcode at the beginning
2451** of the program. If the P1 values differ, then fall through and make
2452** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2453** the same then take the jump.
2454**
2455** For subprograms, there is a bitmask in the VdbeFrame that determines
2456** whether or not the jump should be taken. The bitmask is necessary
2457** because the self-altering code trick does not work for recursive
2458** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002459*/
dan1d8cb212011-12-09 13:24:16 +00002460case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002461 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002462 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002463 if( p->pFrame ){
2464 iAddr = (int)(pOp - p->aOp);
2465 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2466 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002467 goto jump_to_p2;
2468 }
drh18333ef2017-03-24 18:38:41 +00002469 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002470 }else{
drhab087d42017-03-24 17:59:56 +00002471 if( p->aOp[0].p1==pOp->p1 ){
2472 VdbeBranchTaken(1, 2);
2473 goto jump_to_p2;
2474 }
dan1d8cb212011-12-09 13:24:16 +00002475 }
drhab087d42017-03-24 17:59:56 +00002476 VdbeBranchTaken(0, 2);
2477 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002478 break;
2479}
2480
drh3c84ddf2008-01-09 02:15:38 +00002481/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002482**
drhef8662b2011-06-20 21:47:58 +00002483** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002484** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002485** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002486*/
drh1fcfa722018-02-26 15:27:31 +00002487case OP_If: { /* jump, in1 */
2488 int c;
2489 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2490 VdbeBranchTaken(c!=0, 2);
2491 if( c ) goto jump_to_p2;
2492 break;
2493}
2494
drh3c84ddf2008-01-09 02:15:38 +00002495/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002496**
drhef8662b2011-06-20 21:47:58 +00002497** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002498** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002499** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002500*/
drh9cbf3422008-01-17 16:22:13 +00002501case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002502 int c;
drh1fcfa722018-02-26 15:27:31 +00002503 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002504 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002505 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002506 break;
2507}
2508
drh830ecf92009-06-18 00:41:55 +00002509/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002510** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002511**
drh830ecf92009-06-18 00:41:55 +00002512** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002513*/
drh9cbf3422008-01-17 16:22:13 +00002514case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002515 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002516 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002517 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002518 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002519 }
drh477df4b2008-01-05 18:48:24 +00002520 break;
2521}
2522
drh98757152008-01-09 23:04:12 +00002523/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002524** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002525**
drh6a288a32008-01-07 19:20:24 +00002526** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002527*/
drh9cbf3422008-01-17 16:22:13 +00002528case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002529 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002530 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002531 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002532 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002533 }
drh5e00f6c2001-09-13 13:46:56 +00002534 break;
2535}
2536
drh31d6fd52017-04-14 19:03:10 +00002537/* Opcode: IfNullRow P1 P2 P3 * *
2538** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2539**
2540** Check the cursor P1 to see if it is currently pointing at a NULL row.
2541** If it is, then set register P3 to NULL and jump immediately to P2.
2542** If P1 is not on a NULL row, then fall through without making any
2543** changes.
2544*/
2545case OP_IfNullRow: { /* jump */
2546 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002547 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002548 if( p->apCsr[pOp->p1]->nullRow ){
2549 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2550 goto jump_to_p2;
2551 }
2552 break;
2553}
2554
drh092457b2017-12-29 15:04:49 +00002555#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2556/* Opcode: Offset P1 P2 P3 * *
2557** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002558**
drh092457b2017-12-29 15:04:49 +00002559** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002560** start of the payload for the record at which that cursor P1 is currently
2561** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002562**
drh092457b2017-12-29 15:04:49 +00002563** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002564** This opcode does not use P2 itself, but the P2 value is used by the
2565** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002566** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002567**
2568** This opcode is only available if SQLite is compiled with the
2569** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002570*/
drh092457b2017-12-29 15:04:49 +00002571case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002572 VdbeCursor *pC; /* The VDBE cursor */
2573 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2574 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002575 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002576 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002577 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002578 }else{
drh092457b2017-12-29 15:04:49 +00002579 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002580 }
2581 break;
2582}
drh092457b2017-12-29 15:04:49 +00002583#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002584
drh3e9ca092009-09-08 01:14:48 +00002585/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002586** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002587**
danielk1977cfcdaef2004-05-12 07:33:33 +00002588** Interpret the data that cursor P1 points to as a structure built using
2589** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002590** information about the format of the data.) Extract the P2-th column
2591** from this record. If there are less that (P2+1)
2592** values in the record, extract a NULL.
2593**
drh9cbf3422008-01-17 16:22:13 +00002594** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002595**
drh1cc3a362017-04-03 13:17:31 +00002596** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002597** if the P4 argument is a P4_MEM use the value of the P4 argument as
2598** the result.
drh3e9ca092009-09-08 01:14:48 +00002599**
drh1cc3a362017-04-03 13:17:31 +00002600** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002601** the result is guaranteed to only be used as the argument of a length()
2602** or typeof() function, respectively. The loading of large blobs can be
2603** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002604*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002605case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002606 int p2; /* column number to retrieve */
2607 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002608 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002609 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002610 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002611 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002612 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002613 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002614 const u8 *zData; /* Part of the record being decoded */
2615 const u8 *zHdr; /* Next unparsed byte of the header */
2616 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002617 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002618 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002619 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002620
drh8c7715d2019-12-20 14:37:56 +00002621 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
dande892d92016-01-29 19:29:45 +00002622 pC = p->apCsr[pOp->p1];
drh8c7715d2019-12-20 14:37:56 +00002623 assert( pC!=0 );
drh856c1032009-06-02 15:21:42 +00002624 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002625
drh170ad682017-06-02 15:44:22 +00002626 /* If the cursor cache is stale (meaning it is not currently point at
2627 ** the correct row) then bring it up-to-date by doing the necessary
2628 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002629 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002630 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002631
drh9f6168b2016-03-19 23:32:58 +00002632 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002633 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002634 memAboutToChange(p, pDest);
danielk19776c924092007-11-12 08:09:34 +00002635 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002636 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002637 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002638 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002639 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2640 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002641
drha43a02e2016-05-19 17:51:19 +00002642 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002643 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002644 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002645 /* For the special case of as pseudo-cursor, the seekResult field
2646 ** identifies the register that holds the record */
2647 assert( pC->seekResult>0 );
2648 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002649 assert( pReg->flags & MEM_Blob );
2650 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002651 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002652 pC->aRow = (u8*)pReg->z;
2653 }else{
drh6b5631e2014-11-05 15:57:39 +00002654 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002655 goto op_column_out;
2656 }
danielk1977192ac1d2004-05-10 07:17:30 +00002657 }else{
drh06a09a82016-11-25 17:03:03 +00002658 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002659 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002660 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002661 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2662 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002663 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2664 assert( pC->szRow<=pC->payloadSize );
2665 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2666 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002667 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002668 }
danielk1977192ac1d2004-05-10 07:17:30 +00002669 }
drhb73857f2006-03-17 00:25:59 +00002670 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002671 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002672 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002673
drhc81aa2e2014-10-11 23:31:52 +00002674
drh1f613c42017-08-16 14:16:19 +00002675 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002676 /* pC->aRow does not have to hold the entire row, but it does at least
2677 ** need to cover the header of the record. If pC->aRow does not contain
2678 ** the complete header, then set it to zero, forcing the header to be
2679 ** dynamically allocated. */
2680 pC->aRow = 0;
2681 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002682
2683 /* Make sure a corrupt database has not given us an oversize header.
2684 ** Do this now to avoid an oversize memory allocation.
2685 **
2686 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2687 ** types use so much data space that there can only be 4096 and 32 of
2688 ** them, respectively. So the maximum header length results from a
2689 ** 3-byte type for each of the maximum of 32768 columns plus three
2690 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2691 */
drh1f613c42017-08-16 14:16:19 +00002692 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002693 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002694 }
drh95b225a2017-08-16 11:04:22 +00002695 }else{
2696 /* This is an optimization. By skipping over the first few tests
2697 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2698 ** measurable performance gain.
2699 **
drh1f613c42017-08-16 14:16:19 +00002700 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002701 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002702 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002703 ** branch jumps to reads past the end of the record, but never more
2704 ** than a few bytes. Even if the record occurs at the end of the page
2705 ** content area, the "page header" comes after the page content and so
2706 ** this overread is harmless. Similar overreads can occur for a corrupt
2707 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002708 */
2709 zData = pC->aRow;
2710 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002711 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002712 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002713 }
drh399af1d2013-11-20 17:25:55 +00002714 }
drh35cd6432009-06-05 14:17:21 +00002715
drh399af1d2013-11-20 17:25:55 +00002716 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002717 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002718 */
drhc8606e42013-11-20 19:28:03 +00002719 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002720 /* If there is more header available for parsing in the record, try
2721 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002722 */
drhc8606e42013-11-20 19:28:03 +00002723 if( pC->iHdrOffset<aOffset[0] ){
2724 /* Make sure zData points to enough of the record to cover the header. */
2725 if( pC->aRow==0 ){
2726 memset(&sMem, 0, sizeof(sMem));
drh2a740062020-02-05 18:28:17 +00002727 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
drh9467abf2016-02-17 18:44:11 +00002728 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002729 zData = (u8*)sMem.z;
2730 }else{
2731 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002732 }
drhc8606e42013-11-20 19:28:03 +00002733
drh0c8f7602014-09-19 16:56:45 +00002734 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002735 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002736 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002737 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002738 zHdr = zData + pC->iHdrOffset;
2739 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002740 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002741 do{
drhc332e042019-02-12 21:04:33 +00002742 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002743 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002744 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002745 }else{
drhc8606e42013-11-20 19:28:03 +00002746 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002747 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002748 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002749 }
drhc332e042019-02-12 21:04:33 +00002750 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002751 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002752
drh8dd83622014-10-13 23:39:02 +00002753 /* The record is corrupt if any of the following are true:
2754 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002755 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002756 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002757 */
drhc6ce38832015-10-15 21:30:24 +00002758 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2759 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002760 ){
drh95b225a2017-08-16 11:04:22 +00002761 if( aOffset[0]==0 ){
2762 i = 0;
2763 zHdr = zEndHdr;
2764 }else{
2765 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002766 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002767 }
danielk1977dedf45b2006-01-13 17:12:01 +00002768 }
drhddb2b4a2016-03-25 12:10:32 +00002769
drh170c2762016-05-20 21:40:11 +00002770 pC->nHdrParsed = i;
2771 pC->iHdrOffset = (u32)(zHdr - zData);
2772 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002773 }else{
drh9fbc8852016-01-04 03:48:46 +00002774 t = 0;
drh9188b382004-05-14 21:12:22 +00002775 }
drhd3194f52004-05-27 19:59:32 +00002776
drhf2db3382015-04-30 20:33:25 +00002777 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002778 ** still not up to p2, that means that the record has fewer than p2
2779 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002780 */
drhc8606e42013-11-20 19:28:03 +00002781 if( pC->nHdrParsed<=p2 ){
2782 if( pOp->p4type==P4_MEM ){
2783 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2784 }else{
drh22e8d832014-10-29 00:58:38 +00002785 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002786 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002787 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002788 }
drh95fa6062015-10-16 13:50:08 +00002789 }else{
2790 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002791 }
danielk1977192ac1d2004-05-10 07:17:30 +00002792
drh380d6852013-11-20 20:58:00 +00002793 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002794 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002795 ** all valid.
drh9188b382004-05-14 21:12:22 +00002796 */
drhc8606e42013-11-20 19:28:03 +00002797 assert( p2<pC->nHdrParsed );
2798 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002799 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002800 if( VdbeMemDynamic(pDest) ){
2801 sqlite3VdbeMemSetNull(pDest);
2802 }
drh95fa6062015-10-16 13:50:08 +00002803 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002804 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002805 /* This is the common case where the desired content fits on the original
2806 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002807 zData = pC->aRow + aOffset[p2];
2808 if( t<12 ){
2809 sqlite3VdbeSerialGet(zData, t, pDest);
2810 }else{
2811 /* If the column value is a string, we need a persistent value, not
2812 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2813 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2814 */
2815 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2816 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002817 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002818 if( pDest->szMalloc < len+2 ){
2819 pDest->flags = MEM_Null;
2820 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2821 }else{
2822 pDest->z = pDest->zMalloc;
2823 }
2824 memcpy(pDest->z, zData, len);
2825 pDest->z[len] = 0;
2826 pDest->z[len+1] = 0;
2827 pDest->flags = aFlag[t&1];
2828 }
danielk197736963fd2005-02-19 08:18:05 +00002829 }else{
drha1851ef2016-05-20 19:51:28 +00002830 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002831 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002832 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2833 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2834 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002835 ){
drh2a2a6962014-09-16 18:22:44 +00002836 /* Content is irrelevant for
2837 ** 1. the typeof() function,
2838 ** 2. the length(X) function if X is a blob, and
2839 ** 3. if the content length is zero.
2840 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002841 ** content from disk.
2842 **
2843 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2844 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
drhcbae3f82020-01-06 20:48:45 +00002845 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
2846 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
2847 ** and it begins with a bunch of zeros.
dan1f9144e2017-03-17 13:59:06 +00002848 */
drhcbae3f82020-01-06 20:48:45 +00002849 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002850 }else{
drhcb3cabd2016-11-25 19:18:28 +00002851 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002852 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2853 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2854 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002855 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002856 }
drhd3194f52004-05-27 19:59:32 +00002857
danielk19773c9cc8d2005-01-17 03:40:08 +00002858op_column_out:
drhb7654112008-01-12 12:48:07 +00002859 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002860 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002861 break;
drh74588ce2017-09-13 00:13:05 +00002862
2863op_column_corrupt:
2864 if( aOp[0].p3>0 ){
2865 pOp = &aOp[aOp[0].p3-1];
2866 break;
2867 }else{
2868 rc = SQLITE_CORRUPT_BKPT;
2869 goto abort_due_to_error;
2870 }
danielk1977192ac1d2004-05-10 07:17:30 +00002871}
2872
danielk1977751de562008-04-18 09:01:15 +00002873/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002874** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002875**
2876** Apply affinities to a range of P2 registers starting with P1.
2877**
drhbb6783b2017-04-29 18:02:49 +00002878** P4 is a string that is P2 characters long. The N-th character of the
2879** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002880** memory cell in the range.
2881*/
2882case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002883 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002884
drh856c1032009-06-02 15:21:42 +00002885 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002886 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002887 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002888 assert( zAffinity[pOp->p2]==0 );
2889 pIn1 = &aMem[pOp->p1];
drh122c5142019-07-29 05:23:01 +00002890 while( 1 /*exit-by-break*/ ){
drh9f6168b2016-03-19 23:32:58 +00002891 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drhb5f62432019-12-10 02:48:41 +00002892 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
drh83a1daf2019-05-01 18:59:33 +00002893 applyAffinity(pIn1, zAffinity[0], encoding);
2894 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
drh337cc392019-07-29 06:06:53 +00002895 /* When applying REAL affinity, if the result is still an MEM_Int
2896 ** that will fit in 6 bytes, then change the type to MEM_IntReal
2897 ** so that we keep the high-resolution integer value but know that
2898 ** the type really wants to be REAL. */
2899 testcase( pIn1->u.i==140737488355328LL );
2900 testcase( pIn1->u.i==140737488355327LL );
2901 testcase( pIn1->u.i==-140737488355328LL );
2902 testcase( pIn1->u.i==-140737488355329LL );
2903 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
2904 pIn1->flags |= MEM_IntReal;
2905 pIn1->flags &= ~MEM_Int;
2906 }else{
2907 pIn1->u.r = (double)pIn1->u.i;
2908 pIn1->flags |= MEM_Real;
2909 pIn1->flags &= ~MEM_Int;
2910 }
drh83a1daf2019-05-01 18:59:33 +00002911 }
drh6fcc1ec2019-05-01 14:41:47 +00002912 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
drh83a1daf2019-05-01 18:59:33 +00002913 zAffinity++;
2914 if( zAffinity[0]==0 ) break;
drh039fc322009-11-17 18:31:47 +00002915 pIn1++;
drh83a1daf2019-05-01 18:59:33 +00002916 }
danielk1977751de562008-04-18 09:01:15 +00002917 break;
2918}
2919
drh1db639c2008-01-17 02:36:28 +00002920/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002921** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002922**
drh710c4842010-08-30 01:17:20 +00002923** Convert P2 registers beginning with P1 into the [record format]
2924** use as a data record in a database table or as a key
2925** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002926**
drhbb6783b2017-04-29 18:02:49 +00002927** P4 may be a string that is P2 characters long. The N-th character of the
2928** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002929** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002930**
drh8a512562005-11-14 22:29:05 +00002931** The mapping from character to affinity is given by the SQLITE_AFF_
2932** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002933**
drh05883a32015-06-02 15:32:08 +00002934** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002935*/
drh1db639c2008-01-17 02:36:28 +00002936case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002937 Mem *pRec; /* The new record */
2938 u64 nData; /* Number of bytes of data space */
2939 int nHdr; /* Number of bytes of header space */
2940 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002941 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002942 int nVarint; /* Number of bytes in a varint */
2943 u32 serial_type; /* Type field */
2944 Mem *pData0; /* First field to be combined into the record */
2945 Mem *pLast; /* Last field of the record */
2946 int nField; /* Number of fields in the record */
2947 char *zAffinity; /* The affinity string for the record */
2948 int file_format; /* File format to use for encoding */
drhbe37c122015-10-16 14:54:17 +00002949 u32 len; /* Length of a field */
drhb70b0df2019-04-30 01:08:42 +00002950 u8 *zHdr; /* Where to write next byte of the header */
2951 u8 *zPayload; /* Where to write next byte of the payload */
drh856c1032009-06-02 15:21:42 +00002952
drhf3218fe2004-05-28 08:21:02 +00002953 /* Assuming the record contains N fields, the record format looks
2954 ** like this:
2955 **
drh7a224de2004-06-02 01:22:02 +00002956 ** ------------------------------------------------------------------------
2957 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2958 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002959 **
drh9cbf3422008-01-17 16:22:13 +00002960 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002961 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002962 **
2963 ** Each type field is a varint representing the serial type of the
2964 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002965 ** hdr-size field is also a varint which is the offset from the beginning
2966 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002967 */
drh856c1032009-06-02 15:21:42 +00002968 nData = 0; /* Number of bytes of data space */
2969 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002970 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002971 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002972 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002973 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002974 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002975 nField = pOp->p2;
2976 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002977 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002978
drh2b4ded92010-09-27 21:09:31 +00002979 /* Identify the output register */
2980 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2981 pOut = &aMem[pOp->p3];
2982 memAboutToChange(p, pOut);
2983
drh3e6c0602013-12-10 20:53:01 +00002984 /* Apply the requested affinity to all inputs
2985 */
2986 assert( pData0<=pLast );
2987 if( zAffinity ){
2988 pRec = pData0;
2989 do{
drh5ad12512019-05-09 16:22:51 +00002990 applyAffinity(pRec, zAffinity[0], encoding);
danbe812622019-05-17 15:59:11 +00002991 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
2992 pRec->flags |= MEM_IntReal;
2993 pRec->flags &= ~(MEM_Int);
2994 }
drh5ad12512019-05-09 16:22:51 +00002995 REGISTER_TRACE((int)(pRec-aMem), pRec);
2996 zAffinity++;
2997 pRec++;
drh57bf4a82014-02-17 14:59:22 +00002998 assert( zAffinity[0]==0 || pRec<=pLast );
2999 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00003000 }
3001
drhd447dce2017-01-25 20:55:11 +00003002#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00003003 /* NULLs can be safely trimmed from the end of the record, as long as
3004 ** as the schema format is 2 or more and none of the omitted columns
3005 ** have a non-NULL default value. Also, the record must be left with
3006 ** at least one field. If P5>0 then it will be one more than the
3007 ** index of the right-most column with a non-NULL default value */
3008 if( pOp->p5 ){
3009 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
3010 pLast--;
3011 nField--;
3012 }
3013 }
drhd447dce2017-01-25 20:55:11 +00003014#endif
drh585ce192017-01-25 14:58:27 +00003015
drhf3218fe2004-05-28 08:21:02 +00003016 /* Loop through the elements that will make up the record to figure
drh76fd7be2019-07-11 19:50:18 +00003017 ** out how much space is required for the new record. After this loop,
3018 ** the Mem.uTemp field of each term should hold the serial-type that will
3019 ** be used for that term in the generated record:
3020 **
3021 ** Mem.uTemp value type
3022 ** --------------- ---------------
3023 ** 0 NULL
3024 ** 1 1-byte signed integer
3025 ** 2 2-byte signed integer
3026 ** 3 3-byte signed integer
3027 ** 4 4-byte signed integer
3028 ** 5 6-byte signed integer
3029 ** 6 8-byte signed integer
3030 ** 7 IEEE float
3031 ** 8 Integer constant 0
3032 ** 9 Integer constant 1
3033 ** 10,11 reserved for expansion
3034 ** N>=12 and even BLOB
3035 ** N>=13 and odd text
3036 **
3037 ** The following additional values are computed:
3038 ** nHdr Number of bytes needed for the record header
3039 ** nData Number of bytes of data space needed for the record
3040 ** nZero Zero bytes at the end of the record
danielk19778d059842004-05-12 11:24:02 +00003041 */
drh038b7bc2013-12-09 23:17:22 +00003042 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00003043 do{
drh2b4ded92010-09-27 21:09:31 +00003044 assert( memIsValid(pRec) );
drhc1da4392019-07-11 19:22:36 +00003045 if( pRec->flags & MEM_Null ){
3046 if( pRec->flags & MEM_Zero ){
drh41fb3672018-01-12 23:18:38 +00003047 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3048 ** table methods that never invoke sqlite3_result_xxxxx() while
3049 ** computing an unchanging column value in an UPDATE statement.
3050 ** Give such values a special internal-use-only serial-type of 10
3051 ** so that they can be passed through to xUpdate and have
3052 ** a true sqlite3_value_nochange(). */
3053 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
drhc1da4392019-07-11 19:22:36 +00003054 pRec->uTemp = 10;
drh038b7bc2013-12-09 23:17:22 +00003055 }else{
drh76fd7be2019-07-11 19:50:18 +00003056 pRec->uTemp = 0;
drh038b7bc2013-12-09 23:17:22 +00003057 }
drhc1da4392019-07-11 19:22:36 +00003058 nHdr++;
3059 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
3060 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3061 i64 i = pRec->u.i;
drh9c3bb592019-07-30 21:00:13 +00003062 u64 uu;
drhc1da4392019-07-11 19:22:36 +00003063 testcase( pRec->flags & MEM_Int );
3064 testcase( pRec->flags & MEM_IntReal );
3065 if( i<0 ){
drh9c3bb592019-07-30 21:00:13 +00003066 uu = ~i;
drhc1da4392019-07-11 19:22:36 +00003067 }else{
drh9c3bb592019-07-30 21:00:13 +00003068 uu = i;
drhc1da4392019-07-11 19:22:36 +00003069 }
3070 nHdr++;
drh9c3bb592019-07-30 21:00:13 +00003071 testcase( uu==127 ); testcase( uu==128 );
3072 testcase( uu==32767 ); testcase( uu==32768 );
3073 testcase( uu==8388607 ); testcase( uu==8388608 );
3074 testcase( uu==2147483647 ); testcase( uu==2147483648 );
3075 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
3076 if( uu<=127 ){
drhc1da4392019-07-11 19:22:36 +00003077 if( (i&1)==i && file_format>=4 ){
drh9c3bb592019-07-30 21:00:13 +00003078 pRec->uTemp = 8+(u32)uu;
drhc1da4392019-07-11 19:22:36 +00003079 }else{
3080 nData++;
3081 pRec->uTemp = 1;
3082 }
drh9c3bb592019-07-30 21:00:13 +00003083 }else if( uu<=32767 ){
drhc1da4392019-07-11 19:22:36 +00003084 nData += 2;
3085 pRec->uTemp = 2;
drh9c3bb592019-07-30 21:00:13 +00003086 }else if( uu<=8388607 ){
drhc1da4392019-07-11 19:22:36 +00003087 nData += 3;
3088 pRec->uTemp = 3;
drh9c3bb592019-07-30 21:00:13 +00003089 }else if( uu<=2147483647 ){
drhc1da4392019-07-11 19:22:36 +00003090 nData += 4;
3091 pRec->uTemp = 4;
drh9c3bb592019-07-30 21:00:13 +00003092 }else if( uu<=140737488355327LL ){
drhc1da4392019-07-11 19:22:36 +00003093 nData += 6;
3094 pRec->uTemp = 5;
3095 }else{
3096 nData += 8;
3097 if( pRec->flags & MEM_IntReal ){
3098 /* If the value is IntReal and is going to take up 8 bytes to store
3099 ** as an integer, then we might as well make it an 8-byte floating
3100 ** point value */
3101 pRec->u.r = (double)pRec->u.i;
3102 pRec->flags &= ~MEM_IntReal;
3103 pRec->flags |= MEM_Real;
3104 pRec->uTemp = 7;
3105 }else{
3106 pRec->uTemp = 6;
3107 }
3108 }
3109 }else if( pRec->flags & MEM_Real ){
3110 nHdr++;
3111 nData += 8;
3112 pRec->uTemp = 7;
3113 }else{
3114 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
3115 assert( pRec->n>=0 );
3116 len = (u32)pRec->n;
3117 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
3118 if( pRec->flags & MEM_Zero ){
3119 serial_type += pRec->u.nZero*2;
3120 if( nData ){
3121 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
3122 len += pRec->u.nZero;
3123 }else{
3124 nZero += pRec->u.nZero;
3125 }
3126 }
3127 nData += len;
3128 nHdr += sqlite3VarintLen(serial_type);
3129 pRec->uTemp = serial_type;
drhfdf972a2007-05-02 13:30:27 +00003130 }
drh45c3c662016-04-07 14:16:16 +00003131 if( pRec==pData0 ) break;
3132 pRec--;
3133 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00003134
drh654858d2014-11-20 02:18:14 +00003135 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3136 ** which determines the total number of bytes in the header. The varint
3137 ** value is the size of the header in bytes including the size varint
3138 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00003139 testcase( nHdr==126 );
3140 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00003141 if( nHdr<=126 ){
3142 /* The common case */
3143 nHdr += 1;
3144 }else{
3145 /* Rare case of a really large header */
3146 nVarint = sqlite3VarintLen(nHdr);
3147 nHdr += nVarint;
3148 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00003149 }
drh038b7bc2013-12-09 23:17:22 +00003150 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00003151
danielk1977a7a8e142008-02-13 18:25:27 +00003152 /* Make sure the output register has a buffer large enough to store
3153 ** the new record. The output register (pOp->p3) is not allowed to
3154 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00003155 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00003156 */
drh0d7f0cc2018-09-21 13:07:14 +00003157 if( nByte+nZero<=pOut->szMalloc ){
3158 /* The output register is already large enough to hold the record.
3159 ** No error checks or buffer enlargement is required */
3160 pOut->z = pOut->zMalloc;
3161 }else{
3162 /* Need to make sure that the output is not too big and then enlarge
3163 ** the output register to hold the full result */
3164 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
3165 goto too_big;
3166 }
3167 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
3168 goto no_mem;
3169 }
danielk19778d059842004-05-12 11:24:02 +00003170 }
drh9c1905f2008-12-10 22:32:56 +00003171 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00003172 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00003173 if( nZero ){
drh8df32842008-12-09 02:51:23 +00003174 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00003175 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00003176 }
drhb7654112008-01-12 12:48:07 +00003177 UPDATE_MAX_BLOBSIZE(pOut);
drhb70b0df2019-04-30 01:08:42 +00003178 zHdr = (u8 *)pOut->z;
3179 zPayload = zHdr + nHdr;
3180
3181 /* Write the record */
3182 zHdr += putVarint32(zHdr, nHdr);
3183 assert( pData0<=pLast );
3184 pRec = pData0;
3185 do{
3186 serial_type = pRec->uTemp;
3187 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3188 ** additional varints, one per column. */
3189 zHdr += putVarint32(zHdr, serial_type); /* serial type */
3190 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
3191 ** immediately follow the header. */
3192 zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
3193 }while( (++pRec)<=pLast );
3194 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
3195 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
3196
3197 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
3198 REGISTER_TRACE(pOp->p3, pOut);
danielk19778d059842004-05-12 11:24:02 +00003199 break;
3200}
3201
danielk1977a5533162009-02-24 10:01:51 +00003202/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003203** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00003204**
3205** Store the number of entries (an integer value) in the table or index
3206** opened by cursor P1 in register P2
3207*/
3208#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00003209case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00003210 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00003211 BtCursor *pCrsr;
3212
drhc960dcb2015-11-20 19:22:01 +00003213 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
3214 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00003215 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00003216 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh21f6daa2019-10-11 14:21:48 +00003217 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00003218 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00003219 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00003220 pOut->u.i = nEntry;
drh21f6daa2019-10-11 14:21:48 +00003221 goto check_for_interrupt;
danielk1977a5533162009-02-24 10:01:51 +00003222}
3223#endif
3224
danielk1977fd7f0452008-12-17 17:30:26 +00003225/* Opcode: Savepoint P1 * * P4 *
3226**
3227** Open, release or rollback the savepoint named by parameter P4, depending
drh2ce9b6b2019-05-10 14:03:07 +00003228** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3229** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3230** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
danielk1977fd7f0452008-12-17 17:30:26 +00003231*/
3232case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00003233 int p1; /* Value of P1 operand */
3234 char *zName; /* Name of savepoint */
3235 int nName;
3236 Savepoint *pNew;
3237 Savepoint *pSavepoint;
3238 Savepoint *pTmp;
3239 int iSavepoint;
3240 int ii;
3241
3242 p1 = pOp->p1;
3243 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003244
3245 /* Assert that the p1 parameter is valid. Also that if there is no open
3246 ** transaction, then there cannot be any savepoints.
3247 */
3248 assert( db->pSavepoint==0 || db->autoCommit==0 );
3249 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3250 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3251 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003252 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003253
3254 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003255 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003256 /* A new savepoint cannot be created if there are active write
3257 ** statements (i.e. open read/write incremental blob handles).
3258 */
drh22c17b82015-05-15 04:13:15 +00003259 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003260 rc = SQLITE_BUSY;
3261 }else{
drh856c1032009-06-02 15:21:42 +00003262 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003263
drhbe07ec52011-06-03 12:15:26 +00003264#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003265 /* This call is Ok even if this savepoint is actually a transaction
3266 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3267 ** If this is a transaction savepoint being opened, it is guaranteed
3268 ** that the db->aVTrans[] array is empty. */
3269 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003270 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3271 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003272 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003273#endif
dand9495cd2011-04-27 12:08:04 +00003274
danielk1977fd7f0452008-12-17 17:30:26 +00003275 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003276 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003277 if( pNew ){
3278 pNew->zName = (char *)&pNew[1];
3279 memcpy(pNew->zName, zName, nName+1);
3280
3281 /* If there is no open transaction, then mark this as a special
3282 ** "transaction savepoint". */
3283 if( db->autoCommit ){
3284 db->autoCommit = 0;
3285 db->isTransactionSavepoint = 1;
3286 }else{
3287 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003288 }
dan21e8d012011-03-03 20:05:59 +00003289
danielk1977fd7f0452008-12-17 17:30:26 +00003290 /* Link the new savepoint into the database handle's list. */
3291 pNew->pNext = db->pSavepoint;
3292 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003293 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003294 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003295 }
3296 }
3297 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003298 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
drh856c1032009-06-02 15:21:42 +00003299 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003300
3301 /* Find the named savepoint. If there is no such savepoint, then an
3302 ** an error is returned to the user. */
3303 for(
drh856c1032009-06-02 15:21:42 +00003304 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003305 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003306 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003307 ){
3308 iSavepoint++;
3309 }
3310 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003311 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003312 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003313 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003314 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003315 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003316 */
drh22c17b82015-05-15 04:13:15 +00003317 sqlite3VdbeError(p, "cannot release savepoint - "
3318 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003319 rc = SQLITE_BUSY;
3320 }else{
3321
3322 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003323 ** and this is a RELEASE command, then the current transaction
3324 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003325 */
3326 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3327 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003328 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003329 goto vdbe_return;
3330 }
danielk1977fd7f0452008-12-17 17:30:26 +00003331 db->autoCommit = 1;
3332 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003333 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003334 db->autoCommit = 0;
3335 p->rc = rc = SQLITE_BUSY;
3336 goto vdbe_return;
3337 }
danielk197734cf35d2008-12-18 18:31:38 +00003338 rc = p->rc;
drh94649b62019-12-18 02:12:04 +00003339 if( rc ){
3340 db->autoCommit = 0;
3341 }else{
3342 db->isTransactionSavepoint = 0;
3343 }
danielk1977fd7f0452008-12-17 17:30:26 +00003344 }else{
drh47b7fc72014-11-11 01:33:57 +00003345 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003346 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003347 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003348 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003349 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003350 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3351 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003352 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003353 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003354 }
drh47b7fc72014-11-11 01:33:57 +00003355 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003356 assert( p1==SAVEPOINT_RELEASE );
drh47b7fc72014-11-11 01:33:57 +00003357 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003358 }
3359 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003360 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3361 if( rc!=SQLITE_OK ){
3362 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003363 }
danielk1977fd7f0452008-12-17 17:30:26 +00003364 }
drh47b7fc72014-11-11 01:33:57 +00003365 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003366 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003367 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003368 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003369 }
3370 }
drh95866af2019-12-15 00:36:33 +00003371 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003372
3373 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3374 ** savepoints nested inside of the savepoint being operated on. */
3375 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003376 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003377 db->pSavepoint = pTmp->pNext;
3378 sqlite3DbFree(db, pTmp);
3379 db->nSavepoint--;
3380 }
3381
dan1da40a32009-09-19 17:00:31 +00003382 /* If it is a RELEASE, then destroy the savepoint being operated on
3383 ** too. If it is a ROLLBACK TO, then set the number of deferred
3384 ** constraint violations present in the database to the value stored
3385 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003386 if( p1==SAVEPOINT_RELEASE ){
3387 assert( pSavepoint==db->pSavepoint );
3388 db->pSavepoint = pSavepoint->pNext;
3389 sqlite3DbFree(db, pSavepoint);
3390 if( !isTransaction ){
3391 db->nSavepoint--;
3392 }
dan1da40a32009-09-19 17:00:31 +00003393 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003394 assert( p1==SAVEPOINT_ROLLBACK );
dan1da40a32009-09-19 17:00:31 +00003395 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003396 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003397 }
dand9495cd2011-04-27 12:08:04 +00003398
danea8562e2015-04-18 16:25:54 +00003399 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003400 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3401 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3402 }
danielk1977fd7f0452008-12-17 17:30:26 +00003403 }
3404 }
drh9467abf2016-02-17 18:44:11 +00003405 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003406
3407 break;
3408}
3409
drh98757152008-01-09 23:04:12 +00003410/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003411**
3412** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003413** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003414** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3415** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003416**
3417** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003418*/
drh9cbf3422008-01-17 16:22:13 +00003419case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003420 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003421 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003422
drh856c1032009-06-02 15:21:42 +00003423 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003424 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003425 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003426 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003427 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003428 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003429
drhb0c88652016-02-01 13:21:13 +00003430 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003431 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003432 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003433 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003434 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003435 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3436 /* If this instruction implements a COMMIT and other VMs are writing
3437 ** return an error indicating that the other VMs must complete first.
3438 */
3439 sqlite3VdbeError(p, "cannot commit transaction - "
3440 "SQL statements in progress");
3441 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003442 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003443 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003444 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003445 }else{
shane7d3846a2008-12-11 02:58:26 +00003446 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003447 }
3448 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3449 p->pc = (int)(pOp - aOp);
3450 db->autoCommit = (u8)(1-desiredAutoCommit);
3451 p->rc = rc = SQLITE_BUSY;
3452 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003453 }
danielk1977fd7f0452008-12-17 17:30:26 +00003454 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003455 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003456 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003457 }else{
drh900b31e2007-08-28 02:27:51 +00003458 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003459 }
drh900b31e2007-08-28 02:27:51 +00003460 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003461 }else{
drh22c17b82015-05-15 04:13:15 +00003462 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003463 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003464 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003465 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003466
3467 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003468 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003469 }
drh8616cff2019-07-13 16:15:23 +00003470 /*NOTREACHED*/ assert(0);
drh663fc632002-02-02 18:49:19 +00003471}
3472
drhb22f7c82014-02-06 23:56:27 +00003473/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003474**
drh05a86c52014-02-16 01:55:49 +00003475** Begin a transaction on database P1 if a transaction is not already
3476** active.
3477** If P2 is non-zero, then a write-transaction is started, or if a
3478** read-transaction is already active, it is upgraded to a write-transaction.
3479** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003480**
drh001bbcb2003-03-19 03:14:00 +00003481** P1 is the index of the database file on which the transaction is
3482** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003483** file used for temporary tables. Indices of 2 or more are used for
3484** attached databases.
drhcabb0812002-09-14 13:47:32 +00003485**
dane0af83a2009-09-08 19:15:01 +00003486** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3487** true (this flag is set if the Vdbe may modify more than one row and may
3488** throw an ABORT exception), a statement transaction may also be opened.
3489** More specifically, a statement transaction is opened iff the database
3490** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003491** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003492** VDBE to be rolled back after an error without having to roll back the
3493** entire transaction. If no error is encountered, the statement transaction
3494** will automatically commit when the VDBE halts.
3495**
drhb22f7c82014-02-06 23:56:27 +00003496** If P5!=0 then this opcode also checks the schema cookie against P3
3497** and the schema generation counter against P4.
3498** The cookie changes its value whenever the database schema changes.
3499** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003500** and that the current process needs to reread the schema. If the schema
3501** cookie in P3 differs from the schema cookie in the database header or
3502** if the schema generation counter in P4 differs from the current
3503** generation counter, then an SQLITE_SCHEMA error is raised and execution
3504** halts. The sqlite3_step() wrapper function might then reprepare the
3505** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003506*/
drh9cbf3422008-01-17 16:22:13 +00003507case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003508 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003509 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003510
drh1713afb2013-06-28 01:24:57 +00003511 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003512 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003513 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003514 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003515 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3516 rc = SQLITE_READONLY;
3517 goto abort_due_to_error;
3518 }
drh653b82a2009-06-22 11:10:47 +00003519 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003520
danielk197724162fe2004-06-04 06:22:00 +00003521 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003522 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003523 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3524 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003525 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003526 if( (rc&0xff)==SQLITE_BUSY ){
3527 p->pc = (int)(pOp - aOp);
3528 p->rc = rc;
3529 goto vdbe_return;
3530 }
danielk197724162fe2004-06-04 06:22:00 +00003531 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003532 }
dane0af83a2009-09-08 19:15:01 +00003533
drh4d294482019-10-05 15:28:24 +00003534 if( p->usesStmtJournal
3535 && pOp->p2
danc0537fe2013-06-28 19:41:43 +00003536 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003537 ){
3538 assert( sqlite3BtreeIsInTrans(pBt) );
3539 if( p->iStatement==0 ){
3540 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3541 db->nStatement++;
3542 p->iStatement = db->nSavepoint + db->nStatement;
3543 }
dana311b802011-04-26 19:21:34 +00003544
drh346506f2011-05-25 01:16:42 +00003545 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003546 if( rc==SQLITE_OK ){
3547 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3548 }
dan1da40a32009-09-19 17:00:31 +00003549
3550 /* Store the current value of the database handles deferred constraint
3551 ** counter. If the statement transaction needs to be rolled back,
3552 ** the value of this counter needs to be restored too. */
3553 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003554 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003555 }
drh397776a2018-06-06 17:45:51 +00003556 }
3557 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3558 if( pOp->p5
3559 && (iMeta!=pOp->p3
3560 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3561 ){
3562 /*
drh96fdcb42016-09-27 00:09:33 +00003563 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3564 ** version is checked to ensure that the schema has not changed since the
3565 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003566 */
drhb22f7c82014-02-06 23:56:27 +00003567 sqlite3DbFree(db, p->zErrMsg);
3568 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3569 /* If the schema-cookie from the database file matches the cookie
3570 ** stored with the in-memory representation of the schema, do
3571 ** not reload the schema from the database file.
3572 **
3573 ** If virtual-tables are in use, this is not just an optimization.
3574 ** Often, v-tables store their data in other SQLite tables, which
3575 ** are queried from within xNext() and other v-table methods using
3576 ** prepared queries. If such a query is out-of-date, we do not want to
3577 ** discard the database schema, as the user code implementing the
3578 ** v-table would have to be ready for the sqlite3_vtab structure itself
3579 ** to be invalidated whenever sqlite3_step() is called from within
3580 ** a v-table method.
3581 */
3582 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3583 sqlite3ResetOneSchema(db, pOp->p1);
3584 }
3585 p->expired = 1;
3586 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003587 }
drh9467abf2016-02-17 18:44:11 +00003588 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003589 break;
3590}
3591
drhb1fdb2a2008-01-05 04:06:03 +00003592/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003593**
drh9cbf3422008-01-17 16:22:13 +00003594** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003595** P3==1 is the schema version. P3==2 is the database format.
3596** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003597** the main database file and P1==1 is the database file used to store
3598** temporary tables.
drh4a324312001-12-21 14:30:42 +00003599**
drh50e5dad2001-09-15 00:57:28 +00003600** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003601** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003602** executing this instruction.
3603*/
drh27a348c2015-04-13 19:14:06 +00003604case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003605 int iMeta;
drh856c1032009-06-02 15:21:42 +00003606 int iDb;
3607 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003608
drh1713afb2013-06-28 01:24:57 +00003609 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003610 iDb = pOp->p1;
3611 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003612 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003613 assert( iDb>=0 && iDb<db->nDb );
3614 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003615 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003616
danielk1977602b4662009-07-02 07:47:33 +00003617 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003618 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003619 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003620 break;
3621}
3622
drh98757152008-01-09 23:04:12 +00003623/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003624**
drh1861afc2016-02-01 21:48:34 +00003625** Write the integer value P3 into cookie number P2 of database P1.
3626** P2==1 is the schema version. P2==2 is the database format.
3627** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003628** size, and so forth. P1==0 is the main database file and P1==1 is the
3629** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003630**
3631** A transaction must be started before executing this opcode.
3632*/
drh1861afc2016-02-01 21:48:34 +00003633case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003634 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003635
3636 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003637 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003638 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003639 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003640 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003641 pDb = &db->aDb[pOp->p1];
3642 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003643 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003644 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003645 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003646 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003647 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003648 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003649 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003650 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003651 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003652 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003653 }
drhfd426c62006-01-30 15:34:22 +00003654 if( pOp->p1==1 ){
3655 /* Invalidate all prepared statements whenever the TEMP database
3656 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003657 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003658 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003659 }
drh9467abf2016-02-17 18:44:11 +00003660 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003661 break;
3662}
3663
drh98757152008-01-09 23:04:12 +00003664/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003665** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003666**
drhecdc7532001-09-23 02:35:53 +00003667** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003668** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003669** P3==0 means the main database, P3==1 means the database used for
3670** temporary tables, and P3>1 means used the corresponding attached
3671** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003672** values need not be contiguous but all P1 values should be small integers.
3673** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003674**
drh8e9deb62018-06-05 13:43:02 +00003675** Allowed P5 bits:
3676** <ul>
3677** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3678** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003679** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003680** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003681**
danielk1977d336e222009-02-20 10:58:41 +00003682** The P4 value may be either an integer (P4_INT32) or a pointer to
3683** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003684** object, then table being opened must be an [index b-tree] where the
3685** KeyInfo object defines the content and collating
3686** sequence of that index b-tree. Otherwise, if P4 is an integer
3687** value, then the table being opened must be a [table b-tree] with a
3688** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003689**
drh35263192014-07-22 20:02:19 +00003690** See also: OpenWrite, ReopenIdx
3691*/
3692/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3693** Synopsis: root=P2 iDb=P3
3694**
drh8e9deb62018-06-05 13:43:02 +00003695** The ReopenIdx opcode works like OP_OpenRead except that it first
3696** checks to see if the cursor on P1 is already open on the same
3697** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003698** if the cursor is already open, do not reopen it.
3699**
drh8e9deb62018-06-05 13:43:02 +00003700** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3701** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3702** be the same as every other ReopenIdx or OpenRead for the same cursor
3703** number.
drh35263192014-07-22 20:02:19 +00003704**
drh8e9deb62018-06-05 13:43:02 +00003705** Allowed P5 bits:
3706** <ul>
3707** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3708** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003709** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003710** </ul>
3711**
3712** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003713*/
drh98757152008-01-09 23:04:12 +00003714/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003715** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003716**
3717** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003718** page is P2 (or whose root page is held in register P2 if the
3719** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003720**
danielk1977d336e222009-02-20 10:58:41 +00003721** The P4 value may be either an integer (P4_INT32) or a pointer to
3722** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003723** object, then table being opened must be an [index b-tree] where the
3724** KeyInfo object defines the content and collating
3725** sequence of that index b-tree. Otherwise, if P4 is an integer
3726** value, then the table being opened must be a [table b-tree] with a
3727** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003728**
drh8e9deb62018-06-05 13:43:02 +00003729** Allowed P5 bits:
3730** <ul>
3731** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3732** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003733** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003734** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3735** and subsequently delete entries in an index btree. This is a
3736** hint to the storage engine that the storage engine is allowed to
3737** ignore. The hint is not used by the official SQLite b*tree storage
3738** engine, but is used by COMDB2.
3739** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3740** as the root page, not the value of P2 itself.
3741** </ul>
drhf57b3392001-10-08 13:22:32 +00003742**
drh8e9deb62018-06-05 13:43:02 +00003743** This instruction works like OpenRead except that it opens the cursor
3744** in read/write mode.
3745**
3746** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003747*/
drh35263192014-07-22 20:02:19 +00003748case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003749 int nField;
3750 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003751 int p2;
3752 int iDb;
drhf57b3392001-10-08 13:22:32 +00003753 int wrFlag;
3754 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003755 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003756 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003757
drhe0997b32015-03-20 14:57:50 +00003758 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003759 assert( pOp->p4type==P4_KEYINFO );
3760 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003761 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003762 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003763 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003764 }
3765 /* If the cursor is not currently open or is open on a different
3766 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003767case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003768case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003769
drhe0997b32015-03-20 14:57:50 +00003770 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003771 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003772 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3773 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003774
drhba968db2018-07-24 22:02:12 +00003775 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003776 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003777 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003778 }
3779
drh856c1032009-06-02 15:21:42 +00003780 nField = 0;
3781 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003782 p2 = pOp->p2;
3783 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003784 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003785 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003786 pDb = &db->aDb[iDb];
3787 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003788 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003789 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003790 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3791 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003792 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003793 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3794 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003795 }
3796 }else{
3797 wrFlag = 0;
3798 }
dan428c2182012-08-06 18:50:11 +00003799 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003800 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003801 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003802 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003803 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003804 assert( memIsValid(pIn2) );
3805 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003806 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003807 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003808 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003809 ** that opcode will always set the p2 value to 2 or more or else fail.
3810 ** If there were a failure, the prepared statement would have halted
3811 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003812 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003813 }
danielk1977d336e222009-02-20 10:58:41 +00003814 if( pOp->p4type==P4_KEYINFO ){
3815 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003816 assert( pKeyInfo->enc==ENC(db) );
3817 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003818 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003819 }else if( pOp->p4type==P4_INT32 ){
3820 nField = pOp->p4.i;
3821 }
drh653b82a2009-06-22 11:10:47 +00003822 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003823 assert( nField>=0 );
3824 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003825 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003826 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003827 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003828 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003829 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003830#ifdef SQLITE_DEBUG
3831 pCur->wrFlag = wrFlag;
3832#endif
drhc960dcb2015-11-20 19:22:01 +00003833 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003834 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003835 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003836 ** SQLite used to check if the root-page flags were sane at this point
3837 ** and report database corruption if they were not, but this check has
3838 ** since moved into the btree layer. */
3839 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003840
3841open_cursor_set_hints:
3842 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3843 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003844 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh0403cb32015-08-14 23:57:04 +00003845 testcase( pOp->p2 & OPFLAG_SEEKEQ );
drhc960dcb2015-11-20 19:22:01 +00003846 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003847 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003848 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003849 break;
3850}
3851
drhe08e8d62017-05-01 15:15:41 +00003852/* Opcode: OpenDup P1 P2 * * *
3853**
3854** Open a new cursor P1 that points to the same ephemeral table as
3855** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3856** opcode. Only ephemeral cursors may be duplicated.
3857**
3858** Duplicate ephemeral cursors are used for self-joins of materialized views.
3859*/
3860case OP_OpenDup: {
3861 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3862 VdbeCursor *pCx; /* The new cursor */
3863
3864 pOrig = p->apCsr[pOp->p2];
dan2811ea62019-12-23 14:20:46 +00003865 assert( pOrig );
drhe08e8d62017-05-01 15:15:41 +00003866 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3867
3868 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3869 if( pCx==0 ) goto no_mem;
3870 pCx->nullRow = 1;
3871 pCx->isEphemeral = 1;
3872 pCx->pKeyInfo = pOrig->pKeyInfo;
3873 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00003874 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00003875 pCx->isOrdered = pOrig->isOrdered;
drh2c041312018-12-24 02:34:49 +00003876 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00003877 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003878 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3879 ** opened for a database. Since there is already an open cursor when this
3880 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3881 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003882 break;
3883}
3884
3885
drh2a5d9902011-08-26 00:34:45 +00003886/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003887** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003888**
drhb9bb7c12006-06-11 23:41:55 +00003889** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003890** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003891** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003892** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003893**
drhdfe3b582019-01-04 12:35:50 +00003894** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00003895** is cleared (all content is erased).
3896**
drh25d3adb2010-04-05 15:11:08 +00003897** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003898** The cursor points to a BTree table if P4==0 and to a BTree index
3899** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003900** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003901**
drh2a5d9902011-08-26 00:34:45 +00003902** The P5 parameter can be a mask of the BTREE_* flags defined
3903** in btree.h. These flags control aspects of the operation of
3904** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3905** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003906*/
drha21a64d2010-04-06 22:33:55 +00003907/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003908** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003909**
3910** This opcode works the same as OP_OpenEphemeral. It has a
3911** different name to distinguish its use. Tables created using
3912** by this opcode will be used for automatically created transient
3913** indices in joins.
3914*/
3915case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003916case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003917 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003918 KeyInfo *pKeyInfo;
3919
drhd4187c72010-08-30 22:15:45 +00003920 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003921 SQLITE_OPEN_READWRITE |
3922 SQLITE_OPEN_CREATE |
3923 SQLITE_OPEN_EXCLUSIVE |
3924 SQLITE_OPEN_DELETEONCLOSE |
3925 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003926 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003927 assert( pOp->p2>=0 );
drh4afdfa12018-12-31 16:36:42 +00003928 pCx = p->apCsr[pOp->p1];
drh1ee02a12020-01-18 13:53:46 +00003929 if( pCx && pCx->pBtx ){
drh4afdfa12018-12-31 16:36:42 +00003930 /* If the ephermeral table is already open, erase all existing content
3931 ** so that the table is empty again, rather than creating a new table. */
dana5129722019-05-03 18:50:24 +00003932 assert( pCx->isEphemeral );
dan855b5d12019-06-26 21:04:30 +00003933 pCx->seqCount = 0;
3934 pCx->cacheStatus = CACHE_STALE;
drh1ee02a12020-01-18 13:53:46 +00003935 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00003936 }else{
3937 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3938 if( pCx==0 ) goto no_mem;
drhd0fb7962018-12-31 17:58:05 +00003939 pCx->isEphemeral = 1;
3940 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3941 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
3942 vfsFlags);
3943 if( rc==SQLITE_OK ){
3944 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drhc6b52df2002-01-04 03:09:29 +00003945 }
drhd0fb7962018-12-31 17:58:05 +00003946 if( rc==SQLITE_OK ){
3947 /* If a transient index is required, create it by calling
3948 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3949 ** opening it. If a transient table is required, just use the
3950 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3951 */
3952 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3953 assert( pOp->p4type==P4_KEYINFO );
3954 rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
3955 BTREE_BLOBKEY | pOp->p5);
3956 if( rc==SQLITE_OK ){
3957 assert( pCx->pgnoRoot==MASTER_ROOT+1 );
3958 assert( pKeyInfo->db==db );
3959 assert( pKeyInfo->enc==ENC(db) );
3960 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
3961 pKeyInfo, pCx->uc.pCursor);
3962 }
3963 pCx->isTable = 0;
3964 }else{
3965 pCx->pgnoRoot = MASTER_ROOT;
3966 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
3967 0, pCx->uc.pCursor);
3968 pCx->isTable = 1;
3969 }
3970 }
3971 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drh5e00f6c2001-09-13 13:46:56 +00003972 }
drh9467abf2016-02-17 18:44:11 +00003973 if( rc ) goto abort_due_to_error;
dan855b5d12019-06-26 21:04:30 +00003974 pCx->nullRow = 1;
dan5134d132011-09-02 10:31:11 +00003975 break;
3976}
3977
danfad9f9a2014-04-01 18:41:51 +00003978/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003979**
3980** This opcode works like OP_OpenEphemeral except that it opens
3981** a transient index that is specifically designed to sort large
3982** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003983**
3984** If argument P3 is non-zero, then it indicates that the sorter may
3985** assume that a stable sort considering the first P3 fields of each
3986** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003987*/
drhca892a72011-09-03 00:17:51 +00003988case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003989 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003990
drh399af1d2013-11-20 17:25:55 +00003991 assert( pOp->p1>=0 );
3992 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003993 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003994 if( pCx==0 ) goto no_mem;
3995 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003996 assert( pCx->pKeyInfo->db==db );
3997 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003998 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003999 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004000 break;
4001}
4002
dan78d58432014-03-25 15:04:07 +00004003/* Opcode: SequenceTest P1 P2 * * *
4004** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4005**
4006** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4007** to P2. Regardless of whether or not the jump is taken, increment the
4008** the sequence value.
4009*/
4010case OP_SequenceTest: {
4011 VdbeCursor *pC;
4012 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4013 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004014 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00004015 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00004016 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00004017 }
drh5e00f6c2001-09-13 13:46:56 +00004018 break;
4019}
4020
drh5f612292014-02-08 23:20:32 +00004021/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00004022** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00004023**
4024** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00004025** row of data. The content of that one row is the content of memory
4026** register P2. In other words, cursor P1 becomes an alias for the
4027** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00004028**
drh2d8d7ce2010-02-15 15:17:05 +00004029** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00004030** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00004031** individual columns using the OP_Column opcode. The OP_Column opcode
4032** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00004033**
4034** P3 is the number of fields in the records that will be stored by
4035** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004036*/
drh9cbf3422008-01-17 16:22:13 +00004037case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00004038 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00004039
drh653b82a2009-06-22 11:10:47 +00004040 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004041 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00004042 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00004043 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00004044 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004045 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00004046 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004047 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4048 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4049 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4050 ** which is a performance optimization */
4051 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00004052 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00004053 break;
4054}
4055
drh98757152008-01-09 23:04:12 +00004056/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00004057**
4058** Close a cursor previously opened as P1. If P1 is not
4059** currently open, this instruction is a no-op.
4060*/
drh9cbf3422008-01-17 16:22:13 +00004061case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00004062 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4063 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
4064 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004065 break;
4066}
4067
drh97bae792015-06-05 15:59:57 +00004068#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4069/* Opcode: ColumnsUsed P1 * * P4 *
4070**
4071** This opcode (which only exists if SQLite was compiled with
4072** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4073** table or index for cursor P1 are used. P4 is a 64-bit integer
4074** (P4_INT64) in which the first 63 bits are one for each of the
4075** first 63 columns of the table or index that are actually used
4076** by the cursor. The high-order bit is set if any column after
4077** the 64th is used.
4078*/
4079case OP_ColumnsUsed: {
4080 VdbeCursor *pC;
4081 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004082 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00004083 pC->maskUsed = *(u64*)pOp->p4.pI64;
4084 break;
4085}
4086#endif
4087
drh8af3f772014-07-25 18:01:06 +00004088/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004089** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004090**
danielk1977b790c6c2008-04-18 10:25:24 +00004091** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004092** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004093** to an SQL index, then P3 is the first in an array of P4 registers
4094** that are used as an unpacked index key.
4095**
4096** Reposition cursor P1 so that it points to the smallest entry that
4097** is greater than or equal to the key value. If there are no records
4098** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004099**
drhb1d607d2015-11-05 22:30:54 +00004100** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004101** opcode will either land on a record that exactly matches the key, or
4102** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4103** this opcode must be followed by an IdxLE opcode with the same arguments.
4104** The IdxGT opcode will be skipped if this opcode succeeds, but the
4105** IdxGT opcode will be used on subsequent loop iterations. The
4106** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4107** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004108**
drh8af3f772014-07-25 18:01:06 +00004109** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00004110** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004111** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004112**
drh935850e2014-05-24 17:15:15 +00004113** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004114*/
drh8af3f772014-07-25 18:01:06 +00004115/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004116** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00004117**
danielk1977b790c6c2008-04-18 10:25:24 +00004118** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004119** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004120** to an SQL index, then P3 is the first in an array of P4 registers
4121** that are used as an unpacked index key.
4122**
drh576d0a92020-03-12 17:28:27 +00004123** Reposition cursor P1 so that it points to the smallest entry that
danielk1977b790c6c2008-04-18 10:25:24 +00004124** is greater than the key value. If there are no records greater than
4125** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00004126**
drh8af3f772014-07-25 18:01:06 +00004127** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004128** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004129** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004130**
drh935850e2014-05-24 17:15:15 +00004131** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00004132*/
drh8af3f772014-07-25 18:01:06 +00004133/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004134** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004135**
danielk1977b790c6c2008-04-18 10:25:24 +00004136** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004137** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004138** to an SQL index, then P3 is the first in an array of P4 registers
4139** that are used as an unpacked index key.
4140**
4141** Reposition cursor P1 so that it points to the largest entry that
4142** is less than the key value. If there are no records less than
4143** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00004144**
drh8af3f772014-07-25 18:01:06 +00004145** This opcode leaves the cursor configured to move in reverse order,
4146** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004147** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004148**
drh935850e2014-05-24 17:15:15 +00004149** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004150*/
drh8af3f772014-07-25 18:01:06 +00004151/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004152** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00004153**
danielk1977b790c6c2008-04-18 10:25:24 +00004154** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004155** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004156** to an SQL index, then P3 is the first in an array of P4 registers
4157** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00004158**
danielk1977b790c6c2008-04-18 10:25:24 +00004159** Reposition cursor P1 so that it points to the largest entry that
4160** is less than or equal to the key value. If there are no records
4161** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004162**
drh8af3f772014-07-25 18:01:06 +00004163** This opcode leaves the cursor configured to move in reverse order,
4164** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004165** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004166**
drhb1d607d2015-11-05 22:30:54 +00004167** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004168** opcode will either land on a record that exactly matches the key, or
4169** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4170** this opcode must be followed by an IdxLE opcode with the same arguments.
drhb1d607d2015-11-05 22:30:54 +00004171** The IdxGE opcode will be skipped if this opcode succeeds, but the
drh576d0a92020-03-12 17:28:27 +00004172** IdxGE opcode will be used on subsequent loop iterations. The
4173** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4174** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004175**
drh935850e2014-05-24 17:15:15 +00004176** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00004177*/
mistachkin758784d2018-07-25 15:12:29 +00004178case OP_SeekLT: /* jump, in3, group */
4179case OP_SeekLE: /* jump, in3, group */
4180case OP_SeekGE: /* jump, in3, group */
4181case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00004182 int res; /* Comparison result */
4183 int oc; /* Opcode */
4184 VdbeCursor *pC; /* The cursor to seek */
4185 UnpackedRecord r; /* The key to seek for */
4186 int nField; /* Number of columns or fields in the key */
4187 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00004188 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00004189
drh653b82a2009-06-22 11:10:47 +00004190 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00004191 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00004192 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004193 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004194 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00004195 assert( OP_SeekLE == OP_SeekLT+1 );
4196 assert( OP_SeekGE == OP_SeekLT+2 );
4197 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00004198 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00004199 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004200 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00004201 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00004202 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00004203#ifdef SQLITE_DEBUG
4204 pC->seekOp = pOp->opcode;
4205#endif
drhe0997b32015-03-20 14:57:50 +00004206
dana40cb962019-05-14 20:25:22 +00004207 pC->deferredMoveto = 0;
4208 pC->cacheStatus = CACHE_STALE;
drh3da046d2013-11-11 03:24:11 +00004209 if( pC->isTable ){
drh3e364802019-08-22 00:53:16 +00004210 u16 flags3, newType;
drh576d0a92020-03-12 17:28:27 +00004211 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00004212 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
4213 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00004214
drh3da046d2013-11-11 03:24:11 +00004215 /* The input value in P3 might be of any type: integer, real, string,
4216 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00004217 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00004218 pIn3 = &aMem[pOp->p3];
drh3e364802019-08-22 00:53:16 +00004219 flags3 = pIn3->flags;
4220 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00004221 applyNumericAffinity(pIn3, 0);
4222 }
drh3e364802019-08-22 00:53:16 +00004223 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
4224 newType = pIn3->flags; /* Record the type after applying numeric affinity */
4225 pIn3->flags = flags3; /* But convert the type back to its original */
drh959403f2008-12-12 17:56:16 +00004226
drh3da046d2013-11-11 03:24:11 +00004227 /* If the P3 value could not be converted into an integer without
4228 ** loss of information, then special processing is required... */
drh3e364802019-08-22 00:53:16 +00004229 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
4230 if( (newType & MEM_Real)==0 ){
4231 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
drh8616cff2019-07-13 16:15:23 +00004232 VdbeBranchTaken(1,2);
4233 goto jump_to_p2;
dan9edd8c12019-05-08 11:42:49 +00004234 }else{
dan873b0192019-05-09 11:19:27 +00004235 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4236 if( rc!=SQLITE_OK ) goto abort_due_to_error;
dan9edd8c12019-05-08 11:42:49 +00004237 goto seek_not_found;
4238 }
4239 }else
drh959403f2008-12-12 17:56:16 +00004240
danaa1776f2013-11-26 18:22:59 +00004241 /* If the approximation iKey is larger than the actual real search
4242 ** term, substitute >= for > and < for <=. e.g. if the search term
4243 ** is 4.9 and the integer approximation 5:
4244 **
4245 ** (x > 4.9) -> (x >= 5)
4246 ** (x <= 4.9) -> (x < 5)
4247 */
drh74eaba42014-09-18 17:52:15 +00004248 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004249 assert( OP_SeekGE==(OP_SeekGT-1) );
4250 assert( OP_SeekLT==(OP_SeekLE-1) );
4251 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
4252 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00004253 }
4254
4255 /* If the approximation iKey is smaller than the actual real search
4256 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00004257 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004258 assert( OP_SeekLE==(OP_SeekLT+1) );
4259 assert( OP_SeekGT==(OP_SeekGE+1) );
4260 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4261 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004262 }
dan9edd8c12019-05-08 11:42:49 +00004263 }
drhc960dcb2015-11-20 19:22:01 +00004264 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004265 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004266 if( rc!=SQLITE_OK ){
4267 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004268 }
drhaa736092009-06-22 00:55:30 +00004269 }else{
drh576d0a92020-03-12 17:28:27 +00004270 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4271 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4272 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4273 ** with the same key.
drhd6b79462015-11-07 01:19:00 +00004274 */
drhc960dcb2015-11-20 19:22:01 +00004275 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004276 eqOnly = 1;
4277 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4278 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
drh576d0a92020-03-12 17:28:27 +00004279 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
4280 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
drhd6b79462015-11-07 01:19:00 +00004281 assert( pOp[1].p1==pOp[0].p1 );
4282 assert( pOp[1].p2==pOp[0].p2 );
4283 assert( pOp[1].p3==pOp[0].p3 );
4284 assert( pOp[1].p4.i==pOp[0].p4.i );
4285 }
4286
drh3da046d2013-11-11 03:24:11 +00004287 nField = pOp->p4.i;
4288 assert( pOp->p4type==P4_INT32 );
4289 assert( nField>0 );
4290 r.pKeyInfo = pC->pKeyInfo;
4291 r.nField = (u16)nField;
4292
4293 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004294 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004295 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004296 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004297 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004298 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004299 */
dan1fed5da2014-02-25 21:01:25 +00004300 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4301 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4302 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4303 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4304 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004305
4306 r.aMem = &aMem[pOp->p3];
4307#ifdef SQLITE_DEBUG
4308 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4309#endif
drh70528d72015-11-05 20:25:09 +00004310 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004311 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004312 if( rc!=SQLITE_OK ){
4313 goto abort_due_to_error;
4314 }
drhb1d607d2015-11-05 22:30:54 +00004315 if( eqOnly && r.eqSeen==0 ){
4316 assert( res!=0 );
4317 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004318 }
drh3da046d2013-11-11 03:24:11 +00004319 }
drh3da046d2013-11-11 03:24:11 +00004320#ifdef SQLITE_TEST
4321 sqlite3_search_count++;
4322#endif
drh4a1d3652014-02-14 15:13:36 +00004323 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4324 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004325 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004326 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4327 if( rc!=SQLITE_OK ){
4328 if( rc==SQLITE_DONE ){
4329 rc = SQLITE_OK;
4330 res = 1;
4331 }else{
4332 goto abort_due_to_error;
4333 }
4334 }
drh3da046d2013-11-11 03:24:11 +00004335 }else{
4336 res = 0;
4337 }
4338 }else{
drh4a1d3652014-02-14 15:13:36 +00004339 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4340 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004341 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004342 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4343 if( rc!=SQLITE_OK ){
4344 if( rc==SQLITE_DONE ){
4345 rc = SQLITE_OK;
4346 res = 1;
4347 }else{
4348 goto abort_due_to_error;
4349 }
4350 }
drh3da046d2013-11-11 03:24:11 +00004351 }else{
4352 /* res might be negative because the table is empty. Check to
4353 ** see if this is the case.
4354 */
drhc960dcb2015-11-20 19:22:01 +00004355 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004356 }
4357 }
drhb1d607d2015-11-05 22:30:54 +00004358seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004359 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004360 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004361 if( res ){
drhf56fa462015-04-13 21:39:54 +00004362 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004363 }else if( eqOnly ){
4364 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4365 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004366 }
drh5e00f6c2001-09-13 13:46:56 +00004367 break;
4368}
dan71c57db2016-07-09 20:23:55 +00004369
drh8c2b6d72018-06-05 20:45:20 +00004370/* Opcode: SeekHit P1 P2 * * *
4371** Synopsis: seekHit=P2
4372**
4373** Set the seekHit flag on cursor P1 to the value in P2.
dan74ebaad2020-01-04 16:55:57 +00004374* The seekHit flag is used by the IfNoHope opcode.
drh8c2b6d72018-06-05 20:45:20 +00004375**
4376** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4377** either 0 or 1.
4378*/
4379case OP_SeekHit: {
4380 VdbeCursor *pC;
4381 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4382 pC = p->apCsr[pOp->p1];
4383 assert( pC!=0 );
4384 assert( pOp->p2==0 || pOp->p2==1 );
4385 pC->seekHit = pOp->p2 & 1;
4386 break;
4387}
4388
dan74ebaad2020-01-04 16:55:57 +00004389/* Opcode: IfNotOpen P1 P2 * * *
4390** Synopsis: if( !csr[P1] ) goto P2
4391**
4392** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
4393*/
4394case OP_IfNotOpen: { /* jump */
4395 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh56ea69b2020-01-04 18:33:20 +00004396 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
dan74ebaad2020-01-04 16:55:57 +00004397 if( !p->apCsr[pOp->p1] ){
4398 goto jump_to_p2_and_check_for_interrupt;
4399 }
4400 break;
4401}
4402
drh8cff69d2009-11-12 19:59:44 +00004403/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004404** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004405**
drh8cff69d2009-11-12 19:59:44 +00004406** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4407** P4>0 then register P3 is the first of P4 registers that form an unpacked
4408** record.
4409**
4410** Cursor P1 is on an index btree. If the record identified by P3 and P4
4411** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004412** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004413**
drhcefc87f2014-08-01 01:40:33 +00004414** This operation leaves the cursor in a state where it can be
4415** advanced in the forward direction. The Next instruction will work,
4416** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004417**
drh6f225d02013-10-26 13:36:51 +00004418** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004419*/
drh8cff69d2009-11-12 19:59:44 +00004420/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004421** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004422**
drh8cff69d2009-11-12 19:59:44 +00004423** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4424** P4>0 then register P3 is the first of P4 registers that form an unpacked
4425** record.
4426**
4427** Cursor P1 is on an index btree. If the record identified by P3 and P4
4428** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4429** does contain an entry whose prefix matches the P3/P4 record then control
4430** falls through to the next instruction and P1 is left pointing at the
4431** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004432**
drh8af3f772014-07-25 18:01:06 +00004433** This operation leaves the cursor in a state where it cannot be
4434** advanced in either direction. In other words, the Next and Prev
4435** opcodes do not work after this operation.
4436**
drh8c2b6d72018-06-05 20:45:20 +00004437** See also: Found, NotExists, NoConflict, IfNoHope
4438*/
4439/* Opcode: IfNoHope P1 P2 P3 P4 *
4440** Synopsis: key=r[P3@P4]
4441**
4442** Register P3 is the first of P4 registers that form an unpacked
4443** record.
4444**
4445** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4446** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4447** check to see if there is any entry in P1 that matches the
4448** prefix identified by P3 and P4. If no entry matches the prefix,
4449** jump to P2. Otherwise fall through.
4450**
4451** This opcode behaves like OP_NotFound if the seekHit
4452** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4453**
4454** This opcode is used in IN clause processing for a multi-column key.
4455** If an IN clause is attached to an element of the key other than the
4456** left-most element, and if there are no matches on the most recent
4457** seek over the whole key, then it might be that one of the key element
4458** to the left is prohibiting a match, and hence there is "no hope" of
4459** any match regardless of how many IN clause elements are checked.
4460** In such a case, we abandon the IN clause search early, using this
4461** opcode. The opcode name comes from the fact that the
4462** jump is taken if there is "no hope" of achieving a match.
4463**
4464** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004465*/
drh6f225d02013-10-26 13:36:51 +00004466/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004467** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004468**
4469** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4470** P4>0 then register P3 is the first of P4 registers that form an unpacked
4471** record.
4472**
4473** Cursor P1 is on an index btree. If the record identified by P3 and P4
4474** contains any NULL value, jump immediately to P2. If all terms of the
4475** record are not-NULL then a check is done to determine if any row in the
4476** P1 index btree has a matching key prefix. If there are no matches, jump
4477** immediately to P2. If there is a match, fall through and leave the P1
4478** cursor pointing to the matching row.
4479**
4480** This opcode is similar to OP_NotFound with the exceptions that the
4481** branch is always taken if any part of the search key input is NULL.
4482**
drh8af3f772014-07-25 18:01:06 +00004483** This operation leaves the cursor in a state where it cannot be
4484** advanced in either direction. In other words, the Next and Prev
4485** opcodes do not work after this operation.
4486**
drh6f225d02013-10-26 13:36:51 +00004487** See also: NotFound, Found, NotExists
4488*/
drh8c2b6d72018-06-05 20:45:20 +00004489case OP_IfNoHope: { /* jump, in3 */
4490 VdbeCursor *pC;
4491 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4492 pC = p->apCsr[pOp->p1];
4493 assert( pC!=0 );
4494 if( pC->seekHit ) break;
4495 /* Fall through into OP_NotFound */
4496}
drh6f225d02013-10-26 13:36:51 +00004497case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004498case OP_NotFound: /* jump, in3 */
4499case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004500 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004501 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004502 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004503 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004504 int res;
drha582b012016-12-21 19:45:54 +00004505 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004506 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004507 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004508
dan0ff297e2009-09-25 17:03:14 +00004509#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004510 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004511#endif
4512
drhaa736092009-06-22 00:55:30 +00004513 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004514 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004515 pC = p->apCsr[pOp->p1];
4516 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004517#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004518 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004519#endif
drh3c657212009-11-17 23:59:58 +00004520 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004521 assert( pC->eCurType==CURTYPE_BTREE );
4522 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004523 assert( pC->isTable==0 );
4524 if( pOp->p4.i>0 ){
4525 r.pKeyInfo = pC->pKeyInfo;
4526 r.nField = (u16)pOp->p4.i;
4527 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004528#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004529 for(ii=0; ii<r.nField; ii++){
4530 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004531 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004532 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004533 }
drh8aaf7bc2016-09-20 01:19:18 +00004534#endif
drh3da046d2013-11-11 03:24:11 +00004535 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004536 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004537 }else{
drhe46515b2017-05-19 22:51:00 +00004538 assert( pIn3->flags & MEM_Blob );
4539 rc = ExpandBlob(pIn3);
4540 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4541 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004542 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004543 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004544 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004545 }
dan1fed5da2014-02-25 21:01:25 +00004546 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004547 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004548 if( pOp->opcode==OP_NoConflict ){
4549 /* For the OP_NoConflict opcode, take the jump if any of the
4550 ** input fields are NULL, since any key with a NULL will not
4551 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004552 for(ii=0; ii<pIdxKey->nField; ii++){
4553 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004554 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004555 break;
drh6f225d02013-10-26 13:36:51 +00004556 }
4557 }
drh5e00f6c2001-09-13 13:46:56 +00004558 }
drhc960dcb2015-11-20 19:22:01 +00004559 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004560 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004561 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004562 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004563 }
4564 pC->seekResult = res;
4565 alreadyExists = (res==0);
4566 pC->nullRow = 1-alreadyExists;
4567 pC->deferredMoveto = 0;
4568 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004569 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004570 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004571 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004572 }else{
drhf56fa462015-04-13 21:39:54 +00004573 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4574 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004575 }
drh5e00f6c2001-09-13 13:46:56 +00004576 break;
4577}
4578
drheeb95652016-05-26 20:56:38 +00004579/* Opcode: SeekRowid P1 P2 P3 * *
4580** Synopsis: intkey=r[P3]
4581**
4582** P1 is the index of a cursor open on an SQL table btree (with integer
4583** keys). If register P3 does not contain an integer or if P1 does not
4584** contain a record with rowid P3 then jump immediately to P2.
4585** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4586** a record with rowid P3 then
4587** leave the cursor pointing at that record and fall through to the next
4588** instruction.
4589**
4590** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4591** the P3 register must be guaranteed to contain an integer value. With this
4592** opcode, register P3 might not contain an integer.
4593**
4594** The OP_NotFound opcode performs the same operation on index btrees
4595** (with arbitrary multi-value keys).
4596**
4597** This opcode leaves the cursor in a state where it cannot be advanced
4598** in either direction. In other words, the Next and Prev opcodes will
4599** not work following this opcode.
4600**
4601** See also: Found, NotFound, NoConflict, SeekRowid
4602*/
drh9cbf3422008-01-17 16:22:13 +00004603/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004604** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004605**
drh261c02d2013-10-25 14:46:15 +00004606** P1 is the index of a cursor open on an SQL table btree (with integer
4607** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004608** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4609** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4610** leave the cursor pointing at that record and fall through to the next
4611** instruction.
drh6b125452002-01-28 15:53:03 +00004612**
drheeb95652016-05-26 20:56:38 +00004613** The OP_SeekRowid opcode performs the same operation but also allows the
4614** P3 register to contain a non-integer value, in which case the jump is
4615** always taken. This opcode requires that P3 always contain an integer.
4616**
drh261c02d2013-10-25 14:46:15 +00004617** The OP_NotFound opcode performs the same operation on index btrees
4618** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004619**
drh8af3f772014-07-25 18:01:06 +00004620** This opcode leaves the cursor in a state where it cannot be advanced
4621** in either direction. In other words, the Next and Prev opcodes will
4622** not work following this opcode.
4623**
drheeb95652016-05-26 20:56:38 +00004624** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004625*/
drheeb95652016-05-26 20:56:38 +00004626case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004627 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004628 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004629 int res;
4630 u64 iKey;
4631
drh3c657212009-11-17 23:59:58 +00004632 pIn3 = &aMem[pOp->p3];
drh3242c692019-05-04 01:29:13 +00004633 testcase( pIn3->flags & MEM_Int );
4634 testcase( pIn3->flags & MEM_IntReal );
drhb29ef5e2019-10-07 01:05:57 +00004635 testcase( pIn3->flags & MEM_Real );
4636 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
drh169f0772019-05-02 21:36:26 +00004637 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
drhb29ef5e2019-10-07 01:05:57 +00004638 /* If pIn3->u.i does not contain an integer, compute iKey as the
4639 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
4640 ** into an integer without loss of information. Take care to avoid
4641 ** changing the datatype of pIn3, however, as it is used by other
4642 ** parts of the prepared statement. */
4643 Mem x = pIn3[0];
4644 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
4645 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
4646 iKey = x.u.i;
4647 goto notExistsWithKey;
drheeb95652016-05-26 20:56:38 +00004648 }
4649 /* Fall through into OP_NotExists */
4650case OP_NotExists: /* jump, in3 */
4651 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004652 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004653 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhb29ef5e2019-10-07 01:05:57 +00004654 iKey = pIn3->u.i;
4655notExistsWithKey:
drhaa736092009-06-22 00:55:30 +00004656 pC = p->apCsr[pOp->p1];
4657 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004658#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00004659 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004660#endif
drhaa736092009-06-22 00:55:30 +00004661 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004662 assert( pC->eCurType==CURTYPE_BTREE );
4663 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004664 assert( pCrsr!=0 );
4665 res = 0;
drh3da046d2013-11-11 03:24:11 +00004666 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004667 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004668 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004669 pC->nullRow = 0;
4670 pC->cacheStatus = CACHE_STALE;
4671 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004672 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004673 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004674 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004675 assert( rc==SQLITE_OK );
4676 if( pOp->p2==0 ){
4677 rc = SQLITE_CORRUPT_BKPT;
4678 }else{
4679 goto jump_to_p2;
4680 }
danc6157e12015-09-14 09:23:47 +00004681 }
drh9467abf2016-02-17 18:44:11 +00004682 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004683 break;
4684}
4685
drh4c583122008-01-04 22:01:03 +00004686/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004687** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004688**
drh4c583122008-01-04 22:01:03 +00004689** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004690** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004691** The sequence number on the cursor is incremented after this
4692** instruction.
drh4db38a72005-09-01 12:16:28 +00004693*/
drh27a348c2015-04-13 19:14:06 +00004694case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004695 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4696 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004697 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004698 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004699 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004700 break;
4701}
4702
4703
drh98757152008-01-09 23:04:12 +00004704/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004705** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004706**
drhf0863fe2005-06-12 21:35:51 +00004707** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004708** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004709** table that cursor P1 points to. The new record number is written
4710** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004711**
dan76d462e2009-08-30 11:42:51 +00004712** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4713** the largest previously generated record number. No new record numbers are
4714** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004715** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004716** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004717** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004718*/
drh27a348c2015-04-13 19:14:06 +00004719case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004720 i64 v; /* The new rowid */
4721 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4722 int res; /* Result of an sqlite3BtreeLast() */
4723 int cnt; /* Counter to limit the number of searches */
4724 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004725 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004726
drh856c1032009-06-02 15:21:42 +00004727 v = 0;
4728 res = 0;
drh27a348c2015-04-13 19:14:06 +00004729 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004730 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4731 pC = p->apCsr[pOp->p1];
4732 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004733 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004734 assert( pC->eCurType==CURTYPE_BTREE );
4735 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004736 {
drh5cf8e8c2002-02-19 22:42:05 +00004737 /* The next rowid or record number (different terms for the same
4738 ** thing) is obtained in a two-step algorithm.
4739 **
4740 ** First we attempt to find the largest existing rowid and add one
4741 ** to that. But if the largest existing rowid is already the maximum
4742 ** positive integer, we have to fall through to the second
4743 ** probabilistic algorithm
4744 **
4745 ** The second algorithm is to select a rowid at random and see if
4746 ** it already exists in the table. If it does not exist, we have
4747 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004748 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004749 */
drhaa736092009-06-22 00:55:30 +00004750 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004751
drh75f86a42005-02-17 00:03:06 +00004752#ifdef SQLITE_32BIT_ROWID
4753# define MAX_ROWID 0x7fffffff
4754#else
drhfe2093d2005-01-20 22:48:47 +00004755 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4756 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4757 ** to provide the constant while making all compilers happy.
4758 */
danielk197764202cf2008-11-17 15:31:47 +00004759# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004760#endif
drhfe2093d2005-01-20 22:48:47 +00004761
drh5cf8e8c2002-02-19 22:42:05 +00004762 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004763 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004764 if( rc!=SQLITE_OK ){
4765 goto abort_due_to_error;
4766 }
4767 if( res ){
4768 v = 1; /* IMP: R-61914-48074 */
4769 }else{
drhc960dcb2015-11-20 19:22:01 +00004770 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004771 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004772 if( v>=MAX_ROWID ){
4773 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004774 }else{
drhe0670b62014-02-12 21:31:12 +00004775 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004776 }
drh3fc190c2001-09-14 03:24:23 +00004777 }
drhe0670b62014-02-12 21:31:12 +00004778 }
drh205f48e2004-11-05 00:43:11 +00004779
4780#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004781 if( pOp->p3 ){
4782 /* Assert that P3 is a valid memory cell. */
4783 assert( pOp->p3>0 );
4784 if( p->pFrame ){
4785 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004786 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004787 assert( pOp->p3<=pFrame->nMem );
4788 pMem = &pFrame->aMem[pOp->p3];
4789 }else{
4790 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004791 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004792 pMem = &aMem[pOp->p3];
4793 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004794 }
drhe0670b62014-02-12 21:31:12 +00004795 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004796
drhe0670b62014-02-12 21:31:12 +00004797 REGISTER_TRACE(pOp->p3, pMem);
4798 sqlite3VdbeMemIntegerify(pMem);
4799 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4800 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004801 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004802 goto abort_due_to_error;
4803 }
4804 if( v<pMem->u.i+1 ){
4805 v = pMem->u.i + 1;
4806 }
4807 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004808 }
drhe0670b62014-02-12 21:31:12 +00004809#endif
drh5cf8e8c2002-02-19 22:42:05 +00004810 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004811 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004812 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004813 ** engine starts picking positive candidate ROWIDs at random until
4814 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004815 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4816 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004817 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004818 do{
4819 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004820 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004821 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004822 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004823 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004824 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004825 if( rc ) goto abort_due_to_error;
4826 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004827 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004828 goto abort_due_to_error;
4829 }
drh748a52c2010-09-01 11:50:08 +00004830 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004831 }
drha11846b2004-01-07 18:52:56 +00004832 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004833 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004834 }
drh4c583122008-01-04 22:01:03 +00004835 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004836 break;
4837}
4838
danielk19771f4aa332008-01-03 09:51:55 +00004839/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004840** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004841**
jplyon5a564222003-06-02 06:15:58 +00004842** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004843** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004844** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004845** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004846** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004847**
danielk19771f4aa332008-01-03 09:51:55 +00004848** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4849** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004850** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004851** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004852**
drheaf6ae22016-11-09 20:14:34 +00004853** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4854** run faster by avoiding an unnecessary seek on cursor P1. However,
4855** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4856** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004857**
4858** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4859** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4860** is part of an INSERT operation. The difference is only important to
4861** the update hook.
4862**
dan319eeb72011-03-19 08:38:50 +00004863** Parameter P4 may point to a Table structure, or may be NULL. If it is
4864** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4865** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004866**
drh93aed5a2008-01-16 17:46:38 +00004867** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4868** allocated, then ownership of P2 is transferred to the pseudo-cursor
4869** and register P2 becomes ephemeral. If the cursor is changed, the
4870** value of register P2 will then change. Make sure this does not
4871** cause any problems.)
4872**
drhf0863fe2005-06-12 21:35:51 +00004873** This instruction only works on tables. The equivalent instruction
4874** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004875*/
drh50ef6712019-02-22 23:29:56 +00004876case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00004877 Mem *pData; /* MEM cell holding data for the record to be inserted */
4878 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004879 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004880 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4881 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004882 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00004883 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004884
drha6c2ed92009-11-14 23:22:23 +00004885 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004886 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004887 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004888 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004889 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004890 assert( pC->eCurType==CURTYPE_BTREE );
drhbe3da242019-12-29 00:52:41 +00004891 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00004892 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004893 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004894 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004895 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00004896 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00004897
drh50ef6712019-02-22 23:29:56 +00004898 pKey = &aMem[pOp->p3];
4899 assert( pKey->flags & MEM_Int );
4900 assert( memIsValid(pKey) );
4901 REGISTER_TRACE(pOp->p3, pKey);
4902 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004903
drh9b1c62d2011-03-30 21:04:43 +00004904 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004905 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004906 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004907 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004908 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00004909 }else{
drh4ec6f3a2018-01-12 19:33:18 +00004910 pTab = 0;
drh74c33022016-03-30 12:56:55 +00004911 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004912 }
4913
drh9b1c62d2011-03-30 21:04:43 +00004914#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004915 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00004916 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00004917 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
4918 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
4919 }
drh4ec6f3a2018-01-12 19:33:18 +00004920 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
4921 /* Prevent post-update hook from running in cases when it should not */
4922 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00004923 }
dan46c47d42011-03-01 18:42:07 +00004924 }
dancb9a3642017-01-30 19:44:53 +00004925 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004926#endif
dan46c47d42011-03-01 18:42:07 +00004927
drha05a7222008-01-19 03:35:58 +00004928 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004929 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004930 assert( pData->flags & (MEM_Blob|MEM_Str) );
4931 x.pData = pData->z;
4932 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004933 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4934 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004935 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004936 }else{
drh8eeb4462016-05-21 20:03:42 +00004937 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004938 }
drh8eeb4462016-05-21 20:03:42 +00004939 x.pKey = 0;
4940 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004941 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004942 );
drha05a7222008-01-19 03:35:58 +00004943 pC->deferredMoveto = 0;
4944 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004945
drha05a7222008-01-19 03:35:58 +00004946 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004947 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00004948 if( pTab ){
4949 assert( db->xUpdateCallback!=0 );
4950 assert( pTab->aCol!=0 );
4951 db->xUpdateCallback(db->pUpdateArg,
4952 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
4953 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004954 }
drh5e00f6c2001-09-13 13:46:56 +00004955 break;
4956}
4957
dan438b8812015-09-15 15:55:15 +00004958/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004959**
drh5edc3122001-09-13 21:53:09 +00004960** Delete the record at which the P1 cursor is currently pointing.
4961**
drhe807bdb2016-01-21 17:06:33 +00004962** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4963** the cursor will be left pointing at either the next or the previous
4964** record in the table. If it is left pointing at the next record, then
4965** the next Next instruction will be a no-op. As a result, in this case
4966** it is ok to delete a record from within a Next loop. If
4967** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4968** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004969**
drhdef19e32016-01-27 16:26:25 +00004970** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4971** delete one of several associated with deleting a table row and all its
4972** associated index entries. Exactly one of those deletes is the "primary"
4973** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4974** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004975**
4976** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4977** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004978**
drh91fd4d42008-01-19 20:11:25 +00004979** P1 must not be pseudo-table. It has to be a real table with
4980** multiple rows.
4981**
drh5e769a52016-09-28 16:05:53 +00004982** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004983** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4984** have been positioned using OP_NotFound prior to invoking this opcode in
4985** this case. Specifically, if one is configured, the pre-update hook is
4986** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4987** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004988**
4989** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4990** of the memory cell that contains the value that the rowid of the row will
4991** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004992*/
drh9cbf3422008-01-17 16:22:13 +00004993case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004994 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004995 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004996 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004997 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004998
dan46c47d42011-03-01 18:42:07 +00004999 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00005000 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5001 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005002 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005003 assert( pC->eCurType==CURTYPE_BTREE );
5004 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00005005 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00005006 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00005007
drhb53a5a92014-10-12 22:37:22 +00005008#ifdef SQLITE_DEBUG
drh6b559f32020-01-02 19:50:50 +00005009 if( pOp->p4type==P4_TABLE
5010 && HasRowid(pOp->p4.pTab)
5011 && pOp->p5==0
5012 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
5013 ){
dan438b8812015-09-15 15:55:15 +00005014 /* If p5 is zero, the seek operation that positioned the cursor prior to
5015 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5016 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00005017 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan0971ef42019-05-16 20:13:32 +00005018 assert( CORRUPT_DB || pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00005019 }
5020#endif
drh91fd4d42008-01-19 20:11:25 +00005021
dan438b8812015-09-15 15:55:15 +00005022 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5023 ** the name of the db to pass as to it. Also set local pTab to a copy
5024 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5025 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5026 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00005027 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005028 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00005029 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00005030 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005031 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00005032 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00005033 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00005034 }
drh74c33022016-03-30 12:56:55 +00005035 }else{
5036 zDb = 0; /* Not needed. Silence a compiler warning. */
5037 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00005038 }
dan46c47d42011-03-01 18:42:07 +00005039
drh9b1c62d2011-03-30 21:04:43 +00005040#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005041 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00005042 if( db->xPreUpdateCallback && pOp->p4.pTab ){
5043 assert( !(opflags & OPFLAG_ISUPDATE)
5044 || HasRowid(pTab)==0
5045 || (aMem[pOp->p3].flags & MEM_Int)
5046 );
dan46c47d42011-03-01 18:42:07 +00005047 sqlite3VdbePreUpdateHook(p, pC,
5048 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00005049 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00005050 pOp->p3
dan46c47d42011-03-01 18:42:07 +00005051 );
5052 }
dan46c47d42011-03-01 18:42:07 +00005053 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00005054#endif
drhb53a5a92014-10-12 22:37:22 +00005055
drhdef19e32016-01-27 16:26:25 +00005056 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5057 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00005058 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00005059 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00005060
5061#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00005062 if( p->pFrame==0 ){
5063 if( pC->isEphemeral==0
5064 && (pOp->p5 & OPFLAG_AUXDELETE)==0
5065 && (pC->wrFlag & OPFLAG_FORDELETE)==0
5066 ){
5067 nExtraDelete++;
5068 }
5069 if( pOp->p2 & OPFLAG_NCHANGE ){
5070 nExtraDelete--;
5071 }
drhb89aeb62016-01-27 15:49:32 +00005072 }
5073#endif
5074
drhc960dcb2015-11-20 19:22:01 +00005075 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00005076 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005077 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00005078 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00005079
drh91fd4d42008-01-19 20:11:25 +00005080 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00005081 if( opflags & OPFLAG_NCHANGE ){
5082 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00005083 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00005084 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00005085 pC->movetoTarget);
5086 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00005087 }
drh5e00f6c2001-09-13 13:46:56 +00005088 }
dan438b8812015-09-15 15:55:15 +00005089
rdcb0c374f2004-02-20 22:53:38 +00005090 break;
5091}
drhb7f1d9a2009-09-08 02:27:58 +00005092/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00005093**
drhb7f1d9a2009-09-08 02:27:58 +00005094** The value of the change counter is copied to the database handle
5095** change counter (returned by subsequent calls to sqlite3_changes()).
5096** Then the VMs internal change counter resets to 0.
5097** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00005098*/
drh9cbf3422008-01-17 16:22:13 +00005099case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00005100 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00005101 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00005102 break;
5103}
5104
drh1153c7b2013-11-01 22:02:56 +00005105/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00005106** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00005107**
drh1153c7b2013-11-01 22:02:56 +00005108** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00005109** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00005110** the sorter cursor currently points to. Only the first P4 fields
5111** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00005112**
5113** If either P3 or the sorter contains a NULL in one of their significant
5114** fields (not counting the P4 fields at the end which are ignored) then
5115** the comparison is assumed to be equal.
5116**
5117** Fall through to next instruction if the two records compare equal to
5118** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00005119*/
5120case OP_SorterCompare: {
5121 VdbeCursor *pC;
5122 int res;
drhac502322014-07-30 13:56:48 +00005123 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00005124
5125 pC = p->apCsr[pOp->p1];
5126 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00005127 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00005128 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00005129 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00005130 res = 0;
drhac502322014-07-30 13:56:48 +00005131 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00005132 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00005133 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005134 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00005135 break;
5136};
5137
drh6cf4a7d2014-10-13 13:00:58 +00005138/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005139** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00005140**
5141** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00005142** Then clear the column header cache on cursor P3.
5143**
5144** This opcode is normally use to move a record out of the sorter and into
5145** a register that is the source for a pseudo-table cursor created using
5146** OpenPseudo. That pseudo-table cursor is the one that is identified by
5147** parameter P3. Clearing the P3 column cache as part of this opcode saves
5148** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00005149*/
5150case OP_SorterData: {
5151 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00005152
dan5134d132011-09-02 10:31:11 +00005153 pOut = &aMem[pOp->p2];
5154 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00005155 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00005156 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00005157 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00005158 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00005159 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00005160 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00005161 break;
5162}
5163
drhe7b554d2017-01-09 15:44:25 +00005164/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005165** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00005166**
drh9057fc72016-11-25 19:32:32 +00005167** Write into register P2 the complete row content for the row at
5168** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00005169** There is no interpretation of the data.
5170** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00005171** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00005172**
drh9057fc72016-11-25 19:32:32 +00005173** If cursor P1 is an index, then the content is the key of the row.
5174** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00005175**
drhde4fcfd2008-01-19 23:50:26 +00005176** If the P1 cursor must be pointing to a valid row (not a NULL row)
5177** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00005178**
drh8cdafc32018-05-31 19:00:20 +00005179** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00005180** into the database page. That means that the content of the output
5181** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00005182** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00005183** position in order that they can write to the same table. If P3==0
5184** then a copy of the data is made into memory. P3!=0 is faster, but
5185** P3==0 is safer.
5186**
5187** If P3!=0 then the content of the P2 register is unsuitable for use
5188** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00005189** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00005190** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00005191*/
danielk1977a7a8e142008-02-13 18:25:27 +00005192case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00005193 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00005194 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00005195 u32 n;
drh70ce3f02003-04-15 19:22:22 +00005196
drhe7b554d2017-01-09 15:44:25 +00005197 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00005198
drh653b82a2009-06-22 11:10:47 +00005199 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5200 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00005201 assert( pC!=0 );
5202 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00005203 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00005204 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00005205 assert( pC->uc.pCursor!=0 );
5206 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00005207
drh9057fc72016-11-25 19:32:32 +00005208 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00005209 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5210 ** that might invalidate the cursor.
5211 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00005212 ** would fail. Should this ever change (because of changes in the code
5213 ** generator) then the fix would be to insert a call to
5214 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00005215 */
5216 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00005217 assert( sqlite3BtreeCursorIsValid(pCrsr) );
5218#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00005219 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00005220 if( rc!=SQLITE_OK ) goto abort_due_to_error;
5221#endif
drh9a65f2c2009-06-22 19:05:40 +00005222
drha7c90c42016-06-04 20:37:10 +00005223 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00005224 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00005225 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00005226 }
drh722246e2014-10-07 23:02:24 +00005227 testcase( n==0 );
drh2a740062020-02-05 18:28:17 +00005228 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
drh9467abf2016-02-17 18:44:11 +00005229 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00005230 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00005231 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00005232 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00005233 break;
5234}
5235
drh2133d822008-01-03 18:44:59 +00005236/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005237** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005238**
drh2133d822008-01-03 18:44:59 +00005239** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00005240** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00005241**
5242** P1 can be either an ordinary table or a virtual table. There used to
5243** be a separate OP_VRowid opcode for use with virtual tables, but this
5244** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00005245*/
drh27a348c2015-04-13 19:14:06 +00005246case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00005247 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00005248 i64 v;
drh856c1032009-06-02 15:21:42 +00005249 sqlite3_vtab *pVtab;
5250 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00005251
drh27a348c2015-04-13 19:14:06 +00005252 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005253 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5254 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005255 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005256 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00005257 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00005258 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00005259 break;
5260 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00005261 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00005262#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00005263 }else if( pC->eCurType==CURTYPE_VTAB ){
5264 assert( pC->uc.pVCur!=0 );
5265 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00005266 pModule = pVtab->pModule;
5267 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00005268 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00005269 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00005270 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00005271#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00005272 }else{
drhc960dcb2015-11-20 19:22:01 +00005273 assert( pC->eCurType==CURTYPE_BTREE );
5274 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00005275 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00005276 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00005277 if( pC->nullRow ){
5278 pOut->flags = MEM_Null;
5279 break;
5280 }
drha7c90c42016-06-04 20:37:10 +00005281 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00005282 }
drh4c583122008-01-04 22:01:03 +00005283 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005284 break;
5285}
5286
drh9cbf3422008-01-17 16:22:13 +00005287/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005288**
5289** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005290** that occur while the cursor is on the null row will always
5291** write a NULL.
drh17f71932002-02-21 12:01:27 +00005292*/
drh9cbf3422008-01-17 16:22:13 +00005293case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005294 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005295
drh653b82a2009-06-22 11:10:47 +00005296 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5297 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005298 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00005299 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005300 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005301 if( pC->eCurType==CURTYPE_BTREE ){
5302 assert( pC->uc.pCursor!=0 );
5303 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005304 }
drhcf025a82018-06-07 18:01:21 +00005305#ifdef SQLITE_DEBUG
5306 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5307#endif
drh17f71932002-02-21 12:01:27 +00005308 break;
5309}
5310
drh86b40df2017-08-01 19:53:43 +00005311/* Opcode: SeekEnd P1 * * * *
5312**
5313** Position cursor P1 at the end of the btree for the purpose of
5314** appending a new entry onto the btree.
5315**
5316** It is assumed that the cursor is used only for appending and so
5317** if the cursor is valid, then the cursor must already be pointing
5318** at the end of the btree and so no changes are made to
5319** the cursor.
5320*/
5321/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005322**
drh8af3f772014-07-25 18:01:06 +00005323** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005324** will refer to the last entry in the database table or index.
5325** If the table or index is empty and P2>0, then jump immediately to P2.
5326** If P2 is 0 or if the table or index is not empty, fall through
5327** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005328**
5329** This opcode leaves the cursor configured to move in reverse order,
5330** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005331** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005332*/
drh86b40df2017-08-01 19:53:43 +00005333case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005334case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005335 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005336 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005337 int res;
drh9562b552002-02-19 15:00:07 +00005338
drh653b82a2009-06-22 11:10:47 +00005339 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5340 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005341 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005342 assert( pC->eCurType==CURTYPE_BTREE );
5343 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005344 res = 0;
drh3da046d2013-11-11 03:24:11 +00005345 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005346#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005347 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005348#endif
drh86b40df2017-08-01 19:53:43 +00005349 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005350 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005351 pC->seekResult = -1;
5352 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5353 break;
5354 }
5355 }
5356 rc = sqlite3BtreeLast(pCrsr, &res);
5357 pC->nullRow = (u8)res;
5358 pC->deferredMoveto = 0;
5359 pC->cacheStatus = CACHE_STALE;
5360 if( rc ) goto abort_due_to_error;
5361 if( pOp->p2>0 ){
5362 VdbeBranchTaken(res!=0,2);
5363 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005364 }
5365 break;
5366}
5367
drh5e98e832017-02-17 19:24:06 +00005368/* Opcode: IfSmaller P1 P2 P3 * *
5369**
5370** Estimate the number of rows in the table P1. Jump to P2 if that
5371** estimate is less than approximately 2**(0.1*P3).
5372*/
5373case OP_IfSmaller: { /* jump */
5374 VdbeCursor *pC;
5375 BtCursor *pCrsr;
5376 int res;
5377 i64 sz;
5378
5379 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5380 pC = p->apCsr[pOp->p1];
5381 assert( pC!=0 );
5382 pCrsr = pC->uc.pCursor;
5383 assert( pCrsr );
5384 rc = sqlite3BtreeFirst(pCrsr, &res);
5385 if( rc ) goto abort_due_to_error;
5386 if( res==0 ){
5387 sz = sqlite3BtreeRowCountEst(pCrsr);
5388 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5389 }
5390 VdbeBranchTaken(res!=0,2);
5391 if( res ) goto jump_to_p2;
5392 break;
5393}
5394
drh0342b1f2005-09-01 03:07:44 +00005395
drh6bd4dc62016-12-23 16:05:22 +00005396/* Opcode: SorterSort P1 P2 * * *
5397**
5398** After all records have been inserted into the Sorter object
5399** identified by P1, invoke this opcode to actually do the sorting.
5400** Jump to P2 if there are no records to be sorted.
5401**
5402** This opcode is an alias for OP_Sort and OP_Rewind that is used
5403** for Sorter objects.
5404*/
drh9cbf3422008-01-17 16:22:13 +00005405/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005406**
5407** This opcode does exactly the same thing as OP_Rewind except that
5408** it increments an undocumented global variable used for testing.
5409**
5410** Sorting is accomplished by writing records into a sorting index,
5411** then rewinding that index and playing it back from beginning to
5412** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5413** rewinding so that the global variable will be incremented and
5414** regression tests can determine whether or not the optimizer is
5415** correctly optimizing out sorts.
5416*/
drhc6aff302011-09-01 15:32:47 +00005417case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005418case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005419#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005420 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005421 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005422#endif
drh9b47ee32013-08-20 03:13:51 +00005423 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005424 /* Fall through into OP_Rewind */
5425}
drh038ebf62019-03-29 15:21:22 +00005426/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005427**
drhf0863fe2005-06-12 21:35:51 +00005428** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005429** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005430** If the table or index is empty, jump immediately to P2.
5431** If the table or index is not empty, fall through to the following
5432** instruction.
drh8af3f772014-07-25 18:01:06 +00005433**
5434** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005435** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005436** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005437*/
drh9cbf3422008-01-17 16:22:13 +00005438case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005439 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005440 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005441 int res;
drh5e00f6c2001-09-13 13:46:56 +00005442
drh653b82a2009-06-22 11:10:47 +00005443 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00005444 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00005445 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005446 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005447 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005448 res = 1;
drh8af3f772014-07-25 18:01:06 +00005449#ifdef SQLITE_DEBUG
5450 pC->seekOp = OP_Rewind;
5451#endif
dan689ab892011-08-12 15:02:00 +00005452 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005453 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005454 }else{
drhc960dcb2015-11-20 19:22:01 +00005455 assert( pC->eCurType==CURTYPE_BTREE );
5456 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005457 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005458 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00005459 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005460 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005461 }
drh9467abf2016-02-17 18:44:11 +00005462 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005463 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005464 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005465 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005466 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005467 break;
5468}
5469
drh0fd61352014-02-07 02:29:45 +00005470/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005471**
5472** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005473** table or index. If there are no more key/value pairs then fall through
5474** to the following instruction. But if the cursor advance was successful,
5475** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005476**
drh5dad9a32014-07-25 18:37:42 +00005477** The Next opcode is only valid following an SeekGT, SeekGE, or
5478** OP_Rewind opcode used to position the cursor. Next is not allowed
5479** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005480**
drhf93cd942013-11-21 03:12:25 +00005481** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5482** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005483**
drhe39a7322014-02-03 14:04:11 +00005484** The P3 value is a hint to the btree implementation. If P3==1, that
5485** means P1 is an SQL index and that this instruction could have been
5486** omitted if that index had been unique. P3 is usually 0. P3 is
5487** always either 0 or 1.
5488**
dana205a482011-08-27 18:48:57 +00005489** P4 is always of type P4_ADVANCE. The function pointer points to
5490** sqlite3BtreeNext().
5491**
drhafc266a2010-03-31 17:47:44 +00005492** If P5 is positive and the jump is taken, then event counter
5493** number P5-1 in the prepared statement is incremented.
5494**
drhf1949b62018-06-07 17:32:59 +00005495** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005496*/
drh0fd61352014-02-07 02:29:45 +00005497/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005498**
5499** Back up cursor P1 so that it points to the previous key/data pair in its
5500** table or index. If there is no previous key/value pairs then fall through
5501** to the following instruction. But if the cursor backup was successful,
5502** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005503**
drh8af3f772014-07-25 18:01:06 +00005504**
drh5dad9a32014-07-25 18:37:42 +00005505** The Prev opcode is only valid following an SeekLT, SeekLE, or
5506** OP_Last opcode used to position the cursor. Prev is not allowed
5507** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005508**
drhf93cd942013-11-21 03:12:25 +00005509** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5510** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005511**
drhe39a7322014-02-03 14:04:11 +00005512** The P3 value is a hint to the btree implementation. If P3==1, that
5513** means P1 is an SQL index and that this instruction could have been
5514** omitted if that index had been unique. P3 is usually 0. P3 is
5515** always either 0 or 1.
5516**
dana205a482011-08-27 18:48:57 +00005517** P4 is always of type P4_ADVANCE. The function pointer points to
5518** sqlite3BtreePrevious().
5519**
drhafc266a2010-03-31 17:47:44 +00005520** If P5 is positive and the jump is taken, then event counter
5521** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005522*/
drh6bd4dc62016-12-23 16:05:22 +00005523/* Opcode: SorterNext P1 P2 * * P5
5524**
5525** This opcode works just like OP_Next except that P1 must be a
5526** sorter object for which the OP_SorterSort opcode has been
5527** invoked. This opcode advances the cursor to the next sorted
5528** record, or jumps to P2 if there are no more sorted records.
5529*/
drhf93cd942013-11-21 03:12:25 +00005530case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005531 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005532
drhf93cd942013-11-21 03:12:25 +00005533 pC = p->apCsr[pOp->p1];
5534 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005535 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005536 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005537case OP_Prev: /* jump */
5538case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005539 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005540 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005541 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005542 assert( pC!=0 );
5543 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005544 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005545 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5546 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005547
drhcf025a82018-06-07 18:01:21 +00005548 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005549 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005550 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005551 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drh790b37a2019-08-27 17:01:07 +00005552 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
5553 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
5554 || pC->seekOp==OP_IfNoHope);
drhf1949b62018-06-07 17:32:59 +00005555 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005556 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drh790b37a2019-08-27 17:01:07 +00005557 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
drhcf025a82018-06-07 18:01:21 +00005558 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005559
drh2ab792e2017-05-30 18:34:07 +00005560 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005561next_tail:
drha3460582008-07-11 21:02:53 +00005562 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005563 VdbeBranchTaken(rc==SQLITE_OK,2);
5564 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005565 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005566 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005567#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005568 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005569#endif
drhf56fa462015-04-13 21:39:54 +00005570 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005571 }
drh2ab792e2017-05-30 18:34:07 +00005572 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5573 rc = SQLITE_OK;
5574 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005575 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005576}
5577
drh9b4eaeb2016-11-09 00:10:33 +00005578/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005579** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005580**
drhef8662b2011-06-20 21:47:58 +00005581** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005582** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005583** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005584**
drhfb8c56f2016-11-09 01:19:25 +00005585** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005586** key of reg(P2). In that case, P3 is the index of the first register
5587** for the unpacked key. The availability of the unpacked key can sometimes
5588** be an optimization.
5589**
5590** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5591** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005592**
mistachkin21a919f2014-02-07 03:28:02 +00005593** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5594** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5595** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005596**
drheaf6ae22016-11-09 20:14:34 +00005597** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5598** run faster by avoiding an unnecessary seek on cursor P1. However,
5599** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5600** seeks on the cursor or if the most recent seek used a key equivalent
5601** to P2.
drh0fd61352014-02-07 02:29:45 +00005602**
drhf0863fe2005-06-12 21:35:51 +00005603** This instruction only works for indices. The equivalent instruction
5604** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005605*/
drh9cbf3422008-01-17 16:22:13 +00005606case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005607 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005608 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005609
drh653b82a2009-06-22 11:10:47 +00005610 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5611 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005612 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005613 assert( pC!=0 );
drhc879c4e2020-02-06 13:57:08 +00005614 assert( !isSorter(pC) );
drh3c657212009-11-17 23:59:58 +00005615 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005616 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005617 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc879c4e2020-02-06 13:57:08 +00005618 assert( pC->eCurType==CURTYPE_BTREE );
drh3da046d2013-11-11 03:24:11 +00005619 assert( pC->isTable==0 );
5620 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005621 if( rc ) goto abort_due_to_error;
drhc879c4e2020-02-06 13:57:08 +00005622 x.nKey = pIn2->n;
5623 x.pKey = pIn2->z;
5624 x.aMem = aMem + pOp->p3;
5625 x.nMem = (u16)pOp->p4.i;
5626 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
5627 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
5628 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5629 );
5630 assert( pC->deferredMoveto==0 );
5631 pC->cacheStatus = CACHE_STALE;
5632 if( rc) goto abort_due_to_error;
5633 break;
5634}
5635
5636/* Opcode: SorterInsert P1 P2 * * *
5637** Synopsis: key=r[P2]
5638**
5639** Register P2 holds an SQL index key made using the
5640** MakeRecord instructions. This opcode writes that key
5641** into the sorter P1. Data for the entry is nil.
5642*/
5643case OP_SorterInsert: { /* in2 */
5644 VdbeCursor *pC;
5645
5646 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5647 pC = p->apCsr[pOp->p1];
5648 sqlite3VdbeIncrWriteCounter(p, pC);
5649 assert( pC!=0 );
5650 assert( isSorter(pC) );
5651 pIn2 = &aMem[pOp->p2];
5652 assert( pIn2->flags & MEM_Blob );
5653 assert( pC->isTable==0 );
5654 rc = ExpandBlob(pIn2);
5655 if( rc ) goto abort_due_to_error;
5656 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh9467abf2016-02-17 18:44:11 +00005657 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005658 break;
5659}
5660
drhd1d38482008-10-07 23:46:38 +00005661/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005662** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005663**
drhe14006d2008-03-25 17:23:32 +00005664** The content of P3 registers starting at register P2 form
5665** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005666** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005667*/
drhe14006d2008-03-25 17:23:32 +00005668case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005669 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005670 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005671 int res;
5672 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005673
drhe14006d2008-03-25 17:23:32 +00005674 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005675 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005676 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5677 pC = p->apCsr[pOp->p1];
5678 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005679 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005680 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005681 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005682 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005683 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005684 r.pKeyInfo = pC->pKeyInfo;
5685 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005686 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005687 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005688 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005689 if( rc ) goto abort_due_to_error;
5690 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005691 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005692 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005693 }
drh3da046d2013-11-11 03:24:11 +00005694 assert( pC->deferredMoveto==0 );
5695 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005696 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005697 break;
5698}
5699
drh170ad682017-06-02 15:44:22 +00005700/* Opcode: DeferredSeek P1 * P3 P4 *
5701** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005702**
5703** P1 is an open index cursor and P3 is a cursor on the corresponding
5704** table. This opcode does a deferred seek of the P3 table cursor
5705** to the row that corresponds to the current row of P1.
5706**
5707** This is a deferred seek. Nothing actually happens until
5708** the cursor is used to read a record. That way, if no reads
5709** occur, no unnecessary I/O happens.
5710**
5711** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005712** one entry for each column in the P3 table. If array entry a(i)
5713** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005714** equivalent to performing the deferred seek and then reading column i
5715** from P1. This information is stored in P3 and used to redirect
5716** reads against P3 over to P1, thus possibly avoiding the need to
5717** seek and read cursor P3.
5718*/
drh2133d822008-01-03 18:44:59 +00005719/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005720** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005721**
drh2133d822008-01-03 18:44:59 +00005722** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005723** the end of the index key pointed to by cursor P1. This integer should be
5724** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005725**
drh9437bd22009-02-01 00:29:56 +00005726** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005727*/
drh170ad682017-06-02 15:44:22 +00005728case OP_DeferredSeek:
5729case OP_IdxRowid: { /* out2 */
5730 VdbeCursor *pC; /* The P1 index cursor */
5731 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5732 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005733
drh653b82a2009-06-22 11:10:47 +00005734 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5735 pC = p->apCsr[pOp->p1];
5736 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005737 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005738 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005739 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005740 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005741 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5742
5743 /* The IdxRowid and Seek opcodes are combined because of the commonality
5744 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5745 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005746
5747 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005748 ** out from under the cursor. That will never happens for an IdxRowid
5749 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005750 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5751
drh3da046d2013-11-11 03:24:11 +00005752 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005753 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005754 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005755 if( rc!=SQLITE_OK ){
5756 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005757 }
drh170ad682017-06-02 15:44:22 +00005758 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005759 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5760 pTabCur = p->apCsr[pOp->p3];
5761 assert( pTabCur!=0 );
5762 assert( pTabCur->eCurType==CURTYPE_BTREE );
5763 assert( pTabCur->uc.pCursor!=0 );
5764 assert( pTabCur->isTable );
5765 pTabCur->nullRow = 0;
5766 pTabCur->movetoTarget = rowid;
5767 pTabCur->deferredMoveto = 1;
5768 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5769 pTabCur->aAltMap = pOp->p4.ai;
5770 pTabCur->pAltCursor = pC;
5771 }else{
5772 pOut = out2Prerelease(p, pOp);
5773 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005774 }
5775 }else{
5776 assert( pOp->opcode==OP_IdxRowid );
5777 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005778 }
5779 break;
5780}
5781
drhbe3da242019-12-29 00:52:41 +00005782/* Opcode: FinishSeek P1 * * * *
5783**
5784** If cursor P1 was previously moved via OP_DeferredSeek, complete that
5785** seek operation now, without further delay. If the cursor seek has
5786** already occurred, this instruction is a no-op.
5787*/
5788case OP_FinishSeek: {
5789 VdbeCursor *pC; /* The P1 index cursor */
5790
5791 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5792 pC = p->apCsr[pOp->p1];
5793 if( pC->deferredMoveto ){
5794 rc = sqlite3VdbeFinishMoveto(pC);
5795 if( rc ) goto abort_due_to_error;
5796 }
5797 break;
5798}
5799
danielk197761dd5832008-04-18 11:31:12 +00005800/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005801** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005802**
danielk197761dd5832008-04-18 11:31:12 +00005803** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005804** key that omits the PRIMARY KEY. Compare this key value against the index
5805** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5806** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005807**
danielk197761dd5832008-04-18 11:31:12 +00005808** If the P1 index entry is greater than or equal to the key value
5809** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005810*/
5811/* Opcode: IdxGT P1 P2 P3 P4 P5
5812** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005813**
drh4a1d3652014-02-14 15:13:36 +00005814** The P4 register values beginning with P3 form an unpacked index
5815** key that omits the PRIMARY KEY. Compare this key value against the index
5816** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5817** fields at the end.
5818**
5819** If the P1 index entry is greater than the key value
5820** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005821*/
drh3bb9b932010-08-06 02:10:00 +00005822/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005823** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005824**
danielk197761dd5832008-04-18 11:31:12 +00005825** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005826** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5827** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5828** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005829**
danielk197761dd5832008-04-18 11:31:12 +00005830** If the P1 index entry is less than the key value then jump to P2.
5831** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005832*/
drh4a1d3652014-02-14 15:13:36 +00005833/* Opcode: IdxLE P1 P2 P3 P4 P5
5834** Synopsis: key=r[P3@P4]
5835**
5836** The P4 register values beginning with P3 form an unpacked index
5837** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5838** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5839** ROWID on the P1 index.
5840**
5841** If the P1 index entry is less than or equal to the key value then jump
5842** to P2. Otherwise fall through to the next instruction.
5843*/
5844case OP_IdxLE: /* jump */
5845case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005846case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005847case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005848 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005849 int res;
5850 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005851
drh653b82a2009-06-22 11:10:47 +00005852 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5853 pC = p->apCsr[pOp->p1];
5854 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005855 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005856 assert( pC->eCurType==CURTYPE_BTREE );
5857 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005858 assert( pC->deferredMoveto==0 );
5859 assert( pOp->p5==0 || pOp->p5==1 );
5860 assert( pOp->p4type==P4_INT32 );
5861 r.pKeyInfo = pC->pKeyInfo;
5862 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005863 if( pOp->opcode<OP_IdxLT ){
5864 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005865 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005866 }else{
drh4a1d3652014-02-14 15:13:36 +00005867 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005868 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005869 }
5870 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005871#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00005872 {
5873 int i;
5874 for(i=0; i<r.nField; i++){
5875 assert( memIsValid(&r.aMem[i]) );
5876 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
5877 }
5878 }
drh2b4ded92010-09-27 21:09:31 +00005879#endif
drh2dc06482013-12-11 00:59:10 +00005880 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005881 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005882 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5883 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5884 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005885 res = -res;
5886 }else{
drh4a1d3652014-02-14 15:13:36 +00005887 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005888 res++;
5889 }
drh688852a2014-02-17 22:40:43 +00005890 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005891 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005892 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005893 break;
5894}
5895
drh98757152008-01-09 23:04:12 +00005896/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005897**
5898** Delete an entire database table or index whose root page in the database
5899** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005900**
drh98757152008-01-09 23:04:12 +00005901** The table being destroyed is in the main database file if P3==0. If
5902** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005903** that is used to store tables create using CREATE TEMPORARY TABLE.
5904**
drh205f48e2004-11-05 00:43:11 +00005905** If AUTOVACUUM is enabled then it is possible that another root page
5906** might be moved into the newly deleted root page in order to keep all
5907** root pages contiguous at the beginning of the database. The former
5908** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005909** is stored in register P2. If no page movement was required (because the
5910** table being dropped was already the last one in the database) then a
5911** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5912** is stored in register P2.
5913**
5914** This opcode throws an error if there are any active reader VMs when
5915** it is invoked. This is done to avoid the difficulty associated with
5916** updating existing cursors when a root page is moved in an AUTOVACUUM
5917** database. This error is thrown even if the database is not an AUTOVACUUM
5918** db in order to avoid introducing an incompatibility between autovacuum
5919** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005920**
drhb19a2bc2001-09-16 00:13:26 +00005921** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005922*/
drh27a348c2015-04-13 19:14:06 +00005923case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005924 int iMoved;
drh856c1032009-06-02 15:21:42 +00005925 int iDb;
drh3a949872012-09-18 13:20:13 +00005926
drh4031baf2018-05-28 17:31:20 +00005927 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005928 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005929 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005930 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005931 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005932 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005933 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005934 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005935 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005936 }else{
drh856c1032009-06-02 15:21:42 +00005937 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005938 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005939 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005940 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005941 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005942 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005943 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005944#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005945 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005946 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5947 /* All OP_Destroy operations occur on the same btree */
5948 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5949 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005950 }
drh3765df42006-06-28 18:18:09 +00005951#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005952 }
drh5e00f6c2001-09-13 13:46:56 +00005953 break;
5954}
5955
danielk1977c7af4842008-10-27 13:59:33 +00005956/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005957**
5958** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005959** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005960** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005961**
drhf57b3392001-10-08 13:22:32 +00005962** The table being clear is in the main database file if P2==0. If
5963** P2==1 then the table to be clear is in the auxiliary database file
5964** that is used to store tables create using CREATE TEMPORARY TABLE.
5965**
shanebe217792009-03-05 04:20:31 +00005966** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005967** intkey table (an SQL table, not an index). In this case the row change
5968** count is incremented by the number of rows in the table being cleared.
5969** If P3 is greater than zero, then the value stored in register P3 is
5970** also incremented by the number of rows in the table being cleared.
5971**
drhb19a2bc2001-09-16 00:13:26 +00005972** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005973*/
drh9cbf3422008-01-17 16:22:13 +00005974case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005975 int nChange;
5976
drh4031baf2018-05-28 17:31:20 +00005977 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005978 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005979 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005980 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005981 rc = sqlite3BtreeClearTable(
5982 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5983 );
5984 if( pOp->p3 ){
5985 p->nChange += nChange;
5986 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005987 assert( memIsValid(&aMem[pOp->p3]) );
5988 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005989 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005990 }
5991 }
drh9467abf2016-02-17 18:44:11 +00005992 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005993 break;
5994}
5995
drh65ea12c2014-03-19 17:41:36 +00005996/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005997**
drh65ea12c2014-03-19 17:41:36 +00005998** Delete all contents from the ephemeral table or sorter
5999** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00006000**
drh65ea12c2014-03-19 17:41:36 +00006001** This opcode only works for cursors used for sorting and
6002** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00006003*/
drh65ea12c2014-03-19 17:41:36 +00006004case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00006005 VdbeCursor *pC;
6006
6007 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6008 pC = p->apCsr[pOp->p1];
6009 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006010 if( isSorter(pC) ){
6011 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00006012 }else{
drhc960dcb2015-11-20 19:22:01 +00006013 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00006014 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00006015 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00006016 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00006017 }
drh079a3072014-03-19 14:10:55 +00006018 break;
6019}
6020
drh0f3f7662017-08-18 14:34:28 +00006021/* Opcode: CreateBtree P1 P2 P3 * *
6022** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00006023**
drh0f3f7662017-08-18 14:34:28 +00006024** Allocate a new b-tree in the main database file if P1==0 or in the
6025** TEMP database file if P1==1 or in an attached database if
6026** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00006027** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00006028** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00006029*/
drh0f3f7662017-08-18 14:34:28 +00006030case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00006031 int pgno;
drh234c39d2004-07-24 03:30:47 +00006032 Db *pDb;
drh856c1032009-06-02 15:21:42 +00006033
drh4031baf2018-05-28 17:31:20 +00006034 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00006035 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00006036 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00006037 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00006038 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006039 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006040 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00006041 pDb = &db->aDb[pOp->p1];
6042 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00006043 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00006044 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00006045 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00006046 break;
6047}
6048
drh4a54bb52017-02-18 15:58:52 +00006049/* Opcode: SqlExec * * * P4 *
6050**
6051** Run the SQL statement or statements specified in the P4 string.
6052*/
6053case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00006054 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00006055 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00006056 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00006057 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00006058 if( rc ) goto abort_due_to_error;
6059 break;
6060}
6061
drh22645842011-03-24 01:34:03 +00006062/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00006063**
6064** Read and parse all entries from the SQLITE_MASTER table of database P1
drh1595abc2018-08-14 19:27:51 +00006065** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6066** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00006067**
6068** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00006069** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00006070*/
drh9cbf3422008-01-17 16:22:13 +00006071case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00006072 int iDb;
6073 const char *zMaster;
6074 char *zSql;
6075 InitData initData;
6076
drhbdaec522011-04-04 00:14:43 +00006077 /* Any prepared statement that invokes this opcode will hold mutexes
6078 ** on every btree. This is a prerequisite for invoking
6079 ** sqlite3InitCallback().
6080 */
6081#ifdef SQLITE_DEBUG
6082 for(iDb=0; iDb<db->nDb; iDb++){
6083 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
6084 }
6085#endif
drhbdaec522011-04-04 00:14:43 +00006086
drh856c1032009-06-02 15:21:42 +00006087 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00006088 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00006089 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
dane325ffe2018-08-11 13:40:20 +00006090
6091#ifndef SQLITE_OMIT_ALTERTABLE
6092 if( pOp->p4.z==0 ){
6093 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00006094 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
drh1595abc2018-08-14 19:27:51 +00006095 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
dane325ffe2018-08-11 13:40:20 +00006096 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00006097 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00006098 }else
6099#endif
drh1595abc2018-08-14 19:27:51 +00006100 {
drhe0a04a32016-12-16 01:00:21 +00006101 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00006102 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00006103 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00006104 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00006105 initData.mInitFlags = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006106 zSql = sqlite3MPrintf(db,
drhc5a93d42019-08-12 00:08:07 +00006107 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00006108 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00006109 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00006110 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00006111 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00006112 assert( db->init.busy==0 );
6113 db->init.busy = 1;
6114 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00006115 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006116 assert( !db->mallocFailed );
6117 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
6118 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00006119 if( rc==SQLITE_OK && initData.nInitRow==0 ){
6120 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6121 ** at least one SQL statement. Any less than that indicates that
6122 ** the sqlite_master table is corrupt. */
6123 rc = SQLITE_CORRUPT_BKPT;
6124 }
drhdbd6a7d2017-04-05 12:39:49 +00006125 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00006126 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006127 }
drh3c23a882007-01-09 14:01:13 +00006128 }
drh9467abf2016-02-17 18:44:11 +00006129 if( rc ){
6130 sqlite3ResetAllSchemasOfConnection(db);
6131 if( rc==SQLITE_NOMEM ){
6132 goto no_mem;
6133 }
6134 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00006135 }
drh234c39d2004-07-24 03:30:47 +00006136 break;
6137}
6138
drh8bfdf722009-06-19 14:06:03 +00006139#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00006140/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00006141**
6142** Read the sqlite_stat1 table for database P1 and load the content
6143** of that table into the internal index hash table. This will cause
6144** the analysis to be used when preparing all subsequent queries.
6145*/
drh9cbf3422008-01-17 16:22:13 +00006146case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00006147 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6148 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006149 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00006150 break;
6151}
drh8bfdf722009-06-19 14:06:03 +00006152#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00006153
drh98757152008-01-09 23:04:12 +00006154/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006155**
6156** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006157** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00006158** is dropped from disk (using the Destroy opcode) in order to keep
6159** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006160** schema consistent with what is on disk.
6161*/
drh9cbf3422008-01-17 16:22:13 +00006162case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00006163 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006164 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006165 break;
6166}
6167
drh98757152008-01-09 23:04:12 +00006168/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006169**
6170** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006171** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00006172** is dropped from disk (using the Destroy opcode)
6173** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00006174** schema consistent with what is on disk.
6175*/
drh9cbf3422008-01-17 16:22:13 +00006176case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00006177 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006178 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006179 break;
6180}
6181
drh98757152008-01-09 23:04:12 +00006182/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006183**
6184** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006185** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00006186** is dropped from disk (using the Destroy opcode) in order to keep
6187** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006188** schema consistent with what is on disk.
6189*/
drh9cbf3422008-01-17 16:22:13 +00006190case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00006191 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006192 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006193 break;
6194}
6195
drh234c39d2004-07-24 03:30:47 +00006196
drhb7f91642004-10-31 02:22:47 +00006197#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00006198/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00006199**
drh98757152008-01-09 23:04:12 +00006200** Do an analysis of the currently open database. Store in
6201** register P1 the text of an error message describing any problems.
6202** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00006203**
drh66accfc2017-02-22 18:04:42 +00006204** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00006205** At most reg(P3) errors will be reported.
6206** In other words, the analysis stops as soon as reg(P1) errors are
6207** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00006208**
drh98968b22016-03-15 22:00:39 +00006209** The root page numbers of all tables in the database are integers
6210** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00006211**
drh98757152008-01-09 23:04:12 +00006212** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00006213** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00006214**
drh1dcdbc02007-01-27 02:24:54 +00006215** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00006216*/
drhaaab5722002-02-19 13:39:21 +00006217case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00006218 int nRoot; /* Number of tables to check. (Number of root pages.) */
6219 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00006220 int nErr; /* Number of errors reported */
6221 char *z; /* Text of the error report */
6222 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00006223
drh1713afb2013-06-28 01:24:57 +00006224 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00006225 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00006226 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00006227 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00006228 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00006229 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006230 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00006231 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00006232 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00006233 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00006234 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006235 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh21f6daa2019-10-11 14:21:48 +00006236 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00006237 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00006238 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00006239 if( nErr==0 ){
6240 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00006241 }else if( z==0 ){
6242 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00006243 }else{
drh66accfc2017-02-22 18:04:42 +00006244 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00006245 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00006246 }
drhb7654112008-01-12 12:48:07 +00006247 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00006248 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh21f6daa2019-10-11 14:21:48 +00006249 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006250}
drhb7f91642004-10-31 02:22:47 +00006251#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00006252
drh3d4501e2008-12-04 20:40:10 +00006253/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00006254** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006255**
drhbb6783b2017-04-29 18:02:49 +00006256** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00006257** held in register P1.
6258**
6259** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00006260*/
drh93952eb2009-11-13 19:43:43 +00006261case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00006262 pIn1 = &aMem[pOp->p1];
6263 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00006264 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00006265 if( (pIn1->flags & MEM_Blob)==0 ){
6266 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00006267 }
drh9d67afc2018-08-29 20:24:03 +00006268 assert( sqlite3VdbeMemIsRowSet(pIn1) );
6269 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00006270 break;
6271}
6272
6273/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00006274** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00006275**
drhbb6783b2017-04-29 18:02:49 +00006276** Extract the smallest value from the RowSet object in P1
6277** and put that value into register P3.
6278** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00006279** unchanged and jump to instruction P2.
6280*/
drh93952eb2009-11-13 19:43:43 +00006281case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00006282 i64 val;
drh49afe3a2013-07-10 03:05:14 +00006283
drh3c657212009-11-17 23:59:58 +00006284 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00006285 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
6286 if( (pIn1->flags & MEM_Blob)==0
6287 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00006288 ){
6289 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00006290 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00006291 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00006292 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00006293 }else{
6294 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00006295 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00006296 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00006297 }
drh49afe3a2013-07-10 03:05:14 +00006298 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006299}
6300
drh1b26c7c2009-04-22 02:15:47 +00006301/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00006302** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00006303**
drhade97602009-04-21 15:05:18 +00006304** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00006305** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00006306** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00006307** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00006308** next opcode.
danielk19771d461462009-04-21 09:02:45 +00006309**
drhbb6783b2017-04-29 18:02:49 +00006310** The RowSet object is optimized for the case where sets of integers
6311** are inserted in distinct phases, which each set contains no duplicates.
6312** Each set is identified by a unique P4 value. The first set
6313** must have P4==0, the final set must have P4==-1, and for all other sets
6314** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00006315**
6316** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006317** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006318** (b) when P4==-1 there is no need to insert the value, as it will
6319** never be tested for, and (c) when a value that is part of set X is
6320** inserted, there is no need to search to see if the same value was
6321** previously inserted as part of set X (only if it was previously
6322** inserted as part of some other set).
6323*/
drh1b26c7c2009-04-22 02:15:47 +00006324case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006325 int iSet;
6326 int exists;
6327
drh3c657212009-11-17 23:59:58 +00006328 pIn1 = &aMem[pOp->p1];
6329 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006330 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006331 assert( pIn3->flags&MEM_Int );
6332
drh1b26c7c2009-04-22 02:15:47 +00006333 /* If there is anything other than a rowset object in memory cell P1,
6334 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006335 */
drh9d67afc2018-08-29 20:24:03 +00006336 if( (pIn1->flags & MEM_Blob)==0 ){
6337 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006338 }
drh9d67afc2018-08-29 20:24:03 +00006339 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006340 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006341 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006342 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006343 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006344 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006345 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006346 }
6347 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006348 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006349 }
6350 break;
6351}
6352
drh5e00f6c2001-09-13 13:46:56 +00006353
danielk197793758c82005-01-21 08:13:14 +00006354#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006355
drh0fd61352014-02-07 02:29:45 +00006356/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006357**
dan76d462e2009-08-30 11:42:51 +00006358** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006359**
dan76d462e2009-08-30 11:42:51 +00006360** P1 contains the address of the memory cell that contains the first memory
6361** cell in an array of values used as arguments to the sub-program. P2
6362** contains the address to jump to if the sub-program throws an IGNORE
6363** exception using the RAISE() function. Register P3 contains the address
6364** of a memory cell in this (the parent) VM that is used to allocate the
6365** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006366**
6367** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006368**
6369** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006370*/
dan76d462e2009-08-30 11:42:51 +00006371case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006372 int nMem; /* Number of memory registers for sub-program */
6373 int nByte; /* Bytes of runtime space required for sub-program */
6374 Mem *pRt; /* Register to allocate runtime space */
6375 Mem *pMem; /* Used to iterate through memory cells */
6376 Mem *pEnd; /* Last memory cell in new array */
6377 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6378 SubProgram *pProgram; /* Sub-program to execute */
6379 void *t; /* Token identifying trigger */
6380
6381 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006382 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006383 assert( pProgram->nOp>0 );
6384
dan1da40a32009-09-19 17:00:31 +00006385 /* If the p5 flag is clear, then recursive invocation of triggers is
6386 ** disabled for backwards compatibility (p5 is set if this sub-program
6387 ** is really a trigger, not a foreign key action, and the flag set
6388 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006389 **
6390 ** It is recursive invocation of triggers, at the SQL level, that is
6391 ** disabled. In some cases a single trigger may generate more than one
6392 ** SubProgram (if the trigger may be executed with more than one different
6393 ** ON CONFLICT algorithm). SubProgram structures associated with a
6394 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006395 ** variable. */
6396 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006397 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006398 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6399 if( pFrame ) break;
6400 }
6401
danf5894502009-10-07 18:41:19 +00006402 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006403 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006404 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006405 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006406 }
6407
6408 /* Register pRt is used to store the memory required to save the state
6409 ** of the current program, and the memory required at runtime to execute
6410 ** the trigger program. If this trigger has been fired before, then pRt
6411 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006412 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006413 /* SubProgram.nMem is set to the number of memory cells used by the
6414 ** program stored in SubProgram.aOp. As well as these, one memory
6415 ** cell is required for each cursor used by the program. Set local
6416 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6417 */
dan65a7cd12009-09-01 12:16:01 +00006418 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006419 assert( nMem>0 );
6420 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006421 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006422 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006423 + pProgram->nCsr * sizeof(VdbeCursor*)
6424 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006425 pFrame = sqlite3DbMallocZero(db, nByte);
6426 if( !pFrame ){
6427 goto no_mem;
6428 }
6429 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006430 pRt->flags = MEM_Blob|MEM_Dyn;
6431 pRt->z = (char*)pFrame;
6432 pRt->n = nByte;
6433 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006434
6435 pFrame->v = p;
6436 pFrame->nChildMem = nMem;
6437 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006438 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006439 pFrame->aMem = p->aMem;
6440 pFrame->nMem = p->nMem;
6441 pFrame->apCsr = p->apCsr;
6442 pFrame->nCursor = p->nCursor;
6443 pFrame->aOp = p->aOp;
6444 pFrame->nOp = p->nOp;
6445 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006446#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006447 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006448#endif
drh72f56ef2018-08-29 18:47:22 +00006449#ifdef SQLITE_DEBUG
6450 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
6451#endif
dan165921a2009-08-28 18:53:45 +00006452
6453 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6454 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006455 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006456 pMem->db = db;
6457 }
6458 }else{
drh72f56ef2018-08-29 18:47:22 +00006459 pFrame = (VdbeFrame*)pRt->z;
6460 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00006461 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6462 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006463 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006464 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006465 }
6466
6467 p->nFrame++;
6468 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006469 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006470 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006471 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006472 assert( pFrame->pAuxData==0 );
6473 pFrame->pAuxData = p->pAuxData;
6474 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006475 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006476 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006477 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006478 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006479 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006480 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006481 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006482 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006483 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006484 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006485#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006486 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006487#endif
drhb2e61bc2019-01-25 19:29:01 +00006488#ifdef SQLITE_DEBUG
6489 /* Verify that second and subsequent executions of the same trigger do not
6490 ** try to reuse register values from the first use. */
6491 {
6492 int i;
6493 for(i=0; i<p->nMem; i++){
6494 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
drhf5cfe6f2020-03-03 20:48:12 +00006495 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
drhb2e61bc2019-01-25 19:29:01 +00006496 }
6497 }
6498#endif
drhf56fa462015-04-13 21:39:54 +00006499 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00006500 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00006501}
6502
dan76d462e2009-08-30 11:42:51 +00006503/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006504**
dan76d462e2009-08-30 11:42:51 +00006505** This opcode is only ever present in sub-programs called via the
6506** OP_Program instruction. Copy a value currently stored in a memory
6507** cell of the calling (parent) frame to cell P2 in the current frames
6508** address space. This is used by trigger programs to access the new.*
6509** and old.* values.
dan165921a2009-08-28 18:53:45 +00006510**
dan76d462e2009-08-30 11:42:51 +00006511** The address of the cell in the parent frame is determined by adding
6512** the value of the P1 argument to the value of the P1 argument to the
6513** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006514*/
drh27a348c2015-04-13 19:14:06 +00006515case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006516 VdbeFrame *pFrame;
6517 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006518 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006519 pFrame = p->pFrame;
6520 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006521 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6522 break;
6523}
6524
danielk197793758c82005-01-21 08:13:14 +00006525#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006526
dan1da40a32009-09-19 17:00:31 +00006527#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006528/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006529** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006530**
dan0ff297e2009-09-25 17:03:14 +00006531** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6532** If P1 is non-zero, the database constraint counter is incremented
6533** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006534** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006535*/
dan32b09f22009-09-23 17:29:59 +00006536case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006537 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006538 db->nDeferredImmCons += pOp->p2;
6539 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006540 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006541 }else{
dan0ff297e2009-09-25 17:03:14 +00006542 p->nFkConstraint += pOp->p2;
6543 }
6544 break;
6545}
6546
6547/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006548** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006549**
6550** This opcode tests if a foreign key constraint-counter is currently zero.
6551** If so, jump to instruction P2. Otherwise, fall through to the next
6552** instruction.
6553**
6554** If P1 is non-zero, then the jump is taken if the database constraint-counter
6555** is zero (the one that counts deferred constraint violations). If P1 is
6556** zero, the jump is taken if the statement constraint-counter is zero
6557** (immediate foreign key constraint violations).
6558*/
6559case OP_FkIfZero: { /* jump */
6560 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006561 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006562 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006563 }else{
drh688852a2014-02-17 22:40:43 +00006564 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006565 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006566 }
dan1da40a32009-09-19 17:00:31 +00006567 break;
6568}
6569#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6570
drh205f48e2004-11-05 00:43:11 +00006571#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006572/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006573** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006574**
dan76d462e2009-08-30 11:42:51 +00006575** P1 is a register in the root frame of this VM (the root frame is
6576** different from the current frame if this instruction is being executed
6577** within a sub-program). Set the value of register P1 to the maximum of
6578** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006579**
6580** This instruction throws an error if the memory cell is not initially
6581** an integer.
6582*/
dan76d462e2009-08-30 11:42:51 +00006583case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006584 VdbeFrame *pFrame;
6585 if( p->pFrame ){
6586 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6587 pIn1 = &pFrame->aMem[pOp->p1];
6588 }else{
drha6c2ed92009-11-14 23:22:23 +00006589 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006590 }
drh2b4ded92010-09-27 21:09:31 +00006591 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006592 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006593 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006594 sqlite3VdbeMemIntegerify(pIn2);
6595 if( pIn1->u.i<pIn2->u.i){
6596 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006597 }
6598 break;
6599}
6600#endif /* SQLITE_OMIT_AUTOINCREMENT */
6601
drh8b0cf382015-10-06 21:07:06 +00006602/* Opcode: IfPos P1 P2 P3 * *
6603** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006604**
drh16897072015-03-07 00:57:37 +00006605** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006606** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006607** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006608**
drh16897072015-03-07 00:57:37 +00006609** If the initial value of register P1 is less than 1, then the
6610** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006611*/
drh9cbf3422008-01-17 16:22:13 +00006612case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006613 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006614 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006615 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006616 if( pIn1->u.i>0 ){
6617 pIn1->u.i -= pOp->p3;
6618 goto jump_to_p2;
6619 }
drhec7429a2005-10-06 16:53:14 +00006620 break;
6621}
6622
drhcc2fa4c2016-01-25 15:57:29 +00006623/* Opcode: OffsetLimit P1 P2 P3 * *
6624** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006625**
drhcc2fa4c2016-01-25 15:57:29 +00006626** This opcode performs a commonly used computation associated with
6627** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6628** holds the offset counter. The opcode computes the combined value
6629** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6630** value computed is the total number of rows that will need to be
6631** visited in order to complete the query.
6632**
6633** If r[P3] is zero or negative, that means there is no OFFSET
6634** and r[P2] is set to be the value of the LIMIT, r[P1].
6635**
6636** if r[P1] is zero or negative, that means there is no LIMIT
6637** and r[P2] is set to -1.
6638**
6639** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006640*/
drhcc2fa4c2016-01-25 15:57:29 +00006641case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006642 i64 x;
drh3c657212009-11-17 23:59:58 +00006643 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006644 pIn3 = &aMem[pOp->p3];
6645 pOut = out2Prerelease(p, pOp);
6646 assert( pIn1->flags & MEM_Int );
6647 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006648 x = pIn1->u.i;
6649 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6650 /* If the LIMIT is less than or equal to zero, loop forever. This
6651 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6652 ** also loop forever. This is undocumented. In fact, one could argue
6653 ** that the loop should terminate. But assuming 1 billion iterations
6654 ** per second (far exceeding the capabilities of any current hardware)
6655 ** it would take nearly 300 years to actually reach the limit. So
6656 ** looping forever is a reasonable approximation. */
6657 pOut->u.i = -1;
6658 }else{
6659 pOut->u.i = x;
6660 }
drh15007a92006-01-08 18:10:17 +00006661 break;
6662}
6663
drhf99dd352016-12-18 17:42:00 +00006664/* Opcode: IfNotZero P1 P2 * * *
6665** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006666**
drh16897072015-03-07 00:57:37 +00006667** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006668** initially greater than zero, then decrement the value in register P1.
6669** If it is non-zero (negative or positive) and then also jump to P2.
6670** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006671*/
drh16897072015-03-07 00:57:37 +00006672case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006673 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006674 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006675 VdbeBranchTaken(pIn1->u.i<0, 2);
6676 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006677 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006678 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006679 }
6680 break;
6681}
6682
6683/* Opcode: DecrJumpZero P1 P2 * * *
6684** Synopsis: if (--r[P1])==0 goto P2
6685**
drhab5be2e2016-11-30 05:08:59 +00006686** Register P1 must hold an integer. Decrement the value in P1
6687** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006688*/
6689case OP_DecrJumpZero: { /* jump, in1 */
6690 pIn1 = &aMem[pOp->p1];
6691 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006692 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6693 VdbeBranchTaken(pIn1->u.i==0, 2);
6694 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006695 break;
6696}
6697
drh16897072015-03-07 00:57:37 +00006698
drh8f26da62018-07-05 21:22:57 +00006699/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006700** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006701**
drh8f26da62018-07-05 21:22:57 +00006702** Execute the xStep function for an aggregate.
6703** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006704** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006705** accumulator.
drhe5095352002-02-24 03:25:14 +00006706**
drh98757152008-01-09 23:04:12 +00006707** The P5 arguments are taken from register P2 and its
6708** successors.
drhe5095352002-02-24 03:25:14 +00006709*/
drh8f26da62018-07-05 21:22:57 +00006710/* Opcode: AggInverse * P2 P3 P4 P5
6711** Synopsis: accum=r[P3] inverse(r[P2@P5])
6712**
6713** Execute the xInverse function for an aggregate.
6714** The function has P5 arguments. P4 is a pointer to the
6715** FuncDef structure that specifies the function. Register P3 is the
6716** accumulator.
6717**
6718** The P5 arguments are taken from register P2 and its
6719** successors.
6720*/
6721/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006722** Synopsis: accum=r[P3] step(r[P2@P5])
6723**
dan9a947222018-06-14 19:06:36 +00006724** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6725** aggregate. The function has P5 arguments. P4 is a pointer to the
6726** FuncDef structure that specifies the function. Register P3 is the
6727** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006728**
6729** The P5 arguments are taken from register P2 and its
6730** successors.
6731**
6732** This opcode is initially coded as OP_AggStep0. On first evaluation,
6733** the FuncDef stored in P4 is converted into an sqlite3_context and
6734** the opcode is changed. In this way, the initialization of the
6735** sqlite3_context only happens once, instead of on each call to the
6736** step function.
6737*/
drh8f26da62018-07-05 21:22:57 +00006738case OP_AggInverse:
6739case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006740 int n;
drh9c7c9132015-06-26 18:16:52 +00006741 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006742
drh9c7c9132015-06-26 18:16:52 +00006743 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006744 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006745 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6746 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006747 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006748 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6749 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006750 if( pCtx==0 ) goto no_mem;
6751 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006752 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6753 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006754 pCtx->pFunc = pOp->p4.pFunc;
6755 pCtx->iOp = (int)(pOp - aOp);
6756 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006757 pCtx->skipFlag = 0;
6758 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006759 pCtx->argc = n;
6760 pOp->p4type = P4_FUNCCTX;
6761 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006762
6763 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006764 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006765
drh8f26da62018-07-05 21:22:57 +00006766 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006767 /* Fall through into OP_AggStep */
6768}
drh8f26da62018-07-05 21:22:57 +00006769case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006770 int i;
6771 sqlite3_context *pCtx;
6772 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006773
6774 assert( pOp->p4type==P4_FUNCCTX );
6775 pCtx = pOp->p4.pCtx;
6776 pMem = &aMem[pOp->p3];
6777
drh2c885d02018-07-07 19:36:04 +00006778#ifdef SQLITE_DEBUG
6779 if( pOp->p1 ){
6780 /* This is an OP_AggInverse call. Verify that xStep has always
6781 ** been called at least once prior to any xInverse call. */
6782 assert( pMem->uTemp==0x1122e0e3 );
6783 }else{
6784 /* This is an OP_AggStep call. Mark it as such. */
6785 pMem->uTemp = 0x1122e0e3;
6786 }
6787#endif
6788
drh9c7c9132015-06-26 18:16:52 +00006789 /* If this function is inside of a trigger, the register array in aMem[]
6790 ** might change from one evaluation to the next. The next block of code
6791 ** checks to see if the register array has changed, and if so it
6792 ** reinitializes the relavant parts of the sqlite3_context object */
6793 if( pCtx->pMem != pMem ){
6794 pCtx->pMem = pMem;
6795 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6796 }
6797
6798#ifdef SQLITE_DEBUG
6799 for(i=0; i<pCtx->argc; i++){
6800 assert( memIsValid(pCtx->argv[i]) );
6801 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6802 }
6803#endif
6804
drhabfcea22005-09-06 20:36:48 +00006805 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006806 assert( pCtx->pOut->flags==MEM_Null );
6807 assert( pCtx->isError==0 );
6808 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006809#ifndef SQLITE_OMIT_WINDOWFUNC
6810 if( pOp->p1 ){
6811 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6812 }else
6813#endif
6814 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6815
drhf09ac0b2018-01-23 03:44:06 +00006816 if( pCtx->isError ){
6817 if( pCtx->isError>0 ){
6818 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006819 rc = pCtx->isError;
6820 }
drhf09ac0b2018-01-23 03:44:06 +00006821 if( pCtx->skipFlag ){
6822 assert( pOp[-1].opcode==OP_CollSeq );
6823 i = pOp[-1].p1;
6824 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6825 pCtx->skipFlag = 0;
6826 }
6827 sqlite3VdbeMemRelease(pCtx->pOut);
6828 pCtx->pOut->flags = MEM_Null;
6829 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006830 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006831 }
drhf09ac0b2018-01-23 03:44:06 +00006832 assert( pCtx->pOut->flags==MEM_Null );
6833 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006834 break;
6835}
6836
drh8f26da62018-07-05 21:22:57 +00006837/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006838** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006839**
dan9a947222018-06-14 19:06:36 +00006840** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00006841** or window function. Execute the finalizer function
6842** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00006843**
6844** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006845** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006846** argument is not used by this opcode. It is only there to disambiguate
6847** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00006848** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00006849** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006850*/
drh8f26da62018-07-05 21:22:57 +00006851/* Opcode: AggValue * P2 P3 P4 *
6852** Synopsis: r[P3]=value N=P2
6853**
6854** Invoke the xValue() function and store the result in register P3.
6855**
6856** P2 is the number of arguments that the step function takes and
6857** P4 is a pointer to the FuncDef for this function. The P2
6858** argument is not used by this opcode. It is only there to disambiguate
6859** functions that can take varying numbers of arguments. The
6860** P4 argument is only needed for the case where
6861** the step function was not previously called.
6862*/
6863case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00006864case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006865 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006866 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00006867 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00006868 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006869 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006870#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006871 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00006872 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00006873 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006874 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006875 }else
6876#endif
drh8f26da62018-07-05 21:22:57 +00006877 {
6878 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6879 }
dan67a9b8e2018-06-22 20:51:35 +00006880
drh4c8555f2009-06-25 01:47:11 +00006881 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006882 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006883 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006884 }
drh2dca8682008-03-21 17:13:13 +00006885 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006886 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006887 if( sqlite3VdbeMemTooBig(pMem) ){
6888 goto too_big;
6889 }
drh5e00f6c2001-09-13 13:46:56 +00006890 break;
6891}
6892
dan5cf53532010-05-01 16:40:20 +00006893#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006894/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006895**
6896** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006897** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6898** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006899** SQLITE_BUSY or not, respectively. Write the number of pages in the
6900** WAL after the checkpoint into mem[P3+1] and the number of pages
6901** in the WAL that have been checkpointed after the checkpoint
6902** completes into mem[P3+2]. However on an error, mem[P3+1] and
6903** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006904*/
6905case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006906 int i; /* Loop counter */
6907 int aRes[3]; /* Results */
6908 Mem *pMem; /* Write results here */
6909
drh9e92a472013-06-27 17:40:30 +00006910 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006911 aRes[0] = 0;
6912 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006913 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6914 || pOp->p2==SQLITE_CHECKPOINT_FULL
6915 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006916 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006917 );
drh30aa3b92011-02-07 23:56:01 +00006918 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006919 if( rc ){
6920 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006921 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006922 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006923 }
drh30aa3b92011-02-07 23:56:01 +00006924 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6925 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6926 }
dan7c246102010-04-12 19:00:29 +00006927 break;
6928};
dan5cf53532010-05-01 16:40:20 +00006929#endif
drh5e00f6c2001-09-13 13:46:56 +00006930
drhcac29a62010-07-02 19:36:52 +00006931#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006932/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006933**
6934** Change the journal mode of database P1 to P3. P3 must be one of the
6935** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6936** modes (delete, truncate, persist, off and memory), this is a simple
6937** operation. No IO is required.
6938**
6939** If changing into or out of WAL mode the procedure is more complicated.
6940**
6941** Write a string containing the final journal-mode to register P2.
6942*/
drh27a348c2015-04-13 19:14:06 +00006943case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006944 Btree *pBt; /* Btree to change journal mode of */
6945 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006946 int eNew; /* New journal mode */
6947 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006948#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006949 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006950#endif
dane04dc882010-04-20 18:53:15 +00006951
drh27a348c2015-04-13 19:14:06 +00006952 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006953 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006954 assert( eNew==PAGER_JOURNALMODE_DELETE
6955 || eNew==PAGER_JOURNALMODE_TRUNCATE
6956 || eNew==PAGER_JOURNALMODE_PERSIST
6957 || eNew==PAGER_JOURNALMODE_OFF
6958 || eNew==PAGER_JOURNALMODE_MEMORY
6959 || eNew==PAGER_JOURNALMODE_WAL
6960 || eNew==PAGER_JOURNALMODE_QUERY
6961 );
6962 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006963 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006964
dane04dc882010-04-20 18:53:15 +00006965 pBt = db->aDb[pOp->p1].pBt;
6966 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006967 eOld = sqlite3PagerGetJournalMode(pPager);
6968 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6969 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006970
6971#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006972 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006973
drhd80b2332010-05-01 00:59:37 +00006974 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006975 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006976 */
6977 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006978 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006979 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006980 ){
drh0b9b4302010-06-11 17:01:24 +00006981 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006982 }
6983
drh0b9b4302010-06-11 17:01:24 +00006984 if( (eNew!=eOld)
6985 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6986 ){
danc0537fe2013-06-28 19:41:43 +00006987 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006988 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006989 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006990 "cannot change %s wal mode from within a transaction",
6991 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6992 );
drh9467abf2016-02-17 18:44:11 +00006993 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006994 }else{
6995
6996 if( eOld==PAGER_JOURNALMODE_WAL ){
6997 /* If leaving WAL mode, close the log file. If successful, the call
6998 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6999 ** file. An EXCLUSIVE lock may still be held on the database file
7000 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00007001 */
dan7fb89902016-08-12 16:21:15 +00007002 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00007003 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00007004 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00007005 }
drh242c4f72010-06-22 14:49:39 +00007006 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
7007 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7008 ** as an intermediate */
7009 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00007010 }
7011
7012 /* Open a transaction on the database file. Regardless of the journal
7013 ** mode, this transaction always uses a rollback journal.
7014 */
7015 assert( sqlite3BtreeIsInTrans(pBt)==0 );
7016 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00007017 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00007018 }
7019 }
7020 }
dan5cf53532010-05-01 16:40:20 +00007021#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00007022
drh9467abf2016-02-17 18:44:11 +00007023 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00007024 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00007025
dane04dc882010-04-20 18:53:15 +00007026 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00007027 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00007028 pOut->n = sqlite3Strlen30(pOut->z);
7029 pOut->enc = SQLITE_UTF8;
7030 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00007031 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00007032 break;
drhcac29a62010-07-02 19:36:52 +00007033};
7034#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00007035
drhfdbcdee2007-03-27 14:44:50 +00007036#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00007037/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00007038**
drh9ef5e772016-08-19 14:20:56 +00007039** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7040** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00007041**
drh2f6239e2018-12-08 00:43:08 +00007042** If P2 is not zero, then it is a register holding a string which is
7043** the file into which the result of vacuum should be written. When
7044** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00007045*/
drh9cbf3422008-01-17 16:22:13 +00007046case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00007047 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00007048 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
7049 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00007050 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00007051 break;
7052}
drh154d4b22006-09-21 11:02:16 +00007053#endif
drh6f8c91c2003-12-07 00:24:35 +00007054
danielk1977dddbcdc2007-04-26 14:42:34 +00007055#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00007056/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00007057**
7058** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00007059** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00007060** P2. Otherwise, fall through to the next instruction.
7061*/
drh9cbf3422008-01-17 16:22:13 +00007062case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00007063 Btree *pBt;
7064
7065 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007066 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00007067 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00007068 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00007069 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00007070 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00007071 if( rc ){
7072 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00007073 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00007074 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00007075 }
7076 break;
7077}
7078#endif
7079
drhba968db2018-07-24 22:02:12 +00007080/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00007081**
drh25df48d2014-07-22 14:58:12 +00007082** Cause precompiled statements to expire. When an expired statement
7083** is executed using sqlite3_step() it will either automatically
7084** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7085** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00007086**
7087** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00007088** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00007089**
7090** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7091** then running SQL statements are allowed to continue to run to completion.
7092** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7093** that might help the statement run faster but which does not affect the
7094** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00007095*/
drh9cbf3422008-01-17 16:22:13 +00007096case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00007097 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00007098 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00007099 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00007100 }else{
drhba968db2018-07-24 22:02:12 +00007101 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00007102 }
7103 break;
7104}
7105
drh7b14b652019-12-29 22:08:20 +00007106/* Opcode: CursorLock P1 * * * *
7107**
7108** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7109** written by an other cursor.
7110*/
7111case OP_CursorLock: {
7112 VdbeCursor *pC;
7113 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7114 pC = p->apCsr[pOp->p1];
7115 assert( pC!=0 );
7116 assert( pC->eCurType==CURTYPE_BTREE );
7117 sqlite3BtreeCursorPin(pC->uc.pCursor);
7118 break;
7119}
7120
7121/* Opcode: CursorUnlock P1 * * * *
7122**
7123** Unlock the btree to which cursor P1 is pointing so that it can be
7124** written by other cursors.
7125*/
7126case OP_CursorUnlock: {
7127 VdbeCursor *pC;
7128 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7129 pC = p->apCsr[pOp->p1];
7130 assert( pC!=0 );
7131 assert( pC->eCurType==CURTYPE_BTREE );
7132 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
7133 break;
7134}
7135
danielk1977c00da102006-01-07 13:21:04 +00007136#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00007137/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00007138** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00007139**
7140** Obtain a lock on a particular table. This instruction is only used when
7141** the shared-cache feature is enabled.
7142**
danielk197796d48e92009-06-29 06:00:37 +00007143** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00007144** on which the lock is acquired. A readlock is obtained if P3==0 or
7145** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00007146**
7147** P2 contains the root-page of the table to lock.
7148**
drh66a51672008-01-03 00:01:23 +00007149** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00007150** used to generate an error message if the lock cannot be obtained.
7151*/
drh9cbf3422008-01-17 16:22:13 +00007152case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00007153 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00007154 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00007155 int p1 = pOp->p1;
7156 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007157 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00007158 assert( isWriteLock==0 || isWriteLock==1 );
7159 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00007160 if( rc ){
7161 if( (rc&0xFF)==SQLITE_LOCKED ){
7162 const char *z = pOp->p4.z;
7163 sqlite3VdbeError(p, "database table is locked: %s", z);
7164 }
7165 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00007166 }
danielk1977c00da102006-01-07 13:21:04 +00007167 }
7168 break;
7169}
drhb9bb7c12006-06-11 23:41:55 +00007170#endif /* SQLITE_OMIT_SHARED_CACHE */
7171
7172#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007173/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007174**
danielk19773e3a84d2008-08-01 17:37:40 +00007175** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7176** xBegin method for that table.
7177**
7178** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00007179** within a callback to a virtual table xSync() method. If it is, the error
7180** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00007181*/
drh9cbf3422008-01-17 16:22:13 +00007182case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00007183 VTable *pVTab;
7184 pVTab = pOp->p4.pVtab;
7185 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00007186 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00007187 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00007188 break;
7189}
7190#endif /* SQLITE_OMIT_VIRTUALTABLE */
7191
7192#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00007193/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00007194**
dan73779452015-03-19 18:56:17 +00007195** P2 is a register that holds the name of a virtual table in database
7196** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00007197*/
drh9cbf3422008-01-17 16:22:13 +00007198case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00007199 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00007200 const char *zTab; /* Name of the virtual table */
7201
dan73779452015-03-19 18:56:17 +00007202 memset(&sMem, 0, sizeof(sMem));
7203 sMem.db = db;
drh47464062015-03-21 12:22:16 +00007204 /* Because P2 is always a static string, it is impossible for the
7205 ** sqlite3VdbeMemCopy() to fail */
7206 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
7207 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00007208 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00007209 assert( rc==SQLITE_OK );
7210 zTab = (const char*)sqlite3_value_text(&sMem);
7211 assert( zTab || db->mallocFailed );
7212 if( zTab ){
7213 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00007214 }
7215 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00007216 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007217 break;
7218}
7219#endif /* SQLITE_OMIT_VIRTUALTABLE */
7220
7221#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007222/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007223**
drh66a51672008-01-03 00:01:23 +00007224** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00007225** of that table.
drhb9bb7c12006-06-11 23:41:55 +00007226*/
drh9cbf3422008-01-17 16:22:13 +00007227case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00007228 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00007229 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00007230 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00007231 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00007232 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007233 break;
7234}
7235#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00007236
drh9eff6162006-06-12 21:59:13 +00007237#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007238/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00007239**
drh66a51672008-01-03 00:01:23 +00007240** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00007241** P1 is a cursor number. This opcode opens a cursor to the virtual
7242** table and stores that cursor in P1.
7243*/
drh9cbf3422008-01-17 16:22:13 +00007244case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00007245 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00007246 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00007247 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007248 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007249
drh1713afb2013-06-28 01:24:57 +00007250 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00007251 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00007252 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00007253 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007254 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7255 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007256 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007257 }
7258 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00007259 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00007260 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007261 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007262
drh9467abf2016-02-17 18:44:11 +00007263 /* Initialize sqlite3_vtab_cursor base class */
7264 pVCur->pVtab = pVtab;
7265
7266 /* Initialize vdbe cursor object */
7267 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
7268 if( pCur ){
7269 pCur->uc.pVCur = pVCur;
7270 pVtab->nRef++;
7271 }else{
7272 assert( db->mallocFailed );
7273 pModule->xClose(pVCur);
7274 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007275 }
drh9eff6162006-06-12 21:59:13 +00007276 break;
7277}
7278#endif /* SQLITE_OMIT_VIRTUALTABLE */
7279
7280#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00007281/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00007282** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00007283**
7284** P1 is a cursor opened using VOpen. P2 is an address to jump to if
7285** the filtered result set is empty.
7286**
drh66a51672008-01-03 00:01:23 +00007287** P4 is either NULL or a string that was generated by the xBestIndex
7288** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00007289** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00007290**
drh9eff6162006-06-12 21:59:13 +00007291** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00007292** by P1. The integer query plan parameter to xFilter is stored in register
7293** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00007294** xFilter method. Registers P3+2..P3+1+argc are the argc
7295** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00007296** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00007297**
danielk19776dbee812008-01-03 18:39:41 +00007298** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00007299*/
drh9cbf3422008-01-17 16:22:13 +00007300case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00007301 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00007302 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007303 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007304 Mem *pQuery;
7305 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00007306 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00007307 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00007308 VdbeCursor *pCur;
7309 int res;
7310 int i;
7311 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007312
drha6c2ed92009-11-14 23:22:23 +00007313 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00007314 pArgc = &pQuery[1];
7315 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00007316 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00007317 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00007318 assert( pCur->eCurType==CURTYPE_VTAB );
7319 pVCur = pCur->uc.pVCur;
7320 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00007321 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007322
drh9cbf3422008-01-17 16:22:13 +00007323 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00007324 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00007325 nArg = (int)pArgc->u.i;
7326 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007327
drh644a5292006-12-20 14:53:38 +00007328 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00007329 res = 0;
7330 apArg = p->apArg;
7331 for(i = 0; i<nArg; i++){
7332 apArg[i] = &pArgc[i+1];
7333 }
drhc960dcb2015-11-20 19:22:01 +00007334 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00007335 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007336 if( rc ) goto abort_due_to_error;
7337 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00007338 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00007339 VdbeBranchTaken(res!=0,2);
7340 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00007341 break;
7342}
7343#endif /* SQLITE_OMIT_VIRTUALTABLE */
7344
7345#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00007346/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007347** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007348**
drh6f390be2018-01-11 17:04:26 +00007349** Store in register P3 the value of the P2-th column of
7350** the current row of the virtual-table of cursor P1.
7351**
7352** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007353** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007354** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7355** function to return true inside the xColumn method of the virtual
7356** table implementation. The P5 column might also contain other
7357** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7358** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007359*/
7360case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007361 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007362 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007363 Mem *pDest;
7364 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007365
drhdfe88ec2008-11-03 20:55:06 +00007366 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007367 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007368 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007369 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007370 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007371 if( pCur->nullRow ){
7372 sqlite3VdbeMemSetNull(pDest);
7373 break;
7374 }
drhc960dcb2015-11-20 19:22:01 +00007375 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007376 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007377 assert( pModule->xColumn );
7378 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007379 sContext.pOut = pDest;
drh75f10762019-12-14 18:08:22 +00007380 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
drh09d00b22018-09-27 20:20:01 +00007381 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007382 sqlite3VdbeMemSetNull(pDest);
7383 pDest->flags = MEM_Null|MEM_Zero;
7384 pDest->u.nZero = 0;
7385 }else{
7386 MemSetTypeFlag(pDest, MEM_Null);
7387 }
drhc960dcb2015-11-20 19:22:01 +00007388 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007389 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007390 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007391 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007392 rc = sContext.isError;
7393 }
drh9bd038f2014-08-27 14:14:06 +00007394 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007395 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007396 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007397
drhde4fcfd2008-01-19 23:50:26 +00007398 if( sqlite3VdbeMemTooBig(pDest) ){
7399 goto too_big;
7400 }
drh9467abf2016-02-17 18:44:11 +00007401 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007402 break;
7403}
7404#endif /* SQLITE_OMIT_VIRTUALTABLE */
7405
7406#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007407/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007408**
7409** Advance virtual table P1 to the next row in its result set and
7410** jump to instruction P2. Or, if the virtual table has reached
7411** the end of its result set, then fall through to the next instruction.
7412*/
drh9cbf3422008-01-17 16:22:13 +00007413case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007414 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007415 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007416 int res;
drh856c1032009-06-02 15:21:42 +00007417 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007418
drhc54a6172009-06-02 16:06:03 +00007419 res = 0;
drh856c1032009-06-02 15:21:42 +00007420 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007421 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007422 if( pCur->nullRow ){
7423 break;
7424 }
drhc960dcb2015-11-20 19:22:01 +00007425 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007426 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007427 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007428
drhde4fcfd2008-01-19 23:50:26 +00007429 /* Invoke the xNext() method of the module. There is no way for the
7430 ** underlying implementation to return an error if one occurs during
7431 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7432 ** data is available) and the error code returned when xColumn or
7433 ** some other method is next invoked on the save virtual table cursor.
7434 */
drhc960dcb2015-11-20 19:22:01 +00007435 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007436 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007437 if( rc ) goto abort_due_to_error;
7438 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007439 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007440 if( !res ){
7441 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007442 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007443 }
drh49afe3a2013-07-10 03:05:14 +00007444 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007445}
7446#endif /* SQLITE_OMIT_VIRTUALTABLE */
7447
danielk1977182c4ba2007-06-27 15:53:34 +00007448#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007449/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007450**
drh66a51672008-01-03 00:01:23 +00007451** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007452** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007453** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007454*/
drh9cbf3422008-01-17 16:22:13 +00007455case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007456 sqlite3_vtab *pVtab;
7457 Mem *pName;
dan34566c42018-09-20 17:21:21 +00007458 int isLegacy;
7459
7460 isLegacy = (db->flags & SQLITE_LegacyAlter);
7461 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00007462 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007463 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007464 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007465 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007466 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007467 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007468 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007469 testcase( pName->enc==SQLITE_UTF8 );
7470 testcase( pName->enc==SQLITE_UTF16BE );
7471 testcase( pName->enc==SQLITE_UTF16LE );
7472 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007473 if( rc ) goto abort_due_to_error;
7474 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00007475 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00007476 sqlite3VtabImportErrmsg(p, pVtab);
7477 p->expired = 0;
7478 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007479 break;
7480}
7481#endif
drh4cbdda92006-06-14 19:00:20 +00007482
7483#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007484/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007485** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007486**
drh66a51672008-01-03 00:01:23 +00007487** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007488** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007489** are contiguous memory cells starting at P3 to pass to the xUpdate
7490** invocation. The value in register (P3+P2-1) corresponds to the
7491** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007492**
7493** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007494** The argv[0] element (which corresponds to memory cell P3)
7495** is the rowid of a row to delete. If argv[0] is NULL then no
7496** deletion occurs. The argv[1] element is the rowid of the new
7497** row. This can be NULL to have the virtual table select the new
7498** rowid for itself. The subsequent elements in the array are
7499** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007500**
7501** If P2==1 then no insert is performed. argv[0] is the rowid of
7502** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007503**
7504** P1 is a boolean flag. If it is set to true and the xUpdate call
7505** is successful, then the value returned by sqlite3_last_insert_rowid()
7506** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007507**
7508** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7509** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007510*/
drh9cbf3422008-01-17 16:22:13 +00007511case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007512 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007513 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007514 int nArg;
7515 int i;
7516 sqlite_int64 rowid;
7517 Mem **apArg;
7518 Mem *pX;
7519
danb061d052011-04-25 18:49:57 +00007520 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7521 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7522 );
drh9e92a472013-06-27 17:40:30 +00007523 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007524 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007525 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007526 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007527 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7528 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007529 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007530 }
7531 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007532 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007533 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007534 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007535 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007536 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007537 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007538 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007539 assert( memIsValid(pX) );
7540 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007541 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007542 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007543 }
danb061d052011-04-25 18:49:57 +00007544 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007545 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007546 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007547 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007548 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007549 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007550 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007551 }
drhd91c1a12013-02-09 13:58:25 +00007552 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007553 if( pOp->p5==OE_Ignore ){
7554 rc = SQLITE_OK;
7555 }else{
7556 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7557 }
7558 }else{
7559 p->nChange++;
7560 }
drh9467abf2016-02-17 18:44:11 +00007561 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007562 }
drh4cbdda92006-06-14 19:00:20 +00007563 break;
danielk1977399918f2006-06-14 13:03:23 +00007564}
7565#endif /* SQLITE_OMIT_VIRTUALTABLE */
7566
danielk197759a93792008-05-15 17:48:20 +00007567#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7568/* Opcode: Pagecount P1 P2 * * *
7569**
7570** Write the current number of pages in database P1 to memory cell P2.
7571*/
drh27a348c2015-04-13 19:14:06 +00007572case OP_Pagecount: { /* out2 */
7573 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007574 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007575 break;
7576}
7577#endif
7578
drh60ac3f42010-11-23 18:59:27 +00007579
7580#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7581/* Opcode: MaxPgcnt P1 P2 P3 * *
7582**
7583** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007584** Do not let the maximum page count fall below the current page count and
7585** do not change the maximum page count value if P3==0.
7586**
drh60ac3f42010-11-23 18:59:27 +00007587** Store the maximum page count after the change in register P2.
7588*/
drh27a348c2015-04-13 19:14:06 +00007589case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007590 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007591 Btree *pBt;
7592
drh27a348c2015-04-13 19:14:06 +00007593 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007594 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007595 newMax = 0;
7596 if( pOp->p3 ){
7597 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007598 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007599 }
7600 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007601 break;
7602}
7603#endif
7604
drh920cf592019-10-30 16:29:02 +00007605/* Opcode: Function P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00007606** Synopsis: r[P3]=func(r[P2@NP])
drh3e34eab2017-07-19 19:48:40 +00007607**
7608** Invoke a user function (P4 is a pointer to an sqlite3_context object that
drh920cf592019-10-30 16:29:02 +00007609** contains a pointer to the function to be run) with arguments taken
7610** from register P2 and successors. The number of arguments is in
7611** the sqlite3_context object that P4 points to.
7612** The result of the function is stored
drh3e34eab2017-07-19 19:48:40 +00007613** in register P3. Register P3 must not be one of the function inputs.
7614**
7615** P1 is a 32-bit bitmask indicating whether or not each argument to the
7616** function was determined to be constant at compile time. If the first
7617** argument was constant then bit 0 of P1 is set. This is used to determine
7618** whether meta data associated with a user function argument using the
7619** sqlite3_set_auxdata() API may be safely retained until the next
7620** invocation of this opcode.
7621**
drh920cf592019-10-30 16:29:02 +00007622** See also: AggStep, AggFinal, PureFunc
drh3e34eab2017-07-19 19:48:40 +00007623*/
drh920cf592019-10-30 16:29:02 +00007624/* Opcode: PureFunc P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00007625** Synopsis: r[P3]=func(r[P2@NP])
drh920cf592019-10-30 16:29:02 +00007626**
7627** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7628** contains a pointer to the function to be run) with arguments taken
7629** from register P2 and successors. The number of arguments is in
7630** the sqlite3_context object that P4 points to.
7631** The result of the function is stored
7632** in register P3. Register P3 must not be one of the function inputs.
7633**
7634** P1 is a 32-bit bitmask indicating whether or not each argument to the
7635** function was determined to be constant at compile time. If the first
7636** argument was constant then bit 0 of P1 is set. This is used to determine
7637** whether meta data associated with a user function argument using the
7638** sqlite3_set_auxdata() API may be safely retained until the next
7639** invocation of this opcode.
7640**
7641** This opcode works exactly like OP_Function. The only difference is in
7642** its name. This opcode is used in places where the function must be
7643** purely non-deterministic. Some built-in date/time functions can be
7644** either determinitic of non-deterministic, depending on their arguments.
7645** When those function are used in a non-deterministic way, they will check
7646** to see if they were called using OP_PureFunc instead of OP_Function, and
7647** if they were, they throw an error.
7648**
7649** See also: AggStep, AggFinal, Function
7650*/
mistachkin758784d2018-07-25 15:12:29 +00007651case OP_PureFunc: /* group */
7652case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007653 int i;
7654 sqlite3_context *pCtx;
7655
7656 assert( pOp->p4type==P4_FUNCCTX );
7657 pCtx = pOp->p4.pCtx;
7658
7659 /* If this function is inside of a trigger, the register array in aMem[]
7660 ** might change from one evaluation to the next. The next block of code
7661 ** checks to see if the register array has changed, and if so it
7662 ** reinitializes the relavant parts of the sqlite3_context object */
7663 pOut = &aMem[pOp->p3];
7664 if( pCtx->pOut != pOut ){
drh920cf592019-10-30 16:29:02 +00007665 pCtx->pVdbe = p;
drh3e34eab2017-07-19 19:48:40 +00007666 pCtx->pOut = pOut;
7667 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7668 }
drh920cf592019-10-30 16:29:02 +00007669 assert( pCtx->pVdbe==p );
drh3e34eab2017-07-19 19:48:40 +00007670
7671 memAboutToChange(p, pOut);
7672#ifdef SQLITE_DEBUG
7673 for(i=0; i<pCtx->argc; i++){
7674 assert( memIsValid(pCtx->argv[i]) );
7675 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7676 }
7677#endif
7678 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007679 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007680 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7681
7682 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007683 if( pCtx->isError ){
7684 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007685 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7686 rc = pCtx->isError;
7687 }
7688 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007689 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007690 if( rc ) goto abort_due_to_error;
7691 }
7692
7693 /* Copy the result of the function into register P3 */
7694 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7695 sqlite3VdbeChangeEncoding(pOut, encoding);
7696 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7697 }
7698
7699 REGISTER_TRACE(pOp->p3, pOut);
7700 UPDATE_MAX_BLOBSIZE(pOut);
7701 break;
7702}
7703
drhf259df52017-12-27 20:38:35 +00007704/* Opcode: Trace P1 P2 * P4 *
7705**
7706** Write P4 on the statement trace output if statement tracing is
7707** enabled.
7708**
7709** Operand P1 must be 0x7fffffff and P2 must positive.
7710*/
drh74588ce2017-09-13 00:13:05 +00007711/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007712** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007713**
7714** Programs contain a single instance of this opcode as the very first
7715** opcode.
drh949f9cd2008-01-12 21:35:57 +00007716**
7717** If tracing is enabled (by the sqlite3_trace()) interface, then
7718** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007719** Or if P4 is blank, use the string returned by sqlite3_sql().
7720**
7721** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007722**
7723** Increment the value of P1 so that OP_Once opcodes will jump the
7724** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007725**
7726** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7727** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007728*/
drhf259df52017-12-27 20:38:35 +00007729case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007730case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007731 int i;
drhb9f47992018-01-24 12:14:43 +00007732#ifndef SQLITE_OMIT_TRACE
7733 char *zTrace;
7734#endif
drh5fe63bf2016-07-25 02:42:22 +00007735
7736 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7737 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7738 **
7739 ** This assert() provides evidence for:
7740 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7741 ** would have been returned by the legacy sqlite3_trace() interface by
7742 ** using the X argument when X begins with "--" and invoking
7743 ** sqlite3_expanded_sql(P) otherwise.
7744 */
7745 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007746
7747 /* OP_Init is always instruction 0 */
7748 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007749
drhaceb31b2014-02-08 01:40:27 +00007750#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007751 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007752 && !p->doingRerun
7753 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7754 ){
drh3d2a5292016-07-13 22:55:01 +00007755#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007756 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7757 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007758 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007759 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007760 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007761 }else
drh3d2a5292016-07-13 22:55:01 +00007762#endif
drh7adbcff2017-03-20 15:29:28 +00007763 if( db->nVdbeExec>1 ){
7764 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7765 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7766 sqlite3DbFree(db, z);
7767 }else{
drhbd441f72016-07-25 02:31:48 +00007768 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007769 }
drh949f9cd2008-01-12 21:35:57 +00007770 }
drh8f8b2312013-10-18 20:03:43 +00007771#ifdef SQLITE_USE_FCNTL_TRACE
7772 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7773 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007774 int j;
7775 for(j=0; j<db->nDb; j++){
7776 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7777 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007778 }
7779 }
7780#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007781#ifdef SQLITE_DEBUG
7782 if( (db->flags & SQLITE_SqlTrace)!=0
7783 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7784 ){
7785 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7786 }
7787#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007788#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007789 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007790 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007791 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007792 for(i=1; i<p->nOp; i++){
7793 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7794 }
7795 pOp->p1 = 0;
7796 }
7797 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007798 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007799 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007800}
drh949f9cd2008-01-12 21:35:57 +00007801
drh28935362013-12-07 20:39:19 +00007802#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007803/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007804**
7805** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007806** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7807** to values currently held in registers. TK_COLUMN terms in the P4
7808** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007809*/
7810case OP_CursorHint: {
7811 VdbeCursor *pC;
7812
7813 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7814 assert( pOp->p4type==P4_EXPR );
7815 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007816 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007817 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007818 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7819 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007820 }
drh28935362013-12-07 20:39:19 +00007821 break;
7822}
7823#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007824
drh4031baf2018-05-28 17:31:20 +00007825#ifdef SQLITE_DEBUG
7826/* Opcode: Abortable * * * * *
7827**
7828** Verify that an Abort can happen. Assert if an Abort at this point
7829** might cause database corruption. This opcode only appears in debugging
7830** builds.
7831**
7832** An Abort is safe if either there have been no writes, or if there is
7833** an active statement journal.
7834*/
7835case OP_Abortable: {
7836 sqlite3VdbeAssertAbortable(p);
7837 break;
7838}
7839#endif
7840
drh13d79502019-12-23 02:18:49 +00007841#ifdef SQLITE_DEBUG
drh3aef2fb2020-01-02 17:46:02 +00007842/* Opcode: ReleaseReg P1 P2 P3 * P5
drh13d79502019-12-23 02:18:49 +00007843** Synopsis: release r[P1@P2] mask P3
7844**
7845** Release registers from service. Any content that was in the
7846** the registers is unreliable after this opcode completes.
7847**
7848** The registers released will be the P2 registers starting at P1,
7849** except if bit ii of P3 set, then do not release register P1+ii.
7850** In other words, P3 is a mask of registers to preserve.
7851**
7852** Releasing a register clears the Mem.pScopyFrom pointer. That means
7853** that if the content of the released register was set using OP_SCopy,
7854** a change to the value of the source register for the OP_SCopy will no longer
7855** generate an assertion fault in sqlite3VdbeMemAboutToChange().
7856**
drh3aef2fb2020-01-02 17:46:02 +00007857** If P5 is set, then all released registers have their type set
7858** to MEM_Undefined so that any subsequent attempt to read the released
drh13d79502019-12-23 02:18:49 +00007859** register (before it is reinitialized) will generate an assertion fault.
drh3aef2fb2020-01-02 17:46:02 +00007860**
7861** P5 ought to be set on every call to this opcode.
7862** However, there are places in the code generator will release registers
drh13d79502019-12-23 02:18:49 +00007863** before their are used, under the (valid) assumption that the registers
7864** will not be reallocated for some other purpose before they are used and
7865** hence are safe to release.
7866**
7867** This opcode is only available in testing and debugging builds. It is
7868** not generated for release builds. The purpose of this opcode is to help
7869** validate the generated bytecode. This opcode does not actually contribute
7870** to computing an answer.
7871*/
7872case OP_ReleaseReg: {
7873 Mem *pMem;
7874 int i;
7875 u32 constMask;
7876 assert( pOp->p1>0 );
7877 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
7878 pMem = &aMem[pOp->p1];
7879 constMask = pOp->p3;
7880 for(i=0; i<pOp->p2; i++, pMem++){
drh7edce5e2019-12-23 13:24:34 +00007881 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
drh13d79502019-12-23 02:18:49 +00007882 pMem->pScopyFrom = 0;
drh3aef2fb2020-01-02 17:46:02 +00007883 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
drh13d79502019-12-23 02:18:49 +00007884 }
7885 }
7886 break;
7887}
7888#endif
7889
drh91fd4d42008-01-19 20:11:25 +00007890/* Opcode: Noop * * * * *
7891**
7892** Do nothing. This instruction is often useful as a jump
7893** destination.
drh5e00f6c2001-09-13 13:46:56 +00007894*/
drh91fd4d42008-01-19 20:11:25 +00007895/*
7896** The magic Explain opcode are only inserted when explain==2 (which
7897** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7898** This opcode records information from the optimizer. It is the
7899** the same as a no-op. This opcodesnever appears in a real VM program.
7900*/
drh4031baf2018-05-28 17:31:20 +00007901default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007902 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007903
drh5e00f6c2001-09-13 13:46:56 +00007904 break;
7905}
7906
7907/*****************************************************************************
7908** The cases of the switch statement above this line should all be indented
7909** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7910** readability. From this point on down, the normal indentation rules are
7911** restored.
7912*****************************************************************************/
7913 }
drh6e142f52000-06-08 13:36:40 +00007914
drh7b396862003-01-01 23:06:20 +00007915#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007916 {
drh35043cc2018-02-12 20:27:34 +00007917 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007918 if( endTime>start ) pOrigOp->cycles += endTime - start;
7919 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007920 }
drh7b396862003-01-01 23:06:20 +00007921#endif
7922
drh6e142f52000-06-08 13:36:40 +00007923 /* The following code adds nothing to the actual functionality
7924 ** of the program. It is only here for testing and debugging.
7925 ** On the other hand, it does burn CPU cycles every time through
7926 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7927 */
7928#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007929 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007930
drhcf1023c2007-05-08 20:59:49 +00007931#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007932 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007933 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007934 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007935 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007936 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007937 }
drh7cc84c22016-04-11 13:36:42 +00007938 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007939 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007940 }
drh17aceeb2020-01-04 19:12:13 +00007941 if( opProperty==0xff ){
7942 /* Never happens. This code exists to avoid a harmless linkage
7943 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
7944 ** used. */
7945 sqlite3VdbeRegisterDump(p);
7946 }
drh75897232000-05-29 14:26:00 +00007947 }
danielk1977b5402fb2005-01-12 07:15:04 +00007948#endif /* SQLITE_DEBUG */
7949#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007950 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007951
drha05a7222008-01-19 03:35:58 +00007952 /* If we reach this point, it means that execution is finished with
7953 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007954 */
drh9467abf2016-02-17 18:44:11 +00007955abort_due_to_error:
7956 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007957 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007958 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7959 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7960 }
drha05a7222008-01-19 03:35:58 +00007961 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007962 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007963 testcase( sqlite3GlobalConfig.xLog!=0 );
7964 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007965 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007966 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007967 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007968 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007969 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007970 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007971 }
drh900b31e2007-08-28 02:27:51 +00007972
7973 /* This is the only way out of this procedure. We have to
7974 ** release the mutexes on btrees that were acquired at the
7975 ** top. */
7976vdbe_return:
drhc332e042019-02-12 21:04:33 +00007977#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00007978 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
7979 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00007980 if( db->xProgress(db->pProgressArg) ){
7981 nProgressLimit = 0xffffffff;
7982 rc = SQLITE_INTERRUPT;
7983 goto abort_due_to_error;
7984 }
7985 }
7986#endif
drh9b47ee32013-08-20 03:13:51 +00007987 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007988 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007989 assert( rc!=SQLITE_OK || nExtraDelete==0
7990 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7991 );
drhb86ccfb2003-01-28 23:13:10 +00007992 return rc;
7993
drh023ae032007-05-08 12:12:16 +00007994 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7995 ** is encountered.
7996 */
7997too_big:
drh22c17b82015-05-15 04:13:15 +00007998 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007999 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00008000 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00008001
drh98640a32007-06-07 19:08:32 +00008002 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00008003 */
8004no_mem:
drh4a642b62016-02-05 01:55:27 +00008005 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00008006 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00008007 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00008008 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008009
danielk19776f8a5032004-05-10 10:34:51 +00008010 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00008011 ** flag.
8012 */
8013abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00008014 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00008015 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00008016 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00008017 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00008018 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008019}