blob: c004137c47572aac464cdd2e9e3f6ed9d66b8556 [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/*
drh9cbf3422008-01-17 16:22:13 +0000199** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000200** already. Return non-zero if a malloc() fails.
201*/
drhb21c8cd2007-08-21 19:33:56 +0000202#define Stringify(P, enc) \
drhbd9507c2014-08-23 17:21:37 +0000203 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
drhf4479502004-05-27 03:12:53 +0000204 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000205
206/*
danielk1977bd7e4602004-05-24 07:34:48 +0000207** An ephemeral string value (signified by the MEM_Ephem flag) contains
208** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000209** is responsible for deallocating that string. Because the register
210** does not control the string, it might be deleted without the register
211** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000212**
213** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000214** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000215** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000216*/
drhb21c8cd2007-08-21 19:33:56 +0000217#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000218 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000219 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000220
dan689ab892011-08-12 15:02:00 +0000221/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000222#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000223
224/*
drhdfe88ec2008-11-03 20:55:06 +0000225** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000226** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000227*/
drhdfe88ec2008-11-03 20:55:06 +0000228static VdbeCursor *allocateCursor(
229 Vdbe *p, /* The virtual machine */
230 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000231 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000232 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000233 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000234){
235 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000236 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000237 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000238 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000239 **
240 ** * Sometimes cursor numbers are used for a couple of different
241 ** purposes in a vdbe program. The different uses might require
242 ** different sized allocations. Memory cells provide growable
243 ** allocations.
244 **
245 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
246 ** be freed lazily via the sqlite3_release_memory() API. This
247 ** minimizes the number of malloc calls made by the system.
248 **
drh3cdce922016-03-21 00:30:40 +0000249 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000250 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
251 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000252 */
drh9f6168b2016-03-19 23:32:58 +0000253 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000254
danielk19775f096132008-03-28 15:44:09 +0000255 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000256 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000257 nByte =
drh5cc10232013-11-21 01:04:02 +0000258 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000259 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000260
drh9f6168b2016-03-19 23:32:58 +0000261 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000262 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
dan97c8cb32019-01-01 18:00:17 +0000263 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
264 ** is clear. Otherwise, if this is an ephemeral cursor created by
265 ** OP_OpenDup, the cursor will not be closed and will still be part
266 ** of a BtShared.pCursor list. */
267 p->apCsr[iCur]->isEphemeral = 0;
danielk1977be718892006-06-23 08:05:19 +0000268 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000269 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000270 }
drh322f2852014-09-19 00:43:39 +0000271 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000272 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000273 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000274 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000275 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000276 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000277 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000278 if( eCurType==CURTYPE_BTREE ){
279 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000280 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000281 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000282 }
danielk197794eb6a12005-12-15 15:22:08 +0000283 }
drh4774b132004-06-12 20:12:51 +0000284 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000285}
286
danielk19773d1bfea2004-05-14 11:00:53 +0000287/*
drh29d72102006-02-09 22:13:41 +0000288** Try to convert a value into a numeric representation if we can
289** do so without loss of information. In other words, if the string
290** looks like a number, convert it into a number. If it does not
291** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000292**
293** If the bTryForInt flag is true, then extra effort is made to give
294** an integer representation. Strings that look like floating point
295** values but which have no fractional component (example: '48.00')
296** will have a MEM_Int representation when bTryForInt is true.
297**
298** If bTryForInt is false, then if the input string contains a decimal
299** point or exponential notation, the result is only MEM_Real, even
300** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000301*/
drhbd9507c2014-08-23 17:21:37 +0000302static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000303 double rValue;
304 i64 iValue;
305 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000306 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000307 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
308 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
309 pRec->u.i = iValue;
310 pRec->flags |= MEM_Int;
311 }else{
drh74eaba42014-09-18 17:52:15 +0000312 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000313 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000314 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000315 }
drh06b3bd52018-02-01 01:13:33 +0000316 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
317 ** string representation after computing a numeric equivalent, because the
318 ** string representation might not be the canonical representation for the
319 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
320 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000321}
322
323/*
drh8a512562005-11-14 22:29:05 +0000324** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000325**
drh8a512562005-11-14 22:29:05 +0000326** SQLITE_AFF_INTEGER:
327** SQLITE_AFF_REAL:
328** SQLITE_AFF_NUMERIC:
329** Try to convert pRec to an integer representation or a
330** floating-point representation if an integer representation
331** is not possible. Note that the integer representation is
332** always preferred, even if the affinity is REAL, because
333** an integer representation is more space efficient on disk.
334**
335** SQLITE_AFF_TEXT:
336** Convert pRec to a text representation.
337**
drh05883a32015-06-02 15:32:08 +0000338** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000339** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000340*/
drh17435752007-08-16 04:30:38 +0000341static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000342 Mem *pRec, /* The value to apply affinity to */
343 char affinity, /* The affinity to be applied */
344 u8 enc /* Use this text encoding */
345){
drh7ea31cc2014-09-18 14:36:00 +0000346 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000347 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
348 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000349 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000350 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000351 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000352 }else{
353 sqlite3VdbeIntegerAffinity(pRec);
354 }
drh17c40292004-07-21 02:53:29 +0000355 }
drh7ea31cc2014-09-18 14:36:00 +0000356 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000357 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000358 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000359 ** representation. It would be harmless to repeat the conversion if
360 ** there is already a string rep, but it is pointless to waste those
361 ** CPU cycles. */
362 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
363 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
364 sqlite3VdbeMemStringify(pRec, enc, 1);
365 }
danielk19773d1bfea2004-05-14 11:00:53 +0000366 }
dandde548c2015-05-19 19:44:25 +0000367 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000368 }
369}
370
danielk1977aee18ef2005-03-09 12:26:50 +0000371/*
drh29d72102006-02-09 22:13:41 +0000372** Try to convert the type of a function argument or a result column
373** into a numeric representation. Use either INTEGER or REAL whichever
374** is appropriate. But only do the conversion if it is possible without
375** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000376*/
377int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000378 int eType = sqlite3_value_type(pVal);
379 if( eType==SQLITE_TEXT ){
380 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000381 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000382 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000383 }
drh1b27b8c2014-02-10 03:21:57 +0000384 return eType;
drh29d72102006-02-09 22:13:41 +0000385}
386
387/*
danielk1977aee18ef2005-03-09 12:26:50 +0000388** Exported version of applyAffinity(). This one works on sqlite3_value*,
389** not the internal Mem* type.
390*/
danielk19771e536952007-08-16 10:09:01 +0000391void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000392 sqlite3_value *pVal,
393 u8 affinity,
394 u8 enc
395){
drhb21c8cd2007-08-21 19:33:56 +0000396 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000397}
398
drh3d1d90a2014-03-24 15:00:15 +0000399/*
drhf1a89ed2014-08-23 17:41:15 +0000400** pMem currently only holds a string type (or maybe a BLOB that we can
401** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000402** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000403** accordingly.
404*/
405static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
406 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
407 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh0814acd2019-01-25 20:09:04 +0000408 ExpandBlob(pMem);
drh74eaba42014-09-18 17:52:15 +0000409 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000410 return 0;
411 }
drh84d4f1a2017-09-20 10:47:10 +0000412 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000413 return MEM_Int;
414 }
415 return MEM_Real;
416}
417
418/*
drh3d1d90a2014-03-24 15:00:15 +0000419** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
420** none.
421**
422** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000423** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000424*/
425static u16 numericType(Mem *pMem){
426 if( pMem->flags & (MEM_Int|MEM_Real) ){
427 return pMem->flags & (MEM_Int|MEM_Real);
428 }
429 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000430 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000431 }
432 return 0;
433}
434
danielk1977b5402fb2005-01-12 07:15:04 +0000435#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000436/*
danielk1977ca6b2912004-05-21 10:49:47 +0000437** Write a nice string representation of the contents of cell pMem
438** into buffer zBuf, length nBuf.
439*/
drh74161702006-02-24 02:53:49 +0000440void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000441 char *zCsr = zBuf;
442 int f = pMem->flags;
443
drh57196282004-10-06 15:41:16 +0000444 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000445
danielk1977ca6b2912004-05-21 10:49:47 +0000446 if( f&MEM_Blob ){
447 int i;
448 char c;
449 if( f & MEM_Dyn ){
450 c = 'z';
451 assert( (f & (MEM_Static|MEM_Ephem))==0 );
452 }else if( f & MEM_Static ){
453 c = 't';
454 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
455 }else if( f & MEM_Ephem ){
456 c = 'e';
457 assert( (f & (MEM_Static|MEM_Dyn))==0 );
458 }else{
459 c = 's';
460 }
drh85c2dc02017-03-16 13:30:58 +0000461 *(zCsr++) = c;
drh5bb3eb92007-05-04 13:15:55 +0000462 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000463 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000464 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000465 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000466 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000467 }
468 for(i=0; i<16 && i<pMem->n; i++){
469 char z = pMem->z[i];
470 if( z<32 || z>126 ) *zCsr++ = '.';
471 else *zCsr++ = z;
472 }
drh85c2dc02017-03-16 13:30:58 +0000473 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000474 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000475 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000476 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000477 }
danielk1977b1bc9532004-05-22 03:05:33 +0000478 *zCsr = '\0';
479 }else if( f & MEM_Str ){
480 int j, k;
481 zBuf[0] = ' ';
482 if( f & MEM_Dyn ){
483 zBuf[1] = 'z';
484 assert( (f & (MEM_Static|MEM_Ephem))==0 );
485 }else if( f & MEM_Static ){
486 zBuf[1] = 't';
487 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
488 }else if( f & MEM_Ephem ){
489 zBuf[1] = 'e';
490 assert( (f & (MEM_Static|MEM_Dyn))==0 );
491 }else{
492 zBuf[1] = 's';
493 }
494 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000495 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000496 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000497 zBuf[k++] = '[';
498 for(j=0; j<15 && j<pMem->n; j++){
499 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000500 if( c>=0x20 && c<0x7f ){
501 zBuf[k++] = c;
502 }else{
503 zBuf[k++] = '.';
504 }
505 }
506 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000507 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000508 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000509 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000510 }
danielk1977ca6b2912004-05-21 10:49:47 +0000511}
512#endif
513
drh5b6afba2008-01-05 16:29:28 +0000514#ifdef SQLITE_DEBUG
515/*
516** Print the value of a register for tracing purposes:
517*/
drh84e55a82013-11-13 17:58:23 +0000518static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000519 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000520 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000521 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000522 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000523 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000524 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000525 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000526 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000527#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000528 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000529 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000530#endif
drh9d67afc2018-08-29 20:24:03 +0000531 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000532 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000533 }else{
534 char zBuf[200];
535 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000536 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000537 }
dan5b6c8e42016-01-30 15:46:03 +0000538 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000539}
drh84e55a82013-11-13 17:58:23 +0000540static void registerTrace(int iReg, Mem *p){
541 printf("REG[%d] = ", iReg);
542 memTracePrint(p);
543 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000544 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000545}
546#endif
547
548#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000549# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000550#else
551# define REGISTER_TRACE(R,M)
552#endif
553
danielk197784ac9d02004-05-18 09:58:06 +0000554
drh7b396862003-01-01 23:06:20 +0000555#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000556
557/*
558** hwtime.h contains inline assembler code for implementing
559** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000560*/
shane9bcbdad2008-05-29 20:22:37 +0000561#include "hwtime.h"
562
drh7b396862003-01-01 23:06:20 +0000563#endif
564
danielk1977fd7f0452008-12-17 17:30:26 +0000565#ifndef NDEBUG
566/*
567** This function is only called from within an assert() expression. It
568** checks that the sqlite3.nTransaction variable is correctly set to
569** the number of non-transaction savepoints currently in the
570** linked list starting at sqlite3.pSavepoint.
571**
572** Usage:
573**
574** assert( checkSavepointCount(db) );
575*/
576static int checkSavepointCount(sqlite3 *db){
577 int n = 0;
578 Savepoint *p;
579 for(p=db->pSavepoint; p; p=p->pNext) n++;
580 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
581 return 1;
582}
583#endif
584
drh27a348c2015-04-13 19:14:06 +0000585/*
586** Return the register of pOp->p2 after first preparing it to be
587** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000588*/
589static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
590 sqlite3VdbeMemSetNull(pOut);
591 pOut->flags = MEM_Int;
592 return pOut;
593}
drh27a348c2015-04-13 19:14:06 +0000594static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
595 Mem *pOut;
596 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000597 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000598 pOut = &p->aMem[pOp->p2];
599 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000600 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000601 return out2PrereleaseWithClear(pOut);
602 }else{
603 pOut->flags = MEM_Int;
604 return pOut;
605 }
drh27a348c2015-04-13 19:14:06 +0000606}
607
drhb9755982010-07-24 16:34:37 +0000608
609/*
drh0fd61352014-02-07 02:29:45 +0000610** Execute as much of a VDBE program as we can.
611** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000612*/
danielk19774adee202004-05-08 08:23:19 +0000613int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000614 Vdbe *p /* The VDBE */
615){
drhbbe879d2009-11-14 18:04:35 +0000616 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000617 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000618#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
619 Op *pOrigOp; /* Value of pOp at the top of the loop */
620#endif
drhb89aeb62016-01-27 15:49:32 +0000621#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000622 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000623#endif
drhb86ccfb2003-01-28 23:13:10 +0000624 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000625 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000626 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000627 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000628 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000629 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000630#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000631 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000632#endif
drha6c2ed92009-11-14 23:22:23 +0000633 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000634 Mem *pIn1 = 0; /* 1st input operand */
635 Mem *pIn2 = 0; /* 2nd input operand */
636 Mem *pIn3 = 0; /* 3rd input operand */
637 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000638#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000639 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000640#endif
drh856c1032009-06-02 15:21:42 +0000641 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000642
drhca48c902008-01-18 14:08:24 +0000643 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000644 sqlite3VdbeEnter(p);
drh82642f82019-02-12 22:58:32 +0000645#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
646 if( db->xProgress ){
647 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
648 assert( 0 < db->nProgressOps );
649 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
650 }else{
651 nProgressLimit = 0xffffffff;
652 }
653#endif
danielk19772e588c72005-12-09 14:25:08 +0000654 if( p->rc==SQLITE_NOMEM ){
655 /* This happens if a malloc() inside a call to sqlite3_column_text() or
656 ** sqlite3_column_text16() failed. */
657 goto no_mem;
658 }
drhcbd8db32015-08-20 17:18:32 +0000659 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000660 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000661 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000662 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000663 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000664 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000665 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000666 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000667#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000668 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000669 if( p->pc==0
670 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
671 ){
drh3c23a882007-01-09 14:01:13 +0000672 int i;
drh84e55a82013-11-13 17:58:23 +0000673 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000674 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000675 if( p->db->flags & SQLITE_VdbeListing ){
676 printf("VDBE Program Listing:\n");
677 for(i=0; i<p->nOp; i++){
678 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
679 }
drh3c23a882007-01-09 14:01:13 +0000680 }
drh84e55a82013-11-13 17:58:23 +0000681 if( p->db->flags & SQLITE_VdbeEQP ){
682 for(i=0; i<p->nOp; i++){
683 if( aOp[i].opcode==OP_Explain ){
684 if( once ) printf("VDBE Query Plan:\n");
685 printf("%s\n", aOp[i].p4.z);
686 once = 0;
687 }
688 }
689 }
690 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000691 }
danielk19772d1d86f2008-06-20 14:59:51 +0000692 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000693#endif
drh9467abf2016-02-17 18:44:11 +0000694 for(pOp=&aOp[p->pc]; 1; pOp++){
695 /* Errors are detected by individual opcodes, with an immediate
696 ** jumps to abort_due_to_error. */
697 assert( rc==SQLITE_OK );
698
drhf56fa462015-04-13 21:39:54 +0000699 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000700#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000701 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000702#endif
drhbf159fa2013-06-25 22:01:22 +0000703 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000704#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000705 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000706#endif
drh6e142f52000-06-08 13:36:40 +0000707
danielk19778b60e0f2005-01-12 09:10:39 +0000708 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000709 */
danielk19778b60e0f2005-01-12 09:10:39 +0000710#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000711 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000712 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000713 }
drh3f7d4e42004-07-24 14:35:58 +0000714#endif
715
drh6e142f52000-06-08 13:36:40 +0000716
drhf6038712004-02-08 18:07:34 +0000717 /* Check to see if we need to simulate an interrupt. This only happens
718 ** if we have a special test build.
719 */
720#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000721 if( sqlite3_interrupt_count>0 ){
722 sqlite3_interrupt_count--;
723 if( sqlite3_interrupt_count==0 ){
724 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000725 }
726 }
727#endif
728
drh3c657212009-11-17 23:59:58 +0000729 /* Sanity checking on other operands */
730#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000731 {
732 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
733 if( (opProperty & OPFLG_IN1)!=0 ){
734 assert( pOp->p1>0 );
735 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
736 assert( memIsValid(&aMem[pOp->p1]) );
737 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
738 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
739 }
740 if( (opProperty & OPFLG_IN2)!=0 ){
741 assert( pOp->p2>0 );
742 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
743 assert( memIsValid(&aMem[pOp->p2]) );
744 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
745 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
746 }
747 if( (opProperty & OPFLG_IN3)!=0 ){
748 assert( pOp->p3>0 );
749 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
750 assert( memIsValid(&aMem[pOp->p3]) );
751 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
752 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
753 }
754 if( (opProperty & OPFLG_OUT2)!=0 ){
755 assert( pOp->p2>0 );
756 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
757 memAboutToChange(p, &aMem[pOp->p2]);
758 }
759 if( (opProperty & OPFLG_OUT3)!=0 ){
760 assert( pOp->p3>0 );
761 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
762 memAboutToChange(p, &aMem[pOp->p3]);
763 }
drh3c657212009-11-17 23:59:58 +0000764 }
765#endif
drh6dc41482015-04-16 17:31:02 +0000766#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
767 pOrigOp = pOp;
768#endif
drh93952eb2009-11-13 19:43:43 +0000769
drh75897232000-05-29 14:26:00 +0000770 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000771
drh5e00f6c2001-09-13 13:46:56 +0000772/*****************************************************************************
773** What follows is a massive switch statement where each case implements a
774** separate instruction in the virtual machine. If we follow the usual
775** indentation conventions, each case should be indented by 6 spaces. But
776** that is a lot of wasted space on the left margin. So the code within
777** the switch statement will break with convention and be flush-left. Another
778** big comment (similar to this one) will mark the point in the code where
779** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000780**
781** The formatting of each case is important. The makefile for SQLite
782** generates two C files "opcodes.h" and "opcodes.c" by scanning this
783** file looking for lines that begin with "case OP_". The opcodes.h files
784** will be filled with #defines that give unique integer values to each
785** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000786** each string is the symbolic name for the corresponding opcode. If the
787** case statement is followed by a comment of the form "/# same as ... #/"
788** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000789**
drh9cbf3422008-01-17 16:22:13 +0000790** Other keywords in the comment that follows each case are used to
791** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000792** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000793** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000794**
drhac82fcf2002-09-08 17:23:41 +0000795** Documentation about VDBE opcodes is generated by scanning this file
796** for lines of that contain "Opcode:". That line and all subsequent
797** comment lines are used in the generation of the opcode.html documentation
798** file.
799**
800** SUMMARY:
801**
802** Formatting is important to scripts that scan this file.
803** Do not deviate from the formatting style currently in use.
804**
drh5e00f6c2001-09-13 13:46:56 +0000805*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000806
drh9cbf3422008-01-17 16:22:13 +0000807/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000808**
809** An unconditional jump to address P2.
810** The next instruction executed will be
811** the one at index P2 from the beginning of
812** the program.
drhfe705102014-03-06 13:38:37 +0000813**
814** The P1 parameter is not actually used by this opcode. However, it
815** is sometimes set to 1 instead of 0 as a hint to the command-line shell
816** that this Goto is the bottom of a loop and that the lines from P2 down
817** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000818*/
drh9cbf3422008-01-17 16:22:13 +0000819case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000820jump_to_p2_and_check_for_interrupt:
821 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000822
823 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000824 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000825 ** completion. Check to see if sqlite3_interrupt() has been called
826 ** or if the progress callback needs to be invoked.
827 **
828 ** This code uses unstructured "goto" statements and does not look clean.
829 ** But that is not due to sloppy coding habits. The code is written this
830 ** way for performance, to avoid having to run the interrupt and progress
831 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
832 ** faster according to "valgrind --tool=cachegrind" */
833check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000834 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000835#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
836 /* Call the progress callback if it is configured and the required number
837 ** of VDBE ops have been executed (either since this invocation of
838 ** sqlite3VdbeExec() or since last time the progress callback was called).
839 ** If the progress callback returns non-zero, exit the virtual machine with
840 ** a return code SQLITE_ABORT.
841 */
drhb1af9c62019-02-20 13:55:45 +0000842 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000843 assert( db->nProgressOps!=0 );
drhb1af9c62019-02-20 13:55:45 +0000844 nProgressLimit += db->nProgressOps;
drh400fcba2013-11-14 00:09:48 +0000845 if( db->xProgress(db->pProgressArg) ){
drhc332e042019-02-12 21:04:33 +0000846 nProgressLimit = 0xffffffff;
drh49afe3a2013-07-10 03:05:14 +0000847 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000848 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000849 }
drh49afe3a2013-07-10 03:05:14 +0000850 }
851#endif
852
drh5e00f6c2001-09-13 13:46:56 +0000853 break;
854}
drh75897232000-05-29 14:26:00 +0000855
drh2eb95372008-06-06 15:04:36 +0000856/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000857**
drh2eb95372008-06-06 15:04:36 +0000858** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000859** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000860*/
drhb8475df2011-12-09 16:21:19 +0000861case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000862 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000863 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000864 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000865 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000866 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000867 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000868 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000869
870 /* Most jump operations do a goto to this spot in order to update
871 ** the pOp pointer. */
872jump_to_p2:
873 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000874 break;
875}
876
drh2eb95372008-06-06 15:04:36 +0000877/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000878**
drh81cf13e2014-02-07 18:27:53 +0000879** Jump to the next instruction after the address in register P1. After
880** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000881*/
drh2eb95372008-06-06 15:04:36 +0000882case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000883 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000884 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000885 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000886 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000887 break;
888}
889
drhed71a832014-02-07 19:18:10 +0000890/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000891**
drh5dad9a32014-07-25 18:37:42 +0000892** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000893** located at address P3.
894**
drh5dad9a32014-07-25 18:37:42 +0000895** If P2!=0 then the coroutine implementation immediately follows
896** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000897** address P2.
drh5dad9a32014-07-25 18:37:42 +0000898**
899** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000900*/
901case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000902 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000903 assert( pOp->p2>=0 && pOp->p2<p->nOp );
904 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000905 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000906 assert( !VdbeMemDynamic(pOut) );
907 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000908 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000909 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000910 break;
911}
912
913/* Opcode: EndCoroutine P1 * * * *
914**
drhbc5cf382014-08-06 01:08:07 +0000915** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000916** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000917** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000918**
919** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000920*/
921case OP_EndCoroutine: { /* in1 */
922 VdbeOp *pCaller;
923 pIn1 = &aMem[pOp->p1];
924 assert( pIn1->flags==MEM_Int );
925 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
926 pCaller = &aOp[pIn1->u.i];
927 assert( pCaller->opcode==OP_Yield );
928 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000929 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000930 pIn1->flags = MEM_Undefined;
931 break;
932}
933
934/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000935**
drh5dad9a32014-07-25 18:37:42 +0000936** Swap the program counter with the value in register P1. This
937** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000938**
drh5dad9a32014-07-25 18:37:42 +0000939** If the coroutine that is launched by this instruction ends with
940** Yield or Return then continue to the next instruction. But if
941** the coroutine launched by this instruction ends with
942** EndCoroutine, then jump to P2 rather than continuing with the
943** next instruction.
944**
945** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000946*/
drh81cf13e2014-02-07 18:27:53 +0000947case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000948 int pcDest;
drh3c657212009-11-17 23:59:58 +0000949 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000950 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000951 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000952 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000953 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000954 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000955 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000956 break;
957}
958
drhf9c8ce32013-11-05 13:33:55 +0000959/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000960** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000961**
drhef8662b2011-06-20 21:47:58 +0000962** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000963** parameter P1, P2, and P4 as if this were a Halt instruction. If the
964** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000965** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000966*/
967case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000968 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +0000969#ifdef SQLITE_DEBUG
970 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
971#endif
drh5053a792009-02-20 03:02:23 +0000972 if( (pIn3->flags & MEM_Null)==0 ) break;
973 /* Fall through into OP_Halt */
974}
drhe00ee6e2008-06-20 15:24:01 +0000975
drhf9c8ce32013-11-05 13:33:55 +0000976/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000977**
drh3d4501e2008-12-04 20:40:10 +0000978** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000979** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000980**
drh92f02c32004-09-02 14:57:08 +0000981** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
982** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
983** For errors, it can be some other value. If P1!=0 then P2 will determine
984** whether or not to rollback the current transaction. Do not rollback
985** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
986** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000987** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000988**
drh66a51672008-01-03 00:01:23 +0000989** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000990**
drhf9c8ce32013-11-05 13:33:55 +0000991** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
992**
993** 0: (no change)
994** 1: NOT NULL contraint failed: P4
995** 2: UNIQUE constraint failed: P4
996** 3: CHECK constraint failed: P4
997** 4: FOREIGN KEY constraint failed: P4
998**
999** If P5 is not zero and P4 is NULL, then everything after the ":" is
1000** omitted.
1001**
drh9cfcf5d2002-01-29 18:41:24 +00001002** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001003** every program. So a jump past the last instruction of the program
1004** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001005*/
drh9cbf3422008-01-17 16:22:13 +00001006case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001007 VdbeFrame *pFrame;
1008 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001009
drhf56fa462015-04-13 21:39:54 +00001010 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +00001011#ifdef SQLITE_DEBUG
1012 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1013#endif
dan165921a2009-08-28 18:53:45 +00001014 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +00001015 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001016 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001017 p->pFrame = pFrame->pParent;
1018 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001019 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001020 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001021 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001022 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001023 ** currently being halted. If the p2 instruction of this OP_Halt
1024 ** instruction is set to OE_Ignore, then the sub-program is throwing
1025 ** an IGNORE exception. In this case jump to the address specified
1026 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001027 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001028 }
drhbbe879d2009-11-14 18:04:35 +00001029 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001030 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001031 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001032 break;
1033 }
drh92f02c32004-09-02 14:57:08 +00001034 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001035 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001036 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001037 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001038 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001039 if( pOp->p5 ){
1040 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1041 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001042 testcase( pOp->p5==1 );
1043 testcase( pOp->p5==2 );
1044 testcase( pOp->p5==3 );
1045 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001046 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1047 if( pOp->p4.z ){
1048 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1049 }
drhd9b7ec92013-11-06 14:05:21 +00001050 }else{
drh22c17b82015-05-15 04:13:15 +00001051 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001052 }
drh99f5de72016-04-30 02:59:15 +00001053 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001054 }
drh92f02c32004-09-02 14:57:08 +00001055 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001056 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001057 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001058 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001059 }else{
drhd91c1a12013-02-09 13:58:25 +00001060 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001061 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001062 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001063 }
drh900b31e2007-08-28 02:27:51 +00001064 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001065}
drhc61053b2000-06-04 12:58:36 +00001066
drh4c583122008-01-04 22:01:03 +00001067/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001068** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001069**
drh9cbf3422008-01-17 16:22:13 +00001070** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001071*/
drh27a348c2015-04-13 19:14:06 +00001072case OP_Integer: { /* out2 */
1073 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001074 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001075 break;
1076}
1077
drh4c583122008-01-04 22:01:03 +00001078/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001079** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001080**
drh66a51672008-01-03 00:01:23 +00001081** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001082** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001083*/
drh27a348c2015-04-13 19:14:06 +00001084case OP_Int64: { /* out2 */
1085 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001086 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001087 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001088 break;
1089}
drh4f26d6c2004-05-26 23:25:30 +00001090
drh13573c72010-01-12 17:04:07 +00001091#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001092/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001093** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001094**
drh4c583122008-01-04 22:01:03 +00001095** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001096** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001097*/
drh27a348c2015-04-13 19:14:06 +00001098case OP_Real: { /* same as TK_FLOAT, out2 */
1099 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001100 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001101 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001102 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001103 break;
1104}
drh13573c72010-01-12 17:04:07 +00001105#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001106
drh3c84ddf2008-01-09 02:15:38 +00001107/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001108** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001109**
drh66a51672008-01-03 00:01:23 +00001110** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001111** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001112** this transformation, the length of string P4 is computed and stored
1113** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001114*/
drh27a348c2015-04-13 19:14:06 +00001115case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001116 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001117 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001118 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001119 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001120
1121#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001122 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001123 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001124 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001125 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001126 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001127 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001128 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001129 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001130 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001131 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001132 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001133 }
drh66a51672008-01-03 00:01:23 +00001134 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001135 pOp->p4.z = pOut->z;
1136 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001137 }
danielk197793758c82005-01-21 08:13:14 +00001138#endif
drhbb4957f2008-03-20 14:03:29 +00001139 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001140 goto too_big;
1141 }
drh2f555112016-04-30 18:10:34 +00001142 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001143 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001144}
drhf4479502004-05-27 03:12:53 +00001145
drhf07cf6e2015-03-06 16:45:16 +00001146/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001147** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001148**
drh9cbf3422008-01-17 16:22:13 +00001149** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001150**
drh44aebff2016-05-02 10:25:42 +00001151** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001152** the datatype of the register P2 is converted to BLOB. The content is
1153** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001154** of a string, as if it had been CAST. In other words:
1155**
1156** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001157*/
drh27a348c2015-04-13 19:14:06 +00001158case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001159 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001160 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001161 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1162 pOut->z = pOp->p4.z;
1163 pOut->n = pOp->p1;
1164 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001165 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001166#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001167 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001168 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001169 pIn3 = &aMem[pOp->p3];
1170 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001171 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001172 }
drh41d2e662015-12-01 21:23:07 +00001173#endif
danielk1977c572ef72004-05-27 09:28:41 +00001174 break;
1175}
1176
drh053a1282012-09-19 21:15:46 +00001177/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001178** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001179**
drhb8475df2011-12-09 16:21:19 +00001180** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001181** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001182** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001183** set to NULL.
1184**
1185** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1186** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1187** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001188*/
drh27a348c2015-04-13 19:14:06 +00001189case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001190 int cnt;
drh053a1282012-09-19 21:15:46 +00001191 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001192 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001193 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001194 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001195 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001196 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001197#ifdef SQLITE_DEBUG
1198 pOut->uTemp = 0;
1199#endif
drhb8475df2011-12-09 16:21:19 +00001200 while( cnt>0 ){
1201 pOut++;
1202 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001203 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001204 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001205 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001206 cnt--;
1207 }
drhf0863fe2005-06-12 21:35:51 +00001208 break;
1209}
1210
drh05a86c52014-02-16 01:55:49 +00001211/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001212** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001213**
1214** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1215** instruction, but do not free any string or blob memory associated with
1216** the register, so that if the value was a string or blob that was
1217** previously copied using OP_SCopy, the copies will continue to be valid.
1218*/
1219case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001220 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001221 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001222 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001223 break;
1224}
drhf0863fe2005-06-12 21:35:51 +00001225
drha5750cf2014-02-07 13:20:31 +00001226/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001227** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001228**
drh9de221d2008-01-05 06:51:30 +00001229** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001230** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001231*/
drh27a348c2015-04-13 19:14:06 +00001232case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001233 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001234 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001235 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001236 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001237 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001238 break;
1239}
1240
drheaf52d82010-05-12 13:50:23 +00001241/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001242** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001243**
drheaf52d82010-05-12 13:50:23 +00001244** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001245**
drh0fd61352014-02-07 02:29:45 +00001246** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001247** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001248*/
drh27a348c2015-04-13 19:14:06 +00001249case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001250 Mem *pVar; /* Value being transferred */
1251
drheaf52d82010-05-12 13:50:23 +00001252 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001253 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001254 pVar = &p->aVar[pOp->p1 - 1];
1255 if( sqlite3VdbeMemTooBig(pVar) ){
1256 goto too_big;
drh023ae032007-05-08 12:12:16 +00001257 }
drh7441df72017-01-09 19:27:04 +00001258 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001259 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1260 memcpy(pOut, pVar, MEMCELLSIZE);
1261 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1262 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001263 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001264 break;
1265}
danielk1977295ba552004-05-19 10:34:51 +00001266
drhb21e7c72008-06-22 12:37:57 +00001267/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001268** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001269**
drh079a3072014-03-19 14:10:55 +00001270** Move the P3 values in register P1..P1+P3-1 over into
1271** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001272** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001273** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1274** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001275*/
drhe1349cb2008-04-01 00:36:10 +00001276case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001277 int n; /* Number of registers left to copy */
1278 int p1; /* Register to copy from */
1279 int p2; /* Register to copy to */
1280
drhe09f43f2013-11-21 04:18:31 +00001281 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001282 p1 = pOp->p1;
1283 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001284 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001285 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001286
drha6c2ed92009-11-14 23:22:23 +00001287 pIn1 = &aMem[p1];
1288 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001289 do{
drh9f6168b2016-03-19 23:32:58 +00001290 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1291 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001292 assert( memIsValid(pIn1) );
1293 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001294 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001295#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001296 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001297 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001298 }
1299#endif
drhbd6789e2015-04-28 14:00:02 +00001300 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001301 REGISTER_TRACE(p2++, pOut);
1302 pIn1++;
1303 pOut++;
drh079a3072014-03-19 14:10:55 +00001304 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001305 break;
1306}
1307
drhe8e4af72012-09-21 00:04:28 +00001308/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001309** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001310**
drhe8e4af72012-09-21 00:04:28 +00001311** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001312**
1313** This instruction makes a deep copy of the value. A duplicate
1314** is made of any string or blob constant. See also OP_SCopy.
1315*/
drhe8e4af72012-09-21 00:04:28 +00001316case OP_Copy: {
1317 int n;
1318
1319 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001320 pIn1 = &aMem[pOp->p1];
1321 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001322 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001323 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001324 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001325 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1326 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001327#ifdef SQLITE_DEBUG
1328 pOut->pScopyFrom = 0;
1329#endif
drhe8e4af72012-09-21 00:04:28 +00001330 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1331 if( (n--)==0 ) break;
1332 pOut++;
1333 pIn1++;
1334 }
drhe1349cb2008-04-01 00:36:10 +00001335 break;
1336}
1337
drhb1fdb2a2008-01-05 04:06:03 +00001338/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001339** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001340**
drh9cbf3422008-01-17 16:22:13 +00001341** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001342**
1343** This instruction makes a shallow copy of the value. If the value
1344** is a string or blob, then the copy is only a pointer to the
1345** original and hence if the original changes so will the copy.
1346** Worse, if the original is deallocated, the copy becomes invalid.
1347** Thus the program must guarantee that the original will not change
1348** during the lifetime of the copy. Use OP_Copy to make a complete
1349** copy.
1350*/
drh26198bb2013-10-31 11:15:09 +00001351case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001352 pIn1 = &aMem[pOp->p1];
1353 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001354 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001355 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001356#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001357 pOut->pScopyFrom = pIn1;
1358 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001359#endif
drh5e00f6c2001-09-13 13:46:56 +00001360 break;
1361}
drh75897232000-05-29 14:26:00 +00001362
drhfed7ac62015-10-15 18:04:59 +00001363/* Opcode: IntCopy P1 P2 * * *
1364** Synopsis: r[P2]=r[P1]
1365**
1366** Transfer the integer value held in register P1 into register P2.
1367**
1368** This is an optimized version of SCopy that works only for integer
1369** values.
1370*/
1371case OP_IntCopy: { /* out2 */
1372 pIn1 = &aMem[pOp->p1];
1373 assert( (pIn1->flags & MEM_Int)!=0 );
1374 pOut = &aMem[pOp->p2];
1375 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1376 break;
1377}
1378
drh9cbf3422008-01-17 16:22:13 +00001379/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001380** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001381**
shane21e7feb2008-05-30 15:59:49 +00001382** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001383** results. This opcode causes the sqlite3_step() call to terminate
1384** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001385** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001386** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001387*/
drh9cbf3422008-01-17 16:22:13 +00001388case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001389 Mem *pMem;
1390 int i;
1391 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001392 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001393 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001394
dan32b09f22009-09-23 17:29:59 +00001395 /* If this statement has violated immediate foreign key constraints, do
1396 ** not return the number of rows modified. And do not RELEASE the statement
1397 ** transaction. It needs to be rolled back. */
1398 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1399 assert( db->flags&SQLITE_CountRows );
1400 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001401 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001402 }
1403
danielk1977bd434552009-03-18 10:33:00 +00001404 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1405 ** DML statements invoke this opcode to return the number of rows
1406 ** modified to the user. This is the only way that a VM that
1407 ** opens a statement transaction may invoke this opcode.
1408 **
1409 ** In case this is such a statement, close any statement transaction
1410 ** opened by this VM before returning control to the user. This is to
1411 ** ensure that statement-transactions are always nested, not overlapping.
1412 ** If the open statement-transaction is not closed here, then the user
1413 ** may step another VM that opens its own statement transaction. This
1414 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001415 **
1416 ** The statement transaction is never a top-level transaction. Hence
1417 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001418 */
1419 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001420 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001421 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001422
drhd4e70eb2008-01-02 00:34:36 +00001423 /* Invalidate all ephemeral cursor row caches */
1424 p->cacheCtr = (p->cacheCtr + 2)|1;
1425
1426 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001427 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001428 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001429 */
drha6c2ed92009-11-14 23:22:23 +00001430 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001431 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001432 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001433 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001434 assert( (pMem[i].flags & MEM_Ephem)==0
1435 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001436 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001437 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001438 }
drh28039692008-03-17 16:54:01 +00001439 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001440
drh3d2a5292016-07-13 22:55:01 +00001441 if( db->mTrace & SQLITE_TRACE_ROW ){
1442 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1443 }
1444
drhd4e70eb2008-01-02 00:34:36 +00001445 /* Return SQLITE_ROW
1446 */
drhf56fa462015-04-13 21:39:54 +00001447 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001448 rc = SQLITE_ROW;
1449 goto vdbe_return;
1450}
1451
drh5b6afba2008-01-05 16:29:28 +00001452/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001453** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001454**
drh5b6afba2008-01-05 16:29:28 +00001455** Add the text in register P1 onto the end of the text in
1456** register P2 and store the result in register P3.
1457** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001458**
1459** P3 = P2 || P1
1460**
1461** It is illegal for P1 and P3 to be the same register. Sometimes,
1462** if P3 is the same register as P2, the implementation is able
1463** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001464*/
drh5b6afba2008-01-05 16:29:28 +00001465case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001466 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001467
drh3c657212009-11-17 23:59:58 +00001468 pIn1 = &aMem[pOp->p1];
1469 pIn2 = &aMem[pOp->p2];
1470 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001471 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001472 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001473 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001474 break;
drh5e00f6c2001-09-13 13:46:56 +00001475 }
drha0c06522009-06-17 22:50:41 +00001476 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001477 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001478 Stringify(pIn2, encoding);
1479 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001480 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001481 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001482 }
drh9c1905f2008-12-10 22:32:56 +00001483 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001484 goto no_mem;
1485 }
drhc91b2fd2014-03-01 18:13:23 +00001486 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001487 if( pOut!=pIn2 ){
1488 memcpy(pOut->z, pIn2->z, pIn2->n);
1489 }
1490 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001491 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001492 pOut->z[nByte+1] = 0;
1493 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001494 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001495 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001496 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001497 break;
1498}
drh75897232000-05-29 14:26:00 +00001499
drh3c84ddf2008-01-09 02:15:38 +00001500/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001501** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001502**
drh60a713c2008-01-21 16:22:45 +00001503** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001504** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001505** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001506*/
drh3c84ddf2008-01-09 02:15:38 +00001507/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001508** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001509**
drh3c84ddf2008-01-09 02:15:38 +00001510**
shane21e7feb2008-05-30 15:59:49 +00001511** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001512** and store the result in register P3.
1513** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001514*/
drh3c84ddf2008-01-09 02:15:38 +00001515/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001516** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001517**
drh60a713c2008-01-21 16:22:45 +00001518** Subtract the value in register P1 from the value in register P2
1519** and store the result in register P3.
1520** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001521*/
drh9cbf3422008-01-17 16:22:13 +00001522/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001523** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001524**
drh60a713c2008-01-21 16:22:45 +00001525** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001526** and store the result in register P3 (P3=P2/P1). If the value in
1527** register P1 is zero, then the result is NULL. If either input is
1528** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001529*/
drh9cbf3422008-01-17 16:22:13 +00001530/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001531** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001532**
drh40864a12013-11-15 18:58:37 +00001533** Compute the remainder after integer register P2 is divided by
1534** register P1 and store the result in register P3.
1535** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001536** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001537*/
drh5b6afba2008-01-05 16:29:28 +00001538case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1539case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1540case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1541case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1542case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001543 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001544 u16 flags; /* Combined MEM_* flags from both inputs */
1545 u16 type1; /* Numeric type of left operand */
1546 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001547 i64 iA; /* Integer value of left operand */
1548 i64 iB; /* Integer value of right operand */
1549 double rA; /* Real value of left operand */
1550 double rB; /* Real value of right operand */
1551
drh3c657212009-11-17 23:59:58 +00001552 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001553 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001554 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001555 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001556 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001557 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001558 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001559 iA = pIn1->u.i;
1560 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001561 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001562 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001563 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1564 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1565 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001566 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001567 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001568 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001569 iB /= iA;
drh75897232000-05-29 14:26:00 +00001570 break;
1571 }
drhbf4133c2001-10-13 02:59:08 +00001572 default: {
drh856c1032009-06-02 15:21:42 +00001573 if( iA==0 ) goto arithmetic_result_is_null;
1574 if( iA==-1 ) iA = 1;
1575 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001576 break;
1577 }
drh75897232000-05-29 14:26:00 +00001578 }
drh856c1032009-06-02 15:21:42 +00001579 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001580 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001581 }else if( (flags & MEM_Null)!=0 ){
1582 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001583 }else{
drhbe707b32012-12-10 22:19:14 +00001584 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001585fp_math:
drh856c1032009-06-02 15:21:42 +00001586 rA = sqlite3VdbeRealValue(pIn1);
1587 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001588 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001589 case OP_Add: rB += rA; break;
1590 case OP_Subtract: rB -= rA; break;
1591 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001592 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001593 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001594 if( rA==(double)0 ) goto arithmetic_result_is_null;
1595 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001596 break;
1597 }
drhbf4133c2001-10-13 02:59:08 +00001598 default: {
drhe3b89d22019-01-18 17:53:50 +00001599 iA = sqlite3VdbeIntValue(pIn1);
1600 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001601 if( iA==0 ) goto arithmetic_result_is_null;
1602 if( iA==-1 ) iA = 1;
1603 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001604 break;
1605 }
drh5e00f6c2001-09-13 13:46:56 +00001606 }
drhc5a7b512010-01-13 16:25:42 +00001607#ifdef SQLITE_OMIT_FLOATING_POINT
1608 pOut->u.i = rB;
1609 MemSetTypeFlag(pOut, MEM_Int);
1610#else
drh856c1032009-06-02 15:21:42 +00001611 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001612 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001613 }
drh74eaba42014-09-18 17:52:15 +00001614 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001615 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001616 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001617 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001618 }
drhc5a7b512010-01-13 16:25:42 +00001619#endif
drh5e00f6c2001-09-13 13:46:56 +00001620 }
1621 break;
1622
drha05a7222008-01-19 03:35:58 +00001623arithmetic_result_is_null:
1624 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001625 break;
1626}
1627
drh7a957892012-02-02 17:35:43 +00001628/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001629**
drhbb6783b2017-04-29 18:02:49 +00001630** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001631** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1632** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001633** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001634**
drh7a957892012-02-02 17:35:43 +00001635** If P1 is not zero, then it is a register that a subsequent min() or
1636** max() aggregate will set to 1 if the current row is not the minimum or
1637** maximum. The P1 register is initialized to 0 by this instruction.
1638**
danielk1977dc1bdc42004-06-11 10:51:27 +00001639** The interface used by the implementation of the aforementioned functions
1640** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001641** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001642*/
drh9cbf3422008-01-17 16:22:13 +00001643case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001644 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001645 if( pOp->p1 ){
1646 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1647 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001648 break;
1649}
1650
drh98757152008-01-09 23:04:12 +00001651/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001652** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001653**
drh98757152008-01-09 23:04:12 +00001654** Take the bit-wise AND of the values in register P1 and P2 and
1655** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001656** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001657*/
drh98757152008-01-09 23:04:12 +00001658/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001659** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001660**
drh98757152008-01-09 23:04:12 +00001661** Take the bit-wise OR of the values in register P1 and P2 and
1662** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001663** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001664*/
drh98757152008-01-09 23:04:12 +00001665/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001666** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001667**
drh98757152008-01-09 23:04:12 +00001668** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001669** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001670** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001671** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001672*/
drh98757152008-01-09 23:04:12 +00001673/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001674** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001675**
drh98757152008-01-09 23:04:12 +00001676** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001677** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001678** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001679** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001680*/
drh5b6afba2008-01-05 16:29:28 +00001681case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1682case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1683case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1684case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001685 i64 iA;
1686 u64 uA;
1687 i64 iB;
1688 u8 op;
drh6810ce62004-01-31 19:22:56 +00001689
drh3c657212009-11-17 23:59:58 +00001690 pIn1 = &aMem[pOp->p1];
1691 pIn2 = &aMem[pOp->p2];
1692 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001693 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001694 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001695 break;
1696 }
drh158b9cb2011-03-05 20:59:46 +00001697 iA = sqlite3VdbeIntValue(pIn2);
1698 iB = sqlite3VdbeIntValue(pIn1);
1699 op = pOp->opcode;
1700 if( op==OP_BitAnd ){
1701 iA &= iB;
1702 }else if( op==OP_BitOr ){
1703 iA |= iB;
1704 }else if( iB!=0 ){
1705 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1706
1707 /* If shifting by a negative amount, shift in the other direction */
1708 if( iB<0 ){
1709 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1710 op = 2*OP_ShiftLeft + 1 - op;
1711 iB = iB>(-64) ? -iB : 64;
1712 }
1713
1714 if( iB>=64 ){
1715 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1716 }else{
1717 memcpy(&uA, &iA, sizeof(uA));
1718 if( op==OP_ShiftLeft ){
1719 uA <<= iB;
1720 }else{
1721 uA >>= iB;
1722 /* Sign-extend on a right shift of a negative number */
1723 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1724 }
1725 memcpy(&iA, &uA, sizeof(iA));
1726 }
drhbf4133c2001-10-13 02:59:08 +00001727 }
drh158b9cb2011-03-05 20:59:46 +00001728 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001729 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001730 break;
1731}
1732
drh8558cde2008-01-05 05:20:10 +00001733/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001734** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001735**
danielk19770cdc0222008-06-26 18:04:03 +00001736** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001737** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001738**
drh8558cde2008-01-05 05:20:10 +00001739** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001740*/
drh9cbf3422008-01-17 16:22:13 +00001741case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001742 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001743 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001744 sqlite3VdbeMemIntegerify(pIn1);
1745 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001746 break;
1747}
1748
dane5166e02019-03-19 11:56:39 +00001749/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001750**
dane5166e02019-03-19 11:56:39 +00001751** Force the value in register P1 to be an integer. If the value
1752** in P1 is not an integer and cannot be converted into an integer
1753** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001754** raise an SQLITE_MISMATCH exception.
1755*/
drh9cbf3422008-01-17 16:22:13 +00001756case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001757 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001758 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001759 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001760 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001761 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001762 if( pOp->p2==0 ){
1763 rc = SQLITE_MISMATCH;
1764 goto abort_due_to_error;
1765 }else{
drhf56fa462015-04-13 21:39:54 +00001766 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001767 }
drh8aff1012001-12-22 14:49:24 +00001768 }
drh8aff1012001-12-22 14:49:24 +00001769 }
drhc9065332019-04-01 14:01:21 +00001770 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001771 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001772 break;
1773}
1774
drh13573c72010-01-12 17:04:07 +00001775#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001776/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001777**
drh2133d822008-01-03 18:44:59 +00001778** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001779**
drh8a512562005-11-14 22:29:05 +00001780** This opcode is used when extracting information from a column that
1781** has REAL affinity. Such column values may still be stored as
1782** integers, for space efficiency, but after extraction we want them
1783** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001784*/
drh9cbf3422008-01-17 16:22:13 +00001785case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001786 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001787 if( pIn1->flags & MEM_Int ){
1788 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001789 }
drh487e2622005-06-25 18:42:14 +00001790 break;
1791}
drh13573c72010-01-12 17:04:07 +00001792#endif
drh487e2622005-06-25 18:42:14 +00001793
drh8df447f2005-11-01 15:48:24 +00001794#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001795/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001796** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001797**
drh4169e432014-08-25 20:11:52 +00001798** Force the value in register P1 to be the type defined by P2.
1799**
1800** <ul>
drhbb6783b2017-04-29 18:02:49 +00001801** <li> P2=='A' &rarr; BLOB
1802** <li> P2=='B' &rarr; TEXT
1803** <li> P2=='C' &rarr; NUMERIC
1804** <li> P2=='D' &rarr; INTEGER
1805** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001806** </ul>
drh487e2622005-06-25 18:42:14 +00001807**
1808** A NULL value is not changed by this routine. It remains NULL.
1809*/
drh4169e432014-08-25 20:11:52 +00001810case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001811 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001812 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001813 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001814 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1815 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1816 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001817 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001818 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001819 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001820 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001821 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001822 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001823 break;
1824}
drh8a512562005-11-14 22:29:05 +00001825#endif /* SQLITE_OMIT_CAST */
1826
drh79752b62016-08-13 10:02:17 +00001827/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001828** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001829**
1830** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1831** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1832** store the result of comparison in register P2.
1833**
1834** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1835** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1836** to coerce both inputs according to this affinity before the
1837** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1838** affinity is used. Note that the affinity conversions are stored
1839** back into the input registers P1 and P3. So this opcode can cause
1840** persistent changes to registers P1 and P3.
1841**
1842** Once any conversions have taken place, and neither value is NULL,
1843** the values are compared. If both values are blobs then memcmp() is
1844** used to determine the results of the comparison. If both values
1845** are text, then the appropriate collating function specified in
1846** P4 is used to do the comparison. If P4 is not specified then
1847** memcmp() is used to compare text string. If both values are
1848** numeric, then a numeric comparison is used. If the two values
1849** are of different types, then numbers are considered less than
1850** strings and strings are considered less than blobs.
1851**
1852** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1853** true or false and is never NULL. If both operands are NULL then the result
1854** of comparison is true. If either operand is NULL then the result is false.
1855** If neither operand is NULL the result is the same as it would be if
1856** the SQLITE_NULLEQ flag were omitted from P5.
1857**
1858** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001859** content of r[P2] is only changed if the new value is NULL or 0 (false).
1860** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001861*/
1862/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001863** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001864**
1865** This works just like the Eq opcode except that the jump is taken if
1866** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1867** additional information.
1868**
1869** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001870** content of r[P2] is only changed if the new value is NULL or 1 (true).
1871** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001872*/
drh35573352008-01-08 23:54:25 +00001873/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001874** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001875**
drh35573352008-01-08 23:54:25 +00001876** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001877** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1878** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001879**
drh35573352008-01-08 23:54:25 +00001880** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001881** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001882** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001883**
drh35573352008-01-08 23:54:25 +00001884** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001885** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001886** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001887** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001888** affinity is used. Note that the affinity conversions are stored
1889** back into the input registers P1 and P3. So this opcode can cause
1890** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001891**
1892** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001893** the values are compared. If both values are blobs then memcmp() is
1894** used to determine the results of the comparison. If both values
1895** are text, then the appropriate collating function specified in
1896** P4 is used to do the comparison. If P4 is not specified then
1897** memcmp() is used to compare text string. If both values are
1898** numeric, then a numeric comparison is used. If the two values
1899** are of different types, then numbers are considered less than
1900** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001901*/
drh9cbf3422008-01-17 16:22:13 +00001902/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001903** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001904**
drh35573352008-01-08 23:54:25 +00001905** This works just like the Lt opcode except that the jump is taken if
1906** the content of register P3 is less than or equal to the content of
1907** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001908*/
drh9cbf3422008-01-17 16:22:13 +00001909/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001910** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001911**
drh35573352008-01-08 23:54:25 +00001912** This works just like the Lt opcode except that the jump is taken if
1913** the content of register P3 is greater than the content of
1914** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001915*/
drh9cbf3422008-01-17 16:22:13 +00001916/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001917** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001918**
drh35573352008-01-08 23:54:25 +00001919** This works just like the Lt opcode except that the jump is taken if
1920** the content of register P3 is greater than or equal to the content of
1921** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001922*/
drh9cbf3422008-01-17 16:22:13 +00001923case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1924case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1925case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1926case OP_Le: /* same as TK_LE, jump, in1, in3 */
1927case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1928case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001929 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001930 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001931 u16 flags1; /* Copy of initial value of pIn1->flags */
1932 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001933
drh3c657212009-11-17 23:59:58 +00001934 pIn1 = &aMem[pOp->p1];
1935 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001936 flags1 = pIn1->flags;
1937 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001938 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001939 /* One or both operands are NULL */
1940 if( pOp->p5 & SQLITE_NULLEQ ){
1941 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1942 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1943 ** or not both operands are null.
1944 */
drh053a1282012-09-19 21:15:46 +00001945 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00001946 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
1947 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00001948 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001949 && (flags3&MEM_Cleared)==0
1950 ){
drh4910a762016-09-03 01:46:15 +00001951 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00001952 }else{
danbdabe742019-03-18 16:51:24 +00001953 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00001954 }
drh6a2fe092009-09-23 02:29:36 +00001955 }else{
1956 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1957 ** then the result is always NULL.
1958 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1959 */
drh688852a2014-02-17 22:40:43 +00001960 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001961 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001962 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00001963 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001964 MemSetTypeFlag(pOut, MEM_Null);
1965 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001966 }else{
drhf4345e42014-02-18 11:31:59 +00001967 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001968 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001969 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001970 }
drh6a2fe092009-09-23 02:29:36 +00001971 }
1972 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001973 }
drh6a2fe092009-09-23 02:29:36 +00001974 }else{
1975 /* Neither operand is NULL. Do a comparison. */
1976 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001977 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00001978 if( (flags1 | flags3)&MEM_Str ){
1979 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1980 applyNumericAffinity(pIn1,0);
drh24846bc2018-08-06 01:21:53 +00001981 assert( flags3==pIn3->flags );
drhcfdeeeb2018-08-04 20:12:10 +00001982 /* testcase( flags3!=pIn3->flags );
1983 ** this used to be possible with pIn1==pIn3, but not since
1984 ** the column cache was removed. The following assignment
drh24846bc2018-08-06 01:21:53 +00001985 ** is essentially a no-op. But, it provides defense-in-depth
drhcfdeeeb2018-08-04 20:12:10 +00001986 ** in case our analysis is incorrect, so it is left in. */
drh4b37cd42016-06-25 11:43:47 +00001987 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00001988 }
1989 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1990 applyNumericAffinity(pIn3,0);
1991 }
drh24a09622014-09-18 16:28:59 +00001992 }
drh64caee42016-09-09 19:33:00 +00001993 /* Handle the common case of integer comparison here, as an
1994 ** optimization, to avoid a call to sqlite3MemCompare() */
1995 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
1996 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
1997 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
1998 res = 0;
1999 goto compare_op;
2000 }
drh24a09622014-09-18 16:28:59 +00002001 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00002002 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002003 testcase( pIn1->flags & MEM_Int );
2004 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002005 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002006 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2007 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00002008 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00002009 }
drhe5520e22015-12-31 04:34:26 +00002010 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002011 testcase( pIn3->flags & MEM_Int );
2012 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002013 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002014 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2015 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002016 }
drh6a2fe092009-09-23 02:29:36 +00002017 }
drh6a2fe092009-09-23 02:29:36 +00002018 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002019 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002020 }
drh64caee42016-09-09 19:33:00 +00002021compare_op:
drh58596362017-08-03 00:29:23 +00002022 /* At this point, res is negative, zero, or positive if reg[P1] is
2023 ** less than, equal to, or greater than reg[P3], respectively. Compute
2024 ** the answer to this operator in res2, depending on what the comparison
2025 ** operator actually is. The next block of code depends on the fact
2026 ** that the 6 comparison operators are consecutive integers in this
2027 ** order: NE, EQ, GT, LE, LT, GE */
2028 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2029 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2030 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2031 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2032 res2 = aLTb[pOp->opcode - OP_Ne];
2033 }else if( res==0 ){
2034 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2035 res2 = aEQb[pOp->opcode - OP_Ne];
2036 }else{
2037 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2038 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002039 }
2040
drhf56fa462015-04-13 21:39:54 +00002041 /* Undo any changes made by applyAffinity() to the input registers. */
2042 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2043 pIn1->flags = flags1;
2044 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2045 pIn3->flags = flags3;
2046
drh35573352008-01-08 23:54:25 +00002047 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002048 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002049 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002050 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002051 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002052 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2053 ** is only used in contexts where either:
2054 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2055 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2056 ** Therefore it is not necessary to check the content of r[P2] for
2057 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002058 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002059 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002060 testcase( res2==0 && pOp->opcode==OP_Eq );
2061 testcase( res2==1 && pOp->opcode==OP_Eq );
2062 testcase( res2==0 && pOp->opcode==OP_Ne );
2063 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002064 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002065 }
drh2b4ded92010-09-27 21:09:31 +00002066 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002067 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002068 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002069 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002070 }else{
drh6cbbcd82019-04-01 13:06:19 +00002071 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002072 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002073 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002074 }
danielk1977a37cdde2004-05-16 11:15:36 +00002075 }
2076 break;
2077}
drhc9b84a12002-06-20 11:36:48 +00002078
drh79752b62016-08-13 10:02:17 +00002079/* Opcode: ElseNotEq * P2 * * *
2080**
drhfd7459e2016-09-17 17:39:01 +00002081** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2082** If result of an OP_Eq comparison on the same two operands
2083** would have be NULL or false (0), then then jump to P2.
2084** If the result of an OP_Eq comparison on the two previous operands
2085** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002086*/
2087case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2088 assert( pOp>aOp );
2089 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002090 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002091 VdbeBranchTaken(iCompare!=0, 2);
2092 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002093 break;
2094}
2095
2096
drh0acb7e42008-06-25 00:12:41 +00002097/* Opcode: Permutation * * * P4 *
2098**
drhb7dab702017-01-26 18:00:00 +00002099** Set the permutation used by the OP_Compare operator in the next
2100** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002101**
drh953f7612012-12-07 22:18:54 +00002102** The permutation is only valid until the next OP_Compare that has
2103** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2104** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002105**
2106** The first integer in the P4 integer array is the length of the array
2107** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002108*/
2109case OP_Permutation: {
2110 assert( pOp->p4type==P4_INTARRAY );
2111 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002112 assert( pOp[1].opcode==OP_Compare );
2113 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002114 break;
2115}
2116
drh953f7612012-12-07 22:18:54 +00002117/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002118** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002119**
drh710c4842010-08-30 01:17:20 +00002120** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2121** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002122** the comparison for use by the next OP_Jump instruct.
2123**
drh0ca10df2012-12-08 13:26:23 +00002124** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2125** determined by the most recent OP_Permutation operator. If the
2126** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2127** order.
2128**
drh0acb7e42008-06-25 00:12:41 +00002129** P4 is a KeyInfo structure that defines collating sequences and sort
2130** orders for the comparison. The permutation applies to registers
2131** only. The KeyInfo elements are used sequentially.
2132**
2133** The comparison is a sort comparison, so NULLs compare equal,
2134** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002135** and strings are less than blobs.
2136*/
2137case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002138 int n;
2139 int i;
2140 int p1;
2141 int p2;
2142 const KeyInfo *pKeyInfo;
2143 int idx;
2144 CollSeq *pColl; /* Collating sequence to use on this term */
2145 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002146 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002147
drhb7dab702017-01-26 18:00:00 +00002148 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2149 aPermute = 0;
2150 }else{
2151 assert( pOp>aOp );
2152 assert( pOp[-1].opcode==OP_Permutation );
2153 assert( pOp[-1].p4type==P4_INTARRAY );
2154 aPermute = pOp[-1].p4.ai + 1;
2155 assert( aPermute!=0 );
2156 }
drh856c1032009-06-02 15:21:42 +00002157 n = pOp->p3;
2158 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002159 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002160 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002161 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002162 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002163#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002164 if( aPermute ){
2165 int k, mx = 0;
2166 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002167 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2168 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002169 }else{
drh9f6168b2016-03-19 23:32:58 +00002170 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2171 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002172 }
2173#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002174 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002175 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002176 assert( memIsValid(&aMem[p1+idx]) );
2177 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002178 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2179 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002180 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002181 pColl = pKeyInfo->aColl[i];
2182 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002183 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002184 if( iCompare ){
2185 if( bRev ) iCompare = -iCompare;
2186 break;
2187 }
drh16ee60f2008-06-20 18:13:25 +00002188 }
2189 break;
2190}
2191
2192/* Opcode: Jump P1 P2 P3 * *
2193**
2194** Jump to the instruction at address P1, P2, or P3 depending on whether
2195** in the most recent OP_Compare instruction the P1 vector was less than
2196** equal to, or greater than the P2 vector, respectively.
2197*/
drh0acb7e42008-06-25 00:12:41 +00002198case OP_Jump: { /* jump */
2199 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002200 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002201 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002202 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002203 }else{
drh7083a482018-07-10 16:04:04 +00002204 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002205 }
2206 break;
2207}
2208
drh5b6afba2008-01-05 16:29:28 +00002209/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002210** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002211**
drh5b6afba2008-01-05 16:29:28 +00002212** Take the logical AND of the values in registers P1 and P2 and
2213** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002214**
drh5b6afba2008-01-05 16:29:28 +00002215** If either P1 or P2 is 0 (false) then the result is 0 even if
2216** the other input is NULL. A NULL and true or two NULLs give
2217** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002218*/
drh5b6afba2008-01-05 16:29:28 +00002219/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002220** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002221**
2222** Take the logical OR of the values in register P1 and P2 and
2223** store the answer in register P3.
2224**
2225** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2226** even if the other input is NULL. A NULL and false or two NULLs
2227** give a NULL output.
2228*/
2229case OP_And: /* same as TK_AND, in1, in2, out3 */
2230case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002231 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2232 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002233
drh1fcfa722018-02-26 15:27:31 +00002234 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2235 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002236 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002237 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002238 v1 = and_logic[v1*3+v2];
2239 }else{
drh5b6afba2008-01-05 16:29:28 +00002240 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002241 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002242 }
drh3c657212009-11-17 23:59:58 +00002243 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002244 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002245 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002246 }else{
drh5b6afba2008-01-05 16:29:28 +00002247 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002248 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002249 }
drh5e00f6c2001-09-13 13:46:56 +00002250 break;
2251}
2252
drh8abed7b2018-02-26 18:49:05 +00002253/* Opcode: IsTrue P1 P2 P3 P4 *
2254** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2255**
2256** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2257** IS NOT FALSE operators.
2258**
drh96acafb2018-02-27 14:49:25 +00002259** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002260** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2261** NULL, then the P3 is stored in register P2. Invert the answer if P4
2262** is 1.
2263**
2264** The logic is summarized like this:
2265**
2266** <ul>
drh96acafb2018-02-27 14:49:25 +00002267** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2268** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2269** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2270** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002271** </ul>
2272*/
2273case OP_IsTrue: { /* in1, out2 */
2274 assert( pOp->p4type==P4_INT32 );
2275 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002276 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002277 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2278 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2279 break;
2280}
2281
drhe99fa2a2008-12-15 15:27:51 +00002282/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002283** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002284**
drhe99fa2a2008-12-15 15:27:51 +00002285** Interpret the value in register P1 as a boolean value. Store the
2286** boolean complement in register P2. If the value in register P1 is
2287** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002288*/
drh93952eb2009-11-13 19:43:43 +00002289case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002290 pIn1 = &aMem[pOp->p1];
2291 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002292 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002293 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002294 }else{
2295 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002296 }
drh5e00f6c2001-09-13 13:46:56 +00002297 break;
2298}
2299
drhe99fa2a2008-12-15 15:27:51 +00002300/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002301** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002302**
drhe99fa2a2008-12-15 15:27:51 +00002303** Interpret the content of register P1 as an integer. Store the
2304** ones-complement of the P1 value into register P2. If P1 holds
2305** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002306*/
drh93952eb2009-11-13 19:43:43 +00002307case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002308 pIn1 = &aMem[pOp->p1];
2309 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002310 sqlite3VdbeMemSetNull(pOut);
2311 if( (pIn1->flags & MEM_Null)==0 ){
2312 pOut->flags = MEM_Int;
2313 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002314 }
drhbf4133c2001-10-13 02:59:08 +00002315 break;
2316}
2317
drh48f2d3b2011-09-16 01:34:43 +00002318/* Opcode: Once P1 P2 * * *
2319**
drhab087d42017-03-24 17:59:56 +00002320** Fall through to the next instruction the first time this opcode is
2321** encountered on each invocation of the byte-code program. Jump to P2
2322** on the second and all subsequent encounters during the same invocation.
2323**
2324** Top-level programs determine first invocation by comparing the P1
2325** operand against the P1 operand on the OP_Init opcode at the beginning
2326** of the program. If the P1 values differ, then fall through and make
2327** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2328** the same then take the jump.
2329**
2330** For subprograms, there is a bitmask in the VdbeFrame that determines
2331** whether or not the jump should be taken. The bitmask is necessary
2332** because the self-altering code trick does not work for recursive
2333** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002334*/
dan1d8cb212011-12-09 13:24:16 +00002335case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002336 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002337 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002338 if( p->pFrame ){
2339 iAddr = (int)(pOp - p->aOp);
2340 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2341 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002342 goto jump_to_p2;
2343 }
drh18333ef2017-03-24 18:38:41 +00002344 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002345 }else{
drhab087d42017-03-24 17:59:56 +00002346 if( p->aOp[0].p1==pOp->p1 ){
2347 VdbeBranchTaken(1, 2);
2348 goto jump_to_p2;
2349 }
dan1d8cb212011-12-09 13:24:16 +00002350 }
drhab087d42017-03-24 17:59:56 +00002351 VdbeBranchTaken(0, 2);
2352 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002353 break;
2354}
2355
drh3c84ddf2008-01-09 02:15:38 +00002356/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002357**
drhef8662b2011-06-20 21:47:58 +00002358** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002359** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002360** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002361*/
drh1fcfa722018-02-26 15:27:31 +00002362case OP_If: { /* jump, in1 */
2363 int c;
2364 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2365 VdbeBranchTaken(c!=0, 2);
2366 if( c ) goto jump_to_p2;
2367 break;
2368}
2369
drh3c84ddf2008-01-09 02:15:38 +00002370/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002371**
drhef8662b2011-06-20 21:47:58 +00002372** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002373** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002374** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002375*/
drh9cbf3422008-01-17 16:22:13 +00002376case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002377 int c;
drh1fcfa722018-02-26 15:27:31 +00002378 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002379 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002380 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002381 break;
2382}
2383
drh830ecf92009-06-18 00:41:55 +00002384/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002385** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002386**
drh830ecf92009-06-18 00:41:55 +00002387** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002388*/
drh9cbf3422008-01-17 16:22:13 +00002389case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002390 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002391 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002392 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002393 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002394 }
drh477df4b2008-01-05 18:48:24 +00002395 break;
2396}
2397
drh98757152008-01-09 23:04:12 +00002398/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002399** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002400**
drh6a288a32008-01-07 19:20:24 +00002401** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002402*/
drh9cbf3422008-01-17 16:22:13 +00002403case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002404 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002405 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002406 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002407 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002408 }
drh5e00f6c2001-09-13 13:46:56 +00002409 break;
2410}
2411
drh31d6fd52017-04-14 19:03:10 +00002412/* Opcode: IfNullRow P1 P2 P3 * *
2413** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2414**
2415** Check the cursor P1 to see if it is currently pointing at a NULL row.
2416** If it is, then set register P3 to NULL and jump immediately to P2.
2417** If P1 is not on a NULL row, then fall through without making any
2418** changes.
2419*/
2420case OP_IfNullRow: { /* jump */
2421 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002422 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002423 if( p->apCsr[pOp->p1]->nullRow ){
2424 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2425 goto jump_to_p2;
2426 }
2427 break;
2428}
2429
drh092457b2017-12-29 15:04:49 +00002430#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2431/* Opcode: Offset P1 P2 P3 * *
2432** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002433**
drh092457b2017-12-29 15:04:49 +00002434** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002435** start of the payload for the record at which that cursor P1 is currently
2436** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002437**
drh092457b2017-12-29 15:04:49 +00002438** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002439** This opcode does not use P2 itself, but the P2 value is used by the
2440** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002441** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002442**
2443** This opcode is only available if SQLite is compiled with the
2444** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002445*/
drh092457b2017-12-29 15:04:49 +00002446case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002447 VdbeCursor *pC; /* The VDBE cursor */
2448 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2449 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002450 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002451 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002452 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002453 }else{
drh092457b2017-12-29 15:04:49 +00002454 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002455 }
2456 break;
2457}
drh092457b2017-12-29 15:04:49 +00002458#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002459
drh3e9ca092009-09-08 01:14:48 +00002460/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002461** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002462**
danielk1977cfcdaef2004-05-12 07:33:33 +00002463** Interpret the data that cursor P1 points to as a structure built using
2464** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002465** information about the format of the data.) Extract the P2-th column
2466** from this record. If there are less that (P2+1)
2467** values in the record, extract a NULL.
2468**
drh9cbf3422008-01-17 16:22:13 +00002469** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002470**
drh1cc3a362017-04-03 13:17:31 +00002471** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002472** if the P4 argument is a P4_MEM use the value of the P4 argument as
2473** the result.
drh3e9ca092009-09-08 01:14:48 +00002474**
2475** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2476** then the cache of the cursor is reset prior to extracting the column.
2477** The first OP_Column against a pseudo-table after the value of the content
2478** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002479**
drh1cc3a362017-04-03 13:17:31 +00002480** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002481** the result is guaranteed to only be used as the argument of a length()
2482** or typeof() function, respectively. The loading of large blobs can be
2483** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002484*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002485case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002486 int p2; /* column number to retrieve */
2487 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002488 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002489 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002490 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002491 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002492 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002493 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002494 const u8 *zData; /* Part of the record being decoded */
2495 const u8 *zHdr; /* Next unparsed byte of the header */
2496 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002497 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002498 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002499 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002500
dande892d92016-01-29 19:29:45 +00002501 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002502 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002503
drh170ad682017-06-02 15:44:22 +00002504 /* If the cursor cache is stale (meaning it is not currently point at
2505 ** the correct row) then bring it up-to-date by doing the necessary
2506 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002507 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002508 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002509
drh9f6168b2016-03-19 23:32:58 +00002510 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002511 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002512 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002513 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002514 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002515 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002516 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002517 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002518 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2519 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002520
drha43a02e2016-05-19 17:51:19 +00002521 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002522 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002523 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002524 /* For the special case of as pseudo-cursor, the seekResult field
2525 ** identifies the register that holds the record */
2526 assert( pC->seekResult>0 );
2527 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002528 assert( pReg->flags & MEM_Blob );
2529 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002530 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002531 pC->aRow = (u8*)pReg->z;
2532 }else{
drh6b5631e2014-11-05 15:57:39 +00002533 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002534 goto op_column_out;
2535 }
danielk1977192ac1d2004-05-10 07:17:30 +00002536 }else{
drh06a09a82016-11-25 17:03:03 +00002537 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002538 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002539 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002540 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2541 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002542 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2543 assert( pC->szRow<=pC->payloadSize );
2544 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2545 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002546 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002547 }
danielk1977192ac1d2004-05-10 07:17:30 +00002548 }
drhb73857f2006-03-17 00:25:59 +00002549 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002550 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002551 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002552
drhc81aa2e2014-10-11 23:31:52 +00002553
drh1f613c42017-08-16 14:16:19 +00002554 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002555 /* pC->aRow does not have to hold the entire row, but it does at least
2556 ** need to cover the header of the record. If pC->aRow does not contain
2557 ** the complete header, then set it to zero, forcing the header to be
2558 ** dynamically allocated. */
2559 pC->aRow = 0;
2560 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002561
2562 /* Make sure a corrupt database has not given us an oversize header.
2563 ** Do this now to avoid an oversize memory allocation.
2564 **
2565 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2566 ** types use so much data space that there can only be 4096 and 32 of
2567 ** them, respectively. So the maximum header length results from a
2568 ** 3-byte type for each of the maximum of 32768 columns plus three
2569 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2570 */
drh1f613c42017-08-16 14:16:19 +00002571 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002572 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002573 }
drh95b225a2017-08-16 11:04:22 +00002574 }else{
2575 /* This is an optimization. By skipping over the first few tests
2576 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2577 ** measurable performance gain.
2578 **
drh1f613c42017-08-16 14:16:19 +00002579 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002580 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002581 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002582 ** branch jumps to reads past the end of the record, but never more
2583 ** than a few bytes. Even if the record occurs at the end of the page
2584 ** content area, the "page header" comes after the page content and so
2585 ** this overread is harmless. Similar overreads can occur for a corrupt
2586 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002587 */
2588 zData = pC->aRow;
2589 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002590 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002591 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002592 }
drh399af1d2013-11-20 17:25:55 +00002593 }
drh35cd6432009-06-05 14:17:21 +00002594
drh399af1d2013-11-20 17:25:55 +00002595 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002596 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002597 */
drhc8606e42013-11-20 19:28:03 +00002598 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002599 /* If there is more header available for parsing in the record, try
2600 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002601 */
drhc8606e42013-11-20 19:28:03 +00002602 if( pC->iHdrOffset<aOffset[0] ){
2603 /* Make sure zData points to enough of the record to cover the header. */
2604 if( pC->aRow==0 ){
2605 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002606 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002607 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002608 zData = (u8*)sMem.z;
2609 }else{
2610 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002611 }
drhc8606e42013-11-20 19:28:03 +00002612
drh0c8f7602014-09-19 16:56:45 +00002613 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002614 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002615 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002616 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002617 zHdr = zData + pC->iHdrOffset;
2618 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002619 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002620 do{
drhc332e042019-02-12 21:04:33 +00002621 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002622 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002623 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002624 }else{
drhc8606e42013-11-20 19:28:03 +00002625 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002626 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002627 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002628 }
drhc332e042019-02-12 21:04:33 +00002629 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002630 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002631
drh8dd83622014-10-13 23:39:02 +00002632 /* The record is corrupt if any of the following are true:
2633 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002634 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002635 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002636 */
drhc6ce38832015-10-15 21:30:24 +00002637 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2638 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002639 ){
drh95b225a2017-08-16 11:04:22 +00002640 if( aOffset[0]==0 ){
2641 i = 0;
2642 zHdr = zEndHdr;
2643 }else{
2644 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002645 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002646 }
danielk1977dedf45b2006-01-13 17:12:01 +00002647 }
drhddb2b4a2016-03-25 12:10:32 +00002648
drh170c2762016-05-20 21:40:11 +00002649 pC->nHdrParsed = i;
2650 pC->iHdrOffset = (u32)(zHdr - zData);
2651 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002652 }else{
drh9fbc8852016-01-04 03:48:46 +00002653 t = 0;
drh9188b382004-05-14 21:12:22 +00002654 }
drhd3194f52004-05-27 19:59:32 +00002655
drhf2db3382015-04-30 20:33:25 +00002656 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002657 ** still not up to p2, that means that the record has fewer than p2
2658 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002659 */
drhc8606e42013-11-20 19:28:03 +00002660 if( pC->nHdrParsed<=p2 ){
2661 if( pOp->p4type==P4_MEM ){
2662 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2663 }else{
drh22e8d832014-10-29 00:58:38 +00002664 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002665 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002666 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002667 }
drh95fa6062015-10-16 13:50:08 +00002668 }else{
2669 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002670 }
danielk1977192ac1d2004-05-10 07:17:30 +00002671
drh380d6852013-11-20 20:58:00 +00002672 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002673 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002674 ** all valid.
drh9188b382004-05-14 21:12:22 +00002675 */
drhc8606e42013-11-20 19:28:03 +00002676 assert( p2<pC->nHdrParsed );
2677 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002678 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002679 if( VdbeMemDynamic(pDest) ){
2680 sqlite3VdbeMemSetNull(pDest);
2681 }
drh95fa6062015-10-16 13:50:08 +00002682 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002683 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002684 /* This is the common case where the desired content fits on the original
2685 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002686 zData = pC->aRow + aOffset[p2];
2687 if( t<12 ){
2688 sqlite3VdbeSerialGet(zData, t, pDest);
2689 }else{
2690 /* If the column value is a string, we need a persistent value, not
2691 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2692 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2693 */
2694 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2695 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002696 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002697 if( pDest->szMalloc < len+2 ){
2698 pDest->flags = MEM_Null;
2699 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2700 }else{
2701 pDest->z = pDest->zMalloc;
2702 }
2703 memcpy(pDest->z, zData, len);
2704 pDest->z[len] = 0;
2705 pDest->z[len+1] = 0;
2706 pDest->flags = aFlag[t&1];
2707 }
danielk197736963fd2005-02-19 08:18:05 +00002708 }else{
drha1851ef2016-05-20 19:51:28 +00002709 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002710 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002711 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2712 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2713 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002714 ){
drh2a2a6962014-09-16 18:22:44 +00002715 /* Content is irrelevant for
2716 ** 1. the typeof() function,
2717 ** 2. the length(X) function if X is a blob, and
2718 ** 3. if the content length is zero.
2719 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002720 ** content from disk.
2721 **
2722 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2723 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2724 ** read up to 16. So 16 bytes of bogus content is supplied.
2725 */
2726 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002727 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002728 }else{
drhcb3cabd2016-11-25 19:18:28 +00002729 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002730 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2731 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2732 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002733 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002734 }
drhd3194f52004-05-27 19:59:32 +00002735
danielk19773c9cc8d2005-01-17 03:40:08 +00002736op_column_out:
drhb7654112008-01-12 12:48:07 +00002737 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002738 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002739 break;
drh74588ce2017-09-13 00:13:05 +00002740
2741op_column_corrupt:
2742 if( aOp[0].p3>0 ){
2743 pOp = &aOp[aOp[0].p3-1];
2744 break;
2745 }else{
2746 rc = SQLITE_CORRUPT_BKPT;
2747 goto abort_due_to_error;
2748 }
danielk1977192ac1d2004-05-10 07:17:30 +00002749}
2750
danielk1977751de562008-04-18 09:01:15 +00002751/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002752** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002753**
2754** Apply affinities to a range of P2 registers starting with P1.
2755**
drhbb6783b2017-04-29 18:02:49 +00002756** P4 is a string that is P2 characters long. The N-th character of the
2757** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002758** memory cell in the range.
2759*/
2760case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002761 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002762
drh856c1032009-06-02 15:21:42 +00002763 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002764 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002765 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002766 assert( zAffinity[pOp->p2]==0 );
2767 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002768 do{
drh9f6168b2016-03-19 23:32:58 +00002769 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002770 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002771 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002772 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002773 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002774 break;
2775}
2776
drh1db639c2008-01-17 02:36:28 +00002777/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002778** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002779**
drh710c4842010-08-30 01:17:20 +00002780** Convert P2 registers beginning with P1 into the [record format]
2781** use as a data record in a database table or as a key
2782** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002783**
drhbb6783b2017-04-29 18:02:49 +00002784** P4 may be a string that is P2 characters long. The N-th character of the
2785** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002786** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002787**
drh8a512562005-11-14 22:29:05 +00002788** The mapping from character to affinity is given by the SQLITE_AFF_
2789** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002790**
drh05883a32015-06-02 15:32:08 +00002791** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002792*/
drh1db639c2008-01-17 02:36:28 +00002793case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002794 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2795 Mem *pRec; /* The new record */
2796 u64 nData; /* Number of bytes of data space */
2797 int nHdr; /* Number of bytes of header space */
2798 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002799 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002800 int nVarint; /* Number of bytes in a varint */
2801 u32 serial_type; /* Type field */
2802 Mem *pData0; /* First field to be combined into the record */
2803 Mem *pLast; /* Last field of the record */
2804 int nField; /* Number of fields in the record */
2805 char *zAffinity; /* The affinity string for the record */
2806 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002807 int i; /* Space used in zNewRecord[] header */
2808 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002809 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002810
drhf3218fe2004-05-28 08:21:02 +00002811 /* Assuming the record contains N fields, the record format looks
2812 ** like this:
2813 **
drh7a224de2004-06-02 01:22:02 +00002814 ** ------------------------------------------------------------------------
2815 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2816 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002817 **
drh9cbf3422008-01-17 16:22:13 +00002818 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002819 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002820 **
2821 ** Each type field is a varint representing the serial type of the
2822 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002823 ** hdr-size field is also a varint which is the offset from the beginning
2824 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002825 */
drh856c1032009-06-02 15:21:42 +00002826 nData = 0; /* Number of bytes of data space */
2827 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002828 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002829 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002830 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002831 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002832 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002833 nField = pOp->p2;
2834 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002835 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002836
drh2b4ded92010-09-27 21:09:31 +00002837 /* Identify the output register */
2838 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2839 pOut = &aMem[pOp->p3];
2840 memAboutToChange(p, pOut);
2841
drh3e6c0602013-12-10 20:53:01 +00002842 /* Apply the requested affinity to all inputs
2843 */
2844 assert( pData0<=pLast );
2845 if( zAffinity ){
2846 pRec = pData0;
2847 do{
drh57bf4a82014-02-17 14:59:22 +00002848 applyAffinity(pRec++, *(zAffinity++), encoding);
2849 assert( zAffinity[0]==0 || pRec<=pLast );
2850 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002851 }
2852
drhd447dce2017-01-25 20:55:11 +00002853#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002854 /* NULLs can be safely trimmed from the end of the record, as long as
2855 ** as the schema format is 2 or more and none of the omitted columns
2856 ** have a non-NULL default value. Also, the record must be left with
2857 ** at least one field. If P5>0 then it will be one more than the
2858 ** index of the right-most column with a non-NULL default value */
2859 if( pOp->p5 ){
2860 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2861 pLast--;
2862 nField--;
2863 }
2864 }
drhd447dce2017-01-25 20:55:11 +00002865#endif
drh585ce192017-01-25 14:58:27 +00002866
drhf3218fe2004-05-28 08:21:02 +00002867 /* Loop through the elements that will make up the record to figure
2868 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002869 */
drh038b7bc2013-12-09 23:17:22 +00002870 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002871 do{
drh2b4ded92010-09-27 21:09:31 +00002872 assert( memIsValid(pRec) );
drh41fb3672018-01-12 23:18:38 +00002873 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002874 if( pRec->flags & MEM_Zero ){
drhce2fbd12018-01-12 21:00:14 +00002875 if( serial_type==0 ){
drh41fb3672018-01-12 23:18:38 +00002876 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
2877 ** table methods that never invoke sqlite3_result_xxxxx() while
2878 ** computing an unchanging column value in an UPDATE statement.
2879 ** Give such values a special internal-use-only serial-type of 10
2880 ** so that they can be passed through to xUpdate and have
2881 ** a true sqlite3_value_nochange(). */
2882 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
2883 serial_type = 10;
drhce2fbd12018-01-12 21:00:14 +00002884 }else if( nData ){
drh53e66c32015-07-24 15:49:23 +00002885 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002886 }else{
2887 nZero += pRec->u.nZero;
2888 len -= pRec->u.nZero;
2889 }
drhfdf972a2007-05-02 13:30:27 +00002890 }
drh8079a0d2006-01-12 17:20:50 +00002891 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002892 testcase( serial_type==127 );
2893 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002894 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh41fb3672018-01-12 23:18:38 +00002895 pRec->uTemp = serial_type;
drh45c3c662016-04-07 14:16:16 +00002896 if( pRec==pData0 ) break;
2897 pRec--;
2898 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002899
drh654858d2014-11-20 02:18:14 +00002900 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2901 ** which determines the total number of bytes in the header. The varint
2902 ** value is the size of the header in bytes including the size varint
2903 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002904 testcase( nHdr==126 );
2905 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002906 if( nHdr<=126 ){
2907 /* The common case */
2908 nHdr += 1;
2909 }else{
2910 /* Rare case of a really large header */
2911 nVarint = sqlite3VarintLen(nHdr);
2912 nHdr += nVarint;
2913 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002914 }
drh038b7bc2013-12-09 23:17:22 +00002915 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00002916
danielk1977a7a8e142008-02-13 18:25:27 +00002917 /* Make sure the output register has a buffer large enough to store
2918 ** the new record. The output register (pOp->p3) is not allowed to
2919 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002920 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002921 */
drh0d7f0cc2018-09-21 13:07:14 +00002922 if( nByte+nZero<=pOut->szMalloc ){
2923 /* The output register is already large enough to hold the record.
2924 ** No error checks or buffer enlargement is required */
2925 pOut->z = pOut->zMalloc;
2926 }else{
2927 /* Need to make sure that the output is not too big and then enlarge
2928 ** the output register to hold the full result */
2929 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
2930 goto too_big;
2931 }
2932 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
2933 goto no_mem;
2934 }
danielk19778d059842004-05-12 11:24:02 +00002935 }
danielk1977a7a8e142008-02-13 18:25:27 +00002936 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002937
2938 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002939 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002940 j = nHdr;
2941 assert( pData0<=pLast );
2942 pRec = pData0;
2943 do{
drhfacf47a2014-10-13 20:12:47 +00002944 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002945 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2946 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002947 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002948 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2949 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002950 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002951 }while( (++pRec)<=pLast );
2952 assert( i==nHdr );
2953 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002954
drh9f6168b2016-03-19 23:32:58 +00002955 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002956 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002957 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002958 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002959 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002960 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002961 }
drh1013c932008-01-06 00:25:21 +00002962 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002963 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002964 break;
2965}
2966
danielk1977a5533162009-02-24 10:01:51 +00002967/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002968** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002969**
2970** Store the number of entries (an integer value) in the table or index
2971** opened by cursor P1 in register P2
2972*/
2973#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002974case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002975 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002976 BtCursor *pCrsr;
2977
drhc960dcb2015-11-20 19:22:01 +00002978 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2979 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002980 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002981 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002982 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002983 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002984 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002985 pOut->u.i = nEntry;
2986 break;
2987}
2988#endif
2989
danielk1977fd7f0452008-12-17 17:30:26 +00002990/* Opcode: Savepoint P1 * * P4 *
2991**
2992** Open, release or rollback the savepoint named by parameter P4, depending
2993** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2994** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2995*/
2996case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002997 int p1; /* Value of P1 operand */
2998 char *zName; /* Name of savepoint */
2999 int nName;
3000 Savepoint *pNew;
3001 Savepoint *pSavepoint;
3002 Savepoint *pTmp;
3003 int iSavepoint;
3004 int ii;
3005
3006 p1 = pOp->p1;
3007 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003008
3009 /* Assert that the p1 parameter is valid. Also that if there is no open
3010 ** transaction, then there cannot be any savepoints.
3011 */
3012 assert( db->pSavepoint==0 || db->autoCommit==0 );
3013 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3014 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3015 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003016 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003017
3018 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003019 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003020 /* A new savepoint cannot be created if there are active write
3021 ** statements (i.e. open read/write incremental blob handles).
3022 */
drh22c17b82015-05-15 04:13:15 +00003023 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003024 rc = SQLITE_BUSY;
3025 }else{
drh856c1032009-06-02 15:21:42 +00003026 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003027
drhbe07ec52011-06-03 12:15:26 +00003028#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003029 /* This call is Ok even if this savepoint is actually a transaction
3030 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3031 ** If this is a transaction savepoint being opened, it is guaranteed
3032 ** that the db->aVTrans[] array is empty. */
3033 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003034 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3035 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003036 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003037#endif
dand9495cd2011-04-27 12:08:04 +00003038
danielk1977fd7f0452008-12-17 17:30:26 +00003039 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003040 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003041 if( pNew ){
3042 pNew->zName = (char *)&pNew[1];
3043 memcpy(pNew->zName, zName, nName+1);
3044
3045 /* If there is no open transaction, then mark this as a special
3046 ** "transaction savepoint". */
3047 if( db->autoCommit ){
3048 db->autoCommit = 0;
3049 db->isTransactionSavepoint = 1;
3050 }else{
3051 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003052 }
dan21e8d012011-03-03 20:05:59 +00003053
danielk1977fd7f0452008-12-17 17:30:26 +00003054 /* Link the new savepoint into the database handle's list. */
3055 pNew->pNext = db->pSavepoint;
3056 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003057 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003058 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003059 }
3060 }
3061 }else{
drh856c1032009-06-02 15:21:42 +00003062 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003063
3064 /* Find the named savepoint. If there is no such savepoint, then an
3065 ** an error is returned to the user. */
3066 for(
drh856c1032009-06-02 15:21:42 +00003067 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003068 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003069 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003070 ){
3071 iSavepoint++;
3072 }
3073 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003074 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003075 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003076 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003077 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003078 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003079 */
drh22c17b82015-05-15 04:13:15 +00003080 sqlite3VdbeError(p, "cannot release savepoint - "
3081 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003082 rc = SQLITE_BUSY;
3083 }else{
3084
3085 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003086 ** and this is a RELEASE command, then the current transaction
3087 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003088 */
3089 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3090 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003091 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003092 goto vdbe_return;
3093 }
danielk1977fd7f0452008-12-17 17:30:26 +00003094 db->autoCommit = 1;
3095 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003096 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003097 db->autoCommit = 0;
3098 p->rc = rc = SQLITE_BUSY;
3099 goto vdbe_return;
3100 }
danielk197734cf35d2008-12-18 18:31:38 +00003101 db->isTransactionSavepoint = 0;
3102 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003103 }else{
drh47b7fc72014-11-11 01:33:57 +00003104 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003105 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003106 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003107 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003108 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003109 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3110 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003111 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003112 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003113 }
drh47b7fc72014-11-11 01:33:57 +00003114 }else{
3115 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003116 }
3117 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003118 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3119 if( rc!=SQLITE_OK ){
3120 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003121 }
danielk1977fd7f0452008-12-17 17:30:26 +00003122 }
drh47b7fc72014-11-11 01:33:57 +00003123 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003124 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003125 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003126 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003127 }
3128 }
3129
3130 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3131 ** savepoints nested inside of the savepoint being operated on. */
3132 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003133 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003134 db->pSavepoint = pTmp->pNext;
3135 sqlite3DbFree(db, pTmp);
3136 db->nSavepoint--;
3137 }
3138
dan1da40a32009-09-19 17:00:31 +00003139 /* If it is a RELEASE, then destroy the savepoint being operated on
3140 ** too. If it is a ROLLBACK TO, then set the number of deferred
3141 ** constraint violations present in the database to the value stored
3142 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003143 if( p1==SAVEPOINT_RELEASE ){
3144 assert( pSavepoint==db->pSavepoint );
3145 db->pSavepoint = pSavepoint->pNext;
3146 sqlite3DbFree(db, pSavepoint);
3147 if( !isTransaction ){
3148 db->nSavepoint--;
3149 }
dan1da40a32009-09-19 17:00:31 +00003150 }else{
3151 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003152 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003153 }
dand9495cd2011-04-27 12:08:04 +00003154
danea8562e2015-04-18 16:25:54 +00003155 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003156 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3157 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3158 }
danielk1977fd7f0452008-12-17 17:30:26 +00003159 }
3160 }
drh9467abf2016-02-17 18:44:11 +00003161 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003162
3163 break;
3164}
3165
drh98757152008-01-09 23:04:12 +00003166/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003167**
3168** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003169** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003170** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3171** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003172**
3173** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003174*/
drh9cbf3422008-01-17 16:22:13 +00003175case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003176 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003177 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003178
drh856c1032009-06-02 15:21:42 +00003179 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003180 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003181 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003182 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003183 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003184 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003185
drhb0c88652016-02-01 13:21:13 +00003186 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003187 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003188 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003189 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003190 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003191 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3192 /* If this instruction implements a COMMIT and other VMs are writing
3193 ** return an error indicating that the other VMs must complete first.
3194 */
3195 sqlite3VdbeError(p, "cannot commit transaction - "
3196 "SQL statements in progress");
3197 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003198 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003199 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003200 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003201 }else{
shane7d3846a2008-12-11 02:58:26 +00003202 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003203 }
3204 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3205 p->pc = (int)(pOp - aOp);
3206 db->autoCommit = (u8)(1-desiredAutoCommit);
3207 p->rc = rc = SQLITE_BUSY;
3208 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003209 }
danielk1977bd434552009-03-18 10:33:00 +00003210 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003211 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003212 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003213 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003214 }else{
drh900b31e2007-08-28 02:27:51 +00003215 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003216 }
drh900b31e2007-08-28 02:27:51 +00003217 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003218 }else{
drh22c17b82015-05-15 04:13:15 +00003219 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003220 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003221 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003222 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003223
3224 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003225 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003226 }
3227 break;
3228}
3229
drhb22f7c82014-02-06 23:56:27 +00003230/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003231**
drh05a86c52014-02-16 01:55:49 +00003232** Begin a transaction on database P1 if a transaction is not already
3233** active.
3234** If P2 is non-zero, then a write-transaction is started, or if a
3235** read-transaction is already active, it is upgraded to a write-transaction.
3236** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003237**
drh001bbcb2003-03-19 03:14:00 +00003238** P1 is the index of the database file on which the transaction is
3239** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003240** file used for temporary tables. Indices of 2 or more are used for
3241** attached databases.
drhcabb0812002-09-14 13:47:32 +00003242**
dane0af83a2009-09-08 19:15:01 +00003243** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3244** true (this flag is set if the Vdbe may modify more than one row and may
3245** throw an ABORT exception), a statement transaction may also be opened.
3246** More specifically, a statement transaction is opened iff the database
3247** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003248** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003249** VDBE to be rolled back after an error without having to roll back the
3250** entire transaction. If no error is encountered, the statement transaction
3251** will automatically commit when the VDBE halts.
3252**
drhb22f7c82014-02-06 23:56:27 +00003253** If P5!=0 then this opcode also checks the schema cookie against P3
3254** and the schema generation counter against P4.
3255** The cookie changes its value whenever the database schema changes.
3256** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003257** and that the current process needs to reread the schema. If the schema
3258** cookie in P3 differs from the schema cookie in the database header or
3259** if the schema generation counter in P4 differs from the current
3260** generation counter, then an SQLITE_SCHEMA error is raised and execution
3261** halts. The sqlite3_step() wrapper function might then reprepare the
3262** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003263*/
drh9cbf3422008-01-17 16:22:13 +00003264case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003265 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003266 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003267
drh1713afb2013-06-28 01:24:57 +00003268 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003269 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003270 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003271 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003272 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3273 rc = SQLITE_READONLY;
3274 goto abort_due_to_error;
3275 }
drh653b82a2009-06-22 11:10:47 +00003276 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003277
danielk197724162fe2004-06-04 06:22:00 +00003278 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003279 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003280 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3281 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003282 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003283 if( (rc&0xff)==SQLITE_BUSY ){
3284 p->pc = (int)(pOp - aOp);
3285 p->rc = rc;
3286 goto vdbe_return;
3287 }
danielk197724162fe2004-06-04 06:22:00 +00003288 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003289 }
dane0af83a2009-09-08 19:15:01 +00003290
3291 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003292 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003293 ){
3294 assert( sqlite3BtreeIsInTrans(pBt) );
3295 if( p->iStatement==0 ){
3296 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3297 db->nStatement++;
3298 p->iStatement = db->nSavepoint + db->nStatement;
3299 }
dana311b802011-04-26 19:21:34 +00003300
drh346506f2011-05-25 01:16:42 +00003301 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003302 if( rc==SQLITE_OK ){
3303 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3304 }
dan1da40a32009-09-19 17:00:31 +00003305
3306 /* Store the current value of the database handles deferred constraint
3307 ** counter. If the statement transaction needs to be rolled back,
3308 ** the value of this counter needs to be restored too. */
3309 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003310 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003311 }
drh397776a2018-06-06 17:45:51 +00003312 }
3313 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3314 if( pOp->p5
3315 && (iMeta!=pOp->p3
3316 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3317 ){
3318 /*
drh96fdcb42016-09-27 00:09:33 +00003319 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3320 ** version is checked to ensure that the schema has not changed since the
3321 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003322 */
drhb22f7c82014-02-06 23:56:27 +00003323 sqlite3DbFree(db, p->zErrMsg);
3324 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3325 /* If the schema-cookie from the database file matches the cookie
3326 ** stored with the in-memory representation of the schema, do
3327 ** not reload the schema from the database file.
3328 **
3329 ** If virtual-tables are in use, this is not just an optimization.
3330 ** Often, v-tables store their data in other SQLite tables, which
3331 ** are queried from within xNext() and other v-table methods using
3332 ** prepared queries. If such a query is out-of-date, we do not want to
3333 ** discard the database schema, as the user code implementing the
3334 ** v-table would have to be ready for the sqlite3_vtab structure itself
3335 ** to be invalidated whenever sqlite3_step() is called from within
3336 ** a v-table method.
3337 */
3338 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3339 sqlite3ResetOneSchema(db, pOp->p1);
3340 }
3341 p->expired = 1;
3342 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003343 }
drh9467abf2016-02-17 18:44:11 +00003344 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003345 break;
3346}
3347
drhb1fdb2a2008-01-05 04:06:03 +00003348/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003349**
drh9cbf3422008-01-17 16:22:13 +00003350** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003351** P3==1 is the schema version. P3==2 is the database format.
3352** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003353** the main database file and P1==1 is the database file used to store
3354** temporary tables.
drh4a324312001-12-21 14:30:42 +00003355**
drh50e5dad2001-09-15 00:57:28 +00003356** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003357** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003358** executing this instruction.
3359*/
drh27a348c2015-04-13 19:14:06 +00003360case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003361 int iMeta;
drh856c1032009-06-02 15:21:42 +00003362 int iDb;
3363 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003364
drh1713afb2013-06-28 01:24:57 +00003365 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003366 iDb = pOp->p1;
3367 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003368 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003369 assert( iDb>=0 && iDb<db->nDb );
3370 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003371 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003372
danielk1977602b4662009-07-02 07:47:33 +00003373 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003374 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003375 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003376 break;
3377}
3378
drh98757152008-01-09 23:04:12 +00003379/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003380**
drh1861afc2016-02-01 21:48:34 +00003381** Write the integer value P3 into cookie number P2 of database P1.
3382** P2==1 is the schema version. P2==2 is the database format.
3383** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003384** size, and so forth. P1==0 is the main database file and P1==1 is the
3385** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003386**
3387** A transaction must be started before executing this opcode.
3388*/
drh1861afc2016-02-01 21:48:34 +00003389case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003390 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003391
3392 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003393 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003394 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003395 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003396 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003397 pDb = &db->aDb[pOp->p1];
3398 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003399 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003400 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003401 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003402 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003403 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003404 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003405 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003406 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003407 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003408 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003409 }
drhfd426c62006-01-30 15:34:22 +00003410 if( pOp->p1==1 ){
3411 /* Invalidate all prepared statements whenever the TEMP database
3412 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003413 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003414 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003415 }
drh9467abf2016-02-17 18:44:11 +00003416 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003417 break;
3418}
3419
drh98757152008-01-09 23:04:12 +00003420/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003421** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003422**
drhecdc7532001-09-23 02:35:53 +00003423** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003424** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003425** P3==0 means the main database, P3==1 means the database used for
3426** temporary tables, and P3>1 means used the corresponding attached
3427** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003428** values need not be contiguous but all P1 values should be small integers.
3429** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003430**
drh8e9deb62018-06-05 13:43:02 +00003431** Allowed P5 bits:
3432** <ul>
3433** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3434** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3435** of OP_SeekLE/OP_IdxGT)
3436** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003437**
danielk1977d336e222009-02-20 10:58:41 +00003438** The P4 value may be either an integer (P4_INT32) or a pointer to
3439** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003440** object, then table being opened must be an [index b-tree] where the
3441** KeyInfo object defines the content and collating
3442** sequence of that index b-tree. Otherwise, if P4 is an integer
3443** value, then the table being opened must be a [table b-tree] with a
3444** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003445**
drh35263192014-07-22 20:02:19 +00003446** See also: OpenWrite, ReopenIdx
3447*/
3448/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3449** Synopsis: root=P2 iDb=P3
3450**
drh8e9deb62018-06-05 13:43:02 +00003451** The ReopenIdx opcode works like OP_OpenRead except that it first
3452** checks to see if the cursor on P1 is already open on the same
3453** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003454** if the cursor is already open, do not reopen it.
3455**
drh8e9deb62018-06-05 13:43:02 +00003456** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3457** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3458** be the same as every other ReopenIdx or OpenRead for the same cursor
3459** number.
drh35263192014-07-22 20:02:19 +00003460**
drh8e9deb62018-06-05 13:43:02 +00003461** Allowed P5 bits:
3462** <ul>
3463** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3464** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3465** of OP_SeekLE/OP_IdxGT)
3466** </ul>
3467**
3468** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003469*/
drh98757152008-01-09 23:04:12 +00003470/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003471** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003472**
3473** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003474** page is P2 (or whose root page is held in register P2 if the
3475** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003476**
danielk1977d336e222009-02-20 10:58:41 +00003477** The P4 value may be either an integer (P4_INT32) or a pointer to
3478** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003479** object, then table being opened must be an [index b-tree] where the
3480** KeyInfo object defines the content and collating
3481** sequence of that index b-tree. Otherwise, if P4 is an integer
3482** value, then the table being opened must be a [table b-tree] with a
3483** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003484**
drh8e9deb62018-06-05 13:43:02 +00003485** Allowed P5 bits:
3486** <ul>
3487** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3488** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3489** of OP_SeekLE/OP_IdxGT)
3490** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3491** and subsequently delete entries in an index btree. This is a
3492** hint to the storage engine that the storage engine is allowed to
3493** ignore. The hint is not used by the official SQLite b*tree storage
3494** engine, but is used by COMDB2.
3495** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3496** as the root page, not the value of P2 itself.
3497** </ul>
drhf57b3392001-10-08 13:22:32 +00003498**
drh8e9deb62018-06-05 13:43:02 +00003499** This instruction works like OpenRead except that it opens the cursor
3500** in read/write mode.
3501**
3502** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003503*/
drh35263192014-07-22 20:02:19 +00003504case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003505 int nField;
3506 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003507 int p2;
3508 int iDb;
drhf57b3392001-10-08 13:22:32 +00003509 int wrFlag;
3510 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003511 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003512 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003513
drhe0997b32015-03-20 14:57:50 +00003514 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003515 assert( pOp->p4type==P4_KEYINFO );
3516 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003517 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003518 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003519 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003520 }
3521 /* If the cursor is not currently open or is open on a different
3522 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003523case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003524case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003525
drhe0997b32015-03-20 14:57:50 +00003526 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003527 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003528 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3529 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003530
drhba968db2018-07-24 22:02:12 +00003531 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003532 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003533 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003534 }
3535
drh856c1032009-06-02 15:21:42 +00003536 nField = 0;
3537 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003538 p2 = pOp->p2;
3539 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003540 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003541 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003542 pDb = &db->aDb[iDb];
3543 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003544 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003545 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003546 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3547 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003548 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003549 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3550 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003551 }
3552 }else{
3553 wrFlag = 0;
3554 }
dan428c2182012-08-06 18:50:11 +00003555 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003556 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003557 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003558 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003559 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003560 assert( memIsValid(pIn2) );
3561 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003562 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003563 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003564 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003565 ** that opcode will always set the p2 value to 2 or more or else fail.
3566 ** If there were a failure, the prepared statement would have halted
3567 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003568 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003569 }
danielk1977d336e222009-02-20 10:58:41 +00003570 if( pOp->p4type==P4_KEYINFO ){
3571 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003572 assert( pKeyInfo->enc==ENC(db) );
3573 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003574 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003575 }else if( pOp->p4type==P4_INT32 ){
3576 nField = pOp->p4.i;
3577 }
drh653b82a2009-06-22 11:10:47 +00003578 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003579 assert( nField>=0 );
3580 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003581 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003582 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003583 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003584 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003585 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003586#ifdef SQLITE_DEBUG
3587 pCur->wrFlag = wrFlag;
3588#endif
drhc960dcb2015-11-20 19:22:01 +00003589 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003590 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003591 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003592 ** SQLite used to check if the root-page flags were sane at this point
3593 ** and report database corruption if they were not, but this check has
3594 ** since moved into the btree layer. */
3595 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003596
3597open_cursor_set_hints:
3598 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3599 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003600 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003601#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003602 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3603#endif
drhc960dcb2015-11-20 19:22:01 +00003604 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003605 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003606 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003607 break;
3608}
3609
drhe08e8d62017-05-01 15:15:41 +00003610/* Opcode: OpenDup P1 P2 * * *
3611**
3612** Open a new cursor P1 that points to the same ephemeral table as
3613** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3614** opcode. Only ephemeral cursors may be duplicated.
3615**
3616** Duplicate ephemeral cursors are used for self-joins of materialized views.
3617*/
3618case OP_OpenDup: {
3619 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3620 VdbeCursor *pCx; /* The new cursor */
3621
3622 pOrig = p->apCsr[pOp->p2];
3623 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3624
3625 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3626 if( pCx==0 ) goto no_mem;
3627 pCx->nullRow = 1;
3628 pCx->isEphemeral = 1;
3629 pCx->pKeyInfo = pOrig->pKeyInfo;
3630 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00003631 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00003632 pCx->isOrdered = pOrig->isOrdered;
drh2c041312018-12-24 02:34:49 +00003633 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00003634 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003635 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3636 ** opened for a database. Since there is already an open cursor when this
3637 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3638 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003639 break;
3640}
3641
3642
drh2a5d9902011-08-26 00:34:45 +00003643/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003644** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003645**
drhb9bb7c12006-06-11 23:41:55 +00003646** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003647** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003648** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003649** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003650**
drhdfe3b582019-01-04 12:35:50 +00003651** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00003652** is cleared (all content is erased).
3653**
drh25d3adb2010-04-05 15:11:08 +00003654** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003655** The cursor points to a BTree table if P4==0 and to a BTree index
3656** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003657** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003658**
drh2a5d9902011-08-26 00:34:45 +00003659** The P5 parameter can be a mask of the BTREE_* flags defined
3660** in btree.h. These flags control aspects of the operation of
3661** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3662** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003663*/
drha21a64d2010-04-06 22:33:55 +00003664/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003665** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003666**
3667** This opcode works the same as OP_OpenEphemeral. It has a
3668** different name to distinguish its use. Tables created using
3669** by this opcode will be used for automatically created transient
3670** indices in joins.
3671*/
3672case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003673case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003674 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003675 KeyInfo *pKeyInfo;
3676
drhd4187c72010-08-30 22:15:45 +00003677 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003678 SQLITE_OPEN_READWRITE |
3679 SQLITE_OPEN_CREATE |
3680 SQLITE_OPEN_EXCLUSIVE |
3681 SQLITE_OPEN_DELETEONCLOSE |
3682 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003683 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003684 assert( pOp->p2>=0 );
drh4afdfa12018-12-31 16:36:42 +00003685 pCx = p->apCsr[pOp->p1];
3686 if( pCx ){
3687 /* If the ephermeral table is already open, erase all existing content
3688 ** so that the table is empty again, rather than creating a new table. */
3689 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00003690 }else{
3691 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3692 if( pCx==0 ) goto no_mem;
3693 pCx->nullRow = 1;
3694 pCx->isEphemeral = 1;
3695 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3696 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
3697 vfsFlags);
3698 if( rc==SQLITE_OK ){
3699 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drhc6b52df2002-01-04 03:09:29 +00003700 }
drhd0fb7962018-12-31 17:58:05 +00003701 if( rc==SQLITE_OK ){
3702 /* If a transient index is required, create it by calling
3703 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3704 ** opening it. If a transient table is required, just use the
3705 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3706 */
3707 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3708 assert( pOp->p4type==P4_KEYINFO );
3709 rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
3710 BTREE_BLOBKEY | pOp->p5);
3711 if( rc==SQLITE_OK ){
3712 assert( pCx->pgnoRoot==MASTER_ROOT+1 );
3713 assert( pKeyInfo->db==db );
3714 assert( pKeyInfo->enc==ENC(db) );
3715 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
3716 pKeyInfo, pCx->uc.pCursor);
3717 }
3718 pCx->isTable = 0;
3719 }else{
3720 pCx->pgnoRoot = MASTER_ROOT;
3721 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
3722 0, pCx->uc.pCursor);
3723 pCx->isTable = 1;
3724 }
3725 }
3726 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drh5e00f6c2001-09-13 13:46:56 +00003727 }
drh9467abf2016-02-17 18:44:11 +00003728 if( rc ) goto abort_due_to_error;
dan5134d132011-09-02 10:31:11 +00003729 break;
3730}
3731
danfad9f9a2014-04-01 18:41:51 +00003732/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003733**
3734** This opcode works like OP_OpenEphemeral except that it opens
3735** a transient index that is specifically designed to sort large
3736** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003737**
3738** If argument P3 is non-zero, then it indicates that the sorter may
3739** assume that a stable sort considering the first P3 fields of each
3740** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003741*/
drhca892a72011-09-03 00:17:51 +00003742case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003743 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003744
drh399af1d2013-11-20 17:25:55 +00003745 assert( pOp->p1>=0 );
3746 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003747 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003748 if( pCx==0 ) goto no_mem;
3749 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003750 assert( pCx->pKeyInfo->db==db );
3751 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003752 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003753 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003754 break;
3755}
3756
dan78d58432014-03-25 15:04:07 +00003757/* Opcode: SequenceTest P1 P2 * * *
3758** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3759**
3760** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3761** to P2. Regardless of whether or not the jump is taken, increment the
3762** the sequence value.
3763*/
3764case OP_SequenceTest: {
3765 VdbeCursor *pC;
3766 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3767 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003768 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003769 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003770 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003771 }
drh5e00f6c2001-09-13 13:46:56 +00003772 break;
3773}
3774
drh5f612292014-02-08 23:20:32 +00003775/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003776** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003777**
3778** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003779** row of data. The content of that one row is the content of memory
3780** register P2. In other words, cursor P1 becomes an alias for the
3781** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003782**
drh2d8d7ce2010-02-15 15:17:05 +00003783** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003784** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003785** individual columns using the OP_Column opcode. The OP_Column opcode
3786** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003787**
3788** P3 is the number of fields in the records that will be stored by
3789** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003790*/
drh9cbf3422008-01-17 16:22:13 +00003791case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003792 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003793
drh653b82a2009-06-22 11:10:47 +00003794 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003795 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003796 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003797 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003798 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003799 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003800 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003801 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3802 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3803 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3804 ** which is a performance optimization */
3805 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003806 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003807 break;
3808}
3809
drh98757152008-01-09 23:04:12 +00003810/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003811**
3812** Close a cursor previously opened as P1. If P1 is not
3813** currently open, this instruction is a no-op.
3814*/
drh9cbf3422008-01-17 16:22:13 +00003815case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003816 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3817 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3818 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003819 break;
3820}
3821
drh97bae792015-06-05 15:59:57 +00003822#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3823/* Opcode: ColumnsUsed P1 * * P4 *
3824**
3825** This opcode (which only exists if SQLite was compiled with
3826** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3827** table or index for cursor P1 are used. P4 is a 64-bit integer
3828** (P4_INT64) in which the first 63 bits are one for each of the
3829** first 63 columns of the table or index that are actually used
3830** by the cursor. The high-order bit is set if any column after
3831** the 64th is used.
3832*/
3833case OP_ColumnsUsed: {
3834 VdbeCursor *pC;
3835 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003836 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003837 pC->maskUsed = *(u64*)pOp->p4.pI64;
3838 break;
3839}
3840#endif
3841
drh8af3f772014-07-25 18:01:06 +00003842/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003843** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003844**
danielk1977b790c6c2008-04-18 10:25:24 +00003845** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003846** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003847** to an SQL index, then P3 is the first in an array of P4 registers
3848** that are used as an unpacked index key.
3849**
3850** Reposition cursor P1 so that it points to the smallest entry that
3851** is greater than or equal to the key value. If there are no records
3852** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003853**
drhb1d607d2015-11-05 22:30:54 +00003854** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3855** opcode will always land on a record that equally equals the key, or
3856** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3857** opcode must be followed by an IdxLE opcode with the same arguments.
3858** The IdxLE opcode will be skipped if this opcode succeeds, but the
3859** IdxLE opcode will be used on subsequent loop iterations.
3860**
drh8af3f772014-07-25 18:01:06 +00003861** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003862** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003863** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003864**
drh935850e2014-05-24 17:15:15 +00003865** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003866*/
drh8af3f772014-07-25 18:01:06 +00003867/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003868** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003869**
danielk1977b790c6c2008-04-18 10:25:24 +00003870** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003871** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003872** to an SQL index, then P3 is the first in an array of P4 registers
3873** that are used as an unpacked index key.
3874**
3875** Reposition cursor P1 so that it points to the smallest entry that
3876** is greater than the key value. If there are no records greater than
3877** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003878**
drh8af3f772014-07-25 18:01:06 +00003879** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003880** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003881** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003882**
drh935850e2014-05-24 17:15:15 +00003883** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003884*/
drh8af3f772014-07-25 18:01:06 +00003885/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003886** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003887**
danielk1977b790c6c2008-04-18 10:25:24 +00003888** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003889** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003890** to an SQL index, then P3 is the first in an array of P4 registers
3891** that are used as an unpacked index key.
3892**
3893** Reposition cursor P1 so that it points to the largest entry that
3894** is less than the key value. If there are no records less than
3895** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003896**
drh8af3f772014-07-25 18:01:06 +00003897** This opcode leaves the cursor configured to move in reverse order,
3898** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003899** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003900**
drh935850e2014-05-24 17:15:15 +00003901** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003902*/
drh8af3f772014-07-25 18:01:06 +00003903/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003904** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003905**
danielk1977b790c6c2008-04-18 10:25:24 +00003906** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003907** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003908** to an SQL index, then P3 is the first in an array of P4 registers
3909** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003910**
danielk1977b790c6c2008-04-18 10:25:24 +00003911** Reposition cursor P1 so that it points to the largest entry that
3912** is less than or equal to the key value. If there are no records
3913** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003914**
drh8af3f772014-07-25 18:01:06 +00003915** This opcode leaves the cursor configured to move in reverse order,
3916** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003917** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003918**
drhb1d607d2015-11-05 22:30:54 +00003919** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3920** opcode will always land on a record that equally equals the key, or
3921** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3922** opcode must be followed by an IdxGE opcode with the same arguments.
3923** The IdxGE opcode will be skipped if this opcode succeeds, but the
3924** IdxGE opcode will be used on subsequent loop iterations.
3925**
drh935850e2014-05-24 17:15:15 +00003926** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003927*/
mistachkin758784d2018-07-25 15:12:29 +00003928case OP_SeekLT: /* jump, in3, group */
3929case OP_SeekLE: /* jump, in3, group */
3930case OP_SeekGE: /* jump, in3, group */
3931case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00003932 int res; /* Comparison result */
3933 int oc; /* Opcode */
3934 VdbeCursor *pC; /* The cursor to seek */
3935 UnpackedRecord r; /* The key to seek for */
3936 int nField; /* Number of columns or fields in the key */
3937 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003938 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003939
drh653b82a2009-06-22 11:10:47 +00003940 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003941 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003942 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003943 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003944 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003945 assert( OP_SeekLE == OP_SeekLT+1 );
3946 assert( OP_SeekGE == OP_SeekLT+2 );
3947 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003948 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003949 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003950 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003951 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003952 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003953#ifdef SQLITE_DEBUG
3954 pC->seekOp = pOp->opcode;
3955#endif
drhe0997b32015-03-20 14:57:50 +00003956
drh3da046d2013-11-11 03:24:11 +00003957 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003958 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003959 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3960 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003961
drh3da046d2013-11-11 03:24:11 +00003962 /* The input value in P3 might be of any type: integer, real, string,
3963 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003964 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003965 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003966 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003967 applyNumericAffinity(pIn3, 0);
3968 }
drh3da046d2013-11-11 03:24:11 +00003969 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003970
drh3da046d2013-11-11 03:24:11 +00003971 /* If the P3 value could not be converted into an integer without
3972 ** loss of information, then special processing is required... */
3973 if( (pIn3->flags & MEM_Int)==0 ){
3974 if( (pIn3->flags & MEM_Real)==0 ){
3975 /* If the P3 value cannot be converted into any kind of a number,
3976 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003977 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003978 break;
3979 }
drh959403f2008-12-12 17:56:16 +00003980
danaa1776f2013-11-26 18:22:59 +00003981 /* If the approximation iKey is larger than the actual real search
3982 ** term, substitute >= for > and < for <=. e.g. if the search term
3983 ** is 4.9 and the integer approximation 5:
3984 **
3985 ** (x > 4.9) -> (x >= 5)
3986 ** (x <= 4.9) -> (x < 5)
3987 */
drh74eaba42014-09-18 17:52:15 +00003988 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003989 assert( OP_SeekGE==(OP_SeekGT-1) );
3990 assert( OP_SeekLT==(OP_SeekLE-1) );
3991 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3992 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003993 }
3994
3995 /* If the approximation iKey is smaller than the actual real search
3996 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003997 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003998 assert( OP_SeekLE==(OP_SeekLT+1) );
3999 assert( OP_SeekGT==(OP_SeekGE+1) );
4000 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4001 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004002 }
drh3da046d2013-11-11 03:24:11 +00004003 }
drhc960dcb2015-11-20 19:22:01 +00004004 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004005 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004006 if( rc!=SQLITE_OK ){
4007 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004008 }
drhaa736092009-06-22 00:55:30 +00004009 }else{
drhd6b79462015-11-07 01:19:00 +00004010 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
4011 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
4012 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
4013 */
drhc960dcb2015-11-20 19:22:01 +00004014 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004015 eqOnly = 1;
4016 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4017 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4018 assert( pOp[1].p1==pOp[0].p1 );
4019 assert( pOp[1].p2==pOp[0].p2 );
4020 assert( pOp[1].p3==pOp[0].p3 );
4021 assert( pOp[1].p4.i==pOp[0].p4.i );
4022 }
4023
drh3da046d2013-11-11 03:24:11 +00004024 nField = pOp->p4.i;
4025 assert( pOp->p4type==P4_INT32 );
4026 assert( nField>0 );
4027 r.pKeyInfo = pC->pKeyInfo;
4028 r.nField = (u16)nField;
4029
4030 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004031 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004032 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004033 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004034 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004035 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004036 */
dan1fed5da2014-02-25 21:01:25 +00004037 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4038 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4039 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4040 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4041 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004042
4043 r.aMem = &aMem[pOp->p3];
4044#ifdef SQLITE_DEBUG
4045 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4046#endif
drh70528d72015-11-05 20:25:09 +00004047 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004048 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004049 if( rc!=SQLITE_OK ){
4050 goto abort_due_to_error;
4051 }
drhb1d607d2015-11-05 22:30:54 +00004052 if( eqOnly && r.eqSeen==0 ){
4053 assert( res!=0 );
4054 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004055 }
drh3da046d2013-11-11 03:24:11 +00004056 }
4057 pC->deferredMoveto = 0;
4058 pC->cacheStatus = CACHE_STALE;
4059#ifdef SQLITE_TEST
4060 sqlite3_search_count++;
4061#endif
drh4a1d3652014-02-14 15:13:36 +00004062 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4063 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004064 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004065 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4066 if( rc!=SQLITE_OK ){
4067 if( rc==SQLITE_DONE ){
4068 rc = SQLITE_OK;
4069 res = 1;
4070 }else{
4071 goto abort_due_to_error;
4072 }
4073 }
drh3da046d2013-11-11 03:24:11 +00004074 }else{
4075 res = 0;
4076 }
4077 }else{
drh4a1d3652014-02-14 15:13:36 +00004078 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4079 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004080 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004081 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4082 if( rc!=SQLITE_OK ){
4083 if( rc==SQLITE_DONE ){
4084 rc = SQLITE_OK;
4085 res = 1;
4086 }else{
4087 goto abort_due_to_error;
4088 }
4089 }
drh3da046d2013-11-11 03:24:11 +00004090 }else{
4091 /* res might be negative because the table is empty. Check to
4092 ** see if this is the case.
4093 */
drhc960dcb2015-11-20 19:22:01 +00004094 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004095 }
4096 }
drhb1d607d2015-11-05 22:30:54 +00004097seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004098 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004099 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004100 if( res ){
drhf56fa462015-04-13 21:39:54 +00004101 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004102 }else if( eqOnly ){
4103 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4104 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004105 }
drh5e00f6c2001-09-13 13:46:56 +00004106 break;
4107}
dan71c57db2016-07-09 20:23:55 +00004108
drh8c2b6d72018-06-05 20:45:20 +00004109/* Opcode: SeekHit P1 P2 * * *
4110** Synopsis: seekHit=P2
4111**
4112** Set the seekHit flag on cursor P1 to the value in P2.
4113** The seekHit flag is used by the IfNoHope opcode.
4114**
4115** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4116** either 0 or 1.
4117*/
4118case OP_SeekHit: {
4119 VdbeCursor *pC;
4120 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4121 pC = p->apCsr[pOp->p1];
4122 assert( pC!=0 );
4123 assert( pOp->p2==0 || pOp->p2==1 );
4124 pC->seekHit = pOp->p2 & 1;
4125 break;
4126}
4127
drh8cff69d2009-11-12 19:59:44 +00004128/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004129** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004130**
drh8cff69d2009-11-12 19:59:44 +00004131** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4132** P4>0 then register P3 is the first of P4 registers that form an unpacked
4133** record.
4134**
4135** Cursor P1 is on an index btree. If the record identified by P3 and P4
4136** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004137** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004138**
drhcefc87f2014-08-01 01:40:33 +00004139** This operation leaves the cursor in a state where it can be
4140** advanced in the forward direction. The Next instruction will work,
4141** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004142**
drh6f225d02013-10-26 13:36:51 +00004143** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004144*/
drh8cff69d2009-11-12 19:59:44 +00004145/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004146** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004147**
drh8cff69d2009-11-12 19:59:44 +00004148** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4149** P4>0 then register P3 is the first of P4 registers that form an unpacked
4150** record.
4151**
4152** Cursor P1 is on an index btree. If the record identified by P3 and P4
4153** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4154** does contain an entry whose prefix matches the P3/P4 record then control
4155** falls through to the next instruction and P1 is left pointing at the
4156** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004157**
drh8af3f772014-07-25 18:01:06 +00004158** This operation leaves the cursor in a state where it cannot be
4159** advanced in either direction. In other words, the Next and Prev
4160** opcodes do not work after this operation.
4161**
drh8c2b6d72018-06-05 20:45:20 +00004162** See also: Found, NotExists, NoConflict, IfNoHope
4163*/
4164/* Opcode: IfNoHope P1 P2 P3 P4 *
4165** Synopsis: key=r[P3@P4]
4166**
4167** Register P3 is the first of P4 registers that form an unpacked
4168** record.
4169**
4170** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4171** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4172** check to see if there is any entry in P1 that matches the
4173** prefix identified by P3 and P4. If no entry matches the prefix,
4174** jump to P2. Otherwise fall through.
4175**
4176** This opcode behaves like OP_NotFound if the seekHit
4177** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4178**
4179** This opcode is used in IN clause processing for a multi-column key.
4180** If an IN clause is attached to an element of the key other than the
4181** left-most element, and if there are no matches on the most recent
4182** seek over the whole key, then it might be that one of the key element
4183** to the left is prohibiting a match, and hence there is "no hope" of
4184** any match regardless of how many IN clause elements are checked.
4185** In such a case, we abandon the IN clause search early, using this
4186** opcode. The opcode name comes from the fact that the
4187** jump is taken if there is "no hope" of achieving a match.
4188**
4189** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004190*/
drh6f225d02013-10-26 13:36:51 +00004191/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004192** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004193**
4194** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4195** P4>0 then register P3 is the first of P4 registers that form an unpacked
4196** record.
4197**
4198** Cursor P1 is on an index btree. If the record identified by P3 and P4
4199** contains any NULL value, jump immediately to P2. If all terms of the
4200** record are not-NULL then a check is done to determine if any row in the
4201** P1 index btree has a matching key prefix. If there are no matches, jump
4202** immediately to P2. If there is a match, fall through and leave the P1
4203** cursor pointing to the matching row.
4204**
4205** This opcode is similar to OP_NotFound with the exceptions that the
4206** branch is always taken if any part of the search key input is NULL.
4207**
drh8af3f772014-07-25 18:01:06 +00004208** This operation leaves the cursor in a state where it cannot be
4209** advanced in either direction. In other words, the Next and Prev
4210** opcodes do not work after this operation.
4211**
drh6f225d02013-10-26 13:36:51 +00004212** See also: NotFound, Found, NotExists
4213*/
drh8c2b6d72018-06-05 20:45:20 +00004214case OP_IfNoHope: { /* jump, in3 */
4215 VdbeCursor *pC;
4216 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4217 pC = p->apCsr[pOp->p1];
4218 assert( pC!=0 );
4219 if( pC->seekHit ) break;
4220 /* Fall through into OP_NotFound */
4221}
drh6f225d02013-10-26 13:36:51 +00004222case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004223case OP_NotFound: /* jump, in3 */
4224case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004225 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004226 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004227 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004228 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004229 int res;
drha582b012016-12-21 19:45:54 +00004230 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004231 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004232 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004233
dan0ff297e2009-09-25 17:03:14 +00004234#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004235 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004236#endif
4237
drhaa736092009-06-22 00:55:30 +00004238 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004239 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004240 pC = p->apCsr[pOp->p1];
4241 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004242#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004243 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004244#endif
drh3c657212009-11-17 23:59:58 +00004245 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004246 assert( pC->eCurType==CURTYPE_BTREE );
4247 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004248 assert( pC->isTable==0 );
4249 if( pOp->p4.i>0 ){
4250 r.pKeyInfo = pC->pKeyInfo;
4251 r.nField = (u16)pOp->p4.i;
4252 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004253#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004254 for(ii=0; ii<r.nField; ii++){
4255 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004256 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004257 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004258 }
drh8aaf7bc2016-09-20 01:19:18 +00004259#endif
drh3da046d2013-11-11 03:24:11 +00004260 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004261 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004262 }else{
drhe46515b2017-05-19 22:51:00 +00004263 assert( pIn3->flags & MEM_Blob );
4264 rc = ExpandBlob(pIn3);
4265 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4266 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004267 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004268 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004269 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004270 }
dan1fed5da2014-02-25 21:01:25 +00004271 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004272 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004273 if( pOp->opcode==OP_NoConflict ){
4274 /* For the OP_NoConflict opcode, take the jump if any of the
4275 ** input fields are NULL, since any key with a NULL will not
4276 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004277 for(ii=0; ii<pIdxKey->nField; ii++){
4278 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004279 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004280 break;
drh6f225d02013-10-26 13:36:51 +00004281 }
4282 }
drh5e00f6c2001-09-13 13:46:56 +00004283 }
drhc960dcb2015-11-20 19:22:01 +00004284 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004285 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004286 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004287 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004288 }
4289 pC->seekResult = res;
4290 alreadyExists = (res==0);
4291 pC->nullRow = 1-alreadyExists;
4292 pC->deferredMoveto = 0;
4293 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004294 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004295 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004296 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004297 }else{
drhf56fa462015-04-13 21:39:54 +00004298 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4299 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004300 }
drh5e00f6c2001-09-13 13:46:56 +00004301 break;
4302}
4303
drheeb95652016-05-26 20:56:38 +00004304/* Opcode: SeekRowid P1 P2 P3 * *
4305** Synopsis: intkey=r[P3]
4306**
4307** P1 is the index of a cursor open on an SQL table btree (with integer
4308** keys). If register P3 does not contain an integer or if P1 does not
4309** contain a record with rowid P3 then jump immediately to P2.
4310** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4311** a record with rowid P3 then
4312** leave the cursor pointing at that record and fall through to the next
4313** instruction.
4314**
4315** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4316** the P3 register must be guaranteed to contain an integer value. With this
4317** opcode, register P3 might not contain an integer.
4318**
4319** The OP_NotFound opcode performs the same operation on index btrees
4320** (with arbitrary multi-value keys).
4321**
4322** This opcode leaves the cursor in a state where it cannot be advanced
4323** in either direction. In other words, the Next and Prev opcodes will
4324** not work following this opcode.
4325**
4326** See also: Found, NotFound, NoConflict, SeekRowid
4327*/
drh9cbf3422008-01-17 16:22:13 +00004328/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004329** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004330**
drh261c02d2013-10-25 14:46:15 +00004331** P1 is the index of a cursor open on an SQL table btree (with integer
4332** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004333** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4334** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4335** leave the cursor pointing at that record and fall through to the next
4336** instruction.
drh6b125452002-01-28 15:53:03 +00004337**
drheeb95652016-05-26 20:56:38 +00004338** The OP_SeekRowid opcode performs the same operation but also allows the
4339** P3 register to contain a non-integer value, in which case the jump is
4340** always taken. This opcode requires that P3 always contain an integer.
4341**
drh261c02d2013-10-25 14:46:15 +00004342** The OP_NotFound opcode performs the same operation on index btrees
4343** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004344**
drh8af3f772014-07-25 18:01:06 +00004345** This opcode leaves the cursor in a state where it cannot be advanced
4346** in either direction. In other words, the Next and Prev opcodes will
4347** not work following this opcode.
4348**
drheeb95652016-05-26 20:56:38 +00004349** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004350*/
drheeb95652016-05-26 20:56:38 +00004351case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004352 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004353 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004354 int res;
4355 u64 iKey;
4356
drh3c657212009-11-17 23:59:58 +00004357 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004358 if( (pIn3->flags & MEM_Int)==0 ){
drhe4fe6d42018-08-03 15:58:07 +00004359 /* Make sure pIn3->u.i contains a valid integer representation of
4360 ** the key value, but do not change the datatype of the register, as
4361 ** other parts of the perpared statement might be depending on the
4362 ** current datatype. */
4363 u16 origFlags = pIn3->flags;
4364 int isNotInt;
drheeb95652016-05-26 20:56:38 +00004365 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
drhe4fe6d42018-08-03 15:58:07 +00004366 isNotInt = (pIn3->flags & MEM_Int)==0;
4367 pIn3->flags = origFlags;
4368 if( isNotInt ) goto jump_to_p2;
drheeb95652016-05-26 20:56:38 +00004369 }
4370 /* Fall through into OP_NotExists */
4371case OP_NotExists: /* jump, in3 */
4372 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004373 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004374 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4375 pC = p->apCsr[pOp->p1];
4376 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004377#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00004378 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004379#endif
drhaa736092009-06-22 00:55:30 +00004380 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004381 assert( pC->eCurType==CURTYPE_BTREE );
4382 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004383 assert( pCrsr!=0 );
4384 res = 0;
4385 iKey = pIn3->u.i;
4386 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004387 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004388 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004389 pC->nullRow = 0;
4390 pC->cacheStatus = CACHE_STALE;
4391 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004392 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004393 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004394 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004395 assert( rc==SQLITE_OK );
4396 if( pOp->p2==0 ){
4397 rc = SQLITE_CORRUPT_BKPT;
4398 }else{
4399 goto jump_to_p2;
4400 }
danc6157e12015-09-14 09:23:47 +00004401 }
drh9467abf2016-02-17 18:44:11 +00004402 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004403 break;
4404}
4405
drh4c583122008-01-04 22:01:03 +00004406/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004407** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004408**
drh4c583122008-01-04 22:01:03 +00004409** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004410** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004411** The sequence number on the cursor is incremented after this
4412** instruction.
drh4db38a72005-09-01 12:16:28 +00004413*/
drh27a348c2015-04-13 19:14:06 +00004414case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004415 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4416 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004417 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004418 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004419 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004420 break;
4421}
4422
4423
drh98757152008-01-09 23:04:12 +00004424/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004425** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004426**
drhf0863fe2005-06-12 21:35:51 +00004427** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004428** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004429** table that cursor P1 points to. The new record number is written
4430** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004431**
dan76d462e2009-08-30 11:42:51 +00004432** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4433** the largest previously generated record number. No new record numbers are
4434** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004435** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004436** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004437** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004438*/
drh27a348c2015-04-13 19:14:06 +00004439case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004440 i64 v; /* The new rowid */
4441 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4442 int res; /* Result of an sqlite3BtreeLast() */
4443 int cnt; /* Counter to limit the number of searches */
4444 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004445 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004446
drh856c1032009-06-02 15:21:42 +00004447 v = 0;
4448 res = 0;
drh27a348c2015-04-13 19:14:06 +00004449 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004450 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4451 pC = p->apCsr[pOp->p1];
4452 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004453 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004454 assert( pC->eCurType==CURTYPE_BTREE );
4455 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004456 {
drh5cf8e8c2002-02-19 22:42:05 +00004457 /* The next rowid or record number (different terms for the same
4458 ** thing) is obtained in a two-step algorithm.
4459 **
4460 ** First we attempt to find the largest existing rowid and add one
4461 ** to that. But if the largest existing rowid is already the maximum
4462 ** positive integer, we have to fall through to the second
4463 ** probabilistic algorithm
4464 **
4465 ** The second algorithm is to select a rowid at random and see if
4466 ** it already exists in the table. If it does not exist, we have
4467 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004468 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004469 */
drhaa736092009-06-22 00:55:30 +00004470 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004471
drh75f86a42005-02-17 00:03:06 +00004472#ifdef SQLITE_32BIT_ROWID
4473# define MAX_ROWID 0x7fffffff
4474#else
drhfe2093d2005-01-20 22:48:47 +00004475 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4476 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4477 ** to provide the constant while making all compilers happy.
4478 */
danielk197764202cf2008-11-17 15:31:47 +00004479# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004480#endif
drhfe2093d2005-01-20 22:48:47 +00004481
drh5cf8e8c2002-02-19 22:42:05 +00004482 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004483 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004484 if( rc!=SQLITE_OK ){
4485 goto abort_due_to_error;
4486 }
4487 if( res ){
4488 v = 1; /* IMP: R-61914-48074 */
4489 }else{
drhc960dcb2015-11-20 19:22:01 +00004490 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004491 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004492 if( v>=MAX_ROWID ){
4493 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004494 }else{
drhe0670b62014-02-12 21:31:12 +00004495 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004496 }
drh3fc190c2001-09-14 03:24:23 +00004497 }
drhe0670b62014-02-12 21:31:12 +00004498 }
drh205f48e2004-11-05 00:43:11 +00004499
4500#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004501 if( pOp->p3 ){
4502 /* Assert that P3 is a valid memory cell. */
4503 assert( pOp->p3>0 );
4504 if( p->pFrame ){
4505 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004506 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004507 assert( pOp->p3<=pFrame->nMem );
4508 pMem = &pFrame->aMem[pOp->p3];
4509 }else{
4510 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004511 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004512 pMem = &aMem[pOp->p3];
4513 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004514 }
drhe0670b62014-02-12 21:31:12 +00004515 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004516
drhe0670b62014-02-12 21:31:12 +00004517 REGISTER_TRACE(pOp->p3, pMem);
4518 sqlite3VdbeMemIntegerify(pMem);
4519 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4520 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004521 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004522 goto abort_due_to_error;
4523 }
4524 if( v<pMem->u.i+1 ){
4525 v = pMem->u.i + 1;
4526 }
4527 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004528 }
drhe0670b62014-02-12 21:31:12 +00004529#endif
drh5cf8e8c2002-02-19 22:42:05 +00004530 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004531 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004532 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004533 ** engine starts picking positive candidate ROWIDs at random until
4534 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004535 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4536 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004537 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004538 do{
4539 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004540 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004541 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004542 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004543 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004544 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004545 if( rc ) goto abort_due_to_error;
4546 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004547 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004548 goto abort_due_to_error;
4549 }
drh748a52c2010-09-01 11:50:08 +00004550 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004551 }
drha11846b2004-01-07 18:52:56 +00004552 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004553 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004554 }
drh4c583122008-01-04 22:01:03 +00004555 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004556 break;
4557}
4558
danielk19771f4aa332008-01-03 09:51:55 +00004559/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004560** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004561**
jplyon5a564222003-06-02 06:15:58 +00004562** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004563** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004564** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004565** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004566** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004567**
danielk19771f4aa332008-01-03 09:51:55 +00004568** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4569** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004570** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004571** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004572**
drheaf6ae22016-11-09 20:14:34 +00004573** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4574** run faster by avoiding an unnecessary seek on cursor P1. However,
4575** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4576** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004577**
4578** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4579** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4580** is part of an INSERT operation. The difference is only important to
4581** the update hook.
4582**
dan319eeb72011-03-19 08:38:50 +00004583** Parameter P4 may point to a Table structure, or may be NULL. If it is
4584** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4585** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004586**
drh93aed5a2008-01-16 17:46:38 +00004587** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4588** allocated, then ownership of P2 is transferred to the pseudo-cursor
4589** and register P2 becomes ephemeral. If the cursor is changed, the
4590** value of register P2 will then change. Make sure this does not
4591** cause any problems.)
4592**
drhf0863fe2005-06-12 21:35:51 +00004593** This instruction only works on tables. The equivalent instruction
4594** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004595*/
drh50ef6712019-02-22 23:29:56 +00004596case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00004597 Mem *pData; /* MEM cell holding data for the record to be inserted */
4598 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004599 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004600 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4601 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004602 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00004603 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004604
drha6c2ed92009-11-14 23:22:23 +00004605 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004606 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004607 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004608 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004609 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004610 assert( pC->eCurType==CURTYPE_BTREE );
4611 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004612 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004613 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004614 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00004615 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00004616
drh50ef6712019-02-22 23:29:56 +00004617 pKey = &aMem[pOp->p3];
4618 assert( pKey->flags & MEM_Int );
4619 assert( memIsValid(pKey) );
4620 REGISTER_TRACE(pOp->p3, pKey);
4621 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004622
drh9b1c62d2011-03-30 21:04:43 +00004623 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004624 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004625 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004626 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004627 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00004628 }else{
drh4ec6f3a2018-01-12 19:33:18 +00004629 pTab = 0;
drh74c33022016-03-30 12:56:55 +00004630 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004631 }
4632
drh9b1c62d2011-03-30 21:04:43 +00004633#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004634 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00004635 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00004636 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
4637 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
4638 }
drh4ec6f3a2018-01-12 19:33:18 +00004639 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
4640 /* Prevent post-update hook from running in cases when it should not */
4641 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00004642 }
dan46c47d42011-03-01 18:42:07 +00004643 }
dancb9a3642017-01-30 19:44:53 +00004644 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004645#endif
dan46c47d42011-03-01 18:42:07 +00004646
drha05a7222008-01-19 03:35:58 +00004647 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004648 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004649 assert( pData->flags & (MEM_Blob|MEM_Str) );
4650 x.pData = pData->z;
4651 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004652 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4653 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004654 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004655 }else{
drh8eeb4462016-05-21 20:03:42 +00004656 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004657 }
drh8eeb4462016-05-21 20:03:42 +00004658 x.pKey = 0;
4659 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004660 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004661 );
drha05a7222008-01-19 03:35:58 +00004662 pC->deferredMoveto = 0;
4663 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004664
drha05a7222008-01-19 03:35:58 +00004665 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004666 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00004667 if( pTab ){
4668 assert( db->xUpdateCallback!=0 );
4669 assert( pTab->aCol!=0 );
4670 db->xUpdateCallback(db->pUpdateArg,
4671 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
4672 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004673 }
drh5e00f6c2001-09-13 13:46:56 +00004674 break;
4675}
4676
dan438b8812015-09-15 15:55:15 +00004677/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004678**
drh5edc3122001-09-13 21:53:09 +00004679** Delete the record at which the P1 cursor is currently pointing.
4680**
drhe807bdb2016-01-21 17:06:33 +00004681** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4682** the cursor will be left pointing at either the next or the previous
4683** record in the table. If it is left pointing at the next record, then
4684** the next Next instruction will be a no-op. As a result, in this case
4685** it is ok to delete a record from within a Next loop. If
4686** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4687** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004688**
drhdef19e32016-01-27 16:26:25 +00004689** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4690** delete one of several associated with deleting a table row and all its
4691** associated index entries. Exactly one of those deletes is the "primary"
4692** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4693** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004694**
4695** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4696** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004697**
drh91fd4d42008-01-19 20:11:25 +00004698** P1 must not be pseudo-table. It has to be a real table with
4699** multiple rows.
4700**
drh5e769a52016-09-28 16:05:53 +00004701** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004702** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4703** have been positioned using OP_NotFound prior to invoking this opcode in
4704** this case. Specifically, if one is configured, the pre-update hook is
4705** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4706** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004707**
4708** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4709** of the memory cell that contains the value that the rowid of the row will
4710** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004711*/
drh9cbf3422008-01-17 16:22:13 +00004712case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004713 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004714 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004715 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004716 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004717
dan46c47d42011-03-01 18:42:07 +00004718 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004719 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4720 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004721 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004722 assert( pC->eCurType==CURTYPE_BTREE );
4723 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004724 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00004725 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00004726
drhb53a5a92014-10-12 22:37:22 +00004727#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004728 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4729 /* If p5 is zero, the seek operation that positioned the cursor prior to
4730 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4731 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004732 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh92fe38e2014-10-14 13:41:32 +00004733 assert( pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004734 }
4735#endif
drh91fd4d42008-01-19 20:11:25 +00004736
dan438b8812015-09-15 15:55:15 +00004737 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4738 ** the name of the db to pass as to it. Also set local pTab to a copy
4739 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4740 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4741 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004742 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004743 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004744 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004745 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004746 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004747 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004748 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004749 }
drh74c33022016-03-30 12:56:55 +00004750 }else{
4751 zDb = 0; /* Not needed. Silence a compiler warning. */
4752 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004753 }
dan46c47d42011-03-01 18:42:07 +00004754
drh9b1c62d2011-03-30 21:04:43 +00004755#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004756 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004757 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4758 assert( !(opflags & OPFLAG_ISUPDATE)
4759 || HasRowid(pTab)==0
4760 || (aMem[pOp->p3].flags & MEM_Int)
4761 );
dan46c47d42011-03-01 18:42:07 +00004762 sqlite3VdbePreUpdateHook(p, pC,
4763 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004764 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004765 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004766 );
4767 }
dan46c47d42011-03-01 18:42:07 +00004768 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004769#endif
drhb53a5a92014-10-12 22:37:22 +00004770
drhdef19e32016-01-27 16:26:25 +00004771 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4772 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004773 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004774 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004775
4776#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004777 if( p->pFrame==0 ){
4778 if( pC->isEphemeral==0
4779 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4780 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4781 ){
4782 nExtraDelete++;
4783 }
4784 if( pOp->p2 & OPFLAG_NCHANGE ){
4785 nExtraDelete--;
4786 }
drhb89aeb62016-01-27 15:49:32 +00004787 }
4788#endif
4789
drhc960dcb2015-11-20 19:22:01 +00004790 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004791 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004792 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004793 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004794
drh91fd4d42008-01-19 20:11:25 +00004795 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004796 if( opflags & OPFLAG_NCHANGE ){
4797 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004798 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004799 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004800 pC->movetoTarget);
4801 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004802 }
drh5e00f6c2001-09-13 13:46:56 +00004803 }
dan438b8812015-09-15 15:55:15 +00004804
rdcb0c374f2004-02-20 22:53:38 +00004805 break;
4806}
drhb7f1d9a2009-09-08 02:27:58 +00004807/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004808**
drhb7f1d9a2009-09-08 02:27:58 +00004809** The value of the change counter is copied to the database handle
4810** change counter (returned by subsequent calls to sqlite3_changes()).
4811** Then the VMs internal change counter resets to 0.
4812** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004813*/
drh9cbf3422008-01-17 16:22:13 +00004814case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004815 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004816 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004817 break;
4818}
4819
drh1153c7b2013-11-01 22:02:56 +00004820/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00004821** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004822**
drh1153c7b2013-11-01 22:02:56 +00004823** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004824** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004825** the sorter cursor currently points to. Only the first P4 fields
4826** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004827**
4828** If either P3 or the sorter contains a NULL in one of their significant
4829** fields (not counting the P4 fields at the end which are ignored) then
4830** the comparison is assumed to be equal.
4831**
4832** Fall through to next instruction if the two records compare equal to
4833** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004834*/
4835case OP_SorterCompare: {
4836 VdbeCursor *pC;
4837 int res;
drhac502322014-07-30 13:56:48 +00004838 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004839
4840 pC = p->apCsr[pOp->p1];
4841 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004842 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004843 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004844 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004845 res = 0;
drhac502322014-07-30 13:56:48 +00004846 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004847 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004848 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004849 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004850 break;
4851};
4852
drh6cf4a7d2014-10-13 13:00:58 +00004853/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004854** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004855**
4856** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004857** Then clear the column header cache on cursor P3.
4858**
4859** This opcode is normally use to move a record out of the sorter and into
4860** a register that is the source for a pseudo-table cursor created using
4861** OpenPseudo. That pseudo-table cursor is the one that is identified by
4862** parameter P3. Clearing the P3 column cache as part of this opcode saves
4863** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004864*/
4865case OP_SorterData: {
4866 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004867
dan5134d132011-09-02 10:31:11 +00004868 pOut = &aMem[pOp->p2];
4869 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004870 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004871 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004872 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004873 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004874 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004875 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004876 break;
4877}
4878
drhe7b554d2017-01-09 15:44:25 +00004879/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004880** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004881**
drh9057fc72016-11-25 19:32:32 +00004882** Write into register P2 the complete row content for the row at
4883** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00004884** There is no interpretation of the data.
4885** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004886** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004887**
drh9057fc72016-11-25 19:32:32 +00004888** If cursor P1 is an index, then the content is the key of the row.
4889** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00004890**
drhde4fcfd2008-01-19 23:50:26 +00004891** If the P1 cursor must be pointing to a valid row (not a NULL row)
4892** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00004893**
drh8cdafc32018-05-31 19:00:20 +00004894** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00004895** into the database page. That means that the content of the output
4896** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00004897** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00004898** position in order that they can write to the same table. If P3==0
4899** then a copy of the data is made into memory. P3!=0 is faster, but
4900** P3==0 is safer.
4901**
4902** If P3!=0 then the content of the P2 register is unsuitable for use
4903** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00004904** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00004905** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00004906*/
danielk1977a7a8e142008-02-13 18:25:27 +00004907case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004908 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004909 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004910 u32 n;
drh70ce3f02003-04-15 19:22:22 +00004911
drhe7b554d2017-01-09 15:44:25 +00004912 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00004913
drh653b82a2009-06-22 11:10:47 +00004914 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4915 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004916 assert( pC!=0 );
4917 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004918 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00004919 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004920 assert( pC->uc.pCursor!=0 );
4921 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004922
drh9057fc72016-11-25 19:32:32 +00004923 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00004924 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4925 ** that might invalidate the cursor.
4926 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00004927 ** would fail. Should this ever change (because of changes in the code
4928 ** generator) then the fix would be to insert a call to
4929 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004930 */
4931 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004932 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4933#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004934 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004935 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4936#endif
drh9a65f2c2009-06-22 19:05:40 +00004937
drha7c90c42016-06-04 20:37:10 +00004938 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00004939 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00004940 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00004941 }
drh722246e2014-10-07 23:02:24 +00004942 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00004943 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00004944 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00004945 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00004946 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004947 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004948 break;
4949}
4950
drh2133d822008-01-03 18:44:59 +00004951/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004952** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004953**
drh2133d822008-01-03 18:44:59 +00004954** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004955** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004956**
4957** P1 can be either an ordinary table or a virtual table. There used to
4958** be a separate OP_VRowid opcode for use with virtual tables, but this
4959** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004960*/
drh27a348c2015-04-13 19:14:06 +00004961case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004962 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004963 i64 v;
drh856c1032009-06-02 15:21:42 +00004964 sqlite3_vtab *pVtab;
4965 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004966
drh27a348c2015-04-13 19:14:06 +00004967 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004968 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4969 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004970 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004971 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004972 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004973 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004974 break;
4975 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004976 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004977#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004978 }else if( pC->eCurType==CURTYPE_VTAB ){
4979 assert( pC->uc.pVCur!=0 );
4980 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004981 pModule = pVtab->pModule;
4982 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004983 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004984 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004985 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004986#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004987 }else{
drhc960dcb2015-11-20 19:22:01 +00004988 assert( pC->eCurType==CURTYPE_BTREE );
4989 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004990 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004991 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004992 if( pC->nullRow ){
4993 pOut->flags = MEM_Null;
4994 break;
4995 }
drha7c90c42016-06-04 20:37:10 +00004996 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00004997 }
drh4c583122008-01-04 22:01:03 +00004998 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004999 break;
5000}
5001
drh9cbf3422008-01-17 16:22:13 +00005002/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005003**
5004** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005005** that occur while the cursor is on the null row will always
5006** write a NULL.
drh17f71932002-02-21 12:01:27 +00005007*/
drh9cbf3422008-01-17 16:22:13 +00005008case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005009 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005010
drh653b82a2009-06-22 11:10:47 +00005011 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5012 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005013 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00005014 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005015 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005016 if( pC->eCurType==CURTYPE_BTREE ){
5017 assert( pC->uc.pCursor!=0 );
5018 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005019 }
drhcf025a82018-06-07 18:01:21 +00005020#ifdef SQLITE_DEBUG
5021 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5022#endif
drh17f71932002-02-21 12:01:27 +00005023 break;
5024}
5025
drh86b40df2017-08-01 19:53:43 +00005026/* Opcode: SeekEnd P1 * * * *
5027**
5028** Position cursor P1 at the end of the btree for the purpose of
5029** appending a new entry onto the btree.
5030**
5031** It is assumed that the cursor is used only for appending and so
5032** if the cursor is valid, then the cursor must already be pointing
5033** at the end of the btree and so no changes are made to
5034** the cursor.
5035*/
5036/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005037**
drh8af3f772014-07-25 18:01:06 +00005038** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005039** will refer to the last entry in the database table or index.
5040** If the table or index is empty and P2>0, then jump immediately to P2.
5041** If P2 is 0 or if the table or index is not empty, fall through
5042** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005043**
5044** This opcode leaves the cursor configured to move in reverse order,
5045** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005046** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005047*/
drh86b40df2017-08-01 19:53:43 +00005048case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005049case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005050 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005051 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005052 int res;
drh9562b552002-02-19 15:00:07 +00005053
drh653b82a2009-06-22 11:10:47 +00005054 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5055 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005056 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005057 assert( pC->eCurType==CURTYPE_BTREE );
5058 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005059 res = 0;
drh3da046d2013-11-11 03:24:11 +00005060 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005061#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005062 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005063#endif
drh86b40df2017-08-01 19:53:43 +00005064 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005065 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005066 pC->seekResult = -1;
5067 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5068 break;
5069 }
5070 }
5071 rc = sqlite3BtreeLast(pCrsr, &res);
5072 pC->nullRow = (u8)res;
5073 pC->deferredMoveto = 0;
5074 pC->cacheStatus = CACHE_STALE;
5075 if( rc ) goto abort_due_to_error;
5076 if( pOp->p2>0 ){
5077 VdbeBranchTaken(res!=0,2);
5078 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005079 }
5080 break;
5081}
5082
drh5e98e832017-02-17 19:24:06 +00005083/* Opcode: IfSmaller P1 P2 P3 * *
5084**
5085** Estimate the number of rows in the table P1. Jump to P2 if that
5086** estimate is less than approximately 2**(0.1*P3).
5087*/
5088case OP_IfSmaller: { /* jump */
5089 VdbeCursor *pC;
5090 BtCursor *pCrsr;
5091 int res;
5092 i64 sz;
5093
5094 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5095 pC = p->apCsr[pOp->p1];
5096 assert( pC!=0 );
5097 pCrsr = pC->uc.pCursor;
5098 assert( pCrsr );
5099 rc = sqlite3BtreeFirst(pCrsr, &res);
5100 if( rc ) goto abort_due_to_error;
5101 if( res==0 ){
5102 sz = sqlite3BtreeRowCountEst(pCrsr);
5103 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5104 }
5105 VdbeBranchTaken(res!=0,2);
5106 if( res ) goto jump_to_p2;
5107 break;
5108}
5109
drh0342b1f2005-09-01 03:07:44 +00005110
drh6bd4dc62016-12-23 16:05:22 +00005111/* Opcode: SorterSort P1 P2 * * *
5112**
5113** After all records have been inserted into the Sorter object
5114** identified by P1, invoke this opcode to actually do the sorting.
5115** Jump to P2 if there are no records to be sorted.
5116**
5117** This opcode is an alias for OP_Sort and OP_Rewind that is used
5118** for Sorter objects.
5119*/
drh9cbf3422008-01-17 16:22:13 +00005120/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005121**
5122** This opcode does exactly the same thing as OP_Rewind except that
5123** it increments an undocumented global variable used for testing.
5124**
5125** Sorting is accomplished by writing records into a sorting index,
5126** then rewinding that index and playing it back from beginning to
5127** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5128** rewinding so that the global variable will be incremented and
5129** regression tests can determine whether or not the optimizer is
5130** correctly optimizing out sorts.
5131*/
drhc6aff302011-09-01 15:32:47 +00005132case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005133case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005134#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005135 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005136 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005137#endif
drh9b47ee32013-08-20 03:13:51 +00005138 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005139 /* Fall through into OP_Rewind */
5140}
drh038ebf62019-03-29 15:21:22 +00005141/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005142**
drhf0863fe2005-06-12 21:35:51 +00005143** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005144** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005145** If the table or index is empty, jump immediately to P2.
5146** If the table or index is not empty, fall through to the following
5147** instruction.
drh8af3f772014-07-25 18:01:06 +00005148**
5149** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005150** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005151** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005152*/
drh9cbf3422008-01-17 16:22:13 +00005153case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005154 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005155 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005156 int res;
drh5e00f6c2001-09-13 13:46:56 +00005157
drh653b82a2009-06-22 11:10:47 +00005158 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00005159 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00005160 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005161 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005162 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005163 res = 1;
drh8af3f772014-07-25 18:01:06 +00005164#ifdef SQLITE_DEBUG
5165 pC->seekOp = OP_Rewind;
5166#endif
dan689ab892011-08-12 15:02:00 +00005167 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005168 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005169 }else{
drhc960dcb2015-11-20 19:22:01 +00005170 assert( pC->eCurType==CURTYPE_BTREE );
5171 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005172 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005173 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00005174 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005175 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005176 }
drh9467abf2016-02-17 18:44:11 +00005177 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005178 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005179 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005180 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005181 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005182 break;
5183}
5184
drh0fd61352014-02-07 02:29:45 +00005185/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005186**
5187** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005188** table or index. If there are no more key/value pairs then fall through
5189** to the following instruction. But if the cursor advance was successful,
5190** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005191**
drh5dad9a32014-07-25 18:37:42 +00005192** The Next opcode is only valid following an SeekGT, SeekGE, or
5193** OP_Rewind opcode used to position the cursor. Next is not allowed
5194** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005195**
drhf93cd942013-11-21 03:12:25 +00005196** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5197** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005198**
drhe39a7322014-02-03 14:04:11 +00005199** The P3 value is a hint to the btree implementation. If P3==1, that
5200** means P1 is an SQL index and that this instruction could have been
5201** omitted if that index had been unique. P3 is usually 0. P3 is
5202** always either 0 or 1.
5203**
dana205a482011-08-27 18:48:57 +00005204** P4 is always of type P4_ADVANCE. The function pointer points to
5205** sqlite3BtreeNext().
5206**
drhafc266a2010-03-31 17:47:44 +00005207** If P5 is positive and the jump is taken, then event counter
5208** number P5-1 in the prepared statement is incremented.
5209**
drhf1949b62018-06-07 17:32:59 +00005210** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005211*/
drh0fd61352014-02-07 02:29:45 +00005212/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005213**
5214** Back up cursor P1 so that it points to the previous key/data pair in its
5215** table or index. If there is no previous key/value pairs then fall through
5216** to the following instruction. But if the cursor backup was successful,
5217** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005218**
drh8af3f772014-07-25 18:01:06 +00005219**
drh5dad9a32014-07-25 18:37:42 +00005220** The Prev opcode is only valid following an SeekLT, SeekLE, or
5221** OP_Last opcode used to position the cursor. Prev is not allowed
5222** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005223**
drhf93cd942013-11-21 03:12:25 +00005224** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5225** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005226**
drhe39a7322014-02-03 14:04:11 +00005227** The P3 value is a hint to the btree implementation. If P3==1, that
5228** means P1 is an SQL index and that this instruction could have been
5229** omitted if that index had been unique. P3 is usually 0. P3 is
5230** always either 0 or 1.
5231**
dana205a482011-08-27 18:48:57 +00005232** P4 is always of type P4_ADVANCE. The function pointer points to
5233** sqlite3BtreePrevious().
5234**
drhafc266a2010-03-31 17:47:44 +00005235** If P5 is positive and the jump is taken, then event counter
5236** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005237*/
drh6bd4dc62016-12-23 16:05:22 +00005238/* Opcode: SorterNext P1 P2 * * P5
5239**
5240** This opcode works just like OP_Next except that P1 must be a
5241** sorter object for which the OP_SorterSort opcode has been
5242** invoked. This opcode advances the cursor to the next sorted
5243** record, or jumps to P2 if there are no more sorted records.
5244*/
drhf93cd942013-11-21 03:12:25 +00005245case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005246 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005247
drhf93cd942013-11-21 03:12:25 +00005248 pC = p->apCsr[pOp->p1];
5249 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005250 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005251 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005252case OP_Prev: /* jump */
5253case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005254 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005255 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005256 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005257 assert( pC!=0 );
5258 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005259 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005260 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5261 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005262
drhcf025a82018-06-07 18:01:21 +00005263 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005264 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005265 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005266 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcf025a82018-06-07 18:01:21 +00005267 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
drh94f4f872018-12-20 22:08:32 +00005268 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid);
drhf1949b62018-06-07 17:32:59 +00005269 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005270 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drhcf025a82018-06-07 18:01:21 +00005271 || pC->seekOp==OP_Last
5272 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005273
drh2ab792e2017-05-30 18:34:07 +00005274 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005275next_tail:
drha3460582008-07-11 21:02:53 +00005276 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005277 VdbeBranchTaken(rc==SQLITE_OK,2);
5278 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005279 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005280 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005281#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005282 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005283#endif
drhf56fa462015-04-13 21:39:54 +00005284 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005285 }
drh2ab792e2017-05-30 18:34:07 +00005286 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5287 rc = SQLITE_OK;
5288 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005289 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005290}
5291
drh9b4eaeb2016-11-09 00:10:33 +00005292/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005293** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005294**
drhef8662b2011-06-20 21:47:58 +00005295** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005296** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005297** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005298**
drhfb8c56f2016-11-09 01:19:25 +00005299** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005300** key of reg(P2). In that case, P3 is the index of the first register
5301** for the unpacked key. The availability of the unpacked key can sometimes
5302** be an optimization.
5303**
5304** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5305** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005306**
mistachkin21a919f2014-02-07 03:28:02 +00005307** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5308** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5309** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005310**
drheaf6ae22016-11-09 20:14:34 +00005311** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5312** run faster by avoiding an unnecessary seek on cursor P1. However,
5313** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5314** seeks on the cursor or if the most recent seek used a key equivalent
5315** to P2.
drh0fd61352014-02-07 02:29:45 +00005316**
drhf0863fe2005-06-12 21:35:51 +00005317** This instruction only works for indices. The equivalent instruction
5318** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005319*/
drhf013e202016-10-15 18:37:05 +00005320/* Opcode: SorterInsert P1 P2 * * *
5321** Synopsis: key=r[P2]
5322**
5323** Register P2 holds an SQL index key made using the
5324** MakeRecord instructions. This opcode writes that key
5325** into the sorter P1. Data for the entry is nil.
5326*/
drhca892a72011-09-03 00:17:51 +00005327case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005328case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005329 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005330 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005331
drh653b82a2009-06-22 11:10:47 +00005332 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5333 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005334 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005335 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005336 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005337 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005338 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005339 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005340 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005341 assert( pC->isTable==0 );
5342 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005343 if( rc ) goto abort_due_to_error;
5344 if( pOp->opcode==OP_SorterInsert ){
5345 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5346 }else{
drh8eeb4462016-05-21 20:03:42 +00005347 x.nKey = pIn2->n;
5348 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005349 x.aMem = aMem + pOp->p3;
5350 x.nMem = (u16)pOp->p4.i;
5351 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005352 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005353 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5354 );
5355 assert( pC->deferredMoveto==0 );
5356 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005357 }
drh9467abf2016-02-17 18:44:11 +00005358 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005359 break;
5360}
5361
drhd1d38482008-10-07 23:46:38 +00005362/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005363** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005364**
drhe14006d2008-03-25 17:23:32 +00005365** The content of P3 registers starting at register P2 form
5366** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005367** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005368*/
drhe14006d2008-03-25 17:23:32 +00005369case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005370 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005371 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005372 int res;
5373 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005374
drhe14006d2008-03-25 17:23:32 +00005375 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005376 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005377 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5378 pC = p->apCsr[pOp->p1];
5379 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005380 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005381 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005382 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005383 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005384 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005385 r.pKeyInfo = pC->pKeyInfo;
5386 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005387 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005388 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005389 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005390 if( rc ) goto abort_due_to_error;
5391 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005392 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005393 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005394 }
drh3da046d2013-11-11 03:24:11 +00005395 assert( pC->deferredMoveto==0 );
5396 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005397 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005398 break;
5399}
5400
drh170ad682017-06-02 15:44:22 +00005401/* Opcode: DeferredSeek P1 * P3 P4 *
5402** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005403**
5404** P1 is an open index cursor and P3 is a cursor on the corresponding
5405** table. This opcode does a deferred seek of the P3 table cursor
5406** to the row that corresponds to the current row of P1.
5407**
5408** This is a deferred seek. Nothing actually happens until
5409** the cursor is used to read a record. That way, if no reads
5410** occur, no unnecessary I/O happens.
5411**
5412** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005413** one entry for each column in the P3 table. If array entry a(i)
5414** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005415** equivalent to performing the deferred seek and then reading column i
5416** from P1. This information is stored in P3 and used to redirect
5417** reads against P3 over to P1, thus possibly avoiding the need to
5418** seek and read cursor P3.
5419*/
drh2133d822008-01-03 18:44:59 +00005420/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005421** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005422**
drh2133d822008-01-03 18:44:59 +00005423** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005424** the end of the index key pointed to by cursor P1. This integer should be
5425** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005426**
drh9437bd22009-02-01 00:29:56 +00005427** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005428*/
drh170ad682017-06-02 15:44:22 +00005429case OP_DeferredSeek:
5430case OP_IdxRowid: { /* out2 */
5431 VdbeCursor *pC; /* The P1 index cursor */
5432 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5433 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005434
drh653b82a2009-06-22 11:10:47 +00005435 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5436 pC = p->apCsr[pOp->p1];
5437 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005438 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005439 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005440 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005441 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005442 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5443
5444 /* The IdxRowid and Seek opcodes are combined because of the commonality
5445 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5446 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005447
5448 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005449 ** out from under the cursor. That will never happens for an IdxRowid
5450 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005451 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5452
drh3da046d2013-11-11 03:24:11 +00005453 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005454 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005455 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005456 if( rc!=SQLITE_OK ){
5457 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005458 }
drh170ad682017-06-02 15:44:22 +00005459 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005460 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5461 pTabCur = p->apCsr[pOp->p3];
5462 assert( pTabCur!=0 );
5463 assert( pTabCur->eCurType==CURTYPE_BTREE );
5464 assert( pTabCur->uc.pCursor!=0 );
5465 assert( pTabCur->isTable );
5466 pTabCur->nullRow = 0;
5467 pTabCur->movetoTarget = rowid;
5468 pTabCur->deferredMoveto = 1;
5469 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5470 pTabCur->aAltMap = pOp->p4.ai;
5471 pTabCur->pAltCursor = pC;
5472 }else{
5473 pOut = out2Prerelease(p, pOp);
5474 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005475 }
5476 }else{
5477 assert( pOp->opcode==OP_IdxRowid );
5478 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005479 }
5480 break;
5481}
5482
danielk197761dd5832008-04-18 11:31:12 +00005483/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005484** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005485**
danielk197761dd5832008-04-18 11:31:12 +00005486** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005487** key that omits the PRIMARY KEY. Compare this key value against the index
5488** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5489** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005490**
danielk197761dd5832008-04-18 11:31:12 +00005491** If the P1 index entry is greater than or equal to the key value
5492** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005493*/
5494/* Opcode: IdxGT P1 P2 P3 P4 P5
5495** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005496**
drh4a1d3652014-02-14 15:13:36 +00005497** The P4 register values beginning with P3 form an unpacked index
5498** key that omits the PRIMARY KEY. Compare this key value against the index
5499** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5500** fields at the end.
5501**
5502** If the P1 index entry is greater than the key value
5503** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005504*/
drh3bb9b932010-08-06 02:10:00 +00005505/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005506** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005507**
danielk197761dd5832008-04-18 11:31:12 +00005508** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005509** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5510** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5511** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005512**
danielk197761dd5832008-04-18 11:31:12 +00005513** If the P1 index entry is less than the key value then jump to P2.
5514** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005515*/
drh4a1d3652014-02-14 15:13:36 +00005516/* Opcode: IdxLE P1 P2 P3 P4 P5
5517** Synopsis: key=r[P3@P4]
5518**
5519** The P4 register values beginning with P3 form an unpacked index
5520** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5521** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5522** ROWID on the P1 index.
5523**
5524** If the P1 index entry is less than or equal to the key value then jump
5525** to P2. Otherwise fall through to the next instruction.
5526*/
5527case OP_IdxLE: /* jump */
5528case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005529case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005530case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005531 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005532 int res;
5533 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005534
drh653b82a2009-06-22 11:10:47 +00005535 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5536 pC = p->apCsr[pOp->p1];
5537 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005538 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005539 assert( pC->eCurType==CURTYPE_BTREE );
5540 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005541 assert( pC->deferredMoveto==0 );
5542 assert( pOp->p5==0 || pOp->p5==1 );
5543 assert( pOp->p4type==P4_INT32 );
5544 r.pKeyInfo = pC->pKeyInfo;
5545 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005546 if( pOp->opcode<OP_IdxLT ){
5547 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005548 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005549 }else{
drh4a1d3652014-02-14 15:13:36 +00005550 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005551 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005552 }
5553 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005554#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00005555 {
5556 int i;
5557 for(i=0; i<r.nField; i++){
5558 assert( memIsValid(&r.aMem[i]) );
5559 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
5560 }
5561 }
drh2b4ded92010-09-27 21:09:31 +00005562#endif
drh2dc06482013-12-11 00:59:10 +00005563 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005564 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005565 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5566 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5567 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005568 res = -res;
5569 }else{
drh4a1d3652014-02-14 15:13:36 +00005570 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005571 res++;
5572 }
drh688852a2014-02-17 22:40:43 +00005573 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005574 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005575 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005576 break;
5577}
5578
drh98757152008-01-09 23:04:12 +00005579/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005580**
5581** Delete an entire database table or index whose root page in the database
5582** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005583**
drh98757152008-01-09 23:04:12 +00005584** The table being destroyed is in the main database file if P3==0. If
5585** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005586** that is used to store tables create using CREATE TEMPORARY TABLE.
5587**
drh205f48e2004-11-05 00:43:11 +00005588** If AUTOVACUUM is enabled then it is possible that another root page
5589** might be moved into the newly deleted root page in order to keep all
5590** root pages contiguous at the beginning of the database. The former
5591** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005592** is stored in register P2. If no page movement was required (because the
5593** table being dropped was already the last one in the database) then a
5594** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5595** is stored in register P2.
5596**
5597** This opcode throws an error if there are any active reader VMs when
5598** it is invoked. This is done to avoid the difficulty associated with
5599** updating existing cursors when a root page is moved in an AUTOVACUUM
5600** database. This error is thrown even if the database is not an AUTOVACUUM
5601** db in order to avoid introducing an incompatibility between autovacuum
5602** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005603**
drhb19a2bc2001-09-16 00:13:26 +00005604** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005605*/
drh27a348c2015-04-13 19:14:06 +00005606case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005607 int iMoved;
drh856c1032009-06-02 15:21:42 +00005608 int iDb;
drh3a949872012-09-18 13:20:13 +00005609
drh4031baf2018-05-28 17:31:20 +00005610 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005611 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005612 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005613 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005614 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005615 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005616 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005617 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005618 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005619 }else{
drh856c1032009-06-02 15:21:42 +00005620 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005621 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005622 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005623 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005624 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005625 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005626 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005627#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005628 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005629 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5630 /* All OP_Destroy operations occur on the same btree */
5631 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5632 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005633 }
drh3765df42006-06-28 18:18:09 +00005634#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005635 }
drh5e00f6c2001-09-13 13:46:56 +00005636 break;
5637}
5638
danielk1977c7af4842008-10-27 13:59:33 +00005639/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005640**
5641** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005642** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005643** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005644**
drhf57b3392001-10-08 13:22:32 +00005645** The table being clear is in the main database file if P2==0. If
5646** P2==1 then the table to be clear is in the auxiliary database file
5647** that is used to store tables create using CREATE TEMPORARY TABLE.
5648**
shanebe217792009-03-05 04:20:31 +00005649** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005650** intkey table (an SQL table, not an index). In this case the row change
5651** count is incremented by the number of rows in the table being cleared.
5652** If P3 is greater than zero, then the value stored in register P3 is
5653** also incremented by the number of rows in the table being cleared.
5654**
drhb19a2bc2001-09-16 00:13:26 +00005655** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005656*/
drh9cbf3422008-01-17 16:22:13 +00005657case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005658 int nChange;
5659
drh4031baf2018-05-28 17:31:20 +00005660 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005661 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005662 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005663 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005664 rc = sqlite3BtreeClearTable(
5665 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5666 );
5667 if( pOp->p3 ){
5668 p->nChange += nChange;
5669 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005670 assert( memIsValid(&aMem[pOp->p3]) );
5671 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005672 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005673 }
5674 }
drh9467abf2016-02-17 18:44:11 +00005675 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005676 break;
5677}
5678
drh65ea12c2014-03-19 17:41:36 +00005679/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005680**
drh65ea12c2014-03-19 17:41:36 +00005681** Delete all contents from the ephemeral table or sorter
5682** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005683**
drh65ea12c2014-03-19 17:41:36 +00005684** This opcode only works for cursors used for sorting and
5685** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005686*/
drh65ea12c2014-03-19 17:41:36 +00005687case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005688 VdbeCursor *pC;
5689
5690 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5691 pC = p->apCsr[pOp->p1];
5692 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005693 if( isSorter(pC) ){
5694 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005695 }else{
drhc960dcb2015-11-20 19:22:01 +00005696 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005697 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005698 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005699 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005700 }
drh079a3072014-03-19 14:10:55 +00005701 break;
5702}
5703
drh0f3f7662017-08-18 14:34:28 +00005704/* Opcode: CreateBtree P1 P2 P3 * *
5705** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005706**
drh0f3f7662017-08-18 14:34:28 +00005707** Allocate a new b-tree in the main database file if P1==0 or in the
5708** TEMP database file if P1==1 or in an attached database if
5709** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00005710** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00005711** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005712*/
drh0f3f7662017-08-18 14:34:28 +00005713case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005714 int pgno;
drh234c39d2004-07-24 03:30:47 +00005715 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005716
drh4031baf2018-05-28 17:31:20 +00005717 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00005718 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005719 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005720 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005721 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005722 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005723 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005724 pDb = &db->aDb[pOp->p1];
5725 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005726 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005727 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005728 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005729 break;
5730}
5731
drh4a54bb52017-02-18 15:58:52 +00005732/* Opcode: SqlExec * * * P4 *
5733**
5734** Run the SQL statement or statements specified in the P4 string.
5735*/
5736case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00005737 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00005738 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005739 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005740 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005741 if( rc ) goto abort_due_to_error;
5742 break;
5743}
5744
drh22645842011-03-24 01:34:03 +00005745/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005746**
5747** Read and parse all entries from the SQLITE_MASTER table of database P1
drh1595abc2018-08-14 19:27:51 +00005748** that match the WHERE clause P4. If P4 is a NULL pointer, then the
5749** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00005750**
5751** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005752** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005753*/
drh9cbf3422008-01-17 16:22:13 +00005754case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005755 int iDb;
5756 const char *zMaster;
5757 char *zSql;
5758 InitData initData;
5759
drhbdaec522011-04-04 00:14:43 +00005760 /* Any prepared statement that invokes this opcode will hold mutexes
5761 ** on every btree. This is a prerequisite for invoking
5762 ** sqlite3InitCallback().
5763 */
5764#ifdef SQLITE_DEBUG
5765 for(iDb=0; iDb<db->nDb; iDb++){
5766 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5767 }
5768#endif
drhbdaec522011-04-04 00:14:43 +00005769
drh856c1032009-06-02 15:21:42 +00005770 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005771 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005772 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
dane325ffe2018-08-11 13:40:20 +00005773
5774#ifndef SQLITE_OMIT_ALTERTABLE
5775 if( pOp->p4.z==0 ){
5776 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00005777 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
drh1595abc2018-08-14 19:27:51 +00005778 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
dane325ffe2018-08-11 13:40:20 +00005779 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00005780 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00005781 }else
5782#endif
drh1595abc2018-08-14 19:27:51 +00005783 {
drhe0a04a32016-12-16 01:00:21 +00005784 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005785 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00005786 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00005787 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00005788 initData.mInitFlags = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005789 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005790 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005791 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005792 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005793 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005794 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005795 assert( db->init.busy==0 );
5796 db->init.busy = 1;
5797 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00005798 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005799 assert( !db->mallocFailed );
5800 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5801 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00005802 if( rc==SQLITE_OK && initData.nInitRow==0 ){
5803 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
5804 ** at least one SQL statement. Any less than that indicates that
5805 ** the sqlite_master table is corrupt. */
5806 rc = SQLITE_CORRUPT_BKPT;
5807 }
drhdbd6a7d2017-04-05 12:39:49 +00005808 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005809 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005810 }
drh3c23a882007-01-09 14:01:13 +00005811 }
drh9467abf2016-02-17 18:44:11 +00005812 if( rc ){
5813 sqlite3ResetAllSchemasOfConnection(db);
5814 if( rc==SQLITE_NOMEM ){
5815 goto no_mem;
5816 }
5817 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005818 }
drh234c39d2004-07-24 03:30:47 +00005819 break;
5820}
5821
drh8bfdf722009-06-19 14:06:03 +00005822#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005823/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005824**
5825** Read the sqlite_stat1 table for database P1 and load the content
5826** of that table into the internal index hash table. This will cause
5827** the analysis to be used when preparing all subsequent queries.
5828*/
drh9cbf3422008-01-17 16:22:13 +00005829case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005830 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5831 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005832 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005833 break;
5834}
drh8bfdf722009-06-19 14:06:03 +00005835#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005836
drh98757152008-01-09 23:04:12 +00005837/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005838**
5839** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005840** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005841** is dropped from disk (using the Destroy opcode) in order to keep
5842** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005843** schema consistent with what is on disk.
5844*/
drh9cbf3422008-01-17 16:22:13 +00005845case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00005846 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005847 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005848 break;
5849}
5850
drh98757152008-01-09 23:04:12 +00005851/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005852**
5853** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005854** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005855** is dropped from disk (using the Destroy opcode)
5856** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005857** schema consistent with what is on disk.
5858*/
drh9cbf3422008-01-17 16:22:13 +00005859case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00005860 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005861 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005862 break;
5863}
5864
drh98757152008-01-09 23:04:12 +00005865/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005866**
5867** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005868** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005869** is dropped from disk (using the Destroy opcode) in order to keep
5870** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005871** schema consistent with what is on disk.
5872*/
drh9cbf3422008-01-17 16:22:13 +00005873case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00005874 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005875 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005876 break;
5877}
5878
drh234c39d2004-07-24 03:30:47 +00005879
drhb7f91642004-10-31 02:22:47 +00005880#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005881/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005882**
drh98757152008-01-09 23:04:12 +00005883** Do an analysis of the currently open database. Store in
5884** register P1 the text of an error message describing any problems.
5885** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005886**
drh66accfc2017-02-22 18:04:42 +00005887** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005888** At most reg(P3) errors will be reported.
5889** In other words, the analysis stops as soon as reg(P1) errors are
5890** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005891**
drh98968b22016-03-15 22:00:39 +00005892** The root page numbers of all tables in the database are integers
5893** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005894**
drh98757152008-01-09 23:04:12 +00005895** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005896** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005897**
drh1dcdbc02007-01-27 02:24:54 +00005898** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005899*/
drhaaab5722002-02-19 13:39:21 +00005900case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005901 int nRoot; /* Number of tables to check. (Number of root pages.) */
5902 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005903 int nErr; /* Number of errors reported */
5904 char *z; /* Text of the error report */
5905 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005906
drh1713afb2013-06-28 01:24:57 +00005907 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005908 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005909 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005910 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00005911 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00005912 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005913 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005914 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005915 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005916 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005917 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005918 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drhb5c10632017-09-21 00:49:15 +00005919 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00005920 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005921 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005922 if( nErr==0 ){
5923 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005924 }else if( z==0 ){
5925 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005926 }else{
drh66accfc2017-02-22 18:04:42 +00005927 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005928 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005929 }
drhb7654112008-01-12 12:48:07 +00005930 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005931 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005932 break;
5933}
drhb7f91642004-10-31 02:22:47 +00005934#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005935
drh3d4501e2008-12-04 20:40:10 +00005936/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005937** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005938**
drhbb6783b2017-04-29 18:02:49 +00005939** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00005940** held in register P1.
5941**
5942** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005943*/
drh93952eb2009-11-13 19:43:43 +00005944case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005945 pIn1 = &aMem[pOp->p1];
5946 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005947 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00005948 if( (pIn1->flags & MEM_Blob)==0 ){
5949 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005950 }
drh9d67afc2018-08-29 20:24:03 +00005951 assert( sqlite3VdbeMemIsRowSet(pIn1) );
5952 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005953 break;
5954}
5955
5956/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005957** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005958**
drhbb6783b2017-04-29 18:02:49 +00005959** Extract the smallest value from the RowSet object in P1
5960** and put that value into register P3.
5961** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00005962** unchanged and jump to instruction P2.
5963*/
drh93952eb2009-11-13 19:43:43 +00005964case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005965 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005966
drh3c657212009-11-17 23:59:58 +00005967 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00005968 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
5969 if( (pIn1->flags & MEM_Blob)==0
5970 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005971 ){
5972 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005973 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005974 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005975 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005976 }else{
5977 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005978 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005979 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005980 }
drh49afe3a2013-07-10 03:05:14 +00005981 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005982}
5983
drh1b26c7c2009-04-22 02:15:47 +00005984/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005985** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005986**
drhade97602009-04-21 15:05:18 +00005987** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005988** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005989** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005990** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005991** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005992**
drhbb6783b2017-04-29 18:02:49 +00005993** The RowSet object is optimized for the case where sets of integers
5994** are inserted in distinct phases, which each set contains no duplicates.
5995** Each set is identified by a unique P4 value. The first set
5996** must have P4==0, the final set must have P4==-1, and for all other sets
5997** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00005998**
5999** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006000** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006001** (b) when P4==-1 there is no need to insert the value, as it will
6002** never be tested for, and (c) when a value that is part of set X is
6003** inserted, there is no need to search to see if the same value was
6004** previously inserted as part of set X (only if it was previously
6005** inserted as part of some other set).
6006*/
drh1b26c7c2009-04-22 02:15:47 +00006007case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006008 int iSet;
6009 int exists;
6010
drh3c657212009-11-17 23:59:58 +00006011 pIn1 = &aMem[pOp->p1];
6012 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006013 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006014 assert( pIn3->flags&MEM_Int );
6015
drh1b26c7c2009-04-22 02:15:47 +00006016 /* If there is anything other than a rowset object in memory cell P1,
6017 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006018 */
drh9d67afc2018-08-29 20:24:03 +00006019 if( (pIn1->flags & MEM_Blob)==0 ){
6020 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006021 }
drh9d67afc2018-08-29 20:24:03 +00006022 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006023 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006024 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006025 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006026 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006027 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006028 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006029 }
6030 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006031 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006032 }
6033 break;
6034}
6035
drh5e00f6c2001-09-13 13:46:56 +00006036
danielk197793758c82005-01-21 08:13:14 +00006037#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006038
drh0fd61352014-02-07 02:29:45 +00006039/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006040**
dan76d462e2009-08-30 11:42:51 +00006041** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006042**
dan76d462e2009-08-30 11:42:51 +00006043** P1 contains the address of the memory cell that contains the first memory
6044** cell in an array of values used as arguments to the sub-program. P2
6045** contains the address to jump to if the sub-program throws an IGNORE
6046** exception using the RAISE() function. Register P3 contains the address
6047** of a memory cell in this (the parent) VM that is used to allocate the
6048** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006049**
6050** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006051**
6052** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006053*/
dan76d462e2009-08-30 11:42:51 +00006054case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006055 int nMem; /* Number of memory registers for sub-program */
6056 int nByte; /* Bytes of runtime space required for sub-program */
6057 Mem *pRt; /* Register to allocate runtime space */
6058 Mem *pMem; /* Used to iterate through memory cells */
6059 Mem *pEnd; /* Last memory cell in new array */
6060 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6061 SubProgram *pProgram; /* Sub-program to execute */
6062 void *t; /* Token identifying trigger */
6063
6064 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006065 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006066 assert( pProgram->nOp>0 );
6067
dan1da40a32009-09-19 17:00:31 +00006068 /* If the p5 flag is clear, then recursive invocation of triggers is
6069 ** disabled for backwards compatibility (p5 is set if this sub-program
6070 ** is really a trigger, not a foreign key action, and the flag set
6071 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006072 **
6073 ** It is recursive invocation of triggers, at the SQL level, that is
6074 ** disabled. In some cases a single trigger may generate more than one
6075 ** SubProgram (if the trigger may be executed with more than one different
6076 ** ON CONFLICT algorithm). SubProgram structures associated with a
6077 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006078 ** variable. */
6079 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006080 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006081 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6082 if( pFrame ) break;
6083 }
6084
danf5894502009-10-07 18:41:19 +00006085 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006086 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006087 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006088 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006089 }
6090
6091 /* Register pRt is used to store the memory required to save the state
6092 ** of the current program, and the memory required at runtime to execute
6093 ** the trigger program. If this trigger has been fired before, then pRt
6094 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006095 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006096 /* SubProgram.nMem is set to the number of memory cells used by the
6097 ** program stored in SubProgram.aOp. As well as these, one memory
6098 ** cell is required for each cursor used by the program. Set local
6099 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6100 */
dan65a7cd12009-09-01 12:16:01 +00006101 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006102 assert( nMem>0 );
6103 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006104 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006105 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006106 + pProgram->nCsr * sizeof(VdbeCursor*)
6107 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006108 pFrame = sqlite3DbMallocZero(db, nByte);
6109 if( !pFrame ){
6110 goto no_mem;
6111 }
6112 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006113 pRt->flags = MEM_Blob|MEM_Dyn;
6114 pRt->z = (char*)pFrame;
6115 pRt->n = nByte;
6116 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006117
6118 pFrame->v = p;
6119 pFrame->nChildMem = nMem;
6120 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006121 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006122 pFrame->aMem = p->aMem;
6123 pFrame->nMem = p->nMem;
6124 pFrame->apCsr = p->apCsr;
6125 pFrame->nCursor = p->nCursor;
6126 pFrame->aOp = p->aOp;
6127 pFrame->nOp = p->nOp;
6128 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006129#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006130 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006131#endif
drh72f56ef2018-08-29 18:47:22 +00006132#ifdef SQLITE_DEBUG
6133 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
6134#endif
dan165921a2009-08-28 18:53:45 +00006135
6136 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6137 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006138 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006139 pMem->db = db;
6140 }
6141 }else{
drh72f56ef2018-08-29 18:47:22 +00006142 pFrame = (VdbeFrame*)pRt->z;
6143 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00006144 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6145 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006146 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006147 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006148 }
6149
6150 p->nFrame++;
6151 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006152 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006153 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006154 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006155 assert( pFrame->pAuxData==0 );
6156 pFrame->pAuxData = p->pAuxData;
6157 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006158 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006159 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006160 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006161 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006162 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006163 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006164 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006165 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006166 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006167 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006168#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006169 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006170#endif
drhb2e61bc2019-01-25 19:29:01 +00006171#ifdef SQLITE_DEBUG
6172 /* Verify that second and subsequent executions of the same trigger do not
6173 ** try to reuse register values from the first use. */
6174 {
6175 int i;
6176 for(i=0; i<p->nMem; i++){
6177 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
6178 aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
6179 }
6180 }
6181#endif
drhf56fa462015-04-13 21:39:54 +00006182 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00006183 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00006184}
6185
dan76d462e2009-08-30 11:42:51 +00006186/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006187**
dan76d462e2009-08-30 11:42:51 +00006188** This opcode is only ever present in sub-programs called via the
6189** OP_Program instruction. Copy a value currently stored in a memory
6190** cell of the calling (parent) frame to cell P2 in the current frames
6191** address space. This is used by trigger programs to access the new.*
6192** and old.* values.
dan165921a2009-08-28 18:53:45 +00006193**
dan76d462e2009-08-30 11:42:51 +00006194** The address of the cell in the parent frame is determined by adding
6195** the value of the P1 argument to the value of the P1 argument to the
6196** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006197*/
drh27a348c2015-04-13 19:14:06 +00006198case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006199 VdbeFrame *pFrame;
6200 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006201 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006202 pFrame = p->pFrame;
6203 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006204 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6205 break;
6206}
6207
danielk197793758c82005-01-21 08:13:14 +00006208#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006209
dan1da40a32009-09-19 17:00:31 +00006210#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006211/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006212** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006213**
dan0ff297e2009-09-25 17:03:14 +00006214** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6215** If P1 is non-zero, the database constraint counter is incremented
6216** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006217** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006218*/
dan32b09f22009-09-23 17:29:59 +00006219case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006220 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006221 db->nDeferredImmCons += pOp->p2;
6222 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006223 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006224 }else{
dan0ff297e2009-09-25 17:03:14 +00006225 p->nFkConstraint += pOp->p2;
6226 }
6227 break;
6228}
6229
6230/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006231** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006232**
6233** This opcode tests if a foreign key constraint-counter is currently zero.
6234** If so, jump to instruction P2. Otherwise, fall through to the next
6235** instruction.
6236**
6237** If P1 is non-zero, then the jump is taken if the database constraint-counter
6238** is zero (the one that counts deferred constraint violations). If P1 is
6239** zero, the jump is taken if the statement constraint-counter is zero
6240** (immediate foreign key constraint violations).
6241*/
6242case OP_FkIfZero: { /* jump */
6243 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006244 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006245 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006246 }else{
drh688852a2014-02-17 22:40:43 +00006247 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006248 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006249 }
dan1da40a32009-09-19 17:00:31 +00006250 break;
6251}
6252#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6253
drh205f48e2004-11-05 00:43:11 +00006254#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006255/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006256** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006257**
dan76d462e2009-08-30 11:42:51 +00006258** P1 is a register in the root frame of this VM (the root frame is
6259** different from the current frame if this instruction is being executed
6260** within a sub-program). Set the value of register P1 to the maximum of
6261** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006262**
6263** This instruction throws an error if the memory cell is not initially
6264** an integer.
6265*/
dan76d462e2009-08-30 11:42:51 +00006266case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006267 VdbeFrame *pFrame;
6268 if( p->pFrame ){
6269 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6270 pIn1 = &pFrame->aMem[pOp->p1];
6271 }else{
drha6c2ed92009-11-14 23:22:23 +00006272 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006273 }
drh2b4ded92010-09-27 21:09:31 +00006274 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006275 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006276 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006277 sqlite3VdbeMemIntegerify(pIn2);
6278 if( pIn1->u.i<pIn2->u.i){
6279 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006280 }
6281 break;
6282}
6283#endif /* SQLITE_OMIT_AUTOINCREMENT */
6284
drh8b0cf382015-10-06 21:07:06 +00006285/* Opcode: IfPos P1 P2 P3 * *
6286** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006287**
drh16897072015-03-07 00:57:37 +00006288** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006289** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006290** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006291**
drh16897072015-03-07 00:57:37 +00006292** If the initial value of register P1 is less than 1, then the
6293** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006294*/
drh9cbf3422008-01-17 16:22:13 +00006295case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006296 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006297 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006298 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006299 if( pIn1->u.i>0 ){
6300 pIn1->u.i -= pOp->p3;
6301 goto jump_to_p2;
6302 }
drhec7429a2005-10-06 16:53:14 +00006303 break;
6304}
6305
drhcc2fa4c2016-01-25 15:57:29 +00006306/* Opcode: OffsetLimit P1 P2 P3 * *
6307** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006308**
drhcc2fa4c2016-01-25 15:57:29 +00006309** This opcode performs a commonly used computation associated with
6310** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6311** holds the offset counter. The opcode computes the combined value
6312** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6313** value computed is the total number of rows that will need to be
6314** visited in order to complete the query.
6315**
6316** If r[P3] is zero or negative, that means there is no OFFSET
6317** and r[P2] is set to be the value of the LIMIT, r[P1].
6318**
6319** if r[P1] is zero or negative, that means there is no LIMIT
6320** and r[P2] is set to -1.
6321**
6322** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006323*/
drhcc2fa4c2016-01-25 15:57:29 +00006324case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006325 i64 x;
drh3c657212009-11-17 23:59:58 +00006326 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006327 pIn3 = &aMem[pOp->p3];
6328 pOut = out2Prerelease(p, pOp);
6329 assert( pIn1->flags & MEM_Int );
6330 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006331 x = pIn1->u.i;
6332 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6333 /* If the LIMIT is less than or equal to zero, loop forever. This
6334 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6335 ** also loop forever. This is undocumented. In fact, one could argue
6336 ** that the loop should terminate. But assuming 1 billion iterations
6337 ** per second (far exceeding the capabilities of any current hardware)
6338 ** it would take nearly 300 years to actually reach the limit. So
6339 ** looping forever is a reasonable approximation. */
6340 pOut->u.i = -1;
6341 }else{
6342 pOut->u.i = x;
6343 }
drh15007a92006-01-08 18:10:17 +00006344 break;
6345}
6346
drhf99dd352016-12-18 17:42:00 +00006347/* Opcode: IfNotZero P1 P2 * * *
6348** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006349**
drh16897072015-03-07 00:57:37 +00006350** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006351** initially greater than zero, then decrement the value in register P1.
6352** If it is non-zero (negative or positive) and then also jump to P2.
6353** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006354*/
drh16897072015-03-07 00:57:37 +00006355case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006356 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006357 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006358 VdbeBranchTaken(pIn1->u.i<0, 2);
6359 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006360 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006361 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006362 }
6363 break;
6364}
6365
6366/* Opcode: DecrJumpZero P1 P2 * * *
6367** Synopsis: if (--r[P1])==0 goto P2
6368**
drhab5be2e2016-11-30 05:08:59 +00006369** Register P1 must hold an integer. Decrement the value in P1
6370** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006371*/
6372case OP_DecrJumpZero: { /* jump, in1 */
6373 pIn1 = &aMem[pOp->p1];
6374 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006375 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6376 VdbeBranchTaken(pIn1->u.i==0, 2);
6377 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006378 break;
6379}
6380
drh16897072015-03-07 00:57:37 +00006381
drh8f26da62018-07-05 21:22:57 +00006382/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006383** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006384**
drh8f26da62018-07-05 21:22:57 +00006385** Execute the xStep function for an aggregate.
6386** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006387** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006388** accumulator.
drhe5095352002-02-24 03:25:14 +00006389**
drh98757152008-01-09 23:04:12 +00006390** The P5 arguments are taken from register P2 and its
6391** successors.
drhe5095352002-02-24 03:25:14 +00006392*/
drh8f26da62018-07-05 21:22:57 +00006393/* Opcode: AggInverse * P2 P3 P4 P5
6394** Synopsis: accum=r[P3] inverse(r[P2@P5])
6395**
6396** Execute the xInverse function for an aggregate.
6397** The function has P5 arguments. P4 is a pointer to the
6398** FuncDef structure that specifies the function. Register P3 is the
6399** accumulator.
6400**
6401** The P5 arguments are taken from register P2 and its
6402** successors.
6403*/
6404/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006405** Synopsis: accum=r[P3] step(r[P2@P5])
6406**
dan9a947222018-06-14 19:06:36 +00006407** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6408** aggregate. The function has P5 arguments. P4 is a pointer to the
6409** FuncDef structure that specifies the function. Register P3 is the
6410** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006411**
6412** The P5 arguments are taken from register P2 and its
6413** successors.
6414**
6415** This opcode is initially coded as OP_AggStep0. On first evaluation,
6416** the FuncDef stored in P4 is converted into an sqlite3_context and
6417** the opcode is changed. In this way, the initialization of the
6418** sqlite3_context only happens once, instead of on each call to the
6419** step function.
6420*/
drh8f26da62018-07-05 21:22:57 +00006421case OP_AggInverse:
6422case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006423 int n;
drh9c7c9132015-06-26 18:16:52 +00006424 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006425
drh9c7c9132015-06-26 18:16:52 +00006426 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006427 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006428 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6429 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006430 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006431 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6432 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006433 if( pCtx==0 ) goto no_mem;
6434 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006435 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6436 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006437 pCtx->pFunc = pOp->p4.pFunc;
6438 pCtx->iOp = (int)(pOp - aOp);
6439 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006440 pCtx->skipFlag = 0;
6441 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006442 pCtx->argc = n;
6443 pOp->p4type = P4_FUNCCTX;
6444 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006445
6446 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006447 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006448
drh8f26da62018-07-05 21:22:57 +00006449 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006450 /* Fall through into OP_AggStep */
6451}
drh8f26da62018-07-05 21:22:57 +00006452case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006453 int i;
6454 sqlite3_context *pCtx;
6455 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006456
6457 assert( pOp->p4type==P4_FUNCCTX );
6458 pCtx = pOp->p4.pCtx;
6459 pMem = &aMem[pOp->p3];
6460
drh2c885d02018-07-07 19:36:04 +00006461#ifdef SQLITE_DEBUG
6462 if( pOp->p1 ){
6463 /* This is an OP_AggInverse call. Verify that xStep has always
6464 ** been called at least once prior to any xInverse call. */
6465 assert( pMem->uTemp==0x1122e0e3 );
6466 }else{
6467 /* This is an OP_AggStep call. Mark it as such. */
6468 pMem->uTemp = 0x1122e0e3;
6469 }
6470#endif
6471
drh9c7c9132015-06-26 18:16:52 +00006472 /* If this function is inside of a trigger, the register array in aMem[]
6473 ** might change from one evaluation to the next. The next block of code
6474 ** checks to see if the register array has changed, and if so it
6475 ** reinitializes the relavant parts of the sqlite3_context object */
6476 if( pCtx->pMem != pMem ){
6477 pCtx->pMem = pMem;
6478 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6479 }
6480
6481#ifdef SQLITE_DEBUG
6482 for(i=0; i<pCtx->argc; i++){
6483 assert( memIsValid(pCtx->argv[i]) );
6484 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6485 }
6486#endif
6487
drhabfcea22005-09-06 20:36:48 +00006488 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006489 assert( pCtx->pOut->flags==MEM_Null );
6490 assert( pCtx->isError==0 );
6491 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006492#ifndef SQLITE_OMIT_WINDOWFUNC
6493 if( pOp->p1 ){
6494 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6495 }else
6496#endif
6497 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6498
drhf09ac0b2018-01-23 03:44:06 +00006499 if( pCtx->isError ){
6500 if( pCtx->isError>0 ){
6501 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006502 rc = pCtx->isError;
6503 }
drhf09ac0b2018-01-23 03:44:06 +00006504 if( pCtx->skipFlag ){
6505 assert( pOp[-1].opcode==OP_CollSeq );
6506 i = pOp[-1].p1;
6507 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6508 pCtx->skipFlag = 0;
6509 }
6510 sqlite3VdbeMemRelease(pCtx->pOut);
6511 pCtx->pOut->flags = MEM_Null;
6512 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006513 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006514 }
drhf09ac0b2018-01-23 03:44:06 +00006515 assert( pCtx->pOut->flags==MEM_Null );
6516 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006517 break;
6518}
6519
drh8f26da62018-07-05 21:22:57 +00006520/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006521** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006522**
dan9a947222018-06-14 19:06:36 +00006523** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00006524** or window function. Execute the finalizer function
6525** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00006526**
6527** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006528** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006529** argument is not used by this opcode. It is only there to disambiguate
6530** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00006531** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00006532** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006533*/
drh8f26da62018-07-05 21:22:57 +00006534/* Opcode: AggValue * P2 P3 P4 *
6535** Synopsis: r[P3]=value N=P2
6536**
6537** Invoke the xValue() function and store the result in register P3.
6538**
6539** P2 is the number of arguments that the step function takes and
6540** P4 is a pointer to the FuncDef for this function. The P2
6541** argument is not used by this opcode. It is only there to disambiguate
6542** functions that can take varying numbers of arguments. The
6543** P4 argument is only needed for the case where
6544** the step function was not previously called.
6545*/
6546case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00006547case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006548 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006549 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00006550 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00006551 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006552 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006553#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006554 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00006555 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00006556 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006557 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006558 }else
6559#endif
drh8f26da62018-07-05 21:22:57 +00006560 {
6561 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6562 }
dan67a9b8e2018-06-22 20:51:35 +00006563
drh4c8555f2009-06-25 01:47:11 +00006564 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006565 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006566 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006567 }
drh2dca8682008-03-21 17:13:13 +00006568 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006569 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006570 if( sqlite3VdbeMemTooBig(pMem) ){
6571 goto too_big;
6572 }
drh5e00f6c2001-09-13 13:46:56 +00006573 break;
6574}
6575
dan5cf53532010-05-01 16:40:20 +00006576#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006577/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006578**
6579** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006580** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6581** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006582** SQLITE_BUSY or not, respectively. Write the number of pages in the
6583** WAL after the checkpoint into mem[P3+1] and the number of pages
6584** in the WAL that have been checkpointed after the checkpoint
6585** completes into mem[P3+2]. However on an error, mem[P3+1] and
6586** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006587*/
6588case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006589 int i; /* Loop counter */
6590 int aRes[3]; /* Results */
6591 Mem *pMem; /* Write results here */
6592
drh9e92a472013-06-27 17:40:30 +00006593 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006594 aRes[0] = 0;
6595 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006596 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6597 || pOp->p2==SQLITE_CHECKPOINT_FULL
6598 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006599 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006600 );
drh30aa3b92011-02-07 23:56:01 +00006601 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006602 if( rc ){
6603 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006604 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006605 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006606 }
drh30aa3b92011-02-07 23:56:01 +00006607 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6608 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6609 }
dan7c246102010-04-12 19:00:29 +00006610 break;
6611};
dan5cf53532010-05-01 16:40:20 +00006612#endif
drh5e00f6c2001-09-13 13:46:56 +00006613
drhcac29a62010-07-02 19:36:52 +00006614#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006615/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006616**
6617** Change the journal mode of database P1 to P3. P3 must be one of the
6618** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6619** modes (delete, truncate, persist, off and memory), this is a simple
6620** operation. No IO is required.
6621**
6622** If changing into or out of WAL mode the procedure is more complicated.
6623**
6624** Write a string containing the final journal-mode to register P2.
6625*/
drh27a348c2015-04-13 19:14:06 +00006626case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006627 Btree *pBt; /* Btree to change journal mode of */
6628 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006629 int eNew; /* New journal mode */
6630 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006631#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006632 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006633#endif
dane04dc882010-04-20 18:53:15 +00006634
drh27a348c2015-04-13 19:14:06 +00006635 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006636 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006637 assert( eNew==PAGER_JOURNALMODE_DELETE
6638 || eNew==PAGER_JOURNALMODE_TRUNCATE
6639 || eNew==PAGER_JOURNALMODE_PERSIST
6640 || eNew==PAGER_JOURNALMODE_OFF
6641 || eNew==PAGER_JOURNALMODE_MEMORY
6642 || eNew==PAGER_JOURNALMODE_WAL
6643 || eNew==PAGER_JOURNALMODE_QUERY
6644 );
6645 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006646 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006647
dane04dc882010-04-20 18:53:15 +00006648 pBt = db->aDb[pOp->p1].pBt;
6649 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006650 eOld = sqlite3PagerGetJournalMode(pPager);
6651 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6652 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006653
6654#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006655 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006656
drhd80b2332010-05-01 00:59:37 +00006657 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006658 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006659 */
6660 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006661 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006662 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006663 ){
drh0b9b4302010-06-11 17:01:24 +00006664 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006665 }
6666
drh0b9b4302010-06-11 17:01:24 +00006667 if( (eNew!=eOld)
6668 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6669 ){
danc0537fe2013-06-28 19:41:43 +00006670 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006671 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006672 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006673 "cannot change %s wal mode from within a transaction",
6674 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6675 );
drh9467abf2016-02-17 18:44:11 +00006676 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006677 }else{
6678
6679 if( eOld==PAGER_JOURNALMODE_WAL ){
6680 /* If leaving WAL mode, close the log file. If successful, the call
6681 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6682 ** file. An EXCLUSIVE lock may still be held on the database file
6683 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006684 */
dan7fb89902016-08-12 16:21:15 +00006685 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006686 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006687 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006688 }
drh242c4f72010-06-22 14:49:39 +00006689 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6690 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6691 ** as an intermediate */
6692 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006693 }
6694
6695 /* Open a transaction on the database file. Regardless of the journal
6696 ** mode, this transaction always uses a rollback journal.
6697 */
6698 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6699 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006700 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006701 }
6702 }
6703 }
dan5cf53532010-05-01 16:40:20 +00006704#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006705
drh9467abf2016-02-17 18:44:11 +00006706 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006707 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006708
dane04dc882010-04-20 18:53:15 +00006709 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006710 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006711 pOut->n = sqlite3Strlen30(pOut->z);
6712 pOut->enc = SQLITE_UTF8;
6713 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006714 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006715 break;
drhcac29a62010-07-02 19:36:52 +00006716};
6717#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006718
drhfdbcdee2007-03-27 14:44:50 +00006719#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00006720/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00006721**
drh9ef5e772016-08-19 14:20:56 +00006722** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6723** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00006724**
drh2f6239e2018-12-08 00:43:08 +00006725** If P2 is not zero, then it is a register holding a string which is
6726** the file into which the result of vacuum should be written. When
6727** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00006728*/
drh9cbf3422008-01-17 16:22:13 +00006729case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006730 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00006731 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
6732 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00006733 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006734 break;
6735}
drh154d4b22006-09-21 11:02:16 +00006736#endif
drh6f8c91c2003-12-07 00:24:35 +00006737
danielk1977dddbcdc2007-04-26 14:42:34 +00006738#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006739/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006740**
6741** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006742** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006743** P2. Otherwise, fall through to the next instruction.
6744*/
drh9cbf3422008-01-17 16:22:13 +00006745case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006746 Btree *pBt;
6747
6748 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006749 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006750 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006751 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006752 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006753 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006754 if( rc ){
6755 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006756 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006757 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006758 }
6759 break;
6760}
6761#endif
6762
drhba968db2018-07-24 22:02:12 +00006763/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006764**
drh25df48d2014-07-22 14:58:12 +00006765** Cause precompiled statements to expire. When an expired statement
6766** is executed using sqlite3_step() it will either automatically
6767** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6768** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006769**
6770** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006771** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00006772**
6773** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
6774** then running SQL statements are allowed to continue to run to completion.
6775** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
6776** that might help the statement run faster but which does not affect the
6777** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00006778*/
drh9cbf3422008-01-17 16:22:13 +00006779case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00006780 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00006781 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00006782 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00006783 }else{
drhba968db2018-07-24 22:02:12 +00006784 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00006785 }
6786 break;
6787}
6788
danielk1977c00da102006-01-07 13:21:04 +00006789#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006790/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006791** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006792**
6793** Obtain a lock on a particular table. This instruction is only used when
6794** the shared-cache feature is enabled.
6795**
danielk197796d48e92009-06-29 06:00:37 +00006796** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006797** on which the lock is acquired. A readlock is obtained if P3==0 or
6798** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006799**
6800** P2 contains the root-page of the table to lock.
6801**
drh66a51672008-01-03 00:01:23 +00006802** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006803** used to generate an error message if the lock cannot be obtained.
6804*/
drh9cbf3422008-01-17 16:22:13 +00006805case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006806 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006807 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006808 int p1 = pOp->p1;
6809 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006810 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006811 assert( isWriteLock==0 || isWriteLock==1 );
6812 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006813 if( rc ){
6814 if( (rc&0xFF)==SQLITE_LOCKED ){
6815 const char *z = pOp->p4.z;
6816 sqlite3VdbeError(p, "database table is locked: %s", z);
6817 }
6818 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006819 }
danielk1977c00da102006-01-07 13:21:04 +00006820 }
6821 break;
6822}
drhb9bb7c12006-06-11 23:41:55 +00006823#endif /* SQLITE_OMIT_SHARED_CACHE */
6824
6825#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006826/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006827**
danielk19773e3a84d2008-08-01 17:37:40 +00006828** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6829** xBegin method for that table.
6830**
6831** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006832** within a callback to a virtual table xSync() method. If it is, the error
6833** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006834*/
drh9cbf3422008-01-17 16:22:13 +00006835case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006836 VTable *pVTab;
6837 pVTab = pOp->p4.pVtab;
6838 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006839 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006840 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006841 break;
6842}
6843#endif /* SQLITE_OMIT_VIRTUALTABLE */
6844
6845#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006846/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006847**
dan73779452015-03-19 18:56:17 +00006848** P2 is a register that holds the name of a virtual table in database
6849** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006850*/
drh9cbf3422008-01-17 16:22:13 +00006851case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006852 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006853 const char *zTab; /* Name of the virtual table */
6854
dan73779452015-03-19 18:56:17 +00006855 memset(&sMem, 0, sizeof(sMem));
6856 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006857 /* Because P2 is always a static string, it is impossible for the
6858 ** sqlite3VdbeMemCopy() to fail */
6859 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6860 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006861 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006862 assert( rc==SQLITE_OK );
6863 zTab = (const char*)sqlite3_value_text(&sMem);
6864 assert( zTab || db->mallocFailed );
6865 if( zTab ){
6866 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006867 }
6868 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006869 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006870 break;
6871}
6872#endif /* SQLITE_OMIT_VIRTUALTABLE */
6873
6874#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006875/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006876**
drh66a51672008-01-03 00:01:23 +00006877** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006878** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006879*/
drh9cbf3422008-01-17 16:22:13 +00006880case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006881 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006882 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006883 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00006884 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00006885 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006886 break;
6887}
6888#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006889
drh9eff6162006-06-12 21:59:13 +00006890#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006891/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006892**
drh66a51672008-01-03 00:01:23 +00006893** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006894** P1 is a cursor number. This opcode opens a cursor to the virtual
6895** table and stores that cursor in P1.
6896*/
drh9cbf3422008-01-17 16:22:13 +00006897case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006898 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006899 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006900 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006901 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006902
drh1713afb2013-06-28 01:24:57 +00006903 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006904 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006905 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006906 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006907 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6908 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006909 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006910 }
6911 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006912 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006913 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006914 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006915
drh9467abf2016-02-17 18:44:11 +00006916 /* Initialize sqlite3_vtab_cursor base class */
6917 pVCur->pVtab = pVtab;
6918
6919 /* Initialize vdbe cursor object */
6920 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6921 if( pCur ){
6922 pCur->uc.pVCur = pVCur;
6923 pVtab->nRef++;
6924 }else{
6925 assert( db->mallocFailed );
6926 pModule->xClose(pVCur);
6927 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006928 }
drh9eff6162006-06-12 21:59:13 +00006929 break;
6930}
6931#endif /* SQLITE_OMIT_VIRTUALTABLE */
6932
6933#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006934/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006935** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006936**
6937** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6938** the filtered result set is empty.
6939**
drh66a51672008-01-03 00:01:23 +00006940** P4 is either NULL or a string that was generated by the xBestIndex
6941** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006942** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006943**
drh9eff6162006-06-12 21:59:13 +00006944** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006945** by P1. The integer query plan parameter to xFilter is stored in register
6946** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006947** xFilter method. Registers P3+2..P3+1+argc are the argc
6948** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006949** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006950**
danielk19776dbee812008-01-03 18:39:41 +00006951** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006952*/
drh9cbf3422008-01-17 16:22:13 +00006953case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006954 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006955 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006956 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006957 Mem *pQuery;
6958 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006959 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006960 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006961 VdbeCursor *pCur;
6962 int res;
6963 int i;
6964 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006965
drha6c2ed92009-11-14 23:22:23 +00006966 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006967 pArgc = &pQuery[1];
6968 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006969 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006970 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006971 assert( pCur->eCurType==CURTYPE_VTAB );
6972 pVCur = pCur->uc.pVCur;
6973 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006974 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006975
drh9cbf3422008-01-17 16:22:13 +00006976 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006977 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006978 nArg = (int)pArgc->u.i;
6979 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006980
drh644a5292006-12-20 14:53:38 +00006981 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006982 res = 0;
6983 apArg = p->apArg;
6984 for(i = 0; i<nArg; i++){
6985 apArg[i] = &pArgc[i+1];
6986 }
drhc960dcb2015-11-20 19:22:01 +00006987 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006988 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006989 if( rc ) goto abort_due_to_error;
6990 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006991 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006992 VdbeBranchTaken(res!=0,2);
6993 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006994 break;
6995}
6996#endif /* SQLITE_OMIT_VIRTUALTABLE */
6997
6998#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00006999/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007000** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007001**
drh6f390be2018-01-11 17:04:26 +00007002** Store in register P3 the value of the P2-th column of
7003** the current row of the virtual-table of cursor P1.
7004**
7005** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007006** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007007** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7008** function to return true inside the xColumn method of the virtual
7009** table implementation. The P5 column might also contain other
7010** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7011** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007012*/
7013case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007014 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007015 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007016 Mem *pDest;
7017 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007018
drhdfe88ec2008-11-03 20:55:06 +00007019 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007020 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007021 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007022 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007023 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007024 if( pCur->nullRow ){
7025 sqlite3VdbeMemSetNull(pDest);
7026 break;
7027 }
drhc960dcb2015-11-20 19:22:01 +00007028 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007029 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007030 assert( pModule->xColumn );
7031 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007032 sContext.pOut = pDest;
drh09d00b22018-09-27 20:20:01 +00007033 testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
7034 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007035 sqlite3VdbeMemSetNull(pDest);
7036 pDest->flags = MEM_Null|MEM_Zero;
7037 pDest->u.nZero = 0;
7038 }else{
7039 MemSetTypeFlag(pDest, MEM_Null);
7040 }
drhc960dcb2015-11-20 19:22:01 +00007041 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007042 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007043 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007044 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007045 rc = sContext.isError;
7046 }
drh9bd038f2014-08-27 14:14:06 +00007047 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007048 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007049 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007050
drhde4fcfd2008-01-19 23:50:26 +00007051 if( sqlite3VdbeMemTooBig(pDest) ){
7052 goto too_big;
7053 }
drh9467abf2016-02-17 18:44:11 +00007054 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007055 break;
7056}
7057#endif /* SQLITE_OMIT_VIRTUALTABLE */
7058
7059#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007060/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007061**
7062** Advance virtual table P1 to the next row in its result set and
7063** jump to instruction P2. Or, if the virtual table has reached
7064** the end of its result set, then fall through to the next instruction.
7065*/
drh9cbf3422008-01-17 16:22:13 +00007066case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007067 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007068 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007069 int res;
drh856c1032009-06-02 15:21:42 +00007070 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007071
drhc54a6172009-06-02 16:06:03 +00007072 res = 0;
drh856c1032009-06-02 15:21:42 +00007073 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007074 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007075 if( pCur->nullRow ){
7076 break;
7077 }
drhc960dcb2015-11-20 19:22:01 +00007078 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007079 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007080 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007081
drhde4fcfd2008-01-19 23:50:26 +00007082 /* Invoke the xNext() method of the module. There is no way for the
7083 ** underlying implementation to return an error if one occurs during
7084 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7085 ** data is available) and the error code returned when xColumn or
7086 ** some other method is next invoked on the save virtual table cursor.
7087 */
drhc960dcb2015-11-20 19:22:01 +00007088 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007089 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007090 if( rc ) goto abort_due_to_error;
7091 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007092 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007093 if( !res ){
7094 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007095 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007096 }
drh49afe3a2013-07-10 03:05:14 +00007097 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007098}
7099#endif /* SQLITE_OMIT_VIRTUALTABLE */
7100
danielk1977182c4ba2007-06-27 15:53:34 +00007101#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007102/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007103**
drh66a51672008-01-03 00:01:23 +00007104** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007105** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007106** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007107*/
drh9cbf3422008-01-17 16:22:13 +00007108case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007109 sqlite3_vtab *pVtab;
7110 Mem *pName;
dan34566c42018-09-20 17:21:21 +00007111 int isLegacy;
7112
7113 isLegacy = (db->flags & SQLITE_LegacyAlter);
7114 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00007115 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007116 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007117 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007118 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007119 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007120 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007121 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007122 testcase( pName->enc==SQLITE_UTF8 );
7123 testcase( pName->enc==SQLITE_UTF16BE );
7124 testcase( pName->enc==SQLITE_UTF16LE );
7125 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007126 if( rc ) goto abort_due_to_error;
7127 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00007128 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00007129 sqlite3VtabImportErrmsg(p, pVtab);
7130 p->expired = 0;
7131 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007132 break;
7133}
7134#endif
drh4cbdda92006-06-14 19:00:20 +00007135
7136#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007137/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007138** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007139**
drh66a51672008-01-03 00:01:23 +00007140** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007141** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007142** are contiguous memory cells starting at P3 to pass to the xUpdate
7143** invocation. The value in register (P3+P2-1) corresponds to the
7144** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007145**
7146** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007147** The argv[0] element (which corresponds to memory cell P3)
7148** is the rowid of a row to delete. If argv[0] is NULL then no
7149** deletion occurs. The argv[1] element is the rowid of the new
7150** row. This can be NULL to have the virtual table select the new
7151** rowid for itself. The subsequent elements in the array are
7152** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007153**
7154** If P2==1 then no insert is performed. argv[0] is the rowid of
7155** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007156**
7157** P1 is a boolean flag. If it is set to true and the xUpdate call
7158** is successful, then the value returned by sqlite3_last_insert_rowid()
7159** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007160**
7161** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7162** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007163*/
drh9cbf3422008-01-17 16:22:13 +00007164case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007165 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007166 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007167 int nArg;
7168 int i;
7169 sqlite_int64 rowid;
7170 Mem **apArg;
7171 Mem *pX;
7172
danb061d052011-04-25 18:49:57 +00007173 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7174 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7175 );
drh9e92a472013-06-27 17:40:30 +00007176 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007177 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007178 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007179 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007180 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7181 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007182 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007183 }
7184 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007185 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007186 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007187 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007188 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007189 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007190 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007191 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007192 assert( memIsValid(pX) );
7193 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007194 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007195 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007196 }
danb061d052011-04-25 18:49:57 +00007197 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007198 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007199 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007200 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007201 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007202 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007203 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007204 }
drhd91c1a12013-02-09 13:58:25 +00007205 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007206 if( pOp->p5==OE_Ignore ){
7207 rc = SQLITE_OK;
7208 }else{
7209 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7210 }
7211 }else{
7212 p->nChange++;
7213 }
drh9467abf2016-02-17 18:44:11 +00007214 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007215 }
drh4cbdda92006-06-14 19:00:20 +00007216 break;
danielk1977399918f2006-06-14 13:03:23 +00007217}
7218#endif /* SQLITE_OMIT_VIRTUALTABLE */
7219
danielk197759a93792008-05-15 17:48:20 +00007220#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7221/* Opcode: Pagecount P1 P2 * * *
7222**
7223** Write the current number of pages in database P1 to memory cell P2.
7224*/
drh27a348c2015-04-13 19:14:06 +00007225case OP_Pagecount: { /* out2 */
7226 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007227 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007228 break;
7229}
7230#endif
7231
drh60ac3f42010-11-23 18:59:27 +00007232
7233#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7234/* Opcode: MaxPgcnt P1 P2 P3 * *
7235**
7236** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007237** Do not let the maximum page count fall below the current page count and
7238** do not change the maximum page count value if P3==0.
7239**
drh60ac3f42010-11-23 18:59:27 +00007240** Store the maximum page count after the change in register P2.
7241*/
drh27a348c2015-04-13 19:14:06 +00007242case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007243 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007244 Btree *pBt;
7245
drh27a348c2015-04-13 19:14:06 +00007246 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007247 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007248 newMax = 0;
7249 if( pOp->p3 ){
7250 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007251 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007252 }
7253 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007254 break;
7255}
7256#endif
7257
drh3e34eab2017-07-19 19:48:40 +00007258/* Opcode: Function0 P1 P2 P3 P4 P5
7259** Synopsis: r[P3]=func(r[P2@P5])
7260**
7261** Invoke a user function (P4 is a pointer to a FuncDef object that
7262** defines the function) with P5 arguments taken from register P2 and
7263** successors. The result of the function is stored in register P3.
7264** Register P3 must not be one of the function inputs.
7265**
7266** P1 is a 32-bit bitmask indicating whether or not each argument to the
7267** function was determined to be constant at compile time. If the first
7268** argument was constant then bit 0 of P1 is set. This is used to determine
7269** whether meta data associated with a user function argument using the
7270** sqlite3_set_auxdata() API may be safely retained until the next
7271** invocation of this opcode.
7272**
7273** See also: Function, AggStep, AggFinal
7274*/
7275/* Opcode: Function P1 P2 P3 P4 P5
7276** Synopsis: r[P3]=func(r[P2@P5])
7277**
7278** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7279** contains a pointer to the function to be run) with P5 arguments taken
7280** from register P2 and successors. The result of the function is stored
7281** in register P3. Register P3 must not be one of the function inputs.
7282**
7283** P1 is a 32-bit bitmask indicating whether or not each argument to the
7284** function was determined to be constant at compile time. If the first
7285** argument was constant then bit 0 of P1 is set. This is used to determine
7286** whether meta data associated with a user function argument using the
7287** sqlite3_set_auxdata() API may be safely retained until the next
7288** invocation of this opcode.
7289**
7290** SQL functions are initially coded as OP_Function0 with P4 pointing
7291** to a FuncDef object. But on first evaluation, the P4 operand is
7292** automatically converted into an sqlite3_context object and the operation
7293** changed to this OP_Function opcode. In this way, the initialization of
7294** the sqlite3_context object occurs only once, rather than once for each
7295** evaluation of the function.
7296**
7297** See also: Function0, AggStep, AggFinal
7298*/
mistachkin758784d2018-07-25 15:12:29 +00007299case OP_PureFunc0: /* group */
7300case OP_Function0: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007301 int n;
7302 sqlite3_context *pCtx;
7303
7304 assert( pOp->p4type==P4_FUNCDEF );
7305 n = pOp->p5;
7306 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7307 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
7308 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
7309 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7310 if( pCtx==0 ) goto no_mem;
7311 pCtx->pOut = 0;
7312 pCtx->pFunc = pOp->p4.pFunc;
7313 pCtx->iOp = (int)(pOp - aOp);
7314 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007315 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007316 pCtx->argc = n;
7317 pOp->p4type = P4_FUNCCTX;
7318 pOp->p4.pCtx = pCtx;
7319 assert( OP_PureFunc == OP_PureFunc0+2 );
7320 assert( OP_Function == OP_Function0+2 );
7321 pOp->opcode += 2;
7322 /* Fall through into OP_Function */
7323}
mistachkin758784d2018-07-25 15:12:29 +00007324case OP_PureFunc: /* group */
7325case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007326 int i;
7327 sqlite3_context *pCtx;
7328
7329 assert( pOp->p4type==P4_FUNCCTX );
7330 pCtx = pOp->p4.pCtx;
7331
7332 /* If this function is inside of a trigger, the register array in aMem[]
7333 ** might change from one evaluation to the next. The next block of code
7334 ** checks to see if the register array has changed, and if so it
7335 ** reinitializes the relavant parts of the sqlite3_context object */
7336 pOut = &aMem[pOp->p3];
7337 if( pCtx->pOut != pOut ){
7338 pCtx->pOut = pOut;
7339 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7340 }
7341
7342 memAboutToChange(p, pOut);
7343#ifdef SQLITE_DEBUG
7344 for(i=0; i<pCtx->argc; i++){
7345 assert( memIsValid(pCtx->argv[i]) );
7346 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7347 }
7348#endif
7349 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007350 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007351 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7352
7353 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007354 if( pCtx->isError ){
7355 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007356 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7357 rc = pCtx->isError;
7358 }
7359 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007360 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007361 if( rc ) goto abort_due_to_error;
7362 }
7363
7364 /* Copy the result of the function into register P3 */
7365 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7366 sqlite3VdbeChangeEncoding(pOut, encoding);
7367 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7368 }
7369
7370 REGISTER_TRACE(pOp->p3, pOut);
7371 UPDATE_MAX_BLOBSIZE(pOut);
7372 break;
7373}
7374
drhf259df52017-12-27 20:38:35 +00007375/* Opcode: Trace P1 P2 * P4 *
7376**
7377** Write P4 on the statement trace output if statement tracing is
7378** enabled.
7379**
7380** Operand P1 must be 0x7fffffff and P2 must positive.
7381*/
drh74588ce2017-09-13 00:13:05 +00007382/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007383** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007384**
7385** Programs contain a single instance of this opcode as the very first
7386** opcode.
drh949f9cd2008-01-12 21:35:57 +00007387**
7388** If tracing is enabled (by the sqlite3_trace()) interface, then
7389** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007390** Or if P4 is blank, use the string returned by sqlite3_sql().
7391**
7392** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007393**
7394** Increment the value of P1 so that OP_Once opcodes will jump the
7395** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007396**
7397** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7398** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007399*/
drhf259df52017-12-27 20:38:35 +00007400case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007401case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007402 int i;
drhb9f47992018-01-24 12:14:43 +00007403#ifndef SQLITE_OMIT_TRACE
7404 char *zTrace;
7405#endif
drh5fe63bf2016-07-25 02:42:22 +00007406
7407 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7408 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7409 **
7410 ** This assert() provides evidence for:
7411 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7412 ** would have been returned by the legacy sqlite3_trace() interface by
7413 ** using the X argument when X begins with "--" and invoking
7414 ** sqlite3_expanded_sql(P) otherwise.
7415 */
7416 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007417
7418 /* OP_Init is always instruction 0 */
7419 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007420
drhaceb31b2014-02-08 01:40:27 +00007421#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007422 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007423 && !p->doingRerun
7424 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7425 ){
drh3d2a5292016-07-13 22:55:01 +00007426#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007427 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7428 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007429 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007430 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007431 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007432 }else
drh3d2a5292016-07-13 22:55:01 +00007433#endif
drh7adbcff2017-03-20 15:29:28 +00007434 if( db->nVdbeExec>1 ){
7435 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7436 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7437 sqlite3DbFree(db, z);
7438 }else{
drhbd441f72016-07-25 02:31:48 +00007439 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007440 }
drh949f9cd2008-01-12 21:35:57 +00007441 }
drh8f8b2312013-10-18 20:03:43 +00007442#ifdef SQLITE_USE_FCNTL_TRACE
7443 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7444 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007445 int j;
7446 for(j=0; j<db->nDb; j++){
7447 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7448 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007449 }
7450 }
7451#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007452#ifdef SQLITE_DEBUG
7453 if( (db->flags & SQLITE_SqlTrace)!=0
7454 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7455 ){
7456 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7457 }
7458#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007459#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007460 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007461 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007462 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007463 for(i=1; i<p->nOp; i++){
7464 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7465 }
7466 pOp->p1 = 0;
7467 }
7468 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007469 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007470 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007471}
drh949f9cd2008-01-12 21:35:57 +00007472
drh28935362013-12-07 20:39:19 +00007473#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007474/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007475**
7476** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007477** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7478** to values currently held in registers. TK_COLUMN terms in the P4
7479** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007480*/
7481case OP_CursorHint: {
7482 VdbeCursor *pC;
7483
7484 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7485 assert( pOp->p4type==P4_EXPR );
7486 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007487 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007488 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007489 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7490 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007491 }
drh28935362013-12-07 20:39:19 +00007492 break;
7493}
7494#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007495
drh4031baf2018-05-28 17:31:20 +00007496#ifdef SQLITE_DEBUG
7497/* Opcode: Abortable * * * * *
7498**
7499** Verify that an Abort can happen. Assert if an Abort at this point
7500** might cause database corruption. This opcode only appears in debugging
7501** builds.
7502**
7503** An Abort is safe if either there have been no writes, or if there is
7504** an active statement journal.
7505*/
7506case OP_Abortable: {
7507 sqlite3VdbeAssertAbortable(p);
7508 break;
7509}
7510#endif
7511
drh91fd4d42008-01-19 20:11:25 +00007512/* Opcode: Noop * * * * *
7513**
7514** Do nothing. This instruction is often useful as a jump
7515** destination.
drh5e00f6c2001-09-13 13:46:56 +00007516*/
drh91fd4d42008-01-19 20:11:25 +00007517/*
7518** The magic Explain opcode are only inserted when explain==2 (which
7519** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7520** This opcode records information from the optimizer. It is the
7521** the same as a no-op. This opcodesnever appears in a real VM program.
7522*/
drh4031baf2018-05-28 17:31:20 +00007523default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007524 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007525
drh5e00f6c2001-09-13 13:46:56 +00007526 break;
7527}
7528
7529/*****************************************************************************
7530** The cases of the switch statement above this line should all be indented
7531** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7532** readability. From this point on down, the normal indentation rules are
7533** restored.
7534*****************************************************************************/
7535 }
drh6e142f52000-06-08 13:36:40 +00007536
drh7b396862003-01-01 23:06:20 +00007537#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007538 {
drh35043cc2018-02-12 20:27:34 +00007539 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007540 if( endTime>start ) pOrigOp->cycles += endTime - start;
7541 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007542 }
drh7b396862003-01-01 23:06:20 +00007543#endif
7544
drh6e142f52000-06-08 13:36:40 +00007545 /* The following code adds nothing to the actual functionality
7546 ** of the program. It is only here for testing and debugging.
7547 ** On the other hand, it does burn CPU cycles every time through
7548 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7549 */
7550#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007551 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007552
drhcf1023c2007-05-08 20:59:49 +00007553#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007554 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007555 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007556 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007557 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007558 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007559 }
drh7cc84c22016-04-11 13:36:42 +00007560 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007561 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007562 }
drh75897232000-05-29 14:26:00 +00007563 }
danielk1977b5402fb2005-01-12 07:15:04 +00007564#endif /* SQLITE_DEBUG */
7565#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007566 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007567
drha05a7222008-01-19 03:35:58 +00007568 /* If we reach this point, it means that execution is finished with
7569 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007570 */
drh9467abf2016-02-17 18:44:11 +00007571abort_due_to_error:
7572 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007573 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007574 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7575 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7576 }
drha05a7222008-01-19 03:35:58 +00007577 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007578 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007579 testcase( sqlite3GlobalConfig.xLog!=0 );
7580 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007581 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007582 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007583 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007584 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007585 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007586 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007587 }
drh900b31e2007-08-28 02:27:51 +00007588
7589 /* This is the only way out of this procedure. We have to
7590 ** release the mutexes on btrees that were acquired at the
7591 ** top. */
7592vdbe_return:
drhc332e042019-02-12 21:04:33 +00007593#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00007594 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
7595 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00007596 if( db->xProgress(db->pProgressArg) ){
7597 nProgressLimit = 0xffffffff;
7598 rc = SQLITE_INTERRUPT;
7599 goto abort_due_to_error;
7600 }
7601 }
7602#endif
drh9b47ee32013-08-20 03:13:51 +00007603 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007604 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007605 assert( rc!=SQLITE_OK || nExtraDelete==0
7606 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7607 );
drhb86ccfb2003-01-28 23:13:10 +00007608 return rc;
7609
drh023ae032007-05-08 12:12:16 +00007610 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7611 ** is encountered.
7612 */
7613too_big:
drh22c17b82015-05-15 04:13:15 +00007614 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007615 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007616 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007617
drh98640a32007-06-07 19:08:32 +00007618 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007619 */
7620no_mem:
drh4a642b62016-02-05 01:55:27 +00007621 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007622 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007623 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007624 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007625
danielk19776f8a5032004-05-10 10:34:51 +00007626 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007627 ** flag.
7628 */
7629abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007630 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007631 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007632 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007633 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007634 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007635}