blob: c8ee9860f830ea779db1269b406122752bacce53 [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*/
danielk1977be718892006-06-23 08:05:19 +0000243 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000244 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000245 }
drh322f2852014-09-19 00:43:39 +0000246 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000247 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000248 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000249 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000250 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000251 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000252 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000253 if( eCurType==CURTYPE_BTREE ){
254 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000255 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000256 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000257 }
danielk197794eb6a12005-12-15 15:22:08 +0000258 }
drh4774b132004-06-12 20:12:51 +0000259 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000260}
261
danielk19773d1bfea2004-05-14 11:00:53 +0000262/*
drh29d72102006-02-09 22:13:41 +0000263** Try to convert a value into a numeric representation if we can
264** do so without loss of information. In other words, if the string
265** looks like a number, convert it into a number. If it does not
266** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000267**
268** If the bTryForInt flag is true, then extra effort is made to give
269** an integer representation. Strings that look like floating point
270** values but which have no fractional component (example: '48.00')
271** will have a MEM_Int representation when bTryForInt is true.
272**
273** If bTryForInt is false, then if the input string contains a decimal
274** point or exponential notation, the result is only MEM_Real, even
275** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000276*/
drhbd9507c2014-08-23 17:21:37 +0000277static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000278 double rValue;
279 i64 iValue;
280 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000281 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000282 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
283 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
284 pRec->u.i = iValue;
285 pRec->flags |= MEM_Int;
286 }else{
drh74eaba42014-09-18 17:52:15 +0000287 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000288 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000289 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000290 }
drh06b3bd52018-02-01 01:13:33 +0000291 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
292 ** string representation after computing a numeric equivalent, because the
293 ** string representation might not be the canonical representation for the
294 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
295 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000296}
297
298/*
drh8a512562005-11-14 22:29:05 +0000299** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000300**
drh8a512562005-11-14 22:29:05 +0000301** SQLITE_AFF_INTEGER:
302** SQLITE_AFF_REAL:
303** SQLITE_AFF_NUMERIC:
304** Try to convert pRec to an integer representation or a
305** floating-point representation if an integer representation
306** is not possible. Note that the integer representation is
307** always preferred, even if the affinity is REAL, because
308** an integer representation is more space efficient on disk.
309**
310** SQLITE_AFF_TEXT:
311** Convert pRec to a text representation.
312**
drh05883a32015-06-02 15:32:08 +0000313** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000314** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000315*/
drh17435752007-08-16 04:30:38 +0000316static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000317 Mem *pRec, /* The value to apply affinity to */
318 char affinity, /* The affinity to be applied */
319 u8 enc /* Use this text encoding */
320){
drh7ea31cc2014-09-18 14:36:00 +0000321 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000322 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
323 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000324 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000325 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000326 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000327 }else{
328 sqlite3VdbeIntegerAffinity(pRec);
329 }
drh17c40292004-07-21 02:53:29 +0000330 }
drh7ea31cc2014-09-18 14:36:00 +0000331 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000332 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000333 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000334 ** representation. It would be harmless to repeat the conversion if
335 ** there is already a string rep, but it is pointless to waste those
336 ** CPU cycles. */
337 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
338 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
339 sqlite3VdbeMemStringify(pRec, enc, 1);
340 }
danielk19773d1bfea2004-05-14 11:00:53 +0000341 }
dandde548c2015-05-19 19:44:25 +0000342 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000343 }
344}
345
danielk1977aee18ef2005-03-09 12:26:50 +0000346/*
drh29d72102006-02-09 22:13:41 +0000347** Try to convert the type of a function argument or a result column
348** into a numeric representation. Use either INTEGER or REAL whichever
349** is appropriate. But only do the conversion if it is possible without
350** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000351*/
352int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000353 int eType = sqlite3_value_type(pVal);
354 if( eType==SQLITE_TEXT ){
355 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000356 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000357 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000358 }
drh1b27b8c2014-02-10 03:21:57 +0000359 return eType;
drh29d72102006-02-09 22:13:41 +0000360}
361
362/*
danielk1977aee18ef2005-03-09 12:26:50 +0000363** Exported version of applyAffinity(). This one works on sqlite3_value*,
364** not the internal Mem* type.
365*/
danielk19771e536952007-08-16 10:09:01 +0000366void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000367 sqlite3_value *pVal,
368 u8 affinity,
369 u8 enc
370){
drhb21c8cd2007-08-21 19:33:56 +0000371 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000372}
373
drh3d1d90a2014-03-24 15:00:15 +0000374/*
drhf1a89ed2014-08-23 17:41:15 +0000375** pMem currently only holds a string type (or maybe a BLOB that we can
376** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000377** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000378** accordingly.
379*/
380static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
381 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
382 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000383 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000384 return 0;
385 }
drh84d4f1a2017-09-20 10:47:10 +0000386 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000387 return MEM_Int;
388 }
389 return MEM_Real;
390}
391
392/*
drh3d1d90a2014-03-24 15:00:15 +0000393** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
394** none.
395**
396** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000397** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000398*/
399static u16 numericType(Mem *pMem){
400 if( pMem->flags & (MEM_Int|MEM_Real) ){
401 return pMem->flags & (MEM_Int|MEM_Real);
402 }
403 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000404 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000405 }
406 return 0;
407}
408
danielk1977b5402fb2005-01-12 07:15:04 +0000409#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000410/*
danielk1977ca6b2912004-05-21 10:49:47 +0000411** Write a nice string representation of the contents of cell pMem
412** into buffer zBuf, length nBuf.
413*/
drh74161702006-02-24 02:53:49 +0000414void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000415 char *zCsr = zBuf;
416 int f = pMem->flags;
417
drh57196282004-10-06 15:41:16 +0000418 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000419
danielk1977ca6b2912004-05-21 10:49:47 +0000420 if( f&MEM_Blob ){
421 int i;
422 char c;
423 if( f & MEM_Dyn ){
424 c = 'z';
425 assert( (f & (MEM_Static|MEM_Ephem))==0 );
426 }else if( f & MEM_Static ){
427 c = 't';
428 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
429 }else if( f & MEM_Ephem ){
430 c = 'e';
431 assert( (f & (MEM_Static|MEM_Dyn))==0 );
432 }else{
433 c = 's';
434 }
drh85c2dc02017-03-16 13:30:58 +0000435 *(zCsr++) = c;
drh5bb3eb92007-05-04 13:15:55 +0000436 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000437 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000438 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000439 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000440 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000441 }
442 for(i=0; i<16 && i<pMem->n; i++){
443 char z = pMem->z[i];
444 if( z<32 || z>126 ) *zCsr++ = '.';
445 else *zCsr++ = z;
446 }
drh85c2dc02017-03-16 13:30:58 +0000447 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000448 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000449 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000450 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000451 }
danielk1977b1bc9532004-05-22 03:05:33 +0000452 *zCsr = '\0';
453 }else if( f & MEM_Str ){
454 int j, k;
455 zBuf[0] = ' ';
456 if( f & MEM_Dyn ){
457 zBuf[1] = 'z';
458 assert( (f & (MEM_Static|MEM_Ephem))==0 );
459 }else if( f & MEM_Static ){
460 zBuf[1] = 't';
461 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
462 }else if( f & MEM_Ephem ){
463 zBuf[1] = 'e';
464 assert( (f & (MEM_Static|MEM_Dyn))==0 );
465 }else{
466 zBuf[1] = 's';
467 }
468 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000469 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000470 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000471 zBuf[k++] = '[';
472 for(j=0; j<15 && j<pMem->n; j++){
473 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000474 if( c>=0x20 && c<0x7f ){
475 zBuf[k++] = c;
476 }else{
477 zBuf[k++] = '.';
478 }
479 }
480 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000481 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000482 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000483 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000484 }
danielk1977ca6b2912004-05-21 10:49:47 +0000485}
486#endif
487
drh5b6afba2008-01-05 16:29:28 +0000488#ifdef SQLITE_DEBUG
489/*
490** Print the value of a register for tracing purposes:
491*/
drh84e55a82013-11-13 17:58:23 +0000492static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000493 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000494 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000495 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000496 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000497 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000498 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000499 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000500 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000501#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000502 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000503 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000504#endif
drh733bf1b2009-04-22 00:47:00 +0000505 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000506 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000507 }else{
508 char zBuf[200];
509 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000510 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000511 }
dan5b6c8e42016-01-30 15:46:03 +0000512 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000513}
drh84e55a82013-11-13 17:58:23 +0000514static void registerTrace(int iReg, Mem *p){
515 printf("REG[%d] = ", iReg);
516 memTracePrint(p);
517 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000518 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000519}
520#endif
521
522#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000523# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000524#else
525# define REGISTER_TRACE(R,M)
526#endif
527
danielk197784ac9d02004-05-18 09:58:06 +0000528
drh7b396862003-01-01 23:06:20 +0000529#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000530
531/*
532** hwtime.h contains inline assembler code for implementing
533** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000534*/
shane9bcbdad2008-05-29 20:22:37 +0000535#include "hwtime.h"
536
drh7b396862003-01-01 23:06:20 +0000537#endif
538
danielk1977fd7f0452008-12-17 17:30:26 +0000539#ifndef NDEBUG
540/*
541** This function is only called from within an assert() expression. It
542** checks that the sqlite3.nTransaction variable is correctly set to
543** the number of non-transaction savepoints currently in the
544** linked list starting at sqlite3.pSavepoint.
545**
546** Usage:
547**
548** assert( checkSavepointCount(db) );
549*/
550static int checkSavepointCount(sqlite3 *db){
551 int n = 0;
552 Savepoint *p;
553 for(p=db->pSavepoint; p; p=p->pNext) n++;
554 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
555 return 1;
556}
557#endif
558
drh27a348c2015-04-13 19:14:06 +0000559/*
560** Return the register of pOp->p2 after first preparing it to be
561** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000562*/
563static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
564 sqlite3VdbeMemSetNull(pOut);
565 pOut->flags = MEM_Int;
566 return pOut;
567}
drh27a348c2015-04-13 19:14:06 +0000568static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
569 Mem *pOut;
570 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000571 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000572 pOut = &p->aMem[pOp->p2];
573 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000574 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000575 return out2PrereleaseWithClear(pOut);
576 }else{
577 pOut->flags = MEM_Int;
578 return pOut;
579 }
drh27a348c2015-04-13 19:14:06 +0000580}
581
drhb9755982010-07-24 16:34:37 +0000582
583/*
drh0fd61352014-02-07 02:29:45 +0000584** Execute as much of a VDBE program as we can.
585** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000586*/
danielk19774adee202004-05-08 08:23:19 +0000587int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000588 Vdbe *p /* The VDBE */
589){
drhbbe879d2009-11-14 18:04:35 +0000590 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000591 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000592#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
593 Op *pOrigOp; /* Value of pOp at the top of the loop */
594#endif
drhb89aeb62016-01-27 15:49:32 +0000595#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000596 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000597#endif
drhb86ccfb2003-01-28 23:13:10 +0000598 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000599 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000600 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000601 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000602 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000603 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000604#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000605 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000606#endif
drha6c2ed92009-11-14 23:22:23 +0000607 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000608 Mem *pIn1 = 0; /* 1st input operand */
609 Mem *pIn2 = 0; /* 2nd input operand */
610 Mem *pIn3 = 0; /* 3rd input operand */
611 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000612#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000613 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000614#endif
drh856c1032009-06-02 15:21:42 +0000615 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000616
drhca48c902008-01-18 14:08:24 +0000617 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000618 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000619 if( p->rc==SQLITE_NOMEM ){
620 /* This happens if a malloc() inside a call to sqlite3_column_text() or
621 ** sqlite3_column_text16() failed. */
622 goto no_mem;
623 }
drhcbd8db32015-08-20 17:18:32 +0000624 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000625 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000626 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000627 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000628 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000629 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000630 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000631 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000632#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
633 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000634 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000635 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000636 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh2ab792e2017-05-30 18:34:07 +0000637 }else{
638 nProgressLimit = 0xffffffff;
drh0d1961e2013-07-25 16:27:51 +0000639 }
640#endif
drh3c23a882007-01-09 14:01:13 +0000641#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000642 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000643 if( p->pc==0
644 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
645 ){
drh3c23a882007-01-09 14:01:13 +0000646 int i;
drh84e55a82013-11-13 17:58:23 +0000647 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000648 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000649 if( p->db->flags & SQLITE_VdbeListing ){
650 printf("VDBE Program Listing:\n");
651 for(i=0; i<p->nOp; i++){
652 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
653 }
drh3c23a882007-01-09 14:01:13 +0000654 }
drh84e55a82013-11-13 17:58:23 +0000655 if( p->db->flags & SQLITE_VdbeEQP ){
656 for(i=0; i<p->nOp; i++){
657 if( aOp[i].opcode==OP_Explain ){
658 if( once ) printf("VDBE Query Plan:\n");
659 printf("%s\n", aOp[i].p4.z);
660 once = 0;
661 }
662 }
663 }
664 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000665 }
danielk19772d1d86f2008-06-20 14:59:51 +0000666 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000667#endif
drh9467abf2016-02-17 18:44:11 +0000668 for(pOp=&aOp[p->pc]; 1; pOp++){
669 /* Errors are detected by individual opcodes, with an immediate
670 ** jumps to abort_due_to_error. */
671 assert( rc==SQLITE_OK );
672
drhf56fa462015-04-13 21:39:54 +0000673 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000674#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000675 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000676#endif
drhbf159fa2013-06-25 22:01:22 +0000677 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000678#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000679 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000680#endif
drh6e142f52000-06-08 13:36:40 +0000681
danielk19778b60e0f2005-01-12 09:10:39 +0000682 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000683 */
danielk19778b60e0f2005-01-12 09:10:39 +0000684#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000685 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000686 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000687 }
drh3f7d4e42004-07-24 14:35:58 +0000688#endif
689
drh6e142f52000-06-08 13:36:40 +0000690
drhf6038712004-02-08 18:07:34 +0000691 /* Check to see if we need to simulate an interrupt. This only happens
692 ** if we have a special test build.
693 */
694#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000695 if( sqlite3_interrupt_count>0 ){
696 sqlite3_interrupt_count--;
697 if( sqlite3_interrupt_count==0 ){
698 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000699 }
700 }
701#endif
702
drh3c657212009-11-17 23:59:58 +0000703 /* Sanity checking on other operands */
704#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000705 {
706 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
707 if( (opProperty & OPFLG_IN1)!=0 ){
708 assert( pOp->p1>0 );
709 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
710 assert( memIsValid(&aMem[pOp->p1]) );
711 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
712 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
713 }
714 if( (opProperty & OPFLG_IN2)!=0 ){
715 assert( pOp->p2>0 );
716 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
717 assert( memIsValid(&aMem[pOp->p2]) );
718 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
719 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
720 }
721 if( (opProperty & OPFLG_IN3)!=0 ){
722 assert( pOp->p3>0 );
723 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
724 assert( memIsValid(&aMem[pOp->p3]) );
725 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
726 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
727 }
728 if( (opProperty & OPFLG_OUT2)!=0 ){
729 assert( pOp->p2>0 );
730 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
731 memAboutToChange(p, &aMem[pOp->p2]);
732 }
733 if( (opProperty & OPFLG_OUT3)!=0 ){
734 assert( pOp->p3>0 );
735 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
736 memAboutToChange(p, &aMem[pOp->p3]);
737 }
drh3c657212009-11-17 23:59:58 +0000738 }
739#endif
drh6dc41482015-04-16 17:31:02 +0000740#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
741 pOrigOp = pOp;
742#endif
drh93952eb2009-11-13 19:43:43 +0000743
drh75897232000-05-29 14:26:00 +0000744 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000745
drh5e00f6c2001-09-13 13:46:56 +0000746/*****************************************************************************
747** What follows is a massive switch statement where each case implements a
748** separate instruction in the virtual machine. If we follow the usual
749** indentation conventions, each case should be indented by 6 spaces. But
750** that is a lot of wasted space on the left margin. So the code within
751** the switch statement will break with convention and be flush-left. Another
752** big comment (similar to this one) will mark the point in the code where
753** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000754**
755** The formatting of each case is important. The makefile for SQLite
756** generates two C files "opcodes.h" and "opcodes.c" by scanning this
757** file looking for lines that begin with "case OP_". The opcodes.h files
758** will be filled with #defines that give unique integer values to each
759** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000760** each string is the symbolic name for the corresponding opcode. If the
761** case statement is followed by a comment of the form "/# same as ... #/"
762** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000763**
drh9cbf3422008-01-17 16:22:13 +0000764** Other keywords in the comment that follows each case are used to
765** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000766** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000767** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000768**
drhac82fcf2002-09-08 17:23:41 +0000769** Documentation about VDBE opcodes is generated by scanning this file
770** for lines of that contain "Opcode:". That line and all subsequent
771** comment lines are used in the generation of the opcode.html documentation
772** file.
773**
774** SUMMARY:
775**
776** Formatting is important to scripts that scan this file.
777** Do not deviate from the formatting style currently in use.
778**
drh5e00f6c2001-09-13 13:46:56 +0000779*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000780
drh9cbf3422008-01-17 16:22:13 +0000781/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000782**
783** An unconditional jump to address P2.
784** The next instruction executed will be
785** the one at index P2 from the beginning of
786** the program.
drhfe705102014-03-06 13:38:37 +0000787**
788** The P1 parameter is not actually used by this opcode. However, it
789** is sometimes set to 1 instead of 0 as a hint to the command-line shell
790** that this Goto is the bottom of a loop and that the lines from P2 down
791** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000792*/
drh9cbf3422008-01-17 16:22:13 +0000793case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000794jump_to_p2_and_check_for_interrupt:
795 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000796
797 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000798 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000799 ** completion. Check to see if sqlite3_interrupt() has been called
800 ** or if the progress callback needs to be invoked.
801 **
802 ** This code uses unstructured "goto" statements and does not look clean.
803 ** But that is not due to sloppy coding habits. The code is written this
804 ** way for performance, to avoid having to run the interrupt and progress
805 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
806 ** faster according to "valgrind --tool=cachegrind" */
807check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000808 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000809#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
810 /* Call the progress callback if it is configured and the required number
811 ** of VDBE ops have been executed (either since this invocation of
812 ** sqlite3VdbeExec() or since last time the progress callback was called).
813 ** If the progress callback returns non-zero, exit the virtual machine with
814 ** a return code SQLITE_ABORT.
815 */
drh2ab792e2017-05-30 18:34:07 +0000816 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000817 assert( db->nProgressOps!=0 );
818 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
819 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000820 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000821 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000822 }
drh49afe3a2013-07-10 03:05:14 +0000823 }
824#endif
825
drh5e00f6c2001-09-13 13:46:56 +0000826 break;
827}
drh75897232000-05-29 14:26:00 +0000828
drh2eb95372008-06-06 15:04:36 +0000829/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000830**
drh2eb95372008-06-06 15:04:36 +0000831** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000832** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000833*/
drhb8475df2011-12-09 16:21:19 +0000834case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000835 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000836 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000837 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000838 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000839 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000840 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000841 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000842
843 /* Most jump operations do a goto to this spot in order to update
844 ** the pOp pointer. */
845jump_to_p2:
846 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000847 break;
848}
849
drh2eb95372008-06-06 15:04:36 +0000850/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000851**
drh81cf13e2014-02-07 18:27:53 +0000852** Jump to the next instruction after the address in register P1. After
853** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000854*/
drh2eb95372008-06-06 15:04:36 +0000855case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000856 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000857 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000858 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000859 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000860 break;
861}
862
drhed71a832014-02-07 19:18:10 +0000863/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000864**
drh5dad9a32014-07-25 18:37:42 +0000865** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000866** located at address P3.
867**
drh5dad9a32014-07-25 18:37:42 +0000868** If P2!=0 then the coroutine implementation immediately follows
869** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000870** address P2.
drh5dad9a32014-07-25 18:37:42 +0000871**
872** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000873*/
874case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000875 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000876 assert( pOp->p2>=0 && pOp->p2<p->nOp );
877 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000878 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000879 assert( !VdbeMemDynamic(pOut) );
880 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000881 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000882 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000883 break;
884}
885
886/* Opcode: EndCoroutine P1 * * * *
887**
drhbc5cf382014-08-06 01:08:07 +0000888** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000889** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000890** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000891**
892** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000893*/
894case OP_EndCoroutine: { /* in1 */
895 VdbeOp *pCaller;
896 pIn1 = &aMem[pOp->p1];
897 assert( pIn1->flags==MEM_Int );
898 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
899 pCaller = &aOp[pIn1->u.i];
900 assert( pCaller->opcode==OP_Yield );
901 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000902 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000903 pIn1->flags = MEM_Undefined;
904 break;
905}
906
907/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000908**
drh5dad9a32014-07-25 18:37:42 +0000909** Swap the program counter with the value in register P1. This
910** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000911**
drh5dad9a32014-07-25 18:37:42 +0000912** If the coroutine that is launched by this instruction ends with
913** Yield or Return then continue to the next instruction. But if
914** the coroutine launched by this instruction ends with
915** EndCoroutine, then jump to P2 rather than continuing with the
916** next instruction.
917**
918** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000919*/
drh81cf13e2014-02-07 18:27:53 +0000920case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000921 int pcDest;
drh3c657212009-11-17 23:59:58 +0000922 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000923 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000924 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000925 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000926 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000927 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000928 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000929 break;
930}
931
drhf9c8ce32013-11-05 13:33:55 +0000932/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000933** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000934**
drhef8662b2011-06-20 21:47:58 +0000935** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000936** parameter P1, P2, and P4 as if this were a Halt instruction. If the
937** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000938** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000939*/
940case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000941 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +0000942#ifdef SQLITE_DEBUG
943 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
944#endif
drh5053a792009-02-20 03:02:23 +0000945 if( (pIn3->flags & MEM_Null)==0 ) break;
946 /* Fall through into OP_Halt */
947}
drhe00ee6e2008-06-20 15:24:01 +0000948
drhf9c8ce32013-11-05 13:33:55 +0000949/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000950**
drh3d4501e2008-12-04 20:40:10 +0000951** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000952** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000953**
drh92f02c32004-09-02 14:57:08 +0000954** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
955** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
956** For errors, it can be some other value. If P1!=0 then P2 will determine
957** whether or not to rollback the current transaction. Do not rollback
958** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
959** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000960** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000961**
drh66a51672008-01-03 00:01:23 +0000962** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000963**
drhf9c8ce32013-11-05 13:33:55 +0000964** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
965**
966** 0: (no change)
967** 1: NOT NULL contraint failed: P4
968** 2: UNIQUE constraint failed: P4
969** 3: CHECK constraint failed: P4
970** 4: FOREIGN KEY constraint failed: P4
971**
972** If P5 is not zero and P4 is NULL, then everything after the ":" is
973** omitted.
974**
drh9cfcf5d2002-01-29 18:41:24 +0000975** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000976** every program. So a jump past the last instruction of the program
977** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000978*/
drh9cbf3422008-01-17 16:22:13 +0000979case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +0000980 VdbeFrame *pFrame;
981 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000982
drhf56fa462015-04-13 21:39:54 +0000983 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +0000984#ifdef SQLITE_DEBUG
985 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
986#endif
dan165921a2009-08-28 18:53:45 +0000987 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000988 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000989 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000990 p->pFrame = pFrame->pParent;
991 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000992 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000993 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +0000994 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +0000995 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +0000996 ** currently being halted. If the p2 instruction of this OP_Halt
997 ** instruction is set to OE_Ignore, then the sub-program is throwing
998 ** an IGNORE exception. In this case jump to the address specified
999 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001000 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001001 }
drhbbe879d2009-11-14 18:04:35 +00001002 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001003 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001004 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001005 break;
1006 }
drh92f02c32004-09-02 14:57:08 +00001007 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001008 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001009 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001010 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001011 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001012 if( pOp->p5 ){
1013 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1014 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001015 testcase( pOp->p5==1 );
1016 testcase( pOp->p5==2 );
1017 testcase( pOp->p5==3 );
1018 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001019 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1020 if( pOp->p4.z ){
1021 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1022 }
drhd9b7ec92013-11-06 14:05:21 +00001023 }else{
drh22c17b82015-05-15 04:13:15 +00001024 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001025 }
drh99f5de72016-04-30 02:59:15 +00001026 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001027 }
drh92f02c32004-09-02 14:57:08 +00001028 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001029 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001030 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001031 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001032 }else{
drhd91c1a12013-02-09 13:58:25 +00001033 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001034 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001035 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001036 }
drh900b31e2007-08-28 02:27:51 +00001037 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001038}
drhc61053b2000-06-04 12:58:36 +00001039
drh4c583122008-01-04 22:01:03 +00001040/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001041** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001042**
drh9cbf3422008-01-17 16:22:13 +00001043** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001044*/
drh27a348c2015-04-13 19:14:06 +00001045case OP_Integer: { /* out2 */
1046 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001047 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001048 break;
1049}
1050
drh4c583122008-01-04 22:01:03 +00001051/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001052** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001053**
drh66a51672008-01-03 00:01:23 +00001054** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001055** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001056*/
drh27a348c2015-04-13 19:14:06 +00001057case OP_Int64: { /* out2 */
1058 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001059 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001060 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001061 break;
1062}
drh4f26d6c2004-05-26 23:25:30 +00001063
drh13573c72010-01-12 17:04:07 +00001064#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001065/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001066** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001067**
drh4c583122008-01-04 22:01:03 +00001068** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001069** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001070*/
drh27a348c2015-04-13 19:14:06 +00001071case OP_Real: { /* same as TK_FLOAT, out2 */
1072 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001073 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001074 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001075 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001076 break;
1077}
drh13573c72010-01-12 17:04:07 +00001078#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001079
drh3c84ddf2008-01-09 02:15:38 +00001080/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001081** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001082**
drh66a51672008-01-03 00:01:23 +00001083** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001084** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001085** this transformation, the length of string P4 is computed and stored
1086** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001087*/
drh27a348c2015-04-13 19:14:06 +00001088case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001089 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001090 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001091 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001092 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001093
1094#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001095 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001096 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001097 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drh4c583122008-01-04 22:01:03 +00001098 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001099 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001100 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001101 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001102 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001103 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001104 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001105 }
drh66a51672008-01-03 00:01:23 +00001106 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001107 pOp->p4.z = pOut->z;
1108 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001109 }
drh2f555112016-04-30 18:10:34 +00001110 testcase( rc==SQLITE_TOOBIG );
danielk197793758c82005-01-21 08:13:14 +00001111#endif
drhbb4957f2008-03-20 14:03:29 +00001112 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001113 goto too_big;
1114 }
drh2f555112016-04-30 18:10:34 +00001115 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001116 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001117}
drhf4479502004-05-27 03:12:53 +00001118
drhf07cf6e2015-03-06 16:45:16 +00001119/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001120** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001121**
drh9cbf3422008-01-17 16:22:13 +00001122** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001123**
drh44aebff2016-05-02 10:25:42 +00001124** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001125** the datatype of the register P2 is converted to BLOB. The content is
1126** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001127** of a string, as if it had been CAST. In other words:
1128**
1129** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001130*/
drh27a348c2015-04-13 19:14:06 +00001131case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001132 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001133 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001134 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1135 pOut->z = pOp->p4.z;
1136 pOut->n = pOp->p1;
1137 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001138 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001139#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001140 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001141 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001142 pIn3 = &aMem[pOp->p3];
1143 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001144 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001145 }
drh41d2e662015-12-01 21:23:07 +00001146#endif
danielk1977c572ef72004-05-27 09:28:41 +00001147 break;
1148}
1149
drh053a1282012-09-19 21:15:46 +00001150/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001151** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001152**
drhb8475df2011-12-09 16:21:19 +00001153** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001154** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001155** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001156** set to NULL.
1157**
1158** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1159** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1160** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001161*/
drh27a348c2015-04-13 19:14:06 +00001162case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001163 int cnt;
drh053a1282012-09-19 21:15:46 +00001164 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001165 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001166 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001167 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001168 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001169 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001170#ifdef SQLITE_DEBUG
1171 pOut->uTemp = 0;
1172#endif
drhb8475df2011-12-09 16:21:19 +00001173 while( cnt>0 ){
1174 pOut++;
1175 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001176 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001177 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001178 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001179 cnt--;
1180 }
drhf0863fe2005-06-12 21:35:51 +00001181 break;
1182}
1183
drh05a86c52014-02-16 01:55:49 +00001184/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001185** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001186**
1187** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1188** instruction, but do not free any string or blob memory associated with
1189** the register, so that if the value was a string or blob that was
1190** previously copied using OP_SCopy, the copies will continue to be valid.
1191*/
1192case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001193 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001194 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001195 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001196 break;
1197}
drhf0863fe2005-06-12 21:35:51 +00001198
drha5750cf2014-02-07 13:20:31 +00001199/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001200** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001201**
drh9de221d2008-01-05 06:51:30 +00001202** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001203** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001204*/
drh27a348c2015-04-13 19:14:06 +00001205case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001206 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001207 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001208 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001209 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001210 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001211 break;
1212}
1213
drheaf52d82010-05-12 13:50:23 +00001214/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001215** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001216**
drheaf52d82010-05-12 13:50:23 +00001217** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001218**
drh0fd61352014-02-07 02:29:45 +00001219** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001220** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001221*/
drh27a348c2015-04-13 19:14:06 +00001222case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001223 Mem *pVar; /* Value being transferred */
1224
drheaf52d82010-05-12 13:50:23 +00001225 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001226 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001227 pVar = &p->aVar[pOp->p1 - 1];
1228 if( sqlite3VdbeMemTooBig(pVar) ){
1229 goto too_big;
drh023ae032007-05-08 12:12:16 +00001230 }
drh7441df72017-01-09 19:27:04 +00001231 pOut = &aMem[pOp->p2];
drheaf52d82010-05-12 13:50:23 +00001232 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1233 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001234 break;
1235}
danielk1977295ba552004-05-19 10:34:51 +00001236
drhb21e7c72008-06-22 12:37:57 +00001237/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001238** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001239**
drh079a3072014-03-19 14:10:55 +00001240** Move the P3 values in register P1..P1+P3-1 over into
1241** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001242** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001243** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1244** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001245*/
drhe1349cb2008-04-01 00:36:10 +00001246case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001247 int n; /* Number of registers left to copy */
1248 int p1; /* Register to copy from */
1249 int p2; /* Register to copy to */
1250
drhe09f43f2013-11-21 04:18:31 +00001251 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001252 p1 = pOp->p1;
1253 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001254 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001255 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001256
drha6c2ed92009-11-14 23:22:23 +00001257 pIn1 = &aMem[p1];
1258 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001259 do{
drh9f6168b2016-03-19 23:32:58 +00001260 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1261 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001262 assert( memIsValid(pIn1) );
1263 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001264 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001265#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001266 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001267 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001268 }
1269#endif
drhbd6789e2015-04-28 14:00:02 +00001270 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001271 REGISTER_TRACE(p2++, pOut);
1272 pIn1++;
1273 pOut++;
drh079a3072014-03-19 14:10:55 +00001274 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001275 break;
1276}
1277
drhe8e4af72012-09-21 00:04:28 +00001278/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001279** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001280**
drhe8e4af72012-09-21 00:04:28 +00001281** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001282**
1283** This instruction makes a deep copy of the value. A duplicate
1284** is made of any string or blob constant. See also OP_SCopy.
1285*/
drhe8e4af72012-09-21 00:04:28 +00001286case OP_Copy: {
1287 int n;
1288
1289 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001290 pIn1 = &aMem[pOp->p1];
1291 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001292 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001293 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001294 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001295 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1296 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001297#ifdef SQLITE_DEBUG
1298 pOut->pScopyFrom = 0;
1299#endif
drhe8e4af72012-09-21 00:04:28 +00001300 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1301 if( (n--)==0 ) break;
1302 pOut++;
1303 pIn1++;
1304 }
drhe1349cb2008-04-01 00:36:10 +00001305 break;
1306}
1307
drhb1fdb2a2008-01-05 04:06:03 +00001308/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001309** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001310**
drh9cbf3422008-01-17 16:22:13 +00001311** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001312**
1313** This instruction makes a shallow copy of the value. If the value
1314** is a string or blob, then the copy is only a pointer to the
1315** original and hence if the original changes so will the copy.
1316** Worse, if the original is deallocated, the copy becomes invalid.
1317** Thus the program must guarantee that the original will not change
1318** during the lifetime of the copy. Use OP_Copy to make a complete
1319** copy.
1320*/
drh26198bb2013-10-31 11:15:09 +00001321case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001322 pIn1 = &aMem[pOp->p1];
1323 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001324 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001325 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001326#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001327 pOut->pScopyFrom = pIn1;
1328 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001329#endif
drh5e00f6c2001-09-13 13:46:56 +00001330 break;
1331}
drh75897232000-05-29 14:26:00 +00001332
drhfed7ac62015-10-15 18:04:59 +00001333/* Opcode: IntCopy P1 P2 * * *
1334** Synopsis: r[P2]=r[P1]
1335**
1336** Transfer the integer value held in register P1 into register P2.
1337**
1338** This is an optimized version of SCopy that works only for integer
1339** values.
1340*/
1341case OP_IntCopy: { /* out2 */
1342 pIn1 = &aMem[pOp->p1];
1343 assert( (pIn1->flags & MEM_Int)!=0 );
1344 pOut = &aMem[pOp->p2];
1345 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1346 break;
1347}
1348
drh9cbf3422008-01-17 16:22:13 +00001349/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001350** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001351**
shane21e7feb2008-05-30 15:59:49 +00001352** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001353** results. This opcode causes the sqlite3_step() call to terminate
1354** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001355** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001356** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001357*/
drh9cbf3422008-01-17 16:22:13 +00001358case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001359 Mem *pMem;
1360 int i;
1361 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001362 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001363 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001364
drhe6400b92013-11-13 23:48:46 +00001365#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1366 /* Run the progress counter just before returning.
1367 */
1368 if( db->xProgress!=0
drh2ab792e2017-05-30 18:34:07 +00001369 && nVmStep>=nProgressLimit
drhe6400b92013-11-13 23:48:46 +00001370 && db->xProgress(db->pProgressArg)!=0
1371 ){
1372 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001373 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001374 }
1375#endif
1376
dan32b09f22009-09-23 17:29:59 +00001377 /* If this statement has violated immediate foreign key constraints, do
1378 ** not return the number of rows modified. And do not RELEASE the statement
1379 ** transaction. It needs to be rolled back. */
1380 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1381 assert( db->flags&SQLITE_CountRows );
1382 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001383 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001384 }
1385
danielk1977bd434552009-03-18 10:33:00 +00001386 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1387 ** DML statements invoke this opcode to return the number of rows
1388 ** modified to the user. This is the only way that a VM that
1389 ** opens a statement transaction may invoke this opcode.
1390 **
1391 ** In case this is such a statement, close any statement transaction
1392 ** opened by this VM before returning control to the user. This is to
1393 ** ensure that statement-transactions are always nested, not overlapping.
1394 ** If the open statement-transaction is not closed here, then the user
1395 ** may step another VM that opens its own statement transaction. This
1396 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001397 **
1398 ** The statement transaction is never a top-level transaction. Hence
1399 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001400 */
1401 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001402 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001403 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001404
drhd4e70eb2008-01-02 00:34:36 +00001405 /* Invalidate all ephemeral cursor row caches */
1406 p->cacheCtr = (p->cacheCtr + 2)|1;
1407
1408 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001409 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001410 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001411 */
drha6c2ed92009-11-14 23:22:23 +00001412 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001413 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001414 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001415 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001416 assert( (pMem[i].flags & MEM_Ephem)==0
1417 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001418 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001419 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001420 }
drh28039692008-03-17 16:54:01 +00001421 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001422
drh3d2a5292016-07-13 22:55:01 +00001423 if( db->mTrace & SQLITE_TRACE_ROW ){
1424 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1425 }
1426
drhd4e70eb2008-01-02 00:34:36 +00001427 /* Return SQLITE_ROW
1428 */
drhf56fa462015-04-13 21:39:54 +00001429 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001430 rc = SQLITE_ROW;
1431 goto vdbe_return;
1432}
1433
drh5b6afba2008-01-05 16:29:28 +00001434/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001435** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001436**
drh5b6afba2008-01-05 16:29:28 +00001437** Add the text in register P1 onto the end of the text in
1438** register P2 and store the result in register P3.
1439** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001440**
1441** P3 = P2 || P1
1442**
1443** It is illegal for P1 and P3 to be the same register. Sometimes,
1444** if P3 is the same register as P2, the implementation is able
1445** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001446*/
drh5b6afba2008-01-05 16:29:28 +00001447case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001448 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001449
drh3c657212009-11-17 23:59:58 +00001450 pIn1 = &aMem[pOp->p1];
1451 pIn2 = &aMem[pOp->p2];
1452 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001453 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001454 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001455 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001456 break;
drh5e00f6c2001-09-13 13:46:56 +00001457 }
drha0c06522009-06-17 22:50:41 +00001458 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001459 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001460 Stringify(pIn2, encoding);
1461 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001462 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001463 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001464 }
drh9c1905f2008-12-10 22:32:56 +00001465 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001466 goto no_mem;
1467 }
drhc91b2fd2014-03-01 18:13:23 +00001468 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001469 if( pOut!=pIn2 ){
1470 memcpy(pOut->z, pIn2->z, pIn2->n);
1471 }
1472 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001473 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001474 pOut->z[nByte+1] = 0;
1475 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001476 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001477 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001478 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001479 break;
1480}
drh75897232000-05-29 14:26:00 +00001481
drh3c84ddf2008-01-09 02:15:38 +00001482/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001483** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001484**
drh60a713c2008-01-21 16:22:45 +00001485** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001486** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001487** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001488*/
drh3c84ddf2008-01-09 02:15:38 +00001489/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001490** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001491**
drh3c84ddf2008-01-09 02:15:38 +00001492**
shane21e7feb2008-05-30 15:59:49 +00001493** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001494** and store the result in register P3.
1495** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001496*/
drh3c84ddf2008-01-09 02:15:38 +00001497/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001498** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001499**
drh60a713c2008-01-21 16:22:45 +00001500** Subtract the value in register P1 from the value in register P2
1501** and store the result in register P3.
1502** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001503*/
drh9cbf3422008-01-17 16:22:13 +00001504/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001505** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001506**
drh60a713c2008-01-21 16:22:45 +00001507** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001508** and store the result in register P3 (P3=P2/P1). If the value in
1509** register P1 is zero, then the result is NULL. If either input is
1510** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001511*/
drh9cbf3422008-01-17 16:22:13 +00001512/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001513** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001514**
drh40864a12013-11-15 18:58:37 +00001515** Compute the remainder after integer register P2 is divided by
1516** register P1 and store the result in register P3.
1517** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001518** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001519*/
drh5b6afba2008-01-05 16:29:28 +00001520case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1521case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1522case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1523case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1524case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001525 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001526 u16 flags; /* Combined MEM_* flags from both inputs */
1527 u16 type1; /* Numeric type of left operand */
1528 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001529 i64 iA; /* Integer value of left operand */
1530 i64 iB; /* Integer value of right operand */
1531 double rA; /* Real value of left operand */
1532 double rB; /* Real value of right operand */
1533
drh3c657212009-11-17 23:59:58 +00001534 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001535 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001536 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001537 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001538 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001539 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001540 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001541 iA = pIn1->u.i;
1542 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001543 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001544 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001545 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1546 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1547 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001548 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001549 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001550 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001551 iB /= iA;
drh75897232000-05-29 14:26:00 +00001552 break;
1553 }
drhbf4133c2001-10-13 02:59:08 +00001554 default: {
drh856c1032009-06-02 15:21:42 +00001555 if( iA==0 ) goto arithmetic_result_is_null;
1556 if( iA==-1 ) iA = 1;
1557 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001558 break;
1559 }
drh75897232000-05-29 14:26:00 +00001560 }
drh856c1032009-06-02 15:21:42 +00001561 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001562 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001563 }else if( (flags & MEM_Null)!=0 ){
1564 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001565 }else{
drhbe707b32012-12-10 22:19:14 +00001566 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001567fp_math:
drh856c1032009-06-02 15:21:42 +00001568 rA = sqlite3VdbeRealValue(pIn1);
1569 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001570 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001571 case OP_Add: rB += rA; break;
1572 case OP_Subtract: rB -= rA; break;
1573 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001574 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001575 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001576 if( rA==(double)0 ) goto arithmetic_result_is_null;
1577 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001578 break;
1579 }
drhbf4133c2001-10-13 02:59:08 +00001580 default: {
shane75ac1de2009-06-09 18:58:52 +00001581 iA = (i64)rA;
1582 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001583 if( iA==0 ) goto arithmetic_result_is_null;
1584 if( iA==-1 ) iA = 1;
1585 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001586 break;
1587 }
drh5e00f6c2001-09-13 13:46:56 +00001588 }
drhc5a7b512010-01-13 16:25:42 +00001589#ifdef SQLITE_OMIT_FLOATING_POINT
1590 pOut->u.i = rB;
1591 MemSetTypeFlag(pOut, MEM_Int);
1592#else
drh856c1032009-06-02 15:21:42 +00001593 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001594 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001595 }
drh74eaba42014-09-18 17:52:15 +00001596 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001597 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001598 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001599 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001600 }
drhc5a7b512010-01-13 16:25:42 +00001601#endif
drh5e00f6c2001-09-13 13:46:56 +00001602 }
1603 break;
1604
drha05a7222008-01-19 03:35:58 +00001605arithmetic_result_is_null:
1606 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001607 break;
1608}
1609
drh7a957892012-02-02 17:35:43 +00001610/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001611**
drhbb6783b2017-04-29 18:02:49 +00001612** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001613** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1614** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001615** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001616**
drh7a957892012-02-02 17:35:43 +00001617** If P1 is not zero, then it is a register that a subsequent min() or
1618** max() aggregate will set to 1 if the current row is not the minimum or
1619** maximum. The P1 register is initialized to 0 by this instruction.
1620**
danielk1977dc1bdc42004-06-11 10:51:27 +00001621** The interface used by the implementation of the aforementioned functions
1622** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001623** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001624*/
drh9cbf3422008-01-17 16:22:13 +00001625case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001626 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001627 if( pOp->p1 ){
1628 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1629 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001630 break;
1631}
1632
drh98757152008-01-09 23:04:12 +00001633/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001634** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001635**
drh98757152008-01-09 23:04:12 +00001636** Take the bit-wise AND of the values in register P1 and P2 and
1637** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001638** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001639*/
drh98757152008-01-09 23:04:12 +00001640/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001641** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001642**
drh98757152008-01-09 23:04:12 +00001643** Take the bit-wise OR of the values in register P1 and P2 and
1644** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001645** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001646*/
drh98757152008-01-09 23:04:12 +00001647/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001648** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001649**
drh98757152008-01-09 23:04:12 +00001650** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001651** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001652** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001653** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001654*/
drh98757152008-01-09 23:04:12 +00001655/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001656** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001657**
drh98757152008-01-09 23:04:12 +00001658** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001659** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001660** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001661** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001662*/
drh5b6afba2008-01-05 16:29:28 +00001663case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1664case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1665case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1666case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001667 i64 iA;
1668 u64 uA;
1669 i64 iB;
1670 u8 op;
drh6810ce62004-01-31 19:22:56 +00001671
drh3c657212009-11-17 23:59:58 +00001672 pIn1 = &aMem[pOp->p1];
1673 pIn2 = &aMem[pOp->p2];
1674 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001675 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001676 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001677 break;
1678 }
drh158b9cb2011-03-05 20:59:46 +00001679 iA = sqlite3VdbeIntValue(pIn2);
1680 iB = sqlite3VdbeIntValue(pIn1);
1681 op = pOp->opcode;
1682 if( op==OP_BitAnd ){
1683 iA &= iB;
1684 }else if( op==OP_BitOr ){
1685 iA |= iB;
1686 }else if( iB!=0 ){
1687 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1688
1689 /* If shifting by a negative amount, shift in the other direction */
1690 if( iB<0 ){
1691 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1692 op = 2*OP_ShiftLeft + 1 - op;
1693 iB = iB>(-64) ? -iB : 64;
1694 }
1695
1696 if( iB>=64 ){
1697 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1698 }else{
1699 memcpy(&uA, &iA, sizeof(uA));
1700 if( op==OP_ShiftLeft ){
1701 uA <<= iB;
1702 }else{
1703 uA >>= iB;
1704 /* Sign-extend on a right shift of a negative number */
1705 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1706 }
1707 memcpy(&iA, &uA, sizeof(iA));
1708 }
drhbf4133c2001-10-13 02:59:08 +00001709 }
drh158b9cb2011-03-05 20:59:46 +00001710 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001711 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001712 break;
1713}
1714
drh8558cde2008-01-05 05:20:10 +00001715/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001716** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001717**
danielk19770cdc0222008-06-26 18:04:03 +00001718** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001719** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001720**
drh8558cde2008-01-05 05:20:10 +00001721** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001722*/
drh9cbf3422008-01-17 16:22:13 +00001723case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001724 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001725 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001726 sqlite3VdbeMemIntegerify(pIn1);
1727 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001728 break;
1729}
1730
drh9cbf3422008-01-17 16:22:13 +00001731/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001732**
drh9cbf3422008-01-17 16:22:13 +00001733** Force the value in register P1 to be an integer. If the value
1734** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001735** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001736** raise an SQLITE_MISMATCH exception.
1737*/
drh9cbf3422008-01-17 16:22:13 +00001738case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001739 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001740 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001741 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001742 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001743 if( (pIn1->flags & MEM_Int)==0 ){
1744 if( pOp->p2==0 ){
1745 rc = SQLITE_MISMATCH;
1746 goto abort_due_to_error;
1747 }else{
drhf56fa462015-04-13 21:39:54 +00001748 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001749 }
drh8aff1012001-12-22 14:49:24 +00001750 }
drh8aff1012001-12-22 14:49:24 +00001751 }
drh83b301b2013-11-20 00:59:02 +00001752 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001753 break;
1754}
1755
drh13573c72010-01-12 17:04:07 +00001756#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001757/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001758**
drh2133d822008-01-03 18:44:59 +00001759** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001760**
drh8a512562005-11-14 22:29:05 +00001761** This opcode is used when extracting information from a column that
1762** has REAL affinity. Such column values may still be stored as
1763** integers, for space efficiency, but after extraction we want them
1764** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001765*/
drh9cbf3422008-01-17 16:22:13 +00001766case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001767 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001768 if( pIn1->flags & MEM_Int ){
1769 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001770 }
drh487e2622005-06-25 18:42:14 +00001771 break;
1772}
drh13573c72010-01-12 17:04:07 +00001773#endif
drh487e2622005-06-25 18:42:14 +00001774
drh8df447f2005-11-01 15:48:24 +00001775#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001776/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001777** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001778**
drh4169e432014-08-25 20:11:52 +00001779** Force the value in register P1 to be the type defined by P2.
1780**
1781** <ul>
drhbb6783b2017-04-29 18:02:49 +00001782** <li> P2=='A' &rarr; BLOB
1783** <li> P2=='B' &rarr; TEXT
1784** <li> P2=='C' &rarr; NUMERIC
1785** <li> P2=='D' &rarr; INTEGER
1786** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001787** </ul>
drh487e2622005-06-25 18:42:14 +00001788**
1789** A NULL value is not changed by this routine. It remains NULL.
1790*/
drh4169e432014-08-25 20:11:52 +00001791case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001792 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001793 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001794 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001795 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1796 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1797 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001798 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001799 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001800 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001801 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001802 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001803 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001804 break;
1805}
drh8a512562005-11-14 22:29:05 +00001806#endif /* SQLITE_OMIT_CAST */
1807
drh79752b62016-08-13 10:02:17 +00001808/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001809** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001810**
1811** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1812** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1813** store the result of comparison in register P2.
1814**
1815** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1816** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1817** to coerce both inputs according to this affinity before the
1818** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1819** affinity is used. Note that the affinity conversions are stored
1820** back into the input registers P1 and P3. So this opcode can cause
1821** persistent changes to registers P1 and P3.
1822**
1823** Once any conversions have taken place, and neither value is NULL,
1824** the values are compared. If both values are blobs then memcmp() is
1825** used to determine the results of the comparison. If both values
1826** are text, then the appropriate collating function specified in
1827** P4 is used to do the comparison. If P4 is not specified then
1828** memcmp() is used to compare text string. If both values are
1829** numeric, then a numeric comparison is used. If the two values
1830** are of different types, then numbers are considered less than
1831** strings and strings are considered less than blobs.
1832**
1833** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1834** true or false and is never NULL. If both operands are NULL then the result
1835** of comparison is true. If either operand is NULL then the result is false.
1836** If neither operand is NULL the result is the same as it would be if
1837** the SQLITE_NULLEQ flag were omitted from P5.
1838**
1839** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001840** content of r[P2] is only changed if the new value is NULL or 0 (false).
1841** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001842*/
1843/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001844** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001845**
1846** This works just like the Eq opcode except that the jump is taken if
1847** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1848** additional information.
1849**
1850** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001851** content of r[P2] is only changed if the new value is NULL or 1 (true).
1852** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001853*/
drh35573352008-01-08 23:54:25 +00001854/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001855** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001856**
drh35573352008-01-08 23:54:25 +00001857** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001858** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1859** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001860**
drh35573352008-01-08 23:54:25 +00001861** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001862** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001863** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001864**
drh35573352008-01-08 23:54:25 +00001865** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001866** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001867** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001868** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001869** affinity is used. Note that the affinity conversions are stored
1870** back into the input registers P1 and P3. So this opcode can cause
1871** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001872**
1873** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001874** the values are compared. If both values are blobs then memcmp() is
1875** used to determine the results of the comparison. If both values
1876** are text, then the appropriate collating function specified in
1877** P4 is used to do the comparison. If P4 is not specified then
1878** memcmp() is used to compare text string. If both values are
1879** numeric, then a numeric comparison is used. If the two values
1880** are of different types, then numbers are considered less than
1881** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001882*/
drh9cbf3422008-01-17 16:22:13 +00001883/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001884** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001885**
drh35573352008-01-08 23:54:25 +00001886** This works just like the Lt opcode except that the jump is taken if
1887** the content of register P3 is less than or equal to the content of
1888** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001889*/
drh9cbf3422008-01-17 16:22:13 +00001890/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001891** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001892**
drh35573352008-01-08 23:54:25 +00001893** This works just like the Lt opcode except that the jump is taken if
1894** the content of register P3 is greater than the content of
1895** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001896*/
drh9cbf3422008-01-17 16:22:13 +00001897/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001898** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001899**
drh35573352008-01-08 23:54:25 +00001900** This works just like the Lt opcode except that the jump is taken if
1901** the content of register P3 is greater than or equal to the content of
1902** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001903*/
drh9cbf3422008-01-17 16:22:13 +00001904case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1905case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1906case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1907case OP_Le: /* same as TK_LE, jump, in1, in3 */
1908case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1909case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001910 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001911 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001912 u16 flags1; /* Copy of initial value of pIn1->flags */
1913 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001914
drh3c657212009-11-17 23:59:58 +00001915 pIn1 = &aMem[pOp->p1];
1916 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001917 flags1 = pIn1->flags;
1918 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001919 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001920 /* One or both operands are NULL */
1921 if( pOp->p5 & SQLITE_NULLEQ ){
1922 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1923 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1924 ** or not both operands are null.
1925 */
1926 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001927 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001928 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drhc3191d22016-10-18 16:36:15 +00001929 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001930 && (flags3&MEM_Cleared)==0
1931 ){
drh4910a762016-09-03 01:46:15 +00001932 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00001933 }else{
drh4910a762016-09-03 01:46:15 +00001934 res = 1; /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00001935 }
drh6a2fe092009-09-23 02:29:36 +00001936 }else{
1937 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1938 ** then the result is always NULL.
1939 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1940 */
drh688852a2014-02-17 22:40:43 +00001941 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001942 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001943 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00001944 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001945 MemSetTypeFlag(pOut, MEM_Null);
1946 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001947 }else{
drhf4345e42014-02-18 11:31:59 +00001948 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001949 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001950 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001951 }
drh6a2fe092009-09-23 02:29:36 +00001952 }
1953 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001954 }
drh6a2fe092009-09-23 02:29:36 +00001955 }else{
1956 /* Neither operand is NULL. Do a comparison. */
1957 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001958 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00001959 if( (flags1 | flags3)&MEM_Str ){
1960 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1961 applyNumericAffinity(pIn1,0);
drh24846bc2018-08-06 01:21:53 +00001962 assert( flags3==pIn3->flags );
drhcfdeeeb2018-08-04 20:12:10 +00001963 /* testcase( flags3!=pIn3->flags );
1964 ** this used to be possible with pIn1==pIn3, but not since
1965 ** the column cache was removed. The following assignment
drh24846bc2018-08-06 01:21:53 +00001966 ** is essentially a no-op. But, it provides defense-in-depth
drhcfdeeeb2018-08-04 20:12:10 +00001967 ** in case our analysis is incorrect, so it is left in. */
drh4b37cd42016-06-25 11:43:47 +00001968 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00001969 }
1970 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1971 applyNumericAffinity(pIn3,0);
1972 }
drh24a09622014-09-18 16:28:59 +00001973 }
drh64caee42016-09-09 19:33:00 +00001974 /* Handle the common case of integer comparison here, as an
1975 ** optimization, to avoid a call to sqlite3MemCompare() */
1976 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
1977 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
1978 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
1979 res = 0;
1980 goto compare_op;
1981 }
drh24a09622014-09-18 16:28:59 +00001982 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00001983 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001984 testcase( pIn1->flags & MEM_Int );
1985 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001986 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001987 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
1988 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00001989 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00001990 }
drhe5520e22015-12-31 04:34:26 +00001991 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001992 testcase( pIn3->flags & MEM_Int );
1993 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001994 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001995 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
1996 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00001997 }
drh6a2fe092009-09-23 02:29:36 +00001998 }
drh6a2fe092009-09-23 02:29:36 +00001999 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002000 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002001 }
drh64caee42016-09-09 19:33:00 +00002002compare_op:
drh58596362017-08-03 00:29:23 +00002003 /* At this point, res is negative, zero, or positive if reg[P1] is
2004 ** less than, equal to, or greater than reg[P3], respectively. Compute
2005 ** the answer to this operator in res2, depending on what the comparison
2006 ** operator actually is. The next block of code depends on the fact
2007 ** that the 6 comparison operators are consecutive integers in this
2008 ** order: NE, EQ, GT, LE, LT, GE */
2009 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2010 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2011 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2012 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2013 res2 = aLTb[pOp->opcode - OP_Ne];
2014 }else if( res==0 ){
2015 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2016 res2 = aEQb[pOp->opcode - OP_Ne];
2017 }else{
2018 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2019 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002020 }
2021
drhf56fa462015-04-13 21:39:54 +00002022 /* Undo any changes made by applyAffinity() to the input registers. */
2023 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2024 pIn1->flags = flags1;
2025 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2026 pIn3->flags = flags3;
2027
drh35573352008-01-08 23:54:25 +00002028 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002029 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002030 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002031 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002032 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002033 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2034 ** is only used in contexts where either:
2035 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2036 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2037 ** Therefore it is not necessary to check the content of r[P2] for
2038 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002039 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002040 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002041 testcase( res2==0 && pOp->opcode==OP_Eq );
2042 testcase( res2==1 && pOp->opcode==OP_Eq );
2043 testcase( res2==0 && pOp->opcode==OP_Ne );
2044 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002045 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002046 }
drh2b4ded92010-09-27 21:09:31 +00002047 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002048 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002049 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002050 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002051 }else{
drhf4345e42014-02-18 11:31:59 +00002052 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002053 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002054 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002055 }
danielk1977a37cdde2004-05-16 11:15:36 +00002056 }
2057 break;
2058}
drhc9b84a12002-06-20 11:36:48 +00002059
drh79752b62016-08-13 10:02:17 +00002060/* Opcode: ElseNotEq * P2 * * *
2061**
drhfd7459e2016-09-17 17:39:01 +00002062** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2063** If result of an OP_Eq comparison on the same two operands
2064** would have be NULL or false (0), then then jump to P2.
2065** If the result of an OP_Eq comparison on the two previous operands
2066** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002067*/
2068case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2069 assert( pOp>aOp );
2070 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002071 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002072 VdbeBranchTaken(iCompare!=0, 2);
2073 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002074 break;
2075}
2076
2077
drh0acb7e42008-06-25 00:12:41 +00002078/* Opcode: Permutation * * * P4 *
2079**
drhb7dab702017-01-26 18:00:00 +00002080** Set the permutation used by the OP_Compare operator in the next
2081** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002082**
drh953f7612012-12-07 22:18:54 +00002083** The permutation is only valid until the next OP_Compare that has
2084** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2085** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002086**
2087** The first integer in the P4 integer array is the length of the array
2088** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002089*/
2090case OP_Permutation: {
2091 assert( pOp->p4type==P4_INTARRAY );
2092 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002093 assert( pOp[1].opcode==OP_Compare );
2094 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002095 break;
2096}
2097
drh953f7612012-12-07 22:18:54 +00002098/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002099** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002100**
drh710c4842010-08-30 01:17:20 +00002101** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2102** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002103** the comparison for use by the next OP_Jump instruct.
2104**
drh0ca10df2012-12-08 13:26:23 +00002105** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2106** determined by the most recent OP_Permutation operator. If the
2107** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2108** order.
2109**
drh0acb7e42008-06-25 00:12:41 +00002110** P4 is a KeyInfo structure that defines collating sequences and sort
2111** orders for the comparison. The permutation applies to registers
2112** only. The KeyInfo elements are used sequentially.
2113**
2114** The comparison is a sort comparison, so NULLs compare equal,
2115** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002116** and strings are less than blobs.
2117*/
2118case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002119 int n;
2120 int i;
2121 int p1;
2122 int p2;
2123 const KeyInfo *pKeyInfo;
2124 int idx;
2125 CollSeq *pColl; /* Collating sequence to use on this term */
2126 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002127 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002128
drhb7dab702017-01-26 18:00:00 +00002129 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2130 aPermute = 0;
2131 }else{
2132 assert( pOp>aOp );
2133 assert( pOp[-1].opcode==OP_Permutation );
2134 assert( pOp[-1].p4type==P4_INTARRAY );
2135 aPermute = pOp[-1].p4.ai + 1;
2136 assert( aPermute!=0 );
2137 }
drh856c1032009-06-02 15:21:42 +00002138 n = pOp->p3;
2139 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002140 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002141 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002142 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002143 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002144#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002145 if( aPermute ){
2146 int k, mx = 0;
2147 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002148 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2149 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002150 }else{
drh9f6168b2016-03-19 23:32:58 +00002151 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2152 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002153 }
2154#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002155 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002156 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002157 assert( memIsValid(&aMem[p1+idx]) );
2158 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002159 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2160 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002161 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002162 pColl = pKeyInfo->aColl[i];
2163 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002164 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002165 if( iCompare ){
2166 if( bRev ) iCompare = -iCompare;
2167 break;
2168 }
drh16ee60f2008-06-20 18:13:25 +00002169 }
2170 break;
2171}
2172
2173/* Opcode: Jump P1 P2 P3 * *
2174**
2175** Jump to the instruction at address P1, P2, or P3 depending on whether
2176** in the most recent OP_Compare instruction the P1 vector was less than
2177** equal to, or greater than the P2 vector, respectively.
2178*/
drh0acb7e42008-06-25 00:12:41 +00002179case OP_Jump: { /* jump */
2180 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002181 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002182 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002183 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002184 }else{
drh7083a482018-07-10 16:04:04 +00002185 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002186 }
2187 break;
2188}
2189
drh5b6afba2008-01-05 16:29:28 +00002190/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002191** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002192**
drh5b6afba2008-01-05 16:29:28 +00002193** Take the logical AND of the values in registers P1 and P2 and
2194** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002195**
drh5b6afba2008-01-05 16:29:28 +00002196** If either P1 or P2 is 0 (false) then the result is 0 even if
2197** the other input is NULL. A NULL and true or two NULLs give
2198** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002199*/
drh5b6afba2008-01-05 16:29:28 +00002200/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002201** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002202**
2203** Take the logical OR of the values in register P1 and P2 and
2204** store the answer in register P3.
2205**
2206** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2207** even if the other input is NULL. A NULL and false or two NULLs
2208** give a NULL output.
2209*/
2210case OP_And: /* same as TK_AND, in1, in2, out3 */
2211case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002212 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2213 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002214
drh1fcfa722018-02-26 15:27:31 +00002215 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2216 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002217 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002218 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002219 v1 = and_logic[v1*3+v2];
2220 }else{
drh5b6afba2008-01-05 16:29:28 +00002221 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002222 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002223 }
drh3c657212009-11-17 23:59:58 +00002224 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002225 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002226 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002227 }else{
drh5b6afba2008-01-05 16:29:28 +00002228 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002229 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002230 }
drh5e00f6c2001-09-13 13:46:56 +00002231 break;
2232}
2233
drh8abed7b2018-02-26 18:49:05 +00002234/* Opcode: IsTrue P1 P2 P3 P4 *
2235** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2236**
2237** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2238** IS NOT FALSE operators.
2239**
drh96acafb2018-02-27 14:49:25 +00002240** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002241** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2242** NULL, then the P3 is stored in register P2. Invert the answer if P4
2243** is 1.
2244**
2245** The logic is summarized like this:
2246**
2247** <ul>
drh96acafb2018-02-27 14:49:25 +00002248** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2249** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2250** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2251** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002252** </ul>
2253*/
2254case OP_IsTrue: { /* in1, out2 */
2255 assert( pOp->p4type==P4_INT32 );
2256 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002257 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002258 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2259 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2260 break;
2261}
2262
drhe99fa2a2008-12-15 15:27:51 +00002263/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002264** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002265**
drhe99fa2a2008-12-15 15:27:51 +00002266** Interpret the value in register P1 as a boolean value. Store the
2267** boolean complement in register P2. If the value in register P1 is
2268** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002269*/
drh93952eb2009-11-13 19:43:43 +00002270case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002271 pIn1 = &aMem[pOp->p1];
2272 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002273 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002274 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002275 }else{
2276 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002277 }
drh5e00f6c2001-09-13 13:46:56 +00002278 break;
2279}
2280
drhe99fa2a2008-12-15 15:27:51 +00002281/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002282** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002283**
drhe99fa2a2008-12-15 15:27:51 +00002284** Interpret the content of register P1 as an integer. Store the
2285** ones-complement of the P1 value into register P2. If P1 holds
2286** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002287*/
drh93952eb2009-11-13 19:43:43 +00002288case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002289 pIn1 = &aMem[pOp->p1];
2290 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002291 sqlite3VdbeMemSetNull(pOut);
2292 if( (pIn1->flags & MEM_Null)==0 ){
2293 pOut->flags = MEM_Int;
2294 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002295 }
drhbf4133c2001-10-13 02:59:08 +00002296 break;
2297}
2298
drh48f2d3b2011-09-16 01:34:43 +00002299/* Opcode: Once P1 P2 * * *
2300**
drhab087d42017-03-24 17:59:56 +00002301** Fall through to the next instruction the first time this opcode is
2302** encountered on each invocation of the byte-code program. Jump to P2
2303** on the second and all subsequent encounters during the same invocation.
2304**
2305** Top-level programs determine first invocation by comparing the P1
2306** operand against the P1 operand on the OP_Init opcode at the beginning
2307** of the program. If the P1 values differ, then fall through and make
2308** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2309** the same then take the jump.
2310**
2311** For subprograms, there is a bitmask in the VdbeFrame that determines
2312** whether or not the jump should be taken. The bitmask is necessary
2313** because the self-altering code trick does not work for recursive
2314** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002315*/
dan1d8cb212011-12-09 13:24:16 +00002316case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002317 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002318 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002319 if( p->pFrame ){
2320 iAddr = (int)(pOp - p->aOp);
2321 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2322 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002323 goto jump_to_p2;
2324 }
drh18333ef2017-03-24 18:38:41 +00002325 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002326 }else{
drhab087d42017-03-24 17:59:56 +00002327 if( p->aOp[0].p1==pOp->p1 ){
2328 VdbeBranchTaken(1, 2);
2329 goto jump_to_p2;
2330 }
dan1d8cb212011-12-09 13:24:16 +00002331 }
drhab087d42017-03-24 17:59:56 +00002332 VdbeBranchTaken(0, 2);
2333 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002334 break;
2335}
2336
drh3c84ddf2008-01-09 02:15:38 +00002337/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002338**
drhef8662b2011-06-20 21:47:58 +00002339** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002340** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002341** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002342*/
drh1fcfa722018-02-26 15:27:31 +00002343case OP_If: { /* jump, in1 */
2344 int c;
2345 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2346 VdbeBranchTaken(c!=0, 2);
2347 if( c ) goto jump_to_p2;
2348 break;
2349}
2350
drh3c84ddf2008-01-09 02:15:38 +00002351/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002352**
drhef8662b2011-06-20 21:47:58 +00002353** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002354** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002355** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002356*/
drh9cbf3422008-01-17 16:22:13 +00002357case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002358 int c;
drh1fcfa722018-02-26 15:27:31 +00002359 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002360 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002361 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002362 break;
2363}
2364
drh830ecf92009-06-18 00:41:55 +00002365/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002366** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002367**
drh830ecf92009-06-18 00:41:55 +00002368** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002369*/
drh9cbf3422008-01-17 16:22:13 +00002370case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002371 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002372 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002373 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002374 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002375 }
drh477df4b2008-01-05 18:48:24 +00002376 break;
2377}
2378
drh98757152008-01-09 23:04:12 +00002379/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002380** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002381**
drh6a288a32008-01-07 19:20:24 +00002382** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002383*/
drh9cbf3422008-01-17 16:22:13 +00002384case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002385 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002386 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002387 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002388 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002389 }
drh5e00f6c2001-09-13 13:46:56 +00002390 break;
2391}
2392
drh31d6fd52017-04-14 19:03:10 +00002393/* Opcode: IfNullRow P1 P2 P3 * *
2394** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2395**
2396** Check the cursor P1 to see if it is currently pointing at a NULL row.
2397** If it is, then set register P3 to NULL and jump immediately to P2.
2398** If P1 is not on a NULL row, then fall through without making any
2399** changes.
2400*/
2401case OP_IfNullRow: { /* jump */
2402 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002403 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002404 if( p->apCsr[pOp->p1]->nullRow ){
2405 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2406 goto jump_to_p2;
2407 }
2408 break;
2409}
2410
drh092457b2017-12-29 15:04:49 +00002411#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2412/* Opcode: Offset P1 P2 P3 * *
2413** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002414**
drh092457b2017-12-29 15:04:49 +00002415** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002416** start of the payload for the record at which that cursor P1 is currently
2417** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002418**
drh092457b2017-12-29 15:04:49 +00002419** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002420** This opcode does not use P2 itself, but the P2 value is used by the
2421** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002422** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002423**
2424** This opcode is only available if SQLite is compiled with the
2425** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002426*/
drh092457b2017-12-29 15:04:49 +00002427case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002428 VdbeCursor *pC; /* The VDBE cursor */
2429 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2430 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002431 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002432 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002433 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002434 }else{
drh092457b2017-12-29 15:04:49 +00002435 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002436 }
2437 break;
2438}
drh092457b2017-12-29 15:04:49 +00002439#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002440
drh3e9ca092009-09-08 01:14:48 +00002441/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002442** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002443**
danielk1977cfcdaef2004-05-12 07:33:33 +00002444** Interpret the data that cursor P1 points to as a structure built using
2445** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002446** information about the format of the data.) Extract the P2-th column
2447** from this record. If there are less that (P2+1)
2448** values in the record, extract a NULL.
2449**
drh9cbf3422008-01-17 16:22:13 +00002450** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002451**
drh1cc3a362017-04-03 13:17:31 +00002452** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002453** if the P4 argument is a P4_MEM use the value of the P4 argument as
2454** the result.
drh3e9ca092009-09-08 01:14:48 +00002455**
2456** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2457** then the cache of the cursor is reset prior to extracting the column.
2458** The first OP_Column against a pseudo-table after the value of the content
2459** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002460**
drh1cc3a362017-04-03 13:17:31 +00002461** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002462** the result is guaranteed to only be used as the argument of a length()
2463** or typeof() function, respectively. The loading of large blobs can be
2464** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002465*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002466case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002467 int p2; /* column number to retrieve */
2468 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002469 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002470 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002471 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002472 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002473 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002474 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002475 const u8 *zData; /* Part of the record being decoded */
2476 const u8 *zHdr; /* Next unparsed byte of the header */
2477 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002478 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002479 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002480 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002481
dande892d92016-01-29 19:29:45 +00002482 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002483 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002484
drh170ad682017-06-02 15:44:22 +00002485 /* If the cursor cache is stale (meaning it is not currently point at
2486 ** the correct row) then bring it up-to-date by doing the necessary
2487 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002488 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002489 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002490
drh9f6168b2016-03-19 23:32:58 +00002491 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002492 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002493 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002494 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002495 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002496 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002497 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002498 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002499 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2500 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002501
drha43a02e2016-05-19 17:51:19 +00002502 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002503 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002504 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002505 /* For the special case of as pseudo-cursor, the seekResult field
2506 ** identifies the register that holds the record */
2507 assert( pC->seekResult>0 );
2508 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002509 assert( pReg->flags & MEM_Blob );
2510 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002511 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002512 pC->aRow = (u8*)pReg->z;
2513 }else{
drh6b5631e2014-11-05 15:57:39 +00002514 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002515 goto op_column_out;
2516 }
danielk1977192ac1d2004-05-10 07:17:30 +00002517 }else{
drh06a09a82016-11-25 17:03:03 +00002518 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002519 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002520 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002521 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2522 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002523 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2524 assert( pC->szRow<=pC->payloadSize );
2525 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2526 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002527 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002528 }
danielk1977192ac1d2004-05-10 07:17:30 +00002529 }
drhb73857f2006-03-17 00:25:59 +00002530 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002531 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002532 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002533
drhc81aa2e2014-10-11 23:31:52 +00002534
drh1f613c42017-08-16 14:16:19 +00002535 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002536 /* pC->aRow does not have to hold the entire row, but it does at least
2537 ** need to cover the header of the record. If pC->aRow does not contain
2538 ** the complete header, then set it to zero, forcing the header to be
2539 ** dynamically allocated. */
2540 pC->aRow = 0;
2541 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002542
2543 /* Make sure a corrupt database has not given us an oversize header.
2544 ** Do this now to avoid an oversize memory allocation.
2545 **
2546 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2547 ** types use so much data space that there can only be 4096 and 32 of
2548 ** them, respectively. So the maximum header length results from a
2549 ** 3-byte type for each of the maximum of 32768 columns plus three
2550 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2551 */
drh1f613c42017-08-16 14:16:19 +00002552 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002553 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002554 }
drh95b225a2017-08-16 11:04:22 +00002555 }else{
2556 /* This is an optimization. By skipping over the first few tests
2557 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2558 ** measurable performance gain.
2559 **
drh1f613c42017-08-16 14:16:19 +00002560 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002561 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002562 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002563 ** branch jumps to reads past the end of the record, but never more
2564 ** than a few bytes. Even if the record occurs at the end of the page
2565 ** content area, the "page header" comes after the page content and so
2566 ** this overread is harmless. Similar overreads can occur for a corrupt
2567 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002568 */
2569 zData = pC->aRow;
2570 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002571 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002572 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002573 }
drh399af1d2013-11-20 17:25:55 +00002574 }
drh35cd6432009-06-05 14:17:21 +00002575
drh399af1d2013-11-20 17:25:55 +00002576 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002577 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002578 */
drhc8606e42013-11-20 19:28:03 +00002579 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002580 /* If there is more header available for parsing in the record, try
2581 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002582 */
drhc8606e42013-11-20 19:28:03 +00002583 if( pC->iHdrOffset<aOffset[0] ){
2584 /* Make sure zData points to enough of the record to cover the header. */
2585 if( pC->aRow==0 ){
2586 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002587 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002588 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002589 zData = (u8*)sMem.z;
2590 }else{
2591 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002592 }
drhc8606e42013-11-20 19:28:03 +00002593
drh0c8f7602014-09-19 16:56:45 +00002594 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002595 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002596 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002597 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002598 zHdr = zData + pC->iHdrOffset;
2599 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002600 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002601 do{
drh95fa6062015-10-16 13:50:08 +00002602 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002603 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002604 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002605 }else{
drhc8606e42013-11-20 19:28:03 +00002606 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002607 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002608 }
drhfaf37272015-10-16 14:23:42 +00002609 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002610 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002611 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002612
drh8dd83622014-10-13 23:39:02 +00002613 /* The record is corrupt if any of the following are true:
2614 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002615 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002616 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002617 */
drhc6ce38832015-10-15 21:30:24 +00002618 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2619 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002620 ){
drh95b225a2017-08-16 11:04:22 +00002621 if( aOffset[0]==0 ){
2622 i = 0;
2623 zHdr = zEndHdr;
2624 }else{
2625 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002626 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002627 }
danielk1977dedf45b2006-01-13 17:12:01 +00002628 }
drhddb2b4a2016-03-25 12:10:32 +00002629
drh170c2762016-05-20 21:40:11 +00002630 pC->nHdrParsed = i;
2631 pC->iHdrOffset = (u32)(zHdr - zData);
2632 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002633 }else{
drh9fbc8852016-01-04 03:48:46 +00002634 t = 0;
drh9188b382004-05-14 21:12:22 +00002635 }
drhd3194f52004-05-27 19:59:32 +00002636
drhf2db3382015-04-30 20:33:25 +00002637 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002638 ** still not up to p2, that means that the record has fewer than p2
2639 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002640 */
drhc8606e42013-11-20 19:28:03 +00002641 if( pC->nHdrParsed<=p2 ){
2642 if( pOp->p4type==P4_MEM ){
2643 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2644 }else{
drh22e8d832014-10-29 00:58:38 +00002645 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002646 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002647 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002648 }
drh95fa6062015-10-16 13:50:08 +00002649 }else{
2650 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002651 }
danielk1977192ac1d2004-05-10 07:17:30 +00002652
drh380d6852013-11-20 20:58:00 +00002653 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002654 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002655 ** all valid.
drh9188b382004-05-14 21:12:22 +00002656 */
drhc8606e42013-11-20 19:28:03 +00002657 assert( p2<pC->nHdrParsed );
2658 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002659 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002660 if( VdbeMemDynamic(pDest) ){
2661 sqlite3VdbeMemSetNull(pDest);
2662 }
drh95fa6062015-10-16 13:50:08 +00002663 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002664 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002665 /* This is the common case where the desired content fits on the original
2666 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002667 zData = pC->aRow + aOffset[p2];
2668 if( t<12 ){
2669 sqlite3VdbeSerialGet(zData, t, pDest);
2670 }else{
2671 /* If the column value is a string, we need a persistent value, not
2672 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2673 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2674 */
2675 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2676 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002677 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002678 if( pDest->szMalloc < len+2 ){
2679 pDest->flags = MEM_Null;
2680 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2681 }else{
2682 pDest->z = pDest->zMalloc;
2683 }
2684 memcpy(pDest->z, zData, len);
2685 pDest->z[len] = 0;
2686 pDest->z[len+1] = 0;
2687 pDest->flags = aFlag[t&1];
2688 }
danielk197736963fd2005-02-19 08:18:05 +00002689 }else{
drha1851ef2016-05-20 19:51:28 +00002690 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002691 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002692 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2693 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2694 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002695 ){
drh2a2a6962014-09-16 18:22:44 +00002696 /* Content is irrelevant for
2697 ** 1. the typeof() function,
2698 ** 2. the length(X) function if X is a blob, and
2699 ** 3. if the content length is zero.
2700 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002701 ** content from disk.
2702 **
2703 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2704 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2705 ** read up to 16. So 16 bytes of bogus content is supplied.
2706 */
2707 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002708 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002709 }else{
drhcb3cabd2016-11-25 19:18:28 +00002710 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002711 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2712 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2713 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002714 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002715 }
drhd3194f52004-05-27 19:59:32 +00002716
danielk19773c9cc8d2005-01-17 03:40:08 +00002717op_column_out:
drhb7654112008-01-12 12:48:07 +00002718 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002719 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002720 break;
drh74588ce2017-09-13 00:13:05 +00002721
2722op_column_corrupt:
2723 if( aOp[0].p3>0 ){
2724 pOp = &aOp[aOp[0].p3-1];
2725 break;
2726 }else{
2727 rc = SQLITE_CORRUPT_BKPT;
2728 goto abort_due_to_error;
2729 }
danielk1977192ac1d2004-05-10 07:17:30 +00002730}
2731
danielk1977751de562008-04-18 09:01:15 +00002732/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002733** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002734**
2735** Apply affinities to a range of P2 registers starting with P1.
2736**
drhbb6783b2017-04-29 18:02:49 +00002737** P4 is a string that is P2 characters long. The N-th character of the
2738** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002739** memory cell in the range.
2740*/
2741case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002742 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002743
drh856c1032009-06-02 15:21:42 +00002744 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002745 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002746 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002747 assert( zAffinity[pOp->p2]==0 );
2748 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002749 do{
drh9f6168b2016-03-19 23:32:58 +00002750 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002751 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002752 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002753 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002754 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002755 break;
2756}
2757
drh1db639c2008-01-17 02:36:28 +00002758/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002759** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002760**
drh710c4842010-08-30 01:17:20 +00002761** Convert P2 registers beginning with P1 into the [record format]
2762** use as a data record in a database table or as a key
2763** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002764**
drhbb6783b2017-04-29 18:02:49 +00002765** P4 may be a string that is P2 characters long. The N-th character of the
2766** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002767** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002768**
drh8a512562005-11-14 22:29:05 +00002769** The mapping from character to affinity is given by the SQLITE_AFF_
2770** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002771**
drh05883a32015-06-02 15:32:08 +00002772** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002773*/
drh1db639c2008-01-17 02:36:28 +00002774case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002775 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2776 Mem *pRec; /* The new record */
2777 u64 nData; /* Number of bytes of data space */
2778 int nHdr; /* Number of bytes of header space */
2779 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002780 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002781 int nVarint; /* Number of bytes in a varint */
2782 u32 serial_type; /* Type field */
2783 Mem *pData0; /* First field to be combined into the record */
2784 Mem *pLast; /* Last field of the record */
2785 int nField; /* Number of fields in the record */
2786 char *zAffinity; /* The affinity string for the record */
2787 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002788 int i; /* Space used in zNewRecord[] header */
2789 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002790 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002791
drhf3218fe2004-05-28 08:21:02 +00002792 /* Assuming the record contains N fields, the record format looks
2793 ** like this:
2794 **
drh7a224de2004-06-02 01:22:02 +00002795 ** ------------------------------------------------------------------------
2796 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2797 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002798 **
drh9cbf3422008-01-17 16:22:13 +00002799 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002800 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002801 **
2802 ** Each type field is a varint representing the serial type of the
2803 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002804 ** hdr-size field is also a varint which is the offset from the beginning
2805 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002806 */
drh856c1032009-06-02 15:21:42 +00002807 nData = 0; /* Number of bytes of data space */
2808 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002809 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002810 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002811 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002812 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002813 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002814 nField = pOp->p2;
2815 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002816 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002817
drh2b4ded92010-09-27 21:09:31 +00002818 /* Identify the output register */
2819 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2820 pOut = &aMem[pOp->p3];
2821 memAboutToChange(p, pOut);
2822
drh3e6c0602013-12-10 20:53:01 +00002823 /* Apply the requested affinity to all inputs
2824 */
2825 assert( pData0<=pLast );
2826 if( zAffinity ){
2827 pRec = pData0;
2828 do{
drh57bf4a82014-02-17 14:59:22 +00002829 applyAffinity(pRec++, *(zAffinity++), encoding);
2830 assert( zAffinity[0]==0 || pRec<=pLast );
2831 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002832 }
2833
drhd447dce2017-01-25 20:55:11 +00002834#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002835 /* NULLs can be safely trimmed from the end of the record, as long as
2836 ** as the schema format is 2 or more and none of the omitted columns
2837 ** have a non-NULL default value. Also, the record must be left with
2838 ** at least one field. If P5>0 then it will be one more than the
2839 ** index of the right-most column with a non-NULL default value */
2840 if( pOp->p5 ){
2841 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2842 pLast--;
2843 nField--;
2844 }
2845 }
drhd447dce2017-01-25 20:55:11 +00002846#endif
drh585ce192017-01-25 14:58:27 +00002847
drhf3218fe2004-05-28 08:21:02 +00002848 /* Loop through the elements that will make up the record to figure
2849 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002850 */
drh038b7bc2013-12-09 23:17:22 +00002851 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002852 do{
drh2b4ded92010-09-27 21:09:31 +00002853 assert( memIsValid(pRec) );
drh41fb3672018-01-12 23:18:38 +00002854 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002855 if( pRec->flags & MEM_Zero ){
drhce2fbd12018-01-12 21:00:14 +00002856 if( serial_type==0 ){
drh41fb3672018-01-12 23:18:38 +00002857 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
2858 ** table methods that never invoke sqlite3_result_xxxxx() while
2859 ** computing an unchanging column value in an UPDATE statement.
2860 ** Give such values a special internal-use-only serial-type of 10
2861 ** so that they can be passed through to xUpdate and have
2862 ** a true sqlite3_value_nochange(). */
2863 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
2864 serial_type = 10;
drhce2fbd12018-01-12 21:00:14 +00002865 }else if( nData ){
drh53e66c32015-07-24 15:49:23 +00002866 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002867 }else{
2868 nZero += pRec->u.nZero;
2869 len -= pRec->u.nZero;
2870 }
drhfdf972a2007-05-02 13:30:27 +00002871 }
drh8079a0d2006-01-12 17:20:50 +00002872 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002873 testcase( serial_type==127 );
2874 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002875 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh41fb3672018-01-12 23:18:38 +00002876 pRec->uTemp = serial_type;
drh45c3c662016-04-07 14:16:16 +00002877 if( pRec==pData0 ) break;
2878 pRec--;
2879 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002880
drh654858d2014-11-20 02:18:14 +00002881 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2882 ** which determines the total number of bytes in the header. The varint
2883 ** value is the size of the header in bytes including the size varint
2884 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002885 testcase( nHdr==126 );
2886 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002887 if( nHdr<=126 ){
2888 /* The common case */
2889 nHdr += 1;
2890 }else{
2891 /* Rare case of a really large header */
2892 nVarint = sqlite3VarintLen(nHdr);
2893 nHdr += nVarint;
2894 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002895 }
drh038b7bc2013-12-09 23:17:22 +00002896 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002897 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002898 goto too_big;
2899 }
drhf3218fe2004-05-28 08:21:02 +00002900
danielk1977a7a8e142008-02-13 18:25:27 +00002901 /* Make sure the output register has a buffer large enough to store
2902 ** the new record. The output register (pOp->p3) is not allowed to
2903 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002904 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002905 */
drh322f2852014-09-19 00:43:39 +00002906 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002907 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002908 }
danielk1977a7a8e142008-02-13 18:25:27 +00002909 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002910
2911 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002912 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002913 j = nHdr;
2914 assert( pData0<=pLast );
2915 pRec = pData0;
2916 do{
drhfacf47a2014-10-13 20:12:47 +00002917 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002918 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2919 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002920 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002921 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2922 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002923 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002924 }while( (++pRec)<=pLast );
2925 assert( i==nHdr );
2926 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002927
drh9f6168b2016-03-19 23:32:58 +00002928 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002929 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002930 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002931 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002932 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002933 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002934 }
drh1013c932008-01-06 00:25:21 +00002935 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002936 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002937 break;
2938}
2939
danielk1977a5533162009-02-24 10:01:51 +00002940/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002941** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002942**
2943** Store the number of entries (an integer value) in the table or index
2944** opened by cursor P1 in register P2
2945*/
2946#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002947case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002948 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002949 BtCursor *pCrsr;
2950
drhc960dcb2015-11-20 19:22:01 +00002951 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2952 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002953 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002954 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002955 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002956 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002957 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002958 pOut->u.i = nEntry;
2959 break;
2960}
2961#endif
2962
danielk1977fd7f0452008-12-17 17:30:26 +00002963/* Opcode: Savepoint P1 * * P4 *
2964**
2965** Open, release or rollback the savepoint named by parameter P4, depending
2966** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2967** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2968*/
2969case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002970 int p1; /* Value of P1 operand */
2971 char *zName; /* Name of savepoint */
2972 int nName;
2973 Savepoint *pNew;
2974 Savepoint *pSavepoint;
2975 Savepoint *pTmp;
2976 int iSavepoint;
2977 int ii;
2978
2979 p1 = pOp->p1;
2980 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002981
2982 /* Assert that the p1 parameter is valid. Also that if there is no open
2983 ** transaction, then there cannot be any savepoints.
2984 */
2985 assert( db->pSavepoint==0 || db->autoCommit==0 );
2986 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2987 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2988 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002989 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002990
2991 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002992 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002993 /* A new savepoint cannot be created if there are active write
2994 ** statements (i.e. open read/write incremental blob handles).
2995 */
drh22c17b82015-05-15 04:13:15 +00002996 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002997 rc = SQLITE_BUSY;
2998 }else{
drh856c1032009-06-02 15:21:42 +00002999 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003000
drhbe07ec52011-06-03 12:15:26 +00003001#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003002 /* This call is Ok even if this savepoint is actually a transaction
3003 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3004 ** If this is a transaction savepoint being opened, it is guaranteed
3005 ** that the db->aVTrans[] array is empty. */
3006 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003007 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3008 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003009 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003010#endif
dand9495cd2011-04-27 12:08:04 +00003011
danielk1977fd7f0452008-12-17 17:30:26 +00003012 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003013 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003014 if( pNew ){
3015 pNew->zName = (char *)&pNew[1];
3016 memcpy(pNew->zName, zName, nName+1);
3017
3018 /* If there is no open transaction, then mark this as a special
3019 ** "transaction savepoint". */
3020 if( db->autoCommit ){
3021 db->autoCommit = 0;
3022 db->isTransactionSavepoint = 1;
3023 }else{
3024 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003025 }
dan21e8d012011-03-03 20:05:59 +00003026
danielk1977fd7f0452008-12-17 17:30:26 +00003027 /* Link the new savepoint into the database handle's list. */
3028 pNew->pNext = db->pSavepoint;
3029 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003030 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003031 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003032 }
3033 }
3034 }else{
drh856c1032009-06-02 15:21:42 +00003035 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003036
3037 /* Find the named savepoint. If there is no such savepoint, then an
3038 ** an error is returned to the user. */
3039 for(
drh856c1032009-06-02 15:21:42 +00003040 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003041 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003042 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003043 ){
3044 iSavepoint++;
3045 }
3046 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003047 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003048 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003049 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003050 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003051 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003052 */
drh22c17b82015-05-15 04:13:15 +00003053 sqlite3VdbeError(p, "cannot release savepoint - "
3054 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003055 rc = SQLITE_BUSY;
3056 }else{
3057
3058 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003059 ** and this is a RELEASE command, then the current transaction
3060 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003061 */
3062 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3063 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003064 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003065 goto vdbe_return;
3066 }
danielk1977fd7f0452008-12-17 17:30:26 +00003067 db->autoCommit = 1;
3068 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003069 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003070 db->autoCommit = 0;
3071 p->rc = rc = SQLITE_BUSY;
3072 goto vdbe_return;
3073 }
danielk197734cf35d2008-12-18 18:31:38 +00003074 db->isTransactionSavepoint = 0;
3075 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003076 }else{
drh47b7fc72014-11-11 01:33:57 +00003077 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003078 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003079 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003080 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003081 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003082 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3083 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003084 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003085 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003086 }
drh47b7fc72014-11-11 01:33:57 +00003087 }else{
3088 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003089 }
3090 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003091 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3092 if( rc!=SQLITE_OK ){
3093 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003094 }
danielk1977fd7f0452008-12-17 17:30:26 +00003095 }
drh47b7fc72014-11-11 01:33:57 +00003096 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003097 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003098 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003099 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003100 }
3101 }
3102
3103 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3104 ** savepoints nested inside of the savepoint being operated on. */
3105 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003106 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003107 db->pSavepoint = pTmp->pNext;
3108 sqlite3DbFree(db, pTmp);
3109 db->nSavepoint--;
3110 }
3111
dan1da40a32009-09-19 17:00:31 +00003112 /* If it is a RELEASE, then destroy the savepoint being operated on
3113 ** too. If it is a ROLLBACK TO, then set the number of deferred
3114 ** constraint violations present in the database to the value stored
3115 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003116 if( p1==SAVEPOINT_RELEASE ){
3117 assert( pSavepoint==db->pSavepoint );
3118 db->pSavepoint = pSavepoint->pNext;
3119 sqlite3DbFree(db, pSavepoint);
3120 if( !isTransaction ){
3121 db->nSavepoint--;
3122 }
dan1da40a32009-09-19 17:00:31 +00003123 }else{
3124 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003125 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003126 }
dand9495cd2011-04-27 12:08:04 +00003127
danea8562e2015-04-18 16:25:54 +00003128 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003129 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3130 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3131 }
danielk1977fd7f0452008-12-17 17:30:26 +00003132 }
3133 }
drh9467abf2016-02-17 18:44:11 +00003134 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003135
3136 break;
3137}
3138
drh98757152008-01-09 23:04:12 +00003139/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003140**
3141** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003142** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003143** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3144** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003145**
3146** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003147*/
drh9cbf3422008-01-17 16:22:13 +00003148case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003149 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003150 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003151
drh856c1032009-06-02 15:21:42 +00003152 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003153 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003154 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003155 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003156 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003157 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003158
drhb0c88652016-02-01 13:21:13 +00003159 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003160 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003161 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003162 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003163 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003164 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3165 /* If this instruction implements a COMMIT and other VMs are writing
3166 ** return an error indicating that the other VMs must complete first.
3167 */
3168 sqlite3VdbeError(p, "cannot commit transaction - "
3169 "SQL statements in progress");
3170 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003171 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003172 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003173 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003174 }else{
shane7d3846a2008-12-11 02:58:26 +00003175 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003176 }
3177 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3178 p->pc = (int)(pOp - aOp);
3179 db->autoCommit = (u8)(1-desiredAutoCommit);
3180 p->rc = rc = SQLITE_BUSY;
3181 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003182 }
danielk1977bd434552009-03-18 10:33:00 +00003183 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003184 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003185 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003186 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003187 }else{
drh900b31e2007-08-28 02:27:51 +00003188 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003189 }
drh900b31e2007-08-28 02:27:51 +00003190 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003191 }else{
drh22c17b82015-05-15 04:13:15 +00003192 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003193 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003194 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003195 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003196
3197 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003198 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003199 }
3200 break;
3201}
3202
drhb22f7c82014-02-06 23:56:27 +00003203/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003204**
drh05a86c52014-02-16 01:55:49 +00003205** Begin a transaction on database P1 if a transaction is not already
3206** active.
3207** If P2 is non-zero, then a write-transaction is started, or if a
3208** read-transaction is already active, it is upgraded to a write-transaction.
3209** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003210**
drh001bbcb2003-03-19 03:14:00 +00003211** P1 is the index of the database file on which the transaction is
3212** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003213** file used for temporary tables. Indices of 2 or more are used for
3214** attached databases.
drhcabb0812002-09-14 13:47:32 +00003215**
dane0af83a2009-09-08 19:15:01 +00003216** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3217** true (this flag is set if the Vdbe may modify more than one row and may
3218** throw an ABORT exception), a statement transaction may also be opened.
3219** More specifically, a statement transaction is opened iff the database
3220** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003221** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003222** VDBE to be rolled back after an error without having to roll back the
3223** entire transaction. If no error is encountered, the statement transaction
3224** will automatically commit when the VDBE halts.
3225**
drhb22f7c82014-02-06 23:56:27 +00003226** If P5!=0 then this opcode also checks the schema cookie against P3
3227** and the schema generation counter against P4.
3228** The cookie changes its value whenever the database schema changes.
3229** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003230** and that the current process needs to reread the schema. If the schema
3231** cookie in P3 differs from the schema cookie in the database header or
3232** if the schema generation counter in P4 differs from the current
3233** generation counter, then an SQLITE_SCHEMA error is raised and execution
3234** halts. The sqlite3_step() wrapper function might then reprepare the
3235** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003236*/
drh9cbf3422008-01-17 16:22:13 +00003237case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003238 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003239 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003240
drh1713afb2013-06-28 01:24:57 +00003241 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003242 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003243 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003244 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003245 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3246 rc = SQLITE_READONLY;
3247 goto abort_due_to_error;
3248 }
drh653b82a2009-06-22 11:10:47 +00003249 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003250
danielk197724162fe2004-06-04 06:22:00 +00003251 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003252 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003253 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3254 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003255 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003256 if( (rc&0xff)==SQLITE_BUSY ){
3257 p->pc = (int)(pOp - aOp);
3258 p->rc = rc;
3259 goto vdbe_return;
3260 }
danielk197724162fe2004-06-04 06:22:00 +00003261 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003262 }
dane0af83a2009-09-08 19:15:01 +00003263
3264 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003265 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003266 ){
3267 assert( sqlite3BtreeIsInTrans(pBt) );
3268 if( p->iStatement==0 ){
3269 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3270 db->nStatement++;
3271 p->iStatement = db->nSavepoint + db->nStatement;
3272 }
dana311b802011-04-26 19:21:34 +00003273
drh346506f2011-05-25 01:16:42 +00003274 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003275 if( rc==SQLITE_OK ){
3276 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3277 }
dan1da40a32009-09-19 17:00:31 +00003278
3279 /* Store the current value of the database handles deferred constraint
3280 ** counter. If the statement transaction needs to be rolled back,
3281 ** the value of this counter needs to be restored too. */
3282 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003283 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003284 }
drh397776a2018-06-06 17:45:51 +00003285 }
3286 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3287 if( pOp->p5
3288 && (iMeta!=pOp->p3
3289 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3290 ){
3291 /*
drh96fdcb42016-09-27 00:09:33 +00003292 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3293 ** version is checked to ensure that the schema has not changed since the
3294 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003295 */
drhb22f7c82014-02-06 23:56:27 +00003296 sqlite3DbFree(db, p->zErrMsg);
3297 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3298 /* If the schema-cookie from the database file matches the cookie
3299 ** stored with the in-memory representation of the schema, do
3300 ** not reload the schema from the database file.
3301 **
3302 ** If virtual-tables are in use, this is not just an optimization.
3303 ** Often, v-tables store their data in other SQLite tables, which
3304 ** are queried from within xNext() and other v-table methods using
3305 ** prepared queries. If such a query is out-of-date, we do not want to
3306 ** discard the database schema, as the user code implementing the
3307 ** v-table would have to be ready for the sqlite3_vtab structure itself
3308 ** to be invalidated whenever sqlite3_step() is called from within
3309 ** a v-table method.
3310 */
3311 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3312 sqlite3ResetOneSchema(db, pOp->p1);
3313 }
3314 p->expired = 1;
3315 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003316 }
drh9467abf2016-02-17 18:44:11 +00003317 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003318 break;
3319}
3320
drhb1fdb2a2008-01-05 04:06:03 +00003321/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003322**
drh9cbf3422008-01-17 16:22:13 +00003323** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003324** P3==1 is the schema version. P3==2 is the database format.
3325** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003326** the main database file and P1==1 is the database file used to store
3327** temporary tables.
drh4a324312001-12-21 14:30:42 +00003328**
drh50e5dad2001-09-15 00:57:28 +00003329** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003330** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003331** executing this instruction.
3332*/
drh27a348c2015-04-13 19:14:06 +00003333case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003334 int iMeta;
drh856c1032009-06-02 15:21:42 +00003335 int iDb;
3336 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003337
drh1713afb2013-06-28 01:24:57 +00003338 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003339 iDb = pOp->p1;
3340 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003341 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003342 assert( iDb>=0 && iDb<db->nDb );
3343 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003344 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003345
danielk1977602b4662009-07-02 07:47:33 +00003346 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003347 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003348 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003349 break;
3350}
3351
drh98757152008-01-09 23:04:12 +00003352/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003353**
drh1861afc2016-02-01 21:48:34 +00003354** Write the integer value P3 into cookie number P2 of database P1.
3355** P2==1 is the schema version. P2==2 is the database format.
3356** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003357** size, and so forth. P1==0 is the main database file and P1==1 is the
3358** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003359**
3360** A transaction must be started before executing this opcode.
3361*/
drh1861afc2016-02-01 21:48:34 +00003362case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003363 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003364
3365 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003366 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003367 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003368 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003369 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003370 pDb = &db->aDb[pOp->p1];
3371 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003372 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003373 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003374 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003375 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003376 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003377 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003378 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003379 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003380 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003381 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003382 }
drhfd426c62006-01-30 15:34:22 +00003383 if( pOp->p1==1 ){
3384 /* Invalidate all prepared statements whenever the TEMP database
3385 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003386 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003387 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003388 }
drh9467abf2016-02-17 18:44:11 +00003389 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003390 break;
3391}
3392
drh98757152008-01-09 23:04:12 +00003393/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003394** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003395**
drhecdc7532001-09-23 02:35:53 +00003396** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003397** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003398** P3==0 means the main database, P3==1 means the database used for
3399** temporary tables, and P3>1 means used the corresponding attached
3400** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003401** values need not be contiguous but all P1 values should be small integers.
3402** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003403**
drh8e9deb62018-06-05 13:43:02 +00003404** Allowed P5 bits:
3405** <ul>
3406** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3407** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3408** of OP_SeekLE/OP_IdxGT)
3409** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003410**
danielk1977d336e222009-02-20 10:58:41 +00003411** The P4 value may be either an integer (P4_INT32) or a pointer to
3412** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003413** object, then table being opened must be an [index b-tree] where the
3414** KeyInfo object defines the content and collating
3415** sequence of that index b-tree. Otherwise, if P4 is an integer
3416** value, then the table being opened must be a [table b-tree] with a
3417** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003418**
drh35263192014-07-22 20:02:19 +00003419** See also: OpenWrite, ReopenIdx
3420*/
3421/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3422** Synopsis: root=P2 iDb=P3
3423**
drh8e9deb62018-06-05 13:43:02 +00003424** The ReopenIdx opcode works like OP_OpenRead except that it first
3425** checks to see if the cursor on P1 is already open on the same
3426** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003427** if the cursor is already open, do not reopen it.
3428**
drh8e9deb62018-06-05 13:43:02 +00003429** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3430** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3431** be the same as every other ReopenIdx or OpenRead for the same cursor
3432** number.
drh35263192014-07-22 20:02:19 +00003433**
drh8e9deb62018-06-05 13:43:02 +00003434** Allowed P5 bits:
3435** <ul>
3436** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3437** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3438** of OP_SeekLE/OP_IdxGT)
3439** </ul>
3440**
3441** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003442*/
drh98757152008-01-09 23:04:12 +00003443/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003444** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003445**
3446** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003447** page is P2 (or whose root page is held in register P2 if the
3448** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003449**
danielk1977d336e222009-02-20 10:58:41 +00003450** The P4 value may be either an integer (P4_INT32) or a pointer to
3451** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003452** object, then table being opened must be an [index b-tree] where the
3453** KeyInfo object defines the content and collating
3454** sequence of that index b-tree. Otherwise, if P4 is an integer
3455** value, then the table being opened must be a [table b-tree] with a
3456** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003457**
drh8e9deb62018-06-05 13:43:02 +00003458** Allowed P5 bits:
3459** <ul>
3460** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3461** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3462** of OP_SeekLE/OP_IdxGT)
3463** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3464** and subsequently delete entries in an index btree. This is a
3465** hint to the storage engine that the storage engine is allowed to
3466** ignore. The hint is not used by the official SQLite b*tree storage
3467** engine, but is used by COMDB2.
3468** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3469** as the root page, not the value of P2 itself.
3470** </ul>
drhf57b3392001-10-08 13:22:32 +00003471**
drh8e9deb62018-06-05 13:43:02 +00003472** This instruction works like OpenRead except that it opens the cursor
3473** in read/write mode.
3474**
3475** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003476*/
drh35263192014-07-22 20:02:19 +00003477case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003478 int nField;
3479 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003480 int p2;
3481 int iDb;
drhf57b3392001-10-08 13:22:32 +00003482 int wrFlag;
3483 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003484 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003485 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003486
drhe0997b32015-03-20 14:57:50 +00003487 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003488 assert( pOp->p4type==P4_KEYINFO );
3489 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003490 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003491 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003492 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003493 }
3494 /* If the cursor is not currently open or is open on a different
3495 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003496case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003497case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003498
drhe0997b32015-03-20 14:57:50 +00003499 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003500 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003501 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3502 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003503
drhba968db2018-07-24 22:02:12 +00003504 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003505 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003506 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003507 }
3508
drh856c1032009-06-02 15:21:42 +00003509 nField = 0;
3510 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003511 p2 = pOp->p2;
3512 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003513 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003514 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003515 pDb = &db->aDb[iDb];
3516 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003517 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003518 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003519 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3520 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003521 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003522 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3523 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003524 }
3525 }else{
3526 wrFlag = 0;
3527 }
dan428c2182012-08-06 18:50:11 +00003528 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003529 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003530 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003531 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003532 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003533 assert( memIsValid(pIn2) );
3534 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003535 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003536 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003537 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003538 ** that opcode will always set the p2 value to 2 or more or else fail.
3539 ** If there were a failure, the prepared statement would have halted
3540 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003541 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003542 }
danielk1977d336e222009-02-20 10:58:41 +00003543 if( pOp->p4type==P4_KEYINFO ){
3544 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003545 assert( pKeyInfo->enc==ENC(db) );
3546 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003547 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003548 }else if( pOp->p4type==P4_INT32 ){
3549 nField = pOp->p4.i;
3550 }
drh653b82a2009-06-22 11:10:47 +00003551 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003552 assert( nField>=0 );
3553 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003554 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003555 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003556 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003557 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003558 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003559#ifdef SQLITE_DEBUG
3560 pCur->wrFlag = wrFlag;
3561#endif
drhc960dcb2015-11-20 19:22:01 +00003562 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003563 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003564 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003565 ** SQLite used to check if the root-page flags were sane at this point
3566 ** and report database corruption if they were not, but this check has
3567 ** since moved into the btree layer. */
3568 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003569
3570open_cursor_set_hints:
3571 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3572 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003573 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003574#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003575 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3576#endif
drhc960dcb2015-11-20 19:22:01 +00003577 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003578 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003579 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003580 break;
3581}
3582
drhe08e8d62017-05-01 15:15:41 +00003583/* Opcode: OpenDup P1 P2 * * *
3584**
3585** Open a new cursor P1 that points to the same ephemeral table as
3586** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3587** opcode. Only ephemeral cursors may be duplicated.
3588**
3589** Duplicate ephemeral cursors are used for self-joins of materialized views.
3590*/
3591case OP_OpenDup: {
3592 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3593 VdbeCursor *pCx; /* The new cursor */
3594
3595 pOrig = p->apCsr[pOp->p2];
3596 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3597
3598 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3599 if( pCx==0 ) goto no_mem;
3600 pCx->nullRow = 1;
3601 pCx->isEphemeral = 1;
3602 pCx->pKeyInfo = pOrig->pKeyInfo;
3603 pCx->isTable = pOrig->isTable;
3604 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
3605 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003606 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3607 ** opened for a database. Since there is already an open cursor when this
3608 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3609 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003610 break;
3611}
3612
3613
drh2a5d9902011-08-26 00:34:45 +00003614/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003615** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003616**
drhb9bb7c12006-06-11 23:41:55 +00003617** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003618** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003619** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003620** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003621**
drh25d3adb2010-04-05 15:11:08 +00003622** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003623** The cursor points to a BTree table if P4==0 and to a BTree index
3624** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003625** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003626**
drh2a5d9902011-08-26 00:34:45 +00003627** The P5 parameter can be a mask of the BTREE_* flags defined
3628** in btree.h. These flags control aspects of the operation of
3629** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3630** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003631*/
drha21a64d2010-04-06 22:33:55 +00003632/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003633** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003634**
3635** This opcode works the same as OP_OpenEphemeral. It has a
3636** different name to distinguish its use. Tables created using
3637** by this opcode will be used for automatically created transient
3638** indices in joins.
3639*/
3640case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003641case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003642 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003643 KeyInfo *pKeyInfo;
3644
drhd4187c72010-08-30 22:15:45 +00003645 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003646 SQLITE_OPEN_READWRITE |
3647 SQLITE_OPEN_CREATE |
3648 SQLITE_OPEN_EXCLUSIVE |
3649 SQLITE_OPEN_DELETEONCLOSE |
3650 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003651 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003652 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003653 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003654 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003655 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003656 pCx->isEphemeral = 1;
drhfbd8cbd2016-12-10 12:58:15 +00003657 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
drhd4187c72010-08-30 22:15:45 +00003658 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003659 if( rc==SQLITE_OK ){
drhbb2d9b12018-06-06 16:28:40 +00003660 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00003661 }
3662 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003663 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003664 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003665 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003666 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003667 */
drhfbd8cbd2016-12-10 12:58:15 +00003668 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003669 int pgno;
drh66a51672008-01-03 00:01:23 +00003670 assert( pOp->p4type==P4_KEYINFO );
drhfbd8cbd2016-12-10 12:58:15 +00003671 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003672 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003673 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003674 assert( pKeyInfo->db==db );
3675 assert( pKeyInfo->enc==ENC(db) );
drhfbd8cbd2016-12-10 12:58:15 +00003676 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003677 pKeyInfo, pCx->uc.pCursor);
drhc6b52df2002-01-04 03:09:29 +00003678 }
drhf0863fe2005-06-12 21:35:51 +00003679 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003680 }else{
drhfbd8cbd2016-12-10 12:58:15 +00003681 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003682 0, pCx->uc.pCursor);
drhf0863fe2005-06-12 21:35:51 +00003683 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003684 }
drh5e00f6c2001-09-13 13:46:56 +00003685 }
drh9467abf2016-02-17 18:44:11 +00003686 if( rc ) goto abort_due_to_error;
drhd4187c72010-08-30 22:15:45 +00003687 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003688 break;
3689}
3690
danfad9f9a2014-04-01 18:41:51 +00003691/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003692**
3693** This opcode works like OP_OpenEphemeral except that it opens
3694** a transient index that is specifically designed to sort large
3695** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003696**
3697** If argument P3 is non-zero, then it indicates that the sorter may
3698** assume that a stable sort considering the first P3 fields of each
3699** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003700*/
drhca892a72011-09-03 00:17:51 +00003701case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003702 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003703
drh399af1d2013-11-20 17:25:55 +00003704 assert( pOp->p1>=0 );
3705 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003706 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003707 if( pCx==0 ) goto no_mem;
3708 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003709 assert( pCx->pKeyInfo->db==db );
3710 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003711 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003712 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003713 break;
3714}
3715
dan78d58432014-03-25 15:04:07 +00003716/* Opcode: SequenceTest P1 P2 * * *
3717** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3718**
3719** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3720** to P2. Regardless of whether or not the jump is taken, increment the
3721** the sequence value.
3722*/
3723case OP_SequenceTest: {
3724 VdbeCursor *pC;
3725 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3726 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003727 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003728 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003729 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003730 }
drh5e00f6c2001-09-13 13:46:56 +00003731 break;
3732}
3733
drh5f612292014-02-08 23:20:32 +00003734/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003735** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003736**
3737** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003738** row of data. The content of that one row is the content of memory
3739** register P2. In other words, cursor P1 becomes an alias for the
3740** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003741**
drh2d8d7ce2010-02-15 15:17:05 +00003742** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003743** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003744** individual columns using the OP_Column opcode. The OP_Column opcode
3745** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003746**
3747** P3 is the number of fields in the records that will be stored by
3748** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003749*/
drh9cbf3422008-01-17 16:22:13 +00003750case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003751 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003752
drh653b82a2009-06-22 11:10:47 +00003753 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003754 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003755 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003756 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003757 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003758 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003759 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003760 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3761 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3762 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3763 ** which is a performance optimization */
3764 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003765 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003766 break;
3767}
3768
drh98757152008-01-09 23:04:12 +00003769/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003770**
3771** Close a cursor previously opened as P1. If P1 is not
3772** currently open, this instruction is a no-op.
3773*/
drh9cbf3422008-01-17 16:22:13 +00003774case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003775 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3776 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3777 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003778 break;
3779}
3780
drh97bae792015-06-05 15:59:57 +00003781#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3782/* Opcode: ColumnsUsed P1 * * P4 *
3783**
3784** This opcode (which only exists if SQLite was compiled with
3785** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3786** table or index for cursor P1 are used. P4 is a 64-bit integer
3787** (P4_INT64) in which the first 63 bits are one for each of the
3788** first 63 columns of the table or index that are actually used
3789** by the cursor. The high-order bit is set if any column after
3790** the 64th is used.
3791*/
3792case OP_ColumnsUsed: {
3793 VdbeCursor *pC;
3794 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003795 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003796 pC->maskUsed = *(u64*)pOp->p4.pI64;
3797 break;
3798}
3799#endif
3800
drh8af3f772014-07-25 18:01:06 +00003801/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003802** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003803**
danielk1977b790c6c2008-04-18 10:25:24 +00003804** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003805** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003806** to an SQL index, then P3 is the first in an array of P4 registers
3807** that are used as an unpacked index key.
3808**
3809** Reposition cursor P1 so that it points to the smallest entry that
3810** is greater than or equal to the key value. If there are no records
3811** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003812**
drhb1d607d2015-11-05 22:30:54 +00003813** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3814** opcode will always land on a record that equally equals the key, or
3815** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3816** opcode must be followed by an IdxLE opcode with the same arguments.
3817** The IdxLE opcode will be skipped if this opcode succeeds, but the
3818** IdxLE opcode will be used on subsequent loop iterations.
3819**
drh8af3f772014-07-25 18:01:06 +00003820** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003821** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003822** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003823**
drh935850e2014-05-24 17:15:15 +00003824** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003825*/
drh8af3f772014-07-25 18:01:06 +00003826/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003827** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003828**
danielk1977b790c6c2008-04-18 10:25:24 +00003829** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003830** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003831** to an SQL index, then P3 is the first in an array of P4 registers
3832** that are used as an unpacked index key.
3833**
3834** Reposition cursor P1 so that it points to the smallest entry that
3835** is greater than the key value. If there are no records greater than
3836** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003837**
drh8af3f772014-07-25 18:01:06 +00003838** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003839** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003840** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003841**
drh935850e2014-05-24 17:15:15 +00003842** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003843*/
drh8af3f772014-07-25 18:01:06 +00003844/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003845** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003846**
danielk1977b790c6c2008-04-18 10:25:24 +00003847** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003848** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003849** to an SQL index, then P3 is the first in an array of P4 registers
3850** that are used as an unpacked index key.
3851**
3852** Reposition cursor P1 so that it points to the largest entry that
3853** is less than the key value. If there are no records less than
3854** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003855**
drh8af3f772014-07-25 18:01:06 +00003856** This opcode leaves the cursor configured to move in reverse order,
3857** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003858** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003859**
drh935850e2014-05-24 17:15:15 +00003860** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003861*/
drh8af3f772014-07-25 18:01:06 +00003862/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003863** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003864**
danielk1977b790c6c2008-04-18 10:25:24 +00003865** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003866** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003867** to an SQL index, then P3 is the first in an array of P4 registers
3868** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003869**
danielk1977b790c6c2008-04-18 10:25:24 +00003870** Reposition cursor P1 so that it points to the largest entry that
3871** is less than or equal to the key value. If there are no records
3872** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003873**
drh8af3f772014-07-25 18:01:06 +00003874** This opcode leaves the cursor configured to move in reverse order,
3875** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003876** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003877**
drhb1d607d2015-11-05 22:30:54 +00003878** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3879** opcode will always land on a record that equally equals the key, or
3880** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3881** opcode must be followed by an IdxGE opcode with the same arguments.
3882** The IdxGE opcode will be skipped if this opcode succeeds, but the
3883** IdxGE opcode will be used on subsequent loop iterations.
3884**
drh935850e2014-05-24 17:15:15 +00003885** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003886*/
mistachkin758784d2018-07-25 15:12:29 +00003887case OP_SeekLT: /* jump, in3, group */
3888case OP_SeekLE: /* jump, in3, group */
3889case OP_SeekGE: /* jump, in3, group */
3890case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00003891 int res; /* Comparison result */
3892 int oc; /* Opcode */
3893 VdbeCursor *pC; /* The cursor to seek */
3894 UnpackedRecord r; /* The key to seek for */
3895 int nField; /* Number of columns or fields in the key */
3896 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003897 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003898
drh653b82a2009-06-22 11:10:47 +00003899 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003900 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003901 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003902 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003903 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003904 assert( OP_SeekLE == OP_SeekLT+1 );
3905 assert( OP_SeekGE == OP_SeekLT+2 );
3906 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003907 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003908 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003909 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003910 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003911 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003912#ifdef SQLITE_DEBUG
3913 pC->seekOp = pOp->opcode;
3914#endif
drhe0997b32015-03-20 14:57:50 +00003915
drh3da046d2013-11-11 03:24:11 +00003916 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003917 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003918 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3919 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003920
drh3da046d2013-11-11 03:24:11 +00003921 /* The input value in P3 might be of any type: integer, real, string,
3922 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003923 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003924 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003925 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003926 applyNumericAffinity(pIn3, 0);
3927 }
drh3da046d2013-11-11 03:24:11 +00003928 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003929
drh3da046d2013-11-11 03:24:11 +00003930 /* If the P3 value could not be converted into an integer without
3931 ** loss of information, then special processing is required... */
3932 if( (pIn3->flags & MEM_Int)==0 ){
3933 if( (pIn3->flags & MEM_Real)==0 ){
3934 /* If the P3 value cannot be converted into any kind of a number,
3935 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003936 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003937 break;
3938 }
drh959403f2008-12-12 17:56:16 +00003939
danaa1776f2013-11-26 18:22:59 +00003940 /* If the approximation iKey is larger than the actual real search
3941 ** term, substitute >= for > and < for <=. e.g. if the search term
3942 ** is 4.9 and the integer approximation 5:
3943 **
3944 ** (x > 4.9) -> (x >= 5)
3945 ** (x <= 4.9) -> (x < 5)
3946 */
drh74eaba42014-09-18 17:52:15 +00003947 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003948 assert( OP_SeekGE==(OP_SeekGT-1) );
3949 assert( OP_SeekLT==(OP_SeekLE-1) );
3950 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3951 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003952 }
3953
3954 /* If the approximation iKey is smaller than the actual real search
3955 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003956 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003957 assert( OP_SeekLE==(OP_SeekLT+1) );
3958 assert( OP_SeekGT==(OP_SeekGE+1) );
3959 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3960 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003961 }
drh3da046d2013-11-11 03:24:11 +00003962 }
drhc960dcb2015-11-20 19:22:01 +00003963 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003964 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003965 if( rc!=SQLITE_OK ){
3966 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003967 }
drhaa736092009-06-22 00:55:30 +00003968 }else{
drhd6b79462015-11-07 01:19:00 +00003969 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3970 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3971 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3972 */
drhc960dcb2015-11-20 19:22:01 +00003973 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00003974 eqOnly = 1;
3975 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3976 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3977 assert( pOp[1].p1==pOp[0].p1 );
3978 assert( pOp[1].p2==pOp[0].p2 );
3979 assert( pOp[1].p3==pOp[0].p3 );
3980 assert( pOp[1].p4.i==pOp[0].p4.i );
3981 }
3982
drh3da046d2013-11-11 03:24:11 +00003983 nField = pOp->p4.i;
3984 assert( pOp->p4type==P4_INT32 );
3985 assert( nField>0 );
3986 r.pKeyInfo = pC->pKeyInfo;
3987 r.nField = (u16)nField;
3988
3989 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003990 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003991 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003992 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003993 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003994 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003995 */
dan1fed5da2014-02-25 21:01:25 +00003996 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3997 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3998 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3999 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4000 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004001
4002 r.aMem = &aMem[pOp->p3];
4003#ifdef SQLITE_DEBUG
4004 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4005#endif
drh70528d72015-11-05 20:25:09 +00004006 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004007 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004008 if( rc!=SQLITE_OK ){
4009 goto abort_due_to_error;
4010 }
drhb1d607d2015-11-05 22:30:54 +00004011 if( eqOnly && r.eqSeen==0 ){
4012 assert( res!=0 );
4013 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004014 }
drh3da046d2013-11-11 03:24:11 +00004015 }
4016 pC->deferredMoveto = 0;
4017 pC->cacheStatus = CACHE_STALE;
4018#ifdef SQLITE_TEST
4019 sqlite3_search_count++;
4020#endif
drh4a1d3652014-02-14 15:13:36 +00004021 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4022 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004023 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004024 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4025 if( rc!=SQLITE_OK ){
4026 if( rc==SQLITE_DONE ){
4027 rc = SQLITE_OK;
4028 res = 1;
4029 }else{
4030 goto abort_due_to_error;
4031 }
4032 }
drh3da046d2013-11-11 03:24:11 +00004033 }else{
4034 res = 0;
4035 }
4036 }else{
drh4a1d3652014-02-14 15:13:36 +00004037 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4038 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004039 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004040 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4041 if( rc!=SQLITE_OK ){
4042 if( rc==SQLITE_DONE ){
4043 rc = SQLITE_OK;
4044 res = 1;
4045 }else{
4046 goto abort_due_to_error;
4047 }
4048 }
drh3da046d2013-11-11 03:24:11 +00004049 }else{
4050 /* res might be negative because the table is empty. Check to
4051 ** see if this is the case.
4052 */
drhc960dcb2015-11-20 19:22:01 +00004053 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004054 }
4055 }
drhb1d607d2015-11-05 22:30:54 +00004056seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004057 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004058 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004059 if( res ){
drhf56fa462015-04-13 21:39:54 +00004060 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004061 }else if( eqOnly ){
4062 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4063 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004064 }
drh5e00f6c2001-09-13 13:46:56 +00004065 break;
4066}
dan71c57db2016-07-09 20:23:55 +00004067
drh8c2b6d72018-06-05 20:45:20 +00004068/* Opcode: SeekHit P1 P2 * * *
4069** Synopsis: seekHit=P2
4070**
4071** Set the seekHit flag on cursor P1 to the value in P2.
4072** The seekHit flag is used by the IfNoHope opcode.
4073**
4074** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4075** either 0 or 1.
4076*/
4077case OP_SeekHit: {
4078 VdbeCursor *pC;
4079 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4080 pC = p->apCsr[pOp->p1];
4081 assert( pC!=0 );
4082 assert( pOp->p2==0 || pOp->p2==1 );
4083 pC->seekHit = pOp->p2 & 1;
4084 break;
4085}
4086
drh8cff69d2009-11-12 19:59:44 +00004087/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004088** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004089**
drh8cff69d2009-11-12 19:59:44 +00004090** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4091** P4>0 then register P3 is the first of P4 registers that form an unpacked
4092** record.
4093**
4094** Cursor P1 is on an index btree. If the record identified by P3 and P4
4095** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004096** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004097**
drhcefc87f2014-08-01 01:40:33 +00004098** This operation leaves the cursor in a state where it can be
4099** advanced in the forward direction. The Next instruction will work,
4100** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004101**
drh6f225d02013-10-26 13:36:51 +00004102** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004103*/
drh8cff69d2009-11-12 19:59:44 +00004104/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004105** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004106**
drh8cff69d2009-11-12 19:59:44 +00004107** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4108** P4>0 then register P3 is the first of P4 registers that form an unpacked
4109** record.
4110**
4111** Cursor P1 is on an index btree. If the record identified by P3 and P4
4112** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4113** does contain an entry whose prefix matches the P3/P4 record then control
4114** falls through to the next instruction and P1 is left pointing at the
4115** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004116**
drh8af3f772014-07-25 18:01:06 +00004117** This operation leaves the cursor in a state where it cannot be
4118** advanced in either direction. In other words, the Next and Prev
4119** opcodes do not work after this operation.
4120**
drh8c2b6d72018-06-05 20:45:20 +00004121** See also: Found, NotExists, NoConflict, IfNoHope
4122*/
4123/* Opcode: IfNoHope P1 P2 P3 P4 *
4124** Synopsis: key=r[P3@P4]
4125**
4126** Register P3 is the first of P4 registers that form an unpacked
4127** record.
4128**
4129** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4130** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4131** check to see if there is any entry in P1 that matches the
4132** prefix identified by P3 and P4. If no entry matches the prefix,
4133** jump to P2. Otherwise fall through.
4134**
4135** This opcode behaves like OP_NotFound if the seekHit
4136** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4137**
4138** This opcode is used in IN clause processing for a multi-column key.
4139** If an IN clause is attached to an element of the key other than the
4140** left-most element, and if there are no matches on the most recent
4141** seek over the whole key, then it might be that one of the key element
4142** to the left is prohibiting a match, and hence there is "no hope" of
4143** any match regardless of how many IN clause elements are checked.
4144** In such a case, we abandon the IN clause search early, using this
4145** opcode. The opcode name comes from the fact that the
4146** jump is taken if there is "no hope" of achieving a match.
4147**
4148** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004149*/
drh6f225d02013-10-26 13:36:51 +00004150/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004151** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004152**
4153** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4154** P4>0 then 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 record identified by P3 and P4
4158** contains any NULL value, jump immediately to P2. If all terms of the
4159** record are not-NULL then a check is done to determine if any row in the
4160** P1 index btree has a matching key prefix. If there are no matches, jump
4161** immediately to P2. If there is a match, fall through and leave the P1
4162** cursor pointing to the matching row.
4163**
4164** This opcode is similar to OP_NotFound with the exceptions that the
4165** branch is always taken if any part of the search key input is NULL.
4166**
drh8af3f772014-07-25 18:01:06 +00004167** This operation leaves the cursor in a state where it cannot be
4168** advanced in either direction. In other words, the Next and Prev
4169** opcodes do not work after this operation.
4170**
drh6f225d02013-10-26 13:36:51 +00004171** See also: NotFound, Found, NotExists
4172*/
drh8c2b6d72018-06-05 20:45:20 +00004173case OP_IfNoHope: { /* jump, in3 */
4174 VdbeCursor *pC;
4175 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4176 pC = p->apCsr[pOp->p1];
4177 assert( pC!=0 );
4178 if( pC->seekHit ) break;
4179 /* Fall through into OP_NotFound */
4180}
drh6f225d02013-10-26 13:36:51 +00004181case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004182case OP_NotFound: /* jump, in3 */
4183case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004184 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004185 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004186 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004187 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004188 int res;
drha582b012016-12-21 19:45:54 +00004189 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004190 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004191 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004192
dan0ff297e2009-09-25 17:03:14 +00004193#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004194 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004195#endif
4196
drhaa736092009-06-22 00:55:30 +00004197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004198 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004199 pC = p->apCsr[pOp->p1];
4200 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004201#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004202 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004203#endif
drh3c657212009-11-17 23:59:58 +00004204 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004205 assert( pC->eCurType==CURTYPE_BTREE );
4206 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004207 assert( pC->isTable==0 );
4208 if( pOp->p4.i>0 ){
4209 r.pKeyInfo = pC->pKeyInfo;
4210 r.nField = (u16)pOp->p4.i;
4211 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004212#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004213 for(ii=0; ii<r.nField; ii++){
4214 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004215 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004216 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004217 }
drh8aaf7bc2016-09-20 01:19:18 +00004218#endif
drh3da046d2013-11-11 03:24:11 +00004219 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004220 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004221 }else{
drhe46515b2017-05-19 22:51:00 +00004222 assert( pIn3->flags & MEM_Blob );
4223 rc = ExpandBlob(pIn3);
4224 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4225 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004226 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004227 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004228 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004229 }
dan1fed5da2014-02-25 21:01:25 +00004230 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004231 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004232 if( pOp->opcode==OP_NoConflict ){
4233 /* For the OP_NoConflict opcode, take the jump if any of the
4234 ** input fields are NULL, since any key with a NULL will not
4235 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004236 for(ii=0; ii<pIdxKey->nField; ii++){
4237 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004238 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004239 break;
drh6f225d02013-10-26 13:36:51 +00004240 }
4241 }
drh5e00f6c2001-09-13 13:46:56 +00004242 }
drhc960dcb2015-11-20 19:22:01 +00004243 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004244 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004245 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004246 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004247 }
4248 pC->seekResult = res;
4249 alreadyExists = (res==0);
4250 pC->nullRow = 1-alreadyExists;
4251 pC->deferredMoveto = 0;
4252 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004253 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004254 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004255 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004256 }else{
drhf56fa462015-04-13 21:39:54 +00004257 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4258 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004259 }
drh5e00f6c2001-09-13 13:46:56 +00004260 break;
4261}
4262
drheeb95652016-05-26 20:56:38 +00004263/* Opcode: SeekRowid P1 P2 P3 * *
4264** Synopsis: intkey=r[P3]
4265**
4266** P1 is the index of a cursor open on an SQL table btree (with integer
4267** keys). If register P3 does not contain an integer or if P1 does not
4268** contain a record with rowid P3 then jump immediately to P2.
4269** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4270** a record with rowid P3 then
4271** leave the cursor pointing at that record and fall through to the next
4272** instruction.
4273**
4274** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4275** the P3 register must be guaranteed to contain an integer value. With this
4276** opcode, register P3 might not contain an integer.
4277**
4278** The OP_NotFound opcode performs the same operation on index btrees
4279** (with arbitrary multi-value keys).
4280**
4281** This opcode leaves the cursor in a state where it cannot be advanced
4282** in either direction. In other words, the Next and Prev opcodes will
4283** not work following this opcode.
4284**
4285** See also: Found, NotFound, NoConflict, SeekRowid
4286*/
drh9cbf3422008-01-17 16:22:13 +00004287/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004288** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004289**
drh261c02d2013-10-25 14:46:15 +00004290** P1 is the index of a cursor open on an SQL table btree (with integer
4291** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004292** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4293** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4294** leave the cursor pointing at that record and fall through to the next
4295** instruction.
drh6b125452002-01-28 15:53:03 +00004296**
drheeb95652016-05-26 20:56:38 +00004297** The OP_SeekRowid opcode performs the same operation but also allows the
4298** P3 register to contain a non-integer value, in which case the jump is
4299** always taken. This opcode requires that P3 always contain an integer.
4300**
drh261c02d2013-10-25 14:46:15 +00004301** The OP_NotFound opcode performs the same operation on index btrees
4302** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004303**
drh8af3f772014-07-25 18:01:06 +00004304** This opcode leaves the cursor in a state where it cannot be advanced
4305** in either direction. In other words, the Next and Prev opcodes will
4306** not work following this opcode.
4307**
drheeb95652016-05-26 20:56:38 +00004308** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004309*/
drheeb95652016-05-26 20:56:38 +00004310case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004311 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004312 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004313 int res;
4314 u64 iKey;
4315
drh3c657212009-11-17 23:59:58 +00004316 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004317 if( (pIn3->flags & MEM_Int)==0 ){
drhe4fe6d42018-08-03 15:58:07 +00004318 /* Make sure pIn3->u.i contains a valid integer representation of
4319 ** the key value, but do not change the datatype of the register, as
4320 ** other parts of the perpared statement might be depending on the
4321 ** current datatype. */
4322 u16 origFlags = pIn3->flags;
4323 int isNotInt;
drheeb95652016-05-26 20:56:38 +00004324 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
drhe4fe6d42018-08-03 15:58:07 +00004325 isNotInt = (pIn3->flags & MEM_Int)==0;
4326 pIn3->flags = origFlags;
4327 if( isNotInt ) goto jump_to_p2;
drheeb95652016-05-26 20:56:38 +00004328 }
4329 /* Fall through into OP_NotExists */
4330case OP_NotExists: /* jump, in3 */
4331 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00004332 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00004333 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4334 pC = p->apCsr[pOp->p1];
4335 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004336#ifdef SQLITE_DEBUG
drhcf025a82018-06-07 18:01:21 +00004337 pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004338#endif
drhaa736092009-06-22 00:55:30 +00004339 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004340 assert( pC->eCurType==CURTYPE_BTREE );
4341 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004342 assert( pCrsr!=0 );
4343 res = 0;
4344 iKey = pIn3->u.i;
4345 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004346 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004347 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004348 pC->nullRow = 0;
4349 pC->cacheStatus = CACHE_STALE;
4350 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004351 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004352 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004353 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004354 assert( rc==SQLITE_OK );
4355 if( pOp->p2==0 ){
4356 rc = SQLITE_CORRUPT_BKPT;
4357 }else{
4358 goto jump_to_p2;
4359 }
danc6157e12015-09-14 09:23:47 +00004360 }
drh9467abf2016-02-17 18:44:11 +00004361 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004362 break;
4363}
4364
drh4c583122008-01-04 22:01:03 +00004365/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004366** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004367**
drh4c583122008-01-04 22:01:03 +00004368** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004369** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004370** The sequence number on the cursor is incremented after this
4371** instruction.
drh4db38a72005-09-01 12:16:28 +00004372*/
drh27a348c2015-04-13 19:14:06 +00004373case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004374 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4375 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004376 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004377 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004378 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004379 break;
4380}
4381
4382
drh98757152008-01-09 23:04:12 +00004383/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004384** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004385**
drhf0863fe2005-06-12 21:35:51 +00004386** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004387** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004388** table that cursor P1 points to. The new record number is written
4389** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004390**
dan76d462e2009-08-30 11:42:51 +00004391** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4392** the largest previously generated record number. No new record numbers are
4393** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004394** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004395** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004396** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004397*/
drh27a348c2015-04-13 19:14:06 +00004398case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004399 i64 v; /* The new rowid */
4400 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4401 int res; /* Result of an sqlite3BtreeLast() */
4402 int cnt; /* Counter to limit the number of searches */
4403 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004404 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004405
drh856c1032009-06-02 15:21:42 +00004406 v = 0;
4407 res = 0;
drh27a348c2015-04-13 19:14:06 +00004408 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004409 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4410 pC = p->apCsr[pOp->p1];
4411 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004412 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004413 assert( pC->eCurType==CURTYPE_BTREE );
4414 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004415 {
drh5cf8e8c2002-02-19 22:42:05 +00004416 /* The next rowid or record number (different terms for the same
4417 ** thing) is obtained in a two-step algorithm.
4418 **
4419 ** First we attempt to find the largest existing rowid and add one
4420 ** to that. But if the largest existing rowid is already the maximum
4421 ** positive integer, we have to fall through to the second
4422 ** probabilistic algorithm
4423 **
4424 ** The second algorithm is to select a rowid at random and see if
4425 ** it already exists in the table. If it does not exist, we have
4426 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004427 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004428 */
drhaa736092009-06-22 00:55:30 +00004429 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004430
drh75f86a42005-02-17 00:03:06 +00004431#ifdef SQLITE_32BIT_ROWID
4432# define MAX_ROWID 0x7fffffff
4433#else
drhfe2093d2005-01-20 22:48:47 +00004434 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4435 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4436 ** to provide the constant while making all compilers happy.
4437 */
danielk197764202cf2008-11-17 15:31:47 +00004438# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004439#endif
drhfe2093d2005-01-20 22:48:47 +00004440
drh5cf8e8c2002-02-19 22:42:05 +00004441 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004442 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004443 if( rc!=SQLITE_OK ){
4444 goto abort_due_to_error;
4445 }
4446 if( res ){
4447 v = 1; /* IMP: R-61914-48074 */
4448 }else{
drhc960dcb2015-11-20 19:22:01 +00004449 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004450 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004451 if( v>=MAX_ROWID ){
4452 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004453 }else{
drhe0670b62014-02-12 21:31:12 +00004454 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004455 }
drh3fc190c2001-09-14 03:24:23 +00004456 }
drhe0670b62014-02-12 21:31:12 +00004457 }
drh205f48e2004-11-05 00:43:11 +00004458
4459#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004460 if( pOp->p3 ){
4461 /* Assert that P3 is a valid memory cell. */
4462 assert( pOp->p3>0 );
4463 if( p->pFrame ){
4464 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004465 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004466 assert( pOp->p3<=pFrame->nMem );
4467 pMem = &pFrame->aMem[pOp->p3];
4468 }else{
4469 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004470 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004471 pMem = &aMem[pOp->p3];
4472 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004473 }
drhe0670b62014-02-12 21:31:12 +00004474 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004475
drhe0670b62014-02-12 21:31:12 +00004476 REGISTER_TRACE(pOp->p3, pMem);
4477 sqlite3VdbeMemIntegerify(pMem);
4478 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4479 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004480 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004481 goto abort_due_to_error;
4482 }
4483 if( v<pMem->u.i+1 ){
4484 v = pMem->u.i + 1;
4485 }
4486 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004487 }
drhe0670b62014-02-12 21:31:12 +00004488#endif
drh5cf8e8c2002-02-19 22:42:05 +00004489 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004490 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004491 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004492 ** engine starts picking positive candidate ROWIDs at random until
4493 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004494 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4495 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004496 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004497 do{
4498 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004499 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004500 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004501 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004502 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004503 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004504 if( rc ) goto abort_due_to_error;
4505 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004506 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004507 goto abort_due_to_error;
4508 }
drh748a52c2010-09-01 11:50:08 +00004509 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004510 }
drha11846b2004-01-07 18:52:56 +00004511 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004512 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004513 }
drh4c583122008-01-04 22:01:03 +00004514 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004515 break;
4516}
4517
danielk19771f4aa332008-01-03 09:51:55 +00004518/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004519** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004520**
jplyon5a564222003-06-02 06:15:58 +00004521** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004522** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004523** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004524** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004525** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004526**
danielk19771f4aa332008-01-03 09:51:55 +00004527** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4528** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004529** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004530** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004531**
drheaf6ae22016-11-09 20:14:34 +00004532** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4533** run faster by avoiding an unnecessary seek on cursor P1. However,
4534** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4535** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004536**
4537** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4538** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4539** is part of an INSERT operation. The difference is only important to
4540** the update hook.
4541**
dan319eeb72011-03-19 08:38:50 +00004542** Parameter P4 may point to a Table structure, or may be NULL. If it is
4543** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4544** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004545**
drh93aed5a2008-01-16 17:46:38 +00004546** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4547** allocated, then ownership of P2 is transferred to the pseudo-cursor
4548** and register P2 becomes ephemeral. If the cursor is changed, the
4549** value of register P2 will then change. Make sure this does not
4550** cause any problems.)
4551**
drhf0863fe2005-06-12 21:35:51 +00004552** This instruction only works on tables. The equivalent instruction
4553** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004554*/
drhe05c9292009-10-29 13:48:10 +00004555/* Opcode: InsertInt P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00004556** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004557**
4558** This works exactly like OP_Insert except that the key is the
4559** integer value P3, not the value of the integer stored in register P3.
4560*/
4561case OP_Insert:
4562case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004563 Mem *pData; /* MEM cell holding data for the record to be inserted */
4564 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004565 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004566 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4567 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004568 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00004569 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004570
drha6c2ed92009-11-14 23:22:23 +00004571 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004572 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004573 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004574 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004575 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004576 assert( pC->eCurType==CURTYPE_BTREE );
4577 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004578 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004579 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004580 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00004581 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00004582
drhe05c9292009-10-29 13:48:10 +00004583 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004584 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004585 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004586 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004587 REGISTER_TRACE(pOp->p3, pKey);
drh8eeb4462016-05-21 20:03:42 +00004588 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004589 }else{
4590 assert( pOp->opcode==OP_InsertInt );
drh8eeb4462016-05-21 20:03:42 +00004591 x.nKey = pOp->p3;
drhe05c9292009-10-29 13:48:10 +00004592 }
4593
drh9b1c62d2011-03-30 21:04:43 +00004594 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004595 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004596 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004597 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004598 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00004599 }else{
drh4ec6f3a2018-01-12 19:33:18 +00004600 pTab = 0;
drh74c33022016-03-30 12:56:55 +00004601 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004602 }
4603
drh9b1c62d2011-03-30 21:04:43 +00004604#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004605 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00004606 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00004607 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
4608 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
4609 }
drh4ec6f3a2018-01-12 19:33:18 +00004610 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
4611 /* Prevent post-update hook from running in cases when it should not */
4612 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00004613 }
dan46c47d42011-03-01 18:42:07 +00004614 }
dancb9a3642017-01-30 19:44:53 +00004615 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004616#endif
dan46c47d42011-03-01 18:42:07 +00004617
drha05a7222008-01-19 03:35:58 +00004618 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004619 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004620 assert( pData->flags & (MEM_Blob|MEM_Str) );
4621 x.pData = pData->z;
4622 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004623 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4624 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004625 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004626 }else{
drh8eeb4462016-05-21 20:03:42 +00004627 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004628 }
drh8eeb4462016-05-21 20:03:42 +00004629 x.pKey = 0;
4630 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004631 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004632 );
drha05a7222008-01-19 03:35:58 +00004633 pC->deferredMoveto = 0;
4634 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004635
drha05a7222008-01-19 03:35:58 +00004636 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004637 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00004638 if( pTab ){
4639 assert( db->xUpdateCallback!=0 );
4640 assert( pTab->aCol!=0 );
4641 db->xUpdateCallback(db->pUpdateArg,
4642 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
4643 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004644 }
drh5e00f6c2001-09-13 13:46:56 +00004645 break;
4646}
4647
dan438b8812015-09-15 15:55:15 +00004648/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004649**
drh5edc3122001-09-13 21:53:09 +00004650** Delete the record at which the P1 cursor is currently pointing.
4651**
drhe807bdb2016-01-21 17:06:33 +00004652** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4653** the cursor will be left pointing at either the next or the previous
4654** record in the table. If it is left pointing at the next record, then
4655** the next Next instruction will be a no-op. As a result, in this case
4656** it is ok to delete a record from within a Next loop. If
4657** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4658** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004659**
drhdef19e32016-01-27 16:26:25 +00004660** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4661** delete one of several associated with deleting a table row and all its
4662** associated index entries. Exactly one of those deletes is the "primary"
4663** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4664** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004665**
4666** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4667** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004668**
drh91fd4d42008-01-19 20:11:25 +00004669** P1 must not be pseudo-table. It has to be a real table with
4670** multiple rows.
4671**
drh5e769a52016-09-28 16:05:53 +00004672** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004673** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4674** have been positioned using OP_NotFound prior to invoking this opcode in
4675** this case. Specifically, if one is configured, the pre-update hook is
4676** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4677** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004678**
4679** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4680** of the memory cell that contains the value that the rowid of the row will
4681** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004682*/
drh9cbf3422008-01-17 16:22:13 +00004683case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004684 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004685 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004686 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004687 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004688
dan46c47d42011-03-01 18:42:07 +00004689 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004690 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4691 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004692 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004693 assert( pC->eCurType==CURTYPE_BTREE );
4694 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004695 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00004696 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00004697
drhb53a5a92014-10-12 22:37:22 +00004698#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004699 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4700 /* If p5 is zero, the seek operation that positioned the cursor prior to
4701 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4702 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004703 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh92fe38e2014-10-14 13:41:32 +00004704 assert( pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004705 }
4706#endif
drh91fd4d42008-01-19 20:11:25 +00004707
dan438b8812015-09-15 15:55:15 +00004708 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4709 ** the name of the db to pass as to it. Also set local pTab to a copy
4710 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4711 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4712 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004713 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004714 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004715 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004716 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004717 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004718 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004719 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004720 }
drh74c33022016-03-30 12:56:55 +00004721 }else{
4722 zDb = 0; /* Not needed. Silence a compiler warning. */
4723 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004724 }
dan46c47d42011-03-01 18:42:07 +00004725
drh9b1c62d2011-03-30 21:04:43 +00004726#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004727 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004728 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4729 assert( !(opflags & OPFLAG_ISUPDATE)
4730 || HasRowid(pTab)==0
4731 || (aMem[pOp->p3].flags & MEM_Int)
4732 );
dan46c47d42011-03-01 18:42:07 +00004733 sqlite3VdbePreUpdateHook(p, pC,
4734 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004735 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004736 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004737 );
4738 }
dan46c47d42011-03-01 18:42:07 +00004739 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004740#endif
drhb53a5a92014-10-12 22:37:22 +00004741
drhdef19e32016-01-27 16:26:25 +00004742 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4743 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004744 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004745 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004746
4747#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004748 if( p->pFrame==0 ){
4749 if( pC->isEphemeral==0
4750 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4751 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4752 ){
4753 nExtraDelete++;
4754 }
4755 if( pOp->p2 & OPFLAG_NCHANGE ){
4756 nExtraDelete--;
4757 }
drhb89aeb62016-01-27 15:49:32 +00004758 }
4759#endif
4760
drhc960dcb2015-11-20 19:22:01 +00004761 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004762 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004763 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004764 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004765
drh91fd4d42008-01-19 20:11:25 +00004766 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004767 if( opflags & OPFLAG_NCHANGE ){
4768 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004769 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004770 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004771 pC->movetoTarget);
4772 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004773 }
drh5e00f6c2001-09-13 13:46:56 +00004774 }
dan438b8812015-09-15 15:55:15 +00004775
rdcb0c374f2004-02-20 22:53:38 +00004776 break;
4777}
drhb7f1d9a2009-09-08 02:27:58 +00004778/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004779**
drhb7f1d9a2009-09-08 02:27:58 +00004780** The value of the change counter is copied to the database handle
4781** change counter (returned by subsequent calls to sqlite3_changes()).
4782** Then the VMs internal change counter resets to 0.
4783** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004784*/
drh9cbf3422008-01-17 16:22:13 +00004785case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004786 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004787 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004788 break;
4789}
4790
drh1153c7b2013-11-01 22:02:56 +00004791/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00004792** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004793**
drh1153c7b2013-11-01 22:02:56 +00004794** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004795** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004796** the sorter cursor currently points to. Only the first P4 fields
4797** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004798**
4799** If either P3 or the sorter contains a NULL in one of their significant
4800** fields (not counting the P4 fields at the end which are ignored) then
4801** the comparison is assumed to be equal.
4802**
4803** Fall through to next instruction if the two records compare equal to
4804** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004805*/
4806case OP_SorterCompare: {
4807 VdbeCursor *pC;
4808 int res;
drhac502322014-07-30 13:56:48 +00004809 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004810
4811 pC = p->apCsr[pOp->p1];
4812 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004813 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004814 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004815 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004816 res = 0;
drhac502322014-07-30 13:56:48 +00004817 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004818 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004819 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004820 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004821 break;
4822};
4823
drh6cf4a7d2014-10-13 13:00:58 +00004824/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004825** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004826**
4827** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004828** Then clear the column header cache on cursor P3.
4829**
4830** This opcode is normally use to move a record out of the sorter and into
4831** a register that is the source for a pseudo-table cursor created using
4832** OpenPseudo. That pseudo-table cursor is the one that is identified by
4833** parameter P3. Clearing the P3 column cache as part of this opcode saves
4834** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004835*/
4836case OP_SorterData: {
4837 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004838
dan5134d132011-09-02 10:31:11 +00004839 pOut = &aMem[pOp->p2];
4840 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004841 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004842 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004843 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004844 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004845 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004846 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004847 break;
4848}
4849
drhe7b554d2017-01-09 15:44:25 +00004850/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004851** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004852**
drh9057fc72016-11-25 19:32:32 +00004853** Write into register P2 the complete row content for the row at
4854** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00004855** There is no interpretation of the data.
4856** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004857** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004858**
drh9057fc72016-11-25 19:32:32 +00004859** If cursor P1 is an index, then the content is the key of the row.
4860** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00004861**
drhde4fcfd2008-01-19 23:50:26 +00004862** If the P1 cursor must be pointing to a valid row (not a NULL row)
4863** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00004864**
drh8cdafc32018-05-31 19:00:20 +00004865** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00004866** into the database page. That means that the content of the output
4867** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00004868** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00004869** position in order that they can write to the same table. If P3==0
4870** then a copy of the data is made into memory. P3!=0 is faster, but
4871** P3==0 is safer.
4872**
4873** If P3!=0 then the content of the P2 register is unsuitable for use
4874** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00004875** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00004876** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00004877*/
danielk1977a7a8e142008-02-13 18:25:27 +00004878case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004879 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004880 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004881 u32 n;
drh70ce3f02003-04-15 19:22:22 +00004882
drhe7b554d2017-01-09 15:44:25 +00004883 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00004884
drh653b82a2009-06-22 11:10:47 +00004885 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4886 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004887 assert( pC!=0 );
4888 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004889 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00004890 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004891 assert( pC->uc.pCursor!=0 );
4892 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004893
drh9057fc72016-11-25 19:32:32 +00004894 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00004895 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4896 ** that might invalidate the cursor.
4897 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00004898 ** would fail. Should this ever change (because of changes in the code
4899 ** generator) then the fix would be to insert a call to
4900 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004901 */
4902 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004903 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4904#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004905 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004906 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4907#endif
drh9a65f2c2009-06-22 19:05:40 +00004908
drha7c90c42016-06-04 20:37:10 +00004909 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00004910 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00004911 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00004912 }
drh722246e2014-10-07 23:02:24 +00004913 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00004914 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00004915 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00004916 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00004917 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004918 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004919 break;
4920}
4921
drh2133d822008-01-03 18:44:59 +00004922/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004923** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004924**
drh2133d822008-01-03 18:44:59 +00004925** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004926** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004927**
4928** P1 can be either an ordinary table or a virtual table. There used to
4929** be a separate OP_VRowid opcode for use with virtual tables, but this
4930** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004931*/
drh27a348c2015-04-13 19:14:06 +00004932case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004933 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004934 i64 v;
drh856c1032009-06-02 15:21:42 +00004935 sqlite3_vtab *pVtab;
4936 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004937
drh27a348c2015-04-13 19:14:06 +00004938 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004939 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4940 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004941 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004942 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004943 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004944 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004945 break;
4946 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004947 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004948#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004949 }else if( pC->eCurType==CURTYPE_VTAB ){
4950 assert( pC->uc.pVCur!=0 );
4951 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004952 pModule = pVtab->pModule;
4953 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004954 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004955 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004956 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004957#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004958 }else{
drhc960dcb2015-11-20 19:22:01 +00004959 assert( pC->eCurType==CURTYPE_BTREE );
4960 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004961 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004962 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004963 if( pC->nullRow ){
4964 pOut->flags = MEM_Null;
4965 break;
4966 }
drha7c90c42016-06-04 20:37:10 +00004967 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00004968 }
drh4c583122008-01-04 22:01:03 +00004969 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004970 break;
4971}
4972
drh9cbf3422008-01-17 16:22:13 +00004973/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004974**
4975** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004976** that occur while the cursor is on the null row will always
4977** write a NULL.
drh17f71932002-02-21 12:01:27 +00004978*/
drh9cbf3422008-01-17 16:22:13 +00004979case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004980 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004981
drh653b82a2009-06-22 11:10:47 +00004982 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4983 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004984 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004985 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004986 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00004987 if( pC->eCurType==CURTYPE_BTREE ){
4988 assert( pC->uc.pCursor!=0 );
4989 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00004990 }
drhcf025a82018-06-07 18:01:21 +00004991#ifdef SQLITE_DEBUG
4992 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
4993#endif
drh17f71932002-02-21 12:01:27 +00004994 break;
4995}
4996
drh86b40df2017-08-01 19:53:43 +00004997/* Opcode: SeekEnd P1 * * * *
4998**
4999** Position cursor P1 at the end of the btree for the purpose of
5000** appending a new entry onto the btree.
5001**
5002** It is assumed that the cursor is used only for appending and so
5003** if the cursor is valid, then the cursor must already be pointing
5004** at the end of the btree and so no changes are made to
5005** the cursor.
5006*/
5007/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005008**
drh8af3f772014-07-25 18:01:06 +00005009** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005010** will refer to the last entry in the database table or index.
5011** If the table or index is empty and P2>0, then jump immediately to P2.
5012** If P2 is 0 or if the table or index is not empty, fall through
5013** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005014**
5015** This opcode leaves the cursor configured to move in reverse order,
5016** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005017** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005018*/
drh86b40df2017-08-01 19:53:43 +00005019case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005020case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005021 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005022 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005023 int res;
drh9562b552002-02-19 15:00:07 +00005024
drh653b82a2009-06-22 11:10:47 +00005025 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5026 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005027 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005028 assert( pC->eCurType==CURTYPE_BTREE );
5029 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005030 res = 0;
drh3da046d2013-11-11 03:24:11 +00005031 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005032#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005033 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005034#endif
drh86b40df2017-08-01 19:53:43 +00005035 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005036 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005037 pC->seekResult = -1;
5038 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5039 break;
5040 }
5041 }
5042 rc = sqlite3BtreeLast(pCrsr, &res);
5043 pC->nullRow = (u8)res;
5044 pC->deferredMoveto = 0;
5045 pC->cacheStatus = CACHE_STALE;
5046 if( rc ) goto abort_due_to_error;
5047 if( pOp->p2>0 ){
5048 VdbeBranchTaken(res!=0,2);
5049 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005050 }
5051 break;
5052}
5053
drh5e98e832017-02-17 19:24:06 +00005054/* Opcode: IfSmaller P1 P2 P3 * *
5055**
5056** Estimate the number of rows in the table P1. Jump to P2 if that
5057** estimate is less than approximately 2**(0.1*P3).
5058*/
5059case OP_IfSmaller: { /* jump */
5060 VdbeCursor *pC;
5061 BtCursor *pCrsr;
5062 int res;
5063 i64 sz;
5064
5065 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5066 pC = p->apCsr[pOp->p1];
5067 assert( pC!=0 );
5068 pCrsr = pC->uc.pCursor;
5069 assert( pCrsr );
5070 rc = sqlite3BtreeFirst(pCrsr, &res);
5071 if( rc ) goto abort_due_to_error;
5072 if( res==0 ){
5073 sz = sqlite3BtreeRowCountEst(pCrsr);
5074 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5075 }
5076 VdbeBranchTaken(res!=0,2);
5077 if( res ) goto jump_to_p2;
5078 break;
5079}
5080
drh0342b1f2005-09-01 03:07:44 +00005081
drh6bd4dc62016-12-23 16:05:22 +00005082/* Opcode: SorterSort P1 P2 * * *
5083**
5084** After all records have been inserted into the Sorter object
5085** identified by P1, invoke this opcode to actually do the sorting.
5086** Jump to P2 if there are no records to be sorted.
5087**
5088** This opcode is an alias for OP_Sort and OP_Rewind that is used
5089** for Sorter objects.
5090*/
drh9cbf3422008-01-17 16:22:13 +00005091/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005092**
5093** This opcode does exactly the same thing as OP_Rewind except that
5094** it increments an undocumented global variable used for testing.
5095**
5096** Sorting is accomplished by writing records into a sorting index,
5097** then rewinding that index and playing it back from beginning to
5098** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5099** rewinding so that the global variable will be incremented and
5100** regression tests can determine whether or not the optimizer is
5101** correctly optimizing out sorts.
5102*/
drhc6aff302011-09-01 15:32:47 +00005103case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005104case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005105#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005106 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005107 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005108#endif
drh9b47ee32013-08-20 03:13:51 +00005109 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005110 /* Fall through into OP_Rewind */
5111}
dan9a947222018-06-14 19:06:36 +00005112/* Opcode: Rewind P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00005113**
drhf0863fe2005-06-12 21:35:51 +00005114** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005115** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005116** If the table or index is empty, jump immediately to P2.
5117** If the table or index is not empty, fall through to the following
5118** instruction.
drh8af3f772014-07-25 18:01:06 +00005119**
dan9a947222018-06-14 19:06:36 +00005120** If P5 is non-zero and the table is not empty, then the "skip-next"
5121** flag is set on the cursor so that the next OP_Next instruction
5122** executed on it is a no-op.
5123**
drh8af3f772014-07-25 18:01:06 +00005124** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005125** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005126** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005127*/
drh9cbf3422008-01-17 16:22:13 +00005128case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005129 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005130 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005131 int res;
drh5e00f6c2001-09-13 13:46:56 +00005132
drh653b82a2009-06-22 11:10:47 +00005133 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5134 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005135 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005136 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005137 res = 1;
drh8af3f772014-07-25 18:01:06 +00005138#ifdef SQLITE_DEBUG
5139 pC->seekOp = OP_Rewind;
5140#endif
dan689ab892011-08-12 15:02:00 +00005141 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005142 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005143 }else{
drhc960dcb2015-11-20 19:22:01 +00005144 assert( pC->eCurType==CURTYPE_BTREE );
5145 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005146 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005147 rc = sqlite3BtreeFirst(pCrsr, &res);
dan67a9b8e2018-06-22 20:51:35 +00005148#ifndef SQLITE_OMIT_WINDOWFUNC
danc3a20c12018-05-23 20:55:37 +00005149 if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
dan67a9b8e2018-06-22 20:51:35 +00005150#endif
drha11846b2004-01-07 18:52:56 +00005151 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005152 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005153 }
drh9467abf2016-02-17 18:44:11 +00005154 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005155 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005156 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005157 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005158 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005159 break;
5160}
5161
drh0fd61352014-02-07 02:29:45 +00005162/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005163**
5164** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005165** table or index. If there are no more key/value pairs then fall through
5166** to the following instruction. But if the cursor advance was successful,
5167** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005168**
drh5dad9a32014-07-25 18:37:42 +00005169** The Next opcode is only valid following an SeekGT, SeekGE, or
5170** OP_Rewind opcode used to position the cursor. Next is not allowed
5171** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005172**
drhf93cd942013-11-21 03:12:25 +00005173** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5174** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005175**
drhe39a7322014-02-03 14:04:11 +00005176** The P3 value is a hint to the btree implementation. If P3==1, that
5177** means P1 is an SQL index and that this instruction could have been
5178** omitted if that index had been unique. P3 is usually 0. P3 is
5179** always either 0 or 1.
5180**
dana205a482011-08-27 18:48:57 +00005181** P4 is always of type P4_ADVANCE. The function pointer points to
5182** sqlite3BtreeNext().
5183**
drhafc266a2010-03-31 17:47:44 +00005184** If P5 is positive and the jump is taken, then event counter
5185** number P5-1 in the prepared statement is incremented.
5186**
drhf1949b62018-06-07 17:32:59 +00005187** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005188*/
drh0fd61352014-02-07 02:29:45 +00005189/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005190**
5191** Back up cursor P1 so that it points to the previous key/data pair in its
5192** table or index. If there is no previous key/value pairs then fall through
5193** to the following instruction. But if the cursor backup was successful,
5194** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005195**
drh8af3f772014-07-25 18:01:06 +00005196**
drh5dad9a32014-07-25 18:37:42 +00005197** The Prev opcode is only valid following an SeekLT, SeekLE, or
5198** OP_Last opcode used to position the cursor. Prev is not allowed
5199** to follow SeekGT, SeekGE, or OP_Rewind.
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. If P1 is
5202** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +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** sqlite3BtreePrevious().
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.
drhc045ec52002-12-04 20:01:06 +00005214*/
drh6bd4dc62016-12-23 16:05:22 +00005215/* Opcode: SorterNext P1 P2 * * P5
5216**
5217** This opcode works just like OP_Next except that P1 must be a
5218** sorter object for which the OP_SorterSort opcode has been
5219** invoked. This opcode advances the cursor to the next sorted
5220** record, or jumps to P2 if there are no more sorted records.
5221*/
drhf93cd942013-11-21 03:12:25 +00005222case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005223 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005224
drhf93cd942013-11-21 03:12:25 +00005225 pC = p->apCsr[pOp->p1];
5226 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005227 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005228 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005229case OP_Prev: /* jump */
5230case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005231 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005232 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005233 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005234 assert( pC!=0 );
5235 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005236 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005237 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5238 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005239
drhcf025a82018-06-07 18:01:21 +00005240 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005241 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005242 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005243 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcf025a82018-06-07 18:01:21 +00005244 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
5245 || pC->seekOp==OP_NullRow);
drhf1949b62018-06-07 17:32:59 +00005246 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005247 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drhcf025a82018-06-07 18:01:21 +00005248 || pC->seekOp==OP_Last
5249 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005250
drh2ab792e2017-05-30 18:34:07 +00005251 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005252next_tail:
drha3460582008-07-11 21:02:53 +00005253 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005254 VdbeBranchTaken(rc==SQLITE_OK,2);
5255 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005256 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005257 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005258#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005259 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005260#endif
drhf56fa462015-04-13 21:39:54 +00005261 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005262 }
drh2ab792e2017-05-30 18:34:07 +00005263 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5264 rc = SQLITE_OK;
5265 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005266 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005267}
5268
drh9b4eaeb2016-11-09 00:10:33 +00005269/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005270** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005271**
drhef8662b2011-06-20 21:47:58 +00005272** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005273** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005274** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005275**
drhfb8c56f2016-11-09 01:19:25 +00005276** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005277** key of reg(P2). In that case, P3 is the index of the first register
5278** for the unpacked key. The availability of the unpacked key can sometimes
5279** be an optimization.
5280**
5281** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5282** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005283**
mistachkin21a919f2014-02-07 03:28:02 +00005284** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5285** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5286** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005287**
drheaf6ae22016-11-09 20:14:34 +00005288** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5289** run faster by avoiding an unnecessary seek on cursor P1. However,
5290** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5291** seeks on the cursor or if the most recent seek used a key equivalent
5292** to P2.
drh0fd61352014-02-07 02:29:45 +00005293**
drhf0863fe2005-06-12 21:35:51 +00005294** This instruction only works for indices. The equivalent instruction
5295** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005296*/
drhf013e202016-10-15 18:37:05 +00005297/* Opcode: SorterInsert P1 P2 * * *
5298** Synopsis: key=r[P2]
5299**
5300** Register P2 holds an SQL index key made using the
5301** MakeRecord instructions. This opcode writes that key
5302** into the sorter P1. Data for the entry is nil.
5303*/
drhca892a72011-09-03 00:17:51 +00005304case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005305case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005306 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005307 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005308
drh653b82a2009-06-22 11:10:47 +00005309 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5310 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005311 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005312 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005313 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005314 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005315 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005316 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005317 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005318 assert( pC->isTable==0 );
5319 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005320 if( rc ) goto abort_due_to_error;
5321 if( pOp->opcode==OP_SorterInsert ){
5322 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5323 }else{
drh8eeb4462016-05-21 20:03:42 +00005324 x.nKey = pIn2->n;
5325 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005326 x.aMem = aMem + pOp->p3;
5327 x.nMem = (u16)pOp->p4.i;
5328 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005329 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005330 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5331 );
5332 assert( pC->deferredMoveto==0 );
5333 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005334 }
drh9467abf2016-02-17 18:44:11 +00005335 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005336 break;
5337}
5338
drhd1d38482008-10-07 23:46:38 +00005339/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005340** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005341**
drhe14006d2008-03-25 17:23:32 +00005342** The content of P3 registers starting at register P2 form
5343** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005344** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005345*/
drhe14006d2008-03-25 17:23:32 +00005346case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005347 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005348 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005349 int res;
5350 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005351
drhe14006d2008-03-25 17:23:32 +00005352 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005353 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005354 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5355 pC = p->apCsr[pOp->p1];
5356 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005357 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005358 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005359 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005360 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005361 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005362 r.pKeyInfo = pC->pKeyInfo;
5363 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005364 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005365 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005366 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005367 if( rc ) goto abort_due_to_error;
5368 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005369 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005370 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005371 }
drh3da046d2013-11-11 03:24:11 +00005372 assert( pC->deferredMoveto==0 );
5373 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005374 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005375 break;
5376}
5377
drh170ad682017-06-02 15:44:22 +00005378/* Opcode: DeferredSeek P1 * P3 P4 *
5379** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005380**
5381** P1 is an open index cursor and P3 is a cursor on the corresponding
5382** table. This opcode does a deferred seek of the P3 table cursor
5383** to the row that corresponds to the current row of P1.
5384**
5385** This is a deferred seek. Nothing actually happens until
5386** the cursor is used to read a record. That way, if no reads
5387** occur, no unnecessary I/O happens.
5388**
5389** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005390** one entry for each column in the P3 table. If array entry a(i)
5391** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005392** equivalent to performing the deferred seek and then reading column i
5393** from P1. This information is stored in P3 and used to redirect
5394** reads against P3 over to P1, thus possibly avoiding the need to
5395** seek and read cursor P3.
5396*/
drh2133d822008-01-03 18:44:59 +00005397/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005398** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005399**
drh2133d822008-01-03 18:44:59 +00005400** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005401** the end of the index key pointed to by cursor P1. This integer should be
5402** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005403**
drh9437bd22009-02-01 00:29:56 +00005404** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005405*/
drh170ad682017-06-02 15:44:22 +00005406case OP_DeferredSeek:
5407case OP_IdxRowid: { /* out2 */
5408 VdbeCursor *pC; /* The P1 index cursor */
5409 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5410 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005411
drh653b82a2009-06-22 11:10:47 +00005412 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5413 pC = p->apCsr[pOp->p1];
5414 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005415 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005416 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005417 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005418 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005419 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5420
5421 /* The IdxRowid and Seek opcodes are combined because of the commonality
5422 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5423 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005424
5425 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005426 ** out from under the cursor. That will never happens for an IdxRowid
5427 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005428 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5429
drh3da046d2013-11-11 03:24:11 +00005430 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005431 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005432 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005433 if( rc!=SQLITE_OK ){
5434 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005435 }
drh170ad682017-06-02 15:44:22 +00005436 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005437 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5438 pTabCur = p->apCsr[pOp->p3];
5439 assert( pTabCur!=0 );
5440 assert( pTabCur->eCurType==CURTYPE_BTREE );
5441 assert( pTabCur->uc.pCursor!=0 );
5442 assert( pTabCur->isTable );
5443 pTabCur->nullRow = 0;
5444 pTabCur->movetoTarget = rowid;
5445 pTabCur->deferredMoveto = 1;
5446 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5447 pTabCur->aAltMap = pOp->p4.ai;
5448 pTabCur->pAltCursor = pC;
5449 }else{
5450 pOut = out2Prerelease(p, pOp);
5451 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005452 }
5453 }else{
5454 assert( pOp->opcode==OP_IdxRowid );
5455 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005456 }
5457 break;
5458}
5459
danielk197761dd5832008-04-18 11:31:12 +00005460/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005461** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005462**
danielk197761dd5832008-04-18 11:31:12 +00005463** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005464** key that omits the PRIMARY KEY. Compare this key value against the index
5465** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5466** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005467**
danielk197761dd5832008-04-18 11:31:12 +00005468** If the P1 index entry is greater than or equal to the key value
5469** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005470*/
5471/* Opcode: IdxGT P1 P2 P3 P4 P5
5472** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005473**
drh4a1d3652014-02-14 15:13:36 +00005474** The P4 register values beginning with P3 form an unpacked index
5475** key that omits the PRIMARY KEY. Compare this key value against the index
5476** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5477** fields at the end.
5478**
5479** If the P1 index entry is greater than the key value
5480** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005481*/
drh3bb9b932010-08-06 02:10:00 +00005482/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005483** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005484**
danielk197761dd5832008-04-18 11:31:12 +00005485** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005486** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5487** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5488** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005489**
danielk197761dd5832008-04-18 11:31:12 +00005490** If the P1 index entry is less than the key value then jump to P2.
5491** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005492*/
drh4a1d3652014-02-14 15:13:36 +00005493/* Opcode: IdxLE P1 P2 P3 P4 P5
5494** Synopsis: key=r[P3@P4]
5495**
5496** The P4 register values beginning with P3 form an unpacked index
5497** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5498** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5499** ROWID on the P1 index.
5500**
5501** If the P1 index entry is less than or equal to the key value then jump
5502** to P2. Otherwise fall through to the next instruction.
5503*/
5504case OP_IdxLE: /* jump */
5505case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005506case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005507case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005508 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005509 int res;
5510 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005511
drh653b82a2009-06-22 11:10:47 +00005512 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5513 pC = p->apCsr[pOp->p1];
5514 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005515 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005516 assert( pC->eCurType==CURTYPE_BTREE );
5517 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005518 assert( pC->deferredMoveto==0 );
5519 assert( pOp->p5==0 || pOp->p5==1 );
5520 assert( pOp->p4type==P4_INT32 );
5521 r.pKeyInfo = pC->pKeyInfo;
5522 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005523 if( pOp->opcode<OP_IdxLT ){
5524 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005525 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005526 }else{
drh4a1d3652014-02-14 15:13:36 +00005527 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005528 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005529 }
5530 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005531#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00005532 {
5533 int i;
5534 for(i=0; i<r.nField; i++){
5535 assert( memIsValid(&r.aMem[i]) );
5536 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
5537 }
5538 }
drh2b4ded92010-09-27 21:09:31 +00005539#endif
drh2dc06482013-12-11 00:59:10 +00005540 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005541 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005542 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5543 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5544 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005545 res = -res;
5546 }else{
drh4a1d3652014-02-14 15:13:36 +00005547 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005548 res++;
5549 }
drh688852a2014-02-17 22:40:43 +00005550 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005551 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005552 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005553 break;
5554}
5555
drh98757152008-01-09 23:04:12 +00005556/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005557**
5558** Delete an entire database table or index whose root page in the database
5559** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005560**
drh98757152008-01-09 23:04:12 +00005561** The table being destroyed is in the main database file if P3==0. If
5562** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005563** that is used to store tables create using CREATE TEMPORARY TABLE.
5564**
drh205f48e2004-11-05 00:43:11 +00005565** If AUTOVACUUM is enabled then it is possible that another root page
5566** might be moved into the newly deleted root page in order to keep all
5567** root pages contiguous at the beginning of the database. The former
5568** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005569** is stored in register P2. If no page movement was required (because the
5570** table being dropped was already the last one in the database) then a
5571** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5572** is stored in register P2.
5573**
5574** This opcode throws an error if there are any active reader VMs when
5575** it is invoked. This is done to avoid the difficulty associated with
5576** updating existing cursors when a root page is moved in an AUTOVACUUM
5577** database. This error is thrown even if the database is not an AUTOVACUUM
5578** db in order to avoid introducing an incompatibility between autovacuum
5579** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005580**
drhb19a2bc2001-09-16 00:13:26 +00005581** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005582*/
drh27a348c2015-04-13 19:14:06 +00005583case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005584 int iMoved;
drh856c1032009-06-02 15:21:42 +00005585 int iDb;
drh3a949872012-09-18 13:20:13 +00005586
drh4031baf2018-05-28 17:31:20 +00005587 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005588 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005589 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005590 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005591 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005592 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005593 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005594 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005595 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005596 }else{
drh856c1032009-06-02 15:21:42 +00005597 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005598 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005599 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005600 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005601 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005602 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005603 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005604#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005605 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005606 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5607 /* All OP_Destroy operations occur on the same btree */
5608 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5609 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005610 }
drh3765df42006-06-28 18:18:09 +00005611#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005612 }
drh5e00f6c2001-09-13 13:46:56 +00005613 break;
5614}
5615
danielk1977c7af4842008-10-27 13:59:33 +00005616/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005617**
5618** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005619** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005620** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005621**
drhf57b3392001-10-08 13:22:32 +00005622** The table being clear is in the main database file if P2==0. If
5623** P2==1 then the table to be clear is in the auxiliary database file
5624** that is used to store tables create using CREATE TEMPORARY TABLE.
5625**
shanebe217792009-03-05 04:20:31 +00005626** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005627** intkey table (an SQL table, not an index). In this case the row change
5628** count is incremented by the number of rows in the table being cleared.
5629** If P3 is greater than zero, then the value stored in register P3 is
5630** also incremented by the number of rows in the table being cleared.
5631**
drhb19a2bc2001-09-16 00:13:26 +00005632** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005633*/
drh9cbf3422008-01-17 16:22:13 +00005634case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005635 int nChange;
5636
drh4031baf2018-05-28 17:31:20 +00005637 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005638 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005639 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005640 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005641 rc = sqlite3BtreeClearTable(
5642 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5643 );
5644 if( pOp->p3 ){
5645 p->nChange += nChange;
5646 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005647 assert( memIsValid(&aMem[pOp->p3]) );
5648 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005649 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005650 }
5651 }
drh9467abf2016-02-17 18:44:11 +00005652 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005653 break;
5654}
5655
drh65ea12c2014-03-19 17:41:36 +00005656/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005657**
drh65ea12c2014-03-19 17:41:36 +00005658** Delete all contents from the ephemeral table or sorter
5659** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005660**
drh65ea12c2014-03-19 17:41:36 +00005661** This opcode only works for cursors used for sorting and
5662** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005663*/
drh65ea12c2014-03-19 17:41:36 +00005664case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005665 VdbeCursor *pC;
5666
5667 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5668 pC = p->apCsr[pOp->p1];
5669 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005670 if( isSorter(pC) ){
5671 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005672 }else{
drhc960dcb2015-11-20 19:22:01 +00005673 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005674 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005675 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005676 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005677 }
drh079a3072014-03-19 14:10:55 +00005678 break;
5679}
5680
drh0f3f7662017-08-18 14:34:28 +00005681/* Opcode: CreateBtree P1 P2 P3 * *
5682** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005683**
drh0f3f7662017-08-18 14:34:28 +00005684** Allocate a new b-tree in the main database file if P1==0 or in the
5685** TEMP database file if P1==1 or in an attached database if
5686** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00005687** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00005688** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005689*/
drh0f3f7662017-08-18 14:34:28 +00005690case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005691 int pgno;
drh234c39d2004-07-24 03:30:47 +00005692 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005693
drh4031baf2018-05-28 17:31:20 +00005694 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00005695 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005696 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005697 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005698 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005699 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005700 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005701 pDb = &db->aDb[pOp->p1];
5702 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005703 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005704 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005705 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005706 break;
5707}
5708
drh4a54bb52017-02-18 15:58:52 +00005709/* Opcode: SqlExec * * * P4 *
5710**
5711** Run the SQL statement or statements specified in the P4 string.
5712*/
5713case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00005714 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00005715 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005716 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005717 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005718 if( rc ) goto abort_due_to_error;
5719 break;
5720}
5721
drh22645842011-03-24 01:34:03 +00005722/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005723**
5724** Read and parse all entries from the SQLITE_MASTER table of database P1
dan987db762018-08-14 20:18:50 +00005725** that match the WHERE clause P4. If P4 is a NULL pointer, then the
5726** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00005727**
5728** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005729** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005730*/
drh9cbf3422008-01-17 16:22:13 +00005731case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005732 int iDb;
5733 const char *zMaster;
5734 char *zSql;
5735 InitData initData;
5736
drhbdaec522011-04-04 00:14:43 +00005737 /* Any prepared statement that invokes this opcode will hold mutexes
5738 ** on every btree. This is a prerequisite for invoking
5739 ** sqlite3InitCallback().
5740 */
5741#ifdef SQLITE_DEBUG
5742 for(iDb=0; iDb<db->nDb; iDb++){
5743 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5744 }
5745#endif
drhbdaec522011-04-04 00:14:43 +00005746
drh856c1032009-06-02 15:21:42 +00005747 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005748 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005749 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
dane325ffe2018-08-11 13:40:20 +00005750
5751#ifndef SQLITE_OMIT_ALTERTABLE
5752 if( pOp->p4.z==0 ){
5753 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00005754 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
dan987db762018-08-14 20:18:50 +00005755 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
dane325ffe2018-08-11 13:40:20 +00005756 db->mDbFlags |= DBFLAG_SchemaChange;
5757 }else
5758#endif
dan987db762018-08-14 20:18:50 +00005759 {
drhe0a04a32016-12-16 01:00:21 +00005760 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005761 initData.db = db;
5762 initData.iDb = pOp->p1;
5763 initData.pzErrMsg = &p->zErrMsg;
5764 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005765 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005766 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005767 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005768 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005769 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005770 assert( db->init.busy==0 );
5771 db->init.busy = 1;
5772 initData.rc = SQLITE_OK;
5773 assert( !db->mallocFailed );
5774 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5775 if( rc==SQLITE_OK ) rc = initData.rc;
drhdbd6a7d2017-04-05 12:39:49 +00005776 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005777 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005778 }
drh3c23a882007-01-09 14:01:13 +00005779 }
drh9467abf2016-02-17 18:44:11 +00005780 if( rc ){
5781 sqlite3ResetAllSchemasOfConnection(db);
5782 if( rc==SQLITE_NOMEM ){
5783 goto no_mem;
5784 }
5785 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005786 }
drh234c39d2004-07-24 03:30:47 +00005787 break;
5788}
5789
drh8bfdf722009-06-19 14:06:03 +00005790#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005791/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005792**
5793** Read the sqlite_stat1 table for database P1 and load the content
5794** of that table into the internal index hash table. This will cause
5795** the analysis to be used when preparing all subsequent queries.
5796*/
drh9cbf3422008-01-17 16:22:13 +00005797case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005798 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5799 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005800 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005801 break;
5802}
drh8bfdf722009-06-19 14:06:03 +00005803#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005804
drh98757152008-01-09 23:04:12 +00005805/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005806**
5807** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005808** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005809** is dropped from disk (using the Destroy opcode) in order to keep
5810** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005811** schema consistent with what is on disk.
5812*/
drh9cbf3422008-01-17 16:22:13 +00005813case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00005814 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005815 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005816 break;
5817}
5818
drh98757152008-01-09 23:04:12 +00005819/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005820**
5821** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005822** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005823** is dropped from disk (using the Destroy opcode)
5824** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005825** schema consistent with what is on disk.
5826*/
drh9cbf3422008-01-17 16:22:13 +00005827case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00005828 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005829 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005830 break;
5831}
5832
drh98757152008-01-09 23:04:12 +00005833/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005834**
5835** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005836** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005837** is dropped from disk (using the Destroy opcode) in order to keep
5838** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005839** schema consistent with what is on disk.
5840*/
drh9cbf3422008-01-17 16:22:13 +00005841case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00005842 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005843 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005844 break;
5845}
5846
drh234c39d2004-07-24 03:30:47 +00005847
drhb7f91642004-10-31 02:22:47 +00005848#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005849/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005850**
drh98757152008-01-09 23:04:12 +00005851** Do an analysis of the currently open database. Store in
5852** register P1 the text of an error message describing any problems.
5853** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005854**
drh66accfc2017-02-22 18:04:42 +00005855** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005856** At most reg(P3) errors will be reported.
5857** In other words, the analysis stops as soon as reg(P1) errors are
5858** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005859**
drh98968b22016-03-15 22:00:39 +00005860** The root page numbers of all tables in the database are integers
5861** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005862**
drh98757152008-01-09 23:04:12 +00005863** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005864** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005865**
drh1dcdbc02007-01-27 02:24:54 +00005866** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005867*/
drhaaab5722002-02-19 13:39:21 +00005868case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005869 int nRoot; /* Number of tables to check. (Number of root pages.) */
5870 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005871 int nErr; /* Number of errors reported */
5872 char *z; /* Text of the error report */
5873 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005874
drh1713afb2013-06-28 01:24:57 +00005875 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005876 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005877 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005878 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00005879 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00005880 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005881 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005882 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005883 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005884 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005885 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005886 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drhb5c10632017-09-21 00:49:15 +00005887 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00005888 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005889 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005890 if( nErr==0 ){
5891 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005892 }else if( z==0 ){
5893 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005894 }else{
drh66accfc2017-02-22 18:04:42 +00005895 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005896 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005897 }
drhb7654112008-01-12 12:48:07 +00005898 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005899 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005900 break;
5901}
drhb7f91642004-10-31 02:22:47 +00005902#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005903
drh3d4501e2008-12-04 20:40:10 +00005904/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005905** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005906**
drhbb6783b2017-04-29 18:02:49 +00005907** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00005908** held in register P1.
5909**
5910** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005911*/
drh93952eb2009-11-13 19:43:43 +00005912case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005913 pIn1 = &aMem[pOp->p1];
5914 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005915 assert( (pIn2->flags & MEM_Int)!=0 );
5916 if( (pIn1->flags & MEM_RowSet)==0 ){
5917 sqlite3VdbeMemSetRowSet(pIn1);
5918 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005919 }
drh93952eb2009-11-13 19:43:43 +00005920 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005921 break;
5922}
5923
5924/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005925** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005926**
drhbb6783b2017-04-29 18:02:49 +00005927** Extract the smallest value from the RowSet object in P1
5928** and put that value into register P3.
5929** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00005930** unchanged and jump to instruction P2.
5931*/
drh93952eb2009-11-13 19:43:43 +00005932case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005933 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005934
drh3c657212009-11-17 23:59:58 +00005935 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005936 if( (pIn1->flags & MEM_RowSet)==0
5937 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005938 ){
5939 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005940 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005941 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005942 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005943 }else{
5944 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005945 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005946 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005947 }
drh49afe3a2013-07-10 03:05:14 +00005948 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005949}
5950
drh1b26c7c2009-04-22 02:15:47 +00005951/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005952** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005953**
drhade97602009-04-21 15:05:18 +00005954** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005955** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005956** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005957** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005958** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005959**
drhbb6783b2017-04-29 18:02:49 +00005960** The RowSet object is optimized for the case where sets of integers
5961** are inserted in distinct phases, which each set contains no duplicates.
5962** Each set is identified by a unique P4 value. The first set
5963** must have P4==0, the final set must have P4==-1, and for all other sets
5964** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00005965**
5966** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00005967** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005968** (b) when P4==-1 there is no need to insert the value, as it will
5969** never be tested for, and (c) when a value that is part of set X is
5970** inserted, there is no need to search to see if the same value was
5971** previously inserted as part of set X (only if it was previously
5972** inserted as part of some other set).
5973*/
drh1b26c7c2009-04-22 02:15:47 +00005974case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005975 int iSet;
5976 int exists;
5977
drh3c657212009-11-17 23:59:58 +00005978 pIn1 = &aMem[pOp->p1];
5979 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005980 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005981 assert( pIn3->flags&MEM_Int );
5982
drh1b26c7c2009-04-22 02:15:47 +00005983 /* If there is anything other than a rowset object in memory cell P1,
5984 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005985 */
drh733bf1b2009-04-22 00:47:00 +00005986 if( (pIn1->flags & MEM_RowSet)==0 ){
5987 sqlite3VdbeMemSetRowSet(pIn1);
5988 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005989 }
5990
5991 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005992 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005993 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005994 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005995 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005996 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005997 }
5998 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005999 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006000 }
6001 break;
6002}
6003
drh5e00f6c2001-09-13 13:46:56 +00006004
danielk197793758c82005-01-21 08:13:14 +00006005#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006006
drh0fd61352014-02-07 02:29:45 +00006007/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006008**
dan76d462e2009-08-30 11:42:51 +00006009** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006010**
dan76d462e2009-08-30 11:42:51 +00006011** P1 contains the address of the memory cell that contains the first memory
6012** cell in an array of values used as arguments to the sub-program. P2
6013** contains the address to jump to if the sub-program throws an IGNORE
6014** exception using the RAISE() function. Register P3 contains the address
6015** of a memory cell in this (the parent) VM that is used to allocate the
6016** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006017**
6018** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006019**
6020** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006021*/
dan76d462e2009-08-30 11:42:51 +00006022case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006023 int nMem; /* Number of memory registers for sub-program */
6024 int nByte; /* Bytes of runtime space required for sub-program */
6025 Mem *pRt; /* Register to allocate runtime space */
6026 Mem *pMem; /* Used to iterate through memory cells */
6027 Mem *pEnd; /* Last memory cell in new array */
6028 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6029 SubProgram *pProgram; /* Sub-program to execute */
6030 void *t; /* Token identifying trigger */
6031
6032 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006033 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006034 assert( pProgram->nOp>0 );
6035
dan1da40a32009-09-19 17:00:31 +00006036 /* If the p5 flag is clear, then recursive invocation of triggers is
6037 ** disabled for backwards compatibility (p5 is set if this sub-program
6038 ** is really a trigger, not a foreign key action, and the flag set
6039 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006040 **
6041 ** It is recursive invocation of triggers, at the SQL level, that is
6042 ** disabled. In some cases a single trigger may generate more than one
6043 ** SubProgram (if the trigger may be executed with more than one different
6044 ** ON CONFLICT algorithm). SubProgram structures associated with a
6045 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006046 ** variable. */
6047 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006048 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006049 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6050 if( pFrame ) break;
6051 }
6052
danf5894502009-10-07 18:41:19 +00006053 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006054 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006055 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006056 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006057 }
6058
6059 /* Register pRt is used to store the memory required to save the state
6060 ** of the current program, and the memory required at runtime to execute
6061 ** the trigger program. If this trigger has been fired before, then pRt
6062 ** is already allocated. Otherwise, it must be initialized. */
6063 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00006064 /* SubProgram.nMem is set to the number of memory cells used by the
6065 ** program stored in SubProgram.aOp. As well as these, one memory
6066 ** cell is required for each cursor used by the program. Set local
6067 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6068 */
dan65a7cd12009-09-01 12:16:01 +00006069 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006070 assert( nMem>0 );
6071 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006072 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006073 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006074 + pProgram->nCsr * sizeof(VdbeCursor*)
6075 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006076 pFrame = sqlite3DbMallocZero(db, nByte);
6077 if( !pFrame ){
6078 goto no_mem;
6079 }
6080 sqlite3VdbeMemRelease(pRt);
6081 pRt->flags = MEM_Frame;
6082 pRt->u.pFrame = pFrame;
6083
6084 pFrame->v = p;
6085 pFrame->nChildMem = nMem;
6086 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006087 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006088 pFrame->aMem = p->aMem;
6089 pFrame->nMem = p->nMem;
6090 pFrame->apCsr = p->apCsr;
6091 pFrame->nCursor = p->nCursor;
6092 pFrame->aOp = p->aOp;
6093 pFrame->nOp = p->nOp;
6094 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006095#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006096 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006097#endif
dan165921a2009-08-28 18:53:45 +00006098
6099 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6100 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006101 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006102 pMem->db = db;
6103 }
6104 }else{
6105 pFrame = pRt->u.pFrame;
drh9f6168b2016-03-19 23:32:58 +00006106 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6107 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006108 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006109 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006110 }
6111
6112 p->nFrame++;
6113 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006114 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006115 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006116 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006117 assert( pFrame->pAuxData==0 );
6118 pFrame->pAuxData = p->pAuxData;
6119 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006120 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006121 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006122 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006123 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006124 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006125 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006126 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006127 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006128 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006129 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006130#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006131 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006132#endif
drhf56fa462015-04-13 21:39:54 +00006133 pOp = &aOp[-1];
dan165921a2009-08-28 18:53:45 +00006134
6135 break;
6136}
6137
dan76d462e2009-08-30 11:42:51 +00006138/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006139**
dan76d462e2009-08-30 11:42:51 +00006140** This opcode is only ever present in sub-programs called via the
6141** OP_Program instruction. Copy a value currently stored in a memory
6142** cell of the calling (parent) frame to cell P2 in the current frames
6143** address space. This is used by trigger programs to access the new.*
6144** and old.* values.
dan165921a2009-08-28 18:53:45 +00006145**
dan76d462e2009-08-30 11:42:51 +00006146** The address of the cell in the parent frame is determined by adding
6147** the value of the P1 argument to the value of the P1 argument to the
6148** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006149*/
drh27a348c2015-04-13 19:14:06 +00006150case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006151 VdbeFrame *pFrame;
6152 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006153 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006154 pFrame = p->pFrame;
6155 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006156 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6157 break;
6158}
6159
danielk197793758c82005-01-21 08:13:14 +00006160#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006161
dan1da40a32009-09-19 17:00:31 +00006162#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006163/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006164** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006165**
dan0ff297e2009-09-25 17:03:14 +00006166** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6167** If P1 is non-zero, the database constraint counter is incremented
6168** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006169** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006170*/
dan32b09f22009-09-23 17:29:59 +00006171case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006172 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006173 db->nDeferredImmCons += pOp->p2;
6174 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006175 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006176 }else{
dan0ff297e2009-09-25 17:03:14 +00006177 p->nFkConstraint += pOp->p2;
6178 }
6179 break;
6180}
6181
6182/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006183** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006184**
6185** This opcode tests if a foreign key constraint-counter is currently zero.
6186** If so, jump to instruction P2. Otherwise, fall through to the next
6187** instruction.
6188**
6189** If P1 is non-zero, then the jump is taken if the database constraint-counter
6190** is zero (the one that counts deferred constraint violations). If P1 is
6191** zero, the jump is taken if the statement constraint-counter is zero
6192** (immediate foreign key constraint violations).
6193*/
6194case OP_FkIfZero: { /* jump */
6195 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006196 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006197 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006198 }else{
drh688852a2014-02-17 22:40:43 +00006199 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006200 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006201 }
dan1da40a32009-09-19 17:00:31 +00006202 break;
6203}
6204#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6205
drh205f48e2004-11-05 00:43:11 +00006206#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006207/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006208** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006209**
dan76d462e2009-08-30 11:42:51 +00006210** P1 is a register in the root frame of this VM (the root frame is
6211** different from the current frame if this instruction is being executed
6212** within a sub-program). Set the value of register P1 to the maximum of
6213** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006214**
6215** This instruction throws an error if the memory cell is not initially
6216** an integer.
6217*/
dan76d462e2009-08-30 11:42:51 +00006218case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006219 VdbeFrame *pFrame;
6220 if( p->pFrame ){
6221 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6222 pIn1 = &pFrame->aMem[pOp->p1];
6223 }else{
drha6c2ed92009-11-14 23:22:23 +00006224 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006225 }
drh2b4ded92010-09-27 21:09:31 +00006226 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006227 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006228 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006229 sqlite3VdbeMemIntegerify(pIn2);
6230 if( pIn1->u.i<pIn2->u.i){
6231 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006232 }
6233 break;
6234}
6235#endif /* SQLITE_OMIT_AUTOINCREMENT */
6236
drh8b0cf382015-10-06 21:07:06 +00006237/* Opcode: IfPos P1 P2 P3 * *
6238** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006239**
drh16897072015-03-07 00:57:37 +00006240** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006241** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006242** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006243**
drh16897072015-03-07 00:57:37 +00006244** If the initial value of register P1 is less than 1, then the
6245** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006246*/
drh9cbf3422008-01-17 16:22:13 +00006247case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006248 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006249 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006250 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006251 if( pIn1->u.i>0 ){
6252 pIn1->u.i -= pOp->p3;
6253 goto jump_to_p2;
6254 }
drhec7429a2005-10-06 16:53:14 +00006255 break;
6256}
6257
drhcc2fa4c2016-01-25 15:57:29 +00006258/* Opcode: OffsetLimit P1 P2 P3 * *
6259** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006260**
drhcc2fa4c2016-01-25 15:57:29 +00006261** This opcode performs a commonly used computation associated with
6262** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6263** holds the offset counter. The opcode computes the combined value
6264** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6265** value computed is the total number of rows that will need to be
6266** visited in order to complete the query.
6267**
6268** If r[P3] is zero or negative, that means there is no OFFSET
6269** and r[P2] is set to be the value of the LIMIT, r[P1].
6270**
6271** if r[P1] is zero or negative, that means there is no LIMIT
6272** and r[P2] is set to -1.
6273**
6274** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006275*/
drhcc2fa4c2016-01-25 15:57:29 +00006276case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006277 i64 x;
drh3c657212009-11-17 23:59:58 +00006278 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006279 pIn3 = &aMem[pOp->p3];
6280 pOut = out2Prerelease(p, pOp);
6281 assert( pIn1->flags & MEM_Int );
6282 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006283 x = pIn1->u.i;
6284 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6285 /* If the LIMIT is less than or equal to zero, loop forever. This
6286 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6287 ** also loop forever. This is undocumented. In fact, one could argue
6288 ** that the loop should terminate. But assuming 1 billion iterations
6289 ** per second (far exceeding the capabilities of any current hardware)
6290 ** it would take nearly 300 years to actually reach the limit. So
6291 ** looping forever is a reasonable approximation. */
6292 pOut->u.i = -1;
6293 }else{
6294 pOut->u.i = x;
6295 }
drh15007a92006-01-08 18:10:17 +00006296 break;
6297}
6298
drhf99dd352016-12-18 17:42:00 +00006299/* Opcode: IfNotZero P1 P2 * * *
6300** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006301**
drh16897072015-03-07 00:57:37 +00006302** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006303** initially greater than zero, then decrement the value in register P1.
6304** If it is non-zero (negative or positive) and then also jump to P2.
6305** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006306*/
drh16897072015-03-07 00:57:37 +00006307case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006308 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006309 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006310 VdbeBranchTaken(pIn1->u.i<0, 2);
6311 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006312 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006313 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006314 }
6315 break;
6316}
6317
6318/* Opcode: DecrJumpZero P1 P2 * * *
6319** Synopsis: if (--r[P1])==0 goto P2
6320**
drhab5be2e2016-11-30 05:08:59 +00006321** Register P1 must hold an integer. Decrement the value in P1
6322** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006323*/
6324case OP_DecrJumpZero: { /* jump, in1 */
6325 pIn1 = &aMem[pOp->p1];
6326 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006327 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6328 VdbeBranchTaken(pIn1->u.i==0, 2);
6329 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006330 break;
6331}
6332
drh16897072015-03-07 00:57:37 +00006333
drh8f26da62018-07-05 21:22:57 +00006334/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006335** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006336**
drh8f26da62018-07-05 21:22:57 +00006337** Execute the xStep function for an aggregate.
6338** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006339** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006340** accumulator.
drhe5095352002-02-24 03:25:14 +00006341**
drh98757152008-01-09 23:04:12 +00006342** The P5 arguments are taken from register P2 and its
6343** successors.
drhe5095352002-02-24 03:25:14 +00006344*/
drh8f26da62018-07-05 21:22:57 +00006345/* Opcode: AggInverse * P2 P3 P4 P5
6346** Synopsis: accum=r[P3] inverse(r[P2@P5])
6347**
6348** Execute the xInverse function for an aggregate.
6349** The function has P5 arguments. P4 is a pointer to the
6350** FuncDef structure that specifies the function. Register P3 is the
6351** accumulator.
6352**
6353** The P5 arguments are taken from register P2 and its
6354** successors.
6355*/
6356/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006357** Synopsis: accum=r[P3] step(r[P2@P5])
6358**
dan9a947222018-06-14 19:06:36 +00006359** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6360** aggregate. The function has P5 arguments. P4 is a pointer to the
6361** FuncDef structure that specifies the function. Register P3 is the
6362** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006363**
6364** The P5 arguments are taken from register P2 and its
6365** successors.
6366**
6367** This opcode is initially coded as OP_AggStep0. On first evaluation,
6368** the FuncDef stored in P4 is converted into an sqlite3_context and
6369** the opcode is changed. In this way, the initialization of the
6370** sqlite3_context only happens once, instead of on each call to the
6371** step function.
6372*/
drh8f26da62018-07-05 21:22:57 +00006373case OP_AggInverse:
6374case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006375 int n;
drh9c7c9132015-06-26 18:16:52 +00006376 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006377
drh9c7c9132015-06-26 18:16:52 +00006378 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006379 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006380 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6381 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006382 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006383 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6384 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006385 if( pCtx==0 ) goto no_mem;
6386 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006387 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6388 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006389 pCtx->pFunc = pOp->p4.pFunc;
6390 pCtx->iOp = (int)(pOp - aOp);
6391 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006392 pCtx->skipFlag = 0;
6393 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006394 pCtx->argc = n;
6395 pOp->p4type = P4_FUNCCTX;
6396 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006397
6398 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006399 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006400
drh8f26da62018-07-05 21:22:57 +00006401 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006402 /* Fall through into OP_AggStep */
6403}
drh8f26da62018-07-05 21:22:57 +00006404case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006405 int i;
6406 sqlite3_context *pCtx;
6407 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006408
6409 assert( pOp->p4type==P4_FUNCCTX );
6410 pCtx = pOp->p4.pCtx;
6411 pMem = &aMem[pOp->p3];
6412
drh2c885d02018-07-07 19:36:04 +00006413#ifdef SQLITE_DEBUG
6414 if( pOp->p1 ){
6415 /* This is an OP_AggInverse call. Verify that xStep has always
6416 ** been called at least once prior to any xInverse call. */
6417 assert( pMem->uTemp==0x1122e0e3 );
6418 }else{
6419 /* This is an OP_AggStep call. Mark it as such. */
6420 pMem->uTemp = 0x1122e0e3;
6421 }
6422#endif
6423
drh9c7c9132015-06-26 18:16:52 +00006424 /* If this function is inside of a trigger, the register array in aMem[]
6425 ** might change from one evaluation to the next. The next block of code
6426 ** checks to see if the register array has changed, and if so it
6427 ** reinitializes the relavant parts of the sqlite3_context object */
6428 if( pCtx->pMem != pMem ){
6429 pCtx->pMem = pMem;
6430 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6431 }
6432
6433#ifdef SQLITE_DEBUG
6434 for(i=0; i<pCtx->argc; i++){
6435 assert( memIsValid(pCtx->argv[i]) );
6436 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6437 }
6438#endif
6439
drhabfcea22005-09-06 20:36:48 +00006440 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006441 assert( pCtx->pOut->flags==MEM_Null );
6442 assert( pCtx->isError==0 );
6443 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006444#ifndef SQLITE_OMIT_WINDOWFUNC
6445 if( pOp->p1 ){
6446 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6447 }else
6448#endif
6449 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6450
drhf09ac0b2018-01-23 03:44:06 +00006451 if( pCtx->isError ){
6452 if( pCtx->isError>0 ){
6453 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006454 rc = pCtx->isError;
6455 }
drhf09ac0b2018-01-23 03:44:06 +00006456 if( pCtx->skipFlag ){
6457 assert( pOp[-1].opcode==OP_CollSeq );
6458 i = pOp[-1].p1;
6459 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6460 pCtx->skipFlag = 0;
6461 }
6462 sqlite3VdbeMemRelease(pCtx->pOut);
6463 pCtx->pOut->flags = MEM_Null;
6464 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006465 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006466 }
drhf09ac0b2018-01-23 03:44:06 +00006467 assert( pCtx->pOut->flags==MEM_Null );
6468 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006469 break;
6470}
6471
drh8f26da62018-07-05 21:22:57 +00006472/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006473** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006474**
dan9a947222018-06-14 19:06:36 +00006475** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00006476** or window function. Execute the finalizer function
6477** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00006478**
6479** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006480** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006481** argument is not used by this opcode. It is only there to disambiguate
6482** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00006483** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00006484** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006485*/
drh8f26da62018-07-05 21:22:57 +00006486/* Opcode: AggValue * P2 P3 P4 *
6487** Synopsis: r[P3]=value N=P2
6488**
6489** Invoke the xValue() function and store the result in register P3.
6490**
6491** P2 is the number of arguments that the step function takes and
6492** P4 is a pointer to the FuncDef for this function. The P2
6493** argument is not used by this opcode. It is only there to disambiguate
6494** functions that can take varying numbers of arguments. The
6495** P4 argument is only needed for the case where
6496** the step function was not previously called.
6497*/
6498case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00006499case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006500 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006501 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00006502 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00006503 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006504 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006505#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006506 if( pOp->p3 ){
6507 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006508 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006509 }else
6510#endif
drh8f26da62018-07-05 21:22:57 +00006511 {
6512 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6513 }
dan67a9b8e2018-06-22 20:51:35 +00006514
drh4c8555f2009-06-25 01:47:11 +00006515 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006516 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006517 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006518 }
drh2dca8682008-03-21 17:13:13 +00006519 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006520 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006521 if( sqlite3VdbeMemTooBig(pMem) ){
6522 goto too_big;
6523 }
drh5e00f6c2001-09-13 13:46:56 +00006524 break;
6525}
6526
dan5cf53532010-05-01 16:40:20 +00006527#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006528/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006529**
6530** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006531** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6532** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006533** SQLITE_BUSY or not, respectively. Write the number of pages in the
6534** WAL after the checkpoint into mem[P3+1] and the number of pages
6535** in the WAL that have been checkpointed after the checkpoint
6536** completes into mem[P3+2]. However on an error, mem[P3+1] and
6537** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006538*/
6539case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006540 int i; /* Loop counter */
6541 int aRes[3]; /* Results */
6542 Mem *pMem; /* Write results here */
6543
drh9e92a472013-06-27 17:40:30 +00006544 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006545 aRes[0] = 0;
6546 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006547 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6548 || pOp->p2==SQLITE_CHECKPOINT_FULL
6549 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006550 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006551 );
drh30aa3b92011-02-07 23:56:01 +00006552 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006553 if( rc ){
6554 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006555 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006556 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006557 }
drh30aa3b92011-02-07 23:56:01 +00006558 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6559 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6560 }
dan7c246102010-04-12 19:00:29 +00006561 break;
6562};
dan5cf53532010-05-01 16:40:20 +00006563#endif
drh5e00f6c2001-09-13 13:46:56 +00006564
drhcac29a62010-07-02 19:36:52 +00006565#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006566/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006567**
6568** Change the journal mode of database P1 to P3. P3 must be one of the
6569** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6570** modes (delete, truncate, persist, off and memory), this is a simple
6571** operation. No IO is required.
6572**
6573** If changing into or out of WAL mode the procedure is more complicated.
6574**
6575** Write a string containing the final journal-mode to register P2.
6576*/
drh27a348c2015-04-13 19:14:06 +00006577case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006578 Btree *pBt; /* Btree to change journal mode of */
6579 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006580 int eNew; /* New journal mode */
6581 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006582#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006583 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006584#endif
dane04dc882010-04-20 18:53:15 +00006585
drh27a348c2015-04-13 19:14:06 +00006586 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006587 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006588 assert( eNew==PAGER_JOURNALMODE_DELETE
6589 || eNew==PAGER_JOURNALMODE_TRUNCATE
6590 || eNew==PAGER_JOURNALMODE_PERSIST
6591 || eNew==PAGER_JOURNALMODE_OFF
6592 || eNew==PAGER_JOURNALMODE_MEMORY
6593 || eNew==PAGER_JOURNALMODE_WAL
6594 || eNew==PAGER_JOURNALMODE_QUERY
6595 );
6596 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006597 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006598
dane04dc882010-04-20 18:53:15 +00006599 pBt = db->aDb[pOp->p1].pBt;
6600 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006601 eOld = sqlite3PagerGetJournalMode(pPager);
6602 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6603 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006604
6605#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006606 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006607
drhd80b2332010-05-01 00:59:37 +00006608 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006609 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006610 */
6611 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006612 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006613 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006614 ){
drh0b9b4302010-06-11 17:01:24 +00006615 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006616 }
6617
drh0b9b4302010-06-11 17:01:24 +00006618 if( (eNew!=eOld)
6619 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6620 ){
danc0537fe2013-06-28 19:41:43 +00006621 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006622 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006623 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006624 "cannot change %s wal mode from within a transaction",
6625 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6626 );
drh9467abf2016-02-17 18:44:11 +00006627 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006628 }else{
6629
6630 if( eOld==PAGER_JOURNALMODE_WAL ){
6631 /* If leaving WAL mode, close the log file. If successful, the call
6632 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6633 ** file. An EXCLUSIVE lock may still be held on the database file
6634 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006635 */
dan7fb89902016-08-12 16:21:15 +00006636 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006637 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006638 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006639 }
drh242c4f72010-06-22 14:49:39 +00006640 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6641 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6642 ** as an intermediate */
6643 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006644 }
6645
6646 /* Open a transaction on the database file. Regardless of the journal
6647 ** mode, this transaction always uses a rollback journal.
6648 */
6649 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6650 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006651 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006652 }
6653 }
6654 }
dan5cf53532010-05-01 16:40:20 +00006655#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006656
drh9467abf2016-02-17 18:44:11 +00006657 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006658 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006659
dane04dc882010-04-20 18:53:15 +00006660 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006661 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006662 pOut->n = sqlite3Strlen30(pOut->z);
6663 pOut->enc = SQLITE_UTF8;
6664 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006665 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006666 break;
drhcac29a62010-07-02 19:36:52 +00006667};
6668#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006669
drhfdbcdee2007-03-27 14:44:50 +00006670#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh9ef5e772016-08-19 14:20:56 +00006671/* Opcode: Vacuum P1 * * * *
drh6f8c91c2003-12-07 00:24:35 +00006672**
drh9ef5e772016-08-19 14:20:56 +00006673** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6674** for an attached database. The "temp" database may not be vacuumed.
drh6f8c91c2003-12-07 00:24:35 +00006675*/
drh9cbf3422008-01-17 16:22:13 +00006676case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006677 assert( p->readOnly==0 );
drh9ef5e772016-08-19 14:20:56 +00006678 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006679 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006680 break;
6681}
drh154d4b22006-09-21 11:02:16 +00006682#endif
drh6f8c91c2003-12-07 00:24:35 +00006683
danielk1977dddbcdc2007-04-26 14:42:34 +00006684#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006685/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006686**
6687** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006688** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006689** P2. Otherwise, fall through to the next instruction.
6690*/
drh9cbf3422008-01-17 16:22:13 +00006691case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006692 Btree *pBt;
6693
6694 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006695 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006696 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006697 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006698 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006699 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006700 if( rc ){
6701 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006702 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006703 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006704 }
6705 break;
6706}
6707#endif
6708
drhba968db2018-07-24 22:02:12 +00006709/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006710**
drh25df48d2014-07-22 14:58:12 +00006711** Cause precompiled statements to expire. When an expired statement
6712** is executed using sqlite3_step() it will either automatically
6713** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6714** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006715**
6716** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006717** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00006718**
6719** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
6720** then running SQL statements are allowed to continue to run to completion.
6721** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
6722** that might help the statement run faster but which does not affect the
6723** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00006724*/
drh9cbf3422008-01-17 16:22:13 +00006725case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00006726 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00006727 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00006728 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00006729 }else{
drhba968db2018-07-24 22:02:12 +00006730 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00006731 }
6732 break;
6733}
6734
danielk1977c00da102006-01-07 13:21:04 +00006735#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006736/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006737** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006738**
6739** Obtain a lock on a particular table. This instruction is only used when
6740** the shared-cache feature is enabled.
6741**
danielk197796d48e92009-06-29 06:00:37 +00006742** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006743** on which the lock is acquired. A readlock is obtained if P3==0 or
6744** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006745**
6746** P2 contains the root-page of the table to lock.
6747**
drh66a51672008-01-03 00:01:23 +00006748** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006749** used to generate an error message if the lock cannot be obtained.
6750*/
drh9cbf3422008-01-17 16:22:13 +00006751case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006752 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006753 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006754 int p1 = pOp->p1;
6755 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006756 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006757 assert( isWriteLock==0 || isWriteLock==1 );
6758 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006759 if( rc ){
6760 if( (rc&0xFF)==SQLITE_LOCKED ){
6761 const char *z = pOp->p4.z;
6762 sqlite3VdbeError(p, "database table is locked: %s", z);
6763 }
6764 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006765 }
danielk1977c00da102006-01-07 13:21:04 +00006766 }
6767 break;
6768}
drhb9bb7c12006-06-11 23:41:55 +00006769#endif /* SQLITE_OMIT_SHARED_CACHE */
6770
6771#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006772/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006773**
danielk19773e3a84d2008-08-01 17:37:40 +00006774** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6775** xBegin method for that table.
6776**
6777** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006778** within a callback to a virtual table xSync() method. If it is, the error
6779** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006780*/
drh9cbf3422008-01-17 16:22:13 +00006781case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006782 VTable *pVTab;
6783 pVTab = pOp->p4.pVtab;
6784 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006785 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006786 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006787 break;
6788}
6789#endif /* SQLITE_OMIT_VIRTUALTABLE */
6790
6791#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006792/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006793**
dan73779452015-03-19 18:56:17 +00006794** P2 is a register that holds the name of a virtual table in database
6795** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006796*/
drh9cbf3422008-01-17 16:22:13 +00006797case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006798 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006799 const char *zTab; /* Name of the virtual table */
6800
dan73779452015-03-19 18:56:17 +00006801 memset(&sMem, 0, sizeof(sMem));
6802 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006803 /* Because P2 is always a static string, it is impossible for the
6804 ** sqlite3VdbeMemCopy() to fail */
6805 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6806 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006807 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006808 assert( rc==SQLITE_OK );
6809 zTab = (const char*)sqlite3_value_text(&sMem);
6810 assert( zTab || db->mallocFailed );
6811 if( zTab ){
6812 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006813 }
6814 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006815 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006816 break;
6817}
6818#endif /* SQLITE_OMIT_VIRTUALTABLE */
6819
6820#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006821/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006822**
drh66a51672008-01-03 00:01:23 +00006823** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006824** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006825*/
drh9cbf3422008-01-17 16:22:13 +00006826case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006827 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006828 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006829 db->nVDestroy--;
drh9467abf2016-02-17 18:44:11 +00006830 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006831 break;
6832}
6833#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006834
drh9eff6162006-06-12 21:59:13 +00006835#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006836/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006837**
drh66a51672008-01-03 00:01:23 +00006838** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006839** P1 is a cursor number. This opcode opens a cursor to the virtual
6840** table and stores that cursor in P1.
6841*/
drh9cbf3422008-01-17 16:22:13 +00006842case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006843 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006844 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006845 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006846 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006847
drh1713afb2013-06-28 01:24:57 +00006848 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006849 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006850 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006851 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006852 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6853 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006854 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006855 }
6856 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006857 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006858 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006859 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006860
drh9467abf2016-02-17 18:44:11 +00006861 /* Initialize sqlite3_vtab_cursor base class */
6862 pVCur->pVtab = pVtab;
6863
6864 /* Initialize vdbe cursor object */
6865 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6866 if( pCur ){
6867 pCur->uc.pVCur = pVCur;
6868 pVtab->nRef++;
6869 }else{
6870 assert( db->mallocFailed );
6871 pModule->xClose(pVCur);
6872 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006873 }
drh9eff6162006-06-12 21:59:13 +00006874 break;
6875}
6876#endif /* SQLITE_OMIT_VIRTUALTABLE */
6877
6878#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006879/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006880** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006881**
6882** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6883** the filtered result set is empty.
6884**
drh66a51672008-01-03 00:01:23 +00006885** P4 is either NULL or a string that was generated by the xBestIndex
6886** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006887** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006888**
drh9eff6162006-06-12 21:59:13 +00006889** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006890** by P1. The integer query plan parameter to xFilter is stored in register
6891** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006892** xFilter method. Registers P3+2..P3+1+argc are the argc
6893** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006894** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006895**
danielk19776dbee812008-01-03 18:39:41 +00006896** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006897*/
drh9cbf3422008-01-17 16:22:13 +00006898case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006899 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006900 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006901 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006902 Mem *pQuery;
6903 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006904 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006905 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006906 VdbeCursor *pCur;
6907 int res;
6908 int i;
6909 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006910
drha6c2ed92009-11-14 23:22:23 +00006911 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006912 pArgc = &pQuery[1];
6913 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006914 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006915 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006916 assert( pCur->eCurType==CURTYPE_VTAB );
6917 pVCur = pCur->uc.pVCur;
6918 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006919 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006920
drh9cbf3422008-01-17 16:22:13 +00006921 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006922 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006923 nArg = (int)pArgc->u.i;
6924 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006925
drh644a5292006-12-20 14:53:38 +00006926 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006927 res = 0;
6928 apArg = p->apArg;
6929 for(i = 0; i<nArg; i++){
6930 apArg[i] = &pArgc[i+1];
6931 }
drhc960dcb2015-11-20 19:22:01 +00006932 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006933 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006934 if( rc ) goto abort_due_to_error;
6935 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006936 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006937 VdbeBranchTaken(res!=0,2);
6938 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006939 break;
6940}
6941#endif /* SQLITE_OMIT_VIRTUALTABLE */
6942
6943#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00006944/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00006945** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006946**
drh6f390be2018-01-11 17:04:26 +00006947** Store in register P3 the value of the P2-th column of
6948** the current row of the virtual-table of cursor P1.
6949**
6950** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00006951** an unchanging column during an UPDATE operation, then the P5
6952** value is 1. Otherwise, P5 is 0. The P5 value is returned
drhbbd574b2018-05-24 17:25:35 +00006953** by sqlite3_vtab_nochange() routine and can be used
drh6f390be2018-01-11 17:04:26 +00006954** by virtual table implementations to return special "no-change"
6955** marks which can be more efficient, depending on the virtual table.
drh9eff6162006-06-12 21:59:13 +00006956*/
6957case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006958 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006959 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006960 Mem *pDest;
6961 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006962
drhdfe88ec2008-11-03 20:55:06 +00006963 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006964 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00006965 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006966 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006967 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006968 if( pCur->nullRow ){
6969 sqlite3VdbeMemSetNull(pDest);
6970 break;
6971 }
drhc960dcb2015-11-20 19:22:01 +00006972 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006973 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006974 assert( pModule->xColumn );
6975 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006976 sContext.pOut = pDest;
drhce2fbd12018-01-12 21:00:14 +00006977 if( pOp->p5 ){
6978 sqlite3VdbeMemSetNull(pDest);
6979 pDest->flags = MEM_Null|MEM_Zero;
6980 pDest->u.nZero = 0;
6981 }else{
6982 MemSetTypeFlag(pDest, MEM_Null);
6983 }
drhc960dcb2015-11-20 19:22:01 +00006984 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006985 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00006986 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00006987 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00006988 rc = sContext.isError;
6989 }
drh9bd038f2014-08-27 14:14:06 +00006990 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006991 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006992 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006993
drhde4fcfd2008-01-19 23:50:26 +00006994 if( sqlite3VdbeMemTooBig(pDest) ){
6995 goto too_big;
6996 }
drh9467abf2016-02-17 18:44:11 +00006997 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00006998 break;
6999}
7000#endif /* SQLITE_OMIT_VIRTUALTABLE */
7001
7002#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007003/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007004**
7005** Advance virtual table P1 to the next row in its result set and
7006** jump to instruction P2. Or, if the virtual table has reached
7007** the end of its result set, then fall through to the next instruction.
7008*/
drh9cbf3422008-01-17 16:22:13 +00007009case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007010 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007011 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007012 int res;
drh856c1032009-06-02 15:21:42 +00007013 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007014
drhc54a6172009-06-02 16:06:03 +00007015 res = 0;
drh856c1032009-06-02 15:21:42 +00007016 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007017 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007018 if( pCur->nullRow ){
7019 break;
7020 }
drhc960dcb2015-11-20 19:22:01 +00007021 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007022 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007023 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007024
drhde4fcfd2008-01-19 23:50:26 +00007025 /* Invoke the xNext() method of the module. There is no way for the
7026 ** underlying implementation to return an error if one occurs during
7027 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7028 ** data is available) and the error code returned when xColumn or
7029 ** some other method is next invoked on the save virtual table cursor.
7030 */
drhc960dcb2015-11-20 19:22:01 +00007031 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007032 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007033 if( rc ) goto abort_due_to_error;
7034 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007035 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007036 if( !res ){
7037 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007038 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007039 }
drh49afe3a2013-07-10 03:05:14 +00007040 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007041}
7042#endif /* SQLITE_OMIT_VIRTUALTABLE */
7043
danielk1977182c4ba2007-06-27 15:53:34 +00007044#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007045/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007046**
drh66a51672008-01-03 00:01:23 +00007047** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007048** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007049** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007050*/
drh9cbf3422008-01-17 16:22:13 +00007051case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007052 sqlite3_vtab *pVtab;
7053 Mem *pName;
7054
danielk1977595a5232009-07-24 17:58:53 +00007055 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007056 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007057 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007058 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007059 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007060 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007061 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007062 testcase( pName->enc==SQLITE_UTF8 );
7063 testcase( pName->enc==SQLITE_UTF16BE );
7064 testcase( pName->enc==SQLITE_UTF16LE );
7065 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007066 if( rc ) goto abort_due_to_error;
7067 rc = pVtab->pModule->xRename(pVtab, pName->z);
7068 sqlite3VtabImportErrmsg(p, pVtab);
7069 p->expired = 0;
7070 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007071 break;
7072}
7073#endif
drh4cbdda92006-06-14 19:00:20 +00007074
7075#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007076/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007077** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007078**
drh66a51672008-01-03 00:01:23 +00007079** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007080** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007081** are contiguous memory cells starting at P3 to pass to the xUpdate
7082** invocation. The value in register (P3+P2-1) corresponds to the
7083** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007084**
7085** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007086** The argv[0] element (which corresponds to memory cell P3)
7087** is the rowid of a row to delete. If argv[0] is NULL then no
7088** deletion occurs. The argv[1] element is the rowid of the new
7089** row. This can be NULL to have the virtual table select the new
7090** rowid for itself. The subsequent elements in the array are
7091** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007092**
7093** If P2==1 then no insert is performed. argv[0] is the rowid of
7094** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007095**
7096** P1 is a boolean flag. If it is set to true and the xUpdate call
7097** is successful, then the value returned by sqlite3_last_insert_rowid()
7098** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007099**
7100** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7101** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007102*/
drh9cbf3422008-01-17 16:22:13 +00007103case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007104 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007105 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007106 int nArg;
7107 int i;
7108 sqlite_int64 rowid;
7109 Mem **apArg;
7110 Mem *pX;
7111
danb061d052011-04-25 18:49:57 +00007112 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7113 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7114 );
drh9e92a472013-06-27 17:40:30 +00007115 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007116 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007117 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007118 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007119 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7120 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007121 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007122 }
7123 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007124 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007125 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007126 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007127 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007128 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007129 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007130 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007131 assert( memIsValid(pX) );
7132 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007133 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007134 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007135 }
danb061d052011-04-25 18:49:57 +00007136 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007137 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007138 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007139 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007140 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007141 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007142 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007143 }
drhd91c1a12013-02-09 13:58:25 +00007144 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007145 if( pOp->p5==OE_Ignore ){
7146 rc = SQLITE_OK;
7147 }else{
7148 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7149 }
7150 }else{
7151 p->nChange++;
7152 }
drh9467abf2016-02-17 18:44:11 +00007153 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007154 }
drh4cbdda92006-06-14 19:00:20 +00007155 break;
danielk1977399918f2006-06-14 13:03:23 +00007156}
7157#endif /* SQLITE_OMIT_VIRTUALTABLE */
7158
danielk197759a93792008-05-15 17:48:20 +00007159#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7160/* Opcode: Pagecount P1 P2 * * *
7161**
7162** Write the current number of pages in database P1 to memory cell P2.
7163*/
drh27a348c2015-04-13 19:14:06 +00007164case OP_Pagecount: { /* out2 */
7165 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007166 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007167 break;
7168}
7169#endif
7170
drh60ac3f42010-11-23 18:59:27 +00007171
7172#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7173/* Opcode: MaxPgcnt P1 P2 P3 * *
7174**
7175** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007176** Do not let the maximum page count fall below the current page count and
7177** do not change the maximum page count value if P3==0.
7178**
drh60ac3f42010-11-23 18:59:27 +00007179** Store the maximum page count after the change in register P2.
7180*/
drh27a348c2015-04-13 19:14:06 +00007181case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007182 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007183 Btree *pBt;
7184
drh27a348c2015-04-13 19:14:06 +00007185 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007186 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007187 newMax = 0;
7188 if( pOp->p3 ){
7189 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007190 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007191 }
7192 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007193 break;
7194}
7195#endif
7196
drh3e34eab2017-07-19 19:48:40 +00007197/* Opcode: Function0 P1 P2 P3 P4 P5
7198** Synopsis: r[P3]=func(r[P2@P5])
7199**
7200** Invoke a user function (P4 is a pointer to a FuncDef object that
7201** defines the function) with P5 arguments taken from register P2 and
7202** successors. The result of the function is stored in register P3.
7203** Register P3 must not be one of the function inputs.
7204**
7205** P1 is a 32-bit bitmask indicating whether or not each argument to the
7206** function was determined to be constant at compile time. If the first
7207** argument was constant then bit 0 of P1 is set. This is used to determine
7208** whether meta data associated with a user function argument using the
7209** sqlite3_set_auxdata() API may be safely retained until the next
7210** invocation of this opcode.
7211**
7212** See also: Function, AggStep, AggFinal
7213*/
7214/* Opcode: Function P1 P2 P3 P4 P5
7215** Synopsis: r[P3]=func(r[P2@P5])
7216**
7217** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7218** contains a pointer to the function to be run) with P5 arguments taken
7219** from register P2 and successors. The result of the function is stored
7220** in register P3. Register P3 must not be one of the function inputs.
7221**
7222** P1 is a 32-bit bitmask indicating whether or not each argument to the
7223** function was determined to be constant at compile time. If the first
7224** argument was constant then bit 0 of P1 is set. This is used to determine
7225** whether meta data associated with a user function argument using the
7226** sqlite3_set_auxdata() API may be safely retained until the next
7227** invocation of this opcode.
7228**
7229** SQL functions are initially coded as OP_Function0 with P4 pointing
7230** to a FuncDef object. But on first evaluation, the P4 operand is
7231** automatically converted into an sqlite3_context object and the operation
7232** changed to this OP_Function opcode. In this way, the initialization of
7233** the sqlite3_context object occurs only once, rather than once for each
7234** evaluation of the function.
7235**
7236** See also: Function0, AggStep, AggFinal
7237*/
mistachkin758784d2018-07-25 15:12:29 +00007238case OP_PureFunc0: /* group */
7239case OP_Function0: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007240 int n;
7241 sqlite3_context *pCtx;
7242
7243 assert( pOp->p4type==P4_FUNCDEF );
7244 n = pOp->p5;
7245 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7246 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
7247 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
7248 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7249 if( pCtx==0 ) goto no_mem;
7250 pCtx->pOut = 0;
7251 pCtx->pFunc = pOp->p4.pFunc;
7252 pCtx->iOp = (int)(pOp - aOp);
7253 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007254 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007255 pCtx->argc = n;
7256 pOp->p4type = P4_FUNCCTX;
7257 pOp->p4.pCtx = pCtx;
7258 assert( OP_PureFunc == OP_PureFunc0+2 );
7259 assert( OP_Function == OP_Function0+2 );
7260 pOp->opcode += 2;
7261 /* Fall through into OP_Function */
7262}
mistachkin758784d2018-07-25 15:12:29 +00007263case OP_PureFunc: /* group */
7264case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007265 int i;
7266 sqlite3_context *pCtx;
7267
7268 assert( pOp->p4type==P4_FUNCCTX );
7269 pCtx = pOp->p4.pCtx;
7270
7271 /* If this function is inside of a trigger, the register array in aMem[]
7272 ** might change from one evaluation to the next. The next block of code
7273 ** checks to see if the register array has changed, and if so it
7274 ** reinitializes the relavant parts of the sqlite3_context object */
7275 pOut = &aMem[pOp->p3];
7276 if( pCtx->pOut != pOut ){
7277 pCtx->pOut = pOut;
7278 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7279 }
7280
7281 memAboutToChange(p, pOut);
7282#ifdef SQLITE_DEBUG
7283 for(i=0; i<pCtx->argc; i++){
7284 assert( memIsValid(pCtx->argv[i]) );
7285 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7286 }
7287#endif
7288 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007289 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007290 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7291
7292 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007293 if( pCtx->isError ){
7294 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007295 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7296 rc = pCtx->isError;
7297 }
7298 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007299 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007300 if( rc ) goto abort_due_to_error;
7301 }
7302
7303 /* Copy the result of the function into register P3 */
7304 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7305 sqlite3VdbeChangeEncoding(pOut, encoding);
7306 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7307 }
7308
7309 REGISTER_TRACE(pOp->p3, pOut);
7310 UPDATE_MAX_BLOBSIZE(pOut);
7311 break;
7312}
7313
drhf259df52017-12-27 20:38:35 +00007314/* Opcode: Trace P1 P2 * P4 *
7315**
7316** Write P4 on the statement trace output if statement tracing is
7317** enabled.
7318**
7319** Operand P1 must be 0x7fffffff and P2 must positive.
7320*/
drh74588ce2017-09-13 00:13:05 +00007321/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007322** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007323**
7324** Programs contain a single instance of this opcode as the very first
7325** opcode.
drh949f9cd2008-01-12 21:35:57 +00007326**
7327** If tracing is enabled (by the sqlite3_trace()) interface, then
7328** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007329** Or if P4 is blank, use the string returned by sqlite3_sql().
7330**
7331** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007332**
7333** Increment the value of P1 so that OP_Once opcodes will jump the
7334** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007335**
7336** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7337** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007338*/
drhf259df52017-12-27 20:38:35 +00007339case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007340case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007341 int i;
drhb9f47992018-01-24 12:14:43 +00007342#ifndef SQLITE_OMIT_TRACE
7343 char *zTrace;
7344#endif
drh5fe63bf2016-07-25 02:42:22 +00007345
7346 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7347 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7348 **
7349 ** This assert() provides evidence for:
7350 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7351 ** would have been returned by the legacy sqlite3_trace() interface by
7352 ** using the X argument when X begins with "--" and invoking
7353 ** sqlite3_expanded_sql(P) otherwise.
7354 */
7355 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007356
7357 /* OP_Init is always instruction 0 */
7358 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007359
drhaceb31b2014-02-08 01:40:27 +00007360#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007361 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007362 && !p->doingRerun
7363 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7364 ){
drh3d2a5292016-07-13 22:55:01 +00007365#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007366 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7367 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007368 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007369 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007370 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007371 }else
drh3d2a5292016-07-13 22:55:01 +00007372#endif
drh7adbcff2017-03-20 15:29:28 +00007373 if( db->nVdbeExec>1 ){
7374 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7375 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7376 sqlite3DbFree(db, z);
7377 }else{
drhbd441f72016-07-25 02:31:48 +00007378 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007379 }
drh949f9cd2008-01-12 21:35:57 +00007380 }
drh8f8b2312013-10-18 20:03:43 +00007381#ifdef SQLITE_USE_FCNTL_TRACE
7382 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7383 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007384 int j;
7385 for(j=0; j<db->nDb; j++){
7386 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7387 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007388 }
7389 }
7390#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007391#ifdef SQLITE_DEBUG
7392 if( (db->flags & SQLITE_SqlTrace)!=0
7393 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7394 ){
7395 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7396 }
7397#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007398#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007399 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007400 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007401 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007402 for(i=1; i<p->nOp; i++){
7403 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7404 }
7405 pOp->p1 = 0;
7406 }
7407 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007408 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007409 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007410}
drh949f9cd2008-01-12 21:35:57 +00007411
drh28935362013-12-07 20:39:19 +00007412#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007413/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007414**
7415** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007416** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7417** to values currently held in registers. TK_COLUMN terms in the P4
7418** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007419*/
7420case OP_CursorHint: {
7421 VdbeCursor *pC;
7422
7423 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7424 assert( pOp->p4type==P4_EXPR );
7425 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007426 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007427 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007428 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7429 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007430 }
drh28935362013-12-07 20:39:19 +00007431 break;
7432}
7433#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007434
drh4031baf2018-05-28 17:31:20 +00007435#ifdef SQLITE_DEBUG
7436/* Opcode: Abortable * * * * *
7437**
7438** Verify that an Abort can happen. Assert if an Abort at this point
7439** might cause database corruption. This opcode only appears in debugging
7440** builds.
7441**
7442** An Abort is safe if either there have been no writes, or if there is
7443** an active statement journal.
7444*/
7445case OP_Abortable: {
7446 sqlite3VdbeAssertAbortable(p);
7447 break;
7448}
7449#endif
7450
drh91fd4d42008-01-19 20:11:25 +00007451/* Opcode: Noop * * * * *
7452**
7453** Do nothing. This instruction is often useful as a jump
7454** destination.
drh5e00f6c2001-09-13 13:46:56 +00007455*/
drh91fd4d42008-01-19 20:11:25 +00007456/*
7457** The magic Explain opcode are only inserted when explain==2 (which
7458** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7459** This opcode records information from the optimizer. It is the
7460** the same as a no-op. This opcodesnever appears in a real VM program.
7461*/
drh4031baf2018-05-28 17:31:20 +00007462default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007463 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007464
drh5e00f6c2001-09-13 13:46:56 +00007465 break;
7466}
7467
7468/*****************************************************************************
7469** The cases of the switch statement above this line should all be indented
7470** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7471** readability. From this point on down, the normal indentation rules are
7472** restored.
7473*****************************************************************************/
7474 }
drh6e142f52000-06-08 13:36:40 +00007475
drh7b396862003-01-01 23:06:20 +00007476#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007477 {
drh35043cc2018-02-12 20:27:34 +00007478 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007479 if( endTime>start ) pOrigOp->cycles += endTime - start;
7480 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007481 }
drh7b396862003-01-01 23:06:20 +00007482#endif
7483
drh6e142f52000-06-08 13:36:40 +00007484 /* The following code adds nothing to the actual functionality
7485 ** of the program. It is only here for testing and debugging.
7486 ** On the other hand, it does burn CPU cycles every time through
7487 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7488 */
7489#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007490 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007491
drhcf1023c2007-05-08 20:59:49 +00007492#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007493 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007494 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007495 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007496 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007497 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007498 }
drh7cc84c22016-04-11 13:36:42 +00007499 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007500 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007501 }
drh75897232000-05-29 14:26:00 +00007502 }
danielk1977b5402fb2005-01-12 07:15:04 +00007503#endif /* SQLITE_DEBUG */
7504#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007505 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007506
drha05a7222008-01-19 03:35:58 +00007507 /* If we reach this point, it means that execution is finished with
7508 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007509 */
drh9467abf2016-02-17 18:44:11 +00007510abort_due_to_error:
7511 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007512 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007513 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7514 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7515 }
drha05a7222008-01-19 03:35:58 +00007516 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007517 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007518 testcase( sqlite3GlobalConfig.xLog!=0 );
7519 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007520 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007521 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007522 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007523 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007524 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007525 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007526 }
drh900b31e2007-08-28 02:27:51 +00007527
7528 /* This is the only way out of this procedure. We have to
7529 ** release the mutexes on btrees that were acquired at the
7530 ** top. */
7531vdbe_return:
drh77dfd5b2013-08-19 11:15:48 +00007532 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00007533 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007534 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007535 assert( rc!=SQLITE_OK || nExtraDelete==0
7536 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7537 );
drhb86ccfb2003-01-28 23:13:10 +00007538 return rc;
7539
drh023ae032007-05-08 12:12:16 +00007540 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7541 ** is encountered.
7542 */
7543too_big:
drh22c17b82015-05-15 04:13:15 +00007544 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007545 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007546 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007547
drh98640a32007-06-07 19:08:32 +00007548 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007549 */
7550no_mem:
drh4a642b62016-02-05 01:55:27 +00007551 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007552 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007553 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007554 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007555
danielk19776f8a5032004-05-10 10:34:51 +00007556 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007557 ** flag.
7558 */
7559abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007560 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007561 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007562 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007563 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007564 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007565}