blob: 98df0a39772f3f4df28b926a8c88fe3254bd7e50 [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**
drh7083a482018-07-10 16:04:04 +0000125** M is an integer between 2 and 4. 2 indicates a ordinary two-way
126** branch (I=0 means fall through and I=1 means taken). 3 indicates
127** a 3-way branch where the third way is when one of the operands is
128** NULL. 4 indicates the OP_Jump instruction which has three destinations
129** depending on whether the first operand is less than, equal to, or greater
130** than the second.
drh4336b0e2014-08-05 00:53:51 +0000131**
132** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000133** generated the VDBE instruction combined with flag bits. The source
134** code line number is in the lower 24 bits of iSrcLine and the upper
135** 8 bytes are flags. The lower three bits of the flags indicate
136** values for I that should never occur. For example, if the branch is
137** always taken, the flags should be 0x05 since the fall-through and
138** alternate branch are never taken. If a branch is never taken then
139** flags should be 0x06 since only the fall-through approach is allowed.
140**
141** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
142** interested in equal or not-equal. In other words, I==0 and I==2
143** should be treated the same.
144**
145** Since only a line number is retained, not the filename, this macro
146** only works for amalgamation builds. But that is ok, since these macros
147** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000148*/
149#if !defined(SQLITE_VDBE_COVERAGE)
150# define VdbeBranchTaken(I,M)
151#else
drh5655c542014-02-19 19:14:34 +0000152# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000153 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
154 u8 mNever;
155 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
156 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
157 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
158 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
159 I = 1<<I;
160 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
161 ** the flags indicate directions that the branch can never go. If
162 ** a branch really does go in one of those directions, assert right
163 ** away. */
164 mNever = iSrcLine >> 24;
165 assert( (I & mNever)==0 );
166 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
167 I |= mNever;
168 if( M==2 ) I |= 0x04;
169 if( M==4 ){
170 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000171 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000172 }
drh7083a482018-07-10 16:04:04 +0000173 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
174 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000175 }
drh688852a2014-02-17 22:40:43 +0000176#endif
177
178/*
drh9cbf3422008-01-17 16:22:13 +0000179** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000180** already. Return non-zero if a malloc() fails.
181*/
drhb21c8cd2007-08-21 19:33:56 +0000182#define Stringify(P, enc) \
drhbd9507c2014-08-23 17:21:37 +0000183 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
drhf4479502004-05-27 03:12:53 +0000184 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000185
186/*
danielk1977bd7e4602004-05-24 07:34:48 +0000187** An ephemeral string value (signified by the MEM_Ephem flag) contains
188** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000189** is responsible for deallocating that string. Because the register
190** does not control the string, it might be deleted without the register
191** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000192**
193** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000194** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000195** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000196*/
drhb21c8cd2007-08-21 19:33:56 +0000197#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000198 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000199 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000200
dan689ab892011-08-12 15:02:00 +0000201/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000202#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000203
204/*
drhdfe88ec2008-11-03 20:55:06 +0000205** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000206** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000207*/
drhdfe88ec2008-11-03 20:55:06 +0000208static VdbeCursor *allocateCursor(
209 Vdbe *p, /* The virtual machine */
210 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000211 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000212 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000213 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000214){
215 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000216 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000217 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000218 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000219 **
220 ** * Sometimes cursor numbers are used for a couple of different
221 ** purposes in a vdbe program. The different uses might require
222 ** different sized allocations. Memory cells provide growable
223 ** allocations.
224 **
225 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
226 ** be freed lazily via the sqlite3_release_memory() API. This
227 ** minimizes the number of malloc calls made by the system.
228 **
drh3cdce922016-03-21 00:30:40 +0000229 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000230 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
231 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000232 */
drh9f6168b2016-03-19 23:32:58 +0000233 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000234
danielk19775f096132008-03-28 15:44:09 +0000235 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000236 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000237 nByte =
drh5cc10232013-11-21 01:04:02 +0000238 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000239 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000240
drh9f6168b2016-03-19 23:32:58 +0000241 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000242 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
dan97c8cb32019-01-01 18:00:17 +0000243 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
244 ** is clear. Otherwise, if this is an ephemeral cursor created by
245 ** OP_OpenDup, the cursor will not be closed and will still be part
246 ** of a BtShared.pCursor list. */
247 p->apCsr[iCur]->isEphemeral = 0;
danielk1977be718892006-06-23 08:05:19 +0000248 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000249 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000250 }
drh322f2852014-09-19 00:43:39 +0000251 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000252 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000253 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000254 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000255 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000256 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000257 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000258 if( eCurType==CURTYPE_BTREE ){
259 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000260 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000261 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000262 }
danielk197794eb6a12005-12-15 15:22:08 +0000263 }
drh4774b132004-06-12 20:12:51 +0000264 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000265}
266
danielk19773d1bfea2004-05-14 11:00:53 +0000267/*
drh29d72102006-02-09 22:13:41 +0000268** Try to convert a value into a numeric representation if we can
269** do so without loss of information. In other words, if the string
270** looks like a number, convert it into a number. If it does not
271** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000272**
273** If the bTryForInt flag is true, then extra effort is made to give
274** an integer representation. Strings that look like floating point
275** values but which have no fractional component (example: '48.00')
276** will have a MEM_Int representation when bTryForInt is true.
277**
278** If bTryForInt is false, then if the input string contains a decimal
279** point or exponential notation, the result is only MEM_Real, even
280** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000281*/
drhbd9507c2014-08-23 17:21:37 +0000282static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000283 double rValue;
284 i64 iValue;
285 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000286 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000287 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
288 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
289 pRec->u.i = iValue;
290 pRec->flags |= MEM_Int;
291 }else{
drh74eaba42014-09-18 17:52:15 +0000292 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000293 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000294 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000295 }
drh06b3bd52018-02-01 01:13:33 +0000296 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
297 ** string representation after computing a numeric equivalent, because the
298 ** string representation might not be the canonical representation for the
299 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
300 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000301}
302
303/*
drh8a512562005-11-14 22:29:05 +0000304** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000305**
drh8a512562005-11-14 22:29:05 +0000306** SQLITE_AFF_INTEGER:
307** SQLITE_AFF_REAL:
308** SQLITE_AFF_NUMERIC:
309** Try to convert pRec to an integer representation or a
310** floating-point representation if an integer representation
311** is not possible. Note that the integer representation is
312** always preferred, even if the affinity is REAL, because
313** an integer representation is more space efficient on disk.
314**
315** SQLITE_AFF_TEXT:
316** Convert pRec to a text representation.
317**
drh05883a32015-06-02 15:32:08 +0000318** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000319** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000320*/
drh17435752007-08-16 04:30:38 +0000321static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000322 Mem *pRec, /* The value to apply affinity to */
323 char affinity, /* The affinity to be applied */
324 u8 enc /* Use this text encoding */
325){
drh7ea31cc2014-09-18 14:36:00 +0000326 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000327 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
328 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000329 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000330 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000331 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000332 }else{
333 sqlite3VdbeIntegerAffinity(pRec);
334 }
drh17c40292004-07-21 02:53:29 +0000335 }
drh7ea31cc2014-09-18 14:36:00 +0000336 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000337 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000338 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000339 ** representation. It would be harmless to repeat the conversion if
340 ** there is already a string rep, but it is pointless to waste those
341 ** CPU cycles. */
342 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
343 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
344 sqlite3VdbeMemStringify(pRec, enc, 1);
345 }
danielk19773d1bfea2004-05-14 11:00:53 +0000346 }
dandde548c2015-05-19 19:44:25 +0000347 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000348 }
349}
350
danielk1977aee18ef2005-03-09 12:26:50 +0000351/*
drh29d72102006-02-09 22:13:41 +0000352** Try to convert the type of a function argument or a result column
353** into a numeric representation. Use either INTEGER or REAL whichever
354** is appropriate. But only do the conversion if it is possible without
355** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000356*/
357int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000358 int eType = sqlite3_value_type(pVal);
359 if( eType==SQLITE_TEXT ){
360 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000361 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000362 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000363 }
drh1b27b8c2014-02-10 03:21:57 +0000364 return eType;
drh29d72102006-02-09 22:13:41 +0000365}
366
367/*
danielk1977aee18ef2005-03-09 12:26:50 +0000368** Exported version of applyAffinity(). This one works on sqlite3_value*,
369** not the internal Mem* type.
370*/
danielk19771e536952007-08-16 10:09:01 +0000371void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000372 sqlite3_value *pVal,
373 u8 affinity,
374 u8 enc
375){
drhb21c8cd2007-08-21 19:33:56 +0000376 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000377}
378
drh3d1d90a2014-03-24 15:00:15 +0000379/*
drhf1a89ed2014-08-23 17:41:15 +0000380** pMem currently only holds a string type (or maybe a BLOB that we can
381** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000382** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000383** accordingly.
384*/
385static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
386 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
387 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh0814acd2019-01-25 20:09:04 +0000388 ExpandBlob(pMem);
drh74eaba42014-09-18 17:52:15 +0000389 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000390 return 0;
391 }
drh84d4f1a2017-09-20 10:47:10 +0000392 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000393 return MEM_Int;
394 }
395 return MEM_Real;
396}
397
398/*
drh3d1d90a2014-03-24 15:00:15 +0000399** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
400** none.
401**
402** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000403** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000404*/
405static u16 numericType(Mem *pMem){
406 if( pMem->flags & (MEM_Int|MEM_Real) ){
407 return pMem->flags & (MEM_Int|MEM_Real);
408 }
409 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000410 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000411 }
412 return 0;
413}
414
danielk1977b5402fb2005-01-12 07:15:04 +0000415#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000416/*
danielk1977ca6b2912004-05-21 10:49:47 +0000417** Write a nice string representation of the contents of cell pMem
418** into buffer zBuf, length nBuf.
419*/
drh74161702006-02-24 02:53:49 +0000420void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000421 char *zCsr = zBuf;
422 int f = pMem->flags;
423
drh57196282004-10-06 15:41:16 +0000424 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000425
danielk1977ca6b2912004-05-21 10:49:47 +0000426 if( f&MEM_Blob ){
427 int i;
428 char c;
429 if( f & MEM_Dyn ){
430 c = 'z';
431 assert( (f & (MEM_Static|MEM_Ephem))==0 );
432 }else if( f & MEM_Static ){
433 c = 't';
434 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
435 }else if( f & MEM_Ephem ){
436 c = 'e';
437 assert( (f & (MEM_Static|MEM_Dyn))==0 );
438 }else{
439 c = 's';
440 }
drh85c2dc02017-03-16 13:30:58 +0000441 *(zCsr++) = c;
drh5bb3eb92007-05-04 13:15:55 +0000442 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000443 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000444 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000445 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000446 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000447 }
448 for(i=0; i<16 && i<pMem->n; i++){
449 char z = pMem->z[i];
450 if( z<32 || z>126 ) *zCsr++ = '.';
451 else *zCsr++ = z;
452 }
drh85c2dc02017-03-16 13:30:58 +0000453 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000454 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000455 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000456 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000457 }
danielk1977b1bc9532004-05-22 03:05:33 +0000458 *zCsr = '\0';
459 }else if( f & MEM_Str ){
460 int j, k;
461 zBuf[0] = ' ';
462 if( f & MEM_Dyn ){
463 zBuf[1] = 'z';
464 assert( (f & (MEM_Static|MEM_Ephem))==0 );
465 }else if( f & MEM_Static ){
466 zBuf[1] = 't';
467 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
468 }else if( f & MEM_Ephem ){
469 zBuf[1] = 'e';
470 assert( (f & (MEM_Static|MEM_Dyn))==0 );
471 }else{
472 zBuf[1] = 's';
473 }
474 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000475 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000476 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000477 zBuf[k++] = '[';
478 for(j=0; j<15 && j<pMem->n; j++){
479 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000480 if( c>=0x20 && c<0x7f ){
481 zBuf[k++] = c;
482 }else{
483 zBuf[k++] = '.';
484 }
485 }
486 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000487 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000488 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000489 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000490 }
danielk1977ca6b2912004-05-21 10:49:47 +0000491}
492#endif
493
drh5b6afba2008-01-05 16:29:28 +0000494#ifdef SQLITE_DEBUG
495/*
496** Print the value of a register for tracing purposes:
497*/
drh84e55a82013-11-13 17:58:23 +0000498static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000499 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000500 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000501 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000502 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000503 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000504 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000505 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000506 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000507#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000508 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000509 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000510#endif
drh9d67afc2018-08-29 20:24:03 +0000511 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000512 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000513 }else{
514 char zBuf[200];
515 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000516 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000517 }
dan5b6c8e42016-01-30 15:46:03 +0000518 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000519}
drh84e55a82013-11-13 17:58:23 +0000520static void registerTrace(int iReg, Mem *p){
521 printf("REG[%d] = ", iReg);
522 memTracePrint(p);
523 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000524 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000525}
526#endif
527
528#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000529# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000530#else
531# define REGISTER_TRACE(R,M)
532#endif
533
danielk197784ac9d02004-05-18 09:58:06 +0000534
drh7b396862003-01-01 23:06:20 +0000535#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000536
537/*
538** hwtime.h contains inline assembler code for implementing
539** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000540*/
shane9bcbdad2008-05-29 20:22:37 +0000541#include "hwtime.h"
542
drh7b396862003-01-01 23:06:20 +0000543#endif
544
danielk1977fd7f0452008-12-17 17:30:26 +0000545#ifndef NDEBUG
546/*
547** This function is only called from within an assert() expression. It
548** checks that the sqlite3.nTransaction variable is correctly set to
549** the number of non-transaction savepoints currently in the
550** linked list starting at sqlite3.pSavepoint.
551**
552** Usage:
553**
554** assert( checkSavepointCount(db) );
555*/
556static int checkSavepointCount(sqlite3 *db){
557 int n = 0;
558 Savepoint *p;
559 for(p=db->pSavepoint; p; p=p->pNext) n++;
560 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
561 return 1;
562}
563#endif
564
drh27a348c2015-04-13 19:14:06 +0000565/*
566** Return the register of pOp->p2 after first preparing it to be
567** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000568*/
569static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
570 sqlite3VdbeMemSetNull(pOut);
571 pOut->flags = MEM_Int;
572 return pOut;
573}
drh27a348c2015-04-13 19:14:06 +0000574static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
575 Mem *pOut;
576 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000577 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000578 pOut = &p->aMem[pOp->p2];
579 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000580 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000581 return out2PrereleaseWithClear(pOut);
582 }else{
583 pOut->flags = MEM_Int;
584 return pOut;
585 }
drh27a348c2015-04-13 19:14:06 +0000586}
587
drhb9755982010-07-24 16:34:37 +0000588
589/*
drh0fd61352014-02-07 02:29:45 +0000590** Execute as much of a VDBE program as we can.
591** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000592*/
danielk19774adee202004-05-08 08:23:19 +0000593int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000594 Vdbe *p /* The VDBE */
595){
drhbbe879d2009-11-14 18:04:35 +0000596 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000597 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000598#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
599 Op *pOrigOp; /* Value of pOp at the top of the loop */
600#endif
drhb89aeb62016-01-27 15:49:32 +0000601#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000602 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000603#endif
drhb86ccfb2003-01-28 23:13:10 +0000604 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000605 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000606 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000607 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000608 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000609 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000610#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000611 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000612#endif
drha6c2ed92009-11-14 23:22:23 +0000613 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000614 Mem *pIn1 = 0; /* 1st input operand */
615 Mem *pIn2 = 0; /* 2nd input operand */
616 Mem *pIn3 = 0; /* 3rd input operand */
617 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000618#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000619 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000620#endif
drh856c1032009-06-02 15:21:42 +0000621 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000622
drhca48c902008-01-18 14:08:24 +0000623 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000624 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000625 if( p->rc==SQLITE_NOMEM ){
626 /* This happens if a malloc() inside a call to sqlite3_column_text() or
627 ** sqlite3_column_text16() failed. */
628 goto no_mem;
629 }
drhcbd8db32015-08-20 17:18:32 +0000630 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000631 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000632 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000633 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000634 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000635 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000636 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000637 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000638#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
639 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000640 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000641 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000642 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh2ab792e2017-05-30 18:34:07 +0000643 }else{
644 nProgressLimit = 0xffffffff;
drh0d1961e2013-07-25 16:27:51 +0000645 }
646#endif
drh3c23a882007-01-09 14:01:13 +0000647#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000648 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000649 if( p->pc==0
650 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
651 ){
drh3c23a882007-01-09 14:01:13 +0000652 int i;
drh84e55a82013-11-13 17:58:23 +0000653 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000654 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000655 if( p->db->flags & SQLITE_VdbeListing ){
656 printf("VDBE Program Listing:\n");
657 for(i=0; i<p->nOp; i++){
658 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
659 }
drh3c23a882007-01-09 14:01:13 +0000660 }
drh84e55a82013-11-13 17:58:23 +0000661 if( p->db->flags & SQLITE_VdbeEQP ){
662 for(i=0; i<p->nOp; i++){
663 if( aOp[i].opcode==OP_Explain ){
664 if( once ) printf("VDBE Query Plan:\n");
665 printf("%s\n", aOp[i].p4.z);
666 once = 0;
667 }
668 }
669 }
670 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000671 }
danielk19772d1d86f2008-06-20 14:59:51 +0000672 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000673#endif
drh9467abf2016-02-17 18:44:11 +0000674 for(pOp=&aOp[p->pc]; 1; pOp++){
675 /* Errors are detected by individual opcodes, with an immediate
676 ** jumps to abort_due_to_error. */
677 assert( rc==SQLITE_OK );
678
drhf56fa462015-04-13 21:39:54 +0000679 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000680#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000681 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000682#endif
drhbf159fa2013-06-25 22:01:22 +0000683 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000684#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000685 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000686#endif
drh6e142f52000-06-08 13:36:40 +0000687
danielk19778b60e0f2005-01-12 09:10:39 +0000688 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000689 */
danielk19778b60e0f2005-01-12 09:10:39 +0000690#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000691 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000692 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000693 }
drh3f7d4e42004-07-24 14:35:58 +0000694#endif
695
drh6e142f52000-06-08 13:36:40 +0000696
drhf6038712004-02-08 18:07:34 +0000697 /* Check to see if we need to simulate an interrupt. This only happens
698 ** if we have a special test build.
699 */
700#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000701 if( sqlite3_interrupt_count>0 ){
702 sqlite3_interrupt_count--;
703 if( sqlite3_interrupt_count==0 ){
704 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000705 }
706 }
707#endif
708
drh3c657212009-11-17 23:59:58 +0000709 /* Sanity checking on other operands */
710#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000711 {
712 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
713 if( (opProperty & OPFLG_IN1)!=0 ){
714 assert( pOp->p1>0 );
715 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
716 assert( memIsValid(&aMem[pOp->p1]) );
717 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
718 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
719 }
720 if( (opProperty & OPFLG_IN2)!=0 ){
721 assert( pOp->p2>0 );
722 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
723 assert( memIsValid(&aMem[pOp->p2]) );
724 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
725 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
726 }
727 if( (opProperty & OPFLG_IN3)!=0 ){
728 assert( pOp->p3>0 );
729 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
730 assert( memIsValid(&aMem[pOp->p3]) );
731 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
732 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
733 }
734 if( (opProperty & OPFLG_OUT2)!=0 ){
735 assert( pOp->p2>0 );
736 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
737 memAboutToChange(p, &aMem[pOp->p2]);
738 }
739 if( (opProperty & OPFLG_OUT3)!=0 ){
740 assert( pOp->p3>0 );
741 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
742 memAboutToChange(p, &aMem[pOp->p3]);
743 }
drh3c657212009-11-17 23:59:58 +0000744 }
745#endif
drh6dc41482015-04-16 17:31:02 +0000746#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
747 pOrigOp = pOp;
748#endif
drh93952eb2009-11-13 19:43:43 +0000749
drh75897232000-05-29 14:26:00 +0000750 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000751
drh5e00f6c2001-09-13 13:46:56 +0000752/*****************************************************************************
753** What follows is a massive switch statement where each case implements a
754** separate instruction in the virtual machine. If we follow the usual
755** indentation conventions, each case should be indented by 6 spaces. But
756** that is a lot of wasted space on the left margin. So the code within
757** the switch statement will break with convention and be flush-left. Another
758** big comment (similar to this one) will mark the point in the code where
759** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000760**
761** The formatting of each case is important. The makefile for SQLite
762** generates two C files "opcodes.h" and "opcodes.c" by scanning this
763** file looking for lines that begin with "case OP_". The opcodes.h files
764** will be filled with #defines that give unique integer values to each
765** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000766** each string is the symbolic name for the corresponding opcode. If the
767** case statement is followed by a comment of the form "/# same as ... #/"
768** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000769**
drh9cbf3422008-01-17 16:22:13 +0000770** Other keywords in the comment that follows each case are used to
771** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000772** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000773** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000774**
drhac82fcf2002-09-08 17:23:41 +0000775** Documentation about VDBE opcodes is generated by scanning this file
776** for lines of that contain "Opcode:". That line and all subsequent
777** comment lines are used in the generation of the opcode.html documentation
778** file.
779**
780** SUMMARY:
781**
782** Formatting is important to scripts that scan this file.
783** Do not deviate from the formatting style currently in use.
784**
drh5e00f6c2001-09-13 13:46:56 +0000785*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000786
drh9cbf3422008-01-17 16:22:13 +0000787/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000788**
789** An unconditional jump to address P2.
790** The next instruction executed will be
791** the one at index P2 from the beginning of
792** the program.
drhfe705102014-03-06 13:38:37 +0000793**
794** The P1 parameter is not actually used by this opcode. However, it
795** is sometimes set to 1 instead of 0 as a hint to the command-line shell
796** that this Goto is the bottom of a loop and that the lines from P2 down
797** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000798*/
drh9cbf3422008-01-17 16:22:13 +0000799case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000800jump_to_p2_and_check_for_interrupt:
801 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000802
803 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000804 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000805 ** completion. Check to see if sqlite3_interrupt() has been called
806 ** or if the progress callback needs to be invoked.
807 **
808 ** This code uses unstructured "goto" statements and does not look clean.
809 ** But that is not due to sloppy coding habits. The code is written this
810 ** way for performance, to avoid having to run the interrupt and progress
811 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
812 ** faster according to "valgrind --tool=cachegrind" */
813check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000814 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000815#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
816 /* Call the progress callback if it is configured and the required number
817 ** of VDBE ops have been executed (either since this invocation of
818 ** sqlite3VdbeExec() or since last time the progress callback was called).
819 ** If the progress callback returns non-zero, exit the virtual machine with
820 ** a return code SQLITE_ABORT.
821 */
drh2ab792e2017-05-30 18:34:07 +0000822 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000823 assert( db->nProgressOps!=0 );
824 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
825 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000826 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000827 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000828 }
drh49afe3a2013-07-10 03:05:14 +0000829 }
830#endif
831
drh5e00f6c2001-09-13 13:46:56 +0000832 break;
833}
drh75897232000-05-29 14:26:00 +0000834
drh2eb95372008-06-06 15:04:36 +0000835/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000836**
drh2eb95372008-06-06 15:04:36 +0000837** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000838** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000839*/
drhb8475df2011-12-09 16:21:19 +0000840case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000841 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000842 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000843 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000844 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000845 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000846 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000847 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000848
849 /* Most jump operations do a goto to this spot in order to update
850 ** the pOp pointer. */
851jump_to_p2:
852 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000853 break;
854}
855
drh2eb95372008-06-06 15:04:36 +0000856/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000857**
drh81cf13e2014-02-07 18:27:53 +0000858** Jump to the next instruction after the address in register P1. After
859** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000860*/
drh2eb95372008-06-06 15:04:36 +0000861case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000862 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000863 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000864 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000865 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000866 break;
867}
868
drhed71a832014-02-07 19:18:10 +0000869/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000870**
drh5dad9a32014-07-25 18:37:42 +0000871** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000872** located at address P3.
873**
drh5dad9a32014-07-25 18:37:42 +0000874** If P2!=0 then the coroutine implementation immediately follows
875** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000876** address P2.
drh5dad9a32014-07-25 18:37:42 +0000877**
878** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000879*/
880case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000881 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000882 assert( pOp->p2>=0 && pOp->p2<p->nOp );
883 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000884 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000885 assert( !VdbeMemDynamic(pOut) );
886 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000887 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000888 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000889 break;
890}
891
892/* Opcode: EndCoroutine P1 * * * *
893**
drhbc5cf382014-08-06 01:08:07 +0000894** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000895** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000896** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000897**
898** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000899*/
900case OP_EndCoroutine: { /* in1 */
901 VdbeOp *pCaller;
902 pIn1 = &aMem[pOp->p1];
903 assert( pIn1->flags==MEM_Int );
904 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
905 pCaller = &aOp[pIn1->u.i];
906 assert( pCaller->opcode==OP_Yield );
907 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000908 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000909 pIn1->flags = MEM_Undefined;
910 break;
911}
912
913/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000914**
drh5dad9a32014-07-25 18:37:42 +0000915** Swap the program counter with the value in register P1. This
916** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000917**
drh5dad9a32014-07-25 18:37:42 +0000918** If the coroutine that is launched by this instruction ends with
919** Yield or Return then continue to the next instruction. But if
920** the coroutine launched by this instruction ends with
921** EndCoroutine, then jump to P2 rather than continuing with the
922** next instruction.
923**
924** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000925*/
drh81cf13e2014-02-07 18:27:53 +0000926case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000927 int pcDest;
drh3c657212009-11-17 23:59:58 +0000928 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000929 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000930 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000931 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000932 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000933 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000934 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000935 break;
936}
937
drhf9c8ce32013-11-05 13:33:55 +0000938/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000939** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000940**
drhef8662b2011-06-20 21:47:58 +0000941** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000942** parameter P1, P2, and P4 as if this were a Halt instruction. If the
943** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000944** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000945*/
946case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000947 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +0000948#ifdef SQLITE_DEBUG
949 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
950#endif
drh5053a792009-02-20 03:02:23 +0000951 if( (pIn3->flags & MEM_Null)==0 ) break;
952 /* Fall through into OP_Halt */
953}
drhe00ee6e2008-06-20 15:24:01 +0000954
drhf9c8ce32013-11-05 13:33:55 +0000955/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000956**
drh3d4501e2008-12-04 20:40:10 +0000957** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000958** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000959**
drh92f02c32004-09-02 14:57:08 +0000960** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
961** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
962** For errors, it can be some other value. If P1!=0 then P2 will determine
963** whether or not to rollback the current transaction. Do not rollback
964** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
965** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000966** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000967**
drh66a51672008-01-03 00:01:23 +0000968** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000969**
drhf9c8ce32013-11-05 13:33:55 +0000970** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
971**
972** 0: (no change)
973** 1: NOT NULL contraint failed: P4
974** 2: UNIQUE constraint failed: P4
975** 3: CHECK constraint failed: P4
976** 4: FOREIGN KEY constraint failed: P4
977**
978** If P5 is not zero and P4 is NULL, then everything after the ":" is
979** omitted.
980**
drh9cfcf5d2002-01-29 18:41:24 +0000981** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000982** every program. So a jump past the last instruction of the program
983** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000984*/
drh9cbf3422008-01-17 16:22:13 +0000985case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +0000986 VdbeFrame *pFrame;
987 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000988
drhf56fa462015-04-13 21:39:54 +0000989 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +0000990#ifdef SQLITE_DEBUG
991 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
992#endif
dan165921a2009-08-28 18:53:45 +0000993 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000994 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000995 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000996 p->pFrame = pFrame->pParent;
997 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000998 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000999 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001000 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001001 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001002 ** currently being halted. If the p2 instruction of this OP_Halt
1003 ** instruction is set to OE_Ignore, then the sub-program is throwing
1004 ** an IGNORE exception. In this case jump to the address specified
1005 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001006 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001007 }
drhbbe879d2009-11-14 18:04:35 +00001008 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001009 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001010 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001011 break;
1012 }
drh92f02c32004-09-02 14:57:08 +00001013 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001014 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001015 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001016 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001017 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001018 if( pOp->p5 ){
1019 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1020 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001021 testcase( pOp->p5==1 );
1022 testcase( pOp->p5==2 );
1023 testcase( pOp->p5==3 );
1024 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001025 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1026 if( pOp->p4.z ){
1027 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1028 }
drhd9b7ec92013-11-06 14:05:21 +00001029 }else{
drh22c17b82015-05-15 04:13:15 +00001030 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001031 }
drh99f5de72016-04-30 02:59:15 +00001032 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001033 }
drh92f02c32004-09-02 14:57:08 +00001034 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001035 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001036 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001037 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001038 }else{
drhd91c1a12013-02-09 13:58:25 +00001039 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001040 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001041 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001042 }
drh900b31e2007-08-28 02:27:51 +00001043 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001044}
drhc61053b2000-06-04 12:58:36 +00001045
drh4c583122008-01-04 22:01:03 +00001046/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001047** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001048**
drh9cbf3422008-01-17 16:22:13 +00001049** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001050*/
drh27a348c2015-04-13 19:14:06 +00001051case OP_Integer: { /* out2 */
1052 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001053 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001054 break;
1055}
1056
drh4c583122008-01-04 22:01:03 +00001057/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001058** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001059**
drh66a51672008-01-03 00:01:23 +00001060** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001061** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001062*/
drh27a348c2015-04-13 19:14:06 +00001063case OP_Int64: { /* out2 */
1064 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001065 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001066 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001067 break;
1068}
drh4f26d6c2004-05-26 23:25:30 +00001069
drh13573c72010-01-12 17:04:07 +00001070#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001071/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001072** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001073**
drh4c583122008-01-04 22:01:03 +00001074** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001075** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001076*/
drh27a348c2015-04-13 19:14:06 +00001077case OP_Real: { /* same as TK_FLOAT, out2 */
1078 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001079 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001080 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001081 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001082 break;
1083}
drh13573c72010-01-12 17:04:07 +00001084#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001085
drh3c84ddf2008-01-09 02:15:38 +00001086/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001087** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001088**
drh66a51672008-01-03 00:01:23 +00001089** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001090** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001091** this transformation, the length of string P4 is computed and stored
1092** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001093*/
drh27a348c2015-04-13 19:14:06 +00001094case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001095 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001096 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001097 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001098 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001099
1100#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001101 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001102 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001103 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drh4c583122008-01-04 22:01:03 +00001104 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001105 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001106 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001107 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001108 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001109 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001110 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001111 }
drh66a51672008-01-03 00:01:23 +00001112 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001113 pOp->p4.z = pOut->z;
1114 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001115 }
drh2f555112016-04-30 18:10:34 +00001116 testcase( rc==SQLITE_TOOBIG );
danielk197793758c82005-01-21 08:13:14 +00001117#endif
drhbb4957f2008-03-20 14:03:29 +00001118 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001119 goto too_big;
1120 }
drh2f555112016-04-30 18:10:34 +00001121 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001122 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001123}
drhf4479502004-05-27 03:12:53 +00001124
drhf07cf6e2015-03-06 16:45:16 +00001125/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001126** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001127**
drh9cbf3422008-01-17 16:22:13 +00001128** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001129**
drh44aebff2016-05-02 10:25:42 +00001130** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001131** the datatype of the register P2 is converted to BLOB. The content is
1132** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001133** of a string, as if it had been CAST. In other words:
1134**
1135** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001136*/
drh27a348c2015-04-13 19:14:06 +00001137case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001138 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001139 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001140 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1141 pOut->z = pOp->p4.z;
1142 pOut->n = pOp->p1;
1143 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001144 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001145#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001146 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001147 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001148 pIn3 = &aMem[pOp->p3];
1149 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001150 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001151 }
drh41d2e662015-12-01 21:23:07 +00001152#endif
danielk1977c572ef72004-05-27 09:28:41 +00001153 break;
1154}
1155
drh053a1282012-09-19 21:15:46 +00001156/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001157** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001158**
drhb8475df2011-12-09 16:21:19 +00001159** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001160** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001161** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001162** set to NULL.
1163**
1164** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1165** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1166** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001167*/
drh27a348c2015-04-13 19:14:06 +00001168case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001169 int cnt;
drh053a1282012-09-19 21:15:46 +00001170 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001171 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001172 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001173 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001174 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001175 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001176#ifdef SQLITE_DEBUG
1177 pOut->uTemp = 0;
1178#endif
drhb8475df2011-12-09 16:21:19 +00001179 while( cnt>0 ){
1180 pOut++;
1181 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001182 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001183 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001184 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001185 cnt--;
1186 }
drhf0863fe2005-06-12 21:35:51 +00001187 break;
1188}
1189
drh05a86c52014-02-16 01:55:49 +00001190/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001191** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001192**
1193** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1194** instruction, but do not free any string or blob memory associated with
1195** the register, so that if the value was a string or blob that was
1196** previously copied using OP_SCopy, the copies will continue to be valid.
1197*/
1198case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001199 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001200 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001201 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001202 break;
1203}
drhf0863fe2005-06-12 21:35:51 +00001204
drha5750cf2014-02-07 13:20:31 +00001205/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001206** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001207**
drh9de221d2008-01-05 06:51:30 +00001208** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001209** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001210*/
drh27a348c2015-04-13 19:14:06 +00001211case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001212 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001213 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001214 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001215 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001216 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001217 break;
1218}
1219
drheaf52d82010-05-12 13:50:23 +00001220/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001221** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001222**
drheaf52d82010-05-12 13:50:23 +00001223** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001224**
drh0fd61352014-02-07 02:29:45 +00001225** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001226** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001227*/
drh27a348c2015-04-13 19:14:06 +00001228case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001229 Mem *pVar; /* Value being transferred */
1230
drheaf52d82010-05-12 13:50:23 +00001231 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001232 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001233 pVar = &p->aVar[pOp->p1 - 1];
1234 if( sqlite3VdbeMemTooBig(pVar) ){
1235 goto too_big;
drh023ae032007-05-08 12:12:16 +00001236 }
drh7441df72017-01-09 19:27:04 +00001237 pOut = &aMem[pOp->p2];
drheaf52d82010-05-12 13:50:23 +00001238 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1239 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001240 break;
1241}
danielk1977295ba552004-05-19 10:34:51 +00001242
drhb21e7c72008-06-22 12:37:57 +00001243/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001244** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001245**
drh079a3072014-03-19 14:10:55 +00001246** Move the P3 values in register P1..P1+P3-1 over into
1247** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001248** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001249** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1250** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001251*/
drhe1349cb2008-04-01 00:36:10 +00001252case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001253 int n; /* Number of registers left to copy */
1254 int p1; /* Register to copy from */
1255 int p2; /* Register to copy to */
1256
drhe09f43f2013-11-21 04:18:31 +00001257 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001258 p1 = pOp->p1;
1259 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001260 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001261 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001262
drha6c2ed92009-11-14 23:22:23 +00001263 pIn1 = &aMem[p1];
1264 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001265 do{
drh9f6168b2016-03-19 23:32:58 +00001266 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1267 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001268 assert( memIsValid(pIn1) );
1269 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001270 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001271#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001272 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001273 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001274 }
1275#endif
drhbd6789e2015-04-28 14:00:02 +00001276 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001277 REGISTER_TRACE(p2++, pOut);
1278 pIn1++;
1279 pOut++;
drh079a3072014-03-19 14:10:55 +00001280 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001281 break;
1282}
1283
drhe8e4af72012-09-21 00:04:28 +00001284/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001285** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001286**
drhe8e4af72012-09-21 00:04:28 +00001287** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001288**
1289** This instruction makes a deep copy of the value. A duplicate
1290** is made of any string or blob constant. See also OP_SCopy.
1291*/
drhe8e4af72012-09-21 00:04:28 +00001292case OP_Copy: {
1293 int n;
1294
1295 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001296 pIn1 = &aMem[pOp->p1];
1297 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001298 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001299 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001300 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001301 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1302 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001303#ifdef SQLITE_DEBUG
1304 pOut->pScopyFrom = 0;
1305#endif
drhe8e4af72012-09-21 00:04:28 +00001306 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1307 if( (n--)==0 ) break;
1308 pOut++;
1309 pIn1++;
1310 }
drhe1349cb2008-04-01 00:36:10 +00001311 break;
1312}
1313
drhb1fdb2a2008-01-05 04:06:03 +00001314/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001315** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001316**
drh9cbf3422008-01-17 16:22:13 +00001317** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001318**
1319** This instruction makes a shallow copy of the value. If the value
1320** is a string or blob, then the copy is only a pointer to the
1321** original and hence if the original changes so will the copy.
1322** Worse, if the original is deallocated, the copy becomes invalid.
1323** Thus the program must guarantee that the original will not change
1324** during the lifetime of the copy. Use OP_Copy to make a complete
1325** copy.
1326*/
drh26198bb2013-10-31 11:15:09 +00001327case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001328 pIn1 = &aMem[pOp->p1];
1329 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001330 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001331 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001332#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001333 pOut->pScopyFrom = pIn1;
1334 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001335#endif
drh5e00f6c2001-09-13 13:46:56 +00001336 break;
1337}
drh75897232000-05-29 14:26:00 +00001338
drhfed7ac62015-10-15 18:04:59 +00001339/* Opcode: IntCopy P1 P2 * * *
1340** Synopsis: r[P2]=r[P1]
1341**
1342** Transfer the integer value held in register P1 into register P2.
1343**
1344** This is an optimized version of SCopy that works only for integer
1345** values.
1346*/
1347case OP_IntCopy: { /* out2 */
1348 pIn1 = &aMem[pOp->p1];
1349 assert( (pIn1->flags & MEM_Int)!=0 );
1350 pOut = &aMem[pOp->p2];
1351 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1352 break;
1353}
1354
drh9cbf3422008-01-17 16:22:13 +00001355/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001356** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001357**
shane21e7feb2008-05-30 15:59:49 +00001358** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001359** results. This opcode causes the sqlite3_step() call to terminate
1360** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001361** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001362** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001363*/
drh9cbf3422008-01-17 16:22:13 +00001364case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001365 Mem *pMem;
1366 int i;
1367 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001368 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001369 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001370
drhe6400b92013-11-13 23:48:46 +00001371#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1372 /* Run the progress counter just before returning.
1373 */
1374 if( db->xProgress!=0
drh2ab792e2017-05-30 18:34:07 +00001375 && nVmStep>=nProgressLimit
drhe6400b92013-11-13 23:48:46 +00001376 && db->xProgress(db->pProgressArg)!=0
1377 ){
1378 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001379 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001380 }
1381#endif
1382
dan32b09f22009-09-23 17:29:59 +00001383 /* If this statement has violated immediate foreign key constraints, do
1384 ** not return the number of rows modified. And do not RELEASE the statement
1385 ** transaction. It needs to be rolled back. */
1386 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1387 assert( db->flags&SQLITE_CountRows );
1388 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001389 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001390 }
1391
danielk1977bd434552009-03-18 10:33:00 +00001392 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1393 ** DML statements invoke this opcode to return the number of rows
1394 ** modified to the user. This is the only way that a VM that
1395 ** opens a statement transaction may invoke this opcode.
1396 **
1397 ** In case this is such a statement, close any statement transaction
1398 ** opened by this VM before returning control to the user. This is to
1399 ** ensure that statement-transactions are always nested, not overlapping.
1400 ** If the open statement-transaction is not closed here, then the user
1401 ** may step another VM that opens its own statement transaction. This
1402 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001403 **
1404 ** The statement transaction is never a top-level transaction. Hence
1405 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001406 */
1407 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001408 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001409 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001410
drhd4e70eb2008-01-02 00:34:36 +00001411 /* Invalidate all ephemeral cursor row caches */
1412 p->cacheCtr = (p->cacheCtr + 2)|1;
1413
1414 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001415 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001416 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001417 */
drha6c2ed92009-11-14 23:22:23 +00001418 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001419 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001420 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001421 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001422 assert( (pMem[i].flags & MEM_Ephem)==0
1423 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001424 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001425 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001426 }
drh28039692008-03-17 16:54:01 +00001427 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001428
drh3d2a5292016-07-13 22:55:01 +00001429 if( db->mTrace & SQLITE_TRACE_ROW ){
1430 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1431 }
1432
drhd4e70eb2008-01-02 00:34:36 +00001433 /* Return SQLITE_ROW
1434 */
drhf56fa462015-04-13 21:39:54 +00001435 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001436 rc = SQLITE_ROW;
1437 goto vdbe_return;
1438}
1439
drh5b6afba2008-01-05 16:29:28 +00001440/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001441** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001442**
drh5b6afba2008-01-05 16:29:28 +00001443** Add the text in register P1 onto the end of the text in
1444** register P2 and store the result in register P3.
1445** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001446**
1447** P3 = P2 || P1
1448**
1449** It is illegal for P1 and P3 to be the same register. Sometimes,
1450** if P3 is the same register as P2, the implementation is able
1451** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001452*/
drh5b6afba2008-01-05 16:29:28 +00001453case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001454 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001455
drh3c657212009-11-17 23:59:58 +00001456 pIn1 = &aMem[pOp->p1];
1457 pIn2 = &aMem[pOp->p2];
1458 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001459 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001460 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001461 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001462 break;
drh5e00f6c2001-09-13 13:46:56 +00001463 }
drha0c06522009-06-17 22:50:41 +00001464 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001465 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001466 Stringify(pIn2, encoding);
1467 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001468 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001469 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001470 }
drh9c1905f2008-12-10 22:32:56 +00001471 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001472 goto no_mem;
1473 }
drhc91b2fd2014-03-01 18:13:23 +00001474 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001475 if( pOut!=pIn2 ){
1476 memcpy(pOut->z, pIn2->z, pIn2->n);
1477 }
1478 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001479 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001480 pOut->z[nByte+1] = 0;
1481 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001482 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001483 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001484 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001485 break;
1486}
drh75897232000-05-29 14:26:00 +00001487
drh3c84ddf2008-01-09 02:15:38 +00001488/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001489** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001490**
drh60a713c2008-01-21 16:22:45 +00001491** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001492** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001493** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001494*/
drh3c84ddf2008-01-09 02:15:38 +00001495/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001496** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001497**
drh3c84ddf2008-01-09 02:15:38 +00001498**
shane21e7feb2008-05-30 15:59:49 +00001499** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001500** and store the result in register P3.
1501** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001502*/
drh3c84ddf2008-01-09 02:15:38 +00001503/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001504** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001505**
drh60a713c2008-01-21 16:22:45 +00001506** Subtract the value in register P1 from the value in register P2
1507** and store the result in register P3.
1508** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001509*/
drh9cbf3422008-01-17 16:22:13 +00001510/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001511** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001512**
drh60a713c2008-01-21 16:22:45 +00001513** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001514** and store the result in register P3 (P3=P2/P1). If the value in
1515** register P1 is zero, then the result is NULL. If either input is
1516** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001517*/
drh9cbf3422008-01-17 16:22:13 +00001518/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001519** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001520**
drh40864a12013-11-15 18:58:37 +00001521** Compute the remainder after integer register P2 is divided by
1522** register P1 and store the result in register P3.
1523** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001524** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001525*/
drh5b6afba2008-01-05 16:29:28 +00001526case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1527case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1528case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1529case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1530case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001531 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001532 u16 flags; /* Combined MEM_* flags from both inputs */
1533 u16 type1; /* Numeric type of left operand */
1534 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001535 i64 iA; /* Integer value of left operand */
1536 i64 iB; /* Integer value of right operand */
1537 double rA; /* Real value of left operand */
1538 double rB; /* Real value of right operand */
1539
drh3c657212009-11-17 23:59:58 +00001540 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001541 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001542 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001543 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001544 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001545 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001546 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001547 iA = pIn1->u.i;
1548 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001549 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001550 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001551 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1552 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1553 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001554 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001555 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001556 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001557 iB /= iA;
drh75897232000-05-29 14:26:00 +00001558 break;
1559 }
drhbf4133c2001-10-13 02:59:08 +00001560 default: {
drh856c1032009-06-02 15:21:42 +00001561 if( iA==0 ) goto arithmetic_result_is_null;
1562 if( iA==-1 ) iA = 1;
1563 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001564 break;
1565 }
drh75897232000-05-29 14:26:00 +00001566 }
drh856c1032009-06-02 15:21:42 +00001567 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001568 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001569 }else if( (flags & MEM_Null)!=0 ){
1570 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001571 }else{
drhbe707b32012-12-10 22:19:14 +00001572 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001573fp_math:
drh856c1032009-06-02 15:21:42 +00001574 rA = sqlite3VdbeRealValue(pIn1);
1575 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001576 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001577 case OP_Add: rB += rA; break;
1578 case OP_Subtract: rB -= rA; break;
1579 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001580 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001581 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001582 if( rA==(double)0 ) goto arithmetic_result_is_null;
1583 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001584 break;
1585 }
drhbf4133c2001-10-13 02:59:08 +00001586 default: {
drhe3b89d22019-01-18 17:53:50 +00001587 iA = sqlite3VdbeIntValue(pIn1);
1588 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001589 if( iA==0 ) goto arithmetic_result_is_null;
1590 if( iA==-1 ) iA = 1;
1591 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001592 break;
1593 }
drh5e00f6c2001-09-13 13:46:56 +00001594 }
drhc5a7b512010-01-13 16:25:42 +00001595#ifdef SQLITE_OMIT_FLOATING_POINT
1596 pOut->u.i = rB;
1597 MemSetTypeFlag(pOut, MEM_Int);
1598#else
drh856c1032009-06-02 15:21:42 +00001599 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001600 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001601 }
drh74eaba42014-09-18 17:52:15 +00001602 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001603 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001604 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001605 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001606 }
drhc5a7b512010-01-13 16:25:42 +00001607#endif
drh5e00f6c2001-09-13 13:46:56 +00001608 }
1609 break;
1610
drha05a7222008-01-19 03:35:58 +00001611arithmetic_result_is_null:
1612 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001613 break;
1614}
1615
drh7a957892012-02-02 17:35:43 +00001616/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001617**
drhbb6783b2017-04-29 18:02:49 +00001618** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001619** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1620** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001621** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001622**
drh7a957892012-02-02 17:35:43 +00001623** If P1 is not zero, then it is a register that a subsequent min() or
1624** max() aggregate will set to 1 if the current row is not the minimum or
1625** maximum. The P1 register is initialized to 0 by this instruction.
1626**
danielk1977dc1bdc42004-06-11 10:51:27 +00001627** The interface used by the implementation of the aforementioned functions
1628** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001629** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001630*/
drh9cbf3422008-01-17 16:22:13 +00001631case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001632 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001633 if( pOp->p1 ){
1634 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1635 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001636 break;
1637}
1638
drh98757152008-01-09 23:04:12 +00001639/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001640** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001641**
drh98757152008-01-09 23:04:12 +00001642** Take the bit-wise AND of the values in register P1 and P2 and
1643** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001644** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001645*/
drh98757152008-01-09 23:04:12 +00001646/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001647** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001648**
drh98757152008-01-09 23:04:12 +00001649** Take the bit-wise OR of the values in register P1 and P2 and
1650** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001651** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001652*/
drh98757152008-01-09 23:04:12 +00001653/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001654** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001655**
drh98757152008-01-09 23:04:12 +00001656** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001657** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001658** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001659** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001660*/
drh98757152008-01-09 23:04:12 +00001661/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001662** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001663**
drh98757152008-01-09 23:04:12 +00001664** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001665** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001666** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001667** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001668*/
drh5b6afba2008-01-05 16:29:28 +00001669case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1670case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1671case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1672case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001673 i64 iA;
1674 u64 uA;
1675 i64 iB;
1676 u8 op;
drh6810ce62004-01-31 19:22:56 +00001677
drh3c657212009-11-17 23:59:58 +00001678 pIn1 = &aMem[pOp->p1];
1679 pIn2 = &aMem[pOp->p2];
1680 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001681 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001682 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001683 break;
1684 }
drh158b9cb2011-03-05 20:59:46 +00001685 iA = sqlite3VdbeIntValue(pIn2);
1686 iB = sqlite3VdbeIntValue(pIn1);
1687 op = pOp->opcode;
1688 if( op==OP_BitAnd ){
1689 iA &= iB;
1690 }else if( op==OP_BitOr ){
1691 iA |= iB;
1692 }else if( iB!=0 ){
1693 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1694
1695 /* If shifting by a negative amount, shift in the other direction */
1696 if( iB<0 ){
1697 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1698 op = 2*OP_ShiftLeft + 1 - op;
1699 iB = iB>(-64) ? -iB : 64;
1700 }
1701
1702 if( iB>=64 ){
1703 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1704 }else{
1705 memcpy(&uA, &iA, sizeof(uA));
1706 if( op==OP_ShiftLeft ){
1707 uA <<= iB;
1708 }else{
1709 uA >>= iB;
1710 /* Sign-extend on a right shift of a negative number */
1711 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1712 }
1713 memcpy(&iA, &uA, sizeof(iA));
1714 }
drhbf4133c2001-10-13 02:59:08 +00001715 }
drh158b9cb2011-03-05 20:59:46 +00001716 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001717 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001718 break;
1719}
1720
drh8558cde2008-01-05 05:20:10 +00001721/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001722** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001723**
danielk19770cdc0222008-06-26 18:04:03 +00001724** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001725** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001726**
drh8558cde2008-01-05 05:20:10 +00001727** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001728*/
drh9cbf3422008-01-17 16:22:13 +00001729case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001730 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001731 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001732 sqlite3VdbeMemIntegerify(pIn1);
1733 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001734 break;
1735}
1736
drh9cbf3422008-01-17 16:22:13 +00001737/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001738**
drh9cbf3422008-01-17 16:22:13 +00001739** Force the value in register P1 to be an integer. If the value
1740** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001741** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001742** raise an SQLITE_MISMATCH exception.
1743*/
drh9cbf3422008-01-17 16:22:13 +00001744case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001745 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001746 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001747 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001748 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001749 if( (pIn1->flags & MEM_Int)==0 ){
1750 if( pOp->p2==0 ){
1751 rc = SQLITE_MISMATCH;
1752 goto abort_due_to_error;
1753 }else{
drhf56fa462015-04-13 21:39:54 +00001754 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001755 }
drh8aff1012001-12-22 14:49:24 +00001756 }
drh8aff1012001-12-22 14:49:24 +00001757 }
drh83b301b2013-11-20 00:59:02 +00001758 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001759 break;
1760}
1761
drh13573c72010-01-12 17:04:07 +00001762#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001763/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001764**
drh2133d822008-01-03 18:44:59 +00001765** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001766**
drh8a512562005-11-14 22:29:05 +00001767** This opcode is used when extracting information from a column that
1768** has REAL affinity. Such column values may still be stored as
1769** integers, for space efficiency, but after extraction we want them
1770** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001771*/
drh9cbf3422008-01-17 16:22:13 +00001772case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001773 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001774 if( pIn1->flags & MEM_Int ){
1775 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001776 }
drh487e2622005-06-25 18:42:14 +00001777 break;
1778}
drh13573c72010-01-12 17:04:07 +00001779#endif
drh487e2622005-06-25 18:42:14 +00001780
drh8df447f2005-11-01 15:48:24 +00001781#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001782/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001783** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001784**
drh4169e432014-08-25 20:11:52 +00001785** Force the value in register P1 to be the type defined by P2.
1786**
1787** <ul>
drhbb6783b2017-04-29 18:02:49 +00001788** <li> P2=='A' &rarr; BLOB
1789** <li> P2=='B' &rarr; TEXT
1790** <li> P2=='C' &rarr; NUMERIC
1791** <li> P2=='D' &rarr; INTEGER
1792** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001793** </ul>
drh487e2622005-06-25 18:42:14 +00001794**
1795** A NULL value is not changed by this routine. It remains NULL.
1796*/
drh4169e432014-08-25 20:11:52 +00001797case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001798 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001799 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001800 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001801 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1802 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1803 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001804 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001805 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001806 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001807 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001808 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001809 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001810 break;
1811}
drh8a512562005-11-14 22:29:05 +00001812#endif /* SQLITE_OMIT_CAST */
1813
drh79752b62016-08-13 10:02:17 +00001814/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001815** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001816**
1817** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1818** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1819** store the result of comparison in register P2.
1820**
1821** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1822** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1823** to coerce both inputs according to this affinity before the
1824** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1825** affinity is used. Note that the affinity conversions are stored
1826** back into the input registers P1 and P3. So this opcode can cause
1827** persistent changes to registers P1 and P3.
1828**
1829** Once any conversions have taken place, and neither value is NULL,
1830** the values are compared. If both values are blobs then memcmp() is
1831** used to determine the results of the comparison. If both values
1832** are text, then the appropriate collating function specified in
1833** P4 is used to do the comparison. If P4 is not specified then
1834** memcmp() is used to compare text string. If both values are
1835** numeric, then a numeric comparison is used. If the two values
1836** are of different types, then numbers are considered less than
1837** strings and strings are considered less than blobs.
1838**
1839** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1840** true or false and is never NULL. If both operands are NULL then the result
1841** of comparison is true. If either operand is NULL then the result is false.
1842** If neither operand is NULL the result is the same as it would be if
1843** the SQLITE_NULLEQ flag were omitted from P5.
1844**
1845** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001846** content of r[P2] is only changed if the new value is NULL or 0 (false).
1847** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001848*/
1849/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001850** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001851**
1852** This works just like the Eq opcode except that the jump is taken if
1853** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1854** additional information.
1855**
1856** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001857** content of r[P2] is only changed if the new value is NULL or 1 (true).
1858** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001859*/
drh35573352008-01-08 23:54:25 +00001860/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001861** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001862**
drh35573352008-01-08 23:54:25 +00001863** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001864** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1865** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001866**
drh35573352008-01-08 23:54:25 +00001867** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001868** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001869** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001870**
drh35573352008-01-08 23:54:25 +00001871** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001872** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001873** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001874** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001875** affinity is used. Note that the affinity conversions are stored
1876** back into the input registers P1 and P3. So this opcode can cause
1877** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001878**
1879** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001880** the values are compared. If both values are blobs then memcmp() is
1881** used to determine the results of the comparison. If both values
1882** are text, then the appropriate collating function specified in
1883** P4 is used to do the comparison. If P4 is not specified then
1884** memcmp() is used to compare text string. If both values are
1885** numeric, then a numeric comparison is used. If the two values
1886** are of different types, then numbers are considered less than
1887** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001888*/
drh9cbf3422008-01-17 16:22:13 +00001889/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001890** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001891**
drh35573352008-01-08 23:54:25 +00001892** This works just like the Lt opcode except that the jump is taken if
1893** the content of register P3 is less than or equal to the content of
1894** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001895*/
drh9cbf3422008-01-17 16:22:13 +00001896/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001897** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001898**
drh35573352008-01-08 23:54:25 +00001899** This works just like the Lt opcode except that the jump is taken if
1900** the content of register P3 is greater than the content of
1901** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001902*/
drh9cbf3422008-01-17 16:22:13 +00001903/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001904** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001905**
drh35573352008-01-08 23:54:25 +00001906** This works just like the Lt opcode except that the jump is taken if
1907** the content of register P3 is greater than or equal to the content of
1908** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001909*/
drh9cbf3422008-01-17 16:22:13 +00001910case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1911case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1912case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1913case OP_Le: /* same as TK_LE, jump, in1, in3 */
1914case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1915case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001916 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001917 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001918 u16 flags1; /* Copy of initial value of pIn1->flags */
1919 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001920
drh3c657212009-11-17 23:59:58 +00001921 pIn1 = &aMem[pOp->p1];
1922 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001923 flags1 = pIn1->flags;
1924 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001925 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001926 /* One or both operands are NULL */
1927 if( pOp->p5 & SQLITE_NULLEQ ){
1928 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1929 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1930 ** or not both operands are null.
1931 */
1932 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001933 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00001934 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
1935 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00001936 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001937 && (flags3&MEM_Cleared)==0
1938 ){
drh4910a762016-09-03 01:46:15 +00001939 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00001940 }else{
drh4910a762016-09-03 01:46:15 +00001941 res = 1; /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00001942 }
drh6a2fe092009-09-23 02:29:36 +00001943 }else{
1944 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1945 ** then the result is always NULL.
1946 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1947 */
drh688852a2014-02-17 22:40:43 +00001948 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001949 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001950 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00001951 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001952 MemSetTypeFlag(pOut, MEM_Null);
1953 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001954 }else{
drhf4345e42014-02-18 11:31:59 +00001955 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001956 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001957 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001958 }
drh6a2fe092009-09-23 02:29:36 +00001959 }
1960 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001961 }
drh6a2fe092009-09-23 02:29:36 +00001962 }else{
1963 /* Neither operand is NULL. Do a comparison. */
1964 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001965 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00001966 if( (flags1 | flags3)&MEM_Str ){
1967 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1968 applyNumericAffinity(pIn1,0);
drh24846bc2018-08-06 01:21:53 +00001969 assert( flags3==pIn3->flags );
drhcfdeeeb2018-08-04 20:12:10 +00001970 /* testcase( flags3!=pIn3->flags );
1971 ** this used to be possible with pIn1==pIn3, but not since
1972 ** the column cache was removed. The following assignment
drh24846bc2018-08-06 01:21:53 +00001973 ** is essentially a no-op. But, it provides defense-in-depth
drhcfdeeeb2018-08-04 20:12:10 +00001974 ** in case our analysis is incorrect, so it is left in. */
drh4b37cd42016-06-25 11:43:47 +00001975 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00001976 }
1977 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1978 applyNumericAffinity(pIn3,0);
1979 }
drh24a09622014-09-18 16:28:59 +00001980 }
drh64caee42016-09-09 19:33:00 +00001981 /* Handle the common case of integer comparison here, as an
1982 ** optimization, to avoid a call to sqlite3MemCompare() */
1983 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
1984 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
1985 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
1986 res = 0;
1987 goto compare_op;
1988 }
drh24a09622014-09-18 16:28:59 +00001989 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00001990 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001991 testcase( pIn1->flags & MEM_Int );
1992 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001993 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001994 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
1995 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00001996 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00001997 }
drhe5520e22015-12-31 04:34:26 +00001998 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001999 testcase( pIn3->flags & MEM_Int );
2000 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002001 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002002 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2003 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002004 }
drh6a2fe092009-09-23 02:29:36 +00002005 }
drh6a2fe092009-09-23 02:29:36 +00002006 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002007 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002008 }
drh64caee42016-09-09 19:33:00 +00002009compare_op:
drh58596362017-08-03 00:29:23 +00002010 /* At this point, res is negative, zero, or positive if reg[P1] is
2011 ** less than, equal to, or greater than reg[P3], respectively. Compute
2012 ** the answer to this operator in res2, depending on what the comparison
2013 ** operator actually is. The next block of code depends on the fact
2014 ** that the 6 comparison operators are consecutive integers in this
2015 ** order: NE, EQ, GT, LE, LT, GE */
2016 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2017 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2018 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2019 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2020 res2 = aLTb[pOp->opcode - OP_Ne];
2021 }else if( res==0 ){
2022 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2023 res2 = aEQb[pOp->opcode - OP_Ne];
2024 }else{
2025 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2026 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002027 }
2028
drhf56fa462015-04-13 21:39:54 +00002029 /* Undo any changes made by applyAffinity() to the input registers. */
2030 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2031 pIn1->flags = flags1;
2032 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2033 pIn3->flags = flags3;
2034
drh35573352008-01-08 23:54:25 +00002035 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002036 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002037 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002038 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002039 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002040 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2041 ** is only used in contexts where either:
2042 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2043 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2044 ** Therefore it is not necessary to check the content of r[P2] for
2045 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002046 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002047 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002048 testcase( res2==0 && pOp->opcode==OP_Eq );
2049 testcase( res2==1 && pOp->opcode==OP_Eq );
2050 testcase( res2==0 && pOp->opcode==OP_Ne );
2051 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002052 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002053 }
drh2b4ded92010-09-27 21:09:31 +00002054 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002055 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002056 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002057 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002058 }else{
drhf4345e42014-02-18 11:31:59 +00002059 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002060 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002061 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002062 }
danielk1977a37cdde2004-05-16 11:15:36 +00002063 }
2064 break;
2065}
drhc9b84a12002-06-20 11:36:48 +00002066
drh79752b62016-08-13 10:02:17 +00002067/* Opcode: ElseNotEq * P2 * * *
2068**
drhfd7459e2016-09-17 17:39:01 +00002069** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2070** If result of an OP_Eq comparison on the same two operands
2071** would have be NULL or false (0), then then jump to P2.
2072** If the result of an OP_Eq comparison on the two previous operands
2073** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002074*/
2075case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2076 assert( pOp>aOp );
2077 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002078 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002079 VdbeBranchTaken(iCompare!=0, 2);
2080 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002081 break;
2082}
2083
2084
drh0acb7e42008-06-25 00:12:41 +00002085/* Opcode: Permutation * * * P4 *
2086**
drhb7dab702017-01-26 18:00:00 +00002087** Set the permutation used by the OP_Compare operator in the next
2088** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002089**
drh953f7612012-12-07 22:18:54 +00002090** The permutation is only valid until the next OP_Compare that has
2091** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2092** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002093**
2094** The first integer in the P4 integer array is the length of the array
2095** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002096*/
2097case OP_Permutation: {
2098 assert( pOp->p4type==P4_INTARRAY );
2099 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002100 assert( pOp[1].opcode==OP_Compare );
2101 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002102 break;
2103}
2104
drh953f7612012-12-07 22:18:54 +00002105/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002106** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002107**
drh710c4842010-08-30 01:17:20 +00002108** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2109** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002110** the comparison for use by the next OP_Jump instruct.
2111**
drh0ca10df2012-12-08 13:26:23 +00002112** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2113** determined by the most recent OP_Permutation operator. If the
2114** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2115** order.
2116**
drh0acb7e42008-06-25 00:12:41 +00002117** P4 is a KeyInfo structure that defines collating sequences and sort
2118** orders for the comparison. The permutation applies to registers
2119** only. The KeyInfo elements are used sequentially.
2120**
2121** The comparison is a sort comparison, so NULLs compare equal,
2122** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002123** and strings are less than blobs.
2124*/
2125case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002126 int n;
2127 int i;
2128 int p1;
2129 int p2;
2130 const KeyInfo *pKeyInfo;
2131 int idx;
2132 CollSeq *pColl; /* Collating sequence to use on this term */
2133 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002134 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002135
drhb7dab702017-01-26 18:00:00 +00002136 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2137 aPermute = 0;
2138 }else{
2139 assert( pOp>aOp );
2140 assert( pOp[-1].opcode==OP_Permutation );
2141 assert( pOp[-1].p4type==P4_INTARRAY );
2142 aPermute = pOp[-1].p4.ai + 1;
2143 assert( aPermute!=0 );
2144 }
drh856c1032009-06-02 15:21:42 +00002145 n = pOp->p3;
2146 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002147 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002148 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002149 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002150 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002151#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002152 if( aPermute ){
2153 int k, mx = 0;
2154 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002155 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2156 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002157 }else{
drh9f6168b2016-03-19 23:32:58 +00002158 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2159 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002160 }
2161#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002162 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002163 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002164 assert( memIsValid(&aMem[p1+idx]) );
2165 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002166 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2167 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002168 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002169 pColl = pKeyInfo->aColl[i];
2170 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002171 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002172 if( iCompare ){
2173 if( bRev ) iCompare = -iCompare;
2174 break;
2175 }
drh16ee60f2008-06-20 18:13:25 +00002176 }
2177 break;
2178}
2179
2180/* Opcode: Jump P1 P2 P3 * *
2181**
2182** Jump to the instruction at address P1, P2, or P3 depending on whether
2183** in the most recent OP_Compare instruction the P1 vector was less than
2184** equal to, or greater than the P2 vector, respectively.
2185*/
drh0acb7e42008-06-25 00:12:41 +00002186case OP_Jump: { /* jump */
2187 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002188 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002189 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002190 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002191 }else{
drh7083a482018-07-10 16:04:04 +00002192 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002193 }
2194 break;
2195}
2196
drh5b6afba2008-01-05 16:29:28 +00002197/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002198** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002199**
drh5b6afba2008-01-05 16:29:28 +00002200** Take the logical AND of the values in registers P1 and P2 and
2201** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002202**
drh5b6afba2008-01-05 16:29:28 +00002203** If either P1 or P2 is 0 (false) then the result is 0 even if
2204** the other input is NULL. A NULL and true or two NULLs give
2205** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002206*/
drh5b6afba2008-01-05 16:29:28 +00002207/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002208** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002209**
2210** Take the logical OR of the values in register P1 and P2 and
2211** store the answer in register P3.
2212**
2213** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2214** even if the other input is NULL. A NULL and false or two NULLs
2215** give a NULL output.
2216*/
2217case OP_And: /* same as TK_AND, in1, in2, out3 */
2218case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002219 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2220 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002221
drh1fcfa722018-02-26 15:27:31 +00002222 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2223 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002224 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002225 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002226 v1 = and_logic[v1*3+v2];
2227 }else{
drh5b6afba2008-01-05 16:29:28 +00002228 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002229 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002230 }
drh3c657212009-11-17 23:59:58 +00002231 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002232 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002233 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002234 }else{
drh5b6afba2008-01-05 16:29:28 +00002235 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002236 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002237 }
drh5e00f6c2001-09-13 13:46:56 +00002238 break;
2239}
2240
drh8abed7b2018-02-26 18:49:05 +00002241/* Opcode: IsTrue P1 P2 P3 P4 *
2242** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2243**
2244** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2245** IS NOT FALSE operators.
2246**
drh96acafb2018-02-27 14:49:25 +00002247** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002248** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2249** NULL, then the P3 is stored in register P2. Invert the answer if P4
2250** is 1.
2251**
2252** The logic is summarized like this:
2253**
2254** <ul>
drh96acafb2018-02-27 14:49:25 +00002255** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2256** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2257** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2258** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002259** </ul>
2260*/
2261case OP_IsTrue: { /* in1, out2 */
2262 assert( pOp->p4type==P4_INT32 );
2263 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002264 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002265 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2266 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2267 break;
2268}
2269
drhe99fa2a2008-12-15 15:27:51 +00002270/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002271** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002272**
drhe99fa2a2008-12-15 15:27:51 +00002273** Interpret the value in register P1 as a boolean value. Store the
2274** boolean complement in register P2. If the value in register P1 is
2275** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002276*/
drh93952eb2009-11-13 19:43:43 +00002277case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002278 pIn1 = &aMem[pOp->p1];
2279 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002280 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002281 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002282 }else{
2283 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002284 }
drh5e00f6c2001-09-13 13:46:56 +00002285 break;
2286}
2287
drhe99fa2a2008-12-15 15:27:51 +00002288/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002289** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002290**
drhe99fa2a2008-12-15 15:27:51 +00002291** Interpret the content of register P1 as an integer. Store the
2292** ones-complement of the P1 value into register P2. If P1 holds
2293** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002294*/
drh93952eb2009-11-13 19:43:43 +00002295case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002296 pIn1 = &aMem[pOp->p1];
2297 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002298 sqlite3VdbeMemSetNull(pOut);
2299 if( (pIn1->flags & MEM_Null)==0 ){
2300 pOut->flags = MEM_Int;
2301 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002302 }
drhbf4133c2001-10-13 02:59:08 +00002303 break;
2304}
2305
drh48f2d3b2011-09-16 01:34:43 +00002306/* Opcode: Once P1 P2 * * *
2307**
drhab087d42017-03-24 17:59:56 +00002308** Fall through to the next instruction the first time this opcode is
2309** encountered on each invocation of the byte-code program. Jump to P2
2310** on the second and all subsequent encounters during the same invocation.
2311**
2312** Top-level programs determine first invocation by comparing the P1
2313** operand against the P1 operand on the OP_Init opcode at the beginning
2314** of the program. If the P1 values differ, then fall through and make
2315** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2316** the same then take the jump.
2317**
2318** For subprograms, there is a bitmask in the VdbeFrame that determines
2319** whether or not the jump should be taken. The bitmask is necessary
2320** because the self-altering code trick does not work for recursive
2321** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002322*/
dan1d8cb212011-12-09 13:24:16 +00002323case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002324 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002325 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002326 if( p->pFrame ){
2327 iAddr = (int)(pOp - p->aOp);
2328 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2329 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002330 goto jump_to_p2;
2331 }
drh18333ef2017-03-24 18:38:41 +00002332 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002333 }else{
drhab087d42017-03-24 17:59:56 +00002334 if( p->aOp[0].p1==pOp->p1 ){
2335 VdbeBranchTaken(1, 2);
2336 goto jump_to_p2;
2337 }
dan1d8cb212011-12-09 13:24:16 +00002338 }
drhab087d42017-03-24 17:59:56 +00002339 VdbeBranchTaken(0, 2);
2340 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002341 break;
2342}
2343
drh3c84ddf2008-01-09 02:15:38 +00002344/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002345**
drhef8662b2011-06-20 21:47:58 +00002346** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002347** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002348** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002349*/
drh1fcfa722018-02-26 15:27:31 +00002350case OP_If: { /* jump, in1 */
2351 int c;
2352 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2353 VdbeBranchTaken(c!=0, 2);
2354 if( c ) goto jump_to_p2;
2355 break;
2356}
2357
drh3c84ddf2008-01-09 02:15:38 +00002358/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002359**
drhef8662b2011-06-20 21:47:58 +00002360** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002361** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002362** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002363*/
drh9cbf3422008-01-17 16:22:13 +00002364case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002365 int c;
drh1fcfa722018-02-26 15:27:31 +00002366 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002367 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002368 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002369 break;
2370}
2371
drh830ecf92009-06-18 00:41:55 +00002372/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002373** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002374**
drh830ecf92009-06-18 00:41:55 +00002375** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002376*/
drh9cbf3422008-01-17 16:22:13 +00002377case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002378 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002379 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002380 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002381 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002382 }
drh477df4b2008-01-05 18:48:24 +00002383 break;
2384}
2385
drh98757152008-01-09 23:04:12 +00002386/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002387** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002388**
drh6a288a32008-01-07 19:20:24 +00002389** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002390*/
drh9cbf3422008-01-17 16:22:13 +00002391case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002392 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002393 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002394 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002395 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002396 }
drh5e00f6c2001-09-13 13:46:56 +00002397 break;
2398}
2399
drh31d6fd52017-04-14 19:03:10 +00002400/* Opcode: IfNullRow P1 P2 P3 * *
2401** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2402**
2403** Check the cursor P1 to see if it is currently pointing at a NULL row.
2404** If it is, then set register P3 to NULL and jump immediately to P2.
2405** If P1 is not on a NULL row, then fall through without making any
2406** changes.
2407*/
2408case OP_IfNullRow: { /* jump */
2409 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002410 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002411 if( p->apCsr[pOp->p1]->nullRow ){
2412 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2413 goto jump_to_p2;
2414 }
2415 break;
2416}
2417
drh092457b2017-12-29 15:04:49 +00002418#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2419/* Opcode: Offset P1 P2 P3 * *
2420** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002421**
drh092457b2017-12-29 15:04:49 +00002422** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002423** start of the payload for the record at which that cursor P1 is currently
2424** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002425**
drh092457b2017-12-29 15:04:49 +00002426** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002427** This opcode does not use P2 itself, but the P2 value is used by the
2428** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002429** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002430**
2431** This opcode is only available if SQLite is compiled with the
2432** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002433*/
drh092457b2017-12-29 15:04:49 +00002434case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002435 VdbeCursor *pC; /* The VDBE cursor */
2436 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2437 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002438 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002439 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002440 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002441 }else{
drh092457b2017-12-29 15:04:49 +00002442 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002443 }
2444 break;
2445}
drh092457b2017-12-29 15:04:49 +00002446#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002447
drh3e9ca092009-09-08 01:14:48 +00002448/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002449** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002450**
danielk1977cfcdaef2004-05-12 07:33:33 +00002451** Interpret the data that cursor P1 points to as a structure built using
2452** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002453** information about the format of the data.) Extract the P2-th column
2454** from this record. If there are less that (P2+1)
2455** values in the record, extract a NULL.
2456**
drh9cbf3422008-01-17 16:22:13 +00002457** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002458**
drh1cc3a362017-04-03 13:17:31 +00002459** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002460** if the P4 argument is a P4_MEM use the value of the P4 argument as
2461** the result.
drh3e9ca092009-09-08 01:14:48 +00002462**
2463** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2464** then the cache of the cursor is reset prior to extracting the column.
2465** The first OP_Column against a pseudo-table after the value of the content
2466** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002467**
drh1cc3a362017-04-03 13:17:31 +00002468** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002469** the result is guaranteed to only be used as the argument of a length()
2470** or typeof() function, respectively. The loading of large blobs can be
2471** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002472*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002473case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002474 int p2; /* column number to retrieve */
2475 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002476 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002477 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002478 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002479 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002480 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002481 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002482 const u8 *zData; /* Part of the record being decoded */
2483 const u8 *zHdr; /* Next unparsed byte of the header */
2484 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002485 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002486 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002487 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002488
dande892d92016-01-29 19:29:45 +00002489 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002490 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002491
drh170ad682017-06-02 15:44:22 +00002492 /* If the cursor cache is stale (meaning it is not currently point at
2493 ** the correct row) then bring it up-to-date by doing the necessary
2494 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002495 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002496 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002497
drh9f6168b2016-03-19 23:32:58 +00002498 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002499 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002500 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002501 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002502 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002503 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002504 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002505 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002506 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2507 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002508
drha43a02e2016-05-19 17:51:19 +00002509 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002510 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002511 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002512 /* For the special case of as pseudo-cursor, the seekResult field
2513 ** identifies the register that holds the record */
2514 assert( pC->seekResult>0 );
2515 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002516 assert( pReg->flags & MEM_Blob );
2517 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002518 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002519 pC->aRow = (u8*)pReg->z;
2520 }else{
drh6b5631e2014-11-05 15:57:39 +00002521 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002522 goto op_column_out;
2523 }
danielk1977192ac1d2004-05-10 07:17:30 +00002524 }else{
drh06a09a82016-11-25 17:03:03 +00002525 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002526 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002527 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002528 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2529 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002530 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2531 assert( pC->szRow<=pC->payloadSize );
2532 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2533 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002534 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002535 }
danielk1977192ac1d2004-05-10 07:17:30 +00002536 }
drhb73857f2006-03-17 00:25:59 +00002537 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002538 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002539 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002540
drhc81aa2e2014-10-11 23:31:52 +00002541
drh1f613c42017-08-16 14:16:19 +00002542 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002543 /* pC->aRow does not have to hold the entire row, but it does at least
2544 ** need to cover the header of the record. If pC->aRow does not contain
2545 ** the complete header, then set it to zero, forcing the header to be
2546 ** dynamically allocated. */
2547 pC->aRow = 0;
2548 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002549
2550 /* Make sure a corrupt database has not given us an oversize header.
2551 ** Do this now to avoid an oversize memory allocation.
2552 **
2553 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2554 ** types use so much data space that there can only be 4096 and 32 of
2555 ** them, respectively. So the maximum header length results from a
2556 ** 3-byte type for each of the maximum of 32768 columns plus three
2557 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2558 */
drh1f613c42017-08-16 14:16:19 +00002559 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002560 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002561 }
drh95b225a2017-08-16 11:04:22 +00002562 }else{
2563 /* This is an optimization. By skipping over the first few tests
2564 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2565 ** measurable performance gain.
2566 **
drh1f613c42017-08-16 14:16:19 +00002567 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002568 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002569 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002570 ** branch jumps to reads past the end of the record, but never more
2571 ** than a few bytes. Even if the record occurs at the end of the page
2572 ** content area, the "page header" comes after the page content and so
2573 ** this overread is harmless. Similar overreads can occur for a corrupt
2574 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002575 */
2576 zData = pC->aRow;
2577 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002578 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002579 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002580 }
drh399af1d2013-11-20 17:25:55 +00002581 }
drh35cd6432009-06-05 14:17:21 +00002582
drh399af1d2013-11-20 17:25:55 +00002583 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002584 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002585 */
drhc8606e42013-11-20 19:28:03 +00002586 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002587 /* If there is more header available for parsing in the record, try
2588 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002589 */
drhc8606e42013-11-20 19:28:03 +00002590 if( pC->iHdrOffset<aOffset[0] ){
2591 /* Make sure zData points to enough of the record to cover the header. */
2592 if( pC->aRow==0 ){
2593 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002594 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002595 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002596 zData = (u8*)sMem.z;
2597 }else{
2598 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002599 }
drhc8606e42013-11-20 19:28:03 +00002600
drh0c8f7602014-09-19 16:56:45 +00002601 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002602 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002603 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002604 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002605 zHdr = zData + pC->iHdrOffset;
2606 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002607 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002608 do{
drh95fa6062015-10-16 13:50:08 +00002609 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002610 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002611 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002612 }else{
drhc8606e42013-11-20 19:28:03 +00002613 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002614 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002615 }
drhfaf37272015-10-16 14:23:42 +00002616 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002617 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002618 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002619
drh8dd83622014-10-13 23:39:02 +00002620 /* The record is corrupt if any of the following are true:
2621 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002622 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002623 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002624 */
drhc6ce38832015-10-15 21:30:24 +00002625 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2626 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002627 ){
drh95b225a2017-08-16 11:04:22 +00002628 if( aOffset[0]==0 ){
2629 i = 0;
2630 zHdr = zEndHdr;
2631 }else{
2632 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002633 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002634 }
danielk1977dedf45b2006-01-13 17:12:01 +00002635 }
drhddb2b4a2016-03-25 12:10:32 +00002636
drh170c2762016-05-20 21:40:11 +00002637 pC->nHdrParsed = i;
2638 pC->iHdrOffset = (u32)(zHdr - zData);
2639 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002640 }else{
drh9fbc8852016-01-04 03:48:46 +00002641 t = 0;
drh9188b382004-05-14 21:12:22 +00002642 }
drhd3194f52004-05-27 19:59:32 +00002643
drhf2db3382015-04-30 20:33:25 +00002644 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002645 ** still not up to p2, that means that the record has fewer than p2
2646 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002647 */
drhc8606e42013-11-20 19:28:03 +00002648 if( pC->nHdrParsed<=p2 ){
2649 if( pOp->p4type==P4_MEM ){
2650 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2651 }else{
drh22e8d832014-10-29 00:58:38 +00002652 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002653 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002654 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002655 }
drh95fa6062015-10-16 13:50:08 +00002656 }else{
2657 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002658 }
danielk1977192ac1d2004-05-10 07:17:30 +00002659
drh380d6852013-11-20 20:58:00 +00002660 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002661 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002662 ** all valid.
drh9188b382004-05-14 21:12:22 +00002663 */
drhc8606e42013-11-20 19:28:03 +00002664 assert( p2<pC->nHdrParsed );
2665 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002666 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002667 if( VdbeMemDynamic(pDest) ){
2668 sqlite3VdbeMemSetNull(pDest);
2669 }
drh95fa6062015-10-16 13:50:08 +00002670 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002671 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002672 /* This is the common case where the desired content fits on the original
2673 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002674 zData = pC->aRow + aOffset[p2];
2675 if( t<12 ){
2676 sqlite3VdbeSerialGet(zData, t, pDest);
2677 }else{
2678 /* If the column value is a string, we need a persistent value, not
2679 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2680 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2681 */
2682 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2683 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002684 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002685 if( pDest->szMalloc < len+2 ){
2686 pDest->flags = MEM_Null;
2687 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2688 }else{
2689 pDest->z = pDest->zMalloc;
2690 }
2691 memcpy(pDest->z, zData, len);
2692 pDest->z[len] = 0;
2693 pDest->z[len+1] = 0;
2694 pDest->flags = aFlag[t&1];
2695 }
danielk197736963fd2005-02-19 08:18:05 +00002696 }else{
drha1851ef2016-05-20 19:51:28 +00002697 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002698 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002699 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2700 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2701 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002702 ){
drh2a2a6962014-09-16 18:22:44 +00002703 /* Content is irrelevant for
2704 ** 1. the typeof() function,
2705 ** 2. the length(X) function if X is a blob, and
2706 ** 3. if the content length is zero.
2707 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002708 ** content from disk.
2709 **
2710 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2711 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2712 ** read up to 16. So 16 bytes of bogus content is supplied.
2713 */
2714 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002715 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002716 }else{
drhcb3cabd2016-11-25 19:18:28 +00002717 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002718 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2719 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2720 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002721 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002722 }
drhd3194f52004-05-27 19:59:32 +00002723
danielk19773c9cc8d2005-01-17 03:40:08 +00002724op_column_out:
drhb7654112008-01-12 12:48:07 +00002725 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002726 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002727 break;
drh74588ce2017-09-13 00:13:05 +00002728
2729op_column_corrupt:
2730 if( aOp[0].p3>0 ){
2731 pOp = &aOp[aOp[0].p3-1];
2732 break;
2733 }else{
2734 rc = SQLITE_CORRUPT_BKPT;
2735 goto abort_due_to_error;
2736 }
danielk1977192ac1d2004-05-10 07:17:30 +00002737}
2738
danielk1977751de562008-04-18 09:01:15 +00002739/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002740** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002741**
2742** Apply affinities to a range of P2 registers starting with P1.
2743**
drhbb6783b2017-04-29 18:02:49 +00002744** P4 is a string that is P2 characters long. The N-th character of the
2745** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002746** memory cell in the range.
2747*/
2748case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002749 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002750
drh856c1032009-06-02 15:21:42 +00002751 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002752 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002753 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002754 assert( zAffinity[pOp->p2]==0 );
2755 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002756 do{
drh9f6168b2016-03-19 23:32:58 +00002757 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002758 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002759 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002760 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002761 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002762 break;
2763}
2764
drh1db639c2008-01-17 02:36:28 +00002765/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002766** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002767**
drh710c4842010-08-30 01:17:20 +00002768** Convert P2 registers beginning with P1 into the [record format]
2769** use as a data record in a database table or as a key
2770** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002771**
drhbb6783b2017-04-29 18:02:49 +00002772** P4 may be a string that is P2 characters long. The N-th character of the
2773** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002774** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002775**
drh8a512562005-11-14 22:29:05 +00002776** The mapping from character to affinity is given by the SQLITE_AFF_
2777** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002778**
drh05883a32015-06-02 15:32:08 +00002779** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002780*/
drh1db639c2008-01-17 02:36:28 +00002781case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002782 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2783 Mem *pRec; /* The new record */
2784 u64 nData; /* Number of bytes of data space */
2785 int nHdr; /* Number of bytes of header space */
2786 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002787 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002788 int nVarint; /* Number of bytes in a varint */
2789 u32 serial_type; /* Type field */
2790 Mem *pData0; /* First field to be combined into the record */
2791 Mem *pLast; /* Last field of the record */
2792 int nField; /* Number of fields in the record */
2793 char *zAffinity; /* The affinity string for the record */
2794 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002795 int i; /* Space used in zNewRecord[] header */
2796 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002797 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002798
drhf3218fe2004-05-28 08:21:02 +00002799 /* Assuming the record contains N fields, the record format looks
2800 ** like this:
2801 **
drh7a224de2004-06-02 01:22:02 +00002802 ** ------------------------------------------------------------------------
2803 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2804 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002805 **
drh9cbf3422008-01-17 16:22:13 +00002806 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002807 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002808 **
2809 ** Each type field is a varint representing the serial type of the
2810 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002811 ** hdr-size field is also a varint which is the offset from the beginning
2812 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002813 */
drh856c1032009-06-02 15:21:42 +00002814 nData = 0; /* Number of bytes of data space */
2815 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002816 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002817 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002818 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002819 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002820 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002821 nField = pOp->p2;
2822 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002823 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002824
drh2b4ded92010-09-27 21:09:31 +00002825 /* Identify the output register */
2826 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2827 pOut = &aMem[pOp->p3];
2828 memAboutToChange(p, pOut);
2829
drh3e6c0602013-12-10 20:53:01 +00002830 /* Apply the requested affinity to all inputs
2831 */
2832 assert( pData0<=pLast );
2833 if( zAffinity ){
2834 pRec = pData0;
2835 do{
drh57bf4a82014-02-17 14:59:22 +00002836 applyAffinity(pRec++, *(zAffinity++), encoding);
2837 assert( zAffinity[0]==0 || pRec<=pLast );
2838 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002839 }
2840
drhd447dce2017-01-25 20:55:11 +00002841#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002842 /* NULLs can be safely trimmed from the end of the record, as long as
2843 ** as the schema format is 2 or more and none of the omitted columns
2844 ** have a non-NULL default value. Also, the record must be left with
2845 ** at least one field. If P5>0 then it will be one more than the
2846 ** index of the right-most column with a non-NULL default value */
2847 if( pOp->p5 ){
2848 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2849 pLast--;
2850 nField--;
2851 }
2852 }
drhd447dce2017-01-25 20:55:11 +00002853#endif
drh585ce192017-01-25 14:58:27 +00002854
drhf3218fe2004-05-28 08:21:02 +00002855 /* Loop through the elements that will make up the record to figure
2856 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002857 */
drh038b7bc2013-12-09 23:17:22 +00002858 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002859 do{
drh2b4ded92010-09-27 21:09:31 +00002860 assert( memIsValid(pRec) );
drh41fb3672018-01-12 23:18:38 +00002861 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002862 if( pRec->flags & MEM_Zero ){
drhce2fbd12018-01-12 21:00:14 +00002863 if( serial_type==0 ){
drh41fb3672018-01-12 23:18:38 +00002864 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
2865 ** table methods that never invoke sqlite3_result_xxxxx() while
2866 ** computing an unchanging column value in an UPDATE statement.
2867 ** Give such values a special internal-use-only serial-type of 10
2868 ** so that they can be passed through to xUpdate and have
2869 ** a true sqlite3_value_nochange(). */
2870 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
2871 serial_type = 10;
drhce2fbd12018-01-12 21:00:14 +00002872 }else if( nData ){
drh53e66c32015-07-24 15:49:23 +00002873 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002874 }else{
2875 nZero += pRec->u.nZero;
2876 len -= pRec->u.nZero;
2877 }
drhfdf972a2007-05-02 13:30:27 +00002878 }
drh8079a0d2006-01-12 17:20:50 +00002879 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002880 testcase( serial_type==127 );
2881 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002882 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh41fb3672018-01-12 23:18:38 +00002883 pRec->uTemp = serial_type;
drh45c3c662016-04-07 14:16:16 +00002884 if( pRec==pData0 ) break;
2885 pRec--;
2886 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002887
drh654858d2014-11-20 02:18:14 +00002888 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2889 ** which determines the total number of bytes in the header. The varint
2890 ** value is the size of the header in bytes including the size varint
2891 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002892 testcase( nHdr==126 );
2893 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002894 if( nHdr<=126 ){
2895 /* The common case */
2896 nHdr += 1;
2897 }else{
2898 /* Rare case of a really large header */
2899 nVarint = sqlite3VarintLen(nHdr);
2900 nHdr += nVarint;
2901 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002902 }
drh038b7bc2013-12-09 23:17:22 +00002903 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00002904
danielk1977a7a8e142008-02-13 18:25:27 +00002905 /* Make sure the output register has a buffer large enough to store
2906 ** the new record. The output register (pOp->p3) is not allowed to
2907 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002908 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002909 */
drh0d7f0cc2018-09-21 13:07:14 +00002910 if( nByte+nZero<=pOut->szMalloc ){
2911 /* The output register is already large enough to hold the record.
2912 ** No error checks or buffer enlargement is required */
2913 pOut->z = pOut->zMalloc;
2914 }else{
2915 /* Need to make sure that the output is not too big and then enlarge
2916 ** the output register to hold the full result */
2917 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
2918 goto too_big;
2919 }
2920 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
2921 goto no_mem;
2922 }
danielk19778d059842004-05-12 11:24:02 +00002923 }
danielk1977a7a8e142008-02-13 18:25:27 +00002924 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002925
2926 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002927 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002928 j = nHdr;
2929 assert( pData0<=pLast );
2930 pRec = pData0;
2931 do{
drhfacf47a2014-10-13 20:12:47 +00002932 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002933 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2934 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002935 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002936 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2937 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002938 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002939 }while( (++pRec)<=pLast );
2940 assert( i==nHdr );
2941 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002942
drh9f6168b2016-03-19 23:32:58 +00002943 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002944 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002945 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002946 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002947 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002948 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002949 }
drh1013c932008-01-06 00:25:21 +00002950 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002951 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002952 break;
2953}
2954
danielk1977a5533162009-02-24 10:01:51 +00002955/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002956** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002957**
2958** Store the number of entries (an integer value) in the table or index
2959** opened by cursor P1 in register P2
2960*/
2961#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002962case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002963 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002964 BtCursor *pCrsr;
2965
drhc960dcb2015-11-20 19:22:01 +00002966 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2967 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002968 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002969 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002970 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002971 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002972 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002973 pOut->u.i = nEntry;
2974 break;
2975}
2976#endif
2977
danielk1977fd7f0452008-12-17 17:30:26 +00002978/* Opcode: Savepoint P1 * * P4 *
2979**
2980** Open, release or rollback the savepoint named by parameter P4, depending
2981** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2982** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2983*/
2984case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002985 int p1; /* Value of P1 operand */
2986 char *zName; /* Name of savepoint */
2987 int nName;
2988 Savepoint *pNew;
2989 Savepoint *pSavepoint;
2990 Savepoint *pTmp;
2991 int iSavepoint;
2992 int ii;
2993
2994 p1 = pOp->p1;
2995 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002996
2997 /* Assert that the p1 parameter is valid. Also that if there is no open
2998 ** transaction, then there cannot be any savepoints.
2999 */
3000 assert( db->pSavepoint==0 || db->autoCommit==0 );
3001 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3002 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3003 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003004 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003005
3006 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003007 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003008 /* A new savepoint cannot be created if there are active write
3009 ** statements (i.e. open read/write incremental blob handles).
3010 */
drh22c17b82015-05-15 04:13:15 +00003011 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003012 rc = SQLITE_BUSY;
3013 }else{
drh856c1032009-06-02 15:21:42 +00003014 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003015
drhbe07ec52011-06-03 12:15:26 +00003016#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003017 /* This call is Ok even if this savepoint is actually a transaction
3018 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3019 ** If this is a transaction savepoint being opened, it is guaranteed
3020 ** that the db->aVTrans[] array is empty. */
3021 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003022 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3023 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003024 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003025#endif
dand9495cd2011-04-27 12:08:04 +00003026
danielk1977fd7f0452008-12-17 17:30:26 +00003027 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003028 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003029 if( pNew ){
3030 pNew->zName = (char *)&pNew[1];
3031 memcpy(pNew->zName, zName, nName+1);
3032
3033 /* If there is no open transaction, then mark this as a special
3034 ** "transaction savepoint". */
3035 if( db->autoCommit ){
3036 db->autoCommit = 0;
3037 db->isTransactionSavepoint = 1;
3038 }else{
3039 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003040 }
dan21e8d012011-03-03 20:05:59 +00003041
danielk1977fd7f0452008-12-17 17:30:26 +00003042 /* Link the new savepoint into the database handle's list. */
3043 pNew->pNext = db->pSavepoint;
3044 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003045 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003046 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003047 }
3048 }
3049 }else{
drh856c1032009-06-02 15:21:42 +00003050 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003051
3052 /* Find the named savepoint. If there is no such savepoint, then an
3053 ** an error is returned to the user. */
3054 for(
drh856c1032009-06-02 15:21:42 +00003055 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003056 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003057 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003058 ){
3059 iSavepoint++;
3060 }
3061 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003062 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003063 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003064 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003065 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003066 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003067 */
drh22c17b82015-05-15 04:13:15 +00003068 sqlite3VdbeError(p, "cannot release savepoint - "
3069 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003070 rc = SQLITE_BUSY;
3071 }else{
3072
3073 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003074 ** and this is a RELEASE command, then the current transaction
3075 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003076 */
3077 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3078 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003079 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003080 goto vdbe_return;
3081 }
danielk1977fd7f0452008-12-17 17:30:26 +00003082 db->autoCommit = 1;
3083 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003084 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003085 db->autoCommit = 0;
3086 p->rc = rc = SQLITE_BUSY;
3087 goto vdbe_return;
3088 }
danielk197734cf35d2008-12-18 18:31:38 +00003089 db->isTransactionSavepoint = 0;
3090 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003091 }else{
drh47b7fc72014-11-11 01:33:57 +00003092 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003093 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003094 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003095 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003096 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003097 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3098 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003099 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003100 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003101 }
drh47b7fc72014-11-11 01:33:57 +00003102 }else{
3103 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003104 }
3105 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003106 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3107 if( rc!=SQLITE_OK ){
3108 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003109 }
danielk1977fd7f0452008-12-17 17:30:26 +00003110 }
drh47b7fc72014-11-11 01:33:57 +00003111 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003112 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003113 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003114 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003115 }
3116 }
3117
3118 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3119 ** savepoints nested inside of the savepoint being operated on. */
3120 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003121 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003122 db->pSavepoint = pTmp->pNext;
3123 sqlite3DbFree(db, pTmp);
3124 db->nSavepoint--;
3125 }
3126
dan1da40a32009-09-19 17:00:31 +00003127 /* If it is a RELEASE, then destroy the savepoint being operated on
3128 ** too. If it is a ROLLBACK TO, then set the number of deferred
3129 ** constraint violations present in the database to the value stored
3130 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003131 if( p1==SAVEPOINT_RELEASE ){
3132 assert( pSavepoint==db->pSavepoint );
3133 db->pSavepoint = pSavepoint->pNext;
3134 sqlite3DbFree(db, pSavepoint);
3135 if( !isTransaction ){
3136 db->nSavepoint--;
3137 }
dan1da40a32009-09-19 17:00:31 +00003138 }else{
3139 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003140 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003141 }
dand9495cd2011-04-27 12:08:04 +00003142
danea8562e2015-04-18 16:25:54 +00003143 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003144 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3145 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3146 }
danielk1977fd7f0452008-12-17 17:30:26 +00003147 }
3148 }
drh9467abf2016-02-17 18:44:11 +00003149 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003150
3151 break;
3152}
3153
drh98757152008-01-09 23:04:12 +00003154/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003155**
3156** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003157** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003158** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3159** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003160**
3161** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003162*/
drh9cbf3422008-01-17 16:22:13 +00003163case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003164 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003165 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003166
drh856c1032009-06-02 15:21:42 +00003167 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003168 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003169 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003170 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003171 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003172 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003173
drhb0c88652016-02-01 13:21:13 +00003174 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003175 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003176 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003177 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003178 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003179 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3180 /* If this instruction implements a COMMIT and other VMs are writing
3181 ** return an error indicating that the other VMs must complete first.
3182 */
3183 sqlite3VdbeError(p, "cannot commit transaction - "
3184 "SQL statements in progress");
3185 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003186 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003187 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003188 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003189 }else{
shane7d3846a2008-12-11 02:58:26 +00003190 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003191 }
3192 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3193 p->pc = (int)(pOp - aOp);
3194 db->autoCommit = (u8)(1-desiredAutoCommit);
3195 p->rc = rc = SQLITE_BUSY;
3196 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003197 }
danielk1977bd434552009-03-18 10:33:00 +00003198 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003199 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003200 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003201 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003202 }else{
drh900b31e2007-08-28 02:27:51 +00003203 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003204 }
drh900b31e2007-08-28 02:27:51 +00003205 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003206 }else{
drh22c17b82015-05-15 04:13:15 +00003207 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003208 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003209 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003210 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003211
3212 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003213 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003214 }
3215 break;
3216}
3217
drhb22f7c82014-02-06 23:56:27 +00003218/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003219**
drh05a86c52014-02-16 01:55:49 +00003220** Begin a transaction on database P1 if a transaction is not already
3221** active.
3222** If P2 is non-zero, then a write-transaction is started, or if a
3223** read-transaction is already active, it is upgraded to a write-transaction.
3224** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003225**
drh001bbcb2003-03-19 03:14:00 +00003226** P1 is the index of the database file on which the transaction is
3227** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003228** file used for temporary tables. Indices of 2 or more are used for
3229** attached databases.
drhcabb0812002-09-14 13:47:32 +00003230**
dane0af83a2009-09-08 19:15:01 +00003231** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3232** true (this flag is set if the Vdbe may modify more than one row and may
3233** throw an ABORT exception), a statement transaction may also be opened.
3234** More specifically, a statement transaction is opened iff the database
3235** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003236** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003237** VDBE to be rolled back after an error without having to roll back the
3238** entire transaction. If no error is encountered, the statement transaction
3239** will automatically commit when the VDBE halts.
3240**
drhb22f7c82014-02-06 23:56:27 +00003241** If P5!=0 then this opcode also checks the schema cookie against P3
3242** and the schema generation counter against P4.
3243** The cookie changes its value whenever the database schema changes.
3244** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003245** and that the current process needs to reread the schema. If the schema
3246** cookie in P3 differs from the schema cookie in the database header or
3247** if the schema generation counter in P4 differs from the current
3248** generation counter, then an SQLITE_SCHEMA error is raised and execution
3249** halts. The sqlite3_step() wrapper function might then reprepare the
3250** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003251*/
drh9cbf3422008-01-17 16:22:13 +00003252case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003253 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003254 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003255
drh1713afb2013-06-28 01:24:57 +00003256 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003257 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003258 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003259 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003260 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3261 rc = SQLITE_READONLY;
3262 goto abort_due_to_error;
3263 }
drh653b82a2009-06-22 11:10:47 +00003264 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003265
danielk197724162fe2004-06-04 06:22:00 +00003266 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003267 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003268 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3269 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003270 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003271 if( (rc&0xff)==SQLITE_BUSY ){
3272 p->pc = (int)(pOp - aOp);
3273 p->rc = rc;
3274 goto vdbe_return;
3275 }
danielk197724162fe2004-06-04 06:22:00 +00003276 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003277 }
dane0af83a2009-09-08 19:15:01 +00003278
3279 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003280 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003281 ){
3282 assert( sqlite3BtreeIsInTrans(pBt) );
3283 if( p->iStatement==0 ){
3284 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3285 db->nStatement++;
3286 p->iStatement = db->nSavepoint + db->nStatement;
3287 }
dana311b802011-04-26 19:21:34 +00003288
drh346506f2011-05-25 01:16:42 +00003289 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003290 if( rc==SQLITE_OK ){
3291 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3292 }
dan1da40a32009-09-19 17:00:31 +00003293
3294 /* Store the current value of the database handles deferred constraint
3295 ** counter. If the statement transaction needs to be rolled back,
3296 ** the value of this counter needs to be restored too. */
3297 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003298 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003299 }
drh397776a2018-06-06 17:45:51 +00003300 }
3301 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3302 if( pOp->p5
3303 && (iMeta!=pOp->p3
3304 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3305 ){
3306 /*
drh96fdcb42016-09-27 00:09:33 +00003307 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3308 ** version is checked to ensure that the schema has not changed since the
3309 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003310 */
drhb22f7c82014-02-06 23:56:27 +00003311 sqlite3DbFree(db, p->zErrMsg);
3312 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3313 /* If the schema-cookie from the database file matches the cookie
3314 ** stored with the in-memory representation of the schema, do
3315 ** not reload the schema from the database file.
3316 **
3317 ** If virtual-tables are in use, this is not just an optimization.
3318 ** Often, v-tables store their data in other SQLite tables, which
3319 ** are queried from within xNext() and other v-table methods using
3320 ** prepared queries. If such a query is out-of-date, we do not want to
3321 ** discard the database schema, as the user code implementing the
3322 ** v-table would have to be ready for the sqlite3_vtab structure itself
3323 ** to be invalidated whenever sqlite3_step() is called from within
3324 ** a v-table method.
3325 */
3326 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3327 sqlite3ResetOneSchema(db, pOp->p1);
3328 }
3329 p->expired = 1;
3330 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003331 }
drh9467abf2016-02-17 18:44:11 +00003332 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003333 break;
3334}
3335
drhb1fdb2a2008-01-05 04:06:03 +00003336/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003337**
drh9cbf3422008-01-17 16:22:13 +00003338** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003339** P3==1 is the schema version. P3==2 is the database format.
3340** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003341** the main database file and P1==1 is the database file used to store
3342** temporary tables.
drh4a324312001-12-21 14:30:42 +00003343**
drh50e5dad2001-09-15 00:57:28 +00003344** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003345** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003346** executing this instruction.
3347*/
drh27a348c2015-04-13 19:14:06 +00003348case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003349 int iMeta;
drh856c1032009-06-02 15:21:42 +00003350 int iDb;
3351 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003352
drh1713afb2013-06-28 01:24:57 +00003353 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003354 iDb = pOp->p1;
3355 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003356 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003357 assert( iDb>=0 && iDb<db->nDb );
3358 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003359 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003360
danielk1977602b4662009-07-02 07:47:33 +00003361 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003362 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003363 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003364 break;
3365}
3366
drh98757152008-01-09 23:04:12 +00003367/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003368**
drh1861afc2016-02-01 21:48:34 +00003369** Write the integer value P3 into cookie number P2 of database P1.
3370** P2==1 is the schema version. P2==2 is the database format.
3371** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003372** size, and so forth. P1==0 is the main database file and P1==1 is the
3373** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003374**
3375** A transaction must be started before executing this opcode.
3376*/
drh1861afc2016-02-01 21:48:34 +00003377case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003378 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003379
3380 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003381 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003382 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003383 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003384 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003385 pDb = &db->aDb[pOp->p1];
3386 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003387 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003388 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003389 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003390 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003391 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003392 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003393 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003394 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003395 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003396 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003397 }
drhfd426c62006-01-30 15:34:22 +00003398 if( pOp->p1==1 ){
3399 /* Invalidate all prepared statements whenever the TEMP database
3400 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003401 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003402 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003403 }
drh9467abf2016-02-17 18:44:11 +00003404 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003405 break;
3406}
3407
drh98757152008-01-09 23:04:12 +00003408/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003409** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003410**
drhecdc7532001-09-23 02:35:53 +00003411** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003412** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003413** P3==0 means the main database, P3==1 means the database used for
3414** temporary tables, and P3>1 means used the corresponding attached
3415** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003416** values need not be contiguous but all P1 values should be small integers.
3417** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003418**
drh8e9deb62018-06-05 13:43:02 +00003419** Allowed P5 bits:
3420** <ul>
3421** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3422** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3423** of OP_SeekLE/OP_IdxGT)
3424** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003425**
danielk1977d336e222009-02-20 10:58:41 +00003426** The P4 value may be either an integer (P4_INT32) or a pointer to
3427** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003428** object, then table being opened must be an [index b-tree] where the
3429** KeyInfo object defines the content and collating
3430** sequence of that index b-tree. Otherwise, if P4 is an integer
3431** value, then the table being opened must be a [table b-tree] with a
3432** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003433**
drh35263192014-07-22 20:02:19 +00003434** See also: OpenWrite, ReopenIdx
3435*/
3436/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3437** Synopsis: root=P2 iDb=P3
3438**
drh8e9deb62018-06-05 13:43:02 +00003439** The ReopenIdx opcode works like OP_OpenRead except that it first
3440** checks to see if the cursor on P1 is already open on the same
3441** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003442** if the cursor is already open, do not reopen it.
3443**
drh8e9deb62018-06-05 13:43:02 +00003444** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3445** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3446** be the same as every other ReopenIdx or OpenRead for the same cursor
3447** number.
drh35263192014-07-22 20:02:19 +00003448**
drh8e9deb62018-06-05 13:43:02 +00003449** Allowed P5 bits:
3450** <ul>
3451** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3452** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3453** of OP_SeekLE/OP_IdxGT)
3454** </ul>
3455**
3456** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003457*/
drh98757152008-01-09 23:04:12 +00003458/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003459** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003460**
3461** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003462** page is P2 (or whose root page is held in register P2 if the
3463** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003464**
danielk1977d336e222009-02-20 10:58:41 +00003465** The P4 value may be either an integer (P4_INT32) or a pointer to
3466** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003467** object, then table being opened must be an [index b-tree] where the
3468** KeyInfo object defines the content and collating
3469** sequence of that index b-tree. Otherwise, if P4 is an integer
3470** value, then the table being opened must be a [table b-tree] with a
3471** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003472**
drh8e9deb62018-06-05 13:43:02 +00003473** Allowed P5 bits:
3474** <ul>
3475** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3476** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3477** of OP_SeekLE/OP_IdxGT)
3478** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3479** and subsequently delete entries in an index btree. This is a
3480** hint to the storage engine that the storage engine is allowed to
3481** ignore. The hint is not used by the official SQLite b*tree storage
3482** engine, but is used by COMDB2.
3483** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3484** as the root page, not the value of P2 itself.
3485** </ul>
drhf57b3392001-10-08 13:22:32 +00003486**
drh8e9deb62018-06-05 13:43:02 +00003487** This instruction works like OpenRead except that it opens the cursor
3488** in read/write mode.
3489**
3490** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003491*/
drh35263192014-07-22 20:02:19 +00003492case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003493 int nField;
3494 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003495 int p2;
3496 int iDb;
drhf57b3392001-10-08 13:22:32 +00003497 int wrFlag;
3498 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003499 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003500 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003501
drhe0997b32015-03-20 14:57:50 +00003502 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003503 assert( pOp->p4type==P4_KEYINFO );
3504 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003505 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003506 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003507 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003508 }
3509 /* If the cursor is not currently open or is open on a different
3510 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003511case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003512case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003513
drhe0997b32015-03-20 14:57:50 +00003514 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003515 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003516 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3517 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003518
drhba968db2018-07-24 22:02:12 +00003519 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003520 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003521 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003522 }
3523
drh856c1032009-06-02 15:21:42 +00003524 nField = 0;
3525 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003526 p2 = pOp->p2;
3527 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003528 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003529 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003530 pDb = &db->aDb[iDb];
3531 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003532 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003533 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003534 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3535 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003536 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003537 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3538 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003539 }
3540 }else{
3541 wrFlag = 0;
3542 }
dan428c2182012-08-06 18:50:11 +00003543 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003544 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003545 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003546 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003547 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003548 assert( memIsValid(pIn2) );
3549 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003550 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003551 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003552 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003553 ** that opcode will always set the p2 value to 2 or more or else fail.
3554 ** If there were a failure, the prepared statement would have halted
3555 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003556 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003557 }
danielk1977d336e222009-02-20 10:58:41 +00003558 if( pOp->p4type==P4_KEYINFO ){
3559 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003560 assert( pKeyInfo->enc==ENC(db) );
3561 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003562 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003563 }else if( pOp->p4type==P4_INT32 ){
3564 nField = pOp->p4.i;
3565 }
drh653b82a2009-06-22 11:10:47 +00003566 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003567 assert( nField>=0 );
3568 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003569 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003570 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003571 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003572 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003573 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003574#ifdef SQLITE_DEBUG
3575 pCur->wrFlag = wrFlag;
3576#endif
drhc960dcb2015-11-20 19:22:01 +00003577 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003578 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003579 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003580 ** SQLite used to check if the root-page flags were sane at this point
3581 ** and report database corruption if they were not, but this check has
3582 ** since moved into the btree layer. */
3583 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003584
3585open_cursor_set_hints:
3586 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3587 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003588 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003589#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003590 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3591#endif
drhc960dcb2015-11-20 19:22:01 +00003592 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003593 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003594 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003595 break;
3596}
3597
drhe08e8d62017-05-01 15:15:41 +00003598/* Opcode: OpenDup P1 P2 * * *
3599**
3600** Open a new cursor P1 that points to the same ephemeral table as
3601** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3602** opcode. Only ephemeral cursors may be duplicated.
3603**
3604** Duplicate ephemeral cursors are used for self-joins of materialized views.
3605*/
3606case OP_OpenDup: {
3607 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3608 VdbeCursor *pCx; /* The new cursor */
3609
3610 pOrig = p->apCsr[pOp->p2];
3611 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3612
3613 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3614 if( pCx==0 ) goto no_mem;
3615 pCx->nullRow = 1;
3616 pCx->isEphemeral = 1;
3617 pCx->pKeyInfo = pOrig->pKeyInfo;
3618 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00003619 pCx->pgnoRoot = pOrig->pgnoRoot;
3620 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00003621 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003622 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3623 ** opened for a database. Since there is already an open cursor when this
3624 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3625 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003626 break;
3627}
3628
3629
drh2a5d9902011-08-26 00:34:45 +00003630/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003631** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003632**
drhb9bb7c12006-06-11 23:41:55 +00003633** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003634** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003635** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003636** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003637**
drhdfe3b582019-01-04 12:35:50 +00003638** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00003639** is cleared (all content is erased).
3640**
drh25d3adb2010-04-05 15:11:08 +00003641** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003642** The cursor points to a BTree table if P4==0 and to a BTree index
3643** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003644** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003645**
drh2a5d9902011-08-26 00:34:45 +00003646** The P5 parameter can be a mask of the BTREE_* flags defined
3647** in btree.h. These flags control aspects of the operation of
3648** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3649** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003650*/
drha21a64d2010-04-06 22:33:55 +00003651/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003652** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003653**
3654** This opcode works the same as OP_OpenEphemeral. It has a
3655** different name to distinguish its use. Tables created using
3656** by this opcode will be used for automatically created transient
3657** indices in joins.
3658*/
3659case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003660case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003661 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003662 KeyInfo *pKeyInfo;
3663
drhd4187c72010-08-30 22:15:45 +00003664 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003665 SQLITE_OPEN_READWRITE |
3666 SQLITE_OPEN_CREATE |
3667 SQLITE_OPEN_EXCLUSIVE |
3668 SQLITE_OPEN_DELETEONCLOSE |
3669 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003670 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003671 assert( pOp->p2>=0 );
drh4afdfa12018-12-31 16:36:42 +00003672 pCx = p->apCsr[pOp->p1];
3673 if( pCx ){
3674 /* If the ephermeral table is already open, erase all existing content
3675 ** so that the table is empty again, rather than creating a new table. */
3676 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00003677 }else{
3678 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3679 if( pCx==0 ) goto no_mem;
3680 pCx->nullRow = 1;
3681 pCx->isEphemeral = 1;
3682 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3683 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
3684 vfsFlags);
3685 if( rc==SQLITE_OK ){
3686 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drhc6b52df2002-01-04 03:09:29 +00003687 }
drhd0fb7962018-12-31 17:58:05 +00003688 if( rc==SQLITE_OK ){
3689 /* If a transient index is required, create it by calling
3690 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3691 ** opening it. If a transient table is required, just use the
3692 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3693 */
3694 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3695 assert( pOp->p4type==P4_KEYINFO );
3696 rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
3697 BTREE_BLOBKEY | pOp->p5);
3698 if( rc==SQLITE_OK ){
3699 assert( pCx->pgnoRoot==MASTER_ROOT+1 );
3700 assert( pKeyInfo->db==db );
3701 assert( pKeyInfo->enc==ENC(db) );
3702 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
3703 pKeyInfo, pCx->uc.pCursor);
3704 }
3705 pCx->isTable = 0;
3706 }else{
3707 pCx->pgnoRoot = MASTER_ROOT;
3708 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
3709 0, pCx->uc.pCursor);
3710 pCx->isTable = 1;
3711 }
3712 }
3713 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drh5e00f6c2001-09-13 13:46:56 +00003714 }
drh9467abf2016-02-17 18:44:11 +00003715 if( rc ) goto abort_due_to_error;
dan5134d132011-09-02 10:31:11 +00003716 break;
3717}
3718
danfad9f9a2014-04-01 18:41:51 +00003719/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003720**
3721** This opcode works like OP_OpenEphemeral except that it opens
3722** a transient index that is specifically designed to sort large
3723** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003724**
3725** If argument P3 is non-zero, then it indicates that the sorter may
3726** assume that a stable sort considering the first P3 fields of each
3727** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003728*/
drhca892a72011-09-03 00:17:51 +00003729case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003730 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003731
drh399af1d2013-11-20 17:25:55 +00003732 assert( pOp->p1>=0 );
3733 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003734 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003735 if( pCx==0 ) goto no_mem;
3736 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003737 assert( pCx->pKeyInfo->db==db );
3738 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003739 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003740 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003741 break;
3742}
3743
dan78d58432014-03-25 15:04:07 +00003744/* Opcode: SequenceTest P1 P2 * * *
3745** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3746**
3747** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3748** to P2. Regardless of whether or not the jump is taken, increment the
3749** the sequence value.
3750*/
3751case OP_SequenceTest: {
3752 VdbeCursor *pC;
3753 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3754 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003755 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003756 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003757 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003758 }
drh5e00f6c2001-09-13 13:46:56 +00003759 break;
3760}
3761
drh5f612292014-02-08 23:20:32 +00003762/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003763** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003764**
3765** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003766** row of data. The content of that one row is the content of memory
3767** register P2. In other words, cursor P1 becomes an alias for the
3768** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003769**
drh2d8d7ce2010-02-15 15:17:05 +00003770** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003771** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003772** individual columns using the OP_Column opcode. The OP_Column opcode
3773** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003774**
3775** P3 is the number of fields in the records that will be stored by
3776** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003777*/
drh9cbf3422008-01-17 16:22:13 +00003778case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003779 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003780
drh653b82a2009-06-22 11:10:47 +00003781 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003782 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003783 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003784 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003785 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003786 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003787 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003788 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3789 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3790 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3791 ** which is a performance optimization */
3792 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003793 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003794 break;
3795}
3796
drh98757152008-01-09 23:04:12 +00003797/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003798**
3799** Close a cursor previously opened as P1. If P1 is not
3800** currently open, this instruction is a no-op.
3801*/
drh9cbf3422008-01-17 16:22:13 +00003802case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003803 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3804 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3805 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003806 break;
3807}
3808
drh97bae792015-06-05 15:59:57 +00003809#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3810/* Opcode: ColumnsUsed P1 * * P4 *
3811**
3812** This opcode (which only exists if SQLite was compiled with
3813** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3814** table or index for cursor P1 are used. P4 is a 64-bit integer
3815** (P4_INT64) in which the first 63 bits are one for each of the
3816** first 63 columns of the table or index that are actually used
3817** by the cursor. The high-order bit is set if any column after
3818** the 64th is used.
3819*/
3820case OP_ColumnsUsed: {
3821 VdbeCursor *pC;
3822 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003823 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003824 pC->maskUsed = *(u64*)pOp->p4.pI64;
3825 break;
3826}
3827#endif
3828
drh8af3f772014-07-25 18:01:06 +00003829/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003830** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003831**
danielk1977b790c6c2008-04-18 10:25:24 +00003832** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003833** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003834** to an SQL index, then P3 is the first in an array of P4 registers
3835** that are used as an unpacked index key.
3836**
3837** Reposition cursor P1 so that it points to the smallest entry that
3838** is greater than or equal to the key value. If there are no records
3839** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003840**
drhb1d607d2015-11-05 22:30:54 +00003841** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3842** opcode will always land on a record that equally equals the key, or
3843** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3844** opcode must be followed by an IdxLE opcode with the same arguments.
3845** The IdxLE opcode will be skipped if this opcode succeeds, but the
3846** IdxLE opcode will be used on subsequent loop iterations.
3847**
drh8af3f772014-07-25 18:01:06 +00003848** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003849** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003850** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003851**
drh935850e2014-05-24 17:15:15 +00003852** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003853*/
drh8af3f772014-07-25 18:01:06 +00003854/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003855** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003856**
danielk1977b790c6c2008-04-18 10:25:24 +00003857** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003858** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003859** to an SQL index, then P3 is the first in an array of P4 registers
3860** that are used as an unpacked index key.
3861**
3862** Reposition cursor P1 so that it points to the smallest entry that
3863** is greater than the key value. If there are no records greater than
3864** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003865**
drh8af3f772014-07-25 18:01:06 +00003866** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003867** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003868** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003869**
drh935850e2014-05-24 17:15:15 +00003870** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003871*/
drh8af3f772014-07-25 18:01:06 +00003872/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003873** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003874**
danielk1977b790c6c2008-04-18 10:25:24 +00003875** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003876** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003877** to an SQL index, then P3 is the first in an array of P4 registers
3878** that are used as an unpacked index key.
3879**
3880** Reposition cursor P1 so that it points to the largest entry that
3881** is less than the key value. If there are no records less than
3882** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003883**
drh8af3f772014-07-25 18:01:06 +00003884** This opcode leaves the cursor configured to move in reverse order,
3885** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003886** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003887**
drh935850e2014-05-24 17:15:15 +00003888** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003889*/
drh8af3f772014-07-25 18:01:06 +00003890/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003891** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003892**
danielk1977b790c6c2008-04-18 10:25:24 +00003893** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003894** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003895** to an SQL index, then P3 is the first in an array of P4 registers
3896** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003897**
danielk1977b790c6c2008-04-18 10:25:24 +00003898** Reposition cursor P1 so that it points to the largest entry that
3899** is less than or equal to the key value. If there are no records
3900** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003901**
drh8af3f772014-07-25 18:01:06 +00003902** This opcode leaves the cursor configured to move in reverse order,
3903** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003904** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003905**
drhb1d607d2015-11-05 22:30:54 +00003906** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3907** opcode will always land on a record that equally equals the key, or
3908** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3909** opcode must be followed by an IdxGE opcode with the same arguments.
3910** The IdxGE opcode will be skipped if this opcode succeeds, but the
3911** IdxGE opcode will be used on subsequent loop iterations.
3912**
drh935850e2014-05-24 17:15:15 +00003913** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003914*/
mistachkin758784d2018-07-25 15:12:29 +00003915case OP_SeekLT: /* jump, in3, group */
3916case OP_SeekLE: /* jump, in3, group */
3917case OP_SeekGE: /* jump, in3, group */
3918case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00003919 int res; /* Comparison result */
3920 int oc; /* Opcode */
3921 VdbeCursor *pC; /* The cursor to seek */
3922 UnpackedRecord r; /* The key to seek for */
3923 int nField; /* Number of columns or fields in the key */
3924 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003925 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003926
drh653b82a2009-06-22 11:10:47 +00003927 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003928 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003929 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003930 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003931 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003932 assert( OP_SeekLE == OP_SeekLT+1 );
3933 assert( OP_SeekGE == OP_SeekLT+2 );
3934 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003935 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003936 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003937 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003938 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003939 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003940#ifdef SQLITE_DEBUG
3941 pC->seekOp = pOp->opcode;
3942#endif
drhe0997b32015-03-20 14:57:50 +00003943
drh3da046d2013-11-11 03:24:11 +00003944 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003945 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003946 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3947 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003948
drh3da046d2013-11-11 03:24:11 +00003949 /* The input value in P3 might be of any type: integer, real, string,
3950 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003951 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003952 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003953 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003954 applyNumericAffinity(pIn3, 0);
3955 }
drh3da046d2013-11-11 03:24:11 +00003956 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003957
drh3da046d2013-11-11 03:24:11 +00003958 /* If the P3 value could not be converted into an integer without
3959 ** loss of information, then special processing is required... */
3960 if( (pIn3->flags & MEM_Int)==0 ){
3961 if( (pIn3->flags & MEM_Real)==0 ){
3962 /* If the P3 value cannot be converted into any kind of a number,
3963 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003964 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003965 break;
3966 }
drh959403f2008-12-12 17:56:16 +00003967
danaa1776f2013-11-26 18:22:59 +00003968 /* If the approximation iKey is larger than the actual real search
3969 ** term, substitute >= for > and < for <=. e.g. if the search term
3970 ** is 4.9 and the integer approximation 5:
3971 **
3972 ** (x > 4.9) -> (x >= 5)
3973 ** (x <= 4.9) -> (x < 5)
3974 */
drh74eaba42014-09-18 17:52:15 +00003975 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003976 assert( OP_SeekGE==(OP_SeekGT-1) );
3977 assert( OP_SeekLT==(OP_SeekLE-1) );
3978 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3979 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003980 }
3981
3982 /* If the approximation iKey is smaller than the actual real search
3983 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003984 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003985 assert( OP_SeekLE==(OP_SeekLT+1) );
3986 assert( OP_SeekGT==(OP_SeekGE+1) );
3987 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3988 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003989 }
drh3da046d2013-11-11 03:24:11 +00003990 }
drhc960dcb2015-11-20 19:22:01 +00003991 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003992 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003993 if( rc!=SQLITE_OK ){
3994 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003995 }
drhaa736092009-06-22 00:55:30 +00003996 }else{
drhd6b79462015-11-07 01:19:00 +00003997 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3998 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3999 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
4000 */
drhc960dcb2015-11-20 19:22:01 +00004001 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004002 eqOnly = 1;
4003 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4004 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4005 assert( pOp[1].p1==pOp[0].p1 );
4006 assert( pOp[1].p2==pOp[0].p2 );
4007 assert( pOp[1].p3==pOp[0].p3 );
4008 assert( pOp[1].p4.i==pOp[0].p4.i );
4009 }
4010
drh3da046d2013-11-11 03:24:11 +00004011 nField = pOp->p4.i;
4012 assert( pOp->p4type==P4_INT32 );
4013 assert( nField>0 );
4014 r.pKeyInfo = pC->pKeyInfo;
4015 r.nField = (u16)nField;
4016
4017 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004018 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004019 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004020 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004021 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004022 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004023 */
dan1fed5da2014-02-25 21:01:25 +00004024 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4025 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4026 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4027 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4028 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004029
4030 r.aMem = &aMem[pOp->p3];
4031#ifdef SQLITE_DEBUG
4032 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4033#endif
drh70528d72015-11-05 20:25:09 +00004034 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004035 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004036 if( rc!=SQLITE_OK ){
4037 goto abort_due_to_error;
4038 }
drhb1d607d2015-11-05 22:30:54 +00004039 if( eqOnly && r.eqSeen==0 ){
4040 assert( res!=0 );
4041 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004042 }
drh3da046d2013-11-11 03:24:11 +00004043 }
4044 pC->deferredMoveto = 0;
4045 pC->cacheStatus = CACHE_STALE;
4046#ifdef SQLITE_TEST
4047 sqlite3_search_count++;
4048#endif
drh4a1d3652014-02-14 15:13:36 +00004049 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4050 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004051 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004052 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4053 if( rc!=SQLITE_OK ){
4054 if( rc==SQLITE_DONE ){
4055 rc = SQLITE_OK;
4056 res = 1;
4057 }else{
4058 goto abort_due_to_error;
4059 }
4060 }
drh3da046d2013-11-11 03:24:11 +00004061 }else{
4062 res = 0;
4063 }
4064 }else{
drh4a1d3652014-02-14 15:13:36 +00004065 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4066 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004067 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004068 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4069 if( rc!=SQLITE_OK ){
4070 if( rc==SQLITE_DONE ){
4071 rc = SQLITE_OK;
4072 res = 1;
4073 }else{
4074 goto abort_due_to_error;
4075 }
4076 }
drh3da046d2013-11-11 03:24:11 +00004077 }else{
4078 /* res might be negative because the table is empty. Check to
4079 ** see if this is the case.
4080 */
drhc960dcb2015-11-20 19:22:01 +00004081 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004082 }
4083 }
drhb1d607d2015-11-05 22:30:54 +00004084seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004085 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004086 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004087 if( res ){
drhf56fa462015-04-13 21:39:54 +00004088 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004089 }else if( eqOnly ){
4090 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4091 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004092 }
drh5e00f6c2001-09-13 13:46:56 +00004093 break;
4094}
dan71c57db2016-07-09 20:23:55 +00004095
drh8c2b6d72018-06-05 20:45:20 +00004096/* Opcode: SeekHit P1 P2 * * *
4097** Synopsis: seekHit=P2
4098**
4099** Set the seekHit flag on cursor P1 to the value in P2.
4100** The seekHit flag is used by the IfNoHope opcode.
4101**
4102** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4103** either 0 or 1.
4104*/
4105case OP_SeekHit: {
4106 VdbeCursor *pC;
4107 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4108 pC = p->apCsr[pOp->p1];
4109 assert( pC!=0 );
4110 assert( pOp->p2==0 || pOp->p2==1 );
4111 pC->seekHit = pOp->p2 & 1;
4112 break;
4113}
4114
drh8cff69d2009-11-12 19:59:44 +00004115/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004116** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004117**
drh8cff69d2009-11-12 19:59:44 +00004118** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4119** P4>0 then register P3 is the first of P4 registers that form an unpacked
4120** record.
4121**
4122** Cursor P1 is on an index btree. If the record identified by P3 and P4
4123** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004124** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004125**
drhcefc87f2014-08-01 01:40:33 +00004126** This operation leaves the cursor in a state where it can be
4127** advanced in the forward direction. The Next instruction will work,
4128** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004129**
drh6f225d02013-10-26 13:36:51 +00004130** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004131*/
drh8cff69d2009-11-12 19:59:44 +00004132/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004133** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004134**
drh8cff69d2009-11-12 19:59:44 +00004135** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4136** P4>0 then register P3 is the first of P4 registers that form an unpacked
4137** record.
4138**
4139** Cursor P1 is on an index btree. If the record identified by P3 and P4
4140** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4141** does contain an entry whose prefix matches the P3/P4 record then control
4142** falls through to the next instruction and P1 is left pointing at the
4143** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004144**
drh8af3f772014-07-25 18:01:06 +00004145** This operation leaves the cursor in a state where it cannot be
4146** advanced in either direction. In other words, the Next and Prev
4147** opcodes do not work after this operation.
4148**
drh8c2b6d72018-06-05 20:45:20 +00004149** See also: Found, NotExists, NoConflict, IfNoHope
4150*/
4151/* Opcode: IfNoHope P1 P2 P3 P4 *
4152** Synopsis: key=r[P3@P4]
4153**
4154** Register P3 is the first of P4 registers that form an unpacked
4155** record.
4156**
4157** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4158** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4159** check to see if there is any entry in P1 that matches the
4160** prefix identified by P3 and P4. If no entry matches the prefix,
4161** jump to P2. Otherwise fall through.
4162**
4163** This opcode behaves like OP_NotFound if the seekHit
4164** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4165**
4166** This opcode is used in IN clause processing for a multi-column key.
4167** If an IN clause is attached to an element of the key other than the
4168** left-most element, and if there are no matches on the most recent
4169** seek over the whole key, then it might be that one of the key element
4170** to the left is prohibiting a match, and hence there is "no hope" of
4171** any match regardless of how many IN clause elements are checked.
4172** In such a case, we abandon the IN clause search early, using this
4173** opcode. The opcode name comes from the fact that the
4174** jump is taken if there is "no hope" of achieving a match.
4175**
4176** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004177*/
drh6f225d02013-10-26 13:36:51 +00004178/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004179** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004180**
4181** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4182** P4>0 then register P3 is the first of P4 registers that form an unpacked
4183** record.
4184**
4185** Cursor P1 is on an index btree. If the record identified by P3 and P4
4186** contains any NULL value, jump immediately to P2. If all terms of the
4187** record are not-NULL then a check is done to determine if any row in the
4188** P1 index btree has a matching key prefix. If there are no matches, jump
4189** immediately to P2. If there is a match, fall through and leave the P1
4190** cursor pointing to the matching row.
4191**
4192** This opcode is similar to OP_NotFound with the exceptions that the
4193** branch is always taken if any part of the search key input is NULL.
4194**
drh8af3f772014-07-25 18:01:06 +00004195** This operation leaves the cursor in a state where it cannot be
4196** advanced in either direction. In other words, the Next and Prev
4197** opcodes do not work after this operation.
4198**
drh6f225d02013-10-26 13:36:51 +00004199** See also: NotFound, Found, NotExists
4200*/
drh8c2b6d72018-06-05 20:45:20 +00004201case OP_IfNoHope: { /* jump, in3 */
4202 VdbeCursor *pC;
4203 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4204 pC = p->apCsr[pOp->p1];
4205 assert( pC!=0 );
4206 if( pC->seekHit ) break;
4207 /* Fall through into OP_NotFound */
4208}
drh6f225d02013-10-26 13:36:51 +00004209case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004210case OP_NotFound: /* jump, in3 */
4211case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004212 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004213 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004214 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004215 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004216 int res;
drha582b012016-12-21 19:45:54 +00004217 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004218 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004219 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004220
dan0ff297e2009-09-25 17:03:14 +00004221#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004222 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004223#endif
4224
drhaa736092009-06-22 00:55:30 +00004225 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004226 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004227 pC = p->apCsr[pOp->p1];
4228 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004229#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004230 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004231#endif
drh3c657212009-11-17 23:59:58 +00004232 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004233 assert( pC->eCurType==CURTYPE_BTREE );
4234 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004235 assert( pC->isTable==0 );
4236 if( pOp->p4.i>0 ){
4237 r.pKeyInfo = pC->pKeyInfo;
4238 r.nField = (u16)pOp->p4.i;
4239 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004240#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004241 for(ii=0; ii<r.nField; ii++){
4242 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004243 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004244 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004245 }
drh8aaf7bc2016-09-20 01:19:18 +00004246#endif
drh3da046d2013-11-11 03:24:11 +00004247 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004248 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004249 }else{
drhe46515b2017-05-19 22:51:00 +00004250 assert( pIn3->flags & MEM_Blob );
4251 rc = ExpandBlob(pIn3);
4252 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4253 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004254 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004255 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004256 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004257 }
dan1fed5da2014-02-25 21:01:25 +00004258 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004259 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004260 if( pOp->opcode==OP_NoConflict ){
4261 /* For the OP_NoConflict opcode, take the jump if any of the
4262 ** input fields are NULL, since any key with a NULL will not
4263 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004264 for(ii=0; ii<pIdxKey->nField; ii++){
4265 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004266 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004267 break;
drh6f225d02013-10-26 13:36:51 +00004268 }
4269 }
drh5e00f6c2001-09-13 13:46:56 +00004270 }
drhc960dcb2015-11-20 19:22:01 +00004271 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004272 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004273 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004274 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004275 }
4276 pC->seekResult = res;
4277 alreadyExists = (res==0);
4278 pC->nullRow = 1-alreadyExists;
4279 pC->deferredMoveto = 0;
4280 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004281 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004282 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004283 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004284 }else{
drhf56fa462015-04-13 21:39:54 +00004285 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4286 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004287 }
drh5e00f6c2001-09-13 13:46:56 +00004288 break;
4289}
4290
drheeb95652016-05-26 20:56:38 +00004291/* Opcode: SeekRowid P1 P2 P3 * *
4292** Synopsis: intkey=r[P3]
4293**
4294** P1 is the index of a cursor open on an SQL table btree (with integer
4295** keys). If register P3 does not contain an integer or if P1 does not
4296** contain a record with rowid P3 then jump immediately to P2.
4297** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4298** a record with rowid P3 then
4299** leave the cursor pointing at that record and fall through to the next
4300** instruction.
4301**
4302** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4303** the P3 register must be guaranteed to contain an integer value. With this
4304** opcode, register P3 might not contain an integer.
4305**
4306** The OP_NotFound opcode performs the same operation on index btrees
4307** (with arbitrary multi-value keys).
4308**
4309** This opcode leaves the cursor in a state where it cannot be advanced
4310** in either direction. In other words, the Next and Prev opcodes will
4311** not work following this opcode.
4312**
4313** See also: Found, NotFound, NoConflict, SeekRowid
4314*/
drh9cbf3422008-01-17 16:22:13 +00004315/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004316** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004317**
drh261c02d2013-10-25 14:46:15 +00004318** P1 is the index of a cursor open on an SQL table btree (with integer
4319** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004320** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4321** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4322** leave the cursor pointing at that record and fall through to the next
4323** instruction.
drh6b125452002-01-28 15:53:03 +00004324**
drheeb95652016-05-26 20:56:38 +00004325** The OP_SeekRowid opcode performs the same operation but also allows the
4326** P3 register to contain a non-integer value, in which case the jump is
4327** always taken. This opcode requires that P3 always contain an integer.
4328**
drh261c02d2013-10-25 14:46:15 +00004329** The OP_NotFound opcode performs the same operation on index btrees
4330** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004331**
drh8af3f772014-07-25 18:01:06 +00004332** This opcode leaves the cursor in a state where it cannot be advanced
4333** in either direction. In other words, the Next and Prev opcodes will
4334** not work following this opcode.
4335**
drheeb95652016-05-26 20:56:38 +00004336** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004337*/
drheeb95652016-05-26 20:56:38 +00004338case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004339 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004340 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004341 int res;
4342 u64 iKey;
4343
drh3c657212009-11-17 23:59:58 +00004344 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004345 if( (pIn3->flags & MEM_Int)==0 ){
drhe4fe6d42018-08-03 15:58:07 +00004346 /* Make sure pIn3->u.i contains a valid integer representation of
4347 ** the key value, but do not change the datatype of the register, as
4348 ** other parts of the perpared statement might be depending on the
4349 ** current datatype. */
4350 u16 origFlags = pIn3->flags;
4351 int isNotInt;
drheeb95652016-05-26 20:56:38 +00004352 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
drhe4fe6d42018-08-03 15:58:07 +00004353 isNotInt = (pIn3->flags & MEM_Int)==0;
4354 pIn3->flags = origFlags;
4355 if( isNotInt ) goto jump_to_p2;
drheeb95652016-05-26 20:56:38 +00004356 }
4357 /* Fall through into OP_NotExists */
4358case OP_NotExists: /* jump, in3 */
4359 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004360 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004361 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4362 pC = p->apCsr[pOp->p1];
4363 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004364#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00004365 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004366#endif
drhaa736092009-06-22 00:55:30 +00004367 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004368 assert( pC->eCurType==CURTYPE_BTREE );
4369 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004370 assert( pCrsr!=0 );
4371 res = 0;
4372 iKey = pIn3->u.i;
4373 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004374 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004375 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004376 pC->nullRow = 0;
4377 pC->cacheStatus = CACHE_STALE;
4378 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004379 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004380 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004381 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004382 assert( rc==SQLITE_OK );
4383 if( pOp->p2==0 ){
4384 rc = SQLITE_CORRUPT_BKPT;
4385 }else{
4386 goto jump_to_p2;
4387 }
danc6157e12015-09-14 09:23:47 +00004388 }
drh9467abf2016-02-17 18:44:11 +00004389 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004390 break;
4391}
4392
drh4c583122008-01-04 22:01:03 +00004393/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004394** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004395**
drh4c583122008-01-04 22:01:03 +00004396** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004397** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004398** The sequence number on the cursor is incremented after this
4399** instruction.
drh4db38a72005-09-01 12:16:28 +00004400*/
drh27a348c2015-04-13 19:14:06 +00004401case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004402 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4403 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004404 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004405 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004406 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004407 break;
4408}
4409
4410
drh98757152008-01-09 23:04:12 +00004411/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004412** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004413**
drhf0863fe2005-06-12 21:35:51 +00004414** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004415** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004416** table that cursor P1 points to. The new record number is written
4417** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004418**
dan76d462e2009-08-30 11:42:51 +00004419** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4420** the largest previously generated record number. No new record numbers are
4421** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004422** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004423** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004424** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004425*/
drh27a348c2015-04-13 19:14:06 +00004426case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004427 i64 v; /* The new rowid */
4428 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4429 int res; /* Result of an sqlite3BtreeLast() */
4430 int cnt; /* Counter to limit the number of searches */
4431 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004432 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004433
drh856c1032009-06-02 15:21:42 +00004434 v = 0;
4435 res = 0;
drh27a348c2015-04-13 19:14:06 +00004436 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004437 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4438 pC = p->apCsr[pOp->p1];
4439 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004440 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004441 assert( pC->eCurType==CURTYPE_BTREE );
4442 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004443 {
drh5cf8e8c2002-02-19 22:42:05 +00004444 /* The next rowid or record number (different terms for the same
4445 ** thing) is obtained in a two-step algorithm.
4446 **
4447 ** First we attempt to find the largest existing rowid and add one
4448 ** to that. But if the largest existing rowid is already the maximum
4449 ** positive integer, we have to fall through to the second
4450 ** probabilistic algorithm
4451 **
4452 ** The second algorithm is to select a rowid at random and see if
4453 ** it already exists in the table. If it does not exist, we have
4454 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004455 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004456 */
drhaa736092009-06-22 00:55:30 +00004457 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004458
drh75f86a42005-02-17 00:03:06 +00004459#ifdef SQLITE_32BIT_ROWID
4460# define MAX_ROWID 0x7fffffff
4461#else
drhfe2093d2005-01-20 22:48:47 +00004462 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4463 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4464 ** to provide the constant while making all compilers happy.
4465 */
danielk197764202cf2008-11-17 15:31:47 +00004466# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004467#endif
drhfe2093d2005-01-20 22:48:47 +00004468
drh5cf8e8c2002-02-19 22:42:05 +00004469 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004470 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004471 if( rc!=SQLITE_OK ){
4472 goto abort_due_to_error;
4473 }
4474 if( res ){
4475 v = 1; /* IMP: R-61914-48074 */
4476 }else{
drhc960dcb2015-11-20 19:22:01 +00004477 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004478 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004479 if( v>=MAX_ROWID ){
4480 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004481 }else{
drhe0670b62014-02-12 21:31:12 +00004482 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004483 }
drh3fc190c2001-09-14 03:24:23 +00004484 }
drhe0670b62014-02-12 21:31:12 +00004485 }
drh205f48e2004-11-05 00:43:11 +00004486
4487#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004488 if( pOp->p3 ){
4489 /* Assert that P3 is a valid memory cell. */
4490 assert( pOp->p3>0 );
4491 if( p->pFrame ){
4492 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004493 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004494 assert( pOp->p3<=pFrame->nMem );
4495 pMem = &pFrame->aMem[pOp->p3];
4496 }else{
4497 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004498 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004499 pMem = &aMem[pOp->p3];
4500 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004501 }
drhe0670b62014-02-12 21:31:12 +00004502 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004503
drhe0670b62014-02-12 21:31:12 +00004504 REGISTER_TRACE(pOp->p3, pMem);
4505 sqlite3VdbeMemIntegerify(pMem);
4506 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4507 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004508 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004509 goto abort_due_to_error;
4510 }
4511 if( v<pMem->u.i+1 ){
4512 v = pMem->u.i + 1;
4513 }
4514 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004515 }
drhe0670b62014-02-12 21:31:12 +00004516#endif
drh5cf8e8c2002-02-19 22:42:05 +00004517 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004518 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004519 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004520 ** engine starts picking positive candidate ROWIDs at random until
4521 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004522 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4523 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004524 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004525 do{
4526 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004527 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004528 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004529 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004530 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004531 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004532 if( rc ) goto abort_due_to_error;
4533 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004534 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004535 goto abort_due_to_error;
4536 }
drh748a52c2010-09-01 11:50:08 +00004537 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004538 }
drha11846b2004-01-07 18:52:56 +00004539 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004540 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004541 }
drh4c583122008-01-04 22:01:03 +00004542 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004543 break;
4544}
4545
danielk19771f4aa332008-01-03 09:51:55 +00004546/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004547** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004548**
jplyon5a564222003-06-02 06:15:58 +00004549** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004550** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004551** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004552** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004553** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004554**
danielk19771f4aa332008-01-03 09:51:55 +00004555** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4556** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004557** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004558** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004559**
drheaf6ae22016-11-09 20:14:34 +00004560** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4561** run faster by avoiding an unnecessary seek on cursor P1. However,
4562** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4563** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004564**
4565** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4566** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4567** is part of an INSERT operation. The difference is only important to
4568** the update hook.
4569**
dan319eeb72011-03-19 08:38:50 +00004570** Parameter P4 may point to a Table structure, or may be NULL. If it is
4571** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4572** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004573**
drh93aed5a2008-01-16 17:46:38 +00004574** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4575** allocated, then ownership of P2 is transferred to the pseudo-cursor
4576** and register P2 becomes ephemeral. If the cursor is changed, the
4577** value of register P2 will then change. Make sure this does not
4578** cause any problems.)
4579**
drhf0863fe2005-06-12 21:35:51 +00004580** This instruction only works on tables. The equivalent instruction
4581** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004582*/
drhe05c9292009-10-29 13:48:10 +00004583/* Opcode: InsertInt P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00004584** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004585**
4586** This works exactly like OP_Insert except that the key is the
4587** integer value P3, not the value of the integer stored in register P3.
4588*/
4589case OP_Insert:
4590case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004591 Mem *pData; /* MEM cell holding data for the record to be inserted */
4592 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004593 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004594 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4595 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004596 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00004597 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004598
drha6c2ed92009-11-14 23:22:23 +00004599 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004600 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004601 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004602 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004603 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004604 assert( pC->eCurType==CURTYPE_BTREE );
4605 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004606 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004607 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004608 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00004609 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00004610
drhe05c9292009-10-29 13:48:10 +00004611 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004612 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004613 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004614 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004615 REGISTER_TRACE(pOp->p3, pKey);
drh8eeb4462016-05-21 20:03:42 +00004616 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004617 }else{
4618 assert( pOp->opcode==OP_InsertInt );
drh8eeb4462016-05-21 20:03:42 +00004619 x.nKey = pOp->p3;
drhe05c9292009-10-29 13:48:10 +00004620 }
4621
drh9b1c62d2011-03-30 21:04:43 +00004622 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004623 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004624 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004625 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004626 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00004627 }else{
drh4ec6f3a2018-01-12 19:33:18 +00004628 pTab = 0;
drh74c33022016-03-30 12:56:55 +00004629 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004630 }
4631
drh9b1c62d2011-03-30 21:04:43 +00004632#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004633 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00004634 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00004635 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
4636 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
4637 }
drh4ec6f3a2018-01-12 19:33:18 +00004638 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
4639 /* Prevent post-update hook from running in cases when it should not */
4640 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00004641 }
dan46c47d42011-03-01 18:42:07 +00004642 }
dancb9a3642017-01-30 19:44:53 +00004643 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004644#endif
dan46c47d42011-03-01 18:42:07 +00004645
drha05a7222008-01-19 03:35:58 +00004646 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004647 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004648 assert( pData->flags & (MEM_Blob|MEM_Str) );
4649 x.pData = pData->z;
4650 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004651 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4652 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004653 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004654 }else{
drh8eeb4462016-05-21 20:03:42 +00004655 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004656 }
drh8eeb4462016-05-21 20:03:42 +00004657 x.pKey = 0;
4658 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004659 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004660 );
drha05a7222008-01-19 03:35:58 +00004661 pC->deferredMoveto = 0;
4662 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004663
drha05a7222008-01-19 03:35:58 +00004664 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004665 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00004666 if( pTab ){
4667 assert( db->xUpdateCallback!=0 );
4668 assert( pTab->aCol!=0 );
4669 db->xUpdateCallback(db->pUpdateArg,
4670 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
4671 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004672 }
drh5e00f6c2001-09-13 13:46:56 +00004673 break;
4674}
4675
dan438b8812015-09-15 15:55:15 +00004676/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004677**
drh5edc3122001-09-13 21:53:09 +00004678** Delete the record at which the P1 cursor is currently pointing.
4679**
drhe807bdb2016-01-21 17:06:33 +00004680** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4681** the cursor will be left pointing at either the next or the previous
4682** record in the table. If it is left pointing at the next record, then
4683** the next Next instruction will be a no-op. As a result, in this case
4684** it is ok to delete a record from within a Next loop. If
4685** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4686** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004687**
drhdef19e32016-01-27 16:26:25 +00004688** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4689** delete one of several associated with deleting a table row and all its
4690** associated index entries. Exactly one of those deletes is the "primary"
4691** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4692** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004693**
4694** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4695** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004696**
drh91fd4d42008-01-19 20:11:25 +00004697** P1 must not be pseudo-table. It has to be a real table with
4698** multiple rows.
4699**
drh5e769a52016-09-28 16:05:53 +00004700** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004701** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4702** have been positioned using OP_NotFound prior to invoking this opcode in
4703** this case. Specifically, if one is configured, the pre-update hook is
4704** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4705** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004706**
4707** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4708** of the memory cell that contains the value that the rowid of the row will
4709** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004710*/
drh9cbf3422008-01-17 16:22:13 +00004711case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004712 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004713 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004714 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004715 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004716
dan46c47d42011-03-01 18:42:07 +00004717 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004718 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4719 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004720 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004721 assert( pC->eCurType==CURTYPE_BTREE );
4722 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004723 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00004724 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00004725
drhb53a5a92014-10-12 22:37:22 +00004726#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004727 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4728 /* If p5 is zero, the seek operation that positioned the cursor prior to
4729 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4730 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004731 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh92fe38e2014-10-14 13:41:32 +00004732 assert( pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004733 }
4734#endif
drh91fd4d42008-01-19 20:11:25 +00004735
dan438b8812015-09-15 15:55:15 +00004736 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4737 ** the name of the db to pass as to it. Also set local pTab to a copy
4738 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4739 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4740 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004741 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004742 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004743 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004744 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004745 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004746 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004747 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004748 }
drh74c33022016-03-30 12:56:55 +00004749 }else{
4750 zDb = 0; /* Not needed. Silence a compiler warning. */
4751 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004752 }
dan46c47d42011-03-01 18:42:07 +00004753
drh9b1c62d2011-03-30 21:04:43 +00004754#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004755 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004756 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4757 assert( !(opflags & OPFLAG_ISUPDATE)
4758 || HasRowid(pTab)==0
4759 || (aMem[pOp->p3].flags & MEM_Int)
4760 );
dan46c47d42011-03-01 18:42:07 +00004761 sqlite3VdbePreUpdateHook(p, pC,
4762 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004763 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004764 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004765 );
4766 }
dan46c47d42011-03-01 18:42:07 +00004767 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004768#endif
drhb53a5a92014-10-12 22:37:22 +00004769
drhdef19e32016-01-27 16:26:25 +00004770 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4771 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004772 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004773 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004774
4775#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004776 if( p->pFrame==0 ){
4777 if( pC->isEphemeral==0
4778 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4779 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4780 ){
4781 nExtraDelete++;
4782 }
4783 if( pOp->p2 & OPFLAG_NCHANGE ){
4784 nExtraDelete--;
4785 }
drhb89aeb62016-01-27 15:49:32 +00004786 }
4787#endif
4788
drhc960dcb2015-11-20 19:22:01 +00004789 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004790 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004791 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004792 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004793
drh91fd4d42008-01-19 20:11:25 +00004794 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004795 if( opflags & OPFLAG_NCHANGE ){
4796 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004797 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004798 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004799 pC->movetoTarget);
4800 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004801 }
drh5e00f6c2001-09-13 13:46:56 +00004802 }
dan438b8812015-09-15 15:55:15 +00004803
rdcb0c374f2004-02-20 22:53:38 +00004804 break;
4805}
drhb7f1d9a2009-09-08 02:27:58 +00004806/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004807**
drhb7f1d9a2009-09-08 02:27:58 +00004808** The value of the change counter is copied to the database handle
4809** change counter (returned by subsequent calls to sqlite3_changes()).
4810** Then the VMs internal change counter resets to 0.
4811** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004812*/
drh9cbf3422008-01-17 16:22:13 +00004813case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004814 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004815 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004816 break;
4817}
4818
drh1153c7b2013-11-01 22:02:56 +00004819/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00004820** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004821**
drh1153c7b2013-11-01 22:02:56 +00004822** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004823** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004824** the sorter cursor currently points to. Only the first P4 fields
4825** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004826**
4827** If either P3 or the sorter contains a NULL in one of their significant
4828** fields (not counting the P4 fields at the end which are ignored) then
4829** the comparison is assumed to be equal.
4830**
4831** Fall through to next instruction if the two records compare equal to
4832** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004833*/
4834case OP_SorterCompare: {
4835 VdbeCursor *pC;
4836 int res;
drhac502322014-07-30 13:56:48 +00004837 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004838
4839 pC = p->apCsr[pOp->p1];
4840 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004841 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004842 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004843 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004844 res = 0;
drhac502322014-07-30 13:56:48 +00004845 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004846 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004847 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004848 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004849 break;
4850};
4851
drh6cf4a7d2014-10-13 13:00:58 +00004852/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004853** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004854**
4855** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004856** Then clear the column header cache on cursor P3.
4857**
4858** This opcode is normally use to move a record out of the sorter and into
4859** a register that is the source for a pseudo-table cursor created using
4860** OpenPseudo. That pseudo-table cursor is the one that is identified by
4861** parameter P3. Clearing the P3 column cache as part of this opcode saves
4862** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004863*/
4864case OP_SorterData: {
4865 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004866
dan5134d132011-09-02 10:31:11 +00004867 pOut = &aMem[pOp->p2];
4868 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004869 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004870 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004871 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004872 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004873 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004874 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004875 break;
4876}
4877
drhe7b554d2017-01-09 15:44:25 +00004878/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004879** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004880**
drh9057fc72016-11-25 19:32:32 +00004881** Write into register P2 the complete row content for the row at
4882** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00004883** There is no interpretation of the data.
4884** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004885** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004886**
drh9057fc72016-11-25 19:32:32 +00004887** If cursor P1 is an index, then the content is the key of the row.
4888** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00004889**
drhde4fcfd2008-01-19 23:50:26 +00004890** If the P1 cursor must be pointing to a valid row (not a NULL row)
4891** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00004892**
drh8cdafc32018-05-31 19:00:20 +00004893** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00004894** into the database page. That means that the content of the output
4895** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00004896** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00004897** position in order that they can write to the same table. If P3==0
4898** then a copy of the data is made into memory. P3!=0 is faster, but
4899** P3==0 is safer.
4900**
4901** If P3!=0 then the content of the P2 register is unsuitable for use
4902** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00004903** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00004904** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00004905*/
danielk1977a7a8e142008-02-13 18:25:27 +00004906case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004907 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004908 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004909 u32 n;
drh70ce3f02003-04-15 19:22:22 +00004910
drhe7b554d2017-01-09 15:44:25 +00004911 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00004912
drh653b82a2009-06-22 11:10:47 +00004913 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4914 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004915 assert( pC!=0 );
4916 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004917 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00004918 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004919 assert( pC->uc.pCursor!=0 );
4920 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004921
drh9057fc72016-11-25 19:32:32 +00004922 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00004923 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4924 ** that might invalidate the cursor.
4925 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00004926 ** would fail. Should this ever change (because of changes in the code
4927 ** generator) then the fix would be to insert a call to
4928 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004929 */
4930 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004931 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4932#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004933 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004934 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4935#endif
drh9a65f2c2009-06-22 19:05:40 +00004936
drha7c90c42016-06-04 20:37:10 +00004937 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00004938 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00004939 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00004940 }
drh722246e2014-10-07 23:02:24 +00004941 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00004942 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00004943 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00004944 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00004945 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004946 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004947 break;
4948}
4949
drh2133d822008-01-03 18:44:59 +00004950/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004951** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004952**
drh2133d822008-01-03 18:44:59 +00004953** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004954** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004955**
4956** P1 can be either an ordinary table or a virtual table. There used to
4957** be a separate OP_VRowid opcode for use with virtual tables, but this
4958** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004959*/
drh27a348c2015-04-13 19:14:06 +00004960case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004961 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004962 i64 v;
drh856c1032009-06-02 15:21:42 +00004963 sqlite3_vtab *pVtab;
4964 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004965
drh27a348c2015-04-13 19:14:06 +00004966 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004967 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4968 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004969 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004970 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004971 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004972 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004973 break;
4974 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004975 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004976#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004977 }else if( pC->eCurType==CURTYPE_VTAB ){
4978 assert( pC->uc.pVCur!=0 );
4979 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004980 pModule = pVtab->pModule;
4981 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004982 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004983 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004984 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004985#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004986 }else{
drhc960dcb2015-11-20 19:22:01 +00004987 assert( pC->eCurType==CURTYPE_BTREE );
4988 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004989 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004990 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004991 if( pC->nullRow ){
4992 pOut->flags = MEM_Null;
4993 break;
4994 }
drha7c90c42016-06-04 20:37:10 +00004995 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00004996 }
drh4c583122008-01-04 22:01:03 +00004997 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004998 break;
4999}
5000
drh9cbf3422008-01-17 16:22:13 +00005001/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005002**
5003** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005004** that occur while the cursor is on the null row will always
5005** write a NULL.
drh17f71932002-02-21 12:01:27 +00005006*/
drh9cbf3422008-01-17 16:22:13 +00005007case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005008 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005009
drh653b82a2009-06-22 11:10:47 +00005010 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5011 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005012 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00005013 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005014 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005015 if( pC->eCurType==CURTYPE_BTREE ){
5016 assert( pC->uc.pCursor!=0 );
5017 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005018 }
drhcf025a82018-06-07 18:01:21 +00005019#ifdef SQLITE_DEBUG
5020 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5021#endif
drh17f71932002-02-21 12:01:27 +00005022 break;
5023}
5024
drh86b40df2017-08-01 19:53:43 +00005025/* Opcode: SeekEnd P1 * * * *
5026**
5027** Position cursor P1 at the end of the btree for the purpose of
5028** appending a new entry onto the btree.
5029**
5030** It is assumed that the cursor is used only for appending and so
5031** if the cursor is valid, then the cursor must already be pointing
5032** at the end of the btree and so no changes are made to
5033** the cursor.
5034*/
5035/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005036**
drh8af3f772014-07-25 18:01:06 +00005037** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005038** will refer to the last entry in the database table or index.
5039** If the table or index is empty and P2>0, then jump immediately to P2.
5040** If P2 is 0 or if the table or index is not empty, fall through
5041** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005042**
5043** This opcode leaves the cursor configured to move in reverse order,
5044** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005045** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005046*/
drh86b40df2017-08-01 19:53:43 +00005047case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005048case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005049 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005050 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005051 int res;
drh9562b552002-02-19 15:00:07 +00005052
drh653b82a2009-06-22 11:10:47 +00005053 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5054 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005055 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005056 assert( pC->eCurType==CURTYPE_BTREE );
5057 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005058 res = 0;
drh3da046d2013-11-11 03:24:11 +00005059 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005060#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005061 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005062#endif
drh86b40df2017-08-01 19:53:43 +00005063 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005064 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005065 pC->seekResult = -1;
5066 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5067 break;
5068 }
5069 }
5070 rc = sqlite3BtreeLast(pCrsr, &res);
5071 pC->nullRow = (u8)res;
5072 pC->deferredMoveto = 0;
5073 pC->cacheStatus = CACHE_STALE;
5074 if( rc ) goto abort_due_to_error;
5075 if( pOp->p2>0 ){
5076 VdbeBranchTaken(res!=0,2);
5077 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005078 }
5079 break;
5080}
5081
drh5e98e832017-02-17 19:24:06 +00005082/* Opcode: IfSmaller P1 P2 P3 * *
5083**
5084** Estimate the number of rows in the table P1. Jump to P2 if that
5085** estimate is less than approximately 2**(0.1*P3).
5086*/
5087case OP_IfSmaller: { /* jump */
5088 VdbeCursor *pC;
5089 BtCursor *pCrsr;
5090 int res;
5091 i64 sz;
5092
5093 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5094 pC = p->apCsr[pOp->p1];
5095 assert( pC!=0 );
5096 pCrsr = pC->uc.pCursor;
5097 assert( pCrsr );
5098 rc = sqlite3BtreeFirst(pCrsr, &res);
5099 if( rc ) goto abort_due_to_error;
5100 if( res==0 ){
5101 sz = sqlite3BtreeRowCountEst(pCrsr);
5102 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5103 }
5104 VdbeBranchTaken(res!=0,2);
5105 if( res ) goto jump_to_p2;
5106 break;
5107}
5108
drh0342b1f2005-09-01 03:07:44 +00005109
drh6bd4dc62016-12-23 16:05:22 +00005110/* Opcode: SorterSort P1 P2 * * *
5111**
5112** After all records have been inserted into the Sorter object
5113** identified by P1, invoke this opcode to actually do the sorting.
5114** Jump to P2 if there are no records to be sorted.
5115**
5116** This opcode is an alias for OP_Sort and OP_Rewind that is used
5117** for Sorter objects.
5118*/
drh9cbf3422008-01-17 16:22:13 +00005119/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005120**
5121** This opcode does exactly the same thing as OP_Rewind except that
5122** it increments an undocumented global variable used for testing.
5123**
5124** Sorting is accomplished by writing records into a sorting index,
5125** then rewinding that index and playing it back from beginning to
5126** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5127** rewinding so that the global variable will be incremented and
5128** regression tests can determine whether or not the optimizer is
5129** correctly optimizing out sorts.
5130*/
drhc6aff302011-09-01 15:32:47 +00005131case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005132case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005133#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005134 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005135 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005136#endif
drh9b47ee32013-08-20 03:13:51 +00005137 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005138 /* Fall through into OP_Rewind */
5139}
dan9a947222018-06-14 19:06:36 +00005140/* Opcode: Rewind P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00005141**
drhf0863fe2005-06-12 21:35:51 +00005142** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005143** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005144** If the table or index is empty, jump immediately to P2.
5145** If the table or index is not empty, fall through to the following
5146** instruction.
drh8af3f772014-07-25 18:01:06 +00005147**
dan9a947222018-06-14 19:06:36 +00005148** If P5 is non-zero and the table is not empty, then the "skip-next"
5149** flag is set on the cursor so that the next OP_Next instruction
5150** executed on it is a no-op.
5151**
drh8af3f772014-07-25 18:01:06 +00005152** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005153** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005154** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005155*/
drh9cbf3422008-01-17 16:22:13 +00005156case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005157 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005158 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005159 int res;
drh5e00f6c2001-09-13 13:46:56 +00005160
drh653b82a2009-06-22 11:10:47 +00005161 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5162 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005163 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005164 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005165 res = 1;
drh8af3f772014-07-25 18:01:06 +00005166#ifdef SQLITE_DEBUG
5167 pC->seekOp = OP_Rewind;
5168#endif
dan689ab892011-08-12 15:02:00 +00005169 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005170 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005171 }else{
drhc960dcb2015-11-20 19:22:01 +00005172 assert( pC->eCurType==CURTYPE_BTREE );
5173 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005174 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005175 rc = sqlite3BtreeFirst(pCrsr, &res);
dan67a9b8e2018-06-22 20:51:35 +00005176#ifndef SQLITE_OMIT_WINDOWFUNC
danc3a20c12018-05-23 20:55:37 +00005177 if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
dan67a9b8e2018-06-22 20:51:35 +00005178#endif
drha11846b2004-01-07 18:52:56 +00005179 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005180 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005181 }
drh9467abf2016-02-17 18:44:11 +00005182 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005183 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005184 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005185 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005186 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005187 break;
5188}
5189
drh0fd61352014-02-07 02:29:45 +00005190/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005191**
5192** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005193** table or index. If there are no more key/value pairs then fall through
5194** to the following instruction. But if the cursor advance was successful,
5195** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005196**
drh5dad9a32014-07-25 18:37:42 +00005197** The Next opcode is only valid following an SeekGT, SeekGE, or
5198** OP_Rewind opcode used to position the cursor. Next is not allowed
5199** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005200**
drhf93cd942013-11-21 03:12:25 +00005201** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5202** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005203**
drhe39a7322014-02-03 14:04:11 +00005204** The P3 value is a hint to the btree implementation. If P3==1, that
5205** means P1 is an SQL index and that this instruction could have been
5206** omitted if that index had been unique. P3 is usually 0. P3 is
5207** always either 0 or 1.
5208**
dana205a482011-08-27 18:48:57 +00005209** P4 is always of type P4_ADVANCE. The function pointer points to
5210** sqlite3BtreeNext().
5211**
drhafc266a2010-03-31 17:47:44 +00005212** If P5 is positive and the jump is taken, then event counter
5213** number P5-1 in the prepared statement is incremented.
5214**
drhf1949b62018-06-07 17:32:59 +00005215** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005216*/
drh0fd61352014-02-07 02:29:45 +00005217/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005218**
5219** Back up cursor P1 so that it points to the previous key/data pair in its
5220** table or index. If there is no previous key/value pairs then fall through
5221** to the following instruction. But if the cursor backup was successful,
5222** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005223**
drh8af3f772014-07-25 18:01:06 +00005224**
drh5dad9a32014-07-25 18:37:42 +00005225** The Prev opcode is only valid following an SeekLT, SeekLE, or
5226** OP_Last opcode used to position the cursor. Prev is not allowed
5227** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005228**
drhf93cd942013-11-21 03:12:25 +00005229** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5230** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005231**
drhe39a7322014-02-03 14:04:11 +00005232** The P3 value is a hint to the btree implementation. If P3==1, that
5233** means P1 is an SQL index and that this instruction could have been
5234** omitted if that index had been unique. P3 is usually 0. P3 is
5235** always either 0 or 1.
5236**
dana205a482011-08-27 18:48:57 +00005237** P4 is always of type P4_ADVANCE. The function pointer points to
5238** sqlite3BtreePrevious().
5239**
drhafc266a2010-03-31 17:47:44 +00005240** If P5 is positive and the jump is taken, then event counter
5241** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005242*/
drh6bd4dc62016-12-23 16:05:22 +00005243/* Opcode: SorterNext P1 P2 * * P5
5244**
5245** This opcode works just like OP_Next except that P1 must be a
5246** sorter object for which the OP_SorterSort opcode has been
5247** invoked. This opcode advances the cursor to the next sorted
5248** record, or jumps to P2 if there are no more sorted records.
5249*/
drhf93cd942013-11-21 03:12:25 +00005250case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005251 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005252
drhf93cd942013-11-21 03:12:25 +00005253 pC = p->apCsr[pOp->p1];
5254 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005255 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005256 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005257case OP_Prev: /* jump */
5258case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005259 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005260 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005261 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005262 assert( pC!=0 );
5263 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005264 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005265 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5266 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005267
drhcf025a82018-06-07 18:01:21 +00005268 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005269 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005270 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005271 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcf025a82018-06-07 18:01:21 +00005272 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
drh94f4f872018-12-20 22:08:32 +00005273 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid);
drhf1949b62018-06-07 17:32:59 +00005274 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005275 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drhcf025a82018-06-07 18:01:21 +00005276 || pC->seekOp==OP_Last
5277 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005278
drh2ab792e2017-05-30 18:34:07 +00005279 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005280next_tail:
drha3460582008-07-11 21:02:53 +00005281 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005282 VdbeBranchTaken(rc==SQLITE_OK,2);
5283 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005284 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005285 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005286#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005287 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005288#endif
drhf56fa462015-04-13 21:39:54 +00005289 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005290 }
drh2ab792e2017-05-30 18:34:07 +00005291 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5292 rc = SQLITE_OK;
5293 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005294 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005295}
5296
drh9b4eaeb2016-11-09 00:10:33 +00005297/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005298** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005299**
drhef8662b2011-06-20 21:47:58 +00005300** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005301** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005302** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005303**
drhfb8c56f2016-11-09 01:19:25 +00005304** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005305** key of reg(P2). In that case, P3 is the index of the first register
5306** for the unpacked key. The availability of the unpacked key can sometimes
5307** be an optimization.
5308**
5309** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5310** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005311**
mistachkin21a919f2014-02-07 03:28:02 +00005312** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5313** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5314** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005315**
drheaf6ae22016-11-09 20:14:34 +00005316** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5317** run faster by avoiding an unnecessary seek on cursor P1. However,
5318** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5319** seeks on the cursor or if the most recent seek used a key equivalent
5320** to P2.
drh0fd61352014-02-07 02:29:45 +00005321**
drhf0863fe2005-06-12 21:35:51 +00005322** This instruction only works for indices. The equivalent instruction
5323** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005324*/
drhf013e202016-10-15 18:37:05 +00005325/* Opcode: SorterInsert P1 P2 * * *
5326** Synopsis: key=r[P2]
5327**
5328** Register P2 holds an SQL index key made using the
5329** MakeRecord instructions. This opcode writes that key
5330** into the sorter P1. Data for the entry is nil.
5331*/
drhca892a72011-09-03 00:17:51 +00005332case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005333case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005334 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005335 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005336
drh653b82a2009-06-22 11:10:47 +00005337 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5338 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005339 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005340 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005341 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005342 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005343 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005344 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005345 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005346 assert( pC->isTable==0 );
5347 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005348 if( rc ) goto abort_due_to_error;
5349 if( pOp->opcode==OP_SorterInsert ){
5350 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5351 }else{
drh8eeb4462016-05-21 20:03:42 +00005352 x.nKey = pIn2->n;
5353 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005354 x.aMem = aMem + pOp->p3;
5355 x.nMem = (u16)pOp->p4.i;
5356 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005357 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005358 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5359 );
5360 assert( pC->deferredMoveto==0 );
5361 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005362 }
drh9467abf2016-02-17 18:44:11 +00005363 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005364 break;
5365}
5366
drhd1d38482008-10-07 23:46:38 +00005367/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005368** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005369**
drhe14006d2008-03-25 17:23:32 +00005370** The content of P3 registers starting at register P2 form
5371** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005372** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005373*/
drhe14006d2008-03-25 17:23:32 +00005374case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005375 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005376 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005377 int res;
5378 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005379
drhe14006d2008-03-25 17:23:32 +00005380 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005381 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005382 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5383 pC = p->apCsr[pOp->p1];
5384 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005385 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005386 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005387 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005388 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005389 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005390 r.pKeyInfo = pC->pKeyInfo;
5391 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005392 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005393 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005394 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005395 if( rc ) goto abort_due_to_error;
5396 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005397 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005398 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005399 }
drh3da046d2013-11-11 03:24:11 +00005400 assert( pC->deferredMoveto==0 );
5401 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005402 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005403 break;
5404}
5405
drh170ad682017-06-02 15:44:22 +00005406/* Opcode: DeferredSeek P1 * P3 P4 *
5407** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005408**
5409** P1 is an open index cursor and P3 is a cursor on the corresponding
5410** table. This opcode does a deferred seek of the P3 table cursor
5411** to the row that corresponds to the current row of P1.
5412**
5413** This is a deferred seek. Nothing actually happens until
5414** the cursor is used to read a record. That way, if no reads
5415** occur, no unnecessary I/O happens.
5416**
5417** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005418** one entry for each column in the P3 table. If array entry a(i)
5419** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005420** equivalent to performing the deferred seek and then reading column i
5421** from P1. This information is stored in P3 and used to redirect
5422** reads against P3 over to P1, thus possibly avoiding the need to
5423** seek and read cursor P3.
5424*/
drh2133d822008-01-03 18:44:59 +00005425/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005426** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005427**
drh2133d822008-01-03 18:44:59 +00005428** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005429** the end of the index key pointed to by cursor P1. This integer should be
5430** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005431**
drh9437bd22009-02-01 00:29:56 +00005432** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005433*/
drh170ad682017-06-02 15:44:22 +00005434case OP_DeferredSeek:
5435case OP_IdxRowid: { /* out2 */
5436 VdbeCursor *pC; /* The P1 index cursor */
5437 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5438 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005439
drh653b82a2009-06-22 11:10:47 +00005440 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5441 pC = p->apCsr[pOp->p1];
5442 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005443 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005444 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005445 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005446 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005447 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5448
5449 /* The IdxRowid and Seek opcodes are combined because of the commonality
5450 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5451 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005452
5453 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005454 ** out from under the cursor. That will never happens for an IdxRowid
5455 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005456 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5457
drh3da046d2013-11-11 03:24:11 +00005458 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005459 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005460 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005461 if( rc!=SQLITE_OK ){
5462 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005463 }
drh170ad682017-06-02 15:44:22 +00005464 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005465 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5466 pTabCur = p->apCsr[pOp->p3];
5467 assert( pTabCur!=0 );
5468 assert( pTabCur->eCurType==CURTYPE_BTREE );
5469 assert( pTabCur->uc.pCursor!=0 );
5470 assert( pTabCur->isTable );
5471 pTabCur->nullRow = 0;
5472 pTabCur->movetoTarget = rowid;
5473 pTabCur->deferredMoveto = 1;
5474 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5475 pTabCur->aAltMap = pOp->p4.ai;
5476 pTabCur->pAltCursor = pC;
5477 }else{
5478 pOut = out2Prerelease(p, pOp);
5479 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005480 }
5481 }else{
5482 assert( pOp->opcode==OP_IdxRowid );
5483 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005484 }
5485 break;
5486}
5487
danielk197761dd5832008-04-18 11:31:12 +00005488/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005489** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005490**
danielk197761dd5832008-04-18 11:31:12 +00005491** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005492** key that omits the PRIMARY KEY. Compare this key value against the index
5493** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5494** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005495**
danielk197761dd5832008-04-18 11:31:12 +00005496** If the P1 index entry is greater than or equal to the key value
5497** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005498*/
5499/* Opcode: IdxGT P1 P2 P3 P4 P5
5500** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005501**
drh4a1d3652014-02-14 15:13:36 +00005502** The P4 register values beginning with P3 form an unpacked index
5503** key that omits the PRIMARY KEY. Compare this key value against the index
5504** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5505** fields at the end.
5506**
5507** If the P1 index entry is greater than the key value
5508** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005509*/
drh3bb9b932010-08-06 02:10:00 +00005510/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005511** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005512**
danielk197761dd5832008-04-18 11:31:12 +00005513** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005514** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5515** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5516** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005517**
danielk197761dd5832008-04-18 11:31:12 +00005518** If the P1 index entry is less than the key value then jump to P2.
5519** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005520*/
drh4a1d3652014-02-14 15:13:36 +00005521/* Opcode: IdxLE P1 P2 P3 P4 P5
5522** Synopsis: key=r[P3@P4]
5523**
5524** The P4 register values beginning with P3 form an unpacked index
5525** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5526** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5527** ROWID on the P1 index.
5528**
5529** If the P1 index entry is less than or equal to the key value then jump
5530** to P2. Otherwise fall through to the next instruction.
5531*/
5532case OP_IdxLE: /* jump */
5533case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005534case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005535case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005536 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005537 int res;
5538 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005539
drh653b82a2009-06-22 11:10:47 +00005540 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5541 pC = p->apCsr[pOp->p1];
5542 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005543 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005544 assert( pC->eCurType==CURTYPE_BTREE );
5545 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005546 assert( pC->deferredMoveto==0 );
5547 assert( pOp->p5==0 || pOp->p5==1 );
5548 assert( pOp->p4type==P4_INT32 );
5549 r.pKeyInfo = pC->pKeyInfo;
5550 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005551 if( pOp->opcode<OP_IdxLT ){
5552 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005553 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005554 }else{
drh4a1d3652014-02-14 15:13:36 +00005555 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005556 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005557 }
5558 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005559#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00005560 {
5561 int i;
5562 for(i=0; i<r.nField; i++){
5563 assert( memIsValid(&r.aMem[i]) );
5564 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
5565 }
5566 }
drh2b4ded92010-09-27 21:09:31 +00005567#endif
drh2dc06482013-12-11 00:59:10 +00005568 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005569 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005570 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5571 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5572 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005573 res = -res;
5574 }else{
drh4a1d3652014-02-14 15:13:36 +00005575 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005576 res++;
5577 }
drh688852a2014-02-17 22:40:43 +00005578 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005579 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005580 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005581 break;
5582}
5583
drh98757152008-01-09 23:04:12 +00005584/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005585**
5586** Delete an entire database table or index whose root page in the database
5587** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005588**
drh98757152008-01-09 23:04:12 +00005589** The table being destroyed is in the main database file if P3==0. If
5590** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005591** that is used to store tables create using CREATE TEMPORARY TABLE.
5592**
drh205f48e2004-11-05 00:43:11 +00005593** If AUTOVACUUM is enabled then it is possible that another root page
5594** might be moved into the newly deleted root page in order to keep all
5595** root pages contiguous at the beginning of the database. The former
5596** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005597** is stored in register P2. If no page movement was required (because the
5598** table being dropped was already the last one in the database) then a
5599** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5600** is stored in register P2.
5601**
5602** This opcode throws an error if there are any active reader VMs when
5603** it is invoked. This is done to avoid the difficulty associated with
5604** updating existing cursors when a root page is moved in an AUTOVACUUM
5605** database. This error is thrown even if the database is not an AUTOVACUUM
5606** db in order to avoid introducing an incompatibility between autovacuum
5607** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005608**
drhb19a2bc2001-09-16 00:13:26 +00005609** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005610*/
drh27a348c2015-04-13 19:14:06 +00005611case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005612 int iMoved;
drh856c1032009-06-02 15:21:42 +00005613 int iDb;
drh3a949872012-09-18 13:20:13 +00005614
drh4031baf2018-05-28 17:31:20 +00005615 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005616 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005617 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005618 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005619 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005620 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005621 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005622 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005623 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005624 }else{
drh856c1032009-06-02 15:21:42 +00005625 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005626 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005627 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005628 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005629 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005630 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005631 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005632#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005633 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005634 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5635 /* All OP_Destroy operations occur on the same btree */
5636 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5637 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005638 }
drh3765df42006-06-28 18:18:09 +00005639#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005640 }
drh5e00f6c2001-09-13 13:46:56 +00005641 break;
5642}
5643
danielk1977c7af4842008-10-27 13:59:33 +00005644/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005645**
5646** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005647** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005648** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005649**
drhf57b3392001-10-08 13:22:32 +00005650** The table being clear is in the main database file if P2==0. If
5651** P2==1 then the table to be clear is in the auxiliary database file
5652** that is used to store tables create using CREATE TEMPORARY TABLE.
5653**
shanebe217792009-03-05 04:20:31 +00005654** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005655** intkey table (an SQL table, not an index). In this case the row change
5656** count is incremented by the number of rows in the table being cleared.
5657** If P3 is greater than zero, then the value stored in register P3 is
5658** also incremented by the number of rows in the table being cleared.
5659**
drhb19a2bc2001-09-16 00:13:26 +00005660** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005661*/
drh9cbf3422008-01-17 16:22:13 +00005662case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005663 int nChange;
5664
drh4031baf2018-05-28 17:31:20 +00005665 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005666 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005667 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005668 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005669 rc = sqlite3BtreeClearTable(
5670 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5671 );
5672 if( pOp->p3 ){
5673 p->nChange += nChange;
5674 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005675 assert( memIsValid(&aMem[pOp->p3]) );
5676 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005677 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005678 }
5679 }
drh9467abf2016-02-17 18:44:11 +00005680 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005681 break;
5682}
5683
drh65ea12c2014-03-19 17:41:36 +00005684/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005685**
drh65ea12c2014-03-19 17:41:36 +00005686** Delete all contents from the ephemeral table or sorter
5687** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005688**
drh65ea12c2014-03-19 17:41:36 +00005689** This opcode only works for cursors used for sorting and
5690** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005691*/
drh65ea12c2014-03-19 17:41:36 +00005692case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005693 VdbeCursor *pC;
5694
5695 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5696 pC = p->apCsr[pOp->p1];
5697 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005698 if( isSorter(pC) ){
5699 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005700 }else{
drhc960dcb2015-11-20 19:22:01 +00005701 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005702 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005703 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005704 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005705 }
drh079a3072014-03-19 14:10:55 +00005706 break;
5707}
5708
drh0f3f7662017-08-18 14:34:28 +00005709/* Opcode: CreateBtree P1 P2 P3 * *
5710** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005711**
drh0f3f7662017-08-18 14:34:28 +00005712** Allocate a new b-tree in the main database file if P1==0 or in the
5713** TEMP database file if P1==1 or in an attached database if
5714** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00005715** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00005716** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005717*/
drh0f3f7662017-08-18 14:34:28 +00005718case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005719 int pgno;
drh234c39d2004-07-24 03:30:47 +00005720 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005721
drh4031baf2018-05-28 17:31:20 +00005722 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00005723 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005724 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005725 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005726 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005727 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005728 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005729 pDb = &db->aDb[pOp->p1];
5730 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005731 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005732 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005733 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005734 break;
5735}
5736
drh4a54bb52017-02-18 15:58:52 +00005737/* Opcode: SqlExec * * * P4 *
5738**
5739** Run the SQL statement or statements specified in the P4 string.
5740*/
5741case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00005742 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00005743 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005744 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005745 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005746 if( rc ) goto abort_due_to_error;
5747 break;
5748}
5749
drh22645842011-03-24 01:34:03 +00005750/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005751**
5752** Read and parse all entries from the SQLITE_MASTER table of database P1
drh1595abc2018-08-14 19:27:51 +00005753** that match the WHERE clause P4. If P4 is a NULL pointer, then the
5754** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00005755**
5756** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005757** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005758*/
drh9cbf3422008-01-17 16:22:13 +00005759case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005760 int iDb;
5761 const char *zMaster;
5762 char *zSql;
5763 InitData initData;
5764
drhbdaec522011-04-04 00:14:43 +00005765 /* Any prepared statement that invokes this opcode will hold mutexes
5766 ** on every btree. This is a prerequisite for invoking
5767 ** sqlite3InitCallback().
5768 */
5769#ifdef SQLITE_DEBUG
5770 for(iDb=0; iDb<db->nDb; iDb++){
5771 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5772 }
5773#endif
drhbdaec522011-04-04 00:14:43 +00005774
drh856c1032009-06-02 15:21:42 +00005775 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005776 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005777 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
dane325ffe2018-08-11 13:40:20 +00005778
5779#ifndef SQLITE_OMIT_ALTERTABLE
5780 if( pOp->p4.z==0 ){
5781 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00005782 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
drh1595abc2018-08-14 19:27:51 +00005783 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
dane325ffe2018-08-11 13:40:20 +00005784 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00005785 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00005786 }else
5787#endif
drh1595abc2018-08-14 19:27:51 +00005788 {
drhe0a04a32016-12-16 01:00:21 +00005789 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005790 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00005791 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00005792 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00005793 initData.mInitFlags = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005794 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005795 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005796 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005797 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005798 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005799 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005800 assert( db->init.busy==0 );
5801 db->init.busy = 1;
5802 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00005803 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005804 assert( !db->mallocFailed );
5805 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5806 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00005807 if( rc==SQLITE_OK && initData.nInitRow==0 ){
5808 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
5809 ** at least one SQL statement. Any less than that indicates that
5810 ** the sqlite_master table is corrupt. */
5811 rc = SQLITE_CORRUPT_BKPT;
5812 }
drhdbd6a7d2017-04-05 12:39:49 +00005813 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005814 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005815 }
drh3c23a882007-01-09 14:01:13 +00005816 }
drh9467abf2016-02-17 18:44:11 +00005817 if( rc ){
5818 sqlite3ResetAllSchemasOfConnection(db);
5819 if( rc==SQLITE_NOMEM ){
5820 goto no_mem;
5821 }
5822 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005823 }
drh234c39d2004-07-24 03:30:47 +00005824 break;
5825}
5826
drh8bfdf722009-06-19 14:06:03 +00005827#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005828/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005829**
5830** Read the sqlite_stat1 table for database P1 and load the content
5831** of that table into the internal index hash table. This will cause
5832** the analysis to be used when preparing all subsequent queries.
5833*/
drh9cbf3422008-01-17 16:22:13 +00005834case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005835 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5836 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005837 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005838 break;
5839}
drh8bfdf722009-06-19 14:06:03 +00005840#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005841
drh98757152008-01-09 23:04:12 +00005842/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005843**
5844** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005845** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005846** is dropped from disk (using the Destroy opcode) in order to keep
5847** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005848** schema consistent with what is on disk.
5849*/
drh9cbf3422008-01-17 16:22:13 +00005850case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00005851 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005852 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005853 break;
5854}
5855
drh98757152008-01-09 23:04:12 +00005856/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005857**
5858** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005859** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005860** is dropped from disk (using the Destroy opcode)
5861** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005862** schema consistent with what is on disk.
5863*/
drh9cbf3422008-01-17 16:22:13 +00005864case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00005865 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005866 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005867 break;
5868}
5869
drh98757152008-01-09 23:04:12 +00005870/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005871**
5872** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005873** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005874** is dropped from disk (using the Destroy opcode) in order to keep
5875** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005876** schema consistent with what is on disk.
5877*/
drh9cbf3422008-01-17 16:22:13 +00005878case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00005879 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005880 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005881 break;
5882}
5883
drh234c39d2004-07-24 03:30:47 +00005884
drhb7f91642004-10-31 02:22:47 +00005885#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005886/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005887**
drh98757152008-01-09 23:04:12 +00005888** Do an analysis of the currently open database. Store in
5889** register P1 the text of an error message describing any problems.
5890** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005891**
drh66accfc2017-02-22 18:04:42 +00005892** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005893** At most reg(P3) errors will be reported.
5894** In other words, the analysis stops as soon as reg(P1) errors are
5895** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005896**
drh98968b22016-03-15 22:00:39 +00005897** The root page numbers of all tables in the database are integers
5898** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005899**
drh98757152008-01-09 23:04:12 +00005900** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005901** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005902**
drh1dcdbc02007-01-27 02:24:54 +00005903** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005904*/
drhaaab5722002-02-19 13:39:21 +00005905case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005906 int nRoot; /* Number of tables to check. (Number of root pages.) */
5907 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005908 int nErr; /* Number of errors reported */
5909 char *z; /* Text of the error report */
5910 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005911
drh1713afb2013-06-28 01:24:57 +00005912 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005913 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005914 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005915 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00005916 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00005917 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005918 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005919 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005920 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005921 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005922 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005923 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drhb5c10632017-09-21 00:49:15 +00005924 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00005925 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005926 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005927 if( nErr==0 ){
5928 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005929 }else if( z==0 ){
5930 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005931 }else{
drh66accfc2017-02-22 18:04:42 +00005932 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005933 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005934 }
drhb7654112008-01-12 12:48:07 +00005935 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005936 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005937 break;
5938}
drhb7f91642004-10-31 02:22:47 +00005939#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005940
drh3d4501e2008-12-04 20:40:10 +00005941/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005942** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005943**
drhbb6783b2017-04-29 18:02:49 +00005944** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00005945** held in register P1.
5946**
5947** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005948*/
drh93952eb2009-11-13 19:43:43 +00005949case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005950 pIn1 = &aMem[pOp->p1];
5951 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005952 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00005953 if( (pIn1->flags & MEM_Blob)==0 ){
5954 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005955 }
drh9d67afc2018-08-29 20:24:03 +00005956 assert( sqlite3VdbeMemIsRowSet(pIn1) );
5957 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005958 break;
5959}
5960
5961/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005962** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005963**
drhbb6783b2017-04-29 18:02:49 +00005964** Extract the smallest value from the RowSet object in P1
5965** and put that value into register P3.
5966** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00005967** unchanged and jump to instruction P2.
5968*/
drh93952eb2009-11-13 19:43:43 +00005969case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005970 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005971
drh3c657212009-11-17 23:59:58 +00005972 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00005973 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
5974 if( (pIn1->flags & MEM_Blob)==0
5975 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005976 ){
5977 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005978 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005979 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005980 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005981 }else{
5982 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005983 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005984 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005985 }
drh49afe3a2013-07-10 03:05:14 +00005986 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005987}
5988
drh1b26c7c2009-04-22 02:15:47 +00005989/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005990** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005991**
drhade97602009-04-21 15:05:18 +00005992** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005993** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005994** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005995** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005996** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005997**
drhbb6783b2017-04-29 18:02:49 +00005998** The RowSet object is optimized for the case where sets of integers
5999** are inserted in distinct phases, which each set contains no duplicates.
6000** Each set is identified by a unique P4 value. The first set
6001** must have P4==0, the final set must have P4==-1, and for all other sets
6002** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00006003**
6004** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006005** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006006** (b) when P4==-1 there is no need to insert the value, as it will
6007** never be tested for, and (c) when a value that is part of set X is
6008** inserted, there is no need to search to see if the same value was
6009** previously inserted as part of set X (only if it was previously
6010** inserted as part of some other set).
6011*/
drh1b26c7c2009-04-22 02:15:47 +00006012case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006013 int iSet;
6014 int exists;
6015
drh3c657212009-11-17 23:59:58 +00006016 pIn1 = &aMem[pOp->p1];
6017 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006018 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006019 assert( pIn3->flags&MEM_Int );
6020
drh1b26c7c2009-04-22 02:15:47 +00006021 /* If there is anything other than a rowset object in memory cell P1,
6022 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006023 */
drh9d67afc2018-08-29 20:24:03 +00006024 if( (pIn1->flags & MEM_Blob)==0 ){
6025 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006026 }
drh9d67afc2018-08-29 20:24:03 +00006027 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006028 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006029 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006030 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006031 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006032 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006033 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006034 }
6035 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006036 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006037 }
6038 break;
6039}
6040
drh5e00f6c2001-09-13 13:46:56 +00006041
danielk197793758c82005-01-21 08:13:14 +00006042#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006043
drh0fd61352014-02-07 02:29:45 +00006044/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006045**
dan76d462e2009-08-30 11:42:51 +00006046** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006047**
dan76d462e2009-08-30 11:42:51 +00006048** P1 contains the address of the memory cell that contains the first memory
6049** cell in an array of values used as arguments to the sub-program. P2
6050** contains the address to jump to if the sub-program throws an IGNORE
6051** exception using the RAISE() function. Register P3 contains the address
6052** of a memory cell in this (the parent) VM that is used to allocate the
6053** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006054**
6055** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006056**
6057** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006058*/
dan76d462e2009-08-30 11:42:51 +00006059case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006060 int nMem; /* Number of memory registers for sub-program */
6061 int nByte; /* Bytes of runtime space required for sub-program */
6062 Mem *pRt; /* Register to allocate runtime space */
6063 Mem *pMem; /* Used to iterate through memory cells */
6064 Mem *pEnd; /* Last memory cell in new array */
6065 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6066 SubProgram *pProgram; /* Sub-program to execute */
6067 void *t; /* Token identifying trigger */
6068
6069 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006070 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006071 assert( pProgram->nOp>0 );
6072
dan1da40a32009-09-19 17:00:31 +00006073 /* If the p5 flag is clear, then recursive invocation of triggers is
6074 ** disabled for backwards compatibility (p5 is set if this sub-program
6075 ** is really a trigger, not a foreign key action, and the flag set
6076 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006077 **
6078 ** It is recursive invocation of triggers, at the SQL level, that is
6079 ** disabled. In some cases a single trigger may generate more than one
6080 ** SubProgram (if the trigger may be executed with more than one different
6081 ** ON CONFLICT algorithm). SubProgram structures associated with a
6082 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006083 ** variable. */
6084 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006085 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006086 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6087 if( pFrame ) break;
6088 }
6089
danf5894502009-10-07 18:41:19 +00006090 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006091 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006092 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006093 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006094 }
6095
6096 /* Register pRt is used to store the memory required to save the state
6097 ** of the current program, and the memory required at runtime to execute
6098 ** the trigger program. If this trigger has been fired before, then pRt
6099 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006100 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006101 /* SubProgram.nMem is set to the number of memory cells used by the
6102 ** program stored in SubProgram.aOp. As well as these, one memory
6103 ** cell is required for each cursor used by the program. Set local
6104 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6105 */
dan65a7cd12009-09-01 12:16:01 +00006106 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006107 assert( nMem>0 );
6108 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006109 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006110 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006111 + pProgram->nCsr * sizeof(VdbeCursor*)
6112 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006113 pFrame = sqlite3DbMallocZero(db, nByte);
6114 if( !pFrame ){
6115 goto no_mem;
6116 }
6117 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006118 pRt->flags = MEM_Blob|MEM_Dyn;
6119 pRt->z = (char*)pFrame;
6120 pRt->n = nByte;
6121 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006122
6123 pFrame->v = p;
6124 pFrame->nChildMem = nMem;
6125 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006126 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006127 pFrame->aMem = p->aMem;
6128 pFrame->nMem = p->nMem;
6129 pFrame->apCsr = p->apCsr;
6130 pFrame->nCursor = p->nCursor;
6131 pFrame->aOp = p->aOp;
6132 pFrame->nOp = p->nOp;
6133 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006134#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006135 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006136#endif
drh72f56ef2018-08-29 18:47:22 +00006137#ifdef SQLITE_DEBUG
6138 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
6139#endif
dan165921a2009-08-28 18:53:45 +00006140
6141 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6142 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006143 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006144 pMem->db = db;
6145 }
6146 }else{
drh72f56ef2018-08-29 18:47:22 +00006147 pFrame = (VdbeFrame*)pRt->z;
6148 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00006149 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6150 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006151 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006152 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006153 }
6154
6155 p->nFrame++;
6156 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006157 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006158 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006159 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006160 assert( pFrame->pAuxData==0 );
6161 pFrame->pAuxData = p->pAuxData;
6162 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006163 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006164 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006165 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006166 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006167 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006168 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006169 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006170 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006171 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006172 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006173#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006174 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006175#endif
drhb2e61bc2019-01-25 19:29:01 +00006176#ifdef SQLITE_DEBUG
6177 /* Verify that second and subsequent executions of the same trigger do not
6178 ** try to reuse register values from the first use. */
6179 {
6180 int i;
6181 for(i=0; i<p->nMem; i++){
6182 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
6183 aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
6184 }
6185 }
6186#endif
drhf56fa462015-04-13 21:39:54 +00006187 pOp = &aOp[-1];
dan165921a2009-08-28 18:53:45 +00006188
6189 break;
6190}
6191
dan76d462e2009-08-30 11:42:51 +00006192/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006193**
dan76d462e2009-08-30 11:42:51 +00006194** This opcode is only ever present in sub-programs called via the
6195** OP_Program instruction. Copy a value currently stored in a memory
6196** cell of the calling (parent) frame to cell P2 in the current frames
6197** address space. This is used by trigger programs to access the new.*
6198** and old.* values.
dan165921a2009-08-28 18:53:45 +00006199**
dan76d462e2009-08-30 11:42:51 +00006200** The address of the cell in the parent frame is determined by adding
6201** the value of the P1 argument to the value of the P1 argument to the
6202** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006203*/
drh27a348c2015-04-13 19:14:06 +00006204case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006205 VdbeFrame *pFrame;
6206 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006207 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006208 pFrame = p->pFrame;
6209 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006210 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6211 break;
6212}
6213
danielk197793758c82005-01-21 08:13:14 +00006214#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006215
dan1da40a32009-09-19 17:00:31 +00006216#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006217/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006218** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006219**
dan0ff297e2009-09-25 17:03:14 +00006220** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6221** If P1 is non-zero, the database constraint counter is incremented
6222** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006223** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006224*/
dan32b09f22009-09-23 17:29:59 +00006225case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006226 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006227 db->nDeferredImmCons += pOp->p2;
6228 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006229 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006230 }else{
dan0ff297e2009-09-25 17:03:14 +00006231 p->nFkConstraint += pOp->p2;
6232 }
6233 break;
6234}
6235
6236/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006237** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006238**
6239** This opcode tests if a foreign key constraint-counter is currently zero.
6240** If so, jump to instruction P2. Otherwise, fall through to the next
6241** instruction.
6242**
6243** If P1 is non-zero, then the jump is taken if the database constraint-counter
6244** is zero (the one that counts deferred constraint violations). If P1 is
6245** zero, the jump is taken if the statement constraint-counter is zero
6246** (immediate foreign key constraint violations).
6247*/
6248case OP_FkIfZero: { /* jump */
6249 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006250 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006251 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006252 }else{
drh688852a2014-02-17 22:40:43 +00006253 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006254 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006255 }
dan1da40a32009-09-19 17:00:31 +00006256 break;
6257}
6258#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6259
drh205f48e2004-11-05 00:43:11 +00006260#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006261/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006262** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006263**
dan76d462e2009-08-30 11:42:51 +00006264** P1 is a register in the root frame of this VM (the root frame is
6265** different from the current frame if this instruction is being executed
6266** within a sub-program). Set the value of register P1 to the maximum of
6267** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006268**
6269** This instruction throws an error if the memory cell is not initially
6270** an integer.
6271*/
dan76d462e2009-08-30 11:42:51 +00006272case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006273 VdbeFrame *pFrame;
6274 if( p->pFrame ){
6275 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6276 pIn1 = &pFrame->aMem[pOp->p1];
6277 }else{
drha6c2ed92009-11-14 23:22:23 +00006278 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006279 }
drh2b4ded92010-09-27 21:09:31 +00006280 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006281 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006282 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006283 sqlite3VdbeMemIntegerify(pIn2);
6284 if( pIn1->u.i<pIn2->u.i){
6285 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006286 }
6287 break;
6288}
6289#endif /* SQLITE_OMIT_AUTOINCREMENT */
6290
drh8b0cf382015-10-06 21:07:06 +00006291/* Opcode: IfPos P1 P2 P3 * *
6292** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006293**
drh16897072015-03-07 00:57:37 +00006294** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006295** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006296** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006297**
drh16897072015-03-07 00:57:37 +00006298** If the initial value of register P1 is less than 1, then the
6299** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006300*/
drh9cbf3422008-01-17 16:22:13 +00006301case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006302 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006303 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006304 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006305 if( pIn1->u.i>0 ){
6306 pIn1->u.i -= pOp->p3;
6307 goto jump_to_p2;
6308 }
drhec7429a2005-10-06 16:53:14 +00006309 break;
6310}
6311
drhcc2fa4c2016-01-25 15:57:29 +00006312/* Opcode: OffsetLimit P1 P2 P3 * *
6313** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006314**
drhcc2fa4c2016-01-25 15:57:29 +00006315** This opcode performs a commonly used computation associated with
6316** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6317** holds the offset counter. The opcode computes the combined value
6318** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6319** value computed is the total number of rows that will need to be
6320** visited in order to complete the query.
6321**
6322** If r[P3] is zero or negative, that means there is no OFFSET
6323** and r[P2] is set to be the value of the LIMIT, r[P1].
6324**
6325** if r[P1] is zero or negative, that means there is no LIMIT
6326** and r[P2] is set to -1.
6327**
6328** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006329*/
drhcc2fa4c2016-01-25 15:57:29 +00006330case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006331 i64 x;
drh3c657212009-11-17 23:59:58 +00006332 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006333 pIn3 = &aMem[pOp->p3];
6334 pOut = out2Prerelease(p, pOp);
6335 assert( pIn1->flags & MEM_Int );
6336 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006337 x = pIn1->u.i;
6338 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6339 /* If the LIMIT is less than or equal to zero, loop forever. This
6340 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6341 ** also loop forever. This is undocumented. In fact, one could argue
6342 ** that the loop should terminate. But assuming 1 billion iterations
6343 ** per second (far exceeding the capabilities of any current hardware)
6344 ** it would take nearly 300 years to actually reach the limit. So
6345 ** looping forever is a reasonable approximation. */
6346 pOut->u.i = -1;
6347 }else{
6348 pOut->u.i = x;
6349 }
drh15007a92006-01-08 18:10:17 +00006350 break;
6351}
6352
drhf99dd352016-12-18 17:42:00 +00006353/* Opcode: IfNotZero P1 P2 * * *
6354** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006355**
drh16897072015-03-07 00:57:37 +00006356** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006357** initially greater than zero, then decrement the value in register P1.
6358** If it is non-zero (negative or positive) and then also jump to P2.
6359** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006360*/
drh16897072015-03-07 00:57:37 +00006361case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006362 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006363 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006364 VdbeBranchTaken(pIn1->u.i<0, 2);
6365 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006366 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006367 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006368 }
6369 break;
6370}
6371
6372/* Opcode: DecrJumpZero P1 P2 * * *
6373** Synopsis: if (--r[P1])==0 goto P2
6374**
drhab5be2e2016-11-30 05:08:59 +00006375** Register P1 must hold an integer. Decrement the value in P1
6376** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006377*/
6378case OP_DecrJumpZero: { /* jump, in1 */
6379 pIn1 = &aMem[pOp->p1];
6380 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006381 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6382 VdbeBranchTaken(pIn1->u.i==0, 2);
6383 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006384 break;
6385}
6386
drh16897072015-03-07 00:57:37 +00006387
drh8f26da62018-07-05 21:22:57 +00006388/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006389** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006390**
drh8f26da62018-07-05 21:22:57 +00006391** Execute the xStep function for an aggregate.
6392** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006393** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006394** accumulator.
drhe5095352002-02-24 03:25:14 +00006395**
drh98757152008-01-09 23:04:12 +00006396** The P5 arguments are taken from register P2 and its
6397** successors.
drhe5095352002-02-24 03:25:14 +00006398*/
drh8f26da62018-07-05 21:22:57 +00006399/* Opcode: AggInverse * P2 P3 P4 P5
6400** Synopsis: accum=r[P3] inverse(r[P2@P5])
6401**
6402** Execute the xInverse function for an aggregate.
6403** The function has P5 arguments. P4 is a pointer to the
6404** FuncDef structure that specifies the function. Register P3 is the
6405** accumulator.
6406**
6407** The P5 arguments are taken from register P2 and its
6408** successors.
6409*/
6410/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006411** Synopsis: accum=r[P3] step(r[P2@P5])
6412**
dan9a947222018-06-14 19:06:36 +00006413** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6414** aggregate. The function has P5 arguments. P4 is a pointer to the
6415** FuncDef structure that specifies the function. Register P3 is the
6416** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006417**
6418** The P5 arguments are taken from register P2 and its
6419** successors.
6420**
6421** This opcode is initially coded as OP_AggStep0. On first evaluation,
6422** the FuncDef stored in P4 is converted into an sqlite3_context and
6423** the opcode is changed. In this way, the initialization of the
6424** sqlite3_context only happens once, instead of on each call to the
6425** step function.
6426*/
drh8f26da62018-07-05 21:22:57 +00006427case OP_AggInverse:
6428case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006429 int n;
drh9c7c9132015-06-26 18:16:52 +00006430 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006431
drh9c7c9132015-06-26 18:16:52 +00006432 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006433 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006434 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6435 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006436 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006437 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6438 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006439 if( pCtx==0 ) goto no_mem;
6440 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006441 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6442 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006443 pCtx->pFunc = pOp->p4.pFunc;
6444 pCtx->iOp = (int)(pOp - aOp);
6445 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006446 pCtx->skipFlag = 0;
6447 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006448 pCtx->argc = n;
6449 pOp->p4type = P4_FUNCCTX;
6450 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006451
6452 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006453 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006454
drh8f26da62018-07-05 21:22:57 +00006455 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006456 /* Fall through into OP_AggStep */
6457}
drh8f26da62018-07-05 21:22:57 +00006458case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006459 int i;
6460 sqlite3_context *pCtx;
6461 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006462
6463 assert( pOp->p4type==P4_FUNCCTX );
6464 pCtx = pOp->p4.pCtx;
6465 pMem = &aMem[pOp->p3];
6466
drh2c885d02018-07-07 19:36:04 +00006467#ifdef SQLITE_DEBUG
6468 if( pOp->p1 ){
6469 /* This is an OP_AggInverse call. Verify that xStep has always
6470 ** been called at least once prior to any xInverse call. */
6471 assert( pMem->uTemp==0x1122e0e3 );
6472 }else{
6473 /* This is an OP_AggStep call. Mark it as such. */
6474 pMem->uTemp = 0x1122e0e3;
6475 }
6476#endif
6477
drh9c7c9132015-06-26 18:16:52 +00006478 /* If this function is inside of a trigger, the register array in aMem[]
6479 ** might change from one evaluation to the next. The next block of code
6480 ** checks to see if the register array has changed, and if so it
6481 ** reinitializes the relavant parts of the sqlite3_context object */
6482 if( pCtx->pMem != pMem ){
6483 pCtx->pMem = pMem;
6484 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6485 }
6486
6487#ifdef SQLITE_DEBUG
6488 for(i=0; i<pCtx->argc; i++){
6489 assert( memIsValid(pCtx->argv[i]) );
6490 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6491 }
6492#endif
6493
drhabfcea22005-09-06 20:36:48 +00006494 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006495 assert( pCtx->pOut->flags==MEM_Null );
6496 assert( pCtx->isError==0 );
6497 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006498#ifndef SQLITE_OMIT_WINDOWFUNC
6499 if( pOp->p1 ){
6500 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6501 }else
6502#endif
6503 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6504
drhf09ac0b2018-01-23 03:44:06 +00006505 if( pCtx->isError ){
6506 if( pCtx->isError>0 ){
6507 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006508 rc = pCtx->isError;
6509 }
drhf09ac0b2018-01-23 03:44:06 +00006510 if( pCtx->skipFlag ){
6511 assert( pOp[-1].opcode==OP_CollSeq );
6512 i = pOp[-1].p1;
6513 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6514 pCtx->skipFlag = 0;
6515 }
6516 sqlite3VdbeMemRelease(pCtx->pOut);
6517 pCtx->pOut->flags = MEM_Null;
6518 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006519 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006520 }
drhf09ac0b2018-01-23 03:44:06 +00006521 assert( pCtx->pOut->flags==MEM_Null );
6522 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006523 break;
6524}
6525
drh8f26da62018-07-05 21:22:57 +00006526/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006527** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006528**
dan9a947222018-06-14 19:06:36 +00006529** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00006530** or window function. Execute the finalizer function
6531** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00006532**
6533** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006534** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006535** argument is not used by this opcode. It is only there to disambiguate
6536** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00006537** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00006538** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006539*/
drh8f26da62018-07-05 21:22:57 +00006540/* Opcode: AggValue * P2 P3 P4 *
6541** Synopsis: r[P3]=value N=P2
6542**
6543** Invoke the xValue() function and store the result in register P3.
6544**
6545** P2 is the number of arguments that the step function takes and
6546** P4 is a pointer to the FuncDef for this function. The P2
6547** argument is not used by this opcode. It is only there to disambiguate
6548** functions that can take varying numbers of arguments. The
6549** P4 argument is only needed for the case where
6550** the step function was not previously called.
6551*/
6552case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00006553case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006554 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006555 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00006556 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00006557 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006558 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006559#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006560 if( pOp->p3 ){
6561 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006562 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006563 }else
6564#endif
drh8f26da62018-07-05 21:22:57 +00006565 {
6566 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6567 }
dan67a9b8e2018-06-22 20:51:35 +00006568
drh4c8555f2009-06-25 01:47:11 +00006569 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006570 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006571 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006572 }
drh2dca8682008-03-21 17:13:13 +00006573 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006574 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006575 if( sqlite3VdbeMemTooBig(pMem) ){
6576 goto too_big;
6577 }
drh5e00f6c2001-09-13 13:46:56 +00006578 break;
6579}
6580
dan5cf53532010-05-01 16:40:20 +00006581#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006582/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006583**
6584** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006585** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6586** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006587** SQLITE_BUSY or not, respectively. Write the number of pages in the
6588** WAL after the checkpoint into mem[P3+1] and the number of pages
6589** in the WAL that have been checkpointed after the checkpoint
6590** completes into mem[P3+2]. However on an error, mem[P3+1] and
6591** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006592*/
6593case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006594 int i; /* Loop counter */
6595 int aRes[3]; /* Results */
6596 Mem *pMem; /* Write results here */
6597
drh9e92a472013-06-27 17:40:30 +00006598 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006599 aRes[0] = 0;
6600 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006601 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6602 || pOp->p2==SQLITE_CHECKPOINT_FULL
6603 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006604 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006605 );
drh30aa3b92011-02-07 23:56:01 +00006606 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006607 if( rc ){
6608 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006609 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006610 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006611 }
drh30aa3b92011-02-07 23:56:01 +00006612 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6613 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6614 }
dan7c246102010-04-12 19:00:29 +00006615 break;
6616};
dan5cf53532010-05-01 16:40:20 +00006617#endif
drh5e00f6c2001-09-13 13:46:56 +00006618
drhcac29a62010-07-02 19:36:52 +00006619#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006620/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006621**
6622** Change the journal mode of database P1 to P3. P3 must be one of the
6623** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6624** modes (delete, truncate, persist, off and memory), this is a simple
6625** operation. No IO is required.
6626**
6627** If changing into or out of WAL mode the procedure is more complicated.
6628**
6629** Write a string containing the final journal-mode to register P2.
6630*/
drh27a348c2015-04-13 19:14:06 +00006631case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006632 Btree *pBt; /* Btree to change journal mode of */
6633 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006634 int eNew; /* New journal mode */
6635 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006636#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006637 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006638#endif
dane04dc882010-04-20 18:53:15 +00006639
drh27a348c2015-04-13 19:14:06 +00006640 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006641 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006642 assert( eNew==PAGER_JOURNALMODE_DELETE
6643 || eNew==PAGER_JOURNALMODE_TRUNCATE
6644 || eNew==PAGER_JOURNALMODE_PERSIST
6645 || eNew==PAGER_JOURNALMODE_OFF
6646 || eNew==PAGER_JOURNALMODE_MEMORY
6647 || eNew==PAGER_JOURNALMODE_WAL
6648 || eNew==PAGER_JOURNALMODE_QUERY
6649 );
6650 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006651 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006652
dane04dc882010-04-20 18:53:15 +00006653 pBt = db->aDb[pOp->p1].pBt;
6654 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006655 eOld = sqlite3PagerGetJournalMode(pPager);
6656 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6657 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006658
6659#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006660 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006661
drhd80b2332010-05-01 00:59:37 +00006662 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006663 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006664 */
6665 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006666 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006667 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006668 ){
drh0b9b4302010-06-11 17:01:24 +00006669 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006670 }
6671
drh0b9b4302010-06-11 17:01:24 +00006672 if( (eNew!=eOld)
6673 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6674 ){
danc0537fe2013-06-28 19:41:43 +00006675 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006676 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006677 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006678 "cannot change %s wal mode from within a transaction",
6679 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6680 );
drh9467abf2016-02-17 18:44:11 +00006681 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006682 }else{
6683
6684 if( eOld==PAGER_JOURNALMODE_WAL ){
6685 /* If leaving WAL mode, close the log file. If successful, the call
6686 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6687 ** file. An EXCLUSIVE lock may still be held on the database file
6688 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006689 */
dan7fb89902016-08-12 16:21:15 +00006690 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006691 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006692 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006693 }
drh242c4f72010-06-22 14:49:39 +00006694 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6695 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6696 ** as an intermediate */
6697 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006698 }
6699
6700 /* Open a transaction on the database file. Regardless of the journal
6701 ** mode, this transaction always uses a rollback journal.
6702 */
6703 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6704 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006705 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006706 }
6707 }
6708 }
dan5cf53532010-05-01 16:40:20 +00006709#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006710
drh9467abf2016-02-17 18:44:11 +00006711 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006712 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006713
dane04dc882010-04-20 18:53:15 +00006714 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006715 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006716 pOut->n = sqlite3Strlen30(pOut->z);
6717 pOut->enc = SQLITE_UTF8;
6718 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006719 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006720 break;
drhcac29a62010-07-02 19:36:52 +00006721};
6722#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006723
drhfdbcdee2007-03-27 14:44:50 +00006724#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00006725/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00006726**
drh9ef5e772016-08-19 14:20:56 +00006727** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6728** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00006729**
drh2f6239e2018-12-08 00:43:08 +00006730** If P2 is not zero, then it is a register holding a string which is
6731** the file into which the result of vacuum should be written. When
6732** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00006733*/
drh9cbf3422008-01-17 16:22:13 +00006734case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006735 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00006736 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
6737 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00006738 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006739 break;
6740}
drh154d4b22006-09-21 11:02:16 +00006741#endif
drh6f8c91c2003-12-07 00:24:35 +00006742
danielk1977dddbcdc2007-04-26 14:42:34 +00006743#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006744/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006745**
6746** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006747** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006748** P2. Otherwise, fall through to the next instruction.
6749*/
drh9cbf3422008-01-17 16:22:13 +00006750case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006751 Btree *pBt;
6752
6753 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006754 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006755 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006756 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006757 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006758 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006759 if( rc ){
6760 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006761 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006762 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006763 }
6764 break;
6765}
6766#endif
6767
drhba968db2018-07-24 22:02:12 +00006768/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006769**
drh25df48d2014-07-22 14:58:12 +00006770** Cause precompiled statements to expire. When an expired statement
6771** is executed using sqlite3_step() it will either automatically
6772** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6773** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006774**
6775** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006776** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00006777**
6778** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
6779** then running SQL statements are allowed to continue to run to completion.
6780** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
6781** that might help the statement run faster but which does not affect the
6782** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00006783*/
drh9cbf3422008-01-17 16:22:13 +00006784case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00006785 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00006786 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00006787 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00006788 }else{
drhba968db2018-07-24 22:02:12 +00006789 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00006790 }
6791 break;
6792}
6793
danielk1977c00da102006-01-07 13:21:04 +00006794#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006795/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006796** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006797**
6798** Obtain a lock on a particular table. This instruction is only used when
6799** the shared-cache feature is enabled.
6800**
danielk197796d48e92009-06-29 06:00:37 +00006801** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006802** on which the lock is acquired. A readlock is obtained if P3==0 or
6803** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006804**
6805** P2 contains the root-page of the table to lock.
6806**
drh66a51672008-01-03 00:01:23 +00006807** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006808** used to generate an error message if the lock cannot be obtained.
6809*/
drh9cbf3422008-01-17 16:22:13 +00006810case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006811 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006812 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006813 int p1 = pOp->p1;
6814 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006815 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006816 assert( isWriteLock==0 || isWriteLock==1 );
6817 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006818 if( rc ){
6819 if( (rc&0xFF)==SQLITE_LOCKED ){
6820 const char *z = pOp->p4.z;
6821 sqlite3VdbeError(p, "database table is locked: %s", z);
6822 }
6823 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006824 }
danielk1977c00da102006-01-07 13:21:04 +00006825 }
6826 break;
6827}
drhb9bb7c12006-06-11 23:41:55 +00006828#endif /* SQLITE_OMIT_SHARED_CACHE */
6829
6830#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006831/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006832**
danielk19773e3a84d2008-08-01 17:37:40 +00006833** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6834** xBegin method for that table.
6835**
6836** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006837** within a callback to a virtual table xSync() method. If it is, the error
6838** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006839*/
drh9cbf3422008-01-17 16:22:13 +00006840case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006841 VTable *pVTab;
6842 pVTab = pOp->p4.pVtab;
6843 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006844 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006845 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006846 break;
6847}
6848#endif /* SQLITE_OMIT_VIRTUALTABLE */
6849
6850#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006851/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006852**
dan73779452015-03-19 18:56:17 +00006853** P2 is a register that holds the name of a virtual table in database
6854** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006855*/
drh9cbf3422008-01-17 16:22:13 +00006856case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006857 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006858 const char *zTab; /* Name of the virtual table */
6859
dan73779452015-03-19 18:56:17 +00006860 memset(&sMem, 0, sizeof(sMem));
6861 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006862 /* Because P2 is always a static string, it is impossible for the
6863 ** sqlite3VdbeMemCopy() to fail */
6864 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6865 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006866 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006867 assert( rc==SQLITE_OK );
6868 zTab = (const char*)sqlite3_value_text(&sMem);
6869 assert( zTab || db->mallocFailed );
6870 if( zTab ){
6871 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006872 }
6873 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006874 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006875 break;
6876}
6877#endif /* SQLITE_OMIT_VIRTUALTABLE */
6878
6879#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006880/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006881**
drh66a51672008-01-03 00:01:23 +00006882** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006883** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006884*/
drh9cbf3422008-01-17 16:22:13 +00006885case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006886 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006887 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006888 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00006889 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00006890 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006891 break;
6892}
6893#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006894
drh9eff6162006-06-12 21:59:13 +00006895#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006896/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006897**
drh66a51672008-01-03 00:01:23 +00006898** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006899** P1 is a cursor number. This opcode opens a cursor to the virtual
6900** table and stores that cursor in P1.
6901*/
drh9cbf3422008-01-17 16:22:13 +00006902case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006903 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006904 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006905 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006906 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006907
drh1713afb2013-06-28 01:24:57 +00006908 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006909 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006910 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006911 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006912 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6913 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006914 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006915 }
6916 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006917 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006918 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006919 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006920
drh9467abf2016-02-17 18:44:11 +00006921 /* Initialize sqlite3_vtab_cursor base class */
6922 pVCur->pVtab = pVtab;
6923
6924 /* Initialize vdbe cursor object */
6925 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6926 if( pCur ){
6927 pCur->uc.pVCur = pVCur;
6928 pVtab->nRef++;
6929 }else{
6930 assert( db->mallocFailed );
6931 pModule->xClose(pVCur);
6932 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006933 }
drh9eff6162006-06-12 21:59:13 +00006934 break;
6935}
6936#endif /* SQLITE_OMIT_VIRTUALTABLE */
6937
6938#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006939/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006940** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006941**
6942** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6943** the filtered result set is empty.
6944**
drh66a51672008-01-03 00:01:23 +00006945** P4 is either NULL or a string that was generated by the xBestIndex
6946** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006947** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006948**
drh9eff6162006-06-12 21:59:13 +00006949** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006950** by P1. The integer query plan parameter to xFilter is stored in register
6951** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006952** xFilter method. Registers P3+2..P3+1+argc are the argc
6953** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006954** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006955**
danielk19776dbee812008-01-03 18:39:41 +00006956** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006957*/
drh9cbf3422008-01-17 16:22:13 +00006958case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006959 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006960 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006961 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006962 Mem *pQuery;
6963 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006964 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006965 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006966 VdbeCursor *pCur;
6967 int res;
6968 int i;
6969 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006970
drha6c2ed92009-11-14 23:22:23 +00006971 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006972 pArgc = &pQuery[1];
6973 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006974 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006975 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006976 assert( pCur->eCurType==CURTYPE_VTAB );
6977 pVCur = pCur->uc.pVCur;
6978 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006979 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006980
drh9cbf3422008-01-17 16:22:13 +00006981 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006982 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006983 nArg = (int)pArgc->u.i;
6984 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006985
drh644a5292006-12-20 14:53:38 +00006986 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006987 res = 0;
6988 apArg = p->apArg;
6989 for(i = 0; i<nArg; i++){
6990 apArg[i] = &pArgc[i+1];
6991 }
drhc960dcb2015-11-20 19:22:01 +00006992 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006993 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006994 if( rc ) goto abort_due_to_error;
6995 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006996 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006997 VdbeBranchTaken(res!=0,2);
6998 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006999 break;
7000}
7001#endif /* SQLITE_OMIT_VIRTUALTABLE */
7002
7003#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00007004/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007005** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007006**
drh6f390be2018-01-11 17:04:26 +00007007** Store in register P3 the value of the P2-th column of
7008** the current row of the virtual-table of cursor P1.
7009**
7010** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007011** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007012** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7013** function to return true inside the xColumn method of the virtual
7014** table implementation. The P5 column might also contain other
7015** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7016** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007017*/
7018case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007019 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007020 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007021 Mem *pDest;
7022 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007023
drhdfe88ec2008-11-03 20:55:06 +00007024 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007025 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007026 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007027 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007028 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007029 if( pCur->nullRow ){
7030 sqlite3VdbeMemSetNull(pDest);
7031 break;
7032 }
drhc960dcb2015-11-20 19:22:01 +00007033 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007034 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007035 assert( pModule->xColumn );
7036 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007037 sContext.pOut = pDest;
drh09d00b22018-09-27 20:20:01 +00007038 testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
7039 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007040 sqlite3VdbeMemSetNull(pDest);
7041 pDest->flags = MEM_Null|MEM_Zero;
7042 pDest->u.nZero = 0;
7043 }else{
7044 MemSetTypeFlag(pDest, MEM_Null);
7045 }
drhc960dcb2015-11-20 19:22:01 +00007046 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007047 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007048 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007049 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007050 rc = sContext.isError;
7051 }
drh9bd038f2014-08-27 14:14:06 +00007052 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007053 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007054 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007055
drhde4fcfd2008-01-19 23:50:26 +00007056 if( sqlite3VdbeMemTooBig(pDest) ){
7057 goto too_big;
7058 }
drh9467abf2016-02-17 18:44:11 +00007059 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007060 break;
7061}
7062#endif /* SQLITE_OMIT_VIRTUALTABLE */
7063
7064#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007065/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007066**
7067** Advance virtual table P1 to the next row in its result set and
7068** jump to instruction P2. Or, if the virtual table has reached
7069** the end of its result set, then fall through to the next instruction.
7070*/
drh9cbf3422008-01-17 16:22:13 +00007071case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007072 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007073 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007074 int res;
drh856c1032009-06-02 15:21:42 +00007075 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007076
drhc54a6172009-06-02 16:06:03 +00007077 res = 0;
drh856c1032009-06-02 15:21:42 +00007078 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007079 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007080 if( pCur->nullRow ){
7081 break;
7082 }
drhc960dcb2015-11-20 19:22:01 +00007083 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007084 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007085 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007086
drhde4fcfd2008-01-19 23:50:26 +00007087 /* Invoke the xNext() method of the module. There is no way for the
7088 ** underlying implementation to return an error if one occurs during
7089 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7090 ** data is available) and the error code returned when xColumn or
7091 ** some other method is next invoked on the save virtual table cursor.
7092 */
drhc960dcb2015-11-20 19:22:01 +00007093 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007094 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007095 if( rc ) goto abort_due_to_error;
7096 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007097 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007098 if( !res ){
7099 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007100 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007101 }
drh49afe3a2013-07-10 03:05:14 +00007102 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007103}
7104#endif /* SQLITE_OMIT_VIRTUALTABLE */
7105
danielk1977182c4ba2007-06-27 15:53:34 +00007106#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007107/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007108**
drh66a51672008-01-03 00:01:23 +00007109** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007110** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007111** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007112*/
drh9cbf3422008-01-17 16:22:13 +00007113case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007114 sqlite3_vtab *pVtab;
7115 Mem *pName;
dan34566c42018-09-20 17:21:21 +00007116 int isLegacy;
7117
7118 isLegacy = (db->flags & SQLITE_LegacyAlter);
7119 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00007120 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007121 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007122 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007123 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007124 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007125 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007126 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007127 testcase( pName->enc==SQLITE_UTF8 );
7128 testcase( pName->enc==SQLITE_UTF16BE );
7129 testcase( pName->enc==SQLITE_UTF16LE );
7130 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007131 if( rc ) goto abort_due_to_error;
7132 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00007133 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00007134 sqlite3VtabImportErrmsg(p, pVtab);
7135 p->expired = 0;
7136 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007137 break;
7138}
7139#endif
drh4cbdda92006-06-14 19:00:20 +00007140
7141#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007142/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007143** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007144**
drh66a51672008-01-03 00:01:23 +00007145** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007146** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007147** are contiguous memory cells starting at P3 to pass to the xUpdate
7148** invocation. The value in register (P3+P2-1) corresponds to the
7149** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007150**
7151** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007152** The argv[0] element (which corresponds to memory cell P3)
7153** is the rowid of a row to delete. If argv[0] is NULL then no
7154** deletion occurs. The argv[1] element is the rowid of the new
7155** row. This can be NULL to have the virtual table select the new
7156** rowid for itself. The subsequent elements in the array are
7157** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007158**
7159** If P2==1 then no insert is performed. argv[0] is the rowid of
7160** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007161**
7162** P1 is a boolean flag. If it is set to true and the xUpdate call
7163** is successful, then the value returned by sqlite3_last_insert_rowid()
7164** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007165**
7166** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7167** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007168*/
drh9cbf3422008-01-17 16:22:13 +00007169case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007170 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007171 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007172 int nArg;
7173 int i;
7174 sqlite_int64 rowid;
7175 Mem **apArg;
7176 Mem *pX;
7177
danb061d052011-04-25 18:49:57 +00007178 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7179 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7180 );
drh9e92a472013-06-27 17:40:30 +00007181 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007182 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007183 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007184 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007185 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7186 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007187 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007188 }
7189 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007190 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007191 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007192 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007193 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007194 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007195 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007196 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007197 assert( memIsValid(pX) );
7198 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007199 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007200 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007201 }
danb061d052011-04-25 18:49:57 +00007202 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007203 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007204 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007205 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007206 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007207 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007208 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007209 }
drhd91c1a12013-02-09 13:58:25 +00007210 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007211 if( pOp->p5==OE_Ignore ){
7212 rc = SQLITE_OK;
7213 }else{
7214 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7215 }
7216 }else{
7217 p->nChange++;
7218 }
drh9467abf2016-02-17 18:44:11 +00007219 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007220 }
drh4cbdda92006-06-14 19:00:20 +00007221 break;
danielk1977399918f2006-06-14 13:03:23 +00007222}
7223#endif /* SQLITE_OMIT_VIRTUALTABLE */
7224
danielk197759a93792008-05-15 17:48:20 +00007225#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7226/* Opcode: Pagecount P1 P2 * * *
7227**
7228** Write the current number of pages in database P1 to memory cell P2.
7229*/
drh27a348c2015-04-13 19:14:06 +00007230case OP_Pagecount: { /* out2 */
7231 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007232 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007233 break;
7234}
7235#endif
7236
drh60ac3f42010-11-23 18:59:27 +00007237
7238#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7239/* Opcode: MaxPgcnt P1 P2 P3 * *
7240**
7241** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007242** Do not let the maximum page count fall below the current page count and
7243** do not change the maximum page count value if P3==0.
7244**
drh60ac3f42010-11-23 18:59:27 +00007245** Store the maximum page count after the change in register P2.
7246*/
drh27a348c2015-04-13 19:14:06 +00007247case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007248 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007249 Btree *pBt;
7250
drh27a348c2015-04-13 19:14:06 +00007251 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007252 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007253 newMax = 0;
7254 if( pOp->p3 ){
7255 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007256 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007257 }
7258 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007259 break;
7260}
7261#endif
7262
drh3e34eab2017-07-19 19:48:40 +00007263/* Opcode: Function0 P1 P2 P3 P4 P5
7264** Synopsis: r[P3]=func(r[P2@P5])
7265**
7266** Invoke a user function (P4 is a pointer to a FuncDef object that
7267** defines the function) with P5 arguments taken from register P2 and
7268** successors. The result of the function is stored in register P3.
7269** Register P3 must not be one of the function inputs.
7270**
7271** P1 is a 32-bit bitmask indicating whether or not each argument to the
7272** function was determined to be constant at compile time. If the first
7273** argument was constant then bit 0 of P1 is set. This is used to determine
7274** whether meta data associated with a user function argument using the
7275** sqlite3_set_auxdata() API may be safely retained until the next
7276** invocation of this opcode.
7277**
7278** See also: Function, AggStep, AggFinal
7279*/
7280/* Opcode: Function P1 P2 P3 P4 P5
7281** Synopsis: r[P3]=func(r[P2@P5])
7282**
7283** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7284** contains a pointer to the function to be run) with P5 arguments taken
7285** from register P2 and successors. The result of the function is stored
7286** in register P3. Register P3 must not be one of the function inputs.
7287**
7288** P1 is a 32-bit bitmask indicating whether or not each argument to the
7289** function was determined to be constant at compile time. If the first
7290** argument was constant then bit 0 of P1 is set. This is used to determine
7291** whether meta data associated with a user function argument using the
7292** sqlite3_set_auxdata() API may be safely retained until the next
7293** invocation of this opcode.
7294**
7295** SQL functions are initially coded as OP_Function0 with P4 pointing
7296** to a FuncDef object. But on first evaluation, the P4 operand is
7297** automatically converted into an sqlite3_context object and the operation
7298** changed to this OP_Function opcode. In this way, the initialization of
7299** the sqlite3_context object occurs only once, rather than once for each
7300** evaluation of the function.
7301**
7302** See also: Function0, AggStep, AggFinal
7303*/
mistachkin758784d2018-07-25 15:12:29 +00007304case OP_PureFunc0: /* group */
7305case OP_Function0: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007306 int n;
7307 sqlite3_context *pCtx;
7308
7309 assert( pOp->p4type==P4_FUNCDEF );
7310 n = pOp->p5;
7311 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7312 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
7313 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
7314 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7315 if( pCtx==0 ) goto no_mem;
7316 pCtx->pOut = 0;
7317 pCtx->pFunc = pOp->p4.pFunc;
7318 pCtx->iOp = (int)(pOp - aOp);
7319 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007320 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007321 pCtx->argc = n;
7322 pOp->p4type = P4_FUNCCTX;
7323 pOp->p4.pCtx = pCtx;
7324 assert( OP_PureFunc == OP_PureFunc0+2 );
7325 assert( OP_Function == OP_Function0+2 );
7326 pOp->opcode += 2;
7327 /* Fall through into OP_Function */
7328}
mistachkin758784d2018-07-25 15:12:29 +00007329case OP_PureFunc: /* group */
7330case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007331 int i;
7332 sqlite3_context *pCtx;
7333
7334 assert( pOp->p4type==P4_FUNCCTX );
7335 pCtx = pOp->p4.pCtx;
7336
7337 /* If this function is inside of a trigger, the register array in aMem[]
7338 ** might change from one evaluation to the next. The next block of code
7339 ** checks to see if the register array has changed, and if so it
7340 ** reinitializes the relavant parts of the sqlite3_context object */
7341 pOut = &aMem[pOp->p3];
7342 if( pCtx->pOut != pOut ){
7343 pCtx->pOut = pOut;
7344 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7345 }
7346
7347 memAboutToChange(p, pOut);
7348#ifdef SQLITE_DEBUG
7349 for(i=0; i<pCtx->argc; i++){
7350 assert( memIsValid(pCtx->argv[i]) );
7351 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7352 }
7353#endif
7354 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007355 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007356 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7357
7358 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007359 if( pCtx->isError ){
7360 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007361 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7362 rc = pCtx->isError;
7363 }
7364 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007365 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007366 if( rc ) goto abort_due_to_error;
7367 }
7368
7369 /* Copy the result of the function into register P3 */
7370 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7371 sqlite3VdbeChangeEncoding(pOut, encoding);
7372 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7373 }
7374
7375 REGISTER_TRACE(pOp->p3, pOut);
7376 UPDATE_MAX_BLOBSIZE(pOut);
7377 break;
7378}
7379
drhf259df52017-12-27 20:38:35 +00007380/* Opcode: Trace P1 P2 * P4 *
7381**
7382** Write P4 on the statement trace output if statement tracing is
7383** enabled.
7384**
7385** Operand P1 must be 0x7fffffff and P2 must positive.
7386*/
drh74588ce2017-09-13 00:13:05 +00007387/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007388** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007389**
7390** Programs contain a single instance of this opcode as the very first
7391** opcode.
drh949f9cd2008-01-12 21:35:57 +00007392**
7393** If tracing is enabled (by the sqlite3_trace()) interface, then
7394** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007395** Or if P4 is blank, use the string returned by sqlite3_sql().
7396**
7397** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007398**
7399** Increment the value of P1 so that OP_Once opcodes will jump the
7400** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007401**
7402** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7403** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007404*/
drhf259df52017-12-27 20:38:35 +00007405case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007406case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007407 int i;
drhb9f47992018-01-24 12:14:43 +00007408#ifndef SQLITE_OMIT_TRACE
7409 char *zTrace;
7410#endif
drh5fe63bf2016-07-25 02:42:22 +00007411
7412 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7413 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7414 **
7415 ** This assert() provides evidence for:
7416 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7417 ** would have been returned by the legacy sqlite3_trace() interface by
7418 ** using the X argument when X begins with "--" and invoking
7419 ** sqlite3_expanded_sql(P) otherwise.
7420 */
7421 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007422
7423 /* OP_Init is always instruction 0 */
7424 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007425
drhaceb31b2014-02-08 01:40:27 +00007426#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007427 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007428 && !p->doingRerun
7429 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7430 ){
drh3d2a5292016-07-13 22:55:01 +00007431#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007432 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7433 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007434 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007435 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007436 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007437 }else
drh3d2a5292016-07-13 22:55:01 +00007438#endif
drh7adbcff2017-03-20 15:29:28 +00007439 if( db->nVdbeExec>1 ){
7440 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7441 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7442 sqlite3DbFree(db, z);
7443 }else{
drhbd441f72016-07-25 02:31:48 +00007444 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007445 }
drh949f9cd2008-01-12 21:35:57 +00007446 }
drh8f8b2312013-10-18 20:03:43 +00007447#ifdef SQLITE_USE_FCNTL_TRACE
7448 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7449 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007450 int j;
7451 for(j=0; j<db->nDb; j++){
7452 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7453 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007454 }
7455 }
7456#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007457#ifdef SQLITE_DEBUG
7458 if( (db->flags & SQLITE_SqlTrace)!=0
7459 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7460 ){
7461 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7462 }
7463#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007464#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007465 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007466 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007467 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007468 for(i=1; i<p->nOp; i++){
7469 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7470 }
7471 pOp->p1 = 0;
7472 }
7473 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007474 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007475 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007476}
drh949f9cd2008-01-12 21:35:57 +00007477
drh28935362013-12-07 20:39:19 +00007478#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007479/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007480**
7481** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007482** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7483** to values currently held in registers. TK_COLUMN terms in the P4
7484** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007485*/
7486case OP_CursorHint: {
7487 VdbeCursor *pC;
7488
7489 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7490 assert( pOp->p4type==P4_EXPR );
7491 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007492 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007493 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007494 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7495 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007496 }
drh28935362013-12-07 20:39:19 +00007497 break;
7498}
7499#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007500
drh4031baf2018-05-28 17:31:20 +00007501#ifdef SQLITE_DEBUG
7502/* Opcode: Abortable * * * * *
7503**
7504** Verify that an Abort can happen. Assert if an Abort at this point
7505** might cause database corruption. This opcode only appears in debugging
7506** builds.
7507**
7508** An Abort is safe if either there have been no writes, or if there is
7509** an active statement journal.
7510*/
7511case OP_Abortable: {
7512 sqlite3VdbeAssertAbortable(p);
7513 break;
7514}
7515#endif
7516
drh91fd4d42008-01-19 20:11:25 +00007517/* Opcode: Noop * * * * *
7518**
7519** Do nothing. This instruction is often useful as a jump
7520** destination.
drh5e00f6c2001-09-13 13:46:56 +00007521*/
drh91fd4d42008-01-19 20:11:25 +00007522/*
7523** The magic Explain opcode are only inserted when explain==2 (which
7524** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7525** This opcode records information from the optimizer. It is the
7526** the same as a no-op. This opcodesnever appears in a real VM program.
7527*/
drh4031baf2018-05-28 17:31:20 +00007528default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007529 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007530
drh5e00f6c2001-09-13 13:46:56 +00007531 break;
7532}
7533
7534/*****************************************************************************
7535** The cases of the switch statement above this line should all be indented
7536** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7537** readability. From this point on down, the normal indentation rules are
7538** restored.
7539*****************************************************************************/
7540 }
drh6e142f52000-06-08 13:36:40 +00007541
drh7b396862003-01-01 23:06:20 +00007542#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007543 {
drh35043cc2018-02-12 20:27:34 +00007544 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007545 if( endTime>start ) pOrigOp->cycles += endTime - start;
7546 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007547 }
drh7b396862003-01-01 23:06:20 +00007548#endif
7549
drh6e142f52000-06-08 13:36:40 +00007550 /* The following code adds nothing to the actual functionality
7551 ** of the program. It is only here for testing and debugging.
7552 ** On the other hand, it does burn CPU cycles every time through
7553 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7554 */
7555#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007556 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007557
drhcf1023c2007-05-08 20:59:49 +00007558#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007559 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007560 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007561 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007562 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007563 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007564 }
drh7cc84c22016-04-11 13:36:42 +00007565 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007566 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007567 }
drh75897232000-05-29 14:26:00 +00007568 }
danielk1977b5402fb2005-01-12 07:15:04 +00007569#endif /* SQLITE_DEBUG */
7570#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007571 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007572
drha05a7222008-01-19 03:35:58 +00007573 /* If we reach this point, it means that execution is finished with
7574 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007575 */
drh9467abf2016-02-17 18:44:11 +00007576abort_due_to_error:
7577 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007578 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007579 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7580 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7581 }
drha05a7222008-01-19 03:35:58 +00007582 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007583 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007584 testcase( sqlite3GlobalConfig.xLog!=0 );
7585 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007586 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007587 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007588 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007589 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007590 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007591 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007592 }
drh900b31e2007-08-28 02:27:51 +00007593
7594 /* This is the only way out of this procedure. We have to
7595 ** release the mutexes on btrees that were acquired at the
7596 ** top. */
7597vdbe_return:
drh77dfd5b2013-08-19 11:15:48 +00007598 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00007599 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007600 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007601 assert( rc!=SQLITE_OK || nExtraDelete==0
7602 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7603 );
drhb86ccfb2003-01-28 23:13:10 +00007604 return rc;
7605
drh023ae032007-05-08 12:12:16 +00007606 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7607 ** is encountered.
7608 */
7609too_big:
drh22c17b82015-05-15 04:13:15 +00007610 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007611 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007612 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007613
drh98640a32007-06-07 19:08:32 +00007614 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007615 */
7616no_mem:
drh4a642b62016-02-05 01:55:27 +00007617 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007618 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007619 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007620 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007621
danielk19776f8a5032004-05-10 10:34:51 +00007622 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007623 ** flag.
7624 */
7625abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007626 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007627 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007628 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007629 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007630 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007631}