blob: 6eb5f95024b521f78cc2e30abce688b1d3e53512 [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**
137** M is an integer, 2 or 3, that indices how many different ways the
138** branch can go. It is usually 2. "I" is the direction the branch
139** goes. 0 means falls through. 1 means branch is taken. 2 means the
140** second alternative branch is taken.
drh4336b0e2014-08-05 00:53:51 +0000141**
142** iSrcLine is the source code line (from the __LINE__ macro) that
143** generated the VDBE instruction. This instrumentation assumes that all
144** source code is in a single file (the amalgamation). Special values 1
145** and 2 for the iSrcLine parameter mean that this particular branch is
146** always taken or never taken, respectively.
drh688852a2014-02-17 22:40:43 +0000147*/
148#if !defined(SQLITE_VDBE_COVERAGE)
149# define VdbeBranchTaken(I,M)
150#else
drh5655c542014-02-19 19:14:34 +0000151# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
152 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
153 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
154 M = iSrcLine;
155 /* Assert the truth of VdbeCoverageAlwaysTaken() and
156 ** VdbeCoverageNeverTaken() */
157 assert( (M & I)==I );
158 }else{
159 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
160 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
161 iSrcLine,I,M);
162 }
163 }
drh688852a2014-02-17 22:40:43 +0000164#endif
165
166/*
drh9cbf3422008-01-17 16:22:13 +0000167** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000168** already. Return non-zero if a malloc() fails.
169*/
drhb21c8cd2007-08-21 19:33:56 +0000170#define Stringify(P, enc) \
drhbd9507c2014-08-23 17:21:37 +0000171 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
drhf4479502004-05-27 03:12:53 +0000172 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000173
174/*
danielk1977bd7e4602004-05-24 07:34:48 +0000175** An ephemeral string value (signified by the MEM_Ephem flag) contains
176** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000177** is responsible for deallocating that string. Because the register
178** does not control the string, it might be deleted without the register
179** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000180**
181** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000182** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000183** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000184*/
drhb21c8cd2007-08-21 19:33:56 +0000185#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000186 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000187 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000188
dan689ab892011-08-12 15:02:00 +0000189/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000190#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000191
192/*
drhdfe88ec2008-11-03 20:55:06 +0000193** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000194** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000195*/
drhdfe88ec2008-11-03 20:55:06 +0000196static VdbeCursor *allocateCursor(
197 Vdbe *p, /* The virtual machine */
198 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000199 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000200 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000201 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000202){
203 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000204 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000205 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000206 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000207 **
208 ** * Sometimes cursor numbers are used for a couple of different
209 ** purposes in a vdbe program. The different uses might require
210 ** different sized allocations. Memory cells provide growable
211 ** allocations.
212 **
213 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
214 ** be freed lazily via the sqlite3_release_memory() API. This
215 ** minimizes the number of malloc calls made by the system.
216 **
drh3cdce922016-03-21 00:30:40 +0000217 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000218 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
219 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000220 */
drh9f6168b2016-03-19 23:32:58 +0000221 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000222
danielk19775f096132008-03-28 15:44:09 +0000223 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000224 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000225 nByte =
drh5cc10232013-11-21 01:04:02 +0000226 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000227 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000228
drh9f6168b2016-03-19 23:32:58 +0000229 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000230 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977be718892006-06-23 08:05:19 +0000231 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000232 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000233 }
drh322f2852014-09-19 00:43:39 +0000234 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000235 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000236 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000237 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000238 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000239 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000240 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000241 if( eCurType==CURTYPE_BTREE ){
242 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000243 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000244 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000245 }
danielk197794eb6a12005-12-15 15:22:08 +0000246 }
drh4774b132004-06-12 20:12:51 +0000247 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000248}
249
danielk19773d1bfea2004-05-14 11:00:53 +0000250/*
drh29d72102006-02-09 22:13:41 +0000251** Try to convert a value into a numeric representation if we can
252** do so without loss of information. In other words, if the string
253** looks like a number, convert it into a number. If it does not
254** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000255**
256** If the bTryForInt flag is true, then extra effort is made to give
257** an integer representation. Strings that look like floating point
258** values but which have no fractional component (example: '48.00')
259** will have a MEM_Int representation when bTryForInt is true.
260**
261** If bTryForInt is false, then if the input string contains a decimal
262** point or exponential notation, the result is only MEM_Real, even
263** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000264*/
drhbd9507c2014-08-23 17:21:37 +0000265static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000266 double rValue;
267 i64 iValue;
268 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000269 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000270 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
271 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
272 pRec->u.i = iValue;
273 pRec->flags |= MEM_Int;
274 }else{
drh74eaba42014-09-18 17:52:15 +0000275 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000276 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000277 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000278 }
drh06b3bd52018-02-01 01:13:33 +0000279 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
280 ** string representation after computing a numeric equivalent, because the
281 ** string representation might not be the canonical representation for the
282 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
283 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000284}
285
286/*
drh8a512562005-11-14 22:29:05 +0000287** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000288**
drh8a512562005-11-14 22:29:05 +0000289** SQLITE_AFF_INTEGER:
290** SQLITE_AFF_REAL:
291** SQLITE_AFF_NUMERIC:
292** Try to convert pRec to an integer representation or a
293** floating-point representation if an integer representation
294** is not possible. Note that the integer representation is
295** always preferred, even if the affinity is REAL, because
296** an integer representation is more space efficient on disk.
297**
298** SQLITE_AFF_TEXT:
299** Convert pRec to a text representation.
300**
drh05883a32015-06-02 15:32:08 +0000301** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000302** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000303*/
drh17435752007-08-16 04:30:38 +0000304static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000305 Mem *pRec, /* The value to apply affinity to */
306 char affinity, /* The affinity to be applied */
307 u8 enc /* Use this text encoding */
308){
drh7ea31cc2014-09-18 14:36:00 +0000309 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000310 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
311 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000312 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000313 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000314 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000315 }else{
316 sqlite3VdbeIntegerAffinity(pRec);
317 }
drh17c40292004-07-21 02:53:29 +0000318 }
drh7ea31cc2014-09-18 14:36:00 +0000319 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000320 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000321 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000322 ** representation. It would be harmless to repeat the conversion if
323 ** there is already a string rep, but it is pointless to waste those
324 ** CPU cycles. */
325 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
326 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
327 sqlite3VdbeMemStringify(pRec, enc, 1);
328 }
danielk19773d1bfea2004-05-14 11:00:53 +0000329 }
dandde548c2015-05-19 19:44:25 +0000330 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000331 }
332}
333
danielk1977aee18ef2005-03-09 12:26:50 +0000334/*
drh29d72102006-02-09 22:13:41 +0000335** Try to convert the type of a function argument or a result column
336** into a numeric representation. Use either INTEGER or REAL whichever
337** is appropriate. But only do the conversion if it is possible without
338** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000339*/
340int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000341 int eType = sqlite3_value_type(pVal);
342 if( eType==SQLITE_TEXT ){
343 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000344 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000345 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000346 }
drh1b27b8c2014-02-10 03:21:57 +0000347 return eType;
drh29d72102006-02-09 22:13:41 +0000348}
349
350/*
danielk1977aee18ef2005-03-09 12:26:50 +0000351** Exported version of applyAffinity(). This one works on sqlite3_value*,
352** not the internal Mem* type.
353*/
danielk19771e536952007-08-16 10:09:01 +0000354void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000355 sqlite3_value *pVal,
356 u8 affinity,
357 u8 enc
358){
drhb21c8cd2007-08-21 19:33:56 +0000359 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000360}
361
drh3d1d90a2014-03-24 15:00:15 +0000362/*
drhf1a89ed2014-08-23 17:41:15 +0000363** pMem currently only holds a string type (or maybe a BLOB that we can
364** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000365** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000366** accordingly.
367*/
368static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
369 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
370 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000371 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000372 return 0;
373 }
drh84d4f1a2017-09-20 10:47:10 +0000374 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000375 return MEM_Int;
376 }
377 return MEM_Real;
378}
379
380/*
drh3d1d90a2014-03-24 15:00:15 +0000381** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
382** none.
383**
384** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000385** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000386*/
387static u16 numericType(Mem *pMem){
388 if( pMem->flags & (MEM_Int|MEM_Real) ){
389 return pMem->flags & (MEM_Int|MEM_Real);
390 }
391 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000392 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000393 }
394 return 0;
395}
396
danielk1977b5402fb2005-01-12 07:15:04 +0000397#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000398/*
danielk1977ca6b2912004-05-21 10:49:47 +0000399** Write a nice string representation of the contents of cell pMem
400** into buffer zBuf, length nBuf.
401*/
drh74161702006-02-24 02:53:49 +0000402void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000403 char *zCsr = zBuf;
404 int f = pMem->flags;
405
drh57196282004-10-06 15:41:16 +0000406 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000407
danielk1977ca6b2912004-05-21 10:49:47 +0000408 if( f&MEM_Blob ){
409 int i;
410 char c;
411 if( f & MEM_Dyn ){
412 c = 'z';
413 assert( (f & (MEM_Static|MEM_Ephem))==0 );
414 }else if( f & MEM_Static ){
415 c = 't';
416 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
417 }else if( f & MEM_Ephem ){
418 c = 'e';
419 assert( (f & (MEM_Static|MEM_Dyn))==0 );
420 }else{
421 c = 's';
422 }
drh85c2dc02017-03-16 13:30:58 +0000423 *(zCsr++) = c;
drh5bb3eb92007-05-04 13:15:55 +0000424 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000425 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000426 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000427 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000428 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000429 }
430 for(i=0; i<16 && i<pMem->n; i++){
431 char z = pMem->z[i];
432 if( z<32 || z>126 ) *zCsr++ = '.';
433 else *zCsr++ = z;
434 }
drh85c2dc02017-03-16 13:30:58 +0000435 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000436 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000437 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000438 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000439 }
danielk1977b1bc9532004-05-22 03:05:33 +0000440 *zCsr = '\0';
441 }else if( f & MEM_Str ){
442 int j, k;
443 zBuf[0] = ' ';
444 if( f & MEM_Dyn ){
445 zBuf[1] = 'z';
446 assert( (f & (MEM_Static|MEM_Ephem))==0 );
447 }else if( f & MEM_Static ){
448 zBuf[1] = 't';
449 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
450 }else if( f & MEM_Ephem ){
451 zBuf[1] = 'e';
452 assert( (f & (MEM_Static|MEM_Dyn))==0 );
453 }else{
454 zBuf[1] = 's';
455 }
456 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000457 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000458 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000459 zBuf[k++] = '[';
460 for(j=0; j<15 && j<pMem->n; j++){
461 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000462 if( c>=0x20 && c<0x7f ){
463 zBuf[k++] = c;
464 }else{
465 zBuf[k++] = '.';
466 }
467 }
468 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000469 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000470 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000471 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000472 }
danielk1977ca6b2912004-05-21 10:49:47 +0000473}
474#endif
475
drh5b6afba2008-01-05 16:29:28 +0000476#ifdef SQLITE_DEBUG
477/*
478** Print the value of a register for tracing purposes:
479*/
drh84e55a82013-11-13 17:58:23 +0000480static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000481 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000482 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000483 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000484 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000485 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000486 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000487 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000488 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000489#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000490 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000491 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000492#endif
drh733bf1b2009-04-22 00:47:00 +0000493 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000494 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000495 }else{
496 char zBuf[200];
497 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000498 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000499 }
dan5b6c8e42016-01-30 15:46:03 +0000500 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000501}
drh84e55a82013-11-13 17:58:23 +0000502static void registerTrace(int iReg, Mem *p){
503 printf("REG[%d] = ", iReg);
504 memTracePrint(p);
505 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000506 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000507}
508#endif
509
510#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000511# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000512#else
513# define REGISTER_TRACE(R,M)
514#endif
515
danielk197784ac9d02004-05-18 09:58:06 +0000516
drh7b396862003-01-01 23:06:20 +0000517#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000518
519/*
520** hwtime.h contains inline assembler code for implementing
521** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000522*/
shane9bcbdad2008-05-29 20:22:37 +0000523#include "hwtime.h"
524
drh7b396862003-01-01 23:06:20 +0000525#endif
526
danielk1977fd7f0452008-12-17 17:30:26 +0000527#ifndef NDEBUG
528/*
529** This function is only called from within an assert() expression. It
530** checks that the sqlite3.nTransaction variable is correctly set to
531** the number of non-transaction savepoints currently in the
532** linked list starting at sqlite3.pSavepoint.
533**
534** Usage:
535**
536** assert( checkSavepointCount(db) );
537*/
538static int checkSavepointCount(sqlite3 *db){
539 int n = 0;
540 Savepoint *p;
541 for(p=db->pSavepoint; p; p=p->pNext) n++;
542 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
543 return 1;
544}
545#endif
546
drh27a348c2015-04-13 19:14:06 +0000547/*
548** Return the register of pOp->p2 after first preparing it to be
549** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000550*/
551static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
552 sqlite3VdbeMemSetNull(pOut);
553 pOut->flags = MEM_Int;
554 return pOut;
555}
drh27a348c2015-04-13 19:14:06 +0000556static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
557 Mem *pOut;
558 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000559 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000560 pOut = &p->aMem[pOp->p2];
561 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000562 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000563 return out2PrereleaseWithClear(pOut);
564 }else{
565 pOut->flags = MEM_Int;
566 return pOut;
567 }
drh27a348c2015-04-13 19:14:06 +0000568}
569
drhb9755982010-07-24 16:34:37 +0000570
571/*
drh0fd61352014-02-07 02:29:45 +0000572** Execute as much of a VDBE program as we can.
573** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000574*/
danielk19774adee202004-05-08 08:23:19 +0000575int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000576 Vdbe *p /* The VDBE */
577){
drhbbe879d2009-11-14 18:04:35 +0000578 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000579 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000580#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
581 Op *pOrigOp; /* Value of pOp at the top of the loop */
582#endif
drhb89aeb62016-01-27 15:49:32 +0000583#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000584 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000585#endif
drhb86ccfb2003-01-28 23:13:10 +0000586 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000587 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000588 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000589 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000590 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000591 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000592#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000593 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000594#endif
drha6c2ed92009-11-14 23:22:23 +0000595 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000596 Mem *pIn1 = 0; /* 1st input operand */
597 Mem *pIn2 = 0; /* 2nd input operand */
598 Mem *pIn3 = 0; /* 3rd input operand */
599 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000600#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000601 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000602#endif
drh856c1032009-06-02 15:21:42 +0000603 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000604
drhca48c902008-01-18 14:08:24 +0000605 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000606 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000607 if( p->rc==SQLITE_NOMEM ){
608 /* This happens if a malloc() inside a call to sqlite3_column_text() or
609 ** sqlite3_column_text16() failed. */
610 goto no_mem;
611 }
drhcbd8db32015-08-20 17:18:32 +0000612 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000613 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000614 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000615 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000616 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000617 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000618 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000619 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000620#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
621 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000622 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000623 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000624 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh2ab792e2017-05-30 18:34:07 +0000625 }else{
626 nProgressLimit = 0xffffffff;
drh0d1961e2013-07-25 16:27:51 +0000627 }
628#endif
drh3c23a882007-01-09 14:01:13 +0000629#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000630 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000631 if( p->pc==0
632 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
633 ){
drh3c23a882007-01-09 14:01:13 +0000634 int i;
drh84e55a82013-11-13 17:58:23 +0000635 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000636 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000637 if( p->db->flags & SQLITE_VdbeListing ){
638 printf("VDBE Program Listing:\n");
639 for(i=0; i<p->nOp; i++){
640 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
641 }
drh3c23a882007-01-09 14:01:13 +0000642 }
drh84e55a82013-11-13 17:58:23 +0000643 if( p->db->flags & SQLITE_VdbeEQP ){
644 for(i=0; i<p->nOp; i++){
645 if( aOp[i].opcode==OP_Explain ){
646 if( once ) printf("VDBE Query Plan:\n");
647 printf("%s\n", aOp[i].p4.z);
648 once = 0;
649 }
650 }
651 }
652 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000653 }
danielk19772d1d86f2008-06-20 14:59:51 +0000654 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000655#endif
drh9467abf2016-02-17 18:44:11 +0000656 for(pOp=&aOp[p->pc]; 1; pOp++){
657 /* Errors are detected by individual opcodes, with an immediate
658 ** jumps to abort_due_to_error. */
659 assert( rc==SQLITE_OK );
660
drhf56fa462015-04-13 21:39:54 +0000661 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000662#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000663 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000664#endif
drhbf159fa2013-06-25 22:01:22 +0000665 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000666#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000667 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000668#endif
drh6e142f52000-06-08 13:36:40 +0000669
danielk19778b60e0f2005-01-12 09:10:39 +0000670 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000671 */
danielk19778b60e0f2005-01-12 09:10:39 +0000672#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000673 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000674 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000675 }
drh3f7d4e42004-07-24 14:35:58 +0000676#endif
677
drh6e142f52000-06-08 13:36:40 +0000678
drhf6038712004-02-08 18:07:34 +0000679 /* Check to see if we need to simulate an interrupt. This only happens
680 ** if we have a special test build.
681 */
682#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000683 if( sqlite3_interrupt_count>0 ){
684 sqlite3_interrupt_count--;
685 if( sqlite3_interrupt_count==0 ){
686 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000687 }
688 }
689#endif
690
drh3c657212009-11-17 23:59:58 +0000691 /* Sanity checking on other operands */
692#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000693 {
694 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
695 if( (opProperty & OPFLG_IN1)!=0 ){
696 assert( pOp->p1>0 );
697 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
698 assert( memIsValid(&aMem[pOp->p1]) );
699 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
700 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
701 }
702 if( (opProperty & OPFLG_IN2)!=0 ){
703 assert( pOp->p2>0 );
704 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
705 assert( memIsValid(&aMem[pOp->p2]) );
706 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
707 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
708 }
709 if( (opProperty & OPFLG_IN3)!=0 ){
710 assert( pOp->p3>0 );
711 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
712 assert( memIsValid(&aMem[pOp->p3]) );
713 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
714 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
715 }
716 if( (opProperty & OPFLG_OUT2)!=0 ){
717 assert( pOp->p2>0 );
718 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
719 memAboutToChange(p, &aMem[pOp->p2]);
720 }
721 if( (opProperty & OPFLG_OUT3)!=0 ){
722 assert( pOp->p3>0 );
723 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
724 memAboutToChange(p, &aMem[pOp->p3]);
725 }
drh3c657212009-11-17 23:59:58 +0000726 }
727#endif
drh6dc41482015-04-16 17:31:02 +0000728#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
729 pOrigOp = pOp;
730#endif
drh93952eb2009-11-13 19:43:43 +0000731
drh75897232000-05-29 14:26:00 +0000732 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000733
drh5e00f6c2001-09-13 13:46:56 +0000734/*****************************************************************************
735** What follows is a massive switch statement where each case implements a
736** separate instruction in the virtual machine. If we follow the usual
737** indentation conventions, each case should be indented by 6 spaces. But
738** that is a lot of wasted space on the left margin. So the code within
739** the switch statement will break with convention and be flush-left. Another
740** big comment (similar to this one) will mark the point in the code where
741** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000742**
743** The formatting of each case is important. The makefile for SQLite
744** generates two C files "opcodes.h" and "opcodes.c" by scanning this
745** file looking for lines that begin with "case OP_". The opcodes.h files
746** will be filled with #defines that give unique integer values to each
747** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000748** each string is the symbolic name for the corresponding opcode. If the
749** case statement is followed by a comment of the form "/# same as ... #/"
750** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000751**
drh9cbf3422008-01-17 16:22:13 +0000752** Other keywords in the comment that follows each case are used to
753** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000754** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000755** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000756**
drhac82fcf2002-09-08 17:23:41 +0000757** Documentation about VDBE opcodes is generated by scanning this file
758** for lines of that contain "Opcode:". That line and all subsequent
759** comment lines are used in the generation of the opcode.html documentation
760** file.
761**
762** SUMMARY:
763**
764** Formatting is important to scripts that scan this file.
765** Do not deviate from the formatting style currently in use.
766**
drh5e00f6c2001-09-13 13:46:56 +0000767*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000768
drh9cbf3422008-01-17 16:22:13 +0000769/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000770**
771** An unconditional jump to address P2.
772** The next instruction executed will be
773** the one at index P2 from the beginning of
774** the program.
drhfe705102014-03-06 13:38:37 +0000775**
776** The P1 parameter is not actually used by this opcode. However, it
777** is sometimes set to 1 instead of 0 as a hint to the command-line shell
778** that this Goto is the bottom of a loop and that the lines from P2 down
779** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000780*/
drh9cbf3422008-01-17 16:22:13 +0000781case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000782jump_to_p2_and_check_for_interrupt:
783 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000784
785 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000786 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000787 ** completion. Check to see if sqlite3_interrupt() has been called
788 ** or if the progress callback needs to be invoked.
789 **
790 ** This code uses unstructured "goto" statements and does not look clean.
791 ** But that is not due to sloppy coding habits. The code is written this
792 ** way for performance, to avoid having to run the interrupt and progress
793 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
794 ** faster according to "valgrind --tool=cachegrind" */
795check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000796 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000797#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
798 /* Call the progress callback if it is configured and the required number
799 ** of VDBE ops have been executed (either since this invocation of
800 ** sqlite3VdbeExec() or since last time the progress callback was called).
801 ** If the progress callback returns non-zero, exit the virtual machine with
802 ** a return code SQLITE_ABORT.
803 */
drh2ab792e2017-05-30 18:34:07 +0000804 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000805 assert( db->nProgressOps!=0 );
806 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
807 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000808 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000809 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000810 }
drh49afe3a2013-07-10 03:05:14 +0000811 }
812#endif
813
drh5e00f6c2001-09-13 13:46:56 +0000814 break;
815}
drh75897232000-05-29 14:26:00 +0000816
drh2eb95372008-06-06 15:04:36 +0000817/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000818**
drh2eb95372008-06-06 15:04:36 +0000819** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000820** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000821*/
drhb8475df2011-12-09 16:21:19 +0000822case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000823 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000824 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000825 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000826 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000827 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000828 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000829 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000830
831 /* Most jump operations do a goto to this spot in order to update
832 ** the pOp pointer. */
833jump_to_p2:
834 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000835 break;
836}
837
drh2eb95372008-06-06 15:04:36 +0000838/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000839**
drh81cf13e2014-02-07 18:27:53 +0000840** Jump to the next instruction after the address in register P1. After
841** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000842*/
drh2eb95372008-06-06 15:04:36 +0000843case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000844 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000845 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000846 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000847 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000848 break;
849}
850
drhed71a832014-02-07 19:18:10 +0000851/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000852**
drh5dad9a32014-07-25 18:37:42 +0000853** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000854** located at address P3.
855**
drh5dad9a32014-07-25 18:37:42 +0000856** If P2!=0 then the coroutine implementation immediately follows
857** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000858** address P2.
drh5dad9a32014-07-25 18:37:42 +0000859**
860** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000861*/
862case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000863 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000864 assert( pOp->p2>=0 && pOp->p2<p->nOp );
865 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000866 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000867 assert( !VdbeMemDynamic(pOut) );
868 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000869 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000870 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000871 break;
872}
873
874/* Opcode: EndCoroutine P1 * * * *
875**
drhbc5cf382014-08-06 01:08:07 +0000876** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000877** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000878** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000879**
880** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000881*/
882case OP_EndCoroutine: { /* in1 */
883 VdbeOp *pCaller;
884 pIn1 = &aMem[pOp->p1];
885 assert( pIn1->flags==MEM_Int );
886 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
887 pCaller = &aOp[pIn1->u.i];
888 assert( pCaller->opcode==OP_Yield );
889 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000890 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000891 pIn1->flags = MEM_Undefined;
892 break;
893}
894
895/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000896**
drh5dad9a32014-07-25 18:37:42 +0000897** Swap the program counter with the value in register P1. This
898** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000899**
drh5dad9a32014-07-25 18:37:42 +0000900** If the coroutine that is launched by this instruction ends with
901** Yield or Return then continue to the next instruction. But if
902** the coroutine launched by this instruction ends with
903** EndCoroutine, then jump to P2 rather than continuing with the
904** next instruction.
905**
906** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000907*/
drh81cf13e2014-02-07 18:27:53 +0000908case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000909 int pcDest;
drh3c657212009-11-17 23:59:58 +0000910 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000911 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000912 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000913 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000914 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000915 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000916 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000917 break;
918}
919
drhf9c8ce32013-11-05 13:33:55 +0000920/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000921** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000922**
drhef8662b2011-06-20 21:47:58 +0000923** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000924** parameter P1, P2, and P4 as if this were a Halt instruction. If the
925** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000926** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000927*/
928case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000929 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +0000930#ifdef SQLITE_DEBUG
931 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
932#endif
drh5053a792009-02-20 03:02:23 +0000933 if( (pIn3->flags & MEM_Null)==0 ) break;
934 /* Fall through into OP_Halt */
935}
drhe00ee6e2008-06-20 15:24:01 +0000936
drhf9c8ce32013-11-05 13:33:55 +0000937/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000938**
drh3d4501e2008-12-04 20:40:10 +0000939** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000940** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000941**
drh92f02c32004-09-02 14:57:08 +0000942** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
943** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
944** For errors, it can be some other value. If P1!=0 then P2 will determine
945** whether or not to rollback the current transaction. Do not rollback
946** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
947** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000948** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000949**
drh66a51672008-01-03 00:01:23 +0000950** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000951**
drhf9c8ce32013-11-05 13:33:55 +0000952** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
953**
954** 0: (no change)
955** 1: NOT NULL contraint failed: P4
956** 2: UNIQUE constraint failed: P4
957** 3: CHECK constraint failed: P4
958** 4: FOREIGN KEY constraint failed: P4
959**
960** If P5 is not zero and P4 is NULL, then everything after the ":" is
961** omitted.
962**
drh9cfcf5d2002-01-29 18:41:24 +0000963** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000964** every program. So a jump past the last instruction of the program
965** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000966*/
drh9cbf3422008-01-17 16:22:13 +0000967case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +0000968 VdbeFrame *pFrame;
969 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000970
drhf56fa462015-04-13 21:39:54 +0000971 pcx = (int)(pOp - aOp);
drh4031baf2018-05-28 17:31:20 +0000972#ifdef SQLITE_DEBUG
973 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
974#endif
dan165921a2009-08-28 18:53:45 +0000975 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000976 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000977 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000978 p->pFrame = pFrame->pParent;
979 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000980 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000981 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +0000982 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +0000983 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +0000984 ** currently being halted. If the p2 instruction of this OP_Halt
985 ** instruction is set to OE_Ignore, then the sub-program is throwing
986 ** an IGNORE exception. In this case jump to the address specified
987 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +0000988 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +0000989 }
drhbbe879d2009-11-14 18:04:35 +0000990 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000991 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +0000992 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +0000993 break;
994 }
drh92f02c32004-09-02 14:57:08 +0000995 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000996 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +0000997 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +0000998 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +0000999 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001000 if( pOp->p5 ){
1001 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1002 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001003 testcase( pOp->p5==1 );
1004 testcase( pOp->p5==2 );
1005 testcase( pOp->p5==3 );
1006 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001007 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1008 if( pOp->p4.z ){
1009 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1010 }
drhd9b7ec92013-11-06 14:05:21 +00001011 }else{
drh22c17b82015-05-15 04:13:15 +00001012 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001013 }
drh99f5de72016-04-30 02:59:15 +00001014 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001015 }
drh92f02c32004-09-02 14:57:08 +00001016 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001017 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001018 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001019 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001020 }else{
drhd91c1a12013-02-09 13:58:25 +00001021 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001022 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001023 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001024 }
drh900b31e2007-08-28 02:27:51 +00001025 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001026}
drhc61053b2000-06-04 12:58:36 +00001027
drh4c583122008-01-04 22:01:03 +00001028/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001029** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001030**
drh9cbf3422008-01-17 16:22:13 +00001031** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001032*/
drh27a348c2015-04-13 19:14:06 +00001033case OP_Integer: { /* out2 */
1034 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001035 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001036 break;
1037}
1038
drh4c583122008-01-04 22:01:03 +00001039/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001040** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001041**
drh66a51672008-01-03 00:01:23 +00001042** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001043** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001044*/
drh27a348c2015-04-13 19:14:06 +00001045case OP_Int64: { /* out2 */
1046 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001047 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001048 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001049 break;
1050}
drh4f26d6c2004-05-26 23:25:30 +00001051
drh13573c72010-01-12 17:04:07 +00001052#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001053/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001054** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001055**
drh4c583122008-01-04 22:01:03 +00001056** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001057** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001058*/
drh27a348c2015-04-13 19:14:06 +00001059case OP_Real: { /* same as TK_FLOAT, out2 */
1060 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001061 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001062 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001063 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001064 break;
1065}
drh13573c72010-01-12 17:04:07 +00001066#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001067
drh3c84ddf2008-01-09 02:15:38 +00001068/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001069** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001070**
drh66a51672008-01-03 00:01:23 +00001071** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001072** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001073** this transformation, the length of string P4 is computed and stored
1074** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001075*/
drh27a348c2015-04-13 19:14:06 +00001076case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001077 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001078 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001079 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001080 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001081
1082#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001083 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001084 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001085 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drh4c583122008-01-04 22:01:03 +00001086 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001087 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001088 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001089 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001090 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001091 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001092 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001093 }
drh66a51672008-01-03 00:01:23 +00001094 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001095 pOp->p4.z = pOut->z;
1096 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001097 }
drh2f555112016-04-30 18:10:34 +00001098 testcase( rc==SQLITE_TOOBIG );
danielk197793758c82005-01-21 08:13:14 +00001099#endif
drhbb4957f2008-03-20 14:03:29 +00001100 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001101 goto too_big;
1102 }
drh2f555112016-04-30 18:10:34 +00001103 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001104 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001105}
drhf4479502004-05-27 03:12:53 +00001106
drhf07cf6e2015-03-06 16:45:16 +00001107/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001108** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001109**
drh9cbf3422008-01-17 16:22:13 +00001110** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001111**
drh44aebff2016-05-02 10:25:42 +00001112** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001113** the datatype of the register P2 is converted to BLOB. The content is
1114** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001115** of a string, as if it had been CAST. In other words:
1116**
1117** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001118*/
drh27a348c2015-04-13 19:14:06 +00001119case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001120 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001121 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001122 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1123 pOut->z = pOp->p4.z;
1124 pOut->n = pOp->p1;
1125 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001126 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001127#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001128 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001129 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001130 pIn3 = &aMem[pOp->p3];
1131 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001132 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001133 }
drh41d2e662015-12-01 21:23:07 +00001134#endif
danielk1977c572ef72004-05-27 09:28:41 +00001135 break;
1136}
1137
drh053a1282012-09-19 21:15:46 +00001138/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001139** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001140**
drhb8475df2011-12-09 16:21:19 +00001141** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001142** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001143** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001144** set to NULL.
1145**
1146** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1147** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1148** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001149*/
drh27a348c2015-04-13 19:14:06 +00001150case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001151 int cnt;
drh053a1282012-09-19 21:15:46 +00001152 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001153 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001154 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001155 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001156 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001157 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001158 while( cnt>0 ){
1159 pOut++;
1160 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001161 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001162 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001163 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001164 cnt--;
1165 }
drhf0863fe2005-06-12 21:35:51 +00001166 break;
1167}
1168
drh05a86c52014-02-16 01:55:49 +00001169/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001170** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001171**
1172** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1173** instruction, but do not free any string or blob memory associated with
1174** the register, so that if the value was a string or blob that was
1175** previously copied using OP_SCopy, the copies will continue to be valid.
1176*/
1177case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001178 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001179 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001180 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001181 break;
1182}
drhf0863fe2005-06-12 21:35:51 +00001183
drha5750cf2014-02-07 13:20:31 +00001184/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001185** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001186**
drh9de221d2008-01-05 06:51:30 +00001187** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001188** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001189*/
drh27a348c2015-04-13 19:14:06 +00001190case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001191 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001192 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001193 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001194 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001195 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001196 break;
1197}
1198
drheaf52d82010-05-12 13:50:23 +00001199/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001200** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001201**
drheaf52d82010-05-12 13:50:23 +00001202** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001203**
drh0fd61352014-02-07 02:29:45 +00001204** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001205** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001206*/
drh27a348c2015-04-13 19:14:06 +00001207case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001208 Mem *pVar; /* Value being transferred */
1209
drheaf52d82010-05-12 13:50:23 +00001210 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001211 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001212 pVar = &p->aVar[pOp->p1 - 1];
1213 if( sqlite3VdbeMemTooBig(pVar) ){
1214 goto too_big;
drh023ae032007-05-08 12:12:16 +00001215 }
drh7441df72017-01-09 19:27:04 +00001216 pOut = &aMem[pOp->p2];
drheaf52d82010-05-12 13:50:23 +00001217 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1218 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001219 break;
1220}
danielk1977295ba552004-05-19 10:34:51 +00001221
drhb21e7c72008-06-22 12:37:57 +00001222/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001223** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001224**
drh079a3072014-03-19 14:10:55 +00001225** Move the P3 values in register P1..P1+P3-1 over into
1226** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001227** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001228** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1229** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001230*/
drhe1349cb2008-04-01 00:36:10 +00001231case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001232 int n; /* Number of registers left to copy */
1233 int p1; /* Register to copy from */
1234 int p2; /* Register to copy to */
1235
drhe09f43f2013-11-21 04:18:31 +00001236 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001237 p1 = pOp->p1;
1238 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001239 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001240 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001241
drha6c2ed92009-11-14 23:22:23 +00001242 pIn1 = &aMem[p1];
1243 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001244 do{
drh9f6168b2016-03-19 23:32:58 +00001245 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1246 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001247 assert( memIsValid(pIn1) );
1248 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001249 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001250#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001251 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001252 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001253 }
1254#endif
drhbd6789e2015-04-28 14:00:02 +00001255 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001256 REGISTER_TRACE(p2++, pOut);
1257 pIn1++;
1258 pOut++;
drh079a3072014-03-19 14:10:55 +00001259 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001260 break;
1261}
1262
drhe8e4af72012-09-21 00:04:28 +00001263/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001264** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001265**
drhe8e4af72012-09-21 00:04:28 +00001266** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001267**
1268** This instruction makes a deep copy of the value. A duplicate
1269** is made of any string or blob constant. See also OP_SCopy.
1270*/
drhe8e4af72012-09-21 00:04:28 +00001271case OP_Copy: {
1272 int n;
1273
1274 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001275 pIn1 = &aMem[pOp->p1];
1276 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001277 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001278 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001279 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001280 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1281 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001282#ifdef SQLITE_DEBUG
1283 pOut->pScopyFrom = 0;
drh58773a52018-06-12 13:52:23 +00001284 pOut->iTabColHash = 0;
drh953f7612012-12-07 22:18:54 +00001285#endif
drhe8e4af72012-09-21 00:04:28 +00001286 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1287 if( (n--)==0 ) break;
1288 pOut++;
1289 pIn1++;
1290 }
drhe1349cb2008-04-01 00:36:10 +00001291 break;
1292}
1293
drhb1fdb2a2008-01-05 04:06:03 +00001294/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001295** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001296**
drh9cbf3422008-01-17 16:22:13 +00001297** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001298**
1299** This instruction makes a shallow copy of the value. If the value
1300** is a string or blob, then the copy is only a pointer to the
1301** original and hence if the original changes so will the copy.
1302** Worse, if the original is deallocated, the copy becomes invalid.
1303** Thus the program must guarantee that the original will not change
1304** during the lifetime of the copy. Use OP_Copy to make a complete
1305** copy.
1306*/
drh26198bb2013-10-31 11:15:09 +00001307case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001308 pIn1 = &aMem[pOp->p1];
1309 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001310 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001311 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001312#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001313 pOut->pScopyFrom = pIn1;
1314 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001315#endif
drh5e00f6c2001-09-13 13:46:56 +00001316 break;
1317}
drh75897232000-05-29 14:26:00 +00001318
drhfed7ac62015-10-15 18:04:59 +00001319/* Opcode: IntCopy P1 P2 * * *
1320** Synopsis: r[P2]=r[P1]
1321**
1322** Transfer the integer value held in register P1 into register P2.
1323**
1324** This is an optimized version of SCopy that works only for integer
1325** values.
1326*/
1327case OP_IntCopy: { /* out2 */
1328 pIn1 = &aMem[pOp->p1];
1329 assert( (pIn1->flags & MEM_Int)!=0 );
1330 pOut = &aMem[pOp->p2];
1331 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1332 break;
1333}
1334
drh9cbf3422008-01-17 16:22:13 +00001335/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001336** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001337**
shane21e7feb2008-05-30 15:59:49 +00001338** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001339** results. This opcode causes the sqlite3_step() call to terminate
1340** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001341** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001342** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001343*/
drh9cbf3422008-01-17 16:22:13 +00001344case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001345 Mem *pMem;
1346 int i;
1347 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001348 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001349 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001350
drhe6400b92013-11-13 23:48:46 +00001351#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1352 /* Run the progress counter just before returning.
1353 */
1354 if( db->xProgress!=0
drh2ab792e2017-05-30 18:34:07 +00001355 && nVmStep>=nProgressLimit
drhe6400b92013-11-13 23:48:46 +00001356 && db->xProgress(db->pProgressArg)!=0
1357 ){
1358 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001359 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001360 }
1361#endif
1362
dan32b09f22009-09-23 17:29:59 +00001363 /* If this statement has violated immediate foreign key constraints, do
1364 ** not return the number of rows modified. And do not RELEASE the statement
1365 ** transaction. It needs to be rolled back. */
1366 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1367 assert( db->flags&SQLITE_CountRows );
1368 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001369 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001370 }
1371
danielk1977bd434552009-03-18 10:33:00 +00001372 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1373 ** DML statements invoke this opcode to return the number of rows
1374 ** modified to the user. This is the only way that a VM that
1375 ** opens a statement transaction may invoke this opcode.
1376 **
1377 ** In case this is such a statement, close any statement transaction
1378 ** opened by this VM before returning control to the user. This is to
1379 ** ensure that statement-transactions are always nested, not overlapping.
1380 ** If the open statement-transaction is not closed here, then the user
1381 ** may step another VM that opens its own statement transaction. This
1382 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001383 **
1384 ** The statement transaction is never a top-level transaction. Hence
1385 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001386 */
1387 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001388 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001389 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001390
drhd4e70eb2008-01-02 00:34:36 +00001391 /* Invalidate all ephemeral cursor row caches */
1392 p->cacheCtr = (p->cacheCtr + 2)|1;
1393
1394 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001395 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001396 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001397 */
drha6c2ed92009-11-14 23:22:23 +00001398 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001399 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001400 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001401 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001402 assert( (pMem[i].flags & MEM_Ephem)==0
1403 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001404 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001405 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001406 }
drh28039692008-03-17 16:54:01 +00001407 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001408
drh3d2a5292016-07-13 22:55:01 +00001409 if( db->mTrace & SQLITE_TRACE_ROW ){
1410 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1411 }
1412
drhd4e70eb2008-01-02 00:34:36 +00001413 /* Return SQLITE_ROW
1414 */
drhf56fa462015-04-13 21:39:54 +00001415 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001416 rc = SQLITE_ROW;
1417 goto vdbe_return;
1418}
1419
drh5b6afba2008-01-05 16:29:28 +00001420/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001421** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001422**
drh5b6afba2008-01-05 16:29:28 +00001423** Add the text in register P1 onto the end of the text in
1424** register P2 and store the result in register P3.
1425** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001426**
1427** P3 = P2 || P1
1428**
1429** It is illegal for P1 and P3 to be the same register. Sometimes,
1430** if P3 is the same register as P2, the implementation is able
1431** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001432*/
drh5b6afba2008-01-05 16:29:28 +00001433case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001434 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001435
drh3c657212009-11-17 23:59:58 +00001436 pIn1 = &aMem[pOp->p1];
1437 pIn2 = &aMem[pOp->p2];
1438 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001439 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001440 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001441 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001442 break;
drh5e00f6c2001-09-13 13:46:56 +00001443 }
drha0c06522009-06-17 22:50:41 +00001444 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001445 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001446 Stringify(pIn2, encoding);
1447 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001448 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001449 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001450 }
drh9c1905f2008-12-10 22:32:56 +00001451 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001452 goto no_mem;
1453 }
drhc91b2fd2014-03-01 18:13:23 +00001454 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001455 if( pOut!=pIn2 ){
1456 memcpy(pOut->z, pIn2->z, pIn2->n);
1457 }
1458 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001459 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001460 pOut->z[nByte+1] = 0;
1461 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001462 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001463 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001464 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001465 break;
1466}
drh75897232000-05-29 14:26:00 +00001467
drh3c84ddf2008-01-09 02:15:38 +00001468/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001469** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001470**
drh60a713c2008-01-21 16:22:45 +00001471** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001472** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001473** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001474*/
drh3c84ddf2008-01-09 02:15:38 +00001475/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001476** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001477**
drh3c84ddf2008-01-09 02:15:38 +00001478**
shane21e7feb2008-05-30 15:59:49 +00001479** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001480** and store the result in register P3.
1481** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001482*/
drh3c84ddf2008-01-09 02:15:38 +00001483/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001484** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001485**
drh60a713c2008-01-21 16:22:45 +00001486** Subtract the value in register P1 from the value in register P2
1487** and store the result in register P3.
1488** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001489*/
drh9cbf3422008-01-17 16:22:13 +00001490/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001491** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001492**
drh60a713c2008-01-21 16:22:45 +00001493** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001494** and store the result in register P3 (P3=P2/P1). If the value in
1495** register P1 is zero, then the result is NULL. If either input is
1496** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001497*/
drh9cbf3422008-01-17 16:22:13 +00001498/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001499** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001500**
drh40864a12013-11-15 18:58:37 +00001501** Compute the remainder after integer register P2 is divided by
1502** register P1 and store the result in register P3.
1503** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001504** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001505*/
drh5b6afba2008-01-05 16:29:28 +00001506case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1507case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1508case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1509case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1510case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001511 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001512 u16 flags; /* Combined MEM_* flags from both inputs */
1513 u16 type1; /* Numeric type of left operand */
1514 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001515 i64 iA; /* Integer value of left operand */
1516 i64 iB; /* Integer value of right operand */
1517 double rA; /* Real value of left operand */
1518 double rB; /* Real value of right operand */
1519
drh3c657212009-11-17 23:59:58 +00001520 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001521 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001522 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001523 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001524 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001525 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001526 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001527 iA = pIn1->u.i;
1528 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001529 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001530 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001531 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1532 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1533 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001534 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001535 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001536 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001537 iB /= iA;
drh75897232000-05-29 14:26:00 +00001538 break;
1539 }
drhbf4133c2001-10-13 02:59:08 +00001540 default: {
drh856c1032009-06-02 15:21:42 +00001541 if( iA==0 ) goto arithmetic_result_is_null;
1542 if( iA==-1 ) iA = 1;
1543 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001544 break;
1545 }
drh75897232000-05-29 14:26:00 +00001546 }
drh856c1032009-06-02 15:21:42 +00001547 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001548 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001549 }else if( (flags & MEM_Null)!=0 ){
1550 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001551 }else{
drhbe707b32012-12-10 22:19:14 +00001552 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001553fp_math:
drh856c1032009-06-02 15:21:42 +00001554 rA = sqlite3VdbeRealValue(pIn1);
1555 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001556 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001557 case OP_Add: rB += rA; break;
1558 case OP_Subtract: rB -= rA; break;
1559 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001560 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001561 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001562 if( rA==(double)0 ) goto arithmetic_result_is_null;
1563 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001564 break;
1565 }
drhbf4133c2001-10-13 02:59:08 +00001566 default: {
shane75ac1de2009-06-09 18:58:52 +00001567 iA = (i64)rA;
1568 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001569 if( iA==0 ) goto arithmetic_result_is_null;
1570 if( iA==-1 ) iA = 1;
1571 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001572 break;
1573 }
drh5e00f6c2001-09-13 13:46:56 +00001574 }
drhc5a7b512010-01-13 16:25:42 +00001575#ifdef SQLITE_OMIT_FLOATING_POINT
1576 pOut->u.i = rB;
1577 MemSetTypeFlag(pOut, MEM_Int);
1578#else
drh856c1032009-06-02 15:21:42 +00001579 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001580 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001581 }
drh74eaba42014-09-18 17:52:15 +00001582 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001583 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001584 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001585 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001586 }
drhc5a7b512010-01-13 16:25:42 +00001587#endif
drh5e00f6c2001-09-13 13:46:56 +00001588 }
1589 break;
1590
drha05a7222008-01-19 03:35:58 +00001591arithmetic_result_is_null:
1592 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001593 break;
1594}
1595
drh7a957892012-02-02 17:35:43 +00001596/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001597**
drhbb6783b2017-04-29 18:02:49 +00001598** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001599** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1600** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001601** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001602**
drh7a957892012-02-02 17:35:43 +00001603** If P1 is not zero, then it is a register that a subsequent min() or
1604** max() aggregate will set to 1 if the current row is not the minimum or
1605** maximum. The P1 register is initialized to 0 by this instruction.
1606**
danielk1977dc1bdc42004-06-11 10:51:27 +00001607** The interface used by the implementation of the aforementioned functions
1608** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001609** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001610*/
drh9cbf3422008-01-17 16:22:13 +00001611case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001612 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001613 if( pOp->p1 ){
1614 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1615 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001616 break;
1617}
1618
drh98757152008-01-09 23:04:12 +00001619/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001620** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001621**
drh98757152008-01-09 23:04:12 +00001622** Take the bit-wise AND of the values in register P1 and P2 and
1623** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001624** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001625*/
drh98757152008-01-09 23:04:12 +00001626/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001627** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001628**
drh98757152008-01-09 23:04:12 +00001629** Take the bit-wise OR of the values in register P1 and P2 and
1630** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001631** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001632*/
drh98757152008-01-09 23:04:12 +00001633/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001634** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001635**
drh98757152008-01-09 23:04:12 +00001636** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001637** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001638** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001639** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001640*/
drh98757152008-01-09 23:04:12 +00001641/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001642** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001643**
drh98757152008-01-09 23:04:12 +00001644** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001645** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001646** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001647** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001648*/
drh5b6afba2008-01-05 16:29:28 +00001649case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1650case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1651case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1652case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001653 i64 iA;
1654 u64 uA;
1655 i64 iB;
1656 u8 op;
drh6810ce62004-01-31 19:22:56 +00001657
drh3c657212009-11-17 23:59:58 +00001658 pIn1 = &aMem[pOp->p1];
1659 pIn2 = &aMem[pOp->p2];
1660 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001661 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001662 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001663 break;
1664 }
drh158b9cb2011-03-05 20:59:46 +00001665 iA = sqlite3VdbeIntValue(pIn2);
1666 iB = sqlite3VdbeIntValue(pIn1);
1667 op = pOp->opcode;
1668 if( op==OP_BitAnd ){
1669 iA &= iB;
1670 }else if( op==OP_BitOr ){
1671 iA |= iB;
1672 }else if( iB!=0 ){
1673 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1674
1675 /* If shifting by a negative amount, shift in the other direction */
1676 if( iB<0 ){
1677 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1678 op = 2*OP_ShiftLeft + 1 - op;
1679 iB = iB>(-64) ? -iB : 64;
1680 }
1681
1682 if( iB>=64 ){
1683 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1684 }else{
1685 memcpy(&uA, &iA, sizeof(uA));
1686 if( op==OP_ShiftLeft ){
1687 uA <<= iB;
1688 }else{
1689 uA >>= iB;
1690 /* Sign-extend on a right shift of a negative number */
1691 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1692 }
1693 memcpy(&iA, &uA, sizeof(iA));
1694 }
drhbf4133c2001-10-13 02:59:08 +00001695 }
drh158b9cb2011-03-05 20:59:46 +00001696 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001697 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001698 break;
1699}
1700
drh8558cde2008-01-05 05:20:10 +00001701/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001702** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001703**
danielk19770cdc0222008-06-26 18:04:03 +00001704** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001705** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001706**
drh8558cde2008-01-05 05:20:10 +00001707** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001708*/
drh9cbf3422008-01-17 16:22:13 +00001709case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001710 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001711 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001712 sqlite3VdbeMemIntegerify(pIn1);
1713 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001714 break;
1715}
1716
drh9cbf3422008-01-17 16:22:13 +00001717/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001718**
drh9cbf3422008-01-17 16:22:13 +00001719** Force the value in register P1 to be an integer. If the value
1720** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001721** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001722** raise an SQLITE_MISMATCH exception.
1723*/
drh9cbf3422008-01-17 16:22:13 +00001724case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001725 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001726 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001727 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001728 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001729 if( (pIn1->flags & MEM_Int)==0 ){
1730 if( pOp->p2==0 ){
1731 rc = SQLITE_MISMATCH;
1732 goto abort_due_to_error;
1733 }else{
drhf56fa462015-04-13 21:39:54 +00001734 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001735 }
drh8aff1012001-12-22 14:49:24 +00001736 }
drh8aff1012001-12-22 14:49:24 +00001737 }
drh83b301b2013-11-20 00:59:02 +00001738 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001739 break;
1740}
1741
drh13573c72010-01-12 17:04:07 +00001742#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001743/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001744**
drh2133d822008-01-03 18:44:59 +00001745** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001746**
drh8a512562005-11-14 22:29:05 +00001747** This opcode is used when extracting information from a column that
1748** has REAL affinity. Such column values may still be stored as
1749** integers, for space efficiency, but after extraction we want them
1750** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001751*/
drh9cbf3422008-01-17 16:22:13 +00001752case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001753 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001754 if( pIn1->flags & MEM_Int ){
1755 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001756 }
drh487e2622005-06-25 18:42:14 +00001757 break;
1758}
drh13573c72010-01-12 17:04:07 +00001759#endif
drh487e2622005-06-25 18:42:14 +00001760
drh8df447f2005-11-01 15:48:24 +00001761#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001762/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001763** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001764**
drh4169e432014-08-25 20:11:52 +00001765** Force the value in register P1 to be the type defined by P2.
1766**
1767** <ul>
drhbb6783b2017-04-29 18:02:49 +00001768** <li> P2=='A' &rarr; BLOB
1769** <li> P2=='B' &rarr; TEXT
1770** <li> P2=='C' &rarr; NUMERIC
1771** <li> P2=='D' &rarr; INTEGER
1772** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001773** </ul>
drh487e2622005-06-25 18:42:14 +00001774**
1775** A NULL value is not changed by this routine. It remains NULL.
1776*/
drh4169e432014-08-25 20:11:52 +00001777case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001778 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001779 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001780 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001781 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1782 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1783 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001784 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001785 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001786 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001787 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001788 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001789 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001790 break;
1791}
drh8a512562005-11-14 22:29:05 +00001792#endif /* SQLITE_OMIT_CAST */
1793
drh79752b62016-08-13 10:02:17 +00001794/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001795** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001796**
1797** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1798** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1799** store the result of comparison in register P2.
1800**
1801** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1802** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1803** to coerce both inputs according to this affinity before the
1804** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1805** affinity is used. Note that the affinity conversions are stored
1806** back into the input registers P1 and P3. So this opcode can cause
1807** persistent changes to registers P1 and P3.
1808**
1809** Once any conversions have taken place, and neither value is NULL,
1810** the values are compared. If both values are blobs then memcmp() is
1811** used to determine the results of the comparison. If both values
1812** are text, then the appropriate collating function specified in
1813** P4 is used to do the comparison. If P4 is not specified then
1814** memcmp() is used to compare text string. If both values are
1815** numeric, then a numeric comparison is used. If the two values
1816** are of different types, then numbers are considered less than
1817** strings and strings are considered less than blobs.
1818**
1819** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1820** true or false and is never NULL. If both operands are NULL then the result
1821** of comparison is true. If either operand is NULL then the result is false.
1822** If neither operand is NULL the result is the same as it would be if
1823** the SQLITE_NULLEQ flag were omitted from P5.
1824**
1825** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001826** content of r[P2] is only changed if the new value is NULL or 0 (false).
1827** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001828*/
1829/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001830** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001831**
1832** This works just like the Eq opcode except that the jump is taken if
1833** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1834** additional information.
1835**
1836** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001837** content of r[P2] is only changed if the new value is NULL or 1 (true).
1838** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001839*/
drh35573352008-01-08 23:54:25 +00001840/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001841** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001842**
drh35573352008-01-08 23:54:25 +00001843** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001844** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1845** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001846**
drh35573352008-01-08 23:54:25 +00001847** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001848** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001849** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001850**
drh35573352008-01-08 23:54:25 +00001851** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001852** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001853** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001854** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001855** affinity is used. Note that the affinity conversions are stored
1856** back into the input registers P1 and P3. So this opcode can cause
1857** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001858**
1859** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001860** the values are compared. If both values are blobs then memcmp() is
1861** used to determine the results of the comparison. If both values
1862** are text, then the appropriate collating function specified in
1863** P4 is used to do the comparison. If P4 is not specified then
1864** memcmp() is used to compare text string. If both values are
1865** numeric, then a numeric comparison is used. If the two values
1866** are of different types, then numbers are considered less than
1867** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001868*/
drh9cbf3422008-01-17 16:22:13 +00001869/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001870** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001871**
drh35573352008-01-08 23:54:25 +00001872** This works just like the Lt opcode except that the jump is taken if
1873** the content of register P3 is less than or equal to the content of
1874** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001875*/
drh9cbf3422008-01-17 16:22:13 +00001876/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001877** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001878**
drh35573352008-01-08 23:54:25 +00001879** This works just like the Lt opcode except that the jump is taken if
1880** the content of register P3 is greater than the content of
1881** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001882*/
drh9cbf3422008-01-17 16:22:13 +00001883/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001884** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001885**
drh35573352008-01-08 23:54:25 +00001886** This works just like the Lt opcode except that the jump is taken if
1887** the content of register P3 is greater than or equal to the content of
1888** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001889*/
drh9cbf3422008-01-17 16:22:13 +00001890case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1891case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1892case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1893case OP_Le: /* same as TK_LE, jump, in1, in3 */
1894case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1895case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001896 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001897 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001898 u16 flags1; /* Copy of initial value of pIn1->flags */
1899 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001900
drh3c657212009-11-17 23:59:58 +00001901 pIn1 = &aMem[pOp->p1];
1902 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001903 flags1 = pIn1->flags;
1904 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001905 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001906 /* One or both operands are NULL */
1907 if( pOp->p5 & SQLITE_NULLEQ ){
1908 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1909 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1910 ** or not both operands are null.
1911 */
1912 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001913 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001914 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drhc3191d22016-10-18 16:36:15 +00001915 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001916 && (flags3&MEM_Cleared)==0
1917 ){
drh4910a762016-09-03 01:46:15 +00001918 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00001919 }else{
drh4910a762016-09-03 01:46:15 +00001920 res = 1; /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00001921 }
drh6a2fe092009-09-23 02:29:36 +00001922 }else{
1923 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1924 ** then the result is always NULL.
1925 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1926 */
drh688852a2014-02-17 22:40:43 +00001927 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001928 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001929 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00001930 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001931 MemSetTypeFlag(pOut, MEM_Null);
1932 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001933 }else{
drhf4345e42014-02-18 11:31:59 +00001934 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001935 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001936 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001937 }
drh6a2fe092009-09-23 02:29:36 +00001938 }
1939 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001940 }
drh6a2fe092009-09-23 02:29:36 +00001941 }else{
1942 /* Neither operand is NULL. Do a comparison. */
1943 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001944 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00001945 if( (flags1 | flags3)&MEM_Str ){
1946 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1947 applyNumericAffinity(pIn1,0);
drh64caee42016-09-09 19:33:00 +00001948 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
drh4b37cd42016-06-25 11:43:47 +00001949 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00001950 }
1951 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1952 applyNumericAffinity(pIn3,0);
1953 }
drh24a09622014-09-18 16:28:59 +00001954 }
drh64caee42016-09-09 19:33:00 +00001955 /* Handle the common case of integer comparison here, as an
1956 ** optimization, to avoid a call to sqlite3MemCompare() */
1957 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
1958 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
1959 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
1960 res = 0;
1961 goto compare_op;
1962 }
drh24a09622014-09-18 16:28:59 +00001963 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00001964 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001965 testcase( pIn1->flags & MEM_Int );
1966 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001967 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001968 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
1969 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00001970 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00001971 }
drhe5520e22015-12-31 04:34:26 +00001972 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001973 testcase( pIn3->flags & MEM_Int );
1974 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001975 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001976 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
1977 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00001978 }
drh6a2fe092009-09-23 02:29:36 +00001979 }
drh6a2fe092009-09-23 02:29:36 +00001980 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00001981 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001982 }
drh64caee42016-09-09 19:33:00 +00001983compare_op:
drh58596362017-08-03 00:29:23 +00001984 /* At this point, res is negative, zero, or positive if reg[P1] is
1985 ** less than, equal to, or greater than reg[P3], respectively. Compute
1986 ** the answer to this operator in res2, depending on what the comparison
1987 ** operator actually is. The next block of code depends on the fact
1988 ** that the 6 comparison operators are consecutive integers in this
1989 ** order: NE, EQ, GT, LE, LT, GE */
1990 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
1991 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
1992 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
1993 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
1994 res2 = aLTb[pOp->opcode - OP_Ne];
1995 }else if( res==0 ){
1996 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
1997 res2 = aEQb[pOp->opcode - OP_Ne];
1998 }else{
1999 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
2000 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00002001 }
2002
drhf56fa462015-04-13 21:39:54 +00002003 /* Undo any changes made by applyAffinity() to the input registers. */
2004 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2005 pIn1->flags = flags1;
2006 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2007 pIn3->flags = flags3;
2008
drh35573352008-01-08 23:54:25 +00002009 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002010 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002011 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00002012 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002013 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002014 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2015 ** is only used in contexts where either:
2016 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2017 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2018 ** Therefore it is not necessary to check the content of r[P2] for
2019 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002020 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002021 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002022 testcase( res2==0 && pOp->opcode==OP_Eq );
2023 testcase( res2==1 && pOp->opcode==OP_Eq );
2024 testcase( res2==0 && pOp->opcode==OP_Ne );
2025 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002026 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002027 }
drh2b4ded92010-09-27 21:09:31 +00002028 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002029 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002030 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002031 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002032 }else{
drhf4345e42014-02-18 11:31:59 +00002033 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002034 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002035 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002036 }
danielk1977a37cdde2004-05-16 11:15:36 +00002037 }
2038 break;
2039}
drhc9b84a12002-06-20 11:36:48 +00002040
drh79752b62016-08-13 10:02:17 +00002041/* Opcode: ElseNotEq * P2 * * *
2042**
drhfd7459e2016-09-17 17:39:01 +00002043** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2044** If result of an OP_Eq comparison on the same two operands
2045** would have be NULL or false (0), then then jump to P2.
2046** If the result of an OP_Eq comparison on the two previous operands
2047** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002048*/
2049case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2050 assert( pOp>aOp );
2051 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002052 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002053 VdbeBranchTaken(iCompare!=0, 2);
2054 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002055 break;
2056}
2057
2058
drh0acb7e42008-06-25 00:12:41 +00002059/* Opcode: Permutation * * * P4 *
2060**
drhb7dab702017-01-26 18:00:00 +00002061** Set the permutation used by the OP_Compare operator in the next
2062** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002063**
drh953f7612012-12-07 22:18:54 +00002064** The permutation is only valid until the next OP_Compare that has
2065** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2066** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002067**
2068** The first integer in the P4 integer array is the length of the array
2069** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002070*/
2071case OP_Permutation: {
2072 assert( pOp->p4type==P4_INTARRAY );
2073 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002074 assert( pOp[1].opcode==OP_Compare );
2075 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002076 break;
2077}
2078
drh953f7612012-12-07 22:18:54 +00002079/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002080** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002081**
drh710c4842010-08-30 01:17:20 +00002082** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2083** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002084** the comparison for use by the next OP_Jump instruct.
2085**
drh0ca10df2012-12-08 13:26:23 +00002086** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2087** determined by the most recent OP_Permutation operator. If the
2088** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2089** order.
2090**
drh0acb7e42008-06-25 00:12:41 +00002091** P4 is a KeyInfo structure that defines collating sequences and sort
2092** orders for the comparison. The permutation applies to registers
2093** only. The KeyInfo elements are used sequentially.
2094**
2095** The comparison is a sort comparison, so NULLs compare equal,
2096** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002097** and strings are less than blobs.
2098*/
2099case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002100 int n;
2101 int i;
2102 int p1;
2103 int p2;
2104 const KeyInfo *pKeyInfo;
2105 int idx;
2106 CollSeq *pColl; /* Collating sequence to use on this term */
2107 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002108 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002109
drhb7dab702017-01-26 18:00:00 +00002110 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2111 aPermute = 0;
2112 }else{
2113 assert( pOp>aOp );
2114 assert( pOp[-1].opcode==OP_Permutation );
2115 assert( pOp[-1].p4type==P4_INTARRAY );
2116 aPermute = pOp[-1].p4.ai + 1;
2117 assert( aPermute!=0 );
2118 }
drh856c1032009-06-02 15:21:42 +00002119 n = pOp->p3;
2120 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002121 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002122 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002123 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002124 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002125#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002126 if( aPermute ){
2127 int k, mx = 0;
2128 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002129 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2130 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002131 }else{
drh9f6168b2016-03-19 23:32:58 +00002132 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2133 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002134 }
2135#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002136 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002137 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002138 assert( memIsValid(&aMem[p1+idx]) );
2139 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002140 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2141 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002142 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002143 pColl = pKeyInfo->aColl[i];
2144 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002145 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002146 if( iCompare ){
2147 if( bRev ) iCompare = -iCompare;
2148 break;
2149 }
drh16ee60f2008-06-20 18:13:25 +00002150 }
2151 break;
2152}
2153
2154/* Opcode: Jump P1 P2 P3 * *
2155**
2156** Jump to the instruction at address P1, P2, or P3 depending on whether
2157** in the most recent OP_Compare instruction the P1 vector was less than
2158** equal to, or greater than the P2 vector, respectively.
2159*/
drh0acb7e42008-06-25 00:12:41 +00002160case OP_Jump: { /* jump */
2161 if( iCompare<0 ){
drhf56fa462015-04-13 21:39:54 +00002162 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002163 }else if( iCompare==0 ){
drhf56fa462015-04-13 21:39:54 +00002164 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002165 }else{
drhf56fa462015-04-13 21:39:54 +00002166 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002167 }
2168 break;
2169}
2170
drh5b6afba2008-01-05 16:29:28 +00002171/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002172** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002173**
drh5b6afba2008-01-05 16:29:28 +00002174** Take the logical AND of the values in registers P1 and P2 and
2175** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002176**
drh5b6afba2008-01-05 16:29:28 +00002177** If either P1 or P2 is 0 (false) then the result is 0 even if
2178** the other input is NULL. A NULL and true or two NULLs give
2179** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002180*/
drh5b6afba2008-01-05 16:29:28 +00002181/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002182** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002183**
2184** Take the logical OR of the values in register P1 and P2 and
2185** store the answer in register P3.
2186**
2187** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2188** even if the other input is NULL. A NULL and false or two NULLs
2189** give a NULL output.
2190*/
2191case OP_And: /* same as TK_AND, in1, in2, out3 */
2192case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002193 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2194 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002195
drh1fcfa722018-02-26 15:27:31 +00002196 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2197 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002198 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002199 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002200 v1 = and_logic[v1*3+v2];
2201 }else{
drh5b6afba2008-01-05 16:29:28 +00002202 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002203 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002204 }
drh3c657212009-11-17 23:59:58 +00002205 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002206 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002207 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002208 }else{
drh5b6afba2008-01-05 16:29:28 +00002209 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002210 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002211 }
drh5e00f6c2001-09-13 13:46:56 +00002212 break;
2213}
2214
drh8abed7b2018-02-26 18:49:05 +00002215/* Opcode: IsTrue P1 P2 P3 P4 *
2216** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2217**
2218** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2219** IS NOT FALSE operators.
2220**
drh96acafb2018-02-27 14:49:25 +00002221** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002222** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2223** NULL, then the P3 is stored in register P2. Invert the answer if P4
2224** is 1.
2225**
2226** The logic is summarized like this:
2227**
2228** <ul>
drh96acafb2018-02-27 14:49:25 +00002229** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2230** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2231** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2232** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002233** </ul>
2234*/
2235case OP_IsTrue: { /* in1, out2 */
2236 assert( pOp->p4type==P4_INT32 );
2237 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002238 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002239 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2240 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2241 break;
2242}
2243
drhe99fa2a2008-12-15 15:27:51 +00002244/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002245** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002246**
drhe99fa2a2008-12-15 15:27:51 +00002247** Interpret the value in register P1 as a boolean value. Store the
2248** boolean complement in register P2. If the value in register P1 is
2249** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002250*/
drh93952eb2009-11-13 19:43:43 +00002251case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002252 pIn1 = &aMem[pOp->p1];
2253 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002254 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002255 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002256 }else{
2257 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002258 }
drh5e00f6c2001-09-13 13:46:56 +00002259 break;
2260}
2261
drhe99fa2a2008-12-15 15:27:51 +00002262/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002263** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002264**
drhe99fa2a2008-12-15 15:27:51 +00002265** Interpret the content of register P1 as an integer. Store the
2266** ones-complement of the P1 value into register P2. If P1 holds
2267** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002268*/
drh93952eb2009-11-13 19:43:43 +00002269case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002270 pIn1 = &aMem[pOp->p1];
2271 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002272 sqlite3VdbeMemSetNull(pOut);
2273 if( (pIn1->flags & MEM_Null)==0 ){
2274 pOut->flags = MEM_Int;
2275 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002276 }
drhbf4133c2001-10-13 02:59:08 +00002277 break;
2278}
2279
drh48f2d3b2011-09-16 01:34:43 +00002280/* Opcode: Once P1 P2 * * *
2281**
drhab087d42017-03-24 17:59:56 +00002282** Fall through to the next instruction the first time this opcode is
2283** encountered on each invocation of the byte-code program. Jump to P2
2284** on the second and all subsequent encounters during the same invocation.
2285**
2286** Top-level programs determine first invocation by comparing the P1
2287** operand against the P1 operand on the OP_Init opcode at the beginning
2288** of the program. If the P1 values differ, then fall through and make
2289** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2290** the same then take the jump.
2291**
2292** For subprograms, there is a bitmask in the VdbeFrame that determines
2293** whether or not the jump should be taken. The bitmask is necessary
2294** because the self-altering code trick does not work for recursive
2295** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002296*/
dan1d8cb212011-12-09 13:24:16 +00002297case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002298 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002299 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002300 if( p->pFrame ){
2301 iAddr = (int)(pOp - p->aOp);
2302 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2303 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002304 goto jump_to_p2;
2305 }
drh18333ef2017-03-24 18:38:41 +00002306 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002307 }else{
drhab087d42017-03-24 17:59:56 +00002308 if( p->aOp[0].p1==pOp->p1 ){
2309 VdbeBranchTaken(1, 2);
2310 goto jump_to_p2;
2311 }
dan1d8cb212011-12-09 13:24:16 +00002312 }
drhab087d42017-03-24 17:59:56 +00002313 VdbeBranchTaken(0, 2);
2314 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002315 break;
2316}
2317
drh3c84ddf2008-01-09 02:15:38 +00002318/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002319**
drhef8662b2011-06-20 21:47:58 +00002320** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002321** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002322** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002323*/
drh1fcfa722018-02-26 15:27:31 +00002324case OP_If: { /* jump, in1 */
2325 int c;
2326 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2327 VdbeBranchTaken(c!=0, 2);
2328 if( c ) goto jump_to_p2;
2329 break;
2330}
2331
drh3c84ddf2008-01-09 02:15:38 +00002332/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002333**
drhef8662b2011-06-20 21:47:58 +00002334** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002335** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002336** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002337*/
drh9cbf3422008-01-17 16:22:13 +00002338case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002339 int c;
drh1fcfa722018-02-26 15:27:31 +00002340 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002341 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002342 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002343 break;
2344}
2345
drh830ecf92009-06-18 00:41:55 +00002346/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002347** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002348**
drh830ecf92009-06-18 00:41:55 +00002349** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002350*/
drh9cbf3422008-01-17 16:22:13 +00002351case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002352 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002353 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002354 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002355 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002356 }
drh477df4b2008-01-05 18:48:24 +00002357 break;
2358}
2359
drh98757152008-01-09 23:04:12 +00002360/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002361** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002362**
drh6a288a32008-01-07 19:20:24 +00002363** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002364*/
drh9cbf3422008-01-17 16:22:13 +00002365case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002366 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002367 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002368 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002369 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002370 }
drh5e00f6c2001-09-13 13:46:56 +00002371 break;
2372}
2373
drh31d6fd52017-04-14 19:03:10 +00002374/* Opcode: IfNullRow P1 P2 P3 * *
2375** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2376**
2377** Check the cursor P1 to see if it is currently pointing at a NULL row.
2378** If it is, then set register P3 to NULL and jump immediately to P2.
2379** If P1 is not on a NULL row, then fall through without making any
2380** changes.
2381*/
2382case OP_IfNullRow: { /* jump */
2383 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002384 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002385 if( p->apCsr[pOp->p1]->nullRow ){
2386 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2387 goto jump_to_p2;
2388 }
2389 break;
2390}
2391
drh092457b2017-12-29 15:04:49 +00002392#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2393/* Opcode: Offset P1 P2 P3 * *
2394** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002395**
drh092457b2017-12-29 15:04:49 +00002396** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002397** start of the payload for the record at which that cursor P1 is currently
2398** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002399**
drh092457b2017-12-29 15:04:49 +00002400** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002401** This opcode does not use P2 itself, but the P2 value is used by the
2402** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002403** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002404**
2405** This opcode is only available if SQLite is compiled with the
2406** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002407*/
drh092457b2017-12-29 15:04:49 +00002408case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002409 VdbeCursor *pC; /* The VDBE cursor */
2410 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2411 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002412 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002413 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002414 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002415 }else{
drh092457b2017-12-29 15:04:49 +00002416 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002417 }
2418 break;
2419}
drh092457b2017-12-29 15:04:49 +00002420#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002421
drh3e9ca092009-09-08 01:14:48 +00002422/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002423** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002424**
danielk1977cfcdaef2004-05-12 07:33:33 +00002425** Interpret the data that cursor P1 points to as a structure built using
2426** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002427** information about the format of the data.) Extract the P2-th column
2428** from this record. If there are less that (P2+1)
2429** values in the record, extract a NULL.
2430**
drh9cbf3422008-01-17 16:22:13 +00002431** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002432**
drh1cc3a362017-04-03 13:17:31 +00002433** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002434** if the P4 argument is a P4_MEM use the value of the P4 argument as
2435** the result.
drh3e9ca092009-09-08 01:14:48 +00002436**
2437** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2438** then the cache of the cursor is reset prior to extracting the column.
2439** The first OP_Column against a pseudo-table after the value of the content
2440** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002441**
drh1cc3a362017-04-03 13:17:31 +00002442** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002443** the result is guaranteed to only be used as the argument of a length()
2444** or typeof() function, respectively. The loading of large blobs can be
2445** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002446*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002447case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002448 int p2; /* column number to retrieve */
2449 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002450 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002451 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002452 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002453 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002454 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002455 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002456 const u8 *zData; /* Part of the record being decoded */
2457 const u8 *zHdr; /* Next unparsed byte of the header */
2458 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002459 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002460 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002461 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002462
dande892d92016-01-29 19:29:45 +00002463 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002464 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002465
drh170ad682017-06-02 15:44:22 +00002466 /* If the cursor cache is stale (meaning it is not currently point at
2467 ** the correct row) then bring it up-to-date by doing the necessary
2468 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002469 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002470 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002471
drh9f6168b2016-03-19 23:32:58 +00002472 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002473 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002474 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002475 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002476 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002477 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002478 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002479 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002480 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2481 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002482
drha43a02e2016-05-19 17:51:19 +00002483 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002484 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002485 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002486 /* For the special case of as pseudo-cursor, the seekResult field
2487 ** identifies the register that holds the record */
2488 assert( pC->seekResult>0 );
2489 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002490 assert( pReg->flags & MEM_Blob );
2491 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002492 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002493 pC->aRow = (u8*)pReg->z;
2494 }else{
drh6b5631e2014-11-05 15:57:39 +00002495 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002496 goto op_column_out;
2497 }
danielk1977192ac1d2004-05-10 07:17:30 +00002498 }else{
drh06a09a82016-11-25 17:03:03 +00002499 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002500 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002501 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002502 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2503 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002504 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2505 assert( pC->szRow<=pC->payloadSize );
2506 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2507 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002508 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002509 }
danielk1977192ac1d2004-05-10 07:17:30 +00002510 }
drhb73857f2006-03-17 00:25:59 +00002511 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002512 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002513 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002514
drhc81aa2e2014-10-11 23:31:52 +00002515
drh1f613c42017-08-16 14:16:19 +00002516 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002517 /* pC->aRow does not have to hold the entire row, but it does at least
2518 ** need to cover the header of the record. If pC->aRow does not contain
2519 ** the complete header, then set it to zero, forcing the header to be
2520 ** dynamically allocated. */
2521 pC->aRow = 0;
2522 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002523
2524 /* Make sure a corrupt database has not given us an oversize header.
2525 ** Do this now to avoid an oversize memory allocation.
2526 **
2527 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2528 ** types use so much data space that there can only be 4096 and 32 of
2529 ** them, respectively. So the maximum header length results from a
2530 ** 3-byte type for each of the maximum of 32768 columns plus three
2531 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2532 */
drh1f613c42017-08-16 14:16:19 +00002533 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002534 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002535 }
drh95b225a2017-08-16 11:04:22 +00002536 }else{
2537 /* This is an optimization. By skipping over the first few tests
2538 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2539 ** measurable performance gain.
2540 **
drh1f613c42017-08-16 14:16:19 +00002541 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002542 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002543 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002544 ** branch jumps to reads past the end of the record, but never more
2545 ** than a few bytes. Even if the record occurs at the end of the page
2546 ** content area, the "page header" comes after the page content and so
2547 ** this overread is harmless. Similar overreads can occur for a corrupt
2548 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002549 */
2550 zData = pC->aRow;
2551 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002552 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002553 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002554 }
drh399af1d2013-11-20 17:25:55 +00002555 }
drh35cd6432009-06-05 14:17:21 +00002556
drh399af1d2013-11-20 17:25:55 +00002557 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002558 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002559 */
drhc8606e42013-11-20 19:28:03 +00002560 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002561 /* If there is more header available for parsing in the record, try
2562 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002563 */
drhc8606e42013-11-20 19:28:03 +00002564 if( pC->iHdrOffset<aOffset[0] ){
2565 /* Make sure zData points to enough of the record to cover the header. */
2566 if( pC->aRow==0 ){
2567 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002568 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002569 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002570 zData = (u8*)sMem.z;
2571 }else{
2572 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002573 }
drhc8606e42013-11-20 19:28:03 +00002574
drh0c8f7602014-09-19 16:56:45 +00002575 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002576 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002577 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002578 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002579 zHdr = zData + pC->iHdrOffset;
2580 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002581 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002582 do{
drh95fa6062015-10-16 13:50:08 +00002583 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002584 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002585 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002586 }else{
drhc8606e42013-11-20 19:28:03 +00002587 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002588 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002589 }
drhfaf37272015-10-16 14:23:42 +00002590 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002591 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002592 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002593
drh8dd83622014-10-13 23:39:02 +00002594 /* The record is corrupt if any of the following are true:
2595 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002596 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002597 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002598 */
drhc6ce38832015-10-15 21:30:24 +00002599 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2600 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002601 ){
drh95b225a2017-08-16 11:04:22 +00002602 if( aOffset[0]==0 ){
2603 i = 0;
2604 zHdr = zEndHdr;
2605 }else{
2606 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002607 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002608 }
danielk1977dedf45b2006-01-13 17:12:01 +00002609 }
drhddb2b4a2016-03-25 12:10:32 +00002610
drh170c2762016-05-20 21:40:11 +00002611 pC->nHdrParsed = i;
2612 pC->iHdrOffset = (u32)(zHdr - zData);
2613 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002614 }else{
drh9fbc8852016-01-04 03:48:46 +00002615 t = 0;
drh9188b382004-05-14 21:12:22 +00002616 }
drhd3194f52004-05-27 19:59:32 +00002617
drhf2db3382015-04-30 20:33:25 +00002618 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002619 ** still not up to p2, that means that the record has fewer than p2
2620 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002621 */
drhc8606e42013-11-20 19:28:03 +00002622 if( pC->nHdrParsed<=p2 ){
2623 if( pOp->p4type==P4_MEM ){
2624 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2625 }else{
drh22e8d832014-10-29 00:58:38 +00002626 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002627 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002628 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002629 }
drh95fa6062015-10-16 13:50:08 +00002630 }else{
2631 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002632 }
danielk1977192ac1d2004-05-10 07:17:30 +00002633
drh380d6852013-11-20 20:58:00 +00002634 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002635 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002636 ** all valid.
drh9188b382004-05-14 21:12:22 +00002637 */
drhc8606e42013-11-20 19:28:03 +00002638 assert( p2<pC->nHdrParsed );
2639 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002640 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002641 if( VdbeMemDynamic(pDest) ){
2642 sqlite3VdbeMemSetNull(pDest);
2643 }
drh95fa6062015-10-16 13:50:08 +00002644 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002645 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002646 /* This is the common case where the desired content fits on the original
2647 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002648 zData = pC->aRow + aOffset[p2];
2649 if( t<12 ){
2650 sqlite3VdbeSerialGet(zData, t, pDest);
2651 }else{
2652 /* If the column value is a string, we need a persistent value, not
2653 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2654 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2655 */
2656 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2657 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002658 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002659 if( pDest->szMalloc < len+2 ){
2660 pDest->flags = MEM_Null;
2661 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2662 }else{
2663 pDest->z = pDest->zMalloc;
2664 }
2665 memcpy(pDest->z, zData, len);
2666 pDest->z[len] = 0;
2667 pDest->z[len+1] = 0;
2668 pDest->flags = aFlag[t&1];
2669 }
danielk197736963fd2005-02-19 08:18:05 +00002670 }else{
drha1851ef2016-05-20 19:51:28 +00002671 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002672 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002673 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2674 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2675 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002676 ){
drh2a2a6962014-09-16 18:22:44 +00002677 /* Content is irrelevant for
2678 ** 1. the typeof() function,
2679 ** 2. the length(X) function if X is a blob, and
2680 ** 3. if the content length is zero.
2681 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002682 ** content from disk.
2683 **
2684 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2685 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2686 ** read up to 16. So 16 bytes of bogus content is supplied.
2687 */
2688 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002689 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002690 }else{
drhcb3cabd2016-11-25 19:18:28 +00002691 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002692 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2693 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2694 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002695 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002696 }
drhd3194f52004-05-27 19:59:32 +00002697
danielk19773c9cc8d2005-01-17 03:40:08 +00002698op_column_out:
drhb7654112008-01-12 12:48:07 +00002699 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002700 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002701 break;
drh74588ce2017-09-13 00:13:05 +00002702
2703op_column_corrupt:
2704 if( aOp[0].p3>0 ){
2705 pOp = &aOp[aOp[0].p3-1];
2706 break;
2707 }else{
2708 rc = SQLITE_CORRUPT_BKPT;
2709 goto abort_due_to_error;
2710 }
danielk1977192ac1d2004-05-10 07:17:30 +00002711}
2712
danielk1977751de562008-04-18 09:01:15 +00002713/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002714** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002715**
2716** Apply affinities to a range of P2 registers starting with P1.
2717**
drhbb6783b2017-04-29 18:02:49 +00002718** P4 is a string that is P2 characters long. The N-th character of the
2719** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002720** memory cell in the range.
2721*/
2722case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002723 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002724
drh856c1032009-06-02 15:21:42 +00002725 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002726 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002727 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002728 assert( zAffinity[pOp->p2]==0 );
2729 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002730 do{
drh9f6168b2016-03-19 23:32:58 +00002731 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002732 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002733 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002734 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002735 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002736 break;
2737}
2738
drh1db639c2008-01-17 02:36:28 +00002739/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002740** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002741**
drh710c4842010-08-30 01:17:20 +00002742** Convert P2 registers beginning with P1 into the [record format]
2743** use as a data record in a database table or as a key
2744** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002745**
drhbb6783b2017-04-29 18:02:49 +00002746** P4 may be a string that is P2 characters long. The N-th character of the
2747** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002748** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002749**
drh8a512562005-11-14 22:29:05 +00002750** The mapping from character to affinity is given by the SQLITE_AFF_
2751** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002752**
drh05883a32015-06-02 15:32:08 +00002753** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002754*/
drh1db639c2008-01-17 02:36:28 +00002755case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002756 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2757 Mem *pRec; /* The new record */
2758 u64 nData; /* Number of bytes of data space */
2759 int nHdr; /* Number of bytes of header space */
2760 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002761 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002762 int nVarint; /* Number of bytes in a varint */
2763 u32 serial_type; /* Type field */
2764 Mem *pData0; /* First field to be combined into the record */
2765 Mem *pLast; /* Last field of the record */
2766 int nField; /* Number of fields in the record */
2767 char *zAffinity; /* The affinity string for the record */
2768 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002769 int i; /* Space used in zNewRecord[] header */
2770 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002771 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002772
drhf3218fe2004-05-28 08:21:02 +00002773 /* Assuming the record contains N fields, the record format looks
2774 ** like this:
2775 **
drh7a224de2004-06-02 01:22:02 +00002776 ** ------------------------------------------------------------------------
2777 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2778 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002779 **
drh9cbf3422008-01-17 16:22:13 +00002780 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002781 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002782 **
2783 ** Each type field is a varint representing the serial type of the
2784 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002785 ** hdr-size field is also a varint which is the offset from the beginning
2786 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002787 */
drh856c1032009-06-02 15:21:42 +00002788 nData = 0; /* Number of bytes of data space */
2789 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002790 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002791 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002792 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002793 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002794 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002795 nField = pOp->p2;
2796 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002797 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002798
drh2b4ded92010-09-27 21:09:31 +00002799 /* Identify the output register */
2800 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2801 pOut = &aMem[pOp->p3];
2802 memAboutToChange(p, pOut);
2803
drh3e6c0602013-12-10 20:53:01 +00002804 /* Apply the requested affinity to all inputs
2805 */
2806 assert( pData0<=pLast );
2807 if( zAffinity ){
2808 pRec = pData0;
2809 do{
drh57bf4a82014-02-17 14:59:22 +00002810 applyAffinity(pRec++, *(zAffinity++), encoding);
2811 assert( zAffinity[0]==0 || pRec<=pLast );
2812 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002813 }
2814
drhd447dce2017-01-25 20:55:11 +00002815#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002816 /* NULLs can be safely trimmed from the end of the record, as long as
2817 ** as the schema format is 2 or more and none of the omitted columns
2818 ** have a non-NULL default value. Also, the record must be left with
2819 ** at least one field. If P5>0 then it will be one more than the
2820 ** index of the right-most column with a non-NULL default value */
2821 if( pOp->p5 ){
2822 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2823 pLast--;
2824 nField--;
2825 }
2826 }
drhd447dce2017-01-25 20:55:11 +00002827#endif
drh585ce192017-01-25 14:58:27 +00002828
drhf3218fe2004-05-28 08:21:02 +00002829 /* Loop through the elements that will make up the record to figure
2830 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002831 */
drh038b7bc2013-12-09 23:17:22 +00002832 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002833 do{
drh2b4ded92010-09-27 21:09:31 +00002834 assert( memIsValid(pRec) );
drh41fb3672018-01-12 23:18:38 +00002835 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002836 if( pRec->flags & MEM_Zero ){
drhce2fbd12018-01-12 21:00:14 +00002837 if( serial_type==0 ){
drh41fb3672018-01-12 23:18:38 +00002838 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
2839 ** table methods that never invoke sqlite3_result_xxxxx() while
2840 ** computing an unchanging column value in an UPDATE statement.
2841 ** Give such values a special internal-use-only serial-type of 10
2842 ** so that they can be passed through to xUpdate and have
2843 ** a true sqlite3_value_nochange(). */
2844 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
2845 serial_type = 10;
drhce2fbd12018-01-12 21:00:14 +00002846 }else if( nData ){
drh53e66c32015-07-24 15:49:23 +00002847 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002848 }else{
2849 nZero += pRec->u.nZero;
2850 len -= pRec->u.nZero;
2851 }
drhfdf972a2007-05-02 13:30:27 +00002852 }
drh8079a0d2006-01-12 17:20:50 +00002853 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002854 testcase( serial_type==127 );
2855 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002856 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh41fb3672018-01-12 23:18:38 +00002857 pRec->uTemp = serial_type;
drh45c3c662016-04-07 14:16:16 +00002858 if( pRec==pData0 ) break;
2859 pRec--;
2860 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002861
drh654858d2014-11-20 02:18:14 +00002862 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2863 ** which determines the total number of bytes in the header. The varint
2864 ** value is the size of the header in bytes including the size varint
2865 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002866 testcase( nHdr==126 );
2867 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002868 if( nHdr<=126 ){
2869 /* The common case */
2870 nHdr += 1;
2871 }else{
2872 /* Rare case of a really large header */
2873 nVarint = sqlite3VarintLen(nHdr);
2874 nHdr += nVarint;
2875 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002876 }
drh038b7bc2013-12-09 23:17:22 +00002877 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002878 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002879 goto too_big;
2880 }
drhf3218fe2004-05-28 08:21:02 +00002881
danielk1977a7a8e142008-02-13 18:25:27 +00002882 /* Make sure the output register has a buffer large enough to store
2883 ** the new record. The output register (pOp->p3) is not allowed to
2884 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002885 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002886 */
drh322f2852014-09-19 00:43:39 +00002887 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002888 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002889 }
danielk1977a7a8e142008-02-13 18:25:27 +00002890 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002891
2892 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002893 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002894 j = nHdr;
2895 assert( pData0<=pLast );
2896 pRec = pData0;
2897 do{
drhfacf47a2014-10-13 20:12:47 +00002898 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002899 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2900 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002901 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002902 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2903 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002904 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002905 }while( (++pRec)<=pLast );
2906 assert( i==nHdr );
2907 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002908
drh9f6168b2016-03-19 23:32:58 +00002909 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002910 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002911 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002912 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002913 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002914 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002915 }
drh1013c932008-01-06 00:25:21 +00002916 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002917 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002918 break;
2919}
2920
danielk1977a5533162009-02-24 10:01:51 +00002921/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002922** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002923**
2924** Store the number of entries (an integer value) in the table or index
2925** opened by cursor P1 in register P2
2926*/
2927#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002928case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002929 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002930 BtCursor *pCrsr;
2931
drhc960dcb2015-11-20 19:22:01 +00002932 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2933 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002934 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002935 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002936 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002937 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002938 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002939 pOut->u.i = nEntry;
2940 break;
2941}
2942#endif
2943
danielk1977fd7f0452008-12-17 17:30:26 +00002944/* Opcode: Savepoint P1 * * P4 *
2945**
2946** Open, release or rollback the savepoint named by parameter P4, depending
2947** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2948** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2949*/
2950case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002951 int p1; /* Value of P1 operand */
2952 char *zName; /* Name of savepoint */
2953 int nName;
2954 Savepoint *pNew;
2955 Savepoint *pSavepoint;
2956 Savepoint *pTmp;
2957 int iSavepoint;
2958 int ii;
2959
2960 p1 = pOp->p1;
2961 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002962
2963 /* Assert that the p1 parameter is valid. Also that if there is no open
2964 ** transaction, then there cannot be any savepoints.
2965 */
2966 assert( db->pSavepoint==0 || db->autoCommit==0 );
2967 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2968 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2969 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002970 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002971
2972 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002973 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002974 /* A new savepoint cannot be created if there are active write
2975 ** statements (i.e. open read/write incremental blob handles).
2976 */
drh22c17b82015-05-15 04:13:15 +00002977 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002978 rc = SQLITE_BUSY;
2979 }else{
drh856c1032009-06-02 15:21:42 +00002980 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002981
drhbe07ec52011-06-03 12:15:26 +00002982#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002983 /* This call is Ok even if this savepoint is actually a transaction
2984 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2985 ** If this is a transaction savepoint being opened, it is guaranteed
2986 ** that the db->aVTrans[] array is empty. */
2987 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002988 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2989 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002990 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002991#endif
dand9495cd2011-04-27 12:08:04 +00002992
danielk1977fd7f0452008-12-17 17:30:26 +00002993 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00002994 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00002995 if( pNew ){
2996 pNew->zName = (char *)&pNew[1];
2997 memcpy(pNew->zName, zName, nName+1);
2998
2999 /* If there is no open transaction, then mark this as a special
3000 ** "transaction savepoint". */
3001 if( db->autoCommit ){
3002 db->autoCommit = 0;
3003 db->isTransactionSavepoint = 1;
3004 }else{
3005 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003006 }
dan21e8d012011-03-03 20:05:59 +00003007
danielk1977fd7f0452008-12-17 17:30:26 +00003008 /* Link the new savepoint into the database handle's list. */
3009 pNew->pNext = db->pSavepoint;
3010 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003011 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003012 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003013 }
3014 }
3015 }else{
drh856c1032009-06-02 15:21:42 +00003016 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003017
3018 /* Find the named savepoint. If there is no such savepoint, then an
3019 ** an error is returned to the user. */
3020 for(
drh856c1032009-06-02 15:21:42 +00003021 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003022 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003023 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003024 ){
3025 iSavepoint++;
3026 }
3027 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003028 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003029 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003030 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003031 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003032 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003033 */
drh22c17b82015-05-15 04:13:15 +00003034 sqlite3VdbeError(p, "cannot release savepoint - "
3035 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003036 rc = SQLITE_BUSY;
3037 }else{
3038
3039 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003040 ** and this is a RELEASE command, then the current transaction
3041 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003042 */
3043 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3044 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003045 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003046 goto vdbe_return;
3047 }
danielk1977fd7f0452008-12-17 17:30:26 +00003048 db->autoCommit = 1;
3049 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003050 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003051 db->autoCommit = 0;
3052 p->rc = rc = SQLITE_BUSY;
3053 goto vdbe_return;
3054 }
danielk197734cf35d2008-12-18 18:31:38 +00003055 db->isTransactionSavepoint = 0;
3056 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003057 }else{
drh47b7fc72014-11-11 01:33:57 +00003058 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003059 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003060 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003061 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003062 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003063 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3064 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003065 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003066 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003067 }
drh47b7fc72014-11-11 01:33:57 +00003068 }else{
3069 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003070 }
3071 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003072 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3073 if( rc!=SQLITE_OK ){
3074 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003075 }
danielk1977fd7f0452008-12-17 17:30:26 +00003076 }
drh47b7fc72014-11-11 01:33:57 +00003077 if( isSchemaChange ){
danielk1977fd7f0452008-12-17 17:30:26 +00003078 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00003079 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003080 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003081 }
3082 }
3083
3084 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3085 ** savepoints nested inside of the savepoint being operated on. */
3086 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003087 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003088 db->pSavepoint = pTmp->pNext;
3089 sqlite3DbFree(db, pTmp);
3090 db->nSavepoint--;
3091 }
3092
dan1da40a32009-09-19 17:00:31 +00003093 /* If it is a RELEASE, then destroy the savepoint being operated on
3094 ** too. If it is a ROLLBACK TO, then set the number of deferred
3095 ** constraint violations present in the database to the value stored
3096 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003097 if( p1==SAVEPOINT_RELEASE ){
3098 assert( pSavepoint==db->pSavepoint );
3099 db->pSavepoint = pSavepoint->pNext;
3100 sqlite3DbFree(db, pSavepoint);
3101 if( !isTransaction ){
3102 db->nSavepoint--;
3103 }
dan1da40a32009-09-19 17:00:31 +00003104 }else{
3105 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003106 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003107 }
dand9495cd2011-04-27 12:08:04 +00003108
danea8562e2015-04-18 16:25:54 +00003109 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003110 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3111 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3112 }
danielk1977fd7f0452008-12-17 17:30:26 +00003113 }
3114 }
drh9467abf2016-02-17 18:44:11 +00003115 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003116
3117 break;
3118}
3119
drh98757152008-01-09 23:04:12 +00003120/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003121**
3122** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003123** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003124** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3125** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003126**
3127** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003128*/
drh9cbf3422008-01-17 16:22:13 +00003129case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003130 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003131 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003132
drh856c1032009-06-02 15:21:42 +00003133 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003134 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003135 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003136 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003137 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003138 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003139
drhb0c88652016-02-01 13:21:13 +00003140 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003141 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003142 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003143 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003144 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003145 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3146 /* If this instruction implements a COMMIT and other VMs are writing
3147 ** return an error indicating that the other VMs must complete first.
3148 */
3149 sqlite3VdbeError(p, "cannot commit transaction - "
3150 "SQL statements in progress");
3151 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003152 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003153 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003154 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003155 }else{
shane7d3846a2008-12-11 02:58:26 +00003156 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003157 }
3158 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3159 p->pc = (int)(pOp - aOp);
3160 db->autoCommit = (u8)(1-desiredAutoCommit);
3161 p->rc = rc = SQLITE_BUSY;
3162 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003163 }
danielk1977bd434552009-03-18 10:33:00 +00003164 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003165 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003166 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003167 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003168 }else{
drh900b31e2007-08-28 02:27:51 +00003169 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003170 }
drh900b31e2007-08-28 02:27:51 +00003171 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003172 }else{
drh22c17b82015-05-15 04:13:15 +00003173 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003174 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003175 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003176 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003177
3178 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003179 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003180 }
3181 break;
3182}
3183
drhb22f7c82014-02-06 23:56:27 +00003184/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003185**
drh05a86c52014-02-16 01:55:49 +00003186** Begin a transaction on database P1 if a transaction is not already
3187** active.
3188** If P2 is non-zero, then a write-transaction is started, or if a
3189** read-transaction is already active, it is upgraded to a write-transaction.
3190** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003191**
drh001bbcb2003-03-19 03:14:00 +00003192** P1 is the index of the database file on which the transaction is
3193** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003194** file used for temporary tables. Indices of 2 or more are used for
3195** attached databases.
drhcabb0812002-09-14 13:47:32 +00003196**
dane0af83a2009-09-08 19:15:01 +00003197** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3198** true (this flag is set if the Vdbe may modify more than one row and may
3199** throw an ABORT exception), a statement transaction may also be opened.
3200** More specifically, a statement transaction is opened iff the database
3201** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003202** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003203** VDBE to be rolled back after an error without having to roll back the
3204** entire transaction. If no error is encountered, the statement transaction
3205** will automatically commit when the VDBE halts.
3206**
drhb22f7c82014-02-06 23:56:27 +00003207** If P5!=0 then this opcode also checks the schema cookie against P3
3208** and the schema generation counter against P4.
3209** The cookie changes its value whenever the database schema changes.
3210** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003211** and that the current process needs to reread the schema. If the schema
3212** cookie in P3 differs from the schema cookie in the database header or
3213** if the schema generation counter in P4 differs from the current
3214** generation counter, then an SQLITE_SCHEMA error is raised and execution
3215** halts. The sqlite3_step() wrapper function might then reprepare the
3216** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003217*/
drh9cbf3422008-01-17 16:22:13 +00003218case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003219 Btree *pBt;
drhbb2d9b12018-06-06 16:28:40 +00003220 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003221
drh1713afb2013-06-28 01:24:57 +00003222 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003223 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003224 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003225 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003226 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3227 rc = SQLITE_READONLY;
3228 goto abort_due_to_error;
3229 }
drh653b82a2009-06-22 11:10:47 +00003230 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003231
danielk197724162fe2004-06-04 06:22:00 +00003232 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003233 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003234 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3235 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003236 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003237 if( (rc&0xff)==SQLITE_BUSY ){
3238 p->pc = (int)(pOp - aOp);
3239 p->rc = rc;
3240 goto vdbe_return;
3241 }
danielk197724162fe2004-06-04 06:22:00 +00003242 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003243 }
dane0af83a2009-09-08 19:15:01 +00003244
3245 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003246 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003247 ){
3248 assert( sqlite3BtreeIsInTrans(pBt) );
3249 if( p->iStatement==0 ){
3250 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3251 db->nStatement++;
3252 p->iStatement = db->nSavepoint + db->nStatement;
3253 }
dana311b802011-04-26 19:21:34 +00003254
drh346506f2011-05-25 01:16:42 +00003255 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003256 if( rc==SQLITE_OK ){
3257 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3258 }
dan1da40a32009-09-19 17:00:31 +00003259
3260 /* Store the current value of the database handles deferred constraint
3261 ** counter. If the statement transaction needs to be rolled back,
3262 ** the value of this counter needs to be restored too. */
3263 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003264 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003265 }
drh397776a2018-06-06 17:45:51 +00003266 }
3267 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3268 if( pOp->p5
3269 && (iMeta!=pOp->p3
3270 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
3271 ){
3272 /*
drh96fdcb42016-09-27 00:09:33 +00003273 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3274 ** version is checked to ensure that the schema has not changed since the
3275 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003276 */
drhb22f7c82014-02-06 23:56:27 +00003277 sqlite3DbFree(db, p->zErrMsg);
3278 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3279 /* If the schema-cookie from the database file matches the cookie
3280 ** stored with the in-memory representation of the schema, do
3281 ** not reload the schema from the database file.
3282 **
3283 ** If virtual-tables are in use, this is not just an optimization.
3284 ** Often, v-tables store their data in other SQLite tables, which
3285 ** are queried from within xNext() and other v-table methods using
3286 ** prepared queries. If such a query is out-of-date, we do not want to
3287 ** discard the database schema, as the user code implementing the
3288 ** v-table would have to be ready for the sqlite3_vtab structure itself
3289 ** to be invalidated whenever sqlite3_step() is called from within
3290 ** a v-table method.
3291 */
3292 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3293 sqlite3ResetOneSchema(db, pOp->p1);
3294 }
3295 p->expired = 1;
3296 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003297 }
drh9467abf2016-02-17 18:44:11 +00003298 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003299 break;
3300}
3301
drhb1fdb2a2008-01-05 04:06:03 +00003302/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003303**
drh9cbf3422008-01-17 16:22:13 +00003304** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003305** P3==1 is the schema version. P3==2 is the database format.
3306** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003307** the main database file and P1==1 is the database file used to store
3308** temporary tables.
drh4a324312001-12-21 14:30:42 +00003309**
drh50e5dad2001-09-15 00:57:28 +00003310** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003311** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003312** executing this instruction.
3313*/
drh27a348c2015-04-13 19:14:06 +00003314case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003315 int iMeta;
drh856c1032009-06-02 15:21:42 +00003316 int iDb;
3317 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003318
drh1713afb2013-06-28 01:24:57 +00003319 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003320 iDb = pOp->p1;
3321 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003322 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003323 assert( iDb>=0 && iDb<db->nDb );
3324 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003325 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003326
danielk1977602b4662009-07-02 07:47:33 +00003327 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003328 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003329 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003330 break;
3331}
3332
drh98757152008-01-09 23:04:12 +00003333/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003334**
drh1861afc2016-02-01 21:48:34 +00003335** Write the integer value P3 into cookie number P2 of database P1.
3336** P2==1 is the schema version. P2==2 is the database format.
3337** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003338** size, and so forth. P1==0 is the main database file and P1==1 is the
3339** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003340**
3341** A transaction must be started before executing this opcode.
3342*/
drh1861afc2016-02-01 21:48:34 +00003343case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003344 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003345
3346 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003347 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003348 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003349 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003350 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003351 pDb = &db->aDb[pOp->p1];
3352 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003353 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003354 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003355 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003356 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003357 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003358 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003359 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003360 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003361 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003362 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003363 }
drhfd426c62006-01-30 15:34:22 +00003364 if( pOp->p1==1 ){
3365 /* Invalidate all prepared statements whenever the TEMP database
3366 ** schema is changed. Ticket #1644 */
3367 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003368 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003369 }
drh9467abf2016-02-17 18:44:11 +00003370 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003371 break;
3372}
3373
drh98757152008-01-09 23:04:12 +00003374/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003375** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003376**
drhecdc7532001-09-23 02:35:53 +00003377** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003378** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003379** P3==0 means the main database, P3==1 means the database used for
3380** temporary tables, and P3>1 means used the corresponding attached
3381** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003382** values need not be contiguous but all P1 values should be small integers.
3383** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003384**
drh8e9deb62018-06-05 13:43:02 +00003385** Allowed P5 bits:
3386** <ul>
3387** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3388** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3389** of OP_SeekLE/OP_IdxGT)
3390** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003391**
danielk1977d336e222009-02-20 10:58:41 +00003392** The P4 value may be either an integer (P4_INT32) or a pointer to
3393** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003394** object, then table being opened must be an [index b-tree] where the
3395** KeyInfo object defines the content and collating
3396** sequence of that index b-tree. Otherwise, if P4 is an integer
3397** value, then the table being opened must be a [table b-tree] with a
3398** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003399**
drh35263192014-07-22 20:02:19 +00003400** See also: OpenWrite, ReopenIdx
3401*/
3402/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3403** Synopsis: root=P2 iDb=P3
3404**
drh8e9deb62018-06-05 13:43:02 +00003405** The ReopenIdx opcode works like OP_OpenRead except that it first
3406** checks to see if the cursor on P1 is already open on the same
3407** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003408** if the cursor is already open, do not reopen it.
3409**
drh8e9deb62018-06-05 13:43:02 +00003410** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3411** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3412** be the same as every other ReopenIdx or OpenRead for the same cursor
3413** number.
drh35263192014-07-22 20:02:19 +00003414**
drh8e9deb62018-06-05 13:43:02 +00003415** Allowed P5 bits:
3416** <ul>
3417** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3418** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3419** of OP_SeekLE/OP_IdxGT)
3420** </ul>
3421**
3422** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003423*/
drh98757152008-01-09 23:04:12 +00003424/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003425** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003426**
3427** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00003428** page is P2 (or whose root page is held in register P2 if the
3429** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00003430**
danielk1977d336e222009-02-20 10:58:41 +00003431** The P4 value may be either an integer (P4_INT32) or a pointer to
3432** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003433** object, then table being opened must be an [index b-tree] where the
3434** KeyInfo object defines the content and collating
3435** sequence of that index b-tree. Otherwise, if P4 is an integer
3436** value, then the table being opened must be a [table b-tree] with a
3437** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00003438**
drh8e9deb62018-06-05 13:43:02 +00003439** Allowed P5 bits:
3440** <ul>
3441** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3442** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
3443** of OP_SeekLE/OP_IdxGT)
3444** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
3445** and subsequently delete entries in an index btree. This is a
3446** hint to the storage engine that the storage engine is allowed to
3447** ignore. The hint is not used by the official SQLite b*tree storage
3448** engine, but is used by COMDB2.
3449** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
3450** as the root page, not the value of P2 itself.
3451** </ul>
drhf57b3392001-10-08 13:22:32 +00003452**
drh8e9deb62018-06-05 13:43:02 +00003453** This instruction works like OpenRead except that it opens the cursor
3454** in read/write mode.
3455**
3456** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00003457*/
drh35263192014-07-22 20:02:19 +00003458case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003459 int nField;
3460 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003461 int p2;
3462 int iDb;
drhf57b3392001-10-08 13:22:32 +00003463 int wrFlag;
3464 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003465 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003466 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003467
drhe0997b32015-03-20 14:57:50 +00003468 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003469 assert( pOp->p4type==P4_KEYINFO );
3470 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003471 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003472 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003473 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003474 }
3475 /* If the cursor is not currently open or is open on a different
3476 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003477case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003478case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003479
drhe0997b32015-03-20 14:57:50 +00003480 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003481 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003482 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3483 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003484
danfa401de2009-10-16 14:55:03 +00003485 if( p->expired ){
drh47b7fc72014-11-11 01:33:57 +00003486 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003487 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003488 }
3489
drh856c1032009-06-02 15:21:42 +00003490 nField = 0;
3491 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003492 p2 = pOp->p2;
3493 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003494 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003495 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003496 pDb = &db->aDb[iDb];
3497 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003498 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003499 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003500 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3501 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003502 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003503 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3504 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003505 }
3506 }else{
3507 wrFlag = 0;
3508 }
dan428c2182012-08-06 18:50:11 +00003509 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003510 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003511 assert( p2<=(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00003512 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00003513 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003514 assert( memIsValid(pIn2) );
3515 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003516 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003517 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003518 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003519 ** that opcode will always set the p2 value to 2 or more or else fail.
3520 ** If there were a failure, the prepared statement would have halted
3521 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003522 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003523 }
danielk1977d336e222009-02-20 10:58:41 +00003524 if( pOp->p4type==P4_KEYINFO ){
3525 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003526 assert( pKeyInfo->enc==ENC(db) );
3527 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003528 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003529 }else if( pOp->p4type==P4_INT32 ){
3530 nField = pOp->p4.i;
3531 }
drh653b82a2009-06-22 11:10:47 +00003532 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003533 assert( nField>=0 );
3534 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003535 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003536 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003537 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003538 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003539 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003540#ifdef SQLITE_DEBUG
3541 pCur->wrFlag = wrFlag;
3542#endif
drhc960dcb2015-11-20 19:22:01 +00003543 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003544 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003545 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003546 ** SQLite used to check if the root-page flags were sane at this point
3547 ** and report database corruption if they were not, but this check has
3548 ** since moved into the btree layer. */
3549 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003550
3551open_cursor_set_hints:
3552 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3553 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003554 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003555#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003556 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3557#endif
drhc960dcb2015-11-20 19:22:01 +00003558 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003559 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003560 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003561 break;
3562}
3563
drhe08e8d62017-05-01 15:15:41 +00003564/* Opcode: OpenDup P1 P2 * * *
3565**
3566** Open a new cursor P1 that points to the same ephemeral table as
3567** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3568** opcode. Only ephemeral cursors may be duplicated.
3569**
3570** Duplicate ephemeral cursors are used for self-joins of materialized views.
3571*/
3572case OP_OpenDup: {
3573 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3574 VdbeCursor *pCx; /* The new cursor */
3575
3576 pOrig = p->apCsr[pOp->p2];
3577 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3578
3579 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3580 if( pCx==0 ) goto no_mem;
3581 pCx->nullRow = 1;
3582 pCx->isEphemeral = 1;
3583 pCx->pKeyInfo = pOrig->pKeyInfo;
3584 pCx->isTable = pOrig->isTable;
3585 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
3586 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003587 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3588 ** opened for a database. Since there is already an open cursor when this
3589 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3590 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003591 break;
3592}
3593
3594
drh2a5d9902011-08-26 00:34:45 +00003595/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003596** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003597**
drhb9bb7c12006-06-11 23:41:55 +00003598** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003599** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003600** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003601** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003602**
drh25d3adb2010-04-05 15:11:08 +00003603** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003604** The cursor points to a BTree table if P4==0 and to a BTree index
3605** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003606** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003607**
drh2a5d9902011-08-26 00:34:45 +00003608** The P5 parameter can be a mask of the BTREE_* flags defined
3609** in btree.h. These flags control aspects of the operation of
3610** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3611** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003612*/
drha21a64d2010-04-06 22:33:55 +00003613/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003614** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003615**
3616** This opcode works the same as OP_OpenEphemeral. It has a
3617** different name to distinguish its use. Tables created using
3618** by this opcode will be used for automatically created transient
3619** indices in joins.
3620*/
3621case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003622case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003623 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003624 KeyInfo *pKeyInfo;
3625
drhd4187c72010-08-30 22:15:45 +00003626 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003627 SQLITE_OPEN_READWRITE |
3628 SQLITE_OPEN_CREATE |
3629 SQLITE_OPEN_EXCLUSIVE |
3630 SQLITE_OPEN_DELETEONCLOSE |
3631 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003632 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003633 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003634 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003635 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003636 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003637 pCx->isEphemeral = 1;
drhfbd8cbd2016-12-10 12:58:15 +00003638 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
drhd4187c72010-08-30 22:15:45 +00003639 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003640 if( rc==SQLITE_OK ){
drhbb2d9b12018-06-06 16:28:40 +00003641 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00003642 }
3643 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003644 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003645 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003646 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003647 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003648 */
drhfbd8cbd2016-12-10 12:58:15 +00003649 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003650 int pgno;
drh66a51672008-01-03 00:01:23 +00003651 assert( pOp->p4type==P4_KEYINFO );
drhfbd8cbd2016-12-10 12:58:15 +00003652 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003653 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003654 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003655 assert( pKeyInfo->db==db );
3656 assert( pKeyInfo->enc==ENC(db) );
drhfbd8cbd2016-12-10 12:58:15 +00003657 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003658 pKeyInfo, pCx->uc.pCursor);
drhc6b52df2002-01-04 03:09:29 +00003659 }
drhf0863fe2005-06-12 21:35:51 +00003660 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003661 }else{
drhfbd8cbd2016-12-10 12:58:15 +00003662 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003663 0, pCx->uc.pCursor);
drhf0863fe2005-06-12 21:35:51 +00003664 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003665 }
drh5e00f6c2001-09-13 13:46:56 +00003666 }
drh9467abf2016-02-17 18:44:11 +00003667 if( rc ) goto abort_due_to_error;
drhd4187c72010-08-30 22:15:45 +00003668 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003669 break;
3670}
3671
danfad9f9a2014-04-01 18:41:51 +00003672/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003673**
3674** This opcode works like OP_OpenEphemeral except that it opens
3675** a transient index that is specifically designed to sort large
3676** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003677**
3678** If argument P3 is non-zero, then it indicates that the sorter may
3679** assume that a stable sort considering the first P3 fields of each
3680** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003681*/
drhca892a72011-09-03 00:17:51 +00003682case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003683 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003684
drh399af1d2013-11-20 17:25:55 +00003685 assert( pOp->p1>=0 );
3686 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003687 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003688 if( pCx==0 ) goto no_mem;
3689 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003690 assert( pCx->pKeyInfo->db==db );
3691 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003692 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003693 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003694 break;
3695}
3696
dan78d58432014-03-25 15:04:07 +00003697/* Opcode: SequenceTest P1 P2 * * *
3698** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3699**
3700** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3701** to P2. Regardless of whether or not the jump is taken, increment the
3702** the sequence value.
3703*/
3704case OP_SequenceTest: {
3705 VdbeCursor *pC;
3706 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3707 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003708 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003709 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003710 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003711 }
drh5e00f6c2001-09-13 13:46:56 +00003712 break;
3713}
3714
drh5f612292014-02-08 23:20:32 +00003715/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003716** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003717**
3718** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003719** row of data. The content of that one row is the content of memory
3720** register P2. In other words, cursor P1 becomes an alias for the
3721** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003722**
drh2d8d7ce2010-02-15 15:17:05 +00003723** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003724** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003725** individual columns using the OP_Column opcode. The OP_Column opcode
3726** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003727**
3728** P3 is the number of fields in the records that will be stored by
3729** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003730*/
drh9cbf3422008-01-17 16:22:13 +00003731case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003732 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003733
drh653b82a2009-06-22 11:10:47 +00003734 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003735 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003736 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003737 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003738 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003739 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003740 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003741 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3742 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3743 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3744 ** which is a performance optimization */
3745 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003746 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003747 break;
3748}
3749
drh98757152008-01-09 23:04:12 +00003750/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003751**
3752** Close a cursor previously opened as P1. If P1 is not
3753** currently open, this instruction is a no-op.
3754*/
drh9cbf3422008-01-17 16:22:13 +00003755case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003756 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3757 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3758 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003759 break;
3760}
3761
drh97bae792015-06-05 15:59:57 +00003762#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3763/* Opcode: ColumnsUsed P1 * * P4 *
3764**
3765** This opcode (which only exists if SQLite was compiled with
3766** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3767** table or index for cursor P1 are used. P4 is a 64-bit integer
3768** (P4_INT64) in which the first 63 bits are one for each of the
3769** first 63 columns of the table or index that are actually used
3770** by the cursor. The high-order bit is set if any column after
3771** the 64th is used.
3772*/
3773case OP_ColumnsUsed: {
3774 VdbeCursor *pC;
3775 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003776 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003777 pC->maskUsed = *(u64*)pOp->p4.pI64;
3778 break;
3779}
3780#endif
3781
drh8af3f772014-07-25 18:01:06 +00003782/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003783** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003784**
danielk1977b790c6c2008-04-18 10:25:24 +00003785** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003786** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003787** to an SQL index, then P3 is the first in an array of P4 registers
3788** that are used as an unpacked index key.
3789**
3790** Reposition cursor P1 so that it points to the smallest entry that
3791** is greater than or equal to the key value. If there are no records
3792** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003793**
drhb1d607d2015-11-05 22:30:54 +00003794** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3795** opcode will always land on a record that equally equals the key, or
3796** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3797** opcode must be followed by an IdxLE opcode with the same arguments.
3798** The IdxLE opcode will be skipped if this opcode succeeds, but the
3799** IdxLE opcode will be used on subsequent loop iterations.
3800**
drh8af3f772014-07-25 18:01:06 +00003801** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003802** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003803** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003804**
drh935850e2014-05-24 17:15:15 +00003805** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003806*/
drh8af3f772014-07-25 18:01:06 +00003807/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003808** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003809**
danielk1977b790c6c2008-04-18 10:25:24 +00003810** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003811** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003812** to an SQL index, then P3 is the first in an array of P4 registers
3813** that are used as an unpacked index key.
3814**
3815** Reposition cursor P1 so that it points to the smallest entry that
3816** is greater than the key value. If there are no records greater than
3817** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003818**
drh8af3f772014-07-25 18:01:06 +00003819** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003820** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003821** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003822**
drh935850e2014-05-24 17:15:15 +00003823** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003824*/
drh8af3f772014-07-25 18:01:06 +00003825/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003826** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003827**
danielk1977b790c6c2008-04-18 10:25:24 +00003828** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003829** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003830** to an SQL index, then P3 is the first in an array of P4 registers
3831** that are used as an unpacked index key.
3832**
3833** Reposition cursor P1 so that it points to the largest entry that
3834** is less than the key value. If there are no records less than
3835** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003836**
drh8af3f772014-07-25 18:01:06 +00003837** This opcode leaves the cursor configured to move in reverse order,
3838** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003839** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003840**
drh935850e2014-05-24 17:15:15 +00003841** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003842*/
drh8af3f772014-07-25 18:01:06 +00003843/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003844** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003845**
danielk1977b790c6c2008-04-18 10:25:24 +00003846** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003847** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003848** to an SQL index, then P3 is the first in an array of P4 registers
3849** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003850**
danielk1977b790c6c2008-04-18 10:25:24 +00003851** Reposition cursor P1 so that it points to the largest entry that
3852** is less than or equal to the key value. If there are no records
3853** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003854**
drh8af3f772014-07-25 18:01:06 +00003855** This opcode leaves the cursor configured to move in reverse order,
3856** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003857** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003858**
drhb1d607d2015-11-05 22:30:54 +00003859** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3860** opcode will always land on a record that equally equals the key, or
3861** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3862** opcode must be followed by an IdxGE opcode with the same arguments.
3863** The IdxGE opcode will be skipped if this opcode succeeds, but the
3864** IdxGE opcode will be used on subsequent loop iterations.
3865**
drh935850e2014-05-24 17:15:15 +00003866** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003867*/
drh4a1d3652014-02-14 15:13:36 +00003868case OP_SeekLT: /* jump, in3 */
3869case OP_SeekLE: /* jump, in3 */
3870case OP_SeekGE: /* jump, in3 */
3871case OP_SeekGT: { /* jump, in3 */
drhb1d607d2015-11-05 22:30:54 +00003872 int res; /* Comparison result */
3873 int oc; /* Opcode */
3874 VdbeCursor *pC; /* The cursor to seek */
3875 UnpackedRecord r; /* The key to seek for */
3876 int nField; /* Number of columns or fields in the key */
3877 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003878 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003879
drh653b82a2009-06-22 11:10:47 +00003880 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003881 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003882 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003883 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003884 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003885 assert( OP_SeekLE == OP_SeekLT+1 );
3886 assert( OP_SeekGE == OP_SeekLT+2 );
3887 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003888 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003889 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003890 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003891 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003892 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003893#ifdef SQLITE_DEBUG
3894 pC->seekOp = pOp->opcode;
3895#endif
drhe0997b32015-03-20 14:57:50 +00003896
drh3da046d2013-11-11 03:24:11 +00003897 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003898 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003899 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3900 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003901
drh3da046d2013-11-11 03:24:11 +00003902 /* The input value in P3 might be of any type: integer, real, string,
3903 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003904 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003905 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003906 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003907 applyNumericAffinity(pIn3, 0);
3908 }
drh3da046d2013-11-11 03:24:11 +00003909 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003910
drh3da046d2013-11-11 03:24:11 +00003911 /* If the P3 value could not be converted into an integer without
3912 ** loss of information, then special processing is required... */
3913 if( (pIn3->flags & MEM_Int)==0 ){
3914 if( (pIn3->flags & MEM_Real)==0 ){
3915 /* If the P3 value cannot be converted into any kind of a number,
3916 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003917 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003918 break;
3919 }
drh959403f2008-12-12 17:56:16 +00003920
danaa1776f2013-11-26 18:22:59 +00003921 /* If the approximation iKey is larger than the actual real search
3922 ** term, substitute >= for > and < for <=. e.g. if the search term
3923 ** is 4.9 and the integer approximation 5:
3924 **
3925 ** (x > 4.9) -> (x >= 5)
3926 ** (x <= 4.9) -> (x < 5)
3927 */
drh74eaba42014-09-18 17:52:15 +00003928 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003929 assert( OP_SeekGE==(OP_SeekGT-1) );
3930 assert( OP_SeekLT==(OP_SeekLE-1) );
3931 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3932 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003933 }
3934
3935 /* If the approximation iKey is smaller than the actual real search
3936 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003937 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003938 assert( OP_SeekLE==(OP_SeekLT+1) );
3939 assert( OP_SeekGT==(OP_SeekGE+1) );
3940 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3941 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003942 }
drh3da046d2013-11-11 03:24:11 +00003943 }
drhc960dcb2015-11-20 19:22:01 +00003944 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003945 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003946 if( rc!=SQLITE_OK ){
3947 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003948 }
drhaa736092009-06-22 00:55:30 +00003949 }else{
drhd6b79462015-11-07 01:19:00 +00003950 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3951 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3952 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3953 */
drhc960dcb2015-11-20 19:22:01 +00003954 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00003955 eqOnly = 1;
3956 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3957 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3958 assert( pOp[1].p1==pOp[0].p1 );
3959 assert( pOp[1].p2==pOp[0].p2 );
3960 assert( pOp[1].p3==pOp[0].p3 );
3961 assert( pOp[1].p4.i==pOp[0].p4.i );
3962 }
3963
drh3da046d2013-11-11 03:24:11 +00003964 nField = pOp->p4.i;
3965 assert( pOp->p4type==P4_INT32 );
3966 assert( nField>0 );
3967 r.pKeyInfo = pC->pKeyInfo;
3968 r.nField = (u16)nField;
3969
3970 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003971 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003972 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003973 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003974 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003975 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003976 */
dan1fed5da2014-02-25 21:01:25 +00003977 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3978 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3979 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3980 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3981 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003982
3983 r.aMem = &aMem[pOp->p3];
3984#ifdef SQLITE_DEBUG
3985 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3986#endif
drh70528d72015-11-05 20:25:09 +00003987 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00003988 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00003989 if( rc!=SQLITE_OK ){
3990 goto abort_due_to_error;
3991 }
drhb1d607d2015-11-05 22:30:54 +00003992 if( eqOnly && r.eqSeen==0 ){
3993 assert( res!=0 );
3994 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00003995 }
drh3da046d2013-11-11 03:24:11 +00003996 }
3997 pC->deferredMoveto = 0;
3998 pC->cacheStatus = CACHE_STALE;
3999#ifdef SQLITE_TEST
4000 sqlite3_search_count++;
4001#endif
drh4a1d3652014-02-14 15:13:36 +00004002 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4003 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004004 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004005 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4006 if( rc!=SQLITE_OK ){
4007 if( rc==SQLITE_DONE ){
4008 rc = SQLITE_OK;
4009 res = 1;
4010 }else{
4011 goto abort_due_to_error;
4012 }
4013 }
drh3da046d2013-11-11 03:24:11 +00004014 }else{
4015 res = 0;
4016 }
4017 }else{
drh4a1d3652014-02-14 15:13:36 +00004018 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4019 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004020 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004021 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4022 if( rc!=SQLITE_OK ){
4023 if( rc==SQLITE_DONE ){
4024 rc = SQLITE_OK;
4025 res = 1;
4026 }else{
4027 goto abort_due_to_error;
4028 }
4029 }
drh3da046d2013-11-11 03:24:11 +00004030 }else{
4031 /* res might be negative because the table is empty. Check to
4032 ** see if this is the case.
4033 */
drhc960dcb2015-11-20 19:22:01 +00004034 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004035 }
4036 }
drhb1d607d2015-11-05 22:30:54 +00004037seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004038 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004039 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004040 if( res ){
drhf56fa462015-04-13 21:39:54 +00004041 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004042 }else if( eqOnly ){
4043 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4044 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004045 }
drh5e00f6c2001-09-13 13:46:56 +00004046 break;
4047}
dan71c57db2016-07-09 20:23:55 +00004048
drh8c2b6d72018-06-05 20:45:20 +00004049/* Opcode: SeekHit P1 P2 * * *
4050** Synopsis: seekHit=P2
4051**
4052** Set the seekHit flag on cursor P1 to the value in P2.
4053** The seekHit flag is used by the IfNoHope opcode.
4054**
4055** P1 must be a valid b-tree cursor. P2 must be a boolean value,
4056** either 0 or 1.
4057*/
4058case OP_SeekHit: {
4059 VdbeCursor *pC;
4060 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4061 pC = p->apCsr[pOp->p1];
4062 assert( pC!=0 );
4063 assert( pOp->p2==0 || pOp->p2==1 );
4064 pC->seekHit = pOp->p2 & 1;
4065 break;
4066}
4067
drh8cff69d2009-11-12 19:59:44 +00004068/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004069** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004070**
drh8cff69d2009-11-12 19:59:44 +00004071** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4072** P4>0 then register P3 is the first of P4 registers that form an unpacked
4073** record.
4074**
4075** Cursor P1 is on an index btree. If the record identified by P3 and P4
4076** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004077** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004078**
drhcefc87f2014-08-01 01:40:33 +00004079** This operation leaves the cursor in a state where it can be
4080** advanced in the forward direction. The Next instruction will work,
4081** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004082**
drh6f225d02013-10-26 13:36:51 +00004083** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004084*/
drh8cff69d2009-11-12 19:59:44 +00004085/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004086** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004087**
drh8cff69d2009-11-12 19:59:44 +00004088** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4089** P4>0 then register P3 is the first of P4 registers that form an unpacked
4090** record.
4091**
4092** Cursor P1 is on an index btree. If the record identified by P3 and P4
4093** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4094** does contain an entry whose prefix matches the P3/P4 record then control
4095** falls through to the next instruction and P1 is left pointing at the
4096** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004097**
drh8af3f772014-07-25 18:01:06 +00004098** This operation leaves the cursor in a state where it cannot be
4099** advanced in either direction. In other words, the Next and Prev
4100** opcodes do not work after this operation.
4101**
drh8c2b6d72018-06-05 20:45:20 +00004102** See also: Found, NotExists, NoConflict, IfNoHope
4103*/
4104/* Opcode: IfNoHope P1 P2 P3 P4 *
4105** Synopsis: key=r[P3@P4]
4106**
4107** Register P3 is the first of P4 registers that form an unpacked
4108** record.
4109**
4110** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
4111** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
4112** check to see if there is any entry in P1 that matches the
4113** prefix identified by P3 and P4. If no entry matches the prefix,
4114** jump to P2. Otherwise fall through.
4115**
4116** This opcode behaves like OP_NotFound if the seekHit
4117** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
4118**
4119** This opcode is used in IN clause processing for a multi-column key.
4120** If an IN clause is attached to an element of the key other than the
4121** left-most element, and if there are no matches on the most recent
4122** seek over the whole key, then it might be that one of the key element
4123** to the left is prohibiting a match, and hence there is "no hope" of
4124** any match regardless of how many IN clause elements are checked.
4125** In such a case, we abandon the IN clause search early, using this
4126** opcode. The opcode name comes from the fact that the
4127** jump is taken if there is "no hope" of achieving a match.
4128**
4129** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004130*/
drh6f225d02013-10-26 13:36:51 +00004131/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004132** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004133**
4134** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4135** P4>0 then register P3 is the first of P4 registers that form an unpacked
4136** record.
4137**
4138** Cursor P1 is on an index btree. If the record identified by P3 and P4
4139** contains any NULL value, jump immediately to P2. If all terms of the
4140** record are not-NULL then a check is done to determine if any row in the
4141** P1 index btree has a matching key prefix. If there are no matches, jump
4142** immediately to P2. If there is a match, fall through and leave the P1
4143** cursor pointing to the matching row.
4144**
4145** This opcode is similar to OP_NotFound with the exceptions that the
4146** branch is always taken if any part of the search key input is NULL.
4147**
drh8af3f772014-07-25 18:01:06 +00004148** This operation leaves the cursor in a state where it cannot be
4149** advanced in either direction. In other words, the Next and Prev
4150** opcodes do not work after this operation.
4151**
drh6f225d02013-10-26 13:36:51 +00004152** See also: NotFound, Found, NotExists
4153*/
drh8c2b6d72018-06-05 20:45:20 +00004154case OP_IfNoHope: { /* jump, in3 */
4155 VdbeCursor *pC;
4156 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4157 pC = p->apCsr[pOp->p1];
4158 assert( pC!=0 );
4159 if( pC->seekHit ) break;
4160 /* Fall through into OP_NotFound */
4161}
drh6f225d02013-10-26 13:36:51 +00004162case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004163case OP_NotFound: /* jump, in3 */
4164case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004165 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004166 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004167 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004168 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004169 int res;
drha582b012016-12-21 19:45:54 +00004170 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004171 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004172 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004173
dan0ff297e2009-09-25 17:03:14 +00004174#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004175 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004176#endif
4177
drhaa736092009-06-22 00:55:30 +00004178 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004179 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004180 pC = p->apCsr[pOp->p1];
4181 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004182#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004183 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004184#endif
drh3c657212009-11-17 23:59:58 +00004185 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004186 assert( pC->eCurType==CURTYPE_BTREE );
4187 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004188 assert( pC->isTable==0 );
4189 if( pOp->p4.i>0 ){
4190 r.pKeyInfo = pC->pKeyInfo;
4191 r.nField = (u16)pOp->p4.i;
4192 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004193#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004194 for(ii=0; ii<r.nField; ii++){
4195 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004196 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004197 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004198 }
drh8aaf7bc2016-09-20 01:19:18 +00004199#endif
drh3da046d2013-11-11 03:24:11 +00004200 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004201 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004202 }else{
drhe46515b2017-05-19 22:51:00 +00004203 assert( pIn3->flags & MEM_Blob );
4204 rc = ExpandBlob(pIn3);
4205 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4206 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004207 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004208 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004209 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004210 }
dan1fed5da2014-02-25 21:01:25 +00004211 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004212 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004213 if( pOp->opcode==OP_NoConflict ){
4214 /* For the OP_NoConflict opcode, take the jump if any of the
4215 ** input fields are NULL, since any key with a NULL will not
4216 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004217 for(ii=0; ii<pIdxKey->nField; ii++){
4218 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004219 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004220 break;
drh6f225d02013-10-26 13:36:51 +00004221 }
4222 }
drh5e00f6c2001-09-13 13:46:56 +00004223 }
drhc960dcb2015-11-20 19:22:01 +00004224 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004225 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004226 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004227 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004228 }
4229 pC->seekResult = res;
4230 alreadyExists = (res==0);
4231 pC->nullRow = 1-alreadyExists;
4232 pC->deferredMoveto = 0;
4233 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004234 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004235 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004236 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004237 }else{
drhf56fa462015-04-13 21:39:54 +00004238 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4239 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004240 }
drh5e00f6c2001-09-13 13:46:56 +00004241 break;
4242}
4243
drheeb95652016-05-26 20:56:38 +00004244/* Opcode: SeekRowid P1 P2 P3 * *
4245** Synopsis: intkey=r[P3]
4246**
4247** P1 is the index of a cursor open on an SQL table btree (with integer
4248** keys). If register P3 does not contain an integer or if P1 does not
4249** contain a record with rowid P3 then jump immediately to P2.
4250** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4251** a record with rowid P3 then
4252** leave the cursor pointing at that record and fall through to the next
4253** instruction.
4254**
4255** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4256** the P3 register must be guaranteed to contain an integer value. With this
4257** opcode, register P3 might not contain an integer.
4258**
4259** The OP_NotFound opcode performs the same operation on index btrees
4260** (with arbitrary multi-value keys).
4261**
4262** This opcode leaves the cursor in a state where it cannot be advanced
4263** in either direction. In other words, the Next and Prev opcodes will
4264** not work following this opcode.
4265**
4266** See also: Found, NotFound, NoConflict, SeekRowid
4267*/
drh9cbf3422008-01-17 16:22:13 +00004268/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004269** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004270**
drh261c02d2013-10-25 14:46:15 +00004271** P1 is the index of a cursor open on an SQL table btree (with integer
4272** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004273** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4274** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4275** leave the cursor pointing at that record and fall through to the next
4276** instruction.
drh6b125452002-01-28 15:53:03 +00004277**
drheeb95652016-05-26 20:56:38 +00004278** The OP_SeekRowid opcode performs the same operation but also allows the
4279** P3 register to contain a non-integer value, in which case the jump is
4280** always taken. This opcode requires that P3 always contain an integer.
4281**
drh261c02d2013-10-25 14:46:15 +00004282** The OP_NotFound opcode performs the same operation on index btrees
4283** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004284**
drh8af3f772014-07-25 18:01:06 +00004285** This opcode leaves the cursor in a state where it cannot be advanced
4286** in either direction. In other words, the Next and Prev opcodes will
4287** not work following this opcode.
4288**
drheeb95652016-05-26 20:56:38 +00004289** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004290*/
drheeb95652016-05-26 20:56:38 +00004291case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004292 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004293 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004294 int res;
4295 u64 iKey;
4296
drh3c657212009-11-17 23:59:58 +00004297 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004298 if( (pIn3->flags & MEM_Int)==0 ){
4299 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
4300 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
4301 }
4302 /* Fall through into OP_NotExists */
4303case OP_NotExists: /* jump, in3 */
4304 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00004305 assert( pIn3->flags & MEM_Int );
4306 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4307 pC = p->apCsr[pOp->p1];
4308 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004309#ifdef SQLITE_DEBUG
drhcf025a82018-06-07 18:01:21 +00004310 pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00004311#endif
drhaa736092009-06-22 00:55:30 +00004312 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004313 assert( pC->eCurType==CURTYPE_BTREE );
4314 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004315 assert( pCrsr!=0 );
4316 res = 0;
4317 iKey = pIn3->u.i;
4318 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004319 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004320 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004321 pC->nullRow = 0;
4322 pC->cacheStatus = CACHE_STALE;
4323 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004324 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004325 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004326 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004327 assert( rc==SQLITE_OK );
4328 if( pOp->p2==0 ){
4329 rc = SQLITE_CORRUPT_BKPT;
4330 }else{
4331 goto jump_to_p2;
4332 }
danc6157e12015-09-14 09:23:47 +00004333 }
drh9467abf2016-02-17 18:44:11 +00004334 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004335 break;
4336}
4337
drh4c583122008-01-04 22:01:03 +00004338/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004339** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004340**
drh4c583122008-01-04 22:01:03 +00004341** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004342** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004343** The sequence number on the cursor is incremented after this
4344** instruction.
drh4db38a72005-09-01 12:16:28 +00004345*/
drh27a348c2015-04-13 19:14:06 +00004346case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004347 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4348 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004349 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004350 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004351 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004352 break;
4353}
4354
4355
drh98757152008-01-09 23:04:12 +00004356/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004357** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004358**
drhf0863fe2005-06-12 21:35:51 +00004359** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004360** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004361** table that cursor P1 points to. The new record number is written
4362** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004363**
dan76d462e2009-08-30 11:42:51 +00004364** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4365** the largest previously generated record number. No new record numbers are
4366** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004367** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004368** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004369** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004370*/
drh27a348c2015-04-13 19:14:06 +00004371case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004372 i64 v; /* The new rowid */
4373 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4374 int res; /* Result of an sqlite3BtreeLast() */
4375 int cnt; /* Counter to limit the number of searches */
4376 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004377 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004378
drh856c1032009-06-02 15:21:42 +00004379 v = 0;
4380 res = 0;
drh27a348c2015-04-13 19:14:06 +00004381 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004382 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4383 pC = p->apCsr[pOp->p1];
4384 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00004385 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004386 assert( pC->eCurType==CURTYPE_BTREE );
4387 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004388 {
drh5cf8e8c2002-02-19 22:42:05 +00004389 /* The next rowid or record number (different terms for the same
4390 ** thing) is obtained in a two-step algorithm.
4391 **
4392 ** First we attempt to find the largest existing rowid and add one
4393 ** to that. But if the largest existing rowid is already the maximum
4394 ** positive integer, we have to fall through to the second
4395 ** probabilistic algorithm
4396 **
4397 ** The second algorithm is to select a rowid at random and see if
4398 ** it already exists in the table. If it does not exist, we have
4399 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004400 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004401 */
drhaa736092009-06-22 00:55:30 +00004402 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004403
drh75f86a42005-02-17 00:03:06 +00004404#ifdef SQLITE_32BIT_ROWID
4405# define MAX_ROWID 0x7fffffff
4406#else
drhfe2093d2005-01-20 22:48:47 +00004407 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4408 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4409 ** to provide the constant while making all compilers happy.
4410 */
danielk197764202cf2008-11-17 15:31:47 +00004411# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004412#endif
drhfe2093d2005-01-20 22:48:47 +00004413
drh5cf8e8c2002-02-19 22:42:05 +00004414 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004415 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004416 if( rc!=SQLITE_OK ){
4417 goto abort_due_to_error;
4418 }
4419 if( res ){
4420 v = 1; /* IMP: R-61914-48074 */
4421 }else{
drhc960dcb2015-11-20 19:22:01 +00004422 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004423 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004424 if( v>=MAX_ROWID ){
4425 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004426 }else{
drhe0670b62014-02-12 21:31:12 +00004427 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004428 }
drh3fc190c2001-09-14 03:24:23 +00004429 }
drhe0670b62014-02-12 21:31:12 +00004430 }
drh205f48e2004-11-05 00:43:11 +00004431
4432#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004433 if( pOp->p3 ){
4434 /* Assert that P3 is a valid memory cell. */
4435 assert( pOp->p3>0 );
4436 if( p->pFrame ){
4437 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004438 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004439 assert( pOp->p3<=pFrame->nMem );
4440 pMem = &pFrame->aMem[pOp->p3];
4441 }else{
4442 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004443 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004444 pMem = &aMem[pOp->p3];
4445 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004446 }
drhe0670b62014-02-12 21:31:12 +00004447 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004448
drhe0670b62014-02-12 21:31:12 +00004449 REGISTER_TRACE(pOp->p3, pMem);
4450 sqlite3VdbeMemIntegerify(pMem);
4451 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4452 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004453 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004454 goto abort_due_to_error;
4455 }
4456 if( v<pMem->u.i+1 ){
4457 v = pMem->u.i + 1;
4458 }
4459 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004460 }
drhe0670b62014-02-12 21:31:12 +00004461#endif
drh5cf8e8c2002-02-19 22:42:05 +00004462 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004463 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004464 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004465 ** engine starts picking positive candidate ROWIDs at random until
4466 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004467 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4468 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004469 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004470 do{
4471 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004472 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004473 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004474 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004475 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004476 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004477 if( rc ) goto abort_due_to_error;
4478 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004479 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004480 goto abort_due_to_error;
4481 }
drh748a52c2010-09-01 11:50:08 +00004482 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004483 }
drha11846b2004-01-07 18:52:56 +00004484 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004485 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004486 }
drh4c583122008-01-04 22:01:03 +00004487 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004488 break;
4489}
4490
danielk19771f4aa332008-01-03 09:51:55 +00004491/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004492** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004493**
jplyon5a564222003-06-02 06:15:58 +00004494** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004495** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004496** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004497** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004498** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004499**
danielk19771f4aa332008-01-03 09:51:55 +00004500** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4501** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004502** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004503** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004504**
drheaf6ae22016-11-09 20:14:34 +00004505** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4506** run faster by avoiding an unnecessary seek on cursor P1. However,
4507** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4508** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004509**
4510** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4511** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4512** is part of an INSERT operation. The difference is only important to
4513** the update hook.
4514**
dan319eeb72011-03-19 08:38:50 +00004515** Parameter P4 may point to a Table structure, or may be NULL. If it is
4516** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4517** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004518**
drh93aed5a2008-01-16 17:46:38 +00004519** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4520** allocated, then ownership of P2 is transferred to the pseudo-cursor
4521** and register P2 becomes ephemeral. If the cursor is changed, the
4522** value of register P2 will then change. Make sure this does not
4523** cause any problems.)
4524**
drhf0863fe2005-06-12 21:35:51 +00004525** This instruction only works on tables. The equivalent instruction
4526** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004527*/
drhe05c9292009-10-29 13:48:10 +00004528/* Opcode: InsertInt P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00004529** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004530**
4531** This works exactly like OP_Insert except that the key is the
4532** integer value P3, not the value of the integer stored in register P3.
4533*/
4534case OP_Insert:
4535case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004536 Mem *pData; /* MEM cell holding data for the record to be inserted */
4537 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004538 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004539 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4540 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004541 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00004542 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004543
drha6c2ed92009-11-14 23:22:23 +00004544 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004545 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004546 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004547 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004548 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004549 assert( pC->eCurType==CURTYPE_BTREE );
4550 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004551 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004552 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004553 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00004554 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00004555
drhe05c9292009-10-29 13:48:10 +00004556 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004557 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004558 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004559 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004560 REGISTER_TRACE(pOp->p3, pKey);
drh8eeb4462016-05-21 20:03:42 +00004561 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004562 }else{
4563 assert( pOp->opcode==OP_InsertInt );
drh8eeb4462016-05-21 20:03:42 +00004564 x.nKey = pOp->p3;
drhe05c9292009-10-29 13:48:10 +00004565 }
4566
drh9b1c62d2011-03-30 21:04:43 +00004567 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004568 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004569 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004570 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004571 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00004572 }else{
drh4ec6f3a2018-01-12 19:33:18 +00004573 pTab = 0;
drh74c33022016-03-30 12:56:55 +00004574 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004575 }
4576
drh9b1c62d2011-03-30 21:04:43 +00004577#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004578 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00004579 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00004580 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
4581 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
4582 }
drh4ec6f3a2018-01-12 19:33:18 +00004583 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
4584 /* Prevent post-update hook from running in cases when it should not */
4585 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00004586 }
dan46c47d42011-03-01 18:42:07 +00004587 }
dancb9a3642017-01-30 19:44:53 +00004588 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004589#endif
dan46c47d42011-03-01 18:42:07 +00004590
drha05a7222008-01-19 03:35:58 +00004591 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004592 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004593 assert( pData->flags & (MEM_Blob|MEM_Str) );
4594 x.pData = pData->z;
4595 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004596 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4597 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004598 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004599 }else{
drh8eeb4462016-05-21 20:03:42 +00004600 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004601 }
drh8eeb4462016-05-21 20:03:42 +00004602 x.pKey = 0;
4603 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004604 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004605 );
drha05a7222008-01-19 03:35:58 +00004606 pC->deferredMoveto = 0;
4607 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004608
drha05a7222008-01-19 03:35:58 +00004609 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004610 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00004611 if( pTab ){
4612 assert( db->xUpdateCallback!=0 );
4613 assert( pTab->aCol!=0 );
4614 db->xUpdateCallback(db->pUpdateArg,
4615 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
4616 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004617 }
drh5e00f6c2001-09-13 13:46:56 +00004618 break;
4619}
4620
dan438b8812015-09-15 15:55:15 +00004621/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004622**
drh5edc3122001-09-13 21:53:09 +00004623** Delete the record at which the P1 cursor is currently pointing.
4624**
drhe807bdb2016-01-21 17:06:33 +00004625** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4626** the cursor will be left pointing at either the next or the previous
4627** record in the table. If it is left pointing at the next record, then
4628** the next Next instruction will be a no-op. As a result, in this case
4629** it is ok to delete a record from within a Next loop. If
4630** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4631** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004632**
drhdef19e32016-01-27 16:26:25 +00004633** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4634** delete one of several associated with deleting a table row and all its
4635** associated index entries. Exactly one of those deletes is the "primary"
4636** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4637** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004638**
4639** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4640** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004641**
drh91fd4d42008-01-19 20:11:25 +00004642** P1 must not be pseudo-table. It has to be a real table with
4643** multiple rows.
4644**
drh5e769a52016-09-28 16:05:53 +00004645** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004646** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4647** have been positioned using OP_NotFound prior to invoking this opcode in
4648** this case. Specifically, if one is configured, the pre-update hook is
4649** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4650** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004651**
4652** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4653** of the memory cell that contains the value that the rowid of the row will
4654** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004655*/
drh9cbf3422008-01-17 16:22:13 +00004656case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004657 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004658 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004659 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004660 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004661
dan46c47d42011-03-01 18:42:07 +00004662 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004663 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4664 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004665 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004666 assert( pC->eCurType==CURTYPE_BTREE );
4667 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004668 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00004669 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00004670
drhb53a5a92014-10-12 22:37:22 +00004671#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004672 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4673 /* If p5 is zero, the seek operation that positioned the cursor prior to
4674 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4675 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004676 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh92fe38e2014-10-14 13:41:32 +00004677 assert( pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004678 }
4679#endif
drh91fd4d42008-01-19 20:11:25 +00004680
dan438b8812015-09-15 15:55:15 +00004681 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4682 ** the name of the db to pass as to it. Also set local pTab to a copy
4683 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4684 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4685 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004686 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004687 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004688 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004689 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004690 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004691 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004692 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004693 }
drh74c33022016-03-30 12:56:55 +00004694 }else{
4695 zDb = 0; /* Not needed. Silence a compiler warning. */
4696 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004697 }
dan46c47d42011-03-01 18:42:07 +00004698
drh9b1c62d2011-03-30 21:04:43 +00004699#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004700 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004701 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4702 assert( !(opflags & OPFLAG_ISUPDATE)
4703 || HasRowid(pTab)==0
4704 || (aMem[pOp->p3].flags & MEM_Int)
4705 );
dan46c47d42011-03-01 18:42:07 +00004706 sqlite3VdbePreUpdateHook(p, pC,
4707 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004708 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004709 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004710 );
4711 }
dan46c47d42011-03-01 18:42:07 +00004712 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004713#endif
drhb53a5a92014-10-12 22:37:22 +00004714
drhdef19e32016-01-27 16:26:25 +00004715 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4716 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004717 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004718 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004719
4720#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004721 if( p->pFrame==0 ){
4722 if( pC->isEphemeral==0
4723 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4724 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4725 ){
4726 nExtraDelete++;
4727 }
4728 if( pOp->p2 & OPFLAG_NCHANGE ){
4729 nExtraDelete--;
4730 }
drhb89aeb62016-01-27 15:49:32 +00004731 }
4732#endif
4733
drhc960dcb2015-11-20 19:22:01 +00004734 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004735 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004736 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004737 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004738
drh91fd4d42008-01-19 20:11:25 +00004739 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004740 if( opflags & OPFLAG_NCHANGE ){
4741 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004742 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004743 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004744 pC->movetoTarget);
4745 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004746 }
drh5e00f6c2001-09-13 13:46:56 +00004747 }
dan438b8812015-09-15 15:55:15 +00004748
rdcb0c374f2004-02-20 22:53:38 +00004749 break;
4750}
drhb7f1d9a2009-09-08 02:27:58 +00004751/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004752**
drhb7f1d9a2009-09-08 02:27:58 +00004753** The value of the change counter is copied to the database handle
4754** change counter (returned by subsequent calls to sqlite3_changes()).
4755** Then the VMs internal change counter resets to 0.
4756** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004757*/
drh9cbf3422008-01-17 16:22:13 +00004758case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004759 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004760 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004761 break;
4762}
4763
drh1153c7b2013-11-01 22:02:56 +00004764/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00004765** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004766**
drh1153c7b2013-11-01 22:02:56 +00004767** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004768** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004769** the sorter cursor currently points to. Only the first P4 fields
4770** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004771**
4772** If either P3 or the sorter contains a NULL in one of their significant
4773** fields (not counting the P4 fields at the end which are ignored) then
4774** the comparison is assumed to be equal.
4775**
4776** Fall through to next instruction if the two records compare equal to
4777** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004778*/
4779case OP_SorterCompare: {
4780 VdbeCursor *pC;
4781 int res;
drhac502322014-07-30 13:56:48 +00004782 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004783
4784 pC = p->apCsr[pOp->p1];
4785 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004786 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004787 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004788 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004789 res = 0;
drhac502322014-07-30 13:56:48 +00004790 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004791 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004792 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004793 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004794 break;
4795};
4796
drh6cf4a7d2014-10-13 13:00:58 +00004797/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004798** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004799**
4800** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004801** Then clear the column header cache on cursor P3.
4802**
4803** This opcode is normally use to move a record out of the sorter and into
4804** a register that is the source for a pseudo-table cursor created using
4805** OpenPseudo. That pseudo-table cursor is the one that is identified by
4806** parameter P3. Clearing the P3 column cache as part of this opcode saves
4807** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004808*/
4809case OP_SorterData: {
4810 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004811
dan5134d132011-09-02 10:31:11 +00004812 pOut = &aMem[pOp->p2];
4813 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004814 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004815 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004816 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004817 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004818 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004819 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004820 break;
4821}
4822
drhe7b554d2017-01-09 15:44:25 +00004823/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004824** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004825**
drh9057fc72016-11-25 19:32:32 +00004826** Write into register P2 the complete row content for the row at
4827** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00004828** There is no interpretation of the data.
4829** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004830** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004831**
drh9057fc72016-11-25 19:32:32 +00004832** If cursor P1 is an index, then the content is the key of the row.
4833** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00004834**
drhde4fcfd2008-01-19 23:50:26 +00004835** If the P1 cursor must be pointing to a valid row (not a NULL row)
4836** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00004837**
drh8cdafc32018-05-31 19:00:20 +00004838** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00004839** into the database page. That means that the content of the output
4840** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00004841** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00004842** position in order that they can write to the same table. If P3==0
4843** then a copy of the data is made into memory. P3!=0 is faster, but
4844** P3==0 is safer.
4845**
4846** If P3!=0 then the content of the P2 register is unsuitable for use
4847** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00004848** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00004849** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00004850*/
danielk1977a7a8e142008-02-13 18:25:27 +00004851case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004852 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004853 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004854 u32 n;
drh70ce3f02003-04-15 19:22:22 +00004855
drhe7b554d2017-01-09 15:44:25 +00004856 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00004857
drh653b82a2009-06-22 11:10:47 +00004858 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4859 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004860 assert( pC!=0 );
4861 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004862 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00004863 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004864 assert( pC->uc.pCursor!=0 );
4865 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004866
drh9057fc72016-11-25 19:32:32 +00004867 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00004868 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4869 ** that might invalidate the cursor.
4870 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00004871 ** would fail. Should this ever change (because of changes in the code
4872 ** generator) then the fix would be to insert a call to
4873 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004874 */
4875 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004876 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4877#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004878 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004879 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4880#endif
drh9a65f2c2009-06-22 19:05:40 +00004881
drha7c90c42016-06-04 20:37:10 +00004882 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00004883 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00004884 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00004885 }
drh722246e2014-10-07 23:02:24 +00004886 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00004887 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00004888 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00004889 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00004890 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004891 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004892 break;
4893}
4894
drh2133d822008-01-03 18:44:59 +00004895/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004896** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004897**
drh2133d822008-01-03 18:44:59 +00004898** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004899** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004900**
4901** P1 can be either an ordinary table or a virtual table. There used to
4902** be a separate OP_VRowid opcode for use with virtual tables, but this
4903** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004904*/
drh27a348c2015-04-13 19:14:06 +00004905case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004906 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004907 i64 v;
drh856c1032009-06-02 15:21:42 +00004908 sqlite3_vtab *pVtab;
4909 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004910
drh27a348c2015-04-13 19:14:06 +00004911 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004912 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4913 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004914 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004915 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004916 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004917 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004918 break;
4919 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004920 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004921#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004922 }else if( pC->eCurType==CURTYPE_VTAB ){
4923 assert( pC->uc.pVCur!=0 );
4924 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004925 pModule = pVtab->pModule;
4926 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004927 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004928 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004929 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004930#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004931 }else{
drhc960dcb2015-11-20 19:22:01 +00004932 assert( pC->eCurType==CURTYPE_BTREE );
4933 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004934 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004935 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004936 if( pC->nullRow ){
4937 pOut->flags = MEM_Null;
4938 break;
4939 }
drha7c90c42016-06-04 20:37:10 +00004940 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00004941 }
drh4c583122008-01-04 22:01:03 +00004942 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004943 break;
4944}
4945
drh9cbf3422008-01-17 16:22:13 +00004946/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004947**
4948** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004949** that occur while the cursor is on the null row will always
4950** write a NULL.
drh17f71932002-02-21 12:01:27 +00004951*/
drh9cbf3422008-01-17 16:22:13 +00004952case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004953 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004954
drh653b82a2009-06-22 11:10:47 +00004955 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4956 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004957 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004958 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004959 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00004960 if( pC->eCurType==CURTYPE_BTREE ){
4961 assert( pC->uc.pCursor!=0 );
4962 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00004963 }
drhcf025a82018-06-07 18:01:21 +00004964#ifdef SQLITE_DEBUG
4965 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
4966#endif
drh17f71932002-02-21 12:01:27 +00004967 break;
4968}
4969
drh86b40df2017-08-01 19:53:43 +00004970/* Opcode: SeekEnd P1 * * * *
4971**
4972** Position cursor P1 at the end of the btree for the purpose of
4973** appending a new entry onto the btree.
4974**
4975** It is assumed that the cursor is used only for appending and so
4976** if the cursor is valid, then the cursor must already be pointing
4977** at the end of the btree and so no changes are made to
4978** the cursor.
4979*/
4980/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004981**
drh8af3f772014-07-25 18:01:06 +00004982** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004983** will refer to the last entry in the database table or index.
4984** If the table or index is empty and P2>0, then jump immediately to P2.
4985** If P2 is 0 or if the table or index is not empty, fall through
4986** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004987**
4988** This opcode leaves the cursor configured to move in reverse order,
4989** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004990** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00004991*/
drh86b40df2017-08-01 19:53:43 +00004992case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00004993case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004994 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004995 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004996 int res;
drh9562b552002-02-19 15:00:07 +00004997
drh653b82a2009-06-22 11:10:47 +00004998 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4999 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005000 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005001 assert( pC->eCurType==CURTYPE_BTREE );
5002 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005003 res = 0;
drh3da046d2013-11-11 03:24:11 +00005004 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005005#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005006 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005007#endif
drh86b40df2017-08-01 19:53:43 +00005008 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005009 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005010 pC->seekResult = -1;
5011 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5012 break;
5013 }
5014 }
5015 rc = sqlite3BtreeLast(pCrsr, &res);
5016 pC->nullRow = (u8)res;
5017 pC->deferredMoveto = 0;
5018 pC->cacheStatus = CACHE_STALE;
5019 if( rc ) goto abort_due_to_error;
5020 if( pOp->p2>0 ){
5021 VdbeBranchTaken(res!=0,2);
5022 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005023 }
5024 break;
5025}
5026
drh5e98e832017-02-17 19:24:06 +00005027/* Opcode: IfSmaller P1 P2 P3 * *
5028**
5029** Estimate the number of rows in the table P1. Jump to P2 if that
5030** estimate is less than approximately 2**(0.1*P3).
5031*/
5032case OP_IfSmaller: { /* jump */
5033 VdbeCursor *pC;
5034 BtCursor *pCrsr;
5035 int res;
5036 i64 sz;
5037
5038 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5039 pC = p->apCsr[pOp->p1];
5040 assert( pC!=0 );
5041 pCrsr = pC->uc.pCursor;
5042 assert( pCrsr );
5043 rc = sqlite3BtreeFirst(pCrsr, &res);
5044 if( rc ) goto abort_due_to_error;
5045 if( res==0 ){
5046 sz = sqlite3BtreeRowCountEst(pCrsr);
5047 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5048 }
5049 VdbeBranchTaken(res!=0,2);
5050 if( res ) goto jump_to_p2;
5051 break;
5052}
5053
drh0342b1f2005-09-01 03:07:44 +00005054
drh6bd4dc62016-12-23 16:05:22 +00005055/* Opcode: SorterSort P1 P2 * * *
5056**
5057** After all records have been inserted into the Sorter object
5058** identified by P1, invoke this opcode to actually do the sorting.
5059** Jump to P2 if there are no records to be sorted.
5060**
5061** This opcode is an alias for OP_Sort and OP_Rewind that is used
5062** for Sorter objects.
5063*/
drh9cbf3422008-01-17 16:22:13 +00005064/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005065**
5066** This opcode does exactly the same thing as OP_Rewind except that
5067** it increments an undocumented global variable used for testing.
5068**
5069** Sorting is accomplished by writing records into a sorting index,
5070** then rewinding that index and playing it back from beginning to
5071** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5072** rewinding so that the global variable will be incremented and
5073** regression tests can determine whether or not the optimizer is
5074** correctly optimizing out sorts.
5075*/
drhc6aff302011-09-01 15:32:47 +00005076case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005077case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005078#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005079 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005080 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005081#endif
drh9b47ee32013-08-20 03:13:51 +00005082 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005083 /* Fall through into OP_Rewind */
5084}
dan9a947222018-06-14 19:06:36 +00005085/* Opcode: Rewind P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00005086**
drhf0863fe2005-06-12 21:35:51 +00005087** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005088** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005089** If the table or index is empty, jump immediately to P2.
5090** If the table or index is not empty, fall through to the following
5091** instruction.
drh8af3f772014-07-25 18:01:06 +00005092**
dan9a947222018-06-14 19:06:36 +00005093** If P5 is non-zero and the table is not empty, then the "skip-next"
5094** flag is set on the cursor so that the next OP_Next instruction
5095** executed on it is a no-op.
5096**
drh8af3f772014-07-25 18:01:06 +00005097** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005098** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005099** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005100*/
drh9cbf3422008-01-17 16:22:13 +00005101case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005102 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005103 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005104 int res;
drh5e00f6c2001-09-13 13:46:56 +00005105
drh653b82a2009-06-22 11:10:47 +00005106 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5107 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005108 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005109 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005110 res = 1;
drh8af3f772014-07-25 18:01:06 +00005111#ifdef SQLITE_DEBUG
5112 pC->seekOp = OP_Rewind;
5113#endif
dan689ab892011-08-12 15:02:00 +00005114 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005115 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005116 }else{
drhc960dcb2015-11-20 19:22:01 +00005117 assert( pC->eCurType==CURTYPE_BTREE );
5118 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005119 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005120 rc = sqlite3BtreeFirst(pCrsr, &res);
dan67a9b8e2018-06-22 20:51:35 +00005121#ifndef SQLITE_OMIT_WINDOWFUNC
danc3a20c12018-05-23 20:55:37 +00005122 if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
dan67a9b8e2018-06-22 20:51:35 +00005123#endif
drha11846b2004-01-07 18:52:56 +00005124 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005125 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005126 }
drh9467abf2016-02-17 18:44:11 +00005127 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005128 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005129 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005130 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005131 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005132 break;
5133}
5134
drh0fd61352014-02-07 02:29:45 +00005135/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005136**
5137** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005138** table or index. If there are no more key/value pairs then fall through
5139** to the following instruction. But if the cursor advance was successful,
5140** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005141**
drh5dad9a32014-07-25 18:37:42 +00005142** The Next opcode is only valid following an SeekGT, SeekGE, or
5143** OP_Rewind opcode used to position the cursor. Next is not allowed
5144** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005145**
drhf93cd942013-11-21 03:12:25 +00005146** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5147** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005148**
drhe39a7322014-02-03 14:04:11 +00005149** The P3 value is a hint to the btree implementation. If P3==1, that
5150** means P1 is an SQL index and that this instruction could have been
5151** omitted if that index had been unique. P3 is usually 0. P3 is
5152** always either 0 or 1.
5153**
dana205a482011-08-27 18:48:57 +00005154** P4 is always of type P4_ADVANCE. The function pointer points to
5155** sqlite3BtreeNext().
5156**
drhafc266a2010-03-31 17:47:44 +00005157** If P5 is positive and the jump is taken, then event counter
5158** number P5-1 in the prepared statement is incremented.
5159**
drhf1949b62018-06-07 17:32:59 +00005160** See also: Prev
drh8721ce42001-11-07 14:22:00 +00005161*/
drh0fd61352014-02-07 02:29:45 +00005162/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005163**
5164** Back up cursor P1 so that it points to the previous key/data pair in its
5165** table or index. If there is no previous key/value pairs then fall through
5166** to the following instruction. But if the cursor backup was successful,
5167** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005168**
drh8af3f772014-07-25 18:01:06 +00005169**
drh5dad9a32014-07-25 18:37:42 +00005170** The Prev opcode is only valid following an SeekLT, SeekLE, or
5171** OP_Last opcode used to position the cursor. Prev is not allowed
5172** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005173**
drhf93cd942013-11-21 03:12:25 +00005174** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5175** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005176**
drhe39a7322014-02-03 14:04:11 +00005177** The P3 value is a hint to the btree implementation. If P3==1, that
5178** means P1 is an SQL index and that this instruction could have been
5179** omitted if that index had been unique. P3 is usually 0. P3 is
5180** always either 0 or 1.
5181**
dana205a482011-08-27 18:48:57 +00005182** P4 is always of type P4_ADVANCE. The function pointer points to
5183** sqlite3BtreePrevious().
5184**
drhafc266a2010-03-31 17:47:44 +00005185** If P5 is positive and the jump is taken, then event counter
5186** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005187*/
drh6bd4dc62016-12-23 16:05:22 +00005188/* Opcode: SorterNext P1 P2 * * P5
5189**
5190** This opcode works just like OP_Next except that P1 must be a
5191** sorter object for which the OP_SorterSort opcode has been
5192** invoked. This opcode advances the cursor to the next sorted
5193** record, or jumps to P2 if there are no more sorted records.
5194*/
drhf93cd942013-11-21 03:12:25 +00005195case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005196 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005197
drhf93cd942013-11-21 03:12:25 +00005198 pC = p->apCsr[pOp->p1];
5199 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005200 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005201 goto next_tail;
drhf93cd942013-11-21 03:12:25 +00005202case OP_Prev: /* jump */
5203case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005204 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005205 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005206 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005207 assert( pC!=0 );
5208 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005209 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005210 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5211 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
drh8af3f772014-07-25 18:01:06 +00005212
drhcf025a82018-06-07 18:01:21 +00005213 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
drh8af3f772014-07-25 18:01:06 +00005214 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
drhf1949b62018-06-07 17:32:59 +00005215 assert( pOp->opcode!=OP_Next
drh8af3f772014-07-25 18:01:06 +00005216 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcf025a82018-06-07 18:01:21 +00005217 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
5218 || pC->seekOp==OP_NullRow);
drhf1949b62018-06-07 17:32:59 +00005219 assert( pOp->opcode!=OP_Prev
drh8af3f772014-07-25 18:01:06 +00005220 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
drhcf025a82018-06-07 18:01:21 +00005221 || pC->seekOp==OP_Last
5222 || pC->seekOp==OP_NullRow);
drh8af3f772014-07-25 18:01:06 +00005223
drh2ab792e2017-05-30 18:34:07 +00005224 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005225next_tail:
drha3460582008-07-11 21:02:53 +00005226 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005227 VdbeBranchTaken(rc==SQLITE_OK,2);
5228 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005229 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005230 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005231#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005232 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005233#endif
drhf56fa462015-04-13 21:39:54 +00005234 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005235 }
drh2ab792e2017-05-30 18:34:07 +00005236 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5237 rc = SQLITE_OK;
5238 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005239 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005240}
5241
drh9b4eaeb2016-11-09 00:10:33 +00005242/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005243** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005244**
drhef8662b2011-06-20 21:47:58 +00005245** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005246** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005247** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005248**
drhfb8c56f2016-11-09 01:19:25 +00005249** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005250** key of reg(P2). In that case, P3 is the index of the first register
5251** for the unpacked key. The availability of the unpacked key can sometimes
5252** be an optimization.
5253**
5254** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5255** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005256**
mistachkin21a919f2014-02-07 03:28:02 +00005257** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5258** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5259** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005260**
drheaf6ae22016-11-09 20:14:34 +00005261** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5262** run faster by avoiding an unnecessary seek on cursor P1. However,
5263** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5264** seeks on the cursor or if the most recent seek used a key equivalent
5265** to P2.
drh0fd61352014-02-07 02:29:45 +00005266**
drhf0863fe2005-06-12 21:35:51 +00005267** This instruction only works for indices. The equivalent instruction
5268** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005269*/
drhf013e202016-10-15 18:37:05 +00005270/* Opcode: SorterInsert P1 P2 * * *
5271** Synopsis: key=r[P2]
5272**
5273** Register P2 holds an SQL index key made using the
5274** MakeRecord instructions. This opcode writes that key
5275** into the sorter P1. Data for the entry is nil.
5276*/
drhca892a72011-09-03 00:17:51 +00005277case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005278case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005279 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005280 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005281
drh653b82a2009-06-22 11:10:47 +00005282 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5283 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00005284 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00005285 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005286 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005287 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005288 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005289 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005290 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005291 assert( pC->isTable==0 );
5292 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005293 if( rc ) goto abort_due_to_error;
5294 if( pOp->opcode==OP_SorterInsert ){
5295 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5296 }else{
drh8eeb4462016-05-21 20:03:42 +00005297 x.nKey = pIn2->n;
5298 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005299 x.aMem = aMem + pOp->p3;
5300 x.nMem = (u16)pOp->p4.i;
5301 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005302 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005303 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5304 );
5305 assert( pC->deferredMoveto==0 );
5306 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005307 }
drh9467abf2016-02-17 18:44:11 +00005308 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005309 break;
5310}
5311
drhd1d38482008-10-07 23:46:38 +00005312/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005313** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005314**
drhe14006d2008-03-25 17:23:32 +00005315** The content of P3 registers starting at register P2 form
5316** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005317** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005318*/
drhe14006d2008-03-25 17:23:32 +00005319case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005320 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005321 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005322 int res;
5323 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005324
drhe14006d2008-03-25 17:23:32 +00005325 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005326 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005327 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5328 pC = p->apCsr[pOp->p1];
5329 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005330 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00005331 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00005332 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005333 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005334 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005335 r.pKeyInfo = pC->pKeyInfo;
5336 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005337 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005338 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005339 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005340 if( rc ) goto abort_due_to_error;
5341 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005342 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005343 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005344 }
drh3da046d2013-11-11 03:24:11 +00005345 assert( pC->deferredMoveto==0 );
5346 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005347 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005348 break;
5349}
5350
drh170ad682017-06-02 15:44:22 +00005351/* Opcode: DeferredSeek P1 * P3 P4 *
5352** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005353**
5354** P1 is an open index cursor and P3 is a cursor on the corresponding
5355** table. This opcode does a deferred seek of the P3 table cursor
5356** to the row that corresponds to the current row of P1.
5357**
5358** This is a deferred seek. Nothing actually happens until
5359** the cursor is used to read a record. That way, if no reads
5360** occur, no unnecessary I/O happens.
5361**
5362** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005363** one entry for each column in the P3 table. If array entry a(i)
5364** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005365** equivalent to performing the deferred seek and then reading column i
5366** from P1. This information is stored in P3 and used to redirect
5367** reads against P3 over to P1, thus possibly avoiding the need to
5368** seek and read cursor P3.
5369*/
drh2133d822008-01-03 18:44:59 +00005370/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005371** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005372**
drh2133d822008-01-03 18:44:59 +00005373** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005374** the end of the index key pointed to by cursor P1. This integer should be
5375** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005376**
drh9437bd22009-02-01 00:29:56 +00005377** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005378*/
drh170ad682017-06-02 15:44:22 +00005379case OP_DeferredSeek:
5380case OP_IdxRowid: { /* out2 */
5381 VdbeCursor *pC; /* The P1 index cursor */
5382 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5383 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005384
drh653b82a2009-06-22 11:10:47 +00005385 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5386 pC = p->apCsr[pOp->p1];
5387 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005388 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005389 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005390 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005391 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005392 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5393
5394 /* The IdxRowid and Seek opcodes are combined because of the commonality
5395 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5396 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005397
5398 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005399 ** out from under the cursor. That will never happens for an IdxRowid
5400 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005401 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5402
drh3da046d2013-11-11 03:24:11 +00005403 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005404 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005405 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005406 if( rc!=SQLITE_OK ){
5407 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005408 }
drh170ad682017-06-02 15:44:22 +00005409 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005410 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5411 pTabCur = p->apCsr[pOp->p3];
5412 assert( pTabCur!=0 );
5413 assert( pTabCur->eCurType==CURTYPE_BTREE );
5414 assert( pTabCur->uc.pCursor!=0 );
5415 assert( pTabCur->isTable );
5416 pTabCur->nullRow = 0;
5417 pTabCur->movetoTarget = rowid;
5418 pTabCur->deferredMoveto = 1;
5419 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5420 pTabCur->aAltMap = pOp->p4.ai;
5421 pTabCur->pAltCursor = pC;
5422 }else{
5423 pOut = out2Prerelease(p, pOp);
5424 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005425 }
5426 }else{
5427 assert( pOp->opcode==OP_IdxRowid );
5428 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005429 }
5430 break;
5431}
5432
danielk197761dd5832008-04-18 11:31:12 +00005433/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005434** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005435**
danielk197761dd5832008-04-18 11:31:12 +00005436** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005437** key that omits the PRIMARY KEY. Compare this key value against the index
5438** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5439** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005440**
danielk197761dd5832008-04-18 11:31:12 +00005441** If the P1 index entry is greater than or equal to the key value
5442** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005443*/
5444/* Opcode: IdxGT P1 P2 P3 P4 P5
5445** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005446**
drh4a1d3652014-02-14 15:13:36 +00005447** The P4 register values beginning with P3 form an unpacked index
5448** key that omits the PRIMARY KEY. Compare this key value against the index
5449** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5450** fields at the end.
5451**
5452** If the P1 index entry is greater than the key value
5453** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005454*/
drh3bb9b932010-08-06 02:10:00 +00005455/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005456** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005457**
danielk197761dd5832008-04-18 11:31:12 +00005458** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005459** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5460** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5461** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005462**
danielk197761dd5832008-04-18 11:31:12 +00005463** If the P1 index entry is less than the key value then jump to P2.
5464** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005465*/
drh4a1d3652014-02-14 15:13:36 +00005466/* Opcode: IdxLE P1 P2 P3 P4 P5
5467** Synopsis: key=r[P3@P4]
5468**
5469** The P4 register values beginning with P3 form an unpacked index
5470** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5471** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5472** ROWID on the P1 index.
5473**
5474** If the P1 index entry is less than or equal to the key value then jump
5475** to P2. Otherwise fall through to the next instruction.
5476*/
5477case OP_IdxLE: /* jump */
5478case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005479case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005480case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005481 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005482 int res;
5483 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005484
drh653b82a2009-06-22 11:10:47 +00005485 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5486 pC = p->apCsr[pOp->p1];
5487 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005488 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005489 assert( pC->eCurType==CURTYPE_BTREE );
5490 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005491 assert( pC->deferredMoveto==0 );
5492 assert( pOp->p5==0 || pOp->p5==1 );
5493 assert( pOp->p4type==P4_INT32 );
5494 r.pKeyInfo = pC->pKeyInfo;
5495 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005496 if( pOp->opcode<OP_IdxLT ){
5497 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005498 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005499 }else{
drh4a1d3652014-02-14 15:13:36 +00005500 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005501 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005502 }
5503 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005504#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00005505 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00005506#endif
drh2dc06482013-12-11 00:59:10 +00005507 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005508 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005509 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5510 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5511 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005512 res = -res;
5513 }else{
drh4a1d3652014-02-14 15:13:36 +00005514 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005515 res++;
5516 }
drh688852a2014-02-17 22:40:43 +00005517 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005518 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005519 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005520 break;
5521}
5522
drh98757152008-01-09 23:04:12 +00005523/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005524**
5525** Delete an entire database table or index whose root page in the database
5526** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005527**
drh98757152008-01-09 23:04:12 +00005528** The table being destroyed is in the main database file if P3==0. If
5529** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005530** that is used to store tables create using CREATE TEMPORARY TABLE.
5531**
drh205f48e2004-11-05 00:43:11 +00005532** If AUTOVACUUM is enabled then it is possible that another root page
5533** might be moved into the newly deleted root page in order to keep all
5534** root pages contiguous at the beginning of the database. The former
5535** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005536** is stored in register P2. If no page movement was required (because the
5537** table being dropped was already the last one in the database) then a
5538** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5539** is stored in register P2.
5540**
5541** This opcode throws an error if there are any active reader VMs when
5542** it is invoked. This is done to avoid the difficulty associated with
5543** updating existing cursors when a root page is moved in an AUTOVACUUM
5544** database. This error is thrown even if the database is not an AUTOVACUUM
5545** db in order to avoid introducing an incompatibility between autovacuum
5546** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005547**
drhb19a2bc2001-09-16 00:13:26 +00005548** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005549*/
drh27a348c2015-04-13 19:14:06 +00005550case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005551 int iMoved;
drh856c1032009-06-02 15:21:42 +00005552 int iDb;
drh3a949872012-09-18 13:20:13 +00005553
drh4031baf2018-05-28 17:31:20 +00005554 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00005555 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005556 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005557 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005558 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005559 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005560 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005561 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005562 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005563 }else{
drh856c1032009-06-02 15:21:42 +00005564 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005565 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005566 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005567 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005568 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005569 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005570 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005571#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005572 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005573 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5574 /* All OP_Destroy operations occur on the same btree */
5575 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5576 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005577 }
drh3765df42006-06-28 18:18:09 +00005578#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005579 }
drh5e00f6c2001-09-13 13:46:56 +00005580 break;
5581}
5582
danielk1977c7af4842008-10-27 13:59:33 +00005583/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005584**
5585** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005586** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005587** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005588**
drhf57b3392001-10-08 13:22:32 +00005589** The table being clear is in the main database file if P2==0. If
5590** P2==1 then the table to be clear is in the auxiliary database file
5591** that is used to store tables create using CREATE TEMPORARY TABLE.
5592**
shanebe217792009-03-05 04:20:31 +00005593** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005594** intkey table (an SQL table, not an index). In this case the row change
5595** count is incremented by the number of rows in the table being cleared.
5596** If P3 is greater than zero, then the value stored in register P3 is
5597** also incremented by the number of rows in the table being cleared.
5598**
drhb19a2bc2001-09-16 00:13:26 +00005599** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005600*/
drh9cbf3422008-01-17 16:22:13 +00005601case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005602 int nChange;
5603
drh4031baf2018-05-28 17:31:20 +00005604 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00005605 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005606 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005607 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005608 rc = sqlite3BtreeClearTable(
5609 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5610 );
5611 if( pOp->p3 ){
5612 p->nChange += nChange;
5613 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005614 assert( memIsValid(&aMem[pOp->p3]) );
5615 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005616 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005617 }
5618 }
drh9467abf2016-02-17 18:44:11 +00005619 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005620 break;
5621}
5622
drh65ea12c2014-03-19 17:41:36 +00005623/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005624**
drh65ea12c2014-03-19 17:41:36 +00005625** Delete all contents from the ephemeral table or sorter
5626** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005627**
drh65ea12c2014-03-19 17:41:36 +00005628** This opcode only works for cursors used for sorting and
5629** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005630*/
drh65ea12c2014-03-19 17:41:36 +00005631case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005632 VdbeCursor *pC;
5633
5634 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5635 pC = p->apCsr[pOp->p1];
5636 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005637 if( isSorter(pC) ){
5638 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005639 }else{
drhc960dcb2015-11-20 19:22:01 +00005640 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005641 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005642 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005643 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005644 }
drh079a3072014-03-19 14:10:55 +00005645 break;
5646}
5647
drh0f3f7662017-08-18 14:34:28 +00005648/* Opcode: CreateBtree P1 P2 P3 * *
5649** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005650**
drh0f3f7662017-08-18 14:34:28 +00005651** Allocate a new b-tree in the main database file if P1==0 or in the
5652** TEMP database file if P1==1 or in an attached database if
5653** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00005654** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00005655** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005656*/
drh0f3f7662017-08-18 14:34:28 +00005657case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005658 int pgno;
drh234c39d2004-07-24 03:30:47 +00005659 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005660
drh4031baf2018-05-28 17:31:20 +00005661 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00005662 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005663 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005664 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005665 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005666 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005667 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005668 pDb = &db->aDb[pOp->p1];
5669 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005670 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005671 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005672 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005673 break;
5674}
5675
drh4a54bb52017-02-18 15:58:52 +00005676/* Opcode: SqlExec * * * P4 *
5677**
5678** Run the SQL statement or statements specified in the P4 string.
5679*/
5680case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00005681 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00005682 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005683 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005684 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005685 if( rc ) goto abort_due_to_error;
5686 break;
5687}
5688
drh22645842011-03-24 01:34:03 +00005689/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005690**
5691** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005692** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005693**
5694** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005695** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005696*/
drh9cbf3422008-01-17 16:22:13 +00005697case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005698 int iDb;
5699 const char *zMaster;
5700 char *zSql;
5701 InitData initData;
5702
drhbdaec522011-04-04 00:14:43 +00005703 /* Any prepared statement that invokes this opcode will hold mutexes
5704 ** on every btree. This is a prerequisite for invoking
5705 ** sqlite3InitCallback().
5706 */
5707#ifdef SQLITE_DEBUG
5708 for(iDb=0; iDb<db->nDb; iDb++){
5709 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5710 }
5711#endif
drhbdaec522011-04-04 00:14:43 +00005712
drh856c1032009-06-02 15:21:42 +00005713 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005714 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005715 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005716 /* Used to be a conditional */ {
drhe0a04a32016-12-16 01:00:21 +00005717 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005718 initData.db = db;
5719 initData.iDb = pOp->p1;
5720 initData.pzErrMsg = &p->zErrMsg;
5721 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005722 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005723 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005724 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005725 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005726 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005727 assert( db->init.busy==0 );
5728 db->init.busy = 1;
5729 initData.rc = SQLITE_OK;
5730 assert( !db->mallocFailed );
5731 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5732 if( rc==SQLITE_OK ) rc = initData.rc;
drhdbd6a7d2017-04-05 12:39:49 +00005733 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005734 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005735 }
drh3c23a882007-01-09 14:01:13 +00005736 }
drh9467abf2016-02-17 18:44:11 +00005737 if( rc ){
5738 sqlite3ResetAllSchemasOfConnection(db);
5739 if( rc==SQLITE_NOMEM ){
5740 goto no_mem;
5741 }
5742 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005743 }
drh234c39d2004-07-24 03:30:47 +00005744 break;
5745}
5746
drh8bfdf722009-06-19 14:06:03 +00005747#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005748/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005749**
5750** Read the sqlite_stat1 table for database P1 and load the content
5751** of that table into the internal index hash table. This will cause
5752** the analysis to be used when preparing all subsequent queries.
5753*/
drh9cbf3422008-01-17 16:22:13 +00005754case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005755 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5756 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005757 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005758 break;
5759}
drh8bfdf722009-06-19 14:06:03 +00005760#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005761
drh98757152008-01-09 23:04:12 +00005762/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005763**
5764** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005765** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005766** is dropped from disk (using the Destroy opcode) in order to keep
5767** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005768** schema consistent with what is on disk.
5769*/
drh9cbf3422008-01-17 16:22:13 +00005770case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00005771 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005772 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005773 break;
5774}
5775
drh98757152008-01-09 23:04:12 +00005776/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005777**
5778** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005779** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005780** is dropped from disk (using the Destroy opcode)
5781** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005782** schema consistent with what is on disk.
5783*/
drh9cbf3422008-01-17 16:22:13 +00005784case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00005785 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005786 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005787 break;
5788}
5789
drh98757152008-01-09 23:04:12 +00005790/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005791**
5792** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005793** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005794** is dropped from disk (using the Destroy opcode) in order to keep
5795** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005796** schema consistent with what is on disk.
5797*/
drh9cbf3422008-01-17 16:22:13 +00005798case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00005799 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00005800 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005801 break;
5802}
5803
drh234c39d2004-07-24 03:30:47 +00005804
drhb7f91642004-10-31 02:22:47 +00005805#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005806/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005807**
drh98757152008-01-09 23:04:12 +00005808** Do an analysis of the currently open database. Store in
5809** register P1 the text of an error message describing any problems.
5810** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005811**
drh66accfc2017-02-22 18:04:42 +00005812** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005813** At most reg(P3) errors will be reported.
5814** In other words, the analysis stops as soon as reg(P1) errors are
5815** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005816**
drh98968b22016-03-15 22:00:39 +00005817** The root page numbers of all tables in the database are integers
5818** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005819**
drh98757152008-01-09 23:04:12 +00005820** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005821** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005822**
drh1dcdbc02007-01-27 02:24:54 +00005823** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005824*/
drhaaab5722002-02-19 13:39:21 +00005825case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005826 int nRoot; /* Number of tables to check. (Number of root pages.) */
5827 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005828 int nErr; /* Number of errors reported */
5829 char *z; /* Text of the error report */
5830 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005831
drh1713afb2013-06-28 01:24:57 +00005832 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005833 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005834 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005835 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00005836 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00005837 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005838 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005839 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005840 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005841 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005842 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005843 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drhb5c10632017-09-21 00:49:15 +00005844 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00005845 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005846 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005847 if( nErr==0 ){
5848 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005849 }else if( z==0 ){
5850 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005851 }else{
drh66accfc2017-02-22 18:04:42 +00005852 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005853 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005854 }
drhb7654112008-01-12 12:48:07 +00005855 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005856 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005857 break;
5858}
drhb7f91642004-10-31 02:22:47 +00005859#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005860
drh3d4501e2008-12-04 20:40:10 +00005861/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005862** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005863**
drhbb6783b2017-04-29 18:02:49 +00005864** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00005865** held in register P1.
5866**
5867** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005868*/
drh93952eb2009-11-13 19:43:43 +00005869case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005870 pIn1 = &aMem[pOp->p1];
5871 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005872 assert( (pIn2->flags & MEM_Int)!=0 );
5873 if( (pIn1->flags & MEM_RowSet)==0 ){
5874 sqlite3VdbeMemSetRowSet(pIn1);
5875 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005876 }
drh93952eb2009-11-13 19:43:43 +00005877 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005878 break;
5879}
5880
5881/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005882** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005883**
drhbb6783b2017-04-29 18:02:49 +00005884** Extract the smallest value from the RowSet object in P1
5885** and put that value into register P3.
5886** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00005887** unchanged and jump to instruction P2.
5888*/
drh93952eb2009-11-13 19:43:43 +00005889case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005890 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005891
drh3c657212009-11-17 23:59:58 +00005892 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005893 if( (pIn1->flags & MEM_RowSet)==0
5894 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005895 ){
5896 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005897 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005898 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005899 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005900 }else{
5901 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005902 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005903 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005904 }
drh49afe3a2013-07-10 03:05:14 +00005905 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005906}
5907
drh1b26c7c2009-04-22 02:15:47 +00005908/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005909** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005910**
drhade97602009-04-21 15:05:18 +00005911** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005912** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005913** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005914** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005915** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005916**
drhbb6783b2017-04-29 18:02:49 +00005917** The RowSet object is optimized for the case where sets of integers
5918** are inserted in distinct phases, which each set contains no duplicates.
5919** Each set is identified by a unique P4 value. The first set
5920** must have P4==0, the final set must have P4==-1, and for all other sets
5921** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00005922**
5923** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00005924** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005925** (b) when P4==-1 there is no need to insert the value, as it will
5926** never be tested for, and (c) when a value that is part of set X is
5927** inserted, there is no need to search to see if the same value was
5928** previously inserted as part of set X (only if it was previously
5929** inserted as part of some other set).
5930*/
drh1b26c7c2009-04-22 02:15:47 +00005931case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005932 int iSet;
5933 int exists;
5934
drh3c657212009-11-17 23:59:58 +00005935 pIn1 = &aMem[pOp->p1];
5936 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005937 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005938 assert( pIn3->flags&MEM_Int );
5939
drh1b26c7c2009-04-22 02:15:47 +00005940 /* If there is anything other than a rowset object in memory cell P1,
5941 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005942 */
drh733bf1b2009-04-22 00:47:00 +00005943 if( (pIn1->flags & MEM_RowSet)==0 ){
5944 sqlite3VdbeMemSetRowSet(pIn1);
5945 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005946 }
5947
5948 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005949 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005950 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005951 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005952 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005953 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005954 }
5955 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005956 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005957 }
5958 break;
5959}
5960
drh5e00f6c2001-09-13 13:46:56 +00005961
danielk197793758c82005-01-21 08:13:14 +00005962#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005963
drh0fd61352014-02-07 02:29:45 +00005964/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005965**
dan76d462e2009-08-30 11:42:51 +00005966** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005967**
dan76d462e2009-08-30 11:42:51 +00005968** P1 contains the address of the memory cell that contains the first memory
5969** cell in an array of values used as arguments to the sub-program. P2
5970** contains the address to jump to if the sub-program throws an IGNORE
5971** exception using the RAISE() function. Register P3 contains the address
5972** of a memory cell in this (the parent) VM that is used to allocate the
5973** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005974**
5975** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005976**
5977** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005978*/
dan76d462e2009-08-30 11:42:51 +00005979case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005980 int nMem; /* Number of memory registers for sub-program */
5981 int nByte; /* Bytes of runtime space required for sub-program */
5982 Mem *pRt; /* Register to allocate runtime space */
5983 Mem *pMem; /* Used to iterate through memory cells */
5984 Mem *pEnd; /* Last memory cell in new array */
5985 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5986 SubProgram *pProgram; /* Sub-program to execute */
5987 void *t; /* Token identifying trigger */
5988
5989 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005990 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005991 assert( pProgram->nOp>0 );
5992
dan1da40a32009-09-19 17:00:31 +00005993 /* If the p5 flag is clear, then recursive invocation of triggers is
5994 ** disabled for backwards compatibility (p5 is set if this sub-program
5995 ** is really a trigger, not a foreign key action, and the flag set
5996 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005997 **
5998 ** It is recursive invocation of triggers, at the SQL level, that is
5999 ** disabled. In some cases a single trigger may generate more than one
6000 ** SubProgram (if the trigger may be executed with more than one different
6001 ** ON CONFLICT algorithm). SubProgram structures associated with a
6002 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006003 ** variable. */
6004 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006005 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006006 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6007 if( pFrame ) break;
6008 }
6009
danf5894502009-10-07 18:41:19 +00006010 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006011 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006012 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006013 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006014 }
6015
6016 /* Register pRt is used to store the memory required to save the state
6017 ** of the current program, and the memory required at runtime to execute
6018 ** the trigger program. If this trigger has been fired before, then pRt
6019 ** is already allocated. Otherwise, it must be initialized. */
6020 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00006021 /* SubProgram.nMem is set to the number of memory cells used by the
6022 ** program stored in SubProgram.aOp. As well as these, one memory
6023 ** cell is required for each cursor used by the program. Set local
6024 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6025 */
dan65a7cd12009-09-01 12:16:01 +00006026 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006027 assert( nMem>0 );
6028 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006029 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006030 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006031 + pProgram->nCsr * sizeof(VdbeCursor*)
6032 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006033 pFrame = sqlite3DbMallocZero(db, nByte);
6034 if( !pFrame ){
6035 goto no_mem;
6036 }
6037 sqlite3VdbeMemRelease(pRt);
6038 pRt->flags = MEM_Frame;
6039 pRt->u.pFrame = pFrame;
6040
6041 pFrame->v = p;
6042 pFrame->nChildMem = nMem;
6043 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006044 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006045 pFrame->aMem = p->aMem;
6046 pFrame->nMem = p->nMem;
6047 pFrame->apCsr = p->apCsr;
6048 pFrame->nCursor = p->nCursor;
6049 pFrame->aOp = p->aOp;
6050 pFrame->nOp = p->nOp;
6051 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00006052#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006053 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00006054#endif
dan165921a2009-08-28 18:53:45 +00006055
6056 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
6057 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00006058 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00006059 pMem->db = db;
6060 }
6061 }else{
6062 pFrame = pRt->u.pFrame;
drh9f6168b2016-03-19 23:32:58 +00006063 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
6064 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00006065 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00006066 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00006067 }
6068
6069 p->nFrame++;
6070 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00006071 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00006072 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00006073 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00006074 assert( pFrame->pAuxData==0 );
6075 pFrame->pAuxData = p->pAuxData;
6076 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00006077 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00006078 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00006079 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00006080 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00006081 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00006082 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00006083 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00006084 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00006085 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00006086 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00006087#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00006088 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00006089#endif
drhf56fa462015-04-13 21:39:54 +00006090 pOp = &aOp[-1];
dan165921a2009-08-28 18:53:45 +00006091
6092 break;
6093}
6094
dan76d462e2009-08-30 11:42:51 +00006095/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00006096**
dan76d462e2009-08-30 11:42:51 +00006097** This opcode is only ever present in sub-programs called via the
6098** OP_Program instruction. Copy a value currently stored in a memory
6099** cell of the calling (parent) frame to cell P2 in the current frames
6100** address space. This is used by trigger programs to access the new.*
6101** and old.* values.
dan165921a2009-08-28 18:53:45 +00006102**
dan76d462e2009-08-30 11:42:51 +00006103** The address of the cell in the parent frame is determined by adding
6104** the value of the P1 argument to the value of the P1 argument to the
6105** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00006106*/
drh27a348c2015-04-13 19:14:06 +00006107case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00006108 VdbeFrame *pFrame;
6109 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00006110 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00006111 pFrame = p->pFrame;
6112 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00006113 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6114 break;
6115}
6116
danielk197793758c82005-01-21 08:13:14 +00006117#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00006118
dan1da40a32009-09-19 17:00:31 +00006119#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00006120/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006121** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00006122**
dan0ff297e2009-09-25 17:03:14 +00006123** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6124** If P1 is non-zero, the database constraint counter is incremented
6125** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00006126** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00006127*/
dan32b09f22009-09-23 17:29:59 +00006128case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00006129 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00006130 db->nDeferredImmCons += pOp->p2;
6131 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006132 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006133 }else{
dan0ff297e2009-09-25 17:03:14 +00006134 p->nFkConstraint += pOp->p2;
6135 }
6136 break;
6137}
6138
6139/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006140** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006141**
6142** This opcode tests if a foreign key constraint-counter is currently zero.
6143** If so, jump to instruction P2. Otherwise, fall through to the next
6144** instruction.
6145**
6146** If P1 is non-zero, then the jump is taken if the database constraint-counter
6147** is zero (the one that counts deferred constraint violations). If P1 is
6148** zero, the jump is taken if the statement constraint-counter is zero
6149** (immediate foreign key constraint violations).
6150*/
6151case OP_FkIfZero: { /* jump */
6152 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006153 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006154 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006155 }else{
drh688852a2014-02-17 22:40:43 +00006156 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006157 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006158 }
dan1da40a32009-09-19 17:00:31 +00006159 break;
6160}
6161#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6162
drh205f48e2004-11-05 00:43:11 +00006163#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006164/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006165** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006166**
dan76d462e2009-08-30 11:42:51 +00006167** P1 is a register in the root frame of this VM (the root frame is
6168** different from the current frame if this instruction is being executed
6169** within a sub-program). Set the value of register P1 to the maximum of
6170** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006171**
6172** This instruction throws an error if the memory cell is not initially
6173** an integer.
6174*/
dan76d462e2009-08-30 11:42:51 +00006175case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006176 VdbeFrame *pFrame;
6177 if( p->pFrame ){
6178 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6179 pIn1 = &pFrame->aMem[pOp->p1];
6180 }else{
drha6c2ed92009-11-14 23:22:23 +00006181 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006182 }
drh2b4ded92010-09-27 21:09:31 +00006183 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006184 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006185 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006186 sqlite3VdbeMemIntegerify(pIn2);
6187 if( pIn1->u.i<pIn2->u.i){
6188 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006189 }
6190 break;
6191}
6192#endif /* SQLITE_OMIT_AUTOINCREMENT */
6193
drh8b0cf382015-10-06 21:07:06 +00006194/* Opcode: IfPos P1 P2 P3 * *
6195** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006196**
drh16897072015-03-07 00:57:37 +00006197** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006198** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006199** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006200**
drh16897072015-03-07 00:57:37 +00006201** If the initial value of register P1 is less than 1, then the
6202** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006203*/
drh9cbf3422008-01-17 16:22:13 +00006204case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006205 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006206 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006207 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006208 if( pIn1->u.i>0 ){
6209 pIn1->u.i -= pOp->p3;
6210 goto jump_to_p2;
6211 }
drhec7429a2005-10-06 16:53:14 +00006212 break;
6213}
6214
drhcc2fa4c2016-01-25 15:57:29 +00006215/* Opcode: OffsetLimit P1 P2 P3 * *
6216** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006217**
drhcc2fa4c2016-01-25 15:57:29 +00006218** This opcode performs a commonly used computation associated with
6219** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6220** holds the offset counter. The opcode computes the combined value
6221** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6222** value computed is the total number of rows that will need to be
6223** visited in order to complete the query.
6224**
6225** If r[P3] is zero or negative, that means there is no OFFSET
6226** and r[P2] is set to be the value of the LIMIT, r[P1].
6227**
6228** if r[P1] is zero or negative, that means there is no LIMIT
6229** and r[P2] is set to -1.
6230**
6231** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006232*/
drhcc2fa4c2016-01-25 15:57:29 +00006233case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006234 i64 x;
drh3c657212009-11-17 23:59:58 +00006235 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006236 pIn3 = &aMem[pOp->p3];
6237 pOut = out2Prerelease(p, pOp);
6238 assert( pIn1->flags & MEM_Int );
6239 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006240 x = pIn1->u.i;
6241 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6242 /* If the LIMIT is less than or equal to zero, loop forever. This
6243 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6244 ** also loop forever. This is undocumented. In fact, one could argue
6245 ** that the loop should terminate. But assuming 1 billion iterations
6246 ** per second (far exceeding the capabilities of any current hardware)
6247 ** it would take nearly 300 years to actually reach the limit. So
6248 ** looping forever is a reasonable approximation. */
6249 pOut->u.i = -1;
6250 }else{
6251 pOut->u.i = x;
6252 }
drh15007a92006-01-08 18:10:17 +00006253 break;
6254}
6255
drhf99dd352016-12-18 17:42:00 +00006256/* Opcode: IfNotZero P1 P2 * * *
6257** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006258**
drh16897072015-03-07 00:57:37 +00006259** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006260** initially greater than zero, then decrement the value in register P1.
6261** If it is non-zero (negative or positive) and then also jump to P2.
6262** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006263*/
drh16897072015-03-07 00:57:37 +00006264case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006265 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006266 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006267 VdbeBranchTaken(pIn1->u.i<0, 2);
6268 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006269 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006270 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006271 }
6272 break;
6273}
6274
6275/* Opcode: DecrJumpZero P1 P2 * * *
6276** Synopsis: if (--r[P1])==0 goto P2
6277**
drhab5be2e2016-11-30 05:08:59 +00006278** Register P1 must hold an integer. Decrement the value in P1
6279** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006280*/
6281case OP_DecrJumpZero: { /* jump, in1 */
6282 pIn1 = &aMem[pOp->p1];
6283 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006284 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6285 VdbeBranchTaken(pIn1->u.i==0, 2);
6286 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006287 break;
6288}
6289
drh16897072015-03-07 00:57:37 +00006290
dan9a947222018-06-14 19:06:36 +00006291/* Opcode: AggStep0 P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006292** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006293**
dan9a947222018-06-14 19:06:36 +00006294** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6295** aggregate. The function has P5 arguments. P4 is a pointer to the
6296** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00006297** accumulator.
drhe5095352002-02-24 03:25:14 +00006298**
drh98757152008-01-09 23:04:12 +00006299** The P5 arguments are taken from register P2 and its
6300** successors.
drhe5095352002-02-24 03:25:14 +00006301*/
dan9a947222018-06-14 19:06:36 +00006302/* Opcode: AggStep P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00006303** Synopsis: accum=r[P3] step(r[P2@P5])
6304**
dan9a947222018-06-14 19:06:36 +00006305** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
6306** aggregate. The function has P5 arguments. P4 is a pointer to the
6307** FuncDef structure that specifies the function. Register P3 is the
6308** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00006309**
6310** The P5 arguments are taken from register P2 and its
6311** successors.
6312**
6313** This opcode is initially coded as OP_AggStep0. On first evaluation,
6314** the FuncDef stored in P4 is converted into an sqlite3_context and
6315** the opcode is changed. In this way, the initialization of the
6316** sqlite3_context only happens once, instead of on each call to the
6317** step function.
6318*/
drh9c7c9132015-06-26 18:16:52 +00006319case OP_AggStep0: {
drh856c1032009-06-02 15:21:42 +00006320 int n;
drh9c7c9132015-06-26 18:16:52 +00006321 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006322
drh9c7c9132015-06-26 18:16:52 +00006323 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006324 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006325 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6326 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006327 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00006328 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
6329 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00006330 if( pCtx==0 ) goto no_mem;
6331 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00006332 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
6333 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006334 pCtx->pFunc = pOp->p4.pFunc;
6335 pCtx->iOp = (int)(pOp - aOp);
6336 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00006337 pCtx->skipFlag = 0;
6338 pCtx->isError = 0;
drh9c7c9132015-06-26 18:16:52 +00006339 pCtx->argc = n;
6340 pOp->p4type = P4_FUNCCTX;
6341 pOp->p4.pCtx = pCtx;
6342 pOp->opcode = OP_AggStep;
6343 /* Fall through into OP_AggStep */
6344}
6345case OP_AggStep: {
6346 int i;
6347 sqlite3_context *pCtx;
6348 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00006349
6350 assert( pOp->p4type==P4_FUNCCTX );
6351 pCtx = pOp->p4.pCtx;
6352 pMem = &aMem[pOp->p3];
6353
6354 /* If this function is inside of a trigger, the register array in aMem[]
6355 ** might change from one evaluation to the next. The next block of code
6356 ** checks to see if the register array has changed, and if so it
6357 ** reinitializes the relavant parts of the sqlite3_context object */
6358 if( pCtx->pMem != pMem ){
6359 pCtx->pMem = pMem;
6360 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6361 }
6362
6363#ifdef SQLITE_DEBUG
6364 for(i=0; i<pCtx->argc; i++){
6365 assert( memIsValid(pCtx->argv[i]) );
6366 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6367 }
6368#endif
6369
drhabfcea22005-09-06 20:36:48 +00006370 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00006371 assert( pCtx->pOut->flags==MEM_Null );
6372 assert( pCtx->isError==0 );
6373 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00006374#ifndef SQLITE_OMIT_WINDOWFUNC
6375 if( pOp->p1 ){
6376 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
6377 }else
6378#endif
6379 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6380
drhf09ac0b2018-01-23 03:44:06 +00006381 if( pCtx->isError ){
6382 if( pCtx->isError>0 ){
6383 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00006384 rc = pCtx->isError;
6385 }
drhf09ac0b2018-01-23 03:44:06 +00006386 if( pCtx->skipFlag ){
6387 assert( pOp[-1].opcode==OP_CollSeq );
6388 i = pOp[-1].p1;
6389 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6390 pCtx->skipFlag = 0;
6391 }
6392 sqlite3VdbeMemRelease(pCtx->pOut);
6393 pCtx->pOut->flags = MEM_Null;
6394 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00006395 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00006396 }
drhf09ac0b2018-01-23 03:44:06 +00006397 assert( pCtx->pOut->flags==MEM_Null );
6398 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00006399 break;
6400}
6401
dan9a947222018-06-14 19:06:36 +00006402/* Opcode: AggFinal P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006403** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006404**
dan9a947222018-06-14 19:06:36 +00006405** P1 is the memory location that is the accumulator for an aggregate
6406** or window function. If P3 is zero, then execute the finalizer function
6407** for an aggregate and store the result in P1. Or, if P3 is non-zero,
6408** invoke the xValue() function and store the result in register P3.
drha10a34b2005-09-07 22:09:48 +00006409**
6410** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006411** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006412** argument is not used by this opcode. It is only there to disambiguate
6413** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00006414** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00006415** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006416*/
drh9cbf3422008-01-17 16:22:13 +00006417case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006418 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006419 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006420 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006421 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00006422#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00006423 if( pOp->p3 ){
6424 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00006425 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00006426 }else
6427#endif
6428 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6429
drh4c8555f2009-06-25 01:47:11 +00006430 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006431 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006432 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006433 }
drh2dca8682008-03-21 17:13:13 +00006434 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006435 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006436 if( sqlite3VdbeMemTooBig(pMem) ){
6437 goto too_big;
6438 }
drh5e00f6c2001-09-13 13:46:56 +00006439 break;
6440}
6441
dan5cf53532010-05-01 16:40:20 +00006442#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006443/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006444**
6445** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006446** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6447** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006448** SQLITE_BUSY or not, respectively. Write the number of pages in the
6449** WAL after the checkpoint into mem[P3+1] and the number of pages
6450** in the WAL that have been checkpointed after the checkpoint
6451** completes into mem[P3+2]. However on an error, mem[P3+1] and
6452** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006453*/
6454case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006455 int i; /* Loop counter */
6456 int aRes[3]; /* Results */
6457 Mem *pMem; /* Write results here */
6458
drh9e92a472013-06-27 17:40:30 +00006459 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006460 aRes[0] = 0;
6461 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006462 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6463 || pOp->p2==SQLITE_CHECKPOINT_FULL
6464 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006465 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006466 );
drh30aa3b92011-02-07 23:56:01 +00006467 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006468 if( rc ){
6469 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006470 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006471 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006472 }
drh30aa3b92011-02-07 23:56:01 +00006473 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6474 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6475 }
dan7c246102010-04-12 19:00:29 +00006476 break;
6477};
dan5cf53532010-05-01 16:40:20 +00006478#endif
drh5e00f6c2001-09-13 13:46:56 +00006479
drhcac29a62010-07-02 19:36:52 +00006480#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006481/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006482**
6483** Change the journal mode of database P1 to P3. P3 must be one of the
6484** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6485** modes (delete, truncate, persist, off and memory), this is a simple
6486** operation. No IO is required.
6487**
6488** If changing into or out of WAL mode the procedure is more complicated.
6489**
6490** Write a string containing the final journal-mode to register P2.
6491*/
drh27a348c2015-04-13 19:14:06 +00006492case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006493 Btree *pBt; /* Btree to change journal mode of */
6494 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006495 int eNew; /* New journal mode */
6496 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006497#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006498 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006499#endif
dane04dc882010-04-20 18:53:15 +00006500
drh27a348c2015-04-13 19:14:06 +00006501 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006502 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006503 assert( eNew==PAGER_JOURNALMODE_DELETE
6504 || eNew==PAGER_JOURNALMODE_TRUNCATE
6505 || eNew==PAGER_JOURNALMODE_PERSIST
6506 || eNew==PAGER_JOURNALMODE_OFF
6507 || eNew==PAGER_JOURNALMODE_MEMORY
6508 || eNew==PAGER_JOURNALMODE_WAL
6509 || eNew==PAGER_JOURNALMODE_QUERY
6510 );
6511 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006512 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006513
dane04dc882010-04-20 18:53:15 +00006514 pBt = db->aDb[pOp->p1].pBt;
6515 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006516 eOld = sqlite3PagerGetJournalMode(pPager);
6517 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6518 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006519
6520#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006521 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006522
drhd80b2332010-05-01 00:59:37 +00006523 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006524 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006525 */
6526 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006527 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006528 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006529 ){
drh0b9b4302010-06-11 17:01:24 +00006530 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006531 }
6532
drh0b9b4302010-06-11 17:01:24 +00006533 if( (eNew!=eOld)
6534 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6535 ){
danc0537fe2013-06-28 19:41:43 +00006536 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006537 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006538 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006539 "cannot change %s wal mode from within a transaction",
6540 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6541 );
drh9467abf2016-02-17 18:44:11 +00006542 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006543 }else{
6544
6545 if( eOld==PAGER_JOURNALMODE_WAL ){
6546 /* If leaving WAL mode, close the log file. If successful, the call
6547 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6548 ** file. An EXCLUSIVE lock may still be held on the database file
6549 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006550 */
dan7fb89902016-08-12 16:21:15 +00006551 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006552 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006553 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006554 }
drh242c4f72010-06-22 14:49:39 +00006555 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6556 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6557 ** as an intermediate */
6558 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006559 }
6560
6561 /* Open a transaction on the database file. Regardless of the journal
6562 ** mode, this transaction always uses a rollback journal.
6563 */
6564 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6565 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006566 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006567 }
6568 }
6569 }
dan5cf53532010-05-01 16:40:20 +00006570#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006571
drh9467abf2016-02-17 18:44:11 +00006572 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006573 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006574
dane04dc882010-04-20 18:53:15 +00006575 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006576 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006577 pOut->n = sqlite3Strlen30(pOut->z);
6578 pOut->enc = SQLITE_UTF8;
6579 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006580 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006581 break;
drhcac29a62010-07-02 19:36:52 +00006582};
6583#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006584
drhfdbcdee2007-03-27 14:44:50 +00006585#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh9ef5e772016-08-19 14:20:56 +00006586/* Opcode: Vacuum P1 * * * *
drh6f8c91c2003-12-07 00:24:35 +00006587**
drh9ef5e772016-08-19 14:20:56 +00006588** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6589** for an attached database. The "temp" database may not be vacuumed.
drh6f8c91c2003-12-07 00:24:35 +00006590*/
drh9cbf3422008-01-17 16:22:13 +00006591case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006592 assert( p->readOnly==0 );
drh9ef5e772016-08-19 14:20:56 +00006593 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006594 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006595 break;
6596}
drh154d4b22006-09-21 11:02:16 +00006597#endif
drh6f8c91c2003-12-07 00:24:35 +00006598
danielk1977dddbcdc2007-04-26 14:42:34 +00006599#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006600/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006601**
6602** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006603** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006604** P2. Otherwise, fall through to the next instruction.
6605*/
drh9cbf3422008-01-17 16:22:13 +00006606case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006607 Btree *pBt;
6608
6609 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006610 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006611 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006612 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006613 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006614 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006615 if( rc ){
6616 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006617 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006618 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006619 }
6620 break;
6621}
6622#endif
6623
drh98757152008-01-09 23:04:12 +00006624/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006625**
drh25df48d2014-07-22 14:58:12 +00006626** Cause precompiled statements to expire. When an expired statement
6627** is executed using sqlite3_step() it will either automatically
6628** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6629** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006630**
6631** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006632** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00006633*/
drh9cbf3422008-01-17 16:22:13 +00006634case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00006635 if( !pOp->p1 ){
6636 sqlite3ExpirePreparedStatements(db);
6637 }else{
6638 p->expired = 1;
6639 }
6640 break;
6641}
6642
danielk1977c00da102006-01-07 13:21:04 +00006643#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006644/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006645** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006646**
6647** Obtain a lock on a particular table. This instruction is only used when
6648** the shared-cache feature is enabled.
6649**
danielk197796d48e92009-06-29 06:00:37 +00006650** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006651** on which the lock is acquired. A readlock is obtained if P3==0 or
6652** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006653**
6654** P2 contains the root-page of the table to lock.
6655**
drh66a51672008-01-03 00:01:23 +00006656** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006657** used to generate an error message if the lock cannot be obtained.
6658*/
drh9cbf3422008-01-17 16:22:13 +00006659case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006660 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006661 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006662 int p1 = pOp->p1;
6663 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006664 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006665 assert( isWriteLock==0 || isWriteLock==1 );
6666 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006667 if( rc ){
6668 if( (rc&0xFF)==SQLITE_LOCKED ){
6669 const char *z = pOp->p4.z;
6670 sqlite3VdbeError(p, "database table is locked: %s", z);
6671 }
6672 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006673 }
danielk1977c00da102006-01-07 13:21:04 +00006674 }
6675 break;
6676}
drhb9bb7c12006-06-11 23:41:55 +00006677#endif /* SQLITE_OMIT_SHARED_CACHE */
6678
6679#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006680/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006681**
danielk19773e3a84d2008-08-01 17:37:40 +00006682** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6683** xBegin method for that table.
6684**
6685** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006686** within a callback to a virtual table xSync() method. If it is, the error
6687** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006688*/
drh9cbf3422008-01-17 16:22:13 +00006689case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006690 VTable *pVTab;
6691 pVTab = pOp->p4.pVtab;
6692 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006693 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006694 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006695 break;
6696}
6697#endif /* SQLITE_OMIT_VIRTUALTABLE */
6698
6699#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006700/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006701**
dan73779452015-03-19 18:56:17 +00006702** P2 is a register that holds the name of a virtual table in database
6703** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006704*/
drh9cbf3422008-01-17 16:22:13 +00006705case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006706 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006707 const char *zTab; /* Name of the virtual table */
6708
dan73779452015-03-19 18:56:17 +00006709 memset(&sMem, 0, sizeof(sMem));
6710 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006711 /* Because P2 is always a static string, it is impossible for the
6712 ** sqlite3VdbeMemCopy() to fail */
6713 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6714 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006715 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006716 assert( rc==SQLITE_OK );
6717 zTab = (const char*)sqlite3_value_text(&sMem);
6718 assert( zTab || db->mallocFailed );
6719 if( zTab ){
6720 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006721 }
6722 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006723 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006724 break;
6725}
6726#endif /* SQLITE_OMIT_VIRTUALTABLE */
6727
6728#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006729/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006730**
drh66a51672008-01-03 00:01:23 +00006731** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006732** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006733*/
drh9cbf3422008-01-17 16:22:13 +00006734case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006735 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006736 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006737 db->nVDestroy--;
drh9467abf2016-02-17 18:44:11 +00006738 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006739 break;
6740}
6741#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006742
drh9eff6162006-06-12 21:59:13 +00006743#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006744/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006745**
drh66a51672008-01-03 00:01:23 +00006746** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006747** P1 is a cursor number. This opcode opens a cursor to the virtual
6748** table and stores that cursor in P1.
6749*/
drh9cbf3422008-01-17 16:22:13 +00006750case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006751 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006752 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006753 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006754 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006755
drh1713afb2013-06-28 01:24:57 +00006756 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006757 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006758 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006759 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006760 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6761 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006762 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006763 }
6764 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006765 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006766 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006767 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006768
drh9467abf2016-02-17 18:44:11 +00006769 /* Initialize sqlite3_vtab_cursor base class */
6770 pVCur->pVtab = pVtab;
6771
6772 /* Initialize vdbe cursor object */
6773 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6774 if( pCur ){
6775 pCur->uc.pVCur = pVCur;
6776 pVtab->nRef++;
6777 }else{
6778 assert( db->mallocFailed );
6779 pModule->xClose(pVCur);
6780 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006781 }
drh9eff6162006-06-12 21:59:13 +00006782 break;
6783}
6784#endif /* SQLITE_OMIT_VIRTUALTABLE */
6785
6786#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006787/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006788** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006789**
6790** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6791** the filtered result set is empty.
6792**
drh66a51672008-01-03 00:01:23 +00006793** P4 is either NULL or a string that was generated by the xBestIndex
6794** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006795** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006796**
drh9eff6162006-06-12 21:59:13 +00006797** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006798** by P1. The integer query plan parameter to xFilter is stored in register
6799** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006800** xFilter method. Registers P3+2..P3+1+argc are the argc
6801** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006802** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006803**
danielk19776dbee812008-01-03 18:39:41 +00006804** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006805*/
drh9cbf3422008-01-17 16:22:13 +00006806case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006807 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006808 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006809 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006810 Mem *pQuery;
6811 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006812 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006813 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006814 VdbeCursor *pCur;
6815 int res;
6816 int i;
6817 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006818
drha6c2ed92009-11-14 23:22:23 +00006819 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006820 pArgc = &pQuery[1];
6821 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006822 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006823 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006824 assert( pCur->eCurType==CURTYPE_VTAB );
6825 pVCur = pCur->uc.pVCur;
6826 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006827 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006828
drh9cbf3422008-01-17 16:22:13 +00006829 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006830 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006831 nArg = (int)pArgc->u.i;
6832 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006833
drh644a5292006-12-20 14:53:38 +00006834 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006835 res = 0;
6836 apArg = p->apArg;
6837 for(i = 0; i<nArg; i++){
6838 apArg[i] = &pArgc[i+1];
6839 }
drhc960dcb2015-11-20 19:22:01 +00006840 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006841 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006842 if( rc ) goto abort_due_to_error;
6843 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006844 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006845 VdbeBranchTaken(res!=0,2);
6846 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006847 break;
6848}
6849#endif /* SQLITE_OMIT_VIRTUALTABLE */
6850
6851#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00006852/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00006853** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006854**
drh6f390be2018-01-11 17:04:26 +00006855** Store in register P3 the value of the P2-th column of
6856** the current row of the virtual-table of cursor P1.
6857**
6858** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00006859** an unchanging column during an UPDATE operation, then the P5
6860** value is 1. Otherwise, P5 is 0. The P5 value is returned
drhbbd574b2018-05-24 17:25:35 +00006861** by sqlite3_vtab_nochange() routine and can be used
drh6f390be2018-01-11 17:04:26 +00006862** by virtual table implementations to return special "no-change"
6863** marks which can be more efficient, depending on the virtual table.
drh9eff6162006-06-12 21:59:13 +00006864*/
6865case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006866 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006867 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006868 Mem *pDest;
6869 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006870
drhdfe88ec2008-11-03 20:55:06 +00006871 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006872 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00006873 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006874 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006875 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006876 if( pCur->nullRow ){
6877 sqlite3VdbeMemSetNull(pDest);
6878 break;
6879 }
drhc960dcb2015-11-20 19:22:01 +00006880 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006881 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006882 assert( pModule->xColumn );
6883 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006884 sContext.pOut = pDest;
drhce2fbd12018-01-12 21:00:14 +00006885 if( pOp->p5 ){
6886 sqlite3VdbeMemSetNull(pDest);
6887 pDest->flags = MEM_Null|MEM_Zero;
6888 pDest->u.nZero = 0;
6889 }else{
6890 MemSetTypeFlag(pDest, MEM_Null);
6891 }
drhc960dcb2015-11-20 19:22:01 +00006892 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006893 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00006894 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00006895 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00006896 rc = sContext.isError;
6897 }
drh9bd038f2014-08-27 14:14:06 +00006898 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006899 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006900 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006901
drhde4fcfd2008-01-19 23:50:26 +00006902 if( sqlite3VdbeMemTooBig(pDest) ){
6903 goto too_big;
6904 }
drh9467abf2016-02-17 18:44:11 +00006905 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00006906 break;
6907}
6908#endif /* SQLITE_OMIT_VIRTUALTABLE */
6909
6910#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006911/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006912**
6913** Advance virtual table P1 to the next row in its result set and
6914** jump to instruction P2. Or, if the virtual table has reached
6915** the end of its result set, then fall through to the next instruction.
6916*/
drh9cbf3422008-01-17 16:22:13 +00006917case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006918 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006919 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006920 int res;
drh856c1032009-06-02 15:21:42 +00006921 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006922
drhc54a6172009-06-02 16:06:03 +00006923 res = 0;
drh856c1032009-06-02 15:21:42 +00006924 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006925 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00006926 if( pCur->nullRow ){
6927 break;
6928 }
drhc960dcb2015-11-20 19:22:01 +00006929 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006930 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006931 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006932
drhde4fcfd2008-01-19 23:50:26 +00006933 /* Invoke the xNext() method of the module. There is no way for the
6934 ** underlying implementation to return an error if one occurs during
6935 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6936 ** data is available) and the error code returned when xColumn or
6937 ** some other method is next invoked on the save virtual table cursor.
6938 */
drhc960dcb2015-11-20 19:22:01 +00006939 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00006940 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006941 if( rc ) goto abort_due_to_error;
6942 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00006943 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006944 if( !res ){
6945 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00006946 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00006947 }
drh49afe3a2013-07-10 03:05:14 +00006948 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006949}
6950#endif /* SQLITE_OMIT_VIRTUALTABLE */
6951
danielk1977182c4ba2007-06-27 15:53:34 +00006952#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006953/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006954**
drh66a51672008-01-03 00:01:23 +00006955** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006956** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006957** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006958*/
drh9cbf3422008-01-17 16:22:13 +00006959case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006960 sqlite3_vtab *pVtab;
6961 Mem *pName;
6962
danielk1977595a5232009-07-24 17:58:53 +00006963 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006964 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006965 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006966 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006967 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006968 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006969 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006970 testcase( pName->enc==SQLITE_UTF8 );
6971 testcase( pName->enc==SQLITE_UTF16BE );
6972 testcase( pName->enc==SQLITE_UTF16LE );
6973 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00006974 if( rc ) goto abort_due_to_error;
6975 rc = pVtab->pModule->xRename(pVtab, pName->z);
6976 sqlite3VtabImportErrmsg(p, pVtab);
6977 p->expired = 0;
6978 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00006979 break;
6980}
6981#endif
drh4cbdda92006-06-14 19:00:20 +00006982
6983#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006984/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006985** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006986**
drh66a51672008-01-03 00:01:23 +00006987** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006988** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006989** are contiguous memory cells starting at P3 to pass to the xUpdate
6990** invocation. The value in register (P3+P2-1) corresponds to the
6991** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006992**
6993** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006994** The argv[0] element (which corresponds to memory cell P3)
6995** is the rowid of a row to delete. If argv[0] is NULL then no
6996** deletion occurs. The argv[1] element is the rowid of the new
6997** row. This can be NULL to have the virtual table select the new
6998** rowid for itself. The subsequent elements in the array are
6999** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00007000**
7001** If P2==1 then no insert is performed. argv[0] is the rowid of
7002** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00007003**
7004** P1 is a boolean flag. If it is set to true and the xUpdate call
7005** is successful, then the value returned by sqlite3_last_insert_rowid()
7006** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00007007**
7008** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7009** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00007010*/
drh9cbf3422008-01-17 16:22:13 +00007011case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00007012 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007013 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007014 int nArg;
7015 int i;
7016 sqlite_int64 rowid;
7017 Mem **apArg;
7018 Mem *pX;
7019
danb061d052011-04-25 18:49:57 +00007020 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7021 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7022 );
drh9e92a472013-06-27 17:40:30 +00007023 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00007024 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00007025 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00007026 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007027 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7028 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007029 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007030 }
7031 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00007032 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00007033 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00007034 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00007035 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00007036 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00007037 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00007038 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00007039 assert( memIsValid(pX) );
7040 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00007041 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00007042 pX++;
danielk1977399918f2006-06-14 13:03:23 +00007043 }
danb061d052011-04-25 18:49:57 +00007044 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00007045 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00007046 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00007047 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00007048 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00007049 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00007050 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00007051 }
drhd91c1a12013-02-09 13:58:25 +00007052 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00007053 if( pOp->p5==OE_Ignore ){
7054 rc = SQLITE_OK;
7055 }else{
7056 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
7057 }
7058 }else{
7059 p->nChange++;
7060 }
drh9467abf2016-02-17 18:44:11 +00007061 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00007062 }
drh4cbdda92006-06-14 19:00:20 +00007063 break;
danielk1977399918f2006-06-14 13:03:23 +00007064}
7065#endif /* SQLITE_OMIT_VIRTUALTABLE */
7066
danielk197759a93792008-05-15 17:48:20 +00007067#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7068/* Opcode: Pagecount P1 P2 * * *
7069**
7070** Write the current number of pages in database P1 to memory cell P2.
7071*/
drh27a348c2015-04-13 19:14:06 +00007072case OP_Pagecount: { /* out2 */
7073 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00007074 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00007075 break;
7076}
7077#endif
7078
drh60ac3f42010-11-23 18:59:27 +00007079
7080#ifndef SQLITE_OMIT_PAGER_PRAGMAS
7081/* Opcode: MaxPgcnt P1 P2 P3 * *
7082**
7083** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00007084** Do not let the maximum page count fall below the current page count and
7085** do not change the maximum page count value if P3==0.
7086**
drh60ac3f42010-11-23 18:59:27 +00007087** Store the maximum page count after the change in register P2.
7088*/
drh27a348c2015-04-13 19:14:06 +00007089case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00007090 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00007091 Btree *pBt;
7092
drh27a348c2015-04-13 19:14:06 +00007093 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00007094 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00007095 newMax = 0;
7096 if( pOp->p3 ){
7097 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00007098 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00007099 }
7100 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00007101 break;
7102}
7103#endif
7104
drh3e34eab2017-07-19 19:48:40 +00007105/* Opcode: Function0 P1 P2 P3 P4 P5
7106** Synopsis: r[P3]=func(r[P2@P5])
7107**
7108** Invoke a user function (P4 is a pointer to a FuncDef object that
7109** defines the function) with P5 arguments taken from register P2 and
7110** successors. The result of the function is stored in register P3.
7111** Register P3 must not be one of the function inputs.
7112**
7113** P1 is a 32-bit bitmask indicating whether or not each argument to the
7114** function was determined to be constant at compile time. If the first
7115** argument was constant then bit 0 of P1 is set. This is used to determine
7116** whether meta data associated with a user function argument using the
7117** sqlite3_set_auxdata() API may be safely retained until the next
7118** invocation of this opcode.
7119**
7120** See also: Function, AggStep, AggFinal
7121*/
7122/* Opcode: Function P1 P2 P3 P4 P5
7123** Synopsis: r[P3]=func(r[P2@P5])
7124**
7125** Invoke a user function (P4 is a pointer to an sqlite3_context object that
7126** contains a pointer to the function to be run) with P5 arguments taken
7127** from register P2 and successors. The result of the function is stored
7128** in register P3. Register P3 must not be one of the function inputs.
7129**
7130** P1 is a 32-bit bitmask indicating whether or not each argument to the
7131** function was determined to be constant at compile time. If the first
7132** argument was constant then bit 0 of P1 is set. This is used to determine
7133** whether meta data associated with a user function argument using the
7134** sqlite3_set_auxdata() API may be safely retained until the next
7135** invocation of this opcode.
7136**
7137** SQL functions are initially coded as OP_Function0 with P4 pointing
7138** to a FuncDef object. But on first evaluation, the P4 operand is
7139** automatically converted into an sqlite3_context object and the operation
7140** changed to this OP_Function opcode. In this way, the initialization of
7141** the sqlite3_context object occurs only once, rather than once for each
7142** evaluation of the function.
7143**
7144** See also: Function0, AggStep, AggFinal
7145*/
7146case OP_PureFunc0:
7147case OP_Function0: {
7148 int n;
7149 sqlite3_context *pCtx;
7150
7151 assert( pOp->p4type==P4_FUNCDEF );
7152 n = pOp->p5;
7153 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7154 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
7155 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
7156 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7157 if( pCtx==0 ) goto no_mem;
7158 pCtx->pOut = 0;
7159 pCtx->pFunc = pOp->p4.pFunc;
7160 pCtx->iOp = (int)(pOp - aOp);
7161 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007162 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007163 pCtx->argc = n;
7164 pOp->p4type = P4_FUNCCTX;
7165 pOp->p4.pCtx = pCtx;
7166 assert( OP_PureFunc == OP_PureFunc0+2 );
7167 assert( OP_Function == OP_Function0+2 );
7168 pOp->opcode += 2;
7169 /* Fall through into OP_Function */
7170}
7171case OP_PureFunc:
7172case OP_Function: {
7173 int i;
7174 sqlite3_context *pCtx;
7175
7176 assert( pOp->p4type==P4_FUNCCTX );
7177 pCtx = pOp->p4.pCtx;
7178
7179 /* If this function is inside of a trigger, the register array in aMem[]
7180 ** might change from one evaluation to the next. The next block of code
7181 ** checks to see if the register array has changed, and if so it
7182 ** reinitializes the relavant parts of the sqlite3_context object */
7183 pOut = &aMem[pOp->p3];
7184 if( pCtx->pOut != pOut ){
7185 pCtx->pOut = pOut;
7186 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7187 }
7188
7189 memAboutToChange(p, pOut);
7190#ifdef SQLITE_DEBUG
7191 for(i=0; i<pCtx->argc; i++){
7192 assert( memIsValid(pCtx->argv[i]) );
7193 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7194 }
7195#endif
7196 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00007197 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00007198 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7199
7200 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00007201 if( pCtx->isError ){
7202 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00007203 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7204 rc = pCtx->isError;
7205 }
7206 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00007207 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00007208 if( rc ) goto abort_due_to_error;
7209 }
7210
7211 /* Copy the result of the function into register P3 */
7212 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7213 sqlite3VdbeChangeEncoding(pOut, encoding);
7214 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7215 }
7216
7217 REGISTER_TRACE(pOp->p3, pOut);
7218 UPDATE_MAX_BLOBSIZE(pOut);
7219 break;
7220}
7221
drhf259df52017-12-27 20:38:35 +00007222/* Opcode: Trace P1 P2 * P4 *
7223**
7224** Write P4 on the statement trace output if statement tracing is
7225** enabled.
7226**
7227** Operand P1 must be 0x7fffffff and P2 must positive.
7228*/
drh74588ce2017-09-13 00:13:05 +00007229/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007230** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007231**
7232** Programs contain a single instance of this opcode as the very first
7233** opcode.
drh949f9cd2008-01-12 21:35:57 +00007234**
7235** If tracing is enabled (by the sqlite3_trace()) interface, then
7236** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007237** Or if P4 is blank, use the string returned by sqlite3_sql().
7238**
7239** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007240**
7241** Increment the value of P1 so that OP_Once opcodes will jump the
7242** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007243**
7244** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7245** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007246*/
drhf259df52017-12-27 20:38:35 +00007247case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007248case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00007249 int i;
drhb9f47992018-01-24 12:14:43 +00007250#ifndef SQLITE_OMIT_TRACE
7251 char *zTrace;
7252#endif
drh5fe63bf2016-07-25 02:42:22 +00007253
7254 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7255 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7256 **
7257 ** This assert() provides evidence for:
7258 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7259 ** would have been returned by the legacy sqlite3_trace() interface by
7260 ** using the X argument when X begins with "--" and invoking
7261 ** sqlite3_expanded_sql(P) otherwise.
7262 */
7263 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007264
7265 /* OP_Init is always instruction 0 */
7266 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007267
drhaceb31b2014-02-08 01:40:27 +00007268#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007269 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007270 && !p->doingRerun
7271 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7272 ){
drh3d2a5292016-07-13 22:55:01 +00007273#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007274 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7275 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007276 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007277 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007278 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007279 }else
drh3d2a5292016-07-13 22:55:01 +00007280#endif
drh7adbcff2017-03-20 15:29:28 +00007281 if( db->nVdbeExec>1 ){
7282 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7283 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7284 sqlite3DbFree(db, z);
7285 }else{
drhbd441f72016-07-25 02:31:48 +00007286 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007287 }
drh949f9cd2008-01-12 21:35:57 +00007288 }
drh8f8b2312013-10-18 20:03:43 +00007289#ifdef SQLITE_USE_FCNTL_TRACE
7290 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7291 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007292 int j;
7293 for(j=0; j<db->nDb; j++){
7294 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7295 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007296 }
7297 }
7298#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007299#ifdef SQLITE_DEBUG
7300 if( (db->flags & SQLITE_SqlTrace)!=0
7301 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7302 ){
7303 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7304 }
7305#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007306#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007307 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007308 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007309 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007310 for(i=1; i<p->nOp; i++){
7311 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7312 }
7313 pOp->p1 = 0;
7314 }
7315 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007316 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007317 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007318}
drh949f9cd2008-01-12 21:35:57 +00007319
drh28935362013-12-07 20:39:19 +00007320#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007321/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007322**
7323** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007324** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7325** to values currently held in registers. TK_COLUMN terms in the P4
7326** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007327*/
7328case OP_CursorHint: {
7329 VdbeCursor *pC;
7330
7331 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7332 assert( pOp->p4type==P4_EXPR );
7333 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007334 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007335 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007336 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7337 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007338 }
drh28935362013-12-07 20:39:19 +00007339 break;
7340}
7341#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007342
drh4031baf2018-05-28 17:31:20 +00007343#ifdef SQLITE_DEBUG
7344/* Opcode: Abortable * * * * *
7345**
7346** Verify that an Abort can happen. Assert if an Abort at this point
7347** might cause database corruption. This opcode only appears in debugging
7348** builds.
7349**
7350** An Abort is safe if either there have been no writes, or if there is
7351** an active statement journal.
7352*/
7353case OP_Abortable: {
7354 sqlite3VdbeAssertAbortable(p);
7355 break;
7356}
7357#endif
7358
drh299bf7c2018-06-11 17:35:02 +00007359#ifdef SQLITE_DEBUG_COLUMNCACHE
7360/* Opcode: SetTabCol P1 P2 P3 * *
7361**
7362** Set a flag in register REG[P3] indicating that it holds the value
7363** of column P2 from the table on cursor P1. This flag is checked
7364** by a subsequent VerifyTabCol opcode.
7365**
7366** This opcode only appears SQLITE_DEBUG builds. It is used to verify
7367** that the expression table column cache is working correctly.
7368*/
7369case OP_SetTabCol: {
7370 aMem[pOp->p3].iTabColHash = TableColumnHash(pOp->p1,pOp->p2);
7371 break;
7372}
7373/* Opcode: VerifyTabCol P1 P2 P3 * *
7374**
7375** Verify that register REG[P3] contains the value of column P2 from
7376** cursor P1. Assert() if this is not the case.
7377**
7378** This opcode only appears SQLITE_DEBUG builds. It is used to verify
7379** that the expression table column cache is working correctly.
7380*/
7381case OP_VerifyTabCol: {
7382 assert( aMem[pOp->p3].iTabColHash == TableColumnHash(pOp->p1,pOp->p2) );
7383 break;
7384}
7385#endif
7386
drh91fd4d42008-01-19 20:11:25 +00007387/* Opcode: Noop * * * * *
7388**
7389** Do nothing. This instruction is often useful as a jump
7390** destination.
drh5e00f6c2001-09-13 13:46:56 +00007391*/
drh91fd4d42008-01-19 20:11:25 +00007392/*
7393** The magic Explain opcode are only inserted when explain==2 (which
7394** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7395** This opcode records information from the optimizer. It is the
7396** the same as a no-op. This opcodesnever appears in a real VM program.
7397*/
drh4031baf2018-05-28 17:31:20 +00007398default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00007399 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00007400
drh5e00f6c2001-09-13 13:46:56 +00007401 break;
7402}
7403
7404/*****************************************************************************
7405** The cases of the switch statement above this line should all be indented
7406** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7407** readability. From this point on down, the normal indentation rules are
7408** restored.
7409*****************************************************************************/
7410 }
drh6e142f52000-06-08 13:36:40 +00007411
drh7b396862003-01-01 23:06:20 +00007412#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007413 {
drh35043cc2018-02-12 20:27:34 +00007414 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007415 if( endTime>start ) pOrigOp->cycles += endTime - start;
7416 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007417 }
drh7b396862003-01-01 23:06:20 +00007418#endif
7419
drh6e142f52000-06-08 13:36:40 +00007420 /* The following code adds nothing to the actual functionality
7421 ** of the program. It is only here for testing and debugging.
7422 ** On the other hand, it does burn CPU cycles every time through
7423 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7424 */
7425#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007426 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007427
drhcf1023c2007-05-08 20:59:49 +00007428#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007429 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007430 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007431 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007432 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007433 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007434 }
drh7cc84c22016-04-11 13:36:42 +00007435 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007436 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007437 }
drh75897232000-05-29 14:26:00 +00007438 }
danielk1977b5402fb2005-01-12 07:15:04 +00007439#endif /* SQLITE_DEBUG */
7440#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007441 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007442
drha05a7222008-01-19 03:35:58 +00007443 /* If we reach this point, it means that execution is finished with
7444 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007445 */
drh9467abf2016-02-17 18:44:11 +00007446abort_due_to_error:
7447 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007448 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007449 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7450 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7451 }
drha05a7222008-01-19 03:35:58 +00007452 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007453 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007454 testcase( sqlite3GlobalConfig.xLog!=0 );
7455 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007456 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007457 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007458 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007459 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007460 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007461 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007462 }
drh900b31e2007-08-28 02:27:51 +00007463
7464 /* This is the only way out of this procedure. We have to
7465 ** release the mutexes on btrees that were acquired at the
7466 ** top. */
7467vdbe_return:
drh77dfd5b2013-08-19 11:15:48 +00007468 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00007469 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007470 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007471 assert( rc!=SQLITE_OK || nExtraDelete==0
7472 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7473 );
drhb86ccfb2003-01-28 23:13:10 +00007474 return rc;
7475
drh023ae032007-05-08 12:12:16 +00007476 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7477 ** is encountered.
7478 */
7479too_big:
drh22c17b82015-05-15 04:13:15 +00007480 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007481 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007482 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007483
drh98640a32007-06-07 19:08:32 +00007484 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007485 */
7486no_mem:
drh4a642b62016-02-05 01:55:27 +00007487 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007488 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007489 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007490 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007491
danielk19776f8a5032004-05-10 10:34:51 +00007492 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007493 ** flag.
7494 */
7495abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007496 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007497 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007498 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007499 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007500 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007501}