blob: 7c8d2a4ef7726f9b2a33fd849e2617ec186d1063 [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
120/*
drh5655c542014-02-19 19:14:34 +0000121** Invoke the VDBE coverage callback, if that callback is defined. This
122** feature is used for test suite validation only and does not appear an
123** production builds.
124**
drhc9065332019-04-01 14:01:21 +0000125** M is the type of branch. I is the direction taken for this instance of
126** the branch.
127**
128** M: 2 - two-way branch (I=0: fall-thru 1: jump )
129** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
130** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
131**
132** In other words, if M is 2, then I is either 0 (for fall-through) or
133** 1 (for when the branch is taken). If M is 3, the I is 0 for an
134** ordinary fall-through, I is 1 if the branch was taken, and I is 2
135** if the result of comparison is NULL. For M=3, I=2 the jump may or
136** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
137** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
138** depending on if the operands are less than, equal, or greater than.
drh4336b0e2014-08-05 00:53:51 +0000139**
140** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000141** generated the VDBE instruction combined with flag bits. The source
142** code line number is in the lower 24 bits of iSrcLine and the upper
143** 8 bytes are flags. The lower three bits of the flags indicate
144** values for I that should never occur. For example, if the branch is
145** always taken, the flags should be 0x05 since the fall-through and
146** alternate branch are never taken. If a branch is never taken then
147** flags should be 0x06 since only the fall-through approach is allowed.
148**
drhc9065332019-04-01 14:01:21 +0000149** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
drh7083a482018-07-10 16:04:04 +0000150** interested in equal or not-equal. In other words, I==0 and I==2
drhc9065332019-04-01 14:01:21 +0000151** should be treated as equivalent
drh7083a482018-07-10 16:04:04 +0000152**
153** Since only a line number is retained, not the filename, this macro
154** only works for amalgamation builds. But that is ok, since these macros
155** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000156*/
157#if !defined(SQLITE_VDBE_COVERAGE)
158# define VdbeBranchTaken(I,M)
159#else
drh5655c542014-02-19 19:14:34 +0000160# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000161 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
162 u8 mNever;
163 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
164 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
165 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
166 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
167 I = 1<<I;
168 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
169 ** the flags indicate directions that the branch can never go. If
170 ** a branch really does go in one of those directions, assert right
171 ** away. */
172 mNever = iSrcLine >> 24;
173 assert( (I & mNever)==0 );
174 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
drhc9065332019-04-01 14:01:21 +0000175 /* Invoke the branch coverage callback with three arguments:
176 ** iSrcLine - the line number of the VdbeCoverage() macro, with
177 ** flags removed.
178 ** I - Mask of bits 0x07 indicating which cases are are
179 ** fulfilled by this instance of the jump. 0x01 means
180 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
181 ** impossible cases (ex: if the comparison is never NULL)
182 ** are filled in automatically so that the coverage
183 ** measurement logic does not flag those impossible cases
184 ** as missed coverage.
185 ** M - Type of jump. Same as M argument above
186 */
drh7083a482018-07-10 16:04:04 +0000187 I |= mNever;
188 if( M==2 ) I |= 0x04;
189 if( M==4 ){
190 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000191 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000192 }
drh7083a482018-07-10 16:04:04 +0000193 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
194 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000195 }
drh688852a2014-02-17 22:40:43 +0000196#endif
197
198/*
danielk1977bd7e4602004-05-24 07:34:48 +0000199** An ephemeral string value (signified by the MEM_Ephem flag) contains
200** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000201** is responsible for deallocating that string. Because the register
202** does not control the string, it might be deleted without the register
203** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000204**
205** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000206** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000207** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000208*/
drhb21c8cd2007-08-21 19:33:56 +0000209#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000210 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000211 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000212
dan689ab892011-08-12 15:02:00 +0000213/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000214#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000215
216/*
drhdfe88ec2008-11-03 20:55:06 +0000217** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000218** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000219*/
drhdfe88ec2008-11-03 20:55:06 +0000220static VdbeCursor *allocateCursor(
221 Vdbe *p, /* The virtual machine */
222 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000223 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000224 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000225 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000226){
227 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000228 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000229 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000230 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000231 **
232 ** * Sometimes cursor numbers are used for a couple of different
233 ** purposes in a vdbe program. The different uses might require
234 ** different sized allocations. Memory cells provide growable
235 ** allocations.
236 **
237 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
238 ** be freed lazily via the sqlite3_release_memory() API. This
239 ** minimizes the number of malloc calls made by the system.
240 **
drh3cdce922016-03-21 00:30:40 +0000241 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000242 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
243 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000244 */
drh9f6168b2016-03-19 23:32:58 +0000245 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000246
danielk19775f096132008-03-28 15:44:09 +0000247 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000248 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000249 nByte =
drh5cc10232013-11-21 01:04:02 +0000250 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000251 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000252
drh9f6168b2016-03-19 23:32:58 +0000253 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000254 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
dan97c8cb32019-01-01 18:00:17 +0000255 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
256 ** is clear. Otherwise, if this is an ephemeral cursor created by
257 ** OP_OpenDup, the cursor will not be closed and will still be part
258 ** of a BtShared.pCursor list. */
dana5129722019-05-03 18:50:24 +0000259 if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
danielk1977be718892006-06-23 08:05:19 +0000260 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000261 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000262 }
drh322f2852014-09-19 00:43:39 +0000263 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000264 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000265 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000266 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000267 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000268 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000269 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000270 if( eCurType==CURTYPE_BTREE ){
271 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000272 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000273 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000274 }
danielk197794eb6a12005-12-15 15:22:08 +0000275 }
drh4774b132004-06-12 20:12:51 +0000276 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000277}
278
danielk19773d1bfea2004-05-14 11:00:53 +0000279/*
drh8a3884e2019-05-29 21:18:27 +0000280** The string in pRec is known to look like an integer and to have a
281** floating point value of rValue. Return true and set *piValue to the
282** integer value if the string is in range to be an integer. Otherwise,
283** return false.
284*/
285static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
286 i64 iValue = (double)rValue;
287 if( sqlite3RealSameAsInt(rValue,iValue) ){
drhc285ded2019-06-10 18:33:16 +0000288 *piValue = iValue;
289 return 1;
drh8a3884e2019-05-29 21:18:27 +0000290 }
291 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
292}
293
294/*
drh29d72102006-02-09 22:13:41 +0000295** Try to convert a value into a numeric representation if we can
296** do so without loss of information. In other words, if the string
297** looks like a number, convert it into a number. If it does not
298** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000299**
300** If the bTryForInt flag is true, then extra effort is made to give
301** an integer representation. Strings that look like floating point
302** values but which have no fractional component (example: '48.00')
303** will have a MEM_Int representation when bTryForInt is true.
304**
305** If bTryForInt is false, then if the input string contains a decimal
306** point or exponential notation, the result is only MEM_Real, even
307** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000308*/
drhbd9507c2014-08-23 17:21:37 +0000309static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000310 double rValue;
drh975b4c62014-07-26 16:47:23 +0000311 u8 enc = pRec->enc;
drh8a3884e2019-05-29 21:18:27 +0000312 int rc;
drh169f0772019-05-02 21:36:26 +0000313 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
drh8a3884e2019-05-29 21:18:27 +0000314 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
drh9a278222019-06-07 22:26:08 +0000315 if( rc<=0 ) return;
drh8a3884e2019-05-29 21:18:27 +0000316 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
drh975b4c62014-07-26 16:47:23 +0000317 pRec->flags |= MEM_Int;
318 }else{
drh74eaba42014-09-18 17:52:15 +0000319 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000320 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000321 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000322 }
drh06b3bd52018-02-01 01:13:33 +0000323 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
324 ** string representation after computing a numeric equivalent, because the
325 ** string representation might not be the canonical representation for the
326 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
327 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000328}
329
330/*
drh8a512562005-11-14 22:29:05 +0000331** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000332**
drh8a512562005-11-14 22:29:05 +0000333** SQLITE_AFF_INTEGER:
334** SQLITE_AFF_REAL:
335** SQLITE_AFF_NUMERIC:
336** Try to convert pRec to an integer representation or a
337** floating-point representation if an integer representation
338** is not possible. Note that the integer representation is
339** always preferred, even if the affinity is REAL, because
340** an integer representation is more space efficient on disk.
341**
342** SQLITE_AFF_TEXT:
343** Convert pRec to a text representation.
344**
drh05883a32015-06-02 15:32:08 +0000345** SQLITE_AFF_BLOB:
drh96fb16e2019-08-06 14:37:24 +0000346** SQLITE_AFF_NONE:
drh8a512562005-11-14 22:29:05 +0000347** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000348*/
drh17435752007-08-16 04:30:38 +0000349static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000350 Mem *pRec, /* The value to apply affinity to */
351 char affinity, /* The affinity to be applied */
352 u8 enc /* Use this text encoding */
353){
drh7ea31cc2014-09-18 14:36:00 +0000354 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000355 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
356 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000357 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000358 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000359 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000360 }else{
361 sqlite3VdbeIntegerAffinity(pRec);
362 }
drh17c40292004-07-21 02:53:29 +0000363 }
drh7ea31cc2014-09-18 14:36:00 +0000364 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000365 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000366 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000367 ** representation. It would be harmless to repeat the conversion if
368 ** there is already a string rep, but it is pointless to waste those
369 ** CPU cycles. */
370 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
drh169f0772019-05-02 21:36:26 +0000371 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
drh3242c692019-05-04 01:29:13 +0000372 testcase( pRec->flags & MEM_Int );
373 testcase( pRec->flags & MEM_Real );
374 testcase( pRec->flags & MEM_IntReal );
drha3fa1402016-04-29 02:55:05 +0000375 sqlite3VdbeMemStringify(pRec, enc, 1);
376 }
danielk19773d1bfea2004-05-14 11:00:53 +0000377 }
drh169f0772019-05-02 21:36:26 +0000378 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
danielk19773d1bfea2004-05-14 11:00:53 +0000379 }
380}
381
danielk1977aee18ef2005-03-09 12:26:50 +0000382/*
drh29d72102006-02-09 22:13:41 +0000383** Try to convert the type of a function argument or a result column
384** into a numeric representation. Use either INTEGER or REAL whichever
385** is appropriate. But only do the conversion if it is possible without
386** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000387*/
388int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000389 int eType = sqlite3_value_type(pVal);
390 if( eType==SQLITE_TEXT ){
391 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000392 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000393 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000394 }
drh1b27b8c2014-02-10 03:21:57 +0000395 return eType;
drh29d72102006-02-09 22:13:41 +0000396}
397
398/*
danielk1977aee18ef2005-03-09 12:26:50 +0000399** Exported version of applyAffinity(). This one works on sqlite3_value*,
400** not the internal Mem* type.
401*/
danielk19771e536952007-08-16 10:09:01 +0000402void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000403 sqlite3_value *pVal,
404 u8 affinity,
405 u8 enc
406){
drhb21c8cd2007-08-21 19:33:56 +0000407 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000408}
409
drh3d1d90a2014-03-24 15:00:15 +0000410/*
drhf1a89ed2014-08-23 17:41:15 +0000411** pMem currently only holds a string type (or maybe a BLOB that we can
412** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000413** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000414** accordingly.
415*/
416static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
drh9a278222019-06-07 22:26:08 +0000417 int rc;
418 sqlite3_int64 ix;
drh169f0772019-05-02 21:36:26 +0000419 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
drhf1a89ed2014-08-23 17:41:15 +0000420 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh0814acd2019-01-25 20:09:04 +0000421 ExpandBlob(pMem);
drh9a278222019-06-07 22:26:08 +0000422 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
423 if( rc<=0 ){
424 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
425 pMem->u.i = ix;
426 return MEM_Int;
427 }else{
428 return MEM_Real;
429 }
430 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
431 pMem->u.i = ix;
drhf1a89ed2014-08-23 17:41:15 +0000432 return MEM_Int;
433 }
434 return MEM_Real;
435}
436
437/*
drh3d1d90a2014-03-24 15:00:15 +0000438** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
439** none.
440**
441** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000442** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000443*/
444static u16 numericType(Mem *pMem){
drh169f0772019-05-02 21:36:26 +0000445 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +0000446 testcase( pMem->flags & MEM_Int );
447 testcase( pMem->flags & MEM_Real );
448 testcase( pMem->flags & MEM_IntReal );
drh169f0772019-05-02 21:36:26 +0000449 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
drh3d1d90a2014-03-24 15:00:15 +0000450 }
451 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drh3242c692019-05-04 01:29:13 +0000452 testcase( pMem->flags & MEM_Str );
453 testcase( pMem->flags & MEM_Blob );
drhf1a89ed2014-08-23 17:41:15 +0000454 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000455 }
456 return 0;
457}
458
danielk1977b5402fb2005-01-12 07:15:04 +0000459#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000460/*
danielk1977ca6b2912004-05-21 10:49:47 +0000461** Write a nice string representation of the contents of cell pMem
462** into buffer zBuf, length nBuf.
463*/
drh74161702006-02-24 02:53:49 +0000464void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000465 char *zCsr = zBuf;
466 int f = pMem->flags;
467
drh57196282004-10-06 15:41:16 +0000468 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000469
danielk1977ca6b2912004-05-21 10:49:47 +0000470 if( f&MEM_Blob ){
471 int i;
472 char c;
473 if( f & MEM_Dyn ){
474 c = 'z';
475 assert( (f & (MEM_Static|MEM_Ephem))==0 );
476 }else if( f & MEM_Static ){
477 c = 't';
478 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
479 }else if( f & MEM_Ephem ){
480 c = 'e';
481 assert( (f & (MEM_Static|MEM_Dyn))==0 );
482 }else{
483 c = 's';
484 }
drh85c2dc02017-03-16 13:30:58 +0000485 *(zCsr++) = c;
drh70738712019-09-16 20:16:17 +0000486 *(zCsr++) = 'x';
drh5bb3eb92007-05-04 13:15:55 +0000487 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000488 zCsr += sqlite3Strlen30(zCsr);
drhefb5f9a2019-08-30 21:52:13 +0000489 for(i=0; i<25 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000490 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000491 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000492 }
drhefb5f9a2019-08-30 21:52:13 +0000493 *zCsr++ = '|';
494 for(i=0; i<25 && i<pMem->n; i++){
danielk1977ca6b2912004-05-21 10:49:47 +0000495 char z = pMem->z[i];
496 if( z<32 || z>126 ) *zCsr++ = '.';
497 else *zCsr++ = z;
498 }
drh85c2dc02017-03-16 13:30:58 +0000499 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000500 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000501 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000502 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000503 }
danielk1977b1bc9532004-05-22 03:05:33 +0000504 *zCsr = '\0';
505 }else if( f & MEM_Str ){
506 int j, k;
507 zBuf[0] = ' ';
508 if( f & MEM_Dyn ){
509 zBuf[1] = 'z';
510 assert( (f & (MEM_Static|MEM_Ephem))==0 );
511 }else if( f & MEM_Static ){
512 zBuf[1] = 't';
513 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
514 }else if( f & MEM_Ephem ){
515 zBuf[1] = 'e';
516 assert( (f & (MEM_Static|MEM_Dyn))==0 );
517 }else{
518 zBuf[1] = 's';
519 }
520 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000521 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000522 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000523 zBuf[k++] = '[';
drhefb5f9a2019-08-30 21:52:13 +0000524 for(j=0; j<25 && j<pMem->n; j++){
danielk1977b1bc9532004-05-22 03:05:33 +0000525 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000526 if( c>=0x20 && c<0x7f ){
527 zBuf[k++] = c;
528 }else{
529 zBuf[k++] = '.';
530 }
531 }
532 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000533 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000534 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000535 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000536 }
danielk1977ca6b2912004-05-21 10:49:47 +0000537}
538#endif
539
drh5b6afba2008-01-05 16:29:28 +0000540#ifdef SQLITE_DEBUG
541/*
542** Print the value of a register for tracing purposes:
543*/
drh84e55a82013-11-13 17:58:23 +0000544static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000545 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000546 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000547 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000548 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000549 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000550 printf(" si:%lld", p->u.i);
drh169f0772019-05-02 21:36:26 +0000551 }else if( (p->flags & (MEM_IntReal))!=0 ){
drh83a1daf2019-05-01 18:59:33 +0000552 printf(" ir:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000553 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000554 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000555#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000556 }else if( p->flags & MEM_Real ){
drhd1c472d2019-10-03 14:51:59 +0000557 printf(" r:%.17g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000558#endif
drh9d67afc2018-08-29 20:24:03 +0000559 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000560 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000561 }else{
562 char zBuf[200];
563 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000564 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000565 }
dan5b6c8e42016-01-30 15:46:03 +0000566 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000567}
drh84e55a82013-11-13 17:58:23 +0000568static void registerTrace(int iReg, Mem *p){
569 printf("REG[%d] = ", iReg);
570 memTracePrint(p);
571 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000572 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000573}
574#endif
575
576#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000577# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000578#else
579# define REGISTER_TRACE(R,M)
580#endif
581
danielk197784ac9d02004-05-18 09:58:06 +0000582
drh7b396862003-01-01 23:06:20 +0000583#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000584
585/*
586** hwtime.h contains inline assembler code for implementing
587** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000588*/
shane9bcbdad2008-05-29 20:22:37 +0000589#include "hwtime.h"
590
drh7b396862003-01-01 23:06:20 +0000591#endif
592
danielk1977fd7f0452008-12-17 17:30:26 +0000593#ifndef NDEBUG
594/*
595** This function is only called from within an assert() expression. It
596** checks that the sqlite3.nTransaction variable is correctly set to
597** the number of non-transaction savepoints currently in the
598** linked list starting at sqlite3.pSavepoint.
599**
600** Usage:
601**
602** assert( checkSavepointCount(db) );
603*/
604static int checkSavepointCount(sqlite3 *db){
605 int n = 0;
606 Savepoint *p;
607 for(p=db->pSavepoint; p; p=p->pNext) n++;
608 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
609 return 1;
610}
611#endif
612
drh27a348c2015-04-13 19:14:06 +0000613/*
614** Return the register of pOp->p2 after first preparing it to be
615** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000616*/
617static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
618 sqlite3VdbeMemSetNull(pOut);
619 pOut->flags = MEM_Int;
620 return pOut;
621}
drh27a348c2015-04-13 19:14:06 +0000622static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
623 Mem *pOut;
624 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000625 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000626 pOut = &p->aMem[pOp->p2];
627 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000628 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000629 return out2PrereleaseWithClear(pOut);
630 }else{
631 pOut->flags = MEM_Int;
632 return pOut;
633 }
drh27a348c2015-04-13 19:14:06 +0000634}
635
drhb9755982010-07-24 16:34:37 +0000636
637/*
drh0fd61352014-02-07 02:29:45 +0000638** Execute as much of a VDBE program as we can.
639** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000640*/
danielk19774adee202004-05-08 08:23:19 +0000641int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000642 Vdbe *p /* The VDBE */
643){
drhbbe879d2009-11-14 18:04:35 +0000644 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000645 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000646#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
647 Op *pOrigOp; /* Value of pOp at the top of the loop */
648#endif
drhb89aeb62016-01-27 15:49:32 +0000649#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000650 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000651#endif
drhb86ccfb2003-01-28 23:13:10 +0000652 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000653 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000654 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000655 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000656 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000657 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000658#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000659 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000660#endif
drha6c2ed92009-11-14 23:22:23 +0000661 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000662 Mem *pIn1 = 0; /* 1st input operand */
663 Mem *pIn2 = 0; /* 2nd input operand */
664 Mem *pIn3 = 0; /* 3rd input operand */
665 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000666#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000667 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000668#endif
drh856c1032009-06-02 15:21:42 +0000669 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000670
drhca48c902008-01-18 14:08:24 +0000671 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000672 sqlite3VdbeEnter(p);
drh82642f82019-02-12 22:58:32 +0000673#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
674 if( db->xProgress ){
675 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
676 assert( 0 < db->nProgressOps );
677 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
678 }else{
679 nProgressLimit = 0xffffffff;
680 }
681#endif
danielk19772e588c72005-12-09 14:25:08 +0000682 if( p->rc==SQLITE_NOMEM ){
683 /* This happens if a malloc() inside a call to sqlite3_column_text() or
684 ** sqlite3_column_text16() failed. */
685 goto no_mem;
686 }
drhcbd8db32015-08-20 17:18:32 +0000687 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000688 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000689 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000690 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000691 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000692 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000693 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000694 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000695#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000696 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000697 if( p->pc==0
698 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
699 ){
drh3c23a882007-01-09 14:01:13 +0000700 int i;
drh84e55a82013-11-13 17:58:23 +0000701 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000702 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000703 if( p->db->flags & SQLITE_VdbeListing ){
704 printf("VDBE Program Listing:\n");
705 for(i=0; i<p->nOp; i++){
706 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
707 }
drh3c23a882007-01-09 14:01:13 +0000708 }
drh84e55a82013-11-13 17:58:23 +0000709 if( p->db->flags & SQLITE_VdbeEQP ){
710 for(i=0; i<p->nOp; i++){
711 if( aOp[i].opcode==OP_Explain ){
712 if( once ) printf("VDBE Query Plan:\n");
713 printf("%s\n", aOp[i].p4.z);
714 once = 0;
715 }
716 }
717 }
718 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000719 }
danielk19772d1d86f2008-06-20 14:59:51 +0000720 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000721#endif
drh9467abf2016-02-17 18:44:11 +0000722 for(pOp=&aOp[p->pc]; 1; pOp++){
723 /* Errors are detected by individual opcodes, with an immediate
724 ** jumps to abort_due_to_error. */
725 assert( rc==SQLITE_OK );
726
drhf56fa462015-04-13 21:39:54 +0000727 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000728#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000729 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000730#endif
drhbf159fa2013-06-25 22:01:22 +0000731 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000732#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000733 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000734#endif
drh6e142f52000-06-08 13:36:40 +0000735
danielk19778b60e0f2005-01-12 09:10:39 +0000736 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000737 */
danielk19778b60e0f2005-01-12 09:10:39 +0000738#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000739 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000740 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000741 }
drh3f7d4e42004-07-24 14:35:58 +0000742#endif
743
drh6e142f52000-06-08 13:36:40 +0000744
drhf6038712004-02-08 18:07:34 +0000745 /* Check to see if we need to simulate an interrupt. This only happens
746 ** if we have a special test build.
747 */
748#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000749 if( sqlite3_interrupt_count>0 ){
750 sqlite3_interrupt_count--;
751 if( sqlite3_interrupt_count==0 ){
752 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000753 }
754 }
755#endif
756
drh3c657212009-11-17 23:59:58 +0000757 /* Sanity checking on other operands */
758#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000759 {
760 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
761 if( (opProperty & OPFLG_IN1)!=0 ){
762 assert( pOp->p1>0 );
763 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
764 assert( memIsValid(&aMem[pOp->p1]) );
765 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
766 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
767 }
768 if( (opProperty & OPFLG_IN2)!=0 ){
769 assert( pOp->p2>0 );
770 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
771 assert( memIsValid(&aMem[pOp->p2]) );
772 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
773 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
774 }
775 if( (opProperty & OPFLG_IN3)!=0 ){
776 assert( pOp->p3>0 );
777 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
778 assert( memIsValid(&aMem[pOp->p3]) );
779 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
780 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
781 }
782 if( (opProperty & OPFLG_OUT2)!=0 ){
783 assert( pOp->p2>0 );
784 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
785 memAboutToChange(p, &aMem[pOp->p2]);
786 }
787 if( (opProperty & OPFLG_OUT3)!=0 ){
788 assert( pOp->p3>0 );
789 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
790 memAboutToChange(p, &aMem[pOp->p3]);
791 }
drh3c657212009-11-17 23:59:58 +0000792 }
793#endif
drh6dc41482015-04-16 17:31:02 +0000794#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
795 pOrigOp = pOp;
796#endif
drh93952eb2009-11-13 19:43:43 +0000797
drh75897232000-05-29 14:26:00 +0000798 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000799
drh5e00f6c2001-09-13 13:46:56 +0000800/*****************************************************************************
801** What follows is a massive switch statement where each case implements a
802** separate instruction in the virtual machine. If we follow the usual
803** indentation conventions, each case should be indented by 6 spaces. But
804** that is a lot of wasted space on the left margin. So the code within
805** the switch statement will break with convention and be flush-left. Another
806** big comment (similar to this one) will mark the point in the code where
807** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000808**
809** The formatting of each case is important. The makefile for SQLite
810** generates two C files "opcodes.h" and "opcodes.c" by scanning this
811** file looking for lines that begin with "case OP_". The opcodes.h files
812** will be filled with #defines that give unique integer values to each
813** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000814** each string is the symbolic name for the corresponding opcode. If the
815** case statement is followed by a comment of the form "/# same as ... #/"
816** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000817**
drh9cbf3422008-01-17 16:22:13 +0000818** Other keywords in the comment that follows each case are used to
819** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000820** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000821** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000822**
drhac82fcf2002-09-08 17:23:41 +0000823** Documentation about VDBE opcodes is generated by scanning this file
824** for lines of that contain "Opcode:". That line and all subsequent
825** comment lines are used in the generation of the opcode.html documentation
826** file.
827**
828** SUMMARY:
829**
830** Formatting is important to scripts that scan this file.
831** Do not deviate from the formatting style currently in use.
832**
drh5e00f6c2001-09-13 13:46:56 +0000833*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000834
drh9cbf3422008-01-17 16:22:13 +0000835/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000836**
837** An unconditional jump to address P2.
838** The next instruction executed will be
839** the one at index P2 from the beginning of
840** the program.
drhfe705102014-03-06 13:38:37 +0000841**
842** The P1 parameter is not actually used by this opcode. However, it
843** is sometimes set to 1 instead of 0 as a hint to the command-line shell
844** that this Goto is the bottom of a loop and that the lines from P2 down
845** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000846*/
drh9cbf3422008-01-17 16:22:13 +0000847case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000848jump_to_p2_and_check_for_interrupt:
849 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000850
851 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000852 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000853 ** completion. Check to see if sqlite3_interrupt() has been called
854 ** or if the progress callback needs to be invoked.
855 **
856 ** This code uses unstructured "goto" statements and does not look clean.
857 ** But that is not due to sloppy coding habits. The code is written this
858 ** way for performance, to avoid having to run the interrupt and progress
859 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
860 ** faster according to "valgrind --tool=cachegrind" */
861check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000862 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000863#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
864 /* Call the progress callback if it is configured and the required number
865 ** of VDBE ops have been executed (either since this invocation of
866 ** sqlite3VdbeExec() or since last time the progress callback was called).
867 ** If the progress callback returns non-zero, exit the virtual machine with
868 ** a return code SQLITE_ABORT.
869 */
drhb1af9c62019-02-20 13:55:45 +0000870 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000871 assert( db->nProgressOps!=0 );
drhb1af9c62019-02-20 13:55:45 +0000872 nProgressLimit += db->nProgressOps;
drh400fcba2013-11-14 00:09:48 +0000873 if( db->xProgress(db->pProgressArg) ){
drhc332e042019-02-12 21:04:33 +0000874 nProgressLimit = 0xffffffff;
drh49afe3a2013-07-10 03:05:14 +0000875 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000876 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000877 }
drh49afe3a2013-07-10 03:05:14 +0000878 }
879#endif
880
drh5e00f6c2001-09-13 13:46:56 +0000881 break;
882}
drh75897232000-05-29 14:26:00 +0000883
drh2eb95372008-06-06 15:04:36 +0000884/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000885**
drh2eb95372008-06-06 15:04:36 +0000886** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000887** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000888*/
drhb8475df2011-12-09 16:21:19 +0000889case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000890 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000891 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000892 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000893 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000894 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000895 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000896 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000897
898 /* Most jump operations do a goto to this spot in order to update
899 ** the pOp pointer. */
900jump_to_p2:
901 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000902 break;
903}
904
drh2eb95372008-06-06 15:04:36 +0000905/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000906**
drh81cf13e2014-02-07 18:27:53 +0000907** Jump to the next instruction after the address in register P1. After
908** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000909*/
drh2eb95372008-06-06 15:04:36 +0000910case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000911 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000912 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000913 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000914 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000915 break;
916}
917
drhed71a832014-02-07 19:18:10 +0000918/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000919**
drh5dad9a32014-07-25 18:37:42 +0000920** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000921** located at address P3.
922**
drh5dad9a32014-07-25 18:37:42 +0000923** If P2!=0 then the coroutine implementation immediately follows
924** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000925** address P2.
drh5dad9a32014-07-25 18:37:42 +0000926**
927** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000928*/
929case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000930 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000931 assert( pOp->p2>=0 && pOp->p2<p->nOp );
932 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000933 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000934 assert( !VdbeMemDynamic(pOut) );
935 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000936 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000937 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000938 break;
939}
940
941/* Opcode: EndCoroutine P1 * * * *
942**
drhbc5cf382014-08-06 01:08:07 +0000943** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000944** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000945** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000946**
947** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000948*/
949case OP_EndCoroutine: { /* in1 */
950 VdbeOp *pCaller;
951 pIn1 = &aMem[pOp->p1];
952 assert( pIn1->flags==MEM_Int );
953 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
954 pCaller = &aOp[pIn1->u.i];
955 assert( pCaller->opcode==OP_Yield );
956 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000957 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000958 pIn1->flags = MEM_Undefined;
959 break;
960}
961
962/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000963**
drh5dad9a32014-07-25 18:37:42 +0000964** Swap the program counter with the value in register P1. This
965** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000966**
drh5dad9a32014-07-25 18:37:42 +0000967** If the coroutine that is launched by this instruction ends with
968** Yield or Return then continue to the next instruction. But if
969** the coroutine launched by this instruction ends with
970** EndCoroutine, then jump to P2 rather than continuing with the
971** next instruction.
972**
973** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000974*/
drh81cf13e2014-02-07 18:27:53 +0000975case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000976 int pcDest;
drh3c657212009-11-17 23:59:58 +0000977 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000978 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000979 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000980 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000981 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000982 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000983 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000984 break;
985}
986
drhf9c8ce32013-11-05 13:33:55 +0000987/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000988** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000989**
drhef8662b2011-06-20 21:47:58 +0000990** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000991** parameter P1, P2, and P4 as if this were a Halt instruction. If the
992** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000993** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000994*/
995case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000996 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +0000997#ifdef SQLITE_DEBUG
998 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
999#endif
drh5053a792009-02-20 03:02:23 +00001000 if( (pIn3->flags & MEM_Null)==0 ) break;
1001 /* Fall through into OP_Halt */
1002}
drhe00ee6e2008-06-20 15:24:01 +00001003
drhf9c8ce32013-11-05 13:33:55 +00001004/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001005**
drh3d4501e2008-12-04 20:40:10 +00001006** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +00001007** automatically.
drhb19a2bc2001-09-16 00:13:26 +00001008**
drh92f02c32004-09-02 14:57:08 +00001009** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1010** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1011** For errors, it can be some other value. If P1!=0 then P2 will determine
1012** whether or not to rollback the current transaction. Do not rollback
1013** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1014** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +00001015** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +00001016**
drh66a51672008-01-03 00:01:23 +00001017** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +00001018**
drhf9c8ce32013-11-05 13:33:55 +00001019** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1020**
1021** 0: (no change)
1022** 1: NOT NULL contraint failed: P4
1023** 2: UNIQUE constraint failed: P4
1024** 3: CHECK constraint failed: P4
1025** 4: FOREIGN KEY constraint failed: P4
1026**
1027** If P5 is not zero and P4 is NULL, then everything after the ":" is
1028** omitted.
1029**
drh9cfcf5d2002-01-29 18:41:24 +00001030** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001031** every program. So a jump past the last instruction of the program
1032** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001033*/
drh9cbf3422008-01-17 16:22:13 +00001034case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001035 VdbeFrame *pFrame;
1036 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001037
drhf56fa462015-04-13 21:39:54 +00001038 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +00001039#ifdef SQLITE_DEBUG
1040 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1041#endif
dan165921a2009-08-28 18:53:45 +00001042 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +00001043 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001044 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001045 p->pFrame = pFrame->pParent;
1046 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001047 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001048 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001049 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001050 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001051 ** currently being halted. If the p2 instruction of this OP_Halt
1052 ** instruction is set to OE_Ignore, then the sub-program is throwing
1053 ** an IGNORE exception. In this case jump to the address specified
1054 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001055 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001056 }
drhbbe879d2009-11-14 18:04:35 +00001057 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001058 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001059 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001060 break;
1061 }
drh92f02c32004-09-02 14:57:08 +00001062 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001063 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001064 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001065 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001066 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001067 if( pOp->p5 ){
1068 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1069 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001070 testcase( pOp->p5==1 );
1071 testcase( pOp->p5==2 );
1072 testcase( pOp->p5==3 );
1073 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001074 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1075 if( pOp->p4.z ){
1076 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1077 }
drhd9b7ec92013-11-06 14:05:21 +00001078 }else{
drh22c17b82015-05-15 04:13:15 +00001079 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001080 }
drh99f5de72016-04-30 02:59:15 +00001081 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001082 }
drh92f02c32004-09-02 14:57:08 +00001083 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001084 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001085 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001086 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001087 }else{
drhd91c1a12013-02-09 13:58:25 +00001088 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001089 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001090 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001091 }
drh900b31e2007-08-28 02:27:51 +00001092 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001093}
drhc61053b2000-06-04 12:58:36 +00001094
drh4c583122008-01-04 22:01:03 +00001095/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001096** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001097**
drh9cbf3422008-01-17 16:22:13 +00001098** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001099*/
drh27a348c2015-04-13 19:14:06 +00001100case OP_Integer: { /* out2 */
1101 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001102 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001103 break;
1104}
1105
drh4c583122008-01-04 22:01:03 +00001106/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001107** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001108**
drh66a51672008-01-03 00:01:23 +00001109** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001110** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001111*/
drh27a348c2015-04-13 19:14:06 +00001112case OP_Int64: { /* out2 */
1113 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001114 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001115 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001116 break;
1117}
drh4f26d6c2004-05-26 23:25:30 +00001118
drh13573c72010-01-12 17:04:07 +00001119#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001120/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001121** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001122**
drh4c583122008-01-04 22:01:03 +00001123** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001124** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001125*/
drh27a348c2015-04-13 19:14:06 +00001126case OP_Real: { /* same as TK_FLOAT, out2 */
1127 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001128 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001129 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001130 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001131 break;
1132}
drh13573c72010-01-12 17:04:07 +00001133#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001134
drh3c84ddf2008-01-09 02:15:38 +00001135/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001136** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001137**
drh66a51672008-01-03 00:01:23 +00001138** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001139** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001140** this transformation, the length of string P4 is computed and stored
1141** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001142*/
drh27a348c2015-04-13 19:14:06 +00001143case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001144 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001145 pOut = out2Prerelease(p, pOp);
drhea678832008-12-10 19:26:22 +00001146 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001147
1148#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001149 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001150 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001151 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001152 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001153 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001154 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001155 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001156 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001157 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001158 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001159 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001160 }
drh66a51672008-01-03 00:01:23 +00001161 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001162 pOp->p4.z = pOut->z;
1163 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001164 }
danielk197793758c82005-01-21 08:13:14 +00001165#endif
drhbb4957f2008-03-20 14:03:29 +00001166 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001167 goto too_big;
1168 }
drhec722c12019-09-17 21:28:54 +00001169 pOp->opcode = OP_String;
drh2f555112016-04-30 18:10:34 +00001170 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001171 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001172}
drhf4479502004-05-27 03:12:53 +00001173
drhf07cf6e2015-03-06 16:45:16 +00001174/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001175** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001176**
drh9cbf3422008-01-17 16:22:13 +00001177** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001178**
drh44aebff2016-05-02 10:25:42 +00001179** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001180** the datatype of the register P2 is converted to BLOB. The content is
1181** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001182** of a string, as if it had been CAST. In other words:
1183**
1184** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001185*/
drh27a348c2015-04-13 19:14:06 +00001186case OP_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);
drh4c583122008-01-04 22:01:03 +00001189 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1190 pOut->z = pOp->p4.z;
1191 pOut->n = pOp->p1;
1192 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001193 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001194#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001195 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001196 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001197 pIn3 = &aMem[pOp->p3];
1198 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001199 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001200 }
drh41d2e662015-12-01 21:23:07 +00001201#endif
danielk1977c572ef72004-05-27 09:28:41 +00001202 break;
1203}
1204
drh053a1282012-09-19 21:15:46 +00001205/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001206** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001207**
drhb8475df2011-12-09 16:21:19 +00001208** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001209** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001210** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001211** set to NULL.
1212**
1213** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1214** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1215** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001216*/
drh27a348c2015-04-13 19:14:06 +00001217case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001218 int cnt;
drh053a1282012-09-19 21:15:46 +00001219 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001220 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001221 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001222 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001223 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001224 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001225#ifdef SQLITE_DEBUG
1226 pOut->uTemp = 0;
1227#endif
drhb8475df2011-12-09 16:21:19 +00001228 while( cnt>0 ){
1229 pOut++;
1230 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001231 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001232 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001233 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001234 cnt--;
1235 }
drhf0863fe2005-06-12 21:35:51 +00001236 break;
1237}
1238
drh05a86c52014-02-16 01:55:49 +00001239/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001240** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001241**
1242** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1243** instruction, but do not free any string or blob memory associated with
1244** the register, so that if the value was a string or blob that was
1245** previously copied using OP_SCopy, the copies will continue to be valid.
1246*/
1247case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001248 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001249 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001250 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001251 break;
1252}
drhf0863fe2005-06-12 21:35:51 +00001253
drha5750cf2014-02-07 13:20:31 +00001254/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001255** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001256**
drh9de221d2008-01-05 06:51:30 +00001257** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001258** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001259*/
drh27a348c2015-04-13 19:14:06 +00001260case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001261 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001262 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001263 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001264 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001265 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001266 break;
1267}
1268
drheaf52d82010-05-12 13:50:23 +00001269/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001270** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001271**
drheaf52d82010-05-12 13:50:23 +00001272** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001273**
drh0fd61352014-02-07 02:29:45 +00001274** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001275** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001276*/
drh27a348c2015-04-13 19:14:06 +00001277case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001278 Mem *pVar; /* Value being transferred */
1279
drheaf52d82010-05-12 13:50:23 +00001280 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001281 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001282 pVar = &p->aVar[pOp->p1 - 1];
1283 if( sqlite3VdbeMemTooBig(pVar) ){
1284 goto too_big;
drh023ae032007-05-08 12:12:16 +00001285 }
drh7441df72017-01-09 19:27:04 +00001286 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001287 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1288 memcpy(pOut, pVar, MEMCELLSIZE);
1289 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1290 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001291 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001292 break;
1293}
danielk1977295ba552004-05-19 10:34:51 +00001294
drhb21e7c72008-06-22 12:37:57 +00001295/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001296** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001297**
drh079a3072014-03-19 14:10:55 +00001298** Move the P3 values in register P1..P1+P3-1 over into
1299** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001300** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001301** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1302** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001303*/
drhe1349cb2008-04-01 00:36:10 +00001304case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001305 int n; /* Number of registers left to copy */
1306 int p1; /* Register to copy from */
1307 int p2; /* Register to copy to */
1308
drhe09f43f2013-11-21 04:18:31 +00001309 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001310 p1 = pOp->p1;
1311 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001312 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001313 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001314
drha6c2ed92009-11-14 23:22:23 +00001315 pIn1 = &aMem[p1];
1316 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001317 do{
drh9f6168b2016-03-19 23:32:58 +00001318 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1319 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001320 assert( memIsValid(pIn1) );
1321 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001322 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001323#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001324 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001325 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001326 }
1327#endif
drhbd6789e2015-04-28 14:00:02 +00001328 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001329 REGISTER_TRACE(p2++, pOut);
1330 pIn1++;
1331 pOut++;
drh079a3072014-03-19 14:10:55 +00001332 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001333 break;
1334}
1335
drhe8e4af72012-09-21 00:04:28 +00001336/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001337** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001338**
drhe8e4af72012-09-21 00:04:28 +00001339** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001340**
1341** This instruction makes a deep copy of the value. A duplicate
1342** is made of any string or blob constant. See also OP_SCopy.
1343*/
drhe8e4af72012-09-21 00:04:28 +00001344case OP_Copy: {
1345 int n;
1346
1347 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001348 pIn1 = &aMem[pOp->p1];
1349 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001350 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001351 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001352 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001353 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1354 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001355#ifdef SQLITE_DEBUG
1356 pOut->pScopyFrom = 0;
1357#endif
drhe8e4af72012-09-21 00:04:28 +00001358 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1359 if( (n--)==0 ) break;
1360 pOut++;
1361 pIn1++;
1362 }
drhe1349cb2008-04-01 00:36:10 +00001363 break;
1364}
1365
drhb1fdb2a2008-01-05 04:06:03 +00001366/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001367** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001368**
drh9cbf3422008-01-17 16:22:13 +00001369** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001370**
1371** This instruction makes a shallow copy of the value. If the value
1372** is a string or blob, then the copy is only a pointer to the
1373** original and hence if the original changes so will the copy.
1374** Worse, if the original is deallocated, the copy becomes invalid.
1375** Thus the program must guarantee that the original will not change
1376** during the lifetime of the copy. Use OP_Copy to make a complete
1377** copy.
1378*/
drh26198bb2013-10-31 11:15:09 +00001379case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001380 pIn1 = &aMem[pOp->p1];
1381 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001382 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001383 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001384#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001385 pOut->pScopyFrom = pIn1;
1386 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001387#endif
drh5e00f6c2001-09-13 13:46:56 +00001388 break;
1389}
drh75897232000-05-29 14:26:00 +00001390
drhfed7ac62015-10-15 18:04:59 +00001391/* Opcode: IntCopy P1 P2 * * *
1392** Synopsis: r[P2]=r[P1]
1393**
1394** Transfer the integer value held in register P1 into register P2.
1395**
1396** This is an optimized version of SCopy that works only for integer
1397** values.
1398*/
1399case OP_IntCopy: { /* out2 */
1400 pIn1 = &aMem[pOp->p1];
1401 assert( (pIn1->flags & MEM_Int)!=0 );
1402 pOut = &aMem[pOp->p2];
1403 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1404 break;
1405}
1406
drh9cbf3422008-01-17 16:22:13 +00001407/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001408** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001409**
shane21e7feb2008-05-30 15:59:49 +00001410** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001411** results. This opcode causes the sqlite3_step() call to terminate
1412** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001413** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001414** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001415*/
drh9cbf3422008-01-17 16:22:13 +00001416case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001417 Mem *pMem;
1418 int i;
1419 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001420 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001421 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001422
dan32b09f22009-09-23 17:29:59 +00001423 /* If this statement has violated immediate foreign key constraints, do
1424 ** not return the number of rows modified. And do not RELEASE the statement
1425 ** transaction. It needs to be rolled back. */
1426 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1427 assert( db->flags&SQLITE_CountRows );
1428 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001429 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001430 }
1431
danielk1977bd434552009-03-18 10:33:00 +00001432 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1433 ** DML statements invoke this opcode to return the number of rows
1434 ** modified to the user. This is the only way that a VM that
1435 ** opens a statement transaction may invoke this opcode.
1436 **
1437 ** In case this is such a statement, close any statement transaction
1438 ** opened by this VM before returning control to the user. This is to
1439 ** ensure that statement-transactions are always nested, not overlapping.
1440 ** If the open statement-transaction is not closed here, then the user
1441 ** may step another VM that opens its own statement transaction. This
1442 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001443 **
1444 ** The statement transaction is never a top-level transaction. Hence
1445 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001446 */
1447 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001448 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001449 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001450
drhd4e70eb2008-01-02 00:34:36 +00001451 /* Invalidate all ephemeral cursor row caches */
1452 p->cacheCtr = (p->cacheCtr + 2)|1;
1453
1454 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001455 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001456 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001457 */
drha6c2ed92009-11-14 23:22:23 +00001458 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001459 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001460 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001461 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001462 assert( (pMem[i].flags & MEM_Ephem)==0
1463 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001464 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001465 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001466 }
drh28039692008-03-17 16:54:01 +00001467 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001468
drh3d2a5292016-07-13 22:55:01 +00001469 if( db->mTrace & SQLITE_TRACE_ROW ){
1470 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1471 }
1472
drhd4e70eb2008-01-02 00:34:36 +00001473 /* Return SQLITE_ROW
1474 */
drhf56fa462015-04-13 21:39:54 +00001475 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001476 rc = SQLITE_ROW;
1477 goto vdbe_return;
1478}
1479
drh5b6afba2008-01-05 16:29:28 +00001480/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001481** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001482**
drh5b6afba2008-01-05 16:29:28 +00001483** Add the text in register P1 onto the end of the text in
1484** register P2 and store the result in register P3.
1485** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001486**
1487** P3 = P2 || P1
1488**
1489** It is illegal for P1 and P3 to be the same register. Sometimes,
1490** if P3 is the same register as P2, the implementation is able
1491** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001492*/
drh5b6afba2008-01-05 16:29:28 +00001493case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh8a7e11f2019-05-01 15:32:40 +00001494 i64 nByte; /* Total size of the output string or blob */
1495 u16 flags1; /* Initial flags for P1 */
1496 u16 flags2; /* Initial flags for P2 */
danielk19778a6b5412004-05-24 07:04:25 +00001497
drh3c657212009-11-17 23:59:58 +00001498 pIn1 = &aMem[pOp->p1];
1499 pIn2 = &aMem[pOp->p2];
1500 pOut = &aMem[pOp->p3];
drh8a7e11f2019-05-01 15:32:40 +00001501 testcase( pIn1==pIn2 );
1502 testcase( pOut==pIn2 );
danielk1977a7a8e142008-02-13 18:25:27 +00001503 assert( pIn1!=pOut );
drh8a7e11f2019-05-01 15:32:40 +00001504 flags1 = pIn1->flags;
1505 testcase( flags1 & MEM_Null );
1506 testcase( pIn2->flags & MEM_Null );
1507 if( (flags1 | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001508 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001509 break;
drh5e00f6c2001-09-13 13:46:56 +00001510 }
drh8a7e11f2019-05-01 15:32:40 +00001511 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
1512 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001513 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001514 }else if( (flags1 & MEM_Zero)!=0 ){
1515 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001516 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001517 }
1518 flags2 = pIn2->flags;
1519 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
1520 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001521 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001522 }else if( (flags2 & MEM_Zero)!=0 ){
1523 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001524 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001525 }
drh5b6afba2008-01-05 16:29:28 +00001526 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001527 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001528 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001529 }
drhdf82afc2019-05-16 01:22:21 +00001530 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001531 goto no_mem;
1532 }
drhc91b2fd2014-03-01 18:13:23 +00001533 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001534 if( pOut!=pIn2 ){
1535 memcpy(pOut->z, pIn2->z, pIn2->n);
drh8a7e11f2019-05-01 15:32:40 +00001536 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
1537 pIn2->flags = flags2;
danielk1977a7a8e142008-02-13 18:25:27 +00001538 }
1539 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh8a7e11f2019-05-01 15:32:40 +00001540 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1541 pIn1->flags = flags1;
drh81316f82013-10-29 20:40:47 +00001542 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001543 pOut->z[nByte+1] = 0;
drhdf82afc2019-05-16 01:22:21 +00001544 pOut->z[nByte+2] = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001545 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001546 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001547 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001548 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001549 break;
1550}
drh75897232000-05-29 14:26:00 +00001551
drh3c84ddf2008-01-09 02:15:38 +00001552/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001553** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001554**
drh60a713c2008-01-21 16:22:45 +00001555** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001556** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001557** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001558*/
drh3c84ddf2008-01-09 02:15:38 +00001559/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001560** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001561**
drh3c84ddf2008-01-09 02:15:38 +00001562**
shane21e7feb2008-05-30 15:59:49 +00001563** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001564** and store the result in register P3.
1565** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001566*/
drh3c84ddf2008-01-09 02:15:38 +00001567/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001568** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001569**
drh60a713c2008-01-21 16:22:45 +00001570** Subtract the value in register P1 from the value in register P2
1571** and store the result in register P3.
1572** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001573*/
drh9cbf3422008-01-17 16:22:13 +00001574/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001575** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001576**
drh60a713c2008-01-21 16:22:45 +00001577** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001578** and store the result in register P3 (P3=P2/P1). If the value in
1579** register P1 is zero, then the result is NULL. If either input is
1580** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001581*/
drh9cbf3422008-01-17 16:22:13 +00001582/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001583** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001584**
drh40864a12013-11-15 18:58:37 +00001585** Compute the remainder after integer register P2 is divided by
1586** register P1 and store the result in register P3.
1587** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001588** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001589*/
drh5b6afba2008-01-05 16:29:28 +00001590case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1591case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1592case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1593case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1594case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh3d1d90a2014-03-24 15:00:15 +00001595 u16 flags; /* Combined MEM_* flags from both inputs */
1596 u16 type1; /* Numeric type of left operand */
1597 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001598 i64 iA; /* Integer value of left operand */
1599 i64 iB; /* Integer value of right operand */
1600 double rA; /* Real value of left operand */
1601 double rB; /* Real value of right operand */
1602
drh3c657212009-11-17 23:59:58 +00001603 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001604 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001605 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001606 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001607 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001608 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001609 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001610 iA = pIn1->u.i;
1611 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001612 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001613 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1614 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1615 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001616 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001617 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001618 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001619 iB /= iA;
drh75897232000-05-29 14:26:00 +00001620 break;
1621 }
drhbf4133c2001-10-13 02:59:08 +00001622 default: {
drh856c1032009-06-02 15:21:42 +00001623 if( iA==0 ) goto arithmetic_result_is_null;
1624 if( iA==-1 ) iA = 1;
1625 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001626 break;
1627 }
drh75897232000-05-29 14:26:00 +00001628 }
drh856c1032009-06-02 15:21:42 +00001629 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001630 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001631 }else if( (flags & MEM_Null)!=0 ){
1632 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001633 }else{
drh158b9cb2011-03-05 20:59:46 +00001634fp_math:
drh856c1032009-06-02 15:21:42 +00001635 rA = sqlite3VdbeRealValue(pIn1);
1636 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001637 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001638 case OP_Add: rB += rA; break;
1639 case OP_Subtract: rB -= rA; break;
1640 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001641 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001642 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001643 if( rA==(double)0 ) goto arithmetic_result_is_null;
1644 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001645 break;
1646 }
drhbf4133c2001-10-13 02:59:08 +00001647 default: {
drhe3b89d22019-01-18 17:53:50 +00001648 iA = sqlite3VdbeIntValue(pIn1);
1649 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001650 if( iA==0 ) goto arithmetic_result_is_null;
1651 if( iA==-1 ) iA = 1;
1652 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001653 break;
1654 }
drh5e00f6c2001-09-13 13:46:56 +00001655 }
drhc5a7b512010-01-13 16:25:42 +00001656#ifdef SQLITE_OMIT_FLOATING_POINT
1657 pOut->u.i = rB;
1658 MemSetTypeFlag(pOut, MEM_Int);
1659#else
drh856c1032009-06-02 15:21:42 +00001660 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001661 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001662 }
drh74eaba42014-09-18 17:52:15 +00001663 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001664 MemSetTypeFlag(pOut, MEM_Real);
drhc5a7b512010-01-13 16:25:42 +00001665#endif
drh5e00f6c2001-09-13 13:46:56 +00001666 }
1667 break;
1668
drha05a7222008-01-19 03:35:58 +00001669arithmetic_result_is_null:
1670 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001671 break;
1672}
1673
drh7a957892012-02-02 17:35:43 +00001674/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001675**
drhbb6783b2017-04-29 18:02:49 +00001676** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001677** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1678** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001679** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001680**
drh7a957892012-02-02 17:35:43 +00001681** If P1 is not zero, then it is a register that a subsequent min() or
1682** max() aggregate will set to 1 if the current row is not the minimum or
1683** maximum. The P1 register is initialized to 0 by this instruction.
1684**
danielk1977dc1bdc42004-06-11 10:51:27 +00001685** The interface used by the implementation of the aforementioned functions
1686** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001687** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001688*/
drh9cbf3422008-01-17 16:22:13 +00001689case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001690 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001691 if( pOp->p1 ){
1692 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1693 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001694 break;
1695}
1696
drh98757152008-01-09 23:04:12 +00001697/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001698** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001699**
drh98757152008-01-09 23:04:12 +00001700** Take the bit-wise AND of the values in register P1 and P2 and
1701** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001702** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001703*/
drh98757152008-01-09 23:04:12 +00001704/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001705** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001706**
drh98757152008-01-09 23:04:12 +00001707** Take the bit-wise OR of the values in register P1 and P2 and
1708** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001709** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001710*/
drh98757152008-01-09 23:04:12 +00001711/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001712** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001713**
drh98757152008-01-09 23:04:12 +00001714** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001715** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001716** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001717** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001718*/
drh98757152008-01-09 23:04:12 +00001719/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001720** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001721**
drh98757152008-01-09 23:04:12 +00001722** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001723** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001724** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001725** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001726*/
drh5b6afba2008-01-05 16:29:28 +00001727case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1728case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1729case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1730case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001731 i64 iA;
1732 u64 uA;
1733 i64 iB;
1734 u8 op;
drh6810ce62004-01-31 19:22:56 +00001735
drh3c657212009-11-17 23:59:58 +00001736 pIn1 = &aMem[pOp->p1];
1737 pIn2 = &aMem[pOp->p2];
1738 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001739 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001740 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001741 break;
1742 }
drh158b9cb2011-03-05 20:59:46 +00001743 iA = sqlite3VdbeIntValue(pIn2);
1744 iB = sqlite3VdbeIntValue(pIn1);
1745 op = pOp->opcode;
1746 if( op==OP_BitAnd ){
1747 iA &= iB;
1748 }else if( op==OP_BitOr ){
1749 iA |= iB;
1750 }else if( iB!=0 ){
1751 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1752
1753 /* If shifting by a negative amount, shift in the other direction */
1754 if( iB<0 ){
1755 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1756 op = 2*OP_ShiftLeft + 1 - op;
1757 iB = iB>(-64) ? -iB : 64;
1758 }
1759
1760 if( iB>=64 ){
1761 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1762 }else{
1763 memcpy(&uA, &iA, sizeof(uA));
1764 if( op==OP_ShiftLeft ){
1765 uA <<= iB;
1766 }else{
1767 uA >>= iB;
1768 /* Sign-extend on a right shift of a negative number */
1769 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1770 }
1771 memcpy(&iA, &uA, sizeof(iA));
1772 }
drhbf4133c2001-10-13 02:59:08 +00001773 }
drh158b9cb2011-03-05 20:59:46 +00001774 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001775 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001776 break;
1777}
1778
drh8558cde2008-01-05 05:20:10 +00001779/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001780** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001781**
danielk19770cdc0222008-06-26 18:04:03 +00001782** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001783** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001784**
drh8558cde2008-01-05 05:20:10 +00001785** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001786*/
drh9cbf3422008-01-17 16:22:13 +00001787case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001788 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001789 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001790 sqlite3VdbeMemIntegerify(pIn1);
1791 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001792 break;
1793}
1794
dane5166e02019-03-19 11:56:39 +00001795/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001796**
dane5166e02019-03-19 11:56:39 +00001797** Force the value in register P1 to be an integer. If the value
1798** in P1 is not an integer and cannot be converted into an integer
1799** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001800** raise an SQLITE_MISMATCH exception.
1801*/
drh9cbf3422008-01-17 16:22:13 +00001802case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001803 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001804 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001805 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001806 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001807 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001808 if( pOp->p2==0 ){
1809 rc = SQLITE_MISMATCH;
1810 goto abort_due_to_error;
1811 }else{
drhf56fa462015-04-13 21:39:54 +00001812 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001813 }
drh8aff1012001-12-22 14:49:24 +00001814 }
drh8aff1012001-12-22 14:49:24 +00001815 }
drhc9065332019-04-01 14:01:21 +00001816 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001817 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001818 break;
1819}
1820
drh13573c72010-01-12 17:04:07 +00001821#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001822/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001823**
drh2133d822008-01-03 18:44:59 +00001824** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001825**
drh8a512562005-11-14 22:29:05 +00001826** This opcode is used when extracting information from a column that
1827** has REAL affinity. Such column values may still be stored as
1828** integers, for space efficiency, but after extraction we want them
1829** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001830*/
drh9cbf3422008-01-17 16:22:13 +00001831case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001832 pIn1 = &aMem[pOp->p1];
drh169f0772019-05-02 21:36:26 +00001833 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +00001834 testcase( pIn1->flags & MEM_Int );
1835 testcase( pIn1->flags & MEM_IntReal );
drh8558cde2008-01-05 05:20:10 +00001836 sqlite3VdbeMemRealify(pIn1);
drhefb5f9a2019-08-30 21:52:13 +00001837 REGISTER_TRACE(pOp->p1, pIn1);
drh8a512562005-11-14 22:29:05 +00001838 }
drh487e2622005-06-25 18:42:14 +00001839 break;
1840}
drh13573c72010-01-12 17:04:07 +00001841#endif
drh487e2622005-06-25 18:42:14 +00001842
drh8df447f2005-11-01 15:48:24 +00001843#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001844/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001845** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001846**
drh4169e432014-08-25 20:11:52 +00001847** Force the value in register P1 to be the type defined by P2.
1848**
1849** <ul>
drhbb6783b2017-04-29 18:02:49 +00001850** <li> P2=='A' &rarr; BLOB
1851** <li> P2=='B' &rarr; TEXT
1852** <li> P2=='C' &rarr; NUMERIC
1853** <li> P2=='D' &rarr; INTEGER
1854** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001855** </ul>
drh487e2622005-06-25 18:42:14 +00001856**
1857** A NULL value is not changed by this routine. It remains NULL.
1858*/
drh4169e432014-08-25 20:11:52 +00001859case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001860 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001861 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001862 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001863 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1864 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1865 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001866 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001867 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001868 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001869 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001870 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001871 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001872 break;
1873}
drh8a512562005-11-14 22:29:05 +00001874#endif /* SQLITE_OMIT_CAST */
1875
drh79752b62016-08-13 10:02:17 +00001876/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001877** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001878**
1879** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1880** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1881** store the result of comparison in register P2.
1882**
1883** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1884** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1885** to coerce both inputs according to this affinity before the
1886** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1887** affinity is used. Note that the affinity conversions are stored
1888** back into the input registers P1 and P3. So this opcode can cause
1889** persistent changes to registers P1 and P3.
1890**
1891** Once any conversions have taken place, and neither value is NULL,
1892** the values are compared. If both values are blobs then memcmp() is
1893** used to determine the results of the comparison. If both values
1894** are text, then the appropriate collating function specified in
1895** P4 is used to do the comparison. If P4 is not specified then
1896** memcmp() is used to compare text string. If both values are
1897** numeric, then a numeric comparison is used. If the two values
1898** are of different types, then numbers are considered less than
1899** strings and strings are considered less than blobs.
1900**
1901** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1902** true or false and is never NULL. If both operands are NULL then the result
1903** of comparison is true. If either operand is NULL then the result is false.
1904** If neither operand is NULL the result is the same as it would be if
1905** the SQLITE_NULLEQ flag were omitted from P5.
1906**
1907** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001908** content of r[P2] is only changed if the new value is NULL or 0 (false).
1909** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001910*/
1911/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001912** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001913**
1914** This works just like the Eq opcode except that the jump is taken if
1915** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1916** additional information.
1917**
1918** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001919** content of r[P2] is only changed if the new value is NULL or 1 (true).
1920** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001921*/
drh35573352008-01-08 23:54:25 +00001922/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001923** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001924**
drh35573352008-01-08 23:54:25 +00001925** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001926** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1927** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001928**
drh35573352008-01-08 23:54:25 +00001929** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001930** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001931** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001932**
drh35573352008-01-08 23:54:25 +00001933** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001934** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001935** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001936** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001937** affinity is used. Note that the affinity conversions are stored
1938** back into the input registers P1 and P3. So this opcode can cause
1939** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001940**
1941** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001942** the values are compared. If both values are blobs then memcmp() is
1943** used to determine the results of the comparison. If both values
1944** are text, then the appropriate collating function specified in
1945** P4 is used to do the comparison. If P4 is not specified then
1946** memcmp() is used to compare text string. If both values are
1947** numeric, then a numeric comparison is used. If the two values
1948** are of different types, then numbers are considered less than
1949** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001950*/
drh9cbf3422008-01-17 16:22:13 +00001951/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001952** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001953**
drh35573352008-01-08 23:54:25 +00001954** This works just like the Lt opcode except that the jump is taken if
1955** the content of register P3 is less than or equal to the content of
1956** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001957*/
drh9cbf3422008-01-17 16:22:13 +00001958/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001959** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001960**
drh35573352008-01-08 23:54:25 +00001961** This works just like the Lt opcode except that the jump is taken if
1962** the content of register P3 is greater than the content of
1963** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001964*/
drh9cbf3422008-01-17 16:22:13 +00001965/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001966** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001967**
drh35573352008-01-08 23:54:25 +00001968** This works just like the Lt opcode except that the jump is taken if
1969** the content of register P3 is greater than or equal to the content of
1970** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001971*/
drh9cbf3422008-01-17 16:22:13 +00001972case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1973case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1974case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1975case OP_Le: /* same as TK_LE, jump, in1, in3 */
1976case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1977case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001978 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001979 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001980 u16 flags1; /* Copy of initial value of pIn1->flags */
1981 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001982
drh3c657212009-11-17 23:59:58 +00001983 pIn1 = &aMem[pOp->p1];
1984 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001985 flags1 = pIn1->flags;
1986 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001987 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001988 /* One or both operands are NULL */
1989 if( pOp->p5 & SQLITE_NULLEQ ){
1990 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1991 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1992 ** or not both operands are null.
1993 */
drh053a1282012-09-19 21:15:46 +00001994 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00001995 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
1996 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00001997 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001998 && (flags3&MEM_Cleared)==0
1999 ){
drh4910a762016-09-03 01:46:15 +00002000 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002001 }else{
danbdabe742019-03-18 16:51:24 +00002002 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002003 }
drh6a2fe092009-09-23 02:29:36 +00002004 }else{
2005 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2006 ** then the result is always NULL.
2007 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2008 */
drh688852a2014-02-17 22:40:43 +00002009 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002010 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002011 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00002012 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00002013 MemSetTypeFlag(pOut, MEM_Null);
2014 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002015 }else{
drhf4345e42014-02-18 11:31:59 +00002016 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00002017 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00002018 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002019 }
drh6a2fe092009-09-23 02:29:36 +00002020 }
2021 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002022 }
drh6a2fe092009-09-23 02:29:36 +00002023 }else{
2024 /* Neither operand is NULL. Do a comparison. */
2025 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002026 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002027 if( (flags1 | flags3)&MEM_Str ){
drh169f0772019-05-02 21:36:26 +00002028 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002029 applyNumericAffinity(pIn1,0);
drh24846bc2018-08-06 01:21:53 +00002030 assert( flags3==pIn3->flags );
drhcfdeeeb2018-08-04 20:12:10 +00002031 /* testcase( flags3!=pIn3->flags );
2032 ** this used to be possible with pIn1==pIn3, but not since
2033 ** the column cache was removed. The following assignment
drh24846bc2018-08-06 01:21:53 +00002034 ** is essentially a no-op. But, it provides defense-in-depth
drhcfdeeeb2018-08-04 20:12:10 +00002035 ** in case our analysis is incorrect, so it is left in. */
drh4b37cd42016-06-25 11:43:47 +00002036 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002037 }
drh169f0772019-05-02 21:36:26 +00002038 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002039 applyNumericAffinity(pIn3,0);
2040 }
drh24a09622014-09-18 16:28:59 +00002041 }
drh64caee42016-09-09 19:33:00 +00002042 /* Handle the common case of integer comparison here, as an
2043 ** optimization, to avoid a call to sqlite3MemCompare() */
2044 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2045 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2046 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2047 res = 0;
2048 goto compare_op;
2049 }
drh24a09622014-09-18 16:28:59 +00002050 }else if( affinity==SQLITE_AFF_TEXT ){
drh169f0772019-05-02 21:36:26 +00002051 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002052 testcase( pIn1->flags & MEM_Int );
2053 testcase( pIn1->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002054 testcase( pIn1->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002055 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002056 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2057 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00002058 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00002059 }
drh169f0772019-05-02 21:36:26 +00002060 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002061 testcase( pIn3->flags & MEM_Int );
2062 testcase( pIn3->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002063 testcase( pIn3->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002064 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002065 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2066 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002067 }
drh6a2fe092009-09-23 02:29:36 +00002068 }
drh6a2fe092009-09-23 02:29:36 +00002069 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002070 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002071 }
drh64caee42016-09-09 19:33:00 +00002072compare_op:
drh58596362017-08-03 00:29:23 +00002073 /* At this point, res is negative, zero, or positive if reg[P1] is
2074 ** less than, equal to, or greater than reg[P3], respectively. Compute
2075 ** the answer to this operator in res2, depending on what the comparison
2076 ** operator actually is. The next block of code depends on the fact
2077 ** that the 6 comparison operators are consecutive integers in this
2078 ** order: NE, EQ, GT, LE, LT, GE */
2079 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2080 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2081 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2082 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2083 res2 = aLTb[pOp->opcode - OP_Ne];
2084 }else if( res==0 ){
2085 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2086 res2 = aEQb[pOp->opcode - OP_Ne];
2087 }else{
2088 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2089 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002090 }
2091
drhf56fa462015-04-13 21:39:54 +00002092 /* Undo any changes made by applyAffinity() to the input registers. */
2093 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2094 pIn1->flags = flags1;
2095 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2096 pIn3->flags = flags3;
2097
drh35573352008-01-08 23:54:25 +00002098 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002099 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002100 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002101 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002102 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002103 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2104 ** is only used in contexts where either:
2105 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2106 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2107 ** Therefore it is not necessary to check the content of r[P2] for
2108 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002109 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002110 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002111 testcase( res2==0 && pOp->opcode==OP_Eq );
2112 testcase( res2==1 && pOp->opcode==OP_Eq );
2113 testcase( res2==0 && pOp->opcode==OP_Ne );
2114 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002115 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002116 }
drh2b4ded92010-09-27 21:09:31 +00002117 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002118 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002119 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002120 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002121 }else{
drh6cbbcd82019-04-01 13:06:19 +00002122 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002123 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002124 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002125 }
danielk1977a37cdde2004-05-16 11:15:36 +00002126 }
2127 break;
2128}
drhc9b84a12002-06-20 11:36:48 +00002129
drh79752b62016-08-13 10:02:17 +00002130/* Opcode: ElseNotEq * P2 * * *
2131**
drhfd7459e2016-09-17 17:39:01 +00002132** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2133** If result of an OP_Eq comparison on the same two operands
2134** would have be NULL or false (0), then then jump to P2.
2135** If the result of an OP_Eq comparison on the two previous operands
2136** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002137*/
2138case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2139 assert( pOp>aOp );
2140 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002141 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002142 VdbeBranchTaken(iCompare!=0, 2);
2143 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002144 break;
2145}
2146
2147
drh0acb7e42008-06-25 00:12:41 +00002148/* Opcode: Permutation * * * P4 *
2149**
drhb7dab702017-01-26 18:00:00 +00002150** Set the permutation used by the OP_Compare operator in the next
2151** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002152**
drh953f7612012-12-07 22:18:54 +00002153** The permutation is only valid until the next OP_Compare that has
2154** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2155** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002156**
2157** The first integer in the P4 integer array is the length of the array
2158** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002159*/
2160case OP_Permutation: {
2161 assert( pOp->p4type==P4_INTARRAY );
2162 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002163 assert( pOp[1].opcode==OP_Compare );
2164 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002165 break;
2166}
2167
drh953f7612012-12-07 22:18:54 +00002168/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002169** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002170**
drh710c4842010-08-30 01:17:20 +00002171** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2172** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002173** the comparison for use by the next OP_Jump instruct.
2174**
drh0ca10df2012-12-08 13:26:23 +00002175** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2176** determined by the most recent OP_Permutation operator. If the
2177** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2178** order.
2179**
drh0acb7e42008-06-25 00:12:41 +00002180** P4 is a KeyInfo structure that defines collating sequences and sort
2181** orders for the comparison. The permutation applies to registers
2182** only. The KeyInfo elements are used sequentially.
2183**
2184** The comparison is a sort comparison, so NULLs compare equal,
2185** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002186** and strings are less than blobs.
2187*/
2188case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002189 int n;
2190 int i;
2191 int p1;
2192 int p2;
2193 const KeyInfo *pKeyInfo;
2194 int idx;
2195 CollSeq *pColl; /* Collating sequence to use on this term */
2196 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002197 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002198
drhb7dab702017-01-26 18:00:00 +00002199 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2200 aPermute = 0;
2201 }else{
2202 assert( pOp>aOp );
2203 assert( pOp[-1].opcode==OP_Permutation );
2204 assert( pOp[-1].p4type==P4_INTARRAY );
2205 aPermute = pOp[-1].p4.ai + 1;
2206 assert( aPermute!=0 );
2207 }
drh856c1032009-06-02 15:21:42 +00002208 n = pOp->p3;
2209 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002210 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002211 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002212 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002213 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002214#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002215 if( aPermute ){
2216 int k, mx = 0;
2217 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002218 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2219 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002220 }else{
drh9f6168b2016-03-19 23:32:58 +00002221 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2222 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002223 }
2224#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002225 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002226 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002227 assert( memIsValid(&aMem[p1+idx]) );
2228 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002229 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2230 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002231 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002232 pColl = pKeyInfo->aColl[i];
dan6e118922019-08-12 16:36:38 +00002233 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
drha6c2ed92009-11-14 23:22:23 +00002234 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002235 if( iCompare ){
dan6e118922019-08-12 16:36:38 +00002236 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
2237 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
2238 ){
2239 iCompare = -iCompare;
2240 }
drh0acb7e42008-06-25 00:12:41 +00002241 if( bRev ) iCompare = -iCompare;
2242 break;
2243 }
drh16ee60f2008-06-20 18:13:25 +00002244 }
2245 break;
2246}
2247
2248/* Opcode: Jump P1 P2 P3 * *
2249**
2250** Jump to the instruction at address P1, P2, or P3 depending on whether
2251** in the most recent OP_Compare instruction the P1 vector was less than
2252** equal to, or greater than the P2 vector, respectively.
2253*/
drh0acb7e42008-06-25 00:12:41 +00002254case OP_Jump: { /* jump */
2255 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002256 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002257 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002258 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002259 }else{
drh7083a482018-07-10 16:04:04 +00002260 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002261 }
2262 break;
2263}
2264
drh5b6afba2008-01-05 16:29:28 +00002265/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002266** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002267**
drh5b6afba2008-01-05 16:29:28 +00002268** Take the logical AND of the values in registers P1 and P2 and
2269** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002270**
drh5b6afba2008-01-05 16:29:28 +00002271** If either P1 or P2 is 0 (false) then the result is 0 even if
2272** the other input is NULL. A NULL and true or two NULLs give
2273** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002274*/
drh5b6afba2008-01-05 16:29:28 +00002275/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002276** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002277**
2278** Take the logical OR of the values in register P1 and P2 and
2279** store the answer in register P3.
2280**
2281** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2282** even if the other input is NULL. A NULL and false or two NULLs
2283** give a NULL output.
2284*/
2285case OP_And: /* same as TK_AND, in1, in2, out3 */
2286case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002287 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2288 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002289
drh1fcfa722018-02-26 15:27:31 +00002290 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2291 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002292 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002293 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002294 v1 = and_logic[v1*3+v2];
2295 }else{
drh5b6afba2008-01-05 16:29:28 +00002296 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002297 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002298 }
drh3c657212009-11-17 23:59:58 +00002299 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002300 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002301 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002302 }else{
drh5b6afba2008-01-05 16:29:28 +00002303 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002304 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002305 }
drh5e00f6c2001-09-13 13:46:56 +00002306 break;
2307}
2308
drh8abed7b2018-02-26 18:49:05 +00002309/* Opcode: IsTrue P1 P2 P3 P4 *
2310** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2311**
2312** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2313** IS NOT FALSE operators.
2314**
drh96acafb2018-02-27 14:49:25 +00002315** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002316** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2317** NULL, then the P3 is stored in register P2. Invert the answer if P4
2318** is 1.
2319**
2320** The logic is summarized like this:
2321**
2322** <ul>
drh96acafb2018-02-27 14:49:25 +00002323** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2324** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2325** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2326** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002327** </ul>
2328*/
2329case OP_IsTrue: { /* in1, out2 */
2330 assert( pOp->p4type==P4_INT32 );
2331 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002332 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002333 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2334 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2335 break;
2336}
2337
drhe99fa2a2008-12-15 15:27:51 +00002338/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002339** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002340**
drhe99fa2a2008-12-15 15:27:51 +00002341** Interpret the value in register P1 as a boolean value. Store the
2342** boolean complement in register P2. If the value in register P1 is
2343** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002344*/
drh93952eb2009-11-13 19:43:43 +00002345case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002346 pIn1 = &aMem[pOp->p1];
2347 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002348 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002349 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002350 }else{
2351 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002352 }
drh5e00f6c2001-09-13 13:46:56 +00002353 break;
2354}
2355
drhe99fa2a2008-12-15 15:27:51 +00002356/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002357** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002358**
drhe99fa2a2008-12-15 15:27:51 +00002359** Interpret the content of register P1 as an integer. Store the
2360** ones-complement of the P1 value into register P2. If P1 holds
2361** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002362*/
drh93952eb2009-11-13 19:43:43 +00002363case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002364 pIn1 = &aMem[pOp->p1];
2365 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002366 sqlite3VdbeMemSetNull(pOut);
2367 if( (pIn1->flags & MEM_Null)==0 ){
2368 pOut->flags = MEM_Int;
2369 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002370 }
drhbf4133c2001-10-13 02:59:08 +00002371 break;
2372}
2373
drh48f2d3b2011-09-16 01:34:43 +00002374/* Opcode: Once P1 P2 * * *
2375**
drhab087d42017-03-24 17:59:56 +00002376** Fall through to the next instruction the first time this opcode is
2377** encountered on each invocation of the byte-code program. Jump to P2
2378** on the second and all subsequent encounters during the same invocation.
2379**
2380** Top-level programs determine first invocation by comparing the P1
2381** operand against the P1 operand on the OP_Init opcode at the beginning
2382** of the program. If the P1 values differ, then fall through and make
2383** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2384** the same then take the jump.
2385**
2386** For subprograms, there is a bitmask in the VdbeFrame that determines
2387** whether or not the jump should be taken. The bitmask is necessary
2388** because the self-altering code trick does not work for recursive
2389** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002390*/
dan1d8cb212011-12-09 13:24:16 +00002391case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002392 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002393 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002394 if( p->pFrame ){
2395 iAddr = (int)(pOp - p->aOp);
2396 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2397 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002398 goto jump_to_p2;
2399 }
drh18333ef2017-03-24 18:38:41 +00002400 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002401 }else{
drhab087d42017-03-24 17:59:56 +00002402 if( p->aOp[0].p1==pOp->p1 ){
2403 VdbeBranchTaken(1, 2);
2404 goto jump_to_p2;
2405 }
dan1d8cb212011-12-09 13:24:16 +00002406 }
drhab087d42017-03-24 17:59:56 +00002407 VdbeBranchTaken(0, 2);
2408 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002409 break;
2410}
2411
drh3c84ddf2008-01-09 02:15:38 +00002412/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002413**
drhef8662b2011-06-20 21:47:58 +00002414** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002415** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002416** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002417*/
drh1fcfa722018-02-26 15:27:31 +00002418case OP_If: { /* jump, in1 */
2419 int c;
2420 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2421 VdbeBranchTaken(c!=0, 2);
2422 if( c ) goto jump_to_p2;
2423 break;
2424}
2425
drh3c84ddf2008-01-09 02:15:38 +00002426/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002427**
drhef8662b2011-06-20 21:47:58 +00002428** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002429** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002430** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002431*/
drh9cbf3422008-01-17 16:22:13 +00002432case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002433 int c;
drh1fcfa722018-02-26 15:27:31 +00002434 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002435 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002436 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002437 break;
2438}
2439
drh830ecf92009-06-18 00:41:55 +00002440/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002441** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002442**
drh830ecf92009-06-18 00:41:55 +00002443** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002444*/
drh9cbf3422008-01-17 16:22:13 +00002445case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002446 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002447 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002448 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002449 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002450 }
drh477df4b2008-01-05 18:48:24 +00002451 break;
2452}
2453
drh98757152008-01-09 23:04:12 +00002454/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002455** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002456**
drh6a288a32008-01-07 19:20:24 +00002457** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002458*/
drh9cbf3422008-01-17 16:22:13 +00002459case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002460 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002461 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002462 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002463 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002464 }
drh5e00f6c2001-09-13 13:46:56 +00002465 break;
2466}
2467
drh31d6fd52017-04-14 19:03:10 +00002468/* Opcode: IfNullRow P1 P2 P3 * *
2469** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2470**
2471** Check the cursor P1 to see if it is currently pointing at a NULL row.
2472** If it is, then set register P3 to NULL and jump immediately to P2.
2473** If P1 is not on a NULL row, then fall through without making any
2474** changes.
2475*/
2476case OP_IfNullRow: { /* jump */
2477 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002478 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002479 if( p->apCsr[pOp->p1]->nullRow ){
2480 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2481 goto jump_to_p2;
2482 }
2483 break;
2484}
2485
drh092457b2017-12-29 15:04:49 +00002486#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2487/* Opcode: Offset P1 P2 P3 * *
2488** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002489**
drh092457b2017-12-29 15:04:49 +00002490** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002491** start of the payload for the record at which that cursor P1 is currently
2492** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002493**
drh092457b2017-12-29 15:04:49 +00002494** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002495** This opcode does not use P2 itself, but the P2 value is used by the
2496** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002497** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002498**
2499** This opcode is only available if SQLite is compiled with the
2500** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002501*/
drh092457b2017-12-29 15:04:49 +00002502case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002503 VdbeCursor *pC; /* The VDBE cursor */
2504 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2505 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002506 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002507 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002508 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002509 }else{
drh092457b2017-12-29 15:04:49 +00002510 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002511 }
2512 break;
2513}
drh092457b2017-12-29 15:04:49 +00002514#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002515
drh3e9ca092009-09-08 01:14:48 +00002516/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002517** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002518**
danielk1977cfcdaef2004-05-12 07:33:33 +00002519** Interpret the data that cursor P1 points to as a structure built using
2520** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002521** information about the format of the data.) Extract the P2-th column
2522** from this record. If there are less that (P2+1)
2523** values in the record, extract a NULL.
2524**
drh9cbf3422008-01-17 16:22:13 +00002525** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002526**
drh1cc3a362017-04-03 13:17:31 +00002527** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002528** if the P4 argument is a P4_MEM use the value of the P4 argument as
2529** the result.
drh3e9ca092009-09-08 01:14:48 +00002530**
drh1cc3a362017-04-03 13:17:31 +00002531** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002532** the result is guaranteed to only be used as the argument of a length()
2533** or typeof() function, respectively. The loading of large blobs can be
2534** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002535*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002536case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002537 int p2; /* column number to retrieve */
2538 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002539 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002540 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002541 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002542 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002543 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002544 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002545 const u8 *zData; /* Part of the record being decoded */
2546 const u8 *zHdr; /* Next unparsed byte of the header */
2547 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002548 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002549 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002550 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002551
dande892d92016-01-29 19:29:45 +00002552 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002553 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002554
drh170ad682017-06-02 15:44:22 +00002555 /* If the cursor cache is stale (meaning it is not currently point at
2556 ** the correct row) then bring it up-to-date by doing the necessary
2557 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002558 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002559 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002560
drh9f6168b2016-03-19 23:32:58 +00002561 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002562 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002563 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002564 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002565 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002566 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002567 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002568 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002569 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2570 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002571
drha43a02e2016-05-19 17:51:19 +00002572 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002573 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002574 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002575 /* For the special case of as pseudo-cursor, the seekResult field
2576 ** identifies the register that holds the record */
2577 assert( pC->seekResult>0 );
2578 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002579 assert( pReg->flags & MEM_Blob );
2580 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002581 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002582 pC->aRow = (u8*)pReg->z;
2583 }else{
drh6b5631e2014-11-05 15:57:39 +00002584 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002585 goto op_column_out;
2586 }
danielk1977192ac1d2004-05-10 07:17:30 +00002587 }else{
drh06a09a82016-11-25 17:03:03 +00002588 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002589 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002590 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002591 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2592 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002593 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2594 assert( pC->szRow<=pC->payloadSize );
2595 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2596 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002597 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002598 }
danielk1977192ac1d2004-05-10 07:17:30 +00002599 }
drhb73857f2006-03-17 00:25:59 +00002600 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002601 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002602 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002603
drhc81aa2e2014-10-11 23:31:52 +00002604
drh1f613c42017-08-16 14:16:19 +00002605 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002606 /* pC->aRow does not have to hold the entire row, but it does at least
2607 ** need to cover the header of the record. If pC->aRow does not contain
2608 ** the complete header, then set it to zero, forcing the header to be
2609 ** dynamically allocated. */
2610 pC->aRow = 0;
2611 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002612
2613 /* Make sure a corrupt database has not given us an oversize header.
2614 ** Do this now to avoid an oversize memory allocation.
2615 **
2616 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2617 ** types use so much data space that there can only be 4096 and 32 of
2618 ** them, respectively. So the maximum header length results from a
2619 ** 3-byte type for each of the maximum of 32768 columns plus three
2620 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2621 */
drh1f613c42017-08-16 14:16:19 +00002622 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002623 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002624 }
drh95b225a2017-08-16 11:04:22 +00002625 }else{
2626 /* This is an optimization. By skipping over the first few tests
2627 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2628 ** measurable performance gain.
2629 **
drh1f613c42017-08-16 14:16:19 +00002630 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002631 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002632 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002633 ** branch jumps to reads past the end of the record, but never more
2634 ** than a few bytes. Even if the record occurs at the end of the page
2635 ** content area, the "page header" comes after the page content and so
2636 ** this overread is harmless. Similar overreads can occur for a corrupt
2637 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002638 */
2639 zData = pC->aRow;
2640 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002641 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002642 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002643 }
drh399af1d2013-11-20 17:25:55 +00002644 }
drh35cd6432009-06-05 14:17:21 +00002645
drh399af1d2013-11-20 17:25:55 +00002646 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002647 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002648 */
drhc8606e42013-11-20 19:28:03 +00002649 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002650 /* If there is more header available for parsing in the record, try
2651 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002652 */
drhc8606e42013-11-20 19:28:03 +00002653 if( pC->iHdrOffset<aOffset[0] ){
2654 /* Make sure zData points to enough of the record to cover the header. */
2655 if( pC->aRow==0 ){
2656 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002657 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002658 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002659 zData = (u8*)sMem.z;
2660 }else{
2661 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002662 }
drhc8606e42013-11-20 19:28:03 +00002663
drh0c8f7602014-09-19 16:56:45 +00002664 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002665 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002666 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002667 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002668 zHdr = zData + pC->iHdrOffset;
2669 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002670 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002671 do{
drhc332e042019-02-12 21:04:33 +00002672 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002673 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002674 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002675 }else{
drhc8606e42013-11-20 19:28:03 +00002676 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002677 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002678 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002679 }
drhc332e042019-02-12 21:04:33 +00002680 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002681 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002682
drh8dd83622014-10-13 23:39:02 +00002683 /* The record is corrupt if any of the following are true:
2684 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002685 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002686 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002687 */
drhc6ce38832015-10-15 21:30:24 +00002688 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2689 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002690 ){
drh95b225a2017-08-16 11:04:22 +00002691 if( aOffset[0]==0 ){
2692 i = 0;
2693 zHdr = zEndHdr;
2694 }else{
2695 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002696 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002697 }
danielk1977dedf45b2006-01-13 17:12:01 +00002698 }
drhddb2b4a2016-03-25 12:10:32 +00002699
drh170c2762016-05-20 21:40:11 +00002700 pC->nHdrParsed = i;
2701 pC->iHdrOffset = (u32)(zHdr - zData);
2702 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002703 }else{
drh9fbc8852016-01-04 03:48:46 +00002704 t = 0;
drh9188b382004-05-14 21:12:22 +00002705 }
drhd3194f52004-05-27 19:59:32 +00002706
drhf2db3382015-04-30 20:33:25 +00002707 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002708 ** still not up to p2, that means that the record has fewer than p2
2709 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002710 */
drhc8606e42013-11-20 19:28:03 +00002711 if( pC->nHdrParsed<=p2 ){
2712 if( pOp->p4type==P4_MEM ){
2713 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2714 }else{
drh22e8d832014-10-29 00:58:38 +00002715 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002716 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002717 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002718 }
drh95fa6062015-10-16 13:50:08 +00002719 }else{
2720 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002721 }
danielk1977192ac1d2004-05-10 07:17:30 +00002722
drh380d6852013-11-20 20:58:00 +00002723 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002724 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002725 ** all valid.
drh9188b382004-05-14 21:12:22 +00002726 */
drhc8606e42013-11-20 19:28:03 +00002727 assert( p2<pC->nHdrParsed );
2728 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002729 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002730 if( VdbeMemDynamic(pDest) ){
2731 sqlite3VdbeMemSetNull(pDest);
2732 }
drh95fa6062015-10-16 13:50:08 +00002733 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002734 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002735 /* This is the common case where the desired content fits on the original
2736 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002737 zData = pC->aRow + aOffset[p2];
2738 if( t<12 ){
2739 sqlite3VdbeSerialGet(zData, t, pDest);
2740 }else{
2741 /* If the column value is a string, we need a persistent value, not
2742 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2743 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2744 */
2745 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2746 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002747 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002748 if( pDest->szMalloc < len+2 ){
2749 pDest->flags = MEM_Null;
2750 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2751 }else{
2752 pDest->z = pDest->zMalloc;
2753 }
2754 memcpy(pDest->z, zData, len);
2755 pDest->z[len] = 0;
2756 pDest->z[len+1] = 0;
2757 pDest->flags = aFlag[t&1];
2758 }
danielk197736963fd2005-02-19 08:18:05 +00002759 }else{
drha1851ef2016-05-20 19:51:28 +00002760 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002761 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002762 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2763 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2764 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002765 ){
drh2a2a6962014-09-16 18:22:44 +00002766 /* Content is irrelevant for
2767 ** 1. the typeof() function,
2768 ** 2. the length(X) function if X is a blob, and
2769 ** 3. if the content length is zero.
2770 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002771 ** content from disk.
2772 **
2773 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2774 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2775 ** read up to 16. So 16 bytes of bogus content is supplied.
2776 */
2777 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002778 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002779 }else{
drhcb3cabd2016-11-25 19:18:28 +00002780 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002781 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2782 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2783 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002784 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002785 }
drhd3194f52004-05-27 19:59:32 +00002786
danielk19773c9cc8d2005-01-17 03:40:08 +00002787op_column_out:
drhb7654112008-01-12 12:48:07 +00002788 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002789 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002790 break;
drh74588ce2017-09-13 00:13:05 +00002791
2792op_column_corrupt:
2793 if( aOp[0].p3>0 ){
2794 pOp = &aOp[aOp[0].p3-1];
2795 break;
2796 }else{
2797 rc = SQLITE_CORRUPT_BKPT;
2798 goto abort_due_to_error;
2799 }
danielk1977192ac1d2004-05-10 07:17:30 +00002800}
2801
danielk1977751de562008-04-18 09:01:15 +00002802/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002803** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002804**
2805** Apply affinities to a range of P2 registers starting with P1.
2806**
drhbb6783b2017-04-29 18:02:49 +00002807** P4 is a string that is P2 characters long. The N-th character of the
2808** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002809** memory cell in the range.
2810*/
2811case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002812 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002813
drh856c1032009-06-02 15:21:42 +00002814 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002815 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002816 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002817 assert( zAffinity[pOp->p2]==0 );
2818 pIn1 = &aMem[pOp->p1];
drh122c5142019-07-29 05:23:01 +00002819 while( 1 /*exit-by-break*/ ){
drh9f6168b2016-03-19 23:32:58 +00002820 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002821 assert( memIsValid(pIn1) );
drh83a1daf2019-05-01 18:59:33 +00002822 applyAffinity(pIn1, zAffinity[0], encoding);
2823 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
drh337cc392019-07-29 06:06:53 +00002824 /* When applying REAL affinity, if the result is still an MEM_Int
2825 ** that will fit in 6 bytes, then change the type to MEM_IntReal
2826 ** so that we keep the high-resolution integer value but know that
2827 ** the type really wants to be REAL. */
2828 testcase( pIn1->u.i==140737488355328LL );
2829 testcase( pIn1->u.i==140737488355327LL );
2830 testcase( pIn1->u.i==-140737488355328LL );
2831 testcase( pIn1->u.i==-140737488355329LL );
2832 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
2833 pIn1->flags |= MEM_IntReal;
2834 pIn1->flags &= ~MEM_Int;
2835 }else{
2836 pIn1->u.r = (double)pIn1->u.i;
2837 pIn1->flags |= MEM_Real;
2838 pIn1->flags &= ~MEM_Int;
2839 }
drh83a1daf2019-05-01 18:59:33 +00002840 }
drh6fcc1ec2019-05-01 14:41:47 +00002841 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
drh83a1daf2019-05-01 18:59:33 +00002842 zAffinity++;
2843 if( zAffinity[0]==0 ) break;
drh039fc322009-11-17 18:31:47 +00002844 pIn1++;
drh83a1daf2019-05-01 18:59:33 +00002845 }
danielk1977751de562008-04-18 09:01:15 +00002846 break;
2847}
2848
drh1db639c2008-01-17 02:36:28 +00002849/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002850** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002851**
drh710c4842010-08-30 01:17:20 +00002852** Convert P2 registers beginning with P1 into the [record format]
2853** use as a data record in a database table or as a key
2854** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002855**
drhbb6783b2017-04-29 18:02:49 +00002856** P4 may be a string that is P2 characters long. The N-th character of the
2857** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002858** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002859**
drh8a512562005-11-14 22:29:05 +00002860** The mapping from character to affinity is given by the SQLITE_AFF_
2861** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002862**
drh05883a32015-06-02 15:32:08 +00002863** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002864*/
drh1db639c2008-01-17 02:36:28 +00002865case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002866 Mem *pRec; /* The new record */
2867 u64 nData; /* Number of bytes of data space */
2868 int nHdr; /* Number of bytes of header space */
2869 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002870 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002871 int nVarint; /* Number of bytes in a varint */
2872 u32 serial_type; /* Type field */
2873 Mem *pData0; /* First field to be combined into the record */
2874 Mem *pLast; /* Last field of the record */
2875 int nField; /* Number of fields in the record */
2876 char *zAffinity; /* The affinity string for the record */
2877 int file_format; /* File format to use for encoding */
drhbe37c122015-10-16 14:54:17 +00002878 u32 len; /* Length of a field */
drhb70b0df2019-04-30 01:08:42 +00002879 u8 *zHdr; /* Where to write next byte of the header */
2880 u8 *zPayload; /* Where to write next byte of the payload */
drh856c1032009-06-02 15:21:42 +00002881
drhf3218fe2004-05-28 08:21:02 +00002882 /* Assuming the record contains N fields, the record format looks
2883 ** like this:
2884 **
drh7a224de2004-06-02 01:22:02 +00002885 ** ------------------------------------------------------------------------
2886 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2887 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002888 **
drh9cbf3422008-01-17 16:22:13 +00002889 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002890 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002891 **
2892 ** Each type field is a varint representing the serial type of the
2893 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002894 ** hdr-size field is also a varint which is the offset from the beginning
2895 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002896 */
drh856c1032009-06-02 15:21:42 +00002897 nData = 0; /* Number of bytes of data space */
2898 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002899 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002900 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002901 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002902 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002903 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002904 nField = pOp->p2;
2905 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002906 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002907
drh2b4ded92010-09-27 21:09:31 +00002908 /* Identify the output register */
2909 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2910 pOut = &aMem[pOp->p3];
2911 memAboutToChange(p, pOut);
2912
drh3e6c0602013-12-10 20:53:01 +00002913 /* Apply the requested affinity to all inputs
2914 */
2915 assert( pData0<=pLast );
2916 if( zAffinity ){
2917 pRec = pData0;
2918 do{
drh5ad12512019-05-09 16:22:51 +00002919 applyAffinity(pRec, zAffinity[0], encoding);
danbe812622019-05-17 15:59:11 +00002920 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
2921 pRec->flags |= MEM_IntReal;
2922 pRec->flags &= ~(MEM_Int);
2923 }
drh5ad12512019-05-09 16:22:51 +00002924 REGISTER_TRACE((int)(pRec-aMem), pRec);
2925 zAffinity++;
2926 pRec++;
drh57bf4a82014-02-17 14:59:22 +00002927 assert( zAffinity[0]==0 || pRec<=pLast );
2928 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002929 }
2930
drhd447dce2017-01-25 20:55:11 +00002931#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002932 /* NULLs can be safely trimmed from the end of the record, as long as
2933 ** as the schema format is 2 or more and none of the omitted columns
2934 ** have a non-NULL default value. Also, the record must be left with
2935 ** at least one field. If P5>0 then it will be one more than the
2936 ** index of the right-most column with a non-NULL default value */
2937 if( pOp->p5 ){
2938 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2939 pLast--;
2940 nField--;
2941 }
2942 }
drhd447dce2017-01-25 20:55:11 +00002943#endif
drh585ce192017-01-25 14:58:27 +00002944
drhf3218fe2004-05-28 08:21:02 +00002945 /* Loop through the elements that will make up the record to figure
drh76fd7be2019-07-11 19:50:18 +00002946 ** out how much space is required for the new record. After this loop,
2947 ** the Mem.uTemp field of each term should hold the serial-type that will
2948 ** be used for that term in the generated record:
2949 **
2950 ** Mem.uTemp value type
2951 ** --------------- ---------------
2952 ** 0 NULL
2953 ** 1 1-byte signed integer
2954 ** 2 2-byte signed integer
2955 ** 3 3-byte signed integer
2956 ** 4 4-byte signed integer
2957 ** 5 6-byte signed integer
2958 ** 6 8-byte signed integer
2959 ** 7 IEEE float
2960 ** 8 Integer constant 0
2961 ** 9 Integer constant 1
2962 ** 10,11 reserved for expansion
2963 ** N>=12 and even BLOB
2964 ** N>=13 and odd text
2965 **
2966 ** The following additional values are computed:
2967 ** nHdr Number of bytes needed for the record header
2968 ** nData Number of bytes of data space needed for the record
2969 ** nZero Zero bytes at the end of the record
danielk19778d059842004-05-12 11:24:02 +00002970 */
drh038b7bc2013-12-09 23:17:22 +00002971 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002972 do{
drh2b4ded92010-09-27 21:09:31 +00002973 assert( memIsValid(pRec) );
drhc1da4392019-07-11 19:22:36 +00002974 if( pRec->flags & MEM_Null ){
2975 if( pRec->flags & MEM_Zero ){
drh41fb3672018-01-12 23:18:38 +00002976 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
2977 ** table methods that never invoke sqlite3_result_xxxxx() while
2978 ** computing an unchanging column value in an UPDATE statement.
2979 ** Give such values a special internal-use-only serial-type of 10
2980 ** so that they can be passed through to xUpdate and have
2981 ** a true sqlite3_value_nochange(). */
2982 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
drhc1da4392019-07-11 19:22:36 +00002983 pRec->uTemp = 10;
drh038b7bc2013-12-09 23:17:22 +00002984 }else{
drh76fd7be2019-07-11 19:50:18 +00002985 pRec->uTemp = 0;
drh038b7bc2013-12-09 23:17:22 +00002986 }
drhc1da4392019-07-11 19:22:36 +00002987 nHdr++;
2988 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
2989 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
2990 i64 i = pRec->u.i;
drh9c3bb592019-07-30 21:00:13 +00002991 u64 uu;
drhc1da4392019-07-11 19:22:36 +00002992 testcase( pRec->flags & MEM_Int );
2993 testcase( pRec->flags & MEM_IntReal );
2994 if( i<0 ){
drh9c3bb592019-07-30 21:00:13 +00002995 uu = ~i;
drhc1da4392019-07-11 19:22:36 +00002996 }else{
drh9c3bb592019-07-30 21:00:13 +00002997 uu = i;
drhc1da4392019-07-11 19:22:36 +00002998 }
2999 nHdr++;
drh9c3bb592019-07-30 21:00:13 +00003000 testcase( uu==127 ); testcase( uu==128 );
3001 testcase( uu==32767 ); testcase( uu==32768 );
3002 testcase( uu==8388607 ); testcase( uu==8388608 );
3003 testcase( uu==2147483647 ); testcase( uu==2147483648 );
3004 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
3005 if( uu<=127 ){
drhc1da4392019-07-11 19:22:36 +00003006 if( (i&1)==i && file_format>=4 ){
drh9c3bb592019-07-30 21:00:13 +00003007 pRec->uTemp = 8+(u32)uu;
drhc1da4392019-07-11 19:22:36 +00003008 }else{
3009 nData++;
3010 pRec->uTemp = 1;
3011 }
drh9c3bb592019-07-30 21:00:13 +00003012 }else if( uu<=32767 ){
drhc1da4392019-07-11 19:22:36 +00003013 nData += 2;
3014 pRec->uTemp = 2;
drh9c3bb592019-07-30 21:00:13 +00003015 }else if( uu<=8388607 ){
drhc1da4392019-07-11 19:22:36 +00003016 nData += 3;
3017 pRec->uTemp = 3;
drh9c3bb592019-07-30 21:00:13 +00003018 }else if( uu<=2147483647 ){
drhc1da4392019-07-11 19:22:36 +00003019 nData += 4;
3020 pRec->uTemp = 4;
drh9c3bb592019-07-30 21:00:13 +00003021 }else if( uu<=140737488355327LL ){
drhc1da4392019-07-11 19:22:36 +00003022 nData += 6;
3023 pRec->uTemp = 5;
3024 }else{
3025 nData += 8;
3026 if( pRec->flags & MEM_IntReal ){
3027 /* If the value is IntReal and is going to take up 8 bytes to store
3028 ** as an integer, then we might as well make it an 8-byte floating
3029 ** point value */
3030 pRec->u.r = (double)pRec->u.i;
3031 pRec->flags &= ~MEM_IntReal;
3032 pRec->flags |= MEM_Real;
3033 pRec->uTemp = 7;
3034 }else{
3035 pRec->uTemp = 6;
3036 }
3037 }
3038 }else if( pRec->flags & MEM_Real ){
3039 nHdr++;
3040 nData += 8;
3041 pRec->uTemp = 7;
3042 }else{
3043 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
3044 assert( pRec->n>=0 );
3045 len = (u32)pRec->n;
3046 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
3047 if( pRec->flags & MEM_Zero ){
3048 serial_type += pRec->u.nZero*2;
3049 if( nData ){
3050 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
3051 len += pRec->u.nZero;
3052 }else{
3053 nZero += pRec->u.nZero;
3054 }
3055 }
3056 nData += len;
3057 nHdr += sqlite3VarintLen(serial_type);
3058 pRec->uTemp = serial_type;
drhfdf972a2007-05-02 13:30:27 +00003059 }
drh45c3c662016-04-07 14:16:16 +00003060 if( pRec==pData0 ) break;
3061 pRec--;
3062 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00003063
drh654858d2014-11-20 02:18:14 +00003064 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3065 ** which determines the total number of bytes in the header. The varint
3066 ** value is the size of the header in bytes including the size varint
3067 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00003068 testcase( nHdr==126 );
3069 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00003070 if( nHdr<=126 ){
3071 /* The common case */
3072 nHdr += 1;
3073 }else{
3074 /* Rare case of a really large header */
3075 nVarint = sqlite3VarintLen(nHdr);
3076 nHdr += nVarint;
3077 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00003078 }
drh038b7bc2013-12-09 23:17:22 +00003079 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00003080
danielk1977a7a8e142008-02-13 18:25:27 +00003081 /* Make sure the output register has a buffer large enough to store
3082 ** the new record. The output register (pOp->p3) is not allowed to
3083 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00003084 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00003085 */
drh0d7f0cc2018-09-21 13:07:14 +00003086 if( nByte+nZero<=pOut->szMalloc ){
3087 /* The output register is already large enough to hold the record.
3088 ** No error checks or buffer enlargement is required */
3089 pOut->z = pOut->zMalloc;
3090 }else{
3091 /* Need to make sure that the output is not too big and then enlarge
3092 ** the output register to hold the full result */
3093 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
3094 goto too_big;
3095 }
3096 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
3097 goto no_mem;
3098 }
danielk19778d059842004-05-12 11:24:02 +00003099 }
drh9c1905f2008-12-10 22:32:56 +00003100 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00003101 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00003102 if( nZero ){
drh8df32842008-12-09 02:51:23 +00003103 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00003104 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00003105 }
drhb7654112008-01-12 12:48:07 +00003106 UPDATE_MAX_BLOBSIZE(pOut);
drhb70b0df2019-04-30 01:08:42 +00003107 zHdr = (u8 *)pOut->z;
3108 zPayload = zHdr + nHdr;
3109
3110 /* Write the record */
3111 zHdr += putVarint32(zHdr, nHdr);
3112 assert( pData0<=pLast );
3113 pRec = pData0;
3114 do{
3115 serial_type = pRec->uTemp;
3116 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
3117 ** additional varints, one per column. */
3118 zHdr += putVarint32(zHdr, serial_type); /* serial type */
3119 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
3120 ** immediately follow the header. */
3121 zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
3122 }while( (++pRec)<=pLast );
3123 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
3124 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
3125
3126 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
3127 REGISTER_TRACE(pOp->p3, pOut);
danielk19778d059842004-05-12 11:24:02 +00003128 break;
3129}
3130
danielk1977a5533162009-02-24 10:01:51 +00003131/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003132** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00003133**
3134** Store the number of entries (an integer value) in the table or index
3135** opened by cursor P1 in register P2
3136*/
3137#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00003138case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00003139 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00003140 BtCursor *pCrsr;
3141
drhc960dcb2015-11-20 19:22:01 +00003142 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
3143 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00003144 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00003145 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh21f6daa2019-10-11 14:21:48 +00003146 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00003147 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00003148 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00003149 pOut->u.i = nEntry;
drh21f6daa2019-10-11 14:21:48 +00003150 goto check_for_interrupt;
danielk1977a5533162009-02-24 10:01:51 +00003151}
3152#endif
3153
danielk1977fd7f0452008-12-17 17:30:26 +00003154/* Opcode: Savepoint P1 * * P4 *
3155**
3156** Open, release or rollback the savepoint named by parameter P4, depending
drh2ce9b6b2019-05-10 14:03:07 +00003157** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3158** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3159** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
danielk1977fd7f0452008-12-17 17:30:26 +00003160*/
3161case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00003162 int p1; /* Value of P1 operand */
3163 char *zName; /* Name of savepoint */
3164 int nName;
3165 Savepoint *pNew;
3166 Savepoint *pSavepoint;
3167 Savepoint *pTmp;
3168 int iSavepoint;
3169 int ii;
3170
3171 p1 = pOp->p1;
3172 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003173
3174 /* Assert that the p1 parameter is valid. Also that if there is no open
3175 ** transaction, then there cannot be any savepoints.
3176 */
3177 assert( db->pSavepoint==0 || db->autoCommit==0 );
3178 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3179 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3180 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003181 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003182
3183 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003184 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003185 /* A new savepoint cannot be created if there are active write
3186 ** statements (i.e. open read/write incremental blob handles).
3187 */
drh22c17b82015-05-15 04:13:15 +00003188 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003189 rc = SQLITE_BUSY;
3190 }else{
drh856c1032009-06-02 15:21:42 +00003191 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003192
drhbe07ec52011-06-03 12:15:26 +00003193#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003194 /* This call is Ok even if this savepoint is actually a transaction
3195 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3196 ** If this is a transaction savepoint being opened, it is guaranteed
3197 ** that the db->aVTrans[] array is empty. */
3198 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003199 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3200 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003201 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003202#endif
dand9495cd2011-04-27 12:08:04 +00003203
danielk1977fd7f0452008-12-17 17:30:26 +00003204 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003205 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003206 if( pNew ){
3207 pNew->zName = (char *)&pNew[1];
3208 memcpy(pNew->zName, zName, nName+1);
3209
3210 /* If there is no open transaction, then mark this as a special
3211 ** "transaction savepoint". */
3212 if( db->autoCommit ){
3213 db->autoCommit = 0;
3214 db->isTransactionSavepoint = 1;
3215 }else{
3216 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003217 }
dan21e8d012011-03-03 20:05:59 +00003218
danielk1977fd7f0452008-12-17 17:30:26 +00003219 /* Link the new savepoint into the database handle's list. */
3220 pNew->pNext = db->pSavepoint;
3221 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003222 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003223 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003224 }
3225 }
3226 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003227 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
drh856c1032009-06-02 15:21:42 +00003228 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003229
3230 /* Find the named savepoint. If there is no such savepoint, then an
3231 ** an error is returned to the user. */
3232 for(
drh856c1032009-06-02 15:21:42 +00003233 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003234 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003235 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003236 ){
3237 iSavepoint++;
3238 }
3239 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003240 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003241 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003242 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003243 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003244 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003245 */
drh22c17b82015-05-15 04:13:15 +00003246 sqlite3VdbeError(p, "cannot release savepoint - "
3247 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003248 rc = SQLITE_BUSY;
3249 }else{
3250
3251 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003252 ** and this is a RELEASE command, then the current transaction
3253 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003254 */
3255 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3256 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003257 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003258 goto vdbe_return;
3259 }
danielk1977fd7f0452008-12-17 17:30:26 +00003260 db->autoCommit = 1;
3261 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003262 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003263 db->autoCommit = 0;
3264 p->rc = rc = SQLITE_BUSY;
3265 goto vdbe_return;
3266 }
danielk197734cf35d2008-12-18 18:31:38 +00003267 db->isTransactionSavepoint = 0;
3268 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003269 }else{
drh47b7fc72014-11-11 01:33:57 +00003270 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003271 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003272 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003273 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003274 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003275 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3276 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003277 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003278 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003279 }
drh47b7fc72014-11-11 01:33:57 +00003280 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003281 assert( p1==SAVEPOINT_RELEASE );
drh47b7fc72014-11-11 01:33:57 +00003282 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003283 }
3284 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003285 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3286 if( rc!=SQLITE_OK ){
3287 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003288 }
danielk1977fd7f0452008-12-17 17:30:26 +00003289 }
drh47b7fc72014-11-11 01:33:57 +00003290 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003291 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003292 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003293 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003294 }
3295 }
3296
3297 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3298 ** savepoints nested inside of the savepoint being operated on. */
3299 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003300 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003301 db->pSavepoint = pTmp->pNext;
3302 sqlite3DbFree(db, pTmp);
3303 db->nSavepoint--;
3304 }
3305
dan1da40a32009-09-19 17:00:31 +00003306 /* If it is a RELEASE, then destroy the savepoint being operated on
3307 ** too. If it is a ROLLBACK TO, then set the number of deferred
3308 ** constraint violations present in the database to the value stored
3309 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003310 if( p1==SAVEPOINT_RELEASE ){
3311 assert( pSavepoint==db->pSavepoint );
3312 db->pSavepoint = pSavepoint->pNext;
3313 sqlite3DbFree(db, pSavepoint);
3314 if( !isTransaction ){
3315 db->nSavepoint--;
3316 }
dan1da40a32009-09-19 17:00:31 +00003317 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003318 assert( p1==SAVEPOINT_ROLLBACK );
dan1da40a32009-09-19 17:00:31 +00003319 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003320 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003321 }
dand9495cd2011-04-27 12:08:04 +00003322
danea8562e2015-04-18 16:25:54 +00003323 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003324 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3325 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3326 }
danielk1977fd7f0452008-12-17 17:30:26 +00003327 }
3328 }
drh9467abf2016-02-17 18:44:11 +00003329 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003330
3331 break;
3332}
3333
drh98757152008-01-09 23:04:12 +00003334/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003335**
3336** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003337** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003338** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3339** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003340**
3341** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003342*/
drh9cbf3422008-01-17 16:22:13 +00003343case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003344 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003345 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003346
drh856c1032009-06-02 15:21:42 +00003347 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003348 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003349 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003350 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003351 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003352 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003353
drhb0c88652016-02-01 13:21:13 +00003354 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003355 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003356 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003357 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003358 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003359 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3360 /* If this instruction implements a COMMIT and other VMs are writing
3361 ** return an error indicating that the other VMs must complete first.
3362 */
3363 sqlite3VdbeError(p, "cannot commit transaction - "
3364 "SQL statements in progress");
3365 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003366 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003367 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003368 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003369 }else{
shane7d3846a2008-12-11 02:58:26 +00003370 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003371 }
3372 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3373 p->pc = (int)(pOp - aOp);
3374 db->autoCommit = (u8)(1-desiredAutoCommit);
3375 p->rc = rc = SQLITE_BUSY;
3376 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003377 }
danielk1977fd7f0452008-12-17 17:30:26 +00003378 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003379 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003380 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003381 }else{
drh900b31e2007-08-28 02:27:51 +00003382 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003383 }
drh900b31e2007-08-28 02:27:51 +00003384 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003385 }else{
drh22c17b82015-05-15 04:13:15 +00003386 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003387 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003388 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003389 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003390
3391 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003392 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003393 }
drh8616cff2019-07-13 16:15:23 +00003394 /*NOTREACHED*/ assert(0);
drh663fc632002-02-02 18:49:19 +00003395}
3396
drhb22f7c82014-02-06 23:56:27 +00003397/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003398**
drh05a86c52014-02-16 01:55:49 +00003399** Begin a transaction on database P1 if a transaction is not already
3400** active.
3401** If P2 is non-zero, then a write-transaction is started, or if a
3402** read-transaction is already active, it is upgraded to a write-transaction.
3403** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003404**
drh001bbcb2003-03-19 03:14:00 +00003405** P1 is the index of the database file on which the transaction is
3406** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003407** file used for temporary tables. Indices of 2 or more are used for
3408** attached databases.
drhcabb0812002-09-14 13:47:32 +00003409**
dane0af83a2009-09-08 19:15:01 +00003410** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3411** true (this flag is set if the Vdbe may modify more than one row and may
3412** throw an ABORT exception), a statement transaction may also be opened.
3413** More specifically, a statement transaction is opened iff the database
3414** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003415** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003416** VDBE to be rolled back after an error without having to roll back the
3417** entire transaction. If no error is encountered, the statement transaction
3418** will automatically commit when the VDBE halts.
3419**
drhb22f7c82014-02-06 23:56:27 +00003420** If P5!=0 then this opcode also checks the schema cookie against P3
3421** and the schema generation counter against P4.
3422** The cookie changes its value whenever the database schema changes.
3423** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003424** and that the current process needs to reread the schema. If the schema
3425** cookie in P3 differs from the schema cookie in the database header or
3426** if the schema generation counter in P4 differs from the current
3427** generation counter, then an SQLITE_SCHEMA error is raised and execution
3428** halts. The sqlite3_step() wrapper function might then reprepare the
3429** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003430*/
drh9cbf3422008-01-17 16:22:13 +00003431case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003432 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003433 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003434
drh1713afb2013-06-28 01:24:57 +00003435 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003436 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003437 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003438 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003439 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3440 rc = SQLITE_READONLY;
3441 goto abort_due_to_error;
3442 }
drh653b82a2009-06-22 11:10:47 +00003443 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003444
danielk197724162fe2004-06-04 06:22:00 +00003445 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003446 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003447 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3448 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003449 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003450 if( (rc&0xff)==SQLITE_BUSY ){
3451 p->pc = (int)(pOp - aOp);
3452 p->rc = rc;
3453 goto vdbe_return;
3454 }
danielk197724162fe2004-06-04 06:22:00 +00003455 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003456 }
dane0af83a2009-09-08 19:15:01 +00003457
drh4d294482019-10-05 15:28:24 +00003458 if( p->usesStmtJournal
3459 && pOp->p2
danc0537fe2013-06-28 19:41:43 +00003460 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003461 ){
3462 assert( sqlite3BtreeIsInTrans(pBt) );
3463 if( p->iStatement==0 ){
3464 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3465 db->nStatement++;
3466 p->iStatement = db->nSavepoint + db->nStatement;
3467 }
dana311b802011-04-26 19:21:34 +00003468
drh346506f2011-05-25 01:16:42 +00003469 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003470 if( rc==SQLITE_OK ){
3471 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3472 }
dan1da40a32009-09-19 17:00:31 +00003473
3474 /* Store the current value of the database handles deferred constraint
3475 ** counter. If the statement transaction needs to be rolled back,
3476 ** the value of this counter needs to be restored too. */
3477 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003478 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003479 }
drh397776a2018-06-06 17:45:51 +00003480 }
3481 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3482 if( pOp->p5
3483 && (iMeta!=pOp->p3
3484 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3485 ){
3486 /*
drh96fdcb42016-09-27 00:09:33 +00003487 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3488 ** version is checked to ensure that the schema has not changed since the
3489 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003490 */
drhb22f7c82014-02-06 23:56:27 +00003491 sqlite3DbFree(db, p->zErrMsg);
3492 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3493 /* If the schema-cookie from the database file matches the cookie
3494 ** stored with the in-memory representation of the schema, do
3495 ** not reload the schema from the database file.
3496 **
3497 ** If virtual-tables are in use, this is not just an optimization.
3498 ** Often, v-tables store their data in other SQLite tables, which
3499 ** are queried from within xNext() and other v-table methods using
3500 ** prepared queries. If such a query is out-of-date, we do not want to
3501 ** discard the database schema, as the user code implementing the
3502 ** v-table would have to be ready for the sqlite3_vtab structure itself
3503 ** to be invalidated whenever sqlite3_step() is called from within
3504 ** a v-table method.
3505 */
3506 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3507 sqlite3ResetOneSchema(db, pOp->p1);
3508 }
3509 p->expired = 1;
3510 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003511 }
drh9467abf2016-02-17 18:44:11 +00003512 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003513 break;
3514}
3515
drhb1fdb2a2008-01-05 04:06:03 +00003516/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003517**
drh9cbf3422008-01-17 16:22:13 +00003518** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003519** P3==1 is the schema version. P3==2 is the database format.
3520** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003521** the main database file and P1==1 is the database file used to store
3522** temporary tables.
drh4a324312001-12-21 14:30:42 +00003523**
drh50e5dad2001-09-15 00:57:28 +00003524** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003525** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003526** executing this instruction.
3527*/
drh27a348c2015-04-13 19:14:06 +00003528case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003529 int iMeta;
drh856c1032009-06-02 15:21:42 +00003530 int iDb;
3531 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003532
drh1713afb2013-06-28 01:24:57 +00003533 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003534 iDb = pOp->p1;
3535 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003536 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003537 assert( iDb>=0 && iDb<db->nDb );
3538 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003539 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003540
danielk1977602b4662009-07-02 07:47:33 +00003541 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003542 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003543 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003544 break;
3545}
3546
drh98757152008-01-09 23:04:12 +00003547/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003548**
drh1861afc2016-02-01 21:48:34 +00003549** Write the integer value P3 into cookie number P2 of database P1.
3550** P2==1 is the schema version. P2==2 is the database format.
3551** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003552** size, and so forth. P1==0 is the main database file and P1==1 is the
3553** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003554**
3555** A transaction must be started before executing this opcode.
3556*/
drh1861afc2016-02-01 21:48:34 +00003557case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003558 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003559
3560 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003561 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003562 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003563 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003564 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003565 pDb = &db->aDb[pOp->p1];
3566 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003567 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003568 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003569 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003570 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003571 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003572 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003573 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003574 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003575 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003576 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003577 }
drhfd426c62006-01-30 15:34:22 +00003578 if( pOp->p1==1 ){
3579 /* Invalidate all prepared statements whenever the TEMP database
3580 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003581 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003582 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003583 }
drh9467abf2016-02-17 18:44:11 +00003584 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003585 break;
3586}
3587
drh98757152008-01-09 23:04:12 +00003588/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003589** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003590**
drhecdc7532001-09-23 02:35:53 +00003591** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003592** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003593** P3==0 means the main database, P3==1 means the database used for
3594** temporary tables, and P3>1 means used the corresponding attached
3595** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003596** values need not be contiguous but all P1 values should be small integers.
3597** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003598**
drh8e9deb62018-06-05 13:43:02 +00003599** Allowed P5 bits:
3600** <ul>
3601** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3602** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3603** of OP_SeekLE/OP_IdxGT)
3604** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003605**
danielk1977d336e222009-02-20 10:58:41 +00003606** The P4 value may be either an integer (P4_INT32) or a pointer to
3607** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003608** object, then table being opened must be an [index b-tree] where the
3609** KeyInfo object defines the content and collating
3610** sequence of that index b-tree. Otherwise, if P4 is an integer
3611** value, then the table being opened must be a [table b-tree] with a
3612** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003613**
drh35263192014-07-22 20:02:19 +00003614** See also: OpenWrite, ReopenIdx
3615*/
3616/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3617** Synopsis: root=P2 iDb=P3
3618**
drh8e9deb62018-06-05 13:43:02 +00003619** The ReopenIdx opcode works like OP_OpenRead except that it first
3620** checks to see if the cursor on P1 is already open on the same
3621** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003622** if the cursor is already open, do not reopen it.
3623**
drh8e9deb62018-06-05 13:43:02 +00003624** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3625** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3626** be the same as every other ReopenIdx or OpenRead for the same cursor
3627** number.
drh35263192014-07-22 20:02:19 +00003628**
drh8e9deb62018-06-05 13:43:02 +00003629** Allowed P5 bits:
3630** <ul>
3631** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3632** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3633** of OP_SeekLE/OP_IdxGT)
3634** </ul>
3635**
3636** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003637*/
drh98757152008-01-09 23:04:12 +00003638/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003639** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003640**
3641** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003642** page is P2 (or whose root page is held in register P2 if the
3643** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003644**
danielk1977d336e222009-02-20 10:58:41 +00003645** The P4 value may be either an integer (P4_INT32) or a pointer to
3646** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003647** object, then table being opened must be an [index b-tree] where the
3648** KeyInfo object defines the content and collating
3649** sequence of that index b-tree. Otherwise, if P4 is an integer
3650** value, then the table being opened must be a [table b-tree] with a
3651** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003652**
drh8e9deb62018-06-05 13:43:02 +00003653** Allowed P5 bits:
3654** <ul>
3655** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3656** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3657** of OP_SeekLE/OP_IdxGT)
3658** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3659** and subsequently delete entries in an index btree. This is a
3660** hint to the storage engine that the storage engine is allowed to
3661** ignore. The hint is not used by the official SQLite b*tree storage
3662** engine, but is used by COMDB2.
3663** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3664** as the root page, not the value of P2 itself.
3665** </ul>
drhf57b3392001-10-08 13:22:32 +00003666**
drh8e9deb62018-06-05 13:43:02 +00003667** This instruction works like OpenRead except that it opens the cursor
3668** in read/write mode.
3669**
3670** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003671*/
drh35263192014-07-22 20:02:19 +00003672case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003673 int nField;
3674 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003675 int p2;
3676 int iDb;
drhf57b3392001-10-08 13:22:32 +00003677 int wrFlag;
3678 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003679 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003680 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003681
drhe0997b32015-03-20 14:57:50 +00003682 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003683 assert( pOp->p4type==P4_KEYINFO );
3684 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003685 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003686 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003687 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003688 }
3689 /* If the cursor is not currently open or is open on a different
3690 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003691case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003692case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003693
drhe0997b32015-03-20 14:57:50 +00003694 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003695 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003696 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3697 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003698
drhba968db2018-07-24 22:02:12 +00003699 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003700 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003701 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003702 }
3703
drh856c1032009-06-02 15:21:42 +00003704 nField = 0;
3705 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003706 p2 = pOp->p2;
3707 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003708 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003709 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003710 pDb = &db->aDb[iDb];
3711 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003712 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003713 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003714 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3715 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003716 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003717 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3718 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003719 }
3720 }else{
3721 wrFlag = 0;
3722 }
dan428c2182012-08-06 18:50:11 +00003723 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003724 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003725 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003726 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003727 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003728 assert( memIsValid(pIn2) );
3729 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003730 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003731 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003732 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003733 ** that opcode will always set the p2 value to 2 or more or else fail.
3734 ** If there were a failure, the prepared statement would have halted
3735 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003736 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003737 }
danielk1977d336e222009-02-20 10:58:41 +00003738 if( pOp->p4type==P4_KEYINFO ){
3739 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003740 assert( pKeyInfo->enc==ENC(db) );
3741 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003742 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003743 }else if( pOp->p4type==P4_INT32 ){
3744 nField = pOp->p4.i;
3745 }
drh653b82a2009-06-22 11:10:47 +00003746 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003747 assert( nField>=0 );
3748 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003749 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003750 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003751 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003752 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003753 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003754#ifdef SQLITE_DEBUG
3755 pCur->wrFlag = wrFlag;
3756#endif
drhc960dcb2015-11-20 19:22:01 +00003757 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003758 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003759 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003760 ** SQLite used to check if the root-page flags were sane at this point
3761 ** and report database corruption if they were not, but this check has
3762 ** since moved into the btree layer. */
3763 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003764
3765open_cursor_set_hints:
3766 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3767 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003768 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003769#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003770 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3771#endif
drhc960dcb2015-11-20 19:22:01 +00003772 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003773 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003774 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003775 break;
3776}
3777
drhe08e8d62017-05-01 15:15:41 +00003778/* Opcode: OpenDup P1 P2 * * *
3779**
3780** Open a new cursor P1 that points to the same ephemeral table as
3781** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3782** opcode. Only ephemeral cursors may be duplicated.
3783**
3784** Duplicate ephemeral cursors are used for self-joins of materialized views.
3785*/
3786case OP_OpenDup: {
3787 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3788 VdbeCursor *pCx; /* The new cursor */
3789
3790 pOrig = p->apCsr[pOp->p2];
3791 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3792
3793 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3794 if( pCx==0 ) goto no_mem;
3795 pCx->nullRow = 1;
3796 pCx->isEphemeral = 1;
3797 pCx->pKeyInfo = pOrig->pKeyInfo;
3798 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00003799 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00003800 pCx->isOrdered = pOrig->isOrdered;
drh2c041312018-12-24 02:34:49 +00003801 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00003802 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003803 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3804 ** opened for a database. Since there is already an open cursor when this
3805 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3806 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003807 break;
3808}
3809
3810
drh2a5d9902011-08-26 00:34:45 +00003811/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003812** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003813**
drhb9bb7c12006-06-11 23:41:55 +00003814** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003815** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003816** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003817** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003818**
drhdfe3b582019-01-04 12:35:50 +00003819** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00003820** is cleared (all content is erased).
3821**
drh25d3adb2010-04-05 15:11:08 +00003822** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003823** The cursor points to a BTree table if P4==0 and to a BTree index
3824** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003825** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003826**
drh2a5d9902011-08-26 00:34:45 +00003827** The P5 parameter can be a mask of the BTREE_* flags defined
3828** in btree.h. These flags control aspects of the operation of
3829** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3830** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003831*/
drha21a64d2010-04-06 22:33:55 +00003832/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003833** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003834**
3835** This opcode works the same as OP_OpenEphemeral. It has a
3836** different name to distinguish its use. Tables created using
3837** by this opcode will be used for automatically created transient
3838** indices in joins.
3839*/
3840case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003841case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003842 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003843 KeyInfo *pKeyInfo;
3844
drhd4187c72010-08-30 22:15:45 +00003845 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003846 SQLITE_OPEN_READWRITE |
3847 SQLITE_OPEN_CREATE |
3848 SQLITE_OPEN_EXCLUSIVE |
3849 SQLITE_OPEN_DELETEONCLOSE |
3850 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003851 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003852 assert( pOp->p2>=0 );
drh4afdfa12018-12-31 16:36:42 +00003853 pCx = p->apCsr[pOp->p1];
3854 if( pCx ){
3855 /* If the ephermeral table is already open, erase all existing content
3856 ** so that the table is empty again, rather than creating a new table. */
dana5129722019-05-03 18:50:24 +00003857 assert( pCx->isEphemeral );
dan855b5d12019-06-26 21:04:30 +00003858 pCx->seqCount = 0;
3859 pCx->cacheStatus = CACHE_STALE;
dana5129722019-05-03 18:50:24 +00003860 if( pCx->pBtx ){
3861 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
3862 }
drhd0fb7962018-12-31 17:58:05 +00003863 }else{
3864 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3865 if( pCx==0 ) goto no_mem;
drhd0fb7962018-12-31 17:58:05 +00003866 pCx->isEphemeral = 1;
3867 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3868 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
3869 vfsFlags);
3870 if( rc==SQLITE_OK ){
3871 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drhc6b52df2002-01-04 03:09:29 +00003872 }
drhd0fb7962018-12-31 17:58:05 +00003873 if( rc==SQLITE_OK ){
3874 /* If a transient index is required, create it by calling
3875 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3876 ** opening it. If a transient table is required, just use the
3877 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3878 */
3879 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3880 assert( pOp->p4type==P4_KEYINFO );
3881 rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
3882 BTREE_BLOBKEY | pOp->p5);
3883 if( rc==SQLITE_OK ){
3884 assert( pCx->pgnoRoot==MASTER_ROOT+1 );
3885 assert( pKeyInfo->db==db );
3886 assert( pKeyInfo->enc==ENC(db) );
3887 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
3888 pKeyInfo, pCx->uc.pCursor);
3889 }
3890 pCx->isTable = 0;
3891 }else{
3892 pCx->pgnoRoot = MASTER_ROOT;
3893 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
3894 0, pCx->uc.pCursor);
3895 pCx->isTable = 1;
3896 }
3897 }
3898 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drh5e00f6c2001-09-13 13:46:56 +00003899 }
drh9467abf2016-02-17 18:44:11 +00003900 if( rc ) goto abort_due_to_error;
dan855b5d12019-06-26 21:04:30 +00003901 pCx->nullRow = 1;
dan5134d132011-09-02 10:31:11 +00003902 break;
3903}
3904
danfad9f9a2014-04-01 18:41:51 +00003905/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003906**
3907** This opcode works like OP_OpenEphemeral except that it opens
3908** a transient index that is specifically designed to sort large
3909** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003910**
3911** If argument P3 is non-zero, then it indicates that the sorter may
3912** assume that a stable sort considering the first P3 fields of each
3913** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003914*/
drhca892a72011-09-03 00:17:51 +00003915case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003916 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003917
drh399af1d2013-11-20 17:25:55 +00003918 assert( pOp->p1>=0 );
3919 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003920 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003921 if( pCx==0 ) goto no_mem;
3922 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003923 assert( pCx->pKeyInfo->db==db );
3924 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003925 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003926 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003927 break;
3928}
3929
dan78d58432014-03-25 15:04:07 +00003930/* Opcode: SequenceTest P1 P2 * * *
3931** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3932**
3933** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3934** to P2. Regardless of whether or not the jump is taken, increment the
3935** the sequence value.
3936*/
3937case OP_SequenceTest: {
3938 VdbeCursor *pC;
3939 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3940 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003941 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003942 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003943 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003944 }
drh5e00f6c2001-09-13 13:46:56 +00003945 break;
3946}
3947
drh5f612292014-02-08 23:20:32 +00003948/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003949** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003950**
3951** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003952** row of data. The content of that one row is the content of memory
3953** register P2. In other words, cursor P1 becomes an alias for the
3954** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003955**
drh2d8d7ce2010-02-15 15:17:05 +00003956** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003957** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003958** individual columns using the OP_Column opcode. The OP_Column opcode
3959** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003960**
3961** P3 is the number of fields in the records that will be stored by
3962** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003963*/
drh9cbf3422008-01-17 16:22:13 +00003964case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003965 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003966
drh653b82a2009-06-22 11:10:47 +00003967 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003968 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003969 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003970 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003971 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003972 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003973 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003974 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3975 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3976 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3977 ** which is a performance optimization */
3978 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003979 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003980 break;
3981}
3982
drh98757152008-01-09 23:04:12 +00003983/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003984**
3985** Close a cursor previously opened as P1. If P1 is not
3986** currently open, this instruction is a no-op.
3987*/
drh9cbf3422008-01-17 16:22:13 +00003988case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003989 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3990 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3991 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003992 break;
3993}
3994
drh97bae792015-06-05 15:59:57 +00003995#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3996/* Opcode: ColumnsUsed P1 * * P4 *
3997**
3998** This opcode (which only exists if SQLite was compiled with
3999** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4000** table or index for cursor P1 are used. P4 is a 64-bit integer
4001** (P4_INT64) in which the first 63 bits are one for each of the
4002** first 63 columns of the table or index that are actually used
4003** by the cursor. The high-order bit is set if any column after
4004** the 64th is used.
4005*/
4006case OP_ColumnsUsed: {
4007 VdbeCursor *pC;
4008 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004009 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00004010 pC->maskUsed = *(u64*)pOp->p4.pI64;
4011 break;
4012}
4013#endif
4014
drh8af3f772014-07-25 18:01:06 +00004015/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004016** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004017**
danielk1977b790c6c2008-04-18 10:25:24 +00004018** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004019** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004020** to an SQL index, then P3 is the first in an array of P4 registers
4021** that are used as an unpacked index key.
4022**
4023** Reposition cursor P1 so that it points to the smallest entry that
4024** is greater than or equal to the key value. If there are no records
4025** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004026**
drhb1d607d2015-11-05 22:30:54 +00004027** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4028** opcode will always land on a record that equally equals the key, or
4029** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
4030** opcode must be followed by an IdxLE opcode with the same arguments.
4031** The IdxLE opcode will be skipped if this opcode succeeds, but the
4032** IdxLE opcode will be used on subsequent loop iterations.
4033**
drh8af3f772014-07-25 18:01:06 +00004034** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00004035** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004036** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004037**
drh935850e2014-05-24 17:15:15 +00004038** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004039*/
drh8af3f772014-07-25 18:01:06 +00004040/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004041** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00004042**
danielk1977b790c6c2008-04-18 10:25:24 +00004043** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004044** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004045** to an SQL index, then P3 is the first in an array of P4 registers
4046** that are used as an unpacked index key.
4047**
4048** Reposition cursor P1 so that it points to the smallest entry that
4049** is greater than the key value. If there are no records greater than
4050** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00004051**
drh8af3f772014-07-25 18:01:06 +00004052** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004053** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004054** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004055**
drh935850e2014-05-24 17:15:15 +00004056** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00004057*/
drh8af3f772014-07-25 18:01:06 +00004058/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004059** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004060**
danielk1977b790c6c2008-04-18 10:25:24 +00004061** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004062** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004063** to an SQL index, then P3 is the first in an array of P4 registers
4064** that are used as an unpacked index key.
4065**
4066** Reposition cursor P1 so that it points to the largest entry that
4067** is less than the key value. If there are no records less than
4068** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00004069**
drh8af3f772014-07-25 18:01:06 +00004070** This opcode leaves the cursor configured to move in reverse order,
4071** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004072** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004073**
drh935850e2014-05-24 17:15:15 +00004074** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004075*/
drh8af3f772014-07-25 18:01:06 +00004076/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004077** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00004078**
danielk1977b790c6c2008-04-18 10:25:24 +00004079** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004080** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004081** to an SQL index, then P3 is the first in an array of P4 registers
4082** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00004083**
danielk1977b790c6c2008-04-18 10:25:24 +00004084** Reposition cursor P1 so that it points to the largest entry that
4085** is less than or equal to the key value. If there are no records
4086** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004087**
drh8af3f772014-07-25 18:01:06 +00004088** This opcode leaves the cursor configured to move in reverse order,
4089** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004090** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004091**
drhb1d607d2015-11-05 22:30:54 +00004092** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
4093** opcode will always land on a record that equally equals the key, or
4094** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
4095** opcode must be followed by an IdxGE opcode with the same arguments.
4096** The IdxGE opcode will be skipped if this opcode succeeds, but the
4097** IdxGE opcode will be used on subsequent loop iterations.
4098**
drh935850e2014-05-24 17:15:15 +00004099** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00004100*/
mistachkin758784d2018-07-25 15:12:29 +00004101case OP_SeekLT: /* jump, in3, group */
4102case OP_SeekLE: /* jump, in3, group */
4103case OP_SeekGE: /* jump, in3, group */
4104case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00004105 int res; /* Comparison result */
4106 int oc; /* Opcode */
4107 VdbeCursor *pC; /* The cursor to seek */
4108 UnpackedRecord r; /* The key to seek for */
4109 int nField; /* Number of columns or fields in the key */
4110 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00004111 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00004112
drh653b82a2009-06-22 11:10:47 +00004113 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00004114 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00004115 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004116 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004117 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00004118 assert( OP_SeekLE == OP_SeekLT+1 );
4119 assert( OP_SeekGE == OP_SeekLT+2 );
4120 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00004121 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00004122 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004123 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00004124 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00004125 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00004126#ifdef SQLITE_DEBUG
4127 pC->seekOp = pOp->opcode;
4128#endif
drhe0997b32015-03-20 14:57:50 +00004129
dana40cb962019-05-14 20:25:22 +00004130 pC->deferredMoveto = 0;
4131 pC->cacheStatus = CACHE_STALE;
drh3da046d2013-11-11 03:24:11 +00004132 if( pC->isTable ){
drh3e364802019-08-22 00:53:16 +00004133 u16 flags3, newType;
drhd6b79462015-11-07 01:19:00 +00004134 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00004135 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
4136 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00004137
drh3da046d2013-11-11 03:24:11 +00004138 /* The input value in P3 might be of any type: integer, real, string,
4139 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00004140 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00004141 pIn3 = &aMem[pOp->p3];
drh3e364802019-08-22 00:53:16 +00004142 flags3 = pIn3->flags;
4143 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00004144 applyNumericAffinity(pIn3, 0);
4145 }
drh3e364802019-08-22 00:53:16 +00004146 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
4147 newType = pIn3->flags; /* Record the type after applying numeric affinity */
4148 pIn3->flags = flags3; /* But convert the type back to its original */
drh959403f2008-12-12 17:56:16 +00004149
drh3da046d2013-11-11 03:24:11 +00004150 /* If the P3 value could not be converted into an integer without
4151 ** loss of information, then special processing is required... */
drh3e364802019-08-22 00:53:16 +00004152 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
4153 if( (newType & MEM_Real)==0 ){
4154 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
drh8616cff2019-07-13 16:15:23 +00004155 VdbeBranchTaken(1,2);
4156 goto jump_to_p2;
dan9edd8c12019-05-08 11:42:49 +00004157 }else{
dan873b0192019-05-09 11:19:27 +00004158 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4159 if( rc!=SQLITE_OK ) goto abort_due_to_error;
dan9edd8c12019-05-08 11:42:49 +00004160 goto seek_not_found;
4161 }
4162 }else
drh959403f2008-12-12 17:56:16 +00004163
danaa1776f2013-11-26 18:22:59 +00004164 /* If the approximation iKey is larger than the actual real search
4165 ** term, substitute >= for > and < for <=. e.g. if the search term
4166 ** is 4.9 and the integer approximation 5:
4167 **
4168 ** (x > 4.9) -> (x >= 5)
4169 ** (x <= 4.9) -> (x < 5)
4170 */
drh74eaba42014-09-18 17:52:15 +00004171 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004172 assert( OP_SeekGE==(OP_SeekGT-1) );
4173 assert( OP_SeekLT==(OP_SeekLE-1) );
4174 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
4175 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00004176 }
4177
4178 /* If the approximation iKey is smaller than the actual real search
4179 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00004180 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00004181 assert( OP_SeekLE==(OP_SeekLT+1) );
4182 assert( OP_SeekGT==(OP_SeekGE+1) );
4183 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4184 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004185 }
dan9edd8c12019-05-08 11:42:49 +00004186 }
drhc960dcb2015-11-20 19:22:01 +00004187 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004188 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004189 if( rc!=SQLITE_OK ){
4190 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004191 }
drhaa736092009-06-22 00:55:30 +00004192 }else{
drhd6b79462015-11-07 01:19:00 +00004193 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
4194 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
4195 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
4196 */
drhc960dcb2015-11-20 19:22:01 +00004197 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004198 eqOnly = 1;
4199 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4200 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4201 assert( pOp[1].p1==pOp[0].p1 );
4202 assert( pOp[1].p2==pOp[0].p2 );
4203 assert( pOp[1].p3==pOp[0].p3 );
4204 assert( pOp[1].p4.i==pOp[0].p4.i );
4205 }
4206
drh3da046d2013-11-11 03:24:11 +00004207 nField = pOp->p4.i;
4208 assert( pOp->p4type==P4_INT32 );
4209 assert( nField>0 );
4210 r.pKeyInfo = pC->pKeyInfo;
4211 r.nField = (u16)nField;
4212
4213 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004214 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004215 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004216 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004217 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004218 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004219 */
dan1fed5da2014-02-25 21:01:25 +00004220 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4221 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4222 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4223 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4224 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004225
4226 r.aMem = &aMem[pOp->p3];
4227#ifdef SQLITE_DEBUG
4228 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4229#endif
drh70528d72015-11-05 20:25:09 +00004230 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004231 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004232 if( rc!=SQLITE_OK ){
4233 goto abort_due_to_error;
4234 }
drhb1d607d2015-11-05 22:30:54 +00004235 if( eqOnly && r.eqSeen==0 ){
4236 assert( res!=0 );
4237 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004238 }
drh3da046d2013-11-11 03:24:11 +00004239 }
drh3da046d2013-11-11 03:24:11 +00004240#ifdef SQLITE_TEST
4241 sqlite3_search_count++;
4242#endif
drh4a1d3652014-02-14 15:13:36 +00004243 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4244 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004245 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004246 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4247 if( rc!=SQLITE_OK ){
4248 if( rc==SQLITE_DONE ){
4249 rc = SQLITE_OK;
4250 res = 1;
4251 }else{
4252 goto abort_due_to_error;
4253 }
4254 }
drh3da046d2013-11-11 03:24:11 +00004255 }else{
4256 res = 0;
4257 }
4258 }else{
drh4a1d3652014-02-14 15:13:36 +00004259 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4260 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004261 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004262 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4263 if( rc!=SQLITE_OK ){
4264 if( rc==SQLITE_DONE ){
4265 rc = SQLITE_OK;
4266 res = 1;
4267 }else{
4268 goto abort_due_to_error;
4269 }
4270 }
drh3da046d2013-11-11 03:24:11 +00004271 }else{
4272 /* res might be negative because the table is empty. Check to
4273 ** see if this is the case.
4274 */
drhc960dcb2015-11-20 19:22:01 +00004275 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004276 }
4277 }
drhb1d607d2015-11-05 22:30:54 +00004278seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004279 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004280 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004281 if( res ){
drhf56fa462015-04-13 21:39:54 +00004282 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004283 }else if( eqOnly ){
4284 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4285 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004286 }
drh5e00f6c2001-09-13 13:46:56 +00004287 break;
4288}
dan71c57db2016-07-09 20:23:55 +00004289
drh8c2b6d72018-06-05 20:45:20 +00004290/* Opcode: SeekHit P1 P2 * * *
4291** Synopsis: seekHit=P2
4292**
4293** Set the seekHit flag on cursor P1 to the value in P2.
4294** The seekHit flag is used by the IfNoHope opcode.
4295**
4296** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4297** either 0 or 1.
4298*/
4299case OP_SeekHit: {
4300 VdbeCursor *pC;
4301 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4302 pC = p->apCsr[pOp->p1];
4303 assert( pC!=0 );
4304 assert( pOp->p2==0 || pOp->p2==1 );
4305 pC->seekHit = pOp->p2 & 1;
4306 break;
4307}
4308
drh8cff69d2009-11-12 19:59:44 +00004309/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004310** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004311**
drh8cff69d2009-11-12 19:59:44 +00004312** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4313** P4>0 then register P3 is the first of P4 registers that form an unpacked
4314** record.
4315**
4316** Cursor P1 is on an index btree. If the record identified by P3 and P4
4317** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004318** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004319**
drhcefc87f2014-08-01 01:40:33 +00004320** This operation leaves the cursor in a state where it can be
4321** advanced in the forward direction. The Next instruction will work,
4322** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004323**
drh6f225d02013-10-26 13:36:51 +00004324** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004325*/
drh8cff69d2009-11-12 19:59:44 +00004326/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004327** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004328**
drh8cff69d2009-11-12 19:59:44 +00004329** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4330** P4>0 then register P3 is the first of P4 registers that form an unpacked
4331** record.
4332**
4333** Cursor P1 is on an index btree. If the record identified by P3 and P4
4334** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4335** does contain an entry whose prefix matches the P3/P4 record then control
4336** falls through to the next instruction and P1 is left pointing at the
4337** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004338**
drh8af3f772014-07-25 18:01:06 +00004339** This operation leaves the cursor in a state where it cannot be
4340** advanced in either direction. In other words, the Next and Prev
4341** opcodes do not work after this operation.
4342**
drh8c2b6d72018-06-05 20:45:20 +00004343** See also: Found, NotExists, NoConflict, IfNoHope
4344*/
4345/* Opcode: IfNoHope P1 P2 P3 P4 *
4346** Synopsis: key=r[P3@P4]
4347**
4348** Register P3 is the first of P4 registers that form an unpacked
4349** record.
4350**
4351** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4352** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4353** check to see if there is any entry in P1 that matches the
4354** prefix identified by P3 and P4. If no entry matches the prefix,
4355** jump to P2. Otherwise fall through.
4356**
4357** This opcode behaves like OP_NotFound if the seekHit
4358** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4359**
4360** This opcode is used in IN clause processing for a multi-column key.
4361** If an IN clause is attached to an element of the key other than the
4362** left-most element, and if there are no matches on the most recent
4363** seek over the whole key, then it might be that one of the key element
4364** to the left is prohibiting a match, and hence there is "no hope" of
4365** any match regardless of how many IN clause elements are checked.
4366** In such a case, we abandon the IN clause search early, using this
4367** opcode. The opcode name comes from the fact that the
4368** jump is taken if there is "no hope" of achieving a match.
4369**
4370** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004371*/
drh6f225d02013-10-26 13:36:51 +00004372/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004373** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004374**
4375** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4376** P4>0 then register P3 is the first of P4 registers that form an unpacked
4377** record.
4378**
4379** Cursor P1 is on an index btree. If the record identified by P3 and P4
4380** contains any NULL value, jump immediately to P2. If all terms of the
4381** record are not-NULL then a check is done to determine if any row in the
4382** P1 index btree has a matching key prefix. If there are no matches, jump
4383** immediately to P2. If there is a match, fall through and leave the P1
4384** cursor pointing to the matching row.
4385**
4386** This opcode is similar to OP_NotFound with the exceptions that the
4387** branch is always taken if any part of the search key input is NULL.
4388**
drh8af3f772014-07-25 18:01:06 +00004389** This operation leaves the cursor in a state where it cannot be
4390** advanced in either direction. In other words, the Next and Prev
4391** opcodes do not work after this operation.
4392**
drh6f225d02013-10-26 13:36:51 +00004393** See also: NotFound, Found, NotExists
4394*/
drh8c2b6d72018-06-05 20:45:20 +00004395case OP_IfNoHope: { /* jump, in3 */
4396 VdbeCursor *pC;
4397 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4398 pC = p->apCsr[pOp->p1];
4399 assert( pC!=0 );
4400 if( pC->seekHit ) break;
4401 /* Fall through into OP_NotFound */
4402}
drh6f225d02013-10-26 13:36:51 +00004403case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004404case OP_NotFound: /* jump, in3 */
4405case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004406 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004407 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004408 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004409 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004410 int res;
drha582b012016-12-21 19:45:54 +00004411 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004412 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004413 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004414
dan0ff297e2009-09-25 17:03:14 +00004415#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004416 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004417#endif
4418
drhaa736092009-06-22 00:55:30 +00004419 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004420 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004421 pC = p->apCsr[pOp->p1];
4422 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004423#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004424 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004425#endif
drh3c657212009-11-17 23:59:58 +00004426 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004427 assert( pC->eCurType==CURTYPE_BTREE );
4428 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004429 assert( pC->isTable==0 );
4430 if( pOp->p4.i>0 ){
4431 r.pKeyInfo = pC->pKeyInfo;
4432 r.nField = (u16)pOp->p4.i;
4433 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004434#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004435 for(ii=0; ii<r.nField; ii++){
4436 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004437 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004438 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004439 }
drh8aaf7bc2016-09-20 01:19:18 +00004440#endif
drh3da046d2013-11-11 03:24:11 +00004441 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004442 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004443 }else{
drhe46515b2017-05-19 22:51:00 +00004444 assert( pIn3->flags & MEM_Blob );
4445 rc = ExpandBlob(pIn3);
4446 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4447 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004448 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004449 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004450 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004451 }
dan1fed5da2014-02-25 21:01:25 +00004452 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004453 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004454 if( pOp->opcode==OP_NoConflict ){
4455 /* For the OP_NoConflict opcode, take the jump if any of the
4456 ** input fields are NULL, since any key with a NULL will not
4457 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004458 for(ii=0; ii<pIdxKey->nField; ii++){
4459 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004460 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004461 break;
drh6f225d02013-10-26 13:36:51 +00004462 }
4463 }
drh5e00f6c2001-09-13 13:46:56 +00004464 }
drhc960dcb2015-11-20 19:22:01 +00004465 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004466 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004467 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004468 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004469 }
4470 pC->seekResult = res;
4471 alreadyExists = (res==0);
4472 pC->nullRow = 1-alreadyExists;
4473 pC->deferredMoveto = 0;
4474 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004475 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004476 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004477 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004478 }else{
drhf56fa462015-04-13 21:39:54 +00004479 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4480 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004481 }
drh5e00f6c2001-09-13 13:46:56 +00004482 break;
4483}
4484
drheeb95652016-05-26 20:56:38 +00004485/* Opcode: SeekRowid P1 P2 P3 * *
4486** Synopsis: intkey=r[P3]
4487**
4488** P1 is the index of a cursor open on an SQL table btree (with integer
4489** keys). If register P3 does not contain an integer or if P1 does not
4490** contain a record with rowid P3 then jump immediately to P2.
4491** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4492** a record with rowid P3 then
4493** leave the cursor pointing at that record and fall through to the next
4494** instruction.
4495**
4496** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4497** the P3 register must be guaranteed to contain an integer value. With this
4498** opcode, register P3 might not contain an integer.
4499**
4500** The OP_NotFound opcode performs the same operation on index btrees
4501** (with arbitrary multi-value keys).
4502**
4503** This opcode leaves the cursor in a state where it cannot be advanced
4504** in either direction. In other words, the Next and Prev opcodes will
4505** not work following this opcode.
4506**
4507** See also: Found, NotFound, NoConflict, SeekRowid
4508*/
drh9cbf3422008-01-17 16:22:13 +00004509/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004510** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004511**
drh261c02d2013-10-25 14:46:15 +00004512** P1 is the index of a cursor open on an SQL table btree (with integer
4513** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004514** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4515** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4516** leave the cursor pointing at that record and fall through to the next
4517** instruction.
drh6b125452002-01-28 15:53:03 +00004518**
drheeb95652016-05-26 20:56:38 +00004519** The OP_SeekRowid opcode performs the same operation but also allows the
4520** P3 register to contain a non-integer value, in which case the jump is
4521** always taken. This opcode requires that P3 always contain an integer.
4522**
drh261c02d2013-10-25 14:46:15 +00004523** The OP_NotFound opcode performs the same operation on index btrees
4524** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004525**
drh8af3f772014-07-25 18:01:06 +00004526** This opcode leaves the cursor in a state where it cannot be advanced
4527** in either direction. In other words, the Next and Prev opcodes will
4528** not work following this opcode.
4529**
drheeb95652016-05-26 20:56:38 +00004530** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004531*/
drheeb95652016-05-26 20:56:38 +00004532case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004533 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004534 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004535 int res;
4536 u64 iKey;
4537
drh3c657212009-11-17 23:59:58 +00004538 pIn3 = &aMem[pOp->p3];
drh3242c692019-05-04 01:29:13 +00004539 testcase( pIn3->flags & MEM_Int );
4540 testcase( pIn3->flags & MEM_IntReal );
drhb29ef5e2019-10-07 01:05:57 +00004541 testcase( pIn3->flags & MEM_Real );
4542 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
drh169f0772019-05-02 21:36:26 +00004543 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
drhb29ef5e2019-10-07 01:05:57 +00004544 /* If pIn3->u.i does not contain an integer, compute iKey as the
4545 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
4546 ** into an integer without loss of information. Take care to avoid
4547 ** changing the datatype of pIn3, however, as it is used by other
4548 ** parts of the prepared statement. */
4549 Mem x = pIn3[0];
4550 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
4551 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
4552 iKey = x.u.i;
4553 goto notExistsWithKey;
drheeb95652016-05-26 20:56:38 +00004554 }
4555 /* Fall through into OP_NotExists */
4556case OP_NotExists: /* jump, in3 */
4557 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004558 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004559 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhb29ef5e2019-10-07 01:05:57 +00004560 iKey = pIn3->u.i;
4561notExistsWithKey:
drhaa736092009-06-22 00:55:30 +00004562 pC = p->apCsr[pOp->p1];
4563 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004564#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00004565 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004566#endif
drhaa736092009-06-22 00:55:30 +00004567 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004568 assert( pC->eCurType==CURTYPE_BTREE );
4569 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004570 assert( pCrsr!=0 );
4571 res = 0;
drh3da046d2013-11-11 03:24:11 +00004572 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004573 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004574 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004575 pC->nullRow = 0;
4576 pC->cacheStatus = CACHE_STALE;
4577 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004578 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004579 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004580 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004581 assert( rc==SQLITE_OK );
4582 if( pOp->p2==0 ){
4583 rc = SQLITE_CORRUPT_BKPT;
4584 }else{
4585 goto jump_to_p2;
4586 }
danc6157e12015-09-14 09:23:47 +00004587 }
drh9467abf2016-02-17 18:44:11 +00004588 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004589 break;
4590}
4591
drh4c583122008-01-04 22:01:03 +00004592/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004593** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004594**
drh4c583122008-01-04 22:01:03 +00004595** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004596** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004597** The sequence number on the cursor is incremented after this
4598** instruction.
drh4db38a72005-09-01 12:16:28 +00004599*/
drh27a348c2015-04-13 19:14:06 +00004600case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004601 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4602 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004603 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004604 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004605 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004606 break;
4607}
4608
4609
drh98757152008-01-09 23:04:12 +00004610/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004611** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004612**
drhf0863fe2005-06-12 21:35:51 +00004613** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004614** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004615** table that cursor P1 points to. The new record number is written
4616** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004617**
dan76d462e2009-08-30 11:42:51 +00004618** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4619** the largest previously generated record number. No new record numbers are
4620** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004621** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004622** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004623** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004624*/
drh27a348c2015-04-13 19:14:06 +00004625case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004626 i64 v; /* The new rowid */
4627 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4628 int res; /* Result of an sqlite3BtreeLast() */
4629 int cnt; /* Counter to limit the number of searches */
4630 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004631 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004632
drh856c1032009-06-02 15:21:42 +00004633 v = 0;
4634 res = 0;
drh27a348c2015-04-13 19:14:06 +00004635 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004636 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4637 pC = p->apCsr[pOp->p1];
4638 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004639 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004640 assert( pC->eCurType==CURTYPE_BTREE );
4641 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004642 {
drh5cf8e8c2002-02-19 22:42:05 +00004643 /* The next rowid or record number (different terms for the same
4644 ** thing) is obtained in a two-step algorithm.
4645 **
4646 ** First we attempt to find the largest existing rowid and add one
4647 ** to that. But if the largest existing rowid is already the maximum
4648 ** positive integer, we have to fall through to the second
4649 ** probabilistic algorithm
4650 **
4651 ** The second algorithm is to select a rowid at random and see if
4652 ** it already exists in the table. If it does not exist, we have
4653 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004654 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004655 */
drhaa736092009-06-22 00:55:30 +00004656 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004657
drh75f86a42005-02-17 00:03:06 +00004658#ifdef SQLITE_32BIT_ROWID
4659# define MAX_ROWID 0x7fffffff
4660#else
drhfe2093d2005-01-20 22:48:47 +00004661 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4662 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4663 ** to provide the constant while making all compilers happy.
4664 */
danielk197764202cf2008-11-17 15:31:47 +00004665# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004666#endif
drhfe2093d2005-01-20 22:48:47 +00004667
drh5cf8e8c2002-02-19 22:42:05 +00004668 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004669 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004670 if( rc!=SQLITE_OK ){
4671 goto abort_due_to_error;
4672 }
4673 if( res ){
4674 v = 1; /* IMP: R-61914-48074 */
4675 }else{
drhc960dcb2015-11-20 19:22:01 +00004676 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004677 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004678 if( v>=MAX_ROWID ){
4679 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004680 }else{
drhe0670b62014-02-12 21:31:12 +00004681 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004682 }
drh3fc190c2001-09-14 03:24:23 +00004683 }
drhe0670b62014-02-12 21:31:12 +00004684 }
drh205f48e2004-11-05 00:43:11 +00004685
4686#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004687 if( pOp->p3 ){
4688 /* Assert that P3 is a valid memory cell. */
4689 assert( pOp->p3>0 );
4690 if( p->pFrame ){
4691 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004692 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004693 assert( pOp->p3<=pFrame->nMem );
4694 pMem = &pFrame->aMem[pOp->p3];
4695 }else{
4696 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004697 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004698 pMem = &aMem[pOp->p3];
4699 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004700 }
drhe0670b62014-02-12 21:31:12 +00004701 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004702
drhe0670b62014-02-12 21:31:12 +00004703 REGISTER_TRACE(pOp->p3, pMem);
4704 sqlite3VdbeMemIntegerify(pMem);
4705 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4706 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004707 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004708 goto abort_due_to_error;
4709 }
4710 if( v<pMem->u.i+1 ){
4711 v = pMem->u.i + 1;
4712 }
4713 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004714 }
drhe0670b62014-02-12 21:31:12 +00004715#endif
drh5cf8e8c2002-02-19 22:42:05 +00004716 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004717 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004718 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004719 ** engine starts picking positive candidate ROWIDs at random until
4720 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004721 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4722 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004723 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004724 do{
4725 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004726 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004727 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004728 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004729 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004730 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004731 if( rc ) goto abort_due_to_error;
4732 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004733 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004734 goto abort_due_to_error;
4735 }
drh748a52c2010-09-01 11:50:08 +00004736 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004737 }
drha11846b2004-01-07 18:52:56 +00004738 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004739 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004740 }
drh4c583122008-01-04 22:01:03 +00004741 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004742 break;
4743}
4744
danielk19771f4aa332008-01-03 09:51:55 +00004745/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004746** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004747**
jplyon5a564222003-06-02 06:15:58 +00004748** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004749** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004750** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004751** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004752** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004753**
danielk19771f4aa332008-01-03 09:51:55 +00004754** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4755** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004756** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004757** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004758**
drheaf6ae22016-11-09 20:14:34 +00004759** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4760** run faster by avoiding an unnecessary seek on cursor P1. However,
4761** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4762** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004763**
4764** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4765** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4766** is part of an INSERT operation. The difference is only important to
4767** the update hook.
4768**
dan319eeb72011-03-19 08:38:50 +00004769** Parameter P4 may point to a Table structure, or may be NULL. If it is
4770** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4771** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004772**
drh93aed5a2008-01-16 17:46:38 +00004773** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4774** allocated, then ownership of P2 is transferred to the pseudo-cursor
4775** and register P2 becomes ephemeral. If the cursor is changed, the
4776** value of register P2 will then change. Make sure this does not
4777** cause any problems.)
4778**
drhf0863fe2005-06-12 21:35:51 +00004779** This instruction only works on tables. The equivalent instruction
4780** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004781*/
drh50ef6712019-02-22 23:29:56 +00004782case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00004783 Mem *pData; /* MEM cell holding data for the record to be inserted */
4784 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004785 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004786 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4787 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004788 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00004789 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004790
drha6c2ed92009-11-14 23:22:23 +00004791 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004792 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004793 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004794 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004795 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004796 assert( pC->eCurType==CURTYPE_BTREE );
4797 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004798 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004799 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004800 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00004801 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00004802
drh50ef6712019-02-22 23:29:56 +00004803 pKey = &aMem[pOp->p3];
4804 assert( pKey->flags & MEM_Int );
4805 assert( memIsValid(pKey) );
4806 REGISTER_TRACE(pOp->p3, pKey);
4807 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004808
drh9b1c62d2011-03-30 21:04:43 +00004809 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004810 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004811 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004812 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004813 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00004814 }else{
drh4ec6f3a2018-01-12 19:33:18 +00004815 pTab = 0;
drh74c33022016-03-30 12:56:55 +00004816 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004817 }
4818
drh9b1c62d2011-03-30 21:04:43 +00004819#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004820 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00004821 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00004822 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
4823 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
4824 }
drh4ec6f3a2018-01-12 19:33:18 +00004825 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
4826 /* Prevent post-update hook from running in cases when it should not */
4827 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00004828 }
dan46c47d42011-03-01 18:42:07 +00004829 }
dancb9a3642017-01-30 19:44:53 +00004830 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004831#endif
dan46c47d42011-03-01 18:42:07 +00004832
drha05a7222008-01-19 03:35:58 +00004833 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004834 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004835 assert( pData->flags & (MEM_Blob|MEM_Str) );
4836 x.pData = pData->z;
4837 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004838 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4839 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004840 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004841 }else{
drh8eeb4462016-05-21 20:03:42 +00004842 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004843 }
drh8eeb4462016-05-21 20:03:42 +00004844 x.pKey = 0;
4845 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004846 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004847 );
drha05a7222008-01-19 03:35:58 +00004848 pC->deferredMoveto = 0;
4849 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004850
drha05a7222008-01-19 03:35:58 +00004851 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004852 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00004853 if( pTab ){
4854 assert( db->xUpdateCallback!=0 );
4855 assert( pTab->aCol!=0 );
4856 db->xUpdateCallback(db->pUpdateArg,
4857 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
4858 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004859 }
drh5e00f6c2001-09-13 13:46:56 +00004860 break;
4861}
4862
dan438b8812015-09-15 15:55:15 +00004863/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004864**
drh5edc3122001-09-13 21:53:09 +00004865** Delete the record at which the P1 cursor is currently pointing.
4866**
drhe807bdb2016-01-21 17:06:33 +00004867** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4868** the cursor will be left pointing at either the next or the previous
4869** record in the table. If it is left pointing at the next record, then
4870** the next Next instruction will be a no-op. As a result, in this case
4871** it is ok to delete a record from within a Next loop. If
4872** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4873** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004874**
drhdef19e32016-01-27 16:26:25 +00004875** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4876** delete one of several associated with deleting a table row and all its
4877** associated index entries. Exactly one of those deletes is the "primary"
4878** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4879** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004880**
4881** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4882** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004883**
drh91fd4d42008-01-19 20:11:25 +00004884** P1 must not be pseudo-table. It has to be a real table with
4885** multiple rows.
4886**
drh5e769a52016-09-28 16:05:53 +00004887** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004888** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4889** have been positioned using OP_NotFound prior to invoking this opcode in
4890** this case. Specifically, if one is configured, the pre-update hook is
4891** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4892** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004893**
4894** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4895** of the memory cell that contains the value that the rowid of the row will
4896** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004897*/
drh9cbf3422008-01-17 16:22:13 +00004898case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004899 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004900 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004901 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004902 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004903
dan46c47d42011-03-01 18:42:07 +00004904 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004905 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4906 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004907 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004908 assert( pC->eCurType==CURTYPE_BTREE );
4909 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004910 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00004911 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00004912
drhb53a5a92014-10-12 22:37:22 +00004913#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004914 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4915 /* If p5 is zero, the seek operation that positioned the cursor prior to
4916 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4917 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004918 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan0971ef42019-05-16 20:13:32 +00004919 assert( CORRUPT_DB || pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004920 }
4921#endif
drh91fd4d42008-01-19 20:11:25 +00004922
dan438b8812015-09-15 15:55:15 +00004923 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4924 ** the name of the db to pass as to it. Also set local pTab to a copy
4925 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4926 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4927 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004928 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004929 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004930 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004931 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004932 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004933 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004934 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004935 }
drh74c33022016-03-30 12:56:55 +00004936 }else{
4937 zDb = 0; /* Not needed. Silence a compiler warning. */
4938 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004939 }
dan46c47d42011-03-01 18:42:07 +00004940
drh9b1c62d2011-03-30 21:04:43 +00004941#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004942 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004943 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4944 assert( !(opflags & OPFLAG_ISUPDATE)
4945 || HasRowid(pTab)==0
4946 || (aMem[pOp->p3].flags & MEM_Int)
4947 );
dan46c47d42011-03-01 18:42:07 +00004948 sqlite3VdbePreUpdateHook(p, pC,
4949 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004950 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004951 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004952 );
4953 }
dan46c47d42011-03-01 18:42:07 +00004954 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004955#endif
drhb53a5a92014-10-12 22:37:22 +00004956
drhdef19e32016-01-27 16:26:25 +00004957 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4958 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004959 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004960 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004961
4962#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004963 if( p->pFrame==0 ){
4964 if( pC->isEphemeral==0
4965 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4966 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4967 ){
4968 nExtraDelete++;
4969 }
4970 if( pOp->p2 & OPFLAG_NCHANGE ){
4971 nExtraDelete--;
4972 }
drhb89aeb62016-01-27 15:49:32 +00004973 }
4974#endif
4975
drhc960dcb2015-11-20 19:22:01 +00004976 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004977 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004978 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004979 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004980
drh91fd4d42008-01-19 20:11:25 +00004981 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004982 if( opflags & OPFLAG_NCHANGE ){
4983 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004984 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004985 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004986 pC->movetoTarget);
4987 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004988 }
drh5e00f6c2001-09-13 13:46:56 +00004989 }
dan438b8812015-09-15 15:55:15 +00004990
rdcb0c374f2004-02-20 22:53:38 +00004991 break;
4992}
drhb7f1d9a2009-09-08 02:27:58 +00004993/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004994**
drhb7f1d9a2009-09-08 02:27:58 +00004995** The value of the change counter is copied to the database handle
4996** change counter (returned by subsequent calls to sqlite3_changes()).
4997** Then the VMs internal change counter resets to 0.
4998** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004999*/
drh9cbf3422008-01-17 16:22:13 +00005000case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00005001 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00005002 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00005003 break;
5004}
5005
drh1153c7b2013-11-01 22:02:56 +00005006/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00005007** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00005008**
drh1153c7b2013-11-01 22:02:56 +00005009** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00005010** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00005011** the sorter cursor currently points to. Only the first P4 fields
5012** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00005013**
5014** If either P3 or the sorter contains a NULL in one of their significant
5015** fields (not counting the P4 fields at the end which are ignored) then
5016** the comparison is assumed to be equal.
5017**
5018** Fall through to next instruction if the two records compare equal to
5019** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00005020*/
5021case OP_SorterCompare: {
5022 VdbeCursor *pC;
5023 int res;
drhac502322014-07-30 13:56:48 +00005024 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00005025
5026 pC = p->apCsr[pOp->p1];
5027 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00005028 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00005029 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00005030 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00005031 res = 0;
drhac502322014-07-30 13:56:48 +00005032 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00005033 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00005034 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005035 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00005036 break;
5037};
5038
drh6cf4a7d2014-10-13 13:00:58 +00005039/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005040** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00005041**
5042** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00005043** Then clear the column header cache on cursor P3.
5044**
5045** This opcode is normally use to move a record out of the sorter and into
5046** a register that is the source for a pseudo-table cursor created using
5047** OpenPseudo. That pseudo-table cursor is the one that is identified by
5048** parameter P3. Clearing the P3 column cache as part of this opcode saves
5049** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00005050*/
5051case OP_SorterData: {
5052 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00005053
dan5134d132011-09-02 10:31:11 +00005054 pOut = &aMem[pOp->p2];
5055 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00005056 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00005057 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00005058 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00005059 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00005060 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00005061 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00005062 break;
5063}
5064
drhe7b554d2017-01-09 15:44:25 +00005065/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005066** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00005067**
drh9057fc72016-11-25 19:32:32 +00005068** Write into register P2 the complete row content for the row at
5069** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00005070** There is no interpretation of the data.
5071** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00005072** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00005073**
drh9057fc72016-11-25 19:32:32 +00005074** If cursor P1 is an index, then the content is the key of the row.
5075** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00005076**
drhde4fcfd2008-01-19 23:50:26 +00005077** If the P1 cursor must be pointing to a valid row (not a NULL row)
5078** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00005079**
drh8cdafc32018-05-31 19:00:20 +00005080** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00005081** into the database page. That means that the content of the output
5082** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00005083** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00005084** position in order that they can write to the same table. If P3==0
5085** then a copy of the data is made into memory. P3!=0 is faster, but
5086** P3==0 is safer.
5087**
5088** If P3!=0 then the content of the P2 register is unsuitable for use
5089** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00005090** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00005091** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00005092*/
danielk1977a7a8e142008-02-13 18:25:27 +00005093case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00005094 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00005095 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00005096 u32 n;
drh70ce3f02003-04-15 19:22:22 +00005097
drhe7b554d2017-01-09 15:44:25 +00005098 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00005099
drh653b82a2009-06-22 11:10:47 +00005100 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5101 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00005102 assert( pC!=0 );
5103 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00005104 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00005105 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00005106 assert( pC->uc.pCursor!=0 );
5107 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00005108
drh9057fc72016-11-25 19:32:32 +00005109 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00005110 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5111 ** that might invalidate the cursor.
5112 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00005113 ** would fail. Should this ever change (because of changes in the code
5114 ** generator) then the fix would be to insert a call to
5115 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00005116 */
5117 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00005118 assert( sqlite3BtreeCursorIsValid(pCrsr) );
5119#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00005120 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00005121 if( rc!=SQLITE_OK ) goto abort_due_to_error;
5122#endif
drh9a65f2c2009-06-22 19:05:40 +00005123
drha7c90c42016-06-04 20:37:10 +00005124 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00005125 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00005126 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00005127 }
drh722246e2014-10-07 23:02:24 +00005128 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00005129 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00005130 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00005131 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00005132 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00005133 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00005134 break;
5135}
5136
drh2133d822008-01-03 18:44:59 +00005137/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005138** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005139**
drh2133d822008-01-03 18:44:59 +00005140** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00005141** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00005142**
5143** P1 can be either an ordinary table or a virtual table. There used to
5144** be a separate OP_VRowid opcode for use with virtual tables, but this
5145** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00005146*/
drh27a348c2015-04-13 19:14:06 +00005147case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00005148 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00005149 i64 v;
drh856c1032009-06-02 15:21:42 +00005150 sqlite3_vtab *pVtab;
5151 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00005152
drh27a348c2015-04-13 19:14:06 +00005153 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005154 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5155 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005156 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005157 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00005158 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00005159 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00005160 break;
5161 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00005162 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00005163#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00005164 }else if( pC->eCurType==CURTYPE_VTAB ){
5165 assert( pC->uc.pVCur!=0 );
5166 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00005167 pModule = pVtab->pModule;
5168 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00005169 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00005170 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00005171 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00005172#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00005173 }else{
drhc960dcb2015-11-20 19:22:01 +00005174 assert( pC->eCurType==CURTYPE_BTREE );
5175 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00005176 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00005177 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00005178 if( pC->nullRow ){
5179 pOut->flags = MEM_Null;
5180 break;
5181 }
drha7c90c42016-06-04 20:37:10 +00005182 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00005183 }
drh4c583122008-01-04 22:01:03 +00005184 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005185 break;
5186}
5187
drh9cbf3422008-01-17 16:22:13 +00005188/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005189**
5190** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005191** that occur while the cursor is on the null row will always
5192** write a NULL.
drh17f71932002-02-21 12:01:27 +00005193*/
drh9cbf3422008-01-17 16:22:13 +00005194case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005195 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005196
drh653b82a2009-06-22 11:10:47 +00005197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5198 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005199 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00005200 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005201 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005202 if( pC->eCurType==CURTYPE_BTREE ){
5203 assert( pC->uc.pCursor!=0 );
5204 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005205 }
drhcf025a82018-06-07 18:01:21 +00005206#ifdef SQLITE_DEBUG
5207 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5208#endif
drh17f71932002-02-21 12:01:27 +00005209 break;
5210}
5211
drh86b40df2017-08-01 19:53:43 +00005212/* Opcode: SeekEnd P1 * * * *
5213**
5214** Position cursor P1 at the end of the btree for the purpose of
5215** appending a new entry onto the btree.
5216**
5217** It is assumed that the cursor is used only for appending and so
5218** if the cursor is valid, then the cursor must already be pointing
5219** at the end of the btree and so no changes are made to
5220** the cursor.
5221*/
5222/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005223**
drh8af3f772014-07-25 18:01:06 +00005224** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005225** will refer to the last entry in the database table or index.
5226** If the table or index is empty and P2>0, then jump immediately to P2.
5227** If P2 is 0 or if the table or index is not empty, fall through
5228** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005229**
5230** This opcode leaves the cursor configured to move in reverse order,
5231** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005232** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005233*/
drh86b40df2017-08-01 19:53:43 +00005234case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005235case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005236 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005237 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005238 int res;
drh9562b552002-02-19 15:00:07 +00005239
drh653b82a2009-06-22 11:10:47 +00005240 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5241 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005242 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005243 assert( pC->eCurType==CURTYPE_BTREE );
5244 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005245 res = 0;
drh3da046d2013-11-11 03:24:11 +00005246 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005247#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005248 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005249#endif
drh86b40df2017-08-01 19:53:43 +00005250 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005251 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005252 pC->seekResult = -1;
5253 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5254 break;
5255 }
5256 }
5257 rc = sqlite3BtreeLast(pCrsr, &res);
5258 pC->nullRow = (u8)res;
5259 pC->deferredMoveto = 0;
5260 pC->cacheStatus = CACHE_STALE;
5261 if( rc ) goto abort_due_to_error;
5262 if( pOp->p2>0 ){
5263 VdbeBranchTaken(res!=0,2);
5264 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005265 }
5266 break;
5267}
5268
drh5e98e832017-02-17 19:24:06 +00005269/* Opcode: IfSmaller P1 P2 P3 * *
5270**
5271** Estimate the number of rows in the table P1. Jump to P2 if that
5272** estimate is less than approximately 2**(0.1*P3).
5273*/
5274case OP_IfSmaller: { /* jump */
5275 VdbeCursor *pC;
5276 BtCursor *pCrsr;
5277 int res;
5278 i64 sz;
5279
5280 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5281 pC = p->apCsr[pOp->p1];
5282 assert( pC!=0 );
5283 pCrsr = pC->uc.pCursor;
5284 assert( pCrsr );
5285 rc = sqlite3BtreeFirst(pCrsr, &res);
5286 if( rc ) goto abort_due_to_error;
5287 if( res==0 ){
5288 sz = sqlite3BtreeRowCountEst(pCrsr);
5289 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5290 }
5291 VdbeBranchTaken(res!=0,2);
5292 if( res ) goto jump_to_p2;
5293 break;
5294}
5295
drh0342b1f2005-09-01 03:07:44 +00005296
drh6bd4dc62016-12-23 16:05:22 +00005297/* Opcode: SorterSort P1 P2 * * *
5298**
5299** After all records have been inserted into the Sorter object
5300** identified by P1, invoke this opcode to actually do the sorting.
5301** Jump to P2 if there are no records to be sorted.
5302**
5303** This opcode is an alias for OP_Sort and OP_Rewind that is used
5304** for Sorter objects.
5305*/
drh9cbf3422008-01-17 16:22:13 +00005306/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005307**
5308** This opcode does exactly the same thing as OP_Rewind except that
5309** it increments an undocumented global variable used for testing.
5310**
5311** Sorting is accomplished by writing records into a sorting index,
5312** then rewinding that index and playing it back from beginning to
5313** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5314** rewinding so that the global variable will be incremented and
5315** regression tests can determine whether or not the optimizer is
5316** correctly optimizing out sorts.
5317*/
drhc6aff302011-09-01 15:32:47 +00005318case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005319case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005320#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005321 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005322 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005323#endif
drh9b47ee32013-08-20 03:13:51 +00005324 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005325 /* Fall through into OP_Rewind */
5326}
drh038ebf62019-03-29 15:21:22 +00005327/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005328**
drhf0863fe2005-06-12 21:35:51 +00005329** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005330** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005331** If the table or index is empty, jump immediately to P2.
5332** If the table or index is not empty, fall through to the following
5333** instruction.
drh8af3f772014-07-25 18:01:06 +00005334**
5335** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005336** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005337** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005338*/
drh9cbf3422008-01-17 16:22:13 +00005339case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005340 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005341 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005342 int res;
drh5e00f6c2001-09-13 13:46:56 +00005343
drh653b82a2009-06-22 11:10:47 +00005344 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00005345 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00005346 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005347 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005348 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005349 res = 1;
drh8af3f772014-07-25 18:01:06 +00005350#ifdef SQLITE_DEBUG
5351 pC->seekOp = OP_Rewind;
5352#endif
dan689ab892011-08-12 15:02:00 +00005353 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005354 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005355 }else{
drhc960dcb2015-11-20 19:22:01 +00005356 assert( pC->eCurType==CURTYPE_BTREE );
5357 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005358 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005359 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00005360 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005361 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005362 }
drh9467abf2016-02-17 18:44:11 +00005363 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005364 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005365 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005366 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005367 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005368 break;
5369}
5370
drh0fd61352014-02-07 02:29:45 +00005371/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005372**
5373** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005374** table or index. If there are no more key/value pairs then fall through
5375** to the following instruction. But if the cursor advance was successful,
5376** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005377**
drh5dad9a32014-07-25 18:37:42 +00005378** The Next opcode is only valid following an SeekGT, SeekGE, or
5379** OP_Rewind opcode used to position the cursor. Next is not allowed
5380** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005381**
drhf93cd942013-11-21 03:12:25 +00005382** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5383** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005384**
drhe39a7322014-02-03 14:04:11 +00005385** The P3 value is a hint to the btree implementation. If P3==1, that
5386** means P1 is an SQL index and that this instruction could have been
5387** omitted if that index had been unique. P3 is usually 0. P3 is
5388** always either 0 or 1.
5389**
dana205a482011-08-27 18:48:57 +00005390** P4 is always of type P4_ADVANCE. The function pointer points to
5391** sqlite3BtreeNext().
5392**
drhafc266a2010-03-31 17:47:44 +00005393** If P5 is positive and the jump is taken, then event counter
5394** number P5-1 in the prepared statement is incremented.
5395**
drhf1949b62018-06-07 17:32:59 +00005396** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005397*/
drh0fd61352014-02-07 02:29:45 +00005398/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005399**
5400** Back up cursor P1 so that it points to the previous key/data pair in its
5401** table or index. If there is no previous key/value pairs then fall through
5402** to the following instruction. But if the cursor backup was successful,
5403** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005404**
drh8af3f772014-07-25 18:01:06 +00005405**
drh5dad9a32014-07-25 18:37:42 +00005406** The Prev opcode is only valid following an SeekLT, SeekLE, or
5407** OP_Last opcode used to position the cursor. Prev is not allowed
5408** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005409**
drhf93cd942013-11-21 03:12:25 +00005410** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5411** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005412**
drhe39a7322014-02-03 14:04:11 +00005413** The P3 value is a hint to the btree implementation. If P3==1, that
5414** means P1 is an SQL index and that this instruction could have been
5415** omitted if that index had been unique. P3 is usually 0. P3 is
5416** always either 0 or 1.
5417**
dana205a482011-08-27 18:48:57 +00005418** P4 is always of type P4_ADVANCE. The function pointer points to
5419** sqlite3BtreePrevious().
5420**
drhafc266a2010-03-31 17:47:44 +00005421** If P5 is positive and the jump is taken, then event counter
5422** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005423*/
drh6bd4dc62016-12-23 16:05:22 +00005424/* Opcode: SorterNext P1 P2 * * P5
5425**
5426** This opcode works just like OP_Next except that P1 must be a
5427** sorter object for which the OP_SorterSort opcode has been
5428** invoked. This opcode advances the cursor to the next sorted
5429** record, or jumps to P2 if there are no more sorted records.
5430*/
drhf93cd942013-11-21 03:12:25 +00005431case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005432 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005433
drhf93cd942013-11-21 03:12:25 +00005434 pC = p->apCsr[pOp->p1];
5435 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005436 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005437 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005438case OP_Prev: /* jump */
5439case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005440 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005441 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005442 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005443 assert( pC!=0 );
5444 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005445 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005446 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5447 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005448
drhcf025a82018-06-07 18:01:21 +00005449 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005450 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005451 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005452 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drh790b37a2019-08-27 17:01:07 +00005453 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
5454 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
5455 || pC->seekOp==OP_IfNoHope);
drhf1949b62018-06-07 17:32:59 +00005456 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005457 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drh790b37a2019-08-27 17:01:07 +00005458 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
drhcf025a82018-06-07 18:01:21 +00005459 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005460
drh2ab792e2017-05-30 18:34:07 +00005461 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005462next_tail:
drha3460582008-07-11 21:02:53 +00005463 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005464 VdbeBranchTaken(rc==SQLITE_OK,2);
5465 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005466 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005467 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005468#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005469 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005470#endif
drhf56fa462015-04-13 21:39:54 +00005471 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005472 }
drh2ab792e2017-05-30 18:34:07 +00005473 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5474 rc = SQLITE_OK;
5475 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005476 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005477}
5478
drh9b4eaeb2016-11-09 00:10:33 +00005479/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005480** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005481**
drhef8662b2011-06-20 21:47:58 +00005482** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005483** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005484** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005485**
drhfb8c56f2016-11-09 01:19:25 +00005486** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005487** key of reg(P2). In that case, P3 is the index of the first register
5488** for the unpacked key. The availability of the unpacked key can sometimes
5489** be an optimization.
5490**
5491** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5492** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005493**
mistachkin21a919f2014-02-07 03:28:02 +00005494** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5495** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5496** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005497**
drheaf6ae22016-11-09 20:14:34 +00005498** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5499** run faster by avoiding an unnecessary seek on cursor P1. However,
5500** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5501** seeks on the cursor or if the most recent seek used a key equivalent
5502** to P2.
drh0fd61352014-02-07 02:29:45 +00005503**
drhf0863fe2005-06-12 21:35:51 +00005504** This instruction only works for indices. The equivalent instruction
5505** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005506*/
drhf013e202016-10-15 18:37:05 +00005507/* Opcode: SorterInsert P1 P2 * * *
5508** Synopsis: key=r[P2]
5509**
5510** Register P2 holds an SQL index key made using the
5511** MakeRecord instructions. This opcode writes that key
5512** into the sorter P1. Data for the entry is nil.
5513*/
drhca892a72011-09-03 00:17:51 +00005514case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005515case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005516 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005517 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005518
drh653b82a2009-06-22 11:10:47 +00005519 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5520 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005521 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005522 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005523 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005524 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005525 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005526 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005527 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005528 assert( pC->isTable==0 );
5529 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005530 if( rc ) goto abort_due_to_error;
5531 if( pOp->opcode==OP_SorterInsert ){
5532 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5533 }else{
drh8eeb4462016-05-21 20:03:42 +00005534 x.nKey = pIn2->n;
5535 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005536 x.aMem = aMem + pOp->p3;
5537 x.nMem = (u16)pOp->p4.i;
5538 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005539 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005540 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5541 );
5542 assert( pC->deferredMoveto==0 );
5543 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005544 }
drh9467abf2016-02-17 18:44:11 +00005545 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005546 break;
5547}
5548
drhd1d38482008-10-07 23:46:38 +00005549/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005550** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005551**
drhe14006d2008-03-25 17:23:32 +00005552** The content of P3 registers starting at register P2 form
5553** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005554** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005555*/
drhe14006d2008-03-25 17:23:32 +00005556case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005557 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005558 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005559 int res;
5560 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005561
drhe14006d2008-03-25 17:23:32 +00005562 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005563 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005564 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5565 pC = p->apCsr[pOp->p1];
5566 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005567 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005568 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005569 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005570 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005571 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005572 r.pKeyInfo = pC->pKeyInfo;
5573 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005574 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005575 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005576 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005577 if( rc ) goto abort_due_to_error;
5578 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005579 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005580 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005581 }
drh3da046d2013-11-11 03:24:11 +00005582 assert( pC->deferredMoveto==0 );
5583 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005584 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005585 break;
5586}
5587
drh170ad682017-06-02 15:44:22 +00005588/* Opcode: DeferredSeek P1 * P3 P4 *
5589** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005590**
5591** P1 is an open index cursor and P3 is a cursor on the corresponding
5592** table. This opcode does a deferred seek of the P3 table cursor
5593** to the row that corresponds to the current row of P1.
5594**
5595** This is a deferred seek. Nothing actually happens until
5596** the cursor is used to read a record. That way, if no reads
5597** occur, no unnecessary I/O happens.
5598**
5599** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005600** one entry for each column in the P3 table. If array entry a(i)
5601** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005602** equivalent to performing the deferred seek and then reading column i
5603** from P1. This information is stored in P3 and used to redirect
5604** reads against P3 over to P1, thus possibly avoiding the need to
5605** seek and read cursor P3.
5606*/
drh2133d822008-01-03 18:44:59 +00005607/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005608** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005609**
drh2133d822008-01-03 18:44:59 +00005610** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005611** the end of the index key pointed to by cursor P1. This integer should be
5612** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005613**
drh9437bd22009-02-01 00:29:56 +00005614** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005615*/
drh170ad682017-06-02 15:44:22 +00005616case OP_DeferredSeek:
5617case OP_IdxRowid: { /* out2 */
5618 VdbeCursor *pC; /* The P1 index cursor */
5619 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5620 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005621
drh653b82a2009-06-22 11:10:47 +00005622 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5623 pC = p->apCsr[pOp->p1];
5624 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005625 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005626 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005627 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005628 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005629 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5630
5631 /* The IdxRowid and Seek opcodes are combined because of the commonality
5632 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5633 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005634
5635 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005636 ** out from under the cursor. That will never happens for an IdxRowid
5637 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005638 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5639
drh3da046d2013-11-11 03:24:11 +00005640 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005641 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005642 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005643 if( rc!=SQLITE_OK ){
5644 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005645 }
drh170ad682017-06-02 15:44:22 +00005646 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005647 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5648 pTabCur = p->apCsr[pOp->p3];
5649 assert( pTabCur!=0 );
5650 assert( pTabCur->eCurType==CURTYPE_BTREE );
5651 assert( pTabCur->uc.pCursor!=0 );
5652 assert( pTabCur->isTable );
5653 pTabCur->nullRow = 0;
5654 pTabCur->movetoTarget = rowid;
5655 pTabCur->deferredMoveto = 1;
5656 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5657 pTabCur->aAltMap = pOp->p4.ai;
5658 pTabCur->pAltCursor = pC;
5659 }else{
5660 pOut = out2Prerelease(p, pOp);
5661 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005662 }
5663 }else{
5664 assert( pOp->opcode==OP_IdxRowid );
5665 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005666 }
5667 break;
5668}
5669
danielk197761dd5832008-04-18 11:31:12 +00005670/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005671** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005672**
danielk197761dd5832008-04-18 11:31:12 +00005673** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005674** key that omits the PRIMARY KEY. Compare this key value against the index
5675** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5676** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005677**
danielk197761dd5832008-04-18 11:31:12 +00005678** If the P1 index entry is greater than or equal to the key value
5679** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005680*/
5681/* Opcode: IdxGT P1 P2 P3 P4 P5
5682** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005683**
drh4a1d3652014-02-14 15:13:36 +00005684** The P4 register values beginning with P3 form an unpacked index
5685** key that omits the PRIMARY KEY. Compare this key value against the index
5686** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5687** fields at the end.
5688**
5689** If the P1 index entry is greater than the key value
5690** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005691*/
drh3bb9b932010-08-06 02:10:00 +00005692/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005693** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005694**
danielk197761dd5832008-04-18 11:31:12 +00005695** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005696** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5697** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5698** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005699**
danielk197761dd5832008-04-18 11:31:12 +00005700** If the P1 index entry is less than the key value then jump to P2.
5701** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005702*/
drh4a1d3652014-02-14 15:13:36 +00005703/* Opcode: IdxLE P1 P2 P3 P4 P5
5704** Synopsis: key=r[P3@P4]
5705**
5706** The P4 register values beginning with P3 form an unpacked index
5707** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5708** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5709** ROWID on the P1 index.
5710**
5711** If the P1 index entry is less than or equal to the key value then jump
5712** to P2. Otherwise fall through to the next instruction.
5713*/
5714case OP_IdxLE: /* jump */
5715case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005716case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005717case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005718 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005719 int res;
5720 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005721
drh653b82a2009-06-22 11:10:47 +00005722 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5723 pC = p->apCsr[pOp->p1];
5724 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005725 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005726 assert( pC->eCurType==CURTYPE_BTREE );
5727 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005728 assert( pC->deferredMoveto==0 );
5729 assert( pOp->p5==0 || pOp->p5==1 );
5730 assert( pOp->p4type==P4_INT32 );
5731 r.pKeyInfo = pC->pKeyInfo;
5732 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005733 if( pOp->opcode<OP_IdxLT ){
5734 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005735 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005736 }else{
drh4a1d3652014-02-14 15:13:36 +00005737 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005738 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005739 }
5740 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005741#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00005742 {
5743 int i;
5744 for(i=0; i<r.nField; i++){
5745 assert( memIsValid(&r.aMem[i]) );
5746 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
5747 }
5748 }
drh2b4ded92010-09-27 21:09:31 +00005749#endif
drh2dc06482013-12-11 00:59:10 +00005750 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005751 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005752 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5753 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5754 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005755 res = -res;
5756 }else{
drh4a1d3652014-02-14 15:13:36 +00005757 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005758 res++;
5759 }
drh688852a2014-02-17 22:40:43 +00005760 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005761 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005762 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005763 break;
5764}
5765
drh98757152008-01-09 23:04:12 +00005766/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005767**
5768** Delete an entire database table or index whose root page in the database
5769** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005770**
drh98757152008-01-09 23:04:12 +00005771** The table being destroyed is in the main database file if P3==0. If
5772** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005773** that is used to store tables create using CREATE TEMPORARY TABLE.
5774**
drh205f48e2004-11-05 00:43:11 +00005775** If AUTOVACUUM is enabled then it is possible that another root page
5776** might be moved into the newly deleted root page in order to keep all
5777** root pages contiguous at the beginning of the database. The former
5778** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005779** is stored in register P2. If no page movement was required (because the
5780** table being dropped was already the last one in the database) then a
5781** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5782** is stored in register P2.
5783**
5784** This opcode throws an error if there are any active reader VMs when
5785** it is invoked. This is done to avoid the difficulty associated with
5786** updating existing cursors when a root page is moved in an AUTOVACUUM
5787** database. This error is thrown even if the database is not an AUTOVACUUM
5788** db in order to avoid introducing an incompatibility between autovacuum
5789** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005790**
drhb19a2bc2001-09-16 00:13:26 +00005791** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005792*/
drh27a348c2015-04-13 19:14:06 +00005793case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005794 int iMoved;
drh856c1032009-06-02 15:21:42 +00005795 int iDb;
drh3a949872012-09-18 13:20:13 +00005796
drh4031baf2018-05-28 17:31:20 +00005797 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005798 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005799 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005800 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005801 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005802 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005803 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005804 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005805 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005806 }else{
drh856c1032009-06-02 15:21:42 +00005807 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005808 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005809 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005810 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005811 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005812 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005813 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005814#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005815 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005816 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5817 /* All OP_Destroy operations occur on the same btree */
5818 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5819 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005820 }
drh3765df42006-06-28 18:18:09 +00005821#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005822 }
drh5e00f6c2001-09-13 13:46:56 +00005823 break;
5824}
5825
danielk1977c7af4842008-10-27 13:59:33 +00005826/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005827**
5828** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005829** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005830** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005831**
drhf57b3392001-10-08 13:22:32 +00005832** The table being clear is in the main database file if P2==0. If
5833** P2==1 then the table to be clear is in the auxiliary database file
5834** that is used to store tables create using CREATE TEMPORARY TABLE.
5835**
shanebe217792009-03-05 04:20:31 +00005836** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005837** intkey table (an SQL table, not an index). In this case the row change
5838** count is incremented by the number of rows in the table being cleared.
5839** If P3 is greater than zero, then the value stored in register P3 is
5840** also incremented by the number of rows in the table being cleared.
5841**
drhb19a2bc2001-09-16 00:13:26 +00005842** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005843*/
drh9cbf3422008-01-17 16:22:13 +00005844case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005845 int nChange;
5846
drh4031baf2018-05-28 17:31:20 +00005847 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005848 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005849 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005850 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005851 rc = sqlite3BtreeClearTable(
5852 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5853 );
5854 if( pOp->p3 ){
5855 p->nChange += nChange;
5856 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005857 assert( memIsValid(&aMem[pOp->p3]) );
5858 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005859 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005860 }
5861 }
drh9467abf2016-02-17 18:44:11 +00005862 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005863 break;
5864}
5865
drh65ea12c2014-03-19 17:41:36 +00005866/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005867**
drh65ea12c2014-03-19 17:41:36 +00005868** Delete all contents from the ephemeral table or sorter
5869** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005870**
drh65ea12c2014-03-19 17:41:36 +00005871** This opcode only works for cursors used for sorting and
5872** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005873*/
drh65ea12c2014-03-19 17:41:36 +00005874case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005875 VdbeCursor *pC;
5876
5877 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5878 pC = p->apCsr[pOp->p1];
5879 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005880 if( isSorter(pC) ){
5881 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005882 }else{
drhc960dcb2015-11-20 19:22:01 +00005883 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005884 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005885 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005886 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005887 }
drh079a3072014-03-19 14:10:55 +00005888 break;
5889}
5890
drh0f3f7662017-08-18 14:34:28 +00005891/* Opcode: CreateBtree P1 P2 P3 * *
5892** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005893**
drh0f3f7662017-08-18 14:34:28 +00005894** Allocate a new b-tree in the main database file if P1==0 or in the
5895** TEMP database file if P1==1 or in an attached database if
5896** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00005897** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00005898** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005899*/
drh0f3f7662017-08-18 14:34:28 +00005900case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005901 int pgno;
drh234c39d2004-07-24 03:30:47 +00005902 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005903
drh4031baf2018-05-28 17:31:20 +00005904 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00005905 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005906 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005907 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005908 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005909 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005910 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005911 pDb = &db->aDb[pOp->p1];
5912 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005913 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005914 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005915 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005916 break;
5917}
5918
drh4a54bb52017-02-18 15:58:52 +00005919/* Opcode: SqlExec * * * P4 *
5920**
5921** Run the SQL statement or statements specified in the P4 string.
5922*/
5923case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00005924 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00005925 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005926 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005927 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005928 if( rc ) goto abort_due_to_error;
5929 break;
5930}
5931
drh22645842011-03-24 01:34:03 +00005932/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005933**
5934** Read and parse all entries from the SQLITE_MASTER table of database P1
drh1595abc2018-08-14 19:27:51 +00005935** that match the WHERE clause P4. If P4 is a NULL pointer, then the
5936** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00005937**
5938** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005939** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005940*/
drh9cbf3422008-01-17 16:22:13 +00005941case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005942 int iDb;
5943 const char *zMaster;
5944 char *zSql;
5945 InitData initData;
5946
drhbdaec522011-04-04 00:14:43 +00005947 /* Any prepared statement that invokes this opcode will hold mutexes
5948 ** on every btree. This is a prerequisite for invoking
5949 ** sqlite3InitCallback().
5950 */
5951#ifdef SQLITE_DEBUG
5952 for(iDb=0; iDb<db->nDb; iDb++){
5953 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5954 }
5955#endif
drhbdaec522011-04-04 00:14:43 +00005956
drh856c1032009-06-02 15:21:42 +00005957 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005958 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005959 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
dane325ffe2018-08-11 13:40:20 +00005960
5961#ifndef SQLITE_OMIT_ALTERTABLE
5962 if( pOp->p4.z==0 ){
5963 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00005964 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
drh1595abc2018-08-14 19:27:51 +00005965 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
dane325ffe2018-08-11 13:40:20 +00005966 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00005967 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00005968 }else
5969#endif
drh1595abc2018-08-14 19:27:51 +00005970 {
drhe0a04a32016-12-16 01:00:21 +00005971 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005972 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00005973 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00005974 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00005975 initData.mInitFlags = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005976 zSql = sqlite3MPrintf(db,
drhc5a93d42019-08-12 00:08:07 +00005977 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005978 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005979 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005980 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005981 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005982 assert( db->init.busy==0 );
5983 db->init.busy = 1;
5984 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00005985 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005986 assert( !db->mallocFailed );
5987 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5988 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00005989 if( rc==SQLITE_OK && initData.nInitRow==0 ){
5990 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
5991 ** at least one SQL statement. Any less than that indicates that
5992 ** the sqlite_master table is corrupt. */
5993 rc = SQLITE_CORRUPT_BKPT;
5994 }
drhdbd6a7d2017-04-05 12:39:49 +00005995 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005996 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005997 }
drh3c23a882007-01-09 14:01:13 +00005998 }
drh9467abf2016-02-17 18:44:11 +00005999 if( rc ){
6000 sqlite3ResetAllSchemasOfConnection(db);
6001 if( rc==SQLITE_NOMEM ){
6002 goto no_mem;
6003 }
6004 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00006005 }
drh234c39d2004-07-24 03:30:47 +00006006 break;
6007}
6008
drh8bfdf722009-06-19 14:06:03 +00006009#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00006010/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00006011**
6012** Read the sqlite_stat1 table for database P1 and load the content
6013** of that table into the internal index hash table. This will cause
6014** the analysis to be used when preparing all subsequent queries.
6015*/
drh9cbf3422008-01-17 16:22:13 +00006016case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00006017 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6018 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006019 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00006020 break;
6021}
drh8bfdf722009-06-19 14:06:03 +00006022#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00006023
drh98757152008-01-09 23:04:12 +00006024/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006025**
6026** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006027** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00006028** is dropped from disk (using the Destroy opcode) in order to keep
6029** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006030** schema consistent with what is on disk.
6031*/
drh9cbf3422008-01-17 16:22:13 +00006032case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00006033 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006034 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006035 break;
6036}
6037
drh98757152008-01-09 23:04:12 +00006038/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006039**
6040** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006041** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00006042** is dropped from disk (using the Destroy opcode)
6043** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00006044** schema consistent with what is on disk.
6045*/
drh9cbf3422008-01-17 16:22:13 +00006046case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00006047 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006048 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006049 break;
6050}
6051
drh98757152008-01-09 23:04:12 +00006052/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006053**
6054** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006055** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00006056** is dropped from disk (using the Destroy opcode) in order to keep
6057** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006058** schema consistent with what is on disk.
6059*/
drh9cbf3422008-01-17 16:22:13 +00006060case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00006061 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006062 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006063 break;
6064}
6065
drh234c39d2004-07-24 03:30:47 +00006066
drhb7f91642004-10-31 02:22:47 +00006067#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00006068/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00006069**
drh98757152008-01-09 23:04:12 +00006070** Do an analysis of the currently open database. Store in
6071** register P1 the text of an error message describing any problems.
6072** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00006073**
drh66accfc2017-02-22 18:04:42 +00006074** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00006075** At most reg(P3) errors will be reported.
6076** In other words, the analysis stops as soon as reg(P1) errors are
6077** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00006078**
drh98968b22016-03-15 22:00:39 +00006079** The root page numbers of all tables in the database are integers
6080** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00006081**
drh98757152008-01-09 23:04:12 +00006082** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00006083** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00006084**
drh1dcdbc02007-01-27 02:24:54 +00006085** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00006086*/
drhaaab5722002-02-19 13:39:21 +00006087case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00006088 int nRoot; /* Number of tables to check. (Number of root pages.) */
6089 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00006090 int nErr; /* Number of errors reported */
6091 char *z; /* Text of the error report */
6092 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00006093
drh1713afb2013-06-28 01:24:57 +00006094 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00006095 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00006096 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00006097 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00006098 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00006099 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006100 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00006101 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00006102 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00006103 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00006104 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006105 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh21f6daa2019-10-11 14:21:48 +00006106 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00006107 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00006108 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00006109 if( nErr==0 ){
6110 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00006111 }else if( z==0 ){
6112 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00006113 }else{
drh66accfc2017-02-22 18:04:42 +00006114 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00006115 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00006116 }
drhb7654112008-01-12 12:48:07 +00006117 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00006118 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh21f6daa2019-10-11 14:21:48 +00006119 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006120}
drhb7f91642004-10-31 02:22:47 +00006121#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00006122
drh3d4501e2008-12-04 20:40:10 +00006123/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00006124** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006125**
drhbb6783b2017-04-29 18:02:49 +00006126** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00006127** held in register P1.
6128**
6129** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00006130*/
drh93952eb2009-11-13 19:43:43 +00006131case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00006132 pIn1 = &aMem[pOp->p1];
6133 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00006134 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00006135 if( (pIn1->flags & MEM_Blob)==0 ){
6136 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00006137 }
drh9d67afc2018-08-29 20:24:03 +00006138 assert( sqlite3VdbeMemIsRowSet(pIn1) );
6139 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00006140 break;
6141}
6142
6143/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00006144** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00006145**
drhbb6783b2017-04-29 18:02:49 +00006146** Extract the smallest value from the RowSet object in P1
6147** and put that value into register P3.
6148** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00006149** unchanged and jump to instruction P2.
6150*/
drh93952eb2009-11-13 19:43:43 +00006151case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00006152 i64 val;
drh49afe3a2013-07-10 03:05:14 +00006153
drh3c657212009-11-17 23:59:58 +00006154 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00006155 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
6156 if( (pIn1->flags & MEM_Blob)==0
6157 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00006158 ){
6159 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00006160 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00006161 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00006162 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00006163 }else{
6164 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00006165 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00006166 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00006167 }
drh49afe3a2013-07-10 03:05:14 +00006168 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006169}
6170
drh1b26c7c2009-04-22 02:15:47 +00006171/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00006172** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00006173**
drhade97602009-04-21 15:05:18 +00006174** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00006175** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00006176** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00006177** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00006178** next opcode.
danielk19771d461462009-04-21 09:02:45 +00006179**
drhbb6783b2017-04-29 18:02:49 +00006180** The RowSet object is optimized for the case where sets of integers
6181** are inserted in distinct phases, which each set contains no duplicates.
6182** Each set is identified by a unique P4 value. The first set
6183** must have P4==0, the final set must have P4==-1, and for all other sets
6184** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00006185**
6186** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006187** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006188** (b) when P4==-1 there is no need to insert the value, as it will
6189** never be tested for, and (c) when a value that is part of set X is
6190** inserted, there is no need to search to see if the same value was
6191** previously inserted as part of set X (only if it was previously
6192** inserted as part of some other set).
6193*/
drh1b26c7c2009-04-22 02:15:47 +00006194case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006195 int iSet;
6196 int exists;
6197
drh3c657212009-11-17 23:59:58 +00006198 pIn1 = &aMem[pOp->p1];
6199 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006200 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006201 assert( pIn3->flags&MEM_Int );
6202
drh1b26c7c2009-04-22 02:15:47 +00006203 /* If there is anything other than a rowset object in memory cell P1,
6204 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006205 */
drh9d67afc2018-08-29 20:24:03 +00006206 if( (pIn1->flags & MEM_Blob)==0 ){
6207 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006208 }
drh9d67afc2018-08-29 20:24:03 +00006209 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006210 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006211 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006212 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006213 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006214 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006215 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006216 }
6217 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006218 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006219 }
6220 break;
6221}
6222
drh5e00f6c2001-09-13 13:46:56 +00006223
danielk197793758c82005-01-21 08:13:14 +00006224#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006225
drh0fd61352014-02-07 02:29:45 +00006226/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006227**
dan76d462e2009-08-30 11:42:51 +00006228** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006229**
dan76d462e2009-08-30 11:42:51 +00006230** P1 contains the address of the memory cell that contains the first memory
6231** cell in an array of values used as arguments to the sub-program. P2
6232** contains the address to jump to if the sub-program throws an IGNORE
6233** exception using the RAISE() function. Register P3 contains the address
6234** of a memory cell in this (the parent) VM that is used to allocate the
6235** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006236**
6237** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006238**
6239** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006240*/
dan76d462e2009-08-30 11:42:51 +00006241case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006242 int nMem; /* Number of memory registers for sub-program */
6243 int nByte; /* Bytes of runtime space required for sub-program */
6244 Mem *pRt; /* Register to allocate runtime space */
6245 Mem *pMem; /* Used to iterate through memory cells */
6246 Mem *pEnd; /* Last memory cell in new array */
6247 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6248 SubProgram *pProgram; /* Sub-program to execute */
6249 void *t; /* Token identifying trigger */
6250
6251 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006252 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006253 assert( pProgram->nOp>0 );
6254
dan1da40a32009-09-19 17:00:31 +00006255 /* If the p5 flag is clear, then recursive invocation of triggers is
6256 ** disabled for backwards compatibility (p5 is set if this sub-program
6257 ** is really a trigger, not a foreign key action, and the flag set
6258 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006259 **
6260 ** It is recursive invocation of triggers, at the SQL level, that is
6261 ** disabled. In some cases a single trigger may generate more than one
6262 ** SubProgram (if the trigger may be executed with more than one different
6263 ** ON CONFLICT algorithm). SubProgram structures associated with a
6264 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006265 ** variable. */
6266 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006267 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006268 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6269 if( pFrame ) break;
6270 }
6271
danf5894502009-10-07 18:41:19 +00006272 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006273 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006274 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006275 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006276 }
6277
6278 /* Register pRt is used to store the memory required to save the state
6279 ** of the current program, and the memory required at runtime to execute
6280 ** the trigger program. If this trigger has been fired before, then pRt
6281 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006282 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006283 /* SubProgram.nMem is set to the number of memory cells used by the
6284 ** program stored in SubProgram.aOp. As well as these, one memory
6285 ** cell is required for each cursor used by the program. Set local
6286 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6287 */
dan65a7cd12009-09-01 12:16:01 +00006288 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006289 assert( nMem>0 );
6290 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006291 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006292 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006293 + pProgram->nCsr * sizeof(VdbeCursor*)
6294 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006295 pFrame = sqlite3DbMallocZero(db, nByte);
6296 if( !pFrame ){
6297 goto no_mem;
6298 }
6299 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006300 pRt->flags = MEM_Blob|MEM_Dyn;
6301 pRt->z = (char*)pFrame;
6302 pRt->n = nByte;
6303 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006304
6305 pFrame->v = p;
6306 pFrame->nChildMem = nMem;
6307 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006308 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006309 pFrame->aMem = p->aMem;
6310 pFrame->nMem = p->nMem;
6311 pFrame->apCsr = p->apCsr;
6312 pFrame->nCursor = p->nCursor;
6313 pFrame->aOp = p->aOp;
6314 pFrame->nOp = p->nOp;
6315 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006316#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006317 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006318#endif
drh72f56ef2018-08-29 18:47:22 +00006319#ifdef SQLITE_DEBUG
6320 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
6321#endif
dan165921a2009-08-28 18:53:45 +00006322
6323 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6324 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006325 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006326 pMem->db = db;
6327 }
6328 }else{
drh72f56ef2018-08-29 18:47:22 +00006329 pFrame = (VdbeFrame*)pRt->z;
6330 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00006331 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6332 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006333 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006334 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006335 }
6336
6337 p->nFrame++;
6338 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006339 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006340 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006341 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006342 assert( pFrame->pAuxData==0 );
6343 pFrame->pAuxData = p->pAuxData;
6344 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006345 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006346 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006347 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006348 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006349 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006350 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006351 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006352 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006353 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006354 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006355#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006356 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006357#endif
drhb2e61bc2019-01-25 19:29:01 +00006358#ifdef SQLITE_DEBUG
6359 /* Verify that second and subsequent executions of the same trigger do not
6360 ** try to reuse register values from the first use. */
6361 {
6362 int i;
6363 for(i=0; i<p->nMem; i++){
6364 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
6365 aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
6366 }
6367 }
6368#endif
drhf56fa462015-04-13 21:39:54 +00006369 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00006370 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00006371}
6372
dan76d462e2009-08-30 11:42:51 +00006373/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006374**
dan76d462e2009-08-30 11:42:51 +00006375** This opcode is only ever present in sub-programs called via the
6376** OP_Program instruction. Copy a value currently stored in a memory
6377** cell of the calling (parent) frame to cell P2 in the current frames
6378** address space. This is used by trigger programs to access the new.*
6379** and old.* values.
dan165921a2009-08-28 18:53:45 +00006380**
dan76d462e2009-08-30 11:42:51 +00006381** The address of the cell in the parent frame is determined by adding
6382** the value of the P1 argument to the value of the P1 argument to the
6383** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006384*/
drh27a348c2015-04-13 19:14:06 +00006385case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006386 VdbeFrame *pFrame;
6387 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006388 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006389 pFrame = p->pFrame;
6390 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006391 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6392 break;
6393}
6394
danielk197793758c82005-01-21 08:13:14 +00006395#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006396
dan1da40a32009-09-19 17:00:31 +00006397#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006398/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006399** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006400**
dan0ff297e2009-09-25 17:03:14 +00006401** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6402** If P1 is non-zero, the database constraint counter is incremented
6403** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006404** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006405*/
dan32b09f22009-09-23 17:29:59 +00006406case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006407 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006408 db->nDeferredImmCons += pOp->p2;
6409 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006410 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006411 }else{
dan0ff297e2009-09-25 17:03:14 +00006412 p->nFkConstraint += pOp->p2;
6413 }
6414 break;
6415}
6416
6417/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006418** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006419**
6420** This opcode tests if a foreign key constraint-counter is currently zero.
6421** If so, jump to instruction P2. Otherwise, fall through to the next
6422** instruction.
6423**
6424** If P1 is non-zero, then the jump is taken if the database constraint-counter
6425** is zero (the one that counts deferred constraint violations). If P1 is
6426** zero, the jump is taken if the statement constraint-counter is zero
6427** (immediate foreign key constraint violations).
6428*/
6429case OP_FkIfZero: { /* jump */
6430 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006431 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006432 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006433 }else{
drh688852a2014-02-17 22:40:43 +00006434 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006435 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006436 }
dan1da40a32009-09-19 17:00:31 +00006437 break;
6438}
6439#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6440
drh205f48e2004-11-05 00:43:11 +00006441#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006442/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006443** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006444**
dan76d462e2009-08-30 11:42:51 +00006445** P1 is a register in the root frame of this VM (the root frame is
6446** different from the current frame if this instruction is being executed
6447** within a sub-program). Set the value of register P1 to the maximum of
6448** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006449**
6450** This instruction throws an error if the memory cell is not initially
6451** an integer.
6452*/
dan76d462e2009-08-30 11:42:51 +00006453case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006454 VdbeFrame *pFrame;
6455 if( p->pFrame ){
6456 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6457 pIn1 = &pFrame->aMem[pOp->p1];
6458 }else{
drha6c2ed92009-11-14 23:22:23 +00006459 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006460 }
drh2b4ded92010-09-27 21:09:31 +00006461 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006462 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006463 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006464 sqlite3VdbeMemIntegerify(pIn2);
6465 if( pIn1->u.i<pIn2->u.i){
6466 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006467 }
6468 break;
6469}
6470#endif /* SQLITE_OMIT_AUTOINCREMENT */
6471
drh8b0cf382015-10-06 21:07:06 +00006472/* Opcode: IfPos P1 P2 P3 * *
6473** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006474**
drh16897072015-03-07 00:57:37 +00006475** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006476** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006477** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006478**
drh16897072015-03-07 00:57:37 +00006479** If the initial value of register P1 is less than 1, then the
6480** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006481*/
drh9cbf3422008-01-17 16:22:13 +00006482case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006483 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006484 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006485 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006486 if( pIn1->u.i>0 ){
6487 pIn1->u.i -= pOp->p3;
6488 goto jump_to_p2;
6489 }
drhec7429a2005-10-06 16:53:14 +00006490 break;
6491}
6492
drhcc2fa4c2016-01-25 15:57:29 +00006493/* Opcode: OffsetLimit P1 P2 P3 * *
6494** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006495**
drhcc2fa4c2016-01-25 15:57:29 +00006496** This opcode performs a commonly used computation associated with
6497** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6498** holds the offset counter. The opcode computes the combined value
6499** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6500** value computed is the total number of rows that will need to be
6501** visited in order to complete the query.
6502**
6503** If r[P3] is zero or negative, that means there is no OFFSET
6504** and r[P2] is set to be the value of the LIMIT, r[P1].
6505**
6506** if r[P1] is zero or negative, that means there is no LIMIT
6507** and r[P2] is set to -1.
6508**
6509** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006510*/
drhcc2fa4c2016-01-25 15:57:29 +00006511case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006512 i64 x;
drh3c657212009-11-17 23:59:58 +00006513 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006514 pIn3 = &aMem[pOp->p3];
6515 pOut = out2Prerelease(p, pOp);
6516 assert( pIn1->flags & MEM_Int );
6517 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006518 x = pIn1->u.i;
6519 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6520 /* If the LIMIT is less than or equal to zero, loop forever. This
6521 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6522 ** also loop forever. This is undocumented. In fact, one could argue
6523 ** that the loop should terminate. But assuming 1 billion iterations
6524 ** per second (far exceeding the capabilities of any current hardware)
6525 ** it would take nearly 300 years to actually reach the limit. So
6526 ** looping forever is a reasonable approximation. */
6527 pOut->u.i = -1;
6528 }else{
6529 pOut->u.i = x;
6530 }
drh15007a92006-01-08 18:10:17 +00006531 break;
6532}
6533
drhf99dd352016-12-18 17:42:00 +00006534/* Opcode: IfNotZero P1 P2 * * *
6535** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006536**
drh16897072015-03-07 00:57:37 +00006537** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006538** initially greater than zero, then decrement the value in register P1.
6539** If it is non-zero (negative or positive) and then also jump to P2.
6540** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006541*/
drh16897072015-03-07 00:57:37 +00006542case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006543 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006544 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006545 VdbeBranchTaken(pIn1->u.i<0, 2);
6546 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006547 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006548 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006549 }
6550 break;
6551}
6552
6553/* Opcode: DecrJumpZero P1 P2 * * *
6554** Synopsis: if (--r[P1])==0 goto P2
6555**
drhab5be2e2016-11-30 05:08:59 +00006556** Register P1 must hold an integer. Decrement the value in P1
6557** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006558*/
6559case OP_DecrJumpZero: { /* jump, in1 */
6560 pIn1 = &aMem[pOp->p1];
6561 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006562 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6563 VdbeBranchTaken(pIn1->u.i==0, 2);
6564 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006565 break;
6566}
6567
drh16897072015-03-07 00:57:37 +00006568
drh8f26da62018-07-05 21:22:57 +00006569/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006570** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006571**
drh8f26da62018-07-05 21:22:57 +00006572** Execute the xStep function for an aggregate.
6573** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006574** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006575** accumulator.
drhe5095352002-02-24 03:25:14 +00006576**
drh98757152008-01-09 23:04:12 +00006577** The P5 arguments are taken from register P2 and its
6578** successors.
drhe5095352002-02-24 03:25:14 +00006579*/
drh8f26da62018-07-05 21:22:57 +00006580/* Opcode: AggInverse * P2 P3 P4 P5
6581** Synopsis: accum=r[P3] inverse(r[P2@P5])
6582**
6583** Execute the xInverse function for an aggregate.
6584** The function has P5 arguments. P4 is a pointer to the
6585** FuncDef structure that specifies the function. Register P3 is the
6586** accumulator.
6587**
6588** The P5 arguments are taken from register P2 and its
6589** successors.
6590*/
6591/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006592** Synopsis: accum=r[P3] step(r[P2@P5])
6593**
dan9a947222018-06-14 19:06:36 +00006594** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6595** aggregate. The function has P5 arguments. P4 is a pointer to the
6596** FuncDef structure that specifies the function. Register P3 is the
6597** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006598**
6599** The P5 arguments are taken from register P2 and its
6600** successors.
6601**
6602** This opcode is initially coded as OP_AggStep0. On first evaluation,
6603** the FuncDef stored in P4 is converted into an sqlite3_context and
6604** the opcode is changed. In this way, the initialization of the
6605** sqlite3_context only happens once, instead of on each call to the
6606** step function.
6607*/
drh8f26da62018-07-05 21:22:57 +00006608case OP_AggInverse:
6609case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006610 int n;
drh9c7c9132015-06-26 18:16:52 +00006611 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006612
drh9c7c9132015-06-26 18:16:52 +00006613 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006614 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006615 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6616 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006617 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006618 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6619 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006620 if( pCtx==0 ) goto no_mem;
6621 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006622 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6623 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006624 pCtx->pFunc = pOp->p4.pFunc;
6625 pCtx->iOp = (int)(pOp - aOp);
6626 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006627 pCtx->skipFlag = 0;
6628 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006629 pCtx->argc = n;
6630 pOp->p4type = P4_FUNCCTX;
6631 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006632
6633 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006634 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006635
drh8f26da62018-07-05 21:22:57 +00006636 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006637 /* Fall through into OP_AggStep */
6638}
drh8f26da62018-07-05 21:22:57 +00006639case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006640 int i;
6641 sqlite3_context *pCtx;
6642 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006643
6644 assert( pOp->p4type==P4_FUNCCTX );
6645 pCtx = pOp->p4.pCtx;
6646 pMem = &aMem[pOp->p3];
6647
drh2c885d02018-07-07 19:36:04 +00006648#ifdef SQLITE_DEBUG
6649 if( pOp->p1 ){
6650 /* This is an OP_AggInverse call. Verify that xStep has always
6651 ** been called at least once prior to any xInverse call. */
6652 assert( pMem->uTemp==0x1122e0e3 );
6653 }else{
6654 /* This is an OP_AggStep call. Mark it as such. */
6655 pMem->uTemp = 0x1122e0e3;
6656 }
6657#endif
6658
drh9c7c9132015-06-26 18:16:52 +00006659 /* If this function is inside of a trigger, the register array in aMem[]
6660 ** might change from one evaluation to the next. The next block of code
6661 ** checks to see if the register array has changed, and if so it
6662 ** reinitializes the relavant parts of the sqlite3_context object */
6663 if( pCtx->pMem != pMem ){
6664 pCtx->pMem = pMem;
6665 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6666 }
6667
6668#ifdef SQLITE_DEBUG
6669 for(i=0; i<pCtx->argc; i++){
6670 assert( memIsValid(pCtx->argv[i]) );
6671 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6672 }
6673#endif
6674
drhabfcea22005-09-06 20:36:48 +00006675 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006676 assert( pCtx->pOut->flags==MEM_Null );
6677 assert( pCtx->isError==0 );
6678 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006679#ifndef SQLITE_OMIT_WINDOWFUNC
6680 if( pOp->p1 ){
6681 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6682 }else
6683#endif
6684 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6685
drhf09ac0b2018-01-23 03:44:06 +00006686 if( pCtx->isError ){
6687 if( pCtx->isError>0 ){
6688 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006689 rc = pCtx->isError;
6690 }
drhf09ac0b2018-01-23 03:44:06 +00006691 if( pCtx->skipFlag ){
6692 assert( pOp[-1].opcode==OP_CollSeq );
6693 i = pOp[-1].p1;
6694 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6695 pCtx->skipFlag = 0;
6696 }
6697 sqlite3VdbeMemRelease(pCtx->pOut);
6698 pCtx->pOut->flags = MEM_Null;
6699 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006700 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006701 }
drhf09ac0b2018-01-23 03:44:06 +00006702 assert( pCtx->pOut->flags==MEM_Null );
6703 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006704 break;
6705}
6706
drh8f26da62018-07-05 21:22:57 +00006707/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006708** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006709**
dan9a947222018-06-14 19:06:36 +00006710** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00006711** or window function. Execute the finalizer function
6712** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00006713**
6714** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006715** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006716** argument is not used by this opcode. It is only there to disambiguate
6717** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00006718** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00006719** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006720*/
drh8f26da62018-07-05 21:22:57 +00006721/* Opcode: AggValue * P2 P3 P4 *
6722** Synopsis: r[P3]=value N=P2
6723**
6724** Invoke the xValue() function and store the result in register P3.
6725**
6726** P2 is the number of arguments that the step function takes and
6727** P4 is a pointer to the FuncDef for this function. The P2
6728** argument is not used by this opcode. It is only there to disambiguate
6729** functions that can take varying numbers of arguments. The
6730** P4 argument is only needed for the case where
6731** the step function was not previously called.
6732*/
6733case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00006734case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006735 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006736 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00006737 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00006738 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006739 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006740#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006741 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00006742 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00006743 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006744 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006745 }else
6746#endif
drh8f26da62018-07-05 21:22:57 +00006747 {
6748 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6749 }
dan67a9b8e2018-06-22 20:51:35 +00006750
drh4c8555f2009-06-25 01:47:11 +00006751 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006752 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006753 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006754 }
drh2dca8682008-03-21 17:13:13 +00006755 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006756 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006757 if( sqlite3VdbeMemTooBig(pMem) ){
6758 goto too_big;
6759 }
drh5e00f6c2001-09-13 13:46:56 +00006760 break;
6761}
6762
dan5cf53532010-05-01 16:40:20 +00006763#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006764/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006765**
6766** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006767** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6768** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006769** SQLITE_BUSY or not, respectively. Write the number of pages in the
6770** WAL after the checkpoint into mem[P3+1] and the number of pages
6771** in the WAL that have been checkpointed after the checkpoint
6772** completes into mem[P3+2]. However on an error, mem[P3+1] and
6773** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006774*/
6775case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006776 int i; /* Loop counter */
6777 int aRes[3]; /* Results */
6778 Mem *pMem; /* Write results here */
6779
drh9e92a472013-06-27 17:40:30 +00006780 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006781 aRes[0] = 0;
6782 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006783 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6784 || pOp->p2==SQLITE_CHECKPOINT_FULL
6785 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006786 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006787 );
drh30aa3b92011-02-07 23:56:01 +00006788 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006789 if( rc ){
6790 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006791 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006792 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006793 }
drh30aa3b92011-02-07 23:56:01 +00006794 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6795 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6796 }
dan7c246102010-04-12 19:00:29 +00006797 break;
6798};
dan5cf53532010-05-01 16:40:20 +00006799#endif
drh5e00f6c2001-09-13 13:46:56 +00006800
drhcac29a62010-07-02 19:36:52 +00006801#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006802/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006803**
6804** Change the journal mode of database P1 to P3. P3 must be one of the
6805** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6806** modes (delete, truncate, persist, off and memory), this is a simple
6807** operation. No IO is required.
6808**
6809** If changing into or out of WAL mode the procedure is more complicated.
6810**
6811** Write a string containing the final journal-mode to register P2.
6812*/
drh27a348c2015-04-13 19:14:06 +00006813case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006814 Btree *pBt; /* Btree to change journal mode of */
6815 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006816 int eNew; /* New journal mode */
6817 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006818#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006819 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006820#endif
dane04dc882010-04-20 18:53:15 +00006821
drh27a348c2015-04-13 19:14:06 +00006822 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006823 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006824 assert( eNew==PAGER_JOURNALMODE_DELETE
6825 || eNew==PAGER_JOURNALMODE_TRUNCATE
6826 || eNew==PAGER_JOURNALMODE_PERSIST
6827 || eNew==PAGER_JOURNALMODE_OFF
6828 || eNew==PAGER_JOURNALMODE_MEMORY
6829 || eNew==PAGER_JOURNALMODE_WAL
6830 || eNew==PAGER_JOURNALMODE_QUERY
6831 );
6832 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006833 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006834
dane04dc882010-04-20 18:53:15 +00006835 pBt = db->aDb[pOp->p1].pBt;
6836 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006837 eOld = sqlite3PagerGetJournalMode(pPager);
6838 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6839 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006840
6841#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006842 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006843
drhd80b2332010-05-01 00:59:37 +00006844 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006845 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006846 */
6847 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006848 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006849 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006850 ){
drh0b9b4302010-06-11 17:01:24 +00006851 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006852 }
6853
drh0b9b4302010-06-11 17:01:24 +00006854 if( (eNew!=eOld)
6855 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6856 ){
danc0537fe2013-06-28 19:41:43 +00006857 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006858 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006859 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006860 "cannot change %s wal mode from within a transaction",
6861 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6862 );
drh9467abf2016-02-17 18:44:11 +00006863 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006864 }else{
6865
6866 if( eOld==PAGER_JOURNALMODE_WAL ){
6867 /* If leaving WAL mode, close the log file. If successful, the call
6868 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6869 ** file. An EXCLUSIVE lock may still be held on the database file
6870 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006871 */
dan7fb89902016-08-12 16:21:15 +00006872 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006873 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006874 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006875 }
drh242c4f72010-06-22 14:49:39 +00006876 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6877 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6878 ** as an intermediate */
6879 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006880 }
6881
6882 /* Open a transaction on the database file. Regardless of the journal
6883 ** mode, this transaction always uses a rollback journal.
6884 */
6885 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6886 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006887 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006888 }
6889 }
6890 }
dan5cf53532010-05-01 16:40:20 +00006891#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006892
drh9467abf2016-02-17 18:44:11 +00006893 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006894 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006895
dane04dc882010-04-20 18:53:15 +00006896 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006897 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006898 pOut->n = sqlite3Strlen30(pOut->z);
6899 pOut->enc = SQLITE_UTF8;
6900 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006901 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006902 break;
drhcac29a62010-07-02 19:36:52 +00006903};
6904#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006905
drhfdbcdee2007-03-27 14:44:50 +00006906#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00006907/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00006908**
drh9ef5e772016-08-19 14:20:56 +00006909** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6910** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00006911**
drh2f6239e2018-12-08 00:43:08 +00006912** If P2 is not zero, then it is a register holding a string which is
6913** the file into which the result of vacuum should be written. When
6914** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00006915*/
drh9cbf3422008-01-17 16:22:13 +00006916case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006917 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00006918 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
6919 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00006920 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006921 break;
6922}
drh154d4b22006-09-21 11:02:16 +00006923#endif
drh6f8c91c2003-12-07 00:24:35 +00006924
danielk1977dddbcdc2007-04-26 14:42:34 +00006925#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006926/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006927**
6928** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006929** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006930** P2. Otherwise, fall through to the next instruction.
6931*/
drh9cbf3422008-01-17 16:22:13 +00006932case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006933 Btree *pBt;
6934
6935 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006936 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006937 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006938 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006939 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006940 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006941 if( rc ){
6942 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006943 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006944 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006945 }
6946 break;
6947}
6948#endif
6949
drhba968db2018-07-24 22:02:12 +00006950/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006951**
drh25df48d2014-07-22 14:58:12 +00006952** Cause precompiled statements to expire. When an expired statement
6953** is executed using sqlite3_step() it will either automatically
6954** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6955** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006956**
6957** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006958** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00006959**
6960** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
6961** then running SQL statements are allowed to continue to run to completion.
6962** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
6963** that might help the statement run faster but which does not affect the
6964** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00006965*/
drh9cbf3422008-01-17 16:22:13 +00006966case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00006967 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00006968 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00006969 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00006970 }else{
drhba968db2018-07-24 22:02:12 +00006971 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00006972 }
6973 break;
6974}
6975
danielk1977c00da102006-01-07 13:21:04 +00006976#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006977/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006978** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006979**
6980** Obtain a lock on a particular table. This instruction is only used when
6981** the shared-cache feature is enabled.
6982**
danielk197796d48e92009-06-29 06:00:37 +00006983** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006984** on which the lock is acquired. A readlock is obtained if P3==0 or
6985** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006986**
6987** P2 contains the root-page of the table to lock.
6988**
drh66a51672008-01-03 00:01:23 +00006989** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006990** used to generate an error message if the lock cannot be obtained.
6991*/
drh9cbf3422008-01-17 16:22:13 +00006992case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006993 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006994 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006995 int p1 = pOp->p1;
6996 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006997 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006998 assert( isWriteLock==0 || isWriteLock==1 );
6999 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00007000 if( rc ){
7001 if( (rc&0xFF)==SQLITE_LOCKED ){
7002 const char *z = pOp->p4.z;
7003 sqlite3VdbeError(p, "database table is locked: %s", z);
7004 }
7005 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00007006 }
danielk1977c00da102006-01-07 13:21:04 +00007007 }
7008 break;
7009}
drhb9bb7c12006-06-11 23:41:55 +00007010#endif /* SQLITE_OMIT_SHARED_CACHE */
7011
7012#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007013/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007014**
danielk19773e3a84d2008-08-01 17:37:40 +00007015** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7016** xBegin method for that table.
7017**
7018** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00007019** within a callback to a virtual table xSync() method. If it is, the error
7020** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00007021*/
drh9cbf3422008-01-17 16:22:13 +00007022case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00007023 VTable *pVTab;
7024 pVTab = pOp->p4.pVtab;
7025 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00007026 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00007027 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00007028 break;
7029}
7030#endif /* SQLITE_OMIT_VIRTUALTABLE */
7031
7032#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00007033/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00007034**
dan73779452015-03-19 18:56:17 +00007035** P2 is a register that holds the name of a virtual table in database
7036** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00007037*/
drh9cbf3422008-01-17 16:22:13 +00007038case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00007039 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00007040 const char *zTab; /* Name of the virtual table */
7041
dan73779452015-03-19 18:56:17 +00007042 memset(&sMem, 0, sizeof(sMem));
7043 sMem.db = db;
drh47464062015-03-21 12:22:16 +00007044 /* Because P2 is always a static string, it is impossible for the
7045 ** sqlite3VdbeMemCopy() to fail */
7046 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
7047 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00007048 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00007049 assert( rc==SQLITE_OK );
7050 zTab = (const char*)sqlite3_value_text(&sMem);
7051 assert( zTab || db->mallocFailed );
7052 if( zTab ){
7053 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00007054 }
7055 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00007056 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007057 break;
7058}
7059#endif /* SQLITE_OMIT_VIRTUALTABLE */
7060
7061#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007062/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007063**
drh66a51672008-01-03 00:01:23 +00007064** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00007065** of that table.
drhb9bb7c12006-06-11 23:41:55 +00007066*/
drh9cbf3422008-01-17 16:22:13 +00007067case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00007068 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00007069 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00007070 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00007071 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00007072 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007073 break;
7074}
7075#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00007076
drh9eff6162006-06-12 21:59:13 +00007077#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007078/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00007079**
drh66a51672008-01-03 00:01:23 +00007080** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00007081** P1 is a cursor number. This opcode opens a cursor to the virtual
7082** table and stores that cursor in P1.
7083*/
drh9cbf3422008-01-17 16:22:13 +00007084case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00007085 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00007086 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00007087 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007088 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007089
drh1713afb2013-06-28 01:24:57 +00007090 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00007091 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00007092 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00007093 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007094 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7095 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007096 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007097 }
7098 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00007099 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00007100 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007101 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007102
drh9467abf2016-02-17 18:44:11 +00007103 /* Initialize sqlite3_vtab_cursor base class */
7104 pVCur->pVtab = pVtab;
7105
7106 /* Initialize vdbe cursor object */
7107 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
7108 if( pCur ){
7109 pCur->uc.pVCur = pVCur;
7110 pVtab->nRef++;
7111 }else{
7112 assert( db->mallocFailed );
7113 pModule->xClose(pVCur);
7114 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007115 }
drh9eff6162006-06-12 21:59:13 +00007116 break;
7117}
7118#endif /* SQLITE_OMIT_VIRTUALTABLE */
7119
7120#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00007121/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00007122** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00007123**
7124** P1 is a cursor opened using VOpen. P2 is an address to jump to if
7125** the filtered result set is empty.
7126**
drh66a51672008-01-03 00:01:23 +00007127** P4 is either NULL or a string that was generated by the xBestIndex
7128** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00007129** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00007130**
drh9eff6162006-06-12 21:59:13 +00007131** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00007132** by P1. The integer query plan parameter to xFilter is stored in register
7133** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00007134** xFilter method. Registers P3+2..P3+1+argc are the argc
7135** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00007136** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00007137**
danielk19776dbee812008-01-03 18:39:41 +00007138** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00007139*/
drh9cbf3422008-01-17 16:22:13 +00007140case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00007141 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00007142 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007143 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007144 Mem *pQuery;
7145 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00007146 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00007147 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00007148 VdbeCursor *pCur;
7149 int res;
7150 int i;
7151 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007152
drha6c2ed92009-11-14 23:22:23 +00007153 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00007154 pArgc = &pQuery[1];
7155 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00007156 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00007157 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00007158 assert( pCur->eCurType==CURTYPE_VTAB );
7159 pVCur = pCur->uc.pVCur;
7160 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00007161 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007162
drh9cbf3422008-01-17 16:22:13 +00007163 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00007164 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00007165 nArg = (int)pArgc->u.i;
7166 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007167
drh644a5292006-12-20 14:53:38 +00007168 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00007169 res = 0;
7170 apArg = p->apArg;
7171 for(i = 0; i<nArg; i++){
7172 apArg[i] = &pArgc[i+1];
7173 }
drhc960dcb2015-11-20 19:22:01 +00007174 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00007175 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007176 if( rc ) goto abort_due_to_error;
7177 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00007178 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00007179 VdbeBranchTaken(res!=0,2);
7180 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00007181 break;
7182}
7183#endif /* SQLITE_OMIT_VIRTUALTABLE */
7184
7185#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00007186/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007187** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007188**
drh6f390be2018-01-11 17:04:26 +00007189** Store in register P3 the value of the P2-th column of
7190** the current row of the virtual-table of cursor P1.
7191**
7192** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007193** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007194** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7195** function to return true inside the xColumn method of the virtual
7196** table implementation. The P5 column might also contain other
7197** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7198** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007199*/
7200case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007201 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007202 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007203 Mem *pDest;
7204 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007205
drhdfe88ec2008-11-03 20:55:06 +00007206 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007207 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007208 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007209 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007210 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007211 if( pCur->nullRow ){
7212 sqlite3VdbeMemSetNull(pDest);
7213 break;
7214 }
drhc960dcb2015-11-20 19:22:01 +00007215 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007216 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007217 assert( pModule->xColumn );
7218 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007219 sContext.pOut = pDest;
drh09d00b22018-09-27 20:20:01 +00007220 testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
7221 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007222 sqlite3VdbeMemSetNull(pDest);
7223 pDest->flags = MEM_Null|MEM_Zero;
7224 pDest->u.nZero = 0;
7225 }else{
7226 MemSetTypeFlag(pDest, MEM_Null);
7227 }
drhc960dcb2015-11-20 19:22:01 +00007228 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007229 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007230 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007231 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007232 rc = sContext.isError;
7233 }
drh9bd038f2014-08-27 14:14:06 +00007234 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007235 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007236 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007237
drhde4fcfd2008-01-19 23:50:26 +00007238 if( sqlite3VdbeMemTooBig(pDest) ){
7239 goto too_big;
7240 }
drh9467abf2016-02-17 18:44:11 +00007241 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007242 break;
7243}
7244#endif /* SQLITE_OMIT_VIRTUALTABLE */
7245
7246#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007247/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007248**
7249** Advance virtual table P1 to the next row in its result set and
7250** jump to instruction P2. Or, if the virtual table has reached
7251** the end of its result set, then fall through to the next instruction.
7252*/
drh9cbf3422008-01-17 16:22:13 +00007253case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007254 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007255 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007256 int res;
drh856c1032009-06-02 15:21:42 +00007257 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007258
drhc54a6172009-06-02 16:06:03 +00007259 res = 0;
drh856c1032009-06-02 15:21:42 +00007260 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007261 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007262 if( pCur->nullRow ){
7263 break;
7264 }
drhc960dcb2015-11-20 19:22:01 +00007265 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007266 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007267 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007268
drhde4fcfd2008-01-19 23:50:26 +00007269 /* Invoke the xNext() method of the module. There is no way for the
7270 ** underlying implementation to return an error if one occurs during
7271 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7272 ** data is available) and the error code returned when xColumn or
7273 ** some other method is next invoked on the save virtual table cursor.
7274 */
drhc960dcb2015-11-20 19:22:01 +00007275 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007276 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007277 if( rc ) goto abort_due_to_error;
7278 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007279 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007280 if( !res ){
7281 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007282 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007283 }
drh49afe3a2013-07-10 03:05:14 +00007284 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007285}
7286#endif /* SQLITE_OMIT_VIRTUALTABLE */
7287
danielk1977182c4ba2007-06-27 15:53:34 +00007288#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007289/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007290**
drh66a51672008-01-03 00:01:23 +00007291** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007292** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007293** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007294*/
drh9cbf3422008-01-17 16:22:13 +00007295case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007296 sqlite3_vtab *pVtab;
7297 Mem *pName;
dan34566c42018-09-20 17:21:21 +00007298 int isLegacy;
7299
7300 isLegacy = (db->flags & SQLITE_LegacyAlter);
7301 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00007302 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007303 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007304 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007305 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007306 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007307 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007308 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007309 testcase( pName->enc==SQLITE_UTF8 );
7310 testcase( pName->enc==SQLITE_UTF16BE );
7311 testcase( pName->enc==SQLITE_UTF16LE );
7312 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007313 if( rc ) goto abort_due_to_error;
7314 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00007315 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00007316 sqlite3VtabImportErrmsg(p, pVtab);
7317 p->expired = 0;
7318 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007319 break;
7320}
7321#endif
drh4cbdda92006-06-14 19:00:20 +00007322
7323#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007324/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007325** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007326**
drh66a51672008-01-03 00:01:23 +00007327** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007328** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007329** are contiguous memory cells starting at P3 to pass to the xUpdate
7330** invocation. The value in register (P3+P2-1) corresponds to the
7331** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007332**
7333** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007334** The argv[0] element (which corresponds to memory cell P3)
7335** is the rowid of a row to delete. If argv[0] is NULL then no
7336** deletion occurs. The argv[1] element is the rowid of the new
7337** row. This can be NULL to have the virtual table select the new
7338** rowid for itself. The subsequent elements in the array are
7339** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007340**
7341** If P2==1 then no insert is performed. argv[0] is the rowid of
7342** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007343**
7344** P1 is a boolean flag. If it is set to true and the xUpdate call
7345** is successful, then the value returned by sqlite3_last_insert_rowid()
7346** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007347**
7348** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7349** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007350*/
drh9cbf3422008-01-17 16:22:13 +00007351case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007352 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007353 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007354 int nArg;
7355 int i;
7356 sqlite_int64 rowid;
7357 Mem **apArg;
7358 Mem *pX;
7359
danb061d052011-04-25 18:49:57 +00007360 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7361 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7362 );
drh9e92a472013-06-27 17:40:30 +00007363 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007364 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007365 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007366 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007367 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7368 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007369 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007370 }
7371 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007372 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007373 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007374 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007375 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007376 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007377 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007378 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007379 assert( memIsValid(pX) );
7380 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007381 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007382 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007383 }
danb061d052011-04-25 18:49:57 +00007384 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007385 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007386 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007387 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007388 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007389 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007390 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007391 }
drhd91c1a12013-02-09 13:58:25 +00007392 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007393 if( pOp->p5==OE_Ignore ){
7394 rc = SQLITE_OK;
7395 }else{
7396 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7397 }
7398 }else{
7399 p->nChange++;
7400 }
drh9467abf2016-02-17 18:44:11 +00007401 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007402 }
drh4cbdda92006-06-14 19:00:20 +00007403 break;
danielk1977399918f2006-06-14 13:03:23 +00007404}
7405#endif /* SQLITE_OMIT_VIRTUALTABLE */
7406
danielk197759a93792008-05-15 17:48:20 +00007407#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7408/* Opcode: Pagecount P1 P2 * * *
7409**
7410** Write the current number of pages in database P1 to memory cell P2.
7411*/
drh27a348c2015-04-13 19:14:06 +00007412case OP_Pagecount: { /* out2 */
7413 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007414 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007415 break;
7416}
7417#endif
7418
drh60ac3f42010-11-23 18:59:27 +00007419
7420#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7421/* Opcode: MaxPgcnt P1 P2 P3 * *
7422**
7423** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007424** Do not let the maximum page count fall below the current page count and
7425** do not change the maximum page count value if P3==0.
7426**
drh60ac3f42010-11-23 18:59:27 +00007427** Store the maximum page count after the change in register P2.
7428*/
drh27a348c2015-04-13 19:14:06 +00007429case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007430 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007431 Btree *pBt;
7432
drh27a348c2015-04-13 19:14:06 +00007433 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007434 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007435 newMax = 0;
7436 if( pOp->p3 ){
7437 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007438 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007439 }
7440 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007441 break;
7442}
7443#endif
7444
drh3e34eab2017-07-19 19:48:40 +00007445/* Opcode: Function0 P1 P2 P3 P4 P5
7446** Synopsis: r[P3]=func(r[P2@P5])
7447**
7448** Invoke a user function (P4 is a pointer to a FuncDef object that
7449** defines the function) with P5 arguments taken from register P2 and
7450** successors. The result of the function is stored in register P3.
7451** Register P3 must not be one of the function inputs.
7452**
7453** P1 is a 32-bit bitmask indicating whether or not each argument to the
7454** function was determined to be constant at compile time. If the first
7455** argument was constant then bit 0 of P1 is set. This is used to determine
7456** whether meta data associated with a user function argument using the
7457** sqlite3_set_auxdata() API may be safely retained until the next
7458** invocation of this opcode.
7459**
7460** See also: Function, AggStep, AggFinal
7461*/
7462/* Opcode: Function P1 P2 P3 P4 P5
7463** Synopsis: r[P3]=func(r[P2@P5])
7464**
7465** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7466** contains a pointer to the function to be run) with P5 arguments taken
7467** from register P2 and successors. The result of the function is stored
7468** in register P3. Register P3 must not be one of the function inputs.
7469**
7470** P1 is a 32-bit bitmask indicating whether or not each argument to the
7471** function was determined to be constant at compile time. If the first
7472** argument was constant then bit 0 of P1 is set. This is used to determine
7473** whether meta data associated with a user function argument using the
7474** sqlite3_set_auxdata() API may be safely retained until the next
7475** invocation of this opcode.
7476**
7477** SQL functions are initially coded as OP_Function0 with P4 pointing
7478** to a FuncDef object. But on first evaluation, the P4 operand is
7479** automatically converted into an sqlite3_context object and the operation
7480** changed to this OP_Function opcode. In this way, the initialization of
7481** the sqlite3_context object occurs only once, rather than once for each
7482** evaluation of the function.
7483**
7484** See also: Function0, AggStep, AggFinal
7485*/
mistachkin758784d2018-07-25 15:12:29 +00007486case OP_PureFunc0: /* group */
7487case OP_Function0: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007488 int n;
7489 sqlite3_context *pCtx;
7490
7491 assert( pOp->p4type==P4_FUNCDEF );
7492 n = pOp->p5;
7493 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7494 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
7495 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
7496 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7497 if( pCtx==0 ) goto no_mem;
7498 pCtx->pOut = 0;
7499 pCtx->pFunc = pOp->p4.pFunc;
7500 pCtx->iOp = (int)(pOp - aOp);
7501 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007502 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007503 pCtx->argc = n;
7504 pOp->p4type = P4_FUNCCTX;
7505 pOp->p4.pCtx = pCtx;
7506 assert( OP_PureFunc == OP_PureFunc0+2 );
7507 assert( OP_Function == OP_Function0+2 );
7508 pOp->opcode += 2;
7509 /* Fall through into OP_Function */
7510}
mistachkin758784d2018-07-25 15:12:29 +00007511case OP_PureFunc: /* group */
7512case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007513 int i;
7514 sqlite3_context *pCtx;
7515
7516 assert( pOp->p4type==P4_FUNCCTX );
7517 pCtx = pOp->p4.pCtx;
7518
7519 /* If this function is inside of a trigger, the register array in aMem[]
7520 ** might change from one evaluation to the next. The next block of code
7521 ** checks to see if the register array has changed, and if so it
7522 ** reinitializes the relavant parts of the sqlite3_context object */
7523 pOut = &aMem[pOp->p3];
7524 if( pCtx->pOut != pOut ){
7525 pCtx->pOut = pOut;
7526 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7527 }
7528
7529 memAboutToChange(p, pOut);
7530#ifdef SQLITE_DEBUG
7531 for(i=0; i<pCtx->argc; i++){
7532 assert( memIsValid(pCtx->argv[i]) );
7533 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7534 }
7535#endif
7536 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007537 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007538 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7539
7540 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007541 if( pCtx->isError ){
7542 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007543 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7544 rc = pCtx->isError;
7545 }
7546 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007547 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007548 if( rc ) goto abort_due_to_error;
7549 }
7550
7551 /* Copy the result of the function into register P3 */
7552 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7553 sqlite3VdbeChangeEncoding(pOut, encoding);
7554 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7555 }
7556
7557 REGISTER_TRACE(pOp->p3, pOut);
7558 UPDATE_MAX_BLOBSIZE(pOut);
7559 break;
7560}
7561
drhf259df52017-12-27 20:38:35 +00007562/* Opcode: Trace P1 P2 * P4 *
7563**
7564** Write P4 on the statement trace output if statement tracing is
7565** enabled.
7566**
7567** Operand P1 must be 0x7fffffff and P2 must positive.
7568*/
drh74588ce2017-09-13 00:13:05 +00007569/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007570** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007571**
7572** Programs contain a single instance of this opcode as the very first
7573** opcode.
drh949f9cd2008-01-12 21:35:57 +00007574**
7575** If tracing is enabled (by the sqlite3_trace()) interface, then
7576** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007577** Or if P4 is blank, use the string returned by sqlite3_sql().
7578**
7579** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007580**
7581** Increment the value of P1 so that OP_Once opcodes will jump the
7582** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007583**
7584** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7585** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007586*/
drhf259df52017-12-27 20:38:35 +00007587case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007588case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007589 int i;
drhb9f47992018-01-24 12:14:43 +00007590#ifndef SQLITE_OMIT_TRACE
7591 char *zTrace;
7592#endif
drh5fe63bf2016-07-25 02:42:22 +00007593
7594 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7595 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7596 **
7597 ** This assert() provides evidence for:
7598 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7599 ** would have been returned by the legacy sqlite3_trace() interface by
7600 ** using the X argument when X begins with "--" and invoking
7601 ** sqlite3_expanded_sql(P) otherwise.
7602 */
7603 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007604
7605 /* OP_Init is always instruction 0 */
7606 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007607
drhaceb31b2014-02-08 01:40:27 +00007608#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007609 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007610 && !p->doingRerun
7611 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7612 ){
drh3d2a5292016-07-13 22:55:01 +00007613#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007614 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7615 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007616 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007617 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007618 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007619 }else
drh3d2a5292016-07-13 22:55:01 +00007620#endif
drh7adbcff2017-03-20 15:29:28 +00007621 if( db->nVdbeExec>1 ){
7622 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7623 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7624 sqlite3DbFree(db, z);
7625 }else{
drhbd441f72016-07-25 02:31:48 +00007626 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007627 }
drh949f9cd2008-01-12 21:35:57 +00007628 }
drh8f8b2312013-10-18 20:03:43 +00007629#ifdef SQLITE_USE_FCNTL_TRACE
7630 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7631 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007632 int j;
7633 for(j=0; j<db->nDb; j++){
7634 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7635 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007636 }
7637 }
7638#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007639#ifdef SQLITE_DEBUG
7640 if( (db->flags & SQLITE_SqlTrace)!=0
7641 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7642 ){
7643 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7644 }
7645#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007646#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007647 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007648 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007649 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007650 for(i=1; i<p->nOp; i++){
7651 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7652 }
7653 pOp->p1 = 0;
7654 }
7655 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007656 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007657 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007658}
drh949f9cd2008-01-12 21:35:57 +00007659
drh28935362013-12-07 20:39:19 +00007660#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007661/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007662**
7663** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007664** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7665** to values currently held in registers. TK_COLUMN terms in the P4
7666** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007667*/
7668case OP_CursorHint: {
7669 VdbeCursor *pC;
7670
7671 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7672 assert( pOp->p4type==P4_EXPR );
7673 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007674 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007675 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007676 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7677 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007678 }
drh28935362013-12-07 20:39:19 +00007679 break;
7680}
7681#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007682
drh4031baf2018-05-28 17:31:20 +00007683#ifdef SQLITE_DEBUG
7684/* Opcode: Abortable * * * * *
7685**
7686** Verify that an Abort can happen. Assert if an Abort at this point
7687** might cause database corruption. This opcode only appears in debugging
7688** builds.
7689**
7690** An Abort is safe if either there have been no writes, or if there is
7691** an active statement journal.
7692*/
7693case OP_Abortable: {
7694 sqlite3VdbeAssertAbortable(p);
7695 break;
7696}
7697#endif
7698
drh91fd4d42008-01-19 20:11:25 +00007699/* Opcode: Noop * * * * *
7700**
7701** Do nothing. This instruction is often useful as a jump
7702** destination.
drh5e00f6c2001-09-13 13:46:56 +00007703*/
drh91fd4d42008-01-19 20:11:25 +00007704/*
7705** The magic Explain opcode are only inserted when explain==2 (which
7706** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7707** This opcode records information from the optimizer. It is the
7708** the same as a no-op. This opcodesnever appears in a real VM program.
7709*/
drh4031baf2018-05-28 17:31:20 +00007710default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007711 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007712
drh5e00f6c2001-09-13 13:46:56 +00007713 break;
7714}
7715
7716/*****************************************************************************
7717** The cases of the switch statement above this line should all be indented
7718** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7719** readability. From this point on down, the normal indentation rules are
7720** restored.
7721*****************************************************************************/
7722 }
drh6e142f52000-06-08 13:36:40 +00007723
drh7b396862003-01-01 23:06:20 +00007724#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007725 {
drh35043cc2018-02-12 20:27:34 +00007726 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007727 if( endTime>start ) pOrigOp->cycles += endTime - start;
7728 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007729 }
drh7b396862003-01-01 23:06:20 +00007730#endif
7731
drh6e142f52000-06-08 13:36:40 +00007732 /* The following code adds nothing to the actual functionality
7733 ** of the program. It is only here for testing and debugging.
7734 ** On the other hand, it does burn CPU cycles every time through
7735 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7736 */
7737#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007738 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007739
drhcf1023c2007-05-08 20:59:49 +00007740#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007741 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007742 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007743 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007744 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007745 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007746 }
drh7cc84c22016-04-11 13:36:42 +00007747 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007748 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007749 }
drh75897232000-05-29 14:26:00 +00007750 }
danielk1977b5402fb2005-01-12 07:15:04 +00007751#endif /* SQLITE_DEBUG */
7752#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007753 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007754
drha05a7222008-01-19 03:35:58 +00007755 /* If we reach this point, it means that execution is finished with
7756 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007757 */
drh9467abf2016-02-17 18:44:11 +00007758abort_due_to_error:
7759 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007760 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007761 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7762 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7763 }
drha05a7222008-01-19 03:35:58 +00007764 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007765 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007766 testcase( sqlite3GlobalConfig.xLog!=0 );
7767 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007768 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007769 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007770 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007771 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007772 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007773 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007774 }
drh900b31e2007-08-28 02:27:51 +00007775
7776 /* This is the only way out of this procedure. We have to
7777 ** release the mutexes on btrees that were acquired at the
7778 ** top. */
7779vdbe_return:
drhc332e042019-02-12 21:04:33 +00007780#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00007781 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
7782 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00007783 if( db->xProgress(db->pProgressArg) ){
7784 nProgressLimit = 0xffffffff;
7785 rc = SQLITE_INTERRUPT;
7786 goto abort_due_to_error;
7787 }
7788 }
7789#endif
drh9b47ee32013-08-20 03:13:51 +00007790 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007791 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007792 assert( rc!=SQLITE_OK || nExtraDelete==0
7793 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7794 );
drhb86ccfb2003-01-28 23:13:10 +00007795 return rc;
7796
drh023ae032007-05-08 12:12:16 +00007797 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7798 ** is encountered.
7799 */
7800too_big:
drh22c17b82015-05-15 04:13:15 +00007801 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007802 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007803 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007804
drh98640a32007-06-07 19:08:32 +00007805 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007806 */
7807no_mem:
drh4a642b62016-02-05 01:55:27 +00007808 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007809 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007810 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007811 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007812
danielk19776f8a5032004-05-10 10:34:51 +00007813 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007814 ** flag.
7815 */
7816abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007817 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007818 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007819 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007820 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007821 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007822}