blob: 4d643d7266859f982b164a32771e7e324ade0dc2 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drh0fd61352014-02-07 02:29:45 +000012** The code in this file implements the function that runs the
13** bytecode of a prepared statement.
drh75897232000-05-29 14:26:00 +000014**
drhac82fcf2002-09-08 17:23:41 +000015** Various scripts scan this source file in order to generate HTML
16** documentation, headers files, or other derived files. The formatting
17** of the code in this file is, therefore, important. See other comments
18** in this file for details. If in doubt, do not deviate from existing
19** commenting and indentation practices when changing or adding code.
drh75897232000-05-29 14:26:00 +000020*/
21#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000022#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000023
24/*
drh2b4ded92010-09-27 21:09:31 +000025** Invoke this macro on memory cells just prior to changing the
26** value of the cell. This macro verifies that shallow copies are
drh0fd61352014-02-07 02:29:45 +000027** not misused. A shallow copy of a string or blob just copies a
28** pointer to the string or blob, not the content. If the original
29** is changed while the copy is still in use, the string or blob might
30** be changed out from under the copy. This macro verifies that nothing
drhb6e8fd12014-03-06 01:56:33 +000031** like that ever happens.
drh2b4ded92010-09-27 21:09:31 +000032*/
33#ifdef SQLITE_DEBUG
drhe4c88c02012-01-04 12:57:45 +000034# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
drh2b4ded92010-09-27 21:09:31 +000035#else
36# define memAboutToChange(P,M)
37#endif
38
39/*
drh487ab3c2001-11-08 00:45:21 +000040** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000041** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000042** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000043** working correctly. This variable has no function other than to
44** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000045*/
drh0f7eb612006-08-08 13:51:43 +000046#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000047int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000048#endif
drh487ab3c2001-11-08 00:45:21 +000049
drhf6038712004-02-08 18:07:34 +000050/*
51** When this global variable is positive, it gets decremented once before
drhe4c88c02012-01-04 12:57:45 +000052** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
53** field of the sqlite3 structure is set in order to simulate an interrupt.
drhf6038712004-02-08 18:07:34 +000054**
55** This facility is used for testing purposes only. It does not function
56** in an ordinary build.
57*/
drh0f7eb612006-08-08 13:51:43 +000058#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000060#endif
drh1350b032002-02-27 19:00:20 +000061
danielk19777e18c252004-05-25 11:47:24 +000062/*
drh6bf89572004-11-03 16:27:01 +000063** The next global variable is incremented each type the OP_Sort opcode
64** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000065** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000066** has no function other than to help verify the correct operation of the
67** library.
68*/
drh0f7eb612006-08-08 13:51:43 +000069#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000070int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000071#endif
drh6bf89572004-11-03 16:27:01 +000072
73/*
drhae7e1512007-05-02 16:51:59 +000074** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000075** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000076** use this information to make sure that the zero-blob functionality
77** is working correctly. This variable has no function other than to
78** help verify the correct operation of the library.
79*/
80#ifdef SQLITE_TEST
81int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000082static void updateMaxBlobsize(Mem *p){
83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
84 sqlite3_max_blobsize = p->n;
85 }
86}
drhae7e1512007-05-02 16:51:59 +000087#endif
88
89/*
drh9b1c62d2011-03-30 21:04:43 +000090** This macro evaluates to true if either the update hook or the preupdate
91** hook are enabled for database connect DB.
92*/
93#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh74c33022016-03-30 12:56:55 +000094# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000095#else
drh74c33022016-03-30 12:56:55 +000096# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000097#endif
98
99/*
drh0fd61352014-02-07 02:29:45 +0000100** The next global variable is incremented each time the OP_Found opcode
dan0ff297e2009-09-25 17:03:14 +0000101** is executed. This is used to test whether or not the foreign key
102** operation implemented using OP_FkIsZero is working. This variable
103** has no function other than to help verify the correct operation of the
104** library.
105*/
106#ifdef SQLITE_TEST
107int sqlite3_found_count = 0;
108#endif
109
110/*
drhb7654112008-01-12 12:48:07 +0000111** Test a register to see if it exceeds the current maximum blob size.
112** If it does, record the new maximum blob size.
113*/
drhd12602a2016-12-07 15:49:02 +0000114#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
drhca48c902008-01-18 14:08:24 +0000115# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000116#else
117# define UPDATE_MAX_BLOBSIZE(P)
118#endif
119
120/*
drh5655c542014-02-19 19:14:34 +0000121** Invoke the VDBE coverage callback, if that callback is defined. This
122** feature is used for test suite validation only and does not appear an
123** production builds.
124**
125** M is an integer, 2 or 3, that indices how many different ways the
126** branch can go. It is usually 2. "I" is the direction the branch
127** goes. 0 means falls through. 1 means branch is taken. 2 means the
128** second alternative branch is taken.
drh4336b0e2014-08-05 00:53:51 +0000129**
130** iSrcLine is the source code line (from the __LINE__ macro) that
131** generated the VDBE instruction. This instrumentation assumes that all
132** source code is in a single file (the amalgamation). Special values 1
133** and 2 for the iSrcLine parameter mean that this particular branch is
134** always taken or never taken, respectively.
drh688852a2014-02-17 22:40:43 +0000135*/
136#if !defined(SQLITE_VDBE_COVERAGE)
137# define VdbeBranchTaken(I,M)
138#else
drh5655c542014-02-19 19:14:34 +0000139# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
140 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
141 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
142 M = iSrcLine;
143 /* Assert the truth of VdbeCoverageAlwaysTaken() and
144 ** VdbeCoverageNeverTaken() */
145 assert( (M & I)==I );
146 }else{
147 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
148 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
149 iSrcLine,I,M);
150 }
151 }
drh688852a2014-02-17 22:40:43 +0000152#endif
153
154/*
drh9cbf3422008-01-17 16:22:13 +0000155** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000156** already. Return non-zero if a malloc() fails.
157*/
drhb21c8cd2007-08-21 19:33:56 +0000158#define Stringify(P, enc) \
drhbd9507c2014-08-23 17:21:37 +0000159 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
drhf4479502004-05-27 03:12:53 +0000160 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000161
162/*
danielk1977bd7e4602004-05-24 07:34:48 +0000163** An ephemeral string value (signified by the MEM_Ephem flag) contains
164** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000165** is responsible for deallocating that string. Because the register
166** does not control the string, it might be deleted without the register
167** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000168**
169** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000170** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000171** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000172*/
drhb21c8cd2007-08-21 19:33:56 +0000173#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000174 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000175 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000176
dan689ab892011-08-12 15:02:00 +0000177/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000178#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000179
180/*
drhdfe88ec2008-11-03 20:55:06 +0000181** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000182** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000183*/
drhdfe88ec2008-11-03 20:55:06 +0000184static VdbeCursor *allocateCursor(
185 Vdbe *p, /* The virtual machine */
186 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000187 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000188 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000189 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000190){
191 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000192 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000193 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000194 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000195 **
196 ** * Sometimes cursor numbers are used for a couple of different
197 ** purposes in a vdbe program. The different uses might require
198 ** different sized allocations. Memory cells provide growable
199 ** allocations.
200 **
201 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
202 ** be freed lazily via the sqlite3_release_memory() API. This
203 ** minimizes the number of malloc calls made by the system.
204 **
drh3cdce922016-03-21 00:30:40 +0000205 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000206 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
207 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000208 */
drh9f6168b2016-03-19 23:32:58 +0000209 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000210
danielk19775f096132008-03-28 15:44:09 +0000211 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000212 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000213 nByte =
drh5cc10232013-11-21 01:04:02 +0000214 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000215 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000216
drh9f6168b2016-03-19 23:32:58 +0000217 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000218 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977be718892006-06-23 08:05:19 +0000219 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000220 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000221 }
drh322f2852014-09-19 00:43:39 +0000222 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000223 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhfbd8cbd2016-12-10 12:58:15 +0000224 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
drhc960dcb2015-11-20 19:22:01 +0000225 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000226 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000227 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000228 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000229 if( eCurType==CURTYPE_BTREE ){
230 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000231 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000232 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000233 }
danielk197794eb6a12005-12-15 15:22:08 +0000234 }
drh4774b132004-06-12 20:12:51 +0000235 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000236}
237
danielk19773d1bfea2004-05-14 11:00:53 +0000238/*
drh29d72102006-02-09 22:13:41 +0000239** Try to convert a value into a numeric representation if we can
240** do so without loss of information. In other words, if the string
241** looks like a number, convert it into a number. If it does not
242** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000243**
244** If the bTryForInt flag is true, then extra effort is made to give
245** an integer representation. Strings that look like floating point
246** values but which have no fractional component (example: '48.00')
247** will have a MEM_Int representation when bTryForInt is true.
248**
249** If bTryForInt is false, then if the input string contains a decimal
250** point or exponential notation, the result is only MEM_Real, even
251** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000252*/
drhbd9507c2014-08-23 17:21:37 +0000253static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000254 double rValue;
255 i64 iValue;
256 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000257 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000258 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
259 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
260 pRec->u.i = iValue;
261 pRec->flags |= MEM_Int;
262 }else{
drh74eaba42014-09-18 17:52:15 +0000263 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000264 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000265 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000266 }
267}
268
269/*
drh8a512562005-11-14 22:29:05 +0000270** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000271**
drh8a512562005-11-14 22:29:05 +0000272** SQLITE_AFF_INTEGER:
273** SQLITE_AFF_REAL:
274** SQLITE_AFF_NUMERIC:
275** Try to convert pRec to an integer representation or a
276** floating-point representation if an integer representation
277** is not possible. Note that the integer representation is
278** always preferred, even if the affinity is REAL, because
279** an integer representation is more space efficient on disk.
280**
281** SQLITE_AFF_TEXT:
282** Convert pRec to a text representation.
283**
drh05883a32015-06-02 15:32:08 +0000284** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000285** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000286*/
drh17435752007-08-16 04:30:38 +0000287static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000288 Mem *pRec, /* The value to apply affinity to */
289 char affinity, /* The affinity to be applied */
290 u8 enc /* Use this text encoding */
291){
drh7ea31cc2014-09-18 14:36:00 +0000292 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000293 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
294 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000295 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000296 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000297 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000298 }else{
299 sqlite3VdbeIntegerAffinity(pRec);
300 }
drh17c40292004-07-21 02:53:29 +0000301 }
drh7ea31cc2014-09-18 14:36:00 +0000302 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000303 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000304 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000305 ** representation. It would be harmless to repeat the conversion if
306 ** there is already a string rep, but it is pointless to waste those
307 ** CPU cycles. */
308 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
309 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
310 sqlite3VdbeMemStringify(pRec, enc, 1);
311 }
danielk19773d1bfea2004-05-14 11:00:53 +0000312 }
dandde548c2015-05-19 19:44:25 +0000313 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000314 }
315}
316
danielk1977aee18ef2005-03-09 12:26:50 +0000317/*
drh29d72102006-02-09 22:13:41 +0000318** Try to convert the type of a function argument or a result column
319** into a numeric representation. Use either INTEGER or REAL whichever
320** is appropriate. But only do the conversion if it is possible without
321** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000322*/
323int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000324 int eType = sqlite3_value_type(pVal);
325 if( eType==SQLITE_TEXT ){
326 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000327 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000328 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000329 }
drh1b27b8c2014-02-10 03:21:57 +0000330 return eType;
drh29d72102006-02-09 22:13:41 +0000331}
332
333/*
danielk1977aee18ef2005-03-09 12:26:50 +0000334** Exported version of applyAffinity(). This one works on sqlite3_value*,
335** not the internal Mem* type.
336*/
danielk19771e536952007-08-16 10:09:01 +0000337void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000338 sqlite3_value *pVal,
339 u8 affinity,
340 u8 enc
341){
drhb21c8cd2007-08-21 19:33:56 +0000342 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000343}
344
drh3d1d90a2014-03-24 15:00:15 +0000345/*
drhf1a89ed2014-08-23 17:41:15 +0000346** pMem currently only holds a string type (or maybe a BLOB that we can
347** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000348** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000349** accordingly.
350*/
351static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
352 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
353 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000354 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000355 return 0;
356 }
drh84d4f1a2017-09-20 10:47:10 +0000357 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000358 return MEM_Int;
359 }
360 return MEM_Real;
361}
362
363/*
drh3d1d90a2014-03-24 15:00:15 +0000364** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
365** none.
366**
367** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000368** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000369*/
370static u16 numericType(Mem *pMem){
371 if( pMem->flags & (MEM_Int|MEM_Real) ){
372 return pMem->flags & (MEM_Int|MEM_Real);
373 }
374 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000375 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000376 }
377 return 0;
378}
379
danielk1977b5402fb2005-01-12 07:15:04 +0000380#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000381/*
danielk1977ca6b2912004-05-21 10:49:47 +0000382** Write a nice string representation of the contents of cell pMem
383** into buffer zBuf, length nBuf.
384*/
drh74161702006-02-24 02:53:49 +0000385void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000386 char *zCsr = zBuf;
387 int f = pMem->flags;
388
drh57196282004-10-06 15:41:16 +0000389 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000390
danielk1977ca6b2912004-05-21 10:49:47 +0000391 if( f&MEM_Blob ){
392 int i;
393 char c;
394 if( f & MEM_Dyn ){
395 c = 'z';
396 assert( (f & (MEM_Static|MEM_Ephem))==0 );
397 }else if( f & MEM_Static ){
398 c = 't';
399 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
400 }else if( f & MEM_Ephem ){
401 c = 'e';
402 assert( (f & (MEM_Static|MEM_Dyn))==0 );
403 }else{
404 c = 's';
405 }
drh85c2dc02017-03-16 13:30:58 +0000406 *(zCsr++) = c;
drh5bb3eb92007-05-04 13:15:55 +0000407 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000408 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000409 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000410 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000411 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000412 }
413 for(i=0; i<16 && i<pMem->n; i++){
414 char z = pMem->z[i];
415 if( z<32 || z>126 ) *zCsr++ = '.';
416 else *zCsr++ = z;
417 }
drh85c2dc02017-03-16 13:30:58 +0000418 *(zCsr++) = ']';
drhfdf972a2007-05-02 13:30:27 +0000419 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000420 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000421 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000422 }
danielk1977b1bc9532004-05-22 03:05:33 +0000423 *zCsr = '\0';
424 }else if( f & MEM_Str ){
425 int j, k;
426 zBuf[0] = ' ';
427 if( f & MEM_Dyn ){
428 zBuf[1] = 'z';
429 assert( (f & (MEM_Static|MEM_Ephem))==0 );
430 }else if( f & MEM_Static ){
431 zBuf[1] = 't';
432 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
433 }else if( f & MEM_Ephem ){
434 zBuf[1] = 'e';
435 assert( (f & (MEM_Static|MEM_Dyn))==0 );
436 }else{
437 zBuf[1] = 's';
438 }
439 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000440 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000441 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000442 zBuf[k++] = '[';
443 for(j=0; j<15 && j<pMem->n; j++){
444 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000445 if( c>=0x20 && c<0x7f ){
446 zBuf[k++] = c;
447 }else{
448 zBuf[k++] = '.';
449 }
450 }
451 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000452 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000453 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000454 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000455 }
danielk1977ca6b2912004-05-21 10:49:47 +0000456}
457#endif
458
drh5b6afba2008-01-05 16:29:28 +0000459#ifdef SQLITE_DEBUG
460/*
461** Print the value of a register for tracing purposes:
462*/
drh84e55a82013-11-13 17:58:23 +0000463static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000464 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000465 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000466 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000467 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000468 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000469 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000470 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000471 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000472#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000473 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000474 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000475#endif
drh733bf1b2009-04-22 00:47:00 +0000476 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000477 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000478 }else{
479 char zBuf[200];
480 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000481 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000482 }
dan5b6c8e42016-01-30 15:46:03 +0000483 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000484}
drh84e55a82013-11-13 17:58:23 +0000485static void registerTrace(int iReg, Mem *p){
486 printf("REG[%d] = ", iReg);
487 memTracePrint(p);
488 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000489 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000490}
491#endif
492
493#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000494# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000495#else
496# define REGISTER_TRACE(R,M)
497#endif
498
danielk197784ac9d02004-05-18 09:58:06 +0000499
drh7b396862003-01-01 23:06:20 +0000500#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000501
502/*
503** hwtime.h contains inline assembler code for implementing
504** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000505*/
shane9bcbdad2008-05-29 20:22:37 +0000506#include "hwtime.h"
507
drh7b396862003-01-01 23:06:20 +0000508#endif
509
danielk1977fd7f0452008-12-17 17:30:26 +0000510#ifndef NDEBUG
511/*
512** This function is only called from within an assert() expression. It
513** checks that the sqlite3.nTransaction variable is correctly set to
514** the number of non-transaction savepoints currently in the
515** linked list starting at sqlite3.pSavepoint.
516**
517** Usage:
518**
519** assert( checkSavepointCount(db) );
520*/
521static int checkSavepointCount(sqlite3 *db){
522 int n = 0;
523 Savepoint *p;
524 for(p=db->pSavepoint; p; p=p->pNext) n++;
525 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
526 return 1;
527}
528#endif
529
drh27a348c2015-04-13 19:14:06 +0000530/*
531** Return the register of pOp->p2 after first preparing it to be
532** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000533*/
534static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
535 sqlite3VdbeMemSetNull(pOut);
536 pOut->flags = MEM_Int;
537 return pOut;
538}
drh27a348c2015-04-13 19:14:06 +0000539static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
540 Mem *pOut;
541 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000542 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000543 pOut = &p->aMem[pOp->p2];
544 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000545 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000546 return out2PrereleaseWithClear(pOut);
547 }else{
548 pOut->flags = MEM_Int;
549 return pOut;
550 }
drh27a348c2015-04-13 19:14:06 +0000551}
552
drhb9755982010-07-24 16:34:37 +0000553
554/*
drh0fd61352014-02-07 02:29:45 +0000555** Execute as much of a VDBE program as we can.
556** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000557*/
danielk19774adee202004-05-08 08:23:19 +0000558int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000559 Vdbe *p /* The VDBE */
560){
drhbbe879d2009-11-14 18:04:35 +0000561 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000562 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000563#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
564 Op *pOrigOp; /* Value of pOp at the top of the loop */
565#endif
drhb89aeb62016-01-27 15:49:32 +0000566#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000567 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000568#endif
drhb86ccfb2003-01-28 23:13:10 +0000569 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000570 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000571 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000572 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000573 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000574 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000575#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh2ab792e2017-05-30 18:34:07 +0000576 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000577#endif
drha6c2ed92009-11-14 23:22:23 +0000578 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000579 Mem *pIn1 = 0; /* 1st input operand */
580 Mem *pIn2 = 0; /* 2nd input operand */
581 Mem *pIn3 = 0; /* 3rd input operand */
582 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000583#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000584 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000585#endif
drh856c1032009-06-02 15:21:42 +0000586 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000587
drhca48c902008-01-18 14:08:24 +0000588 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000589 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000590 if( p->rc==SQLITE_NOMEM ){
591 /* This happens if a malloc() inside a call to sqlite3_column_text() or
592 ** sqlite3_column_text16() failed. */
593 goto no_mem;
594 }
drhcbd8db32015-08-20 17:18:32 +0000595 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000596 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000597 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000598 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000599 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000600 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000601 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000602 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000603#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
604 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000605 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000606 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000607 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh2ab792e2017-05-30 18:34:07 +0000608 }else{
609 nProgressLimit = 0xffffffff;
drh0d1961e2013-07-25 16:27:51 +0000610 }
611#endif
drh3c23a882007-01-09 14:01:13 +0000612#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000613 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000614 if( p->pc==0
615 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
616 ){
drh3c23a882007-01-09 14:01:13 +0000617 int i;
drh84e55a82013-11-13 17:58:23 +0000618 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000619 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000620 if( p->db->flags & SQLITE_VdbeListing ){
621 printf("VDBE Program Listing:\n");
622 for(i=0; i<p->nOp; i++){
623 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
624 }
drh3c23a882007-01-09 14:01:13 +0000625 }
drh84e55a82013-11-13 17:58:23 +0000626 if( p->db->flags & SQLITE_VdbeEQP ){
627 for(i=0; i<p->nOp; i++){
628 if( aOp[i].opcode==OP_Explain ){
629 if( once ) printf("VDBE Query Plan:\n");
630 printf("%s\n", aOp[i].p4.z);
631 once = 0;
632 }
633 }
634 }
635 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000636 }
danielk19772d1d86f2008-06-20 14:59:51 +0000637 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000638#endif
drh9467abf2016-02-17 18:44:11 +0000639 for(pOp=&aOp[p->pc]; 1; pOp++){
640 /* Errors are detected by individual opcodes, with an immediate
641 ** jumps to abort_due_to_error. */
642 assert( rc==SQLITE_OK );
643
drhf56fa462015-04-13 21:39:54 +0000644 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000645#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000646 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000647#endif
drhbf159fa2013-06-25 22:01:22 +0000648 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000649#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000650 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000651#endif
drh6e142f52000-06-08 13:36:40 +0000652
danielk19778b60e0f2005-01-12 09:10:39 +0000653 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000654 */
danielk19778b60e0f2005-01-12 09:10:39 +0000655#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000656 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000657 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000658 }
drh3f7d4e42004-07-24 14:35:58 +0000659#endif
660
drh6e142f52000-06-08 13:36:40 +0000661
drhf6038712004-02-08 18:07:34 +0000662 /* Check to see if we need to simulate an interrupt. This only happens
663 ** if we have a special test build.
664 */
665#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000666 if( sqlite3_interrupt_count>0 ){
667 sqlite3_interrupt_count--;
668 if( sqlite3_interrupt_count==0 ){
669 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000670 }
671 }
672#endif
673
drh3c657212009-11-17 23:59:58 +0000674 /* Sanity checking on other operands */
675#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000676 {
677 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
678 if( (opProperty & OPFLG_IN1)!=0 ){
679 assert( pOp->p1>0 );
680 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
681 assert( memIsValid(&aMem[pOp->p1]) );
682 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
683 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
684 }
685 if( (opProperty & OPFLG_IN2)!=0 ){
686 assert( pOp->p2>0 );
687 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
688 assert( memIsValid(&aMem[pOp->p2]) );
689 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
690 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
691 }
692 if( (opProperty & OPFLG_IN3)!=0 ){
693 assert( pOp->p3>0 );
694 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
695 assert( memIsValid(&aMem[pOp->p3]) );
696 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
697 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
698 }
699 if( (opProperty & OPFLG_OUT2)!=0 ){
700 assert( pOp->p2>0 );
701 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
702 memAboutToChange(p, &aMem[pOp->p2]);
703 }
704 if( (opProperty & OPFLG_OUT3)!=0 ){
705 assert( pOp->p3>0 );
706 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
707 memAboutToChange(p, &aMem[pOp->p3]);
708 }
drh3c657212009-11-17 23:59:58 +0000709 }
710#endif
drh6dc41482015-04-16 17:31:02 +0000711#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
712 pOrigOp = pOp;
713#endif
drh93952eb2009-11-13 19:43:43 +0000714
drh75897232000-05-29 14:26:00 +0000715 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000716
drh5e00f6c2001-09-13 13:46:56 +0000717/*****************************************************************************
718** What follows is a massive switch statement where each case implements a
719** separate instruction in the virtual machine. If we follow the usual
720** indentation conventions, each case should be indented by 6 spaces. But
721** that is a lot of wasted space on the left margin. So the code within
722** the switch statement will break with convention and be flush-left. Another
723** big comment (similar to this one) will mark the point in the code where
724** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000725**
726** The formatting of each case is important. The makefile for SQLite
727** generates two C files "opcodes.h" and "opcodes.c" by scanning this
728** file looking for lines that begin with "case OP_". The opcodes.h files
729** will be filled with #defines that give unique integer values to each
730** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000731** each string is the symbolic name for the corresponding opcode. If the
732** case statement is followed by a comment of the form "/# same as ... #/"
733** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000734**
drh9cbf3422008-01-17 16:22:13 +0000735** Other keywords in the comment that follows each case are used to
736** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000737** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000738** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000739**
drhac82fcf2002-09-08 17:23:41 +0000740** Documentation about VDBE opcodes is generated by scanning this file
741** for lines of that contain "Opcode:". That line and all subsequent
742** comment lines are used in the generation of the opcode.html documentation
743** file.
744**
745** SUMMARY:
746**
747** Formatting is important to scripts that scan this file.
748** Do not deviate from the formatting style currently in use.
749**
drh5e00f6c2001-09-13 13:46:56 +0000750*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000751
drh9cbf3422008-01-17 16:22:13 +0000752/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000753**
754** An unconditional jump to address P2.
755** The next instruction executed will be
756** the one at index P2 from the beginning of
757** the program.
drhfe705102014-03-06 13:38:37 +0000758**
759** The P1 parameter is not actually used by this opcode. However, it
760** is sometimes set to 1 instead of 0 as a hint to the command-line shell
761** that this Goto is the bottom of a loop and that the lines from P2 down
762** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000763*/
drh9cbf3422008-01-17 16:22:13 +0000764case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000765jump_to_p2_and_check_for_interrupt:
766 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000767
768 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000769 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000770 ** completion. Check to see if sqlite3_interrupt() has been called
771 ** or if the progress callback needs to be invoked.
772 **
773 ** This code uses unstructured "goto" statements and does not look clean.
774 ** But that is not due to sloppy coding habits. The code is written this
775 ** way for performance, to avoid having to run the interrupt and progress
776 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
777 ** faster according to "valgrind --tool=cachegrind" */
778check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000779 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000780#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
781 /* Call the progress callback if it is configured and the required number
782 ** of VDBE ops have been executed (either since this invocation of
783 ** sqlite3VdbeExec() or since last time the progress callback was called).
784 ** If the progress callback returns non-zero, exit the virtual machine with
785 ** a return code SQLITE_ABORT.
786 */
drh2ab792e2017-05-30 18:34:07 +0000787 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000788 assert( db->nProgressOps!=0 );
789 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
790 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000791 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000792 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000793 }
drh49afe3a2013-07-10 03:05:14 +0000794 }
795#endif
796
drh5e00f6c2001-09-13 13:46:56 +0000797 break;
798}
drh75897232000-05-29 14:26:00 +0000799
drh2eb95372008-06-06 15:04:36 +0000800/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000801**
drh2eb95372008-06-06 15:04:36 +0000802** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000803** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000804*/
drhb8475df2011-12-09 16:21:19 +0000805case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000806 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000807 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000808 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000809 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000810 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000811 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000812 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000813
814 /* Most jump operations do a goto to this spot in order to update
815 ** the pOp pointer. */
816jump_to_p2:
817 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000818 break;
819}
820
drh2eb95372008-06-06 15:04:36 +0000821/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000822**
drh81cf13e2014-02-07 18:27:53 +0000823** Jump to the next instruction after the address in register P1. After
824** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000825*/
drh2eb95372008-06-06 15:04:36 +0000826case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000827 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000828 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000829 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000830 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000831 break;
832}
833
drhed71a832014-02-07 19:18:10 +0000834/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000835**
drh5dad9a32014-07-25 18:37:42 +0000836** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000837** located at address P3.
838**
drh5dad9a32014-07-25 18:37:42 +0000839** If P2!=0 then the coroutine implementation immediately follows
840** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000841** address P2.
drh5dad9a32014-07-25 18:37:42 +0000842**
843** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000844*/
845case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000846 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000847 assert( pOp->p2>=0 && pOp->p2<p->nOp );
848 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000849 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000850 assert( !VdbeMemDynamic(pOut) );
851 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000852 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000853 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000854 break;
855}
856
857/* Opcode: EndCoroutine P1 * * * *
858**
drhbc5cf382014-08-06 01:08:07 +0000859** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000860** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000861** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000862**
863** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000864*/
865case OP_EndCoroutine: { /* in1 */
866 VdbeOp *pCaller;
867 pIn1 = &aMem[pOp->p1];
868 assert( pIn1->flags==MEM_Int );
869 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
870 pCaller = &aOp[pIn1->u.i];
871 assert( pCaller->opcode==OP_Yield );
872 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000873 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000874 pIn1->flags = MEM_Undefined;
875 break;
876}
877
878/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000879**
drh5dad9a32014-07-25 18:37:42 +0000880** Swap the program counter with the value in register P1. This
881** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000882**
drh5dad9a32014-07-25 18:37:42 +0000883** If the coroutine that is launched by this instruction ends with
884** Yield or Return then continue to the next instruction. But if
885** the coroutine launched by this instruction ends with
886** EndCoroutine, then jump to P2 rather than continuing with the
887** next instruction.
888**
889** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000890*/
drh81cf13e2014-02-07 18:27:53 +0000891case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000892 int pcDest;
drh3c657212009-11-17 23:59:58 +0000893 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000894 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000895 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000896 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000897 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000898 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000899 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000900 break;
901}
902
drhf9c8ce32013-11-05 13:33:55 +0000903/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000904** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000905**
drhef8662b2011-06-20 21:47:58 +0000906** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000907** parameter P1, P2, and P4 as if this were a Halt instruction. If the
908** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000909** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000910*/
911case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000912 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000913 if( (pIn3->flags & MEM_Null)==0 ) break;
914 /* Fall through into OP_Halt */
915}
drhe00ee6e2008-06-20 15:24:01 +0000916
drhf9c8ce32013-11-05 13:33:55 +0000917/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000918**
drh3d4501e2008-12-04 20:40:10 +0000919** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000920** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000921**
drh92f02c32004-09-02 14:57:08 +0000922** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
923** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
924** For errors, it can be some other value. If P1!=0 then P2 will determine
925** whether or not to rollback the current transaction. Do not rollback
926** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
927** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000928** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000929**
drh66a51672008-01-03 00:01:23 +0000930** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000931**
drhf9c8ce32013-11-05 13:33:55 +0000932** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
933**
934** 0: (no change)
935** 1: NOT NULL contraint failed: P4
936** 2: UNIQUE constraint failed: P4
937** 3: CHECK constraint failed: P4
938** 4: FOREIGN KEY constraint failed: P4
939**
940** If P5 is not zero and P4 is NULL, then everything after the ":" is
941** omitted.
942**
drh9cfcf5d2002-01-29 18:41:24 +0000943** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000944** every program. So a jump past the last instruction of the program
945** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000946*/
drh9cbf3422008-01-17 16:22:13 +0000947case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +0000948 VdbeFrame *pFrame;
949 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000950
drhf56fa462015-04-13 21:39:54 +0000951 pcx = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +0000952 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000953 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000954 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000955 p->pFrame = pFrame->pParent;
956 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000957 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000958 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +0000959 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +0000960 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +0000961 ** currently being halted. If the p2 instruction of this OP_Halt
962 ** instruction is set to OE_Ignore, then the sub-program is throwing
963 ** an IGNORE exception. In this case jump to the address specified
964 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +0000965 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +0000966 }
drhbbe879d2009-11-14 18:04:35 +0000967 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000968 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +0000969 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +0000970 break;
971 }
drh92f02c32004-09-02 14:57:08 +0000972 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000973 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +0000974 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +0000975 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +0000976 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000977 if( pOp->p5 ){
978 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
979 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +0000980 testcase( pOp->p5==1 );
981 testcase( pOp->p5==2 );
982 testcase( pOp->p5==3 );
983 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +0000984 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
985 if( pOp->p4.z ){
986 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
987 }
drhd9b7ec92013-11-06 14:05:21 +0000988 }else{
drh22c17b82015-05-15 04:13:15 +0000989 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000990 }
drh99f5de72016-04-30 02:59:15 +0000991 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000992 }
drh92f02c32004-09-02 14:57:08 +0000993 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000994 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000995 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +0000996 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +0000997 }else{
drhd91c1a12013-02-09 13:58:25 +0000998 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +0000999 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001000 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001001 }
drh900b31e2007-08-28 02:27:51 +00001002 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001003}
drhc61053b2000-06-04 12:58:36 +00001004
drh4c583122008-01-04 22:01:03 +00001005/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001006** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001007**
drh9cbf3422008-01-17 16:22:13 +00001008** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001009*/
drh27a348c2015-04-13 19:14:06 +00001010case OP_Integer: { /* out2 */
1011 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001012 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001013 break;
1014}
1015
drh4c583122008-01-04 22:01:03 +00001016/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001017** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001018**
drh66a51672008-01-03 00:01:23 +00001019** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001020** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001021*/
drh27a348c2015-04-13 19:14:06 +00001022case OP_Int64: { /* out2 */
1023 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001024 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001025 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001026 break;
1027}
drh4f26d6c2004-05-26 23:25:30 +00001028
drh13573c72010-01-12 17:04:07 +00001029#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001030/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001031** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001032**
drh4c583122008-01-04 22:01:03 +00001033** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001034** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001035*/
drh27a348c2015-04-13 19:14:06 +00001036case OP_Real: { /* same as TK_FLOAT, out2 */
1037 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001038 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001039 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001040 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001041 break;
1042}
drh13573c72010-01-12 17:04:07 +00001043#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001044
drh3c84ddf2008-01-09 02:15:38 +00001045/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001046** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001047**
drh66a51672008-01-03 00:01:23 +00001048** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001049** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001050** this transformation, the length of string P4 is computed and stored
1051** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001052*/
drh27a348c2015-04-13 19:14:06 +00001053case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001054 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001055 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001056 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001057 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001058
1059#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001060 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001061 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001062 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drh4c583122008-01-04 22:01:03 +00001063 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001064 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001065 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001066 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001067 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001068 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001069 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001070 }
drh66a51672008-01-03 00:01:23 +00001071 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001072 pOp->p4.z = pOut->z;
1073 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001074 }
drh2f555112016-04-30 18:10:34 +00001075 testcase( rc==SQLITE_TOOBIG );
danielk197793758c82005-01-21 08:13:14 +00001076#endif
drhbb4957f2008-03-20 14:03:29 +00001077 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001078 goto too_big;
1079 }
drh2f555112016-04-30 18:10:34 +00001080 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001081 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001082}
drhf4479502004-05-27 03:12:53 +00001083
drhf07cf6e2015-03-06 16:45:16 +00001084/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001085** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001086**
drh9cbf3422008-01-17 16:22:13 +00001087** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001088**
drh44aebff2016-05-02 10:25:42 +00001089** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001090** the datatype of the register P2 is converted to BLOB. The content is
1091** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001092** of a string, as if it had been CAST. In other words:
1093**
1094** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001095*/
drh27a348c2015-04-13 19:14:06 +00001096case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001097 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001098 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001099 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1100 pOut->z = pOp->p4.z;
1101 pOut->n = pOp->p1;
1102 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001103 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001104#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001105 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001106 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001107 pIn3 = &aMem[pOp->p3];
1108 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001109 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001110 }
drh41d2e662015-12-01 21:23:07 +00001111#endif
danielk1977c572ef72004-05-27 09:28:41 +00001112 break;
1113}
1114
drh053a1282012-09-19 21:15:46 +00001115/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001116** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001117**
drhb8475df2011-12-09 16:21:19 +00001118** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001119** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001120** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001121** set to NULL.
1122**
1123** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1124** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1125** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001126*/
drh27a348c2015-04-13 19:14:06 +00001127case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001128 int cnt;
drh053a1282012-09-19 21:15:46 +00001129 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001130 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001131 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001132 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001133 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001134 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001135 while( cnt>0 ){
1136 pOut++;
1137 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001138 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001139 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001140 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001141 cnt--;
1142 }
drhf0863fe2005-06-12 21:35:51 +00001143 break;
1144}
1145
drh05a86c52014-02-16 01:55:49 +00001146/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001147** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001148**
1149** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1150** instruction, but do not free any string or blob memory associated with
1151** the register, so that if the value was a string or blob that was
1152** previously copied using OP_SCopy, the copies will continue to be valid.
1153*/
1154case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001155 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001156 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001157 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001158 break;
1159}
drhf0863fe2005-06-12 21:35:51 +00001160
drha5750cf2014-02-07 13:20:31 +00001161/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001162** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001163**
drh9de221d2008-01-05 06:51:30 +00001164** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001165** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001166*/
drh27a348c2015-04-13 19:14:06 +00001167case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001168 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001169 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001170 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001171 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001172 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001173 break;
1174}
1175
drheaf52d82010-05-12 13:50:23 +00001176/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001177** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001178**
drheaf52d82010-05-12 13:50:23 +00001179** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001180**
drh0fd61352014-02-07 02:29:45 +00001181** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001182** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001183*/
drh27a348c2015-04-13 19:14:06 +00001184case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001185 Mem *pVar; /* Value being transferred */
1186
drheaf52d82010-05-12 13:50:23 +00001187 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001188 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001189 pVar = &p->aVar[pOp->p1 - 1];
1190 if( sqlite3VdbeMemTooBig(pVar) ){
1191 goto too_big;
drh023ae032007-05-08 12:12:16 +00001192 }
drh7441df72017-01-09 19:27:04 +00001193 pOut = &aMem[pOp->p2];
drheaf52d82010-05-12 13:50:23 +00001194 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1195 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001196 break;
1197}
danielk1977295ba552004-05-19 10:34:51 +00001198
drhb21e7c72008-06-22 12:37:57 +00001199/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001200** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001201**
drh079a3072014-03-19 14:10:55 +00001202** Move the P3 values in register P1..P1+P3-1 over into
1203** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001204** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001205** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1206** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001207*/
drhe1349cb2008-04-01 00:36:10 +00001208case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001209 int n; /* Number of registers left to copy */
1210 int p1; /* Register to copy from */
1211 int p2; /* Register to copy to */
1212
drhe09f43f2013-11-21 04:18:31 +00001213 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001214 p1 = pOp->p1;
1215 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001216 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001217 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001218
drha6c2ed92009-11-14 23:22:23 +00001219 pIn1 = &aMem[p1];
1220 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001221 do{
drh9f6168b2016-03-19 23:32:58 +00001222 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1223 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001224 assert( memIsValid(pIn1) );
1225 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001226 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001227#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001228 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001229 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001230 }
1231#endif
drhbd6789e2015-04-28 14:00:02 +00001232 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001233 REGISTER_TRACE(p2++, pOut);
1234 pIn1++;
1235 pOut++;
drh079a3072014-03-19 14:10:55 +00001236 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001237 break;
1238}
1239
drhe8e4af72012-09-21 00:04:28 +00001240/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001241** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001242**
drhe8e4af72012-09-21 00:04:28 +00001243** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001244**
1245** This instruction makes a deep copy of the value. A duplicate
1246** is made of any string or blob constant. See also OP_SCopy.
1247*/
drhe8e4af72012-09-21 00:04:28 +00001248case OP_Copy: {
1249 int n;
1250
1251 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001252 pIn1 = &aMem[pOp->p1];
1253 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001254 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001255 while( 1 ){
1256 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1257 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001258#ifdef SQLITE_DEBUG
1259 pOut->pScopyFrom = 0;
1260#endif
drhe8e4af72012-09-21 00:04:28 +00001261 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1262 if( (n--)==0 ) break;
1263 pOut++;
1264 pIn1++;
1265 }
drhe1349cb2008-04-01 00:36:10 +00001266 break;
1267}
1268
drhb1fdb2a2008-01-05 04:06:03 +00001269/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001270** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001271**
drh9cbf3422008-01-17 16:22:13 +00001272** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001273**
1274** This instruction makes a shallow copy of the value. If the value
1275** is a string or blob, then the copy is only a pointer to the
1276** original and hence if the original changes so will the copy.
1277** Worse, if the original is deallocated, the copy becomes invalid.
1278** Thus the program must guarantee that the original will not change
1279** during the lifetime of the copy. Use OP_Copy to make a complete
1280** copy.
1281*/
drh26198bb2013-10-31 11:15:09 +00001282case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001283 pIn1 = &aMem[pOp->p1];
1284 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001285 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001286 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001287#ifdef SQLITE_DEBUG
1288 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1289#endif
drh5e00f6c2001-09-13 13:46:56 +00001290 break;
1291}
drh75897232000-05-29 14:26:00 +00001292
drhfed7ac62015-10-15 18:04:59 +00001293/* Opcode: IntCopy P1 P2 * * *
1294** Synopsis: r[P2]=r[P1]
1295**
1296** Transfer the integer value held in register P1 into register P2.
1297**
1298** This is an optimized version of SCopy that works only for integer
1299** values.
1300*/
1301case OP_IntCopy: { /* out2 */
1302 pIn1 = &aMem[pOp->p1];
1303 assert( (pIn1->flags & MEM_Int)!=0 );
1304 pOut = &aMem[pOp->p2];
1305 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1306 break;
1307}
1308
drh9cbf3422008-01-17 16:22:13 +00001309/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001310** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001311**
shane21e7feb2008-05-30 15:59:49 +00001312** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001313** results. This opcode causes the sqlite3_step() call to terminate
1314** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001315** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001316** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001317*/
drh9cbf3422008-01-17 16:22:13 +00001318case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001319 Mem *pMem;
1320 int i;
1321 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001322 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001323 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001324
drhe6400b92013-11-13 23:48:46 +00001325#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1326 /* Run the progress counter just before returning.
1327 */
1328 if( db->xProgress!=0
drh2ab792e2017-05-30 18:34:07 +00001329 && nVmStep>=nProgressLimit
drhe6400b92013-11-13 23:48:46 +00001330 && db->xProgress(db->pProgressArg)!=0
1331 ){
1332 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001333 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001334 }
1335#endif
1336
dan32b09f22009-09-23 17:29:59 +00001337 /* If this statement has violated immediate foreign key constraints, do
1338 ** not return the number of rows modified. And do not RELEASE the statement
1339 ** transaction. It needs to be rolled back. */
1340 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1341 assert( db->flags&SQLITE_CountRows );
1342 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001343 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001344 }
1345
danielk1977bd434552009-03-18 10:33:00 +00001346 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1347 ** DML statements invoke this opcode to return the number of rows
1348 ** modified to the user. This is the only way that a VM that
1349 ** opens a statement transaction may invoke this opcode.
1350 **
1351 ** In case this is such a statement, close any statement transaction
1352 ** opened by this VM before returning control to the user. This is to
1353 ** ensure that statement-transactions are always nested, not overlapping.
1354 ** If the open statement-transaction is not closed here, then the user
1355 ** may step another VM that opens its own statement transaction. This
1356 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001357 **
1358 ** The statement transaction is never a top-level transaction. Hence
1359 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001360 */
1361 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001362 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001363 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001364
drhd4e70eb2008-01-02 00:34:36 +00001365 /* Invalidate all ephemeral cursor row caches */
1366 p->cacheCtr = (p->cacheCtr + 2)|1;
1367
1368 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001369 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001370 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001371 */
drha6c2ed92009-11-14 23:22:23 +00001372 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001373 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001374 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001375 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001376 assert( (pMem[i].flags & MEM_Ephem)==0
1377 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001378 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001379 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001380 }
drh28039692008-03-17 16:54:01 +00001381 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001382
drh3d2a5292016-07-13 22:55:01 +00001383 if( db->mTrace & SQLITE_TRACE_ROW ){
1384 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1385 }
1386
drhd4e70eb2008-01-02 00:34:36 +00001387 /* Return SQLITE_ROW
1388 */
drhf56fa462015-04-13 21:39:54 +00001389 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001390 rc = SQLITE_ROW;
1391 goto vdbe_return;
1392}
1393
drh5b6afba2008-01-05 16:29:28 +00001394/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001395** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001396**
drh5b6afba2008-01-05 16:29:28 +00001397** Add the text in register P1 onto the end of the text in
1398** register P2 and store the result in register P3.
1399** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001400**
1401** P3 = P2 || P1
1402**
1403** It is illegal for P1 and P3 to be the same register. Sometimes,
1404** if P3 is the same register as P2, the implementation is able
1405** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001406*/
drh5b6afba2008-01-05 16:29:28 +00001407case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001408 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001409
drh3c657212009-11-17 23:59:58 +00001410 pIn1 = &aMem[pOp->p1];
1411 pIn2 = &aMem[pOp->p2];
1412 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001413 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001414 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001415 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001416 break;
drh5e00f6c2001-09-13 13:46:56 +00001417 }
drha0c06522009-06-17 22:50:41 +00001418 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001419 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001420 Stringify(pIn2, encoding);
1421 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001422 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001423 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001424 }
drh9c1905f2008-12-10 22:32:56 +00001425 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001426 goto no_mem;
1427 }
drhc91b2fd2014-03-01 18:13:23 +00001428 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001429 if( pOut!=pIn2 ){
1430 memcpy(pOut->z, pIn2->z, pIn2->n);
1431 }
1432 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001433 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001434 pOut->z[nByte+1] = 0;
1435 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001436 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001437 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001438 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001439 break;
1440}
drh75897232000-05-29 14:26:00 +00001441
drh3c84ddf2008-01-09 02:15:38 +00001442/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001443** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001444**
drh60a713c2008-01-21 16:22:45 +00001445** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001446** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001447** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001448*/
drh3c84ddf2008-01-09 02:15:38 +00001449/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001450** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001451**
drh3c84ddf2008-01-09 02:15:38 +00001452**
shane21e7feb2008-05-30 15:59:49 +00001453** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001454** and store the result in register P3.
1455** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001456*/
drh3c84ddf2008-01-09 02:15:38 +00001457/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001458** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001459**
drh60a713c2008-01-21 16:22:45 +00001460** Subtract the value in register P1 from the value in register P2
1461** and store the result in register P3.
1462** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001463*/
drh9cbf3422008-01-17 16:22:13 +00001464/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001465** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001466**
drh60a713c2008-01-21 16:22:45 +00001467** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001468** and store the result in register P3 (P3=P2/P1). If the value in
1469** register P1 is zero, then the result is NULL. If either input is
1470** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001471*/
drh9cbf3422008-01-17 16:22:13 +00001472/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001473** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001474**
drh40864a12013-11-15 18:58:37 +00001475** Compute the remainder after integer register P2 is divided by
1476** register P1 and store the result in register P3.
1477** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001478** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001479*/
drh5b6afba2008-01-05 16:29:28 +00001480case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1481case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1482case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1483case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1484case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001485 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001486 u16 flags; /* Combined MEM_* flags from both inputs */
1487 u16 type1; /* Numeric type of left operand */
1488 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001489 i64 iA; /* Integer value of left operand */
1490 i64 iB; /* Integer value of right operand */
1491 double rA; /* Real value of left operand */
1492 double rB; /* Real value of right operand */
1493
drh3c657212009-11-17 23:59:58 +00001494 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001495 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001496 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001497 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001498 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001499 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001500 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001501 iA = pIn1->u.i;
1502 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001503 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001504 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001505 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1506 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1507 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001508 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001509 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001510 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001511 iB /= iA;
drh75897232000-05-29 14:26:00 +00001512 break;
1513 }
drhbf4133c2001-10-13 02:59:08 +00001514 default: {
drh856c1032009-06-02 15:21:42 +00001515 if( iA==0 ) goto arithmetic_result_is_null;
1516 if( iA==-1 ) iA = 1;
1517 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001518 break;
1519 }
drh75897232000-05-29 14:26:00 +00001520 }
drh856c1032009-06-02 15:21:42 +00001521 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001522 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001523 }else if( (flags & MEM_Null)!=0 ){
1524 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001525 }else{
drhbe707b32012-12-10 22:19:14 +00001526 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001527fp_math:
drh856c1032009-06-02 15:21:42 +00001528 rA = sqlite3VdbeRealValue(pIn1);
1529 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001530 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001531 case OP_Add: rB += rA; break;
1532 case OP_Subtract: rB -= rA; break;
1533 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001534 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001535 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001536 if( rA==(double)0 ) goto arithmetic_result_is_null;
1537 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001538 break;
1539 }
drhbf4133c2001-10-13 02:59:08 +00001540 default: {
shane75ac1de2009-06-09 18:58:52 +00001541 iA = (i64)rA;
1542 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001543 if( iA==0 ) goto arithmetic_result_is_null;
1544 if( iA==-1 ) iA = 1;
1545 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001546 break;
1547 }
drh5e00f6c2001-09-13 13:46:56 +00001548 }
drhc5a7b512010-01-13 16:25:42 +00001549#ifdef SQLITE_OMIT_FLOATING_POINT
1550 pOut->u.i = rB;
1551 MemSetTypeFlag(pOut, MEM_Int);
1552#else
drh856c1032009-06-02 15:21:42 +00001553 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001554 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001555 }
drh74eaba42014-09-18 17:52:15 +00001556 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001557 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001558 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001559 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001560 }
drhc5a7b512010-01-13 16:25:42 +00001561#endif
drh5e00f6c2001-09-13 13:46:56 +00001562 }
1563 break;
1564
drha05a7222008-01-19 03:35:58 +00001565arithmetic_result_is_null:
1566 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001567 break;
1568}
1569
drh7a957892012-02-02 17:35:43 +00001570/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001571**
drhbb6783b2017-04-29 18:02:49 +00001572** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001573** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1574** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001575** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001576**
drh7a957892012-02-02 17:35:43 +00001577** If P1 is not zero, then it is a register that a subsequent min() or
1578** max() aggregate will set to 1 if the current row is not the minimum or
1579** maximum. The P1 register is initialized to 0 by this instruction.
1580**
danielk1977dc1bdc42004-06-11 10:51:27 +00001581** The interface used by the implementation of the aforementioned functions
1582** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001583** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001584*/
drh9cbf3422008-01-17 16:22:13 +00001585case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001586 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001587 if( pOp->p1 ){
1588 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1589 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001590 break;
1591}
1592
drh98757152008-01-09 23:04:12 +00001593/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001594** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001595**
drh98757152008-01-09 23:04:12 +00001596** Take the bit-wise AND of the values in register P1 and P2 and
1597** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001598** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001599*/
drh98757152008-01-09 23:04:12 +00001600/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001601** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001602**
drh98757152008-01-09 23:04:12 +00001603** Take the bit-wise OR of the values in register P1 and P2 and
1604** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001605** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001606*/
drh98757152008-01-09 23:04:12 +00001607/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001608** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001609**
drh98757152008-01-09 23:04:12 +00001610** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001611** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001612** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001613** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001614*/
drh98757152008-01-09 23:04:12 +00001615/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001616** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001617**
drh98757152008-01-09 23:04:12 +00001618** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001619** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001620** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001621** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001622*/
drh5b6afba2008-01-05 16:29:28 +00001623case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1624case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1625case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1626case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001627 i64 iA;
1628 u64 uA;
1629 i64 iB;
1630 u8 op;
drh6810ce62004-01-31 19:22:56 +00001631
drh3c657212009-11-17 23:59:58 +00001632 pIn1 = &aMem[pOp->p1];
1633 pIn2 = &aMem[pOp->p2];
1634 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001635 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001636 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001637 break;
1638 }
drh158b9cb2011-03-05 20:59:46 +00001639 iA = sqlite3VdbeIntValue(pIn2);
1640 iB = sqlite3VdbeIntValue(pIn1);
1641 op = pOp->opcode;
1642 if( op==OP_BitAnd ){
1643 iA &= iB;
1644 }else if( op==OP_BitOr ){
1645 iA |= iB;
1646 }else if( iB!=0 ){
1647 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1648
1649 /* If shifting by a negative amount, shift in the other direction */
1650 if( iB<0 ){
1651 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1652 op = 2*OP_ShiftLeft + 1 - op;
1653 iB = iB>(-64) ? -iB : 64;
1654 }
1655
1656 if( iB>=64 ){
1657 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1658 }else{
1659 memcpy(&uA, &iA, sizeof(uA));
1660 if( op==OP_ShiftLeft ){
1661 uA <<= iB;
1662 }else{
1663 uA >>= iB;
1664 /* Sign-extend on a right shift of a negative number */
1665 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1666 }
1667 memcpy(&iA, &uA, sizeof(iA));
1668 }
drhbf4133c2001-10-13 02:59:08 +00001669 }
drh158b9cb2011-03-05 20:59:46 +00001670 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001671 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001672 break;
1673}
1674
drh8558cde2008-01-05 05:20:10 +00001675/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001676** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001677**
danielk19770cdc0222008-06-26 18:04:03 +00001678** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001679** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001680**
drh8558cde2008-01-05 05:20:10 +00001681** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001682*/
drh9cbf3422008-01-17 16:22:13 +00001683case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001684 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001685 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001686 sqlite3VdbeMemIntegerify(pIn1);
1687 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001688 break;
1689}
1690
drh9cbf3422008-01-17 16:22:13 +00001691/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001692**
drh9cbf3422008-01-17 16:22:13 +00001693** Force the value in register P1 to be an integer. If the value
1694** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001695** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001696** raise an SQLITE_MISMATCH exception.
1697*/
drh9cbf3422008-01-17 16:22:13 +00001698case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001699 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001700 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001701 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001702 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001703 if( (pIn1->flags & MEM_Int)==0 ){
1704 if( pOp->p2==0 ){
1705 rc = SQLITE_MISMATCH;
1706 goto abort_due_to_error;
1707 }else{
drhf56fa462015-04-13 21:39:54 +00001708 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001709 }
drh8aff1012001-12-22 14:49:24 +00001710 }
drh8aff1012001-12-22 14:49:24 +00001711 }
drh83b301b2013-11-20 00:59:02 +00001712 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001713 break;
1714}
1715
drh13573c72010-01-12 17:04:07 +00001716#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001717/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001718**
drh2133d822008-01-03 18:44:59 +00001719** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001720**
drh8a512562005-11-14 22:29:05 +00001721** This opcode is used when extracting information from a column that
1722** has REAL affinity. Such column values may still be stored as
1723** integers, for space efficiency, but after extraction we want them
1724** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001725*/
drh9cbf3422008-01-17 16:22:13 +00001726case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001727 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001728 if( pIn1->flags & MEM_Int ){
1729 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001730 }
drh487e2622005-06-25 18:42:14 +00001731 break;
1732}
drh13573c72010-01-12 17:04:07 +00001733#endif
drh487e2622005-06-25 18:42:14 +00001734
drh8df447f2005-11-01 15:48:24 +00001735#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001736/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001737** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001738**
drh4169e432014-08-25 20:11:52 +00001739** Force the value in register P1 to be the type defined by P2.
1740**
1741** <ul>
drhbb6783b2017-04-29 18:02:49 +00001742** <li> P2=='A' &rarr; BLOB
1743** <li> P2=='B' &rarr; TEXT
1744** <li> P2=='C' &rarr; NUMERIC
1745** <li> P2=='D' &rarr; INTEGER
1746** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001747** </ul>
drh487e2622005-06-25 18:42:14 +00001748**
1749** A NULL value is not changed by this routine. It remains NULL.
1750*/
drh4169e432014-08-25 20:11:52 +00001751case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001752 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001753 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001754 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001755 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1756 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1757 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001758 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001759 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001760 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001761 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001762 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001763 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001764 break;
1765}
drh8a512562005-11-14 22:29:05 +00001766#endif /* SQLITE_OMIT_CAST */
1767
drh79752b62016-08-13 10:02:17 +00001768/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001769** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001770**
1771** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1772** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1773** store the result of comparison in register P2.
1774**
1775** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1776** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1777** to coerce both inputs according to this affinity before the
1778** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1779** affinity is used. Note that the affinity conversions are stored
1780** back into the input registers P1 and P3. So this opcode can cause
1781** persistent changes to registers P1 and P3.
1782**
1783** Once any conversions have taken place, and neither value is NULL,
1784** the values are compared. If both values are blobs then memcmp() is
1785** used to determine the results of the comparison. If both values
1786** are text, then the appropriate collating function specified in
1787** P4 is used to do the comparison. If P4 is not specified then
1788** memcmp() is used to compare text string. If both values are
1789** numeric, then a numeric comparison is used. If the two values
1790** are of different types, then numbers are considered less than
1791** strings and strings are considered less than blobs.
1792**
1793** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1794** true or false and is never NULL. If both operands are NULL then the result
1795** of comparison is true. If either operand is NULL then the result is false.
1796** If neither operand is NULL the result is the same as it would be if
1797** the SQLITE_NULLEQ flag were omitted from P5.
1798**
1799** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001800** content of r[P2] is only changed if the new value is NULL or 0 (false).
1801** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001802*/
1803/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001804** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001805**
1806** This works just like the Eq opcode except that the jump is taken if
1807** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1808** additional information.
1809**
1810** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001811** content of r[P2] is only changed if the new value is NULL or 1 (true).
1812** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001813*/
drh35573352008-01-08 23:54:25 +00001814/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001815** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001816**
drh35573352008-01-08 23:54:25 +00001817** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001818** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1819** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001820**
drh35573352008-01-08 23:54:25 +00001821** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001822** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001823** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001824**
drh35573352008-01-08 23:54:25 +00001825** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001826** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001827** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001828** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001829** affinity is used. Note that the affinity conversions are stored
1830** back into the input registers P1 and P3. So this opcode can cause
1831** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001832**
1833** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001834** the values are compared. If both values are blobs then memcmp() is
1835** used to determine the results of the comparison. If both values
1836** are text, then the appropriate collating function specified in
1837** P4 is used to do the comparison. If P4 is not specified then
1838** memcmp() is used to compare text string. If both values are
1839** numeric, then a numeric comparison is used. If the two values
1840** are of different types, then numbers are considered less than
1841** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001842*/
drh9cbf3422008-01-17 16:22:13 +00001843/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001844** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001845**
drh35573352008-01-08 23:54:25 +00001846** This works just like the Lt opcode except that the jump is taken if
1847** the content of register P3 is less than or equal to the content of
1848** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001849*/
drh9cbf3422008-01-17 16:22:13 +00001850/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001851** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001852**
drh35573352008-01-08 23:54:25 +00001853** This works just like the Lt opcode except that the jump is taken if
1854** the content of register P3 is greater than the content of
1855** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001856*/
drh9cbf3422008-01-17 16:22:13 +00001857/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001858** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001859**
drh35573352008-01-08 23:54:25 +00001860** This works just like the Lt opcode except that the jump is taken if
1861** the content of register P3 is greater than or equal to the content of
1862** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001863*/
drh9cbf3422008-01-17 16:22:13 +00001864case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1865case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1866case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1867case OP_Le: /* same as TK_LE, jump, in1, in3 */
1868case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1869case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001870 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001871 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001872 u16 flags1; /* Copy of initial value of pIn1->flags */
1873 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001874
drh3c657212009-11-17 23:59:58 +00001875 pIn1 = &aMem[pOp->p1];
1876 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001877 flags1 = pIn1->flags;
1878 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001879 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001880 /* One or both operands are NULL */
1881 if( pOp->p5 & SQLITE_NULLEQ ){
1882 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1883 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1884 ** or not both operands are null.
1885 */
1886 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001887 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001888 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drhc3191d22016-10-18 16:36:15 +00001889 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001890 && (flags3&MEM_Cleared)==0
1891 ){
drh4910a762016-09-03 01:46:15 +00001892 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00001893 }else{
drh4910a762016-09-03 01:46:15 +00001894 res = 1; /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00001895 }
drh6a2fe092009-09-23 02:29:36 +00001896 }else{
1897 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1898 ** then the result is always NULL.
1899 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1900 */
drh688852a2014-02-17 22:40:43 +00001901 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001902 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001903 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00001904 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001905 MemSetTypeFlag(pOut, MEM_Null);
1906 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001907 }else{
drhf4345e42014-02-18 11:31:59 +00001908 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001909 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001910 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001911 }
drh6a2fe092009-09-23 02:29:36 +00001912 }
1913 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001914 }
drh6a2fe092009-09-23 02:29:36 +00001915 }else{
1916 /* Neither operand is NULL. Do a comparison. */
1917 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001918 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00001919 if( (flags1 | flags3)&MEM_Str ){
1920 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1921 applyNumericAffinity(pIn1,0);
drh64caee42016-09-09 19:33:00 +00001922 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
drh4b37cd42016-06-25 11:43:47 +00001923 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00001924 }
1925 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
1926 applyNumericAffinity(pIn3,0);
1927 }
drh24a09622014-09-18 16:28:59 +00001928 }
drh64caee42016-09-09 19:33:00 +00001929 /* Handle the common case of integer comparison here, as an
1930 ** optimization, to avoid a call to sqlite3MemCompare() */
1931 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
1932 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
1933 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
1934 res = 0;
1935 goto compare_op;
1936 }
drh24a09622014-09-18 16:28:59 +00001937 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00001938 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001939 testcase( pIn1->flags & MEM_Int );
1940 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001941 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001942 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
1943 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00001944 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00001945 }
drhe5520e22015-12-31 04:34:26 +00001946 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001947 testcase( pIn3->flags & MEM_Int );
1948 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001949 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00001950 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
1951 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00001952 }
drh6a2fe092009-09-23 02:29:36 +00001953 }
drh6a2fe092009-09-23 02:29:36 +00001954 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00001955 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001956 }
drh64caee42016-09-09 19:33:00 +00001957compare_op:
drh58596362017-08-03 00:29:23 +00001958 /* At this point, res is negative, zero, or positive if reg[P1] is
1959 ** less than, equal to, or greater than reg[P3], respectively. Compute
1960 ** the answer to this operator in res2, depending on what the comparison
1961 ** operator actually is. The next block of code depends on the fact
1962 ** that the 6 comparison operators are consecutive integers in this
1963 ** order: NE, EQ, GT, LE, LT, GE */
1964 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
1965 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
1966 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
1967 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
1968 res2 = aLTb[pOp->opcode - OP_Ne];
1969 }else if( res==0 ){
1970 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
1971 res2 = aEQb[pOp->opcode - OP_Ne];
1972 }else{
1973 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
1974 res2 = aGTb[pOp->opcode - OP_Ne];
danielk1977a37cdde2004-05-16 11:15:36 +00001975 }
1976
drhf56fa462015-04-13 21:39:54 +00001977 /* Undo any changes made by applyAffinity() to the input registers. */
1978 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1979 pIn1->flags = flags1;
1980 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
1981 pIn3->flags = flags3;
1982
drh35573352008-01-08 23:54:25 +00001983 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001984 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00001985 iCompare = res;
drh3fffbf92016-09-05 15:02:41 +00001986 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00001987 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00001988 ** and prevents OP_Ne from overwriting NULL with 0. This flag
1989 ** is only used in contexts where either:
1990 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
1991 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
1992 ** Therefore it is not necessary to check the content of r[P2] for
1993 ** NULL. */
drh79752b62016-08-13 10:02:17 +00001994 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00001995 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00001996 testcase( res2==0 && pOp->opcode==OP_Eq );
1997 testcase( res2==1 && pOp->opcode==OP_Eq );
1998 testcase( res2==0 && pOp->opcode==OP_Ne );
1999 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002000 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002001 }
drh2b4ded92010-09-27 21:09:31 +00002002 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002003 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002004 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002005 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002006 }else{
drhf4345e42014-02-18 11:31:59 +00002007 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002008 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002009 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002010 }
danielk1977a37cdde2004-05-16 11:15:36 +00002011 }
2012 break;
2013}
drhc9b84a12002-06-20 11:36:48 +00002014
drh79752b62016-08-13 10:02:17 +00002015/* Opcode: ElseNotEq * P2 * * *
2016**
drhfd7459e2016-09-17 17:39:01 +00002017** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2018** If result of an OP_Eq comparison on the same two operands
2019** would have be NULL or false (0), then then jump to P2.
2020** If the result of an OP_Eq comparison on the two previous operands
2021** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002022*/
2023case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2024 assert( pOp>aOp );
2025 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002026 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002027 VdbeBranchTaken(iCompare!=0, 2);
2028 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002029 break;
2030}
2031
2032
drh0acb7e42008-06-25 00:12:41 +00002033/* Opcode: Permutation * * * P4 *
2034**
drhb7dab702017-01-26 18:00:00 +00002035** Set the permutation used by the OP_Compare operator in the next
2036** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002037**
drh953f7612012-12-07 22:18:54 +00002038** The permutation is only valid until the next OP_Compare that has
2039** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2040** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002041**
2042** The first integer in the P4 integer array is the length of the array
2043** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002044*/
2045case OP_Permutation: {
2046 assert( pOp->p4type==P4_INTARRAY );
2047 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002048 assert( pOp[1].opcode==OP_Compare );
2049 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002050 break;
2051}
2052
drh953f7612012-12-07 22:18:54 +00002053/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002054** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002055**
drh710c4842010-08-30 01:17:20 +00002056** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2057** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002058** the comparison for use by the next OP_Jump instruct.
2059**
drh0ca10df2012-12-08 13:26:23 +00002060** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2061** determined by the most recent OP_Permutation operator. If the
2062** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2063** order.
2064**
drh0acb7e42008-06-25 00:12:41 +00002065** P4 is a KeyInfo structure that defines collating sequences and sort
2066** orders for the comparison. The permutation applies to registers
2067** only. The KeyInfo elements are used sequentially.
2068**
2069** The comparison is a sort comparison, so NULLs compare equal,
2070** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002071** and strings are less than blobs.
2072*/
2073case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002074 int n;
2075 int i;
2076 int p1;
2077 int p2;
2078 const KeyInfo *pKeyInfo;
2079 int idx;
2080 CollSeq *pColl; /* Collating sequence to use on this term */
2081 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002082 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002083
drhb7dab702017-01-26 18:00:00 +00002084 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2085 aPermute = 0;
2086 }else{
2087 assert( pOp>aOp );
2088 assert( pOp[-1].opcode==OP_Permutation );
2089 assert( pOp[-1].p4type==P4_INTARRAY );
2090 aPermute = pOp[-1].p4.ai + 1;
2091 assert( aPermute!=0 );
2092 }
drh856c1032009-06-02 15:21:42 +00002093 n = pOp->p3;
2094 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002095 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002096 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002097 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002098 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002099#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002100 if( aPermute ){
2101 int k, mx = 0;
2102 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002103 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2104 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002105 }else{
drh9f6168b2016-03-19 23:32:58 +00002106 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2107 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002108 }
2109#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002110 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002111 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002112 assert( memIsValid(&aMem[p1+idx]) );
2113 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002114 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2115 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002116 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002117 pColl = pKeyInfo->aColl[i];
2118 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002119 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002120 if( iCompare ){
2121 if( bRev ) iCompare = -iCompare;
2122 break;
2123 }
drh16ee60f2008-06-20 18:13:25 +00002124 }
2125 break;
2126}
2127
2128/* Opcode: Jump P1 P2 P3 * *
2129**
2130** Jump to the instruction at address P1, P2, or P3 depending on whether
2131** in the most recent OP_Compare instruction the P1 vector was less than
2132** equal to, or greater than the P2 vector, respectively.
2133*/
drh0acb7e42008-06-25 00:12:41 +00002134case OP_Jump: { /* jump */
2135 if( iCompare<0 ){
drhf56fa462015-04-13 21:39:54 +00002136 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002137 }else if( iCompare==0 ){
drhf56fa462015-04-13 21:39:54 +00002138 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002139 }else{
drhf56fa462015-04-13 21:39:54 +00002140 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002141 }
2142 break;
2143}
2144
drh5b6afba2008-01-05 16:29:28 +00002145/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002146** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002147**
drh5b6afba2008-01-05 16:29:28 +00002148** Take the logical AND of the values in registers P1 and P2 and
2149** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002150**
drh5b6afba2008-01-05 16:29:28 +00002151** If either P1 or P2 is 0 (false) then the result is 0 even if
2152** the other input is NULL. A NULL and true or two NULLs give
2153** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002154*/
drh5b6afba2008-01-05 16:29:28 +00002155/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002156** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002157**
2158** Take the logical OR of the values in register P1 and P2 and
2159** store the answer in register P3.
2160**
2161** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2162** even if the other input is NULL. A NULL and false or two NULLs
2163** give a NULL output.
2164*/
2165case OP_And: /* same as TK_AND, in1, in2, out3 */
2166case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002167 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2168 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002169
drh3c657212009-11-17 23:59:58 +00002170 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002171 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002172 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002173 }else{
drh5b6afba2008-01-05 16:29:28 +00002174 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002175 }
drh3c657212009-11-17 23:59:58 +00002176 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002177 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002178 v2 = 2;
2179 }else{
drh5b6afba2008-01-05 16:29:28 +00002180 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002181 }
2182 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002183 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002184 v1 = and_logic[v1*3+v2];
2185 }else{
drh5b6afba2008-01-05 16:29:28 +00002186 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002187 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002188 }
drh3c657212009-11-17 23:59:58 +00002189 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002190 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002191 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002192 }else{
drh5b6afba2008-01-05 16:29:28 +00002193 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002194 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002195 }
drh5e00f6c2001-09-13 13:46:56 +00002196 break;
2197}
2198
drhe99fa2a2008-12-15 15:27:51 +00002199/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002200** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002201**
drhe99fa2a2008-12-15 15:27:51 +00002202** Interpret the value in register P1 as a boolean value. Store the
2203** boolean complement in register P2. If the value in register P1 is
2204** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002205*/
drh93952eb2009-11-13 19:43:43 +00002206case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002207 pIn1 = &aMem[pOp->p1];
2208 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002209 sqlite3VdbeMemSetNull(pOut);
2210 if( (pIn1->flags & MEM_Null)==0 ){
2211 pOut->flags = MEM_Int;
2212 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002213 }
drh5e00f6c2001-09-13 13:46:56 +00002214 break;
2215}
2216
drhe99fa2a2008-12-15 15:27:51 +00002217/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002218** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002219**
drhe99fa2a2008-12-15 15:27:51 +00002220** Interpret the content of register P1 as an integer. Store the
2221** ones-complement of the P1 value into register P2. If P1 holds
2222** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002223*/
drh93952eb2009-11-13 19:43:43 +00002224case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002225 pIn1 = &aMem[pOp->p1];
2226 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002227 sqlite3VdbeMemSetNull(pOut);
2228 if( (pIn1->flags & MEM_Null)==0 ){
2229 pOut->flags = MEM_Int;
2230 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002231 }
drhbf4133c2001-10-13 02:59:08 +00002232 break;
2233}
2234
drh48f2d3b2011-09-16 01:34:43 +00002235/* Opcode: Once P1 P2 * * *
2236**
drhab087d42017-03-24 17:59:56 +00002237** Fall through to the next instruction the first time this opcode is
2238** encountered on each invocation of the byte-code program. Jump to P2
2239** on the second and all subsequent encounters during the same invocation.
2240**
2241** Top-level programs determine first invocation by comparing the P1
2242** operand against the P1 operand on the OP_Init opcode at the beginning
2243** of the program. If the P1 values differ, then fall through and make
2244** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2245** the same then take the jump.
2246**
2247** For subprograms, there is a bitmask in the VdbeFrame that determines
2248** whether or not the jump should be taken. The bitmask is necessary
2249** because the self-altering code trick does not work for recursive
2250** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002251*/
dan1d8cb212011-12-09 13:24:16 +00002252case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002253 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002254 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002255 if( p->pFrame ){
2256 iAddr = (int)(pOp - p->aOp);
2257 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2258 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002259 goto jump_to_p2;
2260 }
drh18333ef2017-03-24 18:38:41 +00002261 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002262 }else{
drhab087d42017-03-24 17:59:56 +00002263 if( p->aOp[0].p1==pOp->p1 ){
2264 VdbeBranchTaken(1, 2);
2265 goto jump_to_p2;
2266 }
dan1d8cb212011-12-09 13:24:16 +00002267 }
drhab087d42017-03-24 17:59:56 +00002268 VdbeBranchTaken(0, 2);
2269 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002270 break;
2271}
2272
drh3c84ddf2008-01-09 02:15:38 +00002273/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002274**
drhef8662b2011-06-20 21:47:58 +00002275** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002276** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002277** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002278*/
drh3c84ddf2008-01-09 02:15:38 +00002279/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002280**
drhef8662b2011-06-20 21:47:58 +00002281** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002282** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002283** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002284*/
drh9cbf3422008-01-17 16:22:13 +00002285case OP_If: /* jump, in1 */
2286case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002287 int c;
drh3c657212009-11-17 23:59:58 +00002288 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002289 if( pIn1->flags & MEM_Null ){
2290 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002291 }else{
drhba0232a2005-06-06 17:27:19 +00002292#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002293 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002294#else
drh3c84ddf2008-01-09 02:15:38 +00002295 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002296#endif
drhf5905aa2002-05-26 20:54:33 +00002297 if( pOp->opcode==OP_IfNot ) c = !c;
2298 }
drh688852a2014-02-17 22:40:43 +00002299 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002300 if( c ){
drhf56fa462015-04-13 21:39:54 +00002301 goto jump_to_p2;
drh3c84ddf2008-01-09 02:15:38 +00002302 }
drh5e00f6c2001-09-13 13:46:56 +00002303 break;
2304}
2305
drh830ecf92009-06-18 00:41:55 +00002306/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002307** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002308**
drh830ecf92009-06-18 00:41:55 +00002309** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002310*/
drh9cbf3422008-01-17 16:22:13 +00002311case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002312 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002313 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002314 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002315 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002316 }
drh477df4b2008-01-05 18:48:24 +00002317 break;
2318}
2319
drh98757152008-01-09 23:04:12 +00002320/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002321** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002322**
drh6a288a32008-01-07 19:20:24 +00002323** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002324*/
drh9cbf3422008-01-17 16:22:13 +00002325case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002326 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002327 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002328 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002329 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002330 }
drh5e00f6c2001-09-13 13:46:56 +00002331 break;
2332}
2333
drh31d6fd52017-04-14 19:03:10 +00002334/* Opcode: IfNullRow P1 P2 P3 * *
2335** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2336**
2337** Check the cursor P1 to see if it is currently pointing at a NULL row.
2338** If it is, then set register P3 to NULL and jump immediately to P2.
2339** If P1 is not on a NULL row, then fall through without making any
2340** changes.
2341*/
2342case OP_IfNullRow: { /* jump */
2343 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002344 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002345 if( p->apCsr[pOp->p1]->nullRow ){
2346 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2347 goto jump_to_p2;
2348 }
2349 break;
2350}
2351
drh092457b2017-12-29 15:04:49 +00002352#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2353/* Opcode: Offset P1 P2 P3 * *
2354** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002355**
drh092457b2017-12-29 15:04:49 +00002356** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002357** start of the payload for the record at which that cursor P1 is currently
2358** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002359**
drh092457b2017-12-29 15:04:49 +00002360** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002361** This opcode does not use P2 itself, but the P2 value is used by the
2362** code generator. The P1, P2, and P3 operands to this opcode are the
2363** as as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002364**
2365** This opcode is only available if SQLite is compiled with the
2366** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002367*/
drh092457b2017-12-29 15:04:49 +00002368case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002369 VdbeCursor *pC; /* The VDBE cursor */
2370 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2371 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002372 pOut = &p->aMem[pOp->p3];
drhc64487b2017-12-29 17:21:21 +00002373 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002374 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002375 }else{
drh092457b2017-12-29 15:04:49 +00002376 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
drh2fc865c2017-12-16 20:20:37 +00002377 }
2378 break;
2379}
drh092457b2017-12-29 15:04:49 +00002380#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002381
drh3e9ca092009-09-08 01:14:48 +00002382/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002383** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002384**
danielk1977cfcdaef2004-05-12 07:33:33 +00002385** Interpret the data that cursor P1 points to as a structure built using
2386** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002387** information about the format of the data.) Extract the P2-th column
2388** from this record. If there are less that (P2+1)
2389** values in the record, extract a NULL.
2390**
drh9cbf3422008-01-17 16:22:13 +00002391** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002392**
drh1cc3a362017-04-03 13:17:31 +00002393** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002394** if the P4 argument is a P4_MEM use the value of the P4 argument as
2395** the result.
drh3e9ca092009-09-08 01:14:48 +00002396**
2397** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2398** then the cache of the cursor is reset prior to extracting the column.
2399** The first OP_Column against a pseudo-table after the value of the content
2400** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002401**
drh1cc3a362017-04-03 13:17:31 +00002402** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002403** the result is guaranteed to only be used as the argument of a length()
2404** or typeof() function, respectively. The loading of large blobs can be
2405** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002406*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002407case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002408 int p2; /* column number to retrieve */
2409 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002410 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002411 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002412 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002413 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002414 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002415 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002416 const u8 *zData; /* Part of the record being decoded */
2417 const u8 *zHdr; /* Next unparsed byte of the header */
2418 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002419 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002420 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002421 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002422
dande892d92016-01-29 19:29:45 +00002423 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002424 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002425
drh170ad682017-06-02 15:44:22 +00002426 /* If the cursor cache is stale (meaning it is not currently point at
2427 ** the correct row) then bring it up-to-date by doing the necessary
2428 ** B-Tree seek. */
dande892d92016-01-29 19:29:45 +00002429 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002430 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002431
drh9f6168b2016-03-19 23:32:58 +00002432 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002433 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002434 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002435 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002436 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002437 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002438 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002439 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002440 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2441 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002442
drha43a02e2016-05-19 17:51:19 +00002443 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002444 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002445 if( pC->eCurType==CURTYPE_PSEUDO ){
drhfe0cf7a2017-08-16 19:20:20 +00002446 /* For the special case of as pseudo-cursor, the seekResult field
2447 ** identifies the register that holds the record */
2448 assert( pC->seekResult>0 );
2449 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002450 assert( pReg->flags & MEM_Blob );
2451 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002452 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002453 pC->aRow = (u8*)pReg->z;
2454 }else{
drh6b5631e2014-11-05 15:57:39 +00002455 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002456 goto op_column_out;
2457 }
danielk1977192ac1d2004-05-10 07:17:30 +00002458 }else{
drh06a09a82016-11-25 17:03:03 +00002459 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002460 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002461 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002462 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2463 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002464 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2465 assert( pC->szRow<=pC->payloadSize );
2466 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
2467 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5f7dacb2015-11-20 13:33:56 +00002468 goto too_big;
drh399af1d2013-11-20 17:25:55 +00002469 }
danielk1977192ac1d2004-05-10 07:17:30 +00002470 }
drhb73857f2006-03-17 00:25:59 +00002471 pC->cacheStatus = p->cacheCtr;
drh1f613c42017-08-16 14:16:19 +00002472 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
drh399af1d2013-11-20 17:25:55 +00002473 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002474
drhc81aa2e2014-10-11 23:31:52 +00002475
drh1f613c42017-08-16 14:16:19 +00002476 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002477 /* pC->aRow does not have to hold the entire row, but it does at least
2478 ** need to cover the header of the record. If pC->aRow does not contain
2479 ** the complete header, then set it to zero, forcing the header to be
2480 ** dynamically allocated. */
2481 pC->aRow = 0;
2482 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002483
2484 /* Make sure a corrupt database has not given us an oversize header.
2485 ** Do this now to avoid an oversize memory allocation.
2486 **
2487 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2488 ** types use so much data space that there can only be 4096 and 32 of
2489 ** them, respectively. So the maximum header length results from a
2490 ** 3-byte type for each of the maximum of 32768 columns plus three
2491 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2492 */
drh1f613c42017-08-16 14:16:19 +00002493 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002494 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002495 }
drh95b225a2017-08-16 11:04:22 +00002496 }else{
2497 /* This is an optimization. By skipping over the first few tests
2498 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2499 ** measurable performance gain.
2500 **
drh1f613c42017-08-16 14:16:19 +00002501 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002502 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002503 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002504 ** branch jumps to reads past the end of the record, but never more
2505 ** than a few bytes. Even if the record occurs at the end of the page
2506 ** content area, the "page header" comes after the page content and so
2507 ** this overread is harmless. Similar overreads can occur for a corrupt
2508 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002509 */
2510 zData = pC->aRow;
2511 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002512 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002513 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002514 }
drh399af1d2013-11-20 17:25:55 +00002515 }
drh35cd6432009-06-05 14:17:21 +00002516
drh399af1d2013-11-20 17:25:55 +00002517 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002518 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002519 */
drhc8606e42013-11-20 19:28:03 +00002520 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002521 /* If there is more header available for parsing in the record, try
2522 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002523 */
drhc8606e42013-11-20 19:28:03 +00002524 if( pC->iHdrOffset<aOffset[0] ){
2525 /* Make sure zData points to enough of the record to cover the header. */
2526 if( pC->aRow==0 ){
2527 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002528 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002529 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002530 zData = (u8*)sMem.z;
2531 }else{
2532 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002533 }
drhc8606e42013-11-20 19:28:03 +00002534
drh0c8f7602014-09-19 16:56:45 +00002535 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002536 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002537 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002538 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002539 zHdr = zData + pC->iHdrOffset;
2540 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002541 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002542 do{
drh95fa6062015-10-16 13:50:08 +00002543 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002544 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002545 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002546 }else{
drhc8606e42013-11-20 19:28:03 +00002547 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002548 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002549 }
drhfaf37272015-10-16 14:23:42 +00002550 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002551 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002552 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002553
drh8dd83622014-10-13 23:39:02 +00002554 /* The record is corrupt if any of the following are true:
2555 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002556 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002557 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002558 */
drhc6ce38832015-10-15 21:30:24 +00002559 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2560 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002561 ){
drh95b225a2017-08-16 11:04:22 +00002562 if( aOffset[0]==0 ){
2563 i = 0;
2564 zHdr = zEndHdr;
2565 }else{
2566 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002567 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002568 }
danielk1977dedf45b2006-01-13 17:12:01 +00002569 }
drhddb2b4a2016-03-25 12:10:32 +00002570
drh170c2762016-05-20 21:40:11 +00002571 pC->nHdrParsed = i;
2572 pC->iHdrOffset = (u32)(zHdr - zData);
2573 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002574 }else{
drh9fbc8852016-01-04 03:48:46 +00002575 t = 0;
drh9188b382004-05-14 21:12:22 +00002576 }
drhd3194f52004-05-27 19:59:32 +00002577
drhf2db3382015-04-30 20:33:25 +00002578 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002579 ** still not up to p2, that means that the record has fewer than p2
2580 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002581 */
drhc8606e42013-11-20 19:28:03 +00002582 if( pC->nHdrParsed<=p2 ){
2583 if( pOp->p4type==P4_MEM ){
2584 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2585 }else{
drh22e8d832014-10-29 00:58:38 +00002586 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002587 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002588 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002589 }
drh95fa6062015-10-16 13:50:08 +00002590 }else{
2591 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002592 }
danielk1977192ac1d2004-05-10 07:17:30 +00002593
drh380d6852013-11-20 20:58:00 +00002594 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002595 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002596 ** all valid.
drh9188b382004-05-14 21:12:22 +00002597 */
drhc8606e42013-11-20 19:28:03 +00002598 assert( p2<pC->nHdrParsed );
2599 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002600 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002601 if( VdbeMemDynamic(pDest) ){
2602 sqlite3VdbeMemSetNull(pDest);
2603 }
drh95fa6062015-10-16 13:50:08 +00002604 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002605 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002606 /* This is the common case where the desired content fits on the original
2607 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002608 zData = pC->aRow + aOffset[p2];
2609 if( t<12 ){
2610 sqlite3VdbeSerialGet(zData, t, pDest);
2611 }else{
2612 /* If the column value is a string, we need a persistent value, not
2613 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2614 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2615 */
2616 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2617 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002618 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002619 if( pDest->szMalloc < len+2 ){
2620 pDest->flags = MEM_Null;
2621 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2622 }else{
2623 pDest->z = pDest->zMalloc;
2624 }
2625 memcpy(pDest->z, zData, len);
2626 pDest->z[len] = 0;
2627 pDest->z[len+1] = 0;
2628 pDest->flags = aFlag[t&1];
2629 }
danielk197736963fd2005-02-19 08:18:05 +00002630 }else{
drha1851ef2016-05-20 19:51:28 +00002631 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002632 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002633 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2634 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2635 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002636 ){
drh2a2a6962014-09-16 18:22:44 +00002637 /* Content is irrelevant for
2638 ** 1. the typeof() function,
2639 ** 2. the length(X) function if X is a blob, and
2640 ** 3. if the content length is zero.
2641 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002642 ** content from disk.
2643 **
2644 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2645 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2646 ** read up to 16. So 16 bytes of bogus content is supplied.
2647 */
2648 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002649 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002650 }else{
drhcb3cabd2016-11-25 19:18:28 +00002651 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002652 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2653 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2654 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002655 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002656 }
drhd3194f52004-05-27 19:59:32 +00002657
danielk19773c9cc8d2005-01-17 03:40:08 +00002658op_column_out:
drhb7654112008-01-12 12:48:07 +00002659 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002660 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002661 break;
drh74588ce2017-09-13 00:13:05 +00002662
2663op_column_corrupt:
2664 if( aOp[0].p3>0 ){
2665 pOp = &aOp[aOp[0].p3-1];
2666 break;
2667 }else{
2668 rc = SQLITE_CORRUPT_BKPT;
2669 goto abort_due_to_error;
2670 }
danielk1977192ac1d2004-05-10 07:17:30 +00002671}
2672
danielk1977751de562008-04-18 09:01:15 +00002673/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002674** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002675**
2676** Apply affinities to a range of P2 registers starting with P1.
2677**
drhbb6783b2017-04-29 18:02:49 +00002678** P4 is a string that is P2 characters long. The N-th character of the
2679** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00002680** memory cell in the range.
2681*/
2682case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002683 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002684
drh856c1032009-06-02 15:21:42 +00002685 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002686 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002687 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002688 assert( zAffinity[pOp->p2]==0 );
2689 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002690 do{
drh9f6168b2016-03-19 23:32:58 +00002691 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002692 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002693 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002694 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002695 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002696 break;
2697}
2698
drh1db639c2008-01-17 02:36:28 +00002699/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002700** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002701**
drh710c4842010-08-30 01:17:20 +00002702** Convert P2 registers beginning with P1 into the [record format]
2703** use as a data record in a database table or as a key
2704** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002705**
drhbb6783b2017-04-29 18:02:49 +00002706** P4 may be a string that is P2 characters long. The N-th character of the
2707** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00002708** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002709**
drh8a512562005-11-14 22:29:05 +00002710** The mapping from character to affinity is given by the SQLITE_AFF_
2711** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002712**
drh05883a32015-06-02 15:32:08 +00002713** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002714*/
drh1db639c2008-01-17 02:36:28 +00002715case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002716 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2717 Mem *pRec; /* The new record */
2718 u64 nData; /* Number of bytes of data space */
2719 int nHdr; /* Number of bytes of header space */
2720 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002721 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002722 int nVarint; /* Number of bytes in a varint */
2723 u32 serial_type; /* Type field */
2724 Mem *pData0; /* First field to be combined into the record */
2725 Mem *pLast; /* Last field of the record */
2726 int nField; /* Number of fields in the record */
2727 char *zAffinity; /* The affinity string for the record */
2728 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002729 int i; /* Space used in zNewRecord[] header */
2730 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002731 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002732
drhf3218fe2004-05-28 08:21:02 +00002733 /* Assuming the record contains N fields, the record format looks
2734 ** like this:
2735 **
drh7a224de2004-06-02 01:22:02 +00002736 ** ------------------------------------------------------------------------
2737 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2738 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002739 **
drh9cbf3422008-01-17 16:22:13 +00002740 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002741 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002742 **
2743 ** Each type field is a varint representing the serial type of the
2744 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002745 ** hdr-size field is also a varint which is the offset from the beginning
2746 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002747 */
drh856c1032009-06-02 15:21:42 +00002748 nData = 0; /* Number of bytes of data space */
2749 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002750 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002751 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002752 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002753 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002754 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002755 nField = pOp->p2;
2756 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002757 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002758
drh2b4ded92010-09-27 21:09:31 +00002759 /* Identify the output register */
2760 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2761 pOut = &aMem[pOp->p3];
2762 memAboutToChange(p, pOut);
2763
drh3e6c0602013-12-10 20:53:01 +00002764 /* Apply the requested affinity to all inputs
2765 */
2766 assert( pData0<=pLast );
2767 if( zAffinity ){
2768 pRec = pData0;
2769 do{
drh57bf4a82014-02-17 14:59:22 +00002770 applyAffinity(pRec++, *(zAffinity++), encoding);
2771 assert( zAffinity[0]==0 || pRec<=pLast );
2772 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002773 }
2774
drhd447dce2017-01-25 20:55:11 +00002775#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002776 /* NULLs can be safely trimmed from the end of the record, as long as
2777 ** as the schema format is 2 or more and none of the omitted columns
2778 ** have a non-NULL default value. Also, the record must be left with
2779 ** at least one field. If P5>0 then it will be one more than the
2780 ** index of the right-most column with a non-NULL default value */
2781 if( pOp->p5 ){
2782 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2783 pLast--;
2784 nField--;
2785 }
2786 }
drhd447dce2017-01-25 20:55:11 +00002787#endif
drh585ce192017-01-25 14:58:27 +00002788
drhf3218fe2004-05-28 08:21:02 +00002789 /* Loop through the elements that will make up the record to figure
2790 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002791 */
drh038b7bc2013-12-09 23:17:22 +00002792 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002793 do{
drh2b4ded92010-09-27 21:09:31 +00002794 assert( memIsValid(pRec) );
drhbe37c122015-10-16 14:54:17 +00002795 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002796 if( pRec->flags & MEM_Zero ){
drh038b7bc2013-12-09 23:17:22 +00002797 if( nData ){
drh53e66c32015-07-24 15:49:23 +00002798 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002799 }else{
2800 nZero += pRec->u.nZero;
2801 len -= pRec->u.nZero;
2802 }
drhfdf972a2007-05-02 13:30:27 +00002803 }
drh8079a0d2006-01-12 17:20:50 +00002804 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002805 testcase( serial_type==127 );
2806 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002807 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh45c3c662016-04-07 14:16:16 +00002808 if( pRec==pData0 ) break;
2809 pRec--;
2810 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002811
drh654858d2014-11-20 02:18:14 +00002812 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2813 ** which determines the total number of bytes in the header. The varint
2814 ** value is the size of the header in bytes including the size varint
2815 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002816 testcase( nHdr==126 );
2817 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002818 if( nHdr<=126 ){
2819 /* The common case */
2820 nHdr += 1;
2821 }else{
2822 /* Rare case of a really large header */
2823 nVarint = sqlite3VarintLen(nHdr);
2824 nHdr += nVarint;
2825 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002826 }
drh038b7bc2013-12-09 23:17:22 +00002827 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002828 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002829 goto too_big;
2830 }
drhf3218fe2004-05-28 08:21:02 +00002831
danielk1977a7a8e142008-02-13 18:25:27 +00002832 /* Make sure the output register has a buffer large enough to store
2833 ** the new record. The output register (pOp->p3) is not allowed to
2834 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002835 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002836 */
drh322f2852014-09-19 00:43:39 +00002837 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002838 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002839 }
danielk1977a7a8e142008-02-13 18:25:27 +00002840 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002841
2842 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002843 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002844 j = nHdr;
2845 assert( pData0<=pLast );
2846 pRec = pData0;
2847 do{
drhfacf47a2014-10-13 20:12:47 +00002848 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002849 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2850 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002851 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002852 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2853 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002854 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002855 }while( (++pRec)<=pLast );
2856 assert( i==nHdr );
2857 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002858
drh9f6168b2016-03-19 23:32:58 +00002859 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002860 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002861 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002862 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002863 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002864 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002865 }
drh1013c932008-01-06 00:25:21 +00002866 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002867 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002868 break;
2869}
2870
danielk1977a5533162009-02-24 10:01:51 +00002871/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002872** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002873**
2874** Store the number of entries (an integer value) in the table or index
2875** opened by cursor P1 in register P2
2876*/
2877#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002878case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002879 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002880 BtCursor *pCrsr;
2881
drhc960dcb2015-11-20 19:22:01 +00002882 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2883 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002884 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002885 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002886 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002887 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002888 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002889 pOut->u.i = nEntry;
2890 break;
2891}
2892#endif
2893
danielk1977fd7f0452008-12-17 17:30:26 +00002894/* Opcode: Savepoint P1 * * P4 *
2895**
2896** Open, release or rollback the savepoint named by parameter P4, depending
2897** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2898** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2899*/
2900case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002901 int p1; /* Value of P1 operand */
2902 char *zName; /* Name of savepoint */
2903 int nName;
2904 Savepoint *pNew;
2905 Savepoint *pSavepoint;
2906 Savepoint *pTmp;
2907 int iSavepoint;
2908 int ii;
2909
2910 p1 = pOp->p1;
2911 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002912
2913 /* Assert that the p1 parameter is valid. Also that if there is no open
2914 ** transaction, then there cannot be any savepoints.
2915 */
2916 assert( db->pSavepoint==0 || db->autoCommit==0 );
2917 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2918 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2919 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002920 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002921
2922 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002923 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002924 /* A new savepoint cannot be created if there are active write
2925 ** statements (i.e. open read/write incremental blob handles).
2926 */
drh22c17b82015-05-15 04:13:15 +00002927 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002928 rc = SQLITE_BUSY;
2929 }else{
drh856c1032009-06-02 15:21:42 +00002930 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002931
drhbe07ec52011-06-03 12:15:26 +00002932#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002933 /* This call is Ok even if this savepoint is actually a transaction
2934 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2935 ** If this is a transaction savepoint being opened, it is guaranteed
2936 ** that the db->aVTrans[] array is empty. */
2937 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002938 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2939 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002940 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002941#endif
dand9495cd2011-04-27 12:08:04 +00002942
danielk1977fd7f0452008-12-17 17:30:26 +00002943 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00002944 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00002945 if( pNew ){
2946 pNew->zName = (char *)&pNew[1];
2947 memcpy(pNew->zName, zName, nName+1);
2948
2949 /* If there is no open transaction, then mark this as a special
2950 ** "transaction savepoint". */
2951 if( db->autoCommit ){
2952 db->autoCommit = 0;
2953 db->isTransactionSavepoint = 1;
2954 }else{
2955 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002956 }
dan21e8d012011-03-03 20:05:59 +00002957
danielk1977fd7f0452008-12-17 17:30:26 +00002958 /* Link the new savepoint into the database handle's list. */
2959 pNew->pNext = db->pSavepoint;
2960 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002961 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00002962 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002963 }
2964 }
2965 }else{
drh856c1032009-06-02 15:21:42 +00002966 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002967
2968 /* Find the named savepoint. If there is no such savepoint, then an
2969 ** an error is returned to the user. */
2970 for(
drh856c1032009-06-02 15:21:42 +00002971 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002972 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002973 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002974 ){
2975 iSavepoint++;
2976 }
2977 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00002978 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002979 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002980 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002981 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002982 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002983 */
drh22c17b82015-05-15 04:13:15 +00002984 sqlite3VdbeError(p, "cannot release savepoint - "
2985 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002986 rc = SQLITE_BUSY;
2987 }else{
2988
2989 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002990 ** and this is a RELEASE command, then the current transaction
2991 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002992 */
2993 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2994 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002995 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002996 goto vdbe_return;
2997 }
danielk1977fd7f0452008-12-17 17:30:26 +00002998 db->autoCommit = 1;
2999 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003000 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003001 db->autoCommit = 0;
3002 p->rc = rc = SQLITE_BUSY;
3003 goto vdbe_return;
3004 }
danielk197734cf35d2008-12-18 18:31:38 +00003005 db->isTransactionSavepoint = 0;
3006 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003007 }else{
drh47b7fc72014-11-11 01:33:57 +00003008 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003009 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003010 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003011 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003012 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003013 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3014 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003015 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003016 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003017 }
drh47b7fc72014-11-11 01:33:57 +00003018 }else{
3019 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003020 }
3021 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003022 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3023 if( rc!=SQLITE_OK ){
3024 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003025 }
danielk1977fd7f0452008-12-17 17:30:26 +00003026 }
drh47b7fc72014-11-11 01:33:57 +00003027 if( isSchemaChange ){
danielk1977fd7f0452008-12-17 17:30:26 +00003028 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00003029 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003030 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003031 }
3032 }
3033
3034 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3035 ** savepoints nested inside of the savepoint being operated on. */
3036 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003037 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003038 db->pSavepoint = pTmp->pNext;
3039 sqlite3DbFree(db, pTmp);
3040 db->nSavepoint--;
3041 }
3042
dan1da40a32009-09-19 17:00:31 +00003043 /* If it is a RELEASE, then destroy the savepoint being operated on
3044 ** too. If it is a ROLLBACK TO, then set the number of deferred
3045 ** constraint violations present in the database to the value stored
3046 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003047 if( p1==SAVEPOINT_RELEASE ){
3048 assert( pSavepoint==db->pSavepoint );
3049 db->pSavepoint = pSavepoint->pNext;
3050 sqlite3DbFree(db, pSavepoint);
3051 if( !isTransaction ){
3052 db->nSavepoint--;
3053 }
dan1da40a32009-09-19 17:00:31 +00003054 }else{
3055 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003056 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003057 }
dand9495cd2011-04-27 12:08:04 +00003058
danea8562e2015-04-18 16:25:54 +00003059 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003060 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3061 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3062 }
danielk1977fd7f0452008-12-17 17:30:26 +00003063 }
3064 }
drh9467abf2016-02-17 18:44:11 +00003065 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003066
3067 break;
3068}
3069
drh98757152008-01-09 23:04:12 +00003070/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003071**
3072** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003073** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003074** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3075** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003076**
3077** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003078*/
drh9cbf3422008-01-17 16:22:13 +00003079case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003080 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003081 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003082
drh856c1032009-06-02 15:21:42 +00003083 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003084 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003085 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003086 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003087 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003088 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003089
drhb0c88652016-02-01 13:21:13 +00003090 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003091 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003092 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003093 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003094 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003095 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3096 /* If this instruction implements a COMMIT and other VMs are writing
3097 ** return an error indicating that the other VMs must complete first.
3098 */
3099 sqlite3VdbeError(p, "cannot commit transaction - "
3100 "SQL statements in progress");
3101 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003102 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003103 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003104 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003105 }else{
shane7d3846a2008-12-11 02:58:26 +00003106 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003107 }
3108 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3109 p->pc = (int)(pOp - aOp);
3110 db->autoCommit = (u8)(1-desiredAutoCommit);
3111 p->rc = rc = SQLITE_BUSY;
3112 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003113 }
danielk1977bd434552009-03-18 10:33:00 +00003114 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003115 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003116 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003117 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003118 }else{
drh900b31e2007-08-28 02:27:51 +00003119 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003120 }
drh900b31e2007-08-28 02:27:51 +00003121 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003122 }else{
drh22c17b82015-05-15 04:13:15 +00003123 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003124 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003125 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003126 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003127
3128 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003129 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003130 }
3131 break;
3132}
3133
drhb22f7c82014-02-06 23:56:27 +00003134/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003135**
drh05a86c52014-02-16 01:55:49 +00003136** Begin a transaction on database P1 if a transaction is not already
3137** active.
3138** If P2 is non-zero, then a write-transaction is started, or if a
3139** read-transaction is already active, it is upgraded to a write-transaction.
3140** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003141**
drh001bbcb2003-03-19 03:14:00 +00003142** P1 is the index of the database file on which the transaction is
3143** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003144** file used for temporary tables. Indices of 2 or more are used for
3145** attached databases.
drhcabb0812002-09-14 13:47:32 +00003146**
dane0af83a2009-09-08 19:15:01 +00003147** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3148** true (this flag is set if the Vdbe may modify more than one row and may
3149** throw an ABORT exception), a statement transaction may also be opened.
3150** More specifically, a statement transaction is opened iff the database
3151** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003152** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003153** VDBE to be rolled back after an error without having to roll back the
3154** entire transaction. If no error is encountered, the statement transaction
3155** will automatically commit when the VDBE halts.
3156**
drhb22f7c82014-02-06 23:56:27 +00003157** If P5!=0 then this opcode also checks the schema cookie against P3
3158** and the schema generation counter against P4.
3159** The cookie changes its value whenever the database schema changes.
3160** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003161** and that the current process needs to reread the schema. If the schema
3162** cookie in P3 differs from the schema cookie in the database header or
3163** if the schema generation counter in P4 differs from the current
3164** generation counter, then an SQLITE_SCHEMA error is raised and execution
3165** halts. The sqlite3_step() wrapper function might then reprepare the
3166** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003167*/
drh9cbf3422008-01-17 16:22:13 +00003168case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003169 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00003170 int iMeta;
3171 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00003172
drh1713afb2013-06-28 01:24:57 +00003173 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003174 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003175 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003176 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003177 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3178 rc = SQLITE_READONLY;
3179 goto abort_due_to_error;
3180 }
drh653b82a2009-06-22 11:10:47 +00003181 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003182
danielk197724162fe2004-06-04 06:22:00 +00003183 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003184 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
drhcbd8db32015-08-20 17:18:32 +00003185 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3186 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003187 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003188 if( (rc&0xff)==SQLITE_BUSY ){
3189 p->pc = (int)(pOp - aOp);
3190 p->rc = rc;
3191 goto vdbe_return;
3192 }
danielk197724162fe2004-06-04 06:22:00 +00003193 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003194 }
dane0af83a2009-09-08 19:15:01 +00003195
3196 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003197 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003198 ){
3199 assert( sqlite3BtreeIsInTrans(pBt) );
3200 if( p->iStatement==0 ){
3201 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3202 db->nStatement++;
3203 p->iStatement = db->nSavepoint + db->nStatement;
3204 }
dana311b802011-04-26 19:21:34 +00003205
drh346506f2011-05-25 01:16:42 +00003206 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003207 if( rc==SQLITE_OK ){
3208 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3209 }
dan1da40a32009-09-19 17:00:31 +00003210
3211 /* Store the current value of the database handles deferred constraint
3212 ** counter. If the statement transaction needs to be rolled back,
3213 ** the value of this counter needs to be restored too. */
3214 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003215 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003216 }
drhb22f7c82014-02-06 23:56:27 +00003217
drh51a74d42015-02-28 01:04:27 +00003218 /* Gather the schema version number for checking:
drh96fdcb42016-09-27 00:09:33 +00003219 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3220 ** version is checked to ensure that the schema has not changed since the
3221 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003222 */
drhb22f7c82014-02-06 23:56:27 +00003223 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3224 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3225 }else{
3226 iGen = iMeta = 0;
3227 }
3228 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3229 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3230 sqlite3DbFree(db, p->zErrMsg);
3231 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3232 /* If the schema-cookie from the database file matches the cookie
3233 ** stored with the in-memory representation of the schema, do
3234 ** not reload the schema from the database file.
3235 **
3236 ** If virtual-tables are in use, this is not just an optimization.
3237 ** Often, v-tables store their data in other SQLite tables, which
3238 ** are queried from within xNext() and other v-table methods using
3239 ** prepared queries. If such a query is out-of-date, we do not want to
3240 ** discard the database schema, as the user code implementing the
3241 ** v-table would have to be ready for the sqlite3_vtab structure itself
3242 ** to be invalidated whenever sqlite3_step() is called from within
3243 ** a v-table method.
3244 */
3245 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3246 sqlite3ResetOneSchema(db, pOp->p1);
3247 }
3248 p->expired = 1;
3249 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003250 }
drh9467abf2016-02-17 18:44:11 +00003251 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003252 break;
3253}
3254
drhb1fdb2a2008-01-05 04:06:03 +00003255/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003256**
drh9cbf3422008-01-17 16:22:13 +00003257** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003258** P3==1 is the schema version. P3==2 is the database format.
3259** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003260** the main database file and P1==1 is the database file used to store
3261** temporary tables.
drh4a324312001-12-21 14:30:42 +00003262**
drh50e5dad2001-09-15 00:57:28 +00003263** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003264** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003265** executing this instruction.
3266*/
drh27a348c2015-04-13 19:14:06 +00003267case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003268 int iMeta;
drh856c1032009-06-02 15:21:42 +00003269 int iDb;
3270 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003271
drh1713afb2013-06-28 01:24:57 +00003272 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003273 iDb = pOp->p1;
3274 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003275 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003276 assert( iDb>=0 && iDb<db->nDb );
3277 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003278 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003279
danielk1977602b4662009-07-02 07:47:33 +00003280 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003281 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003282 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003283 break;
3284}
3285
drh98757152008-01-09 23:04:12 +00003286/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003287**
drh1861afc2016-02-01 21:48:34 +00003288** Write the integer value P3 into cookie number P2 of database P1.
3289** P2==1 is the schema version. P2==2 is the database format.
3290** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003291** size, and so forth. P1==0 is the main database file and P1==1 is the
3292** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003293**
3294** A transaction must be started before executing this opcode.
3295*/
drh1861afc2016-02-01 21:48:34 +00003296case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003297 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003298 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003299 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003300 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003301 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003302 pDb = &db->aDb[pOp->p1];
3303 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003304 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003305 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003306 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003307 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003308 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003309 pDb->pSchema->schema_cookie = pOp->p3;
drh8257aa82017-07-26 19:59:13 +00003310 db->mDbFlags |= DBFLAG_SchemaChange;
danielk19770d19f7a2009-06-03 11:25:07 +00003311 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003312 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003313 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003314 }
drhfd426c62006-01-30 15:34:22 +00003315 if( pOp->p1==1 ){
3316 /* Invalidate all prepared statements whenever the TEMP database
3317 ** schema is changed. Ticket #1644 */
3318 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003319 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003320 }
drh9467abf2016-02-17 18:44:11 +00003321 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003322 break;
3323}
3324
drh98757152008-01-09 23:04:12 +00003325/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003326** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003327**
drhecdc7532001-09-23 02:35:53 +00003328** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003329** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003330** P3==0 means the main database, P3==1 means the database used for
3331** temporary tables, and P3>1 means used the corresponding attached
3332** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003333** values need not be contiguous but all P1 values should be small integers.
3334** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003335**
drh98757152008-01-09 23:04:12 +00003336** If P5!=0 then use the content of register P2 as the root page, not
3337** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003338**
drhb19a2bc2001-09-16 00:13:26 +00003339** There will be a read lock on the database whenever there is an
3340** open cursor. If the database was unlocked prior to this instruction
3341** then a read lock is acquired as part of this instruction. A read
3342** lock allows other processes to read the database but prohibits
3343** any other process from modifying the database. The read lock is
3344** released when all cursors are closed. If this instruction attempts
3345** to get a read lock but fails, the script terminates with an
3346** SQLITE_BUSY error code.
3347**
danielk1977d336e222009-02-20 10:58:41 +00003348** The P4 value may be either an integer (P4_INT32) or a pointer to
3349** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3350** structure, then said structure defines the content and collating
3351** sequence of the index being opened. Otherwise, if P4 is an integer
3352** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003353**
drh35263192014-07-22 20:02:19 +00003354** See also: OpenWrite, ReopenIdx
3355*/
3356/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3357** Synopsis: root=P2 iDb=P3
3358**
3359** The ReopenIdx opcode works exactly like ReadOpen except that it first
3360** checks to see if the cursor on P1 is already open with a root page
3361** number of P2 and if it is this opcode becomes a no-op. In other words,
3362** if the cursor is already open, do not reopen it.
3363**
3364** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3365** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3366** every other ReopenIdx or OpenRead for the same cursor number.
3367**
3368** See the OpenRead opcode documentation for additional information.
drh5e00f6c2001-09-13 13:46:56 +00003369*/
drh98757152008-01-09 23:04:12 +00003370/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003371** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003372**
3373** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003374** page is P2. Or if P5!=0 use the content of register P2 to find the
3375** root page.
drhecdc7532001-09-23 02:35:53 +00003376**
danielk1977d336e222009-02-20 10:58:41 +00003377** The P4 value may be either an integer (P4_INT32) or a pointer to
3378** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3379** structure, then said structure defines the content and collating
3380** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003381** value, it is set to the number of columns in the table, or to the
3382** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003383**
drh001bbcb2003-03-19 03:14:00 +00003384** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003385** in read/write mode. For a given table, there can be one or more read-only
3386** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003387**
drh001bbcb2003-03-19 03:14:00 +00003388** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003389*/
drh35263192014-07-22 20:02:19 +00003390case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003391 int nField;
3392 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003393 int p2;
3394 int iDb;
drhf57b3392001-10-08 13:22:32 +00003395 int wrFlag;
3396 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003397 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003398 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003399
drhe0997b32015-03-20 14:57:50 +00003400 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003401 assert( pOp->p4type==P4_KEYINFO );
3402 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003403 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003404 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003405 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003406 }
3407 /* If the cursor is not currently open or is open on a different
3408 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003409case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003410case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003411
drhe0997b32015-03-20 14:57:50 +00003412 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003413 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003414 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3415 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003416
danfa401de2009-10-16 14:55:03 +00003417 if( p->expired ){
drh47b7fc72014-11-11 01:33:57 +00003418 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003419 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003420 }
3421
drh856c1032009-06-02 15:21:42 +00003422 nField = 0;
3423 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003424 p2 = pOp->p2;
3425 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003426 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003427 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003428 pDb = &db->aDb[iDb];
3429 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003430 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003431 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003432 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3433 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003434 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003435 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3436 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003437 }
3438 }else{
3439 wrFlag = 0;
3440 }
dan428c2182012-08-06 18:50:11 +00003441 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003442 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003443 assert( p2<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003444 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003445 assert( memIsValid(pIn2) );
3446 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003447 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003448 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00003449 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00003450 ** that opcode will always set the p2 value to 2 or more or else fail.
3451 ** If there were a failure, the prepared statement would have halted
3452 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003453 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003454 }
danielk1977d336e222009-02-20 10:58:41 +00003455 if( pOp->p4type==P4_KEYINFO ){
3456 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003457 assert( pKeyInfo->enc==ENC(db) );
3458 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00003459 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00003460 }else if( pOp->p4type==P4_INT32 ){
3461 nField = pOp->p4.i;
3462 }
drh653b82a2009-06-22 11:10:47 +00003463 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003464 assert( nField>=0 );
3465 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003466 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003467 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003468 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003469 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003470 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003471#ifdef SQLITE_DEBUG
3472 pCur->wrFlag = wrFlag;
3473#endif
drhc960dcb2015-11-20 19:22:01 +00003474 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003475 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003476 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003477 ** SQLite used to check if the root-page flags were sane at this point
3478 ** and report database corruption if they were not, but this check has
3479 ** since moved into the btree layer. */
3480 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003481
3482open_cursor_set_hints:
3483 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3484 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003485 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003486#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003487 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3488#endif
drhc960dcb2015-11-20 19:22:01 +00003489 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003490 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003491 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003492 break;
3493}
3494
drhe08e8d62017-05-01 15:15:41 +00003495/* Opcode: OpenDup P1 P2 * * *
3496**
3497** Open a new cursor P1 that points to the same ephemeral table as
3498** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
3499** opcode. Only ephemeral cursors may be duplicated.
3500**
3501** Duplicate ephemeral cursors are used for self-joins of materialized views.
3502*/
3503case OP_OpenDup: {
3504 VdbeCursor *pOrig; /* The original cursor to be duplicated */
3505 VdbeCursor *pCx; /* The new cursor */
3506
3507 pOrig = p->apCsr[pOp->p2];
3508 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
3509
3510 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
3511 if( pCx==0 ) goto no_mem;
3512 pCx->nullRow = 1;
3513 pCx->isEphemeral = 1;
3514 pCx->pKeyInfo = pOrig->pKeyInfo;
3515 pCx->isTable = pOrig->isTable;
3516 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
3517 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00003518 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
3519 ** opened for a database. Since there is already an open cursor when this
3520 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
3521 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00003522 break;
3523}
3524
3525
drh2a5d9902011-08-26 00:34:45 +00003526/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003527** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003528**
drhb9bb7c12006-06-11 23:41:55 +00003529** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003530** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003531** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003532** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003533**
drh25d3adb2010-04-05 15:11:08 +00003534** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003535** The cursor points to a BTree table if P4==0 and to a BTree index
3536** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003537** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003538**
drh2a5d9902011-08-26 00:34:45 +00003539** The P5 parameter can be a mask of the BTREE_* flags defined
3540** in btree.h. These flags control aspects of the operation of
3541** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3542** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003543*/
drha21a64d2010-04-06 22:33:55 +00003544/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003545** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003546**
3547** This opcode works the same as OP_OpenEphemeral. It has a
3548** different name to distinguish its use. Tables created using
3549** by this opcode will be used for automatically created transient
3550** indices in joins.
3551*/
3552case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003553case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003554 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003555 KeyInfo *pKeyInfo;
3556
drhd4187c72010-08-30 22:15:45 +00003557 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003558 SQLITE_OPEN_READWRITE |
3559 SQLITE_OPEN_CREATE |
3560 SQLITE_OPEN_EXCLUSIVE |
3561 SQLITE_OPEN_DELETEONCLOSE |
3562 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003563 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003564 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003565 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003566 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003567 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003568 pCx->isEphemeral = 1;
drhfbd8cbd2016-12-10 12:58:15 +00003569 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
drhd4187c72010-08-30 22:15:45 +00003570 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003571 if( rc==SQLITE_OK ){
drhfbd8cbd2016-12-10 12:58:15 +00003572 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
drh5e00f6c2001-09-13 13:46:56 +00003573 }
3574 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003575 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003576 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003577 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003578 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003579 */
drhfbd8cbd2016-12-10 12:58:15 +00003580 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003581 int pgno;
drh66a51672008-01-03 00:01:23 +00003582 assert( pOp->p4type==P4_KEYINFO );
drhfbd8cbd2016-12-10 12:58:15 +00003583 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003584 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003585 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003586 assert( pKeyInfo->db==db );
3587 assert( pKeyInfo->enc==ENC(db) );
drhfbd8cbd2016-12-10 12:58:15 +00003588 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003589 pKeyInfo, pCx->uc.pCursor);
drhc6b52df2002-01-04 03:09:29 +00003590 }
drhf0863fe2005-06-12 21:35:51 +00003591 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003592 }else{
drhfbd8cbd2016-12-10 12:58:15 +00003593 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003594 0, pCx->uc.pCursor);
drhf0863fe2005-06-12 21:35:51 +00003595 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003596 }
drh5e00f6c2001-09-13 13:46:56 +00003597 }
drh9467abf2016-02-17 18:44:11 +00003598 if( rc ) goto abort_due_to_error;
drhd4187c72010-08-30 22:15:45 +00003599 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003600 break;
3601}
3602
danfad9f9a2014-04-01 18:41:51 +00003603/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003604**
3605** This opcode works like OP_OpenEphemeral except that it opens
3606** a transient index that is specifically designed to sort large
3607** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003608**
3609** If argument P3 is non-zero, then it indicates that the sorter may
3610** assume that a stable sort considering the first P3 fields of each
3611** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003612*/
drhca892a72011-09-03 00:17:51 +00003613case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003614 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003615
drh399af1d2013-11-20 17:25:55 +00003616 assert( pOp->p1>=0 );
3617 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003618 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003619 if( pCx==0 ) goto no_mem;
3620 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003621 assert( pCx->pKeyInfo->db==db );
3622 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003623 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003624 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003625 break;
3626}
3627
dan78d58432014-03-25 15:04:07 +00003628/* Opcode: SequenceTest P1 P2 * * *
3629** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3630**
3631** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3632** to P2. Regardless of whether or not the jump is taken, increment the
3633** the sequence value.
3634*/
3635case OP_SequenceTest: {
3636 VdbeCursor *pC;
3637 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3638 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003639 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003640 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003641 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003642 }
drh5e00f6c2001-09-13 13:46:56 +00003643 break;
3644}
3645
drh5f612292014-02-08 23:20:32 +00003646/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003647** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003648**
3649** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003650** row of data. The content of that one row is the content of memory
3651** register P2. In other words, cursor P1 becomes an alias for the
3652** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003653**
drh2d8d7ce2010-02-15 15:17:05 +00003654** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003655** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003656** individual columns using the OP_Column opcode. The OP_Column opcode
3657** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003658**
3659** P3 is the number of fields in the records that will be stored by
3660** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003661*/
drh9cbf3422008-01-17 16:22:13 +00003662case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003663 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003664
drh653b82a2009-06-22 11:10:47 +00003665 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003666 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003667 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003668 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003669 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003670 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003671 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00003672 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
3673 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
3674 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
3675 ** which is a performance optimization */
3676 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00003677 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003678 break;
3679}
3680
drh98757152008-01-09 23:04:12 +00003681/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003682**
3683** Close a cursor previously opened as P1. If P1 is not
3684** currently open, this instruction is a no-op.
3685*/
drh9cbf3422008-01-17 16:22:13 +00003686case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003687 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3688 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3689 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003690 break;
3691}
3692
drh97bae792015-06-05 15:59:57 +00003693#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3694/* Opcode: ColumnsUsed P1 * * P4 *
3695**
3696** This opcode (which only exists if SQLite was compiled with
3697** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3698** table or index for cursor P1 are used. P4 is a 64-bit integer
3699** (P4_INT64) in which the first 63 bits are one for each of the
3700** first 63 columns of the table or index that are actually used
3701** by the cursor. The high-order bit is set if any column after
3702** the 64th is used.
3703*/
3704case OP_ColumnsUsed: {
3705 VdbeCursor *pC;
3706 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003707 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003708 pC->maskUsed = *(u64*)pOp->p4.pI64;
3709 break;
3710}
3711#endif
3712
drh8af3f772014-07-25 18:01:06 +00003713/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003714** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003715**
danielk1977b790c6c2008-04-18 10:25:24 +00003716** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003717** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003718** to an SQL index, then P3 is the first in an array of P4 registers
3719** that are used as an unpacked index key.
3720**
3721** Reposition cursor P1 so that it points to the smallest entry that
3722** is greater than or equal to the key value. If there are no records
3723** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003724**
drhb1d607d2015-11-05 22:30:54 +00003725** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3726** opcode will always land on a record that equally equals the key, or
3727** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3728** opcode must be followed by an IdxLE opcode with the same arguments.
3729** The IdxLE opcode will be skipped if this opcode succeeds, but the
3730** IdxLE opcode will be used on subsequent loop iterations.
3731**
drh8af3f772014-07-25 18:01:06 +00003732** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003733** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003734** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003735**
drh935850e2014-05-24 17:15:15 +00003736** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003737*/
drh8af3f772014-07-25 18:01:06 +00003738/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003739** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003740**
danielk1977b790c6c2008-04-18 10:25:24 +00003741** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003742** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003743** to an SQL index, then P3 is the first in an array of P4 registers
3744** that are used as an unpacked index key.
3745**
3746** Reposition cursor P1 so that it points to the smallest entry that
3747** is greater than the key value. If there are no records greater than
3748** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003749**
drh8af3f772014-07-25 18:01:06 +00003750** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003751** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003752** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003753**
drh935850e2014-05-24 17:15:15 +00003754** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003755*/
drh8af3f772014-07-25 18:01:06 +00003756/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003757** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003758**
danielk1977b790c6c2008-04-18 10:25:24 +00003759** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003760** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003761** to an SQL index, then P3 is the first in an array of P4 registers
3762** that are used as an unpacked index key.
3763**
3764** Reposition cursor P1 so that it points to the largest entry that
3765** is less than the key value. If there are no records less than
3766** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003767**
drh8af3f772014-07-25 18:01:06 +00003768** This opcode leaves the cursor configured to move in reverse order,
3769** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003770** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003771**
drh935850e2014-05-24 17:15:15 +00003772** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003773*/
drh8af3f772014-07-25 18:01:06 +00003774/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003775** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003776**
danielk1977b790c6c2008-04-18 10:25:24 +00003777** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003778** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003779** to an SQL index, then P3 is the first in an array of P4 registers
3780** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003781**
danielk1977b790c6c2008-04-18 10:25:24 +00003782** Reposition cursor P1 so that it points to the largest entry that
3783** is less than or equal to the key value. If there are no records
3784** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003785**
drh8af3f772014-07-25 18:01:06 +00003786** This opcode leaves the cursor configured to move in reverse order,
3787** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003788** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003789**
drhb1d607d2015-11-05 22:30:54 +00003790** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3791** opcode will always land on a record that equally equals the key, or
3792** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3793** opcode must be followed by an IdxGE opcode with the same arguments.
3794** The IdxGE opcode will be skipped if this opcode succeeds, but the
3795** IdxGE opcode will be used on subsequent loop iterations.
3796**
drh935850e2014-05-24 17:15:15 +00003797** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003798*/
drh4a1d3652014-02-14 15:13:36 +00003799case OP_SeekLT: /* jump, in3 */
3800case OP_SeekLE: /* jump, in3 */
3801case OP_SeekGE: /* jump, in3 */
3802case OP_SeekGT: { /* jump, in3 */
drhb1d607d2015-11-05 22:30:54 +00003803 int res; /* Comparison result */
3804 int oc; /* Opcode */
3805 VdbeCursor *pC; /* The cursor to seek */
3806 UnpackedRecord r; /* The key to seek for */
3807 int nField; /* Number of columns or fields in the key */
3808 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003809 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003810
drh653b82a2009-06-22 11:10:47 +00003811 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003812 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003813 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003814 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003815 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003816 assert( OP_SeekLE == OP_SeekLT+1 );
3817 assert( OP_SeekGE == OP_SeekLT+2 );
3818 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003819 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003820 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003821 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003822 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003823 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003824#ifdef SQLITE_DEBUG
3825 pC->seekOp = pOp->opcode;
3826#endif
drhe0997b32015-03-20 14:57:50 +00003827
drh3da046d2013-11-11 03:24:11 +00003828 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003829 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003830 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3831 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003832
drh3da046d2013-11-11 03:24:11 +00003833 /* The input value in P3 might be of any type: integer, real, string,
3834 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003835 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003836 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003837 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003838 applyNumericAffinity(pIn3, 0);
3839 }
drh3da046d2013-11-11 03:24:11 +00003840 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003841
drh3da046d2013-11-11 03:24:11 +00003842 /* If the P3 value could not be converted into an integer without
3843 ** loss of information, then special processing is required... */
3844 if( (pIn3->flags & MEM_Int)==0 ){
3845 if( (pIn3->flags & MEM_Real)==0 ){
3846 /* If the P3 value cannot be converted into any kind of a number,
3847 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003848 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003849 break;
3850 }
drh959403f2008-12-12 17:56:16 +00003851
danaa1776f2013-11-26 18:22:59 +00003852 /* If the approximation iKey is larger than the actual real search
3853 ** term, substitute >= for > and < for <=. e.g. if the search term
3854 ** is 4.9 and the integer approximation 5:
3855 **
3856 ** (x > 4.9) -> (x >= 5)
3857 ** (x <= 4.9) -> (x < 5)
3858 */
drh74eaba42014-09-18 17:52:15 +00003859 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003860 assert( OP_SeekGE==(OP_SeekGT-1) );
3861 assert( OP_SeekLT==(OP_SeekLE-1) );
3862 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3863 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003864 }
3865
3866 /* If the approximation iKey is smaller than the actual real search
3867 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003868 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003869 assert( OP_SeekLE==(OP_SeekLT+1) );
3870 assert( OP_SeekGT==(OP_SeekGE+1) );
3871 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3872 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003873 }
drh3da046d2013-11-11 03:24:11 +00003874 }
drhc960dcb2015-11-20 19:22:01 +00003875 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003876 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003877 if( rc!=SQLITE_OK ){
3878 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003879 }
drhaa736092009-06-22 00:55:30 +00003880 }else{
drhd6b79462015-11-07 01:19:00 +00003881 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3882 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3883 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3884 */
drhc960dcb2015-11-20 19:22:01 +00003885 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00003886 eqOnly = 1;
3887 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3888 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3889 assert( pOp[1].p1==pOp[0].p1 );
3890 assert( pOp[1].p2==pOp[0].p2 );
3891 assert( pOp[1].p3==pOp[0].p3 );
3892 assert( pOp[1].p4.i==pOp[0].p4.i );
3893 }
3894
drh3da046d2013-11-11 03:24:11 +00003895 nField = pOp->p4.i;
3896 assert( pOp->p4type==P4_INT32 );
3897 assert( nField>0 );
3898 r.pKeyInfo = pC->pKeyInfo;
3899 r.nField = (u16)nField;
3900
3901 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003902 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003903 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003904 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003905 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003906 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003907 */
dan1fed5da2014-02-25 21:01:25 +00003908 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3909 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3910 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3911 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3912 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003913
3914 r.aMem = &aMem[pOp->p3];
3915#ifdef SQLITE_DEBUG
3916 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3917#endif
drh70528d72015-11-05 20:25:09 +00003918 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00003919 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00003920 if( rc!=SQLITE_OK ){
3921 goto abort_due_to_error;
3922 }
drhb1d607d2015-11-05 22:30:54 +00003923 if( eqOnly && r.eqSeen==0 ){
3924 assert( res!=0 );
3925 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00003926 }
drh3da046d2013-11-11 03:24:11 +00003927 }
3928 pC->deferredMoveto = 0;
3929 pC->cacheStatus = CACHE_STALE;
3930#ifdef SQLITE_TEST
3931 sqlite3_search_count++;
3932#endif
drh4a1d3652014-02-14 15:13:36 +00003933 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3934 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003935 res = 0;
drh2ab792e2017-05-30 18:34:07 +00003936 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
3937 if( rc!=SQLITE_OK ){
3938 if( rc==SQLITE_DONE ){
3939 rc = SQLITE_OK;
3940 res = 1;
3941 }else{
3942 goto abort_due_to_error;
3943 }
3944 }
drh3da046d2013-11-11 03:24:11 +00003945 }else{
3946 res = 0;
3947 }
3948 }else{
drh4a1d3652014-02-14 15:13:36 +00003949 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3950 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003951 res = 0;
drh2ab792e2017-05-30 18:34:07 +00003952 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
3953 if( rc!=SQLITE_OK ){
3954 if( rc==SQLITE_DONE ){
3955 rc = SQLITE_OK;
3956 res = 1;
3957 }else{
3958 goto abort_due_to_error;
3959 }
3960 }
drh3da046d2013-11-11 03:24:11 +00003961 }else{
3962 /* res might be negative because the table is empty. Check to
3963 ** see if this is the case.
3964 */
drhc960dcb2015-11-20 19:22:01 +00003965 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00003966 }
3967 }
drhb1d607d2015-11-05 22:30:54 +00003968seek_not_found:
drh3da046d2013-11-11 03:24:11 +00003969 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003970 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003971 if( res ){
drhf56fa462015-04-13 21:39:54 +00003972 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00003973 }else if( eqOnly ){
3974 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3975 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00003976 }
drh5e00f6c2001-09-13 13:46:56 +00003977 break;
3978}
dan71c57db2016-07-09 20:23:55 +00003979
drh8cff69d2009-11-12 19:59:44 +00003980/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003981** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003982**
drh8cff69d2009-11-12 19:59:44 +00003983** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3984** P4>0 then register P3 is the first of P4 registers that form an unpacked
3985** record.
3986**
3987** Cursor P1 is on an index btree. If the record identified by P3 and P4
3988** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003989** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003990**
drhcefc87f2014-08-01 01:40:33 +00003991** This operation leaves the cursor in a state where it can be
3992** advanced in the forward direction. The Next instruction will work,
3993** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00003994**
drh6f225d02013-10-26 13:36:51 +00003995** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003996*/
drh8cff69d2009-11-12 19:59:44 +00003997/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003998** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003999**
drh8cff69d2009-11-12 19:59:44 +00004000** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4001** P4>0 then register P3 is the first of P4 registers that form an unpacked
4002** record.
4003**
4004** Cursor P1 is on an index btree. If the record identified by P3 and P4
4005** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4006** does contain an entry whose prefix matches the P3/P4 record then control
4007** falls through to the next instruction and P1 is left pointing at the
4008** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004009**
drh8af3f772014-07-25 18:01:06 +00004010** This operation leaves the cursor in a state where it cannot be
4011** advanced in either direction. In other words, the Next and Prev
4012** opcodes do not work after this operation.
4013**
drh6f225d02013-10-26 13:36:51 +00004014** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00004015*/
drh6f225d02013-10-26 13:36:51 +00004016/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004017** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004018**
4019** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4020** P4>0 then register P3 is the first of P4 registers that form an unpacked
4021** record.
4022**
4023** Cursor P1 is on an index btree. If the record identified by P3 and P4
4024** contains any NULL value, jump immediately to P2. If all terms of the
4025** record are not-NULL then a check is done to determine if any row in the
4026** P1 index btree has a matching key prefix. If there are no matches, jump
4027** immediately to P2. If there is a match, fall through and leave the P1
4028** cursor pointing to the matching row.
4029**
4030** This opcode is similar to OP_NotFound with the exceptions that the
4031** branch is always taken if any part of the search key input is NULL.
4032**
drh8af3f772014-07-25 18:01:06 +00004033** This operation leaves the cursor in a state where it cannot be
4034** advanced in either direction. In other words, the Next and Prev
4035** opcodes do not work after this operation.
4036**
drh6f225d02013-10-26 13:36:51 +00004037** See also: NotFound, Found, NotExists
4038*/
4039case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004040case OP_NotFound: /* jump, in3 */
4041case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004042 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004043 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004044 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004045 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004046 int res;
drha582b012016-12-21 19:45:54 +00004047 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004048 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004049 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004050
dan0ff297e2009-09-25 17:03:14 +00004051#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004052 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004053#endif
4054
drhaa736092009-06-22 00:55:30 +00004055 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004056 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004057 pC = p->apCsr[pOp->p1];
4058 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004059#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004060 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004061#endif
drh3c657212009-11-17 23:59:58 +00004062 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004063 assert( pC->eCurType==CURTYPE_BTREE );
4064 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004065 assert( pC->isTable==0 );
4066 if( pOp->p4.i>0 ){
4067 r.pKeyInfo = pC->pKeyInfo;
4068 r.nField = (u16)pOp->p4.i;
4069 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004070#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004071 for(ii=0; ii<r.nField; ii++){
4072 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004073 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004074 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004075 }
drh8aaf7bc2016-09-20 01:19:18 +00004076#endif
drh3da046d2013-11-11 03:24:11 +00004077 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004078 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004079 }else{
drhe46515b2017-05-19 22:51:00 +00004080 assert( pIn3->flags & MEM_Blob );
4081 rc = ExpandBlob(pIn3);
4082 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
4083 if( rc ) goto no_mem;
drha582b012016-12-21 19:45:54 +00004084 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004085 if( pIdxKey==0 ) goto no_mem;
drh3da046d2013-11-11 03:24:11 +00004086 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004087 }
dan1fed5da2014-02-25 21:01:25 +00004088 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004089 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004090 if( pOp->opcode==OP_NoConflict ){
4091 /* For the OP_NoConflict opcode, take the jump if any of the
4092 ** input fields are NULL, since any key with a NULL will not
4093 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004094 for(ii=0; ii<pIdxKey->nField; ii++){
4095 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004096 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004097 break;
drh6f225d02013-10-26 13:36:51 +00004098 }
4099 }
drh5e00f6c2001-09-13 13:46:56 +00004100 }
drhc960dcb2015-11-20 19:22:01 +00004101 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004102 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004103 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004104 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004105 }
4106 pC->seekResult = res;
4107 alreadyExists = (res==0);
4108 pC->nullRow = 1-alreadyExists;
4109 pC->deferredMoveto = 0;
4110 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004111 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004112 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004113 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004114 }else{
drhf56fa462015-04-13 21:39:54 +00004115 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4116 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004117 }
drh5e00f6c2001-09-13 13:46:56 +00004118 break;
4119}
4120
drheeb95652016-05-26 20:56:38 +00004121/* Opcode: SeekRowid P1 P2 P3 * *
4122** Synopsis: intkey=r[P3]
4123**
4124** P1 is the index of a cursor open on an SQL table btree (with integer
4125** keys). If register P3 does not contain an integer or if P1 does not
4126** contain a record with rowid P3 then jump immediately to P2.
4127** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4128** a record with rowid P3 then
4129** leave the cursor pointing at that record and fall through to the next
4130** instruction.
4131**
4132** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4133** the P3 register must be guaranteed to contain an integer value. With this
4134** opcode, register P3 might not contain an integer.
4135**
4136** The OP_NotFound opcode performs the same operation on index btrees
4137** (with arbitrary multi-value keys).
4138**
4139** This opcode leaves the cursor in a state where it cannot be advanced
4140** in either direction. In other words, the Next and Prev opcodes will
4141** not work following this opcode.
4142**
4143** See also: Found, NotFound, NoConflict, SeekRowid
4144*/
drh9cbf3422008-01-17 16:22:13 +00004145/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004146** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004147**
drh261c02d2013-10-25 14:46:15 +00004148** P1 is the index of a cursor open on an SQL table btree (with integer
4149** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004150** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4151** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4152** leave the cursor pointing at that record and fall through to the next
4153** instruction.
drh6b125452002-01-28 15:53:03 +00004154**
drheeb95652016-05-26 20:56:38 +00004155** The OP_SeekRowid opcode performs the same operation but also allows the
4156** P3 register to contain a non-integer value, in which case the jump is
4157** always taken. This opcode requires that P3 always contain an integer.
4158**
drh261c02d2013-10-25 14:46:15 +00004159** The OP_NotFound opcode performs the same operation on index btrees
4160** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004161**
drh8af3f772014-07-25 18:01:06 +00004162** This opcode leaves the cursor in a state where it cannot be advanced
4163** in either direction. In other words, the Next and Prev opcodes will
4164** not work following this opcode.
4165**
drheeb95652016-05-26 20:56:38 +00004166** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004167*/
drheeb95652016-05-26 20:56:38 +00004168case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004169 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004170 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004171 int res;
4172 u64 iKey;
4173
drh3c657212009-11-17 23:59:58 +00004174 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004175 if( (pIn3->flags & MEM_Int)==0 ){
4176 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
4177 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
4178 }
4179 /* Fall through into OP_NotExists */
4180case OP_NotExists: /* jump, in3 */
4181 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00004182 assert( pIn3->flags & MEM_Int );
4183 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4184 pC = p->apCsr[pOp->p1];
4185 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004186#ifdef SQLITE_DEBUG
4187 pC->seekOp = 0;
4188#endif
drhaa736092009-06-22 00:55:30 +00004189 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004190 assert( pC->eCurType==CURTYPE_BTREE );
4191 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004192 assert( pCrsr!=0 );
4193 res = 0;
4194 iKey = pIn3->u.i;
4195 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004196 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004197 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004198 pC->nullRow = 0;
4199 pC->cacheStatus = CACHE_STALE;
4200 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004201 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004202 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004203 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004204 assert( rc==SQLITE_OK );
4205 if( pOp->p2==0 ){
4206 rc = SQLITE_CORRUPT_BKPT;
4207 }else{
4208 goto jump_to_p2;
4209 }
danc6157e12015-09-14 09:23:47 +00004210 }
drh9467abf2016-02-17 18:44:11 +00004211 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004212 break;
4213}
4214
drh4c583122008-01-04 22:01:03 +00004215/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004216** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004217**
drh4c583122008-01-04 22:01:03 +00004218** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004219** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004220** The sequence number on the cursor is incremented after this
4221** instruction.
drh4db38a72005-09-01 12:16:28 +00004222*/
drh27a348c2015-04-13 19:14:06 +00004223case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004224 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4225 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004226 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004227 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004228 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004229 break;
4230}
4231
4232
drh98757152008-01-09 23:04:12 +00004233/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004234** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004235**
drhf0863fe2005-06-12 21:35:51 +00004236** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004237** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004238** table that cursor P1 points to. The new record number is written
4239** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004240**
dan76d462e2009-08-30 11:42:51 +00004241** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4242** the largest previously generated record number. No new record numbers are
4243** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004244** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004245** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004246** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004247*/
drh27a348c2015-04-13 19:14:06 +00004248case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004249 i64 v; /* The new rowid */
4250 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4251 int res; /* Result of an sqlite3BtreeLast() */
4252 int cnt; /* Counter to limit the number of searches */
4253 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004254 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004255
drh856c1032009-06-02 15:21:42 +00004256 v = 0;
4257 res = 0;
drh27a348c2015-04-13 19:14:06 +00004258 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004259 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4260 pC = p->apCsr[pOp->p1];
4261 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004262 assert( pC->eCurType==CURTYPE_BTREE );
4263 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004264 {
drh5cf8e8c2002-02-19 22:42:05 +00004265 /* The next rowid or record number (different terms for the same
4266 ** thing) is obtained in a two-step algorithm.
4267 **
4268 ** First we attempt to find the largest existing rowid and add one
4269 ** to that. But if the largest existing rowid is already the maximum
4270 ** positive integer, we have to fall through to the second
4271 ** probabilistic algorithm
4272 **
4273 ** The second algorithm is to select a rowid at random and see if
4274 ** it already exists in the table. If it does not exist, we have
4275 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004276 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004277 */
drhaa736092009-06-22 00:55:30 +00004278 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004279
drh75f86a42005-02-17 00:03:06 +00004280#ifdef SQLITE_32BIT_ROWID
4281# define MAX_ROWID 0x7fffffff
4282#else
drhfe2093d2005-01-20 22:48:47 +00004283 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4284 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4285 ** to provide the constant while making all compilers happy.
4286 */
danielk197764202cf2008-11-17 15:31:47 +00004287# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004288#endif
drhfe2093d2005-01-20 22:48:47 +00004289
drh5cf8e8c2002-02-19 22:42:05 +00004290 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004291 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004292 if( rc!=SQLITE_OK ){
4293 goto abort_due_to_error;
4294 }
4295 if( res ){
4296 v = 1; /* IMP: R-61914-48074 */
4297 }else{
drhc960dcb2015-11-20 19:22:01 +00004298 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004299 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004300 if( v>=MAX_ROWID ){
4301 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004302 }else{
drhe0670b62014-02-12 21:31:12 +00004303 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004304 }
drh3fc190c2001-09-14 03:24:23 +00004305 }
drhe0670b62014-02-12 21:31:12 +00004306 }
drh205f48e2004-11-05 00:43:11 +00004307
4308#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004309 if( pOp->p3 ){
4310 /* Assert that P3 is a valid memory cell. */
4311 assert( pOp->p3>0 );
4312 if( p->pFrame ){
4313 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004314 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004315 assert( pOp->p3<=pFrame->nMem );
4316 pMem = &pFrame->aMem[pOp->p3];
4317 }else{
4318 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004319 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004320 pMem = &aMem[pOp->p3];
4321 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004322 }
drhe0670b62014-02-12 21:31:12 +00004323 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004324
drhe0670b62014-02-12 21:31:12 +00004325 REGISTER_TRACE(pOp->p3, pMem);
4326 sqlite3VdbeMemIntegerify(pMem);
4327 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4328 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004329 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004330 goto abort_due_to_error;
4331 }
4332 if( v<pMem->u.i+1 ){
4333 v = pMem->u.i + 1;
4334 }
4335 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004336 }
drhe0670b62014-02-12 21:31:12 +00004337#endif
drh5cf8e8c2002-02-19 22:42:05 +00004338 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004339 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004340 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004341 ** engine starts picking positive candidate ROWIDs at random until
4342 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004343 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4344 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004345 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004346 do{
4347 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004348 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004349 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004350 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004351 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004352 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004353 if( rc ) goto abort_due_to_error;
4354 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004355 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004356 goto abort_due_to_error;
4357 }
drh748a52c2010-09-01 11:50:08 +00004358 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004359 }
drha11846b2004-01-07 18:52:56 +00004360 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004361 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004362 }
drh4c583122008-01-04 22:01:03 +00004363 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004364 break;
4365}
4366
danielk19771f4aa332008-01-03 09:51:55 +00004367/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004368** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004369**
jplyon5a564222003-06-02 06:15:58 +00004370** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004371** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004372** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004373** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004374** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004375**
danielk19771f4aa332008-01-03 09:51:55 +00004376** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4377** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004378** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004379** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004380**
drheaf6ae22016-11-09 20:14:34 +00004381** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4382** run faster by avoiding an unnecessary seek on cursor P1. However,
4383** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4384** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004385**
4386** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4387** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4388** is part of an INSERT operation. The difference is only important to
4389** the update hook.
4390**
dan319eeb72011-03-19 08:38:50 +00004391** Parameter P4 may point to a Table structure, or may be NULL. If it is
4392** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4393** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004394**
drh93aed5a2008-01-16 17:46:38 +00004395** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4396** allocated, then ownership of P2 is transferred to the pseudo-cursor
4397** and register P2 becomes ephemeral. If the cursor is changed, the
4398** value of register P2 will then change. Make sure this does not
4399** cause any problems.)
4400**
drhf0863fe2005-06-12 21:35:51 +00004401** This instruction only works on tables. The equivalent instruction
4402** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004403*/
drhe05c9292009-10-29 13:48:10 +00004404/* Opcode: InsertInt P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00004405** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004406**
4407** This works exactly like OP_Insert except that the key is the
4408** integer value P3, not the value of the integer stored in register P3.
4409*/
4410case OP_Insert:
4411case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004412 Mem *pData; /* MEM cell holding data for the record to be inserted */
4413 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004414 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004415 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4416 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004417 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh74c33022016-03-30 12:56:55 +00004418 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh8eeb4462016-05-21 20:03:42 +00004419 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004420
drh74c33022016-03-30 12:56:55 +00004421 op = 0;
drha6c2ed92009-11-14 23:22:23 +00004422 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004423 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004424 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004425 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004426 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004427 assert( pC->eCurType==CURTYPE_BTREE );
4428 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004429 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004430 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004431 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004432
drhe05c9292009-10-29 13:48:10 +00004433 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004434 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004435 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004436 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004437 REGISTER_TRACE(pOp->p3, pKey);
drh8eeb4462016-05-21 20:03:42 +00004438 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004439 }else{
4440 assert( pOp->opcode==OP_InsertInt );
drh8eeb4462016-05-21 20:03:42 +00004441 x.nKey = pOp->p3;
drhe05c9292009-10-29 13:48:10 +00004442 }
4443
drh9b1c62d2011-03-30 21:04:43 +00004444 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004445 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004446 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004447 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004448 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
dan46c47d42011-03-01 18:42:07 +00004449 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drh74c33022016-03-30 12:56:55 +00004450 }else{
drh13795212017-01-31 15:27:04 +00004451 pTab = 0; /* Not needed. Silence a compiler warning. */
drh74c33022016-03-30 12:56:55 +00004452 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004453 }
4454
drh9b1c62d2011-03-30 21:04:43 +00004455#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004456 /* Invoke the pre-update hook, if any */
4457 if( db->xPreUpdateCallback
dan319eeb72011-03-19 08:38:50 +00004458 && pOp->p4type==P4_TABLE
drh92fe38e2014-10-14 13:41:32 +00004459 && !(pOp->p5 & OPFLAG_ISUPDATE)
dan46c47d42011-03-01 18:42:07 +00004460 ){
drh8eeb4462016-05-21 20:03:42 +00004461 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
dan46c47d42011-03-01 18:42:07 +00004462 }
dancb9a3642017-01-30 19:44:53 +00004463 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004464#endif
dan46c47d42011-03-01 18:42:07 +00004465
drha05a7222008-01-19 03:35:58 +00004466 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004467 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
dan21cd29a2017-10-23 16:03:54 +00004468 assert( pData->flags & (MEM_Blob|MEM_Str) );
4469 x.pData = pData->z;
4470 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00004471 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4472 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004473 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004474 }else{
drh8eeb4462016-05-21 20:03:42 +00004475 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004476 }
drh8eeb4462016-05-21 20:03:42 +00004477 x.pKey = 0;
4478 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004479 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004480 );
drha05a7222008-01-19 03:35:58 +00004481 pC->deferredMoveto = 0;
4482 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004483
drha05a7222008-01-19 03:35:58 +00004484 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004485 if( rc ) goto abort_due_to_error;
drhc556f3c2016-03-30 15:30:07 +00004486 if( db->xUpdateCallback && op ){
drh8eeb4462016-05-21 20:03:42 +00004487 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004488 }
drh5e00f6c2001-09-13 13:46:56 +00004489 break;
4490}
4491
dan438b8812015-09-15 15:55:15 +00004492/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004493**
drh5edc3122001-09-13 21:53:09 +00004494** Delete the record at which the P1 cursor is currently pointing.
4495**
drhe807bdb2016-01-21 17:06:33 +00004496** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4497** the cursor will be left pointing at either the next or the previous
4498** record in the table. If it is left pointing at the next record, then
4499** the next Next instruction will be a no-op. As a result, in this case
4500** it is ok to delete a record from within a Next loop. If
4501** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4502** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004503**
drhdef19e32016-01-27 16:26:25 +00004504** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4505** delete one of several associated with deleting a table row and all its
4506** associated index entries. Exactly one of those deletes is the "primary"
4507** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4508** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004509**
4510** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4511** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004512**
drh91fd4d42008-01-19 20:11:25 +00004513** P1 must not be pseudo-table. It has to be a real table with
4514** multiple rows.
4515**
drh5e769a52016-09-28 16:05:53 +00004516** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004517** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4518** have been positioned using OP_NotFound prior to invoking this opcode in
4519** this case. Specifically, if one is configured, the pre-update hook is
4520** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4521** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004522**
4523** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4524** of the memory cell that contains the value that the rowid of the row will
4525** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004526*/
drh9cbf3422008-01-17 16:22:13 +00004527case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004528 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004529 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004530 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004531 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004532
dan46c47d42011-03-01 18:42:07 +00004533 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004534 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4535 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004536 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004537 assert( pC->eCurType==CURTYPE_BTREE );
4538 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004539 assert( pC->deferredMoveto==0 );
drh9a65f2c2009-06-22 19:05:40 +00004540
drhb53a5a92014-10-12 22:37:22 +00004541#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004542 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4543 /* If p5 is zero, the seek operation that positioned the cursor prior to
4544 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4545 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004546 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh92fe38e2014-10-14 13:41:32 +00004547 assert( pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004548 }
4549#endif
drh91fd4d42008-01-19 20:11:25 +00004550
dan438b8812015-09-15 15:55:15 +00004551 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4552 ** the name of the db to pass as to it. Also set local pTab to a copy
4553 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4554 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4555 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004556 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004557 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004558 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004559 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004560 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004561 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004562 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004563 }
drh74c33022016-03-30 12:56:55 +00004564 }else{
4565 zDb = 0; /* Not needed. Silence a compiler warning. */
4566 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004567 }
dan46c47d42011-03-01 18:42:07 +00004568
drh9b1c62d2011-03-30 21:04:43 +00004569#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004570 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004571 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4572 assert( !(opflags & OPFLAG_ISUPDATE)
4573 || HasRowid(pTab)==0
4574 || (aMem[pOp->p3].flags & MEM_Int)
4575 );
dan46c47d42011-03-01 18:42:07 +00004576 sqlite3VdbePreUpdateHook(p, pC,
4577 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004578 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004579 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004580 );
4581 }
dan46c47d42011-03-01 18:42:07 +00004582 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004583#endif
drhb53a5a92014-10-12 22:37:22 +00004584
drhdef19e32016-01-27 16:26:25 +00004585 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4586 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004587 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004588 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004589
4590#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004591 if( p->pFrame==0 ){
4592 if( pC->isEphemeral==0
4593 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4594 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4595 ){
4596 nExtraDelete++;
4597 }
4598 if( pOp->p2 & OPFLAG_NCHANGE ){
4599 nExtraDelete--;
4600 }
drhb89aeb62016-01-27 15:49:32 +00004601 }
4602#endif
4603
drhc960dcb2015-11-20 19:22:01 +00004604 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004605 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004606 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004607 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004608
drh91fd4d42008-01-19 20:11:25 +00004609 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004610 if( opflags & OPFLAG_NCHANGE ){
4611 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004612 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004613 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004614 pC->movetoTarget);
4615 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004616 }
drh5e00f6c2001-09-13 13:46:56 +00004617 }
dan438b8812015-09-15 15:55:15 +00004618
rdcb0c374f2004-02-20 22:53:38 +00004619 break;
4620}
drhb7f1d9a2009-09-08 02:27:58 +00004621/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004622**
drhb7f1d9a2009-09-08 02:27:58 +00004623** The value of the change counter is copied to the database handle
4624** change counter (returned by subsequent calls to sqlite3_changes()).
4625** Then the VMs internal change counter resets to 0.
4626** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004627*/
drh9cbf3422008-01-17 16:22:13 +00004628case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004629 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004630 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004631 break;
4632}
4633
drh1153c7b2013-11-01 22:02:56 +00004634/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00004635** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004636**
drh1153c7b2013-11-01 22:02:56 +00004637** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004638** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004639** the sorter cursor currently points to. Only the first P4 fields
4640** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004641**
4642** If either P3 or the sorter contains a NULL in one of their significant
4643** fields (not counting the P4 fields at the end which are ignored) then
4644** the comparison is assumed to be equal.
4645**
4646** Fall through to next instruction if the two records compare equal to
4647** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004648*/
4649case OP_SorterCompare: {
4650 VdbeCursor *pC;
4651 int res;
drhac502322014-07-30 13:56:48 +00004652 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004653
4654 pC = p->apCsr[pOp->p1];
4655 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004656 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004657 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004658 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004659 res = 0;
drhac502322014-07-30 13:56:48 +00004660 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004661 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004662 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004663 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004664 break;
4665};
4666
drh6cf4a7d2014-10-13 13:00:58 +00004667/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004668** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004669**
4670** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004671** Then clear the column header cache on cursor P3.
4672**
4673** This opcode is normally use to move a record out of the sorter and into
4674** a register that is the source for a pseudo-table cursor created using
4675** OpenPseudo. That pseudo-table cursor is the one that is identified by
4676** parameter P3. Clearing the P3 column cache as part of this opcode saves
4677** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004678*/
4679case OP_SorterData: {
4680 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004681
dan5134d132011-09-02 10:31:11 +00004682 pOut = &aMem[pOp->p2];
4683 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004684 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004685 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004686 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004687 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004688 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004689 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004690 break;
4691}
4692
drhe7b554d2017-01-09 15:44:25 +00004693/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004694** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004695**
drh9057fc72016-11-25 19:32:32 +00004696** Write into register P2 the complete row content for the row at
4697** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00004698** There is no interpretation of the data.
4699** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004700** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004701**
drh9057fc72016-11-25 19:32:32 +00004702** If cursor P1 is an index, then the content is the key of the row.
4703** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00004704**
drhde4fcfd2008-01-19 23:50:26 +00004705** If the P1 cursor must be pointing to a valid row (not a NULL row)
4706** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00004707**
4708** If P3!=0 then this opcode is allowed to make an ephermeral pointer
4709** into the database page. That means that the content of the output
4710** register will be invalidated as soon as the cursor moves - including
4711** moves caused by other cursors that "save" the the current cursors
4712** position in order that they can write to the same table. If P3==0
4713** then a copy of the data is made into memory. P3!=0 is faster, but
4714** P3==0 is safer.
4715**
4716** If P3!=0 then the content of the P2 register is unsuitable for use
4717** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00004718** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00004719** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00004720*/
danielk1977a7a8e142008-02-13 18:25:27 +00004721case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004722 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004723 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004724 u32 n;
drh70ce3f02003-04-15 19:22:22 +00004725
drhe7b554d2017-01-09 15:44:25 +00004726 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00004727
drh653b82a2009-06-22 11:10:47 +00004728 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4729 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004730 assert( pC!=0 );
4731 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004732 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00004733 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004734 assert( pC->uc.pCursor!=0 );
4735 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004736
drh9057fc72016-11-25 19:32:32 +00004737 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00004738 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4739 ** that might invalidate the cursor.
4740 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00004741 ** would fail. Should this ever change (because of changes in the code
4742 ** generator) then the fix would be to insert a call to
4743 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004744 */
4745 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004746 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4747#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004748 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004749 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4750#endif
drh9a65f2c2009-06-22 19:05:40 +00004751
drha7c90c42016-06-04 20:37:10 +00004752 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00004753 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00004754 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00004755 }
drh722246e2014-10-07 23:02:24 +00004756 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00004757 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00004758 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00004759 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00004760 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004761 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004762 break;
4763}
4764
drh2133d822008-01-03 18:44:59 +00004765/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004766** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004767**
drh2133d822008-01-03 18:44:59 +00004768** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004769** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004770**
4771** P1 can be either an ordinary table or a virtual table. There used to
4772** be a separate OP_VRowid opcode for use with virtual tables, but this
4773** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004774*/
drh27a348c2015-04-13 19:14:06 +00004775case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004776 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004777 i64 v;
drh856c1032009-06-02 15:21:42 +00004778 sqlite3_vtab *pVtab;
4779 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004780
drh27a348c2015-04-13 19:14:06 +00004781 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004782 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4783 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004784 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004785 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004786 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004787 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004788 break;
4789 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004790 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004791#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004792 }else if( pC->eCurType==CURTYPE_VTAB ){
4793 assert( pC->uc.pVCur!=0 );
4794 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004795 pModule = pVtab->pModule;
4796 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004797 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004798 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004799 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004800#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004801 }else{
drhc960dcb2015-11-20 19:22:01 +00004802 assert( pC->eCurType==CURTYPE_BTREE );
4803 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004804 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004805 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004806 if( pC->nullRow ){
4807 pOut->flags = MEM_Null;
4808 break;
4809 }
drha7c90c42016-06-04 20:37:10 +00004810 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00004811 }
drh4c583122008-01-04 22:01:03 +00004812 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004813 break;
4814}
4815
drh9cbf3422008-01-17 16:22:13 +00004816/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004817**
4818** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004819** that occur while the cursor is on the null row will always
4820** write a NULL.
drh17f71932002-02-21 12:01:27 +00004821*/
drh9cbf3422008-01-17 16:22:13 +00004822case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004823 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004824
drh653b82a2009-06-22 11:10:47 +00004825 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4826 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004827 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004828 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004829 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00004830 if( pC->eCurType==CURTYPE_BTREE ){
4831 assert( pC->uc.pCursor!=0 );
4832 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00004833 }
drh17f71932002-02-21 12:01:27 +00004834 break;
4835}
4836
drh86b40df2017-08-01 19:53:43 +00004837/* Opcode: SeekEnd P1 * * * *
4838**
4839** Position cursor P1 at the end of the btree for the purpose of
4840** appending a new entry onto the btree.
4841**
4842** It is assumed that the cursor is used only for appending and so
4843** if the cursor is valid, then the cursor must already be pointing
4844** at the end of the btree and so no changes are made to
4845** the cursor.
4846*/
4847/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004848**
drh8af3f772014-07-25 18:01:06 +00004849** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004850** will refer to the last entry in the database table or index.
4851** If the table or index is empty and P2>0, then jump immediately to P2.
4852** If P2 is 0 or if the table or index is not empty, fall through
4853** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004854**
4855** This opcode leaves the cursor configured to move in reverse order,
4856** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004857** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00004858*/
drh86b40df2017-08-01 19:53:43 +00004859case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00004860case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004861 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004862 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004863 int res;
drh9562b552002-02-19 15:00:07 +00004864
drh653b82a2009-06-22 11:10:47 +00004865 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4866 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004867 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004868 assert( pC->eCurType==CURTYPE_BTREE );
4869 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00004870 res = 0;
drh3da046d2013-11-11 03:24:11 +00004871 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00004872#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00004873 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004874#endif
drh86b40df2017-08-01 19:53:43 +00004875 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00004876 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00004877 pC->seekResult = -1;
4878 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
4879 break;
4880 }
4881 }
4882 rc = sqlite3BtreeLast(pCrsr, &res);
4883 pC->nullRow = (u8)res;
4884 pC->deferredMoveto = 0;
4885 pC->cacheStatus = CACHE_STALE;
4886 if( rc ) goto abort_due_to_error;
4887 if( pOp->p2>0 ){
4888 VdbeBranchTaken(res!=0,2);
4889 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00004890 }
4891 break;
4892}
4893
drh5e98e832017-02-17 19:24:06 +00004894/* Opcode: IfSmaller P1 P2 P3 * *
4895**
4896** Estimate the number of rows in the table P1. Jump to P2 if that
4897** estimate is less than approximately 2**(0.1*P3).
4898*/
4899case OP_IfSmaller: { /* jump */
4900 VdbeCursor *pC;
4901 BtCursor *pCrsr;
4902 int res;
4903 i64 sz;
4904
4905 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4906 pC = p->apCsr[pOp->p1];
4907 assert( pC!=0 );
4908 pCrsr = pC->uc.pCursor;
4909 assert( pCrsr );
4910 rc = sqlite3BtreeFirst(pCrsr, &res);
4911 if( rc ) goto abort_due_to_error;
4912 if( res==0 ){
4913 sz = sqlite3BtreeRowCountEst(pCrsr);
4914 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
4915 }
4916 VdbeBranchTaken(res!=0,2);
4917 if( res ) goto jump_to_p2;
4918 break;
4919}
4920
drh0342b1f2005-09-01 03:07:44 +00004921
drh6bd4dc62016-12-23 16:05:22 +00004922/* Opcode: SorterSort P1 P2 * * *
4923**
4924** After all records have been inserted into the Sorter object
4925** identified by P1, invoke this opcode to actually do the sorting.
4926** Jump to P2 if there are no records to be sorted.
4927**
4928** This opcode is an alias for OP_Sort and OP_Rewind that is used
4929** for Sorter objects.
4930*/
drh9cbf3422008-01-17 16:22:13 +00004931/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004932**
4933** This opcode does exactly the same thing as OP_Rewind except that
4934** it increments an undocumented global variable used for testing.
4935**
4936** Sorting is accomplished by writing records into a sorting index,
4937** then rewinding that index and playing it back from beginning to
4938** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4939** rewinding so that the global variable will be incremented and
4940** regression tests can determine whether or not the optimizer is
4941** correctly optimizing out sorts.
4942*/
drhc6aff302011-09-01 15:32:47 +00004943case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004944case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004945#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004946 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004947 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004948#endif
drh9b47ee32013-08-20 03:13:51 +00004949 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004950 /* Fall through into OP_Rewind */
4951}
drh9cbf3422008-01-17 16:22:13 +00004952/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004953**
drhf0863fe2005-06-12 21:35:51 +00004954** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004955** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00004956** If the table or index is empty, jump immediately to P2.
4957** If the table or index is not empty, fall through to the following
4958** instruction.
drh8af3f772014-07-25 18:01:06 +00004959**
4960** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004961** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004962** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00004963*/
drh9cbf3422008-01-17 16:22:13 +00004964case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004965 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004966 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004967 int res;
drh5e00f6c2001-09-13 13:46:56 +00004968
drh653b82a2009-06-22 11:10:47 +00004969 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4970 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004971 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004972 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004973 res = 1;
drh8af3f772014-07-25 18:01:06 +00004974#ifdef SQLITE_DEBUG
4975 pC->seekOp = OP_Rewind;
4976#endif
dan689ab892011-08-12 15:02:00 +00004977 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004978 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00004979 }else{
drhc960dcb2015-11-20 19:22:01 +00004980 assert( pC->eCurType==CURTYPE_BTREE );
4981 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00004982 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004983 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004984 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004985 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00004986 }
drh9467abf2016-02-17 18:44:11 +00004987 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00004988 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004989 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004990 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004991 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004992 break;
4993}
4994
drh0fd61352014-02-07 02:29:45 +00004995/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004996**
4997** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004998** table or index. If there are no more key/value pairs then fall through
4999** to the following instruction. But if the cursor advance was successful,
5000** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005001**
drh5dad9a32014-07-25 18:37:42 +00005002** The Next opcode is only valid following an SeekGT, SeekGE, or
5003** OP_Rewind opcode used to position the cursor. Next is not allowed
5004** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00005005**
drhf93cd942013-11-21 03:12:25 +00005006** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5007** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00005008**
drhe39a7322014-02-03 14:04:11 +00005009** The P3 value is a hint to the btree implementation. If P3==1, that
5010** means P1 is an SQL index and that this instruction could have been
5011** omitted if that index had been unique. P3 is usually 0. P3 is
5012** always either 0 or 1.
5013**
dana205a482011-08-27 18:48:57 +00005014** P4 is always of type P4_ADVANCE. The function pointer points to
5015** sqlite3BtreeNext().
5016**
drhafc266a2010-03-31 17:47:44 +00005017** If P5 is positive and the jump is taken, then event counter
5018** number P5-1 in the prepared statement is incremented.
5019**
drhf93cd942013-11-21 03:12:25 +00005020** See also: Prev, NextIfOpen
5021*/
drh0fd61352014-02-07 02:29:45 +00005022/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00005023**
drh5dad9a32014-07-25 18:37:42 +00005024** This opcode works just like Next except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00005025** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00005026*/
drh0fd61352014-02-07 02:29:45 +00005027/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005028**
5029** Back up cursor P1 so that it points to the previous key/data pair in its
5030** table or index. If there is no previous key/value pairs then fall through
5031** to the following instruction. But if the cursor backup was successful,
5032** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005033**
drh8af3f772014-07-25 18:01:06 +00005034**
drh5dad9a32014-07-25 18:37:42 +00005035** The Prev opcode is only valid following an SeekLT, SeekLE, or
5036** OP_Last opcode used to position the cursor. Prev is not allowed
5037** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005038**
drhf93cd942013-11-21 03:12:25 +00005039** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5040** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005041**
drhe39a7322014-02-03 14:04:11 +00005042** The P3 value is a hint to the btree implementation. If P3==1, that
5043** means P1 is an SQL index and that this instruction could have been
5044** omitted if that index had been unique. P3 is usually 0. P3 is
5045** always either 0 or 1.
5046**
dana205a482011-08-27 18:48:57 +00005047** P4 is always of type P4_ADVANCE. The function pointer points to
5048** sqlite3BtreePrevious().
5049**
drhafc266a2010-03-31 17:47:44 +00005050** If P5 is positive and the jump is taken, then event counter
5051** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005052*/
drh0fd61352014-02-07 02:29:45 +00005053/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00005054**
drh5dad9a32014-07-25 18:37:42 +00005055** This opcode works just like Prev except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00005056** open it behaves a no-op.
5057*/
drh6bd4dc62016-12-23 16:05:22 +00005058/* Opcode: SorterNext P1 P2 * * P5
5059**
5060** This opcode works just like OP_Next except that P1 must be a
5061** sorter object for which the OP_SorterSort opcode has been
5062** invoked. This opcode advances the cursor to the next sorted
5063** record, or jumps to P2 if there are no more sorted records.
5064*/
drhf93cd942013-11-21 03:12:25 +00005065case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005066 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00005067
drhf93cd942013-11-21 03:12:25 +00005068 pC = p->apCsr[pOp->p1];
5069 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00005070 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00005071 goto next_tail;
5072case OP_PrevIfOpen: /* jump */
5073case OP_NextIfOpen: /* jump */
5074 if( p->apCsr[pOp->p1]==0 ) break;
5075 /* Fall through */
5076case OP_Prev: /* jump */
5077case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005078 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005079 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005080 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00005081 assert( pC!=0 );
5082 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005083 assert( pC->eCurType==CURTYPE_BTREE );
drhf93cd942013-11-21 03:12:25 +00005084 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5085 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
5086 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
5087 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
drh8af3f772014-07-25 18:01:06 +00005088
5089 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
5090 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
5091 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
5092 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcefc87f2014-08-01 01:40:33 +00005093 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
drh8af3f772014-07-25 18:01:06 +00005094 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
5095 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
5096 || pC->seekOp==OP_Last );
5097
drh2ab792e2017-05-30 18:34:07 +00005098 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
drhf93cd942013-11-21 03:12:25 +00005099next_tail:
drha3460582008-07-11 21:02:53 +00005100 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00005101 VdbeBranchTaken(rc==SQLITE_OK,2);
5102 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00005103 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005104 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005105#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005106 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005107#endif
drhf56fa462015-04-13 21:39:54 +00005108 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005109 }
drh2ab792e2017-05-30 18:34:07 +00005110 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
5111 rc = SQLITE_OK;
5112 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00005113 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005114}
5115
drh9b4eaeb2016-11-09 00:10:33 +00005116/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005117** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005118**
drhef8662b2011-06-20 21:47:58 +00005119** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005120** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005121** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005122**
drhfb8c56f2016-11-09 01:19:25 +00005123** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005124** key of reg(P2). In that case, P3 is the index of the first register
5125** for the unpacked key. The availability of the unpacked key can sometimes
5126** be an optimization.
5127**
5128** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5129** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005130**
mistachkin21a919f2014-02-07 03:28:02 +00005131** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5132** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5133** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005134**
drheaf6ae22016-11-09 20:14:34 +00005135** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5136** run faster by avoiding an unnecessary seek on cursor P1. However,
5137** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5138** seeks on the cursor or if the most recent seek used a key equivalent
5139** to P2.
drh0fd61352014-02-07 02:29:45 +00005140**
drhf0863fe2005-06-12 21:35:51 +00005141** This instruction only works for indices. The equivalent instruction
5142** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005143*/
drhf013e202016-10-15 18:37:05 +00005144/* Opcode: SorterInsert P1 P2 * * *
5145** Synopsis: key=r[P2]
5146**
5147** Register P2 holds an SQL index key made using the
5148** MakeRecord instructions. This opcode writes that key
5149** into the sorter P1. Data for the entry is nil.
5150*/
drhca892a72011-09-03 00:17:51 +00005151case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005152case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005153 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005154 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005155
drh653b82a2009-06-22 11:10:47 +00005156 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5157 pC = p->apCsr[pOp->p1];
5158 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005159 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005160 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005161 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005162 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005163 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005164 assert( pC->isTable==0 );
5165 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005166 if( rc ) goto abort_due_to_error;
5167 if( pOp->opcode==OP_SorterInsert ){
5168 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5169 }else{
drh8eeb4462016-05-21 20:03:42 +00005170 x.nKey = pIn2->n;
5171 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005172 x.aMem = aMem + pOp->p3;
5173 x.nMem = (u16)pOp->p4.i;
5174 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005175 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005176 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5177 );
5178 assert( pC->deferredMoveto==0 );
5179 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005180 }
drh9467abf2016-02-17 18:44:11 +00005181 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005182 break;
5183}
5184
drhd1d38482008-10-07 23:46:38 +00005185/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005186** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005187**
drhe14006d2008-03-25 17:23:32 +00005188** The content of P3 registers starting at register P2 form
5189** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005190** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005191*/
drhe14006d2008-03-25 17:23:32 +00005192case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005193 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005194 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005195 int res;
5196 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005197
drhe14006d2008-03-25 17:23:32 +00005198 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005199 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005200 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5201 pC = p->apCsr[pOp->p1];
5202 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005203 assert( pC->eCurType==CURTYPE_BTREE );
5204 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005205 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005206 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005207 r.pKeyInfo = pC->pKeyInfo;
5208 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005209 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005210 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005211 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005212 if( rc ) goto abort_due_to_error;
5213 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005214 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005215 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005216 }
drh3da046d2013-11-11 03:24:11 +00005217 assert( pC->deferredMoveto==0 );
5218 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005219 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005220 break;
5221}
5222
drh170ad682017-06-02 15:44:22 +00005223/* Opcode: DeferredSeek P1 * P3 P4 *
5224** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00005225**
5226** P1 is an open index cursor and P3 is a cursor on the corresponding
5227** table. This opcode does a deferred seek of the P3 table cursor
5228** to the row that corresponds to the current row of P1.
5229**
5230** This is a deferred seek. Nothing actually happens until
5231** the cursor is used to read a record. That way, if no reads
5232** occur, no unnecessary I/O happens.
5233**
5234** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005235** one entry for each column in the P3 table. If array entry a(i)
5236** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005237** equivalent to performing the deferred seek and then reading column i
5238** from P1. This information is stored in P3 and used to redirect
5239** reads against P3 over to P1, thus possibly avoiding the need to
5240** seek and read cursor P3.
5241*/
drh2133d822008-01-03 18:44:59 +00005242/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005243** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005244**
drh2133d822008-01-03 18:44:59 +00005245** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005246** the end of the index key pointed to by cursor P1. This integer should be
5247** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005248**
drh9437bd22009-02-01 00:29:56 +00005249** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005250*/
drh170ad682017-06-02 15:44:22 +00005251case OP_DeferredSeek:
5252case OP_IdxRowid: { /* out2 */
5253 VdbeCursor *pC; /* The P1 index cursor */
5254 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
5255 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005256
drh653b82a2009-06-22 11:10:47 +00005257 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5258 pC = p->apCsr[pOp->p1];
5259 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005260 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005261 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005262 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005263 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005264 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5265
5266 /* The IdxRowid and Seek opcodes are combined because of the commonality
5267 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5268 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005269
5270 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005271 ** out from under the cursor. That will never happens for an IdxRowid
5272 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005273 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5274
drh3da046d2013-11-11 03:24:11 +00005275 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005276 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005277 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005278 if( rc!=SQLITE_OK ){
5279 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005280 }
drh170ad682017-06-02 15:44:22 +00005281 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00005282 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5283 pTabCur = p->apCsr[pOp->p3];
5284 assert( pTabCur!=0 );
5285 assert( pTabCur->eCurType==CURTYPE_BTREE );
5286 assert( pTabCur->uc.pCursor!=0 );
5287 assert( pTabCur->isTable );
5288 pTabCur->nullRow = 0;
5289 pTabCur->movetoTarget = rowid;
5290 pTabCur->deferredMoveto = 1;
5291 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5292 pTabCur->aAltMap = pOp->p4.ai;
5293 pTabCur->pAltCursor = pC;
5294 }else{
5295 pOut = out2Prerelease(p, pOp);
5296 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005297 }
5298 }else{
5299 assert( pOp->opcode==OP_IdxRowid );
5300 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005301 }
5302 break;
5303}
5304
danielk197761dd5832008-04-18 11:31:12 +00005305/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005306** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005307**
danielk197761dd5832008-04-18 11:31:12 +00005308** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005309** key that omits the PRIMARY KEY. Compare this key value against the index
5310** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5311** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005312**
danielk197761dd5832008-04-18 11:31:12 +00005313** If the P1 index entry is greater than or equal to the key value
5314** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005315*/
5316/* Opcode: IdxGT P1 P2 P3 P4 P5
5317** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005318**
drh4a1d3652014-02-14 15:13:36 +00005319** The P4 register values beginning with P3 form an unpacked index
5320** key that omits the PRIMARY KEY. Compare this key value against the index
5321** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5322** fields at the end.
5323**
5324** If the P1 index entry is greater than the key value
5325** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005326*/
drh3bb9b932010-08-06 02:10:00 +00005327/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005328** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005329**
danielk197761dd5832008-04-18 11:31:12 +00005330** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005331** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5332** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5333** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005334**
danielk197761dd5832008-04-18 11:31:12 +00005335** If the P1 index entry is less than the key value then jump to P2.
5336** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005337*/
drh4a1d3652014-02-14 15:13:36 +00005338/* Opcode: IdxLE P1 P2 P3 P4 P5
5339** Synopsis: key=r[P3@P4]
5340**
5341** The P4 register values beginning with P3 form an unpacked index
5342** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5343** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5344** ROWID on the P1 index.
5345**
5346** If the P1 index entry is less than or equal to the key value then jump
5347** to P2. Otherwise fall through to the next instruction.
5348*/
5349case OP_IdxLE: /* jump */
5350case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005351case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005352case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005353 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005354 int res;
5355 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005356
drh653b82a2009-06-22 11:10:47 +00005357 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5358 pC = p->apCsr[pOp->p1];
5359 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005360 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005361 assert( pC->eCurType==CURTYPE_BTREE );
5362 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005363 assert( pC->deferredMoveto==0 );
5364 assert( pOp->p5==0 || pOp->p5==1 );
5365 assert( pOp->p4type==P4_INT32 );
5366 r.pKeyInfo = pC->pKeyInfo;
5367 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005368 if( pOp->opcode<OP_IdxLT ){
5369 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005370 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005371 }else{
drh4a1d3652014-02-14 15:13:36 +00005372 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005373 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005374 }
5375 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005376#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00005377 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00005378#endif
drh2dc06482013-12-11 00:59:10 +00005379 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005380 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005381 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5382 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5383 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005384 res = -res;
5385 }else{
drh4a1d3652014-02-14 15:13:36 +00005386 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005387 res++;
5388 }
drh688852a2014-02-17 22:40:43 +00005389 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005390 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005391 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005392 break;
5393}
5394
drh98757152008-01-09 23:04:12 +00005395/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005396**
5397** Delete an entire database table or index whose root page in the database
5398** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005399**
drh98757152008-01-09 23:04:12 +00005400** The table being destroyed is in the main database file if P3==0. If
5401** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005402** that is used to store tables create using CREATE TEMPORARY TABLE.
5403**
drh205f48e2004-11-05 00:43:11 +00005404** If AUTOVACUUM is enabled then it is possible that another root page
5405** might be moved into the newly deleted root page in order to keep all
5406** root pages contiguous at the beginning of the database. The former
5407** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005408** is stored in register P2. If no page movement was required (because the
5409** table being dropped was already the last one in the database) then a
5410** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5411** is stored in register P2.
5412**
5413** This opcode throws an error if there are any active reader VMs when
5414** it is invoked. This is done to avoid the difficulty associated with
5415** updating existing cursors when a root page is moved in an AUTOVACUUM
5416** database. This error is thrown even if the database is not an AUTOVACUUM
5417** db in order to avoid introducing an incompatibility between autovacuum
5418** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005419**
drhb19a2bc2001-09-16 00:13:26 +00005420** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005421*/
drh27a348c2015-04-13 19:14:06 +00005422case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005423 int iMoved;
drh856c1032009-06-02 15:21:42 +00005424 int iDb;
drh3a949872012-09-18 13:20:13 +00005425
drh9e92a472013-06-27 17:40:30 +00005426 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005427 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005428 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005429 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005430 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005431 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005432 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005433 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005434 }else{
drh856c1032009-06-02 15:21:42 +00005435 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005436 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005437 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005438 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005439 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005440 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005441 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005442#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005443 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005444 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5445 /* All OP_Destroy operations occur on the same btree */
5446 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5447 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005448 }
drh3765df42006-06-28 18:18:09 +00005449#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005450 }
drh5e00f6c2001-09-13 13:46:56 +00005451 break;
5452}
5453
danielk1977c7af4842008-10-27 13:59:33 +00005454/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005455**
5456** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005457** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005458** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005459**
drhf57b3392001-10-08 13:22:32 +00005460** The table being clear is in the main database file if P2==0. If
5461** P2==1 then the table to be clear is in the auxiliary database file
5462** that is used to store tables create using CREATE TEMPORARY TABLE.
5463**
shanebe217792009-03-05 04:20:31 +00005464** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005465** intkey table (an SQL table, not an index). In this case the row change
5466** count is incremented by the number of rows in the table being cleared.
5467** If P3 is greater than zero, then the value stored in register P3 is
5468** also incremented by the number of rows in the table being cleared.
5469**
drhb19a2bc2001-09-16 00:13:26 +00005470** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005471*/
drh9cbf3422008-01-17 16:22:13 +00005472case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005473 int nChange;
5474
5475 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005476 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005477 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005478 rc = sqlite3BtreeClearTable(
5479 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5480 );
5481 if( pOp->p3 ){
5482 p->nChange += nChange;
5483 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005484 assert( memIsValid(&aMem[pOp->p3]) );
5485 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005486 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005487 }
5488 }
drh9467abf2016-02-17 18:44:11 +00005489 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005490 break;
5491}
5492
drh65ea12c2014-03-19 17:41:36 +00005493/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005494**
drh65ea12c2014-03-19 17:41:36 +00005495** Delete all contents from the ephemeral table or sorter
5496** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005497**
drh65ea12c2014-03-19 17:41:36 +00005498** This opcode only works for cursors used for sorting and
5499** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005500*/
drh65ea12c2014-03-19 17:41:36 +00005501case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005502 VdbeCursor *pC;
5503
5504 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5505 pC = p->apCsr[pOp->p1];
5506 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005507 if( isSorter(pC) ){
5508 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005509 }else{
drhc960dcb2015-11-20 19:22:01 +00005510 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005511 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005512 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005513 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005514 }
drh079a3072014-03-19 14:10:55 +00005515 break;
5516}
5517
drh0f3f7662017-08-18 14:34:28 +00005518/* Opcode: CreateBtree P1 P2 P3 * *
5519** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00005520**
drh0f3f7662017-08-18 14:34:28 +00005521** Allocate a new b-tree in the main database file if P1==0 or in the
5522** TEMP database file if P1==1 or in an attached database if
5523** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
5524** it must be 2 (BTREE_BLOBKEY) for a index or WITHOUT ROWID table.
5525** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00005526*/
drh0f3f7662017-08-18 14:34:28 +00005527case OP_CreateBtree: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005528 int pgno;
drh234c39d2004-07-24 03:30:47 +00005529 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005530
drh27a348c2015-04-13 19:14:06 +00005531 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005532 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00005533 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00005534 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005535 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005536 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005537 pDb = &db->aDb[pOp->p1];
5538 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00005539 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00005540 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005541 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005542 break;
5543}
5544
drh4a54bb52017-02-18 15:58:52 +00005545/* Opcode: SqlExec * * * P4 *
5546**
5547** Run the SQL statement or statements specified in the P4 string.
5548*/
5549case OP_SqlExec: {
drhbce04142017-02-23 00:58:36 +00005550 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005551 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005552 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005553 if( rc ) goto abort_due_to_error;
5554 break;
5555}
5556
drh22645842011-03-24 01:34:03 +00005557/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005558**
5559** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005560** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005561**
5562** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005563** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005564*/
drh9cbf3422008-01-17 16:22:13 +00005565case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005566 int iDb;
5567 const char *zMaster;
5568 char *zSql;
5569 InitData initData;
5570
drhbdaec522011-04-04 00:14:43 +00005571 /* Any prepared statement that invokes this opcode will hold mutexes
5572 ** on every btree. This is a prerequisite for invoking
5573 ** sqlite3InitCallback().
5574 */
5575#ifdef SQLITE_DEBUG
5576 for(iDb=0; iDb<db->nDb; iDb++){
5577 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5578 }
5579#endif
drhbdaec522011-04-04 00:14:43 +00005580
drh856c1032009-06-02 15:21:42 +00005581 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005582 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005583 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005584 /* Used to be a conditional */ {
drhe0a04a32016-12-16 01:00:21 +00005585 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005586 initData.db = db;
5587 initData.iDb = pOp->p1;
5588 initData.pzErrMsg = &p->zErrMsg;
5589 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005590 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005591 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005592 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005593 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005594 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005595 assert( db->init.busy==0 );
5596 db->init.busy = 1;
5597 initData.rc = SQLITE_OK;
5598 assert( !db->mallocFailed );
5599 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5600 if( rc==SQLITE_OK ) rc = initData.rc;
drhdbd6a7d2017-04-05 12:39:49 +00005601 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005602 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005603 }
drh3c23a882007-01-09 14:01:13 +00005604 }
drh9467abf2016-02-17 18:44:11 +00005605 if( rc ){
5606 sqlite3ResetAllSchemasOfConnection(db);
5607 if( rc==SQLITE_NOMEM ){
5608 goto no_mem;
5609 }
5610 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005611 }
drh234c39d2004-07-24 03:30:47 +00005612 break;
5613}
5614
drh8bfdf722009-06-19 14:06:03 +00005615#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005616/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005617**
5618** Read the sqlite_stat1 table for database P1 and load the content
5619** of that table into the internal index hash table. This will cause
5620** the analysis to be used when preparing all subsequent queries.
5621*/
drh9cbf3422008-01-17 16:22:13 +00005622case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005623 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5624 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005625 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005626 break;
5627}
drh8bfdf722009-06-19 14:06:03 +00005628#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005629
drh98757152008-01-09 23:04:12 +00005630/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005631**
5632** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005633** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005634** is dropped from disk (using the Destroy opcode) in order to keep
5635** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005636** schema consistent with what is on disk.
5637*/
drh9cbf3422008-01-17 16:22:13 +00005638case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005639 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005640 break;
5641}
5642
drh98757152008-01-09 23:04:12 +00005643/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005644**
5645** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005646** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005647** is dropped from disk (using the Destroy opcode)
5648** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005649** schema consistent with what is on disk.
5650*/
drh9cbf3422008-01-17 16:22:13 +00005651case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005652 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005653 break;
5654}
5655
drh98757152008-01-09 23:04:12 +00005656/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005657**
5658** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005659** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005660** is dropped from disk (using the Destroy opcode) in order to keep
5661** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005662** schema consistent with what is on disk.
5663*/
drh9cbf3422008-01-17 16:22:13 +00005664case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005665 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005666 break;
5667}
5668
drh234c39d2004-07-24 03:30:47 +00005669
drhb7f91642004-10-31 02:22:47 +00005670#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005671/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005672**
drh98757152008-01-09 23:04:12 +00005673** Do an analysis of the currently open database. Store in
5674** register P1 the text of an error message describing any problems.
5675** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005676**
drh66accfc2017-02-22 18:04:42 +00005677** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005678** At most reg(P3) errors will be reported.
5679** In other words, the analysis stops as soon as reg(P1) errors are
5680** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005681**
drh98968b22016-03-15 22:00:39 +00005682** The root page numbers of all tables in the database are integers
5683** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005684**
drh98757152008-01-09 23:04:12 +00005685** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005686** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005687**
drh1dcdbc02007-01-27 02:24:54 +00005688** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005689*/
drhaaab5722002-02-19 13:39:21 +00005690case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005691 int nRoot; /* Number of tables to check. (Number of root pages.) */
5692 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005693 int nErr; /* Number of errors reported */
5694 char *z; /* Text of the error report */
5695 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005696
drh1713afb2013-06-28 01:24:57 +00005697 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005698 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005699 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005700 assert( nRoot>0 );
drhb5c10632017-09-21 00:49:15 +00005701 assert( aRoot[0]==nRoot );
drh9f6168b2016-03-19 23:32:58 +00005702 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005703 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005704 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005705 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005706 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005707 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005708 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drhb5c10632017-09-21 00:49:15 +00005709 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00005710 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005711 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005712 if( nErr==0 ){
5713 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005714 }else if( z==0 ){
5715 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005716 }else{
drh66accfc2017-02-22 18:04:42 +00005717 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005718 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005719 }
drhb7654112008-01-12 12:48:07 +00005720 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005721 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005722 break;
5723}
drhb7f91642004-10-31 02:22:47 +00005724#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005725
drh3d4501e2008-12-04 20:40:10 +00005726/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005727** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005728**
drhbb6783b2017-04-29 18:02:49 +00005729** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00005730** held in register P1.
5731**
5732** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005733*/
drh93952eb2009-11-13 19:43:43 +00005734case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005735 pIn1 = &aMem[pOp->p1];
5736 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005737 assert( (pIn2->flags & MEM_Int)!=0 );
5738 if( (pIn1->flags & MEM_RowSet)==0 ){
5739 sqlite3VdbeMemSetRowSet(pIn1);
5740 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005741 }
drh93952eb2009-11-13 19:43:43 +00005742 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005743 break;
5744}
5745
5746/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005747** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005748**
drhbb6783b2017-04-29 18:02:49 +00005749** Extract the smallest value from the RowSet object in P1
5750** and put that value into register P3.
5751** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00005752** unchanged and jump to instruction P2.
5753*/
drh93952eb2009-11-13 19:43:43 +00005754case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005755 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005756
drh3c657212009-11-17 23:59:58 +00005757 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005758 if( (pIn1->flags & MEM_RowSet)==0
5759 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005760 ){
5761 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005762 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005763 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005764 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005765 }else{
5766 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005767 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005768 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005769 }
drh49afe3a2013-07-10 03:05:14 +00005770 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005771}
5772
drh1b26c7c2009-04-22 02:15:47 +00005773/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005774** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005775**
drhade97602009-04-21 15:05:18 +00005776** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005777** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005778** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005779** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005780** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005781**
drhbb6783b2017-04-29 18:02:49 +00005782** The RowSet object is optimized for the case where sets of integers
5783** are inserted in distinct phases, which each set contains no duplicates.
5784** Each set is identified by a unique P4 value. The first set
5785** must have P4==0, the final set must have P4==-1, and for all other sets
5786** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00005787**
5788** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00005789** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005790** (b) when P4==-1 there is no need to insert the value, as it will
5791** never be tested for, and (c) when a value that is part of set X is
5792** inserted, there is no need to search to see if the same value was
5793** previously inserted as part of set X (only if it was previously
5794** inserted as part of some other set).
5795*/
drh1b26c7c2009-04-22 02:15:47 +00005796case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005797 int iSet;
5798 int exists;
5799
drh3c657212009-11-17 23:59:58 +00005800 pIn1 = &aMem[pOp->p1];
5801 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005802 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005803 assert( pIn3->flags&MEM_Int );
5804
drh1b26c7c2009-04-22 02:15:47 +00005805 /* If there is anything other than a rowset object in memory cell P1,
5806 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005807 */
drh733bf1b2009-04-22 00:47:00 +00005808 if( (pIn1->flags & MEM_RowSet)==0 ){
5809 sqlite3VdbeMemSetRowSet(pIn1);
5810 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005811 }
5812
5813 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005814 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005815 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005816 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005817 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005818 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005819 }
5820 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005821 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005822 }
5823 break;
5824}
5825
drh5e00f6c2001-09-13 13:46:56 +00005826
danielk197793758c82005-01-21 08:13:14 +00005827#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005828
drh0fd61352014-02-07 02:29:45 +00005829/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005830**
dan76d462e2009-08-30 11:42:51 +00005831** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005832**
dan76d462e2009-08-30 11:42:51 +00005833** P1 contains the address of the memory cell that contains the first memory
5834** cell in an array of values used as arguments to the sub-program. P2
5835** contains the address to jump to if the sub-program throws an IGNORE
5836** exception using the RAISE() function. Register P3 contains the address
5837** of a memory cell in this (the parent) VM that is used to allocate the
5838** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005839**
5840** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005841**
5842** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005843*/
dan76d462e2009-08-30 11:42:51 +00005844case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005845 int nMem; /* Number of memory registers for sub-program */
5846 int nByte; /* Bytes of runtime space required for sub-program */
5847 Mem *pRt; /* Register to allocate runtime space */
5848 Mem *pMem; /* Used to iterate through memory cells */
5849 Mem *pEnd; /* Last memory cell in new array */
5850 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5851 SubProgram *pProgram; /* Sub-program to execute */
5852 void *t; /* Token identifying trigger */
5853
5854 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005855 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005856 assert( pProgram->nOp>0 );
5857
dan1da40a32009-09-19 17:00:31 +00005858 /* If the p5 flag is clear, then recursive invocation of triggers is
5859 ** disabled for backwards compatibility (p5 is set if this sub-program
5860 ** is really a trigger, not a foreign key action, and the flag set
5861 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005862 **
5863 ** It is recursive invocation of triggers, at the SQL level, that is
5864 ** disabled. In some cases a single trigger may generate more than one
5865 ** SubProgram (if the trigger may be executed with more than one different
5866 ** ON CONFLICT algorithm). SubProgram structures associated with a
5867 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005868 ** variable. */
5869 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005870 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005871 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5872 if( pFrame ) break;
5873 }
5874
danf5894502009-10-07 18:41:19 +00005875 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005876 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00005877 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00005878 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00005879 }
5880
5881 /* Register pRt is used to store the memory required to save the state
5882 ** of the current program, and the memory required at runtime to execute
5883 ** the trigger program. If this trigger has been fired before, then pRt
5884 ** is already allocated. Otherwise, it must be initialized. */
5885 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005886 /* SubProgram.nMem is set to the number of memory cells used by the
5887 ** program stored in SubProgram.aOp. As well as these, one memory
5888 ** cell is required for each cursor used by the program. Set local
5889 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5890 */
dan65a7cd12009-09-01 12:16:01 +00005891 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00005892 assert( nMem>0 );
5893 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00005894 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005895 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00005896 + pProgram->nCsr * sizeof(VdbeCursor*)
5897 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00005898 pFrame = sqlite3DbMallocZero(db, nByte);
5899 if( !pFrame ){
5900 goto no_mem;
5901 }
5902 sqlite3VdbeMemRelease(pRt);
5903 pRt->flags = MEM_Frame;
5904 pRt->u.pFrame = pFrame;
5905
5906 pFrame->v = p;
5907 pFrame->nChildMem = nMem;
5908 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00005909 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00005910 pFrame->aMem = p->aMem;
5911 pFrame->nMem = p->nMem;
5912 pFrame->apCsr = p->apCsr;
5913 pFrame->nCursor = p->nCursor;
5914 pFrame->aOp = p->aOp;
5915 pFrame->nOp = p->nOp;
5916 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00005917#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005918 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00005919#endif
dan165921a2009-08-28 18:53:45 +00005920
5921 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5922 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005923 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005924 pMem->db = db;
5925 }
5926 }else{
5927 pFrame = pRt->u.pFrame;
drh9f6168b2016-03-19 23:32:58 +00005928 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
5929 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00005930 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00005931 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00005932 }
5933
5934 p->nFrame++;
5935 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00005936 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00005937 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00005938 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00005939 assert( pFrame->pAuxData==0 );
5940 pFrame->pAuxData = p->pAuxData;
5941 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00005942 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005943 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00005944 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00005945 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005946 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00005947 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00005948 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00005949 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00005950 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005951 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00005952#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005953 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00005954#endif
drhf56fa462015-04-13 21:39:54 +00005955 pOp = &aOp[-1];
dan165921a2009-08-28 18:53:45 +00005956
5957 break;
5958}
5959
dan76d462e2009-08-30 11:42:51 +00005960/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005961**
dan76d462e2009-08-30 11:42:51 +00005962** This opcode is only ever present in sub-programs called via the
5963** OP_Program instruction. Copy a value currently stored in a memory
5964** cell of the calling (parent) frame to cell P2 in the current frames
5965** address space. This is used by trigger programs to access the new.*
5966** and old.* values.
dan165921a2009-08-28 18:53:45 +00005967**
dan76d462e2009-08-30 11:42:51 +00005968** The address of the cell in the parent frame is determined by adding
5969** the value of the P1 argument to the value of the P1 argument to the
5970** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005971*/
drh27a348c2015-04-13 19:14:06 +00005972case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00005973 VdbeFrame *pFrame;
5974 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00005975 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00005976 pFrame = p->pFrame;
5977 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005978 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5979 break;
5980}
5981
danielk197793758c82005-01-21 08:13:14 +00005982#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005983
dan1da40a32009-09-19 17:00:31 +00005984#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005985/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005986** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005987**
dan0ff297e2009-09-25 17:03:14 +00005988** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5989** If P1 is non-zero, the database constraint counter is incremented
5990** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005991** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005992*/
dan32b09f22009-09-23 17:29:59 +00005993case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00005994 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00005995 db->nDeferredImmCons += pOp->p2;
5996 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005997 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005998 }else{
dan0ff297e2009-09-25 17:03:14 +00005999 p->nFkConstraint += pOp->p2;
6000 }
6001 break;
6002}
6003
6004/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006005** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006006**
6007** This opcode tests if a foreign key constraint-counter is currently zero.
6008** If so, jump to instruction P2. Otherwise, fall through to the next
6009** instruction.
6010**
6011** If P1 is non-zero, then the jump is taken if the database constraint-counter
6012** is zero (the one that counts deferred constraint violations). If P1 is
6013** zero, the jump is taken if the statement constraint-counter is zero
6014** (immediate foreign key constraint violations).
6015*/
6016case OP_FkIfZero: { /* jump */
6017 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006018 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006019 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006020 }else{
drh688852a2014-02-17 22:40:43 +00006021 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006022 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006023 }
dan1da40a32009-09-19 17:00:31 +00006024 break;
6025}
6026#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6027
drh205f48e2004-11-05 00:43:11 +00006028#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006029/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006030** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006031**
dan76d462e2009-08-30 11:42:51 +00006032** P1 is a register in the root frame of this VM (the root frame is
6033** different from the current frame if this instruction is being executed
6034** within a sub-program). Set the value of register P1 to the maximum of
6035** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006036**
6037** This instruction throws an error if the memory cell is not initially
6038** an integer.
6039*/
dan76d462e2009-08-30 11:42:51 +00006040case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006041 VdbeFrame *pFrame;
6042 if( p->pFrame ){
6043 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6044 pIn1 = &pFrame->aMem[pOp->p1];
6045 }else{
drha6c2ed92009-11-14 23:22:23 +00006046 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006047 }
drh2b4ded92010-09-27 21:09:31 +00006048 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006049 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006050 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006051 sqlite3VdbeMemIntegerify(pIn2);
6052 if( pIn1->u.i<pIn2->u.i){
6053 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006054 }
6055 break;
6056}
6057#endif /* SQLITE_OMIT_AUTOINCREMENT */
6058
drh8b0cf382015-10-06 21:07:06 +00006059/* Opcode: IfPos P1 P2 P3 * *
6060** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006061**
drh16897072015-03-07 00:57:37 +00006062** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006063** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006064** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006065**
drh16897072015-03-07 00:57:37 +00006066** If the initial value of register P1 is less than 1, then the
6067** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006068*/
drh9cbf3422008-01-17 16:22:13 +00006069case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006070 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006071 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006072 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006073 if( pIn1->u.i>0 ){
6074 pIn1->u.i -= pOp->p3;
6075 goto jump_to_p2;
6076 }
drhec7429a2005-10-06 16:53:14 +00006077 break;
6078}
6079
drhcc2fa4c2016-01-25 15:57:29 +00006080/* Opcode: OffsetLimit P1 P2 P3 * *
6081** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006082**
drhcc2fa4c2016-01-25 15:57:29 +00006083** This opcode performs a commonly used computation associated with
6084** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6085** holds the offset counter. The opcode computes the combined value
6086** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6087** value computed is the total number of rows that will need to be
6088** visited in order to complete the query.
6089**
6090** If r[P3] is zero or negative, that means there is no OFFSET
6091** and r[P2] is set to be the value of the LIMIT, r[P1].
6092**
6093** if r[P1] is zero or negative, that means there is no LIMIT
6094** and r[P2] is set to -1.
6095**
6096** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006097*/
drhcc2fa4c2016-01-25 15:57:29 +00006098case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006099 i64 x;
drh3c657212009-11-17 23:59:58 +00006100 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006101 pIn3 = &aMem[pOp->p3];
6102 pOut = out2Prerelease(p, pOp);
6103 assert( pIn1->flags & MEM_Int );
6104 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006105 x = pIn1->u.i;
6106 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6107 /* If the LIMIT is less than or equal to zero, loop forever. This
6108 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6109 ** also loop forever. This is undocumented. In fact, one could argue
6110 ** that the loop should terminate. But assuming 1 billion iterations
6111 ** per second (far exceeding the capabilities of any current hardware)
6112 ** it would take nearly 300 years to actually reach the limit. So
6113 ** looping forever is a reasonable approximation. */
6114 pOut->u.i = -1;
6115 }else{
6116 pOut->u.i = x;
6117 }
drh15007a92006-01-08 18:10:17 +00006118 break;
6119}
6120
drhf99dd352016-12-18 17:42:00 +00006121/* Opcode: IfNotZero P1 P2 * * *
6122** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006123**
drh16897072015-03-07 00:57:37 +00006124** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006125** initially greater than zero, then decrement the value in register P1.
6126** If it is non-zero (negative or positive) and then also jump to P2.
6127** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006128*/
drh16897072015-03-07 00:57:37 +00006129case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006130 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006131 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006132 VdbeBranchTaken(pIn1->u.i<0, 2);
6133 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006134 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006135 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006136 }
6137 break;
6138}
6139
6140/* Opcode: DecrJumpZero P1 P2 * * *
6141** Synopsis: if (--r[P1])==0 goto P2
6142**
drhab5be2e2016-11-30 05:08:59 +00006143** Register P1 must hold an integer. Decrement the value in P1
6144** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006145*/
6146case OP_DecrJumpZero: { /* jump, in1 */
6147 pIn1 = &aMem[pOp->p1];
6148 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006149 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6150 VdbeBranchTaken(pIn1->u.i==0, 2);
6151 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006152 break;
6153}
6154
drh16897072015-03-07 00:57:37 +00006155
drhe2d9e7c2015-06-26 18:47:53 +00006156/* Opcode: AggStep0 * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006157** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006158**
drh0bce8352002-02-28 00:41:10 +00006159** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00006160** function has P5 arguments. P4 is a pointer to the FuncDef
drhe2d9e7c2015-06-26 18:47:53 +00006161** structure that specifies the function. Register P3 is the
6162** accumulator.
drhe5095352002-02-24 03:25:14 +00006163**
drh98757152008-01-09 23:04:12 +00006164** The P5 arguments are taken from register P2 and its
6165** successors.
drhe5095352002-02-24 03:25:14 +00006166*/
drhe2d9e7c2015-06-26 18:47:53 +00006167/* Opcode: AggStep * P2 P3 P4 P5
6168** Synopsis: accum=r[P3] step(r[P2@P5])
6169**
6170** Execute the step function for an aggregate. The
6171** function has P5 arguments. P4 is a pointer to an sqlite3_context
6172** object that is used to run the function. Register P3 is
6173** as the accumulator.
6174**
6175** The P5 arguments are taken from register P2 and its
6176** successors.
6177**
6178** This opcode is initially coded as OP_AggStep0. On first evaluation,
6179** the FuncDef stored in P4 is converted into an sqlite3_context and
6180** the opcode is changed. In this way, the initialization of the
6181** sqlite3_context only happens once, instead of on each call to the
6182** step function.
6183*/
drh9c7c9132015-06-26 18:16:52 +00006184case OP_AggStep0: {
drh856c1032009-06-02 15:21:42 +00006185 int n;
drh9c7c9132015-06-26 18:16:52 +00006186 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006187
drh9c7c9132015-06-26 18:16:52 +00006188 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006189 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006190 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6191 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006192 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh575fad62016-02-05 13:38:36 +00006193 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
drh9c7c9132015-06-26 18:16:52 +00006194 if( pCtx==0 ) goto no_mem;
6195 pCtx->pMem = 0;
6196 pCtx->pFunc = pOp->p4.pFunc;
6197 pCtx->iOp = (int)(pOp - aOp);
6198 pCtx->pVdbe = p;
6199 pCtx->argc = n;
6200 pOp->p4type = P4_FUNCCTX;
6201 pOp->p4.pCtx = pCtx;
6202 pOp->opcode = OP_AggStep;
6203 /* Fall through into OP_AggStep */
6204}
6205case OP_AggStep: {
6206 int i;
6207 sqlite3_context *pCtx;
6208 Mem *pMem;
6209 Mem t;
6210
6211 assert( pOp->p4type==P4_FUNCCTX );
6212 pCtx = pOp->p4.pCtx;
6213 pMem = &aMem[pOp->p3];
6214
6215 /* If this function is inside of a trigger, the register array in aMem[]
6216 ** might change from one evaluation to the next. The next block of code
6217 ** checks to see if the register array has changed, and if so it
6218 ** reinitializes the relavant parts of the sqlite3_context object */
6219 if( pCtx->pMem != pMem ){
6220 pCtx->pMem = pMem;
6221 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6222 }
6223
6224#ifdef SQLITE_DEBUG
6225 for(i=0; i<pCtx->argc; i++){
6226 assert( memIsValid(pCtx->argv[i]) );
6227 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6228 }
6229#endif
6230
drhabfcea22005-09-06 20:36:48 +00006231 pMem->n++;
drhd3b74202014-09-17 16:41:15 +00006232 sqlite3VdbeMemInit(&t, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006233 pCtx->pOut = &t;
6234 pCtx->fErrorOrAux = 0;
6235 pCtx->skipFlag = 0;
drh2d801512016-01-14 22:19:58 +00006236 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
drh9c7c9132015-06-26 18:16:52 +00006237 if( pCtx->fErrorOrAux ){
6238 if( pCtx->isError ){
6239 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
6240 rc = pCtx->isError;
6241 }
6242 sqlite3VdbeMemRelease(&t);
drh9467abf2016-02-17 18:44:11 +00006243 if( rc ) goto abort_due_to_error;
drh9c7c9132015-06-26 18:16:52 +00006244 }else{
6245 assert( t.flags==MEM_Null );
drh1350b032002-02-27 19:00:20 +00006246 }
drh9c7c9132015-06-26 18:16:52 +00006247 if( pCtx->skipFlag ){
drh7a957892012-02-02 17:35:43 +00006248 assert( pOp[-1].opcode==OP_CollSeq );
6249 i = pOp[-1].p1;
6250 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6251 }
drh5e00f6c2001-09-13 13:46:56 +00006252 break;
6253}
6254
drh98757152008-01-09 23:04:12 +00006255/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006256** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006257**
drh13449892005-09-07 21:22:45 +00006258** Execute the finalizer function for an aggregate. P1 is
6259** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00006260**
6261** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006262** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006263** argument is not used by this opcode. It is only there to disambiguate
6264** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00006265** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00006266** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006267*/
drh9cbf3422008-01-17 16:22:13 +00006268case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006269 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006270 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006271 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006272 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00006273 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00006274 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006275 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006276 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006277 }
drh2dca8682008-03-21 17:13:13 +00006278 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006279 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006280 if( sqlite3VdbeMemTooBig(pMem) ){
6281 goto too_big;
6282 }
drh5e00f6c2001-09-13 13:46:56 +00006283 break;
6284}
6285
dan5cf53532010-05-01 16:40:20 +00006286#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006287/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006288**
6289** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006290** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6291** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006292** SQLITE_BUSY or not, respectively. Write the number of pages in the
6293** WAL after the checkpoint into mem[P3+1] and the number of pages
6294** in the WAL that have been checkpointed after the checkpoint
6295** completes into mem[P3+2]. However on an error, mem[P3+1] and
6296** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006297*/
6298case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006299 int i; /* Loop counter */
6300 int aRes[3]; /* Results */
6301 Mem *pMem; /* Write results here */
6302
drh9e92a472013-06-27 17:40:30 +00006303 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006304 aRes[0] = 0;
6305 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006306 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6307 || pOp->p2==SQLITE_CHECKPOINT_FULL
6308 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006309 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006310 );
drh30aa3b92011-02-07 23:56:01 +00006311 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006312 if( rc ){
6313 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006314 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006315 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006316 }
drh30aa3b92011-02-07 23:56:01 +00006317 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6318 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6319 }
dan7c246102010-04-12 19:00:29 +00006320 break;
6321};
dan5cf53532010-05-01 16:40:20 +00006322#endif
drh5e00f6c2001-09-13 13:46:56 +00006323
drhcac29a62010-07-02 19:36:52 +00006324#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006325/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006326**
6327** Change the journal mode of database P1 to P3. P3 must be one of the
6328** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6329** modes (delete, truncate, persist, off and memory), this is a simple
6330** operation. No IO is required.
6331**
6332** If changing into or out of WAL mode the procedure is more complicated.
6333**
6334** Write a string containing the final journal-mode to register P2.
6335*/
drh27a348c2015-04-13 19:14:06 +00006336case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006337 Btree *pBt; /* Btree to change journal mode of */
6338 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006339 int eNew; /* New journal mode */
6340 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006341#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006342 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006343#endif
dane04dc882010-04-20 18:53:15 +00006344
drh27a348c2015-04-13 19:14:06 +00006345 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006346 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006347 assert( eNew==PAGER_JOURNALMODE_DELETE
6348 || eNew==PAGER_JOURNALMODE_TRUNCATE
6349 || eNew==PAGER_JOURNALMODE_PERSIST
6350 || eNew==PAGER_JOURNALMODE_OFF
6351 || eNew==PAGER_JOURNALMODE_MEMORY
6352 || eNew==PAGER_JOURNALMODE_WAL
6353 || eNew==PAGER_JOURNALMODE_QUERY
6354 );
6355 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006356 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006357
dane04dc882010-04-20 18:53:15 +00006358 pBt = db->aDb[pOp->p1].pBt;
6359 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006360 eOld = sqlite3PagerGetJournalMode(pPager);
6361 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6362 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006363
6364#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006365 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006366
drhd80b2332010-05-01 00:59:37 +00006367 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006368 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006369 */
6370 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006371 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006372 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006373 ){
drh0b9b4302010-06-11 17:01:24 +00006374 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006375 }
6376
drh0b9b4302010-06-11 17:01:24 +00006377 if( (eNew!=eOld)
6378 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6379 ){
danc0537fe2013-06-28 19:41:43 +00006380 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006381 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006382 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006383 "cannot change %s wal mode from within a transaction",
6384 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6385 );
drh9467abf2016-02-17 18:44:11 +00006386 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006387 }else{
6388
6389 if( eOld==PAGER_JOURNALMODE_WAL ){
6390 /* If leaving WAL mode, close the log file. If successful, the call
6391 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6392 ** file. An EXCLUSIVE lock may still be held on the database file
6393 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006394 */
dan7fb89902016-08-12 16:21:15 +00006395 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006396 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006397 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006398 }
drh242c4f72010-06-22 14:49:39 +00006399 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6400 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6401 ** as an intermediate */
6402 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006403 }
6404
6405 /* Open a transaction on the database file. Regardless of the journal
6406 ** mode, this transaction always uses a rollback journal.
6407 */
6408 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6409 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006410 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006411 }
6412 }
6413 }
dan5cf53532010-05-01 16:40:20 +00006414#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006415
drh9467abf2016-02-17 18:44:11 +00006416 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006417 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006418
dane04dc882010-04-20 18:53:15 +00006419 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006420 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006421 pOut->n = sqlite3Strlen30(pOut->z);
6422 pOut->enc = SQLITE_UTF8;
6423 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006424 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006425 break;
drhcac29a62010-07-02 19:36:52 +00006426};
6427#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006428
drhfdbcdee2007-03-27 14:44:50 +00006429#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh9ef5e772016-08-19 14:20:56 +00006430/* Opcode: Vacuum P1 * * * *
drh6f8c91c2003-12-07 00:24:35 +00006431**
drh9ef5e772016-08-19 14:20:56 +00006432** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6433** for an attached database. The "temp" database may not be vacuumed.
drh6f8c91c2003-12-07 00:24:35 +00006434*/
drh9cbf3422008-01-17 16:22:13 +00006435case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006436 assert( p->readOnly==0 );
drh9ef5e772016-08-19 14:20:56 +00006437 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006438 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006439 break;
6440}
drh154d4b22006-09-21 11:02:16 +00006441#endif
drh6f8c91c2003-12-07 00:24:35 +00006442
danielk1977dddbcdc2007-04-26 14:42:34 +00006443#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006444/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006445**
6446** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006447** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006448** P2. Otherwise, fall through to the next instruction.
6449*/
drh9cbf3422008-01-17 16:22:13 +00006450case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006451 Btree *pBt;
6452
6453 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006454 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006455 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006456 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006457 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006458 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006459 if( rc ){
6460 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006461 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006462 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006463 }
6464 break;
6465}
6466#endif
6467
drh98757152008-01-09 23:04:12 +00006468/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006469**
drh25df48d2014-07-22 14:58:12 +00006470** Cause precompiled statements to expire. When an expired statement
6471** is executed using sqlite3_step() it will either automatically
6472** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6473** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006474**
6475** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006476** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00006477*/
drh9cbf3422008-01-17 16:22:13 +00006478case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00006479 if( !pOp->p1 ){
6480 sqlite3ExpirePreparedStatements(db);
6481 }else{
6482 p->expired = 1;
6483 }
6484 break;
6485}
6486
danielk1977c00da102006-01-07 13:21:04 +00006487#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006488/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006489** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006490**
6491** Obtain a lock on a particular table. This instruction is only used when
6492** the shared-cache feature is enabled.
6493**
danielk197796d48e92009-06-29 06:00:37 +00006494** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006495** on which the lock is acquired. A readlock is obtained if P3==0 or
6496** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006497**
6498** P2 contains the root-page of the table to lock.
6499**
drh66a51672008-01-03 00:01:23 +00006500** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006501** used to generate an error message if the lock cannot be obtained.
6502*/
drh9cbf3422008-01-17 16:22:13 +00006503case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006504 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00006505 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00006506 int p1 = pOp->p1;
6507 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006508 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006509 assert( isWriteLock==0 || isWriteLock==1 );
6510 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006511 if( rc ){
6512 if( (rc&0xFF)==SQLITE_LOCKED ){
6513 const char *z = pOp->p4.z;
6514 sqlite3VdbeError(p, "database table is locked: %s", z);
6515 }
6516 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006517 }
danielk1977c00da102006-01-07 13:21:04 +00006518 }
6519 break;
6520}
drhb9bb7c12006-06-11 23:41:55 +00006521#endif /* SQLITE_OMIT_SHARED_CACHE */
6522
6523#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006524/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006525**
danielk19773e3a84d2008-08-01 17:37:40 +00006526** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6527** xBegin method for that table.
6528**
6529** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006530** within a callback to a virtual table xSync() method. If it is, the error
6531** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006532*/
drh9cbf3422008-01-17 16:22:13 +00006533case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006534 VTable *pVTab;
6535 pVTab = pOp->p4.pVtab;
6536 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006537 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006538 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006539 break;
6540}
6541#endif /* SQLITE_OMIT_VIRTUALTABLE */
6542
6543#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006544/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006545**
dan73779452015-03-19 18:56:17 +00006546** P2 is a register that holds the name of a virtual table in database
6547** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006548*/
drh9cbf3422008-01-17 16:22:13 +00006549case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006550 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006551 const char *zTab; /* Name of the virtual table */
6552
dan73779452015-03-19 18:56:17 +00006553 memset(&sMem, 0, sizeof(sMem));
6554 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006555 /* Because P2 is always a static string, it is impossible for the
6556 ** sqlite3VdbeMemCopy() to fail */
6557 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6558 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006559 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006560 assert( rc==SQLITE_OK );
6561 zTab = (const char*)sqlite3_value_text(&sMem);
6562 assert( zTab || db->mallocFailed );
6563 if( zTab ){
6564 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006565 }
6566 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006567 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006568 break;
6569}
6570#endif /* SQLITE_OMIT_VIRTUALTABLE */
6571
6572#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006573/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006574**
drh66a51672008-01-03 00:01:23 +00006575** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006576** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006577*/
drh9cbf3422008-01-17 16:22:13 +00006578case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006579 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006580 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006581 db->nVDestroy--;
drh9467abf2016-02-17 18:44:11 +00006582 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006583 break;
6584}
6585#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006586
drh9eff6162006-06-12 21:59:13 +00006587#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006588/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006589**
drh66a51672008-01-03 00:01:23 +00006590** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006591** P1 is a cursor number. This opcode opens a cursor to the virtual
6592** table and stores that cursor in P1.
6593*/
drh9cbf3422008-01-17 16:22:13 +00006594case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006595 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006596 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006597 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006598 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006599
drh1713afb2013-06-28 01:24:57 +00006600 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006601 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006602 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006603 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006604 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6605 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006606 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006607 }
6608 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006609 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006610 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006611 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006612
drh9467abf2016-02-17 18:44:11 +00006613 /* Initialize sqlite3_vtab_cursor base class */
6614 pVCur->pVtab = pVtab;
6615
6616 /* Initialize vdbe cursor object */
6617 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6618 if( pCur ){
6619 pCur->uc.pVCur = pVCur;
6620 pVtab->nRef++;
6621 }else{
6622 assert( db->mallocFailed );
6623 pModule->xClose(pVCur);
6624 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006625 }
drh9eff6162006-06-12 21:59:13 +00006626 break;
6627}
6628#endif /* SQLITE_OMIT_VIRTUALTABLE */
6629
6630#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006631/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006632** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006633**
6634** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6635** the filtered result set is empty.
6636**
drh66a51672008-01-03 00:01:23 +00006637** P4 is either NULL or a string that was generated by the xBestIndex
6638** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006639** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006640**
drh9eff6162006-06-12 21:59:13 +00006641** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006642** by P1. The integer query plan parameter to xFilter is stored in register
6643** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006644** xFilter method. Registers P3+2..P3+1+argc are the argc
6645** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006646** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006647**
danielk19776dbee812008-01-03 18:39:41 +00006648** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006649*/
drh9cbf3422008-01-17 16:22:13 +00006650case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006651 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006652 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006653 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006654 Mem *pQuery;
6655 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006656 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006657 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006658 VdbeCursor *pCur;
6659 int res;
6660 int i;
6661 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006662
drha6c2ed92009-11-14 23:22:23 +00006663 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006664 pArgc = &pQuery[1];
6665 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006666 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006667 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006668 assert( pCur->eCurType==CURTYPE_VTAB );
6669 pVCur = pCur->uc.pVCur;
6670 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006671 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006672
drh9cbf3422008-01-17 16:22:13 +00006673 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006674 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006675 nArg = (int)pArgc->u.i;
6676 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006677
drh644a5292006-12-20 14:53:38 +00006678 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006679 res = 0;
6680 apArg = p->apArg;
6681 for(i = 0; i<nArg; i++){
6682 apArg[i] = &pArgc[i+1];
6683 }
drhc960dcb2015-11-20 19:22:01 +00006684 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006685 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006686 if( rc ) goto abort_due_to_error;
6687 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006688 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006689 VdbeBranchTaken(res!=0,2);
6690 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006691 break;
6692}
6693#endif /* SQLITE_OMIT_VIRTUALTABLE */
6694
6695#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006696/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00006697** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006698**
drh2133d822008-01-03 18:44:59 +00006699** Store the value of the P2-th column of
6700** the row of the virtual-table that the
6701** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00006702*/
6703case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006704 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006705 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006706 Mem *pDest;
6707 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006708
drhdfe88ec2008-11-03 20:55:06 +00006709 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006710 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00006711 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006712 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006713 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006714 if( pCur->nullRow ){
6715 sqlite3VdbeMemSetNull(pDest);
6716 break;
6717 }
drhc960dcb2015-11-20 19:22:01 +00006718 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006719 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006720 assert( pModule->xColumn );
6721 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006722 sContext.pOut = pDest;
6723 MemSetTypeFlag(pDest, MEM_Null);
drhc960dcb2015-11-20 19:22:01 +00006724 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006725 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00006726 if( sContext.isError ){
6727 rc = sContext.isError;
6728 }
drh9bd038f2014-08-27 14:14:06 +00006729 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006730 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006731 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006732
drhde4fcfd2008-01-19 23:50:26 +00006733 if( sqlite3VdbeMemTooBig(pDest) ){
6734 goto too_big;
6735 }
drh9467abf2016-02-17 18:44:11 +00006736 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00006737 break;
6738}
6739#endif /* SQLITE_OMIT_VIRTUALTABLE */
6740
6741#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006742/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006743**
6744** Advance virtual table P1 to the next row in its result set and
6745** jump to instruction P2. Or, if the virtual table has reached
6746** the end of its result set, then fall through to the next instruction.
6747*/
drh9cbf3422008-01-17 16:22:13 +00006748case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006749 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006750 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006751 int res;
drh856c1032009-06-02 15:21:42 +00006752 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006753
drhc54a6172009-06-02 16:06:03 +00006754 res = 0;
drh856c1032009-06-02 15:21:42 +00006755 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006756 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00006757 if( pCur->nullRow ){
6758 break;
6759 }
drhc960dcb2015-11-20 19:22:01 +00006760 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006761 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006762 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006763
drhde4fcfd2008-01-19 23:50:26 +00006764 /* Invoke the xNext() method of the module. There is no way for the
6765 ** underlying implementation to return an error if one occurs during
6766 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6767 ** data is available) and the error code returned when xColumn or
6768 ** some other method is next invoked on the save virtual table cursor.
6769 */
drhc960dcb2015-11-20 19:22:01 +00006770 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00006771 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006772 if( rc ) goto abort_due_to_error;
6773 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00006774 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006775 if( !res ){
6776 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00006777 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00006778 }
drh49afe3a2013-07-10 03:05:14 +00006779 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006780}
6781#endif /* SQLITE_OMIT_VIRTUALTABLE */
6782
danielk1977182c4ba2007-06-27 15:53:34 +00006783#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006784/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006785**
drh66a51672008-01-03 00:01:23 +00006786** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006787** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006788** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006789*/
drh9cbf3422008-01-17 16:22:13 +00006790case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006791 sqlite3_vtab *pVtab;
6792 Mem *pName;
6793
danielk1977595a5232009-07-24 17:58:53 +00006794 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006795 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006796 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006797 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006798 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006799 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006800 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006801 testcase( pName->enc==SQLITE_UTF8 );
6802 testcase( pName->enc==SQLITE_UTF16BE );
6803 testcase( pName->enc==SQLITE_UTF16LE );
6804 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00006805 if( rc ) goto abort_due_to_error;
6806 rc = pVtab->pModule->xRename(pVtab, pName->z);
6807 sqlite3VtabImportErrmsg(p, pVtab);
6808 p->expired = 0;
6809 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00006810 break;
6811}
6812#endif
drh4cbdda92006-06-14 19:00:20 +00006813
6814#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006815/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006816** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006817**
drh66a51672008-01-03 00:01:23 +00006818** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006819** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006820** are contiguous memory cells starting at P3 to pass to the xUpdate
6821** invocation. The value in register (P3+P2-1) corresponds to the
6822** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006823**
6824** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006825** The argv[0] element (which corresponds to memory cell P3)
6826** is the rowid of a row to delete. If argv[0] is NULL then no
6827** deletion occurs. The argv[1] element is the rowid of the new
6828** row. This can be NULL to have the virtual table select the new
6829** rowid for itself. The subsequent elements in the array are
6830** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006831**
6832** If P2==1 then no insert is performed. argv[0] is the rowid of
6833** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006834**
6835** P1 is a boolean flag. If it is set to true and the xUpdate call
6836** is successful, then the value returned by sqlite3_last_insert_rowid()
6837** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006838**
6839** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6840** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006841*/
drh9cbf3422008-01-17 16:22:13 +00006842case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006843 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006844 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006845 int nArg;
6846 int i;
6847 sqlite_int64 rowid;
6848 Mem **apArg;
6849 Mem *pX;
6850
danb061d052011-04-25 18:49:57 +00006851 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6852 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6853 );
drh9e92a472013-06-27 17:40:30 +00006854 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006855 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006856 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6857 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006858 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006859 }
6860 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00006861 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006862 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006863 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006864 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006865 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006866 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006867 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006868 assert( memIsValid(pX) );
6869 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00006870 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006871 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006872 }
danb061d052011-04-25 18:49:57 +00006873 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006874 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006875 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006876 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006877 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006878 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00006879 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006880 }
drhd91c1a12013-02-09 13:58:25 +00006881 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006882 if( pOp->p5==OE_Ignore ){
6883 rc = SQLITE_OK;
6884 }else{
6885 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6886 }
6887 }else{
6888 p->nChange++;
6889 }
drh9467abf2016-02-17 18:44:11 +00006890 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00006891 }
drh4cbdda92006-06-14 19:00:20 +00006892 break;
danielk1977399918f2006-06-14 13:03:23 +00006893}
6894#endif /* SQLITE_OMIT_VIRTUALTABLE */
6895
danielk197759a93792008-05-15 17:48:20 +00006896#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6897/* Opcode: Pagecount P1 P2 * * *
6898**
6899** Write the current number of pages in database P1 to memory cell P2.
6900*/
drh27a348c2015-04-13 19:14:06 +00006901case OP_Pagecount: { /* out2 */
6902 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00006903 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006904 break;
6905}
6906#endif
6907
drh60ac3f42010-11-23 18:59:27 +00006908
6909#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6910/* Opcode: MaxPgcnt P1 P2 P3 * *
6911**
6912** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006913** Do not let the maximum page count fall below the current page count and
6914** do not change the maximum page count value if P3==0.
6915**
drh60ac3f42010-11-23 18:59:27 +00006916** Store the maximum page count after the change in register P2.
6917*/
drh27a348c2015-04-13 19:14:06 +00006918case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00006919 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006920 Btree *pBt;
6921
drh27a348c2015-04-13 19:14:06 +00006922 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00006923 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006924 newMax = 0;
6925 if( pOp->p3 ){
6926 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006927 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006928 }
6929 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006930 break;
6931}
6932#endif
6933
drh3e34eab2017-07-19 19:48:40 +00006934/* Opcode: Function0 P1 P2 P3 P4 P5
6935** Synopsis: r[P3]=func(r[P2@P5])
6936**
6937** Invoke a user function (P4 is a pointer to a FuncDef object that
6938** defines the function) with P5 arguments taken from register P2 and
6939** successors. The result of the function is stored in register P3.
6940** Register P3 must not be one of the function inputs.
6941**
6942** P1 is a 32-bit bitmask indicating whether or not each argument to the
6943** function was determined to be constant at compile time. If the first
6944** argument was constant then bit 0 of P1 is set. This is used to determine
6945** whether meta data associated with a user function argument using the
6946** sqlite3_set_auxdata() API may be safely retained until the next
6947** invocation of this opcode.
6948**
6949** See also: Function, AggStep, AggFinal
6950*/
6951/* Opcode: Function P1 P2 P3 P4 P5
6952** Synopsis: r[P3]=func(r[P2@P5])
6953**
6954** Invoke a user function (P4 is a pointer to an sqlite3_context object that
6955** contains a pointer to the function to be run) with P5 arguments taken
6956** from register P2 and successors. The result of the function is stored
6957** in register P3. Register P3 must not be one of the function inputs.
6958**
6959** P1 is a 32-bit bitmask indicating whether or not each argument to the
6960** function was determined to be constant at compile time. If the first
6961** argument was constant then bit 0 of P1 is set. This is used to determine
6962** whether meta data associated with a user function argument using the
6963** sqlite3_set_auxdata() API may be safely retained until the next
6964** invocation of this opcode.
6965**
6966** SQL functions are initially coded as OP_Function0 with P4 pointing
6967** to a FuncDef object. But on first evaluation, the P4 operand is
6968** automatically converted into an sqlite3_context object and the operation
6969** changed to this OP_Function opcode. In this way, the initialization of
6970** the sqlite3_context object occurs only once, rather than once for each
6971** evaluation of the function.
6972**
6973** See also: Function0, AggStep, AggFinal
6974*/
6975case OP_PureFunc0:
6976case OP_Function0: {
6977 int n;
6978 sqlite3_context *pCtx;
6979
6980 assert( pOp->p4type==P4_FUNCDEF );
6981 n = pOp->p5;
6982 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6983 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
6984 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
6985 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
6986 if( pCtx==0 ) goto no_mem;
6987 pCtx->pOut = 0;
6988 pCtx->pFunc = pOp->p4.pFunc;
6989 pCtx->iOp = (int)(pOp - aOp);
6990 pCtx->pVdbe = p;
6991 pCtx->argc = n;
6992 pOp->p4type = P4_FUNCCTX;
6993 pOp->p4.pCtx = pCtx;
6994 assert( OP_PureFunc == OP_PureFunc0+2 );
6995 assert( OP_Function == OP_Function0+2 );
6996 pOp->opcode += 2;
6997 /* Fall through into OP_Function */
6998}
6999case OP_PureFunc:
7000case OP_Function: {
7001 int i;
7002 sqlite3_context *pCtx;
7003
7004 assert( pOp->p4type==P4_FUNCCTX );
7005 pCtx = pOp->p4.pCtx;
7006
7007 /* If this function is inside of a trigger, the register array in aMem[]
7008 ** might change from one evaluation to the next. The next block of code
7009 ** checks to see if the register array has changed, and if so it
7010 ** reinitializes the relavant parts of the sqlite3_context object */
7011 pOut = &aMem[pOp->p3];
7012 if( pCtx->pOut != pOut ){
7013 pCtx->pOut = pOut;
7014 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7015 }
7016
7017 memAboutToChange(p, pOut);
7018#ifdef SQLITE_DEBUG
7019 for(i=0; i<pCtx->argc; i++){
7020 assert( memIsValid(pCtx->argv[i]) );
7021 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7022 }
7023#endif
7024 MemSetTypeFlag(pOut, MEM_Null);
7025 pCtx->fErrorOrAux = 0;
7026 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
7027
7028 /* If the function returned an error, throw an exception */
7029 if( pCtx->fErrorOrAux ){
7030 if( pCtx->isError ){
7031 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
7032 rc = pCtx->isError;
7033 }
7034 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
7035 if( rc ) goto abort_due_to_error;
7036 }
7037
7038 /* Copy the result of the function into register P3 */
7039 if( pOut->flags & (MEM_Str|MEM_Blob) ){
7040 sqlite3VdbeChangeEncoding(pOut, encoding);
7041 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
7042 }
7043
7044 REGISTER_TRACE(pOp->p3, pOut);
7045 UPDATE_MAX_BLOBSIZE(pOut);
7046 break;
7047}
7048
drhf259df52017-12-27 20:38:35 +00007049/* Opcode: Trace P1 P2 * P4 *
7050**
7051** Write P4 on the statement trace output if statement tracing is
7052** enabled.
7053**
7054** Operand P1 must be 0x7fffffff and P2 must positive.
7055*/
drh74588ce2017-09-13 00:13:05 +00007056/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00007057** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00007058**
7059** Programs contain a single instance of this opcode as the very first
7060** opcode.
drh949f9cd2008-01-12 21:35:57 +00007061**
7062** If tracing is enabled (by the sqlite3_trace()) interface, then
7063** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00007064** Or if P4 is blank, use the string returned by sqlite3_sql().
7065**
7066** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00007067**
7068** Increment the value of P1 so that OP_Once opcodes will jump the
7069** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00007070**
7071** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
7072** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00007073*/
drhf259df52017-12-27 20:38:35 +00007074case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00007075case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00007076 char *zTrace;
drh9e5eb9c2016-09-18 16:08:10 +00007077 int i;
drh5fe63bf2016-07-25 02:42:22 +00007078
7079 /* If the P4 argument is not NULL, then it must be an SQL comment string.
7080 ** The "--" string is broken up to prevent false-positives with srcck1.c.
7081 **
7082 ** This assert() provides evidence for:
7083 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
7084 ** would have been returned by the legacy sqlite3_trace() interface by
7085 ** using the X argument when X begins with "--" and invoking
7086 ** sqlite3_expanded_sql(P) otherwise.
7087 */
7088 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00007089
7090 /* OP_Init is always instruction 0 */
7091 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00007092
drhaceb31b2014-02-08 01:40:27 +00007093#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00007094 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00007095 && !p->doingRerun
7096 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7097 ){
drh3d2a5292016-07-13 22:55:01 +00007098#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00007099 if( db->mTrace & SQLITE_TRACE_LEGACY ){
7100 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00007101 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00007102 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00007103 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00007104 }else
drh3d2a5292016-07-13 22:55:01 +00007105#endif
drh7adbcff2017-03-20 15:29:28 +00007106 if( db->nVdbeExec>1 ){
7107 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
7108 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
7109 sqlite3DbFree(db, z);
7110 }else{
drhbd441f72016-07-25 02:31:48 +00007111 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00007112 }
drh949f9cd2008-01-12 21:35:57 +00007113 }
drh8f8b2312013-10-18 20:03:43 +00007114#ifdef SQLITE_USE_FCNTL_TRACE
7115 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7116 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00007117 int j;
7118 for(j=0; j<db->nDb; j++){
7119 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7120 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00007121 }
7122 }
7123#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007124#ifdef SQLITE_DEBUG
7125 if( (db->flags & SQLITE_SqlTrace)!=0
7126 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7127 ){
7128 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7129 }
7130#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007131#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007132 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007133 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00007134 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00007135 for(i=1; i<p->nOp; i++){
7136 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7137 }
7138 pOp->p1 = 0;
7139 }
7140 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00007141 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00007142 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007143}
drh949f9cd2008-01-12 21:35:57 +00007144
drh28935362013-12-07 20:39:19 +00007145#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007146/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007147**
7148** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007149** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7150** to values currently held in registers. TK_COLUMN terms in the P4
7151** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007152*/
7153case OP_CursorHint: {
7154 VdbeCursor *pC;
7155
7156 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7157 assert( pOp->p4type==P4_EXPR );
7158 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007159 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007160 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007161 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7162 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007163 }
drh28935362013-12-07 20:39:19 +00007164 break;
7165}
7166#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007167
7168/* Opcode: Noop * * * * *
7169**
7170** Do nothing. This instruction is often useful as a jump
7171** destination.
drh5e00f6c2001-09-13 13:46:56 +00007172*/
drh91fd4d42008-01-19 20:11:25 +00007173/*
7174** The magic Explain opcode are only inserted when explain==2 (which
7175** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7176** This opcode records information from the optimizer. It is the
7177** the same as a no-op. This opcodesnever appears in a real VM program.
7178*/
7179default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00007180 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00007181 break;
7182}
7183
7184/*****************************************************************************
7185** The cases of the switch statement above this line should all be indented
7186** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7187** readability. From this point on down, the normal indentation rules are
7188** restored.
7189*****************************************************************************/
7190 }
drh6e142f52000-06-08 13:36:40 +00007191
drh7b396862003-01-01 23:06:20 +00007192#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007193 {
drha01c7c72014-04-25 12:35:31 +00007194 u64 endTime = sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007195 if( endTime>start ) pOrigOp->cycles += endTime - start;
7196 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007197 }
drh7b396862003-01-01 23:06:20 +00007198#endif
7199
drh6e142f52000-06-08 13:36:40 +00007200 /* The following code adds nothing to the actual functionality
7201 ** of the program. It is only here for testing and debugging.
7202 ** On the other hand, it does burn CPU cycles every time through
7203 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7204 */
7205#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007206 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007207
drhcf1023c2007-05-08 20:59:49 +00007208#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007209 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007210 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007211 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007212 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007213 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007214 }
drh7cc84c22016-04-11 13:36:42 +00007215 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007216 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007217 }
drh75897232000-05-29 14:26:00 +00007218 }
danielk1977b5402fb2005-01-12 07:15:04 +00007219#endif /* SQLITE_DEBUG */
7220#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007221 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007222
drha05a7222008-01-19 03:35:58 +00007223 /* If we reach this point, it means that execution is finished with
7224 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007225 */
drh9467abf2016-02-17 18:44:11 +00007226abort_due_to_error:
7227 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007228 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007229 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7230 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7231 }
drha05a7222008-01-19 03:35:58 +00007232 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007233 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007234 testcase( sqlite3GlobalConfig.xLog!=0 );
7235 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007236 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007237 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007238 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007239 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007240 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007241 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007242 }
drh900b31e2007-08-28 02:27:51 +00007243
7244 /* This is the only way out of this procedure. We have to
7245 ** release the mutexes on btrees that were acquired at the
7246 ** top. */
7247vdbe_return:
drh77dfd5b2013-08-19 11:15:48 +00007248 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00007249 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007250 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007251 assert( rc!=SQLITE_OK || nExtraDelete==0
7252 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7253 );
drhb86ccfb2003-01-28 23:13:10 +00007254 return rc;
7255
drh023ae032007-05-08 12:12:16 +00007256 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7257 ** is encountered.
7258 */
7259too_big:
drh22c17b82015-05-15 04:13:15 +00007260 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007261 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007262 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007263
drh98640a32007-06-07 19:08:32 +00007264 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007265 */
7266no_mem:
drh4a642b62016-02-05 01:55:27 +00007267 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007268 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007269 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007270 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007271
danielk19776f8a5032004-05-10 10:34:51 +00007272 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007273 ** flag.
7274 */
7275abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007276 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007277 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007278 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007279 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007280 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007281}