blob: a7b346a99d4cb6707ecea90ad54ef46eea99a83a [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/*
drh299bf7c2018-06-11 17:35:02 +000040** Given a cursor number and a column for a table or index, compute a
41** hash value for use in the Mem.iTabColHash value. The iTabColHash
42** column is only used for verification - it is omitted from production
43** builds. Collisions are harmless in the sense that the correct answer
44** still results. The only harm of collisions is that they can potential
45** reduce column-cache error detection during SQLITE_DEBUG builds.
46**
47** No valid hash should be 0.
48*/
49#define TableColumnHash(T,C) (((u32)(T)<<16)^(u32)(C+2))
50
51/*
drh487ab3c2001-11-08 00:45:21 +000052** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000053** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000054** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000055** working correctly. This variable has no function other than to
56** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000057*/
drh0f7eb612006-08-08 13:51:43 +000058#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000060#endif
drh487ab3c2001-11-08 00:45:21 +000061
drhf6038712004-02-08 18:07:34 +000062/*
63** When this global variable is positive, it gets decremented once before
drhe4c88c02012-01-04 12:57:45 +000064** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
65** field of the sqlite3 structure is set in order to simulate an interrupt.
drhf6038712004-02-08 18:07:34 +000066**
67** This facility is used for testing purposes only. It does not function
68** in an ordinary build.
69*/
drh0f7eb612006-08-08 13:51:43 +000070#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000071int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000072#endif
drh1350b032002-02-27 19:00:20 +000073
danielk19777e18c252004-05-25 11:47:24 +000074/*
drh6bf89572004-11-03 16:27:01 +000075** The next global variable is incremented each type the OP_Sort opcode
76** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000077** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000078** has no function other than to help verify the correct operation of the
79** library.
80*/
drh0f7eb612006-08-08 13:51:43 +000081#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000082int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000083#endif
drh6bf89572004-11-03 16:27:01 +000084
85/*
drhae7e1512007-05-02 16:51:59 +000086** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000087** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000088** use this information to make sure that the zero-blob functionality
89** is working correctly. This variable has no function other than to
90** help verify the correct operation of the library.
91*/
92#ifdef SQLITE_TEST
93int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000094static void updateMaxBlobsize(Mem *p){
95 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
96 sqlite3_max_blobsize = p->n;
97 }
98}
drhae7e1512007-05-02 16:51:59 +000099#endif
100
101/*
drh9b1c62d2011-03-30 21:04:43 +0000102** This macro evaluates to true if either the update hook or the preupdate
103** hook are enabled for database connect DB.
104*/
105#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh74c33022016-03-30 12:56:55 +0000106# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +0000107#else
drh74c33022016-03-30 12:56:55 +0000108# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +0000109#endif
110
111/*
drh0fd61352014-02-07 02:29:45 +0000112** The next global variable is incremented each time the OP_Found opcode
dan0ff297e2009-09-25 17:03:14 +0000113** is executed. This is used to test whether or not the foreign key
114** operation implemented using OP_FkIsZero is working. This variable
115** has no function other than to help verify the correct operation of the
116** library.
117*/
118#ifdef SQLITE_TEST
119int sqlite3_found_count = 0;
120#endif
121
122/*
drhb7654112008-01-12 12:48:07 +0000123** Test a register to see if it exceeds the current maximum blob size.
124** If it does, record the new maximum blob size.
125*/
drhd12602a2016-12-07 15:49:02 +0000126#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
drhca48c902008-01-18 14:08:24 +0000127# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000128#else
129# define UPDATE_MAX_BLOBSIZE(P)
130#endif
131
132/*
drh5655c542014-02-19 19:14:34 +0000133** Invoke the VDBE coverage callback, if that callback is defined. This
134** feature is used for test suite validation only and does not appear an
135** production builds.
136**
drh7083a482018-07-10 16:04:04 +0000137** M is an integer between 2 and 4. 2 indicates a ordinary two-way
138** branch (I=0 means fall through and I=1 means taken). 3 indicates
139** a 3-way branch where the third way is when one of the operands is
140** NULL. 4 indicates the OP_Jump instruction which has three destinations
141** depending on whether the first operand is less than, equal to, or greater
142** than the second.
drh4336b0e2014-08-05 00:53:51 +0000143**
144** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000145** generated the VDBE instruction combined with flag bits. The source
146** code line number is in the lower 24 bits of iSrcLine and the upper
147** 8 bytes are flags. The lower three bits of the flags indicate
148** values for I that should never occur. For example, if the branch is
149** always taken, the flags should be 0x05 since the fall-through and
150** alternate branch are never taken. If a branch is never taken then
151** flags should be 0x06 since only the fall-through approach is allowed.
152**
153** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
154** interested in equal or not-equal. In other words, I==0 and I==2
155** should be treated the same.
156**
157** Since only a line number is retained, not the filename, this macro
158** only works for amalgamation builds. But that is ok, since these macros
159** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000160*/
161#if !defined(SQLITE_VDBE_COVERAGE)
162# define VdbeBranchTaken(I,M)
163#else
drh5655c542014-02-19 19:14:34 +0000164# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000165 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
166 u8 mNever;
167 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
168 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
169 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
170 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
171 I = 1<<I;
172 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
173 ** the flags indicate directions that the branch can never go. If
174 ** a branch really does go in one of those directions, assert right
175 ** away. */
176 mNever = iSrcLine >> 24;
177 assert( (I & mNever)==0 );
178 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
179 I |= mNever;
180 if( M==2 ) I |= 0x04;
181 if( M==4 ){
182 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000183 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000184 }
drh7083a482018-07-10 16:04:04 +0000185 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
186 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000187 }
drh688852a2014-02-17 22:40:43 +0000188#endif
189
190/*
drh9cbf3422008-01-17 16:22:13 +0000191** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000192** already. Return non-zero if a malloc() fails.
193*/
drhb21c8cd2007-08-21 19:33:56 +0000194#define Stringify(P, enc) \
drhbd9507c2014-08-23 17:21:37 +0000195 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
drhf4479502004-05-27 03:12:53 +0000196 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000197
198/*
danielk1977bd7e4602004-05-24 07:34:48 +0000199** An ephemeral string value (signified by the MEM_Ephem flag) contains
200** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000201** is responsible for deallocating that string. Because the register
202** does not control the string, it might be deleted without the register
203** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000204**
205** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000206** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000207** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000208*/
drhb21c8cd2007-08-21 19:33:56 +0000209#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000210 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000211 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000212
dan689ab892011-08-12 15:02:00 +0000213/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000214#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000215
216/*
drhdfe88ec2008-11-03 20:55:06 +0000217** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000218** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000219*/
drhdfe88ec2008-11-03 20:55:06 +0000220static VdbeCursor *allocateCursor(
221 Vdbe *p, /* The virtual machine */
222 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000223 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000224 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000225 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000226){
227 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000228 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000229 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000230 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000231 **
232 ** * Sometimes cursor numbers are used for a couple of different
233 ** purposes in a vdbe program. The different uses might require
234 ** different sized allocations. Memory cells provide growable
235 ** allocations.
236 **
237 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
238 ** be freed lazily via the sqlite3_release_memory() API. This
239 ** minimizes the number of malloc calls made by the system.
240 **
drh3cdce922016-03-21 00:30:40 +0000241 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000242 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
243 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000244 */
drh9f6168b2016-03-19 23:32:58 +0000245 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000246
danielk19775f096132008-03-28 15:44:09 +0000247 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000248 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000249 nByte =
drh5cc10232013-11-21 01:04:02 +0000250 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000251 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000252
drh9f6168b2016-03-19 23:32:58 +0000253 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000254 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977be718892006-06-23 08:05:19 +0000255 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000256 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000257 }
drh322f2852014-09-19 00:43:39 +0000258 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000259 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000260 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000261 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000262 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000263 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000264 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000265 if( eCurType==CURTYPE_BTREE ){
266 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000267 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000268 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000269 }
danielk197794eb6a12005-12-15 15:22:08 +0000270 }
drh4774b132004-06-12 20:12:51 +0000271 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000272}
273
danielk19773d1bfea2004-05-14 11:00:53 +0000274/*
drh29d72102006-02-09 22:13:41 +0000275** Try to convert a value into a numeric representation if we can
276** do so without loss of information. In other words, if the string
277** looks like a number, convert it into a number. If it does not
278** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000279**
280** If the bTryForInt flag is true, then extra effort is made to give
281** an integer representation. Strings that look like floating point
282** values but which have no fractional component (example: '48.00')
283** will have a MEM_Int representation when bTryForInt is true.
284**
285** If bTryForInt is false, then if the input string contains a decimal
286** point or exponential notation, the result is only MEM_Real, even
287** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000288*/
drhbd9507c2014-08-23 17:21:37 +0000289static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000290 double rValue;
291 i64 iValue;
292 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000293 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000294 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
295 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
296 pRec->u.i = iValue;
297 pRec->flags |= MEM_Int;
298 }else{
drh74eaba42014-09-18 17:52:15 +0000299 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000300 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000301 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000302 }
drh06b3bd52018-02-01 01:13:33 +0000303 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
304 ** string representation after computing a numeric equivalent, because the
305 ** string representation might not be the canonical representation for the
306 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
307 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000308}
309
310/*
drh8a512562005-11-14 22:29:05 +0000311** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000312**
drh8a512562005-11-14 22:29:05 +0000313** SQLITE_AFF_INTEGER:
314** SQLITE_AFF_REAL:
315** SQLITE_AFF_NUMERIC:
316** Try to convert pRec to an integer representation or a
317** floating-point representation if an integer representation
318** is not possible. Note that the integer representation is
319** always preferred, even if the affinity is REAL, because
320** an integer representation is more space efficient on disk.
321**
322** SQLITE_AFF_TEXT:
323** Convert pRec to a text representation.
324**
drh05883a32015-06-02 15:32:08 +0000325** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000326** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000327*/
drh17435752007-08-16 04:30:38 +0000328static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000329 Mem *pRec, /* The value to apply affinity to */
330 char affinity, /* The affinity to be applied */
331 u8 enc /* Use this text encoding */
332){
drh7ea31cc2014-09-18 14:36:00 +0000333 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000334 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
335 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000336 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000337 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000338 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000339 }else{
340 sqlite3VdbeIntegerAffinity(pRec);
341 }
drh17c40292004-07-21 02:53:29 +0000342 }
drh7ea31cc2014-09-18 14:36:00 +0000343 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000344 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000345 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000346 ** representation. It would be harmless to repeat the conversion if
347 ** there is already a string rep, but it is pointless to waste those
348 ** CPU cycles. */
349 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
350 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
351 sqlite3VdbeMemStringify(pRec, enc, 1);
352 }
danielk19773d1bfea2004-05-14 11:00:53 +0000353 }
dandde548c2015-05-19 19:44:25 +0000354 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000355 }
356}
357
danielk1977aee18ef2005-03-09 12:26:50 +0000358/*
drh29d72102006-02-09 22:13:41 +0000359** Try to convert the type of a function argument or a result column
360** into a numeric representation. Use either INTEGER or REAL whichever
361** is appropriate. But only do the conversion if it is possible without
362** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000363*/
364int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000365 int eType = sqlite3_value_type(pVal);
366 if( eType==SQLITE_TEXT ){
367 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000368 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000369 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000370 }
drh1b27b8c2014-02-10 03:21:57 +0000371 return eType;
drh29d72102006-02-09 22:13:41 +0000372}
373
374/*
danielk1977aee18ef2005-03-09 12:26:50 +0000375** Exported version of applyAffinity(). This one works on sqlite3_value*,
376** not the internal Mem* type.
377*/
danielk19771e536952007-08-16 10:09:01 +0000378void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000379 sqlite3_value *pVal,
380 u8 affinity,
381 u8 enc
382){
drhb21c8cd2007-08-21 19:33:56 +0000383 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000384}
385
drh3d1d90a2014-03-24 15:00:15 +0000386/*
drhf1a89ed2014-08-23 17:41:15 +0000387** pMem currently only holds a string type (or maybe a BLOB that we can
388** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000389** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000390** accordingly.
391*/
392static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
393 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
394 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000395 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000396 return 0;
397 }
drh84d4f1a2017-09-20 10:47:10 +0000398 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000399 return MEM_Int;
400 }
401 return MEM_Real;
402}
403
404/*
drh3d1d90a2014-03-24 15:00:15 +0000405** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
406** none.
407**
408** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000409** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000410*/
411static u16 numericType(Mem *pMem){
412 if( pMem->flags & (MEM_Int|MEM_Real) ){
413 return pMem->flags & (MEM_Int|MEM_Real);
414 }
415 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000416 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000417 }
418 return 0;
419}
420
danielk1977b5402fb2005-01-12 07:15:04 +0000421#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000422/*
danielk1977ca6b2912004-05-21 10:49:47 +0000423** Write a nice string representation of the contents of cell pMem
424** into buffer zBuf, length nBuf.
425*/
drh74161702006-02-24 02:53:49 +0000426void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000427 char *zCsr = zBuf;
428 int f = pMem->flags;
429
drh57196282004-10-06 15:41:16 +0000430 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000431
danielk1977ca6b2912004-05-21 10:49:47 +0000432 if( f&MEM_Blob ){
433 int i;
434 char c;
435 if( f & MEM_Dyn ){
436 c = 'z';
437 assert( (f & (MEM_Static|MEM_Ephem))==0 );
438 }else if( f & MEM_Static ){
439 c = 't';
440 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
441 }else if( f & MEM_Ephem ){
442 c = 'e';
443 assert( (f & (MEM_Static|MEM_Dyn))==0 );
444 }else{
445 c = 's';
446 }
drh85c2dc02017-03-16 13:30:58 +0000447 *(zCsr++) = c;
drh5bb3eb92007-05-04 13:15:55 +0000448 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000449 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000450 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000451 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000452 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000453 }
454 for(i=0; i<16 && i<pMem->n; i++){
455 char z = pMem->z[i];
456 if( z<32 || z>126 ) *zCsr++ = '.';
457 else *zCsr++ = z;
458 }
drh85c2dc02017-03-16 13:30:58 +0000459 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000460 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000461 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000462 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000463 }
danielk1977b1bc9532004-05-22 03:05:33 +0000464 *zCsr = '\0';
465 }else if( f & MEM_Str ){
466 int j, k;
467 zBuf[0] = ' ';
468 if( f & MEM_Dyn ){
469 zBuf[1] = 'z';
470 assert( (f & (MEM_Static|MEM_Ephem))==0 );
471 }else if( f & MEM_Static ){
472 zBuf[1] = 't';
473 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
474 }else if( f & MEM_Ephem ){
475 zBuf[1] = 'e';
476 assert( (f & (MEM_Static|MEM_Dyn))==0 );
477 }else{
478 zBuf[1] = 's';
479 }
480 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000481 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000482 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000483 zBuf[k++] = '[';
484 for(j=0; j<15 && j<pMem->n; j++){
485 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000486 if( c>=0x20 && c<0x7f ){
487 zBuf[k++] = c;
488 }else{
489 zBuf[k++] = '.';
490 }
491 }
492 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000493 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000494 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000495 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000496 }
danielk1977ca6b2912004-05-21 10:49:47 +0000497}
498#endif
499
drh5b6afba2008-01-05 16:29:28 +0000500#ifdef SQLITE_DEBUG
501/*
502** Print the value of a register for tracing purposes:
503*/
drh84e55a82013-11-13 17:58:23 +0000504static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000505 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000506 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000507 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000508 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000509 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000510 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000511 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000512 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000513#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000514 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000515 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000516#endif
drh733bf1b2009-04-22 00:47:00 +0000517 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000518 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000519 }else{
520 char zBuf[200];
521 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000522 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000523 }
dan5b6c8e42016-01-30 15:46:03 +0000524 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000525}
drh84e55a82013-11-13 17:58:23 +0000526static void registerTrace(int iReg, Mem *p){
527 printf("REG[%d] = ", iReg);
528 memTracePrint(p);
529 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000530 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000531}
532#endif
533
534#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000535# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000536#else
537# define REGISTER_TRACE(R,M)
538#endif
539
danielk197784ac9d02004-05-18 09:58:06 +0000540
drh7b396862003-01-01 23:06:20 +0000541#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000542
543/*
544** hwtime.h contains inline assembler code for implementing
545** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000546*/
shane9bcbdad2008-05-29 20:22:37 +0000547#include "hwtime.h"
548
drh7b396862003-01-01 23:06:20 +0000549#endif
550
danielk1977fd7f0452008-12-17 17:30:26 +0000551#ifndef NDEBUG
552/*
553** This function is only called from within an assert() expression. It
554** checks that the sqlite3.nTransaction variable is correctly set to
555** the number of non-transaction savepoints currently in the
556** linked list starting at sqlite3.pSavepoint.
557**
558** Usage:
559**
560** assert( checkSavepointCount(db) );
561*/
562static int checkSavepointCount(sqlite3 *db){
563 int n = 0;
564 Savepoint *p;
565 for(p=db->pSavepoint; p; p=p->pNext) n++;
566 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
567 return 1;
568}
569#endif
570
drh27a348c2015-04-13 19:14:06 +0000571/*
572** Return the register of pOp->p2 after first preparing it to be
573** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000574*/
575static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
576 sqlite3VdbeMemSetNull(pOut);
577 pOut->flags = MEM_Int;
578 return pOut;
579}
drh27a348c2015-04-13 19:14:06 +0000580static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
581 Mem *pOut;
582 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000583 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000584 pOut = &p->aMem[pOp->p2];
585 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000586 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000587 return out2PrereleaseWithClear(pOut);
588 }else{
589 pOut->flags = MEM_Int;
590 return pOut;
591 }
drh27a348c2015-04-13 19:14:06 +0000592}
593
drhb9755982010-07-24 16:34:37 +0000594
595/*
drh0fd61352014-02-07 02:29:45 +0000596** Execute as much of a VDBE program as we can.
597** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000598*/
danielk19774adee202004-05-08 08:23:19 +0000599int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000600 Vdbe *p /* The VDBE */
601){
drhbbe879d2009-11-14 18:04:35 +0000602 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000603 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000604#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
605 Op *pOrigOp; /* Value of pOp at the top of the loop */
606#endif
drhb89aeb62016-01-27 15:49:32 +0000607#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000608 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000609#endif
drhb86ccfb2003-01-28 23:13:10 +0000610 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000611 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000612 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000613 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000614 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000615 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000616#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000617 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000618#endif
drha6c2ed92009-11-14 23:22:23 +0000619 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000620 Mem *pIn1 = 0; /* 1st input operand */
621 Mem *pIn2 = 0; /* 2nd input operand */
622 Mem *pIn3 = 0; /* 3rd input operand */
623 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000624#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000625 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000626#endif
drh856c1032009-06-02 15:21:42 +0000627 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000628
drhca48c902008-01-18 14:08:24 +0000629 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000630 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000631 if( p->rc==SQLITE_NOMEM ){
632 /* This happens if a malloc() inside a call to sqlite3_column_text() or
633 ** sqlite3_column_text16() failed. */
634 goto no_mem;
635 }
drhcbd8db32015-08-20 17:18:32 +0000636 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000637 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000638 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000639 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000640 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000641 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000642 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000643 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000644#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
645 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000646 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000647 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000648 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh2ab792e2017-05-30 18:34:07 +0000649 }else{
650 nProgressLimit = 0xffffffff;
drh0d1961e2013-07-25 16:27:51 +0000651 }
652#endif
drh3c23a882007-01-09 14:01:13 +0000653#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000654 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000655 if( p->pc==0
656 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
657 ){
drh3c23a882007-01-09 14:01:13 +0000658 int i;
drh84e55a82013-11-13 17:58:23 +0000659 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000660 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000661 if( p->db->flags & SQLITE_VdbeListing ){
662 printf("VDBE Program Listing:\n");
663 for(i=0; i<p->nOp; i++){
664 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
665 }
drh3c23a882007-01-09 14:01:13 +0000666 }
drh84e55a82013-11-13 17:58:23 +0000667 if( p->db->flags & SQLITE_VdbeEQP ){
668 for(i=0; i<p->nOp; i++){
669 if( aOp[i].opcode==OP_Explain ){
670 if( once ) printf("VDBE Query Plan:\n");
671 printf("%s\n", aOp[i].p4.z);
672 once = 0;
673 }
674 }
675 }
676 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000677 }
danielk19772d1d86f2008-06-20 14:59:51 +0000678 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000679#endif
drh9467abf2016-02-17 18:44:11 +0000680 for(pOp=&aOp[p->pc]; 1; pOp++){
681 /* Errors are detected by individual opcodes, with an immediate
682 ** jumps to abort_due_to_error. */
683 assert( rc==SQLITE_OK );
684
drhf56fa462015-04-13 21:39:54 +0000685 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000686#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000687 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000688#endif
drhbf159fa2013-06-25 22:01:22 +0000689 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000690#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000691 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000692#endif
drh6e142f52000-06-08 13:36:40 +0000693
danielk19778b60e0f2005-01-12 09:10:39 +0000694 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000695 */
danielk19778b60e0f2005-01-12 09:10:39 +0000696#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000697 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000698 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000699 }
drh3f7d4e42004-07-24 14:35:58 +0000700#endif
701
drh6e142f52000-06-08 13:36:40 +0000702
drhf6038712004-02-08 18:07:34 +0000703 /* Check to see if we need to simulate an interrupt. This only happens
704 ** if we have a special test build.
705 */
706#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000707 if( sqlite3_interrupt_count>0 ){
708 sqlite3_interrupt_count--;
709 if( sqlite3_interrupt_count==0 ){
710 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000711 }
712 }
713#endif
714
drh3c657212009-11-17 23:59:58 +0000715 /* Sanity checking on other operands */
716#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000717 {
718 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
719 if( (opProperty & OPFLG_IN1)!=0 ){
720 assert( pOp->p1>0 );
721 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
722 assert( memIsValid(&aMem[pOp->p1]) );
723 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
724 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
725 }
726 if( (opProperty & OPFLG_IN2)!=0 ){
727 assert( pOp->p2>0 );
728 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
729 assert( memIsValid(&aMem[pOp->p2]) );
730 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
731 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
732 }
733 if( (opProperty & OPFLG_IN3)!=0 ){
734 assert( pOp->p3>0 );
735 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
736 assert( memIsValid(&aMem[pOp->p3]) );
737 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
738 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
739 }
740 if( (opProperty & OPFLG_OUT2)!=0 ){
741 assert( pOp->p2>0 );
742 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
743 memAboutToChange(p, &aMem[pOp->p2]);
744 }
745 if( (opProperty & OPFLG_OUT3)!=0 ){
746 assert( pOp->p3>0 );
747 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
748 memAboutToChange(p, &aMem[pOp->p3]);
749 }
drh3c657212009-11-17 23:59:58 +0000750 }
751#endif
drh6dc41482015-04-16 17:31:02 +0000752#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
753 pOrigOp = pOp;
754#endif
drh93952eb2009-11-13 19:43:43 +0000755
drh75897232000-05-29 14:26:00 +0000756 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000757
drh5e00f6c2001-09-13 13:46:56 +0000758/*****************************************************************************
759** What follows is a massive switch statement where each case implements a
760** separate instruction in the virtual machine. If we follow the usual
761** indentation conventions, each case should be indented by 6 spaces. But
762** that is a lot of wasted space on the left margin. So the code within
763** the switch statement will break with convention and be flush-left. Another
764** big comment (similar to this one) will mark the point in the code where
765** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000766**
767** The formatting of each case is important. The makefile for SQLite
768** generates two C files "opcodes.h" and "opcodes.c" by scanning this
769** file looking for lines that begin with "case OP_". The opcodes.h files
770** will be filled with #defines that give unique integer values to each
771** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000772** each string is the symbolic name for the corresponding opcode. If the
773** case statement is followed by a comment of the form "/# same as ... #/"
774** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000775**
drh9cbf3422008-01-17 16:22:13 +0000776** Other keywords in the comment that follows each case are used to
777** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000778** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000779** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000780**
drhac82fcf2002-09-08 17:23:41 +0000781** Documentation about VDBE opcodes is generated by scanning this file
782** for lines of that contain "Opcode:". That line and all subsequent
783** comment lines are used in the generation of the opcode.html documentation
784** file.
785**
786** SUMMARY:
787**
788** Formatting is important to scripts that scan this file.
789** Do not deviate from the formatting style currently in use.
790**
drh5e00f6c2001-09-13 13:46:56 +0000791*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000792
drh9cbf3422008-01-17 16:22:13 +0000793/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000794**
795** An unconditional jump to address P2.
796** The next instruction executed will be
797** the one at index P2 from the beginning of
798** the program.
drhfe705102014-03-06 13:38:37 +0000799**
800** The P1 parameter is not actually used by this opcode. However, it
801** is sometimes set to 1 instead of 0 as a hint to the command-line shell
802** that this Goto is the bottom of a loop and that the lines from P2 down
803** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000804*/
drh9cbf3422008-01-17 16:22:13 +0000805case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000806jump_to_p2_and_check_for_interrupt:
807 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000808
809 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000810 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000811 ** completion. Check to see if sqlite3_interrupt() has been called
812 ** or if the progress callback needs to be invoked.
813 **
814 ** This code uses unstructured "goto" statements and does not look clean.
815 ** But that is not due to sloppy coding habits. The code is written this
816 ** way for performance, to avoid having to run the interrupt and progress
817 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
818 ** faster according to "valgrind --tool=cachegrind" */
819check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000820 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000821#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
822 /* Call the progress callback if it is configured and the required number
823 ** of VDBE ops have been executed (either since this invocation of
824 ** sqlite3VdbeExec() or since last time the progress callback was called).
825 ** If the progress callback returns non-zero, exit the virtual machine with
826 ** a return code SQLITE_ABORT.
827 */
drh2ab792e2017-05-30 18:34:07 +0000828 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000829 assert( db->nProgressOps!=0 );
830 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
831 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000832 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000833 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000834 }
drh49afe3a2013-07-10 03:05:14 +0000835 }
836#endif
837
drh5e00f6c2001-09-13 13:46:56 +0000838 break;
839}
drh75897232000-05-29 14:26:00 +0000840
drh2eb95372008-06-06 15:04:36 +0000841/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000842**
drh2eb95372008-06-06 15:04:36 +0000843** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000844** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000845*/
drhb8475df2011-12-09 16:21:19 +0000846case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000847 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000848 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000849 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000850 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000851 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000852 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000853 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000854
855 /* Most jump operations do a goto to this spot in order to update
856 ** the pOp pointer. */
857jump_to_p2:
858 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000859 break;
860}
861
drh2eb95372008-06-06 15:04:36 +0000862/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000863**
drh81cf13e2014-02-07 18:27:53 +0000864** Jump to the next instruction after the address in register P1. After
865** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000866*/
drh2eb95372008-06-06 15:04:36 +0000867case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000868 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000869 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000870 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000871 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000872 break;
873}
874
drhed71a832014-02-07 19:18:10 +0000875/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000876**
drh5dad9a32014-07-25 18:37:42 +0000877** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000878** located at address P3.
879**
drh5dad9a32014-07-25 18:37:42 +0000880** If P2!=0 then the coroutine implementation immediately follows
881** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000882** address P2.
drh5dad9a32014-07-25 18:37:42 +0000883**
884** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000885*/
886case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000887 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000888 assert( pOp->p2>=0 && pOp->p2<p->nOp );
889 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000890 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000891 assert( !VdbeMemDynamic(pOut) );
892 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000893 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000894 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000895 break;
896}
897
898/* Opcode: EndCoroutine P1 * * * *
899**
drhbc5cf382014-08-06 01:08:07 +0000900** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000901** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000902** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000903**
904** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000905*/
906case OP_EndCoroutine: { /* in1 */
907 VdbeOp *pCaller;
908 pIn1 = &aMem[pOp->p1];
909 assert( pIn1->flags==MEM_Int );
910 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
911 pCaller = &aOp[pIn1->u.i];
912 assert( pCaller->opcode==OP_Yield );
913 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000914 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000915 pIn1->flags = MEM_Undefined;
916 break;
917}
918
919/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000920**
drh5dad9a32014-07-25 18:37:42 +0000921** Swap the program counter with the value in register P1. This
922** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000923**
drh5dad9a32014-07-25 18:37:42 +0000924** If the coroutine that is launched by this instruction ends with
925** Yield or Return then continue to the next instruction. But if
926** the coroutine launched by this instruction ends with
927** EndCoroutine, then jump to P2 rather than continuing with the
928** next instruction.
929**
930** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000931*/
drh81cf13e2014-02-07 18:27:53 +0000932case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000933 int pcDest;
drh3c657212009-11-17 23:59:58 +0000934 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000935 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000936 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000937 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000938 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000939 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000940 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000941 break;
942}
943
drhf9c8ce32013-11-05 13:33:55 +0000944/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000945** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000946**
drhef8662b2011-06-20 21:47:58 +0000947** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000948** parameter P1, P2, and P4 as if this were a Halt instruction. If the
949** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000950** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000951*/
952case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000953 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +0000954#ifdef SQLITE_DEBUG
955 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
956#endif
drh5053a792009-02-20 03:02:23 +0000957 if( (pIn3->flags & MEM_Null)==0 ) break;
958 /* Fall through into OP_Halt */
959}
drhe00ee6e2008-06-20 15:24:01 +0000960
drhf9c8ce32013-11-05 13:33:55 +0000961/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000962**
drh3d4501e2008-12-04 20:40:10 +0000963** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000964** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000965**
drh92f02c32004-09-02 14:57:08 +0000966** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
967** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
968** For errors, it can be some other value. If P1!=0 then P2 will determine
969** whether or not to rollback the current transaction. Do not rollback
970** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
971** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000972** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000973**
drh66a51672008-01-03 00:01:23 +0000974** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000975**
drhf9c8ce32013-11-05 13:33:55 +0000976** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
977**
978** 0: (no change)
979** 1: NOT NULL contraint failed: P4
980** 2: UNIQUE constraint failed: P4
981** 3: CHECK constraint failed: P4
982** 4: FOREIGN KEY constraint failed: P4
983**
984** If P5 is not zero and P4 is NULL, then everything after the ":" is
985** omitted.
986**
drh9cfcf5d2002-01-29 18:41:24 +0000987** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000988** every program. So a jump past the last instruction of the program
989** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000990*/
drh9cbf3422008-01-17 16:22:13 +0000991case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +0000992 VdbeFrame *pFrame;
993 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000994
drhf56fa462015-04-13 21:39:54 +0000995 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +0000996#ifdef SQLITE_DEBUG
997 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
998#endif
dan165921a2009-08-28 18:53:45 +0000999 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +00001000 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001001 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001002 p->pFrame = pFrame->pParent;
1003 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001004 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001005 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001006 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001007 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001008 ** currently being halted. If the p2 instruction of this OP_Halt
1009 ** instruction is set to OE_Ignore, then the sub-program is throwing
1010 ** an IGNORE exception. In this case jump to the address specified
1011 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001012 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001013 }
drhbbe879d2009-11-14 18:04:35 +00001014 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001015 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001016 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001017 break;
1018 }
drh92f02c32004-09-02 14:57:08 +00001019 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001020 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +00001021 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +00001022 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001023 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001024 if( pOp->p5 ){
1025 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1026 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001027 testcase( pOp->p5==1 );
1028 testcase( pOp->p5==2 );
1029 testcase( pOp->p5==3 );
1030 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001031 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1032 if( pOp->p4.z ){
1033 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1034 }
drhd9b7ec92013-11-06 14:05:21 +00001035 }else{
drh22c17b82015-05-15 04:13:15 +00001036 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001037 }
drh99f5de72016-04-30 02:59:15 +00001038 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001039 }
drh92f02c32004-09-02 14:57:08 +00001040 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001041 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001042 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001043 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001044 }else{
drhd91c1a12013-02-09 13:58:25 +00001045 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001046 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001047 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001048 }
drh900b31e2007-08-28 02:27:51 +00001049 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001050}
drhc61053b2000-06-04 12:58:36 +00001051
drh4c583122008-01-04 22:01:03 +00001052/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001053** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001054**
drh9cbf3422008-01-17 16:22:13 +00001055** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001056*/
drh27a348c2015-04-13 19:14:06 +00001057case OP_Integer: { /* out2 */
1058 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001059 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001060 break;
1061}
1062
drh4c583122008-01-04 22:01:03 +00001063/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001064** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001065**
drh66a51672008-01-03 00:01:23 +00001066** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001067** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001068*/
drh27a348c2015-04-13 19:14:06 +00001069case OP_Int64: { /* out2 */
1070 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001071 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001072 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001073 break;
1074}
drh4f26d6c2004-05-26 23:25:30 +00001075
drh13573c72010-01-12 17:04:07 +00001076#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001077/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001078** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001079**
drh4c583122008-01-04 22:01:03 +00001080** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001081** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001082*/
drh27a348c2015-04-13 19:14:06 +00001083case OP_Real: { /* same as TK_FLOAT, out2 */
1084 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001085 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001086 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001087 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001088 break;
1089}
drh13573c72010-01-12 17:04:07 +00001090#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001091
drh3c84ddf2008-01-09 02:15:38 +00001092/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001093** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001094**
drh66a51672008-01-03 00:01:23 +00001095** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001096** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001097** this transformation, the length of string P4 is computed and stored
1098** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001099*/
drh27a348c2015-04-13 19:14:06 +00001100case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001101 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001102 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001103 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001104 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001105
1106#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001107 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001108 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001109 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drh4c583122008-01-04 22:01:03 +00001110 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001111 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001112 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001113 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001114 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001115 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001116 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001117 }
drh66a51672008-01-03 00:01:23 +00001118 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001119 pOp->p4.z = pOut->z;
1120 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001121 }
drh2f555112016-04-30 18:10:34 +00001122 testcase( rc==SQLITE_TOOBIG );
danielk197793758c82005-01-21 08:13:14 +00001123#endif
drhbb4957f2008-03-20 14:03:29 +00001124 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001125 goto too_big;
1126 }
drh2f555112016-04-30 18:10:34 +00001127 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001128 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001129}
drhf4479502004-05-27 03:12:53 +00001130
drhf07cf6e2015-03-06 16:45:16 +00001131/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001132** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001133**
drh9cbf3422008-01-17 16:22:13 +00001134** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001135**
drh44aebff2016-05-02 10:25:42 +00001136** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001137** the datatype of the register P2 is converted to BLOB. The content is
1138** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001139** of a string, as if it had been CAST. In other words:
1140**
1141** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001142*/
drh27a348c2015-04-13 19:14:06 +00001143case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001144 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001145 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001146 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1147 pOut->z = pOp->p4.z;
1148 pOut->n = pOp->p1;
1149 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001150 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001151#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001152 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001153 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001154 pIn3 = &aMem[pOp->p3];
1155 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001156 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001157 }
drh41d2e662015-12-01 21:23:07 +00001158#endif
danielk1977c572ef72004-05-27 09:28:41 +00001159 break;
1160}
1161
drh053a1282012-09-19 21:15:46 +00001162/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001163** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001164**
drhb8475df2011-12-09 16:21:19 +00001165** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001166** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001167** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001168** set to NULL.
1169**
1170** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1171** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1172** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001173*/
drh27a348c2015-04-13 19:14:06 +00001174case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001175 int cnt;
drh053a1282012-09-19 21:15:46 +00001176 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001177 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001178 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001179 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001180 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001181 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001182#ifdef SQLITE_DEBUG
1183 pOut->uTemp = 0;
1184#endif
drhb8475df2011-12-09 16:21:19 +00001185 while( cnt>0 ){
1186 pOut++;
1187 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001188 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001189 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001190 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001191 cnt--;
1192 }
drhf0863fe2005-06-12 21:35:51 +00001193 break;
1194}
1195
drh05a86c52014-02-16 01:55:49 +00001196/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001197** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001198**
1199** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1200** instruction, but do not free any string or blob memory associated with
1201** the register, so that if the value was a string or blob that was
1202** previously copied using OP_SCopy, the copies will continue to be valid.
1203*/
1204case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001205 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001206 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001207 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001208 break;
1209}
drhf0863fe2005-06-12 21:35:51 +00001210
drha5750cf2014-02-07 13:20:31 +00001211/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001212** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001213**
drh9de221d2008-01-05 06:51:30 +00001214** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001215** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001216*/
drh27a348c2015-04-13 19:14:06 +00001217case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001218 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001219 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001220 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001221 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001222 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001223 break;
1224}
1225
drheaf52d82010-05-12 13:50:23 +00001226/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001227** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001228**
drheaf52d82010-05-12 13:50:23 +00001229** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001230**
drh0fd61352014-02-07 02:29:45 +00001231** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001232** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001233*/
drh27a348c2015-04-13 19:14:06 +00001234case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001235 Mem *pVar; /* Value being transferred */
1236
drheaf52d82010-05-12 13:50:23 +00001237 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001238 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001239 pVar = &p->aVar[pOp->p1 - 1];
1240 if( sqlite3VdbeMemTooBig(pVar) ){
1241 goto too_big;
drh023ae032007-05-08 12:12:16 +00001242 }
drh7441df72017-01-09 19:27:04 +00001243 pOut = &aMem[pOp->p2];
drheaf52d82010-05-12 13:50:23 +00001244 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1245 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001246 break;
1247}
danielk1977295ba552004-05-19 10:34:51 +00001248
drhb21e7c72008-06-22 12:37:57 +00001249/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001250** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001251**
drh079a3072014-03-19 14:10:55 +00001252** Move the P3 values in register P1..P1+P3-1 over into
1253** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001254** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001255** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1256** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001257*/
drhe1349cb2008-04-01 00:36:10 +00001258case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001259 int n; /* Number of registers left to copy */
1260 int p1; /* Register to copy from */
1261 int p2; /* Register to copy to */
1262
drhe09f43f2013-11-21 04:18:31 +00001263 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001264 p1 = pOp->p1;
1265 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001266 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001267 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001268
drha6c2ed92009-11-14 23:22:23 +00001269 pIn1 = &aMem[p1];
1270 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001271 do{
drh9f6168b2016-03-19 23:32:58 +00001272 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1273 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001274 assert( memIsValid(pIn1) );
1275 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001276 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001277#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001278 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001279 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001280 }
1281#endif
drhbd6789e2015-04-28 14:00:02 +00001282 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001283 REGISTER_TRACE(p2++, pOut);
1284 pIn1++;
1285 pOut++;
drh079a3072014-03-19 14:10:55 +00001286 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001287 break;
1288}
1289
drhe8e4af72012-09-21 00:04:28 +00001290/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001291** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001292**
drhe8e4af72012-09-21 00:04:28 +00001293** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001294**
1295** This instruction makes a deep copy of the value. A duplicate
1296** is made of any string or blob constant. See also OP_SCopy.
1297*/
drhe8e4af72012-09-21 00:04:28 +00001298case OP_Copy: {
1299 int n;
1300
1301 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001302 pIn1 = &aMem[pOp->p1];
1303 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001304 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001305 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001306 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001307 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1308 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001309#ifdef SQLITE_DEBUG
1310 pOut->pScopyFrom = 0;
drh58773a52018-06-12 13:52:23 +00001311 pOut->iTabColHash = 0;
drh953f7612012-12-07 22:18:54 +00001312#endif
drhe8e4af72012-09-21 00:04:28 +00001313 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1314 if( (n--)==0 ) break;
1315 pOut++;
1316 pIn1++;
1317 }
drhe1349cb2008-04-01 00:36:10 +00001318 break;
1319}
1320
drhb1fdb2a2008-01-05 04:06:03 +00001321/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001322** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001323**
drh9cbf3422008-01-17 16:22:13 +00001324** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001325**
1326** This instruction makes a shallow copy of the value. If the value
1327** is a string or blob, then the copy is only a pointer to the
1328** original and hence if the original changes so will the copy.
1329** Worse, if the original is deallocated, the copy becomes invalid.
1330** Thus the program must guarantee that the original will not change
1331** during the lifetime of the copy. Use OP_Copy to make a complete
1332** copy.
1333*/
drh26198bb2013-10-31 11:15:09 +00001334case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001335 pIn1 = &aMem[pOp->p1];
1336 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001337 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001338 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001339#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001340 pOut->pScopyFrom = pIn1;
1341 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001342#endif
drh5e00f6c2001-09-13 13:46:56 +00001343 break;
1344}
drh75897232000-05-29 14:26:00 +00001345
drhfed7ac62015-10-15 18:04:59 +00001346/* Opcode: IntCopy P1 P2 * * *
1347** Synopsis: r[P2]=r[P1]
1348**
1349** Transfer the integer value held in register P1 into register P2.
1350**
1351** This is an optimized version of SCopy that works only for integer
1352** values.
1353*/
1354case OP_IntCopy: { /* out2 */
1355 pIn1 = &aMem[pOp->p1];
1356 assert( (pIn1->flags & MEM_Int)!=0 );
1357 pOut = &aMem[pOp->p2];
1358 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1359 break;
1360}
1361
drh9cbf3422008-01-17 16:22:13 +00001362/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001363** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001364**
shane21e7feb2008-05-30 15:59:49 +00001365** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001366** results. This opcode causes the sqlite3_step() call to terminate
1367** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001368** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001369** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001370*/
drh9cbf3422008-01-17 16:22:13 +00001371case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001372 Mem *pMem;
1373 int i;
1374 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001375 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001376 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001377
drhe6400b92013-11-13 23:48:46 +00001378#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1379 /* Run the progress counter just before returning.
1380 */
1381 if( db->xProgress!=0
drh2ab792e2017-05-30 18:34:07 +00001382 && nVmStep>=nProgressLimit
drhe6400b92013-11-13 23:48:46 +00001383 && db->xProgress(db->pProgressArg)!=0
1384 ){
1385 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001386 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001387 }
1388#endif
1389
dan32b09f22009-09-23 17:29:59 +00001390 /* If this statement has violated immediate foreign key constraints, do
1391 ** not return the number of rows modified. And do not RELEASE the statement
1392 ** transaction. It needs to be rolled back. */
1393 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1394 assert( db->flags&SQLITE_CountRows );
1395 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001396 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001397 }
1398
danielk1977bd434552009-03-18 10:33:00 +00001399 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1400 ** DML statements invoke this opcode to return the number of rows
1401 ** modified to the user. This is the only way that a VM that
1402 ** opens a statement transaction may invoke this opcode.
1403 **
1404 ** In case this is such a statement, close any statement transaction
1405 ** opened by this VM before returning control to the user. This is to
1406 ** ensure that statement-transactions are always nested, not overlapping.
1407 ** If the open statement-transaction is not closed here, then the user
1408 ** may step another VM that opens its own statement transaction. This
1409 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001410 **
1411 ** The statement transaction is never a top-level transaction. Hence
1412 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001413 */
1414 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001415 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001416 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001417
drhd4e70eb2008-01-02 00:34:36 +00001418 /* Invalidate all ephemeral cursor row caches */
1419 p->cacheCtr = (p->cacheCtr + 2)|1;
1420
1421 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001422 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001423 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001424 */
drha6c2ed92009-11-14 23:22:23 +00001425 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001426 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001427 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001428 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001429 assert( (pMem[i].flags & MEM_Ephem)==0
1430 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001431 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001432 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001433 }
drh28039692008-03-17 16:54:01 +00001434 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001435
drh3d2a5292016-07-13 22:55:01 +00001436 if( db->mTrace & SQLITE_TRACE_ROW ){
1437 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1438 }
1439
drhd4e70eb2008-01-02 00:34:36 +00001440 /* Return SQLITE_ROW
1441 */
drhf56fa462015-04-13 21:39:54 +00001442 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001443 rc = SQLITE_ROW;
1444 goto vdbe_return;
1445}
1446
drh5b6afba2008-01-05 16:29:28 +00001447/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001448** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001449**
drh5b6afba2008-01-05 16:29:28 +00001450** Add the text in register P1 onto the end of the text in
1451** register P2 and store the result in register P3.
1452** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001453**
1454** P3 = P2 || P1
1455**
1456** It is illegal for P1 and P3 to be the same register. Sometimes,
1457** if P3 is the same register as P2, the implementation is able
1458** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001459*/
drh5b6afba2008-01-05 16:29:28 +00001460case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001461 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001462
drh3c657212009-11-17 23:59:58 +00001463 pIn1 = &aMem[pOp->p1];
1464 pIn2 = &aMem[pOp->p2];
1465 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001466 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001467 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001468 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001469 break;
drh5e00f6c2001-09-13 13:46:56 +00001470 }
drha0c06522009-06-17 22:50:41 +00001471 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001472 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001473 Stringify(pIn2, encoding);
1474 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001475 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001476 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001477 }
drh9c1905f2008-12-10 22:32:56 +00001478 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001479 goto no_mem;
1480 }
drhc91b2fd2014-03-01 18:13:23 +00001481 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001482 if( pOut!=pIn2 ){
1483 memcpy(pOut->z, pIn2->z, pIn2->n);
1484 }
1485 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001486 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001487 pOut->z[nByte+1] = 0;
1488 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001489 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001490 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001491 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001492 break;
1493}
drh75897232000-05-29 14:26:00 +00001494
drh3c84ddf2008-01-09 02:15:38 +00001495/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001496** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001497**
drh60a713c2008-01-21 16:22:45 +00001498** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001499** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001500** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001501*/
drh3c84ddf2008-01-09 02:15:38 +00001502/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001503** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001504**
drh3c84ddf2008-01-09 02:15:38 +00001505**
shane21e7feb2008-05-30 15:59:49 +00001506** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001507** and store the result in register P3.
1508** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001509*/
drh3c84ddf2008-01-09 02:15:38 +00001510/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001511** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001512**
drh60a713c2008-01-21 16:22:45 +00001513** Subtract the value in register P1 from the value in register P2
1514** and store the result in register P3.
1515** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001516*/
drh9cbf3422008-01-17 16:22:13 +00001517/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001518** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001519**
drh60a713c2008-01-21 16:22:45 +00001520** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001521** and store the result in register P3 (P3=P2/P1). If the value in
1522** register P1 is zero, then the result is NULL. If either input is
1523** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001524*/
drh9cbf3422008-01-17 16:22:13 +00001525/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001526** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001527**
drh40864a12013-11-15 18:58:37 +00001528** Compute the remainder after integer register P2 is divided by
1529** register P1 and store the result in register P3.
1530** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001531** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001532*/
drh5b6afba2008-01-05 16:29:28 +00001533case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1534case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1535case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1536case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1537case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001538 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001539 u16 flags; /* Combined MEM_* flags from both inputs */
1540 u16 type1; /* Numeric type of left operand */
1541 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001542 i64 iA; /* Integer value of left operand */
1543 i64 iB; /* Integer value of right operand */
1544 double rA; /* Real value of left operand */
1545 double rB; /* Real value of right operand */
1546
drh3c657212009-11-17 23:59:58 +00001547 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001548 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001549 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001550 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001551 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001552 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001553 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001554 iA = pIn1->u.i;
1555 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001556 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001557 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001558 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1559 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1560 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001561 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001562 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001563 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001564 iB /= iA;
drh75897232000-05-29 14:26:00 +00001565 break;
1566 }
drhbf4133c2001-10-13 02:59:08 +00001567 default: {
drh856c1032009-06-02 15:21:42 +00001568 if( iA==0 ) goto arithmetic_result_is_null;
1569 if( iA==-1 ) iA = 1;
1570 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001571 break;
1572 }
drh75897232000-05-29 14:26:00 +00001573 }
drh856c1032009-06-02 15:21:42 +00001574 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001575 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001576 }else if( (flags & MEM_Null)!=0 ){
1577 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001578 }else{
drhbe707b32012-12-10 22:19:14 +00001579 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001580fp_math:
drh856c1032009-06-02 15:21:42 +00001581 rA = sqlite3VdbeRealValue(pIn1);
1582 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001583 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001584 case OP_Add: rB += rA; break;
1585 case OP_Subtract: rB -= rA; break;
1586 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001587 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001588 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001589 if( rA==(double)0 ) goto arithmetic_result_is_null;
1590 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001591 break;
1592 }
drhbf4133c2001-10-13 02:59:08 +00001593 default: {
shane75ac1de2009-06-09 18:58:52 +00001594 iA = (i64)rA;
1595 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001596 if( iA==0 ) goto arithmetic_result_is_null;
1597 if( iA==-1 ) iA = 1;
1598 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001599 break;
1600 }
drh5e00f6c2001-09-13 13:46:56 +00001601 }
drhc5a7b512010-01-13 16:25:42 +00001602#ifdef SQLITE_OMIT_FLOATING_POINT
1603 pOut->u.i = rB;
1604 MemSetTypeFlag(pOut, MEM_Int);
1605#else
drh856c1032009-06-02 15:21:42 +00001606 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001607 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001608 }
drh74eaba42014-09-18 17:52:15 +00001609 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001610 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001611 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001612 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001613 }
drhc5a7b512010-01-13 16:25:42 +00001614#endif
drh5e00f6c2001-09-13 13:46:56 +00001615 }
1616 break;
1617
drha05a7222008-01-19 03:35:58 +00001618arithmetic_result_is_null:
1619 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001620 break;
1621}
1622
drh7a957892012-02-02 17:35:43 +00001623/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001624**
drhbb6783b2017-04-29 18:02:49 +00001625** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001626** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1627** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001628** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001629**
drh7a957892012-02-02 17:35:43 +00001630** If P1 is not zero, then it is a register that a subsequent min() or
1631** max() aggregate will set to 1 if the current row is not the minimum or
1632** maximum. The P1 register is initialized to 0 by this instruction.
1633**
danielk1977dc1bdc42004-06-11 10:51:27 +00001634** The interface used by the implementation of the aforementioned functions
1635** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001636** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001637*/
drh9cbf3422008-01-17 16:22:13 +00001638case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001639 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001640 if( pOp->p1 ){
1641 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1642 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001643 break;
1644}
1645
drh98757152008-01-09 23:04:12 +00001646/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001647** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001648**
drh98757152008-01-09 23:04:12 +00001649** Take the bit-wise AND of the values in register P1 and P2 and
1650** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001651** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001652*/
drh98757152008-01-09 23:04:12 +00001653/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001654** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001655**
drh98757152008-01-09 23:04:12 +00001656** Take the bit-wise OR of the values in register P1 and P2 and
1657** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001658** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001659*/
drh98757152008-01-09 23:04:12 +00001660/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001661** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001662**
drh98757152008-01-09 23:04:12 +00001663** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001664** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001665** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001666** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001667*/
drh98757152008-01-09 23:04:12 +00001668/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001669** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001670**
drh98757152008-01-09 23:04:12 +00001671** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001672** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001673** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001674** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001675*/
drh5b6afba2008-01-05 16:29:28 +00001676case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1677case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1678case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1679case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001680 i64 iA;
1681 u64 uA;
1682 i64 iB;
1683 u8 op;
drh6810ce62004-01-31 19:22:56 +00001684
drh3c657212009-11-17 23:59:58 +00001685 pIn1 = &aMem[pOp->p1];
1686 pIn2 = &aMem[pOp->p2];
1687 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001688 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001689 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001690 break;
1691 }
drh158b9cb2011-03-05 20:59:46 +00001692 iA = sqlite3VdbeIntValue(pIn2);
1693 iB = sqlite3VdbeIntValue(pIn1);
1694 op = pOp->opcode;
1695 if( op==OP_BitAnd ){
1696 iA &= iB;
1697 }else if( op==OP_BitOr ){
1698 iA |= iB;
1699 }else if( iB!=0 ){
1700 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1701
1702 /* If shifting by a negative amount, shift in the other direction */
1703 if( iB<0 ){
1704 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1705 op = 2*OP_ShiftLeft + 1 - op;
1706 iB = iB>(-64) ? -iB : 64;
1707 }
1708
1709 if( iB>=64 ){
1710 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1711 }else{
1712 memcpy(&uA, &iA, sizeof(uA));
1713 if( op==OP_ShiftLeft ){
1714 uA <<= iB;
1715 }else{
1716 uA >>= iB;
1717 /* Sign-extend on a right shift of a negative number */
1718 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1719 }
1720 memcpy(&iA, &uA, sizeof(iA));
1721 }
drhbf4133c2001-10-13 02:59:08 +00001722 }
drh158b9cb2011-03-05 20:59:46 +00001723 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001724 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001725 break;
1726}
1727
drh8558cde2008-01-05 05:20:10 +00001728/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001729** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001730**
danielk19770cdc0222008-06-26 18:04:03 +00001731** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001732** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001733**
drh8558cde2008-01-05 05:20:10 +00001734** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001735*/
drh9cbf3422008-01-17 16:22:13 +00001736case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001737 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001738 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001739 sqlite3VdbeMemIntegerify(pIn1);
1740 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001741 break;
1742}
1743
drh9cbf3422008-01-17 16:22:13 +00001744/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001745**
drh9cbf3422008-01-17 16:22:13 +00001746** Force the value in register P1 to be an integer. If the value
1747** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001748** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001749** raise an SQLITE_MISMATCH exception.
1750*/
drh9cbf3422008-01-17 16:22:13 +00001751case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001752 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001753 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001754 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001755 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001756 if( (pIn1->flags & MEM_Int)==0 ){
1757 if( pOp->p2==0 ){
1758 rc = SQLITE_MISMATCH;
1759 goto abort_due_to_error;
1760 }else{
drhf56fa462015-04-13 21:39:54 +00001761 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001762 }
drh8aff1012001-12-22 14:49:24 +00001763 }
drh8aff1012001-12-22 14:49:24 +00001764 }
drh83b301b2013-11-20 00:59:02 +00001765 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001766 break;
1767}
1768
drh13573c72010-01-12 17:04:07 +00001769#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001770/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001771**
drh2133d822008-01-03 18:44:59 +00001772** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001773**
drh8a512562005-11-14 22:29:05 +00001774** This opcode is used when extracting information from a column that
1775** has REAL affinity. Such column values may still be stored as
1776** integers, for space efficiency, but after extraction we want them
1777** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001778*/
drh9cbf3422008-01-17 16:22:13 +00001779case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001780 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001781 if( pIn1->flags & MEM_Int ){
1782 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001783 }
drh487e2622005-06-25 18:42:14 +00001784 break;
1785}
drh13573c72010-01-12 17:04:07 +00001786#endif
drh487e2622005-06-25 18:42:14 +00001787
drh8df447f2005-11-01 15:48:24 +00001788#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001789/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001790** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001791**
drh4169e432014-08-25 20:11:52 +00001792** Force the value in register P1 to be the type defined by P2.
1793**
1794** <ul>
drhbb6783b2017-04-29 18:02:49 +00001795** <li> P2=='A' &rarr; BLOB
1796** <li> P2=='B' &rarr; TEXT
1797** <li> P2=='C' &rarr; NUMERIC
1798** <li> P2=='D' &rarr; INTEGER
1799** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001800** </ul>
drh487e2622005-06-25 18:42:14 +00001801**
1802** A NULL value is not changed by this routine. It remains NULL.
1803*/
drh4169e432014-08-25 20:11:52 +00001804case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001805 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001806 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001807 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001808 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1809 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1810 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001811 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001812 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001813 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001814 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001815 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001816 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001817 break;
1818}
drh8a512562005-11-14 22:29:05 +00001819#endif /* SQLITE_OMIT_CAST */
1820
drh79752b62016-08-13 10:02:17 +00001821/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001822** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001823**
1824** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1825** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1826** store the result of comparison in register P2.
1827**
1828** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1829** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1830** to coerce both inputs according to this affinity before the
1831** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1832** affinity is used. Note that the affinity conversions are stored
1833** back into the input registers P1 and P3. So this opcode can cause
1834** persistent changes to registers P1 and P3.
1835**
1836** Once any conversions have taken place, and neither value is NULL,
1837** the values are compared. If both values are blobs then memcmp() is
1838** used to determine the results of the comparison. If both values
1839** are text, then the appropriate collating function specified in
1840** P4 is used to do the comparison. If P4 is not specified then
1841** memcmp() is used to compare text string. If both values are
1842** numeric, then a numeric comparison is used. If the two values
1843** are of different types, then numbers are considered less than
1844** strings and strings are considered less than blobs.
1845**
1846** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1847** true or false and is never NULL. If both operands are NULL then the result
1848** of comparison is true. If either operand is NULL then the result is false.
1849** If neither operand is NULL the result is the same as it would be if
1850** the SQLITE_NULLEQ flag were omitted from P5.
1851**
1852** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001853** content of r[P2] is only changed if the new value is NULL or 0 (false).
1854** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001855*/
1856/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001857** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001858**
1859** This works just like the Eq opcode except that the jump is taken if
1860** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1861** additional information.
1862**
1863** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001864** content of r[P2] is only changed if the new value is NULL or 1 (true).
1865** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001866*/
drh35573352008-01-08 23:54:25 +00001867/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001868** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001869**
drh35573352008-01-08 23:54:25 +00001870** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001871** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1872** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001873**
drh35573352008-01-08 23:54:25 +00001874** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001875** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001876** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001877**
drh35573352008-01-08 23:54:25 +00001878** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001879** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001880** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001881** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001882** affinity is used. Note that the affinity conversions are stored
1883** back into the input registers P1 and P3. So this opcode can cause
1884** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001885**
1886** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001887** the values are compared. If both values are blobs then memcmp() is
1888** used to determine the results of the comparison. If both values
1889** are text, then the appropriate collating function specified in
1890** P4 is used to do the comparison. If P4 is not specified then
1891** memcmp() is used to compare text string. If both values are
1892** numeric, then a numeric comparison is used. If the two values
1893** are of different types, then numbers are considered less than
1894** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001895*/
drh9cbf3422008-01-17 16:22:13 +00001896/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001897** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001898**
drh35573352008-01-08 23:54:25 +00001899** This works just like the Lt opcode except that the jump is taken if
1900** the content of register P3 is less than or equal to the content of
1901** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001902*/
drh9cbf3422008-01-17 16:22:13 +00001903/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001904** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001905**
drh35573352008-01-08 23:54:25 +00001906** This works just like the Lt opcode except that the jump is taken if
1907** the content of register P3 is greater than the content of
1908** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001909*/
drh9cbf3422008-01-17 16:22:13 +00001910/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001911** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001912**
drh35573352008-01-08 23:54:25 +00001913** This works just like the Lt opcode except that the jump is taken if
1914** the content of register P3 is greater than or equal to the content of
1915** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001916*/
drh9cbf3422008-01-17 16:22:13 +00001917case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1918case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1919case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1920case OP_Le: /* same as TK_LE, jump, in1, in3 */
1921case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1922case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001923 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001924 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001925 u16 flags1; /* Copy of initial value of pIn1->flags */
1926 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001927
drh3c657212009-11-17 23:59:58 +00001928 pIn1 = &aMem[pOp->p1];
1929 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001930 flags1 = pIn1->flags;
1931 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001932 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001933 /* One or both operands are NULL */
1934 if( pOp->p5 & SQLITE_NULLEQ ){
1935 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1936 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1937 ** or not both operands are null.
1938 */
1939 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001940 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001941 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drhc3191d22016-10-18 16:36:15 +00001942 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001943 && (flags3&MEM_Cleared)==0
1944 ){
drh4910a762016-09-03 01:46:15 +00001945 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00001946 }else{
drh4910a762016-09-03 01:46:15 +00001947 res = 1; /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00001948 }
drh6a2fe092009-09-23 02:29:36 +00001949 }else{
1950 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1951 ** then the result is always NULL.
1952 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1953 */
drh688852a2014-02-17 22:40:43 +00001954 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001955 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001956 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00001957 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001958 MemSetTypeFlag(pOut, MEM_Null);
1959 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001960 }else{
drhf4345e42014-02-18 11:31:59 +00001961 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001962 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001963 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001964 }
drh6a2fe092009-09-23 02:29:36 +00001965 }
1966 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001967 }
drh6a2fe092009-09-23 02:29:36 +00001968 }else{
1969 /* Neither operand is NULL. Do a comparison. */
1970 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001971 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00001972 if( (flags1 | flags3)&MEM_Str ){
1973 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1974 applyNumericAffinity(pIn1,0);
drh64caee42016-09-09 19:33:00 +00001975 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
drh4b37cd42016-06-25 11:43:47 +00001976 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00001977 }
1978 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1979 applyNumericAffinity(pIn3,0);
1980 }
drh24a09622014-09-18 16:28:59 +00001981 }
drh64caee42016-09-09 19:33:00 +00001982 /* Handle the common case of integer comparison here, as an
1983 ** optimization, to avoid a call to sqlite3MemCompare() */
1984 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
1985 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
1986 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
1987 res = 0;
1988 goto compare_op;
1989 }
drh24a09622014-09-18 16:28:59 +00001990 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00001991 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001992 testcase( pIn1->flags & MEM_Int );
1993 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001994 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001995 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
1996 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00001997 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00001998 }
drhe5520e22015-12-31 04:34:26 +00001999 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002000 testcase( pIn3->flags & MEM_Int );
2001 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002002 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002003 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2004 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002005 }
drh6a2fe092009-09-23 02:29:36 +00002006 }
drh6a2fe092009-09-23 02:29:36 +00002007 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002008 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002009 }
drh64caee42016-09-09 19:33:00 +00002010compare_op:
drh58596362017-08-03 00:29:23 +00002011 /* At this point, res is negative, zero, or positive if reg[P1] is
2012 ** less than, equal to, or greater than reg[P3], respectively. Compute
2013 ** the answer to this operator in res2, depending on what the comparison
2014 ** operator actually is. The next block of code depends on the fact
2015 ** that the 6 comparison operators are consecutive integers in this
2016 ** order: NE, EQ, GT, LE, LT, GE */
2017 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2018 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
2019 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
2020 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
2021 res2 = aLTb[pOp->opcode - OP_Ne];
2022 }else if( res==0 ){
2023 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
2024 res2 = aEQb[pOp->opcode - OP_Ne];
2025 }else{
2026 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2027 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002028 }
2029
drhf56fa462015-04-13 21:39:54 +00002030 /* Undo any changes made by applyAffinity() to the input registers. */
2031 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2032 pIn1->flags = flags1;
2033 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2034 pIn3->flags = flags3;
2035
drh35573352008-01-08 23:54:25 +00002036 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002037 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002038 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002039 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002040 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002041 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2042 ** is only used in contexts where either:
2043 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2044 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2045 ** Therefore it is not necessary to check the content of r[P2] for
2046 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002047 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002048 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002049 testcase( res2==0 && pOp->opcode==OP_Eq );
2050 testcase( res2==1 && pOp->opcode==OP_Eq );
2051 testcase( res2==0 && pOp->opcode==OP_Ne );
2052 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002053 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002054 }
drh2b4ded92010-09-27 21:09:31 +00002055 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002056 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002057 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002058 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002059 }else{
drhf4345e42014-02-18 11:31:59 +00002060 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002061 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002062 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002063 }
danielk1977a37cdde2004-05-16 11:15:36 +00002064 }
2065 break;
2066}
drhc9b84a12002-06-20 11:36:48 +00002067
drh79752b62016-08-13 10:02:17 +00002068/* Opcode: ElseNotEq * P2 * * *
2069**
drhfd7459e2016-09-17 17:39:01 +00002070** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2071** If result of an OP_Eq comparison on the same two operands
2072** would have be NULL or false (0), then then jump to P2.
2073** If the result of an OP_Eq comparison on the two previous operands
2074** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002075*/
2076case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2077 assert( pOp>aOp );
2078 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002079 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002080 VdbeBranchTaken(iCompare!=0, 2);
2081 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002082 break;
2083}
2084
2085
drh0acb7e42008-06-25 00:12:41 +00002086/* Opcode: Permutation * * * P4 *
2087**
drhb7dab702017-01-26 18:00:00 +00002088** Set the permutation used by the OP_Compare operator in the next
2089** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002090**
drh953f7612012-12-07 22:18:54 +00002091** The permutation is only valid until the next OP_Compare that has
2092** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2093** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002094**
2095** The first integer in the P4 integer array is the length of the array
2096** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002097*/
2098case OP_Permutation: {
2099 assert( pOp->p4type==P4_INTARRAY );
2100 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002101 assert( pOp[1].opcode==OP_Compare );
2102 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002103 break;
2104}
2105
drh953f7612012-12-07 22:18:54 +00002106/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002107** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002108**
drh710c4842010-08-30 01:17:20 +00002109** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2110** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002111** the comparison for use by the next OP_Jump instruct.
2112**
drh0ca10df2012-12-08 13:26:23 +00002113** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2114** determined by the most recent OP_Permutation operator. If the
2115** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2116** order.
2117**
drh0acb7e42008-06-25 00:12:41 +00002118** P4 is a KeyInfo structure that defines collating sequences and sort
2119** orders for the comparison. The permutation applies to registers
2120** only. The KeyInfo elements are used sequentially.
2121**
2122** The comparison is a sort comparison, so NULLs compare equal,
2123** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002124** and strings are less than blobs.
2125*/
2126case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002127 int n;
2128 int i;
2129 int p1;
2130 int p2;
2131 const KeyInfo *pKeyInfo;
2132 int idx;
2133 CollSeq *pColl; /* Collating sequence to use on this term */
2134 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002135 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002136
drhb7dab702017-01-26 18:00:00 +00002137 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2138 aPermute = 0;
2139 }else{
2140 assert( pOp>aOp );
2141 assert( pOp[-1].opcode==OP_Permutation );
2142 assert( pOp[-1].p4type==P4_INTARRAY );
2143 aPermute = pOp[-1].p4.ai + 1;
2144 assert( aPermute!=0 );
2145 }
drh856c1032009-06-02 15:21:42 +00002146 n = pOp->p3;
2147 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002148 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002149 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002150 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002151 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002152#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002153 if( aPermute ){
2154 int k, mx = 0;
2155 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002156 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2157 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002158 }else{
drh9f6168b2016-03-19 23:32:58 +00002159 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2160 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002161 }
2162#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002163 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002164 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002165 assert( memIsValid(&aMem[p1+idx]) );
2166 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002167 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2168 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002169 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002170 pColl = pKeyInfo->aColl[i];
2171 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002172 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002173 if( iCompare ){
2174 if( bRev ) iCompare = -iCompare;
2175 break;
2176 }
drh16ee60f2008-06-20 18:13:25 +00002177 }
2178 break;
2179}
2180
2181/* Opcode: Jump P1 P2 P3 * *
2182**
2183** Jump to the instruction at address P1, P2, or P3 depending on whether
2184** in the most recent OP_Compare instruction the P1 vector was less than
2185** equal to, or greater than the P2 vector, respectively.
2186*/
drh0acb7e42008-06-25 00:12:41 +00002187case OP_Jump: { /* jump */
2188 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002189 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002190 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002191 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002192 }else{
drh7083a482018-07-10 16:04:04 +00002193 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002194 }
2195 break;
2196}
2197
drh5b6afba2008-01-05 16:29:28 +00002198/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002199** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002200**
drh5b6afba2008-01-05 16:29:28 +00002201** Take the logical AND of the values in registers P1 and P2 and
2202** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002203**
drh5b6afba2008-01-05 16:29:28 +00002204** If either P1 or P2 is 0 (false) then the result is 0 even if
2205** the other input is NULL. A NULL and true or two NULLs give
2206** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002207*/
drh5b6afba2008-01-05 16:29:28 +00002208/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002209** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002210**
2211** Take the logical OR of the values in register P1 and P2 and
2212** store the answer in register P3.
2213**
2214** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2215** even if the other input is NULL. A NULL and false or two NULLs
2216** give a NULL output.
2217*/
2218case OP_And: /* same as TK_AND, in1, in2, out3 */
2219case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002220 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2221 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002222
drh1fcfa722018-02-26 15:27:31 +00002223 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2224 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002225 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002226 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002227 v1 = and_logic[v1*3+v2];
2228 }else{
drh5b6afba2008-01-05 16:29:28 +00002229 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002230 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002231 }
drh3c657212009-11-17 23:59:58 +00002232 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002233 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002234 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002235 }else{
drh5b6afba2008-01-05 16:29:28 +00002236 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002237 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002238 }
drh5e00f6c2001-09-13 13:46:56 +00002239 break;
2240}
2241
drh8abed7b2018-02-26 18:49:05 +00002242/* Opcode: IsTrue P1 P2 P3 P4 *
2243** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2244**
2245** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2246** IS NOT FALSE operators.
2247**
drh96acafb2018-02-27 14:49:25 +00002248** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002249** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2250** NULL, then the P3 is stored in register P2. Invert the answer if P4
2251** is 1.
2252**
2253** The logic is summarized like this:
2254**
2255** <ul>
drh96acafb2018-02-27 14:49:25 +00002256** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2257** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2258** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2259** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002260** </ul>
2261*/
2262case OP_IsTrue: { /* in1, out2 */
2263 assert( pOp->p4type==P4_INT32 );
2264 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002265 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002266 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2267 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2268 break;
2269}
2270
drhe99fa2a2008-12-15 15:27:51 +00002271/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002272** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002273**
drhe99fa2a2008-12-15 15:27:51 +00002274** Interpret the value in register P1 as a boolean value. Store the
2275** boolean complement in register P2. If the value in register P1 is
2276** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002277*/
drh93952eb2009-11-13 19:43:43 +00002278case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002279 pIn1 = &aMem[pOp->p1];
2280 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002281 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002282 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002283 }else{
2284 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002285 }
drh5e00f6c2001-09-13 13:46:56 +00002286 break;
2287}
2288
drhe99fa2a2008-12-15 15:27:51 +00002289/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002290** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002291**
drhe99fa2a2008-12-15 15:27:51 +00002292** Interpret the content of register P1 as an integer. Store the
2293** ones-complement of the P1 value into register P2. If P1 holds
2294** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002295*/
drh93952eb2009-11-13 19:43:43 +00002296case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002297 pIn1 = &aMem[pOp->p1];
2298 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002299 sqlite3VdbeMemSetNull(pOut);
2300 if( (pIn1->flags & MEM_Null)==0 ){
2301 pOut->flags = MEM_Int;
2302 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002303 }
drhbf4133c2001-10-13 02:59:08 +00002304 break;
2305}
2306
drh48f2d3b2011-09-16 01:34:43 +00002307/* Opcode: Once P1 P2 * * *
2308**
drhab087d42017-03-24 17:59:56 +00002309** Fall through to the next instruction the first time this opcode is
2310** encountered on each invocation of the byte-code program. Jump to P2
2311** on the second and all subsequent encounters during the same invocation.
2312**
2313** Top-level programs determine first invocation by comparing the P1
2314** operand against the P1 operand on the OP_Init opcode at the beginning
2315** of the program. If the P1 values differ, then fall through and make
2316** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2317** the same then take the jump.
2318**
2319** For subprograms, there is a bitmask in the VdbeFrame that determines
2320** whether or not the jump should be taken. The bitmask is necessary
2321** because the self-altering code trick does not work for recursive
2322** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002323*/
dan1d8cb212011-12-09 13:24:16 +00002324case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002325 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002326 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002327 if( p->pFrame ){
2328 iAddr = (int)(pOp - p->aOp);
2329 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2330 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002331 goto jump_to_p2;
2332 }
drh18333ef2017-03-24 18:38:41 +00002333 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002334 }else{
drhab087d42017-03-24 17:59:56 +00002335 if( p->aOp[0].p1==pOp->p1 ){
2336 VdbeBranchTaken(1, 2);
2337 goto jump_to_p2;
2338 }
dan1d8cb212011-12-09 13:24:16 +00002339 }
drhab087d42017-03-24 17:59:56 +00002340 VdbeBranchTaken(0, 2);
2341 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002342 break;
2343}
2344
drh3c84ddf2008-01-09 02:15:38 +00002345/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002346**
drhef8662b2011-06-20 21:47:58 +00002347** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002348** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002349** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002350*/
drh1fcfa722018-02-26 15:27:31 +00002351case OP_If: { /* jump, in1 */
2352 int c;
2353 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2354 VdbeBranchTaken(c!=0, 2);
2355 if( c ) goto jump_to_p2;
2356 break;
2357}
2358
drh3c84ddf2008-01-09 02:15:38 +00002359/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002360**
drhef8662b2011-06-20 21:47:58 +00002361** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002362** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002363** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002364*/
drh9cbf3422008-01-17 16:22:13 +00002365case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002366 int c;
drh1fcfa722018-02-26 15:27:31 +00002367 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002368 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002369 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002370 break;
2371}
2372
drh830ecf92009-06-18 00:41:55 +00002373/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002374** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002375**
drh830ecf92009-06-18 00:41:55 +00002376** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002377*/
drh9cbf3422008-01-17 16:22:13 +00002378case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002379 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002380 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002381 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002382 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002383 }
drh477df4b2008-01-05 18:48:24 +00002384 break;
2385}
2386
drh98757152008-01-09 23:04:12 +00002387/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002388** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002389**
drh6a288a32008-01-07 19:20:24 +00002390** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002391*/
drh9cbf3422008-01-17 16:22:13 +00002392case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002393 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002394 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002395 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002396 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002397 }
drh5e00f6c2001-09-13 13:46:56 +00002398 break;
2399}
2400
drh31d6fd52017-04-14 19:03:10 +00002401/* Opcode: IfNullRow P1 P2 P3 * *
2402** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2403**
2404** Check the cursor P1 to see if it is currently pointing at a NULL row.
2405** If it is, then set register P3 to NULL and jump immediately to P2.
2406** If P1 is not on a NULL row, then fall through without making any
2407** changes.
2408*/
2409case OP_IfNullRow: { /* jump */
2410 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002411 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002412 if( p->apCsr[pOp->p1]->nullRow ){
2413 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2414 goto jump_to_p2;
2415 }
2416 break;
2417}
2418
drh092457b2017-12-29 15:04:49 +00002419#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2420/* Opcode: Offset P1 P2 P3 * *
2421** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002422**
drh092457b2017-12-29 15:04:49 +00002423** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002424** start of the payload for the record at which that cursor P1 is currently
2425** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002426**
drh092457b2017-12-29 15:04:49 +00002427** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002428** This opcode does not use P2 itself, but the P2 value is used by the
2429** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002430** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002431**
2432** This opcode is only available if SQLite is compiled with the
2433** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002434*/
drh092457b2017-12-29 15:04:49 +00002435case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002436 VdbeCursor *pC; /* The VDBE cursor */
2437 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2438 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002439 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002440 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002441 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002442 }else{
drh092457b2017-12-29 15:04:49 +00002443 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002444 }
2445 break;
2446}
drh092457b2017-12-29 15:04:49 +00002447#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002448
drh3e9ca092009-09-08 01:14:48 +00002449/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002450** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002451**
danielk1977cfcdaef2004-05-12 07:33:33 +00002452** Interpret the data that cursor P1 points to as a structure built using
2453** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002454** information about the format of the data.) Extract the P2-th column
2455** from this record. If there are less that (P2+1)
2456** values in the record, extract a NULL.
2457**
drh9cbf3422008-01-17 16:22:13 +00002458** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002459**
drh1cc3a362017-04-03 13:17:31 +00002460** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002461** if the P4 argument is a P4_MEM use the value of the P4 argument as
2462** the result.
drh3e9ca092009-09-08 01:14:48 +00002463**
2464** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2465** then the cache of the cursor is reset prior to extracting the column.
2466** The first OP_Column against a pseudo-table after the value of the content
2467** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002468**
drh1cc3a362017-04-03 13:17:31 +00002469** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002470** the result is guaranteed to only be used as the argument of a length()
2471** or typeof() function, respectively. The loading of large blobs can be
2472** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002473*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002474case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002475 int p2; /* column number to retrieve */
2476 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002477 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002478 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002479 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002480 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002481 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002482 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002483 const u8 *zData; /* Part of the record being decoded */
2484 const u8 *zHdr; /* Next unparsed byte of the header */
2485 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002486 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002487 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002488 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002489
dande892d92016-01-29 19:29:45 +00002490 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002491 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002492
drh170ad682017-06-02 15:44:22 +00002493 /* If the cursor cache is stale (meaning it is not currently point at
2494 ** the correct row) then bring it up-to-date by doing the necessary
2495 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002496 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002497 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002498
drh9f6168b2016-03-19 23:32:58 +00002499 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002500 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002501 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002502 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002503 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002504 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002505 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002506 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002507 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2508 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002509
drha43a02e2016-05-19 17:51:19 +00002510 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002511 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002512 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002513 /* For the special case of as pseudo-cursor, the seekResult field
2514 ** identifies the register that holds the record */
2515 assert( pC->seekResult>0 );
2516 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002517 assert( pReg->flags & MEM_Blob );
2518 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002519 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002520 pC->aRow = (u8*)pReg->z;
2521 }else{
drh6b5631e2014-11-05 15:57:39 +00002522 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002523 goto op_column_out;
2524 }
danielk1977192ac1d2004-05-10 07:17:30 +00002525 }else{
drh06a09a82016-11-25 17:03:03 +00002526 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002527 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002528 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002529 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2530 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002531 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2532 assert( pC->szRow<=pC->payloadSize );
2533 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2534 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002535 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002536 }
danielk1977192ac1d2004-05-10 07:17:30 +00002537 }
drhb73857f2006-03-17 00:25:59 +00002538 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002539 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002540 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002541
drhc81aa2e2014-10-11 23:31:52 +00002542
drh1f613c42017-08-16 14:16:19 +00002543 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002544 /* pC->aRow does not have to hold the entire row, but it does at least
2545 ** need to cover the header of the record. If pC->aRow does not contain
2546 ** the complete header, then set it to zero, forcing the header to be
2547 ** dynamically allocated. */
2548 pC->aRow = 0;
2549 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002550
2551 /* Make sure a corrupt database has not given us an oversize header.
2552 ** Do this now to avoid an oversize memory allocation.
2553 **
2554 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2555 ** types use so much data space that there can only be 4096 and 32 of
2556 ** them, respectively. So the maximum header length results from a
2557 ** 3-byte type for each of the maximum of 32768 columns plus three
2558 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2559 */
drh1f613c42017-08-16 14:16:19 +00002560 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002561 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002562 }
drh95b225a2017-08-16 11:04:22 +00002563 }else{
2564 /* This is an optimization. By skipping over the first few tests
2565 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2566 ** measurable performance gain.
2567 **
drh1f613c42017-08-16 14:16:19 +00002568 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002569 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002570 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002571 ** branch jumps to reads past the end of the record, but never more
2572 ** than a few bytes. Even if the record occurs at the end of the page
2573 ** content area, the "page header" comes after the page content and so
2574 ** this overread is harmless. Similar overreads can occur for a corrupt
2575 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002576 */
2577 zData = pC->aRow;
2578 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002579 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002580 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002581 }
drh399af1d2013-11-20 17:25:55 +00002582 }
drh35cd6432009-06-05 14:17:21 +00002583
drh399af1d2013-11-20 17:25:55 +00002584 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002585 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002586 */
drhc8606e42013-11-20 19:28:03 +00002587 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002588 /* If there is more header available for parsing in the record, try
2589 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002590 */
drhc8606e42013-11-20 19:28:03 +00002591 if( pC->iHdrOffset<aOffset[0] ){
2592 /* Make sure zData points to enough of the record to cover the header. */
2593 if( pC->aRow==0 ){
2594 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002595 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002596 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002597 zData = (u8*)sMem.z;
2598 }else{
2599 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002600 }
drhc8606e42013-11-20 19:28:03 +00002601
drh0c8f7602014-09-19 16:56:45 +00002602 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002603 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002604 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002605 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002606 zHdr = zData + pC->iHdrOffset;
2607 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002608 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002609 do{
drh95fa6062015-10-16 13:50:08 +00002610 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002611 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002612 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002613 }else{
drhc8606e42013-11-20 19:28:03 +00002614 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002615 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002616 }
drhfaf37272015-10-16 14:23:42 +00002617 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002618 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002619 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002620
drh8dd83622014-10-13 23:39:02 +00002621 /* The record is corrupt if any of the following are true:
2622 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002623 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002624 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002625 */
drhc6ce38832015-10-15 21:30:24 +00002626 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2627 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002628 ){
drh95b225a2017-08-16 11:04:22 +00002629 if( aOffset[0]==0 ){
2630 i = 0;
2631 zHdr = zEndHdr;
2632 }else{
2633 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002634 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002635 }
danielk1977dedf45b2006-01-13 17:12:01 +00002636 }
drhddb2b4a2016-03-25 12:10:32 +00002637
drh170c2762016-05-20 21:40:11 +00002638 pC->nHdrParsed = i;
2639 pC->iHdrOffset = (u32)(zHdr - zData);
2640 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002641 }else{
drh9fbc8852016-01-04 03:48:46 +00002642 t = 0;
drh9188b382004-05-14 21:12:22 +00002643 }
drhd3194f52004-05-27 19:59:32 +00002644
drhf2db3382015-04-30 20:33:25 +00002645 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002646 ** still not up to p2, that means that the record has fewer than p2
2647 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002648 */
drhc8606e42013-11-20 19:28:03 +00002649 if( pC->nHdrParsed<=p2 ){
2650 if( pOp->p4type==P4_MEM ){
2651 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2652 }else{
drh22e8d832014-10-29 00:58:38 +00002653 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002654 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002655 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002656 }
drh95fa6062015-10-16 13:50:08 +00002657 }else{
2658 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002659 }
danielk1977192ac1d2004-05-10 07:17:30 +00002660
drh380d6852013-11-20 20:58:00 +00002661 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002662 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002663 ** all valid.
drh9188b382004-05-14 21:12:22 +00002664 */
drhc8606e42013-11-20 19:28:03 +00002665 assert( p2<pC->nHdrParsed );
2666 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002667 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002668 if( VdbeMemDynamic(pDest) ){
2669 sqlite3VdbeMemSetNull(pDest);
2670 }
drh95fa6062015-10-16 13:50:08 +00002671 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002672 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002673 /* This is the common case where the desired content fits on the original
2674 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002675 zData = pC->aRow + aOffset[p2];
2676 if( t<12 ){
2677 sqlite3VdbeSerialGet(zData, t, pDest);
2678 }else{
2679 /* If the column value is a string, we need a persistent value, not
2680 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2681 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2682 */
2683 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2684 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002685 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002686 if( pDest->szMalloc < len+2 ){
2687 pDest->flags = MEM_Null;
2688 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2689 }else{
2690 pDest->z = pDest->zMalloc;
2691 }
2692 memcpy(pDest->z, zData, len);
2693 pDest->z[len] = 0;
2694 pDest->z[len+1] = 0;
2695 pDest->flags = aFlag[t&1];
2696 }
danielk197736963fd2005-02-19 08:18:05 +00002697 }else{
drha1851ef2016-05-20 19:51:28 +00002698 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002699 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002700 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2701 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2702 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002703 ){
drh2a2a6962014-09-16 18:22:44 +00002704 /* Content is irrelevant for
2705 ** 1. the typeof() function,
2706 ** 2. the length(X) function if X is a blob, and
2707 ** 3. if the content length is zero.
2708 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002709 ** content from disk.
2710 **
2711 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2712 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2713 ** read up to 16. So 16 bytes of bogus content is supplied.
2714 */
2715 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002716 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002717 }else{
drhcb3cabd2016-11-25 19:18:28 +00002718 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002719 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2720 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2721 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002722 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002723 }
drhd3194f52004-05-27 19:59:32 +00002724
danielk19773c9cc8d2005-01-17 03:40:08 +00002725op_column_out:
drhb7654112008-01-12 12:48:07 +00002726 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002727 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002728 break;
drh74588ce2017-09-13 00:13:05 +00002729
2730op_column_corrupt:
2731 if( aOp[0].p3>0 ){
2732 pOp = &aOp[aOp[0].p3-1];
2733 break;
2734 }else{
2735 rc = SQLITE_CORRUPT_BKPT;
2736 goto abort_due_to_error;
2737 }
danielk1977192ac1d2004-05-10 07:17:30 +00002738}
2739
danielk1977751de562008-04-18 09:01:15 +00002740/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002741** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002742**
2743** Apply affinities to a range of P2 registers starting with P1.
2744**
drhbb6783b2017-04-29 18:02:49 +00002745** P4 is a string that is P2 characters long. The N-th character of the
2746** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002747** memory cell in the range.
2748*/
2749case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002750 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002751
drh856c1032009-06-02 15:21:42 +00002752 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002753 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002754 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002755 assert( zAffinity[pOp->p2]==0 );
2756 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002757 do{
drh9f6168b2016-03-19 23:32:58 +00002758 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002759 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002760 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002761 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002762 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002763 break;
2764}
2765
drh1db639c2008-01-17 02:36:28 +00002766/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002767** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002768**
drh710c4842010-08-30 01:17:20 +00002769** Convert P2 registers beginning with P1 into the [record format]
2770** use as a data record in a database table or as a key
2771** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002772**
drhbb6783b2017-04-29 18:02:49 +00002773** P4 may be a string that is P2 characters long. The N-th character of the
2774** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002775** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002776**
drh8a512562005-11-14 22:29:05 +00002777** The mapping from character to affinity is given by the SQLITE_AFF_
2778** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002779**
drh05883a32015-06-02 15:32:08 +00002780** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002781*/
drh1db639c2008-01-17 02:36:28 +00002782case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002783 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2784 Mem *pRec; /* The new record */
2785 u64 nData; /* Number of bytes of data space */
2786 int nHdr; /* Number of bytes of header space */
2787 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002788 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002789 int nVarint; /* Number of bytes in a varint */
2790 u32 serial_type; /* Type field */
2791 Mem *pData0; /* First field to be combined into the record */
2792 Mem *pLast; /* Last field of the record */
2793 int nField; /* Number of fields in the record */
2794 char *zAffinity; /* The affinity string for the record */
2795 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002796 int i; /* Space used in zNewRecord[] header */
2797 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002798 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002799
drhf3218fe2004-05-28 08:21:02 +00002800 /* Assuming the record contains N fields, the record format looks
2801 ** like this:
2802 **
drh7a224de2004-06-02 01:22:02 +00002803 ** ------------------------------------------------------------------------
2804 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2805 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002806 **
drh9cbf3422008-01-17 16:22:13 +00002807 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002808 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002809 **
2810 ** Each type field is a varint representing the serial type of the
2811 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002812 ** hdr-size field is also a varint which is the offset from the beginning
2813 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002814 */
drh856c1032009-06-02 15:21:42 +00002815 nData = 0; /* Number of bytes of data space */
2816 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002817 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002818 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002819 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002820 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002821 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002822 nField = pOp->p2;
2823 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002824 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002825
drh2b4ded92010-09-27 21:09:31 +00002826 /* Identify the output register */
2827 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2828 pOut = &aMem[pOp->p3];
2829 memAboutToChange(p, pOut);
2830
drh3e6c0602013-12-10 20:53:01 +00002831 /* Apply the requested affinity to all inputs
2832 */
2833 assert( pData0<=pLast );
2834 if( zAffinity ){
2835 pRec = pData0;
2836 do{
drh57bf4a82014-02-17 14:59:22 +00002837 applyAffinity(pRec++, *(zAffinity++), encoding);
2838 assert( zAffinity[0]==0 || pRec<=pLast );
2839 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002840 }
2841
drhd447dce2017-01-25 20:55:11 +00002842#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002843 /* NULLs can be safely trimmed from the end of the record, as long as
2844 ** as the schema format is 2 or more and none of the omitted columns
2845 ** have a non-NULL default value. Also, the record must be left with
2846 ** at least one field. If P5>0 then it will be one more than the
2847 ** index of the right-most column with a non-NULL default value */
2848 if( pOp->p5 ){
2849 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2850 pLast--;
2851 nField--;
2852 }
2853 }
drhd447dce2017-01-25 20:55:11 +00002854#endif
drh585ce192017-01-25 14:58:27 +00002855
drhf3218fe2004-05-28 08:21:02 +00002856 /* Loop through the elements that will make up the record to figure
2857 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002858 */
drh038b7bc2013-12-09 23:17:22 +00002859 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002860 do{
drh2b4ded92010-09-27 21:09:31 +00002861 assert( memIsValid(pRec) );
drh41fb3672018-01-12 23:18:38 +00002862 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002863 if( pRec->flags & MEM_Zero ){
drhce2fbd12018-01-12 21:00:14 +00002864 if( serial_type==0 ){
drh41fb3672018-01-12 23:18:38 +00002865 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
2866 ** table methods that never invoke sqlite3_result_xxxxx() while
2867 ** computing an unchanging column value in an UPDATE statement.
2868 ** Give such values a special internal-use-only serial-type of 10
2869 ** so that they can be passed through to xUpdate and have
2870 ** a true sqlite3_value_nochange(). */
2871 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
2872 serial_type = 10;
drhce2fbd12018-01-12 21:00:14 +00002873 }else if( nData ){
drh53e66c32015-07-24 15:49:23 +00002874 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002875 }else{
2876 nZero += pRec->u.nZero;
2877 len -= pRec->u.nZero;
2878 }
drhfdf972a2007-05-02 13:30:27 +00002879 }
drh8079a0d2006-01-12 17:20:50 +00002880 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002881 testcase( serial_type==127 );
2882 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002883 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh41fb3672018-01-12 23:18:38 +00002884 pRec->uTemp = serial_type;
drh45c3c662016-04-07 14:16:16 +00002885 if( pRec==pData0 ) break;
2886 pRec--;
2887 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002888
drh654858d2014-11-20 02:18:14 +00002889 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2890 ** which determines the total number of bytes in the header. The varint
2891 ** value is the size of the header in bytes including the size varint
2892 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002893 testcase( nHdr==126 );
2894 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002895 if( nHdr<=126 ){
2896 /* The common case */
2897 nHdr += 1;
2898 }else{
2899 /* Rare case of a really large header */
2900 nVarint = sqlite3VarintLen(nHdr);
2901 nHdr += nVarint;
2902 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002903 }
drh038b7bc2013-12-09 23:17:22 +00002904 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002905 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002906 goto too_big;
2907 }
drhf3218fe2004-05-28 08:21:02 +00002908
danielk1977a7a8e142008-02-13 18:25:27 +00002909 /* Make sure the output register has a buffer large enough to store
2910 ** the new record. The output register (pOp->p3) is not allowed to
2911 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002912 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002913 */
drh322f2852014-09-19 00:43:39 +00002914 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002915 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002916 }
danielk1977a7a8e142008-02-13 18:25:27 +00002917 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002918
2919 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002920 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002921 j = nHdr;
2922 assert( pData0<=pLast );
2923 pRec = pData0;
2924 do{
drhfacf47a2014-10-13 20:12:47 +00002925 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002926 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2927 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002928 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002929 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2930 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002931 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002932 }while( (++pRec)<=pLast );
2933 assert( i==nHdr );
2934 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002935
drh9f6168b2016-03-19 23:32:58 +00002936 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002937 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002938 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002939 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002940 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002941 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002942 }
drh1013c932008-01-06 00:25:21 +00002943 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002944 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002945 break;
2946}
2947
danielk1977a5533162009-02-24 10:01:51 +00002948/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002949** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002950**
2951** Store the number of entries (an integer value) in the table or index
2952** opened by cursor P1 in register P2
2953*/
2954#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002955case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002956 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002957 BtCursor *pCrsr;
2958
drhc960dcb2015-11-20 19:22:01 +00002959 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2960 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002961 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002962 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002963 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002964 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002965 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002966 pOut->u.i = nEntry;
2967 break;
2968}
2969#endif
2970
danielk1977fd7f0452008-12-17 17:30:26 +00002971/* Opcode: Savepoint P1 * * P4 *
2972**
2973** Open, release or rollback the savepoint named by parameter P4, depending
2974** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2975** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2976*/
2977case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002978 int p1; /* Value of P1 operand */
2979 char *zName; /* Name of savepoint */
2980 int nName;
2981 Savepoint *pNew;
2982 Savepoint *pSavepoint;
2983 Savepoint *pTmp;
2984 int iSavepoint;
2985 int ii;
2986
2987 p1 = pOp->p1;
2988 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002989
2990 /* Assert that the p1 parameter is valid. Also that if there is no open
2991 ** transaction, then there cannot be any savepoints.
2992 */
2993 assert( db->pSavepoint==0 || db->autoCommit==0 );
2994 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2995 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2996 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002997 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002998
2999 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003000 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003001 /* A new savepoint cannot be created if there are active write
3002 ** statements (i.e. open read/write incremental blob handles).
3003 */
drh22c17b82015-05-15 04:13:15 +00003004 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003005 rc = SQLITE_BUSY;
3006 }else{
drh856c1032009-06-02 15:21:42 +00003007 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003008
drhbe07ec52011-06-03 12:15:26 +00003009#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003010 /* This call is Ok even if this savepoint is actually a transaction
3011 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3012 ** If this is a transaction savepoint being opened, it is guaranteed
3013 ** that the db->aVTrans[] array is empty. */
3014 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003015 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3016 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003017 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003018#endif
dand9495cd2011-04-27 12:08:04 +00003019
danielk1977fd7f0452008-12-17 17:30:26 +00003020 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003021 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003022 if( pNew ){
3023 pNew->zName = (char *)&pNew[1];
3024 memcpy(pNew->zName, zName, nName+1);
3025
3026 /* If there is no open transaction, then mark this as a special
3027 ** "transaction savepoint". */
3028 if( db->autoCommit ){
3029 db->autoCommit = 0;
3030 db->isTransactionSavepoint = 1;
3031 }else{
3032 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003033 }
dan21e8d012011-03-03 20:05:59 +00003034
danielk1977fd7f0452008-12-17 17:30:26 +00003035 /* Link the new savepoint into the database handle's list. */
3036 pNew->pNext = db->pSavepoint;
3037 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003038 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003039 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003040 }
3041 }
3042 }else{
drh856c1032009-06-02 15:21:42 +00003043 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003044
3045 /* Find the named savepoint. If there is no such savepoint, then an
3046 ** an error is returned to the user. */
3047 for(
drh856c1032009-06-02 15:21:42 +00003048 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003049 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003050 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003051 ){
3052 iSavepoint++;
3053 }
3054 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003055 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003056 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003057 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003058 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003059 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003060 */
drh22c17b82015-05-15 04:13:15 +00003061 sqlite3VdbeError(p, "cannot release savepoint - "
3062 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003063 rc = SQLITE_BUSY;
3064 }else{
3065
3066 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003067 ** and this is a RELEASE command, then the current transaction
3068 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003069 */
3070 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3071 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003072 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003073 goto vdbe_return;
3074 }
danielk1977fd7f0452008-12-17 17:30:26 +00003075 db->autoCommit = 1;
3076 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003077 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003078 db->autoCommit = 0;
3079 p->rc = rc = SQLITE_BUSY;
3080 goto vdbe_return;
3081 }
danielk197734cf35d2008-12-18 18:31:38 +00003082 db->isTransactionSavepoint = 0;
3083 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003084 }else{
drh47b7fc72014-11-11 01:33:57 +00003085 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003086 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003087 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003088 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003089 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003090 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3091 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003092 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003093 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003094 }
drh47b7fc72014-11-11 01:33:57 +00003095 }else{
3096 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003097 }
3098 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003099 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3100 if( rc!=SQLITE_OK ){
3101 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003102 }
danielk1977fd7f0452008-12-17 17:30:26 +00003103 }
drh47b7fc72014-11-11 01:33:57 +00003104 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003105 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003106 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003107 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003108 }
3109 }
3110
3111 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3112 ** savepoints nested inside of the savepoint being operated on. */
3113 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003114 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003115 db->pSavepoint = pTmp->pNext;
3116 sqlite3DbFree(db, pTmp);
3117 db->nSavepoint--;
3118 }
3119
dan1da40a32009-09-19 17:00:31 +00003120 /* If it is a RELEASE, then destroy the savepoint being operated on
3121 ** too. If it is a ROLLBACK TO, then set the number of deferred
3122 ** constraint violations present in the database to the value stored
3123 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003124 if( p1==SAVEPOINT_RELEASE ){
3125 assert( pSavepoint==db->pSavepoint );
3126 db->pSavepoint = pSavepoint->pNext;
3127 sqlite3DbFree(db, pSavepoint);
3128 if( !isTransaction ){
3129 db->nSavepoint--;
3130 }
dan1da40a32009-09-19 17:00:31 +00003131 }else{
3132 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003133 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003134 }
dand9495cd2011-04-27 12:08:04 +00003135
danea8562e2015-04-18 16:25:54 +00003136 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003137 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3138 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3139 }
danielk1977fd7f0452008-12-17 17:30:26 +00003140 }
3141 }
drh9467abf2016-02-17 18:44:11 +00003142 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003143
3144 break;
3145}
3146
drh98757152008-01-09 23:04:12 +00003147/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003148**
3149** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003150** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003151** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3152** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003153**
3154** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003155*/
drh9cbf3422008-01-17 16:22:13 +00003156case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003157 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003158 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003159
drh856c1032009-06-02 15:21:42 +00003160 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003161 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003162 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003163 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003164 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003165 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003166
drhb0c88652016-02-01 13:21:13 +00003167 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003168 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003169 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003170 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003171 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003172 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3173 /* If this instruction implements a COMMIT and other VMs are writing
3174 ** return an error indicating that the other VMs must complete first.
3175 */
3176 sqlite3VdbeError(p, "cannot commit transaction - "
3177 "SQL statements in progress");
3178 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003179 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003180 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003181 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003182 }else{
shane7d3846a2008-12-11 02:58:26 +00003183 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003184 }
3185 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3186 p->pc = (int)(pOp - aOp);
3187 db->autoCommit = (u8)(1-desiredAutoCommit);
3188 p->rc = rc = SQLITE_BUSY;
3189 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003190 }
danielk1977bd434552009-03-18 10:33:00 +00003191 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003192 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003193 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003194 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003195 }else{
drh900b31e2007-08-28 02:27:51 +00003196 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003197 }
drh900b31e2007-08-28 02:27:51 +00003198 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003199 }else{
drh22c17b82015-05-15 04:13:15 +00003200 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003201 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003202 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003203 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003204
3205 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003206 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003207 }
3208 break;
3209}
3210
drhb22f7c82014-02-06 23:56:27 +00003211/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003212**
drh05a86c52014-02-16 01:55:49 +00003213** Begin a transaction on database P1 if a transaction is not already
3214** active.
3215** If P2 is non-zero, then a write-transaction is started, or if a
3216** read-transaction is already active, it is upgraded to a write-transaction.
3217** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003218**
drh001bbcb2003-03-19 03:14:00 +00003219** P1 is the index of the database file on which the transaction is
3220** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003221** file used for temporary tables. Indices of 2 or more are used for
3222** attached databases.
drhcabb0812002-09-14 13:47:32 +00003223**
dane0af83a2009-09-08 19:15:01 +00003224** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3225** true (this flag is set if the Vdbe may modify more than one row and may
3226** throw an ABORT exception), a statement transaction may also be opened.
3227** More specifically, a statement transaction is opened iff the database
3228** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003229** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003230** VDBE to be rolled back after an error without having to roll back the
3231** entire transaction. If no error is encountered, the statement transaction
3232** will automatically commit when the VDBE halts.
3233**
drhb22f7c82014-02-06 23:56:27 +00003234** If P5!=0 then this opcode also checks the schema cookie against P3
3235** and the schema generation counter against P4.
3236** The cookie changes its value whenever the database schema changes.
3237** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003238** and that the current process needs to reread the schema. If the schema
3239** cookie in P3 differs from the schema cookie in the database header or
3240** if the schema generation counter in P4 differs from the current
3241** generation counter, then an SQLITE_SCHEMA error is raised and execution
3242** halts. The sqlite3_step() wrapper function might then reprepare the
3243** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003244*/
drh9cbf3422008-01-17 16:22:13 +00003245case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003246 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003247 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003248
drh1713afb2013-06-28 01:24:57 +00003249 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003250 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003251 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003252 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003253 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3254 rc = SQLITE_READONLY;
3255 goto abort_due_to_error;
3256 }
drh653b82a2009-06-22 11:10:47 +00003257 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003258
danielk197724162fe2004-06-04 06:22:00 +00003259 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003260 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003261 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3262 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003263 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003264 if( (rc&0xff)==SQLITE_BUSY ){
3265 p->pc = (int)(pOp - aOp);
3266 p->rc = rc;
3267 goto vdbe_return;
3268 }
danielk197724162fe2004-06-04 06:22:00 +00003269 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003270 }
dane0af83a2009-09-08 19:15:01 +00003271
3272 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003273 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003274 ){
3275 assert( sqlite3BtreeIsInTrans(pBt) );
3276 if( p->iStatement==0 ){
3277 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3278 db->nStatement++;
3279 p->iStatement = db->nSavepoint + db->nStatement;
3280 }
dana311b802011-04-26 19:21:34 +00003281
drh346506f2011-05-25 01:16:42 +00003282 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003283 if( rc==SQLITE_OK ){
3284 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3285 }
dan1da40a32009-09-19 17:00:31 +00003286
3287 /* Store the current value of the database handles deferred constraint
3288 ** counter. If the statement transaction needs to be rolled back,
3289 ** the value of this counter needs to be restored too. */
3290 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003291 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003292 }
drh397776a2018-06-06 17:45:51 +00003293 }
3294 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3295 if( pOp->p5
3296 && (iMeta!=pOp->p3
3297 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3298 ){
3299 /*
drh96fdcb42016-09-27 00:09:33 +00003300 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3301 ** version is checked to ensure that the schema has not changed since the
3302 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003303 */
drhb22f7c82014-02-06 23:56:27 +00003304 sqlite3DbFree(db, p->zErrMsg);
3305 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3306 /* If the schema-cookie from the database file matches the cookie
3307 ** stored with the in-memory representation of the schema, do
3308 ** not reload the schema from the database file.
3309 **
3310 ** If virtual-tables are in use, this is not just an optimization.
3311 ** Often, v-tables store their data in other SQLite tables, which
3312 ** are queried from within xNext() and other v-table methods using
3313 ** prepared queries. If such a query is out-of-date, we do not want to
3314 ** discard the database schema, as the user code implementing the
3315 ** v-table would have to be ready for the sqlite3_vtab structure itself
3316 ** to be invalidated whenever sqlite3_step() is called from within
3317 ** a v-table method.
3318 */
3319 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3320 sqlite3ResetOneSchema(db, pOp->p1);
3321 }
3322 p->expired = 1;
3323 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003324 }
drh9467abf2016-02-17 18:44:11 +00003325 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003326 break;
3327}
3328
drhb1fdb2a2008-01-05 04:06:03 +00003329/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003330**
drh9cbf3422008-01-17 16:22:13 +00003331** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003332** P3==1 is the schema version. P3==2 is the database format.
3333** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003334** the main database file and P1==1 is the database file used to store
3335** temporary tables.
drh4a324312001-12-21 14:30:42 +00003336**
drh50e5dad2001-09-15 00:57:28 +00003337** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003338** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003339** executing this instruction.
3340*/
drh27a348c2015-04-13 19:14:06 +00003341case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003342 int iMeta;
drh856c1032009-06-02 15:21:42 +00003343 int iDb;
3344 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003345
drh1713afb2013-06-28 01:24:57 +00003346 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003347 iDb = pOp->p1;
3348 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003349 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003350 assert( iDb>=0 && iDb<db->nDb );
3351 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003352 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003353
danielk1977602b4662009-07-02 07:47:33 +00003354 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003355 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003356 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003357 break;
3358}
3359
drh98757152008-01-09 23:04:12 +00003360/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003361**
drh1861afc2016-02-01 21:48:34 +00003362** Write the integer value P3 into cookie number P2 of database P1.
3363** P2==1 is the schema version. P2==2 is the database format.
3364** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003365** size, and so forth. P1==0 is the main database file and P1==1 is the
3366** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003367**
3368** A transaction must be started before executing this opcode.
3369*/
drh1861afc2016-02-01 21:48:34 +00003370case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003371 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003372
3373 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003374 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003375 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003376 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003377 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003378 pDb = &db->aDb[pOp->p1];
3379 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003380 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003381 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003382 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003383 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003384 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003385 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003386 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003387 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003388 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003389 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003390 }
drhfd426c62006-01-30 15:34:22 +00003391 if( pOp->p1==1 ){
3392 /* Invalidate all prepared statements whenever the TEMP database
3393 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003394 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003395 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003396 }
drh9467abf2016-02-17 18:44:11 +00003397 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003398 break;
3399}
3400
drh98757152008-01-09 23:04:12 +00003401/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003402** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003403**
drhecdc7532001-09-23 02:35:53 +00003404** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003405** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003406** P3==0 means the main database, P3==1 means the database used for
3407** temporary tables, and P3>1 means used the corresponding attached
3408** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003409** values need not be contiguous but all P1 values should be small integers.
3410** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003411**
drh8e9deb62018-06-05 13:43:02 +00003412** Allowed P5 bits:
3413** <ul>
3414** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3415** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3416** of OP_SeekLE/OP_IdxGT)
3417** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003418**
danielk1977d336e222009-02-20 10:58:41 +00003419** The P4 value may be either an integer (P4_INT32) or a pointer to
3420** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003421** object, then table being opened must be an [index b-tree] where the
3422** KeyInfo object defines the content and collating
3423** sequence of that index b-tree. Otherwise, if P4 is an integer
3424** value, then the table being opened must be a [table b-tree] with a
3425** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003426**
drh35263192014-07-22 20:02:19 +00003427** See also: OpenWrite, ReopenIdx
3428*/
3429/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3430** Synopsis: root=P2 iDb=P3
3431**
drh8e9deb62018-06-05 13:43:02 +00003432** The ReopenIdx opcode works like OP_OpenRead except that it first
3433** checks to see if the cursor on P1 is already open on the same
3434** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003435** if the cursor is already open, do not reopen it.
3436**
drh8e9deb62018-06-05 13:43:02 +00003437** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3438** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3439** be the same as every other ReopenIdx or OpenRead for the same cursor
3440** number.
drh35263192014-07-22 20:02:19 +00003441**
drh8e9deb62018-06-05 13:43:02 +00003442** Allowed P5 bits:
3443** <ul>
3444** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3445** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3446** of OP_SeekLE/OP_IdxGT)
3447** </ul>
3448**
3449** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003450*/
drh98757152008-01-09 23:04:12 +00003451/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003452** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003453**
3454** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003455** page is P2 (or whose root page is held in register P2 if the
3456** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003457**
danielk1977d336e222009-02-20 10:58:41 +00003458** The P4 value may be either an integer (P4_INT32) or a pointer to
3459** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003460** object, then table being opened must be an [index b-tree] where the
3461** KeyInfo object defines the content and collating
3462** sequence of that index b-tree. Otherwise, if P4 is an integer
3463** value, then the table being opened must be a [table b-tree] with a
3464** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003465**
drh8e9deb62018-06-05 13:43:02 +00003466** Allowed P5 bits:
3467** <ul>
3468** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3469** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3470** of OP_SeekLE/OP_IdxGT)
3471** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3472** and subsequently delete entries in an index btree. This is a
3473** hint to the storage engine that the storage engine is allowed to
3474** ignore. The hint is not used by the official SQLite b*tree storage
3475** engine, but is used by COMDB2.
3476** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3477** as the root page, not the value of P2 itself.
3478** </ul>
drhf57b3392001-10-08 13:22:32 +00003479**
drh8e9deb62018-06-05 13:43:02 +00003480** This instruction works like OpenRead except that it opens the cursor
3481** in read/write mode.
3482**
3483** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003484*/
drh35263192014-07-22 20:02:19 +00003485case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003486 int nField;
3487 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003488 int p2;
3489 int iDb;
drhf57b3392001-10-08 13:22:32 +00003490 int wrFlag;
3491 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003492 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003493 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003494
drhe0997b32015-03-20 14:57:50 +00003495 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003496 assert( pOp->p4type==P4_KEYINFO );
3497 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003498 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003499 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003500 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003501 }
3502 /* If the cursor is not currently open or is open on a different
3503 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003504case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003505case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003506
drhe0997b32015-03-20 14:57:50 +00003507 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003508 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003509 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3510 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003511
drhba968db2018-07-24 22:02:12 +00003512 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00003513 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003514 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003515 }
3516
drh856c1032009-06-02 15:21:42 +00003517 nField = 0;
3518 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003519 p2 = pOp->p2;
3520 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003521 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003522 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003523 pDb = &db->aDb[iDb];
3524 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003525 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003526 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003527 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3528 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003529 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003530 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3531 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003532 }
3533 }else{
3534 wrFlag = 0;
3535 }
dan428c2182012-08-06 18:50:11 +00003536 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003537 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003538 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003539 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003540 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003541 assert( memIsValid(pIn2) );
3542 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003543 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003544 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003545 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003546 ** that opcode will always set the p2 value to 2 or more or else fail.
3547 ** If there were a failure, the prepared statement would have halted
3548 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003549 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003550 }
danielk1977d336e222009-02-20 10:58:41 +00003551 if( pOp->p4type==P4_KEYINFO ){
3552 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003553 assert( pKeyInfo->enc==ENC(db) );
3554 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003555 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003556 }else if( pOp->p4type==P4_INT32 ){
3557 nField = pOp->p4.i;
3558 }
drh653b82a2009-06-22 11:10:47 +00003559 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003560 assert( nField>=0 );
3561 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003562 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003563 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003564 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003565 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003566 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003567#ifdef SQLITE_DEBUG
3568 pCur->wrFlag = wrFlag;
3569#endif
drhc960dcb2015-11-20 19:22:01 +00003570 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003571 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003572 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003573 ** SQLite used to check if the root-page flags were sane at this point
3574 ** and report database corruption if they were not, but this check has
3575 ** since moved into the btree layer. */
3576 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003577
3578open_cursor_set_hints:
3579 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3580 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003581 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003582#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003583 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3584#endif
drhc960dcb2015-11-20 19:22:01 +00003585 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003586 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003587 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003588 break;
3589}
3590
drhe08e8d62017-05-01 15:15:41 +00003591/* Opcode: OpenDup P1 P2 * * *
3592**
3593** Open a new cursor P1 that points to the same ephemeral table as
3594** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3595** opcode. Only ephemeral cursors may be duplicated.
3596**
3597** Duplicate ephemeral cursors are used for self-joins of materialized views.
3598*/
3599case OP_OpenDup: {
3600 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3601 VdbeCursor *pCx; /* The new cursor */
3602
3603 pOrig = p->apCsr[pOp->p2];
3604 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3605
3606 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3607 if( pCx==0 ) goto no_mem;
3608 pCx->nullRow = 1;
3609 pCx->isEphemeral = 1;
3610 pCx->pKeyInfo = pOrig->pKeyInfo;
3611 pCx->isTable = pOrig->isTable;
3612 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
3613 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003614 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3615 ** opened for a database. Since there is already an open cursor when this
3616 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3617 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003618 break;
3619}
3620
3621
drh2a5d9902011-08-26 00:34:45 +00003622/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003623** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003624**
drhb9bb7c12006-06-11 23:41:55 +00003625** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003626** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003627** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003628** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003629**
drh25d3adb2010-04-05 15:11:08 +00003630** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003631** The cursor points to a BTree table if P4==0 and to a BTree index
3632** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003633** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003634**
drh2a5d9902011-08-26 00:34:45 +00003635** The P5 parameter can be a mask of the BTREE_* flags defined
3636** in btree.h. These flags control aspects of the operation of
3637** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3638** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003639*/
drha21a64d2010-04-06 22:33:55 +00003640/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003641** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003642**
3643** This opcode works the same as OP_OpenEphemeral. It has a
3644** different name to distinguish its use. Tables created using
3645** by this opcode will be used for automatically created transient
3646** indices in joins.
3647*/
3648case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003649case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003650 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003651 KeyInfo *pKeyInfo;
3652
drhd4187c72010-08-30 22:15:45 +00003653 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003654 SQLITE_OPEN_READWRITE |
3655 SQLITE_OPEN_CREATE |
3656 SQLITE_OPEN_EXCLUSIVE |
3657 SQLITE_OPEN_DELETEONCLOSE |
3658 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003659 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003660 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003661 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003662 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003663 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003664 pCx->isEphemeral = 1;
drhfbd8cbd2016-12-10 12:58:15 +00003665 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
drhd4187c72010-08-30 22:15:45 +00003666 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003667 if( rc==SQLITE_OK ){
drhbb2d9b12018-06-06 16:28:40 +00003668 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00003669 }
3670 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003671 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003672 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003673 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003674 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003675 */
drhfbd8cbd2016-12-10 12:58:15 +00003676 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003677 int pgno;
drh66a51672008-01-03 00:01:23 +00003678 assert( pOp->p4type==P4_KEYINFO );
drhfbd8cbd2016-12-10 12:58:15 +00003679 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003680 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003681 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003682 assert( pKeyInfo->db==db );
3683 assert( pKeyInfo->enc==ENC(db) );
drhfbd8cbd2016-12-10 12:58:15 +00003684 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003685 pKeyInfo, pCx->uc.pCursor);
drhc6b52df2002-01-04 03:09:29 +00003686 }
drhf0863fe2005-06-12 21:35:51 +00003687 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003688 }else{
drhfbd8cbd2016-12-10 12:58:15 +00003689 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003690 0, pCx->uc.pCursor);
drhf0863fe2005-06-12 21:35:51 +00003691 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003692 }
drh5e00f6c2001-09-13 13:46:56 +00003693 }
drh9467abf2016-02-17 18:44:11 +00003694 if( rc ) goto abort_due_to_error;
drhd4187c72010-08-30 22:15:45 +00003695 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003696 break;
3697}
3698
danfad9f9a2014-04-01 18:41:51 +00003699/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003700**
3701** This opcode works like OP_OpenEphemeral except that it opens
3702** a transient index that is specifically designed to sort large
3703** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003704**
3705** If argument P3 is non-zero, then it indicates that the sorter may
3706** assume that a stable sort considering the first P3 fields of each
3707** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003708*/
drhca892a72011-09-03 00:17:51 +00003709case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003710 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003711
drh399af1d2013-11-20 17:25:55 +00003712 assert( pOp->p1>=0 );
3713 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003714 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003715 if( pCx==0 ) goto no_mem;
3716 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003717 assert( pCx->pKeyInfo->db==db );
3718 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003719 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003720 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003721 break;
3722}
3723
dan78d58432014-03-25 15:04:07 +00003724/* Opcode: SequenceTest P1 P2 * * *
3725** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3726**
3727** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3728** to P2. Regardless of whether or not the jump is taken, increment the
3729** the sequence value.
3730*/
3731case OP_SequenceTest: {
3732 VdbeCursor *pC;
3733 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3734 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003735 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003736 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003737 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003738 }
drh5e00f6c2001-09-13 13:46:56 +00003739 break;
3740}
3741
drh5f612292014-02-08 23:20:32 +00003742/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003743** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003744**
3745** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003746** row of data. The content of that one row is the content of memory
3747** register P2. In other words, cursor P1 becomes an alias for the
3748** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003749**
drh2d8d7ce2010-02-15 15:17:05 +00003750** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003751** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003752** individual columns using the OP_Column opcode. The OP_Column opcode
3753** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003754**
3755** P3 is the number of fields in the records that will be stored by
3756** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003757*/
drh9cbf3422008-01-17 16:22:13 +00003758case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003759 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003760
drh653b82a2009-06-22 11:10:47 +00003761 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003762 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003763 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003764 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003765 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003766 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003767 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003768 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3769 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3770 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3771 ** which is a performance optimization */
3772 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003773 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003774 break;
3775}
3776
drh98757152008-01-09 23:04:12 +00003777/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003778**
3779** Close a cursor previously opened as P1. If P1 is not
3780** currently open, this instruction is a no-op.
3781*/
drh9cbf3422008-01-17 16:22:13 +00003782case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003783 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3784 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3785 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003786 break;
3787}
3788
drh97bae792015-06-05 15:59:57 +00003789#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3790/* Opcode: ColumnsUsed P1 * * P4 *
3791**
3792** This opcode (which only exists if SQLite was compiled with
3793** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3794** table or index for cursor P1 are used. P4 is a 64-bit integer
3795** (P4_INT64) in which the first 63 bits are one for each of the
3796** first 63 columns of the table or index that are actually used
3797** by the cursor. The high-order bit is set if any column after
3798** the 64th is used.
3799*/
3800case OP_ColumnsUsed: {
3801 VdbeCursor *pC;
3802 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003803 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003804 pC->maskUsed = *(u64*)pOp->p4.pI64;
3805 break;
3806}
3807#endif
3808
drh8af3f772014-07-25 18:01:06 +00003809/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003810** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003811**
danielk1977b790c6c2008-04-18 10:25:24 +00003812** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003813** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003814** to an SQL index, then P3 is the first in an array of P4 registers
3815** that are used as an unpacked index key.
3816**
3817** Reposition cursor P1 so that it points to the smallest entry that
3818** is greater than or equal to the key value. If there are no records
3819** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003820**
drhb1d607d2015-11-05 22:30:54 +00003821** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3822** opcode will always land on a record that equally equals the key, or
3823** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3824** opcode must be followed by an IdxLE opcode with the same arguments.
3825** The IdxLE opcode will be skipped if this opcode succeeds, but the
3826** IdxLE opcode will be used on subsequent loop iterations.
3827**
drh8af3f772014-07-25 18:01:06 +00003828** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003829** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003830** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003831**
drh935850e2014-05-24 17:15:15 +00003832** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003833*/
drh8af3f772014-07-25 18:01:06 +00003834/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003835** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003836**
danielk1977b790c6c2008-04-18 10:25:24 +00003837** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003838** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003839** to an SQL index, then P3 is the first in an array of P4 registers
3840** that are used as an unpacked index key.
3841**
3842** Reposition cursor P1 so that it points to the smallest entry that
3843** is greater than the key value. If there are no records greater than
3844** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003845**
drh8af3f772014-07-25 18:01:06 +00003846** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003847** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003848** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003849**
drh935850e2014-05-24 17:15:15 +00003850** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003851*/
drh8af3f772014-07-25 18:01:06 +00003852/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003853** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003854**
danielk1977b790c6c2008-04-18 10:25:24 +00003855** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003856** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003857** to an SQL index, then P3 is the first in an array of P4 registers
3858** that are used as an unpacked index key.
3859**
3860** Reposition cursor P1 so that it points to the largest entry that
3861** is less than the key value. If there are no records less than
3862** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003863**
drh8af3f772014-07-25 18:01:06 +00003864** This opcode leaves the cursor configured to move in reverse order,
3865** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003866** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003867**
drh935850e2014-05-24 17:15:15 +00003868** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003869*/
drh8af3f772014-07-25 18:01:06 +00003870/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003871** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003872**
danielk1977b790c6c2008-04-18 10:25:24 +00003873** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003874** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003875** to an SQL index, then P3 is the first in an array of P4 registers
3876** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003877**
danielk1977b790c6c2008-04-18 10:25:24 +00003878** Reposition cursor P1 so that it points to the largest entry that
3879** is less than or equal to the key value. If there are no records
3880** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003881**
drh8af3f772014-07-25 18:01:06 +00003882** This opcode leaves the cursor configured to move in reverse order,
3883** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003884** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003885**
drhb1d607d2015-11-05 22:30:54 +00003886** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3887** opcode will always land on a record that equally equals the key, or
3888** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3889** opcode must be followed by an IdxGE opcode with the same arguments.
3890** The IdxGE opcode will be skipped if this opcode succeeds, but the
3891** IdxGE opcode will be used on subsequent loop iterations.
3892**
drh935850e2014-05-24 17:15:15 +00003893** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003894*/
mistachkin758784d2018-07-25 15:12:29 +00003895case OP_SeekLT: /* jump, in3, group */
3896case OP_SeekLE: /* jump, in3, group */
3897case OP_SeekGE: /* jump, in3, group */
3898case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00003899 int res; /* Comparison result */
3900 int oc; /* Opcode */
3901 VdbeCursor *pC; /* The cursor to seek */
3902 UnpackedRecord r; /* The key to seek for */
3903 int nField; /* Number of columns or fields in the key */
3904 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003905 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003906
drh653b82a2009-06-22 11:10:47 +00003907 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003908 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003909 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003910 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003911 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003912 assert( OP_SeekLE == OP_SeekLT+1 );
3913 assert( OP_SeekGE == OP_SeekLT+2 );
3914 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003915 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003916 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003917 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003918 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003919 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003920#ifdef SQLITE_DEBUG
3921 pC->seekOp = pOp->opcode;
3922#endif
drhe0997b32015-03-20 14:57:50 +00003923
drh3da046d2013-11-11 03:24:11 +00003924 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003925 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003926 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3927 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003928
drh3da046d2013-11-11 03:24:11 +00003929 /* The input value in P3 might be of any type: integer, real, string,
3930 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003931 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003932 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003933 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003934 applyNumericAffinity(pIn3, 0);
3935 }
drh3da046d2013-11-11 03:24:11 +00003936 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003937
drh3da046d2013-11-11 03:24:11 +00003938 /* If the P3 value could not be converted into an integer without
3939 ** loss of information, then special processing is required... */
3940 if( (pIn3->flags & MEM_Int)==0 ){
3941 if( (pIn3->flags & MEM_Real)==0 ){
3942 /* If the P3 value cannot be converted into any kind of a number,
3943 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003944 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003945 break;
3946 }
drh959403f2008-12-12 17:56:16 +00003947
danaa1776f2013-11-26 18:22:59 +00003948 /* If the approximation iKey is larger than the actual real search
3949 ** term, substitute >= for > and < for <=. e.g. if the search term
3950 ** is 4.9 and the integer approximation 5:
3951 **
3952 ** (x > 4.9) -> (x >= 5)
3953 ** (x <= 4.9) -> (x < 5)
3954 */
drh74eaba42014-09-18 17:52:15 +00003955 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003956 assert( OP_SeekGE==(OP_SeekGT-1) );
3957 assert( OP_SeekLT==(OP_SeekLE-1) );
3958 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3959 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003960 }
3961
3962 /* If the approximation iKey is smaller than the actual real search
3963 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003964 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003965 assert( OP_SeekLE==(OP_SeekLT+1) );
3966 assert( OP_SeekGT==(OP_SeekGE+1) );
3967 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3968 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003969 }
drh3da046d2013-11-11 03:24:11 +00003970 }
drhc960dcb2015-11-20 19:22:01 +00003971 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003972 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003973 if( rc!=SQLITE_OK ){
3974 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003975 }
drhaa736092009-06-22 00:55:30 +00003976 }else{
drhd6b79462015-11-07 01:19:00 +00003977 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3978 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3979 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3980 */
drhc960dcb2015-11-20 19:22:01 +00003981 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00003982 eqOnly = 1;
3983 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3984 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3985 assert( pOp[1].p1==pOp[0].p1 );
3986 assert( pOp[1].p2==pOp[0].p2 );
3987 assert( pOp[1].p3==pOp[0].p3 );
3988 assert( pOp[1].p4.i==pOp[0].p4.i );
3989 }
3990
drh3da046d2013-11-11 03:24:11 +00003991 nField = pOp->p4.i;
3992 assert( pOp->p4type==P4_INT32 );
3993 assert( nField>0 );
3994 r.pKeyInfo = pC->pKeyInfo;
3995 r.nField = (u16)nField;
3996
3997 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003998 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003999 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004000 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004001 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004002 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004003 */
dan1fed5da2014-02-25 21:01:25 +00004004 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4005 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4006 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4007 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4008 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004009
4010 r.aMem = &aMem[pOp->p3];
4011#ifdef SQLITE_DEBUG
4012 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4013#endif
drh70528d72015-11-05 20:25:09 +00004014 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00004015 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00004016 if( rc!=SQLITE_OK ){
4017 goto abort_due_to_error;
4018 }
drhb1d607d2015-11-05 22:30:54 +00004019 if( eqOnly && r.eqSeen==0 ){
4020 assert( res!=0 );
4021 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004022 }
drh3da046d2013-11-11 03:24:11 +00004023 }
4024 pC->deferredMoveto = 0;
4025 pC->cacheStatus = CACHE_STALE;
4026#ifdef SQLITE_TEST
4027 sqlite3_search_count++;
4028#endif
drh4a1d3652014-02-14 15:13:36 +00004029 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4030 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004031 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004032 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4033 if( rc!=SQLITE_OK ){
4034 if( rc==SQLITE_DONE ){
4035 rc = SQLITE_OK;
4036 res = 1;
4037 }else{
4038 goto abort_due_to_error;
4039 }
4040 }
drh3da046d2013-11-11 03:24:11 +00004041 }else{
4042 res = 0;
4043 }
4044 }else{
drh4a1d3652014-02-14 15:13:36 +00004045 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4046 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004047 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004048 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4049 if( rc!=SQLITE_OK ){
4050 if( rc==SQLITE_DONE ){
4051 rc = SQLITE_OK;
4052 res = 1;
4053 }else{
4054 goto abort_due_to_error;
4055 }
4056 }
drh3da046d2013-11-11 03:24:11 +00004057 }else{
4058 /* res might be negative because the table is empty. Check to
4059 ** see if this is the case.
4060 */
drhc960dcb2015-11-20 19:22:01 +00004061 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004062 }
4063 }
drhb1d607d2015-11-05 22:30:54 +00004064seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004065 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004066 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004067 if( res ){
drhf56fa462015-04-13 21:39:54 +00004068 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004069 }else if( eqOnly ){
4070 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4071 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004072 }
drh5e00f6c2001-09-13 13:46:56 +00004073 break;
4074}
dan71c57db2016-07-09 20:23:55 +00004075
drh8c2b6d72018-06-05 20:45:20 +00004076/* Opcode: SeekHit P1 P2 * * *
4077** Synopsis: seekHit=P2
4078**
4079** Set the seekHit flag on cursor P1 to the value in P2.
4080** The seekHit flag is used by the IfNoHope opcode.
4081**
4082** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4083** either 0 or 1.
4084*/
4085case OP_SeekHit: {
4086 VdbeCursor *pC;
4087 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4088 pC = p->apCsr[pOp->p1];
4089 assert( pC!=0 );
4090 assert( pOp->p2==0 || pOp->p2==1 );
4091 pC->seekHit = pOp->p2 & 1;
4092 break;
4093}
4094
drh8cff69d2009-11-12 19:59:44 +00004095/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004096** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004097**
drh8cff69d2009-11-12 19:59:44 +00004098** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4099** P4>0 then register P3 is the first of P4 registers that form an unpacked
4100** record.
4101**
4102** Cursor P1 is on an index btree. If the record identified by P3 and P4
4103** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004104** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004105**
drhcefc87f2014-08-01 01:40:33 +00004106** This operation leaves the cursor in a state where it can be
4107** advanced in the forward direction. The Next instruction will work,
4108** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004109**
drh6f225d02013-10-26 13:36:51 +00004110** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004111*/
drh8cff69d2009-11-12 19:59:44 +00004112/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004113** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004114**
drh8cff69d2009-11-12 19:59:44 +00004115** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4116** P4>0 then register P3 is the first of P4 registers that form an unpacked
4117** record.
4118**
4119** Cursor P1 is on an index btree. If the record identified by P3 and P4
4120** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4121** does contain an entry whose prefix matches the P3/P4 record then control
4122** falls through to the next instruction and P1 is left pointing at the
4123** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004124**
drh8af3f772014-07-25 18:01:06 +00004125** This operation leaves the cursor in a state where it cannot be
4126** advanced in either direction. In other words, the Next and Prev
4127** opcodes do not work after this operation.
4128**
drh8c2b6d72018-06-05 20:45:20 +00004129** See also: Found, NotExists, NoConflict, IfNoHope
4130*/
4131/* Opcode: IfNoHope P1 P2 P3 P4 *
4132** Synopsis: key=r[P3@P4]
4133**
4134** Register P3 is the first of P4 registers that form an unpacked
4135** record.
4136**
4137** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4138** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4139** check to see if there is any entry in P1 that matches the
4140** prefix identified by P3 and P4. If no entry matches the prefix,
4141** jump to P2. Otherwise fall through.
4142**
4143** This opcode behaves like OP_NotFound if the seekHit
4144** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4145**
4146** This opcode is used in IN clause processing for a multi-column key.
4147** If an IN clause is attached to an element of the key other than the
4148** left-most element, and if there are no matches on the most recent
4149** seek over the whole key, then it might be that one of the key element
4150** to the left is prohibiting a match, and hence there is "no hope" of
4151** any match regardless of how many IN clause elements are checked.
4152** In such a case, we abandon the IN clause search early, using this
4153** opcode. The opcode name comes from the fact that the
4154** jump is taken if there is "no hope" of achieving a match.
4155**
4156** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004157*/
drh6f225d02013-10-26 13:36:51 +00004158/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004159** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004160**
4161** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4162** P4>0 then register P3 is the first of P4 registers that form an unpacked
4163** record.
4164**
4165** Cursor P1 is on an index btree. If the record identified by P3 and P4
4166** contains any NULL value, jump immediately to P2. If all terms of the
4167** record are not-NULL then a check is done to determine if any row in the
4168** P1 index btree has a matching key prefix. If there are no matches, jump
4169** immediately to P2. If there is a match, fall through and leave the P1
4170** cursor pointing to the matching row.
4171**
4172** This opcode is similar to OP_NotFound with the exceptions that the
4173** branch is always taken if any part of the search key input is NULL.
4174**
drh8af3f772014-07-25 18:01:06 +00004175** This operation leaves the cursor in a state where it cannot be
4176** advanced in either direction. In other words, the Next and Prev
4177** opcodes do not work after this operation.
4178**
drh6f225d02013-10-26 13:36:51 +00004179** See also: NotFound, Found, NotExists
4180*/
drh8c2b6d72018-06-05 20:45:20 +00004181case OP_IfNoHope: { /* jump, in3 */
4182 VdbeCursor *pC;
4183 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4184 pC = p->apCsr[pOp->p1];
4185 assert( pC!=0 );
4186 if( pC->seekHit ) break;
4187 /* Fall through into OP_NotFound */
4188}
drh6f225d02013-10-26 13:36:51 +00004189case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004190case OP_NotFound: /* jump, in3 */
4191case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004192 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004193 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004194 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004195 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004196 int res;
drha582b012016-12-21 19:45:54 +00004197 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004198 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004199 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004200
dan0ff297e2009-09-25 17:03:14 +00004201#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004202 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004203#endif
4204
drhaa736092009-06-22 00:55:30 +00004205 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004206 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004207 pC = p->apCsr[pOp->p1];
4208 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004209#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004210 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004211#endif
drh3c657212009-11-17 23:59:58 +00004212 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004213 assert( pC->eCurType==CURTYPE_BTREE );
4214 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004215 assert( pC->isTable==0 );
4216 if( pOp->p4.i>0 ){
4217 r.pKeyInfo = pC->pKeyInfo;
4218 r.nField = (u16)pOp->p4.i;
4219 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004220#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004221 for(ii=0; ii<r.nField; ii++){
4222 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004223 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004224 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004225 }
drh8aaf7bc2016-09-20 01:19:18 +00004226#endif
drh3da046d2013-11-11 03:24:11 +00004227 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004228 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004229 }else{
drhe46515b2017-05-19 22:51:00 +00004230 assert( pIn3->flags & MEM_Blob );
4231 rc = ExpandBlob(pIn3);
4232 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4233 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004234 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004235 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004236 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004237 }
dan1fed5da2014-02-25 21:01:25 +00004238 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004239 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004240 if( pOp->opcode==OP_NoConflict ){
4241 /* For the OP_NoConflict opcode, take the jump if any of the
4242 ** input fields are NULL, since any key with a NULL will not
4243 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004244 for(ii=0; ii<pIdxKey->nField; ii++){
4245 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004246 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004247 break;
drh6f225d02013-10-26 13:36:51 +00004248 }
4249 }
drh5e00f6c2001-09-13 13:46:56 +00004250 }
drhc960dcb2015-11-20 19:22:01 +00004251 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004252 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004253 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004254 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004255 }
4256 pC->seekResult = res;
4257 alreadyExists = (res==0);
4258 pC->nullRow = 1-alreadyExists;
4259 pC->deferredMoveto = 0;
4260 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004261 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004262 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004263 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004264 }else{
drhf56fa462015-04-13 21:39:54 +00004265 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4266 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004267 }
drh5e00f6c2001-09-13 13:46:56 +00004268 break;
4269}
4270
drheeb95652016-05-26 20:56:38 +00004271/* Opcode: SeekRowid P1 P2 P3 * *
4272** Synopsis: intkey=r[P3]
4273**
4274** P1 is the index of a cursor open on an SQL table btree (with integer
4275** keys). If register P3 does not contain an integer or if P1 does not
4276** contain a record with rowid P3 then jump immediately to P2.
4277** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4278** a record with rowid P3 then
4279** leave the cursor pointing at that record and fall through to the next
4280** instruction.
4281**
4282** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4283** the P3 register must be guaranteed to contain an integer value. With this
4284** opcode, register P3 might not contain an integer.
4285**
4286** The OP_NotFound opcode performs the same operation on index btrees
4287** (with arbitrary multi-value keys).
4288**
4289** This opcode leaves the cursor in a state where it cannot be advanced
4290** in either direction. In other words, the Next and Prev opcodes will
4291** not work following this opcode.
4292**
4293** See also: Found, NotFound, NoConflict, SeekRowid
4294*/
drh9cbf3422008-01-17 16:22:13 +00004295/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004296** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004297**
drh261c02d2013-10-25 14:46:15 +00004298** P1 is the index of a cursor open on an SQL table btree (with integer
4299** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004300** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4301** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4302** leave the cursor pointing at that record and fall through to the next
4303** instruction.
drh6b125452002-01-28 15:53:03 +00004304**
drheeb95652016-05-26 20:56:38 +00004305** The OP_SeekRowid opcode performs the same operation but also allows the
4306** P3 register to contain a non-integer value, in which case the jump is
4307** always taken. This opcode requires that P3 always contain an integer.
4308**
drh261c02d2013-10-25 14:46:15 +00004309** The OP_NotFound opcode performs the same operation on index btrees
4310** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004311**
drh8af3f772014-07-25 18:01:06 +00004312** This opcode leaves the cursor in a state where it cannot be advanced
4313** in either direction. In other words, the Next and Prev opcodes will
4314** not work following this opcode.
4315**
drheeb95652016-05-26 20:56:38 +00004316** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004317*/
drheeb95652016-05-26 20:56:38 +00004318case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004319 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004320 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004321 int res;
4322 u64 iKey;
4323
drh3c657212009-11-17 23:59:58 +00004324 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004325 if( (pIn3->flags & MEM_Int)==0 ){
4326 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
4327 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
4328 }
4329 /* Fall through into OP_NotExists */
4330case OP_NotExists: /* jump, in3 */
4331 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00004332 assert( pIn3->flags & MEM_Int );
4333 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
drh3da046d2013-11-11 03:24:11 +00005532 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00005533#endif
drh2dc06482013-12-11 00:59:10 +00005534 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005535 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005536 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5537 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5538 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005539 res = -res;
5540 }else{
drh4a1d3652014-02-14 15:13:36 +00005541 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005542 res++;
5543 }
drh688852a2014-02-17 22:40:43 +00005544 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005545 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005546 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005547 break;
5548}
5549
drh98757152008-01-09 23:04:12 +00005550/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005551**
5552** Delete an entire database table or index whose root page in the database
5553** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005554**
drh98757152008-01-09 23:04:12 +00005555** The table being destroyed is in the main database file if P3==0. If
5556** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005557** that is used to store tables create using CREATE TEMPORARY TABLE.
5558**
drh205f48e2004-11-05 00:43:11 +00005559** If AUTOVACUUM is enabled then it is possible that another root page
5560** might be moved into the newly deleted root page in order to keep all
5561** root pages contiguous at the beginning of the database. The former
5562** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005563** is stored in register P2. If no page movement was required (because the
5564** table being dropped was already the last one in the database) then a
5565** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5566** is stored in register P2.
5567**
5568** This opcode throws an error if there are any active reader VMs when
5569** it is invoked. This is done to avoid the difficulty associated with
5570** updating existing cursors when a root page is moved in an AUTOVACUUM
5571** database. This error is thrown even if the database is not an AUTOVACUUM
5572** db in order to avoid introducing an incompatibility between autovacuum
5573** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005574**
drhb19a2bc2001-09-16 00:13:26 +00005575** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005576*/
drh27a348c2015-04-13 19:14:06 +00005577case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005578 int iMoved;
drh856c1032009-06-02 15:21:42 +00005579 int iDb;
drh3a949872012-09-18 13:20:13 +00005580
drh4031baf2018-05-28 17:31:20 +00005581 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005582 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005583 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005584 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005585 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005586 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005587 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005588 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005589 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005590 }else{
drh856c1032009-06-02 15:21:42 +00005591 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005592 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005593 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005594 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005595 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005596 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005597 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005598#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005599 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005600 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5601 /* All OP_Destroy operations occur on the same btree */
5602 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5603 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005604 }
drh3765df42006-06-28 18:18:09 +00005605#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005606 }
drh5e00f6c2001-09-13 13:46:56 +00005607 break;
5608}
5609
danielk1977c7af4842008-10-27 13:59:33 +00005610/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005611**
5612** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005613** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005614** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005615**
drhf57b3392001-10-08 13:22:32 +00005616** The table being clear is in the main database file if P2==0. If
5617** P2==1 then the table to be clear is in the auxiliary database file
5618** that is used to store tables create using CREATE TEMPORARY TABLE.
5619**
shanebe217792009-03-05 04:20:31 +00005620** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005621** intkey table (an SQL table, not an index). In this case the row change
5622** count is incremented by the number of rows in the table being cleared.
5623** If P3 is greater than zero, then the value stored in register P3 is
5624** also incremented by the number of rows in the table being cleared.
5625**
drhb19a2bc2001-09-16 00:13:26 +00005626** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005627*/
drh9cbf3422008-01-17 16:22:13 +00005628case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005629 int nChange;
5630
drh4031baf2018-05-28 17:31:20 +00005631 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005632 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005633 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005634 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005635 rc = sqlite3BtreeClearTable(
5636 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5637 );
5638 if( pOp->p3 ){
5639 p->nChange += nChange;
5640 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005641 assert( memIsValid(&aMem[pOp->p3]) );
5642 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005643 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005644 }
5645 }
drh9467abf2016-02-17 18:44:11 +00005646 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005647 break;
5648}
5649
drh65ea12c2014-03-19 17:41:36 +00005650/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005651**
drh65ea12c2014-03-19 17:41:36 +00005652** Delete all contents from the ephemeral table or sorter
5653** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005654**
drh65ea12c2014-03-19 17:41:36 +00005655** This opcode only works for cursors used for sorting and
5656** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005657*/
drh65ea12c2014-03-19 17:41:36 +00005658case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005659 VdbeCursor *pC;
5660
5661 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5662 pC = p->apCsr[pOp->p1];
5663 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005664 if( isSorter(pC) ){
5665 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005666 }else{
drhc960dcb2015-11-20 19:22:01 +00005667 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005668 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005669 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005670 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005671 }
drh079a3072014-03-19 14:10:55 +00005672 break;
5673}
5674
drh0f3f7662017-08-18 14:34:28 +00005675/* Opcode: CreateBtree P1 P2 P3 * *
5676** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005677**
drh0f3f7662017-08-18 14:34:28 +00005678** Allocate a new b-tree in the main database file if P1==0 or in the
5679** TEMP database file if P1==1 or in an attached database if
5680** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00005681** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00005682** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005683*/
drh0f3f7662017-08-18 14:34:28 +00005684case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005685 int pgno;
drh234c39d2004-07-24 03:30:47 +00005686 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005687
drh4031baf2018-05-28 17:31:20 +00005688 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00005689 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005690 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005691 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005692 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005693 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005694 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005695 pDb = &db->aDb[pOp->p1];
5696 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005697 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005698 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005699 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005700 break;
5701}
5702
drh4a54bb52017-02-18 15:58:52 +00005703/* Opcode: SqlExec * * * P4 *
5704**
5705** Run the SQL statement or statements specified in the P4 string.
5706*/
5707case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00005708 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00005709 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005710 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005711 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005712 if( rc ) goto abort_due_to_error;
5713 break;
5714}
5715
drh22645842011-03-24 01:34:03 +00005716/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005717**
5718** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005719** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005720**
5721** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005722** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005723*/
drh9cbf3422008-01-17 16:22:13 +00005724case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005725 int iDb;
5726 const char *zMaster;
5727 char *zSql;
5728 InitData initData;
5729
drhbdaec522011-04-04 00:14:43 +00005730 /* Any prepared statement that invokes this opcode will hold mutexes
5731 ** on every btree. This is a prerequisite for invoking
5732 ** sqlite3InitCallback().
5733 */
5734#ifdef SQLITE_DEBUG
5735 for(iDb=0; iDb<db->nDb; iDb++){
5736 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5737 }
5738#endif
drhbdaec522011-04-04 00:14:43 +00005739
drh856c1032009-06-02 15:21:42 +00005740 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005741 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005742 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005743 /* Used to be a conditional */ {
drhe0a04a32016-12-16 01:00:21 +00005744 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005745 initData.db = db;
5746 initData.iDb = pOp->p1;
5747 initData.pzErrMsg = &p->zErrMsg;
5748 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005749 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005750 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005751 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005752 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005753 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005754 assert( db->init.busy==0 );
5755 db->init.busy = 1;
5756 initData.rc = SQLITE_OK;
5757 assert( !db->mallocFailed );
5758 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5759 if( rc==SQLITE_OK ) rc = initData.rc;
drhdbd6a7d2017-04-05 12:39:49 +00005760 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005761 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005762 }
drh3c23a882007-01-09 14:01:13 +00005763 }
drh9467abf2016-02-17 18:44:11 +00005764 if( rc ){
5765 sqlite3ResetAllSchemasOfConnection(db);
5766 if( rc==SQLITE_NOMEM ){
5767 goto no_mem;
5768 }
5769 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005770 }
drh234c39d2004-07-24 03:30:47 +00005771 break;
5772}
5773
drh8bfdf722009-06-19 14:06:03 +00005774#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005775/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005776**
5777** Read the sqlite_stat1 table for database P1 and load the content
5778** of that table into the internal index hash table. This will cause
5779** the analysis to be used when preparing all subsequent queries.
5780*/
drh9cbf3422008-01-17 16:22:13 +00005781case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005782 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5783 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005784 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005785 break;
5786}
drh8bfdf722009-06-19 14:06:03 +00005787#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005788
drh98757152008-01-09 23:04:12 +00005789/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005790**
5791** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005792** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005793** is dropped from disk (using the Destroy opcode) in order to keep
5794** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005795** schema consistent with what is on disk.
5796*/
drh9cbf3422008-01-17 16:22:13 +00005797case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00005798 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005799 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005800 break;
5801}
5802
drh98757152008-01-09 23:04:12 +00005803/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005804**
5805** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005806** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005807** is dropped from disk (using the Destroy opcode)
5808** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005809** schema consistent with what is on disk.
5810*/
drh9cbf3422008-01-17 16:22:13 +00005811case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00005812 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005813 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005814 break;
5815}
5816
drh98757152008-01-09 23:04:12 +00005817/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005818**
5819** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005820** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005821** is dropped from disk (using the Destroy opcode) in order to keep
5822** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005823** schema consistent with what is on disk.
5824*/
drh9cbf3422008-01-17 16:22:13 +00005825case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00005826 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005827 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005828 break;
5829}
5830
drh234c39d2004-07-24 03:30:47 +00005831
drhb7f91642004-10-31 02:22:47 +00005832#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005833/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005834**
drh98757152008-01-09 23:04:12 +00005835** Do an analysis of the currently open database. Store in
5836** register P1 the text of an error message describing any problems.
5837** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005838**
drh66accfc2017-02-22 18:04:42 +00005839** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005840** At most reg(P3) errors will be reported.
5841** In other words, the analysis stops as soon as reg(P1) errors are
5842** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005843**
drh98968b22016-03-15 22:00:39 +00005844** The root page numbers of all tables in the database are integers
5845** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005846**
drh98757152008-01-09 23:04:12 +00005847** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005848** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005849**
drh1dcdbc02007-01-27 02:24:54 +00005850** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005851*/
drhaaab5722002-02-19 13:39:21 +00005852case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005853 int nRoot; /* Number of tables to check. (Number of root pages.) */
5854 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005855 int nErr; /* Number of errors reported */
5856 char *z; /* Text of the error report */
5857 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005858
drh1713afb2013-06-28 01:24:57 +00005859 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005860 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005861 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005862 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00005863 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00005864 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005865 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005866 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005867 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005868 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005869 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005870 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drhb5c10632017-09-21 00:49:15 +00005871 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00005872 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005873 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005874 if( nErr==0 ){
5875 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005876 }else if( z==0 ){
5877 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005878 }else{
drh66accfc2017-02-22 18:04:42 +00005879 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005880 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005881 }
drhb7654112008-01-12 12:48:07 +00005882 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005883 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005884 break;
5885}
drhb7f91642004-10-31 02:22:47 +00005886#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005887
drh3d4501e2008-12-04 20:40:10 +00005888/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005889** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005890**
drhbb6783b2017-04-29 18:02:49 +00005891** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00005892** held in register P1.
5893**
5894** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005895*/
drh93952eb2009-11-13 19:43:43 +00005896case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005897 pIn1 = &aMem[pOp->p1];
5898 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005899 assert( (pIn2->flags & MEM_Int)!=0 );
5900 if( (pIn1->flags & MEM_RowSet)==0 ){
5901 sqlite3VdbeMemSetRowSet(pIn1);
5902 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005903 }
drh93952eb2009-11-13 19:43:43 +00005904 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005905 break;
5906}
5907
5908/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005909** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005910**
drhbb6783b2017-04-29 18:02:49 +00005911** Extract the smallest value from the RowSet object in P1
5912** and put that value into register P3.
5913** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00005914** unchanged and jump to instruction P2.
5915*/
drh93952eb2009-11-13 19:43:43 +00005916case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005917 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005918
drh3c657212009-11-17 23:59:58 +00005919 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005920 if( (pIn1->flags & MEM_RowSet)==0
5921 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005922 ){
5923 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005924 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005925 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005926 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005927 }else{
5928 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005929 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005930 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005931 }
drh49afe3a2013-07-10 03:05:14 +00005932 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005933}
5934
drh1b26c7c2009-04-22 02:15:47 +00005935/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005936** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005937**
drhade97602009-04-21 15:05:18 +00005938** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005939** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005940** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005941** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005942** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005943**
drhbb6783b2017-04-29 18:02:49 +00005944** The RowSet object is optimized for the case where sets of integers
5945** are inserted in distinct phases, which each set contains no duplicates.
5946** Each set is identified by a unique P4 value. The first set
5947** must have P4==0, the final set must have P4==-1, and for all other sets
5948** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00005949**
5950** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00005951** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005952** (b) when P4==-1 there is no need to insert the value, as it will
5953** never be tested for, and (c) when a value that is part of set X is
5954** inserted, there is no need to search to see if the same value was
5955** previously inserted as part of set X (only if it was previously
5956** inserted as part of some other set).
5957*/
drh1b26c7c2009-04-22 02:15:47 +00005958case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005959 int iSet;
5960 int exists;
5961
drh3c657212009-11-17 23:59:58 +00005962 pIn1 = &aMem[pOp->p1];
5963 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005964 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005965 assert( pIn3->flags&MEM_Int );
5966
drh1b26c7c2009-04-22 02:15:47 +00005967 /* If there is anything other than a rowset object in memory cell P1,
5968 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005969 */
drh733bf1b2009-04-22 00:47:00 +00005970 if( (pIn1->flags & MEM_RowSet)==0 ){
5971 sqlite3VdbeMemSetRowSet(pIn1);
5972 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005973 }
5974
5975 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005976 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005977 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005978 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005979 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005980 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005981 }
5982 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005983 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005984 }
5985 break;
5986}
5987
drh5e00f6c2001-09-13 13:46:56 +00005988
danielk197793758c82005-01-21 08:13:14 +00005989#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005990
drh0fd61352014-02-07 02:29:45 +00005991/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005992**
dan76d462e2009-08-30 11:42:51 +00005993** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005994**
dan76d462e2009-08-30 11:42:51 +00005995** P1 contains the address of the memory cell that contains the first memory
5996** cell in an array of values used as arguments to the sub-program. P2
5997** contains the address to jump to if the sub-program throws an IGNORE
5998** exception using the RAISE() function. Register P3 contains the address
5999** of a memory cell in this (the parent) VM that is used to allocate the
6000** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006001**
6002** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006003**
6004** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006005*/
dan76d462e2009-08-30 11:42:51 +00006006case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006007 int nMem; /* Number of memory registers for sub-program */
6008 int nByte; /* Bytes of runtime space required for sub-program */
6009 Mem *pRt; /* Register to allocate runtime space */
6010 Mem *pMem; /* Used to iterate through memory cells */
6011 Mem *pEnd; /* Last memory cell in new array */
6012 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6013 SubProgram *pProgram; /* Sub-program to execute */
6014 void *t; /* Token identifying trigger */
6015
6016 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006017 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006018 assert( pProgram->nOp>0 );
6019
dan1da40a32009-09-19 17:00:31 +00006020 /* If the p5 flag is clear, then recursive invocation of triggers is
6021 ** disabled for backwards compatibility (p5 is set if this sub-program
6022 ** is really a trigger, not a foreign key action, and the flag set
6023 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006024 **
6025 ** It is recursive invocation of triggers, at the SQL level, that is
6026 ** disabled. In some cases a single trigger may generate more than one
6027 ** SubProgram (if the trigger may be executed with more than one different
6028 ** ON CONFLICT algorithm). SubProgram structures associated with a
6029 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006030 ** variable. */
6031 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006032 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006033 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6034 if( pFrame ) break;
6035 }
6036
danf5894502009-10-07 18:41:19 +00006037 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006038 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006039 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006040 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006041 }
6042
6043 /* Register pRt is used to store the memory required to save the state
6044 ** of the current program, and the memory required at runtime to execute
6045 ** the trigger program. If this trigger has been fired before, then pRt
6046 ** is already allocated. Otherwise, it must be initialized. */
6047 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00006048 /* SubProgram.nMem is set to the number of memory cells used by the
6049 ** program stored in SubProgram.aOp. As well as these, one memory
6050 ** cell is required for each cursor used by the program. Set local
6051 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6052 */
dan65a7cd12009-09-01 12:16:01 +00006053 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006054 assert( nMem>0 );
6055 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006056 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006057 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006058 + pProgram->nCsr * sizeof(VdbeCursor*)
6059 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006060 pFrame = sqlite3DbMallocZero(db, nByte);
6061 if( !pFrame ){
6062 goto no_mem;
6063 }
6064 sqlite3VdbeMemRelease(pRt);
6065 pRt->flags = MEM_Frame;
6066 pRt->u.pFrame = pFrame;
6067
6068 pFrame->v = p;
6069 pFrame->nChildMem = nMem;
6070 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006071 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006072 pFrame->aMem = p->aMem;
6073 pFrame->nMem = p->nMem;
6074 pFrame->apCsr = p->apCsr;
6075 pFrame->nCursor = p->nCursor;
6076 pFrame->aOp = p->aOp;
6077 pFrame->nOp = p->nOp;
6078 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006079#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006080 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006081#endif
dan165921a2009-08-28 18:53:45 +00006082
6083 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6084 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006085 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006086 pMem->db = db;
6087 }
6088 }else{
6089 pFrame = pRt->u.pFrame;
drh9f6168b2016-03-19 23:32:58 +00006090 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6091 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006092 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006093 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006094 }
6095
6096 p->nFrame++;
6097 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006098 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006099 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006100 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006101 assert( pFrame->pAuxData==0 );
6102 pFrame->pAuxData = p->pAuxData;
6103 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006104 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006105 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006106 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006107 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006108 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006109 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006110 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006111 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006112 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006113 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006114#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006115 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006116#endif
drhf56fa462015-04-13 21:39:54 +00006117 pOp = &aOp[-1];
dan165921a2009-08-28 18:53:45 +00006118
6119 break;
6120}
6121
dan76d462e2009-08-30 11:42:51 +00006122/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006123**
dan76d462e2009-08-30 11:42:51 +00006124** This opcode is only ever present in sub-programs called via the
6125** OP_Program instruction. Copy a value currently stored in a memory
6126** cell of the calling (parent) frame to cell P2 in the current frames
6127** address space. This is used by trigger programs to access the new.*
6128** and old.* values.
dan165921a2009-08-28 18:53:45 +00006129**
dan76d462e2009-08-30 11:42:51 +00006130** The address of the cell in the parent frame is determined by adding
6131** the value of the P1 argument to the value of the P1 argument to the
6132** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006133*/
drh27a348c2015-04-13 19:14:06 +00006134case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006135 VdbeFrame *pFrame;
6136 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006137 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006138 pFrame = p->pFrame;
6139 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006140 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6141 break;
6142}
6143
danielk197793758c82005-01-21 08:13:14 +00006144#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006145
dan1da40a32009-09-19 17:00:31 +00006146#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006147/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006148** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006149**
dan0ff297e2009-09-25 17:03:14 +00006150** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6151** If P1 is non-zero, the database constraint counter is incremented
6152** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006153** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006154*/
dan32b09f22009-09-23 17:29:59 +00006155case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006156 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006157 db->nDeferredImmCons += pOp->p2;
6158 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006159 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006160 }else{
dan0ff297e2009-09-25 17:03:14 +00006161 p->nFkConstraint += pOp->p2;
6162 }
6163 break;
6164}
6165
6166/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006167** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006168**
6169** This opcode tests if a foreign key constraint-counter is currently zero.
6170** If so, jump to instruction P2. Otherwise, fall through to the next
6171** instruction.
6172**
6173** If P1 is non-zero, then the jump is taken if the database constraint-counter
6174** is zero (the one that counts deferred constraint violations). If P1 is
6175** zero, the jump is taken if the statement constraint-counter is zero
6176** (immediate foreign key constraint violations).
6177*/
6178case OP_FkIfZero: { /* jump */
6179 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006180 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006181 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006182 }else{
drh688852a2014-02-17 22:40:43 +00006183 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006184 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006185 }
dan1da40a32009-09-19 17:00:31 +00006186 break;
6187}
6188#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6189
drh205f48e2004-11-05 00:43:11 +00006190#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006191/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006192** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006193**
dan76d462e2009-08-30 11:42:51 +00006194** P1 is a register in the root frame of this VM (the root frame is
6195** different from the current frame if this instruction is being executed
6196** within a sub-program). Set the value of register P1 to the maximum of
6197** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006198**
6199** This instruction throws an error if the memory cell is not initially
6200** an integer.
6201*/
dan76d462e2009-08-30 11:42:51 +00006202case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006203 VdbeFrame *pFrame;
6204 if( p->pFrame ){
6205 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6206 pIn1 = &pFrame->aMem[pOp->p1];
6207 }else{
drha6c2ed92009-11-14 23:22:23 +00006208 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006209 }
drh2b4ded92010-09-27 21:09:31 +00006210 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006211 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006212 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006213 sqlite3VdbeMemIntegerify(pIn2);
6214 if( pIn1->u.i<pIn2->u.i){
6215 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006216 }
6217 break;
6218}
6219#endif /* SQLITE_OMIT_AUTOINCREMENT */
6220
drh8b0cf382015-10-06 21:07:06 +00006221/* Opcode: IfPos P1 P2 P3 * *
6222** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006223**
drh16897072015-03-07 00:57:37 +00006224** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006225** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006226** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006227**
drh16897072015-03-07 00:57:37 +00006228** If the initial value of register P1 is less than 1, then the
6229** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006230*/
drh9cbf3422008-01-17 16:22:13 +00006231case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006232 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006233 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006234 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006235 if( pIn1->u.i>0 ){
6236 pIn1->u.i -= pOp->p3;
6237 goto jump_to_p2;
6238 }
drhec7429a2005-10-06 16:53:14 +00006239 break;
6240}
6241
drhcc2fa4c2016-01-25 15:57:29 +00006242/* Opcode: OffsetLimit P1 P2 P3 * *
6243** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006244**
drhcc2fa4c2016-01-25 15:57:29 +00006245** This opcode performs a commonly used computation associated with
6246** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6247** holds the offset counter. The opcode computes the combined value
6248** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6249** value computed is the total number of rows that will need to be
6250** visited in order to complete the query.
6251**
6252** If r[P3] is zero or negative, that means there is no OFFSET
6253** and r[P2] is set to be the value of the LIMIT, r[P1].
6254**
6255** if r[P1] is zero or negative, that means there is no LIMIT
6256** and r[P2] is set to -1.
6257**
6258** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006259*/
drhcc2fa4c2016-01-25 15:57:29 +00006260case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006261 i64 x;
drh3c657212009-11-17 23:59:58 +00006262 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006263 pIn3 = &aMem[pOp->p3];
6264 pOut = out2Prerelease(p, pOp);
6265 assert( pIn1->flags & MEM_Int );
6266 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006267 x = pIn1->u.i;
6268 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6269 /* If the LIMIT is less than or equal to zero, loop forever. This
6270 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6271 ** also loop forever. This is undocumented. In fact, one could argue
6272 ** that the loop should terminate. But assuming 1 billion iterations
6273 ** per second (far exceeding the capabilities of any current hardware)
6274 ** it would take nearly 300 years to actually reach the limit. So
6275 ** looping forever is a reasonable approximation. */
6276 pOut->u.i = -1;
6277 }else{
6278 pOut->u.i = x;
6279 }
drh15007a92006-01-08 18:10:17 +00006280 break;
6281}
6282
drhf99dd352016-12-18 17:42:00 +00006283/* Opcode: IfNotZero P1 P2 * * *
6284** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006285**
drh16897072015-03-07 00:57:37 +00006286** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006287** initially greater than zero, then decrement the value in register P1.
6288** If it is non-zero (negative or positive) and then also jump to P2.
6289** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006290*/
drh16897072015-03-07 00:57:37 +00006291case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006292 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006293 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006294 VdbeBranchTaken(pIn1->u.i<0, 2);
6295 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006296 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006297 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006298 }
6299 break;
6300}
6301
6302/* Opcode: DecrJumpZero P1 P2 * * *
6303** Synopsis: if (--r[P1])==0 goto P2
6304**
drhab5be2e2016-11-30 05:08:59 +00006305** Register P1 must hold an integer. Decrement the value in P1
6306** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006307*/
6308case OP_DecrJumpZero: { /* jump, in1 */
6309 pIn1 = &aMem[pOp->p1];
6310 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006311 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6312 VdbeBranchTaken(pIn1->u.i==0, 2);
6313 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006314 break;
6315}
6316
drh16897072015-03-07 00:57:37 +00006317
drh8f26da62018-07-05 21:22:57 +00006318/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006319** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006320**
drh8f26da62018-07-05 21:22:57 +00006321** Execute the xStep function for an aggregate.
6322** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00006323** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006324** accumulator.
drhe5095352002-02-24 03:25:14 +00006325**
drh98757152008-01-09 23:04:12 +00006326** The P5 arguments are taken from register P2 and its
6327** successors.
drhe5095352002-02-24 03:25:14 +00006328*/
drh8f26da62018-07-05 21:22:57 +00006329/* Opcode: AggInverse * P2 P3 P4 P5
6330** Synopsis: accum=r[P3] inverse(r[P2@P5])
6331**
6332** Execute the xInverse function for an aggregate.
6333** The function has P5 arguments. P4 is a pointer to the
6334** FuncDef structure that specifies the function. Register P3 is the
6335** accumulator.
6336**
6337** The P5 arguments are taken from register P2 and its
6338** successors.
6339*/
6340/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006341** Synopsis: accum=r[P3] step(r[P2@P5])
6342**
dan9a947222018-06-14 19:06:36 +00006343** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6344** aggregate. The function has P5 arguments. P4 is a pointer to the
6345** FuncDef structure that specifies the function. Register P3 is the
6346** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006347**
6348** The P5 arguments are taken from register P2 and its
6349** successors.
6350**
6351** This opcode is initially coded as OP_AggStep0. On first evaluation,
6352** the FuncDef stored in P4 is converted into an sqlite3_context and
6353** the opcode is changed. In this way, the initialization of the
6354** sqlite3_context only happens once, instead of on each call to the
6355** step function.
6356*/
drh8f26da62018-07-05 21:22:57 +00006357case OP_AggInverse:
6358case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00006359 int n;
drh9c7c9132015-06-26 18:16:52 +00006360 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006361
drh9c7c9132015-06-26 18:16:52 +00006362 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006363 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006364 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6365 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006366 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006367 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6368 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006369 if( pCtx==0 ) goto no_mem;
6370 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006371 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6372 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006373 pCtx->pFunc = pOp->p4.pFunc;
6374 pCtx->iOp = (int)(pOp - aOp);
6375 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006376 pCtx->skipFlag = 0;
6377 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006378 pCtx->argc = n;
6379 pOp->p4type = P4_FUNCCTX;
6380 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00006381
6382 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00006383 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00006384
drh8f26da62018-07-05 21:22:57 +00006385 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00006386 /* Fall through into OP_AggStep */
6387}
drh8f26da62018-07-05 21:22:57 +00006388case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00006389 int i;
6390 sqlite3_context *pCtx;
6391 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006392
6393 assert( pOp->p4type==P4_FUNCCTX );
6394 pCtx = pOp->p4.pCtx;
6395 pMem = &aMem[pOp->p3];
6396
drh2c885d02018-07-07 19:36:04 +00006397#ifdef SQLITE_DEBUG
6398 if( pOp->p1 ){
6399 /* This is an OP_AggInverse call. Verify that xStep has always
6400 ** been called at least once prior to any xInverse call. */
6401 assert( pMem->uTemp==0x1122e0e3 );
6402 }else{
6403 /* This is an OP_AggStep call. Mark it as such. */
6404 pMem->uTemp = 0x1122e0e3;
6405 }
6406#endif
6407
drh9c7c9132015-06-26 18:16:52 +00006408 /* If this function is inside of a trigger, the register array in aMem[]
6409 ** might change from one evaluation to the next. The next block of code
6410 ** checks to see if the register array has changed, and if so it
6411 ** reinitializes the relavant parts of the sqlite3_context object */
6412 if( pCtx->pMem != pMem ){
6413 pCtx->pMem = pMem;
6414 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6415 }
6416
6417#ifdef SQLITE_DEBUG
6418 for(i=0; i<pCtx->argc; i++){
6419 assert( memIsValid(pCtx->argv[i]) );
6420 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6421 }
6422#endif
6423
drhabfcea22005-09-06 20:36:48 +00006424 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006425 assert( pCtx->pOut->flags==MEM_Null );
6426 assert( pCtx->isError==0 );
6427 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006428#ifndef SQLITE_OMIT_WINDOWFUNC
6429 if( pOp->p1 ){
6430 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6431 }else
6432#endif
6433 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6434
drhf09ac0b2018-01-23 03:44:06 +00006435 if( pCtx->isError ){
6436 if( pCtx->isError>0 ){
6437 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006438 rc = pCtx->isError;
6439 }
drhf09ac0b2018-01-23 03:44:06 +00006440 if( pCtx->skipFlag ){
6441 assert( pOp[-1].opcode==OP_CollSeq );
6442 i = pOp[-1].p1;
6443 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6444 pCtx->skipFlag = 0;
6445 }
6446 sqlite3VdbeMemRelease(pCtx->pOut);
6447 pCtx->pOut->flags = MEM_Null;
6448 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006449 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006450 }
drhf09ac0b2018-01-23 03:44:06 +00006451 assert( pCtx->pOut->flags==MEM_Null );
6452 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006453 break;
6454}
6455
drh8f26da62018-07-05 21:22:57 +00006456/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006457** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006458**
dan9a947222018-06-14 19:06:36 +00006459** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00006460** or window function. Execute the finalizer function
6461** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00006462**
6463** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006464** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006465** argument is not used by this opcode. It is only there to disambiguate
6466** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00006467** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00006468** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006469*/
drh8f26da62018-07-05 21:22:57 +00006470/* Opcode: AggValue * P2 P3 P4 *
6471** Synopsis: r[P3]=value N=P2
6472**
6473** Invoke the xValue() function and store the result in register P3.
6474**
6475** P2 is the number of arguments that the step function takes and
6476** P4 is a pointer to the FuncDef for this function. The P2
6477** argument is not used by this opcode. It is only there to disambiguate
6478** functions that can take varying numbers of arguments. The
6479** P4 argument is only needed for the case where
6480** the step function was not previously called.
6481*/
6482case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00006483case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006484 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006485 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00006486 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00006487 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006488 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006489#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006490 if( pOp->p3 ){
6491 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006492 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006493 }else
6494#endif
drh8f26da62018-07-05 21:22:57 +00006495 {
6496 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6497 }
dan67a9b8e2018-06-22 20:51:35 +00006498
drh4c8555f2009-06-25 01:47:11 +00006499 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006500 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006501 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006502 }
drh2dca8682008-03-21 17:13:13 +00006503 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006504 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006505 if( sqlite3VdbeMemTooBig(pMem) ){
6506 goto too_big;
6507 }
drh5e00f6c2001-09-13 13:46:56 +00006508 break;
6509}
6510
dan5cf53532010-05-01 16:40:20 +00006511#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006512/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006513**
6514** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006515** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6516** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006517** SQLITE_BUSY or not, respectively. Write the number of pages in the
6518** WAL after the checkpoint into mem[P3+1] and the number of pages
6519** in the WAL that have been checkpointed after the checkpoint
6520** completes into mem[P3+2]. However on an error, mem[P3+1] and
6521** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006522*/
6523case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006524 int i; /* Loop counter */
6525 int aRes[3]; /* Results */
6526 Mem *pMem; /* Write results here */
6527
drh9e92a472013-06-27 17:40:30 +00006528 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006529 aRes[0] = 0;
6530 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006531 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6532 || pOp->p2==SQLITE_CHECKPOINT_FULL
6533 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006534 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006535 );
drh30aa3b92011-02-07 23:56:01 +00006536 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006537 if( rc ){
6538 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006539 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006540 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006541 }
drh30aa3b92011-02-07 23:56:01 +00006542 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6543 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6544 }
dan7c246102010-04-12 19:00:29 +00006545 break;
6546};
dan5cf53532010-05-01 16:40:20 +00006547#endif
drh5e00f6c2001-09-13 13:46:56 +00006548
drhcac29a62010-07-02 19:36:52 +00006549#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006550/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006551**
6552** Change the journal mode of database P1 to P3. P3 must be one of the
6553** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6554** modes (delete, truncate, persist, off and memory), this is a simple
6555** operation. No IO is required.
6556**
6557** If changing into or out of WAL mode the procedure is more complicated.
6558**
6559** Write a string containing the final journal-mode to register P2.
6560*/
drh27a348c2015-04-13 19:14:06 +00006561case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006562 Btree *pBt; /* Btree to change journal mode of */
6563 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006564 int eNew; /* New journal mode */
6565 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006566#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006567 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006568#endif
dane04dc882010-04-20 18:53:15 +00006569
drh27a348c2015-04-13 19:14:06 +00006570 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006571 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006572 assert( eNew==PAGER_JOURNALMODE_DELETE
6573 || eNew==PAGER_JOURNALMODE_TRUNCATE
6574 || eNew==PAGER_JOURNALMODE_PERSIST
6575 || eNew==PAGER_JOURNALMODE_OFF
6576 || eNew==PAGER_JOURNALMODE_MEMORY
6577 || eNew==PAGER_JOURNALMODE_WAL
6578 || eNew==PAGER_JOURNALMODE_QUERY
6579 );
6580 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006581 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006582
dane04dc882010-04-20 18:53:15 +00006583 pBt = db->aDb[pOp->p1].pBt;
6584 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006585 eOld = sqlite3PagerGetJournalMode(pPager);
6586 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6587 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006588
6589#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006590 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006591
drhd80b2332010-05-01 00:59:37 +00006592 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006593 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006594 */
6595 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006596 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006597 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006598 ){
drh0b9b4302010-06-11 17:01:24 +00006599 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006600 }
6601
drh0b9b4302010-06-11 17:01:24 +00006602 if( (eNew!=eOld)
6603 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6604 ){
danc0537fe2013-06-28 19:41:43 +00006605 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006606 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006607 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006608 "cannot change %s wal mode from within a transaction",
6609 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6610 );
drh9467abf2016-02-17 18:44:11 +00006611 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006612 }else{
6613
6614 if( eOld==PAGER_JOURNALMODE_WAL ){
6615 /* If leaving WAL mode, close the log file. If successful, the call
6616 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6617 ** file. An EXCLUSIVE lock may still be held on the database file
6618 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006619 */
dan7fb89902016-08-12 16:21:15 +00006620 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006621 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006622 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006623 }
drh242c4f72010-06-22 14:49:39 +00006624 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6625 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6626 ** as an intermediate */
6627 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006628 }
6629
6630 /* Open a transaction on the database file. Regardless of the journal
6631 ** mode, this transaction always uses a rollback journal.
6632 */
6633 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6634 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006635 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006636 }
6637 }
6638 }
dan5cf53532010-05-01 16:40:20 +00006639#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006640
drh9467abf2016-02-17 18:44:11 +00006641 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006642 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006643
dane04dc882010-04-20 18:53:15 +00006644 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006645 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006646 pOut->n = sqlite3Strlen30(pOut->z);
6647 pOut->enc = SQLITE_UTF8;
6648 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006649 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006650 break;
drhcac29a62010-07-02 19:36:52 +00006651};
6652#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006653
drhfdbcdee2007-03-27 14:44:50 +00006654#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh9ef5e772016-08-19 14:20:56 +00006655/* Opcode: Vacuum P1 * * * *
drh6f8c91c2003-12-07 00:24:35 +00006656**
drh9ef5e772016-08-19 14:20:56 +00006657** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6658** for an attached database. The "temp" database may not be vacuumed.
drh6f8c91c2003-12-07 00:24:35 +00006659*/
drh9cbf3422008-01-17 16:22:13 +00006660case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006661 assert( p->readOnly==0 );
drh9ef5e772016-08-19 14:20:56 +00006662 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006663 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006664 break;
6665}
drh154d4b22006-09-21 11:02:16 +00006666#endif
drh6f8c91c2003-12-07 00:24:35 +00006667
danielk1977dddbcdc2007-04-26 14:42:34 +00006668#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006669/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006670**
6671** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006672** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006673** P2. Otherwise, fall through to the next instruction.
6674*/
drh9cbf3422008-01-17 16:22:13 +00006675case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006676 Btree *pBt;
6677
6678 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006679 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006680 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006681 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006682 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006683 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006684 if( rc ){
6685 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006686 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006687 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006688 }
6689 break;
6690}
6691#endif
6692
drhba968db2018-07-24 22:02:12 +00006693/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006694**
drh25df48d2014-07-22 14:58:12 +00006695** Cause precompiled statements to expire. When an expired statement
6696** is executed using sqlite3_step() it will either automatically
6697** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6698** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006699**
6700** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006701** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00006702**
6703** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
6704** then running SQL statements are allowed to continue to run to completion.
6705** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
6706** that might help the statement run faster but which does not affect the
6707** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00006708*/
drh9cbf3422008-01-17 16:22:13 +00006709case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00006710 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00006711 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00006712 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00006713 }else{
drhba968db2018-07-24 22:02:12 +00006714 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00006715 }
6716 break;
6717}
6718
danielk1977c00da102006-01-07 13:21:04 +00006719#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006720/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006721** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006722**
6723** Obtain a lock on a particular table. This instruction is only used when
6724** the shared-cache feature is enabled.
6725**
danielk197796d48e92009-06-29 06:00:37 +00006726** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006727** on which the lock is acquired. A readlock is obtained if P3==0 or
6728** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006729**
6730** P2 contains the root-page of the table to lock.
6731**
drh66a51672008-01-03 00:01:23 +00006732** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006733** used to generate an error message if the lock cannot be obtained.
6734*/
drh9cbf3422008-01-17 16:22:13 +00006735case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006736 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006737 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006738 int p1 = pOp->p1;
6739 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006740 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006741 assert( isWriteLock==0 || isWriteLock==1 );
6742 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006743 if( rc ){
6744 if( (rc&0xFF)==SQLITE_LOCKED ){
6745 const char *z = pOp->p4.z;
6746 sqlite3VdbeError(p, "database table is locked: %s", z);
6747 }
6748 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006749 }
danielk1977c00da102006-01-07 13:21:04 +00006750 }
6751 break;
6752}
drhb9bb7c12006-06-11 23:41:55 +00006753#endif /* SQLITE_OMIT_SHARED_CACHE */
6754
6755#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006756/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006757**
danielk19773e3a84d2008-08-01 17:37:40 +00006758** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6759** xBegin method for that table.
6760**
6761** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006762** within a callback to a virtual table xSync() method. If it is, the error
6763** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006764*/
drh9cbf3422008-01-17 16:22:13 +00006765case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006766 VTable *pVTab;
6767 pVTab = pOp->p4.pVtab;
6768 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006769 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006770 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006771 break;
6772}
6773#endif /* SQLITE_OMIT_VIRTUALTABLE */
6774
6775#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006776/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006777**
dan73779452015-03-19 18:56:17 +00006778** P2 is a register that holds the name of a virtual table in database
6779** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006780*/
drh9cbf3422008-01-17 16:22:13 +00006781case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006782 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006783 const char *zTab; /* Name of the virtual table */
6784
dan73779452015-03-19 18:56:17 +00006785 memset(&sMem, 0, sizeof(sMem));
6786 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006787 /* Because P2 is always a static string, it is impossible for the
6788 ** sqlite3VdbeMemCopy() to fail */
6789 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6790 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006791 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006792 assert( rc==SQLITE_OK );
6793 zTab = (const char*)sqlite3_value_text(&sMem);
6794 assert( zTab || db->mallocFailed );
6795 if( zTab ){
6796 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006797 }
6798 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006799 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006800 break;
6801}
6802#endif /* SQLITE_OMIT_VIRTUALTABLE */
6803
6804#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006805/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006806**
drh66a51672008-01-03 00:01:23 +00006807** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006808** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006809*/
drh9cbf3422008-01-17 16:22:13 +00006810case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006811 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006812 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006813 db->nVDestroy--;
drh9467abf2016-02-17 18:44:11 +00006814 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006815 break;
6816}
6817#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006818
drh9eff6162006-06-12 21:59:13 +00006819#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006820/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006821**
drh66a51672008-01-03 00:01:23 +00006822** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006823** P1 is a cursor number. This opcode opens a cursor to the virtual
6824** table and stores that cursor in P1.
6825*/
drh9cbf3422008-01-17 16:22:13 +00006826case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006827 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006828 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006829 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006830 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006831
drh1713afb2013-06-28 01:24:57 +00006832 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006833 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006834 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006835 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006836 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6837 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006838 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006839 }
6840 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006841 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006842 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006843 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006844
drh9467abf2016-02-17 18:44:11 +00006845 /* Initialize sqlite3_vtab_cursor base class */
6846 pVCur->pVtab = pVtab;
6847
6848 /* Initialize vdbe cursor object */
6849 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6850 if( pCur ){
6851 pCur->uc.pVCur = pVCur;
6852 pVtab->nRef++;
6853 }else{
6854 assert( db->mallocFailed );
6855 pModule->xClose(pVCur);
6856 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006857 }
drh9eff6162006-06-12 21:59:13 +00006858 break;
6859}
6860#endif /* SQLITE_OMIT_VIRTUALTABLE */
6861
6862#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006863/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006864** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006865**
6866** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6867** the filtered result set is empty.
6868**
drh66a51672008-01-03 00:01:23 +00006869** P4 is either NULL or a string that was generated by the xBestIndex
6870** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006871** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006872**
drh9eff6162006-06-12 21:59:13 +00006873** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006874** by P1. The integer query plan parameter to xFilter is stored in register
6875** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006876** xFilter method. Registers P3+2..P3+1+argc are the argc
6877** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006878** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006879**
danielk19776dbee812008-01-03 18:39:41 +00006880** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006881*/
drh9cbf3422008-01-17 16:22:13 +00006882case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006883 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006884 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006885 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006886 Mem *pQuery;
6887 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006888 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006889 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006890 VdbeCursor *pCur;
6891 int res;
6892 int i;
6893 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006894
drha6c2ed92009-11-14 23:22:23 +00006895 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006896 pArgc = &pQuery[1];
6897 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006898 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006899 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006900 assert( pCur->eCurType==CURTYPE_VTAB );
6901 pVCur = pCur->uc.pVCur;
6902 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006903 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006904
drh9cbf3422008-01-17 16:22:13 +00006905 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006906 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006907 nArg = (int)pArgc->u.i;
6908 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006909
drh644a5292006-12-20 14:53:38 +00006910 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006911 res = 0;
6912 apArg = p->apArg;
6913 for(i = 0; i<nArg; i++){
6914 apArg[i] = &pArgc[i+1];
6915 }
drhc960dcb2015-11-20 19:22:01 +00006916 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006917 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006918 if( rc ) goto abort_due_to_error;
6919 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006920 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006921 VdbeBranchTaken(res!=0,2);
6922 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006923 break;
6924}
6925#endif /* SQLITE_OMIT_VIRTUALTABLE */
6926
6927#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00006928/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00006929** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006930**
drh6f390be2018-01-11 17:04:26 +00006931** Store in register P3 the value of the P2-th column of
6932** the current row of the virtual-table of cursor P1.
6933**
6934** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00006935** an unchanging column during an UPDATE operation, then the P5
6936** value is 1. Otherwise, P5 is 0. The P5 value is returned
drhbbd574b2018-05-24 17:25:35 +00006937** by sqlite3_vtab_nochange() routine and can be used
drh6f390be2018-01-11 17:04:26 +00006938** by virtual table implementations to return special "no-change"
6939** marks which can be more efficient, depending on the virtual table.
drh9eff6162006-06-12 21:59:13 +00006940*/
6941case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006942 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006943 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006944 Mem *pDest;
6945 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006946
drhdfe88ec2008-11-03 20:55:06 +00006947 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006948 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00006949 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006950 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006951 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006952 if( pCur->nullRow ){
6953 sqlite3VdbeMemSetNull(pDest);
6954 break;
6955 }
drhc960dcb2015-11-20 19:22:01 +00006956 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006957 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006958 assert( pModule->xColumn );
6959 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006960 sContext.pOut = pDest;
drhce2fbd12018-01-12 21:00:14 +00006961 if( pOp->p5 ){
6962 sqlite3VdbeMemSetNull(pDest);
6963 pDest->flags = MEM_Null|MEM_Zero;
6964 pDest->u.nZero = 0;
6965 }else{
6966 MemSetTypeFlag(pDest, MEM_Null);
6967 }
drhc960dcb2015-11-20 19:22:01 +00006968 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006969 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00006970 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00006971 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00006972 rc = sContext.isError;
6973 }
drh9bd038f2014-08-27 14:14:06 +00006974 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006975 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006976 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006977
drhde4fcfd2008-01-19 23:50:26 +00006978 if( sqlite3VdbeMemTooBig(pDest) ){
6979 goto too_big;
6980 }
drh9467abf2016-02-17 18:44:11 +00006981 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00006982 break;
6983}
6984#endif /* SQLITE_OMIT_VIRTUALTABLE */
6985
6986#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006987/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006988**
6989** Advance virtual table P1 to the next row in its result set and
6990** jump to instruction P2. Or, if the virtual table has reached
6991** the end of its result set, then fall through to the next instruction.
6992*/
drh9cbf3422008-01-17 16:22:13 +00006993case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006994 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006995 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006996 int res;
drh856c1032009-06-02 15:21:42 +00006997 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006998
drhc54a6172009-06-02 16:06:03 +00006999 res = 0;
drh856c1032009-06-02 15:21:42 +00007000 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00007001 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00007002 if( pCur->nullRow ){
7003 break;
7004 }
drhc960dcb2015-11-20 19:22:01 +00007005 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007006 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007007 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00007008
drhde4fcfd2008-01-19 23:50:26 +00007009 /* Invoke the xNext() method of the module. There is no way for the
7010 ** underlying implementation to return an error if one occurs during
7011 ** xNext(). Instead, if an error occurs, true is returned (indicating that
7012 ** data is available) and the error code returned when xColumn or
7013 ** some other method is next invoked on the save virtual table cursor.
7014 */
drhc960dcb2015-11-20 19:22:01 +00007015 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00007016 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007017 if( rc ) goto abort_due_to_error;
7018 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00007019 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00007020 if( !res ){
7021 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00007022 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00007023 }
drh49afe3a2013-07-10 03:05:14 +00007024 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00007025}
7026#endif /* SQLITE_OMIT_VIRTUALTABLE */
7027
danielk1977182c4ba2007-06-27 15:53:34 +00007028#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007029/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00007030**
drh66a51672008-01-03 00:01:23 +00007031** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00007032** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00007033** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00007034*/
drh9cbf3422008-01-17 16:22:13 +00007035case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00007036 sqlite3_vtab *pVtab;
7037 Mem *pName;
7038
danielk1977595a5232009-07-24 17:58:53 +00007039 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00007040 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00007041 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00007042 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00007043 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00007044 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00007045 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00007046 testcase( pName->enc==SQLITE_UTF8 );
7047 testcase( pName->enc==SQLITE_UTF16BE );
7048 testcase( pName->enc==SQLITE_UTF16LE );
7049 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00007050 if( rc ) goto abort_due_to_error;
7051 rc = pVtab->pModule->xRename(pVtab, pName->z);
7052 sqlite3VtabImportErrmsg(p, pVtab);
7053 p->expired = 0;
7054 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00007055 break;
7056}
7057#endif
drh4cbdda92006-06-14 19:00:20 +00007058
7059#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00007060/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007061** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00007062**
drh66a51672008-01-03 00:01:23 +00007063** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00007064** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00007065** are contiguous memory cells starting at P3 to pass to the xUpdate
7066** invocation. The value in register (P3+P2-1) corresponds to the
7067** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00007068**
7069** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00007070** The argv[0] element (which corresponds to memory cell P3)
7071** is the rowid of a row to delete. If argv[0] is NULL then no
7072** deletion occurs. The argv[1] element is the rowid of the new
7073** row. This can be NULL to have the virtual table select the new
7074** rowid for itself. The subsequent elements in the array are
7075** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007076**
7077** If P2==1 then no insert is performed. argv[0] is the rowid of
7078** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007079**
7080** P1 is a boolean flag. If it is set to true and the xUpdate call
7081** is successful, then the value returned by sqlite3_last_insert_rowid()
7082** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007083**
7084** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7085** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007086*/
drh9cbf3422008-01-17 16:22:13 +00007087case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007088 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007089 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007090 int nArg;
7091 int i;
7092 sqlite_int64 rowid;
7093 Mem **apArg;
7094 Mem *pX;
7095
danb061d052011-04-25 18:49:57 +00007096 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7097 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7098 );
drh9e92a472013-06-27 17:40:30 +00007099 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007100 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007101 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007102 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007103 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7104 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007105 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007106 }
7107 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007108 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007109 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007110 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007111 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007112 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007113 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007114 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007115 assert( memIsValid(pX) );
7116 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007117 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007118 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007119 }
danb061d052011-04-25 18:49:57 +00007120 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007121 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007122 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007123 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007124 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007125 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007126 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007127 }
drhd91c1a12013-02-09 13:58:25 +00007128 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007129 if( pOp->p5==OE_Ignore ){
7130 rc = SQLITE_OK;
7131 }else{
7132 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7133 }
7134 }else{
7135 p->nChange++;
7136 }
drh9467abf2016-02-17 18:44:11 +00007137 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007138 }
drh4cbdda92006-06-14 19:00:20 +00007139 break;
danielk1977399918f2006-06-14 13:03:23 +00007140}
7141#endif /* SQLITE_OMIT_VIRTUALTABLE */
7142
danielk197759a93792008-05-15 17:48:20 +00007143#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7144/* Opcode: Pagecount P1 P2 * * *
7145**
7146** Write the current number of pages in database P1 to memory cell P2.
7147*/
drh27a348c2015-04-13 19:14:06 +00007148case OP_Pagecount: { /* out2 */
7149 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007150 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007151 break;
7152}
7153#endif
7154
drh60ac3f42010-11-23 18:59:27 +00007155
7156#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7157/* Opcode: MaxPgcnt P1 P2 P3 * *
7158**
7159** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007160** Do not let the maximum page count fall below the current page count and
7161** do not change the maximum page count value if P3==0.
7162**
drh60ac3f42010-11-23 18:59:27 +00007163** Store the maximum page count after the change in register P2.
7164*/
drh27a348c2015-04-13 19:14:06 +00007165case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007166 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007167 Btree *pBt;
7168
drh27a348c2015-04-13 19:14:06 +00007169 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007170 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007171 newMax = 0;
7172 if( pOp->p3 ){
7173 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007174 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007175 }
7176 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007177 break;
7178}
7179#endif
7180
drh3e34eab2017-07-19 19:48:40 +00007181/* Opcode: Function0 P1 P2 P3 P4 P5
7182** Synopsis: r[P3]=func(r[P2@P5])
7183**
7184** Invoke a user function (P4 is a pointer to a FuncDef object that
7185** defines the function) with P5 arguments taken from register P2 and
7186** successors. The result of the function is stored in register P3.
7187** Register P3 must not be one of the function inputs.
7188**
7189** P1 is a 32-bit bitmask indicating whether or not each argument to the
7190** function was determined to be constant at compile time. If the first
7191** argument was constant then bit 0 of P1 is set. This is used to determine
7192** whether meta data associated with a user function argument using the
7193** sqlite3_set_auxdata() API may be safely retained until the next
7194** invocation of this opcode.
7195**
7196** See also: Function, AggStep, AggFinal
7197*/
7198/* Opcode: Function P1 P2 P3 P4 P5
7199** Synopsis: r[P3]=func(r[P2@P5])
7200**
7201** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7202** contains a pointer to the function to be run) with P5 arguments taken
7203** from register P2 and successors. The result of the function is stored
7204** in register P3. Register P3 must not be one of the function inputs.
7205**
7206** P1 is a 32-bit bitmask indicating whether or not each argument to the
7207** function was determined to be constant at compile time. If the first
7208** argument was constant then bit 0 of P1 is set. This is used to determine
7209** whether meta data associated with a user function argument using the
7210** sqlite3_set_auxdata() API may be safely retained until the next
7211** invocation of this opcode.
7212**
7213** SQL functions are initially coded as OP_Function0 with P4 pointing
7214** to a FuncDef object. But on first evaluation, the P4 operand is
7215** automatically converted into an sqlite3_context object and the operation
7216** changed to this OP_Function opcode. In this way, the initialization of
7217** the sqlite3_context object occurs only once, rather than once for each
7218** evaluation of the function.
7219**
7220** See also: Function0, AggStep, AggFinal
7221*/
mistachkin758784d2018-07-25 15:12:29 +00007222case OP_PureFunc0: /* group */
7223case OP_Function0: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007224 int n;
7225 sqlite3_context *pCtx;
7226
7227 assert( pOp->p4type==P4_FUNCDEF );
7228 n = pOp->p5;
7229 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7230 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
7231 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
7232 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7233 if( pCtx==0 ) goto no_mem;
7234 pCtx->pOut = 0;
7235 pCtx->pFunc = pOp->p4.pFunc;
7236 pCtx->iOp = (int)(pOp - aOp);
7237 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007238 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007239 pCtx->argc = n;
7240 pOp->p4type = P4_FUNCCTX;
7241 pOp->p4.pCtx = pCtx;
7242 assert( OP_PureFunc == OP_PureFunc0+2 );
7243 assert( OP_Function == OP_Function0+2 );
7244 pOp->opcode += 2;
7245 /* Fall through into OP_Function */
7246}
mistachkin758784d2018-07-25 15:12:29 +00007247case OP_PureFunc: /* group */
7248case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00007249 int i;
7250 sqlite3_context *pCtx;
7251
7252 assert( pOp->p4type==P4_FUNCCTX );
7253 pCtx = pOp->p4.pCtx;
7254
7255 /* If this function is inside of a trigger, the register array in aMem[]
7256 ** might change from one evaluation to the next. The next block of code
7257 ** checks to see if the register array has changed, and if so it
7258 ** reinitializes the relavant parts of the sqlite3_context object */
7259 pOut = &aMem[pOp->p3];
7260 if( pCtx->pOut != pOut ){
7261 pCtx->pOut = pOut;
7262 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7263 }
7264
7265 memAboutToChange(p, pOut);
7266#ifdef SQLITE_DEBUG
7267 for(i=0; i<pCtx->argc; i++){
7268 assert( memIsValid(pCtx->argv[i]) );
7269 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7270 }
7271#endif
7272 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007273 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007274 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7275
7276 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007277 if( pCtx->isError ){
7278 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007279 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7280 rc = pCtx->isError;
7281 }
7282 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007283 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007284 if( rc ) goto abort_due_to_error;
7285 }
7286
7287 /* Copy the result of the function into register P3 */
7288 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7289 sqlite3VdbeChangeEncoding(pOut, encoding);
7290 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7291 }
7292
7293 REGISTER_TRACE(pOp->p3, pOut);
7294 UPDATE_MAX_BLOBSIZE(pOut);
7295 break;
7296}
7297
drhf259df52017-12-27 20:38:35 +00007298/* Opcode: Trace P1 P2 * P4 *
7299**
7300** Write P4 on the statement trace output if statement tracing is
7301** enabled.
7302**
7303** Operand P1 must be 0x7fffffff and P2 must positive.
7304*/
drh74588ce2017-09-13 00:13:05 +00007305/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007306** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007307**
7308** Programs contain a single instance of this opcode as the very first
7309** opcode.
drh949f9cd2008-01-12 21:35:57 +00007310**
7311** If tracing is enabled (by the sqlite3_trace()) interface, then
7312** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007313** Or if P4 is blank, use the string returned by sqlite3_sql().
7314**
7315** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007316**
7317** Increment the value of P1 so that OP_Once opcodes will jump the
7318** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007319**
7320** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7321** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007322*/
drhf259df52017-12-27 20:38:35 +00007323case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007324case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007325 int i;
drhb9f47992018-01-24 12:14:43 +00007326#ifndef SQLITE_OMIT_TRACE
7327 char *zTrace;
7328#endif
drh5fe63bf2016-07-25 02:42:22 +00007329
7330 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7331 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7332 **
7333 ** This assert() provides evidence for:
7334 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7335 ** would have been returned by the legacy sqlite3_trace() interface by
7336 ** using the X argument when X begins with "--" and invoking
7337 ** sqlite3_expanded_sql(P) otherwise.
7338 */
7339 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007340
7341 /* OP_Init is always instruction 0 */
7342 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007343
drhaceb31b2014-02-08 01:40:27 +00007344#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007345 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007346 && !p->doingRerun
7347 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7348 ){
drh3d2a5292016-07-13 22:55:01 +00007349#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007350 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7351 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007352 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007353 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007354 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007355 }else
drh3d2a5292016-07-13 22:55:01 +00007356#endif
drh7adbcff2017-03-20 15:29:28 +00007357 if( db->nVdbeExec>1 ){
7358 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7359 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7360 sqlite3DbFree(db, z);
7361 }else{
drhbd441f72016-07-25 02:31:48 +00007362 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007363 }
drh949f9cd2008-01-12 21:35:57 +00007364 }
drh8f8b2312013-10-18 20:03:43 +00007365#ifdef SQLITE_USE_FCNTL_TRACE
7366 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7367 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007368 int j;
7369 for(j=0; j<db->nDb; j++){
7370 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7371 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007372 }
7373 }
7374#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007375#ifdef SQLITE_DEBUG
7376 if( (db->flags & SQLITE_SqlTrace)!=0
7377 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7378 ){
7379 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7380 }
7381#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007382#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007383 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007384 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007385 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007386 for(i=1; i<p->nOp; i++){
7387 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7388 }
7389 pOp->p1 = 0;
7390 }
7391 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007392 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007393 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007394}
drh949f9cd2008-01-12 21:35:57 +00007395
drh28935362013-12-07 20:39:19 +00007396#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007397/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007398**
7399** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007400** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7401** to values currently held in registers. TK_COLUMN terms in the P4
7402** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007403*/
7404case OP_CursorHint: {
7405 VdbeCursor *pC;
7406
7407 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7408 assert( pOp->p4type==P4_EXPR );
7409 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007410 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007411 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007412 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7413 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007414 }
drh28935362013-12-07 20:39:19 +00007415 break;
7416}
7417#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007418
drh4031baf2018-05-28 17:31:20 +00007419#ifdef SQLITE_DEBUG
7420/* Opcode: Abortable * * * * *
7421**
7422** Verify that an Abort can happen. Assert if an Abort at this point
7423** might cause database corruption. This opcode only appears in debugging
7424** builds.
7425**
7426** An Abort is safe if either there have been no writes, or if there is
7427** an active statement journal.
7428*/
7429case OP_Abortable: {
7430 sqlite3VdbeAssertAbortable(p);
7431 break;
7432}
7433#endif
7434
drh299bf7c2018-06-11 17:35:02 +00007435#ifdef SQLITE_DEBUG_COLUMNCACHE
7436/* Opcode: SetTabCol P1 P2 P3 * *
7437**
7438** Set a flag in register REG[P3] indicating that it holds the value
7439** of column P2 from the table on cursor P1. This flag is checked
7440** by a subsequent VerifyTabCol opcode.
7441**
7442** This opcode only appears SQLITE_DEBUG builds. It is used to verify
7443** that the expression table column cache is working correctly.
7444*/
7445case OP_SetTabCol: {
7446 aMem[pOp->p3].iTabColHash = TableColumnHash(pOp->p1,pOp->p2);
7447 break;
7448}
7449/* Opcode: VerifyTabCol P1 P2 P3 * *
7450**
7451** Verify that register REG[P3] contains the value of column P2 from
7452** cursor P1. Assert() if this is not the case.
7453**
7454** This opcode only appears SQLITE_DEBUG builds. It is used to verify
7455** that the expression table column cache is working correctly.
7456*/
7457case OP_VerifyTabCol: {
7458 assert( aMem[pOp->p3].iTabColHash == TableColumnHash(pOp->p1,pOp->p2) );
7459 break;
7460}
7461#endif
7462
drh91fd4d42008-01-19 20:11:25 +00007463/* Opcode: Noop * * * * *
7464**
7465** Do nothing. This instruction is often useful as a jump
7466** destination.
drh5e00f6c2001-09-13 13:46:56 +00007467*/
drh91fd4d42008-01-19 20:11:25 +00007468/*
7469** The magic Explain opcode are only inserted when explain==2 (which
7470** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7471** This opcode records information from the optimizer. It is the
7472** the same as a no-op. This opcodesnever appears in a real VM program.
7473*/
drh4031baf2018-05-28 17:31:20 +00007474default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007475 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007476
drh5e00f6c2001-09-13 13:46:56 +00007477 break;
7478}
7479
7480/*****************************************************************************
7481** The cases of the switch statement above this line should all be indented
7482** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7483** readability. From this point on down, the normal indentation rules are
7484** restored.
7485*****************************************************************************/
7486 }
drh6e142f52000-06-08 13:36:40 +00007487
drh7b396862003-01-01 23:06:20 +00007488#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007489 {
drh35043cc2018-02-12 20:27:34 +00007490 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007491 if( endTime>start ) pOrigOp->cycles += endTime - start;
7492 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007493 }
drh7b396862003-01-01 23:06:20 +00007494#endif
7495
drh6e142f52000-06-08 13:36:40 +00007496 /* The following code adds nothing to the actual functionality
7497 ** of the program. It is only here for testing and debugging.
7498 ** On the other hand, it does burn CPU cycles every time through
7499 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7500 */
7501#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007502 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007503
drhcf1023c2007-05-08 20:59:49 +00007504#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007505 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007506 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007507 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007508 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007509 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007510 }
drh7cc84c22016-04-11 13:36:42 +00007511 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007512 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007513 }
drh75897232000-05-29 14:26:00 +00007514 }
danielk1977b5402fb2005-01-12 07:15:04 +00007515#endif /* SQLITE_DEBUG */
7516#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007517 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007518
drha05a7222008-01-19 03:35:58 +00007519 /* If we reach this point, it means that execution is finished with
7520 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007521 */
drh9467abf2016-02-17 18:44:11 +00007522abort_due_to_error:
7523 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007524 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007525 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7526 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7527 }
drha05a7222008-01-19 03:35:58 +00007528 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007529 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007530 testcase( sqlite3GlobalConfig.xLog!=0 );
7531 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007532 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007533 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007534 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007535 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007536 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007537 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007538 }
drh900b31e2007-08-28 02:27:51 +00007539
7540 /* This is the only way out of this procedure. We have to
7541 ** release the mutexes on btrees that were acquired at the
7542 ** top. */
7543vdbe_return:
drh77dfd5b2013-08-19 11:15:48 +00007544 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00007545 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007546 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007547 assert( rc!=SQLITE_OK || nExtraDelete==0
7548 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7549 );
drhb86ccfb2003-01-28 23:13:10 +00007550 return rc;
7551
drh023ae032007-05-08 12:12:16 +00007552 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7553 ** is encountered.
7554 */
7555too_big:
drh22c17b82015-05-15 04:13:15 +00007556 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007557 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007558 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007559
drh98640a32007-06-07 19:08:32 +00007560 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007561 */
7562no_mem:
drh4a642b62016-02-05 01:55:27 +00007563 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007564 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007565 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007566 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007567
danielk19776f8a5032004-05-10 10:34:51 +00007568 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007569 ** flag.
7570 */
7571abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007572 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007573 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007574 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007575 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007576 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007577}